Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Data/stocksim.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ def read_history(filename):

# Format CSV record
def csv_record(fields):
s = '"%s",%0.2f,"%s","%s",%0.2f,%0.2f,%0.2f,%0.2f,%d' % tuple(fields)
return s
return '"%s",%0.2f,"%s","%s",%0.2f,%0.2f,%0.2f,%0.2f,%d' % tuple(fields)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function csv_record refactored with the following changes:


class StockTrack(object):
def __init__(self,name):
Expand Down
2 changes: 1 addition & 1 deletion Solutions/1_1/art.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
chars = '\|/'

def draw(rows, columns):
for r in range(rows):
for _ in range(rows):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function draw refactored with the following changes:

print(''.join(random.choice(chars) for _ in range(columns)))

if __name__ == '__main__':
Expand Down
23 changes: 5 additions & 18 deletions Solutions/2_6/cta.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,11 @@

tracemalloc.start()

if True:
# Part (b)
import reader
rows = reader.read_csv_as_dicts('../../Data/ctabus.csv',
[sys.intern, sys.intern, sys.intern, int])
else:
# Part (d) - Challenge
import colreader
rows = colreader.read_csv_as_columns('../../Data/ctabus.csv',
[sys.intern, sys.intern, sys.intern, int])

# --------------------------------------------------
# Question 1: How many bus routes are in Chicago?
# Solution: Use a set to get unique values.

routes = set()
for row in rows:
routes.add(row['route'])
# Part (b)
import reader
rows = reader.read_csv_as_dicts('../../Data/ctabus.csv',
[sys.intern, sys.intern, sys.intern, int])
routes = {row['route'] for row in rows}
Comment on lines -10 to +14
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 10-27 refactored with the following changes:

This removes the following comments ( why? ):

# Part (d) - Challenge
# --------------------------------------------------
# Question 1:  How many bus routes are in Chicago?
# Solution: Use a set to get unique values.

print(len(routes), 'routes')

# --------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions Solutions/3_3/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ def read_csv_as_instances(filename, cls):
with open(filename) as f:
rows = csv.reader(f)
headers = next(rows)
for row in rows:
records.append(cls.from_row(row))
records.extend(cls.from_row(row) for row in rows)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_csv_as_instances refactored with the following changes:

return records
3 changes: 1 addition & 2 deletions Solutions/3_5/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ def read_csv_as_instances(filename, cls):
with open(filename) as f:
rows = csv.reader(f)
headers = next(rows)
for row in rows:
records.append(cls.from_row(row))
records.extend(cls.from_row(row) for row in rows)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_csv_as_instances refactored with the following changes:

return records
6 changes: 3 additions & 3 deletions Solutions/3_5/tableformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class HTMLTableFormatter(TableFormatter):
def headings(self, headers):
print('<tr>', end=' ')
for h in headers:
print('<th>%s</th>' % h, end=' ')
print(f'<th>{h}</th>', end=' ')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTMLTableFormatter.headings refactored with the following changes:

print('</tr>')

def row(self, rowdata):
print('<tr>', end=' ')
for d in rowdata:
print('<td>%s</td>' % d, end=' ')
print(f'<td>{d}</td>', end=' ')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTMLTableFormatter.row refactored with the following changes:

print('</tr>')

def create_formatter(name):
Expand All @@ -49,7 +49,7 @@ def create_formatter(name):
elif name == 'html':
formatter_cls = HTMLTableFormatter
else:
raise RuntimeError('Unknown format %s' % name)
raise RuntimeError(f'Unknown format {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function create_formatter refactored with the following changes:

return formatter_cls()


Expand Down
3 changes: 1 addition & 2 deletions Solutions/3_6/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ def read_csv_as_instances(filename, cls):
with open(filename) as f:
rows = csv.reader(f)
headers = next(rows)
for row in rows:
records.append(cls.from_row(row))
records.extend(cls.from_row(row) for row in rows)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_csv_as_instances refactored with the following changes:

return records
6 changes: 3 additions & 3 deletions Solutions/3_6/tableformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class HTMLTableFormatter(TableFormatter):
def headings(self, headers):
print('<tr>', end=' ')
for h in headers:
print('<th>%s</th>' % h, end=' ')
print(f'<th>{h}</th>', end=' ')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTMLTableFormatter.headings refactored with the following changes:

print('</tr>')

def row(self, rowdata):
print('<tr>', end=' ')
for d in rowdata:
print('<td>%s</td>' % d, end=' ')
print(f'<td>{d}</td>', end=' ')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTMLTableFormatter.row refactored with the following changes:

print('</tr>')

def create_formatter(name):
Expand All @@ -49,7 +49,7 @@ def create_formatter(name):
elif name == 'html':
formatter_cls = HTMLTableFormatter
else:
raise RuntimeError('Unknown format %s' % name)
raise RuntimeError(f'Unknown format {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function create_formatter refactored with the following changes:

return formatter_cls()


Expand Down
6 changes: 3 additions & 3 deletions Solutions/3_7/tableformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class HTMLTableFormatter(TableFormatter):
def headings(self, headers):
print('<tr>', end=' ')
for h in headers:
print('<th>%s</th>' % h, end=' ')
print(f'<th>{h}</th>', end=' ')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTMLTableFormatter.headings refactored with the following changes:

print('</tr>')

def row(self, rowdata):
print('<tr>', end=' ')
for d in rowdata:
print('<td>%s</td>' % d, end=' ')
print(f'<td>{d}</td>', end=' ')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTMLTableFormatter.row refactored with the following changes:

print('</tr>')

def create_formatter(name):
Expand All @@ -55,7 +55,7 @@ def create_formatter(name):
elif name == 'html':
formatter = HTMLTableFormatter
else:
raise RuntimeError('Unknown format %s' % name)
raise RuntimeError(f'Unknown format {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function create_formatter refactored with the following changes:

return formatter()


Expand Down
6 changes: 3 additions & 3 deletions Solutions/3_8/tableformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ class HTMLTableFormatter(TableFormatter):
def headings(self, headers):
print('<tr>', end=' ')
for h in headers:
print('<th>%s</th>' % h, end=' ')
print(f'<th>{h}</th>', end=' ')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTMLTableFormatter.headings refactored with the following changes:

print('</tr>')

def row(self, rowdata):
print('<tr>', end=' ')
for d in rowdata:
print('<td>%s</td>' % d, end=' ')
print(f'<td>{d}</td>', end=' ')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function HTMLTableFormatter.row refactored with the following changes:

print('</tr>')

class ColumnFormatMixin:
Expand All @@ -66,7 +66,7 @@ def create_formatter(name, column_formats=None, upper_headers=False):
elif name == 'html':
formatter_cls = HTMLTableFormatter
else:
raise RuntimeError('Unknown format %s' % name)
raise RuntimeError(f'Unknown format {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function create_formatter refactored with the following changes:


if column_formats:
class formatter_cls(ColumnFormatMixin, formatter_cls):
Expand Down
2 changes: 1 addition & 1 deletion Solutions/5_4/typedproperty.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# typedproperty.py

def typedproperty(name, expected_type):
private_name = '_' + name
private_name = f'_{name}'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function typedproperty refactored with the following changes:


@property
def value(self):
Expand Down
5 changes: 2 additions & 3 deletions Solutions/6_1/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def __setattr__(self, name, value):
if name.startswith('_') or name in self._fields:
super().__setattr__(name, value)
else:
raise AttributeError('No attribute %s' % name)
raise AttributeError(f'No attribute {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__setattr__ refactored with the following changes:


def __repr__(self):
return '%s(%s)' % (type(self).__name__,
', '.join(repr(getattr(self, name)) for name in self._fields))
return f"{type(self).__name__}({', '.join(repr(getattr(self, name)) for name in self._fields)})"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__repr__ refactored with the following changes:

5 changes: 2 additions & 3 deletions Solutions/6_2/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def __setattr__(self, name, value):
if name.startswith('_') or name in self._fields:
super().__setattr__(name, value)
else:
raise AttributeError('No attribute %s' % name)
raise AttributeError(f'No attribute {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__setattr__ refactored with the following changes:


def __repr__(self):
return '%s(%s)' % (type(self).__name__,
', '.join(repr(getattr(self, name)) for name in self._fields))
return f"{type(self).__name__}({', '.join(repr(getattr(self, name)) for name in self._fields)})"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__repr__ refactored with the following changes:

5 changes: 2 additions & 3 deletions Solutions/6_3/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ def __setattr__(self, name, value):
if name.startswith('_') or name in self._fields:
super().__setattr__(name, value)
else:
raise AttributeError('No attribute %s' % name)
raise AttributeError(f'No attribute {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__setattr__ refactored with the following changes:


def __repr__(self):
return '%s(%s)' % (type(self).__name__,
', '.join(repr(getattr(self, name)) for name in self._fields))
return f"{type(self).__name__}({', '.join(repr(getattr(self, name)) for name in self._fields)})"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__repr__ refactored with the following changes:


@classmethod
def set_fields(cls):
Expand Down
5 changes: 2 additions & 3 deletions Solutions/6_4/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ def __setattr__(self, name, value):
if name.startswith('_') or name in self._fields:
super().__setattr__(name, value)
else:
raise AttributeError('No attribute %s' % name)
raise AttributeError(f'No attribute {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__setattr__ refactored with the following changes:


def __repr__(self):
return '%s(%s)' % (type(self).__name__,
', '.join(repr(getattr(self, name)) for name in self._fields))
return f"{type(self).__name__}({', '.join(repr(getattr(self, name)) for name in self._fields)})"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__repr__ refactored with the following changes:


@classmethod
def create_init(cls):
Expand Down
14 changes: 7 additions & 7 deletions Solutions/7_3/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ def __setattr__(self, name, value):
if name.startswith('_') or name in self._fields:
super().__setattr__(name, value)
else:
raise AttributeError('No attribute %s' % name)
raise AttributeError(f'No attribute {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__setattr__ refactored with the following changes:


def __repr__(self):
return '%s(%s)' % (type(self).__name__,
', '.join(repr(getattr(self, name)) for name in self._fields))
return f"{type(self).__name__}({', '.join(repr(getattr(self, name)) for name in self._fields)})"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__repr__ refactored with the following changes:


@classmethod
def from_row(cls, row):
Expand Down Expand Up @@ -54,17 +53,18 @@ def validate_attributes(cls):
setattr(cls, name, validated(val))

# Collect all of the field names
cls._fields = tuple([v.name for v in validators])
cls._fields = tuple(v.name for v in validators)

# Collect type conversions. The lambda x:x is an identity
# function that's used in case no expected_type is found.
cls._types = tuple([ getattr(v, 'expected_type', lambda x: x)
for v in validators ])
cls._types = tuple(
getattr(v, 'expected_type', lambda x: x) for v in validators
)

# Create the __init__ method
if cls._fields:
cls.create_init()


Comment on lines -57 to +68
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function validate_attributes refactored with the following changes:

return cls

17 changes: 8 additions & 9 deletions Solutions/7_4/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ def __setattr__(self, name, value):
if name.startswith('_') or name in self._fields:
super().__setattr__(name, value)
else:
raise AttributeError('No attribute %s' % name)
raise AttributeError(f'No attribute {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__setattr__ refactored with the following changes:


def __repr__(self):
return '%s(%s)' % (type(self).__name__,
', '.join(repr(getattr(self, name)) for name in self._fields))
return f"{type(self).__name__}({', '.join(repr(getattr(self, name)) for name in self._fields)})"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__repr__ refactored with the following changes:


@classmethod
def from_row(cls, row):
Expand Down Expand Up @@ -54,20 +53,20 @@ def validate_attributes(cls):
setattr(cls, name, validated(val))

# Collect all of the field names
cls._fields = tuple([v.name for v in validators])
cls._fields = tuple(v.name for v in validators)

# Collect type conversions. The lambda x:x is an identity
# function that's used in case no expected_type is found.
cls._types = tuple([ getattr(v, 'expected_type', lambda x: x)
for v in validators ])
cls._types = tuple(
getattr(v, 'expected_type', lambda x: x) for v in validators
)

# Create the __init__ method
if cls._fields:
cls.create_init()


Comment on lines -57 to +68
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function validate_attributes refactored with the following changes:

return cls

def typed_structure(clsname, **validators):
cls = type(clsname, (Structure,), validators)
return cls
return type(clsname, (Structure,), validators)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function typed_structure refactored with the following changes:

4 changes: 2 additions & 2 deletions Solutions/7_5/mymeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

class mytype(type):
@staticmethod
def __new__(meta, name, bases, __dict__):
def __new__(cls, name, bases, __dict__):
print("Creating class :", name)
print("Base classes :", bases)
print("Attributes :", list(__dict__.keys()))
return super().__new__(meta, name, bases, __dict__)
return super().__new__(cls, name, bases, __dict__)
Comment on lines -5 to +9
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function mytype.__new__ refactored with the following changes:


class myobject(metaclass=mytype):
pass
Expand Down
23 changes: 11 additions & 12 deletions Solutions/7_6/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

class StructureMeta(type):
@classmethod
def __prepare__(meta, clsname, bases):
def __prepare__(cls, clsname, bases):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function StructureMeta.__prepare__ refactored with the following changes:

return ChainMap({}, Validator.validators)

@staticmethod
def __new__(meta, name, bases, methods):
def __new__(cls, name, bases, methods):
methods = methods.maps[0]
return super().__new__(meta, name, bases, methods)
return super().__new__(cls, name, bases, methods)
Comment on lines -12 to +14
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function StructureMeta.__new__ refactored with the following changes:


class Structure(metaclass=StructureMeta):
_fields = ()
Expand All @@ -21,11 +21,10 @@ def __setattr__(self, name, value):
if name.startswith('_') or name in self._fields:
super().__setattr__(name, value)
else:
raise AttributeError('No attribute %s' % name)
raise AttributeError(f'No attribute {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__setattr__ refactored with the following changes:


def __repr__(self):
return '%s(%s)' % (type(self).__name__,
', '.join(repr(getattr(self, name)) for name in self._fields))
return f"{type(self).__name__}({', '.join(repr(getattr(self, name)) for name in self._fields)})"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Structure.__repr__ refactored with the following changes:


@classmethod
def from_row(cls, row):
Expand Down Expand Up @@ -66,20 +65,20 @@ def validate_attributes(cls):
setattr(cls, name, validated(val))

# Collect all of the field names
cls._fields = tuple([v.name for v in validators])
cls._fields = tuple(v.name for v in validators)

# Collect type conversions. The lambda x:x is an identity
# function that's used in case no expected_type is found.
cls._types = tuple([ getattr(v, 'expected_type', lambda x: x)
for v in validators ])
cls._types = tuple(
getattr(v, 'expected_type', lambda x: x) for v in validators
)

# Create the __init__ method
if cls._fields:
cls.create_init()


Comment on lines -69 to +80
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function validate_attributes refactored with the following changes:

return cls

def typed_structure(clsname, **validators):
cls = type(clsname, (Structure,), validators)
return cls
return type(clsname, (Structure,), validators)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function typed_structure refactored with the following changes:

Loading