title: Log parser with generator
body: <pre class="lang-py prettyprint-override"><code>class Parser:
  
      def __init__(self, file_name):
          self.file_name = file_name
          self.dates = {}
          self.j = 17
          self.date_str = ''
          self.prev = None
          self.i = 0
  
      def parse_file(self):
          with open(self.file_name, 'r', encoding='utf-8') as file:
              yield from file
  
      def filter_line(self, line):
          if 'NOK' in line:
              return line
  
      def get_date(self, line):
          try:
              self.date_str = line[1:self.j]
          except AttributeError:
              return None
          except ValueError:
              return None
          return self.date_str
  
      def run(self):
          generator = self.parse_file()
          count = 1
          prev = None
          while True:
              try:
                  line = next(generator)
              except StopIteration:
                  if self.get_date(prev):
                      print(self.get_date(prev), ' NOK ' + str(count) + '\n')
                  break
              if self.filter_line(line):
                  if prev is None:
                      if self.get_date(line):
                          prev = line
                  else:
                      if self.get_date(line):
                          if self.get_date(line) == self.get_date(prev):
                              count += 1
                              prev = line
                          else:
                              # print(self.get_date(prev) + ' NOK ' + str(count) + '\n')
                              count = 1
                              prev = line
                              yield self.get_date(prev),count
  </code></pre>
  <p>How can I simplify the last <code>else</code>, using <code>yield</code>, to output the calculated data to the console? Using <code>print</code> works.</p>
  <p>The result in console must be :</p>
  <blockquote>
  <p><code>[2018-05-17 01:57] 1234</code></p>
  </blockquote>
  <p>Maybe you can simplify the code even more, for example, change the <code>parse_file</code> function by removing <code>yield</code> from there.</p>