0% found this document useful (0 votes)
8 views

Python Code

Python code for Students

Uploaded by

hariom_jadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Code

Python code for Students

Uploaded by

hariom_jadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

**************************************************************************

qry = f"""
SELECT {DBNAME}.prediction.id FROM {DBNAME}.dashboard_predictions as prediction
join {DBNAME}.dashboard_stores as store
on prediction.store_id = store.id
where
DAYNAME(prediction.tsforecast) = 'Saturday'
AND prediction.tsforecast >= '2021-01-16'
AND prediction.prediction_value != 0
AND store.client_id = 1;
"""

cur.execute(qry)

all_ids = ",".join([str(id[0]) for id in cur.fetchall()])

******************************************************************************
file_name = "This is a file"
dt = datetime.date.today()
date = dt.strftime("%Y-%m-%d")
print(date)
print(file_name)
qry = """INSERT INTO prediction_record_file(file_name, file_received_date)
VALUES('%s', '%s')""" %(file_name, date)
print(qry)
cur.execute(qry)
conn.commit()
print("Data inserted in table")

*********************************************************************************
import pandas as pd

data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}

#load data into a DataFrame object:


df = pd.DataFrame(data)
if ~df.empty:
print(df)
print("hello")
********************************************************************************
yesterday = datetime.date.today() - datetime.timedelta(days=1)
date = yesterday.strftime("%Y-%m-%d") # date = "2021-10-27"
file_name = f'greenyard/qa/output/{date}.csv' #previous-day date file
print(file_name)
**************************************************************************
predi_file = f"predictions_{date}.csv"
qry = f""" SELECT -----------------------------------
"""
cur.execute(qry)
data = pd.DataFrame(cur.fetchall(), columns=['Forecasted date', 'Article category', 'Article
family', 'Article subfamily', 'Article name', 'Store id', 'Store name', 'Client', 'Unit', 'product id',
'Prediction', 'Prediction made on'])

s3 = boto3.resource('s3',
aws_access_key_id = ACCESS_ID,
aws_secret_access_key= ACCESS_KEY)
try:
filePath = s3.Object(BUCKET_NAME,
f"{client_folder_name}/{ENVIRONMENT}/predictions/{predi_file}")
csv_buffer = StringIO()
data.to_csv(csv_buffer, index=False)
filePath.put(Body=csv_buffer.getvalue())
except botocore.exceptions.ClientError as e:
print(f"ERROR in prediction file: {e}")

*******************************************************************************************
df = pd.read_csv(file_name,
names=["IPER","REP","SR","FAM","ART","ETI","DES_ART","DES_ETI","DATA","T_MOV","
Q","PV"],
skiprows=1, sep=';', encoding= 'unicode_escape')
try:
cur.execute(f"""TRUNCATE {DBNAME}.staging_sales_table""")
print("\tTable staging_sales_table is truncated")
sqlEngine = create_engine(
"mysql+pymysql://{user}:{pw}@foresightee-dashboard-qa-instance-
1.cac11n2f7bfz.eu-central-1.rds.amazonaws.com/{db}"
.format(user=USR, pw=PASS, db=DBNAME)
)
df.to_sql('staging_sales_table', con=sqlEngine, if_exists='append', chunksize=10000,
index=False)
print("\tData was inserted into staging_sales_table")

except Exception as e:
print("Exit: Database connection failed due to {}".format(e))
return
**********************************************************************************************
def main():
file_names = ['VENDITE_2016.CSV', 'VENDITE_2017.CSV', 'VENDITE_2018.CSV',
'VENDITE_2019.CSV', 'VENDITE_2020.CSV']

for file in file_names:


print("\n* * * Start processing file : %s * * * " %file)
read_promo_file(file)
print("\tEND: (data uploading for file [%s] in done : ***)\n" %file)

if __name__ == '__main__':
if str(input("\nDo you want to proceed to databaes: [%s]?\n \t\ty/n: " %DBNAME)).lower()
== 'y':
main()
else:
print("Exit with NO operation.")

*******************************************************************

file_path = 'greenyard/qa/output/2021-12-14.csv'

date = file_path.split("/")[-1].split(".csv")[0]
print("Prediction file date: ", date)
*********************************************************************************************************
#**********To upload all same type excel files of a folder in database single table***********
def read_mail_file():
sqlEngine = create_engine(
"mysql+pymysql://{user}:{pw}@foresightee-dashboard-qa-instance-
1.cac11n2f7bfz.eu-central-1.rds.amazonaws.com/{db}"
.format(user=USR, pw=PASS, db=DBNAME)
)

files = [f for f in listdir('C:/hariom/mail_file') if isfile(join('C:/hariom/mail_file', f))]


for file in files:
file = 'C:/hariom/mail_file' + '/' + file
print("file :", file)
df = pd.read_excel(file, sheet_name = 0,
names=["DeliveryDate","ClientId","NameLongClient","ArticleId","NameLongArticle","UomSal
es","QtyColli","QtyPieces","QtyWeight","QtyDelivered"],
skiprows=1 )
try:
df.to_sql('staging_16_12_21', con=sqlEngine, if_exists='append', chunksize=10000,
index=False)
print("\tData was inserted ")
except Exception as e:
print("Exit: Database connection failed due to {}".format(e))
return
print("Done ")
************************************************************************************

db_data = cur.fetchall()
print(db_data)
for row in db_data:
data = str(row).strip(".0,()")
print("row[0]:", data)
******************************************************************************
To append a dictionary in lit
with open(self.recepient_file) as f:
lines = f.readlines()
f.close()
records = []
for line in lines:
record = {'name': line.split(",")[0].strip(), 'email': line.split(",")[1].strip()}
records.append(record)
print(“Records :- , records)

Or

with open(self.recepient_file) as f:
lines = f.readlines()
records = [{'name': line.split(",")[0].strip(), 'email': line.split(",")
[1].strip()}for line in lines]
f.close()

—-----------------------------------------------------------------------------------------------

To create a .exe file of python code


Link:- https://www.geeksforgeeks.org/convert-python-script-to-exe-file/

1) Run pip install pyinstaller


2) Go to python code directory and open power shell window by shift+right click mouse
3) Run pyinstaller --onefile -w 'filename.py'
4) Go in dist folder and find your “filename.exe” file

—-------------------------------------------------------------------------------------------------

To read .csv file from S3 bucket without to data frame or pandas

import os
import glob
import boto3
import pandas as pd
from io import BytesIO

s3_client = boto3.client('s3')
bucket_name = "dnb-info"
s3_file_path = "dev/batch-algo/requests/test-2022-06-02-17-54-28.152515.csv"
resp = s3_client.get_object(Bucket=bucket_name, Key=s3_file_path)
with open('f.csv', 'wb') as f:
f.write(BytesIO(resp['Body'].read()).getbuffer())

print("done")

pd.read_csv('f.csv')

***************************************************************************
try this first..(To create new branch and delete old one main branch then create new on and
pull

git reset .
git checkout .

If not working then

1)
git checkout -b tmp
or
git checkout tmp

2)
git branch -D master

3) git fetch

4) git checkout master

5)
git pull origin master

You might also like