Skip to content

Commit

Permalink
chore: rename from Variable to EvalObject, var_property to `eo_…
Browse files Browse the repository at this point in the history
…property`
  • Loading branch information
elhoangvu committed May 1, 2024
1 parent 7c5147a commit 813d10f
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 195 deletions.
8 changes: 4 additions & 4 deletions src/dictrule/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from .generator import Generator
from .rule import Rule
from .context import Context
from .var_property import var_property
from .variable import Variable
from .eo_property import eo_property
from .eval_object import EvalObject
from .__version__ import (
__title__,
__description__,
Expand Down Expand Up @@ -57,8 +57,8 @@
"JoinBlockRule",
"JoinEvalRule",
"FormatRule",
"var_property",
"Variable",
"eo_property",
"EvalObject",
"__title__",
"__description__",
"__url__",
Expand Down
34 changes: 17 additions & 17 deletions src/dictrule/var_property.py → src/dictrule/eo_property.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""var_property decorator module"""
"""eo_property decorator module"""

from typing import (
List,
Expand All @@ -8,23 +8,23 @@
)


class var_property:
"""var_property decorator that defines property for dictrule.Variable.
class eo_property:
"""eo_property decorator that defines property for dictrule.EvalObject.
Examples:
---------
>>> class Person:
... @var_property
... @eo_property
... def name(self) -> str:
... return "Zooxy"
... def age(self) -> int:
... return 20
>>> var = dictrule.Variable.from_object(Person())
>>> print(isinstance(var, Variable))
>>> obj = dictrule.EvalObject.from_object(Person())
>>> print(isinstance(var, EvalObject))
True
>>> print(var.name)
>>> print(obj.name)
Zooxy
>>> print(var.age)
>>> print(obj.age)
20
"""

Expand All @@ -35,7 +35,7 @@ def __init__(
fdel: Optional[Callable[[Any], None]] = None,
doc: Optional[str] = None,
):
"""var_property decorator
"""eo_property decorator
Args:
fget (Optional[Callable[[Any], Any]], optional): Getter method. Defaults to None.
Expand Down Expand Up @@ -69,21 +69,21 @@ def __delete__(self, obj) -> None:
raise AttributeError("can't delete attribute")
self.fdel(obj)

def getter(self, fget) -> "var_property":
def getter(self, fget) -> "eo_property":
"""Getter decorator
Examples:
---------
>>> class Sample:
... @var_property
... @eo_property
... def name(self):
... return "Zooxy"
>>> Sample().name
Zooxy
"""
return type(self)(fget, self.fset, self.fdel, self.__doc__)

def setter(self, fset) -> "var_property":
def setter(self, fset) -> "eo_property":
"""Setter decorator
Examples:
Expand All @@ -94,7 +94,7 @@ def setter(self, fset) -> "var_property":
"""
return type(self)(self.fget, fset, self.fdel, self.__doc__)

def deleter(self, fdel) -> "var_property":
def deleter(self, fdel) -> "eo_property":
"""Deleter decorator
Examples:
Expand All @@ -110,18 +110,18 @@ def properties(
cls,
instance: Any,
) -> List[Callable[[Any], Any]]:
"""Fetches all `var_property` of `instance`.
"""Fetches all `eo_property` of `instance`.
Args:
instance (Any): Instance using `var_property`.
instance (Any): Instance using `eo_property`.
Returns:
List[Callable]: List of `var_property` functions
List[Callable]: List of `eo_property` functions
"""
properties: List[Callable] = []
for name in dir(instance.__class__):
attr = getattr(instance.__class__, name, None)
is_prop = isinstance(attr, var_property)
is_prop = isinstance(attr, eo_property)
if not is_prop:
continue

Expand Down
52 changes: 26 additions & 26 deletions src/dictrule/variable.py → src/dictrule/eval_object.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Variable module"""
"""EvalObject module"""

from typing import (
Any,
Expand All @@ -8,35 +8,35 @@
Optional,
)

from .var_property import var_property
from .eo_property import eo_property


class Variable:
class EvalObject:
"""Parses instances with nested values.
Supported value types:
- An item in Variable.PRIMITIVE_TYPES: [
- An item in EvalObject.PRIMITIVE_TYPES: [
int,
float,
str,
]
- list
- set
- dict
- Variable
- EvalObject
Examples:
---------
>>> class Sample:
... @var_property
... @eo_property
... def name(self) -> str:
... return "Zooxy"
... def age(self) -> int:
... return 20
>>> var = Variable.from_var_property_object(Sample())
>>> print(var.name)
>>> obj = EvalObject.from_eo_property_object(Sample())
>>> print(obj.name)
Zooxy
>>> print(hasattr(var, "age"))
>>> print(hasattr(obj, "age"))
False
"""

Expand All @@ -49,26 +49,26 @@ class Variable:
)

@staticmethod
def from_var_property_object(
def from_eo_property_object(
obj: Any,
) -> "Variable":
"""Parses a Variable from obj where properties are decorated with @var_property.
) -> "EvalObject":
"""Parses a EvalObject from obj where properties are decorated with @eo_property.
Args:
obj (Any): The object to parse from @var_property and supported values.
obj (Any): The object to parse from @eo_property and supported values.
Returns:
Variable: The parsed variable.
EvalObject: The parsed object.
"""

return Variable._parse_value(obj)
return EvalObject._parse_value(obj)

def add_object(
self,
obj: Any,
name: str,
):
"""Adds a new object to the Variable instance with a given name.
"""Adds a new object to the EvalObject instance with a given name.
Args:
obj (Any): The object to be added.
Expand All @@ -95,26 +95,26 @@ def _parse_value(
return

parsed_value = None
if type(value) in Variable.PRIMITIVE_TYPES or isinstance(value, Variable):
if type(value) in EvalObject.PRIMITIVE_TYPES or isinstance(value, EvalObject):
parsed_value = value
elif isinstance(value, list):
parsed_value = Variable._parse_list(
parsed_value = EvalObject._parse_list(
value=value,
)
elif isinstance(value, set):
parsed_value = Variable._parse_set(
parsed_value = EvalObject._parse_set(
value=value,
)
elif isinstance(value, dict):
parsed_value = Variable._parse_dict(
parsed_value = EvalObject._parse_dict(
value=value,
)
else:
attrs = var_property.properties(value)
attrs = eo_property.properties(value)
if not attrs:
return None

parsed_value = Variable()
parsed_value = EvalObject()
for attr in attrs:
setattr(parsed_value, attr.__name__, attr.__get__(value))

Expand All @@ -135,7 +135,7 @@ def _parse_list(

new_list: List[Any] = []
for v in value:
parsed_value = Variable._parse_value(v)
parsed_value = EvalObject._parse_value(v)
if parsed_value:
new_list.append(parsed_value)
return new_list
Expand All @@ -155,7 +155,7 @@ def _parse_set(

new_set: Set[Any] = set()
for v in value:
parsed_value = Variable._parse_value(v)
parsed_value = EvalObject._parse_value(v)
if not parsed_value:
continue

Expand All @@ -178,11 +178,11 @@ def _parse_dict(

new_dict: Dict[str, Any] = {}
for k, v in value.items():
parsed_key = Variable._parse_value(k)
parsed_key = EvalObject._parse_value(k)
if not parsed_key:
continue

parsed_value = Variable._parse_value(v)
parsed_value = EvalObject._parse_value(v)
if not parsed_value:
continue

Expand Down
Loading

0 comments on commit 813d10f

Please sign in to comment.