Created
August 18, 2020 11:22
-
-
Save thoroc/1213e33e1be31ac453e948aacb5c6a16 to your computer and use it in GitHub Desktop.
an overly complicated example to illustrate: https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import os | |
from faker import Faker | |
from faker.providers import internet, lorem | |
from tabulate import tabulate | |
fake = Faker() | |
fake.add_provider(internet) | |
fake.add_provider(lorem) | |
def set_os_env(key: str, value: str): | |
os.environ[key] = value | |
def db_config() -> dict: | |
return { | |
'database': os.getenv('DB_SCHEMA'), | |
'user': os.getenv('DB_USERNAME'), | |
'password': os.getenv('DB_PASSWORD'), | |
'host': os.getenv('DB_HOSTNAME'), | |
'port': os.getenv('DB_PORT') | |
} | |
class Connection: | |
def __init__(self, database: str, user: str, password: str, host: str, port: str) -> None: | |
self.db_schema = database | |
self.db_username = user | |
self.db_password = password | |
self.db_hostname = host | |
self.db_port = port | |
def __str__(self) -> str: | |
header = ['key', 'value'] | |
data = [ | |
['DB_SCHEMA', self.db_schema], | |
['DB_USERNAME', self.db_username], | |
['DB_PASSWORD', self.db_password], | |
['DB_HOSTNAME', self.db_hostname], | |
['DB_PORT', self.db_port], | |
] | |
return tabulate(tabular_data=data, headers=header) | |
set_os_env('DB_SCHEMA', f'{fake.word()}-db') | |
set_os_env('DB_USERNAME', fake.user_name()) | |
set_os_env('DB_PASSWORD', fake.pystr()) | |
set_os_env('DB_HOSTNAME', f'http://{fake.hostname()}') | |
set_os_env('DB_PORT', str(fake.port_number())) | |
conf = db_config() | |
print(conf) | |
conn = Connection(**conf) | |
print(conn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{'database': 'begin-db', 'user': 'clawson', 'password': 'qQnkYSZOixookbgLoBax', 'host': 'http://laptop-29.morris.com', 'port': '59863'} | |
key value | |
----------- --------------------------- | |
DB_SCHEMA begin-db | |
DB_USERNAME clawson | |
DB_PASSWORD qQnkYSZOixookbgLoBax | |
DB_HOSTNAME http://laptop-29.morris.com | |
DB_PORT 59863 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment