inicio mail me! sindicaci;ón

Updates from Python SVN, part 3

  • dir() can be customised with __ dir __ special method:

[code lang="python"] Python 2.6a0 (trunk:54427, Mar 18 2007, 14:13:42) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin Type “help”, “copyright”, “credits” or “license” for more information.

class A(object): … pass … dir(A()) ['class', 'delattr', 'dict', 'doc', ... ] class B(object): … def dir(self): … return [] … dir(B())
[] class C(object): … def init(self): … self.attrs = {’special_attr’: ‘got it’} … def getattr(self, attr): … if attr in self.attrs: … return self.attrs[attr] … def dir(self):
… return self.attrs.keys() … c = C() c.special_attr ‘got it’ dir(c) ['special_attr'] [/code]

  • urllib2 raises URLError instead of OSError if you try to access something with the file:// protocol and it fails.

  • urllib raises IOError if the server’s response contains an invalid HTTP status line.

  • %VAR% and “~user” are expanded correctly in Windows with ntpath.expandvars()!

  • pdb gains a run command to restart the debugger with optional different arguments.

  • ctypes module has a c_bool type.

  • You can get the documentation of a thing with pydoc.render_doc():

[code lang="python"]

import pydoc pydoc.doc(’os’) # this pages the doc pydoc.render_doc(’os’) # this prints on stdout ‘Python Library Documentation: module os\n\nN\x08NA\x08AM\x08ME\x08E\n os - OS routines for Mac, NT, or Posix depending on what system we\’re on.\n\nF\x08FI\x08IL\x08LE\x08E\n … ‘ [/code]

  • Finally the timeit module accept callables in addition of strings. See repeat() and timeit() for details:

[code lang="python"]

import timeit print timeit.repeat(lambda: xrange(100)) [0.64829111099243164, 0.63839888572692871, 0.63987302780151367] print timeit.repeat(lambda: range(100))
[2.9226858615875244, 2.9176130294799805, 2.9172840118408203] print timeit.timeit(lambda: xrange(100)) 0.637830018997 print timeit.timeit(lambda: range(100)) 2.88584208488 [/code]

  • By default textwrap.wrap() drops whitespaces at the end (or the start) of the text, now you can avoid that and preserve all the text:

[code lang="python"]

import textwrap print textwrap.wrap(”A “* 100) ['A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A', 'A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A', 'A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A'] print textwrap.wrap(”A “* 100, drop_whitespace=False) ['A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A ', 'A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A ', 'A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A '] [/code]

see that every string in the second example has its trailing whitespace.

  • os.walk() can follow symlinks now:

[code lang="python"] rhymes@groove ~ % ll temp total 8 drwxr-xr-x 3 rhymes rhymes 102B Mar 18 14:49 a/ lrwxr-xr-x 1 rhymes rhymes 5B Mar 18 14:49 link@ -> /Users/rhymes/Documents

import os for root, dirs, files in walk(’temp’):
… print root, dirs, files … temp ['a', 'link'] [] temp/a [] ['foo'] for root, dirs, files in walk(’temp’, followlinks=True): … print root, dirs, files … temp ['a', 'link'] [] temp/a [] ['foo'] temp/link ['.parallels-vm-directory', ...] [/code]

read the doc as usual for details.

  • webbrowser has been fixed to use GNOME, KDE, Windows default browsers.

  • os.path module has a function to retrieve the relative path of paths:

[code lang="python"]

os.path.relpath(’/Library/Frameworks’) ‘../../../../Library/Frameworks’ os.path.relpath(’/Library/Frameworks’, start=’..’) ‘../../../Library/Frameworks’ [/code]

Related posts

  • Updates from Python SVN, Part 12
  • Updates from Python SVN, Part 6
  • Updates from Python SVN, Part 10
  • Updates from Python SVN, Part 11
  • Updates from Python SVN, Part 16
  • Gravatar

    RiK0 said,

    March 21, 2007 @ 10:51 am

    This times I see a lot of small improvements. Very good. For now I’m just waiting for Leopard, so that I could use Python 2.5 (and especially the RAII-like idiom) in every application.

    Gravatar

    Lawrence said,

    March 21, 2007 @ 11:40 am

    Sadly I can’t use Python 2.5 right now doing mostly libraries and having Windows + 2.3 constraints :-(

    I’m planning to unleash the full power soon, anyway ;-)

    RSS feed for comments on this post · TrackBack URI

    Leave a Comment