-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a652359
commit 44e3ec4
Showing
11 changed files
with
144 additions
and
101 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Design Patterns | ||
|
||
Here are some examples on how to implement some classical design pattern in Python. | ||
|
||
## Singleton | ||
|
||
Singleton Pattern ensures that only one instance of a class exists and provides a global point of access to it. | ||
|
||
```python | ||
class _Glossary: | ||
_instance = None | ||
|
||
# method for the singleton class.... | ||
|
||
def Glossary(path:str ) -> _Glossary: | ||
"""Factory method to access to the unique instance""" | ||
if _Glossary._instance is None: | ||
_Glossary._instance = _Glossary() | ||
_Glossary._instance.load_glossary(path) | ||
return _Glossary._instance | ||
``` | ||
|
||
Access to a singleton from a FastAPI app using dependency injection for example | ||
|
||
```python | ||
def get_db() -> Optional[Database]: | ||
return Database() | ||
|
||
app = FastAPI() | ||
|
||
@app.get("/") | ||
def read_root(db: Optional[Database] = Depends(get_db)): | ||
if db: | ||
result = db.db.my_collection.find_one() | ||
return {"message": result} | ||
``` | ||
|
||
|
||
## Dependency injection | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Code references | ||
|
||
### Basics | ||
|
||
* [firstinput.py](https://github.com/jbcodeforce/python-code/blob/master/python-bible/firstinput.py) for reading user input | ||
* [Variable scope](https://github.com/jbcodeforce/python-code/blob/master/python-bible/scope.py) between global, local,... | ||
* [travis.py](https://github.com/jbcodeforce/python-code/blob/master/python-bible/travis.py) to play with lists, for in range() and conditions | ||
* [cinema.py](https://github.com/jbcodeforce/python-code/blob/master/python-bible/cinema.py) to illustrate how to use for dictionary | ||
* [Play with data structures](https://github.com/jbcodeforce/python-code/blob/master/python-bible/datastructure.py): lists, queues, matrix, sets, and more dictionaries, with how to navigate into those structures | ||
* [Reverse a word and add aye](https://github.com/jbcodeforce/python-code/blob/master/python-bible/pig.py), use loops, break, in voyals... | ||
* [Object Oriented Python](https://github.com/jbcodeforce/python-code/blob/master/python-bible/coins.py): classes and inheritance: using constructor (__init__()) and method with self argument. | ||
* [Modules, import, and packages](https://github.com/jbcodeforce/python-code/blob/master/python-bible/TestFiboModule.py). Do not forget to set PYTHONPATH to the root folder to access any new modules | ||
|
||
### Flask | ||
|
||
* [Flask web app hello world](https://github.com/jbcodeforce/python-code/blob/master/Flask/helloworld/firstApp.py) then [REST API end point with Flask](https://github.com/jbcodeforce/python-code/blob/master/firstRESTApp.py) and staticApp.py | ||
* [Flask serving a simple angular App](https://jbcodeforce.github.io/angular-sandbox) | ||
* [TDD with Flask app and docker from testdriven.io course](https://github.com/jbcodeforce/python-code/tree/master/Flask/flask-tdd-docker) | ||
|
||
|
||
### Algorithms | ||
|
||
* [Sorting arrays](https://github.com/jbcodeforce/python-code/blob/master/algorithms/sort.py): Bubblesort, selection sort, insertion sort and quicksort. | ||
* [Binary Tree with InOrderTraversal, PreOrderTraversal, PostOrderTraversal](https://github.com/jbcodeforce/python-code/blob/master/algorithms/traversalbinarytree.py). | ||
* [Binary search within a sorted array](https://github.com/jbcodeforce/python-code/blob/master/algorithms/binarySearch.py) which is a divide and conquer algorithm. | ||
* [Depth First Search, graph, Breadth First Search](https://github.com/jbcodeforce/python-code/blob/master/algorithms/Graph.py) DFS: explores the highest-depth nodes first before being forced to backtrack and expand shallower nodes. BFS: explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level. | ||
|
||
|
||
### Graphics | ||
|
||
* [Use a simple graphics API](https://github.com/jbcodeforce/python-code/blob/master/graphics/testgraphics.py) to create a window, draw circle and move them. | ||
* [Plotting normal curve with matplotlib](https://github.com/jbcodeforce/python-code/blob/master/matplotlib/PlotGaussian.py) | ||
|
||
### Web scrawling | ||
|
||
Use urllib and beautiful soup to remove html tags from a web page to get text to parse. See [this note](webcrawling/readme.md) for guidances | ||
|
||
* [Use regular expression (re module)](https://github.com/jbcodeforce/python-code/blob/master/web_data/countNumbers.py) to extract number from a text read from a file. | ||
|
||
### Astronomy | ||
|
||
See [detailed note here](astronomy/README.md) and code is under `astronomy` folder. | ||
|
||
### AWS | ||
|
||
To get some sample code to use AWS SDK [see this folder](https://github.com/jbcodeforce/python-code/blob/master/aws). | ||
|
||
## Unit testing | ||
|
||
* [unittest](https://docs.python.org/3/library/unittest.html#) | ||
* [Pytest framework](https://docs.pytest.org/en/7.3.x/#) | ||
* [moto for backend mockup]http://docs.getmoto.org/en/latest/index.html) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,57 @@ | ||
# Python Studies | ||
|
||
This repository regroups a bench of python codes from my own self-training and studies for web development, crawling, python best practice, and [raspberry PI](https://www.raspberrypi.org/) work. | ||
This repository regroup Python codes and notes from my own self-training and studies for web app development, crawling, Data AI, and even with [raspberry PI](https://www.raspberrypi.org/) work. | ||
|
||
## Language Advantages / disadvantages | ||
|
||
**Advantages:** | ||
|
||
* Interpreted with shell to start quickly, more concise language | ||
* 2nd most used programming language | ||
* May be now the 1st most used programming language | ||
* A lot of libraries, used a lot by data scientists | ||
* Combines functional and OOP. | ||
* Combines functional and Object Oriented Programming. | ||
* Raspberry PI language of choice | ||
* Even new libraries are done to implement server side user interface, with project like Streamlit, [gradio.app](https://www.gradio.app/docs/), [Nice gui](https://nicegui.io/); [taipy](taipy.io) | ||
|
||
**Disadvantages:** | ||
|
||
* Slow, not supporting well multi cpu / threading architecture | ||
* Not great for mobile and 3D game programming | ||
|
||
## Code in the order of knowledge acquisition | ||
## Getting started | ||
|
||
### Basics | ||
See this good [tutorial from Programiz](https://www.programiz.com/python-programming#tutorial) | ||
|
||
* [firstinput.py](https://github.com/jbcodeforce/python-code/blob/master/python-bible/firstinput.py) for reading user input | ||
* [Variable scope](https://github.com/jbcodeforce/python-code/blob/master/python-bible/scope.py) between global, local,... | ||
* [travis.py](https://github.com/jbcodeforce/python-code/blob/master/python-bible/travis.py) to play with lists, for in range() and conditions | ||
* [cinema.py](https://github.com/jbcodeforce/python-code/blob/master/python-bible/cinema.py) to illustrate how to use for dictionary | ||
* [Play with data structures](https://github.com/jbcodeforce/python-code/blob/master/python-bible/datastructure.py): lists, queues, matrix, sets, and more dictionaries, with how to navigate into those structures | ||
* [Reverse a word and add aye](https://github.com/jbcodeforce/python-code/blob/master/python-bible/pig.py), use loops, break, in voyals... | ||
* [Object Oriented Python](https://github.com/jbcodeforce/python-code/blob/master/python-bible/coins.py): classes and inheritance: using constructor (__init__()) and method with self argument. | ||
* [Modules, import, and packages](https://github.com/jbcodeforce/python-code/blob/master/python-bible/TestFiboModule.py). Do not forget to set PYTHONPATH to the root folder to access any new modules | ||
Python is an interpreted Object Oriented & functional language. It organizes the code in modules. | ||
Use blank to indent code block. The coding style is known as PEP8. | ||
|
||
### Flask | ||
[3.12 Release Product documentation](https://docs.python.org/3.12/library/index.html) | ||
|
||
* [Flask web app hello world](https://github.com/jbcodeforce/python-code/blob/master/Flask/helloworld/firstApp.py) then [REST API end point with Flask](https://github.com/jbcodeforce/python-code/blob/master/firstRESTApp.py) and staticApp.py | ||
* [Flask serving a simple angular App](https://jbcodeforce.github.io/angular-sandbox) | ||
* [TDD with Flask app and docker from testdriven.io course](https://github.com/jbcodeforce/python-code/tree/master/Flask/flask-tdd-docker) | ||
Use virtual environment to avoid impacting operating system python own libraries. | ||
|
||
```sh | ||
# create one virtual env: it can be reused between a lot of project. | ||
python -m venv .venv | ||
# on windows | ||
source .venv/Scripts/activate | ||
# on Mac | ||
source .venv/activate | ||
``` | ||
|
||
### Algorithms | ||
## Python readings | ||
|
||
* [Sorting arrays](https://github.com/jbcodeforce/python-code/blob/master/algorithms/sort.py): Bubblesort, selection sort, insertion sort and quicksort. | ||
* [Binary Tree with InOrderTraversal, PreOrderTraversal, PostOrderTraversal](https://github.com/jbcodeforce/python-code/blob/master/algorithms/traversalbinarytree.py). | ||
* [Binary search within a sorted array](https://github.com/jbcodeforce/python-code/blob/master/algorithms/binarySearch.py) which is a divide and conquer algorithm. | ||
* [Depth First Search, graph, Breadth First Search](https://github.com/jbcodeforce/python-code/blob/master/algorithms/Graph.py) DFS: explores the highest-depth nodes first before being forced to backtrack and expand shallower nodes. BFS: explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level. | ||
* [3.8 release Product documentation](https://docs.python.org/3.8/library/index.html) | ||
* [Python software foundation tutorial](http://docs.python.org/3/tutorial/index.html) | ||
* [Tutorial from Programiz](https://www.programiz.com/python-programming#tutorial) | ||
* [Using Docker For Python Web Development](https://vsupalov.com/docker-python-development/) | ||
* [The Flask Mega-Tutorial](https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world) | ||
* [Improve environment for Flask webapp](https://vsupalov.com/flask-megatutorial-review/) | ||
* [10 Steps to Set Up Your Python Project for Success](https://towardsdatascience.com/10-steps-to-set-up-your-python-project-for-success-14ff88b5d13) | ||
* [Kafka Python](https://github.com/confluentinc/confluent-kafka-python) | ||
* [Python 2.0 Quick Reference](http://www.brunningonline.net/simon/python/quick-ref2_0.html): old web site | ||
* [Getting started with pymongo a mongodb driver](https://www.mongodb.com/blog/post/getting-started-with-python-and-mongodb) | ||
* [pymongo documentation](https://api.mongodb.com/python/current/api/pymongo/collection.html) | ||
|
||
Statistics | ||
|
||
### Graphics | ||
|
||
* [Use a simple graphics API](https://github.com/jbcodeforce/python-code/blob/master/graphics/testgraphics.py) to create a window, draw circle and move them. | ||
* [Plotting normal curve with matplotlib](https://github.com/jbcodeforce/python-code/blob/master/matplotlib/PlotGaussian.py) | ||
|
||
### Web scrawling | ||
|
||
Use urllib and beautiful soup to remove html tags from a web page to get text to parse. See [this note](webcrawling/readme.md) for guidances | ||
|
||
* [Use regular expression (re module)](https://github.com/jbcodeforce/python-code/blob/master/web_data/countNumbers.py) to extract number from a text read from a file. | ||
|
||
### Astronomy | ||
|
||
See [detailed note here](astronomy/README.md) and code is under `astronomy` folder. | ||
|
||
### AWS | ||
|
||
To get some sample code to use AWS SDK [see this folder](https://github.com/jbcodeforce/python-code/blob/master/aws). | ||
|
||
## Unit testing | ||
|
||
* [unittest](https://docs.python.org/3/library/unittest.html#) | ||
* [Pytest framework](https://docs.pytest.org/en/7.3.x/#) | ||
* [moto for backend mockup]http://docs.getmoto.org/en/latest/index.html) | ||
|
||
## Some tricks | ||
|
||
* placing cursor to previous line and enter will copy the line to a new line | ||
* [The Statistics and Calculus with Python Workshop](https://learning.oreilly.com/library/view/the-statistics-and/9781800209763/) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters