[Tutor] Random module error

2008-05-15 Thread Guess?!?
Hello All,

I am importing module "random" and it is giving me this error below
AttributeError:
'module' object has no attribute 'randrange'

What could be the reason for this? When I chk API ..random module has a
function randrange ..

>>> from TurtleWorld import *
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python25\swampy.1.1\TurtleWorld.py", line 1, in 
from World import *
  File "C:\Python25\swampy.1.1\World.py", line 29, in 
import random
  File "C:\Python25\random.py", line 4, in 
i = random.randrange(10,50)
AttributeError: 'module' object has no attribute 'randrange'
>>>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] looking for ways to get diff between two txt files

2008-05-15 Thread Kent Johnson
On Thu, May 15, 2008 at 1:58 AM, Kamal <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I would really appreciate if someone help to find a way to calculate
> delta (diff) between two similar text files.

This example would be pretty easy to modify to do what you want:
http://personalpages.tds.net/~kent37/blog/arch_m1_2004_06.html#e47

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


Re: [Tutor] Iterate through dictionary values and remove item

2008-05-15 Thread bob gailer

GTXY20 wrote:

Hello all,

I have dictionary like the following:

d={(1,23A):[a,b,c,d],  (1,24A):[b,c,d], (2,23A):[a,b], (2,24A):[a,b,c,d]}

I would like to iterate through the dictionary such that if it finds
the value 'a' in the values of the key that it would remove the value
'b' from the values list. In addition if it finds 'a' in the values
list I would like it to take the first item from the key tuple k[0]
and and then look through the dictionary for that k[0] value and then
remove 'b' from its value list even if 'a' is not present in that
list. So at the end I would like my dictionary to end with:

d={(1,23A):[a,c,d],  (1,24A):[c,d], (2,23A):[a], (2,24A):[a,c,d]}

Initally I did the following:

for k,v in d.items():
u=k[0]
b=k[1]
if 'a' in v:
for k,v in d.items():
if k[0] == u:
for values in v:
if values == 'b':
v.remove(values)

However this will be a very big dictionary and it ended up running for
a very long time - in fact I had to kill the process.

Can anyone suggest an alternate method to do this?

Thanks in advance for any suggestions.
  


Your code sure looks weird!

d={(1,23A):[a,c,d],  (1,24A):[c,d], (2,23A):[a], (2,24A):[a,c,d]}

That gives a syntax error at the first A.
And the rest of the code lacks indentation.

Your code does not match your specification. You said "... if it finds 
the value 'a' in the values of the key" but you test


if 'a' in v:

Please fix these issues and repost.

--
Bob Gailer
919-636-4239 Chapel Hill, NC

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


Re: [Tutor] Iterate through dictionary values and remove item

2008-05-15 Thread Kent Johnson
On Thu, May 15, 2008 at 2:40 AM, GTXY20 <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> I have dictionary like the following:
>
> d={(1,23A):[a,b,c,d],  (1,24A):[b,c,d], (2,23A):[a,b], (2,24A):[a,b,c,d]}
>
> I would like to iterate through the dictionary such that if it finds
> the value 'a' in the values of the key that it would remove the value
> 'b' from the values list. In addition if it finds 'a' in the values
> list I would like it to take the first item from the key tuple k[0]
> and and then look through the dictionary for that k[0] value and then
> remove 'b' from its value list even if 'a' is not present in that
> list. So at the end I would like my dictionary to end with:
>
> d={(1,23A):[a,c,d],  (1,24A):[c,d], (2,23A):[a], (2,24A):[a,c,d]}
>
> Initally I did the following:
>
> for k,v in d.items():
> u=k[0]
> b=k[1]
> if 'a' in v:
> for k,v in d.items():
> if k[0] == u:
> for values in v:
> if values == 'b':
> v.remove(values)
>
> However this will be a very big dictionary and it ended up running for
> a very long time - in fact I had to kill the process.

The main problem is the nested loops. Every time you find an 'a' you
search the whole dict for items starting with matching keys.  There
are some smaller problems as well - you could use d.iteritems()
instead of d.items() to avoid creating intermediate lists, and there
are better ways to remove 'b' from v.

The first thing I would do is reduce this to two passes over the dict
- the first pass can find the keys, the second to delete 'b' from the
list. For example,
toRemove=set(k[0] for k, v in d.iteritems() if 'b' in v)
for k,v in d.iteritems():
if k[0] in toRemove:
try:
v.remove('b')
except ValueError:
pass

If this is too slow, consider splitting up your keys - make a
two-level dict so you can find all the keys starting with a particular
value by a dict lookup instead of search.

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


Re: [Tutor] Random module error

2008-05-15 Thread Kent Johnson
On Thu, May 15, 2008 at 3:00 AM, Guess?!? <[EMAIL PROTECTED]> wrote:
> Hello All,
>
> I am importing module "random" and it is giving me this error
> below  AttributeError: 'module' object has no attribute 'randrange'

You have named your program random.py. The import finds your program
rather than the random module. Rename your program and delete the
associated .pyc file and it will work.

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


[Tutor] Freeze utility

2008-05-15 Thread Diego Trazzi
Can you recommended any good freeze utility for python to create  linux
standalone executables, other than the freeze.py ? this script doen't seems
to include my .gif images...

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


[Tutor] (no subject)

2008-05-15 Thread root
hi ,i am working to replace three lines in a httpd.conf file using
regular expression.i am able to change only one among them but while
replacing other i am not geeting good idea to replace all the three
lines just in a single script ,,my script is like

 #!usr/bin/python

import sys
import os
import re
import  string

def replace_file(file,search,replace):
cregex=re.compile(search)
for current_line in file:
if cregex.search(current_line):
current_line=re.sub(search,replace,current_line)
print current_line
else:
print current_line


def main():
file =open("/root/Desktop/httpd.conf").readlines()
replace_file(file,'User apache ' , 'User myuser ')
#replace_file(file,'Group apache','Group myuser')
#replace_file(file,DirectoryIndex\ index.html\ 
index.html.var,DirectoryIndex\ index.html\ index.html.var\ login.html)



if __name__ == '__main__':
main()


I am running this command to save the output in a temp file then replacing with 
original httpd.conf.

can I do this in a single python script that'll change three lines and 
there should not any need to replace it.that is the change should
 directly save to the source file..??

any help will be appreciated.Thanks


Please do not print this email unless it is absolutely necessary. 

The information contained in this electronic message and any attachments to 
this message are intended for the exclusive use of the addressee(s) and may 
contain proprietary, confidential or privileged information. If you are not the 
intended recipient, you should not disseminate, distribute or copy this e-mail. 
Please notify the sender immediately and destroy all copies of this message and 
any attachments. 

WARNING: Computer viruses can be transmitted via email. The recipient should 
check this email and any attachments for the presence of viruses. The company 
accepts no liability for any damage caused by any virus transmitted by this 
email. 

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


Re: [Tutor] (no subject)

2008-05-15 Thread Kent Johnson
On Thu, May 15, 2008 at 11:22 AM, root <[EMAIL PROTECTED]> wrote:
> hi ,i am working to replace three lines in a httpd.conf file using
>  regular expression

You don't need regex for this, you are replacing plain strings. I
would read the entire file, change the strings and write it out again.
For example,

path = "/root/Desktop/httpd.conf"
f = open(path)
data = f.read()
f.close()
for search, replace in [
   ('User apache ' , 'User myuser '),
   ('Group apache','Group myuser'),
   ('DirectoryIndex\ index.html\ index.html.var','DirectoryIndex\
index.html\ index.html.var\ login.html')
]:
data.replace(search, replace)

f = open(path, 'w')
f.write(data)
f.close()

This replaces the file in place, if you want to make a backup it will
be a little more complex.

A few other notes:
- don't use 'file' as a variable name, it shadows the builtin file() function
- I'm not sure what the \ in your search string are for but they are
interpreted as escape characters. If you want a literal \ in the
string you have to use two \\ or prefix the string with r to make a
raw string.

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


Re: [Tutor] Iterate through dictionary values and remove item

2008-05-15 Thread GTXY20
Hi Kent and Bob,

Bob sorry about the previous code snippet (was late) - I had previously been
trying to accomplish with the following:

for k,v in d.items():
u=k[0]
b=k[1]
if 'a' in v:
for k,v in d.items():
if k[0] == u:
for vals in v:
if vals == 'b':
v.remove('b')

this as mentioned was not performing very well at all.

Kent, I incorporated your suggestion:

toRemove=set(k[0] for k, v in d.iteritems() if 'b' in v)
for k,v in d.iteritems():
   if k[0] in toRemove:
   try:
   v.remove('b')
   except ValueError:
   pass

and the speed and result improved very dramatically. I suspect that I need
to get a better handle on the difference between items() and iteritems() and
what situations would call for them respectively.

Having said that, Kent I am not 100 percent sure of what you menat when you
mention a two-level dict. Can you give me a very brief example?

Thank you so much for your feedback.

G.

On Thu, May 15, 2008 at 7:57 AM, Kent Johnson <[EMAIL PROTECTED]> wrote:

>  On Thu, May 15, 2008 at 2:40 AM, GTXY20 <[EMAIL PROTECTED]> wrote:
> > Hello all,
> >
> > I have dictionary like the following:
> >
> > d={(1,23A):[a,b,c,d],  (1,24A):[b,c,d], (2,23A):[a,b], (2,24A):[a,b,c,d]}
> >
> > I would like to iterate through the dictionary such that if it finds
> > the value 'a' in the values of the key that it would remove the value
> > 'b' from the values list. In addition if it finds 'a' in the values
> > list I would like it to take the first item from the key tuple k[0]
> > and and then look through the dictionary for that k[0] value and then
> > remove 'b' from its value list even if 'a' is not present in that
> > list. So at the end I would like my dictionary to end with:
> >
> > d={(1,23A):[a,c,d],  (1,24A):[c,d], (2,23A):[a], (2,24A):[a,c,d]}
> >
> > Initally I did the following:
> >
> > for k,v in d.items():
> > u=k[0]
> > b=k[1]
> > if 'a' in v:
> > for k,v in d.items():
> > if k[0] == u:
> > for values in v:
> > if values == 'b':
> > v.remove(values)
> >
> > However this will be a very big dictionary and it ended up running for
> > a very long time - in fact I had to kill the process.
>
> The main problem is the nested loops. Every time you find an 'a' you
> search the whole dict for items starting with matching keys.  There
> are some smaller problems as well - you could use d.iteritems()
> instead of d.items() to avoid creating intermediate lists, and there
> are better ways to remove 'b' from v.
>
> The first thing I would do is reduce this to two passes over the dict
> - the first pass can find the keys, the second to delete 'b' from the
> list. For example,
> toRemove=set(k[0] for k, v in d.iteritems() if 'b' in v)
> for k,v in d.iteritems():
>if k[0] in toRemove:
>try:
>v.remove('b')
>except ValueError:
>pass
>
> If this is too slow, consider splitting up your keys - make a
> two-level dict so you can find all the keys starting with a particular
> value by a dict lookup instead of search.
>
> Kent
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate through dictionary values and remove item

2008-05-15 Thread Kent Johnson
On Thu, May 15, 2008 at 12:58 PM, GTXY20 <[EMAIL PROTECTED]> wrote:
> I suspect that I need
> to get a better handle on the difference between items() and iteritems() and
> what situations would call for them respectively.

items() returns a list, iteritems() returns an iterator. If you don't
actually need an explicit list, iteritems() saves the cost of creating
it.

> Having said that, Kent I am not 100 percent sure of what you menat when you
> mention a two-level dict. Can you give me a very brief example?

Your dict d would look like this:
d={
1: {23A:[a,b,c,d],  24A:[b,c,d]},
2: {23A:[a,b], 24A:[a,b,c,d]}
  }

The top level would just use the first element of the current key; the
value would be another dict whose keys are the second element of the
current key. This makes it easy to find all the values whose first key
element is '1', for example. (You could also have the second element
be a list of tuples, that might be better if you just need to iterate
over it.)

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


[Tutor] Open a directory in the default file manager

2008-05-15 Thread Tim Michelsen

Hello,
is there any function/module that allows me to open a directory in the 
default file manager of a operating system?


Here I a piece of code how to open a URL in the default webbrowser:

http://www.python.org/doc/lib/module-webbrowser.html

import webbrowser
myurl = 'http://www.python.org'
webbrowser.open_new_tab(myurl)


I just wanna do the same with a directory.

The function should open Explorer on Windows, Nautilus on Gnome, Thunar 
onn XFCE4, ...


Thanks for your help in advance.

Regards,
Timmie

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


[Tutor] Guido van Robot help

2008-05-15 Thread Gillian
Hi,

Would anyone be able to offer assistance re: using
Guido van Robot to perform pattern recognition (as in
it "sees" a particular pattern (like a 3x5 grid
depiction of the letter "A") and responds
accordingly)? In my example, Guido recognizes a
specific letter by placing down an appropriate amount
of beepers. A = 1 beeper, B = 2, etc.

I'm able to create separate definitions to recognize
specific letters, but my difficulty arises when I try
to make a program so robust that it should be able to
recognize any letter of the alphabet no matter what
order they would be in.

Thanks,

Jill


  __
Looking for the perfect gift? Give the gift of Flickr! 

http://www.flickr.com/gift/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Random module error

2008-05-15 Thread Guess?!?
Hello Kent,

I dont have any program with name random.py. I am using swampy API (
http://allendowney.com/swampy/install.html)
As instructed by author, I do following steps

1> unzip/extract the folder into Python directory
2> go to the swampy directory
3> and invoke python command to start interpreter
4> as soon as I write from TurtleWorld import *

I get that error . I checked inside the folder ...there is no random.py
and
i checked TurtleWorld.py there is only reference to standard module
"random"

Thnks
G





On Thu, May 15, 2008 at 5:00 AM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> On Thu, May 15, 2008 at 3:00 AM, Guess?!? <[EMAIL PROTECTED]> wrote:
> > Hello All,
> >
> > I am importing module "random" and it is giving me this error
> > below  AttributeError: 'module' object has no attribute 'randrange'
>
> You have named your program random.py. The import finds your program
> rather than the random module. Rename your program and delete the
> associated .pyc file and it will work.
>
> Kent
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Random module error

2008-05-15 Thread christopher . henk
Your original post showed this traceback:
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python25\swampy.1.1\TurtleWorld.py", line 1, in 
from World import *
  File "C:\Python25\swampy.1.1\World.py", line 29, in 
import random
  File "C:\Python25\random.py", line 4, in 
i = random.randrange(10,50)
AttributeError: 'module' object has no attribute 'randrange'

Which would suggest that the file World.py imported a file called 
random.py from the directory  C:\Python25\random.py
The random module that comes with python is typically stored in  the 
directory C:\Python25\Lib
and never contains the line:  i = random.randrange(10,50)

So I would look for a .py or .pyc file in your Python25 folder called 
random and remove it like Kent said.

Hope that works.

Chris 




"Guess?!?" <[EMAIL PROTECTED]> 
Sent by: [EMAIL PROTECTED]
05/15/2008 03:48 PM

To
"Kent Johnson" <[EMAIL PROTECTED]>
cc
tutor@python.org
Subject
Re: [Tutor] Random module error






Hello Kent,
 
I dont have any program with name random.py. I am using swampy API (
http://allendowney.com/swampy/install.html)
As instructed by author, I do following steps
 
1> unzip/extract the folder into Python directory
2> go to the swampy directory
3> and invoke python command to start interpreter
4> as soon as I write from TurtleWorld import *
 
I get that error . I checked inside the folder ...there is no 
random.py and 
i checked TurtleWorld.py there is only reference to standard module 
"random"
 
Thnks
G
 
 


 
On Thu, May 15, 2008 at 5:00 AM, Kent Johnson <[EMAIL PROTECTED]> wrote:
On Thu, May 15, 2008 at 3:00 AM, Guess?!? <[EMAIL PROTECTED]> wrote:
> Hello All,
>
> I am importing module "random" and it is giving me this error
> below  AttributeError: 'module' object has no attribute 'randrange'

You have named your program random.py. The import finds your program
rather than the random module. Rename your program and delete the
associated .pyc file and it will work.

Kent
___
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] Random module error

2008-05-15 Thread Guess?!?
Got it fixed ...Thanks Kent and Chris. I might have created a prog and named
it random and later deleted it but pyc was still sitting at the bottom I
purged it and I am back on learning curve  ...

~G

On Thu, May 15, 2008 at 1:18 PM, <[EMAIL PROTECTED]>
wrote:

>
> Your original post showed this traceback:
> Traceback (most recent call last):
>  File "", line 1, in 
>  File "C:\Python25\swampy.1.1\TurtleWorld.py", line 1, in 
>from World import *
>  File "C:\Python25\swampy.1.1\World.py", line 29, in 
>import random
>  File "C:\Python25\random.py", line 4, in 
>i = random.randrange(10,50)
> AttributeError: 'module' object has no attribute 'randrange'
>
>
> Which would suggest that the file World.py imported a file called random.py
> from the directory  C:\Python25\random.py
> The random module that comes with python is typically stored in  the
> directory C:\Python25\Lib
> and never contains the line:  i = random.randrange(10,50)
>
> So I would look for a .py or .pyc file in your Python25 folder called
> random and remove it like Kent said.
>
> Hope that works.
>
> Chris
>
>
>
>   *"Guess?!?" <[EMAIL PROTECTED]>*
> Sent by: [EMAIL PROTECTED]
>
> 05/15/2008 03:48 PM
>To
>  "Kent Johnson" <[EMAIL PROTECTED]>
>  cc
> tutor@python.org  Subject
> Re: [Tutor] Random module error
>
>
>
>
> Hello Kent,
>
> I dont have any program with name random.py. I am using swampy API (*
> http://allendowney.com/swampy/install.html*
> )
> As instructed by author, I do following steps
>
> 1> unzip/extract the folder into Python directory
> 2> go to the swampy directory
> 3> and invoke python command to start interpreter
> 4> as soon as I write from TurtleWorld import *
>
> I get that error . I checked inside the folder ...there is no random.py
> and
> i checked TurtleWorld.py there is only reference to standard module
> "random"
>
> Thnks
> G
>
>
>
>
>
> On Thu, May 15, 2008 at 5:00 AM, Kent Johnson <[EMAIL PROTECTED]<[EMAIL 
> PROTECTED]>>
> wrote:
> On Thu, May 15, 2008 at 3:00 AM, Guess?!? <[EMAIL PROTECTED]<[EMAIL 
> PROTECTED]>>
> wrote:
> > Hello All,
> >
> > I am importing module "random" and it is giving me this error
> > below  AttributeError: 'module' object has no attribute 'randrange'
>
> You have named your program random.py. The import finds your program
> rather than the random module. Rename your program and delete the
> associated .pyc file and it will work.
>
> Kent
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor