py2/h2o_glm.py
def check_obj_has_good_numbers(obj, hierarchy="", curr_depth=0, max_depth=4, allowNaN=False):
    """Represent instance of a class as JSON.
    Arguments:
    obj -- any object
    Return:
    String that represent JSON-encoded object.
    """
    def serialize(obj, hierarchy="", curr_depth=0):
        """Recursively walk object's hierarchy. Limit to max_depth"""
        if curr_depth>max_depth:
            return

        if isinstance(obj, (bool, int, long, float, basestring)):
            try:
                number = float(obj)
                print "Yay!", hierarchy, number
            except:
                if obj is None:
                    print "Not Yay! how come you're giving me None for a coefficient? %s %s" % (hierarchy, obj)
                elif str(obj)=="":
                    print "Not Yay! how come you're giving me an empty string for a coefficient? %s %s" % (hierarchy, obj)
                else:
                    raise Exception("%s %s %s is not a valid float" % (hierarchy, obj, type(obj)))
                # hack for now
                number = 0.0
            if not allowNaN and math.isnan(number):
                raise Exception("%s %s is a NaN" % (hierarchy, obj))
            if not allowNaN and math.isinf(number):
                raise Exception("%s %s is a Inf" % (hierarchy, obj))
            return number

        elif isinstance(obj, dict):
            obj = obj.copy()
            for key in obj:
                obj[key] = serialize(obj[key], hierarchy + ".%" % key, curr_depth+1)
            return obj

        elif isinstance(obj, (list, tuple)):
            return [serialize(item, hierarchy + "[%s]" % i, curr_depth+1) for (i, item) in enumerate(obj)]

        elif hasattr(obj, '__dict__'):
            return serialize(obj.__dict__, hierarchy, curr_depth+1)

        else:
            return repr(obj) # Don't know how to handle, convert to string

    return (serialize(obj, hierarchy, curr_depth+1))
def check_obj_has_good_numbers(obj, hierarchy="", curr_depth=0, max_depth=4, allowNaN=False):
    """Represent instance of a class as JSON.
    Arguments:
    obj -- any object
    Return:
    String that represent JSON-encoded object.
    """
    def serialize(obj, hierarchy="", curr_depth=0):
        """Recursively walk object's hierarchy. Limit to max_depth"""
        if curr_depth>max_depth:
            return

        if isinstance(obj, (bool, int, long, float, basestring)):
            try:
                number = float(obj)
                print "Yay!", hierarchy, number
            except:
                if obj is None:
                    print "Not Yay! how come you're giving me None for a coefficient? %s %s" % (hierarchy, obj)
                elif str(obj)=="":
                    print "Not Yay! how come you're giving me an empty string for a coefficient? %s %s" % (hierarchy, obj)
                else:
                    raise Exception("%s %s %s is not a valid float" % (hierarchy, obj, type(obj)))
                # hack for now
                number = 0.0
            if not allowNaN and math.isnan(number):
                raise Exception("%s %s is a NaN" % (hierarchy, obj))
            if not allowNaN and math.isinf(number):
                raise Exception("%s %s is a Inf" % (hierarchy, obj))
            return number

        elif isinstance(obj, dict):
            obj = obj.copy()
            for key in obj:
                obj[key] = serialize(obj[key], hierarchy + ".%" % key, curr_depth+1)
            return obj

        elif isinstance(obj, (list, tuple)):
            return [serialize(item, hierarchy + "[%s]" % i, curr_depth+1) for (i, item) in enumerate(obj)]

        elif hasattr(obj, '__dict__'):
            return serialize(obj.__dict__, hierarchy, curr_depth+1)

        else:
            return repr(obj) # Don't know how to handle, convert to string

    return (serialize(obj, hierarchy, curr_depth+1))
def check_obj_has_good_numbers ( obj , hierarchy = "" , curr_depth = 0 , max_depth = 4 , allowNaN = False ) : def serialize ( obj , hierarchy = "" , curr_depth = 0 ) : """Recursively walk object's hierarchy. Limit to max_depth""" if curr_depth > max_depth : return if isinstance ( obj , ( bool , int , long , float , basestring ) ) : try : number = float ( obj ) print "Yay!" , hierarchy , number except : if obj is None : print "Not Yay! how come you're giving me None for a coefficient? %s %s" % ( hierarchy , obj ) elif str ( obj ) == "" : print "Not Yay! how come you're giving me an empty string for a coefficient? %s %s" % ( hierarchy , obj ) else : raise Exception ( "%s %s %s is not a valid float" % ( hierarchy , obj , type ( obj ) ) ) # hack for now number = 0.0 if not allowNaN and math . isnan ( number ) : raise Exception ( "%s %s is a NaN" % ( hierarchy , obj ) ) if not allowNaN and math . isinf ( number ) : raise Exception ( "%s %s is a Inf" % ( hierarchy , obj ) ) return number elif isinstance ( obj , dict ) : obj = obj . copy ( ) for key in obj : obj [ key ] = serialize ( obj [ key ] , hierarchy + ".%" % key , curr_depth + 1 ) return obj elif isinstance ( obj , ( list , tuple ) ) : return [ serialize ( item , hierarchy + "[%s]" % i , curr_depth + 1 ) for ( i , item ) in enumerate ( obj ) ] elif hasattr ( obj , '__dict__' ) : return serialize ( obj . __dict__ , hierarchy , curr_depth + 1 ) else : return repr ( obj ) # Don't know how to handle, convert to string return ( serialize ( obj , hierarchy , curr_depth + 1 ) )
Represent instance of a class as JSON.
    Arguments:
    obj -- any object
    Return:
    String that represent JSON-encoded object.
Represent instance of a class as JSON . Arguments : obj -- any object Return : String that represent JSON - encoded object .