diff --git a/01-data-model/vector2d.py b/01-data-model/vector2d.py index a910ac8..557772b 100644 --- a/01-data-model/vector2d.py +++ b/01-data-model/vector2d.py @@ -26,7 +26,6 @@ Scalar multiplication:: >>> abs(v * 3) 15.0 - """ @@ -39,7 +38,7 @@ class Vector: self.y = y def __repr__(self): - return 'Vector(%r, %r)' % (self.x, self.y) + return f'Vector({self.x!r}, {self.y!r})' def __abs__(self): return hypot(self.x, self.y) diff --git a/02-array-seq/bisect_demo.py b/02-array-seq/bisect_demo.py index ec34b9c..d0ea6c2 100644 --- a/02-array-seq/bisect_demo.py +++ b/02-array-seq/bisect_demo.py @@ -11,11 +11,11 @@ Demonstration of ``bisect.bisect``:: 23 @ 11 | | | | | | | | | | |23 22 @ 9 | | | | | | | | |22 10 @ 5 | | | | |10 - 8 @ 5 | | | | |8 - 5 @ 3 | | |5 - 2 @ 1 |2 - 1 @ 1 |1 - 0 @ 0 0 + 8 @ 5 | | | | |8 + 5 @ 3 | | |5 + 2 @ 1 |2 + 1 @ 1 |1 + 0 @ 0 0 Demonstration of ``bisect.bisect_left``:: @@ -27,11 +27,11 @@ Demonstration of ``bisect.bisect_left``:: 23 @ 9 | | | | | | | | |23 22 @ 9 | | | | | | | | |22 10 @ 5 | | | | |10 - 8 @ 4 | | | |8 - 5 @ 2 | |5 - 2 @ 1 |2 - 1 @ 0 1 - 0 @ 0 0 + 8 @ 4 | | | |8 + 5 @ 2 | |5 + 2 @ 1 |2 + 1 @ 0 1 + 0 @ 0 0 """ @@ -59,7 +59,7 @@ if __name__ == '__main__': bisect_fn = bisect.bisect print('DEMO:', bisect_fn.__name__) # <5> - print('haystack ->', ' '.join('%2d' % n for n in HAYSTACK)) + print('haystack ->', ' '.join(f'{n:2}' for n in HAYSTACK)) demo(bisect_fn) # END BISECT_DEMO diff --git a/02-array-seq/bisect_insort.py b/02-array-seq/bisect_insort.py index b49aeae..12277c7 100644 --- a/02-array-seq/bisect_insort.py +++ b/02-array-seq/bisect_insort.py @@ -7,6 +7,6 @@ random.seed(1729) my_list = [] for i in range(SIZE): - new_item = random.randrange(SIZE*2) + new_item = random.randrange(SIZE * 2) bisect.insort(my_list, new_item) - print('%2d ->' % new_item, my_list) + print(f'{new_item:2} ->', my_list) diff --git a/02-array-seq/listcomp_speed.py b/02-array-seq/listcomp_speed.py index 0ec8b58..8e3f5d8 100644 --- a/02-array-seq/listcomp_speed.py +++ b/02-array-seq/listcomp_speed.py @@ -10,7 +10,7 @@ def non_ascii(c): def clock(label, cmd): res = timeit.repeat(cmd, setup=SETUP, number=TIMES) - print(label, *('{:.3f}'.format(x) for x in res)) + print(label, *(f'{x:.3f}' for x in res)) clock('listcomp :', '[ord(s) for s in symbols if ord(s) > 127]') clock('listcomp + func :', '[ord(s) for s in symbols if non_ascii(ord(s))]') diff --git a/02-array-seq/metro_lat_long.py b/02-array-seq/metro_lat_long.py index 37ec551..25b2373 100644 --- a/02-array-seq/metro_lat_long.py +++ b/02-array-seq/metro_lat_long.py @@ -4,7 +4,7 @@ metro_lat_long.py Demonstration of nested tuple unpacking:: >>> main() - | lat. | long. + | lat. | long. Mexico City | 19.4333 | -99.1333 New York-Newark | 40.8086 | -74.0204 Sao Paulo | -23.5478 | -46.6358 @@ -20,11 +20,10 @@ metro_areas = [ ] def main(): - print('{:15} | {:^9} | {:^9}'.format('', 'lat.', 'long.')) - fmt = '{:15} | {:9.4f} | {:9.4f}' + print(f'{"":15} | {"lat.":^9} | {"long.":^9}') for name, cc, pop, (latitude, longitude) in metro_areas: # <2> if longitude <= 0: # <3> - print(fmt.format(name, latitude, longitude)) + print(f'{name:15} | {latitude:9.4f} | {longitude:9.4f}') if __name__ == '__main__': main() diff --git a/03-dict-set/index_default.py b/03-dict-set/index_default.py index 60279a9..839ffd7 100644 --- a/03-dict-set/index_default.py +++ b/03-dict-set/index_default.py @@ -16,7 +16,7 @@ with open(sys.argv[1], encoding='utf-8') as fp: for line_no, line in enumerate(fp, 1): for match in WORD_RE.finditer(line): word = match.group() - column_no = match.start()+1 + column_no = match.start() + 1 location = (line_no, column_no) index[word].append(location) # <2> diff --git a/03-dict-set/support/container_perftest.py b/03-dict-set/support/container_perftest.py index f044e96..ea1453e 100644 --- a/03-dict-set/support/container_perftest.py +++ b/03-dict-set/support/container_perftest.py @@ -41,15 +41,15 @@ def test(container_type, verbose): size=size, verbose=verbose) test = TEST.format(verbose=verbose) tt = timeit.repeat(stmt=test, setup=setup, repeat=5, number=1) - print('|{:{}d}|{:f}'.format(size, MAX_EXPONENT + 1, min(tt))) + print(f'|{size:{MAX_EXPONENT + 1}d}|{min(tt):f}') -if __name__=='__main__': +if __name__ == '__main__': if '-v' in sys.argv: sys.argv.remove('-v') verbose = True else: verbose = False if len(sys.argv) != 2: - print('Usage: %s ' % sys.argv[0]) + print(f'Usage: {sys.argv[0]} ') else: test(sys.argv[1], verbose) diff --git a/03-dict-set/support/container_perftest_datagen.py b/03-dict-set/support/container_perftest_datagen.py index 0f01ddd..df1be7e 100644 --- a/03-dict-set/support/container_perftest_datagen.py +++ b/03-dict-set/support/container_perftest_datagen.py @@ -2,8 +2,8 @@ Generate data for container performance test """ -import random import array +import random MAX_EXPONENT = 7 HAYSTACK_LEN = 10 ** MAX_EXPONENT @@ -12,26 +12,26 @@ SAMPLE_LEN = HAYSTACK_LEN + NEEDLES_LEN // 2 needles = array.array('d') -sample = {1/random.random() for i in range(SAMPLE_LEN)} -print('initial sample: %d elements' % len(sample)) +sample = {1 / random.random() for i in range(SAMPLE_LEN)} +print(f'initial sample: {len(sample)} elements') # complete sample, in case duplicate random numbers were discarded while len(sample) < SAMPLE_LEN: - sample.add(1/random.random()) + sample.add(1 / random.random()) -print('complete sample: %d elements' % len(sample)) +print(f'complete sample: {len(sample)} elements') sample = array.array('d', sample) random.shuffle(sample) not_selected = sample[:NEEDLES_LEN // 2] -print('not selected: %d samples' % len(not_selected)) +print(f'not selected: {len(not_selected)} samples') print(' writing not_selected.arr') with open('not_selected.arr', 'wb') as fp: not_selected.tofile(fp) selected = sample[NEEDLES_LEN // 2:] -print('selected: %d samples' % len(selected)) +print(f'selected: {len(selected)} samples') print(' writing selected.arr') with open('selected.arr', 'wb') as fp: selected.tofile(fp) diff --git a/03-dict-set/support/hashdiff.py b/03-dict-set/support/hashdiff.py index 163e300..97fe72d 100644 --- a/03-dict-set/support/hashdiff.py +++ b/03-dict-set/support/hashdiff.py @@ -1,17 +1,17 @@ import sys MAX_BITS = len(format(sys.maxsize, 'b')) -print('%s-bit Python build' % (MAX_BITS + 1)) +print(f'{MAX_BITS + 1}-bit Python build') def hash_diff(o1, o2): - h1 = '{:>0{}b}'.format(hash(o1), MAX_BITS) - h2 = '{:>0{}b}'.format(hash(o2), MAX_BITS) + h1 = f'{hash(o1):>0{MAX_BITS}b}' + h2 = f'{hash(o2):>0{MAX_BITS}b}' diff = ''.join('!' if b1 != b2 else ' ' for b1, b2 in zip(h1, h2)) - count = '!= {}'.format(diff.count('!')) + count = f'!= {diff.count("!")}' width = max(len(repr(o1)), len(repr(o2)), 8) sep = '-' * (width * 2 + MAX_BITS) - return '{!r:{width}} {}\n{:{width}} {} {}\n{!r:{width}} {}\n{}'.format( - o1, h1, ' ' * width, diff, count, o2, h2, sep, width=width) + return (f'{o1!r:{width}} {h1}\n{" ":{width}} {diff} {count}\n' + f'{o2!r:{width}} {h2}\n{sep}') if __name__ == '__main__': print(hash_diff(1, 1.0)) diff --git a/03-dict-set/transformdict.py b/03-dict-set/transformdict.py index 4c6a7e7..b0cc002 100644 --- a/03-dict-set/transformdict.py +++ b/03-dict-set/transformdict.py @@ -17,7 +17,7 @@ _sentinel = object() class TransformDict(MutableMapping): - '''Dictionary that calls a transformation function when looking + """Dictionary that calls a transformation function when looking up keys, but preserves the original keys. >>> d = TransformDict(str.lower) @@ -26,18 +26,18 @@ class TransformDict(MutableMapping): True >>> set(d.keys()) {'Foo'} - ''' + """ __slots__ = ('_transform', '_original', '_data') def __init__(self, transform, init_dict=None, **kwargs): - '''Create a new TransformDict with the given *transform* function. + """Create a new TransformDict with the given *transform* function. *init_dict* and *kwargs* are optional initializers, as in the dict constructor. - ''' + """ if not callable(transform): - msg = 'expected a callable, got %r' - raise TypeError(msg % transform.__class__) + raise TypeError( + f'expected a callable, got {transform.__class__!r}') self._transform = transform # transformed => original self._original = {} @@ -48,7 +48,7 @@ class TransformDict(MutableMapping): self.update(kwargs) def getitem(self, key): - 'D.getitem(key) -> (stored key, value)' + """D.getitem(key) -> (stored key, value)""" transformed = self._transform(key) original = self._original[transformed] value = self._data[transformed] @@ -56,7 +56,7 @@ class TransformDict(MutableMapping): @property def transform_func(self): - "This TransformDict's transformation function" + """This TransformDict's transformation function""" return self._transform # Minimum set of methods required for MutableMapping @@ -83,7 +83,7 @@ class TransformDict(MutableMapping): # Methods overridden to mitigate the performance overhead. def clear(self): - 'D.clear() -> None. Remove all items from D.' + """D.clear() -> None. Remove all items from D.""" self._data.clear() self._original.clear() @@ -91,14 +91,14 @@ class TransformDict(MutableMapping): return self._transform(key) in self._data def get(self, key, default=None): - 'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.' + """D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.""" return self._data.get(self._transform(key), default) def pop(self, key, default=_sentinel): - '''D.pop(k[,d]) -> v, remove key and return corresponding value. + """D.pop(k[,d]) -> v, remove key and return corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised. - ''' + """ transformed = self._transform(key) if default is _sentinel: del self._original[transformed] @@ -108,16 +108,16 @@ class TransformDict(MutableMapping): return self._data.pop(transformed, default) def popitem(self): - '''D.popitem() -> (k, v), remove and return some (key, value) pair + """D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty. - ''' + """ transformed, value = self._data.popitem() return self._original.pop(transformed), value # Other methods def copy(self): - 'D.copy() -> a shallow copy of D' + """D.copy() -> a shallow copy of D""" other = self.__class__(self._transform) other._original = self._original.copy() other._data = self._data.copy() @@ -137,5 +137,4 @@ class TransformDict(MutableMapping): except TypeError: # Some keys are unhashable, fall back on .items() equiv = list(self.items()) - return '%s(%r, %s)' % (self.__class__.__name__, - self._transform, repr(equiv)) + return f'{self.__class__.__name__}({self._transform!r}, {equiv!r})' diff --git a/04-text-byte/charfinder/cf.py b/04-text-byte/charfinder/cf.py index 5717521..8976a66 100755 --- a/04-text-byte/charfinder/cf.py +++ b/04-text-byte/charfinder/cf.py @@ -8,7 +8,7 @@ FIRST, LAST = ord(' '), sys.maxunicode # <1> def find(*query_words, first=FIRST, last=LAST): # <2> query = {w.upper() for w in query_words} # <3> count = 0 - for code in range(first, last + 1): + for code in range(first, last + 1): char = chr(code) # <4> name = unicodedata.name(char, None) # <5> if name and query.issubset(name.split()): # <6> diff --git a/04-text-byte/default_encodings.py b/04-text-byte/default_encodings.py index 2daf95b..c230dea 100644 --- a/04-text-byte/default_encodings.py +++ b/04-text-byte/default_encodings.py @@ -18,4 +18,4 @@ my_file = open('dummy', 'w') for expression in expressions.split(): value = eval(expression) - print(expression.rjust(30), '->', repr(value)) + print(f'{expression:>30} -> {value!r}') diff --git a/04-text-byte/numerics_demo.py b/04-text-byte/numerics_demo.py index ce43f1b..12f27c2 100644 --- a/04-text-byte/numerics_demo.py +++ b/04-text-byte/numerics_demo.py @@ -7,12 +7,12 @@ re_digit = re.compile(r'\d') sample = '1\xbc\xb2\u0969\u136b\u216b\u2466\u2480\u3285' for char in sample: - print('U+%04x' % ord(char), # <1> + print(f'U+{ord(char):04x}', # <1> char.center(6), # <2> 're_dig' if re_digit.match(char) else '-', # <3> 'isdig' if char.isdigit() else '-', # <4> 'isnum' if char.isnumeric() else '-', # <5> - format(unicodedata.numeric(char), '5.2f'), # <6> + f'{unicodedata.numeric(char):5.2f}', # <6> unicodedata.name(char), # <7> sep='\t') # end::NUMERICS_DEMO[] diff --git a/04-text-byte/ramanujan.py b/04-text-byte/ramanujan.py index cafe072..3906429 100644 --- a/04-text-byte/ramanujan.py +++ b/04-text-byte/ramanujan.py @@ -11,7 +11,7 @@ text_str = ("Ramanujan saw \u0be7\u0bed\u0be8\u0bef" # <3> text_bytes = text_str.encode('utf_8') # <5> -print('Text', repr(text_str), sep='\n ') +print(f'Text\n {text_str!r}') print('Numbers') print(' str :', re_numbers_str.findall(text_str)) # <6> print(' bytes:', re_numbers_bytes.findall(text_bytes)) # <7> diff --git a/04-text-byte/sanitize.py b/04-text-byte/sanitize.py index a86cb55..15eb09a 100644 --- a/04-text-byte/sanitize.py +++ b/04-text-byte/sanitize.py @@ -1,4 +1,3 @@ - """ Radical folding and text sanitizing. diff --git a/05-record-like/dataclass/coordinates.py b/05-record-like/dataclass/coordinates.py index 55fedbd..7bcd7cd 100644 --- a/05-record-like/dataclass/coordinates.py +++ b/05-record-like/dataclass/coordinates.py @@ -16,7 +16,7 @@ class Coordinate: lat: float long: float - + def __str__(self): ns = 'N' if self.lat >= 0 else 'S' we = 'E' if self.long >= 0 else 'W' diff --git a/05-record-like/dataclass/hackerclub.py b/05-record-like/dataclass/hackerclub.py index 8ab2a05..4d9112e 100644 --- a/05-record-like/dataclass/hackerclub.py +++ b/05-record-like/dataclass/hackerclub.py @@ -30,7 +30,7 @@ To fix, ``leo2`` must be created with an explicit ``handle``:: # tag::HACKERCLUB[] from dataclasses import dataclass -from club import ClubMember +from club import ClubMember @dataclass class HackerClubMember(ClubMember): # <1> diff --git a/05-record-like/dataclass/hackerclub_annotated.py b/05-record-like/dataclass/hackerclub_annotated.py index a25a443..a760a55 100644 --- a/05-record-like/dataclass/hackerclub_annotated.py +++ b/05-record-like/dataclass/hackerclub_annotated.py @@ -31,7 +31,7 @@ To fix, ``leo2`` must be created with an explicit ``handle``:: # tag::HACKERCLUB[] from dataclasses import dataclass from typing import ClassVar, Set -from club import ClubMember +from club import ClubMember @dataclass class HackerClubMember(ClubMember): diff --git a/05-record-like/dataclass/resource.py b/05-record-like/dataclass/resource.py index b768f09..90bfaf0 100644 --- a/05-record-like/dataclass/resource.py +++ b/05-record-like/dataclass/resource.py @@ -14,7 +14,7 @@ A complete resource record: >>> description = 'Improving the design of existing code' >>> book = Resource('978-0-13-475759-9', 'Refactoring, 2nd Edition', ... ['Martin Fowler', 'Kent Beck'], date(2018, 11, 19), - ... ResourceType.BOOK, description, 'EN', + ... ResourceType.BOOK, description, 'EN', ... ['computer programming', 'OOP']) >>> book # doctest: +NORMALIZE_WHITESPACE Resource(identifier='978-0-13-475759-9', title='Refactoring, 2nd Edition', diff --git a/05-record-like/dataclass/resource_repr.py b/05-record-like/dataclass/resource_repr.py index ee0a731..e32c88d 100644 --- a/05-record-like/dataclass/resource_repr.py +++ b/05-record-like/dataclass/resource_repr.py @@ -21,7 +21,7 @@ A complete resource record: >>> description = 'Improving the design of existing code' >>> book = Resource('978-0-13-475759-9', 'Refactoring, 2nd Edition', ... ['Martin Fowler', 'Kent Beck'], date(2018, 11, 19), - ... ResourceType.BOOK, description, 'EN', + ... ResourceType.BOOK, description, 'EN', ... ['computer programming', 'OOP']) # tag::DOCTEST[] diff --git a/05-record-like/typing_namedtuple/coordinates.py b/05-record-like/typing_namedtuple/coordinates.py index a3d7401..382d804 100644 --- a/05-record-like/typing_namedtuple/coordinates.py +++ b/05-record-like/typing_namedtuple/coordinates.py @@ -14,7 +14,7 @@ class Coordinate(NamedTuple): lat: float long: float - + def __str__(self): ns = 'N' if self.lat >= 0 else 'S' we = 'E' if self.long >= 0 else 'W' diff --git a/05-record-like/typing_namedtuple/nocheck_demo.py b/05-record-like/typing_namedtuple/nocheck_demo.py index a8b5ed4..e1193ec 100644 --- a/05-record-like/typing_namedtuple/nocheck_demo.py +++ b/05-record-like/typing_namedtuple/nocheck_demo.py @@ -6,4 +6,4 @@ class Coordinate(typing.NamedTuple): long: float trash = Coordinate('foo', None) # <1> -print(trash) +print(trash) diff --git a/06-obj-ref/bus.py b/06-obj-ref/bus.py index 778afa2..b4b4038 100644 --- a/06-obj-ref/bus.py +++ b/06-obj-ref/bus.py @@ -1,4 +1,3 @@ - """ >>> import copy >>> bus1 = Bus(['Alice', 'Bill', 'Claire', 'David']) diff --git a/06-obj-ref/cheese.py b/06-obj-ref/cheese.py index 924e5d0..d58085d 100644 --- a/06-obj-ref/cheese.py +++ b/06-obj-ref/cheese.py @@ -2,7 +2,7 @@ >>> import weakref >>> stock = weakref.WeakValueDictionary() >>> catalog = [Cheese('Red Leicester'), Cheese('Tilsit'), -... Cheese('Brie'), Cheese('Parmesan')] +... Cheese('Brie'), Cheese('Parmesan')] ... >>> for cheese in catalog: ... stock[cheese.kind] = cheese @@ -24,5 +24,5 @@ class Cheese: self.kind = kind def __repr__(self): - return 'Cheese(%r)' % self.kind + return f'Cheese({self.kind!r})' # end::CHEESE_CLASS[] diff --git a/07-1class-func/tagger.py b/07-1class-func/tagger.py index 52c3ddc..f132bef 100644 --- a/07-1class-func/tagger.py +++ b/07-1class-func/tagger.py @@ -1,5 +1,3 @@ - - """ # tag::TAG_DEMO[] >>> tag('br') # <1> diff --git a/08-def-type-hints/RPN_calc/calc.py b/08-def-type-hints/RPN_calc/calc.py index 5827eb9..967b094 100755 --- a/08-def-type-hints/RPN_calc/calc.py +++ b/08-def-type-hints/RPN_calc/calc.py @@ -2,7 +2,7 @@ import sys from array import array -from typing import Mapping, MutableSequence, Callable, Iterable, Sequence, Union, Any +from typing import Mapping, MutableSequence, Callable, Iterable, Union, Any OPERATORS: Mapping[str, Callable[[float, float], float]] = { diff --git a/08-def-type-hints/bus.py b/08-def-type-hints/bus.py index 778afa2..b4b4038 100644 --- a/08-def-type-hints/bus.py +++ b/08-def-type-hints/bus.py @@ -1,4 +1,3 @@ - """ >>> import copy >>> bus1 = Bus(['Alice', 'Bill', 'Claire', 'David']) diff --git a/08-def-type-hints/charindex.py b/08-def-type-hints/charindex.py index 790c464..36d20e3 100644 --- a/08-def-type-hints/charindex.py +++ b/08-def-type-hints/charindex.py @@ -17,7 +17,7 @@ import re import unicodedata from typing import Dict, Set, Iterator -RE_WORD = re.compile('\w+') +RE_WORD = re.compile(r'\w+') STOP_CODE = sys.maxunicode + 1 def tokenize(text: str) -> Iterator[str]: # <1> diff --git a/08-def-type-hints/clip_annot_1ed.py b/08-def-type-hints/clip_annot_1ed.py index ec4b392..4c49239 100644 --- a/08-def-type-hints/clip_annot_1ed.py +++ b/08-def-type-hints/clip_annot_1ed.py @@ -19,7 +19,7 @@ # tag::CLIP_ANNOT[] -def clip(text:str, max_len:'int > 0'=80) -> str: # <1> +def clip(text: str, max_len: 'int > 0'=80) -> str: # <1> """Return text clipped at the last space before or after max_len """ end = None diff --git a/08-def-type-hints/colors.py b/08-def-type-hints/colors.py index 270cd51..36518d6 100644 --- a/08-def-type-hints/colors.py +++ b/08-def-type-hints/colors.py @@ -21,7 +21,7 @@ NAMES = { def rgb2hex(color=Tuple[int, int, int]) -> str: if any(c not in range(256) for c in color): - raise ValueError('Color components must be in range(256)') + raise ValueError('Color components must be in range(256)') values = (f'{n % 256:02x}' for n in color) return '#' + ''.join(values) diff --git a/08-def-type-hints/columnize2.py b/08-def-type-hints/columnize2.py index 68929b5..67b5fdd 100644 --- a/08-def-type-hints/columnize2.py +++ b/08-def-type-hints/columnize2.py @@ -31,7 +31,7 @@ def demo() -> None: print(f'{cell:5}', end='') print() print() - + if __name__ == '__main__': demo() diff --git a/08-def-type-hints/columnize_test.py b/08-def-type-hints/columnize_test.py index 8a0b0f0..5d9446f 100644 --- a/08-def-type-hints/columnize_test.py +++ b/08-def-type-hints/columnize_test.py @@ -48,7 +48,7 @@ def test_columnize_8_in_3(): def test_columnize_8_in_5(): # Not the right number of columns, but the right number of rows. - # This acually looks better, so it's OK! + # This actually looks better, so it's OK! sequence = 'ABCDEFGH' expected = [ ('A', 'C', 'E', 'G'), @@ -60,7 +60,7 @@ def test_columnize_8_in_5(): def test_columnize_7_in_5(): # Not the right number of columns, but the right number of rows. - # This acually looks better, so it's OK! + # This actually looks better, so it's OK! sequence = 'ABCDEFG' expected = [ ('A', 'C', 'E', 'G'), diff --git a/08-def-type-hints/comparable/mymax.py b/08-def-type-hints/comparable/mymax.py index b69a296..26dfec2 100644 --- a/08-def-type-hints/comparable/mymax.py +++ b/08-def-type-hints/comparable/mymax.py @@ -24,7 +24,7 @@ def max(__iterable: Iterable[_CT], *, key: None = ...) -> _CT: def max(__iterable: Iterable[_T], *, key: Callable[[_T], _CT]) -> _T: ... @overload -def max(__iterable: Iterable[_CT], *, key: None = ..., +def max(__iterable: Iterable[_CT], *, key: None = ..., default: _DT) -> Union[_CT, _DT]: ... @overload diff --git a/08-def-type-hints/comparable/top.py b/08-def-type-hints/comparable/top.py index 51b7751..c751635 100644 --- a/08-def-type-hints/comparable/top.py +++ b/08-def-type-hints/comparable/top.py @@ -8,7 +8,7 @@ Example: >>> l = 'mango pear apple kiwi banana'.split() >>> top(l, 3) ['pear', 'mango', 'kiwi'] ->>> +>>> >>> l2 = [(len(s), s) for s in l] >>> l2 [(5, 'mango'), (4, 'pear'), (5, 'apple'), (4, 'kiwi'), (6, 'banana')] diff --git a/08-def-type-hints/coordinates/coordinates.py b/08-def-type-hints/coordinates/coordinates.py index 6986f8a..ca64d79 100644 --- a/08-def-type-hints/coordinates/coordinates.py +++ b/08-def-type-hints/coordinates/coordinates.py @@ -14,6 +14,6 @@ from geolib import geohash as gh # type: ignore PRECISION = 9 -def geohash(lat_lon = Tuple[float, float]) -> str: +def geohash(lat_lon: Tuple[float, float]) -> str: return gh.encode(*lat_lon, PRECISION) # end::GEOHASH[] \ No newline at end of file diff --git a/08-def-type-hints/mode/mode_T.py b/08-def-type-hints/mode/mode_T.py index d646f5c..46953ee 100644 --- a/08-def-type-hints/mode/mode_T.py +++ b/08-def-type-hints/mode/mode_T.py @@ -13,7 +13,7 @@ def mode(data: Iterable[T]) -> T: def demo() -> None: from typing import List, Set, TYPE_CHECKING - pop:List[Set] = [set(), set()] + pop: List[Set] = [set(), set()] m = mode(pop) if TYPE_CHECKING: reveal_type(pop) diff --git a/08-def-type-hints/mode/mode_number.py b/08-def-type-hints/mode/mode_number.py index 3eed434..999387f 100644 --- a/08-def-type-hints/mode/mode_number.py +++ b/08-def-type-hints/mode/mode_number.py @@ -13,7 +13,7 @@ def mode(data: Iterable[NumberT]) -> NumberT: def demo() -> None: - from typing import List, Set, TYPE_CHECKING + from typing import TYPE_CHECKING pop = [Fraction(1, 2), Fraction(1, 3), Fraction(1, 4), Fraction(1, 2)] m = mode(pop) if TYPE_CHECKING: diff --git a/08-def-type-hints/passdrill.py b/08-def-type-hints/passdrill.py index 7523f5e..612e504 100755 --- a/08-def-type-hints/passdrill.py +++ b/08-def-type-hints/passdrill.py @@ -57,7 +57,7 @@ def load_hash() -> Tuple[bytes, bytes]: sys.exit(2) salt, stored_hash = salted_hash.split(b':') - return (b64decode(salt), b64decode(stored_hash)) + return b64decode(salt), b64decode(stored_hash) def practice() -> None: @@ -83,8 +83,7 @@ def practice() -> None: print(f' {answer}\thits={correct}\tmisses={turn-correct}') if turn: - pct = correct / turn * 100 - print(f'\n{turn} turns. {pct:0.1f}% correct.') + print(f'\n{turn} turns. {correct / turn:.1%} correct.') def main(argv: Sequence[str]) -> None: diff --git a/08-def-type-hints/replacer.py b/08-def-type-hints/replacer.py index ea24a88..73ec77d 100644 --- a/08-def-type-hints/replacer.py +++ b/08-def-type-hints/replacer.py @@ -30,7 +30,7 @@ def demo() -> None: l33t = [(p[0], p[1]) for p in 'a4 e3 i1 o0'.split()] text = 'mad skilled noob powned leet' print(zip_replace(text, l33t)) - + if __name__ == '__main__': demo() diff --git a/08-def-type-hints/replacer2.py b/08-def-type-hints/replacer2.py index 9bf3986..786c8d7 100644 --- a/08-def-type-hints/replacer2.py +++ b/08-def-type-hints/replacer2.py @@ -33,7 +33,7 @@ def demo() -> None: l33t = [FromTo(*p) for p in 'a4 e3 i1 o0'.split()] text = 'mad skilled noob powned leet' print(zip_replace(text, l33t)) - + if __name__ == '__main__': demo() diff --git a/08-def-type-hints/typeddict/books_any.py b/08-def-type-hints/typeddict/books_any.py index 49d500f..e696ff3 100644 --- a/08-def-type-hints/typeddict/books_any.py +++ b/08-def-type-hints/typeddict/books_any.py @@ -17,7 +17,7 @@ def to_xml(book: BookDict) -> str: # <1> for key, value in book.items(): if isinstance(value, list): # <3> elements.extend(AUTHOR_EL.format(n) - for n in value) + for n in value) else: tag = key.upper() elements.append(f'<{tag}>{value}') diff --git a/08-def-type-hints/typeddict/test_books_check_fails.py b/08-def-type-hints/typeddict/test_books_check_fails.py index 45cbe83..6166f97 100644 --- a/08-def-type-hints/typeddict/test_books_check_fails.py +++ b/08-def-type-hints/typeddict/test_books_check_fails.py @@ -1,6 +1,3 @@ -import json -from typing import cast - from books import BookDict, to_xml XML_SAMPLE = """ diff --git a/09-closure-deco/average.py b/09-closure-deco/average.py index 5ac9855..e317f40 100644 --- a/09-closure-deco/average.py +++ b/09-closure-deco/average.py @@ -28,6 +28,6 @@ def make_averager(): def averager(new_value): series.append(new_value) total = sum(series) - return total/len(series) + return total / len(series) return averager diff --git a/09-closure-deco/average_oo.py b/09-closure-deco/average_oo.py index 154f055..bb05380 100644 --- a/09-closure-deco/average_oo.py +++ b/09-closure-deco/average_oo.py @@ -10,7 +10,7 @@ """ -class Averager(): +class Averager: def __init__(self): self.series = [] diff --git a/09-closure-deco/fibo_demo.py b/09-closure-deco/fibo_demo.py index 8b76796..045b24d 100644 --- a/09-closure-deco/fibo_demo.py +++ b/09-closure-deco/fibo_demo.py @@ -5,7 +5,7 @@ from clockdeco import clock def fibonacci(n): if n < 2: return n - return fibonacci(n-2) + fibonacci(n-1) + return fibonacci(n - 2) + fibonacci(n - 1) if __name__ == '__main__': diff --git a/09-closure-deco/fibo_demo_lru.py b/09-closure-deco/fibo_demo_lru.py index 1a0cf11..90936f9 100644 --- a/09-closure-deco/fibo_demo_lru.py +++ b/09-closure-deco/fibo_demo_lru.py @@ -8,7 +8,7 @@ from clockdeco import clock def fibonacci(n): if n < 2: return n - return fibonacci(n-2) + fibonacci(n-1) + return fibonacci(n - 2) + fibonacci(n - 1) if __name__ == '__main__': diff --git a/10-dp-1class-func/classic_strategy.py b/10-dp-1class-func/classic_strategy.py index c2ffe34..dc72e19 100644 --- a/10-dp-1class-func/classic_strategy.py +++ b/10-dp-1class-func/classic_strategy.py @@ -8,7 +8,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), # <2> ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, FidelityPromo()) # <3> >>> Order(ann, cart, FidelityPromo()) # <4> @@ -72,8 +72,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' class Promotion(ABC): # the Strategy: an abstract base class diff --git a/10-dp-1class-func/classic_strategy_test.py b/10-dp-1class-func/classic_strategy_test.py index 94999ec..8735811 100644 --- a/10-dp-1class-func/classic_strategy_test.py +++ b/10-dp-1class-func/classic_strategy_test.py @@ -21,7 +21,7 @@ def cart_plain() -> List[LineItem]: return [ LineItem('banana', 4, 0.5), LineItem('apple', 10, 1.5), - LineItem('watermellon', 5, 5.0), + LineItem('watermelon', 5, 5.0), ] diff --git a/10-dp-1class-func/monkeytype/classic_strategy.py b/10-dp-1class-func/monkeytype/classic_strategy.py index 06bfedd..0057d96 100644 --- a/10-dp-1class-func/monkeytype/classic_strategy.py +++ b/10-dp-1class-func/monkeytype/classic_strategy.py @@ -8,7 +8,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), # <2> ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, FidelityPromo()) # <3> >>> Order(ann, cart, FidelityPromo()) # <4> @@ -29,7 +29,6 @@ # tag::CLASSIC_STRATEGY[] from abc import ABC, abstractmethod -from collections import namedtuple import typing @@ -69,8 +68,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' class Promotion(ABC): # the Strategy: an abstract base class diff --git a/10-dp-1class-func/monkeytype/classic_strategy_test.py b/10-dp-1class-func/monkeytype/classic_strategy_test.py index ede42e3..b730962 100644 --- a/10-dp-1class-func/monkeytype/classic_strategy_test.py +++ b/10-dp-1class-func/monkeytype/classic_strategy_test.py @@ -20,7 +20,7 @@ def customer_fidelity_1100() -> Customer: def cart_plain() -> List[LineItem]: return [LineItem('banana', 4, .5), LineItem('apple', 10, 1.5), - LineItem('watermellon', 5, 5.0)] + LineItem('watermelon', 5, 5.0)] def test_fidelity_promo_no_discount(customer_fidelity_0, cart_plain) -> None: diff --git a/10-dp-1class-func/pytypes/classic_strategy.py b/10-dp-1class-func/pytypes/classic_strategy.py index 68b19c0..4cc833f 100644 --- a/10-dp-1class-func/pytypes/classic_strategy.py +++ b/10-dp-1class-func/pytypes/classic_strategy.py @@ -8,7 +8,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), # <2> ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, FidelityPromo()) # <3> >>> Order(ann, cart, FidelityPromo()) # <4> @@ -29,7 +29,6 @@ # tag::CLASSIC_STRATEGY[] from abc import ABC, abstractmethod -from collections import namedtuple import typing from pytypes import typelogged @@ -71,8 +70,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' @typelogged diff --git a/10-dp-1class-func/pytypes/classic_strategy_test.py b/10-dp-1class-func/pytypes/classic_strategy_test.py index ede42e3..b730962 100644 --- a/10-dp-1class-func/pytypes/classic_strategy_test.py +++ b/10-dp-1class-func/pytypes/classic_strategy_test.py @@ -20,7 +20,7 @@ def customer_fidelity_1100() -> Customer: def cart_plain() -> List[LineItem]: return [LineItem('banana', 4, .5), LineItem('apple', 10, 1.5), - LineItem('watermellon', 5, 5.0)] + LineItem('watermelon', 5, 5.0)] def test_fidelity_promo_no_discount(customer_fidelity_0, cart_plain) -> None: diff --git a/10-dp-1class-func/strategy.py b/10-dp-1class-func/strategy.py index 6e464c7..2ab49d1 100644 --- a/10-dp-1class-func/strategy.py +++ b/10-dp-1class-func/strategy.py @@ -8,7 +8,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, fidelity_promo) # <2> >>> Order(ann, cart, fidelity_promo) @@ -71,8 +71,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' # <2> diff --git a/10-dp-1class-func/strategy_best.py b/10-dp-1class-func/strategy_best.py index 4054175..a7c4d74 100644 --- a/10-dp-1class-func/strategy_best.py +++ b/10-dp-1class-func/strategy_best.py @@ -7,7 +7,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> banana_cart = [LineItem('banana', 30, .5), ... LineItem('apple', 10, 1.5)] >>> big_cart = [LineItem(str(item_code), 1, 1.0) diff --git a/10-dp-1class-func/strategy_best2.py b/10-dp-1class-func/strategy_best2.py index 65b3556..62a993e 100644 --- a/10-dp-1class-func/strategy_best2.py +++ b/10-dp-1class-func/strategy_best2.py @@ -7,7 +7,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, fidelity_promo) >>> Order(ann, cart, fidelity_promo) @@ -69,8 +69,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' def fidelity_promo(order): diff --git a/10-dp-1class-func/strategy_best3.py b/10-dp-1class-func/strategy_best3.py index 0574505..39ce3bf 100644 --- a/10-dp-1class-func/strategy_best3.py +++ b/10-dp-1class-func/strategy_best3.py @@ -8,7 +8,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, fidelity_promo) >>> Order(ann, cart, fidelity_promo) @@ -73,8 +73,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' # tag::STRATEGY_BEST3[] diff --git a/10-dp-1class-func/strategy_best4.py b/10-dp-1class-func/strategy_best4.py index b2d12ae..955ac3a 100644 --- a/10-dp-1class-func/strategy_best4.py +++ b/10-dp-1class-func/strategy_best4.py @@ -8,7 +8,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, fidelity) >>> Order(ann, cart, fidelity) @@ -71,8 +71,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' # tag::STRATEGY_BEST4[] diff --git a/10-dp-1class-func/strategy_param.py b/10-dp-1class-func/strategy_param.py index 5447189..7318530 100644 --- a/10-dp-1class-func/strategy_param.py +++ b/10-dp-1class-func/strategy_param.py @@ -6,7 +6,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, fidelity_promo(10)) >>> Order(ann, cart, fidelity_promo(10)) @@ -73,8 +73,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' # tag::STRATEGY_PARAM[] @@ -85,7 +84,7 @@ Promotion = Callable[[Order], float] # <2> def fidelity_promo(percent: float) -> Promotion: """discount for customers with 1000 or more fidelity points""" return lambda order: ( - order.total() * percent / 100.0 if order.customer.fidelity >= 1000 else 0 + order.total() * percent / 100 if order.customer.fidelity >= 1000 else 0 ) @@ -96,7 +95,7 @@ def bulk_item_promo(percent: float) -> Promotion: discount = 0 for item in order.cart: if item.quantity >= 20: - discount += item.total() * percent / 100.0 + discount += item.total() * percent / 100 return discount return discounter @@ -111,13 +110,13 @@ class LargeOrderPromo: def __call__(self, order: Order) -> float: distinct_items = {item.product for item in order.cart} if len(distinct_items) >= 10: - return order.total() * self.percent / 100.0 + return order.total() * self.percent / 100 return 0 def general_discount(percent: float, order: Order) -> float: """unrestricted discount; usage: ``partial(general_discount, 5)``""" - return order.total() * percent / 100.0 + return order.total() * percent / 100 # end::STRATEGY[] diff --git a/10-dp-1class-func/strategy_param_test.py b/10-dp-1class-func/strategy_param_test.py index f7b07e3..d550e2b 100644 --- a/10-dp-1class-func/strategy_param_test.py +++ b/10-dp-1class-func/strategy_param_test.py @@ -23,7 +23,7 @@ def cart_plain() -> List[LineItem]: return [ LineItem('banana', 4, 0.5), LineItem('apple', 10, 1.5), - LineItem('watermellon', 5, 5.0), + LineItem('watermelon', 5, 5.0), ] diff --git a/10-dp-1class-func/strategy_test.py b/10-dp-1class-func/strategy_test.py index 26640fa..bf225ae 100644 --- a/10-dp-1class-func/strategy_test.py +++ b/10-dp-1class-func/strategy_test.py @@ -21,7 +21,7 @@ def cart_plain() -> List[LineItem]: return [ LineItem('banana', 4, 0.5), LineItem('apple', 10, 1.5), - LineItem('watermellon', 5, 5.0), + LineItem('watermelon', 5, 5.0), ] diff --git a/10-dp-1class-func/untyped/classic_strategy.py b/10-dp-1class-func/untyped/classic_strategy.py index 2242fdd..b969780 100644 --- a/10-dp-1class-func/untyped/classic_strategy.py +++ b/10-dp-1class-func/untyped/classic_strategy.py @@ -8,7 +8,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), # <2> ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, FidelityPromo()) # <3> >>> Order(ann, cart, FidelityPromo()) # <4> @@ -65,8 +65,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' class Promotion(ABC): # the Strategy: an abstract base class diff --git a/10-dp-1class-func/untyped/promotions.py b/10-dp-1class-func/untyped/promotions.py index a8795a0..aed4d1b 100644 --- a/10-dp-1class-func/untyped/promotions.py +++ b/10-dp-1class-func/untyped/promotions.py @@ -1,4 +1,3 @@ - def fidelity_promo(order): """5% discount for customers with 1000 or more fidelity points""" return order.total() * .05 if order.customer.fidelity >= 1000 else 0 diff --git a/10-dp-1class-func/untyped/strategy.py b/10-dp-1class-func/untyped/strategy.py index b0c9c86..1f8ad4c 100644 --- a/10-dp-1class-func/untyped/strategy.py +++ b/10-dp-1class-func/untyped/strategy.py @@ -8,7 +8,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, fidelity_promo) # <2> >>> Order(ann, cart, fidelity_promo) @@ -64,8 +64,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' # <2> diff --git a/10-dp-1class-func/untyped/strategy_best.py b/10-dp-1class-func/untyped/strategy_best.py index 89281a8..c0585f7 100644 --- a/10-dp-1class-func/untyped/strategy_best.py +++ b/10-dp-1class-func/untyped/strategy_best.py @@ -7,7 +7,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, fidelity_promo) >>> Order(ann, cart, fidelity_promo) @@ -71,8 +71,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' def fidelity_promo(order): diff --git a/10-dp-1class-func/untyped/strategy_best2.py b/10-dp-1class-func/untyped/strategy_best2.py index c7a9eff..1f4700f 100644 --- a/10-dp-1class-func/untyped/strategy_best2.py +++ b/10-dp-1class-func/untyped/strategy_best2.py @@ -7,7 +7,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, fidelity_promo) >>> Order(ann, cart, fidelity_promo) @@ -71,8 +71,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' def fidelity_promo(order): diff --git a/10-dp-1class-func/untyped/strategy_best3.py b/10-dp-1class-func/untyped/strategy_best3.py index 1b30114..de6ce4d 100644 --- a/10-dp-1class-func/untyped/strategy_best3.py +++ b/10-dp-1class-func/untyped/strategy_best3.py @@ -8,7 +8,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, fidelity_promo) >>> Order(ann, cart, fidelity_promo) @@ -75,8 +75,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' # tag::STRATEGY_BEST3[] diff --git a/10-dp-1class-func/untyped/strategy_best4.py b/10-dp-1class-func/untyped/strategy_best4.py index afa05e1..b523752 100644 --- a/10-dp-1class-func/untyped/strategy_best4.py +++ b/10-dp-1class-func/untyped/strategy_best4.py @@ -8,7 +8,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, fidelity) >>> Order(ann, cart, fidelity) @@ -72,8 +72,7 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' # tag::STRATEGY_BEST4[] diff --git a/10-dp-1class-func/untyped/strategy_param.py b/10-dp-1class-func/untyped/strategy_param.py index 4c23623..d5cc931 100644 --- a/10-dp-1class-func/untyped/strategy_param.py +++ b/10-dp-1class-func/untyped/strategy_param.py @@ -6,7 +6,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, fidelity_promo(10)) >>> Order(ann, cart, fidelity_promo(10)) @@ -60,13 +60,12 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' def fidelity_promo(percent): """discount for customers with 1000 or more fidelity points""" - return lambda order: (order.total() * percent/100.0 + return lambda order: (order.total() * percent / 100 if order.customer.fidelity >= 1000 else 0) @@ -76,7 +75,7 @@ def bulk_item_promo(percent): discount = 0 for item in order.cart: if item.quantity >= 20: - discount += item.total() * percent/100.0 + discount += item.total() * percent / 100 return discount return discounter @@ -86,6 +85,6 @@ def large_order_promo(percent): def discounter(order): distinct_items = {item.product for item in order.cart} if len(distinct_items) >= 10: - return order.total() * percent / 100.0 + return order.total() * percent / 100 return 0 return discounter diff --git a/10-dp-1class-func/untyped/strategy_param2.py b/10-dp-1class-func/untyped/strategy_param2.py index 91d77f2..625bbca 100644 --- a/10-dp-1class-func/untyped/strategy_param2.py +++ b/10-dp-1class-func/untyped/strategy_param2.py @@ -6,7 +6,7 @@ >>> ann = Customer('Ann Smith', 1100) >>> cart = [LineItem('banana', 4, .5), ... LineItem('apple', 10, 1.5), - ... LineItem('watermellon', 5, 5.0)] + ... LineItem('watermelon', 5, 5.0)] >>> Order(joe, cart, FidelityPromo(10)) >>> Order(ann, cart, FidelityPromo(10)) @@ -60,11 +60,10 @@ class Order: # the Context return self.total() - discount def __repr__(self): - fmt = '' - return fmt.format(self.total(), self.due()) + return f'' -class Promotion(): +class Promotion: """compute discount for order""" def __init__(self, percent): @@ -79,7 +78,7 @@ class FidelityPromo(Promotion): def __call__(self, order): if order.customer.fidelity >= 1000: - return order.total() * self.percent/100.0 + return order.total() * self.percent / 100 return 0 @@ -90,7 +89,7 @@ class BulkItemPromo(Promotion): discount = 0 for item in order.cart: if item.quantity >= 20: - discount += item.total() * self.percent/100.0 + discount += item.total() * self.percent / 100 return discount @@ -100,5 +99,5 @@ class LargeOrderPromo(Promotion): def __call__(self, order): distinct_items = {item.product for item in order.cart} if len(distinct_items) >= 10: - return order.total() * self.percent / 100.0 + return order.total() * self.percent / 100 return 0 diff --git a/11-pythonic-obj/mem_test.py b/11-pythonic-obj/mem_test.py index ecf6c04..c18527a 100644 --- a/11-pythonic-obj/mem_test.py +++ b/11-pythonic-obj/mem_test.py @@ -8,17 +8,17 @@ if len(sys.argv) == 2: module_name = sys.argv[1].replace('.py', '') module = importlib.import_module(module_name) else: - print('Usage: {} '.format()) + print(f'Usage: {sys.argv[0]} ') sys.exit(1) fmt = 'Selected Vector2d type: {.__name__}.{.__name__}' print(fmt.format(module, module.Vector2d)) mem_init = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss -print('Creating {:,} Vector2d instances'.format(NUM_VECTORS)) +print(f'Creating {NUM_VECTORS:,} Vector2d instances') vectors = [module.Vector2d(3.0, 4.0) for i in range(NUM_VECTORS)] mem_final = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss -print('Initial RAM usage: {:14,}'.format(mem_init)) -print(' Final RAM usage: {:14,}'.format(mem_final)) +print(f'Initial RAM usage: {mem_init:14,}') +print(f' Final RAM usage: {mem_final:14,}') diff --git a/11-pythonic-obj/private/Expose.java b/11-pythonic-obj/private/Expose.java index dac57fe..35c2dbd 100644 --- a/11-pythonic-obj/private/Expose.java +++ b/11-pythonic-obj/private/Expose.java @@ -18,7 +18,7 @@ public class Expose { System.out.println("message.secret = " + wasHidden); } catch (IllegalAccessException e) { - // this will not happen after setAcessible(true) + // this will not happen after setAccessible(true) System.err.println(e); } } diff --git a/11-pythonic-obj/private/expose.py b/11-pythonic-obj/private/expose.py index 1df710c..094acbd 100644 --- a/11-pythonic-obj/private/expose.py +++ b/11-pythonic-obj/private/expose.py @@ -3,4 +3,4 @@ import Confidential message = Confidential('top secret text') secret_field = Confidential.getDeclaredField('secret') secret_field.setAccessible(True) # break the lock! -print 'message.secret =', secret_field.get(message) +print('message.secret =', secret_field.get(message)) diff --git a/11-pythonic-obj/private/leakprivate.py b/11-pythonic-obj/private/leakprivate.py index bd20bf0..5e4ecbd 100644 --- a/11-pythonic-obj/private/leakprivate.py +++ b/11-pythonic-obj/private/leakprivate.py @@ -7,5 +7,5 @@ for field in fields: # list private fields only if Modifier.isPrivate(field.getModifiers()): field.setAccessible(True) # break the lock - print 'field:', field - print '\t', field.getName(), '=', field.get(message) + print('field:', field) + print('\t', field.getName(), '=', field.get(message)) diff --git a/11-pythonic-obj/private/no_respect.py b/11-pythonic-obj/private/no_respect.py index 4eb3476..291eed8 100644 --- a/11-pythonic-obj/private/no_respect.py +++ b/11-pythonic-obj/private/no_respect.py @@ -1,4 +1,3 @@ - """ In the Jython registry file there is this line: @@ -14,4 +13,4 @@ message = Confidential('top secret text') for name in dir(message): attr = getattr(message, name) if not callable(attr): # non-methods only - print name + '\t=', attr + print(name + '\t=', attr) diff --git a/11-pythonic-obj/vector2d_v3.py b/11-pythonic-obj/vector2d_v3.py index f2e31ee..376a8a4 100644 --- a/11-pythonic-obj/vector2d_v3.py +++ b/11-pythonic-obj/vector2d_v3.py @@ -81,7 +81,7 @@ Tests of hashing: >>> v2 = Vector2d(3.1, 4.2) >>> hash(v1), hash(v2) (7, 384307168202284039) - >>> len(set([v1, v2])) + >>> len({v1, v2}) 2 """ diff --git a/11-pythonic-obj/vector2d_v3_prophash.py b/11-pythonic-obj/vector2d_v3_prophash.py index 0f8830f..3552530 100644 --- a/11-pythonic-obj/vector2d_v3_prophash.py +++ b/11-pythonic-obj/vector2d_v3_prophash.py @@ -83,7 +83,7 @@ Tests of hashing: >>> v2 = Vector2d(3.1, 4.2) >>> hash(v1), hash(v2) (7, 384307168202284039) - >>> len(set([v1, v2])) + >>> len({v1, v2}) 2 # end::VECTOR2D_V3_DEMO[] diff --git a/11-pythonic-obj/vector2d_v3_slots.py b/11-pythonic-obj/vector2d_v3_slots.py index 0e56556..1d0536a 100644 --- a/11-pythonic-obj/vector2d_v3_slots.py +++ b/11-pythonic-obj/vector2d_v3_slots.py @@ -80,7 +80,7 @@ Tests of hashing: >>> v2 = Vector2d(3.1, 4.2) >>> hash(v1), hash(v2) (7, 384307168202284039) - >>> len(set([v1, v2])) + >>> len({v1, v2}) 2 # end::VECTOR2D_V3_DEMO[] diff --git a/20-concurrency/primes/primes.py b/20-concurrency/primes/primes.py index 63dd7a1..0884dcd 100755 --- a/20-concurrency/primes/primes.py +++ b/20-concurrency/primes/primes.py @@ -1,5 +1,4 @@ import math -import itertools PRIME_FIXTURE = [ @@ -36,7 +35,7 @@ def is_prime(n) -> bool: if n % 2 == 0: return False - root = int(math.floor(math.sqrt(n))) + root = math.floor(math.sqrt(n)) for i in range(3, root + 1, 2): if n % i == 0: return False diff --git a/20-concurrency/primes/procs_py37.py b/20-concurrency/primes/procs_py37.py index 2ab5d37..19d86e7 100644 --- a/20-concurrency/primes/procs_py37.py +++ b/20-concurrency/primes/procs_py37.py @@ -34,10 +34,10 @@ def main() -> None: label = 'P' if prime else ' ' print(f'{n:16} {label} {elapsed:9.6f}s') - time = perf_counter() - t0 print('Total time:', f'{time:0.2f}s') + if __name__ == '__main__': main() # end::PRIMES_PROC_MAIN[] diff --git a/20-concurrency/primes/spinner_async_nap.py b/20-concurrency/primes/spinner_async_nap.py index 30fd4f2..13fa066 100644 --- a/20-concurrency/primes/spinner_async_nap.py +++ b/20-concurrency/primes/spinner_async_nap.py @@ -18,7 +18,7 @@ async def is_prime(n): return False sleep = asyncio.sleep # <1> - root = int(math.floor(math.sqrt(n))) + root = math.floor(math.sqrt(n)) for i in range(3, root + 1, 2): if n % i == 0: return False diff --git a/20-concurrency/primes/spinner_prime_async_nap.py b/20-concurrency/primes/spinner_prime_async_nap.py index c1f82ad..48166ee 100644 --- a/20-concurrency/primes/spinner_prime_async_nap.py +++ b/20-concurrency/primes/spinner_prime_async_nap.py @@ -18,7 +18,7 @@ async def is_prime(n): return False sleep = asyncio.sleep # <1> - root = int(math.floor(math.sqrt(n))) + root = math.floor(math.sqrt(n)) for i in range(3, root + 1, 2): if n % i == 0: return False diff --git a/20-concurrency/primes/spinner_prime_proc.py b/20-concurrency/primes/spinner_prime_proc.py index e1454d1..8a532af 100644 --- a/20-concurrency/primes/spinner_prime_proc.py +++ b/20-concurrency/primes/spinner_prime_proc.py @@ -7,7 +7,6 @@ from multiprocessing import Process, Event from multiprocessing import synchronize import itertools -import time from primes import is_prime diff --git a/20-concurrency/primes/spinner_prime_thread.py b/20-concurrency/primes/spinner_prime_thread.py index 11db5d9..000743e 100644 --- a/20-concurrency/primes/spinner_prime_thread.py +++ b/20-concurrency/primes/spinner_prime_thread.py @@ -6,7 +6,6 @@ from threading import Thread, Event import itertools -import time from primes import is_prime diff --git a/20-concurrency/primes/spinner_thread.py b/20-concurrency/primes/spinner_thread.py index 795e116..c7e4c8b 100644 --- a/20-concurrency/primes/spinner_thread.py +++ b/20-concurrency/primes/spinner_thread.py @@ -7,7 +7,6 @@ # tag::SPINNER_THREAD_TOP[] from threading import Thread, Event import itertools -import time from primes import is_prime diff --git a/20-concurrency/primes/threads_py37.py b/20-concurrency/primes/threads_py37.py index 296d26c..d98aca0 100644 --- a/20-concurrency/primes/threads_py37.py +++ b/20-concurrency/primes/threads_py37.py @@ -1,5 +1,5 @@ from time import perf_counter -from typing import Tuple, List, NamedTuple +from typing import List, NamedTuple from threading import Thread from queue import SimpleQueue diff --git a/README.md b/README.md index ec0a6d0..defbde9 100644 --- a/README.md +++ b/README.md @@ -49,4 +49,4 @@ Part / Chapter #|Title|Directory|Notebook|1st ed. Chapter # **VI – Metaprogramming**| 22|Dynamic Attributes and Properties|[22-dyn-attr-prop](22-dyn-attr-prop)||19 23|Attribute Descriptors|[23-descriptor](23-descriptor)||20 -24|Class Metapgrogramming|[24-class-metaprog](24-class-metaprog)||21 +24|Class Metaprogramming|[24-class-metaprog](24-class-metaprog)||21