osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to reassign the value of the variable on runtime?


Hello all, 

I have stuck with some issue and can't solve it myself, your help will be
highly appreciated.

A bit of background: 
In my project I work with MySQL DB and SQLAlchemy. One of its tasks is to
load data from excel files into database.
To achieve this, I use some function that checks whether my db has been
created earlier (if not, it creates a new one) and then establishes a
connection to it.

This function returns "db_engine_object" and "db_meta_object" variables,
this allows me to reuse them when I work with other functions, instead of
closing and reopening connections all the time.

SO:
I have package named "db_control", it consists of number ofmodules, where
one of them is named "alchemy_db_export".

db_control/
	__init__.py
	alchemy_data_export.py
	#other modules

This module looks like this (a relevant part of it):

*********************CODE(alchemy_data_export.py****************************
******************
from .alchemy_db_config import db_existence_check_and_create
from ini_data_builder.main_builder import *
from .db_table_names import *

engine_object, meta_object = db_existence_check_and_create()

def current_data_export(engine=engine_object, meta=meta_object):
    system_data = load_current_system_data()
    conn = engine.connect()
    for table_name in ordered_tables:
 
conn.execute(meta.tables[table_name].insert().values(system_data[table_name]
))
    conn.close()
    print("System's Data successfully loaded into Database!")
.
.
.
.
Some other functions that use "engine_object " and "meta_object" as defaults
too..
.
.
.
**********************************************************************

This one enables me to make such things:

>>>from control_db import alchemy_data_export as alchemy
>>>alchemy.current_data_export()

I don't need to provide any arguments to "current_data_export()" just use a
defaults  (Yep me is lazy ^_^)

BUT the problem begins when I want to create a fresh copy of database
(using, say " create_new_db_()" function).

Can some please (I mean very please) explain me how do I reassign
"engine_object" and "meta_object" variables, 
so they would store(point to) a new connection objects of my database,
while other functions still would see those variables as their defaults? 

I've tried couple of things, like defining these variables globally within
this module (like that way):

****************alchemy_data_export.py**************************************
from .alchemy_db_config import db_existence_check_and_create
from ini_data_builder.main_builder import *
from .db_table_names import *

engine_object, meta_object = db_existence_check_and_create()

def db_new_copy():
	engine_object, meta_object = create_new_db()

def current_data_export(engine=engine_object, meta=meta_object):
    system_data = load_current_system_data()
    conn = engine.connect()
    for table_name in ordered_tables:
 
conn.execute(meta.tables[table_name].insert().values(system_data[table_name]
))
    conn.close()
    print("System's Data successfully loaded into Database!")
**************************************************************************

But in this case I have some warning about "shadowing" 
I can know that I shadow outerscope variables but how can I achieve my goal
other way?
Tried __init__.py  and other 10-20 things but still stuck with it, I'm sure
this is because the lack of experience.


Hope my situation is clear enough ^_^ Tried my best. 

Again thanks a lot for a help.

Ivan.