<s> from functools import cmp_to_key <EOL> import math <EOL> import random <EOL> import re <EOL> import sublime <EOL> import sublime_plugin <EOL> class CalculateCommand ( sublime_plugin . TextCommand ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> sublime_plugin . TextCommand . __init__ ( self , * args , ** kwargs ) <EOL> self . dict = { } <EOL> for key in dir ( random ) : <EOL> self . dict [ key ] = getattr ( random , key ) <EOL> for key in dir ( math ) : <EOL> self . dict [ key ] = getattr ( math , key ) <EOL> def average ( nums ) : <EOL> return sum ( nums ) / len ( nums ) <EOL> self . dict [ '<STR_LIT>' ] = average <EOL> self . dict [ '<STR_LIT>' ] = average <EOL> def password ( length = <NUM_LIT:20> ) : <EOL> pwdchrs = '<STR_LIT>' <EOL> return '<STR_LIT>' . join ( random . choice ( pwdchrs ) for _ in range ( length ) ) <EOL> self . dict [ '<STR_LIT>' ] = password <EOL> self . dict [ '<STR_LIT:password>' ] = password <EOL> def run ( self , edit , ** kwargs ) : <EOL> self . dict [ '<STR_LIT:i>' ] = <NUM_LIT:0> <EOL> if len ( self . view . sel ( ) ) == <NUM_LIT:1> and not self . view . sel ( ) [ <NUM_LIT:0> ] : <EOL> return self . get_formula ( ) <EOL> for region in self . view . sel ( ) : <EOL> try : <EOL> error = self . run_each ( edit , region , ** kwargs ) <EOL> except Exception as exception : <EOL> error = str ( exception ) <EOL> self . dict [ '<STR_LIT:i>' ] = self . dict [ '<STR_LIT:i>' ] + <NUM_LIT:1> <EOL> if error : <EOL> sublime . status_message ( error ) <EOL> def calculate ( self , formula ) : <EOL> formula = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , formula ) <EOL> formula = re . sub ( r'<STR_LIT:\n>' , '<STR_LIT:U+0020>' , formula ) <EOL> result = eval ( formula , self . dict , { } ) <EOL> if not isinstance ( result , str ) : <EOL> result = str ( result ) <EOL> if result [ - <NUM_LIT:2> : ] == '<STR_LIT>' : <EOL> result = result [ : - <NUM_LIT:2> ] <EOL> return result <EOL> def run_each ( self , edit , region , replace = False ) : <EOL> if not region . empty ( ) : <EOL> formula = self . view . substr ( region ) <EOL> value = self . calculate ( formula ) <EOL> if not replace : <EOL> value = "<STR_LIT>" % ( formula , value ) <EOL> self . view . replace ( edit , region , value ) <EOL> def get_formula ( self ) : <EOL> self . view . window ( ) . show_input_panel ( '<STR_LIT>' , '<STR_LIT>' , self . on_calculate , None , None ) <EOL> def on_calculate ( self , formula ) : <EOL> value = self . calculate ( formula ) <EOL> self . view . run_command ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' % value } ) <EOL> class CalculateCountCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , index = <NUM_LIT:1> ) : <EOL> def generate_integer_counter ( initial ) : <EOL> def count ( ) : <EOL> offset = initial <EOL> while True : <EOL> yield str ( offset ) <EOL> offset += <NUM_LIT:1> <EOL> return iter ( count ( ) ) . __next__ <EOL> def generate_hexadecimal_counter ( initial , length ) : <EOL> def count ( ) : <EOL> offset = initial <EOL> while True : <EOL> yield u"<STR_LIT>" % offset <EOL> offset += <NUM_LIT:1> <EOL> return iter ( count ( ) ) . __next__ <EOL> def generate_octal_counter ( initial , length ) : <EOL> def count ( ) : <EOL> offset = initial <EOL> while True : <EOL> yield u"<STR_LIT>" % offset <EOL> offset += <NUM_LIT:1> <EOL> return iter ( count ( ) ) . __next__ <EOL> def generate_string_counter ( initial ) : <EOL> if re . match ( '<STR_LIT>' , initial ) : <EOL> maxord = ord ( '<STR_LIT:z>' ) <EOL> letter = '<STR_LIT:a>' <EOL> else : <EOL> maxord = ord ( '<STR_LIT>' ) <EOL> letter = '<STR_LIT:A>' <EOL> def count ( ) : <EOL> offset = initial <EOL> while True : <EOL> yield offset <EOL> up = <NUM_LIT:1> <EOL> while True : <EOL> o = ord ( offset [ - up ] ) <EOL> o += <NUM_LIT:1> <EOL> tail = '<STR_LIT>' <EOL> if up > <NUM_LIT:1> : <EOL> tail = offset [ - up + <NUM_LIT:1> : ] <EOL> if o > maxord : <EOL> offset = offset [ : - up ] + letter + tail <EOL> up += <NUM_LIT:1> <EOL> if len ( offset ) < up : <EOL> offset = letter + offset <EOL> break <EOL> else : <EOL> offset = offset [ : - up ] + chr ( o ) + tail <EOL> break <EOL> return iter ( count ( ) ) . __next__ <EOL> def generate_plusminus_counter ( initial ) : <EOL> def count ( ) : <EOL> offset = initial <EOL> while True : <EOL> yield str ( offset ) <EOL> if offset == '<STR_LIT:->' : <EOL> offset = '<STR_LIT:+>' <EOL> else : <EOL> offset = '<STR_LIT:->' <EOL> return iter ( count ( ) ) . __next__ <EOL> is_first = True <EOL> subs = [ ] <EOL> for region in self . view . sel ( ) : <EOL> if is_first : <EOL> content = self . view . substr ( region ) <EOL> if re . match ( '<STR_LIT>' , content ) : <EOL> counter = generate_hexadecimal_counter ( int ( content [ <NUM_LIT:2> : ] , <NUM_LIT:16> ) , len ( regions ) ) <EOL> elif re . match ( '<STR_LIT>' , content ) : <EOL> counter = generate_octal_counter ( int ( content [ <NUM_LIT:1> : ] , <NUM_LIT:8> ) , len ( regions ) ) <EOL> elif re . match ( '<STR_LIT>' , content ) : <EOL> counter = generate_integer_counter ( int ( content ) ) <EOL> elif re . match ( '<STR_LIT>' , content ) : <EOL> counter = generate_string_counter ( content ) <EOL> elif re . match ( '<STR_LIT>' , content ) : <EOL> counter = generate_plusminus_counter ( content ) <EOL> else : <EOL> counter = generate_integer_counter ( index ) <EOL> subs . append ( ( region , str ( counter ( ) ) ) ) <EOL> is_first = False <EOL> def get_end ( region_tuple ) : <EOL> return region_tuple [ <NUM_LIT:0> ] . end ( ) <EOL> subs . sort ( key = get_end , reverse = True ) <EOL> for sub in subs : <EOL> self . view . sel ( ) . subtract ( sub [ <NUM_LIT:0> ] ) <EOL> self . view . replace ( edit , sub [ <NUM_LIT:0> ] , sub [ <NUM_LIT:1> ] ) <EOL> self . view . sel ( ) . add ( sublime . Region ( sub [ <NUM_LIT:0> ] . begin ( ) + len ( sub [ <NUM_LIT:1> ] ) , sub [ <NUM_LIT:0> ] . begin ( ) + len ( sub [ <NUM_LIT:1> ] ) ) ) </s>
