在我使用pyasn创建的Python类之后,我希望看到将值通过文件而不是通过创建对象并将成员的值通过object传递的方式的可行性。value的类型是字符串和数字将出现在excel工作表中,这意味着所有值不是另一个模式的参数都将出现在excel工作表中(模式表示类似Credit_card的类)
from pyasn1.type import univ, char, namedtype, namedval, tag, constraint, useful
class Card_type(univ.Enumerated):
pass
Card_type.namedValues = namedval.NamedValues(
('cb', 0),
('visa', 1),
('eurocard', 2),
('diners', 3),
('american-express', 4)
)
class Client(univ.Sequence):
pass
Client.componentType = namedtype.NamedTypes(
namedtype.NamedType('name', char.PrintableString().subtype(subtypeSpec=constraint.ValueSizeConstraint(1, 20)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
namedtype.OptionalNamedType('street', char.PrintableString().subtype(subtypeSpec=constraint.ValueSizeConstraint(1, 50)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
namedtype.NamedType('postcode', char.NumericString().subtype(subtypeSpec=constraint.ValueSizeConstraint(5, 5)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2))),
namedtype.NamedType('town', char.PrintableString().subtype(subtypeSpec=constraint.ValueSizeConstraint(1, 30)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))),
namedtype.DefaultedNamedType('country', char.PrintableString().subtype(subtypeSpec=constraint.ValueSizeConstraint(1, 20)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4)).subtype(value="France"))
)
class Credit_card(univ.Sequence):
pass
Credit_card.componentType = namedtype.NamedTypes(
namedtype.NamedType('type', Card_type().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
namedtype.NamedType('number', char.NumericString().subtype(subtypeSpec=constraint.ValueSizeConstraint(20, 20)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
namedtype.NamedType('expiry-date', char.NumericString().subtype(subtypeSpec=constraint.ValueSizeConstraint(6, 6)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)))
)
class Payment_method(univ.Choice):
pass
Payment_method.componentType = namedtype.NamedTypes(
namedtype.NamedType('check', char.NumericString().subtype(subtypeSpec=constraint.ValueSizeConstraint(15, 15)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
namedtype.NamedType('credit-card', Credit_card().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 1))),
namedtype.NamedType('cash', univ.Null().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)))
)
class Order_header(univ.Sequence):
pass
Order_header.componentType = namedtype.NamedTypes(
namedtype.NamedType('reference', char.NumericString().subtype(subtypeSpec=constraint.ValueSizeConstraint(12, 12)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
namedtype.NamedType('date', char.NumericString().subtype(subtypeSpec=constraint.ValueSizeConstraint(8, 8)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
namedtype.NamedType('client', Client().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 2))),
namedtype.NamedType('payment', Payment_method().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3)))
)
class Order(univ.Sequence):
pass
Order.componentType = namedtype.NamedTypes(
namedtype.NamedType('header', Order_header().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))),
#namedtype.NamedType('items', univ.SequenceOf(componentType=Order_line()).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
)
a=Order()
a['header']['reference']='abcdefghixcv'
print a
#Output
Order:
header=Order_header:
reference=abcdefghixcv
我们可以通过诸如excel之类的文件将上述示例的asn值传递给吗?就像引用'abcdefghixcv'一样。
答案 0 :(得分:0)
问题:能否通过文件传递上述示例的asn值
给出以下从任何文件中检索到的数据:
注意:在此示例中,
client_data
被保存在用id == 12345
引用的第二个文件中!data
值已分配给NamedType
和class Order['header']
的{{1}}。
class Client
为简便起见,我将您的类定义简化为仅使用的部分。
client_data = {'12345':{'name':'John', 'town':'New York'}}
data = {'reference': 'abc', 'date': '2018-10-03', 'client': '12345', 'payment': 'cash'}
我假设可以从class NumericString():
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
def __new__(arg):
obj = object.__new__(NumericString)
obj.__init__(arg)
return obj
class Payment_method():
def __init__(self, method):
self.method = method
def __str__(self):
return self.method
def __new__(arg):
obj = object.__new__(Payment_method)
obj.__init__(arg)
return obj
class Client():
def __init__(self, data):
self.data = data
def __str__(self):
return ", ".join(self.data.values())
def __new__(id):
obj = object.__new__(Client)
obj.__init__(id)
return obj
中检索以下asn1_schema
,但是为简单起见,我手动定义了它。
class Order()
此示例绑定到数据,这意味着Dict asn1_schema = {'reference':NumericString, 'date':NumericString, 'client':Client, 'payment':Payment_method}
知道必须创建哪个data
类。因此,不需要预定义的Order['header']
。
Order
输出:
# For simplicity, define order as a 'dict' order = {'header':{}} # Loop over the given data dict for key in data: # Create a class maped from asn1_schema and pass data value if key == 'client': # For client pass data from 'client_data' using 'id' cls = asn1_schema[key].__new__(client_data[data[key]]) else: cls = asn1_schema[key].__new__(data[key]) # Update order header with this 'NamedType' order['header'][key] = cls # Show the resulting order header for key in order['header']: types = order['header'][key] print("{:10}:\t{:14}:\tvalue:{}".format(key, types.__class__.__name__, types))
经过Python:3.5.3
的测试