[Chicago] 2 unique keys in a python dict?

James Snyder jbsnyder at fanplastic.org
Thu Oct 9 20:30:37 CEST 2008


Someone has written a script that will do that:
http://www.djangosnippets.org/snippets/788/

You'd first have to create your model and do a syncdb to set up the  
tables, but that's easy

I don't have a full code example, but ORDER BY is not hard with the  
Django ORM. Look for order_by in the query docs: http://docs.djangoproject.com/en/dev/topics/db/queries/


Also, it may not give you nice object mapping goodness, but using the  
sqlite3 and csv modules aren't too bad.  Note that going that route,  
however, will require writing the SQL queries yourself :-)

-jsnyder

On Oct 9, 2008, at 1:05 PM, Massimo Di Pierro wrote:

> Can the Django ORM import CSV files and/or generate ORDER BY queries  
> without additional code snippets?
> If so, could you post example?
>
>
> On Oct 9, 2008, at 12:17 PM, Garrett Smith wrote:
>
>> Or just use Django.
>>
>> ----- "Massimo Di Pierro" <mdipierro at cs.depaul.edu> wrote:
>>
>>> Or....
>>>
>>> edit the header of the CSV file and call your columns
>>>
>>>    tid, paid, ttid
>>>
>>> instead of
>>>
>>>    id, paid, transaction
>>>
>>> and remove the '$' signs from there
>>>
>>> download web2py and type
>>>
>>> python web2py.py -S welcome
>>>>>> db=SQLDB()
>>>>>> db.define_table('mytable',
>>>    SQLField('tid','integer'),
>>>    SQLField('paid','double'),
>>>    SQLField('ttid','string'))
>>>
>>>>>> db.import_from_csv_file(open('yourfilename.csv'))
>>>>>> db.commit()
>>>>>>
>>> rows
>>> =
>>> db
>>> ().select
>>> (db
>>> .mytable
>>> .ttid
>>> ,db 
>>> .mytable.paid.sum(),orderby=db.mytable.ttid,groupby=db.mytable.ttid)
>>>>>> for row in rows:
>>> ... print 'transaction',row.ttid,'total
>>> amount',row[db.mytable.paid.sum()]
>>>
>>> On Oct 9, 2008, at 11:06 AM, Lukasz Szybalski wrote:
>>>
>>>> On Thu, Oct 9, 2008 at 10:49 AM,  <skip at pobox.com> wrote:
>>>>>
>>>>> Lukasz> Is it possible to have multiple keys in a python
>>>>> dictionary?
>>>>>
>>>>> Certainly.  Though I don't think that's really what you intended  
>>>>> to
>>>
>>>>> ask.
>>>>>
>>>>> Lukasz> Normally its only 1 key, and I need to have 2
>>>>> dictionaries and
>>>>> Lukasz> somehow manage the uniqueness.
>>>>> Lukasz> me={}
>>>>> Lukasz> me['First']='Lucas'
>>>>> Lukasz> me['Last']='szybalski'
>>>>>
>>>>> Lukasz> I would like to get 2 keys? Is there something similar to
>>>
>>>>> dict
>>>>> Lukasz> that would have the property that 2 or more keys are
>>>>> unique?
>>>>> Lukasz> me= ?
>>>>>
>>>>> I'm unclear what you are asking.  Do you want a list of
>>>>> dictionaries?, e.g.:
>>>>>
>>>>> names = [
>>>>>     {
>>>>>         'First': 'Lucas',
>>>>>         'Last': 'Szybalski',
>>>>>     },
>>>>>     {
>>>>>         'First': 'Skip',
>>>>>         'Last': 'Montanaro',
>>>>>     }
>>>>> ]
>>>>>
>>>>
>>>>
>>>> I have multiple records in a csv file for each userid. I need to
>>>> calculate total amount paid and last transaction.
>>>>
>>>> id,paid,transaction
>>>> 3,$10,20080101
>>>> 3,$10,20080201
>>>>
>>>>
>>>> Final Total
>>>> me={}
>>>> me[3]=($20,20080201)  (dictionary with a list)
>>>>
>>>> now
>>>> id,subid, paid,transaction
>>>> 3,1,$10,20080101
>>>> 3,2,$10,20080201
>>>>
>>>> I guess what Massimo said will work, but now I need to know which
>>> row
>>>> is the paid and which is transaction?!!
>>>> me[3,1]=(10,20080101)
>>>> me[3,2]=(10,20080201)
>>>>
>>>> I guess this could do it
>>>> me[3,1]={'paid':10 , 'transaction':20080101}
>>>>
>>>> Thanks,
>>>> Lucas
>>>> _______________________________________________
>>>> Chicago mailing list
>>>> Chicago at python.org
>>>> http://mail.python.org/mailman/listinfo/chicago
>>>
>>> _______________________________________________
>>> Chicago mailing list
>>> Chicago at python.org
>>> http://mail.python.org/mailman/listinfo/chicago
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> http://mail.python.org/mailman/listinfo/chicago
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago

--
James Snyder
Biomedical Engineering
Northwestern University
jbsnyder at fanplastic.org
http://fanplastic.org/key.txt
ph: (847) 644-2322

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20081009/93b8acc4/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 194 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/chicago/attachments/20081009/93b8acc4/attachment-0001.pgp>


More information about the Chicago mailing list