Skip to content

Commit

Permalink
style(ruff)!: switch to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
squillero committed Jun 17, 2024
1 parent 4421674 commit 848cf1b
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ Test programs [do not need](https://evolution.berkeley.edu/) to be designed, but
* Byron is currently in [alpha](https://en.wikipedia.org/wiki/Software_release_life_cycle#Alpha) and under active development
* The default branch is always the more stable
* Do not clone experimental branches `exp/*` unless you really know what you are doing
* Follow this [style guide](https://github.com/squillero/style/blob/master/python.md) and keep the code formatted with [Black](https://black.readthedocs.io/en/stable/)
* Follow this [style guide](https://github.com/squillero/style/blob/master/python.md) and keep the code formatted with [Ruff formatter](https://docs.astral.sh/ruff/formatter/)
* Follow this [convention](https://github.com/squillero/style/blob/master/git.md) when drafting commit messages
* Write as few lines of code and as many lines of comments as possible (ie. use builtins, exploit generators and list comprehension)
* Be [paranoid](https://cad-polito-it.github.io/byron/paranoia) (cit. *"I need someone to show me the things"*)
* Use [pytest](https://docs.pytest.org/) and [Coverage.py](https://coverage.readthedocs.io/) for unit testing (ie. `coverage run --module pytest --all`)
* Use [ruff](https://docs.astral.sh/ruff/) for linting and [mypy](https://mypy-lang.org/) for additional type checking
* Use [Ruff](https://docs.astral.sh/ruff/linter/) for linting and [mypy](https://mypy-lang.org/) for additional type checking
* Use [direnv](https://direnv.net) to patch environment variables
* And remember that it may be wise to contact [Giovanni](https://github.com/squillero) before trying to change anything

Expand Down
4 changes: 2 additions & 2 deletions byron/classes/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ def evaluate_population(self, population: Population) -> None:
raise RuntimeError(f"Process returned empty stdout (stderr: '{result.stderr}')")
else:
results = list(filter(lambda s: bool(s), result.stdout.split("\n")))
assert len(results) == len(
individuals
assert (
len(results) == len(individuals)
), f"{PARANOIA_VALUE_ERROR}: Number of results and number of individual mismatch: found {len(results)} expected {len(individuals)}"
for ind, line in zip_longest(individuals, results):
value = [float(r) for r in line.split()]
Expand Down
3 changes: 3 additions & 0 deletions byron/classes/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class FrameSequence:
--------
`sequence` factory function
"""

pass


Expand All @@ -84,6 +85,7 @@ class FrameAlternative:
--------
`altrnative` factory function
"""

pass


Expand All @@ -94,4 +96,5 @@ class FrameMacroBunch:
--------
`bunch` factory function
"""

pass
1 change: 1 addition & 0 deletions byron/classes/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

class Node(int):
r"""Simple helper to guarantee Node ids uniqueness"""

__slots__ = []
__LAST_BYRON_NODE = 0

Expand Down
3 changes: 2 additions & 1 deletion byron/classes/selement.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

# =[ HISTORY ]===============================================================
# v1 / June 2023 / Squillero (GX)
"""THE FIRST ONE IS FOR THE DEFINING CLASSES AND THE SECOND ONE IS FOR THE DEFINING METACLASS. """
"""THE FIRST ONE IS FOR THE DEFINING CLASSES AND THE SECOND ONE IS FOR THE DEFINING METACLASS."""

__all__ = ['SElement', 'SElementMeta']

from typing import Callable, Sequence, Optional
Expand Down
1 change: 1 addition & 0 deletions byron/evaluator_.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
"""
Base name for Evaluator's Classes
"""

from byron.classes.evaluator import *
2 changes: 1 addition & 1 deletion byron/global_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
'DEFAULT_EXTRA_PARAMETERS',
'DEFAULT_OPTIONS',
'LOG_LAPSES',
'SE_DIRECTORY'
'SE_DIRECTORY',
]

import logging
Expand Down
15 changes: 6 additions & 9 deletions examples/knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
MAX_WEIGHT = 50
WEIGHTS = [31, 10, 20, 19, 4, 3, 6]
VALUES = [70, 20, 39, 37, 7, 5, 10]
#max value: 107
# max value: 107


@byron.fitness_function
Expand All @@ -43,22 +43,19 @@ def fitness(genotype):


def main():
macro1 = byron.f.macro('{v}', v=byron.f.integer_parameter(1, 3))
macro2 = byron.f.macro('{v}', v=byron.f.integer_parameter(3, 6))
macro3 = byron.f.macro('{v}', v=byron.f.integer_parameter(6, 8))

macro1 = byron.f.macro('{v}', v=byron.f.integer_parameter(1,3))
macro2 = byron.f.macro('{v}', v=byron.f.integer_parameter(3,6))
macro3 = byron.f.macro('{v}', v=byron.f.integer_parameter(6,8))

top_frame = byron.f.bunch([macro1, macro2, macro3], (1,181))
top_frame = byron.f.bunch([macro1, macro2, macro3], (1, 181))

evaluator = byron.evaluator.PythonEvaluator(fitness, strip_phenotypes=True)
# evaluator = byron.evaluator.PythonEvaluator(fitness, strip_phenotypes=True, backend='thread_pool')
# evaluator = byron.evaluator.PythonEvaluator(fitness, strip_phenotypes=True, backend='joblib')

byron.logger.info("main: Using %s", evaluator)

population = byron.ea.adaptive_ea(
top_frame, evaluator, max_generation=500, lambda_=20, mu=10, top_n=1, lifespan=2
)
population = byron.ea.adaptive_ea(top_frame, evaluator, max_generation=500, lambda_=20, mu=10, top_n=1, lifespan=2)

byron.sys.log_operators()

Expand Down
2 changes: 2 additions & 0 deletions examples/onemax/onemax-classic/onemax-classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
NUM_BITS = 50

from icecream import ic


@byron.fitness_function
def fitness(genotype):
"""Vanilla 1-max"""
Expand Down
6 changes: 4 additions & 2 deletions examples/onemax/onemax-go/onemax-go.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ def main():
# evaluator = byron.evaluator.PythonEvaluator(dummy_fitness)

byron.f.set_global_option('$dump_node_info', True)
#final_population = byron.ea.vanilla_ea(top_frame, evaluator, max_generation=1_000, mu=50, lambda_=20, max_fitness=64.0)
final_population = byron.ea.adaptive_ea(top_frame, evaluator, max_generation=1_000, mu=50, lambda_=20, max_fitness=64.0)
# final_population = byron.ea.vanilla_ea(top_frame, evaluator, max_generation=1_000, mu=50, lambda_=20, max_fitness=64.0)
final_population = byron.ea.adaptive_ea(
top_frame, evaluator, max_generation=1_000, mu=50, lambda_=20, max_fitness=64.0
)

# byron.logger.info("[b]POPULATION[/b]")
# max_f = max(i.fitness for i in final_population.individuals)
Expand Down
1 change: 1 addition & 0 deletions experiments/dimitri.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

generators = [op for op in byron.sys.get_operators() if op.num_parents is None]


def get_base_macros():
macros = list()
macros.append(byron.f.macro('int 0x{num:X}', num=byron.f.integer_parameter(0, 2**32)))
Expand Down
4 changes: 3 additions & 1 deletion experiments/i.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
imm=byron.f.integer_parameter(0, 2**16),
)

section_proc = byron.f.sequence([byron.f.macro("proc {_node} near:"), byron.f.bunch(asm_instruction, 3), byron.f.macro("ret")], name="zap")
section_proc = byron.f.sequence(
[byron.f.macro("proc {_node} near:"), byron.f.bunch(asm_instruction, 3), byron.f.macro("ret")], name="zap"
)

asm_call = byron.f.macro("call {target}", target=byron.f.global_reference("zap", creative_zeal=1))

Expand Down
10 changes: 7 additions & 3 deletions experiments/zap.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from icecream import ic


zap = 23
ic(zap)

ic(zap)
print(
"Hello, World! Hello, World! Hello, WorldHello, World! Hello, World! Hello, WorldHello, World! Hello, World! Hello, WorldHello, World! Hello, World! Hello,"
+ " WorldHello, World! Hello, World! Hello,"
+ " World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!"
)
for _ in range(10):
ic()
1
11 changes: 7 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@
line-length = 120

[tool.ruff.lint]
extend-select = [
"UP", # pyupgrade
"D", # pydocstyle
]
#extend-select = [
# "UP", # pyupgrade
# "D", # pydocstyle
#]

[tool.ruff.format]
# Prefer single quotes over double quotes.
quote-style = "preserve"

[tool.bumpver]
current_version = "0.8a1.dev50"
Expand Down

0 comments on commit 848cf1b

Please sign in to comment.