Python

General tips on python coding

Recipes

Installing

sudo apt update
sudo apt install software-properties-common

sudo add-apt-repository ppa:deadsnakes/ppa

sudo apt install python3.8
sudo apt-get install python3.8-dev

Example module docstring

# -*- coding: utf-8 -*-
"""Example Google style docstrings.

This module demonstrates documentation as specified by the `Google Python
Style Guide`_. Docstrings may extend over multiple lines. Sections are created
with a section header and a colon followed by a block of indented text.

Example:
    Examples can be given using either the ``Example`` or ``Examples``
    sections. Sections support any reStructuredText formatting, including
    literal blocks::

        $ python example_google.py

Section breaks are created by resuming unindented text. Section breaks
are also implicitly created anytime a new section starts.

Attributes:
    module_level_variable1 (int): Module level variables may be documented in
        either the ``Attributes`` section of the module docstring, or in an
        inline docstring immediately following the variable.

        Either form is acceptable, but the two should not be mixed. Choose
        one convention to document module level variables and be consistent
        with it.

Todo:
    * For module TODOs
    * You have to also use ``sphinx.ext.todo`` extension

.. _Google Python Style Guide:
   http://google.github.io/styleguide/pyguide.html

"""

Example function Docstring

virtualenv

poetry

Docs

Remove directory

Extract tar files

F-strings

Creating directory if it doesn't exist

Checking nans

Loading config for a specific component

Confusion matrix

Count elements in array

Passing parameters with dictionaries

Merge dictionaries

Store expressions in dictionaries

Factorial of given number

Most frequent value on a list

Get sizes of objects in bytes

Unified list without loops

Fastest way to iterate over rows in pandas DataFrame

Progress bar for loops

Packing and unpacking

Check if file exist

Download file

Generate a set from a list of list

Get intersection from to lists

Get a sorted by value dictionary

Sort a list of dictionaries by value

Reverse a list

Transpose a matrix

Get current time

Useful magic methods

__add__ , __repr__

Creating a package

having setup.py like:

and a module at pkg_name folder, execute

Reminders

  • include a README file detailing the files in your package and how to install the package.

  • Comment your code - use docstrings and inline comments where appropriate.

  • Refactor code when possible - if you find your functions are getting too long, then refactor your code!

  • Use object-oriented programming whenever it makes sense to do so.

  • You're encouraged to write unit tests! The coding exercises in this lesson contained unit tests, so you can use those tests as a model for your package.

  • Use GitHub for version control, and commit your work often.

As a reminder, your package should be placed in a folder with the following folders and files:

  • a folder with the name of your package that contains:

    • the Python code that makes up your package

    • a README.md file

    • an __init__.py

    • license.txt

    • setup.cfg

  • setup.py file

References

Frameworks

OOP

Creating a package

Last updated

Was this helpful?