Formatting Pythonista code before posting it to GitHub
I have found the following best practice tips useful for formatting Pythonista code before posting it to GitHub:
One Time Setup:
- When looking at the list of files at the left of the Pythonisa screen, click the gear icon at the bottom left of the screen.
- When the "Settings" panel appears, tap "Indentation".
- Make sure that Tab Width is set to "4 spaces".
- Make sure that both "Soft Tabs (Spaces)" and "Show Mixed Indentation" are turned on.
- Tap "Done".
- Tap the gear icon again.
- When the "Settings" panel appears, scroll down and tap on "Actions Menu".
- Make sure that both "Copy Text" and "Convert Indentation..." are turned on.
Before Posting to GitHub:
- Open your code in the Pythonista Editor.
- From the Actions Menu in the upper right of the screen, tap "Convert Indentation" and then "Copy Text".
- Paste your code into GitHub.
PEP 8 makes it clear that Spaces are the preferred indentation method for Python. I know that personal preferences may vary but on an iOS device it is difficult to edit tabbed code on the GitHub webpages because the standard iOS keyboard (e.g. the keyboard in Safari) does not have a tab key. Entering a tab character on a GitHub web page involves a painfully tricky cut and paste operation on an iOS device while typing four space characters is trivial.
Pythonista's Soft Tab feature means that the tab key in the Pythonista Editor works as expected yet the resulting code has no tab characters. We get the best of both worlds with Soft Tabs: the convenience of a tab key but code that contains no tab characters. The other advantages of the above approach is that trailing whitespaces are eliminated, IndentationErrors are easier to fix, and tabs in comments are similarly aligned in both the Pythonista Editor and on GitHub.
@ccc You could also run your project through pylint to check proper variable names, indention etc.
I like your idea of adding a pylint step to the process.
Could you please provide a description of how to get pylint working on Pythonista. Or an installation script would be even cooler.
I always wondered why many coders here do not follow PEP-8. Apart from the tab/spaces thing, variable naming comes to mind. IMHO mixedCase for function, method, and variable names isn't really well-liked in the Python world. Also, I see many whitespace-related style errors in code posted here.
@ccc it's been awhile since I set it up. I recall it being pretty easy with shellista/pipista. I'll remove it and do it again to refreash my memory.
I have only two strong opinions on tabs-vs-spaces: 1) I should not have to care or even think about it - it's 2014, for goodness sake, why is this still a thing?? - and 2) mixing indentation styles should be a hard error in Python.
It's great that Pythonista enables me to tap out a quick test on my iPhone with minimal tedium, but I do most of my hacking on my iPad with a Bluetooth keyboard.
Because of that, I've switched to tabs for indentation; with tabs to I can get to the "logical" beginning of a line with cmd-left + right a few times (say, 2-4 on average), but with 4-space soft tabs that was 8-16 - or more - right arrows. Tedium. I started having to care.
(Also, "Convert Indentation" cleans up trailing whitespace? Woo Hoo! I'm gonna run that baby all the time!)
@bjh Paste your tab-ladened code into GitHub and then try to add a few new lines of indented code to it on the GitHub web page using Safari on your iOS device without an external keyboard. Adding new lines of code there is an exercise in frustration that the above steps are trying to avoid.
The Python code editors I use on the desktop insert a set amount of spaces (4) when I press tab or remove them, when I use backspace at the beginning of the line.
My strong opinion on tabs is: they have no place in Python code.