idx
int64
0
63k
question
stringlengths
53
5.28k
target
stringlengths
5
805
14,700
def get_queryset ( self ) : kwargs = { } if self . start_at : kwargs . update ( { '%s__gte' % self . date_field : self . start_at } ) return super ( DateRangeMixin , self ) . get_queryset ( ) . filter ( ** kwargs )
Implements date range filtering on created_at
14,701
def create ( self , fname , lname , group , type , group_api ) : self . __username ( fname , lname ) self . client . add ( self . __distinguished_name ( type , fname = fname , lname = lname ) , API . __object_class ( ) , self . __ldap_attr ( fname , lname , type , group , group_api ) )
Create an LDAP User .
14,702
def show ( self , username ) : filter = [ '(objectclass=posixAccount)' , "(uid={})" . format ( username ) ] return self . client . search ( filter )
Return a specific user s info in LDIF format .
14,703
def find ( self , username ) : filter = [ '(uid={})' . format ( username ) ] results = self . client . search ( filter ) if len ( results ) < 1 : raise ldap_tools . exceptions . NoUserFound ( 'User ({}) not found' . format ( username ) ) return elif len ( results ) > 1 : raise ldap_tools . exceptions . TooManyResults (...
Find user with given username .
14,704
def __username ( self , fname , lname ) : self . username = '.' . join ( [ i . lower ( ) for i in [ fname , lname ] ] )
Convert first name + last name into first . last style username .
14,705
def __distinguished_name ( self , type , fname = None , lname = None , username = None ) : if username is None : uid = "uid={}" . format ( self . username ) else : uid = "uid={}" . format ( username ) dn_list = [ uid , "ou={}" . format ( self . __organizational_unit ( type ) ) , self . client . basedn , ] return ',' . ...
Assemble the DN of the user .
14,706
def __ldap_attr ( self , fname , lname , type , group , group_api ) : return { 'uid' : str ( self . username ) . encode ( ) , 'cn' : ' ' . join ( [ fname , lname ] ) . encode ( ) , 'sn' : str ( lname ) . encode ( ) , 'givenname' : str ( fname ) . encode ( ) , 'homedirectory' : os . path . join ( os . path . sep , 'home...
User LDAP attributes .
14,707
def __create_password ( ) : salt = b64encode ( API . __generate_string ( 32 ) ) password = b64encode ( API . __generate_string ( 64 ) ) return b64encode ( sha1 ( password + salt ) . digest ( ) )
Create a password for the user .
14,708
def __generate_string ( length ) : return '' . join ( SystemRandom ( ) . choice ( string . ascii_letters + string . digits ) for x in range ( length ) ) . encode ( )
Generate a string for password creation .
14,709
def create ( config , name , group , type ) : if type not in ( 'user' , 'service' ) : raise click . BadOptionUsage ( "--type must be 'user' or 'service'" ) client = Client ( ) client . prepare_connection ( ) user_api = API ( client ) group_api = GroupApi ( client ) user_api . create ( name [ 0 ] , name [ 1 ] , group , ...
Create an LDAP user .
14,710
def index ( config ) : client = Client ( ) client . prepare_connection ( ) user_api = API ( client ) CLI . show_user ( user_api . index ( ) )
Display user info in LDIF format .
14,711
def show ( config , username ) : client = Client ( ) client . prepare_connection ( ) user_api = API ( client ) CLI . show_user ( user_api . show ( username ) )
Display a specific user .
14,712
async def set_discovery_enabled ( self ) : endpoint = '/setup/bluetooth/discovery' data = { "enable_discovery" : True } url = API . format ( ip = self . _ipaddress , endpoint = endpoint ) try : async with async_timeout . timeout ( 5 , loop = self . _loop ) : response = await self . _session . post ( url , headers = HEA...
Enable bluetooth discoverablility .
14,713
async def scan_for_devices_multi_run ( self , runs = 2 ) : run = 1 master = { } while run < runs + 1 : await self . scan_for_devices ( ) await self . get_scan_result ( ) if master is None : for device in self . _devices : mac = device [ 'mac_address' ] master [ mac ] = { } master [ mac ] [ 'rssi' ] = device [ 'rssi' ] ...
Scan for devices multiple times .
14,714
def contract_from_file ( fname ) : f = open ( fname ) j = f . read ( ) f . close ( ) return Contract ( json . loads ( j ) )
Loads a Barrister IDL JSON from the given file and returns a Contract class
14,715
def get_prop ( self , key , default_val = None ) : if self . props . has_key ( key ) : return self . props [ key ] else : return default_val
Returns a property set on the context .
14,716
def set_error ( self , code , msg , data = None ) : self . error = err_response ( self . request [ "id" ] , code , msg , data )
Set an error on this request which will prevent request execution . Should only be called from pre hook methods . If called from a post hook this operation will be ignored .
14,717
def add_handler ( self , iface_name , handler ) : if self . contract . has_interface ( iface_name ) : self . handlers [ iface_name ] = handler else : raise RpcException ( ERR_INVALID_REQ , "Unknown interface: '%s'" , iface_name )
Associates the given handler with the interface name . If the interface does not exist in the Contract an RpcException is raised .
14,718
def set_filters ( self , filters ) : if filters == None or isinstance ( filters , ( tuple , list ) ) : self . filters = filters else : self . filters = [ filters ]
Sets the filters for the server .
14,719
def call ( self , req , props = None ) : resp = None if self . log . isEnabledFor ( logging . DEBUG ) : self . log . debug ( "Request: %s" % str ( req ) ) if isinstance ( req , list ) : if len ( req ) < 1 : resp = err_response ( None , ERR_INVALID_REQ , "Invalid Request. Empty batch." ) else : resp = [ ] for r in req :...
Executes a Barrister request and returns a response . If the request is a list then the response will also be a list . If the request is an empty list a RpcException is raised .
14,720
def _call ( self , context ) : req = context . request if not req . has_key ( "method" ) : raise RpcException ( ERR_INVALID_REQ , "Invalid Request. No 'method'." ) method = req [ "method" ] if method == "barrister-idl" : return self . contract . idl_parsed iface_name , func_name = unpack_method ( method ) if self . han...
Executes a single request against a handler . If the req . method == barrister - idl the Contract IDL JSON structure is returned . Otherwise the method is resolved to a handler based on the interface name and the appropriate function is called on the handler .
14,721
def request ( self , req ) : data = json . dumps ( req ) req = urllib2 . Request ( self . url , data , self . headers ) f = self . opener . open ( req ) resp = f . read ( ) f . close ( ) return json . loads ( resp )
Makes a request against the server and returns the deserialized result .
14,722
def call ( self , iface_name , func_name , params ) : req = self . to_request ( iface_name , func_name , params ) if self . log . isEnabledFor ( logging . DEBUG ) : self . log . debug ( "Request: %s" % str ( req ) ) resp = self . transport . request ( req ) if self . log . isEnabledFor ( logging . DEBUG ) : self . log ...
Makes a single RPC request and returns the result .
14,723
def to_request ( self , iface_name , func_name , params ) : if self . validate_req : self . contract . validate_request ( iface_name , func_name , params ) method = "%s.%s" % ( iface_name , func_name ) reqid = self . id_gen ( ) return { "jsonrpc" : "2.0" , "id" : reqid , "method" : method , "params" : params }
Converts the arguments to a JSON - RPC request dict . The id field is populated using the id_gen function passed to the Client constructor .
14,724
def to_result ( self , iface_name , func_name , resp ) : if resp . has_key ( "error" ) : e = resp [ "error" ] data = None if e . has_key ( "data" ) : data = e [ "data" ] raise RpcException ( e [ "code" ] , e [ "message" ] , data ) result = resp [ "result" ] if self . validate_resp : self . contract . validate_response ...
Takes a JSON - RPC response and checks for an error slot . If it exists a RpcException is raised . If no error slot exists the result slot is returned .
14,725
def validate_request ( self , iface_name , func_name , params ) : self . interface ( iface_name ) . function ( func_name ) . validate_params ( params )
Validates that the given params match the expected length and types for this interface and function .
14,726
def validate_response ( self , iface_name , func_name , resp ) : self . interface ( iface_name ) . function ( func_name ) . validate_response ( resp )
Validates that the response matches the return type for the function
14,727
def get ( self , name ) : if self . structs . has_key ( name ) : return self . structs [ name ] elif self . enums . has_key ( name ) : return self . enums [ name ] elif self . interfaces . has_key ( name ) : return self . interfaces [ name ] else : raise RpcException ( ERR_INVALID_PARAMS , "Unknown entity: '%s'" % name...
Returns the struct enum or interface with the given name or raises RpcException if no elements match that name .
14,728
def struct ( self , struct_name ) : if self . structs . has_key ( struct_name ) : return self . structs [ struct_name ] else : raise RpcException ( ERR_INVALID_PARAMS , "Unknown struct: '%s'" , struct_name )
Returns the struct with the given name or raises RpcException if no struct matches
14,729
def interface ( self , iface_name ) : if self . has_interface ( iface_name ) : return self . interfaces [ iface_name ] else : raise RpcException ( ERR_INVALID_PARAMS , "Unknown interface: '%s'" , iface_name )
Returns the interface with the given name or raises RpcException if no interface matches
14,730
def validate ( self , expected_type , is_array , val ) : if val == None : if expected_type . optional : return True , None else : return False , "Value cannot be null" elif is_array : if not isinstance ( val , list ) : return self . _type_err ( val , "list" ) else : for v in val : ok , msg = self . validate ( expected_...
Validates that the expected type matches the value
14,731
def function ( self , func_name ) : if self . functions . has_key ( func_name ) : return self . functions [ func_name ] else : raise RpcException ( ERR_METHOD_NOT_FOUND , "%s: Unknown function: '%s'" , self . name , func_name )
Returns the Function instance associated with the given func_name or raises a RpcException if no function matches .
14,732
def validate ( self , val ) : if val in self . values : return True , None else : return False , "'%s' is not in enum: %s" % ( val , str ( self . values ) )
Validates that the val is in the list of values for this Enum .
14,733
def field ( self , name ) : if self . fields . has_key ( name ) : return self . fields [ name ] elif self . extends : if not self . parent : self . parent = self . contract . struct ( self . extends ) return self . parent . field ( name ) else : return None
Returns the field on this struct with the given name . Will try to find this name on all ancestors if this struct extends another .
14,734
def validate ( self , val ) : if type ( val ) is not dict : return False , "%s is not a dict" % ( str ( val ) ) for k , v in val . items ( ) : field = self . field ( k ) if field : ok , msg = self . contract . validate ( field , field . is_array , v ) if not ok : return False , "field '%s': %s" % ( field . name , msg )...
Validates that the val matches the expected fields for this struct . val must be a dict and must contain only fields represented by this struct and its ancestors .
14,735
def get_all_fields ( self , arr ) : for k , v in self . fields . items ( ) : arr . append ( v ) if self . extends : parent = self . contract . get ( self . extends ) if parent : return parent . get_all_fields ( arr ) return arr
Returns a list containing this struct s fields and all the fields of its ancestors . Used during validation .
14,736
def validate_params ( self , params ) : plen = 0 if params != None : plen = len ( params ) if len ( self . params ) != plen : vals = ( self . full_name , len ( self . params ) , plen ) msg = "Function '%s' expects %d param(s). %d given." % vals raise RpcException ( ERR_INVALID_PARAMS , msg ) if params != None : i = 0 f...
Validates params against expected types for this function . Raises RpcException if the params are invalid .
14,737
def validate_response ( self , resp ) : ok , msg = self . contract . validate ( self . returns , self . returns . is_array , resp ) if not ok : vals = ( self . full_name , str ( resp ) , msg ) msg = "Function '%s' invalid response: '%s'. %s" % vals raise RpcException ( ERR_INVALID_RESP , msg )
Validates resp against expected return type for this function . Raises RpcException if the response is invalid .
14,738
def submit ( xml_root , submit_config , session , dry_run = None , ** kwargs ) : properties . xunit_fill_testrun_id ( xml_root , kwargs . get ( "testrun_id" ) ) if dry_run is not None : properties . set_dry_run ( xml_root , dry_run ) xml_input = utils . etree_to_string ( xml_root ) logger . info ( "Submitting data to %...
Submits data to the Polarion Importer .
14,739
def submit_and_verify ( xml_str = None , xml_file = None , xml_root = None , config = None , session = None , dry_run = None , ** kwargs ) : try : config = config or configuration . get_config ( ) xml_root = _get_xml_root ( xml_root , xml_str , xml_file ) submit_config = SubmitConfig ( xml_root , config , ** kwargs ) s...
Submits data to the Polarion Importer and checks that it was imported .
14,740
def get_job_ids ( self ) : if not self . parsed_response : return None try : job_ids = self . parsed_response [ "files" ] [ "results.xml" ] [ "job-ids" ] except KeyError : return None if not job_ids or job_ids == [ 0 ] : return None return job_ids
Returns job IDs of the import .
14,741
def validate_response ( self ) : if self . response is None : logger . error ( "Failed to submit" ) return False if not self . response : logger . error ( "HTTP status %d: failed to submit to %s" , self . response . status_code , self . response . url , ) return False if not self . parsed_response : logger . error ( "S...
Checks that the response is valid and import succeeded .
14,742
def get_targets ( self ) : if self . xml_root . tag == "testcases" : self . submit_target = self . config . get ( "testcase_taget" ) self . queue_url = self . config . get ( "testcase_queue" ) self . log_url = self . config . get ( "testcase_log" ) elif self . xml_root . tag == "testsuites" : self . submit_target = sel...
Sets targets .
14,743
def get_credentials ( self , ** kwargs ) : login = ( kwargs . get ( "user" ) or os . environ . get ( "POLARION_USERNAME" ) or self . config . get ( "username" ) ) pwd = ( kwargs . get ( "password" ) or os . environ . get ( "POLARION_PASSWORD" ) or self . config . get ( "password" ) ) if not all ( [ login , pwd ] ) : ra...
Sets credentails .
14,744
def e_164 ( msisdn : str ) -> str : number = phonenumbers . parse ( "+{}" . format ( msisdn . lstrip ( "+" ) ) , None ) return phonenumbers . format_number ( number , phonenumbers . PhoneNumberFormat . E164 )
Returns the msisdn in E . 164 international format .
14,745
def loadFile ( self , filePath ) : self . _filePath = filePath if self . _proc . state ( ) != QProcess . Running : self . _kill ( ) self . _run ( self . _filePath ) else : self . _execute ( "pausing_keep_force pt_step 1" ) self . _execute ( "get_property pause" ) self . _execute ( "loadfile \"%s\"" % self . _filePath )...
Loads a file
14,746
def play ( self ) : if self . _proc . state ( ) == QProcess . Running : if self . isPlaying is False : self . _execute ( "pause" ) self . _changePlayingState ( True ) elif self . _filePath is not None : self . _kill ( ) self . _run ( self . _filePath ) self . _changePlayingState ( True )
Starts a playback
14,747
def fourier ( x , N ) : term = 0. for n in range ( 1 , N , 2 ) : term += ( 1. / n ) * math . sin ( n * math . pi * x / L ) return ( 4. / ( math . pi ) ) * term
Fourier approximation with N terms
14,748
def parse_enum ( enum ) : docs = enum [ 'comment' ] code = '<span class="k">enum</span> <span class="gs">%s</span> {\n' % enum [ 'name' ] for v in enum [ "values" ] : if v [ 'comment' ] : for line in v [ 'comment' ] . split ( "\n" ) : code += ' <span class="c1">// %s</span>\n' % line code += ' <span class="nv">%s...
Returns a docco section for the given enum .
14,749
def parse_struct ( s ) : docs = s [ 'comment' ] code = '<span class="k">struct</span> <span class="gs">%s</span>' % s [ 'name' ] if s [ 'extends' ] : code += ' extends <span class="gs">%s</span>' % s [ 'extends' ] code += ' {\n' namelen = 0 typelen = 0 for v in s [ "fields" ] : tlen = len ( format_type ( v , includeOpt...
Returns a docco section for the given struct .
14,750
def parse_interface ( iface ) : sections = [ ] docs = iface [ 'comment' ] code = '<span class="k">interface</span> <span class="gs">%s</span> {\n' % iface [ 'name' ] for v in iface [ "functions" ] : func_code = ' <span class="nf">%s</span>(' % v [ 'name' ] i = 0 for p in v [ "params" ] : if i == 0 : i = 1 else : fun...
Returns a docco section for the given interface .
14,751
def to_sections ( idl_parsed ) : sections = [ ] for entity in idl_parsed : if entity [ "type" ] == "comment" : sections . append ( to_section ( entity [ "value" ] , "" ) ) elif entity [ "type" ] == "enum" : sections . append ( parse_enum ( entity ) ) elif entity [ "type" ] == "struct" : sections . append ( parse_struct...
Iterates through elements in idl_parsed list and returns a list of section dicts . Currently elements of type comment enum struct and interface are processed .
14,752
def sub_location ( self , nbr ) : assert nbr > - 1 , "Sub location number must be greater or equal to 0!" assert nbr < self . nbr_of_sub_locations ( ) - 1 , "Sub location number must be lower than %d!" % self . nbr_of_sub_locations ( ) - 1 return self . _locations_list [ nbr ]
Return a given sub location 0 - based .
14,753
def get_locations_list ( self , lower_bound = 0 , upper_bound = None ) : real_upper_bound = upper_bound if upper_bound is None : real_upper_bound = self . nbr_of_sub_locations ( ) try : return self . _locations_list [ lower_bound : real_upper_bound ] except : return list ( )
Return the internal location list .
14,754
def get_args ( args , kwargs , arg_names ) : n_args = len ( arg_names ) if len ( args ) + len ( kwargs ) > n_args : raise MoultScannerError ( 'Too many arguments supplied. Expected: {}' . format ( n_args ) ) out_args = { } for i , a in enumerate ( args ) : out_args [ arg_names [ i ] ] = a for a in arg_names : if a not ...
Get arguments as a dict .
14,755
def ast_scan_file ( filename , re_fallback = True ) : try : with io . open ( filename , 'rb' ) as fp : try : root = ast . parse ( fp . read ( ) , filename = filename ) except ( SyntaxError , IndentationError ) : if re_fallback : log . debug ( 'Falling back to regex scanner' ) return _ast_scan_file_re ( filename ) else ...
Scans a file for imports using AST .
14,756
def dump ( d , fmt = 'json' , stream = None ) : if fmt == 'json' : return _dump_json ( d , stream = stream ) elif fmt == 'yaml' : return yaml . dump ( d , stream ) elif fmt == 'lha' : s = _dump_lha ( d ) if stream is None : return s else : return stream . write ( s )
Serialize structured data into a stream in JSON YAML or LHA format . If stream is None return the produced string instead .
14,757
def inline_for_model ( model , variants = [ ] , inline_args = { } ) : if not isinstance ( model , ModelBase ) : raise ValueError ( "inline_for_model requires it's argument to be a Django Model" ) d = dict ( model = model ) if variants : d [ 'variants' ] = variants if inline_args : d [ 'args' ] = inline_args class_name ...
A shortcut function to produce ModelInlines for django models
14,758
def initialize_mpi ( mpi = False ) : if mpi : import mpi4py . MPI comm = mpi4py . MPI . COMM_WORLD rank = comm . Get_rank ( ) size = comm . Get_size ( ) else : comm = None rank = 0 size = 1 return { "comm" : comm , "rank" : rank , "size" : size , "mode" : mpi }
initialize mpi settings
14,759
def create_ports ( port , mpi , rank ) : if port == "random" or port is None : ports = { } else : port = int ( port ) ports = { "REQ" : port + 0 , "PUSH" : port + 1 , "SUB" : port + 2 } if mpi == 'all' : for port in ports : ports [ port ] += ( rank * 3 ) return ports
create a list of ports for the current rank
14,760
def import_from_string ( full_class_name ) : s = full_class_name . split ( '.' ) class_name = s [ - 1 ] module_name = full_class_name [ : - len ( class_name ) - 1 ] module = importlib . import_module ( module_name ) klass = getattr ( module , class_name ) return klass
return a class based on it s full class name
14,761
def create_bmi_model ( self , engine , bmi_class = None , wrapper_kwargs = None ) : if wrapper_kwargs is None : wrapper_kwargs = { } if bmi_class is None : wrapper_class = bmi . wrapper . BMIWrapper else : wrapper_class = self . import_from_string ( bmi_class ) try : model = wrapper_class ( engine , ** wrapper_kwargs )...
initialize a bmi mode using an optional class
14,762
def register ( self ) : result = requests . post ( urljoin ( self . tracker , 'models' ) , data = json . dumps ( self . metadata ) ) logger . debug ( "registered at server %s: %s" , self . tracker , result ) self . metadata [ "tracker" ] = result . json ( )
register model at tracking server
14,763
def unregister ( self ) : uuid = self . metadata [ "tracker" ] [ "uuid" ] result = requests . delete ( urljoin ( self . tracker , 'models' + "/" + uuid ) ) logger . debug ( "unregistered at server %s with %s: %s" , self . tracker , uuid , result )
unregister model at tracking server
14,764
def create_sockets ( self ) : ports = self . ports context = zmq . Context ( ) poller = zmq . Poller ( ) rep = context . socket ( zmq . REP ) if "REQ" in ports : rep . bind ( "tcp://*:{port}" . format ( port = ports [ "REQ" ] ) ) else : ports [ "REQ" ] = rep . bind_to_random_port ( "tcp://*" ) pull = context . socket (...
create zmq sockets
14,765
def run ( self ) : model = self . model configfile = self . configfile interval = self . interval sockets = self . sockets model . initialize ( configfile ) if model . state == 'pause' : logger . info ( "model initialized and started in pause mode, waiting for requests" ) else : logger . info ( "model started and initi...
run the model
14,766
def calc_basics ( width = - 1 , length = - 1 , height = 2.4 , prevailing_wind = 2.8 ) : if width == - 1 : width = int ( input ( 'enter building width : ' ) ) if length == - 1 : length = int ( input ( 'enter building length : ' ) ) res = { } res [ 'area' ] = width * length res [ 'perim' ] = 2 * width + 2 * length res [ ...
calculate various aspects of the structure
14,767
def bld_rafter_deflection ( length = - 9 , force = - 9 , E_mod_elasticity = - 9 , I_moment_of_intertia = - 9 ) : if length == - 9 : length = float ( input ( 'enter rafter length : ' ) ) if force == - 9 : force = float ( input ( 'enter Force or weight applied to roof : ' ) ) if E_mod_elasticity == - 9 : E_mod_elasticity...
calculate rafter deflections - see test_calc_building_design . py for Sample values for equations below from Structures II course
14,768
def get_parties ( self , obj ) : return PartySerializer ( Party . objects . all ( ) , many = True ) . data
All parties .
14,769
def get_elections ( self , obj ) : election_day = ElectionDay . objects . get ( date = self . context [ "election_date" ] ) kwargs = { "race__office__body" : obj , "election_day" : election_day } if self . context . get ( "division" ) and obj . slug == "senate" : kwargs [ "division" ] = self . context [ "division" ] el...
All elections held on an election day .
14,770
def parse_channels ( self ) : channels = [ ] for channel in self . _project_dict [ "channels" ] : channels . append ( Channel ( channel , self . _is_sixteen_bit , self . _ignore_list ) ) return channels
Creates an array of Channel objects from the project
14,771
def update ( self ) : for channel in self . channels : channel . update ( ) for i in range ( len ( self . _project_dict [ "channels" ] ) ) : channel_dict = self . _project_dict [ "channels" ] [ i ] for channel in self . channels : if channel . name == channel_dict [ "common.ALLTYPES_NAME" ] : self . _project_dict [ "ch...
Updates the dictionary of the project
14,772
def cmd ( send , * _ ) : url = get ( 'http://distrowatch.com/random.php' ) . url match = re . search ( '=(.*)' , url ) if match : send ( match . group ( 1 ) ) else : send ( "no distro found" )
Gets a random distro .
14,773
def cmd ( send , * _ ) : try : output = subprocess . check_output ( [ 'ddate' ] , universal_newlines = True ) except subprocess . CalledProcessError : output = 'Today is the day you install ddate!' for line in output . splitlines ( ) : send ( line )
Returns the Discordian date .
14,774
def cmd ( send , msg , args ) : parser = arguments . ArgParser ( args [ 'config' ] ) parser . add_argument ( '--chan' , '--channel' , action = arguments . ChanParser ) try : cmdargs , extra = parser . parse_known_args ( msg ) except arguments . ArgumentException as e : send ( str ( e ) ) return target = cmdargs . chann...
Sets a mode .
14,775
def as_json ( context ) : info = { 'info' : cgi . escape ( pprint . pformat ( context . context ) ) , } return Response ( content_type = 'application/json' , body = json . dumps ( info ) )
Return an object s representation as JSON
14,776
def as_tree ( context ) : tree = _build_tree ( context , 2 , 1 ) if type ( tree ) == dict : tree = [ tree ] return Response ( content_type = 'application/json' , body = json . dumps ( tree ) )
Return info about an object s members as JSON
14,777
def main ( ) : character1 = Character ( 'Albogh' , str = 4 , int = 7 , sta = 50 ) character2 = Character ( 'Zoltor' , str = 6 , int = 6 , sta = 70 ) print ( 'PLAYER1 [start]:' , character1 ) print ( 'PLAYER2 [start]:' , character2 ) b = Battle ( character1 , character2 ) print ( b ) print ( 'PLAYER1 [end]:' , character...
Prototype to see how an RPG simulation might be used in the AIKIF framework . The idea is to build a simple character and run a simulation to see how it succeeds in a random world against another players character character stats world locations
14,778
def fight ( self , moves = 10 ) : for i in range ( 1 , moves ) : result , dmg = self . calc_move ( self . c1 , self . c2 ) print ( self . c1 . name + ' ' + result + ' for ' + str ( dmg ) ) self . c1 . sta = self . c1 . sta - dmg if self . is_character_dead ( self . c1 ) : print ( self . c1 . name + ' has died' ) return...
runs a series of fights
14,779
def duration ( self ) : if self . completion_ts : end = self . completed else : end = datetime . utcnow ( ) return end - self . started
Return a timedelta for this build .
14,780
def estimate_completion ( self ) : if self . state != build_states . BUILDING : defer . returnValue ( self . completed ) avg_delta = yield self . connection . getAverageBuildDuration ( self . name ) est_completion = self . started + avg_delta defer . returnValue ( est_completion )
Estimate completion time for a build .
14,781
def target ( self ) : task = yield self . task ( ) if not task : yield defer . succeed ( None ) defer . returnValue ( None ) defer . returnValue ( task . target )
Find the target name for this build .
14,782
def task ( self ) : if not self . task_id : return defer . succeed ( None ) return self . connection . getTaskInfo ( self . task_id )
Find the task for this build .
14,783
def task_id ( self ) : if self [ 'task_id' ] : return self [ 'task_id' ] if self . extra and 'container_koji_task_id' in self . extra : return self . extra [ 'container_koji_task_id' ]
Hack to return a task ID for a build including container CG builds .
14,784
def get_images ( self , obj ) : return { str ( i . tag ) : i . image . url for i in obj . images . all ( ) }
Object of images serialized by tag name .
14,785
def get_override_winner ( self , obj ) : if obj . election . division . level . name == DivisionLevel . DISTRICT : division = obj . election . division . parent else : division = obj . election . division vote = obj . votes . filter ( division = division ) . first ( ) return vote . winning if vote else False
Winner marked in backend .
14,786
def get_override_votes ( self , obj ) : if hasattr ( obj , "meta" ) : if obj . meta . override_ap_votes : all_votes = None for ce in obj . candidate_elections . all ( ) : if all_votes : all_votes = all_votes | ce . votes . all ( ) else : all_votes = ce . votes . all ( ) return VotesSerializer ( all_votes , many = True ...
Votes entered into backend . Only used if override_ap_votes = True .
14,787
def save ( self , * args , ** kwargs ) : if self . publication : publication = self . publication if not self . title : self . title = publication . title if not self . subtitle : first_author = publication . first_author if first_author == publication . last_author : authors = first_author else : authors = '{} et al.'...
Before saving if slide is for a publication use publication info for slide s title subtitle description .
14,788
def execute ( self , context = None , stdout = None , stderr = None ) : total_benchmark = Benchmark ( ) self . context = context or Context ( ) if self . _is_collection ( ) : self . stdout = sys . stdout self . stderr = sys . stderr else : self . stdout = stdout or StringIO ( ) self . stderr = stderr or StringIO ( ) se...
Does all the work of running an example .
14,789
def run ( self , context = None , stdout = None , stderr = None ) : "Like execute, but records a skip if the should_skip method returns True." if self . should_skip ( ) : self . _record_skipped_example ( self . formatter ) self . num_skipped += 1 else : self . execute ( context , stdout , stderr ) return self . num_suc...
Like execute but records a skip if the should_skip method returns True .
14,790
def _setup ( self ) : "Resets the state and prepares for running the example." self . example . error = None self . example . traceback = '' c = Context ( parent = self . context ) self . context = c if self . is_root_runner : run . before_all . execute ( self . context ) self . example . before ( self . context )
Resets the state and prepares for running the example .
14,791
def _execute_example_group ( self ) : "Handles the execution of Example Group" for example in self . example : runner = self . __class__ ( example , self . formatter ) runner . is_root_runner = False successes , failures , skipped = runner . run ( self . context ) self . num_successes += successes self . num_failures +...
Handles the execution of Example Group
14,792
def _execute_example ( self ) : "Handles the execution of the Example" test_benchmark = Benchmark ( ) try : with Registry ( ) , test_benchmark : if accepts_arg ( self . example . testfn ) : self . example . testfn ( self . context ) else : self . context . inject_into_self ( self . example . testfn ) self . example . t...
Handles the execution of the Example
14,793
def _teardown ( self ) : "Handles the restoration of any potential global state set." self . example . after ( self . context ) if self . is_root_runner : run . after_all . execute ( self . context ) self . has_ran = True
Handles the restoration of any potential global state set .
14,794
def pypy_json_encode ( value , pretty = False ) : global _dealing_with_problem if pretty : return pretty_json ( value ) try : _buffer = UnicodeBuilder ( 2048 ) _value2json ( value , _buffer ) output = _buffer . build ( ) return output except Exception as e : from mo_logs import Log if _dealing_with_problem : Log . erro...
pypy DOES NOT OPTIMIZE GENERATOR CODE WELL
14,795
def problem_serializing ( value , e = None ) : from mo_logs import Log try : typename = type ( value ) . __name__ except Exception : typename = "<error getting name>" try : rep = text_type ( repr ( value ) ) except Exception as _ : rep = None if rep == None : Log . error ( "Problem turning value of type {{type}} to jso...
THROW ERROR ABOUT SERIALIZING
14,796
def unicode_key ( key ) : if not isinstance ( key , ( text_type , binary_type ) ) : from mo_logs import Log Log . error ( "{{key|quote}} is not a valid key" , key = key ) return quote ( text_type ( key ) )
CONVERT PROPERTY VALUE TO QUOTED NAME OF SAME
14,797
def cmd ( send , _ , args ) : curr = datetime . now ( ) uptime = args [ 'handler' ] . uptime load_avg = ', ' . join ( [ str ( x ) for x in os . getloadavg ( ) ] ) starttime = curr - uptime [ 'start' ] reloaded = curr - uptime [ 'reloaded' ] send ( "Time since start: %s, load average: %s" % ( starttime , load_avg ) ) se...
Shows the bot s uptime .
14,798
def pcwd ( func ) : @ wraps ( func ) def inner ( * args , ** kw ) : with PreserveWorkingDirectory ( ) : return func ( * args , ** kw ) return inner
A decorator to provide the functionality of the PreserveWorkingDirectory context manager for functions and methods .
14,799
def cmd ( send , msg , _ ) : msg = msg . encode ( 'utf-8' ) send ( hashlib . sha512 ( msg ) . hexdigest ( ) )
SHA512 hashes something .