1

Following Python class i created using pyasn and I like to see the feasiblity of passing the value through a file rather than the way of creating an object and passing the values for the members through the object.types of value which are string and number would be there in excel sheet which means all the parameters whose values are not another schema would be there in excel sheet(schema indicates class like 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

Can we pass the values for asn for the above example through a file such as excel?Like reference with 'abcdefghixcv'.

Awin
  • 91
  • 1
  • 8
  • there are keys like reference,date,postcode etc which need to be filled through excel sheet rather than through manually assigning through objects. – Awin Oct 01 '18 at 05:10
  • Editted the example – Awin Oct 01 '18 at 07:39
  • I know to fill the value from excel sheet for a particular object ,but i want to know using the parameters in excel sheet whether pyasn can start creating the object and fill the value using the excel sheet /or any file on its own. – Awin Oct 01 '18 at 09:26
  • [Edit] your Question to show an example of the meaning of: **"...using the parameters in excel sheet whether pyasn can start creating the object..."**. Explain **" the parameters in excel sheet "**, do you mean the Type of values e.g. 'string', 'number' etc.? – stovfl Oct 01 '18 at 10:34
  • Yes, types of value which are string and number would be there in excel sheet,all the parameters whose values are not another schema would be there in excel sheet(schema means another class) – Awin Oct 01 '18 at 11:16
  • Are you talking about this topic [xlsx-to-xml-with-schema-map](https://stackoverflow.com/questions/25893266/xlsx-to-xml-with-schema-map) – stovfl Oct 01 '18 at 12:09
  • Not sure,I think the topic is about xml schema and this one is asn schema – Awin Oct 03 '18 at 06:03

1 Answers1

0

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

Given the following Data, retrieved from any files:

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'}

For brevity, I have striped down your class definitions to the only used parts.

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

I assume, the following asn1_schema, could be retrieved from class Order(), but for simplicity I have defined it manually.

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

This example is bound to the Data, that means the Dict data knows which Order['header'] class have to create. Therefore no predefined Order are needed.

# 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

Tested with Python:3.5.3

stovfl
  • 14,998
  • 7
  • 24
  • 51