structure updates

This commit is contained in:
2024-05-01 12:28:44 -06:00
parent a689e58eea
commit aeba9bdb34
461 changed files with 0 additions and 0 deletions

162
tech_docs/python/Ansible.md Normal file
View File

@@ -0,0 +1,162 @@
In the realm of Python and Linux, focusing on seamlessly combining their capabilities for system administration and automation, `Ansible` emerges as an incredibly useful tool, albeit not a library in the traditional sense. Ansible is an open-source automation engine that automates software provisioning, configuration management, and application deployment. It uses YAML syntax for expressing automation jobs and SSH for communication between the control machine and target nodes, requiring no agents on the target machines.
While Ansible itself is not a Python library but an application written in Python, it heavily relies on Python and can be extended with custom modules written in Python. This makes it a powerful ally in automating Linux environments using Python. Here's an overview of how Ansible works and why it's valuable for Python developers working in Linux environments:
# `Ansible` Overview
## Installation
On the control machine (your computer or a central server that will manage other nodes), install Ansible using pip:
```
pip install ansible
```
## Basic Concepts
### Playbooks
Ansible's configuration, deployment, and orchestration language. They are expressed in YAML format and describe the tasks to be executed.
### Modules
Units of code Ansible executes. Modules can control system resources, like services or packages, or handle executing system commands.
### Inventory
Defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. The default location for the inventory file is `/etc/ansible/hosts`, but you can specify a different inventory file at the command line.
## Sample Playbook
Here's a basic example of an Ansible playbook that ensures the Apache web server is installed on all hosts in the `webservers` group.
```yaml
---
- name: Ensure Apache is installed
hosts: webservers
tasks:
- name: Install apache2 package
ansible.builtin.yum:
name: apache2
state: present
```
## Running a Playbook
To run an Ansible playbook, use the `ansible-playbook` command followed by the playbook file name.
```
ansible-playbook playbook.yml
```
## Why Use Ansible for Python and Linux?
- **Agentless**: No need to install any software on the nodes you're managing, reducing overhead and potential security vulnerabilities. Ansible only requires SSH access, which is available by default on Linux systems.
- **Idempotency**: Ensures that even if you run the same playbook multiple times, it will not change the system state beyond the desired configuration, preventing unintended side-effects.
- **Extensibility**: Write custom modules and plugins in Python to extend Ansible's capabilities, allowing for automation tasks that are tailored to your specific needs.
- **Integration**: Ansible can be integrated into Python applications to automate the provisioning and management of the infrastructure they run on, leveraging Python's scripting capabilities to interact with Ansible playbooks and results.
While Ansible is an automation tool that uses YAML for its playbook syntax, its foundation in Python and ability to integrate with Python applications make it a powerful tool for system administrators and developers working in Linux environments, especially those already familiar with Python.
Ansible's design philosophy centers around simplicity and the ability to automate complex multi-tier IT application environments. It provides a clear and concise way to manage configuration files, deployments, and service orchestration all from a centralized location, without needing to modify the servers manually or manage scripts on those servers.
---
YAML, which stands for "YAML Ain't Markup Language," is a human-readable data serialization language. It's commonly used for configuration files and in applications where data is being stored or transmitted. YAML is particularly prevalent in the DevOps domain, with tools like Ansible relying on YAML for playbook definitions. Understanding YAML's structure and syntax is essential for effectively utilizing these tools and integrating YAML data handling into Python applications.
### YAML Structure and Syntax
#### Basic Syntax:
- **Indentation** is used to denote structure.
- **Colons (`:`)** separate keys and values.
- **Dashes (`-`)** are used to create lists.
- **Comments** are marked with `#`.
#### Simple Key-Value Pairs:
```yaml
key: value
text: Hello, World!
number: 10
```
#### Lists:
```yaml
items:
- item1
- item2
- item3
```
#### Dictionaries (Maps):
```yaml
person:
name: John Doe
age: 30
```
#### Lists of Dictionaries:
```yaml
employees:
- name: John Doe
job: Developer
- name: Jane Doe
job: Designer
```
#### Multiline Strings:
```yaml
multi_line_text: |
This is a long piece of text.
It spans multiple lines.
```
### Working with YAML in Python
Python's `PyYAML` library allows you to easily parse YAML into Python dictionaries and lists, and vice versa. This makes it straightforward to work with YAML data within your Python applications.
#### Installation:
To work with YAML in Python, you first need to install the `PyYAML` package:
```
pip install PyYAML
```
#### Reading YAML:
To read YAML data from a file or a string, you use `yaml.load()` or `yaml.safe_load()`. It's recommended to use `yaml.safe_load()` to avoid executing arbitrary Python objects.
```python
import yaml
# From a string
yaml_data = """
items:
- item1
- item2
"""
data = yaml.safe_load(yaml_data)
print(data)
# From a file
with open('data.yaml', 'r') as file:
data = yaml.safe_load(file)
print(data)
```
#### Writing YAML:
To serialize Python objects into a YAML string or file, you can use `yaml.dump()`.
```python
import yaml
data = {
'person': {'name': 'John Doe', 'age': 30}
}
# To a string
yaml_str = yaml.dump(data)
print(yaml_str)
# To a file
with open('output.yaml', 'w') as file:
yaml.dump(data, file)
```
### Integration with Ansible and DevOps Tools
Understanding YAML and being proficient in manipulating YAML data with Python are crucial skills for DevOps, especially when working with tools like Ansible. This allows you to:
- Dynamically generate Ansible playbooks.
- Parse output from Ansible runs.
- Integrate Ansible workflows with Python applications.
The simplicity and human-readable nature of YAML, combined with the power of Python for data manipulation, offer a potent toolset for automation, configuration management, and DevOps tasks.

View File

@@ -0,0 +1,100 @@
For web scraping and working with HTML data in Python, `Beautiful Soup` is a highly useful library. It simplifies the process of extracting information from websites, allowing you to parse HTML and XML documents and navigate the parse tree or search for specific elements. Here's a concise reference guide for common use cases with `Beautiful Soup`, designed to help get you started with web scraping tasks:
# `Beautiful Soup` Reference Guide
## Installation
```
pip install beautifulsoup4
```
Note: You'll also need a parser like `lxml` or `html.parser`. For `lxml`, install it via `pip install lxml`.
## Basic Usage
### Importing Beautiful Soup
```python
from bs4 import BeautifulSoup
```
### Loading HTML Content
```python
# Using a string
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
# Using a web page content fetched with requests
import requests
response = requests.get('http://example.com')
soup = BeautifulSoup(response.content, 'html.parser')
```
## Navigating the Parse Tree
### Accessing Tags
```python
# Access the title element
title_tag = soup.title
# Access the name of the tag
print(title_tag.name)
# Access the text within the title tag
print(title_tag.string)
```
### Finding All Instances of a Tag
```python
# Find all 'a' tags
a_tags = soup.find_all('a')
```
### Accessing Attributes
```python
# Access the first 'p' tag
p_tag = soup.find('p')
# Access its 'class' attribute
p_class = p_tag['class']
```
## Searching the Tree
### find and find_all
```python
# Find the first 'a' tag
first_a_tag = soup.find('a')
# Find all 'a' tags with a specific class
specific_a_tags = soup.find_all('a', class_='sister')
# Find using a CSS selector
css_select_tags = soup.select('p.myclass')
```
### Getting Data from Tags
```python
# Get text content from a tag
for tag in soup.find_all('a'):
print(tag.get_text())
# Get a specific attribute value
for tag in soup.find_all('a'):
print(tag.get('href'))
```
## Practical Example: Extracting Data from a Page
```python
# Assuming you have fetched a page with product listings
for product in soup.find_all('div', class_='product'):
name = product.h2.text
price = product.find('span', class_='price').text
print(f'Product: {name}, Price: {price}')
```
`Beautiful Soup` is designed to make your web scraping code more human-readable and concise. It's incredibly effective at parsing messy web HTML content and offers both simplicity and flexibility, making it accessible for beginners yet powerful enough for advanced users. This guide covers basic functionalities, but `Beautiful Soup` supports a wide array of parsing and navigating techniques, making it a versatile tool in your web scraping toolkit.
Beautiful Soup's flexibility and ease of use make it an excellent choice for both novice programmers and professional developers needing to extract information from the web, parse documents, or scrape data from websites efficiently.

122
tech_docs/python/Bokeh.md Normal file
View File

@@ -0,0 +1,122 @@
A very useful Python library, particularly for creating interactive visualizations in web browsers, is `Bokeh`. Bokeh enables the building of complex statistical plots quickly and through simple commands. It's highly versatile, allowing for the creation of interactive plots, dashboards, and data applications with rich, web-based visualization capabilities. Below is a concise reference guide for common use cases with `Bokeh`, formatted in Markdown syntax:
# `Bokeh` Reference Guide
## Installation
```
pip install bokeh
```
## Basic Plotting
### Importing Bokeh
```python
from bokeh.plotting import figure, show, output_file
```
### Creating a Simple Line Plot
```python
# Prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
# Output to static HTML file (opens in browser)
output_file("lines.html")
# Create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
# Add a line renderer with legend and line thickness
p.line(x, y, legend_label="Temp.", line_width=2)
# Show the results
show(p)
```
## Interactive Plots
### Adding Tools
```python
from bokeh.models import PanTool, ResetTool, HoverTool
# Adding pan, reset functionality
p.add_tools(PanTool(), ResetTool())
# Adding hover tool
hover = HoverTool()
hover.tooltips=[
("Index", "$index"),
("(x,y)", "($x, $y)"),
]
p.add_tools(hover)
```
### Widgets and Interactivity
```python
from bokeh.layouts import column
from bokeh.models import Slider, Button
from bokeh.io import curdoc
# Create some widgets
slider = Slider(start=0, end=10, value=1, step=.1, title="Stuff")
button = Button(label="Press me")
# Update function
def update():
# Update function for widgets
...
# Button click event
button.on_click(update)
# Arrange widgets and plot into a layout
layout = column(slider, button, p)
# Add the layout to the current document
curdoc().add_root(layout)
```
## Embedding and Linking Plots
### Linking Plots Together
```python
from bokeh.layouts import gridplot
# Linking axes
p1 = figure(width=250, height=250)
p2 = figure(width=250, height=250, x_range=p1.x_range, y_range=p1.y_range)
p3 = figure(width=250, height=250, x_range=p1.x_range)
# Arrange plots in a grid
grid = gridplot([[p1, p2], [None, p3]])
show(grid)
```
### Embedding Bokeh Plots in HTML
```python
from bokeh.embed import components
script, div = components(p)
# `script` is a <script> tag that contains the data for your plot
# `div` is a <div> tag that the plot view is loaded into
```
## Server Applications
### Running a Bokeh Server
```shell
# Save your script as, e.g., `myapp.py`
# Run the Bokeh server
bokeh serve --show myapp.py
```
```python
# Inside myapp.py, use curdoc to add your layout, figures, etc.
from bokeh.io import curdoc
# Define plots and widgets here
curdoc().add_root(layout)
```
`Bokeh` excels in creating interactive and visually appealing plots that can be easily embedded into web pages or displayed as part of a Python notebook. Its capability to handle large, dynamic data sets makes it suitable for real-time data applications, and its interactivity features engage users in exploring the data more deeply. This guide introduces the basics, but `Bokeh`'s comprehensive features enable much more complex data visualization and interactive applications.

84
tech_docs/python/Click.md Normal file
View File

@@ -0,0 +1,84 @@
In the intersection of Python and Linux for creating powerful command-line interfaces (CLI) and automating shell tasks, `Click` is a standout library. Designed to make the process of writing command-line tools quick and easy by removing much of the boilerplate code typically required, Click offers a clean and modular way to configure command behaviors, parse command-line arguments, and generate help pages. It's particularly well-suited for developers and system administrators who want to build intuitive CLI tools for automation tasks on Linux systems.
### Click Reference Guide
#### Installation
You can install Click using pip:
```sh
pip install click
```
#### Basic Usage
##### Creating a Simple Command
Click allows you to easily turn Python functions into command-line commands.
```python
import click
@click.command()
@click.option('--name', prompt='Your name', help='The person to greet.')
def hello(name):
click.echo(f"Hello, {name}!")
if __name__ == '__main__':
hello()
```
Running this script from the command line and providing a name either via the `--name` option or the prompt will print a greeting. The `@click.option` decorator is used to define command-line options.
##### Grouping Commands
Click supports grouping multiple commands together into a single CLI tool, making it easy to build complex applications.
```python
@click.group()
def cli():
pass
@click.command()
def initdb():
click.echo('Initialized the database')
@click.command()
def dropdb():
click.echo('Dropped the database')
cli.add_command(initdb)
cli.add_command(dropdb)
```
With this setup, running the script with `initdb` or `dropdb` as arguments will execute the respective functions.
#### Advanced Features
##### Nested Commands and Namespaces
Click supports nesting command groups, allowing for a hierarchical command structure that is useful for larger applications with multiple sub-commands.
##### Automatic Help Page Generation
Click automatically generates help pages for your commands and options, based on the decorators you've used in your code. This includes descriptions and option defaults, reducing the need for manual documentation.
##### Prompts and Confirmations
Click can automatically prompt users for input if required options are not provided in the command line, and it can easily handle confirmation dialogs.
```python
@click.command()
@click.option('--yes', is_flag=True, help='Agree to all prompts')
def deploy(yes):
if yes or click.confirm('Do you want to continue?'):
click.echo('Deploying...')
else:
click.echo('Cancelled.')
```
#### Use Cases
- **System Administration Tools**: Building custom scripts for managing server environments, automating deployments, and performing system checks.
- **DevOps Automation**: Creating CLI tools that interface with APIs, manage cloud resources, or automate CI/CD pipelines.
- **File and Data Processing**: Writing command-line applications to manipulate files, process data, or generate reports.
#### Integration with Linux Systems
Click integrates seamlessly with the Linux command-line environment, supporting ANSI colors and styles, auto-completion, and even progress bars for long-running operations.
#### Security Considerations
When accepting input from the command line, especially for tools that perform system modifications, it's crucial to validate input thoroughly to avoid security vulnerabilities like command injection.
Click enhances the Python CLI experience by simplifying command-line application development, focusing on ease of use and composability. Its intuitive design encourages the creation of modular and reusable CLI tools, making it an invaluable resource for Python developers and system administrators working in Linux environments.

View File

@@ -0,0 +1,119 @@
In the realm of Python and Linux, especially for encryption and security, `Cryptography` is a standout library. It offers both high-level recipes and low-level cryptographic primitives, making it suitable for many tasks, from encrypting sensitive data in a Python application to verifying digital signatures. Its design focuses on ease of use and avoiding common security mistakes, making it a valuable tool for developers and system administrators aiming to enhance the security of their Python applications within Linux environments.
### Cryptography Reference Guide
#### Installation
To install the `Cryptography` library, you can use pip:
```sh
pip install cryptography
```
#### Basic Usage
##### Encrypting and Decrypting Data
`Cryptography` makes it straightforward to encrypt and decrypt data securely. Here's an example using Fernet symmetric encryption:
```python
from cryptography.fernet import Fernet
# Generate a key
key = Fernet.generate_key()
cipher_suite = Fernet(key)
# Encrypt some data
text = b"Secret message!"
encrypted_text = cipher_suite.encrypt(text)
print(encrypted_text)
# Decrypt the data
decrypted_text = cipher_suite.decrypt(encrypted_text)
print(decrypted_text)
```
##### Storing and Using Keys Securely
In a real-world application, you would need to store the encryption key securely. Linux systems offer several ways to do this, such as using environment variables or, for more sensitive keys, leveraging hardware security modules (HSM) or dedicated secret management services.
##### Working with SSL/TLS
For projects that require secure communication over the network, `Cryptography` provides tools to work with SSL/TLS protocols, allowing for the creation and management of certificates:
```python
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.serialization import BestAvailableEncryption
# Generate a private key
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
# Create a self-signed certificate
subject = issuer = x509.Name([
x509.NameAttribute(NameOID.COUNTRY_NAME, u"US"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, u"California"),
x509.NameAttribute(NameOID.LOCALITY_NAME, u"San Francisco"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, u"My Company"),
x509.NameAttribute(NameOID.COMMON_NAME, u"mysite.com"),
])
certificate = x509.CertificateBuilder().subject_name(
subject
).issuer_name(
issuer
).public_key(
private_key.public_key()
).serial_number(
x509.random_serial_number()
).not_valid_before(
datetime.datetime.utcnow()
).not_valid_after(
# Our certificate will be valid for 10 days
datetime.datetime.utcnow() + datetime.timedelta(days=10)
).sign(private_key, hashes.SHA256())
```
##### Digital Signatures
`Cryptography` supports creating and verifying digital signatures, an essential feature for verifying the integrity and authenticity of messages or files:
```python
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
# Sign a message
message = b"A message I want to sign"
signature = private_key.sign(
message,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
# Verify the signature
public_key = private_key.public_key()
public_key.verify(
signature,
message,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
```
### Integration with Linux Systems
The `Cryptography` library integrates seamlessly with Linux, enabling secure handling of cryptographic operations for scripts and applications running on Linux servers. It can be used to secure data at rest, data in transit, and for authentication and authorization purposes.
### Use Cases
- Securely storing and accessing sensitive application configuration.
- Encrypting data before storing it in a database.
- Creating secure communication channels for microservices.
- Implementing secure file transfer mechanisms.
### Security Considerations
While `Cryptography` aims to make cryptographic operations safer and easier, it's crucial to stay updated on best practices for cryptography and key management. Regularly update the library to incorporate security fixes and improvements.
`Cryptography` serves as a bridge between Python's ease of use and the stringent security requirements of modern applications on Linux, offering developers and system administrators a powerful toolset for securing applications and data.

95
tech_docs/python/Dash.md Normal file
View File

@@ -0,0 +1,95 @@
For creating interactive web applications from Python scripts, `Dash` by Plotly stands out as a very useful library. Dash enables Python developers to craft interactive web-based data visualizations without requiring in-depth knowledge of web development. Built on top of Flask, Plotly.js, and React.js, Dash is ideal for building data visualization apps with highly custom user interfaces in pure Python. It's especially suited for anyone interested in data science, financial analysis, or building analytical web applications. Here's a concise reference guide for common use cases with `Dash`:
# `Dash` Reference Guide
## Installation
```
pip install dash
```
## Basic Usage
### Creating a Simple Dash App
```python
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
# Initialize the app
app = dash.Dash(__name__)
# Define the app layout
app.layout = html.Div(children=[
html.H1(children='Hello Dash'),
html.Div(children='''Dash: A web application framework for Python.'''),
dcc.Graph(
id='example-graph',
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
],
'layout': {
'title': 'Dash Data Visualization'
}
}
)
])
# Run the app
if __name__ == '__main__':
app.run_server(debug=True)
```
## Adding Callbacks for Interactivity
Callbacks in Dash allow you to make the application interactive. You define callbacks that watch for changes in the UI, process the input data, and update the UI accordingly.
```python
@app.callback(
Output('output-component', 'property-to-update'),
[Input('input-component', 'property-to-watch')]
)
def update_output(input_value):
return 'You have entered "{}"'.format(input_value)
```
## Advanced Usage
### Using Dash Components
Dash offers a variety of components in `dash_core_components` and `dash_html_components` modules, including sliders, dropdowns, graphs, and more, to build the layout of your application.
### Deploying Dash Apps
Dash apps can be deployed on servers with WSGI HTTP servers. Heroku, AWS Elastic Beanstalk, and Google Cloud can host Dash apps. The Dash Deployment Server is an enterprise solution that provides an easy way to deploy, scale, and manage Dash apps.
### Styling Dash Apps
You can style your Dash app using CSS. Place a `.css` file in an `assets` folder in your Dash app directory, and Dash will automatically apply this CSS to your app.
### Advanced Callbacks
Dash supports more complex scenarios with its callback system, such as multiple inputs/outputs, chained callbacks, and callback context for determining which input triggered the callback.
## Example: Interactive Dash App with Callback
```python
# Add a dropdown and an output div
app.layout = html.Div([
dcc.Dropdown(
id='my-dropdown',
options=[
{'label': 'Option 1', 'value': 'OPT1'},
{'label': 'Option 2', 'value': 'OPT2'},
{'label': 'Option 3', 'value': 'OPT3'}
],
value='OPT1'
),
html.Div(id='output-container')
])
@app.callback(
Output('output-container', 'children'),
[Input('my-dropdown', 'value')]
)
def update_output(value):
return 'You have selected "{}"'.format(value)
```
`Dash` is a powerful tool for building interactive web applications in Python without requiring significant web development skills. This guide covers the basics of creating a Dash app, making it interactive with callbacks, and touches on more advanced features and deployment options. Dash's flexibility and ease of use make it a popular choice for data scientists and analysts looking to share their insights through interactive visual dashboards and applications.

117
tech_docs/python/Dask.md Normal file
View File

@@ -0,0 +1,117 @@
For fast data analysis and manipulation, especially for tabular data, `Dask` emerges as a powerful Python library. Dask provides parallel computing capabilities, designed to scale from laptops to clusters, making it particularly useful for working with large datasets that don't fit into memory. Dask parallelizes both NumPy and pandas operations, offering a familiar API for those already comfortable with these tools, but with the added advantage of parallel execution. Here's a concise reference guide for common use cases with `Dask`, formatted in Markdown syntax:
# `Dask` Reference Guide
## Installation
```
pip install dask
```
For complete functionality, including distributed computing features, install with:
```
pip install "dask[complete]"
```
## Basic Concepts
### Importing Dask
```python
import dask.array as da
import dask.dataframe as dd
```
## Dask Arrays
### Creating Dask Arrays
```python
# Create a Dask array from a NumPy array
import numpy as np
x_np = np.arange(1000)
x_da = da.from_array(x_np, chunks=(100))
# Create a Dask array directly
x_da = da.arange(1000, chunks=(100))
```
### Operations on Dask Arrays
```python
# Operations are lazy; they're not computed until explicitly requested
y_da = x_da + x_da
# Compute the result
y_np = y_da.compute()
```
## Dask DataFrames
### Creating Dask DataFrames
```python
# Create a Dask DataFrame from a pandas DataFrame
import pandas as pd
df_pd = pd.DataFrame({'x': [1, 2, 3, 4], 'y': [5, 6, 7, 8]})
df_dd = dd.from_pandas(df_pd, npartitions=2)
# Read a CSV file into a Dask DataFrame
df_dd = dd.read_csv('large-dataset.csv')
```
### Operations on Dask DataFrames
```python
# Operations are performed in parallel and are lazy
result_dd = df_dd[df_dd.y > 5]
# Compute the result to get a pandas DataFrame
result_pd = result_dd.compute()
```
## Parallel Computing with Dask
### Simple Parallelization
```python
from dask import delayed
# Use the delayed decorator to make functions lazy
@delayed
def increment(x):
return x + 1
@delayed
def add(x, y):
return x + y
# Define a small computation graph
x = increment(1)
y = increment(2)
total = add(x, y)
# Compute the result
result = total.compute()
```
### Distributed Computing
```python
from dask.distributed import Client
# Start a local Dask client (automatically uses available cores)
client = Client()
# The Client can now coordinate parallel computations on your machine or on a cluster
```
## Working with Large Datasets
### Handling Out-of-Core Data
Dask's ability to work with datasets larger than memory, by breaking them into manageable chunks and only loading chunks into memory as needed, is one of its key features.
### Example: Aggregations
```python
# Perform a group-by operation on a large dataset
result_dd = df_dd.groupby('category').x.mean()
# Compute the result
result_pd = result_dd.compute()
```
`Dask` is particularly well-suited for data-intensive computations, offering intuitive parallel computing solutions that integrate seamlessly with existing Python data tools. Its lazy evaluation model allows for efficient computation on large datasets, while its distributed computing capabilities enable scaling to clusters for even greater performance. This guide covers basic usage and common patterns in Dask, making it an essential tool for those dealing with large-scale data processing challenges.
Dask's scalable and flexible nature makes it an excellent choice for a wide range of data processing tasks, especially when dealing with large datasets that exceed memory capacities or when leveraging multi-core processors or clusters for parallel computations.

View File

@@ -0,0 +1,94 @@
When focusing on combining the capabilities of Python and Linux for a specific, fine-tuned purpose, `Fabric` emerges as a highly valuable library. Fabric extends Pythons ability to execute local or remote shell commands, thus automating the use of SSH for application deployment or system administration tasks. It's particularly well-suited for streamlining the deployment process, executing server administration tasks, and carrying out remote commands across a fleet of servers. Heres a concise reference guide for common use cases with `Fabric`:
# `Fabric` Reference Guide
## Installation
```
pip install fabric
```
## Basic Usage
### Running Commands Locally
Fabric allows you to run shell commands on your local machine using the `local` function.
```python
from fabric import task
@task
def local_ls(c):
c.local('ls -lah')
```
This function lists the contents of the current directory in a long listing format.
### Executing Commands Remotely
To run commands on remote systems, you use the `run` method. Fabric handles the SSH connection details.
```python
@task
def remote_uname(c):
c.run('uname -a')
```
This command displays system information about the remote machine.
### Connecting to Remote Hosts
You can specify connection parameters such as the hostname, user, and more directly in your tasks.
```python
@task
def deploy(c):
with c.connection(host="example.com", user="username") as conn:
conn.run('git pull')
conn.run('touch tmp/restart.txt')
```
This task demonstrates a simple deployment process on a remote server.
## Advanced Usage
### Serial and Parallel Execution
Fabric can execute tasks in serial (one after the other) or in parallel (simultaneously on multiple hosts), which is particularly useful for deploying changes across a server cluster.
```python
from fabric import SerialGroup, ParallelGroup
@task
def deploy(c):
for conn in SerialGroup('host1', 'host2', 'host3'):
conn.run('uname -a')
@task
def parallel_deploy(c):
for conn in ParallelGroup('host1', 'host2', 'host3'):
conn.run('uname -a')
```
### File Transfers
Fabric provides simple mechanisms for uploading and downloading files between local and remote locations.
```python
@task
def upload_project(c):
c.put('my_project.zip', remote='/opt/projects/')
@task
def download_data(c):
c.get(remote='/var/log/myapp.log', local='logs/')
```
### Context Managers for Changing Directories and Settings
Fabrics context managers can temporarily change the working directory or modify environment settings during command execution.
```python
from fabric import Connection
@task
def deploy(c):
with Connection('example.com') as c:
with c.cd('/path/to/application'):
c.run('git pull')
c.run('./restart.sh')
```
## Security Considerations
- Use SSH keys for authentication to enhance security.
- Manage sensitive information, such as host addresses and passwords, securely and outside your script when possible.
`Fabric` is optimized for simplicity and flexibility, allowing developers and system administrators to automate complex deployment and system administration tasks efficiently. By writing tasks in Python, Fabric users can take advantage of Pythons readability and extensive library ecosystem to create powerful, maintainable scripts for automating their workflows.
Fabric's combination of straightforward syntax and powerful features like serial and parallel execution, file transfers, and the ability to execute both local and remote commands makes it a standout tool for automating server setup, application deployment, and task execution in Linux environments using Python.

108
tech_docs/python/FastAPI.md Normal file
View File

@@ -0,0 +1,108 @@
`FastAPI` is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. The key features of FastAPI include:
- **Fast**: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
- **Fast to code**: Great editor support. Completion everywhere. Less time debugging.
- **Easy**: Designed to be easy to use and learn. Less time reading docs.
- **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
- **Robust**: Get production-ready code. With automatic interactive documentation.
- **Standards-based**: Based on (and fully compatible with) the open standards for APIs: OpenAPI and JSON Schema.
Here's a concise reference guide for common use cases with `FastAPI`:
# `FastAPI` Reference Guide
## Installation
```
pip install fastapi[all]
```
This command installs FastAPI along with all its optional dependencies and features, including `uvicorn`, which is an ASGI server for running the application.
## Basic Usage
### Creating a Simple FastAPI Application
```python
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
```
### Running the Application
To run the application, use `uvicorn` from the command line:
```
uvicorn main:app --reload
```
Assuming your file is named `main.py` and your FastAPI app object is named `app`. The `--reload` flag enables auto-reload so the server will restart after code changes.
## Path Parameters
```python
@app.get("/items/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
```
Type hints are used to validate and convert path parameters.
## Query Parameters
```python
@app.get("/items/")
async def read_items(skip: int = 0, limit: int = 10):
return fake_items_db[skip : skip + limit]
```
## Request Body
```python
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
@app.post("/items/")
async def create_item(item: Item):
return item
```
Pydantic models define the structure and validation of request bodies.
## Interactive API Documentation
FastAPI generates interactive API documentation (using Swagger UI and ReDoc) for your API, accessible from `/docs` and `/redoc`, once you start your application.
## Dependency Injection
```python
from fastapi import Depends, HTTPException
def fake_password_hasher(raw_password: str):
return "supersecret" + raw_password
def verify_password(username: str, password: str):
if username in fake_users_db and fake_password_hasher(password) == fake_users_db[username]["hashed_password"]:
return True
return False
async def get_current_username(username: str = Depends(verify_password)):
return username
```
FastAPI's dependency injection system allows you to have reusable dependencies that can have their own dependencies.
## Background Tasks
```python
from fastapi import BackgroundTasks
def write_log(message: str):
with open("log.txt", mode="a") as log:
log.write(message)
@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
background_tasks.add_task(write_log, f"notification for {email}")
return {"message": "Notification sent in the background"}
```
`FastAPI` is designed for building APIs that are easy to build, test, and use, with a focus on speed and type safety. This guide introduces the basics, but FastAPI's capabilities extend to more advanced features like security and authentication, more complex data modeling with Pydantic, WebSockets, GraphQL, and more, catering to modern web application development needs.
FastAPI's design and performance make it an attractive option for high-speed API services, providing a robust foundation for building reliable, efficient web services and applications.

View File

@@ -0,0 +1,52 @@
When it comes to integrating Python with Linux for effective system administration and automation, `FirewallD` stands out through its Python bindings. While not a library in the strictest sense, FirewallD provides a dynamic firewall management tool with support for network/firewall zones to define the trust level of network connections or interfaces. It is the default firewall management tool on many Linux distributions, including Fedora, CentOS, and RHEL.
The Python bindings for FirewallD allow administrators and developers to interact with the firewall's settings programmatically, offering a powerful method to automate firewall configurations directly from Python scripts. This capability is especially useful for deploying applications that require specific network rules or for managing large-scale server environments where firewall settings need to be adjusted frequently.
### Basic Concepts and Operations
#### Installation
Ensure FirewallD is installed on your Linux system. The Python bindings should be available once FirewallD is installed. For custom scripts or applications, you might need to install additional Python packages or development tools specific to your Linux distribution.
#### Managing Firewall Rules with Python
Using Python to interact with FirewallD involves importing the necessary modules and using the provided API to query or modify firewall settings. Here's a simplified overview of how you might use Python to interact with FirewallD:
```python
import firewall.core.io.firewalld as fwd
import firewall.config as fw_config
# Initialize the FirewallD client
fw = fwd.FirewallClient()
fw.start()
# Get the default zone
default_zone = fw.getDefaultZone()
# List all rules in the default zone
rules = fw.getRules(default_zone)
for rule in rules:
print(rule)
# Adding a new rule
new_rule = ...
fw.addRule(default_zone, new_rule)
# Removing a rule
fw.removeRule(default_zone, rule_to_remove)
fw.stop()
```
This example is conceptual and aims to illustrate the approach rather than provide a ready-to-run script. The actual implementation will vary based on your specific requirements and the version of FirewallD and its Python bindings.
### Automating Firewall Configurations
The real power of using Python with FirewallD lies in automation. For instance, you could develop Python scripts or applications that:
- Automatically configure firewall rules based on the deployment environment.
- Dynamically adjust firewall settings in response to detected security threats or system events.
- Integrate with deployment pipelines to ensure that necessary firewall changes are applied as part of application rollouts.
### Security Considerations
Modifying firewall settings programmatically requires careful consideration to avoid inadvertently compromising your system's security. Ensure that scripts are tested in a controlled environment before deployment and consider implementing safeguards to prevent the application of incorrect firewall rules.
While FirewallD and its Python bindings offer a potent tool for Linux system administrators and Python developers, it's important to approach automation with caution, especially when dealing with security-critical components like firewalls.
The integration of Python with Linux system administration tools like FirewallD exemplifies the versatility of Python for system automation and management, bridging high-level programming capabilities with the robust system control mechanisms provided by Linux.

94
tech_docs/python/Flask.md Normal file
View File

@@ -0,0 +1,94 @@
`Flask` is an essential Python library for web development, offering a lightweight and modular micro web framework for building web applications. It provides the tools, libraries, and technologies to allow developers to build a web application. Flask supports extensions that can add application features as if they were implemented in Flask itself. Here's a concise reference guide for common use cases with `Flask`:
```markdown
# `Flask` Reference Guide
## Installation
```
pip install Flask
```
## Basic Usage
### Creating a Simple Flask Application
```python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
```
This code snippet creates a basic web application that responds with "Hello, World!" when you access the root URL.
### Running the Application
By default, the Flask application will run on `localhost` port `5000`. To run the application, execute the Python script directly, and navigate to `http://127.0.0.1:5000/` in a web browser.
## Routing
Flask allows you to map URLs to Python functions, enabling dynamic content generation based on URL parameters.
```python
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % escape(username)
```
## Templates
Flask integrates with Jinja2 templating engine, allowing for dynamic content generation in HTML.
```python
from flask import render_template
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
```
This will render an HTML template located in the `templates` folder.
## Static Files
Static files such as JavaScript, CSS, and images can be served from the `static` directory.
```html
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
```
## Request Data
Access data sent by the client using Flask's `request` object.
```python
from flask import request
@app.route('/login', methods=['POST'])
def login():
username = request.form['username']
password = request.form['password']
# Logic for login
```
## Redirects and Errors
Flask supports redirects and custom error handling.
```python
from flask import abort, redirect, url_for
@app.route('/redirect')
def redirect_example():
return redirect(url_for('hello_world'))
@app.errorhandler(404)
def page_not_found(error):
return render_template('page_not_found.html'), 404
```
## Flask Extensions
Flask can be extended with various extensions available in the Flask ecosystem for ORM, form validation, authentication, and more.
- Flask-SQLAlchemy for ORM.
- Flask-WTF for forms.
- Flask-Login for authentication.
## Deploying Flask Applications
Flask applications can be deployed in various ways, including traditional web servers like Apache or Nginx using WSGI, as well as containerized environments like Docker, or platforms like Heroku, AWS Elastic Beanstalk, and Google App Engine.
`Flask` is designed to be easy to use and extend, making it suitable for building everything from simple web services to complex web applications. With a strong focus on documentation and community, Flask has become a popular choice among Python developers for web development tasks.
Flask's simplicity and flexibility, allowing for the quick development of web applications with minimal setup, have made it particularly popular in the Python community for both beginners and experienced developers.

View File

@@ -0,0 +1,90 @@
GStreamer is a powerful multimedia framework that enables the creation of a wide range of multimedia applications, such as video editors, streaming media broadcasters, and media players. It provides a comprehensive toolkit for audio and video processing, including capture, encoding, rendering, and streaming. GStreamer's architecture is based on plugins that provide various codec implementations, filters, and other multimedia functionalities.
While GStreamer itself is a complex, C-based framework that operates outside the Python standard library, it offers Python bindings through `PyGObject` (previously `gst-python` for older versions), allowing Python developers to harness its capabilities within Python applications.
### GStreamer Basic Usage
GStreamer's usage revolves around constructing a pipeline that defines the flow of multimedia data from source to destination, applying various transformations along the way.
#### Installation
GStreamer can be installed on most operating systems. On Linux, use your distribution's package manager:
```sh
sudo apt-get install gstreamer1.0-tools gstreamer1.0-plugins-base gstreamer1.0-plugins-good
```
To use GStreamer in Python, you also need to install PyGObject:
```sh
pip install PyGObject
```
You might need additional development packages for `PyGObject` depending on your system.
#### Constructing a Pipeline
A simple pipeline might involve playing a video file. In GStreamer, you create elements (source, sink, filters) and link them together to form a pipeline.
```python
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GLib
# Initialize GStreamer
Gst.init(None)
# Create the elements
source = Gst.ElementFactory.make("filesrc", "source")
decoder = Gst.ElementFactory.make("decodebin", "decoder")
sink = Gst.ElementFactory.make("autovideosink", "sink")
# Create the empty pipeline
pipeline = Gst.Pipeline.new("test-pipeline")
if not pipeline or not source or not decoder or not sink:
print("Not all elements could be created.")
exit(-1)
# Build the pipeline
pipeline.add(source)
pipeline.add(decoder)
pipeline.add(sink)
source.link(decoder)
decoder.link(sink)
# Set the source's location to a file path
source.set_property("location", "/path/to/your/video/file.mp4")
# Start playing
pipeline.set_state(Gst.State.PLAYING)
# Wait until error or EOS (End of Stream)
bus = pipeline.get_bus()
msg = bus.timed_pop_filtered(Gst.CLOCK_TIME_NONE, Gst.MessageType.ERROR | Gst.MessageType.EOS)
# Free resources
pipeline.set_state(Gst.State.NULL)
```
### Common Features and Use Cases
- **Audio/Video Playback and Streaming**: GStreamer can play back media files and stream audio and video over the network. Its versatility supports various codecs and formats.
- **Media Conversion and Processing**: With GStreamer, you can convert media between different formats, apply filters and effects, or extract media metadata.
- **Capturing Media**: GStreamer is capable of capturing audio and video from different sources, such as webcams or microphones, making it suitable for building applications that require media input.
### Python Integration
Integrating GStreamer with Python through PyGObject opens up multimedia processing capabilities to Python developers, allowing the construction of complex media-related applications. GStreamer's pipeline-based architecture aligns well with Python's readability, making multimedia operations both powerful and accessible.
### Tips for Effective GStreamer Use
- **Understanding the Pipeline**: Grasping the concept of pipelines and elements is crucial. Pipelines are the backbone of GStreamer applications, dictating how data flows and is processed.
- **Exploring Plugins**: GStreamer's functionality is extended through plugins. Familiarize yourself with the available plugins for different tasks, as they greatly expand what you can achieve.
- **Error Handling**: Robust error handling is essential. Multimedia processing can be prone to errors due to format incompatibilities or resource issues, so always check the return values and messages from the GStreamer bus.
GStreamer, through its Python bindings, allows for sophisticated multimedia handling directly from Python, bridging complex multimedia processing capabilities with Python's simplicity and versatility. Whether you're developing a custom video player, building a streaming server, or creating multimedia processing applications, GStreamer provides the tools necessary to implement these tasks efficiently and effectively.

View File

@@ -0,0 +1,108 @@
# Installing JupyterLab on a Linux Server
This guide outlines the steps to install JupyterLab on a Linux server, enabling powerful data analysis and machine learning capabilities with remote access.
## Prerequisites
- A Linux server (Debian/Ubuntu or RHEL-based)
- SSH access to the server
- Basic command-line proficiency
## Step 1: Connect to Your Server
Start by establishing an SSH connection to your server.
```bash
ssh username@your_server_ip
```
Replace `username` with your actual server username and `your_server_ip` with the server's IP address.
## Step 2: Update Your Server
Ensure your server's package lists and installed packages are updated.
### For Debian/Ubuntu:
```bash
sudo apt-get update && sudo apt-get upgrade
```
### For RHEL-based systems:
```bash
sudo yum update
```
## Step 3: Install Python and Virtual Environment
JupyterLab requires Python. Install Python 3 and the package to manage virtual environments.
### For Debian/Ubuntu:
```bash
sudo apt-get install python3-venv python3-pip
```
### For RHEL-based systems:
Ensure you have access to the EPEL repository, then:
```bash
sudo yum install python3-pip python3-virtualenv
```
## Step 4: Create and Activate a Virtual Environment
Creating a virtual environment isolates your JupyterLab setup.
```bash
python3 -m venv jupyterlab_env
source jupyterlab_env/bin/activate
```
## Step 5: Install JupyterLab
With the virtual environment activated, install JupyterLab using pip.
```bash
pip install jupyterlab
```
## Step 6: Start JupyterLab
Run JupyterLab, configuring it to allow remote access and to prevent it from trying to open a browser automatically.
```bash
jupyter lab --ip=0.0.0.0 --no-browser
```
**Note**: `--ip=0.0.0.0` makes JupyterLab accessible on all network interfaces of your server. For security, consider setting up a more restrictive IP or using additional security measures like SSH tunneling or a VPN.
## Step 7: Access JupyterLab
Upon starting JupyterLab, the terminal will display a URL beginning with `http://127.0.0.1:8888`. Replace `127.0.0.1` with your server's IP address or hostname to access JupyterLab from your browser.
## Step 8: Secure Your JupyterLab Instance
It's crucial to secure your JupyterLab instance, especially if accessible over the public internet.
### Set a Password for JupyterLab
Run this command and follow the prompts to create a password:
```bash
jupyter notebook password
```
### Consider Additional Security Measures
- Use SSH tunneling for a secure connection.
- Configure a reverse proxy with SSL encryption.
- Employ firewall rules to restrict access.
## Conclusion
You've now set up JupyterLab on your Linux server, ready for data analysis and machine learning projects with the power of server-grade hardware.
---

101
tech_docs/python/Librosa.md Normal file
View File

@@ -0,0 +1,101 @@
`Librosa` is a Python library for audio and music analysis. It provides the building blocks necessary to create music information retrieval systems at a high level of abstraction. Designed for researchers and developers alike, Librosa makes it easy to analyze audio signals and extract information from them, such as pitch, loudness, and timbre. It's particularly well-suited for applications in music genre classification, audio feature extraction for machine learning, beat tracking, and much more.
### Librosa Complete Guide
#### Installation
Librosa requires NumPy, SciPy, and matplotlib, among others. It's recommended to use a scientific Python distribution or a virtual environment to manage these dependencies. Install Librosa using pip:
```sh
pip install librosa
```
### Basic Operations
#### Loading Audio Files
Librosa simplifies the process of loading audio files into Python for analysis.
```python
import librosa
# Load an audio file as a floating point time series.
audio_path = 'path/to/your/audio/file.mp3'
y, sr = librosa.load(audio_path)
```
- `y` is the audio time series.
- `sr` is the sampling rate of `y`.
#### Displaying Waveforms
Visualizing audio is crucial for understanding its properties.
```python
import librosa.display
import matplotlib.pyplot as plt
plt.figure(figsize=(14, 5))
librosa.display.waveplot(y, sr=sr)
plt.title('Waveform')
plt.show()
```
### Feature Extraction
#### Spectrogram
A spectrogram is a visual representation of the spectrum of frequencies in a sound or other signal as they vary with time.
```python
import numpy as np
D = np.abs(librosa.stft(y)) # Short-time Fourier transform
librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), sr=sr, x_axis='time', y_axis='log')
plt.title('Power spectrogram')
plt.colorbar(format='%+2.0f dB')
plt.tight_layout()
plt.show()
```
#### Mel-Frequency Cepstral Coefficients (MFCCs)
MFCCs are commonly used features for speech and audio processing.
```python
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
librosa.display.specshow(mfccs, sr=sr, x_axis='time')
plt.title('MFCC')
plt.colorbar()
plt.tight_layout()
plt.show()
```
#### Beat Tracking
Librosa can detect beats in a musical track, useful for rhythm analysis and music production software.
```python
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
print(f'Tempo: {tempo}')
print(f'Beat frames: {beats}')
```
### Advanced Analysis
#### Harmonic-Percussive Source Separation
Separate an audio signal into its harmonic and percussive components.
```python
y_harmonic, y_percussive = librosa.effects.hpss(y)
```
#### Tempo and Beat Features
Extract tempo and beat-aligned features from the audio.
```python
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
beat_times = librosa.frames_to_time(beat_frames, sr=sr)
```
### Potential Use Cases
- **Music Genre Classification**: Analyzing audio features to classify music into genres.
- **Speech Recognition**: Extracting features from speech for use in natural language processing models.
- **Sound Event Detection**: Identifying specific sounds within audio files, useful for surveillance or wildlife monitoring.
- **Emotion Recognition**: Analyzing vocal patterns to determine the speaker's emotional state.
- **Audio Tagging**: Automatically tagging music or sounds with descriptive labels based on their content.
`Librosa` stands out for its comprehensive set of functions designed for audio signal processing, making it a go-to library for music and audio analysis tasks. Its capability to extract a wide array of audio features with ease positions it as a powerful tool for researchers and developers in fields ranging from machine learning and AI to music production and sound design.

View File

@@ -0,0 +1,152 @@
For creating and manipulating complex data visualizations, `Matplotlib` is an indispensable Python library. It's widely used for generating plots, histograms, bar charts, scatterplots, and more, offering extensive customization options to make the visualizations as informative and appealing as possible. Below is a concise reference guide for common use cases with `Matplotlib`, formatted in Markdown syntax:
# `Matplotlib` Reference Guide
## Installation
```
pip install matplotlib
```
## Basic Plotting
### Importing Matplotlib
```python
import matplotlib.pyplot as plt
```
### Creating a Simple Plot
```python
# Prepare some data
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
# Plot data
plt.plot(x, y)
# Show plot
plt.show()
```
### Creating a Scatter Plot
```python
# Scatter plot
plt.scatter(x, y)
# Show plot
plt.show()
```
### Multiple Plots on Same Axes
```python
# Second set of data
x2 = [1, 2, 3, 4]
y2 = [30, 25, 20, 15]
# Plot data
plt.plot(x, y, label='First Line')
plt.plot(x2, y2, label='Second Line')
# Adding a legend
plt.legend()
# Show plot
plt.show()
```
## Customizing Plots
### Titles, Labels, and Legends
```python
plt.plot(x, y)
# Title
plt.title('My First Plot')
# Axis labels
plt.xlabel('X Axis Label')
plt.ylabel('Y Axis Label')
# Show plot
plt.show()
```
### Line Styles and Markers
```python
plt.plot(x, y, color='red', linestyle='--', marker='o', label='Data Points')
# Show plot
plt.show()
```
### Setting Axis Ranges
```python
plt.plot(x, y)
# Setting the range for the axes
plt.xlim(0, 5)
plt.ylim(5, 35)
# Show plot
plt.show()
```
### Adding Grid Lines
```python
plt.plot(x, y)
# Adding grid
plt.grid(True)
# Show plot
plt.show()
```
## Other Types of Plots
### Histograms
```python
data = [1, 2, 2, 3, 3, 3, 4, 4, 5]
# Create histogram
plt.hist(data, bins=5, alpha=0.5, color='blue')
# Show plot
plt.show()
```
### Bar Charts
```python
categories = ['A', 'B', 'C', 'D']
values = [10, 20, 15, 5]
# Create bar chart
plt.bar(categories, values)
# Show plot
plt.show()
```
### Pie Charts
```python
slices = [7, 2, 2, 13]
categories = ['A', 'B', 'C', 'D']
# Create pie chart
plt.pie(slices, labels=categories, autopct='%1.1f%%')
# Show plot
plt.show()
```
## Saving Figures
```python
plt.plot(x, y)
# Save the figure
plt.savefig('plot.png')
# Show plot
plt.show()
```
`Matplotlib` is a powerful library for creating static, interactive, and animated visualizations in Python. This guide covers the basics of generating and customizing plots, but Matplotlib's functionality is vast, supporting a wide range of plot types and customization options to suit various data visualization needs.

106
tech_docs/python/MoviePy.md Normal file
View File

@@ -0,0 +1,106 @@
`MoviePy` is a versatile library for video editing that offers a broad range of features for processing and manipulating video files. It's designed to be accessible for beginners but powerful enough for advanced users, making it perfect for a wide array of video processing tasks, from editing and cutting to adding text, animations, and effects.
### MoviePy Complete Guide
#### Installation
To get started with MoviePy, you'll need to install it using pip. It might also require installing FFmpeg, which MoviePy uses for video processing under the hood.
```sh
pip install moviepy
```
MoviePy will attempt to download FFmpeg on its first run if it's not already installed.
### Basic Operations
#### Reading and Writing Video
```python
from moviepy.editor import VideoFileClip
# Load a video
clip = VideoFileClip("path/to/video.mp4")
# Write the video to a new file (can also change the format)
clip.write_videofile("path/to/new_video.mp4")
```
### Editing Video
#### Cutting and Trimming Video
```python
# Cut out the first 10 seconds of the video
cut_clip = clip.subclip(10, 20)
```
#### Concatenating Videos
```python
from moviepy.editor import concatenate_videoclips
clip1 = VideoFileClip("path/to/video1.mp4")
clip2 = VideoFileClip("path/to/video2.mp4")
# Combine the clips into one
final_clip = concatenate_videoclips([clip1, clip2])
final_clip.write_videofile("path/to/combined_video.mp4")
```
#### Adding Text
```python
from moviepy.editor import TextClip, CompositeVideoClip
# Create a text clip. You can specify the font, color, etc.
txt_clip = TextClip("My awesome video", fontsize=70, color='white')
# Set the position of the text in the center and the duration to be 10 seconds
txt_clip = txt_clip.set_pos('center').set_duration(10)
# Overlay the text clip on the first video clip
video = CompositeVideoClip([clip, txt_clip])
video.write_videofile("path/to/video_with_text.mp4")
```
### Working with Audio
#### Extracting Audio
```python
audio = clip.audio
audio.write_audiofile("path/to/extracted_audio.mp3")
```
#### Adding or Changing the Audio Track
```python
from moviepy.editor import AudioFileClip
new_audio = AudioFileClip("path/to/new_audio.mp3")
video_with_new_audio = clip.set_audio(new_audio)
video_with_new_audio.write_videofile("path/to/video_with_new_audio.mp4")
```
### Advanced Features
#### Adding Effects
MoviePy supports a variety of effects, such as fading in/out, mirroring, and even custom effects through lambda functions.
```python
# Fade in the audio at the start of the video
fadein_audio_clip = clip.audio.fadein(3.0) # Fade in over 3 seconds
clip = clip.set_audio(fadein_audio_clip)
```
#### Working with Frames
You can manipulate the video at the frame level for custom effects or processing.
```python
# Invert the colors of the video
def invert_colors(get_frame, t):
return 1 - get_frame(t) # Inverts the frame's color
inverted_clip = clip.fl_image(invert_colors)
```
### Potential Use Cases
- **Video Editing and Production**: Creating video content for social media, YouTube, or personal projects.
- **Automated Video Processing**: Automating tasks like resizing videos, converting formats, or extracting highlights.
- **Educational Content**: Generating educational videos with automated text overlays, annotations, or combining lecture recordings with slides.
- **Data Visualization**: Creating video visualizations from data, such as time-lapse videos of data changes over time.
`MoviePy` simplifies complex video editing tasks into straightforward Python code, making it an invaluable tool for developers, content creators, and educators looking to manipulate video content programmatically. Its ability to handle a wide range of video editing tasks with ease opens up numerous possibilities for creative and automated video production.

100
tech_docs/python/NLTK.md Normal file
View File

@@ -0,0 +1,100 @@
For handling natural language processing (NLP) tasks in Python, `NLTK` (Natural Language Toolkit) is a highly useful library. It provides easy-to-use interfaces to over 50 corpora and lexical resources such as WordNet, along with a suite of text processing libraries for classification, tokenization, stemming, tagging, parsing, and semantic reasoning. Heres a concise reference guide for common use cases with `NLTK`, formatted in Markdown syntax:
# `NLTK` Reference Guide
## Installation
```
pip install nltk
```
After installing, you may need to download specific data packages used in your project:
```python
import nltk
nltk.download('popular') # Downloads popular packages
```
## Basic NLP Tasks
### Tokenization
```python
from nltk.tokenize import word_tokenize, sent_tokenize
text = "Hello there, how are you? Weather is great, and Python is awesome."
words = word_tokenize(text)
sentences = sent_tokenize(text)
```
### Removing Stopwords
```python
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
filtered_words = [word for word in words if not word in stop_words]
```
### Stemming
```python
from nltk.stem import PorterStemmer
ps = PorterStemmer()
stemmed_words = [ps.stem(word) for word in filtered_words]
```
### Part-of-Speech Tagging
```python
from nltk import pos_tag
tagged_words = pos_tag(words)
```
### Named Entity Recognition (NER)
```python
from nltk import ne_chunk
ner_tree = ne_chunk(tagged_words)
```
### Working with WordNet
```python
from nltk.corpus import wordnet
# Find synonyms
synonyms = wordnet.synsets("program")
# Example of usage
word = "ship"
synsets = wordnet.synsets(word)
for syn in synsets:
print("Lemma: ", syn.lemmas()[0].name())
print("Definition: ", syn.definition())
```
## Advanced NLP Tasks
### Parsing Sentence Structure
```python
from nltk import CFG
grammar = CFG.fromstring("""
S -> NP VP
VP -> V NP
NP -> 'the' N
N -> 'cat'
V -> 'sat'
""")
```
### Frequency Distribution
```python
from nltk.probability import FreqDist
fdist = FreqDist(words)
most_common_words = fdist.most_common(2)
```
### Sentiment Analysis
NLTK can be used for sentiment analysis, but it's more about providing foundational tools. For complex sentiment analysis, integrating NLTK with machine learning libraries like `scikit-learn` is common.
## Saving and Loading Models
NLTK itself doesn't focus on machine learning models in the way libraries like `scikit-learn` or `tensorflow` do. However, it's often used to preprocess text data for machine learning tasks, after which models can be saved and loaded using those libraries' mechanisms.
`NLTK` is a comprehensive library for building Python programs to work with human language data, offering a wide array of functionalities from simple tokenization to complex parsing and semantic reasoning. This guide introduces the basics, but exploring NLTKs documentation and tutorials can provide deeper insights into handling various NLP tasks.

View File

@@ -0,0 +1,118 @@
For creating and working with graphs (networks) in Python, `NetworkX` stands out as a highly useful library. It provides tools for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. With `NetworkX`, you can load and store networks in standard and nonstandard data formats, generate many types of random and classic networks, analyze network structure, build network models, design new network algorithms, draw networks, and much more. Heres a concise reference guide for common use cases with `NetworkX`:
# `NetworkX` Reference Guide
## Installation
```
pip install networkx
```
## Basic Usage
### Importing NetworkX
```python
import networkx as nx
```
### Creating Graphs
```python
# Create an empty graph
G = nx.Graph()
# Create a directed graph
D = nx.DiGraph()
# Create a multi-graph (multiple edges between same two nodes)
M = nx.MultiGraph()
# Create a multi-directed graph
MD = nx.MultiDiGraph()
```
### Adding Nodes and Edges
```python
# Add a single node
G.add_node(1)
# Add multiple nodes
G.add_nodes_from([2, 3, 4, 5])
# Add an edge (also adds nodes if they don't exist)
G.add_edge(1, 2)
# Add multiple edges (also adds nodes if they don't exist)
G.add_edges_from([(1, 2), (1, 3), (3, 4)])
```
### Graph Properties
```python
# Number of nodes
num_nodes = G.number_of_nodes()
# Number of edges
num_edges = G.number_of_edges()
# Neighbors of a node
neighbors = list(G.neighbors(1))
# Degree of a node
degree = G.degree(1)
```
### Accessing Edges and Nodes
```python
# Access edge attributes
G.edges[1, 2]['attribute_name'] = 'value'
# Access node attributes
G.nodes[1]['attribute_name'] = 'value'
```
## Analyzing Graphs
### Pathfinding
```python
# Shortest path
path = nx.shortest_path(G, source=1, target=4)
# Shortest path length
path_length = nx.shortest_path_length(G, source=1, target=4)
```
### Centrality Measures
```python
# Degree centrality
degree_centrality = nx.degree_centrality(G)
# Betweenness centrality
betweenness = nx.betweenness_centrality(G)
# Closeness centrality
closeness = nx.closeness_centrality(G)
```
### Community Detection
```python
# Using the Clauset-Newman-Moore greedy modularity maximization
from networkx.algorithms.community import greedy_modularity_communities
communities = list(greedy_modularity_communities(G))
```
## Visualizing Graphs
```python
import matplotlib.pyplot as plt
# Simple graph drawing
nx.draw(G, with_labels=True)
plt.show()
# Drawing with layout
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, node_color='skyblue', edge_color='gray')
plt.show()
```
`NetworkX` is incredibly versatile for handling and analyzing complex networks. It supports numerous types of networks and graphs, including social networks, word co-occurrence networks, and many others. This guide covers foundational operations, but `NetworkX`'s capabilities are extensive, supporting advanced graph theory and network analysis methods that can be tailored to specific projects or research needs.
NetworkX's comprehensive set of features and algorithms, combined with its integration with Python's scientific computing ecosystem (like NumPy, SciPy, and Matplotlib), makes it an invaluable tool for researchers and practitioners working in data science, network analysis, and beyond.

160
tech_docs/python/NumPy.md Normal file
View File

@@ -0,0 +1,160 @@
A powerful and versatile Python library for scientific computing is `NumPy`. It provides support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. NumPy is foundational for many other Python data science and machine learning libraries, offering efficient array operations and numerical computations. Here's a concise reference guide for common use cases with `NumPy`, formatted in Markdown syntax:
# `NumPy` Reference Guide
## Installation
```
pip install numpy
```
## Basic Operations
### Importing NumPy
```python
import numpy as np
```
### Creating Arrays
```python
# Create a one-dimensional array
arr_1d = np.array([1, 2, 3])
# Create a two-dimensional array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
# Create an array of zeros
zeros = np.zeros((3, 4))
# Create an array of ones
ones = np.ones((2, 3))
# Create an array with a range of elements
range_array = np.arange(10)
# Create a linearly spaced array
linear_spaced = np.linspace(0, 1, 5)
```
### Array Attributes
```python
# Array shape
print(arr_2d.shape)
# Number of array dimensions
print(arr_2d.ndim)
# Data type of array elements
print(arr_2d.dtype)
# Size of the array (number of elements)
print(arr_2d.size)
```
### Indexing and Slicing
```python
# Get a specific element [r, c]
element = arr_2d[1, 2]
# Get a specific row
row = arr_2d[0, :]
# Get a specific column
col = arr_2d[:, 2]
# Slicing [start_index:end_index:step_size]
slice_arr = arr_2d[0, 0:2]
```
### Basic Array Operations
```python
# Element-wise addition
result = arr_1d + arr_1d
# Element-wise subtraction
result = arr_1d - arr_1d
# Scalar multiplication
result = arr_1d * 2
# Element-wise multiplication
result = arr_1d * arr_1d
# Matrix multiplication
result = np.dot(arr_2d, arr_2d.T)
# Division
result = arr_1d / arr_1d
```
### Mathematical Functions
```python
# Square root
sqrt_arr = np.sqrt(arr_1d)
# Exponential
exp_arr = np.exp(arr_1d)
# Logarithm
log_arr = np.log(arr_1d)
# Trigonometric functions
sin_arr = np.sin(arr_1d)
```
### Statistics
```python
# Minimum
min_val = np.min(arr_1d)
# Maximum
max_val = np.max(arr_1d)
# Sum
sum_val = np.sum(arr_1d)
# Mean
mean_val = np.mean(arr_1d)
# Median
median_val = np.median(arr_1d)
# Standard deviation
std_dev = np.std(arr_1d)
```
### Reshaping and Flattening
```python
# Reshape
reshaped_arr = arr_2d.reshape((3, 2))
# Flatten the array
flat_arr = arr_2d.flatten()
```
## Advanced Operations
### Stacking Arrays
```python
# Vertical stacking
v_stack = np.vstack([arr_1d, arr_1d])
# Horizontal stacking
h_stack = np.hstack([arr_1d, arr_1d])
```
### Splitting Arrays
```python
# Split the array in 3 equally shaped arrays
split_arr = np.split(arr_1d, 3)
```
### Boolean Masking and Advanced Indexing
```python
# Find elements greater than 2
result = arr_1d > 2
# Index with a boolean array
filtered_arr = arr_1d[result]
```
`NumPy` is foundational for numerical and scientific computation in Python, providing efficient operations for handling and processing large data sets. This guide introduces fundamental concepts and operations, but NumPy's capabilities extend much further, making it an essential tool for data analysis, machine learning, and beyond.

View File

@@ -0,0 +1,80 @@
In the intersection of Python and Linux, focusing on harnessing the capabilities of each for a fine-tuned purpose, `Paramiko` stands out as an extremely useful library. Paramiko is a Python implementation of the SSHv2 protocol, providing both client and server functionality. It allows for SSH programming in Python, enabling the execution of commands on remote machines, transferring files, and full SSH session management. Here's a concise reference guide for common use cases with `Paramiko`:
# `Paramiko` Reference Guide
## Installation
```
pip install paramiko
```
## Basic Usage
### Establishing an SSH Connection
```python
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Automatically add host key
ssh.connect('hostname', username='user', password='password') # Connect to the host
```
Replace `'hostname'`, `'user'`, and `'password'` with the actual hostname and credentials.
### Executing Commands Remotely
```python
stdin, stdout, stderr = ssh.exec_command('ls -l')
print(stdout.read().decode())
```
This code executes `ls -l` on the remote machine and prints the output.
### Transferring Files
#### Uploading Files
```python
sftp = ssh.open_sftp()
sftp.put('localfilepath', 'remotefilepath') # Upload file
sftp.close()
```
#### Downloading Files
```python
sftp = ssh.open_sftp()
sftp.get('remotefilepath', 'localfilepath') # Download file
sftp.close()
```
### Handling SSH Keys
```python
key = paramiko.RSAKey.generate(2048) # Generate a new RSA key
private_key = key.write_private_key_file('private_key') # Save the private key
public_key = key.get_base64() # Get the public key
```
SSH keys are more secure and recommended for authentication over passwords.
### Using SSH Keys for Authentication
```python
private_key_path = 'path/to/private/key'
mykey = paramiko.RSAKey(filename=private_key_path)
ssh.connect('hostname', username='user', pkey=mykey)
```
### Starting an SSH Server with Paramiko
Paramiko can also be used to create an SSH server in Python, though this is a more advanced use case and requires setting up server-side components and handling authentication and command execution manually.
## Advanced Usage
### Port Forwarding/SSH Tunneling
Paramiko supports local and remote port forwarding, enabling secure tunneling of network traffic.
### Direct TCP/IP Channel
You can open a direct TCP/IP channel to a remote host, which can be useful for protocols that need a direct connection (e.g., database connections).
### Interactive SSH Sessions
Paramiko allows for more complex interactions with an SSH session, such as those requiring user input, by directly managing stdin, stdout, and stderr streams.
## Security Considerations
- Always validate or manage host keys properly to avoid Man-In-The-Middle (MITM) attacks.
- Prefer using SSH keys over passwords for authentication.
- Keep your Paramiko library up to date to incorporate security patches.
`Paramiko` is an essential tool for automating administrative tasks, data collection, or managing cloud infrastructure across Linux servers from Python. It bridges the capabilities of Python with the secure communication needs of Linux environments, enabling developers and system administrators to automate and manage their systems more effectively.
Paramiko's comprehensive feature set for SSH communication makes it ideal for a wide range of system administration and automation tasks in mixed Python/Linux environments, offering a powerful and flexible way to manage remote systems securely.

110
tech_docs/python/Plotly.md Normal file
View File

@@ -0,0 +1,110 @@
For real-time data analysis and interactive visualization in Jupyter notebooks, `Plotly` is an indispensable Python library. Plotly's strength lies in its ability to produce publication-quality graphs with just a few lines of code, supporting over 40 unique chart types covering a wide range of statistical, financial, geographic, scientific, and 3-dimensional use-cases.
Here's a concise reference guide for common use cases with `Plotly`, particularly aimed at those new to the library or seeking a quick overview:
# `Plotly` Reference Guide
## Installation
```
pip install plotly
```
Note: For Jupyter notebook support, installing the `notebook` and `ipywidgets` packages might also be necessary.
## Basic Plotting
### Importing Plotly
```python
import plotly.graph_objects as go
```
### Creating a Simple Line Chart
```python
fig = go.Figure()
# Add a trace
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[10, 11, 12, 13], mode='lines+markers', name='Line Plot'))
# Set title
fig.update_layout(title='Simple Line Chart')
# Show figure
fig.show()
```
### Creating a Bar Chart
```python
data = {
'x': ['A', 'B', 'C'],
'y': [120, 150, 100]
}
fig = go.Figure(data=go.Bar(x=data['x'], y=data['y']))
fig.update_layout(title_text='Basic Bar Chart')
fig.show()
```
## Interactive Visualizations
### Adding Interactivity
Plotly's figures are interactive by default, with zooming, panning, and hover details. Further customization can enhance user interaction.
### Dropdown Menus
```python
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(args=["type", "scatter"], label="Scatter", method="restyle"),
dict(args=["type", "bar"], label="Bar", method="restyle")
]),
direction="down",
pad={"r": 10, "t": 10},
),
]
)
```
## Advanced Plot Types
### 3D Plots
```python
fig = go.Figure(data=[go.Mesh3d(x=(70, 71, 72), y=(70, 71, 72), z=(1, 2, 3), color='blue', opacity=0.50)])
fig.update_layout(title_text="3D Mesh example")
fig.show()
```
### Heatmaps
```python
fig = go.Figure(data=go.Heatmap(z=[[1, 20, 30], [20, 1, 60], [30, 60, 1]]))
fig.update_layout(title='Basic Heatmap')
fig.show()
```
## Styling and Layout
### Customizing Layouts
```python
fig.update_layout(
title='Plot Title',
xaxis_title='X Axis Title',
yaxis_title='Y Axis Title',
legend_title='Legend Title',
font=dict(
family="Courier New, monospace",
size=18,
color="RebeccaPurple"
)
)
```
## Exporting and Saving Plots
```python
fig.write_image("fig1.jpeg") # Requires the `kaleido` package
```
`Plotly` excels in making sophisticated and interactive visualizations accessible, with extensive documentation and community examples to help users get started and to solve complex visualization problems. Its seamless integration with Jupyter notebooks makes it a favorite for data analysis and presentation in scientific computing and business intelligence.
Plotly's ability to generate web-ready interactive visualizations also makes it ideal for sharing insights with a non-technical audience, ensuring that data-driven stories are engaging and understandable. This guide introduces the core functionality, but Plotly's comprehensive API supports much more, including detailed configuration options for fine-tuning the appearance and behavior of plots.
Plotly's blend of power and simplicity in creating interactive charts makes it a valuable tool for data scientists and analysts looking to explore and present their data dynamically.

View File

@@ -0,0 +1,129 @@
Focusing on Python libraries specific to audio processing opens a vast array of possibilities for handling audio data, from simple playback and recording to complex audio analysis and signal processing tasks. Beyond `Librosa`, which is tailored more towards music and audio analysis, other libraries like `PyAudio` for audio I/O, and `SoundFile` for reading and writing audio files, play crucial roles in the audio processing ecosystem. Heres an integrated guide covering these libraries, highlighting their capabilities and common use cases.
### PyAudio - Audio I/O Interface
PyAudio provides Python bindings for PortAudio, the cross-platform audio I/O library, enabling users to easily use audio input and output devices.
#### Installation
```sh
pip install pyaudio
```
Note: Installing PyAudio might require additional dependencies on your system. Refer to the PyAudio documentation for platform-specific instructions.
#### Basic Usage
##### Playing Audio
```python
import pyaudio
import wave
filename = 'path/to/your/audio/file.wav'
# Open the file
wf = wave.open(filename, 'rb')
# Create a PyAudio interface
p = pyaudio.PyAudio()
# Open a stream to play audio
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# Read data in chunks and play
data = wf.readframes(1024)
while data != b'':
stream.write(data)
data = wf.readframes(1024)
# Close the stream and PyAudio interface
stream.close()
p.terminate()
```
##### Recording Audio
```python
CHUNK = 1024
FORMAT = pyaudio.paInt16 # Depends on your needs
CHANNELS = 2
RATE = 44100 # Sampling rate
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("Recording...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("Finished recording.")
# Stop and close the stream
stream.stop_stream()
stream.close()
# Terminate the PyAudio session
p.terminate()
# Save the recorded data as a WAV file
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
```
### SoundFile - Reading and Writing Audio Files
`SoundFile` is a library for reading from and writing to audio files, such as WAV and FLAC, using data formats supported by libsndfile.
#### Installation
```sh
pip install SoundFile
```
#### Basic Usage
##### Reading an Audio File
```python
import soundfile as sf
data, samplerate = sf.read('path/to/your/audio/file.wav')
# `data` is a NumPy array containing the audio samples
# `samplerate` is the sampling rate of the audio file
```
##### Writing an Audio File
```python
sf.write('path/to/new/audio/file.wav', data, samplerate)
```
### Commonly Used Features Across Libraries
- **Audio Playback and Recording**: PyAudio provides low-level interfaces for audio playback and recording, ideal for applications requiring direct control over audio I/O.
- **Audio Analysis**: Libraries like Librosa are tailored for analyzing audio files, extracting features useful for music information retrieval, machine learning models, and more.
- **File Conversion and Processing**: SoundFile supports a wide range of audio formats, enabling easy conversion between them and manipulation of audio data.
### Potential Use Cases
- **Voice Recognition Systems**: Implementing systems that can recognize spoken commands or transcribe speech into text.
- **Music Genre Classification**: Developing algorithms to categorize music tracks into genres based on their audio features.
- **Sound Effect Generation**: Creating applications that generate sound effects or modify existing audio clips.
- **Audio Content Management**: Building tools for managing large audio datasets, including conversion, metadata tagging, and quality analysis.
### Integration and Workflow
A typical workflow might involve using PyAudio for capturing audio input from a microphone, processing or analyzing the audio with Librosa, and storing the processed audio using SoundFile. Each library addresses different aspects of audio handling in Python, providing a comprehensive toolkit for developers working on audio-related projects.
By leveraging these libraries in combination, developers can cover the entire spectrum of audio processing tasks, from real-time capture and playback to sophisticated analysis and feature extraction, facilitating the development of complex audio applications and systems.

View File

@@ -0,0 +1,76 @@
For Python developers and Linux system administrators who need to interact with the operating system at a low level, particularly for creating, accessing, and managing filesystems directly, `PyFilesystem2` (fs) emerges as an indispensable tool. PyFilesystem2 abstracts various filesystems (local, networked, and virtual) into a simple, unified API. This powerful abstraction allows for writing Python code that works across all supported filesystems, enhancing portability and reducing system-specific complexities.
### PyFilesystem2 Reference Guide
#### Installation
You can install PyFilesystem2 using pip:
```sh
pip install fs
```
#### Basic Usage
##### Opening and Reading Files
PyFilesystem2 simplifies the process of opening and reading files across different filesystems with a uniform API.
```python
from fs import open_fs
with open_fs('~/').open('example.txt', mode='r') as file:
content = file.read()
print(content)
```
##### Listing Directory Contents
Iterating over files and directories becomes straightforward, regardless of the underlying filesystem.
```python
home_fs = open_fs('~/')
for path in home_fs.listdir('/'):
print(path)
```
##### Copying Files and Directories
PyFilesystem2 provides high-level operations for common tasks, like copying files and directories, which work consistently across all filesystems.
```python
from fs.copy import copy_file
copy_file(home_fs, '/path/to/source.txt', home_fs, '/path/to/destination.txt')
```
#### Working with Different Filesystems
One of the key strengths of PyFilesystem2 is its support for a wide range of filesystems through a consistent interface.
- **Local Filesystem**: Access and manipulate files on the local disk.
- **Memory FS**: A filesystem that exists only in memory.
- **FTP and SFTP**: Interact with files over FTP/SFTP just as you would with local files.
- **Zip FS**: Work directly with ZIP archives as if they were directories.
##### Example: Working with SFTP
```python
from fs.sftpfs import SFTPFS
with SFTPFS('example.com', username='user', passwd='password') as sftp_fs:
if not sftp_fs.exists('backup'):
sftp_fs.makedirs('backup')
copy_file(home_fs, 'report.txt', sftp_fs, 'backup/report.txt')
```
#### Advanced Features
- **SubFS**: Access a sub-directory with the same API, enabling simpler navigation and manipulation within a part of the filesystem.
- **WrapFS**: Add additional behavior (like logging or caching) to any filesystem.
- **CompositeFS**: Combine multiple filesystems into a single, logical filesystem.
#### Use Cases
- **Cross-Platform File Operations**: Write code that manipulates files and directories in a way that is transparently compatible across different operating systems and environments.
- **Virtual Filesystems**: Create applications that can operate on virtual filesystems (like in-memory or archive-based filesystems) as easily as on physical storage.
- **Remote File Management**: Develop applications that need to manage files on remote servers (via SFTP, FTP, WebDAV) without changing the application logic.
#### Integration with Linux Systems
PyFilesystem2's ability to interact seamlessly with various filesystems makes it especially powerful on Linux, where systems often interact with a mix of local, network-mounted, and virtual filesystems. It allows developers to build applications that are decoupled from the underlying storage mechanism, facilitating easier deployment and migration across different environments.
#### Security Considerations
When working with filesystems, especially remote or user-supplied ones, it's crucial to handle paths securely, validate inputs, and be aware of filesystem boundaries to avoid security vulnerabilities like path traversal attacks.
PyFilesystem2 elevates filesystem interaction in Python to a new level of simplicity and abstraction, making it an essential library for applications that require flexible and portable file operations. Its comprehensive API and wide range of supported filesystems open up numerous possibilities for Python developers working within the diverse ecosystem of Linux filesystems.

131
tech_docs/python/PyPDF2.md Normal file
View File

@@ -0,0 +1,131 @@
For working with PDF files in Python, the `PyPDF2` library is a commonly used tool. It allows you to split, merge, rotate, and encrypt PDFs, among other functionalities. However, it's worth noting that `PyPDF2` primarily deals with manipulating existing PDFs rather than creating new ones from scratch. For more advanced PDF creation and manipulation, libraries such as `ReportLab` might be more suitable. Below is a concise reference guide for common use cases with `PyPDF2`, formatted in Markdown syntax:
# `PyPDF2` Reference Guide
## Installation
```
pip install PyPDF2
```
## Reading PDFs
```python
from PyPDF2 import PdfReader
# Open a PDF file
reader = PdfReader('path_to_file.pdf')
# Get the number of pages
num_pages = len(reader.pages)
# Read a specific page's text
page = reader.pages[0]
text = page.extract_text()
print(text)
```
## Merging PDFs
```python
from PyPDF2 import PdfReader, PdfWriter
# Create a PDF writer object
writer = PdfWriter()
# Open the first PDF
reader1 = PdfReader('path_to_first_file.pdf')
# Add all its pages to the writer
for page in reader1.pages:
writer.add_page(page)
# Open the second PDF
reader2 = PdfReader('path_to_second_file.pdf')
# Add all its pages to the writer
for page in reader2.pages:
writer.add_page(page)
# Write out the merged PDF
with open('merged_file.pdf', 'wb') as out:
writer.write(out)
```
## Splitting PDFs
```python
from PyPDF2 import PdfReader, PdfWriter
# Function to split a PDF into one PDF per page
def split_pdf(path):
reader = PdfReader(path)
for page_number in range(len(reader.pages)):
writer = PdfWriter()
writer.add_page(reader.pages[page_number])
output_filename = f'page_{page_number+1}.pdf'
with open(output_filename, 'wb') as output_pdf:
writer.write(output_pdf)
split_pdf('path_to_file.pdf')
```
## Rotating Pages
```python
from PyPDF2 import PdfReader, PdfWriter
# Open the PDF
reader = PdfReader('path_to_file.pdf')
writer = PdfWriter()
# Rotate the first page by 90 degrees
page = reader.pages[0].rotate(90) # Rotate clockwise
writer.add_page(page)
# Add the rest of the pages without rotation
for page in reader.pages[1:]:
writer.add_page(page)
# Save the rotated PDF
with open('rotated_file.pdf', 'wb') as out:
writer.write(out)
```
## Encrypting PDFs
```python
from PyPDF2 import PdfReader, PdfWriter
# Open the PDF
reader = PdfReader('path_to_file.pdf')
writer = PdfWriter()
# Add all pages to the writer
for page in reader.pages:
writer.add_page(page)
# Encrypt the PDF
writer.encrypt('password')
# Save the encrypted PDF
with open('encrypted_file.pdf', 'wb') as out:
writer.write(out)
```
## Extracting Information from PDFs
```python
from PyPDF2 import PdfReader
reader = PdfReader('path_to_file.pdf')
# Metadata
metadata = reader.metadata
print(metadata)
# Number of pages
print(len(reader.pages))
# Extract text from all pages
for page in reader.pages:
text = page.extract_text()
print(text)
```
This guide covers the basics of `PyPDF2` for PDF manipulation, including reading, merging, splitting, rotating, and encrypting PDF files. While `PyPDF2` is useful for these operations, it may have limitations with complex PDFs or specific PDF creation needs, for which other libraries like `ReportLab` might be more appropriate.
Keep in mind that `PyPDF2` focuses more on manipulating existing PDF files and might not support all features for complex PDF manipulation or creation from scratch. For more advanced PDF processing or creation, exploring other libraries or combining multiple libraries might be necessary to achieve desired outcomes.

117
tech_docs/python/PyTorch.md Normal file
View File

@@ -0,0 +1,117 @@
`PyTorch` is an open-source machine learning library widely used for applications such as computer vision and natural language processing. It's known for its flexibility, speed, and ease of use. Developed by Facebook's AI Research lab, PyTorch provides two high-level features: Tensor computation (like NumPy) with strong GPU acceleration and Deep Neural Networks built on a tape-based autograd system. Heres a concise reference guide for common use cases with `PyTorch`:
# `PyTorch` Reference Guide
## Installation
```
pip install torch torchvision
```
## Basic Concepts
### Importing PyTorch
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
```
### Tensors
Tensors are a specialized data structure that are very similar to arrays and matrices. In PyTorch, we use tensors to encode the inputs and outputs of a model, as well as the models parameters.
```python
# Create a tensor
x = torch.Tensor([[1, 2], [3, 4]])
# Create a tensor with random data
random_tensor = torch.rand(2, 3)
# Create a tensor with zeros
zeros_tensor = torch.zeros(2, 3)
# Check if your system supports CUDA
device = "cuda" if torch.cuda.is_available() else "cpu"
```
## Building Models
### Defining a Neural Network
```python
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
return self.fc3(x)
net = Net().to(device)
```
## Training a Model
### Loss Function and Optimizer
```python
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
```
### Training Loop
```python
for epoch in range(num_epochs):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data[0].to(device), data[1].to(device)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f'Epoch {epoch + 1}, Loss: {running_loss / len(trainloader)}')
```
## Evaluating the Model
```python
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data[0].to(device), data[1].to(device)
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f'Accuracy of the network on test images: {100 * correct / total}%')
```
## Saving and Loading Models
```python
# Save
torch.save(net.state_dict(), 'model.pth')
# Load
net = Net()
net.load_state_dict(torch.load('model.pth'))
net.to(device)
```
`PyTorch` excels in offering flexibility and speed during the development of complex machine learning models. Its dynamic computation graph paradigm allows modifications to the graph on the fly and maps to Pythonic programming closely. This guide covers foundational concepts and tasks in PyTorch, but the librarys capabilities extend to support advanced machine learning and artificial intelligence projects.
PyTorch's intuitive design and ease of use, along with its comprehensive documentation and vibrant community, make it a preferred tool for both academic researchers and developers in industry.

View File

@@ -0,0 +1,151 @@
# Python Cheat Sheet
## 1. Variables, Data Types, and Basic Operations
Python has several fundamental data types, including integers (int), floating point numbers (float), and strings (str). Python is a dynamically typed language, which means you don't need to declare the data type of a variable when you define it.
```python
a = 10 # Integer
b = 3.14 # Float
c = "Hello, World!" # String
```
Operators allow you to perform operations on variables. Arithmetic, comparison, assignment, logical, and identity operators are some of the main types in Python.
```python
a = 10
b = 20
sum = a + b # Addition
difference = a - b # Subtraction
#... remaining code ...
```
## 2. Control Structures (Conditionals and Loops)
Python uses `if`, `elif`, and `else` for conditional statements. Loops in Python can be programmed using a `for` or `while` loop.
```python
# If-else statement
if a > b:
print("a is greater than b")
else:
print("a is not greater than b")
# For loop
for i in range(5):
print(i)
```
## 3. Functions
Functions in Python are defined using the `def` keyword. They are used to encapsulate a piece of code that performs a specific task.
```python
def greet(name):
print("Hello, " + name)
greet("Alice")
```
## 4. Lists, Tuples, Sets, and Dictionaries
Python has several types of compound data structures that can hold multiple values, including lists, tuples, sets, and dictionaries.
```python
# Lists
my_list = [1, 2, 3, 4, 5]
# Dictionaries
my_dict = {"apple": 1, "banana": 2, "cherry": 3}
```
## 5. File Handling
Python has built-in functions for reading and writing files. `open()` function is used to open a file. It returns a file object and is most commonly used with two arguments: `open(filename, mode)`.
```python
# Reading a file
file = open('file.txt', 'r')
content = file.read()
file.close()
# Writing to a file
file = open('file.txt', 'w')
file.write("Hello, World!")
file.close()
```
## 6. Exception Handling
Python uses `try` and `except` blocks to catch and handle exceptions. Python executes code following the `try` statement as a "normal" part of the program. The code that follows the `except` statement is the program's response to any exceptions in the preceding try clause.
```python
try:
# Code that may raise an exception
x = 1 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
```
## 7. Classes and Objects
Python is an object-oriented programming language. You define classes to create your own types.
```python
class MyClass:
x = 5
p1 = MyClass() # Create an object of MyClass
print(p1.x) # Access the properties of MyClass using the object
```
## 8. Modules and Packages
Modules in Python are simply Python files with a .py extension, which implement a set of functions. A package is a way of organizing related modules into a single directory hierarchy.
```python
# Importing a module
import math
print(math.sqrt(16)) # Use a function from the math module
# Importing
ChatGPT
a package
import os.path
print(os.path.join('/a', 'b', 'c')) # Use a function from the os.path package
```
## 9. Working with JSON
Python has a built-in package called `json`, which can be used to work with JSON data.
```python
import json
# Some JSON:
x = '{"name": "John", "age": 30, "city": "New York"}'
# Parse x:
y = json.loads(x)
# The result is a Python dictionary:
print(y["age"])
```
## 10. Making HTTP requests
The `requests` library is the de facto standard for making HTTP requests in Python. It abstracts the complexities of making requests behind a beautiful, simple API so that you can focus on interacting with services and consuming data in your application.
```python
import requests
response = requests.get('https://www.example.com')
# Print the status code
print(response.status_code)
# Print the content
print(response.text)
```
These are the basics to get you started with Python! Each of these topics has more depth to explore as you become more comfortable with the language.

View File

@@ -0,0 +1,663 @@
# Python Virtual Environment Setup Guide
This guide provides step-by-step instructions on setting up a Python virtual environment and managing dependencies for your project.
## Creating a Virtual Environment
1. **Navigate to Your Project Directory**
Open a terminal and navigate to the directory where your project is located.
```bash
cd path/to/your/project
```
2. **Create the Virtual Environment**
Use the `venv` module to create a virtual environment named `env`.
```bash
python3 -m venv env
```
## Activating the Virtual Environment
1. **Activate the Environment**
Once the environment is created, you need to activate it.
```bash
source env/bin/activate
```
After activation, your prompt will change to indicate that you are in the virtual environment.
## Managing Dependencies
1. **Create a Requirements File**
Create a `requirements.txt` file to keep track of project dependencies.
```bash
touch requirements.txt
```
2. **Freeze Installed Packages**
If you have already installed packages, you can list them in `requirements.txt`.
```bash
pip freeze > requirements.txt
```
3. **View the Requirements File**
To see the contents of `requirements.txt`, use the `cat` command.
```bash
cat requirements.txt
```
### Managing Dependencies
- **Install packages** using `pip` while the environment is activated.
- **Create a `requirements.txt`** file to keep track of your dependencies.
```bash
pip freeze > requirements.txt
```
- **Install dependencies** from the file in a new environment:
```bash
pip install -r requirements.txt
```
## Deactivating the Virtual Environment
1. **Deactivate the Environment**
When you are done working in the virtual environment, deactivate it.
```bash
deactivate
```
# Deactivate the current environment
```bash
deactivate
```
# Remove the environment directory (be careful with this command)
```bash
rm -rf env
```
# Recreate the virtual environment
```bash
python3 -m venv env
```
# Activate the environment
```bash
source env/bin/activate
```
# Install python-docx package
```bash
pip install python-docx
```
## Tips
- Always activate your virtual environment before working on your project.
- Use `pip` to install any new packages while the environment is active.
- Regularly update your `requirements.txt` file to reflect new dependencies.
- Remember to deactivate the virtual environment when you're finished.
By following these steps, you can effectively manage your Python project's dependencies in a clean, isolated environment.
---
## Standard Directory Structure for Python Projects on Debian
In a Python project, particularly on a Debian Linux system, it's important to follow a standard directory structure for organization and efficiency. Here is a recommended structure:
1. **src or app**: This directory holds the source code of the project.
```bash
mkdir src
```
2. **docs**: This is where all the documentation related to the project should be kept.
```bash
mkdir docs
```
3. **tests**: This directory will contain test scripts and test data.
```bash
mkdir tests
```
4. **data**: If your project requires any data files, store them here. It's useful for projects that need to access datasets or other resources.
```bash
mkdir data
```
5. **env**: This is for the Python virtual environment. Although it's common to place it in the project root, some prefer to keep it outside to prevent its accidental inclusion in version control systems like Git.
- To create a virtual environment in Debian, navigate to your project root and run:
```bash
python3 -m venv env
```
- Remember to add `env/` to your `.gitignore` file if you decide to place it within the project root.
### Activating the Virtual Environment on Debian
- After creating the virtual environment, you can activate it using:
```bash
source env/bin/activate
```
### Deactivating the Virtual Environment
- To exit the environment, simply run:
```bash
deactivate
```
### Tips for Debian Users
- Always activate your virtual environment before starting work on the project.
- Install project-specific Python packages within the virtual environment to avoid conflicts with system-wide packages.
- Regularly update your `requirements.txt` file to keep track of dependencies.
- Use relative paths in your scripts and tools for better portability and flexibility.
By following this structure and these tips, you can maintain a well-organized and efficient development environment for your Python projects on Debian.
---
# Setting Up a Virtual Environment for KnowledgeBase-Tech Hugo Site
## Update PATH Environment Variable
Before installing `virtualenv`, ensure your PATH includes the directory where Python packages are installed. If you see a message like "The script virtualenv is installed in '/home/medusa/.local/bin' which is not on PATH," you'll need to update your PATH. Add the following line to your `.bashrc` or `.profile` file:
```bash
export PATH="$HOME/.local/bin:$PATH"
```
Then, reload your profile:
```bash
source ~/.bashrc
```
or for a login shell:
```bash
source ~/.profile
```
## Install Virtual Environment
Ensure Python and pip are installed, then install `virtualenv`:
```bash
pip3 install virtualenv
```
## Create the Virtual Environment
Create a virtual environment named `KnowledgeBase-Tech_env`:
```bash
virtualenv KnowledgeBase-Tech_env
```
## Activate the Virtual Environment
To start using the virtual environment:
```bash
source KnowledgeBase-Tech_env/bin/activate
```
## Install Dependencies
While the environment is active, install any required packages:
```bash
pip install <package-name>
```
## Freeze Requirements
To keep track of your project's dependencies, you can freeze the installed packages into a `requirements.txt` file:
```bash
pip freeze > requirements.txt
```
This file can be used to install the exact versions of the required packages on other setups or by other developers working on the project.
## Deactivate the Virtual Environment
When you're done working, deactivate the environment:
```bash
deactivate
```
## Working with the Environment
Remember to activate the `KnowledgeBase-Tech_env` environment every time you work on the KnowledgeBase-Tech Hugo site. This ensures all dependencies are isolated to this specific project.
Replace `<package-name>` with the specific packages you need for your Hugo site. The addition of the freeze requirements section will help maintain a consistent set of dependencies across different development environments.
---
# Setting Up a Virtual Environment for KnowledgeBase-Tech Hugo Site
## Install Virtual Environment
First, ensure Python and pip are installed, then install `virtualenv`:
```bash
pip3 install virtualenv
```
## Create the Virtual Environment
Create a virtual environment named `KnowledgeBase-Tech_env`:
```bash
virtualenv KnowledgeBase-Tech_env
```
## Activate the Virtual Environment
To start using the virtual environment:
```bash
source KnowledgeBase-Tech_env/bin/activate
```
## Install Dependencies
While the environment is active, install any required packages:
```bash
pip install <package-name>
```
## Deactivate the Virtual Environment
When you're done working, deactivate the environment:
```bash
deactivate
```
## Working with the Environment
Remember to activate the `KnowledgeBase-Tech_env` environment every time you work on the KnowledgeBase-Tech Hugo site. This ensures all dependencies are isolated to this specific project.
Replace `<package-name>` with any specific Python package you need for your Hugo site. This guide will help you maintain a clean and isolated environment for your Hugo site development.
---
**Install Python and pip (if not already installed):**
Ubuntu 22.04 typically comes with Python 3 installed. You can check if Python is installed and its version by running:
```bash
python3 --version
```
To install pip, run:
```bash
sudo apt update
sudo apt install python3-pip
```
2. **Install Virtual Environment:**
With pip installed, you can now install `virtualenv`, which is a tool for creating isolated Python environments. Run the following command:
```bash
pip3 install virtualenv
```
3. **Create a Virtual Environment:**
Navigate to the directory where you want to set up your Mkdocs site and create a virtual environment. Replace `myenv` with your preferred environment name.
```bash
virtualenv myenv
```
4. **Activate the Virtual Environment:**
To start using the virtual environment, you need to activate it. Run:
```bash
source myenv/bin/activate
```
Once activated, your command prompt should change to indicate that you are now working inside `myenv`.
5. **Install Mkdocs in the Virtual Environment:**
With your virtual environment active, install Mkdocs:
```bash
pip install mkdocs
```
6. **Create Your Mkdocs Project:**
After Mkdocs is installed, you can create a new Mkdocs project:
```bash
mkdocs new my-project
```
Replace `my-project` with your project name. This will create a new directory with a basic configuration.
7. **Run Mkdocs Locally:**
To see your Mkdocs site, navigate to your project directory and run:
```bash
cd my-project
mkdocs serve
```
This command starts a local server. You can view your site by going to `http://127.0.0.1:8000` in your web browser.
8. **Deactivate the Virtual Environment:**
When youre done working in the virtual environment, you can deactivate it by running:
```bash
deactivate
```
---
# `venv` User Guide for Python Virtual Environments
The `venv` module, included in Python 3, is used for creating isolated Python environments. This guide provides instructions on creating and managing virtual environments with `venv`.
## Creating Virtual Environments
### Creating the First Environment
1. **Navigate to your project directory:**
```bash
cd path/to/your/project
```
2. **Create a virtual environment named `env1`:**
```bash
python3 -m venv env1
```
3. **Activate the environment:**
- On Windows:
```bash
.\env1\Scripts\activate
```
- On Unix or MacOS:
```bash
source env1/bin/activate
```
### Creating the Second Environment
1. **Create another environment named `env2`:**
```bash
python3 -m venv env2
```
2. **Activate the environment as shown previously.**
## Best Practices
### Activating and Deactivating Environments
- **Activate** an environment before working on the project.
- **Deactivate** when done. Just type `deactivate` in the terminal.
### Managing Dependencies
- **Install packages** using `pip` while the environment is activated.
- **Create a `requirements.txt`** file to keep track of your dependencies.
```bash
pip freeze > requirements.txt
```
- **Install dependencies** from the file in a new environment:
```bash
pip install -r requirements.txt
```
### Keeping Environments Separate
- **Do not commit the environment** folder to version control. Add it to `.gitignore`.
- **Commit `requirements.txt`** to ensure consistency across different setups.
### Updating the Python Version
- If you need to update Python, create a **new environment** with the updated version.
- Reinstall your dependencies in the new environment from `requirements.txt`.
## Example Workflow
1. **Activate the environment** when starting work.
2. **Install and update packages** as needed.
3. **Regularly update `requirements.txt`** to reflect new dependencies.
4. **Deactivate the environment** when done.
By following these practices, you can maintain a clean and consistent development environment for your Python projects using `venv`.
---
# Guide to Python Virtual Environments
Python virtual environments are essential tools for managing project-specific dependencies and Python versions. This guide covers three popular tools: `virtualenv`, `venv`, and `conda`.
## 1. virtualenv
### Description
`virtualenv` is a widely-used tool for creating isolated Python environments. It supports both Python 2 and 3 and allows different environments to have different versions of Python and packages.
### Pros
- Compatible with Python 2 and 3.
- Creates environments with different Python versions.
- Well-documented and widely used.
### Cons
- More complex than `venv`.
- Requires installation as it's not part of the standard library.
### Best for
Developers working on multiple projects with varying dependencies, especially if Python 2 support is needed.
## 2. venv
### Description
`venv` is a module in Python 3 (3.3 and newer) for creating virtual environments, offering a streamlined approach compared to `virtualenv`.
### Pros
- Part of the Python standard library (no extra installation).
- Simpler than `virtualenv`.
- Good for Python 3 projects.
### Cons
- Only for Python 3.
- Less flexibility in Python version management compared to `virtualenv`.
### Best for
Python 3 projects where simplicity and ease of use are key, without the need for Python 2.
## 3. conda
### Description
`conda` is a package and environment management system that supports multiple languages. It's especially popular in data science for managing complex dependencies.
### Pros
- Manages both Python and non-Python packages.
- Ideal for complex, multi-language projects.
- Widely used in data science and machine learning.
### Cons
- More complex than `venv` and `virtualenv`.
- Overkill for simple Python-only projects.
### Best for
Complex projects involving data science, machine learning, or multiple programming languages.
## Choosing the Right Tool
- **Project Complexity**: Use `venv` for simple projects, `virtualenv` for medium complexity, and `conda` for complex, multi-language projects.
- **Ease of Use**: `venv` for straightforward Python 3 projects, `virtualenv` for more control, and `conda` for complex dependency management.
- **Cross-Language Support**: Choose `conda` for projects requiring multi-language support.
- **Community and Documentation**: All three have strong communities and documentation. Choose based on project needs.
In summary, your choice depends on the project's requirements, complexity, and language support. `venv` is suitable for most Python 3 projects, while `virtualenv` and `conda` cater to more complex scenarios.
---
# Best Practices for Structuring Python Virtual Environments
Organizing virtual environments is crucial for maintaining a clean and efficient workspace when working on multiple Python projects. Below are some guidelines to help structure your virtual environments effectively.
## 1. Project-Specific Environments
- **Separate Environment for Each Project**:
Create an individual virtual environment for every project to avoid dependency conflicts.
- **Environment Location**:
```plaintext
Place the virtual environment directory inside the project's root directory.
Example Structure:
MyProject/
├── .gitignore
├── my_project_env/
├── src/
├── tests/
└── requirements.txt
```
Ensure to exclude the environment directory from version control.
## 2. Naming Conventions
- **Descriptive Names**:
Choose names that clearly identify the associated project, like `data_analyzer_env` for a "DataAnalyzer" project.
- **Consistency**:
Maintain consistent naming conventions across different projects.
## 3. Requirements File
- **Use `requirements.txt`**:
Include a `requirements.txt` file in the root directory of each project.
```bash
pip freeze > requirements.txt
```
## 4. Documentation
- **README File**:
Add a README in your project's root, documenting the setup and activation steps for the environment.
## 5. Centralized Management (Optional)
- **Central Directory**:
Alternatively, you can store all virtual environments in a central directory, e.g., `~/python_environments/`.
```plaintext
python_environments/
├── data_analyzer_env/
├── web_app_env/
└── machine_learning_env/
```
- **Naming Reference**:
Ensure the names are descriptive enough to indicate their associated project.
## 6. Environment Variables
- **.env Files**:
Use `.env` files for environment-specific settings, loading them with libraries like `python-dotenv`.
## 7. Regular Maintenance
- **Keep Updated**:
Regularly update the dependencies in your environments.
- **Cleanup**:
Remove or archive environments for inactive projects.
These guidelines aim to provide a structured approach to managing Python virtual environments, enhancing clarity and efficiency in your development workflow.
---
# Managing Environment Variables in Python Virtual Environments
Using `.env` files for environment-specific settings is a best practice in Python development. This guide explains how to set up and use `.env` files within virtual environments.
## What are `.env` Files?
- `.env` files are simple text files that contain environment variables.
- They are used to store configuration settings that should not be hard-coded in your code, such as API keys, database URLs, and other sensitive information.
## Setting Up `.env` Files
### 1. Creating `.env` File
- Place a `.env` file in your project's root directory.
- Add environment variables in the format `KEY=value`.
```plaintext
# Example .env file
DATABASE_URL=postgresql://user:password@localhost/mydatabase
API_KEY=yourapikey
```
### 2. Using `python-dotenv` to Load Variables
- Install `python-dotenv` to easily load the variables from `.env` file.
```bash
pip install python-dotenv
```
- Import `dotenv` in your main script and load the variables.
```python
from dotenv import load_dotenv
load_dotenv()
```
## Accessing Environment Variables
- Access variables using `os.environ`.
```python
import os
database_url = os.getenv('DATABASE_URL')
api_key = os.getenv('API_KEY')
```
## Best Practices
- **Never Commit `.env` Files**: Add `.env` to your `.gitignore` file to prevent sensitive information from being committed to version control.
- **Use Different `.env` Files for Different Environments**: For example, `.env.development`, `.env.production` for different deployment stages.
- **Keep `.env` File Updated**: Regularly update the `.env` file with any new or changed environment variables.
## Security Considerations
- Keep your `.env` files secure and only share them with trusted team members.
- Regularly audit the environment variables and remove any that are no longer in use.
By following these practices, you can securely manage environment-specific settings in your Python projects, keeping sensitive information out of your source code.
---
# Recommended Python Project Structure with Virtual Environment
When setting up a Python project with a virtual environment, organizing your project's directory structure effectively is crucial. Below is an expanded explanation of a typical project structure:
## Project Directory: MyProject
### `MyProject/` - The Root Directory
- This is the main folder that contains your entire project. It's named after your project (`MyProject` in this case).
#### `.gitignore`
- This file tells Git which files or folders to ignore in your project.
- It's essential to include your virtual environment directory (`my_project_env/`) here to prevent it from being tracked by version control, as it contains system-specific settings and dependencies.
#### `my_project_env/`
- This is your virtual environment directory where dependencies and settings specific to this project are stored.
- It's created by running `python3 -m venv my_project_env` inside your root directory.
- This environment keeps your project's dependencies isolated from the global Python installation and other projects.
#### `src/`
- Short for "source", this directory contains all the source code of your project.
- It's a good practice to separate your code into this directory to keep it organized and distinguish it from other project files.
- Within `src/`, you can have various Python scripts and modules that make up your application.
#### `tests/`
- This folder contains all the test code for your project.
- Keeping tests in a separate directory helps in maintaining them distinctly from your actual project code.
- It typically includes unit tests, integration tests, and other test scripts.
#### `requirements.txt`
- This file lists all the Python dependencies required by your project.
- It's created by running `pip freeze > requirements.txt` inside your activated virtual environment.
- This file ensures that anyone who clones your project can install the exact same dependencies by running `pip install -r requirements.txt`.
This structure is just a guideline and can be adjusted based on the specific needs of your project. The key is to keep it organized and logical to facilitate easy navigation and collaboration.

View File

@@ -0,0 +1,44 @@
# Python and JavaScript in Software Development
In Python and JavaScript projects, the concepts of code, runtime, and libraries are crucial, just as they are in general software development.
## Code
### Python
- **Python Code**: Written in `.py` files using the Python programming language, an interpreted, high-level language known for its simplicity and readability.
- **Domains**: Widely used in data analysis, web development, and machine learning.
- **Syntax**: Utilizes indentation to define code blocks, emphasizing readability.
### JavaScript
- **JavaScript Code**: Written in `.js` files, primarily used for client-side scripting in web development.
- **Usage**: Enables interactive elements on web pages and is also used on the server-side (e.g., Node.js).
- **Characteristics**: Event-driven and non-blocking, crucial for responsive web applications.
## Runtime
### Python Runtime
- **CPython**: The most common Python interpreter, written in C.
- **Alternatives**: PyPy for performance (JIT compilation) and Jython for Java integration.
- **Functionality**: Handles tasks like memory management and object lifecycle.
### JavaScript Runtime
- **V8 Engine**: Developed by Google, written in C++, and used in browsers and Node.js.
- **Capabilities**: Manages event loops and asynchronous behavior, key to JavaScript's performance.
## Libraries
### Python Libraries
- **Standard Library**: Included with Python, offering a wide range of functionality (math operations, data structures, file I/O).
- **Third-Party Libraries**: NumPy (numerical computing), Pandas (data manipulation), Flask and Django (web development).
### JavaScript Libraries
- **jQuery**: A third-party library for cross-browser JavaScript DOM manipulation.
- **Modern Libraries**: React (user interfaces), Express.js (server-side applications), D3.js (data visualization).
## Integration and Summary
- **Integration**: Code written by developers is interpreted or compiled by the runtime, which interfaces with libraries for additional functionality.
- **Applications**: Facilitates development of applications from simple scripts to complex web applications.
- **Evolving Nature**: Regular updates to the language, runtime environments, and libraries reflect the dynamic nature of software development.
This document provides a comprehensive understanding of how code, runtime, and libraries work together in Python and JavaScript projects, highlighting their roles in creating powerful and versatile applications.

View File

@@ -0,0 +1,533 @@
# Python Functions: A Comprehensive Guide
Python functions are the building blocks of Python programming, enabling code reusability, organization, and modularity. This guide explores Python functions, their syntax, and how to use them effectively.
## Introduction to Python Functions
A function is a block of code that runs when it's called. It can accept input, produce output, and perform a specific task. Here's a basic example:
```python
# Defining a function
def greet(name):
return f"Hello, {name}!"
# Calling the function
print(greet("Alice"))
```
- **Defining Functions**: Use the `def` keyword followed by the function name and parentheses.
- **Arguments**: Functions can take arguments, which are specified within the parentheses.
- **Returning Values**: Use the `return` statement to send back an output.
## Key Concepts
### Parameters vs. Arguments
- **Parameters** are the variables listed inside the parentheses in the function definition.
- **Arguments** are the values passed to the function when it is called.
### Default Parameters
You can assign default values to parameters, making them optional during a function call:
```python
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
print(greet("Alice")) # Uses default greeting
print(greet("Alice", "Goodbye")) # Overrides default greeting
```
### Keyword Arguments
Keyword arguments allow you to specify arguments by their names, making your function calls more readable:
```python
def describe_pet(animal_type, pet_name):
print(f"I have a {animal_type} named {pet_name}.")
describe_pet(animal_type="hamster", pet_name="Harry")
```
### Arbitrary Arguments
Sometimes you might not know how many arguments will be passed into your function. Use `*args` for arbitrary number of positional arguments and `**kwargs` for arbitrary number of keyword arguments:
```python
def make_pizza(*toppings):
print("Making a pizza with the following toppings:")
for topping in toppings:
print(f"- {topping}")
make_pizza('pepperoni', 'mushrooms', 'green peppers')
```
## Advanced Function Features
### Lambda Functions
Lambda functions are small, anonymous functions defined with the `lambda` keyword. They can have any number of arguments but only one expression:
```python
multiply = lambda x, y: x * y
print(multiply(2, 3))
```
### Function Annotations
Function annotations provide a way of associating metadata with function parameters and return values:
```python
def greet(name: str) -> str:
return f"Hello, {name}!"
```
### Generators
Functions can also be generators, which yield a sequence of values lazily, meaning they generate each value only when needed:
```python
def countdown(num):
while num > 0:
yield num
num -= 1
for i in countdown(5):
print(i)
```
## Best Practices
- **Descriptive Names**: Choose function names that clearly describe their purpose.
- **Small and Focused**: Functions should do one thing and do it well.
- **Documentation Strings**: Use docstrings to describe what your function does, its parameters, and its return value.
## Conclusion
Python functions are a fundamental aspect of writing clean, efficient, and reusable code. By understanding and applying the concepts in this guide, you'll be able to create more complex and modular Python applications with ease.
This guide should provide you with a solid understanding of Python functions, covering their definition, usage, and some advanced features to enhance your programming skills.
---
# Understanding Objects in Python: A Technical Guide
Python is an object-oriented programming language at its core, which means everything in Python is an object. This guide delves into the technical aspects of Python objects, including their creation, manipulation, and the principles that govern their interactions.
## Basics of Python Objects
In Python, objects are instances of classes, which can contain data (attributes) and functions (methods) that operate on the data. Heres how you can define a class and create an object:
```python
class MyClass:
def __init__(self, value):
self.attribute = value
def method(self):
return f"Attribute value: {self.attribute}"
# Creating an object
my_object = MyClass(10)
print(my_object.method())
```
- **Class Definition**: Use the `class` keyword followed by the class name and a colon.
- **The `__init__` Method**: Known as the constructor, it initializes the objects state.
- **Attributes and Methods**: Attributes store the object's state, and methods define its behavior.
## Object Attributes and Methods
### Instance Attributes vs. Class Attributes
- **Instance Attributes**: Defined within methods and prefixed with `self`, unique to each object.
- **Class Attributes**: Defined outside of methods and are shared across all instances of the class.
### Instance Methods, Class Methods, and Static Methods
- **Instance Methods**: Operate on an instance of the class and have access to `self`.
- **Class Methods**: Operate on the class itself, rather than instance, and take `cls` as the first parameter. Use the `@classmethod` decorator.
- **Static Methods**: Do not access the class or its instances and are defined using the `@staticmethod` decorator.
```python
class MyClass:
class_attribute = "Shared"
def __init__(self, value):
self.instance_attribute = value
def instance_method(self):
return self.instance_attribute
@classmethod
def class_method(cls):
return cls.class_attribute
@staticmethod
def static_method():
return 'Static method called'
```
## Inheritance and Polymorphism
Inheritance allows one class to inherit the attributes and methods of another, enabling code reuse and the creation of complex object hierarchies.
```python
class BaseClass:
pass
class DerivedClass(BaseClass):
pass
```
Polymorphism allows objects of different classes to be treated as objects of a common superclass, particularly when they share a method name but implement it differently.
```python
def common_interface(obj):
obj.method_name()
```
## Magic Methods
Magic methods (or dunder methods) are special methods with double underscores at the beginning and end of their names. They enable operator overloading and custom behavior for built-in operations.
```python
class MyClass:
def __init__(self, value):
self.value = value
def __str__(self):
return f"MyClass with value: {self.value}"
```
## Encapsulation and Abstraction
- **Encapsulation**: The bundling of data with the methods that operate on that data.
- **Abstraction**: Hiding the internal implementation details of a class and exposing only the necessary parts.
## Conclusion
Understanding the technical aspects of Python objects is crucial for mastering object-oriented programming in Python. By grasping concepts like inheritance, polymorphism, and magic methods, you can design robust and reusable code structures.
---
# Mastering List Comprehensions in Python
List comprehensions in Python provide a concise way to create lists. They consist of brackets containing an expression followed by a `for` clause, then zero or more `for` or `if` clauses. This guide will explore the syntax and capabilities of list comprehensions, helping you write more Pythonic code.
## Basic Syntax
The basic syntax of a list comprehension is:
```python
[expression for item in iterable]
```
- **expression**: An expression producing a value to be included in the new list.
- **item**: The variable representing each element in the iterable.
- **iterable**: A sequence (list, tuple, string, etc.) or collection (set, dictionary, etc.) that can be iterated over.
### Example: Squaring Numbers
```python
squares = [x**2 for x in range(10)]
print(squares)
```
This creates a list of the squares of numbers 0 through 9.
## Adding Conditionals
List comprehensions can also include conditional statements to filter items from the input iterable.
### Filtering Items
```python
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares)
```
This generates a list of squares for even numbers only.
### Conditional Expressions
You can also use conditional expressions within the expression part of the list comprehension:
```python
values = [x if x > 0 else -x for x in range(-5, 5)]
print(values)
```
This creates a list where negative numbers are made positive.
## Nested List Comprehensions
List comprehensions can be nested to create complex lists:
```python
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [elem for row in matrix for elem in row]
print(flattened)
```
This flattens a list of lists into a single list.
## Using List Comprehensions with Other Data Types
While they're called list comprehensions, this syntax can be used to create sets and dictionaries as well.
### Set Comprehensions
```python
square_set = {x**2 for x in range(-5, 5)}
print(square_set)
```
This creates a set of squared numbers, removing duplicates.
### Dictionary Comprehensions
```python
square_dict = {x: x**2 for x in range(5)}
print(square_dict)
```
This creates a dictionary with numbers as keys and their squares as values.
## Best Practices
- **Readability**: Use list comprehensions for simple expressions and operations. For complex logic, consider using regular loops.
- **Performance**: List comprehensions can be faster than equivalent `for` loops, but readability should not be sacrificed for slight performance gains.
- **Avoid Side Effects**: Do not use list comprehensions for operations that have side effects, such as file I/O or modifying external variables.
## Conclusion
List comprehensions are a powerful feature of Python that allow for clean, readable, and efficient code. By understanding and applying the concepts outlined in this guide, you can leverage list comprehensions to simplify your code while maintaining or even improving its performance.
---
# Python Dictionaries: A Guide for API Calls
Python dictionaries are essential for handling data in Python, especially when working with API calls. This guide provides a concise overview of dictionaries and their use in constructing API payloads.
## Introduction to Python Dictionaries
Dictionaries in Python are collections of key-value pairs, allowing you to store and manage data dynamically. Here's a quick rundown:
```python
# Example of a Python dictionary
my_dict = {
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
```
- **Key Characteristics**:
- **Unordered**: The items do not have a defined order.
- **Changeable**: You can add, remove, or modify items.
- **Indexed**: Accessed by keys, which must be unique and immutable.
## Basic Operations
- **Accessing Items**: `value = my_dict["key1"]`
- **Adding Items**: `my_dict["newKey"] = "newValue"`
- **Removing Items**: `my_dict.pop("key1")`, `del my_dict["key2"]`
- **Looping Through**: `for key in my_dict: print(key, my_dict[key])`
## Using Dictionaries for API Calls
When making API calls, dictionaries are often used to construct payloads or parameters:
```python
# API payload as a dictionary
payload = {
"username": "user",
"password": "pass",
"email": "email@example.com"
}
# Using requests library for API call
import requests
response = requests.post("https://api.example.com/users", json=payload)
```
- Dictionaries are converted to JSON or other formats suitable for web transmission.
- This method simplifies sending structured data over HTTP requests.
## Best Practices
- **Key Management**: Ensure keys are descriptive and consistent.
- **Data Validation**: Validate and sanitize data before adding it to a dictionary, especially when received from user input.
- **Dynamic Construction**: Leverage dictionary comprehensions and dynamic insertion for creating complex payloads.
## Conclusion
Understanding Python dictionaries is fundamental for API interactions, providing a structured and flexible way to handle data. Their key-value nature makes them ideal for constructing API payloads, facilitating efficient data transmission over networks.
Remember to follow best practices for key management and data validation to ensure secure and effective API communication.
This guide encapsulates the essentials of Python dictionaries, focusing on their application in API calls, which should be quite handy for your learning and development tasks.
---
# Advanced Python Concepts and Best Practices
## Advanced OOP Features
### Polymorphism and Duck Typing
Python is known for its "duck typing" philosophy, encapsulating the idea of polymorphism. It means that an object's suitability for a task is determined by the presence of certain methods and properties, rather than the object's type itself.
```python
def quack_and_fly(thing):
thing.quack()
thing.fly()
# If it looks like a duck and quacks like a duck, it's a duck.
```
### Abstract Base Classes (ABCs)
Abstract Base Classes are a form of interface checking more strict than duck typing. ABCs allow for the definition of methods that must be created within any child classes implemented from the abstract base.
```python
from abc import ABC, abstractmethod
class Bird(ABC):
@abstractmethod
def fly(self):
pass
class Duck(Bird):
def fly(self):
print("Duck flying")
```
## Decorators
Decorators allow you to modify or enhance functions without changing their definitions. They are a powerful tool for logging, enforcing access control, instrumentation, and more.
```python
def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
```
## Generators and Iterators
Generators provide a way to create iterators in a more straightforward manner, using the `yield` statement. They are used to iterate through sequences efficiently without requiring the entire sequence to be stored in memory.
```python
def my_generator():
yield 1
yield 2
yield 3
for value in my_generator():
print(value)
```
## Context Managers
Context managers allow setup and teardown operations to be executed around a block of code. The `with` statement simplifies the management of resources such as file streams.
```python
with open('file.txt', 'w') as opened_file:
opened_file.write('Hello, world!')
```
## Exception Handling
Proper exception handling is crucial for creating reliable and resilient applications. Python provides try-except-else-finally blocks for catching and handling exceptions.
```python
try:
# Code block where exceptions can occur
pass
except ExceptionType1:
# Handle specific exception
pass
except ExceptionType2 as e:
# Handle specific exception and access its information
pass
else:
# Execute if no exceptions
pass
finally:
# Execute no matter what
pass
```
## Testing
Testing is critical for ensuring code reliability and functionality. Python's `unittest` and `pytest` frameworks facilitate the creation and management of tests.
```python
# Example using pytest
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
```
This guide presents a deeper dive into essential Python concepts beyond classes and data classes. Mastery of these topics will significantly enhance your Python programming skills and your ability to develop robust, efficient, and maintainable Python applications.
Each of these topics represents a core aspect of Python programming that, when understood and applied, can greatly improve the quality and efficiency of your code. As with any skill, practice and continuous learning are key to mastery.
---
# Python Classes and Data Classes Reference Guide
## Python Classes
### Basic Structure
```python
class MyClass:
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2
def method1(self):
# Method implementation
pass
```
### Key Concepts
- **Encapsulation**: Grouping data and methods that act on the data within a single unit.
- **Inheritance**: Creating a new class that inherits attributes and methods from an existing class.
```python
class DerivedClass(BaseClass):
pass
```
- **Polymorphism**: Allowing methods to do different things based on the object it is acting upon.
- **Abstraction**: Hiding complex implementation details and showing only the necessary features of an object.
## Data Classes (Python 3.7+)
### Basic Usage
```python
from dataclasses import dataclass
@dataclass
class MyDataClass:
attribute1: int
attribute2: float = 0.0
```
### Key Features
- **Automatic Method Generation**: `__init__`, `__repr__`, `__eq__`, and more.
- **Type Hints**: Mandatory for each field, improving code readability.
- **Default Values**: Easily set defaults for fields.
- **Immutability**: Optionally, make instances immutable by using `@dataclass(frozen=True)`.
### Comparison with Standard Classes
- Use **data classes** for simpler, data-centric models to reduce boilerplate.
- Use **standard classes** for more complex behaviors, custom method implementations, and when OOP features like inheritance and polymorphism are needed.
## Practical Tips
- Leverage **inheritance** in standard classes to create a logical, hierarchical structure.
- Use **data classes** for data models in applications like data processing and analysis for cleaner, more maintainable code.
- Remember to use type hints with data classes for better static analysis and error checking.
This reference guide should serve as a quick lookup for the core concepts and usage patterns of Python classes and data classes. Adjust and expand based on specific project needs and complexity.

View File

@@ -0,0 +1,115 @@
`SQLAlchemy` is an indispensable Python library for database operations, providing a full-featured SQL toolkit and Object-Relational Mapping (ORM) capabilities. It allows for efficient and high-performing database access, abstracting away the complexities of raw SQL queries. SQLAlchemy supports a wide range of database backends, including MySQL, PostgreSQL, SQLite, and Oracle, making it a versatile choice for any project requiring database interaction. Heres a concise reference guide for common use cases with `SQLAlchemy`:
# `SQLAlchemy` Reference Guide
## Installation
```
pip install sqlalchemy
```
## Basic Usage
### Connecting to a Database
```python
from sqlalchemy import create_engine
# Create an engine (SQLite in this example)
engine = create_engine('sqlite:///example.db', echo=True)
```
### Defining Models
```python
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
nickname = Column(String)
```
### Creating Schema
```python
# Create all tables stored in this metadata
Base.metadata.create_all(engine)
```
### Starting a Session
```python
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
```
### Inserting Data
```python
new_user = User(name='john', fullname='John Doe', nickname='johnny')
session.add(new_user)
session.commit()
```
### Querying Data
```python
# Query for one instance
user = session.query(User).filter_by(name='john').first()
print(user.fullname)
# Query for multiple instances
for user in session.query(User).order_by(User.id):
print(user.name)
```
### Updating Data
```python
user = session.query(User).filter_by(name='john').first()
user.nickname = 'john the ripper'
session.commit()
```
### Deleting Data
```python
user = session.query(User).filter_by(name='john').first()
session.delete(user)
session.commit()
```
## Advanced Features
### Relationships
```python
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
class Address(Base):
__tablename__ = 'addresses'
id = Column(Integer, primary_key=True)
email_address = Column(String, nullable=False)
user_id = Column(Integer, ForeignKey('users.id'))
user = relationship("User", back_populates="addresses")
User.addresses = relationship("Address", order_by=Address.id, back_populates="user")
```
### Transactions
```python
# SQLAlchemy automatically wraps SQL operations in a transaction.
# Commit to apply changes or rollback to undo them.
session.commit()
# or
session.rollback()
```
### Working with Engines and Connection Pooling
`SQLAlchemy` engine is a factory for database connections, supporting connection pooling and dialect options for different database backends.
`SQLAlchemy` provides a comprehensive set of tools for working with databases in Python, making it easier to implement models, queries, and transactions with less code and greater flexibility. This guide introduces the basic operations, but SQLAlchemy's capabilities extend far beyond these, offering powerful patterns for database interaction and application development.
SQLAlchemy's ORM approach encourages the use of high-level abstractions for database operations, significantly reducing the amount of boilerplate code needed for database interactions and making your code more maintainable.

View File

@@ -0,0 +1,82 @@
`Scrapy` is an open-source and collaborative framework for extracting the data you need from websites in a fast, simple, yet extensible way. It's widely used for web scraping and has built-in support for extracting data from HTML/XML sources using XPath and CSS selectors. Scrapy is designed with a focus on crawling websites and extracting structured data from these pages. Heres a concise reference guide for common use cases with `Scrapy`:
# `Scrapy` Reference Guide
## Installation
```
pip install scrapy
```
## Creating a Scrapy Project
To start a new Scrapy project, navigate to the directory where you want your project to be and run:
```shell
scrapy startproject myproject
```
This command creates a `myproject` directory with the following structure:
```
myproject/
scrapy.cfg # deploy configuration file
myproject/ # project's Python module, you'll import your code from here
__init__.py
items.py # project items definition file
middlewares.py # project middlewares file
pipelines.py # project pipelines file
settings.py # project settings file
spiders/ # a directory where you'll later put your spiders
__init__.py
```
## Defining Items
Items are models that define the structure of the scraped data. They are defined in `items.py`:
```python
import scrapy
class MyItem(scrapy.Item):
name = scrapy.Field()
description = scrapy.Field()
```
## Writing Spiders
Spiders are classes that you define and that Scrapy uses to scrape information from a website (or a group of websites). They must subclass `scrapy.Spider` and define the initial requests to make, optionally how to follow links in the pages, and how to parse the downloaded page content to extract data.
```python
import scrapy
class MySpider(scrapy.Spider):
name = "my_spider"
start_urls = [
'http://example.com',
]
def parse(self, response):
yield {
'url': response.url,
'title': response.css('title::text').get(),
}
```
## Running Spiders
To run a spider, use the `scrapy crawl` command followed by the spiders name:
```shell
scrapy crawl my_spider
```
## Storing the Scraped Data
The simplest way to store the scraped data is by using the `-o` option in the `scrapy crawl` command, which will generate a file containing the scraped data in your chosen format:
```shell
scrapy crawl my_spider -o output.json
```
## Middleware and Pipelines
- **Middlewares** allow you to add custom processing to requests and responses.
- **Item Pipelines** allow you to process and filter the items returned by your spiders. They are defined in `pipelines.py` and need to be activated in your projects `settings.py`.
## Scrapy Shell
Scrapy provides an interactive shell for trying out the selectors without running the spider. Its very useful for developing and debugging:
```shell
scrapy shell 'http://example.com'
```
`Scrapy` is built for efficiency, capable of handling multiple requests concurrently and making it significantly faster than manually written scripts for web scraping. This guide covers basic functionalities, but `Scrapy`'s capabilities extend to a broad range of advanced features, including logging, extensions for deep customization, and built-in support for exporting scraped data in various formats.
Scrapys architecture is built around "spiders," which are self-contained crawlers that are given a set of instructions. In combination with its powerful selector and item capabilities, Scrapy is suited for complex web scraping tasks, from simple website crawls to large-scale web scraping operations.

114
tech_docs/python/Seaborn.md Normal file
View File

@@ -0,0 +1,114 @@
For data visualization in Python, `Seaborn` is a very useful library that builds on top of `Matplotlib`. It provides a high-level interface for drawing attractive and informative statistical graphics. Seaborn simplifies the process of creating complex visualizations from data in pandas DataFrames and arrays, integrating closely with the rest of the Python data science stack. Here's a concise reference guide for common use cases with `Seaborn`:
# `Seaborn` Reference Guide
## Installation
```
pip install seaborn
```
## Basic Usage
### Importing Seaborn
```python
import seaborn as sns
```
### Setting Aesthetics
```python
# Set the aesthetic style of the plots
sns.set_style("whitegrid")
# Set the context for the plot (paper, notebook, talk, poster)
sns.set_context("notebook")
```
## Basic Plots
### Distribution Plots
```python
import numpy as np
# Load a dataset for example
tips = sns.load_dataset("tips")
# Histogram
sns.histplot(data=tips, x="total_bill")
# Kernel Density Estimate (KDE) plot
sns.kdeplot(data=tips, x="total_bill")
# Combining histogram and KDE
sns.histplot(data=tips, x="total_bill", kde=True)
```
### Categorical Plots
```python
# Box plot
sns.boxplot(x="day", y="total_bill", data=tips)
# Violin plot
sns.violinplot(x="day", y="total_bill", data=tips)
# Swarm plot
sns.swarmplot(x="day", y="total_bill", data=tips)
```
### Scatter Plots
```python
# Scatter plot with linear regression model fit
sns.regplot(x="total_bill", y="tip", data=tips)
# Scatter plot without regression model
sns.scatterplot(x="total_bill", y="tip", data=tips)
```
### Heatmaps
```python
# Compute the correlation matrix
corr = tips.corr()
# Generate a heatmap
sns.heatmap(corr, annot=True, fmt=".2f")
```
### Pair Plots
```python
# Pairwise relationships in a dataset
sns.pairplot(tips)
```
## Advanced Visualizations
### Facet Grids
```python
# Create a facet grid
g = sns.FacetGrid(tips, col="time", row="smoker")
g.map(sns.scatterplot, "total_bill", "tip")
```
### Joint Plots
```python
# Draw a plot of two variables with bivariate and univariate graphs
sns.jointplot(x="total_bill", y="tip", data=tips, kind="hex")
```
## Customizing Plots
### Control Figure Aesthetics
```python
# Customize the appearance
sns.set(style="darkgrid", palette="pastel", font="Verdana", font_scale=1.1)
```
### Saving Plots
```python
import matplotlib.pyplot as plt
plt.savefig("output.png")
```
`Seaborn` is particularly well-suited for exploratory data analysis (EDA), making it easy to identify patterns and relationships in data with its diverse plotting functions and beautiful default styles. This guide introduces the basics of creating various types of plots with Seaborn, but the library's capabilities are much broader, offering sophisticated options for customizing plots and analyzing complex datasets.
Seaborns integration with pandas DataFrames enhances its usability in the data science workflow, making it an indispensable tool for data visualization and exploratory data analysis.

View File

@@ -0,0 +1,82 @@
For Python applications that require interaction with the web, especially in the context of automating web browsing, testing web applications, or scraping web content under Linux environments, `Selenium` stands out as a critical tool. Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It's widely used for automating web applications for testing purposes, but it's also capable of doing any web-based administration task automatically.
### Selenium Reference Guide
#### Installation
To use Selenium with Python, you need to install the Selenium package and a WebDriver for the browser you intend to automate (e.g., ChromeDriver for Google Chrome, geckodriver for Firefox).
```sh
pip install selenium
```
Download the WebDriver for your browser and ensure its in your PATH. For Linux systems, this often means placing the WebDriver binary in `/usr/local/bin` or `~/.local/bin`.
#### Basic Usage
##### Starting a Browser Session
Selenium supports multiple browsers out of the box. Heres how to start a session with Google Chrome:
```python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
service = Service(executable_path="/path/to/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("http://www.python.org")
```
##### Interacting with the Page
You can interact with the web page using Selenium's methods to find elements and take actions like clicking or entering text.
```python
search_bar = driver.find_element(By.NAME, "q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.submit()
```
##### Closing the Browser
Dont forget to close your browser session when youre done to free up system resources.
```python
driver.close()
```
#### Advanced Features
- **Headless Mode**: Run browsers in headless mode for faster execution, especially useful in server environments or continuous integration pipelines where no graphical interface is available.
```python
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
```
- **Waiting for Elements**: Selenium can wait for elements to appear or change state, which is useful for dealing with dynamic content or AJAX-loaded data.
```python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myElement"))
)
```
#### Use Cases
- **Automated Testing**: Automate testing of web applications, including unit tests, integration tests, and end-to-end tests.
- **Web Scraping**: Scrape data from websites that require interaction, such as login forms or pagination.
- **Automating Web Tasks**: Automate routine web administration tasks, such as content management, form submissions, or report generation.
#### Integration with Linux Systems
Selenium integrates seamlessly into Linux environments, making it a powerful tool for developers and sysadmins for automating web-based tasks and tests. It can be used in headless mode on servers without a graphical interface, fitting well into automated pipelines and scripts.
#### Security Considerations
When automating web tasks, especially those involving login or sensitive data, ensure you're adhering to the website's terms of service and handling data securely. Avoid storing credentials in plain text and consider using environment variables or secure vaults for sensitive information.
Selenium bridges the gap between Python programming and web browser control, providing a flexible toolkit for automating web interactions. Its comprehensive API supports a wide range of web automation tasks, from testing to data extraction, making it an indispensable resource for Python developers working on web-based applications in Linux environments.

127
tech_docs/python/Shapely.md Normal file
View File

@@ -0,0 +1,127 @@
For high-performance geometric and spatial analysis, `Shapely` is a key Python library. It manipulates and analyzes planar geometric objects, building on the robust, C-based GEOS library for operations like: formation, manipulation, analysis, and visualization of complex geometric shapes. Heres a concise reference guide for common use cases with `Shapely`:
# `Shapely` Reference Guide
## Installation
```
pip install shapely
```
Note: Ensure you have GEOS library or install `Shapely` with binary dependencies included if you're on Windows or Mac.
## Basic Usage
### Importing Shapely
```python
from shapely.geometry import Point, LineString, Polygon
```
### Creating Geometric Objects
#### Points
```python
# Create a point
point = Point(0, 0)
# Create a point with Z dimension
point_z = Point(0, 0, 0)
```
#### Lines
```python
# Create a line from points
line = LineString([(0, 0), (1, 1), (2, 2)])
# Accessing line's coordinates
coords = list(line.coords)
```
#### Polygons
```python
# Create a polygon
polygon = Polygon([(0, 0), (1, 1), (1, 0)])
# Polygon with hole
exterior = [(0, 0), (2, 0), (2, 2), (0, 2)]
hole = [(0.5, 0.5), (1.5, 0.5), (1.5, 1.5)]
polygon_with_hole = Polygon(shell=exterior, holes=[hole])
```
## Operations
### Geometric Operations
```python
# Buffer - create a buffer around the geometry
buffered_point = point.buffer(1.0)
buffered_line = line.buffer(0.5)
# Intersection
intersection = polygon.intersection(buffered_line)
# Union
union = polygon.union(buffered_line)
```
### Geometric Properties
```python
# Area
area = polygon.area
# Length
length = line.length
# Boundary
boundary = polygon.boundary
```
### Relationship Tests
```python
# Check if geometries intersect
intersects = polygon.intersects(line)
# Check if a geometry contains another
contains = polygon.contains(point)
# Check if a geometry is within another
within = point.within(polygon)
```
## Spatial Analysis
### Distance Calculation
```python
# Distance between two geometries
distance = point.distance(line)
```
### Convex Hull
```python
# Create a convex hull around geometry collection
hull = polygon.convex_hull
```
## Advanced Features
### Coordinate Systems and Projections
Shapely does not directly handle coordinate system transformations or projections. It's focused on the geometric operations. For handling projections and transformations, integrating with libraries like `pyproj` is common.
### Integrating with Other Libraries
Shapely geometries can be easily converted to and from formats used by libraries like `GeoPandas` for geospatial analysis and `matplotlib` for plotting.
## Visualizing Geometries
While Shapely itself does not provide visualization capabilities, geometries can be visualized using `matplotlib` or integrated with `GeoPandas` for advanced geospatial visualizations.
```python
import matplotlib.pyplot as plt
from shapely.geometry import Point
point = Point(0.5, 0.5)
x, y = point.x, point.y
plt.plot(x, y, marker='o', color='red')
plt.show()
```
`Shapely` is fundamental for geometric operations in Python, allowing for complex shape analysis and manipulation with straightforward syntax. This guide covers basic geometric creations and operations, but `Shapely` supports a rich set of functionalities that cater to advanced geospatial data manipulation needs.
Shapelys ability to accurately represent and manipulate spatial data makes it an invaluable tool in the field of geospatial analysis, urban planning, and GIS (Geographic Information Systems) development, facilitating sophisticated spatial operations and analyses.

View File

@@ -0,0 +1,69 @@
In the intersection of Python and Linux for automation and process management, `Supervisor` emerges as a critical tool. Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems. It's designed to start processes at boot and keep them running in the desired state according to a configuration file. While Supervisor itself is an application written in Python, it effectively utilizes Python's capabilities to manage system processes, making it an indispensable tool for managing a complex environment of Python applications and scripts.
### Supervisor Reference Guide
#### Installation
Supervisor can be installed via pip, but it may require system-level privileges to install globally or configure certain aspects:
```
pip install supervisor
```
#### Configuration
Supervisor relies on a configuration file, typically named `supervisord.conf`, to determine which programs it should manage. The configuration file specifies how each program should be started, how many instances should run, how they should be restarted if they exit, and more.
##### Sample Configuration
Here's a basic example of what entries in a `supervisord.conf` file might look like:
```ini
[supervisord]
logfile=/var/log/supervisor/supervisord.log
loglevel=info
user=root
[program:your_program]
command=/usr/bin/python3 /path/to/your_script.py
autostart=true
autorestart=true
stderr_logfile=/var/log/your_program.err.log
stdout_logfile=/var/log/your_program.out.log
```
#### Managing Processes
Once Supervisor is installed and configured, you can use the `supervisorctl` command to manage processes defined in the configuration file.
##### Starting Supervisor
To start Supervisor with your configuration:
```
supervisord -c /path/to/your/supervisord.conf
```
##### Using `supervisorctl`
`supervisorctl` provides a command-line interface to interact with the `supervisord` process:
- To **start a program**: `supervisorctl start your_program`
- To **stop a program**: `supervisorctl stop your_program`
- To **restart a program**: `supervisorctl restart your_program`
- To **view the status of all programs**: `supervisorctl status`
#### Web Interface
Supervisor can be configured to provide a web-based interface for monitoring and controlling processes. This is particularly useful for managing multiple processes across different servers.
```ini
[inet_http_server]
port=127.0.0.1:9001
username=user ; Basic auth username
password=secret ; Basic auth password
```
Adding the above section to your `supervisord.conf` enables the web interface.
#### Integration with Python and Linux
- **Custom Scripts**: Supervisor can manage any process, including Python scripts, making it ideal for Python developers needing to ensure their scripts run continuously.
- **Linux System Integration**: It integrates seamlessly with Linux systems, allowing Python applications to be started at boot and monitored.
- **Log Management**: Supervisor handles logging for all managed processes, simplifying the debugging and monitoring of Python applications.
#### Use Cases
Supervisor is extensively used in environments where Python applications need to be kept running in the background indefinitely, such as web applications, data processing scripts, or any long-running Python service.
#### Security Considerations
When using Supervisor, especially when exposing the web interface, ensure that access is properly secured to prevent unauthorized control over your processes.
Supervisor bridges the world of Python application development with Linux system administration, providing a robust toolset for managing long-running Python applications. Its ability to ensure that processes are always running in their desired state, coupled with powerful configuration and management capabilities, makes it an essential tool for developers and system administrators alike.

View File

@@ -0,0 +1,102 @@
For deep learning and neural network creation, `TensorFlow` stands as a cornerstone in the Python ecosystem. Developed by the Google Brain team, TensorFlow is an open-source library that allows developers to build and train complex machine learning models with ease. Its flexible architecture permits deployment across a variety of platforms, from servers to edge devices. Here's a concise reference guide for common use cases with `TensorFlow`, especially tailored for building and training machine learning models:
# `TensorFlow` Reference Guide
## Installation
```
pip install tensorflow
```
Note: For GPU support, use `tensorflow-gpu` instead, and ensure you have the necessary CUDA and cuDNN libraries installed.
## Basic Concepts
### Importing TensorFlow
```python
import tensorflow as tf
```
### Creating Tensors
```python
# Constant tensor
tensor = tf.constant([[1, 2], [3, 4]])
# Variable tensor
variable = tf.Variable([[1, 2], [3, 4]])
```
## Operations with Tensors
```python
# Addition
result = tf.add(tensor, tensor)
# Element-wise multiplication
result = tf.multiply(tensor, tensor)
# Matrix multiplication
result = tf.matmul(tensor, tensor)
```
## Building Neural Networks
### Defining a Sequential Model
```python
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)), # Input layer
tf.keras.layers.Dense(128, activation='relu'), # Hidden layer
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10) # Output layer
])
```
### Compiling the Model
```python
model.compile(optimizer='adam',
loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
```
## Training and Evaluating Models
### Training the Model
```python
model.fit(train_images, train_labels, epochs=5)
```
### Evaluating the Model
```python
model.evaluate(test_images, test_labels, verbose=2)
```
## Making Predictions
```python
probability_model = tf.keras.Sequential([
model,
tf.keras.layers.Softmax()
])
predictions = probability_model.predict(test_images)
```
## Saving and Loading Models
```python
# Save the entire model
model.save('my_model.h5')
# Load the model
new_model = tf.keras.models.load_model('my_model.h5')
```
## Working with Data
```python
# Load a dataset
mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Normalize the data
train_images, test_images = train_images / 255.0, test_images / 255.0
```
`TensorFlow` provides a comprehensive ecosystem of tools, libraries, and community resources that lets researchers push the state-of-the-art in ML, and developers easily build and deploy ML-powered applications. This guide covers the basics of TensorFlow for machine learning, including creating tensors, building and training neural network models, and saving/loading models. TensorFlow's capabilities are extensive and support a wide range of tasks beyond what's covered here, making it an essential tool for modern machine learning development.
```
TensorFlow's robust and scalable nature makes it suitable for both research and production, empowering users to transition seamlessly from concept to code, to training, to deployment.

View File

@@ -0,0 +1,110 @@
YAML, a recursive acronym for "YAML Ain't Markup Language," is a human-readable data serialization standard that can be used in conjunction with all programming languages and is often used for writing configuration files. While Python does not include a built-in library for YAML, the third-party library `PyYAML` is widely used for parsing and generating YAML files.
### PyYAML Usage Guide
#### Installation
PyYAML can be installed via pip. It's straightforward to add to your project:
```sh
pip install PyYAML
```
### Basic Operations
#### Loading YAML
PyYAML provides functions like `yaml.load()` and `yaml.safe_load()` to parse YAML from a string or file. The `safe_load()` function is recommended for loading untrusted input to avoid executing arbitrary Python objects.
##### Parsing YAML from a String
```python
import yaml
yaml_string = """
- hero:
name: John Doe
age: 30
- villain:
name: Jane Doe
age: 25
"""
data = yaml.safe_load(yaml_string)
print(data)
```
##### Reading YAML from a File
```python
with open('data.yaml', 'r') as file:
data = yaml.safe_load(file)
print(data)
```
#### Dumping YAML
To serialize Python objects into a YAML string or file, use `yaml.dump()`.
##### Converting Python Object to YAML String
```python
data = {
'hero': {'name': 'John Doe', 'age': 30},
'villain': {'name': 'Jane Doe', 'age': 25}
}
yaml_string = yaml.dump(data)
print(yaml_string)
```
##### Writing YAML Data to a File
```python
with open('output.yaml', 'w') as file:
yaml.dump(data, file)
```
### Advanced Usage
#### Custom Python Objects
PyYAML can serialize and deserialize custom Python objects through constructors and representers.
##### Serializing Custom Objects
```python
class Hero:
def __init__(self, name, age):
self.name = name
self.age = age
yaml.add_representer(Hero, lambda dumper, obj: dumper.represent_dict({'name': obj.name, 'age': obj.age}))
hero = Hero("John Doe", 30)
print(yaml.dump(hero))
```
##### Deserializing Custom Objects
```python
def hero_constructor(loader, node):
fields = loader.construct_mapping(node)
return Hero(**fields)
yaml.add_constructor('!Hero', hero_constructor)
yaml_string = "!Hero {name: John Doe, age: 30}"
hero = yaml.safe_load(yaml_string)
print(hero.name, hero.age)
```
### Use Cases
- **Configuration Files**: YAML's readability makes it ideal for configuration files used in applications and services.
- **Data Serialization**: YAML is useful for serializing complex data structures, such as trees or objects, in a format that can be edited by humans.
- **Infrastructure as Code (IaC)**: In DevOps, YAML is commonly used to define and manage infrastructure through code for cloud services, container orchestration (like Kubernetes), and automation tools.
### Best Practices
- **Use `safe_load()`**: Always prefer `safe_load()` over `load()` when parsing YAML to avoid executing arbitrary Python objects contained within the YAML file.
- **Keep It Simple**: Although YAML supports complex structures, maintaining simplicity in your YAML documents ensures they remain readable and maintainable.
- **Indentation**: Pay attention to indentation, as it is significant in YAML and a common source of errors.
PyYAML provides a powerful interface for working with YAML in Python, combining the flexibility of YAML with the expressiveness of Python. Whether you're configuring software, defining infrastructure, or simply need a readable format for data serialization, PyYAML equips you with the tools necessary to work effectively with YAML data.

104
tech_docs/python/ZeroMQ.md Normal file
View File

@@ -0,0 +1,104 @@
When focusing on seamless data exchange and communication between different applications or systems, especially in Linux environments where diverse applications often need to interoperate, `ZeroMQ` (also known as `ØMQ`, `0MQ`, or `zmq`) emerges as a highly powerful and useful library. ZeroMQ is a high-performance asynchronous messaging library, aimed at use in distributed or concurrent applications. It provides a message queue, but with a socket-style API.
### ZeroMQ Reference Guide
#### Installation
To start using ZeroMQ with Python, you need to install the Python binding for ZeroMQ, `pyzmq`:
```sh
pip install pyzmq
```
#### Basic Usage
##### Establishing a Simple Publisher-Subscriber Connection
ZeroMQ can operate in various communication patterns. One of the simplest is the publisher-subscriber pattern, where one or more publishers send messages to one or more subscribers.
###### Publisher
```python
import zmq
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5555")
while True:
# Wait for the next request from the client
message = "Hello World"
socket.send_string(message)
print(f"Sent: {message}")
```
###### Subscriber
```python
import zmq
context = zmq.Context()
# Create a new SUB socket and connect to the publisher
socket = context.socket(zmq.SUB)
socket.connect("tcp://localhost:5555")
# Subscribe to all messages
socket.setsockopt_string(zmq.SUBSCRIBE, '')
# Receive messages
while True:
message = socket.recv_string()
print(f"Received: {message}")
```
##### Request-Reply Pattern
Another common pattern is the request-reply pattern, where each query from a client is met with a response from the server.
###### Server
```python
import zmq
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5556")
while True:
# Wait for the next request from the client
message = socket.recv_string()
print(f"Received request: {message}")
# Send reply back to client
socket.send_string("World")
```
###### Client
```python
import zmq
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5556")
# Send a request
socket.send_string("Hello")
# Wait for the reply
message = socket.recv_string()
print(f"Received reply: {message}")
```
#### Advanced Features
- **Multiple Communication Patterns**: Beyond PUB-SUB and REQ-REP, ZeroMQ supports patterns like PUSH-PULL for load balancing and DEALER-ROUTER for complex routing of messages.
- **High Scalability**: ZeroMQ can scale to numerous nodes with minimal effort, enabling high-throughput and low-latency messaging across distributed systems.
- **Language and Platform Agnostic**: ZeroMQ can be used across many programming languages and platforms, making it ideal for heterogeneous environments.
#### Use Cases
- **Microservices Architecture**: Facilitating communication between microservices, whether they are on the same machine or distributed across a network.
- **Real-Time Data Processing**: Streaming data between systems for real-time processing, such as financial tick data for trading systems or sensor data for IoT applications.
- **Task Distribution**: Distributing tasks among a pool of worker processes for parallel processing.
#### Integration with Linux Systems
ZeroMQ is particularly well-suited for Linux systems involved in distributed computing and microservices, where various components may need to communicate efficiently and reliably. It complements Linux's robust networking and process management capabilities.
#### Security Considerations
While ZeroMQ supports encrypted connections using CurveZMQ for secure messaging, it's crucial to properly configure security features, especially when communicating over untrusted networks.
ZeroMQ offers a compelling approach to building complex, scalable, and efficient messaging solutions with Python on Linux, encapsulating powerful messaging patterns behind a simple API. This combination supports the development of highly responsive, distributed applications tailored to the specific demands of modern systems.

View File

@@ -0,0 +1,81 @@
For an advanced programmer new to Python, understanding Python's unique idioms, "Pythonic" ways, and potential gotchas is essential to write efficient, readable, and Pythonic code. Here's a compiled list of Pythonisms, idioms, and gotchas to look out for:
### Pythonisms and Idioms
1. **List Comprehensions**: Compact way to process all or part of the elements in a sequence and return a list with the results.
```python
squares = [x**2 for x in range(10)]
```
2. **Dictionary Comprehensions**: Similar to list comprehensions but for dictionaries.
```python
square_dict = {x: x**2 for x in range(10)}
```
3. **Using Underscore for Unused Variables**: It's a Pythonic way to indicate that a variable is intentionally unused.
```python
for _ in range(10):
do_something()
```
4. **Unpacking**: A way to unpack values from a sequence or iterable into variables.
```python
a, b, *rest = range(10)
```
5. **The Zen of Python**: Accessible by typing `import this`, it's a collection of Python's design principles.
6. **Using `enumerate()` for Loops**: To get both the index and the value of an item in a list.
```python
for index, value in enumerate(my_list):
print(index, value)
```
7. **Function Arguments**: Python supports default, keyword, positional, and arbitrary arguments.
```python
def func(a, b, c=5, *args, **kwargs):
pass
```
8. **The Walrus Operator (`:=`)**: Introduced in Python 3.8, allows you to assign values to variables as part of an expression.
```python
if (n := len(a)) > 10:
print(f"List is too long ({n} elements)")
```
### Gotchas
1. **Mutable Default Arguments**: Default argument values are evaluated only once at function definition time, which means that modifying a default argument will affect all subsequent calls to the function.
```python
def append_to(element, to=[]):
to.append(element)
return to
```
2. **Dynamic Typing**: While flexible, it can lead to unexpected behaviors if not carefully managed, especially when coming from statically-typed languages.
3. **Indentation**: Python uses indentation to define blocks, making code readability a part of the syntax. However, incorrect indentation can lead to `IndentationError` or unexpected behavior.
4. **`==` vs `is`**: `==` checks for equality, while `is` checks for identity. This distinction is crucial for understanding mutable and immutable objects.
```python
a = [1, 2, 3]
b = a
print(a == b) # True
print(a is b) # True
c = a.copy()
print(a == c) # True
print(a is c) # False
```
5. **Looping Pitfalls**: Modifying a list while iterating over it can lead to unexpected behavior. Use slicing or create a new list instead.
6. **Global vs Local Variables**: Variables declared inside a function are local unless explicitly declared global.
7. **Floating Point Arithmetic**: Like many languages, Python's floating-point numbers can have rounding errors.
```python
print(0.1 + 0.2 == 0.3) # False
```
8. **GIL (Global Interpreter Lock)**: Python's GIL means that only one thread can execute Python bytecodes at a time. This can be a gotcha for CPU-bound multithreading applications.
Understanding these Python-specific idioms and gotchas is crucial for leveraging Python's strengths while avoiding common pitfalls, ensuring your transition to Python is smooth and productive.

View File

@@ -0,0 +1,94 @@
For asynchronous programming in Python, `asyncio` stands out as a vital library. It provides a framework that revolves around writing concurrent code using the async/await syntax introduced in Python 3.5. `asyncio` is used for developing server-side applications, database connection libraries, and speed enhancements in web frameworks. Here's a concise reference guide for common use cases with `asyncio`, aiding in understanding and implementing asynchronous programming:
# `asyncio` Reference Guide
## Installation
`asyncio` is included in the standard library of Python 3.5 and later, so no additional installation is necessary.
## Basic Usage
### Importing asyncio
```python
import asyncio
```
### Running an Async Function
```python
async def main():
print('Hello')
await asyncio.sleep(1)
print('World')
# Python 3.7+
asyncio.run(main())
```
### Creating Tasks
```python
async def say_after(delay, what):
await asyncio.sleep(delay)
print(what)
async def main():
task1 = asyncio.create_task(say_after(1, 'hello'))
task2 = asyncio.create_task(say_after(2, 'world'))
print('Started tasks')
# Wait until both tasks are completed
await task1
await task2
asyncio.run(main())
```
### Waiting with `asyncio.gather`
```python
async def main():
# Schedule three calls concurrently
await asyncio.gather(
say_after(1, 'hello'),
say_after(2, 'world'),
say_after(3, '!')
)
asyncio.run(main())
```
## Working with I/O Operations
### Using asyncio for Asynchronous I/O
`asyncio` provides support for asynchronous I/O operations, such as reading and writing to files, network requests, and database operations, using the `aiohttp` library for HTTP requests, for example.
### Example: Asynchronous HTTP Requests
```python
import aiohttp
async def fetch(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def main():
html = await fetch('http://python.org')
print(html[:100])
asyncio.run(main())
```
## Event Loop
### Understanding the Event Loop
The event loop is the core of `asyncio` applications, handling execution of asynchronous tasks, callbacks, and I/O events. Use `asyncio.run()` to run the top-level entry point “main()” function.
### Managing the Event Loop Manually
For fine-grained control over the event loop, you can manage it manually, which is more common in complex applications and libraries.
```python
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.close()
```
`asyncio` makes it easier to write and manage asynchronous code, facilitating the development of efficient and scalable applications, especially for I/O-bound and high-level structured network code. It's a powerful tool in the Python standard library, enabling developers to leverage asynchronous programming patterns for improved performance.

View File

@@ -0,0 +1,70 @@
Exploring further into the realm of Python libraries dedicated to working with audio, `audioread` emerges as a simple yet powerful tool for decoding audio files across various formats. Unlike more complex libraries designed for audio manipulation or analysis, `audioread` focuses on providing a unified interface for reading audio data from files and streams. This makes it particularly useful for applications that need to support a wide range of audio file formats without delving into the intricacies of each format's decoding mechanisms.
### Audioread Complete Guide
#### Installation
```sh
pip install audioread
```
`audioread` abstracts over different backends, including native readers and FFmpeg, to provide a consistent API. Ensure you have FFmpeg installed for comprehensive format support.
### Basic Usage
#### Reading Audio Files
With `audioread`, you can easily load and decode audio files. The library delivers the audio data in a raw format, which can then be used for various purposes, such as analysis, conversion, or playback in a different context.
```python
import audioread
filename = 'path/to/your/audio.mp3'
with audioread.audio_open(filename) as f:
print(f'Channels: {f.channels}')
print(f'Sample rate: {f.samplerate}')
print(f'Duration: {f.duration} seconds')
for buf in f:
# `buf` contains a chunk of raw audio data
pass
```
### Common Use Cases
- **Audio Analysis**: While `audioread` itself doesn't provide analysis features, it's often the first step in a pipeline that involves processing or analyzing audio data. By reading audio files into a raw format, `audioread` facilitates the use of more specialized libraries for tasks like feature extraction, classification, or signal processing.
- **Format-Agnostic Audio Processing Applications**: When developing applications that need to handle audio input regardless of the file format, `audioread` provides a convenient way to abstract away the differences between formats. This is particularly useful for building format-agnostic tools like transcoders, audio analyzers, or media library organizers.
- **Educational Tools and Research**: In academic or research settings, where dealing with diverse datasets is common, `audioread` can simplify the workflow by allowing students and researchers to focus on the analysis rather than the preliminary step of reading audio files of different formats.
### Integration with Analysis Libraries
After loading audio with `audioread`, the raw audio data can be further processed or analyzed using libraries like `Librosa` for music and audio analysis, `numpy` for low-level signal processing, or `scipy` for more advanced signal analysis techniques. This flexibility makes `audioread` a valuable component in a broader audio processing pipeline.
```python
import numpy as np
import audioread
import librosa
# Example: Convert the raw audio bytes to a NumPy array and use Librosa for analysis
def read_and_analyze(filename):
with audioread.audio_open(filename) as f:
# Assuming the file is mono for simplicity
audio_data = b''.join(buf for buf in f)
audio_array = np.frombuffer(audio_data, dtype=np.int16)
# Further processing with Librosa or other libraries
tempo, beats = librosa.beat.beat_track(y=audio_array, sr=f.samplerate)
print(f"Tempo: {tempo}")
# Add further analysis as needed
filename = 'path/to/your/audio.mp3'
read_and_analyze(filename)
```
### Advantages and Limitations
- **Advantages**: `audioread` simplifies dealing with various audio formats, making it easier to build applications that are not limited by format-specific decoding requirements. Its ability to work with multiple backends, including FFmpeg, GStreamer, and the Mac OS X native libraries, ensures wide format coverage.
- **Limitations**: The library focuses solely on reading audio data and does not offer features for writing audio files, manipulating audio, or directly analyzing audio content. For such tasks, additional libraries would be needed, making `audioread` a part of a larger toolkit for audio processing.
`audioread` stands as a straightforward, backend-agnostic solution for reading audio files in Python, ideal for applications requiring basic audio input capabilities across a variety of formats. It serves as a foundational layer upon which more complex audio processing, analysis, and manipulation tasks can be built.

View File

@@ -0,0 +1,81 @@
For working with date and time in Python, `dateutil` is an incredibly useful library that extends the standard `datetime` module. It provides powerful extensions to the standard `datetime` module, available in the Python Standard Library. `dateutil` makes it easier to parse dates from strings, compute deltas, and handle timezone calculations, among other functionalities. Here's a concise reference guide for common use cases with `dateutil`:
# `dateutil` Reference Guide
## Installation
```
pip install python-dateutil
```
## Basic Usage
### Parsing Dates and Times from Strings
```python
from dateutil import parser
dt = parser.parse("2024-03-28 14:45:00")
print(dt)
```
### Relative Delta
Calculating differences between dates, adding or subtracting days, weeks, months, years, etc., in a way that considers the calendar's rules.
```python
from dateutil.relativedelta import relativedelta
# Add 1 month to a specific date
new_date = dt + relativedelta(months=+1)
print(new_date)
# Calculate the difference between two dates
date1 = parser.parse("2024-03-28")
date2 = parser.parse("2024-04-28")
delta = relativedelta(date2, date1)
print(f"Years: {delta.years}, Months: {delta.months}, Days: {delta.days}")
```
### Handling Time Zones
`dateutil` simplifies the work with time zones, converting between time zones, and making timezone-aware datetime objects.
```python
from dateutil import tz
# Get a timezone-aware datetime
eastern = tz.gettz('America/New_York')
dt_aware = dt.replace(tzinfo=tz.gettz('UTC')).astimezone(eastern)
print(dt_aware)
# Convert a timezone-aware datetime to another timezone
paris_tz = tz.gettz('Europe/Paris')
dt_in_paris = dt_aware.astimezone(paris_tz)
print(dt_in_paris)
```
### Recurring Events with `rrule`
`dateutil.rrule` provides support for calculating recurring events based on very flexible recurrence rules, similar to those used in calendars.
```python
from dateutil.rrule import rrule, MONTHLY
from datetime import datetime
# Define a rule for recurring events (e.g., monthly)
rule = rrule(MONTHLY, dtstart=datetime(2024, 1, 1), count=5)
# List all occurrences
for dt_occurrence in rule:
print(dt_occurrence)
```
## Advanced Features
### Working with Holidays
While `dateutil` itself does not directly provide holiday functionality, its powerful parsing and relative delta features can be used in combination with custom holiday definitions or other libraries to handle holidays in applications.
### Fuzzy Parsing
When parsing dates from sources with varying formats or including unwanted characters, `dateutil.parser.parse` supports fuzzy parsing to ignore unknown tokens.
```python
dt_fuzzy = parser.parse("Thursday, March 28, 2024 at 14:45:00", fuzzy=True)
print(dt_fuzzy)
```
`dateutil` enhances the datetime handling capabilities of Python programs, making it easier to perform complex date and time calculations, parsing, and formatting. Its wide range of functionalities addresses most of the common challenges encountered when working with date and time in Python, making it an essential tool for any Python developer dealing with time data.
Dateutil's comprehensive suite of features for dealing with temporal data simplifies many tasks that would otherwise require significant manual coding, especially when dealing with time zones, parsing dates from strings, and calculating date ranges or recurring events.

View File

@@ -0,0 +1,80 @@
For Python developers working within Linux environments who need to manage containerized applications, `Docker SDK for Python` provides a powerful toolset for automating Docker container management. This library allows you to interact with Docker from within your Python applications, scripts, or interactive Python sessions, enabling you to automate Docker workflows, manage containers, images, networks, and more.
### Docker SDK for Python Reference Guide
#### Installation
The Docker SDK for Python can be installed via pip. Ensure you have Docker installed and running on your Linux system before using the SDK.
```sh
pip install docker
```
#### Basic Usage
##### Connecting to Docker
First, import the Docker client in your Python script and establish a connection to the Docker daemon.
```python
import docker
client = docker.from_env()
```
##### Managing Containers
You can create, start, stop, and remove Docker containers easily with the SDK.
###### Creating and Starting a Container
```python
container = client.containers.run("ubuntu:latest", "echo hello world", detach=True)
print(container.logs())
```
###### Listing Active Containers
```python
for container in client.containers.list():
print(container.id)
```
###### Stopping and Removing a Container
```python
container.stop() # Stop the container
container.remove() # Remove the container
```
##### Managing Images
The SDK also allows you to manage Docker images, including pulling new images from Docker Hub and building images from a Dockerfile.
###### Pulling an Image
```python
image = client.images.pull("alpine:latest")
print(image.id)
```
###### Building an Image
Assuming you have a `Dockerfile` in your current directory:
```python
image, build_logs = client.images.build(path=".", tag="my-custom-image:latest")
for chunk in build_logs:
print(chunk)
```
#### Advanced Features
- **Volumes and Networks**: Manage Docker volumes and networks for persistent storage and inter-container communication.
- **Swarm Mode**: Interact with Docker in Swarm mode to manage a cluster of Docker engines.
- **Logs and Events**: Fetch logs from containers and listen to events from the Docker daemon.
#### Use Cases
- **Automated Testing Environments**: Spin up isolated environments for automated testing of applications.
- **CI/CD Pipelines**: Integrate Docker container management into continuous integration and continuous deployment pipelines for building, testing, and deploying applications.
- **Development Environments**: Automate the creation and management of development environments, ensuring consistency across development teams.
#### Integration with Linux Systems
The Docker SDK for Python is especially powerful on Linux, the primary platform for Docker deployments. It leverages Linux's containerization capabilities to provide a robust environment for developing, testing, and deploying containerized applications.
#### Security Considerations
When automating Docker operations, it's important to consider security implications:
- **Image Security**: Ensure that images are obtained from trusted sources and scanned for vulnerabilities.
- **Access Control**: Manage permissions carefully when interacting with the Docker daemon, especially in shared environments or when exposing Docker APIs over a network.
The Docker SDK for Python exemplifies the synergy between Python's versatility and Docker's containerization technology, providing developers with the tools needed to automate and streamline Docker workflows effectively. This integration opens up numerous possibilities for enhancing development practices, application deployment, and system management within Linux environments.

View File

@@ -0,0 +1,226 @@
# Economics 101 Presentation Generator
This project automates the creation of PowerPoint presentations for Economics 101, focusing on both macro and microeconomics topics. Utilizing the `python-pptx` library, this script generates presentations with predefined slides tailored for advanced economic studies.
## Getting Started
Follow these instructions to set up your project environment and run the presentation generation script.
### Prerequisites
- Python 3.6 or higher
- pip and venv
### Setting Up a Virtual Environment
Creating a virtual environment is essential for managing project-specific dependencies.
1. **Navigate to Your Project Directory:**
```bash
cd path/to/your/project
```
2. **Create the Virtual Environment:**
```bash
python3 -m venv env
```
3. **Activate the Environment:**
```bash
source env/bin/activate
```
### Installing Dependencies
After activating the virtual environment, install the required packages specified in `requirements.txt`.
```bash
pip install -r requirements.txt
```
Example `requirements.txt` content:
```
python-pptx==x.x.x
```
### Running the Script
Make the script executable:
```bash
chmod u+x Economics_101_Presentation.py
```
Run the script:
```bash
./Economics_101_Presentation.py
```
Or:
```bash
python3 Economics_101_Presentation.py
```
### Presentation Structure
The script generates a PowerPoint presentation including slides on:
- Introduction to Economics
- Macroeconomics Overview
- Microeconomics Overview
- Fundamental Concepts
- Macroeconomic Indicators
- Microeconomic Analysis
- Monetary Policy
- Fiscal Policy
- International Trade
- Conclusion
### Customizing the Presentation
To customize slide content and layout, modify the `PresentationCreator` class in your script:
```python
#!/usr/bin/env python3
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.chart import XL_CHART_TYPE
from pptx.chart.data import CategoryChartData
from pptx.dml.color import RGBColor
class PresentationCreator:
"""
A class to create PowerPoint presentations with predefined slide templates.
Attributes:
prs (Presentation): A python-pptx Presentation object.
title (str): The title of the presentation.
"""
def __init__(self, title="Presentation"):
"""
Initializes the PresentationCreator with a title for the presentation.
Args:
title (str): The title of the presentation.
"""
self.prs = Presentation()
self.title = title
def add_section_header_slide(self, title):
"""
Adds a section header slide with a given title.
Args:
title (str): The title of the section.
"""
layout = self.prs.slide_layouts[1] # Use Title and Content layout
slide = self.prs.slides.add_slide(layout)
slide.shapes.title.text = title
# Set a subtitle or a short description in the content placeholder if needed
def add_two_content_slide(self, title, left_content, right_content):
"""
Adds a slide with a title and two content sections side by side.
Args:
title (str): The title of the slide.
left_content (str): The content for the left section.
right_content (str): The content for the right section.
"""
layout = self.prs.slide_layouts[1] # Reuse Title and Content layout
slide = self.prs.slides.add_slide(layout)
slide.shapes.title.text = title
slide.placeholders[1].text = left_content # Placeholder for left content
# Note: For a true side-by-side layout, consider adding a textbox or customizing the layout
def add_comparison_slide(self, title, content):
"""
Adds a slide designed for comparisons.
Note: This example simplifies to a single content area for demonstration.
Args:
title (str): The title of the slide.
content (str): The content for comparison.
"""
layout = self.prs.slide_layouts[1] # Simplified use of Title and Content layout
slide = self.prs.slides.add_slide(layout)
slide.shapes.title.text = title
slide.placeholders[1].text = content # Placeholder for comparison content
def add_chart_slide(self, title, data):
"""
Adds a slide with a chart based on provided data.
Args:
title (str): The title of the slide.
data (dict): The data for the chart, including categories and series.
"""
slide_layout = self.prs.slide_layouts[5] # Use Title Only layout for more chart space
slide = self.prs.slides.add_slide(slide_layout)
slide.shapes.title.text = title
chart_data = CategoryChartData()
chart_data.categories = data["categories"]
for name, values in data["series"].items():
chart_data.add_series(name, values)
x, y, cx, cy = Inches(2), Inches(1.5), Inches(6), Inches(4.5)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
).chart
# Chart style and colors can be customized further
def save_presentation(self, filename="presentation.pptx"):
"""
Saves the presentation to a file.
Args:
filename (str): The filename or path to save the presentation.
"""
self.prs.save(filename)
# Example usage
if __name__ == "__main__":
creator = PresentationCreator("Economics 101 Presentation")
# Adding slides
creator.add_section_header_slide("Introduction to Economic Theories")
creator.add_two_content_slide("Supply and Demand", "Supply Side Analysis", "Demand Side Analysis")
creator.add_comparison_slide("Economic Models", "Comparative Analysis")
# Chart data example for economic indicators
chart_data_example = {
"categories": ['2015', '2016', '2017', '2018', '2019'],
"series": {
"GDP Growth": (2.5, 2.9, 2.3, 3.1, 2.2),
"Inflation Rate": (1.5, 1.8, 2.1, 1.9, 1.6),
}
}
creator.add_chart_slide("Economic Indicators Over Time", chart_data_example)
# Saving the presentation
creator.save_presentation("./Economics_101_Presentation.pptx")
```
## Project Structure
- `src/`: Source code
- `docs/`: Documentation
- `tests/`: Test scripts and data
- `data/`: Data files
- `env/`: Virtual environment (excluded from version control)
## Deactivating the Virtual Environment
```bash
deactivate
```
Ensure to replace `path/to/your/project` with your actual project path and adjust the `requirements.txt` as necessary for your dependencies.

View File

@@ -0,0 +1,150 @@
## Managing Environment Variables Securely in Python Projects
This guide provides a step-by-step approach to managing environment variables in Python projects on Ubuntu servers using `dotenv` for handling environment variables and GnuPG (GPG) for file encryption.
### Prerequisites
- Python 3 installed on Ubuntu Server
- Basic familiarity with terminal commands
### Contents
1. [Setting Up dotenv with .env Files](#1-setting-up-dotenv-with-env-files)
2. [Encrypting and Decrypting .env Files with GPG](#2-encrypting-and-decrypting-env-files-with-gpg)
3. [Automating Decryption in Python Scripts](#3-automating-decryption-in-python-scripts)
4. [Backing Up GPG Keys](#4-backing-up-gpg-keys)
5. [Basic GPG Commands](#5-basic-gpg-commands)
---
### 1. Setting Up `dotenv` with `.env` Files
`dotenv` is a module that loads environment variables from a `.env` file into `os.environ`. This section covers creating a `.env` file and integrating `dotenv` into your Python project.
#### Steps:
1. **Create a `.env` File**:
```bash
# Navigate to your project directory
cd /path/to/your/project
# Create a .env file
touch .env
# Add environment variables
echo "API_KEY=yourapikey123" >> .env
echo "DB_PASSWORD=hunter2" >> .env
```
2. **Install `python-dotenv`**:
```bash
pip3 install python-dotenv
```
3. **Modify Your Python Script**:
```python
import os
from dotenv import load_dotenv
load_dotenv() # Loads the .env file
api_key = os.getenv('API_KEY')
db_password = os.getenv('DB_PASSWORD')
# Your script continues here...
```
4. **Update `.gitignore`**:
```bash
echo ".env" >> .gitignore
```
### 2. Encrypting and Decrypting .env Files with GPG
GnuPG (GPG) is used for encrypting files, ensuring sensitive information like environment variables in `.env` files is secure.
#### Steps:
1. **Install GnuPG**:
```bash
sudo apt-get update
sudo apt-get install gnupg
```
2. **Encrypt the `.env` File**:
```bash
gpg --encrypt --recipient your_user_id .env
```
3. **Decrypt the `.env` File When Needed**:
```bash
gpg --output .env --decrypt .env.gpg
```
### 3. Automating Decryption in Python Scripts
Automate the decryption of the `.env` file at the start of your Python script for convenience while maintaining security.
#### Example Function:
```python
import subprocess
import os
from dotenv import load_dotenv
def decrypt_env_file():
subprocess.run(['gpg', '--quiet', '--batch', '--yes', '--decrypt', '--output', '.env', '.env.gpg'])
# Decrypt the .env file
decrypt_env_file()
# Load environment variables
load_dotenv()
# Your main script logic
# Delete the .env file securely after use
os.remove('.env')
```
### 4. Backing Up GPG Keys
Regular backups of GPG keys are essential to avoid losing access to encrypted data.
#### Steps:
1. **Export Your Private Key**:
```bash
gpg --export-secret-keys your_user_id > myprivatekey.asc
```
2. **Export Your Public Key**:
```bash
gpg --export your_user_id > mypublickey.asc
```
### 5. Basic GPG Commands
Familiarize yourself with basic GPG commands for managing your keys and encrypted files.
#### Common Commands:
- **List Keys**: `gpg --list-keys`, `gpg --list-secret-keys`
- **Import Key**: `gpg --import [file]`
- **Export Key**: `gpg --export -a [email/id] > public.key`
- **Delete Key**: `gpg --delete-key [email/id]`, `gpg --delete-secret-key [email/id]`
- **Encrypt File**: `gpg --encrypt --recipient [email/id] [file]`
- **Decrypt File**: `gpg --decrypt [file.gpg]`
---
By following this guide, you will be able to securely manage environment variables in your Python projects, leveraging `dotenv` for environment variable management and GnuPG for encryption, ensuring your sensitive data remains protected.

View File

@@ -0,0 +1,108 @@
`FFmpeg` is a comprehensive, cross-platform solution to record, convert, and stream audio and video. It includes `libavcodec`, a leading audio/video codec library that can be used with a vast array of codecs. While FFmpeg itself is not a Python library, its functionality can be accessed through various Python libraries designed to interact with it, such as `imageio-ffmpeg` for video reading/writing or `ffmpeg-python` for more general FFmpeg operations in Python scripts.
### FFmpeg Basic Usage
FFmpeg's command-line utility covers a wide range of functionalities. Here's how to perform some of the most common operations:
#### Installation
FFmpeg needs to be installed separately from your Python environment. It's available for Linux, Windows, and Mac. On Linux, you can typically install FFmpeg using your distribution's package manager:
```sh
sudo apt-get install ffmpeg # For Debian/Ubuntu
sudo yum install ffmpeg # For CentOS/RHEL
sudo dnf install ffmpeg # For Fedora
```
#### Converting Media Formats
Convert an audio or video file from one format to another:
```sh
ffmpeg -i input.avi output.mp4
```
#### Extracting Audio from Video
Extract the audio stream from a video without altering the original audio quality:
```sh
ffmpeg -i video.mp4 -vn -acodec copy output-audio.aac
```
#### Combining Audio and Video
Combine an audio and video file into one:
```sh
ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac output.mp4
```
#### Resizing Videos
Change the resolution of a video file:
```sh
ffmpeg -i input.mp4 -vf "scale=1280:720" output.mp4
```
#### Cutting Media Files
Extract a specific part of a video (or audio) by specifying the start time and duration:
```sh
ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:30 -c copy output.mp4
```
- `-ss`: Start time
- `-t`: Duration
#### Generating Thumbnails from Videos
Create a thumbnail image at a specific time point in the video:
```sh
ffmpeg -i video.mp4 -ss 00:01:00 -vframes 1 output.png
```
### Python Integration with FFmpeg
#### `ffmpeg-python` Overview
`ffmpeg-python` wraps FFmpeg and provides a Pythonic way to interact with it, allowing for complex filtering and more.
##### Installation
```sh
pip install ffmpeg-python
```
##### Basic Usage with `ffmpeg-python`
- **Reading Metadata**:
```python
import ffmpeg
probe = ffmpeg.probe('video.mp4')
video_streams = [stream for stream in probe['streams'] if stream['codec_type'] == 'video']
print(video_streams[0]['width'], video_streams[0]['height'])
```
- **Converting Media Formats**:
```python
import ffmpeg
ffmpeg.input('input.avi').output('output.mp4').run()
```
### Use Cases
- **Automated Video Production**: Generating video content programmatically, such as combining clips, adding watermarks, or creating video compilations.
- **Media Conversion Services**: Building services or tools for users to convert media files between different formats.
- **Content Analysis**: Extracting frames from videos for image analysis or machine learning purposes.
- **Streaming Applications**: Preparing media for streaming by adjusting formats, bitrates, or resolutions to suit different bandwidths and devices.
### FFmpeg's Versatility
FFmpeg's broad functionality makes it an invaluable tool for developers working on media-heavy applications. Through Python libraries like `ffmpeg-python`, these capabilities become easily accessible within Python scripts, blending FFmpeg's powerful media processing capabilities with Python's ease of use and flexibility. Whether for simple format conversions, complex video processing workflows, or real-time media streaming setups, integrating FFmpeg into Python projects unlocks a vast potential for automating and enhancing media manipulation tasks.

View File

@@ -0,0 +1,100 @@
For Python developers dealing with JSON data, whether for configuration files, data interchange between web services, or server responses, the built-in `json` library is an essential tool. It offers straightforward methods for encoding (serializing) Python objects into JSON strings and decoding (deserializing) JSON strings back into Python objects.
### JSON Library Usage Guide
#### Basic Operations
##### Encoding (Serialization)
Serializing Python objects into JSON strings is achieved with `json.dumps()` for creating a JSON-formatted string and `json.dump()` for writing JSON data directly to a file.
###### Convert Python Object to JSON String
```python
import json
data = {
"name": "John Doe",
"age": 30,
"isEmployed": True,
"skills": ["Python", "Machine Learning", "Web Development"]
}
json_string = json.dumps(data, indent=4)
print(json_string)
```
###### Write JSON Data to a File
```python
with open('data.json', 'w') as file:
json.dump(data, file, indent=4)
```
##### Decoding (Deserialization)
Deserializing JSON strings back into Python objects is done using `json.loads()` for parsing a JSON string and `json.load()` for reading JSON data from a file.
###### Convert JSON String to Python Object
```python
json_string = '{"name": "Jane Doe", "age": 25, "isEmployed": false}'
python_object = json.loads(json_string)
print(python_object)
```
###### Read JSON Data from a File
```python
with open('data.json', 'r') as file:
python_object = json.load(file)
print(python_object)
```
### Advanced Usage
#### Custom Object Encoding and Decoding
The `json` library can be extended to encode custom objects and decode JSON into specific Python classes.
##### Encoding Custom Objects
```python
class User:
def __init__(self, name, age):
self.name = name
self.age = age
def encode_user(obj):
if isinstance(obj, User):
return {"name": obj.name, "age": obj.age, "__User__": True}
return obj
user = User("John Doe", 30)
json_string = json.dumps(user, default=encode_user)
print(json_string)
```
##### Decoding JSON into Custom Python Objects
```python
def decode_user(dct):
if "__User__" in dct:
return User(dct["name"], dct["age"])
return dct
user = json.loads(json_string, object_hook=decode_user)
print(user.name, user.age)
```
### Use Cases
- **Configuration Files**: Use JSON files to store application configurations, making it easy to read and update settings.
- **Data Interchange**: JSON is a common format for data exchange between servers and web applications, particularly in RESTful APIs.
- **Storing and Retrieving Data**: JSON files can serve as a simple way to store data persistently and retrieve it for analysis or reporting.
### Best Practices
- **Handling Exceptions**: Always handle exceptions when parsing JSON to deal with malformed data gracefully.
- **Security Considerations**: Be cautious when deserializing JSON from untrusted sources to avoid security vulnerabilities.
- **Pretty Printing**: Use the `indent` parameter in `json.dumps()` or `json.dump()` for pretty printing, making JSON data easier to read and debug.
The built-in `json` library in Python simplifies the process of working with JSON data, providing powerful tools for serializing and deserializing data efficiently and securely. Whether you're building web applications, working with APIs, or simply need a lightweight format for storing data, the `json` library offers the necessary functionality to work with JSON data effectively.

View File

@@ -0,0 +1,115 @@
Let's focus on `Pillow` (PIL Fork), an extensive library used for opening, manipulating, and saving many different image file formats in Python. It's a continuation and an extension of the original Python Imaging Library (PIL) and is designed to be user-friendly and efficient for processing images.
### Pillow (PIL Fork) Complete Guide
#### Installation
```sh
pip install Pillow
```
### Basic Operations
#### Opening and Saving Images
```python
from PIL import Image
# Open an image
img = Image.open("path/to/image.jpg")
# Save the image in a different format
img.save("path/to/new_image.png")
```
#### Displaying Images
```python
img.show()
```
### Image Manipulation
#### Cropping Images
```python
# Define the box to crop (left, upper, right, lower)
box = (100, 100, 400, 400)
cropped_img = img.crop(box)
cropped_img.show()
```
#### Resizing Images
```python
# Resize the image to a new resolution
new_size = (300, 300)
resized_img = img.resize(new_size)
resized_img.show()
```
#### Rotating and Flipping Images
```python
# Rotate the image by 90 degrees
rotated_img = img.rotate(90)
rotated_img.show()
# Flip the image left to right
flipped_img = img.transpose(Image.FLIP_LEFT_RIGHT)
flipped_img.show()
```
#### Color Transforms
```python
# Convert to grayscale
gray_img = img.convert('L')
gray_img.show()
```
### Image Enhancements
Pillow provides the `ImageEnhance` module for adjusting color, brightness, contrast, and sharpness.
```python
from PIL import ImageEnhance
# Enhance the brightness
enhancer = ImageEnhance.Brightness(img)
bright_img = enhancer.enhance(2.0) # Increase brightness
bright_img.show()
# Enhance the contrast
enhancer = ImageEnhance.Contrast(img)
contrast_img = enhancer.enhance(2.0) # Increase contrast
contrast_img.show()
```
### Drawing on Images
Pillow allows you to draw on images directly using the `ImageDraw` module.
```python
from PIL import ImageDraw
draw = ImageDraw.Draw(img)
draw.line((0, 0) + img.size, fill=128)
draw.line((0, img.size[1], img.size[0], 0), fill=128)
```
### Working with Text
You can also use Pillow to add text to images.
```python
from PIL import ImageFont
draw.text((10, 25), "Hello World", fill="black")
# Using a TTF font
font = ImageFont.truetype("path/to/font.ttf", 15)
draw.text((10, 50), "Hello with font", font=font, fill="black")
```
### Potential Use Cases
- **Automating Image Editing**: Batch processing images for web optimization, creating thumbnails, or applying watermarks.
- **Data Visualization**: Generating dynamic images or charts for web or print.
- **Content Generation**: Creating images for social media posts, website banners, or marketing materials programmatically.
- **Machine Learning Preprocessing**: Preparing image datasets for training, including resizing, normalization, and augmentation.
Pillow's extensive set of features makes it an essential library for any project that involves image processing in Python. Whether you're developing a web application that handles user-uploaded images, creating graphics dynamically, or preprocessing data for machine learning models, Pillow provides the tools necessary to accomplish these tasks efficiently and effectively.

View File

@@ -0,0 +1,141 @@
For working with Excel documents using Python, the `openpyxl` library is a popular choice. It allows you to read and write Excel 2010 xlsx/xlsm/xltx/xltm files, providing capabilities to create new worksheets, change cell values, apply styles, and much more. Below is a concise reference guide for common use cases with `openpyxl`, formatted in Markdown syntax:
# `openpyxl` Reference Guide
## Installation
```
pip install openpyxl
```
## Creating and Saving Workbooks
```python
from openpyxl import Workbook
# Create a new workbook
wb = Workbook()
# Save the workbook
wb.save('path_to_file.xlsx')
```
## Opening Existing Workbooks
```python
from openpyxl import load_workbook
# Load an existing workbook
wb = load_workbook('path_to_existing_file.xlsx')
```
## Working with Sheets
```python
# Create a new sheet
ws1 = wb.create_sheet(title="Sheet1")
# Select a sheet
ws2 = wb["Sheet1"]
# List all sheet names
sheets = wb.sheetnames
# Remove a sheet
del wb["Sheet1"]
```
## Writing to Cells
```python
# Select a cell
cell = ws1['A1']
# Assign a value to the cell
ws1['A1'] = 'Hello'
ws1.cell(row=2, column=2, value="World")
# Iterate over rows and columns
for row in range(1, 5):
for col in range(1, 3):
ws1.cell(row=row, column=col, value=f'Row {row}, Col {col}')
```
## Reading from Cells
```python
# Read a cell's value
value = ws1['A1'].value
# Iterate over rows and columns
for row in ws1.iter_rows(min_row=1, max_col=2, max_row=2):
for cell in row:
print(cell.value)
```
## Applying Styles
```python
from openpyxl.styles import Font, Color, Alignment, Border, Side
# Create a font style
font = Font(name='Calibri', size=12, bold=True, italic=False, color="FF0000")
# Apply the font to a cell
ws1['A1'].font = font
# Create and apply border style
border = Border(left=Side(border_style="thin", color="000000"),
right=Side(border_style="thin", color="000000"),
top=Side(border_style="thin", color="000000"),
bottom=Side(border_style="thin", color="000000"))
ws1['A1'].border = border
# Apply alignment
alignment = Alignment(horizontal="center", vertical="center")
ws1['A1'].alignment = alignment
```
## Formulas
```python
# Assign a formula to a cell
ws1['A3'] = '=SUM(A1:A2)'
```
## Adding Charts
```python
from openpyxl.chart import BarChart, Reference
# Create data for the chart
for i in range(10):
ws1.append([i])
# Create a bar chart
chart = BarChart()
data = Reference(ws1, min_col=1, min_row=1, max_col=1, max_row=10)
chart.add_data(data, titles_from_data=True)
# Add the chart to the sheet
ws1.add_chart(chart, "E5")
```
## Working with Rows and Columns
```python
# Insert a row at the second position
ws1.insert_rows(2)
# Delete the third row
ws1.delete_rows(3)
# Insert a column
ws1.insert_cols(2)
# Delete a column
ws1.delete_cols(3)
# Set row height
ws1.row_dimensions[1].height = 20
# Set column width
ws1.column_dimensions['A'].width = 30
```
## Save Changes
```python
wb.save('path_to_file.xlsx')
```
This guide covers the basics of creating and manipulating Excel files with `openpyxl`, including operations on workbooks and worksheets, cell manipulation, applying styles, and adding charts. These capabilities allow for robust Excel document generation and editing directly from Python scripts.

297
tech_docs/python/pandas.md Normal file
View File

@@ -0,0 +1,297 @@
The `pandas` library is indispensable for data scientists, analysts, and anyone working with data in Python. It provides high-performance, easy-to-use data structures and data analysis tools. Below is a concise reference guide for common use cases with `pandas`, formatted in Markdown syntax:
# `pandas` Reference Guide
## Installation
```
pip install pandas
```
## Basic Concepts
### Importing pandas
```python
import pandas as pd
```
### Data Structures
- **Series**: One-dimensional array with labels.
- **DataFrame**: Two-dimensional, size-mutable, potentially heterogeneous tabular data with labeled axes.
## Creating DataFrames
```python
# From a dictionary
df = pd.DataFrame({
'A': [1, 2, 3],
'B': ['a', 'b', 'c']
})
# From a list of lists
df = pd.DataFrame([
[1, 'a'],
[2, 'b'],
[3, 'c']
], columns=['A', 'B'])
```
## Reading Data
```python
# Read from CSV
df = pd.read_csv('filename.csv')
# Read from Excel
df = pd.read_excel('filename.xlsx')
# Other formats include: read_sql, read_json, read_html, read_clipboard, read_pickle, etc.
```
## Data Inspection
```python
# View the first n rows (default 5)
df.head()
# View the last n rows (default 5)
df.tail()
# Data summary
df.info()
# Statistical summary for numerical columns
df.describe()
```
## Data Selection
```python
# Select a column
df['A']
# Select multiple columns
df[['A', 'B']]
# Select rows by position
df.iloc[0] # First row
df.iloc[0:5] # First five rows
# Select rows by label
df.loc[0] # Row with index label 0
df.loc[0:5] # Rows with index labels from 0 to 5, inclusive
```
## Data Manipulation
```python
# Add a new column
df['C'] = [10, 20, 30]
# Drop a column
df.drop('C', axis=1, inplace=True)
# Rename columns
df.rename(columns={'A': 'Alpha', 'B': 'Beta'}, inplace=True)
# Filter rows
filtered_df = df[df['Alpha'] > 1]
# Apply a function to a column
df['Alpha'] = df['Alpha'].apply(lambda x: x * 2)
```
## Handling Missing Data
```python
# Drop rows with any missing values
df.dropna()
# Fill missing values
df.fillna(value=0)
```
## Grouping and Aggregating
```python
# Group by a column and calculate mean
grouped_df = df.groupby('B').mean()
# Multiple aggregation functions
grouped_df = df.groupby('B').agg(['mean', 'sum'])
```
## Merging, Joining, and Concatenating
```python
# Concatenate DataFrames
pd.concat([df1, df2])
# Merge DataFrames
pd.merge(df1, df2, on='key')
# Join DataFrames
df1.join(df2, on='key')
```
## Saving Data
```python
# Write to CSV
df.to_csv('filename.csv')
# Write to Excel
df.to_excel('filename.xlsx')
# Other formats include: to_sql, to_json, to_html, to_clipboard, to_pickle, etc.
```
`pandas` is incredibly powerful for data cleaning, transformation, analysis, and visualization. This guide covers the basics, but the library's capabilities are vast and highly customizable to suit complex data manipulation and analysis tasks.
---
For high-performance, flexible, and expressive data structures designed to make working with "relational" or "labeled" data both easy and intuitive, `pandas` stands out as a crucial tool in Python data science libraries. It provides essential data manipulation capabilities akin to those found in programming languages like R. Heres a concise reference guide for common use cases with `pandas`, especially tailored for data manipulation and cleaning tasks:
# `pandas` Reference Guide
## Installation
```
pip install pandas
```
## Basic Concepts
### Importing pandas
```python
import pandas as pd
```
### Series and DataFrame
- **Series**: One-dimensional labeled array capable of holding data of any type.
- **DataFrame**: Two-dimensional labeled data structure with columns of potentially different types.
## Creating DataFrames
```python
# From a dictionary
df = pd.DataFrame({
'Column1': [1, 2, 3],
'Column2': ['a', 'b', 'c']
})
# From a list of dictionaries
df = pd.DataFrame([
{'Column1': 1, 'Column2': 'a'},
{'Column1': 2, 'Column2': 'b'}
])
# From a CSV file
df = pd.read_csv('filename.csv')
# From an Excel file
df = pd.read_excel('filename.xlsx')
```
## Basic DataFrame Operations
### Viewing Data
```python
# View the first 5 rows
df.head()
# View the last 5 rows
df.tail()
# Display the index, columns, and underlying numpy data
df.info()
```
### Data Selection
```python
# Select a single column
df['Column1']
# Select multiple columns
df[['Column1', 'Column2']]
# Select rows by position
df.iloc[0] # First row
# Select rows by label
df.loc[0] # Row with index label 0
```
### Data Filtering
```python
# Rows where Column1 is greater than 1
df[df['Column1'] > 1]
```
### Adding and Dropping Columns
```python
# Adding a new column
df['Column3'] = [4, 5, 6]
# Dropping a column
df.drop('Column3', axis=1, inplace=True)
```
### Renaming Columns
```python
df.rename(columns={'Column1': 'NewName1'}, inplace=True)
```
### Handling Missing Data
```python
# Drop rows with any missing values
df.dropna()
# Fill missing values
df.fillna(value=0)
```
## Data Manipulation
### Applying Functions
```python
# Apply a function to each item
df['Column1'] = df['Column1'].apply(lambda x: x * 2)
```
### Grouping Data
```python
# Group by column and calculate mean
df.groupby('Column1').mean()
```
### Merging and Concatenating
```python
# Concatenate DataFrames
pd.concat([df1, df2])
# Merge DataFrames
pd.merge(df1, df2, on='key_column')
```
### Aggregating Data
```python
df.agg({
'Column1': ['min', 'max', 'mean'],
'Column2': ['sum']
})
```
## Working with Time Series
```python
# Convert column to datetime
df['DateColumn'] = pd.to_datetime(df['DateColumn'])
# Set the DateTime column as the index
df.set_index('DateColumn', inplace=True)
# Resample and aggregate by month
df.resample('M').mean()
```
## Saving Data
```python
# Write to a CSV file
df.to_csv('new_file.csv')
# Write to an Excel file
df.to_excel('new_file.xlsx')
```
`pandas` is an indispensable tool for data munging/wrangling. It provides high-level abstractions for complex operations, simplifying tasks like data filtering, transformation, and aggregation. This guide covers foundational operations but barely scratches the surface of `pandas`' capabilities, which are vast and varied, extending well beyond these basics to support complex data manipulation and analysis tasks.
```
Given its powerful and flexible data manipulation capabilities, `pandas` is a cornerstone library for anyone working with data in Python, offering a depth of functionality that covers nearly every aspect of data analysis and manipulation.

99
tech_docs/python/perf.md Normal file
View File

@@ -0,0 +1,99 @@
In the domain of system monitoring and performance analysis on Linux, the `psutil` library emerges as a critical tool, enabling Python applications to gather system utilization data and perform process management. However, for developers and system administrators seeking to delve deeper into system performance analysis and diagnostics, `perf` stands out as a key tool within the Linux ecosystem, though not directly accessible via a standard Python library.
`perf` is a powerful performance counter for Linux, offering access to a wide range of hardware performance counters, such as CPU cycles, instructions per cycle, cache hits and misses, and more. While `perf` itself is a command-line tool rather than a Python library, Python can interact with `perf` data or use it as part of a larger Python-driven monitoring or performance analysis solution.
Since there isn't a direct Python library for `perf`, the integration usually involves using Python to automate `perf` command execution, parse its output, and possibly visualize the data. This approach leverages Python's capabilities for data manipulation and analysis to work with the rich, low-level performance data that `perf` provides.
### Automating `perf` with Python
#### Running `perf` Commands from Python
You can use Python's `subprocess` module to run `perf` commands and capture their output for analysis:
```python
import subprocess
def run_perf_command(command):
result = subprocess.run(["perf", *command.split()], capture_output=True, text=True)
if result.returncode == 0:
return result.stdout
else:
raise Exception(f"perf command failed: {result.stderr}")
# Example usage
output = run_perf_command("stat -e cycles,instructions ls")
print(output)
```
#### Parsing `perf` Output
Once you've captured the output from `perf`, you can use Python to parse and analyze the data. The parsing complexity depends on the specific `perf` command and the data you're interested in:
```python
def parse_perf_stat_output(output):
metrics = {}
for line in output.splitlines():
if "cycles" in line or "instructions" in line:
parts = line.split()
metrics[parts[1]] = parts[0]
return metrics
# Example usage
metrics = parse_perf_stat_output(output)
print(metrics)
```
#### Visualizing `perf` Data
With the parsed data, you can utilize libraries such as `matplotlib` or `pandas` for visualization:
```python
import matplotlib.pyplot as plt
def plot_metrics(metrics):
labels = metrics.keys()
values = [int(v.replace(',', '')) for v in metrics.values()]
plt.bar(labels, values)
plt.xlabel('Metric')
plt.ylabel('Value')
plt.title('Performance Metrics')
plt.show()
# Example usage
plot_metrics(metrics)
```
### Use Cases
- **Performance Analysis**: Automated performance regression testing or benchmarking.
- **System Monitoring**: Building custom monitoring solutions that require access to hardware performance counters.
- **Profiling**: Identifying performance bottlenecks in applications.
### Considerations
- **Permissions**: Running `perf` might require elevated permissions depending on the counters accessed.
- **Complexity**: The `perf` tool can generate vast amounts of data; focus on specific metrics relevant to your analysis to manage complexity.
While not a straightforward Python library integration, the combination of Python and `perf` on Linux unlocks powerful capabilities for performance analysis and monitoring. It exemplifies Python's versatility in system-level integration and automation, providing developers and system administrators with tools to analyze and improve system and application performance.
---
Expanding on the use cases for integrating Python with `perf` and other system tools provides a clearer picture of how these technologies can work together to optimize and monitor Linux systems and applications. Let's delve deeper into each use case:
### Performance Analysis: Automated Performance Regression Testing or Benchmarking
- **Automated Regression Testing**: In continuous integration (CI) pipelines, Python scripts can automate the execution of `perf` before and after code changes to detect performance regressions. By comparing key performance metrics such as CPU cycles, cache misses, or context switches, developers can be alerted to changes that negatively impact performance, even if those changes pass functional tests.
- **Benchmarking**: Python can automate benchmark tests across different system configurations or software versions, collecting performance data with `perf`. This is particularly useful for comparing the performance impact of software updates, hardware upgrades, or configuration changes. The collected data can be analyzed and visualized with Python, helping to understand the performance characteristics of the system under various conditions.
### System Monitoring: Building Custom Monitoring Solutions
- **Real-time Performance Monitoring**: Leveraging Python and `perf`, developers can create custom dashboards that display real-time data on system performance, including CPU utilization, memory bandwidth, and I/O statistics. This allows system administrators to monitor the health and performance of servers and applications, identify trends over time, and make informed decisions about scaling and optimization.
- **Alerting Systems**: By continuously analyzing performance data, Python scripts can detect anomalies or threshold breaches (e.g., CPU usage consistently above 90%) and trigger alerts. These alerts can be integrated into existing monitoring frameworks or messaging platforms, ensuring that teams are promptly notified of potential issues.
### Profiling: Identifying Performance Bottlenecks in Applications
- **Application Profiling**: Python can orchestrate detailed profiling sessions using `perf` to collect data on how applications utilize system resources. This can reveal hotspots in the code—sections that consume disproportionate amounts of CPU time or cause frequent cache misses. Developers can use this information to focus their optimization efforts where they will have the most impact.
- **Comparative Analysis**: For applications running in different environments or on different hardware, Python scripts can compare performance profiles to identify environment-specific bottlenecks or validate hardware choices. For example, profiling an application on both HDD- and SSD-based systems could quantify the performance benefits of faster storage for specific workloads.
- **Visualizing Profiling Data**: After collecting profiling data, Python's rich ecosystem of data visualization libraries can be employed to create intuitive visualizations. This could include heatmaps of CPU usage over time, call graphs showing function call frequencies and durations, or comparative bar charts highlighting performance before and after optimizations.
Integrating Python with `perf` and other Linux performance tools opens up a wide array of possibilities for deep system and application analysis. This approach not only aids in detecting and diagnosing performance issues but also provides a foundation for continuous performance improvement. Custom tools built with Python can adapt to specific project needs, providing insights that generic tools may not capture, and thus becoming an invaluable part of the performance optimization toolkit.

115
tech_docs/python/pillow2.md Normal file
View File

@@ -0,0 +1,115 @@
Let's focus on `Pillow` (PIL Fork), an extensive library used for opening, manipulating, and saving many different image file formats in Python. It's a continuation and an extension of the original Python Imaging Library (PIL) and is designed to be user-friendly and efficient for processing images.
### Pillow (PIL Fork) Complete Guide
#### Installation
```sh
pip install Pillow
```
### Basic Operations
#### Opening and Saving Images
```python
from PIL import Image
# Open an image
img = Image.open("path/to/image.jpg")
# Save the image in a different format
img.save("path/to/new_image.png")
```
#### Displaying Images
```python
img.show()
```
### Image Manipulation
#### Cropping Images
```python
# Define the box to crop (left, upper, right, lower)
box = (100, 100, 400, 400)
cropped_img = img.crop(box)
cropped_img.show()
```
#### Resizing Images
```python
# Resize the image to a new resolution
new_size = (300, 300)
resized_img = img.resize(new_size)
resized_img.show()
```
#### Rotating and Flipping Images
```python
# Rotate the image by 90 degrees
rotated_img = img.rotate(90)
rotated_img.show()
# Flip the image left to right
flipped_img = img.transpose(Image.FLIP_LEFT_RIGHT)
flipped_img.show()
```
#### Color Transforms
```python
# Convert to grayscale
gray_img = img.convert('L')
gray_img.show()
```
### Image Enhancements
Pillow provides the `ImageEnhance` module for adjusting color, brightness, contrast, and sharpness.
```python
from PIL import ImageEnhance
# Enhance the brightness
enhancer = ImageEnhance.Brightness(img)
bright_img = enhancer.enhance(2.0) # Increase brightness
bright_img.show()
# Enhance the contrast
enhancer = ImageEnhance.Contrast(img)
contrast_img = enhancer.enhance(2.0) # Increase contrast
contrast_img.show()
```
### Drawing on Images
Pillow allows you to draw on images directly using the `ImageDraw` module.
```python
from PIL import ImageDraw
draw = ImageDraw.Draw(img)
draw.line((0, 0) + img.size, fill=128)
draw.line((0, img.size[1], img.size[0], 0), fill=128)
```
### Working with Text
You can also use Pillow to add text to images.
```python
from PIL import ImageFont
draw.text((10, 25), "Hello World", fill="black")
# Using a TTF font
font = ImageFont.truetype("path/to/font.ttf", 15)
draw.text((10, 50), "Hello with font", font=font, fill="black")
```
### Potential Use Cases
- **Automating Image Editing**: Batch processing images for web optimization, creating thumbnails, or applying watermarks.
- **Data Visualization**: Generating dynamic images or charts for web or print.
- **Content Generation**: Creating images for social media posts, website banners, or marketing materials programmatically.
- **Machine Learning Preprocessing**: Preparing image datasets for training, including resizing, normalization, and augmentation.
Pillow's extensive set of features makes it an essential library for any project that involves image processing in Python. Whether you're developing a web application that handles user-uploaded images, creating graphics dynamically, or preprocessing data for machine learning models, Pillow provides the tools necessary to accomplish these tasks efficiently and effectively.

View File

@@ -0,0 +1,91 @@
In the domain of Python and Linux, especially when focusing on system-level programming and process management, `psutil` (Python System and Process Utilities) is an incredibly useful library. It provides an interface for retrieving information on system utilization (CPU, memory, disks, network, sensors) and managing processes. It's a cross-platform library designed to work on Windows, Linux, macOS, FreeBSD, OpenBSD, NetBSD, and Sun Solaris, making it highly versatile for system monitoring and management scripts.
### `psutil` Reference Guide
#### Installation
```
pip install psutil
```
#### Basic Usage
##### System Information
`psutil` allows you to monitor various system metrics easily.
###### CPU
```python
import psutil
# Get the number of logical CPUs in the system
print("Logical CPUs:", psutil.cpu_count())
# Get the CPU utilization percentage
print("CPU Usage (%):", psutil.cpu_percent(interval=1))
```
###### Memory
```python
# Get memory usage details
memory = psutil.virtual_memory()
print("Total Memory:", memory.total)
print("Available Memory:", memory.available)
```
###### Disks
```python
# List all mounted disk partitions
partitions = psutil.disk_partitions()
for p in partitions:
print("Device:", p.device)
# Get disk usage stats
usage = psutil.disk_usage('/')
print("Disk Usage:", usage.percent, "%")
```
###### Network
```python
# Get network I/O statistics
net_io = psutil.net_io_counters()
print("Bytes Sent:", net_io.bytes_sent)
print("Bytes Received:", net_io.bytes_recv)
```
##### Managing Processes
`psutil` is also powerful for managing and inspecting processes.
###### List All Processes
```python
# Iterate over all running processes
for proc in psutil.process_iter(['pid', 'name']):
print(proc.info)
```
###### Kill a Process
```python
# Terminate a process by PID
pid = 12345 # Example PID
process = psutil.Process(pid)
process.terminate() # Or process.kill()
```
###### Process Information
```python
# Get detailed info about a specific process
proc = psutil.Process(pid)
print("Process Name:", proc.name())
print("Process Status:", proc.status())
```
#### Cross-platform Compatibility
One of the significant advantages of `psutil` is its cross-platform nature, allowing scripts written on one OS to be easily ported to another with minimal changes, making it ideal for developing system monitoring tools and utilities that need to run across different environments.
#### Advanced Features
- **Sensors**: Access hardware temperature sensors (e.g., CPU temp).
- **Battery**: Check battery status and percentage.
- **Users**: List users currently logged in.
#### Security Considerations
When using `psutil` for managing processes, especially killing or altering system processes, ensure your scripts are executed with appropriate permissions to avoid unintended system disruptions. Also, consider the security implications of deploying scripts that monitor system and user activity.
`psutil` bridges the gap between Python scripting and system management tasks, offering a rich set of functionalities for interacting with and monitoring system resources. It's a go-to library for system administrators, developers working on system monitoring tools, or anyone interested in programmatic access to system and process information within Python scripts.

View File

@@ -0,0 +1,89 @@
In the sphere of Python and Linux integration, especially for system and network monitoring, `pyroute2` is a highly significant library. Pyroute2 is a pure Python library that provides functionality for network configuration, similar to the `iproute2` utility in Linux, which is responsible for network device management, tunneling, routing, and more. This makes pyroute2 particularly valuable for developers and system administrators looking to automate network configuration and querying directly from Python scripts.
### Pyroute2 Reference Guide
#### Installation
To install pyroute2, you can use pip:
```
pip install pyroute2
```
#### Basic Usage
##### Listing Network Interfaces
```python
from pyroute2 import IPRoute
ip = IPRoute()
# List all interfaces
interfaces = ip.get_links()
for interface in interfaces:
print(interface.get_attr('IFLA_IFNAME'))
ip.close()
```
##### Changing Interface State
```python
from pyroute2 import IPRoute
ip = IPRoute()
# Bring an interface down
index = ip.link_lookup(ifname='eth0')[0]
ip.link('set', index=index, state='down')
# Bring an interface up
ip.link('set', index=index, state='up')
ip.close()
```
##### Managing IP Addresses
```python
from pyroute2 import IPRoute
ip = IPRoute()
# Add an IP address to an interface
index = ip.link_lookup(ifname='eth0')[0]
ip.addr('add', index=index, address='192.168.1.2', mask=24)
# Remove an IP address from an interface
ip.addr('delete', index=index, address='192.168.1.2', mask=24)
ip.close()
```
##### Working with Routes
```python
from pyroute2 import IPRoute
ip = IPRoute()
# Add a route
ip.route('add', dst='0.0.0.0/0', gateway='192.168.1.1')
# Delete a route
ip.route('delete', dst='0.0.0.0/0', gateway='192.168.1.1')
ip.close()
```
#### Advanced Features
- **Monitoring Network Events**: Pyroute2 can be used to monitor system-wide network events, such as interface changes, IP address additions/removals, and more, in real time.
- **Network Namespaces Support**: Manage network namespaces, allowing for isolated networking environments within the same Linux instance.
- **Wireless Configuration**: Some capabilities for managing wireless interfaces and settings, though these may be limited compared to dedicated wireless tools.
#### Use Cases
Pyroute2 is particularly useful for:
- Automating network configuration as part of deployment scripts or infrastructure provisioning.
- Building custom network management and monitoring tools tailored to specific requirements.
- Integrating network configuration changes into Python-based applications or services.
#### Security Considerations
When automating network configurations, especially in production environments, it's crucial to implement error checking and validation to prevent misconfigurations that could lead to network outages or vulnerabilities.
Pyroute2 brings the extensive capabilities of Linux's networking stack into the Python ecosystem, offering a powerful tool for network automation and monitoring. By providing direct access to complex network configurations through Python scripts, it enables a higher degree of automation and integration for system and network administrators, as well as developers working on network-intensive Python applications.

100
tech_docs/python/pytest.md Normal file
View File

@@ -0,0 +1,100 @@
`pytest` is an essential Python library for developers focused on testing their code. It provides a powerful yet simple framework for writing and running tests, supporting unit tests, functional tests, and more complex test scenarios. `pytest` stands out for its ease of use, detailed assertions, fixtures for setup and teardown, and support for parameterized testing, among other features. Here's a concise reference guide for common use cases with `pytest`:
# `pytest` Reference Guide
## Installation
```
pip install pytest
```
## Basic Usage
### Writing Your First Test
Tests are Python functions that start with `test_`. Place your test functions in files named `test_*.py` or `*_test.py`.
```python
# content of test_sample.py
def func(x):
return x + 1
def test_answer():
assert func(3) == 4
```
### Running Tests
Navigate to the directory containing your test file and run:
```
pytest
```
`pytest` will discover and run all tests following its naming conventions.
## Assertions
`pytest` uses standard Python `assert` for verifying expectations and values in tests, providing detailed output on assertion failures.
```python
def test_check_equal():
assert func(4) == 5
```
## Fixtures
Fixtures provide a powerful mechanism for setup and teardown operations, creating test data, and configuring the test environment.
```python
import pytest
@pytest.fixture
def input_value():
return 38
def test_divisible_by_2(input_value):
assert input_value % 2 == 0
```
## Parameterized Tests
Parameterize tests to run the same test function with different inputs.
```python
@pytest.mark.parametrize("test_input,expected", [(1, 2), (2, 3), (3, 4)])
def test_eval(test_input, expected):
assert func(test_input) == expected
```
## Handling Exceptions
Test that a certain exception is raised using the `pytest.raises` context manager.
```python
def test_exception():
with pytest.raises(ValueError):
raise ValueError
```
## Skipping Tests
Skip a test function or expect it to fail.
```python
@pytest.mark.skip(reason="not implemented yet")
def test_to_skip():
pass
@pytest.mark.xfail
def test_might_fail():
pass
```
## Using Plugins and Integrations
`pytest` supports numerous plugins for integration with other tools and extending its functionality, such as `pytest-django` for Django projects, `pytest-asyncio` for testing asyncio code, and many more.
## Configuration
Use a `pytest.ini`, `tox.ini`, or `pyproject.toml` file to configure `pytest` behavior, like custom markers, test paths, and plugin configurations.
## Advanced Usage
`pytest` offers a broad spectrum of advanced features, including but not limited to test fixtures scopes, using mocks, and integrating with continuous integration (CI) tools.
## Continuous Integration
`pytest` can easily integrate with CI tools like Jenkins, GitHub Actions, or GitLab CI, running your test suite on every push or pull request automatically.
`pytest` simplifies and accelerates the testing process, encouraging best practices in test writing and execution. Its extensive documentation and active community make it a go-to choice for Python developers looking to ensure their code meets the highest standards of quality.
`pytest` is favored in the Python community for its rich ecosystem, simplicity, and ability to handle not just simple unit tests but also complex functional testing for applications and libraries.

View File

@@ -0,0 +1,185 @@
Certainly! Let's streamline the enhancements to focus on a few key visual improvements that will make your document look neat and professional without being too elaborate:
### 1. **Apply Basic Styles to Headings**
This will help distinguish different sections and make your document more readable.
```python
from docx.shared import Pt
# Apply a simple style to headings
for level in range(1, 4):
heading_style = doc.styles['Heading {}'.format(level)]
heading_style.font.name = 'Arial'
heading_style.font.size = Pt(14 if level == 1 else 12 if level == 2 else 10)
heading_style.font.bold = True
```
### 2. **Use Tables for Ingredients**
Organizing ingredients in a table makes it easier for the reader to understand the quantities needed.
```python
# Create a simple table for ingredients
def add_ingredients_table(doc, ingredients):
table = doc.add_table(rows=1, cols=2)
table.style = 'Table Grid'
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Ingredient'
hdr_cells[1].text = 'Quantity'
for ingredient, quantity in ingredients:
row = table.add_row().cells
row[0].text = ingredient
row[1].text = quantity
# Example usage:
add_ingredients_table(doc, [
("Apple Cider Vinegar", "1 cup"),
("Sugar or Honey", "1 tablespoon"),
# Add more ingredients as needed...
])
```
### 3. **Insert Section Breaks for Clarity**
Instead of page breaks, using section breaks can help organize the document without adding too many pages.
```python
# Add a section break
def add_section_break(doc):
doc.add_section()
```
These changes apply a clean and functional style to your document, improving readability and organization without overcomplicating the design. You can adjust the specifics like font sizes and table styles according to your preferences and the overall length of your document.
---
## Reference guide for common use cases with the `python-docx` library:
# `python-docx` Reference Guide
## Installation
```
pip install python-docx
```
## Creating and Saving Documents
```python
from docx import Document
# Create a new document
doc = Document()
# Save the document
doc.save('path_to_file.docx')
```
## Opening Existing Documents
```python
doc = Document('path_to_existing_file.docx')
```
## Adding Paragraphs
```python
# Add a paragraph
paragraph = doc.add_paragraph('Your text here.')
# Add a paragraph with a predefined style
doc.add_paragraph('Styled text', style='Heading1')
```
## Adding Headings
```python
# Add a heading level 1
doc.add_heading('Heading Level 1', level=1)
# Add a heading level 2
doc.add_heading('Heading Level 2', level=2)
```
## Working with Runs
```python
# Add a run to an existing paragraph
run = paragraph.add_run(' Additional text')
# Apply bold and italic
run.bold = True
run.italic = True
```
## Inserting Images
```python
# Add an image
doc.add_picture('path_to_image.jpg', width=docx.shared.Inches(1), height=docx.shared.Cm(4))
```
## Creating Tables
```python
# Create a table with 2 rows and 2 columns
table = doc.add_table(rows=2, cols=2)
# Add data to cells
table.cell(0, 0).text = 'First cell'
table.cell(0, 1).text = 'Second cell'
```
## Adding Lists
```python
# Add a bulleted list
doc.add_paragraph('First item', style='ListBullet')
doc.add_paragraph('Second item', style='ListBullet')
# Add a numbered list
doc.add_paragraph('First item', style='ListNumber')
doc.add_paragraph('Second item', style='ListNumber')
```
## Using Headers and Footers
```python
# Access the header of the document
header = doc.sections[0].header
header.paragraphs[0].text = "Header text"
# Access the footer of the document
footer = doc.sections[0].footer
footer.paragraphs[0].text = "Footer text"
```
## Applying Styles
```python
# Apply a style to a paragraph
paragraph.style = 'Title'
# Apply a style to a run
run.style = 'QuoteChar'
```
## Page Layout
```python
# Change page orientation to landscape
section = doc.sections[0]
section.orientation = WD_ORIENT.LANDSCAPE
# Set page width and height
section.page_width = docx.shared.Mm(297)
section.page_height = docx.shared.Mm(210)
```
## Adding Footnotes and Endnotes
```python
# Add a footnote
footnote = doc.add_footnote()
footnote.add_paragraph('Footnote text')
# Add an endnote
endnote = doc.add_endnote()
endnote.add_paragraph('Endnote text')
```
## Adding Bookmarks and Hyperlinks
```python
# Add a hyperlink
doc.add_paragraph().add_hyperlink('OpenAI', 'https://www.openai.com', 'Hyperlink')
```
Remember to save your document after making changes:
```python
doc.save('path_to_file.docx')
```

View File

@@ -0,0 +1,107 @@
For working with PowerPoint presentations in Python, the `python-pptx` library is a go-to solution. It allows for creating new presentations or modifying existing ones, adding slides, manipulating text, inserting images, and more. Heres a concise reference guide covering common use cases with `python-pptx`, formatted in Markdown syntax:
# `python-pptx` Reference Guide
## Installation
```
pip install python-pptx
```
## Creating a New Presentation
```python
from pptx import Presentation
# Create a new Presentation object
prs = Presentation()
# Save the presentation
prs.save('new_presentation.pptx')
```
## Opening Existing Presentations
```python
# Load an existing presentation
prs = Presentation('path_to_existing_file.pptx')
```
## Adding Slides
```python
from pptx.util import Inches
# Add a slide with a title and content layout
slide_layout = prs.slide_layouts[1] # 0 is the layout for a title slide, 1 for title and content
slide = prs.slides.add_slide(slide_layout)
# Set title and content
title = slide.shapes.title
title.text = "This is the slide title"
content = slide.placeholders[1]
content.text = "This is the slide content"
```
## Adding Text to Slides
```python
# Add a textbox to a slide
x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5)
textbox = slide.shapes.add_textbox(x, y, cx, cy)
# Add text to the textbox
text_frame = textbox.text_frame
p = text_frame.add_paragraph()
p.text = "This is a textbox"
```
## Inserting Images
```python
# Add an image to a slide
img_path = 'path_to_image.jpg'
x, y = Inches(1), Inches(1)
slide.shapes.add_picture(img_path, x, y, width=Inches(4))
```
## Working with Shapes
```python
# Add a shape (e.g., rectangle)
x, y, cx, cy = Inches(3), Inches(3), Inches(2), Inches(1)
shape = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE, x, y, cx, cy
)
shape.fill.solid()
shape.fill.fore_color.rgb = RGBColor(255, 0, 0) # Red fill
shape.text = "A rectangle"
```
## Applying Styles to Text
```python
from pptx.dml.color import RGBColor
from pptx.util import Pt
# Set font size, bold, italic, and color
run = p.add_run()
run.text = "Stylized text"
font = run.font
font.size = Pt(20)
font.bold = True
font.italic = True
font.color.rgb = RGBColor(0, 102, 204) # Blue
```
## Managing Slide Layouts
```python
# Iterate through available slide layouts
for layout in prs.slide_layouts:
print(layout.name)
# Use a specific layout for a new slide
custom_layout = prs.slide_layouts[2] # Choosing a specific layout
slide = prs.slides.add_slide(custom_layout)
```
## Saving Presentations
```python
# Save changes to the presentation
prs.save('modified_presentation.pptx')
```
This guide covers foundational operations like creating and saving presentations, adding slides with various layouts, inserting and formatting text, adding images, and working with shapes. The `python-pptx` library offers extensive functionalities for PowerPoint manipulation, making it a powerful tool for automating presentation creation and editing tasks in Python.

View File

@@ -0,0 +1,70 @@
# Installing Python 3 on macOS with Homebrew
This guide provides a concise overview of installing Python 3 on an M2 MacBook Air using Homebrew, and setting up `pip` and `venv` for a robust Python development environment.
## Prerequisites
- Access to Terminal
- Connectivity to the Internet
## Step 1: Install Homebrew
Open Terminal and run:
```sh
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
## Step 2: Install Python 3
Install Python 3, which includes `pip3` and `venv`, by running:
```sh
brew install python
```
## Step 3: Verify Python 3 Installation
Check the Python version to verify installation:
```sh
python3 --version
```
## Step 4: Verify pip Installation
Ensure `pip3` is installed:
```sh
pip3 --version
```
## Step 5: Setup Virtual Environments with venv
Navigate to your project directory:
```sh
cd path/to/your/project
```
Create a virtual environment:
```sh
python3 -m venv venv
```
Activate the virtual environment:
```sh
source venv/bin/activate
```
## Step 6: Deactivate Virtual Environment
To exit the virtual environment:
```sh
deactivate
```
This guide ensures you have a functional Python environment on macOS, ideal for development activities.

View File

@@ -0,0 +1,112 @@
Certainly! Here's a refactored and well-organized guide with additional details, tailored to setting up a Python project using a virtual environment for managing YouTube Music playlist data. This will be provided in Markdown format, ideal for a README file or documentation purposes.
### Markdown Content for the Guide
Here is how you can structure your guide in markdown, including script execution, package management, and best practices:
```markdown
# Python Project Setup Guide
This guide outlines the steps for setting up a Python project to manage YouTube Music playlist data using `ytmusicapi` and SQLite. It includes instructions on creating a virtual environment, managing dependencies, setting up OAuth authentication, initializing a SQLite database, fetching and storing data, and performing data analysis.
## Table of Contents
1. [Setting Up a Virtual Environment](#setting-up-a-virtual-environment)
2. [Installing Required Packages](#installing-required-packages)
3. [OAuth Authentication Setup](#oauth-authentication-setup)
4. [Database Setup](#database-setup)
5. [Fetching and Storing Data](#fetching-and-storing-data)
6. [Data Analysis](#data-analysis)
7. [Conclusion](#conclusion)
## Setting Up a Virtual Environment
Create and activate a virtual environment to isolate your package installations.
```bash
#!/usr/bin/env python3
python -m venv venv
source venv/bin/activate # On macOS and Linux
.\venv\Scripts\activate # On Windows
```
## Installing Required Packages
Ensure `ytmusicapi` is included in your `requirements.txt` and install:
```bash
pip install -r requirements.txt
```
## OAuth Authentication Setup
Initialize OAuth to authenticate your access to YouTube Music.
```bash
ytmusicapi oauth
```
Follow the on-screen prompts to authenticate. This process will generate an `oauth.json` file in your project directory.
## Database Setup
Create a SQLite database and define tables to store playlists and track data.
```python
import sqlite3
conn = sqlite3.connect('youtube_music.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS playlists (
playlist_id TEXT PRIMARY KEY,
title TEXT,
description TEXT,
privacy TEXT,
itemCount INTEGER
)
''')
```
## Fetching and Storing Data
Use `ytmusicapi` to fetch playlist data and store it in your database.
```python
from ytmusicapi import YTMusic
ytmusic = YTMusic('oauth.json')
playlists = ytmusic.get_library_playlists(limit=100)
# Insert data into the database...
```
## Data Analysis
Analyze your data directly from the SQLite database using SQL queries.
```python
# Most Popular Artists
c.execute('SELECT artist, COUNT(*) FROM tracks GROUP BY artist ORDER BY COUNT(*) DESC')
popular_artists = c.fetchall()
```
## Conclusion
With this setup, you can manage your YouTube Music playlists and perform data analysis using Python and SQLite effectively.
## Best Practices
- **Virtual Environment**: Always activate your virtual environment when working on the project.
- **Dependencies**: Regularly update your `requirements.txt` to reflect new dependencies.
- **Version Control**: Include `venv` in your `.gitignore` file and commit `requirements.txt` to maintain consistency across environments.
### Python Code to Write Markdown to a File
Here's the Python script to output the above Markdown content into a file named `YT_Music_Guide.md`:
```python
markdown_content = """[Your Markdown content from above]"""
with open('YT_Music_Guide.md', 'w') as file:
file.write(markdown_content)
```
This script will create a markdown file named `YT_Music_Guide.md` in your project directory. This file can be opened with any text editor or viewed on platforms like GitHub to provide clear documentation for setting up and running your Python project.

View File

@@ -0,0 +1,72 @@
# Python Style Guide
## Table of Contents
1. [Code Layout](#code-layout)
2. [Naming Conventions](#naming-conventions)
3. [Comments](#comments)
4. [Docstrings](#docstrings)
5. [Imports](#imports)
6. [Whitespace in Expressions and Statements](#whitespace-in-expressions-and-statements)
7. [Programming Recommendations](#programming-recommendations)
8. [Testing](#testing)
## Code Layout
- **Indentation**: Use 4 spaces per indentation level.
- **Line Length**: Limit all lines to a maximum of 79 characters.
- **Blank Lines**: Use blank lines to separate functions and classes, and larger blocks of code inside functions.
- **Encoding**: Use UTF-8 encoding for Python 3.
## Naming Conventions
- **Modules**: Short, lowercase names, preferably without underscores.
- **Classes**: Use the CapWords convention.
- **Functions**: Lowercase, with words separated by underscores.
- **Variables**: Lowercase, with words separated by underscores.
- **Constants**: Uppercase, with words separated by underscores.
## Comments
- **Inline Comments**: Use sparingly and focus on why, not what.
- **Block Comments**: Indent to the same level as the code it describes.
## Docstrings
- **Public Modules**: Describe what the module contains and its purpose.
- **Functions**: Describe what the function does and its arguments.
- **Classes**: Describe what the class does.
## Imports
- **Import Order**: Within each import group (standard library, third-party, local), sort the imports alphabetically.
- **Standard Library Imports**: First, separate from third-party libraries.
- **Related Third-Party Imports**: Second.
- **Local Application/Library Specific Imports**: Third.
- **Absolute Imports**: Prefer absolute imports over relative imports to improve readability and avoid issues with module naming conflicts.
- **Wildcards**: Avoid using wildcard imports (`from module import *`) as they can pollute the namespace and make it unclear which names are present.
## Whitespace in Expressions and Statements
- Avoid extraneous whitespace in the following situations:
- Immediately inside parentheses, brackets, or braces.
- Between a trailing comma and a following close parenthesis.
- Immediately before a comma, semicolon, or colon.
- **Binary Operators**: Surround binary operators with a single space on either side for readability, except when assigning default parameter values in function definitions.
- **Compound Statements**: Avoid using backslash line continuation. Instead, use implicit line joining within parentheses, brackets, and braces.
## Programming Recommendations
- **`is not` over `not ... is`**: Use `is not` operator rather than `not ... is`.
- **List Comprehensions**: Preferable over multiple `for` and `if` statements.
- **String Quotes**: Prefer single quotes for string literals, unless the string contains a single quote character. Use double quotes for triple-quoted strings and docstrings.
- **Exception Handling**: Use specific exception classes when catching exceptions, rather than using a bare `except:` clause. Avoid catching `Exception` or `StandardError` unless you are re-raising the exception or in the outermost block in a thread.
- **Context Managers**: Use context managers (`with` statement) when working with file objects and other resources that need to be properly closed or released.
- **Generator Expressions**: Consider using generator expressions instead of list comprehensions for large datasets or when you don't need to store the entire result in memory.
## Testing
- **Test Framework**: Use a standard testing framework like `unittest` or `pytest` to write and run tests for your code.
- **Test Coverage**: Strive for high test coverage to ensure your code is well-tested and to catch potential bugs early in the development process.
Remember, this is a starting guide. Feel free to adapt and expand it based on the needs of your projects and your personal preferences.

View File

@@ -0,0 +1,108 @@
The `requests` library in Python is a simple, yet powerful HTTP library, designed for human beings. It provides methods to send all kinds of HTTP requests, making it invaluable for a wide range of applications involving HTTP requests to interact with web services or APIs.
### Requests Library Usage Guide
#### Installation
```sh
pip install requests
```
### Basic HTTP Requests
The core of `requests` is its ability to make all types of HTTP requests. Here are brief examples categorized by request type:
#### GET Requests
Retrieve data from a specified resource.
```python
import requests
response = requests.get('https://api.github.com')
print(response.json()) # Convert JSON response to a dictionary
```
#### POST Requests
Send data to a server to create/update a resource.
```python
payload = {'key': 'value'}
response = requests.post('https://httpbin.org/post', data=payload)
print(response.text)
```
### Working with Parameters and Headers
#### Passing URL Parameters
Send GET requests with query parameters.
```python
params = {'q': 'Python HTTP library'}
response = requests.get('https://www.google.com/search', params=params)
print(response.url) # Check the formed URL
```
#### Custom Headers
Modify request headers for tasks like setting user-agent or authentication tokens.
```python
headers = {'User-Agent': 'My App/0.0.1'}
response = requests.get('https://api.github.com', headers=headers)
```
### Handling Response Content
#### Response Status Codes
Check response status to handle different scenarios like success or error.
```python
if response.status_code == 200:
print('Success!')
elif response.status_code == 404:
print('Not Found.')
```
#### JSON Response Content
Directly access JSON response content for APIs that send back JSON data.
```python
response = requests.get('https://api.github.com')
print(response.json()) # JSON response content
```
### Advanced Usage
#### Sessions with Context Managers
Use Sessions for keeping certain parameters across requests.
```python
with requests.Session() as session:
session.headers.update({'User-Agent': 'My App'})
response = session.get('https://api.github.com')
```
#### Handling Timeouts
Specify a timeout duration to avoid hanging indefinitely.
```python
try:
response = requests.get('https://api.github.com', timeout=1)
except requests.Timeout:
print('The request timed out')
```
### Project Ideas
- **Web Scraping**: Automate data collection from websites, respecting their `robots.txt` policies, to gather data for analysis.
- **API Consumption**: Develop applications that interact with various third-party APIs for tasks like social media posting, weather updates, or cryptocurrency price tracking.
- **Automated Testing**: Create scripts to test web applications' APIs, checking their responses, status codes, and performance.
- **Website Monitoring**: Develop a tool to monitor website uptime by periodically sending requests and alerting you via email or SMS when your site goes down.
### Security and Best Practices
- **Sensitive Data**: Avoid sending sensitive information like passwords or API keys as URL parameters. Use headers or POST requests instead.
- **Error Handling**: Always check for response status codes and handle exceptions like connection errors or timeouts gracefully.
- **Session Objects**: Use Session objects for persistent connections, especially when making multiple requests to the same host.
The `requests` library

View File

@@ -0,0 +1,132 @@
This library provides simple and efficient tools for predictive data analysis and is built on NumPy, SciPy, and matplotlib. It includes a wide range of supervised and unsupervised learning algorithms. Below is a concise reference guide for common use cases with `scikit-learn`, formatted in Markdown syntax:
# `scikit-learn` Reference Guide
## Installation
```
pip install scikit-learn
```
## Basic Concepts
### Importing scikit-learn
```python
import sklearn
```
## Preprocessing Data
```python
from sklearn.preprocessing import StandardScaler, OneHotEncoder
# Standardize features
scaler = StandardScaler().fit(X)
X_scaled = scaler.transform(X)
# One-hot encode categorical variables
encoder = OneHotEncoder().fit(X_categorical)
X_encoded = encoder.transform(X_categorical)
```
## Splitting Data
```python
from sklearn.model_selection import train_test_split
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
```
## Supervised Learning Algorithms
### Linear Regression
```python
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
```
### Classification (Logistic Regression)
```python
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
```
### Decision Trees
```python
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
```
## Unsupervised Learning Algorithms
### K-Means Clustering
```python
from sklearn.cluster import KMeans
model = KMeans(n_clusters=3)
model.fit(X)
labels = model.predict(X)
```
### Principal Component Analysis (PCA)
```python
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
```
## Model Evaluation
### Cross-Validation
```python
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
```
### Classification Metrics
```python
from sklearn.metrics import accuracy_score, confusion_matrix
accuracy = accuracy_score(y_test, predictions)
conf_matrix = confusion_matrix(y_test, predictions)
```
### Regression Metrics
```python
from sklearn.metrics import mean_squared_error, r2_score
mse = mean_squared_error(y_test, predictions)
r2 = r2_score(y_test, predictions)
```
## Tuning Hyperparameters
```python
from sklearn.model_selection import GridSearchCV
param_grid = {'n_estimators': [10, 50, 100], 'max_features': ['auto', 'sqrt', 'log2']}
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5)
grid_search.fit(X_train, y_train)
best_params = grid_search.best_params_
```
## Saving and Loading Models
```python
from joblib import dump, load
# Save a model
dump(model, 'model.joblib')
# Load a model
model = load('model.joblib')
```
`scikit-learn` is a versatile and comprehensive library that simplifies the implementation of many machine learning algorithms for data analysis projects. This guide touches on key features such as data preprocessing, model selection, training, and evaluation, but `scikit-learn`'s functionality extends far beyond these basics, making it a foundational tool in the machine learning practitioner's toolkit.

View File

@@ -0,0 +1,92 @@
In the realm of network programming and socket management in Python on Linux systems, the `socket` library stands out as a fundamental and powerful tool. The `socket` library in Python provides access to the BSD socket interface, offering a low-level means to create client/server programs, manage connections, send and receive data over the network, and interact with various network protocols directly from Python scripts. This library is particularly valuable for developing network applications, implementing custom protocols, or creating tools for network testing and exploration.
### Socket Library Reference Guide
#### Installation
The `socket` library is part of Python's standard library, so it's available in any standard Python installation without the need for additional packages.
#### Basic Usage
##### Creating a Socket
To create a socket, you simply need to import the `socket` module and use the `socket()` function, specifying the address family and socket type.
```python
import socket
# Create an IPv4 (AF_INET) socket object using the TCP protocol (SOCK_STREAM)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
```
##### Connecting to a Server
For a client program, use the `connect()` method to establish a connection to a server.
```python
server_address = ('hostname', 10000) # Hostname and port number
sock.connect(server_address)
```
##### Sending and Receiving Data
Once a connection is established, data can be sent using `sendall()` and received with `recv()`.
```python
# Sending data
message = 'This is the message. It will be repeated.'
sock.sendall(message.encode('utf-8'))
# Receiving data
data = sock.recv(1024)
print('Received', data.decode('utf-8'))
```
##### Closing a Socket
After communication is complete, close the connection to free up resources.
```python
sock.close()
```
##### Creating a Server
To create a server that listens for incoming connections, bind the socket to an address and port, and call `listen()` to start accepting connections.
```python
# Create a TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 10000)
server_socket.bind(server_address)
# Listen for incoming connections
server_socket.listen()
while True:
# Wait for a connection
connection, client_address = server_socket.accept()
try:
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
if data:
connection.sendall(data)
else:
break
finally:
# Clean up the connection
connection.close()
```
#### Use Cases
- **Custom Network Protocols**: Implementing specialized communication protocols for unique application requirements.
- **Network Utilities**: Building tools for network diagnostics, such as port scanners, ping implementations, or simple HTTP servers.
- **Socket-based IPC**: Utilizing sockets for inter-process communication on a single system, leveraging UNIX domain sockets (`AF_UNIX`).
#### Integration with Linux Systems
Python's `socket` library is particularly powerful on Linux due to the operating system's robust networking stack and support for various address families and socket types, including UNIX domain sockets for IPC.
#### Security Considerations
When developing network applications:
- Implement proper error handling to manage exceptions, such as connection timeouts or refused connections.
- Consider encryption for data in transit to protect sensitive information.
- Validate all data received over the network to prevent injection attacks or buffer overflows.
The `socket` library is a cornerstone for network programming in Python, offering deep integration with Linux's networking capabilities. It allows developers to create sophisticated network applications and utilities, harnessing the full power of the system's networking capabilities directly from Python.

View File

@@ -0,0 +1,67 @@
In the landscape of Python and Linux, especially when it comes to managing system-level tasks, `systemd` integration becomes crucial for developers and system administrators. The `systemd` ecosystem, being the init system and service manager for many Linux distributions, plays a pivotal role in system startup, service management, and logging. For Python applications that need to interact with or act as services managed by `systemd`, the `python-systemd` library is invaluable.
### Python-systemd Reference Guide
#### Installation
The `python-systemd` library can be installed via pip, but it's essential to ensure that your system has `systemd` and its development headers installed first. On many Linux distributions, you might need to install `libsystemd-dev` or a similar package using your package manager.
```sh
pip install systemd-python
```
#### Basic Usage
##### Interacting with `systemd` Journal
One of the key features of `systemd` is its centralized logging system, the journal. `python-systemd` allows Python scripts to write messages to the journal, enhancing debugging and logging capabilities beyond simple file-based logs.
###### Writing to the Journal
```python
from systemd import journal
journal.send("Hello from Python!", FIELD2="Additional data", PRIORITY=journal.LOG_INFO)
```
##### Monitoring the Journal
For applications that need to respond to system or application events, `python-systemd` can monitor the journal for new entries.
```python
from systemd import journal
j = journal.Reader()
j.log_level(journal.LOG_INFO)
j.add_match(_SYSTEMD_UNIT="my-service.service")
for entry in j:
print(f"Received log: {entry['MESSAGE']}")
```
##### Managing `systemd` Services
While `python-systemd` focuses primarily on journal interaction, managing `systemd` services (starting, stopping, or querying service status) typically involves command-line tools like `systemctl`. Python scripts can invoke these commands using the `subprocess` module for service management tasks.
```python
import subprocess
def start_service(service_name):
subprocess.run(["systemctl", "start", service_name], check=True)
def stop_service(service_name):
subprocess.run(["systemctl", "stop", service_name], check=True)
def get_service_status(service_name):
result = subprocess.run(["systemctl", "status", service_name], capture_output=True, text=True)
return result.stdout
```
#### Use Cases
- **Custom Logging**: Integrate Python applications with the `systemd` journal for unified system logging.
- **Event-Driven Applications**: Monitor the journal for specific system events or log messages from other services to trigger actions within a Python application.
- **Service Management Utilities**: Create Python scripts or applications to manage `systemd` services, potentially as part of deployment scripts, monitoring tools, or administrative dashboards.
#### Integration with Linux Systems
`python-systemd` leverages the capabilities of `systemd` to provide Python applications with deep integration into Linux system management. This is particularly useful for applications that need to operate as part of the system's startup and service management infrastructure or that benefit from tight integration with system logging.
#### Security Considerations
When interacting with system services or logs, ensure that your Python scripts run with the appropriate permissions and consider security implications, especially if processing log data or managing services based on external inputs.
The `python-systemd` library exemplifies the powerful integration possibilities between Python and Linux's `systemd`, offering Python developers tools to harness `systemd`'s capabilities for logging, service management, and system interaction. This facilitates the development of robust, system-aware applications and utilities that can leverage the full breadth of `systemd`'s features on modern Linux systems.

View File

@@ -0,0 +1,46 @@
from scipy.io.wavfile import write
import numpy as np
# Parameters
sample_rate = 44100 # Sample rate in Hz
duration = 16 # Total duration of the countdown in seconds
base_frequency = 440 # Base frequency for the first beep in Hz
final_frequency = 880 # Final frequency for the beep before the end tone in Hz
end_tone_frequency = 1760 # Frequency for the final tone
volume = 0.5 # Volume, from 0 to 1
beep_duration = 0.3 # Duration of each beep in seconds
beep_interval = 1 # Interval between beeps in seconds, decrease towards the end
end_tone_duration = 1.5 # Duration of the final tone in seconds
# Time array
t = np.linspace(0, duration, int(sample_rate * duration), False)
# Generate silent audio
audio = np.zeros_like(t)
# Generate beeps
for i in range(int(duration / beep_interval)):
# Calculate beep frequency to increase over time
freq = np.linspace(base_frequency, final_frequency, int(duration / beep_interval))[i]
# Calculate start and end samples for the current beep
start_sample = int(i * beep_interval * sample_rate)
end_sample = int(start_sample + beep_duration * sample_rate)
# Generate beep
beep = np.sin(2 * np.pi * freq * t[start_sample:end_sample]) * volume
# Insert beep into the audio signal
audio[start_sample:end_sample] += beep
# Generate the final tone
start_sample = int(duration * sample_rate - end_tone_duration * sample_rate)
end_sample = int(duration * sample_rate)
end_tone = np.sin(2 * np.pi * end_tone_frequency * t[start_sample:end_sample]) * volume
audio[start_sample:end_sample] += end_tone
# Ensure audio is in 16-bit format
audio = np.int16(audio / np.max(np.abs(audio)) * 32767)
# Save to file
wav_file = '/mnt/data/countdown_timer_tone.wav'
write(wav_file, sample_rate, audio)
wav_file

View File

@@ -0,0 +1,71 @@
For those working at the intersection of Python and Linux, particularly in the realm of filesystem monitoring and event handling, `watchdog` stands out as a very useful library. It provides a platform-independent API to monitor file system events for changes in directories and files. This makes it exceptionally useful for a variety of tasks, from auto-deploying applications when code changes in a directory to performing backup and synchronization tasks, or even triggering custom scripts in response to file system modifications.
### Watchdog Reference Guide
#### Installation
You can install `watchdog` using pip:
```sh
pip install watchdog
```
#### Basic Usage
##### Watching Directories for Changes
To start monitoring a directory for changes, you can use the `watchdog.observers.Observer` class and a subclass of `watchdog.events.FileSystemEventHandler` to handle events.
```python
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print(f'Event type: {event.event_type} Path: {event.src_path}')
if __name__ == "__main__":
path = "/path/to/directory"
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
```
This script will monitor the specified `path` for any changes and print out a message whenever a modification is detected.
#### Handling Different Types of Events
`watchdog` can handle various types of file system events, including:
- File creation (`on_created`)
- File deletion (`on_deleted`)
- File modification (`on_modified`)
- Directory moving or renaming (`on_moved`)
You can override these methods in your `FileSystemEventHandler` subclass to perform specific actions for each type of event.
#### Recursive Directory Watching
By setting `recursive=True` in the `observer.schedule()` method, `watchdog` will monitor all subdirectories of the specified path for changes. This is particularly useful for watching entire project directories or deeply nested filesystem structures.
#### Integration with Linux Systems
`watchdog` is especially powerful on Linux systems for automating system administration tasks, such as:
- Automatically compiling or testing code when changes are detected.
- Syncing files to a backup location or remote server on modification.
- Triggering alerts or logging when specific files or directories change.
#### Cross-platform Compatibility
While `watchdog` is particularly useful in Linux environments, it's designed to be cross-platform, working on Windows and macOS as well. This makes it an excellent tool for developing scripts and applications that need to operate on multiple operating systems.
#### Use Cases
Some common use cases for `watchdog` include:
- Developing continuous integration tools that react to code changes.
- Building custom backup and synchronization utilities.
- Implementing security monitoring tools that alert on unexpected file changes.
#### Performance Considerations
When monitoring large directories or using `recursive=True`, keep performance considerations in mind. `watchdog` might consume more resources under these conditions, so it's important to optimize your event handling code for efficiency.
`watchdog` provides a Pythonic way to tap into filesystem events, offering a blend of simplicity and power for file monitoring tasks. Its ease of use and flexibility make it an indispensable tool for automating tasks that react to changes in the filesystem, enhancing productivity and enabling sophisticated filesystem-based triggers in Python applications and scripts.