-
Notifications
You must be signed in to change notification settings - Fork 1
Sourcery Starbot ⭐ refactored sudharsan2020/python-mastery #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| chars = '\|/' | ||
|
|
||
| def draw(rows, columns): | ||
| for r in range(rows): | ||
| for _ in range(rows): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| print(''.join(random.choice(chars) for _ in range(columns))) | ||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
This removes the following comments ( why? ): |
||
| print(len(routes), 'routes') | ||
|
|
||
| # -------------------------------------------------- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return records | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return records | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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=' ') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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=' ') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| print('</tr>') | ||
|
|
||
| def create_formatter(name): | ||
|
|
@@ -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}') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return formatter_cls() | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return records | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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=' ') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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=' ') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| print('</tr>') | ||
|
|
||
| def create_formatter(name): | ||
|
|
@@ -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}') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return formatter_cls() | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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=' ') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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=' ') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| print('</tr>') | ||
|
|
||
| def create_formatter(name): | ||
|
|
@@ -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}') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return formatter() | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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=' ') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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=' ') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| print('</tr>') | ||
|
|
||
| class ColumnFormatMixin: | ||
|
|
@@ -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}') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| if column_formats: | ||
| class formatter_cls(ColumnFormatMixin, formatter_cls): | ||
|
|
||
| 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}' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| @property | ||
| def value(self): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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)})" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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)})" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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)})" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| @classmethod | ||
| def set_fields(cls): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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)})" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| @classmethod | ||
| def create_init(cls): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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)})" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| @classmethod | ||
| def from_row(cls, row): | ||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return cls | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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)})" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| @classmethod | ||
| def from_row(cls, row): | ||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return cls | ||
|
|
||
| def typed_structure(clsname, **validators): | ||
| cls = type(clsname, (Structure,), validators) | ||
| return cls | ||
| return type(clsname, (Structure,), validators) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| class myobject(metaclass=mytype): | ||
| pass | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,13 +5,13 @@ | |
|
|
||
| class StructureMeta(type): | ||
| @classmethod | ||
| def __prepare__(meta, clsname, bases): | ||
| def __prepare__(cls, clsname, bases): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| class Structure(metaclass=StructureMeta): | ||
| _fields = () | ||
|
|
@@ -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}') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| 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)})" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| @classmethod | ||
| def from_row(cls, row): | ||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return cls | ||
|
|
||
| def typed_structure(clsname, **validators): | ||
| cls = type(clsname, (Structure,), validators) | ||
| return cls | ||
| return type(clsname, (Structure,), validators) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
csv_recordrefactored with the following changes:inline-immediately-returned-variable)