Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:44:07+00:00 2026-06-04T12:44:07+00:00

I’ve got a bad smell in my code. Perhaps I just need to let

  • 0

I’ve got a bad smell in my code. Perhaps I just need to let it air out for a bit, but right now it’s bugging me.

I need to create three different input files to run three Radiative Transfer Modeling (RTM) applications, so that I can compare their outputs. This process will be repeated for thousands of sets of inputs, so I’m automating it with a python script.

I’d like to store the input parameters as a generic python object that I can pass to three other functions, who will each translate that general object into the specific parameters needed to run the RTM software they are responsible. I think this makes sense, but feel free to criticize my approach.

There are many possible input parameters for each piece of RTM software. Many of them over-lap. Most of them are kept at sensible defaults, but should be easily changed.

I started with a simple dict

config = {
    day_of_year: 138,
    time_of_day: 36000, #seconds
    solar_azimuth_angle: 73, #degrees
    solar_zenith_angle: 17, #degrees
    ...
}

There are a lot of parameters, and they can be cleanly categorized into groups, so I thought of using dicts within the dict:

config = {
    day_of_year: 138,
    time_of_day: 36000, #seconds
    solar: {
        azimuth_angle: 73, #degrees
        zenith_angle: 17, #degrees
        ...
    },
    ...
}

I like that. But there are a lot of redundant properties. The solar azimuth and zenith angles, for example, can be found if the other is known, so why hard-code both? So I started looking into python’s builtin property. That lets me do nifty things with the data if I store it as object attributes:

class Configuration(object):
    day_of_year = 138,
    time_of_day = 36000, #seconds
    solar_azimuth_angle = 73, #degrees
    @property
    def solar_zenith_angle(self):
        return 90 - self.solar_azimuth_angle
    ...

config = Configuration()

But now I’ve lost the structure I had from the second dict example.

Note that some of the properties are less trivial than my solar_zenith_angle example, and might require access to other attributes outside of the group of attributes it is a part of. For example I can calculate solar_azimuth_angle if I know the day of year, time of day, latitude, and longitude.

What I’m looking for:

A simple way to store configuration data whose values can all be accessed in a uniform way, are nicely structured, and may exist either as attributes (real values) or properties (calculated from other attributes).

A possibility that is kind of boring:

Store everything in the dict of dicts I outlined earlier, and having other functions run over the object and calculate the calculatable values? This doesn’t sound fun. Or clean. To me it sounds messy and frustrating.

An ugly one that works:

After a long time trying different strategies and mostly getting no where, I came up with one possible solution that seems to work:

My classes: (smells a bit func-y, er, funky. def-initely.)

class SubConfig(object):
    """
    Store logical groupings of object attributes and properties.

    The parent object must be passed to the constructor so that we can still
    access the parent object's other attributes and properties. Useful if we
    want to use them to compute a property in here.
    """
    def __init__(self, parent, *args, **kwargs):
        super(SubConfig, self).__init__(*args, **kwargs)
        self.parent = parent


class Configuration(object):
    """
    Some object which holds many attributes and properties.

    Related configurations settings are grouped in SubConfig objects.
    """
    def __init__(self, *args, **kwargs):
        super(Configuration, self).__init__(*args, **kwargs)
        self.root_config = 2

        class _AConfigGroup(SubConfig):
            sub_config = 3
            @property
            def sub_property(self):
                return self.sub_config * self.parent.root_config
        self.group = _AConfigGroup(self) # Stinky?!

How I can use them: (works as I would like)

config = Configuration()

# Inspect the state of the attributes and properties.
print("\nInitial configuration state:")
print("config.rootconfig: %s" % config.root_config)
print("config.group.sub_config: %s" % config.group.sub_config)
print("config.group.sub_property: %s (calculated)" % config.group.sub_property)

# Inspect whether the properties compute the correct value after we alter
# some attributes.
config.root_config = 4
config.group.sub_config = 5

print("\nState after modifications:")
print("config.rootconfig: %s" % config.root_config)
print("config.group.sub_config: %s" % config.group.sub_config)
print("config.group.sub_property: %s (calculated)" % config.group.sub_property)

The behavior: (output of execution of all of the above code, as expected)

Initial configuration state:
config.rootconfig: 2
config.group.sub_config: 3
config.group.sub_property: 6 (calculated)

State after modifications:
config.rootconfig: 4
config.group.sub_config: 5
config.group.sub_property: 20 (calculated)

Why I don’t like it:

Storing configuration data in class definitions inside of the main object’s __init__() doesn’t feel elegant. Especially having to instantiate them immediately after definition like that. Ugh. I can deal with that for the parent class, sure, but doing it in a constructor…

Storing the same classes outside the main Configuration object doesn’t feel elegant either, since properties in the inner classes may depend on the attributes of Configuration (or their siblings inside it).

I could deal with defining the functions outside of everything, so inside having things like

@property
def solar_zenith_angle(self):
   return calculate_zenith(self.solar_azimuth_angle)

but I can’t figure out how to do something like

@property
def solar.zenith_angle(self):
    return calculate_zenith(self.solar.azimuth_angle)

(when I try to be clever about it I always run into <property object at 0xXXXXX>)

So what is the right way to go about this? Am I missing something basic or taking a very wrong approach? Does anyone know a clever solution?

Help! My python code isn’t beautiful! I must be doing something wrong!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:44:09+00:00Added an answer on June 4, 2026 at 12:44 pm

    Well, here’s an ugly way to at least make sure your properties get called:

    class ConfigGroup(object):
        def __init__(self, config):
            self.config = config
    
        def __getattribute__(self, name):
            v = object.__getattribute__(self, name)
            if hasattr(v, '__get__'):
                return v.__get__(self, ConfigGroup)
            return v
    
    class Config(object):
        def __init__(self):
            self.a = 10
            self.group = ConfigGroup(self)
            self.group.a = property(lambda group: group.config.a*2)
    

    Of course, at this point you might as well forego property entirely and just check if the attribute is callable in __getattribute__.

    Or you could go all out and have fun with metaclasses:

    def config_meta(classname, parents, attrs):
        defaults = {}
        groups = {}
        newattrs = {'defaults':defaults, 'groups':groups}
        for name, value in attrs.items():
            if name.startswith('__'):
                newattrs[name] = value
            elif isinstance(value, type):
                groups[name] = value
            else:
                defaults[name] = value
        def init(self):
            for name, value in defaults.items():
                self.__dict__[name] = value
            for name, value in groups.items():
                group = value()
                group.config = self
                self.__dict__[name] = group
        newattrs['__init__'] = init
        return type(classname, parents, newattrs)
    
    class Config2(object):
        __metaclass__ = config_meta
        a = 10
        b = 2
        class group(object):
            c = 5
            @property
            def d(self):
                return self.c * self.config.a
    

    Use it like this:

    >>> c2.a
    10
    >>> c2.group.d
    50
    >>> c2.a = 6
    >>> c2.group.d
    30
    

    Final edit (?): if you don’t want to have to “backtrack” using self.config in subgroup property definitions, you can use the following instead:

    class group_property(property):
        def __get__(self, obj, objtype=None):
            return super(group_property, self).__get__(obj.config, objtype)
    
        def __set__(self, obj, value):
            super(group_property, self).__set__(obj.config, value)
    
        def __delete__(self, obj):
            return super(group_property, self).__del__(obj.config)
    
    class Config2(object):
        ...
        class group(object):
            ...
            @group_property
            def e(config):
                return config.group.c * config.a
    

    group_property receives the base config object instead of the group object, so paths always start from the root. Therefore, e is equivalent to the previously defined d.

    BTW, supporting nested groups is left as an exercise for the reader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I need to clean up various Word 'smart' characters in user input, including but
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.