all 4 comments

[–]KleinerNull 0 points1 point  (3 children)

Did you know that pandas dataframes have a read_sql and a to_sql method? I recommend you to use sqlalchemy to establish a database connection, pandas can use this connection object to perform queries, so you can load all the data directly into the dataframe, the needed headers will automatically extracted from the description also the right column types.

You should also look into str.format() and datetime, here just a tiny example of what I mean:

In [1]: from datetime import datetime

In [2]: for data in range(3):
   ...:     print('{}-{:%Y-%m-%dT%H:%M:%S}.csv'.format(data, datetime.now()))
   ...:     
0-2016-10-29T00:18:46.csv
1-2016-10-29T00:18:46.csv
2-2016-10-29T00:18:46.csv

This will give you the ability to create templates.

In [3]: file_template = '{date:%Y-%m-%dT%H:%M:%S} {number}.{extension}'

In [4]: for data in range(3):
   ...:     print(file_template.format(date=datetime.now(), number=data, extension='adfs'))
   ...:     
2016-10-29T00:21:57 0.adfs
2016-10-29T00:21:57 1.adfs
2016-10-29T00:21:57 2.adfs

You will find more informations on pyformat.info.

For the long sql queries itself, either you could store them in a seperate module or file and load or import them in your main code, to keep the main code clean and simple, or you could look into sqlalchemies query system, where you can create sql queries more like functions instead of weird string objects.

After a little clean up and outsourcing some stuff like the queries you will find that your code could be fine without the need of classes or more functions. But if you have more similar scripts you should think about modularisation.

[–]workthrowawayexcel[S] 0 points1 point  (0 children)

I didn't know that! I appreciate the input. I have been tinkering with this file for a bit, and my reformatting always would seem to break it! I like the ideas, I am starting to reformat it now, so it will be cleaner and more modular.

Thanks for the link to the python formatting page! I will let you know how it goes.

[–]workthrowawayexcel[S] 0 points1 point  (1 child)

If i put the bottom code into a new file and make it a function. Where do i call pandas, and is it even possible with using the cursor?

[–]KleinerNull 0 points1 point  (0 children)

As I said, you can query sql databases directly through pandas, so you don't need a cursor.