How to convert dictionary list to custom objects in Python

erondem

I have a list which has some dictionaries to be bulk uploaded. I need to check if elements of list can be cast to a custom object(as below). Is there any elegant way to do it like type comparison?

This is my model

class CheckModel(object):

    def __init__(self,SerialNumber,UID, Guid = None,Date = None):
        self.SerialNumber = SerialNumber
        self.UID = UID
        self.Guid = str(uuid.uuid4()) if Guid is None else Guid
        self.Date = datetime.now().isoformat() if Date is None else Date

And this is my test data. How can I cast only first element(because first element is the only correct one.) of this list into CheckModel object?

test = [{
        "Guid":"d0c035a7-0e01-4a37-8fe9-251fb5633fc9",
        "SerialNumber":"1716154A",
        "UID":"F13BDB3B",
        "Date":"2019-12-03T13:50:19.882Z"

    },
    {
        "Guid":"d0585-0e01-4a47-8fe9-251245f33fc9",
        "SerialNumber":"1716154A",
        "Date":"2019-12-03T13:50:19.882Z"
    },
    {
        "Guid":"12414a7-0e01-4a47-8fe9-251245f33fc9",
        "SerialNumber":"1716154A",
        "UID":"F13BDB3B",
        "Date":"2019-12-03"
    }]
Rakesh

You can create a custom cleanup function and then use filter

Ex:

import datetime
import uuid

class CheckModel(object):

    def __init__(self,SerialNumber,UID, Guid = None,Date = None):
        self.SerialNumber = SerialNumber
        self.UID = UID
        self.Guid = str(uuid.uuid4()) if Guid is None else Guid
        self.Date = datetime.datetime.now().isoformat() if Date is None else Date

#Clean Up Function.             
def clean_data(data):
    if all(key in data for key in ("Guid", "SerialNumber", "UID", "Date")):
        try:
            datetime.datetime.strptime(data["Date"], "%Y-%m-%dT%H:%M:%S.%fZ")
            return True
        except:
            pass
    return False 


test = [{
        "Guid":"d0c035a7-0e01-4a37-8fe9-251fb5633fc9",
        "SerialNumber":"1716154A",
        "UID":"F13BDB3B",
        "Date":"2019-12-03T13:50:19.882Z"

    },
    {
        "Guid":"d0585-0e01-4a47-8fe9-251245f33fc9",
        "SerialNumber":"1716154A",
        "Date":"2019-12-03T13:50:19.882Z"
    },
    {
        "Guid":"12414a7-0e01-4a47-8fe9-251245f33fc9",
        "SerialNumber":"1716154A",
        "UID":"F13BDB3B",
        "Date":"2019-12-03"
    }]

model_data = [CheckModel(**data) for data in filter(clean_data, test)]
print(model_data)

Output:

[<__main__.CheckModel object at 0x0000000002F0AFD0>]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python: filtering a list of custom objects, by one of its dictionary attributes

python convert list to dictionary

Convert a list to a dictionary in Python

How to convert this list into a dictionary

How to convert a list of tuples to a dictionary of dictionaries in Python?

How to convert python list to python dictionary with sequence

How to convert list into dictionary

How to convert String values in a dictionary to a list in python?

How to convert list of string to dictionary in python

How to convert list of string to list of dictionary in Python

How to convert list to dictionary in python with key value?

how to convert list of dictionary into string python

How to convert a list of dictionary values containing Timesamp objects into datetime objects in Python?

How to access a dictionary value inside a list of custom objects?

python dictionary and list: how to convert it?

Convert list of lists to custom dictionary

How convert list of dictionaries to a dictionary of dictionaries in Python

Convert a dictionary to a list - Python

How to convert list of list to dictionary in Python?

Python 3 - How to convert a string to a list of dictionary

How to convert unciode to list of dictionary in python

How to convert dictionary list?

How to convert list of list to list of objects in python?

How do I convert list into dictionary in Python?

How to convert list to dictionary

how to convert a list to dictionary in python?

How to convert dictionary to pandas DataFrame in Python when dictionary value is a List?

How to convert every dictionary in a list to a nested dictionary in python?

How to convert list to dictionary in Python?