python2.7 - How to create non-fixed classes using Python?
世界只因有你
世界只因有你 2017-06-12 09:22:44
0
4
815

New to python, I can’t find what I want to search for. For example, read data from a json configuration file, then create class objects based on the data, and then call the methods of these objects. The data of this json configuration file can be set freely. There may be a set of data corresponding to an object of a class. There may also be three sets of data corresponding to three classes of objects.
How to write code to read this json data and create an object of the class?
For example
Class targetobject(object):

def __init__(attr1,attr2):

def func1(self):
...

And our json data is... "object1":{"attr1":"dawdad","attr2":"dwagfreg"},"object2":{"attr1":"45324523", "attr2":"45634"},

As for the actual data, it may not be called object1 object2, it may be another name. And after I want to read this json file, create an object of the class and assign it to the data in json

世界只因有你
世界只因有你

reply all(4)
扔个三星炸死你

The poster wants to create objects dynamically. Can be created with exec

    a = {"object1": {"attr1": "dawdad", "attr2": "dwagfreg"}, "object2": {"attr1": "45324523", "attr2": "45634"}}
    for obj_name in a:
        exec ('class %s(object):pass' % (obj_name))
        for attr in a[obj_name]:
            exec ('%s.%s="%s"' % (obj_name, attr, a[obj_name][attr]))
    print(object1.attr1)

output dawdad

You can also use type to create

class targetobject(object):
    def __init__(self, attr1, attr2):
        self.attr1 = attr1

    def func1(self):
        pass


if __name__ == '__main__':
    a = {"object1": {"attr1": "dawdad", "attr2": "dwagfreg"}, "object2": {"attr1": "45324523", "attr2": "45634"}}
    for obj_name in a:
        obj = type(str(obj_name), (targetobject,), a[obj_name])
        print(obj)
        print(obj.attr1)
滿天的星座

When creating a class in Python, it is not necessary to provide all members (that is to say, all Python objects are not fixed)

eg.

class A:
    pass
a.b = 1             # 可以通过
a.__dict__['c'] = 2 # 使用__dict__获取数据成员
print(a.b) # 1
print(a.c) # 2

So just read Json and add the corresponding data members in Json to any object

为情所困

It sounds like the poster wants this kind of structure:

# coding: utf-8

from collections import namedtuple


obj = {"object1":{"attr1":"dawdad","attr2":"dwagfreg"}, "object2":{"attr1":"45324523","attr2":"45634"}}

obj_insts = []
for obj_name in obj:
    obj_attr = obj[obj_name]
    attrs = obj_attr.keys()
    attr_values = obj_attr.values()
    Obj = namedtuple(obj_name, attrs)
    obj_insts.append(Obj(*attr_values))

print(obj_insts)
print(ibj_insts[0].attr1)
[object1(attr2='dwagfreg', attr1='dawdad'), object2(attr2='45634', attr1='45324523')]
dawdad


If it is a configuration file, it is recommended to use toml (https://github.com/toml-lang/...
Install python toml and you can use it.

伊谢尔伦

Actually, I think you want to turn the jsondata in the file into an operable object, just like a dictionary... Let’s see if the jsonlibrary meets the requirements:

cat 1.txt:
{"object1":{"attr1":"dawdad","attr2":"dwagfreg"},"object2":"attr1":"45324523","attr2":"45634"}}

# python交互shell
>>> import json
>>> a = json.load(open('1.txt'))
>>> a
{u'object1': {u'attr2': u'dwagfreg', u'attr1': u'dawdad'}, u'object2': {u'attr2': u'45634', u'attr1': u'45324523'}}
>>> a['object1']
{u'attr2': u'dwagfreg', u'attr1': u'dawdad'}
 
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template