Last active
January 27, 2021 15:09
-
-
Save imbilltucker/2fe5f44f918ff70d7f762836ef23c6d3 to your computer and use it in GitHub Desktop.
things I wish I knew starting python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Things I wish I knew when starting python | |
improve the basic interpreter | |
PYTHONSTARTUP is an environment variable you can use to fix the command prompt, i.e. provide autocomplete via tab, and suggestions. | |
set $PYTHONSTARTUP=~/.pythonstartup in .bashrc | |
export PYTHONSTARTUP=~/.pythonstartup | |
turn off creation of .pyc files | |
PYTHONDONTWRITEBYTECODE=1 | |
enable auto-complete in the REPL | |
import rlcompleter | |
import readline | |
# To allow tab completion | |
readline.parse_and_bind("tab: complete") | |
# Include the following lines to enable peristent command history | |
# # (saved to ~/.python_history) | |
import os, atexit | |
readline_history_file = os.path.join(os.path.expanduser('~'), '.python_history') | |
try: | |
readline.read_history_file(readline_history_file) | |
except IOError: | |
pass | |
readline.set_history_length(3000) | |
atexit.register(readline.write_history_file, readline_history_file) | |
look at these resources: | |
https://docs.python.org/2/using/cmdline.html#envvar-PYTHONSTARTUP | |
http://www.cs.berkeley.edu/~russell/classes/cs188/f14/faq.html | |
other interpreters | |
bpython (colors and doc prompts) | |
ipython (never leave ipython; use it like a system prompt) | |
ipython notebook (store mathematica-like notebooks that contain your python) | |
Resources for learning Python | |
exercism http://exercism.io/ | |
Learn Python the Hard Way http://learnpythonthehardway.org/book/ | |
Raymond Hettinger's twitter: https://twitter.com/raymondh?lang=en | |
pyvideo.org | |
Hitchiker's guide to Python: http://docs.python-guide.org/en/latest/ | |
talkpython: https://talkpython.fm/ | |
Real Python: https://realpython.com/ | |
Debugging tools | |
pdb, like gdb but for python (python -m pdb <mypythonscript.py>) | |
pudb, for bringing back 90's pascal, but having watch windows. (pip install-able) | |
linting tools | |
They are pip installable! | |
flake8 https://dev.to/sethmichaellarson/linting-as-lightweight-defect-detection-for-python | |
flake8 <dir> | |
pylint | |
virtual environments | |
I still like just virtualenv, but YMMV | |
use virtualenvwrapper | |
try using the system one before pip install | |
pipenv is the new-hotness: https://docs.pipenv.org/ | |
vex helps manage virtualenvs if not using bash; https://pypi.python.org/pypi/vex | |
pipenv | |
https://pipenv.readthedocs.io/en/latest/ | |
poetry | |
https://poetry.eustace.io/ | |
remove pyc files from your life | |
set this in your profile and .pyc files won't get created export PYTHONDONTWRITEBYTECODE=1 | |
testing framework | |
(I prefer pytest) | |
unittest (built in) | |
pytest (I prefer) | |
nose | |
strings | |
convert from unicode to ascii This ignore error conversions to ascii, but has a best guess for converting letters to an ascii equivalent. | |
import unicodedata | |
normed_unicode = unicodedata.normalize('NFKD', utf_string) | |
ascii_string = normed_unicode.encode('ascii', 'ignore') | |
"mystring: {a}".format(a) | |
don't have to call 'format' immediately | |
provides tools for formatting strings nicely | |
f-strings are the new, faster thing: | |
f"this is my local var name: {x}" | |
gotchas | |
shadow variables: scoping can cause problems. use global | |
x = 5 | |
def foo(y): | |
x = 1 | |
print("X is: {}".format(str(x)) | |
y = x + 1 | |
print("X is: {}".format(str(x)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment