Python

RELATED: SETUP > Dev Environment > Python |

Awesome References:

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

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> vs from <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 a from * statement; but visible via the import <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

Packaging

Online IDE

Open source libraries

  • 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

  • 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/

Example - the Amazon awsiotsdk
  • Git clone the repo: https://github.com/aws/aws-iot-device-sdk-python-v2

  • The code folder is aws-iot-device-sdk-python-v2/awsiot

  • The installation instructions are defined in the setup.py:

#!/usr/bin/env python
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.

import codecs
import re
import os
from setuptools import setup, find_packages

VERSION_RE = re.compile(r""".*__version__ = ["'](.*?)['"]""", re.S)
PROJECT_DIR = os.path.dirname(os.path.realpath(__file__))

def _load_readme():
    readme_path = os.path.join(PROJECT_DIR, 'README.md')
    with codecs.open(readme_path, 'r', 'utf-8') as f:
        return f.read()

def _load_version():
    init_path = os.path.join(PROJECT_DIR, 'awsiot', '__init__.py')
    with open(init_path) as fp:
        return VERSION_RE.match(fp.read()).group(1)

setup(
    name='awsiotsdk',
    version=_load_version(),
    license='License :: OSI Approved :: Apache Software License',
    description='AWS IoT SDK based on the AWS Common Runtime',
    long_description=_load_readme(),
    long_description_content_type='text/markdown',
    author='AWS SDK Common Runtime Team',
    url='https://github.com/aws/aws-iot-device-sdk-python-v2',
    packages=find_packages(include=['awsiot*']),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: Apache Software License",
        "Operating System :: OS Independent",
    ],
    install_requires=[
        'awscrt==0.16.0',
    ],
    python_requires='>=3.7',
)
  • To install the package

python3 -m pip install ./aws-iot-device-sdk-python-v2

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