Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) created a q5011_2t adder specifically for waves 1 and 2 #5

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions add_q5011_2t_w1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import json
import pandas as pd
import psycopg2
import zipfile
from datetime import datetime

from glob import glob


class IVDate1Updater:
IVDATE1_DATETIME_FORMAT = "%d.%m.%Y %H:%M:%S" # 02.05.2022 15:16:18
ISO_DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" # 2022-05-02 15:04:09

def __init__(self):
self.config = self.get_config()

def get_config(self):
with open("config.json", "r", encoding="utf-8") as input_file:
return json.load(input_file)

def get_dataframe(self, file_name):
df = pd.read_excel(file_name)
df = df.astype({"IVDate1": "str"})

return df

def is_month_incorrect(self, dataframe):
first_row = dataframe.iloc[0]
ivdate = first_row["IVDate1"]
ivdate = datetime.strptime(ivdate, self.IVDATE1_DATETIME_FORMAT)

for _, row in dataframe.iterrows():
recruiting_date = row["IVDate1"]


def update_table(self, dataframe, month_is_incorrect):

with psycopg2.connect(
host=self.config["db_host"],
dbname=self.config["db_name"],
user=self.config["db_user"],
password=self.config["db_password"],
) as conn:
with conn.cursor() as cur:
for _, row in dataframe.iterrows():
ivdate1 = row["IVDate1"]
ivdate1 = datetime.strptime(ivdate1, self.IVDATE1_DATETIME_FORMAT)
ivdate1 = datetime.strftime(ivdate1, self.ISO_DATETIME_FORMAT)

query_parameters = {
"id": row["ID"],
"q5011_2t": ivdate1,
}
cur.execute(
"""
UPDATE recruits_log
SET q5011_2t = %(q5011_2t)s
WHERE id = %(id)s;
""",
query_parameters,
)
conn.commit()

def make_fixed_recruiting_date(self, recruiting_date):
month = recruiting_date[5:7]
new_month = self.get_new_month(month)

rd_year = recruiting_date[:4]
rd_day_and_time = recruiting_date[8:]

new_recruiting_date = f"{rd_year}-{new_month}-{rd_day_and_time}"

return new_recruiting_date

def get_new_month(self, month):
month = int(month) + 1
month = f"{month:02d}"

return month

def run(self):
for file_name in glob("./xlsx/*.zip"):
print(file_name[7:])
with zipfile.ZipFile(file_name, "r") as zip:
with zip.open(zip.namelist()[0]) as excel_file:
dataframe = self.get_dataframe(excel_file)
month_is_incorrect = self.is_month_incorrect(dataframe)
self.update_table(dataframe, month_is_incorrect)

def run_excel(self):
for file_name in glob("./xlsx/*.xlsx"):
print(file_name[7:])
dataframe = self.get_dataframe(file_name)
month_is_incorrect = self.is_month_incorrect(dataframe)
self.update_table(dataframe, False)


if __name__ == "__main__":
u = IVDate1Updater()
u.run_excel()

# u.run()