Skip to content

hovey/pyschool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

black coverage codecov

pyschool

The pyschool repository demonstrates

Best Practices, Pythonic Practices, and Design Patterns implemented in Python.

Best Practices

Pythonic Practices

  • Iterator
  • Decorator (of Python)
  • List comprehension
  • Error handling

Patterns

  • Adapter
  • Singleton
  • Decorator
  • Factory
    • Static Factory Method
    • Builder
    • Abstract Factory
  • Publish-Subscribe (aka "PubSub" and Observer)

Examples

  • Anatomy of a figure
  • Animation
  • Colors
  • Element scale versus density
  • Growth charts
  • Imports
  • Least squares
  • Midpoint differentiation
  • Oscillator
  • Quartiles
  • Scientific notation

Toolbox

Pythonic Patterns

Attributes

  • See the get_set example, which goes through a short code example, with evolution from bad, to better, to best implementations.

Client-Service

  • To come.

Decorators

Docstrings

Error Checking (ask for forgiveness, not permission)

Publish-Subscribe (aka Observer)

Import

  • When importing modules from non-local directories. Used often in client-service patterns.

Inheritance

  • A compact example, showing a verb astraction (e.g., speak) and inheritance of behavior, with a simple client-service architecture.
  • shapes

Model, View, Controller (MVC)

Unit Test

Examples from xyfigure_test.py:

$ python xyfigure_test.py                      # for terse interaction,
$ python -m unittest xyfigure_test             # for default interaction,
$ python -m unittest -v xyfigure_test          # for higher verbosity, and

$ python -m unittest xyfigure_test.TestImageDiff.test_same  # e.g., to test the test_same() method

Examples

Conda

  • virual environment (venv)
# from [Apollo/sibl] with all folders containin __init__.py file:
(base) conda create -n temp
(base) conda activate temp
(temp) conda install numpy scipy matplotlib
(temp) conda install pip
(temp) pip install -e .  # -e is development mode, if code updates, new pip install is not required
(temp) conda list
(temp) 
(temp) conda deactivate
(base) conda remove -n temp --all
# list all environments:
(base) conda info --envs
  • also, pip virtual environment (venv)
(base) python -m venv fire

Computation

Debugger

Example: From ~/sibl/xyfigure/test in VS Code, Run | Open Configurations and add to launch.json the following:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "/Users/Apollo/sibl/xyfigure/xyfigure.py",
            "args": ["signal_process_serialize.json"],
            "console": "integratedTerminal"
        }
    ]
}

Production

Once the code base has sufficient development, and it is ready for production, use the following steps:

# -----------------
# production server
# -----------------
$ cd ~/sibl/xyfigure
$ rm -r xyfigure.egg-info/
$ vim setup.py   # update setup.py, typically increment the version, located parent file README.md

# update server if necessary
$ python -m pip install --user --upgrade setuptools wheel
$ python -m pip install --user --upgrade twine

# build to the dist/ subdirectory
$ python setup.py sdist bdist_wheel

# assure the PyPI API token for the server is created on pypi.org and saved on the server at ~/.pypirc

# remove any old .gz and .whl files in dist/ subdirectory
$ cd dist/  #rm old .gz and old .whl
$ cd ../  # back to the ~/sibl/xyfigure directory

# deploy
$ python -m twine upload dist/*

# ------
# client
# ------
$ pip list
$ pip uninstall sibllib  # the old name
$ cd /nscratch/chovey/casco_sim/temp/
$ pip install --user xyfigure-0.0.4-py3-none-any.whl
$ pip list

References