| |
- drange(start: float, stop: float, step: float = 1) -> collections.abc.Iterator[float]
- This function implements range for Decimal value.
>>> import GetType
>>> for i in GetType.drange(0, 5, 2): i
...
0.0
2.0
4.0
>>> for i in GetType.drange(0.1, 0.5, 0.2): i
...
0.1
0.3
>>> [i for i in GetType.drange(0, 100, 0.1)][-1]
99.9
>>>
- get_boolean(value: str, raise_exception: bool = True, default: Any = None, case_sensitive: bool = False, true_values: collections.abc.Iterator[str] = ('yes', 'on', '1', 'true', 'y'), false_values: collections.abc.Iterator[str] = ('no', 'off', '0', 'false', 'n')) -> None
- This function returns a boolean from string value.
>>> import GetType
>>> GetType.get_boolean('true')
True
>>> GetType.get_boolean('yes')
True
>>> GetType.get_boolean('on')
True
>>> GetType.get_boolean('1')
True
>>> GetType.get_boolean('y')
True
>>> GetType.get_boolean('false')
False
>>> GetType.get_boolean('off')
False
>>> GetType.get_boolean('no')
False
>>> GetType.get_boolean('n')
False
>>> GetType.get_boolean('0')
False
>>> GetType.get_boolean('FALSE')
False
>>> GetType.get_boolean('TRUE')
True
>>> GetType.get_boolean('+', true_values=['+'], false_values=['-', 'X'])
True
>>> GetType.get_boolean('-', true_values=['+'], false_values=['-', 'X'])
False
>>> GetType.get_boolean('X', true_values=['+'], false_values=['-', 'X'])
False
>>> GetType.get_boolean('x', true_values=['+'], false_values=['-', 'X'])
False
>>> try: GetType.get_boolean(
... 'invalid',
... true_values=['+'],
... false_values=['-', 'X'],
... )
... except ValueError: print("ValueError: 'invalid' is not in ['-', 'x', '+']")
...
ValueError: 'invalid' is not in ['-', 'x', '+']
>>> GetType.get_boolean(
... 'invalid',
... true_values=['+'],
... false_values=['-', 'X'],
... raise_exception=False,
... )
>>> GetType.get_boolean(
... 'invalid',
... true_values=['+'],
... false_values=['-', 'X'],
... raise_exception=False,
... default="invalid",
... )
'invalid'
>>> GetType.get_boolean(
... 'x',
... true_values=['+'],
... false_values=['-', 'X'],
... raise_exception=False,
... default="invalid",
... )
False
>>> GetType.get_boolean(
... 'x',
... true_values=['+'],
... false_values=['-', 'X'],
... raise_exception=False,
... default="invalid",
... case_sensitive=True,
... )
'invalid'
>>>
- get_ipv4_addresses(value: str, default: Any = None, separator: str = ',', generator_char: str = '-', raise_exception: bool = True) -> collections.abc.Iterator[ipaddress.IPv4Address]
- This function yields ip addresses.
>>> import GetType
>>> [str(i) for i in GetType.get_ipv4_addresses("10.10.10.10-10.10.10.0")]
['10.10.10.0', '10.10.10.1', '10.10.10.2', '10.10.10.3', '10.10.10.4', '10.10.10.5', '10.10.10.6', '10.10.10.7', '10.10.10.8', '10.10.10.9']
>>> [str(i) for i in GetType.get_ipv4_addresses("10.10.10.0/29")]
['10.10.10.1', '10.10.10.2', '10.10.10.3', '10.10.10.4', '10.10.10.5', '10.10.10.6']
>>> [str(i) for i in GetType.get_ipv4_addresses("10.10.10.0")]
['10.10.10.0']
>>> [str(i) for i in GetType.get_ipv4_addresses("10.10.10.0/33")]
Traceback (most recent call last):
...
ValueError: '10.10.10.0/33' is not a valid network (example: 127.0.0.1/29) [<valid ip>/<0-32>].
>>> [str(i) for i in GetType.get_ipv4_addresses("10.10.10.0/abc")]
Traceback (most recent call last):
...
ValueError: '10.10.10.0/abc' is not a valid network (example: 127.0.0.1/29) [<valid ip>/<0-32>].
>>> [str(i) for i in GetType.get_ipv4_addresses("10.10.10.0/abc", raise_exception=False)]
[]
>>> [str(i) for i in GetType.get_ipv4_addresses("10.10.10.0/abc", default="0.0.0.0", raise_exception=False)]
['0.0.0.0']
>>> [str(i) for i in GetType.get_ipv4_addresses("10.10.10.0?10.10.10.2", generator_char="?")]
['10.10.10.0', '10.10.10.1']
>>> [str(i) for i in GetType.get_ipv4_addresses("10.10.10.0/10.10.10.2", generator_char="/")]
Traceback (most recent call last):
...
ValueError: separator/generator_char should not be '/' (reserved by network)
>>> [str(i) for i in GetType.get_ipv4_addresses("10.10.10.0/10.10.10.2", separator="/")]
Traceback (most recent call last):
...
ValueError: separator/generator_char should not be '/' (reserved by network)
>>> [str(i) for i in GetType.get_ipv4_addresses("abc", default="0.0.0.0", raise_exception=False)]
['0.0.0.0']
>>> [str(i) for i in GetType.get_ipv4_addresses("abc-def", default="0.0.0.0", raise_exception=False)]
['0.0.0.0']
>>> [str(i) for i in GetType.get_ipv4_addresses("127.0.0.1,127.0.0.2")]
['127.0.0.1', '127.0.0.2']
>>>
- get_numbers(value: str, default: Any = None, separator: str = ',', step_char: str = ':', step_default: int = 1, generator_char: str = '-', raise_exception: bool = True) -> collections.abc.Iterator[float]
- This function yields integers from string value.
>>> import GetType
>>> [i for i in GetType.get_numbers("4")]
[4.0]
>>> [i for i in GetType.get_numbers("4.1")]
[4.1]
>>> [i for i in GetType.get_numbers("4.1,5")]
[4.1, 5.0]
>>> [i for i in GetType.get_numbers("4.1,5-7")]
[4.1, 5.0, 6.0]
>>> [i for i in GetType.get_numbers("4.1?5-6.6:0.5", separator="?")]
[4.1, 5.0, 5.5, 6.0, 6.5]
>>> [i for i in GetType.get_numbers("4.1,5-6.6?invalid", step_char="?")]
Traceback (most recent call last):
...
ValueError: 'invalid' is not a valid number.
>>> [i for i in GetType.get_numbers("4.1,invalid?6.6:0.1", generator_char="?")]
Traceback (most recent call last):
...
ValueError: 'invalid' is not a valid number.
>>> [i for i in GetType.get_numbers("4.1,??,", generator_char="?")]
Traceback (most recent call last):
...
ValueError: Generator should be '<float>?<float>:<float>' or '<float>?<float>' or '[float]' (examples: '0?5.5:0.2' '0?5.5' '4') not '??'
>>> [i for i in GetType.get_numbers("4.1,??,", raise_exception=False)]
[4.1]
>>> [i for i in GetType.get_numbers("4.1,??,5.2-5.8:0.2", raise_exception=False)]
[4.1, 5.2, 5.4, 5.6]
>>> [i for i in GetType.get_numbers("4.1,??,5.2-5.8:abc", raise_exception=False)]
[4.1, 5.2]
>>> [i for i in GetType.get_numbers("4.1,??,5.2-5.8:abc", raise_exception=False, step_default=0.2)]
[4.1, 5.2, 5.4, 5.6]
>>> [i for i in GetType.get_numbers("1,?,abc-def,5.2-5.8:abc", raise_exception=False, default=0)]
[1.0, 0, 0, 5.2]
>>> [i for i in GetType.get_numbers("5.2-5.8-abGetType.html", raise_exception=False, default=0)]
[0]
>>>
- is_ip(value: str, raise_exception: bool) -> bool
- This function valids IPv4 addresses.
>>> import GetType
>>> GetType.is_ip("0.0.0.0", True)
True
>>> GetType.is_ip("255.255.255.255", True)
True
>>> GetType.is_ip("255.255.255", True)
Traceback (most recent call last):
...
ValueError: '255.255.255' is not a valid IPv4 address(should be 4 byte values separate by '.', examples: 0.0.0.0 255.255.255.255)
>>> GetType.is_ip("255.255.255.256", True)
Traceback (most recent call last):
...
ValueError: '255.255.255.256' is not a valid IPv4 address ('256' is not a valid byte value).
>>> GetType.is_ip("255.255.255.abc", True)
Traceback (most recent call last):
...
ValueError: '255.255.255.abc' is not a valid IPv4 address ('abc' is not a valid byte value).
>>> GetType.is_ip("255.255.255.01", True)
Traceback (most recent call last):
...
ValueError: '255.255.255.01' is not a valid IPv4 address ('01' is not a valid byte value).
>>> GetType.is_ip("255.255.255.01", False)
False
>>> GetType.is_ip("255.255.255", False)
False
>>>
- is_number(value: str, raise_exception: bool) -> bool
- This function checks value for valid number.
>>> import GetType
>>> GetType.is_number("1", True)
True
>>> GetType.is_number("1.1", True)
True
>>> GetType.is_number("abc", True)
Traceback (most recent call last):
...
ValueError: 'abc' is not a valid number.
>>> GetType.is_number("abc", False)
False
>>>
- try_type(value: str) -> Any
- This function returns a typed value (be careful with this function,
detection can be bad (example: if you want a string of number this
function will return a float)).
>>> import GetType
>>> GetType.try_type("1")
1.0
>>> GetType.try_type("1.5")
1.5
>>> GetType.try_type("yes")
True
>>> GetType.try_type("on")
True
>>> GetType.try_type("y")
True
>>> GetType.try_type("true")
True
>>> GetType.try_type("false")
False
>>> GetType.try_type("n")
False
>>> GetType.try_type("no")
False
>>> GetType.try_type("0")
0.0
>>> GetType.try_type("")
>>> GetType.try_type("null")
>>> GetType.try_type("None")
>>> GetType.try_type("none")
>>> GetType.try_type("NULL")
>>> GetType.try_type("abc")
'abc'
>>> GetType.try_type("my string")
'my string'
>>> GetType.try_type("Y")
True
>>>
|