Skip to content

Commit

Permalink
Refactor code for better readability and maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
fvergaracl committed Mar 11, 2024
1 parent ff311e2 commit fd17646
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
7 changes: 3 additions & 4 deletions app/engine/all_engine_strategies.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import importlib
from app.engine.check_base_strategy_class import check_class_methods_and_variables
from app.engine.check_base_strategy_class import (
check_class_methods_and_variables
)


def all_engine_strategies():
Expand All @@ -14,9 +16,6 @@ def all_engine_strategies():
strategy = file[:-3]
strategies.append(strategy)

# import all strategies and check if they are valid with check_base_strategy_class
# if not, remove them from the list

all_strategies_classes = []
for strategy in strategies:

Expand Down
7 changes: 6 additions & 1 deletion app/engine/base_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def __init__(
self.variable_basic_points = variable_basic_points
self.variable_bonus_points = variable_bonus_points

def get_strategy_id(self):
# get filename of this file
return self.__class__.__name__

def get_strategy_name(self):
return self.strategy_name

Expand Down Expand Up @@ -42,7 +46,8 @@ def set_variables(self, new_variables):
return variables_changed

def get_variables(self):
return {k: v for k, v in self.__dict__.items() if k.startswith("variable_")}
return {k: v for k, v in self.__dict__.items() if
k.startswith("variable_")}

def get_variable(self, variable_name):
if hasattr(self, variable_name):
Expand Down
1 change: 1 addition & 0 deletions app/engine/check_base_strategy_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def check_class_methods_and_variables(Class_to_check):
instance = Class_to_check()

methods = [
"get_strategy_id",
"get_strategy_name", "get_strategy_description",
"get_strategy_name_slug", "get_strategy_version",
"get_variable_basic_points", "get_variable_bonus_points",
Expand Down
5 changes: 4 additions & 1 deletion app/services/strategy_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from app.repository.strategy_repository import StrategyRepository
from app.services.base_service import BaseService
from app.engine.all_engine_strategies import all_engine_strategies
import inspect


class StrategyService(BaseService):
Expand All @@ -14,8 +15,10 @@ def list_all_strategies(self):
response = []
for strategy in all_unclean_strategies:
# id = filename
archivo_clase = inspect.getfile(strategy)
response.append({
"id": strategy.get_strategy_id(),

"id": archivo_clase,
"name": strategy.get_strategy_name(),
"description": strategy.get_strategy_description(),
"nameSlug": strategy.get_strategy_name_slug(),
Expand Down

0 comments on commit fd17646

Please sign in to comment.