33
33
db = SQLAlchemy (session_options = {'autoflush' : False }) # pylint: disable=C0103
34
34
35
35
36
- def load_pickle_obj (obj ):
37
- return pickle .loads (obj )
38
-
39
-
40
- def load_npz_from_db (obj ):
41
- bytestream = io .BytesIO (obj )
42
- bytestream .seek (0 )
43
- return np .load (bytestream )['array' ]
44
-
45
-
46
- class PickleToNpz (types .TypeDecorator ):
36
+ class Npz (types .TypeDecorator ):
47
37
"""Marshals a pickles already in the database to npz if not already npz"""
48
38
impl = types .LargeBinary
49
39
@@ -56,15 +46,11 @@ def process_bind_param(self, value, dialect):
56
46
return bytestream .read ()
57
47
58
48
def process_result_value (self , value , dialect ):
59
- # Some columns are still pickles,
60
- # others have been converted to NPZ
61
49
if value is None :
62
50
return None
63
- try :
64
- result = load_pickle_obj (value )
65
- except pickle .UnpicklingError :
66
- result = load_npz_from_db (value )
67
- return result
51
+ bytestream = io .BytesIO (value )
52
+ bytestream .seek (0 )
53
+ return np .load (bytestream )['array' ]
68
54
69
55
70
56
@compiles (db .PickleType , 'mysql' )
@@ -563,7 +549,7 @@ class RawFrame(db.Model):
563
549
project_id = db .Column (db .Integer , db .ForeignKey ('projects.id' ),
564
550
primary_key = True , nullable = False )
565
551
frame_id = db .Column (db .Integer , primary_key = True , nullable = False )
566
- frame = db .Column (PickleToNpz )
552
+ frame = db .Column (Npz )
567
553
568
554
def __init__ (self , frame_id , frame ):
569
555
self .frame_id = frame_id
@@ -585,7 +571,7 @@ class RGBFrame(db.Model):
585
571
project_id = db .Column (db .Integer , db .ForeignKey ('projects.id' ),
586
572
primary_key = True , nullable = False )
587
573
frame_id = db .Column (db .Integer , primary_key = True , nullable = False )
588
- frame = db .Column (PickleToNpz )
574
+ frame = db .Column (Npz )
589
575
590
576
def __init__ (self , frame_id , frame ):
591
577
self .frame_id = frame_id
@@ -683,7 +669,7 @@ class LabelFrame(db.Model):
683
669
project_id = db .Column (db .Integer , db .ForeignKey ('projects.id' ),
684
670
primary_key = True , nullable = False )
685
671
frame_id = db .Column (db .Integer , primary_key = True , nullable = False )
686
- frame = db .Column (MutableNdarray .as_mutable (PickleToNpz ))
672
+ frame = db .Column (MutableNdarray .as_mutable (Npz ))
687
673
688
674
actions = association_proxy ('frame_actions' , 'action' )
689
675
@@ -787,7 +773,7 @@ class FrameMemento(db.Model):
787
773
project_id = db .Column (db .Integer )
788
774
action_id = db .Column (db .Integer )
789
775
frame_id = db .Column (db .Integer )
790
- frame_array = db .Column (PickleToNpz )
776
+ frame_array = db .Column (Npz )
791
777
792
778
action = db .relationship ("Action" , backref = "action_frames" )
793
779
frame = db .relationship ("LabelFrame" , backref = "frame_actions" )
0 commit comments