通过文件(如ex)填充值

2024-05-29 03:10:40 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我使用pyasn创建的Python类,我希望看到通过文件传递值的可行性,而不是创建对象并通过对象类型在excel表中会有字符串和数字的值,这意味着所有参数的值都不是另一个模式将出现在excel表中(模式表示像信用卡这样的类)

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'。你知道吗


Tags: tagorderheaderunivcharconstraintsubtypenamedtype
1条回答
网友
1楼 · 发布于 2024-05-29 03:10:40

Question: Can we pass the values for asn for the above example through a file

给定以下数据,从任何文件检索:

Note: For this example, the client_data are hold in a second file referenced with the id == 12345! The data values are already assigned to the NamedType of class Order['header'] and 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()检索,但为了简单起见,我已经手动定义了它。你知道吗

asn1_schema = {'reference':NumericString, 'date':NumericString, 'client':Client, 'payment':Payment_method}

这个例子绑定到数据,这意味着Dictdata知道必须创建哪个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))

Output:

date      : NumericString :     value:2018-10-03
reference : NumericString :     value:abc
client    : Client        :     value:John, New York
payment   : Payment_method:     value:cash

测试Python:3.5.3

相关问题 更多 >

    热门问题