def set_patches(self, key, distance, patches): '''Set the specified object attribute as a list of patches. distance is a double. patches is a list of (upper_left_coord, lower_right_coord) tuples, where a coordinate is an (x, y) tuple. The key name should probably be _filter.%s.patches, where %s is the filter name from Session.''' self.set_binary(key, PatchesAttributeCodec().encode((distance, patches)))
def _make_object_json(application, search_key, object_key, obj): '''Convert an object attribute dict into a dict suitable for JSON encoding.''' result = {} for k, v in obj.iteritems(): data = None # Inline known attribute types that can be represented in JSON if k.endswith('.int'): data = IntegerAttributeCodec().decode(v) elif k.endswith('.double'): data = DoubleAttributeCodec().decode(v) elif k.endswith('.patches'): distance, patches = PatchesAttributeCodec().decode(v) data = { 'distance': distance, 'patches': [{ 'x0': tl[0], 'y0': tl[1], 'x1': br[0], 'y1': br[1] } for tl, br in patches], } else: # Treat remaining attributes as strings if: they don't have # a suffix representing a known binary type, they can be decoded # by the string codec (i.e., their last byte is 0), they are # valid UTF-8, and they are not the '' (object data) attribute. try: _base, suffix = k.rsplit('.', 1) except ValueError: suffix = None try: if k != '' and suffix not in ('jpeg', 'png', 'rgbimage', 'binary'): data = StringAttributeCodec().decode(v).decode('UTF-8') except ValueError: pass if data is not None: result[k] = { 'data': data, } else: result[k] = { 'raw_url': application.reverse_url('attribute-raw', search_key, object_key, k), 'image_url': application.reverse_url('attribute-image', search_key, object_key, k), } result['_ResultURL'] = { 'data': application.reverse_url('result', search_key, object_key), } _result_object_schema.validate(result) return result
def get_patches(self, key): '''Get the specified object attribute as a list of patches. Returns (distance, patches), where patches is a tuple of (upper_left_coord, lower_right_coord) tuples and a coordinate is an (x, y) tuple.''' return PatchesAttributeCodec().decode(self.get_binary(key))