Python memo
Published on 19 November 2023
Install python on windows
Running python
cd mon_projet
python -m venv mon_venv
source mon_venv/bin/activate
# Sur windows
mon_venv\Scripts\activate
deactivate
install requirement
pip install -r requirements.txt
initiation
GoF Design Patterns in Python
GoF design patterns are a set of 23 design patterns that were described in the book "Design Patterns: Elements of Reusable Object-Oriented Software".
Name |
Description |
Factory Method |
Create objects without specifying the exact class to instantiate. |
Abstract Factory |
Create families of related or dependent objects without specifying their concrete class. |
Builder |
Separates the construction of a complex object from its representation so that the same construction process can create different representations. |
Prototype |
Create new objects by copying an existing prototype. |
Singleton |
Restricts the instantiation of a class to a single object. |
Adapter |
Converts the interface of a class into another interface the client expects. |
Bridge |
Decouples an abstraction from its implementation so that the two can vary independently. |
composite |
Compose objects into tree structures to represent part-whole hierarchies. |
Decorator |
Dynamically attach additional responsibilities to an object. |
Facade |
Provides a unified interface to a set of interfaces in a subsystem. |
Flyweight |
Reduces the cost of creating and using lightweight objects. |
Proxy |
Provides a substitute or placeholder for another object in order to control access to it. |
Chain of Responsibility |
Allows more than one object to handle a request. |
Command |
Encapsulates a request in an object, thereby allowing clients to be parameterized with different requests, queues, or logs, and supporting undoable operations. |
Interpreter |
Give a representation of the grammar of a language and use this representation to interpret sentences in this language. |
Iterator |
Provides a way to traverse the elements of an aggregate object sequentially without exposing its underlying representation. |
Mediator |
Enables reducing complex dependencies between objects by having them communicate only via a mediator object |
Reminder |
Provides the ability to restore an object to its previous state (without violating encapsulation). |
Observe |
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. |
State |
Allows an object to change its behavior when its internal state changes. |
Strategy |
Defines a family of algorithms, encapsulates each one, and makes them interchangeable. |
Template Method |
Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. |
Visitor |
Allows defining a new operation to be performed on an object structure without changing the classes of the objects on which it operates. |
Functional programming
Library |
URL |
description |
PyMonad |
PyMonad is an implementation of monadic data structures in Python based on programming languages such as Haskell and F# with implementations for the most commonly used monad types in functional programming.https://www.miguelfarrajota.com/2021/06/monads-in-python-with-pymonad/[article] |
|
PyFunctional |
PyFunctional is a Python library for functional programming that provides tools for working with pure functions and iterables. |
|
Toolz |
Toolz is a Python library for functional programming that provides tools for working with pure functions and iterables. |
|
Fn.py |
Fn.py is a Python library for functional programming that provides tools for working with pure functions and iterables. |
|
Coconut |
Coconut adds several features to Python for functional programming, including pattern matching.https://stackoverflow.com/questions/11909681/are-there-pattern-matching-functions-in-python-like-this[conversation sof] |
side effects
loops
# impérative way
result = []
for i in inputs:
x = f(g(i))
result.append(x)
# idiomatic way
result = map(comp(f,g), inputs)