私のコードがclean_up()
メソッドでスタックしますMyClass()
my_class.py:
import os
import pandas as pd
import psycopg2, pymysql, pyodbc
from db_credentials_dict import db_credentials
class MyClass():
def __init__(self, from_database, to_table_name, report_name):
...
def get_sql(self):
...
def get_connection(self):
...
def query_to_csv(self):
...
def csv_to_postgres(self):
...
def extract_and_load(self):
self.query_to_csv()
self.csv_to_postgres()
def get_final_sql(self):
...
def postgres_to_csv(self):
...
def clean_up(self):
print('\nTruncating {}...'.format(self.to_table_name), end='')
with self.postgres_connection.cursor() as cursor:
cursor.execute("SELECT NOT EXISTS (SELECT 1 FROM %s)" % self.to_table_name)
empty = cursor.fetchone()[0]
if not empty:
cursor.execute("TRUNCATE TABLE %s" % self.to_table_name)
self.postgres_connection.commit()
print('DONE')
print('Removing {}...'.format(self.filename), end='')
if os.path.exists(self.filepath):
os.remove(self.filepath)
print('DONE')
else:
print('{} does not exist'.format(self.filename))
main.py:
from my_class import MyClass
from time import sleep
bookings = MyClass(from_database='...',to_table_name='...',report_name='...')
bookings2 = MyClass(from_database='...',to_table_name='...',report_name='...')
channel = MyClass(from_database='...',to_table_name='...',report_name='...')
cost = MyClass(from_database='...',to_table_name='...',report_name='...')
tables = [bookings, bookings2, channel, cost]
for table in tables:
table.extract_and_load()
daily_report = MyClass(from_database='...',to_table_name='...',report_name='...')
daily_report.postgres_to_csv()
sleep(10)
for table in tables:
table.clean_up()
私が実行するmain.py
と、最後のループまですべてが実行されますfor table in tables: table.clean_up()
。エラーや警告は発生せず、そこでスタックするだけです。
そのメソッドを独自に実行すると、正常に機能します。つまり、postgresテーブルが切り捨てられます。これを機能させ、他のすべてのメソッドが実行されたときに最終メソッドが実行されない理由を理解するのに助けが必要です。
clean_up()
単独で実行した場合の出力:
Truncating bookings...DONE
Removing bookings.csv...DONE
Truncating bookings2...DONE
Removing bookings2.csv...DONE
Truncating channel...DONE
Removing channel.csv...DONE
Truncating cost...DONE
Removing cost.csv...DONE
コード全体の機能:
- さまざまなデータベースからデータを抽出してそれらのクエリを実行するSQLクエリを含むファイルを取得します。
- それらをcsvにエクスポートする
- csvをpostgresデータベースにインポートする
- データをまとめて、データを視覚化するためのBIツールで使用するcsvとしてエクスポートするpostgresクエリを作成する
- postgresのデータを切り捨て、ポイント2でcsvファイルを削除する
あなたはおそらくこれが狂気だと思っているでしょうし、私も同意します。私は現在、私が持っているもので支払いを行っていますが、会社のデータ(したがって、データの切り捨てと削除)のため、自分のコンピューターにデータを保存できません。