Udun's Labs

Python Notes

Random

mylist = ['x', 3, 'b'] print '[%s]' % ', '.join(map(str, mylist))

Infinite generator: iter(int, 1) - try li=filter(None, iter(int, 1))

>>> def ii(): yield filter(None, iter(int, 1))
>>> ii
<function ii at 0x025FBA70>
>>> ii()
<generator object ii at 0x0262B788>
>>> ii()
<generator object ii at 0x0262BFD0>
>>> ii()[0]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: 'generator' object has no attribute '__getitem__'
>>> ii().next()
# Infinite loop

I want to have a set of lists (as opposed to adding the list items to the set: s |= set(li))

Random refactoring puzzle:

# before - indexes better be some set (although just _micro_ optimization)
def SetChecked(self, indexes):
    for i in indexes:
        assert 0 <= i < self.Count, "Index (%s) out of range" % i
    for i in range(self.Count):
        self.Check(i, i in indexes)
# after ?
def SetChecked(self, indexes):
    for i in indexes: # will pick a random index from the set - may check
    # some items before assertion as opposed to before - important ?
        assert 0 <= i < self.Count, "Index (%s) out of range" % i
        self.Check(i, True)

Thou Shalt Not Modify A List During Iteration - for sets just use pop

My deleted: Override python list operators dynamically - see https://wiki.python.org/moin/FromFunctionToMethod - and non deleted Override list’s builtins dynamically in class scope and Access base class attribute in derived class - in ‘class scope’

To be expanded:

Interpreter facts
>>> def thatsSoUnlikeJava():
...     try:
...         raise Exception
...     finally:return True
...     
>>> print thatsSoUnlikeJava()
True
>>> class C(object):
...     def __m(self): return 0
...     def m(self): return self.__m()
...
>>> class D(C):
...     def __m(self): return 1
...
>>> D().__m()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'D' object has no attribute '__m'
>>> D().m()
0
>>> def wrap():
...     m=0
...     def wrapped():
...         m+=1
...         return m
...     return wrapped
...
>>> print wrap()()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<input>", line 4, in wrapped
UnboundLocalError: local variable 'm' referenced before assignment
Links

argparse:

Import never ceases to surprise me. For instance in the code I have:

try:
    from win32com.shell import shell, shellcon

In Python27\Lib\site-packages\win32com there was no shell to be seen - then I noticed in Python27\Lib\site-packages\win32com\__init__.py:

__path__.append( win32api.GetFullPathName( __path__[0] + "\\..\\win32comext") )

And Python27\Lib\site-packages\win32comext\shell\__init__.py:

import win32com
win32com.__PackageSupportBuildPath__(__path__)

And sure enough C:\_\Python27\Lib\site-packages\win32comext\shell\shell.pyd. What’s in there ? Centipydes probably. There’s shecllcon.py too. One can read there:

FO_MOVE = 1
FO_COPY = 2
FO_DELETE = 3
FO_RENAME = 4

almost as in the program I am maintaining.

Python launcher for windows

Double click on a script will open with specified (by shebang) python version. Easiest to install: install python 3.3+ (see this for installer options). Gentle introduction and the PEP.

You might want to visit my sponsors:

Comments