Skip to content

Sourcery refactored master branch#1

Open
sourcery-ai[bot] wants to merge 1 commit intomasterfrom
sourcery/master
Open

Sourcery refactored master branch#1
sourcery-ai[bot] wants to merge 1 commit intomasterfrom
sourcery/master

Conversation

@sourcery-ai
Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot commented Dec 4, 2023

Branch master refactored 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 master branch, then run:

git fetch origin sourcery/master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from tatstratasys December 4, 2023 15:24
# objects
def isr_routine(gpio):
print("pin " + repr(gpio.getPin(True)) + " = " + repr(gpio.read()))
print(f"pin {repr(gpio.getPin(True))} = {repr(gpio.read())}")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function isr_routine refactored with the following changes:

x = mraa.Gpio(pin)

print("Starting ISR for pin " + repr(pin))
print(f"Starting ISR for pin {repr(pin)}")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 49-49 refactored with the following changes:


# read a 16bit reg, obviously it's uncalibrated so mostly a useless value :)
print(str(x.readWordReg(0xf6)))
print(x.readWordReg(0xf6))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 42-53 refactored with the following changes:

else:
val = 1

val = 0 if (val >= 1) else 1
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 41-46 refactored with the following changes:

This removes the following comments ( why? ):

# never reached mostly

dev = m.Spi(0)

for x in range(0,100):
for _ in range(0,100):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 33-33 refactored with the following changes:

Comment on lines -417 to +401
if i == 'Parameters:' or i == 'Exceptions:' or i == 'Returns:':
if i in ['Parameters:', 'Exceptions:', 'Returns:']:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Doxy2SWIG.clean_pieces refactored with the following changes:

Comment on lines -12 to +17
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")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Clean.test_existing_swigtype refactored with the following changes:

Comment on lines -27 to +48
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))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GeneralChecks.test_mraa_version refactored with the following changes:

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))}"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GpioChecks.setUp refactored with the following changes:

Comment on lines -54 to +55
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]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GpioChecks.test_set_GPIO_as_output refactored with the following changes:

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]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GpioChecks.test_set_GPIO_as_input refactored with the following changes:

Comment on lines -69 to +68
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]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GpioChecks.test_GPIO_as_output_write_HIGH_level refactored with the following changes:

Comment on lines -77 to +75
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]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GpioChecks.test_GPIO_as_output_write_LOW_level refactored with the following changes:

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")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PlatformChecksEdison.test_mraa_check_platform_ADC_max_resolution refactored with the following changes:

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")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PlatformChecks.test_adc_std_res refactored with the following changes:

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")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PlatformChecks.test_adc_max_res refactored with the following changes:

Comment on lines -39 to +43
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",
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SpiChecksLsbmode.test_spi_set_lsbmode_false refactored with the following changes:

Comment on lines -45 to +51
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",
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SpiChecksLsbmode.test_spi_set_lsbmode_true refactored with the following changes:

Comment on lines -38 to +42
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)
])
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SpiChecksWrite.test_spi_write refactored with the following changes:

Comment on lines -39 to +40
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)])
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function UartChecksRead.test_uart_read refactored with the following changes:

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)))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function UartChecksWrite.test_uart_write refactored with the following changes:

Copy link
Copy Markdown
Author

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants