text
stringlengths 75
104k
| code_tokens
sequence | avg_line_len
float64 7.91
980
| score
float64 0
0.18
|
---|---|---|---|
def save_group(self, group):
""" Group was saved. """
# If group already exists, take over existing group rather then error.
try:
lgroup = self._get_group(group.name)
changes = changeset(lgroup, {})
except ObjectDoesNotExist:
lgroup = self._group_class()
changes = changeset(lgroup, {
'cn': group.name,
})
changes = changes.merge({
'description': group.description
})
save(changes, database=self._database) | [
"def",
"save_group",
"(",
"self",
",",
"group",
")",
":",
"# If group already exists, take over existing group rather then error.",
"try",
":",
"lgroup",
"=",
"self",
".",
"_get_group",
"(",
"group",
".",
"name",
")",
"changes",
"=",
"changeset",
"(",
"lgroup",
",",
"{",
"}",
")",
"except",
"ObjectDoesNotExist",
":",
"lgroup",
"=",
"self",
".",
"_group_class",
"(",
")",
"changes",
"=",
"changeset",
"(",
"lgroup",
",",
"{",
"'cn'",
":",
"group",
".",
"name",
",",
"}",
")",
"changes",
"=",
"changes",
".",
"merge",
"(",
"{",
"'description'",
":",
"group",
".",
"description",
"}",
")",
"save",
"(",
"changes",
",",
"database",
"=",
"self",
".",
"_database",
")"
] | 33.5 | 0.00363 |
def get(self):
"""Gets the next item from the queue.
Returns a Future that resolves to the next item once it is available.
"""
io_loop = IOLoop.current()
new_get = Future()
with self._lock:
get, self._get = self._get, new_get
answer = Future()
def _on_node(future):
if future.exception(): # pragma: no cover (never happens)
return answer.set_exc_info(future.exc_info())
node = future.result()
value = node.value
new_hole, node.next = node.next, None
new_get.set_result(new_hole)
answer.set_result(value)
def _on_get(future):
if future.exception(): # pragma: no cover (never happens)
return answer.set_exc_info(future.exc_info())
hole = future.result()
io_loop.add_future(hole, _on_node)
io_loop.add_future(get, _on_get)
return answer | [
"def",
"get",
"(",
"self",
")",
":",
"io_loop",
"=",
"IOLoop",
".",
"current",
"(",
")",
"new_get",
"=",
"Future",
"(",
")",
"with",
"self",
".",
"_lock",
":",
"get",
",",
"self",
".",
"_get",
"=",
"self",
".",
"_get",
",",
"new_get",
"answer",
"=",
"Future",
"(",
")",
"def",
"_on_node",
"(",
"future",
")",
":",
"if",
"future",
".",
"exception",
"(",
")",
":",
"# pragma: no cover (never happens)",
"return",
"answer",
".",
"set_exc_info",
"(",
"future",
".",
"exc_info",
"(",
")",
")",
"node",
"=",
"future",
".",
"result",
"(",
")",
"value",
"=",
"node",
".",
"value",
"new_hole",
",",
"node",
".",
"next",
"=",
"node",
".",
"next",
",",
"None",
"new_get",
".",
"set_result",
"(",
"new_hole",
")",
"answer",
".",
"set_result",
"(",
"value",
")",
"def",
"_on_get",
"(",
"future",
")",
":",
"if",
"future",
".",
"exception",
"(",
")",
":",
"# pragma: no cover (never happens)",
"return",
"answer",
".",
"set_exc_info",
"(",
"future",
".",
"exc_info",
"(",
")",
")",
"hole",
"=",
"future",
".",
"result",
"(",
")",
"io_loop",
".",
"add_future",
"(",
"hole",
",",
"_on_node",
")",
"io_loop",
".",
"add_future",
"(",
"get",
",",
"_on_get",
")",
"return",
"answer"
] | 29.75 | 0.002035 |
def structure(self, obj, cl):
# type: (Any, Type[T]) -> T
"""Convert unstructured Python data structures to structured data."""
return self._structure_func.dispatch(cl)(obj, cl) | [
"def",
"structure",
"(",
"self",
",",
"obj",
",",
"cl",
")",
":",
"# type: (Any, Type[T]) -> T",
"return",
"self",
".",
"_structure_func",
".",
"dispatch",
"(",
"cl",
")",
"(",
"obj",
",",
"cl",
")"
] | 39.6 | 0.014851 |
def file_to_str(fname):
"""
Read a file into a string
PRE: fname is a small file (to avoid hogging memory and its discontents)
"""
data = None
# rU = read with Universal line terminator
with open(fname, 'rU') as fd:
data = fd.read()
return data | [
"def",
"file_to_str",
"(",
"fname",
")",
":",
"data",
"=",
"None",
"# rU = read with Universal line terminator",
"with",
"open",
"(",
"fname",
",",
"'rU'",
")",
"as",
"fd",
":",
"data",
"=",
"fd",
".",
"read",
"(",
")",
"return",
"data"
] | 27.5 | 0.003521 |
def round_to_x_digits(number, digits):
"""
Returns 'number' rounded to 'digits' digits.
"""
return round(number * math.pow(10, digits)) / math.pow(10, digits) | [
"def",
"round_to_x_digits",
"(",
"number",
",",
"digits",
")",
":",
"return",
"round",
"(",
"number",
"*",
"math",
".",
"pow",
"(",
"10",
",",
"digits",
")",
")",
"/",
"math",
".",
"pow",
"(",
"10",
",",
"digits",
")"
] | 34 | 0.005747 |
def get_sdc_by_name(self, name):
"""
Get ScaleIO SDC object by its name
:param name: Name of SDC
:return: ScaleIO SDC object
:raise KeyError: No SDC with specified name found
:rtype: SDC object
"""
for sdc in self.sdc:
if sdc.name == name:
return sdc
raise KeyError("SDC of that name not found") | [
"def",
"get_sdc_by_name",
"(",
"self",
",",
"name",
")",
":",
"for",
"sdc",
"in",
"self",
".",
"sdc",
":",
"if",
"sdc",
".",
"name",
"==",
"name",
":",
"return",
"sdc",
"raise",
"KeyError",
"(",
"\"SDC of that name not found\"",
")"
] | 32 | 0.005063 |
def _parse_impute2_line(self, line):
"""Parses the current IMPUTE2 line (a single variant).
Args:
line (str): An IMPUTE2 line.
Returns:
Genotypes: The genotype in dosage format.
Warning
=======
By default, the genotypes object has multiallelic set to False.
"""
# Splitting
row = line.rstrip("\r\n").split(" ")
# Constructing the probabilities
prob = np.array(row[5:], dtype=float)
prob.shape = (prob.shape[0] // 3, 3)
# Constructing the dosage
dosage = 2 * prob[:, 2] + prob[:, 1]
if self.prob_t > 0:
dosage[~np.any(prob >= self.prob_t, axis=1)] = np.nan
return Genotypes(
Variant(row[1], CHROM_STR_ENCODE.get(row[0], row[0]), int(row[2]),
[row[3], row[4]]),
dosage,
reference=row[3],
coded=row[4],
multiallelic=False,
) | [
"def",
"_parse_impute2_line",
"(",
"self",
",",
"line",
")",
":",
"# Splitting",
"row",
"=",
"line",
".",
"rstrip",
"(",
"\"\\r\\n\"",
")",
".",
"split",
"(",
"\" \"",
")",
"# Constructing the probabilities",
"prob",
"=",
"np",
".",
"array",
"(",
"row",
"[",
"5",
":",
"]",
",",
"dtype",
"=",
"float",
")",
"prob",
".",
"shape",
"=",
"(",
"prob",
".",
"shape",
"[",
"0",
"]",
"//",
"3",
",",
"3",
")",
"# Constructing the dosage",
"dosage",
"=",
"2",
"*",
"prob",
"[",
":",
",",
"2",
"]",
"+",
"prob",
"[",
":",
",",
"1",
"]",
"if",
"self",
".",
"prob_t",
">",
"0",
":",
"dosage",
"[",
"~",
"np",
".",
"any",
"(",
"prob",
">=",
"self",
".",
"prob_t",
",",
"axis",
"=",
"1",
")",
"]",
"=",
"np",
".",
"nan",
"return",
"Genotypes",
"(",
"Variant",
"(",
"row",
"[",
"1",
"]",
",",
"CHROM_STR_ENCODE",
".",
"get",
"(",
"row",
"[",
"0",
"]",
",",
"row",
"[",
"0",
"]",
")",
",",
"int",
"(",
"row",
"[",
"2",
"]",
")",
",",
"[",
"row",
"[",
"3",
"]",
",",
"row",
"[",
"4",
"]",
"]",
")",
",",
"dosage",
",",
"reference",
"=",
"row",
"[",
"3",
"]",
",",
"coded",
"=",
"row",
"[",
"4",
"]",
",",
"multiallelic",
"=",
"False",
",",
")"
] | 27.970588 | 0.002033 |
def _seed(self, seed=-1):
"""
Initialize the random seed
"""
if seed != -1:
self.random = NupicRandom(seed)
else:
self.random = NupicRandom() | [
"def",
"_seed",
"(",
"self",
",",
"seed",
"=",
"-",
"1",
")",
":",
"if",
"seed",
"!=",
"-",
"1",
":",
"self",
".",
"random",
"=",
"NupicRandom",
"(",
"seed",
")",
"else",
":",
"self",
".",
"random",
"=",
"NupicRandom",
"(",
")"
] | 20.75 | 0.017341 |
def fail(self, tup):
"""Indicate that processing of a Tuple has failed.
:param tup: the Tuple to fail (its ``id`` if ``str``).
:type tup: :class:`str` or :class:`pystorm.component.Tuple`
"""
tup_id = tup.id if isinstance(tup, Tuple) else tup
self.send_message({"command": "fail", "id": tup_id}) | [
"def",
"fail",
"(",
"self",
",",
"tup",
")",
":",
"tup_id",
"=",
"tup",
".",
"id",
"if",
"isinstance",
"(",
"tup",
",",
"Tuple",
")",
"else",
"tup",
"self",
".",
"send_message",
"(",
"{",
"\"command\"",
":",
"\"fail\"",
",",
"\"id\"",
":",
"tup_id",
"}",
")"
] | 42 | 0.005831 |
def _oct_to_dec(ip, check=True):
"""Octal to decimal conversion."""
if check and not is_oct(ip):
raise ValueError('_oct_to_dec: invalid IP: "%s"' % ip)
if isinstance(ip, int):
ip = oct(ip)
return int(str(ip), 8) | [
"def",
"_oct_to_dec",
"(",
"ip",
",",
"check",
"=",
"True",
")",
":",
"if",
"check",
"and",
"not",
"is_oct",
"(",
"ip",
")",
":",
"raise",
"ValueError",
"(",
"'_oct_to_dec: invalid IP: \"%s\"'",
"%",
"ip",
")",
"if",
"isinstance",
"(",
"ip",
",",
"int",
")",
":",
"ip",
"=",
"oct",
"(",
"ip",
")",
"return",
"int",
"(",
"str",
"(",
"ip",
")",
",",
"8",
")"
] | 33.857143 | 0.004115 |
def resizeEvent(self, event):
"""
Handles a resize event for this overlay, centering the central widget if
one is found.
:param event | <QtCore.QEvent>
"""
super(XOverlayWidget, self).resizeEvent(event)
self.adjustSize() | [
"def",
"resizeEvent",
"(",
"self",
",",
"event",
")",
":",
"super",
"(",
"XOverlayWidget",
",",
"self",
")",
".",
"resizeEvent",
"(",
"event",
")",
"self",
".",
"adjustSize",
"(",
")"
] | 30.444444 | 0.010638 |
def _get_layout_as_etree(layout_dict):
"""
Convert something that looks like this:
{
'color' : ['red'],
'shapefile' : ['blah.shp']
}
Into something that looks like this:
<layout>
<item>
<name>color</name>
<value>red</value>
</item>
<item>
<name>shapefile</name>
<value>blah.shp</value>
</item>
</layout>
"""
if layout_dict is None:
return None
layout = etree.Element("layout")
layout_dict = eval(layout_dict)
for k, v in layout_dict.items():
item = etree.SubElement(layout, "item")
name = etree.SubElement(item, "name")
name.text = k
value = etree.SubElement(item, "value")
value.text = str(v)
return layout | [
"def",
"_get_layout_as_etree",
"(",
"layout_dict",
")",
":",
"if",
"layout_dict",
"is",
"None",
":",
"return",
"None",
"layout",
"=",
"etree",
".",
"Element",
"(",
"\"layout\"",
")",
"layout_dict",
"=",
"eval",
"(",
"layout_dict",
")",
"for",
"k",
",",
"v",
"in",
"layout_dict",
".",
"items",
"(",
")",
":",
"item",
"=",
"etree",
".",
"SubElement",
"(",
"layout",
",",
"\"item\"",
")",
"name",
"=",
"etree",
".",
"SubElement",
"(",
"item",
",",
"\"name\"",
")",
"name",
".",
"text",
"=",
"k",
"value",
"=",
"etree",
".",
"SubElement",
"(",
"item",
",",
"\"value\"",
")",
"value",
".",
"text",
"=",
"str",
"(",
"v",
")",
"return",
"layout"
] | 23.454545 | 0.001241 |
def init(self):
"""
Adds custom calculations to orbit simulation.
This routine is run once, and only once, upon instantiation.
Adds quasi-dipole coordiantes, velocity calculation in ECEF coords,
adds the attitude vectors of spacecraft assuming x is ram pointing and
z is generally nadir, adds ionospheric parameters from the Interational
Reference Ionosphere (IRI), as well as simulated winds from the
Horiontal Wind Model (HWM).
"""
self.custom.add(add_quasi_dipole_coordinates, 'modify')
self.custom.add(add_aacgm_coordinates, 'modify')
self.custom.add(calculate_ecef_velocity, 'modify')
self.custom.add(add_sc_attitude_vectors, 'modify')
self.custom.add(add_iri_thermal_plasma, 'modify')
self.custom.add(add_hwm_winds_and_ecef_vectors, 'modify')
self.custom.add(add_igrf, 'modify')
# project simulated vectors onto s/c basis
# IGRF
# create metadata to be added along with vector projection
in_meta = {'desc':'IGRF geomagnetic field expressed in the s/c basis.',
'units':'nT'}
# project IGRF
self.custom.add(project_ecef_vector_onto_sc, 'modify', 'end', 'B_ecef_x', 'B_ecef_y', 'B_ecef_z',
'B_sc_x', 'B_sc_y', 'B_sc_z',
meta=[in_meta.copy(), in_meta.copy(), in_meta.copy()])
# project total wind vector
self.custom.add(project_hwm_onto_sc, 'modify')
# neutral parameters
self.custom.add(add_msis, 'modify') | [
"def",
"init",
"(",
"self",
")",
":",
"self",
".",
"custom",
".",
"add",
"(",
"add_quasi_dipole_coordinates",
",",
"'modify'",
")",
"self",
".",
"custom",
".",
"add",
"(",
"add_aacgm_coordinates",
",",
"'modify'",
")",
"self",
".",
"custom",
".",
"add",
"(",
"calculate_ecef_velocity",
",",
"'modify'",
")",
"self",
".",
"custom",
".",
"add",
"(",
"add_sc_attitude_vectors",
",",
"'modify'",
")",
"self",
".",
"custom",
".",
"add",
"(",
"add_iri_thermal_plasma",
",",
"'modify'",
")",
"self",
".",
"custom",
".",
"add",
"(",
"add_hwm_winds_and_ecef_vectors",
",",
"'modify'",
")",
"self",
".",
"custom",
".",
"add",
"(",
"add_igrf",
",",
"'modify'",
")",
"# project simulated vectors onto s/c basis",
"# IGRF",
"# create metadata to be added along with vector projection",
"in_meta",
"=",
"{",
"'desc'",
":",
"'IGRF geomagnetic field expressed in the s/c basis.'",
",",
"'units'",
":",
"'nT'",
"}",
"# project IGRF",
"self",
".",
"custom",
".",
"add",
"(",
"project_ecef_vector_onto_sc",
",",
"'modify'",
",",
"'end'",
",",
"'B_ecef_x'",
",",
"'B_ecef_y'",
",",
"'B_ecef_z'",
",",
"'B_sc_x'",
",",
"'B_sc_y'",
",",
"'B_sc_z'",
",",
"meta",
"=",
"[",
"in_meta",
".",
"copy",
"(",
")",
",",
"in_meta",
".",
"copy",
"(",
")",
",",
"in_meta",
".",
"copy",
"(",
")",
"]",
")",
"# project total wind vector",
"self",
".",
"custom",
".",
"add",
"(",
"project_hwm_onto_sc",
",",
"'modify'",
")",
"# neutral parameters",
"self",
".",
"custom",
".",
"add",
"(",
"add_msis",
",",
"'modify'",
")"
] | 46.333333 | 0.005766 |
def active_language(self):
"""
Returns active language.
"""
# Current instance language (if user uses activate_language() method)
if self._language is not None:
return self._language
# Current site language (translation.get_language())
current = utils.get_language()
if current in self.supported_languages:
return current
# Default language descriptor
return self.default_language | [
"def",
"active_language",
"(",
"self",
")",
":",
"# Current instance language (if user uses activate_language() method)",
"if",
"self",
".",
"_language",
"is",
"not",
"None",
":",
"return",
"self",
".",
"_language",
"# Current site language (translation.get_language())",
"current",
"=",
"utils",
".",
"get_language",
"(",
")",
"if",
"current",
"in",
"self",
".",
"supported_languages",
":",
"return",
"current",
"# Default language descriptor",
"return",
"self",
".",
"default_language"
] | 31.466667 | 0.004115 |