Re: Adding a new convenience filter 'contains_any'

2010-03-08 Thread Russell Keith-Magee
On Tue, Mar 9, 2010 at 3:24 AM, Phlip  wrote:
>> > And if the PEP8 told you to ... just jump off a cliff... would you?
>>
>> Sounds like you might benefit from actually reading it:
>
> This thread arc is about me lamenting the positive value of style
> guides, even those with wrong line-items.
>
> Nobody here has said to get creative with any style guideline, or
> disobey it.

No - seriously: Have you read PEP8? The opening paragraphs of PEP8
address this exact point.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-08 Thread Phlip
> > And if the PEP8 told you to ... just jump off a cliff... would you?
>
> Sounds like you might benefit from actually reading it:

This thread arc is about me lamenting the positive value of style
guides, even those with wrong line-items.

Nobody here has said to get creative with any style guideline, or
disobey it.

> http://www.python.org/dev/peps/pep-0008/

> "Bureaucrat Conrad, you are technically correct -- the best kind of correct."

(-:

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-08 Thread James Bennett
On Mon, Mar 8, 2010 at 11:11 AM, Phlip  wrote:
> And if the PEP8 told you to ... just jump off a cliff... would you?

Sounds like you might benefit from actually reading it:

http://www.python.org/dev/peps/pep-0008/


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-08 Thread Phlip
> In this case, it's not just a team style guide - it's PEP8, which clearly 
> says:

And if the PEP8 told you to ... just jump off a cliff... would you?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-05 Thread Russell Keith-Magee
On Sat, Mar 6, 2010 at 9:09 AM, Phlip  wrote:
>> >>> (I also disagree with leaving out the spaces around =, but obviously
>> >>> obeying a team style guide supersedes improving it...)
>>
>> >> ... the goal of clarity/legibility
>>
>> > uh...
>>
>> ;)  I meant *having* a style guide was consistent with said goal.
>
> right - following an aesthetic style guide is more important than the
> validity of each of its line items, so the Nuremberg Defense applies
>
> (not so a _technical_ style guide!;)

In this case, it's not just a team style guide - it's PEP8, which clearly says:

 - Don't use spaces around the '=' sign when used to indicate a
  keyword argument or a default parameter value.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-05 Thread Phlip
> >>> (I also disagree with leaving out the spaces around =, but obviously
> >>> obeying a team style guide supersedes improving it...)
>
> >> ... the goal of clarity/legibility
>
> > uh...
>
> ;)  I meant *having* a style guide was consistent with said goal.

right - following an aesthetic style guide is more important than the
validity of each of its line items, so the Nuremberg Defense applies

(not so a _technical_ style guide!;)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-05 Thread Peter Herndon

On Mar 5, 2010, at 1:03 PM, Phlip wrote:

> On Mar 5, 8:41 am, Peter Herndon  wrote:
> 
>> On Mar 4, 2010, at 7:37 PM, Phlip wrote:
> 
>>> (I also disagree with leaving out the spaces around =, but obviously
>>> obeying a team style guide supersedes improving it...)
>> 
>> ... the goal of clarity/legibility
> 
> uh...

;)  I meant *having* a style guide was consistent with said goal.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-05 Thread Phlip
On Mar 5, 8:41 am, Peter Herndon  wrote:

> On Mar 4, 2010, at 7:37 PM, Phlip wrote:

> > (I also disagree with leaving out the spaces around =, but obviously
> > obeying a team style guide supersedes improving it...)
>
> ... the goal of clarity/legibility

uh...

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-05 Thread Peter Herndon

On Mar 4, 2010, at 7:37 PM, Phlip wrote:
> 
> Sorry, I didn't say "operator overload" (which are either spot-on or
> sophomoric, when they work).

I'm using "operator overloading" in this case because "id=" already has a 
specific function, and you would have it do something in addition to that 
function.  I may be using the term incorrectly.

> Because, IIRC, no SQL database can store arrays ('list's) in fields,
> the =[] event is free to be "overloaded" by its type.

Entertainingly, Postgresql does have array types 
(http://www.postgresql.org/docs/8.4/interactive/arrays.html), though that's 
more of an exception than the general case.  Django's ORM doesn't support db 
arrays directly, though there is the CommaSeparatedIntegerField, which 
approximates a primitive version.

> So id=[] would resolve to id__in=[], id=Scalar to id__exact=Scalar,
> and the explicit versions are available if you suspect that
> overloading is fragile.

Interesting thought.  Might be worth doing, to see how practical it is.  You 
should start it as a third-party project, see how much traction it gets.
> 
> (I also disagree with leaving out the spaces around =, but obviously
> obeying a team style guide supersedes improving it...)

Interestingly, that particular style choice is part of the language-level style 
guide, PEP8 (http://www.python.org/dev/peps/pep-0008/), rather than 
Django-specific.  Needless to say, it's a guide, rather than a syntax error, so 
it could be (and occasionally is, in some projects) ignored at the project 
level.  In the case of Django, though, PEP8 is specified.  I personally think 
it's a good thing that there's a language-level style guide, which is entirely 
consistent with both the goal of clarity/legibility, and the philosophy that 
"there should be one --and preferably only one-- obvious way to do it".  It 
means I don't have to think about style, I can instead focus on solving the 
problem.  Which is, really, the entire point of any style guide.

---Peter

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-05 Thread Masklinn
On 5 Mar 2010, at 05:15 , aditya bhargava wrote:
> 
> Not quite, because 'in' does exact matching, and I'm looking for the inexact
> matching that 'contains' provides. Here's one way to simulate a
> 'contains_any' filter:
> 
>t = ["acrylic","watercolors"]   # my list of tags
> 
>q = Q(tags__name__contains=t[0])
>for i in range(1,len(t)):
>q = q | Q(tags__name__contains=t[i])
> 
>o = Image.objects.filter(q) # o is the result of the contains_any filter
> 
> 
> This works fine, but here is the ideal way I'm hoping to write the same
> thing:
> o = Image.objects.filter(tags__name__contains_any=t)
> 
> 
> But I'm not sure how to go about adding that new filter.

Are you using django-tagging? It probably provides convenience methods for that 
kind of "I want the boolean result of the interesection between these two sets" 
filter.

You might also want to check the googles using "intersect" or "intersection" as 
a keyword, to get stuff like 
http://stackoverflow.com/questions/108193/union-and-intersect-in-django

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-04 Thread aditya bhargava
Not quite, because 'in' does exact matching, and I'm looking for the inexact
matching that 'contains' provides. Here's one way to simulate a
'contains_any' filter:

t = ["acrylic","watercolors"]   # my list of tags

q = Q(tags__name__contains=t[0])
for i in range(1,len(t)):
q = q | Q(tags__name__contains=t[i])

o = Image.objects.filter(q) # o is the result of the contains_any filter


This works fine, but here is the ideal way I'm hoping to write the same
thing:
o = Image.objects.filter(tags__name__contains_any=t)


But I'm not sure how to go about adding that new filter.


Aditya

On Thu, Mar 4, 2010 at 3:52 PM, Peter Herndon  wrote:

> On Mar 4, 2010, at 2:56 PM, aditya wrote:
>
> > I would like to add a new filter for models to my django build that
> > can be used as follows:
> >
> > tags = ['tag1','tag2','tag3''tagn']
> > i = Image.objects.filter(tags__contains_any=tags)
> >
> >
> > Essentially, instead of passing a string, I pass a list and get a set
> > of valid objects that match *any* of the items in the list. I'm a loss
> > for where to look for the relevant code, thoughI'm trying to find
> > where the 'contains' filter has been implemented, so that I can take a
> > look at how its written. Currently I'm looking at db.models.query and
> > db.models.query_utils.  Where should I be looking?
> >
> Hi Aditya,
>
> Won't the "in" filter do exactly what you need?
>
> http://docs.djangoproject.com/en/1.1/ref/models/querysets/#in
>
> ---Peter
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-04 Thread Phlip

> >  Entry.objects.filter(id__in=[1, 3, 4])
>
> > ...couldn't id=[] overload if the target is a list?
>
> I suppose it could if it were written that way, but it isn't.  The filter 
> form "filter(=)" is shorthand for the form 
> "filter(__exact=)".  To my knowledge, no one has written an 
> operator overload

Sorry, I didn't say "operator overload" (which are either spot-on or
sophomoric, when they work).

I just meant Entry.objects.filter(id = [1, 3, 4]).

Because, IIRC, no SQL database can store arrays ('list's) in fields,
the =[] event is free to be "overloaded" by its type.

So id=[] would resolve to id__in=[], id=Scalar to id__exact=Scalar,
and the explicit versions are available if you suspect that
overloading is fragile.

(I also disagree with leaving out the spaces around =, but obviously
obeying a team style guide supersedes improving it...)

--
  Phlip

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-04 Thread Peter Herndon

On Mar 4, 2010, at 6:07 PM, Phlip wrote:

> Peter Herndon wrote:
> 
>> Won't the "in" filter do exactly what you need?
>> 
>> http://docs.djangoproject.com/en/1.1/ref/models/querysets/#in
> 
> Why do you need even __in? Given...
> 
>  Entry.objects.filter(id__in=[1, 3, 4])
> 
> ...couldn't id=[] overload if the target is a list?

I suppose it could if it were written that way, but it isn't.  The filter form 
"filter(=)" is shorthand for the form 
"filter(__exact=)".  To my knowledge, no one has written an 
operator overload for a Django filter operation.  I could of course be mistaken 
about that, but in my opinion, such overloading would go against the Python 
philosophy that "explicit is better than implicit."  Since an "in" operator 
already exists, and is a direct analog of the SQL "IN" operator, using that 
seems preferable.  Again, assuming I'm properly understanding the original 
poster's problem.

I believe, from reading your recent back-and-forth with James, that you are 
looking for ways to reduce the amount of code you need to type.  Neither Python 
nor Django are oriented towards code brevity as their primary motivation; 
instead, ensuring that code can be easily read and understood is primary.  
Occasionally, legibility clashes with brevity, as you've pointed out.  I 
personally find this emphasis to be a pleasant release from being forced to 
interpret code that is obfuscated by its brevity.  But that's just my 
preference.  

---Peter

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-04 Thread Phlip
Peter Herndon wrote:

> Won't the "in" filter do exactly what you need?
>
> http://docs.djangoproject.com/en/1.1/ref/models/querysets/#in

Why do you need even __in? Given...

  Entry.objects.filter(id__in=[1, 3, 4])

...couldn't id=[] overload if the target is a list?

--
  Phlip

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Adding a new convenience filter 'contains_any'

2010-03-04 Thread Peter Herndon
On Mar 4, 2010, at 2:56 PM, aditya wrote:

> I would like to add a new filter for models to my django build that
> can be used as follows:
> 
> tags = ['tag1','tag2','tag3''tagn']
> i = Image.objects.filter(tags__contains_any=tags)
> 
> 
> Essentially, instead of passing a string, I pass a list and get a set
> of valid objects that match *any* of the items in the list. I'm a loss
> for where to look for the relevant code, thoughI'm trying to find
> where the 'contains' filter has been implemented, so that I can take a
> look at how its written. Currently I'm looking at db.models.query and
> db.models.query_utils.  Where should I be looking?
> 
Hi Aditya,

Won't the "in" filter do exactly what you need?

http://docs.djangoproject.com/en/1.1/ref/models/querysets/#in

---Peter

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.