Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > QuerySets in Dictionaries

Reply
Thread Tools

QuerySets in Dictionaries

 
 
scoopseven
Guest
Posts: n/a
 
      11-12-2009
I need to create a dictionary of querysets. I have code that looks
like:

query1 = Myobject.objects.filter(status=1)
query2 = Myobject.objects.filter(status=2)
query3 = Myobject.objects.filter(status=3)

d={}
d['a'] = query1
d['b'] = query2
d['c'] = query3

Is there a way to do this that I'm missing?

Thanks.
 
Reply With Quote
 
 
 
 
Simon Brunning
Guest
Posts: n/a
 
      11-12-2009
2009/11/12 scoopseven <(E-Mail Removed)>:
> I need to create a dictionary of querysets. ¬*I have code that looks
> like:
>
> query1 = Myobject.objects.filter(status=1)
> query2 = Myobject.objects.filter(status=2)
> query3 = Myobject.objects.filter(status=3)
>
> d={}
> d['a'] = query1
> d['b'] = query2
> d['c'] = query3
>
> Is there a way to do this that I'm missing?


Untested:

wanted = (('a', 1), ('b', 2), ('c', 2))
d = dict((key, Myobject.objects.filter(status=number)) for (key,
number) in wanted)


--
Cheers,
Simon B.
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-12-2009
scoopseven schrieb:
> I need to create a dictionary of querysets. I have code that looks
> like:
>
> query1 = Myobject.objects.filter(status=1)
> query2 = Myobject.objects.filter(status=2)
> query3 = Myobject.objects.filter(status=3)
>
> d={}
> d['a'] = query1
> d['b'] = query2
> d['c'] = query3
>
> Is there a way to do this that I'm missing?



d = dict(a=Myobject.objects.filter(status=1),
b=Myobject.objects.filter(status=2),
c=Myobject.objects.filter(status=3))

Diez
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-13-2009
On Thu, 12 Nov 2009 10:39:33 -0800, scoopseven wrote:

> I need to create a dictionary of querysets. I have code that looks
> like:
>
> query1 = Myobject.objects.filter(status=1)
> query2 = Myobject.objects.filter(status=2)
> query3 = Myobject.objects.filter(status=3)
>
> d={}
> d['a'] = query1
> d['b'] = query2
> d['c'] = query3
>
> Is there a way to do this that I'm missing?


I don't understand your problem. Assuming Myobject is defined, and has
the appropriate attribute objects.filter, the above should work exactly
as you give it.

What is your actual problem? Are you asking for help in defining a
queryset?



--
Steven
 
Reply With Quote
 
scoopseven
Guest
Posts: n/a
 
      11-13-2009
On Nov 12, 8:55*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
> On Thu, 12 Nov 2009 10:39:33 -0800, scoopseven wrote:
> > I need to create a dictionary of querysets. *I have code that looks
> > like:

>
> > query1 = Myobject.objects.filter(status=1)
> > query2 = Myobject.objects.filter(status=2)
> > query3 = Myobject.objects.filter(status=3)

>
> > d={}
> > d['a'] = query1
> > d['b'] = query2
> > d['c'] = query3

>
> > Is there a way to do this that I'm missing?

>
> I don't understand your problem. Assuming Myobject is defined, and has
> the appropriate attribute objects.filter, the above should work exactly
> as you give it.
>
> What is your actual problem? Are you asking for help in defining a
> queryset?
>
> --
> Steven


I actually had a queryset that was dynamically generated, so I ended
up having to use the eval function, like this...

d = {}
for thing in things:
query_name = 'thing_' + str(thing.id)
query_string = 'Thing.objects.filter(type=' + str(thing.id) +
').order_by(\'-date\')[:3]'
executable_string = query_name + ' = Thing.objects.filter
(type=' + str(thing.id) + ').order_by(\'-date\')[:3]'
exec(executable_string)
d[query_name] = eval(query_string)

And no, this is not based on user-generated input.

Thanks for the input.
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-14-2009
scoopseven schrieb:
> On Nov 12, 8:55 pm, Steven D'Aprano <st...@REMOVE-THIS-
> cybersource.com.au> wrote:
>> On Thu, 12 Nov 2009 10:39:33 -0800, scoopseven wrote:
>>> I need to create a dictionary of querysets. I have code that looks
>>> like:
>>> query1 = Myobject.objects.filter(status=1)
>>> query2 = Myobject.objects.filter(status=2)
>>> query3 = Myobject.objects.filter(status=3)
>>> d={}
>>> d['a'] = query1
>>> d['b'] = query2
>>> d['c'] = query3
>>> Is there a way to do this that I'm missing?

>> I don't understand your problem. Assuming Myobject is defined, and has
>> the appropriate attribute objects.filter, the above should work exactly
>> as you give it.
>>
>> What is your actual problem? Are you asking for help in defining a
>> queryset?
>>
>> --
>> Steven

>
> I actually had a queryset that was dynamically generated, so I ended
> up having to use the eval function, like this...
>
> d = {}
> for thing in things:
> query_name = 'thing_' + str(thing.id)
> query_string = 'Thing.objects.filter(type=' + str(thing.id) +
> ').order_by(\'-date\')[:3]'
> executable_string = query_name + ' = Thing.objects.filter
> (type=' + str(thing.id) + ').order_by(\'-date\')[:3]'
> exec(executable_string)
> d[query_name] = eval(query_string)
>
> And no, this is not based on user-generated input.


Why do you need this to use exec? And it seems that executable_string is
superflous - or do you want the side-effect? But then, you have the
results in d already.


query = Thing.objects.filter(type="%i" % thing.id).order_by('-date')[:3]

d[query_name] = query

As a rule of thumb: if you think you need exec, you don't.


Diez
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-15-2009
On Fri, 13 Nov 2009 14:10:10 -0800, scoopseven wrote:

> I actually had a queryset that was dynamically generated, so I ended up
> having to use the eval function, like this...
>
> d = {}
> for thing in things:
> query_name = 'thing_' + str(thing.id)
> query_string = 'Thing.objects.filter(type=' + str(thing.id) +
> ').order_by(\'-date\')[:3]'
> executable_string = query_name + ' = Thing.objects.filter
> (type=' + str(thing.id) + ').order_by(\'-date\')[:3]'
> exec(executable_string)
> d[query_name] = eval(query_string)



What an unmaintainable mess.

If I've understood it, you can make it less crap by (1) getting rid of
the unnecessary escaped quotes, (2) avoiding generating the same strings
multiple times, and (3) avoiding string concatenation.

d = {}
for thing in things:
expr = "Thing.objects.filter(type=%s).order_by('-date')[:3]"
expr = rhs % thing.id
name = "thing_%s" % thing.id
exec("%s = %s" % (name, expr))
d[name] = eval(expr)


What else can we do to fix it? Firstly, why are you creating local
variables "thing_XXX" (where XXX is the thing ID) *and* dictionary keys
of exactly the same name? I'm sure you don't need the local variables.
(If you think you do, you almost certainly don't.) That gets rid of the
exec.

Next, let's get rid of the eval:

d = {}
for thing in things:
x = thing.id
name = "thing_%s" % x
d[name] = Thing.objects.filter(type=x).order_by('-date')[:3]


About half the size, ten times the speed, and 1000 times the readability.

Unless I've missed something, you don't need either exec or eval.



--
Steven
 
Reply With Quote
 
scoopseven
Guest
Posts: n/a
 
      11-17-2009
On Nov 14, 11:55*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
> On Fri, 13 Nov 2009 14:10:10 -0800, scoopseven wrote:
> > I actually had a queryset that was dynamically generated, so I ended up
> > having to use the eval function, like this...

>
> > d = {}
> > for thing in things:
> > * * * * query_name = 'thing_' + str(thing.id)
> > * * * * query_string = 'Thing.objects.filter(type=' + str(thing.id) + *
> > ').order_by(\'-date\')[:3]'
> > * * * * executable_string = query_name + ' = Thing.objects.filter
> > (type=' + str(thing.id) + ').order_by(\'-date\')[:3]'
> > * * * * exec(executable_string)
> > * * * * d[query_name] = eval(query_string)

>
> What an unmaintainable mess.
>
> If I've understood it, you can make it less crap by (1) getting rid of
> the unnecessary escaped quotes, (2) avoiding generating the same strings
> multiple times, and (3) avoiding string concatenation.
>
> d = {}
> for thing in things:
> * * expr = "Thing.objects.filter(type=%s).order_by('-date')[:3]"
> * * expr = rhs % thing.id
> * * name = "thing_%s" % thing.id
> * * exec("%s = %s" % (name, expr))
> * * d[name] = eval(expr)
>
> What else can we do to fix it? Firstly, why are you creating local
> variables "thing_XXX" (where XXX is the thing ID) *and* dictionary keys
> of exactly the same name? I'm sure you don't need the local variables.
> (If you think you do, you almost certainly don't.) That gets rid of the
> exec.
>
> Next, let's get rid of the eval:
>
> d = {}
> for thing in things:
> * * x = thing.id
> * * name = "thing_%s" % x
> * * d[name] = Thing.objects.filter(type=x).order_by('-date')[:3]
>
> About half the size, ten times the speed, and 1000 times the readability.
>
> Unless I've missed something, you don't need either exec or eval.
>
> --
> Steven


Steven,

This worked like a charm! Thank you for your insight, it's greatly
appreciated.

Mark
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
TB1.5: Dictionaries =?windows-1252?Q?won=92t_install?= Hendrik Maryns Firefox 10 01-19-2006 04:36 PM
Jazzy and Aspell dictionaries jch.helary@free.fr Java 1 01-09-2006 05:28 PM
tools to create class dictionaries for documentation TJS ASP .Net 0 06-26-2005 08:49 PM



Advertisments