3

以下を明確にするための努力に感謝します:Pythonで、複数のクラスが宣言されているクラスごとに1つのオブジェクトを動的に作成する方法はありますか? 私のばかげた推測は、次のように説明できます。

...

db からのデータがあるとします。

props = dict_cur.fetchall() 
classes_names = []
data = []

for i in props:
    classes_names.append(i['client_name'].title()) 

classes = []
data = []

for i in props:
    data.append(dict(i)) 

for i, d in zip(classes_names, data):
    classes.append(type(i, (object,), dict(**d)))

print classes
#printing list of classes

objects = []
for obj in classes:
objects.append(obj())

for obj in objects:
    print obj.client_name, obj.client_id

これは非常に素朴なアプローチであり、次のように、作成されたクラスから通常の方法で継承することはできません。

class ClientProcess(Someclient): #Someclient is the name of the created class before


    def __init__(self):
        print "Someclient stuff" 

目標は非常に単純です。できればテーブルに格納されているプロパティを使用して、いくつかのクラスのオブジェクトを作成しますが、同時に、クラスごとに異なる特定のメソッドが実装されるすべてのクライアントのクラス宣言を行います。うまく機能し、Factory メソッドの Python バージョンを使用する最初のスクリプトは、(クライアント ID であるコマンドライン引数に基づいて) 一度に 1 つのクラス (クライアント) しか処理できないため、十分ではありません。

4

3 に答える 3

0

collections.namedtuple. 終わり。編集:詳しく説明するには、

from collections import namedtuple
rows = dict_cur.fetchall() 
# creates the class Row which is a tuple, but each position argument
# corresponds to the column name in that position
# Row can be instantiated as a tuple and then its elements can be accessed
# by name class attributes 
Row = namedtuple("Row", zip(*dict_cur.description)[0])
objects = [Row(row) for row in rows]

for o in objects:
     print o.client_name, ' is ' , o
于 2015-10-13T00:17:29.793 に答える
0
class GetConfig(object):

    def __init__(self, client_id):
        self.client_id = client_id
        #construct the query here to get the clients data ...where client_id = self.client_id
        d = {'logfile': 'some_long_path', 'contact_name': 'some_name'}


    class FirstClient(object):

    def __init__(self):
        client_id = '111111111'
        props = GetConfig(client_id)
        #print props.d

    def check_source(self):
        print "Checking FirstClient source"
        return "Something"
        #print props.d

    def check_downl(self):
        print "Checking FirstClient downloaded"


class SecondClient(object):

    def __init__(self):
        client_id = "222222"
        props = GetConfig(client_id)


    def check_source(self):
        print "Checking SecondClient source"

    def check_downl(self):         
        print "Checking SecondClient downloaded"

myfactory = {
"firstclient" : FirstClient,
"secondclient" : SecondClient,
}

for i in myfactory.values():

    i().check_source()
    i().check_downl()
于 2012-10-25T20:30:21.913 に答える
0

私があなたを正しく理解していれば、次の方法を使用して、動的に作成されたクラスをサブクラス化できます。

In : classes = []

In : cls_name = 'BaseCls1'

In : classes.append(type(cls_name, (object, ), {'x': 1}))

In : classes[0].x
Out: 1

In : classes[0].__bases__
Out: (object,)

# two ways to create subclass out of BaseCls1

In : class SubCls1(classes[0]):
   :     x = 2
   :
In : SubCls1.x
Out: 2

In : SubCls1.__bases__
Out: (__main__.BaseCls1,)


In : SubCls2 = type('SubCls2', (classes[0],), {'x': 2})

In : SubCls2.x
Out: 2

In : SubCls2.__bases__
Out: (__main__.BaseCls1,)
于 2012-10-25T05:27:09.740 に答える