<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import <EOL> import calendar <EOL> from datetime import datetime , timedelta <EOL> from twisted . python . compat import nativeString <EOL> from twisted . python . util import FancyStrMixin <EOL> RFC4034_TIME_FORMAT = '<STR_LIT>' <EOL> class SerialNumber ( FancyStrMixin , object ) : <EOL> """<STR_LIT>""" <EOL> showAttributes = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> def __init__ ( self , number , serialBits = <NUM_LIT:32> ) : <EOL> """<STR_LIT>""" <EOL> self . _serialBits = serialBits <EOL> self . _modulo = <NUM_LIT:2> ** serialBits <EOL> self . _halfRing = <NUM_LIT:2> ** ( serialBits - <NUM_LIT:1> ) <EOL> self . _maxAdd = <NUM_LIT:2> ** ( serialBits - <NUM_LIT:1> ) - <NUM_LIT:1> <EOL> self . _number = int ( number ) % self . _modulo <EOL> def _convertOther ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( other , SerialNumber ) : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % ( self , other ) ) <EOL> if self . _serialBits != other . _serialBits : <EOL> raise TypeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( self , other ) ) <EOL> return other <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return nativeString ( '<STR_LIT>' % ( self . _number , ) ) <EOL> def __int__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _number <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> other = self . _convertOther ( other ) <EOL> return other . _number == self . _number <EOL> def __ne__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return not self . __eq__ ( other ) <EOL> def __lt__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> other = self . _convertOther ( other ) <EOL> return ( <EOL> ( self . _number < other . _number <EOL> and ( other . _number - self . _number ) < self . _halfRing ) <EOL> or <EOL> ( self . _number > other . _number <EOL> and ( self . _number - other . _number ) > self . _halfRing ) <EOL> ) <EOL> def __gt__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> other = self . _convertOther ( other ) <EOL> return ( <EOL> ( self . _number < other . _number <EOL> and ( other . _number - self . _number ) > self . _halfRing ) <EOL> or <EOL> ( self . _number > other . _number <EOL> and ( self . _number - other . _number ) < self . _halfRing ) <EOL> ) <EOL> def __le__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> other = self . _convertOther ( other ) <EOL> return self == other or self < other <EOL> def __ge__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> other = self . _convertOther ( other ) <EOL> return self == other or self > other <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> other = self . _convertOther ( other ) <EOL> if other . _number <= self . _maxAdd : <EOL> return SerialNumber ( <EOL> ( self . _number + other . _number ) % self . _modulo , <EOL> serialBits = self . _serialBits ) <EOL> else : <EOL> raise ArithmeticError ( <EOL> '<STR_LIT>' % ( <EOL> other . _number , self . _maxAdd , ) ) <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return hash ( self . _number ) <EOL> @ classmethod <EOL> def fromRFC4034DateString ( cls , utcDateString ) : <EOL> """<STR_LIT>""" <EOL> parsedDate = datetime . strptime ( utcDateString , RFC4034_TIME_FORMAT ) <EOL> secondsSinceEpoch = calendar . timegm ( parsedDate . utctimetuple ( ) ) <EOL> return cls ( secondsSinceEpoch , serialBits = <NUM_LIT:32> ) <EOL> def toRFC4034DateString ( self ) : <EOL> """<STR_LIT>""" <EOL> d = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) + timedelta ( seconds = self . _number ) <EOL> return nativeString ( d . strftime ( RFC4034_TIME_FORMAT ) ) <EOL> __all__ = [ '<STR_LIT>' ] </s>
