From 61cf3f1127b985d8c12265e2c029f1b9ce98f103 Mon Sep 17 00:00:00 2001 From: Sourcery AI Date: Mon, 4 Sep 2023 07:58:55 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- Data/stocksim.py | 3 +-- Solutions/1_1/art.py | 2 +- Solutions/2_6/cta.py | 23 ++++--------------- Solutions/3_3/reader.py | 3 +-- Solutions/3_5/reader.py | 3 +-- Solutions/3_5/tableformat.py | 6 ++--- Solutions/3_6/reader.py | 3 +-- Solutions/3_6/tableformat.py | 6 ++--- Solutions/3_7/tableformat.py | 6 ++--- Solutions/3_8/tableformat.py | 6 ++--- Solutions/5_4/typedproperty.py | 2 +- Solutions/6_1/structure.py | 5 ++-- Solutions/6_2/structure.py | 5 ++-- Solutions/6_3/structure.py | 5 ++-- Solutions/6_4/structure.py | 5 ++-- Solutions/7_3/structure.py | 14 +++++------ Solutions/7_4/structure.py | 17 +++++++------- Solutions/7_5/mymeta.py | 4 ++-- Solutions/7_6/structure.py | 23 +++++++++---------- Solutions/7_6/tableformat.py | 6 ++--- Solutions/8_1/structure.py | 23 +++++++++---------- Solutions/8_2/structure.py | 23 +++++++++---------- Solutions/8_2/tableformat.py | 6 ++--- Solutions/8_3/structure.py | 23 +++++++++---------- Solutions/8_3/tableformat.py | 6 ++--- Solutions/8_6/cofollow.py | 2 +- Solutions/8_6/structure.py | 23 +++++++++---------- Solutions/8_6/tableformat.py | 6 ++--- Solutions/9_1/simplemod.py | 2 +- Solutions/9_2/structly/structure.py | 23 +++++++++---------- Solutions/9_2/structly/tableformat.py | 6 ++--- Solutions/9_3/structly/structure.py | 23 +++++++++---------- .../9_3/structly/tableformat/formats/html.py | 4 ++-- .../9_3/structly/tableformat/formatter.py | 2 +- Solutions/9_4/structly/structure.py | 23 +++++++++---------- .../9_4/structly/tableformat/formats/html.py | 4 ++-- .../9_4/structly/tableformat/formatter.py | 4 ++-- 37 files changed, 160 insertions(+), 190 deletions(-) diff --git a/Data/stocksim.py b/Data/stocksim.py index 63fc602d..ec05cae0 100755 --- a/Data/stocksim.py +++ b/Data/stocksim.py @@ -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) class StockTrack(object): def __init__(self,name): diff --git a/Solutions/1_1/art.py b/Solutions/1_1/art.py index 7e989db3..b92c9858 100644 --- a/Solutions/1_1/art.py +++ b/Solutions/1_1/art.py @@ -6,7 +6,7 @@ chars = '\|/' def draw(rows, columns): - for r in range(rows): + for _ in range(rows): print(''.join(random.choice(chars) for _ in range(columns))) if __name__ == '__main__': diff --git a/Solutions/2_6/cta.py b/Solutions/2_6/cta.py index c8be14ff..370a9817 100644 --- a/Solutions/2_6/cta.py +++ b/Solutions/2_6/cta.py @@ -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} print(len(routes), 'routes') # -------------------------------------------------- diff --git a/Solutions/3_3/reader.py b/Solutions/3_3/reader.py index 79255b6d..1e2d09b3 100644 --- a/Solutions/3_3/reader.py +++ b/Solutions/3_3/reader.py @@ -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) return records diff --git a/Solutions/3_5/reader.py b/Solutions/3_5/reader.py index 79255b6d..1e2d09b3 100644 --- a/Solutions/3_5/reader.py +++ b/Solutions/3_5/reader.py @@ -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) return records diff --git a/Solutions/3_5/tableformat.py b/Solutions/3_5/tableformat.py index 6f3cba36..ea6b295f 100644 --- a/Solutions/3_5/tableformat.py +++ b/Solutions/3_5/tableformat.py @@ -32,13 +32,13 @@ class HTMLTableFormatter(TableFormatter): def headings(self, headers): print('', end=' ') for h in headers: - print('%s' % h, end=' ') + print(f'{h}', end=' ') print('') def row(self, rowdata): print('', end=' ') for d in rowdata: - print('%s' % d, end=' ') + print(f'{d}', end=' ') print('') 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}') return formatter_cls() diff --git a/Solutions/3_6/reader.py b/Solutions/3_6/reader.py index 79255b6d..1e2d09b3 100644 --- a/Solutions/3_6/reader.py +++ b/Solutions/3_6/reader.py @@ -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) return records diff --git a/Solutions/3_6/tableformat.py b/Solutions/3_6/tableformat.py index 6f3cba36..ea6b295f 100644 --- a/Solutions/3_6/tableformat.py +++ b/Solutions/3_6/tableformat.py @@ -32,13 +32,13 @@ class HTMLTableFormatter(TableFormatter): def headings(self, headers): print('', end=' ') for h in headers: - print('%s' % h, end=' ') + print(f'{h}', end=' ') print('') def row(self, rowdata): print('', end=' ') for d in rowdata: - print('%s' % d, end=' ') + print(f'{d}', end=' ') print('') 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}') return formatter_cls() diff --git a/Solutions/3_7/tableformat.py b/Solutions/3_7/tableformat.py index cd4a93b4..5dab0a0c 100644 --- a/Solutions/3_7/tableformat.py +++ b/Solutions/3_7/tableformat.py @@ -38,13 +38,13 @@ class HTMLTableFormatter(TableFormatter): def headings(self, headers): print('', end=' ') for h in headers: - print('%s' % h, end=' ') + print(f'{h}', end=' ') print('') def row(self, rowdata): print('', end=' ') for d in rowdata: - print('%s' % d, end=' ') + print(f'{d}', end=' ') print('') 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}') return formatter() diff --git a/Solutions/3_8/tableformat.py b/Solutions/3_8/tableformat.py index f50ef48a..2f3845ef 100644 --- a/Solutions/3_8/tableformat.py +++ b/Solutions/3_8/tableformat.py @@ -39,13 +39,13 @@ class HTMLTableFormatter(TableFormatter): def headings(self, headers): print('', end=' ') for h in headers: - print('%s' % h, end=' ') + print(f'{h}', end=' ') print('') def row(self, rowdata): print('', end=' ') for d in rowdata: - print('%s' % d, end=' ') + print(f'{d}', end=' ') print('') 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}') if column_formats: class formatter_cls(ColumnFormatMixin, formatter_cls): diff --git a/Solutions/5_4/typedproperty.py b/Solutions/5_4/typedproperty.py index ec61b348..ed87a7b5 100644 --- a/Solutions/5_4/typedproperty.py +++ b/Solutions/5_4/typedproperty.py @@ -1,7 +1,7 @@ # typedproperty.py def typedproperty(name, expected_type): - private_name = '_' + name + private_name = f'_{name}' @property def value(self): diff --git a/Solutions/6_1/structure.py b/Solutions/6_1/structure.py index 7f8aa995..cbe5f6a2 100644 --- a/Solutions/6_1/structure.py +++ b/Solutions/6_1/structure.py @@ -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}') 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)})" diff --git a/Solutions/6_2/structure.py b/Solutions/6_2/structure.py index fb485252..65441944 100644 --- a/Solutions/6_2/structure.py +++ b/Solutions/6_2/structure.py @@ -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}') 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)})" diff --git a/Solutions/6_3/structure.py b/Solutions/6_3/structure.py index 55b08ce3..7d846991 100644 --- a/Solutions/6_3/structure.py +++ b/Solutions/6_3/structure.py @@ -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}') 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)})" @classmethod def set_fields(cls): diff --git a/Solutions/6_4/structure.py b/Solutions/6_4/structure.py index ef1f123c..7b049f5f 100644 --- a/Solutions/6_4/structure.py +++ b/Solutions/6_4/structure.py @@ -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}') 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)})" @classmethod def create_init(cls): diff --git a/Solutions/7_3/structure.py b/Solutions/7_3/structure.py index 15356fb6..0ed471fa 100644 --- a/Solutions/7_3/structure.py +++ b/Solutions/7_3/structure.py @@ -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}') 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)})" @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() - + return cls diff --git a/Solutions/7_4/structure.py b/Solutions/7_4/structure.py index 8886c122..777b6631 100644 --- a/Solutions/7_4/structure.py +++ b/Solutions/7_4/structure.py @@ -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}') 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)})" @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() - + return cls def typed_structure(clsname, **validators): - cls = type(clsname, (Structure,), validators) - return cls + return type(clsname, (Structure,), validators) diff --git a/Solutions/7_5/mymeta.py b/Solutions/7_5/mymeta.py index f8ba2636..e1f47ebd 100644 --- a/Solutions/7_5/mymeta.py +++ b/Solutions/7_5/mymeta.py @@ -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__) class myobject(metaclass=mytype): pass diff --git a/Solutions/7_6/structure.py b/Solutions/7_6/structure.py index 240bcd6a..fcd17846 100644 --- a/Solutions/7_6/structure.py +++ b/Solutions/7_6/structure.py @@ -5,13 +5,13 @@ class StructureMeta(type): @classmethod - def __prepare__(meta, clsname, bases): + def __prepare__(cls, clsname, bases): 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) 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}') 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)})" @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() - + return cls def typed_structure(clsname, **validators): - cls = type(clsname, (Structure,), validators) - return cls + return type(clsname, (Structure,), validators) diff --git a/Solutions/7_6/tableformat.py b/Solutions/7_6/tableformat.py index 42c6f133..b75f5251 100644 --- a/Solutions/7_6/tableformat.py +++ b/Solutions/7_6/tableformat.py @@ -38,13 +38,13 @@ class HTMLTableFormatter(TableFormatter): def headings(self, headers): print('', end=' ') for h in headers: - print('%s' % h, end=' ') + print(f'{h}', end=' ') print('') def row(self, rowdata): print('', end=' ') for d in rowdata: - print('%s' % d, end=' ') + print(f'{d}', end=' ') print('') class ColumnFormatMixin: @@ -65,7 +65,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}') if column_formats: class formatter_cls(ColumnFormatMixin, formatter_cls): diff --git a/Solutions/8_1/structure.py b/Solutions/8_1/structure.py index 9ccac5c1..fb0345c4 100644 --- a/Solutions/8_1/structure.py +++ b/Solutions/8_1/structure.py @@ -5,13 +5,13 @@ class StructureMeta(type): @classmethod - def __prepare__(meta, clsname, bases): + def __prepare__(cls, clsname, bases): 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) 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}') 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)})" def __iter__(self): for name in self._fields: @@ -73,20 +72,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() - + return cls def typed_structure(clsname, **validators): - cls = type(clsname, (Structure,), validators) - return cls + return type(clsname, (Structure,), validators) diff --git a/Solutions/8_2/structure.py b/Solutions/8_2/structure.py index ef1ac95b..d3583038 100644 --- a/Solutions/8_2/structure.py +++ b/Solutions/8_2/structure.py @@ -5,13 +5,13 @@ class StructureMeta(type): @classmethod - def __prepare__(meta, clsname, bases): + def __prepare__(cls, clsname, bases): 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) 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}') 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)})" def __iter__(self): for name in self._fields: @@ -72,20 +71,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() - + return cls def typed_structure(clsname, **validators): - cls = type(clsname, (Structure,), validators) - return cls + return type(clsname, (Structure,), validators) diff --git a/Solutions/8_2/tableformat.py b/Solutions/8_2/tableformat.py index 42c6f133..b75f5251 100644 --- a/Solutions/8_2/tableformat.py +++ b/Solutions/8_2/tableformat.py @@ -38,13 +38,13 @@ class HTMLTableFormatter(TableFormatter): def headings(self, headers): print('', end=' ') for h in headers: - print('%s' % h, end=' ') + print(f'{h}', end=' ') print('') def row(self, rowdata): print('', end=' ') for d in rowdata: - print('%s' % d, end=' ') + print(f'{d}', end=' ') print('') class ColumnFormatMixin: @@ -65,7 +65,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}') if column_formats: class formatter_cls(ColumnFormatMixin, formatter_cls): diff --git a/Solutions/8_3/structure.py b/Solutions/8_3/structure.py index ef1ac95b..d3583038 100644 --- a/Solutions/8_3/structure.py +++ b/Solutions/8_3/structure.py @@ -5,13 +5,13 @@ class StructureMeta(type): @classmethod - def __prepare__(meta, clsname, bases): + def __prepare__(cls, clsname, bases): 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) 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}') 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)})" def __iter__(self): for name in self._fields: @@ -72,20 +71,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() - + return cls def typed_structure(clsname, **validators): - cls = type(clsname, (Structure,), validators) - return cls + return type(clsname, (Structure,), validators) diff --git a/Solutions/8_3/tableformat.py b/Solutions/8_3/tableformat.py index 42c6f133..b75f5251 100644 --- a/Solutions/8_3/tableformat.py +++ b/Solutions/8_3/tableformat.py @@ -38,13 +38,13 @@ class HTMLTableFormatter(TableFormatter): def headings(self, headers): print('', end=' ') for h in headers: - print('%s' % h, end=' ') + print(f'{h}', end=' ') print('') def row(self, rowdata): print('', end=' ') for d in rowdata: - print('%s' % d, end=' ') + print(f'{d}', end=' ') print('') class ColumnFormatMixin: @@ -65,7 +65,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}') if column_formats: class formatter_cls(ColumnFormatMixin, formatter_cls): diff --git a/Solutions/8_6/cofollow.py b/Solutions/8_6/cofollow.py index a0026496..5ceae0e9 100644 --- a/Solutions/8_6/cofollow.py +++ b/Solutions/8_6/cofollow.py @@ -15,7 +15,7 @@ def follow(filename,target): def receive(expected_type): msg = yield - assert isinstance(msg, expected_type), 'Expected type %s' % (expected_type) + assert isinstance(msg, expected_type), f'Expected type {expected_type}' return msg # Decorator for coroutines diff --git a/Solutions/8_6/structure.py b/Solutions/8_6/structure.py index ef1ac95b..d3583038 100644 --- a/Solutions/8_6/structure.py +++ b/Solutions/8_6/structure.py @@ -5,13 +5,13 @@ class StructureMeta(type): @classmethod - def __prepare__(meta, clsname, bases): + def __prepare__(cls, clsname, bases): 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) 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}') 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)})" def __iter__(self): for name in self._fields: @@ -72,20 +71,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() - + return cls def typed_structure(clsname, **validators): - cls = type(clsname, (Structure,), validators) - return cls + return type(clsname, (Structure,), validators) diff --git a/Solutions/8_6/tableformat.py b/Solutions/8_6/tableformat.py index 42c6f133..b75f5251 100644 --- a/Solutions/8_6/tableformat.py +++ b/Solutions/8_6/tableformat.py @@ -38,13 +38,13 @@ class HTMLTableFormatter(TableFormatter): def headings(self, headers): print('', end=' ') for h in headers: - print('%s' % h, end=' ') + print(f'{h}', end=' ') print('') def row(self, rowdata): print('', end=' ') for d in rowdata: - print('%s' % d, end=' ') + print(f'{d}', end=' ') print('') class ColumnFormatMixin: @@ -65,7 +65,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}') if column_formats: class formatter_cls(ColumnFormatMixin, formatter_cls): diff --git a/Solutions/9_1/simplemod.py b/Solutions/9_1/simplemod.py index 2075a62e..2c879383 100644 --- a/Solutions/9_1/simplemod.py +++ b/Solutions/9_1/simplemod.py @@ -4,7 +4,7 @@ # A simple function def foo(): - print("x is %s" % x) + print(f"x is {x}") # A simple class class Spam: diff --git a/Solutions/9_2/structly/structure.py b/Solutions/9_2/structly/structure.py index 1f0d36e3..e6bdb075 100644 --- a/Solutions/9_2/structly/structure.py +++ b/Solutions/9_2/structly/structure.py @@ -5,13 +5,13 @@ class StructureMeta(type): @classmethod - def __prepare__(meta, clsname, bases): + def __prepare__(cls, clsname, bases): 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) 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}') 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)})" def __iter__(self): for name in self._fields: @@ -72,20 +71,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() - + return cls def typed_structure(clsname, **validators): - cls = type(clsname, (Structure,), validators) - return cls + return type(clsname, (Structure,), validators) diff --git a/Solutions/9_2/structly/tableformat.py b/Solutions/9_2/structly/tableformat.py index 62fc7ba8..404d0353 100644 --- a/Solutions/9_2/structly/tableformat.py +++ b/Solutions/9_2/structly/tableformat.py @@ -39,13 +39,13 @@ class HTMLTableFormatter(TableFormatter): def headings(self, headers): print('', end=' ') for h in headers: - print('%s' % h, end=' ') + print(f'{h}', end=' ') print('') def row(self, rowdata): print('', end=' ') for d in rowdata: - print('%s' % d, end=' ') + print(f'{d}', end=' ') print('') 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}') if column_formats: class formatter_cls(ColumnFormatMixin, formatter_cls): diff --git a/Solutions/9_3/structly/structure.py b/Solutions/9_3/structly/structure.py index 0deb747c..2dfba794 100644 --- a/Solutions/9_3/structly/structure.py +++ b/Solutions/9_3/structly/structure.py @@ -7,13 +7,13 @@ class StructureMeta(type): @classmethod - def __prepare__(meta, clsname, bases): + def __prepare__(cls, clsname, bases): 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) class Structure(metaclass=StructureMeta): _fields = () @@ -23,11 +23,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}') 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)})" def __iter__(self): for name in self._fields: @@ -74,20 +73,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() - + return cls def typed_structure(clsname, **validators): - cls = type(clsname, (Structure,), validators) - return cls + return type(clsname, (Structure,), validators) diff --git a/Solutions/9_3/structly/tableformat/formats/html.py b/Solutions/9_3/structly/tableformat/formats/html.py index eaeeeaff..efafa062 100644 --- a/Solutions/9_3/structly/tableformat/formats/html.py +++ b/Solutions/9_3/structly/tableformat/formats/html.py @@ -6,11 +6,11 @@ class HTMLTableFormatter(TableFormatter): def headings(self, headers): print('', end=' ') for h in headers: - print('%s' % h, end=' ') + print(f'{h}', end=' ') print('') def row(self, rowdata): print('', end=' ') for d in rowdata: - print('%s' % d, end=' ') + print(f'{d}', end=' ') print('') diff --git a/Solutions/9_3/structly/tableformat/formatter.py b/Solutions/9_3/structly/tableformat/formatter.py index 2ad245cf..a5686af8 100644 --- a/Solutions/9_3/structly/tableformat/formatter.py +++ b/Solutions/9_3/structly/tableformat/formatter.py @@ -41,7 +41,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}') if column_formats: class formatter_cls(ColumnFormatMixin, formatter_cls): diff --git a/Solutions/9_4/structly/structure.py b/Solutions/9_4/structly/structure.py index 0deb747c..2dfba794 100644 --- a/Solutions/9_4/structly/structure.py +++ b/Solutions/9_4/structly/structure.py @@ -7,13 +7,13 @@ class StructureMeta(type): @classmethod - def __prepare__(meta, clsname, bases): + def __prepare__(cls, clsname, bases): 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) class Structure(metaclass=StructureMeta): _fields = () @@ -23,11 +23,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}') 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)})" def __iter__(self): for name in self._fields: @@ -74,20 +73,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() - + return cls def typed_structure(clsname, **validators): - cls = type(clsname, (Structure,), validators) - return cls + return type(clsname, (Structure,), validators) diff --git a/Solutions/9_4/structly/tableformat/formats/html.py b/Solutions/9_4/structly/tableformat/formats/html.py index eaeeeaff..efafa062 100644 --- a/Solutions/9_4/structly/tableformat/formats/html.py +++ b/Solutions/9_4/structly/tableformat/formats/html.py @@ -6,11 +6,11 @@ class HTMLTableFormatter(TableFormatter): def headings(self, headers): print('', end=' ') for h in headers: - print('%s' % h, end=' ') + print(f'{h}', end=' ') print('') def row(self, rowdata): print('', end=' ') for d in rowdata: - print('%s' % d, end=' ') + print(f'{d}', end=' ') print('') diff --git a/Solutions/9_4/structly/tableformat/formatter.py b/Solutions/9_4/structly/tableformat/formatter.py index 893d7ba5..1da53290 100644 --- a/Solutions/9_4/structly/tableformat/formatter.py +++ b/Solutions/9_4/structly/tableformat/formatter.py @@ -39,10 +39,10 @@ def headings(self, headers): def create_formatter(name, column_formats=None, upper_headers=False): if name not in TableFormatter._formats: __import__(f'{__package__}.formats.{name}') - + formatter_cls = TableFormatter._formats.get(name) if not formatter_cls: - raise RuntimeError('Unknown format %s' % name) + raise RuntimeError(f'Unknown format {name}') if column_formats: class formatter_cls(ColumnFormatMixin, formatter_cls):