-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function_rules.py
93 lines (65 loc) · 3.86 KB
/
lambda_function_rules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
----------------------------------------------------------------------------------
[2.10 Lambda Function Rules](https://docs.sourcery.ai/Reference/Custom-Rules/gpsg/#210-lambda-function-rules)
----------------------------------------------------------------------------------
This example file contains pieces of code that either comply with or violate the
rules `lambdas-should-be-short`, `map-lambda-to-generator` and
`filter-lambda-to-generator`.
Also note that the rules `map-lambda-to-generator` and
`filter-lambda-to-generator` have replacements, and hence Sourcery can
automatically fix them.
"""
from typing import Callable, Iterator
# no violation: small lambdas are OK
def squarer() -> Callable[[int], int]:
"""Generate a function that calculates the square of integers."""
return lambda x: x**2
# no violation: small lambdas are OK
def power() -> Callable[[int], int]:
"""Generate a function that takes two `x` and `y` and calculates `x**y`."""
return lambda x, y: x**y
# violation of `lambdas-should-be-short` because that lambda is huge!
def get_very_long_and_involved_calculator_for_x() -> Callable[[int], int]:
"""Generate a function that does a very involved calculation with `x`."""
return lambda x: do_something_very_long_and_involved_with(x) - do_other_very_long_and_involved_things_with(x)
# violation of `lambdas-should-be-short` because that lambda is huge!
def get_very_long_and_involved_calculator_for_xyz() -> Callable[[int, int, int], int]:
"""Generate a function that does an involved calculation with multiple inputs."""
return lambda x, y, z: do_something_very_long_and_involved_with(x) - do_other_very_long_and_involved_things_with(y, z)
# violation of `map-lambda-to-generator`: mapping lambdas can be refactored as Pythonic
# generators
def transform_numbers_with_lambda(numbers: list[float]) -> Iterator[float]:
"""Perform transformation in list of numbers."""
# should be `return (x**2 for x in numbers)`
return map(lambda x: x**2, numbers)
# violation of `map-lambda-to-generator`: mapping lambdas and converting the result to a
# list can be refactored as Pythonic list comprehensions
def transform_numbers_with_lambda_list(numbers: list[float]) -> list[float]:
"""Perform transformation in list of numbers, returning another list."""
# should be `return [x**2 for x in numbers]`
return list(map(lambda x: x**2, numbers))
# no violation: mapping with an existing function is alright
def transform_numbers_with_function(numbers: list[float]) -> Iterator[float]:
"""Perform transformation in list of numbers."""
return map(squarer, numbers)
# no violation: mapping with multiple arguments is OK since the generator version would
# involve a call to `zip`, which is not necessarily cleaner
def add_numbers(left: list[float], right: list[float]) -> Iterator[float]:
"""Perform transformation in list of numbers."""
return map(lambda x, y: x + y, left, right)
# violation of `filter-lambda-to-generator`: filtering with lambdas can be refactored as
# Pythonic generators
def filter_numbers_with_lambda(numbers: list[float]) -> Iterator[float]:
"""Filter numbers in list."""
# should be `return (x for x in numbers if x % 2 == 0)`
return filter(lambda x: x % 2 == 0, numbers)
# violation of `filter-lambda-to-generator`: filtering with lambdas and converting the
# result to a list can be refactored as Pythonic list comprehensions
def filter_numbers_with_lambda_list(numbers: list[float]) -> list[float]:
"""Filter numbers in list, returning another list"""
# should be `return [x for x in numbers if x % 2 == 0]`
return list(filter(lambda x: x % 2 == 0, numbers))
# no violation: mapping with an existing function is alright
def filter_numbers_with_function(numbers: list[float]) -> Iterator[float]:
"""Perform transformation in list of numbers."""
return filter(is_even, numbers)