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 #2425 from mihail911/jsonhelpers
Browse files Browse the repository at this point in the history
Jsonhelpers
  • Loading branch information
scottpurdy committed Aug 12, 2015
2 parents 9d05a85 + c73be6b commit 81a2c15
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions nupic/data/jsonhelpers.py
Expand Up @@ -33,6 +33,8 @@
#
# TODO: offer a combined json parsing/validation function that applies
# defaults from the schema
# TODO: duplicate of 'validate', 'ValidationError', 'loadJSONValueFromFile'
# in swarming.hypersearch.utils -- will want to remove that later

import json
import math
Expand Down
6 changes: 3 additions & 3 deletions nupic/swarming/HypersearchWorker.py
Expand Up @@ -32,16 +32,16 @@
import StringIO
import traceback

from nupic.data import jsonhelpers
from nupic.swarming.hypersearch.utils import clippedObj
from nupic.support import initLogging
from nupic.support.configuration import Configuration
from nupic.support.ExtendedLogger import ExtendedLogger
from nupic.swarming.hypersearch.errorcodes import ErrorCodes
from nupic.swarming.hypersearch.utils import clippedObj, validate
from nupic.database.ClientJobsDAO import ClientJobsDAO
from HypersearchV2 import HypersearchV2



class HypersearchWorker(object):
""" The HypersearchWorker is responsible for evaluating one or more models
within a specific Hypersearch job.
Expand Down Expand Up @@ -298,7 +298,7 @@ def run(self):
jsonSchemaPath = os.path.join(os.path.dirname(__file__),
"jsonschema",
"jobParamsSchema.json")
jsonhelpers.validate(jobParams, schemaPath=jsonSchemaPath)
validate(jobParams, schemaPath=jsonSchemaPath)


hsVersion = jobParams.get('hsVersion', None)
Expand Down
62 changes: 62 additions & 0 deletions nupic/swarming/hypersearch/utils.py
Expand Up @@ -34,12 +34,15 @@
import types
import signal
import uuid
import validictory

from nupic.database.ClientJobsDAO import (
ClientJobsDAO, InvalidConnectionException)

# TODO: Note the function 'rUpdate' is also duplicated in the
# nupic.data.dictutils module -- we will eventually want to change this
# TODO: 'ValidationError', 'validate', 'loadJSONValueFromFile' duplicated in
# nupic.data.jsonhelpers -- will want to remove later

class JobFailException(Exception):
""" If a model raises this exception, then the runModelXXX code will
Expand Down Expand Up @@ -667,6 +670,65 @@ def clippedObj(obj, maxElementSize=64):



class ValidationError(validictory.ValidationError):
pass



def validate(value, **kwds):
""" Validate a python value against json schema:
validate(value, schemaPath)
validate(value, schemaDict)
value: python object to validate against the schema
The json schema may be specified either as a path of the file containing
the json schema or as a python dictionary using one of the
following keywords as arguments:
schemaPath: Path of file containing the json schema object.
schemaDict: Python dictionary containing the json schema object
Returns: nothing
Raises:
ValidationError when value fails json validation
"""

assert len(kwds.keys()) >= 1
assert 'schemaPath' in kwds or 'schemaDict' in kwds

schemaDict = None
if 'schemaPath' in kwds:
schemaPath = kwds.pop('schemaPath')
schemaDict = loadJsonValueFromFile(schemaPath)
elif 'schemaDict' in kwds:
schemaDict = kwds.pop('schemaDict')

try:
validictory.validate(value, schemaDict, **kwds)
except validictory.ValidationError as e:
raise ValidationError(e)



def loadJsonValueFromFile(inputFilePath):
""" Loads a json value from a file and converts it to the corresponding python
object.
inputFilePath:
Path of the json file;
Returns:
python value that represents the loaded json value
"""
with open(inputFilePath) as fileObj:
value = json.load(fileObj)

return value



def sortedJSONDumpS(obj):
"""
Return a JSON representation of obj with sorted keys on any embedded dicts.
Expand Down

0 comments on commit 81a2c15

Please sign in to comment.