Re: [Tutor] [tutor]Imagechop error

2008-02-03 Thread Alan Gauld

"Varsha Purohit" <[EMAIL PROTECTED]> wrote

> AttributeError: 'str' object has no attribute 'load'I am unable 
> to
> understand what is the problem. Can any one help me understand this
> imagechop module ??

When posting errors don't just post the last line. Post the whole
error message since the stacv trace often contains the most
useful data.

> file1 = "Nearest.jpg"
> file2 = "Bilinear.jpg"
>
> diff = ImageChops.difference(file1, file2)

You are passing file names not files so it may be that
you need to pass file objects? But I don't know because
I've bnever used ImageChops

Alan G. 


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


[Tutor] Bad time to get into Python?

2008-02-03 Thread Dotan Cohen
The little programming that I need I have been able to get away with
silly php and bash scripts. However, my needs are getting bigger and I
see Python as an ideal language for my console apps, and the
occasional GUI that I might write for the wife. However, with the
coming of Python3 and the new syntax, is this a bad time to start
learning Python? I don't want to learn 2.x if 3.x will replace it, and
not be compatible, in one year. I know that I can continue using 2.x,
but maybe I should wait until 3.x is released to start learning? What
does the community think?

That asked, I've heard that 2.6 can be configured to warn when using
code that will not run in 3.x. Is this correct? How is this done? I'd
like to do it on a per-file basis, so that I will only need to run one
version of python on this machine. I want my own apps to throw errors,
but not other python apps on this system. Is there some error-level
code that I can run?

Thanks in advance.

Dotan Cohen

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

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bad time to get into Python?

2008-02-03 Thread Alan Gauld

"Dotan Cohen" <[EMAIL PROTECTED]> wrote

> coming of Python3 and the new syntax, is this a bad time to start
> learning Python? I don't want to learn 2.x if 3.x will replace it,

3.x won't be the end of changes in Python, amy more than
other languages change. While the changes for 3.x will be
bigger than for previous languages my understanding is
that they ae not huge and certainly not as big as the
jump from VB6 to VB.NET for example.

A coming version change is never a good reason not
to learn a language IMHO. More important is to ask
why learn that language in the first place? What will
it offer that your current skills don;t already provide?
If you can answer that question positively then the
version change will likely make no significant change
to the cost/benefit equation.

> That asked, I've heard that 2.6 can be configured
> to warn when using code that will not run in 3.x.
> Is this correct? How is this done?

I beliebe you re right but don;t know the mechanism.
But I should think it equally likely that there will be
tools available either with the release or very soon
after thart will, at the very least, identify the areas
needing change - if not actually making most of
the changes for you. This nearly always happens
with significant language upgrades.

> version of python on this machine. I want my own apps to throw 
> errors,
> but not other python apps on this system. Is there some error-level
> code that I can run?

I'm not clear what you mean by that bit.

-- 
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


[Tutor] os.system() problem

2008-02-03 Thread dave selby
Hi all,

I am not sure if this is a Python or bash issue :).

In bash if I execute 'motion' with the following ...

[EMAIL PROTECTED]:~/.kde/share/apps/kmotion$ motion &> /dev/null &
[1] 10734
[EMAIL PROTECTED]:~/.kde/share/apps/kmotion$

I get what I expect, a background job, however if I execute it from
Python with an os.system ...

os.system('motion &> /dev/null &')

I get tons of output to the BASH shell ...

[0] Processing thread 0 - config file /etc/motion/motion.conf
[0] Processing config file /etc/motion/motion.1.conf
[0] Processing config file /etc/motion/motion.2.conf
[1] Thread is from /etc/motion/motion.1.conf
[2] Thread is from /etc/motion/motion.2.conf
[1] Thread started
[2] Thread started
[1] File of type 2 saved to: /var/lib/motion/20080203/01/tmp/175253.jpg
...etc ...

I just can't work out why this is happening & how to stop it ?. Any ideas ?

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


Re: [Tutor] os.system() problem

2008-02-03 Thread Michael Langford
Popen may be a better call for what you're trying to do. Some of the
higher numbered popens will give you the stderr stream. The lowered
numbered ones are probably all you need.

http://docs.python.org/lib/node529.html

   --Michael

On Feb 3, 2008 12:54 PM, dave selby <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I am not sure if this is a Python or bash issue :).
>
> In bash if I execute 'motion' with the following ...
>
> [EMAIL PROTECTED]:~/.kde/share/apps/kmotion$ motion &> /dev/null &
> [1] 10734
> [EMAIL PROTECTED]:~/.kde/share/apps/kmotion$
>
> I get what I expect, a background job, however if I execute it from
> Python with an os.system ...
>
> os.system('motion &> /dev/null &')
>
> I get tons of output to the BASH shell ...
>
> [0] Processing thread 0 - config file /etc/motion/motion.conf
> [0] Processing config file /etc/motion/motion.1.conf
> [0] Processing config file /etc/motion/motion.2.conf
> [1] Thread is from /etc/motion/motion.1.conf
> [2] Thread is from /etc/motion/motion.2.conf
> [1] Thread started
> [2] Thread started
> [1] File of type 2 saved to: /var/lib/motion/20080203/01/tmp/175253.jpg
> ...etc ...
>
> I just can't work out why this is happening & how to stop it ?. Any ideas ?
>
> 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
>



-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bad time to get into Python?

2008-02-03 Thread Dotan Cohen
On 03/02/2008, Alan Gauld <[EMAIL PROTECTED]> wrote:
>
> "Dotan Cohen" <[EMAIL PROTECTED]> wrote
>
> > coming of Python3 and the new syntax, is this a bad time to start
> > learning Python? I don't want to learn 2.x if 3.x will replace it,
>
> 3.x won't be the end of changes in Python, amy more than
> other languages change. While the changes for 3.x will be
> bigger than for previous languages my understanding is
> that they ae not huge and certainly not as big as the
> jump from VB6 to VB.NET for example.
>
> A coming version change is never a good reason not
> to learn a language IMHO. More important is to ask
> why learn that language in the first place? What will
> it offer that your current skills don;t already provide?
> If you can answer that question positively then the
> version change will likely make no significant change
> to the cost/benefit equation.

I currently use php on the webserver, and bash on desktop. I'm good at
neither, and I've been leaning towards using php more and more on the
desktop for scripting (the php -q flag supresses http header output
for console usage). Python seems like a real language that can replace
both. It is also flexible enough to let me do a GUI app in Qt if need
be, or even a windows app should I ever need. I'm already convinced
that I need to learn Python. But I don't want to learn it twice, and
I'm in no rush.

> > That asked, I've heard that 2.6 can be configured
> > to warn when using code that will not run in 3.x.
> > Is this correct? How is this done?
>
> I beliebe you re right but don;t know the mechanism.
> But I should think it equally likely that there will be
> tools available either with the release or very soon
> after thart will, at the very least, identify the areas
> needing change - if not actually making most of
> the changes for you. This nearly always happens
> with significant language upgrades.
>
> > version of python on this machine. I want my own apps to throw
> > errors,
> > but not other python apps on this system. Is there some error-level
> > code that I can run?
>
> I'm not clear what you mean by that bit.

In php error reporting can be changed on the fly, in the script file
itself. So I can have one script that logs errors to a text file, and
another that prints errors to the browser/CLI. I was hoping for
something similar in Python.

Dotan Cohen

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

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bad time to get into Python?

2008-02-03 Thread Thomas Pani
Dotan Cohen wrote:
> However, with the coming of Python3 and the new syntax, is this a bad time to 
> start learning Python?
Not at all, I'd say. Changes will be fairly small, with the main changes
being:
 - print is replaced by a print() function
 - / will become the float division operator
 - changes towards iterators (e.g. range() doesn't return a list)
 - string filetype changes
There are lots of other changes, but most of them include removing
already deprecated idioms. [1] has a list of Python 3k changes.
There's also the 2to3 conversion tool which allows you to run lots of
conversion automated.

> That asked, I've heard that 2.6 can be configured to warn when using
> code that will not run in 3.x. Is this correct?
Yes, 2.6 will support a "Py3k warnings mode". It should also have many
of 3k's features already implemented, allowing to run both side-by-side
or via __future__. PEP 3000 ([2]) has more info on this.

> How is this done? I'd like to do it on a per-file basis, so that I will
> only need to run one version of python on this machine.
Don't know. But if your only using it for some home-coding, you would
just once do the conversion and then update to 3k.

> I want my own apps to throw errors,
> but not other python apps on this system. Is there some error-level
> code that I can run?
Not sure what you mean by that. Are you refering to exception-handling?

I'd say it's not a bad time to learn Python. There will be some major
changes in 3k, but as long as you don't have to maintain 2.6 and 3.0 in
parallel, conversion should be easy enough.

Cheers,
thomas pani

[1] http://docs.python.org/dev/3.0/whatsnew/3.0.html
[2] http://www.python.org/dev/peps/pep-3000/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bad time to get into Python?

2008-02-03 Thread Kent Johnson
Dotan Cohen wrote:
> The little programming that I need I have been able to get away with
> silly php and bash scripts. However, my needs are getting bigger and I
> see Python as an ideal language for my console apps, and the
> occasional GUI that I might write for the wife. However, with the
> coming of Python3 and the new syntax, is this a bad time to start
> learning Python? I don't want to learn 2.x if 3.x will replace it, and
> not be compatible, in one year. I know that I can continue using 2.x,
> but maybe I should wait until 3.x is released to start learning? What
> does the community think?

Don't wait. Python 2.5 is very useful today. Python 2.x will be viable 
for years. Python 2.6 is not even scheduled for release until this 
summer and it will be maintained long after that. PEP 3000 says,

"I expect that there will be parallel Python 2.x and 3.x releases for 
some time; the Python 2.x releases will continue for a longer time than 
the traditional 2.x.y bugfix releases. Typically, we stop releasing 
bugfix versions for 2.x once version 2.(x+1) has been released. But I 
expect there to be at least one or two new 2.x releases even after 3.0 
(final) has been released, probably well into 3.1 or 3.2. This will to 
some extent depend on community demand for continued 2.x support, 
acceptance and stability of 3.0, and volunteer stamina."

http://www.python.org/dev/peps/pep-3000/


> That asked, I've heard that 2.6 can be configured to warn when using
> code that will not run in 3.x. Is this correct? How is this done? I'd
> like to do it on a per-file basis, so that I will only need to run one
> version of python on this machine. I want my own apps to throw errors,
> but not other python apps on this system. Is there some error-level
> code that I can run?

There is a command-line switch in 2.6, -3, which will enables warnings 
about features that will be removed in Python 3.0, and some features of 
Python 3.0 are being back-ported to Python 2.6:
http://docs.python.org/dev/whatsnew/2.6.html#python-3-0

There is also a tool being developed (2to3) to convert Python 2.x code 
to 3.0 semi-automatically:
http://svn.python.org/view/sandbox/trunk/2to3/README?rev=57919&view=markup

However, the goal of these efforts, IIUC, is *not* to allow a single 
script to run in both 2.6 and 3.0, it is to enable easy porting from 2.6 
to 3.0. In particular, my understanding is that the -3 warnings will 
warn of constructs that cannot be correctly converted by 2to3. More 
details here:
http://www.python.org/dev/peps/pep-3000/#compatibility-and-transition

So I would say the outlook for 2.6 is better than you think but the 
outlook for compatibility is worse.

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


Re: [Tutor] Bad time to get into Python?

2008-02-03 Thread Dotan Cohen
On 03/02/2008, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Dotan Cohen wrote:
>  > The little programming that I need I have been able to get away with
>  > silly php and bash scripts. However, my needs are getting bigger and I
>  > see Python as an ideal language for my console apps, and the
>  > occasional GUI that I might write for the wife. However, with the
>  > coming of Python3 and the new syntax, is this a bad time to start
>  > learning Python? I don't want to learn 2.x if 3.x will replace it, and
>  > not be compatible, in one year. I know that I can continue using 2.x,
>  > but maybe I should wait until 3.x is released to start learning? What
>  > does the community think?
>
>
> Don't wait. Python 2.5 is very useful today. Python 2.x will be viable
>  for years. Python 2.6 is not even scheduled for release until this
>  summer and it will be maintained long after that. PEP 3000 says,
>
>  "I expect that there will be parallel Python 2.x and 3.x releases for
>  some time; the Python 2.x releases will continue for a longer time than
>  the traditional 2.x.y bugfix releases. Typically, we stop releasing
>  bugfix versions for 2.x once version 2.(x+1) has been released. But I
>  expect there to be at least one or two new 2.x releases even after 3.0
>  (final) has been released, probably well into 3.1 or 3.2. This will to
>  some extent depend on community demand for continued 2.x support,
>  acceptance and stability of 3.0, and volunteer stamina."
>
>
>  http://www.python.org/dev/peps/pep-3000/
>
>
>
> > That asked, I've heard that 2.6 can be configured to warn when using
>  > code that will not run in 3.x. Is this correct? How is this done? I'd
>  > like to do it on a per-file basis, so that I will only need to run one
>  > version of python on this machine. I want my own apps to throw errors,
>  > but not other python apps on this system. Is there some error-level
>  > code that I can run?
>
>
> There is a command-line switch in 2.6, -3, which will enables warnings
>  about features that will be removed in Python 3.0, and some features of
>  Python 3.0 are being back-ported to Python 2.6:
>  http://docs.python.org/dev/whatsnew/2.6.html#python-3-0
>
>  There is also a tool being developed (2to3) to convert Python 2.x code
>  to 3.0 semi-automatically:
>  http://svn.python.org/view/sandbox/trunk/2to3/README?rev=57919&view=markup
>
>  However, the goal of these efforts, IIUC, is *not* to allow a single
>  script to run in both 2.6 and 3.0, it is to enable easy porting from 2.6
>  to 3.0. In particular, my understanding is that the -3 warnings will
>  warn of constructs that cannot be correctly converted by 2to3. More
>  details here:
>  http://www.python.org/dev/peps/pep-3000/#compatibility-and-transition
>
>  So I would say the outlook for 2.6 is better than you think but the
>  outlook for compatibility is worse.
>
>
>  Kent
>

Thanks. My concern is not that the code won't run on Python3, rather,
that the effort that I put into learning 2.x will be wasted when 3.x
will be current. Now I'm a bit more confident, however. I'll get to
work learning right away. Thanks.

Dotan Cohen

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

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bad time to get into Python?

2008-02-03 Thread Kent Johnson
Dotan Cohen wrote:
> Thanks. My concern is not that the code won't run on Python3, rather,
> that the effort that I put into learning 2.x will be wasted when 3.x
> will be current. 

No, it will not be wasted. 3.0 is not a new language, it is a major 
tuneup of the existing language.

> Now I'm a bit more confident, however. I'll get to
> work learning right away. Thanks.

Great! Come back when you have more questions!

Kent


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


[Tutor] Hello and newbie question about "self"

2008-02-03 Thread Patrick
Hi guru's,

New to the list. I bought O'Reilly's Learning Python (3rd edition for
2.5) a while back. Slowly making my way through it and was pleasantly
surprised that Python seems easier than C. Until...I bumped into the
"self" thingy.

Can anyone please point me to a document that explains "self" in
layman's terms. Or lacking such a doc throw in a much appreciated
layman's explanation what "self" is and when/where to use it? 

In the book (so far) I've seen "self" pop up in these examples (on pages
457 and 458):

class C1(C2, C3):
def setname(self, who)
self.name = who

class C1(C2, C3):
def __init__(self, who)
self.name = who

Thanks for any pointers!

Regards,
Patrick

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


[Tutor] TypeError

2008-02-03 Thread Seon Kang
When i tried to run my program, i was given this message
(this is the message in part)

file "C:Python25\lib\site-packages\livewires\games.py", line 503, in_tick
self.position = ((self._x + self._dx), (self._y + self._dy))
TypeError: unsopported opernad type(s) for +: 'int' and type'

What is the nature of my problem?
But more specifically, what is the 'type' it's referring to?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] newbie code review please!

2008-02-03 Thread Tyler Smith
Hi,

I'm a middling amateur coder in C and elisp, trying to wrap my head around
python syntax. I've implemented the code for the markov chain random
text generator from Kernighan and Pike's "The practice of
programming", and I'd appreciate any tips on how to make it more
pythonic.

The code appears to work as intended. It reads a file, breaks the text
into a list of individual words. It then builds a hash table in the
form of: 

(prefix-word-1, prefix-word-2):[list-of-possible-following-words]

It finishes by using the hash table to generate a nonsensical, and
occasionally amusing bit of text based on the input.

Any comments welcome!

Thanks,

Tyler

#! /usr/bin/python

import sys, random

word_max = 1000

# Read in file
infile = open(sys.argv[1], 'r')
ifstring = infile.read()

# break file into words
iflist = ifstring.replace('\n', ' ').split()

# build hash of form (prefix1, prefix2): [word]
word_hash = {}
for i in range(len(iflist) - 2):
pr1 = iflist[i]
pr2 = iflist[i+1]
tv = iflist[i+2]
tk = (pr1, pr2)
if word_hash.has_key(tk) and tv not in word_hash[tk]:
word_hash[tk].append(tv)
else:
word_hash[tk] = [tv]

if word_hash.has_key((iflist[-2], iflist[-1])):
word_hash[(iflist[-2], iflist[-1])].append('\n')
else:
word_hash[(iflist[-2], iflist[-1])] = ['\n']

# output first two words
w1, w2 = iflist[0:2]
print w1, w2,

# generate new words from hash
word_num = 0
while w2 <> '\n' and word_num < word_max:
w1, w2 = w2, random.choice(word_hash[(w1, w2)])
print w2,
word_num += 1


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


Re: [Tutor] Hello and newbie question about "self"

2008-02-03 Thread Michael Langford
In C, you may have "objectorientedesque" code like the following;

struct net {
int foo;
int bar;
int baz;
};


void populate_net( struct net* mynet, int fooval, int barval)
{
  mynet->foo = fooval;
  mynet->bar = barval;
  mynet ->baz = fooval * 5;
}

int connect_to_net(struct net* mynet)
{
 return  open_internet_connection(mynet->foo);
}

int main (void)
{
  struct net inet;
  populate_net(&inet,2,2);

  int returncode = connect_to_net(&inet);
  printf("%d\n",returncode);
}

In that batch of C code, you manipulate the struct without fiddling
with its fields in the user code. You let the functions change its
values so that they are done correctly.

In python, you are doing something similar. However, they make some
syntactic sugar to make it so you don't have to pass the object in
explicily. That is what self is.

So the analgous python code is:


class net(object):
   def __init__(self,fooval,barbal):
   self.foo = fooval
   self.bar = barval
   self.baz = fooval*5

def connect_to(self):
   return open_internet_connection(self.foo)


inet = net(2,2)
returncode = inet.connect_to()
print returncode

See how you don't have to pass in the inet object in? Instead you call
it with the inet.connect_to() function, and the object itself is
passed in explicitly as self?

That's all it is.

Btw, make sure to always include "self". Otherwise you'll be writing a
class method and it doesn't work the same way.

--Michael

On Feb 3, 2008 3:15 PM, Patrick <[EMAIL PROTECTED]> wrote:
> Hi guru's,
>
> New to the list. I bought O'Reilly's Learning Python (3rd edition for
> 2.5) a while back. Slowly making my way through it and was pleasantly
> surprised that Python seems easier than C. Until...I bumped into the
> "self" thingy.
>
> Can anyone please point me to a document that explains "self" in
> layman's terms. Or lacking such a doc throw in a much appreciated
> layman's explanation what "self" is and when/where to use it?
>
> In the book (so far) I've seen "self" pop up in these examples (on pages
> 457 and 458):
>
> class C1(C2, C3):
> def setname(self, who)
> self.name = who
>
> class C1(C2, C3):
> def __init__(self, who)
> self.name = who
>
> Thanks for any pointers!
>
> Regards,
> Patrick
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bad time to get into Python?

2008-02-03 Thread Kent Johnson
I did a little research on the question of running the same script 
unmodified in Python 2.6 and 3.0. It seems that there is no consensus 
opinion and it may depend on your personal tolerance for compatibility 
cruft. Here is a c.l.py thread of interest:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/4e0a71d86e80d0f9/8e7fb527e32d3064?hl=en&;

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


Re: [Tutor] TypeError

2008-02-03 Thread Kent Johnson
Seon Kang wrote:
> When i tried to run my program, i was given this message
> (this is the message in part)
>  
> file "C:Python25\lib\site-packages\livewires\games.py", line 503, in_tick
> self.position = ((self._x + self._dx), (self._y + self._dy))
> TypeError: unsopported opernad type(s) for +: 'int' and type'
>  
> What is the nature of my problem?

It seems that either self._dx or self._dy is not an integer.

> But more specifically, what is the 'type' it's referring to?

A type is the class of a value. For example, int, str and float are all 
types.

If you show us the full traceback and some of your code we can be more 
helpful.

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


Re: [Tutor] Hello and newbie question about "self"

2008-02-03 Thread Kent Johnson
Patrick wrote:
> Hi guru's,
> 
> New to the list. I bought O'Reilly's Learning Python (3rd edition for
> 2.5) a while back. Slowly making my way through it and was pleasantly
> surprised that Python seems easier than C. Until...I bumped into the
> "self" thingy.

This should be covered by any tutorial. 2nd edition of Learning Python 
has a section called Methods that introduces self. Also see
http://www.ibiblio.org/swaroopch/byteofpython/read/self.html
http://hetland.org/writing/instant-hacking.html

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


Re: [Tutor] newbie code review please!

2008-02-03 Thread Kent Johnson
Tyler Smith wrote:

> iflist = ifstring.replace('\n', ' ').split()

The replace is not needed, split() splits on any whitespace including 
newlines.

> # build hash of form (prefix1, prefix2): [word]
> word_hash = {}
> for i in range(len(iflist) - 2):
> pr1 = iflist[i]
> pr2 = iflist[i+1]
> tv = iflist[i+2]
> tk = (pr1, pr2)

or, tk = tuple(iflist[i:i+2])

> if word_hash.has_key(tk) and tv not in word_hash[tk]:
> word_hash[tk].append(tv)
> else:
> word_hash[tk] = [tv]

This could be done much more cleanly with a defaultdict that maps a pair 
to a set:

from collections import defaultdict
wordhash = defaultdict(set)
...then just
   wordhash[tk].add(tv)

> if word_hash.has_key((iflist[-2], iflist[-1])):
> word_hash[(iflist[-2], iflist[-1])].append('\n')
> else:
> word_hash[(iflist[-2], iflist[-1])] = ['\n']   

word_hash[(iflist[-2], iflist[-1])].add('\n')

> # generate new words from hash
> word_num = 0
> while w2 <> '\n' and word_num < word_max:
> w1, w2 = w2, random.choice(word_hash[(w1, w2)])
> print w2,
> word_num += 1

or,
for word_num in range(word_max):
 w1, w2 = w2, random.choice(word_hash[(w1, w2)])
 print w2,
 if w2 == '\n':
 break

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


Re: [Tutor] TypeError

2008-02-03 Thread Kent Johnson
Seon Kang wrote:
> Why does it not specify the type?

Actually, it is specifying the type of the bad argument, which is itself 
'type'. Confusing, I know. Try this:
 >>> 1+int

Traceback (most recent call last):
   File "", line 1, in 
: unsupported operand type(s) for +: 'int' 
and 'type'

The type of 1 is 'int' and the type of int is 'type'. It is telling you 
that you are trying to add an integer to a type object.

> here is the entire section of the module that i imported that's giving 
> me the trouble. Why is it giving the TypeError?

It would really help to see *your* code and the *complete* traceback. 
Can you figure out where _dx and _dy are coming from? They don't seem to 
be assigned in the code you show here.

Also please use Reply All to reply to the list.

Kent

>  
>  
> 
> class Sprite(object):
> def __init__(self, image, angle=0,
>  x=0, y=0,
>  top=None, bottom=None, left=None, right=None,
>  dx=0, dy=0,
>  interval=1, is_collideable=True):
>
> if not Screen.initialized:
> raise GamesError, "Screen object must be intialized before 
> any Sprite object"
>  
> self._surface = image
> self._orig_surface = image# Surface before any rotation
> self._rect = self._surface.get_rect()
> 
> self.position = (x, y)
> 
> if top != None:
> self.top = top
> if bottom != None:
> self.bottom = bottom
> if left != None:
> self.left = left
> if right != None:
> self.right = right
> 
> self.velocity = (dx, dy)
> 
> self._angle = angle % 360
> if self._angle != 0:
> self._rotate()
> 
> self.is_collideable = is_collideable
> 
> self._interval = interval
> self._tickable = 1
> self._next = 0
> 
> self._gone = 0
> 
> def __del__(self):
> if screen and not self._gone:
> self.destroy()
> 
> def _draw(self):
> """
> Draw object on screen by blitting the image onto the screen.
> """
> screen.blit_and_dirty(self._surface, self._rect)
> 
> def _erase(self):
> """
> Erase object from screen by blitting the background over where 
> it was.
> """
> screen.blit_background(self._rect)
>  
> def _replace(self, new_surface):
> x, y = self.position
> self._surface = new_surface
> self._rect = self._surface.get_rect()
> self.position = (x, y)
> 
> def _rotate(self):
> self._replace(pygame.transform.rotate(self._orig_surface, 
> -self._angle))
> 
> def _tick(self):
> self._next = self._next + 1
> if self._next >= self._interval:
> self._next = 0 
> self.tick()
> if self._dx or self._dy:
> self.position = ( (self._x + self._dx), (self._y + self._dy) )
> self.update()
> 
> def start (self):
> self._tickable = 1
> self._next = 0
> 
> def stop (self):
> self._tickable = 0
> 
> def update(self):
> pass
> 
> def tick(self):
> pass
>  
> def overlaps(self, other):
> if not self.is_collideable or not other.is_collideable:
> return False
> else:
> return self._rect.colliderect(other._rect)
> 
> def elevate(self, above=None):
> """
> Elevate an object to the top of the stack, or above the specified
> object.
> """
> screen._elevate(self, above)
> 
> def lower(self, below=None):
> """
> Lower an object to the bottom of the stack, or below the specified
> object.
> """
> screen._lower(self, below)
> 
> def destroy(self):
> """
> Erase object from screen and remove it from the list of objects
> maintained by games module.
> """
> self._erase()
> screen.remove(self)
> self._gone = 1
> 
> 
> 
>  
> On 2/3/08, *Kent Johnson* <[EMAIL PROTECTED] > 
> wrote:
> 
> Seon Kang wrote:
>  > When i tried to run my program, i was given this message
>  > (this is the message in part)
>  >
>  > file "C:Python25\lib\site-packages\livewires\games.py", line 503,
> in_tick
>  > self.position = ((self._x + self._dx), (self._y + self._dy))
>  > TypeError: unsopported opernad type(s) for +: 'int' and type'
>  >
>  > What is the nature of my problem?
> 
> It seems that either self._dx or self._dy is not an integer.
> 
>  > But more specifically, what is the 'type' it's referring to?
> 
> A type is the class of a value. For example, int, str and float are all
> types.
> 
> If you show us the full traceback and some of your code we can be more
> helpful.
> 
>   

[Tutor] PyCon 2008 Tutorial Sessions

2008-02-03 Thread Greg Lindstrom
Registration for PyCon 2008 is now open.  Held in Chicago March 14-16 with
"Tutorial Thursday" on March 13 and sprints afterwards, it is expected that
nearly 800 Python enthusiasts will attend at least part of the conference.
It is totally run by volunteers and is affordable for just about anyone.  I
would like to point out the tutorial sessions being offered; all the way
from "Python for the Absolute Beginner", to advanced courses on optimizing
Python programs (with 27 more classes in between).  Click on over to
http://us.pycon.org/2008/about/  for information about the conference.  Here
is a list of the classes being offered along with the instructors scheduled
to present the classes (click to get more information on a session).  It's a
great way to learn more Python.

*Morning Session* (9:00am-12:20pm)

   - Eggs and Buildout Deployment in
Python(Jeff Rush)
   - Python 101 for
Programmers(Steve
Holden)
   - Introduction to
SQLAlchemy(Jonathan
Ellis and Michael Bayer)
   - Python plotting with matplotlib and
pylab(John Hunter)
   - SWIG Master Class
(David Beazley)
   - Secrets of the Framework
Creators(Feihong
Hsu and Kumar McMillan)
   - Introduction to
NumPy(Travis Oliphant
and Eric Jones)
   - Making Small Software for Small People, Sugar/OLPC Coding by
Example(Mike C.
Fletcher)
   - Hands-on Python for the Absolute Beginner
I(Dr. Andrew
Harrington)

*Afternoon Session* (1:20pm-4:40pm)

   - Python 101
(Stuart
Williams)
   - Getting Started with Pylons/TurboGears2 & WSGI: Modern Python Web
   Development  (Mark
   Ramm and Ben Bangert)
   - Advanced 
SQLAlchemy(Jonathan
Ellis and Michael Bayer)
   - Django Tutorial
(Jacob Kaplan-Moss)
   - wxPython I: Intro to GUI Programming with wxPython and
MVC(David
Goodger)
   - Faster Python Programs through Optimization and Extensions
I(Mike
Müller)
   - Tools for Scientific Computing in
Python(Travis
Oliphant and Eric Jones)
   - Generator Tricks for Systems
Programmers(David
Beazley)
   - Basic Python for Java
Programmers(Alex
Martelli and Anna Ravenscroft)
   - Hands-on Python for the Absolute Beginner
II(Dr.
Andrew Harrington)

*Evening Session* (6:10pm-9:30pm)

   - Python 102
(Stuart
Williams)
   - Mastering Pylons and TurboGears 2: Moving Beyond the
Basics.(Mark Ramm,
Ben Bangert)
   - Practical Applications of Agile (Web) Testing
Tools(C. Titus
Brown and Grig Gheorghiu)
   - Django Code Lab
(Jacob Kaplan-Moss,
Adrian Holovaty and James Bennett)
   - wxPython II: Advanced GUI Programming with wxPython and
MVC(David
Goodger)
   - Faster Python Programs through Optimization and Extensions
II(Mike
Müller)
   - Automating Windows Applications with
win32com(Roy H.
Han)
   - Internet Programming with
Python(Wesley
J. Chun)
   - Tail Wags Fangs: What Python Developers Should Know About
Plone(Rob Lineberger)
   - Pygame: Modern game
development(Noah
Kantrowitz and Marc Destefano)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] PHP & Python suggestions....

2008-02-03 Thread GTXY20
Hi all,

First off let me say how helpful and informative this mailing list is. It is
very much appreciated.

Anyway, I am looking for some suggestions for reading up on how to call
Python from PHP scripts, specifically calling from a PHP web application -
PHP will call python script with some arguments and python will run on the
server and return the results within another PHP page.

Once again thanks.

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


Re: [Tutor] Hello and newbie question about "self"

2008-02-03 Thread Alan Gauld
"Patrick" <[EMAIL PROTECTED]> wrote 

> Can anyone please point me to a document that explains "self" in
> layman's terms. 

Try the OOP topic inmy tutorial...

> Or lacking such a doc throw in a much appreciated
> layman's explanation what "self" is and when/where to use it? 

Others have given code samples but a conceptuial explanation 
is that 
a) self is only used in OO programming within metjhods of a class.
b) self refers to the actual instance of the object receiving the 
message with caused the method to be invoked.

Thus if we have a class C with a method m and 3 instances 
a,b and z then when we invoke a.m() self will refer to a and 
when we invoke b.m() self will refer to b. This means that the 
innards of the method can use self to access the instance 
specific data for that invocation.

If you have used C++ at all you might recognise it as the 
same as 'this' in C++ except that in Python you must explicitly 
specify it whereas C++ creates 'this' magically behind the scenes.

See my tutorial for more on this under the heading 
"Using classes".

If you haven't started writing classes yet, you can safely ignore 
it for now!


-- 
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] Hello and newbie question about "self"

2008-02-03 Thread Patrick Lists
Hi Alan,

Alan Gauld wrote:
> "Patrick" <[EMAIL PROTECTED]> wrote 
> 
>> Can anyone please point me to a document that explains "self" in
>> layman's terms. 
> 
> Try the OOP topic inmy tutorial...

Thanks will have a look.

>> Or lacking such a doc throw in a much appreciated
>> layman's explanation what "self" is and when/where to use it? 
> 
> Others have given code samples but a conceptuial explanation 
> is that 
> a) self is only used in OO programming within metjhods of a class.

Now that really helps. I was wondering about that and this answers it.

> b) self refers to the actual instance of the object receiving the 
> message with caused the method to be invoked.

This and reading chapter 23 in the book makes things much clearer now. 
Thanks!

> Thus if we have a class C with a method m and 3 instances 
> a,b and z then when we invoke a.m() self will refer to a and 
> when we invoke b.m() self will refer to b. This means that the 
> innards of the method can use self to access the instance 
> specific data for that invocation.

Even more clear now :)

> If you have used C++ at all you might recognise it as the 
> same as 'this' in C++ except that in Python you must explicitly 
> specify it whereas C++ creates 'this' magically behind the scenes.

Last time I used C++ was (iirc) in 1987 with a Borland product. I recall 
"this" and remember I got stuck on it then too.

> See my tutorial for more on this under the heading 
> "Using classes".

Will do. Thanks for the pointer.

> If you haven't started writing classes yet, you can safely ignore 
> it for now!

I probably won't need to start writing classes but I really want to 
finish the book before I start coding something. I have a small script I 
did in (horrible) bash and look forward to try to implement it in (less 
horrible) Python.

Thanks for your help.

Regards,
Patrick



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


Re: [Tutor] Hello and newbie question about "self"

2008-02-03 Thread Patrick
Hi Alan,

Alan Gauld wrote:
> "Patrick" <[EMAIL PROTECTED]> wrote 
> 
>> Can anyone please point me to a document that explains "self" in
>> layman's terms. 
> 
> Try the OOP topic inmy tutorial...

Thanks will have a look.

>> Or lacking such a doc throw in a much appreciated
>> layman's explanation what "self" is and when/where to use it? 
> 
> Others have given code samples but a conceptuial explanation 
> is that 
> a) self is only used in OO programming within metjhods of a class.

Now that really helps. I was wondering about that and this answers it.

> b) self refers to the actual instance of the object receiving the 
> message with caused the method to be invoked.

This and reading chapter 23 in the book makes things much clearer now.
Thanks!

> Thus if we have a class C with a method m and 3 instances 
> a,b and z then when we invoke a.m() self will refer to a and 
> when we invoke b.m() self will refer to b. This means that the 
> innards of the method can use self to access the instance 
> specific data for that invocation.

Even more clear now :)

> If you have used C++ at all you might recognise it as the 
> same as 'this' in C++ except that in Python you must explicitly 
> specify it whereas C++ creates 'this' magically behind the scenes.

Last time I used C++ was (iirc) in 1987 with a Borland product. I recall
"this" and remember I got stuck on it then too.

> See my tutorial for more on this under the heading 
> "Using classes".

Will do. Thanks for the pointer.

> If you haven't started writing classes yet, you can safely ignore 
> it for now!

I probably won't need to start writing classes but I really want to
finish the book before I start coding something. I have a small script I
did in (horrible) bash and look forward to try to implement it in (less
horrible) Python.

Thanks for your help.

Regards,
Patrick




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


Re: [Tutor] Hello and newbie question about "self"

2008-02-03 Thread Patrick
Hi Michael,

Michael Langford wrote:
> In C, you may have "objectorientedesque" code like the following;
> 
> struct net {
> int foo;
> int bar;
> int baz;
> };
> 
> 
> void populate_net( struct net* mynet, int fooval, int barval)
> {
>   mynet->foo = fooval;
>   mynet->bar = barval;
>   mynet ->baz = fooval * 5;
> }
> 
> int connect_to_net(struct net* mynet)
> {
>  return  open_internet_connection(mynet->foo);
> }
> 
> int main (void)
> {
>   struct net inet;
>   populate_net(&inet,2,2);
> 
>   int returncode = connect_to_net(&inet);
>   printf("%d\n",returncode);
> }

Heh I had to grab my C book and browse up on structs and pointers to get 
  an idea what this was all about :)

> In that batch of C code, you manipulate the struct without fiddling
> with its fields in the user code. You let the functions change its
> values so that they are done correctly.

Ok that makes sense.

> In python, you are doing something similar. However, they make some
> syntactic sugar to make it so you don't have to pass the object in
> explicily. That is what self is.

Got it.

> So the analgous python code is:
> 
> 
> class net(object):
>def __init__(self,fooval,barbal):
>self.foo = fooval
>self.bar = barval
>self.baz = fooval*5
> 
> def connect_to(self):
>return open_internet_connection(self.foo)
> 
> 
> inet = net(2,2)
> returncode = inet.connect_to()
> print returncode
> 
> See how you don't have to pass in the inet object in? Instead you call
> it with the inet.connect_to() function, and the object itself is
> passed in explicitly as self?

Aaah starting to understand now.

> That's all it is.
> 
> Btw, make sure to always include "self". Otherwise you'll be writing a
> class method and it doesn't work the same way.

Thanks for the elaborate explanation!

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


Re: [Tutor] Hello and newbie question about "self"

2008-02-03 Thread Patrick
Hi Kent,

Kent Johnson wrote:
> Patrick wrote:
>> Hi guru's,
>>
>> New to the list. I bought O'Reilly's Learning Python (3rd edition for
>> 2.5) a while back. Slowly making my way through it and was pleasantly
>> surprised that Python seems easier than C. Until...I bumped into the
>> "self" thingy.
> 
> This should be covered by any tutorial. 2nd edition of Learning Python 
> has a section called Methods that introduces self. Also see

Yes it does but all was not clear after I finished the first part 
(chapter 22 in my 3rd edition). Just read chapter 23 and things are 
clearer now. Luckily the start of chapter 24 mentions that it's no big 
deal if I didn't understand everything because they will dive deeper 
into it explaining more.

> http://www.ibiblio.org/swaroopch/byteofpython/read/self.html
> http://hetland.org/writing/instant-hacking.html

Thanks for the links. Will read them when I have some spare cycles.

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


Re: [Tutor] Hello and newbie question about "self"

2008-02-03 Thread Kent Johnson
Tony Cappellini wrote:
>>> http://www.ibiblio.org/swaroopch/byteofpython/read/self.html
> Is there a typo in the contents of this web page?

Yes, you are right, C++ uses 'this'.

Kent

> 
> Should this statement
> 
> Note for C++/Java/C# Programmers
> 
> The self in Python is equivalent to the "self" pointer in C++ and the
> this reference in Java and C#.
> 
> 
> Actually be
> 
> Note for C++/Java/C# Programmers
> 
> The self in Python is equivalent to the "this" pointer in C++ and the
> this reference in Java and C#.
> 
> 
> (I substituted "this" for "self")
> 

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


Re: [Tutor] TypeError

2008-02-03 Thread Kent Johnson
Seon Kang wrote:
> class EnemyPokemon(games.Sprite):
> 
> image = games.load_image("bulbasaur.bmp")
> 
> def __init__(self, speed = 2, odds_change = 200, y = 240):
> """ Initialize the Chef object. """
> super(EnemyPokemon, self).__init__(image = EnemyPokemon.image, y 
> = 240,
>x = 580,
>dy = speed)
>
> self.odds_change = odds_change
> self.time_til_shoot = 0

> enemy_pokemon = EnemyPokemon(games.Sprite)

Here is the problem. The EnemyPokemon.__init__() method takes a speed 
parameter, an int. You are passing it games.Sprite, a type. The speed 
parameter becomes dy for the sprite and causes the error.

Kent

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


Re: [Tutor] Hello and newbie question about "self"

2008-02-03 Thread Tony Cappellini
>>http://www.ibiblio.org/swaroopch/byteofpython/read/self.html
Is there a typo in the contents of this web page?

Should this statement

Note for C++/Java/C# Programmers

The self in Python is equivalent to the "self" pointer in C++ and the
this reference in Java and C#.


Actually be

Note for C++/Java/C# Programmers

The self in Python is equivalent to the "this" pointer in C++ and the
this reference in Java and C#.


(I substituted "this" for "self")
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PHP & Python suggestions....

2008-02-03 Thread Alan Gauld

"GTXY20" <[EMAIL PROTECTED]> wrote

> Anyway, I am looking for some suggestions for reading up on how to 
> call
> Python from PHP scripts, specifically calling from a PHP web 
> application -
> PHP will call python script with some arguments and python will run 
> on the
> server and return the results within another PHP page.

Since PHP is also running on the server then the equivalent of
os.system in Python should work. The equivalent of os.popen
would p[robably be even better, although tyou could use an
output file to exchange data. My (very old) PHP book says:

passthru can be used to put the outoput on the web page or using
back ticks you can capture the output in a variable.

So

$myvar = `python myscript.py`

would store the output of the Python command as a string in $myvar

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] Hello and newbie question about "self"

2008-02-03 Thread Alan Gauld

"Patrick Lists" <[EMAIL PROTECTED]> wrote

> I probably won't need to start writing classes but I really want to
> finish the book before I start coding something.

Such things are personal but I would personally recommend
writing as much code as you can as soon as you can. Do
the examples in the book but treat them as starting points.
Try modifying them and guess what will happen. See if you
are right. If so you understand it! If not go back and dig until
you see why your expectation was wrong and why the code
did what it dd.

This is one of the great things about Python's >>> interactive
prompt, you can learn so much by just doodling with code.

Alan G. 


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