-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.py
46 lines (35 loc) · 1020 Bytes
/
python.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
# Current date and time
import datetime
datetime.datetime.now().isoformat()
# Max int
import sys
sys.maxint # Python 2
sys.maxsize # Python 3
# Generate Exception
raise Exception('Describe exception')
raise SpecificException('Specific exception')
# Call parent class's method
class Foo(Bar):
def baz(self, arg):
return super(Foo, self).baz(arg) # Python 2
return super().baz(arg) # Python 3
# Pandas remove columns
df = df.drop(columns=['B', 'C'])
# Pandas apply by row
df.apply(lambda x: foo(x['a'], x['b'], x['c']), axis=1)
# Find all occurrences of a substring
import re
[m.start() for m in re.finditer('test', 'test test test test')]
# Regex replace
re.sub('(\d)\.(\d)', '\\1_\\2', '12. 12.23') # > '12. 12_23'
# List of files
import glob
print(glob.glob("/path/to/files/*.txt"))
# Jupyter - Progress bar
from tqdm import tqdm
for i in tqdm(range(10000)):
...
# Check string is number
str.isdigit()
# Sorting array of dict
sorted(arr, key=lambda x: x['field'], reverse=False)