nodes
stringlengths 501
22.4k
| edges
stringlengths 138
5.07k
| code
stringlengths 108
19.3k
|
---|---|---|
0, module; 1, function_definition; 2, function_name:index; 3, parameters; 4, block; 5, identifier:self; 6, identifier:columnnames; 7, default_parameter; 8, expression_statement; 9, import_from_statement; 10, return_statement; 11, identifier:sort; 12, True; 13, comment:"""Return a tableindex object.
:class:`tableindex` lets one get the row numbers of the rows holding
given values for the columns for which the index is created.
It uses an in-memory index on which a binary search is done.
By default the table is sorted on the given columns to get the correct
index order.
For example::
t = table('3c343.MS')
tinx = t.index('ANTENNA1')
print tinx.rownumbers(0) # print rownrs containing ANTENNA1=0
"""; 14, relative_import; 15, dotted_name; 16, call; 17, import_prefix; 18, dotted_name; 19, identifier:tableindex; 20, identifier:tableindex; 21, argument_list; 22, identifier:tableindex; 23, identifier:self; 24, identifier:columnnames; 25, identifier:sort | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 7, 11; 7, 12; 8, 13; 9, 14; 9, 15; 10, 16; 14, 17; 14, 18; 15, 19; 16, 20; 16, 21; 18, 22; 21, 23; 21, 24; 21, 25 | def index(self, columnnames, sort=True):
"""Return a tableindex object.
:class:`tableindex` lets one get the row numbers of the rows holding
given values for the columns for which the index is created.
It uses an in-memory index on which a binary search is done.
By default the table is sorted on the given columns to get the correct
index order.
For example::
t = table('3c343.MS')
tinx = t.index('ANTENNA1')
print tinx.rownumbers(0) # print rownrs containing ANTENNA1=0
"""
from .tableindex import tableindex
return tableindex(self, columnnames, sort) |
0, module; 1, function_definition; 2, function_name:rownumbers; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, expression_statement; 8, if_statement; 9, return_statement; 10, identifier:table; 11, None; 12, comment:"""Return a list containing the row numbers of this table.
This method can be useful after a selection or a sort.
It returns the row numbers of the rows in this table with respect
to the given table. If no table is given, the original table is used.
For example::
t = table('W53.MS')
t1 = t.selectrows([1,3,5,7,9]) # select a few rows
t1.rownumbers(t)
# [1 3 5 7 9]
t2 = t1.selectrows([2,5]) # select rows from the selection
t2.rownumbers(t1)
# [2 5] # rownrs of t2 in table t1
t2.rownumbers(t)
# [3 9] # rownrs of t2 in t
t2.rownumbers()
# [3 9]
The last statements show that the method returns the row numbers
referring to the given table. Table t2 contains rows 2 and 5 in
table t1, which are rows 3 and 9 in table t.
"""; 13, comparison_operator:table is None; 14, block; 15, call; 16, identifier:table; 17, None; 18, return_statement; 19, attribute; 20, argument_list; 21, call; 22, identifier:self; 23, identifier:_rownumbers; 24, identifier:table; 25, attribute; 26, argument_list; 27, identifier:self; 28, identifier:_rownumbers; 29, call; 30, identifier:Table; 31, argument_list | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 6, 10; 6, 11; 7, 12; 8, 13; 8, 14; 9, 15; 13, 16; 13, 17; 14, 18; 15, 19; 15, 20; 18, 21; 19, 22; 19, 23; 20, 24; 21, 25; 21, 26; 25, 27; 25, 28; 26, 29; 29, 30; 29, 31 | def rownumbers(self, table=None):
"""Return a list containing the row numbers of this table.
This method can be useful after a selection or a sort.
It returns the row numbers of the rows in this table with respect
to the given table. If no table is given, the original table is used.
For example::
t = table('W53.MS')
t1 = t.selectrows([1,3,5,7,9]) # select a few rows
t1.rownumbers(t)
# [1 3 5 7 9]
t2 = t1.selectrows([2,5]) # select rows from the selection
t2.rownumbers(t1)
# [2 5] # rownrs of t2 in table t1
t2.rownumbers(t)
# [3 9] # rownrs of t2 in t
t2.rownumbers()
# [3 9]
The last statements show that the method returns the row numbers
referring to the given table. Table t2 contains rows 2 and 5 in
table t1, which are rows 3 and 9 in table t.
"""
if table is None:
return self._rownumbers(Table())
return self._rownumbers(table) |
0, module; 1, function_definition; 2, function_name:showstructure; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, expression_statement; 11, return_statement; 12, identifier:dataman; 13, True; 14, identifier:column; 15, True; 16, identifier:subtable; 17, False; 18, identifier:sort; 19, False; 20, comment:"""Show table structure in a formatted string.
The structure of this table and optionally its subtables is shown.
It shows the data manager info and column descriptions.
Optionally the columns are sorted in alphabetical order.
`dataman`
Show data manager info? If False, only column info is shown.
If True, data manager info and columns per data manager are shown.
`column`
Show column description per data manager? Only takes effect if
dataman=True.
`subtable`
Show the structure of all subtables (recursively).
The names of subtables are always shown.
'sort'
Sort the columns in alphabetical order?
"""; 21, call; 22, attribute; 23, argument_list; 24, identifier:self; 25, identifier:_showstructure; 26, identifier:dataman; 27, identifier:column; 28, identifier:subtable; 29, identifier:sort | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, 10; 4, 11; 6, 12; 6, 13; 7, 14; 7, 15; 8, 16; 8, 17; 9, 18; 9, 19; 10, 20; 11, 21; 21, 22; 21, 23; 22, 24; 22, 25; 23, 26; 23, 27; 23, 28; 23, 29 | def showstructure(self, dataman=True, column=True, subtable=False,
sort=False):
"""Show table structure in a formatted string.
The structure of this table and optionally its subtables is shown.
It shows the data manager info and column descriptions.
Optionally the columns are sorted in alphabetical order.
`dataman`
Show data manager info? If False, only column info is shown.
If True, data manager info and columns per data manager are shown.
`column`
Show column description per data manager? Only takes effect if
dataman=True.
`subtable`
Show the structure of all subtables (recursively).
The names of subtables are always shown.
'sort'
Sort the columns in alphabetical order?
"""
return self._showstructure(dataman, column, subtable, sort) |
0, module; 1, function_definition; 2, function_name:query; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, default_parameter; 13, expression_statement; 14, if_statement; 15, expression_statement; 16, if_statement; 17, expression_statement; 18, if_statement; 19, if_statement; 20, if_statement; 21, if_statement; 22, if_statement; 23, return_statement; 24, identifier:query; 25, string; 26, identifier:name; 27, string; 28, identifier:sortlist; 29, string; 30, identifier:columns; 31, string; 32, identifier:limit; 33, integer:0; 34, identifier:offset; 35, integer:0; 36, identifier:style; 37, string; 38, comment:"""Query the table and return the result as a reference table.
This method queries the table. It forms a
`TaQL <../../doc/199.html>`_
command from the given arguments and executes it using the
:func:`taql` function.
The result is returned in a so-called reference table which
references the selected columns and rows in the original table.
Usually a reference table is temporary, but it can be made
persistent by giving it a name.
Note that a reference table is handled as any table, thus can be
queried again.
All arguments are optional, but at least one of `query`, `name`,
`sortlist`, and `columns` should be used.
See the `TaQL note <../../doc/199.html>`_ for the
detailed description of the the arguments representing the various
parts of a TaQL command.
`query`
The WHERE part of a TaQL command.
`name`
The name of the reference table if it is to be made persistent.
`sortlist`
The ORDERBY part of a TaQL command. It is a single string in which
commas have to be used to separate sort keys.
`columns`
The columns to be selected (projection in data base terms). It is a
single string in which commas have to be used to separate column
names. Apart from column names, expressions can be given as well.
`limit`
If > 0, maximum number of rows to be selected.
`offset`
If > 0, ignore the first N matches.
`style`
The TaQL syntax style to be used (defaults to Python).
"""; 39, boolean_operator; 40, block; 41, assignment; 42, identifier:columns; 43, block; 44, augmented_assignment; 45, identifier:query; 46, block; 47, identifier:sortlist; 48, block; 49, comparison_operator:limit > 0; 50, block; 51, comparison_operator:offset > 0; 52, block; 53, identifier:name; 54, block; 55, call; 56, string_content:Python; 57, boolean_operator; 58, comparison_operator:offset <= 0; 59, raise_statement; 60, identifier:command; 61, string; 62, expression_statement; 63, identifier:command; 64, string; 65, expression_statement; 66, expression_statement; 67, identifier:limit; 68, integer:0; 69, expression_statement; 70, identifier:offset; 71, integer:0; 72, expression_statement; 73, expression_statement; 74, identifier:tablecommand; 75, argument_list; 76, boolean_operator; 77, line_continuation:\; 78, comparison_operator:limit <= 0; 79, identifier:offset; 80, integer:0; 81, call; 82, string_content:select; 83, augmented_assignment; 84, string_content:from $1; 85, augmented_assignment; 86, augmented_assignment; 87, augmented_assignment; 88, augmented_assignment; 89, augmented_assignment; 90, identifier:command; 91, identifier:style; 92, list; 93, boolean_operator; 94, not_operator; 95, identifier:limit; 96, integer:0; 97, identifier:ValueError; 98, argument_list; 99, identifier:command; 100, identifier:columns; 101, identifier:command; 102, binary_operator:' where ' + query; 103, identifier:command; 104, binary_operator:' orderby ' + sortlist; 105, identifier:command; 106, binary_operator:' limit %d' % limit; 107, identifier:command; 108, binary_operator:' offset %d' % offset; 109, identifier:command; 110, binary_operator:' giving ' + name; 111, identifier:self; 112, not_operator; 113, not_operator; 114, identifier:columns; 115, binary_operator:'No selection done (arguments query, ' +
'sortlist, columns, limit, and offset are empty)'; 116, string; 117, identifier:query; 118, string; 119, identifier:sortlist; 120, string; 121, identifier:limit; 122, string; 123, identifier:offset; 124, string; 125, identifier:name; 126, identifier:query; 127, identifier:sortlist; 128, string; 129, string; 130, string_content:where; 131, string_content:orderby; 132, string_content:limit %d; 133, string_content:offset %d; 134, string_content:giving; 135, string_content:No selection done (arguments query,; 136, string_content:sortlist, columns, limit, and offset are empty) | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 4, 23; 6, 24; 6, 25; 7, 26; 7, 27; 8, 28; 8, 29; 9, 30; 9, 31; 10, 32; 10, 33; 11, 34; 11, 35; 12, 36; 12, 37; 13, 38; 14, 39; 14, 40; 15, 41; 16, 42; 16, 43; 17, 44; 18, 45; 18, 46; 19, 47; 19, 48; 20, 49; 20, 50; 21, 51; 21, 52; 22, 53; 22, 54; 23, 55; 37, 56; 39, 57; 39, 58; 40, 59; 41, 60; 41, 61; 43, 62; 44, 63; 44, 64; 46, 65; 48, 66; 49, 67; 49, 68; 50, 69; 51, 70; 51, 71; 52, 72; 54, 73; 55, 74; 55, 75; 57, 76; 57, 77; 57, 78; 58, 79; 58, 80; 59, 81; 61, 82; 62, 83; 64, 84; 65, 85; 66, 86; 69, 87; 72, 88; 73, 89; 75, 90; 75, 91; 75, 92; 76, 93; 76, 94; 78, 95; 78, 96; 81, 97; 81, 98; 83, 99; 83, 100; 85, 101; 85, 102; 86, 103; 86, 104; 87, 105; 87, 106; 88, 107; 88, 108; 89, 109; 89, 110; 92, 111; 93, 112; 93, 113; 94, 114; 98, 115; 102, 116; 102, 117; 104, 118; 104, 119; 106, 120; 106, 121; 108, 122; 108, 123; 110, 124; 110, 125; 112, 126; 113, 127; 115, 128; 115, 129; 116, 130; 118, 131; 120, 132; 122, 133; 124, 134; 128, 135; 129, 136 | def query(self, query='', name='', sortlist='', columns='',
limit=0, offset=0, style='Python'):
"""Query the table and return the result as a reference table.
This method queries the table. It forms a
`TaQL <../../doc/199.html>`_
command from the given arguments and executes it using the
:func:`taql` function.
The result is returned in a so-called reference table which
references the selected columns and rows in the original table.
Usually a reference table is temporary, but it can be made
persistent by giving it a name.
Note that a reference table is handled as any table, thus can be
queried again.
All arguments are optional, but at least one of `query`, `name`,
`sortlist`, and `columns` should be used.
See the `TaQL note <../../doc/199.html>`_ for the
detailed description of the the arguments representing the various
parts of a TaQL command.
`query`
The WHERE part of a TaQL command.
`name`
The name of the reference table if it is to be made persistent.
`sortlist`
The ORDERBY part of a TaQL command. It is a single string in which
commas have to be used to separate sort keys.
`columns`
The columns to be selected (projection in data base terms). It is a
single string in which commas have to be used to separate column
names. Apart from column names, expressions can be given as well.
`limit`
If > 0, maximum number of rows to be selected.
`offset`
If > 0, ignore the first N matches.
`style`
The TaQL syntax style to be used (defaults to Python).
"""
if not query and not sortlist and not columns and \
limit <= 0 and offset <= 0:
raise ValueError('No selection done (arguments query, ' +
'sortlist, columns, limit, and offset are empty)')
command = 'select '
if columns:
command += columns
command += ' from $1'
if query:
command += ' where ' + query
if sortlist:
command += ' orderby ' + sortlist
if limit > 0:
command += ' limit %d' % limit
if offset > 0:
command += ' offset %d' % offset
if name:
command += ' giving ' + name
return tablecommand(command, style, [self]) |
0, module; 1, function_definition; 2, function_name:sort; 3, parameters; 4, block; 5, identifier:self; 6, identifier:sortlist; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, expression_statement; 12, expression_statement; 13, if_statement; 14, if_statement; 15, if_statement; 16, return_statement; 17, identifier:name; 18, string; 19, identifier:limit; 20, integer:0; 21, identifier:offset; 22, integer:0; 23, identifier:style; 24, string; 25, comment:"""Sort the table and return the result as a reference table.
This method sorts the table. It forms a
`TaQL <../../doc/199.html>`_
command from the given arguments and executes it using the
:func:`taql` function.
The result is returned in a so-called reference table which references
the columns and rows in the original table. Usually a reference
table is temporary, but it can be made persistent by giving it a name.
Note that a reference table is handled as any table, thus can be
queried again.
`sortlist`
The ORDERBY part of a TaQL command. It is a single string in which
commas have to be used to separate sort keys. A sort key can be the
name of a column, but it can be an expression as well.
`name`
The name of the reference table if it is to be made persistent.
`limit`
If > 0, maximum number of rows to be selected after the sort step.
It can, for instance, be used to select the N highest values.
`offset`
If > 0, ignore the first `offset` matches after the sort step.
`style`
The TaQL syntax style to be used (defaults to Python).
"""; 26, assignment; 27, comparison_operator:limit > 0; 28, block; 29, comparison_operator:offset > 0; 30, block; 31, identifier:name; 32, block; 33, call; 34, string_content:Python; 35, identifier:command; 36, binary_operator:'select from $1 orderby ' + sortlist; 37, identifier:limit; 38, integer:0; 39, expression_statement; 40, identifier:offset; 41, integer:0; 42, expression_statement; 43, expression_statement; 44, identifier:tablecommand; 45, argument_list; 46, string; 47, identifier:sortlist; 48, augmented_assignment; 49, augmented_assignment; 50, augmented_assignment; 51, identifier:command; 52, identifier:style; 53, list; 54, string_content:select from $1 orderby; 55, identifier:command; 56, binary_operator:' limit %d' % limit; 57, identifier:command; 58, binary_operator:' offset %d' % offset; 59, identifier:command; 60, binary_operator:' giving ' + name; 61, identifier:self; 62, string; 63, identifier:limit; 64, string; 65, identifier:offset; 66, string; 67, identifier:name; 68, string_content:limit %d; 69, string_content:offset %d; 70, string_content:giving | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 7, 17; 7, 18; 8, 19; 8, 20; 9, 21; 9, 22; 10, 23; 10, 24; 11, 25; 12, 26; 13, 27; 13, 28; 14, 29; 14, 30; 15, 31; 15, 32; 16, 33; 24, 34; 26, 35; 26, 36; 27, 37; 27, 38; 28, 39; 29, 40; 29, 41; 30, 42; 32, 43; 33, 44; 33, 45; 36, 46; 36, 47; 39, 48; 42, 49; 43, 50; 45, 51; 45, 52; 45, 53; 46, 54; 48, 55; 48, 56; 49, 57; 49, 58; 50, 59; 50, 60; 53, 61; 56, 62; 56, 63; 58, 64; 58, 65; 60, 66; 60, 67; 62, 68; 64, 69; 66, 70 | def sort(self, sortlist, name='',
limit=0, offset=0, style='Python'):
"""Sort the table and return the result as a reference table.
This method sorts the table. It forms a
`TaQL <../../doc/199.html>`_
command from the given arguments and executes it using the
:func:`taql` function.
The result is returned in a so-called reference table which references
the columns and rows in the original table. Usually a reference
table is temporary, but it can be made persistent by giving it a name.
Note that a reference table is handled as any table, thus can be
queried again.
`sortlist`
The ORDERBY part of a TaQL command. It is a single string in which
commas have to be used to separate sort keys. A sort key can be the
name of a column, but it can be an expression as well.
`name`
The name of the reference table if it is to be made persistent.
`limit`
If > 0, maximum number of rows to be selected after the sort step.
It can, for instance, be used to select the N highest values.
`offset`
If > 0, ignore the first `offset` matches after the sort step.
`style`
The TaQL syntax style to be used (defaults to Python).
"""
command = 'select from $1 orderby ' + sortlist
if limit > 0:
command += ' limit %d' % limit
if offset > 0:
command += ' offset %d' % offset
if name:
command += ' giving ' + name
return tablecommand(command, style, [self]) |
0, module; 1, function_definition; 2, function_name:describe_pdb; 3, parameters; 4, block; 5, identifier:pdb_id; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, return_statement; 11, comment:"""Get description and metadata of a PDB entry
Parameters
----------
pdb_id : string
A 4 character string giving a pdb entry of interest
Returns
-------
out : string
A text pdb description from PDB
Examples
--------
>>> describe_pdb('4lza')
{'citation_authors': 'Malashkevich, V.N., Bhosle, R., Toro, R., Hillerich, B., Gizzi, A., Garforth, S., Kar, A., Chan, M.K., Lafluer, J., Patel, H., Matikainen, B., Chamala, S., Lim, S., Celikgil, A., Villegas, G., Evans, B., Love, J., Fiser, A., Khafizov, K., Seidel, R., Bonanno, J.B., Almo, S.C.',
'deposition_date': '2013-07-31',
'expMethod': 'X-RAY DIFFRACTION',
'keywords': 'TRANSFERASE',
'last_modification_date': '2013-08-14',
'nr_atoms': '0',
'nr_entities': '1',
'nr_residues': '390',
'release_date': '2013-08-14',
'resolution': '1.84',
'status': 'CURRENT',
'structureId': '4LZA',
'structure_authors': 'Malashkevich, V.N., Bhosle, R., Toro, R., Hillerich, B., Gizzi, A., Garforth, S., Kar, A., Chan, M.K., Lafluer, J., Patel, H., Matikainen, B., Chamala, S., Lim, S., Celikgil, A., Villegas, G., Evans, B., Love, J., Fiser, A., Khafizov, K., Seidel, R., Bonanno, J.B., Almo, S.C., New York Structural Genomics Research Consortium (NYSGRC)',
'title': 'Crystal structure of adenine phosphoribosyltransferase from Thermoanaerobacter pseudethanolicus ATCC 33223, NYSGRC Target 029700.'}
"""; 12, assignment; 13, assignment; 14, assignment; 15, identifier:out; 16, identifier:out; 17, call; 18, identifier:out; 19, call; 20, identifier:out; 21, call; 22, identifier:get_info; 23, argument_list; 24, identifier:to_dict; 25, argument_list; 26, identifier:remove_at_sign; 27, argument_list; 28, identifier:pdb_id; 29, keyword_argument; 30, identifier:out; 31, subscript; 32, identifier:url_root; 33, string; 34, subscript; 35, string; 36, string_content:http://www.rcsb.org/pdb/rest/describePDB?structureId=; 37, identifier:out; 38, string; 39, string_content:PDB; 40, string_content:PDBdescription | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 6, 11; 7, 12; 8, 13; 9, 14; 10, 15; 12, 16; 12, 17; 13, 18; 13, 19; 14, 20; 14, 21; 17, 22; 17, 23; 19, 24; 19, 25; 21, 26; 21, 27; 23, 28; 23, 29; 25, 30; 27, 31; 29, 32; 29, 33; 31, 34; 31, 35; 33, 36; 34, 37; 34, 38; 35, 39; 38, 40 | def describe_pdb(pdb_id):
"""Get description and metadata of a PDB entry
Parameters
----------
pdb_id : string
A 4 character string giving a pdb entry of interest
Returns
-------
out : string
A text pdb description from PDB
Examples
--------
>>> describe_pdb('4lza')
{'citation_authors': 'Malashkevich, V.N., Bhosle, R., Toro, R., Hillerich, B., Gizzi, A., Garforth, S., Kar, A., Chan, M.K., Lafluer, J., Patel, H., Matikainen, B., Chamala, S., Lim, S., Celikgil, A., Villegas, G., Evans, B., Love, J., Fiser, A., Khafizov, K., Seidel, R., Bonanno, J.B., Almo, S.C.',
'deposition_date': '2013-07-31',
'expMethod': 'X-RAY DIFFRACTION',
'keywords': 'TRANSFERASE',
'last_modification_date': '2013-08-14',
'nr_atoms': '0',
'nr_entities': '1',
'nr_residues': '390',
'release_date': '2013-08-14',
'resolution': '1.84',
'status': 'CURRENT',
'structureId': '4LZA',
'structure_authors': 'Malashkevich, V.N., Bhosle, R., Toro, R., Hillerich, B., Gizzi, A., Garforth, S., Kar, A., Chan, M.K., Lafluer, J., Patel, H., Matikainen, B., Chamala, S., Lim, S., Celikgil, A., Villegas, G., Evans, B., Love, J., Fiser, A., Khafizov, K., Seidel, R., Bonanno, J.B., Almo, S.C., New York Structural Genomics Research Consortium (NYSGRC)',
'title': 'Crystal structure of adenine phosphoribosyltransferase from Thermoanaerobacter pseudethanolicus ATCC 33223, NYSGRC Target 029700.'}
"""
out = get_info(pdb_id, url_root = 'http://www.rcsb.org/pdb/rest/describePDB?structureId=')
out = to_dict(out)
out = remove_at_sign(out['PDBdescription']['PDB'])
return out |
0, module; 1, function_definition; 2, function_name:matrix_index; 3, parameters; 4, block; 5, identifier:user; 6, expression_statement; 7, expression_statement; 8, return_statement; 9, comment:"""
Returns the keys associated with each axis of the matrices.
The first key is always the name of the current user, followed by the
sorted names of all the correspondants.
"""; 10, assignment; 11, binary_operator:[user.name] + other_keys; 12, identifier:other_keys; 13, call; 14, list; 15, identifier:other_keys; 16, identifier:sorted; 17, argument_list; 18, attribute; 19, list_comprehension; 20, identifier:user; 21, identifier:name; 22, identifier:k; 23, for_in_clause; 24, if_clause; 25, identifier:k; 26, call; 27, comparison_operator:k != user.name; 28, attribute; 29, argument_list; 30, identifier:k; 31, attribute; 32, attribute; 33, identifier:keys; 34, identifier:user; 35, identifier:name; 36, identifier:user; 37, identifier:network | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 6, 9; 7, 10; 8, 11; 10, 12; 10, 13; 11, 14; 11, 15; 13, 16; 13, 17; 14, 18; 17, 19; 18, 20; 18, 21; 19, 22; 19, 23; 19, 24; 23, 25; 23, 26; 24, 27; 26, 28; 26, 29; 27, 30; 27, 31; 28, 32; 28, 33; 31, 34; 31, 35; 32, 36; 32, 37 | def matrix_index(user):
"""
Returns the keys associated with each axis of the matrices.
The first key is always the name of the current user, followed by the
sorted names of all the correspondants.
"""
other_keys = sorted([k for k in user.network.keys() if k != user.name])
return [user.name] + other_keys |
0, module; 1, function_definition; 2, function_name:assortativity_indicators; 3, parameters; 4, block; 5, identifier:user; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, comment:# Use all indicator except reporting variables and attributes; 11, expression_statement; 12, expression_statement; 13, for_statement; 14, expression_statement; 15, for_statement; 16, return_statement; 17, comment:"""
Computes the assortativity of indicators.
This indicator measures the similarity of the current user with his
correspondants, for all bandicoot indicators. For each one, it calculates
the variance of the current user's value with the values for all his
correspondants:
.. math::
\\text{assortativity}(J) = \\frac{1}{n} \\sum_i^n (J_{\\text{user}} - J_{\\text{i}})^2
for the indicator :math:`J`, and all the :math:`n` correspondents.
"""; 18, assignment; 19, assignment; 20, assignment; 21, assignment; 22, assignment; 23, pattern_list; 24, call; 25, block; 26, assignment; 27, identifier:i; 28, identifier:count_indicator; 29, block; 30, identifier:assortativity; 31, identifier:matrix; 32, call; 33, identifier:count_indicator; 34, call; 35, identifier:total_indicator; 36, call; 37, identifier:ego_indics; 38, call; 39, identifier:ego_indics; 40, dictionary_comprehension; 41, identifier:i; 42, identifier:u_name; 43, identifier:enumerate; 44, argument_list; 45, expression_statement; 46, comment:# Non reciprocated edge; 47, if_statement; 48, expression_statement; 49, for_statement; 50, identifier:assortativity; 51, dictionary; 52, expression_statement; 53, identifier:matrix_undirected_unweighted; 54, argument_list; 55, identifier:defaultdict; 56, argument_list; 57, identifier:defaultdict; 58, argument_list; 59, identifier:all; 60, argument_list; 61, pair; 62, for_in_clause; 63, if_clause; 64, call; 65, assignment; 66, boolean_operator; 67, block; 68, assignment; 69, identifier:a; 70, identifier:ego_indics; 71, block; 72, assignment; 73, identifier:user; 74, identifier:int; 75, identifier:int; 76, identifier:user; 77, keyword_argument; 78, identifier:a; 79, identifier:value; 80, pattern_list; 81, call; 82, boolean_operator; 83, identifier:matrix_index; 84, argument_list; 85, identifier:correspondent; 86, call; 87, boolean_operator; 88, comparison_operator:matrix[0][i] == 0; 89, continue_statement; 90, identifier:neighbor_indics; 91, call; 92, if_statement; 93, subscript; 94, binary_operator:count_indicator[i] / total_indicator[i]; 95, identifier:flatten; 96, True; 97, identifier:a; 98, identifier:value; 99, attribute; 100, argument_list; 101, boolean_operator; 102, comparison_operator:a[:10] != "attributes"; 103, identifier:user; 104, attribute; 105, argument_list; 106, comparison_operator:correspondent is None; 107, comparison_operator:u_name == user.name; 108, subscript; 109, integer:0; 110, identifier:all; 111, argument_list; 112, boolean_operator; 113, block; 114, identifier:assortativity; 115, identifier:i; 116, subscript; 117, subscript; 118, identifier:ego_indics; 119, identifier:items; 120, comparison_operator:a != "name"; 121, comparison_operator:a[:11] != "reporting__"; 122, subscript; 123, string:"attributes"; 124, attribute; 125, identifier:get; 126, identifier:u_name; 127, None; 128, identifier:correspondent; 129, None; 130, identifier:u_name; 131, attribute; 132, subscript; 133, identifier:i; 134, identifier:correspondent; 135, keyword_argument; 136, comparison_operator:ego_indics[a] is not None; 137, comparison_operator:neighbor_indics[a] is not None; 138, expression_statement; 139, expression_statement; 140, identifier:count_indicator; 141, identifier:i; 142, identifier:total_indicator; 143, identifier:i; 144, identifier:a; 145, string:"name"; 146, subscript; 147, string:"reporting__"; 148, identifier:a; 149, slice; 150, identifier:user; 151, identifier:network; 152, identifier:user; 153, identifier:name; 154, identifier:matrix; 155, integer:0; 156, identifier:flatten; 157, True; 158, subscript; 159, None; 160, subscript; 161, None; 162, augmented_assignment; 163, augmented_assignment; 164, identifier:a; 165, slice; 166, integer:10; 167, identifier:ego_indics; 168, identifier:a; 169, identifier:neighbor_indics; 170, identifier:a; 171, subscript; 172, integer:1; 173, subscript; 174, binary_operator:(ego_indics[a] - neighbor_indics[a]) ** 2; 175, integer:11; 176, identifier:total_indicator; 177, identifier:a; 178, identifier:count_indicator; 179, identifier:a; 180, parenthesized_expression; 181, integer:2; 182, binary_operator:ego_indics[a] - neighbor_indics[a]; 183, subscript; 184, subscript; 185, identifier:ego_indics; 186, identifier:a; 187, identifier:neighbor_indics; 188, identifier:a | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 6, 17; 7, 18; 8, 19; 9, 20; 11, 21; 12, 22; 13, 23; 13, 24; 13, 25; 14, 26; 15, 27; 15, 28; 15, 29; 16, 30; 18, 31; 18, 32; 19, 33; 19, 34; 20, 35; 20, 36; 21, 37; 21, 38; 22, 39; 22, 40; 23, 41; 23, 42; 24, 43; 24, 44; 25, 45; 25, 46; 25, 47; 25, 48; 25, 49; 26, 50; 26, 51; 29, 52; 32, 53; 32, 54; 34, 55; 34, 56; 36, 57; 36, 58; 38, 59; 38, 60; 40, 61; 40, 62; 40, 63; 44, 64; 45, 65; 47, 66; 47, 67; 48, 68; 49, 69; 49, 70; 49, 71; 52, 72; 54, 73; 56, 74; 58, 75; 60, 76; 60, 77; 61, 78; 61, 79; 62, 80; 62, 81; 63, 82; 64, 83; 64, 84; 65, 85; 65, 86; 66, 87; 66, 88; 67, 89; 68, 90; 68, 91; 71, 92; 72, 93; 72, 94; 77, 95; 77, 96; 80, 97; 80, 98; 81, 99; 81, 100; 82, 101; 82, 102; 84, 103; 86, 104; 86, 105; 87, 106; 87, 107; 88, 108; 88, 109; 91, 110; 91, 111; 92, 112; 92, 113; 93, 114; 93, 115; 94, 116; 94, 117; 99, 118; 99, 119; 101, 120; 101, 121; 102, 122; 102, 123; 104, 124; 104, 125; 105, 126; 105, 127; 106, 128; 106, 129; 107, 130; 107, 131; 108, 132; 108, 133; 111, 134; 111, 135; 112, 136; 112, 137; 113, 138; 113, 139; 116, 140; 116, 141; 117, 142; 117, 143; 120, 144; 120, 145; 121, 146; 121, 147; 122, 148; 122, 149; 124, 150; 124, 151; 131, 152; 131, 153; 132, 154; 132, 155; 135, 156; 135, 157; 136, 158; 136, 159; 137, 160; 137, 161; 138, 162; 139, 163; 146, 164; 146, 165; 149, 166; 158, 167; 158, 168; 160, 169; 160, 170; 162, 171; 162, 172; 163, 173; 163, 174; 165, 175; 171, 176; 171, 177; 173, 178; 173, 179; 174, 180; 174, 181; 180, 182; 182, 183; 182, 184; 183, 185; 183, 186; 184, 187; 184, 188 | def assortativity_indicators(user):
"""
Computes the assortativity of indicators.
This indicator measures the similarity of the current user with his
correspondants, for all bandicoot indicators. For each one, it calculates
the variance of the current user's value with the values for all his
correspondants:
.. math::
\\text{assortativity}(J) = \\frac{1}{n} \\sum_i^n (J_{\\text{user}} - J_{\\text{i}})^2
for the indicator :math:`J`, and all the :math:`n` correspondents.
"""
matrix = matrix_undirected_unweighted(user)
count_indicator = defaultdict(int)
total_indicator = defaultdict(int)
# Use all indicator except reporting variables and attributes
ego_indics = all(user, flatten=True)
ego_indics = {a: value for a, value in ego_indics.items()
if a != "name" and a[:11] != "reporting__" and
a[:10] != "attributes"}
for i, u_name in enumerate(matrix_index(user)):
correspondent = user.network.get(u_name, None)
# Non reciprocated edge
if correspondent is None or u_name == user.name or matrix[0][i] == 0:
continue
neighbor_indics = all(correspondent, flatten=True)
for a in ego_indics:
if ego_indics[a] is not None and neighbor_indics[a] is not None:
total_indicator[a] += 1
count_indicator[a] += (ego_indics[a] - neighbor_indics[a]) ** 2
assortativity = {}
for i in count_indicator:
assortativity[i] = count_indicator[i] / total_indicator[i]
return assortativity |
0, module; 1, function_definition; 2, function_name:assortativity_attributes; 3, parameters; 4, block; 5, identifier:user; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, for_statement; 11, expression_statement; 12, for_statement; 13, return_statement; 14, comment:"""
Computes the assortativity of the nominal attributes.
This indicator measures the homophily of the current user with his
correspondants, for each attributes. It returns a value between 0
(no assortativity) and 1 (all the contacts share the same value):
the percentage of contacts sharing the same value.
"""; 15, assignment; 16, assignment; 17, assignment; 18, pattern_list; 19, call; 20, block; 21, assignment; 22, identifier:a; 23, attribute; 24, block; 25, identifier:assortativity; 26, identifier:matrix; 27, call; 28, identifier:neighbors; 29, list_comprehension; 30, identifier:neighbors_attrbs; 31, dictionary; 32, identifier:i; 33, identifier:u_name; 34, identifier:enumerate; 35, argument_list; 36, expression_statement; 37, if_statement; 38, if_statement; 39, identifier:assortativity; 40, dictionary; 41, identifier:user; 42, identifier:attributes; 43, expression_statement; 44, expression_statement; 45, expression_statement; 46, identifier:matrix_undirected_unweighted; 47, argument_list; 48, identifier:k; 49, for_in_clause; 50, if_clause; 51, call; 52, assignment; 53, boolean_operator; 54, block; 55, attribute; 56, block; 57, assignment; 58, assignment; 59, assignment; 60, identifier:user; 61, identifier:k; 62, call; 63, comparison_operator:k != user.name; 64, identifier:matrix_index; 65, argument_list; 66, identifier:correspondent; 67, call; 68, boolean_operator; 69, comparison_operator:matrix[0][i] == 0; 70, continue_statement; 71, identifier:correspondent; 72, identifier:has_attributes; 73, expression_statement; 74, identifier:total; 75, call; 76, identifier:den; 77, call; 78, subscript; 79, conditional_expression:total / den if den != 0 else None; 80, attribute; 81, argument_list; 82, identifier:k; 83, attribute; 84, identifier:user; 85, attribute; 86, argument_list; 87, comparison_operator:correspondent is None; 88, comparison_operator:u_name == user.name; 89, subscript; 90, integer:0; 91, assignment; 92, identifier:sum; 93, generator_expression; 94, identifier:sum; 95, generator_expression; 96, identifier:assortativity; 97, identifier:a; 98, binary_operator:total / den; 99, comparison_operator:den != 0; 100, None; 101, attribute; 102, identifier:keys; 103, identifier:user; 104, identifier:name; 105, attribute; 106, identifier:get; 107, identifier:u_name; 108, None; 109, identifier:correspondent; 110, None; 111, identifier:u_name; 112, attribute; 113, subscript; 114, identifier:i; 115, subscript; 116, attribute; 117, integer:1; 118, for_in_clause; 119, if_clause; 120, integer:1; 121, for_in_clause; 122, if_clause; 123, identifier:total; 124, identifier:den; 125, identifier:den; 126, integer:0; 127, identifier:user; 128, identifier:network; 129, identifier:user; 130, identifier:network; 131, identifier:user; 132, identifier:name; 133, identifier:matrix; 134, integer:0; 135, identifier:neighbors_attrbs; 136, attribute; 137, identifier:correspondent; 138, identifier:attributes; 139, identifier:n; 140, identifier:neighbors; 141, boolean_operator; 142, identifier:n; 143, identifier:neighbors; 144, comparison_operator:n in neighbors_attrbs; 145, identifier:correspondent; 146, identifier:name; 147, comparison_operator:n in neighbors_attrbs; 148, comparison_operator:user.attributes[a] == neighbors_attrbs[n][a]; 149, identifier:n; 150, identifier:neighbors_attrbs; 151, identifier:n; 152, identifier:neighbors_attrbs; 153, subscript; 154, subscript; 155, attribute; 156, identifier:a; 157, subscript; 158, identifier:a; 159, identifier:user; 160, identifier:attributes; 161, identifier:neighbors_attrbs; 162, identifier:n | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 6, 14; 7, 15; 8, 16; 9, 17; 10, 18; 10, 19; 10, 20; 11, 21; 12, 22; 12, 23; 12, 24; 13, 25; 15, 26; 15, 27; 16, 28; 16, 29; 17, 30; 17, 31; 18, 32; 18, 33; 19, 34; 19, 35; 20, 36; 20, 37; 20, 38; 21, 39; 21, 40; 23, 41; 23, 42; 24, 43; 24, 44; 24, 45; 27, 46; 27, 47; 29, 48; 29, 49; 29, 50; 35, 51; 36, 52; 37, 53; 37, 54; 38, 55; 38, 56; 43, 57; 44, 58; 45, 59; 47, 60; 49, 61; 49, 62; 50, 63; 51, 64; 51, 65; 52, 66; 52, 67; 53, 68; 53, 69; 54, 70; 55, 71; 55, 72; 56, 73; 57, 74; 57, 75; 58, 76; 58, 77; 59, 78; 59, 79; 62, 80; 62, 81; 63, 82; 63, 83; 65, 84; 67, 85; 67, 86; 68, 87; 68, 88; 69, 89; 69, 90; 73, 91; 75, 92; 75, 93; 77, 94; 77, 95; 78, 96; 78, 97; 79, 98; 79, 99; 79, 100; 80, 101; 80, 102; 83, 103; 83, 104; 85, 105; 85, 106; 86, 107; 86, 108; 87, 109; 87, 110; 88, 111; 88, 112; 89, 113; 89, 114; 91, 115; 91, 116; 93, 117; 93, 118; 93, 119; 95, 120; 95, 121; 95, 122; 98, 123; 98, 124; 99, 125; 99, 126; 101, 127; 101, 128; 105, 129; 105, 130; 112, 131; 112, 132; 113, 133; 113, 134; 115, 135; 115, 136; 116, 137; 116, 138; 118, 139; 118, 140; 119, 141; 121, 142; 121, 143; 122, 144; 136, 145; 136, 146; 141, 147; 141, 148; 144, 149; 144, 150; 147, 151; 147, 152; 148, 153; 148, 154; 153, 155; 153, 156; 154, 157; 154, 158; 155, 159; 155, 160; 157, 161; 157, 162 | def assortativity_attributes(user):
"""
Computes the assortativity of the nominal attributes.
This indicator measures the homophily of the current user with his
correspondants, for each attributes. It returns a value between 0
(no assortativity) and 1 (all the contacts share the same value):
the percentage of contacts sharing the same value.
"""
matrix = matrix_undirected_unweighted(user)
neighbors = [k for k in user.network.keys() if k != user.name]
neighbors_attrbs = {}
for i, u_name in enumerate(matrix_index(user)):
correspondent = user.network.get(u_name, None)
if correspondent is None or u_name == user.name or matrix[0][i] == 0:
continue
if correspondent.has_attributes:
neighbors_attrbs[correspondent.name] = correspondent.attributes
assortativity = {}
for a in user.attributes:
total = sum(1 for n in neighbors if n in neighbors_attrbs and user.attributes[a] == neighbors_attrbs[n][a])
den = sum(1 for n in neighbors if n in neighbors_attrbs)
assortativity[a] = total / den if den != 0 else None
return assortativity |
0, module; 1, function_definition; 2, function_name:merge_close; 3, parameters; 4, block; 5, identifier:events; 6, identifier:min_interval; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, return_statement; 13, identifier:merge_to_longer; 14, False; 15, comment:"""Merge events that are separated by a less than a minimum interval.
Parameters
----------
events : list of dict
events with 'start' and 'end' times, from one or several channels.
**Events must be sorted by their start time.**
min_interval : float
minimum delay between consecutive events, in seconds
merge_to_longer : bool (default: False)
If True, info (chan, peak, etc.) from the longer of the 2 events is
kept. Otherwise, info from the earlier onset spindle is kept.
Returns
-------
list of dict
original events list with close events merged.
"""; 16, assignment; 17, assignment; 18, identifier:higher; 19, identifier:events; 20, block; 21, identifier:merged; 22, identifier:half_iv; 23, binary_operator:min_interval / 2; 24, identifier:merged; 25, list; 26, if_statement; 27, identifier:min_interval; 28, integer:2; 29, not_operator; 30, block; 31, else_clause; 32, identifier:merged; 33, expression_statement; 34, block; 35, call; 36, expression_statement; 37, if_statement; 38, attribute; 39, argument_list; 40, assignment; 41, comparison_operator:higher['start'] - half_iv <= lower['end'] + half_iv; 42, block; 43, else_clause; 44, identifier:merged; 45, identifier:append; 46, identifier:higher; 47, identifier:lower; 48, subscript; 49, binary_operator:higher['start'] - half_iv; 50, binary_operator:lower['end'] + half_iv; 51, if_statement; 52, block; 53, identifier:merged; 54, unary_operator; 55, subscript; 56, identifier:half_iv; 57, subscript; 58, identifier:half_iv; 59, boolean_operator; 60, block; 61, else_clause; 62, expression_statement; 63, integer:1; 64, identifier:higher; 65, string; 66, identifier:lower; 67, string; 68, identifier:merge_to_longer; 69, parenthesized_expression; 70, expression_statement; 71, expression_statement; 72, expression_statement; 73, block; 74, call; 75, string_content:start; 76, string_content:end; 77, comparison_operator:higher['end'] - higher['start'] >
lower['end'] - lower['start']; 78, assignment; 79, call; 80, assignment; 81, expression_statement; 82, expression_statement; 83, attribute; 84, argument_list; 85, binary_operator:higher['end'] - higher['start']; 86, binary_operator:lower['end'] - lower['start']; 87, identifier:start; 88, call; 89, attribute; 90, argument_list; 91, subscript; 92, identifier:higher; 93, assignment; 94, call; 95, identifier:merged; 96, identifier:append; 97, identifier:higher; 98, subscript; 99, subscript; 100, subscript; 101, subscript; 102, identifier:min; 103, argument_list; 104, identifier:higher; 105, identifier:update; 106, dictionary; 107, identifier:merged; 108, unary_operator; 109, identifier:end; 110, call; 111, attribute; 112, argument_list; 113, identifier:higher; 114, string; 115, identifier:higher; 116, string; 117, identifier:lower; 118, string; 119, identifier:lower; 120, string; 121, subscript; 122, subscript; 123, pair; 124, integer:1; 125, identifier:max; 126, argument_list; 127, subscript; 128, identifier:update; 129, dictionary; 130, string_content:end; 131, string_content:start; 132, string_content:end; 133, string_content:start; 134, identifier:lower; 135, string; 136, identifier:higher; 137, string; 138, string; 139, identifier:start; 140, subscript; 141, subscript; 142, identifier:merged; 143, unary_operator; 144, pair; 145, string_content:start; 146, string_content:start; 147, string_content:start; 148, identifier:lower; 149, string; 150, identifier:higher; 151, string; 152, integer:1; 153, string; 154, identifier:end; 155, string_content:end; 156, string_content:end; 157, string_content:end | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 7, 13; 7, 14; 8, 15; 9, 16; 10, 17; 11, 18; 11, 19; 11, 20; 12, 21; 16, 22; 16, 23; 17, 24; 17, 25; 20, 26; 23, 27; 23, 28; 26, 29; 26, 30; 26, 31; 29, 32; 30, 33; 31, 34; 33, 35; 34, 36; 34, 37; 35, 38; 35, 39; 36, 40; 37, 41; 37, 42; 37, 43; 38, 44; 38, 45; 39, 46; 40, 47; 40, 48; 41, 49; 41, 50; 42, 51; 43, 52; 48, 53; 48, 54; 49, 55; 49, 56; 50, 57; 50, 58; 51, 59; 51, 60; 51, 61; 52, 62; 54, 63; 55, 64; 55, 65; 57, 66; 57, 67; 59, 68; 59, 69; 60, 70; 60, 71; 60, 72; 61, 73; 62, 74; 65, 75; 67, 76; 69, 77; 70, 78; 71, 79; 72, 80; 73, 81; 73, 82; 74, 83; 74, 84; 77, 85; 77, 86; 78, 87; 78, 88; 79, 89; 79, 90; 80, 91; 80, 92; 81, 93; 82, 94; 83, 95; 83, 96; 84, 97; 85, 98; 85, 99; 86, 100; 86, 101; 88, 102; 88, 103; 89, 104; 89, 105; 90, 106; 91, 107; 91, 108; 93, 109; 93, 110; 94, 111; 94, 112; 98, 113; 98, 114; 99, 115; 99, 116; 100, 117; 100, 118; 101, 119; 101, 120; 103, 121; 103, 122; 106, 123; 108, 124; 110, 125; 110, 126; 111, 127; 111, 128; 112, 129; 114, 130; 116, 131; 118, 132; 120, 133; 121, 134; 121, 135; 122, 136; 122, 137; 123, 138; 123, 139; 126, 140; 126, 141; 127, 142; 127, 143; 129, 144; 135, 145; 137, 146; 138, 147; 140, 148; 140, 149; 141, 150; 141, 151; 143, 152; 144, 153; 144, 154; 149, 155; 151, 156; 153, 157 | def merge_close(events, min_interval, merge_to_longer=False):
"""Merge events that are separated by a less than a minimum interval.
Parameters
----------
events : list of dict
events with 'start' and 'end' times, from one or several channels.
**Events must be sorted by their start time.**
min_interval : float
minimum delay between consecutive events, in seconds
merge_to_longer : bool (default: False)
If True, info (chan, peak, etc.) from the longer of the 2 events is
kept. Otherwise, info from the earlier onset spindle is kept.
Returns
-------
list of dict
original events list with close events merged.
"""
half_iv = min_interval / 2
merged = []
for higher in events:
if not merged:
merged.append(higher)
else:
lower = merged[-1]
if higher['start'] - half_iv <= lower['end'] + half_iv:
if merge_to_longer and (higher['end'] - higher['start'] >
lower['end'] - lower['start']):
start = min(lower['start'], higher['start'])
higher.update({'start': start})
merged[-1] = higher
else:
end = max(lower['end'], higher['end'])
merged[-1].update({'end': end})
else:
merged.append(higher)
return merged |
0, module; 1, function_definition; 2, function_name:_remove_duplicate; 3, parameters; 4, block; 5, identifier:old_events; 6, identifier:dat; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, comment:# more convenient, it copies old_event first and then compares; 12, expression_statement; 13, expression_statement; 14, if_statement; 15, expression_statement; 16, expression_statement; 17, for_statement; 18, return_statement; 19, comment:"""Remove duplicates from the events.
Parameters
----------
old_events : ndarray (dtype='int')
N x 3 matrix with start, peak, end samples
dat : ndarray (dtype='float')
vector with the data after detection-transformation (to compute peak)
Returns
-------
ndarray (dtype='int')
vector of indices of the events to keep
ndarray (dtype='int')
N x 3 matrix with start, peak, end samples
Notes
-----
old_events is assumed to be sorted. It only checks for the start time and
end time. When two (or more) events have the same start time and the same
end time, then it takes the largest peak.
There is no tolerance, indices need to be identical.
"""; 20, assignment; 21, assignment; 22, augmented_assignment; 23, assignment; 24, assignment; 25, call; 26, block; 27, assignment; 28, assignment; 29, pattern_list; 30, call; 31, block; 32, expression_list; 33, identifier:diff_events; 34, call; 35, identifier:dupl; 36, subscript; 37, identifier:dupl; 38, integer:1; 39, identifier:n_nondupl_events; 40, binary_operator:old_events.shape[0] - len(dupl); 41, identifier:new_events; 42, call; 43, identifier:len; 44, argument_list; 45, expression_statement; 46, identifier:i; 47, integer:0; 48, identifier:indices; 49, list; 50, identifier:i_old; 51, identifier:one_old_event; 52, identifier:enumerate; 53, argument_list; 54, if_statement; 55, identifier:indices; 56, identifier:new_events; 57, identifier:diff; 58, argument_list; 59, call; 60, integer:0; 61, subscript; 62, call; 63, identifier:zeros; 64, argument_list; 65, identifier:dupl; 66, call; 67, identifier:old_events; 68, comparison_operator:i_old not in dupl; 69, block; 70, else_clause; 71, identifier:old_events; 72, keyword_argument; 73, identifier:where; 74, argument_list; 75, attribute; 76, integer:0; 77, identifier:len; 78, argument_list; 79, tuple; 80, keyword_argument; 81, attribute; 82, argument_list; 83, identifier:i_old; 84, identifier:dupl; 85, expression_statement; 86, expression_statement; 87, expression_statement; 88, block; 89, identifier:axis; 90, integer:0; 91, binary_operator:(diff_events[:, 0] == 0) & (diff_events[:, 2] == 0); 92, identifier:old_events; 93, identifier:shape; 94, identifier:dupl; 95, identifier:n_nondupl_events; 96, subscript; 97, identifier:dtype; 98, string; 99, identifier:lg; 100, identifier:debug; 101, binary_operator:'Removing ' + str(len(dupl)) + ' duplicate events'; 102, assignment; 103, augmented_assignment; 104, call; 105, expression_statement; 106, expression_statement; 107, if_statement; 108, parenthesized_expression; 109, parenthesized_expression; 110, attribute; 111, integer:1; 112, string_content:int; 113, binary_operator:'Removing ' + str(len(dupl)); 114, string; 115, subscript; 116, identifier:one_old_event; 117, identifier:i; 118, integer:1; 119, attribute; 120, argument_list; 121, assignment; 122, assignment; 123, comparison_operator:dat[peak_0] >= dat[peak_1]; 124, block; 125, else_clause; 126, comparison_operator:diff_events[:, 0] == 0; 127, comparison_operator:diff_events[:, 2] == 0; 128, identifier:old_events; 129, identifier:shape; 130, string; 131, call; 132, string_content:duplicate events; 133, identifier:new_events; 134, identifier:i; 135, slice; 136, identifier:indices; 137, identifier:append; 138, identifier:i_old; 139, identifier:peak_0; 140, subscript; 141, identifier:peak_1; 142, subscript; 143, subscript; 144, subscript; 145, expression_statement; 146, block; 147, subscript; 148, integer:0; 149, subscript; 150, integer:0; 151, string_content:Removing; 152, identifier:str; 153, argument_list; 154, identifier:new_events; 155, binary_operator:i - 1; 156, integer:1; 157, identifier:one_old_event; 158, integer:1; 159, identifier:dat; 160, identifier:peak_0; 161, identifier:dat; 162, identifier:peak_1; 163, assignment; 164, expression_statement; 165, identifier:diff_events; 166, slice; 167, integer:0; 168, identifier:diff_events; 169, slice; 170, integer:2; 171, call; 172, identifier:i; 173, integer:1; 174, subscript; 175, identifier:peak_0; 176, assignment; 177, identifier:len; 178, argument_list; 179, identifier:new_events; 180, binary_operator:i - 1; 181, integer:1; 182, subscript; 183, identifier:peak_1; 184, identifier:dupl; 185, identifier:i; 186, integer:1; 187, identifier:new_events; 188, binary_operator:i - 1; 189, integer:1; 190, identifier:i; 191, integer:1 | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 7, 19; 8, 20; 9, 21; 10, 22; 12, 23; 13, 24; 14, 25; 14, 26; 15, 27; 16, 28; 17, 29; 17, 30; 17, 31; 18, 32; 20, 33; 20, 34; 21, 35; 21, 36; 22, 37; 22, 38; 23, 39; 23, 40; 24, 41; 24, 42; 25, 43; 25, 44; 26, 45; 27, 46; 27, 47; 28, 48; 28, 49; 29, 50; 29, 51; 30, 52; 30, 53; 31, 54; 32, 55; 32, 56; 34, 57; 34, 58; 36, 59; 36, 60; 40, 61; 40, 62; 42, 63; 42, 64; 44, 65; 45, 66; 53, 67; 54, 68; 54, 69; 54, 70; 58, 71; 58, 72; 59, 73; 59, 74; 61, 75; 61, 76; 62, 77; 62, 78; 64, 79; 64, 80; 66, 81; 66, 82; 68, 83; 68, 84; 69, 85; 69, 86; 69, 87; 70, 88; 72, 89; 72, 90; 74, 91; 75, 92; 75, 93; 78, 94; 79, 95; 79, 96; 80, 97; 80, 98; 81, 99; 81, 100; 82, 101; 85, 102; 86, 103; 87, 104; 88, 105; 88, 106; 88, 107; 91, 108; 91, 109; 96, 110; 96, 111; 98, 112; 101, 113; 101, 114; 102, 115; 102, 116; 103, 117; 103, 118; 104, 119; 104, 120; 105, 121; 106, 122; 107, 123; 107, 124; 107, 125; 108, 126; 109, 127; 110, 128; 110, 129; 113, 130; 113, 131; 114, 132; 115, 133; 115, 134; 115, 135; 119, 136; 119, 137; 120, 138; 121, 139; 121, 140; 122, 141; 122, 142; 123, 143; 123, 144; 124, 145; 125, 146; 126, 147; 126, 148; 127, 149; 127, 150; 130, 151; 131, 152; 131, 153; 140, 154; 140, 155; 140, 156; 142, 157; 142, 158; 143, 159; 143, 160; 144, 161; 144, 162; 145, 163; 146, 164; 147, 165; 147, 166; 147, 167; 149, 168; 149, 169; 149, 170; 153, 171; 155, 172; 155, 173; 163, 174; 163, 175; 164, 176; 171, 177; 171, 178; 174, 179; 174, 180; 174, 181; 176, 182; 176, 183; 178, 184; 180, 185; 180, 186; 182, 187; 182, 188; 182, 189; 188, 190; 188, 191 | def _remove_duplicate(old_events, dat):
"""Remove duplicates from the events.
Parameters
----------
old_events : ndarray (dtype='int')
N x 3 matrix with start, peak, end samples
dat : ndarray (dtype='float')
vector with the data after detection-transformation (to compute peak)
Returns
-------
ndarray (dtype='int')
vector of indices of the events to keep
ndarray (dtype='int')
N x 3 matrix with start, peak, end samples
Notes
-----
old_events is assumed to be sorted. It only checks for the start time and
end time. When two (or more) events have the same start time and the same
end time, then it takes the largest peak.
There is no tolerance, indices need to be identical.
"""
diff_events = diff(old_events, axis=0)
dupl = where((diff_events[:, 0] == 0) & (diff_events[:, 2] == 0))[0]
dupl += 1 # more convenient, it copies old_event first and then compares
n_nondupl_events = old_events.shape[0] - len(dupl)
new_events = zeros((n_nondupl_events, old_events.shape[1]), dtype='int')
if len(dupl):
lg.debug('Removing ' + str(len(dupl)) + ' duplicate events')
i = 0
indices = []
for i_old, one_old_event in enumerate(old_events):
if i_old not in dupl:
new_events[i, :] = one_old_event
i += 1
indices.append(i_old)
else:
peak_0 = new_events[i - 1, 1]
peak_1 = one_old_event[1]
if dat[peak_0] >= dat[peak_1]:
new_events[i - 1, 1] = peak_0
else:
new_events[i - 1, 1] = peak_1
return indices, new_events |
0, module; 1, function_definition; 2, function_name:get_app_list; 3, parameters; 4, block; 5, identifier:site; 6, identifier:request; 7, expression_statement; 8, expression_statement; 9, comment:# Sort the apps alphabetically.; 10, expression_statement; 11, comment:# Sort the models alphabetically within each app.; 12, for_statement; 13, return_statement; 14, comment:"""
Returns a sorted list of all the installed apps that have been
registered in this site.
"""; 15, assignment; 16, assignment; 17, identifier:app; 18, identifier:app_list; 19, block; 20, identifier:app_list; 21, identifier:app_dict; 22, call; 23, identifier:app_list; 24, call; 25, expression_statement; 26, identifier:_build_app_dict; 27, argument_list; 28, identifier:sorted; 29, argument_list; 30, call; 31, identifier:site; 32, identifier:request; 33, call; 34, keyword_argument; 35, attribute; 36, argument_list; 37, attribute; 38, argument_list; 39, identifier:key; 40, lambda; 41, subscript; 42, identifier:sort; 43, keyword_argument; 44, identifier:app_dict; 45, identifier:values; 46, lambda_parameters; 47, call; 48, identifier:app; 49, string; 50, identifier:key; 51, lambda; 52, identifier:x; 53, attribute; 54, argument_list; 55, string_content:models; 56, lambda_parameters; 57, subscript; 58, subscript; 59, identifier:lower; 60, identifier:x; 61, identifier:x; 62, string; 63, identifier:x; 64, string; 65, string_content:name; 66, string_content:name | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 7, 14; 8, 15; 10, 16; 12, 17; 12, 18; 12, 19; 13, 20; 15, 21; 15, 22; 16, 23; 16, 24; 19, 25; 22, 26; 22, 27; 24, 28; 24, 29; 25, 30; 27, 31; 27, 32; 29, 33; 29, 34; 30, 35; 30, 36; 33, 37; 33, 38; 34, 39; 34, 40; 35, 41; 35, 42; 36, 43; 37, 44; 37, 45; 40, 46; 40, 47; 41, 48; 41, 49; 43, 50; 43, 51; 46, 52; 47, 53; 47, 54; 49, 55; 51, 56; 51, 57; 53, 58; 53, 59; 56, 60; 57, 61; 57, 62; 58, 63; 58, 64; 62, 65; 64, 66 | def get_app_list(site, request):
"""
Returns a sorted list of all the installed apps that have been
registered in this site.
"""
app_dict = _build_app_dict(site, request)
# Sort the apps alphabetically.
app_list = sorted(app_dict.values(), key=lambda x: x['name'].lower())
# Sort the models alphabetically within each app.
for app in app_list:
app['models'].sort(key=lambda x: x['name'])
return app_list |
0, module; 1, function_definition; 2, function_name:sort_dependencies; 3, parameters; 4, block; 5, identifier:self; 6, identifier:image; 7, default_parameter; 8, expression_statement; 9, if_statement; 10, if_statement; 11, expression_statement; 12, for_statement; 13, expression_statement; 14, return_statement; 15, identifier:dependencies; 16, None; 17, comment:"""
Topologically sort the docker commands by their requirements
Note:
Circular "requires" dependencies are assumed to have already been checked in
get_external_base_image, they are not checked here
Args:
image (str): process this docker image's dependencies
dependencies (OrderedDict): running cache of sorted dependencies (ordered dict)
Returns:
List[str]: list of dependencies a topologically-sorted build order
"""; 18, comparison_operator:dependencies is None; 19, block; 20, comparison_operator:image in dependencies; 21, block; 22, assignment; 23, identifier:dep; 24, identifier:requires; 25, block; 26, assignment; 27, call; 28, identifier:dependencies; 29, None; 30, expression_statement; 31, comment:# using this as an ordered set - not storing any values; 32, identifier:image; 33, identifier:dependencies; 34, return_statement; 35, identifier:requires; 36, call; 37, expression_statement; 38, subscript; 39, None; 40, attribute; 41, argument_list; 42, assignment; 43, attribute; 44, argument_list; 45, call; 46, identifier:dependencies; 47, identifier:image; 48, identifier:dependencies; 49, identifier:keys; 50, identifier:dependencies; 51, call; 52, subscript; 53, identifier:get; 54, string; 55, list; 56, attribute; 57, argument_list; 58, identifier:OrderedDict; 59, argument_list; 60, attribute; 61, identifier:image; 62, string_content:requires; 63, identifier:self; 64, identifier:sort_dependencies; 65, identifier:dep; 66, identifier:dependencies; 67, identifier:self; 68, identifier:ymldefs | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 7, 15; 7, 16; 8, 17; 9, 18; 9, 19; 10, 20; 10, 21; 11, 22; 12, 23; 12, 24; 12, 25; 13, 26; 14, 27; 18, 28; 18, 29; 19, 30; 19, 31; 20, 32; 20, 33; 21, 34; 22, 35; 22, 36; 25, 37; 26, 38; 26, 39; 27, 40; 27, 41; 30, 42; 36, 43; 36, 44; 37, 45; 38, 46; 38, 47; 40, 48; 40, 49; 42, 50; 42, 51; 43, 52; 43, 53; 44, 54; 44, 55; 45, 56; 45, 57; 51, 58; 51, 59; 52, 60; 52, 61; 54, 62; 56, 63; 56, 64; 57, 65; 57, 66; 60, 67; 60, 68 | def sort_dependencies(self, image, dependencies=None):
"""
Topologically sort the docker commands by their requirements
Note:
Circular "requires" dependencies are assumed to have already been checked in
get_external_base_image, they are not checked here
Args:
image (str): process this docker image's dependencies
dependencies (OrderedDict): running cache of sorted dependencies (ordered dict)
Returns:
List[str]: list of dependencies a topologically-sorted build order
"""
if dependencies is None:
dependencies = OrderedDict() # using this as an ordered set - not storing any values
if image in dependencies:
return
requires = self.ymldefs[image].get('requires', [])
for dep in requires:
self.sort_dependencies(dep, dependencies)
dependencies[image] = None
return dependencies.keys() |
0, module; 1, function_definition; 2, function_name:makeOfficialGlyphOrder; 3, parameters; 4, block; 5, identifier:font; 6, default_parameter; 7, expression_statement; 8, if_statement; 9, expression_statement; 10, expression_statement; 11, if_statement; 12, for_statement; 13, expression_statement; 14, return_statement; 15, identifier:glyphOrder; 16, None; 17, comment:""" Make the final glyph order for 'font'.
If glyphOrder is None, try getting the font.glyphOrder list.
If not explicit glyphOrder is defined, sort glyphs alphabetically.
If ".notdef" glyph is present in the font, force this to always be
the first glyph (at index 0).
"""; 18, comparison_operator:glyphOrder is None; 19, block; 20, assignment; 21, assignment; 22, comparison_operator:".notdef" in names; 23, block; 24, identifier:name; 25, identifier:glyphOrder; 26, block; 27, call; 28, identifier:order; 29, identifier:glyphOrder; 30, None; 31, expression_statement; 32, identifier:names; 33, call; 34, identifier:order; 35, list; 36, string:".notdef"; 37, identifier:names; 38, expression_statement; 39, expression_statement; 40, if_statement; 41, expression_statement; 42, expression_statement; 43, attribute; 44, argument_list; 45, assignment; 46, identifier:set; 47, argument_list; 48, call; 49, call; 50, comparison_operator:name not in names; 51, block; 52, call; 53, call; 54, identifier:order; 55, identifier:extend; 56, call; 57, identifier:glyphOrder; 58, call; 59, call; 60, attribute; 61, argument_list; 62, attribute; 63, argument_list; 64, identifier:name; 65, identifier:names; 66, continue_statement; 67, attribute; 68, argument_list; 69, attribute; 70, argument_list; 71, identifier:sorted; 72, argument_list; 73, identifier:getattr; 74, argument_list; 75, attribute; 76, argument_list; 77, identifier:names; 78, identifier:remove; 79, string:".notdef"; 80, identifier:order; 81, identifier:append; 82, string:".notdef"; 83, identifier:names; 84, identifier:remove; 85, identifier:name; 86, identifier:order; 87, identifier:append; 88, identifier:name; 89, identifier:names; 90, identifier:font; 91, string:"glyphOrder"; 92, tuple; 93, identifier:font; 94, identifier:keys | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 6, 15; 6, 16; 7, 17; 8, 18; 8, 19; 9, 20; 10, 21; 11, 22; 11, 23; 12, 24; 12, 25; 12, 26; 13, 27; 14, 28; 18, 29; 18, 30; 19, 31; 20, 32; 20, 33; 21, 34; 21, 35; 22, 36; 22, 37; 23, 38; 23, 39; 26, 40; 26, 41; 26, 42; 27, 43; 27, 44; 31, 45; 33, 46; 33, 47; 38, 48; 39, 49; 40, 50; 40, 51; 41, 52; 42, 53; 43, 54; 43, 55; 44, 56; 45, 57; 45, 58; 47, 59; 48, 60; 48, 61; 49, 62; 49, 63; 50, 64; 50, 65; 51, 66; 52, 67; 52, 68; 53, 69; 53, 70; 56, 71; 56, 72; 58, 73; 58, 74; 59, 75; 59, 76; 60, 77; 60, 78; 61, 79; 62, 80; 62, 81; 63, 82; 67, 83; 67, 84; 68, 85; 69, 86; 69, 87; 70, 88; 72, 89; 74, 90; 74, 91; 74, 92; 75, 93; 75, 94 | def makeOfficialGlyphOrder(font, glyphOrder=None):
""" Make the final glyph order for 'font'.
If glyphOrder is None, try getting the font.glyphOrder list.
If not explicit glyphOrder is defined, sort glyphs alphabetically.
If ".notdef" glyph is present in the font, force this to always be
the first glyph (at index 0).
"""
if glyphOrder is None:
glyphOrder = getattr(font, "glyphOrder", ())
names = set(font.keys())
order = []
if ".notdef" in names:
names.remove(".notdef")
order.append(".notdef")
for name in glyphOrder:
if name not in names:
continue
names.remove(name)
order.append(name)
order.extend(sorted(names))
return order |
0, module; 1, function_definition; 2, function_name:sorted; 3, parameters; 4, block; 5, identifier:cls; 6, identifier:items; 7, identifier:orders; 8, expression_statement; 9, return_statement; 10, string; 11, call; 12, string_content:Returns the elements in `items` sorted according to `orders`; 13, identifier:sorted; 14, argument_list; 15, identifier:items; 16, keyword_argument; 17, identifier:cmp; 18, call; 19, attribute; 20, argument_list; 21, identifier:cls; 22, identifier:multipleOrderComparison; 23, identifier:orders | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 8, 10; 9, 11; 10, 12; 11, 13; 11, 14; 14, 15; 14, 16; 16, 17; 16, 18; 18, 19; 18, 20; 19, 21; 19, 22; 20, 23 | def sorted(cls, items, orders):
'''Returns the elements in `items` sorted according to `orders`'''
return sorted(items, cmp=cls.multipleOrderComparison(orders)) |
0, module; 1, function_definition; 2, function_name:get_unique_fields; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, for_statement; 8, return_statement; 9, comment:"""List field names that are unique_together with `sort_order`."""; 10, identifier:unique_together; 11, attribute; 12, block; 13, list; 14, attribute; 15, identifier:unique_together; 16, if_statement; 17, identifier:self; 18, identifier:_meta; 19, comparison_operator:'sort_order' in unique_together; 20, block; 21, string; 22, identifier:unique_together; 23, expression_statement; 24, expression_statement; 25, return_statement; 26, string_content:sort_order; 27, assignment; 28, call; 29, list_comprehension; 30, identifier:unique_fields; 31, call; 32, attribute; 33, argument_list; 34, binary_operator:'%s_id' % f; 35, for_in_clause; 36, identifier:list; 37, argument_list; 38, identifier:unique_fields; 39, identifier:remove; 40, string; 41, string; 42, identifier:f; 43, identifier:f; 44, identifier:unique_fields; 45, identifier:unique_together; 46, string_content:sort_order; 47, string_content:%s_id | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 6, 9; 7, 10; 7, 11; 7, 12; 8, 13; 11, 14; 11, 15; 12, 16; 14, 17; 14, 18; 16, 19; 16, 20; 19, 21; 19, 22; 20, 23; 20, 24; 20, 25; 21, 26; 23, 27; 24, 28; 25, 29; 27, 30; 27, 31; 28, 32; 28, 33; 29, 34; 29, 35; 31, 36; 31, 37; 32, 38; 32, 39; 33, 40; 34, 41; 34, 42; 35, 43; 35, 44; 37, 45; 40, 46; 41, 47 | def get_unique_fields(self):
"""List field names that are unique_together with `sort_order`."""
for unique_together in self._meta.unique_together:
if 'sort_order' in unique_together:
unique_fields = list(unique_together)
unique_fields.remove('sort_order')
return ['%s_id' % f for f in unique_fields]
return [] |
0, module; 1, function_definition; 2, function_name:_is_sort_order_unique_together_with_something; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, for_statement; 9, return_statement; 10, comment:"""
Is the sort_order field unique_together with something
"""; 11, assignment; 12, identifier:fields; 13, identifier:unique_together; 14, block; 15, False; 16, identifier:unique_together; 17, attribute; 18, if_statement; 19, attribute; 20, identifier:unique_together; 21, boolean_operator; 22, block; 23, identifier:self; 24, identifier:_meta; 25, comparison_operator:'sort_order' in fields; 26, comparison_operator:len(fields) > 1; 27, return_statement; 28, string; 29, identifier:fields; 30, call; 31, integer:1; 32, True; 33, string_content:sort_order; 34, identifier:len; 35, argument_list; 36, identifier:fields | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 6, 10; 7, 11; 8, 12; 8, 13; 8, 14; 9, 15; 11, 16; 11, 17; 14, 18; 17, 19; 17, 20; 18, 21; 18, 22; 19, 23; 19, 24; 21, 25; 21, 26; 22, 27; 25, 28; 25, 29; 26, 30; 26, 31; 27, 32; 28, 33; 30, 34; 30, 35; 35, 36 | def _is_sort_order_unique_together_with_something(self):
"""
Is the sort_order field unique_together with something
"""
unique_together = self._meta.unique_together
for fields in unique_together:
if 'sort_order' in fields and len(fields) > 1:
return True
return False |
0, module; 1, function_definition; 2, function_name:_update; 3, parameters; 4, block; 5, identifier:qs; 6, expression_statement; 7, try_statement; 8, comment:"""
Increment the sort_order in a queryset.
Handle IntegrityErrors caused by unique constraints.
"""; 9, block; 10, except_clause; 11, with_statement; 12, identifier:IntegrityError; 13, block; 14, with_clause; 15, block; 16, for_statement; 17, with_item; 18, expression_statement; 19, identifier:obj; 20, call; 21, block; 22, call; 23, call; 24, attribute; 25, argument_list; 26, expression_statement; 27, attribute; 28, argument_list; 29, attribute; 30, argument_list; 31, identifier:qs; 32, identifier:order_by; 33, string; 34, call; 35, identifier:transaction; 36, identifier:atomic; 37, identifier:qs; 38, identifier:update; 39, keyword_argument; 40, string_content:-sort_order; 41, attribute; 42, argument_list; 43, identifier:sort_order; 44, binary_operator:models.F('sort_order') + 1; 45, call; 46, identifier:update; 47, keyword_argument; 48, call; 49, integer:1; 50, attribute; 51, argument_list; 52, identifier:sort_order; 53, binary_operator:models.F('sort_order') + 1; 54, attribute; 55, argument_list; 56, identifier:qs; 57, identifier:filter; 58, keyword_argument; 59, call; 60, integer:1; 61, identifier:models; 62, identifier:F; 63, string; 64, identifier:pk; 65, attribute; 66, attribute; 67, argument_list; 68, string_content:sort_order; 69, identifier:obj; 70, identifier:pk; 71, identifier:models; 72, identifier:F; 73, string; 74, string_content:sort_order | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 6, 8; 7, 9; 7, 10; 9, 11; 10, 12; 10, 13; 11, 14; 11, 15; 13, 16; 14, 17; 15, 18; 16, 19; 16, 20; 16, 21; 17, 22; 18, 23; 20, 24; 20, 25; 21, 26; 22, 27; 22, 28; 23, 29; 23, 30; 24, 31; 24, 32; 25, 33; 26, 34; 27, 35; 27, 36; 29, 37; 29, 38; 30, 39; 33, 40; 34, 41; 34, 42; 39, 43; 39, 44; 41, 45; 41, 46; 42, 47; 44, 48; 44, 49; 45, 50; 45, 51; 47, 52; 47, 53; 48, 54; 48, 55; 50, 56; 50, 57; 51, 58; 53, 59; 53, 60; 54, 61; 54, 62; 55, 63; 58, 64; 58, 65; 59, 66; 59, 67; 63, 68; 65, 69; 65, 70; 66, 71; 66, 72; 67, 73; 73, 74 | def _update(qs):
"""
Increment the sort_order in a queryset.
Handle IntegrityErrors caused by unique constraints.
"""
try:
with transaction.atomic():
qs.update(sort_order=models.F('sort_order') + 1)
except IntegrityError:
for obj in qs.order_by('-sort_order'):
qs.filter(pk=obj.pk).update(sort_order=models.F('sort_order') + 1) |
0, module; 1, function_definition; 2, function_name:set_orders; 3, parameters; 4, block; 5, identifier:self; 6, identifier:object_pks; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, comment:# Call list() on the values right away, so they don't get affected by the; 11, comment:# update() later (since values_list() is lazy).; 12, expression_statement; 13, comment:# Check there are no unrecognised entries in the object_pks list. If so,; 14, comment:# throw an error. We only have to check that they're the same length because; 15, comment:# orders is built using only entries in object_pks, and all the pks are unique,; 16, comment:# so if their lengths are the same, the elements must match up exactly.; 17, if_statement; 18, with_statement; 19, comment:# Return the operated-on queryset for convenience.; 20, return_statement; 21, comment:"""
Perform a mass update of sort_orders across the full queryset.
Accepts a list, object_pks, of the intended order for the objects.
Works as follows:
- Compile a list of all sort orders in the queryset. Leave out anything that
isn't in the object_pks list - this deals with pagination and any
inconsistencies.
- Get the maximum among all model object sort orders. Update the queryset to add
it to all the existing sort order values. This lifts them 'out of the way' of
unique_together clashes when setting the intended sort orders.
- Set the sort order on each object. Use only sort_order values that the objects
had before calling this method, so they get rearranged in place.
Performs O(n) queries.
"""; 22, assignment; 23, assignment; 24, assignment; 25, comparison_operator:len(orders) != len(object_pks); 26, block; 27, with_clause; 28, block; 29, identifier:objects_to_sort; 30, identifier:objects_to_sort; 31, call; 32, identifier:max_value; 33, subscript; 34, identifier:orders; 35, call; 36, call; 37, call; 38, expression_statement; 39, expression_statement; 40, raise_statement; 41, with_item; 42, expression_statement; 43, for_statement; 44, attribute; 45, argument_list; 46, call; 47, string; 48, identifier:list; 49, argument_list; 50, identifier:len; 51, argument_list; 52, identifier:len; 53, argument_list; 54, assignment; 55, assignment; 56, call; 57, call; 58, call; 59, pattern_list; 60, call; 61, comment:# Use update() to save a query per item and dodge the insertion sort; 62, comment:# code in save().; 63, block; 64, identifier:self; 65, identifier:filter; 66, keyword_argument; 67, attribute; 68, argument_list; 69, string_content:sort_order__max; 70, call; 71, identifier:orders; 72, identifier:object_pks; 73, identifier:pks; 74, call; 75, identifier:message; 76, call; 77, identifier:TypeError; 78, argument_list; 79, attribute; 80, argument_list; 81, attribute; 82, argument_list; 83, identifier:pk; 84, identifier:order; 85, identifier:zip; 86, argument_list; 87, expression_statement; 88, identifier:pk__in; 89, identifier:object_pks; 90, call; 91, identifier:aggregate; 92, call; 93, attribute; 94, argument_list; 95, identifier:set; 96, argument_list; 97, attribute; 98, argument_list; 99, identifier:message; 100, identifier:transaction; 101, identifier:atomic; 102, identifier:objects_to_sort; 103, identifier:update; 104, keyword_argument; 105, identifier:object_pks; 106, identifier:orders; 107, call; 108, attribute; 109, argument_list; 110, attribute; 111, argument_list; 112, identifier:objects_to_sort; 113, identifier:values_list; 114, string; 115, keyword_argument; 116, call; 117, string; 118, identifier:format; 119, list_comprehension; 120, identifier:sort_order; 121, binary_operator:models.F('sort_order') + max_value; 122, attribute; 123, argument_list; 124, attribute; 125, identifier:all; 126, identifier:models; 127, identifier:Max; 128, string; 129, string_content:sort_order; 130, identifier:flat; 131, True; 132, attribute; 133, argument_list; 134, string_content:The following object_pks are not in this queryset: {}; 135, identifier:pk; 136, for_in_clause; 137, if_clause; 138, call; 139, identifier:max_value; 140, call; 141, identifier:update; 142, keyword_argument; 143, attribute; 144, identifier:objects; 145, string_content:sort_order; 146, identifier:objects_to_sort; 147, identifier:values_list; 148, string; 149, keyword_argument; 150, identifier:pk; 151, identifier:object_pks; 152, comparison_operator:pk not in pks; 153, attribute; 154, argument_list; 155, attribute; 156, argument_list; 157, identifier:sort_order; 158, identifier:order; 159, identifier:self; 160, identifier:model; 161, string_content:pk; 162, identifier:flat; 163, True; 164, identifier:pk; 165, identifier:pks; 166, identifier:models; 167, identifier:F; 168, string; 169, identifier:self; 170, identifier:filter; 171, keyword_argument; 172, string_content:sort_order; 173, identifier:pk; 174, identifier:pk | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 7, 21; 8, 22; 9, 23; 12, 24; 17, 25; 17, 26; 18, 27; 18, 28; 20, 29; 22, 30; 22, 31; 23, 32; 23, 33; 24, 34; 24, 35; 25, 36; 25, 37; 26, 38; 26, 39; 26, 40; 27, 41; 28, 42; 28, 43; 31, 44; 31, 45; 33, 46; 33, 47; 35, 48; 35, 49; 36, 50; 36, 51; 37, 52; 37, 53; 38, 54; 39, 55; 40, 56; 41, 57; 42, 58; 43, 59; 43, 60; 43, 61; 43, 62; 43, 63; 44, 64; 44, 65; 45, 66; 46, 67; 46, 68; 47, 69; 49, 70; 51, 71; 53, 72; 54, 73; 54, 74; 55, 75; 55, 76; 56, 77; 56, 78; 57, 79; 57, 80; 58, 81; 58, 82; 59, 83; 59, 84; 60, 85; 60, 86; 63, 87; 66, 88; 66, 89; 67, 90; 67, 91; 68, 92; 70, 93; 70, 94; 74, 95; 74, 96; 76, 97; 76, 98; 78, 99; 79, 100; 79, 101; 81, 102; 81, 103; 82, 104; 86, 105; 86, 106; 87, 107; 90, 108; 90, 109; 92, 110; 92, 111; 93, 112; 93, 113; 94, 114; 94, 115; 96, 116; 97, 117; 97, 118; 98, 119; 104, 120; 104, 121; 107, 122; 107, 123; 108, 124; 108, 125; 110, 126; 110, 127; 111, 128; 114, 129; 115, 130; 115, 131; 116, 132; 116, 133; 117, 134; 119, 135; 119, 136; 119, 137; 121, 138; 121, 139; 122, 140; 122, 141; 123, 142; 124, 143; 124, 144; 128, 145; 132, 146; 132, 147; 133, 148; 133, 149; 136, 150; 136, 151; 137, 152; 138, 153; 138, 154; 140, 155; 140, 156; 142, 157; 142, 158; 143, 159; 143, 160; 148, 161; 149, 162; 149, 163; 152, 164; 152, 165; 153, 166; 153, 167; 154, 168; 155, 169; 155, 170; 156, 171; 168, 172; 171, 173; 171, 174 | def set_orders(self, object_pks):
"""
Perform a mass update of sort_orders across the full queryset.
Accepts a list, object_pks, of the intended order for the objects.
Works as follows:
- Compile a list of all sort orders in the queryset. Leave out anything that
isn't in the object_pks list - this deals with pagination and any
inconsistencies.
- Get the maximum among all model object sort orders. Update the queryset to add
it to all the existing sort order values. This lifts them 'out of the way' of
unique_together clashes when setting the intended sort orders.
- Set the sort order on each object. Use only sort_order values that the objects
had before calling this method, so they get rearranged in place.
Performs O(n) queries.
"""
objects_to_sort = self.filter(pk__in=object_pks)
max_value = self.model.objects.all().aggregate(
models.Max('sort_order')
)['sort_order__max']
# Call list() on the values right away, so they don't get affected by the
# update() later (since values_list() is lazy).
orders = list(objects_to_sort.values_list('sort_order', flat=True))
# Check there are no unrecognised entries in the object_pks list. If so,
# throw an error. We only have to check that they're the same length because
# orders is built using only entries in object_pks, and all the pks are unique,
# so if their lengths are the same, the elements must match up exactly.
if len(orders) != len(object_pks):
pks = set(objects_to_sort.values_list('pk', flat=True))
message = 'The following object_pks are not in this queryset: {}'.format(
[pk for pk in object_pks if pk not in pks]
)
raise TypeError(message)
with transaction.atomic():
objects_to_sort.update(sort_order=models.F('sort_order') + max_value)
for pk, order in zip(object_pks, orders):
# Use update() to save a query per item and dodge the insertion sort
# code in save().
self.filter(pk=pk).update(sort_order=order)
# Return the operated-on queryset for convenience.
return objects_to_sort |
0, module; 1, function_definition; 2, function_name:set_sort_cb; 3, parameters; 4, block; 5, identifier:self; 6, identifier:w; 7, identifier:index; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, comment:"""This callback is invoked when the user selects a new sort order
from the preferences pane."""; 12, assignment; 13, call; 14, identifier:name; 15, subscript; 16, attribute; 17, argument_list; 18, attribute; 19, identifier:index; 20, attribute; 21, identifier:set; 22, keyword_argument; 23, identifier:self; 24, identifier:sort_options; 25, identifier:self; 26, identifier:t_; 27, identifier:sort_order; 28, identifier:name | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 8, 11; 9, 12; 10, 13; 12, 14; 12, 15; 13, 16; 13, 17; 15, 18; 15, 19; 16, 20; 16, 21; 17, 22; 18, 23; 18, 24; 20, 25; 20, 26; 22, 27; 22, 28 | def set_sort_cb(self, w, index):
"""This callback is invoked when the user selects a new sort order
from the preferences pane."""
name = self.sort_options[index]
self.t_.set(sort_order=name) |
0, module; 1, function_definition; 2, function_name:run; 3, parameters; 4, block; 5, identifier:self; 6, list_splat_pattern; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, if_statement; 11, return_statement; 12, identifier:args; 13, comment:"""Get and set configuration parameters.
This command gets or sets parameter values from the user configuration
file. On Linux systems, configuration will be stored in the file
'~/.sortinghat'.
"""; 14, assignment; 15, assignment; 16, comparison_operator:params.action == 'get'; 17, block; 18, elif_clause; 19, else_clause; 20, identifier:code; 21, identifier:params; 22, call; 23, identifier:config_file; 24, call; 25, attribute; 26, string; 27, expression_statement; 28, comparison_operator:params.action == 'set'; 29, block; 30, block; 31, attribute; 32, argument_list; 33, attribute; 34, argument_list; 35, identifier:params; 36, identifier:action; 37, string_content:get; 38, assignment; 39, attribute; 40, string; 41, expression_statement; 42, raise_statement; 43, attribute; 44, identifier:parse_args; 45, identifier:args; 46, attribute; 47, identifier:expanduser; 48, string; 49, identifier:code; 50, call; 51, identifier:params; 52, identifier:action; 53, string_content:set; 54, assignment; 55, call; 56, identifier:self; 57, identifier:parser; 58, identifier:os; 59, identifier:path; 60, string_content:~/.sortinghat; 61, attribute; 62, argument_list; 63, identifier:code; 64, call; 65, identifier:RuntimeError; 66, argument_list; 67, identifier:self; 68, identifier:get; 69, attribute; 70, identifier:config_file; 71, attribute; 72, argument_list; 73, string:"Not get or set action given"; 74, identifier:params; 75, identifier:parameter; 76, identifier:self; 77, identifier:set; 78, attribute; 79, attribute; 80, identifier:config_file; 81, identifier:params; 82, identifier:parameter; 83, identifier:params; 84, identifier:value | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 6, 12; 7, 13; 8, 14; 9, 15; 10, 16; 10, 17; 10, 18; 10, 19; 11, 20; 14, 21; 14, 22; 15, 23; 15, 24; 16, 25; 16, 26; 17, 27; 18, 28; 18, 29; 19, 30; 22, 31; 22, 32; 24, 33; 24, 34; 25, 35; 25, 36; 26, 37; 27, 38; 28, 39; 28, 40; 29, 41; 30, 42; 31, 43; 31, 44; 32, 45; 33, 46; 33, 47; 34, 48; 38, 49; 38, 50; 39, 51; 39, 52; 40, 53; 41, 54; 42, 55; 43, 56; 43, 57; 46, 58; 46, 59; 48, 60; 50, 61; 50, 62; 54, 63; 54, 64; 55, 65; 55, 66; 61, 67; 61, 68; 62, 69; 62, 70; 64, 71; 64, 72; 66, 73; 69, 74; 69, 75; 71, 76; 71, 77; 72, 78; 72, 79; 72, 80; 78, 81; 78, 82; 79, 83; 79, 84 | def run(self, *args):
"""Get and set configuration parameters.
This command gets or sets parameter values from the user configuration
file. On Linux systems, configuration will be stored in the file
'~/.sortinghat'.
"""
params = self.parser.parse_args(args)
config_file = os.path.expanduser('~/.sortinghat')
if params.action == 'get':
code = self.get(params.parameter, config_file)
elif params.action == 'set':
code = self.set(params.parameter, params.value, config_file)
else:
raise RuntimeError("Not get or set action given")
return code |
0, module; 1, function_definition; 2, function_name:export; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, for_statement; 11, expression_statement; 12, expression_statement; 13, return_statement; 14, identifier:source; 15, None; 16, comment:"""Export a set of unique identities.
Method to export unique identities from the registry. Identities schema
will follow Sorting Hat JSON format.
When source parameter is given, only those unique identities which have
one or more identities from the given source will be exported.
:param source: source of the identities to export
:returns: a JSON formatted str
"""; 17, assignment; 18, assignment; 19, identifier:uid; 20, identifier:uids; 21, block; 22, assignment; 23, assignment; 24, call; 25, identifier:uidentities; 26, dictionary; 27, identifier:uids; 28, call; 29, expression_statement; 30, expression_statement; 31, expression_statement; 32, expression_statement; 33, expression_statement; 34, identifier:blacklist; 35, list_comprehension; 36, identifier:obj; 37, dictionary; 38, attribute; 39, argument_list; 40, attribute; 41, argument_list; 42, assignment; 43, assignment; 44, call; 45, assignment; 46, assignment; 47, attribute; 48, for_in_clause; 49, pair; 50, pair; 51, pair; 52, pair; 53, pair; 54, identifier:json; 55, identifier:dumps; 56, identifier:obj; 57, keyword_argument; 58, keyword_argument; 59, keyword_argument; 60, keyword_argument; 61, identifier:api; 62, identifier:unique_identities; 63, attribute; 64, keyword_argument; 65, identifier:enrollments; 66, list_comprehension; 67, identifier:u; 68, call; 69, attribute; 70, argument_list; 71, subscript; 72, identifier:u; 73, subscript; 74, identifier:enrollments; 75, identifier:mb; 76, identifier:excluded; 77, identifier:mb; 78, call; 79, string; 80, call; 81, string; 82, identifier:source; 83, string; 84, identifier:blacklist; 85, string; 86, dictionary; 87, string; 88, identifier:uidentities; 89, identifier:default; 90, attribute; 91, identifier:indent; 92, integer:4; 93, identifier:separators; 94, tuple; 95, identifier:sort_keys; 96, True; 97, identifier:self; 98, identifier:db; 99, identifier:source; 100, identifier:source; 101, call; 102, for_in_clause; 103, attribute; 104, argument_list; 105, subscript; 106, identifier:sort; 107, keyword_argument; 108, identifier:uidentities; 109, attribute; 110, subscript; 111, string; 112, attribute; 113, argument_list; 114, string_content:time; 115, identifier:str; 116, argument_list; 117, string_content:source; 118, string_content:blacklist; 119, string_content:organizations; 120, string_content:uidentities; 121, identifier:self; 122, identifier:_json_encoder; 123, string; 124, string; 125, attribute; 126, argument_list; 127, identifier:rol; 128, call; 129, identifier:uid; 130, identifier:to_dict; 131, identifier:u; 132, string; 133, identifier:key; 134, lambda; 135, identifier:uid; 136, identifier:uuid; 137, identifier:uidentities; 138, attribute; 139, string_content:enrollments; 140, identifier:api; 141, identifier:blacklist; 142, attribute; 143, call; 144, string_content:,; 145, string_content::; 146, identifier:rol; 147, identifier:to_dict; 148, attribute; 149, argument_list; 150, string_content:identities; 151, lambda_parameters; 152, subscript; 153, identifier:uid; 154, identifier:uuid; 155, identifier:self; 156, identifier:db; 157, attribute; 158, argument_list; 159, identifier:api; 160, identifier:enrollments; 161, attribute; 162, keyword_argument; 163, identifier:x; 164, identifier:x; 165, string; 166, attribute; 167, identifier:now; 168, identifier:self; 169, identifier:db; 170, identifier:uuid; 171, attribute; 172, string_content:id; 173, identifier:datetime; 174, identifier:datetime; 175, identifier:uid; 176, identifier:uuid | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 6, 14; 6, 15; 7, 16; 8, 17; 9, 18; 10, 19; 10, 20; 10, 21; 11, 22; 12, 23; 13, 24; 17, 25; 17, 26; 18, 27; 18, 28; 21, 29; 21, 30; 21, 31; 21, 32; 21, 33; 22, 34; 22, 35; 23, 36; 23, 37; 24, 38; 24, 39; 28, 40; 28, 41; 29, 42; 30, 43; 31, 44; 32, 45; 33, 46; 35, 47; 35, 48; 37, 49; 37, 50; 37, 51; 37, 52; 37, 53; 38, 54; 38, 55; 39, 56; 39, 57; 39, 58; 39, 59; 39, 60; 40, 61; 40, 62; 41, 63; 41, 64; 42, 65; 42, 66; 43, 67; 43, 68; 44, 69; 44, 70; 45, 71; 45, 72; 46, 73; 46, 74; 47, 75; 47, 76; 48, 77; 48, 78; 49, 79; 49, 80; 50, 81; 50, 82; 51, 83; 51, 84; 52, 85; 52, 86; 53, 87; 53, 88; 57, 89; 57, 90; 58, 91; 58, 92; 59, 93; 59, 94; 60, 95; 60, 96; 63, 97; 63, 98; 64, 99; 64, 100; 66, 101; 66, 102; 68, 103; 68, 104; 69, 105; 69, 106; 70, 107; 71, 108; 71, 109; 73, 110; 73, 111; 78, 112; 78, 113; 79, 114; 80, 115; 80, 116; 81, 117; 83, 118; 85, 119; 87, 120; 90, 121; 90, 122; 94, 123; 94, 124; 101, 125; 101, 126; 102, 127; 102, 128; 103, 129; 103, 130; 105, 131; 105, 132; 107, 133; 107, 134; 109, 135; 109, 136; 110, 137; 110, 138; 111, 139; 112, 140; 112, 141; 113, 142; 116, 143; 123, 144; 124, 145; 125, 146; 125, 147; 128, 148; 128, 149; 132, 150; 134, 151; 134, 152; 138, 153; 138, 154; 142, 155; 142, 156; 143, 157; 143, 158; 148, 159; 148, 160; 149, 161; 149, 162; 151, 163; 152, 164; 152, 165; 157, 166; 157, 167; 161, 168; 161, 169; 162, 170; 162, 171; 165, 172; 166, 173; 166, 174; 171, 175; 171, 176 | def export(self, source=None):
"""Export a set of unique identities.
Method to export unique identities from the registry. Identities schema
will follow Sorting Hat JSON format.
When source parameter is given, only those unique identities which have
one or more identities from the given source will be exported.
:param source: source of the identities to export
:returns: a JSON formatted str
"""
uidentities = {}
uids = api.unique_identities(self.db, source=source)
for uid in uids:
enrollments = [rol.to_dict()
for rol in api.enrollments(self.db, uuid=uid.uuid)]
u = uid.to_dict()
u['identities'].sort(key=lambda x: x['id'])
uidentities[uid.uuid] = u
uidentities[uid.uuid]['enrollments'] = enrollments
blacklist = [mb.excluded for mb in api.blacklist(self.db)]
obj = {'time': str(datetime.datetime.now()),
'source': source,
'blacklist': blacklist,
'organizations': {},
'uidentities': uidentities}
return json.dumps(obj, default=self._json_encoder,
indent=4, separators=(',', ': '),
sort_keys=True) |
0, module; 1, function_definition; 2, function_name:export; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, for_statement; 10, expression_statement; 11, return_statement; 12, comment:"""Export a set of organizations.
Method to export organizations from the registry. Organizations schema
will follow Sorting Hat JSON format.
:returns: a JSON formatted str
"""; 13, assignment; 14, assignment; 15, identifier:org; 16, identifier:orgs; 17, block; 18, assignment; 19, call; 20, identifier:organizations; 21, dictionary; 22, identifier:orgs; 23, call; 24, expression_statement; 25, expression_statement; 26, expression_statement; 27, identifier:obj; 28, dictionary; 29, attribute; 30, argument_list; 31, attribute; 32, argument_list; 33, assignment; 34, call; 35, assignment; 36, pair; 37, pair; 38, pair; 39, pair; 40, identifier:json; 41, identifier:dumps; 42, identifier:obj; 43, keyword_argument; 44, keyword_argument; 45, keyword_argument; 46, keyword_argument; 47, identifier:api; 48, identifier:registry; 49, attribute; 50, identifier:domains; 51, list_comprehension; 52, attribute; 53, argument_list; 54, subscript; 55, identifier:domains; 56, string; 57, call; 58, string; 59, list; 60, string; 61, identifier:organizations; 62, string; 63, dictionary; 64, identifier:default; 65, attribute; 66, identifier:indent; 67, integer:4; 68, identifier:separators; 69, tuple; 70, identifier:sort_keys; 71, True; 72, identifier:self; 73, identifier:db; 74, dictionary; 75, for_in_clause; 76, identifier:domains; 77, identifier:sort; 78, keyword_argument; 79, identifier:organizations; 80, attribute; 81, string_content:time; 82, identifier:str; 83, argument_list; 84, string_content:blacklist; 85, string_content:organizations; 86, string_content:uidentities; 87, identifier:self; 88, identifier:_json_encoder; 89, string; 90, string; 91, pair; 92, pair; 93, identifier:dom; 94, attribute; 95, identifier:key; 96, lambda; 97, identifier:org; 98, identifier:name; 99, call; 100, string_content:,; 101, string_content::; 102, string; 103, attribute; 104, string; 105, attribute; 106, identifier:org; 107, identifier:domains; 108, lambda_parameters; 109, subscript; 110, attribute; 111, argument_list; 112, string_content:domain; 113, identifier:dom; 114, identifier:domain; 115, string_content:is_top; 116, identifier:dom; 117, identifier:is_top_domain; 118, identifier:x; 119, identifier:x; 120, string; 121, attribute; 122, identifier:now; 123, string_content:domain; 124, identifier:datetime; 125, identifier:datetime | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 6, 12; 7, 13; 8, 14; 9, 15; 9, 16; 9, 17; 10, 18; 11, 19; 13, 20; 13, 21; 14, 22; 14, 23; 17, 24; 17, 25; 17, 26; 18, 27; 18, 28; 19, 29; 19, 30; 23, 31; 23, 32; 24, 33; 25, 34; 26, 35; 28, 36; 28, 37; 28, 38; 28, 39; 29, 40; 29, 41; 30, 42; 30, 43; 30, 44; 30, 45; 30, 46; 31, 47; 31, 48; 32, 49; 33, 50; 33, 51; 34, 52; 34, 53; 35, 54; 35, 55; 36, 56; 36, 57; 37, 58; 37, 59; 38, 60; 38, 61; 39, 62; 39, 63; 43, 64; 43, 65; 44, 66; 44, 67; 45, 68; 45, 69; 46, 70; 46, 71; 49, 72; 49, 73; 51, 74; 51, 75; 52, 76; 52, 77; 53, 78; 54, 79; 54, 80; 56, 81; 57, 82; 57, 83; 58, 84; 60, 85; 62, 86; 65, 87; 65, 88; 69, 89; 69, 90; 74, 91; 74, 92; 75, 93; 75, 94; 78, 95; 78, 96; 80, 97; 80, 98; 83, 99; 89, 100; 90, 101; 91, 102; 91, 103; 92, 104; 92, 105; 94, 106; 94, 107; 96, 108; 96, 109; 99, 110; 99, 111; 102, 112; 103, 113; 103, 114; 104, 115; 105, 116; 105, 117; 108, 118; 109, 119; 109, 120; 110, 121; 110, 122; 120, 123; 121, 124; 121, 125 | def export(self):
"""Export a set of organizations.
Method to export organizations from the registry. Organizations schema
will follow Sorting Hat JSON format.
:returns: a JSON formatted str
"""
organizations = {}
orgs = api.registry(self.db)
for org in orgs:
domains = [{'domain': dom.domain,
'is_top': dom.is_top_domain}
for dom in org.domains]
domains.sort(key=lambda x: x['domain'])
organizations[org.name] = domains
obj = {'time': str(datetime.datetime.now()),
'blacklist': [],
'organizations': organizations,
'uidentities': {}}
return json.dumps(obj, default=self._json_encoder,
indent=4, separators=(',', ': '),
sort_keys=True) |
0, module; 1, function_definition; 2, function_name:registry; 3, parameters; 4, block; 5, identifier:db; 6, default_parameter; 7, expression_statement; 8, expression_statement; 9, with_statement; 10, return_statement; 11, identifier:term; 12, None; 13, comment:"""List the organizations available in the registry.
The function will return the list of organizations. If term
parameter is set, it will only return the information about
the organizations which match that term. When the given term does
not match with any organization from the registry a 'NotFounError'
exception will be raised.
:param db: database manager
:param term: term to match with organizations names
:returns: a list of organizations sorted by their name
:raises NotFoundError: raised when the given term is not found on
any organization from the registry
"""; 14, assignment; 15, with_clause; 16, block; 17, identifier:orgs; 18, identifier:orgs; 19, list; 20, with_item; 21, if_statement; 22, comment:# Detach objects from the session; 23, expression_statement; 24, as_pattern; 25, identifier:term; 26, block; 27, else_clause; 28, call; 29, call; 30, as_pattern_target; 31, expression_statement; 32, if_statement; 33, block; 34, attribute; 35, argument_list; 36, attribute; 37, argument_list; 38, identifier:session; 39, assignment; 40, not_operator; 41, block; 42, expression_statement; 43, identifier:session; 44, identifier:expunge_all; 45, identifier:db; 46, identifier:connect; 47, identifier:orgs; 48, call; 49, identifier:orgs; 50, raise_statement; 51, assignment; 52, attribute; 53, argument_list; 54, call; 55, identifier:orgs; 56, call; 57, call; 58, identifier:all; 59, identifier:NotFoundError; 60, argument_list; 61, attribute; 62, argument_list; 63, attribute; 64, argument_list; 65, keyword_argument; 66, call; 67, identifier:all; 68, call; 69, line_continuation:\; 70, identifier:order_by; 71, attribute; 72, identifier:entity; 73, identifier:term; 74, attribute; 75, argument_list; 76, attribute; 77, argument_list; 78, identifier:Organization; 79, identifier:name; 80, call; 81, line_continuation:\; 82, identifier:order_by; 83, attribute; 84, call; 85, line_continuation:\; 86, identifier:filter; 87, call; 88, attribute; 89, argument_list; 90, identifier:Organization; 91, identifier:name; 92, attribute; 93, argument_list; 94, attribute; 95, argument_list; 96, identifier:session; 97, identifier:query; 98, identifier:Organization; 99, identifier:session; 100, identifier:query; 101, identifier:Organization; 102, attribute; 103, identifier:like; 104, binary_operator:'%' + term + '%'; 105, identifier:Organization; 106, identifier:name; 107, binary_operator:'%' + term; 108, string; 109, string; 110, identifier:term; 111, string_content:%; 112, string_content:% | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 6, 11; 6, 12; 7, 13; 8, 14; 9, 15; 9, 16; 10, 17; 14, 18; 14, 19; 15, 20; 16, 21; 16, 22; 16, 23; 20, 24; 21, 25; 21, 26; 21, 27; 23, 28; 24, 29; 24, 30; 26, 31; 26, 32; 27, 33; 28, 34; 28, 35; 29, 36; 29, 37; 30, 38; 31, 39; 32, 40; 32, 41; 33, 42; 34, 43; 34, 44; 36, 45; 36, 46; 39, 47; 39, 48; 40, 49; 41, 50; 42, 51; 48, 52; 48, 53; 50, 54; 51, 55; 51, 56; 52, 57; 52, 58; 54, 59; 54, 60; 56, 61; 56, 62; 57, 63; 57, 64; 60, 65; 61, 66; 61, 67; 63, 68; 63, 69; 63, 70; 64, 71; 65, 72; 65, 73; 66, 74; 66, 75; 68, 76; 68, 77; 71, 78; 71, 79; 74, 80; 74, 81; 74, 82; 75, 83; 76, 84; 76, 85; 76, 86; 77, 87; 80, 88; 80, 89; 83, 90; 83, 91; 84, 92; 84, 93; 87, 94; 87, 95; 88, 96; 88, 97; 89, 98; 92, 99; 92, 100; 93, 101; 94, 102; 94, 103; 95, 104; 102, 105; 102, 106; 104, 107; 104, 108; 107, 109; 107, 110; 108, 111; 109, 112 | def registry(db, term=None):
"""List the organizations available in the registry.
The function will return the list of organizations. If term
parameter is set, it will only return the information about
the organizations which match that term. When the given term does
not match with any organization from the registry a 'NotFounError'
exception will be raised.
:param db: database manager
:param term: term to match with organizations names
:returns: a list of organizations sorted by their name
:raises NotFoundError: raised when the given term is not found on
any organization from the registry
"""
orgs = []
with db.connect() as session:
if term:
orgs = session.query(Organization).\
filter(Organization.name.like('%' + term + '%')).\
order_by(Organization.name).all()
if not orgs:
raise NotFoundError(entity=term)
else:
orgs = session.query(Organization).\
order_by(Organization.name).all()
# Detach objects from the session
session.expunge_all()
return orgs |
0, module; 1, function_definition; 2, function_name:countries; 3, parameters; 4, block; 5, identifier:db; 6, default_parameter; 7, default_parameter; 8, expression_statement; 9, function_definition; 10, if_statement; 11, expression_statement; 12, with_statement; 13, return_statement; 14, identifier:code; 15, None; 16, identifier:term; 17, None; 18, comment:"""List the countries available in the registry.
The function will return the list of countries. When either 'code' or
'term' parameters are set, it will only return the information about
those countries that match them.
Take into account that 'code' is a country identifier composed by two
letters (i.e ES or US). A 'InvalidValueError' exception will be raised when
this identifier is not valid. If this value is valid, 'term' parameter
will be ignored.
When the given values do not match with any country from the registry
a 'NotFounError' exception will be raised.
:param db: database manager
:param code: country identifier composed by two letters
:param term: term to match with countries names
:returns: a list of countries sorted by their country id
:raises InvalidValueError: raised when 'code' is not a string composed by
two letters
:raises NotFoundError: raised when the given 'code' or 'term' is not
found for any country from the registry
"""; 19, function_name:_is_code_valid; 20, parameters; 21, block; 22, boolean_operator; 23, block; 24, assignment; 25, with_clause; 26, block; 27, identifier:cs; 28, identifier:code; 29, return_statement; 30, comparison_operator:code is not None; 31, not_operator; 32, raise_statement; 33, identifier:cs; 34, list; 35, with_item; 36, expression_statement; 37, if_statement; 38, comment:# Detach objects from the session; 39, expression_statement; 40, boolean_operator; 41, identifier:code; 42, None; 43, call; 44, call; 45, as_pattern; 46, assignment; 47, boolean_operator; 48, block; 49, else_clause; 50, call; 51, boolean_operator; 52, line_continuation:\; 53, call; 54, identifier:_is_code_valid; 55, argument_list; 56, identifier:InvalidValueError; 57, argument_list; 58, call; 59, as_pattern_target; 60, identifier:query; 61, call; 62, identifier:code; 63, identifier:term; 64, if_statement; 65, expression_statement; 66, if_statement; 67, block; 68, attribute; 69, argument_list; 70, comparison_operator:type(code) == str; 71, line_continuation:\; 72, comparison_operator:len(code) == 2; 73, attribute; 74, argument_list; 75, identifier:code; 76, binary_operator:'country code must be a 2 length alpha string - %s given'
% str(code); 77, attribute; 78, argument_list; 79, identifier:session; 80, attribute; 81, argument_list; 82, identifier:code; 83, block; 84, elif_clause; 85, assignment; 86, not_operator; 87, block; 88, expression_statement; 89, identifier:session; 90, identifier:expunge_all; 91, call; 92, identifier:str; 93, call; 94, integer:2; 95, identifier:code; 96, identifier:isalpha; 97, string; 98, call; 99, identifier:db; 100, identifier:connect; 101, identifier:session; 102, identifier:query; 103, identifier:Country; 104, expression_statement; 105, identifier:term; 106, block; 107, identifier:cs; 108, call; 109, identifier:cs; 110, expression_statement; 111, raise_statement; 112, assignment; 113, identifier:type; 114, argument_list; 115, identifier:len; 116, argument_list; 117, string_content:country code must be a 2 length alpha string - %s given; 118, identifier:str; 119, argument_list; 120, assignment; 121, expression_statement; 122, attribute; 123, argument_list; 124, assignment; 125, call; 126, identifier:cs; 127, call; 128, identifier:code; 129, identifier:code; 130, identifier:code; 131, identifier:query; 132, call; 133, assignment; 134, call; 135, identifier:all; 136, identifier:e; 137, conditional_expression:code if code else term; 138, identifier:NotFoundError; 139, argument_list; 140, attribute; 141, argument_list; 142, attribute; 143, argument_list; 144, identifier:query; 145, call; 146, attribute; 147, argument_list; 148, identifier:code; 149, identifier:code; 150, identifier:term; 151, keyword_argument; 152, call; 153, identifier:all; 154, identifier:query; 155, identifier:filter; 156, comparison_operator:Country.code == code.upper(); 157, attribute; 158, argument_list; 159, identifier:query; 160, identifier:order_by; 161, attribute; 162, identifier:entity; 163, identifier:e; 164, attribute; 165, argument_list; 166, attribute; 167, call; 168, identifier:query; 169, identifier:filter; 170, call; 171, identifier:Country; 172, identifier:code; 173, call; 174, line_continuation:\; 175, identifier:order_by; 176, attribute; 177, identifier:Country; 178, identifier:code; 179, attribute; 180, argument_list; 181, attribute; 182, argument_list; 183, attribute; 184, argument_list; 185, identifier:Country; 186, identifier:code; 187, identifier:code; 188, identifier:upper; 189, attribute; 190, identifier:like; 191, binary_operator:'%' + term + '%'; 192, identifier:session; 193, identifier:query; 194, identifier:Country; 195, identifier:Country; 196, identifier:name; 197, binary_operator:'%' + term; 198, string; 199, string; 200, identifier:term; 201, string_content:%; 202, string_content:% | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 6, 14; 6, 15; 7, 16; 7, 17; 8, 18; 9, 19; 9, 20; 9, 21; 10, 22; 10, 23; 11, 24; 12, 25; 12, 26; 13, 27; 20, 28; 21, 29; 22, 30; 22, 31; 23, 32; 24, 33; 24, 34; 25, 35; 26, 36; 26, 37; 26, 38; 26, 39; 29, 40; 30, 41; 30, 42; 31, 43; 32, 44; 35, 45; 36, 46; 37, 47; 37, 48; 37, 49; 39, 50; 40, 51; 40, 52; 40, 53; 43, 54; 43, 55; 44, 56; 44, 57; 45, 58; 45, 59; 46, 60; 46, 61; 47, 62; 47, 63; 48, 64; 48, 65; 48, 66; 49, 67; 50, 68; 50, 69; 51, 70; 51, 71; 51, 72; 53, 73; 53, 74; 55, 75; 57, 76; 58, 77; 58, 78; 59, 79; 61, 80; 61, 81; 64, 82; 64, 83; 64, 84; 65, 85; 66, 86; 66, 87; 67, 88; 68, 89; 68, 90; 70, 91; 70, 92; 72, 93; 72, 94; 73, 95; 73, 96; 76, 97; 76, 98; 77, 99; 77, 100; 80, 101; 80, 102; 81, 103; 83, 104; 84, 105; 84, 106; 85, 107; 85, 108; 86, 109; 87, 110; 87, 111; 88, 112; 91, 113; 91, 114; 93, 115; 93, 116; 97, 117; 98, 118; 98, 119; 104, 120; 106, 121; 108, 122; 108, 123; 110, 124; 111, 125; 112, 126; 112, 127; 114, 128; 116, 129; 119, 130; 120, 131; 120, 132; 121, 133; 122, 134; 122, 135; 124, 136; 124, 137; 125, 138; 125, 139; 127, 140; 127, 141; 132, 142; 132, 143; 133, 144; 133, 145; 134, 146; 134, 147; 137, 148; 137, 149; 137, 150; 139, 151; 140, 152; 140, 153; 142, 154; 142, 155; 143, 156; 145, 157; 145, 158; 146, 159; 146, 160; 147, 161; 151, 162; 151, 163; 152, 164; 152, 165; 156, 166; 156, 167; 157, 168; 157, 169; 158, 170; 161, 171; 161, 172; 164, 173; 164, 174; 164, 175; 165, 176; 166, 177; 166, 178; 167, 179; 167, 180; 170, 181; 170, 182; 173, 183; 173, 184; 176, 185; 176, 186; 179, 187; 179, 188; 181, 189; 181, 190; 182, 191; 183, 192; 183, 193; 184, 194; 189, 195; 189, 196; 191, 197; 191, 198; 197, 199; 197, 200; 198, 201; 199, 202 | def countries(db, code=None, term=None):
"""List the countries available in the registry.
The function will return the list of countries. When either 'code' or
'term' parameters are set, it will only return the information about
those countries that match them.
Take into account that 'code' is a country identifier composed by two
letters (i.e ES or US). A 'InvalidValueError' exception will be raised when
this identifier is not valid. If this value is valid, 'term' parameter
will be ignored.
When the given values do not match with any country from the registry
a 'NotFounError' exception will be raised.
:param db: database manager
:param code: country identifier composed by two letters
:param term: term to match with countries names
:returns: a list of countries sorted by their country id
:raises InvalidValueError: raised when 'code' is not a string composed by
two letters
:raises NotFoundError: raised when the given 'code' or 'term' is not
found for any country from the registry
"""
def _is_code_valid(code):
return type(code) == str \
and len(code) == 2 \
and code.isalpha()
if code is not None and not _is_code_valid(code):
raise InvalidValueError('country code must be a 2 length alpha string - %s given'
% str(code))
cs = []
with db.connect() as session:
query = session.query(Country)
if code or term:
if code:
query = query.filter(Country.code == code.upper())
elif term:
query = query.filter(Country.name.like('%' + term + '%'))
cs = query.order_by(Country.code).all()
if not cs:
e = code if code else term
raise NotFoundError(entity=e)
else:
cs = session.query(Country).\
order_by(Country.code).all()
# Detach objects from the session
session.expunge_all()
return cs |
0, module; 1, function_definition; 2, function_name:enrollments; 3, parameters; 4, block; 5, identifier:db; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, expression_statement; 11, if_statement; 12, if_statement; 13, if_statement; 14, if_statement; 15, if_statement; 16, expression_statement; 17, with_statement; 18, return_statement; 19, identifier:uuid; 20, None; 21, identifier:organization; 22, None; 23, identifier:from_date; 24, None; 25, identifier:to_date; 26, None; 27, comment:"""List the enrollment information available in the registry.
This function will return a list of enrollments. If 'uuid'
parameter is set, it will return the enrollments related to that
unique identity; if 'organization' parameter is given, it will
return the enrollments related to that organization; if both
parameters are set, the function will return the list of enrollments
of 'uuid' on the 'organization'.
Enrollments between a period can also be listed using 'from_date' and
'to_date' parameters. When these are set, the function will return
all those enrollments where Enrollment.start >= from_date AND
Enrollment.end <= to_date. Defaults values for these dates are
1900-01-01 and 2100-01-01.
When either 'uuid' or 'organization' are not in the registry a
'NotFoundError' exception will be raised.
:param db: database manager
:param uuid: unique identifier
:param organization: name of the organization
:param from_date: date when the enrollment starts
:param to_date: date when the enrollment ends
:returns: a list of enrollments sorted by uuid or by organization.
:raises NotFoundError: when either 'uuid' or 'organization' are not
found in the registry.
:raises InvalidValeError: it is raised in two cases, when "from_date" < 1900-01-01 or
"to_date" > 2100-01-01; when "from_date > to_date".
"""; 28, not_operator; 29, block; 30, not_operator; 31, block; 32, boolean_operator; 33, block; 34, boolean_operator; 35, block; 36, boolean_operator; 37, block; 38, assignment; 39, with_clause; 40, block; 41, identifier:enrollments; 42, identifier:from_date; 43, expression_statement; 44, identifier:to_date; 45, expression_statement; 46, comparison_operator:from_date < MIN_PERIOD_DATE; 47, comparison_operator:from_date > MAX_PERIOD_DATE; 48, raise_statement; 49, comparison_operator:to_date < MIN_PERIOD_DATE; 50, comparison_operator:to_date > MAX_PERIOD_DATE; 51, raise_statement; 52, boolean_operator; 53, comparison_operator:from_date > to_date; 54, raise_statement; 55, identifier:enrollments; 56, list; 57, with_item; 58, expression_statement; 59, comment:# Filter by uuid; 60, if_statement; 61, comment:# Filter by organization; 62, if_statement; 63, comment:# Get the results; 64, expression_statement; 65, comment:# Detach objects from the session; 66, expression_statement; 67, assignment; 68, assignment; 69, identifier:from_date; 70, identifier:MIN_PERIOD_DATE; 71, identifier:from_date; 72, identifier:MAX_PERIOD_DATE; 73, call; 74, identifier:to_date; 75, identifier:MIN_PERIOD_DATE; 76, identifier:to_date; 77, identifier:MAX_PERIOD_DATE; 78, call; 79, identifier:from_date; 80, identifier:to_date; 81, identifier:from_date; 82, identifier:to_date; 83, call; 84, as_pattern; 85, assignment; 86, identifier:uuid; 87, block; 88, identifier:organization; 89, block; 90, assignment; 91, call; 92, identifier:from_date; 93, identifier:MIN_PERIOD_DATE; 94, identifier:to_date; 95, identifier:MAX_PERIOD_DATE; 96, identifier:InvalidValueError; 97, argument_list; 98, identifier:InvalidValueError; 99, argument_list; 100, identifier:InvalidValueError; 101, argument_list; 102, call; 103, as_pattern_target; 104, identifier:query; 105, call; 106, expression_statement; 107, if_statement; 108, expression_statement; 109, expression_statement; 110, if_statement; 111, expression_statement; 112, identifier:enrollments; 113, call; 114, attribute; 115, argument_list; 116, binary_operator:"'from_date' %s is out of bounds" % str(from_date); 117, binary_operator:"'to_date' %s is out of bounds" % str(to_date); 118, binary_operator:"'from_date' %s cannot be greater than %s"
% (from_date, to_date); 119, attribute; 120, argument_list; 121, identifier:session; 122, attribute; 123, argument_list; 124, assignment; 125, not_operator; 126, block; 127, assignment; 128, assignment; 129, not_operator; 130, block; 131, assignment; 132, attribute; 133, argument_list; 134, identifier:session; 135, identifier:expunge_all; 136, string:"'from_date' %s is out of bounds"; 137, call; 138, string:"'to_date' %s is out of bounds"; 139, call; 140, string:"'from_date' %s cannot be greater than %s"; 141, tuple; 142, identifier:db; 143, identifier:connect; 144, call; 145, line_continuation:\; 146, identifier:filter; 147, comparison_operator:Enrollment.start >= from_date; 148, comparison_operator:Enrollment.end <= to_date; 149, identifier:uidentity; 150, call; 151, identifier:uidentity; 152, raise_statement; 153, identifier:query; 154, call; 155, identifier:org; 156, call; 157, identifier:org; 158, raise_statement; 159, identifier:query; 160, call; 161, call; 162, identifier:all; 163, identifier:str; 164, argument_list; 165, identifier:str; 166, argument_list; 167, identifier:from_date; 168, identifier:to_date; 169, attribute; 170, argument_list; 171, attribute; 172, identifier:from_date; 173, attribute; 174, identifier:to_date; 175, identifier:find_unique_identity; 176, argument_list; 177, call; 178, attribute; 179, argument_list; 180, identifier:find_organization; 181, argument_list; 182, call; 183, attribute; 184, argument_list; 185, attribute; 186, argument_list; 187, identifier:from_date; 188, identifier:to_date; 189, call; 190, line_continuation:\; 191, identifier:join; 192, identifier:UniqueIdentity; 193, identifier:Organization; 194, identifier:Enrollment; 195, identifier:start; 196, identifier:Enrollment; 197, identifier:end; 198, identifier:session; 199, identifier:uuid; 200, identifier:NotFoundError; 201, argument_list; 202, identifier:query; 203, identifier:filter; 204, comparison_operator:Enrollment.uidentity == uidentity; 205, identifier:session; 206, identifier:organization; 207, identifier:NotFoundError; 208, argument_list; 209, identifier:query; 210, identifier:filter; 211, comparison_operator:Enrollment.organization == org; 212, identifier:query; 213, identifier:order_by; 214, attribute; 215, attribute; 216, attribute; 217, attribute; 218, attribute; 219, argument_list; 220, keyword_argument; 221, attribute; 222, identifier:uidentity; 223, keyword_argument; 224, attribute; 225, identifier:org; 226, identifier:UniqueIdentity; 227, identifier:uuid; 228, identifier:Organization; 229, identifier:name; 230, identifier:Enrollment; 231, identifier:start; 232, identifier:Enrollment; 233, identifier:end; 234, identifier:session; 235, identifier:query; 236, identifier:Enrollment; 237, identifier:entity; 238, identifier:uuid; 239, identifier:Enrollment; 240, identifier:uidentity; 241, identifier:entity; 242, identifier:organization; 243, identifier:Enrollment; 244, identifier:organization | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 6, 19; 6, 20; 7, 21; 7, 22; 8, 23; 8, 24; 9, 25; 9, 26; 10, 27; 11, 28; 11, 29; 12, 30; 12, 31; 13, 32; 13, 33; 14, 34; 14, 35; 15, 36; 15, 37; 16, 38; 17, 39; 17, 40; 18, 41; 28, 42; 29, 43; 30, 44; 31, 45; 32, 46; 32, 47; 33, 48; 34, 49; 34, 50; 35, 51; 36, 52; 36, 53; 37, 54; 38, 55; 38, 56; 39, 57; 40, 58; 40, 59; 40, 60; 40, 61; 40, 62; 40, 63; 40, 64; 40, 65; 40, 66; 43, 67; 45, 68; 46, 69; 46, 70; 47, 71; 47, 72; 48, 73; 49, 74; 49, 75; 50, 76; 50, 77; 51, 78; 52, 79; 52, 80; 53, 81; 53, 82; 54, 83; 57, 84; 58, 85; 60, 86; 60, 87; 62, 88; 62, 89; 64, 90; 66, 91; 67, 92; 67, 93; 68, 94; 68, 95; 73, 96; 73, 97; 78, 98; 78, 99; 83, 100; 83, 101; 84, 102; 84, 103; 85, 104; 85, 105; 87, 106; 87, 107; 87, 108; 89, 109; 89, 110; 89, 111; 90, 112; 90, 113; 91, 114; 91, 115; 97, 116; 99, 117; 101, 118; 102, 119; 102, 120; 103, 121; 105, 122; 105, 123; 106, 124; 107, 125; 107, 126; 108, 127; 109, 128; 110, 129; 110, 130; 111, 131; 113, 132; 113, 133; 114, 134; 114, 135; 116, 136; 116, 137; 117, 138; 117, 139; 118, 140; 118, 141; 119, 142; 119, 143; 122, 144; 122, 145; 122, 146; 123, 147; 123, 148; 124, 149; 124, 150; 125, 151; 126, 152; 127, 153; 127, 154; 128, 155; 128, 156; 129, 157; 130, 158; 131, 159; 131, 160; 132, 161; 132, 162; 137, 163; 137, 164; 139, 165; 139, 166; 141, 167; 141, 168; 144, 169; 144, 170; 147, 171; 147, 172; 148, 173; 148, 174; 150, 175; 150, 176; 152, 177; 154, 178; 154, 179; 156, 180; 156, 181; 158, 182; 160, 183; 160, 184; 161, 185; 161, 186; 164, 187; 166, 188; 169, 189; 169, 190; 169, 191; 170, 192; 170, 193; 171, 194; 171, 195; 173, 196; 173, 197; 176, 198; 176, 199; 177, 200; 177, 201; 178, 202; 178, 203; 179, 204; 181, 205; 181, 206; 182, 207; 182, 208; 183, 209; 183, 210; 184, 211; 185, 212; 185, 213; 186, 214; 186, 215; 186, 216; 186, 217; 189, 218; 189, 219; 201, 220; 204, 221; 204, 222; 208, 223; 211, 224; 211, 225; 214, 226; 214, 227; 215, 228; 215, 229; 216, 230; 216, 231; 217, 232; 217, 233; 218, 234; 218, 235; 219, 236; 220, 237; 220, 238; 221, 239; 221, 240; 223, 241; 223, 242; 224, 243; 224, 244 | def enrollments(db, uuid=None, organization=None, from_date=None, to_date=None):
"""List the enrollment information available in the registry.
This function will return a list of enrollments. If 'uuid'
parameter is set, it will return the enrollments related to that
unique identity; if 'organization' parameter is given, it will
return the enrollments related to that organization; if both
parameters are set, the function will return the list of enrollments
of 'uuid' on the 'organization'.
Enrollments between a period can also be listed using 'from_date' and
'to_date' parameters. When these are set, the function will return
all those enrollments where Enrollment.start >= from_date AND
Enrollment.end <= to_date. Defaults values for these dates are
1900-01-01 and 2100-01-01.
When either 'uuid' or 'organization' are not in the registry a
'NotFoundError' exception will be raised.
:param db: database manager
:param uuid: unique identifier
:param organization: name of the organization
:param from_date: date when the enrollment starts
:param to_date: date when the enrollment ends
:returns: a list of enrollments sorted by uuid or by organization.
:raises NotFoundError: when either 'uuid' or 'organization' are not
found in the registry.
:raises InvalidValeError: it is raised in two cases, when "from_date" < 1900-01-01 or
"to_date" > 2100-01-01; when "from_date > to_date".
"""
if not from_date:
from_date = MIN_PERIOD_DATE
if not to_date:
to_date = MAX_PERIOD_DATE
if from_date < MIN_PERIOD_DATE or from_date > MAX_PERIOD_DATE:
raise InvalidValueError("'from_date' %s is out of bounds" % str(from_date))
if to_date < MIN_PERIOD_DATE or to_date > MAX_PERIOD_DATE:
raise InvalidValueError("'to_date' %s is out of bounds" % str(to_date))
if from_date and to_date and from_date > to_date:
raise InvalidValueError("'from_date' %s cannot be greater than %s"
% (from_date, to_date))
enrollments = []
with db.connect() as session:
query = session.query(Enrollment).\
join(UniqueIdentity, Organization).\
filter(Enrollment.start >= from_date,
Enrollment.end <= to_date)
# Filter by uuid
if uuid:
uidentity = find_unique_identity(session, uuid)
if not uidentity:
raise NotFoundError(entity=uuid)
query = query.filter(Enrollment.uidentity == uidentity)
# Filter by organization
if organization:
org = find_organization(session, organization)
if not org:
raise NotFoundError(entity=organization)
query = query.filter(Enrollment.organization == org)
# Get the results
enrollments = query.order_by(UniqueIdentity.uuid,
Organization.name,
Enrollment.start,
Enrollment.end).all()
# Detach objects from the session
session.expunge_all()
return enrollments |
0, module; 1, function_definition; 2, function_name:blacklist; 3, parameters; 4, block; 5, identifier:db; 6, default_parameter; 7, expression_statement; 8, expression_statement; 9, with_statement; 10, return_statement; 11, identifier:term; 12, None; 13, comment:"""List the blacklisted entities available in the registry.
The function will return the list of blacklisted entities. If term
parameter is set, it will only return the information about the
entities which match that term. When the given term does not match
with any entry on the blacklist a 'NotFoundError' exception will
be raised.
:param db: database manager
:param term: term to match with blacklisted entries
:returns: a list of blacklisted entities sorted by their name
:raises NotFoundError: raised when the given term is not found on
any blacklisted entry from the registry
"""; 14, assignment; 15, with_clause; 16, block; 17, identifier:mbs; 18, identifier:mbs; 19, list; 20, with_item; 21, if_statement; 22, comment:# Detach objects from the session; 23, expression_statement; 24, as_pattern; 25, identifier:term; 26, block; 27, else_clause; 28, call; 29, call; 30, as_pattern_target; 31, expression_statement; 32, if_statement; 33, block; 34, attribute; 35, argument_list; 36, attribute; 37, argument_list; 38, identifier:session; 39, assignment; 40, not_operator; 41, block; 42, expression_statement; 43, identifier:session; 44, identifier:expunge_all; 45, identifier:db; 46, identifier:connect; 47, identifier:mbs; 48, call; 49, identifier:mbs; 50, raise_statement; 51, assignment; 52, attribute; 53, argument_list; 54, call; 55, identifier:mbs; 56, call; 57, call; 58, identifier:all; 59, identifier:NotFoundError; 60, argument_list; 61, attribute; 62, argument_list; 63, attribute; 64, argument_list; 65, keyword_argument; 66, call; 67, identifier:all; 68, call; 69, line_continuation:\; 70, identifier:order_by; 71, attribute; 72, identifier:entity; 73, identifier:term; 74, attribute; 75, argument_list; 76, attribute; 77, argument_list; 78, identifier:MatchingBlacklist; 79, identifier:excluded; 80, call; 81, line_continuation:\; 82, identifier:order_by; 83, attribute; 84, call; 85, line_continuation:\; 86, identifier:filter; 87, call; 88, attribute; 89, argument_list; 90, identifier:MatchingBlacklist; 91, identifier:excluded; 92, attribute; 93, argument_list; 94, attribute; 95, argument_list; 96, identifier:session; 97, identifier:query; 98, identifier:MatchingBlacklist; 99, identifier:session; 100, identifier:query; 101, identifier:MatchingBlacklist; 102, attribute; 103, identifier:like; 104, binary_operator:'%' + term + '%'; 105, identifier:MatchingBlacklist; 106, identifier:excluded; 107, binary_operator:'%' + term; 108, string; 109, string; 110, identifier:term; 111, string_content:%; 112, string_content:% | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 6, 11; 6, 12; 7, 13; 8, 14; 9, 15; 9, 16; 10, 17; 14, 18; 14, 19; 15, 20; 16, 21; 16, 22; 16, 23; 20, 24; 21, 25; 21, 26; 21, 27; 23, 28; 24, 29; 24, 30; 26, 31; 26, 32; 27, 33; 28, 34; 28, 35; 29, 36; 29, 37; 30, 38; 31, 39; 32, 40; 32, 41; 33, 42; 34, 43; 34, 44; 36, 45; 36, 46; 39, 47; 39, 48; 40, 49; 41, 50; 42, 51; 48, 52; 48, 53; 50, 54; 51, 55; 51, 56; 52, 57; 52, 58; 54, 59; 54, 60; 56, 61; 56, 62; 57, 63; 57, 64; 60, 65; 61, 66; 61, 67; 63, 68; 63, 69; 63, 70; 64, 71; 65, 72; 65, 73; 66, 74; 66, 75; 68, 76; 68, 77; 71, 78; 71, 79; 74, 80; 74, 81; 74, 82; 75, 83; 76, 84; 76, 85; 76, 86; 77, 87; 80, 88; 80, 89; 83, 90; 83, 91; 84, 92; 84, 93; 87, 94; 87, 95; 88, 96; 88, 97; 89, 98; 92, 99; 92, 100; 93, 101; 94, 102; 94, 103; 95, 104; 102, 105; 102, 106; 104, 107; 104, 108; 107, 109; 107, 110; 108, 111; 109, 112 | def blacklist(db, term=None):
"""List the blacklisted entities available in the registry.
The function will return the list of blacklisted entities. If term
parameter is set, it will only return the information about the
entities which match that term. When the given term does not match
with any entry on the blacklist a 'NotFoundError' exception will
be raised.
:param db: database manager
:param term: term to match with blacklisted entries
:returns: a list of blacklisted entities sorted by their name
:raises NotFoundError: raised when the given term is not found on
any blacklisted entry from the registry
"""
mbs = []
with db.connect() as session:
if term:
mbs = session.query(MatchingBlacklist).\
filter(MatchingBlacklist.excluded.like('%' + term + '%')).\
order_by(MatchingBlacklist.excluded).all()
if not mbs:
raise NotFoundError(entity=term)
else:
mbs = session.query(MatchingBlacklist).\
order_by(MatchingBlacklist.excluded).all()
# Detach objects from the session
session.expunge_all()
return mbs |
0, module; 1, function_definition; 2, function_name:__parse; 3, parameters; 4, block; 5, identifier:self; 6, identifier:stream; 7, expression_statement; 8, if_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, comment:"""Parse Sorting Hat stream"""; 14, not_operator; 15, block; 16, assignment; 17, call; 18, call; 19, call; 20, identifier:stream; 21, raise_statement; 22, identifier:json; 23, call; 24, attribute; 25, argument_list; 26, attribute; 27, argument_list; 28, attribute; 29, argument_list; 30, call; 31, attribute; 32, argument_list; 33, identifier:self; 34, identifier:__parse_organizations; 35, identifier:json; 36, identifier:self; 37, identifier:__parse_identities; 38, identifier:json; 39, identifier:self; 40, identifier:__parse_blacklist; 41, identifier:json; 42, identifier:InvalidFormatError; 43, argument_list; 44, identifier:self; 45, identifier:__load_json; 46, identifier:stream; 47, keyword_argument; 48, identifier:cause; 49, string:"stream cannot be empty or None" | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 7, 13; 8, 14; 8, 15; 9, 16; 10, 17; 11, 18; 12, 19; 14, 20; 15, 21; 16, 22; 16, 23; 17, 24; 17, 25; 18, 26; 18, 27; 19, 28; 19, 29; 21, 30; 23, 31; 23, 32; 24, 33; 24, 34; 25, 35; 26, 36; 26, 37; 27, 38; 28, 39; 28, 40; 29, 41; 30, 42; 30, 43; 31, 44; 31, 45; 32, 46; 43, 47; 47, 48; 47, 49 | def __parse(self, stream):
"""Parse Sorting Hat stream"""
if not stream:
raise InvalidFormatError(cause="stream cannot be empty or None")
json = self.__load_json(stream)
self.__parse_organizations(json)
self.__parse_identities(json)
self.__parse_blacklist(json) |
0, module; 1, function_definition; 2, function_name:__parse_blacklist; 3, parameters; 4, block; 5, identifier:self; 6, identifier:json; 7, expression_statement; 8, try_statement; 9, comment:"""Parse blacklist entries using Sorting Hat format.
The Sorting Hat blacklist format is a JSON stream that
stores a list of blacklisted entries.
Next, there is an example of a valid stream:
{
"blacklist": [
"John Doe",
"John Smith",
"[email protected]"
]
}
:param stream: stream to parse
:raises InvalidFormatError: raised when the format of the stream is
not valid.
"""; 10, block; 11, except_clause; 12, for_statement; 13, as_pattern; 14, block; 15, identifier:entry; 16, subscript; 17, block; 18, identifier:KeyError; 19, as_pattern_target; 20, expression_statement; 21, raise_statement; 22, identifier:json; 23, string; 24, if_statement; 25, expression_statement; 26, expression_statement; 27, if_statement; 28, identifier:e; 29, assignment; 30, call; 31, string_content:blacklist; 32, not_operator; 33, block; 34, assignment; 35, assignment; 36, not_operator; 37, block; 38, identifier:msg; 39, binary_operator:"invalid json format. Attribute %s not found" % e.args; 40, identifier:InvalidFormatError; 41, argument_list; 42, identifier:entry; 43, expression_statement; 44, raise_statement; 45, identifier:excluded; 46, call; 47, identifier:bl; 48, call; 49, identifier:bl; 50, expression_statement; 51, expression_statement; 52, string:"invalid json format. Attribute %s not found"; 53, attribute; 54, keyword_argument; 55, assignment; 56, call; 57, attribute; 58, argument_list; 59, attribute; 60, argument_list; 61, assignment; 62, assignment; 63, identifier:e; 64, identifier:args; 65, identifier:cause; 66, identifier:msg; 67, identifier:msg; 68, string:"invalid json format. Blacklist entries cannot be null or empty"; 69, identifier:InvalidFormatError; 70, argument_list; 71, identifier:self; 72, identifier:__encode; 73, identifier:entry; 74, attribute; 75, identifier:get; 76, identifier:excluded; 77, None; 78, identifier:bl; 79, call; 80, subscript; 81, identifier:bl; 82, keyword_argument; 83, identifier:self; 84, identifier:_blacklist; 85, identifier:MatchingBlacklist; 86, argument_list; 87, attribute; 88, identifier:excluded; 89, identifier:cause; 90, identifier:msg; 91, keyword_argument; 92, identifier:self; 93, identifier:_blacklist; 94, identifier:excluded; 95, identifier:excluded | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 7, 9; 8, 10; 8, 11; 10, 12; 11, 13; 11, 14; 12, 15; 12, 16; 12, 17; 13, 18; 13, 19; 14, 20; 14, 21; 16, 22; 16, 23; 17, 24; 17, 25; 17, 26; 17, 27; 19, 28; 20, 29; 21, 30; 23, 31; 24, 32; 24, 33; 25, 34; 26, 35; 27, 36; 27, 37; 29, 38; 29, 39; 30, 40; 30, 41; 32, 42; 33, 43; 33, 44; 34, 45; 34, 46; 35, 47; 35, 48; 36, 49; 37, 50; 37, 51; 39, 52; 39, 53; 41, 54; 43, 55; 44, 56; 46, 57; 46, 58; 48, 59; 48, 60; 50, 61; 51, 62; 53, 63; 53, 64; 54, 65; 54, 66; 55, 67; 55, 68; 56, 69; 56, 70; 57, 71; 57, 72; 58, 73; 59, 74; 59, 75; 60, 76; 60, 77; 61, 78; 61, 79; 62, 80; 62, 81; 70, 82; 74, 83; 74, 84; 79, 85; 79, 86; 80, 87; 80, 88; 82, 89; 82, 90; 86, 91; 87, 92; 87, 93; 91, 94; 91, 95 | def __parse_blacklist(self, json):
"""Parse blacklist entries using Sorting Hat format.
The Sorting Hat blacklist format is a JSON stream that
stores a list of blacklisted entries.
Next, there is an example of a valid stream:
{
"blacklist": [
"John Doe",
"John Smith",
"[email protected]"
]
}
:param stream: stream to parse
:raises InvalidFormatError: raised when the format of the stream is
not valid.
"""
try:
for entry in json['blacklist']:
if not entry:
msg = "invalid json format. Blacklist entries cannot be null or empty"
raise InvalidFormatError(cause=msg)
excluded = self.__encode(entry)
bl = self._blacklist.get(excluded, None)
if not bl:
bl = MatchingBlacklist(excluded=excluded)
self._blacklist[excluded] = bl
except KeyError as e:
msg = "invalid json format. Attribute %s not found" % e.args
raise InvalidFormatError(cause=msg) |
0, module; 1, function_definition; 2, function_name:__parse_organizations; 3, parameters; 4, block; 5, identifier:self; 6, identifier:json; 7, expression_statement; 8, try_statement; 9, comment:"""Parse organizations using Sorting Hat format.
The Sorting Hat organizations format is a JSON stream which
its keys are the name of the organizations. Each organization
object has a list of domains. For instance:
{
"organizations": {
"Bitergia": [
{
"domain": "api.bitergia.com",
"is_top": false
},
{
"domain": "bitergia.com",
"is_top": true
}
],
"Example": []
},
"time": "2015-01-20 20:10:56.133378"
}
:param json: stream to parse
:raises InvalidFormatError: raised when the format of the stream is
not valid.
"""; 10, block; 11, except_clause; 12, for_statement; 13, as_pattern; 14, block; 15, identifier:organization; 16, subscript; 17, block; 18, identifier:KeyError; 19, as_pattern_target; 20, expression_statement; 21, raise_statement; 22, identifier:json; 23, string; 24, expression_statement; 25, expression_statement; 26, if_statement; 27, expression_statement; 28, for_statement; 29, identifier:e; 30, assignment; 31, call; 32, string_content:organizations; 33, assignment; 34, assignment; 35, not_operator; 36, block; 37, assignment; 38, identifier:domain; 39, identifier:domains; 40, block; 41, identifier:msg; 42, binary_operator:"invalid json format. Attribute %s not found" % e.args; 43, identifier:InvalidFormatError; 44, argument_list; 45, identifier:name; 46, call; 47, identifier:org; 48, call; 49, identifier:org; 50, expression_statement; 51, expression_statement; 52, identifier:domains; 53, subscript; 54, if_statement; 55, expression_statement; 56, expression_statement; 57, string:"invalid json format. Attribute %s not found"; 58, attribute; 59, keyword_argument; 60, attribute; 61, argument_list; 62, attribute; 63, argument_list; 64, assignment; 65, assignment; 66, subscript; 67, identifier:organization; 68, comparison_operator:type(domain['is_top']) != bool; 69, block; 70, assignment; 71, call; 72, identifier:e; 73, identifier:args; 74, identifier:cause; 75, identifier:msg; 76, identifier:self; 77, identifier:__encode; 78, identifier:organization; 79, attribute; 80, identifier:get; 81, identifier:name; 82, None; 83, identifier:org; 84, call; 85, subscript; 86, identifier:org; 87, identifier:json; 88, string; 89, call; 90, identifier:bool; 91, expression_statement; 92, raise_statement; 93, identifier:dom; 94, call; 95, attribute; 96, argument_list; 97, identifier:self; 98, identifier:_organizations; 99, identifier:Organization; 100, argument_list; 101, attribute; 102, identifier:name; 103, string_content:organizations; 104, identifier:type; 105, argument_list; 106, assignment; 107, call; 108, identifier:Domain; 109, argument_list; 110, attribute; 111, identifier:append; 112, identifier:dom; 113, keyword_argument; 114, identifier:self; 115, identifier:_organizations; 116, subscript; 117, identifier:msg; 118, string:"invalid json format. 'is_top' must have a bool value"; 119, identifier:InvalidFormatError; 120, argument_list; 121, keyword_argument; 122, keyword_argument; 123, identifier:org; 124, identifier:domains; 125, identifier:name; 126, identifier:name; 127, identifier:domain; 128, string; 129, keyword_argument; 130, identifier:domain; 131, subscript; 132, identifier:is_top_domain; 133, subscript; 134, string_content:is_top; 135, identifier:cause; 136, identifier:msg; 137, identifier:domain; 138, string; 139, identifier:domain; 140, string; 141, string_content:domain; 142, string_content:is_top | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 7, 9; 8, 10; 8, 11; 10, 12; 11, 13; 11, 14; 12, 15; 12, 16; 12, 17; 13, 18; 13, 19; 14, 20; 14, 21; 16, 22; 16, 23; 17, 24; 17, 25; 17, 26; 17, 27; 17, 28; 19, 29; 20, 30; 21, 31; 23, 32; 24, 33; 25, 34; 26, 35; 26, 36; 27, 37; 28, 38; 28, 39; 28, 40; 30, 41; 30, 42; 31, 43; 31, 44; 33, 45; 33, 46; 34, 47; 34, 48; 35, 49; 36, 50; 36, 51; 37, 52; 37, 53; 40, 54; 40, 55; 40, 56; 42, 57; 42, 58; 44, 59; 46, 60; 46, 61; 48, 62; 48, 63; 50, 64; 51, 65; 53, 66; 53, 67; 54, 68; 54, 69; 55, 70; 56, 71; 58, 72; 58, 73; 59, 74; 59, 75; 60, 76; 60, 77; 61, 78; 62, 79; 62, 80; 63, 81; 63, 82; 64, 83; 64, 84; 65, 85; 65, 86; 66, 87; 66, 88; 68, 89; 68, 90; 69, 91; 69, 92; 70, 93; 70, 94; 71, 95; 71, 96; 79, 97; 79, 98; 84, 99; 84, 100; 85, 101; 85, 102; 88, 103; 89, 104; 89, 105; 91, 106; 92, 107; 94, 108; 94, 109; 95, 110; 95, 111; 96, 112; 100, 113; 101, 114; 101, 115; 105, 116; 106, 117; 106, 118; 107, 119; 107, 120; 109, 121; 109, 122; 110, 123; 110, 124; 113, 125; 113, 126; 116, 127; 116, 128; 120, 129; 121, 130; 121, 131; 122, 132; 122, 133; 128, 134; 129, 135; 129, 136; 131, 137; 131, 138; 133, 139; 133, 140; 138, 141; 140, 142 | def __parse_organizations(self, json):
"""Parse organizations using Sorting Hat format.
The Sorting Hat organizations format is a JSON stream which
its keys are the name of the organizations. Each organization
object has a list of domains. For instance:
{
"organizations": {
"Bitergia": [
{
"domain": "api.bitergia.com",
"is_top": false
},
{
"domain": "bitergia.com",
"is_top": true
}
],
"Example": []
},
"time": "2015-01-20 20:10:56.133378"
}
:param json: stream to parse
:raises InvalidFormatError: raised when the format of the stream is
not valid.
"""
try:
for organization in json['organizations']:
name = self.__encode(organization)
org = self._organizations.get(name, None)
if not org:
org = Organization(name=name)
self._organizations[name] = org
domains = json['organizations'][organization]
for domain in domains:
if type(domain['is_top']) != bool:
msg = "invalid json format. 'is_top' must have a bool value"
raise InvalidFormatError(cause=msg)
dom = Domain(domain=domain['domain'],
is_top_domain=domain['is_top'])
org.domains.append(dom)
except KeyError as e:
msg = "invalid json format. Attribute %s not found" % e.args
raise InvalidFormatError(cause=msg) |
0, module; 1, function_definition; 2, function_name:import_blacklist; 3, parameters; 4, block; 5, identifier:self; 6, identifier:parser; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, expression_statement; 13, comment:"""Import blacklist.
New entries parsed by 'parser' will be added to the blacklist.
:param parser: sorting hat parser
"""; 14, assignment; 15, call; 16, assignment; 17, identifier:entry; 18, identifier:blacklist; 19, block; 20, call; 21, identifier:blacklist; 22, attribute; 23, attribute; 24, argument_list; 25, identifier:n; 26, integer:0; 27, try_statement; 28, attribute; 29, argument_list; 30, identifier:parser; 31, identifier:blacklist; 32, identifier:self; 33, identifier:log; 34, string:"Loading blacklist..."; 35, block; 36, except_clause; 37, except_clause; 38, identifier:self; 39, identifier:log; 40, binary_operator:"%d/%d blacklist entries loaded" % (n, len(blacklist)); 41, expression_statement; 42, expression_statement; 43, expression_statement; 44, as_pattern; 45, block; 46, as_pattern; 47, block; 48, string:"%d/%d blacklist entries loaded"; 49, tuple; 50, call; 51, call; 52, augmented_assignment; 53, identifier:ValueError; 54, as_pattern_target; 55, raise_statement; 56, identifier:AlreadyExistsError; 57, as_pattern_target; 58, expression_statement; 59, expression_statement; 60, identifier:n; 61, call; 62, attribute; 63, argument_list; 64, attribute; 65, argument_list; 66, identifier:n; 67, integer:1; 68, identifier:e; 69, call; 70, identifier:e; 71, assignment; 72, call; 73, identifier:len; 74, argument_list; 75, identifier:api; 76, identifier:add_to_matching_blacklist; 77, attribute; 78, attribute; 79, identifier:self; 80, identifier:display; 81, string; 82, keyword_argument; 83, identifier:RuntimeError; 84, argument_list; 85, identifier:msg; 86, binary_operator:"%s. Not added." % str(e); 87, attribute; 88, argument_list; 89, identifier:blacklist; 90, identifier:self; 91, identifier:db; 92, identifier:entry; 93, identifier:excluded; 94, string_content:load_blacklist.tmpl; 95, identifier:entry; 96, attribute; 97, call; 98, string:"%s. Not added."; 99, call; 100, identifier:self; 101, identifier:warning; 102, identifier:msg; 103, identifier:entry; 104, identifier:excluded; 105, identifier:str; 106, argument_list; 107, identifier:str; 108, argument_list; 109, identifier:e; 110, identifier:e | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 7, 13; 8, 14; 9, 15; 10, 16; 11, 17; 11, 18; 11, 19; 12, 20; 14, 21; 14, 22; 15, 23; 15, 24; 16, 25; 16, 26; 19, 27; 20, 28; 20, 29; 22, 30; 22, 31; 23, 32; 23, 33; 24, 34; 27, 35; 27, 36; 27, 37; 28, 38; 28, 39; 29, 40; 35, 41; 35, 42; 35, 43; 36, 44; 36, 45; 37, 46; 37, 47; 40, 48; 40, 49; 41, 50; 42, 51; 43, 52; 44, 53; 44, 54; 45, 55; 46, 56; 46, 57; 47, 58; 47, 59; 49, 60; 49, 61; 50, 62; 50, 63; 51, 64; 51, 65; 52, 66; 52, 67; 54, 68; 55, 69; 57, 70; 58, 71; 59, 72; 61, 73; 61, 74; 62, 75; 62, 76; 63, 77; 63, 78; 64, 79; 64, 80; 65, 81; 65, 82; 69, 83; 69, 84; 71, 85; 71, 86; 72, 87; 72, 88; 74, 89; 77, 90; 77, 91; 78, 92; 78, 93; 81, 94; 82, 95; 82, 96; 84, 97; 86, 98; 86, 99; 87, 100; 87, 101; 88, 102; 96, 103; 96, 104; 97, 105; 97, 106; 99, 107; 99, 108; 106, 109; 108, 110 | def import_blacklist(self, parser):
"""Import blacklist.
New entries parsed by 'parser' will be added to the blacklist.
:param parser: sorting hat parser
"""
blacklist = parser.blacklist
self.log("Loading blacklist...")
n = 0
for entry in blacklist:
try:
api.add_to_matching_blacklist(self.db, entry.excluded)
self.display('load_blacklist.tmpl', entry=entry.excluded)
n += 1
except ValueError as e:
raise RuntimeError(str(e))
except AlreadyExistsError as e:
msg = "%s. Not added." % str(e)
self.warning(msg)
self.log("%d/%d blacklist entries loaded" % (n, len(blacklist))) |
0, module; 1, function_definition; 2, function_name:import_organizations; 3, parameters; 4, block; 5, identifier:self; 6, identifier:parser; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, for_statement; 11, identifier:overwrite; 12, False; 13, comment:"""Import organizations.
New domains and organizations parsed by 'parser' will be added
to the registry. Remember that a domain can only be assigned to
one organization. If one of the given domains is already on the registry,
the new relationship will NOT be created unless 'overwrite' were set
to 'True'.
:param parser: sorting hat parser
:param overwrite: force to reassign domains
"""; 14, assignment; 15, identifier:org; 16, identifier:orgs; 17, block; 18, identifier:orgs; 19, attribute; 20, try_statement; 21, for_statement; 22, identifier:parser; 23, identifier:organizations; 24, block; 25, except_clause; 26, except_clause; 27, identifier:dom; 28, attribute; 29, block; 30, expression_statement; 31, as_pattern; 32, block; 33, as_pattern; 34, block; 35, identifier:org; 36, identifier:domains; 37, try_statement; 38, call; 39, identifier:ValueError; 40, as_pattern_target; 41, raise_statement; 42, identifier:AlreadyExistsError; 43, as_pattern_target; 44, pass_statement; 45, block; 46, except_clause; 47, except_clause; 48, attribute; 49, argument_list; 50, identifier:e; 51, call; 52, identifier:e; 53, expression_statement; 54, expression_statement; 55, as_pattern; 56, block; 57, as_pattern; 58, block; 59, identifier:api; 60, identifier:add_organization; 61, attribute; 62, attribute; 63, identifier:RuntimeError; 64, argument_list; 65, call; 66, call; 67, tuple; 68, as_pattern_target; 69, raise_statement; 70, identifier:AlreadyExistsError; 71, as_pattern_target; 72, expression_statement; 73, expression_statement; 74, identifier:self; 75, identifier:db; 76, identifier:org; 77, identifier:name; 78, call; 79, attribute; 80, argument_list; 81, attribute; 82, argument_list; 83, identifier:ValueError; 84, identifier:NotFoundError; 85, identifier:e; 86, call; 87, identifier:e; 88, assignment; 89, call; 90, identifier:str; 91, argument_list; 92, identifier:api; 93, identifier:add_domain; 94, attribute; 95, attribute; 96, attribute; 97, keyword_argument; 98, keyword_argument; 99, identifier:self; 100, identifier:display; 101, string; 102, keyword_argument; 103, keyword_argument; 104, identifier:RuntimeError; 105, argument_list; 106, identifier:msg; 107, binary_operator:"%s. Not updated." % str(e); 108, attribute; 109, argument_list; 110, identifier:e; 111, identifier:self; 112, identifier:db; 113, identifier:org; 114, identifier:name; 115, identifier:dom; 116, identifier:domain; 117, identifier:is_top_domain; 118, attribute; 119, identifier:overwrite; 120, identifier:overwrite; 121, string_content:load_domains.tmpl; 122, identifier:domain; 123, attribute; 124, identifier:organization; 125, attribute; 126, call; 127, string:"%s. Not updated."; 128, call; 129, identifier:self; 130, identifier:warning; 131, identifier:msg; 132, identifier:dom; 133, identifier:is_top_domain; 134, identifier:dom; 135, identifier:domain; 136, identifier:org; 137, identifier:name; 138, identifier:str; 139, argument_list; 140, identifier:str; 141, argument_list; 142, identifier:e; 143, identifier:e | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 7, 11; 7, 12; 8, 13; 9, 14; 10, 15; 10, 16; 10, 17; 14, 18; 14, 19; 17, 20; 17, 21; 19, 22; 19, 23; 20, 24; 20, 25; 20, 26; 21, 27; 21, 28; 21, 29; 24, 30; 25, 31; 25, 32; 26, 33; 26, 34; 28, 35; 28, 36; 29, 37; 30, 38; 31, 39; 31, 40; 32, 41; 33, 42; 33, 43; 34, 44; 37, 45; 37, 46; 37, 47; 38, 48; 38, 49; 40, 50; 41, 51; 43, 52; 45, 53; 45, 54; 46, 55; 46, 56; 47, 57; 47, 58; 48, 59; 48, 60; 49, 61; 49, 62; 51, 63; 51, 64; 53, 65; 54, 66; 55, 67; 55, 68; 56, 69; 57, 70; 57, 71; 58, 72; 58, 73; 61, 74; 61, 75; 62, 76; 62, 77; 64, 78; 65, 79; 65, 80; 66, 81; 66, 82; 67, 83; 67, 84; 68, 85; 69, 86; 71, 87; 72, 88; 73, 89; 78, 90; 78, 91; 79, 92; 79, 93; 80, 94; 80, 95; 80, 96; 80, 97; 80, 98; 81, 99; 81, 100; 82, 101; 82, 102; 82, 103; 86, 104; 86, 105; 88, 106; 88, 107; 89, 108; 89, 109; 91, 110; 94, 111; 94, 112; 95, 113; 95, 114; 96, 115; 96, 116; 97, 117; 97, 118; 98, 119; 98, 120; 101, 121; 102, 122; 102, 123; 103, 124; 103, 125; 105, 126; 107, 127; 107, 128; 108, 129; 108, 130; 109, 131; 118, 132; 118, 133; 123, 134; 123, 135; 125, 136; 125, 137; 126, 138; 126, 139; 128, 140; 128, 141; 139, 142; 141, 143 | def import_organizations(self, parser, overwrite=False):
"""Import organizations.
New domains and organizations parsed by 'parser' will be added
to the registry. Remember that a domain can only be assigned to
one organization. If one of the given domains is already on the registry,
the new relationship will NOT be created unless 'overwrite' were set
to 'True'.
:param parser: sorting hat parser
:param overwrite: force to reassign domains
"""
orgs = parser.organizations
for org in orgs:
try:
api.add_organization(self.db, org.name)
except ValueError as e:
raise RuntimeError(str(e))
except AlreadyExistsError as e:
pass
for dom in org.domains:
try:
api.add_domain(self.db, org.name, dom.domain,
is_top_domain=dom.is_top_domain,
overwrite=overwrite)
self.display('load_domains.tmpl', domain=dom.domain,
organization=org.name)
except (ValueError, NotFoundError) as e:
raise RuntimeError(str(e))
except AlreadyExistsError as e:
msg = "%s. Not updated." % str(e)
self.warning(msg) |
0, module; 1, function_definition; 2, function_name:import_identities; 3, parameters; 4, block; 5, identifier:self; 6, identifier:parser; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, expression_statement; 13, expression_statement; 14, if_statement; 15, expression_statement; 16, try_statement; 17, return_statement; 18, identifier:matching; 19, None; 20, identifier:match_new; 21, False; 22, identifier:no_strict_matching; 23, False; 24, identifier:reset; 25, False; 26, identifier:verbose; 27, False; 28, comment:"""Import identities information on the registry.
New unique identities, organizations and enrollment data parsed
by 'parser' will be added to the registry.
Optionally, this method can look for possible identities that match with
the new one to insert using 'matching' method. If a match is found,
that means both identities are likely the same. Therefore, both identities
would be merged into one. The 'match_new' parameter can be set to match
and merge only new loaded identities. Rigorous validation of mathching
values (i.e, well formed email addresses) will be disabled when
<no_strict_matching> is set to to `True`.
When `reset` is set, relationships and enrollments will be removed
before loading any data.
:param parser: sorting hat parser
:param matching: type of matching used to merge existing identities
:param match_new: match and merge only the new loaded identities
:param no_strict_matching: disable strict matching (i.e, well-formed email addresses)
:param reset: remove relationships and enrollments before loading data
:param verbose: run in verbose mode when matching is set
"""; 29, assignment; 30, identifier:matching; 31, block; 32, assignment; 33, block; 34, except_clause; 35, identifier:CMD_SUCCESS; 36, identifier:matcher; 37, None; 38, expression_statement; 39, try_statement; 40, identifier:uidentities; 41, attribute; 42, expression_statement; 43, as_pattern; 44, block; 45, assignment; 46, block; 47, except_clause; 48, identifier:parser; 49, identifier:identities; 50, call; 51, identifier:LoadError; 52, as_pattern_target; 53, expression_statement; 54, return_statement; 55, identifier:strict; 56, not_operator; 57, expression_statement; 58, expression_statement; 59, as_pattern; 60, block; 61, attribute; 62, argument_list; 63, identifier:e; 64, call; 65, attribute; 66, identifier:no_strict_matching; 67, assignment; 68, assignment; 69, identifier:MatcherNotSupportedError; 70, as_pattern_target; 71, expression_statement; 72, return_statement; 73, identifier:self; 74, identifier:__load_unique_identities; 75, identifier:uidentities; 76, identifier:matcher; 77, identifier:match_new; 78, identifier:reset; 79, identifier:verbose; 80, attribute; 81, argument_list; 82, identifier:e; 83, identifier:code; 84, identifier:blacklist; 85, call; 86, identifier:matcher; 87, call; 88, identifier:e; 89, call; 90, attribute; 91, identifier:self; 92, identifier:error; 93, call; 94, attribute; 95, argument_list; 96, identifier:create_identity_matcher; 97, argument_list; 98, attribute; 99, argument_list; 100, identifier:e; 101, identifier:code; 102, identifier:str; 103, argument_list; 104, identifier:api; 105, identifier:blacklist; 106, attribute; 107, identifier:matching; 108, identifier:blacklist; 109, keyword_argument; 110, identifier:self; 111, identifier:error; 112, call; 113, identifier:e; 114, identifier:self; 115, identifier:db; 116, identifier:strict; 117, identifier:strict; 118, identifier:str; 119, argument_list; 120, identifier:e | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 7, 18; 7, 19; 8, 20; 8, 21; 9, 22; 9, 23; 10, 24; 10, 25; 11, 26; 11, 27; 12, 28; 13, 29; 14, 30; 14, 31; 15, 32; 16, 33; 16, 34; 17, 35; 29, 36; 29, 37; 31, 38; 31, 39; 32, 40; 32, 41; 33, 42; 34, 43; 34, 44; 38, 45; 39, 46; 39, 47; 41, 48; 41, 49; 42, 50; 43, 51; 43, 52; 44, 53; 44, 54; 45, 55; 45, 56; 46, 57; 46, 58; 47, 59; 47, 60; 50, 61; 50, 62; 52, 63; 53, 64; 54, 65; 56, 66; 57, 67; 58, 68; 59, 69; 59, 70; 60, 71; 60, 72; 61, 73; 61, 74; 62, 75; 62, 76; 62, 77; 62, 78; 62, 79; 64, 80; 64, 81; 65, 82; 65, 83; 67, 84; 67, 85; 68, 86; 68, 87; 70, 88; 71, 89; 72, 90; 80, 91; 80, 92; 81, 93; 85, 94; 85, 95; 87, 96; 87, 97; 89, 98; 89, 99; 90, 100; 90, 101; 93, 102; 93, 103; 94, 104; 94, 105; 95, 106; 97, 107; 97, 108; 97, 109; 98, 110; 98, 111; 99, 112; 103, 113; 106, 114; 106, 115; 109, 116; 109, 117; 112, 118; 112, 119; 119, 120 | def import_identities(self, parser, matching=None, match_new=False,
no_strict_matching=False,
reset=False, verbose=False):
"""Import identities information on the registry.
New unique identities, organizations and enrollment data parsed
by 'parser' will be added to the registry.
Optionally, this method can look for possible identities that match with
the new one to insert using 'matching' method. If a match is found,
that means both identities are likely the same. Therefore, both identities
would be merged into one. The 'match_new' parameter can be set to match
and merge only new loaded identities. Rigorous validation of mathching
values (i.e, well formed email addresses) will be disabled when
<no_strict_matching> is set to to `True`.
When `reset` is set, relationships and enrollments will be removed
before loading any data.
:param parser: sorting hat parser
:param matching: type of matching used to merge existing identities
:param match_new: match and merge only the new loaded identities
:param no_strict_matching: disable strict matching (i.e, well-formed email addresses)
:param reset: remove relationships and enrollments before loading data
:param verbose: run in verbose mode when matching is set
"""
matcher = None
if matching:
strict = not no_strict_matching
try:
blacklist = api.blacklist(self.db)
matcher = create_identity_matcher(matching, blacklist, strict=strict)
except MatcherNotSupportedError as e:
self.error(str(e))
return e.code
uidentities = parser.identities
try:
self.__load_unique_identities(uidentities, matcher, match_new,
reset, verbose)
except LoadError as e:
self.error(str(e))
return e.code
return CMD_SUCCESS |
0, module; 1, function_definition; 2, function_name:initialize; 3, parameters; 4, block; 5, identifier:self; 6, identifier:name; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, if_statement; 14, try_statement; 15, return_statement; 16, identifier:reuse; 17, False; 18, comment:"""Create an empty Sorting Hat registry.
This method creates a new database including the schema of Sorting Hat.
Any attempt to create a new registry over an existing instance will
produce an error, except if reuse=True. In that case, the
database will be reused, assuming the database schema is correct
(it won't be created in this case).
:param name: name of the database
:param reuse: reuse database if it already exists
"""; 19, assignment; 20, assignment; 21, assignment; 22, assignment; 23, comparison_operator:'-' in name; 24, block; 25, block; 26, except_clause; 27, except_clause; 28, except_clause; 29, identifier:CMD_SUCCESS; 30, identifier:user; 31, subscript; 32, identifier:password; 33, subscript; 34, identifier:host; 35, subscript; 36, identifier:port; 37, subscript; 38, string; 39, identifier:name; 40, expression_statement; 41, return_statement; 42, expression_statement; 43, comment:# Try to access and create schema; 44, expression_statement; 45, comment:# Load countries list; 46, expression_statement; 47, as_pattern; 48, block; 49, as_pattern; 50, block; 51, as_pattern; 52, block; 53, attribute; 54, string; 55, attribute; 56, string; 57, attribute; 58, string; 59, attribute; 60, string; 61, string_content:-; 62, call; 63, identifier:CODE_VALUE_ERROR; 64, call; 65, assignment; 66, call; 67, identifier:DatabaseExists; 68, as_pattern_target; 69, if_statement; 70, identifier:DatabaseError; 71, as_pattern_target; 72, expression_statement; 73, return_statement; 74, identifier:LoadError; 75, as_pattern_target; 76, expression_statement; 77, expression_statement; 78, return_statement; 79, identifier:self; 80, identifier:_kwargs; 81, string_content:user; 82, identifier:self; 83, identifier:_kwargs; 84, string_content:password; 85, identifier:self; 86, identifier:_kwargs; 87, string_content:host; 88, identifier:self; 89, identifier:_kwargs; 90, string_content:port; 91, attribute; 92, argument_list; 93, attribute; 94, argument_list; 95, identifier:db; 96, call; 97, attribute; 98, argument_list; 99, identifier:e; 100, not_operator; 101, block; 102, identifier:e; 103, call; 104, identifier:CODE_DATABASE_ERROR; 105, identifier:e; 106, call; 107, call; 108, identifier:CODE_LOAD_ERROR; 109, identifier:self; 110, identifier:error; 111, binary_operator:"dabase name '%s' cannot contain '-' characters" % name; 112, identifier:Database; 113, identifier:create; 114, identifier:user; 115, identifier:password; 116, identifier:name; 117, identifier:host; 118, identifier:port; 119, identifier:Database; 120, argument_list; 121, identifier:self; 122, identifier:__load_countries; 123, identifier:db; 124, identifier:reuse; 125, expression_statement; 126, return_statement; 127, attribute; 128, argument_list; 129, attribute; 130, argument_list; 131, attribute; 132, argument_list; 133, string:"dabase name '%s' cannot contain '-' characters"; 134, identifier:name; 135, identifier:user; 136, identifier:password; 137, identifier:name; 138, identifier:host; 139, identifier:port; 140, call; 141, identifier:CODE_DATABASE_EXISTS; 142, identifier:self; 143, identifier:error; 144, call; 145, identifier:Database; 146, identifier:drop; 147, identifier:user; 148, identifier:password; 149, identifier:name; 150, identifier:host; 151, identifier:port; 152, identifier:self; 153, identifier:error; 154, call; 155, attribute; 156, argument_list; 157, identifier:str; 158, argument_list; 159, identifier:str; 160, argument_list; 161, identifier:self; 162, identifier:error; 163, call; 164, identifier:e; 165, identifier:e; 166, identifier:str; 167, argument_list; 168, identifier:e | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 7, 16; 7, 17; 8, 18; 9, 19; 10, 20; 11, 21; 12, 22; 13, 23; 13, 24; 14, 25; 14, 26; 14, 27; 14, 28; 15, 29; 19, 30; 19, 31; 20, 32; 20, 33; 21, 34; 21, 35; 22, 36; 22, 37; 23, 38; 23, 39; 24, 40; 24, 41; 25, 42; 25, 43; 25, 44; 25, 45; 25, 46; 26, 47; 26, 48; 27, 49; 27, 50; 28, 51; 28, 52; 31, 53; 31, 54; 33, 55; 33, 56; 35, 57; 35, 58; 37, 59; 37, 60; 38, 61; 40, 62; 41, 63; 42, 64; 44, 65; 46, 66; 47, 67; 47, 68; 48, 69; 49, 70; 49, 71; 50, 72; 50, 73; 51, 74; 51, 75; 52, 76; 52, 77; 52, 78; 53, 79; 53, 80; 54, 81; 55, 82; 55, 83; 56, 84; 57, 85; 57, 86; 58, 87; 59, 88; 59, 89; 60, 90; 62, 91; 62, 92; 64, 93; 64, 94; 65, 95; 65, 96; 66, 97; 66, 98; 68, 99; 69, 100; 69, 101; 71, 102; 72, 103; 73, 104; 75, 105; 76, 106; 77, 107; 78, 108; 91, 109; 91, 110; 92, 111; 93, 112; 93, 113; 94, 114; 94, 115; 94, 116; 94, 117; 94, 118; 96, 119; 96, 120; 97, 121; 97, 122; 98, 123; 100, 124; 101, 125; 101, 126; 103, 127; 103, 128; 106, 129; 106, 130; 107, 131; 107, 132; 111, 133; 111, 134; 120, 135; 120, 136; 120, 137; 120, 138; 120, 139; 125, 140; 126, 141; 127, 142; 127, 143; 128, 144; 129, 145; 129, 146; 130, 147; 130, 148; 130, 149; 130, 150; 130, 151; 131, 152; 131, 153; 132, 154; 140, 155; 140, 156; 144, 157; 144, 158; 154, 159; 154, 160; 155, 161; 155, 162; 156, 163; 158, 164; 160, 165; 163, 166; 163, 167; 167, 168 | def initialize(self, name, reuse=False):
"""Create an empty Sorting Hat registry.
This method creates a new database including the schema of Sorting Hat.
Any attempt to create a new registry over an existing instance will
produce an error, except if reuse=True. In that case, the
database will be reused, assuming the database schema is correct
(it won't be created in this case).
:param name: name of the database
:param reuse: reuse database if it already exists
"""
user = self._kwargs['user']
password = self._kwargs['password']
host = self._kwargs['host']
port = self._kwargs['port']
if '-' in name:
self.error("dabase name '%s' cannot contain '-' characters" % name)
return CODE_VALUE_ERROR
try:
Database.create(user, password, name, host, port)
# Try to access and create schema
db = Database(user, password, name, host, port)
# Load countries list
self.__load_countries(db)
except DatabaseExists as e:
if not reuse:
self.error(str(e))
return CODE_DATABASE_EXISTS
except DatabaseError as e:
self.error(str(e))
return CODE_DATABASE_ERROR
except LoadError as e:
Database.drop(user, password, name, host, port)
self.error(str(e))
return CODE_LOAD_ERROR
return CMD_SUCCESS |
0, module; 1, function_definition; 2, function_name:uuid; 3, parameters; 4, block; 5, identifier:source; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, if_statement; 11, if_statement; 12, if_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, return_statement; 17, identifier:email; 18, None; 19, identifier:name; 20, None; 21, identifier:username; 22, None; 23, comment:"""Get the UUID related to the identity data.
Based on the input data, the function will return the UUID associated
to an identity. On this version, the UUID will be the SHA1 of
"source:email:name:username" string. This string is case insensitive,
which means same values for the input parameters in upper
or lower case will produce the same UUID.
The value of 'name' will converted to its unaccent form which means
same values with accent or unnacent chars (i.e 'ö and o') will
generate the same UUID.
For instance, these combinations will produce the same UUID:
('scm', '[email protected]', 'John Smith', 'jsmith'),
('scm', 'jsmith@example,com', 'Jöhn Smith', 'jsmith'),
('scm', '[email protected]', 'John Smith', 'JSMITH'),
('scm', '[email protected]', 'john Smith', 'jsmith')
:param source: data source
:param email: email of the identity
:param name: full name of the identity
:param username: user name used by the identity
:returns: a universal unique identifier for Sorting Hat
:raises ValueError: when source is None or empty; each one of the
parameters is None; parameters are empty.
"""; 24, comparison_operator:source is None; 25, block; 26, comparison_operator:source == ''; 27, block; 28, not_operator; 29, block; 30, assignment; 31, assignment; 32, assignment; 33, identifier:uuid_; 34, identifier:source; 35, None; 36, raise_statement; 37, identifier:source; 38, string; 39, raise_statement; 40, parenthesized_expression; 41, raise_statement; 42, identifier:s; 43, call; 44, identifier:sha1; 45, call; 46, identifier:uuid_; 47, call; 48, call; 49, call; 50, boolean_operator; 51, call; 52, attribute; 53, argument_list; 54, attribute; 55, argument_list; 56, attribute; 57, argument_list; 58, identifier:ValueError; 59, argument_list; 60, identifier:ValueError; 61, argument_list; 62, boolean_operator; 63, identifier:username; 64, identifier:ValueError; 65, argument_list; 66, call; 67, identifier:lower; 68, identifier:hashlib; 69, identifier:sha1; 70, call; 71, identifier:sha1; 72, identifier:hexdigest; 73, string:"source cannot be None"; 74, string:"source cannot be an empty string"; 75, identifier:email; 76, identifier:name; 77, string:"identity data cannot be None or empty"; 78, attribute; 79, argument_list; 80, attribute; 81, argument_list; 82, string; 83, identifier:join; 84, tuple; 85, identifier:s; 86, identifier:encode; 87, string; 88, keyword_argument; 89, string_content::; 90, call; 91, call; 92, call; 93, call; 94, string_content:UTF-8; 95, identifier:errors; 96, string:"surrogateescape"; 97, identifier:to_unicode; 98, argument_list; 99, identifier:to_unicode; 100, argument_list; 101, identifier:to_unicode; 102, argument_list; 103, identifier:to_unicode; 104, argument_list; 105, identifier:source; 106, identifier:email; 107, identifier:name; 108, keyword_argument; 109, identifier:username; 110, identifier:unaccent; 111, True | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 6, 17; 6, 18; 7, 19; 7, 20; 8, 21; 8, 22; 9, 23; 10, 24; 10, 25; 11, 26; 11, 27; 12, 28; 12, 29; 13, 30; 14, 31; 15, 32; 16, 33; 24, 34; 24, 35; 25, 36; 26, 37; 26, 38; 27, 39; 28, 40; 29, 41; 30, 42; 30, 43; 31, 44; 31, 45; 32, 46; 32, 47; 36, 48; 39, 49; 40, 50; 41, 51; 43, 52; 43, 53; 45, 54; 45, 55; 47, 56; 47, 57; 48, 58; 48, 59; 49, 60; 49, 61; 50, 62; 50, 63; 51, 64; 51, 65; 52, 66; 52, 67; 54, 68; 54, 69; 55, 70; 56, 71; 56, 72; 59, 73; 61, 74; 62, 75; 62, 76; 65, 77; 66, 78; 66, 79; 70, 80; 70, 81; 78, 82; 78, 83; 79, 84; 80, 85; 80, 86; 81, 87; 81, 88; 82, 89; 84, 90; 84, 91; 84, 92; 84, 93; 87, 94; 88, 95; 88, 96; 90, 97; 90, 98; 91, 99; 91, 100; 92, 101; 92, 102; 93, 103; 93, 104; 98, 105; 100, 106; 102, 107; 102, 108; 104, 109; 108, 110; 108, 111 | def uuid(source, email=None, name=None, username=None):
"""Get the UUID related to the identity data.
Based on the input data, the function will return the UUID associated
to an identity. On this version, the UUID will be the SHA1 of
"source:email:name:username" string. This string is case insensitive,
which means same values for the input parameters in upper
or lower case will produce the same UUID.
The value of 'name' will converted to its unaccent form which means
same values with accent or unnacent chars (i.e 'ö and o') will
generate the same UUID.
For instance, these combinations will produce the same UUID:
('scm', '[email protected]', 'John Smith', 'jsmith'),
('scm', 'jsmith@example,com', 'Jöhn Smith', 'jsmith'),
('scm', '[email protected]', 'John Smith', 'JSMITH'),
('scm', '[email protected]', 'john Smith', 'jsmith')
:param source: data source
:param email: email of the identity
:param name: full name of the identity
:param username: user name used by the identity
:returns: a universal unique identifier for Sorting Hat
:raises ValueError: when source is None or empty; each one of the
parameters is None; parameters are empty.
"""
if source is None:
raise ValueError("source cannot be None")
if source == '':
raise ValueError("source cannot be an empty string")
if not (email or name or username):
raise ValueError("identity data cannot be None or empty")
s = ':'.join((to_unicode(source),
to_unicode(email),
to_unicode(name, unaccent=True),
to_unicode(username))).lower()
sha1 = hashlib.sha1(s.encode('UTF-8', errors="surrogateescape"))
uuid_ = sha1.hexdigest()
return uuid_ |
0, module; 1, function_definition; 2, function_name:check; 3, parameters; 4, block; 5, identifier:labels; 6, expression_statement; 7, comment:# type checking; 8, if_statement; 9, if_statement; 10, if_statement; 11, comment:# dimension checking; 12, expression_statement; 13, if_statement; 14, if_statement; 15, if_statement; 16, comment:# sort checking; 17, for_statement; 18, comment:"""Raise IOError if labels are not correct
`labels` must be a list of sorted numpy arrays of equal
dimensions (must be 1D or 2D). In the case of 2D labels,
the second axis must have the same shape for all labels.
"""; 19, not_operator; 20, block; 21, not_operator; 22, block; 23, not_operator; 24, block; 25, assignment; 26, comparison_operator:ndim not in [1, 2]; 27, block; 28, not_operator; 29, block; 30, comparison_operator:ndim == 2; 31, block; 32, identifier:label; 33, identifier:labels; 34, block; 35, call; 36, raise_statement; 37, call; 38, raise_statement; 39, call; 40, raise_statement; 41, identifier:ndim; 42, attribute; 43, identifier:ndim; 44, list; 45, raise_statement; 46, call; 47, raise_statement; 48, identifier:ndim; 49, integer:2; 50, expression_statement; 51, if_statement; 52, expression_statement; 53, comment:# print label, index; 54, comment:# print len(index), label.shape[0]; 55, assert_statement; 56, if_statement; 57, identifier:isinstance; 58, argument_list; 59, call; 60, identifier:len; 61, argument_list; 62, call; 63, identifier:all; 64, argument_list; 65, call; 66, subscript; 67, identifier:ndim; 68, integer:1; 69, integer:2; 70, call; 71, identifier:all; 72, argument_list; 73, call; 74, assignment; 75, not_operator; 76, block; 77, assignment; 78, comparison_operator:len(index) == label.shape[0]; 79, not_operator; 80, block; 81, identifier:labels; 82, identifier:list; 83, identifier:IOError; 84, argument_list; 85, identifier:labels; 86, identifier:IOError; 87, argument_list; 88, list_comprehension; 89, identifier:IOError; 90, argument_list; 91, identifier:labels; 92, integer:0; 93, identifier:IOError; 94, argument_list; 95, list_comprehension; 96, identifier:IOError; 97, argument_list; 98, identifier:shape1; 99, subscript; 100, call; 101, raise_statement; 102, identifier:index; 103, parenthesized_expression; 104, call; 105, subscript; 106, call; 107, raise_statement; 108, string; 109, string; 110, call; 111, for_in_clause; 112, string; 113, string; 114, comparison_operator:l.ndim == ndim; 115, for_in_clause; 116, string; 117, attribute; 118, integer:1; 119, identifier:all; 120, argument_list; 121, call; 122, conditional_expression:np.argsort(label) if label.ndim == 1
else np.lexsort(label.T); 123, identifier:len; 124, argument_list; 125, attribute; 126, integer:0; 127, identifier:all; 128, generator_expression; 129, call; 130, string_content:labels are not in a list; 131, string_content:the labels list is empty; 132, identifier:isinstance; 133, argument_list; 134, identifier:l; 135, identifier:labels; 136, string_content:all labels must be numpy arrays; 137, string_content:labels dimension must be 1 or 2; 138, attribute; 139, identifier:ndim; 140, identifier:l; 141, identifier:labels; 142, string_content:all labels dimensions must be equal; 143, subscript; 144, identifier:shape; 145, list_comprehension; 146, identifier:IOError; 147, argument_list; 148, call; 149, comparison_operator:label.ndim == 1; 150, call; 151, identifier:index; 152, identifier:label; 153, identifier:shape; 154, comparison_operator:n == index[n]; 155, for_in_clause; 156, identifier:IOError; 157, argument_list; 158, identifier:l; 159, attribute; 160, identifier:l; 161, identifier:ndim; 162, identifier:labels; 163, integer:0; 164, comparison_operator:l.shape[1] == shape1; 165, for_in_clause; 166, string; 167, attribute; 168, argument_list; 169, attribute; 170, integer:1; 171, attribute; 172, argument_list; 173, identifier:n; 174, subscript; 175, identifier:n; 176, call; 177, string; 178, identifier:np; 179, identifier:ndarray; 180, subscript; 181, identifier:shape1; 182, identifier:l; 183, identifier:labels; 184, string_content:all labels must have same shape on 2nd dim; 185, identifier:np; 186, identifier:argsort; 187, identifier:label; 188, identifier:label; 189, identifier:ndim; 190, identifier:np; 191, identifier:lexsort; 192, attribute; 193, identifier:index; 194, identifier:n; 195, identifier:range; 196, argument_list; 197, string_content:labels are not sorted in increasing order; 198, attribute; 199, integer:1; 200, identifier:label; 201, identifier:T; 202, binary_operator:label.shape[0]-1; 203, identifier:l; 204, identifier:shape; 205, subscript; 206, integer:1; 207, attribute; 208, integer:0; 209, identifier:label; 210, identifier:shape | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 6, 18; 8, 19; 8, 20; 9, 21; 9, 22; 10, 23; 10, 24; 12, 25; 13, 26; 13, 27; 14, 28; 14, 29; 15, 30; 15, 31; 17, 32; 17, 33; 17, 34; 19, 35; 20, 36; 21, 37; 22, 38; 23, 39; 24, 40; 25, 41; 25, 42; 26, 43; 26, 44; 27, 45; 28, 46; 29, 47; 30, 48; 30, 49; 31, 50; 31, 51; 34, 52; 34, 53; 34, 54; 34, 55; 34, 56; 35, 57; 35, 58; 36, 59; 37, 60; 37, 61; 38, 62; 39, 63; 39, 64; 40, 65; 42, 66; 42, 67; 44, 68; 44, 69; 45, 70; 46, 71; 46, 72; 47, 73; 50, 74; 51, 75; 51, 76; 52, 77; 55, 78; 56, 79; 56, 80; 58, 81; 58, 82; 59, 83; 59, 84; 61, 85; 62, 86; 62, 87; 64, 88; 65, 89; 65, 90; 66, 91; 66, 92; 70, 93; 70, 94; 72, 95; 73, 96; 73, 97; 74, 98; 74, 99; 75, 100; 76, 101; 77, 102; 77, 103; 78, 104; 78, 105; 79, 106; 80, 107; 84, 108; 87, 109; 88, 110; 88, 111; 90, 112; 94, 113; 95, 114; 95, 115; 97, 116; 99, 117; 99, 118; 100, 119; 100, 120; 101, 121; 103, 122; 104, 123; 104, 124; 105, 125; 105, 126; 106, 127; 106, 128; 107, 129; 108, 130; 109, 131; 110, 132; 110, 133; 111, 134; 111, 135; 112, 136; 113, 137; 114, 138; 114, 139; 115, 140; 115, 141; 116, 142; 117, 143; 117, 144; 120, 145; 121, 146; 121, 147; 122, 148; 122, 149; 122, 150; 124, 151; 125, 152; 125, 153; 128, 154; 128, 155; 129, 156; 129, 157; 133, 158; 133, 159; 138, 160; 138, 161; 143, 162; 143, 163; 145, 164; 145, 165; 147, 166; 148, 167; 148, 168; 149, 169; 149, 170; 150, 171; 150, 172; 154, 173; 154, 174; 155, 175; 155, 176; 157, 177; 159, 178; 159, 179; 164, 180; 164, 181; 165, 182; 165, 183; 166, 184; 167, 185; 167, 186; 168, 187; 169, 188; 169, 189; 171, 190; 171, 191; 172, 192; 174, 193; 174, 194; 176, 195; 176, 196; 177, 197; 180, 198; 180, 199; 192, 200; 192, 201; 196, 202; 198, 203; 198, 204; 202, 205; 202, 206; 205, 207; 205, 208; 207, 209; 207, 210 | def check(labels):
"""Raise IOError if labels are not correct
`labels` must be a list of sorted numpy arrays of equal
dimensions (must be 1D or 2D). In the case of 2D labels,
the second axis must have the same shape for all labels.
"""
# type checking
if not isinstance(labels, list):
raise IOError('labels are not in a list')
if not len(labels):
raise IOError('the labels list is empty')
if not all([isinstance(l, np.ndarray) for l in labels]):
raise IOError('all labels must be numpy arrays')
# dimension checking
ndim = labels[0].ndim
if ndim not in [1, 2]:
raise IOError('labels dimension must be 1 or 2')
if not all([l.ndim == ndim for l in labels]):
raise IOError('all labels dimensions must be equal')
if ndim == 2:
shape1 = labels[0].shape[1]
if not all([l.shape[1] == shape1 for l in labels]):
raise IOError('all labels must have same shape on 2nd dim')
# sort checking
for label in labels:
index = (np.argsort(label) if label.ndim == 1
else np.lexsort(label.T))
# print label, index
# print len(index), label.shape[0]
assert len(index) == label.shape[0]
if not all(n == index[n] for n in range(label.shape[0]-1)):
raise IOError('labels are not sorted in increasing order') |
0, module; 1, function_definition; 2, function_name:to_items; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, expression_statement; 8, return_statement; 9, identifier:func; 10, identifier:str; 11, comment:"""
Contruct a list of dictionary items.
The items are normalized using:
- A sort function by key (for consistent results)
- A transformation function for values
The transformation function will default to `str`, which is a good choice when encoding values
as part of a response; this requires that complex types (UUID, Enum, etc.) have a valid string
encoding.
The transformation function should be set to `identity` in cases where raw values are desired;
this is normally necessary when passing page data to controller functions as kwargs.
"""; 12, list_comprehension; 13, tuple; 14, for_in_clause; 15, identifier:key; 16, call; 17, identifier:key; 18, call; 19, identifier:func; 20, argument_list; 21, identifier:sorted; 22, argument_list; 23, subscript; 24, call; 25, attribute; 26, identifier:key; 27, attribute; 28, argument_list; 29, identifier:self; 30, identifier:kwargs; 31, attribute; 32, identifier:keys; 33, identifier:self; 34, identifier:kwargs | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 6, 9; 6, 10; 7, 11; 8, 12; 12, 13; 12, 14; 13, 15; 13, 16; 14, 17; 14, 18; 16, 19; 16, 20; 18, 21; 18, 22; 20, 23; 22, 24; 23, 25; 23, 26; 24, 27; 24, 28; 25, 29; 25, 30; 27, 31; 27, 32; 31, 33; 31, 34 | def to_items(self, func=str):
"""
Contruct a list of dictionary items.
The items are normalized using:
- A sort function by key (for consistent results)
- A transformation function for values
The transformation function will default to `str`, which is a good choice when encoding values
as part of a response; this requires that complex types (UUID, Enum, etc.) have a valid string
encoding.
The transformation function should be set to `identity` in cases where raw values are desired;
this is normally necessary when passing page data to controller functions as kwargs.
"""
return [
(key, func(self.kwargs[key]))
for key in sorted(self.kwargs.keys())
] |
0, module; 1, function_definition; 2, function_name:SortBy; 3, parameters; 4, block; 5, list_splat_pattern; 6, expression_statement; 7, expression_statement; 8, for_statement; 9, return_statement; 10, identifier:qs; 11, comment:"""Convert a list of Q objects into list of sort instructions"""; 12, assignment; 13, identifier:q; 14, identifier:qs; 15, block; 16, identifier:sort; 17, identifier:sort; 18, list; 19, if_statement; 20, call; 21, block; 22, else_clause; 23, attribute; 24, argument_list; 25, expression_statement; 26, block; 27, attribute; 28, identifier:endswith; 29, string; 30, call; 31, expression_statement; 32, identifier:q; 33, identifier:_path; 34, string_content:.desc; 35, attribute; 36, argument_list; 37, call; 38, identifier:sort; 39, identifier:append; 40, tuple; 41, attribute; 42, argument_list; 43, subscript; 44, identifier:DESCENDING; 45, identifier:sort; 46, identifier:append; 47, tuple; 48, attribute; 49, slice; 50, attribute; 51, identifier:ASCENDING; 52, identifier:q; 53, identifier:_path; 54, unary_operator; 55, identifier:q; 56, identifier:_path; 57, integer:5 | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 5, 10; 6, 11; 7, 12; 8, 13; 8, 14; 8, 15; 9, 16; 12, 17; 12, 18; 15, 19; 19, 20; 19, 21; 19, 22; 20, 23; 20, 24; 21, 25; 22, 26; 23, 27; 23, 28; 24, 29; 25, 30; 26, 31; 27, 32; 27, 33; 29, 34; 30, 35; 30, 36; 31, 37; 35, 38; 35, 39; 36, 40; 37, 41; 37, 42; 40, 43; 40, 44; 41, 45; 41, 46; 42, 47; 43, 48; 43, 49; 47, 50; 47, 51; 48, 52; 48, 53; 49, 54; 50, 55; 50, 56; 54, 57 | def SortBy(*qs):
"""Convert a list of Q objects into list of sort instructions"""
sort = []
for q in qs:
if q._path.endswith('.desc'):
sort.append((q._path[:-5], DESCENDING))
else:
sort.append((q._path, ASCENDING))
return sort |
0, module; 1, function_definition; 2, function_name:toposorted; 3, parameters; 4, block; 5, identifier:nodes; 6, identifier:edges; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, for_statement; 11, expression_statement; 12, expression_statement; 13, while_statement; 14, return_statement; 15, comment:"""
Perform a topological sort on the input resources.
The topological sort uses Kahn's algorithm, which is a stable sort and will preserve this
ordering; note that a DFS will produce a worst case ordering from the perspective of batching.
"""; 16, assignment; 17, assignment; 18, identifier:edge; 19, identifier:edges; 20, block; 21, assignment; 22, assignment; 23, identifier:working_set; 24, block; 25, identifier:results; 26, identifier:incoming; 27, call; 28, identifier:outgoing; 29, call; 30, expression_statement; 31, expression_statement; 32, identifier:working_set; 33, call; 34, identifier:results; 35, list; 36, expression_statement; 37, for_statement; 38, if_statement; 39, expression_statement; 40, identifier:defaultdict; 41, argument_list; 42, identifier:defaultdict; 43, argument_list; 44, call; 45, call; 46, identifier:list; 47, argument_list; 48, assignment; 49, identifier:node; 50, identifier:working_set; 51, block; 52, comparison_operator:len(working_set) == len(remaining); 53, block; 54, assignment; 55, identifier:set; 56, identifier:set; 57, attribute; 58, argument_list; 59, attribute; 60, argument_list; 61, call; 62, identifier:remaining; 63, list; 64, if_statement; 65, expression_statement; 66, for_statement; 67, call; 68, call; 69, raise_statement; 70, identifier:working_set; 71, identifier:remaining; 72, subscript; 73, identifier:add; 74, attribute; 75, subscript; 76, identifier:add; 77, attribute; 78, attribute; 79, argument_list; 80, subscript; 81, comment:# node still has incoming edges; 82, block; 83, call; 84, identifier:child; 85, subscript; 86, block; 87, identifier:len; 88, argument_list; 89, identifier:len; 90, argument_list; 91, call; 92, identifier:incoming; 93, attribute; 94, identifier:edge; 95, identifier:from_id; 96, identifier:outgoing; 97, attribute; 98, identifier:edge; 99, identifier:to_id; 100, identifier:nodes; 101, identifier:values; 102, identifier:incoming; 103, attribute; 104, expression_statement; 105, continue_statement; 106, attribute; 107, argument_list; 108, identifier:outgoing; 109, attribute; 110, expression_statement; 111, identifier:working_set; 112, identifier:remaining; 113, identifier:Exception; 114, argument_list; 115, identifier:edge; 116, identifier:to_id; 117, identifier:edge; 118, identifier:from_id; 119, identifier:node; 120, identifier:id; 121, call; 122, identifier:results; 123, identifier:append; 124, identifier:node; 125, identifier:node; 126, identifier:id; 127, call; 128, string:"Cycle detected"; 129, attribute; 130, argument_list; 131, attribute; 132, argument_list; 133, identifier:remaining; 134, identifier:append; 135, identifier:node; 136, subscript; 137, identifier:remove; 138, attribute; 139, identifier:incoming; 140, identifier:child; 141, identifier:node; 142, identifier:id | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 7, 15; 8, 16; 9, 17; 10, 18; 10, 19; 10, 20; 11, 21; 12, 22; 13, 23; 13, 24; 14, 25; 16, 26; 16, 27; 17, 28; 17, 29; 20, 30; 20, 31; 21, 32; 21, 33; 22, 34; 22, 35; 24, 36; 24, 37; 24, 38; 24, 39; 27, 40; 27, 41; 29, 42; 29, 43; 30, 44; 31, 45; 33, 46; 33, 47; 36, 48; 37, 49; 37, 50; 37, 51; 38, 52; 38, 53; 39, 54; 41, 55; 43, 56; 44, 57; 44, 58; 45, 59; 45, 60; 47, 61; 48, 62; 48, 63; 51, 64; 51, 65; 51, 66; 52, 67; 52, 68; 53, 69; 54, 70; 54, 71; 57, 72; 57, 73; 58, 74; 59, 75; 59, 76; 60, 77; 61, 78; 61, 79; 64, 80; 64, 81; 64, 82; 65, 83; 66, 84; 66, 85; 66, 86; 67, 87; 67, 88; 68, 89; 68, 90; 69, 91; 72, 92; 72, 93; 74, 94; 74, 95; 75, 96; 75, 97; 77, 98; 77, 99; 78, 100; 78, 101; 80, 102; 80, 103; 82, 104; 82, 105; 83, 106; 83, 107; 85, 108; 85, 109; 86, 110; 88, 111; 90, 112; 91, 113; 91, 114; 93, 115; 93, 116; 97, 117; 97, 118; 103, 119; 103, 120; 104, 121; 106, 122; 106, 123; 107, 124; 109, 125; 109, 126; 110, 127; 114, 128; 121, 129; 121, 130; 127, 131; 127, 132; 129, 133; 129, 134; 130, 135; 131, 136; 131, 137; 132, 138; 136, 139; 136, 140; 138, 141; 138, 142 | def toposorted(nodes, edges):
"""
Perform a topological sort on the input resources.
The topological sort uses Kahn's algorithm, which is a stable sort and will preserve this
ordering; note that a DFS will produce a worst case ordering from the perspective of batching.
"""
incoming = defaultdict(set)
outgoing = defaultdict(set)
for edge in edges:
incoming[edge.to_id].add(edge.from_id)
outgoing[edge.from_id].add(edge.to_id)
working_set = list(nodes.values())
results = []
while working_set:
remaining = []
for node in working_set:
if incoming[node.id]:
# node still has incoming edges
remaining.append(node)
continue
results.append(node)
for child in outgoing[node.id]:
incoming[child].remove(node.id)
if len(working_set) == len(remaining):
raise Exception("Cycle detected")
working_set = remaining
return results |
0, module; 1, function_definition; 2, function_name:sorted_keywords_by_order; 3, parameters; 4, block; 5, identifier:keywords; 6, identifier:order; 7, expression_statement; 8, comment:# we need to delete item with no value; 9, for_statement; 10, expression_statement; 11, for_statement; 12, for_statement; 13, return_statement; 14, comment:"""Sort keywords based on defined order.
:param keywords: Keyword to be sorted.
:type keywords: dict
:param order: Ordered list of key.
:type order: list
:return: Ordered dictionary based on order list.
:rtype: OrderedDict
"""; 15, pattern_list; 16, call; 17, block; 18, assignment; 19, identifier:key; 20, identifier:order; 21, block; 22, identifier:keyword; 23, identifier:keywords; 24, block; 25, identifier:ordered_keywords; 26, identifier:key; 27, identifier:value; 28, identifier:list; 29, argument_list; 30, if_statement; 31, identifier:ordered_keywords; 32, call; 33, if_statement; 34, if_statement; 35, call; 36, comparison_operator:value is None; 37, block; 38, identifier:OrderedDict; 39, argument_list; 40, comparison_operator:key in list(keywords.keys()); 41, block; 42, comparison_operator:keyword not in order; 43, block; 44, attribute; 45, argument_list; 46, identifier:value; 47, None; 48, delete_statement; 49, identifier:key; 50, call; 51, expression_statement; 52, identifier:keyword; 53, identifier:order; 54, expression_statement; 55, identifier:keywords; 56, identifier:items; 57, subscript; 58, identifier:list; 59, argument_list; 60, assignment; 61, assignment; 62, identifier:keywords; 63, identifier:key; 64, call; 65, subscript; 66, call; 67, subscript; 68, call; 69, attribute; 70, argument_list; 71, identifier:ordered_keywords; 72, identifier:key; 73, attribute; 74, argument_list; 75, identifier:ordered_keywords; 76, identifier:keyword; 77, attribute; 78, argument_list; 79, identifier:keywords; 80, identifier:keys; 81, identifier:keywords; 82, identifier:get; 83, identifier:key; 84, identifier:keywords; 85, identifier:get; 86, identifier:keyword | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 7, 14; 9, 15; 9, 16; 9, 17; 10, 18; 11, 19; 11, 20; 11, 21; 12, 22; 12, 23; 12, 24; 13, 25; 15, 26; 15, 27; 16, 28; 16, 29; 17, 30; 18, 31; 18, 32; 21, 33; 24, 34; 29, 35; 30, 36; 30, 37; 32, 38; 32, 39; 33, 40; 33, 41; 34, 42; 34, 43; 35, 44; 35, 45; 36, 46; 36, 47; 37, 48; 40, 49; 40, 50; 41, 51; 42, 52; 42, 53; 43, 54; 44, 55; 44, 56; 48, 57; 50, 58; 50, 59; 51, 60; 54, 61; 57, 62; 57, 63; 59, 64; 60, 65; 60, 66; 61, 67; 61, 68; 64, 69; 64, 70; 65, 71; 65, 72; 66, 73; 66, 74; 67, 75; 67, 76; 68, 77; 68, 78; 69, 79; 69, 80; 73, 81; 73, 82; 74, 83; 77, 84; 77, 85; 78, 86 | def sorted_keywords_by_order(keywords, order):
"""Sort keywords based on defined order.
:param keywords: Keyword to be sorted.
:type keywords: dict
:param order: Ordered list of key.
:type order: list
:return: Ordered dictionary based on order list.
:rtype: OrderedDict
"""
# we need to delete item with no value
for key, value in list(keywords.items()):
if value is None:
del keywords[key]
ordered_keywords = OrderedDict()
for key in order:
if key in list(keywords.keys()):
ordered_keywords[key] = keywords.get(key)
for keyword in keywords:
if keyword not in order:
ordered_keywords[keyword] = keywords.get(keyword)
return ordered_keywords |
0, module; 1, function_definition; 2, function_name:get_layer_modes; 3, parameters; 4, block; 5, identifier:subcategory; 6, expression_statement; 7, expression_statement; 8, return_statement; 9, comment:"""Return all sorted layer modes from exposure or hazard.
:param subcategory: Hazard or Exposure key.
:type subcategory: str
:returns: List of layer modes definition.
:rtype: list
"""; 10, assignment; 11, call; 12, identifier:layer_modes; 13, subscript; 14, identifier:sorted; 15, argument_list; 16, call; 17, string; 18, identifier:layer_modes; 19, keyword_argument; 20, identifier:definition; 21, argument_list; 22, string_content:layer_modes; 23, identifier:key; 24, lambda; 25, identifier:subcategory; 26, lambda_parameters; 27, subscript; 28, identifier:k; 29, identifier:k; 30, string; 31, string_content:key | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 6, 9; 7, 10; 8, 11; 10, 12; 10, 13; 11, 14; 11, 15; 13, 16; 13, 17; 15, 18; 15, 19; 16, 20; 16, 21; 17, 22; 19, 23; 19, 24; 21, 25; 24, 26; 24, 27; 26, 28; 27, 29; 27, 30; 30, 31 | def get_layer_modes(subcategory):
"""Return all sorted layer modes from exposure or hazard.
:param subcategory: Hazard or Exposure key.
:type subcategory: str
:returns: List of layer modes definition.
:rtype: list
"""
layer_modes = definition(subcategory)['layer_modes']
return sorted(layer_modes, key=lambda k: k['key']) |
0, module; 1, function_definition; 2, function_name:add_ordered_combo_item; 3, parameters; 4, block; 5, identifier:combo; 6, identifier:text; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, expression_statement; 11, if_statement; 12, expression_statement; 13, for_statement; 14, comment:# otherwise just add it to the end; 15, if_statement; 16, identifier:data; 17, None; 18, identifier:count_selected_features; 19, None; 20, identifier:icon; 21, None; 22, comment:"""Add a combo item ensuring that all items are listed alphabetically.
Although QComboBox allows you to set an InsertAlphabetically enum
this only has effect when a user interactively adds combo items to
an editable combo. This we have this little function to ensure that
combos are always sorted alphabetically.
:param combo: Combo box receiving the new item.
:type combo: QComboBox
:param text: Display text for the combo.
:type text: str
:param data: Optional UserRole data to be associated with the item.
:type data: QVariant, str
:param count_selected_features: A count to display if the layer has some
selected features. Default to None, nothing will be displayed.
:type count_selected_features: None, int
:param icon: Icon to display in the combobox.
:type icon: QIcon
"""; 23, comparison_operator:count_selected_features is not None; 24, block; 25, assignment; 26, identifier:combo_index; 27, call; 28, block; 29, identifier:icon; 30, block; 31, else_clause; 32, identifier:count_selected_features; 33, None; 34, expression_statement; 35, identifier:size; 36, call; 37, identifier:range; 38, argument_list; 39, expression_statement; 40, comment:# see if text alphabetically precedes item_text; 41, if_statement; 42, expression_statement; 43, block; 44, augmented_assignment; 45, attribute; 46, argument_list; 47, integer:0; 48, identifier:size; 49, assignment; 50, comparison_operator:cmp(text.lower(), item_text.lower()) < 0; 51, block; 52, call; 53, expression_statement; 54, identifier:text; 55, binary_operator:' (' + tr('{count} selected features').format(
count=count_selected_features) + ')'; 56, identifier:combo; 57, identifier:count; 58, identifier:item_text; 59, call; 60, call; 61, integer:0; 62, if_statement; 63, return_statement; 64, attribute; 65, argument_list; 66, call; 67, binary_operator:' (' + tr('{count} selected features').format(
count=count_selected_features); 68, string; 69, attribute; 70, argument_list; 71, identifier:cmp; 72, argument_list; 73, identifier:icon; 74, block; 75, else_clause; 76, identifier:combo; 77, identifier:insertItem; 78, identifier:size; 79, identifier:icon; 80, identifier:text; 81, identifier:data; 82, attribute; 83, argument_list; 84, string; 85, call; 86, string_content:); 87, identifier:combo; 88, identifier:itemText; 89, identifier:combo_index; 90, call; 91, call; 92, expression_statement; 93, block; 94, identifier:combo; 95, identifier:insertItem; 96, identifier:size; 97, identifier:text; 98, identifier:data; 99, string_content:(; 100, attribute; 101, argument_list; 102, attribute; 103, argument_list; 104, attribute; 105, argument_list; 106, call; 107, expression_statement; 108, call; 109, identifier:format; 110, keyword_argument; 111, identifier:text; 112, identifier:lower; 113, identifier:item_text; 114, identifier:lower; 115, attribute; 116, argument_list; 117, call; 118, identifier:tr; 119, argument_list; 120, identifier:count; 121, identifier:count_selected_features; 122, identifier:combo; 123, identifier:insertItem; 124, identifier:combo_index; 125, identifier:icon; 126, identifier:text; 127, identifier:data; 128, attribute; 129, argument_list; 130, string; 131, identifier:combo; 132, identifier:insertItem; 133, identifier:combo_index; 134, identifier:text; 135, identifier:data; 136, string_content:{count} selected features | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 7, 16; 7, 17; 8, 18; 8, 19; 9, 20; 9, 21; 10, 22; 11, 23; 11, 24; 12, 25; 13, 26; 13, 27; 13, 28; 15, 29; 15, 30; 15, 31; 23, 32; 23, 33; 24, 34; 25, 35; 25, 36; 27, 37; 27, 38; 28, 39; 28, 40; 28, 41; 30, 42; 31, 43; 34, 44; 36, 45; 36, 46; 38, 47; 38, 48; 39, 49; 41, 50; 41, 51; 42, 52; 43, 53; 44, 54; 44, 55; 45, 56; 45, 57; 49, 58; 49, 59; 50, 60; 50, 61; 51, 62; 51, 63; 52, 64; 52, 65; 53, 66; 55, 67; 55, 68; 59, 69; 59, 70; 60, 71; 60, 72; 62, 73; 62, 74; 62, 75; 64, 76; 64, 77; 65, 78; 65, 79; 65, 80; 65, 81; 66, 82; 66, 83; 67, 84; 67, 85; 68, 86; 69, 87; 69, 88; 70, 89; 72, 90; 72, 91; 74, 92; 75, 93; 82, 94; 82, 95; 83, 96; 83, 97; 83, 98; 84, 99; 85, 100; 85, 101; 90, 102; 90, 103; 91, 104; 91, 105; 92, 106; 93, 107; 100, 108; 100, 109; 101, 110; 102, 111; 102, 112; 104, 113; 104, 114; 106, 115; 106, 116; 107, 117; 108, 118; 108, 119; 110, 120; 110, 121; 115, 122; 115, 123; 116, 124; 116, 125; 116, 126; 116, 127; 117, 128; 117, 129; 119, 130; 128, 131; 128, 132; 129, 133; 129, 134; 129, 135; 130, 136 | def add_ordered_combo_item(
combo, text, data=None, count_selected_features=None, icon=None):
"""Add a combo item ensuring that all items are listed alphabetically.
Although QComboBox allows you to set an InsertAlphabetically enum
this only has effect when a user interactively adds combo items to
an editable combo. This we have this little function to ensure that
combos are always sorted alphabetically.
:param combo: Combo box receiving the new item.
:type combo: QComboBox
:param text: Display text for the combo.
:type text: str
:param data: Optional UserRole data to be associated with the item.
:type data: QVariant, str
:param count_selected_features: A count to display if the layer has some
selected features. Default to None, nothing will be displayed.
:type count_selected_features: None, int
:param icon: Icon to display in the combobox.
:type icon: QIcon
"""
if count_selected_features is not None:
text += ' (' + tr('{count} selected features').format(
count=count_selected_features) + ')'
size = combo.count()
for combo_index in range(0, size):
item_text = combo.itemText(combo_index)
# see if text alphabetically precedes item_text
if cmp(text.lower(), item_text.lower()) < 0:
if icon:
combo.insertItem(combo_index, icon, text, data)
else:
combo.insertItem(combo_index, text, data)
return
# otherwise just add it to the end
if icon:
combo.insertItem(size, icon, text, data)
else:
combo.insertItem(size, text, data) |
0, module; 1, function_definition; 2, function_name:sort; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, comment:"""Sort list elements by ID."""; 9, call; 10, attribute; 11, argument_list; 12, call; 13, identifier:sort; 14, keyword_argument; 15, identifier:super; 16, argument_list; 17, identifier:key; 18, lambda; 19, identifier:JSSObjectList; 20, identifier:self; 21, lambda_parameters; 22, attribute; 23, identifier:k; 24, identifier:k; 25, identifier:id | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 6, 8; 7, 9; 9, 10; 9, 11; 10, 12; 10, 13; 11, 14; 12, 15; 12, 16; 14, 17; 14, 18; 16, 19; 16, 20; 18, 21; 18, 22; 21, 23; 22, 24; 22, 25 | def sort(self):
"""Sort list elements by ID."""
super(JSSObjectList, self).sort(key=lambda k: k.id) |
0, module; 1, function_definition; 2, function_name:sort_by_name; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, comment:"""Sort list elements by name."""; 9, call; 10, attribute; 11, argument_list; 12, call; 13, identifier:sort; 14, keyword_argument; 15, identifier:super; 16, argument_list; 17, identifier:key; 18, lambda; 19, identifier:JSSObjectList; 20, identifier:self; 21, lambda_parameters; 22, attribute; 23, identifier:k; 24, identifier:k; 25, identifier:name | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 6, 8; 7, 9; 9, 10; 9, 11; 10, 12; 10, 13; 11, 14; 12, 15; 12, 16; 14, 17; 14, 18; 16, 19; 16, 20; 18, 21; 18, 22; 21, 23; 22, 24; 22, 25 | def sort_by_name(self):
"""Sort list elements by name."""
super(JSSObjectList, self).sort(key=lambda k: k.name) |
0, module; 1, function_definition; 2, function_name:find_sorted_task_dependencies; 3, parameters; 4, block; 5, identifier:task; 6, identifier:task_name; 7, identifier:task_id; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, comment:# make sure we deal with the decision task first, or we may populate; 17, comment:# signing:build0:decision before signing:decision; 18, expression_statement; 19, expression_statement; 20, expression_statement; 21, return_statement; 22, comment:"""Find the taskIds of the chain of trust dependencies of a given task.
Args:
task (dict): the task definition to inspect.
task_name (str): the name of the task, for logging and naming children.
task_id (str): the taskId of the task.
Returns:
list: tuples associating dependent task ``name`` to dependent task ``taskId``.
"""; 23, call; 24, assignment; 25, assignment; 26, assignment; 27, assignment; 28, assignment; 29, assignment; 30, assignment; 31, call; 32, call; 33, identifier:dependencies; 34, attribute; 35, argument_list; 36, identifier:cot_input_dependencies; 37, list_comprehension; 38, identifier:upstream_artifacts_dependencies; 39, list_comprehension; 40, identifier:dependencies; 41, list; 42, identifier:dependencies; 43, call; 44, identifier:parent_task_id; 45, boolean_operator; 46, identifier:parent_task_type; 47, string; 48, identifier:parent_tuple; 49, call; 50, attribute; 51, argument_list; 52, attribute; 53, argument_list; 54, identifier:log; 55, identifier:info; 56, call; 57, call; 58, for_in_clause; 59, call; 60, for_in_clause; 61, list_splat; 62, list_splat; 63, identifier:_sort_dependencies_by_name_then_task_id; 64, argument_list; 65, call; 66, call; 67, string_content:parent; 68, identifier:_craft_dependency_tuple; 69, argument_list; 70, identifier:dependencies; 71, identifier:insert; 72, integer:0; 73, identifier:parent_tuple; 74, identifier:log; 75, identifier:info; 76, call; 77, attribute; 78, argument_list; 79, identifier:_craft_dependency_tuple; 80, argument_list; 81, pattern_list; 82, call; 83, identifier:_craft_dependency_tuple; 84, argument_list; 85, identifier:artifact_dict; 86, call; 87, identifier:cot_input_dependencies; 88, identifier:upstream_artifacts_dependencies; 89, identifier:dependencies; 90, identifier:get_parent_task_id; 91, argument_list; 92, identifier:get_decision_task_id; 93, argument_list; 94, identifier:task_name; 95, identifier:parent_task_type; 96, identifier:parent_task_id; 97, attribute; 98, argument_list; 99, string:"find_sorted_task_dependencies {} {}"; 100, identifier:format; 101, identifier:task_name; 102, identifier:task_id; 103, identifier:task_name; 104, identifier:task_type; 105, identifier:task_id; 106, identifier:task_type; 107, identifier:task_id; 108, attribute; 109, argument_list; 110, identifier:task_name; 111, subscript; 112, subscript; 113, attribute; 114, argument_list; 115, identifier:task; 116, identifier:task; 117, string; 118, identifier:format; 119, identifier:dependencies; 120, call; 121, identifier:items; 122, identifier:artifact_dict; 123, string; 124, identifier:artifact_dict; 125, string; 126, call; 127, identifier:get; 128, string; 129, list; 130, string_content:found dependencies: {}; 131, attribute; 132, argument_list; 133, string_content:taskType; 134, string_content:taskId; 135, attribute; 136, argument_list; 137, string_content:upstreamArtifacts; 138, call; 139, identifier:get; 140, string; 141, dictionary; 142, identifier:task; 143, identifier:get; 144, string; 145, dictionary; 146, attribute; 147, argument_list; 148, string_content:inputs; 149, string_content:payload; 150, subscript; 151, identifier:get; 152, string; 153, dictionary; 154, identifier:task; 155, string; 156, string_content:chainOfTrust; 157, string_content:extra | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 8, 22; 9, 23; 10, 24; 11, 25; 12, 26; 13, 27; 14, 28; 15, 29; 18, 30; 19, 31; 20, 32; 21, 33; 23, 34; 23, 35; 24, 36; 24, 37; 25, 38; 25, 39; 26, 40; 26, 41; 27, 42; 27, 43; 28, 44; 28, 45; 29, 46; 29, 47; 30, 48; 30, 49; 31, 50; 31, 51; 32, 52; 32, 53; 34, 54; 34, 55; 35, 56; 37, 57; 37, 58; 39, 59; 39, 60; 41, 61; 41, 62; 43, 63; 43, 64; 45, 65; 45, 66; 47, 67; 49, 68; 49, 69; 50, 70; 50, 71; 51, 72; 51, 73; 52, 74; 52, 75; 53, 76; 56, 77; 56, 78; 57, 79; 57, 80; 58, 81; 58, 82; 59, 83; 59, 84; 60, 85; 60, 86; 61, 87; 62, 88; 64, 89; 65, 90; 65, 91; 66, 92; 66, 93; 69, 94; 69, 95; 69, 96; 76, 97; 76, 98; 77, 99; 77, 100; 78, 101; 78, 102; 80, 103; 80, 104; 80, 105; 81, 106; 81, 107; 82, 108; 82, 109; 84, 110; 84, 111; 84, 112; 86, 113; 86, 114; 91, 115; 93, 116; 97, 117; 97, 118; 98, 119; 108, 120; 108, 121; 111, 122; 111, 123; 112, 124; 112, 125; 113, 126; 113, 127; 114, 128; 114, 129; 117, 130; 120, 131; 120, 132; 123, 133; 125, 134; 126, 135; 126, 136; 128, 137; 131, 138; 131, 139; 132, 140; 132, 141; 135, 142; 135, 143; 136, 144; 136, 145; 138, 146; 138, 147; 140, 148; 144, 149; 146, 150; 146, 151; 147, 152; 147, 153; 150, 154; 150, 155; 152, 156; 155, 157 | def find_sorted_task_dependencies(task, task_name, task_id):
"""Find the taskIds of the chain of trust dependencies of a given task.
Args:
task (dict): the task definition to inspect.
task_name (str): the name of the task, for logging and naming children.
task_id (str): the taskId of the task.
Returns:
list: tuples associating dependent task ``name`` to dependent task ``taskId``.
"""
log.info("find_sorted_task_dependencies {} {}".format(task_name, task_id))
cot_input_dependencies = [
_craft_dependency_tuple(task_name, task_type, task_id)
for task_type, task_id in task['extra'].get('chainOfTrust', {}).get('inputs', {}).items()
]
upstream_artifacts_dependencies = [
_craft_dependency_tuple(task_name, artifact_dict['taskType'], artifact_dict['taskId'])
for artifact_dict in task.get('payload', {}).get('upstreamArtifacts', [])
]
dependencies = [*cot_input_dependencies, *upstream_artifacts_dependencies]
dependencies = _sort_dependencies_by_name_then_task_id(dependencies)
parent_task_id = get_parent_task_id(task) or get_decision_task_id(task)
parent_task_type = 'parent'
# make sure we deal with the decision task first, or we may populate
# signing:build0:decision before signing:decision
parent_tuple = _craft_dependency_tuple(task_name, parent_task_type, parent_task_id)
dependencies.insert(0, parent_tuple)
log.info('found dependencies: {}'.format(dependencies))
return dependencies |
0, module; 1, function_definition; 2, function_name:get_all_artifacts_per_task_id; 3, parameters; 4, block; 5, identifier:chain; 6, identifier:upstream_artifacts; 7, expression_statement; 8, expression_statement; 9, for_statement; 10, if_statement; 11, comment:# Avoid duplicate paths per task_id; 12, for_statement; 13, return_statement; 14, comment:"""Return every artifact to download, including the Chain Of Trust Artifacts.
Args:
chain (ChainOfTrust): the chain of trust object
upstream_artifacts: the list of upstream artifact definitions
Returns:
dict: sorted list of paths to downloaded artifacts ordered by taskId
"""; 15, assignment; 16, identifier:link; 17, attribute; 18, comment:# Download task-graph.json for decision+action task cot verification; 19, block; 20, identifier:upstream_artifacts; 21, block; 22, pattern_list; 23, call; 24, block; 25, identifier:all_artifacts_per_task_id; 26, identifier:all_artifacts_per_task_id; 27, dictionary; 28, identifier:chain; 29, identifier:links; 30, if_statement; 31, comment:# Download actions.json for decision+action task cot verification; 32, if_statement; 33, for_statement; 34, identifier:task_id; 35, identifier:paths; 36, attribute; 37, argument_list; 38, expression_statement; 39, comparison_operator:link.task_type in PARENT_TASK_TYPES; 40, block; 41, comparison_operator:link.task_type in DECISION_TASK_TYPES; 42, block; 43, identifier:upstream_dict; 44, identifier:upstream_artifacts; 45, block; 46, identifier:all_artifacts_per_task_id; 47, identifier:items; 48, assignment; 49, attribute; 50, identifier:PARENT_TASK_TYPES; 51, expression_statement; 52, attribute; 53, identifier:DECISION_TASK_TYPES; 54, expression_statement; 55, expression_statement; 56, expression_statement; 57, subscript; 58, call; 59, identifier:link; 60, identifier:task_type; 61, call; 62, identifier:link; 63, identifier:task_type; 64, call; 65, call; 66, call; 67, identifier:all_artifacts_per_task_id; 68, identifier:task_id; 69, identifier:sorted; 70, argument_list; 71, identifier:add_enumerable_item_to_dict; 72, argument_list; 73, identifier:add_enumerable_item_to_dict; 74, argument_list; 75, identifier:add_enumerable_item_to_dict; 76, argument_list; 77, identifier:add_enumerable_item_to_dict; 78, argument_list; 79, call; 80, keyword_argument; 81, keyword_argument; 82, keyword_argument; 83, keyword_argument; 84, keyword_argument; 85, keyword_argument; 86, keyword_argument; 87, keyword_argument; 88, keyword_argument; 89, keyword_argument; 90, keyword_argument; 91, keyword_argument; 92, identifier:set; 93, argument_list; 94, identifier:dict_; 95, identifier:all_artifacts_per_task_id; 96, identifier:key; 97, attribute; 98, identifier:item; 99, string; 100, identifier:dict_; 101, identifier:all_artifacts_per_task_id; 102, identifier:key; 103, attribute; 104, identifier:item; 105, string; 106, identifier:dict_; 107, identifier:all_artifacts_per_task_id; 108, identifier:key; 109, attribute; 110, identifier:item; 111, string; 112, identifier:dict_; 113, identifier:all_artifacts_per_task_id; 114, identifier:key; 115, subscript; 116, identifier:item; 117, subscript; 118, identifier:paths; 119, identifier:link; 120, identifier:task_id; 121, string_content:public/task-graph.json; 122, identifier:link; 123, identifier:task_id; 124, string_content:public/actions.json; 125, identifier:link; 126, identifier:task_id; 127, string_content:public/parameters.yml; 128, identifier:upstream_dict; 129, string; 130, identifier:upstream_dict; 131, string; 132, string_content:taskId; 133, string_content:paths | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 7, 14; 8, 15; 9, 16; 9, 17; 9, 18; 9, 19; 10, 20; 10, 21; 12, 22; 12, 23; 12, 24; 13, 25; 15, 26; 15, 27; 17, 28; 17, 29; 19, 30; 19, 31; 19, 32; 21, 33; 22, 34; 22, 35; 23, 36; 23, 37; 24, 38; 30, 39; 30, 40; 32, 41; 32, 42; 33, 43; 33, 44; 33, 45; 36, 46; 36, 47; 38, 48; 39, 49; 39, 50; 40, 51; 41, 52; 41, 53; 42, 54; 42, 55; 45, 56; 48, 57; 48, 58; 49, 59; 49, 60; 51, 61; 52, 62; 52, 63; 54, 64; 55, 65; 56, 66; 57, 67; 57, 68; 58, 69; 58, 70; 61, 71; 61, 72; 64, 73; 64, 74; 65, 75; 65, 76; 66, 77; 66, 78; 70, 79; 72, 80; 72, 81; 72, 82; 74, 83; 74, 84; 74, 85; 76, 86; 76, 87; 76, 88; 78, 89; 78, 90; 78, 91; 79, 92; 79, 93; 80, 94; 80, 95; 81, 96; 81, 97; 82, 98; 82, 99; 83, 100; 83, 101; 84, 102; 84, 103; 85, 104; 85, 105; 86, 106; 86, 107; 87, 108; 87, 109; 88, 110; 88, 111; 89, 112; 89, 113; 90, 114; 90, 115; 91, 116; 91, 117; 93, 118; 97, 119; 97, 120; 99, 121; 103, 122; 103, 123; 105, 124; 109, 125; 109, 126; 111, 127; 115, 128; 115, 129; 117, 130; 117, 131; 129, 132; 131, 133 | def get_all_artifacts_per_task_id(chain, upstream_artifacts):
"""Return every artifact to download, including the Chain Of Trust Artifacts.
Args:
chain (ChainOfTrust): the chain of trust object
upstream_artifacts: the list of upstream artifact definitions
Returns:
dict: sorted list of paths to downloaded artifacts ordered by taskId
"""
all_artifacts_per_task_id = {}
for link in chain.links:
# Download task-graph.json for decision+action task cot verification
if link.task_type in PARENT_TASK_TYPES:
add_enumerable_item_to_dict(
dict_=all_artifacts_per_task_id, key=link.task_id, item='public/task-graph.json'
)
# Download actions.json for decision+action task cot verification
if link.task_type in DECISION_TASK_TYPES:
add_enumerable_item_to_dict(
dict_=all_artifacts_per_task_id, key=link.task_id, item='public/actions.json'
)
add_enumerable_item_to_dict(
dict_=all_artifacts_per_task_id, key=link.task_id, item='public/parameters.yml'
)
if upstream_artifacts:
for upstream_dict in upstream_artifacts:
add_enumerable_item_to_dict(
dict_=all_artifacts_per_task_id, key=upstream_dict['taskId'], item=upstream_dict['paths']
)
# Avoid duplicate paths per task_id
for task_id, paths in all_artifacts_per_task_id.items():
all_artifacts_per_task_id[task_id] = sorted(set(paths))
return all_artifacts_per_task_id |
0, module; 1, function_definition; 2, function_name:get_upstream_artifacts_full_paths_per_task_id; 3, parameters; 4, block; 5, identifier:context; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, for_statement; 13, return_statement; 14, comment:"""List the downloaded upstream artifacts.
Args:
context (scriptworker.context.Context): the scriptworker context.
Returns:
dict, dict: lists of the paths to upstream artifacts, sorted by task_id.
First dict represents the existing upstream artifacts. The second one
maps the optional artifacts that couldn't be downloaded
Raises:
scriptworker.exceptions.ScriptWorkerTaskException: when an artifact doesn't exist.
"""; 15, assignment; 16, assignment; 17, assignment; 18, assignment; 19, assignment; 20, pattern_list; 21, identifier:task_ids_and_relative_paths; 22, block; 23, expression_list; 24, identifier:upstream_artifacts; 25, subscript; 26, identifier:task_ids_and_relative_paths; 27, list_comprehension; 28, identifier:optional_artifacts_per_task_id; 29, call; 30, identifier:upstream_artifacts_full_paths_per_task_id; 31, dictionary; 32, identifier:failed_paths_per_task_id; 33, dictionary; 34, identifier:task_id; 35, identifier:paths; 36, for_statement; 37, identifier:upstream_artifacts_full_paths_per_task_id; 38, identifier:failed_paths_per_task_id; 39, subscript; 40, string; 41, tuple; 42, for_in_clause; 43, identifier:get_optional_artifacts_per_task_id; 44, argument_list; 45, identifier:path; 46, identifier:paths; 47, block; 48, attribute; 49, string; 50, string_content:upstreamArtifacts; 51, subscript; 52, subscript; 53, identifier:artifact_definition; 54, identifier:upstream_artifacts; 55, identifier:upstream_artifacts; 56, try_statement; 57, identifier:context; 58, identifier:task; 59, string_content:payload; 60, identifier:artifact_definition; 61, string; 62, identifier:artifact_definition; 63, string; 64, block; 65, except_clause; 66, string_content:taskId; 67, string_content:paths; 68, expression_statement; 69, expression_statement; 70, identifier:ScriptWorkerTaskException; 71, block; 72, assignment; 73, call; 74, if_statement; 75, identifier:path_to_add; 76, call; 77, identifier:add_enumerable_item_to_dict; 78, argument_list; 79, comparison_operator:path in optional_artifacts_per_task_id.get(task_id, []); 80, block; 81, else_clause; 82, identifier:get_and_check_single_upstream_artifact_full_path; 83, argument_list; 84, keyword_argument; 85, keyword_argument; 86, keyword_argument; 87, identifier:path; 88, call; 89, expression_statement; 90, expression_statement; 91, block; 92, identifier:context; 93, identifier:task_id; 94, identifier:path; 95, identifier:dict_; 96, identifier:upstream_artifacts_full_paths_per_task_id; 97, identifier:key; 98, identifier:task_id; 99, identifier:item; 100, identifier:path_to_add; 101, attribute; 102, argument_list; 103, call; 104, call; 105, raise_statement; 106, identifier:optional_artifacts_per_task_id; 107, identifier:get; 108, identifier:task_id; 109, list; 110, attribute; 111, argument_list; 112, identifier:add_enumerable_item_to_dict; 113, argument_list; 114, identifier:log; 115, identifier:warning; 116, call; 117, keyword_argument; 118, keyword_argument; 119, keyword_argument; 120, attribute; 121, argument_list; 122, identifier:dict_; 123, identifier:failed_paths_per_task_id; 124, identifier:key; 125, identifier:task_id; 126, identifier:item; 127, identifier:path; 128, string:'Optional artifact "{}" of task "{}" not found'; 129, identifier:format; 130, identifier:path; 131, identifier:task_id | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 6, 14; 7, 15; 8, 16; 9, 17; 10, 18; 11, 19; 12, 20; 12, 21; 12, 22; 13, 23; 15, 24; 15, 25; 16, 26; 16, 27; 17, 28; 17, 29; 18, 30; 18, 31; 19, 32; 19, 33; 20, 34; 20, 35; 22, 36; 23, 37; 23, 38; 25, 39; 25, 40; 27, 41; 27, 42; 29, 43; 29, 44; 36, 45; 36, 46; 36, 47; 39, 48; 39, 49; 40, 50; 41, 51; 41, 52; 42, 53; 42, 54; 44, 55; 47, 56; 48, 57; 48, 58; 49, 59; 51, 60; 51, 61; 52, 62; 52, 63; 56, 64; 56, 65; 61, 66; 63, 67; 64, 68; 64, 69; 65, 70; 65, 71; 68, 72; 69, 73; 71, 74; 72, 75; 72, 76; 73, 77; 73, 78; 74, 79; 74, 80; 74, 81; 76, 82; 76, 83; 78, 84; 78, 85; 78, 86; 79, 87; 79, 88; 80, 89; 80, 90; 81, 91; 83, 92; 83, 93; 83, 94; 84, 95; 84, 96; 85, 97; 85, 98; 86, 99; 86, 100; 88, 101; 88, 102; 89, 103; 90, 104; 91, 105; 101, 106; 101, 107; 102, 108; 102, 109; 103, 110; 103, 111; 104, 112; 104, 113; 110, 114; 110, 115; 111, 116; 113, 117; 113, 118; 113, 119; 116, 120; 116, 121; 117, 122; 117, 123; 118, 124; 118, 125; 119, 126; 119, 127; 120, 128; 120, 129; 121, 130; 121, 131 | def get_upstream_artifacts_full_paths_per_task_id(context):
"""List the downloaded upstream artifacts.
Args:
context (scriptworker.context.Context): the scriptworker context.
Returns:
dict, dict: lists of the paths to upstream artifacts, sorted by task_id.
First dict represents the existing upstream artifacts. The second one
maps the optional artifacts that couldn't be downloaded
Raises:
scriptworker.exceptions.ScriptWorkerTaskException: when an artifact doesn't exist.
"""
upstream_artifacts = context.task['payload']['upstreamArtifacts']
task_ids_and_relative_paths = [
(artifact_definition['taskId'], artifact_definition['paths'])
for artifact_definition in upstream_artifacts
]
optional_artifacts_per_task_id = get_optional_artifacts_per_task_id(upstream_artifacts)
upstream_artifacts_full_paths_per_task_id = {}
failed_paths_per_task_id = {}
for task_id, paths in task_ids_and_relative_paths:
for path in paths:
try:
path_to_add = get_and_check_single_upstream_artifact_full_path(context, task_id, path)
add_enumerable_item_to_dict(
dict_=upstream_artifacts_full_paths_per_task_id,
key=task_id, item=path_to_add
)
except ScriptWorkerTaskException:
if path in optional_artifacts_per_task_id.get(task_id, []):
log.warning('Optional artifact "{}" of task "{}" not found'.format(path, task_id))
add_enumerable_item_to_dict(
dict_=failed_paths_per_task_id,
key=task_id, item=path
)
else:
raise
return upstream_artifacts_full_paths_per_task_id, failed_paths_per_task_id |
0, module; 1, function_definition; 2, function_name:_PreparedData; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, expression_statement; 8, if_statement; 9, expression_statement; 10, if_statement; 11, for_statement; 12, return_statement; 13, identifier:order_by; 14, tuple; 15, comment:"""Prepares the data for enumeration - sorting it by order_by.
Args:
order_by: Optional. Specifies the name of the column(s) to sort by, and
(optionally) which direction to sort in. Default sort direction
is asc. Following formats are accepted:
"string_col_name" -- For a single key in default (asc) order.
("string_col_name", "asc|desc") -- For a single key.
[("col_1","asc|desc"), ("col_2","asc|desc")] -- For more than
one column, an array of tuples of (col_name, "asc|desc").
Returns:
The data sorted by the keys given.
Raises:
DataTableException: Sort direction not in 'asc' or 'desc'
"""; 16, not_operator; 17, block; 18, assignment; 19, boolean_operator; 20, block; 21, identifier:key; 22, call; 23, block; 24, identifier:sorted_data; 25, identifier:order_by; 26, return_statement; 27, identifier:sorted_data; 28, subscript; 29, call; 30, parenthesized_expression; 31, expression_statement; 32, identifier:reversed; 33, argument_list; 34, if_statement; 35, attribute; 36, attribute; 37, slice; 38, identifier:isinstance; 39, argument_list; 40, boolean_operator; 41, assignment; 42, identifier:order_by; 43, call; 44, block; 45, elif_clause; 46, else_clause; 47, identifier:self; 48, identifier:__data; 49, identifier:self; 50, identifier:__data; 51, identifier:order_by; 52, attribute; 53, boolean_operator; 54, comparison_operator:order_by[1].lower() in ["asc", "desc"]; 55, identifier:order_by; 56, tuple; 57, identifier:isinstance; 58, argument_list; 59, expression_statement; 60, parenthesized_expression; 61, block; 62, block; 63, identifier:six; 64, identifier:string_types; 65, call; 66, comparison_operator:len(order_by) == 2; 67, call; 68, list; 69, identifier:order_by; 70, identifier:key; 71, attribute; 72, call; 73, boolean_operator; 74, expression_statement; 75, expression_statement; 76, raise_statement; 77, identifier:isinstance; 78, argument_list; 79, call; 80, integer:2; 81, attribute; 82, argument_list; 83, string:"asc"; 84, string:"desc"; 85, identifier:six; 86, identifier:string_types; 87, attribute; 88, argument_list; 89, boolean_operator; 90, comparison_operator:key[1].lower() in ("asc", "desc"); 91, assignment; 92, call; 93, call; 94, identifier:order_by; 95, identifier:tuple; 96, identifier:len; 97, argument_list; 98, subscript; 99, identifier:lower; 100, identifier:sorted_data; 101, identifier:sort; 102, keyword_argument; 103, call; 104, comparison_operator:len(key) == 2; 105, call; 106, tuple; 107, identifier:key_func; 108, lambda; 109, attribute; 110, argument_list; 111, identifier:DataTableException; 112, argument_list; 113, identifier:order_by; 114, identifier:order_by; 115, integer:1; 116, identifier:key; 117, lambda; 118, identifier:isinstance; 119, argument_list; 120, call; 121, integer:2; 122, attribute; 123, argument_list; 124, string:"asc"; 125, string:"desc"; 126, lambda_parameters; 127, call; 128, identifier:sorted_data; 129, identifier:sort; 130, keyword_argument; 131, keyword_argument; 132, concatenated_string; 133, lambda_parameters; 134, call; 135, identifier:key; 136, tuple; 137, identifier:len; 138, argument_list; 139, subscript; 140, identifier:lower; 141, identifier:x; 142, attribute; 143, argument_list; 144, identifier:key; 145, identifier:key_func; 146, identifier:reverse; 147, comparison_operator:key[1].lower() != "asc"; 148, string:"Expected tuple with second value: "; 149, string:"'asc' or 'desc'"; 150, identifier:x; 151, attribute; 152, argument_list; 153, identifier:list; 154, identifier:tuple; 155, identifier:key; 156, identifier:key; 157, integer:1; 158, subscript; 159, identifier:get; 160, subscript; 161, call; 162, string:"asc"; 163, subscript; 164, identifier:get; 165, identifier:key; 166, identifier:x; 167, integer:0; 168, identifier:key; 169, integer:0; 170, attribute; 171, argument_list; 172, identifier:x; 173, integer:0; 174, subscript; 175, identifier:lower; 176, identifier:key; 177, integer:1 | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 6, 13; 6, 14; 7, 15; 8, 16; 8, 17; 9, 18; 10, 19; 10, 20; 11, 21; 11, 22; 11, 23; 12, 24; 16, 25; 17, 26; 18, 27; 18, 28; 19, 29; 19, 30; 20, 31; 22, 32; 22, 33; 23, 34; 26, 35; 28, 36; 28, 37; 29, 38; 29, 39; 30, 40; 31, 41; 33, 42; 34, 43; 34, 44; 34, 45; 34, 46; 35, 47; 35, 48; 36, 49; 36, 50; 39, 51; 39, 52; 40, 53; 40, 54; 41, 55; 41, 56; 43, 57; 43, 58; 44, 59; 45, 60; 45, 61; 46, 62; 52, 63; 52, 64; 53, 65; 53, 66; 54, 67; 54, 68; 56, 69; 58, 70; 58, 71; 59, 72; 60, 73; 61, 74; 61, 75; 62, 76; 65, 77; 65, 78; 66, 79; 66, 80; 67, 81; 67, 82; 68, 83; 68, 84; 71, 85; 71, 86; 72, 87; 72, 88; 73, 89; 73, 90; 74, 91; 75, 92; 76, 93; 78, 94; 78, 95; 79, 96; 79, 97; 81, 98; 81, 99; 87, 100; 87, 101; 88, 102; 89, 103; 89, 104; 90, 105; 90, 106; 91, 107; 91, 108; 92, 109; 92, 110; 93, 111; 93, 112; 97, 113; 98, 114; 98, 115; 102, 116; 102, 117; 103, 118; 103, 119; 104, 120; 104, 121; 105, 122; 105, 123; 106, 124; 106, 125; 108, 126; 108, 127; 109, 128; 109, 129; 110, 130; 110, 131; 112, 132; 117, 133; 117, 134; 119, 135; 119, 136; 120, 137; 120, 138; 122, 139; 122, 140; 126, 141; 127, 142; 127, 143; 130, 144; 130, 145; 131, 146; 131, 147; 132, 148; 132, 149; 133, 150; 134, 151; 134, 152; 136, 153; 136, 154; 138, 155; 139, 156; 139, 157; 142, 158; 142, 159; 143, 160; 147, 161; 147, 162; 151, 163; 151, 164; 152, 165; 158, 166; 158, 167; 160, 168; 160, 169; 161, 170; 161, 171; 163, 172; 163, 173; 170, 174; 170, 175; 174, 176; 174, 177 | def _PreparedData(self, order_by=()):
"""Prepares the data for enumeration - sorting it by order_by.
Args:
order_by: Optional. Specifies the name of the column(s) to sort by, and
(optionally) which direction to sort in. Default sort direction
is asc. Following formats are accepted:
"string_col_name" -- For a single key in default (asc) order.
("string_col_name", "asc|desc") -- For a single key.
[("col_1","asc|desc"), ("col_2","asc|desc")] -- For more than
one column, an array of tuples of (col_name, "asc|desc").
Returns:
The data sorted by the keys given.
Raises:
DataTableException: Sort direction not in 'asc' or 'desc'
"""
if not order_by:
return self.__data
sorted_data = self.__data[:]
if isinstance(order_by, six.string_types) or (
isinstance(order_by, tuple) and len(order_by) == 2 and
order_by[1].lower() in ["asc", "desc"]):
order_by = (order_by,)
for key in reversed(order_by):
if isinstance(key, six.string_types):
sorted_data.sort(key=lambda x: x[0].get(key))
elif (isinstance(key, (list, tuple)) and len(key) == 2 and
key[1].lower() in ("asc", "desc")):
key_func = lambda x: x[0].get(key[0])
sorted_data.sort(key=key_func, reverse=key[1].lower() != "asc")
else:
raise DataTableException("Expected tuple with second value: "
"'asc' or 'desc'")
return sorted_data |
0, module; 1, function_definition; 2, function_name:ToJSCode; 3, parameters; 4, block; 5, identifier:self; 6, identifier:name; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, expression_statement; 11, if_statement; 12, expression_statement; 13, comment:# We first create the table with the given name; 14, expression_statement; 15, if_statement; 16, comment:# We add the columns to the table; 17, for_statement; 18, expression_statement; 19, comment:# We now go over the data and add each row; 20, for_statement; 21, return_statement; 22, identifier:columns_order; 23, None; 24, identifier:order_by; 25, tuple; 26, comment:"""Writes the data table as a JS code string.
This method writes a string of JS code that can be run to
generate a DataTable with the specified data. Typically used for debugging
only.
Args:
name: The name of the table. The name would be used as the DataTable's
variable name in the created JS code.
columns_order: Optional. Specifies the order of columns in the
output table. Specify a list of all column IDs in the order
in which you want the table created.
Note that you must list all column IDs in this parameter,
if you use it.
order_by: Optional. Specifies the name of the column(s) to sort by.
Passed as is to _PreparedData.
Returns:
A string of JS code that, when run, generates a DataTable with the given
name and the data stored in the DataTable object.
Example result:
"var tab1 = new google.visualization.DataTable();
tab1.addColumn("string", "a", "a");
tab1.addColumn("number", "b", "b");
tab1.addColumn("boolean", "c", "c");
tab1.addRows(10);
tab1.setCell(0, 0, "a");
tab1.setCell(0, 1, 1, null, {"foo": "bar"});
tab1.setCell(0, 2, true);
...
tab1.setCell(9, 0, "c");
tab1.setCell(9, 1, 3, "3$");
tab1.setCell(9, 2, false);"
Raises:
DataTableException: The data does not match the type.
"""; 27, assignment; 28, comparison_operator:columns_order is None; 29, block; 30, assignment; 31, assignment; 32, attribute; 33, block; 34, pattern_list; 35, call; 36, block; 37, augmented_assignment; 38, tuple_pattern; 39, call; 40, comment:# We add all the elements of this row by their order; 41, block; 42, identifier:jscode; 43, identifier:encoder; 44, call; 45, identifier:columns_order; 46, None; 47, expression_statement; 48, identifier:col_dict; 49, call; 50, identifier:jscode; 51, binary_operator:"var %s = new google.visualization.DataTable();\n" % name; 52, identifier:self; 53, identifier:custom_properties; 54, expression_statement; 55, identifier:i; 56, identifier:col; 57, identifier:enumerate; 58, argument_list; 59, expression_statement; 60, if_statement; 61, identifier:jscode; 62, binary_operator:"%s.addRows(%d);\n" % (name, len(self.__data)); 63, identifier:i; 64, tuple_pattern; 65, identifier:enumerate; 66, argument_list; 67, for_statement; 68, if_statement; 69, identifier:DataTableJSONEncoder; 70, argument_list; 71, assignment; 72, identifier:dict; 73, argument_list; 74, string:"var %s = new google.visualization.DataTable();\n"; 75, identifier:name; 76, augmented_assignment; 77, identifier:columns_order; 78, augmented_assignment; 79, subscript; 80, block; 81, string:"%s.addRows(%d);\n"; 82, tuple; 83, identifier:row; 84, identifier:cp; 85, call; 86, tuple_pattern; 87, call; 88, block; 89, identifier:cp; 90, block; 91, identifier:columns_order; 92, list_comprehension; 93, list_comprehension; 94, identifier:jscode; 95, binary_operator:"%s.setTableProperties(%s);\n" % (
name, encoder.encode(self.custom_properties)); 96, identifier:jscode; 97, binary_operator:"%s.addColumn(%s, %s, %s);\n" % (
name,
encoder.encode(col_dict[col]["type"]),
encoder.encode(col_dict[col]["label"]),
encoder.encode(col_dict[col]["id"])); 98, subscript; 99, string:"custom_properties"; 100, expression_statement; 101, identifier:name; 102, call; 103, attribute; 104, argument_list; 105, identifier:j; 106, identifier:col; 107, identifier:enumerate; 108, argument_list; 109, if_statement; 110, expression_statement; 111, if_statement; 112, expression_statement; 113, subscript; 114, for_in_clause; 115, tuple; 116, for_in_clause; 117, string:"%s.setTableProperties(%s);\n"; 118, tuple; 119, string:"%s.addColumn(%s, %s, %s);\n"; 120, tuple; 121, identifier:col_dict; 122, identifier:col; 123, augmented_assignment; 124, identifier:len; 125, argument_list; 126, identifier:self; 127, identifier:_PreparedData; 128, identifier:order_by; 129, identifier:columns_order; 130, boolean_operator; 131, block; 132, assignment; 133, call; 134, block; 135, else_clause; 136, augmented_assignment; 137, identifier:col; 138, string:"id"; 139, identifier:col; 140, attribute; 141, subscript; 142, identifier:col; 143, identifier:col; 144, attribute; 145, identifier:name; 146, call; 147, identifier:name; 148, call; 149, call; 150, call; 151, identifier:jscode; 152, binary_operator:"%s.setColumnProperties(%d, %s);\n" % (
name, i, encoder.encode(col_dict[col]["custom_properties"])); 153, attribute; 154, comparison_operator:col not in row; 155, comparison_operator:row[col] is None; 156, continue_statement; 157, identifier:value; 158, call; 159, identifier:isinstance; 160, argument_list; 161, expression_statement; 162, if_statement; 163, comment:# We have a formatted value or custom property as well; 164, expression_statement; 165, block; 166, identifier:jscode; 167, binary_operator:"%s.setRowProperties(%d, %s);\n" % (
name, i, encoder.encode(cp)); 168, identifier:self; 169, identifier:__columns; 170, identifier:col; 171, string:"id"; 172, identifier:self; 173, identifier:__columns; 174, attribute; 175, argument_list; 176, attribute; 177, argument_list; 178, attribute; 179, argument_list; 180, attribute; 181, argument_list; 182, string:"%s.setColumnProperties(%d, %s);\n"; 183, tuple; 184, identifier:self; 185, identifier:__data; 186, identifier:col; 187, identifier:row; 188, subscript; 189, None; 190, attribute; 191, argument_list; 192, identifier:value; 193, identifier:tuple; 194, assignment; 195, comparison_operator:len(value) == 3; 196, block; 197, augmented_assignment; 198, expression_statement; 199, string:"%s.setRowProperties(%d, %s);\n"; 200, tuple; 201, identifier:encoder; 202, identifier:encode; 203, attribute; 204, identifier:encoder; 205, identifier:encode; 206, subscript; 207, identifier:encoder; 208, identifier:encode; 209, subscript; 210, identifier:encoder; 211, identifier:encode; 212, subscript; 213, identifier:name; 214, identifier:i; 215, call; 216, identifier:row; 217, identifier:col; 218, identifier:self; 219, identifier:CoerceValue; 220, subscript; 221, subscript; 222, identifier:cell_cp; 223, string:""; 224, call; 225, integer:3; 226, expression_statement; 227, identifier:jscode; 228, parenthesized_expression; 229, augmented_assignment; 230, identifier:name; 231, identifier:i; 232, call; 233, identifier:self; 234, identifier:custom_properties; 235, subscript; 236, string:"type"; 237, subscript; 238, string:"label"; 239, subscript; 240, string:"id"; 241, attribute; 242, argument_list; 243, identifier:row; 244, identifier:col; 245, subscript; 246, string:"type"; 247, identifier:len; 248, argument_list; 249, assignment; 250, binary_operator:"%s.setCell(%d, %d, %s, %s%s);\n" %
(name, i, j,
self.EscapeForJSCode(encoder, value[0]),
self.EscapeForJSCode(encoder, value[1]), cell_cp); 251, identifier:jscode; 252, binary_operator:"%s.setCell(%d, %d, %s);\n" % (
name, i, j, self.EscapeForJSCode(encoder, value)); 253, attribute; 254, argument_list; 255, identifier:col_dict; 256, identifier:col; 257, identifier:col_dict; 258, identifier:col; 259, identifier:col_dict; 260, identifier:col; 261, identifier:encoder; 262, identifier:encode; 263, subscript; 264, identifier:col_dict; 265, identifier:col; 266, identifier:value; 267, identifier:cell_cp; 268, binary_operator:", %s" % encoder.encode(row[col][2]); 269, string:"%s.setCell(%d, %d, %s, %s%s);\n"; 270, tuple; 271, string:"%s.setCell(%d, %d, %s);\n"; 272, tuple; 273, identifier:encoder; 274, identifier:encode; 275, identifier:cp; 276, subscript; 277, string:"custom_properties"; 278, string:", %s"; 279, call; 280, identifier:name; 281, identifier:i; 282, identifier:j; 283, call; 284, call; 285, identifier:cell_cp; 286, identifier:name; 287, identifier:i; 288, identifier:j; 289, call; 290, identifier:col_dict; 291, identifier:col; 292, attribute; 293, argument_list; 294, attribute; 295, argument_list; 296, attribute; 297, argument_list; 298, attribute; 299, argument_list; 300, identifier:encoder; 301, identifier:encode; 302, subscript; 303, identifier:self; 304, identifier:EscapeForJSCode; 305, identifier:encoder; 306, subscript; 307, identifier:self; 308, identifier:EscapeForJSCode; 309, identifier:encoder; 310, subscript; 311, identifier:self; 312, identifier:EscapeForJSCode; 313, identifier:encoder; 314, identifier:value; 315, subscript; 316, integer:2; 317, identifier:value; 318, integer:0; 319, identifier:value; 320, integer:1; 321, identifier:row; 322, identifier:col | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 7, 22; 7, 23; 8, 24; 8, 25; 9, 26; 10, 27; 11, 28; 11, 29; 12, 30; 14, 31; 15, 32; 15, 33; 17, 34; 17, 35; 17, 36; 18, 37; 20, 38; 20, 39; 20, 40; 20, 41; 21, 42; 27, 43; 27, 44; 28, 45; 28, 46; 29, 47; 30, 48; 30, 49; 31, 50; 31, 51; 32, 52; 32, 53; 33, 54; 34, 55; 34, 56; 35, 57; 35, 58; 36, 59; 36, 60; 37, 61; 37, 62; 38, 63; 38, 64; 39, 65; 39, 66; 41, 67; 41, 68; 44, 69; 44, 70; 47, 71; 49, 72; 49, 73; 51, 74; 51, 75; 54, 76; 58, 77; 59, 78; 60, 79; 60, 80; 62, 81; 62, 82; 64, 83; 64, 84; 66, 85; 67, 86; 67, 87; 67, 88; 68, 89; 68, 90; 71, 91; 71, 92; 73, 93; 76, 94; 76, 95; 78, 96; 78, 97; 79, 98; 79, 99; 80, 100; 82, 101; 82, 102; 85, 103; 85, 104; 86, 105; 86, 106; 87, 107; 87, 108; 88, 109; 88, 110; 88, 111; 90, 112; 92, 113; 92, 114; 93, 115; 93, 116; 95, 117; 95, 118; 97, 119; 97, 120; 98, 121; 98, 122; 100, 123; 102, 124; 102, 125; 103, 126; 103, 127; 104, 128; 108, 129; 109, 130; 109, 131; 110, 132; 111, 133; 111, 134; 111, 135; 112, 136; 113, 137; 113, 138; 114, 139; 114, 140; 115, 141; 115, 142; 116, 143; 116, 144; 118, 145; 118, 146; 120, 147; 120, 148; 120, 149; 120, 150; 123, 151; 123, 152; 125, 153; 130, 154; 130, 155; 131, 156; 132, 157; 132, 158; 133, 159; 133, 160; 134, 161; 134, 162; 134, 163; 134, 164; 135, 165; 136, 166; 136, 167; 140, 168; 140, 169; 141, 170; 141, 171; 144, 172; 144, 173; 146, 174; 146, 175; 148, 176; 148, 177; 149, 178; 149, 179; 150, 180; 150, 181; 152, 182; 152, 183; 153, 184; 153, 185; 154, 186; 154, 187; 155, 188; 155, 189; 158, 190; 158, 191; 160, 192; 160, 193; 161, 194; 162, 195; 162, 196; 164, 197; 165, 198; 167, 199; 167, 200; 174, 201; 174, 202; 175, 203; 176, 204; 176, 205; 177, 206; 178, 207; 178, 208; 179, 209; 180, 210; 180, 211; 181, 212; 183, 213; 183, 214; 183, 215; 188, 216; 188, 217; 190, 218; 190, 219; 191, 220; 191, 221; 194, 222; 194, 223; 195, 224; 195, 225; 196, 226; 197, 227; 197, 228; 198, 229; 200, 230; 200, 231; 200, 232; 203, 233; 203, 234; 206, 235; 206, 236; 209, 237; 209, 238; 212, 239; 212, 240; 215, 241; 215, 242; 220, 243; 220, 244; 221, 245; 221, 246; 224, 247; 224, 248; 226, 249; 228, 250; 229, 251; 229, 252; 232, 253; 232, 254; 235, 255; 235, 256; 237, 257; 237, 258; 239, 259; 239, 260; 241, 261; 241, 262; 242, 263; 245, 264; 245, 265; 248, 266; 249, 267; 249, 268; 250, 269; 250, 270; 252, 271; 252, 272; 253, 273; 253, 274; 254, 275; 263, 276; 263, 277; 268, 278; 268, 279; 270, 280; 270, 281; 270, 282; 270, 283; 270, 284; 270, 285; 272, 286; 272, 287; 272, 288; 272, 289; 276, 290; 276, 291; 279, 292; 279, 293; 283, 294; 283, 295; 284, 296; 284, 297; 289, 298; 289, 299; 292, 300; 292, 301; 293, 302; 294, 303; 294, 304; 295, 305; 295, 306; 296, 307; 296, 308; 297, 309; 297, 310; 298, 311; 298, 312; 299, 313; 299, 314; 302, 315; 302, 316; 306, 317; 306, 318; 310, 319; 310, 320; 315, 321; 315, 322 | def ToJSCode(self, name, columns_order=None, order_by=()):
"""Writes the data table as a JS code string.
This method writes a string of JS code that can be run to
generate a DataTable with the specified data. Typically used for debugging
only.
Args:
name: The name of the table. The name would be used as the DataTable's
variable name in the created JS code.
columns_order: Optional. Specifies the order of columns in the
output table. Specify a list of all column IDs in the order
in which you want the table created.
Note that you must list all column IDs in this parameter,
if you use it.
order_by: Optional. Specifies the name of the column(s) to sort by.
Passed as is to _PreparedData.
Returns:
A string of JS code that, when run, generates a DataTable with the given
name and the data stored in the DataTable object.
Example result:
"var tab1 = new google.visualization.DataTable();
tab1.addColumn("string", "a", "a");
tab1.addColumn("number", "b", "b");
tab1.addColumn("boolean", "c", "c");
tab1.addRows(10);
tab1.setCell(0, 0, "a");
tab1.setCell(0, 1, 1, null, {"foo": "bar"});
tab1.setCell(0, 2, true);
...
tab1.setCell(9, 0, "c");
tab1.setCell(9, 1, 3, "3$");
tab1.setCell(9, 2, false);"
Raises:
DataTableException: The data does not match the type.
"""
encoder = DataTableJSONEncoder()
if columns_order is None:
columns_order = [col["id"] for col in self.__columns]
col_dict = dict([(col["id"], col) for col in self.__columns])
# We first create the table with the given name
jscode = "var %s = new google.visualization.DataTable();\n" % name
if self.custom_properties:
jscode += "%s.setTableProperties(%s);\n" % (
name, encoder.encode(self.custom_properties))
# We add the columns to the table
for i, col in enumerate(columns_order):
jscode += "%s.addColumn(%s, %s, %s);\n" % (
name,
encoder.encode(col_dict[col]["type"]),
encoder.encode(col_dict[col]["label"]),
encoder.encode(col_dict[col]["id"]))
if col_dict[col]["custom_properties"]:
jscode += "%s.setColumnProperties(%d, %s);\n" % (
name, i, encoder.encode(col_dict[col]["custom_properties"]))
jscode += "%s.addRows(%d);\n" % (name, len(self.__data))
# We now go over the data and add each row
for (i, (row, cp)) in enumerate(self._PreparedData(order_by)):
# We add all the elements of this row by their order
for (j, col) in enumerate(columns_order):
if col not in row or row[col] is None:
continue
value = self.CoerceValue(row[col], col_dict[col]["type"])
if isinstance(value, tuple):
cell_cp = ""
if len(value) == 3:
cell_cp = ", %s" % encoder.encode(row[col][2])
# We have a formatted value or custom property as well
jscode += ("%s.setCell(%d, %d, %s, %s%s);\n" %
(name, i, j,
self.EscapeForJSCode(encoder, value[0]),
self.EscapeForJSCode(encoder, value[1]), cell_cp))
else:
jscode += "%s.setCell(%d, %d, %s);\n" % (
name, i, j, self.EscapeForJSCode(encoder, value))
if cp:
jscode += "%s.setRowProperties(%d, %s);\n" % (
name, i, encoder.encode(cp))
return jscode |
0, module; 1, function_definition; 2, function_name:ToHtml; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, if_statement; 16, expression_statement; 17, expression_statement; 18, for_statement; 19, expression_statement; 20, expression_statement; 21, comment:# We now go over the data and add each row; 22, for_statement; 23, expression_statement; 24, return_statement; 25, identifier:columns_order; 26, None; 27, identifier:order_by; 28, tuple; 29, comment:"""Writes the data table as an HTML table code string.
Args:
columns_order: Optional. Specifies the order of columns in the
output table. Specify a list of all column IDs in the order
in which you want the table created.
Note that you must list all column IDs in this parameter,
if you use it.
order_by: Optional. Specifies the name of the column(s) to sort by.
Passed as is to _PreparedData.
Returns:
An HTML table code string.
Example result (the result is without the newlines):
<html><body><table border="1">
<thead><tr><th>a</th><th>b</th><th>c</th></tr></thead>
<tbody>
<tr><td>1</td><td>"z"</td><td>2</td></tr>
<tr><td>"3$"</td><td>"w"</td><td></td></tr>
</tbody>
</table></body></html>
Raises:
DataTableException: The data does not match the type.
"""; 30, assignment; 31, assignment; 32, assignment; 33, assignment; 34, assignment; 35, assignment; 36, comparison_operator:columns_order is None; 37, block; 38, assignment; 39, assignment; 40, identifier:col; 41, identifier:columns_order; 42, block; 43, assignment; 44, assignment; 45, pattern_list; 46, call; 47, block; 48, assignment; 49, binary_operator:table_template % (columns_html + rows_html); 50, identifier:table_template; 51, string:"<html><body><table border=\"1\">%s</table></body></html>"; 52, identifier:columns_template; 53, string:"<thead><tr>%s</tr></thead>"; 54, identifier:rows_template; 55, string:"<tbody>%s</tbody>"; 56, identifier:row_template; 57, string:"<tr>%s</tr>"; 58, identifier:header_cell_template; 59, string:"<th>%s</th>"; 60, identifier:cell_template; 61, string:"<td>%s</td>"; 62, identifier:columns_order; 63, None; 64, expression_statement; 65, identifier:col_dict; 66, call; 67, identifier:columns_list; 68, list; 69, expression_statement; 70, identifier:columns_html; 71, binary_operator:columns_template % "".join(columns_list); 72, identifier:rows_list; 73, list; 74, identifier:row; 75, identifier:unused_cp; 76, attribute; 77, argument_list; 78, expression_statement; 79, comment:# We add all the elements of this row by their order; 80, for_statement; 81, expression_statement; 82, identifier:rows_html; 83, binary_operator:rows_template % "".join(rows_list); 84, identifier:table_template; 85, parenthesized_expression; 86, assignment; 87, identifier:dict; 88, argument_list; 89, call; 90, identifier:columns_template; 91, call; 92, identifier:self; 93, identifier:_PreparedData; 94, identifier:order_by; 95, assignment; 96, identifier:col; 97, identifier:columns_order; 98, comment:# For empty string we want empty quotes ("").; 99, block; 100, call; 101, identifier:rows_template; 102, call; 103, binary_operator:columns_html + rows_html; 104, identifier:columns_order; 105, list_comprehension; 106, list_comprehension; 107, attribute; 108, argument_list; 109, attribute; 110, argument_list; 111, identifier:cells_list; 112, list; 113, expression_statement; 114, if_statement; 115, if_statement; 116, attribute; 117, argument_list; 118, attribute; 119, argument_list; 120, identifier:columns_html; 121, identifier:rows_html; 122, subscript; 123, for_in_clause; 124, tuple; 125, for_in_clause; 126, identifier:columns_list; 127, identifier:append; 128, binary_operator:header_cell_template %
html.escape(col_dict[col]["label"]); 129, string:""; 130, identifier:join; 131, identifier:columns_list; 132, assignment; 133, boolean_operator; 134, block; 135, call; 136, comment:# We have a formatted value and we're going to use it; 137, block; 138, else_clause; 139, identifier:rows_list; 140, identifier:append; 141, binary_operator:row_template % "".join(cells_list); 142, string:""; 143, identifier:join; 144, identifier:rows_list; 145, identifier:col; 146, string:"id"; 147, identifier:col; 148, attribute; 149, subscript; 150, identifier:col; 151, identifier:col; 152, attribute; 153, identifier:header_cell_template; 154, call; 155, identifier:value; 156, string:""; 157, comparison_operator:col in row; 158, comparison_operator:row[col] is not None; 159, expression_statement; 160, identifier:isinstance; 161, argument_list; 162, expression_statement; 163, block; 164, identifier:row_template; 165, call; 166, identifier:self; 167, identifier:__columns; 168, identifier:col; 169, string:"id"; 170, identifier:self; 171, identifier:__columns; 172, attribute; 173, argument_list; 174, identifier:col; 175, identifier:row; 176, subscript; 177, None; 178, assignment; 179, identifier:value; 180, identifier:tuple; 181, call; 182, expression_statement; 183, attribute; 184, argument_list; 185, identifier:html; 186, identifier:escape; 187, subscript; 188, identifier:row; 189, identifier:col; 190, identifier:value; 191, call; 192, attribute; 193, argument_list; 194, call; 195, string:""; 196, identifier:join; 197, identifier:cells_list; 198, subscript; 199, string:"label"; 200, attribute; 201, argument_list; 202, identifier:cells_list; 203, identifier:append; 204, binary_operator:cell_template % html.escape(self.ToString(value[1])); 205, attribute; 206, argument_list; 207, identifier:col_dict; 208, identifier:col; 209, identifier:self; 210, identifier:CoerceValue; 211, subscript; 212, subscript; 213, identifier:cell_template; 214, call; 215, identifier:cells_list; 216, identifier:append; 217, binary_operator:cell_template % html.escape(self.ToString(value)); 218, identifier:row; 219, identifier:col; 220, subscript; 221, string:"type"; 222, attribute; 223, argument_list; 224, identifier:cell_template; 225, call; 226, identifier:col_dict; 227, identifier:col; 228, identifier:html; 229, identifier:escape; 230, call; 231, attribute; 232, argument_list; 233, attribute; 234, argument_list; 235, identifier:html; 236, identifier:escape; 237, call; 238, identifier:self; 239, identifier:ToString; 240, subscript; 241, attribute; 242, argument_list; 243, identifier:value; 244, integer:1; 245, identifier:self; 246, identifier:ToString; 247, identifier:value | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 4, 23; 4, 24; 6, 25; 6, 26; 7, 27; 7, 28; 8, 29; 9, 30; 10, 31; 11, 32; 12, 33; 13, 34; 14, 35; 15, 36; 15, 37; 16, 38; 17, 39; 18, 40; 18, 41; 18, 42; 19, 43; 20, 44; 22, 45; 22, 46; 22, 47; 23, 48; 24, 49; 30, 50; 30, 51; 31, 52; 31, 53; 32, 54; 32, 55; 33, 56; 33, 57; 34, 58; 34, 59; 35, 60; 35, 61; 36, 62; 36, 63; 37, 64; 38, 65; 38, 66; 39, 67; 39, 68; 42, 69; 43, 70; 43, 71; 44, 72; 44, 73; 45, 74; 45, 75; 46, 76; 46, 77; 47, 78; 47, 79; 47, 80; 47, 81; 48, 82; 48, 83; 49, 84; 49, 85; 64, 86; 66, 87; 66, 88; 69, 89; 71, 90; 71, 91; 76, 92; 76, 93; 77, 94; 78, 95; 80, 96; 80, 97; 80, 98; 80, 99; 81, 100; 83, 101; 83, 102; 85, 103; 86, 104; 86, 105; 88, 106; 89, 107; 89, 108; 91, 109; 91, 110; 95, 111; 95, 112; 99, 113; 99, 114; 99, 115; 100, 116; 100, 117; 102, 118; 102, 119; 103, 120; 103, 121; 105, 122; 105, 123; 106, 124; 106, 125; 107, 126; 107, 127; 108, 128; 109, 129; 109, 130; 110, 131; 113, 132; 114, 133; 114, 134; 115, 135; 115, 136; 115, 137; 115, 138; 116, 139; 116, 140; 117, 141; 118, 142; 118, 143; 119, 144; 122, 145; 122, 146; 123, 147; 123, 148; 124, 149; 124, 150; 125, 151; 125, 152; 128, 153; 128, 154; 132, 155; 132, 156; 133, 157; 133, 158; 134, 159; 135, 160; 135, 161; 137, 162; 138, 163; 141, 164; 141, 165; 148, 166; 148, 167; 149, 168; 149, 169; 152, 170; 152, 171; 154, 172; 154, 173; 157, 174; 157, 175; 158, 176; 158, 177; 159, 178; 161, 179; 161, 180; 162, 181; 163, 182; 165, 183; 165, 184; 172, 185; 172, 186; 173, 187; 176, 188; 176, 189; 178, 190; 178, 191; 181, 192; 181, 193; 182, 194; 183, 195; 183, 196; 184, 197; 187, 198; 187, 199; 191, 200; 191, 201; 192, 202; 192, 203; 193, 204; 194, 205; 194, 206; 198, 207; 198, 208; 200, 209; 200, 210; 201, 211; 201, 212; 204, 213; 204, 214; 205, 215; 205, 216; 206, 217; 211, 218; 211, 219; 212, 220; 212, 221; 214, 222; 214, 223; 217, 224; 217, 225; 220, 226; 220, 227; 222, 228; 222, 229; 223, 230; 225, 231; 225, 232; 230, 233; 230, 234; 231, 235; 231, 236; 232, 237; 233, 238; 233, 239; 234, 240; 237, 241; 237, 242; 240, 243; 240, 244; 241, 245; 241, 246; 242, 247 | def ToHtml(self, columns_order=None, order_by=()):
"""Writes the data table as an HTML table code string.
Args:
columns_order: Optional. Specifies the order of columns in the
output table. Specify a list of all column IDs in the order
in which you want the table created.
Note that you must list all column IDs in this parameter,
if you use it.
order_by: Optional. Specifies the name of the column(s) to sort by.
Passed as is to _PreparedData.
Returns:
An HTML table code string.
Example result (the result is without the newlines):
<html><body><table border="1">
<thead><tr><th>a</th><th>b</th><th>c</th></tr></thead>
<tbody>
<tr><td>1</td><td>"z"</td><td>2</td></tr>
<tr><td>"3$"</td><td>"w"</td><td></td></tr>
</tbody>
</table></body></html>
Raises:
DataTableException: The data does not match the type.
"""
table_template = "<html><body><table border=\"1\">%s</table></body></html>"
columns_template = "<thead><tr>%s</tr></thead>"
rows_template = "<tbody>%s</tbody>"
row_template = "<tr>%s</tr>"
header_cell_template = "<th>%s</th>"
cell_template = "<td>%s</td>"
if columns_order is None:
columns_order = [col["id"] for col in self.__columns]
col_dict = dict([(col["id"], col) for col in self.__columns])
columns_list = []
for col in columns_order:
columns_list.append(header_cell_template %
html.escape(col_dict[col]["label"]))
columns_html = columns_template % "".join(columns_list)
rows_list = []
# We now go over the data and add each row
for row, unused_cp in self._PreparedData(order_by):
cells_list = []
# We add all the elements of this row by their order
for col in columns_order:
# For empty string we want empty quotes ("").
value = ""
if col in row and row[col] is not None:
value = self.CoerceValue(row[col], col_dict[col]["type"])
if isinstance(value, tuple):
# We have a formatted value and we're going to use it
cells_list.append(cell_template % html.escape(self.ToString(value[1])))
else:
cells_list.append(cell_template % html.escape(self.ToString(value)))
rows_list.append(row_template % "".join(cells_list))
rows_html = rows_template % "".join(rows_list)
return table_template % (columns_html + rows_html) |
0, module; 1, function_definition; 2, function_name:ToCsv; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, if_statement; 13, expression_statement; 14, function_definition; 15, expression_statement; 16, comment:# We now go over the data and add each row; 17, for_statement; 18, return_statement; 19, identifier:columns_order; 20, None; 21, identifier:order_by; 22, tuple; 23, identifier:separator; 24, string:","; 25, comment:"""Writes the data table as a CSV string.
Output is encoded in UTF-8 because the Python "csv" module can't handle
Unicode properly according to its documentation.
Args:
columns_order: Optional. Specifies the order of columns in the
output table. Specify a list of all column IDs in the order
in which you want the table created.
Note that you must list all column IDs in this parameter,
if you use it.
order_by: Optional. Specifies the name of the column(s) to sort by.
Passed as is to _PreparedData.
separator: Optional. The separator to use between the values.
Returns:
A CSV string representing the table.
Example result:
'a','b','c'
1,'z',2
3,'w',''
Raises:
DataTableException: The data does not match the type.
"""; 26, assignment; 27, assignment; 28, comparison_operator:columns_order is None; 29, block; 30, assignment; 31, function_name:ensure_str; 32, parameters; 33, block; 34, call; 35, pattern_list; 36, call; 37, block; 38, call; 39, identifier:csv_buffer; 40, call; 41, identifier:writer; 42, call; 43, identifier:columns_order; 44, None; 45, expression_statement; 46, identifier:col_dict; 47, call; 48, identifier:s; 49, expression_statement; 50, if_statement; 51, return_statement; 52, attribute; 53, argument_list; 54, identifier:row; 55, identifier:unused_cp; 56, attribute; 57, argument_list; 58, expression_statement; 59, comment:# We add all the elements of this row by their order; 60, for_statement; 61, expression_statement; 62, attribute; 63, argument_list; 64, attribute; 65, argument_list; 66, attribute; 67, argument_list; 68, assignment; 69, identifier:dict; 70, argument_list; 71, string:"Compatibility function. Ensures using of str rather than unicode."; 72, call; 73, block; 74, call; 75, identifier:writer; 76, identifier:writerow; 77, list_comprehension; 78, identifier:self; 79, identifier:_PreparedData; 80, identifier:order_by; 81, assignment; 82, identifier:col; 83, identifier:columns_order; 84, block; 85, call; 86, identifier:csv_buffer; 87, identifier:getvalue; 88, identifier:six; 89, identifier:StringIO; 90, identifier:csv; 91, identifier:writer; 92, identifier:csv_buffer; 93, keyword_argument; 94, identifier:columns_order; 95, list_comprehension; 96, list_comprehension; 97, identifier:isinstance; 98, argument_list; 99, return_statement; 100, attribute; 101, argument_list; 102, call; 103, for_in_clause; 104, identifier:cells_list; 105, list; 106, expression_statement; 107, if_statement; 108, if_statement; 109, attribute; 110, argument_list; 111, identifier:delimiter; 112, identifier:separator; 113, subscript; 114, for_in_clause; 115, tuple; 116, for_in_clause; 117, identifier:s; 118, identifier:str; 119, identifier:s; 120, identifier:s; 121, identifier:encode; 122, string:"utf-8"; 123, identifier:ensure_str; 124, argument_list; 125, identifier:col; 126, identifier:columns_order; 127, assignment; 128, boolean_operator; 129, block; 130, call; 131, comment:# We have a formatted value. Using it only for date/time types.; 132, block; 133, else_clause; 134, identifier:writer; 135, identifier:writerow; 136, identifier:cells_list; 137, identifier:col; 138, string:"id"; 139, identifier:col; 140, attribute; 141, subscript; 142, identifier:col; 143, identifier:col; 144, attribute; 145, subscript; 146, identifier:value; 147, string:""; 148, comparison_operator:col in row; 149, comparison_operator:row[col] is not None; 150, expression_statement; 151, identifier:isinstance; 152, argument_list; 153, if_statement; 154, block; 155, identifier:self; 156, identifier:__columns; 157, identifier:col; 158, string:"id"; 159, identifier:self; 160, identifier:__columns; 161, subscript; 162, string:"label"; 163, identifier:col; 164, identifier:row; 165, subscript; 166, None; 167, assignment; 168, identifier:value; 169, identifier:tuple; 170, comparison_operator:col_dict[col]["type"] in ["date", "datetime", "timeofday"]; 171, block; 172, else_clause; 173, expression_statement; 174, identifier:col_dict; 175, identifier:col; 176, identifier:row; 177, identifier:col; 178, identifier:value; 179, call; 180, subscript; 181, list; 182, expression_statement; 183, block; 184, call; 185, attribute; 186, argument_list; 187, subscript; 188, string:"type"; 189, string:"date"; 190, string:"datetime"; 191, string:"timeofday"; 192, call; 193, expression_statement; 194, attribute; 195, argument_list; 196, identifier:self; 197, identifier:CoerceValue; 198, subscript; 199, subscript; 200, identifier:col_dict; 201, identifier:col; 202, attribute; 203, argument_list; 204, call; 205, identifier:cells_list; 206, identifier:append; 207, call; 208, identifier:row; 209, identifier:col; 210, subscript; 211, string:"type"; 212, identifier:cells_list; 213, identifier:append; 214, call; 215, attribute; 216, argument_list; 217, identifier:ensure_str; 218, argument_list; 219, identifier:col_dict; 220, identifier:col; 221, identifier:ensure_str; 222, argument_list; 223, identifier:cells_list; 224, identifier:append; 225, call; 226, call; 227, call; 228, identifier:ensure_str; 229, argument_list; 230, attribute; 231, argument_list; 232, attribute; 233, argument_list; 234, call; 235, identifier:self; 236, identifier:ToString; 237, identifier:value; 238, identifier:self; 239, identifier:ToString; 240, subscript; 241, attribute; 242, argument_list; 243, identifier:value; 244, integer:1; 245, identifier:self; 246, identifier:ToString; 247, subscript; 248, identifier:value; 249, integer:0 | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 6, 19; 6, 20; 7, 21; 7, 22; 8, 23; 8, 24; 9, 25; 10, 26; 11, 27; 12, 28; 12, 29; 13, 30; 14, 31; 14, 32; 14, 33; 15, 34; 17, 35; 17, 36; 17, 37; 18, 38; 26, 39; 26, 40; 27, 41; 27, 42; 28, 43; 28, 44; 29, 45; 30, 46; 30, 47; 32, 48; 33, 49; 33, 50; 33, 51; 34, 52; 34, 53; 35, 54; 35, 55; 36, 56; 36, 57; 37, 58; 37, 59; 37, 60; 37, 61; 38, 62; 38, 63; 40, 64; 40, 65; 42, 66; 42, 67; 45, 68; 47, 69; 47, 70; 49, 71; 50, 72; 50, 73; 51, 74; 52, 75; 52, 76; 53, 77; 56, 78; 56, 79; 57, 80; 58, 81; 60, 82; 60, 83; 60, 84; 61, 85; 62, 86; 62, 87; 64, 88; 64, 89; 66, 90; 66, 91; 67, 92; 67, 93; 68, 94; 68, 95; 70, 96; 72, 97; 72, 98; 73, 99; 74, 100; 74, 101; 77, 102; 77, 103; 81, 104; 81, 105; 84, 106; 84, 107; 84, 108; 85, 109; 85, 110; 93, 111; 93, 112; 95, 113; 95, 114; 96, 115; 96, 116; 98, 117; 98, 118; 99, 119; 100, 120; 100, 121; 101, 122; 102, 123; 102, 124; 103, 125; 103, 126; 106, 127; 107, 128; 107, 129; 108, 130; 108, 131; 108, 132; 108, 133; 109, 134; 109, 135; 110, 136; 113, 137; 113, 138; 114, 139; 114, 140; 115, 141; 115, 142; 116, 143; 116, 144; 124, 145; 127, 146; 127, 147; 128, 148; 128, 149; 129, 150; 130, 151; 130, 152; 132, 153; 133, 154; 140, 155; 140, 156; 141, 157; 141, 158; 144, 159; 144, 160; 145, 161; 145, 162; 148, 163; 148, 164; 149, 165; 149, 166; 150, 167; 152, 168; 152, 169; 153, 170; 153, 171; 153, 172; 154, 173; 161, 174; 161, 175; 165, 176; 165, 177; 167, 178; 167, 179; 170, 180; 170, 181; 171, 182; 172, 183; 173, 184; 179, 185; 179, 186; 180, 187; 180, 188; 181, 189; 181, 190; 181, 191; 182, 192; 183, 193; 184, 194; 184, 195; 185, 196; 185, 197; 186, 198; 186, 199; 187, 200; 187, 201; 192, 202; 192, 203; 193, 204; 194, 205; 194, 206; 195, 207; 198, 208; 198, 209; 199, 210; 199, 211; 202, 212; 202, 213; 203, 214; 204, 215; 204, 216; 207, 217; 207, 218; 210, 219; 210, 220; 214, 221; 214, 222; 215, 223; 215, 224; 216, 225; 218, 226; 222, 227; 225, 228; 225, 229; 226, 230; 226, 231; 227, 232; 227, 233; 229, 234; 230, 235; 230, 236; 231, 237; 232, 238; 232, 239; 233, 240; 234, 241; 234, 242; 240, 243; 240, 244; 241, 245; 241, 246; 242, 247; 247, 248; 247, 249 | def ToCsv(self, columns_order=None, order_by=(), separator=","):
"""Writes the data table as a CSV string.
Output is encoded in UTF-8 because the Python "csv" module can't handle
Unicode properly according to its documentation.
Args:
columns_order: Optional. Specifies the order of columns in the
output table. Specify a list of all column IDs in the order
in which you want the table created.
Note that you must list all column IDs in this parameter,
if you use it.
order_by: Optional. Specifies the name of the column(s) to sort by.
Passed as is to _PreparedData.
separator: Optional. The separator to use between the values.
Returns:
A CSV string representing the table.
Example result:
'a','b','c'
1,'z',2
3,'w',''
Raises:
DataTableException: The data does not match the type.
"""
csv_buffer = six.StringIO()
writer = csv.writer(csv_buffer, delimiter=separator)
if columns_order is None:
columns_order = [col["id"] for col in self.__columns]
col_dict = dict([(col["id"], col) for col in self.__columns])
def ensure_str(s):
"Compatibility function. Ensures using of str rather than unicode."
if isinstance(s, str):
return s
return s.encode("utf-8")
writer.writerow([ensure_str(col_dict[col]["label"])
for col in columns_order])
# We now go over the data and add each row
for row, unused_cp in self._PreparedData(order_by):
cells_list = []
# We add all the elements of this row by their order
for col in columns_order:
value = ""
if col in row and row[col] is not None:
value = self.CoerceValue(row[col], col_dict[col]["type"])
if isinstance(value, tuple):
# We have a formatted value. Using it only for date/time types.
if col_dict[col]["type"] in ["date", "datetime", "timeofday"]:
cells_list.append(ensure_str(self.ToString(value[1])))
else:
cells_list.append(ensure_str(self.ToString(value[0])))
else:
cells_list.append(ensure_str(self.ToString(value)))
writer.writerow(cells_list)
return csv_buffer.getvalue() |
0, module; 1, function_definition; 2, function_name:_ToJSonObj; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, expression_statement; 9, if_statement; 10, expression_statement; 11, comment:# Creating the column JSON objects; 12, expression_statement; 13, for_statement; 14, comment:# Creating the rows jsons; 15, expression_statement; 16, for_statement; 17, expression_statement; 18, if_statement; 19, return_statement; 20, identifier:columns_order; 21, None; 22, identifier:order_by; 23, tuple; 24, comment:"""Returns an object suitable to be converted to JSON.
Args:
columns_order: Optional. A list of all column IDs in the order in which
you want them created in the output table. If specified,
all column IDs must be present.
order_by: Optional. Specifies the name of the column(s) to sort by.
Passed as is to _PreparedData().
Returns:
A dictionary object for use by ToJSon or ToJSonResponse.
"""; 25, comparison_operator:columns_order is None; 26, block; 27, assignment; 28, assignment; 29, identifier:col_id; 30, identifier:columns_order; 31, block; 32, assignment; 33, pattern_list; 34, call; 35, block; 36, assignment; 37, attribute; 38, block; 39, identifier:json_obj; 40, identifier:columns_order; 41, None; 42, expression_statement; 43, identifier:col_dict; 44, call; 45, identifier:col_objs; 46, list; 47, expression_statement; 48, if_statement; 49, expression_statement; 50, identifier:row_objs; 51, list; 52, identifier:row; 53, identifier:cp; 54, attribute; 55, argument_list; 56, expression_statement; 57, for_statement; 58, expression_statement; 59, if_statement; 60, expression_statement; 61, identifier:json_obj; 62, dictionary; 63, identifier:self; 64, identifier:custom_properties; 65, expression_statement; 66, assignment; 67, identifier:dict; 68, argument_list; 69, assignment; 70, subscript; 71, block; 72, call; 73, identifier:self; 74, identifier:_PreparedData; 75, identifier:order_by; 76, assignment; 77, identifier:col; 78, identifier:columns_order; 79, block; 80, assignment; 81, identifier:cp; 82, block; 83, call; 84, pair; 85, pair; 86, assignment; 87, identifier:columns_order; 88, list_comprehension; 89, list_comprehension; 90, identifier:col_obj; 91, dictionary; 92, subscript; 93, string:"custom_properties"; 94, expression_statement; 95, attribute; 96, argument_list; 97, identifier:cell_objs; 98, list; 99, expression_statement; 100, if_statement; 101, expression_statement; 102, identifier:row_obj; 103, dictionary; 104, expression_statement; 105, attribute; 106, argument_list; 107, string:"cols"; 108, identifier:col_objs; 109, string:"rows"; 110, identifier:row_objs; 111, subscript; 112, attribute; 113, subscript; 114, for_in_clause; 115, tuple; 116, for_in_clause; 117, pair; 118, pair; 119, pair; 120, identifier:col_dict; 121, identifier:col_id; 122, assignment; 123, identifier:col_objs; 124, identifier:append; 125, identifier:col_obj; 126, assignment; 127, comparison_operator:value is None; 128, block; 129, elif_clause; 130, else_clause; 131, call; 132, pair; 133, assignment; 134, identifier:row_objs; 135, identifier:append; 136, identifier:row_obj; 137, identifier:json_obj; 138, string:"p"; 139, identifier:self; 140, identifier:custom_properties; 141, identifier:col; 142, string:"id"; 143, identifier:col; 144, attribute; 145, subscript; 146, identifier:col; 147, identifier:col; 148, attribute; 149, string:"id"; 150, subscript; 151, string:"label"; 152, subscript; 153, string:"type"; 154, subscript; 155, subscript; 156, subscript; 157, identifier:value; 158, call; 159, identifier:value; 160, None; 161, expression_statement; 162, call; 163, block; 164, block; 165, attribute; 166, argument_list; 167, string:"c"; 168, identifier:cell_objs; 169, subscript; 170, identifier:cp; 171, identifier:self; 172, identifier:__columns; 173, identifier:col; 174, string:"id"; 175, identifier:self; 176, identifier:__columns; 177, subscript; 178, string:"id"; 179, subscript; 180, string:"label"; 181, subscript; 182, string:"type"; 183, identifier:col_obj; 184, string:"p"; 185, subscript; 186, string:"custom_properties"; 187, attribute; 188, argument_list; 189, assignment; 190, identifier:isinstance; 191, argument_list; 192, expression_statement; 193, if_statement; 194, if_statement; 195, expression_statement; 196, identifier:cell_objs; 197, identifier:append; 198, identifier:cell_obj; 199, identifier:row_obj; 200, string:"p"; 201, identifier:col_dict; 202, identifier:col_id; 203, identifier:col_dict; 204, identifier:col_id; 205, identifier:col_dict; 206, identifier:col_id; 207, identifier:col_dict; 208, identifier:col_id; 209, identifier:self; 210, identifier:CoerceValue; 211, call; 212, subscript; 213, identifier:cell_obj; 214, None; 215, identifier:value; 216, identifier:tuple; 217, assignment; 218, boolean_operator; 219, block; 220, comparison_operator:len(value) == 3; 221, block; 222, assignment; 223, attribute; 224, argument_list; 225, subscript; 226, string:"type"; 227, identifier:cell_obj; 228, dictionary; 229, comparison_operator:len(value) > 1; 230, comparison_operator:value[1] is not None; 231, expression_statement; 232, call; 233, integer:3; 234, expression_statement; 235, identifier:cell_obj; 236, dictionary; 237, identifier:row; 238, identifier:get; 239, identifier:col; 240, None; 241, identifier:col_dict; 242, identifier:col; 243, pair; 244, call; 245, integer:1; 246, subscript; 247, None; 248, assignment; 249, identifier:len; 250, argument_list; 251, assignment; 252, pair; 253, string:"v"; 254, subscript; 255, identifier:len; 256, argument_list; 257, identifier:value; 258, integer:1; 259, subscript; 260, subscript; 261, identifier:value; 262, subscript; 263, subscript; 264, string:"v"; 265, identifier:value; 266, identifier:value; 267, integer:0; 268, identifier:value; 269, identifier:cell_obj; 270, string:"f"; 271, identifier:value; 272, integer:1; 273, identifier:cell_obj; 274, string:"p"; 275, identifier:value; 276, integer:2 | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 6, 20; 6, 21; 7, 22; 7, 23; 8, 24; 9, 25; 9, 26; 10, 27; 12, 28; 13, 29; 13, 30; 13, 31; 15, 32; 16, 33; 16, 34; 16, 35; 17, 36; 18, 37; 18, 38; 19, 39; 25, 40; 25, 41; 26, 42; 27, 43; 27, 44; 28, 45; 28, 46; 31, 47; 31, 48; 31, 49; 32, 50; 32, 51; 33, 52; 33, 53; 34, 54; 34, 55; 35, 56; 35, 57; 35, 58; 35, 59; 35, 60; 36, 61; 36, 62; 37, 63; 37, 64; 38, 65; 42, 66; 44, 67; 44, 68; 47, 69; 48, 70; 48, 71; 49, 72; 54, 73; 54, 74; 55, 75; 56, 76; 57, 77; 57, 78; 57, 79; 58, 80; 59, 81; 59, 82; 60, 83; 62, 84; 62, 85; 65, 86; 66, 87; 66, 88; 68, 89; 69, 90; 69, 91; 70, 92; 70, 93; 71, 94; 72, 95; 72, 96; 76, 97; 76, 98; 79, 99; 79, 100; 79, 101; 80, 102; 80, 103; 82, 104; 83, 105; 83, 106; 84, 107; 84, 108; 85, 109; 85, 110; 86, 111; 86, 112; 88, 113; 88, 114; 89, 115; 89, 116; 91, 117; 91, 118; 91, 119; 92, 120; 92, 121; 94, 122; 95, 123; 95, 124; 96, 125; 99, 126; 100, 127; 100, 128; 100, 129; 100, 130; 101, 131; 103, 132; 104, 133; 105, 134; 105, 135; 106, 136; 111, 137; 111, 138; 112, 139; 112, 140; 113, 141; 113, 142; 114, 143; 114, 144; 115, 145; 115, 146; 116, 147; 116, 148; 117, 149; 117, 150; 118, 151; 118, 152; 119, 153; 119, 154; 122, 155; 122, 156; 126, 157; 126, 158; 127, 159; 127, 160; 128, 161; 129, 162; 129, 163; 130, 164; 131, 165; 131, 166; 132, 167; 132, 168; 133, 169; 133, 170; 144, 171; 144, 172; 145, 173; 145, 174; 148, 175; 148, 176; 150, 177; 150, 178; 152, 179; 152, 180; 154, 181; 154, 182; 155, 183; 155, 184; 156, 185; 156, 186; 158, 187; 158, 188; 161, 189; 162, 190; 162, 191; 163, 192; 163, 193; 163, 194; 164, 195; 165, 196; 165, 197; 166, 198; 169, 199; 169, 200; 177, 201; 177, 202; 179, 203; 179, 204; 181, 205; 181, 206; 185, 207; 185, 208; 187, 209; 187, 210; 188, 211; 188, 212; 189, 213; 189, 214; 191, 215; 191, 216; 192, 217; 193, 218; 193, 219; 194, 220; 194, 221; 195, 222; 211, 223; 211, 224; 212, 225; 212, 226; 217, 227; 217, 228; 218, 229; 218, 230; 219, 231; 220, 232; 220, 233; 221, 234; 222, 235; 222, 236; 223, 237; 223, 238; 224, 239; 224, 240; 225, 241; 225, 242; 228, 243; 229, 244; 229, 245; 230, 246; 230, 247; 231, 248; 232, 249; 232, 250; 234, 251; 236, 252; 243, 253; 243, 254; 244, 255; 244, 256; 246, 257; 246, 258; 248, 259; 248, 260; 250, 261; 251, 262; 251, 263; 252, 264; 252, 265; 254, 266; 254, 267; 256, 268; 259, 269; 259, 270; 260, 271; 260, 272; 262, 273; 262, 274; 263, 275; 263, 276 | def _ToJSonObj(self, columns_order=None, order_by=()):
"""Returns an object suitable to be converted to JSON.
Args:
columns_order: Optional. A list of all column IDs in the order in which
you want them created in the output table. If specified,
all column IDs must be present.
order_by: Optional. Specifies the name of the column(s) to sort by.
Passed as is to _PreparedData().
Returns:
A dictionary object for use by ToJSon or ToJSonResponse.
"""
if columns_order is None:
columns_order = [col["id"] for col in self.__columns]
col_dict = dict([(col["id"], col) for col in self.__columns])
# Creating the column JSON objects
col_objs = []
for col_id in columns_order:
col_obj = {"id": col_dict[col_id]["id"],
"label": col_dict[col_id]["label"],
"type": col_dict[col_id]["type"]}
if col_dict[col_id]["custom_properties"]:
col_obj["p"] = col_dict[col_id]["custom_properties"]
col_objs.append(col_obj)
# Creating the rows jsons
row_objs = []
for row, cp in self._PreparedData(order_by):
cell_objs = []
for col in columns_order:
value = self.CoerceValue(row.get(col, None), col_dict[col]["type"])
if value is None:
cell_obj = None
elif isinstance(value, tuple):
cell_obj = {"v": value[0]}
if len(value) > 1 and value[1] is not None:
cell_obj["f"] = value[1]
if len(value) == 3:
cell_obj["p"] = value[2]
else:
cell_obj = {"v": value}
cell_objs.append(cell_obj)
row_obj = {"c": cell_objs}
if cp:
row_obj["p"] = cp
row_objs.append(row_obj)
json_obj = {"cols": col_objs, "rows": row_objs}
if self.custom_properties:
json_obj["p"] = self.custom_properties
return json_obj |
0, module; 1, function_definition; 2, function_name:ToJSon; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, if_statement; 11, return_statement; 12, identifier:columns_order; 13, None; 14, identifier:order_by; 15, tuple; 16, comment:"""Returns a string that can be used in a JS DataTable constructor.
This method writes a JSON string that can be passed directly into a Google
Visualization API DataTable constructor. Use this output if you are
hosting the visualization HTML on your site, and want to code the data
table in Python. Pass this string into the
google.visualization.DataTable constructor, e.g,:
... on my page that hosts my visualization ...
google.setOnLoadCallback(drawTable);
function drawTable() {
var data = new google.visualization.DataTable(_my_JSon_string, 0.6);
myTable.draw(data);
}
Args:
columns_order: Optional. Specifies the order of columns in the
output table. Specify a list of all column IDs in the order
in which you want the table created.
Note that you must list all column IDs in this parameter,
if you use it.
order_by: Optional. Specifies the name of the column(s) to sort by.
Passed as is to _PreparedData().
Returns:
A JSon constructor string to generate a JS DataTable with the data
stored in the DataTable object.
Example result (the result is without the newlines):
{cols: [{id:"a",label:"a",type:"number"},
{id:"b",label:"b",type:"string"},
{id:"c",label:"c",type:"number"}],
rows: [{c:[{v:1},{v:"z"},{v:2}]}, c:{[{v:3,f:"3$"},{v:"w"},null]}],
p: {'foo': 'bar'}}
Raises:
DataTableException: The data does not match the type.
"""; 17, assignment; 18, not_operator; 19, block; 20, identifier:encoded_response_str; 21, identifier:encoded_response_str; 22, call; 23, call; 24, return_statement; 25, attribute; 26, argument_list; 27, identifier:isinstance; 28, argument_list; 29, call; 30, call; 31, identifier:encode; 32, call; 33, identifier:encoded_response_str; 34, identifier:str; 35, attribute; 36, argument_list; 37, identifier:DataTableJSONEncoder; 38, argument_list; 39, attribute; 40, argument_list; 41, identifier:encoded_response_str; 42, identifier:encode; 43, string:"utf-8"; 44, identifier:self; 45, identifier:_ToJSonObj; 46, identifier:columns_order; 47, identifier:order_by | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 6, 12; 6, 13; 7, 14; 7, 15; 8, 16; 9, 17; 10, 18; 10, 19; 11, 20; 17, 21; 17, 22; 18, 23; 19, 24; 22, 25; 22, 26; 23, 27; 23, 28; 24, 29; 25, 30; 25, 31; 26, 32; 28, 33; 28, 34; 29, 35; 29, 36; 30, 37; 30, 38; 32, 39; 32, 40; 35, 41; 35, 42; 36, 43; 39, 44; 39, 45; 40, 46; 40, 47 | def ToJSon(self, columns_order=None, order_by=()):
"""Returns a string that can be used in a JS DataTable constructor.
This method writes a JSON string that can be passed directly into a Google
Visualization API DataTable constructor. Use this output if you are
hosting the visualization HTML on your site, and want to code the data
table in Python. Pass this string into the
google.visualization.DataTable constructor, e.g,:
... on my page that hosts my visualization ...
google.setOnLoadCallback(drawTable);
function drawTable() {
var data = new google.visualization.DataTable(_my_JSon_string, 0.6);
myTable.draw(data);
}
Args:
columns_order: Optional. Specifies the order of columns in the
output table. Specify a list of all column IDs in the order
in which you want the table created.
Note that you must list all column IDs in this parameter,
if you use it.
order_by: Optional. Specifies the name of the column(s) to sort by.
Passed as is to _PreparedData().
Returns:
A JSon constructor string to generate a JS DataTable with the data
stored in the DataTable object.
Example result (the result is without the newlines):
{cols: [{id:"a",label:"a",type:"number"},
{id:"b",label:"b",type:"string"},
{id:"c",label:"c",type:"number"}],
rows: [{c:[{v:1},{v:"z"},{v:2}]}, c:{[{v:3,f:"3$"},{v:"w"},null]}],
p: {'foo': 'bar'}}
Raises:
DataTableException: The data does not match the type.
"""
encoded_response_str = DataTableJSONEncoder().encode(self._ToJSonObj(columns_order, order_by))
if not isinstance(encoded_response_str, str):
return encoded_response_str.encode("utf-8")
return encoded_response_str |
0, module; 1, function_definition; 2, function_name:_radix_sort; 3, parameters; 4, block; 5, identifier:L; 6, default_parameter; 7, expression_statement; 8, if_statement; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, expression_statement; 13, return_statement; 14, identifier:i; 15, integer:0; 16, comment:"""
Most significant char radix sort
"""; 17, comparison_operator:len(L) <= 1; 18, block; 19, assignment; 20, assignment; 21, identifier:s; 22, identifier:L; 23, block; 24, assignment; 25, binary_operator:done_bucket + [ b for blist in buckets for b in blist ]; 26, call; 27, integer:1; 28, return_statement; 29, identifier:done_bucket; 30, list; 31, identifier:buckets; 32, list_comprehension; 33, if_statement; 34, identifier:buckets; 35, list_comprehension; 36, identifier:done_bucket; 37, list_comprehension; 38, identifier:len; 39, argument_list; 40, identifier:L; 41, list; 42, for_in_clause; 43, comparison_operator:i >= len(s); 44, block; 45, else_clause; 46, call; 47, for_in_clause; 48, identifier:b; 49, for_in_clause; 50, for_in_clause; 51, identifier:L; 52, identifier:x; 53, call; 54, identifier:i; 55, call; 56, expression_statement; 57, block; 58, identifier:_radix_sort; 59, argument_list; 60, identifier:b; 61, identifier:buckets; 62, identifier:blist; 63, identifier:buckets; 64, identifier:b; 65, identifier:blist; 66, identifier:range; 67, argument_list; 68, identifier:len; 69, argument_list; 70, call; 71, expression_statement; 72, identifier:b; 73, binary_operator:i + 1; 74, integer:255; 75, identifier:s; 76, attribute; 77, argument_list; 78, call; 79, identifier:i; 80, integer:1; 81, identifier:done_bucket; 82, identifier:append; 83, identifier:s; 84, attribute; 85, argument_list; 86, subscript; 87, identifier:append; 88, identifier:s; 89, identifier:buckets; 90, call; 91, identifier:ord; 92, argument_list; 93, subscript; 94, identifier:s; 95, identifier:i | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 6, 14; 6, 15; 7, 16; 8, 17; 8, 18; 9, 19; 10, 20; 11, 21; 11, 22; 11, 23; 12, 24; 13, 25; 17, 26; 17, 27; 18, 28; 19, 29; 19, 30; 20, 31; 20, 32; 23, 33; 24, 34; 24, 35; 25, 36; 25, 37; 26, 38; 26, 39; 28, 40; 32, 41; 32, 42; 33, 43; 33, 44; 33, 45; 35, 46; 35, 47; 37, 48; 37, 49; 37, 50; 39, 51; 42, 52; 42, 53; 43, 54; 43, 55; 44, 56; 45, 57; 46, 58; 46, 59; 47, 60; 47, 61; 49, 62; 49, 63; 50, 64; 50, 65; 53, 66; 53, 67; 55, 68; 55, 69; 56, 70; 57, 71; 59, 72; 59, 73; 67, 74; 69, 75; 70, 76; 70, 77; 71, 78; 73, 79; 73, 80; 76, 81; 76, 82; 77, 83; 78, 84; 78, 85; 84, 86; 84, 87; 85, 88; 86, 89; 86, 90; 90, 91; 90, 92; 92, 93; 93, 94; 93, 95 | def _radix_sort(L, i=0):
"""
Most significant char radix sort
"""
if len(L) <= 1:
return L
done_bucket = []
buckets = [ [] for x in range(255) ]
for s in L:
if i >= len(s):
done_bucket.append(s)
else:
buckets[ ord(s[i]) ].append(s)
buckets = [ _radix_sort(b, i + 1) for b in buckets ]
return done_bucket + [ b for blist in buckets for b in blist ] |
0, module; 1, function_definition; 2, function_name:fixed_legend_filter_sort; 3, parameters; 4, block; 5, identifier:self; 6, identifier:fixed_legend_filter_sort; 7, expression_statement; 8, expression_statement; 9, comment:# noqa: E501; 10, if_statement; 11, expression_statement; 12, comment:"""Sets the fixed_legend_filter_sort of this ChartSettings.
Whether to display \"Top\"- or \"Bottom\"-ranked series in the fixed legend # noqa: E501
:param fixed_legend_filter_sort: The fixed_legend_filter_sort of this ChartSettings. # noqa: E501
:type: str
"""; 13, assignment; 14, comparison_operator:fixed_legend_filter_sort not in allowed_values; 15, block; 16, assignment; 17, identifier:allowed_values; 18, list; 19, identifier:fixed_legend_filter_sort; 20, identifier:allowed_values; 21, raise_statement; 22, attribute; 23, identifier:fixed_legend_filter_sort; 24, string:"TOP"; 25, string:"BOTTOM"; 26, call; 27, identifier:self; 28, identifier:_fixed_legend_filter_sort; 29, identifier:ValueError; 30, argument_list; 31, call; 32, attribute; 33, argument_list; 34, string:"Invalid value for `fixed_legend_filter_sort` ({0}), must be one of {1}"; 35, comment:# noqa: E501; 36, identifier:format; 37, identifier:fixed_legend_filter_sort; 38, identifier:allowed_values | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 7, 12; 8, 13; 10, 14; 10, 15; 11, 16; 13, 17; 13, 18; 14, 19; 14, 20; 15, 21; 16, 22; 16, 23; 18, 24; 18, 25; 21, 26; 22, 27; 22, 28; 26, 29; 26, 30; 30, 31; 31, 32; 31, 33; 32, 34; 32, 35; 32, 36; 33, 37; 33, 38 | def fixed_legend_filter_sort(self, fixed_legend_filter_sort):
"""Sets the fixed_legend_filter_sort of this ChartSettings.
Whether to display \"Top\"- or \"Bottom\"-ranked series in the fixed legend # noqa: E501
:param fixed_legend_filter_sort: The fixed_legend_filter_sort of this ChartSettings. # noqa: E501
:type: str
"""
allowed_values = ["TOP", "BOTTOM"] # noqa: E501
if fixed_legend_filter_sort not in allowed_values:
raise ValueError(
"Invalid value for `fixed_legend_filter_sort` ({0}), must be one of {1}" # noqa: E501
.format(fixed_legend_filter_sort, allowed_values)
)
self._fixed_legend_filter_sort = fixed_legend_filter_sort |
0, module; 1, function_definition; 2, function_name:print_table; 3, parameters; 4, block; 5, identifier:title; 6, identifier:headers; 7, identifier:rows; 8, default_parameter; 9, expression_statement; 10, if_statement; 11, if_statement; 12, expression_statement; 13, expression_statement; 14, identifier:sort_columns; 15, None; 16, comment:"""
Print a table of rows with headers using tabulate.
Parameters:
title (:term: string):
String that will be output before the Table.
headers (list or tuple):
List of strings that defines the header for each row. Each string
in this list may be multiline by inserting EOL in the string
rows (iterable of iterables)
The outer iterable is the rows. The inner iterables are the colums
for each row
args (int or list of int that defines sort)
Defines the cols that will be sorted. If int, it defines the column
that will be sorted. If list of int, the sort is in sort order of
cols in the list (i.e. minor sorts to the left, major sorts to the
right)
"""; 17, comparison_operator:sort_columns is not None; 18, block; 19, identifier:title; 20, block; 21, else_clause; 22, call; 23, call; 24, identifier:sort_columns; 25, None; 26, if_statement; 27, expression_statement; 28, block; 29, identifier:print; 30, argument_list; 31, identifier:print; 32, argument_list; 33, call; 34, block; 35, elif_clause; 36, else_clause; 37, call; 38, expression_statement; 39, call; 40, string; 41, identifier:isinstance; 42, argument_list; 43, expression_statement; 44, call; 45, block; 46, block; 47, identifier:print; 48, argument_list; 49, call; 50, attribute; 51, argument_list; 52, identifier:sort_columns; 53, identifier:int; 54, assignment; 55, identifier:isinstance; 56, argument_list; 57, expression_statement; 58, assert_statement; 59, binary_operator:'\n%s:' % title; 60, identifier:print; 61, argument_list; 62, identifier:tabulate; 63, identifier:tabulate; 64, identifier:rows; 65, identifier:headers; 66, keyword_argument; 67, identifier:rows; 68, call; 69, identifier:sort_columns; 70, tuple; 71, assignment; 72, False; 73, string:"Sort_columns must be int or list/tuple of int"; 74, string; 75, identifier:title; 76, string; 77, identifier:tablefmt; 78, string; 79, identifier:sorted; 80, argument_list; 81, identifier:list; 82, identifier:tuple; 83, identifier:rows; 84, call; 85, string_content; 86, string_content:simple; 87, identifier:rows; 88, keyword_argument; 89, identifier:sorted; 90, argument_list; 91, escape_sequence:\n; 92, identifier:key; 93, call; 94, identifier:rows; 95, keyword_argument; 96, identifier:itemgetter; 97, argument_list; 98, identifier:key; 99, call; 100, identifier:sort_columns; 101, identifier:itemgetter; 102, argument_list; 103, list_splat; 104, identifier:sort_columns | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 8, 14; 8, 15; 9, 16; 10, 17; 10, 18; 11, 19; 11, 20; 11, 21; 12, 22; 13, 23; 17, 24; 17, 25; 18, 26; 20, 27; 21, 28; 22, 29; 22, 30; 23, 31; 23, 32; 26, 33; 26, 34; 26, 35; 26, 36; 27, 37; 28, 38; 30, 39; 32, 40; 33, 41; 33, 42; 34, 43; 35, 44; 35, 45; 36, 46; 37, 47; 37, 48; 38, 49; 39, 50; 39, 51; 42, 52; 42, 53; 43, 54; 44, 55; 44, 56; 45, 57; 46, 58; 48, 59; 49, 60; 49, 61; 50, 62; 50, 63; 51, 64; 51, 65; 51, 66; 54, 67; 54, 68; 56, 69; 56, 70; 57, 71; 58, 72; 58, 73; 59, 74; 59, 75; 61, 76; 66, 77; 66, 78; 68, 79; 68, 80; 70, 81; 70, 82; 71, 83; 71, 84; 74, 85; 78, 86; 80, 87; 80, 88; 84, 89; 84, 90; 85, 91; 88, 92; 88, 93; 90, 94; 90, 95; 93, 96; 93, 97; 95, 98; 95, 99; 97, 100; 99, 101; 99, 102; 102, 103; 103, 104 | def print_table(title, headers, rows, sort_columns=None):
"""
Print a table of rows with headers using tabulate.
Parameters:
title (:term: string):
String that will be output before the Table.
headers (list or tuple):
List of strings that defines the header for each row. Each string
in this list may be multiline by inserting EOL in the string
rows (iterable of iterables)
The outer iterable is the rows. The inner iterables are the colums
for each row
args (int or list of int that defines sort)
Defines the cols that will be sorted. If int, it defines the column
that will be sorted. If list of int, the sort is in sort order of
cols in the list (i.e. minor sorts to the left, major sorts to the
right)
"""
if sort_columns is not None:
if isinstance(sort_columns, int):
rows = sorted(rows, key=itemgetter(sort_columns))
elif isinstance(sort_columns, (list, tuple)):
rows = sorted(rows, key=itemgetter(*sort_columns))
else:
assert False, "Sort_columns must be int or list/tuple of int"
if title:
print('\n%s:' % title)
else:
print('')
print(tabulate.tabulate(rows, headers, tablefmt='simple'))
print('') |
0, module; 1, function_definition; 2, function_name:formatted; 3, parameters; 4, comment:# pylint: disable=line-too-long; 5, block; 6, identifier:self; 7, expression_statement; 8, comment:# noqa: E501; 9, comment:# pylint: enable=line-too-long; 10, expression_statement; 11, if_statement; 12, return_statement; 13, comment:"""
Return a human readable string with the statistics for this container.
The operations are sorted by decreasing average time.
The three columns for `ServerTime` are included only if the WBEM server
has returned WBEM server response times.
Example if statistics are enabled::
Statistics (times in seconds, lengths in Bytes):
Count Excep ClientTime ServerTime RequestLen ReplyLen Operation
Cnt Avg Min Max Avg Min Max Avg Min Max Avg Min Max
3 0 0.234 0.100 0.401 0.204 0.080 0.361 1233 1000 1500 26667 20000 35000 EnumerateInstances
1 0 0.100 0.100 0.100 0.080 0.080 0.080 1200 1200 1200 22000 22000 22000 EnumerateInstanceNames
. . .
Example if statistics are disabled::
Statistics (times in seconds, lengths in Bytes):
Disabled
"""; 14, assignment; 15, attribute; 16, block; 17, else_clause; 18, call; 19, identifier:ret; 20, string:"Statistics (times in seconds, lengths in Bytes):\n"; 21, identifier:self; 22, identifier:enabled; 23, expression_statement; 24, comment:# Test to see if any server time is non-zero; 25, expression_statement; 26, for_statement; 27, comment:# pylint: disable=protected-access; 28, if_statement; 29, for_statement; 30, block; 31, attribute; 32, argument_list; 33, assignment; 34, assignment; 35, pattern_list; 36, identifier:snapshot; 37, comment:# pylint: disable=unused-variable; 38, comment:# pylint: disable=protected-access; 39, block; 40, identifier:include_svr; 41, block; 42, else_clause; 43, pattern_list; 44, identifier:snapshot; 45, comment:# pylint: disable=unused-variable; 46, block; 47, expression_statement; 48, identifier:ret; 49, identifier:strip; 50, identifier:snapshot; 51, call; 52, identifier:include_svr; 53, False; 54, identifier:name; 55, identifier:stats; 56, if_statement; 57, expression_statement; 58, block; 59, identifier:name; 60, identifier:stats; 61, expression_statement; 62, augmented_assignment; 63, identifier:sorted; 64, argument_list; 65, attribute; 66, block; 67, augmented_assignment; 68, expression_statement; 69, augmented_assignment; 70, identifier:ret; 71, string:"Disabled"; 72, call; 73, keyword_argument; 74, keyword_argument; 75, identifier:stats; 76, identifier:_server_time_stored; 77, expression_statement; 78, identifier:ret; 79, attribute; 80, augmented_assignment; 81, identifier:ret; 82, call; 83, attribute; 84, argument_list; 85, identifier:key; 86, lambda; 87, identifier:reverse; 88, True; 89, assignment; 90, identifier:OperationStatistic; 91, identifier:_formatted_header_w_svr; 92, identifier:ret; 93, attribute; 94, attribute; 95, argument_list; 96, identifier:self; 97, identifier:snapshot; 98, lambda_parameters; 99, attribute; 100, identifier:include_svr; 101, True; 102, identifier:OperationStatistic; 103, identifier:_formatted_header; 104, identifier:stats; 105, identifier:formatted; 106, identifier:include_svr; 107, identifier:item; 108, subscript; 109, identifier:avg_time; 110, identifier:item; 111, integer:1 | 0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 5, 7; 5, 8; 5, 9; 5, 10; 5, 11; 5, 12; 7, 13; 10, 14; 11, 15; 11, 16; 11, 17; 12, 18; 14, 19; 14, 20; 15, 21; 15, 22; 16, 23; 16, 24; 16, 25; 16, 26; 16, 27; 16, 28; 16, 29; 17, 30; 18, 31; 18, 32; 23, 33; 25, 34; 26, 35; 26, 36; 26, 37; 26, 38; 26, 39; 28, 40; 28, 41; 28, 42; 29, 43; 29, 44; 29, 45; 29, 46; 30, 47; 31, 48; 31, 49; 33, 50; 33, 51; 34, 52; 34, 53; 35, 54; 35, 55; 39, 56; 41, 57; 42, 58; 43, 59; 43, 60; 46, 61; 47, 62; 51, 63; 51, 64; 56, 65; 56, 66; 57, 67; 58, 68; 61, 69; 62, 70; 62, 71; 64, 72; 64, 73; 64, 74; 65, 75; 65, 76; 66, 77; 67, 78; 67, 79; 68, 80; 69, 81; 69, 82; 72, 83; 72, 84; 73, 85; 73, 86; 74, 87; 74, 88; 77, 89; 79, 90; 79, 91; 80, 92; 80, 93; 82, 94; 82, 95; 83, 96; 83, 97; 86, 98; 86, 99; 89, 100; 89, 101; 93, 102; 93, 103; 94, 104; 94, 105; 95, 106; 98, 107; 99, 108; 99, 109; 108, 110; 108, 111 | def formatted(self):
# pylint: disable=line-too-long
"""
Return a human readable string with the statistics for this container.
The operations are sorted by decreasing average time.
The three columns for `ServerTime` are included only if the WBEM server
has returned WBEM server response times.
Example if statistics are enabled::
Statistics (times in seconds, lengths in Bytes):
Count Excep ClientTime ServerTime RequestLen ReplyLen Operation
Cnt Avg Min Max Avg Min Max Avg Min Max Avg Min Max
3 0 0.234 0.100 0.401 0.204 0.080 0.361 1233 1000 1500 26667 20000 35000 EnumerateInstances
1 0 0.100 0.100 0.100 0.080 0.080 0.080 1200 1200 1200 22000 22000 22000 EnumerateInstanceNames
. . .
Example if statistics are disabled::
Statistics (times in seconds, lengths in Bytes):
Disabled
""" # noqa: E501
# pylint: enable=line-too-long
ret = "Statistics (times in seconds, lengths in Bytes):\n"
if self.enabled:
snapshot = sorted(self.snapshot(),
key=lambda item: item[1].avg_time,
reverse=True)
# Test to see if any server time is non-zero
include_svr = False
for name, stats in snapshot: # pylint: disable=unused-variable
# pylint: disable=protected-access
if stats._server_time_stored:
include_svr = True
# pylint: disable=protected-access
if include_svr:
ret += OperationStatistic._formatted_header_w_svr
else:
ret += OperationStatistic._formatted_header
for name, stats in snapshot: # pylint: disable=unused-variable
ret += stats.formatted(include_svr)
else:
ret += "Disabled"
return ret.strip() |
0, module; 1, function_definition; 2, function_name:get_clusters; 3, parameters; 4, block; 5, identifier:self; 6, identifier:platform; 7, identifier:retry_contexts; 8, identifier:all_clusters; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, while_statement; 13, expression_statement; 14, expression_statement; 15, return_statement; 16, string; 17, assignment; 18, assignment; 19, boolean_operator; 20, block; 21, assignment; 22, assignment; 23, identifier:ret; 24, string_content:return clusters sorted by load.; 25, identifier:possible_cluster_info; 26, dictionary; 27, identifier:candidates; 28, call; 29, identifier:candidates; 30, not_operator; 31, expression_statement; 32, for_statement; 33, expression_statement; 34, identifier:ret; 35, call; 36, identifier:ret; 37, call; 38, identifier:set; 39, argument_list; 40, identifier:possible_cluster_info; 41, call; 42, identifier:cluster; 43, call; 44, block; 45, augmented_assignment; 46, identifier:sorted; 47, argument_list; 48, identifier:sorted; 49, argument_list; 50, call; 51, identifier:wait_for_any_cluster; 52, argument_list; 53, identifier:sorted; 54, argument_list; 55, expression_statement; 56, if_statement; 57, if_statement; 58, try_statement; 59, identifier:candidates; 60, call; 61, call; 62, keyword_argument; 63, identifier:ret; 64, keyword_argument; 65, attribute; 66, argument_list; 67, identifier:retry_contexts; 68, identifier:candidates; 69, keyword_argument; 70, assignment; 71, attribute; 72, block; 73, attribute; 74, block; 75, block; 76, except_clause; 77, identifier:set; 78, argument_list; 79, attribute; 80, argument_list; 81, identifier:key; 82, lambda; 83, identifier:key; 84, lambda; 85, identifier:copy; 86, identifier:copy; 87, identifier:all_clusters; 88, identifier:key; 89, call; 90, identifier:ctx; 91, subscript; 92, identifier:ctx; 93, identifier:in_retry_wait; 94, continue_statement; 95, identifier:ctx; 96, identifier:failed; 97, continue_statement; 98, expression_statement; 99, expression_statement; 100, identifier:OsbsException; 101, block; 102, list_comprehension; 103, identifier:possible_cluster_info; 104, identifier:values; 105, lambda_parameters; 106, attribute; 107, lambda_parameters; 108, attribute; 109, identifier:attrgetter; 110, argument_list; 111, identifier:retry_contexts; 112, attribute; 113, assignment; 114, assignment; 115, expression_statement; 116, identifier:c; 117, for_in_clause; 118, if_clause; 119, identifier:c; 120, attribute; 121, identifier:priority; 122, identifier:c; 123, identifier:c; 124, identifier:load; 125, string; 126, identifier:cluster; 127, identifier:name; 128, identifier:cluster_info; 129, call; 130, subscript; 131, identifier:cluster_info; 132, call; 133, identifier:c; 134, identifier:candidates; 135, attribute; 136, identifier:c; 137, identifier:cluster; 138, string_content:priority; 139, attribute; 140, argument_list; 141, identifier:possible_cluster_info; 142, identifier:cluster; 143, attribute; 144, argument_list; 145, subscript; 146, identifier:failed; 147, identifier:self; 148, identifier:get_cluster_info; 149, identifier:cluster; 150, identifier:platform; 151, identifier:ctx; 152, identifier:try_again_later; 153, attribute; 154, identifier:retry_contexts; 155, attribute; 156, identifier:self; 157, identifier:find_cluster_retry_delay; 158, identifier:c; 159, identifier:name | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 9, 16; 10, 17; 11, 18; 12, 19; 12, 20; 13, 21; 14, 22; 15, 23; 16, 24; 17, 25; 17, 26; 18, 27; 18, 28; 19, 29; 19, 30; 20, 31; 20, 32; 20, 33; 21, 34; 21, 35; 22, 36; 22, 37; 28, 38; 28, 39; 30, 40; 31, 41; 32, 42; 32, 43; 32, 44; 33, 45; 35, 46; 35, 47; 37, 48; 37, 49; 39, 50; 41, 51; 41, 52; 43, 53; 43, 54; 44, 55; 44, 56; 44, 57; 44, 58; 45, 59; 45, 60; 47, 61; 47, 62; 49, 63; 49, 64; 50, 65; 50, 66; 52, 67; 54, 68; 54, 69; 55, 70; 56, 71; 56, 72; 57, 73; 57, 74; 58, 75; 58, 76; 60, 77; 60, 78; 61, 79; 61, 80; 62, 81; 62, 82; 64, 83; 64, 84; 65, 85; 65, 86; 66, 87; 69, 88; 69, 89; 70, 90; 70, 91; 71, 92; 71, 93; 72, 94; 73, 95; 73, 96; 74, 97; 75, 98; 75, 99; 76, 100; 76, 101; 78, 102; 79, 103; 79, 104; 82, 105; 82, 106; 84, 107; 84, 108; 89, 109; 89, 110; 91, 111; 91, 112; 98, 113; 99, 114; 101, 115; 102, 116; 102, 117; 102, 118; 105, 119; 106, 120; 106, 121; 107, 122; 108, 123; 108, 124; 110, 125; 112, 126; 112, 127; 113, 128; 113, 129; 114, 130; 114, 131; 115, 132; 117, 133; 117, 134; 118, 135; 120, 136; 120, 137; 125, 138; 129, 139; 129, 140; 130, 141; 130, 142; 132, 143; 132, 144; 135, 145; 135, 146; 139, 147; 139, 148; 140, 149; 140, 150; 143, 151; 143, 152; 144, 153; 145, 154; 145, 155; 153, 156; 153, 157; 155, 158; 155, 159 | def get_clusters(self, platform, retry_contexts, all_clusters):
''' return clusters sorted by load. '''
possible_cluster_info = {}
candidates = set(copy.copy(all_clusters))
while candidates and not possible_cluster_info:
wait_for_any_cluster(retry_contexts)
for cluster in sorted(candidates, key=attrgetter('priority')):
ctx = retry_contexts[cluster.name]
if ctx.in_retry_wait:
continue
if ctx.failed:
continue
try:
cluster_info = self.get_cluster_info(cluster, platform)
possible_cluster_info[cluster] = cluster_info
except OsbsException:
ctx.try_again_later(self.find_cluster_retry_delay)
candidates -= set([c for c in candidates if retry_contexts[c.name].failed])
ret = sorted(possible_cluster_info.values(), key=lambda c: c.cluster.priority)
ret = sorted(ret, key=lambda c: c.load)
return ret |
0, module; 1, function_definition; 2, function_name:get_profile_names_and_default; 3, parameters; 4, type; 5, block; 6, parenthesized_expression; 7, expression_statement; 8, with_statement; 9, subscript; 10, comment:"""Return the list of profile names and the default profile object.
The list of names is sorted.
"""; 11, with_clause; 12, block; 13, attribute; 14, subscript; 15, subscript; 16, with_item; 17, return_statement; 18, identifier:typing; 19, identifier:Tuple; 20, attribute; 21, identifier:str; 22, attribute; 23, identifier:Profile; 24, as_pattern; 25, expression_list; 26, identifier:typing; 27, identifier:Sequence; 28, identifier:typing; 29, identifier:Optional; 30, call; 31, as_pattern_target; 32, call; 33, attribute; 34, attribute; 35, argument_list; 36, identifier:config; 37, identifier:sorted; 38, argument_list; 39, identifier:config; 40, identifier:default; 41, identifier:ProfileStore; 42, identifier:open; 43, identifier:config | 0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 4, 6; 5, 7; 5, 8; 6, 9; 7, 10; 8, 11; 8, 12; 9, 13; 9, 14; 9, 15; 11, 16; 12, 17; 13, 18; 13, 19; 14, 20; 14, 21; 15, 22; 15, 23; 16, 24; 17, 25; 20, 26; 20, 27; 22, 28; 22, 29; 24, 30; 24, 31; 25, 32; 25, 33; 30, 34; 30, 35; 31, 36; 32, 37; 32, 38; 33, 39; 33, 40; 34, 41; 34, 42; 38, 43 | def get_profile_names_and_default() -> (
typing.Tuple[typing.Sequence[str], typing.Optional[Profile]]):
"""Return the list of profile names and the default profile object.
The list of names is sorted.
"""
with ProfileStore.open() as config:
return sorted(config), config.default |
0, module; 1, function_definition; 2, function_name:print_all_commands; 3, parameters; 4, block; 5, identifier:self; 6, keyword_separator; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, for_statement; 14, if_statement; 15, identifier:no_pager; 16, False; 17, comment:"""Print help for all commands.
Commands are sorted in alphabetical order and wrapping is done
based on the width of the terminal.
"""; 18, assignment; 19, assignment; 20, assignment; 21, assignment; 22, identifier:name; 23, identifier:command_names; 24, block; 25, identifier:no_pager; 26, block; 27, else_clause; 28, identifier:formatter; 29, call; 30, identifier:command_names; 31, call; 32, identifier:max_name_len; 33, binary_operator:max([len(name) for name in command_names]) + 1; 34, identifier:commands; 35, string:""; 36, expression_statement; 37, expression_statement; 38, expression_statement; 39, while_statement; 40, if_statement; 41, expression_statement; 42, block; 43, attribute; 44, argument_list; 45, identifier:sorted; 46, argument_list; 47, call; 48, integer:1; 49, assignment; 50, assignment; 51, assignment; 52, comparison_operator:len(command_line) > formatter._width; 53, block; 54, identifier:command_line; 55, block; 56, call; 57, expression_statement; 58, attribute; 59, identifier:_get_formatter; 60, call; 61, identifier:max; 62, argument_list; 63, identifier:command; 64, subscript; 65, identifier:extra_padding; 66, binary_operator:max_name_len - len(name); 67, identifier:command_line; 68, binary_operator:'%s%s%s' % (
name, ' ' * extra_padding, command.description); 69, call; 70, attribute; 71, expression_statement; 72, expression_statement; 73, if_statement; 74, expression_statement; 75, identifier:print; 76, argument_list; 77, call; 78, identifier:self; 79, identifier:parent_parser; 80, attribute; 81, argument_list; 82, list_comprehension; 83, attribute; 84, identifier:name; 85, identifier:max_name_len; 86, call; 87, string; 88, tuple; 89, identifier:len; 90, argument_list; 91, identifier:formatter; 92, identifier:_width; 93, assignment; 94, augmented_assignment; 95, comparison_operator:len(lines) > 1; 96, block; 97, else_clause; 98, augmented_assignment; 99, subscript; 100, identifier:print_with_pager; 101, argument_list; 102, attribute; 103, identifier:keys; 104, call; 105, for_in_clause; 106, attribute; 107, identifier:choices; 108, identifier:len; 109, argument_list; 110, string_content:%s%s%s; 111, identifier:name; 112, binary_operator:' ' * extra_padding; 113, attribute; 114, identifier:command_line; 115, identifier:lines; 116, call; 117, identifier:commands; 118, binary_operator:"%s\n" % lines[0]; 119, call; 120, integer:1; 121, expression_statement; 122, expression_statement; 123, block; 124, identifier:commands; 125, binary_operator:"%s\n" % command_line; 126, identifier:commands; 127, slice; 128, subscript; 129, attribute; 130, identifier:choices; 131, identifier:len; 132, argument_list; 133, identifier:name; 134, identifier:command_names; 135, attribute; 136, identifier:subparsers; 137, identifier:name; 138, string; 139, identifier:extra_padding; 140, identifier:command; 141, identifier:description; 142, attribute; 143, argument_list; 144, string:"%s\n"; 145, subscript; 146, identifier:len; 147, argument_list; 148, assignment; 149, assignment; 150, expression_statement; 151, string:"%s\n"; 152, identifier:command_line; 153, unary_operator; 154, identifier:commands; 155, slice; 156, attribute; 157, identifier:subparsers; 158, identifier:name; 159, identifier:self; 160, identifier:parent_parser; 161, string_content:; 162, identifier:textwrap; 163, identifier:wrap; 164, identifier:command_line; 165, attribute; 166, identifier:lines; 167, integer:0; 168, identifier:lines; 169, subscript; 170, binary_operator:(' ' * max_name_len) + lines[1]; 171, identifier:command_line; 172, call; 173, assignment; 174, integer:1; 175, unary_operator; 176, identifier:self; 177, identifier:parent_parser; 178, identifier:formatter; 179, identifier:_width; 180, identifier:lines; 181, integer:1; 182, parenthesized_expression; 183, subscript; 184, attribute; 185, argument_list; 186, identifier:command_line; 187, None; 188, integer:1; 189, binary_operator:' ' * max_name_len; 190, identifier:lines; 191, integer:1; 192, string; 193, identifier:join; 194, subscript; 195, string; 196, identifier:max_name_len; 197, string_content:; 198, identifier:lines; 199, slice; 200, string_content:; 201, integer:1 | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 7, 15; 7, 16; 8, 17; 9, 18; 10, 19; 11, 20; 12, 21; 13, 22; 13, 23; 13, 24; 14, 25; 14, 26; 14, 27; 18, 28; 18, 29; 19, 30; 19, 31; 20, 32; 20, 33; 21, 34; 21, 35; 24, 36; 24, 37; 24, 38; 24, 39; 24, 40; 26, 41; 27, 42; 29, 43; 29, 44; 31, 45; 31, 46; 33, 47; 33, 48; 36, 49; 37, 50; 38, 51; 39, 52; 39, 53; 40, 54; 40, 55; 41, 56; 42, 57; 43, 58; 43, 59; 46, 60; 47, 61; 47, 62; 49, 63; 49, 64; 50, 65; 50, 66; 51, 67; 51, 68; 52, 69; 52, 70; 53, 71; 53, 72; 53, 73; 55, 74; 56, 75; 56, 76; 57, 77; 58, 78; 58, 79; 60, 80; 60, 81; 62, 82; 64, 83; 64, 84; 66, 85; 66, 86; 68, 87; 68, 88; 69, 89; 69, 90; 70, 91; 70, 92; 71, 93; 72, 94; 73, 95; 73, 96; 73, 97; 74, 98; 76, 99; 77, 100; 77, 101; 80, 102; 80, 103; 82, 104; 82, 105; 83, 106; 83, 107; 86, 108; 86, 109; 87, 110; 88, 111; 88, 112; 88, 113; 90, 114; 93, 115; 93, 116; 94, 117; 94, 118; 95, 119; 95, 120; 96, 121; 96, 122; 97, 123; 98, 124; 98, 125; 99, 126; 99, 127; 101, 128; 102, 129; 102, 130; 104, 131; 104, 132; 105, 133; 105, 134; 106, 135; 106, 136; 109, 137; 112, 138; 112, 139; 113, 140; 113, 141; 116, 142; 116, 143; 118, 144; 118, 145; 119, 146; 119, 147; 121, 148; 122, 149; 123, 150; 125, 151; 125, 152; 127, 153; 128, 154; 128, 155; 129, 156; 129, 157; 132, 158; 135, 159; 135, 160; 138, 161; 142, 162; 142, 163; 143, 164; 143, 165; 145, 166; 145, 167; 147, 168; 148, 169; 148, 170; 149, 171; 149, 172; 150, 173; 153, 174; 155, 175; 156, 176; 156, 177; 165, 178; 165, 179; 169, 180; 169, 181; 170, 182; 170, 183; 172, 184; 172, 185; 173, 186; 173, 187; 175, 188; 182, 189; 183, 190; 183, 191; 184, 192; 184, 193; 185, 194; 189, 195; 189, 196; 192, 197; 194, 198; 194, 199; 195, 200; 199, 201 | def print_all_commands(self, *, no_pager=False):
"""Print help for all commands.
Commands are sorted in alphabetical order and wrapping is done
based on the width of the terminal.
"""
formatter = self.parent_parser._get_formatter()
command_names = sorted(self.parent_parser.subparsers.choices.keys())
max_name_len = max([len(name) for name in command_names]) + 1
commands = ""
for name in command_names:
command = self.parent_parser.subparsers.choices[name]
extra_padding = max_name_len - len(name)
command_line = '%s%s%s' % (
name, ' ' * extra_padding, command.description)
while len(command_line) > formatter._width:
lines = textwrap.wrap(command_line, formatter._width)
commands += "%s\n" % lines[0]
if len(lines) > 1:
lines[1] = (' ' * max_name_len) + lines[1]
command_line = ' '.join(lines[1:])
else:
command_line = None
if command_line:
commands += "%s\n" % command_line
if no_pager:
print(commands[:-1])
else:
print_with_pager(commands[:-1]) |
0, module; 1, function_definition; 2, function_name:rank_dated_files; 3, parameters; 4, block; 5, identifier:pattern; 6, identifier:dir; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, return_statement; 11, identifier:descending; 12, True; 13, comment:"""Search a directory for files that match a pattern. Return an ordered list of these files by filename.
Args:
pattern: The glob pattern to search for.
dir: Path to directory where the files will be searched for.
descending: Default True, will sort alphabetically by descending order.
Returns:
list: Rank-ordered list by filename.
"""; 14, assignment; 15, call; 16, identifier:files; 17, call; 18, identifier:sorted; 19, argument_list; 20, attribute; 21, argument_list; 22, identifier:files; 23, keyword_argument; 24, identifier:glob; 25, identifier:glob; 26, call; 27, identifier:reverse; 28, identifier:descending; 29, attribute; 30, argument_list; 31, identifier:op; 32, identifier:join; 33, identifier:dir; 34, identifier:pattern | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 7, 11; 7, 12; 8, 13; 9, 14; 10, 15; 14, 16; 14, 17; 15, 18; 15, 19; 17, 20; 17, 21; 19, 22; 19, 23; 20, 24; 20, 25; 21, 26; 23, 27; 23, 28; 26, 29; 26, 30; 29, 31; 29, 32; 30, 33; 30, 34 | def rank_dated_files(pattern, dir, descending=True):
"""Search a directory for files that match a pattern. Return an ordered list of these files by filename.
Args:
pattern: The glob pattern to search for.
dir: Path to directory where the files will be searched for.
descending: Default True, will sort alphabetically by descending order.
Returns:
list: Rank-ordered list by filename.
"""
files = glob.glob(op.join(dir, pattern))
return sorted(files, reverse=descending) |
0, module; 1, function_definition; 2, function_name:best_structures; 3, parameters; 4, block; 5, identifier:uniprot_id; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, expression_statement; 11, expression_statement; 12, if_statement; 13, comment:# if output dir is specified but not outname, use the uniprot; 14, if_statement; 15, if_statement; 16, comment:# Load a possibly existing json file; 17, if_statement; 18, expression_statement; 19, comment:# Filter for sequence identity percentage; 20, if_statement; 21, return_statement; 22, identifier:outname; 23, None; 24, identifier:outdir; 25, None; 26, identifier:seq_ident_cutoff; 27, float:0.0; 28, identifier:force_rerun; 29, False; 30, comment:"""Use the PDBe REST service to query for the best PDB structures for a UniProt ID.
More information found here: https://www.ebi.ac.uk/pdbe/api/doc/sifts.html
Link used to retrieve results: https://www.ebi.ac.uk/pdbe/api/mappings/best_structures/:accession
The list of PDB structures mapping to a UniProt accession sorted by coverage of the protein and, if the same, resolution.
Here is the ranking algorithm described by the PDB paper:
https://nar.oxfordjournals.org/content/44/D1/D385.full
"Finally, a single quality indicator is also calculated for each entry by taking the harmonic average
of all the percentile scores representing model and model-data-fit quality measures and then subtracting
10 times the numerical value of the resolution (in Angstrom) of the entry to ensure that resolution plays
a role in characterising the quality of a structure. This single empirical 'quality measure' value is used
by the PDBe query system to sort results and identify the 'best' structure in a given context. At present,
entries determined by methods other than X-ray crystallography do not have similar data quality information
available and are not considered as 'best structures'."
Args:
uniprot_id (str): UniProt Accession ID
outname (str): Basename of the output file of JSON results
outdir (str): Path to output directory of JSON results
seq_ident_cutoff (float): Cutoff results based on percent coverage (in decimal form)
force_rerun (bool): Obtain best structures mapping ignoring previously downloaded results
Returns:
list: Rank-ordered list of dictionaries representing chain-specific PDB entries. Keys are:
* pdb_id: the PDB ID which maps to the UniProt ID
* chain_id: the specific chain of the PDB which maps to the UniProt ID
* coverage: the percent coverage of the entire UniProt sequence
* resolution: the resolution of the structure
* start: the structure residue number which maps to the start of the mapped sequence
* end: the structure residue number which maps to the end of the mapped sequence
* unp_start: the sequence residue number which maps to the structure start
* unp_end: the sequence residue number which maps to the structure end
* experimental_method: type of experiment used to determine structure
* tax_id: taxonomic ID of the protein's original organism
"""; 31, assignment; 32, not_operator; 33, block; 34, boolean_operator; 35, block; 36, identifier:outname; 37, block; 38, not_operator; 39, block; 40, comment:# Otherwise run the web request; 41, else_clause; 42, assignment; 43, comparison_operator:seq_ident_cutoff != 0; 44, block; 45, identifier:data; 46, identifier:outfile; 47, string; 48, identifier:outdir; 49, expression_statement; 50, not_operator; 51, identifier:outdir; 52, expression_statement; 53, expression_statement; 54, expression_statement; 55, call; 56, with_statement; 57, expression_statement; 58, comment:# TODO: add a checker for a cached file of uniprot -> PDBs - can be generated within gempro pipeline and stored; 59, block; 60, identifier:data; 61, subscript; 62, identifier:seq_ident_cutoff; 63, integer:0; 64, for_statement; 65, assignment; 66, identifier:outname; 67, assignment; 68, assignment; 69, assignment; 70, attribute; 71, argument_list; 72, with_clause; 73, block; 74, call; 75, expression_statement; 76, if_statement; 77, comment:# Write the json file if specified; 78, if_statement; 79, call; 80, identifier:uniprot_id; 81, identifier:result; 82, identifier:data; 83, block; 84, identifier:outdir; 85, string; 86, identifier:outname; 87, identifier:uniprot_id; 88, identifier:outname; 89, call; 90, identifier:outfile; 91, call; 92, attribute; 93, identifier:force_rerun; 94, keyword_argument; 95, keyword_argument; 96, with_item; 97, expression_statement; 98, attribute; 99, argument_list; 100, assignment; 101, comparison_operator:response.status_code == 404; 102, block; 103, else_clause; 104, identifier:outfile; 105, block; 106, identifier:dict; 107, argument_list; 108, if_statement; 109, attribute; 110, argument_list; 111, attribute; 112, argument_list; 113, identifier:ssbio; 114, identifier:utils; 115, identifier:flag; 116, identifier:force_rerun; 117, identifier:outfile; 118, identifier:outfile; 119, as_pattern; 120, assignment; 121, identifier:log; 122, identifier:debug; 123, call; 124, identifier:response; 125, call; 126, attribute; 127, integer:404; 128, expression_statement; 129, expression_statement; 130, block; 131, with_statement; 132, expression_statement; 133, identifier:raw_data; 134, comparison_operator:result['coverage'] < seq_ident_cutoff; 135, block; 136, identifier:op; 137, identifier:join; 138, identifier:outdir; 139, identifier:outname; 140, string; 141, identifier:format; 142, identifier:outname; 143, call; 144, as_pattern_target; 145, identifier:raw_data; 146, call; 147, attribute; 148, argument_list; 149, attribute; 150, argument_list; 151, identifier:response; 152, identifier:status_code; 153, call; 154, assignment; 155, expression_statement; 156, expression_statement; 157, with_clause; 158, block; 159, call; 160, subscript; 161, identifier:seq_ident_cutoff; 162, expression_statement; 163, string_content:{}.json; 164, identifier:open; 165, argument_list; 166, identifier:f; 167, attribute; 168, argument_list; 169, string; 170, identifier:format; 171, identifier:uniprot_id; 172, identifier:requests; 173, identifier:get; 174, call; 175, keyword_argument; 176, attribute; 177, argument_list; 178, identifier:raw_data; 179, dictionary; 180, call; 181, assignment; 182, with_item; 183, expression_statement; 184, attribute; 185, argument_list; 186, identifier:result; 187, string; 188, call; 189, identifier:outfile; 190, string; 191, identifier:json; 192, identifier:load; 193, identifier:f; 194, string_content:{}: loaded existing json file; 195, attribute; 196, argument_list; 197, identifier:data; 198, dictionary; 199, identifier:log; 200, identifier:debug; 201, call; 202, pair; 203, attribute; 204, argument_list; 205, identifier:raw_data; 206, call; 207, as_pattern; 208, call; 209, identifier:log; 210, identifier:debug; 211, call; 212, string_content:coverage; 213, attribute; 214, argument_list; 215, string_content:r; 216, string; 217, identifier:format; 218, identifier:uniprot_id; 219, pair; 220, attribute; 221, argument_list; 222, identifier:uniprot_id; 223, dictionary; 224, identifier:log; 225, identifier:debug; 226, call; 227, attribute; 228, argument_list; 229, call; 230, as_pattern_target; 231, attribute; 232, argument_list; 233, attribute; 234, argument_list; 235, identifier:data; 236, identifier:remove; 237, identifier:result; 238, string_content:https://www.ebi.ac.uk/pdbe/api/mappings/best_structures/{}; 239, string; 240, string; 241, string; 242, identifier:format; 243, identifier:uniprot_id; 244, attribute; 245, argument_list; 246, identifier:response; 247, identifier:json; 248, identifier:open; 249, argument_list; 250, identifier:f; 251, identifier:json; 252, identifier:dump; 253, identifier:raw_data; 254, identifier:f; 255, string; 256, identifier:format; 257, identifier:uniprot_id; 258, string_content:key; 259, string_content:value; 260, string_content:{}: 404 returned, probably no structures available.; 261, string; 262, identifier:format; 263, identifier:uniprot_id; 264, identifier:outfile; 265, string; 266, string_content:{}: Saved json file of best structures; 267, string_content:{}: Obtained best structures; 268, string_content:w | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 6, 22; 6, 23; 7, 24; 7, 25; 8, 26; 8, 27; 9, 28; 9, 29; 10, 30; 11, 31; 12, 32; 12, 33; 14, 34; 14, 35; 15, 36; 15, 37; 17, 38; 17, 39; 17, 40; 17, 41; 18, 42; 20, 43; 20, 44; 21, 45; 31, 46; 31, 47; 32, 48; 33, 49; 34, 50; 34, 51; 35, 52; 37, 53; 37, 54; 38, 55; 39, 56; 39, 57; 41, 58; 41, 59; 42, 60; 42, 61; 43, 62; 43, 63; 44, 64; 49, 65; 50, 66; 52, 67; 53, 68; 54, 69; 55, 70; 55, 71; 56, 72; 56, 73; 57, 74; 59, 75; 59, 76; 59, 77; 59, 78; 61, 79; 61, 80; 64, 81; 64, 82; 64, 83; 65, 84; 65, 85; 67, 86; 67, 87; 68, 88; 68, 89; 69, 90; 69, 91; 70, 92; 70, 93; 71, 94; 71, 95; 72, 96; 73, 97; 74, 98; 74, 99; 75, 100; 76, 101; 76, 102; 76, 103; 78, 104; 78, 105; 79, 106; 79, 107; 83, 108; 89, 109; 89, 110; 91, 111; 91, 112; 92, 113; 92, 114; 94, 115; 94, 116; 95, 117; 95, 118; 96, 119; 97, 120; 98, 121; 98, 122; 99, 123; 100, 124; 100, 125; 101, 126; 101, 127; 102, 128; 102, 129; 103, 130; 105, 131; 105, 132; 107, 133; 108, 134; 108, 135; 109, 136; 109, 137; 110, 138; 110, 139; 111, 140; 111, 141; 112, 142; 119, 143; 119, 144; 120, 145; 120, 146; 123, 147; 123, 148; 125, 149; 125, 150; 126, 151; 126, 152; 128, 153; 129, 154; 130, 155; 130, 156; 131, 157; 131, 158; 132, 159; 134, 160; 134, 161; 135, 162; 140, 163; 143, 164; 143, 165; 144, 166; 146, 167; 146, 168; 147, 169; 147, 170; 148, 171; 149, 172; 149, 173; 150, 174; 150, 175; 153, 176; 153, 177; 154, 178; 154, 179; 155, 180; 156, 181; 157, 182; 158, 183; 159, 184; 159, 185; 160, 186; 160, 187; 162, 188; 165, 189; 165, 190; 167, 191; 167, 192; 168, 193; 169, 194; 174, 195; 174, 196; 175, 197; 175, 198; 176, 199; 176, 200; 177, 201; 179, 202; 180, 203; 180, 204; 181, 205; 181, 206; 182, 207; 183, 208; 184, 209; 184, 210; 185, 211; 187, 212; 188, 213; 188, 214; 190, 215; 195, 216; 195, 217; 196, 218; 198, 219; 201, 220; 201, 221; 202, 222; 202, 223; 203, 224; 203, 225; 204, 226; 206, 227; 206, 228; 207, 229; 207, 230; 208, 231; 208, 232; 211, 233; 211, 234; 213, 235; 213, 236; 214, 237; 216, 238; 219, 239; 219, 240; 220, 241; 220, 242; 221, 243; 226, 244; 226, 245; 227, 246; 227, 247; 229, 248; 229, 249; 230, 250; 231, 251; 231, 252; 232, 253; 232, 254; 233, 255; 233, 256; 234, 257; 239, 258; 240, 259; 241, 260; 244, 261; 244, 262; 245, 263; 249, 264; 249, 265; 255, 266; 261, 267; 265, 268 | def best_structures(uniprot_id, outname=None, outdir=None, seq_ident_cutoff=0.0, force_rerun=False):
"""Use the PDBe REST service to query for the best PDB structures for a UniProt ID.
More information found here: https://www.ebi.ac.uk/pdbe/api/doc/sifts.html
Link used to retrieve results: https://www.ebi.ac.uk/pdbe/api/mappings/best_structures/:accession
The list of PDB structures mapping to a UniProt accession sorted by coverage of the protein and, if the same, resolution.
Here is the ranking algorithm described by the PDB paper:
https://nar.oxfordjournals.org/content/44/D1/D385.full
"Finally, a single quality indicator is also calculated for each entry by taking the harmonic average
of all the percentile scores representing model and model-data-fit quality measures and then subtracting
10 times the numerical value of the resolution (in Angstrom) of the entry to ensure that resolution plays
a role in characterising the quality of a structure. This single empirical 'quality measure' value is used
by the PDBe query system to sort results and identify the 'best' structure in a given context. At present,
entries determined by methods other than X-ray crystallography do not have similar data quality information
available and are not considered as 'best structures'."
Args:
uniprot_id (str): UniProt Accession ID
outname (str): Basename of the output file of JSON results
outdir (str): Path to output directory of JSON results
seq_ident_cutoff (float): Cutoff results based on percent coverage (in decimal form)
force_rerun (bool): Obtain best structures mapping ignoring previously downloaded results
Returns:
list: Rank-ordered list of dictionaries representing chain-specific PDB entries. Keys are:
* pdb_id: the PDB ID which maps to the UniProt ID
* chain_id: the specific chain of the PDB which maps to the UniProt ID
* coverage: the percent coverage of the entire UniProt sequence
* resolution: the resolution of the structure
* start: the structure residue number which maps to the start of the mapped sequence
* end: the structure residue number which maps to the end of the mapped sequence
* unp_start: the sequence residue number which maps to the structure start
* unp_end: the sequence residue number which maps to the structure end
* experimental_method: type of experiment used to determine structure
* tax_id: taxonomic ID of the protein's original organism
"""
outfile = ''
if not outdir:
outdir = ''
# if output dir is specified but not outname, use the uniprot
if not outname and outdir:
outname = uniprot_id
if outname:
outname = op.join(outdir, outname)
outfile = '{}.json'.format(outname)
# Load a possibly existing json file
if not ssbio.utils.force_rerun(flag=force_rerun, outfile=outfile):
with open(outfile, 'r') as f:
raw_data = json.load(f)
log.debug('{}: loaded existing json file'.format(uniprot_id))
# Otherwise run the web request
else:
# TODO: add a checker for a cached file of uniprot -> PDBs - can be generated within gempro pipeline and stored
response = requests.get('https://www.ebi.ac.uk/pdbe/api/mappings/best_structures/{}'.format(uniprot_id),
data={'key': 'value'})
if response.status_code == 404:
log.debug('{}: 404 returned, probably no structures available.'.format(uniprot_id))
raw_data = {uniprot_id: {}}
else:
log.debug('{}: Obtained best structures'.format(uniprot_id))
raw_data = response.json()
# Write the json file if specified
if outfile:
with open(outfile, 'w') as f:
json.dump(raw_data, f)
log.debug('{}: Saved json file of best structures'.format(uniprot_id))
data = dict(raw_data)[uniprot_id]
# Filter for sequence identity percentage
if seq_ident_cutoff != 0:
for result in data:
if result['coverage'] < seq_ident_cutoff:
data.remove(result)
return data |
0, module; 1, function_definition; 2, function_name:map_uniprot_to_pdb; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, if_statement; 11, comment:# Check if a UniProt ID is attached to the representative sequence; 12, expression_statement; 13, if_statement; 14, if_statement; 15, if_statement; 16, expression_statement; 17, expression_statement; 18, if_statement; 19, return_statement; 20, identifier:seq_ident_cutoff; 21, float:0.0; 22, identifier:outdir; 23, None; 24, identifier:force_rerun; 25, False; 26, comment:"""Map the representative sequence's UniProt ID to PDB IDs using the PDBe "Best Structures" API.
Will save a JSON file of the results to the protein sequences folder.
The "Best structures" API is available at https://www.ebi.ac.uk/pdbe/api/doc/sifts.html
The list of PDB structures mapping to a UniProt accession sorted by coverage of the protein and,
if the same, resolution.
Args:
seq_ident_cutoff (float): Sequence identity cutoff in decimal form
outdir (str): Output directory to cache JSON results of search
force_rerun (bool): Force re-downloading of JSON results if they already exist
Returns:
list: A rank-ordered list of PDBProp objects that map to the UniProt ID
"""; 27, not_operator; 28, block; 29, assignment; 30, not_operator; 31, block; 32, comparison_operator:'-' in uniprot_id; 33, block; 34, not_operator; 35, block; 36, assignment; 37, assignment; 38, identifier:best_structures; 39, block; 40, else_clause; 41, identifier:new_pdbs; 42, attribute; 43, expression_statement; 44, return_statement; 45, identifier:uniprot_id; 46, attribute; 47, identifier:uniprot_id; 48, expression_statement; 49, return_statement; 50, string; 51, identifier:uniprot_id; 52, expression_statement; 53, expression_statement; 54, identifier:outdir; 55, expression_statement; 56, if_statement; 57, identifier:best_structures; 58, call; 59, identifier:new_pdbs; 60, list; 61, expression_statement; 62, for_statement; 63, expression_statement; 64, block; 65, identifier:self; 66, identifier:representative_sequence; 67, call; 68, None; 69, attribute; 70, identifier:uniprot; 71, call; 72, None; 73, string_content:-; 74, call; 75, assignment; 76, assignment; 77, not_operator; 78, block; 79, attribute; 80, argument_list; 81, assignment; 82, identifier:best_structure; 83, identifier:best_structures; 84, block; 85, call; 86, expression_statement; 87, attribute; 88, argument_list; 89, identifier:self; 90, identifier:representative_sequence; 91, attribute; 92, argument_list; 93, attribute; 94, argument_list; 95, identifier:uniprot_id; 96, subscript; 97, identifier:outdir; 98, attribute; 99, identifier:outdir; 100, raise_statement; 101, attribute; 102, identifier:best_structures; 103, identifier:uniprot_id; 104, keyword_argument; 105, keyword_argument; 106, keyword_argument; 107, keyword_argument; 108, identifier:rank; 109, integer:1; 110, expression_statement; 111, expression_statement; 112, expression_statement; 113, comment:# load_pdb will append this protein to the list; 114, expression_statement; 115, comment:# Also add this chain to the chains attribute so we can save the; 116, comment:# info we get from best_structures; 117, expression_statement; 118, expression_statement; 119, expression_statement; 120, expression_statement; 121, expression_statement; 122, expression_statement; 123, expression_statement; 124, expression_statement; 125, attribute; 126, argument_list; 127, call; 128, identifier:log; 129, identifier:error; 130, call; 131, identifier:log; 132, identifier:error; 133, call; 134, identifier:log; 135, identifier:debug; 136, call; 137, call; 138, integer:0; 139, identifier:self; 140, identifier:sequence_dir; 141, call; 142, attribute; 143, identifier:pdb; 144, identifier:outname; 145, call; 146, identifier:outdir; 147, identifier:outdir; 148, identifier:seq_ident_cutoff; 149, identifier:seq_ident_cutoff; 150, identifier:force_rerun; 151, identifier:force_rerun; 152, assignment; 153, call; 154, assignment; 155, assignment; 156, call; 157, assignment; 158, assignment; 159, call; 160, assignment; 161, call; 162, call; 163, augmented_assignment; 164, identifier:log; 165, identifier:debug; 166, call; 167, attribute; 168, argument_list; 169, attribute; 170, argument_list; 171, attribute; 172, argument_list; 173, attribute; 174, argument_list; 175, attribute; 176, argument_list; 177, identifier:ValueError; 178, argument_list; 179, identifier:ssbio; 180, identifier:databases; 181, attribute; 182, argument_list; 183, identifier:currpdb; 184, call; 185, attribute; 186, argument_list; 187, identifier:currchain; 188, call; 189, identifier:new_pdb; 190, call; 191, attribute; 192, argument_list; 193, identifier:pdb_specific_keys; 194, list; 195, identifier:chain_specific_keys; 196, list; 197, attribute; 198, argument_list; 199, identifier:new_chain; 200, call; 201, attribute; 202, argument_list; 203, attribute; 204, argument_list; 205, identifier:rank; 206, integer:1; 207, attribute; 208, argument_list; 209, identifier:log; 210, identifier:debug; 211, call; 212, string; 213, identifier:format; 214, attribute; 215, string; 216, identifier:format; 217, attribute; 218, string:'{}: "-" detected in UniProt ID, isoform specific sequences are ignored with best structures API'; 219, identifier:format; 220, attribute; 221, identifier:uniprot_id; 222, identifier:split; 223, string; 224, string; 225, string; 226, identifier:format; 227, call; 228, identifier:str; 229, argument_list; 230, identifier:new_pdbs; 231, identifier:append; 232, identifier:currpdb; 233, identifier:str; 234, argument_list; 235, attribute; 236, argument_list; 237, identifier:new_pdb; 238, identifier:add_chain_ids; 239, identifier:currchain; 240, string; 241, string; 242, string; 243, string; 244, string; 245, string; 246, string; 247, identifier:new_pdb; 248, identifier:update; 249, identifier:best_structure; 250, keyword_argument; 251, attribute; 252, argument_list; 253, identifier:new_chain; 254, identifier:update; 255, identifier:best_structure; 256, keyword_argument; 257, identifier:new_chain; 258, identifier:update; 259, dictionary; 260, string; 261, identifier:format; 262, attribute; 263, identifier:uniprot_id; 264, call; 265, attribute; 266, argument_list; 267, string_content:{}: no representative sequence set, cannot use best structures API; 268, identifier:self; 269, identifier:id; 270, string_content:{}: no representative UniProt ID set, cannot use best structures API; 271, identifier:self; 272, identifier:id; 273, identifier:self; 274, identifier:id; 275, string_content:-; 276, string_content:Output directory must be specified; 277, string_content:{}_best_structures; 278, identifier:custom_slugify; 279, argument_list; 280, call; 281, subscript; 282, identifier:self; 283, identifier:load_pdb; 284, keyword_argument; 285, keyword_argument; 286, string_content:experimental_method; 287, string_content:resolution; 288, string_content:coverage; 289, string_content:start; 290, string_content:end; 291, string_content:unp_start; 292, string_content:unp_end; 293, identifier:only_keys; 294, identifier:pdb_specific_keys; 295, attribute; 296, identifier:get_by_id; 297, identifier:currchain; 298, identifier:only_keys; 299, identifier:chain_specific_keys; 300, pair; 301, string_content:{}, {}: {} PDB/chain pairs mapped; 302, identifier:self; 303, identifier:id; 304, identifier:len; 305, argument_list; 306, string; 307, identifier:format; 308, attribute; 309, identifier:uniprot_id; 310, identifier:uniprot_id; 311, attribute; 312, argument_list; 313, identifier:best_structure; 314, string; 315, identifier:pdb_id; 316, identifier:currpdb; 317, identifier:mapped_chains; 318, identifier:currchain; 319, identifier:new_pdb; 320, identifier:chains; 321, string; 322, identifier:rank; 323, identifier:best_structures; 324, string_content:{}, {}: no PDB/chain pairs mapped; 325, identifier:self; 326, identifier:id; 327, subscript; 328, identifier:lower; 329, string_content:chain_id; 330, string_content:rank; 331, identifier:best_structure; 332, string; 333, string_content:pdb_id | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 6, 20; 6, 21; 7, 22; 7, 23; 8, 24; 8, 25; 9, 26; 10, 27; 10, 28; 12, 29; 13, 30; 13, 31; 14, 32; 14, 33; 15, 34; 15, 35; 16, 36; 17, 37; 18, 38; 18, 39; 18, 40; 19, 41; 27, 42; 28, 43; 28, 44; 29, 45; 29, 46; 30, 47; 31, 48; 31, 49; 32, 50; 32, 51; 33, 52; 33, 53; 34, 54; 35, 55; 35, 56; 36, 57; 36, 58; 37, 59; 37, 60; 39, 61; 39, 62; 39, 63; 40, 64; 42, 65; 42, 66; 43, 67; 44, 68; 46, 69; 46, 70; 48, 71; 49, 72; 50, 73; 52, 74; 53, 75; 55, 76; 56, 77; 56, 78; 58, 79; 58, 80; 61, 81; 62, 82; 62, 83; 62, 84; 63, 85; 64, 86; 67, 87; 67, 88; 69, 89; 69, 90; 71, 91; 71, 92; 74, 93; 74, 94; 75, 95; 75, 96; 76, 97; 76, 98; 77, 99; 78, 100; 79, 101; 79, 102; 80, 103; 80, 104; 80, 105; 80, 106; 80, 107; 81, 108; 81, 109; 84, 110; 84, 111; 84, 112; 84, 113; 84, 114; 84, 115; 84, 116; 84, 117; 84, 118; 84, 119; 84, 120; 84, 121; 84, 122; 84, 123; 84, 124; 85, 125; 85, 126; 86, 127; 87, 128; 87, 129; 88, 130; 91, 131; 91, 132; 92, 133; 93, 134; 93, 135; 94, 136; 96, 137; 96, 138; 98, 139; 98, 140; 100, 141; 101, 142; 101, 143; 104, 144; 104, 145; 105, 146; 105, 147; 106, 148; 106, 149; 107, 150; 107, 151; 110, 152; 111, 153; 112, 154; 114, 155; 117, 156; 118, 157; 119, 158; 120, 159; 121, 160; 122, 161; 123, 162; 124, 163; 125, 164; 125, 165; 126, 166; 127, 167; 127, 168; 130, 169; 130, 170; 133, 171; 133, 172; 136, 173; 136, 174; 137, 175; 137, 176; 141, 177; 141, 178; 142, 179; 142, 180; 145, 181; 145, 182; 152, 183; 152, 184; 153, 185; 153, 186; 154, 187; 154, 188; 155, 189; 155, 190; 156, 191; 156, 192; 157, 193; 157, 194; 158, 195; 158, 196; 159, 197; 159, 198; 160, 199; 160, 200; 161, 201; 161, 202; 162, 203; 162, 204; 163, 205; 163, 206; 166, 207; 166, 208; 167, 209; 167, 210; 168, 211; 169, 212; 169, 213; 170, 214; 171, 215; 171, 216; 172, 217; 173, 218; 173, 219; 174, 220; 175, 221; 175, 222; 176, 223; 178, 224; 181, 225; 181, 226; 182, 227; 184, 228; 184, 229; 185, 230; 185, 231; 186, 232; 188, 233; 188, 234; 190, 235; 190, 236; 191, 237; 191, 238; 192, 239; 194, 240; 194, 241; 196, 242; 196, 243; 196, 244; 196, 245; 196, 246; 197, 247; 197, 248; 198, 249; 198, 250; 200, 251; 200, 252; 201, 253; 201, 254; 202, 255; 202, 256; 203, 257; 203, 258; 204, 259; 207, 260; 207, 261; 208, 262; 208, 263; 208, 264; 211, 265; 211, 266; 212, 267; 214, 268; 214, 269; 215, 270; 217, 271; 217, 272; 220, 273; 220, 274; 223, 275; 224, 276; 225, 277; 227, 278; 227, 279; 229, 280; 234, 281; 235, 282; 235, 283; 236, 284; 236, 285; 240, 286; 241, 287; 242, 288; 243, 289; 244, 290; 245, 291; 246, 292; 250, 293; 250, 294; 251, 295; 251, 296; 252, 297; 256, 298; 256, 299; 259, 300; 260, 301; 262, 302; 262, 303; 264, 304; 264, 305; 265, 306; 265, 307; 266, 308; 266, 309; 279, 310; 280, 311; 280, 312; 281, 313; 281, 314; 284, 315; 284, 316; 285, 317; 285, 318; 295, 319; 295, 320; 300, 321; 300, 322; 305, 323; 306, 324; 308, 325; 308, 326; 311, 327; 311, 328; 314, 329; 321, 330; 327, 331; 327, 332; 332, 333 | def map_uniprot_to_pdb(self, seq_ident_cutoff=0.0, outdir=None, force_rerun=False):
"""Map the representative sequence's UniProt ID to PDB IDs using the PDBe "Best Structures" API.
Will save a JSON file of the results to the protein sequences folder.
The "Best structures" API is available at https://www.ebi.ac.uk/pdbe/api/doc/sifts.html
The list of PDB structures mapping to a UniProt accession sorted by coverage of the protein and,
if the same, resolution.
Args:
seq_ident_cutoff (float): Sequence identity cutoff in decimal form
outdir (str): Output directory to cache JSON results of search
force_rerun (bool): Force re-downloading of JSON results if they already exist
Returns:
list: A rank-ordered list of PDBProp objects that map to the UniProt ID
"""
if not self.representative_sequence:
log.error('{}: no representative sequence set, cannot use best structures API'.format(self.id))
return None
# Check if a UniProt ID is attached to the representative sequence
uniprot_id = self.representative_sequence.uniprot
if not uniprot_id:
log.error('{}: no representative UniProt ID set, cannot use best structures API'.format(self.id))
return None
if '-' in uniprot_id:
log.debug('{}: "-" detected in UniProt ID, isoform specific sequences are ignored with best structures API'.format(self.id))
uniprot_id = uniprot_id.split('-')[0]
if not outdir:
outdir = self.sequence_dir
if not outdir:
raise ValueError('Output directory must be specified')
best_structures = ssbio.databases.pdb.best_structures(uniprot_id,
outname='{}_best_structures'.format(custom_slugify(uniprot_id)),
outdir=outdir,
seq_ident_cutoff=seq_ident_cutoff,
force_rerun=force_rerun)
new_pdbs = []
if best_structures:
rank = 1
for best_structure in best_structures:
currpdb = str(best_structure['pdb_id'].lower())
new_pdbs.append(currpdb)
currchain = str(best_structure['chain_id'])
# load_pdb will append this protein to the list
new_pdb = self.load_pdb(pdb_id=currpdb, mapped_chains=currchain)
# Also add this chain to the chains attribute so we can save the
# info we get from best_structures
new_pdb.add_chain_ids(currchain)
pdb_specific_keys = ['experimental_method', 'resolution']
chain_specific_keys = ['coverage', 'start', 'end', 'unp_start', 'unp_end']
new_pdb.update(best_structure, only_keys=pdb_specific_keys)
new_chain = new_pdb.chains.get_by_id(currchain)
new_chain.update(best_structure, only_keys=chain_specific_keys)
new_chain.update({'rank': rank})
rank += 1
log.debug('{}, {}: {} PDB/chain pairs mapped'.format(self.id, uniprot_id, len(best_structures)))
else:
log.debug('{}, {}: no PDB/chain pairs mapped'.format(self.id, uniprot_id))
return new_pdbs |
0, module; 1, function_definition; 2, function_name:map_uniprot_to_pdb; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, comment:# First get all UniProt IDs and check if they have PDBs; 11, expression_statement; 12, for_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, comment:# Now run the best_structures API for all genes; 17, for_statement; 18, expression_statement; 19, expression_statement; 20, identifier:seq_ident_cutoff; 21, float:0.0; 22, identifier:outdir; 23, None; 24, identifier:force_rerun; 25, False; 26, comment:"""Map all representative sequences' UniProt ID to PDB IDs using the PDBe "Best Structures" API.
Will save a JSON file of the results to each protein's ``sequences`` folder.
The "Best structures" API is available at https://www.ebi.ac.uk/pdbe/api/doc/sifts.html
The list of PDB structures mapping to a UniProt accession sorted by coverage of the protein and,
if the same, resolution.
Args:
seq_ident_cutoff (float): Sequence identity cutoff in decimal form
outdir (str): Output directory to cache JSON results of search
force_rerun (bool): Force re-downloading of JSON results if they already exist
Returns:
list: A rank-ordered list of PDBProp objects that map to the UniProt ID
"""; 27, assignment; 28, identifier:g; 29, attribute; 30, block; 31, call; 32, assignment; 33, assignment; 34, identifier:g; 35, call; 36, block; 37, call; 38, call; 39, identifier:all_representative_uniprots; 40, list; 41, identifier:self; 42, identifier:genes_with_a_representative_sequence; 43, expression_statement; 44, if_statement; 45, attribute; 46, argument_list; 47, identifier:uniprots_to_pdbs; 48, call; 49, identifier:counter; 50, integer:0; 51, identifier:tqdm; 52, argument_list; 53, expression_statement; 54, if_statement; 55, attribute; 56, argument_list; 57, attribute; 58, argument_list; 59, assignment; 60, identifier:uniprot_id; 61, comment:# TODO: add warning or something for isoform ids?; 62, block; 63, identifier:log; 64, identifier:info; 65, string; 66, attribute; 67, argument_list; 68, attribute; 69, assignment; 70, identifier:uniprot_id; 71, block; 72, identifier:log; 73, identifier:info; 74, call; 75, identifier:log; 76, identifier:info; 77, string:'Completed UniProt --> best PDB mapping. See the "df_pdb_ranking" attribute for a summary dataframe.'; 78, identifier:uniprot_id; 79, attribute; 80, if_statement; 81, expression_statement; 82, string_content:Mapping UniProt IDs --> PDB IDs...; 83, identifier:bs_unip; 84, identifier:mapping; 85, keyword_argument; 86, keyword_argument; 87, keyword_argument; 88, identifier:self; 89, identifier:genes_with_a_representative_sequence; 90, identifier:uniprot_id; 91, attribute; 92, if_statement; 93, if_statement; 94, attribute; 95, argument_list; 96, attribute; 97, identifier:uniprot; 98, comparison_operator:'-' in uniprot_id; 99, block; 100, call; 101, identifier:fr; 102, string; 103, identifier:to; 104, string; 105, identifier:query; 106, identifier:all_representative_uniprots; 107, attribute; 108, identifier:uniprot; 109, comparison_operator:'-' in uniprot_id; 110, block; 111, comparison_operator:uniprot_id in uniprots_to_pdbs; 112, block; 113, else_clause; 114, string; 115, identifier:format; 116, call; 117, call; 118, attribute; 119, identifier:representative_sequence; 120, string; 121, identifier:uniprot_id; 122, expression_statement; 123, attribute; 124, argument_list; 125, string_content:ACC; 126, string_content:PDB_ID; 127, attribute; 128, identifier:representative_sequence; 129, string; 130, identifier:uniprot_id; 131, expression_statement; 132, identifier:uniprot_id; 133, identifier:uniprots_to_pdbs; 134, expression_statement; 135, if_statement; 136, block; 137, string_content:{}/{}: number of genes with at least one experimental structure; 138, identifier:len; 139, argument_list; 140, identifier:len; 141, argument_list; 142, identifier:g; 143, identifier:protein; 144, string_content:-; 145, assignment; 146, identifier:all_representative_uniprots; 147, identifier:append; 148, identifier:uniprot_id; 149, identifier:g; 150, identifier:protein; 151, string_content:-; 152, assignment; 153, assignment; 154, identifier:best_structures; 155, block; 156, expression_statement; 157, attribute; 158, attribute; 159, identifier:uniprot_id; 160, subscript; 161, identifier:uniprot_id; 162, subscript; 163, identifier:best_structures; 164, call; 165, expression_statement; 166, expression_statement; 167, call; 168, identifier:self; 169, identifier:genes_with_experimental_structures; 170, identifier:self; 171, identifier:genes; 172, call; 173, integer:0; 174, call; 175, integer:0; 176, attribute; 177, argument_list; 178, augmented_assignment; 179, call; 180, attribute; 181, argument_list; 182, attribute; 183, argument_list; 184, attribute; 185, argument_list; 186, attribute; 187, identifier:map_uniprot_to_pdb; 188, keyword_argument; 189, keyword_argument; 190, keyword_argument; 191, identifier:counter; 192, integer:1; 193, attribute; 194, argument_list; 195, identifier:log; 196, identifier:debug; 197, call; 198, identifier:uniprot_id; 199, identifier:split; 200, string; 201, identifier:uniprot_id; 202, identifier:split; 203, string; 204, identifier:g; 205, identifier:protein; 206, identifier:seq_ident_cutoff; 207, identifier:seq_ident_cutoff; 208, identifier:outdir; 209, identifier:outdir; 210, identifier:force_rerun; 211, identifier:force_rerun; 212, identifier:log; 213, identifier:debug; 214, call; 215, attribute; 216, argument_list; 217, string_content:-; 218, string_content:-; 219, attribute; 220, argument_list; 221, string; 222, identifier:format; 223, attribute; 224, identifier:uniprot_id; 225, string; 226, identifier:format; 227, attribute; 228, call; 229, string_content:{}, {}: no PDBs available; 230, identifier:g; 231, identifier:id; 232, string_content:{}: {} PDBs mapped; 233, identifier:g; 234, identifier:id; 235, identifier:len; 236, argument_list; 237, identifier:best_structures | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 6, 20; 6, 21; 7, 22; 7, 23; 8, 24; 8, 25; 9, 26; 11, 27; 12, 28; 12, 29; 12, 30; 13, 31; 14, 32; 15, 33; 17, 34; 17, 35; 17, 36; 18, 37; 19, 38; 27, 39; 27, 40; 29, 41; 29, 42; 30, 43; 30, 44; 31, 45; 31, 46; 32, 47; 32, 48; 33, 49; 33, 50; 35, 51; 35, 52; 36, 53; 36, 54; 37, 55; 37, 56; 38, 57; 38, 58; 43, 59; 44, 60; 44, 61; 44, 62; 45, 63; 45, 64; 46, 65; 48, 66; 48, 67; 52, 68; 53, 69; 54, 70; 54, 71; 55, 72; 55, 73; 56, 74; 57, 75; 57, 76; 58, 77; 59, 78; 59, 79; 62, 80; 62, 81; 65, 82; 66, 83; 66, 84; 67, 85; 67, 86; 67, 87; 68, 88; 68, 89; 69, 90; 69, 91; 71, 92; 71, 93; 74, 94; 74, 95; 79, 96; 79, 97; 80, 98; 80, 99; 81, 100; 85, 101; 85, 102; 86, 103; 86, 104; 87, 105; 87, 106; 91, 107; 91, 108; 92, 109; 92, 110; 93, 111; 93, 112; 93, 113; 94, 114; 94, 115; 95, 116; 95, 117; 96, 118; 96, 119; 98, 120; 98, 121; 99, 122; 100, 123; 100, 124; 102, 125; 104, 126; 107, 127; 107, 128; 109, 129; 109, 130; 110, 131; 111, 132; 111, 133; 112, 134; 112, 135; 113, 136; 114, 137; 116, 138; 116, 139; 117, 140; 117, 141; 118, 142; 118, 143; 120, 144; 122, 145; 123, 146; 123, 147; 124, 148; 127, 149; 127, 150; 129, 151; 131, 152; 134, 153; 135, 154; 135, 155; 136, 156; 139, 157; 141, 158; 145, 159; 145, 160; 152, 161; 152, 162; 153, 163; 153, 164; 155, 165; 155, 166; 156, 167; 157, 168; 157, 169; 158, 170; 158, 171; 160, 172; 160, 173; 162, 174; 162, 175; 164, 176; 164, 177; 165, 178; 166, 179; 167, 180; 167, 181; 172, 182; 172, 183; 174, 184; 174, 185; 176, 186; 176, 187; 177, 188; 177, 189; 177, 190; 178, 191; 178, 192; 179, 193; 179, 194; 180, 195; 180, 196; 181, 197; 182, 198; 182, 199; 183, 200; 184, 201; 184, 202; 185, 203; 186, 204; 186, 205; 188, 206; 188, 207; 189, 208; 189, 209; 190, 210; 190, 211; 193, 212; 193, 213; 194, 214; 197, 215; 197, 216; 200, 217; 203, 218; 214, 219; 214, 220; 215, 221; 215, 222; 216, 223; 216, 224; 219, 225; 219, 226; 220, 227; 220, 228; 221, 229; 223, 230; 223, 231; 225, 232; 227, 233; 227, 234; 228, 235; 228, 236; 236, 237 | def map_uniprot_to_pdb(self, seq_ident_cutoff=0.0, outdir=None, force_rerun=False):
"""Map all representative sequences' UniProt ID to PDB IDs using the PDBe "Best Structures" API.
Will save a JSON file of the results to each protein's ``sequences`` folder.
The "Best structures" API is available at https://www.ebi.ac.uk/pdbe/api/doc/sifts.html
The list of PDB structures mapping to a UniProt accession sorted by coverage of the protein and,
if the same, resolution.
Args:
seq_ident_cutoff (float): Sequence identity cutoff in decimal form
outdir (str): Output directory to cache JSON results of search
force_rerun (bool): Force re-downloading of JSON results if they already exist
Returns:
list: A rank-ordered list of PDBProp objects that map to the UniProt ID
"""
# First get all UniProt IDs and check if they have PDBs
all_representative_uniprots = []
for g in self.genes_with_a_representative_sequence:
uniprot_id = g.protein.representative_sequence.uniprot
if uniprot_id:
# TODO: add warning or something for isoform ids?
if '-' in uniprot_id:
uniprot_id = uniprot_id.split('-')[0]
all_representative_uniprots.append(uniprot_id)
log.info('Mapping UniProt IDs --> PDB IDs...')
uniprots_to_pdbs = bs_unip.mapping(fr='ACC', to='PDB_ID', query=all_representative_uniprots)
counter = 0
# Now run the best_structures API for all genes
for g in tqdm(self.genes_with_a_representative_sequence):
uniprot_id = g.protein.representative_sequence.uniprot
if uniprot_id:
if '-' in uniprot_id:
uniprot_id = uniprot_id.split('-')[0]
if uniprot_id in uniprots_to_pdbs:
best_structures = g.protein.map_uniprot_to_pdb(seq_ident_cutoff=seq_ident_cutoff, outdir=outdir, force_rerun=force_rerun)
if best_structures:
counter += 1
log.debug('{}: {} PDBs mapped'.format(g.id, len(best_structures)))
else:
log.debug('{}, {}: no PDBs available'.format(g.id, uniprot_id))
log.info('{}/{}: number of genes with at least one experimental structure'.format(len(self.genes_with_experimental_structures),
len(self.genes)))
log.info('Completed UniProt --> best PDB mapping. See the "df_pdb_ranking" attribute for a summary dataframe.') |
0, module; 1, function_definition; 2, function_name:get_properties_by_type; 3, parameters; 4, block; 5, identifier:self; 6, identifier:type; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, if_statement; 11, if_statement; 12, expression_statement; 13, for_statement; 14, return_statement; 15, identifier:recursive; 16, True; 17, identifier:parent_path; 18, string:""; 19, comment:"""
Returns a sorted list of fields that match the type.
:param type the type of the field "string","integer" or a list of types
:param recursive recurse to sub object
:returns a sorted list of fields the match the type
"""; 20, identifier:parent_path; 21, block; 22, call; 23, block; 24, assignment; 25, identifier:prop; 26, call; 27, block; 28, call; 29, expression_statement; 30, identifier:isinstance; 31, argument_list; 32, if_statement; 33, identifier:properties; 34, list; 35, identifier:list; 36, argument_list; 37, if_statement; 38, if_statement; 39, if_statement; 40, identifier:sorted; 41, argument_list; 42, augmented_assignment; 43, identifier:type; 44, identifier:str; 45, comparison_operator:type == "*"; 46, block; 47, else_clause; 48, call; 49, comparison_operator:prop.type in type; 50, block; 51, elif_clause; 52, not_operator; 53, block; 54, comparison_operator:prop.type in ["nested", "object"]; 55, block; 56, identifier:properties; 57, identifier:parent_path; 58, string:"."; 59, identifier:type; 60, string:"*"; 61, expression_statement; 62, block; 63, attribute; 64, argument_list; 65, attribute; 66, identifier:type; 67, expression_statement; 68, continue_statement; 69, boolean_operator; 70, block; 71, identifier:recursive; 72, continue_statement; 73, attribute; 74, list; 75, expression_statement; 76, assignment; 77, expression_statement; 78, attribute; 79, identifier:values; 80, identifier:prop; 81, identifier:type; 82, call; 83, boolean_operator; 84, comparison_operator:prop.fields[prop.name].type in type; 85, expression_statement; 86, continue_statement; 87, identifier:prop; 88, identifier:type; 89, string:"nested"; 90, string:"object"; 91, call; 92, identifier:type; 93, binary_operator:set(MAPPING_NAME_TYPE.keys()) - set(["nested", "multi_field", "multifield"]); 94, assignment; 95, identifier:self; 96, identifier:properties; 97, attribute; 98, argument_list; 99, comparison_operator:prop.type == "multi_field"; 100, comparison_operator:prop.name in prop.fields; 101, attribute; 102, identifier:type; 103, call; 104, attribute; 105, argument_list; 106, call; 107, call; 108, identifier:type; 109, list; 110, identifier:properties; 111, identifier:append; 112, tuple; 113, attribute; 114, string:"multi_field"; 115, attribute; 116, attribute; 117, subscript; 118, identifier:type; 119, attribute; 120, argument_list; 121, identifier:properties; 122, identifier:extend; 123, call; 124, identifier:set; 125, argument_list; 126, identifier:set; 127, argument_list; 128, identifier:type; 129, binary_operator:parent_path + prop.name; 130, identifier:prop; 131, identifier:prop; 132, identifier:type; 133, identifier:prop; 134, identifier:name; 135, identifier:prop; 136, identifier:fields; 137, attribute; 138, attribute; 139, identifier:properties; 140, identifier:append; 141, tuple; 142, attribute; 143, argument_list; 144, call; 145, list; 146, identifier:parent_path; 147, attribute; 148, identifier:prop; 149, identifier:fields; 150, identifier:prop; 151, identifier:name; 152, binary_operator:parent_path + prop.name; 153, identifier:prop; 154, identifier:prop; 155, identifier:get_properties_by_type; 156, identifier:type; 157, keyword_argument; 158, keyword_argument; 159, attribute; 160, argument_list; 161, string:"nested"; 162, string:"multi_field"; 163, string:"multifield"; 164, identifier:prop; 165, identifier:name; 166, identifier:parent_path; 167, attribute; 168, identifier:recursive; 169, identifier:recursive; 170, identifier:parent_path; 171, binary_operator:parent_path + prop.name; 172, identifier:MAPPING_NAME_TYPE; 173, identifier:keys; 174, identifier:prop; 175, identifier:name; 176, identifier:parent_path; 177, attribute; 178, identifier:prop; 179, identifier:name | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 7, 15; 7, 16; 8, 17; 8, 18; 9, 19; 10, 20; 10, 21; 11, 22; 11, 23; 12, 24; 13, 25; 13, 26; 13, 27; 14, 28; 21, 29; 22, 30; 22, 31; 23, 32; 24, 33; 24, 34; 26, 35; 26, 36; 27, 37; 27, 38; 27, 39; 28, 40; 28, 41; 29, 42; 31, 43; 31, 44; 32, 45; 32, 46; 32, 47; 36, 48; 37, 49; 37, 50; 37, 51; 38, 52; 38, 53; 39, 54; 39, 55; 41, 56; 42, 57; 42, 58; 45, 59; 45, 60; 46, 61; 47, 62; 48, 63; 48, 64; 49, 65; 49, 66; 50, 67; 50, 68; 51, 69; 51, 70; 52, 71; 53, 72; 54, 73; 54, 74; 55, 75; 61, 76; 62, 77; 63, 78; 63, 79; 65, 80; 65, 81; 67, 82; 69, 83; 69, 84; 70, 85; 70, 86; 73, 87; 73, 88; 74, 89; 74, 90; 75, 91; 76, 92; 76, 93; 77, 94; 78, 95; 78, 96; 82, 97; 82, 98; 83, 99; 83, 100; 84, 101; 84, 102; 85, 103; 91, 104; 91, 105; 93, 106; 93, 107; 94, 108; 94, 109; 97, 110; 97, 111; 98, 112; 99, 113; 99, 114; 100, 115; 100, 116; 101, 117; 101, 118; 103, 119; 103, 120; 104, 121; 104, 122; 105, 123; 106, 124; 106, 125; 107, 126; 107, 127; 109, 128; 112, 129; 112, 130; 113, 131; 113, 132; 115, 133; 115, 134; 116, 135; 116, 136; 117, 137; 117, 138; 119, 139; 119, 140; 120, 141; 123, 142; 123, 143; 125, 144; 127, 145; 129, 146; 129, 147; 137, 148; 137, 149; 138, 150; 138, 151; 141, 152; 141, 153; 142, 154; 142, 155; 143, 156; 143, 157; 143, 158; 144, 159; 144, 160; 145, 161; 145, 162; 145, 163; 147, 164; 147, 165; 152, 166; 152, 167; 157, 168; 157, 169; 158, 170; 158, 171; 159, 172; 159, 173; 167, 174; 167, 175; 171, 176; 171, 177; 177, 178; 177, 179 | def get_properties_by_type(self, type, recursive=True, parent_path=""):
"""
Returns a sorted list of fields that match the type.
:param type the type of the field "string","integer" or a list of types
:param recursive recurse to sub object
:returns a sorted list of fields the match the type
"""
if parent_path:
parent_path += "."
if isinstance(type, str):
if type == "*":
type = set(MAPPING_NAME_TYPE.keys()) - set(["nested", "multi_field", "multifield"])
else:
type = [type]
properties = []
for prop in list(self.properties.values()):
if prop.type in type:
properties.append((parent_path + prop.name, prop))
continue
elif prop.type == "multi_field" and prop.name in prop.fields and prop.fields[prop.name].type in type:
properties.append((parent_path + prop.name, prop))
continue
if not recursive:
continue
if prop.type in ["nested", "object"]:
properties.extend(
prop.get_properties_by_type(type, recursive=recursive, parent_path=parent_path + prop.name))
return sorted(properties) |
0, module; 1, function_definition; 2, function_name:insert; 3, parameters; 4, block; 5, identifier:self; 6, identifier:index; 7, identifier:key; 8, identifier:value; 9, expression_statement; 10, if_statement; 11, expression_statement; 12, expression_statement; 13, comment:"""Inserts the key, value pair before the item with the given index."""; 14, comparison_operator:key in self.keyOrder; 15, block; 16, call; 17, call; 18, identifier:key; 19, attribute; 20, expression_statement; 21, delete_statement; 22, if_statement; 23, attribute; 24, argument_list; 25, attribute; 26, argument_list; 27, identifier:self; 28, identifier:keyOrder; 29, assignment; 30, subscript; 31, comparison_operator:n < index; 32, block; 33, attribute; 34, identifier:insert; 35, identifier:index; 36, identifier:key; 37, call; 38, identifier:__setitem__; 39, identifier:key; 40, identifier:value; 41, identifier:n; 42, call; 43, attribute; 44, identifier:n; 45, identifier:n; 46, identifier:index; 47, expression_statement; 48, identifier:self; 49, identifier:keyOrder; 50, identifier:super; 51, argument_list; 52, attribute; 53, argument_list; 54, identifier:self; 55, identifier:keyOrder; 56, augmented_assignment; 57, identifier:SortedDict; 58, identifier:self; 59, attribute; 60, identifier:index; 61, identifier:key; 62, identifier:index; 63, integer:1; 64, identifier:self; 65, identifier:keyOrder | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 9, 13; 10, 14; 10, 15; 11, 16; 12, 17; 14, 18; 14, 19; 15, 20; 15, 21; 15, 22; 16, 23; 16, 24; 17, 25; 17, 26; 19, 27; 19, 28; 20, 29; 21, 30; 22, 31; 22, 32; 23, 33; 23, 34; 24, 35; 24, 36; 25, 37; 25, 38; 26, 39; 26, 40; 29, 41; 29, 42; 30, 43; 30, 44; 31, 45; 31, 46; 32, 47; 33, 48; 33, 49; 37, 50; 37, 51; 42, 52; 42, 53; 43, 54; 43, 55; 47, 56; 51, 57; 51, 58; 52, 59; 52, 60; 53, 61; 56, 62; 56, 63; 59, 64; 59, 65 | def insert(self, index, key, value):
"""Inserts the key, value pair before the item with the given index."""
if key in self.keyOrder:
n = self.keyOrder.index(key)
del self.keyOrder[n]
if n < index:
index -= 1
self.keyOrder.insert(index, key)
super(SortedDict, self).__setitem__(key, value) |
0, module; 1, function_definition; 2, function_name:sort_qualified_edges; 3, parameters; 4, type; 5, block; 6, identifier:graph; 7, generic_type; 8, expression_statement; 9, expression_statement; 10, return_statement; 11, identifier:Iterable; 12, type_parameter; 13, comment:"""Return the qualified edges, sorted first by citation, then by evidence, then by annotations.
:param BELGraph graph: A BEL graph
"""; 14, assignment; 15, call; 16, type; 17, identifier:qualified_edges; 18, generator_expression; 19, identifier:sorted; 20, argument_list; 21, identifier:EdgeTuple; 22, tuple; 23, for_in_clause; 24, if_clause; 25, identifier:qualified_edges; 26, keyword_argument; 27, identifier:u; 28, identifier:v; 29, identifier:k; 30, identifier:d; 31, pattern_list; 32, call; 33, boolean_operator; 34, identifier:key; 35, identifier:_sort_qualified_edges_helper; 36, identifier:u; 37, identifier:v; 38, identifier:k; 39, identifier:d; 40, attribute; 41, argument_list; 42, call; 43, call; 44, identifier:graph; 45, identifier:edges; 46, keyword_argument; 47, keyword_argument; 48, attribute; 49, argument_list; 50, attribute; 51, argument_list; 52, identifier:keys; 53, True; 54, identifier:data; 55, True; 56, identifier:graph; 57, identifier:has_edge_citation; 58, identifier:u; 59, identifier:v; 60, identifier:k; 61, identifier:graph; 62, identifier:has_edge_evidence; 63, identifier:u; 64, identifier:v; 65, identifier:k | 0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 4, 7; 5, 8; 5, 9; 5, 10; 7, 11; 7, 12; 8, 13; 9, 14; 10, 15; 12, 16; 14, 17; 14, 18; 15, 19; 15, 20; 16, 21; 18, 22; 18, 23; 18, 24; 20, 25; 20, 26; 22, 27; 22, 28; 22, 29; 22, 30; 23, 31; 23, 32; 24, 33; 26, 34; 26, 35; 31, 36; 31, 37; 31, 38; 31, 39; 32, 40; 32, 41; 33, 42; 33, 43; 40, 44; 40, 45; 41, 46; 41, 47; 42, 48; 42, 49; 43, 50; 43, 51; 46, 52; 46, 53; 47, 54; 47, 55; 48, 56; 48, 57; 49, 58; 49, 59; 49, 60; 50, 61; 50, 62; 51, 63; 51, 64; 51, 65 | def sort_qualified_edges(graph) -> Iterable[EdgeTuple]:
"""Return the qualified edges, sorted first by citation, then by evidence, then by annotations.
:param BELGraph graph: A BEL graph
"""
qualified_edges = (
(u, v, k, d)
for u, v, k, d in graph.edges(keys=True, data=True)
if graph.has_edge_citation(u, v, k) and graph.has_edge_evidence(u, v, k)
)
return sorted(qualified_edges, key=_sort_qualified_edges_helper) |
0, module; 1, function_definition; 2, function_name:_citation_sort_key; 3, parameters; 4, type; 5, block; 6, typed_parameter; 7, identifier:str; 8, expression_statement; 9, return_statement; 10, identifier:t; 11, type; 12, comment:"""Make a confusing 4 tuple sortable by citation."""; 13, call; 14, identifier:EdgeTuple; 15, attribute; 16, argument_list; 17, string:'"{}", "{}"'; 18, identifier:format; 19, subscript; 20, subscript; 21, subscript; 22, identifier:CITATION_TYPE; 23, subscript; 24, identifier:CITATION_REFERENCE; 25, subscript; 26, identifier:CITATION; 27, subscript; 28, identifier:CITATION; 29, identifier:t; 30, integer:3; 31, identifier:t; 32, integer:3 | 0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 4, 7; 5, 8; 5, 9; 6, 10; 6, 11; 8, 12; 9, 13; 11, 14; 13, 15; 13, 16; 15, 17; 15, 18; 16, 19; 16, 20; 19, 21; 19, 22; 20, 23; 20, 24; 21, 25; 21, 26; 23, 27; 23, 28; 25, 29; 25, 30; 27, 31; 27, 32 | def _citation_sort_key(t: EdgeTuple) -> str:
"""Make a confusing 4 tuple sortable by citation."""
return '"{}", "{}"'.format(t[3][CITATION][CITATION_TYPE], t[3][CITATION][CITATION_REFERENCE]) |
0, module; 1, function_definition; 2, function_name:clean_pubmed_identifiers; 3, parameters; 4, type; 5, block; 6, typed_parameter; 7, generic_type; 8, expression_statement; 9, return_statement; 10, identifier:pmids; 11, type; 12, identifier:List; 13, type_parameter; 14, comment:"""Clean a list of PubMed identifiers with string strips, deduplicates, and sorting."""; 15, call; 16, generic_type; 17, type; 18, identifier:sorted; 19, argument_list; 20, identifier:Iterable; 21, type_parameter; 22, identifier:str; 23, set_comprehension; 24, type; 25, call; 26, for_in_clause; 27, identifier:str; 28, attribute; 29, argument_list; 30, identifier:pmid; 31, identifier:pmids; 32, call; 33, identifier:strip; 34, identifier:str; 35, argument_list; 36, identifier:pmid | 0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 4, 7; 5, 8; 5, 9; 6, 10; 6, 11; 7, 12; 7, 13; 8, 14; 9, 15; 11, 16; 13, 17; 15, 18; 15, 19; 16, 20; 16, 21; 17, 22; 19, 23; 21, 24; 23, 25; 23, 26; 24, 27; 25, 28; 25, 29; 26, 30; 26, 31; 28, 32; 28, 33; 32, 34; 32, 35; 35, 36 | def clean_pubmed_identifiers(pmids: Iterable[str]) -> List[str]:
"""Clean a list of PubMed identifiers with string strips, deduplicates, and sorting."""
return sorted({str(pmid).strip() for pmid in pmids}) |
0, module; 1, function_definition; 2, function_name:hash_dump; 3, parameters; 4, type; 5, block; 6, identifier:data; 7, identifier:str; 8, expression_statement; 9, return_statement; 10, comment:"""Hash an arbitrary JSON dictionary by dumping it in sorted order, encoding it in UTF-8, then hashing the bytes.
:param data: An arbitrary JSON-serializable object
:type data: dict or list or tuple
"""; 11, call; 12, attribute; 13, argument_list; 14, call; 15, identifier:hexdigest; 16, attribute; 17, argument_list; 18, identifier:hashlib; 19, identifier:sha512; 20, call; 21, attribute; 22, argument_list; 23, call; 24, identifier:encode; 25, string; 26, attribute; 27, argument_list; 28, string_content:utf-8; 29, identifier:json; 30, identifier:dumps; 31, identifier:data; 32, keyword_argument; 33, identifier:sort_keys; 34, True | 0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 4, 7; 5, 8; 5, 9; 8, 10; 9, 11; 11, 12; 11, 13; 12, 14; 12, 15; 14, 16; 14, 17; 16, 18; 16, 19; 17, 20; 20, 21; 20, 22; 21, 23; 21, 24; 22, 25; 23, 26; 23, 27; 25, 28; 26, 29; 26, 30; 27, 31; 27, 32; 32, 33; 32, 34 | def hash_dump(data) -> str:
"""Hash an arbitrary JSON dictionary by dumping it in sorted order, encoding it in UTF-8, then hashing the bytes.
:param data: An arbitrary JSON-serializable object
:type data: dict or list or tuple
"""
return hashlib.sha512(json.dumps(data, sort_keys=True).encode('utf-8')).hexdigest() |
0, module; 1, function_definition; 2, function_name:yield_sorted_by_type; 3, parameters; 4, block; 5, list_splat_pattern; 6, expression_statement; 7, function_definition; 8, return_statement; 9, identifier:typelist; 10, comment:"""
a useful decorator for the collect_impl method of SuperChange
subclasses. Caches the yielded changes, and re-emits them
collected by their type. The order of the types can be specified
by listing the types as arguments to this decorator. Unlisted
types will be yielded last in no guaranteed order.
Grouping happens by exact type match only. Inheritance is not
taken into consideration for grouping.
"""; 11, function_name:decorate; 12, parameters; 13, block; 14, identifier:decorate; 15, identifier:fun; 16, decorated_definition; 17, return_statement; 18, decorator; 19, function_definition; 20, identifier:decorated; 21, call; 22, function_name:decorated; 23, parameters; 24, block; 25, identifier:wraps; 26, argument_list; 27, list_splat_pattern; 28, dictionary_splat_pattern; 29, return_statement; 30, identifier:fun; 31, identifier:args; 32, identifier:kwds; 33, call; 34, identifier:iterate_by_type; 35, argument_list; 36, call; 37, identifier:typelist; 38, identifier:fun; 39, argument_list; 40, list_splat; 41, dictionary_splat; 42, identifier:args; 43, identifier:kwds | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 5, 9; 6, 10; 7, 11; 7, 12; 7, 13; 8, 14; 12, 15; 13, 16; 13, 17; 16, 18; 16, 19; 17, 20; 18, 21; 19, 22; 19, 23; 19, 24; 21, 25; 21, 26; 23, 27; 23, 28; 24, 29; 26, 30; 27, 31; 28, 32; 29, 33; 33, 34; 33, 35; 35, 36; 35, 37; 36, 38; 36, 39; 39, 40; 39, 41; 40, 42; 41, 43 | def yield_sorted_by_type(*typelist):
"""
a useful decorator for the collect_impl method of SuperChange
subclasses. Caches the yielded changes, and re-emits them
collected by their type. The order of the types can be specified
by listing the types as arguments to this decorator. Unlisted
types will be yielded last in no guaranteed order.
Grouping happens by exact type match only. Inheritance is not
taken into consideration for grouping.
"""
def decorate(fun):
@wraps(fun)
def decorated(*args, **kwds):
return iterate_by_type(fun(*args, **kwds), typelist)
return decorated
return decorate |
0, module; 1, function_definition; 2, function_name:build_route_timetable; 3, parameters; 4, type; 5, block; 6, typed_parameter; 7, typed_parameter; 8, typed_parameter; 9, identifier:DataFrame; 10, expression_statement; 11, expression_statement; 12, if_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, for_statement; 18, expression_statement; 19, return_statement; 20, identifier:feed; 21, type; 22, identifier:route_id; 23, type; 24, identifier:dates; 25, type; 26, comment:"""
Return a timetable for the given route and dates.
Parameters
----------
feed : Feed
route_id : string
ID of a route in ``feed.routes``
dates : string or list
A YYYYMMDD date string or list thereof
Returns
-------
DataFrame
The columns are all those in ``feed.trips`` plus those in
``feed.stop_times`` plus ``'date'``, and the trip IDs
are restricted to the given route ID.
The result is sorted first by date and then by grouping by
trip ID and sorting the groups by their first departure time.
Skip dates outside of the Feed's dates.
If there is no route activity on the given dates, then return
an empty DataFrame.
Notes
-----
Assume the following feed attributes are not ``None``:
- ``feed.stop_times``
- Those used in :func:`.trips.get_trips`
"""; 27, assignment; 28, not_operator; 29, block; 30, assignment; 31, assignment; 32, assignment; 33, assignment; 34, identifier:date; 35, identifier:dates; 36, comment:# Slice to trips active on date; 37, block; 38, assignment; 39, call; 40, string:"Feed"; 41, identifier:str; 42, generic_type; 43, identifier:dates; 44, call; 45, identifier:dates; 46, return_statement; 47, identifier:t; 48, call; 49, identifier:t; 50, call; 51, identifier:a; 52, call; 53, identifier:frames; 54, list; 55, expression_statement; 56, expression_statement; 57, expression_statement; 58, comment:# Groupby trip ID and sort groups by their minimum departure time.; 59, comment:# For some reason NaN departure times mess up the transform below.; 60, comment:# So temporarily fill NaN departure times as a workaround.; 61, expression_statement; 62, expression_statement; 63, expression_statement; 64, identifier:f; 65, call; 66, attribute; 67, argument_list; 68, identifier:List; 69, type_parameter; 70, attribute; 71, argument_list; 72, call; 73, attribute; 74, argument_list; 75, attribute; 76, argument_list; 77, attribute; 78, argument_list; 79, assignment; 80, assignment; 81, assignment; 82, assignment; 83, assignment; 84, call; 85, attribute; 86, argument_list; 87, call; 88, identifier:drop; 89, list; 90, keyword_argument; 91, type; 92, identifier:feed; 93, identifier:restrict_dates; 94, identifier:dates; 95, attribute; 96, argument_list; 97, identifier:pd; 98, identifier:merge; 99, attribute; 100, attribute; 101, subscript; 102, identifier:copy; 103, identifier:feed; 104, identifier:compute_trip_activity; 105, identifier:dates; 106, identifier:ids; 107, subscript; 108, identifier:f; 109, call; 110, subscript; 111, identifier:date; 112, subscript; 113, call; 114, subscript; 115, call; 116, attribute; 117, argument_list; 118, identifier:pd; 119, identifier:concat; 120, identifier:frames; 121, attribute; 122, argument_list; 123, string:"min_dt"; 124, string:"dt"; 125, identifier:axis; 126, integer:1; 127, identifier:str; 128, identifier:pd; 129, identifier:DataFrame; 130, identifier:feed; 131, identifier:trips; 132, identifier:feed; 133, identifier:stop_times; 134, identifier:t; 135, comparison_operator:t["route_id"] == route_id; 136, attribute; 137, comparison_operator:a[date] == 1; 138, string:"trip_id"; 139, attribute; 140, argument_list; 141, identifier:f; 142, string:"date"; 143, identifier:f; 144, string:"dt"; 145, attribute; 146, argument_list; 147, identifier:f; 148, string:"min_dt"; 149, attribute; 150, argument_list; 151, identifier:frames; 152, identifier:append; 153, identifier:f; 154, identifier:f; 155, identifier:sort_values; 156, list; 157, subscript; 158, identifier:route_id; 159, identifier:a; 160, identifier:loc; 161, subscript; 162, integer:1; 163, subscript; 164, identifier:copy; 165, subscript; 166, identifier:fillna; 167, keyword_argument; 168, subscript; 169, identifier:transform; 170, identifier:min; 171, string:"date"; 172, string:"min_dt"; 173, string:"stop_sequence"; 174, identifier:t; 175, string:"route_id"; 176, identifier:a; 177, identifier:date; 178, identifier:t; 179, call; 180, identifier:f; 181, string:"departure_time"; 182, identifier:method; 183, string:"ffill"; 184, call; 185, string:"dt"; 186, attribute; 187, argument_list; 188, attribute; 189, argument_list; 190, subscript; 191, identifier:isin; 192, identifier:ids; 193, identifier:f; 194, identifier:groupby; 195, string:"trip_id"; 196, identifier:t; 197, string:"trip_id" | 0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 3, 7; 3, 8; 4, 9; 5, 10; 5, 11; 5, 12; 5, 13; 5, 14; 5, 15; 5, 16; 5, 17; 5, 18; 5, 19; 6, 20; 6, 21; 7, 22; 7, 23; 8, 24; 8, 25; 10, 26; 11, 27; 12, 28; 12, 29; 13, 30; 14, 31; 15, 32; 16, 33; 17, 34; 17, 35; 17, 36; 17, 37; 18, 38; 19, 39; 21, 40; 23, 41; 25, 42; 27, 43; 27, 44; 28, 45; 29, 46; 30, 47; 30, 48; 31, 49; 31, 50; 32, 51; 32, 52; 33, 53; 33, 54; 37, 55; 37, 56; 37, 57; 37, 58; 37, 59; 37, 60; 37, 61; 37, 62; 37, 63; 38, 64; 38, 65; 39, 66; 39, 67; 42, 68; 42, 69; 44, 70; 44, 71; 46, 72; 48, 73; 48, 74; 50, 75; 50, 76; 52, 77; 52, 78; 55, 79; 56, 80; 57, 81; 61, 82; 62, 83; 63, 84; 65, 85; 65, 86; 66, 87; 66, 88; 67, 89; 67, 90; 69, 91; 70, 92; 70, 93; 71, 94; 72, 95; 72, 96; 73, 97; 73, 98; 74, 99; 74, 100; 75, 101; 75, 102; 77, 103; 77, 104; 78, 105; 79, 106; 79, 107; 80, 108; 80, 109; 81, 110; 81, 111; 82, 112; 82, 113; 83, 114; 83, 115; 84, 116; 84, 117; 85, 118; 85, 119; 86, 120; 87, 121; 87, 122; 89, 123; 89, 124; 90, 125; 90, 126; 91, 127; 95, 128; 95, 129; 99, 130; 99, 131; 100, 132; 100, 133; 101, 134; 101, 135; 107, 136; 107, 137; 107, 138; 109, 139; 109, 140; 110, 141; 110, 142; 112, 143; 112, 144; 113, 145; 113, 146; 114, 147; 114, 148; 115, 149; 115, 150; 116, 151; 116, 152; 117, 153; 121, 154; 121, 155; 122, 156; 135, 157; 135, 158; 136, 159; 136, 160; 137, 161; 137, 162; 139, 163; 139, 164; 145, 165; 145, 166; 146, 167; 149, 168; 149, 169; 150, 170; 156, 171; 156, 172; 156, 173; 157, 174; 157, 175; 161, 176; 161, 177; 163, 178; 163, 179; 165, 180; 165, 181; 167, 182; 167, 183; 168, 184; 168, 185; 179, 186; 179, 187; 184, 188; 184, 189; 186, 190; 186, 191; 187, 192; 188, 193; 188, 194; 189, 195; 190, 196; 190, 197 | def build_route_timetable(
feed: "Feed", route_id: str, dates: List[str]
) -> DataFrame:
"""
Return a timetable for the given route and dates.
Parameters
----------
feed : Feed
route_id : string
ID of a route in ``feed.routes``
dates : string or list
A YYYYMMDD date string or list thereof
Returns
-------
DataFrame
The columns are all those in ``feed.trips`` plus those in
``feed.stop_times`` plus ``'date'``, and the trip IDs
are restricted to the given route ID.
The result is sorted first by date and then by grouping by
trip ID and sorting the groups by their first departure time.
Skip dates outside of the Feed's dates.
If there is no route activity on the given dates, then return
an empty DataFrame.
Notes
-----
Assume the following feed attributes are not ``None``:
- ``feed.stop_times``
- Those used in :func:`.trips.get_trips`
"""
dates = feed.restrict_dates(dates)
if not dates:
return pd.DataFrame()
t = pd.merge(feed.trips, feed.stop_times)
t = t[t["route_id"] == route_id].copy()
a = feed.compute_trip_activity(dates)
frames = []
for date in dates:
# Slice to trips active on date
ids = a.loc[a[date] == 1, "trip_id"]
f = t[t["trip_id"].isin(ids)].copy()
f["date"] = date
# Groupby trip ID and sort groups by their minimum departure time.
# For some reason NaN departure times mess up the transform below.
# So temporarily fill NaN departure times as a workaround.
f["dt"] = f["departure_time"].fillna(method="ffill")
f["min_dt"] = f.groupby("trip_id")["dt"].transform(min)
frames.append(f)
f = pd.concat(frames)
return f.sort_values(["date", "min_dt", "stop_sequence"]).drop(
["min_dt", "dt"], axis=1
) |
0, module; 1, function_definition; 2, function_name:almost_equal; 3, parameters; 4, type; 5, block; 6, typed_parameter; 7, typed_parameter; 8, identifier:bool; 9, expression_statement; 10, if_statement; 11, identifier:f; 12, type; 13, identifier:g; 14, type; 15, comment:"""
Return ``True`` if and only if the given DataFrames are equal after
sorting their columns names, sorting their values, and
reseting their indices.
"""; 16, boolean_operator; 17, block; 18, else_clause; 19, identifier:DataFrame; 20, identifier:DataFrame; 21, attribute; 22, attribute; 23, return_statement; 24, comment:# Put in canonical order; 25, block; 26, identifier:f; 27, identifier:empty; 28, identifier:g; 29, identifier:empty; 30, call; 31, expression_statement; 32, expression_statement; 33, return_statement; 34, attribute; 35, argument_list; 36, assignment; 37, assignment; 38, call; 39, identifier:f; 40, identifier:equals; 41, identifier:g; 42, identifier:F; 43, parenthesized_expression; 44, identifier:G; 45, parenthesized_expression; 46, attribute; 47, argument_list; 48, call; 49, call; 50, identifier:F; 51, identifier:equals; 52, identifier:G; 53, attribute; 54, argument_list; 55, attribute; 56, argument_list; 57, call; 58, identifier:reset_index; 59, keyword_argument; 60, call; 61, identifier:reset_index; 62, keyword_argument; 63, attribute; 64, argument_list; 65, identifier:drop; 66, True; 67, attribute; 68, argument_list; 69, identifier:drop; 70, True; 71, call; 72, identifier:sort_values; 73, call; 74, call; 75, identifier:sort_values; 76, call; 77, attribute; 78, argument_list; 79, identifier:list; 80, argument_list; 81, attribute; 82, argument_list; 83, identifier:list; 84, argument_list; 85, identifier:f; 86, identifier:sort_index; 87, keyword_argument; 88, attribute; 89, identifier:g; 90, identifier:sort_index; 91, keyword_argument; 92, attribute; 93, identifier:axis; 94, integer:1; 95, identifier:f; 96, identifier:columns; 97, identifier:axis; 98, integer:1; 99, identifier:g; 100, identifier:columns | 0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 3, 7; 4, 8; 5, 9; 5, 10; 6, 11; 6, 12; 7, 13; 7, 14; 9, 15; 10, 16; 10, 17; 10, 18; 12, 19; 14, 20; 16, 21; 16, 22; 17, 23; 18, 24; 18, 25; 21, 26; 21, 27; 22, 28; 22, 29; 23, 30; 25, 31; 25, 32; 25, 33; 30, 34; 30, 35; 31, 36; 32, 37; 33, 38; 34, 39; 34, 40; 35, 41; 36, 42; 36, 43; 37, 44; 37, 45; 38, 46; 38, 47; 43, 48; 45, 49; 46, 50; 46, 51; 47, 52; 48, 53; 48, 54; 49, 55; 49, 56; 53, 57; 53, 58; 54, 59; 55, 60; 55, 61; 56, 62; 57, 63; 57, 64; 59, 65; 59, 66; 60, 67; 60, 68; 62, 69; 62, 70; 63, 71; 63, 72; 64, 73; 67, 74; 67, 75; 68, 76; 71, 77; 71, 78; 73, 79; 73, 80; 74, 81; 74, 82; 76, 83; 76, 84; 77, 85; 77, 86; 78, 87; 80, 88; 81, 89; 81, 90; 82, 91; 84, 92; 87, 93; 87, 94; 88, 95; 88, 96; 91, 97; 91, 98; 92, 99; 92, 100 | def almost_equal(f: DataFrame, g: DataFrame) -> bool:
"""
Return ``True`` if and only if the given DataFrames are equal after
sorting their columns names, sorting their values, and
reseting their indices.
"""
if f.empty or g.empty:
return f.equals(g)
else:
# Put in canonical order
F = (
f.sort_index(axis=1)
.sort_values(list(f.columns))
.reset_index(drop=True)
)
G = (
g.sort_index(axis=1)
.sort_values(list(g.columns))
.reset_index(drop=True)
)
return F.equals(G) |
0, module; 1, function_definition; 2, function_name:build_stop_timetable; 3, parameters; 4, type; 5, block; 6, typed_parameter; 7, typed_parameter; 8, typed_parameter; 9, identifier:DataFrame; 10, expression_statement; 11, expression_statement; 12, if_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, for_statement; 18, expression_statement; 19, return_statement; 20, identifier:feed; 21, type; 22, identifier:stop_id; 23, type; 24, identifier:dates; 25, type; 26, comment:"""
Return a DataFrame containing the timetable for the given stop ID
and dates.
Parameters
----------
feed : Feed
stop_id : string
ID of the stop for which to build the timetable
dates : string or list
A YYYYMMDD date string or list thereof
Returns
-------
DataFrame
The columns are all those in ``feed.trips`` plus those in
``feed.stop_times`` plus ``'date'``, and the stop IDs are
restricted to the given stop ID.
The result is sorted by date then departure time.
Notes
-----
Assume the following feed attributes are not ``None``:
- ``feed.trips``
- Those used in :func:`.stop_times.get_stop_times`
"""; 27, assignment; 28, not_operator; 29, block; 30, assignment; 31, assignment; 32, assignment; 33, assignment; 34, identifier:date; 35, identifier:dates; 36, comment:# Slice to stops active on date; 37, block; 38, assignment; 39, call; 40, string:"Feed"; 41, identifier:str; 42, generic_type; 43, identifier:dates; 44, call; 45, identifier:dates; 46, return_statement; 47, identifier:t; 48, call; 49, identifier:t; 50, call; 51, identifier:a; 52, call; 53, identifier:frames; 54, list; 55, expression_statement; 56, expression_statement; 57, expression_statement; 58, expression_statement; 59, identifier:f; 60, call; 61, attribute; 62, argument_list; 63, identifier:List; 64, type_parameter; 65, attribute; 66, argument_list; 67, call; 68, attribute; 69, argument_list; 70, attribute; 71, argument_list; 72, attribute; 73, argument_list; 74, assignment; 75, assignment; 76, assignment; 77, call; 78, attribute; 79, argument_list; 80, identifier:f; 81, identifier:sort_values; 82, list; 83, type; 84, identifier:feed; 85, identifier:restrict_dates; 86, identifier:dates; 87, attribute; 88, argument_list; 89, identifier:pd; 90, identifier:merge; 91, attribute; 92, attribute; 93, subscript; 94, identifier:copy; 95, identifier:feed; 96, identifier:compute_trip_activity; 97, identifier:dates; 98, identifier:ids; 99, subscript; 100, identifier:f; 101, call; 102, subscript; 103, identifier:date; 104, attribute; 105, argument_list; 106, identifier:pd; 107, identifier:concat; 108, identifier:frames; 109, string:"date"; 110, string:"departure_time"; 111, identifier:str; 112, identifier:pd; 113, identifier:DataFrame; 114, identifier:feed; 115, identifier:trips; 116, identifier:feed; 117, identifier:stop_times; 118, identifier:t; 119, comparison_operator:t["stop_id"] == stop_id; 120, attribute; 121, comparison_operator:a[date] == 1; 122, string:"trip_id"; 123, attribute; 124, argument_list; 125, identifier:f; 126, string:"date"; 127, identifier:frames; 128, identifier:append; 129, identifier:f; 130, subscript; 131, identifier:stop_id; 132, identifier:a; 133, identifier:loc; 134, subscript; 135, integer:1; 136, subscript; 137, identifier:copy; 138, identifier:t; 139, string:"stop_id"; 140, identifier:a; 141, identifier:date; 142, identifier:t; 143, call; 144, attribute; 145, argument_list; 146, subscript; 147, identifier:isin; 148, identifier:ids; 149, identifier:t; 150, string:"trip_id" | 0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 3, 7; 3, 8; 4, 9; 5, 10; 5, 11; 5, 12; 5, 13; 5, 14; 5, 15; 5, 16; 5, 17; 5, 18; 5, 19; 6, 20; 6, 21; 7, 22; 7, 23; 8, 24; 8, 25; 10, 26; 11, 27; 12, 28; 12, 29; 13, 30; 14, 31; 15, 32; 16, 33; 17, 34; 17, 35; 17, 36; 17, 37; 18, 38; 19, 39; 21, 40; 23, 41; 25, 42; 27, 43; 27, 44; 28, 45; 29, 46; 30, 47; 30, 48; 31, 49; 31, 50; 32, 51; 32, 52; 33, 53; 33, 54; 37, 55; 37, 56; 37, 57; 37, 58; 38, 59; 38, 60; 39, 61; 39, 62; 42, 63; 42, 64; 44, 65; 44, 66; 46, 67; 48, 68; 48, 69; 50, 70; 50, 71; 52, 72; 52, 73; 55, 74; 56, 75; 57, 76; 58, 77; 60, 78; 60, 79; 61, 80; 61, 81; 62, 82; 64, 83; 65, 84; 65, 85; 66, 86; 67, 87; 67, 88; 68, 89; 68, 90; 69, 91; 69, 92; 70, 93; 70, 94; 72, 95; 72, 96; 73, 97; 74, 98; 74, 99; 75, 100; 75, 101; 76, 102; 76, 103; 77, 104; 77, 105; 78, 106; 78, 107; 79, 108; 82, 109; 82, 110; 83, 111; 87, 112; 87, 113; 91, 114; 91, 115; 92, 116; 92, 117; 93, 118; 93, 119; 99, 120; 99, 121; 99, 122; 101, 123; 101, 124; 102, 125; 102, 126; 104, 127; 104, 128; 105, 129; 119, 130; 119, 131; 120, 132; 120, 133; 121, 134; 121, 135; 123, 136; 123, 137; 130, 138; 130, 139; 134, 140; 134, 141; 136, 142; 136, 143; 143, 144; 143, 145; 144, 146; 144, 147; 145, 148; 146, 149; 146, 150 | def build_stop_timetable(
feed: "Feed", stop_id: str, dates: List[str]
) -> DataFrame:
"""
Return a DataFrame containing the timetable for the given stop ID
and dates.
Parameters
----------
feed : Feed
stop_id : string
ID of the stop for which to build the timetable
dates : string or list
A YYYYMMDD date string or list thereof
Returns
-------
DataFrame
The columns are all those in ``feed.trips`` plus those in
``feed.stop_times`` plus ``'date'``, and the stop IDs are
restricted to the given stop ID.
The result is sorted by date then departure time.
Notes
-----
Assume the following feed attributes are not ``None``:
- ``feed.trips``
- Those used in :func:`.stop_times.get_stop_times`
"""
dates = feed.restrict_dates(dates)
if not dates:
return pd.DataFrame()
t = pd.merge(feed.trips, feed.stop_times)
t = t[t["stop_id"] == stop_id].copy()
a = feed.compute_trip_activity(dates)
frames = []
for date in dates:
# Slice to stops active on date
ids = a.loc[a[date] == 1, "trip_id"]
f = t[t["trip_id"].isin(ids)].copy()
f["date"] = date
frames.append(f)
f = pd.concat(frames)
return f.sort_values(["date", "departure_time"]) |
0, module; 1, function_definition; 2, function_name:get_unit_property_names; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, expression_statement; 8, if_statement; 9, if_statement; 10, identifier:unit_id; 11, None; 12, string; 13, comparison_operator:unit_id is None; 14, block; 15, call; 16, block; 17, else_clause; 18, string_content:Get a list of property names for a given unit, or for all units if unit_id is None
Parameters
----------
unit_id: int
The unit id for which the property names will be returned
If None (default), will return property names for all units
Returns
----------
property_names
The list of property names from the specified unit(s); 19, identifier:unit_id; 20, None; 21, expression_statement; 22, for_statement; 23, expression_statement; 24, return_statement; 25, identifier:isinstance; 26, argument_list; 27, if_statement; 28, block; 29, assignment; 30, identifier:unit_id; 31, call; 32, block; 33, assignment; 34, identifier:property_names; 35, identifier:unit_id; 36, tuple; 37, comparison_operator:unit_id in self.get_unit_ids(); 38, block; 39, else_clause; 40, raise_statement; 41, identifier:property_names; 42, list; 43, attribute; 44, argument_list; 45, expression_statement; 46, for_statement; 47, identifier:property_names; 48, call; 49, identifier:int; 50, attribute; 51, identifier:unit_id; 52, call; 53, if_statement; 54, expression_statement; 55, return_statement; 56, block; 57, call; 58, identifier:self; 59, identifier:get_unit_ids; 60, assignment; 61, identifier:curr_property_name; 62, identifier:curr_property_names; 63, block; 64, identifier:sorted; 65, argument_list; 66, identifier:np; 67, identifier:integer; 68, attribute; 69, argument_list; 70, comparison_operator:unit_id not in self._unit_properties; 71, block; 72, assignment; 73, identifier:property_names; 74, raise_statement; 75, identifier:ValueError; 76, argument_list; 77, identifier:curr_property_names; 78, call; 79, expression_statement; 80, call; 81, identifier:self; 82, identifier:get_unit_ids; 83, identifier:unit_id; 84, attribute; 85, expression_statement; 86, identifier:property_names; 87, call; 88, call; 89, binary_operator:str(unit_id) + " must be an int"; 90, attribute; 91, argument_list; 92, call; 93, identifier:list; 94, argument_list; 95, identifier:self; 96, identifier:_unit_properties; 97, assignment; 98, identifier:sorted; 99, argument_list; 100, identifier:ValueError; 101, argument_list; 102, call; 103, string:" must be an int"; 104, identifier:self; 105, identifier:get_unit_property_names; 106, identifier:unit_id; 107, attribute; 108, argument_list; 109, call; 110, subscript; 111, dictionary; 112, call; 113, binary_operator:str(unit_id) + " is not a valid unit_id"; 114, identifier:str; 115, argument_list; 116, identifier:property_names; 117, identifier:append; 118, identifier:curr_property_name; 119, identifier:set; 120, argument_list; 121, attribute; 122, identifier:unit_id; 123, attribute; 124, argument_list; 125, call; 126, string:" is not a valid unit_id"; 127, identifier:unit_id; 128, identifier:property_names; 129, identifier:self; 130, identifier:_unit_properties; 131, subscript; 132, identifier:keys; 133, identifier:str; 134, argument_list; 135, attribute; 136, identifier:unit_id; 137, identifier:unit_id; 138, identifier:self; 139, identifier:_unit_properties | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 6, 10; 6, 11; 7, 12; 8, 13; 8, 14; 9, 15; 9, 16; 9, 17; 12, 18; 13, 19; 13, 20; 14, 21; 14, 22; 14, 23; 14, 24; 15, 25; 15, 26; 16, 27; 17, 28; 21, 29; 22, 30; 22, 31; 22, 32; 23, 33; 24, 34; 26, 35; 26, 36; 27, 37; 27, 38; 27, 39; 28, 40; 29, 41; 29, 42; 31, 43; 31, 44; 32, 45; 32, 46; 33, 47; 33, 48; 36, 49; 36, 50; 37, 51; 37, 52; 38, 53; 38, 54; 38, 55; 39, 56; 40, 57; 43, 58; 43, 59; 45, 60; 46, 61; 46, 62; 46, 63; 48, 64; 48, 65; 50, 66; 50, 67; 52, 68; 52, 69; 53, 70; 53, 71; 54, 72; 55, 73; 56, 74; 57, 75; 57, 76; 60, 77; 60, 78; 63, 79; 65, 80; 68, 81; 68, 82; 70, 83; 70, 84; 71, 85; 72, 86; 72, 87; 74, 88; 76, 89; 78, 90; 78, 91; 79, 92; 80, 93; 80, 94; 84, 95; 84, 96; 85, 97; 87, 98; 87, 99; 88, 100; 88, 101; 89, 102; 89, 103; 90, 104; 90, 105; 91, 106; 92, 107; 92, 108; 94, 109; 97, 110; 97, 111; 99, 112; 101, 113; 102, 114; 102, 115; 107, 116; 107, 117; 108, 118; 109, 119; 109, 120; 110, 121; 110, 122; 112, 123; 112, 124; 113, 125; 113, 126; 115, 127; 120, 128; 121, 129; 121, 130; 123, 131; 123, 132; 125, 133; 125, 134; 131, 135; 131, 136; 134, 137; 135, 138; 135, 139 | def get_unit_property_names(self, unit_id=None):
'''Get a list of property names for a given unit, or for all units if unit_id is None
Parameters
----------
unit_id: int
The unit id for which the property names will be returned
If None (default), will return property names for all units
Returns
----------
property_names
The list of property names from the specified unit(s)
'''
if unit_id is None:
property_names = []
for unit_id in self.get_unit_ids():
curr_property_names = self.get_unit_property_names(unit_id)
for curr_property_name in curr_property_names:
property_names.append(curr_property_name)
property_names = sorted(list(set(property_names)))
return property_names
if isinstance(unit_id, (int, np.integer)):
if unit_id in self.get_unit_ids():
if unit_id not in self._unit_properties:
self._unit_properties[unit_id] = {}
property_names = sorted(self._unit_properties[unit_id].keys())
return property_names
else:
raise ValueError(str(unit_id) + " is not a valid unit_id")
else:
raise ValueError(str(unit_id) + " must be an int") |
0, module; 1, function_definition; 2, function_name:copy_unit_properties; 3, parameters; 4, block; 5, identifier:self; 6, identifier:sorting; 7, default_parameter; 8, expression_statement; 9, if_statement; 10, if_statement; 11, identifier:unit_ids; 12, None; 13, string; 14, comparison_operator:unit_ids is None; 15, block; 16, call; 17, block; 18, else_clause; 19, string_content:Copy unit properties from another sorting extractor to the current
sorting extractor.
Parameters
----------
sorting: SortingExtractor
The sorting extractor from which the properties will be copied
unit_ids: (array_like, int)
The list (or single value) of unit_ids for which the properties will be copied.; 20, identifier:unit_ids; 21, None; 22, expression_statement; 23, identifier:isinstance; 24, argument_list; 25, expression_statement; 26, for_statement; 27, block; 28, assignment; 29, identifier:unit_ids; 30, identifier:int; 31, assignment; 32, identifier:curr_property_name; 33, identifier:curr_property_names; 34, block; 35, for_statement; 36, identifier:unit_ids; 37, call; 38, identifier:curr_property_names; 39, call; 40, expression_statement; 41, expression_statement; 42, identifier:unit_id; 43, identifier:unit_ids; 44, block; 45, attribute; 46, argument_list; 47, attribute; 48, argument_list; 49, assignment; 50, call; 51, expression_statement; 52, for_statement; 53, identifier:sorting; 54, identifier:get_unit_ids; 55, identifier:sorting; 56, identifier:get_unit_property_names; 57, keyword_argument; 58, identifier:value; 59, call; 60, attribute; 61, argument_list; 62, assignment; 63, identifier:curr_property_name; 64, identifier:curr_property_names; 65, block; 66, identifier:unit_id; 67, identifier:unit_ids; 68, attribute; 69, argument_list; 70, identifier:self; 71, identifier:set_unit_property; 72, keyword_argument; 73, keyword_argument; 74, keyword_argument; 75, identifier:curr_property_names; 76, call; 77, expression_statement; 78, expression_statement; 79, identifier:sorting; 80, identifier:get_unit_property; 81, keyword_argument; 82, keyword_argument; 83, identifier:unit_id; 84, identifier:unit_ids; 85, identifier:property_name; 86, identifier:curr_property_name; 87, identifier:value; 88, identifier:value; 89, attribute; 90, argument_list; 91, assignment; 92, call; 93, identifier:unit_id; 94, identifier:unit_ids; 95, identifier:property_name; 96, identifier:curr_property_name; 97, identifier:sorting; 98, identifier:get_unit_property_names; 99, keyword_argument; 100, identifier:value; 101, call; 102, attribute; 103, argument_list; 104, identifier:unit_id; 105, identifier:unit_id; 106, attribute; 107, argument_list; 108, identifier:self; 109, identifier:set_unit_property; 110, keyword_argument; 111, keyword_argument; 112, keyword_argument; 113, identifier:sorting; 114, identifier:get_unit_property; 115, keyword_argument; 116, keyword_argument; 117, identifier:unit_id; 118, identifier:unit_id; 119, identifier:property_name; 120, identifier:curr_property_name; 121, identifier:value; 122, identifier:value; 123, identifier:unit_id; 124, identifier:unit_id; 125, identifier:property_name; 126, identifier:curr_property_name | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 7, 11; 7, 12; 8, 13; 9, 14; 9, 15; 10, 16; 10, 17; 10, 18; 13, 19; 14, 20; 14, 21; 15, 22; 16, 23; 16, 24; 17, 25; 17, 26; 18, 27; 22, 28; 24, 29; 24, 30; 25, 31; 26, 32; 26, 33; 26, 34; 27, 35; 28, 36; 28, 37; 31, 38; 31, 39; 34, 40; 34, 41; 35, 42; 35, 43; 35, 44; 37, 45; 37, 46; 39, 47; 39, 48; 40, 49; 41, 50; 44, 51; 44, 52; 45, 53; 45, 54; 47, 55; 47, 56; 48, 57; 49, 58; 49, 59; 50, 60; 50, 61; 51, 62; 52, 63; 52, 64; 52, 65; 57, 66; 57, 67; 59, 68; 59, 69; 60, 70; 60, 71; 61, 72; 61, 73; 61, 74; 62, 75; 62, 76; 65, 77; 65, 78; 68, 79; 68, 80; 69, 81; 69, 82; 72, 83; 72, 84; 73, 85; 73, 86; 74, 87; 74, 88; 76, 89; 76, 90; 77, 91; 78, 92; 81, 93; 81, 94; 82, 95; 82, 96; 89, 97; 89, 98; 90, 99; 91, 100; 91, 101; 92, 102; 92, 103; 99, 104; 99, 105; 101, 106; 101, 107; 102, 108; 102, 109; 103, 110; 103, 111; 103, 112; 106, 113; 106, 114; 107, 115; 107, 116; 110, 117; 110, 118; 111, 119; 111, 120; 112, 121; 112, 122; 115, 123; 115, 124; 116, 125; 116, 126 | def copy_unit_properties(self, sorting, unit_ids=None):
'''Copy unit properties from another sorting extractor to the current
sorting extractor.
Parameters
----------
sorting: SortingExtractor
The sorting extractor from which the properties will be copied
unit_ids: (array_like, int)
The list (or single value) of unit_ids for which the properties will be copied.
'''
if unit_ids is None:
unit_ids = sorting.get_unit_ids()
if isinstance(unit_ids, int):
curr_property_names = sorting.get_unit_property_names(unit_id=unit_ids)
for curr_property_name in curr_property_names:
value = sorting.get_unit_property(unit_id=unit_ids, property_name=curr_property_name)
self.set_unit_property(unit_id=unit_ids, property_name=curr_property_name, value=value)
else:
for unit_id in unit_ids:
curr_property_names = sorting.get_unit_property_names(unit_id=unit_id)
for curr_property_name in curr_property_names:
value = sorting.get_unit_property(unit_id=unit_id, property_name=curr_property_name)
self.set_unit_property(unit_id=unit_id, property_name=curr_property_name, value=value) |
0, module; 1, function_definition; 2, function_name:copy_unit_spike_features; 3, parameters; 4, block; 5, identifier:self; 6, identifier:sorting; 7, default_parameter; 8, expression_statement; 9, if_statement; 10, if_statement; 11, identifier:unit_ids; 12, None; 13, string; 14, comparison_operator:unit_ids is None; 15, block; 16, call; 17, block; 18, else_clause; 19, string_content:Copy unit spike features from another sorting extractor to the current
sorting extractor.
Parameters
----------
sorting: SortingExtractor
The sorting extractor from which the spike features will be copied
unit_ids: (array_like, int)
The list (or single value) of unit_ids for which the spike features will be copied.
def get_unit_spike_features(self, unit_id, feature_name, start_frame=None, end_frame=None):; 20, identifier:unit_ids; 21, None; 22, expression_statement; 23, identifier:isinstance; 24, argument_list; 25, expression_statement; 26, for_statement; 27, block; 28, assignment; 29, identifier:unit_ids; 30, identifier:int; 31, assignment; 32, identifier:curr_feature_name; 33, identifier:curr_feature_names; 34, block; 35, for_statement; 36, identifier:unit_ids; 37, call; 38, identifier:curr_feature_names; 39, call; 40, expression_statement; 41, expression_statement; 42, identifier:unit_id; 43, identifier:unit_ids; 44, block; 45, attribute; 46, argument_list; 47, attribute; 48, argument_list; 49, assignment; 50, call; 51, expression_statement; 52, for_statement; 53, identifier:sorting; 54, identifier:get_unit_ids; 55, identifier:sorting; 56, identifier:get_unit_spike_feature_names; 57, keyword_argument; 58, identifier:value; 59, call; 60, attribute; 61, argument_list; 62, assignment; 63, identifier:curr_feature_name; 64, identifier:curr_feature_names; 65, block; 66, identifier:unit_id; 67, identifier:unit_ids; 68, attribute; 69, argument_list; 70, identifier:self; 71, identifier:set_unit_spike_features; 72, keyword_argument; 73, keyword_argument; 74, keyword_argument; 75, identifier:curr_feature_names; 76, call; 77, expression_statement; 78, expression_statement; 79, identifier:sorting; 80, identifier:get_unit_spike_features; 81, keyword_argument; 82, keyword_argument; 83, identifier:unit_id; 84, identifier:unit_ids; 85, identifier:feature_name; 86, identifier:curr_feature_name; 87, identifier:value; 88, identifier:value; 89, attribute; 90, argument_list; 91, assignment; 92, call; 93, identifier:unit_id; 94, identifier:unit_ids; 95, identifier:feature_name; 96, identifier:curr_feature_name; 97, identifier:sorting; 98, identifier:get_unit_spike_feature_names; 99, keyword_argument; 100, identifier:value; 101, call; 102, attribute; 103, argument_list; 104, identifier:unit_id; 105, identifier:unit_id; 106, attribute; 107, argument_list; 108, identifier:self; 109, identifier:set_unit_spike_features; 110, keyword_argument; 111, keyword_argument; 112, keyword_argument; 113, identifier:sorting; 114, identifier:get_unit_spike_features; 115, keyword_argument; 116, keyword_argument; 117, identifier:unit_id; 118, identifier:unit_id; 119, identifier:feature_name; 120, identifier:curr_feature_name; 121, identifier:value; 122, identifier:value; 123, identifier:unit_id; 124, identifier:unit_id; 125, identifier:feature_name; 126, identifier:curr_feature_name | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 7, 11; 7, 12; 8, 13; 9, 14; 9, 15; 10, 16; 10, 17; 10, 18; 13, 19; 14, 20; 14, 21; 15, 22; 16, 23; 16, 24; 17, 25; 17, 26; 18, 27; 22, 28; 24, 29; 24, 30; 25, 31; 26, 32; 26, 33; 26, 34; 27, 35; 28, 36; 28, 37; 31, 38; 31, 39; 34, 40; 34, 41; 35, 42; 35, 43; 35, 44; 37, 45; 37, 46; 39, 47; 39, 48; 40, 49; 41, 50; 44, 51; 44, 52; 45, 53; 45, 54; 47, 55; 47, 56; 48, 57; 49, 58; 49, 59; 50, 60; 50, 61; 51, 62; 52, 63; 52, 64; 52, 65; 57, 66; 57, 67; 59, 68; 59, 69; 60, 70; 60, 71; 61, 72; 61, 73; 61, 74; 62, 75; 62, 76; 65, 77; 65, 78; 68, 79; 68, 80; 69, 81; 69, 82; 72, 83; 72, 84; 73, 85; 73, 86; 74, 87; 74, 88; 76, 89; 76, 90; 77, 91; 78, 92; 81, 93; 81, 94; 82, 95; 82, 96; 89, 97; 89, 98; 90, 99; 91, 100; 91, 101; 92, 102; 92, 103; 99, 104; 99, 105; 101, 106; 101, 107; 102, 108; 102, 109; 103, 110; 103, 111; 103, 112; 106, 113; 106, 114; 107, 115; 107, 116; 110, 117; 110, 118; 111, 119; 111, 120; 112, 121; 112, 122; 115, 123; 115, 124; 116, 125; 116, 126 | def copy_unit_spike_features(self, sorting, unit_ids=None):
'''Copy unit spike features from another sorting extractor to the current
sorting extractor.
Parameters
----------
sorting: SortingExtractor
The sorting extractor from which the spike features will be copied
unit_ids: (array_like, int)
The list (or single value) of unit_ids for which the spike features will be copied.
def get_unit_spike_features(self, unit_id, feature_name, start_frame=None, end_frame=None):
'''
if unit_ids is None:
unit_ids = sorting.get_unit_ids()
if isinstance(unit_ids, int):
curr_feature_names = sorting.get_unit_spike_feature_names(unit_id=unit_ids)
for curr_feature_name in curr_feature_names:
value = sorting.get_unit_spike_features(unit_id=unit_ids, feature_name=curr_feature_name)
self.set_unit_spike_features(unit_id=unit_ids, feature_name=curr_feature_name, value=value)
else:
for unit_id in unit_ids:
curr_feature_names = sorting.get_unit_spike_feature_names(unit_id=unit_id)
for curr_feature_name in curr_feature_names:
value = sorting.get_unit_spike_features(unit_id=unit_id, feature_name=curr_feature_name)
self.set_unit_spike_features(unit_id=unit_id, feature_name=curr_feature_name, value=value) |
0, module; 1, function_definition; 2, function_name:merge_units; 3, parameters; 4, block; 5, identifier:self; 6, identifier:unit_ids; 7, expression_statement; 8, expression_statement; 9, for_statement; 10, expression_statement; 11, if_statement; 12, string; 13, assignment; 14, identifier:i; 15, call; 16, block; 17, assignment; 18, parenthesized_expression; 19, comment:#Find all unique feature names and create all feature lists; 20, block; 21, else_clause; 22, string_content:This function merges two roots from the curation tree according to the given unit_ids. It creates a new unit_id and root
that has the merged roots as children.
Parameters
----------
unit_ids: list
The unit ids to be merged; 23, identifier:root_ids; 24, list; 25, identifier:range; 26, argument_list; 27, expression_statement; 28, expression_statement; 29, identifier:indices_to_be_deleted; 30, list; 31, boolean_operator; 32, expression_statement; 33, for_statement; 34, expression_statement; 35, for_statement; 36, expression_statement; 37, expression_statement; 38, for_statement; 39, expression_statement; 40, expression_statement; 41, expression_statement; 42, expression_statement; 43, for_statement; 44, expression_statement; 45, expression_statement; 46, expression_statement; 47, delete_statement; 48, expression_statement; 49, expression_statement; 50, for_statement; 51, block; 52, call; 53, assignment; 54, call; 55, call; 56, comparison_operator:len(unit_ids) > 1; 57, assignment; 58, identifier:unit_id; 59, identifier:unit_ids; 60, block; 61, assignment; 62, identifier:feature_names; 63, subscript; 64, block; 65, assignment; 66, assignment; 67, identifier:i; 68, call; 69, block; 70, assignment; 71, call; 72, assignment; 73, assignment; 74, identifier:unit_id; 75, identifier:unit_ids; 76, block; 77, assignment; 78, assignment; 79, call; 80, identifier:all_spike_trains; 81, assignment; 82, call; 83, pattern_list; 84, call; 85, block; 86, raise_statement; 87, identifier:len; 88, argument_list; 89, identifier:root_id; 90, attribute; 91, attribute; 92, argument_list; 93, attribute; 94, argument_list; 95, call; 96, integer:1; 97, identifier:all_feature_names; 98, list; 99, expression_statement; 100, expression_statement; 101, identifier:shared_feature_names; 102, call; 103, identifier:all_feature_names; 104, slice; 105, expression_statement; 106, identifier:shared_feature_names; 107, call; 108, identifier:shared_features; 109, list; 110, identifier:range; 111, argument_list; 112, expression_statement; 113, identifier:new_root_id; 114, binary_operator:max(self._all_ids)+1; 115, attribute; 116, argument_list; 117, identifier:new_root; 118, call; 119, identifier:all_spike_trains; 120, list; 121, expression_statement; 122, expression_statement; 123, expression_statement; 124, for_statement; 125, delete_statement; 126, expression_statement; 127, comment:#clear spiketrain; 128, expression_statement; 129, identifier:all_spike_trains; 130, call; 131, identifier:sort_indices; 132, call; 133, attribute; 134, argument_list; 135, attribute; 136, list_comprehension; 137, attribute; 138, argument_list; 139, identifier:i; 140, identifier:feature_name; 141, identifier:enumerate; 142, argument_list; 143, expression_statement; 144, call; 145, attribute; 146, subscript; 147, identifier:unit_id; 148, identifier:root_ids; 149, identifier:append; 150, identifier:root_id; 151, call; 152, identifier:issubset; 153, call; 154, identifier:len; 155, argument_list; 156, assignment; 157, call; 158, identifier:set; 159, argument_list; 160, integer:1; 161, call; 162, identifier:list; 163, argument_list; 164, call; 165, call; 166, call; 167, integer:1; 168, attribute; 169, identifier:append; 170, identifier:new_root_id; 171, identifier:Unit; 172, argument_list; 173, assignment; 174, call; 175, call; 176, pattern_list; 177, call; 178, block; 179, subscript; 180, call; 181, call; 182, attribute; 183, argument_list; 184, attribute; 185, argument_list; 186, identifier:new_root; 187, identifier:set_spike_train; 188, subscript; 189, identifier:self; 190, identifier:_roots; 191, subscript; 192, for_in_clause; 193, if_clause; 194, attribute; 195, identifier:append; 196, identifier:new_root; 197, identifier:shared_feature_names; 198, call; 199, identifier:ValueError; 200, argument_list; 201, identifier:self; 202, identifier:_roots; 203, attribute; 204, identifier:i; 205, identifier:set; 206, argument_list; 207, identifier:set; 208, argument_list; 209, identifier:unit_ids; 210, identifier:feature_names; 211, call; 212, attribute; 213, argument_list; 214, subscript; 215, attribute; 216, argument_list; 217, identifier:shared_feature_names; 218, identifier:len; 219, argument_list; 220, attribute; 221, argument_list; 222, identifier:max; 223, argument_list; 224, identifier:self; 225, identifier:_all_ids; 226, identifier:new_root_id; 227, identifier:root_index; 228, call; 229, attribute; 230, argument_list; 231, attribute; 232, argument_list; 233, identifier:i; 234, identifier:feature_name; 235, identifier:enumerate; 236, argument_list; 237, expression_statement; 238, expression_statement; 239, attribute; 240, identifier:unit_id; 241, attribute; 242, argument_list; 243, attribute; 244, argument_list; 245, identifier:np; 246, identifier:concatenate; 247, identifier:all_spike_trains; 248, identifier:np; 249, identifier:argsort; 250, identifier:all_spike_trains; 251, call; 252, identifier:sort_indices; 253, attribute; 254, identifier:i; 255, pattern_list; 256, call; 257, comparison_operator:i not in indices_to_be_deleted; 258, identifier:self; 259, identifier:_roots; 260, attribute; 261, argument_list; 262, binary_operator:str(unit_ids) + " has one or more invalid unit ids"; 263, identifier:self; 264, identifier:_roots; 265, identifier:unit_ids; 266, identifier:root_ids; 267, attribute; 268, argument_list; 269, identifier:all_feature_names; 270, identifier:append; 271, identifier:feature_names; 272, identifier:all_feature_names; 273, integer:0; 274, identifier:shared_feature_names; 275, identifier:intersection_update; 276, identifier:feature_names; 277, identifier:shared_feature_names; 278, identifier:shared_features; 279, identifier:append; 280, list; 281, attribute; 282, attribute; 283, argument_list; 284, identifier:new_root; 285, identifier:add_child; 286, subscript; 287, identifier:all_spike_trains; 288, identifier:append; 289, call; 290, identifier:shared_feature_names; 291, assignment; 292, call; 293, identifier:self; 294, identifier:_unit_features; 295, subscript; 296, identifier:set_spike_train; 297, call; 298, identifier:indices_to_be_deleted; 299, identifier:append; 300, identifier:root_index; 301, attribute; 302, argument_list; 303, identifier:self; 304, identifier:_roots; 305, identifier:i; 306, identifier:_; 307, identifier:enumerate; 308, argument_list; 309, identifier:i; 310, identifier:indices_to_be_deleted; 311, identifier:self; 312, identifier:set_unit_spike_features; 313, identifier:new_root_id; 314, identifier:feature_name; 315, subscript; 316, call; 317, string:" has one or more invalid unit ids"; 318, identifier:self; 319, identifier:get_unit_spike_feature_names; 320, identifier:unit_id; 321, identifier:self; 322, identifier:_all_ids; 323, identifier:root_ids; 324, identifier:index; 325, identifier:unit_id; 326, attribute; 327, identifier:root_index; 328, attribute; 329, argument_list; 330, identifier:features; 331, call; 332, attribute; 333, argument_list; 334, attribute; 335, identifier:root_index; 336, attribute; 337, argument_list; 338, identifier:np; 339, identifier:asarray; 340, identifier:all_spike_trains; 341, identifier:root_ids; 342, call; 343, identifier:sort_indices; 344, identifier:str; 345, argument_list; 346, identifier:self; 347, identifier:_roots; 348, subscript; 349, identifier:get_spike_train; 350, attribute; 351, argument_list; 352, subscript; 353, identifier:append; 354, identifier:features; 355, identifier:self; 356, identifier:_roots; 357, identifier:np; 358, identifier:asarray; 359, list; 360, attribute; 361, argument_list; 362, identifier:unit_ids; 363, attribute; 364, identifier:root_index; 365, identifier:self; 366, identifier:get_unit_spike_features; 367, identifier:unit_id; 368, identifier:feature_name; 369, identifier:shared_features; 370, identifier:i; 371, identifier:np; 372, identifier:concatenate; 373, subscript; 374, identifier:self; 375, identifier:_roots; 376, identifier:shared_features; 377, identifier:i | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 7, 12; 8, 13; 9, 14; 9, 15; 9, 16; 10, 17; 11, 18; 11, 19; 11, 20; 11, 21; 12, 22; 13, 23; 13, 24; 15, 25; 15, 26; 16, 27; 16, 28; 17, 29; 17, 30; 18, 31; 20, 32; 20, 33; 20, 34; 20, 35; 20, 36; 20, 37; 20, 38; 20, 39; 20, 40; 20, 41; 20, 42; 20, 43; 20, 44; 20, 45; 20, 46; 20, 47; 20, 48; 20, 49; 20, 50; 21, 51; 26, 52; 27, 53; 28, 54; 31, 55; 31, 56; 32, 57; 33, 58; 33, 59; 33, 60; 34, 61; 35, 62; 35, 63; 35, 64; 36, 65; 37, 66; 38, 67; 38, 68; 38, 69; 39, 70; 40, 71; 41, 72; 42, 73; 43, 74; 43, 75; 43, 76; 44, 77; 45, 78; 46, 79; 47, 80; 48, 81; 49, 82; 50, 83; 50, 84; 50, 85; 51, 86; 52, 87; 52, 88; 53, 89; 53, 90; 54, 91; 54, 92; 55, 93; 55, 94; 56, 95; 56, 96; 57, 97; 57, 98; 60, 99; 60, 100; 61, 101; 61, 102; 63, 103; 63, 104; 64, 105; 65, 106; 65, 107; 66, 108; 66, 109; 68, 110; 68, 111; 69, 112; 70, 113; 70, 114; 71, 115; 71, 116; 72, 117; 72, 118; 73, 119; 73, 120; 76, 121; 76, 122; 76, 123; 76, 124; 76, 125; 76, 126; 76, 127; 76, 128; 77, 129; 77, 130; 78, 131; 78, 132; 79, 133; 79, 134; 81, 135; 81, 136; 82, 137; 82, 138; 83, 139; 83, 140; 84, 141; 84, 142; 85, 143; 86, 144; 88, 145; 90, 146; 90, 147; 91, 148; 91, 149; 92, 150; 93, 151; 93, 152; 94, 153; 95, 154; 95, 155; 99, 156; 100, 157; 102, 158; 102, 159; 104, 160; 105, 161; 107, 162; 107, 163; 111, 164; 112, 165; 114, 166; 114, 167; 115, 168; 115, 169; 116, 170; 118, 171; 118, 172; 121, 173; 122, 174; 123, 175; 124, 176; 124, 177; 124, 178; 125, 179; 126, 180; 128, 181; 130, 182; 130, 183; 132, 184; 132, 185; 133, 186; 133, 187; 134, 188; 135, 189; 135, 190; 136, 191; 136, 192; 136, 193; 137, 194; 137, 195; 138, 196; 142, 197; 143, 198; 144, 199; 144, 200; 145, 201; 145, 202; 146, 203; 146, 204; 151, 205; 151, 206; 153, 207; 153, 208; 155, 209; 156, 210; 156, 211; 157, 212; 157, 213; 159, 214; 161, 215; 161, 216; 163, 217; 164, 218; 164, 219; 165, 220; 165, 221; 166, 222; 166, 223; 168, 224; 168, 225; 172, 226; 173, 227; 173, 228; 174, 229; 174, 230; 175, 231; 175, 232; 176, 233; 176, 234; 177, 235; 177, 236; 178, 237; 178, 238; 179, 239; 179, 240; 180, 241; 180, 242; 181, 243; 181, 244; 182, 245; 182, 246; 183, 247; 184, 248; 184, 249; 185, 250; 188, 251; 188, 252; 191, 253; 191, 254; 192, 255; 192, 256; 193, 257; 194, 258; 194, 259; 198, 260; 198, 261; 200, 262; 203, 263; 203, 264; 206, 265; 208, 266; 211, 267; 211, 268; 212, 269; 212, 270; 213, 271; 214, 272; 214, 273; 215, 274; 215, 275; 216, 276; 219, 277; 220, 278; 220, 279; 221, 280; 223, 281; 228, 282; 228, 283; 229, 284; 229, 285; 230, 286; 231, 287; 231, 288; 232, 289; 236, 290; 237, 291; 238, 292; 239, 293; 239, 294; 241, 295; 241, 296; 242, 297; 243, 298; 243, 299; 244, 300; 251, 301; 251, 302; 253, 303; 253, 304; 255, 305; 255, 306; 256, 307; 256, 308; 257, 309; 257, 310; 260, 311; 260, 312; 261, 313; 261, 314; 261, 315; 262, 316; 262, 317; 267, 318; 267, 319; 268, 320; 281, 321; 281, 322; 282, 323; 282, 324; 283, 325; 286, 326; 286, 327; 289, 328; 289, 329; 291, 330; 291, 331; 292, 332; 292, 333; 295, 334; 295, 335; 297, 336; 297, 337; 301, 338; 301, 339; 302, 340; 308, 341; 315, 342; 315, 343; 316, 344; 316, 345; 326, 346; 326, 347; 328, 348; 328, 349; 331, 350; 331, 351; 332, 352; 332, 353; 333, 354; 334, 355; 334, 356; 336, 357; 336, 358; 337, 359; 342, 360; 342, 361; 345, 362; 348, 363; 348, 364; 350, 365; 350, 366; 351, 367; 351, 368; 352, 369; 352, 370; 360, 371; 360, 372; 361, 373; 363, 374; 363, 375; 373, 376; 373, 377 | def merge_units(self, unit_ids):
'''This function merges two roots from the curation tree according to the given unit_ids. It creates a new unit_id and root
that has the merged roots as children.
Parameters
----------
unit_ids: list
The unit ids to be merged
'''
root_ids = []
for i in range(len(self._roots)):
root_id = self._roots[i].unit_id
root_ids.append(root_id)
indices_to_be_deleted = []
if(set(unit_ids).issubset(set(root_ids)) and len(unit_ids) > 1):
#Find all unique feature names and create all feature lists
all_feature_names = []
for unit_id in unit_ids:
feature_names = self.get_unit_spike_feature_names(unit_id)
all_feature_names.append(feature_names)
shared_feature_names = set(all_feature_names[0])
for feature_names in all_feature_names[1:]:
shared_feature_names.intersection_update(feature_names)
shared_feature_names = list(shared_feature_names)
shared_features = []
for i in range(len(shared_feature_names)):
shared_features.append([])
new_root_id = max(self._all_ids)+1
self._all_ids.append(new_root_id)
new_root = Unit(new_root_id)
all_spike_trains = []
for unit_id in unit_ids:
root_index = root_ids.index(unit_id)
new_root.add_child(self._roots[root_index])
all_spike_trains.append(self._roots[root_index].get_spike_train())
for i, feature_name in enumerate(shared_feature_names):
features = self.get_unit_spike_features(unit_id, feature_name)
shared_features[i].append(features)
del self._unit_features[unit_id]
self._roots[root_index].set_spike_train(np.asarray([])) #clear spiketrain
indices_to_be_deleted.append(root_index)
all_spike_trains = np.concatenate(all_spike_trains)
sort_indices = np.argsort(all_spike_trains)
new_root.set_spike_train(np.asarray(all_spike_trains)[sort_indices])
del all_spike_trains
self._roots = [self._roots[i] for i,_ in enumerate(root_ids) if i not in indices_to_be_deleted]
self._roots.append(new_root)
for i, feature_name in enumerate(shared_feature_names):
self.set_unit_spike_features(new_root_id, feature_name, np.concatenate(shared_features[i])[sort_indices])
else:
raise ValueError(str(unit_ids) + " has one or more invalid unit ids") |
0, module; 1, function_definition; 2, function_name:split_unit; 3, parameters; 4, block; 5, identifier:self; 6, identifier:unit_id; 7, identifier:indices; 8, expression_statement; 9, expression_statement; 10, for_statement; 11, if_statement; 12, string; 13, assignment; 14, identifier:i; 15, call; 16, block; 17, parenthesized_expression; 18, block; 19, else_clause; 20, string_content:This function splits a root from the curation tree according to the given unit_id and indices. It creates two new unit_ids
and roots that have the split root as a child. This function splits the spike train of the root by the given indices.
Parameters
----------
unit_id: int
The unit id to be split
indices: list
The indices of the unit spike train at which the spike train will be split.; 21, identifier:root_ids; 22, list; 23, identifier:range; 24, argument_list; 25, expression_statement; 26, expression_statement; 27, comparison_operator:unit_id in root_ids; 28, expression_statement; 29, expression_statement; 30, expression_statement; 31, expression_statement; 32, try_statement; 33, expression_statement; 34, expression_statement; 35, delete_statement; 36, expression_statement; 37, expression_statement; 38, expression_statement; 39, expression_statement; 40, expression_statement; 41, expression_statement; 42, expression_statement; 43, expression_statement; 44, expression_statement; 45, expression_statement; 46, expression_statement; 47, expression_statement; 48, for_statement; 49, delete_statement; 50, delete_statement; 51, block; 52, call; 53, assignment; 54, call; 55, identifier:unit_id; 56, identifier:root_ids; 57, assignment; 58, assignment; 59, assignment; 60, assignment; 61, block; 62, except_clause; 63, assignment; 64, assignment; 65, identifier:original_spike_train; 66, assignment; 67, call; 68, assignment; 69, call; 70, call; 71, assignment; 72, call; 73, assignment; 74, call; 75, call; 76, call; 77, call; 78, identifier:feature_name; 79, call; 80, block; 81, subscript; 82, subscript; 83, raise_statement; 84, identifier:len; 85, argument_list; 86, identifier:root_id; 87, attribute; 88, attribute; 89, argument_list; 90, identifier:indices_1; 91, call; 92, identifier:root_index; 93, call; 94, identifier:new_child; 95, subscript; 96, identifier:original_spike_train; 97, call; 98, expression_statement; 99, identifier:IndexError; 100, block; 101, identifier:indices_2; 102, call; 103, identifier:spike_train_2; 104, subscript; 105, identifier:new_root_1_id; 106, binary_operator:max(self._all_ids)+1; 107, attribute; 108, argument_list; 109, identifier:new_root_1; 110, call; 111, attribute; 112, argument_list; 113, attribute; 114, argument_list; 115, identifier:new_root_2_id; 116, binary_operator:max(self._all_ids)+1; 117, attribute; 118, argument_list; 119, identifier:new_root_2; 120, call; 121, attribute; 122, argument_list; 123, attribute; 124, argument_list; 125, attribute; 126, argument_list; 127, attribute; 128, argument_list; 129, attribute; 130, argument_list; 131, expression_statement; 132, expression_statement; 133, expression_statement; 134, attribute; 135, identifier:unit_id; 136, attribute; 137, identifier:root_index; 138, call; 139, attribute; 140, subscript; 141, identifier:unit_id; 142, identifier:root_ids; 143, identifier:append; 144, identifier:root_id; 145, attribute; 146, argument_list; 147, attribute; 148, argument_list; 149, attribute; 150, identifier:root_index; 151, attribute; 152, argument_list; 153, assignment; 154, expression_statement; 155, identifier:list; 156, argument_list; 157, identifier:original_spike_train; 158, identifier:indices_2; 159, call; 160, integer:1; 161, attribute; 162, identifier:append; 163, identifier:new_root_1_id; 164, identifier:Unit; 165, argument_list; 166, identifier:new_root_1; 167, identifier:add_child; 168, identifier:new_child; 169, identifier:new_root_1; 170, identifier:set_spike_train; 171, identifier:spike_train_1; 172, call; 173, integer:1; 174, attribute; 175, identifier:append; 176, identifier:new_root_2_id; 177, identifier:Unit; 178, argument_list; 179, identifier:new_root_2; 180, identifier:add_child; 181, identifier:new_child; 182, identifier:new_root_2; 183, identifier:set_spike_train; 184, identifier:spike_train_2; 185, attribute; 186, identifier:append; 187, identifier:new_root_1; 188, attribute; 189, identifier:append; 190, identifier:new_root_2; 191, identifier:self; 192, identifier:get_unit_spike_feature_names; 193, identifier:unit_id; 194, assignment; 195, call; 196, call; 197, identifier:self; 198, identifier:_unit_features; 199, identifier:self; 200, identifier:_roots; 201, identifier:ValueError; 202, argument_list; 203, identifier:self; 204, identifier:_roots; 205, attribute; 206, identifier:i; 207, identifier:np; 208, identifier:sort; 209, call; 210, identifier:root_ids; 211, identifier:index; 212, identifier:unit_id; 213, identifier:self; 214, identifier:_roots; 215, subscript; 216, identifier:get_spike_train; 217, identifier:spike_train_1; 218, subscript; 219, call; 220, binary_operator:set(range(len(original_spike_train))) - set(indices_1); 221, identifier:max; 222, argument_list; 223, identifier:self; 224, identifier:_all_ids; 225, identifier:new_root_1_id; 226, identifier:max; 227, argument_list; 228, identifier:self; 229, identifier:_all_ids; 230, identifier:new_root_2_id; 231, identifier:self; 232, identifier:_roots; 233, identifier:self; 234, identifier:_roots; 235, identifier:full_features; 236, call; 237, attribute; 238, argument_list; 239, attribute; 240, argument_list; 241, binary_operator:str(unit_id) + " non-valid unit id"; 242, identifier:self; 243, identifier:_roots; 244, attribute; 245, argument_list; 246, attribute; 247, identifier:root_index; 248, identifier:original_spike_train; 249, identifier:indices_1; 250, identifier:print; 251, argument_list; 252, call; 253, call; 254, attribute; 255, attribute; 256, attribute; 257, argument_list; 258, identifier:self; 259, identifier:set_unit_spike_features; 260, identifier:new_root_1_id; 261, identifier:feature_name; 262, subscript; 263, identifier:self; 264, identifier:set_unit_spike_features; 265, identifier:new_root_2_id; 266, identifier:feature_name; 267, subscript; 268, call; 269, string:" non-valid unit id"; 270, identifier:np; 271, identifier:asarray; 272, call; 273, identifier:self; 274, identifier:_roots; 275, binary_operator:str(indices) + " out of bounds for the spike train of " + str(unit_id); 276, identifier:set; 277, argument_list; 278, identifier:set; 279, argument_list; 280, identifier:self; 281, identifier:_all_ids; 282, identifier:self; 283, identifier:_all_ids; 284, identifier:self; 285, identifier:get_unit_spike_features; 286, identifier:unit_id; 287, identifier:feature_name; 288, identifier:full_features; 289, identifier:indices_1; 290, identifier:full_features; 291, identifier:indices_2; 292, identifier:str; 293, argument_list; 294, identifier:list; 295, argument_list; 296, binary_operator:str(indices) + " out of bounds for the spike train of "; 297, call; 298, call; 299, identifier:indices_1; 300, identifier:unit_id; 301, call; 302, call; 303, string:" out of bounds for the spike train of "; 304, identifier:str; 305, argument_list; 306, identifier:range; 307, argument_list; 308, identifier:set; 309, argument_list; 310, identifier:str; 311, argument_list; 312, identifier:unit_id; 313, call; 314, identifier:indices; 315, identifier:indices; 316, identifier:len; 317, argument_list; 318, identifier:original_spike_train | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 8, 12; 9, 13; 10, 14; 10, 15; 10, 16; 11, 17; 11, 18; 11, 19; 12, 20; 13, 21; 13, 22; 15, 23; 15, 24; 16, 25; 16, 26; 17, 27; 18, 28; 18, 29; 18, 30; 18, 31; 18, 32; 18, 33; 18, 34; 18, 35; 18, 36; 18, 37; 18, 38; 18, 39; 18, 40; 18, 41; 18, 42; 18, 43; 18, 44; 18, 45; 18, 46; 18, 47; 18, 48; 18, 49; 18, 50; 19, 51; 24, 52; 25, 53; 26, 54; 27, 55; 27, 56; 28, 57; 29, 58; 30, 59; 31, 60; 32, 61; 32, 62; 33, 63; 34, 64; 35, 65; 36, 66; 37, 67; 38, 68; 39, 69; 40, 70; 41, 71; 42, 72; 43, 73; 44, 74; 45, 75; 46, 76; 47, 77; 48, 78; 48, 79; 48, 80; 49, 81; 50, 82; 51, 83; 52, 84; 52, 85; 53, 86; 53, 87; 54, 88; 54, 89; 57, 90; 57, 91; 58, 92; 58, 93; 59, 94; 59, 95; 60, 96; 60, 97; 61, 98; 62, 99; 62, 100; 63, 101; 63, 102; 64, 103; 64, 104; 66, 105; 66, 106; 67, 107; 67, 108; 68, 109; 68, 110; 69, 111; 69, 112; 70, 113; 70, 114; 71, 115; 71, 116; 72, 117; 72, 118; 73, 119; 73, 120; 74, 121; 74, 122; 75, 123; 75, 124; 76, 125; 76, 126; 77, 127; 77, 128; 79, 129; 79, 130; 80, 131; 80, 132; 80, 133; 81, 134; 81, 135; 82, 136; 82, 137; 83, 138; 85, 139; 87, 140; 87, 141; 88, 142; 88, 143; 89, 144; 91, 145; 91, 146; 93, 147; 93, 148; 95, 149; 95, 150; 97, 151; 97, 152; 98, 153; 100, 154; 102, 155; 102, 156; 104, 157; 104, 158; 106, 159; 106, 160; 107, 161; 107, 162; 108, 163; 110, 164; 110, 165; 111, 166; 111, 167; 112, 168; 113, 169; 113, 170; 114, 171; 116, 172; 116, 173; 117, 174; 117, 175; 118, 176; 120, 177; 120, 178; 121, 179; 121, 180; 122, 181; 123, 182; 123, 183; 124, 184; 125, 185; 125, 186; 126, 187; 127, 188; 127, 189; 128, 190; 129, 191; 129, 192; 130, 193; 131, 194; 132, 195; 133, 196; 134, 197; 134, 198; 136, 199; 136, 200; 138, 201; 138, 202; 139, 203; 139, 204; 140, 205; 140, 206; 145, 207; 145, 208; 146, 209; 147, 210; 147, 211; 148, 212; 149, 213; 149, 214; 151, 215; 151, 216; 153, 217; 153, 218; 154, 219; 156, 220; 159, 221; 159, 222; 161, 223; 161, 224; 165, 225; 172, 226; 172, 227; 174, 228; 174, 229; 178, 230; 185, 231; 185, 232; 188, 233; 188, 234; 194, 235; 194, 236; 195, 237; 195, 238; 196, 239; 196, 240; 202, 241; 205, 242; 205, 243; 209, 244; 209, 245; 215, 246; 215, 247; 218, 248; 218, 249; 219, 250; 219, 251; 220, 252; 220, 253; 222, 254; 227, 255; 236, 256; 236, 257; 237, 258; 237, 259; 238, 260; 238, 261; 238, 262; 239, 263; 239, 264; 240, 265; 240, 266; 240, 267; 241, 268; 241, 269; 244, 270; 244, 271; 245, 272; 246, 273; 246, 274; 251, 275; 252, 276; 252, 277; 253, 278; 253, 279; 254, 280; 254, 281; 255, 282; 255, 283; 256, 284; 256, 285; 257, 286; 257, 287; 262, 288; 262, 289; 267, 290; 267, 291; 268, 292; 268, 293; 272, 294; 272, 295; 275, 296; 275, 297; 277, 298; 279, 299; 293, 300; 295, 301; 296, 302; 296, 303; 297, 304; 297, 305; 298, 306; 298, 307; 301, 308; 301, 309; 302, 310; 302, 311; 305, 312; 307, 313; 309, 314; 311, 315; 313, 316; 313, 317; 317, 318 | def split_unit(self, unit_id, indices):
'''This function splits a root from the curation tree according to the given unit_id and indices. It creates two new unit_ids
and roots that have the split root as a child. This function splits the spike train of the root by the given indices.
Parameters
----------
unit_id: int
The unit id to be split
indices: list
The indices of the unit spike train at which the spike train will be split.
'''
root_ids = []
for i in range(len(self._roots)):
root_id = self._roots[i].unit_id
root_ids.append(root_id)
if(unit_id in root_ids):
indices_1 = np.sort(np.asarray(list(set(indices))))
root_index = root_ids.index(unit_id)
new_child = self._roots[root_index]
original_spike_train = self._roots[root_index].get_spike_train()
try:
spike_train_1 = original_spike_train[indices_1]
except IndexError:
print(str(indices) + " out of bounds for the spike train of " + str(unit_id))
indices_2 = list(set(range(len(original_spike_train))) - set(indices_1))
spike_train_2 = original_spike_train[indices_2]
del original_spike_train
new_root_1_id = max(self._all_ids)+1
self._all_ids.append(new_root_1_id)
new_root_1 = Unit(new_root_1_id)
new_root_1.add_child(new_child)
new_root_1.set_spike_train(spike_train_1)
new_root_2_id = max(self._all_ids)+1
self._all_ids.append(new_root_2_id)
new_root_2 = Unit(new_root_2_id)
new_root_2.add_child(new_child)
new_root_2.set_spike_train(spike_train_2)
self._roots.append(new_root_1)
self._roots.append(new_root_2)
for feature_name in self.get_unit_spike_feature_names(unit_id):
full_features = self.get_unit_spike_features(unit_id, feature_name)
self.set_unit_spike_features(new_root_1_id, feature_name, full_features[indices_1])
self.set_unit_spike_features(new_root_2_id, feature_name, full_features[indices_2])
del self._unit_features[unit_id]
del self._roots[root_index]
else:
raise ValueError(str(unit_id) + " non-valid unit id") |
0, module; 1, function_definition; 2, function_name:_find_best_fit; 3, parameters; 4, block; 5, identifier:self; 6, identifier:pbin; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, try_statement; 11, comment:"""
Return best fitness rectangle from rectangles packing _sorted_rect list
Arguments:
pbin (PackingAlgorithm): Packing bin
Returns:
key of the rectangle with best fitness
"""; 12, assignment; 13, assignment; 14, block; 15, except_clause; 16, identifier:fit; 17, generator_expression; 18, identifier:fit; 19, generator_expression; 20, expression_statement; 21, return_statement; 22, identifier:ValueError; 23, block; 24, tuple; 25, for_in_clause; 26, identifier:f; 27, for_in_clause; 28, if_clause; 29, assignment; 30, identifier:rect; 31, return_statement; 32, call; 33, identifier:k; 34, pattern_list; 35, call; 36, identifier:f; 37, identifier:fit; 38, comparison_operator:f[0] is not None; 39, pattern_list; 40, call; 41, None; 42, attribute; 43, argument_list; 44, identifier:k; 45, identifier:r; 46, attribute; 47, argument_list; 48, subscript; 49, None; 50, identifier:_; 51, identifier:rect; 52, identifier:min; 53, argument_list; 54, identifier:pbin; 55, identifier:fitness; 56, subscript; 57, subscript; 58, attribute; 59, identifier:items; 60, identifier:f; 61, integer:0; 62, identifier:fit; 63, keyword_argument; 64, identifier:r; 65, integer:0; 66, identifier:r; 67, integer:1; 68, identifier:self; 69, identifier:_sorted_rect; 70, identifier:key; 71, attribute; 72, identifier:self; 73, identifier:first_item | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 7, 11; 8, 12; 9, 13; 10, 14; 10, 15; 12, 16; 12, 17; 13, 18; 13, 19; 14, 20; 14, 21; 15, 22; 15, 23; 17, 24; 17, 25; 19, 26; 19, 27; 19, 28; 20, 29; 21, 30; 23, 31; 24, 32; 24, 33; 25, 34; 25, 35; 27, 36; 27, 37; 28, 38; 29, 39; 29, 40; 31, 41; 32, 42; 32, 43; 34, 44; 34, 45; 35, 46; 35, 47; 38, 48; 38, 49; 39, 50; 39, 51; 40, 52; 40, 53; 42, 54; 42, 55; 43, 56; 43, 57; 46, 58; 46, 59; 48, 60; 48, 61; 53, 62; 53, 63; 56, 64; 56, 65; 57, 66; 57, 67; 58, 68; 58, 69; 63, 70; 63, 71; 71, 72; 71, 73 | def _find_best_fit(self, pbin):
"""
Return best fitness rectangle from rectangles packing _sorted_rect list
Arguments:
pbin (PackingAlgorithm): Packing bin
Returns:
key of the rectangle with best fitness
"""
fit = ((pbin.fitness(r[0], r[1]), k) for k, r in self._sorted_rect.items())
fit = (f for f in fit if f[0] is not None)
try:
_, rect = min(fit, key=self.first_item)
return rect
except ValueError:
return None |
0, module; 1, function_definition; 2, function_name:_item_attributes_match; 3, parameters; 4, comment:# type: (CryptoConfig, Dict, Dict) -> Bool; 5, block; 6, identifier:crypto_config; 7, identifier:plaintext_item; 8, identifier:encrypted_item; 9, expression_statement; 10, for_statement; 11, return_statement; 12, comment:"""Determines whether the unencrypted values in the plaintext items attributes are the same as those in the
encrypted item. Essentially this uses brute force to cover when we don't know the primary and sort
index attribute names, since they can't be encrypted.
:param CryptoConfig crypto_config: CryptoConfig used in encrypting the given items
:param dict plaintext_item: The plaintext item
:param dict encrypted_item: The encrypted item
:return: Bool response, True if the unencrypted attributes in the plaintext item match those in
the encrypted item
:rtype: bool
"""; 13, pattern_list; 14, call; 15, block; 16, True; 17, identifier:name; 18, identifier:value; 19, attribute; 20, argument_list; 21, if_statement; 22, if_statement; 23, identifier:plaintext_item; 24, identifier:items; 25, comparison_operator:crypto_config.attribute_actions.action(name) == CryptoAction.ENCRYPT_AND_SIGN; 26, block; 27, comparison_operator:encrypted_item.get(name) != value; 28, block; 29, call; 30, attribute; 31, continue_statement; 32, call; 33, identifier:value; 34, return_statement; 35, attribute; 36, argument_list; 37, identifier:CryptoAction; 38, identifier:ENCRYPT_AND_SIGN; 39, attribute; 40, argument_list; 41, False; 42, attribute; 43, identifier:action; 44, identifier:name; 45, identifier:encrypted_item; 46, identifier:get; 47, identifier:name; 48, identifier:crypto_config; 49, identifier:attribute_actions | 0, 1; 1, 2; 1, 3; 1, 4; 1, 5; 3, 6; 3, 7; 3, 8; 5, 9; 5, 10; 5, 11; 9, 12; 10, 13; 10, 14; 10, 15; 11, 16; 13, 17; 13, 18; 14, 19; 14, 20; 15, 21; 15, 22; 19, 23; 19, 24; 21, 25; 21, 26; 22, 27; 22, 28; 25, 29; 25, 30; 26, 31; 27, 32; 27, 33; 28, 34; 29, 35; 29, 36; 30, 37; 30, 38; 32, 39; 32, 40; 34, 41; 35, 42; 35, 43; 36, 44; 39, 45; 39, 46; 40, 47; 42, 48; 42, 49 | def _item_attributes_match(crypto_config, plaintext_item, encrypted_item):
# type: (CryptoConfig, Dict, Dict) -> Bool
"""Determines whether the unencrypted values in the plaintext items attributes are the same as those in the
encrypted item. Essentially this uses brute force to cover when we don't know the primary and sort
index attribute names, since they can't be encrypted.
:param CryptoConfig crypto_config: CryptoConfig used in encrypting the given items
:param dict plaintext_item: The plaintext item
:param dict encrypted_item: The encrypted item
:return: Bool response, True if the unencrypted attributes in the plaintext item match those in
the encrypted item
:rtype: bool
"""
for name, value in plaintext_item.items():
if crypto_config.attribute_actions.action(name) == CryptoAction.ENCRYPT_AND_SIGN:
continue
if encrypted_item.get(name) != value:
return False
return True |
0, module; 1, function_definition; 2, function_name:load_css; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, for_statement; 13, expression_statement; 14, comment:# Remove common prefix; 15, if_statement; 16, expression_statement; 17, return_statement; 18, comment:"""
Creates a dict of all icons available in CSS file, and finds out
what's their common prefix.
:returns sorted icons dict, common icon prefix
"""; 19, assignment; 20, assignment; 21, assignment; 22, assignment; 23, assignment; 24, identifier:rule; 25, attribute; 26, block; 27, assignment; 28, boolean_operator; 29, block; 30, assignment; 31, expression_list; 32, identifier:icons; 33, call; 34, identifier:common_prefix; 35, None; 36, identifier:parser; 37, call; 38, identifier:stylesheet; 39, call; 40, identifier:is_icon; 41, call; 42, identifier:stylesheet; 43, identifier:rules; 44, expression_statement; 45, comment:# Skip CSS classes that are not icons; 46, if_statement; 47, comment:# Find out what the common prefix is; 48, if_statement; 49, for_statement; 50, identifier:common_prefix; 51, boolean_operator; 52, not_operator; 53, comparison_operator:len(common_prefix) > 0; 54, expression_statement; 55, for_statement; 56, expression_statement; 57, identifier:sorted_icons; 58, call; 59, identifier:sorted_icons; 60, identifier:common_prefix; 61, identifier:dict; 62, argument_list; 63, attribute; 64, argument_list; 65, attribute; 66, argument_list; 67, attribute; 68, argument_list; 69, assignment; 70, not_operator; 71, block; 72, comparison_operator:common_prefix is None; 73, block; 74, else_clause; 75, identifier:match; 76, call; 77, block; 78, identifier:common_prefix; 79, string; 80, attribute; 81, call; 82, integer:0; 83, assignment; 84, identifier:name; 85, call; 86, block; 87, assignment; 88, identifier:OrderedDict; 89, argument_list; 90, identifier:tinycss; 91, identifier:make_parser; 92, string; 93, identifier:parser; 94, identifier:parse_stylesheet_file; 95, attribute; 96, identifier:re; 97, identifier:compile; 98, string:"\.(.*):before,?"; 99, identifier:selector; 100, call; 101, call; 102, continue_statement; 103, identifier:common_prefix; 104, None; 105, expression_statement; 106, block; 107, attribute; 108, argument_list; 109, expression_statement; 110, for_statement; 111, identifier:self; 112, identifier:keep_prefix; 113, identifier:len; 114, argument_list; 115, identifier:non_prefixed_icons; 116, dictionary; 117, attribute; 118, argument_list; 119, expression_statement; 120, identifier:icons; 121, identifier:non_prefixed_icons; 122, call; 123, string_content:page3; 124, identifier:self; 125, identifier:css_file; 126, attribute; 127, argument_list; 128, attribute; 129, argument_list; 130, assignment; 131, expression_statement; 132, identifier:is_icon; 133, identifier:finditer; 134, identifier:selector; 135, assignment; 136, identifier:declaration; 137, attribute; 138, block; 139, identifier:common_prefix; 140, identifier:icons; 141, identifier:keys; 142, assignment; 143, identifier:sorted; 144, argument_list; 145, attribute; 146, identifier:as_css; 147, identifier:is_icon; 148, identifier:match; 149, identifier:selector; 150, identifier:common_prefix; 151, subscript; 152, assignment; 153, identifier:name; 154, subscript; 155, identifier:rule; 156, identifier:declarations; 157, if_statement; 158, subscript; 159, subscript; 160, call; 161, keyword_argument; 162, identifier:rule; 163, identifier:selector; 164, identifier:selector; 165, slice; 166, identifier:common_prefix; 167, call; 168, call; 169, integer:0; 170, comparison_operator:declaration.name == "content"; 171, block; 172, identifier:non_prefixed_icons; 173, subscript; 174, identifier:icons; 175, identifier:name; 176, attribute; 177, argument_list; 178, identifier:key; 179, lambda; 180, integer:1; 181, attribute; 182, argument_list; 183, attribute; 184, argument_list; 185, attribute; 186, string:"content"; 187, expression_statement; 188, comment:# Strip quotation marks; 189, if_statement; 190, expression_statement; 191, identifier:name; 192, slice; 193, identifier:icons; 194, identifier:items; 195, lambda_parameters; 196, subscript; 197, attribute; 198, identifier:commonprefix; 199, tuple; 200, identifier:match; 201, identifier:groups; 202, identifier:declaration; 203, identifier:name; 204, assignment; 205, call; 206, block; 207, assignment; 208, call; 209, identifier:t; 210, identifier:t; 211, integer:0; 212, identifier:os; 213, identifier:path; 214, identifier:common_prefix; 215, subscript; 216, identifier:val; 217, call; 218, attribute; 219, argument_list; 220, expression_statement; 221, subscript; 222, call; 223, identifier:len; 224, argument_list; 225, identifier:selector; 226, slice; 227, attribute; 228, argument_list; 229, identifier:re; 230, identifier:match; 231, string:"^['\"].*['\"]$"; 232, identifier:val; 233, assignment; 234, identifier:icons; 235, identifier:name; 236, identifier:unichr; 237, argument_list; 238, identifier:common_prefix; 239, integer:1; 240, attribute; 241, identifier:as_css; 242, identifier:val; 243, subscript; 244, call; 245, identifier:declaration; 246, identifier:value; 247, identifier:val; 248, slice; 249, identifier:int; 250, argument_list; 251, integer:1; 252, unary_operator; 253, subscript; 254, integer:16; 255, integer:1; 256, identifier:val; 257, slice; 258, integer:1 | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 6, 18; 7, 19; 8, 20; 9, 21; 10, 22; 11, 23; 12, 24; 12, 25; 12, 26; 13, 27; 15, 28; 15, 29; 16, 30; 17, 31; 19, 32; 19, 33; 20, 34; 20, 35; 21, 36; 21, 37; 22, 38; 22, 39; 23, 40; 23, 41; 25, 42; 25, 43; 26, 44; 26, 45; 26, 46; 26, 47; 26, 48; 26, 49; 27, 50; 27, 51; 28, 52; 28, 53; 29, 54; 29, 55; 29, 56; 30, 57; 30, 58; 31, 59; 31, 60; 33, 61; 33, 62; 37, 63; 37, 64; 39, 65; 39, 66; 41, 67; 41, 68; 44, 69; 46, 70; 46, 71; 48, 72; 48, 73; 48, 74; 49, 75; 49, 76; 49, 77; 51, 78; 51, 79; 52, 80; 53, 81; 53, 82; 54, 83; 55, 84; 55, 85; 55, 86; 56, 87; 58, 88; 58, 89; 63, 90; 63, 91; 64, 92; 65, 93; 65, 94; 66, 95; 67, 96; 67, 97; 68, 98; 69, 99; 69, 100; 70, 101; 71, 102; 72, 103; 72, 104; 73, 105; 74, 106; 76, 107; 76, 108; 77, 109; 77, 110; 80, 111; 80, 112; 81, 113; 81, 114; 83, 115; 83, 116; 85, 117; 85, 118; 86, 119; 87, 120; 87, 121; 89, 122; 92, 123; 95, 124; 95, 125; 100, 126; 100, 127; 101, 128; 101, 129; 105, 130; 106, 131; 107, 132; 107, 133; 108, 134; 109, 135; 110, 136; 110, 137; 110, 138; 114, 139; 117, 140; 117, 141; 119, 142; 122, 143; 122, 144; 126, 145; 126, 146; 128, 147; 128, 148; 129, 149; 130, 150; 130, 151; 131, 152; 135, 153; 135, 154; 137, 155; 137, 156; 138, 157; 142, 158; 142, 159; 144, 160; 144, 161; 145, 162; 145, 163; 151, 164; 151, 165; 152, 166; 152, 167; 154, 168; 154, 169; 157, 170; 157, 171; 158, 172; 158, 173; 159, 174; 159, 175; 160, 176; 160, 177; 161, 178; 161, 179; 165, 180; 167, 181; 167, 182; 168, 183; 168, 184; 170, 185; 170, 186; 171, 187; 171, 188; 171, 189; 171, 190; 173, 191; 173, 192; 176, 193; 176, 194; 179, 195; 179, 196; 181, 197; 181, 198; 182, 199; 183, 200; 183, 201; 185, 202; 185, 203; 187, 204; 189, 205; 189, 206; 190, 207; 192, 208; 195, 209; 196, 210; 196, 211; 197, 212; 197, 213; 199, 214; 199, 215; 204, 216; 204, 217; 205, 218; 205, 219; 206, 220; 207, 221; 207, 222; 208, 223; 208, 224; 215, 225; 215, 226; 217, 227; 217, 228; 218, 229; 218, 230; 219, 231; 219, 232; 220, 233; 221, 234; 221, 235; 222, 236; 222, 237; 224, 238; 226, 239; 227, 240; 227, 241; 233, 242; 233, 243; 237, 244; 240, 245; 240, 246; 243, 247; 243, 248; 244, 249; 244, 250; 248, 251; 248, 252; 250, 253; 250, 254; 252, 255; 253, 256; 253, 257; 257, 258 | def load_css(self):
"""
Creates a dict of all icons available in CSS file, and finds out
what's their common prefix.
:returns sorted icons dict, common icon prefix
"""
icons = dict()
common_prefix = None
parser = tinycss.make_parser('page3')
stylesheet = parser.parse_stylesheet_file(self.css_file)
is_icon = re.compile("\.(.*):before,?")
for rule in stylesheet.rules:
selector = rule.selector.as_css()
# Skip CSS classes that are not icons
if not is_icon.match(selector):
continue
# Find out what the common prefix is
if common_prefix is None:
common_prefix = selector[1:]
else:
common_prefix = os.path.commonprefix((common_prefix,
selector[1:]))
for match in is_icon.finditer(selector):
name = match.groups()[0]
for declaration in rule.declarations:
if declaration.name == "content":
val = declaration.value.as_css()
# Strip quotation marks
if re.match("^['\"].*['\"]$", val):
val = val[1:-1]
icons[name] = unichr(int(val[1:], 16))
common_prefix = common_prefix or ''
# Remove common prefix
if not self.keep_prefix and len(common_prefix) > 0:
non_prefixed_icons = {}
for name in icons.keys():
non_prefixed_icons[name[len(common_prefix):]] = icons[name]
icons = non_prefixed_icons
sorted_icons = OrderedDict(sorted(icons.items(), key=lambda t: t[0]))
return sorted_icons, common_prefix |
0, module; 1, function_definition; 2, function_name:rules; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, for_statement; 9, comment:# Sort the data in order to get a consistent output; 10, expression_statement; 11, return_statement; 12, comment:"""
Returns a sorted list of firewall rules.
Returns:
list
"""; 13, assignment; 14, identifier:main_row; 15, attribute; 16, block; 17, assignment; 18, identifier:sorted_list; 19, identifier:list_of_rules; 20, list; 21, identifier:self; 22, identifier:dict_rules; 23, if_statement; 24, identifier:sorted_list; 25, call; 26, comparison_operator:'rules' in main_row; 27, block; 28, else_clause; 29, identifier:sorted; 30, argument_list; 31, string; 32, identifier:main_row; 33, for_statement; 34, block; 35, identifier:list_of_rules; 36, keyword_argument; 37, string_content:rules; 38, identifier:rule_row; 39, subscript; 40, block; 41, expression_statement; 42, expression_statement; 43, identifier:key; 44, lambda; 45, identifier:main_row; 46, string; 47, if_statement; 48, assignment; 49, call; 50, lambda_parameters; 51, tuple; 52, string_content:rules; 53, comparison_operator:'grants' in rule_row; 54, block; 55, else_clause; 56, identifier:fr; 57, call; 58, attribute; 59, argument_list; 60, identifier:fr; 61, call; 62, call; 63, call; 64, call; 65, call; 66, call; 67, call; 68, call; 69, call; 70, call; 71, string; 72, identifier:rule_row; 73, for_statement; 74, block; 75, identifier:FirewallRule; 76, argument_list; 77, identifier:list_of_rules; 78, identifier:append; 79, identifier:fr; 80, identifier:str; 81, argument_list; 82, identifier:str; 83, argument_list; 84, identifier:str; 85, argument_list; 86, identifier:str; 87, argument_list; 88, identifier:str; 89, argument_list; 90, identifier:str; 91, argument_list; 92, identifier:str; 93, argument_list; 94, identifier:str; 95, argument_list; 96, identifier:str; 97, argument_list; 98, identifier:str; 99, argument_list; 100, string_content:grants; 101, identifier:grant_row; 102, subscript; 103, block; 104, expression_statement; 105, expression_statement; 106, subscript; 107, subscript; 108, subscript; 109, attribute; 110, attribute; 111, attribute; 112, attribute; 113, attribute; 114, attribute; 115, attribute; 116, attribute; 117, attribute; 118, attribute; 119, identifier:rule_row; 120, string; 121, if_statement; 122, assignment; 123, call; 124, identifier:main_row; 125, string; 126, identifier:main_row; 127, string; 128, identifier:main_row; 129, string; 130, identifier:fr; 131, identifier:id; 132, identifier:fr; 133, identifier:name; 134, identifier:fr; 135, identifier:description; 136, identifier:fr; 137, identifier:rules_direction; 138, identifier:fr; 139, identifier:rules_ip_protocol; 140, identifier:fr; 141, identifier:rules_from_port; 142, identifier:fr; 143, identifier:rules_to_port; 144, identifier:fr; 145, identifier:rules_grants_group_id; 146, identifier:fr; 147, identifier:rules_grants_name; 148, identifier:fr; 149, identifier:rules_grants_cidr_ip; 150, string_content:grants; 151, comparison_operator:'group_id' in grant_row; 152, comment:# Set a var to not go over 80 chars; 153, block; 154, elif_clause; 155, else_clause; 156, identifier:fr; 157, call; 158, attribute; 159, argument_list; 160, string_content:id; 161, string_content:name; 162, string_content:description; 163, string; 164, identifier:grant_row; 165, expression_statement; 166, comment:# Some VPC grants don't specify a name; 167, if_statement; 168, expression_statement; 169, expression_statement; 170, comparison_operator:'cidr_ip' in grant_row; 171, block; 172, block; 173, identifier:FirewallRule; 174, argument_list; 175, identifier:list_of_rules; 176, identifier:append; 177, identifier:fr; 178, string_content:group_id; 179, assignment; 180, comparison_operator:'name' in grant_row; 181, block; 182, else_clause; 183, assignment; 184, call; 185, string; 186, identifier:grant_row; 187, expression_statement; 188, expression_statement; 189, raise_statement; 190, subscript; 191, subscript; 192, subscript; 193, keyword_argument; 194, keyword_argument; 195, keyword_argument; 196, keyword_argument; 197, identifier:group_id; 198, subscript; 199, string; 200, identifier:grant_row; 201, expression_statement; 202, block; 203, identifier:fr; 204, call; 205, attribute; 206, argument_list; 207, string_content:cidr_ip; 208, assignment; 209, call; 210, call; 211, identifier:main_row; 212, string; 213, identifier:main_row; 214, string; 215, identifier:main_row; 216, string; 217, identifier:rules_direction; 218, subscript; 219, identifier:rules_ip_protocol; 220, subscript; 221, identifier:rules_from_port; 222, subscript; 223, identifier:rules_to_port; 224, subscript; 225, identifier:grant_row; 226, string; 227, string_content:name; 228, assignment; 229, expression_statement; 230, identifier:FirewallRule; 231, argument_list; 232, identifier:list_of_rules; 233, identifier:append; 234, identifier:fr; 235, identifier:fr; 236, call; 237, attribute; 238, argument_list; 239, identifier:ValueError; 240, argument_list; 241, string_content:id; 242, string_content:name; 243, string_content:description; 244, identifier:rule_row; 245, string; 246, identifier:rule_row; 247, string; 248, identifier:rule_row; 249, string; 250, identifier:rule_row; 251, string; 252, string_content:group_id; 253, identifier:row_name; 254, subscript; 255, assignment; 256, subscript; 257, subscript; 258, subscript; 259, keyword_argument; 260, keyword_argument; 261, keyword_argument; 262, keyword_argument; 263, keyword_argument; 264, keyword_argument; 265, keyword_argument; 266, identifier:FirewallRule; 267, argument_list; 268, identifier:list_of_rules; 269, identifier:append; 270, identifier:fr; 271, string:"Unsupported grant:"; 272, identifier:grant_row; 273, string_content:direction; 274, string_content:ip_protocol; 275, string_content:from_port; 276, string_content:to_port; 277, identifier:grant_row; 278, string; 279, identifier:row_name; 280, None; 281, identifier:main_row; 282, string; 283, identifier:main_row; 284, string; 285, identifier:main_row; 286, string; 287, identifier:rules_direction; 288, subscript; 289, identifier:rules_ip_protocol; 290, subscript; 291, identifier:rules_from_port; 292, subscript; 293, identifier:rules_to_port; 294, subscript; 295, identifier:rules_grants_group_id; 296, identifier:group_id; 297, identifier:rules_grants_name; 298, identifier:row_name; 299, identifier:rules_description; 300, subscript; 301, subscript; 302, subscript; 303, subscript; 304, keyword_argument; 305, keyword_argument; 306, keyword_argument; 307, keyword_argument; 308, keyword_argument; 309, keyword_argument; 310, string_content:name; 311, string_content:id; 312, string_content:name; 313, string_content:description; 314, identifier:rule_row; 315, string; 316, identifier:rule_row; 317, string; 318, identifier:rule_row; 319, string; 320, identifier:rule_row; 321, string; 322, identifier:grant_row; 323, string; 324, identifier:main_row; 325, string; 326, identifier:main_row; 327, string; 328, identifier:main_row; 329, string; 330, identifier:rules_direction; 331, subscript; 332, identifier:rules_ip_protocol; 333, subscript; 334, identifier:rules_from_port; 335, subscript; 336, identifier:rules_to_port; 337, subscript; 338, identifier:rules_grants_cidr_ip; 339, subscript; 340, identifier:rules_description; 341, subscript; 342, string_content:direction; 343, string_content:ip_protocol; 344, string_content:from_port; 345, string_content:to_port; 346, string_content:description; 347, string_content:id; 348, string_content:name; 349, string_content:description; 350, identifier:rule_row; 351, string; 352, identifier:rule_row; 353, string; 354, identifier:rule_row; 355, string; 356, identifier:rule_row; 357, string; 358, identifier:grant_row; 359, string; 360, identifier:grant_row; 361, string; 362, string_content:direction; 363, string_content:ip_protocol; 364, string_content:from_port; 365, string_content:to_port; 366, string_content:cidr_ip; 367, string_content:description | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 6, 12; 7, 13; 8, 14; 8, 15; 8, 16; 10, 17; 11, 18; 13, 19; 13, 20; 15, 21; 15, 22; 16, 23; 17, 24; 17, 25; 23, 26; 23, 27; 23, 28; 25, 29; 25, 30; 26, 31; 26, 32; 27, 33; 28, 34; 30, 35; 30, 36; 31, 37; 33, 38; 33, 39; 33, 40; 34, 41; 34, 42; 36, 43; 36, 44; 39, 45; 39, 46; 40, 47; 41, 48; 42, 49; 44, 50; 44, 51; 46, 52; 47, 53; 47, 54; 47, 55; 48, 56; 48, 57; 49, 58; 49, 59; 50, 60; 51, 61; 51, 62; 51, 63; 51, 64; 51, 65; 51, 66; 51, 67; 51, 68; 51, 69; 51, 70; 53, 71; 53, 72; 54, 73; 55, 74; 57, 75; 57, 76; 58, 77; 58, 78; 59, 79; 61, 80; 61, 81; 62, 82; 62, 83; 63, 84; 63, 85; 64, 86; 64, 87; 65, 88; 65, 89; 66, 90; 66, 91; 67, 92; 67, 93; 68, 94; 68, 95; 69, 96; 69, 97; 70, 98; 70, 99; 71, 100; 73, 101; 73, 102; 73, 103; 74, 104; 74, 105; 76, 106; 76, 107; 76, 108; 81, 109; 83, 110; 85, 111; 87, 112; 89, 113; 91, 114; 93, 115; 95, 116; 97, 117; 99, 118; 102, 119; 102, 120; 103, 121; 104, 122; 105, 123; 106, 124; 106, 125; 107, 126; 107, 127; 108, 128; 108, 129; 109, 130; 109, 131; 110, 132; 110, 133; 111, 134; 111, 135; 112, 136; 112, 137; 113, 138; 113, 139; 114, 140; 114, 141; 115, 142; 115, 143; 116, 144; 116, 145; 117, 146; 117, 147; 118, 148; 118, 149; 120, 150; 121, 151; 121, 152; 121, 153; 121, 154; 121, 155; 122, 156; 122, 157; 123, 158; 123, 159; 125, 160; 127, 161; 129, 162; 151, 163; 151, 164; 153, 165; 153, 166; 153, 167; 153, 168; 153, 169; 154, 170; 154, 171; 155, 172; 157, 173; 157, 174; 158, 175; 158, 176; 159, 177; 163, 178; 165, 179; 167, 180; 167, 181; 167, 182; 168, 183; 169, 184; 170, 185; 170, 186; 171, 187; 171, 188; 172, 189; 174, 190; 174, 191; 174, 192; 174, 193; 174, 194; 174, 195; 174, 196; 179, 197; 179, 198; 180, 199; 180, 200; 181, 201; 182, 202; 183, 203; 183, 204; 184, 205; 184, 206; 185, 207; 187, 208; 188, 209; 189, 210; 190, 211; 190, 212; 191, 213; 191, 214; 192, 215; 192, 216; 193, 217; 193, 218; 194, 219; 194, 220; 195, 221; 195, 222; 196, 223; 196, 224; 198, 225; 198, 226; 199, 227; 201, 228; 202, 229; 204, 230; 204, 231; 205, 232; 205, 233; 206, 234; 208, 235; 208, 236; 209, 237; 209, 238; 210, 239; 210, 240; 212, 241; 214, 242; 216, 243; 218, 244; 218, 245; 220, 246; 220, 247; 222, 248; 222, 249; 224, 250; 224, 251; 226, 252; 228, 253; 228, 254; 229, 255; 231, 256; 231, 257; 231, 258; 231, 259; 231, 260; 231, 261; 231, 262; 231, 263; 231, 264; 231, 265; 236, 266; 236, 267; 237, 268; 237, 269; 238, 270; 240, 271; 240, 272; 245, 273; 247, 274; 249, 275; 251, 276; 254, 277; 254, 278; 255, 279; 255, 280; 256, 281; 256, 282; 257, 283; 257, 284; 258, 285; 258, 286; 259, 287; 259, 288; 260, 289; 260, 290; 261, 291; 261, 292; 262, 293; 262, 294; 263, 295; 263, 296; 264, 297; 264, 298; 265, 299; 265, 300; 267, 301; 267, 302; 267, 303; 267, 304; 267, 305; 267, 306; 267, 307; 267, 308; 267, 309; 278, 310; 282, 311; 284, 312; 286, 313; 288, 314; 288, 315; 290, 316; 290, 317; 292, 318; 292, 319; 294, 320; 294, 321; 300, 322; 300, 323; 301, 324; 301, 325; 302, 326; 302, 327; 303, 328; 303, 329; 304, 330; 304, 331; 305, 332; 305, 333; 306, 334; 306, 335; 307, 336; 307, 337; 308, 338; 308, 339; 309, 340; 309, 341; 315, 342; 317, 343; 319, 344; 321, 345; 323, 346; 325, 347; 327, 348; 329, 349; 331, 350; 331, 351; 333, 352; 333, 353; 335, 354; 335, 355; 337, 356; 337, 357; 339, 358; 339, 359; 341, 360; 341, 361; 351, 362; 353, 363; 355, 364; 357, 365; 359, 366; 361, 367 | def rules(self):
"""
Returns a sorted list of firewall rules.
Returns:
list
"""
list_of_rules = []
for main_row in self.dict_rules:
if 'rules' in main_row:
for rule_row in main_row['rules']:
if 'grants' in rule_row:
for grant_row in rule_row['grants']:
if 'group_id' in grant_row:
# Set a var to not go over 80 chars
group_id = grant_row['group_id']
# Some VPC grants don't specify a name
if 'name' in grant_row:
row_name = grant_row['name']
else:
row_name = None
fr = FirewallRule(
main_row['id'],
main_row['name'],
main_row['description'],
rules_direction=rule_row['direction'],
rules_ip_protocol=rule_row['ip_protocol'],
rules_from_port=rule_row['from_port'],
rules_to_port=rule_row['to_port'],
rules_grants_group_id=group_id,
rules_grants_name=row_name,
rules_description=grant_row['description'])
list_of_rules.append(fr)
elif 'cidr_ip' in grant_row:
fr = FirewallRule(
main_row['id'],
main_row['name'],
main_row['description'],
rules_direction=rule_row['direction'],
rules_ip_protocol=rule_row['ip_protocol'],
rules_from_port=rule_row['from_port'],
rules_to_port=rule_row['to_port'],
rules_grants_cidr_ip=grant_row['cidr_ip'],
rules_description=grant_row['description'])
list_of_rules.append(fr)
else:
raise ValueError("Unsupported grant:",
grant_row)
else:
fr = FirewallRule(
main_row['id'],
main_row['name'],
main_row['description'],
rules_direction=rule_row['direction'],
rules_ip_protocol=rule_row['ip_protocol'],
rules_from_port=rule_row['from_port'],
rules_to_port=rule_row['to_port'])
list_of_rules.append(fr)
else:
fr = FirewallRule(main_row['id'],
main_row['name'],
main_row['description'])
list_of_rules.append(fr)
# Sort the data in order to get a consistent output
sorted_list = sorted(list_of_rules,
key=lambda fr: (str(fr.id),
str(fr.name),
str(fr.description),
str(fr.rules_direction),
str(fr.rules_ip_protocol),
str(fr.rules_from_port),
str(fr.rules_to_port),
str(fr.rules_grants_group_id),
str(fr.rules_grants_name),
str(fr.rules_grants_cidr_ip)))
return sorted_list |
0, module; 1, function_definition; 2, function_name:_select_mgmt_networks; 3, parameters; 4, block; 5, identifier:self; 6, identifier:conf; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, if_statement; 11, for_statement; 12, return_statement; 13, comment:"""
Select management networks. If no management network is found, it will
mark the first network found by sorted the network lists. Also adding
default DNS domain, if none is set.
Args:
conf(spec): spec
"""; 14, assignment; 15, assignment; 16, comparison_operator:len(mgmts) == 0; 17, block; 18, identifier:mgmt_name; 19, identifier:mgmts; 20, block; 21, identifier:mgmts; 22, identifier:nets; 23, subscript; 24, identifier:mgmts; 25, call; 26, call; 27, integer:0; 28, expression_statement; 29, expression_statement; 30, expression_statement; 31, expression_statement; 32, if_statement; 33, identifier:conf; 34, string; 35, identifier:sorted; 36, argument_list; 37, identifier:len; 38, argument_list; 39, assignment; 40, call; 41, assignment; 42, call; 43, comparison_operator:nets[mgmt_name].get('dns_domain_name', None) is None; 44, block; 45, string_content:nets; 46, list_comprehension; 47, identifier:mgmts; 48, identifier:mgmt_name; 49, subscript; 50, attribute; 51, argument_list; 52, subscript; 53, True; 54, attribute; 55, argument_list; 56, call; 57, None; 58, expression_statement; 59, identifier:name; 60, for_in_clause; 61, if_clause; 62, call; 63, integer:0; 64, identifier:LOGGER; 65, identifier:debug; 66, string; 67, identifier:mgmt_name; 68, subscript; 69, string; 70, identifier:mgmts; 71, identifier:append; 72, identifier:mgmt_name; 73, attribute; 74, argument_list; 75, assignment; 76, pattern_list; 77, call; 78, comparison_operator:net.get('management') is True; 79, identifier:sorted; 80, argument_list; 81, string_content:No management network configured, selecting network %s; 82, identifier:nets; 83, identifier:mgmt_name; 84, string_content:management; 85, subscript; 86, identifier:get; 87, string; 88, None; 89, subscript; 90, string; 91, identifier:name; 92, identifier:net; 93, attribute; 94, argument_list; 95, call; 96, True; 97, parenthesized_expression; 98, identifier:nets; 99, identifier:mgmt_name; 100, string_content:dns_domain_name; 101, subscript; 102, string; 103, string_content:lago.local; 104, identifier:nets; 105, identifier:iteritems; 106, attribute; 107, argument_list; 108, call; 109, identifier:nets; 110, identifier:mgmt_name; 111, string_content:dns_domain_name; 112, identifier:net; 113, identifier:get; 114, string; 115, attribute; 116, argument_list; 117, string_content:management; 118, identifier:nets; 119, identifier:keys | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 7, 13; 8, 14; 9, 15; 10, 16; 10, 17; 11, 18; 11, 19; 11, 20; 12, 21; 14, 22; 14, 23; 15, 24; 15, 25; 16, 26; 16, 27; 17, 28; 17, 29; 17, 30; 17, 31; 20, 32; 23, 33; 23, 34; 25, 35; 25, 36; 26, 37; 26, 38; 28, 39; 29, 40; 30, 41; 31, 42; 32, 43; 32, 44; 34, 45; 36, 46; 38, 47; 39, 48; 39, 49; 40, 50; 40, 51; 41, 52; 41, 53; 42, 54; 42, 55; 43, 56; 43, 57; 44, 58; 46, 59; 46, 60; 46, 61; 49, 62; 49, 63; 50, 64; 50, 65; 51, 66; 51, 67; 52, 68; 52, 69; 54, 70; 54, 71; 55, 72; 56, 73; 56, 74; 58, 75; 60, 76; 60, 77; 61, 78; 62, 79; 62, 80; 66, 81; 68, 82; 68, 83; 69, 84; 73, 85; 73, 86; 74, 87; 74, 88; 75, 89; 75, 90; 76, 91; 76, 92; 77, 93; 77, 94; 78, 95; 78, 96; 80, 97; 85, 98; 85, 99; 87, 100; 89, 101; 89, 102; 90, 103; 93, 104; 93, 105; 95, 106; 95, 107; 97, 108; 101, 109; 101, 110; 102, 111; 106, 112; 106, 113; 107, 114; 108, 115; 108, 116; 114, 117; 115, 118; 115, 119 | def _select_mgmt_networks(self, conf):
"""
Select management networks. If no management network is found, it will
mark the first network found by sorted the network lists. Also adding
default DNS domain, if none is set.
Args:
conf(spec): spec
"""
nets = conf['nets']
mgmts = sorted(
[
name for name, net in nets.iteritems()
if net.get('management') is True
]
)
if len(mgmts) == 0:
mgmt_name = sorted((nets.keys()))[0]
LOGGER.debug(
'No management network configured, selecting network %s',
mgmt_name
)
nets[mgmt_name]['management'] = True
mgmts.append(mgmt_name)
for mgmt_name in mgmts:
if nets[mgmt_name].get('dns_domain_name', None) is None:
nets[mgmt_name]['dns_domain_name'] = 'lago.local'
return mgmts |
0, module; 1, function_definition; 2, function_name:validate_wavetable; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, comment:# First check for invalid values; 8, expression_statement; 9, if_statement; 10, comment:# Now check for monotonicity & enforce ascending; 11, expression_statement; 12, if_statement; 13, comment:# Check for duplicate values; 14, expression_statement; 15, if_statement; 16, comment:"""Enforce monotonic, ascending wavelength array with no zero or
negative values.
Raises
------
pysynphot.exceptions.DuplicateWavelength
Wavelength array contains duplicate entries.
pysynphot.exceptions.UnsortedWavelength
Wavelength array is not monotonic ascending or descending.
pysynphot.exceptions.ZeroWavelength
Wavelength array has zero or negative value(s).
"""; 17, assignment; 18, call; 19, block; 20, assignment; 21, not_operator; 22, block; 23, assignment; 24, call; 25, block; 26, identifier:wave; 27, attribute; 28, attribute; 29, argument_list; 30, expression_statement; 31, raise_statement; 32, identifier:sorted; 33, call; 34, call; 35, if_statement; 36, identifier:dw; 37, binary_operator:sorted[1:] - sorted[:-1]; 38, attribute; 39, argument_list; 40, expression_statement; 41, raise_statement; 42, identifier:self; 43, identifier:_wavetable; 44, identifier:N; 45, identifier:any; 46, comparison_operator:wave <= 0; 47, assignment; 48, call; 49, attribute; 50, argument_list; 51, attribute; 52, argument_list; 53, call; 54, comment:# monotonic descending is allowed; 55, block; 56, else_clause; 57, subscript; 58, subscript; 59, identifier:N; 60, identifier:any; 61, comparison_operator:dw == 0; 62, assignment; 63, call; 64, identifier:wave; 65, integer:0; 66, identifier:wrong; 67, subscript; 68, attribute; 69, argument_list; 70, identifier:N; 71, identifier:sort; 72, identifier:wave; 73, identifier:N; 74, identifier:alltrue; 75, comparison_operator:sorted == wave; 76, attribute; 77, argument_list; 78, pass_statement; 79, block; 80, identifier:sorted; 81, slice; 82, identifier:sorted; 83, slice; 84, identifier:dw; 85, integer:0; 86, identifier:wrong; 87, subscript; 88, attribute; 89, argument_list; 90, call; 91, integer:0; 92, identifier:exceptions; 93, identifier:ZeroWavelength; 94, string; 95, keyword_argument; 96, identifier:sorted; 97, identifier:wave; 98, identifier:N; 99, identifier:alltrue; 100, comparison_operator:sorted[::-1] == wave; 101, expression_statement; 102, raise_statement; 103, integer:1; 104, unary_operator; 105, call; 106, integer:0; 107, identifier:exceptions; 108, identifier:DuplicateWavelength; 109, string:"Wavelength array contains duplicate entries"; 110, keyword_argument; 111, attribute; 112, argument_list; 113, string_content:Negative or Zero wavelength occurs in wavelength array; 114, identifier:rows; 115, identifier:wrong; 116, subscript; 117, identifier:wave; 118, assignment; 119, call; 120, integer:1; 121, attribute; 122, argument_list; 123, identifier:rows; 124, identifier:wrong; 125, identifier:N; 126, identifier:where; 127, comparison_operator:wave <= 0; 128, identifier:sorted; 129, slice; 130, identifier:wrong; 131, subscript; 132, attribute; 133, argument_list; 134, identifier:N; 135, identifier:where; 136, comparison_operator:dw == 0; 137, identifier:wave; 138, integer:0; 139, unary_operator; 140, call; 141, integer:0; 142, identifier:exceptions; 143, identifier:UnsortedWavelength; 144, string; 145, keyword_argument; 146, identifier:dw; 147, integer:0; 148, integer:1; 149, attribute; 150, argument_list; 151, string_content:Wavelength array is not monotonic; 152, identifier:rows; 153, identifier:wrong; 154, identifier:N; 155, identifier:where; 156, comparison_operator:sorted != wave; 157, identifier:sorted; 158, identifier:wave | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 6, 16; 8, 17; 9, 18; 9, 19; 11, 20; 12, 21; 12, 22; 14, 23; 15, 24; 15, 25; 17, 26; 17, 27; 18, 28; 18, 29; 19, 30; 19, 31; 20, 32; 20, 33; 21, 34; 22, 35; 23, 36; 23, 37; 24, 38; 24, 39; 25, 40; 25, 41; 27, 42; 27, 43; 28, 44; 28, 45; 29, 46; 30, 47; 31, 48; 33, 49; 33, 50; 34, 51; 34, 52; 35, 53; 35, 54; 35, 55; 35, 56; 37, 57; 37, 58; 38, 59; 38, 60; 39, 61; 40, 62; 41, 63; 46, 64; 46, 65; 47, 66; 47, 67; 48, 68; 48, 69; 49, 70; 49, 71; 50, 72; 51, 73; 51, 74; 52, 75; 53, 76; 53, 77; 55, 78; 56, 79; 57, 80; 57, 81; 58, 82; 58, 83; 61, 84; 61, 85; 62, 86; 62, 87; 63, 88; 63, 89; 67, 90; 67, 91; 68, 92; 68, 93; 69, 94; 69, 95; 75, 96; 75, 97; 76, 98; 76, 99; 77, 100; 79, 101; 79, 102; 81, 103; 83, 104; 87, 105; 87, 106; 88, 107; 88, 108; 89, 109; 89, 110; 90, 111; 90, 112; 94, 113; 95, 114; 95, 115; 100, 116; 100, 117; 101, 118; 102, 119; 104, 120; 105, 121; 105, 122; 110, 123; 110, 124; 111, 125; 111, 126; 112, 127; 116, 128; 116, 129; 118, 130; 118, 131; 119, 132; 119, 133; 121, 134; 121, 135; 122, 136; 127, 137; 127, 138; 129, 139; 131, 140; 131, 141; 132, 142; 132, 143; 133, 144; 133, 145; 136, 146; 136, 147; 139, 148; 140, 149; 140, 150; 144, 151; 145, 152; 145, 153; 149, 154; 149, 155; 150, 156; 156, 157; 156, 158 | def validate_wavetable(self):
"""Enforce monotonic, ascending wavelength array with no zero or
negative values.
Raises
------
pysynphot.exceptions.DuplicateWavelength
Wavelength array contains duplicate entries.
pysynphot.exceptions.UnsortedWavelength
Wavelength array is not monotonic ascending or descending.
pysynphot.exceptions.ZeroWavelength
Wavelength array has zero or negative value(s).
"""
# First check for invalid values
wave = self._wavetable
if N.any(wave <= 0):
wrong = N.where(wave <= 0)[0]
raise exceptions.ZeroWavelength(
'Negative or Zero wavelength occurs in wavelength array',
rows=wrong)
# Now check for monotonicity & enforce ascending
sorted = N.sort(wave)
if not N.alltrue(sorted == wave):
if N.alltrue(sorted[::-1] == wave):
# monotonic descending is allowed
pass
else:
wrong = N.where(sorted != wave)[0]
raise exceptions.UnsortedWavelength(
'Wavelength array is not monotonic', rows=wrong)
# Check for duplicate values
dw = sorted[1:] - sorted[:-1]
if N.any(dw == 0):
wrong = N.where(dw == 0)[0]
raise exceptions.DuplicateWavelength(
"Wavelength array contains duplicate entries", rows=wrong) |
0, module; 1, function_definition; 2, function_name:getStateIndex; 3, parameters; 4, block; 5, identifier:self; 6, identifier:state; 7, expression_statement; 8, expression_statement; 9, return_statement; 10, comment:"""
Returns the index of a state by calculating the state code and searching for this code a sorted list.
Can be called on multiple states at once.
"""; 11, assignment; 12, call; 13, identifier:statecodes; 14, call; 15, attribute; 16, argument_list; 17, attribute; 18, argument_list; 19, call; 20, identifier:astype; 21, identifier:int; 22, identifier:self; 23, identifier:getStateCode; 24, identifier:state; 25, attribute; 26, argument_list; 27, identifier:np; 28, identifier:searchsorted; 29, attribute; 30, identifier:statecodes; 31, identifier:self; 32, identifier:codes | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 7, 10; 8, 11; 9, 12; 11, 13; 11, 14; 12, 15; 12, 16; 14, 17; 14, 18; 15, 19; 15, 20; 16, 21; 17, 22; 17, 23; 18, 24; 19, 25; 19, 26; 25, 27; 25, 28; 26, 29; 26, 30; 29, 31; 29, 32 | def getStateIndex(self,state):
"""
Returns the index of a state by calculating the state code and searching for this code a sorted list.
Can be called on multiple states at once.
"""
statecodes = self.getStateCode(state)
return np.searchsorted(self.codes,statecodes).astype(int) |
0, module; 1, function_definition; 2, function_name:guess_chimera_path; 3, parameters; 4, block; 5, default_parameter; 6, expression_statement; 7, expression_statement; 8, if_statement; 9, if_statement; 10, return_statement; 11, identifier:search_all; 12, False; 13, comment:"""
Try to guess Chimera installation path.
Parameters
----------
search_all : bool, optional, default=False
If no CHIMERADIR env var is set, collect all posible
locations of Chimera installations.
Returns
-------
paths: list of str
Alphabetically sorted list of possible Chimera locations
"""; 14, assignment; 15, boolean_operator; 16, comment:# try headless?; 17, block; 18, not_operator; 19, block; 20, identifier:paths; 21, identifier:paths; 22, call; 23, not_operator; 24, identifier:search_all; 25, expression_statement; 26, expression_statement; 27, identifier:paths; 28, expression_statement; 29, identifier:_search_chimera; 30, argument_list; 31, identifier:paths; 32, assignment; 33, assignment; 34, call; 35, identifier:CHIMERA_BINARY; 36, identifier:CHIMERA_LOCATIONS; 37, identifier:CHIMERA_PREFIX; 38, keyword_argument; 39, identifier:headless; 40, call; 41, identifier:paths; 42, call; 43, attribute; 44, argument_list; 45, identifier:search_all; 46, identifier:search_all; 47, attribute; 48, argument_list; 49, identifier:_search_chimera; 50, argument_list; 51, identifier:sys; 52, identifier:exit; 53, call; 54, string; 55, identifier:format; 56, call; 57, string; 58, identifier:headless; 59, identifier:CHIMERA_LOCATIONS; 60, identifier:CHIMERA_PREFIX; 61, keyword_argument; 62, attribute; 63, argument_list; 64, string_content:{0[0]}{1}{0[1]}; 65, attribute; 66, argument_list; 67, string_content:-headless; 68, identifier:search_all; 69, identifier:search_all; 70, string:"Could not find UCSF Chimera.\n{}"; 71, identifier:format; 72, identifier:_INSTRUCTIONS; 73, attribute; 74, identifier:split; 75, identifier:CHIMERA_BINARY; 76, identifier:os; 77, identifier:path | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 5, 11; 5, 12; 6, 13; 7, 14; 8, 15; 8, 16; 8, 17; 9, 18; 9, 19; 10, 20; 14, 21; 14, 22; 15, 23; 15, 24; 17, 25; 17, 26; 18, 27; 19, 28; 22, 29; 22, 30; 23, 31; 25, 32; 26, 33; 28, 34; 30, 35; 30, 36; 30, 37; 30, 38; 32, 39; 32, 40; 33, 41; 33, 42; 34, 43; 34, 44; 38, 45; 38, 46; 40, 47; 40, 48; 42, 49; 42, 50; 43, 51; 43, 52; 44, 53; 47, 54; 47, 55; 48, 56; 48, 57; 50, 58; 50, 59; 50, 60; 50, 61; 53, 62; 53, 63; 54, 64; 56, 65; 56, 66; 57, 67; 61, 68; 61, 69; 62, 70; 62, 71; 63, 72; 65, 73; 65, 74; 66, 75; 73, 76; 73, 77 | def guess_chimera_path(search_all=False):
"""
Try to guess Chimera installation path.
Parameters
----------
search_all : bool, optional, default=False
If no CHIMERADIR env var is set, collect all posible
locations of Chimera installations.
Returns
-------
paths: list of str
Alphabetically sorted list of possible Chimera locations
"""
paths = _search_chimera(CHIMERA_BINARY, CHIMERA_LOCATIONS, CHIMERA_PREFIX,
search_all=search_all)
if not paths and search_all: # try headless?
headless = '{0[0]}{1}{0[1]}'.format(os.path.split(CHIMERA_BINARY), '-headless')
paths = _search_chimera(headless, CHIMERA_LOCATIONS, CHIMERA_PREFIX,
search_all=search_all)
if not paths:
sys.exit("Could not find UCSF Chimera.\n{}".format(_INSTRUCTIONS))
return paths |
0, module; 1, function_definition; 2, function_name:_topo_sort; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, comment:# select the operation that will be performed; 12, if_statement; 13, for_statement; 14, while_statement; 15, if_statement; 16, return_statement; 17, identifier:forward; 18, True; 19, comment:"""
Topological sort.
Returns a list of nodes where the successors (based on outgoing and
incoming edges selected by the forward parameter) of any given node
appear in the sequence after that node.
"""; 20, assignment; 21, assignment; 22, assignment; 23, identifier:forward; 24, block; 25, else_clause; 26, identifier:node; 27, call; 28, block; 29, identifier:queue; 30, block; 31, comparison_operator:len(topo_list) == len(self.node_list()); 32, block; 33, else_clause; 34, tuple; 35, identifier:topo_list; 36, list; 37, identifier:queue; 38, call; 39, identifier:indeg; 40, dictionary; 41, expression_statement; 42, expression_statement; 43, expression_statement; 44, block; 45, attribute; 46, argument_list; 47, expression_statement; 48, if_statement; 49, expression_statement; 50, expression_statement; 51, for_statement; 52, call; 53, call; 54, expression_statement; 55, comment:# the graph has cycles, invalid topological sort; 56, block; 57, identifier:valid; 58, identifier:topo_list; 59, identifier:deque; 60, argument_list; 61, assignment; 62, assignment; 63, assignment; 64, expression_statement; 65, expression_statement; 66, expression_statement; 67, identifier:self; 68, identifier:node_list; 69, assignment; 70, identifier:degree; 71, block; 72, else_clause; 73, assignment; 74, call; 75, identifier:edge; 76, call; 77, block; 78, identifier:len; 79, argument_list; 80, identifier:len; 81, argument_list; 82, assignment; 83, expression_statement; 84, identifier:get_edges; 85, attribute; 86, identifier:get_degree; 87, attribute; 88, identifier:get_next; 89, attribute; 90, assignment; 91, assignment; 92, assignment; 93, identifier:degree; 94, call; 95, expression_statement; 96, block; 97, identifier:curr_node; 98, call; 99, attribute; 100, argument_list; 101, identifier:get_edges; 102, argument_list; 103, expression_statement; 104, if_statement; 105, identifier:topo_list; 106, call; 107, identifier:valid; 108, True; 109, assignment; 110, identifier:self; 111, identifier:out_edges; 112, identifier:self; 113, identifier:inc_degree; 114, identifier:self; 115, identifier:tail; 116, identifier:get_edges; 117, attribute; 118, identifier:get_degree; 119, attribute; 120, identifier:get_next; 121, attribute; 122, identifier:get_degree; 123, argument_list; 124, assignment; 125, expression_statement; 126, attribute; 127, argument_list; 128, identifier:topo_list; 129, identifier:append; 130, identifier:curr_node; 131, identifier:curr_node; 132, assignment; 133, comparison_operator:tail_id in indeg; 134, block; 135, attribute; 136, argument_list; 137, identifier:valid; 138, False; 139, identifier:self; 140, identifier:inc_edges; 141, identifier:self; 142, identifier:out_degree; 143, identifier:self; 144, identifier:head; 145, identifier:node; 146, subscript; 147, identifier:degree; 148, call; 149, identifier:queue; 150, identifier:popleft; 151, identifier:tail_id; 152, call; 153, identifier:tail_id; 154, identifier:indeg; 155, expression_statement; 156, if_statement; 157, identifier:self; 158, identifier:node_list; 159, identifier:indeg; 160, identifier:node; 161, attribute; 162, argument_list; 163, identifier:get_next; 164, argument_list; 165, augmented_assignment; 166, comparison_operator:indeg[tail_id] == 0; 167, block; 168, identifier:queue; 169, identifier:append; 170, identifier:node; 171, identifier:edge; 172, subscript; 173, integer:1; 174, subscript; 175, integer:0; 176, expression_statement; 177, identifier:indeg; 178, identifier:tail_id; 179, identifier:indeg; 180, identifier:tail_id; 181, call; 182, attribute; 183, argument_list; 184, identifier:queue; 185, identifier:append; 186, identifier:tail_id | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 6, 17; 6, 18; 7, 19; 8, 20; 9, 21; 10, 22; 12, 23; 12, 24; 12, 25; 13, 26; 13, 27; 13, 28; 14, 29; 14, 30; 15, 31; 15, 32; 15, 33; 16, 34; 20, 35; 20, 36; 21, 37; 21, 38; 22, 39; 22, 40; 24, 41; 24, 42; 24, 43; 25, 44; 27, 45; 27, 46; 28, 47; 28, 48; 30, 49; 30, 50; 30, 51; 31, 52; 31, 53; 32, 54; 33, 55; 33, 56; 34, 57; 34, 58; 38, 59; 38, 60; 41, 61; 42, 62; 43, 63; 44, 64; 44, 65; 44, 66; 45, 67; 45, 68; 47, 69; 48, 70; 48, 71; 48, 72; 49, 73; 50, 74; 51, 75; 51, 76; 51, 77; 52, 78; 52, 79; 53, 80; 53, 81; 54, 82; 56, 83; 61, 84; 61, 85; 62, 86; 62, 87; 63, 88; 63, 89; 64, 90; 65, 91; 66, 92; 69, 93; 69, 94; 71, 95; 72, 96; 73, 97; 73, 98; 74, 99; 74, 100; 76, 101; 76, 102; 77, 103; 77, 104; 79, 105; 81, 106; 82, 107; 82, 108; 83, 109; 85, 110; 85, 111; 87, 112; 87, 113; 89, 114; 89, 115; 90, 116; 90, 117; 91, 118; 91, 119; 92, 120; 92, 121; 94, 122; 94, 123; 95, 124; 96, 125; 98, 126; 98, 127; 99, 128; 99, 129; 100, 130; 102, 131; 103, 132; 104, 133; 104, 134; 106, 135; 106, 136; 109, 137; 109, 138; 117, 139; 117, 140; 119, 141; 119, 142; 121, 143; 121, 144; 123, 145; 124, 146; 124, 147; 125, 148; 126, 149; 126, 150; 132, 151; 132, 152; 133, 153; 133, 154; 134, 155; 134, 156; 135, 157; 135, 158; 146, 159; 146, 160; 148, 161; 148, 162; 152, 163; 152, 164; 155, 165; 156, 166; 156, 167; 161, 168; 161, 169; 162, 170; 164, 171; 165, 172; 165, 173; 166, 174; 166, 175; 167, 176; 172, 177; 172, 178; 174, 179; 174, 180; 176, 181; 181, 182; 181, 183; 182, 184; 182, 185; 183, 186 | def _topo_sort(self, forward=True):
"""
Topological sort.
Returns a list of nodes where the successors (based on outgoing and
incoming edges selected by the forward parameter) of any given node
appear in the sequence after that node.
"""
topo_list = []
queue = deque()
indeg = {}
# select the operation that will be performed
if forward:
get_edges = self.out_edges
get_degree = self.inc_degree
get_next = self.tail
else:
get_edges = self.inc_edges
get_degree = self.out_degree
get_next = self.head
for node in self.node_list():
degree = get_degree(node)
if degree:
indeg[node] = degree
else:
queue.append(node)
while queue:
curr_node = queue.popleft()
topo_list.append(curr_node)
for edge in get_edges(curr_node):
tail_id = get_next(edge)
if tail_id in indeg:
indeg[tail_id] -= 1
if indeg[tail_id] == 0:
queue.append(tail_id)
if len(topo_list) == len(self.node_list()):
valid = True
else:
# the graph has cycles, invalid topological sort
valid = False
return (valid, topo_list) |
0, module; 1, function_definition; 2, function_name:execute_sql; 3, parameters; 4, block; 5, identifier:server_context; 6, identifier:schema_name; 7, identifier:sql; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, default_parameter; 13, default_parameter; 14, default_parameter; 15, default_parameter; 16, default_parameter; 17, expression_statement; 18, expression_statement; 19, expression_statement; 20, if_statement; 21, if_statement; 22, if_statement; 23, if_statement; 24, if_statement; 25, if_statement; 26, if_statement; 27, return_statement; 28, identifier:container_path; 29, None; 30, identifier:max_rows; 31, None; 32, identifier:sort; 33, None; 34, identifier:offset; 35, None; 36, identifier:container_filter; 37, None; 38, identifier:save_in_session; 39, None; 40, identifier:parameters; 41, None; 42, identifier:required_version; 43, None; 44, identifier:timeout; 45, identifier:_default_timeout; 46, comment:"""
Execute sql query against a LabKey server.
:param server_context: A LabKey server context. See utils.create_server_context.
:param schema_name: schema of table
:param sql: String of labkey sql to execute
:param container_path: labkey container path if not already set in context
:param max_rows: max number of rows to return
:param sort: comma separated list of column names to sort by
:param offset: number of rows to offset results by
:param container_filter: enumeration of the various container filters available. See:
https://www.labkey.org/download/clientapi_docs/javascript-api/symbols/LABKEY.Query.html#.containerFilter
:param save_in_session: save query result as a named view to the session
:param parameters: parameter values to pass through to a parameterized query
:param required_version: Api version of response
:param timeout: timeout of request in seconds (defaults to 30s)
:return:
"""; 47, assignment; 48, assignment; 49, comparison_operator:container_filter is not None; 50, block; 51, comparison_operator:max_rows is not None; 52, block; 53, comparison_operator:offset is not None; 54, block; 55, comparison_operator:sort is not None; 56, block; 57, comparison_operator:save_in_session is not None; 58, block; 59, comparison_operator:parameters is not None; 60, block; 61, comparison_operator:required_version is not None; 62, block; 63, call; 64, identifier:url; 65, call; 66, identifier:payload; 67, dictionary; 68, identifier:container_filter; 69, None; 70, expression_statement; 71, identifier:max_rows; 72, None; 73, expression_statement; 74, identifier:offset; 75, None; 76, expression_statement; 77, identifier:sort; 78, None; 79, expression_statement; 80, identifier:save_in_session; 81, None; 82, expression_statement; 83, identifier:parameters; 84, None; 85, for_statement; 86, identifier:required_version; 87, None; 88, expression_statement; 89, attribute; 90, argument_list; 91, attribute; 92, argument_list; 93, pair; 94, pair; 95, assignment; 96, assignment; 97, assignment; 98, assignment; 99, assignment; 100, pattern_list; 101, call; 102, block; 103, assignment; 104, identifier:server_context; 105, identifier:make_request; 106, identifier:url; 107, identifier:payload; 108, keyword_argument; 109, identifier:server_context; 110, identifier:build_url; 111, string; 112, string; 113, keyword_argument; 114, string; 115, identifier:schema_name; 116, string; 117, identifier:sql; 118, subscript; 119, identifier:container_filter; 120, subscript; 121, identifier:max_rows; 122, subscript; 123, identifier:offset; 124, subscript; 125, identifier:sort; 126, subscript; 127, identifier:save_in_session; 128, identifier:key; 129, identifier:value; 130, attribute; 131, argument_list; 132, expression_statement; 133, subscript; 134, identifier:required_version; 135, identifier:timeout; 136, identifier:timeout; 137, string_content:query; 138, string_content:executeSql.api; 139, identifier:container_path; 140, identifier:container_path; 141, string_content:schemaName; 142, string_content:sql; 143, identifier:payload; 144, string; 145, identifier:payload; 146, string; 147, identifier:payload; 148, string; 149, identifier:payload; 150, string; 151, identifier:payload; 152, string; 153, identifier:parameters; 154, identifier:items; 155, assignment; 156, identifier:payload; 157, string; 158, string_content:containerFilter; 159, string_content:maxRows; 160, string_content:offset; 161, string_content:query.sort; 162, string_content:saveInSession; 163, subscript; 164, identifier:value; 165, string_content:apiVersion; 166, identifier:payload; 167, binary_operator:'query.param.' + key; 168, string; 169, identifier:key; 170, string_content:query.param. | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 3, 13; 3, 14; 3, 15; 3, 16; 4, 17; 4, 18; 4, 19; 4, 20; 4, 21; 4, 22; 4, 23; 4, 24; 4, 25; 4, 26; 4, 27; 8, 28; 8, 29; 9, 30; 9, 31; 10, 32; 10, 33; 11, 34; 11, 35; 12, 36; 12, 37; 13, 38; 13, 39; 14, 40; 14, 41; 15, 42; 15, 43; 16, 44; 16, 45; 17, 46; 18, 47; 19, 48; 20, 49; 20, 50; 21, 51; 21, 52; 22, 53; 22, 54; 23, 55; 23, 56; 24, 57; 24, 58; 25, 59; 25, 60; 26, 61; 26, 62; 27, 63; 47, 64; 47, 65; 48, 66; 48, 67; 49, 68; 49, 69; 50, 70; 51, 71; 51, 72; 52, 73; 53, 74; 53, 75; 54, 76; 55, 77; 55, 78; 56, 79; 57, 80; 57, 81; 58, 82; 59, 83; 59, 84; 60, 85; 61, 86; 61, 87; 62, 88; 63, 89; 63, 90; 65, 91; 65, 92; 67, 93; 67, 94; 70, 95; 73, 96; 76, 97; 79, 98; 82, 99; 85, 100; 85, 101; 85, 102; 88, 103; 89, 104; 89, 105; 90, 106; 90, 107; 90, 108; 91, 109; 91, 110; 92, 111; 92, 112; 92, 113; 93, 114; 93, 115; 94, 116; 94, 117; 95, 118; 95, 119; 96, 120; 96, 121; 97, 122; 97, 123; 98, 124; 98, 125; 99, 126; 99, 127; 100, 128; 100, 129; 101, 130; 101, 131; 102, 132; 103, 133; 103, 134; 108, 135; 108, 136; 111, 137; 112, 138; 113, 139; 113, 140; 114, 141; 116, 142; 118, 143; 118, 144; 120, 145; 120, 146; 122, 147; 122, 148; 124, 149; 124, 150; 126, 151; 126, 152; 130, 153; 130, 154; 132, 155; 133, 156; 133, 157; 144, 158; 146, 159; 148, 160; 150, 161; 152, 162; 155, 163; 155, 164; 157, 165; 163, 166; 163, 167; 167, 168; 167, 169; 168, 170 | def execute_sql(server_context, schema_name, sql, container_path=None,
max_rows=None,
sort=None,
offset=None,
container_filter=None,
save_in_session=None,
parameters=None,
required_version=None,
timeout=_default_timeout):
"""
Execute sql query against a LabKey server.
:param server_context: A LabKey server context. See utils.create_server_context.
:param schema_name: schema of table
:param sql: String of labkey sql to execute
:param container_path: labkey container path if not already set in context
:param max_rows: max number of rows to return
:param sort: comma separated list of column names to sort by
:param offset: number of rows to offset results by
:param container_filter: enumeration of the various container filters available. See:
https://www.labkey.org/download/clientapi_docs/javascript-api/symbols/LABKEY.Query.html#.containerFilter
:param save_in_session: save query result as a named view to the session
:param parameters: parameter values to pass through to a parameterized query
:param required_version: Api version of response
:param timeout: timeout of request in seconds (defaults to 30s)
:return:
"""
url = server_context.build_url('query', 'executeSql.api', container_path=container_path)
payload = {
'schemaName': schema_name,
'sql': sql
}
if container_filter is not None:
payload['containerFilter'] = container_filter
if max_rows is not None:
payload['maxRows'] = max_rows
if offset is not None:
payload['offset'] = offset
if sort is not None:
payload['query.sort'] = sort
if save_in_session is not None:
payload['saveInSession'] = save_in_session
if parameters is not None:
for key, value in parameters.items():
payload['query.param.' + key] = value
if required_version is not None:
payload['apiVersion'] = required_version
return server_context.make_request(url, payload, timeout=timeout) |
0, module; 1, function_definition; 2, function_name:find_missing_projections; 3, parameters; 4, block; 5, identifier:label_list; 6, identifier:projections; 7, expression_statement; 8, expression_statement; 9, if_statement; 10, for_statement; 11, return_statement; 12, comment:"""
Finds all combinations of labels in `label_list` that are not covered by an entry in the dictionary of
`projections`. Returns a list containing tuples of uncovered label combinations or en empty list if there are none.
All uncovered label combinations are naturally sorted.
Each entry in the dictionary of projections represents a single projection that maps a combination of labels (key)
to a single new label (value). The combination of labels to be mapped is a tuple of naturally sorted labels that
apply to one or more segments simultaneously. By defining a special wildcard projection using `('**',)` is is not
required to specify a projection for every single combination of labels.
Args:
label_list (audiomate.annotations.LabelList): The label list to relabel
projections (dict): A dictionary that maps tuples of label combinations to string
labels.
Returns:
List: List of combinations of labels that are not covered by any projection
Example:
>>> ll = annotations.LabelList(labels=[
... annotations.Label('b', 3.2, 4.5),
... annotations.Label('a', 4.0, 4.9),
... annotations.Label('c', 4.2, 5.1)
... ])
>>> find_missing_projections(ll, {('b',): 'new_label'})
[('a', 'b'), ('a', 'b', 'c'), ('a', 'c'), ('c',)]
"""; 13, assignment; 14, comparison_operator:WILDCARD_COMBINATION in projections; 15, block; 16, identifier:labeled_segment; 17, call; 18, block; 19, call; 20, identifier:unmapped_combinations; 21, call; 22, identifier:WILDCARD_COMBINATION; 23, identifier:projections; 24, return_statement; 25, attribute; 26, argument_list; 27, expression_statement; 28, if_statement; 29, identifier:sorted; 30, argument_list; 31, identifier:set; 32, argument_list; 33, list; 34, identifier:label_list; 35, identifier:ranges; 36, assignment; 37, comparison_operator:combination not in projections; 38, block; 39, identifier:unmapped_combinations; 40, identifier:combination; 41, call; 42, identifier:combination; 43, identifier:projections; 44, expression_statement; 45, identifier:tuple; 46, argument_list; 47, call; 48, call; 49, attribute; 50, argument_list; 51, identifier:sorted; 52, argument_list; 53, identifier:unmapped_combinations; 54, identifier:add; 55, identifier:combination; 56, list_comprehension; 57, attribute; 58, for_in_clause; 59, identifier:label; 60, identifier:value; 61, identifier:label; 62, subscript; 63, identifier:labeled_segment; 64, integer:2 | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 7, 12; 8, 13; 9, 14; 9, 15; 10, 16; 10, 17; 10, 18; 11, 19; 13, 20; 13, 21; 14, 22; 14, 23; 15, 24; 17, 25; 17, 26; 18, 27; 18, 28; 19, 29; 19, 30; 21, 31; 21, 32; 24, 33; 25, 34; 25, 35; 27, 36; 28, 37; 28, 38; 30, 39; 36, 40; 36, 41; 37, 42; 37, 43; 38, 44; 41, 45; 41, 46; 44, 47; 46, 48; 47, 49; 47, 50; 48, 51; 48, 52; 49, 53; 49, 54; 50, 55; 52, 56; 56, 57; 56, 58; 57, 59; 57, 60; 58, 61; 58, 62; 62, 63; 62, 64 | def find_missing_projections(label_list, projections):
"""
Finds all combinations of labels in `label_list` that are not covered by an entry in the dictionary of
`projections`. Returns a list containing tuples of uncovered label combinations or en empty list if there are none.
All uncovered label combinations are naturally sorted.
Each entry in the dictionary of projections represents a single projection that maps a combination of labels (key)
to a single new label (value). The combination of labels to be mapped is a tuple of naturally sorted labels that
apply to one or more segments simultaneously. By defining a special wildcard projection using `('**',)` is is not
required to specify a projection for every single combination of labels.
Args:
label_list (audiomate.annotations.LabelList): The label list to relabel
projections (dict): A dictionary that maps tuples of label combinations to string
labels.
Returns:
List: List of combinations of labels that are not covered by any projection
Example:
>>> ll = annotations.LabelList(labels=[
... annotations.Label('b', 3.2, 4.5),
... annotations.Label('a', 4.0, 4.9),
... annotations.Label('c', 4.2, 5.1)
... ])
>>> find_missing_projections(ll, {('b',): 'new_label'})
[('a', 'b'), ('a', 'b', 'c'), ('a', 'c'), ('c',)]
"""
unmapped_combinations = set()
if WILDCARD_COMBINATION in projections:
return []
for labeled_segment in label_list.ranges():
combination = tuple(sorted([label.value for label in labeled_segment[2]]))
if combination not in projections:
unmapped_combinations.add(combination)
return sorted(unmapped_combinations) |
0, module; 1, function_definition; 2, function_name:load_projections; 3, parameters; 4, block; 5, identifier:projections_file; 6, expression_statement; 7, expression_statement; 8, for_statement; 9, return_statement; 10, comment:"""
Loads projections defined in the given `projections_file`.
The `projections_file` is expected to be in the following format::
old_label_1 | new_label_1
old_label_1 old_label_2 | new_label_2
old_label_3 |
You can define one projection per line. Each projection starts with a list of one or multiple
old labels (separated by a single whitespace) that are separated from the new label by a pipe
(`|`). In the code above, the segment labeled with `old_label_1` will be labeled with
`new_label_1` after applying the projection. Segments that are labeled with `old_label_1`
**and** `old_label_2` concurrently are relabeled to `new_label_2`. All segments labeled with
`old_label_3` are dropped. Combinations of multiple labels are automatically sorted in natural
order.
Args:
projections_file (str): Path to the file with projections
Returns:
dict: Dictionary where the keys are tuples of labels to project to the key's value
Example:
>>> load_projections('/path/to/projections.txt')
{('b',): 'foo', ('a', 'b'): 'a_b', ('a',): 'bar'}
"""; 11, assignment; 12, identifier:parts; 13, call; 14, block; 15, identifier:projections; 16, identifier:projections; 17, dictionary; 18, attribute; 19, argument_list; 20, expression_statement; 21, expression_statement; 22, expression_statement; 23, identifier:textfile; 24, identifier:read_separated_lines_generator; 25, identifier:projections_file; 26, string; 27, assignment; 28, assignment; 29, assignment; 30, string_content:|; 31, identifier:combination; 32, call; 33, identifier:new_label; 34, call; 35, subscript; 36, identifier:new_label; 37, identifier:tuple; 38, argument_list; 39, attribute; 40, argument_list; 41, identifier:projections; 42, identifier:combination; 43, call; 44, subscript; 45, identifier:strip; 46, identifier:sorted; 47, argument_list; 48, identifier:parts; 49, integer:1; 50, list_comprehension; 51, call; 52, for_in_clause; 53, attribute; 54, argument_list; 55, identifier:label; 56, call; 57, identifier:label; 58, identifier:strip; 59, attribute; 60, argument_list; 61, subscript; 62, identifier:split; 63, string; 64, identifier:parts; 65, integer:0; 66, string_content: | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 6, 10; 7, 11; 8, 12; 8, 13; 8, 14; 9, 15; 11, 16; 11, 17; 13, 18; 13, 19; 14, 20; 14, 21; 14, 22; 18, 23; 18, 24; 19, 25; 19, 26; 20, 27; 21, 28; 22, 29; 26, 30; 27, 31; 27, 32; 28, 33; 28, 34; 29, 35; 29, 36; 32, 37; 32, 38; 34, 39; 34, 40; 35, 41; 35, 42; 38, 43; 39, 44; 39, 45; 43, 46; 43, 47; 44, 48; 44, 49; 47, 50; 50, 51; 50, 52; 51, 53; 51, 54; 52, 55; 52, 56; 53, 57; 53, 58; 56, 59; 56, 60; 59, 61; 59, 62; 60, 63; 61, 64; 61, 65; 63, 66 | def load_projections(projections_file):
"""
Loads projections defined in the given `projections_file`.
The `projections_file` is expected to be in the following format::
old_label_1 | new_label_1
old_label_1 old_label_2 | new_label_2
old_label_3 |
You can define one projection per line. Each projection starts with a list of one or multiple
old labels (separated by a single whitespace) that are separated from the new label by a pipe
(`|`). In the code above, the segment labeled with `old_label_1` will be labeled with
`new_label_1` after applying the projection. Segments that are labeled with `old_label_1`
**and** `old_label_2` concurrently are relabeled to `new_label_2`. All segments labeled with
`old_label_3` are dropped. Combinations of multiple labels are automatically sorted in natural
order.
Args:
projections_file (str): Path to the file with projections
Returns:
dict: Dictionary where the keys are tuples of labels to project to the key's value
Example:
>>> load_projections('/path/to/projections.txt')
{('b',): 'foo', ('a', 'b'): 'a_b', ('a',): 'bar'}
"""
projections = {}
for parts in textfile.read_separated_lines_generator(projections_file, '|'):
combination = tuple(sorted([label.strip() for label in parts[0].split(' ')]))
new_label = parts[1].strip()
projections[combination] = new_label
return projections |
0, module; 1, function_definition; 2, function_name:label_values; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, return_statement; 9, comment:"""
Return a list of all occuring label values.
Returns:
list: Lexicographically sorted list (str) of label values.
Example:
>>> ll = LabelList(labels=[
>>> Label('a', 3.2, 4.5),
>>> Label('b', 5.1, 8.9),
>>> Label('c', 7.2, 10.5),
>>> Label('d', 10.5, 14),
>>> Label('d', 15, 18)
>>> ])
>>> ll.label_values()
['a', 'b', 'c', 'd']
"""; 10, assignment; 11, call; 12, identifier:all_labels; 13, call; 14, identifier:sorted; 15, argument_list; 16, identifier:set; 17, argument_list; 18, identifier:all_labels; 19, list_comprehension; 20, attribute; 21, for_in_clause; 22, identifier:l; 23, identifier:value; 24, identifier:l; 25, identifier:self | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 6, 9; 7, 10; 8, 11; 10, 12; 10, 13; 11, 14; 11, 15; 13, 16; 13, 17; 15, 18; 17, 19; 19, 20; 19, 21; 20, 22; 20, 23; 21, 24; 21, 25 | def label_values(self):
"""
Return a list of all occuring label values.
Returns:
list: Lexicographically sorted list (str) of label values.
Example:
>>> ll = LabelList(labels=[
>>> Label('a', 3.2, 4.5),
>>> Label('b', 5.1, 8.9),
>>> Label('c', 7.2, 10.5),
>>> Label('d', 10.5, 14),
>>> Label('d', 15, 18)
>>> ])
>>> ll.label_values()
['a', 'b', 'c', 'd']
"""
all_labels = set([l.value for l in self])
return sorted(all_labels) |
0, module; 1, function_definition; 2, function_name:get_utt_regions; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, for_statement; 10, return_statement; 11, comment:"""
Return the regions of all utterances, assuming all utterances are concatenated.
It is assumed that the utterances are sorted in ascending order for concatenation.
A region is defined by offset (in chunks), length (num-chunks) and
a list of references to the utterance datasets in the containers.
Returns:
list: List of with a tuple for every utterances containing the region info.
"""; 12, assignment; 13, assignment; 14, identifier:utt_idx; 15, call; 16, block; 17, identifier:regions; 18, identifier:regions; 19, list; 20, identifier:current_offset; 21, integer:0; 22, identifier:sorted; 23, argument_list; 24, expression_statement; 25, expression_statement; 26, expression_statement; 27, for_statement; 28, if_statement; 29, expression_statement; 30, expression_statement; 31, expression_statement; 32, comment:# Sets the offset for the next utterances; 33, expression_statement; 34, attribute; 35, assignment; 36, assignment; 37, assignment; 38, identifier:cnt; 39, attribute; 40, block; 41, comparison_operator:len(set(num_frames)) != 1; 42, block; 43, assignment; 44, assignment; 45, call; 46, augmented_assignment; 47, identifier:self; 48, identifier:utt_ids; 49, identifier:offset; 50, identifier:current_offset; 51, identifier:num_frames; 52, list; 53, identifier:refs; 54, list; 55, identifier:self; 56, identifier:containers; 57, expression_statement; 58, expression_statement; 59, call; 60, integer:1; 61, raise_statement; 62, identifier:num_chunks; 63, call; 64, identifier:region; 65, tuple; 66, attribute; 67, argument_list; 68, identifier:current_offset; 69, identifier:num_chunks; 70, call; 71, call; 72, identifier:len; 73, argument_list; 74, call; 75, attribute; 76, argument_list; 77, identifier:offset; 78, identifier:num_chunks; 79, identifier:refs; 80, identifier:regions; 81, identifier:append; 82, identifier:region; 83, attribute; 84, argument_list; 85, attribute; 86, argument_list; 87, call; 88, identifier:ValueError; 89, argument_list; 90, identifier:math; 91, identifier:ceil; 92, binary_operator:num_frames[0] / float(self.frames_per_chunk); 93, identifier:num_frames; 94, identifier:append; 95, subscript; 96, identifier:refs; 97, identifier:append; 98, call; 99, identifier:set; 100, argument_list; 101, call; 102, subscript; 103, call; 104, attribute; 105, integer:0; 106, attribute; 107, argument_list; 108, identifier:num_frames; 109, attribute; 110, argument_list; 111, identifier:num_frames; 112, integer:0; 113, identifier:float; 114, argument_list; 115, call; 116, identifier:shape; 117, identifier:cnt; 118, identifier:get; 119, identifier:utt_idx; 120, keyword_argument; 121, string; 122, identifier:format; 123, identifier:utt_idx; 124, attribute; 125, attribute; 126, argument_list; 127, identifier:mem_map; 128, True; 129, string_content:Utterance {} has not the same number of frames in all containers!; 130, identifier:self; 131, identifier:frames_per_chunk; 132, identifier:cnt; 133, identifier:get; 134, identifier:utt_idx | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 6, 11; 7, 12; 8, 13; 9, 14; 9, 15; 9, 16; 10, 17; 12, 18; 12, 19; 13, 20; 13, 21; 15, 22; 15, 23; 16, 24; 16, 25; 16, 26; 16, 27; 16, 28; 16, 29; 16, 30; 16, 31; 16, 32; 16, 33; 23, 34; 24, 35; 25, 36; 26, 37; 27, 38; 27, 39; 27, 40; 28, 41; 28, 42; 29, 43; 30, 44; 31, 45; 33, 46; 34, 47; 34, 48; 35, 49; 35, 50; 36, 51; 36, 52; 37, 53; 37, 54; 39, 55; 39, 56; 40, 57; 40, 58; 41, 59; 41, 60; 42, 61; 43, 62; 43, 63; 44, 64; 44, 65; 45, 66; 45, 67; 46, 68; 46, 69; 57, 70; 58, 71; 59, 72; 59, 73; 61, 74; 63, 75; 63, 76; 65, 77; 65, 78; 65, 79; 66, 80; 66, 81; 67, 82; 70, 83; 70, 84; 71, 85; 71, 86; 73, 87; 74, 88; 74, 89; 75, 90; 75, 91; 76, 92; 83, 93; 83, 94; 84, 95; 85, 96; 85, 97; 86, 98; 87, 99; 87, 100; 89, 101; 92, 102; 92, 103; 95, 104; 95, 105; 98, 106; 98, 107; 100, 108; 101, 109; 101, 110; 102, 111; 102, 112; 103, 113; 103, 114; 104, 115; 104, 116; 106, 117; 106, 118; 107, 119; 107, 120; 109, 121; 109, 122; 110, 123; 114, 124; 115, 125; 115, 126; 120, 127; 120, 128; 121, 129; 124, 130; 124, 131; 125, 132; 125, 133; 126, 134 | def get_utt_regions(self):
"""
Return the regions of all utterances, assuming all utterances are concatenated.
It is assumed that the utterances are sorted in ascending order for concatenation.
A region is defined by offset (in chunks), length (num-chunks) and
a list of references to the utterance datasets in the containers.
Returns:
list: List of with a tuple for every utterances containing the region info.
"""
regions = []
current_offset = 0
for utt_idx in sorted(self.utt_ids):
offset = current_offset
num_frames = []
refs = []
for cnt in self.containers:
num_frames.append(cnt.get(utt_idx).shape[0])
refs.append(cnt.get(utt_idx, mem_map=True))
if len(set(num_frames)) != 1:
raise ValueError('Utterance {} has not the same number of frames in all containers!'.format(utt_idx))
num_chunks = math.ceil(num_frames[0] / float(self.frames_per_chunk))
region = (offset, num_chunks, refs)
regions.append(region)
# Sets the offset for the next utterances
current_offset += num_chunks
return regions |
0, module; 1, function_definition; 2, function_name:write_separated_lines; 3, parameters; 4, block; 5, identifier:path; 6, identifier:values; 7, default_parameter; 8, default_parameter; 9, expression_statement; 10, expression_statement; 11, if_statement; 12, expression_statement; 13, identifier:separator; 14, string; 15, identifier:sort_by_column; 16, integer:0; 17, comment:"""
Writes list or dict to file line by line. Dict can have list as value then they written
separated on the line.
Parameters:
path (str): Path to write file to.
values (dict, list): A dictionary or a list to write to the file.
separator (str): Separator to use between columns.
sort_by_column (int): if >= 0, sorts the list by the given index, if its 0 or 1 and its a
dictionary it sorts it by either the key (0) or value (1). By default
0, meaning sorted by the first column or the key.
"""; 18, assignment; 19, comparison_operator:type(values) is dict; 20, block; 21, elif_clause; 22, call; 23, string_content:; 24, identifier:f; 25, call; 26, call; 27, identifier:dict; 28, if_statement; 29, for_statement; 30, boolean_operator; 31, block; 32, attribute; 33, argument_list; 34, identifier:open; 35, argument_list; 36, identifier:type; 37, argument_list; 38, comparison_operator:sort_by_column in [0, 1]; 39, block; 40, else_clause; 41, pattern_list; 42, identifier:items; 43, block; 44, comparison_operator:type(values) is list; 45, comparison_operator:type(values) is set; 46, if_statement; 47, for_statement; 48, identifier:f; 49, identifier:close; 50, identifier:path; 51, string; 52, keyword_argument; 53, identifier:values; 54, identifier:sort_by_column; 55, list; 56, expression_statement; 57, block; 58, identifier:key; 59, identifier:value; 60, if_statement; 61, expression_statement; 62, call; 63, identifier:list; 64, call; 65, identifier:set; 66, comparison_operator:0 <= sort_by_column < len(values); 67, block; 68, else_clause; 69, identifier:record; 70, identifier:items; 71, block; 72, string_content:w; 73, identifier:encoding; 74, string; 75, integer:0; 76, integer:1; 77, assignment; 78, expression_statement; 79, comparison_operator:type(value) in [list, set]; 80, block; 81, call; 82, identifier:type; 83, argument_list; 84, identifier:type; 85, argument_list; 86, integer:0; 87, identifier:sort_by_column; 88, call; 89, expression_statement; 90, block; 91, expression_statement; 92, expression_statement; 93, string_content:utf-8; 94, identifier:items; 95, call; 96, assignment; 97, call; 98, list; 99, expression_statement; 100, attribute; 101, argument_list; 102, identifier:values; 103, identifier:values; 104, identifier:len; 105, argument_list; 106, assignment; 107, expression_statement; 108, assignment; 109, call; 110, identifier:sorted; 111, argument_list; 112, identifier:items; 113, call; 114, identifier:type; 115, argument_list; 116, identifier:list; 117, identifier:set; 118, assignment; 119, identifier:f; 120, identifier:write; 121, call; 122, identifier:values; 123, identifier:items; 124, call; 125, assignment; 126, identifier:str_values; 127, list_comprehension; 128, attribute; 129, argument_list; 130, call; 131, keyword_argument; 132, attribute; 133, argument_list; 134, identifier:value; 135, identifier:value; 136, call; 137, attribute; 138, argument_list; 139, identifier:sorted; 140, argument_list; 141, identifier:items; 142, identifier:values; 143, call; 144, for_in_clause; 145, identifier:f; 146, identifier:write; 147, call; 148, attribute; 149, argument_list; 150, identifier:key; 151, lambda; 152, identifier:values; 153, identifier:items; 154, attribute; 155, argument_list; 156, string; 157, identifier:format; 158, identifier:key; 159, identifier:separator; 160, identifier:value; 161, identifier:values; 162, identifier:str; 163, argument_list; 164, identifier:value; 165, identifier:record; 166, attribute; 167, argument_list; 168, identifier:values; 169, identifier:items; 170, lambda_parameters; 171, subscript; 172, identifier:separator; 173, identifier:join; 174, list_comprehension; 175, string_content; 176, identifier:value; 177, string; 178, identifier:format; 179, call; 180, identifier:t; 181, identifier:t; 182, identifier:sort_by_column; 183, call; 184, for_in_clause; 185, escape_sequence:\n; 186, string_content; 187, attribute; 188, argument_list; 189, identifier:str; 190, argument_list; 191, identifier:x; 192, identifier:value; 193, escape_sequence:\n; 194, identifier:separator; 195, identifier:join; 196, identifier:str_values; 197, identifier:x | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, 9; 4, 10; 4, 11; 4, 12; 7, 13; 7, 14; 8, 15; 8, 16; 9, 17; 10, 18; 11, 19; 11, 20; 11, 21; 12, 22; 14, 23; 18, 24; 18, 25; 19, 26; 19, 27; 20, 28; 20, 29; 21, 30; 21, 31; 22, 32; 22, 33; 25, 34; 25, 35; 26, 36; 26, 37; 28, 38; 28, 39; 28, 40; 29, 41; 29, 42; 29, 43; 30, 44; 30, 45; 31, 46; 31, 47; 32, 48; 32, 49; 35, 50; 35, 51; 35, 52; 37, 53; 38, 54; 38, 55; 39, 56; 40, 57; 41, 58; 41, 59; 43, 60; 43, 61; 44, 62; 44, 63; 45, 64; 45, 65; 46, 66; 46, 67; 46, 68; 47, 69; 47, 70; 47, 71; 51, 72; 52, 73; 52, 74; 55, 75; 55, 76; 56, 77; 57, 78; 60, 79; 60, 80; 61, 81; 62, 82; 62, 83; 64, 84; 64, 85; 66, 86; 66, 87; 66, 88; 67, 89; 68, 90; 71, 91; 71, 92; 74, 93; 77, 94; 77, 95; 78, 96; 79, 97; 79, 98; 80, 99; 81, 100; 81, 101; 83, 102; 85, 103; 88, 104; 88, 105; 89, 106; 90, 107; 91, 108; 92, 109; 95, 110; 95, 111; 96, 112; 96, 113; 97, 114; 97, 115; 98, 116; 98, 117; 99, 118; 100, 119; 100, 120; 101, 121; 105, 122; 106, 123; 106, 124; 107, 125; 108, 126; 108, 127; 109, 128; 109, 129; 111, 130; 111, 131; 113, 132; 113, 133; 115, 134; 118, 135; 118, 136; 121, 137; 121, 138; 124, 139; 124, 140; 125, 141; 125, 142; 127, 143; 127, 144; 128, 145; 128, 146; 129, 147; 130, 148; 130, 149; 131, 150; 131, 151; 132, 152; 132, 153; 136, 154; 136, 155; 137, 156; 137, 157; 138, 158; 138, 159; 138, 160; 140, 161; 143, 162; 143, 163; 144, 164; 144, 165; 147, 166; 147, 167; 148, 168; 148, 169; 151, 170; 151, 171; 154, 172; 154, 173; 155, 174; 156, 175; 163, 176; 166, 177; 166, 178; 167, 179; 170, 180; 171, 181; 171, 182; 174, 183; 174, 184; 175, 185; 177, 186; 179, 187; 179, 188; 183, 189; 183, 190; 184, 191; 184, 192; 186, 193; 187, 194; 187, 195; 188, 196; 190, 197 | def write_separated_lines(path, values, separator=' ', sort_by_column=0):
"""
Writes list or dict to file line by line. Dict can have list as value then they written
separated on the line.
Parameters:
path (str): Path to write file to.
values (dict, list): A dictionary or a list to write to the file.
separator (str): Separator to use between columns.
sort_by_column (int): if >= 0, sorts the list by the given index, if its 0 or 1 and its a
dictionary it sorts it by either the key (0) or value (1). By default
0, meaning sorted by the first column or the key.
"""
f = open(path, 'w', encoding='utf-8')
if type(values) is dict:
if sort_by_column in [0, 1]:
items = sorted(values.items(), key=lambda t: t[sort_by_column])
else:
items = values.items()
for key, value in items:
if type(value) in [list, set]:
value = separator.join([str(x) for x in value])
f.write('{}{}{}\n'.format(key, separator, value))
elif type(values) is list or type(values) is set:
if 0 <= sort_by_column < len(values):
items = sorted(values)
else:
items = values
for record in items:
str_values = [str(value) for value in record]
f.write('{}\n'.format(separator.join(str_values)))
f.close() |
0, module; 1, function_definition; 2, function_name:sort_set; 3, parameters; 4, block; 5, identifier:s; 6, expression_statement; 7, if_statement; 8, expression_statement; 9, if_statement; 10, return_statement; 11, comment:"""Return a sorted list of the contents of a set
This is intended to be used to iterate over world state, where you just need keys
to be in some deterministic order, but the sort order should be obvious from the key.
Non-strings come before strings and then tuples. Tuples compare element-wise as normal.
But ultimately all comparisons are between values' ``repr``.
This is memoized.
"""; 12, not_operator; 13, block; 14, assignment; 15, comparison_operator:s not in _sort_set_memo; 16, block; 17, subscript; 18, call; 19, raise_statement; 20, identifier:s; 21, call; 22, identifier:s; 23, identifier:_sort_set_memo; 24, expression_statement; 25, identifier:_sort_set_memo; 26, identifier:s; 27, identifier:isinstance; 28, argument_list; 29, call; 30, identifier:frozenset; 31, argument_list; 32, assignment; 33, identifier:s; 34, identifier:Set; 35, identifier:TypeError; 36, argument_list; 37, identifier:s; 38, subscript; 39, call; 40, string:"sets only"; 41, identifier:_sort_set_memo; 42, identifier:s; 43, identifier:sorted; 44, argument_list; 45, identifier:s; 46, keyword_argument; 47, identifier:key; 48, identifier:_sort_set_key | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 6, 11; 7, 12; 7, 13; 8, 14; 9, 15; 9, 16; 10, 17; 12, 18; 13, 19; 14, 20; 14, 21; 15, 22; 15, 23; 16, 24; 17, 25; 17, 26; 18, 27; 18, 28; 19, 29; 21, 30; 21, 31; 24, 32; 28, 33; 28, 34; 29, 35; 29, 36; 31, 37; 32, 38; 32, 39; 36, 40; 38, 41; 38, 42; 39, 43; 39, 44; 44, 45; 44, 46; 46, 47; 46, 48 | def sort_set(s):
"""Return a sorted list of the contents of a set
This is intended to be used to iterate over world state, where you just need keys
to be in some deterministic order, but the sort order should be obvious from the key.
Non-strings come before strings and then tuples. Tuples compare element-wise as normal.
But ultimately all comparisons are between values' ``repr``.
This is memoized.
"""
if not isinstance(s, Set):
raise TypeError("sets only")
s = frozenset(s)
if s not in _sort_set_memo:
_sort_set_memo[s] = sorted(s, key=_sort_set_key)
return _sort_set_memo[s] |
0, module; 1, function_definition; 2, function_name:parse_tags; 3, parameters; 4, block; 5, identifier:tagstring; 6, expression_statement; 7, if_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, comment:# Defer splitting of non-quoted sections until we know if there are; 12, comment:# any unquoted commas.; 13, expression_statement; 14, expression_statement; 15, try_statement; 16, if_statement; 17, expression_statement; 18, expression_statement; 19, return_statement; 20, comment:"""
Parses tag input, with multiple word input being activated and
delineated by commas and double quotes. Quotes take precedence, so
they may contain commas.
Returns a sorted list of unique tag names.
Adapted from Taggit, modified to not split strings on spaces.
Ported from Jonathan Buchanan's `django-tagging
<http://django-tagging.googlecode.com/>`_
"""; 21, not_operator; 22, block; 23, assignment; 24, assignment; 25, assignment; 26, assignment; 27, assignment; 28, block; 29, except_clause; 30, identifier:to_be_split; 31, block; 32, assignment; 33, call; 34, identifier:words; 35, identifier:tagstring; 36, return_statement; 37, identifier:tagstring; 38, call; 39, identifier:words; 40, list; 41, identifier:buffer; 42, list; 43, identifier:to_be_split; 44, list; 45, identifier:i; 46, call; 47, while_statement; 48, identifier:StopIteration; 49, comment:# If we were parsing an open quote which was never closed treat; 50, comment:# the buffer as unquoted.; 51, block; 52, for_statement; 53, identifier:words; 54, call; 55, attribute; 56, argument_list; 57, list; 58, identifier:force_text; 59, argument_list; 60, identifier:iter; 61, argument_list; 62, True; 63, block; 64, if_statement; 65, identifier:chunk; 66, identifier:to_be_split; 67, block; 68, identifier:list; 69, argument_list; 70, identifier:words; 71, identifier:sort; 72, identifier:tagstring; 73, identifier:tagstring; 74, expression_statement; 75, if_statement; 76, identifier:buffer; 77, block; 78, expression_statement; 79, call; 80, assignment; 81, comparison_operator:c == '"'; 82, block; 83, else_clause; 84, expression_statement; 85, call; 86, identifier:set; 87, argument_list; 88, identifier:c; 89, call; 90, identifier:c; 91, string:'"'; 92, if_statement; 93, expression_statement; 94, while_statement; 95, if_statement; 96, block; 97, call; 98, attribute; 99, argument_list; 100, identifier:words; 101, attribute; 102, argument_list; 103, identifier:buffer; 104, block; 105, assignment; 106, comparison_operator:c != '"'; 107, block; 108, identifier:buffer; 109, block; 110, expression_statement; 111, attribute; 112, argument_list; 113, identifier:words; 114, identifier:extend; 115, call; 116, identifier:six; 117, identifier:next; 118, identifier:i; 119, expression_statement; 120, expression_statement; 121, identifier:c; 122, call; 123, identifier:c; 124, string:'"'; 125, expression_statement; 126, expression_statement; 127, expression_statement; 128, if_statement; 129, expression_statement; 130, call; 131, identifier:to_be_split; 132, identifier:append; 133, call; 134, identifier:split_strip; 135, argument_list; 136, call; 137, assignment; 138, attribute; 139, argument_list; 140, call; 141, assignment; 142, assignment; 143, identifier:word; 144, block; 145, assignment; 146, attribute; 147, argument_list; 148, attribute; 149, argument_list; 150, identifier:chunk; 151, subscript; 152, attribute; 153, argument_list; 154, identifier:buffer; 155, list; 156, identifier:six; 157, identifier:next; 158, identifier:i; 159, attribute; 160, argument_list; 161, identifier:c; 162, call; 163, identifier:word; 164, call; 165, expression_statement; 166, identifier:buffer; 167, list; 168, identifier:buffer; 169, identifier:append; 170, identifier:c; 171, string; 172, identifier:join; 173, identifier:buffer; 174, attribute; 175, string; 176, identifier:to_be_split; 177, identifier:append; 178, call; 179, identifier:buffer; 180, identifier:append; 181, identifier:c; 182, attribute; 183, argument_list; 184, attribute; 185, argument_list; 186, call; 187, identifier:settings; 188, identifier:TAGGIT_SELECTIZE; 189, string_content:DELIMITER; 190, attribute; 191, argument_list; 192, identifier:six; 193, identifier:next; 194, identifier:i; 195, call; 196, identifier:strip; 197, attribute; 198, argument_list; 199, string; 200, identifier:join; 201, identifier:buffer; 202, attribute; 203, argument_list; 204, identifier:words; 205, identifier:append; 206, identifier:word; 207, string; 208, identifier:join; 209, identifier:buffer | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 4, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 4, 17; 4, 18; 4, 19; 6, 20; 7, 21; 7, 22; 8, 23; 9, 24; 10, 25; 13, 26; 14, 27; 15, 28; 15, 29; 16, 30; 16, 31; 17, 32; 18, 33; 19, 34; 21, 35; 22, 36; 23, 37; 23, 38; 24, 39; 24, 40; 25, 41; 25, 42; 26, 43; 26, 44; 27, 45; 27, 46; 28, 47; 29, 48; 29, 49; 29, 50; 29, 51; 31, 52; 32, 53; 32, 54; 33, 55; 33, 56; 36, 57; 38, 58; 38, 59; 46, 60; 46, 61; 47, 62; 47, 63; 51, 64; 52, 65; 52, 66; 52, 67; 54, 68; 54, 69; 55, 70; 55, 71; 59, 72; 61, 73; 63, 74; 63, 75; 64, 76; 64, 77; 67, 78; 69, 79; 74, 80; 75, 81; 75, 82; 75, 83; 77, 84; 78, 85; 79, 86; 79, 87; 80, 88; 80, 89; 81, 90; 81, 91; 82, 92; 82, 93; 82, 94; 82, 95; 83, 96; 84, 97; 85, 98; 85, 99; 87, 100; 89, 101; 89, 102; 92, 103; 92, 104; 93, 105; 94, 106; 94, 107; 95, 108; 95, 109; 96, 110; 97, 111; 97, 112; 98, 113; 98, 114; 99, 115; 101, 116; 101, 117; 102, 118; 104, 119; 104, 120; 105, 121; 105, 122; 106, 123; 106, 124; 107, 125; 107, 126; 109, 127; 109, 128; 109, 129; 110, 130; 111, 131; 111, 132; 112, 133; 115, 134; 115, 135; 119, 136; 120, 137; 122, 138; 122, 139; 125, 140; 126, 141; 127, 142; 128, 143; 128, 144; 129, 145; 130, 146; 130, 147; 133, 148; 133, 149; 135, 150; 135, 151; 136, 152; 136, 153; 137, 154; 137, 155; 138, 156; 138, 157; 139, 158; 140, 159; 140, 160; 141, 161; 141, 162; 142, 163; 142, 164; 144, 165; 145, 166; 145, 167; 146, 168; 146, 169; 147, 170; 148, 171; 148, 172; 149, 173; 151, 174; 151, 175; 152, 176; 152, 177; 153, 178; 159, 179; 159, 180; 160, 181; 162, 182; 162, 183; 164, 184; 164, 185; 165, 186; 174, 187; 174, 188; 175, 189; 178, 190; 178, 191; 182, 192; 182, 193; 183, 194; 184, 195; 184, 196; 186, 197; 186, 198; 190, 199; 190, 200; 191, 201; 195, 202; 195, 203; 197, 204; 197, 205; 198, 206; 202, 207; 202, 208; 203, 209 | def parse_tags(tagstring):
"""
Parses tag input, with multiple word input being activated and
delineated by commas and double quotes. Quotes take precedence, so
they may contain commas.
Returns a sorted list of unique tag names.
Adapted from Taggit, modified to not split strings on spaces.
Ported from Jonathan Buchanan's `django-tagging
<http://django-tagging.googlecode.com/>`_
"""
if not tagstring:
return []
tagstring = force_text(tagstring)
words = []
buffer = []
# Defer splitting of non-quoted sections until we know if there are
# any unquoted commas.
to_be_split = []
i = iter(tagstring)
try:
while True:
c = six.next(i)
if c == '"':
if buffer:
to_be_split.append(''.join(buffer))
buffer = []
c = six.next(i)
while c != '"':
buffer.append(c)
c = six.next(i)
if buffer:
word = ''.join(buffer).strip()
if word:
words.append(word)
buffer = []
else:
buffer.append(c)
except StopIteration:
# If we were parsing an open quote which was never closed treat
# the buffer as unquoted.
if buffer:
to_be_split.append(''.join(buffer))
if to_be_split:
for chunk in to_be_split:
words.extend(split_strip(chunk, settings.TAGGIT_SELECTIZE['DELIMITER']))
words = list(set(words))
words.sort()
return words |
0, module; 1, function_definition; 2, function_name:_sort_by_unique_fields; 3, parameters; 4, block; 5, identifier:model; 6, identifier:model_objs; 7, identifier:unique_fields; 8, expression_statement; 9, expression_statement; 10, function_definition; 11, return_statement; 12, comment:"""
Sort a list of models by their unique fields.
Sorting models in an upsert greatly reduces the chances of deadlock
when doing concurrent upserts
"""; 13, assignment; 14, function_name:sort_key; 15, parameters; 16, block; 17, call; 18, identifier:unique_fields; 19, list_comprehension; 20, identifier:model_obj; 21, return_statement; 22, identifier:sorted; 23, argument_list; 24, identifier:field; 25, for_in_clause; 26, if_clause; 27, call; 28, identifier:model_objs; 29, keyword_argument; 30, identifier:field; 31, attribute; 32, comparison_operator:field.attname in unique_fields; 33, identifier:tuple; 34, generator_expression; 35, identifier:key; 36, identifier:sort_key; 37, attribute; 38, identifier:fields; 39, attribute; 40, identifier:unique_fields; 41, call; 42, for_in_clause; 43, identifier:model; 44, identifier:_meta; 45, identifier:field; 46, identifier:attname; 47, attribute; 48, argument_list; 49, identifier:field; 50, identifier:unique_fields; 51, identifier:field; 52, identifier:get_db_prep_save; 53, call; 54, identifier:connection; 55, identifier:getattr; 56, argument_list; 57, identifier:model_obj; 58, attribute; 59, identifier:field; 60, identifier:attname | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 8, 12; 9, 13; 10, 14; 10, 15; 10, 16; 11, 17; 13, 18; 13, 19; 15, 20; 16, 21; 17, 22; 17, 23; 19, 24; 19, 25; 19, 26; 21, 27; 23, 28; 23, 29; 25, 30; 25, 31; 26, 32; 27, 33; 27, 34; 29, 35; 29, 36; 31, 37; 31, 38; 32, 39; 32, 40; 34, 41; 34, 42; 37, 43; 37, 44; 39, 45; 39, 46; 41, 47; 41, 48; 42, 49; 42, 50; 47, 51; 47, 52; 48, 53; 48, 54; 53, 55; 53, 56; 56, 57; 56, 58; 58, 59; 58, 60 | def _sort_by_unique_fields(model, model_objs, unique_fields):
"""
Sort a list of models by their unique fields.
Sorting models in an upsert greatly reduces the chances of deadlock
when doing concurrent upserts
"""
unique_fields = [
field for field in model._meta.fields
if field.attname in unique_fields
]
def sort_key(model_obj):
return tuple(
field.get_db_prep_save(getattr(model_obj, field.attname),
connection)
for field in unique_fields
)
return sorted(model_objs, key=sort_key) |
0, module; 1, function_definition; 2, function_name:find; 3, parameters; 4, block; 5, identifier:self; 6, identifier:datum; 7, expression_statement; 8, if_statement; 9, if_statement; 10, return_statement; 11, comment:"""Return sorted value of This if list or dict."""; 12, boolean_operator; 13, block; 14, boolean_operator; 15, block; 16, identifier:datum; 17, call; 18, attribute; 19, return_statement; 20, call; 21, call; 22, expression_statement; 23, return_statement; 24, identifier:isinstance; 25, argument_list; 26, identifier:self; 27, identifier:expressions; 28, identifier:datum; 29, identifier:isinstance; 30, argument_list; 31, identifier:isinstance; 32, argument_list; 33, assignment; 34, list; 35, attribute; 36, identifier:dict; 37, attribute; 38, identifier:dict; 39, attribute; 40, identifier:list; 41, identifier:key; 42, parenthesized_expression; 43, call; 44, identifier:datum; 45, identifier:value; 46, identifier:datum; 47, identifier:value; 48, identifier:datum; 49, identifier:value; 50, conditional_expression:functools.cmp_to_key(self._compare)
if self.expressions else None; 51, attribute; 52, argument_list; 53, call; 54, attribute; 55, None; 56, attribute; 57, identifier:wrap; 58, list_comprehension; 59, attribute; 60, argument_list; 61, identifier:self; 62, identifier:expressions; 63, identifier:jsonpath_rw; 64, identifier:DatumInContext; 65, identifier:value; 66, for_in_clause; 67, identifier:functools; 68, identifier:cmp_to_key; 69, attribute; 70, identifier:value; 71, call; 72, identifier:self; 73, identifier:_compare; 74, identifier:sorted; 75, argument_list; 76, attribute; 77, keyword_argument; 78, identifier:datum; 79, identifier:value; 80, identifier:key; 81, identifier:key | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 7, 11; 8, 12; 8, 13; 9, 14; 9, 15; 10, 16; 12, 17; 12, 18; 13, 19; 14, 20; 14, 21; 15, 22; 15, 23; 17, 24; 17, 25; 18, 26; 18, 27; 19, 28; 20, 29; 20, 30; 21, 31; 21, 32; 22, 33; 23, 34; 25, 35; 25, 36; 30, 37; 30, 38; 32, 39; 32, 40; 33, 41; 33, 42; 34, 43; 35, 44; 35, 45; 37, 46; 37, 47; 39, 48; 39, 49; 42, 50; 43, 51; 43, 52; 50, 53; 50, 54; 50, 55; 51, 56; 51, 57; 52, 58; 53, 59; 53, 60; 54, 61; 54, 62; 56, 63; 56, 64; 58, 65; 58, 66; 59, 67; 59, 68; 60, 69; 66, 70; 66, 71; 69, 72; 69, 73; 71, 74; 71, 75; 75, 76; 75, 77; 76, 78; 76, 79; 77, 80; 77, 81 | def find(self, datum):
"""Return sorted value of This if list or dict."""
if isinstance(datum.value, dict) and self.expressions:
return datum
if isinstance(datum.value, dict) or isinstance(datum.value, list):
key = (functools.cmp_to_key(self._compare)
if self.expressions else None)
return [jsonpath_rw.DatumInContext.wrap(
[value for value in sorted(datum.value, key=key)])]
return datum |
0, module; 1, function_definition; 2, function_name:get_subset_riverid_index_list; 3, parameters; 4, block; 5, identifier:self; 6, identifier:river_id_list; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, return_statement; 16, comment:"""
Gets the subset riverid_list from the netcdf file
Optional returns include the list of valid river ids in the dataset
as well as a list of missing rive rids
Parameters
----------
river_id_list: list or :obj:`numpy.array`
Array of river ID's for the river segments you want the index of.
Returns
-------
:obj:`numpy.array`
A sorted array of the river index in the NetCDF file that
were found.
:obj:`numpy.array`
A sorted array of the river IDs that were found.
list
An array of the missing river ids.
"""; 17, assignment; 18, assignment; 19, assignment; 20, identifier:river_id; 21, identifier:river_id_list; 22, comment:# get where streamids are in netcdf file; 23, block; 24, assignment; 25, assignment; 26, assignment; 27, tuple; 28, identifier:netcdf_river_indices_list; 29, list; 30, identifier:valid_river_ids; 31, list; 32, identifier:missing_river_ids; 33, list; 34, try_statement; 35, identifier:np_valid_river_indices_list; 36, call; 37, identifier:np_valid_river_ids; 38, call; 39, identifier:sorted_indexes; 40, call; 41, subscript; 42, subscript; 43, call; 44, block; 45, except_clause; 46, attribute; 47, argument_list; 48, attribute; 49, argument_list; 50, attribute; 51, argument_list; 52, identifier:np_valid_river_indices_list; 53, identifier:sorted_indexes; 54, identifier:np_valid_river_ids; 55, identifier:sorted_indexes; 56, attribute; 57, argument_list; 58, expression_statement; 59, expression_statement; 60, identifier:IndexError; 61, block; 62, identifier:np; 63, identifier:array; 64, identifier:netcdf_river_indices_list; 65, identifier:np; 66, identifier:array; 67, identifier:valid_river_ids; 68, identifier:np; 69, identifier:argsort; 70, identifier:np_valid_river_indices_list; 71, identifier:np; 72, identifier:array; 73, identifier:missing_river_ids; 74, call; 75, call; 76, expression_statement; 77, expression_statement; 78, attribute; 79, argument_list; 80, attribute; 81, argument_list; 82, call; 83, call; 84, identifier:netcdf_river_indices_list; 85, line_continuation:\; 86, identifier:append; 87, call; 88, identifier:valid_river_ids; 89, identifier:append; 90, identifier:river_id; 91, identifier:log; 92, argument_list; 93, attribute; 94, argument_list; 95, attribute; 96, argument_list; 97, call; 98, string:"WARNING"; 99, identifier:missing_river_ids; 100, identifier:append; 101, identifier:river_id; 102, identifier:self; 103, identifier:get_river_index; 104, identifier:river_id; 105, attribute; 106, argument_list; 107, concatenated_string; 108, identifier:format; 109, identifier:river_id; 110, string:"ReachID {0} not found in netCDF dataset."; 111, string:" Skipping ..." | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 4, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 7, 16; 8, 17; 9, 18; 10, 19; 11, 20; 11, 21; 11, 22; 11, 23; 12, 24; 13, 25; 14, 26; 15, 27; 17, 28; 17, 29; 18, 30; 18, 31; 19, 32; 19, 33; 23, 34; 24, 35; 24, 36; 25, 37; 25, 38; 26, 39; 26, 40; 27, 41; 27, 42; 27, 43; 34, 44; 34, 45; 36, 46; 36, 47; 38, 48; 38, 49; 40, 50; 40, 51; 41, 52; 41, 53; 42, 54; 42, 55; 43, 56; 43, 57; 44, 58; 44, 59; 45, 60; 45, 61; 46, 62; 46, 63; 47, 64; 48, 65; 48, 66; 49, 67; 50, 68; 50, 69; 51, 70; 56, 71; 56, 72; 57, 73; 58, 74; 59, 75; 61, 76; 61, 77; 74, 78; 74, 79; 75, 80; 75, 81; 76, 82; 77, 83; 78, 84; 78, 85; 78, 86; 79, 87; 80, 88; 80, 89; 81, 90; 82, 91; 82, 92; 83, 93; 83, 94; 87, 95; 87, 96; 92, 97; 92, 98; 93, 99; 93, 100; 94, 101; 95, 102; 95, 103; 96, 104; 97, 105; 97, 106; 105, 107; 105, 108; 106, 109; 107, 110; 107, 111 | def get_subset_riverid_index_list(self, river_id_list):
"""
Gets the subset riverid_list from the netcdf file
Optional returns include the list of valid river ids in the dataset
as well as a list of missing rive rids
Parameters
----------
river_id_list: list or :obj:`numpy.array`
Array of river ID's for the river segments you want the index of.
Returns
-------
:obj:`numpy.array`
A sorted array of the river index in the NetCDF file that
were found.
:obj:`numpy.array`
A sorted array of the river IDs that were found.
list
An array of the missing river ids.
"""
netcdf_river_indices_list = []
valid_river_ids = []
missing_river_ids = []
for river_id in river_id_list:
# get where streamids are in netcdf file
try:
netcdf_river_indices_list \
.append(self.get_river_index(river_id))
valid_river_ids.append(river_id)
except IndexError:
log("ReachID {0} not found in netCDF dataset."
" Skipping ...".format(river_id),
"WARNING")
missing_river_ids.append(river_id)
np_valid_river_indices_list = np.array(netcdf_river_indices_list)
np_valid_river_ids = np.array(valid_river_ids)
sorted_indexes = np.argsort(np_valid_river_indices_list)
return(np_valid_river_indices_list[sorted_indexes],
np_valid_river_ids[sorted_indexes],
np.array(missing_river_ids)) |
0, module; 1, function_definition; 2, function_name:get_child_type_choices; 3, parameters; 4, block; 5, identifier:self; 6, identifier:request; 7, identifier:action; 8, expression_statement; 9, comment:# Get choices from the super class to check permissions.; 10, expression_statement; 11, comment:# Update label with verbose name from plugins.; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, if_statement; 16, return_statement; 17, comment:"""
Override choice labels with ``verbose_name`` from plugins and sort.
"""; 18, assignment; 19, assignment; 20, assignment; 21, assignment; 22, identifier:plugins; 23, block; 24, identifier:choices; 25, identifier:choices; 26, call; 27, identifier:plugins; 28, call; 29, identifier:labels; 30, dictionary; 31, identifier:sort_priorities; 32, dictionary; 33, for_statement; 34, expression_statement; 35, return_statement; 36, attribute; 37, argument_list; 38, attribute; 39, argument_list; 40, identifier:plugin; 41, identifier:plugins; 42, block; 43, assignment; 44, call; 45, call; 46, line_continuation:\; 47, identifier:get_child_type_choices; 48, identifier:request; 49, identifier:action; 50, attribute; 51, identifier:get_plugins; 52, expression_statement; 53, expression_statement; 54, expression_statement; 55, identifier:choices; 56, list_comprehension; 57, identifier:sorted; 58, argument_list; 59, identifier:super; 60, argument_list; 61, identifier:self; 62, identifier:child_model_plugin_class; 63, assignment; 64, assignment; 65, assignment; 66, tuple; 67, for_in_clause; 68, identifier:choices; 69, keyword_argument; 70, identifier:ChildModelPluginPolymorphicParentModelAdmin; 71, identifier:self; 72, identifier:pk; 73, attribute; 74, subscript; 75, call; 76, subscript; 77, call; 78, identifier:ctype; 79, subscript; 80, pattern_list; 81, identifier:choices; 82, identifier:cmp; 83, lambda; 84, attribute; 85, identifier:pk; 86, identifier:labels; 87, identifier:pk; 88, identifier:capfirst; 89, argument_list; 90, identifier:sort_priorities; 91, identifier:pk; 92, identifier:getattr; 93, argument_list; 94, identifier:labels; 95, identifier:ctype; 96, identifier:ctype; 97, identifier:_; 98, lambda_parameters; 99, call; 100, identifier:plugin; 101, identifier:content_type; 102, attribute; 103, identifier:plugin; 104, string; 105, subscript; 106, identifier:a; 107, identifier:b; 108, identifier:cmp; 109, argument_list; 110, identifier:plugin; 111, identifier:verbose_name; 112, string_content:sort_priority; 113, identifier:labels; 114, identifier:pk; 115, subscript; 116, subscript; 117, identifier:sort_priorities; 118, subscript; 119, identifier:sort_priorities; 120, subscript; 121, identifier:a; 122, integer:0; 123, identifier:b; 124, integer:0 | 0, 1; 1, 2; 1, 3; 1, 4; 3, 5; 3, 6; 3, 7; 4, 8; 4, 9; 4, 10; 4, 11; 4, 12; 4, 13; 4, 14; 4, 15; 4, 16; 8, 17; 10, 18; 12, 19; 13, 20; 14, 21; 15, 22; 15, 23; 16, 24; 18, 25; 18, 26; 19, 27; 19, 28; 20, 29; 20, 30; 21, 31; 21, 32; 23, 33; 23, 34; 23, 35; 26, 36; 26, 37; 28, 38; 28, 39; 33, 40; 33, 41; 33, 42; 34, 43; 35, 44; 36, 45; 36, 46; 36, 47; 37, 48; 37, 49; 38, 50; 38, 51; 42, 52; 42, 53; 42, 54; 43, 55; 43, 56; 44, 57; 44, 58; 45, 59; 45, 60; 50, 61; 50, 62; 52, 63; 53, 64; 54, 65; 56, 66; 56, 67; 58, 68; 58, 69; 60, 70; 60, 71; 63, 72; 63, 73; 64, 74; 64, 75; 65, 76; 65, 77; 66, 78; 66, 79; 67, 80; 67, 81; 69, 82; 69, 83; 73, 84; 73, 85; 74, 86; 74, 87; 75, 88; 75, 89; 76, 90; 76, 91; 77, 92; 77, 93; 79, 94; 79, 95; 80, 96; 80, 97; 83, 98; 83, 99; 84, 100; 84, 101; 89, 102; 93, 103; 93, 104; 93, 105; 98, 106; 98, 107; 99, 108; 99, 109; 102, 110; 102, 111; 104, 112; 105, 113; 105, 114; 109, 115; 109, 116; 115, 117; 115, 118; 116, 119; 116, 120; 118, 121; 118, 122; 120, 123; 120, 124 | def get_child_type_choices(self, request, action):
"""
Override choice labels with ``verbose_name`` from plugins and sort.
"""
# Get choices from the super class to check permissions.
choices = super(ChildModelPluginPolymorphicParentModelAdmin, self) \
.get_child_type_choices(request, action)
# Update label with verbose name from plugins.
plugins = self.child_model_plugin_class.get_plugins()
labels = {}
sort_priorities = {}
if plugins:
for plugin in plugins:
pk = plugin.content_type.pk
labels[pk] = capfirst(plugin.verbose_name)
sort_priorities[pk] = getattr(plugin, 'sort_priority', labels[pk])
choices = [(ctype, labels[ctype]) for ctype, _ in choices]
return sorted(choices,
cmp=lambda a, b: cmp(
sort_priorities[a[0]],
sort_priorities[b[0]]
)
)
return choices |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.