[Tutor] List of lists optimization and printing.

2011-07-29 Thread Sergey
Hello.

Yes, catb.org and so on.
I was searching for some kind of finding max width in a table (a list of two
lists) and had found this mailing list.
So I want somebody to look at my code and say what can be done better from a
programmer point of view.
Just SQL like printing and writing.
I mean optimization.
I always want to know what is the best way. Thanks.
* I don't know is it good to paste 62 lines there so here's the pastie
http://pastie.org/2291721
I hope it will be monospaced.^U
I know about SQLAlchemy but I just want to have my data in plain text because I
can't find good cross-platform suites to keep contacts, many contacts. I mean
phone number, name and so on. Something for a little bit entertprise tasks.
Yes.
Thanks.
Debian bleeding edge.
Python 2.6 default. And 2.7, 3.2 too.

Site: site
Login: login
Password: password
[['site', 'login', 'password']]
[4, 5, 8]
+--+---+--+
| Site | Login | Password |
+--+---+--+
| site | login | password |
+--+---+--+


South Africas premier free email service - www.webmail.co.za 

Save on insurance with OUTsurance
https://www.outsurance.co.za/insurance-quote/?source=webmailmailer&cr=facp11_468x60&cid=221


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mainloop conflict

2011-07-29 Thread Peter Otten
Stefan Behnel wrote:

> Christopher King, 29.07.2011 17:08:
>> On Thursday, July 28, 2011, Dave Angel wrote:
>>> On 07/28/2011 08:32 PM, Christopher King wrote:

 Dear Tutor Dudes,
  I have a socket Gui program. The only problem is that socket.recv
>> waits
 for a response, which totally screws Tkinter I think. I tried making
 the timeout extremely small (it was alright if I didn't receive
 anything, I was excepting that a lot) but I think that screwed socket.
 Anyone have
>> words
 of wisdom.

 Sincerely,
  Me

>>> Sure:
>>>
>>> Do the socket I/O on a separate thread.
>>
>> I was afraid of that.
> 
> Understandable. However, as I already said, you don't need to do that,
> just go the obvious route.

While searching for

http://effbot.org/zone/tkinter-threads.htm

I stumbled upon

http://effbot.org/pyfaq/can-i-have-tk-events-handled-while-waiting-for-i-o.htm

which looks even simpler indeed.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mainloop conflict

2011-07-29 Thread Stefan Behnel

Christopher King, 29.07.2011 17:08:

On Thursday, July 28, 2011, Dave Angel wrote:

On 07/28/2011 08:32 PM, Christopher King wrote:


Dear Tutor Dudes,
 I have a socket Gui program. The only problem is that socket.recv

waits

for a response, which totally screws Tkinter I think. I tried making the
timeout extremely small (it was alright if I didn't receive anything, I
was excepting that a lot) but I think that screwed socket. Anyone have

words

of wisdom.

Sincerely,
 Me


Sure:

Do the socket I/O on a separate thread.


I was afraid of that.


Understandable. However, as I already said, you don't need to do that, just 
go the obvious route.


Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sum files' size

2011-07-29 Thread Susana Iraiis Delgado Rodriguez
Thank you to all of you!

After I read your mails I started to modify my code, I applied Ramit
suggestion and got the result I wanted:

mport os
file_list = []
folders = None
for root, folders, files in os.walk('C:\\'):
file_list.extend(os.path.join(
root,fi) for fi in files if (fi.endswith.shp))
for row, filepath in enumerate(file_list, start=1):
n = os.path.splitext(filepath)
p = n[0]+'.prj'
shx = n[0]+'.shx'


#Function to get size in humam readable terms:
def sizeof_fmt(num):
 for x in ['bytes','KB','MB','GB','TB']:
  if num < 1024.0:
   return "%3.1f%s" % (num, x)
  num /= 1024.0
s = os.path.getsize(filepath)
shx1 = os.path.getsize(shx)
p1 = s = os.path.getsize(p)
total = sizeof_fmt(s+shx1+p1)



2011/7/28 Prasad, Ramit 

> >kb = sizeof_fmt(s)
> >shx1 = os.path.getsize(shx)
> >kb2 = sizeof_fmt(shx1)
> > total = kb+kb2+kb3
>
> Instead only retrieve the formatted output at the end. That way you will
> not have to worry about converting back from strings, nor have to worry
> about adding number with different units (e.g. 10KB + 10MB).
>
> kb = s
> kb2 = os.path.getsize(shx)
> total = sizeof_fmt(kb+kb2+kb3)
>
> Ramit
>
>
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
>
>
>
> This communication is for informational purposes only. It is not
> intended as an offer or solicitation for the purchase or sale of
> any financial instrument or as an official confirmation of any
> transaction. All market prices, data and other information are not
> warranted as to completeness or accuracy and are subject to change
> without notice. Any comments or statements made herein do not
> necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
> and affiliates.
>
> This transmission may contain information that is privileged,
> confidential, legally privileged, and/or exempt from disclosure
> under applicable law. If you are not the intended recipient, you
> are hereby notified that any disclosure, copying, distribution, or
> use of the information contained herein (including any reliance
> thereon) is STRICTLY PROHIBITED. Although this transmission and any
> attachments are believed to be free of any virus or other defect
> that might affect any computer system into which it is received and
> opened, it is the responsibility of the recipient to ensure that it
> is virus free and no responsibility is accepted by JPMorgan Chase &
> Co., its subsidiaries and affiliates, as applicable, for any loss
> or damage arising in any way from its use. If you received this
> transmission in error, please immediately contact the sender and
> destroy the material in its entirety, whether in electronic or hard
> copy format. Thank you.
>
> Please refer to http://www.jpmorgan.com/pages/disclosures for
> disclosures relating to European legal entities.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running files from command prompt

2011-07-29 Thread Alexander Quest
Alexander- thanks for the tip as to sticking with Python 3.
Steven, I greatly appreciate that breakdown. You're right about the error:
it was a syntax error on that line; I'll make sure to include the
descriptions in the future. As far as finding a new tutorial, I am going to
see if Google's class works out with Python 3.1, and if not, I'll switch
over to a different one.

-Alexander

On Thu, Jul 28, 2011 at 10:27 PM, Steven D'Aprano wrote:

> Alexander Quest wrote:
>
>> To clarify, the particular file that was giving me trouble was the basic
>> "hello world" file. The original code on line 29 read as such: print
>> 'Hello', name
>> When I ran "C:\google-python-exercises> python hello.py, it gave me an
>> error
>> on that line (line 29), but when I changed that line to print ('Hello',
>> name), that is, including the parentheses, it printed out "hello world" as
>> it should. I'm assuming that this means that one of the differences
>> between
>> Python 2.X and Python 3.X is that the print function necessitates
>> parentheses in the latter versions but not in the former.
>>
>
>
> Yes, that is correct.
>
> To be a programmer (whether professional or amateur), you need to learn to
> *pay attention to the error given*. "It gave me an error" is meaningless.
> What does the error message say?
>
> In this case, I expect it is a SyntaxError. But you need to learn to read
> the error message and understand what it is trying to tell you. Some errors
> are cryptic and don't help, but generally speaking Python is pretty good
> about giving useful error messages:
>
>
> >>> a = [1, 2, 3]
> >>> len a
>  File "", line 1
>len a
>^
> SyntaxError: invalid syntax
>
>
> Admittedly you do need to learn that Python functions require parentheses,
> but apart from that, the error tells you what is wrong: you can't follow a
> function len with another name a without something between them. This is
> illegal syntax.
>
>
>
>
>  I am a bit
>> confused as to why this is, assuming I am correct in my assumption above,
>> because I was under the impression that code written for earlier python
>> versions will work for later python versions, as is the case here.
>>
>
> Not quite. It is (mostly) true for Python 1.x and 2.x, but Python 3 has
> deliberately included some backwards incompatible changes. The biggest two
> are that strings are now Unicode rather than byte strings, and that print is
> now a function instead of a statement. So, yes, in Python 3 you have to call
> it with parentheses.
>
> The differences are still quite minor -- think of Python 2.x and Python 3.x
> being like the differences between American English and British English.
> Provided you pay attention to the error messages, and remember to add round
> brackets after print, tutorials for 2.x should still *mostly* work.
>
>
>
>  I just wanted to add this info to clarify my last question regarding
>> whether
>> or not I should install Python 2.X and uninstall Python 3.1 that I have
>> now,
>>
>
> Personally, I would consider it wiser to find a Python 3 tutorial. Python 3
> is the future, and you will need to learn it eventually.
>
>
>
>
> --
> Steven
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mainloop conflict

2011-07-29 Thread Dave Angel

On 07/29/2011 11:08 AM, Christopher King wrote:

I was afraid of that.

Please don't top-post.  It's becoming more prevalent on this list, but 
it makes things very confusing.  I need to put my response just after 
yours, but then the other relevant pieces are out of order.

On Thursday, July 28, 2011, Dave Angel  wrote:

On 07/28/2011 08:32 PM, Christopher King wrote:

Dear Tutor Dudes,
 I have a socket Gui program. The only problem is that socket.recv

waits

for a response, which totally screws Tkinter I think. I tried making the
timeout extremely small (it was alright if I didn't receive anything, I
was excepting that a lot) but I think that screwed socket. Anyone have

words

of wisdom.

Sincerely,
 Me


Sure:

Do the socket I/O on a separate thread.

--

DaveA





Threads aren't that tough in Python.

Threads aren't always helpful in CPython, because of the GIL. But this 
is one case where they are.  And somebody has pointed out a library 
which presumably hides the thread details from you.




--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mainloop conflict

2011-07-29 Thread Christopher King
I was afraid of that.

On Thursday, July 28, 2011, Dave Angel  wrote:
> On 07/28/2011 08:32 PM, Christopher King wrote:
>>
>> Dear Tutor Dudes,
>> I have a socket Gui program. The only problem is that socket.recv
waits
>> for a response, which totally screws Tkinter I think. I tried making the
>> timeout extremely small (it was alright if I didn't receive anything, I
>> was excepting that a lot) but I think that screwed socket. Anyone have
words
>> of wisdom.
>>
>> Sincerely,
>> Me
>>
> Sure:
>
> Do the socket I/O on a separate thread.
>
> --
>
> DaveA
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading .gz files

2011-07-29 Thread Steven D'Aprano

Oh, I forgot to say something else...

Hanlie Pretorius wrote:


f1 = 'GSMaP_MVK+.20050101.00.0.1deg.hourly.v484.gz'
f2 = ''text.txt.gz'
if1 = gzip.open(f1, 'rb')
if2 = gzip.open(f2,'rb')
try:
   print if1.read()
   print 'done with f1'



Once you've read the file once, the file pointer is at the end of the 
file, and reading it again returns the empty string. Example:



>>> y = gzip.open('spam.gz', 'rb')
>>> y.read()  # read to the end
'spam spam spam'
>>> y.read()  # anything left?
''
>>> y.seek(0)  # go back to the beginning
>>> y.read()
'spam spam spam'



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading .gz files

2011-07-29 Thread Steven D'Aprano

Hanlie Pretorius wrote:


[code]
import gzip

f1 = 'GSMaP_MVK+.20050101.00.0.1deg.hourly.v484.gz'
f2 = ''text.txt.gz'
if1 = gzip.open(f1, 'rb')
if2 = gzip.open(f2,'rb')
try:
   print if1.read()
   print 'done with f1'
   print if2.read()
   print 'done with f2'
finally:
   if1.close()
   if2.close()
[/code]

[output]
done with f1
Text to test gzip module.
done with f2
[/output]



This seems to indicate that something is wrong with f1 (the GSMaP file - a


Are you sure it is an actual gzip file? You refer to gzip, 7-zip, and 
unzip in your post -- these are all different compression formats. Just 
because the file is *named* .gz doesn't necessarily mean it is a gzip file.



Try this:

if1 = gzip.open(f1, 'rb')
print repr(if1.read())
print if1.size, if1.crc, if1.name

and see what they say.





--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Urllib Problem

2011-07-29 Thread Steven D'Aprano

George Anonymous wrote:

I am trying to make a simple programm with Python 3,that tries to open
differnet pages from a wordlist and prints which are alive.Here is the code:
from urllib import request
fob=open('c:/passwords/pass.txt','r')
x = fob.readlines()
for i in x:
urllib.request.openurl('www.google.gr/' + i)

But it doesent work.Whats the problem?



A guessing game! I LOVE guessing games!!! :)

Let's seen let me guess what you mean by "doesn't work":

- the computer locks up and sits there until you hit the restart switch
- the computer gives a Blue Screen Of Death
- Python raises an exception
- Python downloads the Yahoo website instead of Google
- something else


My guess is... you're getting a NameError exception, like this one:


>>> from urllib import request
>>> x = urllib.request.openurl('www.google.com')
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'urllib' is not defined


Am I close?


You need to use request.urlopen, not urllib.request.openurl.

That's your *first* problem. There are more. Come back if you need help 
with the others, and next time, don't make us play guessing games. Show 
us the code you use -- copy and paste it, don't retype it from memory -- 
what you expect should happen, and what actually happens instead.





--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Urllib Problem

2011-07-29 Thread Alexander
On Fri, Jul 29, 2011 at 5:58 AM, Karim  wrote:

> **
> On 07/29/2011 11:52 AM, George Anonymous wrote:
>
> I am trying to make a simple programm with Python 3,that tries to open
> differnet pages from a wordlist and prints which are alive.Here is the code:
> from urllib import request
> fob=open('c:/passwords/pass.txt','r')
> x = fob.readlines()
> for i in x:
> urllib.request.openurl('www.google.gr/' + i)
>
> But it doesent work.Whats the problem?
>
>
> Please give the exception error you get?!
> And you should have in the html header
> the html code error number which gives
> you the fail answer from the server.
>
> Cheers
> Karim
>
> As Karim noted you'll want to mention any exceptions you are getting. I'm
not sure what it is you are trying to do with your code. If you'd like to
try to open each line and try something if it works else an exception the
code may read something similar to:

fob = open('C:/passwords/pass.txt','r')
fob_rlines = fob.readlines()
for line in fob_rlines:
try:
#whatever it is you would like to do with each line
except Exception: #where code didn't work and an exception occured
#whatever you would like to do when a particular *Exception* occurs
Hope that helps,
Alexander

>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription 
> options:http://mail.python.org/mailman/listinfo/tutor
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Reading .gz files

2011-07-29 Thread Hanlie Pretorius
Hi,

I'm working on Windows XP with Python 2.6.

I need to read and process hundreds of binary files that are in the
.gz archive format.

I found a site (http://www.doughellmann.com/PyMOTW/gzip/) and tried
their code with two files: one of the hundreds of files that I need to
process (f1 below) and one that I created with 7-Zip from a text file
that contains the text 'Text to test gzip module.' (f2 below). The
code and the output follow:

[code]
import gzip

f1 = 'GSMaP_MVK+.20050101.00.0.1deg.hourly.v484.gz'
f2 = ''text.txt.gz'
if1 = gzip.open(f1, 'rb')
if2 = gzip.open(f2,'rb')
try:
   print if1.read()
   print 'done with f1'
   print if2.read()
   print 'done with f2'
finally:
   if1.close()
   if2.close()
[/code]

[output]
done with f1
Text to test gzip module.
done with f2
[/output]

This seems to indicate that something is wrong with f1 (the GSMaP file - a
binary file), but I can unzip the file manually and read it with a
python script. I have hundreds of GSMAP files that have unique
archived file names, but they all unzip to the same binary file, so I
have to process the archived files in the python script.

I would be grateful if someone could help me achieve this.

Regards
Hanlie
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Urllib Problem

2011-07-29 Thread Karim

On 07/29/2011 11:52 AM, George Anonymous wrote:
I am trying to make a simple programm with Python 3,that tries to open 
differnet pages from a wordlist and prints which are alive.Here is the 
code:

from urllib import request
fob=open('c:/passwords/pass.txt','r')
x = fob.readlines()
for i in x:
urllib.request.openurl('www.google.gr/ ' + i)

But it doesent work.Whats the problem?



Please give the exception error you get?!
And you should have in the html header
the html code error number which gives
you the fail answer from the server.

Cheers
Karim



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Urllib Problem

2011-07-29 Thread George Anonymous
I am trying to make a simple programm with Python 3,that tries to open
differnet pages from a wordlist and prints which are alive.Here is the code:
from urllib import request
fob=open('c:/passwords/pass.txt','r')
x = fob.readlines()
for i in x:
urllib.request.openurl('www.google.gr/' + i)

But it doesent work.Whats the problem?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] About the Mailing List

2011-07-29 Thread Alan Gauld

Steven D'Aprano wrote:


You can see the archives by going to this page here:

http://mail.python.org/mailman/listinfo/tutor

and following the links to the archives. There are *three* given: two 
external archives (Activestate and Gmane) and one held by python.org 
itself.


And if you use Gmane you can read the list as a newsfeed which gives you 
the advantages of a digest and the ability to send single targetted 
replies. Both Outlook Express and Thunderbird can read newsfeeds as well 
as mail. Other mail tools may well do likewise.


HTH,

Alan G
A gmane news user :-)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor