Eldar88 Eldar88 - 6 months ago 13
JSON Question

Pass multiple variables to a function

I'm working on a project where I read a JSON file and retrieve different information associated with an image I'm working on.

I'm stuck on how to pass to my

json-read
function multiple variables and use them to move into my JSON file and retrieve the value that I need (the code works only when I pass 3 variables).

This is a part of the code:

class board(object):
def __init__(self, json, image):
self.json = json
self.image = image


def extract_json(one,two,three):
with open('document.json') as data_file:
data = json.load(data_file)
return data[one][two][three]


def crop_img(coords):
im = Image.open('./tabellone.jpg')
cropped_im = im.crop(coords)
return cropped_im

score_home_coords = extract_json("boxes", "score_home", "coord")
score_home_crop = crop_img(score_home_coords)
score_home_crop.save("./images/score_home_cropped.jpg")
path1 = "./images/score_home_cropped.jpg"
command = "ssocr-2.16.3/ssocr -d -1 -b black -l red %s" % path1
score_home = int(subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read())

MyAgonism = board('document.json', './tabellone.jpg')
print "Score Home: %s" % MyAgonism.score_home


I have created an instance of the class MyAgonism, to which I pass the json file name along with the image file name I have to work on.

init receives them, but how to use them inside the 2 class methods? I would like to do something like this:

def extract_json(one,two,three):
with open(SELF.JSON) as data_file:
data = json.load(data_file)
return data[one][two][three]


def crop_img(coords):
im = Image.open(SELF.IMAGE)
cropped_im = im.crop(coords)
return cropped_im


JSON file sample:

{
"background": "black",
"foreground": "red",
"boxes": {
"time_minutes": {"foreground": "red", "type" : "7segments" , "coord": [267,132,315,182]},
"time_seconds": {"foreground": "red", "type" : "7segments" , "coord": [327,132,389,182]},
"score_home" : {"foreground": "red", "type": "7segments", "coord": [204,51,276,101]},
...


Thanks in advance for your help.




I need something like this:

def extract_json(self, *args):
with open('document.json') as data_file:
data = json.load(data_file)
return data[args]


but all the args are displayed in this way:


(args1, args2, args3, args4)


and i need instead


[args1][args2][args3][args4]


to correctly access to my json file with the right keys




Fixed, the method was useless. I just used:

with open(self.json_path) as f:
self.d = json.load(f)

Answer

You had just forgotten to put self as first argument in both your class methods:

def extract_json(self, one, two, three):
    with open(self.json) as data_file:
        ...

  def crop_img(self, coords):
    im = Image.open(self.image)
    ...

Simplified explanation:

  • Methods of a class which need to use some variables passed during the instanciation (instance specific values) must have self as first argument. These are called instance methods.
  • Methods of a class which do not need to access the instance attributes, but need to have access to class attributes or methods, need to have cls as first argument. These are called class methods.
  • Methods of a class which do not need to access self and only perform some logic and return a value, do not need self, nor cls. These are called static methods.

Some more explanations here