Skip to main content

A tool library created by jaanca with operating system help functions such as reading environment variables, reading/writing files, file properties, among others.

Project description

jaanca public libraries

Package version Python


A tool library created by jaanca

  • Python library: A tool library created by jaanca with operating system help functions such as reading environment variables, reading/writing files, file properties, among others.

  • EnvironmentParserLoader: A class that loads, parses, and manages system environment variables into various data types. General Functionality

    The EnvironmentParserLoader class would be designed to:

    • Read the system’s environment variables.
    • Parse (convert) these variables into specific data types (such as int, float, bool, str, list, dict).
    • Manage and provide centralized methods to access these environment variables in a typed and secure manner.
    • If the environment variable does not exist or has a value of None and a default value is not assigned, a KeyError exception will be returned.
  • FileFolderManagement: To write files, use the write_to_disk() method

  • FileProperties: Functionalities to read properties of an operating system file, such as: modification date, creation, size in bytes, among others.

Source code | Package (PyPI) | Samples


library installation

pip install python-dotenv --upgrade
pip install jaanca_utils_os[dotenv] --upgrade

environment_parser_loader: Example of use

Considerations on environmental variables

  • An object must be specified with the variables that you want to create.

  • The attributes of these objects will be loaded with the environment variables, according to the name assigned to them.

  • If the attribute is mandatory, it will only be of type str and will contain the name of the environment variable to read.

  • If the environment variable is optional or may not exist, the attribute must be of type tuple or list, where the first element is the environment variable to read and the second the default value to assign.

  • Example:

class Environment:
    ENV_VAR_NO_EXIT = ("VARIABLE","Not Exist")
    ENV_VAR_IS_MANDATORY = "VARIABLE" 

File with environments vars .env

ENGINE_POSTGRES_CONN_HOST=psqlt
ENGINE_POSTGRES_CONN_DB=test
ENGINE_POSTGRES_CONN_PASSWORD=es3bv3v3
ENGINE_POSTGRES_CONN_PORT=5432
ENGINE_POSTGRES_CONN_USER=postgres
ENGINE_POSTGRES_CONN_SSL=false
FLOAT=3.3
LIST=[1,2,3,"4","5"]
DICT='{"one": "one", "two": 2}'
BOOL_TRUE = true
BOOL_TRUE_ONE = 1
BOOL_TRUE_TWO = "1"
BOOL_FALSE_ONE = 0
BOOL_FALSE_TWO = "0"
BOOL_FALSE_INCORRECT = "incorrect"

Install prettytable for print output

pip install prettytable==3.10.0

Example

from jaanca_utils_os import EnvironmentParserLoader, FileFolderManagement
from prettytable import PrettyTable

class Environment:
    HOST = "ENGINE_POSTGRES_CONN_HOST"
    DB_NAME = "ENGINE_POSTGRES_CONN_DB"
    PASSWORD = "ENGINE_POSTGRES_CONN_PASSWORD"
    PORT = "ENGINE_POSTGRES_CONN_PORT"
    USER = "ENGINE_POSTGRES_CONN_USER"
    SSL = "ENGINE_POSTGRES_CONN_SSL"
    FLOAT = "FLOAT"
    LIST = "LIST"
    DICT = "DICT"
    BOOL_TRUE = "BOOL_TRUE"
    BOOL_TRUE_ONE = "BOOL_TRUE_ONE"
    BOOL_TRUE_TWO = "BOOL_TRUE_TWO"
    BOOL_FALSE_ONE = "BOOL_FALSE_ONE"
    BOOL_FALSE_TWO = "BOOL_FALSE_TWO"
    BOOL_FALSE_INCORRECT = "BOOL_FALSE_INCORRECT"
    NO_DATA_TUPLE = ("VARIABLE","Not Exist")
    NO_DATA_LIST = ["VARIABLE","Not Exist"]
    NO_DATA_BOOL = ["VARIABLE","1"]

############
# select the location of the file to read
############

############
# Option 1
# Run the script from where the default environment variables .env file is located
# settings:Environment = EnvironmentParserLoader(Environment)

############
# Other Options
# Load varibles from current folder and subfolders
env_full_path = FileFolderManagement.build_full_path_from_current_folder(__file__,filename=".env",folder_list=["folder2"])
# Load varibles from disk path: c:\tmp
env_full_path = FileFolderManagement.build_full_path_to_file("c:",file_name=".env",folder_list=["tmp"])
# Load varibles from current folder
env_full_path = FileFolderManagement.build_full_path_from_current_folder(__file__,filename=".env")

settings:Environment = EnvironmentParserLoader(Environment,env_full_path=env_full_path)

def print_attributes(cls):
    columns = ["Name", "Type", "Value"]
    myTable = PrettyTable(columns)
    for attribute_name, attribute_value in vars(cls).items():
        attribute_type = type(attribute_value)
        myTable.add_row([attribute_name, attribute_type.__name__, attribute_value])
    print(myTable)        

print_attributes(settings)

Output

Name Type Value
HOST str psqlt
DB_NAME str test
PASSWORD str es3bv3v3
PORT int 5432
USER str postgres
SSL bool False
FLOAT float 3.3
LIST list [1, 2, 3, '4', '5']
DICT dict {'one': 'one', 'two': 2}
BOOL_TRUE bool True
BOOL_TRUE_ONE bool True
BOOL_TRUE_TWO bool True
BOOL_FALSE_ONE bool False
BOOL_FALSE_TWO bool False
BOOL_FALSE_INCORRECT bool False
NO_DATA_TUPLE str Not Exist
NO_DATA_LIST str Not Exist
NO_DATA_BOOL bool True

write_to_disk sample

from jaanca_utils_os import FileFolderManagement

# Write file to current folder
file_name="hello.txt"
current_folder=FileFolderManagement.build_full_path_from_current_folder(__file__)
folders=FileFolderManagement.get_folder_list(current_folder)
file_name_full_path = FileFolderManagement.build_full_path_to_file("c:",file_name,folders)
text = """Hello world !
Hello world !"""
status,error_msg=FileFolderManagement(file_name_full_path).write_to_disk(text)
if(status):
    print("file created successfully: "+file_name_full_path)
else:
    print("error:" + error_msg)

# Write file in root path
file_name="hello.txt"
file_name_full_path = FileFolderManagement.build_full_path_to_file("c:",file_name)
text = """Hello world !
Hello world !"""
status,error_msg=FileFolderManagement(file_name_full_path).write_to_disk(text)
if(status):
    print("file created successfully: "+file_name_full_path)
else:
    print("error:" + error_msg)

FileProperties sample

from jaanca_utils_os import FileProperties, FileFolderManagement
import json

file_name="hello asa s sas. as as a. as as .txt"
filename_full_path_from_current_folder=FileFolderManagement.build_full_path_from_current_folder(__file__,folder_list=["file"])
file_properties=FileProperties(filename_full_path_from_current_folder)
status = file_properties.get_attribute_reading_status()
if status is True:
    print(json.dumps(file_properties.get_dict(),indent=4))
    print(f"name:{file_properties.name}")
    print(f"extension:{file_properties.extension}")
    print(f"modification_date:{file_properties.modification_date}")
else:
    print(status)

Semantic Versioning

jaanca-utils-os < MAJOR >.< MINOR >.< PATCH >

  • MAJOR: version when you make incompatible API changes
  • MINOR: version when you add functionality in a backwards compatible manner
  • PATCH: version when you make backwards compatible bug fixes

Definitions for releasing versions

  • https://peps.python.org/pep-0440/

    • X.YaN (Alpha release): Identify and fix early-stage bugs. Not suitable for production use.
    • X.YbN (Beta release): Stabilize and refine features. Address reported bugs. Prepare for official release.
    • X.YrcN (Release candidate): Final version before official release. Assumes all major features are complete and stable. Recommended for testing in non-critical environments.
    • X.Y (Final release/Stable/Production): Completed, stable version ready for use in production. Full release for public use.

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Types of changes

  • Added for new features.
  • Changed for changes in existing functionality.
  • Deprecated for soon-to-be removed features.
  • Removed for now removed features.
  • Fixed for any bug fixes.
  • Security in case of vulnerabilities.

[0.0.1rcX] - 2024-05-27

Added

  • First tests using pypi.org in develop environment.

[0.1.1-6] - 2024-05-27

Added

  • Completion of testing and launch into production.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jaanca_utils_os-0.1.6.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

jaanca_utils_os-0.1.6-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file jaanca_utils_os-0.1.6.tar.gz.

File metadata

  • Download URL: jaanca_utils_os-0.1.6.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.7

File hashes

Hashes for jaanca_utils_os-0.1.6.tar.gz
Algorithm Hash digest
SHA256 72537e26fab21133adffc11532bf5292381c6923593b08b707d6bbee35aaf15f
MD5 d6a3f51ad7026db97d30d5350c01bf48
BLAKE2b-256 97286cebdeb3289ce0a42f835efbfe2a364442b5f7b9d1d13067369e78bf2d84

See more details on using hashes here.

File details

Details for the file jaanca_utils_os-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for jaanca_utils_os-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4a735add4785858a24253a44dcc74cc0be4ab813e969160302714e13564770a7
MD5 658073ff67a477c20c9be6060fa7ebc1
BLAKE2b-256 0cc5095433d52d7a1b6bd564789cb7855bdeb22e15d2a263e0206e8e2e78853d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page