1
class DefaultConfig(object):
    class S3(object):
        DATA_ROOT = 's3://%(bucket_name)s/NAS'
        DATA_LOCATION = '{}/%(instrument_id)s/%(run_id)s'.format(DefaultConfig.S3.DATA_ROOT)

The code above gives me the following error.

      File "./s3Utils.py", line 5, in <module>
    from InfraConfig import InfraConfig as IC
  File "/opt/src/datasource/src/main/python/util/InfraConfig.py", line 4, in <module>
    class DefaultConfig(object):
  File "/opt/src/datasource/src/main/python/util/InfraConfig.py", line 6, in DefaultConfig
    class S3(object):
  File "/opt/src/datasource/src/main/python/util/InfraConfig.py", line 14, in S3
    DATA_LOCATION = '{}/%(instrument_id)s/%(run_id)s'.format(DefaultConfig.S3.DATA_ROOT)
NameError: name 'DefaultConfig' is not defined

Why is it unable to find DefaultConfig.S3.DATA_ROOT Also, this is my attempt at writing structured configuration with reuse of values of DefaultConfig. Is there a way to do this without writing a yml file?

2
  • 1
    @ŁukaszRogalski: This actually is an MCVE. Commented Sep 15, 2016 at 19:04
  • @user2357112 yep, I misread and misinterpreted the question Commented Sep 15, 2016 at 19:32

2 Answers 2

2

It is unable to find the DefaultConfing because DefaultConfig is not defined at the moment S3 is created.

Remember that class are objects. Because there are objects, that means they need to be instantiate. Python instantiate a class at the end of its definition, and therefore register it in the globals. Because the class definition is not finished, you can't use the DefaultConfig name.

Sign up to request clarification or add additional context in comments.

Comments

1

You should use it without any prefixes:

class DefaultConfig(object):
  class S3(object):
    DATA_ROOT = 's3://%(bucket_name)s/NAS'
    DATA_LOCATION = '{}/%(instrument_id)s/%(run_id)s'.format(DATA_ROOT)

 print DefaultConfig.S3.DATA_LOCATION

returns:

> s3://%(bucket_name)s/NAS/%(instrument_id)s/%(run_id)s

Comments

Your Answer

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.