Sari Serhan Python Toolbox 100 Scripts For Developers 2023
Sari Serhan Python Toolbox 100 Scripts For Developers 2023
First edition
I. WEB SCRAPING
II. AUTOMATION
V. TEXT PROCESSING
IX. UTILITY
XI. SECURITY
XIV. DATABASE
Web Scraping
import requests
from bs4 import BeautifulSoup
Note:
1. You need to install the requests and beautifulsoup4 libraries if you haven't already. You can do this using:
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
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:
Now, you can use the fol lowing Python script to get stock prices:
import yfinance as yf
import time
# Replace 'AAPL' with the stock symbol you want to monitor (e.g., 'AAPL' for Apple)
stock_ symbol = 'AAPL'
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
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
if response.status_code == 200 :
soup = BeautifulSoup (response.text, 'html . parser' )
# 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
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_ '
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'
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
#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)
# Send email
server.sendmail(sender_email, to_email, msg.as_string{))
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:
import schedule
import time
def job () :
print( "Task is running ..•• " )
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:
import shutil
import schedule
import time
import datetime
def backup () :
source_directory = "/path/to/source"
backup_d i rectory = "/path/to/backup"
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)
• 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
if name == "_main_" ·
# List of software packages to install
softwa re_packages = [ "package!" , "package2" , "package3" ]
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
import pandas as pd
import matplotlib . pyplot as plt
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.
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:
Bar Chart:
# Sample data
categories= [ 'Category A' , 'Category B' , 'Category C' ]
values= (25 , 40 , 30 )
Line Chart:
# Sample data
x_values = (1, 2 , 3 , 4 , 5)
y_values = (10 , 12 , 18 , 15 , 20 ]
# Sample data
x_values = (1 , 2, 3 , 4 , SI
y_values = (10 , 12 , 18 , 15 , 20 1
Histogram:
# 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()
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:
2. Example script:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Create a Dataframe
weather_df = pd.DataFrame(data)
weather_df[ 'Date' ] = pd.to_datetime{weather_df[ 'Date' ))
# Plotting
plt.figure(figsize=( 12 , 6))
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:
2. Example script:
import pandas as pd
import matplotlib . pyplot as plt
# Create a Dataframe
survey_ df = pd . DataFrame(data)
# Plotting
plt ..figure(figsize= ( l2 , 4 ))
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:
2. Example script:
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
# Example usage:
input_image = "path/to/your/input_image.jpg"
output_image = "path/to/your/output_image.jpg"
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:
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:
# Apply filters
filtered_img = img.filter(ImageFilter.BLUR)
filtered_img = filtered_img,filter(ImageFilter .C0NT0UR)
fi ltered_img = filtered_img ..filter( ImageFilter. EDGE_ENHANCE)
# Example usage:
input_image = "path/to/your/input_image.jpg"
output_image = "path/to/your/output_image.jpg"
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:
# Create a thumbnail
img.thumbnail(thumbnail_ size}
# Example usage:
input_image = "path/to/your/input_image.jpg"
oLJtput_ thumbnail = "path/to/your /output_thumbnai l. j pg"
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.
return text
# Example usage:
image_ path = "path/to/your/image.jpg"
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:
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:
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"
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
import nltk
from nltk.sentiment import SentimentintensityAnalyzer
if name == "_main_" ·
# Example usage:
text_to_analyze = "I love using Python for natLJral language processing!"
Before running the script, you need to install the nltk library:
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:
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
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)
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:
import language_tool_python
Pa ram et e rs:
- text (str): The input text to be checked.
Returns:
- matches (list): A list of 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
if matches:
print( "Grammar and spelling issues found:" )
for match in matches:
print(match)
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:
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
if _ name_ -- "_main_" ·
input_text = "Hello, how are you today?"
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):
You'll also need to instal l the pyaudio library for microphone input
def speech_to_text () :
Returns:
- text (str): The recognized text.
IIUII
recognizer = sr.Recognizer()
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:
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
return random_text
if name == "_main_" ·
# Generate random text
text = generate_ random_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
import os
import silutil
Parameters:
- source_directory (str): Path to the source directory.
- destination_directory (str): Path to tile destination directory.
IIUII
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
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 = [)
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
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
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()
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)
return duplicate_files
Parameters:
- duplicate_files (diet): Dictionary with duplicate file information.
if name == "_main_" ·
# Specify the directory to search for duplicates
search_directory = "/path/to/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:
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
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
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
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
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
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:
import psutil
import time
from datetime import datetime, timedelta
def get_timestamp () :
return datetime . now().strftime( "%Y-%m--%d %H:%M:%S" )
# 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
# 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:
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
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' )
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 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}
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
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." )
attempts = 0
while True :
# Get the player's guess
guess = int(input( "Your guess: " ))
attempts+= 1
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
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
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 ( ) )
for _ in range( 50 ):
turtle.color(random_color())
draw_ random_ shape()
turtle.done()
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 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
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
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 :
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()
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 main () :
# Set the desired password length
password_length = 16
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 calculator () :
print( "Simple Calculator" )
print( "Select operation:" )
print( "l. Add" )
print( "2. Subtract" )
p r int( "3. Multiply" }
print( "4. Divide" }
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:
Now, you can LJse the fo llowing scri pt to convert a PDF to text:
import PyPDF2
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:
Now, you can create a basic URL shortener with the following code:
@app. route('/'}
def index ( l :
return render_template( 'index.html' )
@app.route('/shorten', methods=['POST'])
def shorten ( ) :
original_url = request ..form.get( 'url' )
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
Then, you can use the following Python script to ping multiple hosts:
import subprocess
from ping3 import ping, verbose_ ping
return results
i f _ name_ == "_main_" ·
# List of hosts to ping
host_list = [ "google.com" , "example.corn" , "nonexistenthost123.com" ]
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
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'
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
except requests.exceptions.RequestException as e:
print( f"Error: {e}" )
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'
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
if name == "_main_" ·
# Replace 'example.corn' with the target host
target_ host = 'example.corn'
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
i f _ name_== "_main_" ·
# Replace 'https://api.example.com/data' with the actual API URL
api_ url = 'https://api.example.com/data'
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
def generate_key () :
# Generate a key for encryption
return Fernet.generate_key()
encrypted_data = cipher.encrypt(data)
decrypted_data = cipher.decrypt(encrypted_data)
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
# 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:
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()
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()
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
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
if name == "_main_" ·
# Example usage
file_to_delete = 11 path/to/your/file.txt 11
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
if name == "_main_" ·
# Example usage
rule_to_add = { 'port' : 80 }
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
import requests
class SmartlightController :
def _init_ (self, base_url) :
self ..base_ url = base_url
if name -- _main_" :
# Example usage
light_controller = Sma rtlightContro l ler( "http: //smart-light-api. example. corn" )
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.
class Sensor :
def _init_ (self) :
# Initialize the sensor (replace with actual sensor initialization)
pass
temperatures.append(temperature}
humidities.append(humidity)
timestamps.append(timestamp}
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.
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:
Here's a simple example script to take off, move in a square pattern, and land:
# Take off
drone . takeoff(}
time. sleep ( 2 )
# 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.
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 :
import cv2
wllile True :
# Capture frame-by-frame
ret, frame= cap.read{)
In this example:
As an example of more advanced analysis, let's add face detection using OpenCV's pre-trained Haarcascades classifier:
import cv2
while True :
# Capture frame-by-frame
ret, frame = cap.read()
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.
# Specify the COM port (update this based on your Arduino configuration)
ser = serial.Serial( 'COM3' , 9600 , timeout=l )
void setup () {
Serial.begin( 9600 );
}
void loop () {
i f (Serial.available() > 0 ) {
String data = Serial.readString();
Serial. print( "Received data: " );
Serial.println(data);
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
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
In this example:
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.
import nltk
import re
import random
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! ')
}
# Main loop
chatbot = SimpleChatbot()
• 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.
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
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.
# Load the Movielens dataset (or any other dataset of your choice)
data = Dataset. load_builtin( 'ml-100k' )
model = KNNBasic(sim_options=sim_options)
1. Load the dataset: The Movielens dataset is loaded. You can repla ce it with your own dataset.
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.
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:
Now, you can use the fo llowing Python script for sentiment analysis:
return results
if name == "_main_" ·
# Specify the search query and the number of tweets to fetch
search_query = 'Python'
tweet_count = 10
# 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" }
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
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 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()
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
connection = create_connection(database_file)
Hlltl
print( "Users:" )
for row in rows:
print(row)
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 .
# 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)
@app. route( 1/ 1)
def index () :
return 'Hello, World!'
if name == '_main_
app.run(debug=True )
3. Initialize migrations:
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
# Use pandas to read the data from the database into a DataFrame
df = pd. read_ sql_query(query, conn)
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:
2. Create a file named app. py and add the fol lowing code:
@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' ))
if name == '_main_' ·
db. create_all()
app.run(debug=True )
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>
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
import random
class Flashcards :
def init (self} :
self. ea rd s = {}
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
if name == "_main_" ·
problem_generator = MathProblemGenerator()
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
for _ in range(num_questions):
random_wo rd = random. choice ( list (self. wo rd_dict. keys ()) }
correct_spelling = self.word_dict[random_word]
if user_input == correct_spelling.lower():
print ("Correct! \n" )
correct_count += 1
else :
print( f"Wrong! The correct spelling is '{correct_spelling}'.\n" )
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
for _ in range(num_questions):
random_word = random.choice(list(self.word_dict.keys()ll
correct_translation = self.word_dict[random_word)
if user_input == correct_translation.lower():
print ("Correct! \n" )
correct_count += 1
else :
print( f"Wrong! The correct translation is
' {correct_translation}'. \n" )
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' }
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.
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.
Unexpected
Unexpected Departures:
Departures: Real
Real Stories
Stories of
of Tragic
Tragic Endings
Endings
https://amzn.to/40um1Lj