0

I'm usually used to Laravel PHP where I am able to do things such as Auth::User()->name however I need to make a game in Python for my class.
I would like to be able to do Colours.black or something similar however I am struggling.

I have a Python file called Colours.py and it looks something like this:

import json


class Colours(object):

    def __init__(self):
        with open('app/colours.json') as data:
                self.__dict__ = json.loads(data)

My JSON file looks like this:

[
  {
    "black": "(0, 0, 0)",
    "white": "(255, 255, 255)",
    "green": "(0, 255, 0)"
  }
]

And then in another Python file called Main.py I have the following:

from app.models import Colours

print(dir(Colours))

This code does not work.

Edit: Sorry forgot to give the output:

['Colours', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'json']

Edit 2: I am able to do:

print(Colours.Colours().black)
## Output: (0, 0, 0)

However I was wondering whethere this is a way to do Just Colours.black?

Ethan
  • 53
  • 1
  • 11
  • Try to have more focused questions. What is the output you are expecting. What about this code doesn't satisfy your needs? – Filip Apr 30 '20 at 16:07
  • Try using `json.load(data)[0]` to extract the dictionary from the outer list. This will initialize your `__dict__` variable with the key-value pairs from the JSON. (Note: the JSON contains a list of length one, where the single element is the dictionary you want) – AbbeGijly Apr 30 '20 at 16:08
  • @AbbeGijly Still doesn't seem to work :( – Ethan Apr 30 '20 at 16:10
  • @Filip Is that better? ^^ – Ethan Apr 30 '20 at 16:11
  • Also replace `json.loads` with `json.load`. `.load()` takes a file handle; `.loads()` takes a string – AbbeGijly Apr 30 '20 at 16:11
  • @Ethan yeah that's better but are you sure you didn't delete any of the output? Why isn't the `__init__` method displayed? – Filip Apr 30 '20 at 16:14
  • I think you are printing the file and not the class. `Colours` is the file you imported and it can't show you the `__init__` method inside your class. Try `print(dir(Colours.Colours))` – Filip Apr 30 '20 at 16:19
  • @Filip This just prints `['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] ` – Ethan Apr 30 '20 at 16:23
  • @Ethan yeah try printing the `__dict__` method, I just helped you go to your class, because you were printing the available file magic methods – Filip Apr 30 '20 at 16:36
  • To print the instance's variables you should use `vars()` method instead of `dir()`. To know more about their differences: https://stackoverflow.com/questions/980249/difference-between-dir-and-vars-keys-in-python – revliscano Apr 30 '20 at 17:35

2 Answers2

0

You can solve it by creating the class as type with static variables so you won't need to initialize the class

Colours.py:

import json

with open('app/colours.json') as data:
   Colours = type("Colours", (), json.load(data)[0])
Aviv Cohen
  • 108
  • 6
  • Thank you! I had to modify your solution to look like this: `with open('app/colours.json') as data: colour = type("Colours", (), json.load(data))` So it made more sense grammatically. – Ethan Apr 30 '20 at 16:48
0

You are viewing the file's magic methods and not the class'.

First you need to import your Colours.py file and then run the __init__ method of your Colours class. Try creating an object to do that:

from app.models import Colours

obj = Colours.Colours()
print(obj.__dict__)

Colours.py

import json

class Colours(object):
    def __init__(self):
        with open("app/colours.json") as data:
            self.__dict__ = json.load(data)[0]

This code will print the dictionary in your __dict__ method.

To explain what is happening here. First you are importing your Colours.py file inside the app/models/ directories. Inside that file you have created a Colours class that you need to run it's __init__ magic method that will set the __dict__ method to the dictionary from you json file. So first you need to run that init method by running that class by creating an object from it. Then you just access that method and you print that dictionary.

Filip
  • 898
  • 1
  • 8
  • 24