<s> import re <EOL> import string <EOL> import unicodedata <EOL> from unstdlib . six import text_type , PY3 , string_types , binary_type , u <EOL> from unstdlib . six . moves import xrange <EOL> if PY3 : <EOL> text_type_magicmethod = "<STR_LIT>" <EOL> else : <EOL> text_type_magicmethod = "<STR_LIT>" <EOL> from . random_ import random <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class r ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , val ) : <EOL> self . val = val <EOL> def __repr__ ( self ) : <EOL> if PY3 : <EOL> if isinstance ( self . val , text_type ) : <EOL> return '<STR_LIT:u>' + repr ( self . val ) <EOL> else : <EOL> if isinstance ( self . val , str ) : <EOL> return '<STR_LIT:b>' + repr ( self . val ) <EOL> return repr ( self . val ) <EOL> _Default = object ( ) <EOL> def random_string ( length = <NUM_LIT:6> , alphabet = string . ascii_letters + string . digits ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( [ random . choice ( alphabet ) for i in xrange ( length ) ] ) <EOL> def number_to_string ( n , alphabet ) : <EOL> """<STR_LIT>""" <EOL> result = '<STR_LIT>' <EOL> base = len ( alphabet ) <EOL> current = int ( n ) <EOL> if current < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" , n ) <EOL> while current : <EOL> result = alphabet [ current % base ] + result <EOL> current = current // base <EOL> return result <EOL> def string_to_number ( s , alphabet ) : <EOL> """<STR_LIT>""" <EOL> base = len ( alphabet ) <EOL> inverse_alphabet = dict ( zip ( alphabet , xrange ( <NUM_LIT:0> , base ) ) ) <EOL> n = <NUM_LIT:0> <EOL> exp = <NUM_LIT:0> <EOL> for i in reversed ( s ) : <EOL> n += inverse_alphabet [ i ] * ( base ** exp ) <EOL> exp += <NUM_LIT:1> <EOL> return n <EOL> def bytes_to_number ( b , endian = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if endian == '<STR_LIT>' : <EOL> b = reversed ( b ) <EOL> n = <NUM_LIT:0> <EOL> for i , ch in enumerate ( bytearray ( b ) ) : <EOL> n ^= ch << i * <NUM_LIT:8> <EOL> return n <EOL> def number_to_bytes ( n , endian = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> res = [ ] <EOL> while n : <EOL> n , ch = divmod ( n , <NUM_LIT> ) <EOL> if PY3 : <EOL> res . append ( ch ) <EOL> else : <EOL> res . append ( chr ( ch ) ) <EOL> if endian == '<STR_LIT>' : <EOL> res . reverse ( ) <EOL> if PY3 : <EOL> return bytes ( res ) <EOL> else : <EOL> return '<STR_LIT>' . join ( res ) <EOL> def to_str ( obj , encoding = '<STR_LIT:utf-8>' , ** encode_args ) : <EOL> r"""<STR_LIT>""" <EOL> if isinstance ( obj , binary_type ) : <EOL> return obj <EOL> if isinstance ( obj , text_type ) or hasattr ( obj , text_type_magicmethod ) : <EOL> return text_type ( obj ) . encode ( encoding , ** encode_args ) <EOL> return binary_type ( obj ) <EOL> def to_unicode ( obj , encoding = '<STR_LIT:utf-8>' , fallback = '<STR_LIT>' , ** decode_args ) : <EOL> r"""<STR_LIT>""" <EOL> if not isinstance ( obj , binary_type ) : <EOL> if isinstance ( obj , text_type ) or hasattr ( obj , text_type_magicmethod ) : <EOL> return text_type ( obj ) <EOL> obj_str = binary_type ( obj ) <EOL> else : <EOL> obj_str = obj <EOL> try : <EOL> return text_type ( obj_str , encoding , ** decode_args ) <EOL> except UnicodeDecodeError : <EOL> return text_type ( obj_str , fallback , ** decode_args ) <EOL> def to_int ( s , default = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return int ( s ) <EOL> except ( TypeError , ValueError ) : <EOL> return default <EOL> _infs = set ( [ float ( "<STR_LIT>" ) , float ( "<STR_LIT>" ) ] ) <EOL> def to_float ( s , default = <NUM_LIT:0.0> , allow_nan = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> f = float ( s ) <EOL> except ( TypeError , ValueError ) : <EOL> return default <EOL> if not allow_nan : <EOL> if f != f or f in _infs : <EOL> return default <EOL> return f <EOL> def format_int ( n , singular = _Default , plural = _Default ) : <EOL> """<STR_LIT>""" <EOL> n = int ( n ) <EOL> if singular in ( None , _Default ) : <EOL> if plural is _Default : <EOL> plural = None <EOL> singular = u'<STR_LIT>' <EOL> elif plural is _Default : <EOL> plural = singular + u'<STR_LIT:s>' <EOL> if n == <NUM_LIT:1> or not plural : <EOL> return singular . format ( n ) <EOL> return plural . format ( n ) <EOL> RE_NUMBER = re . compile ( r'<STR_LIT>' ) <EOL> def dollars_to_cents ( s , allow_negative = False ) : <EOL> """<STR_LIT>""" <EOL> if not s : <EOL> return <EOL> if isinstance ( s , string_types ) : <EOL> s = '<STR_LIT>' . join ( RE_NUMBER . findall ( s ) ) <EOL> dollars = int ( round ( float ( s ) * <NUM_LIT:100> ) ) <EOL> if not allow_negative and dollars < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return dollars <EOL> RE_SLUG = re . compile ( r'<STR_LIT>' ) <EOL> def slugify ( s , delimiter = '<STR_LIT:->' ) : <EOL> """<STR_LIT>""" <EOL> s = unicodedata . normalize ( '<STR_LIT>' , to_unicode ( s ) ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) . decode ( '<STR_LIT:ascii>' ) <EOL> return RE_SLUG . sub ( delimiter , s ) . strip ( delimiter ) . lower ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import doctest <EOL> doctest . testmod ( optionflags = doctest . ELLIPSIS ) </s>
