PDA

View Full Version : Coming sooner: Python 2.3


Sir Penguin
14-07-2003, 22:44:49
The first release canditate is pegged for later this month, and the final release is due next month. Not much that's as groundbreaking as in 2.2, but some neat stuff. Here's some from the beta that I found interesting, in maybe some sort of general overall order:

bz2 module: uses the bzip2 algorithm for compression (generally better than zlib)
generators: using the 'yield' keyword in a function instead of 'return' makes the function a generator. The function's local variables are not deallocated when the function yields. Rather, they are kept and when the fct.next() method is called, the function is run again with all the local variables intact (for example, it can start in the middle of a loop). 'return' can still be used, without arguments, to exit a generator function and deallocate its local variables.
universal newlines: opening a file with 'U' appended to the mode will convert all \r and \r\n characters to the \n character.
logging: the new logging package makes it easier to write debugging info, general info, warnings, errors, etc. to a log. It also includes an exception() method that records the latest exception.
timeit module: a new module. Its contents are used to find the execution time of a block of Python code (passed to the function as a string) on the local machine.
extended slicing: slices can now contain a step argument, like that in the range(b,e,s) function ( speaking of which, iterating through the xrange() function is now faster than iterating through range() ). For example, 'ar[0:9:2]' returns [the elements of 'ar' at indexes 0,2,4,6,8].
there's a builtin function sum() that returns the sum of all the elements of a sequence of numbers. It can't be used to concatenate strings, though.
you can look for substrings in a string using the 'in' keyword! Before, you could only search for a single character. Now, you can search for complete strings (for example, ' if "eng" in "Penguin" ' returns true).
you can tell the strip() method what characters to strip.
the sort() method is faster than before
arrays can be used with the += and *= assignments (the first adds to the array, the second replicates the array)
there's a new heapq module that uses a heap to store a queue. Way more efficient that using a list.
the math.log() function now takes an argument that specifies the base of the logarithm! No more messing around with logb (x) / logb (a) or whatever the hell it is.
the math module now has the functions radians() and degrees(), for converting between radians and degrees.
there's a new 'platform' module that you can use to get all sorts of useful information about the platform you're on (os version, cpu type, etc.)
there's now a random.sample(p,n) method that selects n elements of the sequence p and returns them in random order (this can be used to randomise a sequence in place). Also, a new randomisation routine was written, but I don't remember where it's used.
the new 'textwrap' module can be used to wrap long strings to a specified width.

Some nifty stuff! Distilled from: http://www.python.org/doc/2.3b2/whatsnew/

SP

Sir Penguin
14-07-2003, 22:45:30
Pity post.

SP

Deacon
19-07-2003, 00:38:05
Python 2.2 is part of my BLFS setup. Whenever GNOME gets straightened out, I'll be ready. But I've been knocked offline for a few days by modem damage. And this also happened as I was wiping my machine to re-install everything..

Qaj the Fuzzy Love Worm
19-07-2003, 01:50:27
<deadpan>Wooooooow</deadpan>

Deacon
19-07-2003, 02:53:41
But the good news is that I have a new, cheap, but fast PCI modem. It's got a PCTel PCT789 chipset. It works in Windows, and it even works in Linux. All you have to do is compile your kernel (no need to install it), then unpack and build the driver. After the build, insmod pctel, then insmod ptserial. Run dmesg, and you should see the gory details of loading the driver. If it's successful, it'll say something to the effect that your modem is on /dev/ttyS15. The install even creates a symlink to /dev/modem. Nifty. :)

Deacon
19-07-2003, 02:58:58
And I now have my line running through a surge protector. Let's hope it does what it's supposed to do. :)

Sir Penguin
19-07-2003, 02:59:02
The release candidate was released today!

Some other neat stuff:
you can pass arguments to the dict() function to create a new dictionary.
you can make a new dictionary with the dict.fromkeys(l) function, which takes a sequence of non-mutable objects as an argument. The sequence members are the keys for the dictionary.
pop(k) method for dictionary objects. Pretty much the same as for lists, but with a key instead of an index. You can set a default return value if the key doesn't exist.
itertools: "high speed, memory efficient looping constructs inspired by Haskell and SML"
it's the whole random module that gets the new generator. It's an implementation of the Mersenne Twister algorithm (I guess that's good).
new data types for sets. Supports intersection, union, symm. diff., and maybe some other stuff.
trace: used to track program execution. I guess it's like a debugger or something.
a little trick with extended slices: s[::-1] reverses a string.
long integer multiplication is faster (OK, I don't really care, but it sounds neat).

SP

Sean
19-07-2003, 09:19:00
a little trick with extended slices: s[::-1] reverses a string.
:clueless:.

extended slicing: slices can now contain a step argument, like that in the range(b,e,s) function ( speaking of which, iterating through the xrange() function is now faster than iterating through range() ). For example, 'ar[0:9:2]' returns [the elements of 'ar' at indexes 0,2,4,6,8].
Excellent.