Conversation
| # objects | ||
| def isr_routine(gpio): | ||
| print("pin " + repr(gpio.getPin(True)) + " = " + repr(gpio.read())) | ||
| print(f"pin {repr(gpio.getPin(True))} = {repr(gpio.read())}") |
There was a problem hiding this comment.
Function isr_routine refactored with the following changes:
- Use f-string instead of string concatenation [×3] (
use-fstring-for-concatenation)
| x = mraa.Gpio(pin) | ||
|
|
||
| print("Starting ISR for pin " + repr(pin)) | ||
| print(f"Starting ISR for pin {repr(pin)}") |
There was a problem hiding this comment.
Lines 49-49 refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
|
|
||
| # read a 16bit reg, obviously it's uncalibrated so mostly a useless value :) | ||
| print(str(x.readWordReg(0xf6))) | ||
| print(x.readWordReg(0xf6)) |
There was a problem hiding this comment.
Lines 42-53 refactored with the following changes:
- Remove unnecessary call to
str()withinprint()[×2] (remove-str-from-print)
| else: | ||
| val = 1 | ||
|
|
||
| val = 0 if (val >= 1) else 1 |
There was a problem hiding this comment.
Lines 41-46 refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
This removes the following comments ( why? ):
# never reached mostly
| dev = m.Spi(0) | ||
|
|
||
| for x in range(0,100): | ||
| for _ in range(0,100): |
There was a problem hiding this comment.
Lines 33-33 refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| if i == 'Parameters:' or i == 'Exceptions:' or i == 'Returns:': | ||
| if i in ['Parameters:', 'Exceptions:', 'Returns:']: |
There was a problem hiding this comment.
Function Doxy2SWIG.clean_pieces refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| unclean = [] | ||
|
|
||
| for fileName in os.listdir(rootDir): | ||
| if swigtypeStr in fileName: | ||
| unclean.append(fileName) | ||
|
|
||
| unclean = [ | ||
| fileName for fileName in os.listdir(rootDir) if swigtypeStr in fileName | ||
| ] | ||
| self.assertEqual( len(unclean), 0, | ||
| "\nmraa contains unclean Java bindings:\n" + \ | ||
| "\n".join(unclean) + "\n\n") | ||
| "\n".join(unclean) + "\n\n") |
There was a problem hiding this comment.
Function Clean.test_existing_swigtype refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension)
| ok = False | ||
| for file in os.listdir(cSamplesDir): | ||
| if file == cSampleName: | ||
| ok = True | ||
| break | ||
| ok = any(file == cSampleName for file in os.listdir(cSamplesDir)) | ||
| if not ok: | ||
| missing_c_files.append(cSampleName) | ||
|
|
||
| #check for Cpp files | ||
| if cSampleName.endswith('.cpp'): | ||
| ok = False | ||
| for file in os.listdir(cppSamplesDir): | ||
| if file == cSampleName: | ||
| ok = True | ||
| break | ||
| ok = any(file == cSampleName for file in os.listdir(cppSamplesDir)) | ||
| if not ok: | ||
| missing_cpp_files.append(cSampleName) | ||
|
|
||
| #check for java files | ||
| javaSampleName = javaSampleName.lstrip("java/") | ||
| if javaSampleName.endswith('.java'): | ||
| ok = False | ||
| for file in os.listdir(javaSamplesDir): | ||
| if file == javaSampleName: | ||
| ok = True | ||
| break | ||
| ok = any(file == javaSampleName for file in os.listdir(javaSamplesDir)) | ||
| if not ok: | ||
| missing_java_files.append(javaSampleName) | ||
|
|
||
| self.assertEqual( len(missing_java_files) + len(missing_c_files) + len(missing_cpp_files), 0, | ||
| "\nThe following files are missing from samples:\n" + \ | ||
| "\n".join(missing_c_files) + "\n" + \ | ||
| "\n".join(missing_cpp_files) + "\n" + \ | ||
| "\n".join(missing_java_files)) | ||
| "\n".join(missing_c_files) + "\n" + \ | ||
| "\n".join(missing_cpp_files) + "\n" + \ | ||
| "\n".join(missing_java_files)) |
There was a problem hiding this comment.
Function SampleNames.test_existing_samples refactored with the following changes:
- Use any() instead of for loop [×3] (
use-any)
| def test_mraa_version(self): | ||
| self.version = m.getVersion() | ||
| print("Version is: " + self.version) | ||
| print(f"Version is: {self.version}") |
There was a problem hiding this comment.
Function GeneralChecks.test_mraa_version refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| def setUp(self): | ||
| self.pin = m.Gpio(MRAA_GPIO) | ||
| self.gpio_path = "/sys/class/gpio/gpio" + str(self.pin.getPin(True)) | ||
| self.gpio_path = f"/sys/class/gpio/gpio{str(self.pin.getPin(True))}" |
There was a problem hiding this comment.
Function GpioChecks.setUp refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| dir_file = open(self.gpio_path + "/direction") | ||
| dir_file_content = dir_file.readline()[:-1] | ||
| dir_file.close() | ||
| with open(f"{self.gpio_path}/direction") as dir_file: | ||
| dir_file_content = dir_file.readline()[:-1] |
There was a problem hiding this comment.
Function GpioChecks.test_set_GPIO_as_output refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed) - Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| dir_file_content = dir_file.readline()[:-1] | ||
| dir_file.close() | ||
| with open(f"{self.gpio_path}/direction") as dir_file: | ||
| dir_file_content = dir_file.readline()[:-1] |
There was a problem hiding this comment.
Function GpioChecks.test_set_GPIO_as_input refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed) - Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| val_file = open(self.gpio_path + "/value") | ||
| sysfs_pin_value = val_file.readline()[:-1] | ||
| val_file.close() | ||
| with open(f"{self.gpio_path}/value") as val_file: | ||
| sysfs_pin_value = val_file.readline()[:-1] |
There was a problem hiding this comment.
Function GpioChecks.test_GPIO_as_output_write_HIGH_level refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed) - Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| val_file = open(self.gpio_path + "/value") | ||
| sysfs_pin_value = val_file.readline()[:-1] | ||
| val_file.close() | ||
| with open(f"{self.gpio_path}/value") as val_file: | ||
| sysfs_pin_value = val_file.readline()[:-1] |
There was a problem hiding this comment.
Function GpioChecks.test_GPIO_as_output_write_LOW_level refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed) - Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| def test_mraa_check_platform_ADC_max_resolution(self): | ||
| self.p_ADC_mres = m.adcRawBits() | ||
| print("Platform ADC max. resolution is: " + str(self.p_ADC_mres) + " bits") | ||
| print(f"Platform ADC max. resolution is: {str(self.p_ADC_mres)} bits") |
There was a problem hiding this comment.
Function PlatformChecksEdison.test_mraa_check_platform_ADC_max_resolution refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation)
| def test_adc_std_res(self): | ||
| adc_std_res = m.adcSupportedBits() | ||
| print("Platform ADC standard resolution is: " + str(adc_std_res) + " bits") | ||
| print(f"Platform ADC standard resolution is: {str(adc_std_res)} bits") |
There was a problem hiding this comment.
Function PlatformChecks.test_adc_std_res refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation)
| def test_adc_max_res(self): | ||
| adc_max_res = m.adcRawBits() | ||
| print("Platform ADC max. resolution is: " + str(adc_max_res) + " bits") | ||
| print(f"Platform ADC max. resolution is: {str(adc_max_res)} bits") |
There was a problem hiding this comment.
Function PlatformChecks.test_adc_max_res refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation)
| self.assertEqual(self.spi.lsbmode(TEST_LSBMODE), | ||
| m.SUCCESS, | ||
| "Setting LSB mode to %s did not return success" %TEST_LSBMODE) | ||
| self.assertEqual( | ||
| self.spi.lsbmode(TEST_LSBMODE), | ||
| m.SUCCESS, | ||
| f"Setting LSB mode to {TEST_LSBMODE} did not return success", | ||
| ) |
There was a problem hiding this comment.
Function SpiChecksLsbmode.test_spi_set_lsbmode_false refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| self.assertEqual(self.spi.lsbmode(TEST_LSBMODE), | ||
| m.SUCCESS, | ||
| "Setting LSB mode to %s did not return success" %TEST_LSBMODE) | ||
| self.assertEqual( | ||
| self.spi.lsbmode(TEST_LSBMODE), | ||
| m.SUCCESS, | ||
| f"Setting LSB mode to {TEST_LSBMODE} did not return success", | ||
| ) |
There was a problem hiding this comment.
Function SpiChecksLsbmode.test_spi_set_lsbmode_true refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring)
| DATA_TO_WRITE = bytearray([0xEE for i in range(MOCK_SPI_TEST_DATA_LEN)]) | ||
| DATA_TO_EXPECT = bytearray([0xEE ^ MOCK_SPI_REPLY_DATA_MODIFIER_BYTE for i in range(MOCK_SPI_TEST_DATA_LEN)]) | ||
| DATA_TO_WRITE = bytearray([0xEE for _ in range(MOCK_SPI_TEST_DATA_LEN)]) | ||
| DATA_TO_EXPECT = bytearray([ | ||
| 0xEE ^ MOCK_SPI_REPLY_DATA_MODIFIER_BYTE | ||
| for _ in range(MOCK_SPI_TEST_DATA_LEN) | ||
| ]) |
There was a problem hiding this comment.
Function SpiChecksWrite.test_spi_write refactored with the following changes:
- Replace unused for index with underscore [×2] (
for-index-underscore)
| EXPECTED_RESULT = bytearray([MOCK_UART_DATA_BYTE for x in range(TEST_DATA_LEN)]) | ||
| EXPECTED_RESULT = bytearray( | ||
| [MOCK_UART_DATA_BYTE for _ in range(TEST_DATA_LEN)]) |
There was a problem hiding this comment.
Function UartChecksRead.test_uart_read refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| def test_uart_write(self): | ||
| TEST_DATA_LEN = 10 | ||
| TEST_DATA = bytearray([x for x in range(TEST_DATA_LEN)]) | ||
| TEST_DATA = bytearray(list(range(TEST_DATA_LEN))) |
There was a problem hiding this comment.
Function UartChecksWrite.test_uart_write refactored with the following changes:
- Replace identity comprehension with call to collection constructor (
identity-comprehension)
There was a problem hiding this comment.
PR Type: Refactoring
Summary of PR: This PR includes a series of refactoring changes aimed at improving code readability and maintainability. It applies modern Python syntax such as f-strings for string formatting, simplifies conditional statements, and uses list comprehensions and generator expressions for more concise and readable loops. The changes are spread across various Python scripts, including example scripts, test cases, and source files.
General PR suggestions:
- Ensure that the refactoring maintains the original logic and functionality of the code. While the changes seem to be syntactic improvements, it's crucial to verify that they do not introduce any behavioral changes.
- Review the use of f-strings throughout the changes to ensure consistency. There is at least one instance where an old-style string formatting is still used, and it would be beneficial to update it to an f-string for uniformity.
- Consider running the full test suite to ensure that the refactored code passes all tests and that no new issues have been introduced by the changes.
- Review the changes for any potential edge cases that might not be covered by the current test cases, especially where conditional logic has been altered.
- Since the PR is generated by an automated tool, it would be wise to manually review the changes to ensure that they align with the project's coding standards and practices.
Your trial expires on December 18, 2023. Please email tim@sourcery.ai to continue using Sourcery ✨
Branch
masterrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run:Help us improve this pull request!