1

I have set up a flask application and use data downloaded from a mysql db as pandas dataframe. The df ist create by a function and not by a class. There I created a POST Call to reload data which is used for the algorithms.

I want to know how I can rerun the import of df_leitdaten. If I add del df_leitdaten and import again. I do not get the result I wished.

Initialy I import a module which extracts and combine data from the db via

from address_validation_api.etl.db_call import df_leitdaten

In the Script I am doing:

df_a= pd.read_sql(""" SELECT * FROM tab_a""",  open_connection_mysql())
df_b= pd.read_sql(""" SELECT * FROM tab_b""",  open_connection_mysql())
df_c= pd.read_sql(""" SELECT * FROM tab_c""",  open_connection_mysql())

df_d = df_a.append([df_b, df_c])
df_leitdaten = df_d.drop_duplicates(subset=['x', 'y', 'z']
                             keep = 'last')

The POST call exceutes reload_leitdata() and has the functionality:

def initial_leitdata():
    return (df_leitdaten.shape, 
            hashlib.sha256(pd.util.hash_pandas_object(df_leitdaten,
 index=True).values).hexdigest())    

def reload_leitdata():
    before = initial_leitdata()
    LOG.info('Shape of df_before: %s', str(before[0]))
    LOG.info('Hash of df_before: %s', str(before[1]))
    del df_leitdaten
    from address_validation_api.etl.db_call import df_leitdaten
    after = (df_leitdaten.shape,
             hashlib.sha256(pd.util.hash_pandas_object(df_leitdaten,
             index=True).values).hexdigest())
    LOG.info('Shape of df_after: %s', str(after[0]))
    LOG.info('Hash of df_after: %s', str(after[1]))
    status = False
    if not before[1] == after[1]:
        status = True
    return (before, after, status)

I want to show the difference in the shape of the df. I added some data to the db and run the script but the shape of the df does not change. Even if I deployed the API there is no change. My desiered output is something like

{'status':True,
 'Shape of df_before:', (100,2)
 'Hash of df_before:', 'abc',
 'Shape of df_after:', (101,2)
 'Hash of df_after:', 'abcd'}
René
  • 79
  • 8
  • if you want ot reimport module then you have somthing wrong with your code. Normally modules are imported only once and then you run many times functions from this module. If in module you have code which is not in function then you should put it in function and then you don't have to reimport it but run this function again. – furas Aug 12 '19 at 12:24
  • Does ist mean, when I set up my functionality as class then I can initialize the class again and run the function? – René Aug 12 '19 at 12:25
  • Possible duplicate of [How do I unload (reload) a Python module?](https://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module) – Diptangsu Goswami Aug 12 '19 at 12:39
  • Take a look at [this](https://docs.python.org/2.7/library/functions.html#reload). – Diptangsu Goswami Aug 12 '19 at 12:40
  • In that example the import might be a module like pandas or numpy. In my case it is a function in a seperated script. – René Aug 12 '19 at 12:40
  • @René that it means that you have to put ALL codes in functions - if you do somethink outside functions and it is executed when you import modules then you can't run it again without reloading module. If you put this in functions and run it in module then you still can rerun this function without reloading module. Better show what you have in imported module. – furas Aug 12 '19 at 12:55

0 Answers0