@@ -31,42 +31,48 @@ def to_text(self):
3131 def to_binary (self ):
3232 return pack ('>HH' , self .dim (), 0 ) + self ._value .tobytes ()
3333
34- def from_text (value ):
35- return HalfVector ([float (v ) for v in value [1 :- 1 ].split (',' )])
34+ @classmethod
35+ def from_text (cls , value ):
36+ return cls ([float (v ) for v in value [1 :- 1 ].split (',' )])
3637
37- def from_binary (value ):
38+ @classmethod
39+ def from_binary (cls , value ):
3840 dim , unused = unpack_from ('>HH' , value )
39- return HalfVector (np .frombuffer (value , dtype = '>f2' , count = dim , offset = 4 ))
41+ return cls (np .frombuffer (value , dtype = '>f2' , count = dim , offset = 4 ))
4042
41- def _to_db (value , dim = None ):
43+ @classmethod
44+ def _to_db (cls , value , dim = None ):
4245 if value is None :
4346 return value
4447
45- if not isinstance (value , HalfVector ):
46- value = HalfVector (value )
48+ if not isinstance (value , cls ):
49+ value = cls (value )
4750
4851 if dim is not None and value .dim () != dim :
4952 raise ValueError ('expected %d dimensions, not %d' % (dim , value .dim ()))
5053
5154 return value .to_text ()
5255
53- def _to_db_binary (value ):
56+ @classmethod
57+ def _to_db_binary (cls , value ):
5458 if value is None :
5559 return value
5660
57- if not isinstance (value , HalfVector ):
58- value = HalfVector (value )
61+ if not isinstance (value , cls ):
62+ value = cls (value )
5963
6064 return value .to_binary ()
6165
62- def _from_db (value ):
63- if value is None or isinstance (value , HalfVector ):
66+ @classmethod
67+ def _from_db (cls , value ):
68+ if value is None or isinstance (value , cls ):
6469 return value
6570
66- return HalfVector .from_text (value )
71+ return cls .from_text (value )
6772
68- def _from_db_binary (value ):
69- if value is None or isinstance (value , HalfVector ):
73+ @classmethod
74+ def _from_db_binary (cls , value ):
75+ if value is None or isinstance (value , cls ):
7076 return value
7177
72- return HalfVector .from_binary (value )
78+ return cls .from_binary (value )
0 commit comments