Package: dotmagic
dotmagicA Python library consists of a set of utilities for managing configuration files.Installationpip install dotmagicUsage ofConfigClassTheConfigclass is designed to simplify the management of configuration values from various sources, such as.envfiles or dictionaries. It also provides the option to override environment variables with the loaded configuration.ImportingConfigFirst, import theConfigclass into your Python script:fromdotmagic.configimportConfigCreating aConfigInstanceTo use theConfigclass, create an instance of it. You can specify the configuration source (.envfile or dictionary), default values, and whether to override environment variables.# Example 1: Load from a .env fileconfig=Config(".env")# Example 2: Load from a dictionaryconfig_data={"PORT":"8080","DEBUG":"True",}config=Config(config_data)# Example 3: Load from a .env file and override environment variablesconfig=Config(".env",override=True)Accessing Configuration ValuesYou can access configuration values using dot notation or dictionary-style indexing.# Accessing values using dot notationport=config.PORTdebug=config.DEBUG# Accessing values using dictionary-style indexingport=config["PORT"]debug=config["DEBUG"]Handling Missing ValuesTheConfigclass provides a way to handle missing values. If a value is not found and is marked as required, it will raise aKeyError. If it's not required, it will returnNone.try:missing_value=config.required.MISSING_KEY# Raises KeyErrorexceptKeyError:print("MISSING_KEY is required but missing.")missing_value=config.MISSING_KEY# NoneExamplesHere are some examples of using theConfigclass:# Example 1: Loading from a .env fileconfig=Config(".env")port=config.PORT# Access the PORT value from the .env file# Example 2: Loading from a dictionaryconfig_data={"PORT":"8080","DEBUG":"True",}config=Config(config_data)port=config.PORT# Access the PORT value as an integer (parsed from a string)# Example 3: Using ConfigParse for parsing configuration values from a dictionaryconfig_data={"PORT":"8080","DEBUG":"True",}config=ConfigParse(config_data)port=config.PORT# Access the PORT value as an integer (parsed from a string)TheConfigclass simplifies the management of configuration values, making it easy to work with environment variables and configuration files.Usage ofdotdictdotdictis a custom dictionary class that allows you to access its values using dot notation. Here's how you can use it:# Import the dotdict classfromdotdictimportdotdict# Create a dotdict from a dictionarymy_dict={"key1":"value1","key2":"value2"}dot_dict=dotdict(my_dict)# Access values using dot notationvalue1=dot_dict.key1# "value1"value2=dot_dict.key2# "value2"# You can also access values using dictionary-style indexingvalue1=dot_dict["key1"]# "value1"value2=dot_dict["key2"]# "value2"Usage ofsecondssecondsis a function that converts a time string into seconds. Here's how you can use it:# Import the seconds functionfromdotdictimportseconds# Convert time strings to secondsseconds_in_a_day=seconds("1d")# 86400seconds_in_a_day_and_an_hour=seconds("1d1h")# 90000seconds_in_a_day_hour_and_minute=seconds("1d1h1m")# 90060seconds_in_a_day_hour_minute_and_second=seconds("1d1h1m1s")# 90061Thesecondsfunction takes a time string as input and returns the equivalent time duration in seconds. It supports various formats, such as "1d" (1 day), "1d1h" (1 day and 1 hour), "1d1h1m" (1 day, 1 hour, and 1 minute), and "1d1h1m1s" (1 day, 1 hour, 1 minute, and 1 second). If the input time string is invalid, it raises aValueError.LicenseThis project is licensed under the terms of theGNU General Public License v3.0.