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

>
> "Becky Mcquilling"  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 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 Alan Gauld


"Becky Mcquilling"  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] Creating a class and calling an exception

2011-06-03 Thread Becky Mcquilling
So I'm new to doing creating classes in python and took a script I had
written to parse a log file and tried to create a class just to understand
it a little better. The first script below works fine.

It looks at the log, checks the date and if it's too old, raises the
exception and returns a set value or if the file doesn't exist it raises the
exception.  If the file is there and is no more than a day old, it parses
the log and returns what was backed up by the utility I'm using.

The Second script written here, always raises the exception and I'm missing
why, any advice?

*FIRST SCRIPT*

import re, os
import stat
import sys
from datetime import date, timedelta

log_file = 'd:/backup_logs/backups.log'
re_backup_status = re.compile(r'^\s+Files\s+:\s+\d', re.IGNORECASE)
"""Example of return from regexFiles:  Succeeded"168933 failed:0 """

def main():

  try:
s = os.stat(log_file)
mtime = date.fromtimestamp(s[stat.ST_MTIME])
yesterday = date.today() - timedelta(days=1)
mode = s[stat.ST_MODE]


if stat.S_ISREG(mode):
  if mtime < yesterday:
print "%s" % ('succeeded:0 failed:1')
  else:
log1 = (open(log_file, 'r'))
for line in log1:
  if re_backup_status.search(line):
status_out = line.split()
print '%s succeeded:%s failed:%s'  % (
  'map:files', status_out[2], status_out[6])
log1.close()
  except:  print "%s" %('succeeded:0 failed:1')

if __name__ == '__main__':
  main()
*
*
*SECOND SCRIPT*
*
*
import re
import os
import stat
import sys
from datetime import date, timedelta

class Log_Parser:
def __init__(self):
self.re_backup_status = re.compile(r'^\s+Files\s+:\s+\d',
re.IGNORECASE)
"""Example of return from regexFiles:  Succeeded"168933 failed:0 """

def log_parse(self, log_file):

  try:
s = os.stat(log_file)
mtime = date.fromtimestamp(s[stat.ST_MTIME])
yesterday = date.today() - timedelta(days=1)
mode = s[stat.ST_MODE]

if stat.S_ISREG(mode):
  print ('file is there')
  if mtime < yesterday:
print('file is the wrong date')
print "%s" % ('succeeded:0 failed:1')
  else:
log1 = (open(log_file, 'r'))
for line in log1:
  if re_backup_status.search(line):
status_out = line.split()
print '%s succeeded:%s failed:%s'  % (
  'map:files', status_out[2], status_out[6])
log1.close()
  except:  print "%s" %('succeeded:0 failed:1')


backup_log_parser = Log_Parser()
backup_log_parser.log_parse('d:/backup_logs/backups.log')
___
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"  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


Re: [Tutor] creating a class

2010-10-05 Thread James Mills
On Wed, Oct 6, 2010 at 4:16 AM, T MURPHY  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


[Tutor] creating a class

2010-10-05 Thread T MURPHY
how do i go about creating a class in python.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor