Source code for openglider.utils

#! /usr/bin/python2
# -*- coding: utf-8; -*-
#
# (c) 2013 booya (http://booya.at)
#
# This file is part of the OpenGlider project.
#
# OpenGlider is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# OpenGlider is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenGlider.  If not, see <http://www.gnu.org/licenses/>.

from openglider.utils.cache import recursive_getattr

[docs]def sign(val): val = float(val) return (val > 0) - (val < 0)
[docs]def consistent_value(elements, attribute): vals = [recursive_getattr(element, attribute) for element in elements] if vals[1:] == vals[:-1]: return vals[0]
[docs]def linspace(start, stop, count): return [start + y/(count-1) * (stop-start) for y in range(count)]
[docs]class dualmethod(object): """ A Decorator to have a combined class-/instancemethod >>>class a: ... @dualmethod ... def test(this): ... return this ... >>>a.test() <class '__main__.a'> >>>a().test() <__main__.a object at 0x7f133b5f7198> >>> an instance-check could be: is_instance = not type(this) is type """ def __init__(self, func): self.func = func def __get__(self, obj, cls=None): obj = obj or cls is_instance = not type(obj) is type def temp(*args, **kwargs): return self.func(obj, *args, **kwargs) return temp
[docs]class Config(object): def __init__(self, dct=None): self.__dict__ = {key: value for key, value in self.__class__.__dict__.items() if not key.startswith('_')} self.__dict__.update(dct or {}) def __json__(self): return self.__dict__ def __repr__(self): return self.__dict__.__repr__() def __iter__(self): for key, value in self.__dict__.items(): yield key, value #return self.__dict__.__iter__() def __getitem__(self, item): return self.__getattribute__(item)
[docs] def get(self, key): return self.__getattribute__(key)
if __name__ == "__main__": a = {"a": 1, "b":{"c":2}} d = Config(a) print(d.b.c)