Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcodeforce committed Apr 27, 2024
2 parents ff4b2bb + 8cd3f57 commit 24bd0f3
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 101 deletions.
File renamed without changes.
40 changes: 40 additions & 0 deletions docs/coding/dp.md
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

52 changes: 52 additions & 0 deletions docs/coding/references.md
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.
82 changes: 32 additions & 50 deletions docs/index.md
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/)
17 changes: 0 additions & 17 deletions docs/python/compendium.md

This file was deleted.

19 changes: 0 additions & 19 deletions docs/python/python-summary.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
# Python Summary

See this good [tutorial from Programiz](https://www.programiz.com/python-programming#tutorial)

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.

[3.9 release Product documentation](https://docs.python.org/3.9/library/index.html)

## Getting started

Start a python interpreter: `python3` and start entering python code, develop a progname.py file and use `python3 progname.py`, or add `#!/usr/bin/env python3` to make it self runnable.

The code can be structured with function `def name():` and then with our without a main part:

```python
if __name__ == "__main__":
```

Better to use main statement when using objects and classes.

## Concepts

Expand Down
File renamed without changes.
33 changes: 19 additions & 14 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
site_name: Python code studies
repo_url: https://github.com/jbcodeforce/python-code
nav:
- Introduction: index.md
- Development environment: dev-env.md
- Summary: python/python-summary.md
- FAQ: python/faq.md
- Web App with Flask:
- Introduction:
- Basic: index.md
- Summary: python/python-summary.md
- Advanced: python/faq.md
- Coding:
- Development environment: coding/dev-env.md
- Design patterns: coding/dp.md
- Code studies: coding/references.md
- Data & AI:
- Pandas: data/pandas/index.md
- Web Data: data/webcrawling/readme.md
- Python and data sciences: https://jbcodeforce.github.io/ML-studies
- Web App:
- FastAPI: fastapi/index.md
- Flask studies and basic samples: flask/readme.md
- Flask Rest microservice with TDD and Docker: flask/flask-tdd-docker.md
- AWS: aws/index.md
- Web Data: webcrawling/readme.md
- Pandas: pandas/index.md
- Sub-projects:
- FastAPI: fastapi/index.md
- Python and Spark: https://jbcodeforce.github.io/spark-studies
- Python and data sciences: https://jbcodeforce.github.io/ML-studies
- Astronomy statistics and image processing: astronomy/README.md
- Flask webapp template with docker: https://github.com/jbcodeforce/python-code/tree/master/Flask/firstRESTapp
- Pydantic: python/pydantic.md
- Techno:
- AWS: techno/aws/index.md
- Python and Spark: https://jbcodeforce.github.io/spark-studies
- Astronomy statistics and image processing: astronomy/README.md
- Kafka consumer and producer: https://jbcodeforce.github.io/kafka-studies
- Compendium: python/compendium.md
- Back to main: https://jbcodeforce.github.io/

theme:
Expand Down
2 changes: 1 addition & 1 deletion web_server/websocket_server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

class ConnectionManager:
"""
Used to keep active connection with the multiple clients
Used to keep active connection with multiple clients
"""
def __init__(self):
self.active_connections: list[WebSocket] = []
Expand Down

0 comments on commit 24bd0f3

Please sign in to comment.