Semantics changing on whitespace?
July 03, 2008 at 10:41 AM | categories: python, oldblog | View Commentsif True:vs
print "Yay"
print "Woo"
frag. 1
if True:However, generally speaking this does actually mean what people intended it to mean. The common exception is where, you might want to write this:
print "Yay"
print "Woo"
frag. 2
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):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)
def main(self):
while True:
print "Woo"
\
sys.stderr.write("DEBUG - UM, in loop\n")
print "Yay"
frag. 4
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 =10means the same as all of these:
bar = 2
X[foo/bar]
frag. 5
X [foo/bar] X[foo /bar] X [foo /bar] X[foo / bar] X [foo / bar]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:
frag. 6
X[foo/bar] X [foo/bar] X[foo /bar] X [foo /bar] X[foo / bar] X [foo / bar]And it's also reasonable to assume that the following are at least intended to be different:
frag. 7
if (1)vs
printf("Yay\n");
printf("Woo\n");
frag. 8
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:vs
print "Yay"
print "Woo"
#end if
frag. 10 - same as frag 1 after running through pindent.py
if True:And whilst hell would freeze over before the addition of a keyword 'end' to python, it strikes me that being able to write:
print "Yay"
#end if
print "Woo"
frag. 11 - same as frag 2 after running through pindent.py
if True:and
print "Yay"
print "Woo"
end
frag. 12 - same as frag 1 after running through pindent.py
if True:and
print "Yay"
end
print "Woo"
frag. 13 - same as frag 2 after running through pindent.py
class Foo(object):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.
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
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?
Is it just me...
July 02, 2008 at 05:20 PM | categories: python, oldblog | View CommentsDrawing Kamaelia Systems?
June 28, 2008 at 12:29 AM | categories: python, oldblog | View CommentsIf 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.gzRight at this instant all that application does is this:
~/tmp> cd Kamaelia-Scribble-N-Link-0.1.0/
~/tmp> sudo python setup.py install
- 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
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. :-)
Twitter - Why?
June 25, 2008 at 04:33 PM | categories: python, oldblog | View CommentsHack for Mashed - Kamaelia Speak And Learn
June 22, 2008 at 06:10 AM | categories: python, oldblog | View CommentsWhere 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
Mashed Hack - IRC Speaker Bot
June 21, 2008 at 10:52 PM | categories: python, oldblog | View Comments- 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:
Kamaelia Mashed Release Candidate
June 21, 2008 at 08:12 PM | categories: python, oldblog | View CommentsKamaelia at BBC MASHED
June 20, 2008 at 07:53 PM | categories: python, oldblog | View CommentsThe 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 :) )
- 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
- 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
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 & usefulMore detail:
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!"
GET MASHED (Link Kamaelia site)How to get started hacking with Kamaelia - making concurrency fun, easy & useful with Michael Sparks
(12:15 - 12:45) - 21 Jun 08Kamaelia 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!
- Website: http://edit.kamaelia.org/Developers/
- Introduction: http://edit.kamaelia.org/NewIntroduction
- Toybox: http://edit.kamaelia.org/Components
- Cookbook: http://edit.kamaelia.org/Cookbook
- Get Mashed with Kamaelia
Garden Table area at Top West HallDownload iCalendar: How to get started hacking with Kamaelia
Powering a home server using solar power?
May 31, 2008 at 04:07 PM | categories: python, oldblog | View Comments- 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
I'd be interested in hearing from anyone who's tried doing this.
How will you be celebrating the 60th Anniversary of the Computer?
May 30, 2008 at 06:31 PM | categories: python, oldblog | View CommentsWe'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 ?
:-)
« Previous Page -- Next Page »