Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor for Cleaner Code & Stability #609

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions doc/gui/examples/charts/error-bars-asymmetric.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,14 @@
# y values: [0..n_samples-1]
y = range(0, n_samples)

data = {
# The x series is made of random numbers between 1 and 10
"x": [random.uniform(1, 10) for i in y],
"y": y,
}
data = {"x": [random.uniform(1, 10) for _ in y], "y": y}

options = {
"error_x": {
"type": "data",
# Allows for a 'plus' and a 'minus' error data
"symmetric": False,
# The 'plus' error data is a series of random numbers
"array": [random.uniform(0, 5) for i in y],
# The 'minus' error data is a series of random numbers
"arrayminus": [random.uniform(0, 2) for i in y],
# Color of the error bar
"array": [random.uniform(0, 5) for _ in y],
"arrayminus": [random.uniform(0, 2) for _ in y],
"color": "red",
}
}
Expand Down
2 changes: 1 addition & 1 deletion doc/gui/examples/charts/histogram-cumulative.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from taipy.gui import Gui

# Random data set
data = [random.random() for i in range(500)]
data = [random.random() for _ in range(500)]

options = {
# Enable the cumulative histogram
Expand Down
2 changes: 1 addition & 1 deletion doc/gui/examples/charts/histogram-horizontal.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from taipy.gui import Gui

# Random data set
data = {"Count": [random.random() for i in range(100)]}
data = {"Count": [random.random() for _ in range(100)]}

page = """
# Histograms - Horizontal
Expand Down
2 changes: 1 addition & 1 deletion doc/gui/examples/charts/histogram-nbins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from taipy.gui import Gui

# Random set of 100 samples
samples = {"x": [random.gauss() for i in range(100)]}
samples = {"x": [random.gauss() for _ in range(100)]}

# Use the same data for both traces
data = [samples, samples]
Expand Down
2 changes: 1 addition & 1 deletion doc/gui/examples/charts/histogram-normalized.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from taipy.gui import Gui

# Random data set
data = [random.random() for i in range(100)]
data = [random.random() for _ in range(100)]

# Normalize to show bin probabilities
options = {"histnorm": "probability"}
Expand Down
5 changes: 4 additions & 1 deletion doc/gui/examples/charts/histogram-overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
from taipy.gui import Gui

# Data set made of two series of random numbers
data = [{"x": [random.random() + 1 for i in range(100)]}, {"x": [random.random() + 1.1 for i in range(100)]}]
data = [
{"x": [random.random() + 1 for _ in range(100)]},
{"x": [random.random() + 1.1 for _ in range(100)]},
]

options = [
# First data set displayed as semi-transparent, green bars
Expand Down
2 changes: 1 addition & 1 deletion doc/gui/examples/charts/histogram-simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from taipy import Gui

# Random data set
data = [random.gauss(0, 5) for i in range(1000)]
data = [random.gauss(0, 5) for _ in range(1000)]

page = """
# Histogram - Simple
Expand Down
5 changes: 4 additions & 1 deletion doc/gui/examples/charts/histogram-stacked.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
from taipy.gui import Gui

# Data set made of two series of random numbers
data = {"A": [random.random() for i in range(200)], "B": [random.random() for i in range(200)]}
data = {
"A": [random.random() for _ in range(200)],
"B": [random.random() for _ in range(200)],
}

# Names of the two traces
names = ["A samples", "B samples"]
Expand Down
8 changes: 4 additions & 4 deletions doc/gui/examples/charts/treemap-simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
# Data set: the first 10 elements of the Fibonacci sequence
n_numbers = 10
fibonacci = [0, 1]
for i in range(2, n_numbers):
fibonacci.append(fibonacci[i - 1] + fibonacci[i - 2])

data = {"index": [i for i in range(1, n_numbers + 1)], "fibonacci": fibonacci}
fibonacci.extend(
fibonacci[i - 1] + fibonacci[i - 2] for i in range(2, n_numbers)
)
data = {"index": list(range(1, n_numbers + 1)), "fibonacci": fibonacci}

page = """
# TreeMap - Simple
Expand Down
7 changes: 5 additions & 2 deletions taipy/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ def func_with_doc(section, attribute_name, default, configuration_methods, add_t

for exposed_configuration_method, configuration_method in configuration_methods:
annotation = " @staticmethod\n"
sign = " def " + exposed_configuration_method + str(signature(configuration_method)) + ":\n"
doc = ' """' + configuration_method.__doc__ + '"""\n'
sign = (
f" def {exposed_configuration_method}{str(signature(configuration_method))}"
+ ":\n"
)
doc = f' """{configuration_method.__doc__}' + '"""\n'
content = " pass\n\n"
f.write(annotation + sign + doc + content)
return func(section, attribute_name, default, configuration_methods, add_to_unconflicted_sections)
Expand Down
6 changes: 2 additions & 4 deletions taipy/config/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ def __update_sections(self, entity_config, other_entity_configs):
entity_config[self.DEFAULT_KEY] = other_entity_configs[self.DEFAULT_KEY]
for cfg_id, sub_config in other_entity_configs.items():
if cfg_id != self.DEFAULT_KEY:
if cfg_id in entity_config:
entity_config[cfg_id]._update(sub_config._to_dict(), entity_config.get(self.DEFAULT_KEY))
else:
if cfg_id not in entity_config:
entity_config[cfg_id] = copy(sub_config)
entity_config[cfg_id]._update(sub_config._to_dict(), entity_config.get(self.DEFAULT_KEY))
entity_config[cfg_id]._update(sub_config._to_dict(), entity_config.get(self.DEFAULT_KEY))
self.__point_nested_section_to_self(sub_config)

def __point_nested_section_to_self(self, section):
Expand Down
23 changes: 11 additions & 12 deletions taipy/config/_serializer/_base_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,25 @@ def _stringify(cls, as_dict):
if as_dict is None:
return None
if isinstance(as_dict, Section):
return as_dict.id + ":SECTION"
return f"{as_dict.id}:SECTION"
if isinstance(as_dict, Scope):
return as_dict.name + ":SCOPE"
return f"{as_dict.name}:SCOPE"
if isinstance(as_dict, Frequency):
return as_dict.name + ":FREQUENCY"
return f"{as_dict.name}:FREQUENCY"
if isinstance(as_dict, bool):
return str(as_dict) + ":bool"
return f"{str(as_dict)}:bool"
if isinstance(as_dict, int):
return str(as_dict) + ":int"
return f"{str(as_dict)}:int"
if isinstance(as_dict, float):
return str(as_dict) + ":float"
return f"{str(as_dict)}:float"
if isinstance(as_dict, datetime):
return as_dict.isoformat() + ":datetime"
return f"{as_dict.isoformat()}:datetime"
if isinstance(as_dict, timedelta):
return cls._timedelta_to_str(as_dict) + ":timedelta"
return f"{cls._timedelta_to_str(as_dict)}:timedelta"
if inspect.isfunction(as_dict) or isinstance(as_dict, types.BuiltinFunctionType):
return as_dict.__module__ + "." + as_dict.__name__ + ":function"
return f"{as_dict.__module__}.{as_dict.__name__}:function"
if inspect.isclass(as_dict):
return as_dict.__module__ + "." + as_dict.__qualname__ + ":class"
return f"{as_dict.__module__}.{as_dict.__qualname__}:class"
if isinstance(as_dict, dict):
return {str(key): cls._stringify(val) for key, val in as_dict.items()}
if isinstance(as_dict, list):
Expand Down Expand Up @@ -115,8 +115,7 @@ def _pythonify(cls, val):
r"^(.+):(\bbool\b|\bstr\b|\bint\b|\bfloat\b|\bdatetime\b||\btimedelta\b|"
r"\bfunction\b|\bclass\b|\bSCOPE\b|\bFREQUENCY\b|\bSECTION\b)?$"
)
match = re.fullmatch(TYPE_PATTERN, str(val))
if match:
if match := re.fullmatch(TYPE_PATTERN, str(val)):
actual_val = match.group(1)
dynamic_type = match.group(2)
if dynamic_type == "SECTION":
Expand Down
21 changes: 10 additions & 11 deletions taipy/config/checker/_checkers/_config_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ def _check_children(
config_value,
f"{config_key} field of {parent_config_class.__name__} `{config_id}` is empty.",
)
else:
if not (
(isinstance(config_value, List) or isinstance(config_value, Set))
and all(map(lambda x: isinstance(x, child_config_class), config_value))
):
self._error(
config_key,
config_value,
f"{config_key} field of {parent_config_class.__name__} `{config_id}` must be populated with a list "
f"of {child_config_class.__name__} objects.",
)
elif not (
(isinstance(config_value, (List, Set)))
and all(map(lambda x: isinstance(x, child_config_class), config_value))
):
self._error(
config_key,
config_value,
f"{config_key} field of {parent_config_class.__name__} `{config_id}` must be populated with a list "
f"of {child_config_class.__name__} objects.",
)

def _check_existing_config_id(self, config):
if not config.id:
Expand Down
2 changes: 1 addition & 1 deletion taipy/config/common/_config_blocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _check_if_is_blocking(*args, **kwargs):
" modifying the Configuration. For more information, please refer to:"
" https://docs.taipy.io/en/latest/manuals/running_services/#running-core."
)
cls.__logger.error("ConfigurationUpdateBlocked: " + error_message)
cls.__logger.error(f"ConfigurationUpdateBlocked: {error_message}")
raise ConfigurationUpdateBlocked(error_message)

return f(*args, **kwargs)
Expand Down
5 changes: 2 additions & 3 deletions taipy/config/common/_template_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def _replace_templates(cls, template, type=str, required=True, default=None):
def _replace_template(cls, template, type, required, default):
if "ENV" not in str(template):
return template
match = re.fullmatch(cls._PATTERN, str(template))
if match:
if match := re.fullmatch(cls._PATTERN, str(template)):
var = match.group(1)
dynamic_type = match.group(3)
val = os.environ.get(var)
Expand Down Expand Up @@ -77,7 +76,7 @@ def _to_bool(val: str) -> bool:
possible_values = ["true", "false"]
if str.lower(val) not in possible_values:
raise InconsistentEnvVariableError("{val} is not a Boolean.")
return str.lower(val) == "true" or not (str.lower(val) == "false")
return str.lower(val) == "true" or str.lower(val) != "false"

@staticmethod
def _to_int(val: str) -> int:
Expand Down
20 changes: 9 additions & 11 deletions taipy/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,10 @@ def _register_default(cls, default_section: Section):
cls._default_config._unique_sections[default_section.name]._update(default_section._to_dict())
else:
cls._default_config._unique_sections[default_section.name] = default_section
elif def_sections := cls._default_config._sections.get(default_section.name, None):
def_sections[default_section.id] = default_section
else:
if def_sections := cls._default_config._sections.get(default_section.name, None):
def_sections[default_section.id] = default_section
else:
cls._default_config._sections[default_section.name] = {default_section.id: default_section}
cls._default_config._sections[default_section.name] = {default_section.id: default_section}
cls._serializer._section_class[default_section.name] = default_section.__class__ # type: ignore
cls.__json_serializer._section_class[default_section.name] = default_section.__class__ # type: ignore
cls._compile_configs()
Expand All @@ -195,14 +194,13 @@ def _register(cls, section):
cls._python_config._unique_sections[section.name]._update(section._to_dict())
else:
cls._python_config._unique_sections[section.name] = section
else:
if sections := cls._python_config._sections.get(section.name, None):
if sections.get(section.id, None):
sections[section.id]._update(section._to_dict())
else:
sections[section.id] = section
elif sections := cls._python_config._sections.get(section.name, None):
if sections.get(section.id, None):
sections[section.id]._update(section._to_dict())
else:
cls._python_config._sections[section.name] = {section.id: section}
sections[section.id] = section
else:
cls._python_config._sections[section.name] = {section.id: section}
cls._serializer._section_class[section.name] = section.__class__
cls.__json_serializer._section_class[section.name] = section.__class__
cls._compile_configs()
Expand Down
47 changes: 26 additions & 21 deletions taipy/core/_backup/_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,32 @@ def _append_to_backup_file(new_file_path: str):


def _remove_from_backup_file(to_remove_file_path: str):
if preserve_file_path := os.getenv(__BACKUP_FILE_PATH_ENVIRONMENT_VARIABLE_NAME, None):
storage_folder = os.path.abspath(Config.core.storage_folder) + os.sep
if not os.path.abspath(to_remove_file_path).startswith(storage_folder):
try:
with open(preserve_file_path, "r+") as f:
old_backup = f.read()
to_remove_file_path = to_remove_file_path + "\n"

# To avoid removing the file path of different data nodes that are pointing
# to the same file. We will only replace the file path only once.
if old_backup.startswith(to_remove_file_path):
new_backup = old_backup.replace(to_remove_file_path, "", 1)
else:
new_backup = old_backup.replace("\n" + to_remove_file_path, "\n", 1)

if new_backup is not old_backup:
f.seek(0)
f.write(new_backup)
f.truncate()
except Exception:
pass
if not (
preserve_file_path := os.getenv(
__BACKUP_FILE_PATH_ENVIRONMENT_VARIABLE_NAME, None
)
):
return
storage_folder = os.path.abspath(Config.core.storage_folder) + os.sep
if not os.path.abspath(to_remove_file_path).startswith(storage_folder):
try:
with open(preserve_file_path, "r+") as f:
old_backup = f.read()
to_remove_file_path += "\n"

# To avoid removing the file path of different data nodes that are pointing
# to the same file. We will only replace the file path only once.
if old_backup.startswith(to_remove_file_path):
new_backup = old_backup.replace(to_remove_file_path, "", 1)
else:
new_backup = old_backup.replace("\n" + to_remove_file_path, "\n", 1)

if new_backup is not old_backup:
f.seek(0)
f.write(new_backup)
f.truncate()
except Exception:
pass


def _replace_in_backup_file(old_file_path: str, new_file_path: str):
Expand Down
4 changes: 2 additions & 2 deletions taipy/core/_core_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ def parse_arguments(cls):
@classmethod
def __add_taipy_prefix(cls, key: str):
if key.startswith("--no-"):
return key[:5] + "taipy-" + key[5:]
return f"{key[:5]}taipy-{key[5:]}"

return key[:2] + "taipy-" + key[2:]
return f"{key[:2]}taipy-{key[2:]}"
16 changes: 7 additions & 9 deletions taipy/core/_entity/_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, src: _Node, dest: _Node):

class _DAG:
def __init__(self, dag: nx.DiGraph):
self._sorted_nodes = list(nodes for nodes in nx.topological_generations(dag))
self._sorted_nodes = list(nx.topological_generations(dag))
self._length, self._width = self.__compute_size()
self._grid_length, self._grid_width = self.__compute_grid_size()
self._nodes = self.__compute_nodes()
Expand All @@ -54,7 +54,7 @@ def edges(self) -> List[_Edge]:
return self._edges

def __compute_size(self) -> Tuple[int, int]:
return len(self._sorted_nodes), max([len(i) for i in self._sorted_nodes])
return len(self._sorted_nodes), max(len(i) for i in self._sorted_nodes)

def __compute_grid_size(self) -> Tuple[int, int]:
if self._width == 1:
Expand All @@ -65,8 +65,7 @@ def __compute_grid_size(self) -> Tuple[int, int]:

def __compute_nodes(self) -> Dict[str, _Node]:
nodes = {}
x = 0
for same_lvl_nodes in self._sorted_nodes:
for x, same_lvl_nodes in enumerate(self._sorted_nodes):
lcl_wdt = len(same_lvl_nodes)
is_max = lcl_wdt != self.width
if self.width != 1:
Expand All @@ -77,14 +76,13 @@ def __compute_nodes(self) -> Dict[str, _Node]:
for node in same_lvl_nodes:
y += y_incr
nodes[node.id] = _Node(node, x, y)
x += 1
return nodes

def __compute_edges(self, dag) -> List[_Edge]:
edges = []
for edge in dag.edges():
edges.append(_Edge(self.nodes[edge[0].id], self.nodes[edge[1].id]))
return edges
return [
_Edge(self.nodes[edge[0].id], self.nodes[edge[1].id])
for edge in dag.edges()
]

@staticmethod
def __lcm(*integers) -> int:
Expand Down
2 changes: 1 addition & 1 deletion taipy/core/_entity/_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class _Entity:

def __enter__(self):
self._is_in_context = True
self._in_context_attributes_changed_collector = list()
self._in_context_attributes_changed_collector = []
return self

def __exit__(self, exc_type, exc_value, exc_traceback):
Expand Down
Loading