4. XObject and XNamedObject

class XObject( )
The XObject class should not be used directly but with inheritence. Indeed, if a class inherit the XObject, a python Meta-class is used to transform a list of keywords (declared as class attributes) in something understood by python itself. Note that, this job is done at the class creation and not at the first instance. The documented keywords are __init__xattributes__, __init__argslen__, __object__xattributes__ and __object__xmethods__.

class XNamedObject( )
The XNamedObject class inherit from XObject, and thus the comportment is similar. The difference is that an attribute name is defined for the classes which inherit XNamedObject. More-over, the name is initialized at a value which ``makes sense'' in the user python script. Indeed, the name is found by the code calling the class. Technically, it is not done by parsing the code source but by disassembling the code itself (see python module dis). Using XNamedObject avoid to write code like a = A(name='a'). For instance:
# --
# Copyright (C) CEA, EDF
# Author : Erwan ADAM (CEA)
# --

import unittest

from xdata import *

class A(XNamedObject):
    pass

class ATestCase(unittest.TestCase):
    def test(self):
        a = A()
        self.failUnlessEqual(a.getName(), "a")
        self.failUnlessEqual(a.name, "a")
        a.name = "aaa"
        self.failUnlessEqual(a.getName(), "aaa")
        self.failUnlessEqual(a.name, "aaa")
        return
    pass

if __name__ == '__main__':
    unittest.main()
    pass

If the name cannot be found, an exception is not raised at the object creation but if one try to access the name:

# --
# Copyright (C) CEA, EDF
# Author : Erwan ADAM (CEA)
# --

import unittest

from xdata import *

class A(XNamedObject):
    pass

def f(x):
    return x

class ATestCase(unittest.TestCase):
    def test(self):
        x = f(A())
        # Obsolete : getName() does not raise but return None
        # self.failUnlessRaises(XAttributeError, x.getName)
        # self.failUnlessRaises(XAttributeError, getattr, x, "name")
        return
    pass

if __name__ == '__main__':
    unittest.main()
    pass



Subsections