<commit_before>#! /usr/bin/env python
# ----------------------------------------------------------------------
#  Copyright (C) 2010 Numenta Inc. All rights reserved.
#
#  The information and source code contained herein is the
#  exclusive property of Numenta Inc. No part of this software
#  may be used, reproduced, stored or distributed in any form,
#  without explicit written authorization from Numenta Inc.
# ----------------------------------------------------------------------

from __future__ import with_statement
import os
from nupic.support.consoleprinter import ConsolePrinterMixin, Tee

class MyClass(ConsolePrinterMixin):
  def __init__(self):
    ConsolePrinterMixin.__init__(self)

  def run(self):
    for i in xrange(0, 4):
      self.cPrint(i, "message at level %d", i)



if __name__ == "__main__":
  mydir = os.path.dirname(os.path.abspath(__file__))

  filename = os.path.abspath("console_output.txt")
  if os.path.exists(filename):
    os.remove(filename)

  # Capture output to a file so that we can compare it
  with Tee(filename):
    c1 = MyClass()
    print "Running with default verbosity"
    c1.run()
    print

    print "Running with verbosity 2"
    c1.consolePrinterVerbosity = 2
    c1.run()
    print

    print "Running with verbosity 0"
    c1.consolePrinterVerbosity = 0
    c1.run()
    print

    c1.cPrint(0, "Message %s two %s", "with", "args")
    c1.cPrint(0, "Message with no newline", newline=False)
    c1.cPrint(0, " Message with newline")
    c1.cPrint(0, "Message with %s and %s", "no newline", "args", newline=False)
    c1.cPrint(0, " Message with %s and %s", "newline", "args")


    print "Done"

  caughtException = False
  try:
    c1.cPrint(0, "Message", badkw="badvalue")
  except KeyError, e:
    caughtException = True
  if not caughtException:
    raise Exception("Bad keyword for cPrint should have been caught")



  referenceFilename = os.path.join(mydir, "testconsoleprinter_output.txt")
  expected = open(referenceFilename).readlines()
  actual = open(filename).readlines()

  print ("Comparing files '%s'" % referenceFilename)
  print ("and             '%s'" % filename)


  if len(expected) != len(actual):
    raise Exception("Expected %d lines of output got %d lines" % (len(expected), len(actual)))

  for i in xrange(len(expected)):
    if expected[i].strip() != actual[i].strip():
      raise Exception("Line %d of output differs from expected" % i)

  print "Output is correct"<commit_msg>Started converting to unittest format<commit_after>#!/usr/bin/env python
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2014, Numenta, Inc.  Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------

from __future__ import with_statement
import os
import unittest2 as unittest

from nupic.support.consoleprinter import ConsolePrinterMixin, Tee



# Class used for testing

class MyClass(ConsolePrinterMixin):


  def __init__(self):
    ConsolePrinterMixin.__init__(self)


  def run(self):
    for i in xrange(0, 4):
      self.cPrint(i, "message at level %d", i)



class ConsolePrinterTest(unittest.TestCase):


  def testPrint(self):
    mydir = os.path.dirname(os.path.abspath(__file__))

    filename = os.path.abspath("console_output.txt")
    if os.path.exists(filename):
      os.remove(filename)

    # Capture output to a file so that we can compare it
    with Tee(filename):
      c1 = MyClass()
      print "Running with default verbosity"
      c1.run()
      print

      print "Running with verbosity 2"
      c1.consolePrinterVerbosity = 2
      c1.run()
      print

      print "Running with verbosity 0"
      c1.consolePrinterVerbosity = 0
      c1.run()
      print

      c1.cPrint(0, "Message %s two %s", "with", "args")
      c1.cPrint(0, "Message with no newline", newline=False)
      c1.cPrint(0, " Message with newline")
      c1.cPrint(0, "Message with %s and %s", "no newline", "args", newline=False)
      c1.cPrint(0, " Message with %s and %s", "newline", "args")


      print "Done"

    caughtException = False
    try:
      c1.cPrint(0, "Message", badkw="badvalue")
    except KeyError, e:
      caughtException = True
    if not caughtException:
      raise Exception("Bad keyword for cPrint should have been caught")



    referenceFilename = os.path.join(mydir, "testconsoleprinter_output.txt")
    expected = open(referenceFilename).readlines()
    actual = open(filename).readlines()

    print ("Comparing files '%s'" % referenceFilename)
    print ("and             '%s'" % filename)


    if len(expected) != len(actual):
      raise Exception("Expected %d lines of output got %d lines" % (len(expected), len(actual)))

    for i in xrange(len(expected)):
      if expected[i].strip() != actual[i].strip():
        raise Exception("Line %d of output differs from expected" % i)



if __name__ == "__main__":
  unittest.main()
{'fraction_non_alphanumeric': 0.15279241306638566, 'fraction_numerical': 0.010186160871092378, 'mean_word_length': 2.832436069986541, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '671235', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1668, 'n_words': 561}