Path: csiph.com!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.038 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'defaults': 0.05; 'subject:How': 0.09; 'to)': 0.09; 'variables,': 0.09; 'def': 0.13; 'variables': 0.15; '__init__.py': 0.16; 'alchemy': 0.16; 'conn': 0.16; 'conn.close()': 0.16; 'function).': 0.16; 'one)': 0.16; 'subject:variable': 0.16; 'creates': 0.18; 'load': 0.20; 'all,': 0.20; 'not,': 0.22; 'arguments': 0.22; 'skip:c 70': 0.22; 'bit': 0.23; 'help.': 0.23; '(like': 0.23; 'tried': 0.24; 'import': 0.24; 'module': 0.25; "i've": 0.25; 'skip:" 20': 0.26; 'appreciated.': 0.27; 'earlier': 0.27; 'defining': 0.27; 'function': 0.28; 'looks': 0.29; 'way?': 0.29; 'excel': 0.29; 'objects': 0.29; 'allows': 0.30; "i'm": 0.30; 'connection': 0.30; 'checks': 0.30; 'connections': 0.30; "can't": 0.32; 'skip:. 10': 0.32; 'problem': 0.33; 'message-id:@gmail.com': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'clear': 0.35; 'fresh': 0.35; 'tasks': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'project': 0.36; 'instead': 0.36; 'created': 0.36; 'closing': 0.36; 'loaded': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'say': 0.37; 'thanks': 0.37; 'charset:us-ascii': 0.37; 'things': 0.38; 'files': 0.38; 'mean': 0.38; 'data': 0.39; 'sure': 0.39; 'subject:the': 0.39; 'enough': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; 'some': 0.40; 'your': 0.60; 'hope': 0.61; 'provide': 0.61; 'within': 0.64; 'goal': 0.64; 'experience.': 0.66; 'reuse': 0.66; 'situation': 0.67; 'skip:* 70': 0.70; 'received:109': 0.75; 'lack': 0.76; 'background:': 0.81; '^_^': 0.84; 'please)': 0.84; 'subject:value': 0.84; 'shadow': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:mime-version:content-type :content-transfer-encoding:thread-index:content-language; bh=CSaZDdTFlCku5EQfzLJpMMtlzvNJoGY85YBoc0U8vOo=; b=UZcx0uKyG2hUPkP1BFIDr8lIcqIJEYK3rb9ovwzpwjlK5Lqk20vr9jAXERDvgtd39w D7Ss2NDdtxgIfNoPi8bC6OOPCL4KUstIxg3GvRLpeW1Uf4cj5jPzvBcI5BqIcH8xfyGk 2EEGnSN6sZsuWlADfM8NflmRpBOYOGE1xd2T3jYprHo28MpKTD9+Q787qUhtZztRo8rY 7WncaLgShw3KS4MkyxgDFwwx0pZZNNGkSW/lcBXZBKJneSjkamsuybxBL1GMYgHPBk/H +EQubzJlfE0+WwdPs3aGm578Qc4AC3XEX03Q0yDwaQXqLBTAZvseXkr3hY4sYUUlZKVT jouw== X-Received: by 10.195.11.37 with SMTP id ef5mr6418561wjd.60.1440699961909; Thu, 27 Aug 2015 11:26:01 -0700 (PDT) From: "Ivan Evstegneev" To: Subject: How to reassign the value of the variable on runtime? Date: Thu, 27 Aug 2015 21:25:56 +0300 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AdDg8KjQVTk/5lifQmiOCyAkgL5+Jg== Content-Language: en-us X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 115 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1440699970 news.xs4all.nl 23839 [2001:888:2000:d::a6]:51381 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95704 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.