Re: [Tutor] eazy python question involving functions and parameters

2009-09-21 Thread wesley chun
>> Just think: 4 players left means that this is the semi final.
>
> What a brilliant answer! It tells him how to do it if he just stops and
> thinks but gives nothing away. I love it. :-)

i agree with alan on this. in fact, i can just picture the brackets in
my head already. :-)


another thing from the OP that no one has addressed yet is what *this* means:

> > Write an expression (not a statement!)

to the veterans, we don't have to think twice, but this may not be
obvious to a beginner. basically, an expression is something like 4 *
5, foo(), [x for x in range(5)], etc. that evaluates to *some* Python
object, like a number, instance, or a list, etc. this also includes
function (calls) because it is associated with the single return value
that comes back from every call.

in contrast, a statement is something that has no intrinsic value (nor
associated Python object), such as print, pass, continue, or any
keywords like those.

hope this helps too!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] real world decorators

2009-09-21 Thread Patrick Sabin

John wrote:

Hi,

I think I understand what decorators are and how they work.  Maybe it's just 
me but I don't know where I'd use them in my real world programming.  I see 
how they work with profile or coverage but does anyone have real world uses.  


@classmethod, @staticmethod, @property, @abstractclass, 
@abstractproperty are in my opinion the most import. Some toolkits use 
them too.



I mostly create wxPython apps and don't see where they might apply.



The wx toolkit is designed to work with C++ and wxPython is just a 
language binding for it. As far as I know C++ doesn't support Decorators 
or something similar like it. So there won't be many uses for decorators 
in wx apps.



I know the tutors will enlighten me!


Some ideas for decorators, if you want to play around with them:

@fireandforget
Make a function call in another thread, so you can return
immediately - without returning result. This could be for
example useful to send an email without needing to wait, until
it is sent.
@cache
Cache the results of a function in a dictionary. If the function
is called look up the value of the cache first. This only works
with side-effect free functions of course.
@timeout
Check if the execution time of a function exceeds a timeout
limit. If so raise an exception.
@ignoreexceptions

In my opinion, you should avoid using decorators in productive code, if 
you don't have a good reason to do otherwise. Every decorator adds some 
sort of "magic" to your program and using too much "magic" will make it 
difficult to maintain.


- Patrick

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] eazy python question involving functions and parameters

2009-09-21 Thread Ken Oliver


-Original Message-
>From: wesley chun 
>Sent: Sep 21, 2009 4:49 AM
>To: tutor@python.org
>Subject: Re: [Tutor] eazy python question involving functions and parameters
>
>>> Just think: 4 players left means that this is the semi final.
>>
>> What a brilliant answer! It tells him how to do it if he just stops and
>> thinks but gives nothing away. I love it. :-)
>
>i agree with alan on this. in fact, i can just picture the brackets in
>my head already. :-)

Couldn't this be a king of the hill type of competition with winner of first 
contest takes on a remaining challenger and the winner of that takes on the 
last challenger?

 .
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] real world decorators

2009-09-21 Thread John
On Sunday 20 September 2009 03:43:32 pm Eike Welk wrote:
> On Sunday 20 September 2009, John wrote:
> > Hi,
> >
> > I think I understand what decorators are and how they work.  Maybe
> > it's just me but I don't know where I'd use them in my real world
> > programming.  I see how they work with profile or coverage but does
> > anyone have real world uses.
>
> Frequently used are @classmethod and @staticmethod:
> http://docs.python.org/library/functions.html#classmethod
>
> An other interesting usage of decorators is Phillip J.
> Eby's 'simplegeneric' library, where decorated functions replace big
> if... elif... constructions:
> http://cheeseshop.python.org/pypi/simplegeneric/0.6
>
> But really decorators are just syntactical sugar. They are an elegant
> way to write a special case of a function call.
>
> Kind regards,
> Eike.

Thanks to all that responded on list and off.  You guys have provided much to 
read.  

Thanks

Johnf

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] eazy python question involving functions and parameters

2009-09-21 Thread Corey Richardson
another thing from the OP that no one has addressed yet is what *this* 
means:


>/ > Write an expression (not a statement!)
/
to the veterans, we don't have to think twice, but this may not be
obvious to a beginner. basically, an expression is something like 4 *
5, foo(), [x for x in range(5)], etc. that evaluates to *some* Python
object, like a number, instance, or a list, etc. this also includes
function (calls) because it is associated with the single return value
that comes back from every call.

in contrast, a statement is something that has no intrinsic value (nor
associated Python object), such as print, pass, continue, or any
keywords like those.

hope this helps too!
-- wesley

   Umm.you just completely confused me. What does it do?

<>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] eazy python question involving functions and parameters

2009-09-21 Thread wesley chun
>> an expression is something ... that evaluates to *some* Python object
>>:
>> in contrast, a statement is something that has no intrinsic value
>
>               Umm.you just completely confused me. What does it do?


ok, now *i*'m the one confused... what does *what* do?

both expressions and statements represent executable Python code,
however, a snippet of code like "4*5" has a value... it evaluates to
20, which you can optionally save or (re)use. similarly, a function,
like this one that does nothing: "def foo(): pass", also has a return
value (None) that you can save.

however, calls to print (in Python 2), pass, if, while, for, try,
etc., do NOT have any return value, because they are statements, not
expressions. in fact, if you try to save a value when executing those
commands, you get an error:

In [1]: x = print 'hi'
-
   File "", line 1
 x = print 'hi'
 ^
SyntaxError: invalid syntax

In [2]:

of course, since print is a function in Python 3, it changes from
being a statement (in Python 2) to an expression (in Python 3).

again, the easiest way to differentiate b/w the 2 is whether or not it
results in some Python object. if it doesn, it's an expression; if
not, it's a statement.

clear as mud? hope this somewhat helps...
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] eazy python question involving functions and parameters

2009-09-21 Thread Alan Gauld

And to remove all doubt

wesley chun wrote:

again, the easiest way to differentiate b/w the 2 is whether or not it
results in some Python object. if it doesn, it's an expression; if
not, it's a statement.


Should say ... if it does, it's an expression

You'll find my explanation of expressions in the Functional Programming 
topic in the advanced section of my tutor.


Alan G
http://www.alan-g.me.uk
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] need to hire a tutor for online, i can pay by week or per hr $$$

2009-09-21 Thread daggerdvm

hi i need a tutor for help with python.  I'll ask you basic questions through
email and such.  I can pay by the hour or by week or whatever.

reply here or to bmxx5...@aol.com
-- 
View this message in context: 
http://www.nabble.com/need-to-hire-a-tutor-for-online%2C-i-can-pay-by-week-or-per-hr-%24%24%24-tp25530473p25530473.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] NameError

2009-09-21 Thread kreglet

I keep getting the following error and don't uderstand why:

Traceback (most recent call last):
  File "/home/kreglet/bin/test.py", line 15, in btnStatclick
btnStat.set_label("Pressed")
NameError: global name 'btnStat' is not defined


#!/usr/bin/env python

import gtk
import sys

class NewAppWindow(gtk.Window):

def btnStatclick(self, widget, data=None):
#print status
if self.status == True:
btnStat.set_label("Not Pressed")
self.status =False
print self.status 
elif self.status == False:
btnStat.set_label("Pressed")
self.status =True
print self.status 

def endapp(widget, data=None):
sys.exit()

def __init__(self):
super(NewAppWindow, self).__init__()
self.set_title("New App")
self.set_size_request(1024, 768)
self.set_keep_above(True)
self.set_position(gtk.WIN_POS_CENTER)
self.set_modal(True)
self.status=False

fixed = gtk.Layout()
btnClose = gtk.Button()
btnClose.set_use_stock(True)
btnClose.set_label("gtk-close")
btnClose.show()

btnStat = gtk.Button("Status")

fixed.put(btnStat, 650, 10)
btnStat.connect("clicked", self.btnStatclick)
fixed.put(btnClose, 650, 50)
btnClose.connect("clicked", self.endapp)
  
self.add(fixed)
self.connect("destroy", gtk.main_quit)
self.show_all()

NewAppWindow()
gtk.main()

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need to hire a tutor for online, i can pay by week or per hr $$$

2009-09-21 Thread Kent Johnson
On Mon, Sep 21, 2009 at 4:40 PM, daggerdvm  wrote:
>
> hi i need a tutor for help with python.  I'll ask you basic questions through
> email and such.  I can pay by the hour or by week or whatever.

We do that for free here, just post your questions to the list. We try
not to give answers to homework problems, other than that ask away!

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] NameError

2009-09-21 Thread Wayne
On Mon, Sep 21, 2009 at 4:00 PM, kreglet  wrote:

>
> I keep getting the following error and don't uderstand why:
>
> Traceback (most recent call last):
>  File "/home/kreglet/bin/test.py", line 15, in btnStatclick
>btnStat.set_label("Pressed")
> NameError: global name 'btnStat' is not defined
>
>
> #!/usr/bin/env python
>
> import gtk
> import sys
>
> class NewAppWindow(gtk.Window):
>
>def btnStatclick(self, widget, data=None):
>#print status
>if self.status == True:
>btnStat.set_label("Not Pressed")
>self.status =False
>print self.status
>elif self.status == False:
>btnStat.set_label("Pressed")
>self.status =True
>print self.status


btnStat isn't defined here. I think you should be declaring a self.btnStat
in the __init__ method (which I usually put as the first function in my
class - I'm honestly not sure if it makes a difference), and calling
self.btnStat

Unlike C/C++, python is explicit when dealing with members of the class. I'm
not sure if anything is implied when it comes to namespaces, but I'm pretty
sure it's all gotta be specific.

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] NameError

2009-09-21 Thread wesley chun
On Mon, Sep 21, 2009 at 2:06 PM, Wayne  wrote:
> On Mon, Sep 21, 2009 at 4:00 PM, kreglet  wrote:
>>
>> I keep getting the following error and don't uderstand why:
>>
>> Traceback (most recent call last):
>>  File "/home/kreglet/bin/test.py", line 15, in btnStatclick
>>    btnStat.set_label("Pressed")
>> NameError: global name 'btnStat' is not defined
>>
>>
>> #!/usr/bin/env python
>>
>> import gtk
>> import sys
>>
>> class NewAppWindow(gtk.Window):
>>
>>    def btnStatclick(self, widget, data=None):
>>        #print status
>>        if self.status == True:
>>            btnStat.set_label("Not Pressed")
>>            self.status =False
>>            print self.status
>>        elif self.status == False:
>>            btnStat.set_label("Pressed")
>>            self.status =True
>>            print self.status
>
> btnStat isn't defined here. I think you should be declaring a self.btnStat
> in the __init__ method (which I usually put as the first function in my
> class - I'm honestly not sure if it makes a difference), and calling
> self.btnStat


pretty much what wayne suggested. you need to put the btnStat variable
into your instance's namespace, hence self.btnStat = ... instead of
just btnStat whose namemapping will disappear when __init__()
completes even though the object still exists -- it's just an extra
reference held somewhere else.

by stashing it away into the instance('s dictionary), you'll be able
to access it again in the btnStatclick() callback.

best wishes,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to get homework help (was need to hire a tutor... )

2009-09-21 Thread Tim Bowden
On Mon, 2009-09-21 at 17:04 -0400, Kent Johnson wrote:
> On Mon, Sep 21, 2009 at 4:40 PM, daggerdvm  wrote:
> >
> > hi i need a tutor for help with python.  I'll ask you basic questions 
> > through
> > email and such.  I can pay by the hour or by week or whatever.
> 
> We do that for free here, just post your questions to the list. We try
> not to give answers to homework problems, other than that ask away!
> 
> Kent

I'm new on this list <*waves hello to everyone*>, but I suspect the
trick to getting help here with homework is pretty much the same as most
other tech lists.

1.  Flag the problem as homework.  Don't try and deceive those who are
trying to help.

2.  Outline your understanding of the problem; Both what you do
understand, and what you don't.

3.  Post the code you've tried.  For larger code blocks, try and explain
what you're wanting each section of the code to do.  This is probably
more for your benefit (to help clarify your thinking on the problem)
than for the experts here; They'll have a pretty good handle on what the
code really is doing ;-)

4. If you haven't got code yet, post pseudo code outlining how you might
approach the problem.  This will help develop your algorithm for solving
the problem.  Don't underestimate the importance of this step.  If you
don't have a good algorithm for solving the problem, all the language
syntax in the world won't help.

5.  Accept that you won't be given answers directly, but there is an
enormous amount of patience, help and sympathy for those first starting
out. Everyone who has reached the 'high ground' on the learning journey
likes to see someone new starting out and making headway on the same
journey.  They'll (almost always) gladly extend a hand in help, but
won't do the journey for you.  Anything worthwhile must be earned, and
that means putting in the hard yards.  Be polite, patient and respectful
of those who have gone before you, and they'll keep extending the hand
of help.  Abuse the help, and it won't last long.  A bit like life in
general really.

6.  Don't post the same question in every forum you can find.  Any
decent forum will have enough experts to help out.  And don't abuse
those contributing to the discussion; remember that bit about showing
respect?  And that help being withdrawn?

7.  The advice given on http://www.pubbs.net/python/200909/108644/
shouldn't be ignored (yes, I can use google too).  ESR (love him or
loath him) really was giving good advice in
http://catb.org/~esr/faqs/smart-questions.html.  There is a reason it
gets quoted regularly.  Ignore it at your peril.


Try it on the problem you posted previously; you might not have
understood the hints that were given (probably not if your postings
elsewhere are anything to go by) but if you're fair dinkum give it a go.
If your just wanting to pass your class by getting others to solve your
programming problems for you, get some spine and do it properly or fail
graciously.  The alternative is to be a fraud, and that never gets
respect.

Tim Bowden

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] ex-ftp

2009-09-21 Thread prasad rao
hello  friends
  I am trying to write a class to save a url.page.
But it is not working.It is saving the html.page.But not getting
images.I am unable to show the list (object.links).
Please take a look at it and show me how to rectify it.

import urllib2,ftplib,re
class Collect:
def __init__(self,parent):
self.parent=parent
self.links=[]
self.ims=[]
s=urllib2.urlopen(self.parent)
data=s.read()
self.data=data
a=re.compile ('<[aA].*[\'"](.*)[\'"].*>'); b=re.compile('http://www.asstr.org')
c.save(c.data)
c.bring()
#c.show(c.ims)
c.links

Thanks in advance.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ex-ftp

2009-09-21 Thread Mal Wanstall
On Tue, Sep 22, 2009 at 2:39 PM, prasad rao  wrote:
> hello  friends
>   I am trying to write a class to save a url.page.
> But it is not working.It is saving the html.page.But not getting
> images.I am unable to show the list (object.links).
> Please take a look at it and show me how to rectify it.
>
> import urllib2,ftplib,re
> class Collect:
>     def __init__(self,parent):
>     self.parent=parent
>     self.links=[]
>     self.ims=[]
>     s=urllib2.urlopen(self.parent)
>     data=s.read()
>     self.data=data
>     a=re.compile ('<[aA].*[\'"](.*)[\'"].*>'); b=re.compile(' img[\'"](.+)[\'"].*')
>     try:
>  z=re.search(a,self.data).group(1)
>  self.links.extend(z)
>     except:pass
>     try:
>  y=re.search(b,self.data).group(1)
>  self.ims.extend(y)
>     except:pass
>     return
>
>     def save(self,data):
>     d=open('C:/%s .html'%self.parent[10:15],'w')
>     d.write(data)
>     return
>     def bring(self):
>     ftp=ftplib.FTP(self.parent)
>     ftp.login()
>     for x in self.ims:
>     data=ftp.retlines(x)
>     d=open('C:/%s'%x,'w')
>     d.write(data)
>     return
>
>     def show(self,z):
>     for x in z:
>     print x
>     return
>
>
> c=Collect('http://www.asstr.org')
> c.save(c.data)
> c.bring()
> #c.show(c.ims)
> c.links
>
> Thanks in advance.
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

Might be nice to mention to all who access these emails from work that
the site this script is scraping is not safe for work.
-Mal
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor