Skip to content

Latest commit

 

History

History
90 lines (58 loc) · 3.96 KB

README.md

File metadata and controls

90 lines (58 loc) · 3.96 KB

Learning Python

Miscellaneous practice code in Python and things I've used.

Just one of the things I'm learning. https://github.com/hchiam/learning

You can create share-able online live demos with trinket.io (similar to CodePen for JS/HTML/CSS).

Notes on Python built-ins worth knowing

https://github.com/hchiam/learning-python/blob/master/python-built-ins-worth-learning.md

Run code linter

To run python linter pylint on a file (for example pylint_example.py), run this in the CLI:

pylint pylint_example.py

Or even better, you can auto-run that command every time you save a file in the folder. To do that, do pip3 install rerun and then run this in the CLI:

rerun "pylint pylint_example.py; python3 pylint_example.py"

This works just like nodemon for JS/Node.js.

Type checking

I use pyright to do static type checking in VSCode.

You can see an example of (gradual) type annotations here.

Maintainability Index (MI score)

The MI combines lines of code, cyclomatic complexity, and the Halstead volume metric (i.e. number of variables, operations, decision paths, and lines of code). After you pip install radon, you can get the MI score of your code:

radon mi your_code.py -s

Similar to how I use plato for JavaScript code.

More tools for things like refactoring code (rope) and tracking project code complexity (wily): https://realpython.com/python-refactoring/

pip uninstall matplotlib
pip install matplotlib

More random notes

pretty print pprint: https://www.geeksforgeeks.org/pprint-data-pretty-printer-python/

all([True, False]): https://www.w3schools.com/python/ref_func_all.asp

any([True, False]): https://www.w3schools.com/python/ref_func_any.asp

from __future__ import newkeywordthing: https://stackoverflow.com/questions/7075082/what-is-future-in-python-used-for-and-how-when-to-use-it-and-how-it-works

import inspect: https://stackoverflow.com/questions/3711184/how-to-use-inspect-to-get-the-callers-info-from-callee-in-python

print(wikipedia.summary('web query')): https://pypi.org/project/wikipedia/ (see example for other params & methods)

brew install python to upgrade python to the latest version https://stackoverflow.com/questions/74214615/how-to-update-python-version-in-terminal

  • brew link --overwrite python@3.12
  • alias py='python3.12' in .bash_profile (alias bas='source ~/.bash_profile') so py --version outputs something like Python 3.12.2

python3 -m http.server 8000 to quickly set up a basic local server that serves index.html at http://localhost:8000/

  • or python -m SimpleHTTPServer 8000 for older python versions
  • consider bash script: alias srv='python3 -m http.server 8000'

Other Repos