Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8303557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:34:36+00:00 2026-06-08T17:34:36+00:00

I wrote a simple program to read through a log and to parse through

  • 0

I wrote a simple program to read through a log and to parse through and obtain the lowest beginning number (the head) and to print it. I am now editing that program and combining it with a class I wrote to parse an actual logfile. Essentially, as opposed to sorting based off of the simple number from the log from my previous program, I now need to reference the parsed information from one class into another class. I was wondering what the most convenient way to do this. I am a beginner programmer in python and don’t know if I can explicitly reference the class.

Here are the classes.

Parser

class LogLine:

    SEVERITIES = ['EMERG','ALERT','CRIT','ERR','WARNING','NOTICE','INFO','DEBUG']
    severity = 1


    def __init__(self, line):
        try:
            m = re.match(r"^(\d{4}-\d{2}-\d{2}\s*\d{2}:\d{2}:\d{2}),?(\d{3}),?(\s+\[(?:[^\]]+)\])+\s+[A-Z]+\s+(\s?[a-zA-Z0-9\.])+\s?(\((?:\s?\w)+\))\s?(\s?.)+", line)
            timestr, msstr, sevstr, self.filename, linestr, self.message = m.groups()
            self.line = int(linestr)
            self.sev = self.SEVERITIES.index(sevstr)
            self.time = float(calendar.timegm(time.strptime(timestr, "%Y-%m-%d %H:%M:%S,%f"))) + float(msstr)/1000.0
            dt = datetime.strptime(t, "%Y-%m-%d %H:%M:%S,%f")
        except Exception:
            print 'error',self.filename


    def get_time(self):
        return self.time
    def get_severity(self):
        return self.sev
    def get_message(self):
        return self.message
    def get_filename(self):
        return self.filename
    def get_line(self):
        return self.line

Sorter

class LogFile:

    def __init__(self,filepath):
        self.logfile = open(filepath, "r")
        self.head = None

    def __str__(self):
        return "x=" + str(self.x) + "y="+str(self.y)

    def readline(self):
        if self.head != None:
            h = self.head
            self.head = None
            return h
        else:
            return self.logfile.readline().rstrip(' ')

    def get_line(self):
        if self.head == None:
            self.head = self.readline().rstrip(' ')
            return self.head.get.line()
        else:
            return self.head.get.line()

    def close (self):
        self.logfile.close()

I have begun to edit my second class by adding the get_line function. Don’t know if I’m on the right track.

In simpler terms, I need the head to become “LogLine”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:34:37+00:00Added an answer on June 8, 2026 at 5:34 pm

    It is okay to use one class from another class. You have one class that parses a single line from a log file and builds an object that represents the line; and you have another class that reads lines from a log file. It would be very natural for the second class to call the first class.

    Here is a very simple class that reads all lines from a log file and builds a list:

    class LogFile(object):
        def __init__(self,filepath):
            with open(filepath, "r") as f:
                self.lst = [LogLine(line) for line in f]
    

    You can see that self.lst is being set to a list of lines from the input log file, but not just the text of the line; the code is calling LogLine(line) to store instances of LogLine. If you want, you can sort the list after you build it:

    self.lst.sort(key=LogLine.get_line)
    

    If the log files are very large, it might not be practical to build the list. You have a .get_line() method function, and we can use that:

    class LogFile(object):
        def __init__(self,filepath):
            self.logfile = open(filepath, "r")
    
        def get_line(self):
            try:
                line = next(self.logfile)  # get next line from open file object
                return LogLine(line)
            except StopIteration:  # next() raises this when you reach the end of the file
                return None  # return 
    
        def close(self):
            self.logfile.close()
    

    An open file object (returned by the open() function) can be iterated. We can call next() on this object and it will give us the next input line. When the end of file is reached, Python will raise StopIteration to signal the end of the file.

    Here the code will catch the StopIteration exception and return None when the end of the log file is reached. But I think this isn’t the best way to handle this problem. Let’s make the LogFile class work in for loops and such:

    class LogFile(object):
        def __init__(self,filepath):
            self.f = open(filepath)
    
        def __next__(self):  # Python 3.x needs this to be named "__next__"
            try:
                line = next(self.f)
                return LogLine(line)
            except StopIteration:
                # when we reach the end of input, close the file object
                self.f.close()
                # re-raise the exception
                raise
        next = __next__  # Python 2.x needs this to be named "next"
    

    A for loop in Python will repeatedly call the .__next__() method function (Python 3.x) or else the .next() method function (Python 2.x) until the StopIteration exception is raised. Here we have defined both method function names so this code should work in Python 2.x or in Python 3.x.

    Now you can do this:

    for ll in LogFile("some_log_file"):
        ... # do something with ll, which will always be a LogLine instance
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a simple program for work that does the following: read
I am trying to write a simple program in C# that will read data
I wrote a simple program to solve the math problem: A^2+B^2 = 12 A*B
I wrote a simple program to test a theory that finally block will always
I wrote a simple program in java web forms but i am receiving the
I wrote a simple test program for opencv to see if it's working after
I wrote a simple C program: #include <unistd.h> #include <stdio.h> int main( int argc,
A while back I wrote a simple python program to brute-force the single solution
I recently wrote a program that used a simple producer/consumer pattern. It initially had
I just wrote a simple C++ program in Visual Studio 2010 and I use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.