[Tutor] Need help in making this code more pythonic

2009-07-26 Thread ammar azif
Hello,

I need some suggestions on how to make this code pythonic. The program 
basically runs a simulation of an object that moves on a 2D plane. The object 
keeps on moving from its current coordinate to  randomly picked adjacent 
coordinates with the object is not allowed to move move to a previously covered 
coordinate point as the rule. If the object is unable to move the program will 
terminate. Some of the concerns I have in mind is the code in the move method 
in Person class. I am sure there are some built-in methods or features in 
python that I could use to improve the implementation of that method. Hope you 
guys can enlighten me.





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


Re: [Tutor] Need help in making this code more pythonic

2009-07-26 Thread ammar azif
Sorry I forgot to attach the code file.
Here it is.


--- On Sun, 7/26/09, ammar azif ceasar...@yahoo.com wrote:

From: ammar azif ceasar...@yahoo.com
Subject: [Tutor] Need help in making this code more pythonic
To: tutor@python.org
Date: Sunday, July 26, 2009, 6:23 AM

Hello,

I need some suggestions on how to make this code pythonic. The program 
basically runs a simulation of an object that moves on a 2D plane. The object 
keeps on moving from its current coordinate to  randomly picked adjacent 
coordinates with the object is not allowed to move move to a previously covered 
coordinate point as the rule. If the object is unable to move the program will 
terminate. Some of the concerns I have in mind is the code in the move method 
in Person class. I am sure there are some built-in methods or features in 
python that I could use to improve the implementation of that method. Hope you 
guys can enlighten me.





  
-Inline Attachment Follows-

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



  import random
import time
class Person(object):
def __init__(self, position,length,width):
self.positions = {position:None} #coordinates covered 
self.position = position #current coordinate
self.length = length # world length
self.width= width #world width


def move(self):
x = self.position[0]
y = self.position[1]
#set dead end 
cannot_move = True
#list of potential next coordinates
potential_positions =[]
if x+1=0 and x+1=self.length-1:
if not self.positions.has_key((x+1,y)):
cannot_move = False
potential_positions.append((x+1,y))
   
if x-1=0 and x-1=self.length-1:
if not self.positions.has_key((x-1,y)):
cannot_move = False
potential_positions.append((x-1,y))
 
if y-1=0 and y-1=self.width-1:
if not self.positions.has_key((x,y-1)):
cannot_move = False
potential_positions.append((x,y-1))
 
if y+1=0 and y+1=self.width-1:
if not self.positions.has_key((x,y+1)):
cannot_move = False
potential_positions.append((x,y+1))


if cannot_move :
#return value that signifies the person cannot move
return False
else:
#randomly pick a coordinate from list of potential coordinates
index=random.randrange(len(potential_positions))
self.positions[potential_positions[index]]=None
self.position = potential_positions[index]
return True


width = 10
length = 10
person = Person((0,0),length,width)
plots = {(0,0):None}
print 'Initial Positions Covered Count:'+str(len(person.positions))
print 'Moving!'


while person.move():
string = 'Current position:' + str(person.position)+'Total covered:'+str(len(person.positions))+'\n'

for x in range(width):
for y in range(length):

if person.positions.has_key((x,y)):
string+='X'
else:
string+=' '
if y==length-1:
string+='\n'
print string

time.sleep(1)

print 'Cannot move!'
print person.positions












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


[Tutor] Puzzled

2008-08-29 Thread ammar azif
I wrote a python program that used time() function from the time module to 
retrieve time in seconds since Epoch. After the value was retrieved which I 
checked is a float by using type(),  the value was then written into a file in 
binary format. Then another C program that I wrote opened the file and 
converted the value into a time_t variable but it was totally different from 
the correct value. Then I found that the time_t size is actually 4 byte integer 
which is not the same with 8-byte float value returned by time.time(). Why is 
this so? Being written with C library, isn't python suppose to work well with 
it?



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


Re: [Tutor] Puzzled

2008-08-29 Thread ammar azif
Thanks for the explanation. Btw, How can I get the size of python primitive 
data types in bytes? Is it defined somewhere in a file that I can look at? 

--- On Fri, 8/29/08, Kent Johnson [EMAIL PROTECTED] wrote:
From: Kent Johnson [EMAIL PROTECTED]
Subject: Re: [Tutor] Puzzled
To: [EMAIL PROTECTED]
Cc: tutor@python.org
Date: Friday, August 29, 2008, 4:41 PM

On Fri, Aug 29, 2008 at 5:13 PM, ammar azif [EMAIL PROTECTED] wrote:
 I wrote a python program that used time() function from the time module to
 retrieve time in seconds since Epoch. After the value was retrieved which
I
 checked is a float by using type(),  the value was then written into a
file
 in binary format. Then another C program that I wrote opened the file and
 converted the value into a time_t variable but it was totally different
from
 the correct value. Then I found that the time_t size is actually 4 byte
 integer which is not the same with 8-byte float value returned by
 time.time(). Why is this so? Being written with C library, isn't
python
 suppose to work well with it?

The C time_t type is very loosely specified; in ANSI C it is only
required to be an arithmetic type. According to Wikipedia
(http://en.wikipedia.org/wiki/Time_t), Posix-compliant systems still
have latitude to implement it as 32 or 64 bits.

Python tries to be bit higher level, giving you fractional seconds if
the implementation supports it and a common data type across
implementations. So there is not an exact match in functionality.

If you want to write data to file in a format that can be read by
another program, you should look at the struct module.

Kent



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


[Tutor] My experience on web.py

2008-08-04 Thread ammar azif
Hi,

I am writing this to tell my experience on web.py. Two weeks ago, I was looking 
for a python web framework that is simple, straight-forward, easy to use and 
powerful at the same time. Django  stood out as the most popular when I 
googled. I tried to use django but I found that the framework hides alot of 
things from me and files are generated by the framework  automaticaly and I 
felt like I wasnt in control. I know that django is powerful, but the learning 
curve is too steep for me and  I need to develop my app as soon as possible. I 
decided to give web.py a try and I found that the framework is easy to use and 
it gives a lot of control to the developer when handling GET and POST request 
and all these can be done in a single source code and using this framework has 
taught me a lot of low level web application programming basics. I might be 
wrong as I havent try django or any other frameworks yet. Hope python gurus 
here can share their thoughts on these
 matters,  




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


[Tutor] Launching default web browsers.

2008-07-12 Thread ammar azif
Hi,

I am going to develop an application that will launch the user's computer 
default web browser pointing to a URL. How to do this in python and which 
library should I use? Also, my development platform is Linux but the 
application is targeted to run in Windows platform. Is there any major platform 
dependent barriers that I will face?

Thanks.  



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


[Tutor] Document Control System

2008-01-14 Thread ammar azif
Hello,

Is python suitable in developing web based document control system ? I was 
thinking of using python and mysql to create such system. Users can download 
latest document as well as upload drafts. Is this possible or i should use PHP 
instead?

   
-
Be a better friend, newshound, and know-it-all with Yahoo! Mobile.  Try it now.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] scope/namespaces

2007-04-24 Thread ammar azif
Something in python disturbs me ,

when i write a for loop,

i am able to access the variable declared in that loop after the loop finishes 
which i am not able to do in languages like c/c++ or java. Is it different in 
python?

   
-
Ahhh...imagining that irresistible new car smell?
 Check outnew cars at Yahoo! Autos.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help on raw_input()

2007-04-14 Thread ammar azif
Hi,

i wanted to get a string from raw_input like this

raw_input('')
 \n\nsomestring

but the problem is raw input will return the string
'\\n\\nsomestring'

My question is
Are there any function to convert back those string to  '\n\nsomestring' ?

Thanks


   
-
Ahhh...imagining that irresistible new car smell?
 Check outnew cars at Yahoo! Autos.___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help on raw_input()

2007-04-14 Thread ammar azif
Actually i wanted to write a http client using just he low level socket module.
The program will prompt the user asking for input and the use will type http 
commands like 'GET /index.html HTTP/1.0\r\nHost: somehost\r\n\r\n'
when i use the raw_input function , the string that i get is 
'GET /index.html HTTP/1.0\\r\\nHost: somehost\\r\\n\\r\\n'

is there any easy way other than modify this string ? Perhaps regular 
expression?



Alan Gauld [EMAIL PROTECTED] wrote: ammar azif  wrote

 i wanted to get a string from raw_input like this
 
 raw_input('')
 \n\nsomestring

OK, Can you explain precisely what you want the string to contain.
\n is the string representation of a newline. Do you want to enter 
something that starts with two newlines? Or do you literally want
the sequence \,n,\,n?

If its the latter thats what Python has stored. The double slash 
is only there when python displays the result (Try using len and 
comparing if you aren't convinced)

If you want to actually capture newline characters from raw_input, 
thats more tricky. But before we get to that can you clarify what 
you actually want?

Alan G.

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


   
-
Ahhh...imagining that irresistible new car smell?
 Check outnew cars at Yahoo! Autos.___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help on raw_input()

2007-04-14 Thread ammar azif
Thanks the encode method really helps.

Alan Gauld [EMAIL PROTECTED] wrote: ammar azif  wrote

 Actually i wanted to write a http client using just he low level 
 socket module.

I won;t ask why! But the httplib module probably does what you want
just in case you dodn't realize it existed...

 The program will prompt the user asking for input and the
 use will type http commands like
 'GET /index.html HTTP/1.0\r\nHost: somehost\r\n\r\n'

OK, So you want the user to acrtually type \,n,\,n and you
will then send that string to be interpreted as newlines?

 when i use the raw_input function , the string that i get is
 'GET /index.html HTTP/1.0\\r\\nHost: somehost\\r\\n\\r\\n'

The double \\ doesn''t actually exist its just Python telling you
that it is a literal \ character not an escaped sequence.
As I said earlier if you check the len() of the string it will
only have one character per backslash.

I think it's already doing what you want!
You just need to turn the \n's that the user entered into
newline characters, Kent has shown you how to do that
with the decode() method...

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


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


   
-
Ahhh...imagining that irresistible new car smell?
 Check outnew cars at Yahoo! Autos.___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tkinter-Button class

2007-03-18 Thread ammar azif
Hi,

Thanks for the help guys.


I have tried gui programming using Tkinter and use the Button class which 
accepts the command argument which is a function object.

The question is how to send arguments if the function accepts arguments.





 
-
 Get your own web address.
 Have a HUGE year through Yahoo! Small Business.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Is generator function reusable?

2007-03-17 Thread ammar azif
Lets say a generator function instance is created and has finished its 
iteration.
Can that instance be reused again? can it accept anymore values? or we should 
discard it and create a new generator instance.

Thanks

 
-
Need Mail bonding?
Go to the Yahoo! Mail QA for great tips from Yahoo! Answers users.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Is generator function similar to multi threading?

2007-03-16 Thread ammar azif
Is generator function similar to multi threading?



 
-
Don't be flakey. Get Yahoo! Mail for Mobile and 
always stay connected to friends.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Built-in function to check a character is ASCII character?

2007-03-16 Thread ammar azif
Is there any Built-in function to check whether  a character is an ASCII 
character?

I am trying to learn event driven programming in python. Heres my code:

while 1:
key=msvcrt.getch()
if key==255:
continue
else:
print the key is , key


But the output is:-

the key is  ÿ
the key is  ÿ
the key is  ÿ
the key is  ÿ
the key is  ÿ
the key is  ÿ
the key is  ÿ
the key is  ÿ(infinite loop)

Why is this happening ? what is the  ÿ character?





 
-
Now that's room service! Choose from over 150,000 hotels 
in 45,000 destinations on Yahoo! Travel to find your fit.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor