I wrote two scripts in Python:
The first appends to a list the key values of a dictionary.
The second one uses that list to create columns on a MySQL database.
Originally I wrote them in the same module. However, is it better if I separate them in two different modules or keep them together?
If it's better separating them, what is a pythonic way to use lists from one file in another? I know importing variables is not recommended.
Here is the code:
import pymysql
# This script first extract dictionary key values. Then, it creates columns using the keys values from the dictionaries.
# login into mysql
conn = pymysql.connect("localhost", "*****", "******", "tutorial")
# creating a cursor object
c = conn.cursor()
# update here table from mysql
table_name = "table01"
# data
dicts = {'id': 5141, 'product_id': 193, 'price_ex_tax': 90.0000, 'wrapping_cost_tax': 0.0000, 'type': 'physical', 'ebay_item_id': 444, 'option_set_id': 38, 'total_inc_tax': 198.0000, 'quantity': 2, 'price_inc_tax': 99.0000, 'cost_price_ex_tax': 0.0000, 'name': 'UQ Bachelor Graduation Gown Set', 'configurable_fields': [], 'base_cost_price': 0.0000, 'fixed_shipping_cost': 0.0000, 'wrapping_message': '', 'order_address_id': 964, 'total_ex_tax': 180.0000, 'refund_amount': 0.0000, 'event_name': None, 'cost_price_inc_tax': 0.0000, 'cost_price_tax': 0.0000, 'wrapping_cost_inc_tax': 0.0000, 'wrapping_name': '', 'price_tax': 9.0000, 'is_bundled_product ': False, 'ebay_transaction_id': 4444, 'bin_picking_number': 4444, 'parent_order_product_id': None, 'event_date': '', 'total_tax': 18.0000, 'wrapping_cost_ex_tax': 0.0000, 'base_total': 198.0000, 'product_options': [{'id': 4208, 'display_name': 'Gown size (based on height)', 'name': 'Bachelor gown size', 'display_value': 'L (175-182cm)', 'display_style': 'Pick list', 'type': 'Product list', 'option_id': 19, 'value': 77, 'product_option_id': 175, 'order_product_id': 5141}, {'id': 4209, 'display_name': 'Hood', 'name': 'H-QLD-BAC-STD', 'display_value': 'UQ Bachelor Hood', 'display_style': 'Pick list', 'type': 'Product list', 'option_id': 42, 'value': 119, 'product_option_id': 176, 'order_product_id': 5141}, {'id': 4210, 'display_name': 'Trencher size (based on head circumference)', 'name': 'Trencher size', 'display_value': 'M (53-54cm)', 'display_style': 'Pick list', 'type': 'Product list', 'option_id': 20, 'value': 81, 'product_option_id': 177, 'order_product_id': 5141}], 'base_price': 99.0000, 'sku': 'S-QLD-BAC-STD', 'return_id': 0, 'applied_discounts': [{'id': 'coupon', 'amount': 30}], 'quantity_shipped': 0, 'base_wrapping_cost': 0.0000, 'is_refunded': False, 'weight': 2.0000, 'order_id': 615496} # noqa
# creating empty lists
int_keys_lists = []
str_keys_lists = []
list_keys_lists = []
def extractDictKeys():
# for loop that runs through the dictionary, extracting the keys when their valures are int or str, and appending to the corresponding list
for i, j in enumerate(dicts):
k, v = list(dicts.items())[i]
if type(dicts[j]) != str:
int_keys_lists.append(k)
else:
str_keys_lists.append(k)
def createColumnStrKeys():
# for loop that create a column for each str key on the list
for i, j in enumerate(str_keys_lists):
c.execute("ALTER TABLE {0} ADD COLUMN {1} VARCHAR(255)".format(table_name, str_keys_lists[i]))
conn.commit()
def createColumnIntKeys():
# for loop that create a column for each id or float key on the list
for i, j in enumerate(int_keys_lists):
c.execute("ALTER TABLE {0} ADD COLUMN {1} int(30)".format(table_name, int_keys_lists[i]))
conn.commit()
extractDictKeys()
createColumnStrKeys()
createColumnIntKeys()