Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2397 from mihail911/Utils
Browse files Browse the repository at this point in the history
Utils
  • Loading branch information
scottpurdy committed Aug 10, 2015
2 parents cb82655 + 8f93f23 commit 7871a7a
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 53 deletions.
42 changes: 0 additions & 42 deletions nupic/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,45 +566,6 @@ def handleErrorPatch(*args, **kwargs):
handler.handleError = handleErrorPatch

return



def clippedObj(obj, maxElementSize=64):
"""
Return a clipped version of obj suitable for printing, This
is useful when generating log messages by printing data structures, but
don't want the message to be too long.
If passed in a dict, list, or namedtuple, each element of the structure's
string representation will be limited to 'maxElementSize' characters. This
will return a new object where the string representation of each element
has been truncated to fit within maxElementSize.
"""

# Is it a named tuple?
if hasattr(obj, '_asdict'):
obj = obj._asdict()


# Printing a dict?
if isinstance(obj, dict):
objOut = dict()
for key,val in obj.iteritems():
objOut[key] = clippedObj(val)

# Printing a list?
elif hasattr(obj, '__iter__'):
objOut = []
for val in obj:
objOut.append(clippedObj(val))

# Some other object
else:
objOut = str(obj)
if len(objOut) > maxElementSize:
objOut = objOut[0:maxElementSize] + '...'

return objOut



Expand Down Expand Up @@ -744,6 +705,3 @@ def aggregationDivide(dividend, divisor):

else:
return float(dividendMonthSec['seconds']) / divisorMonthSec['seconds']



2 changes: 1 addition & 1 deletion nupic/swarming/DummyModelRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from nupic.frameworks.opf.modelfactory import ModelFactory
from nupic.frameworks.opf import opfhelpers
from nupic.frameworks.opf.opfutils import ModelResult
from nupic.swarming import utils
from nupic.swarming.hypersearch import utils
from nupic.swarming.ModelRunner import OPFModelRunner


Expand Down
6 changes: 3 additions & 3 deletions nupic/swarming/HypersearchV2.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
from nupic.data import dictutils
from nupic.frameworks.opf import opfhelpers
from nupic.frameworks.opf.opfutils import InferenceType
from nupic.support import clippedObj
from nupic.swarming.hypersearch.utils import clippedObj
from nupic.support.serializationutils import sortedJSONDumpS
from nupic.support.configuration import Configuration
from nupic.swarming.hypersearch.errorcodes import ErrorCodes
from nupic.database.ClientJobsDAO import (
ClientJobsDAO, InvalidConnectionException)
from nupic.swarming.utils import (runModelGivenBaseAndParams,
runDummyModel)
from nupic.swarming.hypersearch.utils import (runModelGivenBaseAndParams,
runDummyModel)
from nupic.swarming.permutationhelpers import *
from nupic.swarming.exp_generator.ExpGenerator import expGenerator

Expand Down
2 changes: 1 addition & 1 deletion nupic/swarming/HypersearchWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import traceback

from nupic.data import jsonhelpers
from nupic.support import clippedObj
from nupic.swarming.hypersearch.utils import clippedObj
from nupic.support import initLogging
from nupic.support.configuration import Configuration
from nupic.support.ExtendedLogger import ExtendedLogger
Expand Down
2 changes: 1 addition & 1 deletion nupic/swarming/ModelRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from nupic.swarming.hypersearch.errorcodes import ErrorCodes
from nupic.database.ClientJobsDAO import ClientJobsDAO
from nupic.swarming import regression
from nupic.swarming import utils
from nupic.swarming.hypersearch import utils



Expand Down
2 changes: 1 addition & 1 deletion nupic/swarming/ModelTerminator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import functools

from nupic.swarming.utils import PeriodicActivityRequest
from nupic.swarming.hypersearch.utils import PeriodicActivityRequest



Expand Down
39 changes: 39 additions & 0 deletions nupic/swarming/utils.py → nupic/swarming/hypersearch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,42 @@ def generatePersistentJobGUID():
"""
return "JOB_UUID1-" + str(uuid.uuid1())



def clippedObj(obj, maxElementSize=64):
"""
Return a clipped version of obj suitable for printing, This
is useful when generating log messages by printing data structures, but
don't want the message to be too long.
If passed in a dict, list, or namedtuple, each element of the structure's
string representation will be limited to 'maxElementSize' characters. This
will return a new object where the string representation of each element
has been truncated to fit within maxElementSize.
"""

# Is it a named tuple?
if hasattr(obj, '_asdict'):
obj = obj._asdict()


# Printing a dict?
if isinstance(obj, dict):
objOut = dict()
for key,val in obj.iteritems():
objOut[key] = clippedObj(val)

# Printing a list?
elif hasattr(obj, '__iter__'):
objOut = []
for val in obj:
objOut.append(clippedObj(val))

# Some other object
else:
objOut = str(obj)
if len(objOut) > maxElementSize:
objOut = objOut[0:maxElementSize] + '...'

return objOut
3 changes: 2 additions & 1 deletion nupic/swarming/permutations_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@

from nupic.support import object_json as json
import nupic.database.ClientJobsDAO as cjdao
from nupic.swarming import HypersearchWorker, utils
from nupic.swarming import HypersearchWorker
from nupic.swarming.hypersearch import utils
from nupic.swarming.HypersearchV2 import HypersearchV2
from nupic.swarming.exp_generator.ExpGenerator import expGenerator

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/nupic/opf/expgenerator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
TestCaseBase as HelperTestCaseBase)
from nupic.swarming import HypersearchWorker
from nupic.swarming.permutationhelpers import PermuteChoices
from nupic.swarming.utils import generatePersistentJobGUID
from nupic.swarming.hypersearch.utils import generatePersistentJobGUID
from nupic.frameworks.opf.expdescriptionapi import OpfEnvironment
from nupic.swarming.exp_generator import ExpGenerator
from nupic.frameworks.opf.opfutils import (InferenceType,
Expand Down
2 changes: 1 addition & 1 deletion tests/regression/run_opf_benchmarks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from nupic.support.configuration import Configuration
from nupic.support.unittesthelpers.testcasebase import unittest
from nupic.swarming import permutations_runner
from nupic.swarming.utils import generatePersistentJobGUID
from nupic.swarming.hypersearch.utils import generatePersistentJobGUID



Expand Down
2 changes: 1 addition & 1 deletion tests/swarming/nupic/swarming/swarming_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
TestCaseBase as HelperTestCaseBase)
from nupic.swarming import HypersearchWorker
from nupic.swarming.api import getSwarmModelParams, createAndStartSwarm
from nupic.swarming.utils import generatePersistentJobGUID
from nupic.swarming.hypersearch.utils import generatePersistentJobGUID
from nupic.swarming.DummyModelRunner import OPFDummyModelRunner

DEFAULT_JOB_TIMEOUT_SEC = 60 * 2
Expand Down

0 comments on commit 7871a7a

Please sign in to comment.