-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython.txt
More file actions
162 lines (102 loc) · 3.27 KB
/
Python.txt
File metadata and controls
162 lines (102 loc) · 3.27 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
GET ENVIRONMENT VARIABLE
VERSION
python --version
---------------------
conda env list
conda env create -f rait_env.yml
conda activate rait_env
-----------------
import os
os.getenv('AzureStorageConnectionString')
-------------------------
import sys
print(sys.version_info)
-------------------------
INSTALLED PACKAGES WITH VERSIONS
pip list
-------------------------------------------
GET PATH
python -c "import sys; print('\n'.join(sys.path))"
-----------------------------
MORE VERBOSITY
.vscode => launch.json => "preLaunchTask" => (Add --verbose) => "func: host start --verbose"
------------------
TRY CATCH
import sys
try:
except:
logging.error("Error: " + str(sys.exc_info()[0]))
-----------------------------------------
DATE TIME
import datetime
x = datetime.datetime.now()
'{:02d}'.format(date.day) + "_" + '{:02d}'.format(date.month) + "_" + '{:04d}'.format(date.year)
----------------
BLOB STORAGE
Terminal => pip install azure-storage-blob
requirements.txt
azure-functions
azure-storage-blob
import os, uuid
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
blobStorageConnectionString = os.getenv('AzureStorageConnectionString')
blob_service_client = BlobServiceClient.from_connection_string(blobStorageConnectionString)
#Blob Container Instance
blobContainerName = os.getenv('ContainerName')
blob_container_client = blob_service_client.get_container_client(blobContainerName)
if blob_container_client.exists() == False:
blob_container_client.create_container()
blob = BlobClient.from_connection_string(conn_str=blobStorageConnectionString, container_name=blobContainerName, blob_name=blobName)
#if we run this function more than once a day
if blob.exists() == True:
blob.delete_blob()
with open(fileName, "rb") as data:
blob.upload_blob(data)
----------------------
CREATE FILE AND READ CONTENT
file = open(fileName, 'w')
file.write("Test => Workgroup => " + workGroup)
file.close()
with open(fileName, "rb") as data:
Do something with the data
-----------------------
KEY VAULT
pip install azure-identity
pip install azure-keyvault-secrets
KVUri = os.getenv('KeyVault_Service_URL')
azureIdentityCredential = DefaultAzureCredential()
KeyVaultClient = SecretClient(vault_url=KVUri, credential=azureIdentityCredential)
-----------------------
ETL
with open(path,"r") as f:
text = f.readline().strip() #Strips removes things like new line etc
data = [int(i) for i in text.split(",")] #creates a list of integers, instead of a list of strings
return data
---------------------
LIST COMPRENHENSION
data = [int(i) for i in text.split(",")] #creates a list of integers, instead of a list of strings
data = [i + 1 for i in data] //sums 1 to each value in the data array
------------------
CSV WRITER
def load(data, path):
import csv
with open(path, "w") as f:
csv_writer = csv.writer(f)
csv_writer.writerow(data)
return
---------------
PREFECT
from prefect import task, Flow
@task
def load(data, path):
return
with Flow("nameOfTheFlow") as flow:
flow.visualize()
flow.run()
---------------
PREFECT PARAMETERS
from prefect import task, Flow, Parameters
inside the flow builder
path = Parameter(name="path",required=True)
flow.run(parameters={"path":"xx.csv"})
---------------------------