Created
January 11, 2022 00:29
-
-
Save kirankotari/ba3f65d69d8efd5d922eeca375afd2d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from distutils import util | |
def _msg(args, msg=None): | |
msg = "input value error, please check your inputs." if msg is None else msg | |
return (f"{msg} {args[-1]}",) | |
def _try(func): | |
def wrap(*args, **kwargs): | |
try: | |
return func(*args, **kwargs) | |
except ValueError as e: | |
e.args = _msg(e.args) | |
raise(e) | |
except Exception as e: | |
raise(e) | |
return wrap | |
@_try | |
def inner(ip, typecaste, delimiter=None): | |
if type(ip) == str: | |
ip = list(map(typecaste, ip.split(delimiter))) | |
# ip = [typecaste(i) for i in ip.split(delimiter)] | |
return ip if len(ip) > 1 else ip[-1] | |
# if type(ip) == list: | |
# return [typecaste(i) for i in ip] | |
# if type(ip) == tuple: | |
# return tuple([typecaste(i) for i in ip]) | |
# # TODO: need to add more data types like list, dict, etc | |
# def inner_dict(ip, key_type, value_type): | |
# return dict(zip(inner(ip.keys(), key_type), inner(ip.values(), value_type))) | |
def _int(ip, delimiter=" "): | |
return inner(ip, int, delimiter) | |
def _float(ip, delimiter=" "): | |
return inner(ip, float, delimiter) | |
def _str(ip, delimiter=" "): | |
return inner(ip, str, delimiter) | |
def _bool(ip, delimiter=" "): | |
return inner(ip, lambda x: bool(util.strtobool(x)), delimiter) | |
def _complex(ip, delimiter=" "): | |
# TODO: delimiter issue on ip = "2 + 1j" | |
return inner(ip, complex, delimiter) | |
def mix(v): | |
precidence=float if '.' in v else int | |
try: | |
return float(v) if precidence is float else int(v) | |
except ValueError: | |
# TODO: can contains list, dict, etc | |
return str(v) | |
def _mix(ip, delimiter=" "): | |
return inner(ip, mix, delimiter) | |
# def _list(): | |
# pass | |
# def _dict(): | |
# pass | |
# def _tuple(): | |
# pass | |
# def _set(): | |
# pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment