Python
RELATED: SETUP > Dev Environment > Python |
Books
Google Drive > Books > Dev Languages > Python - https://drive.google.com/drive/folders/0B-bIAh9FWNPda0ZfWHpScFR3SWs?resourcekey=0-G5fNrsHYYOAIzXp-ksAIpQ&usp=sharing
Create & Delete virtual environment
Go to: SETUP > Dev Environments > Python - Highlights - virtual environment...
Capture pip packages
$ pip freeze > requirements.txt # export
$ pip install -r requirements.txt # import
Python conventions
Module naming using all lowercase letters, and underscore if needed. Refer to https://www.python.org/dev/peps/pep-0008/#package-and-module-names for more details.
DEVELOP
Create Django project
Source: AWS - Deploying a Django application to Elastic Beanstalk
Create a scaffold Django project:
$ cd ~/projects/eb-webapp
# create the virtual environment in the venv folder
$ virtualenv ./venv
$ source ./venv/bin/activate
(venv) $ pip install django==2.2
# show package has been installed
(venv) $ pip freeze
(venv) $ django-admin startproject webapp
(venv) $ cd webapp
(venv) $ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
[...]
Django version 2.2, using settings 'webapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
API
Go to DEVELOP > API
Useful Notes 🚧
debugger -
$ python -i
import pdb;
pdb.set_trace(); # launch debugger
dunder methods - special methods to perform basic object operations also known as "dunder" methods; double underscore before and after.
my_collection.__getitem__(key)
Cheat sheet (ref-1)
A single statement can span across multiple lines by enclosing part of the statement in a bracketed pair—parentheses
()
, square brackets[]
, curly braces{}
, triple-quoted string literals"""_"""
, or end with a backslash\
(ref-1)Features to remember: List unpacking; (ref-1)
import <module>
vsfrom <module> import *
- former: loads & become a variable to be used (e.g.,load mod1; mod1.func1()
); the latter: copy the function only and use it without going through the module again (e.g.from mod1 import func1; func1()
)Variables prefixed with a single underscore (e.g.,
_X
) are not copied out when a client imports a module’s names with afrom *
statement; but visible via theimport <module-name>
(ref-1)AVOID names that have two leading and trailing underscores (
__X__
) are system-defined names that have special meaning to the interpreter.Documentation strings (docstrings for short) are ignored but are saved and displayed by tools
DEPLOY
Excellent Python tutorial, deployment, etc. https://www.linode.com/docs/guides/development/python/
Packaging
Online IDE
Open source libraries
Game - https://www.pygame.org/news
Science - https://matplotlib.org/
Game programming and multimedia with pygame, cgkit, pyglet, PySoy, Panda3D, and others
Serial port communication on Windows, Linux, and more with the PySerial ex- tension
Image processing with PIL and its newer Pillow fork, PyOpenGL, Blender, Maya, and more
Robot control programming with the PyRo toolkit
Natural language analysis with the NLTK package
Instrumentation on the Raspberry Pi and Arduino boards
Finance: yfinance (
pip install yfinance
)Mobile computing with ports of Python to the Google Android and Apple iOS platforms
Excel spreadsheet function and macro programming with the PyXLL or DataNi- tro add-ins
Excel module: http://pypi.python.org/pypi/xlrd; example: https://www.gurobi.com/documentation/11.0/examples/diet4_py.html
Media file content and metadata tag processing with PyMedia, ID3, PIL/Pillow, and more
Artificial intelligence with the PyBrain neural net library and the Milk machine learning toolkit
Expert system programming with PyCLIPS, Pyke, Pyrolog, and pyDatalog
Network monitoring with zenoss, written in and customized with Python
Python-scripted design and modeling with PythonCAD, PythonOCC, FreeCAD, and others
Document processing and generation with ReportLab, Sphinx, Cheetah, PyPDF, and so on
Data visualization with Mayavi, matplotlib, VTK, VPython, and more
XML parsing with the xml library package, the xmlrpclib module, and third-party extensions
JSON and CSV file processing with the json and csv modules
Data mining with the Orange framework, the Pattern bundle, Scrapy, and custom code
How-to
How-to: install Python Packages from a local folder
Reference: https://www.activestate.com/resources/quick-reads/how-to-manually-install-python-packages/
References
REF-1 - Learning Python, Fifth Edition by Mark Lutz
REF-2 - Fluent Python, 2nd Edition by Luciano Ramalho
[REF-3] - RealPython Tutorial; excellent Python tutorial website; not all contents are free
[REF-4] - Pip User Guide;
Last updated