Skip to content

rslakra/PyTheorem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyTheorem (Python)


The PyTheorem repository helps in the learning of the python language and contains the algorithms, abstract data types and implementation of them including the interview preparation algorithms in Python language.

Python Tests

Folder Structure Conventions


Although this layout is pretty straightforward, it has several drawbacks that arise as the app complexity increases. For example, it will be hard for you to reuse the application logic in other projects because all the functionality is bundled in webapp/__init__.py. If you split this functionality into modules instead, then you could reuse complete modules across different projects.

/
├── adts                            # an adts package/
│    ├── array                      # an array package/
│    ├── graph                      # a graph package/
│    │    ├── __init__.py           # The package initializer
│    │    ├── README.md             # Instructions and helpful links
│    │    └── /                     # 
│    ├── hash                       # a hash package/
│    ├── heap                       # a heap package/
│    ├── iterator                   # an iterator package/
│    ├── lang                       # a lang package/
│    ├── linkedlist                 # a linkedlist package/
│    ├── list                       # a list package/
│    ├── logs                       # a logs package/
│    ├── map                        # a map package/
│    ├── queue                      # a queue package/
│    ├── search                     # a search package/
│    ├── security                   # a security package/
│    ├── sort                       # a sort package/
│    ├── stack                      # a stack package/
│    ├── text                       # a text package/
│    ├── time                       # a time package/
│    ├── tree                       # a tree package/
│    ├── trie                       # a trie package/
│    ├── __init__.py                # The package initializer
│    └── README.md                  # The README file of ews module
├── algos                           # an algos package/
│    ├── array                      # an array package/
│    ├── graph                      # a graph package/
│    │    ├── __init__.py           # The package initializer
│    │    ├── README.md             # Instructions and helpful links
│    │    └── _                     # The package initializer
│    ├── hash                       # a hash package/
│    ├── heap                       # a heap package/
│    ├── iterator                   # an iterator package/
│    ├── lang                       # a lang package/
│    ├── linkedlist                 # a linkedlist package/
│    ├── list                       # a list package/
│    ├── logs                       # a logs package/
│    ├── map                        # a map package/
│    ├── queue                      # a queue package/
│    ├── search                     # a search package/
│    ├── security                   # a security package/
│    ├── sort                       # a sort package/
│    ├── stack                      # a stack package/
│    ├── text                       # a text package/
│    ├── time                       # a time package/
│    ├── tree                       # a tree package/
│    ├── trie                       # a trie package/
│    ├── __init__.py                # The package initializer
│    └── README.md                  # The README file of ews module
├── aptitude                        # an aptitude package/
├── aws                             # an AWS package/
├── configs                         # The configs package
├── core                            # The core package
├── domain                          # a domain package/
├── games                           # a games package/
├── quiz                            # a quiz package/
├── README.md                       # Instructions and helpful links
├── requirements.txt                # a list of package dependencies
├── robots.txt                      # tells which URLs the search engine crawlers can access on your site
└── /                               # 

Python Projects Structures

Folder Description
/apidoc the doc-generated API docs
/code the project files
/doc the documentation
/lib the C-language libraries
/scripts or /bin the that kind of command-line interface stuff
/tests the tests of the project

Building Application


Local Development

Check Python settings

python3 --version
python3 -m pip --version
python3 -m ensurepip --default-pip

Setup a virtual environment

python3 -m pip install virtualenv
python3 -m venv venv
source deactivate
source venv/bin/activate

Activate venv

source is Linux/macOS command and doesn't work in Windows.

  • Windows
venv\Scripts\activate
  • Mac OS/Linux
source venv/bin/activate

OR

. ./venv/bin/activate  

Output:

(venv) <UserName>@<HostName> PyTheorem %

The parenthesized (venv) in front of the prompt indicates that you’ve successfully activated the virtual environment.

Deactivate Virtual Env

deactivate

Output:

<UserName>@<HostName> PyTheorem %

Upgrade pip release

pip install --upgrade pip

Install Packages/Requirements (Dependencies)

  • Install at the system level
brew install python-requests
  • Install in specific Virtual Env
pip install requests
pip install beautifulsoup4
python -m pip install requests

Install Requirements

pip install -r requirements.txt

Save Requirements (Dependencies)

pip freeze > requirements.txt

Build Python Project

python -m build

Configuration Setup

Set a local configuration file. Create or update local .env configuration file.

pip install python-dotenv
cp default.env .env

Now, update the default local configurations as follows:

# App Configs
APP_HOST = 0.0.0.0
HOST = 0.0.0.0
APP_PORT = 8080
PORT = 8080
APP_ENV = develop
DEBUG = False
#
# Pool Configs
#
DEFAULT_POOL_SIZE = 1
RDS_POOL_SIZE = 1
#
# Logger Configs
#
LOG_FILE_NAME = 'PyTheorem.log'
#
# Database Configs
#
DB_HOSTNAME = 127.0.0.1
DB_PORT =
DB_NAME = PyTheorem
DB_USERNAME = PyTheorem
DB_PASSWORD = Password

By default, Flask will run the application on port 5000.

Run Flask Application

python -m flask --app webapp run --port 8080 --debug

By default, Flask runs the application on port 5000.

python wsgi.py

OR

#flask --app wsgi run
python -m flask --app wsgi run
# http://127.0.0.1:5000/PyTheorem

OR

python -m flask --app wsgi run --port 8080 --debug
# http://127.0.0.1:8080/PyTheorem

OR

# Production Mode

# equivalent to 'from app import app'
gunicorn wsgi:app
# gunicorn -w <n> 'wsgi:app'
gunicorn -w 2 'wsgi:app'
# http://127.0.0.1:8000/PyTheorem

gunicorn -c gunicorn.conf.py wsgi:app
# http://127.0.0.1:8080/PyTheorem

Note:- You can stop the development server by pressing Ctrl+C in your terminal.

Access Application

- [IWS on port 8080](http://127.0.0.1:8080/PyTheorem)
- [IWS on port 8000](http://127.0.0.1:8000/PyTheorem)
- [IWS on port 5000](http://127.0.0.1:5000/PyTheorem)

Testing

Unit Tests

python -m unittest
python -m unittest discover -s ./pytheorem/tests -p "test_*.py"

Performance Testing

# Run this in a separate terminal
# so that the load generation continues and you can carry on with the rest of the steps
kubectl run -i --tty load-generator --rm --image=busybox:1.28 --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"

Capacity Planning

CPU Bound Systems

  • Formula
RPS = TotalCore * (1/TaskDurationInSeconds)

i.e.:
4 * (1000/100) = 40

Total Cores Task Duration RPS
4 100ms 40
4 50ms 80
4 10ms 400

Reference

Python Basics

Logger Guide

Documentation

Load Balancing

Events in Distributed Systems

Makefile

Author


  • Rohtash Lakra

About

This repository contains the python project.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages