-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.py
More file actions
146 lines (120 loc) · 3.7 KB
/
text.py
File metadata and controls
146 lines (120 loc) · 3.7 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import hashlib
import re
from typing import Optional, List, Any
from python_utils.types_hinting import NumberIFD
def format_percent(value: Optional[NumberIFD]):
"""
Get value and return a % repr
Args:
value: value to be used for % repr
Returns:
A % repr of the passed value
Examples:
>>> format_percent(13)
'1300%'
>>> format_percent(0.13)
'13.0%'
>>> from decimal import Decimal
>>> format_percent(Decimal('0.123456'))
'12.35%'
"""
return f"{round(value * 100, 2)}%"
def format_currency(value: Optional[NumberIFD], symbol: str) -> Optional[str]:
"""
Get value and number and return a currency representation
Args:
value:
symbol: string representing the symbol of currency
Returns:
string representing currency and value
Examples:
>>> format_currency(120.1234, 'EUR')
'EUR120.12'
>>> from decimal import Decimal
>>> format_currency(Decimal('120.1234'), 'EUR')
'EUR120.12'
>>> format_currency(None, 'EUR') is None
True
"""
if not value:
return None
return f"{symbol}{round(value, 2):,}"
def human_string(text: str) -> str:
"""
Transform text to human string
Args:
text: string to be converted
Returns:
converted string
Examples:
>>> human_string('test_str')
'Test Str'
>>> human_string("")
''
"""
if not text:
return ""
return " ".join(word.title() for word in text.split("_"))
camel_case_pattern = re.compile(r"(?<!^)(?=[A-Z])")
def camel_case_to_snake_case(text: str) -> str:
"""
Convert camel case string to snake case, Substitute spaces with _
Args:
text: string to be converted
Returns:
converted string
Examples:
>>> camel_case_to_snake_case('TestStrHello')
'test_str_hello'
>>> camel_case_to_snake_case('Test Str Hello dummy CAPITAL12')
'test__str__hello__dummy__c_a_p_i_t_a_l12'
"""
return camel_case_pattern.sub("_", text.replace(" ", "_")).lower()
# Indicates 1 or more spaces
spaces_pattern = re.compile(r" +")
def human_string_to_snake_case(text: str) -> str:
"""
Transform text from human string to snake case
Args:
text: string to be converted
Returns:
converted string
Examples:
>>> human_string_to_snake_case('Test Str TEST')
'test_str_test'
"""
return spaces_pattern.sub("_", text.strip().lower())
def extract_with_pattern(text: str, pattern: str) -> Optional[str]:
"""
Return the first found result of a given pattern in the string or None if not found
E.g: ('Test _t Test _t', '_t') -> '_t'
Args:
text: string to find the match
pattern: pattern against which the string is trying to match
Returns:
First result of given pattern or None
Examples:
>>> extract_with_pattern('Test _t Test _t', '_t')
'_t'
>>> extract_with_pattern('Test _t Test _t', 'A') is None
True
>>> extract_with_pattern('Test 123', '\\d+')
'123'
"""
compiled_pattern = re.compile(pattern)
results = compiled_pattern.search(text)
if results:
return results.group(0)
def create_hash(data: List[Any]) -> str:
"""
Given a list of params calculate a hash based on their values
Args:
data: list of params to be used for the hash
Returns:
calculated hash value
Examples:
>>> create_hash(['test', 'hash', 1])
'5ca1b365312e58a36bb985fc770a490b'
"""
merged_data = "-".join([str(rec) for rec in data])
return hashlib.md5(merged_data.encode("utf-8")).hexdigest()