Home > Software design >  How do you transfer the information of a dictionary from 1 python file to another and then back?
How do you transfer the information of a dictionary from 1 python file to another and then back?

Time:01-24

So here's the rundown of what I'm trying to do. It's long but I'm trying to be as specific as possible.

I am making a text adventure game with python. The problem was that the file got too big and I needed to create sister files for the different definitions because the editing started to get sluggish.

for the main file. When I run the main file, it grabs the definition just fine. But the sister file spits an error that it can't read the needed dictionary in the code. So I tried to just copy the dictionaries from the main file to the sister file. It works, but since the stats of everything in the dictionaries don't transfer from file to file and back, I tried this.

file 1:

import os,sys,ast
from file2 import *
player = {'name' : 'player',
          'hp' : 10}
enemy = {'name' : 'enemy2',
          'damage' : 5}
attack()

file 2:

import os,sys,ast
player = {'name' : 'player',
          'hp' : 10}
enemy = {'name' : 'enemy2',
          'damage' : 5}
with open("file1.py") as f:
    data = f.read(player)
    dictionary = ast.literal_eval(data)

def attack():
    player['hp'] = player['hp'] - enemy['damage']

I keep getting this error:

TypeError: argument should be integer or None, not 'dict'

But when I try to convert it to an integer:

File 2:

import os,sys,ast
enemy1 = {'name' : enemy1,
          'damage' : 10}
enemy2 = {'name' : enemy2,
          'damage' : 5}
with open('file.py') as f:
    data = f.read(int(str(player)))
    dictionary = ast.literal_eval(data)

def attack():
    player['hp'] = player['hp'] - enemy['damage']

I get:

ValueError: invalid literal for int() with base 10: '{}'

Because of the non-integer values.

I am trying to only load the one specific dictionary to the file, not all the rest of them at once. The reason I can't just have all of the exact dictionaries in the 2 files is that the definitions change the values in the dictionaries. Note that I am very limited in my knowledge and am mostly self taught through myself, sites like this and geeks for geeks.

I hope that this explanation of my problem covers what I'm trying to ask.

CodePudding user response:

Edit

Like the commenters suggest, rewrite your attack function to accept arguments instead of using globals.

# File 2
def attack(player, enemy):
    player['hp'] = player['hp'] - enemy['damage']

# File 1
…
attack(player, enemy)

CodePudding user response:

So I have finally found the answer to my question.

What you would do (while still using globals) is load the specific dictionary into a text file:

file1:

    import os,sys,ast

    dictionary = {...}

    from file2 import function
    filehandler = open("file.txt", "a")
    data = str(dictionary)
    filehandler.write(data)
    filehandler.close()
    
    function()
    
    with open('file.txt') as f:
        data = f.read()
        dictionary = ast.literal_eval(data)
        os.remove("file.txt")

file2:

    import sys,os,ast
    
    dictionary = {...}

    def function():
        with open('file.txt') as f:
            data = f.read()
            dictionary = ast.literal_eval(data)
            os.remove("file.txt")
        ...
        filehandler = open("file.txt", "a")
        data = str(dictionary)
        filehandler.write(data)
        filehandler.close()

Here's an explanation on what happens.

In file 1, it grabs every value from 'dictionary', creates a new text file, and writes it into the text file. It then calls the function from file 2.

The function in file2 then grabs the text file and reads it. It grabs the dictionary information from the file, loads it into it own dictionary, and deletes the text file so it doesn't take up space.

After the function is done, it grabs the new values in the dictionary, creates a new text file, and writes it into the text file. Then file 1 (since the function is done) grabs the text file, reads it, grabs the information and replaces the old dictionary information with the new information, and deletes the text file.

Note that as far as I know, you need to do this creating text files and reading them for every different dictionary. So I would suggest using a function that does this for every dictionary that you need transfer over.

The whole reason for this is because the way my function works, it requires to use multiple different dictionaries, and sometimes uses different amounts of dictionaries.

So for anyone has the same problem as me, do what my answer is. But I would suggest writing the function to accept arguments.

  •  Tags:  
  • Related