| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# (c) Jérôme Laheurte 2015-2026 |
| 4 |
# See LICENSE.txt |
| 5 |
|
| 6 |
""" |
| 7 |
Miscellaneous utilities. |
| 8 |
""" |
| 9 |
|
| 10 |
import functools |
| 11 |
|
| 12 |
|
| 13 |
class Singleton(type): |
| 14 |
""" |
| 15 |
Singleton metaclass |
| 16 |
""" |
| 17 |
def __new__(mcs, name, bases, attrs): |
| 18 |
cls = type.__new__(mcs, name, bases, attrs) |
| 19 |
cls.__eq__ = lambda self, other: other is self |
| 20 |
cls.__lt__ = lambda self, other: not other is self |
| 21 |
cls.__copy__ = lambda self: self |
| 22 |
cls.__deepcopy__ = lambda self, memo: self |
| 23 |
cls.__repr__ = lambda self: self.__reprval__ |
| 24 |
cls.__len__ = lambda self: len(self.__reprval__) |
| 25 |
cls.__hash__ = lambda self: hash(id(self)) |
| 26 |
return functools.total_ordering(cls)() |
| 27 |
|
| 28 |
|
| 29 |
def callback_by_name(func_name): # pylint: disable=missing-function-docstring |
| 30 |
def _wrapper(instance, *args, **kwargs): |
| 31 |
return getattr(instance, func_name)(*args, **kwargs) |
| 32 |
return _wrapper |
| 33 |
|
| 34 |
|
| 35 |
def chars(s): # pylint: disable=missing-function-docstring |
| 36 |
return (s, s.encode('ascii')[0]) |