1
\$\begingroup\$

I have a simple SDK in Python for internal use. It's basically 150~200 lines of code of simple API calls wrapped around nicely that is comfortable for us to use.

It holds a few clients with methods (and each method wraps some API calls).

So a proper usage would be:

from datatube.datatube import Datatube
client = Datatube()
client.do_stuff(...)

Currently it expects 3 environment variables - two for authentication purposes (these are the secret ones) and one for the environment (dev/stg/prod). I want to change the environment variable to be passed on as an argument (seems better). So:

from datatube.datatube import Datatube
client = Datatube('stg')
client.do_stuff(...)

Inside Datatube I'll take the correct config based on the environment. Currently I have:

class BaseConfig:
 pass
class DevelopmentConfig(BaseConfig):
 DATATUBE_URL = 'https://..'
class StagingConfig(BaseConfig):
 DATATUBE_URL = 'https://..'
class ProductionConfig(BaseConfig):
 DATATUBE_URL = 'https://..'
def get_config(env: str):
 env = env.lower()
 if env == 'dev':
 return DevelopmentConfig()
 if env == 'stg':
 return StagingConfig()
 elif env == 'prod':
 return ProductionConfig()
 else:
 raise KeyError(f"unsupported env: {env}. supported environments: dev/stg/prod")

If I'll use type annotation on get_config like:

def get_config(env: str) -> BaseConfig

Then the IDE won't like it (I guess because BaseConfig is empty, but I don't know how to do it nicely - isn't an abstract class kind of an overkill?)

Any recommendations would be helpful.

Using Python 3.9

asked Jul 12, 2021 at 14:44
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Class inheritance is overkill here. Just use dataclass instances:

from dataclasses import dataclass
@dataclass
class Config:
 DATATUBE_URL: str
ENVIRONMENTS = {
 'dev': Config(
 DATATUBE_URL='https://..',
 ),
 'stg': Config(
 DATATUBE_URL='https://..',
 ),
 'prd': Config(
 DATATUBE_URL='https://..',
 ),
}
def get_config(env_name: str) -> Config:
 env = ENVIRONMENTS.get(env_name.lower())
 if env is None:
 raise KeyError(
 f"unsupported env: {env_name}. supported environments: " +
 ', '.join(ENVIRONMENTS.keys()))
 return env
answered Jul 12, 2021 at 18:27
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.