+ r = 0
+ pt = 0
+ for c in t[1:]:
+ r = r + 1
+ if c != '.':
+ if pt != 1:
+ cpt = cpt + 1
+ s = s + c
+ else:
+ pt = 1
+ if r + 1 == len(t) or t[r + 1:].atof() == 0.:
+ break
+ s = s + 'E' + neg * '-' + repr(cpt)
+ return s
+
+
+def import_object(uri):
+ """Load and return a python object (class, function...).
+ Its `uri` looks like "mainpkg.subpkg.module.object", this means
+ that "mainpkg.subpkg.module" is imported and "object" is
+ the object to return.
+ """
+ path = uri.split('.')
+ modname = '.'.join(path[:-1])
+ if len(modname) == 0:
+ raise ImportError(u"invalid uri: %s" % uri)
+ mod = object = '?'
+ objname = path[-1]
+ try:
+ __import__(modname)
+ mod = sys.modules[modname]
+ except ImportError as err:
+ raise ImportError(
+ "can not import module : %s (%s)" % (modname, str(err)))
+ try:
+ object = getattr(mod, objname)
+ except AttributeError as err:
+ raise AttributeError("object (%s) not found in module '%s'. "
+ "Module content is: %s" % (objname, modname, tuple(dir(mod))))
+ return object
+
+
+class Singleton(object):
+
+ """Singleton implementation in python."""
+ # add _singleton_id attribute to the class to be independant of import
+ # path used
+ __inst = {}
+
+ def __new__(cls, *args, **kargs):
+ cls_id = getattr(cls, '_singleton_id', cls)
+ if Singleton.__inst.get(cls_id) is None:
+ Singleton.__inst[cls_id] = object.__new__(cls)
+ return Singleton.__inst[cls_id]
+
+
+class Enum(object):
+
+ """
+ This class emulates a C-like enum for python. It is initialized with a list
+ of strings to be used as the enum symbolic keys. The enum values are automatically
+ generated as sequencing integer starting at 0.
+ """
+
+ def __init__(self, *keys):
+ """Constructor"""
+ self._dict_keys = {}
+ for inum, key in enumerate(keys):
+ setattr(self, key, 2 ** inum)
+ self._dict_keys[2 ** inum] = key
+
+ def exists(self, value):
+ """Tell if value is in the enumeration"""
+ return self.get_id(value) is not None