From 3d824b3c2b0527b005d4117a9ed601206c167286 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Mon, 28 Aug 2023 11:58:38 +0200 Subject: [PATCH] initial --- .gitignore | 1 + export_month.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 10 ++++++++++ 3 files changed, 59 insertions(+) create mode 100644 .gitignore create mode 100644 export_month.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21d0b89 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.venv/ diff --git a/export_month.py b/export_month.py new file mode 100644 index 0000000..934a53c --- /dev/null +++ b/export_month.py @@ -0,0 +1,48 @@ +import psycopg2 +import sqlalchemy +import pandas as pd +import argparse +import sys +import datetime +import dateutil.relativedelta + + +parser = argparse.ArgumentParser(description="export_month.py") +parser.add_argument('--lastmonth', '-l', + help="Export data of the last month", + required=False, + action='store_true', + default=False) +parser.add_argument('--month', '-m', + help="Export data of the given month, format: YYYY-MM-DD", + required=False, + default='0000-00-00') + +args = parser.parse_args() +if not args.lastmonth and args.month == '0000-00-00': + raise Exception('Either --lastmonth or --month must be given') +if args.lastmonth and args.month != '0000-00-00': + raise Exception('Only one of --lastmonth and --month must be given') + +if args.lastmonth: + startdate = datetime.date.today().replace(day=1) - dateutil.relativedelta.relativedelta(months=1) +else: + startdate = datetime.datetime.strptime(args.month, '%Y-%m-%d').replace(day=1) + +enddate = startdate + dateutil.relativedelta.relativedelta(months=1) + + +try: + engine = sqlalchemy.create_engine('postgresql://wn@db.hottis.de/mainscnt') + with engine.connect() as conn: + dat = pd.read_sql_query(f"select time, location, freq from mainsfrequency where valid=1 and time >= '{startdate}' and time < '{enddate}'", con=conn) +finally: + engine.dispose() + + +dat = dat.pivot_table(index='time', columns='location', values='freq') + +csv_filename = f"{startdate.strftime('%Y-%m')}.csv" +dat.to_csv(csv_filename) + + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..639ab98 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,10 @@ +greenlet==2.0.2 +numpy==1.25.2 +pandas==2.0.3 +psycopg2==2.9.7 +python-dateutil==2.8.2 +pytz==2023.3 +six==1.16.0 +SQLAlchemy==2.0.20 +typing_extensions==4.7.1 +tzdata==2023.3