Compare commits
1 Commits
0e0eb64ce0
...
docker
| Author | SHA1 | Date | |
|---|---|---|---|
| 42f8df2589 |
@@ -1,281 +0,0 @@
|
|||||||
# 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
|
|
||||||
|
|
||||||
@@ -1,218 +0,0 @@
|
|||||||
# 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
|
|
||||||
|
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
```python
|
||||||
|
import docker
|
||||||
|
import time
|
||||||
|
|
||||||
|
docker_client = docker.DockerClient(base_url="unix://var/run/docker.sock")
|
||||||
|
docker_client.ping()
|
||||||
|
|
||||||
|
print("All Networks:\n")
|
||||||
|
|
||||||
|
all_networks = docker_client.networks.list()
|
||||||
|
|
||||||
|
for network in all_networks:
|
||||||
|
print(network.name, network.id)
|
||||||
|
|
||||||
|
print("\nNetworks Named host and bridge:\n")
|
||||||
|
|
||||||
|
system_networks = docker_client.networks.list(names=["host", "bridge"])
|
||||||
|
|
||||||
|
for network in system_networks:
|
||||||
|
print(network.name, network.id)
|
||||||
|
|
||||||
|
print("\nNetwork With Custom ID:\n")
|
||||||
|
|
||||||
|
custom_id_networks = docker_client.networks.list(
|
||||||
|
ids=["29c9e588bb8e0db6445f2a2278a1c2f42e39dc163c0a404f744dc4139fe47d21"]
|
||||||
|
)
|
||||||
|
|
||||||
|
for network in custom_id_networks:
|
||||||
|
print(network.name, network.id)
|
||||||
|
|
||||||
|
print("\nNetwork With Custom ID (Including Attributes):\n")
|
||||||
|
|
||||||
|
custom_id_networks = docker_client.networks.list(
|
||||||
|
ids=["29c9e588bb8e0db6445f2a2278a1c2f42e39dc163c0a404f744dc4139fe47d21"]
|
||||||
|
)
|
||||||
|
|
||||||
|
for network in custom_id_networks:
|
||||||
|
print(network.name, network.id, network.attrs)
|
||||||
|
|
||||||
|
print("\nNetwork With Custom Filter:\n")
|
||||||
|
|
||||||
|
filtered_networks = docker_client.networks.list(
|
||||||
|
names=["gitea_default"],
|
||||||
|
filters={"driver": "bridge"}
|
||||||
|
)
|
||||||
|
|
||||||
|
for network in filtered_networks:
|
||||||
|
if network.attrs["Driver"]:
|
||||||
|
print(network.name, network.id)
|
||||||
|
|
||||||
|
print("\nNetwork With Custom Filter (Greedy):\n")
|
||||||
|
|
||||||
|
filtered_networks = docker_client.networks.list(
|
||||||
|
names=["gitea_default"],
|
||||||
|
filters={"driver": "bridge"},
|
||||||
|
greedy=True
|
||||||
|
)
|
||||||
|
|
||||||
|
for network in filtered_networks:
|
||||||
|
if network.attrs["Driver"]:
|
||||||
|
print(network.name, network.id, network.attrs)
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user