PrettyPrinted / youtube_video_code

The code for all the YouTube videos I publish on YouTube.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: type object argument after ** must be a mapping, not str

blobscy247 opened this issue · comments

I followed the Youtube tutorial and for some reason I keep coming up with the following error

TypeError: type object argument after ** must be a mapping, not str

I used a different JSON generator but everything worked until I tried to loop through the Users and append their details to the user_list. The code is below for reference, any help on this would be much appreciated:

import os

os.chdir(r'C:\Users\robbi\Desktop\Coding\My Python Scripts\APIs')

class User:
    def __init__(self, gender, name, location, email, login, dob, registered, phone, cell, id, picture, nat):
        self.gender = gender
        self.title = name['title']
        self.first_name = name['first']
        self.last_name = name['last']
        self.location = location['city']
        self.email = email
        self.username = login['username']
        self.password = login['password']
        self.dob = dob['date']
        self.age = dob['age']
        self.registered = registered['date']
        self.phone = phone
        self.cell = cell
        self.id = id['value']
        self.picture = picture['large']
        self.nat = nat
    
    @classmethod
    def from_json(cls, json_string):
        json_dict = json.loads(json_string)
        return cls(**json_dict)

    def __repr__(self):
        return f'<User { self.first_name }>'

json_string = '''{
         "gender":"female",
         "name":{
            "title":"Miss",
            "first":"Maeva",
            "last":"Gagnon"
         },
         "location":{
            "street":{
               "number":3400,
               "name":"3rd St"
            },
            "city":"Belmont",
            "state":"Nunavut",
            "country":"Canada",
            "postcode":"N5J 3K1",
            "coordinates":{
               "latitude":"-60.0772",
               "longitude":"148.5555"
            },
            "timezone":{
               "offset":"+11:00",
               "description":"Magadan, Solomon Islands, New Caledonia"
            }
         },
         "email":"maeva.gagnon@example.com",
         "login":{
            "uuid":"b51f5119-d542-4c59-8ed1-11d257a6dd1a",
            "username":"beautifulbird203",
            "password":"sinned",
            "salt":"vqkYxMd6",
            "md5":"f2f79890187d94eabbba380c59c29ca2",
            "sha1":"2ce9a4227ca1717b45a99cb1f12fdef57486258a",
            "sha256":"106b7af161a58f6c5f08d2c0ce337ff0f8d978816f09dcbef082efd11c71ac44"
         },
         "dob":{
            "date":"1989-09-17T06:18:26.692Z",
            "age":31
         },
         "registered":{
            "date":"2004-11-14T22:10:48.600Z",
            "age":16
         },
         "phone":"464-880-6563",
         "cell":"560-036-0580",
         "id":{
            "name":"",
            "value":null
         },
         "picture":{
            "large":"https://randomuser.me/api/portraits/women/32.jpg",
            "medium":"https://randomuser.me/api/portraits/med/women/32.jpg",
            "thumbnail":"https://randomuser.me/api/portraits/thumb/women/32.jpg"
         },
         "nat":"CA"
      }'''

#user = User.from_json(json_string)
#print(user)

users_list = []
with open('data.json', 'r') as json_file:
    user_data = json.loads(json_file.read())
    for u in user_data:
        users_list.append(User(**u))

print(users_list)

I have the same problem, did you find any answer?