autotype-input 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- autotype/__init__.py +9 -0
- autotype/converter.py +11 -0
- autotype/core.py +70 -0
- autotype/registry.py +27 -0
- autotype_input-0.1.0.dist-info/METADATA +13 -0
- autotype_input-0.1.0.dist-info/RECORD +8 -0
- autotype_input-0.1.0.dist-info/WHEEL +5 -0
- autotype_input-0.1.0.dist-info/top_level.txt +1 -0
autotype/__init__.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from collections import __all__
|
|
2
|
+
from .core import auto_input,auto_convert
|
|
3
|
+
from .registry import (register_converter, unregister_converter)
|
|
4
|
+
from .converter import Converter
|
|
5
|
+
_all__ = ["auto_input",
|
|
6
|
+
"auto_convert",
|
|
7
|
+
"register_converter",
|
|
8
|
+
"unregister_converter",
|
|
9
|
+
"Converter"]
|
autotype/converter.py
ADDED
autotype/core.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
from datetime import date, datetime, time
|
|
3
|
+
from .registry import get_converter, NO_MATCH
|
|
4
|
+
from .converter import Converter
|
|
5
|
+
def auto_convert(value):
|
|
6
|
+
"""Automatically coverts a string to a suitable python datatype"""
|
|
7
|
+
|
|
8
|
+
if not isinstance(value,str):
|
|
9
|
+
return value
|
|
10
|
+
|
|
11
|
+
value = value.strip()
|
|
12
|
+
|
|
13
|
+
for converter in get_converter():
|
|
14
|
+
if isinstance(converter,Converter):
|
|
15
|
+
if converter.can_convert(value):
|
|
16
|
+
return converter.convert(value)
|
|
17
|
+
else:
|
|
18
|
+
result = converter(value)
|
|
19
|
+
if result is not NO_MATCH:
|
|
20
|
+
return result
|
|
21
|
+
|
|
22
|
+
#Boolean
|
|
23
|
+
if value.lower()=="true":
|
|
24
|
+
return True
|
|
25
|
+
|
|
26
|
+
if value.lower()=="false":
|
|
27
|
+
return False
|
|
28
|
+
|
|
29
|
+
#None
|
|
30
|
+
if value.lower() == "none":
|
|
31
|
+
return None
|
|
32
|
+
|
|
33
|
+
#Bytes
|
|
34
|
+
if value.startswith(("b'", 'b"')) and value.endswith(("'", '"')):
|
|
35
|
+
try:
|
|
36
|
+
result = ast.literal_eval(value)
|
|
37
|
+
if isinstance(result,bytes):
|
|
38
|
+
return result
|
|
39
|
+
except (ValueError, SyntaxError):
|
|
40
|
+
pass
|
|
41
|
+
|
|
42
|
+
#DAte
|
|
43
|
+
try:
|
|
44
|
+
return date.fromisoformat(value)
|
|
45
|
+
except ValueError:
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
#Datetime
|
|
49
|
+
try:
|
|
50
|
+
return datetime.fromisoformat(value)
|
|
51
|
+
except ValueError:
|
|
52
|
+
pass
|
|
53
|
+
|
|
54
|
+
#Time
|
|
55
|
+
try:
|
|
56
|
+
return time.fromisoformat(value)
|
|
57
|
+
except ValueError:
|
|
58
|
+
pass
|
|
59
|
+
|
|
60
|
+
#Other libraries
|
|
61
|
+
try:
|
|
62
|
+
return ast.literal_eval(value)
|
|
63
|
+
except(ValueError, SyntaxError):
|
|
64
|
+
pass
|
|
65
|
+
|
|
66
|
+
return value
|
|
67
|
+
|
|
68
|
+
def auto_input(prompt=""):
|
|
69
|
+
"""takes user input and automatcally determine its datatype"""
|
|
70
|
+
return auto_convert(input(prompt))
|
autotype/registry.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
_converters = []
|
|
2
|
+
class _NoMatch:
|
|
3
|
+
pass
|
|
4
|
+
|
|
5
|
+
NO_MATCH = _NoMatch()
|
|
6
|
+
|
|
7
|
+
def register_converter(converter, priority=None):
|
|
8
|
+
"""register a converter"""
|
|
9
|
+
if priority is None:
|
|
10
|
+
priority = getattr(converter, "priority", 0)
|
|
11
|
+
|
|
12
|
+
_converters.append((priority,converter))
|
|
13
|
+
_converters.sort(key=lambda item:item[0], reverse=True)
|
|
14
|
+
|
|
15
|
+
def registy_converter(converter, priority=0):
|
|
16
|
+
entry = (priority, converter)
|
|
17
|
+
_converters.append(entry)
|
|
18
|
+
_converters.sort(key=lambda item: item[0], reverse=True)
|
|
19
|
+
|
|
20
|
+
def unregister_converter(converter):
|
|
21
|
+
"Unregister a converter"
|
|
22
|
+
global _converters
|
|
23
|
+
_converters = [(priority, registered_converter)for priority,registered_converter in _converters if registered_converter is not converter]
|
|
24
|
+
|
|
25
|
+
def get_converter():
|
|
26
|
+
"""Return registered converters in priority order"""
|
|
27
|
+
return [converter for priority, converter in _converters]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: autotype-input
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Automatic datatype detection and conversion for python input
|
|
5
|
+
Author: DEBAYAN DEY
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
|
|
9
|
+
#Autotype Input
|
|
10
|
+
Automatically detects common Python datatypes from user input
|
|
11
|
+
|
|
12
|
+
##Installation
|
|
13
|
+
pip install autotype-input
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
autotype/__init__.py,sha256=MIbRt61S3d5dTCz2HdzYQuWrhlCulkxiCc_2x3cozgc,314
|
|
2
|
+
autotype/converter.py,sha256=45ae3GuxyQjN_kTA9xF7V-OXQUAh9uArMKyUmH6RmqI,258
|
|
3
|
+
autotype/core.py,sha256=ECt1GH3ZetANOnz28wR1qyPlhHus9vtpIPOsaoe6X0c,1713
|
|
4
|
+
autotype/registry.py,sha256=VI2wwieNgsNhegEsINErXcq4jxurWuqMGwBggo0v_S4,905
|
|
5
|
+
autotype_input-0.1.0.dist-info/METADATA,sha256=7v4L_ey3WE33uo3hUNna029JuGRZeKQGS3S17Qua55s,346
|
|
6
|
+
autotype_input-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
7
|
+
autotype_input-0.1.0.dist-info/top_level.txt,sha256=wvDSUgJjr5p15HoD9Aab66FqCyzSq4baSF6yjopemxA,9
|
|
8
|
+
autotype_input-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
autotype
|