mbed sdk python coding styles
Mbed OS 2 and Mbed OS 5
This is a guide for Mbed OS 2. If you’re working with Mbed OS 5, please see the Coding Style guide in the new handbook.
Rationale¶
Some of our tools in workspace_tools are written in Python 2.7. In case of developing tools for python we prefer to keep similar code styles across all Python source code. Please note that not all rules must be enforced. For example we do not limit you to 80 characters per line, just be sure your code can fit to widescreen display.
Coding guidelines¶
Please stay compatible with Python 2.7 but nothing stops you to write your code so in the future it will by Python 3 friendly.
Please check our Python source code (especially test_api.py and singletest.py) to get notion of how your new code should look like). We know our code is not perfect but please try to fit the same coding style to existing code so source looks consistent and is not series of different flavors.
Some general guidelines:
- Please do not use TABs. Please use 4 spaces instead for indentations.
- Please put space character between operators, after comma etc.
Code sample:
udpecho_server_auto.py snippet
import re import sys import uuid from sys import stdout from host_test import DefaultTest from socket import socket, AF_INET, SOCK_DGRAM class UDPEchoServerTest(DefaultTest): ECHO_SERVER_ADDRESS = "" ECHO_PORT = 0 s = None # Socket PATTERN_SERVER_IP = "Server IP Address is (\d+).(\d+).(\d+).(\d+):(\d+)" re_detect_server_ip = re.compile(PATTERN_SERVER_IP) def test(self): result = True serial_ip_msg = self.mbed.serial_readline() if serial_ip_msg is None: return self.RESULT_IO_SERIAL self.notify(serial_ip_msg) # Searching for IP address and port prompted by server m = self.re_detect_server_ip.search(serial_ip_msg) if m and len(m.groups()): self.ECHO_SERVER_ADDRESS = ".".join(m.groups()[:4]) self.ECHO_PORT = int(m.groups()[4]) # must be integer for socket.connect method self.notify("HOST: UDP Server found at: " + self.ECHO_SERVER_ADDRESS + ":" + str(self.ECHO_PORT)) # We assume this test fails so can't send 'error' message to server try: self.s = socket(AF_INET, SOCK_DGRAM) except Exception, e: self.s = None self.notify("HOST: Socket error: %s"% e) return self.RESULT_ERROR for i in range(0, 100): TEST_STRING = str(uuid.uuid4()) self.s.sendto(TEST_STRING, (self.ECHO_SERVER_ADDRESS, self.ECHO_PORT)) data = self.s.recv(len(TEST_STRING)) received_str = repr(data)[1:-1] if TEST_STRING != received_str: result = False break sys.stdout.write('.') stdout.flush() else: result = False if self.s is not None: self.s.close() return self.RESULT_SUCCESS if result else self.RESULT_FAILURE if __name__ == '__main__': UDPEchoServerTest().run()
Static Code Analizers for Python¶
If you are old-school developer for sure you remember tools like lint. "lint was the name originally given to a particular program that flagged some suspicious and non-portable constructs (likely to be bugs) in C language source code." Now lint-like programs are used to check similar code issues for multiple languages, also for Python. Please do use them if you want to commit new code to workspace_tools and other mbed SDK Python tooling.
Below is the list Python lint tools you may want to use:
- pyflakes - Please scan your code with pyflakes and remove all issues reported by it. If you are unsure if something should be modified or not you can skip lint report related fix and report this issue as possible additional commit in your pull request description.
- pylint - Please scan your code with pylint and check if there are any issues which can be resolved and are obvious "to fix" bugs. For example you may forgot to add 'self' as first parameter in class method parameter list or you are calling unknown functions / functions from not imported modules.
- pychecker - optional, but more the merrier ;)