logs was: Re: converting from perl: variable sized unpack
Roman Suzi
rnd at onego.ru
Sun Jul 15 01:53:24 EDT 2001
On Sat, 14 Jul 2001, Eric Hagemann wrote:
>I have a lot of code to parse logs as well -- neat trick (at least I think
>it neat !)
>to overcome this "problem" is
>
>name_of_field0 = 0
>name_of_field1 = 1
>name_of_field2 = 2
>.
>f = line.split()
>.
>
>then access the elements as
>foo = f[name_of_field1]
>
>in my applications I can use the len(f) to determine the type of line I am
>parsing (by its length)
>
>This way you get variable length parsing as well as "named" arguments
O, yes! I do it even more interestingly:
I have a separate module for description of log structure and
have a special object with short name, describing log struct:
# logs.py
class ml(Log):
name_of_field0 = 0
name_of_field1 = 1
name_of_field2 = 2
Then I can use:
from logs import *
rec = split(line)
dosomething(rec[ml.name_of_field0], ...)
And I plan to add something useful to Log so I can do it simpler:
rec = ml(line)
dosomething(rec.name_of_field0, ...)
But am not sure if it is needed yet.
Sincerely yours, Roman Suzi
--
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Sunday, July 15, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "This is Borg. <ESC> is futile <CTRL> is inevitable" _/
More information about the Python-list
mailing list