1414from ._decimal import ctx as decimal_ctx
1515from ._decimal import from_bytes as decimal_from_bytes
1616from ._decimal import to_bytes as decimal_to_bytes
17- from .error import (CantComare , NotImplementedYet , NotSupported ,
18- UnsupportedDatatype , IllegalUsage )
17+ from .error import (
18+ CantComare ,
19+ NotImplementedYet ,
20+ NotSupported ,
21+ UnsupportedDatatype ,
22+ IllegalUsage ,
23+ )
1924
2025log = logging .getLogger ("exonum datatypes" )
2126dbg = log .debug
@@ -38,23 +43,23 @@ def __init__(self, *val):
3843
3944 def __eq__ (self , other ):
4045 return (
41- ( hasattr (other , "__class__" )
42- and self .__class__ == other .__class__
43- and hasattr (other , "val" )
44- and self .val == other .val )
45- or (self .val == other ) )
46+ hasattr (other , "__class__" )
47+ and self .__class__ == other .__class__
48+ and hasattr (other , "val" )
49+ and self .val == other .val
50+ ) or (self .val == other )
4651
4752 def __ne__ (self , other ):
4853 return not self .__eq__ (other )
4954
5055 @classmethod
5156 def read (cls , buf , offset = 0 ):
52- val , = struct .unpack_from (cls .fmt , buf , offset = offset )
57+ val , = struct .unpack_from (cls .fmt , buf , offset = offset )
5358 return cls (val )
5459
5560 def write (self , buf , offset ):
5661 raw = struct .pack (self .fmt , self .val )
57- buf [offset : offset + self .sz ] = raw
62+ buf [offset : offset + self .sz ] = raw
5863
5964 def __str__ (self ):
6065 return u"{}({})" .format (self .__class__ .__name__ , self .val )
@@ -76,32 +81,31 @@ def write(self, buf, offset):
7681 self .extend_buffer (buf )
7782
7883 size = len (buf ) - start
79- buf [offset : end ] = struct .pack (self .fmt , start , size )
84+ buf [offset :end ] = struct .pack (self .fmt , start , size )
8085
8186 @classmethod
8287 def read (cls , buf , offset = 0 ):
8388 segm_offset , cnt = struct .unpack_from (cls .fmt , buf , offset = offset )
84- dbg ("Segment {} lays at position = {} count = {}" .format (
85- cls , segm_offset , cnt ))
89+ dbg ("Segment {} lays at position = {} count = {}" .format (cls , segm_offset , cnt ))
8690 return cls .read_buffer (buf , offset = segm_offset , cnt = cnt )
8791
8892
8993class bool (ExonumField ):
90- fmt = '<B'
94+ fmt = "<B"
9195
9296
9397class u8 (ExonumField ):
94- fmt = '<B'
98+ fmt = "<B"
9599
96100
97101class u16 (ExonumField ):
98102 sz = 2
99- fmt = '<H'
103+ fmt = "<H"
100104
101105
102106class u32 (ExonumField ):
103107 sz = 4
104- fmt = '<I'
108+ fmt = "<I"
105109
106110
107111class ExonumBigInt (ExonumField ):
@@ -120,32 +124,32 @@ def plain(self):
120124
121125class u64 (ExonumBigInt ):
122126 sz = 8
123- fmt = '<Q'
127+ fmt = "<Q"
124128
125129
126130class i8 (ExonumField ):
127- fmt = '<b'
131+ fmt = "<b"
128132
129133
130134class i16 (ExonumField ):
131135 sz = 2
132- fmt = '<h'
136+ fmt = "<h"
133137
134138
135139class i32 (ExonumField ):
136140 sz = 4
137- fmt = '<i'
141+ fmt = "<i"
138142
139143
140144class i64 (ExonumBigInt ):
141145 sz = 8
142- fmt = '<q'
146+ fmt = "<q"
143147
144148
145149@six .python_2_unicode_compatible
146150class Hash (ExonumField ):
147151 sz = 32
148- fmt = ' 32s'
152+ fmt = " 32s"
149153
150154 def __str__ (self ):
151155 return u"{}({})" .format (self .__class__ .__name__ , self .plain ())
@@ -160,12 +164,12 @@ class PublicKey(Hash):
160164
161165class Signature (Hash ):
162166 sz = 64
163- fmt = ' 64s'
167+ fmt = " 64s"
164168
165169
166170class DateTime (ExonumField ):
167171 sz = 12
168- fmt = ' <qI'
172+ fmt = " <qI"
169173
170174 def __init__ (self , * val ):
171175 if len (val ) == 0 :
@@ -178,23 +182,22 @@ def __init__(self, *val):
178182 self .val = nanotime .datetime (val )
179183 elif isinstance (val , nanotime .nanotime ):
180184 self .val = val
181- elif (isinstance (val , dict )
182- and "nanos" in val
183- and "secs" in val ):
184- self .val = (nanotime .seconds (int (val ["secs" ]))
185- + nanotime .nanoseconds (int (val ["nanos" ])))
185+ elif isinstance (val , dict ) and "nanos" in val and "secs" in val :
186+ self .val = nanotime .seconds (int (val ["secs" ])) + nanotime .nanoseconds (
187+ int (val ["nanos" ])
188+ )
186189 else :
187190 raise UnsupportedDatatype (
188- "Type {} is not supported for initializing DateTime"
189- . format ( type ( val )) )
191+ "Type {} is not supported for initializing DateTime" . format ( type ( val ))
192+ )
190193
191194 def __eq__ (self , other ):
192195 return (
193- ( hasattr (other , "__class__" )
194- and self .__class__ == other .__class__
195- and hasattr (other , "val" )
196- and self .val .nanoseconds () == other .val .nanoseconds () )
197- or (self .val == other ) )
196+ hasattr (other , "__class__" )
197+ and self .__class__ == other .__class__
198+ and hasattr (other , "val" )
199+ and self .val .nanoseconds () == other .val .nanoseconds ()
200+ ) or (self .val == other )
198201
199202 def to_pair (self ):
200203 sec = int (self .val .seconds ())
@@ -204,7 +207,7 @@ def to_pair(self):
204207 def write (self , buf , offset ):
205208 sec , nan = self .to_pair ()
206209 raw = struct .pack (self .fmt , sec , nan )
207- buf [offset : offset + self .sz ] = raw
210+ buf [offset : offset + self .sz ] = raw
208211
209212 @classmethod
210213 def read (cls , buf , offset = 0 ):
@@ -231,11 +234,11 @@ def __init__(self, *val):
231234 self .val = UUID (val )
232235
233236 def write (self , buf , offset ):
234- buf [offset : offset + self .sz ] = self .val .bytes
237+ buf [offset : offset + self .sz ] = self .val .bytes
235238
236239 @classmethod
237240 def read (cls , buf , offset = 0 ):
238- return cls (UUID (bytes = buf [offset : offset + cls .sz ]))
241+ return cls (UUID (bytes = buf [offset : offset + cls .sz ]))
239242
240243 def plain (self ):
241244 return self .val .hex
@@ -257,7 +260,7 @@ def __init__(self, *val):
257260
258261 def write (self , buf , offset ):
259262 end = offset + self .sz
260- buf [offset : end ] = struct .pack (self .fmt , * decimal_to_bytes (self .val ))
263+ buf [offset :end ] = struct .pack (self .fmt , * decimal_to_bytes (self .val ))
261264
262265 @classmethod
263266 def read (cls , buf , offset = 0 ):
@@ -288,7 +291,7 @@ def __init__(self, *val):
288291 def write (self , buf , offset ):
289292 raw = self .val [0 ].packed + struct .pack ("<H" , self .val [1 ])
290293
291- buf [offset : offset + self .sz ] = raw
294+ buf [offset : offset + self .sz ] = raw
292295
293296 @classmethod
294297 def read (cls , buf , offset = 0 ):
@@ -309,7 +312,7 @@ def extend_buffer(self, buf):
309312
310313 @classmethod
311314 def read_buffer (cls , buf , offset = 0 , cnt = 0 ):
312- return cls (buf [offset : offset + cnt ].decode ("utf-8" ))
315+ return cls (buf [offset : offset + cnt ].decode ("utf-8" ))
313316
314317 def plain (self ):
315318 return self .val
@@ -350,11 +353,13 @@ def read_buffer(cls, buf, offset=0, cnt=0):
350353 return cls (v )
351354
352355 def write (self , buf , offset ):
353- dbg ("writing vector ({}) of sz {} at offset {}" .format (
354- self .T .__name__ , self .count (), offset ))
356+ dbg (
357+ "writing vector ({}) of sz {} at offset {}" .format (
358+ self .T .__name__ , self .count (), offset
359+ )
360+ )
355361
356- buf [offset : offset +
357- self .sz ] = struct .pack (self .fmt , len (buf ), self .count ())
362+ buf [offset : offset + self .sz ] = struct .pack (self .fmt , len (buf ), self .count ())
358363 self .extend_buffer (buf )
359364
360365 def extend_buffer (self , buf ):
@@ -371,9 +376,7 @@ def plain(self):
371376
372377def Vec (T ):
373378 if issubclass (T , ExonumField ):
374- return type ("Vec<{}>" .format (T .__name__ ),
375- (Vector , ),
376- {"T" : T })()
379+ return type ("Vec<{}>" .format (T .__name__ ), (Vector ,), {"T" : T })()
377380 raise NotSupported ()
378381
379382
@@ -396,7 +399,7 @@ def __init__(self, val=None, **kwargs):
396399 self .cnt += field_ .sz
397400 if issubclass (cls , ExonumSegment ):
398401 self .cnt += field_ .count ()
399- setattr (self , field , field_ )
402+ setattr (self , field , field_ )
400403
401404 def __eq__ (self , other ):
402405 if self .__class__ != other .__class__ :
@@ -426,20 +429,19 @@ def __str__(self):
426429 def read (cls , buf , offset = 0 ):
427430 offset , cnt = struct .unpack_from (cls .fmt , buf , offset = offset )
428431 dbg ("{} lays at position = {} count = {}" .format (cls , offset , cnt ))
429- return cls .read_buffer (buf [offset : offset + cnt ])
432+ return cls .read_buffer (buf [offset : offset + cnt ])
430433
431434 @classmethod
432435 def read_buffer (cls , buf , offset = 0 , cnt = None ):
433436 if cnt is None :
434437 cnt = len (buf )
435- segment = buf [offset : offset + cnt ]
438+ segment = buf [offset : offset + cnt ]
436439 dbg ("read_buffer of ExonumBase sz {}" .format (cls .sz ))
437440 offset = 0
438441 data = {}
439442 for field in cls .__exonum_fields__ :
440443 fcls = getattr (cls , field )
441- dbg ("trying to read {} {} at offset {}"
442- .format (field , fcls , offset ))
444+ dbg ("trying to read {} {} at offset {}" .format (field , fcls , offset ))
443445 val = fcls .read (segment , offset )
444446 offset += fcls .sz
445447
@@ -452,10 +454,7 @@ def to_bytes(self):
452454 return bytes (b )
453455
454456 def plain (self ):
455- return {
456- k : getattr (self , k ).plain ()
457- for k in self .__exonum_fields__
458- }
457+ return {k : getattr (self , k ).plain () for k in self .__exonum_fields__ }
459458
460459
461460class EncodingStruct (type ):
@@ -469,16 +468,17 @@ def __new__(self, name, bases, classdict):
469468 "{}: {} - "
470469 "One cant use ExonumField this way.\n "
471470 "You should initialize it in class definition.\n "
472- "ex: {} = {}()" .format (name , k , k , v .__name__ ))
473-
471+ "ex: {} = {}()" .format (name , k , k , v .__name__ )
472+ )
473+
474474 if isinstance (v , ExonumField ):
475475 classdict [k ] = v .__class__
476476 fields .append ((k , v ))
477477 sz += v .sz
478478
479479 fields .sort (key = lambda v : v [1 ]._order )
480- classdict [' __exonum_fields__' ] = [k for k , _ in fields ]
481- classdict [' fields_sz' ] = sz
480+ classdict [" __exonum_fields__" ] = [k for k , _ in fields ]
481+ classdict [" fields_sz" ] = sz
482482
483483 # py2 compat
484484 _bases = list (bases )
@@ -491,4 +491,5 @@ def __new__(self, name, bases, classdict):
491491 ("protocol_version" , u8 ),
492492 ("message_id" , u16 ),
493493 ("service_id" , u16 ),
494- ("payload_sz" , u32 ))
494+ ("payload_sz" , u32 ),
495+ )
0 commit comments