-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.py
59 lines (50 loc) · 2.21 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
#--------------------------------------------------------------------------------------
# Regular Expressions
#--------------------------------------------------------------------------------------
import re
haystack = file.read()
needle = '<credentialsId>(.*)</credentialsId>'
ids = [matched.group(1) for matched in re.finditer(needle, haystack)]
newstring = re.sub('^[+\- |\\\]*needle', 'replacement', haystack)
#--------------------------------------------------------------------------------------
# Generate Random Strings
#--------------------------------------------------------------------------------------
import secrets
for x in range(15):
print(f"random hex string : {secrets.token_hex(16)}")
#--------------------------------------------------------------------------------------
# Date and Time manipulation
#--------------------------------------------------------------------------------------
import datetime
today_str = '21-02-2020 10:01'
today = datetime.datetime.strptime(today_str, '%d-%m-%Y %H:%M')
now = datetime.datetime.now()
now_pretty = now.strftime("%d-%m-%Y (%H:%M:%S.%f)")
import time
current_time_millis = int(round(time.time() * 1000))
#--------------------------------------------------------------------------------------
# JSON
#--------------------------------------------------------------------------------------
import json
json.dumps(response, indent=4, sort_keys=True)
json.dumps(json.loads(response.text), indent=4, sort_keys=True)
#--------------------------------------------------------------------------------------
# Decorator
#--------------------------------------------------------------------------------------
def deco(original_function):
def wrapper():
print("Code Executing Before")
original_function()
print("Code Executing After")
return wrapper()
@deco
def foo():
print("This is a decorated function")
foo()
import os
#--------------------------------------------------------------------------------------
# Read secret from home .file
#--------------------------------------------------------------------------------------
def get_pinboard_token():
secrets_dir = os.getenv('SECRETS_FILES')
return open(f"{secrets_dir}\pinboard.token", 'r').read()