Git Product home page Git Product logo

python-tutorial's Introduction

python-tutorial

Binder Build Docker container

Run the tutorial on your computer

You have two ways in which you can run the tutorial locally.

1. With a conda environment

0. Prerequisites

To run the tutorial locally, you should first install conda (or mamba).

It is also suggested that you have a recent version of git. Check out how to install git on your operating system.

1. Download the material

Go to the directory on your machine where you want to download the material and clone the repository:

git clone https://github.com/empa-scientific-it/python-tutorial

Alternatively, you can manually download a ZIP archive with the latest version of the material:

Download ZIP archive

Extract the archive in a directory of your choice.

2. Create a dedicated environment

Enter the tutorial folder with

cd /path/to/python-tutorial

You should now create a new environment with conda:

conda env create -f binder/environment.yml

Warning

If you are on Windows and using Command Prompt or the PowerShell, please make sure to adjust the paths in the commands above accordingly.

Then activate the environment with

conda activate python-tutorial

You can update the existing environment (that is, downloading the latest version of the packages) with:

conda env update -f binder/environment.yml

3. Launch the tutorial via Jupyter

Finally, launch JupyterLab with

jupyter lab

To update the existing environment, run

conda env update -f environment.yml

2. With Docker

Note

The following instructions are for Windows. With minor changes, the steps work on macOS or Linux as well.

  1. Install Docker Desktop: First, you need to install Docker Desktop on your Windows machine. You can download it from the official Docker website: https://www.docker.com/products/docker-desktop.

  2. Create a folder: Open File Explorer and create a new folder where you want to save the tutorial's materials. For example, you could create a folder called "python-tutorial" on your Desktop.

  3. Open PowerShell: Once Docker Desktop is installed, open PowerShell on your Windows machine. You can do this by pressing the "Windows" key and typing "PowerShell" in the search bar.

  4. Pull the Docker image: In PowerShell, run the following command to pull the "empascientificit/python-tutorial" Docker image:

docker pull ghcr.io/empa-scientific-it/python-tutorial:latest
  1. Run the Docker container: Once the image is downloaded, run the following command to start a Docker container from the image:
docker run -p  8888:8888  --name python_tutorial -v /path/to/python-tutorial:/home/jovyan/work ghcr.io/empa-scientific-it/python-tutorial:latest jupyter lab --ip 0.0.0.0 --no-browser

Replace /path/to/python-tutorial with the path to the folder you created in step 2, for example C:/Users/yourusername/Desktop/python-tutorial.

Note

The above command will mirror the content of your local folder (e.g., C:/Users/yourusername/Desktop/python-tutorial) to the work/ folder inside the container. In this way, every file or folder you copy or create into work/ will be saved on your machine, and will remain there even if you stop Docker.

  1. Access the Jupyter Notebook: Open a web browser and navigate to http://localhost:8888/lab. You should see the Jupyter Notebook interface. Enter the token provided in the PowerShell console to access the notebook. Alternatively, you can directly click on the link that appears in the PowerShell after the container has started.

You can now use the Jupyter in the Docker container to run the python-tutorial. When you're done, you can stop the container by pressing Ctrl+C in the PowerShell console.

Note

If you want to restart the container, you can simply run the command docker container start python_tutorial.

python-tutorial's People

Contributors

baffelli avatar despadam avatar edoardob90 avatar fabioacl avatar pre-commit-ci[bot] avatar ramador09 avatar yakutovicha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

python-tutorial's Issues

Python package structure

Advanced

Here is a list of pre-commit checks that I typically use for my project:

Replace lstrip method with string slicing

It turns out that the"".lstrip() method doesn't work as we were expecting. It removes all the characters that match the letters provided to the lstrip(). Here is the example.

In [2]: "aaaabbbbbccccd".lstrip("cba")
Out[2]: 'd'

I think we should replace it with a simple string slicing.

Input and output

Basic

  • Reading files:
    • open, close
    • binary vs text mode (encoding)
    • read, append, overwrite modes
    • writing and reading with iteration or with read_lines/write_lines
  • Reading and writing csv with the built-in csv module
  • Managing Paths with pathlib
    • creating paths
    • resolving paths
    • path manipulation (suffix, appending, renaming, globbing, ...)

Advanced

  • Streams with the io module
  • Implementing file-like behaviour on other objects (adding open/close methods to other resources)
  • Context managers for resource management (with keyword)
    • understanding context managers
    • implementing context managers to handle custom resources
  • Databases
    • local databases with sqlite3 module
    • database querying with pandas (maybe, it's not part of Python stdlib)

Functions Quiz Q2: Expected Answer Inconsistent with Tutorial

The tutorial states about type hints:

Python doesn't require you to explicitly declare a variable's type. However, when defining functions, you can add type hints in the function's signature.

Type hints improve code readability and can help catch potential type-related bugs early during development.

The Python interpreter does not enforce type hints and will not check them during runtime. Type hints are primarily intended for improving code readability, serving as documentation for developers, and making IDEs much more helpful when writing code.

However, the question "What is the purpose of type hints in Python function definitions?" is not correctly answered, when the answer "To make the code more readable" is selected.

Rephrase exercise 1 in control flow

The problems with find the pair (Exercise 2) have a misleading return type. Shall we return a list or just a number?
It seems we need to return the product of the triple, but it is not so clear from the question.

Python packages, modules and releases

Basic

  • Python modules to split a python program for easier maintenance and code reuse.
  • Creating a module and importing its content (note on import *)
  • Package - a collection of python modules.
  • Intra-package references (from ./.. import)
  • Making a python package installable (pyproject.toml)
  • Releasing python package on PyPI.

Advanced

  • Executing modules as scripts (if __name__ == "__main__")
  • Importing * from a package (use __all__).
  • Setting up Python package dependencies
  • Dependencies and optional dependencies of a Python package
  • Python management tools: setuptools, flit, hatch, poetry.

Multithreading in Python

Basics

  • What's Python Global Interpreter Lock

Advanced

  • Multithreading vs multiprocessing
  • Process pool and thread pool executor
  • Python can only run I/O bound threads concurrently, CPU bound work is blocking (GIL)
  • Alternatives to Multithreading:
    • Asyncio
    • Coroutines
  • Synchronisation primitives:
    • Channels
    • Locks
    • Mutexes

Control flow

Basics

  • Looping constructs and iterables
  • Altering and breaking out of loops: continue, break
  • if-else construct and problems with nesting too many branches
  • The else clause for loops

Advanced

  • The match construct (Python 3.10.x) to avoid nesting
  • Pros & cons of looping (when it's better to use functional programming methods)
  • Recursion and its limits
  • The built-in Exception class
  • Built-in exceptions: TypeError, NameError, KeyError, ZeroDivisionError, OverflowError
  • The try-except-finally construct
  • How to handle exceptions to have a stack trace useful for debugging

Basic datatypes: 3rd exercise on strings needs to be improved.

The solution to exercise 3 on strings is not strictly correct:

I provide a solution that is

my_string.split()

One might also think of the following solution that could also work:

my_string.split(" ")

However, those two approaches work differently with an empty string:

"".split()  # returns an empty list []
"".split(" ")  # returns a list with an empty string [""]

We should either adapt the description or change solution to .split(" ").

Functions

Basics

  • Writing a function: parameters, body, return statement
  • Annotations and docs
  • Parameters vs Arguments
  • Position and keyword arguments
  • How Python executes a function (definition vs execution)
  • Scope (global and local)

Advanced

  • Functions with mutable arguments and default values
  • Functions are objects
    • Functions can be stored in data structures
    • Functions can be passed to other functions
    • Functions can be nested
    • Functions can capture the local state (closures)
    • Functions can return other functions (decorators)
  • Functions and iterables: unpacking, the * and ** operators, *args and **kwargs
  • Lambda expressions and their purpose/limitations
  • Pure vs impure function (with side-effects), best practices with separating the two kinds

Improve feedback on exercises

Currently, we're using pytest to test users' answers against reference solutions. However, we want to enhance the feedback provided to users when their answer is incorrect or doesn't match the reference solution. We would like to explore alternative approaches that encourage users to explore diverse solutions and improve their coding skills while still receiving helpful guidance.

Here are some potential approaches to consider:

  1. Compare function signatures (we're sort of already doing that)
  2. Leverage Abstract Syntax Trees (AST)
  3. Test multiple reference solutions
  4. Create custom pytest assertions

A couple of comments to (2) and (4).

Abstract Syntax Trees (AST)

We can use Python's ast module to parse users' code and the reference solutions into ASTs. By comparing the structural similarities between the two ASTs, we can provide a useful feedback while allowing for diverse solutions. This encourages users to follow good coding practices without adhering to a specific solution.

Custom pytest assertions

Develop custom pytest assertions that evaluate users' code based on specific criteria, such as the proper use of loops, conditional statements, and other coding constructs. This way, we can provide feedback based on the user's understanding and application of core programming concepts.

Improve test output

Show the stdout even if the test suceeds...I think we had this in an earlier version.

Functional programming

Basic

  • Iteration and filtering using comprehensions
  • Reduction (useful for "sliding window" operations)
  • Combining iterators with itertools:
    • tee
    • zip
    • map & filter (lazy methods)
    • generalized zip
    • transposing

Advanced

  • Lazy computation and iteration with generators (yield)
  • itertools:
    • infinite iterations
    • groupby
  • functools:
    • caching
    • partial application (currying, partial objects)
    • single dispatch

Use AST module to provide hints for exercises

When we evaluate a cell, we could check for some of the most common mistakes that aren't syntax-related.
Example we saw during the course:

  • Assigning a new constant value to the the function input variables
  • Printing as a last statement in the function body
  • Missing return statement
  • Returning a "print" statement
  • Returning None

Add explanation of ipytest to FAQ.md

We should maybe add a few lines to the FAQ explaining the course participants how to run the test. We should explicitly warn them against modifying the signature or the name of the solution function and tell them that the cell magic will call the function for them, therefore there is no need for calling the function explicitly.

Topics of the advanced tutorial

Important
We agreed on the following definition of "advanced": something that requires the understanding of and build on what we said in the Intro Tutorial. We are assuming that all the participants are comfortable with every Intro topic.

Advanced topics

Python Libraries

Ipynbname does not work locally

Instead of finding the name of the notebook, ipynbname.name() raises a FileNotFoundError when working with a local ipython kernel in visual studio code. I suggest using the old way of loading tests where we manually specified the module where they resided. I anyway would prefer a mode explicit solution.

Extend introduction notebook.

  • Modify the timetable to put intro first
  • Extend information on tests
  • Add intro to Jupyter Lab:
    • Interface
    • Create/delete folders and files
    • Open Terminal

Naming: enumerate notebooks

It might be better to enumerate the notebooks (starting with 0-intro.ipynb) so that they are sorted in GitHub as well

Built-in types and data structures

Basic

  • Dynamic typing in Python (vs static)
  • build-in data types
    • floats and integers
    • strings
    • booleans
  • Basic operations with the build-in data types
  • Data structures
    • lists and tuples
    • dictionaries
    • sets
  • Mutable vs immutable types (the id function)
  • Basic operations with data structures.

Advanced

  • Explicit type annotation in Python. For more details see PEP3107, and PEP484
  • Typing package
  • Collections module
    • deque
    • object counters
    • name tuples
  • Best practices (exercises showing how using appropriate types is beneficial in some cases).
    • list vs set vs dict
    • deque

Added branch for testing of solutions

As discussed today with @edoardob90. We are trying to get test suites to run automatically with some cell magic.
Please have a look at the branch testsuite. Unfortunately I am not able to create a merge request because I accidentally pushed these changes to the main branch...let me know if you have a way to fix that other than my kludges.

Provide small exercises in the middle of notebooks

Currently, in many notebooks, the exercises are placed at the end, and they are often quite hard.

It would make more sense to do more "intermediate" exercises after we cover each topic to better prepare the students for the hard ones.

This will also suggest the students go through each topic more thoroughly.

Here is the list of notebooks that need to be adapted:

  • #118
  • Control flow
  • Input-Output
  • Functional programming
  • #117

Object Oriented Programming

Basics

  • Creating a class: a blueprint for objects
  • Methods and attributes
  • Objects instantiation
  • Special methods and the Python Data Model (a tour or __dunder__ methods)
  • Duck-typing

Advanced

  • The @property function to define attributes (setters and getters)
  • About private attributes: comparison with OOP languages like C++/Java
  • Inheritance
  • Write better classes with dataclasses and attrs

Load testuite in every notebook

I noticed that most notebooks don't use the cell magic %load_ext tutorial.tests.testsuite. We should add it at the top of every notebook. However, we also should remind people to run all cells above before working on their exercises, otherwise this won't work.

Add a quick introduction to Jupyter?

When running the tutorial we assumed that many things are obvious in Jupyter, but it turned out not to be the case. Users with no experience in Programming might still have trobles operating it.

I think a short introduction might be very helpful here.

Add .gitignore

This refers to the exercise on Modules and Packages. Just to avoid that students add unwanted files to the git repo.

OOP Exercise "The N-body Problem" Part 2: Sign Error in Description

To calculate the change of a moon's velocity, the description states:

If Gx = 5 (the x position of Ganymede) and Cx = 3, then Ganymede's x velocity changes by +1 (because Gx > Cx), and Callisto's x velocity must change by -1 (because Cx < Gx).

In order to reproduce the given example after 10 steps, I had to change sign of the "force", i.e. Ganymede's x velocity has to change by -1. It seems this is catched in the solution by the negative sign in

self.velocities[n] -= delta

I/O: fix test to `solution_read_write_file`

This test doesn't seem right:

def test_read_write_file(function_to_test):
input_file = get_data("lines.txt")
output_file = pl.Path("output.txt")
test_output_file = pl.Path("test_output.txt")
function_to_test(input_file, output_file)
reference_solution_read_write_file(input_file, test_output_file)
with open(output_file) as output_file, open(test_output_file) as tes:
assert output_file.readlines() == tes.readlines()

Topics survey

We can collect any feedback/suggestion here. You should also be able to modify the survey with the link I sent by email.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.