updated contents from Atlas repo
This commit is contained in:
61
objects/attr_list.py
Normal file
61
objects/attr_list.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import sys
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def fn():
|
||||
pass
|
||||
|
||||
class Class():
|
||||
pass
|
||||
|
||||
# int, str,
|
||||
sample_types = [object, list, Class, type(Class), type(fn)]
|
||||
if '-' in sys.argv:
|
||||
del sample_types[0] # exlude `object`
|
||||
sample_objs = [type_() for type_ in sample_types[:-2]] + [Class, fn]
|
||||
sample_oids = [id(obj) for obj in sample_objs]
|
||||
|
||||
fmt = '{attr:17}' + '|{:8}' * len(sample_types)
|
||||
|
||||
headings = [t.__name__ for t in sample_types]
|
||||
headings[headings.index('Class')] = 'instance'
|
||||
headings[headings.index('type')] = 'class'
|
||||
|
||||
common_attrs = set()
|
||||
for obj in sample_objs:
|
||||
for attr_name in dir(obj):
|
||||
common_attrs.add(attr_name)
|
||||
|
||||
print(fmt.format(*headings, attr=''))
|
||||
|
||||
counter = Counter()
|
||||
for attr_name in sorted(common_attrs):
|
||||
if not attr_name.startswith('__'):
|
||||
continue
|
||||
flags = []
|
||||
found = 0
|
||||
for obj in sample_objs:
|
||||
try:
|
||||
attr = getattr(obj, attr_name)
|
||||
if type(attr) == type:
|
||||
flag = 'type'
|
||||
elif callable(attr):
|
||||
flag = 'method'
|
||||
else:
|
||||
flag = 'data'
|
||||
counter[id(obj)] += 1
|
||||
found += 1
|
||||
except AttributeError:
|
||||
flag = ''
|
||||
flags.append(flag)
|
||||
if '-' in sys.argv:
|
||||
include = found < len(sample_objs)
|
||||
else:
|
||||
include = found == len(sample_objs)
|
||||
if include:
|
||||
print(fmt.format(*flags, attr=attr_name))
|
||||
|
||||
counts = [counter[oid] for oid in sample_oids]
|
||||
print(fmt.format(*counts, attr='TOTALS'))
|
||||
|
||||
print(sys.argv)
|
||||
28
objects/attrs_not_in_object.py
Normal file
28
objects/attrs_not_in_object.py
Normal file
@@ -0,0 +1,28 @@
|
||||
|instance|list |function|class
|
||||
__add__ | |method | |
|
||||
__annotations__ | | |data |
|
||||
__call__ | | |method |method
|
||||
__closure__ | | |data |
|
||||
__code__ | | |data |
|
||||
__contains__ | |method | |
|
||||
__defaults__ | | |data |
|
||||
__delitem__ | |method | |
|
||||
__dict__ |data | |data |data
|
||||
__get__ | | |method |
|
||||
__getitem__ | |method | |
|
||||
__globals__ | | |data |
|
||||
__iadd__ | |method | |
|
||||
__imul__ | |method | |
|
||||
__iter__ | |method | |
|
||||
__kwdefaults__ | | |data |
|
||||
__len__ | |method | |
|
||||
__module__ |data | |data |data
|
||||
__mul__ | |method | |
|
||||
__name__ | | |data |data
|
||||
__qualname__ | | |data |data
|
||||
__reversed__ | |method | |
|
||||
__rmul__ | |method | |
|
||||
__setitem__ | |method | |
|
||||
__weakref__ |data | | |data
|
||||
TOTALS | 25| 34| 34| 28
|
||||
['attr_list.py', '-']
|
||||
29
objects/bus.py
Normal file
29
objects/bus.py
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
"""
|
||||
>>> import copy
|
||||
>>> bus1 = Bus(['Alice', 'Bill', 'Claire', 'David'])
|
||||
>>> bus2 = copy.copy(bus1)
|
||||
>>> bus3 = copy.deepcopy(bus1)
|
||||
>>> bus1.drop('Bill')
|
||||
>>> bus2.passengers
|
||||
['Alice', 'Claire', 'David']
|
||||
>>> bus3.passengers
|
||||
['Alice', 'Bill', 'Claire', 'David']
|
||||
|
||||
"""
|
||||
|
||||
# BEGIN BUS_CLASS
|
||||
class Bus:
|
||||
|
||||
def __init__(self, passengers=None):
|
||||
if passengers is None:
|
||||
self.passengers = []
|
||||
else:
|
||||
self.passengers = list(passengers)
|
||||
|
||||
def pick(self, name):
|
||||
self.passengers.append(name)
|
||||
|
||||
def drop(self, name):
|
||||
self.passengers.remove(name)
|
||||
# END BUS_CLASS
|
||||
54
objects/cards.py
Normal file
54
objects/cards.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""
|
||||
|
||||
Spadille is the nickname for the Ace of Spades in some games
|
||||
(see `Webster 1913`_)
|
||||
|
||||
>>> beer_card = Card('7', Suite.diamonds)
|
||||
>>> beer_card
|
||||
Card('7', Suite.diamonds)
|
||||
>>> spadille = Card('A', Suite.spades, long_rank='Ace')
|
||||
>>> spadille
|
||||
Card('A', Suite.spades)
|
||||
>>> print(spadille)
|
||||
Ace of spades
|
||||
>>> bytes(spadille)
|
||||
b'A\\x01'
|
||||
>>> charles = Card('K', Suite.hearts)
|
||||
>>> bytes(charles)
|
||||
b'K\\x04'
|
||||
>>> big_cassino = Card('10', Suite.diamonds)
|
||||
>>> bytes(big_cassino)
|
||||
b'T\\x02'
|
||||
|
||||
__ http://machaut.uchicago.edu/cgi-bin/WEBSTER.sh?WORD=spadille
|
||||
|
||||
"""
|
||||
|
||||
from enum import Enum
|
||||
|
||||
Suite = Enum('Suite', 'spades diamonds clubs hearts')
|
||||
|
||||
class Card:
|
||||
|
||||
def __init__(self, rank, suite, *, long_rank=None):
|
||||
self.rank = rank
|
||||
if long_rank is None:
|
||||
self.long_rank = self.rank
|
||||
else:
|
||||
self.long_rank = long_rank
|
||||
self.suite = suite
|
||||
|
||||
def __str__(self):
|
||||
return '{long_rank} of {suite.name}'.format(**self.__dict__)
|
||||
|
||||
def __repr__(self):
|
||||
constructor = '{cls.__name__}({args})'
|
||||
args = '{0.rank!r}, Suite.{0.suite.name}'.format(self)
|
||||
return constructor.format(cls=self.__class__, args=args)
|
||||
|
||||
def __bytes__(self):
|
||||
if self.rank == '10':
|
||||
rank_byte = b'T'
|
||||
else:
|
||||
rank_byte = bytes([ord(self.rank)])
|
||||
return rank_byte + bytes([self.suite.value])
|
||||
122
objects/cards_format.py
Normal file
122
objects/cards_format.py
Normal file
@@ -0,0 +1,122 @@
|
||||
"""
|
||||
Test Suite formatting:
|
||||
|
||||
>>> Suite.spades
|
||||
<Suite.spades: 0>
|
||||
>>> print(Suite.spades)
|
||||
Suite.spades
|
||||
>>> format(Suite.spades)
|
||||
'spades'
|
||||
>>> format(Suite.spades, 's')
|
||||
'spades'
|
||||
>>> format(Suite.spades, 'S')
|
||||
'Spades'
|
||||
>>> format(Suite.spades, 'p')
|
||||
'♠'
|
||||
>>> format(Suite.spades, 'z')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Invalid format spec 'z' for object of type 'Suite'
|
||||
>>> bytes(Suite.spades), bytes(Suite.clubs)
|
||||
(b'\\x00', b'\\x03')
|
||||
|
||||
Spadille is the nickname for the Ace of Spades in some games
|
||||
(see `Webster 1913`_)
|
||||
|
||||
>>> spadille = Card('A', Suite.spades, long_rank='Ace')
|
||||
>>> spadille
|
||||
Card('A', 'spades')
|
||||
>>> print(spadille)
|
||||
Ace of spades
|
||||
>>> format(spadille)
|
||||
'A-spades'
|
||||
>>> format(spadille, 'r/p')
|
||||
'A/♠'
|
||||
>>> format(spadille, 'R of S')
|
||||
'Ace of Spades'
|
||||
>>> bytes(spadille)
|
||||
b'A\\x00'
|
||||
>>> beer_card = Card('7', Suite.diamonds)
|
||||
>>> bytes(beer_card)
|
||||
b'7\\x02'
|
||||
>>> big_cassino = Card('10', Suite.diamonds)
|
||||
>>> bytes(big_cassino)
|
||||
b'10\\x02'
|
||||
|
||||
__ http://machaut.uchicago.edu/cgi-bin/WEBSTER.sh?WORD=spadille
|
||||
|
||||
"""
|
||||
|
||||
from enum import Enum
|
||||
from operator import attrgetter
|
||||
import re
|
||||
|
||||
spades diamonds clubs hearts
|
||||
|
||||
class Suite(Enum):
|
||||
spades = '\u2660' # U+2660 ♠ BLACK SPADE SUIT
|
||||
diamonds = '\u2662' # U+2662 ♢ WHITE DIAMOND SUIT
|
||||
clubs = '\u2663' # U+2663 ♣ BLACK CLUB SUIT
|
||||
hearts = '\u2661' # U+2661 ♡ WHITE HEART SUIT
|
||||
|
||||
def format_p(self):
|
||||
return chr(0x2660 + self.value)
|
||||
|
||||
def format_s(self):
|
||||
return self.name
|
||||
|
||||
def format_S(self):
|
||||
return self.name.capitalize()
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes([self.value])
|
||||
|
||||
def __format__(self, format_spec):
|
||||
use_spec = 's' if format_spec == '' else format_spec
|
||||
format_method = getattr(self, 'format_' + use_spec, None)
|
||||
if format_method:
|
||||
return format_method()
|
||||
|
||||
msg = "Invalid format spec {!r} for object of type 'Suite'"
|
||||
raise ValueError(msg.format(format_spec))
|
||||
|
||||
class Card:
|
||||
|
||||
def __init__(self, rank, suite, *, long_rank=None):
|
||||
self.rank = rank
|
||||
if long_rank is None:
|
||||
self.long_rank = self.rank
|
||||
else:
|
||||
self.long_rank = long_rank
|
||||
self.suite = suite
|
||||
|
||||
def __str__(self):
|
||||
return '{long_rank} of {suite.name}'.format(**self.__dict__)
|
||||
|
||||
def __repr__(self):
|
||||
template = '{cls.__name__}({rank!r}, {suite.name!r})'
|
||||
return template.format(cls=self.__class__, **self.__dict__)
|
||||
|
||||
def __bytes__(self):
|
||||
rank_bytes = bytes(ord(char) for char in self.rank)
|
||||
return rank_bytes + bytes(self.suite)
|
||||
|
||||
rank_codes = {
|
||||
'r': attrgetter('rank'),
|
||||
'R': attrgetter('long_rank'),
|
||||
}
|
||||
|
||||
def __format__(self, format_spec):
|
||||
if not format_spec:
|
||||
format_spec = 'r-s'
|
||||
result = []
|
||||
for code in format_spec:
|
||||
if code in Card.rank_codes:
|
||||
result.append(Card.rank_codes[code](self))
|
||||
else:
|
||||
try:
|
||||
result.append(format(self.suite, code))
|
||||
except ValueError:
|
||||
result.append(code)
|
||||
return ''.join(result)
|
||||
|
||||
28
objects/cheese.py
Normal file
28
objects/cheese.py
Normal file
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
>>> import weakref
|
||||
>>> stock = weakref.WeakValueDictionary()
|
||||
>>> catalog = [Cheese('Red Leicester'), Cheese('Tilsit'),
|
||||
... Cheese('Brie'), Cheese('Parmesan')]
|
||||
...
|
||||
>>> for cheese in catalog:
|
||||
... stock[cheese.kind] = cheese
|
||||
...
|
||||
>>> sorted(stock.keys())
|
||||
['Brie', 'Parmesan', 'Red Leicester', 'Tilsit']
|
||||
>>> del catalog
|
||||
>>> sorted(stock.keys())
|
||||
['Parmesan']
|
||||
>>> del cheese
|
||||
>>> sorted(stock.keys())
|
||||
[]
|
||||
"""
|
||||
|
||||
# BEGIN CHEESE_CLASS
|
||||
class Cheese:
|
||||
|
||||
def __init__(self, kind):
|
||||
self.kind = kind
|
||||
|
||||
def __repr__(self):
|
||||
return 'Cheese(%r)' % self.kind
|
||||
# END CHEESE_CLASS
|
||||
25
objects/common_attrs.txt
Normal file
25
objects/common_attrs.txt
Normal file
@@ -0,0 +1,25 @@
|
||||
|object |instance|list |function|class
|
||||
__class__ |type |type |type |type |type
|
||||
__delattr__ |method |method |method |method |method
|
||||
__dir__ |method |method |method |method |method
|
||||
__doc__ |data |data |data |data |data
|
||||
__eq__ |method |method |method |method |method
|
||||
__format__ |method |method |method |method |method
|
||||
__ge__ |method |method |method |method |method
|
||||
__getattribute__ |method |method |method |method |method
|
||||
__gt__ |method |method |method |method |method
|
||||
__hash__ |method |method |data |method |method
|
||||
__init__ |method |method |method |method |method
|
||||
__le__ |method |method |method |method |method
|
||||
__lt__ |method |method |method |method |method
|
||||
__ne__ |method |method |method |method |method
|
||||
__new__ |method |method |method |method |method
|
||||
__reduce__ |method |method |method |method |method
|
||||
__reduce_ex__ |method |method |method |method |method
|
||||
__repr__ |method |method |method |method |method
|
||||
__setattr__ |method |method |method |method |method
|
||||
__sizeof__ |method |method |method |method |method
|
||||
__str__ |method |method |method |method |method
|
||||
__subclasshook__ |method |method |method |method |method
|
||||
TOTALS | 22| 25| 34| 34| 28
|
||||
['attr_list.py', '+']
|
||||
47
objects/haunted_bus.py
Normal file
47
objects/haunted_bus.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
>>> bus1 = HountedBus(['Alice', 'Bill'])
|
||||
>>> bus1.passengers
|
||||
['Alice', 'Bill']
|
||||
>>> bus1.pick('Charlie')
|
||||
>>> bus1.drop('Alice')
|
||||
>>> bus1.passengers
|
||||
['Bill', 'Charlie']
|
||||
>>> bus2 = HountedBus()
|
||||
>>> bus2.pick('Carrie')
|
||||
>>> bus2.passengers
|
||||
['Carrie']
|
||||
>>> bus3 = HountedBus()
|
||||
>>> bus3.passengers
|
||||
['Carrie']
|
||||
>>> bus3.pick('Dave')
|
||||
>>> bus2.passengers
|
||||
['Carrie', 'Dave']
|
||||
>>> bus2.passengers is bus3.passengers
|
||||
True
|
||||
>>> bus1.passengers
|
||||
['Bill', 'Charlie']
|
||||
|
||||
|
||||
>>> dir(HountedBus.__init__) # doctest: +ELLIPSIS
|
||||
['__annotations__', '__call__', ..., '__defaults__', ...]
|
||||
>>> HountedBus.__init__.__defaults__
|
||||
(['Carrie', 'Dave'],)
|
||||
>>> HountedBus.__init__.__defaults__[0] is bus2.passengers
|
||||
True
|
||||
|
||||
"""
|
||||
|
||||
# BEGIN HAUNTED_BUS_CLASS
|
||||
class HountedBus:
|
||||
"""A bus model hounted by ghost passengers"""
|
||||
|
||||
def __init__(self, passengers=[]): # <1>
|
||||
self.passengers = passengers # <2>
|
||||
|
||||
def pick(self, name):
|
||||
self.passengers.append(name) # <3>
|
||||
|
||||
def drop(self, name):
|
||||
self.passengers.remove(name)
|
||||
# END HAUNTED_BUS_CLASS
|
||||
|
||||
28
objects/not_so_common_attrs.txt
Normal file
28
objects/not_so_common_attrs.txt
Normal file
@@ -0,0 +1,28 @@
|
||||
|object |instance|list |function|class
|
||||
__add__ | | |method | |
|
||||
__annotations__ | | | |data |
|
||||
__call__ | | | |method |method
|
||||
__closure__ | | | |data |
|
||||
__code__ | | | |data |
|
||||
__contains__ | | |method | |
|
||||
__defaults__ | | | |data |
|
||||
__delitem__ | | |method | |
|
||||
__dict__ | |data | |data |data
|
||||
__get__ | | | |method |
|
||||
__getitem__ | | |method | |
|
||||
__globals__ | | | |data |
|
||||
__iadd__ | | |method | |
|
||||
__imul__ | | |method | |
|
||||
__iter__ | | |method | |
|
||||
__kwdefaults__ | | | |data |
|
||||
__len__ | | |method | |
|
||||
__module__ | |data | |data |data
|
||||
__mul__ | | |method | |
|
||||
__name__ | | | |data |data
|
||||
__qualname__ | | | |data |data
|
||||
__reversed__ | | |method | |
|
||||
__rmul__ | | |method | |
|
||||
__setitem__ | | |method | |
|
||||
__weakref__ | |data | | |data
|
||||
TOTALS | 22| 25| 34| 34| 28
|
||||
['attr_list.py']
|
||||
26
objects/twilight_bus.py
Normal file
26
objects/twilight_bus.py
Normal file
@@ -0,0 +1,26 @@
|
||||
"""
|
||||
>>> basketball_team = ['Sue', 'Tina', 'Maya', 'Diana', 'Pat']
|
||||
>>> bus = TwilightBus(basketball_team)
|
||||
>>> bus.drop('Tina')
|
||||
>>> bus.drop('Pat')
|
||||
>>> basketball_team
|
||||
['Sue', 'Maya', 'Diana']
|
||||
"""
|
||||
|
||||
# BEGIN TWILIGHT_BUS_CLASS
|
||||
class TwilightBus:
|
||||
"""A bus model that makes passengers vanish"""
|
||||
|
||||
def __init__(self, passengers=None):
|
||||
if passengers is None:
|
||||
self.passengers = [] # <1>
|
||||
else:
|
||||
self.passengers = passengers #<2>
|
||||
|
||||
def pick(self, name):
|
||||
self.passengers.append(name)
|
||||
|
||||
def drop(self, name):
|
||||
self.passengers.remove(name) # <3>
|
||||
# END TWILIGHT_BUS_CLASS
|
||||
|
||||
Reference in New Issue
Block a user