refactor: resolve duplicate string literals, mutable default arguments, and type mismatches in Python scripts - #174
Conversation
gab-arrobo
left a comment
There was a problem hiding this comment.
Some minor changes due to warnings from git when applying patch. Also, some other minor format changes
$ git apply pr.patch
pr.patch:187: trailing whitespace.
if rtt_percentiles is None:
pr.patch:234: trailing whitespace.
BLACKLISTED_HOST = 'www.blacklisted.com'
pr.patch:349: trailing whitespace.
UNBIND_OPEN_ERROR = "Error: unbind failed for %s - Cannot open %s"
pr.patch:413: trailing whitespace.
# constants for duplicate literals
warning: 4 lines add whitespace errors.
| from pybess import protobuf_to_dict as pb_conv | ||
|
|
||
| # constants for duplicate literals | ||
| BLACKLISTED_HOST = 'www.blacklisted.com' |
There was a problem hiding this comment.
| BLACKLISTED_HOST = 'www.blacklisted.com' | |
| BLACKLISTED_HOST = 'www.blacklisted.com' |
| from os.path import exists, abspath, dirname, basename | ||
|
|
||
| # constants for duplicate literals | ||
| UNBIND_OPEN_ERROR = "Error: unbind failed for %s - Cannot open %s" |
There was a problem hiding this comment.
| UNBIND_OPEN_ERROR = "Error: unbind failed for %s - Cannot open %s" | |
| UNBIND_OPEN_ERROR = "Error: unbind failed for %s - Cannot open %s" |
| import textwrap | ||
| import argparse | ||
|
|
||
| # constants for duplicate literals |
There was a problem hiding this comment.
| # constants for duplicate literals | |
| # constants for duplicate literals |
| will have RTT stats reported as the average of those seen between | ||
| subsequent calls to `next()`. | ||
| """ | ||
| if rtt_percentiles is None: |
There was a problem hiding this comment.
| if rtt_percentiles is None: | |
| if rtt_percentiles is None: |
| print(UNBIND_OPEN_ERROR | ||
| % (dev_id, filename)) |
There was a problem hiding this comment.
Should this become a single line?
| print(UNBIND_OPEN_ERROR | |
| % (dev_id, filename)) | |
| print(UNBIND_OPEN_ERROR % (dev_id, filename)) |
| print(UNBIND_OPEN_ERROR | ||
| % (dev_id, filename)) |
There was a problem hiding this comment.
| print(UNBIND_OPEN_ERROR | |
| % (dev_id, filename)) | |
| print(UNBIND_OPEN_ERROR % (dev_id, filename)) |
| print(UNBIND_OPEN_ERROR | ||
| % (dev_id, filename)) |
There was a problem hiding this comment.
| print(UNBIND_OPEN_ERROR | |
| % (dev_id, filename)) | |
| print(UNBIND_OPEN_ERROR % (dev_id, filename)) |
There was a problem hiding this comment.
Pull request overview
This PR refactors several Python scripts/tests to reduce repeated string literals, fix a mutable default argument, and tighten up a subprocess invocation parameter type, aiming to improve code quality without changing intended behavior.
Changes:
- Introduces constants to replace duplicated string literals across build tooling, CLI output, and module tests.
- Fixes a mutable default argument in
PortStatsGeneratorby switching fromlist()toNone+ in-body initialization. - Adjusts
subprocess.check_call(..., shell=...)to use a booleanTrueinstead of the string'True'.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| build.py | Adds a constant for the builtin protobuf output directory and reuses it in generation/cleanup paths. |
| bin/dpdk-devbind.py | Factors repeated error/help strings into constants and reuses them across error paths. |
| bessctl/test_samples.py | Introduces a constant for the daemon start command string used across test setup/recovery. |
| bessctl/sugar.py | Introduces a constant for the __bess_env__ prefix used in environment-variable substitution strings. |
| bessctl/module_tests/url_filter.py | Replaces repeated host literals with constants in URL filter tests. |
| bessctl/module_tests/arp.py | Replaces repeated MAC address literals with a constant in ARP responder tests. |
| bessctl/measurement_utils.py | Fixes a mutable default argument by initializing rtt_percentiles safely per instance. |
| bessctl/commands.py | Consolidates repeated CLI output strings into constants and fixes a shell parameter type. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| cli.fout.write(' Available plugins: ') | ||
| if drivers: | ||
| cli.fout.write('%s\n' % ', '.join(plugins)) | ||
| else: | ||
| cli.fout.write('(none)\n') | ||
| cli.fout.write(NONE_MESSAGE) |
| bessctl = os.path.join(this_dir, 'bessctl') | ||
| sample_dir = os.path.join(this_dir, 'conf/samples') | ||
|
|
||
| # constants for duplicate lliterals |
| from pybess import protobuf_to_dict as pb_conv | ||
|
|
||
| # constants for duplicate literals | ||
| BLACKLISTED_HOST = 'www.blacklisted.com' |
| will have RTT stats reported as the average of those seen between | ||
| subsequent calls to `next()`. | ||
| """ | ||
| if rtt_percentiles is None: |
| import textwrap | ||
| import argparse | ||
|
|
||
| # constants for duplicate literals |
…s, and type mismatches in Python scripts Signed-off-by: bhagathkrishnacdac <bhagath.krishna@cdac.in>
e89998b to
86efcdd
Compare
Overview
This PR addresses several critical code quality issues across the project's Python files (under bessctl/, bin/, and build.py), focusing on string deduplication, correct parameter initialization, and type safety.
Key Changes
Verification
These changes do not alter any external behaviors, console outputs, or build pipeline results. All changes represent standard, non-functional code quality improvements.