Friday, 10 May 2019

Introduction to Python

Python versions

To find out which version the executable python refers to, execute this:

$ python --version
Python 2.7.15rc1


Importing packages


import datetime
from jsonschema2db import JSONSchemaToPostgres

Getting Package Info


$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import JSONSchema2DB
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'JSONSchema2DB'
>>> import jsonschema2db
>>> help(jsonschema2db)
>>> exit()

help(package_name) opens a documentation mode which lists all classes, functions etc.:

Help on module jsonschema2db:

NAME
    jsonschema2db

CLASSES
    builtins.object
        JSONSchemaToDatabase
            JSONSchemaToPostgres
            JSONSchemaToRedshift
    
    class JSONSchemaToDatabase(builtins.object)
     |  JSONSchemaToDatabase is the mother class for everything
     |  
     |  :param schema: The JSON schema, as a native Python dict
     |  :param database_flavor: Either "postgres" or "redshift"
...

FILE
    /home/bojan/.local/lib/python3.6/site-packages/jsonschema2db.py

If we want to see docs for some particular class within the package we can start package examination from the top level and go deeper:

$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import scrapy
>>> help(scrapy)
>>> help(scrapy.http)
>>> help(scrapy.http.response)
>>> help(scrapy.http.response.Response)

Another example:

(venv) xxx:~/path/to/tensorflow-demo$ python
Python 3.6.8 (default, Oct  7 2019, 12:59:55) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help
Type help() for interactive help, or help(object) for help about object.
>>> help("packages")
No Python documentation found for 'packages'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.

>>> help("modules")
...
_operator           copyreg             nis                 tensorflow
_osx_support        crypt               nntplib             tensorflow_core
_pickle             cryptography        notebook            tensorflow_estimator

...

>>> help("tensorflow")

>>> help("tensorflow_core")



pydoc is is Python module which generates documentation about modules, classes, functions and methods.

First run:

$ pydoc modules

or

$ python 
Python 3.6.8 (default, Oct  7 2019, 12:59:55) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help('modules')

...to get a list of all modules.

Then run:

$ pydoc module

Example:

$ pydoc tensorflow_core
$ pydoc tensorflow_core.keras
$ pydoc tensorflow_core.keras.datasets.mnist

pydoc path:

$ which pydoc
/usr/bin/pydoc

pydoc can be used from virtual environment because. Virtual env's activate script exposes pydoc function (see section about virtual environments in this article).

How to find package version programmatically?


Almost every normal package in python assigns the variable .__version__ or VERSION to the current version. So if you want to find the version of some package you can do the following

import a
a.__version__ # or a.VERSION

[source]

Functions


Example:

def add(a, b):
    return a+b


Python 3 (3.5, from 2011) introduces (optional) function annotations:

def add(a: number, b: number) -> number:
    return a+b

After importing some module, we can use dir(module) to list all its variables and functions. To find out types of arguments and return value of some function we can use inspect.signature or pydoc.render_doc. Example:

print("inspect.signature(mnist.load_data): {}".format(inspect.signature(mnist.load_data)))

print("pydoc.render_doc(mnist.load_data): {}".format(pydoc.render_doc(mnist.load_data)))

introspection - How do I look inside a Python object? - Stack Overflow
PEP 257 -- Docstring Conventions | Python.org



Classes


class MyClass:

In Python 3 each class implicitly inherit from object.

Inheritance

class Derived(Base):


Member Functions

class C:
    def foo(self, a):
        ...

    def bar(self, b):
        ...
        self.foo(a)
        ...

Python call function within class - Stack Overflow


Data Types

List

Python List


Dictionary


Python Dictionary Methods
python dictionaries and JSON (crash course)

Environment




Special Variables


__name__
__main__


When the Python interpeter reads a source file, it first defines a few special variables e.g. __name__ variable. If you are running your module (the source file) as the main program, e.g.

$ python foo.py

...the interpreter will assign the hard-coded string "__main__" to the __name__ variable.

If there's a statement like this in the main program, or in some other module the main program imports:

# Suppose this is in some other main program.
import foo

The interpreter will search for your foo.py file (along with searching for a few other variants), and prior to executing that module, it will assign the name "foo" from the import statement to the __name__ variable, i.e.

# It's as if the interpreter inserts this at the top
# of your module when it's imported from another module.
__name__ = "foo"


Virtual Environments

Installing Python in Docker


https://stackoverflow.com/questions/22938679/error-trying-to-install-postgres-for-python-psycopg2


How to compare version strings?


How do I compare version numbers in Python?

from packaging import version
>>> version.parse("2.3.1") < version.parse("10.1.2")

If module packaging is not installed, you might get the following error:

No module named 'packaging'


Solution is to install it via pip (after optionally updating pip itself):

$ pip3 install --upgrade pip
$ pip3 install packaging

References and Further Reading

Learn Python Programming - The Definitive Guide
How to install multiple python packages at once using pip

No comments: