As an addendum to Safe's notes (thanks, by the way):
eta-reduction (which I couldn't remember on thursday): lambda x. f x --eta-> f
Probably not the best way to multiplay a list by "factor":
map(factor.__mul__, row)
This is the same as the list comprehension:
[x*factor for x in row]
and the list comprehension is way more pythonic.
Converting a string to hex:
"somestring".encode('hex')
Sadly this doesn't work (yet?) in Python 3. Have to use
binascii.hexlify instead, which doesn't work on strings (which are
unicode) only on "bytes". Frankly, I found all that rather annoying.
For iterators: aniterator.next() is replaced with next(aniterator).
"next" has become a builtin function in other words.
In Python 3 it becomes important whether you're using a file IO for
text (strings) or binary (bytes). My tests use binary reads and
writes on a StringIO. Which should be replaced with io.BytesIO in
this case. On no account copy my hilarious "from io import BytesIO as
StringIO".
A minor matter of "except:" syntax:
except ValueError, e:
in Python 3 is:
except ValueError as e:
And you can use the "as" syntax in Python 2.x as well (for Python 2.6
onwards). The "as" syntax avoids the "except ValueError, TypeError"
mistake ("except (ValueError, TypeError):" was probably intended).
Python 3 is more strict with some things (is this why the tab thing
was an issue? I don't want tabs in the file anyway).
Much of the code is not very pythonic, it is heavily influenced by my
inner functional programmer. That leads to some eye-opening
discussions perhaps. This use of itertools.chain was noted (recall
that itertools is my second favourite module):
(not the only use, but typical)
array('B', itertools.chain(*row))
The above code converts a list of rows (each row is a list, so this is
a list of lists) into a single giant array (and using "list(" instead
of "array('B'," would make the result a giant list, similarly for
tuple).
I enjoyed the session. Thanks again to everyone that attended.
Cheers,
drj
--
To post: [email protected]
To unsubscribe: [email protected]
Feeds: http://groups.google.com/group/python-north-west/feeds
More options: http://groups.google.com/group/python-north-west