0% found this document useful (0 votes)
43 views

Sari Serhan Python Toolbox 100 Scripts For Developers 2023

Uploaded by

Odiljon Djamalov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Sari Serhan Python Toolbox 100 Scripts For Developers 2023

Uploaded by

Odiljon Djamalov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 193

Serhan Sari

Python Toolbox: 100 Scripts for Developers


Enhance Your Development Skills with Ready-to-Use
Python Scripts
Copyright © 2023 by Serhan Sari

All rights reserved. No part of this publication may be reproduced,


stored or transmitted in any form or by any means, electronic,
mechanical, photocopying, recording, scanning, or otherwise
without written permission from the publisher. It is illegal to copy
this book, post it to a website, or distribute it by any other means
without permission.

Serhan Sari asserts the moral right to be identified as the author


of this work.

Serhan Sari has no responsibility for the persistence or accuracy of


URLs for external or third-party Internet Websites referred to in
this publication and does not guarantee that any content on such
Websites is, or will remain, accurate or appropriate.

First edition

This book was professionally typeset on Reedsy


Find out more at reedsy.com
Contents

I. WEB SCRAPING

Extract news headlines from a website


Scrape product information from an e-commerce site
Monitor and extract stock prices
Scraping multilingual content for translation purposes
Scrape weather forecasts for specific locations

II. AUTOMATION

Automate repetitive file management tasks


Automate email sending with attachments
Create a script to schedule and run tasks
Automate data backup processes
Build a script to automate software installations

III. DATA ANALYSIS AND VISUALIZATION

Analyze and visualize financial data


Create graphs and charts for data presentations
Analyze and visualize weather data
Generate statistics from survey data
Create word cloud visualizations from text data

IV. IMAGE PROCESSING


Crop and resize images
Apply filters and e ects to photos
Create image thumbnails
Extract text from images using OCR
Watermark images with a logo or text

V. TEXT PROCESSING

Perform sentiment analysis on text data


Build a script for text summarization
Create a spell-checker or grammar checker
Convert text to speech or speech to text
Generate random text for testing purposes

VI. FILE MANAGEMENT

Sort and organize files in a directory


Search for files with specific extensions
Clean up duplicate files
Monitor changes in file directories
Rename files in bulk according to a pattern
Chapter 31

VII. SYSTEM MONITORING AND REPORTING

Monitor system resource usage (CPU, memory, disk)


Generate daily/weekly reports on system statistics
Monitor network tra c and generate reports
Create a script to log and analyze system events
Build a script to track and notify about system uptime

VIII. GAMES AND ENTERTAINMENT


Create a simple text-based game
Build a script to generate random jokes or facts
Design a quiz or trivia game
Develop a script for generating random art
Create a script to simulate rolling dice

IX. UTILITY

Calculate and convert units (e.g., currency exchange rates)


Create a script for generating strong passwords
Build a simple calculator
Convert between di erent file formats (e.g., PDF to text)
Implement a URL shortener

X. NETWORK AND INTERNET

Ping multiple hosts to check their status


Monitor website availability and response times
Retrieve and analyze website headers
Create a port scanner to check for open ports
Automate interactions with web APIs

XI. SECURITY

Encrypt and decrypt files


Create a simple password manager
Generate and verify digital signatures
Build a script for secure file deletion
Create a basic firewall rule manager

XII. IOT AND HARDWARE CONTROL


Build scripts to control IoT devices (e.g., lights, thermostats)
Monitor and display sensor data (e.g., temperature,
humidity)
Control a robot or drone
Capture and analyze data from webcams or cameras
Create a script to interface with microcontrollers (e.g.,
Arduino)

XIII. AI AND MACHINE LEARNING

Implement a basic machine learning model (e.g., linear


regression)
Develop a simple chatbot using natural language processing
Train a model for image recognition
Create a recommendation system
Build a script for sentiment analysis on social media data

XIV. DATABASE

Automate database backup and restore


Generate and execute SQL queries
Build a script for database migration
Extract data from databases to CSV or Excel files
Create a basic CRUD application

XV. EDUCATION AND LEARNING

Create flashcards for studying


Build a script for math problem generation
Develop a spelling or vocabulary quiz
Implement a script for learning a new language
Create a timer for productivity and focus
About the Author
Also by Serhan Sari
I

Web Scraping

Web scraping scripts are programs designed to


extract data from websites. These scripts use various
techniques to navigate through the structure of web
pages, locate relevant information, and then
retrieve and store that data for further use. Web
scraping is commonly used for tasks such as data
extraction, data mining, and content aggregation.
Extract news headlines from a
website
To extract news headlines from a website using Python, you can use the requests library to fetch the HTML content of
the webpage and Beauti fu lSoup for parsing the HTML and extracting the head lines. Here's a basic example using
these libraries:

import requests
from bs4 import BeautifulSoup

def get_headlines (url) :


# Send a GET request to the URL
response = requests.get(url)

# Check if the request was successful (status code 200)


if response.status_code == 200 :
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.text, 'html .parser' )

# Locate the HTML elements containing the headlines


# Adjust the CSS selectors based on the structure of the webpage
headlines = soup.select( '.headline-class' } # Replace with the actual CSS
selector

# Extract and print the headlines


for headline in headlines:
print(headline . text)
else :
print ( f"Fai led to retrieve content. Status code: {response. status_code}" )

# Replace 'https://example.com' with the actual URL of the website


get_headlines( 'https://example.com' }

Note:

1. You need to install the requests and beautifulsoup4 libraries if you haven't already. You can do this using:

pip install requests beautifulsoup4

2. Adjust the css selector inside the soup.select (} method to match the structure of the HTMLcontaining the
headlines on the specific website you are targeting.
Scrape product informa on from an
e-commerce site
Web scraping e-commerce sites should be done ethica lly and in compliance with the site"s terms of service. Here's a
basic example usi ng Python, requests , and Beauti fulSoup to scrape product information from an e-commerce site .
In this example, we'll use a hypothetical URL, and you should replace it with the actual URLof the e-commerce site you
want to scrape:

import requests
from bs4 import BeautifulSoup

def scrape_product_information (urll :


# Send a GET request to the URL
response = requests.get(url)

# Check if the request was successful (status code 200)


if response.status_code == 200 :
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser' )

Locate the HTML elements containing product information


#
Adjust the CSS selectors based on the structure of the webpage
#
product_containers = soup . select( '.product-container-class' l # Replace with
the actual CSS selector

# Extract and print product information


for product_container in product_containers:
# Extract product details (adjust the CSS selectors accordingly)
product_name = product_container.select_one( '.product-name-
class' ) . text . strip()
product_price = product_container . select_one( ' .product-price-
class' ) . text . strip()

print( f"Product Name: {product_name}" )


print ( f"Product Price: {product_price}" )
print( "-" * 50 )
else :
print ( f"Fai led to retrieve content. Status code: {response. status_code}" )

# Replace 'https://example-ecommerce-site.com' with the actual URL of the e-commerce


site
scrape_product_information( 'https://example-ecommerce-site.com' )
Things to note:

1. Install the required libraries using :

pip install requests beautifulsoup4

2. Adjust the CSS selectors inside soup .select ( ) to match t he structure of the HTML containing product
information on the specific e-commerce site you are targeting.

3. Web scraping should be done responsibly, and you should be aware of the legal and ethical implications. Some
websites may have restrictions on web scra ping in their terms of service. Always review and comply with the
terms of the website you are scraping.

4. Make requests at a reasonable rate to avoid overloading the server and potential IP blocking .
Monitor and extract stock prices

To monitor and extract stock prices with Python, you can use various libraries, and one popular choice is yfinance for
Yahoo Finance data. Here"s a simple example:

First, you need to install the yfinance li brary:

pip install yfinance

Now, you can use the fol lowing Python script to get stock prices:

import yfinance as yf
import time

def get_ stock_ price (ticker) :


stock = yf.Ticker(ticker}
price = stock.history(period= 'ld' )[ 'Close' ].iloc[-1 1
return price

def monitor_ stock_ price (ticker, interval_seconds=60) :


while True :
price = get_stock_price(ticker)
print( f'{ticker} Stock Price: ${price: .2f}' )
time.sleep{interval_ seconds}

# Replace 'AAPL' with the stock symbol you want to monitor (e.g., 'AAPL' for Apple)
stock_ symbol = 'AAPL'

# Monitor stock price every 60 seconds


monitor_ stock_ price(stock_ symbol)

Replace 'AAPL' with the stock symbol of the company you're interested in . This script will continuously print the stock
price at the spec ified interval (in seconds). You can adjust the inte rva l_seconds parameter based on how freq uently
you want to check the stock price.
Scraping mul lingual content for
transla on purposes
To scrape multilingual content for translation purposes using Python, you can use the requests library for making
HTTP requests and BeautifulSoup for parsing HTML content. Additionally, you may want to use a translation API ,
such as Google Tra nslate, to translate the content. Here's a basic example using these libra ries:

import requests
from bs4 import BeautifulSoup
from googletrans import Translator

def scrape_and_translate (url, target_language= 'en' ) :


# Make an HTTP request to the website
response = requests.get(url)

if response.status_ code == 200 :


# Parse the HTML content
soup = BeautifulSoup(response.text, 'html.parser' )

# Extract text content from the webpage


text_ content = soup.get_text()

Translate the text content


#
translator = Translator()
translated_content = translator.translate(text_content,
dest=target _ language).text

return translated_content
else :
print ( f"Failed to retrieve content. Status code: {response. status_code}" )
return None

# Example usage
url_to_scrape = 'https://example.com'
t rans lated_text = scrape_and_translate ( u rl_to_ scrape, target_ language= ' fr'' )

if translated_ text:
print( f"Trans lated Content: \n{translated_text}" )

In this example:

• The requests library is used to make an HTT P request to the specified URL.
• Beaut ifu lSoup is used to parse the HTML content and extract the text.
• The googletrans library is used for translation. Install it using pip install googletrans==4. 0. 0-rcl.

Note: Make sure to review the terms of service of the translation API you use and comply with any usage restrictions.
Scrape weather forecasts for specific
loca ons

You can scrape weather forecasts using Python with the help of web scraping libraries like Beautifu lSoup and requests.
Here's a simple example using Python:

import requests
from bs4 import BeautifulSoup

def scrape_weather (location) :


# Replace 'YOUR_WEATHER_WEBSITE_URL' with the actual URL of the weather website
you want to scrape
url = f'YOUR_WEATHER_WEBSITE_URL/{location}'
response = requests.get(url)

if response.status_code == 200 :
soup = BeautifulSoup (response.text, 'html . parser' )

Use BeautifulSoup to extract the relevant information from the HTML


#
Replace these selectors with the actual HTML elements that contain the
#
weather information
temperature = soup.select_one{ ' .temperature' ).text
condition = soup.select_one( ' .weather-condition' ).text

print( f'Temperature: {temperature}\nCondition: {condition}' )


else :
print( 'Failed to retrieve weather information.' )

# Replace 'CITY_NAME' with the name of the city for which you want to get the
weather forecast
scrape_weather( 'CITY_NAME' )

Before using this code, you need to inspect the HTML structure of the website from which you want to scrape weather
information. Determine the appropriate HTML tags and classes that contain the data you need, and update the code
accordingly.
II

Automa on

Automation scripts are programs designed to


perform repetitive tasks or processes without direct
human intervention, enhancing e ciency and
reducing manual workload. These scripts leverage
programming languages to streamline routine
operations, ranging from file management and
data processing to system monitoring and software
installations. Automation scripts contribute to
increased productivity, decreased error rates, and
consistent task execution across various domains,
such as system administration, data analysis, and
software development. By eliminating manual
intervention in repetitive workflows, automation
scripts empower users to focus on more complex
and strategic aspects of their work, ultimately
optimizing resource utilization and enhancing
overall operational e ectiveness.
Automate repe ve file management
tasks
Automating repetitive file management tasks with Python can save time and effort. Here's a general guide on how to
perform common file management tasks using Python:

1. Renaming Files: To rename files, you can use the os module. Here's an example that renames all files in a
directory by adding a prefix:

import os

folder_path = '/path/to/your/files'
prefix= 'new_prefix_ '

for filename in os. listdir(folder_path):


if os.path.isfile(os.path.join(folder_path, filename)):
new_filename =prefix+ filename
os. rename ( o s. path.join (folder_path, f ilename) ,
os.path.join(folder_path, new_filename))

2. Moving Files: To move fi les from one directory to another, use the shuti l module:

import shutil

source_path = '/path/to/source'
destination_path = '/path/to/destination'

shutil.move(source_path, destination_path)

3. Copying Files: For copying files, also use the shutil module:

import shutil

source_path = '/path/to/source'
destination_path = '/path/to/destination'

shutil.copy{source_path, destination_path)
4. Deleting Files: To delete files, you can use the os module:

import os

file_to_delete = '/path/to/your/file.txt'

if os.path . exists(file_to_delete):
os.remove(file_to_delete)
else :
print( "The file does not exist." )

5. Batch Processing: To apply the same operation to multip le files, you can LJse loops and functions. For example, if
you want to delete all files with a certain extension:

import os

folder_path = '/path/to/your/files'
extension_to_delete = '.txt'

for f ilename in os. l istdir(folder_path):


if filename.endswith(extension_to_delete):
file_to_delete = os . path.join(folder_path, filename)
os . remove(file_to_delete)

Make sure to rep lace the paths and filenames with your actual file paths and names. Additionally, exercise caution when
performing operations like file deletion to avoid accidental loss of data. Always test your scripts on a small set of files
before applying them to a larger dataset.
Automate email sending with
a achments
Firstly, you need to enable "Less secure app access" in your Gmail account settings or generate an "App Password" if
you have two-factor authentication enabled.

import smtplib
from email.mime.multipart import MIMEMultipart
from email . mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

def send_email (subject, body, to_email, attachment_path) :


# Email configuration
smtp_server = 'smtp.gmail.com'
smtp_port = 587
sender_email = '[email protected]'
sender_password = 'your_password' # Use App Password if two-factor
authentication is enabled

# Create message container


msg = MIMEMultipart()
msg [ 'From' ] = sender_email
msg [ 'To' l = to_email
msg[ 'Subject' l = subject

# Attach body as plain text


msg .attach{MIMEText(body, ' plain' ))

#Attach file
attachment = open{attachment_path, 'rb' )
part= MIMEBase( 'application' , 'octet-stream' }
part. set_payload( attachment . read ())
encoders. encode_base64( pa rt)
part .add_header( 'Content-Disposition' , f' attachment; filename="
{attachment_path}"' )
msg .attach(part)

# Connect to the SMTP server


with smtplib.SMTP{smtp_server, smtp_port) as server:
server.starttls()
server. login(sender_email, sender_password)

# Send email
server.sendmail(sender_email, to_email, msg.as_string{))

print( f"Email sent successfully to {to_email}" )


# Example usage
send_email(
subject= 'Test Email with Attachment' ,
body= 'This is the body of the email.' ,
to_email= 'recipient@example .com' ,
attachment_path= '/path/to/your/file.txt'

Make sure to rep lace the placeholder va lues with your own emai l deta il s and the actual f ile path you wa nt to attach.
Additional ly, always keep your email credentia ls secure and co nsid er using environment variables to store sensitive
information .
Create a script to schedule and run
tasks

To schedule and run tasks in Python, you can use the schedule library, whic h is a lig htweight library that provides a
simple interface for job scheduling. First, you need to install the library using:

pip install schedule


_J
Now, you can create a script to schedule and run tasks at specified intervals. Here's a simple example:

import schedule
import time

def job () :
print( "Task is running ..•• " )

# Schedule the job to run every 1 hour


schedule.every( l ) . hours.do(job)

# You can add more schedules for different intervals


# For example, every 30 minutes: schedule.every(30).minutes.do(job)
# For every day at a specific time: schedule. every() .day. at ( "10: 30") .do( job)

# Run the scheduler


while True :
schedule. run_pending ()
time.sleep( l ) # You can adjust the sleep duration as needed

This script defines a job function that prints a message, schedules the job to run every hour using
schedule.every ( 1) • hours. do (job) , and t hen runs the schedu ler in a loop with a short delay between iterations.

You can customize t he scheduling based on your needs, and you can also modify the job function to perform any task
you want.

Remember to adapt the script according to your specific requirements and adjust the sched uling intervals accordingly.
Additional ly, consider using more robust solutions like Celery for more complex task scheduling in larger projects.
Automate data backup processes

Automating data backup processes in Python can be ac hieved using various libraries and tools. One popular library for
file operations is shuti l , and combining it with a scheduling li brary like schedule can help automate the backup
process. Here's a simple example:

1. Install the schedule library if you haven't already:

pip install schedule

2. Create a Python script to perform the backup:

import shutil
import schedule
import time
import datetime

def backup () :
source_directory = "/path/to/source"
backup_d i rectory = "/path/to/backup"

timestamp = datetime . datetime.now().strftime( "%Y%111%dl\sH%Ml\sS" )


backup_folder = f"backup_{timestamp}"

try :
shuti l. copyt ree( source_directo ry, f "{backup_directory}/{backup_ folder}" )
print( "Backup completed successfully . " }
except Exception as e:
print( f"Backup failed. Error: {e}" )

# Schedule the backup to run every day at a specific time (e.g., 2:00 AM)
schedule.every() .day.at( "02:00" ) .do(backup)

#Run the scheduler


while True :
schedule . run_pending(}
time.s l eep( l )
In this script:

• The backup function uses shuti l. copyt ree to copy the entire source directory to a new backup folder within
the specified backup directory.
• The backup folder is named with a timestam p to ensure uniqueness.
• The schedule library is used to schedule the backup function to run every day at a specific time (adjust t he t ime
as needed).

Adjust the sou rce_directo ry and backup_directo ry variables according to your setup. You can also customize
t he backup schedule and implement more advanced features based on your specific req uirements.

Keep in mind t hat this is a basic example, and in a production environment, you might want to consider error handling,
loggi ng, and possibly more robust backup solutions depending on your needs.
Build a script to automate so ware
installa ons

Automating software installations in Python can be done using the subprocess module to run system commands.
Below is a simple example scri pt that installs multiple software packages on a Linux system using the package manager
(e.g., apt for Ubuntu) :

import subprocess

def install_software (packages) :


try :
for package in packages:
print( f"Installing {package} ••• " }
subprocess.run([ "sudo" , "apt" , "install" , "-y" , package]. clleck=True )
print( f"{package} installed successfully . \n" )
except subprocess.CalledProcessError as e:
print( f"Error installing {package} . {e}" )

if name == "_main_" ·
# List of software packages to install
softwa re_packages = [ "package!" , "package2" , "package3" ]

# Run the installation


install_software(software_packages)

Replace " package!", "package2" , etc., with the actual package names you want to install. You may need to adjust
the package manager command (s udo apt install) based on the package manager used by your operating system.

Remember:

• Ensure that the script is executed with appropriate privileges to install software (sudo is used in this example).
• Be cautious when automating installations, especially with the use of sudo. Unauthorized or incorrect instal lations
ea n affect system stability.

This script is a basic examp le, and in a production environment, you might want to consider additional features such as
logging, error handling, and more robust configuration options.
III

Data Analysis and Visualiza on

Data analysis and visualization are tools developed


to process and interpret large datasets, extract
meaningful insights, and present them in an
understandable format. These scripts often employ
programming languages like Python or R, utilizing
statistical techniques and visualization libraries to
uncover patterns, trends, and relationships within
the data, from generating descriptive statistics to
creating informative charts and graphs.
Analyze and visualize financial data
Analyzing and visualizing financial data can be done using various Python libraries. Two popular libraries for this purpose
are pandas for data manipulation and analysis and matplotlib for creating visualizations. Here"s a simple example to
get you started:

1. Install necessary libraries:

pip install pandas matplotlib

2. Sample Python script for financial data analysis and visualization:

import pandas as pd
import matplotlib . pyplot as plt

# Sample financial data (replace tnis witn your dataset)


data= {
'Date' : [ '2023-01-01' , '2023-01-02' , '2023-01-03' , '2023-01-04' , '2023-01-05' ),
'Price' : (100 , 105 , 98 , 102 , 110 ),
}

# Create a Dataframe from tne data


df = pd.DataFrame(data)

# Convert the 'Date' column to datetime format


df[ 'Date' ) = pd.to_datetime(df[ ' Date' ))

# Set the 'Date' column as the index


df.set_index( 'Date' , inplace=True )

# Plotting the financial data


plt . figure(figsize=( 10 , 6 ))
plt . plot(df.index, df[ 'Price' ], marker= 'o' , linestyle= '-' , color= 'b' )
plt . title( 'Financial Data Analysis' }
plt.xlabel( 'Date' )
plt.ylabel( 'Price' )
plt . grid( True )
plt . show()

This example assumes you have a time series of financial data with 'Date' and 'Price' columns. Adjust the script
according to your actual dataset.
3. Enhance with financial libraries:**

For more advanced financia l analysis and visualizations, you can consider using libraries such as numpy for numerical
operations, pa ndas_data reader for fetc hing fi nancia l data from various sources, and mp l finance for specialized
financial plots.

pip install numpy pandas_datareader mplfinance

Below is a more comprehensive example:

import pandas_datareader as pdr


import numpy as np
import mplf inance as mpf

# Fetch financial data from Yahoo Finance


symbol = 'AAPL'
start_date = '2022-01-01'
end_date = '2023-01-01'
financial_data = pdr.get_data_yahoo{symbol, start=start_date, end=end_datel

# Calculate moving average


financial_data[ 'MA20' ) = financial_data [ 'Close' ].rolling{window=20 ).mean{)

#Plot candlestick chart with moving average


mpf.plot(
financial_data,
type='candle' ,
mav=(20 ,),
title=f'{symbol} Stock Price and 20-Day Moving Average' ,
ylabel='Price' ,
show_nontrading =True ,

This script fetc hes stock data for Apple (AAPL) from Yahoo Finance, calculates a 2O-day moving average, and visualizes
the data using mp l finance . Adjust the symbol, start_date, and end_date according to your needs.
Create graphs and charts for data
presenta ons
Creating graphs and charts for data presentations in Python can be done using various libraries. Two commonly used
libraries for this purpose are matp lot lib and seabo rn. Here are examples of how you can create different types of
charts using these libraries:

1. Install necessary libraries:

pip install matplotlib seaborn

2. Example scripts for different types of charts:

Bar Chart:

import matplotlib.pyplot as plt

# Sample data
categories= [ 'Category A' , 'Category B' , 'Category C' ]
values= (25 , 40 , 30 )

# Create a bar chart


plt.bar{categories, values, color= 'blue' )
plt.title( 'Bar Chart Example' )
plt.xlabel( 'Categories' )
plt.ylabel( 'Values' )
plt. show(}

Line Chart:

import matplotlib.pyplot as plt

# Sample data
x_values = (1, 2 , 3 , 4 , 5)
y_values = (10 , 12 , 18 , 15 , 20 ]

# Create a line chart


plt.plot(x_values, y_values, marker= 'o' , linestyle= '-' , color= 'green' )
plt.title( 'Line Chart Example' )
plt.xlabel( 'X-axis' )
plt.ylabel( 'Y-axis' )
plt. show()
Scatter Plot:

import matplotlib.pyplot as plt

# Sample data
x_values = (1 , 2, 3 , 4 , SI
y_values = (10 , 12 , 18 , 15 , 20 1

# Create a scatter plot


plt ..scatter(x_values, y_values, color= ' red' l
plt ..title( 'Scatter Plot Example' l
plt ..xlabel( 'X-axis' )
plt.ylabel( 'Y-axis' )
plt . show()

Histogram:

import matplotlib.pyplot as plt


import numpy as np

# Generate random data for the histogram


data = np.random.randn( 1000 )

# Create a histogram
plt ..hist(data, bins=20 , color='purple' , edgecolo r= 'black' l
plt.title( 'Histogram Example' )
plt.xlabel( 'Values' )
plt.ylabel( 'Frequency' )
plt ..show()

Heatmap (using seaborn):

import seaborn as sns


import numpy as np

# Generate random data for the heatmap


data = np.random.rand( S, 5 )

# Create a heatmap using seaborn


s ns ..heat map (data, annot=True , cmap=' v i r idis' }
plt.title( 'Heatmap Example' )
plt ..show()

These are just basic examples, and both matp lot lib and seabo rn offer a wide range of customization options for
creating complex and informative data visua lizations. Adjust the scripts based on your specrfic data and visua lization
requirements.
Analyze and visualize weather data
Analyzing and visualizing weather data can be done using Python with the help of various libraries. For this purpose, you
might want to use libraries such as pandas for data manipulation, matp lot lib for plotting, and seabo rn for additional
plotting styles. Here 's a basic example to get you started:

1. Install necessary libraries:

pip install pandas matplotlib seaborn

2. Example script:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Sample weather data (replace this with your own data)


data= {
'Date' : I '2023-01-01' , '2023-01-02' , '2023-01-03' , '2023-01-04' , '2023-01-05' I ,
'Temperature' : [25 , 28 , 22 , 30 , 26 ),
'Humidity' : (60 , 55 , 70 , 45 , 50 ),
'Wind Speed' : (10 , 12 , 8, 15 , 11)
}

# Create a Dataframe
weather_df = pd.DataFrame(data)
weather_df[ 'Date' ] = pd.to_datetime{weather_df[ 'Date' ))

# Print the DataFrame


print(weather_df}

# Plotting
plt.figure(figsize=( 12 , 6))

# Line plot for temperature


plt.subplot( 2, 2, 1)
sns.lineplot(x= 'Date' , y= 'Temperature' , data=weather_dfl
plt.title( 'Temperature Over Time' )

# Bar plot for humidity


plt.subplot( 2, 2, 2)
sns.barplot(x= 'Date' , y= 'Humidity' , data=weather_df)
plt.title( 'Humidity Over Time' )
# Scatter plot for temperature vs . wind speed
plt.subplot( 2 , 2 , 3 )
sns.scatterplot(x= 'Temperature' , y= 'Wind Speed' , data=weather_df)
plt.title( 'Temperature vs. Wind Speed' )

# Heatmap for correlation matrix


plt.subplot( 2 , 2 , 4 )
corr_matrix = weather_df.corr()
sns.heatmap(corr_matrix, annot=True , cmap= 'coolwarm' )
plt.title( 'Correlation Matrix' }

plt.tight_ layout()
plt. show()

In this example, I've used a synthetic dataset with columns for date, temperature, humidity, and wind speed. The script
creates different types of plots such as line plot, bar plot, scatter plot, and a heatmap showing the correlation matrix
between variables.

Replace the sample data with your actual weat her data for meaningful visualizations. Adjust the script based on the
specific aspects of weather data you want to analyze and visualize.
Generate sta s cs from survey data
To generate statistics from survey data in Python, you can use the pandas library for data manipulation and the
matplotlib library for visualization. Below is a basic example to get you started:

1. Install necessary libraries:

pip install pandas matplotlib

2. Example script:

import pandas as pd
import matplotlib . pyplot as plt

# Sample survey data (replace this with your own data)


data = {
'Age' : (25 , 30 , 22 , 35 , 28 , 40 , 32 , 28 , 22 , 36),
'Income' : [50000 , 60000 , 45000 , 75000 , 55000 , 80000 , 70000 , 60000 , 48000 ,
72000 ),
'Satisfaction' : (4 , 5 , 3, 5 , 4 , 5 , 4 , 3 , 3 , 5),
'Education' : [ 'Bachelor' , 'Master' , 'Bachelor' , 'PhD' , 'Bachelor' , 'PhD' ,
'Master' , 'Bachelor' , 'Master' , 'PhD' ]
}

# Create a Dataframe
survey_ df = pd . DataFrame(data)

# Print basic statistics


print ( "Basic Statistics:" )
print(survey_df.describe())

# Plotting
plt ..figure(figsize= ( l2 , 4 ))

# Histogram for Age


plt.subplot( l , 3 , 1 )
plt.hist(survey_df [ 'Age' ), bins=S, edgecolo r= 'black' )
plt . title( ' Age Distribution' )

# Boxplot for Income


plt.subplot( l , 3 , 2 )
p lt .. boxp lot ( survey_df [ 'Income' 1)
plt.title( 'Income Distribution' )
# Bar plot for Education
plt . subplot( l , 3 , 3 )
education_ counts = survey_df[ 'Education' ].value_ counts()
education_ counts.plot(kind = 'bar' , color= 'skyblue' )
plt.title( 'Education Level' )

plt . tight_ layout()


pH . show(}

In t his example, I' ve used a synthetic dataset with columns for age, income, satisfaction, and education. The script
calculates basic statistics using describe() and creates differe nt types of plots such as a histogram, boxplot, and bar
plot.

Replace the sample data with your actual survey data for meaningfu l statistics and visualizations. Adjust the script based
on the specific aspects of your survey data that you want to analyze and visualize.
Create word cloud visualiza ons from
text data
To create word cloud visualizations from text data in Python, you can use the wordcloud library along with other
libraries such as matp lot lib for visualization and nl tk for text processing. Here's a basic example:

1. Install necessary libraries:

pip install wordcloud matplotlib nltk

2. Example script:

import matplotlib.pyplot as plt


from wordcloud import WordCloud
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk import download

# Download NLTK resources (stopwords)


download( 'stopwords' )
download( 'punkt' )

# Sample text data (replace this with your own text}


text_data = "'"'
Python is an amazing programming language. It is widely used for data analysis,
machine learning, and web development. Python has a large and active community
that contributes to its growth and success .
IIHH

# Tokenize the text and remove stopwords


stop_words = set(stopwords.words( 'english' ))
tokens = word_tokenize(text_data}
filtered_tokens = [word. lower(} for word in tokens if word.isalpha(} and
word.lower() not in stop_words)

# Join the tokens into a single string


processed_text = ' ' .join(filtered_tokensl

# Generate word cloud


wordcloud = WordCloud(width=800 , height=400 ,
backg round_co lo r= 'white' ) • generate( processed_text}

# Plot the WordCloud image


plt.figure(figsize=(l0 , 5 ))
plt. imshow(wordcloud, interpolation=' bi linear' )
pH.axis( 'off' )
plt. show()
In this example, the script preprocesses the text by tokenizing it, converting words to lowercase, and remov ing common
English stopwords. The processed text is then used to generate a word cloud using the WordCloud class from t he
WO rdc loud library.

Replace the text_data variable with your actual text data . Adjust the preprocessing steps based on your specific
requirements. You can also customize the appearance of the word cloud by modifying parameters in the Wo rdC loud
constructor.
IV

Image Processing

Image Processing is a computational tool designed


to manipulate and enhance digital images through
various algorithms and techniques. Leveraging
programming languages such as Python or
MATLAB, these scripts perform tasks like resizing,
cropping, filtering, and applying e ects to images.
More advanced applications involve tasks like
object recognition, segmentation, and color
correction.
Crop and resize images
To crop and re size images with Python, you can use the Pillow (PIL) library. If you haven't installed Pillow yet, you can do
so by running:

pip install Pillow

Now, here's an example Python script to crop and resize an image:

from PIL import Image

def crop_and_resize (input_path, output_path, crop_box, new_size) :


# Open the image
img = Image.open(input_path)

# Crop the image


cropped_img = img.crop(crop_box)

# Resize the cropped image


resized_img = cropped_ img.resize(new_ size)

# Save the result


resized_img.save(output_path)

# Example usage:
input_image = "path/to/your/input_image.jpg"
output_image = "path/to/your/output_image.jpg"

# Define the crop box (left, upper, right, lower)


crop_box = (100 , 100 , 500 , 500 )

# Define the new size (width, height)


new_ size = {300 , 300 )

# Call the function


crop_and_ resize(input_image, output_ image, crop_box, new_ size)

Replace "path/to/your/ input_image. j pg" and "path/to/your /output_image. j pg" with the actual paths for
your input and output images.

In this example:

• c rop_box defines the region to be cropped from t he original image.


• new_s ize specifies the width and height of the resized image.

Adjust these parameters according to your requirements. Run the script, and it will crop and resize the image
accordingly.
Apply filters and effects to photos

To apply fi lters and effects to photos in Python, you can use the Pillow (PIL) library. Here's an example script that applies
a few filters to an image:

from PIL import Image, ImageFilter

def apply_filters (input_path, output_path) :


# Open the image
img = Image.open(input_path)

# Apply filters
filtered_img = img.filter(ImageFilter.BLUR)
filtered_img = filtered_img,filter(ImageFilter .C0NT0UR)
fi ltered_img = filtered_img ..filter( ImageFilter. EDGE_ENHANCE)

# Save the result


filtered_img.save(output_path)

# Example usage:
input_image = "path/to/your/input_image.jpg"
output_image = "path/to/your/output_image.jpg"

# Call the function


apply_filters(input_image, output_image)

Replace "path/to/your/ input_image. j pg" and "path/to/your/output_image. j pg" with the actual paths for
your input and output images.

In this example, I' ve applied a few standard filters like BLUR, CONTOUR, and EDGE_ENHANCE. You can experiment with
other filters provided by the ImageF i l te r module in Pillow.

Run the script, and it will apply the specified filters to the image and save the resu lt. Adjust the filters according to your
preferences and requirements.
Create image thumbnails

To create image thumbnails in Python, you ca n use the Pillow (PIL) library. Here's a simple script to generate thumbnai ls
from an image:

from PIL import Image

def create_ thumbnail (input_path, output_path, thumbnail_size=(l00, 100)) :


# Open the image
img = Image.open(input_path)

# Create a thumbnail
img.thumbnail(thumbnail_ size}

# Save the thumbnail


img.save(output_path)

# Example usage:
input_image = "path/to/your/input_image.jpg"
oLJtput_ thumbnail = "path/to/your /output_thumbnai l. j pg"

# Call the function


create_ thumbnail(inpLit_ image, output_thumbnail}

Replace "path/to/your/ input_image. j pg" and "path/to/your /output_ thumbnail. j pg" with the actua l
paths for your input image and the desired out put thumbnail.

In t his example, a thLJmbnail size of (100, 100) pixel.sis used, but you can adjust the thumbnai l_s ize parameter
according to your requirements.

Run the script, and it will generate a thLJmbnail of the specified size and save it to t he output path.
Extract text from images using OCR
To extract text from images using Optical Character Recognition (OCR) in Python, you can use the Tesseract OCR
engine along with the pytesseract library. Additionally, yoLI 'II need to have Tesseract installed on your machine.

Here's an example script to perform OCR on an image:

from PIL import Image


import pytesseract

def extract_text_from_image (image_path) :


# Open the image using Pillow (PIL)
img = Image.open(image_path)

# Use pytesseract to do OCR on the image


text = pytesseract.image_t o_s tring(img}

return text

# Example usage:
image_ path = "path/to/your/image.jpg"

# Call the function


result_text = extract_t ext_ from_image{image_path)

# Print the extracted text


print( "Extracted Text:" )
print(result_text)

Replace "path/to/your/ image. j pg" with the actual path to your image.

Before running this script, make sure to install the required libraries:

pip install pillow pytesseract

Also, yoLJ need to have Tesseract installed on your machine. You can download it from the official GitHub repository:
https://github.com/tesseract-ocr/tesseract

After insta lling Tesseract, you might need to specify its executable path in your script. Update the
pytesseract. pytesseract. tesseract_cmd variable accordingly:

pytesseract. pytesse ract. tesseract_ cmd = r' C: \Program Files\ Tesseract-


0CR\ tesseract. exe' # Example path for Windows
Watermark images with a logo or text
To watermark images with a logo or text in Python, you can use the PIL (Pillow) library for image processing. Below is an
example script that demonstrates how to add a watermark to an image:

from PIL import Image, ImageDraw, ImageFont

def add_watermark (input_image_path, autput_image_path, watermark_text) :


# Open the input image
original_image = Image.open(input_image_path)

# Create a copy of the original image to avoid modifying it directly


watennarked_image = original_image.copy()

# Initialize ImageDraw for drawing on the image


draw = ImageDraw.Draw(watermarked_image)

# Set watermark text font and size


font = ImageFont.load_default()

# Set watermark text color and opacity


text_color = (255 , 255 , 255 ) # White
text_opacity = 100 # Opacity level (0-255)

#Calculate watermark position (you can adjust the position as needed)


watennark_position = {10 , 10 )

# Add text watermark to the image


draw.text(watermark_position, watermark_text, font =font, fill=text_color +
(text_opacity,))

#Save the watermarked image


watennarked_image . save(output_image_path)

if name == "_main_" ·
# Example usage:
input_image_path = "path/to/your/image.jpg"
output_image_path = "path/to/output/watermarked_image.jpg"
watennark_text = "Your Watermark"

add_watermark(input_image_path, output_image_path, watermark_text)

Replace "path/to/your/ image. j pg" with the actual path to your input image and
"path/to/output/wate rma rked_image. j pg" with the desired path for the watermarked output image. You can
also customize the watermark_ text, font, color, opacity, and position based on your preferences.
V

Text Processing

Text Processing Scripts are computational tools


designed to manipulate and analyze textual data
using programming languages like Python or Java.
These scripts can perform a wide range of tasks,
from simple text cleaning and formatting to more
complex operations like sentiment analysis, natural
language processing, and information extraction.
They are employed in various applications, such as
data preprocessing for machine learning, content
analysis for social media, and text summarization
for information retrieval. Text Processing Scripts
play a crucial role in automating and streamlining
text-related tasks, enabling researchers, developers,
and data scientists to e ciently handle and derive
insights from large volumes of textual information
across di erent domains.
Perform sen ment analysis on text
data
Sentiment analysis involves determining the sentiment expressed in a piece of text, such as whether it is positive,
negative, or neutral. The n l t k (Natural Language Tool kit) library in Python is commonly LJsed for sentiment analysis.
Below is an example scri pt demonstrating how to perform sentiment analysis using n l tk:

import nltk
from nltk.sentiment import SentimentintensityAnalyzer

def analyze_ sentiment (text) :


# Initialize the Sentiment Intensity Analyzer
sia = SentimentintensityAnalyzer(l

# Get the polarity scores for the text


sentiment_scores = sia.polarity_scores(text)

# Determine sentiment based on the compound score


compoLJnd_score = sentiment_scores [ 'compound' ]
i f compound_score >= 0.05 :
sentiment= 'Positive'
elif compound_score <= -0.05 :
sentiment= 'Negative'
else :
sentiment = 'Neutral'

return sentiment, sentiment_scores

if name == "_main_" ·
# Example usage:
text_to_analyze = "I love using Python for natLJral language processing!"

sentiment_result, scores= analyze_sentiment(text_to_analyze)

print( f"Text: {text_to_analyze}" )


print( f"Sentiment: {sentiment_result}" )
print( f"Sentiment Scores: {scores}" )

Before running the script, you need to install the nltk library:

pip install nltk

Additionally, yoLJ may need to download the vade r _ lexicon data LJsed by the sentiment analyzer:

import nltk
nltk.download( 'vader_lexicon' )
7
Replace t he text_to_ana lyze variable with the text yoLJ want to analyze. The scri pt will output the sentiment (Positive,
Negative, or Neutral) and the sentiment scores, inc luding the compoLJnd score that represe nts the overall sentiment.
Adjust the threshold values in the script to customize the sentiment classification based on your preferences.
Build a script for text summariza on

Text summarization is a complex task, and there are multiple approaches to achieve it. One popular method is to use the
gen s im library, which provides an implementation of the TextRank algorithm for extractive summarization. Here's an
example script:

from gensim.summarization import summarize

def summarize_text (text, ratio=0.2) :

Summarize a given text using the TextRank algorithm .

Parameters:
- text (str): The input text to be summarized .
- ratio (float): The ratio of the original text to keep in the summary (default
is 0.2) .

Returns:
- summary (str): The summarized text .
IIIIU

summary = summarize(text, ratio=ratio)


return summary

if name == "_main_" :
Example usage:
#
input_text = '"'"
GPT-3, the latest and largest language model developed by OpenAI, has gained
significant attention for its remarkable natural language processing capabilities.
The model, with its 175 billion parameters, has been applied to various tasks,
including text generation, translation, and summarization.
Text summarization, in particular, is a useful application of GPT-3, allowing
for the extraction of key information from lengthy documents.
IIIIU

summarized_text = summarize_text(input_text)

print( "Original Text:" )


print(input_text)
print( "\nSummarized Text:" )
print(summarized_text)
Before running the script, you need to install the gensim library:

pip install gensim

Replace t he input_text variable with t he text you want to summarize. The script uses the summarize fu nction from
gen s im to perform extractive summarization. The ratio parameter determines t he proportion of the original text to be
retained in the summary. Adjust this parameter as needed.
Create a spell-checker or grammar
checker
Creating a spell-checker or grammar checker involves using language processing libraries, and one popular choice is the
language_too l_python library. It's a Python wrapper for the LanguageTool API, Which can check grammar and style
issues in a given text. Here's an example script:

First, install the library:

pip ins t all language-tool-python

Now, you can create a simple spell-checker/grammar-checker script:

import language_tool_python

def check_text (text) :


IIIIH

Check grammar and spelling in the given text.

Pa ram et e rs:
- text (str): The input text to be checked.

Returns:
- matches (list): A list of grammar and spelling suggestions.

tool = language_tool_python, LanguageToo 1( 'en-US' )


matches= t ool.check(text)
return matches

def correct_text (text, matches) :


IIIIH

Correct the text based on the grammar and spelling suggestions.

Parameters:
- text (str): The input text to be corrected.
- matches (list): A list of grammar and spelling suggestions.

Returns:
- corrected_text (str): The corrected text.
IIIIH

corrected_text = language_too l_python.correct(text, matches)


return corrected_text
if name == "_main_" ·
# Example usage:
input_text = "This is an example sentence with some grammatical and spel
mistakes."

# Check the text


matches = check_text(input_text)

if matches:
print( "Grammar and spelling issues found:" )
for match in matches:
print(match)

# Correct the text


corrected_text = correct_text(input_text, matches)
print( "\nCorrected Text:" )
print(corrected_text)
else :
print( "No grammar or spelling issues found." )

Replace the input_text variable with the text you want to check. The script uses the La nguageToo l class to check
the text and provides a list of matches (grammar and spelling issues). It then uses the correct function to correct the
text based on the suggestions. Adjust the language code ("en-US' in this case) based on your requirements.
Convert text to speech or speech to
text
To conven text to speech or speech to text in Python, you can use libraries such as gTTS (Google Text-to-Speech) for
text-to-speech conversion and SpeechRecognition for speech-to-text conversion. Here's how you can do both:

Text to Speech (TTS):

Install the gTTS library:

pip install gtts

Now, you can create a simple script for text-to-speech conversion:

from gtts import gTTS


import os

def text_ to_ speech {text, language= 'en' ) :


UIIII

Convert text to speech.

Parameters:
- text {str): The input text to be converted.
- language (str): The language code (default is ' en').

Returns:
- audio_path (str): The path to the generated audio file.
HIIII

tts = gTTS(text =text, lang=language)


audio_path = 'output.mp3'
tts.save(audio_path)
return audio_path

if _ name_ -- "_main_" ·
input_text = "Hello, how are you today?"

# Convert text to speech


audio_path = text_to_speech(input_text)

# Play the generated audio file


os. system( f"sta rt {audio_path}" )

Replace input_text with the text you want to convert. The script uses gTTS to create an audio fi le (output. mp3) and
plays it using the default aud io player.
Speech to Text (STT):

Install the SpeechRecognition library:

pip install SpeechRecognition

You'll also need to instal l the pyaudio library for microphone input

pip install pyaudio

Now, create a script for speech-to-text conversion:

import speech_ recognition as sr

def speech_to_text () :

Convert speech to text using the microphone.

Returns:
- text (str): The recognized text.
IIUII

recognizer = sr.Recognizer()

with sr.Microphone() as source:


print( "Say something:" )
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)

try :
text = recognizer.recognize_google{audio)
print ( "You said:" , text)
return text
except sr.UnknownValueError:
print( "Sorry, I could not understand audio." )
return ""
except sr.RequestError as e:
print( f"Could not request results from Google Speech Recognition service;
{e}" l
return ""

if name -- "_main_" ·
# Convert speech to text
recognized_text = speech_to_text()
Generate random text for tes ng
purposes
You can use the LoremText module from the faker library to generate random text for testing purposes in Python.
First, you need to install the faker library:

pip install faker

Now, you can create a script to generate random text:

from faker import Faker

def generate_ random_text (paragraphs=3, sentences_per_paragraph=S) :


111111

Generate random text for testing purposes.

Parameters:
- paragraphs (int): Number of paragraphs (default is 3) .
- sentences_per_paragraph (int) : Number of sentences per paragraph (default is
5).

Returns:
- random_text (str): The generated random text.
lllltl

fake = Faker()
fake.seed( 0 ) # Set seed for reproducibility

random_ text = "\n" .join(


fake.paragraph(nb_ sentences=sentences_per_paragraph) for in
range(paragraphs)
)

return random_text

if name == "_main_" ·
# Generate random text
text = generate_ random_text()

# Print the generated text


print(text)

Adjust the paragraphs and sentences_pe r _paragraph parameters as needed. The Faker library allows you to
generate various types of fake data, and in this case, we're using it to create random paragrap hs. The seed ( 0) line
ensures reproducibility if you want to generate t he same random text later.
VI

File Management

File Management are automation tool developed


using programming languages like Python or Bash
to perform various tasks related to file organization,
manipulation, and maintenance. These scripts can
encompass functionalities such as sorting and
organizing files within directories, searching for
specific file types or patterns, removing duplicate
files, and monitoring changes in file directories.
Sort and organize files in a directory
To sort and organize files in a directory using Python, you can use the os and s iluti l modules. Here's a simple example
script that sorts files based on their extensions and organizes them into corresponding folders:

import os
import silutil

def organize_files (source_directory, destination_directory) :


"""
Organize files in the source directory and move them to corresponding folders
based on their file extensions in the destination directory.

Parameters:
- source_directory (str): Path to the source directory.
- destination_directory (str): Path to tile destination directory.
IIUII

# Create destination directory if it doesn't exist


if not os.path.exists(destination_directory):
os.makedirs(destination_directory)

# Iterate over files in tile source directory


for filename in os.listdir(source_directory):
source_path = os. path.join( sou rce_directory, f ilename)

# Check if it's a file


if os.path.isfile(source_patil):
# Get the file extension
_, extension = os.path.splitext(filename)

# Remove tile dot from the extension


extension = extension [l :]

# Create a folder for the extension if it doesn't exist


extension_folder = os.patil.join(destination_directory, extension)
if not os.path.exists(extension_folder):
os.makedirs(extension_folder)

# Move tile file to the corresponding folder


destination_patil = os.patll.join(extension_folder, filename)
silutil.move{source_patil, destination_path)

if name == "_main_" ·
# Specify source and destination directories
source_dir = "/path/to/source/directory"
destination_dir = "/path/to/destination/directory"

# Organize files
organize_files(source_dir, destination_dirl
Search for files with specific
extensions
To search for files with specific extensions in a directory using Python, you can use the os module. Here's a simple
scri pt that demonstrates how to do this:

import os

def search_files (directory, extensions) :


111111

Search for files with specific extensions in a directory.

Parameters:
- directory (str): Path to the directory to search.
- extensions (list): List of file extensions to look for.

Returns:
- List of file paths matching the specified extensions.
111111

matching_files = [)

# Iterate over files in the directory


for root,_, files in os.walk(directory):
for file in files:
# Check if the file has a matching extension
if any( file. lower(}. endswith (ext. lower( l) for ext in extensions):
file_path = os.path.join(root, file)
matching_files.append(file _path)

return matching_files

if name == "_main_" :
# Specify the directory and extensions to search for
search_directory = "/path/to/search/directory"
desired_extensions = [". txt" , " . pdf" , 11 • docx" I

# Search for files


result_files = search_files(search_directory, desired_extensions)

# Print the matching file paths


if result_files:
print( "Matching files:" )
for file_path in result_files:
print(file_path)
else :
print( "No matching files found." )

Replace "/path/to/search/directory" with the actual path of the directory you want to search, and modify the
des i red_extensions list to include the file extensions you"re interested in. This script wi ll recursively search for fi les
with the specified extensions in the given directory and its subdirectories.
Clean up duplicate files

Cleaning up duplicate files in a directory can be achieved by comparing file contents and removing redundant copies.
Here's a Python script that identifies and removes duplicate fi les based on their content:

import os
import hash lib

def hash_file (file_path, block_size=65536) :

Generate the hash of a file.

Parameters:
- file_path (str): Path to the file.
- block_size (int): Block size for reading the file.

Returns:
- Hexadecimal representation of the file hash.
"""
hasher = hashlib.md5()
with open(file_path, 'rb' ) as file:
buf = file.read(block_size)
while len(buf) > 0 :
hasher.update(buf)
buf = file.read(block_size)
return hasher.hexdigest()

def find_duplicate_files (directory) :


HIIII

Find duplicate files in a directory.

Parameters:
- directory (str): Path to the directory.

Returns:
- Dictionary where keys are file hashes and values are lists of file paths.

file_hash_dict = {}
duplicate_files = {}
# Iterate over files in the directory
for root, _, files in os . walk(directory):
for file in files:
file_path = os.path.join(root, file)
file_hash = hash_file(file_path)

# Check if the hash is already in the dictionary


if file_hash in file_hash_dict:
# Duplicate found
if file_hash not in duplicate_files:
duplicate_files[file_hash] = [file_hash_dict[file_hash]]
duplicate_files[file_hash ] .append{file_path)
else :
file_hash_dict[file_hash] = file_path

return duplicate_files

def remove_duplicates (duplicate_files) :


IIUH

Remove duplicate files.

Parameters:
- duplicate_files (diet): Dictionary with duplicate file information.

for file_hash, duplicates in duplicate_files.items():


# Keep the first file and remove duplicates
for duplicate in duplicates[ l :]:
os.remove{duplicatel
print{ f"Removed duplicate: {duplicate}" )

if name == "_main_" ·
# Specify the directory to search for duplicates
search_directory = "/path/to/search/directory"

# Find duplicate files


duplicates = find_duplicate_files(search_directory)

# Remove duplicates
if duplicates:
print( "Duplicate files found:" )
for file_hash, files in duplicates.items():
print ( f"Ha sh: {f ile_hash }" l
for file_path in files:
print {f" {f ile_path}" )
remove_duplicates(duplicates)
else :
print ( "No duplicate files found." )

Replace "/path/to/searc h/directory" with the actua l path of the directory you want to searc h for du plicates. This script
uses MD5 hashing to compare file contents and removes redundant copies while keeping the first occurrence. Please
use it with caution and make sure to have backups before running it on valuab:le data.
Monitor changes in file directories
Monitoring changes in file directories can be achieved using the watchdog library in Python. This library provides a
simple API for monitoring file system events. You can instal l it using:

pip install watchdog

Here's an example script that monitors a directory for file system events:

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyHandler (FileSystemEventHandler) :


def on_modified (self, event) :
if event.is_directory:
return
# File modified
print( f'File {event.src_path} has been modified' )

def on_created (self, event} :


if event. is_directory:
return
# File created
print( f'File {event.src_path} has been created' )

def on_deleted (self, event) :


if event.is_directory:
return
# File deleted
print( f'File {event.src_path} has been deleted' )

if name == "_main_" ·
path = "/path/to/directory/to/monitor"

event_handler = MyHandler()
observer = Observer()
observer.schedule{event_ handler, path, recursive=True )
observer.start{)

try :
while True :
time.sleep( l )
except Keyboardinterrupt:
observer. stop{)
observer .. join()

Replace "/path/to/directory/to/monitor" with the actual path of t he directory you want to monitor. This script will print
messages when files are modified, created, or deleted within the specified directory.

Make sure to adapt the script accord ing to your specific use case. You can extend t he MyHandle r class to include
additional event handli ng methods if needed.
Rename files in bulk according to a
pa ern
To rename fi les in bulk according to a pattern in Python, you can use the os module for interacting with the file system
and the re module for regular expressions. Here's an example script that renames files in a directory based on a
specified pattern:

import os
import re

def rename_ files (directory, pattern) :


# Get the list of files in the directory
files= os. listdir(directory}

# Compile the regular expression pattern


regex_pattern = re.compile(pattern)

for filename in files:


# Check if the filename matches the pattern
match= regex_pattern.match(filename)
if match:
# Construct the new filename based on the pattern
new_filename = match.group( l ) # Adjust this line based on your pattern
new_filepath = os.path.join(directory, new_filename}

# Rename the file


os.rename(os.path.join(directory, filename), new_filepath)
print( f'Renamed: {filename} to {new_filename}' )

if name == "_main_" ·
# Specify the directory and pattern
directory_to_rename = "/path/to/directory"
renaming_pattern = r'your_pattern_(\d+}\.txt' # Adjust this pattern based on
your needs

# Call the rename_files function


rename_files(directory_to_rename, renaming_patternl

Replace "/path/to/directory" with the actual path of the directory containing the files you want to rename. Adjust the
renaming_patte rn variable according to the pattern you want to match. The example pattern assumes you want to
extract numbers from filenames, but you should customize it based on your specific requiremen ts.

Make sure to review and understand the regular expression and adjust it to match your file naming pattern accurately.
Always back up your files before ru nning any script that modifies file names.
Chapter 31
Chapter31
VII

System Monitoring and Repor ng

System Monitoring and Reporting are powerful


tools developed using languages like Python or
Bash to track and analyze various aspects of a
computer system’s performance. These scripts
actively monitor critical system resources such as
CPU usage, memory utilization, and disk activity.
They can generate regular reports summarizing
system statistics on a daily or weekly basis,
providing insights into resource trends and
potential issues.
Monitor system resource usage (CPU,
memory, disk)
To monitor system resource usage (CPU, memory, disk) with Python, you can use third-party libraries like psutil . Here"s a
simple example script that shows how to use psutil to monitor these resources:

import psutil
import time

def monitor_resources () :
while True :
# CPU usage
cpu_percent = psutil. cpu_percent ( inte rva l=l }

# Memory usage
memory_info = psutil.virtual_memory()
used_memo ry = memory _info .. used
total_memory = memory_info.total
memory_percent = memory_info.percent

# Disk usage
disk_info = psutil.disk_usage( '/' )
used_disk = disk_info.used
total_disk = disk_info.total
disk_percent = disk_info.percent

# Print the information


print( f"CPU Usage: {cpu_percent}%'' }
print( f"Memory Usage: {used_memory / ( 1024 **
3 ): .2f} GB / {total_memory /
( 1024 ** 3 ): .2 f} GB ({memory_percent}%)" )
print( f"Disk Usage: {used_disk / ( 1024 **
3 }: .2f} GB/ {total_disk / ( 1024
** 3 ): .2f} GB ({disk_percent}%)" )

# Sleep for a while before checking again


time.sleep( S)

if name == "_main_" :
monitor_resources()

This script uses the psuti l library to retrieve CPU, memory, and disk usage information. The ps uti l. cpu_percent
function is used to get the CPU usage percentage, and the psuti l. v irtua l_memo ry and psuti l. dis k_usage
funct ions provide information about memory and disk usage, respectively.

The script ru ns in an infinite loop, printing the resource usage information every 5 seconds. You can customize the
interval or add additional functionality based on your specific requirements. To use this script, you'll need to install the
psutil library by running:

pip install psutil


Generate daily/weekly reports on
system sta s cs
To generate daily or weekly reports on system statistics using Python, you can modify the script provided earlier to write
the information to a file. Here's an example that writes daily reports to a text file:

import psutil
import time
from datetime import datetime, timedelta

def get_timestamp () :
return datetime . now().strftime( "%Y-%m--%d %H:%M:%S" )

def write_report (report_path, content} :


with open(report_path, 'a' ) as file:
file .write(content + '\n' )

def monitor_and_report (report_path) :


while True :
timestamp = get_timestamp()

# CPU usage
cpu_percent = psutil.cpu_pe r cent(interval=l )

# Memory usage
memory_info = psutil . virtual_memory()
used_memory = memory_info . used
tota l_memory = memory_info.total
memory_percen t = memory_info.percent

# Disk usage
disk_i nfo = psutil . disk_usage( '/ 1 )
used_disk = disk_info.used
tota l_disk = disk_info.total
disk_percent = disk_info. percent

# Prep a re the report content


report_content = (
f"Timestamp: {timestamp}\n"
f"CPU Usage: {cpu_percent}%\n"
f"Memory Usage: {used_memory / ( 1024 ** 3): .2f} GB / {total_memory /
( 1024 ** 3): .2f} GB ({memory_percent}%)\n"
f"Disk Usage: {used_disk / (1024 ** 3): .2f} GB/ {total_disk / ( 1024 **
3): . 2f } GB ({disk_percent}%)\n"
)

#Write the report to the file


write_ report(report_path, report_content)

# Sleep for 24 hours (86400 seconds) before generating the next report
time . sleep( 86400 )
if name == "_main_" :
daily_ report_path = 'daily_system_report.txt'
monitor_and_report (dai ly_ report_path) _J
This script continuously monitors system statistics and appends the collected information to a text file
(dai ly_system_report. txt ). You can adjust the file path and customize the report content as needed.

Keep in mind that this script will run indefinitely, generating a report once per day. You can modify the sleep duration to
adjust the reporting frequency. Additiona ll y, you may want to consider using a more structured format for the reports,
such as CSV or JSON , depending on your reporting needs.
Monitor network traffic and generate
reports
Monitoring network traffic and generating repons can be done using Python with the help of external libraries such as
psuti l and scapy. Below is a basic example to get you started. Please note that you need to install the psuti l and
s capy libraries if you haven't already:

pip install psutil scapy

Now, you can use the following Python script to monitor network traffic and generate reports:

import psutil
from scapy.all import sniff

def get_network_usage () :
# Get current network statistics
network_stats = psutil.net_io_counters()
bytes_sent = network_stats.bytes_sent
bytes_ recei ved = network_stats.bytes_ recv

return bytes_sent, bytes_ received

def packet_callback (packet) :


# Handle each packet received during sniffing
if packet.haslayer( "IP" ):
# Extract source and destination IP addresses
s rc_ip = packet[" IP" l • s re
dst_ip = packet ["IP" ].dst

# You can perform further analysis or logging here

def monitor_network (report_path) :


while True :
# Get network usage before sniffing
initial_sent, i nitial_ received = get_network_usage()

# Sniff packets for a specific duration (e.g., 60 seconds)


sniff(prn =packet_callback, store=0, timeout=60 )

# Get network usage after sniffing


final_sent, final_ received = get_network_usage(l

# Calculate the difference in bytes


sent_diff = final_sent - initial_sent
received_diff = final_ received - initial_ received
# Prepare the report content
report_ content = (
f"Bytes Sent: {sent_diff}\n"
f"Bytes Received: {received_diff}\n"

# Write the report to the file


with open(report_path, 'a' ) as file:
f ile.write(report_ content + '\n' }

if name == "_main_" ·
network_ report_path = 'network_traffic_report.txt'
monitor_network(network_ report_ path)

This script continuously monitors network traffic for a specified duration (60 seconds in this case} and logs the
difference in bytes sent and received during that period. It uses the psuti l library to obtain initial and final network
statistics and scapy for packet sniffing.

Note: Sniffing network traffic may require elevated privileges. Ensure t hat you have the necessary permissions to
capture packets on your network. Also, this script provides a basic example, and you may need to customize it based on
your specific reporting requirements.
Create a script to log and analyze
system events
To log and analyze system events in Python, you can use the psuti l library to gather system information and the
logging module to create log entries. Here's a simple script that logs CPU and memory usage at regular intervals:

import psutil
import logging
import t ime

# Configure logging
logging.basicConfig(filename= 'system_events.log' , level=logging.INFO, format = '%
(asctime)s [%(levelname)s] %(message)s' )

def log_ system_ stats () :


# Get CPU and memory usage
cpu_percent = psutil.cpu_percent(interval=l )
memory_ stats = psutil.virtual_memory()

# Log the information


logging.info{ f"CPU Usage: {cpu_percent}% I Memory Usage:
{memory_stats.percent}%" )

i f _ name_== "_main_" ·
try :
# Log system stats every 5 minutes
while True :
log_ system_ stats()
time.sleep( 300 ) # 300 seconds= 5 minutes

except Keyboardinterrupt:
print ( "Logging stopped." l

This script logs CPU and memory usage information every 5 minutes and stores it in a file na med system_events. log.
You can customize the logging interval and add more system metrics as needed.

To analyze the logs, you can use various tools or write additional Python scripts to parse and extract specific information
f rom the log file.

Keep in mind that this is a basic example, and you might want to extend it based on your specific needs. Additionally, you
can explore more advanced logging frameworks like logu ru or integrate with external services for log ana lysis.
Build a script to track and no fy
about system up me
To track and notify about system uptime using Python, you can create a script that periodically checks the system
uptime and sends notifications when certain conditions are met. Here's a simple example using the psuti l library for
system information and the smtp lib library for sending email notifications:

import psutil
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import time

# Email configuration
sender_email = "[email protected]"
receiver_email = "recipient_emai l@gmai l. corn"
email_password = "your_email_password"

def send_notification (subject, message) :


# Setup the MIME
message= MIMEMultipart()
message[ 'From' l = sender_email
message[ 'To' l = receiver_email
message[ 'Subject' ! = subject

# Attach the message body


body= MIMEText(message)
message.attach(body}

# Connect to the SMTP server


with smtplib.SMTP( "smtp.gmail .com" , 587 ) as server:
server.starttls()
server. login(sender_email, email_password)

# Send the email


server. sendmail( sender_email, receiver_email, message ..as_st ring ())

def track_system_uptime () :
# Notify if the system uptime is greater than 24 hours
while True :
uptime= psutil.boot_time()
current_time = time.time()
uptime_hours = (current_time - uptime) / 3600

if uptime_hours >= 24 :
subject = "System Uptime Notification"
mes sage = f"System has been running for { uptime_hou rs : • 2f} hours."
send_notification(subject, message}

time.sleep( 3600 ) # Check every hour


if name ==
"_main_" ·
track_ system_uptime()

This script checks t he system uptime every hour and sends an email notification if the uptime exceeds 24 hours . Make
sure to re place the place holder email addresses and password with your actual email credentials.

Note: For security reasons, consider using environment variables or a conf iguration file to store sensitive information like
email credentials.

Additionally, you may explore other notification mechanisms, such as sending messages via a messaging service (e.g.,
Telegram, Slack) or using a desktop notification library. Adjust the script according to your preferred notification method.
VIII

Games and Entertainment

Games and Entertainment Scripts are dynamic


applications designed to provide users with
engaging and enjoyable experiences. Developed
using languages such as Python or JavaScript, these
scripts cover a wide range of interactive content,
including text-based games, simulations, and
multimedia entertainment. They often incorporate
elements of graphics, sound, and user interaction to
create immersive virtual environments.
Create a simple text-based game

This is a guessing game where the player needs to guess a randomly generated number:

import random

def guess_the_number (} :
print( "Welcome to the Guess the Number game!" )
print( "I'm thinking of a number between 1 and 100." )

# Generate a random number between 1 and 100


secret_ number = random.randint( l , 100 )

attempts = 0
while True :
# Get the player's guess
guess = int(input( "Your guess: " ))
attempts+= 1

# Check i f the guess is correct


if guess == secret_number:
print( f"Congratulations! You guessed the number in {attempts}
attempts." )
break
elif guess< secret_number:
print( "Too low. Try again." )
else :
print( "Too high. Try again .." )

if name == "_main_" :
guess_the_number()

Copy and paste this code into a Python fi le (e.g., guessing_game. py) and run it. The player will be prompted to guess
the randomly generated number, and the game will provide hints if the guess is too high or too low. The game continues
until the correct number is guessed.

Feel f ree to modify the game or add more features to make it more interest ing!
Build a script to generate random
jokes or facts
Here's a simple Python script that generates random jokes and facts. This example uses predefined lists of jokes and
facts, and it ra ndomly selects one each time the script is run:

import random

def generate_ random_j oke () :


jokes = I
"Why don't scientists trust atoms? Because they make up everything!" ,
"I told my wife she should embrace her mistakes. She gave me a hug." ,
"Parallel lines have so much in common. It's a shame they'll never meet ." ,
"Why did the scarecrow win an award? Because he was outstanding in his
field!" ,
"I used to play piano by ear, but now I use my hands and fingers." ,

return random ..choice (jokes)

def generate_ random_fact () :


facts = I
"Honey never spoils. Archaeologists have found pots of honey in ancient
Egyptian tombs that are over 3,000 years old and still perfectly edible." ,
"The Eiffel Tower can be 15 cm taller during the summer, due to the
expansion of the iron in the heat." ,
"Bananas are berries, but strawberries aren't." ,
"Cows have best friends and can become stressed when they are separated ." ,
"A group of flamingos is called a 'flamboyance.'" ,

return random.choice(facts)

if name == "_main_" ·
j oke_o r _ fact = random. choice { I" joke" , "fact" 1)

if joke_or_fact = "joke" :
print( "Here's a random joke for you:" )
print(generate_ random_ joke())
else :
print( "Here's a random fact for you:" )
print(generate_ random_ fact())

Copy and paste this code into a Python fi le (e.g., random_joke_or_ fact. py) and run it. It will print either a random
joke or a random fact each time you run the script.
Design a quiz or trivia game

Creating a quiz or trivia game in Python can be a fun project! Here's a simple example using Python :

import random

class Quiz :
def _init_ (self, questions) :
self.questions = questions
self.score = 0

def ask_question (self, question) :


print(question[ "question" ])
for i, option in enumerate(question ["options" ), start =l ):
print ( f" {i}. {opt ion}" )

user_answer = input( "Your answer (enter the option number):" )


if user_answer.isdigit() and 1 <= int(user_ answer) <=
len(question[ "options" ]):
user_answer_ index = int(user_answer) - 1
if question ["options" l [user _answer_ index] -- question ["answer" I :
print ("Correct! \n" )
self.score += 1
else :
print( f"Wrong! The correct answer is: {question['answer')}\n" )
else :
print( "Invalid input. Please enter a valid option number.\n" )

def start_quiz (self) :


random.shuffle(self.questions)
for question in self.questions:
self.ask_question(question)

print( f"Quiz completed! Your score: {self.score}/{len(self.questionsl}" )


if name == "_main_" ·
# Define your quiz questions
quiz_questio ns = [
{
"question" : "What is the capital of France?" ,
"options" : ["Berlin" , "Madrid" , "Paris" , "Rome" ],
"answer" : "Paris" ,
},
{
"question" : "Which planet is known as the Red Planet?" ,
"options" : ["Venus" , "Mars" , "Jupiter" , "Saturn" ),
"answer" : "Mars" ,
},
# Add more questions as needed

# Create an instance of the Quiz class


my_quiz = Quiz(quiz_questions)

# Start the quiz


my_quiz.start_ quiz()

Copy and paste this code into a Python file (e.g ., quiz_game. py) and ru n it. You can customize the questions, options,
and correct answers in the quiz_questions list. The game will shuffle the questions and ask the user to select the
correct option for each question .

Feel free to expand and customize the quiz with more features, difficulty levels, or categories!
Develop a script for genera ng
random art
Creating a script for generating random art in Python can be a creative and fun project. One approach is to use the
turtle module, which provides a simple way to draw shapes and patterns. Here's a basic example:

import turtle
import random

def random_color () :
return (random. random ( ) , random . random (), random . random ( ) )

def draw_ random_ art () :


turtle.speed( 2 )
turtle. bgco lo r( "b lack" )

for _ in range( 50 ):
turtle.color(random_color())
draw_ random_ shape()

turtle.done()

def draw_ random_ shape () :


shape = random. choice( ["circle" , "square" , "triangle" ])
size = random . randint( 10 , 100 )
x = random. randint( -300 , 300 )
y = random.randint( - 300 , 300 )

turtle . penup()
turtle.goto(x, y)
turtle. pendown()

if shape== "circle" :
turtle.circle(size)
e lif shape = "square" :
for _ in range( 4 ):
turtle.forward(size)
turtle.right( 90 )
elif shape ="triangle" :
for _ in range( 3 ):
turtle.forward(size)
turtle.right( 120 )

if name == "_main_" :
draw_ random_art()

This script uses the tu rt le module to draw random shapes (circles, squares, or triangles) with random colors. The
random_color function generates a ra ndom RGB color. You can customize this script by adding more shapes,
adjusting sizes, or incorporating different drawing techniques.
Create a script to simulate rolling dice

import random

def roll_ dice (num_rolls=l, num_sides=6) :


results = []
for _ in range(num_rolls):
result= random.randint( l , num_sides)
results. append (result)
return results

def main () :
num_rolls = int(input( "Enter the number of times you want to roll the dice: " ))
num_sides = int(input( "Enter the number of sides on the dice: " ) l

results= roll_dice(num_rolls, num_sides)

p rint( "Results: " )


for i, result in enumerate(results, start=l ):
print( f"Roll {i}: {result}" )

i f _name_ -- _main_" :
main( l

This script defines a ro l l_d ice function that takes the number of rolls and the number of sides on the dice as
parameters and returns a list of results. The ma in function takes user input for the number of rolls and sides, calls the
ro l l_dice function, and prints the results.

To run this script, copy and paste it into a Python environment, execute it, and follow the prompts to input the number of
rolls and sides on the dice. The script wil l then simulate rolling the dice and display t he re sults.
IX

U lity

Utility Scripts are versatile tools developed to


streamline various tasks, enhance e ciency, and
automate routine processes. Crafted using
programming languages like Python or Bash, these
scripts address a broad spectrum of functionalities,
ranging from file management and data analysis to
system monitoring and automation. Utility Scripts
aims to simplify complex operations, minimize
manual e orts, and contribute to a smoother
workflow.
Calculate and convert units (e.g.,
currency exchange rates)

To calculate and convert units, including currency exchange rates, you can use various Python libraries. Here's a simple
example using the forex-python library for currency conversion :

First, you need to install th e library:

pip install forex-python

Now, you can use t he following Python script:

f ram fo rex_ python. converter import Cur rencyRates

def convert_currency (amount, from_currency, to_currency) :


c = CurrencyRates()
exchange_ rate = c.get_rate(from_ currency, to_currency)
converted_ amount = amount* exchange_ rate
return converted_amount

def main () :
amount = float(input( "Enter the amount: " ))
from_ currency = input( "Enter the source currency code: " ).upper()
to_ currency = input( "Enter the target currency code: " ) . upper()

result = convert_ currency(amount, from_ currency, to_ currency)

print( f"{amount} {from_currency} is equal to {result: .2 f} {to_currency}" )

i f _ name_ -- _main_" ·
main(}

This script defines a f unction convert_currency t hat takes an amount, source currency, and target currency, and
retu rns the converted amount. The ma in function takes user input for the amount and currencies and then displays the
converted amount.
Create a script for genera ng strong
passwords
import random
import string

def generate_password (length=12) :


# Define character sets for the password
lowercase_letters = string.ascii_lowercase
uppercase_letters = string.ascii_uppercase
digits= string.digits
special_characters = string . punctuation

#Combine character sets


a ll_characters = lowercase_ letters + uppe rcase_ letters + digits +
special_characters

# Ensure at least one character from each set


password = I
random.choice(lowercase_letters),
random.choice(uppercase_letters),
random.choice(digits),
random.choice(special_characters),

# Fill the rest of the password with random characters


remaining_length = length - len(password}
password. extend (random.choice (a ll_characters l for in range( remaining_ length) )

# Shuffle the password to randomize character positions


random.shuffle( password)

# Convert the list of characters to a string


return '' ,join(password)

def main () :
# Set the desired password length
password_length = 16

# Generate a strong password


password= generate_password(password_length)

print( f"Generated Password: {password}" )

if _name_ -- _main_" ·
main()

This script defines a function generate_pas sword that creates a strong password by combining lowercase letters,
uppercase letters, digits, and special characters. The main function sets the desired password length (you can modify
it), generates a password, and prints it.
Build a simple calculator
def add {x, y) :
return X + y

def subtract Ix, y) :


return x - y

def multiply (x, y) :


return x y*
def divide (x, y) :
if y != 0 :
return x / y
else :
return "Error: Division by zero"

def calculator () :
print( "Simple Calculator" )
print( "Select operation:" )
print( "l. Add" )
print( "2. Subtract" )
p r int( "3. Multiply" }
print( "4. Divide" }

choice = input( "Enter choice (1/2/3/4): " )

if choice in { '1' , '2' , '3' , '4' } :


nu ml = float(input( "Enter first number:" })
num2 = float ( input ( "Enter second number: " ) }

if choice == '1' :
print( f"{numl} + {num2} = {add(numl, num2}}" )
elif choice = '2' :
print( f"{numl} - {num2} = {subtract(numl, num2)}" )
elif choice = '3' :
print( f"{numl} *
{num2} = {multiply(numl, num2)}" )
elif choice = '4' :
print( f"{numl} / {num2} = {divide(numl, num2)}" )
else :
print( "Invalid input. Please enter a valid number (1/2/3/4)." )

if _ name_== "_main_" ·
calculator()

This script defines basic arithmetic functions (add , subtract, multiply, and divide) and a ea lcu lato r fu nction
that takes user input to perform the selected operation. It includes input validation to handle invalid choices or division
by zero.

Run the script, and it will prompt you to select an operation and enter two numbe rs. It will then perform the chosen
operation and display the result.
Convert between different file
formats (e.g., PDF to text)

To convert a PDF to text in Python, yoLJ can LJse t he PyPDF2 library. First, you"ll need to install the library using:

pip install PyPDF2

Now, you can LJse the fo llowing scri pt to convert a PDF to text:

import PyPDF2

def pdf_to_text (pdf_path) :


tex t = ""
with open(pdf_path, "rb" ) as file:
pdf_ reader = PyPDF2.PdfFileReader(file)
num_pages = pdf_ reader.num Pages

for page_nLim in range(num_ pages):


page = pdf_ reader.getPage(page_num)
text += page ..extractText ()

return text

if name == "_main_" ·
pdf_ path = "path/to/your/file.pdf" # Replace with the path to yoLir PDF file
extracted_ text = pdf_to_ text(pdf_ path)

print(extracted_text)

Replace "path/to/your/file. pdf" with the actual path to your PDF file. This script uses PyPDF2 to extract text
from each page of the PDF and concatenates it into a single string.

Keep in mind that the text extraction might not be perfect, especially for complex PDFs with images or non -standard
fonts. For more advanced PDF processing, yo LI might want to explore other libraries like pdfmine r or PyMuPDF
(M uPDF).
Implement a URL shortener
Creating a URL shortener involves generating a unique code for each URL and storing the mapping between the code
and the origi nal URL. Below is a simple example of a URL shortener using Python and the Flask web framework. You'll
need to install Flask first:

pip install Flask

Now, you can create a basic URL shortener with the following code:

from flask import Flask, render_template, request, redirect


import string
import random

app = Flask( _ name_ )

# Dictionary to store the mapping between short codes and URLs


url_mapping = {}

# Function to generate a random short code


def generate_short_code () :
characters = string.ascii_letters + string.digits
return '' .join(random.choice{characters) for i in range( 6 ))

@app. route('/'}
def index ( l :
return render_template( 'index.html' )

@app.route('/shorten', methods=['POST'])
def shorten ( ) :
original_url = request ..form.get( 'url' )

Check if the URL is already shortened


#
for short_code, url in url_mapping . items():
if url = original_url:
return render_temp late( ' index. html' , short_url=f"{ request. host_url}
{short_code}" )

# Generate a new short code


short_code = generate_short_code()

# Store the mapping


url_mapping[short_code] = original_url

short_url = f"{request ..host_url}{short_code}"

return render_template( 'index .html' , short_url=short_url)


@app.route('/<short_code>')
def redirect_to_ original (short_code) :
# Redirect to the original URL i f the short code exists
original_url = url_mapping.get(short_ code)
i f original_ url:
return redirect(original_url)
else :
return render_template( 'index.html' , error="Short URL not found" }

if name -- _ main_ ' ·


app.run{debug=True l

Save t his script as app. py . This example uses Flask for the web application. Run the script, and the URL shortener will
be available at http:/ /127. 0. 0.1: 5000/.

This is a basic implementation, and it's essential to handle issues like collisions (two URLs generating the same short
code) and persistence (storing the mappings in a database for durability). For a production environment, consider using
a database like SQLite or a more robust web fra mework.
X

Network and Internet

Network and Internet Scripts play a crucial role in


managing, optimizing, and securing online
connectivity and data transfer. These scripts are
developed using programming languages like
Python or Perl to automate tasks related to network
administration, monitoring, and interaction with
web services. They can be employed for a variety of
purposes, such as monitoring website availability,
analyzing network tra c, performing security-
related tasks like port scanning, and automating
interactions with web APIs.
Ping mul ple hosts to check their
status
To ping multiple hosts and check their status using Python, you can use the ping3 library. First, you'll need to install the
library:

pip install ping3

Then, you can use the following Python script to ping multiple hosts:

import subprocess
from ping3 import ping, verbose_ ping

def ping_hosts (hosts) :


results = {}

for host in hosts:


try :
response = ping(host, timeout=2 )
if response is not None :
results [host] = "On line"
else :
results [ host] = "Offline"
except Exception as e:
results [host] = f"Error: {str(el}"

return results

i f _ name_ == "_main_" ·
# List of hosts to ping
host_list = [ "google.com" , "example.corn" , "nonexistenthost123.com" ]

# Ping the hosts and get the results


results = ping_hosts(host_ list)

# Display the results


for host, status in results.items():
print( f"{host}: {status}" )

This script defines a function ping_hos t s that takes a list of hostnames or IP addresses, pings each host, and returns a
dictionary indicating whether each host is online or offline.

Note that the ping3 library may not work on all operating systems due to differences in the availability of ICMP ping
requests. If you encounter issues or limitations, you might need to consider other approaches, such as using the
s ubp rocess module to call the system"s ping command. Keep in mind that running subprocess commands may have
security implications, so make sure to validate and sanitize inputs if they come from untrusted sources.
Monitor website availability and
response mes
To monitor website availability and response times with Python, you can use the requests library to send HTTP
requests and measure the time it takes to receive a response. Additionally, you can schedule these checks at regular
intervals using a library like schedule to create a basic website monitori ng script. Here"s a simple example :

import requests
import schedule
import time

def check_website (url) :


try :
# Record the start time
start_ time = time.time()

# Send a GET request to the website


response = requests.get(url)

# Calculate the response time


response_time = time.time() - start_ time

# Print the results


print( f"Website: {url}" )
print( f"Status Code: {response.status_code}" )
print( f"Response Time: {response_time: .2 f} seconds" )
print( '"' l

except requests.exceptions.RequestException as e:
print( f"Error: {e}" l
print( '"' )

def job () :
# Replace 'https://example.com' with the URL you want to monitor
website_url = 'https://example.com'

# Check the website


check_website(website_url)

# Schedule the job to run every 5 minutes


schedule.every( S).minutes.do(job)

if name == "_main_" ·
# Run the scheduled jobs
while True :
schedule.run_pending()
time.sleep( l )

This script defines a check_websi te function that sends a GET request to the specified URL using the requests
library, measures the response time, and pri nts t he results. The job function is scheduled to run every 5 minutes using
the schedule library.
Retrieve and analyze website headers
To retrieve and analyze website headers with Python, you can use the requests library to send HTTP requests and
inspect the response headers. Additionally, you can use the http. client library for more detailed analysis. Here's an
example script:

import requests
import http.client

def get_headers (url) :


try :
# Send a HEAD request to retrieve headers only
response = requests.head(url)

# Print the status code


print( f"Status Code: {response.status_code}" )

# Print the response headers


print( "\nResponse Headers:" )
for key, value in response.headers.items():
print( f"{key}: {value}" )

# Analyze headers using http.client


analyze_headers(response.headers)

except requests.exceptions.RequestException as e:
print( f"Error: {e}" )

def analyze_headers (headers) :


# Extract and analyze specific headers
server = headers.get( 'server' , 'N/A' )
content_type = headers.get( 'content-type' , 'N/A' )

print( "\nAnalysis:" )
pr i nt( f"Server: {server}" )
print( f "Content Type: {content_type}" )

if name == "_main_" ·
# Replace 'https://example.com' with the URL you want to analyze
url = 'https://example.com'

# Retrieve and analyze headers


get_headers(url)

This script defines a get_headers function that sends a HEAD request to the specified URL using the requests
library. It then prints the status code and response headers. The ana lyze_headers functio n extracts and analyzes
specific headers, such as the server and content type.
Create a port scanner to check for
open ports
To create a basic port scanner in Python, you can use the socket library to attempt connections to different ports on a
target host. Here's a simple example of a port scanner:

import socket

def scan_ ports (target_host, start_port, end_port) :


print( f"Scanning ports on {target_host} ••. \n" }

# Loop through the specified range of ports


for port in range(start_port, end_port + 1 ):
# Create a socket object
sock = socket.socket(socket.AF_ INET, socket.SOCK_STREAM}
sock.settimeout( l ) #Seta timeout for the connection attempt

# Attempt to connect to the target host and port


result = sock.connect_ex{(target_host, port))

# Check i f the connection was successful


if result == 0 :
print ( f" Po rt {po rt} is open" )
else :
print( f"Port {port} is closed" )

# Close the socket


sock. close()

if name == "_main_" ·
# Replace 'example.corn' with the target host
target_ host = 'example.corn'

# Specify the range of ports to scan (e.g., from 1 to 100)


start_port = 1
end_port = 100

# Perform the port scan


scan_ports(target_host, start_port, end_port)

Replace the ta rget_host variable with the target host's IP address or domain name. Specify the range of ports you
want to scan by setting the sta rt_po rt and end_po rt variables.

Run the script, and it will attempt to connect to each port in the specified range. If the connection is successful, it will
print that the port is open; otherwise, it will indicate that the port is closed.

Keep in mind that scanning ports without perm ission is against the terms of service of many networks, and unaut horized
port scanning may be illegal. Always ensure that you have the necessary permissions before scanning ports on a
network.
Automate interac ons with web APIs
To automate interactions with web APls in Python, you can use the requests library. Below is a simple example
demonstrating how to make a GET request to an API and handle the JSON response:

import requests

def fetch_data (api_url) :


try :
# Make a GET request to the API
response = requests.get(api_url)

# Check i f the request was successful (status code 200)


if response.status_ code == 200 :
# Parse the JS~ response
data = response.json()
return data
else :
print( f"Error: {response.status_code}" )
except Exception as e:
print( f"An error occurred: {e}" )

i f _ name_== "_main_" ·
# Replace 'https://api.example.com/data' with the actual API URL
api_ url = 'https://api.example.com/data'

# Fetch data from the API


api_data = fetch_ data(api_ url)

if api_data:
# Process and use the retrieved data as needed
print( "API Data:" )
print(api_data)

Replace the api_u rl variable with the actual URL of the API you want to interact with. The fetch_data function sends
a GET request to the specified API URL, checks if the request was successful, and then parses and returns the JSON
data.

Note: Some APls may require authentication or additional parameters in the request. In such cases, you may need to
include headers, API keys, or other authentication details in the request.

Make sure to read the API documentation to LJnderstand the required para meters and any aLJthentication mechanisms.
Additional ly, consider error handling and handling rate limits as part of your script.
XI

Security

Security scripts are essential tools in the realm of


cybersecurity, designed to automate and enhance
various aspects of digital defense. These scripts play
a crucial role in safeguarding computer systems,
networks, and data by automating tasks such as
vulnerability scanning, penetration testing, log
analysis, and intrusion detection.
Encrypt and decrypt files
To encrypt and decrypt fi les in Python, you can use the cryptography library, which provides a high-level interface for
various cryptographic operations. Below is an example of how you can encrypt and decrypt a file using the Fernet
symmetric key encryption scheme:

from cryptograpny.fernet import Fernet

def generate_key () :
# Generate a key for encryption
return Fernet.generate_key()

def write_key_to_file ( key, filename= "secret. key" ) :


# Write tne key to a file
with open( f ilename, "wb" ) as key_file:
key_file.write(key)

def load_key_from_file (filename="secret. key" ) :


# Load the key from a file
return open ( f ilename, "rb" ). read()

def encrypt_file (file_path, key) :


# Encrypt tne contents of a file
cipher= Fernet(key)

with open(file_patn, "rb" ) as file:


data= file.read()

encrypted_data = cipher.encrypt(data)

with open(file_patn + ".encrypted" , "wb" ) as encrypted_file:


encrypted_file.write(encrypted_data)

def decrypt_file (encrypted_file_path, key) :


# Decrypt tne contents of an encrypted file
cipher= Fernet(key)

with open(encrypted_file_path, "rb" } as encrypted_file:


encrypted_data = encrypted_file.read()

decrypted_data = cipher.decrypt(encrypted_data)

with open(encrypted_file_path. rstrip( " .encrypted" }, 11 wb 11 } as decrypted_file:


decrypted_file.write(decrypted_data)
if name == "_main_" ·
# Generate a key and write it to a file
key= generate_key()
write_key_to_file(key)

# Encrypt a file using the generated key


file_to_encrypt = "example.txt"
encrypt_file(file_to_encrypt, key}
print (f"File '{file_to_encrypt}' encrypted." )

# Decrypt the encrypted file using the same key


encrypted_file_to_decrypt = "example.txt.encrypted"
decrypt_file( encrypted_f i le_to_dec rypt, key)
print (f"File '{encrypted_file_to_decrypt}' decrypted." )

In this example, the generate_key function generates a key, and the write_key_to_fi le and
load_key_ from_ file functions handle writing and loading the key to and from a file, respectively. The
encrypt_ file and dee rypt_ file functions use Fernet to perform the encryption and decryption operations.

Make sure to keep the encryption key secure since it's necessary for both encryption and decryption. Additionally,
consider using more advanced key management practices for production-leve l applications.
Create a simple password manager
Creating a secure and reliable password manager involves severa l considerations, and for production use, it's
recommended to use established libraries and follow security best practices. However, for educational purposes, I can
provide a simple example of a password manager using Python with basic file storage. Note that this examp le is not
suitable for real-world use.

import json
from cryptography. fe rnet import Fernet

class PasswordManager :
def _init_ (self, master_password, key_fi le="key. key" , data_file="data. j son" ) :
self.key_file = key_file
self.data_file = data_file

# Load or generate encryption key


self.key= self. load_key() or self.generate_key()

# Initialize Fernet cipher with the key


self.cipher= Fernet(self.key}

# Unlock the password manager


self.unlock(master_password)

def generate_key (self) :


# Generate a key and save it to the key file
key= Fernet.generate_key()
with open (self. key_file, "wb" ) as key_file:
key_file.write(key)
return key

def load_key (self) :


try :
# Load the key from the key file
return open(self.key_file, "rb" ).read()
except FileNotFoundError:
return None

def unlock (self, master_password) :


# Check if the master password is correct
if self.encrypt{master_password.encode(l) - self.load_key(}:
print( "Password manager unlocked ." )
else :
raise ValueError( "Incorrect master password." )
def encrypt (self, data) :
# Encrypt data using the Fernet cipher
return self . cipher.encrypt(data}

def decrypt (self, data) :


# Decrypt data using the Fernet cipher
return self ..cipher. decrypt ( data}

def save_data (self, data} :


# Encrypt and save data to the data file
encrypted_data = self.encrypt(json.dumps(data}.encode())
with open(self.data_file, "wb" ) as data_file:
data_file.write(encrypted_data}
print( "Data saved." )

def load_data (self) :


try :
# Load and decrypt data from the data file
with open(self.data_file, "rb" } as data_file:
encrypted_data = data_file.read()
decrypted_data = self,decrypt(encrypted_data)
return json.loads(decrypted_data}
except Fi leNot FoundE r ro r .:
return {}

def get_password (self, service) :


# Get the password for a specific service
data = self ..load_data (}
return data. get( service, "Service not found." }

def set_password (self, service, password) :


# Set or update the password for a service
data= self.load_data(}
data[service] = password
self.save_data(data)
print( f"Password for '{service}' set or updated." )

if name == "_ main_ " ·


# Example usage
master_password = input( "Enter your master password: " }

# Create an instance of the PasswordManager


password_manager = PasswordManager(master_password)

# Set or update passwords


password_manager.set_password( "example. cam" , "strongpassword123" )
passwo rd_manage r. set_passwo rd ("another-service" , "secret password" )

# Get passwords
p rint( "Passwo rd for example. corn:" , password_manage r. get_password ("example. cam" ))
p rint( "Passwo rd for non-existent service:" , passwo rd_manager. get_passwo rd ( "non-
existent-service" ))

This script uses the cryptography library for Fernet encryption and handles the master password verification,
password storage, and retrieval. Keep in mind that this is a simple example and lacks many security features found in
professional password managers. Always use reputab le password management tools for actual use.
Generate and verify digital signatures

To generate and verify digital signatures in Python, you can use the cryptography library, which provides a secure way
to work with cryptographic operations. Below is an example of how to generate and verify digital signatures using
cryptography:

from cryptography.hazmat.backends import default_backend


from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding

def generate_key_pair () :
# Generate an RSA key pair
private_key = rsa.generate_private_key{
public_exponent=65537 ,
key_size=2048 ,
backend=default_backend()

public_key = private_key.public_key()

# Serialize the public key to PEM format


public_key_pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.Publicformat.SubjectPublicKeyinfo

return private_key, public_key_pem

def sign_message {private_key, message) :


# Sign a message using the private key
signature = private_key.sign(
message,
padding. PSS (
mgf=padding.MGF1{hashes.SHA256{)),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()

return signature
def verify_signature (public_key_pem, message, signature) :
# Deserialize the public key from PEM format
pub lic_ key = serialization. load_pem_pub lic_ key ( pub lic_ key_ pem,
backend=default_ backend()}

try :
# Verify the signature
public_ key.verify{
signature,
message,
padding.PSS{
mgf=padding.MGF1(hashes.SHA256{)),
salt_ length=padding.PSS.MAX_ LENGTH
),
hashes. SHA256()

print ("Signature is valid . 11 }


return True
except Exception as e:
print( f 11 Signature verification failed: {e} 11 }
return False

if name -- _main_" :
# Example usage
private_ key, public_ key = generate_ key_pair()

#Message to be signed
message = b11 Hello, this is a signed message . 11

# Sign the message


signature = sign_ message(private_ key, message}

# Verify the signature


verify_signature(public_ key, message, signature)

This script generates an RSA key pair, signs a message with the private key, and then verifies the signature using the
corresponding public key. Note that this is a basic example, and in real-wo rld scenarios, you should handle key storage
and management securely.
Build a script for secure file dele on
To securely delete a file in Python, you can use the shuti l library along with the os libra ry. Here"s a simple script that
overwrites the file with random data before deleting it:

import os
import shutil
import random

def secure_delete (file_path, passes=3) :


# Function to overwrite the file with random data
def overwrite_with_random (file_path) :
with open(file_ path, 'wb' ) as file:
file.write(os.urandom(os.path.getsize(file_ path)))

# Check if the file exists


if not os.path.exists(file_path):
print( f"File not found: {file_path}" )
return

# Overwrite the file with random data multiple times


for _ in range(passes):
overwrite_with_random(file_path)

# Delete the file


os.remove(file_path)
print( f"Securely deleted: {file_path}" )

if name == "_main_" ·
# Example usage
file_to_delete = 11 path/to/your/file.txt 11

# Specify the number of passes {default is 3)


num_ passes = 3

# Securely delete the file


secure_delete(file_ to_delete, passes=num_passes)

This script defines a secu re_de lete fu nction that overwrites the content of the file with ra ndom data multiple times
before finally deleting it using os. remove() . The numbe r of passes can be adjusted based on your security
requirements.

Note: Keep in mind that secure deletion is a complex topic, and the effectiveness of this method may depend on various
fa ctors, including the file system and storage medium. Always be cautious whe n handling sensitive data and consider
consulting security expens for critical applications.
Create a basic firewall rule manager
Creating a basic firewall rule manager with Python would typically involve interacting with the underlying firewa ll
software on your system. Below is a simplified example using Python to manage iptables rules on Linux. Please note that
this example assumes you have the necessary permissions to manipulate firewall rules.

import subprocess

def add_ firewall_ rule {rule) :


# Add a new iptables rule
subprocess.run([ 'iptables' , '-A' , 'INPUT' , '-p' , 'tcp' , '--dport' ,
str(rule[ 'port' ]), '-j' , 'ACCEPT' ])

def remove_firewall_rule (rule) :


# Remove an iptables rule
subprocess.run([ 'iptables' , '-D' , 'INPUT' , '-p' , 'tcp' , '--dport' ,
str(rule[ 'port' ]l, '-j' , 'ACCEPT' ])

def display_firewall_ rules () :


# Display current iptables rules
subprocess.run([ 'iptables' , '-L' , '-n' , '-v' ll

if name == "_main_" ·
# Example usage
rule_to_add = { 'port' : 80 }

# Add a new firewall rule


add_ firewall_ rule(rule_to_ add)

# Display current firewall rules


display_ firewall_ rules()

# Remove the added firewall rule


remove_ firewall_ rule(rule_ to_add)

# Display updated firewall rules


display_ firewall_ rules()

In this example:

• add_firewa l l_rule: Adds a new iptables rule to allow incoming traff1c on a specified port.
• remove_ f irewa l l_rule: Removes a specific iptables rule.
• display_firewall_rules: Displays the current iptables rules.

Remember that this is a basic example, and you may need to adapt it based on your specific requirements and the
firewall software you are using. Additionally, manipulating firewall rules requires elevated privileges, so ensure your script
is run with the appropriate permissions.

Also, keep in mind that directly manipulating firewal l rules can have security implications, so use caution and consider
security best practices when implementing such functionality.
XII

IoT and Hardware Control

IoT and Hardware Control Scripts are essential tools


for managing and interacting with Internet of
Things (IoT) devices and hardware components.
These scripts, often developed using languages like
Python, enable users to automate and control smart
devices, sensors, and other hardware entities. They
facilitate tasks such as turning lights on or o ,
adjusting thermostat settings, or collecting and
displaying sensor data by interfacing with
microcontrollers like Arduino.
Build scripts to control IoT devices
(e.g., lights, thermostats)
Controlling loT devices with Python often involves interacting with the devices' APls or using specific libraries provided
by the device manufacturers. Below are general steps and an example script using the requests library to control a
hypothetical smart light bulb. Note that the actlial implementation may vary depending on the device and its API.

import requests

class SmartlightController :
def _init_ (self, base_url) :
self ..base_ url = base_url

def turn_on (self) :


endpoint = "/api/turn-on"
response = requests.post(self.base_url + endpoint)
return response.json()

def turn_off (self) :


endpoint = "/api/turn-off"
response = requests.post(self.base_url + endpoint)
return response.json()

def set_brightness (self, brightness_level) :


endpoint = f"/api/set-brightness/{brightness_level}"
response = requests.post(self.base_url + endpoint)
return response.json()

if name -- _main_" :
# Example usage
light_controller = Sma rtlightContro l ler( "http: //smart-light-api. example. corn" )

# Turn on the smart light


response = light_controller . turn_ on()
print(response)

# Set brightness to 50%


response = light_controller . set_brightness( 50 }
print(response)

# Turn off the smart light


response = light_controller . turn_off()
print( response)

In t his example:

• SmartlightControl ler is a class that communicates with the smart light API.
• turn_on, tu rn_off , and set_brightness are methods to control the smart light.

Before using this script, you would need to replace the base_url and adjust the API endpoints based on t he
documentation provided by the smart light manufacturer. Always ensure that you have the necessary authentication and
authorization to control the loT devices.

For practical use, consider using specific libraries or SDKs provided by the device manufacturer whenever available. For
instance, popular smart home platforms li ke Philips Hue or Tuya offer Python libraries or APls for controlling t heir
devices.
Monitor and display sensor data (e.g.,
temperature, humidity)
Monitoring and displaying sensor data with Python typically involves interfacing with sensors and using libraries to
visualize the data. Below is a basic example using the matplotlib library to plot temperature and humidity data from a
hypothetical sensor.

First, you'll need to instal l the required library:

pip install matplotlib

Then, you can use the following script as a starting point:

import matplotlib.pyplot as plt


import random
from datetime import datetime
import time

class Sensor :
def _init_ (self) :
# Initialize the sensor (replace with actual sensor initialization)
pass

def get_data (self) :


# Simulate sensor data (replace with actual data retrieval)
temperature= random.uniform( 20 , 25 )
humidity= random.uniform( 40 , 60 )
timestamp = datetime.now()
return temperature, humidity, timestamp

def plot_sensor_data (sensor, duration, interval) :


temperatures= 11
humidities = II
timestamps = II

end_time =time.t ime(}+ duration


while time.time() < end_time:
temperature, humidity, timestamp = sensor.get_data()

temperatures.append(temperature}
humidities.append(humidity)
timestamps.append(timestamp}

# Plot data in real-time


plot_realtime(timestamps, temperatures, humidities}

time.sleep(interval)
def plot_ realtime (timestamps, temperatures, humidities) :
plt.plot(timestamps, temperatures, label= 'Temperature (°C)' )
plt.plot(timestamps, humidities, label= 'Humidity (%)' )

plt.xlabel( 'Time' )
plt.ylabel( 'Value' )
plt.title( 'Sensor Data' )
plt. legend()
plt.draw()
plt.pause( 0.1 )
plt. elf()

if name -- "_main_" ·
sensor = Sensor()
plot_ sensor_data(se nsor, duration=60 , interval=l )

In this example:

• Sensor is a class represe nting your sensor (re place it with actual sensor code).
• get_data simulates sensor data (replace it with actual data retrieval) .
• p lot_sen so r _data continuously retrieves data and updates the plot in real-time.

This is a basic example, and you may need to adjust it based on your specific sensor and requirements. If you have a
specific sensor in mind, check if the re's a Python library available for interfacing with it. Popular sensor libraries include
Adaf rui t_C i rcui tPython for various sensors.
Control a robot or drone

Controlling a robot or drone with Python often involves using specific libraries or APls provided by the manufacturer of
the robot or drone. Below, I'll provide a general overview of the process and some examples using popular platforms.

Controlling a Drone with Python (DJI Tello)

OJI Tello is a popular and affordable drone that can be controlled using Python. You can use the d j i te l lopy library for
this purpose. First, you'll need to install the library:

pip install djitellopy

Here's a simple example script to take off, move in a square pattern, and land:

from djitellopy import Tello


import time

# Connect to the drone


drone= Tello()
drone . connect ( }

# Take off
drone . takeoff(}
time. sleep ( 2 )

# Move in a square pattern


for _ in range( 4 }:
drone.move_forward( 100 ) # Move forward 100 cm
drone.rotate_clockwise( 90 ) # Rotate 90 degrees
time.sleep( l )

# Land
drone. land()
Controlling a Robot with Python (ROS - Robot Operating System)

ROS (Robot Operating System) is a f lexible framework for writing robot software. It provides libraries and tools to help
software developers create robot applicat ions. ROS has Python bindings, and you can use it to control a wide range of
robots.

Here's a simple example using ROS to control a robot:

# Import ROS libraries


import rospy
from geometry_msgs.msg import Twist

# Initialize the ROS node


rospy.init_ node( 'robot_control_node' , anonymous =True )

# Create a publisher to send velocity commands


cmd_vel_pub = rospy.Publisher( ' /cmd_vel' , Twist, queue_ size=10 )

# Create a Twist message to send linear and angular velocities


cmd_ vel_msg = Twist()
cmd_vel_msg. linear.x = 0.2 # Set linear velocity
cmd_vel_msg.angular.z = 0.1 # Set angular velocity

# Publish the Twist message


cmd_ vel_ pub.publish(cmd_vel_msg)

# Sleep to allow time for the robot to move


rospy.sleep( 2 )

# Stop the robot


cmd_ vel_msg. linear.x = 0.0
cmd_vel_msg.angular.z = 0.0
cmd_vel_ pub.publish(cmd_vel_msg)

Please note t hat the examples provided are simplified, and you may need to adapt them based on the specific drone or
robot you are working with. Always refer to the documentation of the hardware you're using for detailed instructions on
interfacing with Python.
Capture and analyze data from
webcams or cameras
To capture and analyze data from webcams or cameras using Python, you can use the OpenCV library. Opencv is a
powerful computer vision library that provides various tools for image and video analysis. Below is a basic example to
get you started :

pip install opencv-python

Capture Video from Webcam and Perform Basic Analysis

import cv2

# Open a connection to the webcam (0 is usually the default webcam)


cap= cv2.VideoCapture( 0 )

wllile True :
# Capture frame-by-frame
ret, frame= cap.read{)

# Display tile resulting frame


cv2.imsllow( ' Webcam Feed' , frame)

# Perform analysis on tile frame (add your analysis code here)

# Break tile loop i f 'q' is pressed


if cv2.waitKey{ 1 ) & 0xFF = ord( 'q' ):
break

# Release tile webcam and close all windows


cap. re lease { )
cv2.destroyAllWindows()

In this example:

• cv2. VideoCapture(0) opens a connection to the default webcam.


• cap. read ( ) captures a f rame from the webcam.
• cv2. imshow ( ) displays the frame in a window.
• Analysis code can be added inside the loop to perform operations on the captured fra mes .
Advanced Analysis: Face Detection

As an example of more advanced analysis, let's add face detection using OpenCV's pre-trained Haarcascades classifier:

import cv2

# Load the pre-trained face cascade


face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml' }

# Open a connection to the webcam


cap = cv2.VideoCapture( 0 )

while True :
# Capture frame-by-frame
ret, frame = cap.read()

# Convert the frame to grayscale for face detection


gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detect faces in the frame


faces = face_cascade.detectMultiScale(gray, scalefactor=l.3 , minNeighbors=S}

# Draw rectangles around the detected faces


for (x, y, w, h) in faces:
cv2. rectangle(frame, (x, y), (x+w, y+h), (255 , 0 , 0 ), 2)

# Display the resulting frame


cv2.imshow( 'Face Detection' , frame)

# Break the loop if 'q' is pressed


if cv2.waitKey( l ) & 0xFF == ord( 'q' ):
break

# Release the webcam and close all windows


cap. release()
cv2.destroyAllWindows()

This example adds face detection using a Haarcascades classifier. You can customize and extend the analysis based on
your specific requirements.
Create a script to interface with
microcontrollers (e.g., Arduino)

To interface with microcontrollers, suc h as Arduino, using Python, you can use the pyse rial li brary. This library allows
communication with serial ports, which is commonly used for connecting to microcontrollers. Below is a simple example
that demonstrates how to send and receive data between Python and Arduino over a serial connection.

pip install pyserial

Python Scri pt (Sending data to Arduino)


7
import serial
import time

# Specify the COM port (update this based on your Arduino configuration)
ser = serial.Serial( 'COM3' , 9600 , timeout=l )

def send_data_to_arduino (data) :


ser.write(data.encode())
time.sleep( 2 ) # Allow time for Arduino to process data

# Example: Sending "Hello, Arduino!" to Arduino


send_data_to_arduino( "Hello, Arduino!" )

# Close the serial connection


ser.close{)
Arduino Sketch (Receiving data from Python)

void setup () {
Serial.begin( 9600 );
}

void loop () {
i f (Serial.available() > 0 ) {
String data = Serial.readString();
Serial. print( "Received data: " );
Serial.println(data);

// Add your Arduino logic based on received data here


}
}

In this example:

• The Python script sends data to the Arduino using se r. write (data. encode () l.
• The Arduino sketch reads the incoming data using Serial. readSt ring () and processes it accordingly.

Remember to replace 'CIX13' with the appropriate COM port to which your Arduino is connected. Also, ensure that the
baud rate (9600 in this case) matches the baud rate specified in your Arduino sketch (Serial. begin( 9600 )) .

You can customize both the Python script and Arduino sketch based on the specific data and functionality you need for
your project.
XIII

AI and Machine Learning

AI and Machine Learning are powerful tools for


implementing artificial intelligence (AI) and
machine learning algorithms. These scripts, often
written in languages like Python, facilitate the
development, training, and deployment of
machine-learning models. They encompass a wide
range of applications, from basic linear regression
models to complex neural networks. These scripts
enable tasks such as image recognition, natural
language processing, and recommendation systems.
Implement a basic machine learning
model (e.g., linear regression)
Let's create a simple example of a linear regression model using Python and the popular sciki t- learn li brary. In this
example, we'll generate some ra ndom data and fit a linear regression model to it.

pip install scikit-learn

import numpy as np
import matplotlib.pyplot as plt
from sklea rn. mode l _se le et ion import train_ test _sp lit
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mea n_squared_error

# Generate random data


np.random.seed( 0}
X = 2 * np.random.rand( 100 , 1 )
y = 4 + 3 * X + np.random.randn( 100 , 1 )

# Split the data into training and testing sets


X_ train, X_test, y_train, y_ test = train_test_split(X, y, test_ size=0.2 ,
random_ state=42 )

#Traina linear regression model


model = LinearRegression()
model.fit(X_train, y_ train)

# Make predictions on the test set


y_ pred = model.predict(X_test)

# Evaluate the model


mse = mean_squared_error(y_test, y_ pred)
print( f'Mean Squared Error: {mse}' )

# Plot the results


plt.scatter{X_ test, y_test, color= 'black' )
plt.plot(X_ test, y_pred, colo r= 'blue' , linewidth=3 )
plt.xlabel( 'X' }
plt. ylabel( 'y' }
plt.title( 'Linear Regression Example' }
plt.show()

In this example:

• We generate random data using numpy .


• The data is split into training and testing sets using train_test_sp lit from scikit- learn.
• We train a linear regression model using the training data.
• The model makes predictions on the test set, and we evaluate its performance using mean squared error.
• Finally, we visualize the results with a scatter plot.

This is a basic example, and in a real-world scenario, you would typically work with real data and perform more thorough
data preprocessing, feature engineering, and model evaluation. Adjust the code based on your specific use case and
dataset.
Develop a simple chatbot using
natural language processing
We can use the nl tk library for natural language processing and the re library for regular expressions. For simplicity,
we' ll create a rule-based chatbot that responds to a few predefined patterns.

pip install nltk

import nltk
import re
import random

# Download NLTK data {if not already downloaded)


nltk.download( 'punkt' )

class SimpleChatbot :
def _ init_ (self) :
self.responses = {
r'hellolhilhey' : [ 'Hello!' , 'Hi there!' , 'Hey!' ],
r'how are you' : [ 'I' m doing well, thank you! ', ' I 'm just a computer
program, but I' m fine. How about you? '),
r' what is your name ': [' I am a simple chatbot. ', ' You can call me
Chatbot. 'I,
r' bye lgoodbye ': [' Goodbye! ', ' See you later! ', ' Bye! ')
}

def respond(self, user_input):


for pattern, responses in self.responses.items():
if re.search(pattern, user_input, re.IGNORECASE):
return random.choice(responses)
return "I' m sorry, I don 't understand that."

# Main loop
chatbot = SimpleChatbot()

print{"Simple Chatbot: Hello! Type ' bye ' to exit.")


while True:
user_input = input("You: ")
if user_input.lower() == ' bye ' :
print("Simple Chatbot: Goodbye!")
break
response= chatbot.respond(user_input)
print(f"Simple Chatbot: {response}")
In this example:

• The Simp leChatbot class has a dictionary (responses ) that maps patterns to possible responses.
• The respond method searches for a pattern in the user's input and returns a random response if a match is
found .
• The main loop allows the user to input messages until they type 'bye' to exit.

Keep in mind t hat this is a very basic chatbot, and for more sophisticated and dynamic chatbots, you might want to
explore using machine learning models, such as those provided by the transformers library for more advanced
natural language processing tasks.
Train a model for image recogni on
Training a model for image recognition involves several steps, and it often requires a large labeled dataset. In this
example, I' ll use the popular deep learning framework TensorFlow and its high-level API , Keras. We'll create a simp le
convolutional neu ral network (CNN) for image classification. For demonstration purposes, I'll use the CI FAR-1O dataset,
which consists of 60,000 32x32 color images in 10 different classes.

pip install tensorflow

import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical

# Load and p1reprocess the CIFAR-10 dataset


(train_images, train_ labels), (test_images, test_ labels) = cifar10.load_data()
train_images, test_images = train_images / 255.0 , test_images / 255.0 # Normalize
pixel values to be between 0 and 1
train_labels, test_labels = to_categorical(train_labels),
to_categorical(test_labels)

# Build the CNN model


model = models . Sequential()
model . add(layers.Conv2D( 32 , (3, 3 ), activation='relu' , input_shape=(32 , 32 , 3)))
model.add(layers.MaxPooli ng2D(( 2, 2)))
model . add(layers.Conv2D( 64 , (3, 3 ), activation='relu' ))
model . add(layers.MaxPooli ng2D(( 2, 2 )))
model . add( l ayers.Conv2D( 64 , (3, 3 ), activation=' relu' ))
model . add(layers.Flatten(})
model . add(layers.Dense( 64 , activation= 'relu' })
model . add(layers.Dense( 10 , activation= 'softmax' ))

# Compile the model


model.compile(optimizer= 'adam' , loss='categorical_crossentropy' , metrics=
[ 'accuracy' I)

# Train the model


model . fit(train_images, train_labels, epochs=10 , validation_data=(test_images,
test_ labels) )

# Evaluate the model


test_loss , test_acc = mode l .evaluate(test_images, test_labe l s)
print( f"Test accuracy: {test_acc}" )
This script does th e following :

1. Load and preprocess the dataset: The CIFAR - 10 dataset is loaded, and the pixel values are norma lized.

2. Build the CNN model: A simple CNN model is created using Conv2D layers for convolution, MaxPooling2D layers
for downsampling, and Dense layers for classification .

3. Compile the model: Specify the optimizer, loss f unction, and metrics for the model.

4. Train the model: The model is tra ined on the training dataset.

5. Evaluate the model: The model is evaluated on the test dataset, and the accuracy is printed.

Keep in mind that for real -world applications, you might need a more complex model, and you should explore techniques
like tra nsfer learning with pre -tra ined models on larger datasets.
Create a recommenda on system
Creating a recommendation system involves various approaches, and one popular method is collaborative filtering. In
this example, I'll show you how to build a simple user-based collaborative filtering recommendation system using Python
and the Surprise library.

pip install sc i kit-surprise

from surprise import Dataset, Reader, KNNBasic


from surprise.model_selection import cross_validate, train_test_split
from surprise.accuracy import rmse

# Load the Movielens dataset (or any other dataset of your choice)
data = Dataset. load_builtin( 'ml-100k' )

# Define the reader with the appropriate rating scale


reader = Reader(rating_scale=(l , 5))

# Load the dataset with the reader


data = Dataset. load_from_df(data.raw_ ratings, reader)

# Split the dataset into training and testing sets


trainset, testset = train_test_split(data, test_size=0.2 )

# Use the k....f-.1.1 algorithm for collaborative filtering


sim_options = {
'name' : 'cosine' ,
'user_based' : True ,
}

model = KNNBasic(sim_options=sim_options)

#Train the model on the training set


model.fit(trainset)

# Make predictions on the test set


predictions = model.test(testset)

# Evaluate the model using RMSE (Root Mean Squared Error)


accuracy = rmse(predictions)
print ( f"RMSE: {accuracy}" l

# Recommend items for a specific user (user ID 196 in this example)


user_id = str( 196 )
user_ ratings = trainset.ur[trainset.to_inner_uid(user_id)]
items_ rated_by_user = {item_ id .: rating for ( item_id, rating) in user_ rat i ngs}
items_not_ rated_by_user = set(trainset ..all_items(}) - set(items_ rated_by_user}
# Predict ratings for items not rated by the user
item_ratings = [(item_id, model.predict(user_id, item_id).est) for item_id in
items_not_rated_by_user]

# Sort the recommendations by predicted rating


sorted_ratings = sorted(item_ratings, key= lambda x: x[ l ], reverse=True )

# Print the top N recommendations


top_n = 5
top_recommendations = sorted_ratings[:top_nl
print ( f"Top {top_n} recommendations for user {user_id}:" )
for item_id, rating in top_recommendations:
print (f"Item ID: {item_id}, Predicted Rating: {rating}" )

This script does the following :

1. Load the dataset: The Movielens dataset is loaded. You can repla ce it with your own dataset.

2. Split the dataset: It is split into training and testing sets.

3. Choose the collaborative filtering algorithm: The k-NN algorithm is used with cosine similarity.

4. Train the model: The model is tra ined on the training set.

5. Make predictions: Predictions are made on the test set.

6. Evaluate the model: The model is evaluated using RMSE.

7. Generate recommendations: Recommendations are generated for a specific user (user ID 196 in this example).

This is a basic example, and for rea l-world scenarios, you might want to explore more advanced techniq ues and consider
factors like item content, matrix factorization, or deep learning -based approaches .
Build a script for sen ment analysis
on social media data
To perform sentiment analysis on social media data, you can use Natural Language Processing (N LP) libraries. In this
example, I'll use the TextB lob library for simplicity. Ensure you have it installed before running the script:

pip install textblob

Now, you can use the fo llowing Python script for sentiment analysis:

from textblob import TextBlob


import tweepy

# Set up your Twitter API credentials


consumer_key = 'your_consumer_key'
consumer_secret = 'your_consumer_secret'
access_token = 'your_access_token'
access_token_secret = 'your_access_token_secret'

# Authenticate with Twitter API


auth = tweepy.OAuthHandler(consumer_key, consumer_secret}
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth}

def analyze_sentiment (tweet) :


# Create a TextBlob object
analysis = TextBlob(tweet)

# Determine sentiment polarity (-1 to 1)


if analysis.sentiment.polarity> 0 :
return 'Positive'
elif analysis.sentiment.polarity -- 0:
return 'Neutral'
else :
return 'Negative'

def fetch_tweets (query, count=10) :


# Fetch tweets based on the query
tweets = tweepy.Cursor(api.search, q=query, lang='en' ),items(count}

# Analyze sentiment for each tweet


results = []
for tweet in tweets:
result = {
'text' : tweet.text,
'sentiment' : analyze_sent i ment{tweet.text}
}
results.append(resultl

return results
if name == "_main_" ·
# Specify the search query and the number of tweets to fetch
search_query = 'Python'
tweet_count = 10

# Fetch and analyze tweets


tweets= fetch_tweets(search_query, tweet_count}

# Display results
print( f"Sentiment analysis for {tweet_count} tweets with the query
'{sea rch_query}' : \n" )
for index, tweet in enumerate(tweets, start=l ):
print( f"Tweet {index}:\nText: {tweet['text']}\nSentiment:
{tweet I' sentiment'] }\n" }

Replace 'your_consumer_key' , 'your_consumer _secret' , 'your_access_ token ' , and


'your_access_token_sec ret' with your actual Twitter API credentials.

This script defines functions to analyze sentiment using TextBlob and fetch tweets using the Tweepy library. It then
prints the sentiment analysis results for each fetched tweet.

Note: Ensure you have the necessary access and permissions for using the Twitter API.
XIV

Database

Database are instrumental in managing and


manipulating data within a database system. These
scripts, often written in languages like SQL or
Python, execute queries and commands to interact
with databases, whether for creating tables,
inserting records, updating information, or
retrieving specific data. They serve as a bridge
between applications and databases, allowing
seamless communication and e cient data
handling.
Automate database backup and
restore

To automate database backup and restore with Python, you can use the subprocess module to execute command- line
operations. Below is a simple example using mysq ldump for MySQL databases. Adjustments may be needed based on
your specific database system.

Ensure you have the necessary database client tools installed, and replace placeholders such as YOUR_DB_USER,
YOUR_DB_PASSWORD, YOUR_DB_NAME, and file paths with your actual database credentials and paths.

import subprocess
import datetime
import os

def backup_database () :
# Database connection details
db_user = 'YOUR_DB_USER'
db_password = 'YOUR_DB_PASSWORD'
db_name = 'YOUR_DB_NAME'

# Backup file name with timestamp


timestamp = datetime.datetime.now().strftime( '%Y%m%d%HS\sM%S' )
backup_file = f'database_backup_{timestamp} .sql'

# Backup command
command= [
'mysqldump' ,
'-u' , db_user,
'-p' + db_password,
'--databases' , db_name,
'--result-file=' + backup_file

try :
# Execute backup command
subprocess.run(command, check=True )
print( f"Database backup successful. Backup file: {backup_file}" )
except subprocess.CalledProcessError as e:
print( f"Error during database backup: {e}" )
def restore_database (backup_file) :
# Database connection details
db_user = 'YOUR_DB_USER'
db_password = 'YOUR_DB_PASSWORD'
db_name = 'YOUR_DB_NAME'

# Restore command
command = [
'mysql' ,
'-u' , db_user ,
'-p' + db_password,
db_name,
'<' , backup_file

try :
Execute restore command
#
subprocess.run(command , check=True , she l l =True )
print ( "Database restore successful." )
except subprocess . Ca l ledProcessError as e:
print( f"Error during database restore: {e}" )

if name == "_main_" :
# Perform backup
backup_database()

# Example: Perform restore


# Specify the backup file to restore
# restore_file = 'database_backup_20220101120000.sql'
# restore_database(restore_file)

This script defines two functions: backup_database to create a database backup and restore_database to restore
a database from a backup file. You can schedule the backup script to run periodically using a task scheduler (e.g., cron
on Unix-like systems or Task Scheduler on Windows). Adjust the commands and paths based on your specific database
and system requirements.
Generate and execute SQL queries

To generate and execute SQL queries with Python, you can use a database connector library such as sqlite3 (for
SQLite), psycopg2 (for Postgre SQL), mysql-connecto r-python (for MySQL), or other libra ries depending on your
database system. Below is a basic example using the sqlite3 library for SQLite. Make sure to install the appropriate
library based on your database system.

Here's an examp le script that connects to an SQLite database, creates a table, inserts data, and executes a query:

import sqlite3

def create connection (db_file) :


"""Create a database connection to an SQLite database."""
try :
connection = sqlite3 . connect(db_file)
return connection
except sqlite3.Error as e:
print( f"Error: {e}" l
return None

def execute_query (connection, query} :


"""Execute an SQL query."""
try :
cursor = connection.cursor(}
cursor.execute(query)
connection.commit()
print( "Query executed successfully." }
except sqlite3.Error as e:
print( f"Error executing query: {e}" )
def main () :
# Connect to the SQLite database (create it if not exists)
database_file = example.db"
11

connection = create_connection(database_file)

# Define an SQL query to create a table


create_table_query = 111111

CREATE TABLE IF NOT EXISTS users


id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
email TEXT NOT NULL
);
Ulltl

# Execute the table creation query


execute_query(connection, create_table_query)

# Insert some sample data into the table


insert_data_query = 111111

INSERT INTO users (username, email)


VALUES
( 1 john_doe 1 , '[email protected]'),
('jane_doe 1 , [email protected]');
1

Hlltl

# Execute the data insertion query


execute_query(connection, insert_data_query)

# Select all rows from the users table


select_query = "SELECT* FROM users;"

# Execute the select query and print the results


cursor = connection.cursor()
cursor.execute(select_query)
rows = cursor.fetchall()

print( "Users:" )
for row in rows:
print(row)

# Close the database connection


connection.close()

if _ name_ -- _main_" :
main()

In this example, the script connects to an SQLite database (example. db), creates a table named users, inserts sample
data, and then selects and prints all rows from the users table. Modify the queries according to your database schema
and requirements.
Build a script for database migra on
Database migration scripts are typically specific to the database management system (DBMS) you are using. Below is an
example using Flask-Migrate for migrating a SQLite database in a Flask application. Adjustments would be needed for
different DBMS or framewo rks .

1. Install necessary libraries:

pip install Flask Flask-SQLAlchemy Flask-Migrate

2. Create a Flask application with SQLAlchemy and Flask-Migrate:

# app.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from fla sk_mig rate import Migrate

app = Flask(_name_)
app.config[ 'SQLALCHEMY_!DATABASE_URI' ] = 'sqlite:///example.db'
app.config[ 'SQLALCHEMY_TRACK_MODIFICATIONS' ] = False

db = SQLAlchemy(app)
migrate= Migrate(app, db)

class User (db.Model) :


id= db.Column(db.Integer, primary_key=True )
username = db.Column(db.String( 80 }, unique=True , nullable=False )

# Add more models and relationships as needed

@app. route( 1/ 1)
def index () :
return 'Hello, World!'

if name == '_main_
app.run(debug=True )

3. Initialize migrations:

python app.py db init


This creates a migrations directory.

4. Create an initial migration:

python app ..py db migrate -m "initial migration"

This generates an initial migration script in the migrations directory.

5. Apply the initial migration to the database:

python app.py db upgrade

Now, your database is set up.

6. Suppose you make changes to your models. Create a new migration:

python app.py db migrate -m "description of changes"

7. Apply the new migration:

python app ..py db upgrade

This process allows you to version control your database schema and apply changes in a structured manner.

Remember to customize the example according to your project's structure and requirements. If you are using a different
framework or DBMS, the process might be a bit different.
Extract data from databases to CSV
or Excel files

You can use the pandas library in Python to easily extract data from databases and save it to CSV or Excel files. Here's a
simple examp le using pandas and the sqlite3 module tor SQLite databases:

import sqlite3
import pandas as pd

# Connect to the SQLite database


conn = sqlite3 ., connect( 'example.db' ) # Replace 'example.db' with your database file

# Query to select data from a table (replace 'your_table' and 'your_columns'


accordingly)
query = "SELECT* FROM your_table"

# Use pandas to read the data from the database into a DataFrame
df = pd. read_ sql_query(query, conn)

# Save Dataframe to CSV


df.to_ csv( ' output.csv' , index=False ) # Replace 'output.csv' with your desired CSV
file name

# Save Dataframe to Excel


df.to_excel( ' output .xlsx' , index=False ) # Replace 'output.xlsx' with your desired
Excel file name

# Close the database connection


conn. close()

Make sure to rep lace 'example. db' , 'your_tab le' , and 'your_ columns ' with your specific database file, table,
and column names. Also, customize the output file names as needed.

If you're using a different database system (e.g., MySQL, PostgreSQL), you may need to install additional libraries (like
mysql-connector or psycopg2) and adjust the connection code accordingly. The basic process of using pandas for
read ing from a database and saving to csv or Excel remains the same.
Create a basic CRUD applica on
Creating a basic CRUD (Create, Read, Update, Delete) application typically involves using a web framewo rk. Flask is a
lightweight web framework for Python that is well-suited for building small to medium-sized web applications. Here's a
simple example of a CRUD application using Flask and SQLite as the database:

1. First, install Flask:

pip install Flask

2. Create a file named app. py and add the fol lowing code:

from flask import Flask, render_template, request, redirect, url_for


from flask_sqlalchemy import SQLAlchemy

app = Flask( _ name_ )


app ..config[ 'SQLALCHEMY_DATABASE_URI' ] = 'sqlite:///crud.db' # Use SQLite for
simplicity
db = SQLAlchemy(app)

class Item (db.Model) :


id = db.Column(db.Integer, primary_key=True )
name = db.Column(db.String( S0 ), nullable=False )

@app. route('/')
def index ( l :
items = Item.query.all()
return render_template( 'index.html' , items=items)

@app.route('/add', methods=['POST'])
def add () :
name = request.form [ 'name' I
new_item = Item(name=name)
db.session . add(new_item)
db.session . commit{)
return redirect ( url_fo r( 'index' ))

@app.route('/edit/<int:item_id>', methods=['GET', 'POST'))


def edit (item_id) :
item = Item.query.get(item_id)
if request . method = 'POST' :
item. name = request.form [ ' name' I
db . session.commit(l
return redirect(url_for( 'index' })
return render_template( 'edit.html' , item=item)
@app.route('/delete/<int:item_id>')
def delete (item_id) :
item= Item.query.get(item_id)
db.session.delete(item)
db.session.commit()
return redirect(url_for( 'index' ))

if name == '_main_' ·
db. create_all()
app.run(debug=True )

3. Create two HTMLtemplates in a fo lder named templates:

o templates/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-W">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content ="width=device-width, initial-scale=l.0">
<ti tle>CRUD App</t it le>
</head>
<body>
<hl>Items</hl>
cul>
{% for item in items%}
<li>{{ item.name }} - <a href="{{ url_for( 'edit',
item_id=item ..id) }}">Edit</a> I <a href="{{ url_for( 'delete',
item_id=item. id) }}">Delete</ a></ li>
{% endfor %}
</Ul>
<form action="{{ url_for('add') }}" method="post">
<label for="name">Item Name: </label>
<input type="text" id="name" name="name" required>
<button type="submit">Add Item</button>
</form>
</body>
</html>
o templates/edit. html:

<!DOCTYPE html>
<html lang ="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name=" viewpo rt" content="width=dev ice-width, ini t ia l-s ea le=l. 0" >
<title> Edit Item</title>
</head>
<body>
<hl> Edit Item</hl>
<form action="{{ url_for( 'edit', item_id=item. id) }}" method ="post">
<label for="name">Item Name: </label>
<input type=" text" id="name" name=" name" value="{ { item. name } }"
required>
<button type="submit">Save Changes</button>
</form>
</body>
</html>

4. Run your Flask application:

python app.py

Visit http;/(127.0.o.1 :5000/ in your browser to see and interact with the CRUD application.

This is a simple example, and you can extend it based on your needs. Remember to handle security aspects such as
input validation, error handling, and authentication in a real- world application.
XV

Educa on and Learning

Education and Learning play a vital role in


enhancing the educational experience through
automation and interactive tools. These scripts
encompass a wide range of applications, from
creating flashcards and math problem generators to
spelling and vocabulary quizzes. They facilitate the
development of educational games, language
learning tools, and even timer-based productivity
aids.
Create flashcards for studying
Creating a simple flashcard program in Python involves handling questions and answers, and then allowing the user to
review them. Here's a basic example:

import random

class Flashcards :
def init (self} :
self. ea rd s = {}

def add_card (self, question, answer) :


self.cards[question] = answer

def review_cards (self) :


questions= list(self.cards.keys())
random.shuffle(questions}

for question in questions:


user_answer = input ( f"Question: {question}\nYou r Answer: " )
correct_answer = self. ea rds [question]

if user_answer.lower() == correct_answer.lower{):
print {"Correct! \n" )
else :
print {f"Wrong ! The correct answer is: {correct_answer}\n" )

if _name_ -- "_main_" ·
flashcards= Flashcards()

# Add flashcards
flashcards.add_card( "What is the capital of France?" , "Paris" )
flashcards.add_card( "What is the largest mammal?" , "Blue whale" )
flashcards.add_card( "Who wrote 'Romeo and Juliet'?" , "William Shakespeare" )

# Review flashcards
flashcards. review_ca rds ()

This script defines a Flashcards class with methods to add flashcards and review them. You can customize the
questions and answers based on your study materials. To run this script, save it to a file (e.g., flashcards. py) and
execute it using Python:

python flashcards.py

Feel free to expand this script with additional features, such as saving and loading flashcards from a file, implementing a
scori ng system, or creating a more interactive user interface.
Build a script for math problem
genera on
This script generates addition and subtraction problems with random numbers:

import random

class MathProblemGenerator :
def generate_addition_problem(self) :
numl = random.randint( l , 20 }
num2 = random.randint( l , 20 )
return f"{numl} + {num2}" , numl + num2

def generate_ subtraction_ problem (self) :


numl = random.randint( l , 20 )
num2 = random.randint( l , min(numl, 20 ))
return f"{numl} - {num2}" , numl - num2

def generate_math_ problem (self) :


if random.choice( [True , False )): # Randomly choose addition or subtraction
return self .. gene rate_add i tion_p rob lem ()
else :
return self .. generate_subtraction_problem()

if name == "_main_" ·
problem_generator = MathProblemGenerator()

for _ in range( S ): # Generate 5 math problems


problem, solution= problem_generator.generate_math_problem()
user_answer = input( f"Solve: {problem} = " )

if user_answer.isdigit() and int(user_answer) -- solution:


print ( "Correct! \n" )
else :
print ( f"Wrong ! The correct answer is: {so lution}\n" )

This script defines a MathProb lemGene rat or class with methods for generating addition and subtraction problems.
The gene rate_math_p rob lem method randomly selects between addition and subtraction . The user is prompted to
solve the generated problems, and feedback is provided.

You can customize the script to include other types of math problems or extend it with additional features based on your
requirements.
Develop a spelling or vocabulary quiz
Below is a simple Python script for a spelling or vocabulary quiz. This script uses a dictionary to store words and their
correct spellings. The user is prompted with a word, and they need to enter the correct spelling.

import random

class SpellingQuiz :
def _init_ (self, word_dict) :
self.word_dict = word_dict

def run_quiz (self, num_questions) :


correct_count = 0

for _ in range(num_questions):
random_wo rd = random. choice ( list (self. wo rd_dict. keys ()) }
correct_spelling = self.word_dict[random_word]

user_input = input( f"How do you spell '{random_word}'?


" ).strip(). lower{)

if user_input == correct_spelling.lower():
print ("Correct! \n" )
correct_count += 1
else :
print( f"Wrong! The correct spelling is '{correct_spelling}'.\n" )

print( f"You got {correct_count} out of {num_questions} correct." )

if name == "_main_" ·
# Dictionary of words and their correct spellings
word_dictionary = {
"python" : "Python" ,
"progra1111Ding" : "Programming" ,
"challenge" : "Challenge" ,
"coding" : "Coding" ,
"algorithm" : "Algorithm"
}

spelling_quiz = SpellingQuiz(word_dictionary)
spelling_quiz.run_quiz(num_questions=S)

You can customize the wo rd_dictiona ry with your own set of words and correct spellings. The ru n_qu iz method
runs the quiz, asking the user to spell random words from the dictionary. After completing the quiz, it provides the user
with the number of correct answers.

Feel free to modify the script to suit your needs or add more features to enhance the quiz experience.
Implement a script for learning a new
language
Creating a script for learning a new language can involve various activities such as flashcards, quizzes, and vocabulary
exercises. Here's a simple Python script that implements a basic language lea rning quiz. This script focuses on
translating words from Eng lish to another language.

import random

class LanguageLearningQuiz :
def _init_ (self, word_dict) :
self.word_dict = word_dict

def run_quiz (self, num_questions) :


correct_count = 0

for _ in range(num_questions):
random_word = random.choice(list(self.word_dict.keys()ll
correct_translation = self.word_dict[random_word)

user_inpu t = input (f"What is the translation of ' { random_word}'?


" ).strip().lower(l

if user_input == correct_translation.lower():
print ("Correct! \n" )
correct_count += 1
else :
print( f"Wrong! The correct translation is
' {correct_translation}'. \n" )

print( f"You got {correct_count} out of {num_questions} correct." )

if name == "_main_" ·
# Dictionary of English words and their translations
language_dictionary = {
"hello" : "hola" ,
"goodbye" : "adi6s" ,
"thank you" : "gracias" ,
uyes" : .. si 11 ,

uno 11 : "no"
}

learning_quiz = LanguagelearningQuiz(language_dictionary)
learning_quiz.run_quiz(num_questions=S )

In this script

• The language_dictionary contains English words as keys and their translations as values.
• The run_qu iz method prompts the user to translate random English words and checks the correctness of the
input.
• After completing the quiz, the script prints t he number of correct answers.

You can extend this script by adding more features like different languages, more complex sentence structures, or
incorporating mu ltimedia elements to make the learning experience more interactive.
Create a mer for produc vity and
focus
Here's a simple Python script to create a timer for productivity and focus . This script uses the time modu le to
implement a countdown timer. It will notify you when the time is up.

import time
import os
import platform

def clear_terminal () :
# Clear terminal screen based on the operating system
if platform.system(}.lower() == 'windows' :
os.system( 'cls' }
else :
os.system( 'clear' }

def countdown_timer (minutes) :


seconds = minutes* 60

for remaining_ time in range(seconds, 0 , -1 ):


minutes, seconds = divmod(remaining_ time, 60 )
timer_ display = f"{minutes: 02 d}:{seconds: 02 d}"

clear_ terminal(}
print ( f"Timer : {timer_display}" }

time.sleep( ! )

clear_terminal()
print( "Time's up! ., .~" )

if _name_ == "_main_" ·
try :
# Set the timer duration in minutes
timer_ duration = int(input( "Enter the timer duration in minutes: " ))
countdown_ timer(timer_duration)
except ValueError:
print ("Invalid input . Please enter a valid number of minutes ." )

In this script:

• The countdown_timer function takes the duration in minutes and counts down to 0.
• The clear_terminal function clears the terminal screen to update the timer display dynamically.
• The timer will display in the format MM:ss.
• Once the timer reaches o, it pri nts a message indicating that t he time is up.

You can customize t his script furthe r by adding sound notifications or integrating it with GUI libraries for a more user-
friendly experience.
About the Author
Serhan Sarı is a software engineer and accomplished author
with a passion for both technology and the written word. His
diverse range of interests includes martial arts,
snowboarding, and the intricate world of coding.
As a software engineer, Serhan has demonstrated an
exceptional aptitude for crafting innovative solutions and
developing software applications that make a meaningful
impact. His commitment to the field of technology has led
him to create and contribute to various projects, showcasing
his dedication to the ever-evolving world of software
development.

In addition to his technical prowess, Serhan is a published


author, where he channels his creativity and storytelling
abilities. Through his written works, he takes readers on
captivating journeys, revealing intricate plots and thought-
provoking narratives.

Outside of the digital realm, Serhan finds solace and


excitement in the realm of martial arts. He continuously
hones his skills, mastering various disciplines that require
discipline, physical prowess, and mental acuity. Whether it’s
on the mat or in the dojo, he exemplifies dedication and
commitment.

When winter graces the land with its snowy touch, you’ll
often find Serhan on the slopes, snowboarding with a sense of
adventure. He embraces the thrill of carving through fresh
powder, demonstrating a fearless spirit and a zest for life’s
exhilarating moments.

Serhan Sarı’s life is a blend of technological innovation,


literary creativity, martial arts discipline, and the adrenaline
of snowboarding. With an insatiable curiosity and a
determined spirit, he continues to explore new horizons,
always eager to take on the next challenge.
You can connect with me on:
c; _ _ _ _ _ _ __
https://www.serhansari.com
Also by Serhan Sari

“Unexpected Departures: Real Stories of Tragic Endings” is a


compelling book that delves deep into the lives of ordinary
individuals who found themselves in extraordinary and
heart-wrenching situations. The stories within this collection
are drawn from real-life experiences and narrated with raw
emotion, o ering a powerful glimpse into the human
condition when faced with sudden, tragic events.

The book covers a wide spectrum of tragic endings, from


personal losses to life-altering accidents and even unforeseen
twists of fate. The common thread running through these
narratives is the resilience and courage displayed by the
people who lived through these trying moments. Their stories
serve as a testament to the indomitable spirit of humanity.

With a focus on personal narratives, “Unexpected


Departures” invites readers to connect with the deeply
human side of tragedy. It explores the emotional journeys
that follow such unexpected endings, including the grief,
hope, and healing that come with them. These stories not
only provide insights into the fragility of life but also o er a
profound exploration of the strength of the human spirit in
the face of adversity.

Readers of “Unexpected Departures” will be moved and


inspired by real-life accounts, finding solace and empathy in
the shared experiences of those who have navigated their way
through unforeseen tragedies. This book is a touching
reminder of our common humanity and the power of
resilience when life takes unexpected turns.

Unexpected
Unexpected Departures:
Departures: Real
Real Stories
Stories of
of Tragic
Tragic Endings
Endings
https://amzn.to/40um1Lj

You might also like