Python development

1. Introduction

Header page, see subpages for:

2. References

Pipenv — the officially recommended Python packaging tool from Python.org

https://pipenv-es.readthedocs.io/es/stable/


pyenv - tool used by pipenv for choosing the Python version specified in Pipfile

https://brain2life.hashnode.dev/how-to-install-pyenv-python-version-manager-on-ubuntu-2004


PyCharm - Python IDE by JetBrains

https://www.jetbrains.com/pycharm/


3. Installation

a) Install pipenv using pip (not apt, since version is too old and not maintained):

$ pip install pipenv

To activate this project's virtualenv, run pipenv shell.

Alternatively, run a command inside the virtualenv with pipenv run.


b) Install pyenv, so that pipenv can choose the Python version specified in Pipfile:

For Ubuntu 20.04

Prerequisite dependencies:

sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev \

libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \

libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev


c) Download and execute installation script:

curl https://pyenv.run | bash


d) Add the following entries into your ~/.bashrc file:

# pyenv

export PATH="$HOME/.pyenv/bin:$PATH"

eval "$(pyenv init --path)"

eval "$(pyenv virtualenv-init -)"


e) Restart your shell:

exec $SHELL


f) Validate installation:

pyenv --version


g) Uninstallation:

rm -fr ~/.pyenv


export PATH="$HOME/.pyenv/bin:$PATH"

eval "$(pyenv init --path)"

eval "$(pyenv virtualenv-init -)"


exec $SHELL


4. Makefile

Sample: sixqueue

.PHONY: pipenvini pipenvend clean


# ###################

# Pre-requisites:

#

# *pipenv installation (apt has too old version)

# $ pip install pipenv

#

# *pyenv (used by pipenv for choosing the Python version specified in Pipfile)

# https://brain2life.hashnode.dev/how-to-install-pyenv-python-version-manager-on-ubuntu-2004

#

# ###################


# INI CONFIG ########


# END CONFIG ########



help:

@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'


pipenvini: ## creates a new python environment with all dependencies (Pipfile)

pipenv install --dev


pipenvend: ## remove the python environment

pipenv --rm


clean: ## removes all build artifacts

rm -rf build/ dist/ .eggs/ && rm -rf src/*.egg-info/


test: ## run unit tests

# (use the discover method of the unittest module to auto-detect all the test files and run them)

pipenv run python -m unittest -v

# (all tests in tests)

# pipenv run python -m unittest tests

# (an specific test)

# pipenv run python -m unittest -v tests.test_mock_requests

# pipenv run python -m unittest -v tests.test_mock_requests_person_alumni


10. Visibility_ and __

A single leading underscore means you're not supposed to access it "from the outside".

Two leading underscores (w/o trailing underscores) carry the message even more forcefully.

80. Data types

Python has the following data types built-in by default, in these categories:

Text Type: str

Numeric Types: int, float, complex

Sequence Types: list, tuple, range

Mapping Type: dict

Set Types: set, frozenset

Boolean Type: bool

Binary Types: bytes, bytearray, memoryview

None Type: NoneType


81. Function declaration

For parameter type hints, see:

 https://stackoverflow.com/a/21384492/1323562

https://peps.python.org/pep-0484/

Eg w/ Optional (None) parameters:

from typing import Union


def new_id(calendarCode: str, subjectCode: str, studentCode: Union[int, None], groupingCode: Union[str, None]) -> str:

To annotate that the function can potentially return None in addition to the primary type, e.g. str, use Optional:

from typing import Optional


def test(flag: bool) -> Optional[str]:

91. Dictionary

Empty dict initialization:

# {} symbol to initialize empty dictionary

emptyDict = {}

# built-in  to initialize empty dictionary

emptyDict = dict()

Check if dict is empty:

# Using bool() to heck if dictionary isn't empty 

res = not bool(test_dict)

# Using not operator to check if dictionary isn't empty 

res = not test_dict

Access dict values by key:

mydict = dict()

mydict["uno"] = "One"

mydict["tres"] = "Three"


print(mydict.get("tres"))

print(mydict.get("segundo")) #None


print (mydict["uno"])

print (mydict["segundo"]) #ERROR! KeyError: 'segundo'

92. String

print("{2} has a friend called {0} and a sister called {1}".format("Betty", "Linda", "Daisy"))


tool="Unsupervised algorithms"

goal="patterns"

print("{title} try to find {aim} in the dataset".format(title=tool, aim=goal))


Eg: Tuple of strings

myTuple = ("John", "Peter", "Vicky")

x = "#".join(myTuple) 

Eg: Array of integers

d = dict()

d["codes"]= [11, 22, 33]


print(d) //{'codes': [11, 22, 33]}

print( ';'.join(map(str, d["codes"])) ) //11;22;33