[Tutor] Tutor Digest, Vol 104, Issue 69

Cheng googcheng at gmail.com
Wed Oct 17 12:29:59 CEST 2012



tutor-request at python.org编写:

>Send Tutor mailing list submissions to
>	tutor at python.org
>
>To subscribe or unsubscribe via the World Wide Web, visit
>	http://mail.python.org/mailman/listinfo/tutor
>or, via email, send a message with subject or body 'help' to
>	tutor-request at python.org
>
>You can reach the person managing the list at
>	tutor-owner at python.org
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of Tutor digest..."
>
>
>Today's Topics:
>
>   1. Re: CSV -> sqlite tables with foreign keys (eryksun)
>   2. Re: Why difference between printing string & typing its
>      object reference at the prompt? (Dwight Hutto)
>   3. Re: managing memory large dictionaries in python (Dwight Hutto)
>   4. Re: modulo (Dwight Hutto)
>   5. program for a problem (Tharuni Dheeraj)
>   6. Consecutive Sequence (syed zaidi)
>
>
>----------------------------------------------------------------------
>
>Message: 1
>Date: Wed, 17 Oct 2012 05:19:27 -0400
>From: eryksun <eryksun at gmail.com>
>To: Monte Milanuk <memilanuk at gmail.com>
>Cc: Tutor at python.org
>Subject: Re: [Tutor] CSV -> sqlite tables with foreign keys
>Message-ID:
>	<CACL+1avbryzMMWtiZHi9R4uBMgM3=NYZt65_NMDLNBndv5a02Q at mail.gmail.com>
>Content-Type: text/plain; charset=UTF-8
>
>On Tue, Oct 16, 2012 at 11:59 PM, Monte Milanuk <memilanuk at gmail.com> wrote:
>>
>>         address = list(row)
>>         address.append(person)
>>         row = tuple(address)
>
>The rows from the csv.reader are already lists. Also, the parameter
>list in the 2nd argument only needs to be a sequence (e.g. tuple,
>list, string), or it can also be a dict if the statement uses named
>placeholders.
>
>> from what I've found on the web, I get the distinct impression
>> that converting from a tuple to a list and back is considered
>> poor practice at best and generally to be avoided.
>
>To update a record in a tuple you can use slicing and concatenation
>(+) as an alternative to creating a temporary list.  A list is more
>applicable to homogenous data (e.g. a list of tuples, each a data
>record). If you want a container for a record that you can modify more
>efficiently, use a dict or a custom object. For the latter, look into
>ORMs such as Storm:
>
>https://storm.canonical.com/
>
>> Any suggestions?
>
>I found a Stack Overflow answer that uses a table "view" combined with
>an "instead of" trigger to update two tables with one insert.
>
>http://stackoverflow.com/a/11715983/205580
>
>Here's my meager attempt at an adaptation (some names have been
>changed to protect the innocent...):
>
>    import csv
>    import sqlite3
>
>    con = sqlite3.connect(':memory:')
>    cur = con.cursor()
>
>    cur.execute('''create table person (
>      id integer primary key autoincrement,
>      firstname, midinit, lastname, birthdate)
>    ''')
>
>    cur.execute('''create table address (
>      id integer primary key autoincrement,
>      person_id integer references person not null,
>      street, city, state, zipcode)
>    ''')
>
>    cur.execute('''create view person_view as
>      select
>        person.firstname, person.midinit, person.lastname,
>        person.birthdate, address.street, address.city,
>        address.state, address.zipcode
>      from
>        person inner join address on person.id = address.person_id
>    ''')
>
>    cur.execute('''create trigger person_view_insert
>      instead of insert on person_view
>      begin
>        insert into
>          person (firstname, midinit, lastname, birthdate)
>          values (new.firstname, new.midinit, new.lastname,
>                  new.birthdate);
>        insert into
>          address (person_id, street, city, state, zipcode)
>          values ((select last_insert_rowid()),
>                  new.street, new.city, new.state, new.zipcode);
>      end
>    ''')
>
>    import io
>    data = io.BytesIO(b'''\
>    John,G.,Smith,1972-11-10,123 Any Place,Somewhere,Missouri,58932
>    Jane,L.,Jones,1971-12-20,321 Some Place,Anywhere,Kansas,12345
>    ''')
>
>    reader = csv.reader(data)
>    for row in reader:
>        cur.execute('''insert into
>          person_view (firstname, midinit, lastname, birthdate,
>                       street, city, state, zipcode)
>          values (?,?,?,?,?,?,?,?)''', row)
>
>    # output
>    for row in cur.execute('select * from person'):
>        print row
>    for row in cur.execute('select * from address'):
>        print row
>
>
>person table:
>
>    (1, u'John', u'G.', u'Smith', u'1972-11-10')
>    (2, u'Jane', u'L.', u'Jones', u'1971-12-20')
>
>address table:
>
>    (1, 1, u'123 Any Place', u'Somewhere', u'Missouri', u'58932')
>    (2, 2, u'321 Some Place', u'Anywhere', u'Kansas', u'12345')
>
>
>------------------------------
>
>Message: 2
>Date: Wed, 10 Oct 2012 21:58:39 -0400
>From: Dwight Hutto <dwightdhutto at gmail.com>
>To: "Steven D'Aprano" <steve at pearwood.info>
>Cc: tutor at python.org
>Subject: Re: [Tutor] Why difference between printing string & typing
>	its object reference at the prompt?
>Message-ID:
>	<CA+vVgJXY29009t04oUcqhT0=AT6dW3y66pEHF4zKjARfmi4gsQ at mail.gmail.com>
>Content-Type: text/plain; charset=ISO-8859-1
>
>If your app has  a standard usage of phrases, you can place a file in
>that translates a tag into a particular language phrase.
>
>
>
>if submit_tag_selection == 'english':
>     submit = 'Submit'
>if submit_tag_selection == 'english':
>     submit = 'Soumettre'
>
>Of course this could be done without the if, you would just translate
>the normal selections within a file with the commonly used phrases in
>the app, and substitute it within a parse for:
>
>x = open('translate_file_french', 'r')
>for line in x:
>     if line.split('=')[0] == 'Submit':
>           print '%s'   %   (line.split('=')[1])
>
>'Soumettre'
>
>*Untested, but should work
>
>
>-- 
>Best Regards,
>David Hutto
>CEO: http://www.hitwebdevelopment.com
>
>
>------------------------------
>
>Message: 3
>Date: Tue, 16 Oct 2012 21:30:43 -0400
>From: Dwight Hutto <dwightdhutto at gmail.com>
>To: Abhishek Pratap <abhishek.vit at gmail.com>
>Cc: tutor at python.org
>Subject: Re: [Tutor] managing memory large dictionaries in python
>Message-ID:
>	<CA+vVgJX-9MqG9KM_sFiWv_jMe6mPnc1W50u3Tvj78AWCKMrqzQ at mail.gmail.com>
>Content-Type: text/plain; charset=ISO-8859-1
>
>On Tue, Oct 16, 2012 at 12:57 PM, Abhishek Pratap
><abhishek.vit at gmail.com> wrote:
>> Hi Guys
>>
>> For my problem I need to store 400-800 million 20 characters keys in a
>> dictionary and do counting. This data structure takes about 60-100 Gb
>> of RAM.
>> I am wondering if there are slick ways to map the dictionary to a file
>> on disk and not store it in memory but still access it as dictionary
>> object. Speed is not the main concern in this problem and persistence
>> is not needed as the counting will only be done once on the data. We
>> want the script to run on smaller memory machines if possible.
>>
>> I did think about databases for this but intuitively it looks like a
>> overkill coz for each key you have to first check whether it is
>> already present and increase the count by 1  and if not then insert
>> the key into dbase.
>>
>> Just want to take your opinion on this.
>>
>> Thanks!
>> -Abhi
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
>My inexperienced advice would be to begin with the storage areas
>available. I would begin by eliminating certain things such as:
>
>x = {'one_entry' : 1}
>
>into
>
>x = {'one_entry':1}
>
>To map, you would want maybe different db files that contain certain
>info within a certain range. 0-1000 entries in the first file, etc.
>
>os.walk a directory, and find the mapped file in a particular range
>file, then go straight to the entry needed.
>
>Make the dict one long line, and you could eliminate any /n newline chars.
>
> I could do better with more time, but that seems like a good solution
>at this point.
>
>Best Regards,
>David Hutto
>CEO: http://www.hitwebdevelopment.com
>
>
>------------------------------
>
>Message: 4
>Date: Sun, 7 Oct 2012 20:16:03 -0400
>From: Dwight Hutto <dwightdhutto at gmail.com>
>To: d at davea.name
>Cc: "tutor at python.org" <tutor at python.org>, Jan Karel Schreuder
>	<jan9447 at me.com>
>Subject: Re: [Tutor] modulo
>Message-ID:
>	<CA+vVgJUbeTkUCi=fqqR4qi2yuCLWo7L+wkbNY6DkKGddnVPFPg at mail.gmail.com>
>Content-Type: text/plain; charset=ISO-8859-1
>
>______________________________________________
>>>> I'm not a professional programmer, so I might be way off base here.
>
>You mean you haven't dealt with this subject yet...
>
>But what I like about Pythons modulo solution is that I can use it to
>right and left shift in lists or tuples, and I will link to the first
>element when I right shift past the last element and link to the last
>element when I left shift past the first element. In other words I can
>consider the last as a chain where the last and the first element are
>connected. This I find useful in surprisingly many situations.
>
>It's uised for, what it's used for, until you know the full lower
>level implementation/parsing of objects like immutables(tuples), and
>mutables(lists,dicts,strings,etc
>
>>>
>>>
>> Certainly, but you've never had to do that with lists or tuples having
>> negative lengths.  It's a negative modulus that I'm complaining about.
>
>Can you show some example code here?
>
>-- 
>Best Regards,
>David Hutto
>CEO: http://www.hitwebdevelopment.com
>
>
>------------------------------
>
>Message: 5
>Date: Mon, 8 Oct 2012 08:55:09 -0700
>From: Tharuni Dheeraj <tharunichowdary at gmail.com>
>To: tutor at python.org
>Subject: [Tutor] program for a problem
>Message-ID:
>	<CAEcme6LmD5DWBqb4E_0JnypYOY5x0uEYNQ36wmgpuaUhv8VYyw at mail.gmail.com>
>Content-Type: text/plain; charset="iso-8859-1"
>
>please send me the program for the following que:
>
>Write a program that asks the user for a dollar amount.It then reports the
>corresponding number of euros by using the current exchange rate.
>--
>Regards,
>Tharuni Dheeraj
>-------------- next part --------------
>An HTML attachment was scrubbed...
>URL: <http://mail.python.org/pipermail/tutor/attachments/20121008/4ecc37c4/attachment-0001.html>
>
>------------------------------
>
>Message: 6
>Date: Sat, 13 Oct 2012 12:02:20 +0100
>From: syed zaidi <syedzaidi85 at hotmail.co.uk>
>To: <tutor at python.org>
>Subject: [Tutor] Consecutive Sequence
>Message-ID: <BLU143-W8F81515088A8D2AB4889FEB730 at phx.gbl>
>Content-Type: text/plain; charset="iso-8859-1"
>
>
>Hi,I am trying to develop a python code that takes a character string as input and  finds for the occurrence of letters that are occurring thrice or more consecutively.For E.g.
>a = 'atttttaattaaacagagtgagcagaaaat'In the output I want a list of those characters that are occuring thrice or more.
>like in this case outout must b out_put = ['ttttt','aaa','aaaa']
>Can someone please suggest a code for this. 		 	   		  
>-------------- next part --------------
>An HTML attachment was scrubbed...
>URL: <http://mail.python.org/pipermail/tutor/attachments/20121013/8aa14077/attachment.html>
>
>------------------------------
>
>Subject: Digest Footer
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>------------------------------
>
>End of Tutor Digest, Vol 104, Issue 69
>**************************************


More information about the Tutor mailing list