Skip to content

Commit

Permalink
Use the walrus operator to make nicer if-statements, now that we dont…
Browse files Browse the repository at this point in the history
… have to support python 3.8.
  • Loading branch information
cyberw committed Apr 18, 2024
1 parent 73a7e1a commit 790a9fb
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 22 deletions.
3 changes: 1 addition & 2 deletions locust/argument_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ def parse(self, stream):
result = OrderedDict()

for section in self.sections:
data = configargparse.get_toml_section(config, section)
if data:
if data := configargparse.get_toml_section(config, section):
for key, value in data.items():
if isinstance(value, list):
result[key] = value
Expand Down
3 changes: 1 addition & 2 deletions locust/contrib/fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ def rest(
except Exception as e:
error_lines = []
for l in traceback.format_exc().split("\n"):
m = self._callstack_regex.match(l)
if m:
if m := self._callstack_regex.match(l):
filename = re.sub(r"/(home|Users/\w*)/", "~/", m.group(1))
error_lines.append(filename + ":" + m.group(2) + m.group(3))
short_resp = resp.text[:200] if resp.text else resp.text
Expand Down
3 changes: 1 addition & 2 deletions locust/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def get_html_report(
start_ts = stats.start_time
start_time = datetime.datetime.utcfromtimestamp(start_ts).strftime("%Y-%m-%d %H:%M:%S")

end_ts = stats.last_request_timestamp
if end_ts:
if end_ts := stats.last_request_timestamp:
end_time = datetime.datetime.utcfromtimestamp(end_ts).strftime("%Y-%m-%d %H:%M:%S")
else:
end_time = start_time
Expand Down
3 changes: 1 addition & 2 deletions locust/input_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ def input_listener_func():

try:
while True:
input = poller.poll()
if input:
if input := poller.poll():
for key in key_to_func_map:
if input == key:
key_to_func_map[key]()
Expand Down
6 changes: 2 additions & 4 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ def is_valid_percentile(parameter):
gc.collect() # avoid freezing garbage
gc.freeze() # move all objects to perm gen so ref counts dont get updated
for _ in range(options.processes):
child_pid = gevent.fork()
if child_pid:
if child_pid := gevent.fork():
children.append(child_pid)
logging.debug(f"Started child worker with pid #{child_pid}")
else:
Expand Down Expand Up @@ -315,8 +314,7 @@ def kill_workers(children):

# make sure specified User exists
if options.user_classes:
missing = set(options.user_classes) - set(user_classes.keys())
if missing:
if missing := set(options.user_classes) - set(user_classes.keys()):
logger.error(f"Unknown User(s): {', '.join(missing)}\n")
sys.exit(1)
else:
Expand Down
3 changes: 1 addition & 2 deletions locust/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ def diff_response_time_dicts(latest: dict[int, int], old: dict[int, int]) -> dic
"""
new = {}
for t in latest:
diff = latest[t] - old.get(t, 0)
if diff:
if diff := latest[t] - old.get(t, 0):
new[t] = diff
return new

Expand Down
3 changes: 1 addition & 2 deletions locust/test/test_debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def my_task(self):
pass

def _stop_user():
user = getattr(debug._env, "single_user_instance", None)
if user:
if user := getattr(debug._env, "single_user_instance", None):
user._state = LOCUST_STATE_STOPPING

t = Timer(1, _stop_user)
Expand Down
6 changes: 2 additions & 4 deletions locust/test/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def fast():

@app.route("/slow")
def slow():
delay = request.args.get("delay")
if delay:
if delay := request.args.get("delay"):
gevent.sleep(float(delay))
else:
gevent.sleep(random.choice([0.5, 1, 1.5]))
Expand Down Expand Up @@ -85,8 +84,7 @@ def status_204():

@app.route("/redirect", methods=["GET", "POST"])
def do_redirect():
delay = request.args.get("delay")
if delay:
if delay := request.args.get("delay"):
gevent.sleep(float(delay))
url = request.args.get("url", "/ultra_fast")
return redirect(url)
Expand Down
3 changes: 1 addition & 2 deletions locust/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,7 @@ def update_template_args(self):

options = self.environment.parsed_options

is_distributed = isinstance(self.environment.runner, MasterRunner)
if is_distributed:
if is_distributed := isinstance(self.environment.runner, MasterRunner):
worker_count = self.environment.runner.worker_count
else:
worker_count = 0
Expand Down

0 comments on commit 790a9fb

Please sign in to comment.