sync to Atlas repo
This commit is contained in:
BIN
concurrency/flags/graphs.ods
Normal file
BIN
concurrency/flags/graphs.ods
Normal file
Binary file not shown.
53
dicts/strkeydict0_userdictsub.py
Normal file
53
dicts/strkeydict0_userdictsub.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
"""StrKeyDict0 converts non-string keys to `str` on lookup
|
||||||
|
|
||||||
|
# BEGIN STRKEYDICT0_TESTS
|
||||||
|
|
||||||
|
Tests for item retrieval using `d[key]` notation::
|
||||||
|
|
||||||
|
>>> d = StrKeyDict0([('2', 'two'), ('4', 'four')])
|
||||||
|
>>> d['2']
|
||||||
|
'two'
|
||||||
|
>>> d[4]
|
||||||
|
'four'
|
||||||
|
>>> d[1]
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
KeyError: '1'
|
||||||
|
|
||||||
|
|
||||||
|
Tests for item retrieval using `d.get(key)` notation::
|
||||||
|
|
||||||
|
>>> d.get('2')
|
||||||
|
'two'
|
||||||
|
>>> d.get(4)
|
||||||
|
'four'
|
||||||
|
>>> d.get(1, 'N/A')
|
||||||
|
'N/A'
|
||||||
|
|
||||||
|
|
||||||
|
Tests for the `in` operator::
|
||||||
|
|
||||||
|
>>> 2 in d
|
||||||
|
True
|
||||||
|
>>> 1 in d
|
||||||
|
False
|
||||||
|
|
||||||
|
# END STRKEYDICT0_TESTS
|
||||||
|
"""
|
||||||
|
|
||||||
|
# BEGIN STRKEYDICT0
|
||||||
|
|
||||||
|
import collections
|
||||||
|
|
||||||
|
|
||||||
|
class StrKeyDict0(collections.UserDict): # <1>
|
||||||
|
|
||||||
|
def __missing__(self, key):
|
||||||
|
if isinstance(key, str): # <2>
|
||||||
|
raise KeyError(key)
|
||||||
|
return self[str(key)] # <3>
|
||||||
|
|
||||||
|
def __contains__(self, key):
|
||||||
|
return str(key) in self.data # <3>
|
||||||
|
|
||||||
|
# END STRKEYDICT0
|
||||||
95
dicts/strkeydict_dictsub.py
Normal file
95
dicts/strkeydict_dictsub.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
"""StrKeyDict always converts non-string keys to `str`
|
||||||
|
|
||||||
|
This is a variation of `strkeydict.StrKeyDict` implemented
|
||||||
|
as a `dict` built-in subclass (instead of a `UserDict` subclass)
|
||||||
|
|
||||||
|
Test for initializer: keys are converted to `str`.
|
||||||
|
|
||||||
|
>>> d = StrKeyDict([(2, 'two'), ('4', 'four')])
|
||||||
|
>>> sorted(d.keys())
|
||||||
|
['2', '4']
|
||||||
|
|
||||||
|
Tests for item retrieval using `d[key]` notation::
|
||||||
|
|
||||||
|
>>> d['2']
|
||||||
|
'two'
|
||||||
|
>>> d[4]
|
||||||
|
'four'
|
||||||
|
>>> d[1]
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
KeyError: '1'
|
||||||
|
|
||||||
|
Tests for item retrieval using `d.get(key)` notation::
|
||||||
|
|
||||||
|
>>> d.get('2')
|
||||||
|
'two'
|
||||||
|
>>> d.get(4)
|
||||||
|
'four'
|
||||||
|
>>> d.get(1, 'N/A')
|
||||||
|
'N/A'
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import collections.abc
|
||||||
|
|
||||||
|
|
||||||
|
class StrKeyDict(dict):
|
||||||
|
|
||||||
|
def __init__(self, iterable=None, **kwds):
|
||||||
|
super().__init__()
|
||||||
|
self.update(iterable, **kwds)
|
||||||
|
|
||||||
|
def __missing__(self, key):
|
||||||
|
if isinstance(key, str):
|
||||||
|
raise KeyError(key)
|
||||||
|
return self[str(key)]
|
||||||
|
|
||||||
|
def __contains__(self, key):
|
||||||
|
return key in self.keys() or str(key) in self.keys()
|
||||||
|
|
||||||
|
def __setitem__(self, key, item):
|
||||||
|
super().__setitem__(str(key), item)
|
||||||
|
|
||||||
|
def get(self, key, default=None):
|
||||||
|
try:
|
||||||
|
return self[key]
|
||||||
|
except KeyError:
|
||||||
|
return default
|
||||||
|
|
||||||
|
def update(self, iterable=None, **kwds):
|
||||||
|
if iterable is not None:
|
||||||
|
if isinstance(iterable, collections.abc.Mapping):
|
||||||
|
pairs = iterable.items()
|
||||||
|
else:
|
||||||
|
pairs = ((k, v) for k, v in iterable)
|
||||||
|
for key, value in pairs:
|
||||||
|
self[key] = value
|
||||||
|
if kwds:
|
||||||
|
self.update(kwds)
|
||||||
36
functions/accgen.py
Normal file
36
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
|
||||||
45
interfaces/dict_subclass.py
Normal file
45
interfaces/dict_subclass.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
# Code and text by BitBucket user "enigmacurry" posted to
|
||||||
|
# https://bitbucket.org/pypy/pypy/issue/708/discrepancy-in-dict-subclass-__getitem__
|
||||||
|
# Adapted by Luciano Ramalho:
|
||||||
|
# - changed comments to docstring to run with doctest;
|
||||||
|
# - added test for Test class raising exception
|
||||||
|
# - and added () to print.
|
||||||
|
|
||||||
|
"""
|
||||||
|
This is a test case to describe a bug I'm seeing in PyPy 1.5. I have
|
||||||
|
a Cache object that is a dictionary that supports lookup via regular
|
||||||
|
attribute access. For instance:
|
||||||
|
|
||||||
|
>>> c = Cache()
|
||||||
|
>>> c["asdf"] = "asdf"
|
||||||
|
>>> c.asdf == c["asdf"]
|
||||||
|
True
|
||||||
|
>>> t = Test()
|
||||||
|
>>> t["asdf"] = "asdf"
|
||||||
|
>>> t.asdf == t["asdf"]
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
Exception: Trying to getitem: asdf
|
||||||
|
|
||||||
|
When looking up keys via attribute, PyPy 1.5 calls __getitem__
|
||||||
|
whereas CPython 2.7.1 does not.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class Cache(dict):
|
||||||
|
"A dictionary that supports attribute style key lookup"
|
||||||
|
def __init__(self, **kw):
|
||||||
|
dict.__init__(self, kw)
|
||||||
|
self.__dict__ = self
|
||||||
|
|
||||||
|
class Test(Cache):
|
||||||
|
def __getitem__(self, item):
|
||||||
|
# I want to process items differently than attributes:
|
||||||
|
raise Exception("Trying to getitem: %s" % item)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
t = Test()
|
||||||
|
t["asdf"] = "asdf"
|
||||||
|
#CPython does not call __getitem__ .. PyPy does:
|
||||||
|
print(t.asdf)
|
||||||
|
#Doesn't matter if it's a member of __dict__ or not:
|
||||||
|
print(t.__getattribute__)
|
||||||
64
interfaces/tombola_subhook.py
Normal file
64
interfaces/tombola_subhook.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
"""
|
||||||
|
Variation of ``tombola.Tombola`` implementing ``__subclasshook__``.
|
||||||
|
|
||||||
|
Tests with simple classes::
|
||||||
|
|
||||||
|
>>> Tombola.__subclasshook__(object)
|
||||||
|
NotImplemented
|
||||||
|
>>> class Complete:
|
||||||
|
... def __init__(): pass
|
||||||
|
... def load(): pass
|
||||||
|
... def pick(): pass
|
||||||
|
... def loaded(): pass
|
||||||
|
...
|
||||||
|
>>> Tombola.__subclasshook__(Complete)
|
||||||
|
True
|
||||||
|
>>> issubclass(Complete, Tombola)
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from inspect import getmembers, isfunction
|
||||||
|
|
||||||
|
|
||||||
|
class Tombola(ABC): # <1>
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def __init__(self, iterable): # <2>
|
||||||
|
"""New instance is loaded from an iterable."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def load(self, iterable):
|
||||||
|
"""Add items from an iterable."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def pick(self): # <3>
|
||||||
|
"""Remove item at random, returning it.
|
||||||
|
|
||||||
|
This method should raise `LookupError` when the instance is empty.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def loaded(self): # <4>
|
||||||
|
try:
|
||||||
|
item = self.pick()
|
||||||
|
except LookupError:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
self.load([item]) # put it back
|
||||||
|
return True
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __subclasshook__(cls, other_cls):
|
||||||
|
if cls is Tombola:
|
||||||
|
interface_names = function_names(cls)
|
||||||
|
found_names = set()
|
||||||
|
for a_cls in other_cls.__mro__:
|
||||||
|
found_names |= function_names(a_cls)
|
||||||
|
if found_names >= interface_names:
|
||||||
|
return True
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
|
|
||||||
|
def function_names(obj):
|
||||||
|
return {name for name, _ in getmembers(obj, isfunction)}
|
||||||
48
iterables/aritprog_v1.py
Normal file
48
iterables/aritprog_v1.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
"""
|
||||||
|
Arithmetic progression class
|
||||||
|
|
||||||
|
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||||
|
>>> list(ap)
|
||||||
|
[1.0, 1.5, 2.0, 2.5]
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import array
|
||||||
|
from collections import abc
|
||||||
|
|
||||||
|
class ArithmeticProgression:
|
||||||
|
|
||||||
|
def __init__(self, begin, step, end):
|
||||||
|
self.begin = begin
|
||||||
|
self.step = step
|
||||||
|
self.end = end
|
||||||
|
self._build()
|
||||||
|
|
||||||
|
def _build(self):
|
||||||
|
self._numbers = array.array('d')
|
||||||
|
n = self.begin
|
||||||
|
while n < self.end:
|
||||||
|
self._numbers.append(n)
|
||||||
|
n += self.step
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return ArithmeticProgressionIterator(self._numbers)
|
||||||
|
|
||||||
|
|
||||||
|
class ArithmeticProgressionIterator(abc.Iterator):
|
||||||
|
|
||||||
|
def __init__(self, series):
|
||||||
|
self._series = series
|
||||||
|
self._index = 0
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
if self._index < len(self._series):
|
||||||
|
item = self._series[self._index]
|
||||||
|
self._index += 1
|
||||||
|
return item
|
||||||
|
else:
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
37
iterables/aritprog_v1b.py
Normal file
37
iterables/aritprog_v1b.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
"""
|
||||||
|
Arithmetic progression class
|
||||||
|
|
||||||
|
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||||
|
>>> list(ap)
|
||||||
|
[1.0, 1.5, 2.0, 2.5]
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import array
|
||||||
|
from collections import abc
|
||||||
|
|
||||||
|
class ArithmeticProgression:
|
||||||
|
|
||||||
|
def __init__(self, begin, step, end):
|
||||||
|
self.begin = begin
|
||||||
|
self.step = step
|
||||||
|
self.end = end
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return ArithmeticProgressionIterator(self)
|
||||||
|
|
||||||
|
|
||||||
|
class ArithmeticProgressionIterator(abc.Iterator):
|
||||||
|
|
||||||
|
def __init__(self, arithmetic_progression):
|
||||||
|
self._ap = arithmetic_progression
|
||||||
|
self._index = 0
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
result = self._ap.begin + self._ap.step * self._index
|
||||||
|
if result < self._ap.end:
|
||||||
|
self._index += 1
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
raise StopIteration
|
||||||
32
iterables/aritprog_v2.py
Normal file
32
iterables/aritprog_v2.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
"""
|
||||||
|
Arithmetic progression class
|
||||||
|
|
||||||
|
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||||
|
>>> list(ap)
|
||||||
|
[1.0, 1.5, 2.0, 2.5]
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import array
|
||||||
|
from collections import abc
|
||||||
|
|
||||||
|
class ArithmeticProgression:
|
||||||
|
|
||||||
|
def __init__(self, begin, step, end):
|
||||||
|
self.begin = begin
|
||||||
|
self.step = step
|
||||||
|
self.end = end
|
||||||
|
self._build()
|
||||||
|
|
||||||
|
def _build(self):
|
||||||
|
self._numbers = array.array('d')
|
||||||
|
n = self.begin
|
||||||
|
while n < self.end:
|
||||||
|
self._numbers.append(n)
|
||||||
|
n += self.step
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for item in self._numbers:
|
||||||
|
yield item
|
||||||
|
return StopIteration
|
||||||
27
iterables/aritprog_v3.py
Normal file
27
iterables/aritprog_v3.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
"""
|
||||||
|
Arithmetic progression class
|
||||||
|
|
||||||
|
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||||
|
>>> list(ap)
|
||||||
|
[1.0, 1.5, 2.0, 2.5]
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import array
|
||||||
|
from collections import abc
|
||||||
|
|
||||||
|
class ArithmeticProgression:
|
||||||
|
|
||||||
|
def __init__(self, begin, step, end=None):
|
||||||
|
self.begin = float(begin)
|
||||||
|
self.step = float(step)
|
||||||
|
self.end = end # None -> "infinite" series
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
result = self.begin
|
||||||
|
forever = self.end is None
|
||||||
|
while forever or result < self.end:
|
||||||
|
yield result
|
||||||
|
result += self.step
|
||||||
|
raise StopIteration
|
||||||
29
iterables/aritprog_v4.py
Normal file
29
iterables/aritprog_v4.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
"""
|
||||||
|
Arithmetic progression class
|
||||||
|
|
||||||
|
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||||
|
>>> list(ap)
|
||||||
|
[1.0, 1.5, 2.0, 2.5]
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import array
|
||||||
|
from collections import abc
|
||||||
|
|
||||||
|
class ArithmeticProgression:
|
||||||
|
|
||||||
|
def __init__(self, begin, step, end=None):
|
||||||
|
self.begin = begin
|
||||||
|
self.step = step
|
||||||
|
self.end = end # None -> "infinite" series
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
result = type(self.step)(self.begin)
|
||||||
|
forever = self.end is None
|
||||||
|
index = 0
|
||||||
|
while forever or result < self.end:
|
||||||
|
yield result
|
||||||
|
index += 1
|
||||||
|
result = self.begin + self.step * index
|
||||||
|
raise StopIteration
|
||||||
24
iterables/test_aritprog.py
Normal file
24
iterables/test_aritprog.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
"""
|
||||||
|
Demonstrate difference between Arithmetic Progression calculated
|
||||||
|
as a series of increments accumulating errors versus one addition
|
||||||
|
and one multiplication.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from fractions import Fraction
|
||||||
|
from aritprog_v3 import ArithmeticProgression as APv3
|
||||||
|
from aritprog_v4 import ArithmeticProgression as APv4
|
||||||
|
|
||||||
|
ap3 = iter(APv3(1, .1))
|
||||||
|
ap4 = iter(APv4(1, .1))
|
||||||
|
ap_frac = iter(APv4(Fraction(1, 1), Fraction(1, 10)))
|
||||||
|
epsilon = 10**-10
|
||||||
|
iteration = 0
|
||||||
|
delta = next(ap3) - next(ap4)
|
||||||
|
frac = next(ap_frac)
|
||||||
|
while abs(delta) <= epsilon:
|
||||||
|
delta = next(ap3) - next(ap4)
|
||||||
|
frac = next(ap_frac)
|
||||||
|
iteration +=1
|
||||||
|
|
||||||
|
print('iteration: {}\tfraction: {}\tepsilon: {}\tdelta: {}'.
|
||||||
|
format(iteration, frac, epsilon, delta))
|
||||||
Reference in New Issue
Block a user