-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomprehensions_and_asignments.py
71 lines (50 loc) · 2.27 KB
/
comprehensions_and_asignments.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
# Usamos list o dict comprehensions cuando queremos crear estructuras de datos en una sola línea,
# por claridad de código. Cuando queremos realizar operaciones sobre los elementos,
# que hagan la comprehension
# demasiado compleja, mejor usar un bucle for
import re
from typing import Iterable
from typing import Set
def run_calculation(i: int) -> int:
pass
numbers = []
for i in range(10):
numbers.append(run_calculation(i))
numbers = [run_calculation(i) for i in range(10)]
# Generalmente la versión con comprehensions es un poco más eficiente
# Vamos a ver un ejemplo más complejo de comprehensions y posteriormente,
# asignaciones, que es una nueva feature de Python 3.8
ARN_REGEX = "hola"
ARN_REGEX = re.compile(r"arn:aws:[a-z0-9\-]*:[a-z0-9\-]*:(?P<account_id>\d+):.*")
def collect_account_ids_from_arns(arns: Iterable[str]) -> Set[str]:
"""Given several ARNs in the form
arn:partition:service:region:account-id:resource-id
Collect the unique account IDs found on those strings, and return them.
"""
collected_account_ids = set()
for arn in arns:
matched = re.match(ARN_REGEX, arn)
if matched is not None:
account_id = matched.groupdict()["account_id"]
collected_account_ids.add(account_id)
return collected_account_ids
def collect_account_ids_from_arns2(arns: Iterable[str]) -> Set[str]:
matched_arns = filter(None, (re.match(ARN_REGEX, arn) for arn in arns))
return {m.groupdict()["account_id"] for m in matched_arns}
def collect_account_ids_from_arns3(arns: Iterable[str]) -> Set[str]:
return {
matched.groupdict()["account_id"]
for arn in arns
if (matched := re.match(ARN_REGEX, arn)) is not None
}
# filter(function, iterable):
# Devuelve los elementos del iterable que evaluán True en la función 'function'
# Esto es un poco más rebuscado, si usamos filter(None, iterable)
# Nos devuelve los elementos 'truthy', es decir, aquellos que, convertidos a Booleanos,
# devuelven True
if __name__ == "__main__":
input_example = ["plopezpe", "pedro", "plopezpe@redhat.com", "Toshiba"]
# user_id:name:email:hardware
res1 = collect_account_ids_from_arns(input_example)
# res2 = collect_account_ids_from_arns_comp(input_example)
# print(res1, res2)