Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def tearDown(self):
jsonpickle.handlers.unregister(CustomObject)
def restore(self, obj):
if 'array_ref' in obj:
array_ref = obj['array_ref']
# it is important to return a copy here, because h5 only creates views to the data.
ds = self.file[array_ref]
return ds[:]
else:
values = obj['values']
result = np.empty(len(values), dtype=object)
for i, e in enumerate(values):
result[i] = self.context.restore(e, reset=False)
return result
class NumpyExtractedDtypeHandler(handlers.BaseHandler):
"""
if one extracts a value from a numpy array, the resulting type is numpy.int64 etc.
We convert these values to Python primitives right here.
All float types up to float64 are mapped by builtin.float
All integer (signed/unsigned) types up to int64 are mapped by builtin.int
"""
integers = (np.bool_,
np.int8, np.int16, np.int32, np.int64,
np.uint8, np.uint16, np.uint32, np.uint64)
floats__ = (np.float16, np.float32, np.float64)
np_dtypes = integers + floats__
def __init__(self, context):
super(NumpyExtractedDtypeHandler, self).__init__(context=context)
data = obj.__class__()
flatten = self._flatten_key_value_pair
for k, v in sorted(obj.items(), key=util.itemgetter):
flatten(k, v, data)
# the collections.defaultdict protocol
if hasattr(obj, 'default_factory') and callable(obj.default_factory):
factory = obj.default_factory
if util.is_type(factory):
# Reference the type
value = _mktyperef(factory)
else:
# Create an instance from the factory and assume that the
# resulting instance is a suitable examplar.
value = self._flatten(handlers.CloneFactory(factory()))
data['default_factory'] = value
return data
def get_font_name(index):
fonts = get_font_names()
return fonts[index]
def get_font_index(face):
fonts = get_font_names()
try:
return fonts.index(face)
except ValueError:
return 0
class wxFontHandler(jsonpickle.handlers.BaseHandler):
def flatten(self, obj, data):
data["font"] = font_to_str(obj)
return data
def restore(self, obj):
font_str = obj["font"]
return str_to_font(font_str)
jsonpickle.handlers.registry.register(wx.Font, wxFontHandler)
"""
The flask application package.
"""
from flask import Flask
from flask_cors import CORS, cross_origin
import numpy
import jsonpickle
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
app.trainingstatus = 0
class NumpyFloatHandler(jsonpickle.handlers.BaseHandler):
"""
Automatic conversion of numpy float to python floats
Required for jsonpickle to work correctly
"""
def flatten(self, obj, data):
"""
Converts and rounds a Numpy.float* to Python float
"""
return round(obj,6)
jsonpickle.handlers.registry.register(numpy.int, NumpyFloatHandler)
jsonpickle.handlers.registry.register(numpy.int32, NumpyFloatHandler)
jsonpickle.handlers.registry.register(numpy.int64, NumpyFloatHandler)
jsonpickle.handlers.registry.register(numpy.float, NumpyFloatHandler)
jsonpickle.handlers.registry.register(numpy.float32, NumpyFloatHandler)
jsonpickle.handlers.registry.register(numpy.float64, NumpyFloatHandler)
class NumpyFloatHandler(jsonpickle.handlers.BaseHandler):
"""
Automatic conversion of numpy float to python floats
Required for jsonpickle to work correctly
"""
def flatten(self, obj, data):
"""
Converts and rounds a Numpy.float* to Python float
"""
return round(obj,6)
jsonpickle.handlers.registry.register(numpy.float, NumpyFloatHandler)
jsonpickle.handlers.registry.register(numpy.float32, NumpyFloatHandler)
jsonpickle.handlers.registry.register(numpy.float64, NumpyFloatHandler)
class NumpyIntHandler(jsonpickle.handlers.BaseHandler):
"""
Automatic conversion of numpy float to python floats
Required for jsonpickle to work correctly
"""
def flatten(self, obj, data):
"""
Converts and rounds a Numpy.float* to Python float
"""
return int(obj)
jsonpickle.handlers.registry.register(numpy.int8, NumpyIntHandler)
jsonpickle.handlers.registry.register(numpy.int16, NumpyIntHandler)
jsonpickle.handlers.registry.register(numpy.int32, NumpyIntHandler)
jsonpickle.handlers.registry.register(numpy.int64, NumpyIntHandler)
jsonpickle.handlers.registry.register(numpy.int_, NumpyIntHandler)
def _restore_object(self, obj):
class_name = obj[tags.OBJECT]
cls = loadclass(class_name)
handler = handlers.get(cls, handlers.get(class_name))
if handler is not None: # custom handler
proxy = _Proxy()
self._mkref(proxy)
instance = handler(self).restore(obj)
proxy.reset(instance)
self._swapref(proxy, instance)
return instance
if cls is None:
return self._mkref(obj)
return self._restore_object_instance(obj, cls)