How to share a dictionary between scripts

Asked

Viewed 148 times

0

I have a central dictionary that gets all jsons files from my working directory:

def loaddata():
with os.scandir('Jsons/') as it:
    for entry in it:
        if entry.is_file() and entry.name.endswith('.json'):
            pass
        with open(entry.path, 'r') as f:
            key = entry.name[:-5]
            value = f.read()
            center_dict[key] = json.loads(value)

I need this dictionary to be accessible to all the other ".py files" I have in the directory. I can import functions between these scripts normally with:

import funcao from arquivo.

Yet when I try to do the same for that dictionary

ImportError: cannot import name 'dict_teste' from 'script'.

What I’m doing for now is calling loaddata() for each of the scripts however this takes too long and ends the dynamism of the application. Do you know any way to turn this dictionary into something that can be accessed for all scripts or some other way to deal with the problem ?

****** Edit ******

This central dictionary is an instruction dictionary. It needs to be read by each of the scripts, however it will not be modified at any time.

  • I believe you have to create a function that takes the dictionary as parameter. Then you call this function in the desired python file

  • some idea of a function that takes this dictionary from my main script and turns it into a global variable from my secondary script ?

  • Will these scripts run at the same time? If so, should the changes made to the dictionary by one reflect on the others? And if two scripts change the same position at the same time?

  • Yeah, one script calls the other. I do not change the dictionary at any time in the code however each code needs a specific part of that dictionary. For example my main.py needs the dict_central['main'], my second.py needs the dict_central['second'] ... Your third question I didn’t understand.

1 answer

1


I solved the problem by creating a new script called jsonmanager.py:

import json
import os

center_dict = {}

def loaddata():
    with os.scandir('Jsons/') as it:
        for entry in it:
            if entry.is_file() and entry.name.endswith('.json'):
                pass
            with open(entry.path, 'r') as f:
                key = entry.name[:-5]
                value = f.read()
                center_dict[key] = json.loads(value)

In my main code I added:

from jsonmanager import loaddata

loaddata()

And in all my other secondary scripts all I need to do is a simple import:

from jsonmanager import center_dict
  • 1

    As you’re already calling loaddata() in the body of the module json_manager.py, means it will already run automatically in the first from jsonmanager import center_dict found. Then you don’t need to add a call to loaddata() in your main script; it will be running loaddata() twice.

  • I made this correction in my script but went unnoticed here, thanks for the notification. I will edit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.