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

Use Assignment Expression (Walrus) In Conditional #42

Closed
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
6 changes: 2 additions & 4 deletions sigoptlite/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ def validate_input_dict(cls, input_dict):
def create_object(cls, **input_dict):
cls.set_object(input_dict, "assignments", LocalAssignments)
cls.set_list_of_objects(input_dict, field="values", local_class=MetricEvaluationBuilder)
values = input_dict.get("values")
if values:
if values := input_dict.get("values"):
input_dict["metric_evaluations"] = {me.name: me for me in values}
input_dict.pop("values", None)
cls.set_object(input_dict, "task", LocalTaskBuilder)
Expand All @@ -273,8 +272,7 @@ class MetricEvaluationBuilder(BuilderBase):
def validate_input_dict(cls, input_dict):
assert isinstance(input_dict["name"], str)
assert isinstance(input_dict["value"], (int, float))
value_stddev = input_dict.get("value_stddev", None)
if value_stddev is not None:
if (value_stddev := input_dict.get("value_stddev", None)) is not None:
assert isinstance(input_dict["value_stddev"], (int, float))
assert input_dict["value_stddev"] >= 0
assert set(input_dict.keys()) == {"name", "value", "value_stddev"}
Expand Down
3 changes: 1 addition & 2 deletions sigoptlite/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def path_to_route(self, path, method):

def request(self, method, path, data, headers):
route = self.path_to_route(path, method)
handler = self.routes.get(route, {}).get(method)
if handler is None:
if (handler := self.routes.get(route, {}).get(method)) is None:
raise Exception(f"{PRODUCT_NAME} only supports the following routes: {' '.join(self.routes.keys())}")
return handler(data)
3 changes: 1 addition & 2 deletions sigoptlite/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ def get_optimized_measurements_for_maximization(self, experiment):
return [self.get_value_for_maximization(metric) for metric in experiment.optimized_metrics]

def get_value_for_maximization(self, metric):
value = self.get_metric_evaluation_by_name(metric.name).value
if value is None:
if (value := self.get_metric_evaluation_by_name(metric.name).value) is None:
raise Exception(f"Metric `{metric.name}` is not in observation data.")
if metric.is_minimized:
return -value
Expand Down
3 changes: 1 addition & 2 deletions sigoptlite/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ def make_assignments_from_point(experiment, point):
def get_point_from_assignments(experiment, assignments):
point = numpy.empty(experiment.dimension)
for i, parameter in enumerate(experiment.parameters):
parameter_value = assignments.get(parameter.name, None)
if parameter_value is None:
if (parameter_value := assignments.get(parameter.name, None)) is None:
parameter_value = replacement_value_if_missing(parameter)
if parameter.has_log_transformation:
parameter_value = numpy.log10(parameter_value)
Expand Down
9 changes: 3 additions & 6 deletions sigoptlite/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ def validate_experiment(experiment, cls_name):
if not experiment.parallel_bandwidth == 1:
raise ValueError(f"{cls_name} must have parallel_bandwidth == 1")

observation_budget = experiment.observation_budget
if observation_budget is None:
if (observation_budget := experiment.observation_budget) is None:
if experiment.num_solutions > 1:
raise ValueError(f"observation_budget is required for a {cls_name} with multiple solutions")
if experiment.requires_pareto_frontier_optimization:
Expand Down Expand Up @@ -250,8 +249,7 @@ def validate_experiment(experiment, cls_name):
validate_conditionals_for_experiment(experiment)

# Check feature viability of multitask
tasks = experiment.tasks
if tasks:
if tasks := experiment.tasks:
if experiment.requires_pareto_frontier_optimization:
raise ValueError(f"{cls_name} cannot have both tasks and multiple optimized metrics")
if experiment.has_constraint_metrics:
Expand Down Expand Up @@ -441,8 +439,7 @@ def validate_constraints_for_experiment(experiment):

term_types = []
for term in terms:
coeff = term.weight
if coeff == 0:
if (coeff := term.weight) == 0:
continue
name = term.name
if name in integer_params_names:
Expand Down
Loading