idx
int64
0
251k
question
stringlengths
53
3.53k
target
stringlengths
5
1.23k
len_question
int64
20
893
len_target
int64
3
238
7,100
def cli ( code , cif , parameters , daemon ) : from aiida import orm from aiida . plugins import factories from aiida_codtools . common . cli import CliParameters , CliRunner from aiida_codtools . common . resources import get_default_options process = factories . CalculationFactory ( code . get_attribute ( 'input_plugin' ) ) parameters = CliParameters . from_string ( parameters ) . get_dictionary ( ) inputs = { 'cif' : cif , 'code' : code , 'metadata' : { 'options' : get_default_options ( ) } } if parameters : inputs [ 'parameters' ] = orm . Dict ( dict = parameters ) cli_runner = CliRunner ( process , inputs ) cli_runner . run ( daemon = daemon )
Run any cod - tools calculation for the given CifData node .
186
14
7,101
def make ( parser ) : s = parser . add_subparsers ( title = 'commands' , metavar = 'COMMAND' , help = 'description' , ) def gen_pass_f ( args ) : gen_pass ( ) gen_pass_parser = s . add_parser ( 'gen-pass' , help = 'generate the password' ) gen_pass_parser . set_defaults ( func = gen_pass_f ) def cmd_f ( args ) : cmd ( args . user , args . hosts . split ( ',' ) , args . key_filename , args . password , args . run ) cmd_parser = s . add_parser ( 'cmd' , help = 'run command line on the target host' ) cmd_parser . add_argument ( '--run' , help = 'the command running on the remote node' , action = 'store' , default = None , dest = 'run' ) cmd_parser . set_defaults ( func = cmd_f )
DEPRECATED prepare OpenStack basic environment
222
8
7,102
def pw_converter ( handler , flt ) : import peewee as pw if isinstance ( flt , Filter ) : return flt model = handler . model field = getattr ( model , flt ) if isinstance ( field , pw . BooleanField ) : return PWBoolFilter ( flt ) if field . choices : choices = [ ( Filter . default , '---' ) ] + list ( field . choices ) return PWChoiceFilter ( flt , choices = choices ) return PWFilter ( flt )
Convert column name to filter .
115
7
7,103
def process ( self , * args , * * kwargs ) : super ( RawIDField , self ) . process ( * args , * * kwargs ) if self . object_data : self . description = self . description or str ( self . object_data )
Get a description .
58
4
7,104
def _value ( self ) : if self . data is not None : value = self . data . _data . get ( self . field . to_field . name ) return str ( value ) return ''
Get field value .
43
4
7,105
def sort ( self , request , reverse = False ) : field = self . model . _meta . fields . get ( self . columns_sort ) if not field : return self . collection if reverse : field = field . desc ( ) return self . collection . order_by ( field )
Sort current collection .
60
4
7,106
def value ( self , data ) : value = data . get ( self . name ) if value : return int ( value ) return self . default
Get value from data .
30
5
7,107
def get_fields ( node , fields_tag = "field_list" ) : fields_nodes = [ c for c in node . children if c . tagname == fields_tag ] if len ( fields_nodes ) == 0 : return { } assert len ( fields_nodes ) == 1 , "multiple nodes with tag " + fields_tag fields_node = fields_nodes [ 0 ] fields = [ { f . tagname : f . rawsource . strip ( ) for f in n . children } for n in fields_node . children if n . tagname == "field" ] return { f [ "field_name" ] : f [ "field_body" ] for f in fields }
Get the field names and their values from a node .
153
11
7,108
def extract_signature ( docstring ) : root = publish_doctree ( docstring , settings_overrides = { "report_level" : 5 } ) fields = get_fields ( root ) return fields . get ( SIG_FIELD )
Extract the signature from a docstring .
54
9
7,109
def split_parameter_types ( parameters ) : if parameters == "" : return [ ] # only consider the top level commas, ignore the ones in [] commas = [ ] bracket_depth = 0 for i , char in enumerate ( parameters ) : if ( char == "," ) and ( bracket_depth == 0 ) : commas . append ( i ) elif char == "[" : bracket_depth += 1 elif char == "]" : bracket_depth -= 1 types = [ ] last_i = 0 for i in commas : types . append ( parameters [ last_i : i ] . strip ( ) ) last_i = i + 1 else : types . append ( parameters [ last_i : ] . strip ( ) ) return types
Split a parameter types declaration into individual types .
160
9
7,110
def parse_signature ( signature ) : if " -> " not in signature : # signature comment: no parameters, treat variable type as return type param_types , return_type = None , signature . strip ( ) else : lhs , return_type = [ s . strip ( ) for s in signature . split ( " -> " ) ] csv = lhs [ 1 : - 1 ] . strip ( ) # remove the parentheses around the parameter type list param_types = split_parameter_types ( csv ) requires = set ( _RE_QUALIFIED_TYPES . findall ( signature ) ) return param_types , return_type , requires
Parse a signature into its input and return parameter types .
141
12
7,111
def get_aliases ( lines ) : aliases = { } for line in lines : line = line . strip ( ) if len ( line ) > 0 and line . startswith ( SIG_ALIAS ) : _ , content = line . split ( SIG_ALIAS ) alias , signature = [ t . strip ( ) for t in content . split ( "=" ) ] aliases [ alias ] = signature return aliases
Get the type aliases in the source .
87
8
7,112
def get_stub ( source , generic = False ) : generator = StubGenerator ( source , generic = generic ) stub = generator . generate_stub ( ) return stub
Get the stub code for a source code .
37
9
7,113
def get_mod_paths ( mod_name , out_dir ) : paths = [ ] try : mod = get_loader ( mod_name ) source = Path ( mod . path ) if source . name . endswith ( ".py" ) : source_rel = Path ( * mod_name . split ( "." ) ) if source . name == "__init__.py" : source_rel = source_rel . joinpath ( "__init__.py" ) destination = Path ( out_dir , source_rel . with_suffix ( ".pyi" ) ) paths . append ( ( source , destination ) ) except Exception as e : _logger . debug ( e ) _logger . warning ( "cannot handle module, skipping: %s" , mod_name ) return paths
Get source and stub paths for a module .
174
9
7,114
def get_pkg_paths ( pkg_name , out_dir ) : paths = [ ] try : pkg = import_module ( pkg_name ) if not hasattr ( pkg , "__path__" ) : return get_mod_paths ( pkg_name , out_dir ) for mod_info in walk_packages ( pkg . __path__ , pkg . __name__ + "." ) : mod_paths = get_mod_paths ( mod_info . name , out_dir ) paths . extend ( mod_paths ) except Exception as e : _logger . debug ( e ) _logger . warning ( "cannot handle package, skipping: %s" , pkg_name ) return paths
Recursively get all source and stub paths for a package .
164
13
7,115
def process_docstring ( app , what , name , obj , options , lines ) : aliases = getattr ( app , "_sigaliases" , None ) if aliases is None : if what == "module" : aliases = get_aliases ( inspect . getsource ( obj ) . splitlines ( ) ) app . _sigaliases = aliases sig_marker = ":" + SIG_FIELD + ":" is_class = what in ( "class" , "exception" ) signature = extract_signature ( "\n" . join ( lines ) ) if signature is None : if not is_class : return init_method = getattr ( obj , "__init__" ) init_doc = init_method . __doc__ init_lines = init_doc . splitlines ( ) [ 1 : ] if len ( init_lines ) > 1 : init_doc = textwrap . dedent ( "\n" . join ( init_lines [ 1 : ] ) ) init_lines = init_doc . splitlines ( ) if sig_marker not in init_doc : return sig_started = False for line in init_lines : if line . lstrip ( ) . startswith ( sig_marker ) : sig_started = True if sig_started : lines . append ( line ) signature = extract_signature ( "\n" . join ( lines ) ) if is_class : obj = init_method param_types , rtype , _ = parse_signature ( signature ) param_names = [ p for p in inspect . signature ( obj ) . parameters ] if is_class and ( param_names [ 0 ] == "self" ) : del param_names [ 0 ] # if something goes wrong, don't insert parameter types if len ( param_names ) == len ( param_types ) : for name , type_ in zip ( param_names , param_types ) : find = ":param %(name)s:" % { "name" : name } alias = aliases . get ( type_ ) if alias is not None : type_ = "*%(type)s* :sup:`%(alias)s`" % { "type" : type_ , "alias" : alias } for i , line in enumerate ( lines ) : if line . startswith ( find ) : lines . insert ( i , ":type %(name)s: %(type)s" % { "name" : name , "type" : type_ } ) break if not is_class : for i , line in enumerate ( lines ) : if line . startswith ( ( ":return:" , ":returns:" ) ) : lines . insert ( i , ":rtype: " + rtype ) break # remove the signature field sig_start = 0 while sig_start < len ( lines ) : if lines [ sig_start ] . startswith ( sig_marker ) : break sig_start += 1 sig_end = sig_start + 1 while sig_end < len ( lines ) : if ( not lines [ sig_end ] ) or ( lines [ sig_end ] [ 0 ] != " " ) : break sig_end += 1 for i in reversed ( range ( sig_start , sig_end ) ) : del lines [ i ]
Modify the docstring before generating documentation .
711
9
7,116
def main ( argv = None ) : parser = ArgumentParser ( prog = "pygenstub" ) parser . add_argument ( "--version" , action = "version" , version = "%(prog)s " + __version__ ) parser . add_argument ( "files" , nargs = "*" , help = "generate stubs for given files" ) parser . add_argument ( "-m" , "--module" , action = "append" , metavar = "MODULE" , dest = "modules" , default = [ ] , help = "generate stubs for given modules" , ) parser . add_argument ( "-o" , "--output" , metavar = "PATH" , dest = "out_dir" , help = "change the output directory" ) parser . add_argument ( "--generic" , action = "store_true" , default = False , help = "generate generic stubs" ) parser . add_argument ( "--debug" , action = "store_true" , help = "enable debug messages" ) argv = argv if argv is not None else sys . argv arguments = parser . parse_args ( argv [ 1 : ] ) # set debug mode if arguments . debug : logging . basicConfig ( level = logging . DEBUG ) _logger . debug ( "running in debug mode" ) out_dir = arguments . out_dir if arguments . out_dir is not None else "" if ( out_dir == "" ) and ( len ( arguments . modules ) > 0 ) : print ( "Output directory must be given when generating stubs for modules." ) sys . exit ( 1 ) modules = [ ] for path in arguments . files : paths = Path ( path ) . glob ( "**/*.py" ) if Path ( path ) . is_dir ( ) else [ Path ( path ) ] for source in paths : if str ( source ) . startswith ( os . path . pardir ) : source = source . absolute ( ) . resolve ( ) if ( out_dir != "" ) and source . is_absolute ( ) : source = source . relative_to ( source . root ) destination = Path ( out_dir , source . with_suffix ( ".pyi" ) ) modules . append ( ( source , destination ) ) for mod_name in arguments . modules : modules . extend ( get_pkg_paths ( mod_name , out_dir ) ) for source , destination in modules : _logger . info ( "generating stub for %s to path %s" , source , destination ) with source . open ( ) as f : code = f . read ( ) try : stub = get_stub ( code , generic = arguments . generic ) except Exception as e : print ( source , "-" , e , file = sys . stderr ) continue if stub != "" : if not destination . parent . exists ( ) : destination . parent . mkdir ( parents = True ) with destination . open ( "w" ) as f : f . write ( "# " + EDIT_WARNING + "\n\n" + stub )
Start the command line interface .
674
6
7,117
def add_variable ( self , node ) : if node . name not in self . variable_names : self . variables . append ( node ) self . variable_names . add ( node . name ) node . parent = self
Add a variable node to this node .
47
8
7,118
def get_code ( self ) : stub = [ ] for child in self . variables : stub . extend ( child . get_code ( ) ) if ( ( len ( self . variables ) > 0 ) and ( len ( self . children ) > 0 ) and ( not isinstance ( self , ClassNode ) ) ) : stub . append ( "" ) for child in self . children : stub . extend ( child . get_code ( ) ) return stub
Get the stub code for this node .
95
8
7,119
def get_code ( self ) : stub = [ ] for deco in self . decorators : if ( deco in DECORATORS ) or deco . endswith ( ".setter" ) : stub . append ( "@" + deco ) parameters = [ ] for name , type_ , has_default in self . parameters : decl = "%(n)s%(t)s%(d)s" % { "n" : name , "t" : ": " + type_ if type_ else "" , "d" : " = ..." if has_default else "" , } parameters . append ( decl ) slots = { "a" : "async " if self . _async else "" , "n" : self . name , "p" : ", " . join ( parameters ) , "r" : self . rtype , } prototype = "%(a)sdef %(n)s(%(p)s) -> %(r)s: ..." % slots if len ( prototype ) <= LINE_LENGTH_LIMIT : stub . append ( prototype ) elif len ( INDENT + slots [ "p" ] ) <= LINE_LENGTH_LIMIT : stub . append ( "%(a)sdef %(n)s(" % slots ) stub . append ( INDENT + slots [ "p" ] ) stub . append ( ") -> %(r)s: ..." % slots ) else : stub . append ( "%(a)sdef %(n)s(" % slots ) for param in parameters : stub . append ( INDENT + param + "," ) stub . append ( ") -> %(r)s: ..." % slots ) return stub
Get the stub code for this function .
368
8
7,120
def get_code ( self ) : stub = [ ] bases = ( "(" + ", " . join ( self . bases ) + ")" ) if len ( self . bases ) > 0 else "" slots = { "n" : self . name , "b" : bases } if ( len ( self . children ) == 0 ) and ( len ( self . variables ) == 0 ) : stub . append ( "class %(n)s%(b)s: ..." % slots ) else : stub . append ( "class %(n)s%(b)s:" % slots ) super_code = super ( ) . get_code ( ) if PY3 else StubNode . get_code ( self ) for line in super_code : stub . append ( INDENT + line ) return stub
Get the stub code for this class .
170
8
7,121
def collect_aliases ( self ) : self . aliases = get_aliases ( self . _code_lines ) for alias , signature in self . aliases . items ( ) : _ , _ , requires = parse_signature ( signature ) self . required_types |= requires self . defined_types |= { alias }
Collect the type aliases in the source .
69
8
7,122
def visit_Import ( self , node ) : line = self . _code_lines [ node . lineno - 1 ] module_name = line . split ( "import" ) [ 0 ] . strip ( ) for name in node . names : imported_name = name . name if name . asname : imported_name = name . asname + "::" + imported_name self . imported_namespaces [ imported_name ] = module_name
Visit an import node .
96
5
7,123
def visit_ImportFrom ( self , node ) : line = self . _code_lines [ node . lineno - 1 ] module_name = line . split ( "from" ) [ 1 ] . split ( "import" ) [ 0 ] . strip ( ) for name in node . names : imported_name = name . name if name . asname : imported_name = name . asname + "::" + imported_name self . imported_names [ imported_name ] = module_name
Visit an from - import node .
106
7
7,124
def visit_Assign ( self , node ) : line = self . _code_lines [ node . lineno - 1 ] if SIG_COMMENT in line : line = _RE_COMMENT_IN_STRING . sub ( "" , line ) if ( SIG_COMMENT not in line ) and ( not self . generic ) : return if SIG_COMMENT in line : _ , signature = line . split ( SIG_COMMENT ) _ , return_type , requires = parse_signature ( signature ) self . required_types |= requires parent = self . _parents [ - 1 ] for var in node . targets : if isinstance ( var , ast . Name ) : name , p = var . id , parent elif ( isinstance ( var , ast . Attribute ) and isinstance ( var . value , ast . Name ) and ( var . value . id == "self" ) ) : name , p = var . attr , parent . parent else : name , p = None , None if name is not None : if self . generic : return_type = "Any" self . required_types . add ( return_type ) stub_node = VariableNode ( name , return_type ) p . add_variable ( stub_node )
Visit an assignment node .
267
5
7,125
def visit_FunctionDef ( self , node ) : node = self . get_function_node ( node ) if node is not None : node . _async = False
Visit a function node .
36
5
7,126
def visit_AsyncFunctionDef ( self , node ) : node = self . get_function_node ( node ) if node is not None : node . _async = True
Visit an async function node .
37
6
7,127
def visit_ClassDef ( self , node ) : self . defined_types . add ( node . name ) bases = [ ] for n in node . bases : base_parts = [ ] while True : if not isinstance ( n , ast . Attribute ) : base_parts . append ( n . id ) break else : base_parts . append ( n . attr ) n = n . value bases . append ( "." . join ( base_parts [ : : - 1 ] ) ) self . required_types |= set ( bases ) signature = get_signature ( node ) stub_node = ClassNode ( node . name , bases = bases , signature = signature ) self . _parents [ - 1 ] . add_child ( stub_node ) self . _parents . append ( stub_node ) self . generic_visit ( node ) del self . _parents [ - 1 ]
Visit a class node .
190
5
7,128
def generate_import_from ( module_ , names ) : regular_names = [ n for n in names if "::" not in n ] as_names = [ n for n in names if "::" in n ] line = "" if len ( regular_names ) > 0 : slots = { "m" : module_ , "n" : ", " . join ( sorted ( regular_names ) ) } line = "from %(m)s import %(n)s" % slots if len ( line ) > LINE_LENGTH_LIMIT : slots [ "n" ] = INDENT + ( ",\n" + INDENT ) . join ( sorted ( regular_names ) ) + "," line = "from %(m)s import (\n%(n)s\n)" % slots if len ( as_names ) > 0 : line += "\n" for as_name in as_names : a , n = as_name . split ( "::" ) line += "from %(m)s import %(n)s as %(a)s" % { "m" : module_ , "n" : n , "a" : a } return line
Generate an import line .
259
6
7,129
def has_csv_permission ( self , request , obj = None ) : if getattr ( settings , 'DJANGO_EXPORTS_REQUIRE_PERM' , None ) : opts = self . opts codename = '%s_%s' % ( 'csv' , opts . object_name . lower ( ) ) return request . user . has_perm ( "%s.%s" % ( opts . app_label , codename ) ) return True
Returns True if the given request has permission to add an object . Can be overridden by the user in subclasses . By default we assume all staff users can use this action unless DJANGO_EXPORTS_REQUIRE_PERM is set to True in your django settings .
105
58
7,130
def assoc ( self , key , value ) : copydict = ImmutableDict ( ) copydict . tree = self . tree . assoc ( hash ( key ) , ( key , value ) ) copydict . _length = self . _length + 1 return copydict
Returns a new ImmutableDict instance with value associated with key . The implicit parameter is not modified .
58
21
7,131
def update ( self , other = None , * * kwargs ) : copydict = ImmutableDict ( ) if other : vallist = [ ( hash ( key ) , ( key , other [ key ] ) ) for key in other ] else : vallist = [ ] if kwargs : vallist += [ ( hash ( key ) , ( key , kwargs [ key ] ) ) for key in kwargs ] copydict . tree = self . tree . multi_assoc ( vallist ) copydict . _length = iter_length ( copydict . tree ) return copydict
Takes the same arguments as the update method in the builtin dict class . However this version returns a new ImmutableDict instead of modifying in - place .
131
33
7,132
def remove ( self , key ) : copydict = ImmutableDict ( ) copydict . tree = self . tree . remove ( hash ( key ) ) copydict . _length = self . _length - 1 return copydict
Returns a new ImmutableDict with the given key removed .
48
13
7,133
def _load_config ( self ) : config = SafeConfigParser ( ) config_file = os . path . join ( self . config_path , 'settings.ini' ) config . read ( config_file ) for option , type in list ( AVAILABLE_OPTIONS . items ( ) ) : if config . has_option ( 'DEFAULT' , option ) : if type == 'int' : value = config . getint ( 'DEFAULT' , option ) if type == 'float' : value = config . getfloat ( 'DEFAULT' , option ) if type == 'bool' : value = config . getboolean ( 'DEFAULT' , option ) if type == 'str' : value = config . get ( 'DEFAULT' , option ) self . livestreamer . set_option ( option , value )
Load and parse config file pass options to livestreamer
179
10
7,134
def urn ( self , value : Union [ URN , str ] ) : if isinstance ( value , str ) : value = URN ( value ) elif not isinstance ( value , URN ) : raise TypeError ( "New urn must be string or {} instead of {}" . format ( type ( URN ) , type ( value ) ) ) self . _urn = value
Set the urn
83
4
7,135
def get_cts_metadata ( self , key : str , lang : str = None ) -> Literal : return self . metadata . get_single ( RDF_NAMESPACES . CTS . term ( key ) , lang )
Get easily a metadata from the CTS namespace
51
9
7,136
def set_metadata_from_collection ( self , text_metadata : CtsTextMetadata ) : edition , work , textgroup = tuple ( ( [ text_metadata ] + text_metadata . parents ) [ : 3 ] ) for node in textgroup . metadata . get ( RDF_NAMESPACES . CTS . groupname ) : lang = node . language self . metadata . add ( RDF_NAMESPACES . CTS . groupname , lang = lang , value = str ( node ) ) self . set_creator ( str ( node ) , lang ) for node in work . metadata . get ( RDF_NAMESPACES . CTS . title ) : lang = node . language self . metadata . add ( RDF_NAMESPACES . CTS . title , lang = lang , value = str ( node ) ) self . set_title ( str ( node ) , lang ) for node in edition . metadata . get ( RDF_NAMESPACES . CTS . label ) : lang = node . language self . metadata . add ( RDF_NAMESPACES . CTS . label , lang = lang , value = str ( node ) ) self . set_subject ( str ( node ) , lang ) for node in edition . metadata . get ( RDF_NAMESPACES . CTS . description ) : lang = node . language self . metadata . add ( RDF_NAMESPACES . CTS . description , lang = lang , value = str ( node ) ) self . set_description ( str ( node ) , lang ) if not self . citation . is_set ( ) and edition . citation . is_set ( ) : self . citation = edition . citation
Set the object metadata using its collections recursively
365
10
7,137
def create_datapoint ( value , timestamp = None , * * tags ) : if timestamp is None : timestamp = time_millis ( ) if type ( timestamp ) is datetime : timestamp = datetime_to_time_millis ( timestamp ) item = { 'timestamp' : timestamp , 'value' : value } if tags is not None : item [ 'tags' ] = tags return item
Creates a single datapoint dict with a value timestamp and tags .
86
15
7,138
def create_metric ( metric_type , metric_id , data ) : if not isinstance ( data , list ) : data = [ data ] return { 'type' : metric_type , 'id' : metric_id , 'data' : data }
Create Hawkular - Metrics submittable structure .
56
11
7,139
def put ( self , data ) : if not isinstance ( data , list ) : data = [ data ] r = collections . defaultdict ( list ) for d in data : metric_type = d . pop ( 'type' , None ) if metric_type is None : raise HawkularError ( 'Undefined MetricType' ) r [ metric_type ] . append ( d ) # This isn't transactional, but .. ouh well. One can always repost everything. for l in r : self . _post ( self . _get_metrics_raw_url ( self . _get_url ( l ) ) , r [ l ] , parse_json = False )
Send multiple different metric_ids to the server in a single batch . Metrics can be a mixture of types .
146
23
7,140
def push ( self , metric_type , metric_id , value , timestamp = None ) : if type ( timestamp ) is datetime : timestamp = datetime_to_time_millis ( timestamp ) item = create_metric ( metric_type , metric_id , create_datapoint ( value , timestamp ) ) self . put ( item )
Pushes a single metric_id datapoint combination to the server .
75
15
7,141
def query_metric ( self , metric_type , metric_id , start = None , end = None , * * query_options ) : if start is not None : if type ( start ) is datetime : query_options [ 'start' ] = datetime_to_time_millis ( start ) else : query_options [ 'start' ] = start if end is not None : if type ( end ) is datetime : query_options [ 'end' ] = datetime_to_time_millis ( end ) else : query_options [ 'end' ] = end return self . _get ( self . _get_metrics_raw_url ( self . _get_metrics_single_url ( metric_type , metric_id ) ) , * * query_options )
Query for metrics datapoints from the server .
173
11
7,142
def query_metric_stats ( self , metric_type , metric_id = None , start = None , end = None , bucketDuration = None , * * query_options ) : if start is not None : if type ( start ) is datetime : query_options [ 'start' ] = datetime_to_time_millis ( start ) else : query_options [ 'start' ] = start if end is not None : if type ( end ) is datetime : query_options [ 'end' ] = datetime_to_time_millis ( end ) else : query_options [ 'end' ] = end if bucketDuration is not None : if type ( bucketDuration ) is timedelta : query_options [ 'bucketDuration' ] = timedelta_to_duration ( bucketDuration ) else : query_options [ 'bucketDuration' ] = bucketDuration if metric_id is not None : url = self . _get_metrics_stats_url ( self . _get_metrics_single_url ( metric_type , metric_id ) ) else : if len ( query_options ) < 0 : raise HawkularError ( 'Tags are required when querying without metric_id' ) url = self . _get_metrics_stats_url ( self . _get_url ( metric_type ) ) return self . _get ( url , * * query_options )
Query for metric aggregates from the server . This is called buckets in the Hawkular - Metrics documentation .
302
22
7,143
def query_metric_definition ( self , metric_type , metric_id ) : return self . _get ( self . _get_metrics_single_url ( metric_type , metric_id ) )
Query definition of a single metric id .
46
8
7,144
def query_metric_definitions ( self , metric_type = None , id_filter = None , * * tags ) : params = { } if id_filter is not None : params [ 'id' ] = id_filter if metric_type is not None : params [ 'type' ] = MetricType . short ( metric_type ) if len ( tags ) > 0 : params [ 'tags' ] = self . _transform_tags ( * * tags ) return self . _get ( self . _get_url ( ) , * * params )
Query available metric definitions .
120
5
7,145
def query_tag_values ( self , metric_type = None , * * tags ) : tagql = self . _transform_tags ( * * tags ) return self . _get ( self . _get_metrics_tags_url ( self . _get_url ( metric_type ) ) + '/{}' . format ( tagql ) )
Query for possible tag values .
76
6
7,146
def query_metric_tags ( self , metric_type , metric_id ) : definition = self . _get ( self . _get_metrics_tags_url ( self . _get_metrics_single_url ( metric_type , metric_id ) ) ) return definition
Returns a list of tags in the metric definition .
62
10
7,147
def delete_metric_tags ( self , metric_type , metric_id , * * deleted_tags ) : tags = self . _transform_tags ( * * deleted_tags ) tags_url = self . _get_metrics_tags_url ( self . _get_metrics_single_url ( metric_type , metric_id ) ) + '/{0}' . format ( tags ) self . _delete ( tags_url )
Delete one or more tags from the metric definition .
97
10
7,148
def create_tenant ( self , tenant_id , retentions = None ) : item = { 'id' : tenant_id } if retentions is not None : item [ 'retentions' ] = retentions self . _post ( self . _get_tenants_url ( ) , json . dumps ( item , indent = 2 ) )
Create a tenant . Currently nothing can be set ( to be fixed after the master version of Hawkular - Metrics has fixed implementation .
78
27
7,149
def get_default_options ( num_machines = 1 , max_wallclock_seconds = 1800 , withmpi = False ) : return { 'resources' : { 'num_machines' : int ( num_machines ) } , 'max_wallclock_seconds' : int ( max_wallclock_seconds ) , 'withmpi' : withmpi , }
Return an instance of the options dictionary with the minimally required parameters for a JobCalculation and set to default values unless overriden
86
27
7,150
def take_screenshot ( self ) : if not self . failed : return browser = getattr ( world , 'browser' , None ) if not browser : return try : scenario_name = self . scenario . name scenario_index = self . scenario . feature . scenarios . index ( self . scenario ) + 1 except AttributeError : scenario_name = self . background . keyword scenario_index = 0 if self . outline is None : outline_index_str = '' else : outline_index = self . scenario . outlines . index ( self . outline ) + 1 outline_index_str = '_{}' . format ( outline_index ) base_name = FORMAT . format ( feature_file = os . path . relpath ( self . feature . filename ) , scenario_index = scenario_index , scenario_name = scenario_name , outline_index = outline_index_str , ) base_name = re . sub ( r'\W' , '_' , base_name , flags = re . UNICODE ) base_name = os . path . join ( DIRECTORY , base_name ) world . browser . save_screenshot ( '{}.png' . format ( base_name ) ) with open ( '{}.html' . format ( base_name ) , 'w' ) as page_source_file : page_source_file . write ( world . browser . page_source )
Take a screenshot after a failed step .
301
8
7,151
def kunc_p ( v , v0 , k0 , k0p , order = 5 ) : return cal_p_kunc ( v , [ v0 , k0 , k0p ] , order = order , uncertainties = isuncertainties ( [ v , v0 , k0 , k0p ] ) )
calculate Kunc EOS see Dorogokupets 2015 for detail
71
16
7,152
def cal_p_kunc ( v , k , order = 5 , uncertainties = True ) : v0 = k [ 0 ] k0 = k [ 1 ] k0p = k [ 2 ] x = np . power ( v / v0 , 1. / 3. ) f1 = ( 1. - x ) / ( np . power ( x , order ) ) if uncertainties : f2 = unp . exp ( ( 1.5 * k0p - order + 0.5 ) * ( 1. - x ) ) else : f2 = np . exp ( ( 1.5 * k0p - order + 0.5 ) * ( 1. - x ) ) p = 3. * k0 * f1 * f2 return p
calculate Kunc EOS see Dorogokupets2015 for functional form
160
17
7,153
def find_files ( path = '' , ext = '' , level = None , typ = list , dirs = False , files = True , verbosity = 0 ) : gen = generate_files ( path , ext = ext , level = level , dirs = dirs , files = files , verbosity = verbosity ) if isinstance ( typ ( ) , collections . Mapping ) : return typ ( ( ff [ 'path' ] , ff ) for ff in gen ) elif typ is not None : return typ ( gen ) else : return gen
Recursively find all files in the indicated directory
116
10
7,154
def serialize ( self ) : if type ( self . value ) == int : return "i{:X}s" . format ( self . value ) . encode ( 'ascii' ) if type ( self . value ) == str : value = self . value . encode ( 'utf-8' ) return "{:X}:" . format ( len ( value ) ) . encode ( 'ascii' ) + value if type ( self . value ) == bytes : value = base64 . standard_b64encode ( self . value ) return "u{:X}:" . format ( len ( value ) ) . encode ( 'ascii' ) + value if type ( self . value ) == list : items = [ LiveMessageToken ( m ) . serialize ( ) for m in self . value ] return b'l' + b'' . join ( items ) + b's' if type ( self . value ) == dict : items = [ ] for key , value in self . value . items ( ) : items . append ( LiveMessageToken ( str ( key ) ) . serialize ( ) ) items . append ( LiveMessageToken ( value ) . serialize ( ) ) return b'h' + b'' . join ( items ) + b's' raise RuntimeError ( "Unknown type %s" % type ( self . value ) )
Serialize the token and return it as bytes .
289
10
7,155
def write ( self , file_or_path , append = False , timeout = 10 ) : if isinstance ( file_or_path , six . string_types ) : if self . coverage : file_or_path = get_smother_filename ( file_or_path , self . coverage . config . parallel ) outfile = Lock ( file_or_path , mode = 'a+' , timeout = timeout , fail_when_locked = False ) else : outfile = noclose ( file_or_path ) with outfile as fh : if append : fh . seek ( 0 ) try : other = Smother . load ( fh ) except ValueError : # no smother data pass else : self |= other fh . seek ( 0 ) fh . truncate ( ) # required to overwrite data in a+ mode json . dump ( self . data , fh )
Write Smother results to a file .
193
8
7,156
def query_context ( self , regions , file_factory = PythonFile ) : result = set ( ) for region in regions : try : pf = file_factory ( region . filename ) except InvalidPythonFile : continue # region and/or coverage report may use paths # relative to this directory. Ensure we find a match # if they use different conventions. paths = { os . path . abspath ( region . filename ) , os . path . relpath ( region . filename ) } for test_context , hits in six . iteritems ( self . data ) : if test_context in result : continue for path in paths : if region . intersects ( pf , hits . get ( path , [ ] ) ) : result . add ( test_context ) return QueryResult ( result )
Return which set of test contexts intersect a set of code regions .
167
13
7,157
def add_child ( self , child ) : if isinstance ( child , BaseCitation ) : self . _children . append ( child )
Adds a child to the CitationSet
30
7
7,158
def depth ( self ) -> int : if len ( self . children ) : return 1 + max ( [ child . depth for child in self . children ] ) else : return 1
Depth of the citation scheme
37
5
7,159
def set_link ( self , prop , value ) : # https://rdflib.readthedocs.io/en/stable/ # URIRef == identifiers (urn, http, URI in general) # Literal == String or Number (can have a language) # BNode == Anonymous nodes (So no specific identifier) # eg. BNode : Edition(MartialEpigrams:URIRef) ---has_metadata--> Metadata(BNode) if not isinstance ( value , URIRef ) : value = URIRef ( value ) self . metadata . add ( prop , value )
Set given link in CTS Namespace
130
8
7,160
def editions ( self ) : return [ item for urn , item in self . parent . children . items ( ) if isinstance ( item , CtsEditionMetadata ) ]
Get all editions of the texts
38
6
7,161
def get_description ( self , lang = None ) : return self . metadata . get_single ( key = RDF_NAMESPACES . CTS . description , lang = lang )
Get the DC description of the object
40
7
7,162
def lang ( self ) : return str ( self . graph . value ( self . asNode ( ) , DC . language ) )
Languages this text is in
27
6
7,163
def lang ( self , lang ) : self . graph . set ( ( self . asNode ( ) , DC . language , Literal ( lang ) ) )
Language this text is available in
33
6
7,164
def update ( self , other ) : if not isinstance ( other , CtsWorkMetadata ) : raise TypeError ( "Cannot add %s to CtsWorkMetadata" % type ( other ) ) elif self . urn != other . urn : raise InvalidURN ( "Cannot add CtsWorkMetadata %s to CtsWorkMetadata %s " % ( self . urn , other . urn ) ) for urn , text in other . children . items ( ) : self . texts [ urn ] = text self . texts [ urn ] . parent = self self . texts [ urn ] . resource = None return self
Merge two XmlCtsWorkMetadata Objects .
141
12
7,165
def get_translation_in ( self , key = None ) : if key is not None : return [ item for item in self . texts . values ( ) if isinstance ( item , CtsTranslationMetadata ) and item . lang == key ] else : return [ item for item in self . texts . values ( ) if isinstance ( item , CtsTranslationMetadata ) ]
Find a translation with given language
80
6
7,166
def update ( self , other ) : if not isinstance ( other , CtsTextgroupMetadata ) : raise TypeError ( "Cannot add %s to CtsTextgroupMetadata" % type ( other ) ) elif str ( self . urn ) != str ( other . urn ) : raise InvalidURN ( "Cannot add CtsTextgroupMetadata %s to CtsTextgroupMetadata %s " % ( self . urn , other . urn ) ) for urn , work in other . works . items ( ) : if urn in self . works : self . works [ urn ] . update ( deepcopy ( work ) ) else : self . works [ urn ] = deepcopy ( work ) self . works [ urn ] . parent = self self . works [ urn ] . resource = None return self
Merge two Textgroup Objects .
181
7
7,167
def get ( self , tags = [ ] , trigger_ids = [ ] ) : params = { } if len ( tags ) > 0 : params [ 'tags' ] = ',' . join ( tags ) if len ( trigger_ids ) > 0 : params [ 'triggerIds' ] = ',' . join ( trigger_ids ) url = self . _service_url ( 'triggers' , params = params ) triggers_dict = self . _get ( url ) return Trigger . list_to_object_list ( triggers_dict )
Get triggers with optional filtering . Querying without parameters returns all the trigger definitions .
117
17
7,168
def create ( self , trigger ) : data = self . _serialize_object ( trigger ) if isinstance ( trigger , FullTrigger ) : returned_dict = self . _post ( self . _service_url ( [ 'triggers' , 'trigger' ] ) , data ) return FullTrigger ( returned_dict ) else : returned_dict = self . _post ( self . _service_url ( 'triggers' ) , data ) return Trigger ( returned_dict )
Create a new trigger .
103
5
7,169
def update ( self , trigger_id , full_trigger ) : data = self . _serialize_object ( full_trigger ) rdict = self . _put ( self . _service_url ( [ 'triggers' , 'trigger' , trigger_id ] ) , data ) return FullTrigger ( rdict )
Update an existing full trigger .
69
6
7,170
def create_group ( self , trigger ) : data = self . _serialize_object ( trigger ) return Trigger ( self . _post ( self . _service_url ( [ 'triggers' , 'groups' ] ) , data ) )
Create a new group trigger .
53
6
7,171
def group_members ( self , group_id , include_orphans = False ) : params = { 'includeOrphans' : str ( include_orphans ) . lower ( ) } url = self . _service_url ( [ 'triggers' , 'groups' , group_id , 'members' ] , params = params ) return Trigger . list_to_object_list ( self . _get ( url ) )
Find all group member trigger definitions
92
6
7,172
def update_group ( self , group_id , trigger ) : data = self . _serialize_object ( trigger ) self . _put ( self . _service_url ( [ 'triggers' , 'groups' , group_id ] ) , data , parse_json = False )
Update an existing group trigger definition and its member definitions .
63
11
7,173
def delete_group ( self , group_id , keep_non_orphans = False , keep_orphans = False ) : params = { 'keepNonOrphans' : str ( keep_non_orphans ) . lower ( ) , 'keepOrphans' : str ( keep_orphans ) . lower ( ) } self . _delete ( self . _service_url ( [ 'triggers' , 'groups' , group_id ] , params = params ) )
Delete a group trigger
103
4
7,174
def create_group_member ( self , member ) : data = self . _serialize_object ( member ) return Trigger ( self . _post ( self . _service_url ( [ 'triggers' , 'groups' , 'members' ] ) , data ) )
Create a new member trigger for a parent trigger .
59
10
7,175
def set_group_conditions ( self , group_id , conditions , trigger_mode = None ) : data = self . _serialize_object ( conditions ) if trigger_mode is not None : url = self . _service_url ( [ 'triggers' , 'groups' , group_id , 'conditions' , trigger_mode ] ) else : url = self . _service_url ( [ 'triggers' , 'groups' , group_id , 'conditions' ] ) response = self . _put ( url , data ) return Condition . list_to_object_list ( response )
Set the group conditions .
133
5
7,176
def set_conditions ( self , trigger_id , conditions , trigger_mode = None ) : data = self . _serialize_object ( conditions ) if trigger_mode is not None : url = self . _service_url ( [ 'triggers' , trigger_id , 'conditions' , trigger_mode ] ) else : url = self . _service_url ( [ 'triggers' , trigger_id , 'conditions' ] ) response = self . _put ( url , data ) return Condition . list_to_object_list ( response )
Set the conditions for the trigger .
123
7
7,177
def conditions ( self , trigger_id ) : response = self . _get ( self . _service_url ( [ 'triggers' , trigger_id , 'conditions' ] ) ) return Condition . list_to_object_list ( response )
Get all conditions for a specific trigger .
55
8
7,178
def create_dampening ( self , trigger_id , dampening ) : data = self . _serialize_object ( dampening ) url = self . _service_url ( [ 'triggers' , trigger_id , 'dampenings' ] ) return Dampening ( self . _post ( url , data ) )
Create a new dampening .
73
6
7,179
def delete_dampening ( self , trigger_id , dampening_id ) : self . _delete ( self . _service_url ( [ 'triggers' , trigger_id , 'dampenings' , dampening_id ] ) )
Delete an existing dampening definition .
56
7
7,180
def update_dampening ( self , trigger_id , dampening_id ) : data = self . _serialize_object ( dampening ) url = self . _service_url ( [ 'triggers' , trigger_id , 'dampenings' , dampening_id ] ) return Dampening ( self . _put ( url , data ) )
Update an existing dampening definition .
80
7
7,181
def create_group_dampening ( self , group_id , dampening ) : data = self . _serialize_object ( dampening ) url = self . _service_url ( [ 'triggers' , 'groups' , group_id , 'dampenings' ] ) return Dampening ( self . _post ( url , data ) )
Create a new group dampening
79
6
7,182
def update_group_dampening ( self , group_id , dampening_id , dampening ) : data = self . _serialize_object ( dampening ) url = self . _service_url ( [ 'triggers' , 'groups' , group_id , 'dampenings' , dampening_id ] ) return Dampening ( self . _put ( url , data ) )
Update an existing group dampening
89
6
7,183
def delete_group_dampening ( self , group_id , dampening_id ) : self . _delete ( self . _service_url ( [ 'triggers' , 'groups' , group_id , 'dampenings' , dampening_id ] ) )
Delete an existing group dampening
62
6
7,184
def set_group_member_orphan ( self , member_id ) : self . _put ( self . _service_url ( [ 'triggers' , 'groups' , 'members' , member_id , 'orphan' ] ) , data = None , parse_json = False )
Make a non - orphan member trigger into an orphan .
65
11
7,185
def set_group_member_unorphan ( self , member_id , unorphan_info ) : data = self . _serialize_object ( unorphan_info ) data = self . _service_url ( [ 'triggers' , 'groups' , 'members' , member_id , 'unorphan' ] ) return Trigger ( self . _put ( url , data ) )
Make an orphan member trigger into an group trigger .
88
10
7,186
def enable ( self , trigger_ids = [ ] ) : trigger_ids = ',' . join ( trigger_ids ) url = self . _service_url ( [ 'triggers' , 'enabled' ] , params = { 'triggerIds' : trigger_ids , 'enabled' : 'true' } ) self . _put ( url , data = None , parse_json = False )
Enable triggers .
86
3
7,187
def get_twitter ( app_key = None , app_secret = None , search = 'python' , location = '' , * * kwargs ) : if not app_key : from settings_secret import TWITTER_API_KEY as app_key if not app_secret : from settings_secret import TWITTER_API_SECRET as app_secret twitter = Twython ( app_key , app_secret , oauth_version = 2 ) return Twython ( app_key , access_token = twitter . obtain_access_token ( ) )
Location may be specified with a string name or latitude longitude radius
121
13
7,188
def limitted_dump ( cursor = None , twitter = None , path = 'tweets.json' , limit = 450 , rate = TWITTER_SEARCH_RATE_LIMIT , indent = - 1 ) : if not twitter : twitter = get_twitter ( ) cursor = cursor or 'python' if isinstance ( cursor , basestring ) : cursor = get_cursor ( twitter , search = cursor ) newline = '\n' if indent is not None else '' if indent < 0 : indent = None # TODO: keep track of T0 for the optimal "reset" sleep duration with ( open ( path , 'w' ) if not isinstance ( path , file ) else path ) as f : f . write ( '[\n' ) for i , obj in enumerate ( cursor ) : f . write ( json . dumps ( obj , indent = indent ) ) if i < limit - 1 : f . write ( ',' + newline ) else : break remaining = int ( twitter . get_lastfunction_header ( 'x-rate-limit-remaining' ) ) if remaining > 0 : sleep ( 1. / rate ) else : sleep ( 15 * 60 ) f . write ( '\n]\n' )
Dump a limitted number of json . dump - able objects to the indicated file
268
17
7,189
def altshuler_grun ( v , v0 , gamma0 , gamma_inf , beta ) : x = v / v0 return gamma_inf + ( gamma0 - gamma_inf ) * np . power ( x , beta )
calculate Gruneisen parameter for Altshuler equation
52
13
7,190
def altshuler_debyetemp ( v , v0 , gamma0 , gamma_inf , beta , theta0 ) : x = v / v0 if isuncertainties ( [ v , v0 , gamma0 , gamma_inf , beta , theta0 ] ) : theta = theta0 * np . power ( x , - 1. * gamma_inf ) * unp . exp ( ( gamma0 - gamma_inf ) / beta * ( 1. - np . power ( x , beta ) ) ) else : theta = theta0 * np . power ( x , - 1. * gamma_inf ) * np . exp ( ( gamma0 - gamma_inf ) / beta * ( 1. - np . power ( x , beta ) ) ) return theta
calculate Debye temperature for Altshuler equation
170
12
7,191
def dorogokupets2007_pth ( v , temp , v0 , gamma0 , gamma_inf , beta , theta0 , n , z , three_r = 3. * constants . R , t_ref = 300. ) : v_mol = vol_uc2mol ( v , z ) # x = v_mol / v0_mol gamma = altshuler_grun ( v , v0 , gamma0 , gamma_inf , beta ) theta = altshuler_debyetemp ( v , v0 , gamma0 , gamma_inf , beta , theta0 ) def f ( t ) : xx = theta / t debye = debye_E ( xx ) Eth = three_r * n * t * debye return ( gamma / v_mol * Eth ) * 1.e-9 return f ( temp ) - f ( t_ref )
calculate thermal pressure for Dorogokupets 2007 EOS
197
14
7,192
def bind ( cls , app , * paths , methods = None , name = None , view = None ) : # Register self in admin if view is None : app . ps . admin . register ( cls ) if not paths : paths = ( '%s/%s' % ( app . ps . admin . cfg . prefix , name or cls . name ) , ) cls . url = paths [ 0 ] return super ( AdminHandler , cls ) . bind ( app , * paths , methods = methods , name = name , view = view )
Connect to admin interface and application .
119
7
7,193
def action ( cls , view ) : name = "%s:%s" % ( cls . name , view . __name__ ) path = "%s/%s" % ( cls . url , view . __name__ ) cls . actions . append ( ( view . __doc__ , path ) ) return cls . register ( path , name = name ) ( view )
Register admin view action .
83
5
7,194
async def dispatch ( self , request , * * kwargs ) : # Authorize request self . auth = await self . authorize ( request ) # Load collection self . collection = await self . load_many ( request ) # Load resource self . resource = await self . load_one ( request ) if request . method == 'GET' and self . resource is None : # Filter collection self . collection = await self . filter ( request ) # Sort collection self . columns_sort = request . query . get ( 'ap-sort' , self . columns_sort ) if self . columns_sort : reverse = self . columns_sort . startswith ( '-' ) self . columns_sort = self . columns_sort . lstrip ( '+-' ) self . collection = await self . sort ( request , reverse = reverse ) # Paginate collection try : self . offset = int ( request . query . get ( 'ap-offset' , 0 ) ) if self . limit : self . count = await self . count ( request ) self . collection = await self . paginate ( request ) except ValueError : pass return await super ( AdminHandler , self ) . dispatch ( request , * * kwargs )
Dispatch a request .
255
4
7,195
async def sort ( self , request , reverse = False ) : return sorted ( self . collection , key = lambda o : getattr ( o , self . columns_sort , 0 ) , reverse = reverse )
Sort collection .
44
3
7,196
async def get_form ( self , request ) : if not self . form : return None formdata = await request . post ( ) return self . form ( formdata , obj = self . resource )
Base point load resource .
43
5
7,197
async def get ( self , request ) : form = await self . get_form ( request ) ctx = dict ( active = self , form = form , request = request ) if self . resource : return self . app . ps . jinja2 . render ( self . template_item , * * ctx ) return self . app . ps . jinja2 . render ( self . template_list , * * ctx )
Get collection of resources .
93
5
7,198
def columns_formatter ( cls , colname ) : def wrapper ( func ) : cls . columns_formatters [ colname ] = func return func return wrapper
Decorator to mark a function as columns formatter .
36
12
7,199
def render_value ( self , data , column ) : renderer = self . columns_formatters . get ( column , format_value ) return renderer ( self , data , column )
Render value .
40
3