I never really feel like i'm any good at using Python unless I am doing cool things with List Comprehension.
Here's a quick example of nested list comprehension I used today to create a new list from a list containing lists of three items
Visually:-
[['mostly cloudy (day)', 'cloudy3.png', 'cloudy3.png'],
['partly cloudy (night)', 'cloudy2_night.png', 'cloudy2_night.png'],
['partly cloudy (day)', 'cloudy2.png', 'cloudy2.png'],
....................................
['Sunny', 'sunny.png', 'sunny.png']]
To this:-
[['mostly cloudy (night)', 'cloudy3_night.png'],
['mostly cloudy (day)', 'cloudy3.png'],
['partly cloudy (night)', 'cloudy2_night.png'],
....................................
['Sunny', 'sunny.png']]
The code then:-
[[a[0],a[1]] for a in theList]
Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
Monday, 4 January 2010
Sunday, 11 October 2009
A Question Of Python Style
Having ended up almost solely with Python at the end of a journey of many languages I have learnt and used; style has been something important to me.
Python has pep8 which states somethings definitively such as:-
* Use 4 spaces per indentation level" (I was actually using 2 until now!)
* Limit all lines to a maximum of 79 characters.
* Separate top-level function and class definitions with two blank lines.
* Method definitions inside a class are separated by a single blank line.
...so things like naming styles are purely at the discretion of the user. Fair enough but i like to be consistent where I can with these things, and coming from a Java background I pretty much stick with a style I picked up from there. Here's some explaination. Things always seemed very well defined in the land of Sun!
http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367
http://geosoft.no/development/javastyle.html
...and so
* a class is ClassName()
* method names are lookBehindYou()
* class attributes are as above
this one from the geosoft link above I like also
* "Abbreviations and acronyms should not be uppercase when used as name.
exportHtmlSource(); // NOT: exportHTMLSource();
openDvdPlayer(); // NOT: openDVDPlayer();"
I've not mentioned doc strings here, the use of pep8 tool, pylint or pyflakes or even given an example class, well I guess that's the beauty of blogging!
This is worth knowing though http://wiki.laptop.org/go/Python_Style_Guide oooh and removing trailing whitespace in vim... thats :%s/\s\+$//
Python has pep8 which states somethings definitively such as:-
* Use 4 spaces per indentation level" (I was actually using 2 until now!)
* Limit all lines to a maximum of 79 characters.
* Separate top-level function and class definitions with two blank lines.
* Method definitions inside a class are separated by a single blank line.
...so things like naming styles are purely at the discretion of the user. Fair enough but i like to be consistent where I can with these things, and coming from a Java background I pretty much stick with a style I picked up from there. Here's some explaination. Things always seemed very well defined in the land of Sun!
http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367
http://geosoft.no/development/javastyle.html
...and so
* a class is ClassName()
* method names are lookBehindYou()
* class attributes are as above
this one from the geosoft link above I like also
* "Abbreviations and acronyms should not be uppercase when used as name.
exportHtmlSource(); // NOT: exportHTMLSource();
openDvdPlayer(); // NOT: openDVDPlayer();"
I've not mentioned doc strings here, the use of pep8 tool, pylint or pyflakes or even given an example class, well I guess that's the beauty of blogging!
This is worth knowing though http://wiki.laptop.org/go/Python_Style_Guide oooh and removing trailing whitespace in vim... thats :%s/\s\+$//
Tuesday, 29 September 2009
Python function args
I rarely use anything beyond named parameters, but this post captures what you need to know.
http://kbyanc.blogspot.com/2007/07/python-aggregating-function-arguments.html
http://kbyanc.blogspot.com/2007/07/python-aggregating-function-arguments.html
Monday, 21 September 2009
Building Python from Source
How easy it is to install python from source? Very.
./configure --prefix=/usr/local/python2.6/
make (took 4mins on my x86_64 dc)
make install
./configure --prefix=/usr/local/python2.6/
make (took 4mins on my x86_64 dc)
make install
Subscribe to:
Posts (Atom)