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 8896693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:08:26+00:00 2026-06-15T00:08:26+00:00

Sorry, badly worded title. I hope a simple example will make it clear. Here’s

  • 0

Sorry, badly worded title. I hope a simple example will make it clear. Here’s the easiest way to do what I want to do:

class Lemon(object):

    headers = ['ripeness', 'colour', 'juiciness', 'seeds?']

    def to_row(self):
        return [self.ripeness, self.colour, self.juiciness, self.seeds > 0]

def save_lemons(lemonset):
    f = open('lemons.csv', 'w')
    out = csv.writer(f)
    out.write(Lemon.headers)
    for lemon in lemonset:
        out.writerow(lemon.to_row())

This works alright for this small example, but I feel like I’m “repeating myself” in the Lemon class. And in the actual code I’m trying to write (where the number of variables I’m exporting is ~50 rather than 4, and where to_row calls a number of private methods that do a bunch of weird calculations), it becomes awkward.

As I write the code to generate a row, I need to constantly refer to the “headers” variable to make sure I’m building my list in the correct order. If I want to change the variables being outputted, I need to make sure to_row and headers are being changed in parallel (exactly the kind of thing that DRY is meant to prevent, right?).

Is there a better way I could design this code? I’ve been playing with function decorators, but nothing has stuck. Ideally I should still be able to get at the headers without having a particular lemon instance (i.e. it should be a class variable or class method), and I don’t want to have a separate method for each variable.

  • 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-15T00:08:27+00:00Added an answer on June 15, 2026 at 12:08 am

    We could use some metaclass shenanegans to do this…

    In python 2, attributes are passed to the metaclass in a dict, without
    preserving order, we’ll also want a base class to work with so we can
    distinguish class attributes that should be mapped into the row. In python3, we could dispense with just about all of this base descriptor class.

    import itertools
    import functools
    @functools.total_ordering
    class DryDescriptor(object):
        _order_gen = itertools.count()
        def __init__(self, alias=None):
            self.alias = alias
            self.order = next(self._order_gen)
    
        def __lt__(self, other):
            return self.order < other.order
    

    We will want a python descriptor for every attribute we wish to map into the
    row. slots are a nice way to get data descriptors without much work. One
    caveat, though, we’ll have to manually remove the helper instance to make the
    real slot descriptor visible.

    class slot(DryDescriptor):
        def annotate(self, attr, attrs):
            del attrs[attr]
            self.attr = attr
            slots = attrs.setdefault('__slots__', []).append(attr)
    
        def annotate_class(self, cls):
            if self.alias is not None:
                setattr(cls, self.alias, getattr(self.attr))
    

    For computed fields, we can memoize results. Memoizing off of the annotated
    instance is tricky without a memory leak, we need weakref. alternatively, we
    could have arranged for another slot just to store the cached value. This also isn’t quite thread safe, but pretty close.

    import weakref
    class memo(DryDescriptor):
        _memo = None
        def __call__(self, method):
            self.getter = method
            return self
    
        def annotate(self, attr, attrs):
            if self.alias is not None:
                attrs[self.alias] = self
    
        def annotate_class(self, cls): pass
    
        def __get__(self, instance, owner):
            if instance is None:
                return self
            if self._memo is None:
                self._memo = weakref.WeakKeyDictionary()
            try:
                return self._memo[instance]
            except KeyError:
                return self._memo.setdefault(instance, self.getter(instance))
    

    On the metaclass, all of the descriptors we created above are found, sorted by
    creation order, and instructed to annotate the new, created class. This does
    not correctly treat derived classes and could use some other conveniences like
    an __init__ for all the slots.

    class DryMeta(type):
        def __new__(mcls, name, bases, attrs):
            descriptors = sorted((value, key) 
                                 for key, value 
                                 in attrs.iteritems() 
                                 if isinstance(value, DryDescriptor))
    
            for descriptor, attr in descriptors:
                descriptor.annotate(attr, attrs)
    
            cls = type.__new__(mcls, name, bases, attrs)
            for descriptor, attr in descriptors:
                descriptor.annotate_class(cls)
    
            cls._header_descriptors = [getattr(cls, attr) for descriptor, attr in descriptors]
            return cls
    

    Finally, we want a base class to inherit from so that we can have a to_row
    method. this just invokes all of the __get__s for all of the respective
    descriptors, in order.

    class DryBase(object):
        __metaclass__ = DryMeta
    
        def to_row(self):
            cls = type(self)
            return [desc.__get__(self, cls) for desc in cls._header_descriptors]
    

    Assuming all of that is tucked away, out of sight, the definition of a class
    that uses this feature is mostly free of repitition. The only short coming is
    that to be practical, every field needs a python friendly name, thus we had the
    alias key to associate 'seeds?' to has_seeds

    class ADryRow(DryBase):
        __slots__ = ['seeds']
    
        ripeness = slot()
        colour = slot()
        juiciness = slot()
    
        @memo(alias='seeds?')
        def has_seeds(self):
            print "Expensive!!!"
            return self.seeds > 0
    
    >>> my_row = ADryRow()
    >>> my_row.ripeness = "tart"
    >>> my_row.colour = "#8C2"
    >>> my_row.juiciness = 0.3479
    >>> my_row.seeds = 19
    >>>
    >>> print my_row.to_row()
    Expensive!!!
    ['tart', '#8C2', 0.3479, True]
    >>> print my_row.to_row()
    ['tart', '#8C2', 0.3479, True]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's a badly worded title but I can't come up with anything better, sorry!
Sorry for the badly worded title, I currently have this if statement: - if
Sorry about the cranky title, here's the example: <table> <tr> <th class=hey-1-aa></th> </tr> <tr>
Sorry for the badly-worded title. I've been looking through the documentation, but I cannot
Sorry for this simple question, but I can't solve it... There is an example:
sorry for the dummy question but I cannot find a simple and clean way
Sorry I'm new here and my code will probably not be properly displayed... how
Sorry if the title is not clear enough, I have <a> elements with the
Sorry for the badly phrased title, I don't know what else to call this
Sorry, for the badly phrased title. I don't know much object-oriented PHP, so, I

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.