1

I am writing simple application which has json file for store some user's information.

{
    "users": [
        {
            "last result": 15,
            "login": "admin",
            "id": 1,
            "password": "1"
        },
        {
            "last result": 2,
            "login": "user",
            "id": 2,
            "password": "1"
        }
    ]
}

For example I need to change value "last result" from "user" = admin I have method for take data from json:

def load_data_from_json(test_file_name):
    with open(test_file_name, encoding="utf-8") as data_file:
        return json.load(data_file)

test_file_name - json's name (in my code it's - "auth.json")

Then I'm trying to set new value:

def set_last_result(login, new_result):
    for user in load_data_from_json("auth.json")["users"]:
        if user["login"] == login:
            user["last result"] = new_result
    raise Exception("User '{}' not found.".format(login))

But I have an error:

  File "/Users/future/PycharmProjects/module_for_test/user.py", line 53, in set_last_result
    raise Exception("User '{}' not found.".format(login))
Exception: User 'admin' not found.

If I just take the "last result"value

def get_last_result(login):
    for user in load_data_from_json("auth.json")["users"]:
        if user["login"] == login:
            return user["last result"]
    raise Exception("User '{}' not found.".format(login))

All work's well. Where is my mistake?

P.S. I'm using Python 3.4

P.S.S Fix my error message.

sapi
  • 9,944
  • 8
  • 41
  • 71
Anton Erjomin
  • 183
  • 1
  • 1
  • 9
  • I don't see any `print(user["admin"])` in your code, and more important I don't see any `"admin"` key in your user dictionary, that's where the `KeyError` probably comes from. – Rik Poggi Aug 08 '14 at 21:59
  • Users don't have 'admin' keys, they have 'login' keys. When you print `user['admin']` you generate a key error. – g.d.d.c Aug 08 '14 at 21:59
  • Sorry, I posted not correct error message, now i've fixed it. – Anton Erjomin Aug 08 '14 at 22:03
  • 3
    Your get method returns when it finds the user. The set method does not, thus it will always finish the for loop and then raise the exception. – Dunes Aug 08 '14 at 22:09
  • possible duplicate of [Write JSON data to file in python](http://stackoverflow.com/questions/12309269/write-json-data-to-file-in-python) – Aprillion Aug 08 '14 at 22:24

1 Answers1

0

To answer your concern about the exception:

as @Dunes said, if you don't want to raise the exception, you cannot allow the for loop to finish, so in your case you can return:

def set_last_result(login, new_result):
    for user in load_data_from_json("auth.json")["users"]:
        if user["login"] == login:
            user["last result"] = new_result
            return
    raise Exception("User '{}' not found.".format(login))

in a more general situation, not just inside a function, you can use for ... else:

def set_last_result(login, new_result):
    for user in load_data_from_json("auth.json")["users"]:
        if user["login"] == login:
            user["last result"] = new_result
            break
    else:  # will execute only if `for` loops through all values without break
        raise Exception("User '{}' not found.".format(login))

But there is also a different problem:

You are updating a local variable user which will have no effect whatsoever in the outer scope of the set_last_result function, most notably it won't update the loaded json file.

To do that you need to Write JSON data to file in python.

Community
  • 1
  • 1
Aprillion
  • 21,510
  • 5
  • 55
  • 89