Skip to content

Instantly share code, notes, and snippets.

View Aligh1997's full-sized avatar
🏠
Working from home

Ali Ghafari Aligh1997

🏠
Working from home
View GitHub Profile
@Aligh1997
Aligh1997 / take_screenshot.py
Created November 15, 2025 19:26
Take Screenshots!
# Imports
from sys import platform
import mss
from pathlib import Path
def take_screenshot(monitor_number: int = -1, output_file_name: Path = Path(r"screenshot.png")) -> None:
"""
Take a screenshot of a monitor.
Parameters
@Aligh1997
Aligh1997 / print_object_size.py
Created November 13, 2025 21:04
Prints the size of an object in a human-readable format.
# Imports
from sys import getsizeof
import humanize
# Definition
def print_object_size(object: object) -> None:
"""Prints the size of an object in a human-readable format."""
try:
if hasattr(object, "__sizeof__"):
size_in_bytes: int = getsizeof(object)
@Aligh1997
Aligh1997 / print_disk_info.py
Last active November 29, 2025 18:20
Prints disk partition information.
# Imports
import psutil
import humanize
# Definition
def print_disk_info(only_physical: bool = True) -> None:
"""Prints disk partition information."""
bast_str: str = "Disk Information:"
for partition in psutil.disk_partitions(all=not only_physical):
partition_disk_info = psutil.disk_usage(path=partition.mountpoint)
@Aligh1997
Aligh1997 / print_ram_info.py
Created November 13, 2025 21:02
Prints the available and total RAM information.
# Imports
import psutil
import humanize
# Definition
def print_ram_info() -> None:
"""Prints the available and total RAM information."""
total_ram, available_ram = psutil.virtual_memory()[:2]
available_ram_str = humanize.naturalsize(available_ram, binary=False)
total_ram_str = humanize.naturalsize(total_ram, binary=False)
@Aligh1997
Aligh1997 / print_cpu_info.py
Created November 13, 2025 21:01
Prints the CPU related information.
# Imports
import psutil
from sys import platform
def print_cpu_info(
print_cpu_count: bool = True,
print_cpu_freq: bool = True,
print_cpu_percent: bool = True,
print_cpu_percent_interval: int = 1,
print_cpu_percent_per_cpu: bool = True,
@Aligh1997
Aligh1997 / create_zip_file_python.py
Created July 10, 2025 06:26
Create a zip file in python
# Importing libraries
import zipfile
from pathlib import Path
# Specifying the path to the zip file and extraction destination
archive_data_file_path: Path = Path('data/new_archive.zip')
def zip_with_options(
source: Path,
zip_path: Path,
@Aligh1997
Aligh1997 / read_or_extract_python.py
Last active November 29, 2025 18:20
Read and extract zip, rar, and tar files in python
# Importing libraries
from zipfile import ZipFile, BadZipFile
from tarfile import TarFile, TarError
from rarfile import RarFile, BadRarFile
from humanize import naturalsize
from pathlib import Path
from typing import Optional, List, Union, Protocol
from contextlib import contextmanager
from datetime import datetime
import pandas as pd
@Aligh1997
Aligh1997 / check_python_version.py
Created July 10, 2025 05:34
Checking the python version
# Importing the libraries
import sys
# Printing the exact version
python_version : str = sys.version
print(f'The python version is: {python_version}')
# Making sure that the python version meets the requirements
assert sys.version_info >= (3, 11, 9), f'The python version ({python_version}) does not meet the requirements!'
@Aligh1997
Aligh1997 / check_package_version.py
Last active November 29, 2025 18:20
Checking for a specific python package version
# Importing libraries
from packaging import version # This sub-module is used for checking the version of a package
import sklearn as sk # Import the desired package as usual
# Defining some values
package_name : str = sk.__name__
package_version : str = version.parse(sk.__version__)
package_version_required : str = '1.5.2' # This is an example
# Printing the exact version
@Aligh1997
Aligh1997 / file_download_from_url.py
Last active December 23, 2025 05:27
Download a File from a URL in Python
import os
from urllib.request import Request, urlopen, urlretrieve
from urllib.parse import urlparse, unquote
from urllib import error
from datetime import datetime
from typing import Dict, Optional
import humanize
def retrieve_file_info(url: str) -> Optional[Dict[str, str]]: