通过Excel等文件填充值 - python

在我使用pyasn创建的Python类之后,我想看到通过文件传递值的可行性,而不是创建对象并通过对象传递成员值的方式的可行性。值的类型是字符串和数字在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'的引用。

参考方案

问题:我们能否通过文件传递上述示例的asn值

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

  注意:在本例中,client_data被保存在用id == 12345引用的第二个文件中! data值已分配给NamedTypeclass Order['header']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}

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

  输出:

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

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…

TypeError:'str'对象不支持项目分配,带有json文件的python - python

以下是我的代码import json with open('johns.json', 'r') as q: l = q.read() data = json.loads(l) data['john'] = '{}' data['john']['use…