Nubie question: how to not pass "self" in call to seek() function ?

Barak, Ron Ron.Barak at lsi.com
Thu Jan 8 10:44:16 EST 2009


Hi Mark,

I think my open_file() - that is called in __init__ - assures that self.input_file is a regular text file,
regardless if filename is a gz or a regular text file.

My Python is Python 2.5.2.

Bye,
Ron.

-----Original Message-----
From: Mark Tolonen [mailto:metolone+gmane at gmail.com]
Sent: Thursday, January 08, 2009 17:16
To: python-list at python.org
Subject: Re: Nubie question: how to not pass "self" in call to seek() function ?


"Barak, Ron" <> wrote in message news:7F0503CD69378F49BE0DC30661C6CCF602494BA6 at enbmail01.lsi.com...
> Hi,
>
> I am getting the error TypeError: seek() takes exactly 2 arguments (3
> given), namely:
>
> $ ./_LogStream.py
> Traceback (most recent call last):
>   File "./_LogStream.py", line 47, in <module>
>     log_stream.last_line_loc_and_contents()
>   File "./_LogStream.py", line 20, in last_line_loc_and_contents
>     self.input_file.seek(-1, 2) # grab the last character
> TypeError: seek() takes exactly 2 arguments (3 given)
>
> When I run the below code.
>
> I understand that the extra argument is the "self", but I don't know
> how to change my class to make the seek(-1,2) work.
>
> Could you help ?

Perhaps the extra agrument is 2.  You don't mention your Python version, but in Python 2.6.1 gzip files don't support seeking from the end.  An older
(your?) version of Python may not be providing as helpful an error message.

>>> f=gzip.GzipFile('blah.gz','r')
>>> f.seek(-1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\dev\python\lib\gzip.py", line 368, in seek
    raise ValueError('Seek from end not supported')
ValueError: Seek from end not supported

-Mark


>
> Thanks,
> Ron.
>
> $ cat _LogStream.py
> #!/usr/bin/env python
>
> import gzip
> import sys
>
> from Debug import _line as line
>
> class LogStream():
>
>     def __init__(self, filename):
>         self.filename = filename
>         self.input_file = self.open_file(filename)
>         self.index_increment = 10
>         self.last_line_offset = -1
>
>     def last_line_loc_and_contents(self, estimated_line_size=1024):
>
>         assert estimated_line_size > 0
>         file_size = len(self.input_file.read())
>         self.input_file.seek(-1, 2) # grab the last character
>
>         if self.input_file.read(1) == '\n': # a "proper" text file
>             file_size -= 1
>
>     def open_file(self, in_file):
>         """
>         The gzip module checks if the input file is a gzipped file,
> only at the read stage.
>         This is why the f.readline() is needed.
>         """
>         try:
>             f = gzip.GzipFile(in_file, "r")
>             f.readline()
>         except IOError:
>             f = open(in_file, "r")
>             f.readline()
>
>         f.seek(0)
>         return(f)
>
>
> if __name__ == "__main__":
>     filename =
> "../save_state/hp/save_state-ssp8400-0709R00004_081126-121659/var/log\\sac.log.4.gz"
>     log_stream = LogStream(filename)
>     log_stream.limit_ = 1000
>     log_stream.index_increment = 12
>
>     log_stream.last_line_loc_and_contents()




--
http://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list