Compare commits
6 Commits
docker
...
b251ae7e2e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b251ae7e2e | ||
|
|
e2711d6591 | ||
|
|
1b5d863dfa | ||
|
|
0e0eb64ce0 | ||
| 581d4999ba | |||
| 2b7ffd4dde |
281
Docs/Basic/05-oop.md
Normal file
281
Docs/Basic/05-oop.md
Normal file
@@ -0,0 +1,281 @@
|
||||
# 05 – Object-Oriented Programming (OOP) in Python
|
||||
|
||||
This document explains the basics of **Object-Oriented Programming (OOP)** in Python using simple examples.
|
||||
We cover:
|
||||
|
||||
* Classes and objects
|
||||
* Attributes and methods
|
||||
* Class attributes vs instance attributes
|
||||
* Inheritance
|
||||
* Special (magic) methods
|
||||
|
||||
---
|
||||
|
||||
## 1. Basic Class, Attribute, and Method
|
||||
|
||||
### Code
|
||||
|
||||
```python
|
||||
class test_class():
|
||||
def __init__(self, input):
|
||||
self.parm = input
|
||||
print("Class Created")
|
||||
|
||||
def result(self):
|
||||
print(f"param is : {self.parm}")
|
||||
|
||||
var = test_class('abbas')
|
||||
var.result()
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
#### Class
|
||||
|
||||
* `test_class` is a **class**, which acts as a blueprint for creating objects.
|
||||
|
||||
#### `__init__` method (Constructor)
|
||||
|
||||
* `__init__` is a **special method** that runs automatically when a new object is created.
|
||||
* `input` is a **parameter** passed when creating the object.
|
||||
* `self.parm = input` creates an **instance attribute** called `parm`.
|
||||
|
||||
#### Attribute
|
||||
|
||||
* `parm` is an **attribute** (a variable that belongs to the object).
|
||||
* It stores data specific to each object.
|
||||
|
||||
#### Method
|
||||
|
||||
* `result()` is a **method** (a function that belongs to the class).
|
||||
* It uses `self.parm` to access the object’s data.
|
||||
|
||||
#### Object Creation
|
||||
|
||||
```python
|
||||
var = test_class('abbas')
|
||||
```
|
||||
|
||||
* Creates an object named `var`.
|
||||
* Calls `__init__` automatically.
|
||||
|
||||
#### Method Call
|
||||
|
||||
```python
|
||||
var.result()
|
||||
```
|
||||
|
||||
* Calls the `result` method on the object.
|
||||
|
||||
---
|
||||
|
||||
## 2. Class Attributes vs Instance Attributes
|
||||
|
||||
### Code
|
||||
|
||||
```python
|
||||
class test_class():
|
||||
test_value = 'abbas'
|
||||
|
||||
def __init__(self, input):
|
||||
self.parm = input
|
||||
print("Class Created")
|
||||
|
||||
def result(self):
|
||||
print(f"param is : {self.parm}")
|
||||
|
||||
var = test_class('abbas')
|
||||
var2 = test_class('mmd')
|
||||
|
||||
var.result()
|
||||
var.test_value
|
||||
|
||||
var2.test_value = 'mmd'
|
||||
var2.test_value
|
||||
var.test_value
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
#### Class Attribute
|
||||
|
||||
```python
|
||||
test_value = 'abbas'
|
||||
```
|
||||
|
||||
* This is a **class attribute**.
|
||||
* It belongs to the class itself.
|
||||
* Shared by all objects unless overridden.
|
||||
|
||||
#### Instance Attribute
|
||||
|
||||
```python
|
||||
self.parm = input
|
||||
```
|
||||
|
||||
* This is an **instance attribute**.
|
||||
* Each object has its own value.
|
||||
|
||||
#### Behavior Analysis
|
||||
|
||||
```python
|
||||
var.test_value
|
||||
```
|
||||
|
||||
* Accesses the class attribute → `'abbas'`
|
||||
|
||||
```python
|
||||
var2.test_value = 'mmd'
|
||||
```
|
||||
|
||||
* Creates a **new instance attribute** for `var2`.
|
||||
* Does not change the class attribute.
|
||||
|
||||
```python
|
||||
var2.test_value
|
||||
```
|
||||
|
||||
* Returns `'mmd'` (instance attribute)
|
||||
|
||||
```python
|
||||
var.test_value
|
||||
```
|
||||
|
||||
* Still returns `'abbas'` (class attribute)
|
||||
|
||||
#### Key Rule
|
||||
|
||||
* Instance attributes override class attributes **only for that object**.
|
||||
|
||||
---
|
||||
|
||||
## 3. Inheritance
|
||||
|
||||
### Code
|
||||
|
||||
```python
|
||||
class class_1():
|
||||
def __init__(self):
|
||||
print("Class 1 Created")
|
||||
|
||||
def hi(self):
|
||||
print("Hi")
|
||||
|
||||
class class_2(class_1):
|
||||
def __init__(self):
|
||||
print("Class 2 Created")
|
||||
self.hi()
|
||||
|
||||
b = class_2()
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
#### Parent Class
|
||||
|
||||
```python
|
||||
class class_1():
|
||||
```
|
||||
|
||||
* `class_1` is the **parent (base) class**.
|
||||
|
||||
#### Child Class
|
||||
|
||||
```python
|
||||
class class_2(class_1):
|
||||
```
|
||||
|
||||
* `class_2` **inherits** from `class_1`.
|
||||
* It automatically has access to all public methods of `class_1`.
|
||||
|
||||
#### Method Usage
|
||||
|
||||
```python
|
||||
self.hi()
|
||||
```
|
||||
|
||||
* `hi()` is defined in `class_1`.
|
||||
* Because of inheritance, `class_2` can call it.
|
||||
|
||||
#### Output Order
|
||||
|
||||
```text
|
||||
Class 2 Created
|
||||
Hi
|
||||
```
|
||||
|
||||
#### Important Note
|
||||
|
||||
* `class_1.__init__()` is **not called automatically** here.
|
||||
* To call it, you would need:
|
||||
|
||||
```python
|
||||
super().__init__()
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Special (Magic) Methods
|
||||
|
||||
### Code
|
||||
|
||||
```python
|
||||
class class_1():
|
||||
def __init__(self):
|
||||
print("Class 1 Created")
|
||||
|
||||
def __len__(self):
|
||||
return 1
|
||||
|
||||
def __str__(self):
|
||||
return 'print command on class'
|
||||
|
||||
def __del__(self):
|
||||
return 'on del value'
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
Special methods start and end with **double underscores (`__`)** and control built-in behavior.
|
||||
|
||||
#### `__init__`
|
||||
|
||||
* Runs when an object is created.
|
||||
|
||||
#### `__len__`
|
||||
|
||||
```python
|
||||
len(object)
|
||||
```
|
||||
|
||||
* Defines the behavior of `len()` on the object.
|
||||
* Returns `1` in this example.
|
||||
|
||||
#### `__str__`
|
||||
|
||||
```python
|
||||
print(object)
|
||||
```
|
||||
|
||||
* Defines the string representation of the object.
|
||||
* Used by `print()` and `str()`.
|
||||
|
||||
#### `__del__`
|
||||
|
||||
* Runs when the object is deleted or garbage-collected.
|
||||
* Used rarely in modern Python.
|
||||
* Return value is ignored.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
* **Class**: Blueprint for objects
|
||||
* **Object**: Instance of a class
|
||||
* **Attribute**: Data stored in an object
|
||||
* **Method**: Function inside a class
|
||||
* **Class Attribute**: Shared across all objects
|
||||
* **Instance Attribute**: Unique per object
|
||||
* **Inheritance**: Child class reuses parent class logic
|
||||
* **Magic Methods**: Customize built-in Python behavior
|
||||
|
||||
218
Docs/Basic/06-pkg-modules.md
Normal file
218
Docs/Basic/06-pkg-modules.md
Normal file
@@ -0,0 +1,218 @@
|
||||
# 06 – Packages and Modules in Python
|
||||
|
||||
This document explains how **modules**, **packages**, and the `__name__` concept work in Python.
|
||||
These features help organize code, reuse functionality, and build scalable projects.
|
||||
|
||||
---
|
||||
|
||||
## 1. Importing External Modules
|
||||
|
||||
Python allows you to import **external libraries** installed in your environment.
|
||||
|
||||
### Example: Using the `emoji` Module
|
||||
|
||||
### Code
|
||||
|
||||
```python
|
||||
import emoji
|
||||
print(emoji.emojize("abbas is :red_heart:"))
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
* `import emoji` imports the entire `emoji` module.
|
||||
* `emoji.emojize()` converts emoji aliases into actual emoji characters.
|
||||
* You must use the module name (`emoji`) to access its functions.
|
||||
|
||||
---
|
||||
|
||||
### Importing a Specific Function
|
||||
|
||||
### Code
|
||||
|
||||
```python
|
||||
from emoji import emojize
|
||||
print(emojize("abbas is :red_heart:"))
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
* `from emoji import emojize` imports only the `emojize` function.
|
||||
* You can call the function directly without prefixing the module name.
|
||||
* This approach is cleaner when you only need a specific function.
|
||||
|
||||
---
|
||||
|
||||
## 2. Creating a Module
|
||||
|
||||
A **module** is a single Python file containing functions, classes, or variables.
|
||||
|
||||
### File Structure
|
||||
|
||||
```
|
||||
hi.py
|
||||
main.py
|
||||
```
|
||||
|
||||
### `hi.py`
|
||||
|
||||
```python
|
||||
def hi():
|
||||
print("Hi :)")
|
||||
```
|
||||
|
||||
### `main.py`
|
||||
|
||||
```python
|
||||
import hi
|
||||
hi.hi()
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
* `hi.py` is a module.
|
||||
* `hi()` is a function defined inside the module.
|
||||
* `import hi` loads the module.
|
||||
* `hi.hi()` calls the function from the module.
|
||||
|
||||
---
|
||||
|
||||
## 3. Creating a Package
|
||||
|
||||
A **package** is a directory that contains multiple modules.
|
||||
|
||||
### Package Structure
|
||||
|
||||
```
|
||||
honor/
|
||||
│── __init__.py
|
||||
│── hi.py
|
||||
main.py
|
||||
```
|
||||
|
||||
### `honor/hi.py`
|
||||
|
||||
```python
|
||||
def hello():
|
||||
print("Hi :)")
|
||||
```
|
||||
|
||||
### `honor/__init__.py`
|
||||
|
||||
```python
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
* The `honor` directory is a package.
|
||||
* `__init__.py` tells Python that this directory is a package.
|
||||
* The file can be empty, but it **must exist** (especially for older Python versions and clarity).
|
||||
|
||||
---
|
||||
|
||||
### Importing from a Package (Method 1)
|
||||
|
||||
### `main.py`
|
||||
|
||||
```python
|
||||
from honor import hi
|
||||
hi.hello()
|
||||
```
|
||||
|
||||
#### Explanation
|
||||
|
||||
* Imports the `hi` module from the `honor` package.
|
||||
* Accesses the function using `hi.hello()`.
|
||||
|
||||
---
|
||||
|
||||
### Importing from a Package (Method 2)
|
||||
|
||||
### `main.py`
|
||||
|
||||
```python
|
||||
from honor.hi import hello
|
||||
hello()
|
||||
```
|
||||
|
||||
#### Explanation
|
||||
|
||||
* Imports the `hello` function directly.
|
||||
* Allows calling the function without the module name.
|
||||
* Cleaner when only one function is needed.
|
||||
|
||||
---
|
||||
|
||||
## 4. The `__name__` Concept
|
||||
|
||||
Every Python file has a built-in variable called `__name__`.
|
||||
|
||||
### Code
|
||||
|
||||
```python
|
||||
print(__name__)
|
||||
```
|
||||
|
||||
### Behavior
|
||||
|
||||
#### When a File Is Run Directly
|
||||
|
||||
```bash
|
||||
python3 abbas.py
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```text
|
||||
__main__
|
||||
```
|
||||
|
||||
* This means the file is the **entry point** of the program.
|
||||
|
||||
#### When a File Is Imported
|
||||
|
||||
```python
|
||||
import abbas
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```text
|
||||
abbas
|
||||
```
|
||||
|
||||
* `__name__` is set to the **module name**, not `__main__`.
|
||||
|
||||
---
|
||||
|
||||
## 5. Why `__name__ == "__main__"` Is Important
|
||||
|
||||
This pattern allows code to run **only when the file is executed directly**, not when imported.
|
||||
|
||||
### Example
|
||||
|
||||
```python
|
||||
def main():
|
||||
print("Running directly")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
* The code inside the `if` block runs only when the file is executed directly.
|
||||
* Prevents unwanted execution when the file is imported as a module.
|
||||
* This is a standard Python best practice.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
* **Module**: A single `.py` file
|
||||
* **Package**: A directory containing modules
|
||||
* `__init__.py`: Marks a directory as a package
|
||||
* `import module`: Imports the whole module
|
||||
* `from module import item`: Imports specific items
|
||||
* `__name__`: Identifies how a file is executed
|
||||
|
||||
219
Docs/Basic/07-error-handeling.md
Normal file
219
Docs/Basic/07-error-handeling.md
Normal file
@@ -0,0 +1,219 @@
|
||||
# 06 – Error Handling, Linting, Formatting, and Testing in Python
|
||||
|
||||
This document explains how Python handles runtime errors, how to write safer code using `try / except`, and how to improve code quality using **linting**, **formatting**, and **unit testing** tools.
|
||||
|
||||
---
|
||||
|
||||
## 1. Error Handling with `try / except`
|
||||
|
||||
Python uses `try / except` blocks to handle runtime errors gracefully without crashing the program.
|
||||
|
||||
### Example
|
||||
|
||||
```python
|
||||
def abbas(a, b):
|
||||
try:
|
||||
res = a / b
|
||||
print(res)
|
||||
except ZeroDivisionError:
|
||||
print("Zero Number Detected")
|
||||
except Exception as e:
|
||||
print(f"Error Detected {e}")
|
||||
|
||||
abbas(1, 0)
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
* The code inside `try` is executed first.
|
||||
* If no error occurs, the result is printed.
|
||||
* If `b` is `0`, a `ZeroDivisionError` is raised.
|
||||
* The specific `ZeroDivisionError` block runs first.
|
||||
* Any other error is caught by the generic `Exception` block.
|
||||
|
||||
### Key Rule
|
||||
|
||||
* Always catch **specific exceptions first**.
|
||||
* Use `Exception` only as a fallback.
|
||||
|
||||
---
|
||||
|
||||
## 2. Full `try / except` Structure
|
||||
|
||||
Python supports additional blocks for more control.
|
||||
|
||||
### Syntax
|
||||
|
||||
```python
|
||||
try:
|
||||
# code that may raise an error
|
||||
except:
|
||||
# runs if an error occurs
|
||||
else:
|
||||
# runs if no error occurs
|
||||
finally:
|
||||
# always runs
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
* `try`: code that may fail
|
||||
* `except`: handles errors
|
||||
* `else`: runs only if no exception occurred
|
||||
* `finally`: runs no matter what (used for cleanup)
|
||||
|
||||
---
|
||||
|
||||
## 3. Linting with `pylint`
|
||||
|
||||
Linting analyzes code for:
|
||||
|
||||
* Syntax errors
|
||||
* Style problems
|
||||
* Bad practices
|
||||
|
||||
### Basic Command
|
||||
|
||||
```bash
|
||||
pylint main.py
|
||||
```
|
||||
|
||||
### Detailed Report
|
||||
|
||||
```bash
|
||||
pylint --report y main.py
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
* `pylint` gives a score and suggestions.
|
||||
* Helps maintain readable and maintainable code.
|
||||
* Commonly used in professional Python projects.
|
||||
|
||||
---
|
||||
|
||||
## 4. Code Formatting with `black`
|
||||
|
||||
`black` is an automatic code formatter that enforces a consistent style.
|
||||
|
||||
### Command
|
||||
|
||||
```bash
|
||||
black main.py
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
* Reformats code automatically.
|
||||
* Removes style debates.
|
||||
* Safe and widely used.
|
||||
|
||||
---
|
||||
|
||||
## 5. Unit Testing with `unittest`
|
||||
|
||||
Unit tests verify that individual parts of code behave as expected.
|
||||
|
||||
---
|
||||
|
||||
### Application Code
|
||||
|
||||
#### `abbas.py`
|
||||
|
||||
```python
|
||||
def bemola(a, b):
|
||||
try:
|
||||
res = a / b
|
||||
print(res)
|
||||
except ZeroDivisionError:
|
||||
print("Zero Number Detected")
|
||||
except Exception as e:
|
||||
print(f"Error Detected {e}")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Test Code
|
||||
|
||||
#### `abbas_test.py`
|
||||
|
||||
```python
|
||||
import unittest
|
||||
from abbas import bemola
|
||||
|
||||
class TestAbbas(unittest.TestCase):
|
||||
|
||||
def test_bemola(self):
|
||||
a = 10
|
||||
b = 2
|
||||
self.assertEqual(bemola(a, b), 5)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Explanation
|
||||
|
||||
#### `unittest.TestCase`
|
||||
|
||||
* Base class for writing test cases.
|
||||
|
||||
#### Test Method
|
||||
|
||||
```python
|
||||
def test_bemola(self):
|
||||
```
|
||||
|
||||
* Test methods must start with `test_`.
|
||||
|
||||
#### Assertion
|
||||
|
||||
```python
|
||||
self.assertEqual(bemola(a, b), 5)
|
||||
```
|
||||
|
||||
* Checks if the function returns the expected result.
|
||||
|
||||
---
|
||||
|
||||
### Important Note (Design Issue)
|
||||
|
||||
The function `bemola` **prints** the result but does not return it.
|
||||
|
||||
```python
|
||||
print(res)
|
||||
```
|
||||
|
||||
This causes the test to fail because the function returns `None`.
|
||||
|
||||
#### Correct Implementation
|
||||
|
||||
```python
|
||||
def bemola(a, b):
|
||||
try:
|
||||
return a / b
|
||||
except ZeroDivisionError:
|
||||
return "Zero Number Detected"
|
||||
except Exception as e:
|
||||
return f"Error Detected {e}"
|
||||
```
|
||||
|
||||
This version:
|
||||
|
||||
* Returns values instead of printing
|
||||
* Is testable
|
||||
* Follows best practices
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
* `try / except` prevents program crashes
|
||||
* `else` runs only when no error occurs
|
||||
* `finally` always runs
|
||||
* `pylint` improves code quality
|
||||
* `black` enforces formatting
|
||||
* `unittest` verifies correctness
|
||||
* Functions should **return values**, not print them, when tested
|
||||
182
Docs/Basic/08-decorators.md
Normal file
182
Docs/Basic/08-decorators.md
Normal file
@@ -0,0 +1,182 @@
|
||||
# 08 – Decorators in Python
|
||||
|
||||
This document explains **decorators**, how they work, and how they are used to extend function behavior without modifying the original function code.
|
||||
|
||||
---
|
||||
|
||||
## 1. What Is a Decorator?
|
||||
|
||||
A **decorator** is a function that:
|
||||
|
||||
* Takes another function as input
|
||||
* Adds extra behavior
|
||||
* Returns a new function
|
||||
|
||||
Decorators are commonly used for:
|
||||
|
||||
* Input validation
|
||||
* Logging
|
||||
* Authentication
|
||||
* Performance measurement
|
||||
* Access control
|
||||
|
||||
---
|
||||
|
||||
## 2. Basic Decorator Structure
|
||||
|
||||
A decorator has three layers:
|
||||
|
||||
1. The decorator function
|
||||
2. The wrapper function
|
||||
3. The original function
|
||||
|
||||
### General Pattern
|
||||
|
||||
```python
|
||||
def decorator(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
# extra behavior
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. Example: Input Validation Decorator
|
||||
|
||||
### Code
|
||||
|
||||
```python
|
||||
def check_number(func):
|
||||
def wrapper(a, b):
|
||||
if not (isinstance(a, (int, float)) and isinstance(b, (int, float))):
|
||||
print("Input must be numbers")
|
||||
return
|
||||
return func(a, b)
|
||||
return wrapper
|
||||
```
|
||||
|
||||
### Explanation
|
||||
|
||||
* `check_number` is the decorator.
|
||||
* `func` is the function being decorated.
|
||||
* `wrapper` replaces the original function.
|
||||
* `a` and `b` are the arguments passed to the original function.
|
||||
* `isinstance(a, (int, float))` ensures inputs are numeric.
|
||||
* If validation fails, execution stops.
|
||||
* If validation passes, the original function is called.
|
||||
|
||||
---
|
||||
|
||||
## 4. Using the Decorator with `@` Syntax
|
||||
|
||||
### Code
|
||||
|
||||
```python
|
||||
@check_number
|
||||
def bemola(a, b):
|
||||
try:
|
||||
res = a / b
|
||||
print(res)
|
||||
except ZeroDivisionError:
|
||||
print("Zero Number Detected")
|
||||
except Exception as e:
|
||||
print(f"Error Detected {e}")
|
||||
```
|
||||
|
||||
### What Happens Internally
|
||||
|
||||
This line:
|
||||
|
||||
```python
|
||||
@check_number
|
||||
```
|
||||
|
||||
Is equivalent to:
|
||||
|
||||
```python
|
||||
bemola = check_number(bemola)
|
||||
```
|
||||
|
||||
The function `bemola` is replaced by `wrapper`.
|
||||
|
||||
---
|
||||
|
||||
## 5. Execution Flow
|
||||
|
||||
When calling:
|
||||
|
||||
```python
|
||||
bemola(10, 2)
|
||||
```
|
||||
|
||||
The flow is:
|
||||
|
||||
1. `wrapper(10, 2)` is called
|
||||
2. Inputs are validated
|
||||
3. `func(10, 2)` is executed
|
||||
4. Result is printed
|
||||
|
||||
If calling:
|
||||
|
||||
```python
|
||||
bemola(10, "a")
|
||||
```
|
||||
|
||||
The output will be:
|
||||
|
||||
```text
|
||||
Input must be numbers
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. Why Use Decorators?
|
||||
|
||||
Without decorators, input validation would need to be repeated in every function.
|
||||
|
||||
Decorators allow:
|
||||
|
||||
* Reusable logic
|
||||
* Cleaner code
|
||||
* Separation of concerns
|
||||
|
||||
---
|
||||
|
||||
## 7. Limitations in This Example
|
||||
|
||||
* The decorator only works with exactly two arguments.
|
||||
* It does not preserve the original function’s metadata (`__name__`, `__doc__`).
|
||||
|
||||
---
|
||||
|
||||
## 8. Improved Version (Best Practice)
|
||||
|
||||
```python
|
||||
from functools import wraps
|
||||
|
||||
def check_number(func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
if not all(isinstance(x, (int, float)) for x in args):
|
||||
print("Input must be numbers")
|
||||
return
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
```
|
||||
|
||||
### Improvements
|
||||
|
||||
* Supports any number of arguments
|
||||
* Preserves function name and documentation
|
||||
* More reusable and professional
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
* Decorators modify function behavior without changing its code
|
||||
* They wrap functions inside another function
|
||||
* `@decorator` is syntactic sugar
|
||||
* Commonly used for validation, logging, and access control
|
||||
* Best practice is to use `*args`, `**kwargs`, and `functools.wraps`
|
||||
266
Docs/Basic/09-standard-libs.md
Normal file
266
Docs/Basic/09-standard-libs.md
Normal file
@@ -0,0 +1,266 @@
|
||||
# 09 – Python Standard Library
|
||||
|
||||
This document introduces some of the most commonly used **Python standard library** modules:
|
||||
|
||||
* `datetime`
|
||||
* `math`
|
||||
* `random`
|
||||
* `decimal`
|
||||
|
||||
These modules come bundled with Python and require no external installation.
|
||||
|
||||
---
|
||||
|
||||
## 1. Date and Time with `datetime`
|
||||
|
||||
The `datetime` module provides classes for working with dates and times.
|
||||
|
||||
---
|
||||
|
||||
### Working with Dates
|
||||
|
||||
#### Code
|
||||
|
||||
```python
|
||||
import datetime
|
||||
|
||||
date_1 = datetime.date(2026, 1, 1)
|
||||
|
||||
print(date_1.year)
|
||||
print(date_1.month)
|
||||
print(date_1.day)
|
||||
|
||||
print(date_1.weekday)
|
||||
print(date_1.ctime)
|
||||
```
|
||||
|
||||
#### Explanation
|
||||
|
||||
* `datetime.date(year, month, day)` creates a date object.
|
||||
* `.year`, `.month`, `.day` access individual components.
|
||||
|
||||
#### Important Note
|
||||
|
||||
```python
|
||||
date_1.weekday()
|
||||
```
|
||||
|
||||
* Returns the day of the week as an integer:
|
||||
|
||||
* Monday = 0
|
||||
* Sunday = 6
|
||||
|
||||
```python
|
||||
date_1.ctime()
|
||||
```
|
||||
|
||||
* Returns a human-readable string representation of the date.
|
||||
|
||||
---
|
||||
|
||||
### Working with Time
|
||||
|
||||
#### Code
|
||||
|
||||
```python
|
||||
time_1 = datetime.time(12, 12)
|
||||
|
||||
print(time_1.hour)
|
||||
print(time_1.min)
|
||||
```
|
||||
|
||||
#### Explanation
|
||||
|
||||
* `datetime.time(hour, minute)` creates a time object.
|
||||
* `.hour` returns the hour.
|
||||
* `.minute` returns the minute.
|
||||
|
||||
---
|
||||
|
||||
### Working with Date and Time Together
|
||||
|
||||
#### Code
|
||||
|
||||
```python
|
||||
abbas_birth = datetime.datetime(2026, 1, 1, 12, 12)
|
||||
today = datetime.date.today()
|
||||
now = datetime.datetime.now()
|
||||
|
||||
diff_time = now - abbas_birth
|
||||
```
|
||||
|
||||
#### Explanation
|
||||
|
||||
* `datetime.datetime` includes both date and time.
|
||||
* `date.today()` returns today’s date.
|
||||
* `datetime.now()` returns the current date and time.
|
||||
* Subtracting two `datetime` objects returns a `timedelta`.
|
||||
|
||||
---
|
||||
|
||||
## 2. Mathematical Operations with `math`
|
||||
|
||||
The `math` module provides advanced mathematical functions and constants.
|
||||
|
||||
---
|
||||
|
||||
### Mathematical Constants
|
||||
|
||||
```python
|
||||
import math
|
||||
|
||||
print(math.pi)
|
||||
print(math.e)
|
||||
print(math.inf)
|
||||
```
|
||||
|
||||
* `math.pi`: π constant
|
||||
* `math.e`: Euler’s number
|
||||
* `math.inf`: infinity
|
||||
|
||||
---
|
||||
|
||||
### Power and Rounding
|
||||
|
||||
```python
|
||||
print(math.pow(2, 3))
|
||||
|
||||
print(round(4.2))
|
||||
print(round(4.8))
|
||||
```
|
||||
|
||||
* `math.pow(a, b)` returns `a` raised to the power of `b`.
|
||||
* `round()` rounds to the nearest integer.
|
||||
|
||||
---
|
||||
|
||||
### Floor and Ceil
|
||||
|
||||
```python
|
||||
print(math.floor(4.2))
|
||||
print(math.floor(4.9))
|
||||
|
||||
print(math.ceil(4.2))
|
||||
print(math.ceil(4.9))
|
||||
```
|
||||
|
||||
* `floor`: rounds down
|
||||
* `ceil`: rounds up
|
||||
|
||||
---
|
||||
|
||||
### Logarithms
|
||||
|
||||
```python
|
||||
print(math.log(100, 10))
|
||||
```
|
||||
|
||||
* Returns the logarithm of 100 with base 10.
|
||||
|
||||
---
|
||||
|
||||
## 3. Random Values with `random`
|
||||
|
||||
The `random` module is used to generate pseudo-random values.
|
||||
|
||||
---
|
||||
|
||||
### Random Numbers
|
||||
|
||||
```python
|
||||
import random
|
||||
|
||||
print(random.randint(1, 6))
|
||||
print(random.random())
|
||||
```
|
||||
|
||||
* `randint(a, b)`: random integer between `a` and `b` (inclusive)
|
||||
* `random()`: random float between `0` and `1`
|
||||
|
||||
---
|
||||
|
||||
### Random Selection
|
||||
|
||||
```python
|
||||
number_list = list(range(15))
|
||||
print(random.choice(number_list))
|
||||
|
||||
char_list = ['a', 'm', 's']
|
||||
print(random.choice(char_list))
|
||||
```
|
||||
|
||||
* `choice()` selects a random element from a sequence.
|
||||
|
||||
---
|
||||
|
||||
### Shuffling
|
||||
|
||||
```python
|
||||
random.shuffle(number_list)
|
||||
print(number_list)
|
||||
```
|
||||
|
||||
* `shuffle()` randomly rearranges the list in place.
|
||||
|
||||
---
|
||||
|
||||
## 4. Decimal Precision with `decimal`
|
||||
|
||||
The `decimal` module provides precise decimal arithmetic, avoiding floating-point errors.
|
||||
|
||||
---
|
||||
|
||||
### Decimal Context
|
||||
|
||||
```python
|
||||
import decimal
|
||||
|
||||
print(decimal.getcontext())
|
||||
```
|
||||
|
||||
* Shows current precision and rounding settings.
|
||||
|
||||
---
|
||||
|
||||
### Float vs Decimal
|
||||
|
||||
```python
|
||||
print(decimal.Decimal(0.1))
|
||||
print(decimal.Decimal('0.1'))
|
||||
```
|
||||
|
||||
* Passing a float carries floating-point error.
|
||||
* Passing a string preserves exact value.
|
||||
|
||||
---
|
||||
|
||||
### Precision Comparison
|
||||
|
||||
```python
|
||||
print(0.1 + 0.2 == 0.3)
|
||||
```
|
||||
|
||||
Returns `False` due to floating-point precision issues.
|
||||
|
||||
```python
|
||||
print(decimal.Decimal(0.1) + decimal.Decimal(0.2) == decimal.Decimal(0.3))
|
||||
```
|
||||
|
||||
Still `False` because the floats are imprecise.
|
||||
|
||||
```python
|
||||
print(decimal.Decimal('0.1') + decimal.Decimal('0.2') == decimal.Decimal('0.3'))
|
||||
```
|
||||
|
||||
Returns `True` because strings preserve precision.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
* `datetime` handles dates and times
|
||||
* `math` provides mathematical constants and functions
|
||||
* `random` generates pseudo-random values
|
||||
* `decimal` solves floating-point precision problems
|
||||
* Always use strings when creating `Decimal` values
|
||||
|
||||
Reference in New Issue
Block a user