idx int64 0 251k | question stringlengths 53 3.53k | target stringlengths 5 1.23k | len_question int64 20 893 | len_target int64 3 238 |
|---|---|---|---|---|
19,000 | def choose_plural ( amount , variants ) : try : if isinstance ( variants , six . string_types ) : uvariants = smart_text ( variants , encoding ) else : uvariants = [ smart_text ( v , encoding ) for v in variants ] res = numeral . choose_plural ( amount , uvariants ) except Exception as err : # because filter must die silently try : default_variant = variants except Exception : default_variant = "" res = default_value % { 'error' : err , 'value' : default_variant } return res | Choose proper form for plural . | 126 | 6 |
19,001 | def in_words ( amount , gender = None ) : try : res = numeral . in_words ( amount , getattr ( numeral , str ( gender ) , None ) ) except Exception as err : # because filter must die silently res = default_value % { 'error' : err , 'value' : str ( amount ) } return res | In - words representation of amount . | 74 | 7 |
19,002 | def sum_string ( amount , gender , items ) : try : if isinstance ( items , six . string_types ) : uitems = smart_text ( items , encoding , default_uvalue ) else : uitems = [ smart_text ( i , encoding ) for i in items ] res = numeral . sum_string ( amount , getattr ( numeral , str ( gender ) , None ) , uitems ) except Exception as err : # because tag's renderer must die silently res = default_value % { 'error' : err , 'value' : str ( amount ) } return res | in_words and choose_plural in a one flask Makes in - words representation of value with choosing correct form of noun . | 128 | 26 |
19,003 | def rl_cleanspaces ( x ) : patterns = ( # arguments for re.sub: pattern and repl # удаляем пробел перед знаками препинания ( r' +([\.,?!\)]+)' , r'\1' ) , # добавляем пробел после знака препинания, если только за ним нет другого ( r'([\.,?!\)]+)([^\.!,?\)]+)' , r'\1 \2' ) , # убираем пробел после открывающей скобки ( r'(\S+)\s*(\()\s*(\S+)' , r'\1 (\3' ) , ) # удаляем двойные, начальные и конечные пробелы return os . linesep . join ( ' ' . join ( part for part in line . split ( ' ' ) if part ) for line in _sub_patterns ( patterns , x ) . split ( os . linesep ) ) | Clean double spaces trailing spaces heading spaces spaces before punctuations | 364 | 11 |
19,004 | def rl_quotes ( x ) : patterns = ( # открывающие кавычки ставятся обычно вплотную к слову слева # а закрывающие -- вплотную справа # открывающие русские кавычки-ёлочки ( re . compile ( r'((?:^|\s))(")((?u))' , re . UNICODE ) , u'\\1\xab\\3' ) , # закрывающие русские кавычки-ёлочки ( re . compile ( r'(\S)(")((?u))' , re . UNICODE ) , u'\\1\xbb\\3' ) , # открывающие кавычки-лапки, вместо одинарных кавычек ( re . compile ( r'((?:^|\s))(\')((?u))' , re . UNICODE ) , u'\\1\u201c\\3' ) , # закрывающие кавычки-лапки ( re . compile ( r'(\S)(\')((?u))' , re . UNICODE ) , u'\\1\u201d\\3' ) , ) return _sub_patterns ( patterns , x ) | Replace quotes by typographic quotes | 459 | 7 |
19,005 | def distance_of_time ( from_time , accuracy = 1 ) : try : to_time = None if conf . settings . USE_TZ : to_time = utils . timezone . now ( ) res = dt . distance_of_time_in_words ( from_time , accuracy , to_time ) except Exception as err : # because filter must die silently try : default_distance = "%s seconds" % str ( int ( time . time ( ) - from_time ) ) except Exception : default_distance = "" res = default_value % { 'error' : err , 'value' : default_distance } return res | Display distance of time from current time . | 139 | 8 |
19,006 | def ru_strftime ( date , format = "%d.%m.%Y" , inflected_day = False , preposition = False ) : try : res = dt . ru_strftime ( format , date , inflected = True , inflected_day = inflected_day , preposition = preposition ) except Exception as err : # because filter must die silently try : default_date = date . strftime ( format ) except Exception : default_date = str ( date ) res = default_value % { 'error' : err , 'value' : default_date } return res | Russian strftime formats date with given format . | 128 | 9 |
19,007 | def ru_strftime ( format = u"%d.%m.%Y" , date = None , inflected = False , inflected_day = False , preposition = False ) : if date is None : date = datetime . datetime . today ( ) weekday = date . weekday ( ) prepos = preposition and DAY_NAMES [ weekday ] [ 3 ] or u"" month_idx = inflected and 2 or 1 day_idx = ( inflected_day or preposition ) and 2 or 1 # for russian typography standard, # 1 April 2007, but 01.04.2007 if u'%b' in format or u'%B' in format : format = format . replace ( u'%d' , six . text_type ( date . day ) ) format = format . replace ( u'%a' , prepos + DAY_NAMES [ weekday ] [ 0 ] ) format = format . replace ( u'%A' , prepos + DAY_NAMES [ weekday ] [ day_idx ] ) format = format . replace ( u'%b' , MONTH_NAMES [ date . month - 1 ] [ 0 ] ) format = format . replace ( u'%B' , MONTH_NAMES [ date . month - 1 ] [ month_idx ] ) # Python 2: strftime's argument must be str # Python 3: strftime's argument str, not a bitestring if six . PY2 : # strftime must be str, so encode it to utf8: s_format = format . encode ( "utf-8" ) s_res = date . strftime ( s_format ) # and back to unicode u_res = s_res . decode ( "utf-8" ) else : u_res = date . strftime ( format ) return u_res | Russian strftime without locale | 400 | 5 |
19,008 | def _get_float_remainder ( fvalue , signs = 9 ) : check_positive ( fvalue ) if isinstance ( fvalue , six . integer_types ) : return "0" if isinstance ( fvalue , Decimal ) and fvalue . as_tuple ( ) [ 2 ] == 0 : # Decimal.as_tuple() -> (sign, digit_tuple, exponent) # если экспонента "0" -- значит дробной части нет return "0" signs = min ( signs , len ( FRACTIONS ) ) # нужно remainder в строке, потому что дробные X.0Y # будут "ломаться" до X.Y remainder = str ( fvalue ) . split ( '.' ) [ 1 ] iremainder = int ( remainder ) orig_remainder = remainder factor = len ( str ( remainder ) ) - signs if factor > 0 : # после запятой цифр больше чем signs, округляем iremainder = int ( round ( iremainder / ( 10.0 ** factor ) ) ) format = "%%0%dd" % min ( len ( remainder ) , signs ) remainder = format % iremainder if len ( remainder ) > signs : # при округлении цифр вида 0.998 ругаться raise ValueError ( "Signs overflow: I can't round only fractional part \ of %s to fit %s in %d signs" % ( str ( fvalue ) , orig_remainder , signs ) ) return remainder | Get remainder of float i . e . 2 . 05 - > 05 | 463 | 14 |
19,009 | def choose_plural ( amount , variants ) : if isinstance ( variants , six . text_type ) : variants = split_values ( variants ) check_length ( variants , 3 ) amount = abs ( amount ) if amount % 10 == 1 and amount % 100 != 11 : variant = 0 elif amount % 10 >= 2 and amount % 10 <= 4 and ( amount % 100 < 10 or amount % 100 >= 20 ) : variant = 1 else : variant = 2 return variants [ variant ] | Choose proper case depending on amount | 103 | 6 |
19,010 | def get_plural ( amount , variants , absence = None ) : if amount or absence is None : return u"%d %s" % ( amount , choose_plural ( amount , variants ) ) else : return absence | Get proper case with value | 48 | 5 |
19,011 | def in_words ( amount , gender = None ) : check_positive ( amount ) if isinstance ( amount , Decimal ) and amount . as_tuple ( ) [ 2 ] == 0 : # если целое, # т.е. Decimal.as_tuple -> (sign, digits tuple, exponent), exponent=0 # то как целое amount = int ( amount ) if gender is None : args = ( amount , ) else : args = ( amount , gender ) # если целое if isinstance ( amount , six . integer_types ) : return in_words_int ( * args ) # если дробное elif isinstance ( amount , ( float , Decimal ) ) : return in_words_float ( * args ) # ни float, ни int, ни Decimal else : # до сюда не должно дойти raise TypeError ( "amount should be number type (int, long, float, Decimal), got %s" % type ( amount ) ) | Numeral in words | 272 | 4 |
19,012 | def _sum_string_fn ( into , tmp_val , gender , items = None ) : if items is None : items = ( u"" , u"" , u"" ) one_item , two_items , five_items = items check_positive ( tmp_val ) if tmp_val == 0 : return into , tmp_val words = [ ] rest = tmp_val % 1000 tmp_val = tmp_val // 1000 if rest == 0 : # последние три знака нулевые if into == u"" : into = u"%s " % five_items return into , tmp_val # начинаем подсчет с rest end_word = five_items # сотни words . append ( HUNDREDS [ rest // 100 ] ) # десятки rest = rest % 100 rest1 = rest // 10 # особый случай -- tens=1 tens = rest1 == 1 and TENS [ rest ] or TENS [ rest1 ] words . append ( tens ) # единицы if rest1 < 1 or rest1 > 1 : amount = rest % 10 end_word = choose_plural ( amount , items ) words . append ( ONES [ amount ] [ gender - 1 ] ) words . append ( end_word ) # добавляем то, что уже было words . append ( into ) # убираем пустые подстроки words = filter ( lambda x : len ( x ) > 0 , words ) # склеиваем и отдаем return u" " . join ( words ) . strip ( ) , tmp_val | Make in - words representation of single order | 445 | 8 |
19,013 | def check_length ( value , length ) : _length = len ( value ) if _length != length : raise ValueError ( "length must be %d, not %d" % ( length , _length ) ) | Checks length of value | 46 | 5 |
19,014 | def check_positive ( value , strict = False ) : if not strict and value < 0 : raise ValueError ( "Value must be positive or zero, not %s" % str ( value ) ) if strict and value <= 0 : raise ValueError ( "Value must be positive, not %s" % str ( value ) ) | Checks if variable is positive | 69 | 6 |
19,015 | def detranslify ( text ) : try : res = translit . detranslify ( text ) except Exception as err : # because filter must die silently res = default_value % { 'error' : err , 'value' : text } return res | Detranslify russian text | 55 | 7 |
19,016 | def apply ( diff , recs , strict = True ) : index_columns = diff [ '_index' ] indexed = records . index ( copy . deepcopy ( list ( recs ) ) , index_columns ) _add_records ( indexed , diff [ 'added' ] , index_columns , strict = strict ) _remove_records ( indexed , diff [ 'removed' ] , index_columns , strict = strict ) _update_records ( indexed , diff [ 'changed' ] , strict = strict ) return records . sort ( indexed . values ( ) ) | Transform the records with the patch . May fail if the records do not match those expected in the patch . | 127 | 21 |
19,017 | def load ( istream , strict = True ) : try : diff = json . load ( istream ) if strict : jsonschema . validate ( diff , SCHEMA ) except ValueError : raise InvalidPatchError ( 'patch is not valid JSON' ) except jsonschema . exceptions . ValidationError as e : raise InvalidPatchError ( e . message ) return diff | Deserialize a patch object . | 81 | 7 |
19,018 | def save ( diff , stream = sys . stdout , compact = False ) : flags = { 'sort_keys' : True } if not compact : flags [ 'indent' ] = 2 json . dump ( diff , stream , * * flags ) | Serialize a patch object . | 53 | 6 |
19,019 | def create ( from_records , to_records , index_columns , ignore_columns = None ) : from_indexed = records . index ( from_records , index_columns ) to_indexed = records . index ( to_records , index_columns ) if ignore_columns is not None : from_indexed = records . filter_ignored ( from_indexed , ignore_columns ) to_indexed = records . filter_ignored ( to_indexed , ignore_columns ) return create_indexed ( from_indexed , to_indexed , index_columns ) | Diff two sets of records using the index columns as the primary key for both datasets . | 138 | 17 |
19,020 | def _compare_rows ( from_recs , to_recs , keys ) : return set ( k for k in keys if sorted ( from_recs [ k ] . items ( ) ) != sorted ( to_recs [ k ] . items ( ) ) ) | Return the set of keys which have changed . | 59 | 9 |
19,021 | def record_diff ( lhs , rhs ) : delta = { } for k in set ( lhs ) . union ( rhs ) : from_ = lhs [ k ] to_ = rhs [ k ] if from_ != to_ : delta [ k ] = { 'from' : from_ , 'to' : to_ } return delta | Diff an individual row . | 76 | 5 |
19,022 | def filter_significance ( diff , significance ) : changed = diff [ 'changed' ] # remove individual field changes that are significant reduced = [ { 'key' : delta [ 'key' ] , 'fields' : { k : v for k , v in delta [ 'fields' ] . items ( ) if _is_significant ( v , significance ) } } for delta in changed ] # call a key changed only if it still has significant changes filtered = [ delta for delta in reduced if delta [ 'fields' ] ] diff = diff . copy ( ) diff [ 'changed' ] = filtered return diff | Prune any changes in the patch which are due to numeric changes less than this level of significance . | 128 | 20 |
19,023 | def _is_significant ( change , significance ) : try : a = float ( change [ 'from' ] ) b = float ( change [ 'to' ] ) except ValueError : return True return abs ( a - b ) > 10 ** ( - significance ) | Return True if a change is genuinely significant given our tolerance . | 55 | 12 |
19,024 | def diff_files ( from_file , to_file , index_columns , sep = ',' , ignored_columns = None ) : with open ( from_file ) as from_stream : with open ( to_file ) as to_stream : from_records = records . load ( from_stream , sep = sep ) to_records = records . load ( to_stream , sep = sep ) return patch . create ( from_records , to_records , index_columns , ignore_columns = ignored_columns ) | Diff two CSV files returning the patch which transforms one into the other . | 119 | 14 |
19,025 | def patch_file ( patch_stream : TextIO , fromcsv_stream : TextIO , tocsv_stream : TextIO , strict : bool = True , sep : str = ',' ) : diff = patch . load ( patch_stream ) from_records = records . load ( fromcsv_stream , sep = sep ) to_records = patch . apply ( diff , from_records , strict = strict ) # what order should the columns be in? if to_records : # have data, use a nice ordering all_columns = to_records [ 0 ] . keys ( ) index_columns = diff [ '_index' ] fieldnames = _nice_fieldnames ( all_columns , index_columns ) else : # no data, use the original order fieldnames = from_records . fieldnames records . save ( to_records , fieldnames , tocsv_stream ) | Apply the patch to the source CSV file and save the result to the target file . | 197 | 17 |
19,026 | def patch_records ( diff , from_records , strict = True ) : return patch . apply ( diff , from_records , strict = strict ) | Apply the patch to the sequence of records returning the transformed records . | 34 | 13 |
19,027 | def _nice_fieldnames ( all_columns , index_columns ) : non_index_columns = set ( all_columns ) . difference ( index_columns ) return index_columns + sorted ( non_index_columns ) | Indexes on the left other fields in alphabetical order on the right . | 55 | 15 |
19,028 | def csvdiff_cmd ( index_columns , from_csv , to_csv , style = None , output = None , sep = ',' , quiet = False , ignore_columns = None , significance = None ) : if ignore_columns is not None : for i in ignore_columns : if i in index_columns : error . abort ( "You can't ignore an index column" ) ostream = ( open ( output , 'w' ) if output else io . StringIO ( ) if quiet else sys . stdout ) try : if style == 'summary' : _diff_and_summarize ( from_csv , to_csv , index_columns , ostream , sep = sep , ignored_columns = ignore_columns , significance = significance ) else : compact = ( style == 'compact' ) _diff_files_to_stream ( from_csv , to_csv , index_columns , ostream , compact = compact , sep = sep , ignored_columns = ignore_columns , significance = significance ) except records . InvalidKeyError as e : error . abort ( e . args [ 0 ] ) finally : ostream . close ( ) | Compare two csv files to see what rows differ between them . The files are each expected to have a header row and for each row to be uniquely identified by one or more indexing columns . | 256 | 39 |
19,029 | def _diff_and_summarize ( from_csv , to_csv , index_columns , stream = sys . stdout , sep = ',' , ignored_columns = None , significance = None ) : from_records = list ( records . load ( from_csv , sep = sep ) ) to_records = records . load ( to_csv , sep = sep ) diff = patch . create ( from_records , to_records , index_columns , ignored_columns ) if significance is not None : diff = patch . filter_significance ( diff , significance ) _summarize_diff ( diff , len ( from_records ) , stream = stream ) exit_code = ( EXIT_SAME if patch . is_empty ( diff ) else EXIT_DIFFERENT ) sys . exit ( exit_code ) | Print a summary of the difference between the two files . | 187 | 11 |
19,030 | def csvpatch_cmd ( input_csv , input = None , output = None , strict = True ) : patch_stream = ( sys . stdin if input is None else open ( input ) ) tocsv_stream = ( sys . stdout if output is None else open ( output , 'w' ) ) fromcsv_stream = open ( input_csv ) try : patch_file ( patch_stream , fromcsv_stream , tocsv_stream , strict = strict ) except patch . InvalidPatchError as e : error . abort ( 'reading patch, {0}' . format ( e . args [ 0 ] ) ) finally : patch_stream . close ( ) fromcsv_stream . close ( ) tocsv_stream . close ( ) | Apply the changes from a csvdiff patch to an existing CSV file . | 159 | 15 |
19,031 | def sort ( records : Sequence [ Record ] ) -> List [ Record ] : return sorted ( records , key = _record_key ) | Sort records into a canonical order suitable for comparison . | 28 | 10 |
19,032 | def _record_key ( record : Record ) -> List [ Tuple [ Column , str ] ] : return sorted ( record . items ( ) ) | An orderable representation of this record . | 31 | 8 |
19,033 | def getargspecs ( func ) : if func is None : raise TypeError ( 'None is not a Python function' ) if hasattr ( func , 'ch_func' ) : return getargspecs ( func . ch_func ) elif hasattr ( func , 'ov_func' ) : return getargspecs ( func . ov_func ) if hasattr ( inspect , 'getfullargspec' ) : return inspect . getfullargspec ( func ) # Python 3 else : return inspect . getargspec ( func ) | Bridges inspect . getargspec and inspect . getfullargspec . Automatically selects the proper one depending of current Python version . Automatically bypasses wrappers from typechecked - and override - decorators . | 116 | 43 |
19,034 | def get_required_kwonly_args ( argspecs ) : try : kwonly = argspecs . kwonlyargs if argspecs . kwonlydefaults is None : return kwonly res = [ ] for name in kwonly : if not name in argspecs . kwonlydefaults : res . append ( name ) return res except AttributeError : return [ ] | Determines whether given argspecs implies required keywords - only args and returns them as a list . Returns empty list if no such args exist . | 87 | 30 |
19,035 | def getargnames ( argspecs , with_unbox = False ) : # todo: We can maybe make use of inspect.formatargspec args = argspecs . args vargs = argspecs . varargs try : kw = argspecs . keywords except AttributeError : kw = argspecs . varkw try : kwonly = argspecs . kwonlyargs except AttributeError : kwonly = None res = [ ] if not args is None : res . extend ( args ) if not vargs is None : res . append ( '*' + vargs if with_unbox else vargs ) if not kwonly is None : res . extend ( kwonly ) if not kw is None : res . append ( '**' + kw if with_unbox else kw ) return res | Resembles list of arg - names as would be seen in a function signature including var - args var - keywords and keyword - only args . | 183 | 29 |
19,036 | def get_class_that_defined_method ( meth ) : if is_classmethod ( meth ) : return meth . __self__ if hasattr ( meth , 'im_class' ) : return meth . im_class elif hasattr ( meth , '__qualname__' ) : # Python 3 try : cls_names = meth . __qualname__ . split ( '.<locals>' , 1 ) [ 0 ] . rsplit ( '.' , 1 ) [ 0 ] . split ( '.' ) cls = inspect . getmodule ( meth ) for cls_name in cls_names : cls = getattr ( cls , cls_name ) if isinstance ( cls , type ) : return cls except AttributeError : # If this was called from a decorator and meth is not a method, this # can result in AttributeError, because at decorator-time meth has not # yet been added to module. If it's really a method, its class would be # already in, so no problem in that case. pass raise ValueError ( str ( meth ) + ' is not a method.' ) | Determines the class owning the given method . | 245 | 10 |
19,037 | def is_classmethod ( meth ) : if inspect . ismethoddescriptor ( meth ) : return isinstance ( meth , classmethod ) if not inspect . ismethod ( meth ) : return False if not inspect . isclass ( meth . __self__ ) : return False if not hasattr ( meth . __self__ , meth . __name__ ) : return False return meth == getattr ( meth . __self__ , meth . __name__ ) | Detects if the given callable is a classmethod . | 96 | 12 |
19,038 | def get_current_args ( caller_level = 0 , func = None , argNames = None ) : if argNames is None : argNames = getargnames ( getargspecs ( func ) ) if func is None : func = get_current_function ( caller_level + 1 ) if isinstance ( func , property ) : func = func . fget if func . fset is None else func . fset stck = inspect . stack ( ) lcs = stck [ 1 + caller_level ] [ 0 ] . f_locals return tuple ( [ lcs [ t ] for t in argNames ] ) | Determines the args of current function call . Use caller_level > 0 to get args of even earlier function calls in current stack . | 134 | 28 |
19,039 | def getmodule ( code ) : try : md = inspect . getmodule ( code , code . co_filename ) except AttributeError : return inspect . getmodule ( code ) if md is None : # Jython-specific: # This is currently just a crutch; todo: resolve __pyclasspath__ properly! cfname = code . co_filename . replace ( '__pyclasspath__' , os . path . realpath ( '' ) + os . sep + '__pyclasspath__' ) cfname = cfname . replace ( '$py.class' , '.py' ) md = inspect . getmodule ( code , cfname ) if md is None : md = inspect . getmodule ( code ) return md | More robust variant of inspect . getmodule . E . g . has less issues on Jython . | 156 | 20 |
19,040 | def _calc_traceback_limit ( tb ) : limit = 1 tb2 = tb while not tb2 . tb_next is None : try : maybe_pytypes = tb2 . tb_next . tb_frame . f_code . co_filename . split ( os . sep ) [ - 2 ] except IndexError : maybe_pytypes = None if maybe_pytypes == 'pytypes' and not tb2 . tb_next . tb_frame . f_code == pytypes . typechecker . _pytypes___import__ . __code__ : break else : limit += 1 tb2 = tb2 . tb_next return limit | Calculates limit - parameter to strip away pytypes internals when used with API from traceback module . | 153 | 22 |
19,041 | def _pytypes_excepthook ( exctype , value , tb ) : if pytypes . clean_traceback and issubclass ( exctype , TypeError ) : traceback . print_exception ( exctype , value , tb , _calc_traceback_limit ( tb ) ) else : if _sys_excepthook is None : sys . __excepthook__ ( exctype , value , tb ) else : _sys_excepthook ( exctype , value , tb ) | An excepthook suitable for use as sys . excepthook that strips away the part of the traceback belonging to pytypes internals . Can be switched on and off via pytypes . clean_traceback or pytypes . set_clean_traceback . The latter automatically installs this hook in sys . excepthook . | 125 | 70 |
19,042 | def get_generator_type ( genr ) : if genr in _checked_generator_types : return _checked_generator_types [ genr ] if not genr . gi_frame is None and 'gen_type' in genr . gi_frame . f_locals : return genr . gi_frame . f_locals [ 'gen_type' ] else : cllble , nesting , slf = util . get_callable_fq_for_code ( genr . gi_code ) if cllble is None : return Generator return _funcsigtypes ( cllble , slf , nesting [ - 1 ] if slf else None , genr . gi_frame . f_globals if not genr . gi_frame is None else None ) [ 1 ] | Obtains PEP 484 style type of a generator object i . e . returns a typing . Generator object . | 184 | 23 |
19,043 | def get_Generic_parameters ( tp , generic_supertype ) : try : res = _select_Generic_superclass_parameters ( tp , generic_supertype ) except TypeError : res = None if res is None : raise TypeError ( "%s has no proper parameters defined by %s." % ( type_str ( tp ) , type_str ( generic_supertype ) ) ) else : return tuple ( res ) | tp must be a subclass of generic_supertype . Retrieves the type values from tp that correspond to parameters defined by generic_supertype . | 95 | 31 |
19,044 | def get_Tuple_params ( tpl ) : try : return tpl . __tuple_params__ except AttributeError : try : if tpl . __args__ is None : return None # Python 3.6 if tpl . __args__ [ 0 ] == ( ) : return ( ) else : if tpl . __args__ [ - 1 ] is Ellipsis : return tpl . __args__ [ : - 1 ] if len ( tpl . __args__ ) > 1 else None else : return tpl . __args__ except AttributeError : return None | Python version independent function to obtain the parameters of a typing . Tuple object . Omits the ellipsis argument if present . Use is_Tuple_ellipsis for that . Tested with CPython 2 . 7 3 . 5 3 . 6 and Jython 2 . 7 . 1 . | 125 | 61 |
19,045 | def is_Tuple_ellipsis ( tpl ) : try : return tpl . __tuple_use_ellipsis__ except AttributeError : try : if tpl . __args__ is None : return False # Python 3.6 if tpl . __args__ [ - 1 ] is Ellipsis : return True except AttributeError : pass return False | Python version independent function to check if a typing . Tuple object contains an ellipsis . | 80 | 19 |
19,046 | def is_Union ( tp ) : if tp is Union : return True try : # Python 3.6 return tp . __origin__ is Union except AttributeError : try : return isinstance ( tp , typing . UnionMeta ) except AttributeError : return False | Python version independent check if a type is typing . Union . Tested with CPython 2 . 7 3 . 5 3 . 6 and Jython 2 . 7 . 1 . | 59 | 35 |
19,047 | def is_builtin_type ( tp ) : return hasattr ( __builtins__ , tp . __name__ ) and tp is getattr ( __builtins__ , tp . __name__ ) | Checks if the given type is a builtin one . | 47 | 12 |
19,048 | def get_types ( func ) : return _get_types ( func , util . is_classmethod ( func ) , util . is_method ( func ) ) | Works like get_type_hints but returns types as a sequence rather than a dictionary . Types are returned in declaration order of the corresponding arguments . | 35 | 30 |
19,049 | def get_member_types ( obj , member_name , prop_getter = False ) : cls = obj . __class__ member = getattr ( cls , member_name ) slf = not ( isinstance ( member , staticmethod ) or isinstance ( member , classmethod ) ) clsm = isinstance ( member , classmethod ) return _get_types ( member , clsm , slf , cls , prop_getter ) | Still experimental incomplete and hardly tested . Works like get_types but is also applicable to descriptors . | 97 | 20 |
19,050 | def _get_types ( func , clsm , slf , clss = None , prop_getter = False , unspecified_type = Any , infer_defaults = None ) : func0 = util . _actualfunc ( func , prop_getter ) # check consistency regarding special case with 'self'-keyword if not slf : argNames = util . getargnames ( util . getargspecs ( func0 ) ) if len ( argNames ) > 0 : if clsm : if argNames [ 0 ] != 'cls' : util . _warn_argname ( 'classmethod using non-idiomatic cls argname' , func0 , slf , clsm , clss ) if clss is None and ( slf or clsm ) : if slf : assert util . is_method ( func ) or isinstance ( func , property ) if clsm : assert util . is_classmethod ( func ) clss = util . get_class_that_defined_method ( func ) assert hasattr ( clss , func . __name__ ) args , res = _funcsigtypes ( func , slf or clsm , clss , None , prop_getter , unspecified_type = unspecified_type , infer_defaults = infer_defaults ) return _match_stub_type ( args ) , _match_stub_type ( res ) | Helper for get_types and get_member_types . | 299 | 12 |
19,051 | def _get_type_hints ( func , args = None , res = None , infer_defaults = None ) : if args is None or res is None : args2 , res2 = _get_types ( func , util . is_classmethod ( func ) , util . is_method ( func ) , unspecified_type = type ( NotImplemented ) , infer_defaults = infer_defaults ) if args is None : args = args2 if res is None : res = res2 slf = 1 if util . is_method ( func ) else 0 argNames = util . getargnames ( util . getargspecs ( util . _actualfunc ( func ) ) ) result = { } if not args is Any : prms = get_Tuple_params ( args ) for i in range ( slf , len ( argNames ) ) : if not prms [ i - slf ] is type ( NotImplemented ) : result [ argNames [ i ] ] = prms [ i - slf ] result [ 'return' ] = res return result | Helper for get_type_hints . | 232 | 9 |
19,052 | def _issubclass_Mapping_covariant ( subclass , superclass , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) : if is_Generic ( subclass ) : if subclass . __origin__ is None or not issubclass ( subclass . __origin__ , Mapping ) : return _issubclass_Generic ( subclass , superclass , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) if superclass . __args__ is None : if not pytypes . check_unbound_types : raise TypeError ( "Attempted to check unbound mapping type(superclass): " + str ( superclass ) ) if pytypes . strict_unknown_check : # Nothing is subtype of unknown type return False super_args = ( Any , Any ) else : super_args = superclass . __args__ if subclass . __args__ is None : if not pytypes . check_unbound_types : raise TypeError ( "Attempted to check unbound mapping type(subclass): " + str ( subclass ) ) if pytypes . strict_unknown_check : # Nothing can subclass unknown type # For value type it would be okay if superclass had Any as value type, # as unknown type is subtype of Any. However, since key type is invariant # and also unknown, it cannot pass. return False sub_args = ( Any , Any ) else : sub_args = subclass . __args__ if not _issubclass ( sub_args [ 0 ] , super_args [ 0 ] , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) : return False if not _issubclass ( sub_args [ 1 ] , super_args [ 1 ] , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) : return False return True return issubclass ( subclass , superclass ) | Helper for _issubclass a . k . a pytypes . issubtype . This subclass - check treats Mapping - values as covariant . | 472 | 31 |
19,053 | def _issubclass_Union_rec ( subclass , superclass , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) : # this function is partly based on code from typing module 3.5.2.2 super_args = get_Union_params ( superclass ) if super_args is None : return is_Union ( subclass ) elif is_Union ( subclass ) : sub_args = get_Union_params ( subclass ) if sub_args is None : return False return all ( _issubclass ( c , superclass , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) for c in ( sub_args ) ) elif isinstance ( subclass , TypeVar ) : if subclass in super_args : return True if subclass . __constraints__ : return _issubclass ( Union [ subclass . __constraints__ ] , superclass , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) return False else : return any ( _issubclass ( subclass , t , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) for t in super_args ) | Helper for _issubclass_Union . | 317 | 9 |
19,054 | def _isinstance ( obj , cls , bound_Generic = None , bound_typevars = None , bound_typevars_readonly = False , follow_fwd_refs = True , _recursion_check = None ) : if bound_typevars is None : bound_typevars = { } # Special treatment if cls is Iterable[...] if is_Generic ( cls ) and cls . __origin__ is typing . Iterable : if not is_iterable ( obj ) : return False itp = get_iterable_itemtype ( obj ) if itp is None : return not pytypes . check_iterables else : return _issubclass ( itp , cls . __args__ [ 0 ] , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) if is_Callable ( cls ) : return _isinstance_Callable ( obj , cls , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) return _issubclass ( deep_type ( obj ) , cls , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) | Access this via pytypes . is_of_type . Works like isinstance but supports PEP 484 style types from typing module . | 302 | 28 |
19,055 | def generator_checker_py3 ( gen , gen_type , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) : initialized = False sn = None try : while True : a = gen . send ( sn ) if initialized or not a is None : if not gen_type . __args__ [ 0 ] is Any and not _isinstance ( a , gen_type . __args__ [ 0 ] , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) : tpa = deep_type ( a ) msg = _make_generator_error_message ( deep_type ( a ) , gen , gen_type . __args__ [ 0 ] , 'has incompatible yield type' ) _raise_typecheck_error ( msg , True , a , tpa , gen_type . __args__ [ 0 ] ) # raise pytypes.ReturnTypeError(_make_generator_error_message(deep_type(a), gen, # gen_type.__args__[0], 'has incompatible yield type')) initialized = True sn = yield a if not gen_type . __args__ [ 1 ] is Any and not _isinstance ( sn , gen_type . __args__ [ 1 ] , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) : tpsn = deep_type ( sn ) msg = _make_generator_error_message ( tpsn , gen , gen_type . __args__ [ 1 ] , 'has incompatible send type' ) _raise_typecheck_error ( msg , False , sn , tpsn , gen_type . __args__ [ 1 ] ) # raise pytypes.InputTypeError(_make_generator_error_message(deep_type(sn), gen, # gen_type.__args__[1], 'has incompatible send type')) except StopIteration as st : # Python 3: # todo: Check if st.value is always defined (i.e. as None if not present) if not gen_type . __args__ [ 2 ] is Any and not _isinstance ( st . value , gen_type . __args__ [ 2 ] , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) : tpst = deep_type ( st . value ) msg = _make_generator_error_message ( tpst , gen , gen_type . __args__ [ 2 ] , 'has incompatible return type' ) _raise_typecheck_error ( msg , True , st . value , tpst , gen_type . __args__ [ 2 ] ) # raise pytypes.ReturnTypeError(_make_generator_error_message(sttp, gen, # gen_type.__args__[2], 'has incompatible return type')) raise st | Builds a typechecking wrapper around a Python 3 style generator object . | 681 | 14 |
19,056 | def generator_checker_py2 ( gen , gen_type , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) : initialized = False sn = None while True : a = gen . send ( sn ) if initialized or not a is None : if not gen_type . __args__ [ 0 ] is Any and not _isinstance ( a , gen_type . __args__ [ 0 ] , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) : tpa = deep_type ( a ) msg = _make_generator_error_message ( tpa , gen , gen_type . __args__ [ 0 ] , 'has incompatible yield type' ) _raise_typecheck_error ( msg , True , a , tpa , gen_type . __args__ [ 0 ] ) # raise pytypes.ReturnTypeError(_make_generator_error_message(tpa, gen, # gen_type.__args__[0], 'has incompatible yield type')) initialized = True sn = yield a if not gen_type . __args__ [ 1 ] is Any and not _isinstance ( sn , gen_type . __args__ [ 1 ] , bound_Generic , bound_typevars , bound_typevars_readonly , follow_fwd_refs , _recursion_check ) : tpsn = deep_type ( sn ) msg = _make_generator_error_message ( tpsn , gen , gen_type . __args__ [ 1 ] , 'has incompatible send type' ) _raise_typecheck_error ( msg , False , sn , tpsn , gen_type . __args__ [ 1 ] ) | Builds a typechecking wrapper around a Python 2 style generator object . | 403 | 14 |
19,057 | def annotations_func ( func ) : if not has_type_hints ( func ) : # What about defaults? func . __annotations__ = { } func . __annotations__ = _get_type_hints ( func , infer_defaults = False ) return func | Works like annotations but is only applicable to functions methods and properties . | 60 | 13 |
19,058 | def annotations_class ( cls ) : assert ( isclass ( cls ) ) # To play it safe we avoid to modify the dict while iterating over it, # so we previously cache keys. # For this we don't use keys() because of Python 3. # Todo: Better use inspect.getmembers here keys = [ key for key in cls . __dict__ ] for key in keys : memb = cls . __dict__ [ key ] if _check_as_func ( memb ) : annotations_func ( memb ) elif isclass ( memb ) : annotations_class ( memb ) return cls | Works like annotations but is only applicable to classes . | 136 | 10 |
19,059 | def dump_cache ( path = None , python2 = False , suffix = None ) : typelogging_enabled_tmp = pytypes . typelogging_enabled pytypes . typelogging_enabled = False if suffix is None : suffix = 'pyi2' if python2 else 'pyi' if path is None : path = pytypes . default_typelogger_path modules = { } for key in _member_cache : node = _member_cache [ key ] mname = node . get_modulename ( ) if not mname in modules : mnode = _module_node ( mname ) modules [ mname ] = mnode else : mnode = modules [ mname ] mnode . append ( node ) for module in modules : _dump_module ( modules [ module ] , path , python2 , suffix ) pytypes . typelogging_enabled = typelogging_enabled_tmp | Writes cached observations by | 197 | 5 |
19,060 | def get_indentation ( func ) : src_lines = getsourcelines ( func ) [ 0 ] for line in src_lines : if not ( line . startswith ( '@' ) or line . startswith ( 'def' ) or line . lstrip ( ) . startswith ( '#' ) ) : return line [ : len ( line ) - len ( line . lstrip ( ) ) ] return pytypes . default_indent | Extracts a function s indentation as a string In contrast to an inspect . indentsize based implementation this function preserves tabs if present . | 99 | 29 |
19,061 | def typelogged_func ( func ) : if not pytypes . typelogging_enabled : return func if hasattr ( func , 'do_logging' ) : func . do_logging = True return func elif hasattr ( func , 'do_typecheck' ) : # actually shouldn't happen return _typeinspect_func ( func , func . do_typecheck , True ) else : return _typeinspect_func ( func , False , True ) | Works like typelogged but is only applicable to functions methods and properties . | 102 | 15 |
19,062 | def typelogged_class ( cls ) : if not pytypes . typelogging_enabled : return cls assert ( isclass ( cls ) ) # To play it safe we avoid to modify the dict while iterating over it, # so we previously cache keys. # For this we don't use keys() because of Python 3. # Todo: Better use inspect.getmembers here keys = [ key for key in cls . __dict__ ] for key in keys : memb = cls . __dict__ [ key ] if _check_as_func ( memb ) : setattr ( cls , key , typelogged_func ( memb ) ) elif isclass ( memb ) : typelogged_class ( memb ) return cls | Works like typelogged but is only applicable to classes . | 165 | 12 |
19,063 | def typelogged_module ( md ) : if not pytypes . typelogging_enabled : return md if isinstance ( md , str ) : if md in sys . modules : md = sys . modules [ md ] if md is None : return md elif md in pytypes . typechecker . _pending_modules : # if import is pending, we just store this call for later pytypes . typechecker . _pending_modules [ md ] . append ( typelogged_module ) return md assert ( ismodule ( md ) ) if md . __name__ in pytypes . typechecker . _pending_modules : # if import is pending, we just store this call for later pytypes . typechecker . _pending_modules [ md . __name__ ] . append ( typelogged_module ) # we already process the module now as far as possible for its internal use # todo: Issue warning here that not the whole module might be covered yet assert ( ismodule ( md ) ) if md . __name__ in _fully_typelogged_modules and _fully_typelogged_modules [ md . __name__ ] == len ( md . __dict__ ) : return md # To play it safe we avoid to modify the dict while iterating over it, # so we previously cache keys. # For this we don't use keys() because of Python 3. # Todo: Better use inspect.getmembers here keys = [ key for key in md . __dict__ ] for key in keys : memb = md . __dict__ [ key ] if _check_as_func ( memb ) and memb . __module__ == md . __name__ : setattr ( md , key , typelogged_func ( memb ) ) elif isclass ( memb ) and memb . __module__ == md . __name__ : typelogged_class ( memb ) if not md . __name__ in pytypes . typechecker . _pending_modules : _fully_typelogged_modules [ md . __name__ ] = len ( md . __dict__ ) return md | Works like typelogged but is only applicable to modules by explicit call ) . md must be a module or a module name contained in sys . modules . | 459 | 31 |
19,064 | def enable_global_typechecked_decorator ( flag = True , retrospective = True ) : global global_typechecked_decorator global_typechecked_decorator = flag if import_hook_enabled : _install_import_hook ( ) if global_typechecked_decorator and retrospective : _catch_up_global_typechecked_decorator ( ) return global_typechecked_decorator | Enables or disables global typechecking mode via decorators . See flag global_typechecked_decorator . In contrast to setting the flag directly this function provides a retrospective option . If retrospective is true this will also affect already imported modules not only future imports . Does not work if checking_enabled is false . Does not work reliably if checking_enabled has ever been set to false during current run . | 91 | 82 |
19,065 | def enable_global_auto_override_decorator ( flag = True , retrospective = True ) : global global_auto_override_decorator global_auto_override_decorator = flag if import_hook_enabled : _install_import_hook ( ) if global_auto_override_decorator and retrospective : _catch_up_global_auto_override_decorator ( ) return global_auto_override_decorator | Enables or disables global auto_override mode via decorators . See flag global_auto_override_decorator . In contrast to setting the flag directly this function provides a retrospective option . If retrospective is true this will also affect already imported modules not only future imports . | 103 | 58 |
19,066 | def enable_global_annotations_decorator ( flag = True , retrospective = True ) : global global_annotations_decorator global_annotations_decorator = flag if import_hook_enabled : _install_import_hook ( ) if global_annotations_decorator and retrospective : _catch_up_global_annotations_decorator ( ) return global_annotations_decorator | Enables or disables global annotation mode via decorators . See flag global_annotations_decorator . In contrast to setting the flag directly this function provides a retrospective option . If retrospective is true this will also affect already imported modules not only future imports . | 91 | 53 |
19,067 | def enable_global_typelogged_decorator ( flag = True , retrospective = True ) : global global_typelogged_decorator global_typelogged_decorator = flag if import_hook_enabled : _install_import_hook ( ) if global_typelogged_decorator and retrospective : _catch_up_global_typelogged_decorator ( ) return global_typelogged_decorator | Enables or disables global typelog mode via decorators . See flag global_typelogged_decorator . In contrast to setting the flag directly this function provides a retrospective option . If retrospective is true this will also affect already imported modules not only future imports . | 97 | 56 |
19,068 | def enable_global_typechecked_profiler ( flag = True ) : global global_typechecked_profiler , _global_type_agent , global_typelogged_profiler global_typechecked_profiler = flag if flag and checking_enabled : if _global_type_agent is None : _global_type_agent = TypeAgent ( ) _global_type_agent . start ( ) elif not _global_type_agent . active : _global_type_agent . start ( ) elif not flag and not global_typelogged_profiler and not _global_type_agent is None and _global_type_agent . active : _global_type_agent . stop ( ) | Enables or disables global typechecking mode via a profiler . See flag global_typechecked_profiler . Does not work if checking_enabled is false . | 153 | 34 |
19,069 | def enable_global_typelogged_profiler ( flag = True ) : global global_typelogged_profiler , _global_type_agent , global_typechecked_profiler global_typelogged_profiler = flag if flag and typelogging_enabled : if _global_type_agent is None : _global_type_agent = TypeAgent ( ) _global_type_agent . start ( ) elif not _global_type_agent . active : _global_type_agent . start ( ) elif not flag and not global_typechecked_profiler and not _global_type_agent is None and _global_type_agent . active : _global_type_agent . stop ( ) | Enables or disables global typelogging mode via a profiler . See flag global_typelogged_profiler . Does not work if typelogging_enabled is false . | 156 | 38 |
19,070 | def typechecked_func ( func , force = False , argType = None , resType = None , prop_getter = False ) : if not pytypes . checking_enabled and not pytypes . do_logging_in_typechecked : return func assert ( _check_as_func ( func ) ) if not force and is_no_type_check ( func ) : return func if hasattr ( func , 'do_typecheck' ) : func . do_typecheck = True return func elif hasattr ( func , 'do_logging' ) : # actually shouldn't happen return _typeinspect_func ( func , True , func . do_logging , argType , resType , prop_getter ) else : return _typeinspect_func ( func , True , False , argType , resType , prop_getter ) | Works like typechecked but is only applicable to functions methods and properties . | 185 | 14 |
19,071 | def typechecked_class ( cls , force = False , force_recursive = False ) : return _typechecked_class ( cls , set ( ) , force , force_recursive ) | Works like typechecked but is only applicable to classes . | 42 | 11 |
19,072 | def auto_override_class ( cls , force = False , force_recursive = False ) : if not pytypes . checking_enabled : return cls assert ( isclass ( cls ) ) if not force and is_no_type_check ( cls ) : return cls # To play it safe we avoid to modify the dict while iterating over it, # so we previously cache keys. # For this we don't use keys() because of Python 3. # Todo: Better use inspect.getmembers here keys = [ key for key in cls . __dict__ ] for key in keys : memb = cls . __dict__ [ key ] if force_recursive or not is_no_type_check ( memb ) : if isfunction ( memb ) or ismethod ( memb ) or ismethoddescriptor ( memb ) : if util . _has_base_method ( memb , cls ) : setattr ( cls , key , override ( memb ) ) elif isclass ( memb ) : auto_override_class ( memb , force_recursive , force_recursive ) return cls | Works like auto_override but is only applicable to classes . | 250 | 13 |
19,073 | def is_no_type_check ( memb ) : try : return hasattr ( memb , '__no_type_check__' ) and memb . __no_type_check__ or memb in _not_type_checked except TypeError : return False | Checks if an object was annotated with | 59 | 9 |
19,074 | def check_argument_types ( cllable = None , call_args = None , clss = None , caller_level = 0 ) : return _check_caller_type ( False , cllable , call_args , clss , caller_level + 1 ) | Can be called from within a function or method to apply typechecking to the arguments that were passed in by the caller . Checking is applied w . r . t . type hints of the function or method hosting the call to check_argument_types . | 59 | 50 |
19,075 | def check_return_type ( value , cllable = None , clss = None , caller_level = 0 ) : return _check_caller_type ( True , cllable , value , clss , caller_level + 1 ) | Can be called from within a function or method to apply typechecking to the value that is going to be returned . Checking is applied w . r . t . type hints of the function or method hosting the call to check_return_type . | 53 | 49 |
19,076 | def load_diagram_from_csv ( filepath , bpmn_diagram ) : sequence_flows = bpmn_diagram . sequence_flows process_elements_dict = bpmn_diagram . process_elements diagram_attributes = bpmn_diagram . diagram_attributes plane_attributes = bpmn_diagram . plane_attributes process_dict = BpmnDiagramGraphCSVImport . import_csv_file_as_dict ( filepath ) BpmnDiagramGraphCSVImport . populate_diagram_elements_dict ( diagram_attributes ) BpmnDiagramGraphCSVImport . populate_process_elements_dict ( process_elements_dict , process_dict ) BpmnDiagramGraphCSVImport . populate_plane_elements_dict ( plane_attributes ) BpmnDiagramGraphCSVImport . import_nodes ( process_dict , bpmn_diagram , sequence_flows ) BpmnDiagramGraphCSVImport . representation_adjustment ( process_dict , bpmn_diagram , sequence_flows ) | Reads an CSV file from given filepath and maps it into inner representation of BPMN diagram . Returns an instance of BPMNDiagramGraph class . | 251 | 33 |
19,077 | def load_diagram_from_xml ( filepath , bpmn_diagram ) : diagram_graph = bpmn_diagram . diagram_graph sequence_flows = bpmn_diagram . sequence_flows process_elements_dict = bpmn_diagram . process_elements diagram_attributes = bpmn_diagram . diagram_attributes plane_attributes = bpmn_diagram . plane_attributes collaboration = bpmn_diagram . collaboration document = BpmnDiagramGraphImport . read_xml_file ( filepath ) # According to BPMN 2.0 XML Schema, there's only one 'BPMNDiagram' and 'BPMNPlane' diagram_element = document . getElementsByTagNameNS ( "*" , "BPMNDiagram" ) [ 0 ] plane_element = diagram_element . getElementsByTagNameNS ( "*" , "BPMNPlane" ) [ 0 ] BpmnDiagramGraphImport . import_diagram_and_plane_attributes ( diagram_attributes , plane_attributes , diagram_element , plane_element ) BpmnDiagramGraphImport . import_process_elements ( document , diagram_graph , sequence_flows , process_elements_dict , plane_element ) collaboration_element_list = document . getElementsByTagNameNS ( "*" , consts . Consts . collaboration ) if collaboration_element_list is not None and len ( collaboration_element_list ) > 0 : # Diagram has multiple pools and lanes collaboration_element = collaboration_element_list [ 0 ] BpmnDiagramGraphImport . import_collaboration_element ( diagram_graph , collaboration_element , collaboration ) if consts . Consts . message_flows in collaboration : message_flows = collaboration [ consts . Consts . message_flows ] else : message_flows = { } participants = [ ] if consts . Consts . participants in collaboration : participants = collaboration [ consts . Consts . participants ] for element in utils . BpmnImportUtils . iterate_elements ( plane_element ) : if element . nodeType != element . TEXT_NODE : tag_name = utils . BpmnImportUtils . remove_namespace_from_tag_name ( element . tagName ) if tag_name == consts . Consts . bpmn_shape : BpmnDiagramGraphImport . import_shape_di ( participants , diagram_graph , element ) elif tag_name == consts . Consts . bpmn_edge : BpmnDiagramGraphImport . import_flow_di ( diagram_graph , sequence_flows , message_flows , element ) | Reads an XML file from given filepath and maps it into inner representation of BPMN diagram . Returns an instance of BPMNDiagramGraph class . | 605 | 33 |
19,078 | def import_collaboration_element ( diagram_graph , collaboration_element , collaboration_dict ) : collaboration_dict [ consts . Consts . id ] = collaboration_element . getAttribute ( consts . Consts . id ) collaboration_dict [ consts . Consts . participants ] = { } participants_dict = collaboration_dict [ consts . Consts . participants ] collaboration_dict [ consts . Consts . message_flows ] = { } message_flows_dict = collaboration_dict [ consts . Consts . message_flows ] for element in utils . BpmnImportUtils . iterate_elements ( collaboration_element ) : if element . nodeType != element . TEXT_NODE : tag_name = utils . BpmnImportUtils . remove_namespace_from_tag_name ( element . tagName ) if tag_name == consts . Consts . participant : BpmnDiagramGraphImport . import_participant_element ( diagram_graph , participants_dict , element ) elif tag_name == consts . Consts . message_flow : BpmnDiagramGraphImport . import_message_flow_to_graph ( diagram_graph , message_flows_dict , element ) | Method that imports information from collaboration element . | 269 | 8 |
19,079 | def import_participant_element ( diagram_graph , participants_dictionary , participant_element ) : participant_id = participant_element . getAttribute ( consts . Consts . id ) name = participant_element . getAttribute ( consts . Consts . name ) process_ref = participant_element . getAttribute ( consts . Consts . process_ref ) if participant_element . getAttribute ( consts . Consts . process_ref ) == '' : diagram_graph . add_node ( participant_id ) diagram_graph . node [ participant_id ] [ consts . Consts . type ] = consts . Consts . participant diagram_graph . node [ participant_id ] [ consts . Consts . process ] = participant_id participants_dictionary [ participant_id ] = { consts . Consts . name : name , consts . Consts . process_ref : process_ref } | Adds participant element to the collaboration dictionary . | 197 | 8 |
19,080 | def import_process_elements ( document , diagram_graph , sequence_flows , process_elements_dict , plane_element ) : for process_element in document . getElementsByTagNameNS ( "*" , consts . Consts . process ) : BpmnDiagramGraphImport . import_process_element ( process_elements_dict , process_element ) process_id = process_element . getAttribute ( consts . Consts . id ) process_attributes = process_elements_dict [ process_id ] lane_set_list = process_element . getElementsByTagNameNS ( "*" , consts . Consts . lane_set ) if lane_set_list is not None and len ( lane_set_list ) > 0 : # according to BPMN 2.0 XML Schema, there's at most one 'laneSet' element inside 'process' lane_set = lane_set_list [ 0 ] BpmnDiagramGraphImport . import_lane_set_element ( process_attributes , lane_set , plane_element ) for element in utils . BpmnImportUtils . iterate_elements ( process_element ) : if element . nodeType != element . TEXT_NODE : tag_name = utils . BpmnImportUtils . remove_namespace_from_tag_name ( element . tagName ) BpmnDiagramGraphImport . __import_element_by_tag_name ( diagram_graph , sequence_flows , process_id , process_attributes , element , tag_name ) for flow in utils . BpmnImportUtils . iterate_elements ( process_element ) : if flow . nodeType != flow . TEXT_NODE : tag_name = utils . BpmnImportUtils . remove_namespace_from_tag_name ( flow . tagName ) if tag_name == consts . Consts . sequence_flow : BpmnDiagramGraphImport . import_sequence_flow_to_graph ( diagram_graph , sequence_flows , process_id , flow ) | Method for importing all process elements in diagram . | 462 | 9 |
19,081 | def import_child_lane_set_element ( child_lane_set_element , plane_element ) : lane_set_id = child_lane_set_element . getAttribute ( consts . Consts . id ) lanes_attr = { } for element in utils . BpmnImportUtils . iterate_elements ( child_lane_set_element ) : if element . nodeType != element . TEXT_NODE : tag_name = utils . BpmnImportUtils . remove_namespace_from_tag_name ( element . tagName ) if tag_name == consts . Consts . lane : lane = element lane_id = lane . getAttribute ( consts . Consts . id ) lane_attr = BpmnDiagramGraphImport . import_lane_element ( lane , plane_element ) lanes_attr [ lane_id ] = lane_attr child_lane_set_attr = { consts . Consts . id : lane_set_id , consts . Consts . lanes : lanes_attr } return child_lane_set_attr | Method for importing childLaneSet element from diagram file . | 238 | 12 |
19,082 | def import_task_to_graph ( diagram_graph , process_id , process_attributes , task_element ) : BpmnDiagramGraphImport . import_activity_to_graph ( diagram_graph , process_id , process_attributes , task_element ) | Adds to graph the new element that represents BPMN task . In our representation tasks have only basic attributes and elements inherited from Activity type so this method only needs to call add_flownode_to_graph . | 60 | 44 |
19,083 | def import_data_object_to_graph ( diagram_graph , process_id , process_attributes , data_object_element ) : BpmnDiagramGraphImport . import_flow_node_to_graph ( diagram_graph , process_id , process_attributes , data_object_element ) data_object_id = data_object_element . getAttribute ( consts . Consts . id ) diagram_graph . node [ data_object_id ] [ consts . Consts . is_collection ] = data_object_element . getAttribute ( consts . Consts . is_collection ) if data_object_element . hasAttribute ( consts . Consts . is_collection ) else "false" | Adds to graph the new element that represents BPMN data object . Data object inherits attributes from FlowNode . In addition an attribute isCollection is added to the node . | 158 | 35 |
19,084 | def import_parallel_gateway_to_graph ( diagram_graph , process_id , process_attributes , element ) : BpmnDiagramGraphImport . import_gateway_to_graph ( diagram_graph , process_id , process_attributes , element ) | Adds to graph the new element that represents BPMN parallel gateway . Parallel gateway doesn t have additional attributes . Separate method is used to improve code readability . | 61 | 33 |
19,085 | def export_task_info ( node_params , output_element ) : if consts . Consts . default in node_params and node_params [ consts . Consts . default ] is not None : output_element . set ( consts . Consts . default , node_params [ consts . Consts . default ] ) | Adds Task node attributes to exported XML element | 72 | 8 |
19,086 | def export_subprocess_info ( bpmn_diagram , subprocess_params , output_element ) : output_element . set ( consts . Consts . triggered_by_event , subprocess_params [ consts . Consts . triggered_by_event ] ) if consts . Consts . default in subprocess_params and subprocess_params [ consts . Consts . default ] is not None : output_element . set ( consts . Consts . default , subprocess_params [ consts . Consts . default ] ) # for each node in graph add correct type of element, its attributes and BPMNShape element subprocess_id = subprocess_params [ consts . Consts . id ] nodes = bpmn_diagram . get_nodes_list_by_process_id ( subprocess_id ) for node in nodes : node_id = node [ 0 ] params = node [ 1 ] BpmnDiagramGraphExport . export_node_data ( bpmn_diagram , node_id , params , output_element ) # for each edge in graph add sequence flow element, its attributes and BPMNEdge element flows = bpmn_diagram . get_flows_list_by_process_id ( subprocess_id ) for flow in flows : params = flow [ 2 ] BpmnDiagramGraphExport . export_flow_process_data ( params , output_element ) | Adds Subprocess node attributes to exported XML element | 313 | 9 |
19,087 | def export_data_object_info ( bpmn_diagram , data_object_params , output_element ) : output_element . set ( consts . Consts . is_collection , data_object_params [ consts . Consts . is_collection ] ) | Adds DataObject node attributes to exported XML element | 60 | 9 |
19,088 | def export_complex_gateway_info ( node_params , output_element ) : output_element . set ( consts . Consts . gateway_direction , node_params [ consts . Consts . gateway_direction ] ) if consts . Consts . default in node_params and node_params [ consts . Consts . default ] is not None : output_element . set ( consts . Consts . default , node_params [ consts . Consts . default ] ) | Adds ComplexGateway node attributes to exported XML element | 106 | 10 |
19,089 | def export_event_based_gateway_info ( node_params , output_element ) : output_element . set ( consts . Consts . gateway_direction , node_params [ consts . Consts . gateway_direction ] ) output_element . set ( consts . Consts . instantiate , node_params [ consts . Consts . instantiate ] ) output_element . set ( consts . Consts . event_gateway_type , node_params [ consts . Consts . event_gateway_type ] ) | Adds EventBasedGateway node attributes to exported XML element | 118 | 11 |
19,090 | def export_inclusive_exclusive_gateway_info ( node_params , output_element ) : output_element . set ( consts . Consts . gateway_direction , node_params [ consts . Consts . gateway_direction ] ) if consts . Consts . default in node_params and node_params [ consts . Consts . default ] is not None : output_element . set ( consts . Consts . default , node_params [ consts . Consts . default ] ) | Adds InclusiveGateway or ExclusiveGateway node attributes to exported XML element | 109 | 15 |
19,091 | def export_parallel_gateway_info ( node_params , output_element ) : output_element . set ( consts . Consts . gateway_direction , node_params [ consts . Consts . gateway_direction ] ) | Adds parallel gateway node attributes to exported XML element | 51 | 9 |
19,092 | def export_start_event_info ( node_params , output_element ) : output_element . set ( consts . Consts . parallel_multiple , node_params . get ( consts . Consts . parallel_multiple ) ) output_element . set ( consts . Consts . is_interrupting , node_params . get ( consts . Consts . is_interrupting ) ) definitions = node_params . get ( consts . Consts . event_definitions ) for definition in definitions : definition_id = definition [ consts . Consts . id ] definition_type = definition [ consts . Consts . definition_type ] output_definition = eTree . SubElement ( output_element , definition_type ) if definition_id != "" : output_definition . set ( consts . Consts . id , definition_id ) | Adds StartEvent attributes to exported XML element | 185 | 8 |
19,093 | def export_throw_event_info ( node_params , output_element ) : definitions = node_params [ consts . Consts . event_definitions ] for definition in definitions : definition_id = definition [ consts . Consts . id ] definition_type = definition [ consts . Consts . definition_type ] output_definition = eTree . SubElement ( output_element , definition_type ) if definition_id != "" : output_definition . set ( consts . Consts . id , definition_id ) | Adds EndEvent or IntermediateThrowingEvent attributes to exported XML element | 113 | 13 |
19,094 | def export_boundary_event_info ( node_params , output_element ) : output_element . set ( consts . Consts . parallel_multiple , node_params [ consts . Consts . parallel_multiple ] ) output_element . set ( consts . Consts . cancel_activity , node_params [ consts . Consts . cancel_activity ] ) output_element . set ( consts . Consts . attached_to_ref , node_params [ consts . Consts . attached_to_ref ] ) definitions = node_params [ consts . Consts . event_definitions ] for definition in definitions : definition_id = definition [ consts . Consts . id ] definition_type = definition [ consts . Consts . definition_type ] output_definition = eTree . SubElement ( output_element , definition_type ) if definition_id != "" : output_definition . set ( consts . Consts . id , definition_id ) | Adds IntermediateCatchEvent attributes to exported XML element | 211 | 10 |
19,095 | def export_process_element ( definitions , process_id , process_attributes_dictionary ) : process = eTree . SubElement ( definitions , consts . Consts . process ) process . set ( consts . Consts . id , process_id ) process . set ( consts . Consts . is_closed , process_attributes_dictionary [ consts . Consts . is_closed ] ) process . set ( consts . Consts . is_executable , process_attributes_dictionary [ consts . Consts . is_executable ] ) process . set ( consts . Consts . process_type , process_attributes_dictionary [ consts . Consts . process_type ] ) return process | Creates process element for exported BPMN XML file . | 159 | 12 |
19,096 | def export_lane_set ( process , lane_set , plane_element ) : lane_set_xml = eTree . SubElement ( process , consts . Consts . lane_set ) for key , value in lane_set [ consts . Consts . lanes ] . items ( ) : BpmnDiagramGraphExport . export_lane ( lane_set_xml , key , value , plane_element ) | Creates laneSet element for exported BPMN XML file . | 90 | 13 |
19,097 | def export_child_lane_set ( parent_xml_element , child_lane_set , plane_element ) : lane_set_xml = eTree . SubElement ( parent_xml_element , consts . Consts . lane_set ) for key , value in child_lane_set [ consts . Consts . lanes ] . items ( ) : BpmnDiagramGraphExport . export_lane ( lane_set_xml , key , value , plane_element ) | Creates childLaneSet element for exported BPMN XML file . | 104 | 15 |
19,098 | def export_lane ( parent_xml_element , lane_id , lane_attr , plane_element ) : lane_xml = eTree . SubElement ( parent_xml_element , consts . Consts . lane ) lane_xml . set ( consts . Consts . id , lane_id ) lane_xml . set ( consts . Consts . name , lane_attr [ consts . Consts . name ] ) if consts . Consts . child_lane_set in lane_attr and len ( lane_attr [ consts . Consts . child_lane_set ] ) : child_lane_set = lane_attr [ consts . Consts . child_lane_set ] BpmnDiagramGraphExport . export_child_lane_set ( lane_xml , child_lane_set , plane_element ) if consts . Consts . flow_node_refs in lane_attr and len ( lane_attr [ consts . Consts . flow_node_refs ] ) : for flow_node_ref_id in lane_attr [ consts . Consts . flow_node_refs ] : flow_node_ref_xml = eTree . SubElement ( lane_xml , consts . Consts . flow_node_ref ) flow_node_ref_xml . text = flow_node_ref_id output_element_di = eTree . SubElement ( plane_element , BpmnDiagramGraphExport . bpmndi_namespace + consts . Consts . bpmn_shape ) output_element_di . set ( consts . Consts . id , lane_id + "_gui" ) output_element_di . set ( consts . Consts . bpmn_element , lane_id ) output_element_di . set ( consts . Consts . is_horizontal , lane_attr [ consts . Consts . is_horizontal ] ) bounds = eTree . SubElement ( output_element_di , "omgdc:Bounds" ) bounds . set ( consts . Consts . width , lane_attr [ consts . Consts . width ] ) bounds . set ( consts . Consts . height , lane_attr [ consts . Consts . height ] ) bounds . set ( consts . Consts . x , lane_attr [ consts . Consts . x ] ) bounds . set ( consts . Consts . y , lane_attr [ consts . Consts . y ] ) | Creates lane element for exported BPMN XML file . | 545 | 12 |
19,099 | def export_node_di_data ( node_id , params , plane ) : output_element_di = eTree . SubElement ( plane , BpmnDiagramGraphExport . bpmndi_namespace + consts . Consts . bpmn_shape ) output_element_di . set ( consts . Consts . id , node_id + "_gui" ) output_element_di . set ( consts . Consts . bpmn_element , node_id ) bounds = eTree . SubElement ( output_element_di , "omgdc:Bounds" ) bounds . set ( consts . Consts . width , params [ consts . Consts . width ] ) bounds . set ( consts . Consts . height , params [ consts . Consts . height ] ) bounds . set ( consts . Consts . x , params [ consts . Consts . x ] ) bounds . set ( consts . Consts . y , params [ consts . Consts . y ] ) if params [ consts . Consts . type ] == consts . Consts . subprocess : output_element_di . set ( consts . Consts . is_expanded , params [ consts . Consts . is_expanded ] ) | Creates a new BPMNShape XML element for given node parameters and adds it to plane element . | 277 | 21 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.