update from Atlas

This commit is contained in:
Luciano Ramalho
2015-04-02 10:34:57 -03:00
parent 573e1a94c4
commit cf96836b60
28 changed files with 8 additions and 59 deletions

View 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)

View 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', '-']

54
attic/objects/cards.py Normal file
View 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])

View 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)

View 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', '+']

View 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']

View File

@@ -1,3 +0,0 @@
# coding: cp1252
print('Olá, Mundo!')