Skip to content

Commit

Permalink
Fix Ruff-reported errors manually
Browse files Browse the repository at this point in the history
  • Loading branch information
rht authored and tpike3 committed Feb 10, 2023
1 parent 09cf299 commit cea722d
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 31 deletions.
3 changes: 2 additions & 1 deletion mesa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
__title__ = "mesa"
__version__ = "1.1.1"
__license__ = "Apache 2.0"
__copyright__ = f"Copyright {datetime.date.today().year} Project Mesa Team"
_this_year = datetime.datetime.now(tz=datetime.timezone.utc).date().year
__copyright__ = f"Copyright {_this_year} Project Mesa Team"
2 changes: 1 addition & 1 deletion mesa/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def runserver(project):

with open("run.py") as f:
code = compile(f.read(), "run.py", "exec")
exec(code, {}, {})
exec(code, {}, {}) # noqa: S102


@click.command()
Expand Down
6 changes: 3 additions & 3 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,15 +1019,15 @@ def out_of_bounds(self, pos: FloatCoordinate) -> bool:
class NetworkGrid:
"""Network Grid where each node contains zero or more agents."""

def __init__(self, G: Any) -> None:
def __init__(self, g: Any) -> None:
"""Create a new network.
Args:
G: a NetworkX graph instance.
"""
self.G = G
self.G = g
for node_id in self.G.nodes:
G.nodes[node_id]["agent"] = self.default_val()
g.nodes[node_id]["agent"] = self.default_val()

@staticmethod
def default_val() -> list:
Expand Down
11 changes: 5 additions & 6 deletions mesa/visualization/UserParam.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,11 @@ def value(self, value):
self._value = self.min_value
elif self._value > self.max_value:
self._value = self.max_value
elif self.param_type == self.CHOICE:
if self._value not in self.choices:
print(
"Selected choice value not in available choices, selected first choice from 'choices' list"
)
self._value = self.choices[0]
elif (self.param_type == self.CHOICE) and self._value not in self.choices:
print(
"Selected choice value not in available choices, selected first choice from 'choices' list"
)
self._value = self.choices[0]

@property
def json(self):
Expand Down
10 changes: 5 additions & 5 deletions mesa/visualization/modules/BarChartVisualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ def render(self, model):
if self.scope == "agent":
df = data_collector.get_agent_vars_dataframe().astype("float")
latest_step = df.index.levels[0][-1]
labelStrings = [f["Label"] for f in self.fields]
dict = df.loc[latest_step].T.loc[labelStrings].to_dict()
label_strings = [f["Label"] for f in self.fields]
dict = df.loc[latest_step].T.loc[label_strings].to_dict()
current_values = list(dict.values())

elif self.scope == "model":
outDict = {}
out_dict = {}
for s in self.fields:
name = s["Label"]
try:
val = data_collector.model_vars[name][-1]
except (IndexError, KeyError):
val = 0
outDict[name] = val
current_values.append(outDict)
out_dict[name] = val
current_values.append(out_dict)
else:
raise ValueError("scope must be 'agent' or 'model'")
return current_values
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ extend-ignore = [
"S101", # Use of `assert` detected
"B017", # `assertRaises(Exception)` should be considered evil TODO
"PGH004", # Use specific rule codes when using `noqa` TODO
"B905", # `zip()` without an explicit `strict=` parameter
"N802", # Function name should be lowercase
]
extend-exclude = ["docs", "build"]
# Hardcode to Python 3.8.
Expand Down
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
os.makedirs(external_dir_single, exist_ok=True)


def ensure_JS_dep(dirname, url):
def ensure_js_dep(dirname, url):
dst_path = os.path.join(external_dir, dirname)
if os.path.isdir(dst_path):
# Do nothing if already downloaded
Expand All @@ -50,7 +50,7 @@ def ensure_JS_dep(dirname, url):
print("Done")


def ensure_JS_dep_single(url, out_name=None):
def ensure_js_dep_single(url, out_name=None):
# Used for downloading e.g. D3.js single file
if out_name is None:
out_name = url.split("/")[-1]
Expand All @@ -67,29 +67,29 @@ def ensure_JS_dep_single(url, out_name=None):

# Ensure Bootstrap
bootstrap_version = "5.1.3"
ensure_JS_dep(
ensure_js_dep(
f"bootstrap-{bootstrap_version}-dist",
f"https://github.com/twbs/bootstrap/releases/download/v{bootstrap_version}/bootstrap-{bootstrap_version}-dist.zip",
)

# Ensure Bootstrap Slider
bootstrap_slider_version = "11.0.2"
ensure_JS_dep(
ensure_js_dep(
f"bootstrap-slider-{bootstrap_slider_version}",
f"https://github.com/seiyria/bootstrap-slider/archive/refs/tags/v{bootstrap_slider_version}.zip",
)

# Important: when updating the D3 version, make sure to update the constant
# D3_JS_FILE in mesa/visualization/ModularVisualization.py.
d3_version = "7.4.3"
ensure_JS_dep_single(
ensure_js_dep_single(
f"https://cdnjs.cloudflare.com/ajax/libs/d3/{d3_version}/d3.min.js",
out_name=f"d3-{d3_version}.min.js",
)
# Important: Make sure to update CHART_JS_FILE in
# mesa/visualization/ModularVisualization.py.
chartjs_version = "3.6.1"
ensure_JS_dep_single(
ensure_js_dep_single(
f"https://cdn.jsdelivr.net/npm/chart.js@{chartjs_version}/dist/chart.min.js",
out_name=f"chart-{chartjs_version}.min.js",
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_examples(self):
f"{example.replace('-', '_')}.server"
)
server.server.render_model()
Model = getattr(mod, classcase(example))
model = Model()
model_class = getattr(mod, classcase(example))
model = model_class()
for _ in range(10):
model.step()
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def tearDown(self):
"Skipping test_run, because examples folder was moved. More discussion needed."
)
def test_run(self):
with patch("mesa.visualization.ModularServer") as ModularServer:
with patch("mesa.visualization.ModularServer") as ModularServer: # noqa: N806
example_dir = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../examples/wolf_sheep")
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def setUp(self):
"""
Create a test network grid and populate with Mock Agents.
"""
G = nx.cycle_graph(TestSingleNetworkGrid.GRAPH_SIZE)
G = nx.cycle_graph(TestSingleNetworkGrid.GRAPH_SIZE) # noqa: N806
self.space = NetworkGrid(G)
self.agents = []
for i, pos in enumerate(TEST_AGENTS_NETWORK_SINGLE):
Expand Down Expand Up @@ -408,7 +408,7 @@ def setUp(self):
"""
Create a test network grid and populate with Mock Agents.
"""
G = nx.complete_graph(TestMultipleNetworkGrid.GRAPH_SIZE)
G = nx.complete_graph(TestMultipleNetworkGrid.GRAPH_SIZE) # noqa: N806
self.space = NetworkGrid(G)
self.agents = []
for i, pos in enumerate(TEST_AGENTS_NETWORK_MULTIPLE):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_random_activation_step_steps_each_agent(self):
model.step()
agent_steps = [i.steps for i in model.schedule.agents]
# one step for each of 2 agents
assert all(map(lambda x: x == 1, agent_steps))
assert all(x == 1 for x in agent_steps)

def test_intrastep_remove(self):
"""
Expand All @@ -214,8 +214,8 @@ def test_simultaneous_activation_step_steps_and_advances_each_agent(self):
# one step for each of 2 agents
agent_steps = [i.steps for i in model.schedule.agents]
agent_advances = [i.advances for i in model.schedule.agents]
assert all(map(lambda x: x == 1, agent_steps))
assert all(map(lambda x: x == 1, agent_advances))
assert all(x == 1 for x in agent_steps)
assert all(x == 1 for x in agent_advances)


class TestRandomActivationByType(TestCase):
Expand Down Expand Up @@ -254,7 +254,7 @@ def test_random_activation_step_steps_each_agent(self):
model.step()
agent_steps = [i.steps for i in model.schedule.agents]
# one step for each of 2 agents
assert all(map(lambda x: x == 1, agent_steps))
assert all(x == 1 for x in agent_steps)

def test_add_non_unique_ids(self):
"""
Expand Down

0 comments on commit cea722d

Please sign in to comment.