Skip to content

Commit 104b2c9

Browse files
committed
update from atlas
1 parent 290079e commit 104b2c9

24 files changed

+671
-202
lines changed

datamodel/frenchdeck.doctest 01-data-model/frenchdeck.doctest

+8-8
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@ Card(rank='Q', suit='hearts')
3131
2 Card(rank='3', suit='spades')
3232
3 Card(rank='4', suit='spades')
3333
...
34-
>>> def alt_color_rank(card):
34+
>>> suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)
35+
>>> def spades_high(card):
3536
... rank_value = FrenchDeck.ranks.index(card.rank)
36-
... suits = 'diamonds clubs hearts spades'.split()
37-
... return rank_value * len(suits) + suits.index(card.suit)
37+
... return rank_value * len(suit_values) + suit_values[card.suit]
3838

3939
Rank test:
4040

41-
>>> alt_color_rank(Card('2', 'diamonds'))
41+
>>> spades_high(Card('2', 'clubs'))
4242
0
43-
>>> alt_color_rank(Card('A', 'spades'))
43+
>>> spades_high(Card('A', 'spades'))
4444
51
4545

46-
>>> for card in sorted(deck, key=alt_color_rank): # doctest: +ELLIPSIS
46+
>>> for card in sorted(deck, key=spades_high): # doctest: +ELLIPSIS
4747
... print(card)
48-
Card(rank='2', suit='diamonds')
4948
Card(rank='2', suit='clubs')
49+
Card(rank='2', suit='diamonds')
5050
Card(rank='2', suit='hearts')
5151
...
52-
Card(rank='A', suit='clubs')
52+
Card(rank='A', suit='diamonds')
5353
Card(rank='A', suit='hearts')
5454
Card(rank='A', suit='spades')
File renamed without changes.
File renamed without changes.

02-array-seq/listcomp_speed.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import timeit
2+
3+
TIMES = 10000
4+
5+
SETUP = """
6+
symbols = '$¢£¥€¤'
7+
def non_ascii(c):
8+
return c > 127
9+
"""
10+
11+
def clock(label, cmd):
12+
res = timeit.repeat(cmd, setup=SETUP, number=TIMES)
13+
print(label, *('{:.3f}'.format(x) for x in res))
14+
15+
clock('listcomp :', '[ord(s) for s in symbols if ord(s) > 127]')
16+
clock('listcomp + func :', '[ord(s) for s in symbols if non_ascii(ord(s))]')
17+
clock('filter + lambda :', 'list(filter(lambda c: c > 127, map(ord, symbols)))')
18+
clock('filter + func :', 'list(filter(non_ascii, map(ord, symbols)))')

21-class-metaprog/evaldecos.py

-22
This file was deleted.

21-class-metaprog/evalsupport.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
print('<[100]> evalsupport module start')
2+
3+
def deco_alpha(cls):
4+
print('<[200]> deco_alpha')
5+
6+
def inner_1(self):
7+
print('<[300]> deco_alpha:inner_1')
8+
9+
cls.method_y = inner_1
10+
return cls
11+
12+
# BEGIN META_ALEPH
13+
class MetaAleph(type):
14+
print('<[400]> MetaAleph body')
15+
16+
def __init__(self, name, bases, dic):
17+
print('<[500]> MetaAleph.__init__')
18+
19+
def inner_2(self):
20+
print('<[600]> MetaAleph.__init__:inner_2')
21+
22+
self.method_z = inner_2
23+
# END META_ALEPH
24+
25+
print('<[700]> evalsupport module end')

21-class-metaprog/evaltime.py

+26-69
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,49 @@
1-
import atexit
1+
from evalsupport import deco_alpha
22

3-
from evaldecos import deco_alpha
4-
from evaldecos import deco_beta
3+
print('<[1]> evaltime module start')
54

65

7-
print('<<1>> start')
8-
9-
10-
func_A = lambda: print('<<2>> func_A')
11-
12-
13-
def func_B():
14-
print('<<3>> func_B')
15-
16-
def func_C():
17-
print('<<4>> func_C')
18-
19-
return func_C
20-
21-
22-
@deco_alpha
23-
def func_D():
24-
print('<<6>> func_D')
25-
26-
27-
def func_E():
28-
print('<<7>> func_E')
29-
30-
31-
class ClassOne(object):
32-
print('<<7>> ClassOne body')
6+
class ClassOne():
7+
print('<[2]> ClassOne body')
338

349
def __init__(self):
35-
print('<<8>> ClassOne.__init__')
10+
print('<[3]> ClassOne.__init__')
3611

3712
def __del__(self):
38-
print('<<9>> ClassOne.__del__')
13+
print('<[4]> ClassOne.__del__')
3914

40-
def method1(self):
41-
print('<<10>> ClassOne.method')
42-
return "result from 'ClassOne.method1'"
15+
def method_x(self):
16+
print('<[5]> ClassOne.method_x')
4317

4418
class ClassTwo(object):
45-
print('<<11>> ClassTwo body')
46-
47-
48-
@deco_beta
49-
class ClassThree(ClassOne):
50-
print('<<12>> ClassThree body')
19+
print('<[6]> ClassTwo body')
5120

52-
def method3(self):
53-
print('<<13>> ClassOne.method')
54-
return "result from 'ClassThree.method3'"
55-
56-
57-
if True:
58-
print("<<14>> 'if True'")
5921

22+
@deco_alpha
23+
class ClassThree():
24+
print('<[7]> ClassThree body')
6025

61-
if False:
62-
print("<<15>> 'if False'")
26+
def method_y(self):
27+
print('<[8]> ClassThree.method_y')
6328

6429

65-
atexit.register(func_E)
30+
class ClassFour(ClassThree):
31+
print('<[9]> ClassFour body')
6632

67-
print("<<16>> right before 'if ... __main__'")
33+
def method_y(self):
34+
print('<[10]> ClassFour.method_y')
6835

6936

7037
if __name__ == '__main__':
71-
print('<<17>> start __main__ block')
72-
print(func_A)
73-
print(func_A())
74-
print('<<18>> continue __main__ block')
75-
print(func_B)
76-
b_result = func_B()
77-
print(b_result)
38+
print('<[11]> ClassOne tests', 30 * '.')
7839
one = ClassOne()
79-
one.method1()
80-
b_result()
81-
print(func_D)
82-
func_D()
40+
one.method_x()
41+
print('<[12]> ClassThree tests', 30 * '.')
8342
three = ClassThree()
84-
three.method3()
85-
86-
class ClassFour(object):
87-
print('<<19>> ClasFour body')
88-
89-
print('<<20>> end __main__ block')
43+
three.method_y()
44+
print('<[13]> ClassFour tests', 30 * '.')
45+
four = ClassFour()
46+
four.method_y()
9047

9148

92-
print('<<21>> The End')
49+
print('<[14]> evaltime module end')

21-class-metaprog/evaltime_meta.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from evalsupport import deco_alpha
2+
from evalsupport import MetaAleph
3+
4+
print('<[1]> evaltime_meta module start')
5+
6+
7+
@deco_alpha
8+
class ClassThree():
9+
print('<[2]> ClassThree body')
10+
11+
def method_y(self):
12+
print('<[3]> ClassThree.method_y')
13+
14+
15+
class ClassFour(ClassThree):
16+
print('<[4]> ClassFour body')
17+
18+
def method_y(self):
19+
print('<[5]> ClassFour.method_y')
20+
21+
22+
class ClassFive(metaclass=MetaAleph):
23+
print('<[6]> ClassFive body')
24+
25+
def __init__(self):
26+
print('<[7]> ClassFive.__init__')
27+
28+
def method_z(self):
29+
print('<[8]> ClassFive.method_y')
30+
31+
32+
class ClassSix(ClassFive):
33+
print('<[9]> ClassSix body')
34+
35+
def method_z(self):
36+
print('<[10]> ClassSix.method_y')
37+
38+
39+
if __name__ == '__main__':
40+
print('<[11]> ClassThree tests', 30 * '.')
41+
three = ClassThree()
42+
three.method_y()
43+
print('<[12]> ClassFour tests', 30 * '.')
44+
four = ClassFour()
45+
four.method_y()
46+
print('<[13]> ClassFive tests', 30 * '.')
47+
five = ClassFive()
48+
five.method_z()
49+
print('<[14]> ClassSix tests', 30 * '.')
50+
six = ClassSix()
51+
six.method_z()
52+
53+
print('<[15]> evaltime_meta module end')

21-class-metaprog/factories.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,42 @@
99
>>> name, weight, _ = rex # <3>
1010
>>> name, weight
1111
('Rex', 30)
12-
>>> "{2}'s dog weights {1}kg".format(*rex)
12+
>>> "{2}'s dog weights {1}kg".format(*rex) # <4>
1313
"Bob's dog weights 30kg"
14-
>>> rex.weight = 32 # <4>
14+
>>> rex.weight = 32 # <5>
1515
>>> rex
1616
Dog(name='Rex', weight=32, owner='Bob')
17-
>>> Dog.__mro__ # <5>
17+
>>> Dog.__mro__ # <6>
1818
(<class 'factories.Dog'>, <class 'object'>)
1919
2020
# END RECORD_FACTORY_DEMO
2121
"""
2222

2323
# BEGIN RECORD_FACTORY
2424
def record_factory(cls_name, field_names):
25-
if isinstance(field_names, str): # <1>
25+
if isinstance(field_names, str):
2626
field_names = field_names.replace(',', ' ').split()
27-
__slots__ = tuple(field_names)
27+
field_names = tuple(field_names) # <1>
2828

29-
def __init__(self, *args, **kwargs):
29+
def __init__(self, *args, **kwargs): # <2>
3030
attrs = dict(zip(self.__slots__, args))
3131
attrs.update(kwargs)
3232
for name, value in attrs.items():
3333
setattr(self, name, value)
3434

35-
def __iter__(self):
35+
def __iter__(self): # <3>
3636
for name in self.__slots__:
3737
yield getattr(self, name)
3838

39-
def __repr__(self):
39+
def __repr__(self): # <4>
4040
values = ', '.join('{}={!r}'.format(*i) for i
4141
in zip(self.__slots__, self))
4242
return '{}({})'.format(self.__class__.__name__, values)
4343

44-
cls_attrs = dict(__slots__ = __slots__,
45-
__init__ = __init__,
46-
__iter__ = __iter__,
47-
__repr__ = __repr__)
44+
cls_attrs = dict(__slots__ = field_names, # <5>
45+
__init__ = __init__,
46+
__iter__ = __iter__,
47+
__repr__ = __repr__)
4848

49-
return type(cls_name, (object,), cls_attrs)
49+
return type(cls_name, (object,), cls_attrs) # <6>
5050
# END RECORD_FACTORY

datamodel/tox.ini

-4
This file was deleted.

descriptors/bulkfood_v4.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
3030
>>> raisins = LineItem('Golden raisins', 10, 6.95)
3131
>>> dir(raisins) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
32-
['_Quantity:0', '_Quantity:1', '__class__', ...
32+
['_Quantity#0', '_Quantity#1', '__class__', ...
3333
'description', 'price', 'subtotal', 'weight']
34-
>>> getattr(raisins, '_Quantity:0')
34+
>>> getattr(raisins, '_Quantity#0')
3535
10
36-
>>> getattr(raisins, '_Quantity:1')
36+
>>> getattr(raisins, '_Quantity#1')
3737
6.95
3838
3939
"""
@@ -47,7 +47,7 @@ def __init__(self):
4747
cls = self.__class__ # <2>
4848
prefix = cls.__name__
4949
index = cls.__counter
50-
self.storage_name = '_{}:{}'.format(prefix, index) # <3>
50+
self.storage_name = '_{}#{}'.format(prefix, index) # <3>
5151
cls.__counter += 1 # <4>
5252

5353
def __get__(self, instance, owner): # <5>

descriptors/bulkfood_v4b.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,20 @@
2929
3030
>>> raisins = LineItem('Golden raisins', 10, 6.95)
3131
>>> dir(raisins) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
32-
['_Quantity:0', '_Quantity:1', '__class__', ...
32+
['_Quantity#0', '_Quantity#1', '__class__', ...
3333
'description', 'price', 'subtotal', 'weight']
34-
>>> getattr(raisins, '_Quantity:0')
34+
>>> getattr(raisins, '_Quantity#0')
3535
10
36-
>>> getattr(raisins, '_Quantity:1')
36+
>>> getattr(raisins, '_Quantity#1')
3737
6.95
3838
3939
If the descriptor is accessed in the class, the descriptor object is
4040
returned:
4141
42-
>>> LineItem.price # doctest: +ELLIPSIS
42+
>>> LineItem.weight # doctest: +ELLIPSIS
4343
<bulkfood_v4b.Quantity object at 0x...>
44-
>>> br_nuts = LineItem('Brazil nuts', 10, 34.95)
45-
>>> br_nuts.price
46-
34.95
44+
>>> LineItem.weight.storage_name
45+
'_Quantity#0'
4746
4847
"""
4948

@@ -56,7 +55,7 @@ def __init__(self):
5655
cls = self.__class__
5756
prefix = cls.__name__
5857
index = cls.__counter
59-
self.storage_name = '_{}:{}'.format(prefix, index)
58+
self.storage_name = '_{}#{}'.format(prefix, index)
6059
cls.__counter += 1
6160

6261
def __get__(self, instance, owner):

0 commit comments

Comments
 (0)