Skip to content

Commit

Permalink
0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt authored Dec 18, 2021
1 parent 043961a commit bc3b3c3
Showing 1 changed file with 66 additions and 2 deletions.
68 changes: 66 additions & 2 deletions arclet/alconna/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import TypeVar
import inspect
from typing import TypeVar, Union, Type, Dict

AnyIP = r"(\d+)\.(\d+)\.(\d+)\.(\d+)"
AnyDigit = r"(\d+)"
Expand All @@ -7,4 +8,67 @@
Bool = r"(True|False)"

NonTextElement = TypeVar("NonTextElement")
MessageChain = TypeVar("MessageChain")
MessageChain = TypeVar("MessageChain")
Argument_T = Union[str, Type[NonTextElement]]


class Args:
args: Dict[str, Argument_T]
_default: Dict[str, Union[str, NonTextElement]]
empty: bool = False

def __init__(self, **kwargs):
self.args = {k: v for k, v in kwargs.items() if k not in ('name', 'type')}
self._default = {}
if not self.args:
self.empty = True

def default(self, **kwargs):
self._default = {k: v for k, v in kwargs.items() if k not in ('name', 'type')}
return self

def check(self, keyword: str):
if keyword in self._default:
if self._default[keyword] is None:
return inspect.Signature.empty
return self._default[keyword]

def params(self, sep: str = " "):
argument_string = ""
i = 0
length = len(self.args)
for k, v in self.args.items():
arg = f"<{k}"
if not isinstance(v, str):
arg += f": Type_{v.__name__}"
if k in self._default:
default = self._default[k]
if default is None:
arg += " default: Empty"
elif isinstance(default, str):
arg += f" default: {default}"
else:
arg += f" default: Type_{default.__name__}"
argument_string += arg + ">"
i += 1
if i != length:
argument_string += sep
return argument_string

def __iter__(self):
for k, v in self.args.items():
yield k, v

def __len__(self):
return len(self.args)

def __repr__(self):
if self.empty:
return "Empty"
repr_string = ""
for k, v in self.args.items():
text = f"'{k}': {v}"
if k in self._default:
text += f" default={self._default[k]}"
repr_string += text + ",\n"
return "\n" + repr_string + ""

0 comments on commit bc3b3c3

Please sign in to comment.