gpt4 book ai didi

python - 通过excel等文件填充值

转载 作者:太空宇宙 更新时间:2023-11-03 21:45:13 24 4
gpt4 key购买 nike

以下Python类是我使用pyasn创建的,我喜欢看到通过文件传递值的可行性,而不是创建对象并通过对象传递成员值的方式。值的类型是字符串和数字将出现在 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”进行引用。

最佳答案

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

我假设,可以从 class Order() 检索以下 asn1_schema,但为了简单起见,我手动定义了它。

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

此示例绑定(bind)到数据,这意味着 Dict 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))

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 进行测试

关于python - 通过excel等文件填充值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52552393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com