Search code examples
pythonjupyter-notebookpycharmsteam

How do I make code save local variables between its processes in python like ipython notebook does?


I think my title isn't clear so... I made this code which fetches top Dota TV games as an array of these match_ids and prints them in the end. STEAM_LGN, STEAM_PSW are steam login/password combination.

from steam.client import SteamClient
from dota2.client import Dota2Client

client = SteamClient()
dota = Dota2Client(client)

@client.on('logged_on')
def start_dota():
    dota.launch()

match_ids = []
@dota.on('top_source_tv_games')
def response(result):
    for match in result.game_list: # games
        match_ids.append(match.match_id)  
    
def request_matches():
    dota.request_top_source_tv_games()

client.cli_login(username=STEAM_LGN, password=STEAM_PSW)

request_matches()
dota.on('top_source_tv_games', response)
print(match_ids)

The thing I'm having a problem with

When using Anaconda iPython Notebook -> when I run the cell for the first time -> it returns me

[]

but when I do it for the second time, it returns me a real data, for example

[5769568657, 5769554974, 5769555609, 5769572298, 5769543230, 5769561446, 5769562113, 5769552763, 5769550735, 5769563870]

So every time when I am playing in my ipython notebook sandbox -> I hit Shift+Enter twice and get the data.

But now I need to tranfer this code to a bigger project. So for example let's say I save that code to dota2.info.py file and have this code in another file referencing dota2.info.py:

import subprocess
import time 

### some code ###

while(True):
    subprocess.call(['python', './dota2info.py'])
    time.sleep(10)

And when running project code this always prints [] like it was doing on first Shift+Enter cell-running in Anaconda ipython notebook.

[]
[] 
[]
...

So my question is what should I do in this situation ? How can I solve this problem of (I don't know) ValvePython/dota2 code caching some important data in local unknown to me variables in ipython notebook?

Ideally I want the code immediately give me real data without having these [].


Solution

  • Hard to tell why it happens, but as a possible workaround I'd try wrapping the code in the cell you're re-running, in a function that retries until nonempty results are achieved.

    For example, assuming everything was in the rerun cell except the imports, this might be dota2info.py:

    from steam.client import SteamClient
    from dota2.client import Dota2Client
    import time
    
    def get_results():
        client = SteamClient()
        dota = Dota2Client(client)
    
        @client.on('logged_on')
        def start_dota():
            dota.launch()
    
        match_ids = []
        @dota.on('top_source_tv_games')
        def response(result):
            for match in result.game_list: # games
                match_ids.append(match.match_id)  
            
        def request_matches():
            dota.request_top_source_tv_games()
    
        client.cli_login(username=STEAM_LGN, password=STEAM_PSW)
    
        request_matches()
        dota.on('top_source_tv_games', response)
        return match_ids
    
    if __name__ == "__main__":
        results = get_results()
        max_retries = 10
        retries = 0
        while not results and retries < max_retries:
            time.sleep(3)  # wait number of seconds to retry
            results = get_results()
            retries += 1
    
        print(results)