Tiago Katcipis | 17 May 20:16
Picon

Best way to define comparison

Im writing a class on python and i want to implement the == and != operators on it. I have read about __cmp__ and about __eq__ for == and __ne__ for ! =. My question is... who is the better to use? and if there is no better what are the advantages and disvantages of them. Some articles talk about using cmp, and others about the eq...its a little confusing :-(


_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Che M | 17 May 06:07
Picon
Favicon

datetime syntax error for May 8th and 9th 2008??

Am I missing something?  Why does May 7th
and other dates work but I'm getting "invalid
token" for May 8th and 9th?  (I have not tested
many other dates of the year in this way) This
is from a freshly begun IDLE session:

IDLE 1.2     
>>> import datetime
>>> datetime.datetime(2008,05,07)
datetime.datetime(2008, 5, 7, 0, 0)
>>> datetime.datetime(2008, 05, 08)
SyntaxError: invalid token
>>> datetime.datetime(2008, 05, 09)
SyntaxError: invalid token

Is this a glitch in the Matrix?

Che

E-mail for the greater good. Join the i’m Initiative from Microsoft.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Adam Clarridge | 17 May 00:56
Picon

Dictionaries or Numpy?

Hi, I'm writing an AI for a board game called Blokus, and I believe
that the quality of the program is going to greatly depend on the
efficiency of the algorithm I use to generate legal moves and rate
them.

I know sort of how I'm going to do this legal move generation, and I
know it is going to require lots of accesses to an array of some kind
which will represent the game board.

I'm fairly new to Python, and I am not sure whether it would be faster
for me to use a Dictionary data type to represent the board
(advantages: indices can be strings or tuples, and the dictionary can
store different types of data as well, so programming would be easier)
or the Numpy array type (don't know much about this, but I have heard
Numpy is faster).

So is Numpy faster for simple array accesses? That would be valuable
and I'd deal with having an integer-indexed integer array if I had to.

Code might look something like this:

for playable_corner in all_playable_corners:
    for piece in all_available_pieces:
        for each_square in piece:
            # if square is not in a legal position, don't add to list
of legal moves

So that if statement would be accessing the board array many many
times. I guess my question is: Is there any way to test Dictionary
access time vs. Numpy array access time?

Thanks,

Adam
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Joel Miller | 16 May 23:39
Picon

quickly pulling marbles out of urns

I'm looking for a faster way to do the following problem:

I have an urn with many different colors of marbles in it.  I pull one
out and note the color.  I do not replace it.

For the programming of this, I actually know how many are yellow,
green, etc.  So the way the code works right now is:

import math
...

randindex = math.randint(1,number_of_marbles)
for color in colors:
   if randindex<=marble_count[color]: #we've found what color it will be
       break
   else:                                          #try next color
       randindex -= marble_count[color]
marble_count[color] -= 1
number_of_marbles -= 1
return color

Unfortunately, I have hundreds of thousands of colors, so it spends a
while on this loop.  And I have to keep choosing marbles many times at
different points of the code.

Is there a quicker way to do this?

Thanks,
Joel
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Moishy Gluck | 16 May 17:20
Picon

Re: Getting started with Python



On Fri, May 16, 2008 at 11:20 AM, Moishy Gluck <moishyyehuda <at> gmail.com> wrote:
You need to place a "__init__.py" file in a directory you want to reference in an import statement.

I don't believe the content of the file is important but you can place code in the file that will affect how files are imported.

The import syntax is a such.Place "__init__.py" in both "directory1" and "directory2"

Good luck simon.


On Fri, May 16, 2008 at 8:16 AM, ppaarrkk <simon_ecc <at> yahoo.co.uk> wrote:

I can't.


>>> import file.py


is all very well if the interpreter knows where file.py is.


I want to do this :

>>> import /directory1/directory2/file.py


Is this not possible ?

--
View this message in context: http://www.nabble.com/Getting-started-with-Python-tp17273337p17273337.html
Sent from the Python - tutor mailing list archive at Nabble.com.

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
ppaarrkk | 16 May 14:16
Picon

Getting started with Python


I can't.

>>> import file.py

is all very well if the interpreter knows where file.py is.

I want to do this :

>>> import /directory1/directory2/file.py

Is this not possible ?

--

-- 
View this message in context: http://www.nabble.com/Getting-started-with-Python-tp17273337p17273337.html
Sent from the Python - tutor mailing list archive at Nabble.com.

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Gillian | 15 May 19:46
Picon
Favicon

Guido van Robot help

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 <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Tim Michelsen | 15 May 20:08
Picon

Open a directory in the default file manager

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 <at> python.org
http://mail.python.org/mailman/listinfo/tutor

root | 15 May 17:21

(no subject)

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 <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Diego Trazzi | 15 May 14:09
Picon

Freeze utility

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 <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Guess?!? | 15 May 08:58
Picon

Random module error

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 "<stdin>", line 1, in <module>
  File "C:\Python25\swampy.1.1\TurtleWorld.py", line 1, in <module>
    from World import *
  File "C:\Python25\swampy.1.1\World.py", line 29, in <module>
    import random
  File "C:\Python25\random.py", line 4, in <module>
    i = random.randrange(10,50)
AttributeError: 'module' object has no attribute 'randrange'
>>>
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Gmane