forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.py
More file actions
30 lines (24 loc) · 1.12 KB
/
types.py
File metadata and controls
30 lines (24 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
import abc
import numbers
infinity = float('inf')
infinities = (infinity, -infinity)
class _PossiblyInfiniteIntegralType(abc.ABCMeta):
def __instancecheck__(self, thing):
return isinstance(thing, numbers.Integral) or (thing in infinities)
class PossiblyInfiniteIntegral(numbers.Number,
metaclass=_PossiblyInfiniteIntegralType):
'''An integer or infinity (including negative infinity.)'''
class _PossiblyInfiniteRealType(abc.ABCMeta):
def __instancecheck__(self, thing):
return isinstance(thing, numbers.Real) or (thing in infinities)
class PossiblyInfiniteReal(numbers.Number,
metaclass=_PossiblyInfiniteRealType):
'''A real number or infinity (including negative infinity.)'''
class _NaturalType(abc.ABCMeta):
def __instancecheck__(self, thing):
return isinstance(thing, numbers.Integral) and thing >= 1
class Natural(numbers.Number,
metaclass=_NaturalType):
'''A natural number, meaning a positive integer (0 not included.)'''