Re: [Tutor] Trying to understand sys.getrefcount()

2006-10-13 Thread Alan Gauld
> class junk(object):
> ... pass
> ...
 sys.getrefcount(junk)
> 5

> I understand why j1 is 1 higher than I expect, based on the docs, 
> but why
> does getrefcount(junk) return 5 before any instances of class junk 
> are made?

Because Python s holding 4 references to the class object internally.
For example the namespace dictionary will have one. I've no idea what
all the others will be but presumably thats whats happening. Remember
classes are objects too.

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 32, Issue 49

2006-10-13 Thread Meher Kolli
Message: 8Date: Fri, 13 Oct 2006 02:03:49 +0100From: "Asrarahmed Kadri" <
[EMAIL PROTECTED]>Subject: [Tutor] How to write strings with new line character in afileTo: pythontutor Message-ID:<[EMAIL PROTECTED]>Content-Type: text/plain; charset="iso-8859-1"
Folks,I am trying to enter names in a file; each on a new line with this code, butnot working:done = 0*while not done:str = raw_input("Enter login name:\t to quit type 'q': ")
if str == 'q':done = 1else:  **   str = str + '\n'**   fd.write(str)*The traceback is as under:Traceback (most recent call last):  File "scrap.py", line 38, in ?
fd.write(str)IOError: (0, 'Error')Please help to rectify this error...You can't  add  \n  to  a string ..up have  use something like this   if str == 'q':   done = 1
   fd.close()   else:   fd.write("%s \n" %(str))  -- Meher KolliSunnyvale,CA-94085
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 32, Issue 49

2006-10-13 Thread Alan Gauld

"Meher Kolli" <[EMAIL PROTECTED]> wrote in message 

> You can't  add  \n  to  a string ..up have  use something like this
> 
>   if str == 'q':
>   done = 1
>   fd.close()
>   else:
>   fd.write("%s \n" %(str))

What makes you think so?

>>> s = 'l'
>>> s += '\n'
>>> s
'l\n'
>>>

Seems to work '\n' is just a character like any other.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to write strings with new line character in a file

2006-10-13 Thread Asrarahmed Kadri
Here is the complete code:
fd is the file handle. 
import sys

 def check_dup(fd1):    print fd1    fd1.seek(0,0)    done = 0    list1 = []    while not done:    x = fd1.readline()    if x == "":    done = 1    else:
    list1.append(x)    return list1
    fname = raw_input("Enter the file name to write data to:\t")
fd = open(fname,'a+')print fddone = 0
while not done:    str = raw_input("Enter login name:\t to quit type 'q'")        if str == 'q':    done = 1    else:    flag = check_dup(fd)    print flag    if str in flag:
    print "Login already exists.!!"    else:    fd.seek(0,2)    fd.write(str + '\n')
 
 
 
 
 
 
 
On 10/13/06, Bob Gailer <[EMAIL PROTECTED]> wrote:
Asrarahmed Kadri wrote:> Folks,>> I am trying to enter names in a file; each on a new line with this
> code, but not working:>> done = 0> *while not done:> str = raw_input("Enter login name:\t to quit type 'q': ")> if str == 'q':> done = 1> else:  *
> *   str = str + '\n'*> *   fd.write(str)> *No can help without seeing the code that creates fd. Please post that.> The traceback is as under:>> Traceback (most recent call last):
>   File "scrap.py", line 38, in ?> fd.write(str)> IOError: (0, 'Error')>> Please help to rectify this error...> --> To HIM you shall return.> 
>> ___> Tutor maillist  -  Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor
>--Bob Gailer510-978-4454-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to write string in a file, each one on new line???

2006-10-13 Thread Asrarahmed Kadri
Folks,
 
I dont like to swamp your mailboxes with my trivial questions. But please tell me why this isnt working??
 
str = str + '\n'
fd.write(str)   # fd is teh file handle 
 
the traceback is as under:
 
Traceback (most recent call last):  File "scrap.py", line 39, in ?    fd.write(str + '\n')IOError: (0, 'Error')
 
-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to write strings with new line character in a file

2006-10-13 Thread Kent Johnson
Asrarahmed Kadri wrote:
> Here is the complete code:
> fd is the file handle.
>  
> import sys
> 
>  def check_dup(fd1):
> print fd1
> fd1.seek(0,0)
> done = 0
> list1 = []
> while not done:
> x = fd1.readline()
> if x == "":
> done = 1
> else:
> list1.append(x)
> return list1
> 
>
> fname = raw_input("Enter the file name to write data to:\t")
> 
> fd = open(fname,'a+')
> print fd
> done = 0
> 
> while not done:
> str = raw_input("Enter login name:\t to quit type 'q'")
>
> if str == 'q':
> done = 1
> else:
> flag = check_dup(fd)
> print flag
> if str in flag:
> print "Login already exists.!!"
> else:
> fd.seek(0,2)
> fd.write(str + '\n')

I don't know why this isn't working, but I do know a better way to write 
it that will work. Keep the list of login names in a list in memory 
until you have all the names, then write them to a file. Your program 
will look something like this:

ask the file name
open the file for reading and read all the names into a list
close the file

while not done:
   ask the user for a name
   if the name is not in the list:
 add the name to the list

open the file for writing
write the list to the file
close the file

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to write string in a file, each one on new line???

2006-10-13 Thread Kent Johnson
Asrarahmed Kadri wrote:
> Folks,
>  
> I dont like to swamp your mailboxes with my trivial questions. 

We don't mind trivial questions, this is a list for beginners. But there 
is no need to swamp our mailboxes by asking the same question more than 
once, and if you include complete code and complete error tracebacks you 
have a much better chance of getting a good answer.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Code for checking whether an input string is a palindrome or not.

2006-10-13 Thread Asrarahmed Kadri
Hi,
 
Here is a code that reports whether the string is a palindrome or not. I have posted it to help people new to programming ( i am also a newbie) to understand the logic and comment on it.
 
Share and expand your knowledge as well as understanding...
 
Happy scripting
 
Regards,
Asrar
 
Code:
 

  string1 = raw_input("enter a string\n")
str_len = len(string1)
 
flag = 0j = str_len-1
for i in range(0,(str_len-1)/2): if string1[i] == string1[j]:  j = j-1
 else:  flag = 1  breakif flag ==0: print "The entered string is a PALINDROME"else: print "The entered string is not a PALINDROME"-- To HIM you shall return. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code for checking whether an input string is a palindromeor not.

2006-10-13 Thread Alan Gauld
"Asrarahmed Kadri" <[EMAIL PROTECTED]> wrote in message

> Here is a code that reports whether the string is a palindrome or 
> not.

I'd suggest a simpler approach is to use the string/list methods in 
Python:

string1 = list(raw_input("enter a string\n"))  #using a list is easier
string2 = string1[:]  # make a copy
string2.reverse()
if string1 == string2:
 print "The entered string is a PALINDROME"
else:
 print "The entered string is not a PALINDROME"

HTH,

Alan G

> string1 = raw_input("enter a string\n")
> str_len = len(string1)
> flag = 0
> j = str_len-1
>
> for i in range(0,(str_len-1)/2):
> if string1[i] == string1[j]:
>  j = j-1
>
> else:
>  flag = 1
>  break
> if flag ==0:
> print "The entered string is a PALINDROME"
> else:
> print "The entered string is not a PALINDROME"




> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code for checking whether an input string is a palindrome or not.

2006-10-13 Thread Simon Brunning
def pal(spam): return spam == spam[::-1]

;-)

-- 
Cheers,
Simon B
[EMAIL PROTECTED]
http://www.brunningonline.net/simon/blog/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code for checking whether an input string is a palindrome or not.

2006-10-13 Thread Kent Johnson
Asrarahmed Kadri wrote:
> 
> Hi,
>  
> Here is a code that reports whether the string is a palindrome or not. I 
> have posted it to help people new to programming ( i am also a newbie) 
> to understand the logic and comment on it.
>  
> Share and expand your knowledge as well as understanding...
>  
> Happy scripting
>  
> Regards,
> Asrar
>  

Others have pointed out that there are easier ways to do this. I'll 
comment on your code.

> Code:
>  
> 
>   
> string1 = raw_input("enter a string\n")
> 
> str_len = len(string1)
> 
>  
> 
> flag = 0
> j = str_len-1
> 
> for i in range(0,(str_len-1)/2):

I think there is an off-by-one error here; try 'abcdba' as a test.

>  if string1[i] == string1[j]:

Negative indices would work well here, you don't need j at all:
   if string1[i] == string1[-i]:

This would also be a good place to use for: / else: but that is a bit 
more advanced.

Kent

>   j = j-1
> 
>  else:
>   flag = 1
>   break
> if flag ==0:
>  print "The entered string is a PALINDROME"
> else:
>  print "The entered string is not a PALINDROME"
> 
> 
> -- 
> To HIM you shall return.
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 32, Issue 51

2006-10-13 Thread Meher Kolli
On 10/13/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]
> wrote:Send Tutor mailing list submissions to
tutor@python.orgTo subscribe or unsubscribe via the World Wide Web, visithttp://mail.python.org/mailman/listinfo/tutoror, via email, send a message with subject or body 'help' to
[EMAIL PROTECTED]You can reach the person managing the list at[EMAIL PROTECTED]
When replying, please edit your Subject line so it is more specificthan "Re: Contents of Tutor digest..."Today's Topics:   1. Re: Tutor Digest, Vol 32, Issue 49 (Alan Gauld)--
Message: 1Date: Fri, 13 Oct 2006 10:09:04 +0100From: "Alan Gauld" <[EMAIL PROTECTED]>Subject: Re: [Tutor] Tutor Digest, Vol 32, Issue 49
To: tutor@python.orgMessage-ID: <[EMAIL PROTECTED]>Content-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original"Meher Kolli" <[EMAIL PROTECTED]> wrote in message> You can't  add  \n  to  a string ..up have  use something like this
>>   if str == 'q':>   done = 1>   fd.close()>   else:>   fd.write("%s \n" %(str))What makes you think so?>>> s = 'l'>>> s += '\n'
>>> s'l\n'>>>Seems to work '\n' is just a character like any other.The issue was to have a newline or  return  character after  every string,  Here it is just iterpreted as \n alphabet., but  not as a return character.
--Alan GauldAuthor of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld--___Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutorEnd of Tutor Digest, Vol 32, Issue 51*
-- Meher KolliSunnyvale,CA-94085
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 32, Issue 51

2006-10-13 Thread Danny Yoo
>> What makes you think so?
>> 
>> >>> s = 'l'
>> >>> s += '\n'
>> >>> s
>> 'l\n'
>> >>>
>> 
>> Seems to work '\n' is just a character like any other.
>
> The issue was to have a newline or return character after every string, 
> Here it is just iterpreted as \n alphabet., but not as a return 
> character.


Hi Meher,

Are you familiar with Python's rules for escaped character literals?

 http://docs.python.org/ref/strings.html

In regular character literals, '\n' is treated as the single newline 
character, and not as the literal sequence of a backslash and 'n'.  We can 
see this concretely:

#
>>> len('\n')
1
#


The backslashing notation is necessary in Python because we often need to 
talk about characters that are hard to type literally.  The escape 
characters provide us a coded way to talk about those characters.

If we did want to talk about the string that contains the "backslash" and 
"n" sequence, we have to be a little subtle:

##
>>> len('\\n')
2
##


Alan and I want to clarify that the way the newlines are being handled in 
the original program looks perfectly fine at the moment; I don't think 
that's the source of the problem.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code for checking whether an input string is a palindrome or not.

2006-10-13 Thread Kent Johnson
Asrarahmed Kadri wrote:
> Yes, you are right; the code isnt working for 'abcdba';
> So what should I do to rectify it.

Try walking through the code by hand so you understand why it is 
failing. Then maybe you can figure out how to fix it. It's important to 
learn to find and fix errors yourself.

Kent

PS Please reply on the list.

>  
> Thanks,
> Asrar
> 
>  
> On 10/13/06, *Kent Johnson* <[EMAIL PROTECTED] > 
> wrote:
> 
> Asrarahmed Kadri wrote:
>  >
>  > Hi,
>  >
>  > Here is a code that reports whether the string is a palindrome or
> not. I
>  > have posted it to help people new to programming ( i am also a
> newbie)
>  > to understand the logic and comment on it.
>  >
>  > Share and expand your knowledge as well as understanding...
>  >
>  > Happy scripting
>  >
>  > Regards,
>  > Asrar
>  >
> 
> Others have pointed out that there are easier ways to do this. I'll
> comment on your code.
> 
>  > Code:
>  >
>  >
>  >
>  > string1 = raw_input("enter a string\n")
>  >
>  > str_len = len(string1)
>  >
>  >
>  >
>  > flag = 0
>  > j = str_len-1
>  >
>  > for i in range(0,(str_len-1)/2):
> 
> I think there is an off-by-one error here; try 'abcdba' as a test.
> 
>  >  if string1[i] == string1[j]:
> 
> Negative indices would work well here, you don't need j at all:
>   if string1[i] == string1[-i]:
> 
> This would also be a good place to use for: / else: but that is a bit
> more advanced.
> 
> Kent
> 
>  >   j = j-1
>  >
>  >  else:
>  >   flag = 1
>  >   break
>  > if flag ==0:
>  >  print "The entered string is a PALINDROME"
>  > else:
>  >  print "The entered string is not a PALINDROME"
>  >
>  >
>  > --
>  > To HIM you shall return.
>  >
>  >
>  >
> 
>  >
>  > ___
>  > Tutor maillist  -  Tutor@python.org 
>  > http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> 
> 
> -- 
> To HIM you shall return.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to write strings with new line character in a file

2006-10-13 Thread Danny Yoo
> fname = raw_input("Enter the file name to write data to:\t")
>
> fd = open(fname,'a+')
> print fd
> done = 0

Ok, good, that helps a lot: that's exactly what we need to diagnose the 
problem.  There appears to be something funky that happens with 
append-plus mode.  This problem has come up before on the list:

 http://aspn.activestate.com/ASPN/Mail/Message/python-tutor/2961494

In general, any of the '+' modes on a Windows platform is slightly broken. 
See:

 http://mail.python.org/pipermail/tutor/2006-September/049511.html

for approaches that people suggest to avoid "plus" mode issues.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code for checking whether an input string is a palindrome or not.

2006-10-13 Thread Danny Yoo

>> Yes, you are right; the code isnt working for 'abcdba';
>> So what should I do to rectify it.
>
> Try walking through the code by hand so you understand why it is 
> failing. Then maybe you can figure out how to fix it. It's important to 
> learn to find and fix errors yourself.

Hi Asrar,

On a slight tangent: you have a bunch of code that takes input from the 
user, and then checks it for palindromeness.  If you have a 
palindrome-checking function that eats strings, then you can do tests 
without pestering the user.

So my suggestion is to try to take your existing code and make a function 
called 'is_palindrome' (or if you want, something shorter like pali()). 
The key is that once you have a function, you should be able to write 
small tests like:

 def pali(word):
 """Returns True if word is palindromic."""
 ## ... fill me in

 ## Test cases:
 assert (pali("aa") == True)
 assert (pali("abcdba") == False)

Once you have something like this, then you can run all tests together 
every time you change the palindrome function.  As you've experienced, the 
problem is subtle enough that there's going to be a few edge cases.  You 
want to make it easy to make sure all the things that worked before 
continue to work.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] executing with double click on linux

2006-10-13 Thread Alfonso
Jonathon Sisson escribió:
> Alfonso wrote:
>   
>> Sorry for the too obvious question. I'm new to python and have no idea 
>> how can I make execute a compiled .pyc with a double click in linux, 
>> with gnome. Trying to double click in a .py gives allways the question 
>> wether I want to execute the text file, or read it. (This behaviour can 
>> be changed with gconf-editor, but as it is for security matters, I would 
>> prefer to execute the .pyc with a double click). I have tried to 
>> associate python to the .pyc (first time I executed it there was no 
>> programm associated), but it doesn't work.
>>
>> 
>
> I don't know how much Linux experience you have (judging by the
> "double-click" concept, I'm assuming you're coming from a Windows
> background or are perhaps catering to users with only a Windows
> background) (correct me if I'm wrong)...so I'm going to break this down
> as much as I can.
>
> .pyc files are considered binary files by Linux.  As such, bash attempts
> to execute them as binary (ELF, etc...), which obviously won't work.  If
> you really need to have double-click/execute functionality, consider
> writing a small shell script to execute the .pyc file for you.
>
> For instance, let's say you have a python script "foo.py" and a compiled
> python script "foo.pyc".  If you attempt to run foo.py from the shell
> and you have a proper header (i.e. #!/usr/bin/python), then bash can
> execute the script.  I'm assuming that GNOME has similar functionality
> (I prefer Fluxbox to either GNOME or KDE), which allows your .py files
> to execute directly.  .pyc, however, even with a file association, fails
> to launch...on my system, I get this error:
>
> $ ./foo.pyc
> bash: ./foo.pyc: cannot execute binary file
>
> bash recognizes the file as binary, but it fails to launch as an ELF
> binary (or whatever you're set up to run).  To fix it, simply write a
> shell script as such:
>
> 
> #!/bin/sh
>
> python /home/me/scripts/foo.pyc
>
> 
>
> Name the script whatever you want (i.e. foo.sh) then run from the
> commandline:
>
> $ chmod 700 foo.sh
>
> This gives the script read/write/execute permissions for the owner of
> the script...if you require read/write/execute/etc...for group or all,
> change 700 to whatever you need.  (i.e. 755 for rwxr-xr-x permissions)
>
> (Alternatively you can right click on the shell script and set
> permissions graphically...whichever you prefer)  Now, you should be able
> to double-click the script (or preferably a shortcut to the script on
> your desktop), which will launch the compiled python module for you.  If
> this seems like a lot of work, then perhaps you could write a shell
> script to automate the task of creating the shell script and setting
> permissions each time you create a new .pyc...doh?
>
> Hope this is a satisfactory answer...and if anyone knows something I
> have overlooked, please let Alfonso and I know.
>
> Jonathon
>
>
>   
>> Thank you for your answer.
>>
>>  
>> __ 
>> LLama Gratis a cualquier PC del Mundo. 
>> Llamadas a fijos y móviles desde 1 céntimo por minuto. 
>> http://es.voice.yahoo.com
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>> 
>
>   
Thank you very much for your answer.

Both true, coming from windows, three years with linux. I don't have any 
fear to executing from the shell, but It's not the same thing with some 
of my friends, totally new to linux, and not very much friends of the 
console. I would like to distribute to them my python programms (when I 
have the knolowdge to writte something decent with python :) ).

 If I use a bash script I have the same "problem" that with executing 
the .py. Gnome asks wether you want to execute or read it, etc. This is 
due to security reasons, and can be disabled using the tool 
gconf-editor, you can change that behaviour so that the files are 
allways executed or opened with a text editor. But I didn't want to 
change that, and I thought this could be easily avoided just executing 
the .pyc files, because I have programmed with mono (.net) in linux and 
that works so. I mean, if I'm not mistaked .pyc are compiled in 
intermediate language files, just like .exe in .net/mono. With mono all 
I have to do was chmod at the .exe, then changing the programm with 
which the .exe is opened (to mono) and that works fine for me, it can be 
opened with a double click. But it's not an elf binary, I think...

I think there must be a way to execute with a launcher the programm 
without being asked each time. As it is for example with bittornado or 
bittorrent, both written in python and both are executed from the 
applications menu or from a launcher at the desktop with no intermediate 
question about if you want to show or execute the file, or just to 
cancel the action. In fact, when I execute bittornado it's a file called 
btdownoladgui.bitto

Re: [Tutor] executing with double click on linux

2006-10-13 Thread johnf
On Friday 13 October 2006 12:33, Alfonso wrote:
Could be wrong but can't you use file associations?  I do on my SUSE system.

John
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to write strings with new line character in a file

2006-10-13 Thread Bob Gailer
Asrarahmed Kadri wrote:
> Here is the complete code:
> fd is the file handle.
>  
> import sys
>
>  def check_dup(fd1):
> print fd1
> fd1.seek(0,0)
> done = 0
> list1 = []
> while not done:
> x = fd1.readline()
> if x == "":
> done = 1
> else:
> list1.append(x)
> return list1
>
>
> fname = raw_input("Enter the file name to write data to:\t")
>
> fd = open(fname,'a+')
> print fd
> done = 0
>
> while not done:
> str = raw_input("Enter login name:\t to quit type 'q'")
>
> if str == 'q':
> done = 1
> else:
> flag = check_dup(fd)
> print flag
> if str in flag:
> print "Login already exists.!!"
> else:
> fd.seek(0,2)
> fd.write(str + '\n')
>
Thank you. I can't get this to fail, so I wonder whether it has to do 
with permissions? What OS are you running on?

Also note when you open a file for output (append or write) it is 
inadvisable to change the file position or to read it ( as you are doing).

As Kent points out there are better ways to do what you are doing. My 
(minimalist?) version is:

fname = raw_input("Enter the file name to write data to:\t")
fd = open(fname,'a+')
names = set()
while 1:
name = raw_input("Enter login name:\t to quit type 'q'")
if name == 'q':   break
if name in names: print "Login already exists.!!"
names.add(name)
fname.write('\n'.join(names))


-- 
Bob Gailer
510-978-4454

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to write strings with new line character in a file OOPS CORRECTION

2006-10-13 Thread Bob Gailer
Bob Gailer wrote:
> Asrarahmed Kadri wrote:
>   
>> Here is the complete code:
>> fd is the file handle.
>>  
>> import sys
>>
>>  def check_dup(fd1):
>> print fd1
>> fd1.seek(0,0)
>> done = 0
>> list1 = []
>> while not done:
>> x = fd1.readline()
>> if x == "":
>> done = 1
>> else:
>> list1.append(x)
>> return list1
>>
>>
>> fname = raw_input("Enter the file name to write data to:\t")
>>
>> fd = open(fname,'a+')
>> print fd
>> done = 0
>>
>> while not done:
>> str = raw_input("Enter login name:\t to quit type 'q'")
>>
>> if str == 'q':
>> done = 1
>> else:
>> flag = check_dup(fd)
>> print flag
>> if str in flag:
>> print "Login already exists.!!"
>> else:
>> fd.seek(0,2)
>> fd.write(str + '\n')
>>
>> 
> Thank you. I can't get this to fail, so I wonder whether it has to do 
> with permissions? What OS are you running on?
>
> Also note when you open a file for output (append or write) it is 
> inadvisable to change the file position or to read it ( as you are doing).
>
> As Kent points out there are better ways to do what you are doing. My 
> (minimalist?) version is:
>
> fname = raw_input("Enter the file name to write data to:\t")
> fd = open(fname,'a+')
> names = set()
> while 1:
> name = raw_input("Enter login name:\t to quit type 'q'")
> if name == 'q':   break
> if name in names: print "Login already exists.!!"
> names.add(name)
> fname.write('\n'.join(names))  OOPS
>   
Should be fd.write('\n'.join(names))
>
>   


-- 
Bob Gailer
510-978-4454

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Zipfile and File manipulation questions.

2006-10-13 Thread Chris Hengge
First question..This is the code that I have:for filename in zfile.namelist():    outfile = open(filename, 'w')    outfile.write(zfile.read(filename))    outfile.close()Is there a way to say :
for filename in zfile.namelist() contains '.txt, .exe':    outfile = open(filename, 'w')    outfile.write(zfile.read(filename))    outfile.close()second question is along the same lines.. 
I'm looking for a way to say:os.remove('All by .py')or-os.remove('*.txt, *.exe')Thank you all again for your time and effort. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Zipfile and File manipulation questions.

2006-10-13 Thread Danny Yoo
> Is there a way to say :
> for filename in zfile.namelist() contains '.txt, .exe':

Hi Chris,

Yes.  It sounds like you want to filter zfile.namelist() and restrict the 
entries to those with particular extensions.  Try a "list comprehension" 
to filter for those interesting filenames:

   for filename in [f for f in zfile.namelist()
if os.path.splitext(f)[1] in ('.txt', '.exe')]:

This may be a little dense and hard to read.  So you can always break this 
out into a function:

#
def keep_txt_exe_filenames(file_list):
 """keep_txt_exe_filenames: list of strings -> list of strings
 Returns a list of all filenames that end in .txt or .exe."""
 return [f for f in file_list
if os.path.splitext(f)[1] in ('.txt', '.exe')]
#

At least that tight expression is documented and can be treated as a black 
box.  But after you have this function, you can use that helper function:

for filename in keep_txt_exe_filenames(zfile.namelist()):
...

which is usually fairly easy for a person to understand.


> os.remove('*.txt, *.exe')

One way to approach this is with loops.  You might find the glob module 
useful here:

 http://docs.python.org/lib/module-glob.html

It'll help to match the file names for each glob pattern.  Again, if you 
find yourself doing this a bit, make a helper function to make it easier 
to say next time.  *grin*


Good luck!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tkinter tutorials

2006-10-13 Thread Dick Moores
At 05:45 PM 10/12/2006, Danny Yoo wrote:
>John Grayson's "Python and Tkinter Programming" is an excellent book on
>learning the Tkinter toolkit:
>
>  http://www.manning.com/grayson/

Danny, "Python and Tkinter Programming" is still in its first 
edition, which came out in 2000. Python has changed a lot since then. 
Hasn't Tkinter? Is the book still usable by a Tkinter beginner?

Dick Moores



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] A puzzle for you

2006-10-13 Thread John Fouhy
>From the python_dev LiveJournal community ---

Predict the outcome of the following code:

##
from random import *
seed()
[choice(dict((x+1,0) for x in range(1000))) for x in range(693)][0]
##

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor