Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get instance variables in order in Python

Tags:

python

Let's say I have the following class:

class Foo(object):
    def __init__(self, value):
        self.d = value
        self.a = value
        self.s = value
        self.k = value

I want to retrieve the instance variables in the order of declaration.

I tried with vars() without any success:

list(vars(Foo('value')).keys())
# ['a', 'k', 's', 'd']

What I would like:

list(magic_method(Foo('value')).keys())
# ['d', 'a', 's', 'k']

Edit:

Obviously, there would be a different value for each field.

My goal is to generate XML thanks to the object variables. To be valid, the XML tags has to be in the correct order.

This, combined with __iter__ override would allow me to only have to manage dictionaries of object to generate my XML.

Let's take a library as example. Imagine you have a class Book, Date, Person, Author and Borrower:

class Book(object):
    def self.__init__()
        self._borrower = Borrower()
        self._author = Author()

class Date(object)
    def __init__(self, date):
        self._date = date

class Person(object):
    def __init__(self, name):
        self._name = name

class Author(object):
    def __init__(self):
        self._person = Person("Daniel")

class Borrower(object):
    def __init__(self):
        self._person = Person("Jack")
        self._date = Date("2016-06-02")

I would like to create the following XML:

<Book>
    <Borrower>
        <Person>Jack</Person>
        <Date>2016-06-02</Date>
    </Borrower>
    <Author>
        <Person>Daniel</Person>
    </Author>
</Book>

I know the classes might look weird (like Date here), but I wanted to make the problem as simple as possible (and there are fields that make perfect sense). In practice, I would query a database and probably pass an record identifier in initializers. The point is that there are some data that respects the same syntax (i.e. Person here).

To summarize, I would like to create such an XML using Python objects. Order matters. That's why I wanted to retrieve variables in order for that purpose: I could then extract the class and generate the XML tag.

like image 251
maddersky Avatar asked Jun 02 '16 11:06

maddersky


1 Answers

If you want ordering of object variables you can use something like that:

from collections import OrderedDict

class FooModel(object):
    def __new__(cls, *args, **kwargs):
        instance = object.__new__(cls)
        instance.__odict__ = OrderedDict()
        return instance

    def __setattr__(self, key, value):
        if key != '__odict__':
            self.__odict__[key] = value
        object.__setattr__(self, key, value)

    def keys(self):
        return self.__odict__.keys()

    def iteritems(self):
        return self.__odict__.iteritems()


class Foo(FooModel):
    def __init__(self, value):
        self.d = value
        self.a = value
        self.s = value
        self.k = value

Output:

>>> f = Foo('value')
>>> f.x = 5
>>> f.y = 10
>>> f.a = 15
>>> f2 = Foo('value')
>>> print "f.keys()", f.keys()
f.keys() ['d', 'a', 's', 'k', 'x', 'y']
>>> print "f2.keys()", f2.keys()
f2.keys() ['d', 'a', 's', 'k']
print list(f.iteritems())
[('d', 'value'), ('a', 15), ('s', 'value'), ('k', 'value'), ('x', 5), ('y', 10)]
like image 119
Schore Avatar answered Oct 13 '22 22:10

Schore