Search code examples
pythonjupyter-notebookmodularization

Jupyter Notebook version of previous Stackoverflow Python Question - Best/Cleanest way to define Python Constants


I am writing a Python script in Jupyter notebook to run 20+ long SQL queries. I have defined the SQL query strings in a separate file queryStrings.ipynb and the main body of code is in file analytics2020.ipynb.

This old StackOverflow post describes a nice clean way to define lists of constants ion a sepertate file (see the last answer ... the one from Ned Batchelder)

python-best-cleanest-way-to-define-constant-lists-or-dictionarys

However this does not appear to work in Jupyter Notebook. I have created the two seperate files

  1. queryStrings.ipynb

    q_CurrWeekiOSDailySessionCountDuration = '''
    with session_boundaries as (
    SELECT
        e.cust_id_attr_value
       ,e.event_timestamp
       ,DATEDIFF(minutes, LAG(e.event_timestamp) OVER(PARTITION BY e.cust_id_attr_value ORDER BY e.event_timestamp), e.event_timestamp) AS inactivity_time
       ,LAG(e.event_timestamp) OVER(PARTITION BY e.cust_id_attr_value ORDER BY e.event_timestamp) as prior_event_timestamp
    FROM
       APPLICATIONDB e
    WHERE
       event_data:"c-platform-m-os" = 'iOS' AND 
       event_timestamp BETWEEN \'{:s}\' AND \'{:s}\'
    )
    select 
        session_date,
        sum(num_sessions) as total_sessions,
    
     etc. etc. 
     ''' 
    
  2. analytics2020.ipynb

    import pandas as pd
    
    
    
    import numpy as np
    
    from queryStrings import q_CurrWeekiOSDailySessionCountDuration
    
    print('===== q_CurrWeekiOSDailySessionCountDuration ====')
    
    print(q_CurrWeekiOSDailySessionCountDuration)
    

However, when I try running this I get an error:

26 from queryStrings import q_CurrWeekiOSDailySessionCountDuration
     27 print('===== q_CurrWeekiOSDailySessionCountDuration ====')
     28 print(q_CurrWeekiOSDailySessionCountDuration)

ModuleNotFoundError: No module named 'queryStrings'

The previous post I quoted however tells me this ought to work. Perhaps it's I have a hunch that this is because these files are Jupyter Notebook .ipynb files rather than plain vanilla .py files.

Would appreciate any help resolving this! Thanks so much.


Solution

  • I did some research found a way to do this in Jupyter using the %store class.

    So in queryStrings.ipynb I added the line:

    %store q_CurrWeekiOSDailySessionCountDuration
    

    Then in analytics2020.ipynb I added then line

    %store -r q_CurrWeekiOSDailySessionCountDuration
    

    And then presto! It worked.