-
Couldn't load subscription status.
- 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?
Conversation
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.
Due to GitHub API limits, only the first 60 comments can be shown.
| build_if_needed(pylit, f.with_suffix(f.suffix+'.txt'), f) | ||
| build_if_needed(pylit, f.with_suffix(f'{f.suffix}.txt'), f) | ||
| for f in files_py | ||
| ] | ||
| for item in txt_builds: | ||
| logger.info(item) | ||
|
|
||
| index_build = build_if_needed( | ||
| make_index, Path("index.txt"), | ||
| *[f.with_suffix(f.suffix+'.txt') for f in files_py] | ||
| make_index, | ||
| Path("index.txt"), | ||
| *[f.with_suffix(f'{f.suffix}.txt') for f in files_py], |
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_files refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation)
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Function fact refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| 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 | ||
|
|
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 gamma refactored with the following changes:
- Inline variable that is immediately yielded (
inline-immediately-yielded-variable)
| 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) |
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 Gamma_Half refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Function sum_numeric refactored with the following changes:
- Convert for loop into call to sum() (
sum-comprehension) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| 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)) |
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 isprimei refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Use any() instead of for loop (
use-any) - Remove redundant conditional [×2] (
remove-redundant-if) - Invert any/all to simplify comparisons (
invert-any-all)
| 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)) |
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 isprimeg refactored with the following changes:
- Invert any/all to simplify comparisons (
invert-any-all)
| 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()) | ||
| ) |
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 strip_head refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp) - Simplify comparison to string length (
simplify-str-len-comparison) - Swap if/else branches of if expression to remove negation (
swap-if-expression) - Replaces an empty collection equality with a boolean operation (
simplify-empty-collection-comparison)
| 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): |
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 performance refactored with the following changes:
- Replace unused for index with underscore [×3] (
for-index-underscore) - Invert any/all to simplify comparisons (
invert-any-all)
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Function multy refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Function test_mults refactored with the following changes:
- Simplify numeric comparison [×3] (
simplify-numeric-comparison)
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Function remove refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| if text is None: | ||
| return None | ||
| return Decimal(remove(text, "$,")) | ||
| return None if text is None else Decimal(remove(text, "$,")) |
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 clean_decimal_3 refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
|
|
||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Function pfactorsr refactored with the following changes:
- Replace x * x with x ** 2 (
square-identity)
| try: | ||
| data_f = float(data) | ||
| return data_f | ||
| return float(data) |
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 float_none refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| z_2 = (z(x, m_2, s_2) for x in samples2) | ||
| r = sum(zx1 * zx2 for zx1, zx2 in zip(z_1, z_2)) / len(samples1) | ||
| return r | ||
| return sum(zx1 * zx2 for zx1, zx2 in zip(z_1, z_2)) / len(samples1) |
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 corr refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| for line in data: | ||
| for x in line: | ||
| yield x | ||
| yield from line |
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 flatten refactored with the following changes:
- Replace yield inside for loop with yield from (
yield-from)
| 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()) | ||
| ) |
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 strip_head refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp) - Simplify comparison to string length (
simplify-str-len-comparison) - Swap if/else branches of if expression to remove negation (
swap-if-expression) - Replaces an empty collection equality with a boolean operation (
simplify-empty-collection-comparison)
| full_sized_items = list( | ||
| tuple(next(flat_iter) for i in range(n)) for row in range(len(sequence) // n) | ||
| ) | ||
| trailer = tuple(flat_iter) | ||
| if trailer: | ||
| full_sized_items = [ | ||
| tuple(next(flat_iter) for _ in range(n)) | ||
| for _ in range(len(sequence) // n) | ||
| ] | ||
| if trailer := tuple(flat_iter): |
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 group_by_seq refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Replace unused for index with underscore [×2] (
for-index-underscore) - Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension)
| return b | ||
| else: | ||
| return add(a - 1, b + 1) | ||
| return b if a == 0 else add(a - 1, b + 1) |
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 add refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| if n == 0: | ||
| return 1 | ||
| else: | ||
| return n * fact(n - 1) | ||
| return 1 if n == 0 else n * fact(n - 1) |
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 fact refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| n = n - 1 | ||
| n -= 1 |
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 fasts refactored with the following changes:
- Replace assignment with augmented assignment (
aug-assign)
| else: # Ignore other filesystem objects | ||
| pass |
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 all_print refactored with the following changes:
- Remove redundant pass statement (
remove-redundant-pass)
This removes the following comments ( why? ):
# Ignore other filesystem objects
| if n == 1: | ||
| return 1 | ||
| return fib(n - 1) + fib(n - 2) | ||
| return 1 if n == 1 else fib(n - 1) + fib(n - 2) |
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 fib refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| else: | ||
| q, r = divmod(n, 2) | ||
| if r == 1: | ||
| return a * fastexp_w(a, n - 1) | ||
| else: | ||
| return (t := fastexp_w(a, q)) * t | ||
| q, r = divmod(n, 2) | ||
| return a * fastexp_w(a, n - 1) if r == 1 else (t := fastexp_w(a, q)) * t |
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 fastexp_w refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else) - Replace if statement with if expression (
assign-if-exp)
| if n % 2 == 0: | ||
| return n // 2 | ||
| return 3 * n + 1 | ||
| return n // 2 if n % 2 == 0 else 3 * n + 1 |
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 syracuse refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| return dict(group(quantized)) | ||
| except StopIteration: | ||
| return dict() | ||
| return {} |
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 group_sort refactored with the following changes:
- Replace
dict()with{}(dict-literal)
| pairs: Callable[[RawPairIter], list[Pair]] = lambda source: list( | ||
| pairs: Callable[[RawPairIter], list[Pair]] = lambda source: [ | ||
| Pair(*row) for row in source | ||
| ) | ||
| ] |
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.
Lines 34-36 refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension)
| sorted_iter: Iterator[tuple[BaseT, ...]], | ||
| base: int, | ||
| same_rank_list: list[tuple[BaseT, ...]], | ||
| ) -> Iterator[tuple[float, tuple[BaseT, ...]]]: | ||
| sorted_iter: Iterator[tuple[BaseT, ...]], | ||
| base: int, | ||
| same_rank_list: list[tuple[BaseT, ...]], | ||
| ) -> Iterator[tuple[float, tuple[BaseT, ...]]]: |
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 rank2_rec refactored with the following changes:
- Replace yield inside for loop with yield from (
yield-from)
| data = list(Ranked_XY(rank=pmap(), raw=p) for p in pairs) | ||
| data = [Ranked_XY(rank=pmap(), raw=p) for p in pairs] | ||
|
|
||
| for attribute_name in ("x", "y"): | ||
| ranked = rank(data, lambda rxy: cast(float, getattr(rxy.raw, attribute_name))) | ||
| data = list( | ||
| data = [ | ||
| original.set( | ||
| rank=original.rank.set(attribute_name, r) # type: ignore [arg-type] | ||
| ) | ||
| for r, original in ranked | ||
| ) | ||
| ] |
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 rank_xy refactored with the following changes:
- Replace list(), dict() or set() with comprehension [×2] (
collection-builtin-to-comprehension)
Branch
mainrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
mainbranch, then run:Help us improve this pull request!