update from Atlas with major reorg
This commit is contained in:
25
attic/metaprog/plainpoint.py
Normal file
25
attic/metaprog/plainpoint.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""
|
||||
A class equivalent to the class statement below would be generated by this code:
|
||||
|
||||
>>> import collections
|
||||
>>> Point = collections.plainclass('Point', 'x y')
|
||||
"""
|
||||
|
||||
class Point(object):
|
||||
__slots__ = ['x', 'y'] # save memory in the likely event there are many instances
|
||||
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def __repr__(self):
|
||||
return 'Point({!r}, {!r})'.format(self.x, self.y)
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, Point):
|
||||
return NotImplemented
|
||||
return self.x == other.x and self.y == other.y
|
||||
|
||||
def __iter__(self, other): # support unpacking
|
||||
yield self.x
|
||||
yield self.y
|
||||
29
attic/metaprog/prop_inheritance.py
Normal file
29
attic/metaprog/prop_inheritance.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""
|
||||
Alex Martelli, _Python in a Nutshell, 2e._ (O'Reilly, 2006), p. 101
|
||||
|
||||
==========================
|
||||
Properties and inheritance
|
||||
==========================
|
||||
|
||||
Properties are inherited normally, just like any other attribute.
|
||||
However, there’s a little trap for the unwary: the methods called
|
||||
upon to access a property are those that are defined in the class
|
||||
in which the property itself is defined, without intrinsic use of
|
||||
further overriding that may happen in subclasses. For example:
|
||||
"""
|
||||
|
||||
class B(object):
|
||||
|
||||
def f(self):
|
||||
return 23
|
||||
|
||||
g = property(f)
|
||||
|
||||
class C(B):
|
||||
|
||||
def f(self):
|
||||
return 42
|
||||
|
||||
c = C()
|
||||
|
||||
print(c.g) # prints 23, not 42
|
||||
44
attic/metaprog/special_attrs.py
Normal file
44
attic/metaprog/special_attrs.py
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
'''
|
||||
4.13. Special Attributes
|
||||
|
||||
The implementation adds a few special read-only attributes to
|
||||
several object types, where they are relevant.
|
||||
|
||||
Some of these are not reported by the dir() built-in function.
|
||||
|
||||
https://docs.python.org/3/library/stdtypes.html#special-attributes
|
||||
'''
|
||||
|
||||
|
||||
obj_attrs = {'__dict__', '__class__'}
|
||||
|
||||
cls_data_attrs = {'__slots__', '__bases__', '__name__', '__qualname__', '__mro__'}
|
||||
|
||||
cls_methods = {'mro', '__subclasses__'}
|
||||
|
||||
cls_attrs = cls_data_attrs | cls_methods
|
||||
|
||||
|
||||
an_object = object()
|
||||
|
||||
class EmptyClass():
|
||||
pass
|
||||
|
||||
an_instance = EmptyClass()
|
||||
|
||||
class EmptySlots():
|
||||
__slots__ = ()
|
||||
|
||||
a_slots_instance = EmptySlots()
|
||||
|
||||
|
||||
objs = EmptyClass, EmptySlots, an_object, an_instance, a_slots_instance
|
||||
|
||||
for obj in objs:
|
||||
print('-' * 60)
|
||||
print(repr(obj), ':', type(obj))
|
||||
dir_obj = set(dir(obj))
|
||||
print('obj_attrs not listed:', sorted(obj_attrs - dir_obj))
|
||||
print('all cls_attrs :', sorted(cls_attrs))
|
||||
print('cls_attrs not listed:', sorted(cls_attrs - dir_obj))
|
||||
44
attic/metaprog/spreadsheet.py
Normal file
44
attic/metaprog/spreadsheet.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""
|
||||
Spreadsheet example adapted from Raymond Hettinger's `recipe`__
|
||||
|
||||
__ http://code.activestate.com/recipes/355045-spreadsheet/
|
||||
|
||||
Demonstration::
|
||||
|
||||
>>> from math import sin, pi
|
||||
>>> ss = Spreadsheet(sin=sin, pi=pi)
|
||||
>>> ss['a1'] = '-5'
|
||||
>>> ss['a2'] = 'a1*6'
|
||||
>>> ss['a3'] = 'a2*7'
|
||||
>>> ss['a3']
|
||||
-210
|
||||
>>> ss['b1'] = 'sin(pi/4)'
|
||||
>>> ss['b1'] # doctest:+ELLIPSIS
|
||||
0.707106781186...
|
||||
>>> ss.getformula('b1')
|
||||
'sin(pi/4)'
|
||||
>>> ss['c1'] = 'abs(a2)'
|
||||
>>> ss['c1']
|
||||
30
|
||||
>>> ss['d1'] = '3*'
|
||||
>>> ss['d1']
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
SyntaxError: unexpected EOF while parsing
|
||||
"""
|
||||
|
||||
|
||||
class Spreadsheet:
|
||||
|
||||
def __init__(self, **tools):
|
||||
self._cells = {}
|
||||
self._tools = tools
|
||||
|
||||
def __setitem__(self, key, formula):
|
||||
self._cells[key] = formula
|
||||
|
||||
def getformula(self, key):
|
||||
return self._cells[key]
|
||||
|
||||
def __getitem__(self, key):
|
||||
return eval(self._cells[key], self._tools, self)
|
||||
54
attic/metaprog/spreadsheet2.py
Normal file
54
attic/metaprog/spreadsheet2.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""
|
||||
Spreadsheet example adapted from Raymond Hettinger's `recipe`__
|
||||
|
||||
__ http://code.activestate.com/recipes/355045-spreadsheet/
|
||||
|
||||
Demonstration::
|
||||
|
||||
>>> from math import sin, pi
|
||||
>>> ss = Spreadsheet(sin=sin, pi=pi, abs=abs)
|
||||
>>> ss['a1'] = '-5'
|
||||
>>> ss['a2'] = 'a1*6'
|
||||
>>> ss['a3'] = 'a2*7'
|
||||
>>> ss['a3']
|
||||
-210
|
||||
>>> ss['b1'] = 'sin(pi/4)'
|
||||
>>> ss['b1'] # doctest:+ELLIPSIS
|
||||
0.707106781186...
|
||||
>>> ss.getformula('b1')
|
||||
'sin(pi/4)'
|
||||
>>> ss['c1'] = 'abs(a2)'
|
||||
>>> ss['c1']
|
||||
30
|
||||
>>> ss['c2'] = 'len(a2)'
|
||||
>>> ss['c2']
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
NameError: name 'len' is not defined
|
||||
>>> ss['d1'] = '3*'
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
SyntaxError: unexpected EOF while parsing ['d1'] = '3*'
|
||||
"""
|
||||
|
||||
|
||||
class Spreadsheet:
|
||||
|
||||
def __init__(self, **tools):
|
||||
self._cells = {}
|
||||
self._tools = {'__builtins__' : {}}
|
||||
self._tools.update(tools)
|
||||
|
||||
def __setitem__(self, key, formula):
|
||||
try:
|
||||
compile(formula, '<__setitem__>', 'eval')
|
||||
except SyntaxError as exc:
|
||||
msg = '{} [{!r}] = {!r}'.format(exc.msg, key, formula)
|
||||
raise SyntaxError(msg)
|
||||
self._cells[key] = formula
|
||||
|
||||
def getformula(self, key):
|
||||
return self._cells[key]
|
||||
|
||||
def __getitem__(self, key):
|
||||
return eval(self._cells[key], self._tools, self)
|
||||
Reference in New Issue
Block a user