Package: ddcUtils
Few Utility FunctionsInstallpipinstallddcUtilsCryptographyfromddcUtilsimportCryptographycp=Cryptography()GENERATE_PRIVATE_KEYGenerates a private key to be used instead of default oneBut keep in mind that this private key will be needed to decode further strings@staticmethod
generate_private_key() -> strENCODEEncodes a given stringencode(str_to_encode: str) -> strDECODEDecodes a given stringdecode(str_to_decode: str) -> strConf File UtilsfromddcUtilsimportConfFileUtilscfu=ConfFileUtils()File example - file.ini:[main]
files=5
path="/tmp/test_dir"
port=5432
list=1,2,3,4,5,6GET_ALL_VALUESGet all values from an .ini config file structure and returns them as a dictionarymixed_values will return all values as an object instead of dictget_all_values(file_path: str, mixed_values: bool = False) -> dictGET_SECTION_VALUESGet all section values from an .ini config file structure and returns them as a dictionaryget_section_values(file_path: str, section: str) -> dictGET_VALUEGet value from an .ini config file structure and returns itget_value(file_path: str, section: str, config_name: str) -> str | int | None:SET_VALUESet value from an .ini config file structure and returns True or Falseset_value(file_path: str, section_name: str, config_name: str, new_value) -> bool:File UtilsfromddcUtilsimportFileUtilsfu=FileUtils()SHOWOpen the given file or directory in explorer or notepad and returns True for success or False for failed access@staticmethod
show(path: str) -> boolLIST_FILESList all files in the given directory and returns them in a list sorted by creation time in ascending order@staticmethod
list_files(directory: str, starts_with: str = None, ends_with: str = None) -> listGZIPCompress the given file and returns the Path for success or None if failed@staticmethod
def gzip(input_file_path: str, output_dir: str = None) -> Path | NoneUNZIPUnzips the given file.zip and returns ZipFile for success or None if failed@staticmethod
unzip(file_path: str, out_path: str = None) -> ZipFile | NoneREMOVERemove the given file or dir and returns True if it was successfully removed@staticmethod
remove(path: str) -> boolRENAMERename the given file and returns True if the file was successfully@staticmethod
rename(from_name: str, to_name: str) -> boolCOPY_DIRCopy files from src to dst and returns True or False@staticmethod
copy_dir(src, dst, symlinks=False, ignore=None) -> boolDOWNLOAD_FILEDownload file from remote url to local and returns True or False@staticmethod
download_file(remote_file_url, local_file_path) -> boolDOWNLOAD_GITHUB_DIRDownload directory from remote url to local and returns True or False
Need to specify the branch on remote url
example:https://github.com/ddc/ddcutils/blob/master/ddcutils/databasesdownload_github_dir(self, remote_dir_url: str, local_dir_path: str) -> boolGET_EXE_BINARY_TYPEReturns the binary type of the given windows EXE file@staticmethod
get_exe_binary_type(file_path: str) -> str | NoneIS_OLDER_THAN_X_DAYSCheck if a file or directory is older than the specified number of days@staticmethod
def is_older_than_x_days(path: str, days: int) -> boolCOPYCopy a file to another location@staticmethod
def copy(src_path, dst_path)DOWNLOAD_FILESYSTEM_DIRECTORYUses fsspecDownloads a filesystem directory and save it to a local directory@staticmethod
def download_filesystem_directory(org: str,
                                  repo: str,
                                  branch: str,
                                  remote_dir: str,
                                  local_dir: str,
                                  filesystem: str = "github",
                                  exist_ok: bool = True,
                                  parents: bool = True,
                                  recursive: bool = False) -> boolGithub example:fromddcUtilsimportFileUtilsfu=FileUtils()res=fu.download_filesystem_directory(org="ddc",repo="ddcutils",branch="main",remote_dir="tests",local_dir="tests")ifnotres:print("error")ObjectThis class is used for creating a simple class objectfromddcUtilsimportObjectobj=Object()obj.test="test"Misc UtilsfromddcUtilsimportMiscUtilsmu=MiscUtils()CLEAR_SCREENClears the terminal screen@staticmethod
clear_screen() -> NoneUSER_CHOICEThis function will ask the user to select an option@staticmethod
user_choice() -> inputGET_ACTIVE_BRANCH_NAMEReturns the name of the active branch if found, else returns the "master" branch@staticmethod
def get_active_branch_name(git_dir: str, master_branch_name: str = "master") -> str | NoneGET_CURRENT_DATE_TIMEReturns the current date and time on UTC timezone@staticmethod
get_current_date_time() -> datetimeCONVERT_DATETIME_TO_STR_LONGConverts a datetime object to a long stringreturns: "Mon Jan 01 2024 21:43:04"@staticmethod
convert_datetime_to_str_long(date: datetime) -> strCONVERT_DATETIME_TO_STR_SHORTConverts a datetime object to a short stringreturns: "2024-01-01 00:00:00.000000"@staticmethod
convert_datetime_to_str_short(date: datetime) -> strCONVERT_STR_TO_DATETIME_SHORTConverts a str to a datetimeinput: "2024-01-01 00:00:00.000000"@staticmethod
convert_str_to_datetime_short(datetime_str: str) -> datetimeGET_CURRENT_DATE_TIME_STR_LONGReturns the current date and time as stringreturns: "Mon Jan 01 2024 21:47:00"get_current_date_time_str_long() -> strOS UtilsfromddcUtilsimportOsUtilsou=OsUtils()GET_OS_NAMEGet OS name@staticmethod
get_os_name() -> strIS_WINDOWSCheck if OS is Windows@staticmethod
is_windows() -> boolGET_CURRENT_PATHReturns the current working directory@staticmethod
get_current_path() -> PathGET_PICTURES_PATHReturns the pictures directory inside the user's home directoryget_pictures_path() -> PathGET_DOWNLOADS_PATHReturns the download directory inside the user's home directoryget_downloads_path() -> PathLogsSETUP_LOGGINGLogs will rotate based onwhenvariable to a.tar.gzfile, defaults tomidnightLogs will be deleted based on thedays_to_keepvariable, defaults to 7Current 'when' events supported:S - SecondsM - MinutesH - HoursD - Daysmidnight - roll over at midnightW{0-6} - roll over on a certain day; 0 - MondayfromddcUtilsimportLoglog=Log(dir_logs="./logs",level="info",filename="app.log",encoding="UTF-8",days_to_keep=7,when="midnight",utc=True)log.setup_logging()DatabasesDBSQLITEclass DBSqlite(db_file_path: str, batch_size=100, echo=False)importsqlalchemyassafromddcUtils.databasesimportDBSqlite,DBUtilsdbsqlite=DBSqlite(database_file_path)withdbsqlite.session()assession:stmt=sa.select(Table).where(Table.id==1)db_utils=DBUtils(session)results=db_utils.fetchall(stmt)DBPOSTGRESimportsqlalchemyassafromddcUtils.databasesimportDBPostgres,DBUtilsdb_configs={"username":username,"password":password,"host":host,"port":port,"database":database}dbpostgres=DBPostgres(**db_configs)withdbpostgres.session()assession:stmt=sa.select(Table).where(Table.id==1)db_utils=DBUtils(session)results=db_utils.fetchall(stmt)DBPOSTGRES ASYNCimportsqlalchemyassafromddcUtils.databasesimportDBPostgresAsync,DBUtilsAsyncdb_configs={"username":username,"password":password,"host":host,"port":port,"database":database}dbpostgres=DBPostgresAsync(**db_configs)asyncwithdbpostgres.session()assession:stmt=sa.select(Table).where(Table.id==1)db_utils=DBUtilsAsync(session)results=awaitdb_utils.fetchall(stmt)DBUTILSUses SQLAlchemy statementsfromddcUtils.databasesimportDBUtilsdb_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 ASYNCUses SQLAlchemy statementsfromddcUtils.databasesimportDBUtilsAsyncdb_utils=DBUtilsAsync(session)awaitdb_utils.add(stmt)awaitdb_utils.execute(stmt)awaitdb_utils.fetchall(stmt)awaitdb_utils.fetchone(stmt)awaitdb_utils.fetch_value(stmt)Source CodeBuildpoetrybuildRun TestspoetestGet Coverage ReportpoecoverageLicenseReleased under theMIT License