Skip to content

Commit

Permalink
fix bug write data to default_value.
Browse files Browse the repository at this point in the history
make __repr__ clearly
  • Loading branch information
TinDang97 committed Nov 19, 2020
1 parent 8ffb96c commit 7c3244a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
33 changes: 20 additions & 13 deletions pyopt/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
]

import inspect
from copy import deepcopy
from typing import Optional, Union, Callable, Any, Sequence, Tuple

SETFUNC_OPTION_TYPE = (bool, type(None), staticmethod, classmethod)
Expand Down Expand Up @@ -56,7 +57,7 @@ def __init__(self, name: str,
Args:
name: Key of option. Auto fill if not set.
set_filter: Filter value before set.
default_value: Default value of option. It isn't filtered by filter.
default_value: Default value of option.
doc: docstring
"""
self.__name = name
Expand Down Expand Up @@ -100,13 +101,18 @@ def _wrap_setter(_self, value):
return OptionsBase.fset(_self, self.__name, value)

def _wrap_getter(_self):
return OptionsBase.fget(_self, self.__name)
try:
return OptionsBase.fget(_self, self.__name)
except UnsetOption as e:
if self.__default_value is not None:
_wrap_setter(_self, deepcopy(self.__default_value))
return OptionsBase.fget(_self, self.__name)
raise e

def _wrap_deleter(_self):
if self.__default_value is not None:
return OptionsBase.fset(_self, self.__name, self.__default_value)
_wrap_setter(_self, deepcopy(self.__default_value))
return OptionsBase.fdelete(_self, self.__name)

super().__init__(_wrap_getter, _wrap_setter, _wrap_deleter)

def __repr__(self):
Expand Down Expand Up @@ -155,7 +161,7 @@ def __init__(self, options: Optional['OptionsBase'] = None):
name_opts = set()
for name, opt in self.options():
if opt.default_value is not None:
self.__opts[opt.name] = opt.default_value
setattr(self, name, deepcopy(opt.default_value))

if opt.name in name_opts:
raise DuplicateOptionName(f"Duplicate option's name: `{opt.name}` at {name} option")
Expand All @@ -166,30 +172,31 @@ def __repr__(self):
_repr = list()
for attr, opt in self.options():
# attribute name
_repr.append(f'\n\t- {attr:15}')
_repr.append(f'\n\t-> {attr:12}')

# option name + value
try:
v = self.__getattribute__(attr)
v = getattr(self, attr)
if isinstance(v, OptionsBase):
_repr.append(v.__class__.__name__)
if v:
_repr.append(f"({v.__str__()})")
else:
_repr.append(f"({None})")
else:
if isinstance(v, str):
v = f'"{v}"'
if not isinstance(v, (int, float)):
v = f"'{v}'"
except UnsetOption:
v = "Unset"
v = "[UNSET]"

v = f'| "{opt.name}" = {v}'
v = f'| -{opt.name} {v}'
_repr.append(f"{v:30}")

# doc
if opt.doc:
_repr.append(f' | Doc: {opt.doc}')
return f"{self.__class__.__name__}: {''.join(_repr)}\n"
_repr.append(f' | {opt.doc}')
return f"{self.__class__.__name__.upper()}" \
f"{''.join(_repr)}\n"

def __str__(self):
return self.__opts.__str__()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='python-options',
version='1.0.0-2',
version='1.0.0-3',
packages=find_packages(),
url='https://github.com/TinDang97/python-options',
license='LICENSE',
Expand Down

0 comments on commit 7c3244a

Please sign in to comment.