Re: [Tutor] Creating a class and calling an exception

2011-06-03 Thread Alan Gauld


Becky Mcquilling ladymcse2...@gmail.com wrote

The Second script written here, always raises the exception and I'm 
missing

why, any advice?
class Log_Parser:
   def __init__(self):
   self.re_backup_status = re.compile(r'^\s+Files\s+:\s+\d',
re.IGNORECASE)

   def log_parse(self, log_file):

 try:

   for line in log1:
 if re_backup_status.search(line):


When accessing an attribute of a class you need to prefix with self.
You are getting a name error here I suspect.


   log1.close()
 except:  print %s %('succeeded:0 failed:1')


But your cattchall exception is hiding the error. If you add
a raise inside the exception handler while debugging it
will print the full error text.

If you did that you would have been pointed to the
line with the error and told the name of the bad name.
Using a catchall exceptioon is OK after the code is
working if you want to shield your users from tracebacks,
but during debugging catchall exceptions are evil,
don't do it.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] Creating a class and calling an exception

2011-06-03 Thread Prasad, Ramit
 class Log_Parser:

It is highly recommended that you use new-style classes which would be
class Log_Parser(object):


Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase  Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase 
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Creating a class and calling an exception

2011-06-03 Thread Becky Mcquilling
Thank you very much.  I should have added that I did that and it ran. I had
one other error in the script thta I missed.  I fixed that and it ran, then
I named them as recommended and it still works and fits better with
conventions, so I learned two things.


On Fri, Jun 3, 2011 at 1:05 AM, Alan Gauld alan.ga...@btinternet.comwrote:


 Becky Mcquilling ladymcse2...@gmail.com wrote

  The Second script written here, always raises the exception and I'm
 missing
 why, any advice?
 class Log_Parser:
   def __init__(self):
   self.re_backup_status = re.compile(r'^\s+Files\s+:\s+\d',
 re.IGNORECASE)

   def log_parse(self, log_file):

 try:
 

   for line in log1:
 if re_backup_status.search(line):


 When accessing an attribute of a class you need to prefix with self.
 You are getting a name error here I suspect.


log1.close()
 except:  print %s %('succeeded:0 failed:1')


 But your cattchall exception is hiding the error. If you add
 a raise inside the exception handler while debugging it
 will print the full error text.

 If you did that you would have been pointed to the
 line with the error and told the name of the bad name.
 Using a catchall exceptioon is OK after the code is
 working if you want to shield your users from tracebacks,
 but during debugging catchall exceptions are evil,
 don't do it.

 HTH,

 --
 Alan Gauld
 Author of the Learn to Program web site
 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating a class

2010-10-05 Thread James Mills
On Wed, Oct 6, 2010 at 4:16 AM, T MURPHY t...@hotmail.com wrote:
 how do i go about creating a class in python.

By using the class keyword.

Example:

class Fruit(object):

   def __init__(self, name)
  self.name = name

class Apple(Fruit):

   def __init__(self):
  super(Apple, self).__init__(apple)

apple = Apple()
print apple.name

For more information, I suggest you start reading
the python tutorial (1)

cheers
James

1. http://docs.python.org/tutorial/

-- 
-- James Mills
--
-- Problems are solved by method
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating a class

2010-10-05 Thread Alan Gauld


T MURPHY t...@hotmail.com wrote 


how do i go about creating a class in python.


class C: pass

is the simplest way. 
But it's not very useful, being empty.


But most tutorials discuss OOP, which one are you using?

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



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