Literate Programmin - good explanation.
In [1]:
import hashlib
import inspect
import types
In [2]:
types.ClassType
Out[2]:
In [15]:
class Bar:
pass
class Foo(Bar):
def __getitem__(s):
pass
type(Foo) is types.ClassType
Out[15]:
In [16]:
inspect.getmembers(Foo)
Out[16]:
In [2]:
def fdigest(filename):
f = open(filename,mode='rb')
m = hashlib.sha256(f.read())
f.close()
return m.hexdigest()
In [3]:
h = fdigest('Loads.ipynb')
h
Out[3]:
In [17]:
def extend(old):
"""This is used as a class decorator to 'extend' class definitions,
for example, over widely dispersed areas. EG:
class Foo(object):
. . .
@extend(Foo)
class Foo:
def meth2(...):
. . .
will result in one class Foo containing all methods, etc."""
def _extend(new,old=old):
if new.__name__ != old.__name__:
raise TypeError("Class names must match: '{}' != '{}'".format(new.__name__,old.__name__))
if type(new) is not types.ClassType:
raise TypeError("Extension class must be an old-style class (not derived from class object)")
if len(new.__bases__) != 0:
raise TypeError("Extension class must not have a base class.")
ng = ['__doc__','__module__']
for a,v in inspect.getmembers(new):
if a not in ng:
if type(v) is types.MethodType:
if v.im_self is None:
v = types.MethodType(v.im_func,None,old)
else:
v = classmethod(v.im_func)
elif type(v) is property:
v = property(v.fget,v.fset,v.fdel)
elif type(v) is types.FunctionType:
v = staticmethod(types.FunctionType(v.func_code,v.func_globals,v.func_name,v.func_defaults,v.func_closure))
setattr(old,a,v)
#print('Set {} in class {} to type {}'.format(a,old.__name__,type(v)))
return old
return _extend
In [18]:
class Foo(object):
def im(s):
return s
@classmethod
def cm(s):
return s
In [19]:
l = inspect.getmembers(Foo)
l
Out[19]:
In [18]:
def _cm2(c):
return 'cm2',c
v = l[-2][1].im_self
Foo.cm2 = types.MethodType(_cm2,v,Foo)
type(v),v,l[-2][1].im_class
Out[18]:
In [19]:
l = inspect.getmembers(Foo)
l
Out[19]:
In [20]:
def _cm3(c):
return '_cm3',c
Foo.cm3 = classmethod(_cm3)
l = inspect.getmembers(Foo)
l
Out[20]:
In [28]:
v = l[-2][1]
v.im_self, type(v)
Out[28]:
In [21]:
Foo.cm(), Foo.cm2()
Out[21]:
In [22]:
class Bar(Foo):
pass
In [23]:
Bar.cm(), Bar.cm2()
Out[23]:
In [24]:
Bar.cm3()
Out[24]:
In [29]:
from salib import extend
In [30]:
class Zip(object):
def im(c):
return 'Zip.im',c
@classmethod
def cm(c):
return 'Zip.cm',c
In [32]:
@extend(Zip)
class Zip:
def im2(c):
return 'Zip.im2',c
@classmethod
def cm2(c):
return 'Zip.cm2',c
In [34]:
z = Zip()
z.im(), z.cm(), z.im2(), z.cm2()
Out[34]:
In [35]:
class Zap(Zip):
def im3(c):
return 'Zap.im3',c
@classmethod
def cm3(c):
return 'Zap.cm3',c
In [36]:
zz = Zap()
zz.im(), zz.cm(), zz.im2(), zz.cm2(), zz.im3(), zz.cm3()
Out[36]:
In [20]:
class Zoop:
pass
In [21]:
Zoop.__module__
Out[21]:
In [22]:
import sys
m = sys.modules['__main__']
In [23]:
m
Out[23]:
In [26]:
getattr(m,'Zoop')
Out[26]:
In [27]:
c = Zoop
m.__dict__[c.__name__]
Out[27]:
In [2]:
from IPython.core.magic import register_cell_magic
In [3]:
@register_cell_magic('test')
def _test(line,cell):
print('Line:',line)
print('Cell:',cell)
return 13
In [6]:
%%test foo be doo
a + b
c
Out[6]:
In [15]:
%%time
n = 1000000
sum(range(n))
In [19]:
%lsmagic
Out[19]:
In [52]:
cc = compile('''a=3;
b=4;
c=a*b;
c*10''','here','exec')
In [54]:
eval(cc)
In [53]:
cc
Out[53]:
In [48]:
cc.co_code
Out[48]:
In [49]:
dir(cc)
Out[49]:
In [50]:
cc.co_filename
Out[50]:
In [ ]: