将类实例组合到python中的另一个实例中?

时间:2014-04-21 05:59:28

标签: python join instance persistent chain

我在设计一个"拼图游戏"像工具一样管理不同的水管,就像零件组合一样有趣。

  1. 我有不同用途的不同单件类型(冷却器,加热器,净化器......)
  2. 具有不同接口尺寸的那些部件可以相互连接(1/4英寸,1/6英寸......)
  3. 我希望这些部件可以存储在数据库中,并且可以组合成一个全新的部件组合(随机或有意),但仍然可以考虑作为一个功能部件。

    这是最初的想法

    class MetaInfo():
        def __init__(self, name, intype,outtype,shape,serialno):
            this.name = name
            this.intype = intype
            this.outtype = outtype
            this.shape = shape
            this.sn = serialno
    
    
    def parts():
        def __init__(self, meta):
            this.meta = meta
    
    def linkwith(self, part):
        if part.meta.shape == this.meta.shape: 
        # make it simple, logical here is just same shape can be connected each other 
            return ???         # a new parts combination 
        else:
            raise Error 
    
    m1 = MetaInfo("cooler", "hotwater", "coldwater", "1/4 inch round", "SN:11111" )
    m2 = MetaInfo("heater", "coldwater", "hotwater", "1/4 inch round", "SN:22222" )
    m3 = MetaInfo("purifier", "coldwater", "hotwater", "1/6 inch round", "SN:33333" )
    
    
    a = parts(m1)
    b = parts(m2)
    c = parts(m3)
    

    以下是我需要你的帮助:

    1. 如何将m1,m2,m3保存为可以在人类可读数据库中持久化的列表,下次只更改该数据库本身我可以添加元数据?

    2. 如何将不同的部分链接为新组合?如

      e = a.linkwith(b)
      d = c.linkwith(a)
      

      并将其存储在该数据库中?

    3. 我可以制作一个长链,制作一个新的零件实例,例如

      f = c.linkwith(a,b,d,e)
      
    4. 并且很容易发现哪个部分无法链接到这个链中,这里有不同大小的c部分?

      非常感谢。

1 个答案:

答案 0 :(得分:1)

我很无聊。它非常粗糙,但它的工作原理。如果你这么做,你会想要使用一个数据库;但我理解想要使用人类可读的格式。

from copy import copy
import csv

class Part_Base(object):
    pass

class MultiPart_Base(list):
    pass

class part_meta(type):
    part_names = {}
    parts      = []
    def __init__(cls, cls_name, cls_bases, cls_dict):
        super(part_meta, cls).__init__(cls_name, cls_bases, cls_dict)
        if(not Part_Base in cls_bases):
            part_meta.part_names[cls_name] = cls

    def __call__(self, *args, **kwargs):
        name = kwargs.get("name", "")
        if(part_meta.part_names.has_key(name) and not (self is part_meta.part_names[name])):
            obj = part_meta.part_names[name].__call__(*args, **kwargs)
        else:
            obj = None
            if(not part_meta.part_names.has_key(self.__name__)):
                new_class = part_meta(name, (Generic_Part,), {})
                globals()[name] = new_class
                obj = new_class(*args, **kwargs)
            else:
                obj = super(part_meta, self).__call__(*args, **kwargs)
        if not obj in part_meta.parts:
            part_meta.parts.append(obj)
        return obj

    @classmethod
    def save(cls):
        all_fields = list(reduce(lambda x, y: x | set(y.fields), cls.parts, set([])))
        with open("parts.csv", "w") as file_h:
            writer = csv.DictWriter\
            (
                file_h,
                all_fields,
                restval        = "",
                extrasaction   = "ignore",
                dialect        = "excel",
                lineterminator = "\n",

            )
            writer.writeheader()
            for part in cls.parts:
                writer.writerow({field : getattr(part, field) for field in part.fields})

    @classmethod
    def load(cls):
        with open("parts.csv", "r") as file_h:
            reader = csv.DictReader(file_h)
            for row in reader:
                Part(**row)

class Part(Part_Base):
    __metaclass__ = part_meta
    fields        = []
    def __init__(self, **kwargs):
        for name, value in kwargs.items():
            setattr(self, name, value)
        self.fields += kwargs.keys()

    def __repr__(self):
        return "<%s>" % self.description

    @property
    def description(self):           
        return "%s: %s %s %s %s" % (self.name, self.intype, self.outtype, self.shape, self.serialno)

    def linkwith(self, *parts):
        return Generic_MultiPart(self, *parts)

class Generic_Part(Part):
    def __init__(self, **kwargs):
        kwargs["name"] = self.__class__.__name__
        super(Generic_Part, self).__init__(**kwargs)

class Generic_MultiPart(MultiPart_Base):
    def __init__(self, *parts):
        super(Generic_MultiPart, self).__init__()
        if len(parts) >= 2:
            self.shape = parts[0].shape
            self.linkwith(*parts)
        else:
            raise ValueError("Not enough parts")

    def __repr__(self):
        return "<MultiPart: %s>" % super(Generic_MultiPart, self).__repr__()

    def linkwith(self, *parts):
        for part in parts:
            if part.shape == self.shape:
                if isinstance(part, Part):
                    self.append(part)
                elif isinstance(part, MultiPart_Base):
                    self.extend(part)
            else:
                raise ValueError("Incompatible parts")
        return self

class cooler(Generic_Part):
    intype  = "hotwater"
    outtype = "coldwater"
    fields  = ["intype", "outtype"]

class heater(Generic_Part):
    intype  = "coldwater"
    outtype = "hotwater"
    fields  = ["intype", "outtype"]

def make_some_parts():
    some_parts = \
    [
        # This is actually a cooler object
        # The metaclass uses the cooler class from above
        # to create the object
        Part
        (
            name     = "cooler",
            shape    = "1/4 inch round",
            serialno = "SN:11111"
        ),
        # Using the heater class directly
        heater
        (
            shape    = "1/4 inch round",
            serialno = "SN:22222"
        ),
        Part
        (
            name     = "purifier",
            intype   = "coldwater",
            outtype  = "hotwater",
            shape    = "1/6 inch round",
            serialno = "SN:33333"
        ),
        Part
        (
            name     = "carbon_filter",
            intype   = "coldwater",
            outtype  = "coldwater",
            shape    = "1/4 inch round",
            serialno = "SN:33333"
        )
    ]

    useless_part = some_parts[0].linkwith(some_parts[1])
    print useless_part
    filter_part  = copy(useless_part).linkwith(some_parts[3])
    print filter_part

    part_meta.save()

def load_some_parts():
    part_meta.load()
    print part_meta.parts

您可以手动编辑parts.csv(在Excel或其他中),它将生成所描述的部分。 保存/恢复功能尚未扩展到MultiParts;你可以做到。