Skip to content

Commit 1f48f43

Browse files
committed
ruff related fixes
1 parent d523372 commit 1f48f43

File tree

19 files changed

+40
-53
lines changed

19 files changed

+40
-53
lines changed

examples/bank_reserves/bank_reserves/agents.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,26 @@ def __init__(self, model, moore, bank, rich_threshold):
6565
def do_business(self):
6666
"""check if person has any savings, any money in wallet, or if the
6767
bank can loan them any money"""
68-
if self.savings > 0 or self.wallet > 0 or self.bank.bank_to_loan > 0:
68+
if (self.savings > 0 or self.wallet > 0 or self.bank.bank_to_loan > 0) and len(self.cell.agents) > 1:
6969
# create list of people at my location (includes self)
70-
71-
# check if other people are at my location
72-
if len(self.cell.agents) > 1:
73-
# set customer to self for while loop condition
74-
customer = self
75-
while customer == self:
76-
customer = self.random.choice(self.cell.agents)
77-
# 50% chance of trading with customer
70+
# set customer to self for while loop condition
71+
customer = self
72+
while customer == self:
73+
customer = self.random.choice(self.cell.agents)
74+
# 50% chance of trading with customer
75+
if self.random.randint(0, 1) == 0:
76+
# 50% chance of trading $5
7877
if self.random.randint(0, 1) == 0:
79-
# 50% chance of trading $5
80-
if self.random.randint(0, 1) == 0:
81-
# give customer $5 from my wallet
82-
# (may result in negative wallet)
83-
customer.wallet += 5
84-
self.wallet -= 5
85-
# 50% chance of trading $2
86-
else:
87-
# give customer $2 from my wallet
88-
# (may result in negative wallet)
89-
customer.wallet += 2
90-
self.wallet -= 2
78+
# give customer $5 from my wallet
79+
# (may result in negative wallet)
80+
customer.wallet += 5
81+
self.wallet -= 5
82+
# 50% chance of trading $2
83+
else:
84+
# give customer $2 from my wallet
85+
# (may result in negative wallet)
86+
customer.wallet += 2
87+
self.wallet -= 2
9188

9289
def balance_books(self):
9390
# check if wallet is negative from trading with customer

examples/bank_reserves/batch_run.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,19 @@
2323
directory from which Python was run. The CSV file will contain the data from
2424
every step of every run.
2525
"""
26-
26+
import itertools
2727
import mesa
2828
import pandas as pd
29-
from bank_reserves.model import BankReservesModel
29+
from bank_reserves.agents import Bank, Person
30+
from bank_reserves.model import (
31+
get_num_mid_agents,
32+
get_num_poor_agents,
33+
get_num_rich_agents,
34+
get_total_loans,
35+
get_total_money,
36+
get_total_savings,
37+
get_total_wallets,
38+
)
3039

3140

3241
def track_params(model):
@@ -39,7 +48,6 @@ def track_run(model):
3948

4049
class BankReservesModel(mesa.Model):
4150
# id generator to track run number in batch run data
42-
id_gen = itertools.count(1)
4351

4452
# grid height
4553
grid_h = 20

examples/charts/charts/agents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def do_business(self):
6767
bank can loan them any money"""
6868
if self.savings > 0 or self.wallet > 0 or self.bank.bank_to_loan > 0:
6969
# create list of people at my location (includes self)
70-
my_cell = [a for a in self.cell.agents if not a == self]
70+
my_cell = [a for a in self.cell.agents if a != self]
7171
# check if other people are at my location
7272
if len(my_cell) > 1:
7373
# set customer to self for while loop condition

examples/charts/charts/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
import mesa
1414
import numpy as np
15+
from mesa.experimental.cell_space import OrthogonalMooreGrid
1516

1617
from .agents import Bank, Person
17-
from mesa.experimental.cell_space import OrthogonalMooreGrid
1818

1919
"""
2020
If you want to perform a parameter sweep, call batch_run.py instead of run.py.

examples/forest_fire/forest_fire/model.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import mesa
2-
32
from mesa.experimental.cell_space import OrthogonalMooreGrid
43

54
from .agent import TreeCell

examples/hex_snowflake/hex_snowflake/cell.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import mesa
21

32
from mesa.experimental.cell_space import FixedAgent
43

examples/hex_snowflake/hex_snowflake/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, width=50, height=50):
2020

2121
# Place a dead cell at each location.
2222
for entry in self.grid.all_cells:
23-
cell = Cell(entry, self)
23+
Cell(entry, self)
2424

2525
# activate the center(ish) cell.
2626
centerish_cell = self.grid[(width // 2, height // 2)]

examples/hotelling_law/hotelling_law/agents.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import random
33

44
import numpy as np
5-
65
from mesa.experimental.cell_space import CellAgent
76

87

examples/hotelling_law/hotelling_law/model.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import numpy as np
44
from mesa import Model
5-
from mesa.agent import AgentSet
65
from mesa.datacollection import DataCollector
76
from mesa.experimental.cell_space import OrthogonalMooreGrid
87

examples/pd_grid/pd_grid/agent.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import mesa
21

32
from mesa.experimental.cell_space import CellAgent
43

@@ -32,9 +31,7 @@ def step(self):
3231
if better than own score."""
3332

3433
# neighbors = self.model.grid.get_neighbors(self.pos, True, include_center=True)
35-
neighbors = list(self.cell.neighborhood.agents) + [
36-
self,
37-
]
34+
neighbors = [*list(self.cell.neighborhood.agents), self]
3835
best_neighbor = max(neighbors, key=lambda a: a.score)
3936
self.next_move = best_neighbor.move
4037

0 commit comments

Comments
 (0)