def insert(self, collection, obj): obj_id = self._make_id() obj = create_storage_obj(collection, obj, obj_id) collection_fn = self._get_file(collection) try: # Insert record into file to_json(obj, filename=collection_fn) return obj_id except Exception as e: raise error.InvalidSerialization( f"Problem inserting object into collection: {e}, {obj!r}")
def insert(self, collection, obj): obj_id = self._make_id() obj = create_storage_obj(collection, obj, obj_id) try: obj = to_json(obj) except Exception as e: raise error.InvalidSerialization( f"Problem inserting object into collection: {e}, {obj!r}") with self.lock: self.collections.setdefault(collection, {})[obj_id] = obj return obj_id
def insert_current(self, collection, obj, store_permanently=True): obj_id = self._make_id() obj = create_storage_obj(collection, obj, obj_id) try: obj = to_json(obj) except Exception as e: raise error.InvalidSerialization( f"Problem serializing object for insertion: {e} {obj!r}") with self.lock: self.current[collection] = obj if store_permanently: self.collections.setdefault(collection, {})[obj_id] = obj return obj_id
def to_json(obj, filename=None, append=True, **kwargs): """Convert a Python object to a JSON string. Will handle `datetime` objects as well as `astropy.unit.Quantity` objects. Astropy quantities will be converted to a dict: `{"value": val, "unit": unit}`. Examples: .. doctest:: >>> from panoptes.utils.serializers import to_json >>> from astropy import units as u >>> config = { "name": "Mauna Loa", "elevation": 3397 * u.meter } >>> to_json(config) '{"name": "Mauna Loa", "elevation": "3397.0 m"}' >>> to_json({"numpy_array": np.arange(10)}) '{"numpy_array": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}' >>> from panoptes.utils.time import current_time >>> to_json({"current_time": current_time()}) # doctest: +SKIP '{"current_time": "2019-04-08 22:19:28.402198"}' Args: obj (`object`): The object to be converted to JSON, usually a dict. filename (`str`, optional): Path to file for saving. append (`bool`, optional): Append to `filename`, default True. Setting False will clobber the file. **kwargs: Keyword arguments passed to `json.dumps`. Returns: `str`: The JSON string representation of the object. """ try: json_str = json.dumps(obj, default=serialize_object, **kwargs) except Exception as e: raise error.InvalidSerialization(e) if filename is not None: mode = 'w' if append: mode = 'a' with open(filename, mode) as fn: fn.write(json_str + '\n') return json_str
def insert_current(self, collection, obj, store_permanently=True): obj_id = self._make_id() obj = create_storage_obj(collection, obj, obj_id) current_fn = self._get_file(collection, permanent=False) result = obj_id try: # Overwrite current collection file with obj. to_json(obj, filename=current_fn, append=False) except Exception as e: raise error.InvalidSerialization( f"Problem serializing object for insertion: {e} {current_fn} {obj!r}" ) if not store_permanently: return result else: return self.insert(collection, obj)