idx
int64
0
252k
question
stringlengths
48
5.28k
target
stringlengths
5
1.23k
236,100
def key_type ( self , type_ ) : if not self . is_mapping ( type_ ) : raise TypeError ( 'Type "%s" is not "mapping" container' % str ( type_ ) ) return self . __find_xxx_type ( type_ , self . key_type_index , self . key_type_typedef , 'container_key_type' )
returns reference to the class key type declaration
236,101
def remove_defaults ( self , type_or_string ) : name = type_or_string if not utils . is_str ( type_or_string ) : name = self . class_declaration ( type_or_string ) . name if not self . remove_defaults_impl : return name no_defaults = self . remove_defaults_impl ( name ) if not no_defaults : return name return no_defaul...
Removes template defaults from a templated class instantiation .
236,102
def take_parenting ( self , inst ) : if self is inst : return for decl in inst . declarations : decl . parent = self self . declarations . append ( decl ) inst . declarations = [ ]
Takes parenting from inst and transfers it to self .
236,103
def remove_declaration ( self , decl ) : del self . declarations [ self . declarations . index ( decl ) ] decl . cache . reset ( )
Removes declaration from members list .
236,104
def namespace ( self , name = None , function = None , recursive = None ) : return ( self . _find_single ( scopedef . scopedef_t . _impl_matchers [ namespace_t . namespace ] , name = name , function = function , recursive = recursive ) )
Returns reference to namespace declaration that matches a defined criteria .
236,105
def namespaces ( self , name = None , function = None , recursive = None , allow_empty = None ) : return ( self . _find_multiple ( scopedef . scopedef_t . _impl_matchers [ namespace_t . namespace ] , name = name , function = function , recursive = recursive , allow_empty = allow_empty ) )
Returns a set of namespace declarations that match a defined criteria .
236,106
def free_function ( self , name = None , function = None , return_type = None , arg_types = None , header_dir = None , header_file = None , recursive = None ) : return ( self . _find_single ( scopedef . scopedef_t . _impl_matchers [ namespace_t . free_function ] , name = name , function = function , decl_type = self . ...
Returns reference to free function declaration that matches a defined criteria .
236,107
def free_functions ( self , name = None , function = None , return_type = None , arg_types = None , header_dir = None , header_file = None , recursive = None , allow_empty = None ) : return ( self . _find_multiple ( scopedef . scopedef_t . _impl_matchers [ namespace_t . free_function ] , name = name , function = functi...
Returns a set of free function declarations that match a defined criteria .
236,108
def free_operator ( self , name = None , function = None , symbol = None , return_type = None , arg_types = None , header_dir = None , header_file = None , recursive = None ) : return ( self . _find_single ( scopedef . scopedef_t . _impl_matchers [ namespace_t . free_operator ] , name = self . _build_operator_name ( na...
Returns reference to free operator declaration that matches a defined criteria .
236,109
def extract ( text , default = UNKNOWN ) : if not text : return default found = CALLING_CONVENTION_TYPES . pattern . match ( text ) if found : return found . group ( 'cc' ) return default
extracts calling convention from the text . If the calling convention could not be found the default is used
236,110
def is_same_function ( f1 , f2 ) : if f1 is f2 : return True if f1 . __class__ is not f2 . __class__ : return False if isinstance ( f1 , calldef_members . member_calldef_t ) and f1 . has_const != f2 . has_const : return False if f1 . name != f2 . name : return False if not is_same_return_type ( f1 , f2 ) : return False...
returns true if f1 and f2 is same function
236,111
def make_flatten ( decl_or_decls ) : def proceed_single ( decl ) : answer = [ decl ] if not isinstance ( decl , scopedef_t ) : return answer for elem in decl . declarations : if isinstance ( elem , scopedef_t ) : answer . extend ( proceed_single ( elem ) ) else : answer . append ( elem ) return answer decls = [ ] if is...
Converts tree representation of declarations to flatten one .
236,112
def find_all_declarations ( declarations , decl_type = None , name = None , parent = None , recursive = True , fullname = None ) : if recursive : decls = make_flatten ( declarations ) else : decls = declarations return list ( filter ( algorithm . match_declaration_t ( decl_type = decl_type , name = name , fullname = fu...
Returns a list of all declarations that match criteria defined by developer .
236,113
def find_declaration ( declarations , decl_type = None , name = None , parent = None , recursive = True , fullname = None ) : decl = find_all_declarations ( declarations , decl_type = decl_type , name = name , parent = parent , recursive = recursive , fullname = fullname ) if len ( decl ) == 1 : return decl [ 0 ]
Returns single declaration that match criteria defined by developer . If more the one declaration was found None will be returned .
236,114
def find_first_declaration ( declarations , decl_type = None , name = None , parent = None , recursive = True , fullname = None ) : decl_matcher = algorithm . match_declaration_t ( decl_type = decl_type , name = name , fullname = fullname , parent = parent ) if recursive : decls = make_flatten ( declarations ) else : d...
Returns first declaration that match criteria defined by developer .
236,115
def declaration_files ( decl_or_decls ) : files = set ( ) decls = make_flatten ( decl_or_decls ) for decl in decls : if decl . location : files . add ( decl . location . file_name ) return files
Returns set of files
236,116
def find ( decl_matcher , decls , recursive = True ) : where = [ ] if isinstance ( decls , list ) : where . extend ( decls ) else : where . append ( decls ) if recursive : where = make_flatten ( where ) return list ( filter ( decl_matcher , where ) )
Returns a list of declarations that match decl_matcher defined criteria or None
236,117
def find_single ( decl_matcher , decls , recursive = True ) : answer = matcher . find ( decl_matcher , decls , recursive ) if len ( answer ) == 1 : return answer [ 0 ]
Returns a reference to the declaration that match decl_matcher defined criteria .
236,118
def get_single ( decl_matcher , decls , recursive = True ) : answer = matcher . find ( decl_matcher , decls , recursive ) if len ( answer ) == 1 : return answer [ 0 ] elif not answer : raise runtime_errors . declaration_not_found_t ( decl_matcher ) else : raise runtime_errors . multiple_declarations_found_t ( decl_matc...
Returns a reference to declaration that match decl_matcher defined criteria .
236,119
def clear_optimizer ( self ) : self . _optimized = False self . _type2decls = { } self . _type2name2decls = { } self . _type2decls_nr = { } self . _type2name2decls_nr = { } self . _all_decls = None self . _all_decls_not_recursive = None for decl in self . declarations : if isinstance ( decl , scopedef_t ) : decl . clea...
Cleans query optimizer state
236,120
def init_optimizer ( self ) : if self . name == '::' : self . _logger . debug ( "preparing data structures for query optimizer - started" ) start_time = timeit . default_timer ( ) self . clear_optimizer ( ) for dtype in scopedef_t . _impl_all_decl_types : self . _type2decls [ dtype ] = [ ] self . _type2decls_nr [ dtype...
Initializes query optimizer state .
236,121
def decls ( self , name = None , function = None , decl_type = None , header_dir = None , header_file = None , recursive = None , allow_empty = None ) : return ( self . _find_multiple ( self . _impl_matchers [ scopedef_t . decl ] , name = name , function = function , decl_type = decl_type , header_dir = header_dir , he...
returns a set of declarations that are matched defined criteria
236,122
def classes ( self , name = None , function = None , header_dir = None , header_file = None , recursive = None , allow_empty = None ) : return ( self . _find_multiple ( self . _impl_matchers [ scopedef_t . class_ ] , name = name , function = function , decl_type = self . _impl_decl_types [ scopedef_t . class_ ] , heade...
returns a set of class declarations that are matched defined criteria
236,123
def variable ( self , name = None , function = None , decl_type = None , header_dir = None , header_file = None , recursive = None ) : return ( self . _find_single ( self . _impl_matchers [ scopedef_t . variable ] , name = name , function = function , decl_type = decl_type , header_dir = header_dir , header_file = head...
returns reference to variable declaration that is matched defined criteria
236,124
def variables ( self , name = None , function = None , decl_type = None , header_dir = None , header_file = None , recursive = None , allow_empty = None ) : return ( self . _find_multiple ( self . _impl_matchers [ scopedef_t . variable ] , name = name , function = function , decl_type = decl_type , header_dir = header_...
returns a set of variable declarations that are matched defined criteria
236,125
def member_functions ( self , name = None , function = None , return_type = None , arg_types = None , header_dir = None , header_file = None , recursive = None , allow_empty = None ) : return ( self . _find_multiple ( self . _impl_matchers [ scopedef_t . member_function ] , name = name , function = function , decl_type...
returns a set of member function declarations that are matched defined criteria
236,126
def constructor ( self , name = None , function = None , return_type = None , arg_types = None , header_dir = None , header_file = None , recursive = None ) : return ( self . _find_single ( self . _impl_matchers [ scopedef_t . constructor ] , name = name , function = function , decl_type = self . _impl_decl_types [ sco...
returns reference to constructor declaration that is matched defined criteria
236,127
def constructors ( self , name = None , function = None , return_type = None , arg_types = None , header_dir = None , header_file = None , recursive = None , allow_empty = None ) : return ( self . _find_multiple ( self . _impl_matchers [ scopedef_t . constructor ] , name = name , function = function , decl_type = self ...
returns a set of constructor declarations that are matched defined criteria
236,128
def casting_operators ( self , name = None , function = None , return_type = None , arg_types = None , header_dir = None , header_file = None , recursive = None , allow_empty = None ) : return ( self . _find_multiple ( self . _impl_matchers [ scopedef_t . casting_operator ] , name = name , function = function , decl_ty...
returns a set of casting operator declarations that are matched defined criteria
236,129
def enumerations ( self , name = None , function = None , header_dir = None , header_file = None , recursive = None , allow_empty = None ) : return ( self . _find_multiple ( self . _impl_matchers [ scopedef_t . enumeration ] , name = name , function = function , decl_type = self . _impl_decl_types [ scopedef_t . enumer...
returns a set of enumeration declarations that are matched defined criteria
236,130
def typedef ( self , name = None , function = None , header_dir = None , header_file = None , recursive = None ) : return ( self . _find_single ( self . _impl_matchers [ scopedef_t . typedef ] , name = name , function = function , decl_type = self . _impl_decl_types [ scopedef_t . typedef ] , header_dir = header_dir , ...
returns reference to typedef declaration that is matched defined criteria
236,131
def typedefs ( self , name = None , function = None , header_dir = None , header_file = None , recursive = None , allow_empty = None ) : return ( self . _find_multiple ( self . _impl_matchers [ scopedef_t . typedef ] , name = name , function = function , decl_type = self . _impl_decl_types [ scopedef_t . typedef ] , he...
returns a set of typedef declarations that are matched defined criteria
236,132
def load_xml_generator_configuration ( configuration , ** defaults ) : parser = configuration if utils . is_str ( configuration ) : parser = ConfigParser ( ) parser . read ( configuration ) cfg = xml_generator_configuration_t ( ) values = defaults if not values : values = { } if parser . has_section ( 'xml_generator' )...
Loads CastXML or GCC - XML configuration .
236,133
def create_compiler_path ( xml_generator , compiler_path ) : if xml_generator == 'castxml' and compiler_path is None : if platform . system ( ) == 'Windows' : p = subprocess . Popen ( [ 'where' , 'cl' ] , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) compiler_path = p . stdout . read ( ) . decode ( "utf-8" ...
Try to guess a path for the compiler .
236,134
def raise_on_wrong_settings ( self ) : self . __ensure_dir_exists ( self . working_directory , 'working directory' ) for idir in self . include_paths : self . __ensure_dir_exists ( idir , 'include directory' ) if self . __xml_generator not in [ "castxml" , "gccxml" ] : msg = ( 'xml_generator("%s") should either be ' + ...
Validates the configuration settings and raises RuntimeError on error
236,135
def declaration_path ( decl ) : if not decl : return [ ] if not decl . cache . declaration_path : result = [ decl . name ] parent = decl . parent while parent : if parent . cache . declaration_path : result . reverse ( ) decl . cache . declaration_path = parent . cache . declaration_path + result return decl . cache . ...
Returns a list of parent declarations names .
236,136
def partial_declaration_path ( decl ) : if not decl : return [ ] if not decl . cache . partial_declaration_path : result = [ decl . partial_name ] parent = decl . parent while parent : if parent . cache . partial_declaration_path : result . reverse ( ) decl . cache . partial_declaration_path = parent . cache . partial_...
Returns a list of parent declarations names without template arguments that have default value .
236,137
def full_name ( decl , with_defaults = True ) : if None is decl : raise RuntimeError ( "Unable to generate full name for None object!" ) if with_defaults : if not decl . cache . full_name : path = declaration_path ( decl ) if path == [ "" ] : decl . cache . full_name = "" else : decl . cache . full_name = full_name_fro...
Returns declaration full qualified name .
236,138
def get_named_parent ( decl ) : if not decl : return None parent = decl . parent while parent and ( not parent . name or parent . name == '::' ) : parent = parent . parent return parent
Returns a reference to a named parent declaration .
236,139
def print_declarations ( decls , detailed = True , recursive = True , writer = lambda x : sys . stdout . write ( x + os . linesep ) , verbose = True ) : prn = decl_printer_t ( 0 , detailed , recursive , writer , verbose = verbose ) if not isinstance ( decls , list ) : decls = [ decls ] for d in decls : prn . level = 0 ...
print declarations tree rooted at each of the included nodes .
236,140
def dump_declarations ( declarations , file_path ) : with open ( file_path , "w+" ) as f : print_declarations ( declarations , writer = f . write )
Dump declarations tree rooted at each of the included nodes to the file
236,141
def __add_symbols ( self , cmd ) : if self . __config . define_symbols : symbols = self . __config . define_symbols cmd . append ( '' . join ( [ ' -D"%s"' % def_symbol for def_symbol in symbols ] ) ) if self . __config . undefine_symbols : un_symbols = self . __config . undefine_symbols cmd . append ( '' . join ( [ ' -...
Add all additional defined and undefined symbols .
236,142
def create_xml_file ( self , source_file , destination = None ) : xml_file = destination if xml_file : utils . remove_file_no_raise ( xml_file , self . __config ) else : xml_file = utils . create_temp_file_name ( suffix = '.xml' ) ffname = source_file if not os . path . isabs ( ffname ) : ffname = self . __file_full_na...
This method will generate a xml file using an external tool .
236,143
def create_xml_file_from_string ( self , content , destination = None ) : header_file = utils . create_temp_file_name ( suffix = '.h' ) try : with open ( header_file , "w+" ) as header : header . write ( content ) xml_file = self . create_xml_file ( header_file , destination ) finally : utils . remove_file_no_raise ( h...
Creates XML file from text .
236,144
def read_cpp_source_file ( self , source_file ) : xml_file = '' try : ffname = self . __file_full_name ( source_file ) self . logger . debug ( "Reading source file: [%s]." , ffname ) decls = self . __dcache . cached_value ( ffname , self . __config ) if not decls : self . logger . debug ( "File has not been found in ca...
Reads C ++ source file and returns declarations tree
236,145
def read_xml_file ( self , xml_file ) : assert self . __config is not None ffname = self . __file_full_name ( xml_file ) self . logger . debug ( "Reading xml file: [%s]" , xml_file ) decls = self . __dcache . cached_value ( ffname , self . __config ) if not decls : self . logger . debug ( "File has not been found in ca...
Read generated XML file .
236,146
def read_string ( self , content ) : header_file = utils . create_temp_file_name ( suffix = '.h' ) with open ( header_file , "w+" ) as f : f . write ( content ) try : decls = self . read_file ( header_file ) except Exception : utils . remove_file_no_raise ( header_file , self . __config ) raise utils . remove_file_no_r...
Reads a Python string that contains C ++ code and return the declarations tree .
236,147
def apply_visitor ( visitor , decl_inst ) : fname = 'visit_' + decl_inst . __class__ . __name__ [ : - 2 ] if not hasattr ( visitor , fname ) : raise runtime_errors . visit_function_has_not_been_found_t ( visitor , decl_inst ) return getattr ( visitor , fname ) ( )
Applies a visitor on declaration instance .
236,148
def does_match_exist ( self , inst ) : answer = True if self . _decl_type is not None : answer &= isinstance ( inst , self . _decl_type ) if self . name is not None : answer &= inst . name == self . name if self . parent is not None : answer &= self . parent is inst . parent if self . fullname is not None : if inst . n...
Returns True if inst does match one of specified criteria .
236,149
def partial_name ( self ) : if None is self . _partial_name : self . _partial_name = self . _get_partial_name_impl ( ) return self . _partial_name
Declaration name without template default arguments .
236,150
def top_parent ( self ) : parent = self . parent while parent is not None : if parent . parent is None : return parent else : parent = parent . parent return self
Reference to top parent declaration .
236,151
def clone ( self , ** keywd ) : return argument_t ( name = keywd . get ( 'name' , self . name ) , decl_type = keywd . get ( 'decl_type' , self . decl_type ) , default_value = keywd . get ( 'default_value' , self . default_value ) , attributes = keywd . get ( 'attributes' , self . attributes ) )
constructs new argument_t instance
236,152
def required_args ( self ) : r_args = [ ] for arg in self . arguments : if not arg . default_value : r_args . append ( arg ) else : break return r_args
list of all required arguments
236,153
def overloads ( self ) : if not self . parent : return [ ] return self . parent . calldefs ( name = self . name , function = lambda decl : decl is not self , allow_empty = True , recursive = False )
A list of overloaded callables ( i . e . other callables with the same name within the same scope .
236,154
def file_signature ( filename ) : if not os . path . isfile ( filename ) : return None if not os . path . exists ( filename ) : return None sig = hashlib . sha1 ( ) with open ( filename , "rb" ) as f : buf = f . read ( ) sig . update ( buf ) return sig . hexdigest ( )
Return a signature for a file .
236,155
def __load ( file_name ) : if os . path . exists ( file_name ) and not os . path . isfile ( file_name ) : raise RuntimeError ( 'Cache should be initialized with valid full file name' ) if not os . path . exists ( file_name ) : open ( file_name , 'w+b' ) . close ( ) return { } cache_file_obj = open ( file_name , 'rb' ) ...
Load pickled cache from file and return the object .
236,156
def update ( self , source_file , configuration , declarations , included_files ) : record = record_t ( source_signature = file_signature ( source_file ) , config_signature = configuration_signature ( configuration ) , included_files = included_files , included_files_signature = list ( map ( file_signature , included_f...
Update a cached record with the current key and value contents .
236,157
def cached_value ( self , source_file , configuration ) : key = record_t . create_key ( source_file , configuration ) if key not in self . __cache : return None record = self . __cache [ key ] if self . __is_valid_signature ( record ) : record . was_hit = True return record . declarations del self . __cache [ key ] ret...
Attempt to lookup the cached declarations for the given file and configuration .
236,158
def find_version ( file_path ) : with io . open ( os . path . join ( os . path . dirname ( __file__ ) , os . path . normpath ( file_path ) ) , encoding = "utf8" ) as fp : content = fp . read ( ) version_match = re . search ( r"^__version__ = ['\"]([^'\"]*)['\"]" , content , re . M ) if version_match : return version_ma...
Find the version of pygccxml .
236,159
def __read_byte_size ( decl , attrs ) : size = attrs . get ( XML_AN_SIZE , 0 ) decl . byte_size = int ( size ) / 8
Using duck typing to set the size instead of in constructor
236,160
def __read_byte_offset ( decl , attrs ) : offset = attrs . get ( XML_AN_OFFSET , 0 ) decl . byte_offset = int ( offset ) / 8
Using duck typing to set the offset instead of in constructor
236,161
def __read_byte_align ( decl , attrs ) : align = attrs . get ( XML_AN_ALIGN , 0 ) decl . byte_align = int ( align ) / 8
Using duck typing to set the alignment
236,162
def bind_aliases ( decls ) : visited = set ( ) typedefs = [ decl for decl in decls if isinstance ( decl , declarations . typedef_t ) ] for decl in typedefs : type_ = declarations . remove_alias ( decl . decl_type ) if not isinstance ( type_ , declarations . declarated_t ) : continue cls_inst = type_ . declaration if no...
This function binds between class and it s typedefs .
236,163
def after_scenario ( ctx , scenario ) : if hasattr ( ctx , 'container' ) and hasattr ( ctx , 'client' ) : try : ctx . client . remove_container ( ctx . container , force = True ) except : pass
Cleans up docker containers used as test fixtures after test completes .
236,164
def set_blocking ( fd , blocking = True ) : old_flag = fcntl . fcntl ( fd , fcntl . F_GETFL ) if blocking : new_flag = old_flag & ~ os . O_NONBLOCK else : new_flag = old_flag | os . O_NONBLOCK fcntl . fcntl ( fd , fcntl . F_SETFL , new_flag ) return not bool ( old_flag & os . O_NONBLOCK )
Set the given file - descriptor blocking or non - blocking .
236,165
def select ( read_streams , write_streams , timeout = 0 ) : exception_streams = [ ] try : return builtin_select . select ( read_streams , write_streams , exception_streams , timeout , ) [ 0 : 2 ] except builtin_select . error as e : no = e . errno if six . PY3 else e [ 0 ] if no == errno . EINTR : return ( [ ] , [ ] ) ...
Select the streams from read_streams that are ready for reading and streams from write_streams ready for writing .
236,166
def read ( self , n = 4096 ) : while True : try : if hasattr ( self . fd , 'recv' ) : return self . fd . recv ( n ) return os . read ( self . fd . fileno ( ) , n ) except EnvironmentError as e : if e . errno not in Stream . ERRNO_RECOVERABLE : raise e
Return n bytes of data from the Stream or None at end of stream .
236,167
def do_write ( self ) : while True : try : written = 0 if hasattr ( self . fd , 'send' ) : written = self . fd . send ( self . buffer ) else : written = os . write ( self . fd . fileno ( ) , self . buffer ) self . buffer = self . buffer [ written : ] if self . close_requested and len ( self . buffer ) == 0 : self . clo...
Flushes as much pending data from the internal write buffer as possible .
236,168
def read ( self , n = 4096 ) : size = self . _next_packet_size ( n ) if size <= 0 : return else : data = six . binary_type ( ) while len ( data ) < size : nxt = self . stream . read ( size - len ( data ) ) if not nxt : return data data = data + nxt return data
Read up to n bytes of data from the Stream after demuxing .
236,169
def flush ( self , n = 4096 ) : try : read = self . from_stream . read ( n ) if read is None or len ( read ) == 0 : self . eof = True if self . propagate_close : self . to_stream . close ( ) return None return self . to_stream . write ( read ) except OSError as e : if e . errno != errno . EPIPE : raise e
Flush n bytes of data from the reader Stream to the writer Stream .
236,170
def exec_command ( client , container , command , interactive = True , stdout = None , stderr = None , stdin = None ) : exec_id = exec_create ( client , container , command , interactive = interactive ) operation = ExecOperation ( client , exec_id , interactive = interactive , stdout = stdout , stderr = stderr , stdin ...
Run provided command via exec API in provided container .
236,171
def start ( self ) : if os . isatty ( self . fd . fileno ( ) ) and self . israw ( ) : self . original_attributes = termios . tcgetattr ( self . fd ) tty . setraw ( self . fd )
Saves the current terminal attributes and makes the tty raw .
236,172
def stop ( self ) : if self . original_attributes is not None : termios . tcsetattr ( self . fd , termios . TCSADRAIN , self . original_attributes , )
Restores the terminal attributes back to before setting raw mode .
236,173
def start ( self ) : def handle ( signum , frame ) : if signum == signal . SIGWINCH : self . pty . resize ( ) self . original_handler = signal . signal ( signal . SIGWINCH , handle )
Start trapping WINCH signals and resizing the PTY .
236,174
def stop ( self ) : if self . original_handler is not None : signal . signal ( signal . SIGWINCH , self . original_handler )
Stop trapping WINCH signals and restore the previous WINCH handler .
236,175
def resize ( self , height , width , ** kwargs ) : self . client . resize ( self . container , height = height , width = width )
resize pty within container
236,176
def resize ( self , height , width , ** kwargs ) : self . client . exec_resize ( self . exec_id , height = height , width = width )
resize pty of an execed process
236,177
def _exec_info ( self ) : if self . _info is None : self . _info = self . client . exec_inspect ( self . exec_id ) return self . _info
Caching wrapper around client . exec_inspect
236,178
def format_number ( x ) : if isinstance ( x , float ) : value = '{:.17g}' . format ( x ) else : value = str ( x ) return value
Format number to string
236,179
def parse_number_auto_dtype ( x ) : value = float ( x ) if value . is_integer ( ) : value = int ( value ) return value
Parse number from string with automatic type detection .
236,180
def _determine_datatype ( fields ) : np_typestring = _TYPEMAP_NRRD2NUMPY [ fields [ 'type' ] ] if np . dtype ( np_typestring ) . itemsize > 1 and fields [ 'encoding' ] not in [ 'ASCII' , 'ascii' , 'text' , 'txt' ] : if 'endian' not in fields : raise NRRDError ( 'Header is missing required field: "endian".' ) elif field...
Determine the numpy dtype of the data .
236,181
def _validate_magic_line ( line ) : if not line . startswith ( 'NRRD' ) : raise NRRDError ( 'Invalid NRRD magic line. Is this an NRRD file?' ) try : version = int ( line [ 4 : ] ) if version > 5 : raise NRRDError ( 'Unsupported NRRD file version (version: %i). This library only supports v%i and below.' % ( version , 5 ...
For NRRD files the first four characters are always NRRD and remaining characters give information about the file format version
236,182
def read ( filename , custom_field_map = None , index_order = 'F' ) : with open ( filename , 'rb' ) as fh : header = read_header ( fh , custom_field_map ) data = read_data ( header , fh , filename , index_order ) return data , header
Read a NRRD file and return the header and data
236,183
def add_notes ( self , note , duration = None ) : if self . instrument != None : if not self . instrument . can_play_notes ( note ) : raise InstrumentRangeError , "Note '%s' is not in range of the instrument (%s)" % ( note , self . instrument ) if duration == None : duration = 4 if len ( self . bars ) == 0 : self . bar...
Add a Note note as string or NoteContainer to the last Bar .
236,184
def get_notes ( self ) : for bar in self . bars : for beat , duration , notes in bar : yield beat , duration , notes
Return an iterator that iterates through every bar in the this track .
236,185
def from_chords ( self , chords , duration = 1 ) : tun = self . get_tuning ( ) def add_chord ( chord , duration ) : if type ( chord ) == list : for c in chord : add_chord ( c , duration * 2 ) else : chord = NoteContainer ( ) . from_chord ( chord ) if tun : chord = tun . find_chord_fingering ( chord , return_best_as_Not...
Add chords to the Track .
236,186
def get_tuning ( self ) : if self . instrument and self . instrument . tuning : return self . instrument . tuning return self . tuning
Return a StringTuning object .
236,187
def transpose ( self , interval , up = True ) : for bar in self . bars : bar . transpose ( interval , up ) return self
Transpose all the notes in the track up or down the interval .
236,188
def set_meter ( self , meter ) : if _meter . valid_beat_duration ( meter [ 1 ] ) : self . meter = ( meter [ 0 ] , meter [ 1 ] ) self . length = meter [ 0 ] * ( 1.0 / meter [ 1 ] ) elif meter == ( 0 , 0 ) : self . meter = ( 0 , 0 ) self . length = 0.0 else : raise MeterFormatError ( "The meter argument '%s' is not an " ...
Set the meter of this bar .
236,189
def place_notes ( self , notes , duration ) : if hasattr ( notes , 'notes' ) : pass elif hasattr ( notes , 'name' ) : notes = NoteContainer ( notes ) elif type ( notes ) == str : notes = NoteContainer ( notes ) elif type ( notes ) == list : notes = NoteContainer ( notes ) if self . current_beat + 1.0 / duration <= self...
Place the notes on the current_beat .
236,190
def place_notes_at ( self , notes , at ) : for x in self . bar : if x [ 0 ] == at : x [ 0 ] [ 2 ] += notes
Place notes at the given index .
236,191
def remove_last_entry ( self ) : self . current_beat -= 1.0 / self . bar [ - 1 ] [ 1 ] self . bar = self . bar [ : - 1 ] return self . current_beat
Remove the last NoteContainer in the Bar .
236,192
def is_full ( self ) : if self . length == 0.0 : return False if len ( self . bar ) == 0 : return False if self . current_beat >= self . length - 0.001 : return True return False
Return False if there is room in this Bar for another NoteContainer True otherwise .
236,193
def change_note_duration ( self , at , to ) : if valid_beat_duration ( to ) : diff = 0 for x in self . bar : if diff != 0 : x [ 0 ] [ 0 ] -= diff if x [ 0 ] == at : cur = x [ 0 ] [ 1 ] x [ 0 ] [ 1 ] = to diff = 1 / cur - 1 / to
Change the note duration at the given index to the given duration .
236,194
def get_range ( self ) : ( min , max ) = ( 100000 , - 1 ) for cont in self . bar : for note in cont [ 2 ] : if int ( note ) < int ( min ) : min = note elif int ( note ) > int ( max ) : max = note return ( min , max )
Return the highest and the lowest note in a tuple .
236,195
def transpose ( self , interval , up = True ) : for cont in self . bar : cont [ 2 ] . transpose ( interval , up )
Transpose the notes in the bar up or down the interval .
236,196
def get_note_names ( self ) : res = [ ] for cont in self . bar : for x in cont [ 2 ] . get_note_names ( ) : if x not in res : res . append ( x ) return res
Return a list of unique note names in the Bar .
236,197
def parse_midi_file_header ( self , fp ) : try : if fp . read ( 4 ) != 'MThd' : raise HeaderError ( 'Not a valid MIDI file header. Byte %d.' % self . bytes_read ) self . bytes_read += 4 except : raise IOError ( "Couldn't read from file." ) try : chunk_size = self . bytes_to_int ( fp . read ( 4 ) ) self . bytes_read += ...
Read the header of a MIDI file and return a tuple containing the format type number of tracks and parsed time division information .
236,198
def parse_time_division ( self , bytes ) : value = self . bytes_to_int ( bytes ) if not value & 0x8000 : return { 'fps' : False , 'ticks_per_beat' : value & 0x7FFF } else : SMPTE_frames = ( value & 0x7F00 ) >> 2 if SMPTE_frames not in [ 24 , 25 , 29 , 30 ] : raise TimeDivisionError , "'%d' is not a valid value for the ...
Parse the time division found in the header of a MIDI file and return a dictionary with the boolean fps set to indicate whether to use frames per second or ticks per beat .
236,199
def parse_track ( self , fp ) : events = [ ] chunk_size = self . parse_track_header ( fp ) bytes = chunk_size while chunk_size > 0 : ( delta_time , chunk_delta ) = self . parse_varbyte_as_int ( fp ) chunk_size -= chunk_delta ( event , chunk_delta ) = self . parse_midi_event ( fp ) chunk_size -= chunk_delta events . app...
Parse a MIDI track from its header to its events .