3

I'm trying to pass a name attribute as a parameter in a function, to make it more generic.

For example, I'd like to modify this function:

@classmethod
def count_events_per_day(cls, myqueryset):
    return myqueryset.filter(
        created__range=(a,b))

to make something like:

@classmethod
def count_events_per_day(cls, myqueryset, attr_name): # attr_name would be passed as a string
    return myqueryset.filter(
        attr_name__range=(a,b))

Is there a way to do this? I searched on SO but I suppose the keywords I use are not relevants since I can't find any answer.

Thanks!

David Dahan
  • 10,576
  • 11
  • 64
  • 137
  • I'm afraid that I don't understand. So you want your attribute name to be automatically modified? – Games Brainiac Jun 02 '14 at 13:46
  • @GamesBrainiac That's it. As I need to use this method with different attributes (not `created` only), I'd like to avoid to write N times this method for the N attributes I have. – David Dahan Jun 02 '14 at 13:49
  • What type is attr_name? Is it just a string? – Games Brainiac Jun 02 '14 at 13:49
  • @GamesBrainiac Nope. It's an attribute of an object. My class is `Event` and one of its attributes is `created`. – David Dahan Jun 02 '14 at 13:55
  • @DavidW. Could you please show us how you call `count_events_per_day`, and what are the values of arguments? And why couldn't you pass `'created'` as `attr_name`? – Bunyk Jun 02 '14 at 13:59
  • @Bunyk That was the reason I did not answer the question. Its a little ambiguous. – Games Brainiac Jun 02 '14 at 14:01
  • @GamesBrainiac Names are usually the strings. Because what else they could be? Integers or lambdas? That would be strange. :) – Bunyk Jun 02 '14 at 14:03
  • @Bunyk Yes, but as you can see, that it is of type event. – Games Brainiac Jun 02 '14 at 14:04
  • Sorry about this, attr_name in the parameter of the function I'd like to write IS a string.. I just wanted to say that in a Django context, the attribute is not a string, it's an attribute ('keyword'). – David Dahan Jun 02 '14 at 14:12

1 Answers1

2

It's easy, because you could pass arguments to function from dictionary:

@classmethod
def count_events_per_day(cls, myqueryset, attr_name):
    return myqueryset.filter(**{
        attr_name + '__range': (a, b)
    })

UPD: Adding link to answer with explanation of double-star syntax: https://stackoverflow.com/a/2921893/816449

Community
  • 1
  • 1
Bunyk
  • 7,635
  • 8
  • 47
  • 79
  • This creates a `SyntaxError: keyword can't be an expression` :( – David Dahan Jun 02 '14 at 13:35
  • @DavidW. What do you pass as attr_name? `'created'` ? – Bunyk Jun 02 '14 at 13:44
  • Because, usually such error means that you used in name of argument some characters which are not allowed in identifiers, like `'.'`, or `'+'`... – Bunyk Jun 02 '14 at 13:54
  • 1
    `attr_name` is not a string, I'm afraid. – Games Brainiac Jun 02 '14 at 13:56
  • 1
    Ahh yes, of course I'm passing a string. I just wanted to say that attr is not a string in a Django context. Sorry. Btw, it seems to work, but I don't understand how ^^ I continue to test something and I go back! Thanks :) – David Dahan Jun 02 '14 at 14:10
  • This works well, thanks a lot for this. May I ask you to add some explanations about the syntax you're using to help beginners like me to understand concepts instead of just copy-past the answer? :) – David Dahan Jun 02 '14 at 14:23
  • 1
    @DavidW. Sure. This is already explained by others, I just added link. – Bunyk Jun 02 '14 at 14:31