Semantics changing on whitespace?

July 03, 2008 at 10:41 AM | categories: python, oldblog | View Comments

In python it's clear that the semantics of a piece of code change depending on whitespace - for example:
if True:
    print "Yay"
    print "Woo"
frag. 1
vs
if True:
    print "Yay"
print "Woo"
frag. 2
However, generally speaking this does actually mean what people intended it to mean. The common exception is where, you might want to write this:
class Foo(object):
    def main(self):
         while True:
              print "Woo"
sys.stderr.write("DEBUG - UM, in loop\n")
              print "Yay"
frag. 3

Whereas of course python views the sys.stderr.write line as the end of the while, def, & class blocks. Often people do the above (in non-python languages) because they want to make it easier to find where they've inserted debug code, and lament the lack of it python. As an aside, you can of course do the above in python, if you add an extra line in:
class Foo(object):
    def main(self):
         while True:
              print "Woo"
              \
sys.stderr.write("DEBUG - UM, in loop\n")
              print "Yay"
frag. 4
Since the continuation marker effectively causes the next line to be part of the same line, thereby meaning it's logically indented, even if not in reality. ("sys" is still at the start of the line in the source of course)

However, as far as whitespace goes, I think that's as far as the change in semantics due to white space goes of course *except* that it's also used as a delimiter between tokens. (This is kinda necessary after they found with fortran many years back that allowing whitespace in identifiers was a rather bad idea in practice)

Anyhow, this does tend to mean that the last line of this:
foo =10
bar = 2
X[foo/bar]

frag. 5
means the same as all of these:
X [foo/bar] X[foo /bar] X [foo /bar] X[foo / bar] X [foo / bar]
frag. 6
Whereas apparently in ruby it wouldn't - based on some recent posts. In fact, I think only 2 of them do the same thing. That's actually pretty insane (but then I'm sure people think the same about python's whitespace rules), but clearly a consequence of allowing foo bar to mean something similar (if not identical ?) to foo(bar). However, it also clearly breaches a the rule of least surprise. Whilst the problem with the rule of least surprise is "who is surprised", I think it's reasonable for someone looking at code to assume that the following all do the same things:
X[foo/bar] X [foo/bar] X[foo /bar] X [foo /bar] X[foo / bar] X [foo / bar]
frag. 7
And it's also reasonable to assume that the following are at least intended to be different:
if (1)
   
printf("Yay\n");
    printf("Woo\n");
frag. 8
vs
if (1)
   
printf("Yay\n");
printf("Woo\n");
frag. 9

But of course in C, they aren't. Now that's why most C programmers wouldn't do that, but it's made me wonder. C has this foible, which every C programmer knows about. Ruby has the above foible which I'm guessing most if not all ruby programmers are aware of, but with python it's whitespace semantics (which are intended to actually encourage good behaviour and fix the "problem" with frags 8 vs 9) that everyone knows about and does put people off...

ie The biggest barrier (that I hear of) to adoption of python is the fact that frags 1 & 2 do mean different things. I'm not sure why it's a huge barrier, but it does turn out to be the single factor that turns most people off the language (in my experience...). Whilst you do have something like pindent.py which allows frags 1& 2 to look like this:
if True:
print "Yay"
print "Woo"
#end if
frag. 10 - same as frag 1 after running through pindent.py
vs
if True:
    print "Yay"
#end if
print "Woo"
frag. 11 - same as frag 2 after running through pindent.py
And whilst hell would freeze over before the addition of a keyword 'end' to python, it strikes me that being able to write:
if True:
  print "Yay"
 print "Woo"
end
frag. 12 - same as frag 1 after running through pindent.py
and
if True:
    print "Yay"
end
print "Woo"
frag. 13 - same as frag 2 after running through pindent.py
and
class Foo(object):
    def main(self):
         while True:
              print "Woo"
sys.stderr.write("DEBUG - UM, in loop\n")
              print "Yay"
         end
    end
end
frag. 14 - same as frag 2 after running through pindent.py
Wouldn't actually be the end of the world, and would actually simplify things for beginners, but also simplify things when people are embedding code and copying/pasting code from webpages from archives of lists etc. It'd mean that web templating languages which allow python code inside templates (not a wise idea to mix code with templates really, but it does happen) would be able to use the same syntax, etc.

It would also do away with the one major criticism of python. To make it available, my personal preference is that it would have to be available as a command line switch, which defaults to off. However, as mentioned hell would freeze over before it was added, so the question that springs to mind is "is it worth writing a pre-processor for" ? I can see some benefits in doing so for example it would mean that python was less fussy about things like frags 12 and 14 - both of which have whitespace issues python would scream about. frag 12 has a common mistake - whereas frag 14 contains a common desire. (at least for people who are used to that temporary debug style in many languages)

It'd also mean (perhaps) that resurrecting kids books teaching programming could use python happily without people wondering whether they've counted the dedents correctly - since they'd be able to count up end keywords.

It'd also open the door to handwriting based coding in python... (since indenting 8 "spaces" when writing doesn't make much sense - and your indentation isn't going to be perfect then either)

So the question for me, is it worth writing? I personally suspect it is, and the preprocessor needed would be quite simple to write either from scratch or to derive from pindent.py, but wonder what other people's opinions are. How long did whitespace sensitivity in python stop you learning it? (It put me off for 5 years) Has it stopped you wanting to bother? Do you think such a pre-processor would be a really bad idea?


Read and Post Comments

Is it just me...

July 02, 2008 at 05:20 PM | categories: python, oldblog | View Comments

... or is this a neat way to open a gaping security hole on your server? I can only presume that the backend of said beast only allows "arbitrary" connections to known hosts and ports, or it's a spammer's (and similar) delight.
Read and Post Comments

Drawing Kamaelia Systems?

June 28, 2008 at 12:29 AM | categories: python, oldblog | View Comments

Just been fiddling around with the idea of using the handwriting/gesture recognition code to allow the creation of kamaelia systems using an entirely pen driven approach. At first blush this does actually look like it could be a realistic proposition. The code that enables this in terms of proof of concept is *incredibly* hacky at present, but does seem, on a very basic level, to work. Using our packaging tools, I've packaged this up as an experimental release here:

If you're curious about it and have python & pygame already installed, installation boils down to this:

~/tmp> tar zxvf Kamaelia-Scribble-N-Link-0.1.0.tar.gz
~/tmp> cd Kamaelia-Scribble-N-Link-0.1.0/
~/tmp> sudo python setup.py install
Right at this instant all that application does is this:
  • It will recognise strokes drawn that look like an "o" or a "u" and assume you want to "draw"/add a new unconfigured component
  • Drawing a joined up "x" (ie curvy one like a "backward c going upwards, then forward c downwards") will delete the last component added
  • Drawing a stroke from top left to bottom right will "switch on" the "makelink" mode.
    • The link will start from the next link clicked - eg an outbox
    • The link will terminate with the following link clicked - eg an inbox
And that's pretty much it. No configuration of nodes at this time. The sort of output it will happily create looks like this:


However, it is an interesting start/proof of concept - it certainly is beginning to look like we will be able to literally draw software at some point in the future... Any feedback welcome. :-)
Read and Post Comments

Twitter - Why?

June 25, 2008 at 04:33 PM | categories: python, oldblog | View Comments

Why do you twitter?

Read and Post Comments

Hack for Mashed - Kamaelia Speak And Learn

June 22, 2008 at 06:10 AM | categories: python, oldblog | View Comments

This hack is a "toy" for a small child to assist them to learn to read and write. They are asked to write specific words - both textually and also using speech synthesis. What they write is then read out to them. This version is by no means "complete", but it works. It currently allows a child to learn to write the word cat :-)

Where to Get it:

First initial version here:

Installing:

~/incoming/> tar zxvf Kamaelia\ Speak\ N\ Learn-1.0.0.tar.gz

Where's the Source?

Running:

~> SpeakNLearn.py

Dependencies:

  • You need to have espeak installed
  • You need to have aplay installed
  • You need to have pygame installed
Michael, June 2008
Read and Post Comments

Mashed Hack - IRC Speaker Bot

June 21, 2008 at 10:52 PM | categories: python, oldblog | View Comments

Since I wanted to "sit" on  some IRC channels here at Mashed and rather than flit channels or waste screen estate I decided to do a short hack to listen to the channel. As a hack for mashed, it does the following things:
  • It's IRC bot
  • It joins 3 channels - #kamtest, #kamaelia & #mashed
  • It listens to the channel

Anything that anyone says is then spoken (via espeak) by the bot. ie speech synth.

You can find the code here:
Read and Post Comments

Kamaelia Mashed Release Candidate

June 21, 2008 at 08:12 PM | categories: python, oldblog | View Comments

As per my previous post, the Kamaelia 0.6.0 release candidate #6 is available and can be downloaded from http://edit.kamaelia.org/release/

Read and Post Comments

Kamaelia at BBC MASHED

June 20, 2008 at 07:53 PM | categories: python, oldblog | View Comments

Going to BBC Mashed? Want to go? (a few are tickets left) Interested in one of these : {Kamaelia, Python, Concurrency, Multicore, DVB, Building network systems, Pygame Based Systems, Audio/video based} ? If so, please come along to the talk! If there's a clash, don't worry, I'm starting a page called GetMashed which will contain at least some starting points which I'll cover. (It's not the same as seeing it for yourself though :)

The talk is at the start of the weekend intended to get you started with all of those things. As well as the session itself, a new release of Kamaelia is in the works with the first Beta/Release Candidate made available to an unsuspected world. What's going in the release?
  • Well, the usual slew of extra components and bug fixes
  • A variety of new tools - from video shot change detection, through to SMTP greylisting
  • Multiprocess & hence multicore support (experimental at this stage, but so far so good :) )
And a large number of extra components - this is actually a lot more than I originally expected/anticipated. However they're divided into two halves - new components and components from applications merged into the repository. (the latter means that you can take parts of random Kamaelia applications and embed them in other random Kamaelia applications. As a result: New Components:
  • Kamaelia.
    • Chassis
      • Seq
    • Codec
      • WAV, YUV4MPEG
    • Device
      • DVB
        • SoftDemux
        • Parse
          • ParseEventInformationTable, ParseNetworkInformationTable, ParseProgramAssociationTable, ParseProgramMapTable, ParseServiceDescriptionTable, ParseTimeAndDateTable, ParseTimeOffsetTable, PrettifyTables, ReassemblePSITables
    • Experimental
      • Chassis, ERParsing
    • File
      • MaxSpeedFileReader, UnixProcess2
    • Internet
      • TimeOutCSA
    • Protocol
      • MimeRequestComponent, RecoverOrder, SDP
      • AIM
        • AIMHarness, ChatManager, LoginHandler, OSCARClient
      • HTTP
        • Handlers
          • Minimal, Handlers/SessionExample, UploadTorrents
      • IRC
        • IRCClient
      • RTP
        • NullPayloadPreFramer, NullPayloadRTP, RTCPHeader, RTPHeader, RtpPacker, RTP
      • Util
        • Tokenisation
          • Simple
        • Collate, FirstOnly, Max, OneShot, PromptedTurnstile, RangeFilter, RateChunker, SequentialTransformer, Sync, TagWithSequenceNumber, TwoWaySplitter
      • UI
        • Pygame
          • Text, VideoSurface
      • Video
        • CropAndScale, DetectShotChanges, PixFormatConversion
      • Visualisation
        • ER
          • ERLaws, ERVisualiserServer, ExtraWindowFurniture, PAttribute, PEntity, PISA, PRelation
      • XML
        • SimpleXMLParser
Components merged in from Apps:
  • Kamaelia
    • Apps
      • Compose
        • BuildViewer, CodeGen, PipeBuild, PipelineWriter, GUI
        • GUI
          • ArgumentsPanel, BuilderControlsGUI, TextOutputGUI
      • IRCLogger
        • Support
      • Show
        • GraphSlides
      • Whiteboard
        • Audio, Canvas, CheckpointSequencer, CommandConsole, Entuple, Options, Painter, Palette, Router, Routers, SingleShot, TagFiltering, Tokenisation, TwoWaySplitter, UI
All of this also totally ignores the new examples & new Kamaelia based apps.
Also a new website is coming soon, but time ran out :-)

So given all that, session details:
What: How to get started hacking with Kamaelia  - making concurrency fun, easy & useful
When: 12:15 - 12:45, 21 June 2008
Where: Garden Table area at Top West Hall, Alexandra Palace, London
Who: Me - Michael Sparks :-)
Why: To help people get started with hacking Kamaelie
How: One part presentation, two parts tutorial, lightning talk style and a note of "this is what I look like, come find me!"
More detail:

How to get started hacking with Kamaelia - making concurrency fun, easy & useful with Michael Sparks

Kamaelia is an open source project from BBC R&D. It makes prototyping new and interesting systems simple. Kamaelia systems are naturally concurrent and transform easily into production quality, maintainable systems.

It is useful for things from 3D systems through building PVRs through video playback, through shot change detection, through P2P distribution (live and bit torrent), through whiteboards, handwriting/gesture recognition, speech generation, and games systems, and back through DVB on both the reception side and broadcast side, and lots more not mentioned...

This talk will introduce Kamaelia, helping you get started - complete with a new release to boot!

Discussion Thread in the forum

(12:15 - 12:45) - 21 Jun 08
Garden Table area at Top West Hall

Download iCalendar: How to get started hacking with Kamaelia

GET MASHED (Link Kamaelia site)


Read and Post Comments

Powering a home server using solar power?

May 31, 2008 at 04:07 PM | categories: python, oldblog | View Comments

I'm considering looking at how to do this, since it looks doable, but wonder if anyone has any experience of actually doing this, even partially. It's a very similar problem, it seems, to running a TV using solar power when camping, so it's not that strange a scenario. I've come to the conclusion that one way of doing this is as follows:
  • Use a cheap/old/low power laptop
  • Buy a car battery - or perhaps a 12V 100Ah battery
  • Use a 60W solar panel (or similar) to charge the 100Ah battery
  • Use a car cigarette light connector based power adapter to connect laptop to battery
The issue there is the 60W solar panel is expensive right now, and I'm wondering if there's a way of reducing this figure down lower - if anyone has experience of doing this. I'm also not sure it's sufficient - I suspect it has to be higher rated than that.

I'd be interested in hearing from anyone who's tried doing this.

Read and Post Comments

How will you be celebrating the 60th Anniversary of the Computer?

May 30, 2008 at 06:31 PM | categories: python, oldblog | View Comments

The Small-Scale Experimental Machine, known as SSEM, or the "Baby", was designed and built at The University of Manchester, and made its first successful run of a program on June 21st 1948. It was the first machine that had all the components now classically regarded as characteristic of the basic computer. Most importantly it was the first computer that could store not only data but any (short!) user program in electronic memory and process it at electronic speed. -- http://www.computer50.org/

We're planning a new release of Kamaelia for that day, which also co-incides with BBC Mashed (aka BBC hackday 2). The new release should include, among other things, our multicore support, basic software transactional memory, generator, thread, process based components, a variety of example applications, a revamped website (yes, it needs a lick of paint) as well as a large number of new components from improved DVB support, better pygame tools, etc.

How are you planning on celebrating the 60th anniversary ?

:-)

Read and Post Comments

« Previous Page -- Next Page »