Skip to main content

This is a simple environment variable solution for .json file, which is based on 'os' and 'json' lib

Project description

from json_env.json_env import show_all

Introduce

  • This pak opened base on MIT Licence
  • This is a simple environment variable solution for .json file
  • It's based on 'os' and 'json' libs
  • Use this lib you don't need to worry type issue of 'os.environ' it's all handled by this pak

What's new 1.1.3

  • You can set environment variables now!
set_env(key: str, val: JsonVar | Any)
  • You can show all environment variables now!
show_all()

Install

pip install json-env-sln

Example

test case

{
  "str": "Asashishi",
  "bool_str": "true",
  "int_str": "107",
  "float_str": "1.07",
  "none_str": "null",
  "none": null,
  "bool": false,
  "int": 107,
  "float": 1.07,
  "array": [
    "Asashishi",
    "true",
    "107",
    "1.07",
    "null",
    null,
    false,
    107,
    1.07
  ],
  "object": {
    "str": "Asashishi",
    "bool_str": "true",
    "int_str": "107",
    "float_str": "1.07",
    "none_str": "null",
    "none": null,
    "bool": false,
    "int": 107,
    "float": 1.07,
    "array": [
      "Asashishi",
      "true",
      "107",
      "1.07",
      "null",
      null,
      false,
      107,
      1.07
    ]
  }
}

Use

  • Test case
import os
import time
from json_env import set_env, get_env, load_env, show_all

start: float = time.time()

# load env from json file
load_env(os.path.join(os.getcwd(),"env.json"))

# get
print(get_env("str"), type(get_env("str")))
print(get_env("bool_str"), type(get_env("bool_str")))
print(get_env("int_str"), type(get_env("int_str")))
print(get_env("float_str"), type(get_env("float_str")))
print(get_env("none_str"), type(get_env("none_str")))

print(get_env("none"), type(get_env("none")))
print(get_env("bool"), type(get_env("bool")))
print(get_env("int"), type(get_env("int")))
print(get_env("float"), type(get_env("float")))

print(get_env("array"), type(get_env("array")))
array: list = get_env("array")
for item in array:
    print(type(item))

print(get_env("object"), type(get_env("object")))
json_object: dict = get_env("object")
for key, val in json_object.items():
    print(type(val))

print("="*120)
print("="*120)

# set then get
set_env("set_str", get_env("str"))
print(get_env("set_str"), type(get_env("set_str")))
set_env("set_bool_str", "true")
print(get_env("set_bool_str"), type(get_env("set_bool_str")))
set_env("set_int_str","107")
print(get_env("set_int_str"), type(get_env("set_int_str")))
set_env("set_float_str","1.07")
print(get_env("set_float_str"), type(get_env("set_float_str")))
set_env("set_none_str","null")
print(get_env("set_none_str"), type(get_env("set_none_str")))

set_env("set_none",None)
print(get_env("set_none"), type(get_env("set_none")))
set_env("set_bool",True)
print(get_env("set_bool"), type(get_env("set_bool")))
set_env("set_int",107)
print(get_env("set_int"), type(get_env("set_int")))
set_env("set_float",1.07)
print(get_env("set_float"), type(get_env("set_float")))

set_env("set_array", array)
print(get_env("set_array"), type(get_env("set_array")))
set_array: list = get_env("set_array")
for item in set_array:
    print(type(item))

set_env("set_object", json_object)
print(get_env("set_object"), type(get_env("set_object")))
set_json_object: dict = get_env("set_object")
for key, val in set_json_object.items():
    print(type(val))

print("="*240)
print("="*240)

# show all
show_all()

print(f"Test total time cost: {time.time() - start}s")

The results are

Asashishi <class 'str'>
true <class 'str'>
107 <class 'str'>
1.07 <class 'str'>
null <class 'str'>
None <class 'NoneType'>
False <class 'bool'>
107 <class 'int'>
1.07 <class 'float'>
['Asashishi', 'true', '107', '1.07', 'null', None, False, 107, 1.07] <class 'list'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'NoneType'>
<class 'bool'>
<class 'int'>
<class 'float'>
{'str': 'Asashishi', 'bool_str': 'true', 'int_str': '107', 'float_str': '1.07', 'none_str': 'null', 'none': None, 'bool': False, 'int': 107, 'float': 1.07, 'array': ['Asashishi', 'true', '107', '1.07', 'null', None, False, 107, 1.07]} <class 'dict'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'NoneType'>
<class 'bool'>
<class 'int'>
<class 'float'>
<class 'list'>
================================================================================================================================================================================================================================================
================================================================================================================================================================================================================================================
Asashishi <class 'str'>
true <class 'str'>
107 <class 'str'>
1.07 <class 'str'>
null <class 'str'>
None <class 'NoneType'>
True <class 'bool'>
107 <class 'int'>
1.07 <class 'float'>
['Asashishi', 'true', '107', '1.07', 'null', None, False, 107, 1.07] <class 'list'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'NoneType'>
<class 'bool'>
<class 'int'>
<class 'float'>
{'str': 'Asashishi', 'bool_str': 'true', 'int_str': '107', 'float_str': '1.07', 'none_str': 'null', 'none': None, 'bool': False, 'int': 107, 'float': 1.07, 'array': ['Asashishi', 'true', '107', '1.07', 'null', None, False, 107, 1.07]} <class 'dict'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'NoneType'>
<class 'bool'>
<class 'int'>
<class 'float'>
<class 'list'>
================================================================================================================================================================================================================================================
================================================================================================================================================================================================================================================
key: str, val: Asashishi, type: <class 'str'>
key: bool_str, val: true, type: <class 'str'>
key: int_str, val: 107, type: <class 'str'>
key: float_str, val: 1.07, type: <class 'str'>
key: none_str, val: null, type: <class 'str'>
key: none, val: None, type: <class 'NoneType'>
key: bool, val: False, type: <class 'bool'>
key: int, val: 107, type: <class 'int'>
key: float, val: 1.07, type: <class 'float'>
key: array, val: ['Asashishi', 'true', '107', '1.07', 'null', None, False, 107, 1.07], type: <class 'list'>
key: object, val: {'str': 'Asashishi', 'bool_str': 'true', 'int_str': '107', 'float_str': '1.07', 'none_str': 'null', 'none': None, 'bool': False, 'int': 107, 'float': 1.07, 'array': ['Asashishi', 'true', '107', '1.07', 'null', None, False, 107, 1.07]}, type: <class 'dict'>
key: set_str, val: Asashishi, type: <class 'str'>
key: set_bool_str, val: true, type: <class 'str'>
key: set_int_str, val: 107, type: <class 'str'>
key: set_float_str, val: 1.07, type: <class 'str'>
key: set_none_str, val: null, type: <class 'str'>
key: set_none, val: None, type: <class 'NoneType'>
key: set_bool, val: True, type: <class 'bool'>
key: set_int, val: 107, type: <class 'int'>
key: set_float, val: 1.07, type: <class 'float'>
key: set_array, val: ['Asashishi', 'true', '107', '1.07', 'null', None, False, 107, 1.07], type: <class 'list'>
key: set_object, val: {'str': 'Asashishi', 'bool_str': 'true', 'int_str': '107', 'float_str': '1.07', 'none_str': 'null', 'none': None, 'bool': False, 'int': 107, 'float': 1.07, 'array': ['Asashishi', 'true', '107', '1.07', 'null', None, False, 107, 1.07]}, type: <class 'dict'>

Test total time cost: 0.0017666816711425781s

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

json_env_sln-1.1.3.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_env_sln-1.1.3-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file json_env_sln-1.1.3.tar.gz.

File metadata

  • Download URL: json_env_sln-1.1.3.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for json_env_sln-1.1.3.tar.gz
Algorithm Hash digest
SHA256 d255dad54a72497ab49e1cc60e4dba793dd4313086d7fce872f3ce3cfbf0acdf
MD5 168585b2c130e5360aa58d8f705e3c7e
BLAKE2b-256 c30d4ec99406356f5f207b7dde045b1a1ff948b6e56a71711db00917bb980a7d

See more details on using hashes here.

File details

Details for the file json_env_sln-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: json_env_sln-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for json_env_sln-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 906d4076f55db364614a02442da095db601809121af82b3bafcaeb7aa7079d06
MD5 a0dc2b0815867e09b38a0850799dd8bf
BLAKE2b-256 462a4f4370e589f7a6995cdcf3c949ded835a003e1878f7be8070e36bf795e0f

See more details on using hashes here.

Supported by

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