-2
import random
import os
import json

class User:
    def _init_(self, username, password, balance):
        self.username = username
        self.password = password
        self.balance = balance



def file_read(source):
    with open (source) as file:
        data = file.read()
        dictionary = json.loads(data)
        return dictionary

and then the external file is this

{"John":["pass123", 2000], "Jenson": ["pass123", 2000]}

my initial thought was to use a for items in dict but i am unsure how to create multiple objects from that preferably being named by username thank you.

J.Rimmer
  • 27
  • 6

4 Answers4

1

Probably what you are looking for. Remember it is __init__ and not _init_.

import json

def file_read(source):
    with open(source) as file:
        data = file.read()
        dictionary = json.loads(data)
        return dictionary

class User:
    def __init__(self, username, password, balance):
        self.username = username
        self.password = password
        self.balance = balance

    def __str__(self): # String representation of the object for the output.
        return f"{self.username}@{self.password} with balance of {self.balance}"

dictionary = file_read("file.json")

users = []
for key, item in dictionary.items():
    user = User(key, item[0], item[1])
    users.append(user)

# Printing results for output sake.
for user in users:
    print(user)

outputs:

John@pass123 with balance of 2000
Jenson@pass123 with balance of 2000

with file.json as:

{"John":["pass123", 2000], "Jenson": ["pass123", 2000]}
felipe
  • 7,324
  • 2
  • 28
  • 37
  • sorry im just starting out with oop i shall have at the look at the code you have sent and try and implement this thankyou – J.Rimmer Dec 04 '19 at 20:04
  • No need to be sorry -- we all started somewhere. The second to last `for-loop` is where we are iterating through the items of the dictionary through the use of [`dictionary.items()`](https://docs.python.org/3/tutorial/datastructures.html#looping-techniques), and on the line `user = User(key, item[0], item[1])` we are creating our user object. – felipe Dec 04 '19 at 20:49
1

Simple solution using a dict comprehension and var-args:

{ k: User(k, *v) for k, v in file_read(filename).items() }

Alternatively you can do it with destructuring:

{ k: User(k, pw, bal) for k, (pw, bal) in file_read(filename).items() }
kaya3
  • 47,440
  • 4
  • 68
  • 97
0

What i think you are asking is to create objects for each of the names in the file.

class User:
    def __init__(self, username, password, balance):
        self.username = username
        self.password = password
        self.balance = balance
def file_read(source):
    with open (source) as file:
       data = file.read()
       dictionary = json.loads(data)
       return dictionary
dicValuesRead = file_read(source)
d = {}
for k,v in dicValuesRead.items():
    d[k] = User(k, v[0], v[1])

print (d)
Yatish Kadam
  • 454
  • 2
  • 11
0

preferably being named by username

That is problematic, the most common solution for that part of your question is to create a dictionary of {username:object} pairs. See How do I create a variable number of variables?

Your function returns a dictionary.

  • iterate over the dictionary items which will produce (name,(password,balance)) tuples
  • for each item extract the individual values; name,(password,balance)= item
  • pass those to the class to make a new object and add it to the dictionary
  • new_dict[name] = object
wwii
  • 23,232
  • 7
  • 37
  • 77