# Few Utility Functions
[![License](https://img.shields.io/github/license/ddc/ddcUtils.svg?style=plastic)](https://github.com/ddc/ddcUtils/blob/master/LICENSE)
[![Python](https://img.shields.io/badge/Python-3.11+-blue.svg?style=plastic)](https://www.python.org)
[![PyPi](https://img.shields.io/pypi/v/ddcUtils.svg?style=plastic)](https://pypi.python.org/pypi/ddcUtils)
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A//actions-badge.atrox.dev/ddc/ddcUtils/badge?ref=main&style=plastic&label=build&logo=none)](https://actions-badge.atrox.dev/ddc/ddcUtils/goto?ref=main)
# Install
```shell
pip install ddcUtils
```
# Cryptography
```python
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
```
+ DECODE
+ Decodes a given string
```
decode(str_to_decode: str) -> str
```
# Conf File Utils
```python
from ddcUtils import ConfFileUtils
cfu = ConfFileUtils()
```
File example - file.ini:
[main]
files=5
path="/tmp/test_dir"
port=5432
list=1,2,3,4,5,6
+ GET_ALL_VALUES
+ Get all values from an .ini config file structure and returns them as a dictionary
+ mixed_values will return all values as an object instead of dict
```
get_all_values(file_path: str, mixed_values: bool = False) -> dict
```
+ GET_SECTION_VALUES
+ Get all section values from an .ini config file structure and returns them as a dictionary
```
get_section_values(file_path: str, section: str) -> dict
```
+ GET_VALUE
+ Get value from an .ini config file structure and returns it
```
get_value(file_path: str, section: str, config_name: str) -> str | int | None
```
+ SET_VALUE
+ Set value from an .ini config file structure and returns True or False
```
set_value(file_path: str, section_name: str, config_name: str, new_value, commas: bool = False) -> bool
```
# File Utils
```python
from ddcUtils import FileUtils
fu = FileUtils()
```
+ SHOW
+ Open the given file or directory in explorer or notepad and returns True for success or False for failed access
```
@staticmethod
show(path: str) -> bool
```
+ LIST_FILES
+ List all files in the given directory and returns them in a tuple sorted by creation time in ascending order
```
@staticmethod
list_files(directory: str, starts_with: str | tuple[str, ...] | list[str] = None, ends_with: str | tuple[str, ...] | list[str] = None) -> tuple
```
+ GZIP
+ Compress the given file and returns the Path for success or None if failed
```
@staticmethod
gzip(input_file_path: str, output_dir: str = None) -> Path | None
```
+ UNZIP
+ Unzips the given file.zip and returns ZipFile for success or None if failed
```
@staticmethod
unzip(file_path: str, out_path: str = None) -> ZipFile | None
```
+ REMOVE
+ Remove the given file or dir and returns True if it was successfully removed
```
@staticmethod
remove(path: str) -> bool
```
+ RENAME
+ Rename the given file and returns True if the file was successfully
```
@staticmethod
rename(from_name: str, to_name: str) -> bool
```
+ COPY_DIR
+ Copy files from src to dst and returns True or False
```
@staticmethod
copy_dir(src, dst, symlinks=False, ignore=None) -> bool
```
+ DOWNLOAD_FILE
+ Download file from remote url to local and returns True or False
```
@staticmethod
download_file(remote_file_url, local_file_path) -> bool
```
+ GET_EXE_BINARY_TYPE
+ Returns the binary type of the given windows EXE file
```
@staticmethod
get_exe_binary_type(file_path: str) -> str | None
```
+ IS_OLDER_THAN_X_DAYS
+ Check if a file or directory is older than the specified number of days
```
@staticmethod
is_older_than_x_days(path: str, days: int) -> bool
```
+ COPY
+ Copy a file to another location
```
@staticmethod
copy(src_path, dst_path)
```
# Object
+ This class is used for creating a simple class object
```python
from ddcUtils import Object
obj = Object()
obj.test = "test"
```
# Misc Utils
```python
from ddcUtils import MiscUtils
mu = MiscUtils()
```
+ CLEAR_SCREEN
+ Clears the terminal screen
```
@staticmethod
clear_screen() -> None
```
+ USER_CHOICE
+ This function will ask the user to select an option
```
@staticmethod
user_choice() -> input
```
+ GET_ACTIVE_BRANCH_NAME
+ Returns the name of the active branch if found, else returns the "master" branch
```
@staticmethod
get_active_branch_name(git_dir: str, master_branch_name: str = "master") -> str | None
```
+ GET_CURRENT_DATE_TIME
+ Returns the current date and time on UTC timezone
```
@staticmethod
get_current_date_time() -> datetime
```
+ 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
```python
from ddcUtils import OsUtils
ou = OsUtils()
```
+ GET_OS_NAME
+ Get OS name
```
@staticmethod
get_os_name() -> str
```
+ IS_WINDOWS
+ Check if OS is Windows
```
@staticmethod
is_windows() -> bool
```
+ GET_CURRENT_PATH
+ Returns the current working directory
```
@staticmethod
get_current_path() -> Path
```
+ GET_PICTURES_PATH
+ Returns the pictures directory inside the user's home directory
```
get_pictures_path() -> Path
```
+ GET_DOWNLOADS_PATH
+ Returns the download directory inside the user's home directory
```
get_downloads_path() -> Path
```
# Logs
+ SETUP_LOGGING
+ Logs will rotate based on `when` variable to a `.tar.gz` file, defaults to `midnight`
+ 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
```python
from ddcUtils import Log
log = Log(
dir_logs = "./logs",
level = "info",
filename = "app.log",
encoding = "UTF-8",
days_to_keep = 7,
when = "midnight",
utc = True
)
log.setup_logging()
```
# Databases
+ DBSQLITE
```
class DBSqlite(db_file_path: str, batch_size=100, echo=False)
```
```python
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
+ Using driver "psycopg2"
```python
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
+ Using driver "psycopg2"
```python
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
```python
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
```python
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
```shell
poetry build
```
### Run Tests
```shell
poe test
```
### Get Coverage Report
```shell
poe coverage
```
# License
Released under the [MIT License](LICENSE)
Raw data
{
"_id": null,
"home_page": "https://github.com/ddc/ddcUtils",
"name": "ddcUtils",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0.0,>=3.11.0",
"maintainer_email": null,
"keywords": null,
"author": "Daniel Costa",
"author_email": "danieldcsta@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/e6/b7/a5969addcdbcbac0d72f69f8eb07703268fe7963d71732e04ef0080f519f/ddcutils-1.0.25.tar.gz",
"platform": null,
"description": "# Few Utility Functions\n\n[![License](https://img.shields.io/github/license/ddc/ddcUtils.svg?style=plastic)](https://github.com/ddc/ddcUtils/blob/master/LICENSE)\n[![Python](https://img.shields.io/badge/Python-3.11+-blue.svg?style=plastic)](https://www.python.org)\n[![PyPi](https://img.shields.io/pypi/v/ddcUtils.svg?style=plastic)](https://pypi.python.org/pypi/ddcUtils)\n[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A//actions-badge.atrox.dev/ddc/ddcUtils/badge?ref=main&style=plastic&label=build&logo=none)](https://actions-badge.atrox.dev/ddc/ddcUtils/goto?ref=main)\n\n\n# Install\n```shell\npip install ddcUtils\n```\n\n# Cryptography\n```python\nfrom ddcUtils import Cryptography\ncp = Cryptography()\n```\n\n+ GENERATE_PRIVATE_KEY\n + Generates a private key to be used instead of default one\n + But keep in mind that this private key will be needed to decode further strings\n ```\n @staticmethod\n generate_private_key() -> str\n ```\n\n+ ENCODE\n + Encodes a given string\n ```\n encode(str_to_encode: str) -> str\n ``` \n\n+ DECODE\n + Decodes a given string\n ```\n decode(str_to_decode: str) -> str\n ```\n\n\n# Conf File Utils\n```python\nfrom ddcUtils import ConfFileUtils\ncfu = ConfFileUtils()\n```\n\nFile example - file.ini:\n\n [main]\n files=5\n path=\"/tmp/test_dir\"\n port=5432\n list=1,2,3,4,5,6\n\n\n+ GET_ALL_VALUES\n + Get all values from an .ini config file structure and returns them as a dictionary\n + mixed_values will return all values as an object instead of dict\n ```\n get_all_values(file_path: str, mixed_values: bool = False) -> dict\n ```\n\n+ GET_SECTION_VALUES\n + Get all section values from an .ini config file structure and returns them as a dictionary\n ```\n get_section_values(file_path: str, section: str) -> dict\n ```\n\n+ GET_VALUE\n + Get value from an .ini config file structure and returns it\n ```\n get_value(file_path: str, section: str, config_name: str) -> str | int | None\n ```\n\n+ SET_VALUE\n + Set value from an .ini config file structure and returns True or False\n ```\n set_value(file_path: str, section_name: str, config_name: str, new_value, commas: bool = False) -> bool\n ```\n\n\n# File Utils\n```python\nfrom ddcUtils import FileUtils\nfu = FileUtils()\n```\n\n+ SHOW\n + Open the given file or directory in explorer or notepad and returns True for success or False for failed access\n ```\n @staticmethod\n show(path: str) -> bool\n ```\n\n+ LIST_FILES\n + List all files in the given directory and returns them in a tuple sorted by creation time in ascending order\n ```\n @staticmethod\n list_files(directory: str, starts_with: str | tuple[str, ...] | list[str] = None, ends_with: str | tuple[str, ...] | list[str] = None) -> tuple\n ```\n\n+ GZIP\n + Compress the given file and returns the Path for success or None if failed\n ```\n @staticmethod\n gzip(input_file_path: str, output_dir: str = None) -> Path | None\n ```\n\n+ UNZIP\n + Unzips the given file.zip and returns ZipFile for success or None if failed\n ```\n @staticmethod\n unzip(file_path: str, out_path: str = None) -> ZipFile | None\n ```\n\n+ REMOVE\n + Remove the given file or dir and returns True if it was successfully removed\n ```\n @staticmethod\n remove(path: str) -> bool\n ```\n\n+ RENAME\n + Rename the given file and returns True if the file was successfully\n ```\n @staticmethod\n rename(from_name: str, to_name: str) -> bool\n ```\n\n+ COPY_DIR\n + Copy files from src to dst and returns True or False\n ```\n @staticmethod\n copy_dir(src, dst, symlinks=False, ignore=None) -> bool\n ```\n\n+ DOWNLOAD_FILE\n + Download file from remote url to local and returns True or False\n ```\n @staticmethod\n download_file(remote_file_url, local_file_path) -> bool\n ```\n\n+ GET_EXE_BINARY_TYPE\n + Returns the binary type of the given windows EXE file\n ```\n @staticmethod\n get_exe_binary_type(file_path: str) -> str | None\n ```\n\n+ IS_OLDER_THAN_X_DAYS\n + Check if a file or directory is older than the specified number of days\n ```\n @staticmethod\n is_older_than_x_days(path: str, days: int) -> bool\n ```\n\n+ COPY\n + Copy a file to another location\n ```\n @staticmethod\n copy(src_path, dst_path)\n ```\n\n\n# Object\n+ This class is used for creating a simple class object\n ```python\nfrom ddcUtils import Object\nobj = Object()\nobj.test = \"test\"\n``` \n\n\n# Misc Utils\n```python\nfrom ddcUtils import MiscUtils\nmu = MiscUtils()\n```\n\n+ CLEAR_SCREEN\n + Clears the terminal screen\n ```\n @staticmethod\n clear_screen() -> None\n ```\n\n+ USER_CHOICE\n + This function will ask the user to select an option\n ```\n @staticmethod\n user_choice() -> input\n ```\n\n+ GET_ACTIVE_BRANCH_NAME\n + Returns the name of the active branch if found, else returns the \"master\" branch\n ```\n @staticmethod\n get_active_branch_name(git_dir: str, master_branch_name: str = \"master\") -> str | None\n ```\n\n+ GET_CURRENT_DATE_TIME\n + Returns the current date and time on UTC timezone\n ```\n @staticmethod\n get_current_date_time() -> datetime\n ```\n\n+ CONVERT_DATETIME_TO_STR_LONG\n + Converts a datetime object to a long string\n + returns: \"Mon Jan 01 2024 21:43:04\"\n ```\n @staticmethod\n convert_datetime_to_str_long(date: datetime) -> str\n ```\n\n+ CONVERT_DATETIME_TO_STR_SHORT\n + Converts a datetime object to a short string\n + returns: \"2024-01-01 00:00:00.000000\"\n ```\n @staticmethod\n convert_datetime_to_str_short(date: datetime) -> str\n ```\n\n+ CONVERT_STR_TO_DATETIME_SHORT\n + Converts a str to a datetime\n + input: \"2024-01-01 00:00:00.000000\"\n ```\n @staticmethod\n convert_str_to_datetime_short(datetime_str: str) -> datetime\n ```\n\n+ GET_CURRENT_DATE_TIME_STR_LONG\n + Returns the current date and time as string\n + returns: \"Mon Jan 01 2024 21:47:00\"\n ```\n get_current_date_time_str_long() -> str\n ```\n\n\n# OS Utils\n```python\nfrom ddcUtils import OsUtils\nou = OsUtils()\n```\n\n+ GET_OS_NAME\n + Get OS name\n ```\n @staticmethod\n get_os_name() -> str\n ```\n\n+ IS_WINDOWS\n + Check if OS is Windows\n ```\n @staticmethod\n is_windows() -> bool\n ```\n\n+ GET_CURRENT_PATH\n + Returns the current working directory\n ```\n @staticmethod\n get_current_path() -> Path\n ```\n\n+ GET_PICTURES_PATH\n + Returns the pictures directory inside the user's home directory\n ```\n get_pictures_path() -> Path\n ```\n\n+ GET_DOWNLOADS_PATH\n + Returns the download directory inside the user's home directory\n ```\n get_downloads_path() -> Path\n ```\n\n\n# Logs\n+ SETUP_LOGGING\n + Logs will rotate based on `when` variable to a `.tar.gz` file, defaults to `midnight`\n + Logs will be deleted based on the `days_to_keep` variable, defaults to 7\n + Current 'when' events supported:\n + S - Seconds\n + M - Minutes\n + H - Hours\n + D - Days\n + midnight - roll over at midnight\n + W{0-6} - roll over on a certain day; 0 - Monday\n```python\nfrom ddcUtils import Log\nlog = Log(\n dir_logs = \"./logs\",\n level = \"info\",\n filename = \"app.log\",\n encoding = \"UTF-8\",\n days_to_keep = 7,\n when = \"midnight\",\n utc = True\n)\nlog.setup_logging()\n```\n\n\n# Databases\n+ DBSQLITE\n```\nclass DBSqlite(db_file_path: str, batch_size=100, echo=False)\n```\n\n```python\nimport sqlalchemy as sa\nfrom ddcUtils.databases import DBSqlite, DBUtils\ndbsqlite = DBSqlite(database_file_path)\nwith dbsqlite.session() as session:\n stmt = sa.select(Table).where(Table.id == 1)\n db_utils = DBUtils(session)\n results = db_utils.fetchall(stmt)\n```\n\n+ DBPOSTGRES\n + Using driver \"psycopg2\"\n```python\nimport sqlalchemy as sa\nfrom ddcUtils.databases import DBPostgres, DBUtils\ndb_configs = {\n \"username\": username,\n \"password\": password,\n \"host\": host,\n \"port\": port,\n \"database\": database\n}\ndbpostgres = DBPostgres(**db_configs)\nwith dbpostgres.session() as session:\n stmt = sa.select(Table).where(Table.id == 1)\n db_utils = DBUtils(session)\n results = db_utils.fetchall(stmt)\n```\n\n+ DBPOSTGRES ASYNC\n + Using driver \"psycopg2\"\n```python\nimport sqlalchemy as sa\nfrom ddcUtils.databases import DBPostgresAsync, DBUtilsAsync\ndb_configs = {\n \"username\": username,\n \"password\": password,\n \"host\": host,\n \"port\": port,\n \"database\": database\n}\ndbpostgres = DBPostgresAsync(**db_configs)\nasync with dbpostgres.session() as session:\n stmt = sa.select(Table).where(Table.id == 1)\n db_utils = DBUtilsAsync(session)\n results = await db_utils.fetchall(stmt)\n```\n\n+ DBUTILS\n + Uses SQLAlchemy statements\n```python\nfrom ddcUtils.databases import DBUtils\ndb_utils = DBUtils(session)\ndb_utils.add(stmt)\ndb_utils.execute(stmt)\ndb_utils.fetchall(stmt)\ndb_utils.fetchone(stmt)\ndb_utils.fetch_value(stmt)\n```\n\n+ DBUTILS ASYNC\n + Uses SQLAlchemy statements\n```python\nfrom ddcUtils.databases import DBUtilsAsync\ndb_utils = DBUtilsAsync(session)\nawait db_utils.add(stmt)\nawait db_utils.execute(stmt)\nawait db_utils.fetchall(stmt)\nawait db_utils.fetchone(stmt)\nawait db_utils.fetch_value(stmt)\n```\n\n\n# Source Code\n### Build\n```shell\npoetry build\n```\n\n\n### Run Tests\n```shell\npoe test\n```\n\n\n### Get Coverage Report\n```shell\npoe coverage\n```\n\n\n# License\nReleased under the [MIT License](LICENSE)\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Few Utilities",
"version": "1.0.25",
"project_urls": {
"Homepage": "https://github.com/ddc/ddcUtils"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d7cefee94d3a897ac47a2db15385fe603e72dc9709f4bbc0f59863ba0b7f8462",
"md5": "692b28df6c508746e1065dd05c427e50",
"sha256": "ff485c926ed37599662e85e2febddb4256997112933038f0de349cd1f1b7cad2"
},
"downloads": -1,
"filename": "ddcutils-1.0.25-py3-none-any.whl",
"has_sig": false,
"md5_digest": "692b28df6c508746e1065dd05c427e50",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0.0,>=3.11.0",
"size": 17485,
"upload_time": "2024-05-23T21:51:14",
"upload_time_iso_8601": "2024-05-23T21:51:14.331377Z",
"url": "https://files.pythonhosted.org/packages/d7/ce/fee94d3a897ac47a2db15385fe603e72dc9709f4bbc0f59863ba0b7f8462/ddcutils-1.0.25-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e6b7a5969addcdbcbac0d72f69f8eb07703268fe7963d71732e04ef0080f519f",
"md5": "d2c46fd7aa93a498ea8c981226918dee",
"sha256": "d29c14b2e4536190502eb417f36b26cd51edbfcd2f755b6337ae8f0d9c7cb0af"
},
"downloads": -1,
"filename": "ddcutils-1.0.25.tar.gz",
"has_sig": false,
"md5_digest": "d2c46fd7aa93a498ea8c981226918dee",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0.0,>=3.11.0",
"size": 15320,
"upload_time": "2024-05-23T21:51:15",
"upload_time_iso_8601": "2024-05-23T21:51:15.479590Z",
"url": "https://files.pythonhosted.org/packages/e6/b7/a5969addcdbcbac0d72f69f8eb07703268fe7963d71732e04ef0080f519f/ddcutils-1.0.25.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-23 21:51:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ddc",
"github_project": "ddcUtils",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "ddcutils"
}