-1

Im currently trying to build and app to manage budgets or expenses, im new to python and everything really, so im getting help from a friend, and im not quite familiarized with all the processes involved on coding. So, i created a class for users, another for loading a list, and other to writte(inject) the list on a json that was created in the user class. I hope i was clear explaining.

this is the user code:

import os  
import json



class Usuario:

        def __init__(self,nombre):
            self.nombre = nombre
            self.verify_json()
            print(f"Te damos la bienvenida {self.nombre}")

            
        def get_nombre(self):
            return self.nombre
        
        def save_json(self,nombre):
            with open('data/'+nombre +"_usuario.json","w") as f:
                json.dump(self.nombre,f,indent=4)

        def verify_json(self):
            if not os.path.exists('data/'+self.nombre + '_usuario.json'):
                self.save_json(self.nombre)
                                
            else:
                with open('data/'+self.nombre + '_usuario.json', 'r') as file:
                    return json.load(file)
                    
                    
                
        def get_json(self):
            dir = 'data/'+ self.nombre + '_usuario.json'
            with open(dir, 'r') as file:
                return json.load(file)

I have changed this few times, because when i was calling the json file from another script i was getting the error that it wasnt a str. Now, from here im calling it:

class w_jason():

    def __init__(self,archivo_json,lista_cuentas, nombre):
        self.nombre = nombre
        self.archivo_json = archivo_json
        self.lista_cuentas = lista_cuentas
        self.meses = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio',
                      'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre']
        
        self.archivo_user = self.leer_json()
        
    
    def listas_in_json(self):
        self.cuentas_dict = {self.lista_cuentas[i]: [] for i in range(len(self.lista_cuentas))}
        self.meses_dict = {self.meses[i]: self.cuentas_dict for i in range(len(self.meses))}
        return self.meses_dict
    
    def leer_json(self):
        with open(self.archivo_json, 'r') as file:
            return json.load(file)
       
    
    def edit_json(self):      
       with open(self.archivo_json, 'w') as file:
            json.dump(self.archivo_user, file, indent=4)
    

This class its receiving the path of the already created json archive, but im getting the error:


Traceback (most recent call last):
  File "/Users/jabac/Desktop/work/proyecto_cuenta/scripts/test.py", line 7, in <module>
    m_json = w_jason(mariana.get_json, marian_cuentas.get_lista_cuentas, mariana.get_nombre)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jabac/Desktop/work/proyecto_cuenta/scripts/write.py", line 19, in __init__
    self.archivo_user = self.leer_json()
                        ^^^^^^^^^^^^^^^^
  File "/Users/jabac/Desktop/work/proyecto_cuenta/scripts/write.py", line 28, in leer_json
    with open(self.archivo_json, 'r') as file:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected str, bytes or os.PathLike object, not method

So, I don't know what else to do. I was returning a str with the path of the file, but it was also giving me the same error, now I tried to just return the json file, and still get the error.

The app was working minutes ago, and I don't know what i did that changed that behavior.

I'm sorry if this was too long, and thanks in advance

1 Answer 1

0

I solved it, sorry guys, i wasnt putting the () after calling the method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.