- 
                Notifications
    You must be signed in to change notification settings 
- Fork 0
Sourcery refactored main branch #1
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 | 
|---|---|---|
|  | @@ -72,9 +72,7 @@ def fact(k: int) -> int: | |
| >>> fact(4) | ||
| 24 | ||
| """ | ||
| if k < 2: | ||
| return 1 | ||
| return reduce(operator.mul, range(2,k+1)) | ||
| return 1 if k < 2 else reduce(operator.mul, range(2,k+1)) | ||
| 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  
 | ||
|  | ||
| # The implementation uses ``reduce( operator.mul, ... )`` to compute | ||
| # the product of a sequence of integer values. | ||
|  | @@ -121,14 +119,14 @@ def gamma(s: float, z: float) -> float: | |
|  | ||
| def terms(s: float, z: float) -> Iterator[float]: | ||
| for k in range(1000): | ||
| term = ((-1)**k/fact(k))*(z**(s+k)/(s+k)) | ||
| yield term | ||
|  | ||
| T_ = TypeVar("T_") | ||
| yield ((-1)**k/fact(k))*(z**(s+k)/(s+k)) | ||
|  | ||
| T_ = TypeVar("T_") | ||
| def take_until(function: Callable[[T_], bool], source: Iterable[T_]) -> Iterator[T_]: | ||
| for v in source: | ||
| if function(v): return | ||
| yield v | ||
|  | ||
| 
      Comment on lines
    
      -124
     to 
      +129
    
   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  
 | ||
| ε = 1E-8 | ||
| return sum(take_until(lambda t: abs(t) < ε, terms(s, z))) | ||
|  | ||
|  | @@ -498,11 +496,10 @@ def Gamma_Half(k: float) -> float: | |
| 0.8862269 | ||
| """ | ||
| ε = 1E-6 | ||
| if abs(k-int(k)-.5) < ε: | ||
| n = int(k-.5) | ||
| return fact(2*n)/(4**n*fact(n))*math.sqrt(math.pi) | ||
| else: | ||
| if abs(k - int(k) - 0.5) >= ε: | ||
| return float(Gamma2(k)) | ||
| n = int(k-.5) | ||
| return fact(2*n)/(4**n*fact(n))*math.sqrt(math.pi) | ||
| 
      Comment on lines
    
      -501
     to 
      +502
    
   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 the value is an :math:`n+\dfrac{1}{2} \pm \epsilon`, we'll use the special | ||
| # close-form expression. If the value is not close to :math:`n+\dfrac{1}{2}`, | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -5,22 +5,15 @@ | |
|  | ||
|  | ||
| def sum_numeric(limit: int = 10) -> int: | ||
| s = 0 | ||
| for n in range(1, limit): | ||
| if n % 3 == 0 or n % 5 == 0: | ||
| s += n | ||
| return s | ||
| return sum(n for n in range(1, limit) if n % 3 == 0 or n % 5 == 0) | ||
| 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 test_sum_numeric() -> None: | ||
| assert sum_numeric() == 23 | ||
|  | ||
|  | ||
| def sum_object_light(limit: int = 10) -> int: | ||
| m: list[int] = list() | ||
| for n in range(1, limit): | ||
| if n % 3 == 0 or n % 5 == 0: | ||
| m.append(n) | ||
| m: list[int] = [n for n in range(1, limit) if n % 3 == 0 or n % 5 == 0] | ||
| 
      Comment on lines
    
      -20
     to 
      +16
    
   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 sum(m) | ||
|  | ||
|  | ||
|  | @@ -53,9 +46,7 @@ def test_full_oo() -> None: | |
|  | ||
| def foldr(seq: Sequence[int], op: Callable[[int, int], int], init: int) -> int: | ||
| """Recursive reduce operation, fold from right to left.""" | ||
| if len(seq) == 0: | ||
| return init | ||
| return op(seq[0], foldr(seq[1:], op, init)) | ||
| return init if len(seq) == 0 else op(seq[0], foldr(seq[1:], op, init)) | ||
| 
      Comment on lines
    
      -56
     to 
      +49
    
   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 test_foldr() -> None: | ||
|  | @@ -67,9 +58,7 @@ def test_foldr() -> None: | |
|  | ||
|  | ||
| def sumr(seq: Sequence[int]) -> int: | ||
| if len(seq) == 0: | ||
| return 0 | ||
| return seq[0] + sumr(seq[1:]) | ||
| return 0 if len(seq) == 0 else seq[0] + sumr(seq[1:]) | ||
| 
      Comment on lines
    
      -70
     to 
      +61
    
   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  
 | ||
|  | ||
|  | ||
| REPL_sumr = """ | ||
|  | @@ -198,15 +187,11 @@ def foldright(n, v=1): | |
|  | ||
|  | ||
| def foldleft(n: int) -> list[int]: | ||
| if n == 0: | ||
| return [] | ||
| return foldleft(n - 1) + [n] | ||
| return [] if n == 0 else foldleft(n - 1) + [n] | ||
| 
      Comment on lines
    
      -201
     to 
      +190
    
   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 foldright(n: int, v: int = 1) -> list[int]: | ||
| if v == n: | ||
| return [v] | ||
| return [v] + foldright(n, v + 1) | ||
| return [v] if v == n else [v] + foldright(n, v + 1) | ||
| 
      Comment on lines
    
      -207
     to 
      +194
    
   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 test_fold_left_right() -> None: | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -80,9 +80,7 @@ def repeat(f: Callable[[float], float], a: float) -> Iterator[float]: | |
| def within(ε: float, iterable: Iterator[float]) -> float: | ||
| def head_tail(ε: float, a: float, iterable: Iterator[float]) -> float: | ||
| b = next(iterable) | ||
| if abs(a - b) <= ε: | ||
| return b | ||
| return head_tail(ε, b, iterable) | ||
| return b if abs(a - b) <= ε else head_tail(ε, b, iterable) | ||
| 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 head_tail(ε, next(iterable), iterable) | ||
|  | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -21,13 +21,12 @@ def distance( | |
| def redundant( | ||
| lat1: float, lon1: float, lat2: float, lon2: float, R: float = 360 * 60 / math.tau | ||
| ) -> float: | ||
| d = math.hypot( | ||
| return math.hypot( | ||
| R | ||
| * (math.radians(lon1) - math.radians(lon2)) | ||
| * math.cos((math.radians(lat1) + math.radians(lat2)) / 2), | ||
| R * (math.radians(lat1) - math.radians(lat2)), | ||
| ) | ||
| return d | ||
| 
      Comment on lines
    
      -24
     to 
      -30
    
   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 imperative( | ||
|  | @@ -52,8 +51,7 @@ def imperative( | |
| x2 = x ** 2 | ||
| y2 = y ** 2 | ||
| x2y2 = x2 + y2 | ||
| d = math.sqrt(x2y2) | ||
| return d | ||
| return math.sqrt(x2y2) | ||
| 
      Comment on lines
    
      -55
     to 
      +54
    
   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  
 | ||
|  | ||
|  | ||
| from pytest import approx | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -18,19 +18,15 @@ def main(source_path: Path = DEFAULT_PATH) -> None: | |
| with source_path.open() as source: | ||
| rdr = csv.DictReader(source) | ||
| for row in rdr: | ||
| if "-" in row["ZIP"]: | ||
| text_zip = row["ZIP"] | ||
| text_zip = row["ZIP"] | ||
| if "-" in text_zip: | ||
| missing_zeroes = 10 - len(text_zip) | ||
| if missing_zeroes: | ||
| text_zip = missing_zeroes * "0" + text_zip | ||
| elif 5 < len(text_zip) < 9: | ||
| missing_zeroes = 9 - len(text_zip) | ||
| else: | ||
| text_zip = row["ZIP"] | ||
| if 5 < len(row["ZIP"]) < 9: | ||
| missing_zeroes = 9 - len(text_zip) | ||
| else: | ||
| missing_zeroes = 5 - len(text_zip) | ||
| if missing_zeroes: | ||
| text_zip = missing_zeroes * "0" + text_zip | ||
| missing_zeroes = 5 - len(text_zip) | ||
| if missing_zeroes: | ||
| text_zip = missing_zeroes * "0" + text_zip | ||
| 
      Comment on lines
    
      -21
     to 
      +29
    
   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  
 | ||
| frequency[text_zip] += 1 | ||
| print(frequency) | ||
|  | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -112,9 +112,7 @@ def sum_to(limit: int) -> int: | |
| def isprimer(n: int) -> bool: | ||
| def iscoprime(k: int, a: int, b: int) -> bool: | ||
| """Is k coprime with a value in the given range?""" | ||
| if a == b: | ||
| return True | ||
| return (k % a != 0) and iscoprime(k, a + 1, b) | ||
| return True if a == b else (k % a != 0) and iscoprime(k, a + 1, b) | ||
| 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 iscoprime(n, 2, int(math.sqrt(n)) + 1) | ||
|  | ||
|  | @@ -135,17 +133,12 @@ def test_isprimer() -> None: | |
|  | ||
| def isprimei(n: int) -> bool: | ||
| """Is n prime?""" | ||
| if n < 2: | ||
| if n < 2 or n != 2 and n % 2 == 0: | ||
| return False | ||
| elif n == 2: | ||
| return True | ||
| elif n % 2 == 0: | ||
| return False | ||
| else: | ||
| for i in range(3, 1 + int(math.sqrt(n)), 2): | ||
| if n % i == 0: | ||
| return False | ||
| return True | ||
| return all(n % i != 0 for i in range(3, 1 + int(math.sqrt(n)), 2)) | ||
| 
      Comment on lines
    
      -138
     to 
      +141
    
   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 test_isprimei() -> None: | ||
|  | @@ -186,7 +179,7 @@ def isprimeg(n: int) -> bool: | |
| return True | ||
| if n % 2 == 0: | ||
| return False | ||
| return not any(n % p == 0 for p in range(3, int(math.sqrt(n)) + 1, 2)) | ||
| return all(n % p != 0 for p in range(3, int(math.sqrt(n)) + 1, 2)) | ||
| 
      Comment on lines
    
      -189
     to 
      +182
    
   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 test_isprimeg() -> None: | ||
|  | @@ -401,9 +394,11 @@ def limit_of_performance() -> None: | |
|  | ||
|  | ||
| def strip_head(source: TextIO, line: str) -> tuple[TextIO, str]: | ||
| if len(line.strip()) == 0: | ||
| return source, source.readline() | ||
| return strip_head(source, source.readline()) | ||
| return ( | ||
| strip_head(source, source.readline()) | ||
| if line.strip() | ||
| else (source, source.readline()) | ||
| ) | ||
| 
      Comment on lines
    
      -404
     to 
      +401
    
   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 get_columns(source: TextIO, line: str) -> Iterator[str]: | ||
|  | @@ -432,17 +427,17 @@ def performance() -> None: | |
| assert len(primes) == 1000 | ||
|  | ||
| start = time.perf_counter() | ||
| for repeat in range(1000): | ||
| for _ in range(1000): | ||
| assert all(isprimei(x) for x in primes) | ||
| print(f"all() {time.perf_counter() - start:.3f}") | ||
|  | ||
| start = time.perf_counter() | ||
| for repeat in range(1000): | ||
| assert not any(not isprimei(x) for x in primes) | ||
| for _ in range(1000): | ||
| assert all(isprimei(x) for x in primes) | ||
| print(f"not any() {time.perf_counter() - start:.3f}") | ||
|  | ||
| start = time.perf_counter() | ||
| for repeat in range(1000): | ||
| for _ in range(1000): | ||
| 
      Comment on lines
    
      -435
     to 
      +440
    
   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  
 | ||
| assert reduce(lambda x, y: x and y, (isprimei(x) for x in primes)) | ||
| print(f"reduce(and,...) {time.perf_counter() - start:.3f}") | ||
|  | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -80,9 +80,7 @@ def shifty(b: int) -> int: | |
|  | ||
|  | ||
| def multy(b: int) -> int: | ||
| if b == 0: | ||
| return 1 | ||
| return 2 * multy(b - 1) | ||
| return 1 if b == 0 else 2 * multy(b - 1) | ||
| 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 faster(b: int) -> int: | ||
|  | @@ -95,9 +93,9 @@ def faster(b: int) -> int: | |
|  | ||
|  | ||
| def test_mults() -> None: | ||
| assert shifty(17) - 1 == 131071 | ||
| assert multy(17) - 1 == 131071 | ||
| assert faster(17) - 1 == 131071 | ||
| assert shifty(17) == 131072 | ||
| assert multy(17) == 131072 | ||
| assert faster(17) == 131072 | ||
| 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  
 | ||
|  | ||
|  | ||
| # Implementations of Mersenne with strategy objects plugged in properly. | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -43,15 +43,11 @@ def test_clean_decimal_2() -> None: | |
|  | ||
| def remove(text: str, chars: str) -> str: | ||
| """Remove all of the given chars from a string.""" | ||
| if chars: | ||
| return remove(text.replace(chars[0], ""), chars[1:]) | ||
| return text | ||
| return remove(text.replace(chars[0], ""), chars[1:]) if chars else text | ||
| 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 clean_decimal_3(text: str | None) -> Decimal | None: | ||
| if text is None: | ||
| return None | ||
| return Decimal(remove(text, "$,")) | ||
| return None if text is None else Decimal(remove(text, "$,")) | ||
| 
      Comment on lines
    
      -52
     to 
      +50
    
   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 test_clean_decimal_3() -> None: | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -99,7 +99,7 @@ def pfactorsr(x: int) -> Iterator[int]: | |
| """Pure Recursion factors. Limited to numbers below about 4,000,000""" | ||
|  | ||
| def factor_n(x: int, n: int) -> Iterator[int]: | ||
| if n * n > x: | ||
| if n**2 > x: | ||
| 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  
 | ||
| yield x | ||
| return | ||
| if x % n == 0: | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -34,8 +34,7 @@ def float_none(data: str) -> float | None: | |
| 1.23 | ||
| """ | ||
| try: | ||
| data_f = float(data) | ||
| return data_f | ||
| return float(data) | ||
| 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  
 | ||
| except ValueError: | ||
| return None | ||
|  | ||
|  | @@ -58,7 +57,7 @@ def float_row(row: list[str]) -> R_Float: | |
| return list(map(float_none, row)) | ||
|  | ||
| def all_numeric(row: R_Float) -> bool: | ||
| return not any(v is None for v in row) | ||
| return all(v is not None for v in row) | ||
| 
      Comment on lines
    
      -61
     to 
      +60
    
   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 cast(Iterator[list[float]], filter(all_numeric, map(float_row, row_iter))) | ||
|  | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -121,19 +121,14 @@ def color_GPL_r(file_obj: TextIO) -> Iterator[Color]: | |
| def read_head(file_obj: TextIO) -> tuple[TextIO, str, str, str]: | ||
| headers = "".join(file_obj.readline() for _ in range(4)) | ||
| if match := header_pat.match(headers): | ||
| return ( | ||
| file_obj, | ||
| match.group(1), | ||
| match.group(2), | ||
| file_obj.readline().rstrip(), | ||
| ) | ||
| return file_obj, match[1], match[2], file_obj.readline().rstrip() | ||
| else: | ||
| raise ValueError(f"invalid {headers!r}") | ||
|  | ||
| def read_tail( | ||
| file_obj: TextIO, palette_name: str, columns: str, next_line: str | ||
| ) -> Iterator[Color]: | ||
| if len(next_line) == 0: | ||
| file_obj: TextIO, palette_name: str, columns: str, next_line: str | ||
| ) -> Iterator[Color]: | ||
| if not next_line: | ||
| 
      Comment on lines
    
      -124
     to 
      +131
    
   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 | ||
| r, g, b, *name = next_line.split() | ||
| yield Color(int(r), int(g), int(b), " ".join(name)) | ||
|  | @@ -163,7 +158,7 @@ def row_iter_gpl(file_obj: TextIO) -> tuple[str, str, Iterator[list[str]]]: | |
| def read_head(file_obj: TextIO) -> tuple[str, str, TextIO]: | ||
| headers = "".join(file_obj.readline() for _ in range(4)) | ||
| if match := header_pat.match(headers): | ||
| return match.group(1), match.group(2), file_obj | ||
| return match[1], match[2], file_obj | ||
| 
      Comment on lines
    
      -166
     to 
      +161
    
   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  
 | ||
| else: | ||
| raise ValueError(f"invalid {headers!r}") | ||
|  | ||
|  | @@ -206,10 +201,7 @@ def load_colors(row_iter_gpl: tuple[str, str, Iterator[list[str]]]) -> dict[str, | |
| colors = tuple( | ||
| Color(int(r), int(g), int(b), " ".join(name)) for r, g, b, *name in row_iter | ||
| ) | ||
| # print( colors ) | ||
| mapping = dict((c.name, c) for c in colors) | ||
| # print( mapping ) | ||
| return mapping | ||
| return {c.name: c for c in colors} | ||
| 
      Comment on lines
    
      -209
     to 
      +204
    
   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  
 This removes the following comments ( why? ):  | ||
|  | ||
|  | ||
| REPL_test_gpl = """ | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -280,9 +280,7 @@ def legs_filter( | |
| ) -> Leg_Iter: | ||
| begin = next(lat_lon_iter) | ||
| for end in lat_lon_iter: | ||
| if rejection_rule(begin, end): | ||
| pass | ||
| else: | ||
| if not rejection_rule(begin, 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  
 | ||
| yield begin, end | ||
| begin = end | ||
|  | ||
|  | ||
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
make_filesrefactored with the following changes:use-fstring-for-concatenation)