Skip to content

Commit 2d336f3

Browse files
committed
-
1 parent 3ccc4f4 commit 2d336f3

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

source_py2/python_toolbox/misc_tools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
_ascii_variable_pattern, is_legal_ascii_variable_name,
99
is_magic_variable_name, get_actual_type, is_number, identity_function,
1010
do_nothing, OwnNameDiscoveringDescriptor, find_clear_place_on_circle,
11-
general_sum, general_product, is_legal_email_address, is_type
11+
general_sum, general_product, is_legal_email_address, is_type, NonInstatiable
1212
)
1313
from . import name_mangling
1414
from .proxy_property import ProxyProperty

source_py2/python_toolbox/misc_tools/misc_tools.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,12 @@ def is_type(thing):
276276
'''Is `thing` a class? Allowing both new-style and old-style classes.'''
277277
return isinstance(thing, (type, types.ClassType))
278278

279+
class NonInstatiable(object):
280+
'''
281+
Class that can't be instatiated.
282+
283+
Inherit from this for classes that should never be instantiated, like
284+
constants and settings.
285+
'''
286+
def __new__(self, *args, **kwargs):
287+
raise RuntimeError('This class may not be instatiated.')

source_py2/python_toolbox/reasoned_bool.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def __eq__(self, other):
4040
return bool(self) == other
4141

4242

43+
def __hash__(self):
44+
return hash(bool(self))
45+
46+
4347
def __neq__(self, other):
4448
return not self.__eq__(other)
4549

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2009-2013 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
from python_toolbox import cute_testing
5+
6+
from python_toolbox.misc_tools import NonInstatiable
7+
8+
9+
def test():
10+
class MyNonInstatiable(NonInstatiable):
11+
pass
12+
13+
with cute_testing.RaiseAssertor(exception_type=RuntimeError):
14+
MyNonInstatiable()

0 commit comments

Comments
 (0)