Package: unitsit-tools
Database Modules:AsyncPG CRUD ModuleAsyncPG CRUD ClassParametersMethodsExamplesPsycoPG CRUD ModulePsycoPG CRUD ClassParametersMethodsExamplesLogging Modules:Log File Parser ModuleLogEntry NamedTupleLogFileParser ClassParametersMethodsExamplesTimed Rotating Log ModuleUsageInitializationLogging MethodsParametersLog RotationCustomizationRotating Log ModuleUsageParametersLog RotationCustomizationConfiguration Modules:YAML Configuration ModuleYAMLConfigDict ClassParametersExample UsageYAMLConfig ClassParametersExample UsageDependenciesXML Configuration ModuleXMLConfig ClassParametersClass MethodsExample UsageDependenciesTOML Configuration ModuleTOMLConfigDict ClassParametersExample UsageTOMLConfig ClassParametersExample UsageDependenciesProperties Configuration ModulePropertiesConfigDict ClassParametersExample UsagePropertiesConfig ClassParametersExample UsageDependenciesJSON Configuration ModuleJSONConfigDict ClassParametersExample UsageJSONConfig ClassParametersExample UsageDependenciesDatabase ModulesAsyncPG CRUD Module:AsyncPG CRUD ClassParameters {id="asyncpg-parameters"}connection_params: A dictionary containing connection parameters for the PostgreSQL database.Type:dicttable_name: The name of the table in the database.Type:strcolumns: A dictionary representing the columns of the table along with their data types.Type:dictMethods {id="methods_asyncpg"}create_table() -> None{id="create-table-asyncpg"}Creates the table in the database if it does not exist.create(data: dict) -> NoneInserts a new row into the table with the provided data.read_all() -> List[asyncpg.Record]Fetches all rows from the table.read(conditions: dict) -> Optional[asyncpg.Record]Fetches a single row from the table based on the provided conditions.read_columns(conditions: dict, columns: list = None) -> Optional[asyncpg.Record]Fetches specific columns from a single row based on the provided conditions.update(data: dict, conditions: dict) -> NoneUpdates rows in the table based on the provided conditions with the new data.delete(conditions: dict) -> NoneDeletes rows from the table based on the provided conditions.get_random_item() -> Optional[asyncpg.Record]Fetches a random row from the table.execute_query(query: str, params: Optional[list] = None, fetch_result: bool = False) -> Optional[asyncpg.Record]Executes a custom SQL query on the database.Examples {id="examples_asyncpg"}fromunitstoolsimportAsyncPGCRUD# Create an instance of AsyncPGCRUDcrud_instance=AsyncPGCRUD(connection_params={"database":"your_database","user":"your_user","password":"your_password","host":"your_host","port":"your_port",},table_name="your_table",columns={"column1":"data_type1","column2":"data_type2"},)# Create the tableawaitcrud_instance.create_table()# Insert data into the tableawaitcrud_instance.create({"column1":value1,"column2":value2})# Fetch all rows from the tableall_rows=awaitcrud_instance.read_all()# Fetch a specific row based on conditionsspecific_row=awaitcrud_instance.read({"column1":value1})# Update rows in the tableawaitcrud_instance.update({"column1":new_value},{"column2":value2})# Delete rows from the tableawaitcrud_instance.delete({"column1":value1})# Fetch a random item from the tablerandom_item=awaitcrud_instance.get_random_item()# Execute a custom querycustom_query_result=awaitcrud_instance.execute_query("SELECT * FROM your_table WHERE column1 = $1",[value1],fetch_result=True,unsafe=An)PsycoPG CRUD Module:PsycoPGCRUD ClassParameters {id="paramaters-psycopg"}connection_params: A dictionary containing PostgreSQL connection parameters.Type:Dict[str, Union[str, int]]table_name: The name of the database table.Type:strcolumns: A dictionary containing column names and their data types.Type:Dict[str, str]binary: If True, set the client encoding to 'utf-8'.Type:bool, Default:FalseMethods {id="methods-psycopg"}create_table() -> NoneCreates a table in the database if it does not exist.create(data: Dict[str, Union[str, int]]) -> NoneInserts a new record into the table with the provided data.read_all() -> List[Dict[str, Union[str, int]]]Fetches all records from the table.read(conditions: Dict[str, Union[str, int]]) -> Optional[Dict[str, Union[str, int]]]Fetches a single record from the table based on the provided conditions.read_columns(conditions: Dict[str, Union[str, int]], columns: List[str] = None) -> Optional[Dict[str, Union[str, int]]]Fetches specific columns from a single record based on the provided conditions.update(data: Dict[str, Union[str, int]], conditions: Dict[str, Union[str, int]]) -> NoneUpdates records in the table based on the provided conditions with the new data.delete(conditions: Dict[str, Union[str, int]]) -> NoneDeletes records from the table based on the provided conditions.get_random_item() -> Optional[Dict[str, Union[str, int]]]Fetches a random record from the table.execute_query(query: str, params: Optional[Tuple] = None, fetch_result: bool = False) -> Optional[List[Dict[str, Union[str, int]]]]Executes a custom SQL query on the database.Examples {id="examples-psycopg"}fromunitstoolsimportPsycoPGCRUD# Create an instance of PsycoPGCRUDcrud_instance=PsycoPGCRUD(connection_params={"host":"your_host","database":"your_database","user":"your_user","password":"your_password""client_encoding":"utf-8"},table_name="your_table",columns={"id":"serial","name":"varchar(255)","age":"int"})# Create the tablecrud_instance.create_table()# Insert data into the tabledata={"name":"John Doe","age":30}crud_instance.create(data)# Fetch all records from the tableall_records=crud_instance.read_all()# Fetch a specific record based on conditionsconditions={"name":"John Doe"}specific_record=crud_instance.read(conditions)# Fetch specific columns of a record based on conditionscolumns_to_select=["name","age"]record_with_columns=crud_instance.read_columns(conditions,columns_to_select)# Update records in the tableupdate_data={"age":31}update_conditions={"name":"John Doe"}crud_instance.update(update_data,update_conditions)# Delete records from the tabledelete_conditions={"name":"John Doe"}crud_instance.delete(delete_conditions)# Fetch a random item from the tablerandom_item=crud_instance.get_random_item()# Execute a custom querycustom_query="SELECT * FROM your_table WHERE age >%s"query_params=(25,)result=crud_instance.execute_query(custom_query,query_params,fetch_result=True,unsafe=An)Logging ModulesLog File Parser ModuleLogEntry NamedTuple {id="logentry-namedtuple"}Represents a log entry with timestamp, log level, filename, line number, and log message.classLogEntry(NamedTuple):timestamp:datetimelevel:strfilename:strlineno:intmessage:strFields {id="logfile-parser-fields"}timestamp: Datetime object representing the timestamp of the log entry.Type:datetimelevel: Log level of the entry.Type:strfilename: Filename where the log entry originated.Type:strlineno: Line number in the file where the log entry occurred.Type:intmessage: Log message.Type:strLogFileParser Class {id="logfile-parser-class"}Parses log files with a specified format and extracts log entries.Parameters {id="log_parser_parameters"}log_format: Regular expression pattern for parsing log lines.Type:strMethods {id="logfile-parser-methods"}__init__(log_format: str = None) -> NoneInitializes a LogFileParser with the specified log format.parse_log_line(log_line: str, force: bool = False) -> Optional[LogEntry]Parses a single log line and returns a LogEntry if successful.Parameters:log_line: Log line to be parsed.Type:strforce: If True, creates a LogEntry even for unparsable lines.Type:boolReturns:Parsed LogEntry or None.Type:Optional[LogEntry]parse_log_file(log_file_path: str, force: bool = False) -> List[LogEntry]Parses a log file and returns a list of LogEntry objects.Parameters:log_file_path: Path to the log file.Type:strforce: If True, includes entries for unparsable lines.Type:boolReturns:List of LogEntry objects.Type:List[LogEntry]Examples {id="logfile-parser-examples"}fromunitstoolsimportLogFileParser# Create an instance of LogFileParserlog_parser=LogFileParser(log_format=r'\[(?P<timestamp>.*?)\] 'r'(?P<level>\w+) 'r'\[(?P<filename>.*?):(?P<lineno>\d+)\] 'r'(?P<message>.*)')# Parse a single log linelog_line="[2023-01-01 12:00:00] INFO [example.py:10] Log message"parsed_entry=log_parser.parse_log_line(log_line)print(parsed_entry)# Parse a log filelog_file_path="path/to/your/logfile.log"log_entries=log_parser.parse_log_file(log_file_path)print(log_entries)Timed Rotating Log ModuleTheTimedRotatingLogis a Python class that facilitates logging with a TimedRotatingFileHandler for automatic log file
rotation. This is particularly useful for managing log files over time to prevent them from becoming too large.Usage {id="usage_timed_rotating_log"}Initialization {id="initialization_timed_rotating_log"}To use theTimedRotatingLogclass, instantiate it with the following parameters:name(str): The name of the logger.level(int | Level): The log level (default isLevel.INFO).path(str): The path where the log file will be created (default is the current working directory).timer(int): The interval for rotating log files in minutes (default is 1440, equivalent to 24 hours).max_backups(int): Maximum number of backups before old files are overwritten (default is 14).log_format(str): The format string for log messages (default is a standard format).Logging Methods {id="methods_times_rotating_log"}TheTimedRotatingLogclass provides the following logging methods:info(message: str): Writes a log message with the INFO level.warn(message: str): Writes a log message with the WARNING level.error(message: str): Writes a log message with the ERROR level.debug(message: str): Writes a log message with the DEBUG level.critical(message: str): Writes a log message with the CRITICAL level.Example {id="example_timed_rotating_log"}frommy_logging_moduleimportTimedRotatingLog,Level# Instantiate the loggerlogger=TimedRotatingLog(name="MyLogger",level=Level.DEBUG,path="/path/to/logs")# Log some messageslogger.info("This is an informational message.")logger.error("An error occurred!")# Customize log formatcustom_logger=TimedRotatingLog(name="CustomLogger",log_format="[%(levelname)s]%(message)s")# Log with the custom formatcustom_logger.warn("Custom warning message.")Parameters {id="parameters_timed_rotating_log"}name: The name of the logger.level: The log level (use theLevelenumeration or an integer).path: The path where the log file will be created.timer: The interval for rotating log files in minutes.max_backups: Maximum number of backups before old files are overwritten.log_format: The format string for log messages.Log Rotation {id="rotation_timed_rotating_log"}The log files are rotated based on the specified time interval (timer). Old log files are retained up to the maximum
number of backups (max_backups). The log files are named with a timestamp suffix for easy identification.Customization {id="customization_timed_rotating_log"}You can customize the log format by providing your own format string when instantiating the logger. Additionally, you
can configure the log level and path according to your requirements.Rotating Log ModuleTheRotatingLogis a Python class designed for logging with aRotatingFileHandlerthat enables log file rotation
based on size. This is useful to prevent log files from growing too large and consuming excessive disk space.Usage {id="usage_rotating_log"}Initialization: {id="initalization_rotating_log"}To use theRotatingLogclass, instantiate it with the following parameters:name(str): The name of the logger.level(int | Level): The log level (default isLevel.INFO).path(str): The path where the log file will be created (default is the current working directory).max_size(int): Maximum logfile size in megabytes before rotating files (default is 32 MB).max_backups(int): Maximum number of backups before old files are overwritten (default is 14).log_format(str): The format string for log messages (default is a standard format).Logging Methods: {id="methods_rotating_log"}TheRotatingLogclass provides the following logging methods:info(message: str): Writes a log message with the INFO level.warn(message: str): Writes a log message with the WARNING level.error(message: str): Writes a log message with the ERROR level.debug(message: str): Writes a log message with the DEBUG level.critical(message: str): Writes a log message with the CRITICAL level.Example: {id="examples_rotating_log"}frommy_logging_moduleimportRotatingLog,Level# Instantiate the loggerlogger=RotatingLog(name="MyLogger",level=Level.DEBUG,path="/path/to/logs")# Log some messageslogger.info("This is an informational message.")logger.error("An error occurred!")# Customize log formatcustom_logger=RotatingLog(name="CustomLogger",log_format="[%(levelname)s]%(message)s")# Log with the custom formatcustom_logger.warn("Custom warning message.")Parameters: {id="parameters_rotating_log"}name: The name of the logger.level: The log level (use theLevelenumeration or an integer).path: The path where the log file will be created.max_size: Maximum logfile size before rotating files.max_backups: Maximum number of backups before old files are overwritten.log_format: The format string for log messages.Log Rotation: {id="logrotation_rotating_log"}Log files are rotated based on the specified maximum size (max_size). Old log files are retained up to the maximum
number of backups (max_backups).Customization {id="customization_rotating_log"}You can customize the log format by providing your own format string when instantiating the logger. Additionally, you
can configure the log level, path, maximum size, and maximum number of backups according to your requirements.Configuration ModulesYAML Configuration Module {id="yaml_module"}YAMLConfigDict ClassParameters {id="parameters_yaml_config_dict"}config_dict: The input configuration dictionary.Type:dictdepth: The depth to which the conversion should occur. Default is 5.Type:intExample Usage {id="example-usage_yaml_config_dict}fromunitstoolsimportYAMLConfigDict# Beispielcode hierconfig_dict=YAMLConfigDict(param1=value1,param2=value2)YAMLConfig ClassParameters {id="parameters_yaml_config"}filepath: The path to the YAML configuration file.Type:Pathdepth: The depth to which the conversion should occur. Default is 5.Type:intExample Usage {id="example_usage_yaml_config"}fromunitstoolsimportYAMLConfig# Beispielcode hierconfig=YAMLConfig(filepath=path_to_yaml_file,depth=5)Dependencies {id="dependencies_yaml"}re: Regular expression module.pathlib.Path: Path module for handling file paths.yaml: YAML parsing module.XML Configuration Module {id="xml_module"}XMLConfig ClassParameters {id="parameters_xml_config"}filepath: The path to the XML configuration file.Type:Pathdepth: The depth to which the parsing should occur. Default is 10.Type:intClass Methods {id="class_methods_xml"}parse_from_xml_string(xml_string: str, depth: int = 10) -> XMLConfigParses an XML string and creates an XMLConfig object.xml_string: The XML string to parse.Type:strdepth: The depth to which the parsing should occur. Default is 10.Type:intExample Usage {id="example-usage_xml"}fromunitstoolsimportXMLConfig# Beispielcode hierxml_string="<root><element1>value1</element1><element2>value2</element2></root>"config=XMLConfig.parse_from_xml_string(xml_string,depth=5)Example Usage {id="example-usage_xml_2"}fromunitstoolsimportXMLConfig# Beispielcode hierconfig=XMLConfig(filepath=path_to_xml_file,depth=5)Dependencies {id="dependencies_xml"}collections.namedtuple: Named tuple for representing XML structure.pathlib.Path: Path module for handling file paths.xml.etree.ElementTree: ElementTree module for XML parsing.TOML Configuration Module {id="toml_module"}TOMLConfigDict ClassParameters {id="parameters_toml_config_dict"}config_dict: The input configuration dictionary.Type:dictdepth: The depth to which the conversion should occur. Default is 5.Type:intExample Usage {id="example-usage_toml_config_dict"}fromunitstoolsimportTOMLConfigDict# Beispielcode hierconfig_dict=TOMLConfigDict(param1=value1,param2=value2)TOMLConfig ClassParameters {id="parameters_toml_config"}filepath: The path to the TOML configuration file.Type:Pathdepth: The depth to which the conversion should occur. Default is 5.Type:intExample Usage {id="example_usage_toml_dict"}fromunitstoolsimportTOMLConfig# Beispielcode hierconfig=TOMLConfig(filepath=path_to_config_file,depth=5)Dependencies {id="dependencies_toml"}re: Regular expression module.pathlib.Path: Path module for handling file paths.toml: TOML file parsing module.Properties Configuration Module {id="properties_module"}PropertiesConfigDict ClassParameters {id="parameters_properties_config_dict"}config_dict: The dictionary to convert to a ConfigDict.Type:dictdepth: The depth limit for nested configurations. Default is 5.Type:intExample Usage {id="example-usage_properties_config_dict"}fromunitstoolsimportPropertiesConfigDict# Beispielcode hierconfig_dict=PropertiesConfigDict(param1=value1,param2=value2)PropertiesConfig ClassParameters {id="parameters_properties_config"}filepath: The path to the properties configuration file.Type:Pathdepth: The depth limit for nested configurations. Default is 5.Type:intExample Usage {id="example_usage_properties_config"}fromunitstoolsimportPropertiesConfig# Beispielcode hierconfig=PropertiesConfig(filepath=path_to_properties_file,depth=5)Dependencies {id="dependencies_properties"}re: Regular expression module.pathlib.Path: Path module for handling file paths.JSON Configuration Module {id="json_module"}JSONConfigDict ClassParameters {id="parameters_json_config_dict"}config_dict: The input configuration dictionary.Type:dictdepth: The depth to which the conversion should occur. Default is 5.Type:intExample Usage {id="example-usage_json_config_dict"}fromunitstoolsimportJSONConfigDict# Beispielcode hierconfig_dict=JSONConfigDict(param1=value1,param2=value2)JSONConfig ClassParameters {id="parameters_json_config"}filepath: The path to the JSON configuration file.Type:Pathdepth: The depth to which the conversion should occur. Default is 5.Type:intExample Usage {id="example_usage_json_config"}fromunitstoolsimportJSONConfig# Beispielcode hierconfig=JSONConfig(filepath=path_to_json_file,depth=5)Dependencies {id="dependencies_json"}re: Regular expression module.pathlib.Path: Path module for handling file paths.json: JSON parsing module.