dotmagic


Namedotmagic JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/viperadnan-git/dotmagic
SummaryA Python library for accessing environment variables using dot notation.
upload_time2023-08-23 09:20:52
maintainer
docs_urlNone
authorAdnan Ahmad
requires_python>=3.8
licenseGPLv3+
keywords environment variables dotenv env config configuration
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # dotmagic

A Python library consists of a set of utilities for managing configuration files.

## Installation

```console
pip install dotmagic
```

### Usage of `Config` Class

The `Config` class is designed to simplify the management of configuration values from various sources, such as `.env` files or dictionaries. It also provides the option to override environment variables with the loaded configuration.

### Importing `Config`

First, import the `Config` class into your Python script:

```python
from dotmagic.config import Config
```

### Creating a `Config` Instance

To use the `Config` class, create an instance of it. You can specify the configuration source (`.env` file or dictionary), default values, and whether to override environment variables.

```python
# Example 1: Load from a .env file
config = Config(".env")

# Example 2: Load from a dictionary
config_data = {
    "PORT": "8080",
    "DEBUG": "True",
}
config = Config(config_data)

# Example 3: Load from a .env file and override environment variables
config = Config(".env", override=True)
```

### Accessing Configuration Values

You can access configuration values using dot notation or dictionary-style indexing.

```python
# Accessing values using dot notation
port = config.PORT
debug = config.DEBUG

# Accessing values using dictionary-style indexing
port = config["PORT"]
debug = config["DEBUG"]
```

### Handling Missing Values

The `Config` class provides a way to handle missing values. If a value is not found and is marked as required, it will raise a `KeyError`. If it's not required, it will return `None`.

```python
try:
    missing_value = config.required.MISSING_KEY  # Raises KeyError
except KeyError:
    print("MISSING_KEY is required but missing.")

missing_value = config.MISSING_KEY  # None
```

### Examples

Here are some examples of using the `Config` class:

```python
# Example 1: Loading from a .env file
config = Config(".env")
port = config.PORT  # Access the PORT value from the .env file

# Example 2: Loading from a dictionary
config_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 dictionary
config_data = {
    "PORT": "8080",
    "DEBUG": "True",
}
config = ConfigParse(config_data)
port = config.PORT  # Access the PORT value as an integer (parsed from a string)
```

The `Config` class simplifies the management of configuration values, making it easy to work with environment variables and configuration files.

### Usage of `dotdict`

`dotdict` is a custom dictionary class that allows you to access its values using dot notation. Here's how you can use it:

```python
# Import the dotdict class
from dotdict import dotdict

# Create a dotdict from a dictionary
my_dict = {"key1": "value1", "key2": "value2"}
dot_dict = dotdict(my_dict)

# Access values using dot notation
value1 = dot_dict.key1  # "value1"
value2 = dot_dict.key2  # "value2"

# You can also access values using dictionary-style indexing
value1 = dot_dict["key1"]  # "value1"
value2 = dot_dict["key2"]  # "value2"
```

### Usage of `seconds`

`seconds` is a function that converts a time string into seconds. Here's how you can use it:

```python
# Import the seconds function
from dotdict import seconds

# Convert time strings to seconds
seconds_in_a_day = seconds("1d")          # 86400
seconds_in_a_day_and_an_hour = seconds("1d1h")     # 90000
seconds_in_a_day_hour_and_minute = seconds("1d1h1m")   # 90060
seconds_in_a_day_hour_minute_and_second = seconds("1d1h1m1s")  # 90061
```

The `seconds` function 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 a `ValueError`.

## License

This project is licensed under the terms of the [GNU General Public License v3.0.](./LICENSE)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/viperadnan-git/dotmagic",
    "name": "dotmagic",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "environment,variables,dotenv,env,config,configuration",
    "author": "Adnan Ahmad",
    "author_email": "viperadnan@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/b0/f0/75d90050d50fa923290cc97b0664e4f130f925db652d1f0057e4398c0b86/dotmagic-0.1.1.tar.gz",
    "platform": null,
    "description": "# dotmagic\n\nA Python library consists of a set of utilities for managing configuration files.\n\n## Installation\n\n```console\npip install dotmagic\n```\n\n### Usage of `Config` Class\n\nThe `Config` class is designed to simplify the management of configuration values from various sources, such as `.env` files or dictionaries. It also provides the option to override environment variables with the loaded configuration.\n\n### Importing `Config`\n\nFirst, import the `Config` class into your Python script:\n\n```python\nfrom dotmagic.config import Config\n```\n\n### Creating a `Config` Instance\n\nTo use the `Config` class, create an instance of it. You can specify the configuration source (`.env` file or dictionary), default values, and whether to override environment variables.\n\n```python\n# Example 1: Load from a .env file\nconfig = Config(\".env\")\n\n# Example 2: Load from a dictionary\nconfig_data = {\n    \"PORT\": \"8080\",\n    \"DEBUG\": \"True\",\n}\nconfig = Config(config_data)\n\n# Example 3: Load from a .env file and override environment variables\nconfig = Config(\".env\", override=True)\n```\n\n### Accessing Configuration Values\n\nYou can access configuration values using dot notation or dictionary-style indexing.\n\n```python\n# Accessing values using dot notation\nport = config.PORT\ndebug = config.DEBUG\n\n# Accessing values using dictionary-style indexing\nport = config[\"PORT\"]\ndebug = config[\"DEBUG\"]\n```\n\n### Handling Missing Values\n\nThe `Config` class provides a way to handle missing values. If a value is not found and is marked as required, it will raise a `KeyError`. If it's not required, it will return `None`.\n\n```python\ntry:\n    missing_value = config.required.MISSING_KEY  # Raises KeyError\nexcept KeyError:\n    print(\"MISSING_KEY is required but missing.\")\n\nmissing_value = config.MISSING_KEY  # None\n```\n\n### Examples\n\nHere are some examples of using the `Config` class:\n\n```python\n# Example 1: Loading from a .env file\nconfig = Config(\".env\")\nport = config.PORT  # Access the PORT value from the .env file\n\n# Example 2: Loading from a dictionary\nconfig_data = {\n    \"PORT\": \"8080\",\n    \"DEBUG\": \"True\",\n}\nconfig = Config(config_data)\nport = config.PORT  # Access the PORT value as an integer (parsed from a string)\n\n# Example 3: Using ConfigParse for parsing configuration values from a dictionary\nconfig_data = {\n    \"PORT\": \"8080\",\n    \"DEBUG\": \"True\",\n}\nconfig = ConfigParse(config_data)\nport = config.PORT  # Access the PORT value as an integer (parsed from a string)\n```\n\nThe `Config` class simplifies the management of configuration values, making it easy to work with environment variables and configuration files.\n\n### Usage of `dotdict`\n\n`dotdict` is a custom dictionary class that allows you to access its values using dot notation. Here's how you can use it:\n\n```python\n# Import the dotdict class\nfrom dotdict import dotdict\n\n# Create a dotdict from a dictionary\nmy_dict = {\"key1\": \"value1\", \"key2\": \"value2\"}\ndot_dict = dotdict(my_dict)\n\n# Access values using dot notation\nvalue1 = dot_dict.key1  # \"value1\"\nvalue2 = dot_dict.key2  # \"value2\"\n\n# You can also access values using dictionary-style indexing\nvalue1 = dot_dict[\"key1\"]  # \"value1\"\nvalue2 = dot_dict[\"key2\"]  # \"value2\"\n```\n\n### Usage of `seconds`\n\n`seconds` is a function that converts a time string into seconds. Here's how you can use it:\n\n```python\n# Import the seconds function\nfrom dotdict import seconds\n\n# Convert time strings to seconds\nseconds_in_a_day = seconds(\"1d\")          # 86400\nseconds_in_a_day_and_an_hour = seconds(\"1d1h\")     # 90000\nseconds_in_a_day_hour_and_minute = seconds(\"1d1h1m\")   # 90060\nseconds_in_a_day_hour_minute_and_second = seconds(\"1d1h1m1s\")  # 90061\n```\n\nThe `seconds` function 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 a `ValueError`.\n\n## License\n\nThis project is licensed under the terms of the [GNU General Public License v3.0.](./LICENSE)\n",
    "bugtrack_url": null,
    "license": "GPLv3+",
    "summary": "A Python library for accessing environment variables using dot notation.",
    "version": "0.1.1",
    "project_urls": {
        "Download": "https://github.com/viperadnan-git/gdnan/archive/v0.1.1.tar.gz",
        "Homepage": "https://github.com/viperadnan-git/dotmagic"
    },
    "split_keywords": [
        "environment",
        "variables",
        "dotenv",
        "env",
        "config",
        "configuration"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3735b686ccbacb5931fb038eb0ed0717082b25886532f4823b99b87f6c85b746",
                "md5": "bb797dc1968114f96c7d359f3d3a105c",
                "sha256": "25523271d64e13093949d62f6d9f098ed85aa37f7e50eba1ce0170c29ab53672"
            },
            "downloads": -1,
            "filename": "dotmagic-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bb797dc1968114f96c7d359f3d3a105c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 17269,
            "upload_time": "2023-08-23T09:20:51",
            "upload_time_iso_8601": "2023-08-23T09:20:51.378777Z",
            "url": "https://files.pythonhosted.org/packages/37/35/b686ccbacb5931fb038eb0ed0717082b25886532f4823b99b87f6c85b746/dotmagic-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b0f075d90050d50fa923290cc97b0664e4f130f925db652d1f0057e4398c0b86",
                "md5": "c5ccf8c130db5c9e37ca685fc38cc7a8",
                "sha256": "cd28123d559129764b9f418875480d2abec2870e9f6152830b84e2db6a35c6bd"
            },
            "downloads": -1,
            "filename": "dotmagic-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "c5ccf8c130db5c9e37ca685fc38cc7a8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 16751,
            "upload_time": "2023-08-23T09:20:52",
            "upload_time_iso_8601": "2023-08-23T09:20:52.435830Z",
            "url": "https://files.pythonhosted.org/packages/b0/f0/75d90050d50fa923290cc97b0664e4f130f925db652d1f0057e4398c0b86/dotmagic-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-23 09:20:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "viperadnan-git",
    "github_project": "dotmagic",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "dotmagic"
}
        
Elapsed time: 0.10700s