Skip to content

Instantly share code, notes, and snippets.

@rnrbarbosa
Last active May 26, 2019 22:09
Show Gist options
  • Save rnrbarbosa/7fd5d84f64e3d344100f9d6bd83c111b to your computer and use it in GitHub Desktop.
Save rnrbarbosa/7fd5d84f64e3d344100f9d6bd83c111b to your computer and use it in GitHub Desktop.
VYPER EXAMPLE
from vyper import v
import json, yaml
import os,string
v.set_env_prefix('ADANACTL')
v.automatic_env()
#---------------------------------------------
# JSON CONFIG EXAMPLE
#---------------------------------------------
print("---\nJSON Example\n---")
# Set some ENV vars for testing
os.environ['ADANACTL_HOST.ADDRESS'] = "10.0.0.1"
v.set_config_type('json')
json_example = '''
{
"host": {
"address": "localhost",
"port": 5799
},
"datastore": {
"metric": {
"host": "127.0.0.1",
"port": 3099
},
"warehouse": {
"host": "198.0.0.1",
"port": 2112
}
}
}
'''
v.read_config(json_example)
print("Host Address: " + v.get('host.address'))
print("Host Port: " + str(v.get('host.port')))
#---------------------------------------------
# YAML CONFIG EXAMPLE
#---------------------------------------------
print("---\nYAML Example\n---")
# Overriding the Config with ENV variable
os.environ['ADANACTL_CLOTHING.TROUSER'] = "Chinos"
v.set_config_type('yaml') # or v.set_config_type('YAML')
yaml_example = '''
Hacker: true
name: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:
jacket: leather
trousers: denim
age: 35
eyes : brown
beard: true
'''
v.read_config(yaml_example)
print("Clothing Trouser: " + v.get('clothing.trouser'))
print("Clothing Jacket" + v.get('clothing.jacket'))
from vyper import v
import json, yaml, os
v.set_env_prefix('ADANACTL')
v.automatic_env()
# Set some ENV vars for testing
os.environ['ADANACTL_HACKER'] = "False"
os.environ['ADANACTL_NAME'] = "Roberto"
# FROM JSON config file
v.set_config_name('cfg1')
v.set_config_type('json')
v.add_config_path('.')
cfg1 = v.read_in_config()
print(cfg1)
# FROM YAML config file
v.set_config_type('yaml')
v.set_config_name('cfg2')
v.add_config_path('.')
cfg2 = v.read_in_config()
print(cfg2)
print('--')
print(v.get('Hacker'))
print(v.get('NAME'))
{
"host": {
"address": "localhost",
"port": 5799
},
"datastore": {
"metric": {
"host": "127.0.0.1",
"port": 3099
},
"warehouse": {
"host": "198.0.0.1",
"port": 2112
}
}
}
---
Hacker: true
NAME: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:
jacket: leather
trousers: denim
age: 35
eyes : brown
beard: true
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment