[Tutor] names and variables

2008-08-20 Thread Jim Morcombe

I have an object called "myObject" with an attribute called "colour".

If  I type:
   print myObject.colour
then python prints:
red

Now, I have a variable "myAttribute" and set its value to "colour" by 
typing:

   myAttribute = "colour"

I want to have a line of code:  something like this:
   print myObject.myAttribute
and have Python interpret it as meaning "print myObject.colour" and 
hence print "red"



Can this be done?  If so, how?

Jim Morcombe



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


Re: [Tutor] names and variables

2008-08-20 Thread Marc Tompkins
On Wed, Aug 20, 2008 at 2:30 AM, Jim Morcombe <[EMAIL PROTECTED]>wrote:

> I have an object called "myObject" with an attribute called "colour".
>
> If  I type:
>   print myObject.colour
> then python prints:
>red
>
> Now, I have a variable "myAttribute" and set its value to "colour" by
> typing:
>   myAttribute = "colour"
>
> I want to have a line of code:  something like this:
>   print myObject.myAttribute
> and have Python interpret it as meaning "print myObject.colour" and hence
> print "red"
>
>
> Can this be done?  If so, how?
>
> Jim Morcombe
>

print(getattr(myObject, "colour"))

Here's the effbot article:  http://effbot.org/zone/python-getattr.htm

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


Re: [Tutor] names and variables

2008-08-20 Thread Lie Ryan
On Wed, 2008-08-20 at 11:37 +0200, [EMAIL PROTECTED] wrote:
> Message: 7
> Date: Wed, 20 Aug 2008 17:30:37 +0800
> From: Jim Morcombe <[EMAIL PROTECTED]>
> Subject: [Tutor] names and variables
> To: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
> I have an object called "myObject" with an attribute called "colour".
> 
> If  I type:
> print myObject.colour
> then python prints:
>  red
> 
> Now, I have a variable "myAttribute" and set its value to "colour" by 
> typing:
> myAttribute = "colour"
> 
> I want to have a line of code:  something like this:
> print myObject.myAttribute
> and have Python interpret it as meaning "print myObject.colour" and 
> hence print "red"
> 
> 
> Can this be done?  If so, how?
> 

Do you mean this:

>>> class A(object): pass
... 
>>> class A(object):
... colour = 'red'
... 
>>> a = A()
>>> def getcolour(self):
... print self.colour
>>> A.myAttribute = getcolour
>>> a.myAttribute()
'red'

Of course, this is why python's is called dynamic language: the ability
to change a class' definition on runtime, even on an already
instantiated objects. 

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


Re: [Tutor] names and variables

2008-08-20 Thread Kent Johnson
On Wed, Aug 20, 2008 at 5:37 AM, Marc Tompkins <[EMAIL PROTECTED]> wrote:

> print(getattr(myObject, "colour"))

Or, of course,
print getattr(myObject, myAttribute) which is what the OP wanted.

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


Re: [Tutor] Is this a "Class" problem?

2008-08-20 Thread Jeff Younker
I'm assuming you do that so that you don't confuse what a Python  
built-in method (or function) is,

compared to methods (or functions) that you've authored.


It's done more to distinguish classes from methods and attributes.  I  
can't claim
any credit though.  It's part of the python coding conventions in  
PEP-8.  You can

find the document at http://www.python.org/dev/peps/pep-0008 .

The actual coding convention for attribute and variable names is
lower_case_separated_by_underscores, but there are projects out there
that use initialLowerCamelCase.  Pick whatever your current project
is using and stick with it.

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


[Tutor] named pipe problem

2008-08-20 Thread dave selby
Hi All,


I am trying to get a named pipe working, I have already made a fifo

[EMAIL PROTECTED]:/usr/local/bin$ ls -al /home/dave/kmotion2/www/pipe_func
prw-rw 1 dave www-data 0 2008-08-20 20:25 /home/dave/kmotion2/www/pipe_func
[EMAIL PROTECTED]:/usr/local/bin$

but when I execute my test code to add an item to the fifo ...

func = '199'
www_dir = '/home/dave/kmotion2/www'
print '%s/pipe_func' % www_dir
pipeout = os.open('%s/pipe_func' % www_dir, os.O_WRONLY)
print 'xxx'
os.write(pipeout, func)
os.close(pipeout)

I get the path printed out & then the script hangs silently on pipeout
= os.open('%s/pipe_func' % www_dir, os.O_WRONLY)

Can anyone tell me why ? I expected it to return immediately

Cheers

Dave



-- 

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Reformatting phone number

2008-08-20 Thread Dotan Cohen
I have a small script (linux) that takes a phone number as an argument:
#!/usr/bin/env python
import sys
number = '+' + sys.argv[1]


However, if the first digit of the phone number is a 0 then I need to
replace that 0 with "972". I can add the "972", but how do I remove
the leading "0"?

For instance, this code:
#!/usr/bin/env python
import sys
if sys.argv[1][0] == 0:
number = '+972' + sys.argv[1]
else:
number = '+' + sys.argv[1]

Turns the phone number "0123" into "9720123" but I need "972123". How
is this done? Better yet, where in the fine manual would I find this?

Thanks in advance.

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת

ä-ö-ü-ß-Ä-Ö-Ü
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] named pipe problem

2008-08-20 Thread Kent Johnson
On Wed, Aug 20, 2008 at 3:54 PM, dave selby <[EMAIL PROTECTED]> wrote:
> Hi All,
>
>
> I am trying to get a named pipe working, I have already made a fifo
>
> [EMAIL PROTECTED]:/usr/local/bin$ ls -al /home/dave/kmotion2/www/pipe_func
> prw-rw 1 dave www-data 0 2008-08-20 20:25 
> /home/dave/kmotion2/www/pipe_func
> [EMAIL PROTECTED]:/usr/local/bin$
>
> but when I execute my test code to add an item to the fifo ...
>
>func = '199'
>www_dir = '/home/dave/kmotion2/www'
>print '%s/pipe_func' % www_dir
>pipeout = os.open('%s/pipe_func' % www_dir, os.O_WRONLY)
>print 'xxx'
>os.write(pipeout, func)
>os.close(pipeout)
>
> I get the path printed out & then the script hangs silently on pipeout
> = os.open('%s/pipe_func' % www_dir, os.O_WRONLY)
>
> Can anyone tell me why ? I expected it to return immediately

See this informative thread:
http://mail.python.org/pipermail/python-list/2006-August/396499.html

Summary: opening a pipe for write blocks until it is also opened for read.

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


Re: [Tutor] Reformatting phone number

2008-08-20 Thread Dotan Cohen
I have gotten a bit farther, but I cannot get this to work as
described in the previous mail:

#!/usr/bin/env python
import sys
preNumber =  sys.argv[1]
if preNumber[0] == 0:
number = '+972' + preNumber[1:]
else:
number = '+' + preNumber

Where is my flaw?

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת

ä-ö-ü-ß-Ä-Ö-Ü
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reformatting phone number

2008-08-20 Thread Dotan Cohen
I was missing the quotes in the if statement. Changing
if preNumber[0] == 0:
to
if preNumber[0] == "0":
fixed the problem.

Why did I need those quotes? The 0 is numerical, so it should not need
the quotes, no?

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת

ä-ö-ü-ß-Ä-Ö-Ü
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reformatting phone number

2008-08-20 Thread Robert Berman




Perhaps because preNumber is a character and not an
integer?

Robert

Dotan Cohen wrote:

  I was missing the quotes in the if statement. Changing
if preNumber[0] == 0:
to
if preNumber[0] == "0":
fixed the problem.

Why did I need those quotes? The 0 is numerical, so it should not need
the quotes, no?

  



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


Re: [Tutor] named pipe problem

2008-08-20 Thread Kent Johnson
On Wed, Aug 20, 2008 at 4:53 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> See this informative thread:
> http://mail.python.org/pipermail/python-list/2006-August/396499.html
>
> Summary: opening a pipe for write blocks until it is also opened for read.

Also, on Mac OSX at least ordinary file operations seem to work fine.
For example, in one Terminal window:

$ mkfifo testf
$ cat < testf

In another Terminal window, start Python and type
In [5]: f=open('testf', 'w')
In [6]: f.write('foo\n')
In [7]: f.flush()
In [8]: f.close()

The output appears in the first window and the cat command finishes.
If do the Python part first, the open() blocks until I issue the cat
command.

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


Re: [Tutor] Reformatting phone number

2008-08-20 Thread Dotan Cohen
2008/8/21 Robert Berman <[EMAIL PROTECTED]>:
> Perhaps because preNumber is a character and not an integer?
>

Perhaps. In php the distinction was made by the fact that there were
no non-numerical characters in a string. I don't know enough Python to
know if this is the case. Can I declare a variable type in Python as
in C?

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת

ä-ö-ü-ß-Ä-Ö-Ü
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reformatting phone number

2008-08-20 Thread Robert Berman




Not directly as in C, but, for example, if you have
s='3' and you want s used as an integer, you can say s=int(s) and it is
an integer. Conversely, if you have a question about the type, you
could also say type(s) which, depending, will return, 'str','int', etc.


Hope this helps a bit.

Robert

Dotan Cohen wrote:

  2008/8/21 Robert Berman <[EMAIL PROTECTED]>:
  
  
Perhaps because preNumber is a character and not an integer?


  
  
Perhaps. In php the distinction was made by the fact that there were
no non-numerical characters in a string. I don't know enough Python to
know if this is the case. Can I declare a variable type in Python as
in C?

  



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


Re: [Tutor] Reformatting phone number

2008-08-20 Thread Alan Gauld

"Dotan Cohen" <[EMAIL PROTECTED]> wrote


know if this is the case. Can I declare a variable type in Python as
in C?


In Python values have types and variables are simply
names associated with values.

Thus

v = '123'# v 'is' a string because '123' is a string
v = 123 # now v 'is' an int because 123 is an int.

The variable takes on the type of the value with which it is
associated but it can be made to refer to any other value
and thus its type effectively changes. So it's best not to
think of variables having types but rather consider objects
(or values) as having a type, and variables as being
associated with objects..

This is one of the most fundamental characteristics of
Python and one of the hardest for users of other languages
to adapt to.

HTH,

--
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] Reformatting phone number

2008-08-20 Thread Dotan Cohen
2008/8/21 Alan Gauld <[EMAIL PROTECTED]>:
> "Dotan Cohen" <[EMAIL PROTECTED]> wrote
>
>> know if this is the case. Can I declare a variable type in Python as
>> in C?
>
> In Python values have types and variables are simply
> names associated with values.
>
> Thus
>
> v = '123'# v 'is' a string because '123' is a string
> v = 123 # now v 'is' an int because 123 is an int.
>
> The variable takes on the type of the value with which it is
> associated but it can be made to refer to any other value
> and thus its type effectively changes. So it's best not to
> think of variables having types but rather consider objects
> (or values) as having a type, and variables as being
> associated with objects..
>
> This is one of the most fundamental characteristics of
> Python and one of the hardest for users of other languages
> to adapt to.
>

This characteristic is similar enough to php (a language where one
doesn't even think about types) that I will get used to it. Thanks.

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת

ä-ö-ü-ß-Ä-Ö-Ü
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reformatting phone number

2008-08-20 Thread Dotan Cohen
2008/8/21 Robert Berman <[EMAIL PROTECTED]>:
> Not directly as in C, but, for example, if you have s='3' and you want s
> used as an integer, you can say s=int(s) and it is an integer. Conversely,
> if you have a question about the type, you could also say type(s) which,
> depending, will return, 'str','int', etc.
>
> Hope this helps a bit.
>

It does help, and I will make a point of familiarizing myself with the
errors reported by runtime errors (is this a valid term in Python, as
there are no compile errors) such as those generated from code like
this:
s='hello, world!'
s=int(s)

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת

ä-ö-ü-ß-Ä-Ö-Ü
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reformatting phone number

2008-08-20 Thread Kent Johnson
On Wed, Aug 20, 2008 at 4:43 PM, Dotan Cohen <[EMAIL PROTECTED]> wrote:
> I have a small script (linux) that takes a phone number as an argument:
> #!/usr/bin/env python
> import sys
> number = '+' + sys.argv[1]
> 
>
> However, if the first digit of the phone number is a 0 then I need to
> replace that 0 with "972". I can add the "972", but how do I remove
> the leading "0"?
>
> For instance, this code:
> #!/usr/bin/env python
> import sys
> if sys.argv[1][0] == 0:

Another way to write this is
  if sys.argv[1].startswith('0'):

>number = '+972' + sys.argv[1]

You need to learn about slicing. This is a way of indexing any
sequence, including strings. See
http://docs.python.org/tut/node5.html#SECTION00512
http://docs.python.org/lib/typesseq.html

In particular, you want
  number = '+972' + sys.argv[1][1:]

which takes all characters of argv[0] after the first.

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


[Tutor] programming tic tac toe

2008-08-20 Thread Ark
Hi.
I programmed a simple tic tac toe game in python.  I already finished it,
but I'm not pleased with the way I used to identify a line.
I used a list (with lists inside) to represent the board.  And to identify a
winning line I used many if's, like this one:
def line(board):
if board[0][0] == board[1][1] == board[2][2]:
return True
...
...
return False
It's only an examble, but I did it that way.
I did not like using all those if's, and I would like to hear suggestions to
find a line in the board, maybe a more  intelligent approach.
I would like a solution using lists because it would be useful for example
in C too, but I have been thinking about another way to represent a board.

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


Re: [Tutor] programming tic tac toe

2008-08-20 Thread Alan Gauld

"Ark" <[EMAIL PROTECTED]> wrote

I used a list (with lists inside) to represent the board.  And to 
identify a

winning line I used many if's, like this one:
def line(board):
   if board[0][0] == board[1][1] == board[2][2]:
   return True


I did not like using all those if's, and I would like to hear 
suggestions to

find a line in the board, maybe a more  intelligent approach.


Given that there are always 3x3 cells on an oxo board and
therefore only 8 possible winning lines hard coding is probably
a reasonable approach. You could make it into a single boolean
condition which would be slightly less typing and slightly faster
but otherwise what you have is fine I think:

def line(board):
   return  board[0][0] == board[0][1] == board[0][2] or
 board[1][0] == board[1][1] == board[1][2] or
 ...
 board[2][0] == board[1][1] == board[[0][2]

But I'm not sure its that much nicer! :-)

For the more general case of an NxN board then you
should probably consider using a loop and relative
indexing but for 3x3 hard coded is better IMHO.

--
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] Reformatting phone number

2008-08-20 Thread Dotan Cohen
2008/8/21 Kent Johnson <[EMAIL PROTECTED]>:
> Another way to write this is
>  if sys.argv[1].startswith('0'):

Nice! I had looked for this type of function, but could not find it.

Is there a list of functions, organized by categories, for Python?
Take for example these pages from te php documentation:
http://il.php.net/manual/en/book.strings.php
http://il.php.net/manual/en/book.array.php
http://il.php.net/manual/en/book.math.php

which are all linked from:
http://il.php.net/manual/en/funcref.php

Is there anything like this for Python? If not, I am willing to code
the site if some Python gurus will help contribute their knowledge.



> You need to learn about slicing. This is a way of indexing any
> sequence, including strings. See
> http://docs.python.org/tut/node5.html#SECTION00512

This I have read, while googling this thread.

> http://docs.python.org/lib/typesseq.html

This I will read, to keep up to speed.

Thanks!

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת

ä-ö-ü-ß-Ä-Ö-Ü
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reformatting phone number

2008-08-20 Thread Dotan Cohen
2008/8/21 Robert Berman <[EMAIL PROTECTED]>:
> One can 'quasi' compile Python code. Since you come from a C background and
> I come from a C++ background, a Python compile isn't really compiling an
> object module. I don't see an object file, I don't see an executable;
> therefore, in my opinion, Python is an interpretive language and I love it.
> It is easy to work with. It is incredibly friendly, incredibly powerful, and
> it makes building algorithms and shells not only easy but also fun. I really
> like this language. The more I learn of it, the more I like it. True,  at
> times I do miss the speed of C and C++; but I think we tend to ignore the
> cost of development time excesses in those languages.

I don't have more of a C background than a one-semester course in
university. But the details between the languages are night and day. I
am enjoying Python. I recently heard that Python is like writing
psuedo-code that runs. I am seeing that this is true. I especially
love the use of indentation for block demarcation.

> To answer your question, I hope. You get errors from the interpretor and
> from Python at run time. The interpretor errors tend to be very easy to spot
> and to fix; they are primarily indentation or syntax errors. The run time
> errors actually show you what failed. You can experiment until you fix them.

Error are certainly the best way to learn.

> I hope you enjoy using the language as much as I do.

Thanks.

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת

ä-ö-ü-ß-Ä-Ö-Ü
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor