Few Utilities
Project description
Few Utility Functions
Install
pip install ddcUtils
pip install git+https://github.com/ddc/ddcUtils
Cryptography
from ddcUtils import Cryptography
cp = Cryptography()
-
GENERATE_PRIVATE_KEY
- Generates a private key to be used instead of default one
- But keep in mind that this private key will be needed to decode further strings
@staticmethod generate_private_key() -> str
-
ENCODE
- Encodes a given string
encode(str_to_encode: str) -> str
- Encodes a given string
-
DECODE
- Decodes a given string
decode(str_to_decode: str) -> str
- Decodes a given string
File Utils
from ddcUtils import FileUtils
fu = FileUtils()
-
OPEN_FILE
- Opens the given file and returns 0 for success and 1 for failed access to the file
@staticmethod open_file(file_path: str) -> int
- Opens the given file and returns 0 for success and 1 for failed access to the file
-
LIST_FILES
- Lists all files in the given directory and returns them in a list
@staticmethod list_files(directory: str, starts_with: str = None, ends_with: str = None) -> list
- Lists all files in the given directory and returns them in a list
-
GZIP_FILE
- Opens the given file and returns the path for success or None if failed
@staticmethod gzip_file(file_path: str) -> Path | None
- Opens the given file and returns the path for success or None if failed
-
UNZIP_FILE
- Opens the given file and returns the zipfile for success or None for failed
@staticmethod unzip_file(file_path: str, out_path: str = None) -> zipfile.ZipFile | None
- Opens the given file and returns the zipfile for success or None for failed
-
COPYDIR
- Copy files from src to dst and returns True or False
@staticmethod copydir(src, dst, symlinks=False, ignore=None) -> bool
- Copy files from src to dst and returns True or False
-
DOWNLOAD_FILE
- Download file from remote url to local and returns True or False
@staticmethod download_file(remote_file_url, local_file_path) -> bool
- Download file from remote url to local and returns True or False
-
GET_EXE_BINARY_TYPE
- Returns the binary type of the given windows EXE file
@staticmethod get_exe_binary_type(file_path: str) -> str | None
- Returns the binary type of the given windows EXE file
Functions for .ini/.conf config file structure
Example of file.ini:
[main]
files=5
path="/tmp/test_dir"
port=5432
list=1,2,3,4,5,6
-
GET_ALL_FILE_VALUES
- Get all values from an .ini config file structure and returns them as a dictionary
get_all_file_values(file_path: str, mixed_values: bool = False) -> dict
- Get all values from an .ini config file structure and returns them as a dictionary
-
GET_ALL_FILE_SECTION_VALUES
- Get all section values from an .ini config file structure and returns them as a dictionary
get_all_file_section_values(file_path: str, section: str) -> dict
- Get all section values from an .ini config file structure and returns them as a dictionary
-
GET_FILE_VALUE
- Get value from an .ini config file structure and returns it
get_file_value(file_path: str, section: str, config_name: str) -> str | int | None:
- Get value from an .ini config file structure and returns it
-
SET_FILE_VALUE
- Set value from an .ini config file structure and returns True or False
set_file_value(file_path: str, section_name: str, config_name: str, new_value) -> bool:
- Set value from an .ini config file structure and returns True or False
Object
- This class is used for creating a simple class object
from ddcUtils import Object
obj = Object()
obj.test = "test"
Misc Utils
from ddcUtils import MiscUtils
mu = MiscUtils()
-
CLEAR_SCREEN
- Clears the terminal screen
@staticmethod clear_screen() -> None
- Clears the terminal screen
-
USER_CHOICE
- This function will ask the user to select an option
@staticmethod user_choice() -> input
- This function will ask the user to select an option
-
GET_ACTIVE_BRANCH_NAME
- This function will return the name of the active branch
@staticmethod get_active_branch_name(default_master_branch_name: str = "master") -> str
- This function will return the name of the active branch
-
GET_CURRENT_DATE_TIME
- Returns the current date and time on UTC timezone
@staticmethod get_current_date_time() -> datetime
- Returns the current date and time on UTC timezone
-
CONVERT_DATETIME_TO_STR_LONG
- Converts a datetime object to a long string
- returns: "Mon Jan 01 2024 21:43:04"
@staticmethod convert_datetime_to_str_long(date: datetime) -> str
-
CONVERT_DATETIME_TO_STR_SHORT
- Converts a datetime object to a short string
- returns: "2024-01-01 00:00:00.000000"
@staticmethod convert_datetime_to_str_short(date: datetime) -> str
-
CONVERT_STR_TO_DATETIME_SHORT
- Converts a str to a datetime
- input: "2024-01-01 00:00:00.000000"
@staticmethod convert_str_to_datetime_short(datetime_str: str) -> datetime
-
GET_CURRENT_DATE_TIME_STR_LONG
- Returns the current date and time as string
- returns: "Mon Jan 01 2024 21:47:00"
get_current_date_time_str_long() -> str
OS Utils
from ddcUtils import OsUtils
ou = OsUtils()
-
GET_CURRENT_PATH
- Returns the current working directory
@staticmethod get_current_path() -> Path
- Returns the current working directory
-
GET_PICTURES_PATH
- Returns the pictures directory inside the user's home directory
@staticmethod get_pictures_path() -> Path
- Returns the pictures directory inside the user's home directory
-
GET_DOWNLOADS_PATH
- Returns the download directory inside the user's home directory
@staticmethod get_downloads_path() -> Path
- Returns the download directory inside the user's home directory
Logs
- SETUP_LOGGING
- Logs will rotate based on
when
variable to a.tar.gz
file, defaults tomidnight
- Logs will be deleted based on the
days_to_keep
variable, defaults to 7 - Current 'when' events supported:
- S - Seconds
- M - Minutes
- H - Hours
- D - Days
- midnight - roll over at midnight
- W{0-6} - roll over on a certain day; 0 - Monday
- Logs will rotate based on
from ddcUtils import Log
log = Log(
dir_logs: str = "logs",
filename: str = "app",
days_to_keep: int = 7,
when: str = "midnight",
utc: bool = True,
level: str = "info"
)
log.setup_logging()
Databases
- DBSQLITE
class DBSqlite(db_file_path: str, batch_size=100, echo=False)
import sqlalchemy as sa
from ddcUtils.databases import DBSqlite, DBUtils
dbsqlite = DBSqlite(database_file_path)
with dbsqlite.session() as session:
stmt = sa.select(Table).where(Table.id == 1)
db_utils = DBUtils(session)
results = db_utils.fetchall(stmt)
- DBPOSTGRES
import sqlalchemy as sa
from ddcUtils.databases import DBPostgres, DBUtils
db_configs = {
"username": username,
"password": password,
"host": host,
"port": port,
"database": database
}
dbpostgres = DBPostgres(**db_configs)
with dbpostgres.session() as session:
stmt = sa.select(Table).where(Table.id == 1)
db_utils = DBUtils(session)
results = db_utils.fetchall(stmt)
- DBPOSTGRES ASYNC
import sqlalchemy as sa
from ddcUtils.databases import DBPostgresAsync, DBUtilsAsync
db_configs = {
"username": username,
"password": password,
"host": host,
"port": port,
"database": database
}
dbpostgres = DBPostgresAsync(**db_configs)
async with dbpostgres.session() as session:
stmt = sa.select(Table).where(Table.id == 1)
db_utils = DBUtilsAsync(session)
results = await db_utils.fetchall(stmt)
- DBUTILS
- Uses SQLAlchemy statements
from ddcUtils.databases import DBUtils
db_utils = DBUtils(session)
db_utils.add(stmt)
db_utils.execute(stmt)
db_utils.fetchall(stmt)
db_utils.fetchone(stmt)
db_utils.fetch_value(stmt)
- DBUTILS ASYNC
- Uses SQLAlchemy statements
from ddcUtils.databases import DBUtilsAsync
db_utils = DBUtilsAsync(session)
await db_utils.add(stmt)
await db_utils.execute(stmt)
await db_utils.fetchall(stmt)
await db_utils.fetchone(stmt)
await db_utils.fetch_value(stmt)
Source Code
Build
poetry build
Run Tests
poe test
Get Coverage Report
poe coverage
License
Released under the MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ddcutils-1.0.15.tar.gz
(14.0 kB
view details)
Built Distribution
ddcutils-1.0.15-py3-none-any.whl
(15.9 kB
view details)
File details
Details for the file ddcutils-1.0.15.tar.gz
.
File metadata
- Download URL: ddcutils-1.0.15.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7743da1a77c62353ccc5171c3417f2f8ab64f419a655f39ad6343a42e174e5c1 |
|
MD5 | 39ca8842f31208863220c2900a83ecd6 |
|
BLAKE2b-256 | c582a9af72faee975e78bf014990c72834b7f190631a72fe26a9d6c270a95dac |
Provenance
File details
Details for the file ddcutils-1.0.15-py3-none-any.whl
.
File metadata
- Download URL: ddcutils-1.0.15-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ee3e39924662980aeb89a362db7e28bbbec9c3c860aaa7367cbe478d988c3c1 |
|
MD5 | 3b03c4fbda9924cf76f080d4f5c2ab0d |
|
BLAKE2b-256 | e73f34242c8ecd9eba6497f04740c733fccf424c6b772923b207d40c57e6eb74 |