tradeup-package 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tradeup_package-0.1.1/LICENSE +21 -0
- tradeup_package-0.1.1/PKG-INFO +8 -0
- tradeup_package-0.1.1/README.md +20 -0
- tradeup_package-0.1.1/pyproject.toml +13 -0
- tradeup_package-0.1.1/setup.cfg +4 -0
- tradeup_package-0.1.1/tradeup/Public.py +193 -0
- tradeup_package-0.1.1/tradeup/__init__.py +1 -0
- tradeup_package-0.1.1/tradeup_package.egg-info/PKG-INFO +8 -0
- tradeup_package-0.1.1/tradeup_package.egg-info/SOURCES.txt +9 -0
- tradeup_package-0.1.1/tradeup_package.egg-info/dependency_links.txt +1 -0
- tradeup_package-0.1.1/tradeup_package.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Zachary Yang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: tradeup_package
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Author-email: Zachary Yang <zachary.yang@tradeup.com>
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
License-File: LICENSE
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
This package is designed for internal use only. Should you have any questions, please contact Zachary Yang via Teams or zachary.yang@tradeup.com
|
|
2
|
+
|
|
3
|
+
Release note 0.0.2 2023/10/18 13:55
|
|
4
|
+
Added Previous_BDay(), return previous business day
|
|
5
|
+
Added db_insert(), insert dataframes into database
|
|
6
|
+
Added data_pull, pull data from MySQL DB including original column names
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
Release note 0.0.3 2023/10/30 15:18
|
|
10
|
+
Added clear_table(), clear records based on customized conditions
|
|
11
|
+
Added last_business_day()
|
|
12
|
+
|
|
13
|
+
Release note 0.0.4 2023/11/09 15:37
|
|
14
|
+
Added to_float(), used mainly in BR parsing, e.g. change string '9,000,154.91-' to float -9000154.91
|
|
15
|
+
|
|
16
|
+
Release note 0.0.5 2023/11/29 16:43
|
|
17
|
+
Created function create_partition(), automatically create partition of next month for each table, with hard coded user name and password
|
|
18
|
+
|
|
19
|
+
Release note 0.1.1 2024/05/02 10:59
|
|
20
|
+
Defined all functions under class Amazon as static method, no need to include argument 'self' when calling functions
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools", "wheel"]
|
|
3
|
+
|
|
4
|
+
[project]
|
|
5
|
+
name = "tradeup_package"
|
|
6
|
+
version = "0.1.1"
|
|
7
|
+
authors = [{ name="Zachary Yang", email="zachary.yang@tradeup.com" }]
|
|
8
|
+
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Programming Language :: Python :: 3",
|
|
11
|
+
"License :: OSI Approved :: MIT License",
|
|
12
|
+
"Operating System :: OS Independent",
|
|
13
|
+
]
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import pymsteams
|
|
2
|
+
import awswrangler as wr
|
|
3
|
+
import pandas as pd
|
|
4
|
+
import numpy as np
|
|
5
|
+
import sqlalchemy
|
|
6
|
+
import datetime
|
|
7
|
+
import os
|
|
8
|
+
import ast
|
|
9
|
+
import boto3
|
|
10
|
+
from botocore.exceptions import ClientError
|
|
11
|
+
import redshift_connector
|
|
12
|
+
import pymysql
|
|
13
|
+
import calendar
|
|
14
|
+
from sqlalchemy import create_engine, MetaData, text
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def onError(title, destination, assignee, error_message, script_name, script_location):
|
|
18
|
+
myTeamsMessage = pymsteams.connectorcard(destination)
|
|
19
|
+
# create the section
|
|
20
|
+
myMessageSection = pymsteams.cardsection()
|
|
21
|
+
# Activity Elements
|
|
22
|
+
myMessageSection.activityTitle(title)
|
|
23
|
+
myMessageSection.activitySubtitle("production pipeline alert")
|
|
24
|
+
myMessageSection.activityImage("https://teamsnodesample.azurewebsites.net/static/img/image4.png")
|
|
25
|
+
# Facts are key value pairs displayed in a list.
|
|
26
|
+
myMessageSection.addFact("Assignning to", assignee)
|
|
27
|
+
myMessageSection.addFact("Error Message", error_message)
|
|
28
|
+
myMessageSection.addFact("Script Name", script_name)
|
|
29
|
+
myMessageSection.addFact("Script Location", script_location)
|
|
30
|
+
# Add your section to the connector card object before sending
|
|
31
|
+
myTeamsMessage.addSection(myMessageSection)
|
|
32
|
+
myTeamsMessage.summary(title)
|
|
33
|
+
myTeamsMessage.send()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def clear_table(date, table_name=None, db_name=None, user=None, password=None, IP_address=None, port=None):
|
|
38
|
+
db = pymysql.connect(host = IP_address, user = user, passwd = password, database = db_name, port = port, charset='utf8')
|
|
39
|
+
try:
|
|
40
|
+
cursor = db.cursor()
|
|
41
|
+
cursor.execute(f'''
|
|
42
|
+
delete FROM {table_name}
|
|
43
|
+
where Date = '{date}';
|
|
44
|
+
|
|
45
|
+
''')
|
|
46
|
+
db.commit()
|
|
47
|
+
print(f'clear table {table_name}: 1')
|
|
48
|
+
except Exception as e:
|
|
49
|
+
print(e)
|
|
50
|
+
finally:
|
|
51
|
+
db.close()
|
|
52
|
+
|
|
53
|
+
def insert_db(df=None, table_name=None, db_name=None, user=None, password=None, IP_address=None, port=None):
|
|
54
|
+
engine = create_engine(f"mysql+pymysql://{user}:{password}@{IP_address}:{port}/{db_name}")
|
|
55
|
+
try:
|
|
56
|
+
df.to_sql(name=table_name, con=engine, if_exists='append', index=False)
|
|
57
|
+
print(f'{table_name} ingestion: 1')
|
|
58
|
+
except Exception as e:
|
|
59
|
+
raise ValueError(e)
|
|
60
|
+
finally:
|
|
61
|
+
engine.dispose()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def last_business_day(year, month):
|
|
65
|
+
last_day = calendar.monthrange(year, month)[1]
|
|
66
|
+
day = datetime.date(year, month, last_day)
|
|
67
|
+
while day.weekday() > 4:
|
|
68
|
+
day -= datetime.timedelta(days=1)
|
|
69
|
+
return day
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def data_pull(user=None, password=None, IP_address=None, port=None, db_name=None, SQL_query=None):
|
|
73
|
+
try:
|
|
74
|
+
# Create the SQLAlchemy engine
|
|
75
|
+
engine = create_engine(f"mysql+pymysql://{user}:{password}@{IP_address}:{port}/{db_name}")
|
|
76
|
+
|
|
77
|
+
# Execute the SQL query
|
|
78
|
+
query = f"{SQL_query}"
|
|
79
|
+
df = pd.read_sql_query(query, engine)
|
|
80
|
+
return df
|
|
81
|
+
except Exception as e:
|
|
82
|
+
raise ValueError(e)
|
|
83
|
+
finally:
|
|
84
|
+
engine.dispose()
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def to_float(series):
|
|
90
|
+
new_lst = []
|
|
91
|
+
lst = list(series)
|
|
92
|
+
for i in lst:
|
|
93
|
+
if i == "" or type(i) == float:
|
|
94
|
+
new_lst.append(np.nan)
|
|
95
|
+
else:
|
|
96
|
+
if "-" in i:
|
|
97
|
+
i = i.replace(",","").replace("-","").replace(" ","")
|
|
98
|
+
i = float(i) * (-1)
|
|
99
|
+
new_lst.append(i)
|
|
100
|
+
else:
|
|
101
|
+
i = i.replace(",","").replace(" ","")
|
|
102
|
+
new_lst.append(float(i))
|
|
103
|
+
return new_lst
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class amazon():
|
|
111
|
+
@staticmethod
|
|
112
|
+
def get_secret(self, secret_name, region_name):
|
|
113
|
+
# Create a Secrets Manager client
|
|
114
|
+
session = boto3.session.Session()
|
|
115
|
+
client = session.client(service_name='secretsmanager',region_name=region_name)
|
|
116
|
+
try:
|
|
117
|
+
get_secret_value_response = client.get_secret_value(
|
|
118
|
+
SecretId=secret_name
|
|
119
|
+
)
|
|
120
|
+
except ClientError as e:
|
|
121
|
+
raise e
|
|
122
|
+
|
|
123
|
+
password = get_secret_value_response['SecretString']
|
|
124
|
+
return password
|
|
125
|
+
|
|
126
|
+
@staticmethod
|
|
127
|
+
def check_holiday(self, date_obj, con, which='NYSE_Holiday'):
|
|
128
|
+
date_str = date_obj.strftime("%Y-%m-%d")
|
|
129
|
+
df = wr.redshift.read_sql_query(
|
|
130
|
+
sql=f"""
|
|
131
|
+
SELECT "{which}" FROM Holiday.Holiday WHERE "Date" ='{date_str}'
|
|
132
|
+
""",
|
|
133
|
+
con=con
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
# not in holiday list
|
|
137
|
+
if len(df) == 0:
|
|
138
|
+
return False
|
|
139
|
+
|
|
140
|
+
# in holiday list and decide
|
|
141
|
+
if df.iloc[0,0] == 1:
|
|
142
|
+
return True
|
|
143
|
+
else:
|
|
144
|
+
return False
|
|
145
|
+
|
|
146
|
+
@staticmethod
|
|
147
|
+
def data_pull(self, sql_query, con, indicator):
|
|
148
|
+
data = wr.redshift.read_sql_query(
|
|
149
|
+
sql=sql_query,
|
|
150
|
+
con=con
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
print(indicator)
|
|
154
|
+
return data
|
|
155
|
+
|
|
156
|
+
@staticmethod
|
|
157
|
+
def sql_execute(self, sql_query, con, indicator):
|
|
158
|
+
cursor: redshift_connector.Cursor = con.cursor()
|
|
159
|
+
cursor.execute(sql_query)
|
|
160
|
+
|
|
161
|
+
print(indicator)
|
|
162
|
+
|
|
163
|
+
@staticmethod
|
|
164
|
+
def insert_table(self, db_params, data, con, method, mode, dtype, schema = None, table=None):
|
|
165
|
+
if schema is None:
|
|
166
|
+
schema = db_params['schema_name']
|
|
167
|
+
if table is None:
|
|
168
|
+
table=db_params['table_name']
|
|
169
|
+
if method == 'copy':
|
|
170
|
+
wr.redshift.copy(
|
|
171
|
+
df = data,
|
|
172
|
+
path =f's3://tradeup-it/parquet/{schema}/{table}',
|
|
173
|
+
schema=schema,
|
|
174
|
+
table=table,
|
|
175
|
+
con=con,
|
|
176
|
+
mode=mode, # Append, overwrite or upsert
|
|
177
|
+
use_column_names = True,
|
|
178
|
+
index=False,
|
|
179
|
+
dtype=dtype # Optional: specify column data types
|
|
180
|
+
)
|
|
181
|
+
print(f'insert: 1')
|
|
182
|
+
elif method == 'to_sql':
|
|
183
|
+
wr.redshift.to_sql(
|
|
184
|
+
df = data,
|
|
185
|
+
schema=schema,
|
|
186
|
+
table=table,
|
|
187
|
+
con=con,
|
|
188
|
+
mode=mode, # Append, overwrite or upsert
|
|
189
|
+
use_column_names = True,
|
|
190
|
+
index=False # Typically, you don't want DataFrame indexes in your database table
|
|
191
|
+
#dtype={"column1": "INTEGER", "column2": "VARCHAR(255)"} # Optional: specify column data types
|
|
192
|
+
)
|
|
193
|
+
print('insert: 1')
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .Public import *
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: tradeup_package
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Author-email: Zachary Yang <zachary.yang@tradeup.com>
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
License-File: LICENSE
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tradeup
|