Skip to content

Commit

Permalink
Merge pull request #2032 from locustio/pyupgrade_remove-python2-isms
Browse files Browse the repository at this point in the history
Ran pyupgrade on the code base, removing various "Python2-isms".
  • Loading branch information
cyberw authored Feb 25, 2022
2 parents d3b43c5 + 96e13d1 commit 04264f2
Show file tree
Hide file tree
Showing 41 changed files with 148 additions and 195 deletions.
4 changes: 4 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Migrate code style to Black
7c0fcc213d3988f6e7c6ffef63b24afe00e5fbd9
2e7a8b5697a98d1d314d6fc3ef0589f81f09d7fe
# upgrade code style to 3.6 using pyupgrade
6ec972f4dbb880bf0c7a11809e6c1ba194c9784c
# upgrade code style to use f-strings using flynt
313b80f27f525441c449593a3aeaf38389f63c13
12 changes: 6 additions & 6 deletions benchmarks/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ class User100(User):
itertools.product(worker_count_cases, user_count_cases, number_of_user_classes_cases, spawn_rate_cases)
):
if user_count / spawn_rate > 1000:
print("Skipping user_count = {:,} - spawn_rate = {:,}".format(user_count, spawn_rate))
print(f"Skipping user_count = {user_count:,} - spawn_rate = {spawn_rate:,}")
continue

workers = [WorkerNode(str(i + 1)) for i in range(worker_count)]
Expand Down Expand Up @@ -614,10 +614,10 @@ class User100(User):
table.add_rows(
[
[
"{:,}".format(worker_count),
"{:,}".format(user_count),
f"{worker_count:,}",
f"{user_count:,}",
number_of_user_classes,
"{:,}".format(spawn_rate),
f"{spawn_rate:,}",
cpu_ramp_up,
cpu_ramp_down,
]
Expand All @@ -632,8 +632,8 @@ class User100(User):
print()
print(table)

with open("results-dispatch-benchmarks-{}.txt".format(int(now)), "wt") as file:
with open(f"results-dispatch-benchmarks-{int(now)}.txt", "wt") as file:
file.write(table.get_string())

with open("results-dispatch-benchmarks-{}.json".format(int(now)), "wt") as file:
with open(f"results-dispatch-benchmarks-{int(now)}.json", "wt") as file:
file.write(table.get_json_string())
13 changes: 5 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# This file is execfile()d with the current directory set to its containing dir.
#
Expand All @@ -17,7 +16,7 @@
# Run command `locust --help` and store output in cli-help-output.txt which is included in the docs
def save_locust_help_output():
cli_help_output_file = os.path.join(os.path.abspath(os.path.dirname(__file__)), "cli-help-output.txt")
print("Running `locust --help` command and storing output in %s" % cli_help_output_file)
print(f"Running `locust --help` command and storing output in {cli_help_output_file}")
help_output = subprocess.check_output(["locust", "--help"]).decode("utf-8")
with open(cli_help_output_file, "w") as f:
f.write(help_output)
Expand All @@ -30,19 +29,17 @@ def save_locust_help_output():

def save_locust_env_variables():
env_options_output_file = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config-options.rst")
print("Generating RST table for Locust environment variables and storing in %s" % env_options_output_file)
print(f"Generating RST table for Locust environment variables and storing in {env_options_output_file}")
parser = get_empty_argument_parser()
setup_parser_arguments(parser)
table_data = []
for action in parser._actions:
if action.env_var:
table_data.append(
(
", ".join(["``%s``" % c for c in action.option_strings]),
"``%s``" % action.env_var,
", ".join(
["``%s``" % c for c in parser.get_possible_config_keys(action) if not c.startswith("--")]
),
", ".join([f"``{c}``" for c in action.option_strings]),
f"``{action.env_var}``",
", ".join([f"``{c}``" for c in parser.get_possible_config_keys(action) if not c.startswith("--")]),
action.help,
)
)
Expand Down
2 changes: 1 addition & 1 deletion examples/add_command_line_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def _(parser):

@events.test_start.add_listener
def _(environment, **kw):
print("Custom argument supplied: %s" % environment.parsed_options.my_argument)
print(f"Custom argument supplied: {environment.parsed_options.my_argument}")


class WebsiteUser(HttpUser):
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class WebsiteUser(HttpUser):

def __init__(self, parent):
self.username = usernames.pop()
super(WebsiteUser, self).__init__(parent)
super().__init__(parent)

@task
def task(self):
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_shape/wait_user_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, *args, **kwargs):
# the User has a slow initialization for gathering data to randomly
# select.
time.sleep(random.randint(0, 5))
super(WebsiteUser, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)


Step = namedtuple("Step", ["users", "dwell"])
Expand All @@ -50,7 +50,7 @@ class StepLoadShape(LoadTestShape):
def __init__(self, *args, **kwargs):
self.step = 0
self.time_active = False
super(StepLoadShape, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

def tick(self):
if self.step >= len(self.targets_with_times):
Expand Down
2 changes: 0 additions & 2 deletions examples/events.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

"""
This is an example of a locustfile that uses Locust's built in event hooks to
track the sum of the content-length header in all successful HTTP responses
Expand Down
14 changes: 3 additions & 11 deletions examples/extend_web_ui/extend.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

"""
This is an example of a locustfile that uses Locust's built in event and web
UI extension hooks to track the sum of the content-length header in all
Expand Down Expand Up @@ -91,8 +89,8 @@ def request_content_length_csv():
Add route to enable downloading of content-length stats as CSV
"""
response = make_response(content_length_csv())
file_name = "content_length{0}.csv".format(time())
disposition = "attachment;filename={0}".format(file_name)
file_name = f"content_length{time()}.csv"
disposition = f"attachment;filename={file_name}"
response.headers["Content-type"] = "text/csv"
response.headers["Content-disposition"] = disposition
return response
Expand All @@ -110,13 +108,7 @@ def content_length_csv():

if stats:
for url, inner_stats in stats.items():
rows.append(
'"%s",%.2f'
% (
url,
inner_stats["content-length"],
)
)
rows.append(f"\"{url}\",{inner_stats['content-length']:.2f}")
return "\n".join(rows)

# register our new routes and extended UI with the Locust web UI
Expand Down
1 change: 0 additions & 1 deletion examples/grpc/hello_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions examples/grpc/hello_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import hello_pb2 as hello__pb2


class HelloServiceStub(object):
class HelloServiceStub:
"""Missing associated documentation comment in .proto file."""

def __init__(self, channel):
Expand All @@ -21,7 +21,7 @@ def __init__(self, channel):
)


class HelloServiceServicer(object):
class HelloServiceServicer:
"""Missing associated documentation comment in .proto file."""

def SayHello(self, request, context):
Expand All @@ -44,7 +44,7 @@ def add_HelloServiceServicer_to_server(servicer, server):


# This class is part of an EXPERIMENTAL API.
class HelloService(object):
class HelloService:
"""Missing associated documentation comment in .proto file."""

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion examples/sdk_session_patching/session_patch_locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ArchivistUser(locust.HttpUser):
def on_start(self):
AUTH_TOKEN = None

with open("auth.text", "r") as f:
with open("auth.text") as f:
AUTH_TOKEN = f.read()

# Start an instance of of the SDK
Expand Down
2 changes: 1 addition & 1 deletion generate_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
version,
]

print("Running command: %s\n" % " ".join(cmd))
print(f"Running command: {' '.join(cmd)}\n")
subprocess.run(cmd)
2 changes: 1 addition & 1 deletion locust/argument_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def setup_parser_arguments(parser):
"-V",
action="version",
help="Show program's version number and exit",
version="%(prog)s {}".format(version),
version=f"%(prog)s {version}",
)
other_group.add_argument(
"--exit-code-on-error",
Expand Down
2 changes: 1 addition & 1 deletion locust/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _build_url(self, path):
if absolute_http_url_regexp.match(path):
return path
else:
return "%s%s" % (self.base_url, path)
return f"{self.base_url}{path}"

@contextmanager
def rename_request(self, name: str):
Expand Down
4 changes: 2 additions & 2 deletions locust/contrib/fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _build_url(self, path):
if absolute_http_url_regexp.match(path):
return path
else:
return "%s%s" % (self.base_url, path)
return f"{self.base_url}{path}"

def _send_request_safe_mode(self, method, url, **kwargs):
"""
Expand Down Expand Up @@ -303,7 +303,7 @@ def __init__(self, environment):
"You must specify the base host. Either in the host attribute in the User class, or on the command line using the --host option."
)
if not re.match(r"^https?://[^/]+", self.host, re.I):
raise LocustError("Invalid host (`%s`), must be a valid base URL. E.g. http://example.com" % self.host)
raise LocustError(f"Invalid host (`{self.host}`), must be a valid base URL. E.g. http://example.com")

self.client = FastHttpSession(
self.environment,
Expand Down
2 changes: 1 addition & 1 deletion locust/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def infinite_cycle_gen(users: List[Tuple[User, int]]) -> Generator[Optional[str]
normalized_values = [
(
user.__name__,
round(target_min_weight * value / min([u[1] for u in users])),
round(target_min_weight * value / min(u[1] for u in users)),
)
for user, value in users
]
Expand Down
4 changes: 2 additions & 2 deletions locust/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def __init__(
self._remove_user_classes_with_weight_zero()

# Validate there's no class with the same name but in different modules
if len(set(user_class.__name__ for user_class in self.user_classes)) != len(self.user_classes):
if len({user_class.__name__ for user_class in self.user_classes}) != len(self.user_classes):
raise ValueError(
"The following user classes have the same class name: {}".format(
", ".join(map(methodcaller("fullname"), self.user_classes))
Expand All @@ -111,7 +111,7 @@ def _create_runner(
**kwargs,
) -> RunnerType:
if self.runner is not None:
raise RunnerAlreadyExistsError("Environment.runner already exists (%s)" % self.runner)
raise RunnerAlreadyExistsError(f"Environment.runner already exists ({self.runner})")
self.runner: RunnerType = runner_class(self, *args, **kwargs)

# Attach the runner to the shape class so that the shape class can access user count state
Expand Down
2 changes: 1 addition & 1 deletion locust/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_html_report(environment, show_download_link=True):
if environment.host:
host = environment.host
elif environment.runner.user_classes:
all_hosts = set([l.host for l in environment.runner.user_classes])
all_hosts = {l.host for l in environment.runner.user_classes}
if len(all_hosts) == 1:
host = list(all_hosts)[0]

Expand Down
2 changes: 1 addition & 1 deletion locust/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def setup_logging(loglevel, logfile=None):
"disable_existing_loggers": False,
"formatters": {
"default": {
"format": "[%(asctime)s] {0}/%(levelname)s/%(name)s: %(message)s".format(HOSTNAME),
"format": f"[%(asctime)s] {HOSTNAME}/%(levelname)s/%(name)s: %(message)s",
},
"plain": {
"format": "%(message)s",
Expand Down
22 changes: 10 additions & 12 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def main():
if options.user_classes:
missing = set(options.user_classes) - set(user_classes.keys())
if missing:
logger.error("Unknown User(s): %s\n" % (", ".join(missing)))
logger.error(f"Unknown User(s): {', '.join(missing)}\n")
sys.exit(1)
else:
names = set(options.user_classes) & set(user_classes.keys())
Expand All @@ -196,11 +196,9 @@ def main():
resource.setrlimit(resource.RLIMIT_NOFILE, [minimum_open_file_limit, resource.RLIM_INFINITY])
except BaseException:
logger.warning(
(
f"System open file limit '{current_open_file_limit}' is below minimum setting '{minimum_open_file_limit}'. "
"It's not high enough for load testing, and the OS didn't allow locust to increase it by itself. "
"See https://github.com/locustio/locust/wiki/Installation#increasing-maximum-number-of-open-files-limit for more info."
)
f"""System open file limit '{current_open_file_limit}' is below minimum setting '{minimum_open_file_limit}'.
It's not high enough for load testing, and the OS didn't allow locust to increase it by itself.
See https://github.com/locustio/locust/wiki/Installation#increasing-maximum-number-of-open-files-limit for more info."""
)

# create locust Environment
Expand Down Expand Up @@ -237,7 +235,7 @@ def main():
try:
runner = environment.create_worker_runner(options.master_host, options.master_port)
logger.debug("Connected to locust master: %s:%s", options.master_host, options.master_port)
except socket.error as e:
except OSError as e:
logger.error("Failed to connect to the Locust master: %s", e)
sys.exit(-1)
else:
Expand Down Expand Up @@ -274,7 +272,7 @@ def main():
else:
web_host = options.web_host
if web_host:
logger.info("Starting web interface at %s://%s:%s" % (protocol, web_host, options.web_port))
logger.info(f"Starting web interface at {protocol}://{web_host}:{options.web_port}")
else:
logger.info(
"Starting web interface at %s://0.0.0.0:%s (accepting connections from all network interfaces)"
Expand Down Expand Up @@ -316,7 +314,7 @@ def stop_and_optionally_quit():
logger.info("--run-time limit reached, stopping test")
runner.stop()
if options.autoquit != -1:
logger.debug("Autoquit time limit set to %s seconds" % options.autoquit)
logger.debug(f"Autoquit time limit set to {options.autoquit} seconds")
time.sleep(options.autoquit)
logger.info("--autoquit time reached, shutting down")
runner.quit()
Expand Down Expand Up @@ -378,7 +376,7 @@ def start_automatic_run():
headless_master_greenlet.link_exception(greenlet_exception_handler)

if options.run_time:
logger.info("Run time limit set to %s seconds" % options.run_time)
logger.info(f"Run time limit set to {options.run_time} seconds")
spawn_run_time_quit_greenlet()
elif not options.worker and not environment.shape_class:
logger.info("No run time limit set, use CTRL+C to interrupt")
Expand Down Expand Up @@ -436,7 +434,7 @@ def shutdown():
else:
code = 0

logger.info("Shutting down (exit code %s)" % code)
logger.info(f"Shutting down (exit code {code})")
if stats_printer_greenlet is not None:
stats_printer_greenlet.kill(block=False)
if headless_master_greenlet is not None:
Expand Down Expand Up @@ -466,7 +464,7 @@ def save_html_report():
gevent.signal_handler(signal.SIGTERM, sig_term_handler)

try:
logger.info("Starting Locust %s" % version)
logger.info(f"Starting Locust {version}")
if options.autostart:
start_automatic_run()

Expand Down
2 changes: 1 addition & 1 deletion locust/rpc/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def __init__(self, message_type, data, node_id):
self.node_id = node_id

def __repr__(self):
return "<Message %s:%s>" % (self.type, self.node_id)
return f"<Message {self.type}:{self.node_id}>"

def serialize(self):
return msgpack.dumps((self.type, self.data, self.node_id))
Expand Down
4 changes: 2 additions & 2 deletions locust/rpc/zmqrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ class Server(BaseSocket):
def __init__(self, host, port):
BaseSocket.__init__(self, zmq.ROUTER)
if port == 0:
self.port = self.socket.bind_to_random_port("tcp://%s" % host)
self.port = self.socket.bind_to_random_port(f"tcp://{host}")
else:
try:
self.socket.bind("tcp://%s:%i" % (host, port))
self.port = port
except zmqerr.ZMQError as e:
raise RPCError("Socket bind failure: %s" % (e))
raise RPCError(f"Socket bind failure: {e}")


class Client(BaseSocket):
Expand Down
Loading

0 comments on commit 04264f2

Please sign in to comment.