updated from Atlas
This commit is contained in:
36
attic/functions/accgen.py
Normal file
36
attic/functions/accgen.py
Normal file
@@ -0,0 +1,36 @@
|
||||
"""
|
||||
Accumulator generator examples
|
||||
|
||||
http://www.paulgraham.com/accgen.html
|
||||
|
||||
>>> f3 = foo(3)
|
||||
>>> f3(2)
|
||||
5
|
||||
>>> f3(2)
|
||||
7
|
||||
>>> f3(2)
|
||||
9
|
||||
|
||||
|
||||
"""
|
||||
|
||||
class foo0:
|
||||
def __init__(self, n):
|
||||
self.n = n
|
||||
def __call__(self, i):
|
||||
self.n += i
|
||||
return self.n
|
||||
|
||||
def foo0(n):
|
||||
def bar(i):
|
||||
bar.s += i
|
||||
return bar.s
|
||||
bar.s = n
|
||||
return bar
|
||||
|
||||
def foo(n):
|
||||
def bar(i):
|
||||
nonlocal n
|
||||
n += i
|
||||
return n
|
||||
return bar
|
||||
25
attic/functions/attrgetter_demo.py
Normal file
25
attic/functions/attrgetter_demo.py
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
metro_data = [
|
||||
('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),
|
||||
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
|
||||
('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
|
||||
('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
|
||||
('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833)),
|
||||
]
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
LatLong = namedtuple('LatLong', 'lat long')
|
||||
Metropolis = namedtuple('Metropolis', 'name cc pop coord')
|
||||
|
||||
metro_areas = [Metropolis(name, cc, pop, LatLong(lat, long_))
|
||||
for name, cc, pop, (lat, long_) in metro_data]
|
||||
|
||||
metro_areas[0]
|
||||
metro_areas[0].coord.lat
|
||||
|
||||
from operator import attrgetter
|
||||
name_lat = attrgetter('name', 'coord.lat')
|
||||
|
||||
for city in sorted(metro_areas, key=attrgetter('coord.lat')):
|
||||
print(name_lat(city))
|
||||
31
attic/functions/attrgetter_demo.rst
Normal file
31
attic/functions/attrgetter_demo.rst
Normal file
@@ -0,0 +1,31 @@
|
||||
>>> metro_data = [
|
||||
... ('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),
|
||||
... ('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
|
||||
... ('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
|
||||
... ('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
|
||||
... ('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833)),
|
||||
... ]
|
||||
# BEGIN ATTRGETTER_DEMO
|
||||
|
||||
>>> from collections import namedtuple
|
||||
>>> LatLong = namedtuple('LatLong', 'lat long') # <1>
|
||||
>>> Metropolis = namedtuple('Metropolis', 'name cc pop coord') # <2>
|
||||
>>> metro_areas = [Metropolis(name, cc, pop, LatLong(lat, long_)) # <3>
|
||||
... for name, cc, pop, (lat, long_) in metro_data]
|
||||
>>> metro_areas[0]
|
||||
Metropolis(name='Tokyo', cc='JP', pop=36.933, coord=LatLong(lat=35.689722, long=139.691667))
|
||||
>>> metro_areas[0].coord.lat # <4>
|
||||
35.689722
|
||||
>>> from operator import attrgetter
|
||||
>>> name_lat = attrgetter('name', 'coord.lat') # <5>
|
||||
>>>
|
||||
>>> for city in sorted(metro_areas, key=attrgetter('coord.lat')): # <6>
|
||||
... print(name_lat(city)) # <7>
|
||||
...
|
||||
('Sao Paulo', -23.547778)
|
||||
('Mexico City', 19.433333)
|
||||
('Delhi NCR', 28.613889)
|
||||
('Tokyo', 35.689722)
|
||||
('New York-Newark', 40.808611)
|
||||
|
||||
# END ATTRGETTER_DEMO
|
||||
5
attic/functions/hello.py
Normal file
5
attic/functions/hello.py
Normal file
@@ -0,0 +1,5 @@
|
||||
import bobo
|
||||
|
||||
@bobo.query('/')
|
||||
def hello(person):
|
||||
return 'Hello %s!' % person
|
||||
76
attic/functions/strkeydict2.py
Normal file
76
attic/functions/strkeydict2.py
Normal file
@@ -0,0 +1,76 @@
|
||||
"""StrKeyDict always converts non-string keys to `str`
|
||||
|
||||
Tests for item retrieval using `d[key]` notation::
|
||||
|
||||
>>> d = StrKeyDict([('2', 'two'), ('4', 'four')])
|
||||
>>> d['2']
|
||||
'two'
|
||||
>>> d[4]
|
||||
'four'
|
||||
>>> d[1]
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
KeyError: '1'
|
||||
|
||||
Tests for the `in` operator::
|
||||
|
||||
>>> 2 in d
|
||||
True
|
||||
>>> 1 in d
|
||||
False
|
||||
|
||||
Test for item assignment using non-string key::
|
||||
|
||||
>>> d[0] = 'zero'
|
||||
>>> d['0']
|
||||
'zero'
|
||||
|
||||
Tests for update using a `dict` or a sequence of pairs::
|
||||
|
||||
>>> d.update({6:'six', '8':'eight'})
|
||||
>>> sorted(d.keys())
|
||||
['0', '2', '4', '6', '8']
|
||||
>>> d.update([(10, 'ten'), ('12', 'twelve')])
|
||||
>>> sorted(d.keys())
|
||||
['0', '10', '12', '2', '4', '6', '8']
|
||||
>>> d.update([1, 3, 5])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: 'int' object is not iterable
|
||||
|
||||
"""
|
||||
# BEGIN STRKEYDICT
|
||||
|
||||
import collections
|
||||
import collections.abc
|
||||
|
||||
|
||||
class StrKeyDict(collections.UserDict): # <1>
|
||||
|
||||
def __init__(self, args, normalize=str, **kwargs):
|
||||
super().__init__(self, *args, **kwargs)
|
||||
self.normalize = normalize
|
||||
|
||||
def __missing__(self, key): # <2>
|
||||
if self.normalize(key) == key:
|
||||
raise KeyError(key)
|
||||
return self[self.normalize(key)]
|
||||
|
||||
def __contains__(self, key):
|
||||
return self.normalize(key) in self.data # <3>
|
||||
|
||||
def __setitem__(self, key, item):
|
||||
self.data[self.normalize(key)] = item # <4>
|
||||
|
||||
def update(self, iterable=None, **kwds):
|
||||
if iterable is not None:
|
||||
if isinstance(iterable, collections.abc.Mapping): # <5>
|
||||
pairs = iterable.items()
|
||||
else:
|
||||
pairs = ((k, v) for k, v in iterable) # <6>
|
||||
for key, value in pairs:
|
||||
self[key] = value # <7>
|
||||
if kwds:
|
||||
self.update(kwds) # <8>
|
||||
|
||||
# END STRKEYDICT
|
||||
Reference in New Issue
Block a user