<s> """<STR_LIT>""" <EOL> from kazoo . exceptions import BadVersionError <EOL> from kazoo . retry import ForceRetryError <EOL> class Counter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client , path , default = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . client = client <EOL> self . path = path <EOL> self . default = default <EOL> self . default_type = type ( default ) <EOL> self . _ensured_path = False <EOL> def _ensure_node ( self ) : <EOL> if not self . _ensured_path : <EOL> self . client . ensure_path ( self . path ) <EOL> self . _ensured_path = True <EOL> def _value ( self ) : <EOL> self . _ensure_node ( ) <EOL> old , stat = self . client . get ( self . path ) <EOL> old = old . decode ( '<STR_LIT:ascii>' ) if old != b'<STR_LIT>' else self . default <EOL> version = stat . version <EOL> data = self . default_type ( old ) <EOL> return data , version <EOL> @ property <EOL> def value ( self ) : <EOL> return self . _value ( ) [ <NUM_LIT:0> ] <EOL> def _change ( self , value ) : <EOL> if not isinstance ( value , self . default_type ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . client . retry ( self . _inner_change , value ) <EOL> return self <EOL> def _inner_change ( self , value ) : <EOL> data , version = self . _value ( ) <EOL> data = repr ( data + value ) . encode ( '<STR_LIT:ascii>' ) <EOL> try : <EOL> self . client . set ( self . path , data , version = version ) <EOL> except BadVersionError : <EOL> raise ForceRetryError ( ) <EOL> def __add__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return self . _change ( value ) <EOL> def __sub__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return self . _change ( - value ) </s>
