repo
stringlengths
7
55
path
stringlengths
4
223
func_name
stringlengths
1
134
original_string
stringlengths
75
104k
language
stringclasses
1 value
code
stringlengths
75
104k
code_tokens
listlengths
19
28.4k
docstring
stringlengths
1
46.9k
docstring_tokens
listlengths
1
1.97k
sha
stringlengths
40
40
url
stringlengths
87
315
partition
stringclasses
1 value
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/color/colormap.py
_normalize
def _normalize(x, cmin=None, cmax=None, clip=True): """Normalize an array from the range [cmin, cmax] to [0,1], with optional clipping.""" if not isinstance(x, np.ndarray): x = np.array(x) if cmin is None: cmin = x.min() if cmax is None: cmax = x.max() if cmin == cmax: return .5 * np.ones(x.shape) else: cmin, cmax = float(cmin), float(cmax) y = (x - cmin) * 1. / (cmax - cmin) if clip: y = np.clip(y, 0., 1.) return y
python
def _normalize(x, cmin=None, cmax=None, clip=True): """Normalize an array from the range [cmin, cmax] to [0,1], with optional clipping.""" if not isinstance(x, np.ndarray): x = np.array(x) if cmin is None: cmin = x.min() if cmax is None: cmax = x.max() if cmin == cmax: return .5 * np.ones(x.shape) else: cmin, cmax = float(cmin), float(cmax) y = (x - cmin) * 1. / (cmax - cmin) if clip: y = np.clip(y, 0., 1.) return y
[ "def", "_normalize", "(", "x", ",", "cmin", "=", "None", ",", "cmax", "=", "None", ",", "clip", "=", "True", ")", ":", "if", "not", "isinstance", "(", "x", ",", "np", ".", "ndarray", ")", ":", "x", "=", "np", ".", "array", "(", "x", ")", "if", "cmin", "is", "None", ":", "cmin", "=", "x", ".", "min", "(", ")", "if", "cmax", "is", "None", ":", "cmax", "=", "x", ".", "max", "(", ")", "if", "cmin", "==", "cmax", ":", "return", ".5", "*", "np", ".", "ones", "(", "x", ".", "shape", ")", "else", ":", "cmin", ",", "cmax", "=", "float", "(", "cmin", ")", ",", "float", "(", "cmax", ")", "y", "=", "(", "x", "-", "cmin", ")", "*", "1.", "/", "(", "cmax", "-", "cmin", ")", "if", "clip", ":", "y", "=", "np", ".", "clip", "(", "y", ",", "0.", ",", "1.", ")", "return", "y" ]
Normalize an array from the range [cmin, cmax] to [0,1], with optional clipping.
[ "Normalize", "an", "array", "from", "the", "range", "[", "cmin", "cmax", "]", "to", "[", "0", "1", "]", "with", "optional", "clipping", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/color/colormap.py#L49-L65
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/color/colormap.py
_mix_simple
def _mix_simple(a, b, x): """Mix b (with proportion x) with a.""" x = np.clip(x, 0.0, 1.0) return (1.0 - x)*a + x*b
python
def _mix_simple(a, b, x): """Mix b (with proportion x) with a.""" x = np.clip(x, 0.0, 1.0) return (1.0 - x)*a + x*b
[ "def", "_mix_simple", "(", "a", ",", "b", ",", "x", ")", ":", "x", "=", "np", ".", "clip", "(", "x", ",", "0.0", ",", "1.0", ")", "return", "(", "1.0", "-", "x", ")", "*", "a", "+", "x", "*", "b" ]
Mix b (with proportion x) with a.
[ "Mix", "b", "(", "with", "proportion", "x", ")", "with", "a", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/color/colormap.py#L69-L72
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/color/colormap.py
smoothstep
def smoothstep(edge0, edge1, x): """ performs smooth Hermite interpolation between 0 and 1 when edge0 < x < edge1. """ # Scale, bias and saturate x to 0..1 range x = np.clip((x - edge0)/(edge1 - edge0), 0.0, 1.0) # Evaluate polynomial return x*x*(3 - 2*x)
python
def smoothstep(edge0, edge1, x): """ performs smooth Hermite interpolation between 0 and 1 when edge0 < x < edge1. """ # Scale, bias and saturate x to 0..1 range x = np.clip((x - edge0)/(edge1 - edge0), 0.0, 1.0) # Evaluate polynomial return x*x*(3 - 2*x)
[ "def", "smoothstep", "(", "edge0", ",", "edge1", ",", "x", ")", ":", "# Scale, bias and saturate x to 0..1 range", "x", "=", "np", ".", "clip", "(", "(", "x", "-", "edge0", ")", "/", "(", "edge1", "-", "edge0", ")", ",", "0.0", ",", "1.0", ")", "# Evaluate polynomial", "return", "x", "*", "x", "*", "(", "3", "-", "2", "*", "x", ")" ]
performs smooth Hermite interpolation between 0 and 1 when edge0 < x < edge1.
[ "performs", "smooth", "Hermite", "interpolation", "between", "0", "and", "1", "when", "edge0", "<", "x", "<", "edge1", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/color/colormap.py#L98-L104
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/color/colormap.py
step
def step(colors, x, controls=None): x = x.ravel() """Step interpolation from a set of colors. x belongs in [0, 1].""" assert (controls[0], controls[-1]) == (0., 1.) ncolors = len(colors) assert ncolors == len(controls) - 1 assert ncolors >= 2 x_step = _find_controls(x, controls, ncolors-1) return colors[x_step, ...]
python
def step(colors, x, controls=None): x = x.ravel() """Step interpolation from a set of colors. x belongs in [0, 1].""" assert (controls[0], controls[-1]) == (0., 1.) ncolors = len(colors) assert ncolors == len(controls) - 1 assert ncolors >= 2 x_step = _find_controls(x, controls, ncolors-1) return colors[x_step, ...]
[ "def", "step", "(", "colors", ",", "x", ",", "controls", "=", "None", ")", ":", "x", "=", "x", ".", "ravel", "(", ")", "assert", "(", "controls", "[", "0", "]", ",", "controls", "[", "-", "1", "]", ")", "==", "(", "0.", ",", "1.", ")", "ncolors", "=", "len", "(", "colors", ")", "assert", "ncolors", "==", "len", "(", "controls", ")", "-", "1", "assert", "ncolors", ">=", "2", "x_step", "=", "_find_controls", "(", "x", ",", "controls", ",", "ncolors", "-", "1", ")", "return", "colors", "[", "x_step", ",", "...", "]" ]
Step interpolation from a set of colors. x belongs in [0, 1].
[ "Step", "interpolation", "from", "a", "set", "of", "colors", ".", "x", "belongs", "in", "[", "0", "1", "]", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/color/colormap.py#L107-L115
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/color/colormap.py
_glsl_mix
def _glsl_mix(controls=None): """Generate a GLSL template function from a given interpolation patterns and control points.""" assert (controls[0], controls[-1]) == (0., 1.) ncolors = len(controls) assert ncolors >= 2 if ncolors == 2: s = " return mix($color_0, $color_1, t);\n" else: s = "" for i in range(ncolors-1): if i == 0: ifs = 'if (t < %.6f)' % (controls[i+1]) elif i == (ncolors-2): ifs = 'else' else: ifs = 'else if (t < %.6f)' % (controls[i+1]) adj_t = '(t - %s) / %s' % (controls[i], controls[i+1] - controls[i]) s += ("%s {\n return mix($color_%d, $color_%d, %s);\n} " % (ifs, i, i+1, adj_t)) return "vec4 colormap(float t) {\n%s\n}" % s
python
def _glsl_mix(controls=None): """Generate a GLSL template function from a given interpolation patterns and control points.""" assert (controls[0], controls[-1]) == (0., 1.) ncolors = len(controls) assert ncolors >= 2 if ncolors == 2: s = " return mix($color_0, $color_1, t);\n" else: s = "" for i in range(ncolors-1): if i == 0: ifs = 'if (t < %.6f)' % (controls[i+1]) elif i == (ncolors-2): ifs = 'else' else: ifs = 'else if (t < %.6f)' % (controls[i+1]) adj_t = '(t - %s) / %s' % (controls[i], controls[i+1] - controls[i]) s += ("%s {\n return mix($color_%d, $color_%d, %s);\n} " % (ifs, i, i+1, adj_t)) return "vec4 colormap(float t) {\n%s\n}" % s
[ "def", "_glsl_mix", "(", "controls", "=", "None", ")", ":", "assert", "(", "controls", "[", "0", "]", ",", "controls", "[", "-", "1", "]", ")", "==", "(", "0.", ",", "1.", ")", "ncolors", "=", "len", "(", "controls", ")", "assert", "ncolors", ">=", "2", "if", "ncolors", "==", "2", ":", "s", "=", "\" return mix($color_0, $color_1, t);\\n\"", "else", ":", "s", "=", "\"\"", "for", "i", "in", "range", "(", "ncolors", "-", "1", ")", ":", "if", "i", "==", "0", ":", "ifs", "=", "'if (t < %.6f)'", "%", "(", "controls", "[", "i", "+", "1", "]", ")", "elif", "i", "==", "(", "ncolors", "-", "2", ")", ":", "ifs", "=", "'else'", "else", ":", "ifs", "=", "'else if (t < %.6f)'", "%", "(", "controls", "[", "i", "+", "1", "]", ")", "adj_t", "=", "'(t - %s) / %s'", "%", "(", "controls", "[", "i", "]", ",", "controls", "[", "i", "+", "1", "]", "-", "controls", "[", "i", "]", ")", "s", "+=", "(", "\"%s {\\n return mix($color_%d, $color_%d, %s);\\n} \"", "%", "(", "ifs", ",", "i", ",", "i", "+", "1", ",", "adj_t", ")", ")", "return", "\"vec4 colormap(float t) {\\n%s\\n}\"", "%", "s" ]
Generate a GLSL template function from a given interpolation patterns and control points.
[ "Generate", "a", "GLSL", "template", "function", "from", "a", "given", "interpolation", "patterns", "and", "control", "points", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/color/colormap.py#L119-L140
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/color/colormap.py
_process_glsl_template
def _process_glsl_template(template, colors): """Replace $color_i by color #i in the GLSL template.""" for i in range(len(colors) - 1, -1, -1): color = colors[i] assert len(color) == 4 vec4_color = 'vec4(%.3f, %.3f, %.3f, %.3f)' % tuple(color) template = template.replace('$color_%d' % i, vec4_color) return template
python
def _process_glsl_template(template, colors): """Replace $color_i by color #i in the GLSL template.""" for i in range(len(colors) - 1, -1, -1): color = colors[i] assert len(color) == 4 vec4_color = 'vec4(%.3f, %.3f, %.3f, %.3f)' % tuple(color) template = template.replace('$color_%d' % i, vec4_color) return template
[ "def", "_process_glsl_template", "(", "template", ",", "colors", ")", ":", "for", "i", "in", "range", "(", "len", "(", "colors", ")", "-", "1", ",", "-", "1", ",", "-", "1", ")", ":", "color", "=", "colors", "[", "i", "]", "assert", "len", "(", "color", ")", "==", "4", "vec4_color", "=", "'vec4(%.3f, %.3f, %.3f, %.3f)'", "%", "tuple", "(", "color", ")", "template", "=", "template", ".", "replace", "(", "'$color_%d'", "%", "i", ",", "vec4_color", ")", "return", "template" ]
Replace $color_i by color #i in the GLSL template.
[ "Replace", "$color_i", "by", "color", "#i", "in", "the", "GLSL", "template", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/color/colormap.py#L160-L167
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/color/colormap.py
get_colormap
def get_colormap(name, *args, **kwargs): """Obtain a colormap Some colormaps can have additional configuration parameters. Refer to their corresponding documentation for more information. Parameters ---------- name : str | Colormap Colormap name. Can also be a Colormap for pass-through. Examples -------- >>> get_colormap('autumn') >>> get_colormap('single_hue', hue=10) """ if isinstance(name, BaseColormap): cmap = name else: if not isinstance(name, string_types): raise TypeError('colormap must be a Colormap or string name') if name not in _colormaps: raise KeyError('colormap name %s not found' % name) cmap = _colormaps[name] if inspect.isclass(cmap): cmap = cmap(*args, **kwargs) return cmap
python
def get_colormap(name, *args, **kwargs): """Obtain a colormap Some colormaps can have additional configuration parameters. Refer to their corresponding documentation for more information. Parameters ---------- name : str | Colormap Colormap name. Can also be a Colormap for pass-through. Examples -------- >>> get_colormap('autumn') >>> get_colormap('single_hue', hue=10) """ if isinstance(name, BaseColormap): cmap = name else: if not isinstance(name, string_types): raise TypeError('colormap must be a Colormap or string name') if name not in _colormaps: raise KeyError('colormap name %s not found' % name) cmap = _colormaps[name] if inspect.isclass(cmap): cmap = cmap(*args, **kwargs) return cmap
[ "def", "get_colormap", "(", "name", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "isinstance", "(", "name", ",", "BaseColormap", ")", ":", "cmap", "=", "name", "else", ":", "if", "not", "isinstance", "(", "name", ",", "string_types", ")", ":", "raise", "TypeError", "(", "'colormap must be a Colormap or string name'", ")", "if", "name", "not", "in", "_colormaps", ":", "raise", "KeyError", "(", "'colormap name %s not found'", "%", "name", ")", "cmap", "=", "_colormaps", "[", "name", "]", "if", "inspect", ".", "isclass", "(", "cmap", ")", ":", "cmap", "=", "cmap", "(", "*", "args", ",", "*", "*", "kwargs", ")", "return", "cmap" ]
Obtain a colormap Some colormaps can have additional configuration parameters. Refer to their corresponding documentation for more information. Parameters ---------- name : str | Colormap Colormap name. Can also be a Colormap for pass-through. Examples -------- >>> get_colormap('autumn') >>> get_colormap('single_hue', hue=10)
[ "Obtain", "a", "colormap" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/color/colormap.py#L980-L1009
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/color/colormap.py
Colormap.map
def map(self, x): """The Python mapping function from the [0,1] interval to a list of rgba colors Parameters ---------- x : array-like The values to map. Returns ------- colors : list List of rgba colors. """ return self._map_function(self.colors.rgba, x, self._controls)
python
def map(self, x): """The Python mapping function from the [0,1] interval to a list of rgba colors Parameters ---------- x : array-like The values to map. Returns ------- colors : list List of rgba colors. """ return self._map_function(self.colors.rgba, x, self._controls)
[ "def", "map", "(", "self", ",", "x", ")", ":", "return", "self", ".", "_map_function", "(", "self", ".", "colors", ".", "rgba", ",", "x", ",", "self", ".", "_controls", ")" ]
The Python mapping function from the [0,1] interval to a list of rgba colors Parameters ---------- x : array-like The values to map. Returns ------- colors : list List of rgba colors.
[ "The", "Python", "mapping", "function", "from", "the", "[", "0", "1", "]", "interval", "to", "a", "list", "of", "rgba", "colors" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/color/colormap.py#L363-L377
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/border.py
_BorderVisual.visual_border_width
def visual_border_width(self): """ The border width in visual coordinates """ render_to_doc = \ self.transforms.get_transform('document', 'visual') vec = render_to_doc.map([self.border_width, self.border_width, 0]) origin = render_to_doc.map([0, 0, 0]) visual_border_width = [vec[0] - origin[0], vec[1] - origin[1]] # we need to flip the y axis because coordinate systems are inverted visual_border_width[1] *= -1 return visual_border_width
python
def visual_border_width(self): """ The border width in visual coordinates """ render_to_doc = \ self.transforms.get_transform('document', 'visual') vec = render_to_doc.map([self.border_width, self.border_width, 0]) origin = render_to_doc.map([0, 0, 0]) visual_border_width = [vec[0] - origin[0], vec[1] - origin[1]] # we need to flip the y axis because coordinate systems are inverted visual_border_width[1] *= -1 return visual_border_width
[ "def", "visual_border_width", "(", "self", ")", ":", "render_to_doc", "=", "self", ".", "transforms", ".", "get_transform", "(", "'document'", ",", "'visual'", ")", "vec", "=", "render_to_doc", ".", "map", "(", "[", "self", ".", "border_width", ",", "self", ".", "border_width", ",", "0", "]", ")", "origin", "=", "render_to_doc", ".", "map", "(", "[", "0", ",", "0", ",", "0", "]", ")", "visual_border_width", "=", "[", "vec", "[", "0", "]", "-", "origin", "[", "0", "]", ",", "vec", "[", "1", "]", "-", "origin", "[", "1", "]", "]", "# we need to flip the y axis because coordinate systems are inverted", "visual_border_width", "[", "1", "]", "*=", "-", "1", "return", "visual_border_width" ]
The border width in visual coordinates
[ "The", "border", "width", "in", "visual", "coordinates" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/border.py#L110-L124
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Exporter.run
def run(self, fig): """ Run the exporter on the given figure Parmeters --------- fig : matplotlib.Figure instance The figure to export """ # Calling savefig executes the draw() command, putting elements # in the correct place. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) if self.close_mpl: import matplotlib.pyplot as plt plt.close(fig) self.crawl_fig(fig)
python
def run(self, fig): """ Run the exporter on the given figure Parmeters --------- fig : matplotlib.Figure instance The figure to export """ # Calling savefig executes the draw() command, putting elements # in the correct place. fig.savefig(io.BytesIO(), format='png', dpi=fig.dpi) if self.close_mpl: import matplotlib.pyplot as plt plt.close(fig) self.crawl_fig(fig)
[ "def", "run", "(", "self", ",", "fig", ")", ":", "# Calling savefig executes the draw() command, putting elements", "# in the correct place.", "fig", ".", "savefig", "(", "io", ".", "BytesIO", "(", ")", ",", "format", "=", "'png'", ",", "dpi", "=", "fig", ".", "dpi", ")", "if", "self", ".", "close_mpl", ":", "import", "matplotlib", ".", "pyplot", "as", "plt", "plt", ".", "close", "(", "fig", ")", "self", ".", "crawl_fig", "(", "fig", ")" ]
Run the exporter on the given figure Parmeters --------- fig : matplotlib.Figure instance The figure to export
[ "Run", "the", "exporter", "on", "the", "given", "figure" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L63-L78
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Exporter.process_transform
def process_transform(transform, ax=None, data=None, return_trans=False, force_trans=None): """Process the transform and convert data to figure or data coordinates Parameters ---------- transform : matplotlib Transform object The transform applied to the data ax : matplotlib Axes object (optional) The axes the data is associated with data : ndarray (optional) The array of data to be transformed. return_trans : bool (optional) If true, return the final transform of the data force_trans : matplotlib.transform instance (optional) If supplied, first force the data to this transform Returns ------- code : string Code is either "data", "axes", "figure", or "display", indicating the type of coordinates output. transform : matplotlib transform the transform used to map input data to output data. Returned only if return_trans is True new_data : ndarray Data transformed to match the given coordinate code. Returned only if data is specified """ if isinstance(transform, transforms.BlendedGenericTransform): warnings.warn("Blended transforms not yet supported. " "Zoom behavior may not work as expected.") if force_trans is not None: if data is not None: data = (transform - force_trans).transform(data) transform = force_trans code = "display" if ax is not None: for (c, trans) in [("data", ax.transData), ("axes", ax.transAxes), ("figure", ax.figure.transFigure), ("display", transforms.IdentityTransform())]: if transform.contains_branch(trans): code, transform = (c, transform - trans) break if data is not None: if return_trans: return code, transform.transform(data), transform else: return code, transform.transform(data) else: if return_trans: return code, transform else: return code
python
def process_transform(transform, ax=None, data=None, return_trans=False, force_trans=None): """Process the transform and convert data to figure or data coordinates Parameters ---------- transform : matplotlib Transform object The transform applied to the data ax : matplotlib Axes object (optional) The axes the data is associated with data : ndarray (optional) The array of data to be transformed. return_trans : bool (optional) If true, return the final transform of the data force_trans : matplotlib.transform instance (optional) If supplied, first force the data to this transform Returns ------- code : string Code is either "data", "axes", "figure", or "display", indicating the type of coordinates output. transform : matplotlib transform the transform used to map input data to output data. Returned only if return_trans is True new_data : ndarray Data transformed to match the given coordinate code. Returned only if data is specified """ if isinstance(transform, transforms.BlendedGenericTransform): warnings.warn("Blended transforms not yet supported. " "Zoom behavior may not work as expected.") if force_trans is not None: if data is not None: data = (transform - force_trans).transform(data) transform = force_trans code = "display" if ax is not None: for (c, trans) in [("data", ax.transData), ("axes", ax.transAxes), ("figure", ax.figure.transFigure), ("display", transforms.IdentityTransform())]: if transform.contains_branch(trans): code, transform = (c, transform - trans) break if data is not None: if return_trans: return code, transform.transform(data), transform else: return code, transform.transform(data) else: if return_trans: return code, transform else: return code
[ "def", "process_transform", "(", "transform", ",", "ax", "=", "None", ",", "data", "=", "None", ",", "return_trans", "=", "False", ",", "force_trans", "=", "None", ")", ":", "if", "isinstance", "(", "transform", ",", "transforms", ".", "BlendedGenericTransform", ")", ":", "warnings", ".", "warn", "(", "\"Blended transforms not yet supported. \"", "\"Zoom behavior may not work as expected.\"", ")", "if", "force_trans", "is", "not", "None", ":", "if", "data", "is", "not", "None", ":", "data", "=", "(", "transform", "-", "force_trans", ")", ".", "transform", "(", "data", ")", "transform", "=", "force_trans", "code", "=", "\"display\"", "if", "ax", "is", "not", "None", ":", "for", "(", "c", ",", "trans", ")", "in", "[", "(", "\"data\"", ",", "ax", ".", "transData", ")", ",", "(", "\"axes\"", ",", "ax", ".", "transAxes", ")", ",", "(", "\"figure\"", ",", "ax", ".", "figure", ".", "transFigure", ")", ",", "(", "\"display\"", ",", "transforms", ".", "IdentityTransform", "(", ")", ")", "]", ":", "if", "transform", ".", "contains_branch", "(", "trans", ")", ":", "code", ",", "transform", "=", "(", "c", ",", "transform", "-", "trans", ")", "break", "if", "data", "is", "not", "None", ":", "if", "return_trans", ":", "return", "code", ",", "transform", ".", "transform", "(", "data", ")", ",", "transform", "else", ":", "return", "code", ",", "transform", ".", "transform", "(", "data", ")", "else", ":", "if", "return_trans", ":", "return", "code", ",", "transform", "else", ":", "return", "code" ]
Process the transform and convert data to figure or data coordinates Parameters ---------- transform : matplotlib Transform object The transform applied to the data ax : matplotlib Axes object (optional) The axes the data is associated with data : ndarray (optional) The array of data to be transformed. return_trans : bool (optional) If true, return the final transform of the data force_trans : matplotlib.transform instance (optional) If supplied, first force the data to this transform Returns ------- code : string Code is either "data", "axes", "figure", or "display", indicating the type of coordinates output. transform : matplotlib transform the transform used to map input data to output data. Returned only if return_trans is True new_data : ndarray Data transformed to match the given coordinate code. Returned only if data is specified
[ "Process", "the", "transform", "and", "convert", "data", "to", "figure", "or", "data", "coordinates" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L81-L138
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Exporter.crawl_fig
def crawl_fig(self, fig): """Crawl the figure and process all axes""" with self.renderer.draw_figure(fig=fig, props=utils.get_figure_properties(fig)): for ax in fig.axes: self.crawl_ax(ax)
python
def crawl_fig(self, fig): """Crawl the figure and process all axes""" with self.renderer.draw_figure(fig=fig, props=utils.get_figure_properties(fig)): for ax in fig.axes: self.crawl_ax(ax)
[ "def", "crawl_fig", "(", "self", ",", "fig", ")", ":", "with", "self", ".", "renderer", ".", "draw_figure", "(", "fig", "=", "fig", ",", "props", "=", "utils", ".", "get_figure_properties", "(", "fig", ")", ")", ":", "for", "ax", "in", "fig", ".", "axes", ":", "self", ".", "crawl_ax", "(", "ax", ")" ]
Crawl the figure and process all axes
[ "Crawl", "the", "figure", "and", "process", "all", "axes" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L140-L145
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Exporter.crawl_ax
def crawl_ax(self, ax): """Crawl the axes and process all elements within""" with self.renderer.draw_axes(ax=ax, props=utils.get_axes_properties(ax)): for line in ax.lines: self.draw_line(ax, line) for text in ax.texts: self.draw_text(ax, text) for (text, ttp) in zip([ax.xaxis.label, ax.yaxis.label, ax.title], ["xlabel", "ylabel", "title"]): if(hasattr(text, 'get_text') and text.get_text()): self.draw_text(ax, text, force_trans=ax.transAxes, text_type=ttp) for artist in ax.artists: # TODO: process other artists if isinstance(artist, matplotlib.text.Text): self.draw_text(ax, artist) for patch in ax.patches: self.draw_patch(ax, patch) for collection in ax.collections: self.draw_collection(ax, collection) for image in ax.images: self.draw_image(ax, image) legend = ax.get_legend() if legend is not None: props = utils.get_legend_properties(ax, legend) with self.renderer.draw_legend(legend=legend, props=props): if props['visible']: self.crawl_legend(ax, legend)
python
def crawl_ax(self, ax): """Crawl the axes and process all elements within""" with self.renderer.draw_axes(ax=ax, props=utils.get_axes_properties(ax)): for line in ax.lines: self.draw_line(ax, line) for text in ax.texts: self.draw_text(ax, text) for (text, ttp) in zip([ax.xaxis.label, ax.yaxis.label, ax.title], ["xlabel", "ylabel", "title"]): if(hasattr(text, 'get_text') and text.get_text()): self.draw_text(ax, text, force_trans=ax.transAxes, text_type=ttp) for artist in ax.artists: # TODO: process other artists if isinstance(artist, matplotlib.text.Text): self.draw_text(ax, artist) for patch in ax.patches: self.draw_patch(ax, patch) for collection in ax.collections: self.draw_collection(ax, collection) for image in ax.images: self.draw_image(ax, image) legend = ax.get_legend() if legend is not None: props = utils.get_legend_properties(ax, legend) with self.renderer.draw_legend(legend=legend, props=props): if props['visible']: self.crawl_legend(ax, legend)
[ "def", "crawl_ax", "(", "self", ",", "ax", ")", ":", "with", "self", ".", "renderer", ".", "draw_axes", "(", "ax", "=", "ax", ",", "props", "=", "utils", ".", "get_axes_properties", "(", "ax", ")", ")", ":", "for", "line", "in", "ax", ".", "lines", ":", "self", ".", "draw_line", "(", "ax", ",", "line", ")", "for", "text", "in", "ax", ".", "texts", ":", "self", ".", "draw_text", "(", "ax", ",", "text", ")", "for", "(", "text", ",", "ttp", ")", "in", "zip", "(", "[", "ax", ".", "xaxis", ".", "label", ",", "ax", ".", "yaxis", ".", "label", ",", "ax", ".", "title", "]", ",", "[", "\"xlabel\"", ",", "\"ylabel\"", ",", "\"title\"", "]", ")", ":", "if", "(", "hasattr", "(", "text", ",", "'get_text'", ")", "and", "text", ".", "get_text", "(", ")", ")", ":", "self", ".", "draw_text", "(", "ax", ",", "text", ",", "force_trans", "=", "ax", ".", "transAxes", ",", "text_type", "=", "ttp", ")", "for", "artist", "in", "ax", ".", "artists", ":", "# TODO: process other artists", "if", "isinstance", "(", "artist", ",", "matplotlib", ".", "text", ".", "Text", ")", ":", "self", ".", "draw_text", "(", "ax", ",", "artist", ")", "for", "patch", "in", "ax", ".", "patches", ":", "self", ".", "draw_patch", "(", "ax", ",", "patch", ")", "for", "collection", "in", "ax", ".", "collections", ":", "self", ".", "draw_collection", "(", "ax", ",", "collection", ")", "for", "image", "in", "ax", ".", "images", ":", "self", ".", "draw_image", "(", "ax", ",", "image", ")", "legend", "=", "ax", ".", "get_legend", "(", ")", "if", "legend", "is", "not", "None", ":", "props", "=", "utils", ".", "get_legend_properties", "(", "ax", ",", "legend", ")", "with", "self", ".", "renderer", ".", "draw_legend", "(", "legend", "=", "legend", ",", "props", "=", "props", ")", ":", "if", "props", "[", "'visible'", "]", ":", "self", ".", "crawl_legend", "(", "ax", ",", "legend", ")" ]
Crawl the axes and process all elements within
[ "Crawl", "the", "axes", "and", "process", "all", "elements", "within" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L147-L176
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Exporter.crawl_legend
def crawl_legend(self, ax, legend): """ Recursively look through objects in legend children """ legendElements = list(utils.iter_all_children(legend._legend_box, skipContainers=True)) legendElements.append(legend.legendPatch) for child in legendElements: # force a large zorder so it appears on top child.set_zorder(1E6 + child.get_zorder()) try: # What kind of object... if isinstance(child, matplotlib.patches.Patch): self.draw_patch(ax, child, force_trans=ax.transAxes) elif isinstance(child, matplotlib.text.Text): if not (child is legend.get_children()[-1] and child.get_text() == 'None'): self.draw_text(ax, child, force_trans=ax.transAxes) elif isinstance(child, matplotlib.lines.Line2D): self.draw_line(ax, child, force_trans=ax.transAxes) elif isinstance(child, matplotlib.collections.Collection): self.draw_collection(ax, child, force_pathtrans=ax.transAxes) else: warnings.warn("Legend element %s not impemented" % child) except NotImplementedError: warnings.warn("Legend element %s not impemented" % child)
python
def crawl_legend(self, ax, legend): """ Recursively look through objects in legend children """ legendElements = list(utils.iter_all_children(legend._legend_box, skipContainers=True)) legendElements.append(legend.legendPatch) for child in legendElements: # force a large zorder so it appears on top child.set_zorder(1E6 + child.get_zorder()) try: # What kind of object... if isinstance(child, matplotlib.patches.Patch): self.draw_patch(ax, child, force_trans=ax.transAxes) elif isinstance(child, matplotlib.text.Text): if not (child is legend.get_children()[-1] and child.get_text() == 'None'): self.draw_text(ax, child, force_trans=ax.transAxes) elif isinstance(child, matplotlib.lines.Line2D): self.draw_line(ax, child, force_trans=ax.transAxes) elif isinstance(child, matplotlib.collections.Collection): self.draw_collection(ax, child, force_pathtrans=ax.transAxes) else: warnings.warn("Legend element %s not impemented" % child) except NotImplementedError: warnings.warn("Legend element %s not impemented" % child)
[ "def", "crawl_legend", "(", "self", ",", "ax", ",", "legend", ")", ":", "legendElements", "=", "list", "(", "utils", ".", "iter_all_children", "(", "legend", ".", "_legend_box", ",", "skipContainers", "=", "True", ")", ")", "legendElements", ".", "append", "(", "legend", ".", "legendPatch", ")", "for", "child", "in", "legendElements", ":", "# force a large zorder so it appears on top", "child", ".", "set_zorder", "(", "1E6", "+", "child", ".", "get_zorder", "(", ")", ")", "try", ":", "# What kind of object...", "if", "isinstance", "(", "child", ",", "matplotlib", ".", "patches", ".", "Patch", ")", ":", "self", ".", "draw_patch", "(", "ax", ",", "child", ",", "force_trans", "=", "ax", ".", "transAxes", ")", "elif", "isinstance", "(", "child", ",", "matplotlib", ".", "text", ".", "Text", ")", ":", "if", "not", "(", "child", "is", "legend", ".", "get_children", "(", ")", "[", "-", "1", "]", "and", "child", ".", "get_text", "(", ")", "==", "'None'", ")", ":", "self", ".", "draw_text", "(", "ax", ",", "child", ",", "force_trans", "=", "ax", ".", "transAxes", ")", "elif", "isinstance", "(", "child", ",", "matplotlib", ".", "lines", ".", "Line2D", ")", ":", "self", ".", "draw_line", "(", "ax", ",", "child", ",", "force_trans", "=", "ax", ".", "transAxes", ")", "elif", "isinstance", "(", "child", ",", "matplotlib", ".", "collections", ".", "Collection", ")", ":", "self", ".", "draw_collection", "(", "ax", ",", "child", ",", "force_pathtrans", "=", "ax", ".", "transAxes", ")", "else", ":", "warnings", ".", "warn", "(", "\"Legend element %s not impemented\"", "%", "child", ")", "except", "NotImplementedError", ":", "warnings", ".", "warn", "(", "\"Legend element %s not impemented\"", "%", "child", ")" ]
Recursively look through objects in legend children
[ "Recursively", "look", "through", "objects", "in", "legend", "children" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L178-L205
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Exporter.draw_line
def draw_line(self, ax, line, force_trans=None): """Process a matplotlib line and call renderer.draw_line""" coordinates, data = self.process_transform(line.get_transform(), ax, line.get_xydata(), force_trans=force_trans) linestyle = utils.get_line_style(line) if linestyle['dasharray'] is None: linestyle = None markerstyle = utils.get_marker_style(line) if (markerstyle['marker'] in ['None', 'none', None] or markerstyle['markerpath'][0].size == 0): markerstyle = None label = line.get_label() if markerstyle or linestyle: self.renderer.draw_marked_line(data=data, coordinates=coordinates, linestyle=linestyle, markerstyle=markerstyle, label=label, mplobj=line)
python
def draw_line(self, ax, line, force_trans=None): """Process a matplotlib line and call renderer.draw_line""" coordinates, data = self.process_transform(line.get_transform(), ax, line.get_xydata(), force_trans=force_trans) linestyle = utils.get_line_style(line) if linestyle['dasharray'] is None: linestyle = None markerstyle = utils.get_marker_style(line) if (markerstyle['marker'] in ['None', 'none', None] or markerstyle['markerpath'][0].size == 0): markerstyle = None label = line.get_label() if markerstyle or linestyle: self.renderer.draw_marked_line(data=data, coordinates=coordinates, linestyle=linestyle, markerstyle=markerstyle, label=label, mplobj=line)
[ "def", "draw_line", "(", "self", ",", "ax", ",", "line", ",", "force_trans", "=", "None", ")", ":", "coordinates", ",", "data", "=", "self", ".", "process_transform", "(", "line", ".", "get_transform", "(", ")", ",", "ax", ",", "line", ".", "get_xydata", "(", ")", ",", "force_trans", "=", "force_trans", ")", "linestyle", "=", "utils", ".", "get_line_style", "(", "line", ")", "if", "linestyle", "[", "'dasharray'", "]", "is", "None", ":", "linestyle", "=", "None", "markerstyle", "=", "utils", ".", "get_marker_style", "(", "line", ")", "if", "(", "markerstyle", "[", "'marker'", "]", "in", "[", "'None'", ",", "'none'", ",", "None", "]", "or", "markerstyle", "[", "'markerpath'", "]", "[", "0", "]", ".", "size", "==", "0", ")", ":", "markerstyle", "=", "None", "label", "=", "line", ".", "get_label", "(", ")", "if", "markerstyle", "or", "linestyle", ":", "self", ".", "renderer", ".", "draw_marked_line", "(", "data", "=", "data", ",", "coordinates", "=", "coordinates", ",", "linestyle", "=", "linestyle", ",", "markerstyle", "=", "markerstyle", ",", "label", "=", "label", ",", "mplobj", "=", "line", ")" ]
Process a matplotlib line and call renderer.draw_line
[ "Process", "a", "matplotlib", "line", "and", "call", "renderer", ".", "draw_line" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L207-L225
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Exporter.draw_text
def draw_text(self, ax, text, force_trans=None, text_type=None): """Process a matplotlib text object and call renderer.draw_text""" content = text.get_text() if content: transform = text.get_transform() position = text.get_position() coords, position = self.process_transform(transform, ax, position, force_trans=force_trans) style = utils.get_text_style(text) self.renderer.draw_text(text=content, position=position, coordinates=coords, text_type=text_type, style=style, mplobj=text)
python
def draw_text(self, ax, text, force_trans=None, text_type=None): """Process a matplotlib text object and call renderer.draw_text""" content = text.get_text() if content: transform = text.get_transform() position = text.get_position() coords, position = self.process_transform(transform, ax, position, force_trans=force_trans) style = utils.get_text_style(text) self.renderer.draw_text(text=content, position=position, coordinates=coords, text_type=text_type, style=style, mplobj=text)
[ "def", "draw_text", "(", "self", ",", "ax", ",", "text", ",", "force_trans", "=", "None", ",", "text_type", "=", "None", ")", ":", "content", "=", "text", ".", "get_text", "(", ")", "if", "content", ":", "transform", "=", "text", ".", "get_transform", "(", ")", "position", "=", "text", ".", "get_position", "(", ")", "coords", ",", "position", "=", "self", ".", "process_transform", "(", "transform", ",", "ax", ",", "position", ",", "force_trans", "=", "force_trans", ")", "style", "=", "utils", ".", "get_text_style", "(", "text", ")", "self", ".", "renderer", ".", "draw_text", "(", "text", "=", "content", ",", "position", "=", "position", ",", "coordinates", "=", "coords", ",", "text_type", "=", "text_type", ",", "style", "=", "style", ",", "mplobj", "=", "text", ")" ]
Process a matplotlib text object and call renderer.draw_text
[ "Process", "a", "matplotlib", "text", "object", "and", "call", "renderer", ".", "draw_text" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L227-L240
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Exporter.draw_patch
def draw_patch(self, ax, patch, force_trans=None): """Process a matplotlib patch object and call renderer.draw_path""" vertices, pathcodes = utils.SVG_path(patch.get_path()) transform = patch.get_transform() coordinates, vertices = self.process_transform(transform, ax, vertices, force_trans=force_trans) linestyle = utils.get_path_style(patch, fill=patch.get_fill()) self.renderer.draw_path(data=vertices, coordinates=coordinates, pathcodes=pathcodes, style=linestyle, mplobj=patch)
python
def draw_patch(self, ax, patch, force_trans=None): """Process a matplotlib patch object and call renderer.draw_path""" vertices, pathcodes = utils.SVG_path(patch.get_path()) transform = patch.get_transform() coordinates, vertices = self.process_transform(transform, ax, vertices, force_trans=force_trans) linestyle = utils.get_path_style(patch, fill=patch.get_fill()) self.renderer.draw_path(data=vertices, coordinates=coordinates, pathcodes=pathcodes, style=linestyle, mplobj=patch)
[ "def", "draw_patch", "(", "self", ",", "ax", ",", "patch", ",", "force_trans", "=", "None", ")", ":", "vertices", ",", "pathcodes", "=", "utils", ".", "SVG_path", "(", "patch", ".", "get_path", "(", ")", ")", "transform", "=", "patch", ".", "get_transform", "(", ")", "coordinates", ",", "vertices", "=", "self", ".", "process_transform", "(", "transform", ",", "ax", ",", "vertices", ",", "force_trans", "=", "force_trans", ")", "linestyle", "=", "utils", ".", "get_path_style", "(", "patch", ",", "fill", "=", "patch", ".", "get_fill", "(", ")", ")", "self", ".", "renderer", ".", "draw_path", "(", "data", "=", "vertices", ",", "coordinates", "=", "coordinates", ",", "pathcodes", "=", "pathcodes", ",", "style", "=", "linestyle", ",", "mplobj", "=", "patch", ")" ]
Process a matplotlib patch object and call renderer.draw_path
[ "Process", "a", "matplotlib", "patch", "object", "and", "call", "renderer", ".", "draw_path" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L242-L254
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Exporter.draw_collection
def draw_collection(self, ax, collection, force_pathtrans=None, force_offsettrans=None): """Process a matplotlib collection and call renderer.draw_collection""" (transform, transOffset, offsets, paths) = collection._prepare_points() offset_coords, offsets = self.process_transform( transOffset, ax, offsets, force_trans=force_offsettrans) path_coords = self.process_transform( transform, ax, force_trans=force_pathtrans) processed_paths = [utils.SVG_path(path) for path in paths] processed_paths = [(self.process_transform( transform, ax, path[0], force_trans=force_pathtrans)[1], path[1]) for path in processed_paths] path_transforms = collection.get_transforms() try: # matplotlib 1.3: path_transforms are transform objects. # Convert them to numpy arrays. path_transforms = [t.get_matrix() for t in path_transforms] except AttributeError: # matplotlib 1.4: path transforms are already numpy arrays. pass styles = {'linewidth': collection.get_linewidths(), 'facecolor': collection.get_facecolors(), 'edgecolor': collection.get_edgecolors(), 'alpha': collection._alpha, 'zorder': collection.get_zorder()} offset_dict = {"data": "before", "screen": "after"} offset_order = offset_dict[collection.get_offset_position()] self.renderer.draw_path_collection(paths=processed_paths, path_coordinates=path_coords, path_transforms=path_transforms, offsets=offsets, offset_coordinates=offset_coords, offset_order=offset_order, styles=styles, mplobj=collection)
python
def draw_collection(self, ax, collection, force_pathtrans=None, force_offsettrans=None): """Process a matplotlib collection and call renderer.draw_collection""" (transform, transOffset, offsets, paths) = collection._prepare_points() offset_coords, offsets = self.process_transform( transOffset, ax, offsets, force_trans=force_offsettrans) path_coords = self.process_transform( transform, ax, force_trans=force_pathtrans) processed_paths = [utils.SVG_path(path) for path in paths] processed_paths = [(self.process_transform( transform, ax, path[0], force_trans=force_pathtrans)[1], path[1]) for path in processed_paths] path_transforms = collection.get_transforms() try: # matplotlib 1.3: path_transforms are transform objects. # Convert them to numpy arrays. path_transforms = [t.get_matrix() for t in path_transforms] except AttributeError: # matplotlib 1.4: path transforms are already numpy arrays. pass styles = {'linewidth': collection.get_linewidths(), 'facecolor': collection.get_facecolors(), 'edgecolor': collection.get_edgecolors(), 'alpha': collection._alpha, 'zorder': collection.get_zorder()} offset_dict = {"data": "before", "screen": "after"} offset_order = offset_dict[collection.get_offset_position()] self.renderer.draw_path_collection(paths=processed_paths, path_coordinates=path_coords, path_transforms=path_transforms, offsets=offsets, offset_coordinates=offset_coords, offset_order=offset_order, styles=styles, mplobj=collection)
[ "def", "draw_collection", "(", "self", ",", "ax", ",", "collection", ",", "force_pathtrans", "=", "None", ",", "force_offsettrans", "=", "None", ")", ":", "(", "transform", ",", "transOffset", ",", "offsets", ",", "paths", ")", "=", "collection", ".", "_prepare_points", "(", ")", "offset_coords", ",", "offsets", "=", "self", ".", "process_transform", "(", "transOffset", ",", "ax", ",", "offsets", ",", "force_trans", "=", "force_offsettrans", ")", "path_coords", "=", "self", ".", "process_transform", "(", "transform", ",", "ax", ",", "force_trans", "=", "force_pathtrans", ")", "processed_paths", "=", "[", "utils", ".", "SVG_path", "(", "path", ")", "for", "path", "in", "paths", "]", "processed_paths", "=", "[", "(", "self", ".", "process_transform", "(", "transform", ",", "ax", ",", "path", "[", "0", "]", ",", "force_trans", "=", "force_pathtrans", ")", "[", "1", "]", ",", "path", "[", "1", "]", ")", "for", "path", "in", "processed_paths", "]", "path_transforms", "=", "collection", ".", "get_transforms", "(", ")", "try", ":", "# matplotlib 1.3: path_transforms are transform objects.", "# Convert them to numpy arrays.", "path_transforms", "=", "[", "t", ".", "get_matrix", "(", ")", "for", "t", "in", "path_transforms", "]", "except", "AttributeError", ":", "# matplotlib 1.4: path transforms are already numpy arrays.", "pass", "styles", "=", "{", "'linewidth'", ":", "collection", ".", "get_linewidths", "(", ")", ",", "'facecolor'", ":", "collection", ".", "get_facecolors", "(", ")", ",", "'edgecolor'", ":", "collection", ".", "get_edgecolors", "(", ")", ",", "'alpha'", ":", "collection", ".", "_alpha", ",", "'zorder'", ":", "collection", ".", "get_zorder", "(", ")", "}", "offset_dict", "=", "{", "\"data\"", ":", "\"before\"", ",", "\"screen\"", ":", "\"after\"", "}", "offset_order", "=", "offset_dict", "[", "collection", ".", "get_offset_position", "(", ")", "]", "self", ".", "renderer", ".", "draw_path_collection", "(", "paths", "=", "processed_paths", ",", "path_coordinates", "=", "path_coords", ",", "path_transforms", "=", "path_transforms", ",", "offsets", "=", "offsets", ",", "offset_coordinates", "=", "offset_coords", ",", "offset_order", "=", "offset_order", ",", "styles", "=", "styles", ",", "mplobj", "=", "collection", ")" ]
Process a matplotlib collection and call renderer.draw_collection
[ "Process", "a", "matplotlib", "collection", "and", "call", "renderer", ".", "draw_collection" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L256-L299
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Exporter.draw_image
def draw_image(self, ax, image): """Process a matplotlib image object and call renderer.draw_image""" self.renderer.draw_image(imdata=utils.image_to_base64(image), extent=image.get_extent(), coordinates="data", style={"alpha": image.get_alpha(), "zorder": image.get_zorder()}, mplobj=image)
python
def draw_image(self, ax, image): """Process a matplotlib image object and call renderer.draw_image""" self.renderer.draw_image(imdata=utils.image_to_base64(image), extent=image.get_extent(), coordinates="data", style={"alpha": image.get_alpha(), "zorder": image.get_zorder()}, mplobj=image)
[ "def", "draw_image", "(", "self", ",", "ax", ",", "image", ")", ":", "self", ".", "renderer", ".", "draw_image", "(", "imdata", "=", "utils", ".", "image_to_base64", "(", "image", ")", ",", "extent", "=", "image", ".", "get_extent", "(", ")", ",", "coordinates", "=", "\"data\"", ",", "style", "=", "{", "\"alpha\"", ":", "image", ".", "get_alpha", "(", ")", ",", "\"zorder\"", ":", "image", ".", "get_zorder", "(", ")", "}", ",", "mplobj", "=", "image", ")" ]
Process a matplotlib image object and call renderer.draw_image
[ "Process", "a", "matplotlib", "image", "object", "and", "call", "renderer", ".", "draw_image" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L301-L308
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Renderer.draw_marked_line
def draw_marked_line(self, data, coordinates, linestyle, markerstyle, label, mplobj=None): """Draw a line that also has markers. If this isn't reimplemented by a renderer object, by default, it will make a call to BOTH draw_line and draw_markers when both markerstyle and linestyle are not None in the same Line2D object. """ if linestyle is not None: self.draw_line(data, coordinates, linestyle, label, mplobj) if markerstyle is not None: self.draw_markers(data, coordinates, markerstyle, label, mplobj)
python
def draw_marked_line(self, data, coordinates, linestyle, markerstyle, label, mplobj=None): """Draw a line that also has markers. If this isn't reimplemented by a renderer object, by default, it will make a call to BOTH draw_line and draw_markers when both markerstyle and linestyle are not None in the same Line2D object. """ if linestyle is not None: self.draw_line(data, coordinates, linestyle, label, mplobj) if markerstyle is not None: self.draw_markers(data, coordinates, markerstyle, label, mplobj)
[ "def", "draw_marked_line", "(", "self", ",", "data", ",", "coordinates", ",", "linestyle", ",", "markerstyle", ",", "label", ",", "mplobj", "=", "None", ")", ":", "if", "linestyle", "is", "not", "None", ":", "self", ".", "draw_line", "(", "data", ",", "coordinates", ",", "linestyle", ",", "label", ",", "mplobj", ")", "if", "markerstyle", "is", "not", "None", ":", "self", ".", "draw_markers", "(", "data", ",", "coordinates", ",", "markerstyle", ",", "label", ",", "mplobj", ")" ]
Draw a line that also has markers. If this isn't reimplemented by a renderer object, by default, it will make a call to BOTH draw_line and draw_markers when both markerstyle and linestyle are not None in the same Line2D object.
[ "Draw", "a", "line", "that", "also", "has", "markers", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L455-L467
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Renderer.draw_line
def draw_line(self, data, coordinates, style, label, mplobj=None): """ Draw a line. By default, draw the line via the draw_path() command. Some renderers might wish to override this and provide more fine-grained behavior. In matplotlib, lines are generally created via the plt.plot() command, though this command also can create marker collections. Parameters ---------- data : array_like A shape (N, 2) array of datapoints. coordinates : string A string code, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. style : dictionary a dictionary specifying the appearance of the line. mplobj : matplotlib object the matplotlib plot element which generated this line """ pathcodes = ['M'] + (data.shape[0] - 1) * ['L'] pathstyle = dict(facecolor='none', **style) pathstyle['edgecolor'] = pathstyle.pop('color') pathstyle['edgewidth'] = pathstyle.pop('linewidth') self.draw_path(data=data, coordinates=coordinates, pathcodes=pathcodes, style=pathstyle, mplobj=mplobj)
python
def draw_line(self, data, coordinates, style, label, mplobj=None): """ Draw a line. By default, draw the line via the draw_path() command. Some renderers might wish to override this and provide more fine-grained behavior. In matplotlib, lines are generally created via the plt.plot() command, though this command also can create marker collections. Parameters ---------- data : array_like A shape (N, 2) array of datapoints. coordinates : string A string code, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. style : dictionary a dictionary specifying the appearance of the line. mplobj : matplotlib object the matplotlib plot element which generated this line """ pathcodes = ['M'] + (data.shape[0] - 1) * ['L'] pathstyle = dict(facecolor='none', **style) pathstyle['edgecolor'] = pathstyle.pop('color') pathstyle['edgewidth'] = pathstyle.pop('linewidth') self.draw_path(data=data, coordinates=coordinates, pathcodes=pathcodes, style=pathstyle, mplobj=mplobj)
[ "def", "draw_line", "(", "self", ",", "data", ",", "coordinates", ",", "style", ",", "label", ",", "mplobj", "=", "None", ")", ":", "pathcodes", "=", "[", "'M'", "]", "+", "(", "data", ".", "shape", "[", "0", "]", "-", "1", ")", "*", "[", "'L'", "]", "pathstyle", "=", "dict", "(", "facecolor", "=", "'none'", ",", "*", "*", "style", ")", "pathstyle", "[", "'edgecolor'", "]", "=", "pathstyle", ".", "pop", "(", "'color'", ")", "pathstyle", "[", "'edgewidth'", "]", "=", "pathstyle", ".", "pop", "(", "'linewidth'", ")", "self", ".", "draw_path", "(", "data", "=", "data", ",", "coordinates", "=", "coordinates", ",", "pathcodes", "=", "pathcodes", ",", "style", "=", "pathstyle", ",", "mplobj", "=", "mplobj", ")" ]
Draw a line. By default, draw the line via the draw_path() command. Some renderers might wish to override this and provide more fine-grained behavior. In matplotlib, lines are generally created via the plt.plot() command, though this command also can create marker collections. Parameters ---------- data : array_like A shape (N, 2) array of datapoints. coordinates : string A string code, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. style : dictionary a dictionary specifying the appearance of the line. mplobj : matplotlib object the matplotlib plot element which generated this line
[ "Draw", "a", "line", ".", "By", "default", "draw", "the", "line", "via", "the", "draw_path", "()", "command", ".", "Some", "renderers", "might", "wish", "to", "override", "this", "and", "provide", "more", "fine", "-", "grained", "behavior", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L469-L495
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Renderer._iter_path_collection
def _iter_path_collection(paths, path_transforms, offsets, styles): """Build an iterator over the elements of the path collection""" N = max(len(paths), len(offsets)) if not path_transforms: path_transforms = [np.eye(3)] edgecolor = styles['edgecolor'] if np.size(edgecolor) == 0: edgecolor = ['none'] facecolor = styles['facecolor'] if np.size(facecolor) == 0: facecolor = ['none'] elements = [paths, path_transforms, offsets, edgecolor, styles['linewidth'], facecolor] it = itertools return it.islice(py3k.zip(*py3k.map(it.cycle, elements)), N)
python
def _iter_path_collection(paths, path_transforms, offsets, styles): """Build an iterator over the elements of the path collection""" N = max(len(paths), len(offsets)) if not path_transforms: path_transforms = [np.eye(3)] edgecolor = styles['edgecolor'] if np.size(edgecolor) == 0: edgecolor = ['none'] facecolor = styles['facecolor'] if np.size(facecolor) == 0: facecolor = ['none'] elements = [paths, path_transforms, offsets, edgecolor, styles['linewidth'], facecolor] it = itertools return it.islice(py3k.zip(*py3k.map(it.cycle, elements)), N)
[ "def", "_iter_path_collection", "(", "paths", ",", "path_transforms", ",", "offsets", ",", "styles", ")", ":", "N", "=", "max", "(", "len", "(", "paths", ")", ",", "len", "(", "offsets", ")", ")", "if", "not", "path_transforms", ":", "path_transforms", "=", "[", "np", ".", "eye", "(", "3", ")", "]", "edgecolor", "=", "styles", "[", "'edgecolor'", "]", "if", "np", ".", "size", "(", "edgecolor", ")", "==", "0", ":", "edgecolor", "=", "[", "'none'", "]", "facecolor", "=", "styles", "[", "'facecolor'", "]", "if", "np", ".", "size", "(", "facecolor", ")", "==", "0", ":", "facecolor", "=", "[", "'none'", "]", "elements", "=", "[", "paths", ",", "path_transforms", ",", "offsets", ",", "edgecolor", ",", "styles", "[", "'linewidth'", "]", ",", "facecolor", "]", "it", "=", "itertools", "return", "it", ".", "islice", "(", "py3k", ".", "zip", "(", "*", "py3k", ".", "map", "(", "it", ".", "cycle", ",", "elements", ")", ")", ",", "N", ")" ]
Build an iterator over the elements of the path collection
[ "Build", "an", "iterator", "over", "the", "elements", "of", "the", "path", "collection" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L498-L516
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Renderer.draw_path_collection
def draw_path_collection(self, paths, path_coordinates, path_transforms, offsets, offset_coordinates, offset_order, styles, mplobj=None): """ Draw a collection of paths. The paths, offsets, and styles are all iterables, and the number of paths is max(len(paths), len(offsets)). By default, this is implemented via multiple calls to the draw_path() function. For efficiency, Renderers may choose to customize this implementation. Examples of path collections created by matplotlib are scatter plots, histograms, contour plots, and many others. Parameters ---------- paths : list list of tuples, where each tuple has two elements: (data, pathcodes). See draw_path() for a description of these. path_coordinates: string the coordinates code for the paths, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. path_transforms: array_like an array of shape (*, 3, 3), giving a series of 2D Affine transforms for the paths. These encode translations, rotations, and scalings in the standard way. offsets: array_like An array of offsets of shape (N, 2) offset_coordinates : string the coordinates code for the offsets, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. offset_order : string either "before" or "after". This specifies whether the offset is applied before the path transform, or after. The matplotlib backend equivalent is "before"->"data", "after"->"screen". styles: dictionary A dictionary in which each value is a list of length N, containing the style(s) for the paths. mplobj : matplotlib object the matplotlib plot element which generated this collection """ if offset_order == "before": raise NotImplementedError("offset before transform") for tup in self._iter_path_collection(paths, path_transforms, offsets, styles): (path, path_transform, offset, ec, lw, fc) = tup vertices, pathcodes = path path_transform = transforms.Affine2D(path_transform) vertices = path_transform.transform(vertices) # This is a hack: if path_coordinates == "figure": path_coordinates = "points" style = {"edgecolor": utils.color_to_hex(ec), "facecolor": utils.color_to_hex(fc), "edgewidth": lw, "dasharray": "10,0", "alpha": styles['alpha'], "zorder": styles['zorder']} self.draw_path(data=vertices, coordinates=path_coordinates, pathcodes=pathcodes, style=style, offset=offset, offset_coordinates=offset_coordinates, mplobj=mplobj)
python
def draw_path_collection(self, paths, path_coordinates, path_transforms, offsets, offset_coordinates, offset_order, styles, mplobj=None): """ Draw a collection of paths. The paths, offsets, and styles are all iterables, and the number of paths is max(len(paths), len(offsets)). By default, this is implemented via multiple calls to the draw_path() function. For efficiency, Renderers may choose to customize this implementation. Examples of path collections created by matplotlib are scatter plots, histograms, contour plots, and many others. Parameters ---------- paths : list list of tuples, where each tuple has two elements: (data, pathcodes). See draw_path() for a description of these. path_coordinates: string the coordinates code for the paths, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. path_transforms: array_like an array of shape (*, 3, 3), giving a series of 2D Affine transforms for the paths. These encode translations, rotations, and scalings in the standard way. offsets: array_like An array of offsets of shape (N, 2) offset_coordinates : string the coordinates code for the offsets, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. offset_order : string either "before" or "after". This specifies whether the offset is applied before the path transform, or after. The matplotlib backend equivalent is "before"->"data", "after"->"screen". styles: dictionary A dictionary in which each value is a list of length N, containing the style(s) for the paths. mplobj : matplotlib object the matplotlib plot element which generated this collection """ if offset_order == "before": raise NotImplementedError("offset before transform") for tup in self._iter_path_collection(paths, path_transforms, offsets, styles): (path, path_transform, offset, ec, lw, fc) = tup vertices, pathcodes = path path_transform = transforms.Affine2D(path_transform) vertices = path_transform.transform(vertices) # This is a hack: if path_coordinates == "figure": path_coordinates = "points" style = {"edgecolor": utils.color_to_hex(ec), "facecolor": utils.color_to_hex(fc), "edgewidth": lw, "dasharray": "10,0", "alpha": styles['alpha'], "zorder": styles['zorder']} self.draw_path(data=vertices, coordinates=path_coordinates, pathcodes=pathcodes, style=style, offset=offset, offset_coordinates=offset_coordinates, mplobj=mplobj)
[ "def", "draw_path_collection", "(", "self", ",", "paths", ",", "path_coordinates", ",", "path_transforms", ",", "offsets", ",", "offset_coordinates", ",", "offset_order", ",", "styles", ",", "mplobj", "=", "None", ")", ":", "if", "offset_order", "==", "\"before\"", ":", "raise", "NotImplementedError", "(", "\"offset before transform\"", ")", "for", "tup", "in", "self", ".", "_iter_path_collection", "(", "paths", ",", "path_transforms", ",", "offsets", ",", "styles", ")", ":", "(", "path", ",", "path_transform", ",", "offset", ",", "ec", ",", "lw", ",", "fc", ")", "=", "tup", "vertices", ",", "pathcodes", "=", "path", "path_transform", "=", "transforms", ".", "Affine2D", "(", "path_transform", ")", "vertices", "=", "path_transform", ".", "transform", "(", "vertices", ")", "# This is a hack:", "if", "path_coordinates", "==", "\"figure\"", ":", "path_coordinates", "=", "\"points\"", "style", "=", "{", "\"edgecolor\"", ":", "utils", ".", "color_to_hex", "(", "ec", ")", ",", "\"facecolor\"", ":", "utils", ".", "color_to_hex", "(", "fc", ")", ",", "\"edgewidth\"", ":", "lw", ",", "\"dasharray\"", ":", "\"10,0\"", ",", "\"alpha\"", ":", "styles", "[", "'alpha'", "]", ",", "\"zorder\"", ":", "styles", "[", "'zorder'", "]", "}", "self", ".", "draw_path", "(", "data", "=", "vertices", ",", "coordinates", "=", "path_coordinates", ",", "pathcodes", "=", "pathcodes", ",", "style", "=", "style", ",", "offset", "=", "offset", ",", "offset_coordinates", "=", "offset_coordinates", ",", "mplobj", "=", "mplobj", ")" ]
Draw a collection of paths. The paths, offsets, and styles are all iterables, and the number of paths is max(len(paths), len(offsets)). By default, this is implemented via multiple calls to the draw_path() function. For efficiency, Renderers may choose to customize this implementation. Examples of path collections created by matplotlib are scatter plots, histograms, contour plots, and many others. Parameters ---------- paths : list list of tuples, where each tuple has two elements: (data, pathcodes). See draw_path() for a description of these. path_coordinates: string the coordinates code for the paths, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. path_transforms: array_like an array of shape (*, 3, 3), giving a series of 2D Affine transforms for the paths. These encode translations, rotations, and scalings in the standard way. offsets: array_like An array of offsets of shape (N, 2) offset_coordinates : string the coordinates code for the offsets, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. offset_order : string either "before" or "after". This specifies whether the offset is applied before the path transform, or after. The matplotlib backend equivalent is "before"->"data", "after"->"screen". styles: dictionary A dictionary in which each value is a list of length N, containing the style(s) for the paths. mplobj : matplotlib object the matplotlib plot element which generated this collection
[ "Draw", "a", "collection", "of", "paths", ".", "The", "paths", "offsets", "and", "styles", "are", "all", "iterables", "and", "the", "number", "of", "paths", "is", "max", "(", "len", "(", "paths", ")", "len", "(", "offsets", "))", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L518-L582
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Renderer.draw_markers
def draw_markers(self, data, coordinates, style, label, mplobj=None): """ Draw a set of markers. By default, this is done by repeatedly calling draw_path(), but renderers should generally overload this method to provide a more efficient implementation. In matplotlib, markers are created using the plt.plot() command. Parameters ---------- data : array_like A shape (N, 2) array of datapoints. coordinates : string A string code, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. style : dictionary a dictionary specifying the appearance of the markers. mplobj : matplotlib object the matplotlib plot element which generated this marker collection """ vertices, pathcodes = style['markerpath'] pathstyle = dict((key, style[key]) for key in ['alpha', 'edgecolor', 'facecolor', 'zorder', 'edgewidth']) pathstyle['dasharray'] = "10,0" for vertex in data: self.draw_path(data=vertices, coordinates="points", pathcodes=pathcodes, style=pathstyle, offset=vertex, offset_coordinates=coordinates, mplobj=mplobj)
python
def draw_markers(self, data, coordinates, style, label, mplobj=None): """ Draw a set of markers. By default, this is done by repeatedly calling draw_path(), but renderers should generally overload this method to provide a more efficient implementation. In matplotlib, markers are created using the plt.plot() command. Parameters ---------- data : array_like A shape (N, 2) array of datapoints. coordinates : string A string code, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. style : dictionary a dictionary specifying the appearance of the markers. mplobj : matplotlib object the matplotlib plot element which generated this marker collection """ vertices, pathcodes = style['markerpath'] pathstyle = dict((key, style[key]) for key in ['alpha', 'edgecolor', 'facecolor', 'zorder', 'edgewidth']) pathstyle['dasharray'] = "10,0" for vertex in data: self.draw_path(data=vertices, coordinates="points", pathcodes=pathcodes, style=pathstyle, offset=vertex, offset_coordinates=coordinates, mplobj=mplobj)
[ "def", "draw_markers", "(", "self", ",", "data", ",", "coordinates", ",", "style", ",", "label", ",", "mplobj", "=", "None", ")", ":", "vertices", ",", "pathcodes", "=", "style", "[", "'markerpath'", "]", "pathstyle", "=", "dict", "(", "(", "key", ",", "style", "[", "key", "]", ")", "for", "key", "in", "[", "'alpha'", ",", "'edgecolor'", ",", "'facecolor'", ",", "'zorder'", ",", "'edgewidth'", "]", ")", "pathstyle", "[", "'dasharray'", "]", "=", "\"10,0\"", "for", "vertex", "in", "data", ":", "self", ".", "draw_path", "(", "data", "=", "vertices", ",", "coordinates", "=", "\"points\"", ",", "pathcodes", "=", "pathcodes", ",", "style", "=", "pathstyle", ",", "offset", "=", "vertex", ",", "offset_coordinates", "=", "coordinates", ",", "mplobj", "=", "mplobj", ")" ]
Draw a set of markers. By default, this is done by repeatedly calling draw_path(), but renderers should generally overload this method to provide a more efficient implementation. In matplotlib, markers are created using the plt.plot() command. Parameters ---------- data : array_like A shape (N, 2) array of datapoints. coordinates : string A string code, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. style : dictionary a dictionary specifying the appearance of the markers. mplobj : matplotlib object the matplotlib plot element which generated this marker collection
[ "Draw", "a", "set", "of", "markers", ".", "By", "default", "this", "is", "done", "by", "repeatedly", "calling", "draw_path", "()", "but", "renderers", "should", "generally", "overload", "this", "method", "to", "provide", "a", "more", "efficient", "implementation", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L584-L613
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py
Renderer.draw_path
def draw_path(self, data, coordinates, pathcodes, style, offset=None, offset_coordinates="data", mplobj=None): """ Draw a path. In matplotlib, paths are created by filled regions, histograms, contour plots, patches, etc. Parameters ---------- data : array_like A shape (N, 2) array of datapoints. coordinates : string A string code, which should be either 'data' for data coordinates, 'figure' for figure (pixel) coordinates, or "points" for raw point coordinates (useful in conjunction with offsets, below). pathcodes : list A list of single-character SVG pathcodes associated with the data. Path codes are one of ['M', 'm', 'L', 'l', 'Q', 'q', 'T', 't', 'S', 's', 'C', 'c', 'Z', 'z'] See the SVG specification for details. Note that some path codes consume more than one datapoint (while 'Z' consumes none), so in general, the length of the pathcodes list will not be the same as that of the data array. style : dictionary a dictionary specifying the appearance of the line. offset : list (optional) the (x, y) offset of the path. If not given, no offset will be used. offset_coordinates : string (optional) A string code, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. mplobj : matplotlib object the matplotlib plot element which generated this path """ raise NotImplementedError()
python
def draw_path(self, data, coordinates, pathcodes, style, offset=None, offset_coordinates="data", mplobj=None): """ Draw a path. In matplotlib, paths are created by filled regions, histograms, contour plots, patches, etc. Parameters ---------- data : array_like A shape (N, 2) array of datapoints. coordinates : string A string code, which should be either 'data' for data coordinates, 'figure' for figure (pixel) coordinates, or "points" for raw point coordinates (useful in conjunction with offsets, below). pathcodes : list A list of single-character SVG pathcodes associated with the data. Path codes are one of ['M', 'm', 'L', 'l', 'Q', 'q', 'T', 't', 'S', 's', 'C', 'c', 'Z', 'z'] See the SVG specification for details. Note that some path codes consume more than one datapoint (while 'Z' consumes none), so in general, the length of the pathcodes list will not be the same as that of the data array. style : dictionary a dictionary specifying the appearance of the line. offset : list (optional) the (x, y) offset of the path. If not given, no offset will be used. offset_coordinates : string (optional) A string code, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. mplobj : matplotlib object the matplotlib plot element which generated this path """ raise NotImplementedError()
[ "def", "draw_path", "(", "self", ",", "data", ",", "coordinates", ",", "pathcodes", ",", "style", ",", "offset", "=", "None", ",", "offset_coordinates", "=", "\"data\"", ",", "mplobj", "=", "None", ")", ":", "raise", "NotImplementedError", "(", ")" ]
Draw a path. In matplotlib, paths are created by filled regions, histograms, contour plots, patches, etc. Parameters ---------- data : array_like A shape (N, 2) array of datapoints. coordinates : string A string code, which should be either 'data' for data coordinates, 'figure' for figure (pixel) coordinates, or "points" for raw point coordinates (useful in conjunction with offsets, below). pathcodes : list A list of single-character SVG pathcodes associated with the data. Path codes are one of ['M', 'm', 'L', 'l', 'Q', 'q', 'T', 't', 'S', 's', 'C', 'c', 'Z', 'z'] See the SVG specification for details. Note that some path codes consume more than one datapoint (while 'Z' consumes none), so in general, the length of the pathcodes list will not be the same as that of the data array. style : dictionary a dictionary specifying the appearance of the line. offset : list (optional) the (x, y) offset of the path. If not given, no offset will be used. offset_coordinates : string (optional) A string code, which should be either 'data' for data coordinates, or 'figure' for figure (pixel) coordinates. mplobj : matplotlib object the matplotlib plot element which generated this path
[ "Draw", "a", "path", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/ext/_bundled/mplexporter.py#L638-L673
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC.from_times
def from_times(cls, times, delta_t=DEFAULT_OBSERVATION_TIME): """ Create a TimeMOC from a `astropy.time.Time` Parameters ---------- times : `astropy.time.Time` astropy observation times delta_t : `astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMOC order to represent the observations (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``). Returns ------- time_moc : `~mocpy.tmoc.TimeMOC` """ times_arr = np.asarray(times.jd * TimeMOC.DAY_MICRO_SEC, dtype=int) intervals_arr = np.vstack((times_arr, times_arr + 1)).T # degrade the TimeMoc to the order computer from ``delta_t`` order = TimeMOC.time_resolution_to_order(delta_t) return TimeMOC(IntervalSet(intervals_arr)).degrade_to_order(order)
python
def from_times(cls, times, delta_t=DEFAULT_OBSERVATION_TIME): """ Create a TimeMOC from a `astropy.time.Time` Parameters ---------- times : `astropy.time.Time` astropy observation times delta_t : `astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMOC order to represent the observations (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``). Returns ------- time_moc : `~mocpy.tmoc.TimeMOC` """ times_arr = np.asarray(times.jd * TimeMOC.DAY_MICRO_SEC, dtype=int) intervals_arr = np.vstack((times_arr, times_arr + 1)).T # degrade the TimeMoc to the order computer from ``delta_t`` order = TimeMOC.time_resolution_to_order(delta_t) return TimeMOC(IntervalSet(intervals_arr)).degrade_to_order(order)
[ "def", "from_times", "(", "cls", ",", "times", ",", "delta_t", "=", "DEFAULT_OBSERVATION_TIME", ")", ":", "times_arr", "=", "np", ".", "asarray", "(", "times", ".", "jd", "*", "TimeMOC", ".", "DAY_MICRO_SEC", ",", "dtype", "=", "int", ")", "intervals_arr", "=", "np", ".", "vstack", "(", "(", "times_arr", ",", "times_arr", "+", "1", ")", ")", ".", "T", "# degrade the TimeMoc to the order computer from ``delta_t``", "order", "=", "TimeMOC", ".", "time_resolution_to_order", "(", "delta_t", ")", "return", "TimeMOC", "(", "IntervalSet", "(", "intervals_arr", ")", ")", ".", "degrade_to_order", "(", "order", ")" ]
Create a TimeMOC from a `astropy.time.Time` Parameters ---------- times : `astropy.time.Time` astropy observation times delta_t : `astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMOC order to represent the observations (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``). Returns ------- time_moc : `~mocpy.tmoc.TimeMOC`
[ "Create", "a", "TimeMOC", "from", "a", "astropy", ".", "time", ".", "Time" ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L31-L53
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC.from_time_ranges
def from_time_ranges(cls, min_times, max_times, delta_t=DEFAULT_OBSERVATION_TIME): """ Create a TimeMOC from a range defined by two `astropy.time.Time` Parameters ---------- min_times : `astropy.time.Time` astropy times defining the left part of the intervals max_times : `astropy.time.Time` astropy times defining the right part of the intervals delta_t : `astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMOC order to represent the observations (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``). Returns ------- time_moc : `~mocpy.tmoc.TimeMOC` """ min_times_arr = np.asarray(min_times.jd * TimeMOC.DAY_MICRO_SEC, dtype=int) max_times_arr = np.asarray(max_times.jd * TimeMOC.DAY_MICRO_SEC, dtype=int) intervals_arr = np.vstack((min_times_arr, max_times_arr + 1)).T # degrade the TimeMoc to the order computer from ``delta_t`` order = TimeMOC.time_resolution_to_order(delta_t) return TimeMOC(IntervalSet(intervals_arr)).degrade_to_order(order)
python
def from_time_ranges(cls, min_times, max_times, delta_t=DEFAULT_OBSERVATION_TIME): """ Create a TimeMOC from a range defined by two `astropy.time.Time` Parameters ---------- min_times : `astropy.time.Time` astropy times defining the left part of the intervals max_times : `astropy.time.Time` astropy times defining the right part of the intervals delta_t : `astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMOC order to represent the observations (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``). Returns ------- time_moc : `~mocpy.tmoc.TimeMOC` """ min_times_arr = np.asarray(min_times.jd * TimeMOC.DAY_MICRO_SEC, dtype=int) max_times_arr = np.asarray(max_times.jd * TimeMOC.DAY_MICRO_SEC, dtype=int) intervals_arr = np.vstack((min_times_arr, max_times_arr + 1)).T # degrade the TimeMoc to the order computer from ``delta_t`` order = TimeMOC.time_resolution_to_order(delta_t) return TimeMOC(IntervalSet(intervals_arr)).degrade_to_order(order)
[ "def", "from_time_ranges", "(", "cls", ",", "min_times", ",", "max_times", ",", "delta_t", "=", "DEFAULT_OBSERVATION_TIME", ")", ":", "min_times_arr", "=", "np", ".", "asarray", "(", "min_times", ".", "jd", "*", "TimeMOC", ".", "DAY_MICRO_SEC", ",", "dtype", "=", "int", ")", "max_times_arr", "=", "np", ".", "asarray", "(", "max_times", ".", "jd", "*", "TimeMOC", ".", "DAY_MICRO_SEC", ",", "dtype", "=", "int", ")", "intervals_arr", "=", "np", ".", "vstack", "(", "(", "min_times_arr", ",", "max_times_arr", "+", "1", ")", ")", ".", "T", "# degrade the TimeMoc to the order computer from ``delta_t``", "order", "=", "TimeMOC", ".", "time_resolution_to_order", "(", "delta_t", ")", "return", "TimeMOC", "(", "IntervalSet", "(", "intervals_arr", ")", ")", ".", "degrade_to_order", "(", "order", ")" ]
Create a TimeMOC from a range defined by two `astropy.time.Time` Parameters ---------- min_times : `astropy.time.Time` astropy times defining the left part of the intervals max_times : `astropy.time.Time` astropy times defining the right part of the intervals delta_t : `astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMOC order to represent the observations (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``). Returns ------- time_moc : `~mocpy.tmoc.TimeMOC`
[ "Create", "a", "TimeMOC", "from", "a", "range", "defined", "by", "two", "astropy", ".", "time", ".", "Time" ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L56-L82
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC.add_neighbours
def add_neighbours(self): """ Add all the pixels at max order in the neighbourhood of the moc """ time_delta = 1 << (2*(IntervalSet.HPY_MAX_ORDER - self.max_order)) intervals_arr = self._interval_set._intervals intervals_arr[:, 0] = np.maximum(intervals_arr[:, 0] - time_delta, 0) intervals_arr[:, 1] = np.minimum(intervals_arr[:, 1] + time_delta, (1 << 58) - 1) self._interval_set = IntervalSet(intervals_arr)
python
def add_neighbours(self): """ Add all the pixels at max order in the neighbourhood of the moc """ time_delta = 1 << (2*(IntervalSet.HPY_MAX_ORDER - self.max_order)) intervals_arr = self._interval_set._intervals intervals_arr[:, 0] = np.maximum(intervals_arr[:, 0] - time_delta, 0) intervals_arr[:, 1] = np.minimum(intervals_arr[:, 1] + time_delta, (1 << 58) - 1) self._interval_set = IntervalSet(intervals_arr)
[ "def", "add_neighbours", "(", "self", ")", ":", "time_delta", "=", "1", "<<", "(", "2", "*", "(", "IntervalSet", ".", "HPY_MAX_ORDER", "-", "self", ".", "max_order", ")", ")", "intervals_arr", "=", "self", ".", "_interval_set", ".", "_intervals", "intervals_arr", "[", ":", ",", "0", "]", "=", "np", ".", "maximum", "(", "intervals_arr", "[", ":", ",", "0", "]", "-", "time_delta", ",", "0", ")", "intervals_arr", "[", ":", ",", "1", "]", "=", "np", ".", "minimum", "(", "intervals_arr", "[", ":", ",", "1", "]", "+", "time_delta", ",", "(", "1", "<<", "58", ")", "-", "1", ")", "self", ".", "_interval_set", "=", "IntervalSet", "(", "intervals_arr", ")" ]
Add all the pixels at max order in the neighbourhood of the moc
[ "Add", "all", "the", "pixels", "at", "max", "order", "in", "the", "neighbourhood", "of", "the", "moc" ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L84-L95
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC.remove_neighbours
def remove_neighbours(self): """ Remove all the pixels at max order located at the bound of the moc """ time_delta = 1 << (2*(IntervalSet.HPY_MAX_ORDER - self.max_order)) intervals_arr = self._interval_set._intervals intervals_arr[:, 0] = np.minimum(intervals_arr[:, 0] + time_delta, (1 << 58) - 1) intervals_arr[:, 1] = np.maximum(intervals_arr[:, 1] - time_delta, 0) good_intervals = intervals_arr[:, 1] > intervals_arr[:, 0] self._interval_set = IntervalSet(intervals_arr[good_intervals])
python
def remove_neighbours(self): """ Remove all the pixels at max order located at the bound of the moc """ time_delta = 1 << (2*(IntervalSet.HPY_MAX_ORDER - self.max_order)) intervals_arr = self._interval_set._intervals intervals_arr[:, 0] = np.minimum(intervals_arr[:, 0] + time_delta, (1 << 58) - 1) intervals_arr[:, 1] = np.maximum(intervals_arr[:, 1] - time_delta, 0) good_intervals = intervals_arr[:, 1] > intervals_arr[:, 0] self._interval_set = IntervalSet(intervals_arr[good_intervals])
[ "def", "remove_neighbours", "(", "self", ")", ":", "time_delta", "=", "1", "<<", "(", "2", "*", "(", "IntervalSet", ".", "HPY_MAX_ORDER", "-", "self", ".", "max_order", ")", ")", "intervals_arr", "=", "self", ".", "_interval_set", ".", "_intervals", "intervals_arr", "[", ":", ",", "0", "]", "=", "np", ".", "minimum", "(", "intervals_arr", "[", ":", ",", "0", "]", "+", "time_delta", ",", "(", "1", "<<", "58", ")", "-", "1", ")", "intervals_arr", "[", ":", ",", "1", "]", "=", "np", ".", "maximum", "(", "intervals_arr", "[", ":", ",", "1", "]", "-", "time_delta", ",", "0", ")", "good_intervals", "=", "intervals_arr", "[", ":", ",", "1", "]", ">", "intervals_arr", "[", ":", ",", "0", "]", "self", ".", "_interval_set", "=", "IntervalSet", "(", "intervals_arr", "[", "good_intervals", "]", ")" ]
Remove all the pixels at max order located at the bound of the moc
[ "Remove", "all", "the", "pixels", "at", "max", "order", "located", "at", "the", "bound", "of", "the", "moc" ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L97-L110
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC._process_degradation
def _process_degradation(self, another_moc, order_op): """ Degrade (down-sampling) self and ``another_moc`` to ``order_op`` order Parameters ---------- another_moc : `~mocpy.tmoc.TimeMoc` order_op : int the order in which self and ``another_moc`` will be down-sampled to. Returns ------- result : (`~mocpy.tmoc.TimeMoc`, `~mocpy.tmoc.TimeMoc`) self and ``another_moc`` degraded TimeMocs """ max_order = max(self.max_order, another_moc.max_order) if order_op > max_order: message = 'Requested time resolution for the operation cannot be applied.\n' \ 'The TimeMoc object resulting from the operation is of time resolution {0} sec.'.format( TimeMOC.order_to_time_resolution(max_order).sec) warnings.warn(message, UserWarning) self_degradation = self.degrade_to_order(order_op) another_moc_degradation = another_moc.degrade_to_order(order_op) result = self_degradation, another_moc_degradation return result
python
def _process_degradation(self, another_moc, order_op): """ Degrade (down-sampling) self and ``another_moc`` to ``order_op`` order Parameters ---------- another_moc : `~mocpy.tmoc.TimeMoc` order_op : int the order in which self and ``another_moc`` will be down-sampled to. Returns ------- result : (`~mocpy.tmoc.TimeMoc`, `~mocpy.tmoc.TimeMoc`) self and ``another_moc`` degraded TimeMocs """ max_order = max(self.max_order, another_moc.max_order) if order_op > max_order: message = 'Requested time resolution for the operation cannot be applied.\n' \ 'The TimeMoc object resulting from the operation is of time resolution {0} sec.'.format( TimeMOC.order_to_time_resolution(max_order).sec) warnings.warn(message, UserWarning) self_degradation = self.degrade_to_order(order_op) another_moc_degradation = another_moc.degrade_to_order(order_op) result = self_degradation, another_moc_degradation return result
[ "def", "_process_degradation", "(", "self", ",", "another_moc", ",", "order_op", ")", ":", "max_order", "=", "max", "(", "self", ".", "max_order", ",", "another_moc", ".", "max_order", ")", "if", "order_op", ">", "max_order", ":", "message", "=", "'Requested time resolution for the operation cannot be applied.\\n'", "'The TimeMoc object resulting from the operation is of time resolution {0} sec.'", ".", "format", "(", "TimeMOC", ".", "order_to_time_resolution", "(", "max_order", ")", ".", "sec", ")", "warnings", ".", "warn", "(", "message", ",", "UserWarning", ")", "self_degradation", "=", "self", ".", "degrade_to_order", "(", "order_op", ")", "another_moc_degradation", "=", "another_moc", ".", "degrade_to_order", "(", "order_op", ")", "result", "=", "self_degradation", ",", "another_moc_degradation", "return", "result" ]
Degrade (down-sampling) self and ``another_moc`` to ``order_op`` order Parameters ---------- another_moc : `~mocpy.tmoc.TimeMoc` order_op : int the order in which self and ``another_moc`` will be down-sampled to. Returns ------- result : (`~mocpy.tmoc.TimeMoc`, `~mocpy.tmoc.TimeMoc`) self and ``another_moc`` degraded TimeMocs
[ "Degrade", "(", "down", "-", "sampling", ")", "self", "and", "another_moc", "to", "order_op", "order" ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L123-L150
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC.intersection
def intersection(self, another_moc, delta_t=DEFAULT_OBSERVATION_TIME): """ Intersection between self and moc. ``delta_t`` gives the possibility to the user to set a time resolution for performing the tmoc intersection Parameters ---------- another_moc : `~mocpy.abstract_moc.AbstractMOC` the MOC/TimeMOC used for performing the intersection with self delta_t : `~astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMoc order to represent the observations. (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``) Returns ------- result : `~mocpy.moc.MOC` or `~mocpy.tmoc.TimeMOC` MOC object whose interval set corresponds to : self & ``moc`` """ order_op = TimeMOC.time_resolution_to_order(delta_t) self_degraded, moc_degraded = self._process_degradation(another_moc, order_op) return super(TimeMOC, self_degraded).intersection(moc_degraded)
python
def intersection(self, another_moc, delta_t=DEFAULT_OBSERVATION_TIME): """ Intersection between self and moc. ``delta_t`` gives the possibility to the user to set a time resolution for performing the tmoc intersection Parameters ---------- another_moc : `~mocpy.abstract_moc.AbstractMOC` the MOC/TimeMOC used for performing the intersection with self delta_t : `~astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMoc order to represent the observations. (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``) Returns ------- result : `~mocpy.moc.MOC` or `~mocpy.tmoc.TimeMOC` MOC object whose interval set corresponds to : self & ``moc`` """ order_op = TimeMOC.time_resolution_to_order(delta_t) self_degraded, moc_degraded = self._process_degradation(another_moc, order_op) return super(TimeMOC, self_degraded).intersection(moc_degraded)
[ "def", "intersection", "(", "self", ",", "another_moc", ",", "delta_t", "=", "DEFAULT_OBSERVATION_TIME", ")", ":", "order_op", "=", "TimeMOC", ".", "time_resolution_to_order", "(", "delta_t", ")", "self_degraded", ",", "moc_degraded", "=", "self", ".", "_process_degradation", "(", "another_moc", ",", "order_op", ")", "return", "super", "(", "TimeMOC", ",", "self_degraded", ")", ".", "intersection", "(", "moc_degraded", ")" ]
Intersection between self and moc. ``delta_t`` gives the possibility to the user to set a time resolution for performing the tmoc intersection Parameters ---------- another_moc : `~mocpy.abstract_moc.AbstractMOC` the MOC/TimeMOC used for performing the intersection with self delta_t : `~astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMoc order to represent the observations. (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``) Returns ------- result : `~mocpy.moc.MOC` or `~mocpy.tmoc.TimeMOC` MOC object whose interval set corresponds to : self & ``moc``
[ "Intersection", "between", "self", "and", "moc", ".", "delta_t", "gives", "the", "possibility", "to", "the", "user", "to", "set", "a", "time", "resolution", "for", "performing", "the", "tmoc", "intersection" ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L152-L176
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC.total_duration
def total_duration(self): """ Get the total duration covered by the temporal moc Returns ------- duration : `~astropy.time.TimeDelta` total duration of all the observation times of the tmoc total duration of all the observation times of the tmoc """ if self._interval_set.empty(): return 0 total_time_us = 0 # The interval set is checked for consistency before looping over all the intervals for (start_time, stop_time) in self._interval_set._intervals: total_time_us = total_time_us + (stop_time - start_time) duration = TimeDelta(total_time_us / 1e6, format='sec', scale='tdb') return duration
python
def total_duration(self): """ Get the total duration covered by the temporal moc Returns ------- duration : `~astropy.time.TimeDelta` total duration of all the observation times of the tmoc total duration of all the observation times of the tmoc """ if self._interval_set.empty(): return 0 total_time_us = 0 # The interval set is checked for consistency before looping over all the intervals for (start_time, stop_time) in self._interval_set._intervals: total_time_us = total_time_us + (stop_time - start_time) duration = TimeDelta(total_time_us / 1e6, format='sec', scale='tdb') return duration
[ "def", "total_duration", "(", "self", ")", ":", "if", "self", ".", "_interval_set", ".", "empty", "(", ")", ":", "return", "0", "total_time_us", "=", "0", "# The interval set is checked for consistency before looping over all the intervals", "for", "(", "start_time", ",", "stop_time", ")", "in", "self", ".", "_interval_set", ".", "_intervals", ":", "total_time_us", "=", "total_time_us", "+", "(", "stop_time", "-", "start_time", ")", "duration", "=", "TimeDelta", "(", "total_time_us", "/", "1e6", ",", "format", "=", "'sec'", ",", "scale", "=", "'tdb'", ")", "return", "duration" ]
Get the total duration covered by the temporal moc Returns ------- duration : `~astropy.time.TimeDelta` total duration of all the observation times of the tmoc total duration of all the observation times of the tmoc
[ "Get", "the", "total", "duration", "covered", "by", "the", "temporal", "moc" ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L230-L251
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC.consistency
def consistency(self): """ Get a percentage of fill between the min and max time the moc is defined. A value near 0 shows a sparse temporal moc (i.e. the moc does not cover a lot of time and covers very distant times. A value near 1 means that the moc covers a lot of time without big pauses. Returns ------- result : float fill percentage (between 0 and 1.) """ result = self.total_duration.jd / (self.max_time - self.min_time).jd return result
python
def consistency(self): """ Get a percentage of fill between the min and max time the moc is defined. A value near 0 shows a sparse temporal moc (i.e. the moc does not cover a lot of time and covers very distant times. A value near 1 means that the moc covers a lot of time without big pauses. Returns ------- result : float fill percentage (between 0 and 1.) """ result = self.total_duration.jd / (self.max_time - self.min_time).jd return result
[ "def", "consistency", "(", "self", ")", ":", "result", "=", "self", ".", "total_duration", ".", "jd", "/", "(", "self", ".", "max_time", "-", "self", ".", "min_time", ")", ".", "jd", "return", "result" ]
Get a percentage of fill between the min and max time the moc is defined. A value near 0 shows a sparse temporal moc (i.e. the moc does not cover a lot of time and covers very distant times. A value near 1 means that the moc covers a lot of time without big pauses. Returns ------- result : float fill percentage (between 0 and 1.)
[ "Get", "a", "percentage", "of", "fill", "between", "the", "min", "and", "max", "time", "the", "moc", "is", "defined", "." ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L254-L270
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC.min_time
def min_time(self): """ Get the `~astropy.time.Time` time of the tmoc first observation Returns ------- min_time : `astropy.time.Time` time of the first observation """ min_time = Time(self._interval_set.min / TimeMOC.DAY_MICRO_SEC, format='jd', scale='tdb') return min_time
python
def min_time(self): """ Get the `~astropy.time.Time` time of the tmoc first observation Returns ------- min_time : `astropy.time.Time` time of the first observation """ min_time = Time(self._interval_set.min / TimeMOC.DAY_MICRO_SEC, format='jd', scale='tdb') return min_time
[ "def", "min_time", "(", "self", ")", ":", "min_time", "=", "Time", "(", "self", ".", "_interval_set", ".", "min", "/", "TimeMOC", ".", "DAY_MICRO_SEC", ",", "format", "=", "'jd'", ",", "scale", "=", "'tdb'", ")", "return", "min_time" ]
Get the `~astropy.time.Time` time of the tmoc first observation Returns ------- min_time : `astropy.time.Time` time of the first observation
[ "Get", "the", "~astropy", ".", "time", ".", "Time", "time", "of", "the", "tmoc", "first", "observation" ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L273-L285
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC.max_time
def max_time(self): """ Get the `~astropy.time.Time` time of the tmoc last observation Returns ------- max_time : `~astropy.time.Time` time of the last observation """ max_time = Time(self._interval_set.max / TimeMOC.DAY_MICRO_SEC, format='jd', scale='tdb') return max_time
python
def max_time(self): """ Get the `~astropy.time.Time` time of the tmoc last observation Returns ------- max_time : `~astropy.time.Time` time of the last observation """ max_time = Time(self._interval_set.max / TimeMOC.DAY_MICRO_SEC, format='jd', scale='tdb') return max_time
[ "def", "max_time", "(", "self", ")", ":", "max_time", "=", "Time", "(", "self", ".", "_interval_set", ".", "max", "/", "TimeMOC", ".", "DAY_MICRO_SEC", ",", "format", "=", "'jd'", ",", "scale", "=", "'tdb'", ")", "return", "max_time" ]
Get the `~astropy.time.Time` time of the tmoc last observation Returns ------- max_time : `~astropy.time.Time` time of the last observation
[ "Get", "the", "~astropy", ".", "time", ".", "Time", "time", "of", "the", "tmoc", "last", "observation" ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L288-L300
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC.contains
def contains(self, times, keep_inside=True, delta_t=DEFAULT_OBSERVATION_TIME): """ Get a mask array (e.g. a numpy boolean array) of times being inside (or outside) the TMOC instance. Parameters ---------- times : `astropy.time.Time` astropy times to check whether they are contained in the TMOC or not. keep_inside : bool, optional True by default. If so the filtered table contains only observations that are located the MOC. If ``keep_inside`` is False, the filtered table contains all observations lying outside the MOC. delta_t : `astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMOC order to represent the observations (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``). Returns ------- array : `~numpy.darray` A mask boolean array """ # the requested order for filtering the astropy observations table is more precise than the order # of the TimeMoc object current_max_order = self.max_order new_max_order = TimeMOC.time_resolution_to_order(delta_t) if new_max_order > current_max_order: message = 'Requested time resolution filtering cannot be applied.\n' \ 'Filtering is applied with a time resolution of {0} sec.'.format( TimeMOC.order_to_time_resolution(current_max_order).sec) warnings.warn(message, UserWarning) rough_tmoc = self.degrade_to_order(new_max_order) pix_arr = (times.jd * TimeMOC.DAY_MICRO_SEC) pix_arr = pix_arr.astype(int) intervals_arr = rough_tmoc._interval_set._intervals inf_arr = np.vstack([pix_arr[i] >= intervals_arr[:, 0] for i in range(pix_arr.shape[0])]) sup_arr = np.vstack([pix_arr[i] <= intervals_arr[:, 1] for i in range(pix_arr.shape[0])]) if keep_inside: res = inf_arr & sup_arr filtered_rows = np.any(res, axis=1) else: res = ~inf_arr | ~sup_arr filtered_rows = np.all(res, axis=1) return filtered_rows
python
def contains(self, times, keep_inside=True, delta_t=DEFAULT_OBSERVATION_TIME): """ Get a mask array (e.g. a numpy boolean array) of times being inside (or outside) the TMOC instance. Parameters ---------- times : `astropy.time.Time` astropy times to check whether they are contained in the TMOC or not. keep_inside : bool, optional True by default. If so the filtered table contains only observations that are located the MOC. If ``keep_inside`` is False, the filtered table contains all observations lying outside the MOC. delta_t : `astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMOC order to represent the observations (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``). Returns ------- array : `~numpy.darray` A mask boolean array """ # the requested order for filtering the astropy observations table is more precise than the order # of the TimeMoc object current_max_order = self.max_order new_max_order = TimeMOC.time_resolution_to_order(delta_t) if new_max_order > current_max_order: message = 'Requested time resolution filtering cannot be applied.\n' \ 'Filtering is applied with a time resolution of {0} sec.'.format( TimeMOC.order_to_time_resolution(current_max_order).sec) warnings.warn(message, UserWarning) rough_tmoc = self.degrade_to_order(new_max_order) pix_arr = (times.jd * TimeMOC.DAY_MICRO_SEC) pix_arr = pix_arr.astype(int) intervals_arr = rough_tmoc._interval_set._intervals inf_arr = np.vstack([pix_arr[i] >= intervals_arr[:, 0] for i in range(pix_arr.shape[0])]) sup_arr = np.vstack([pix_arr[i] <= intervals_arr[:, 1] for i in range(pix_arr.shape[0])]) if keep_inside: res = inf_arr & sup_arr filtered_rows = np.any(res, axis=1) else: res = ~inf_arr | ~sup_arr filtered_rows = np.all(res, axis=1) return filtered_rows
[ "def", "contains", "(", "self", ",", "times", ",", "keep_inside", "=", "True", ",", "delta_t", "=", "DEFAULT_OBSERVATION_TIME", ")", ":", "# the requested order for filtering the astropy observations table is more precise than the order", "# of the TimeMoc object", "current_max_order", "=", "self", ".", "max_order", "new_max_order", "=", "TimeMOC", ".", "time_resolution_to_order", "(", "delta_t", ")", "if", "new_max_order", ">", "current_max_order", ":", "message", "=", "'Requested time resolution filtering cannot be applied.\\n'", "'Filtering is applied with a time resolution of {0} sec.'", ".", "format", "(", "TimeMOC", ".", "order_to_time_resolution", "(", "current_max_order", ")", ".", "sec", ")", "warnings", ".", "warn", "(", "message", ",", "UserWarning", ")", "rough_tmoc", "=", "self", ".", "degrade_to_order", "(", "new_max_order", ")", "pix_arr", "=", "(", "times", ".", "jd", "*", "TimeMOC", ".", "DAY_MICRO_SEC", ")", "pix_arr", "=", "pix_arr", ".", "astype", "(", "int", ")", "intervals_arr", "=", "rough_tmoc", ".", "_interval_set", ".", "_intervals", "inf_arr", "=", "np", ".", "vstack", "(", "[", "pix_arr", "[", "i", "]", ">=", "intervals_arr", "[", ":", ",", "0", "]", "for", "i", "in", "range", "(", "pix_arr", ".", "shape", "[", "0", "]", ")", "]", ")", "sup_arr", "=", "np", ".", "vstack", "(", "[", "pix_arr", "[", "i", "]", "<=", "intervals_arr", "[", ":", ",", "1", "]", "for", "i", "in", "range", "(", "pix_arr", ".", "shape", "[", "0", "]", ")", "]", ")", "if", "keep_inside", ":", "res", "=", "inf_arr", "&", "sup_arr", "filtered_rows", "=", "np", ".", "any", "(", "res", ",", "axis", "=", "1", ")", "else", ":", "res", "=", "~", "inf_arr", "|", "~", "sup_arr", "filtered_rows", "=", "np", ".", "all", "(", "res", ",", "axis", "=", "1", ")", "return", "filtered_rows" ]
Get a mask array (e.g. a numpy boolean array) of times being inside (or outside) the TMOC instance. Parameters ---------- times : `astropy.time.Time` astropy times to check whether they are contained in the TMOC or not. keep_inside : bool, optional True by default. If so the filtered table contains only observations that are located the MOC. If ``keep_inside`` is False, the filtered table contains all observations lying outside the MOC. delta_t : `astropy.time.TimeDelta`, optional the duration of one observation. It is set to 30 min by default. This data is used to compute the more efficient TimeMOC order to represent the observations (Best order = the less precise order which is able to discriminate two observations separated by ``delta_t``). Returns ------- array : `~numpy.darray` A mask boolean array
[ "Get", "a", "mask", "array", "(", "e", ".", "g", ".", "a", "numpy", "boolean", "array", ")", "of", "times", "being", "inside", "(", "or", "outside", ")", "the", "TMOC", "instance", "." ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L302-L350
train
cds-astro/mocpy
mocpy/tmoc/tmoc.py
TimeMOC.plot
def plot(self, title='TimeMoc', view=(None, None)): """ Plot the TimeMoc in a time window. This method uses interactive matplotlib. The user can move its mouse through the plot to see the time (at the mouse position). Parameters ---------- title : str, optional The title of the plot. Set to 'TimeMoc' by default. view : (`~astropy.time.Time`, `~astropy.time.Time`), optional Define the view window in which the observations are plotted. Set to (None, None) by default (i.e. all the observation time window is rendered). """ from matplotlib.colors import LinearSegmentedColormap import matplotlib.pyplot as plt if self._interval_set.empty(): print('Nothing to print. This TimeMoc object is empty.') return plot_order = 15 if self.max_order > plot_order: plotted_moc = self.degrade_to_order(plot_order) else: plotted_moc = self min_jd = plotted_moc.min_time.jd if not view[0] else view[0].jd max_jd = plotted_moc.max_time.jd if not view[1] else view[1].jd if max_jd < min_jd: raise ValueError("Invalid selection: max_jd = {0} must be > to min_jd = {1}".format(max_jd, min_jd)) fig1 = plt.figure(figsize=(9.5, 5)) ax = fig1.add_subplot(111) ax.set_xlabel('iso') ax.get_yaxis().set_visible(False) size = 2000 delta = (max_jd - min_jd) / size min_jd_time = min_jd ax.set_xticks([0, size]) ax.set_xticklabels(Time([min_jd_time, max_jd], format='jd', scale='tdb').iso, rotation=70) y = np.zeros(size) for (s_time_us, e_time_us) in plotted_moc._interval_set._intervals: s_index = int((s_time_us / TimeMOC.DAY_MICRO_SEC - min_jd_time) / delta) e_index = int((e_time_us / TimeMOC.DAY_MICRO_SEC - min_jd_time) / delta) y[s_index:(e_index+1)] = 1.0 # hack in case of full time mocs. if np.all(y): y[0] = 0 z = np.tile(y, (int(size//10), 1)) plt.title(title) color_map = LinearSegmentedColormap.from_list('w2r', ['#fffff0', '#aa0000']) color_map.set_under('w') color_map.set_bad('gray') plt.imshow(z, interpolation='bilinear', cmap=color_map) def on_mouse_motion(event): for txt in ax.texts: txt.set_visible(False) text = ax.text(0, 0, "", va="bottom", ha="left") time = Time(event.xdata * delta + min_jd_time, format='jd', scale='tdb') tx = '{0}'.format(time.iso) text.set_position((event.xdata - 50, 700)) text.set_rotation(70) text.set_text(tx) cid = fig1.canvas.mpl_connect('motion_notify_event', on_mouse_motion) plt.show()
python
def plot(self, title='TimeMoc', view=(None, None)): """ Plot the TimeMoc in a time window. This method uses interactive matplotlib. The user can move its mouse through the plot to see the time (at the mouse position). Parameters ---------- title : str, optional The title of the plot. Set to 'TimeMoc' by default. view : (`~astropy.time.Time`, `~astropy.time.Time`), optional Define the view window in which the observations are plotted. Set to (None, None) by default (i.e. all the observation time window is rendered). """ from matplotlib.colors import LinearSegmentedColormap import matplotlib.pyplot as plt if self._interval_set.empty(): print('Nothing to print. This TimeMoc object is empty.') return plot_order = 15 if self.max_order > plot_order: plotted_moc = self.degrade_to_order(plot_order) else: plotted_moc = self min_jd = plotted_moc.min_time.jd if not view[0] else view[0].jd max_jd = plotted_moc.max_time.jd if not view[1] else view[1].jd if max_jd < min_jd: raise ValueError("Invalid selection: max_jd = {0} must be > to min_jd = {1}".format(max_jd, min_jd)) fig1 = plt.figure(figsize=(9.5, 5)) ax = fig1.add_subplot(111) ax.set_xlabel('iso') ax.get_yaxis().set_visible(False) size = 2000 delta = (max_jd - min_jd) / size min_jd_time = min_jd ax.set_xticks([0, size]) ax.set_xticklabels(Time([min_jd_time, max_jd], format='jd', scale='tdb').iso, rotation=70) y = np.zeros(size) for (s_time_us, e_time_us) in plotted_moc._interval_set._intervals: s_index = int((s_time_us / TimeMOC.DAY_MICRO_SEC - min_jd_time) / delta) e_index = int((e_time_us / TimeMOC.DAY_MICRO_SEC - min_jd_time) / delta) y[s_index:(e_index+1)] = 1.0 # hack in case of full time mocs. if np.all(y): y[0] = 0 z = np.tile(y, (int(size//10), 1)) plt.title(title) color_map = LinearSegmentedColormap.from_list('w2r', ['#fffff0', '#aa0000']) color_map.set_under('w') color_map.set_bad('gray') plt.imshow(z, interpolation='bilinear', cmap=color_map) def on_mouse_motion(event): for txt in ax.texts: txt.set_visible(False) text = ax.text(0, 0, "", va="bottom", ha="left") time = Time(event.xdata * delta + min_jd_time, format='jd', scale='tdb') tx = '{0}'.format(time.iso) text.set_position((event.xdata - 50, 700)) text.set_rotation(70) text.set_text(tx) cid = fig1.canvas.mpl_connect('motion_notify_event', on_mouse_motion) plt.show()
[ "def", "plot", "(", "self", ",", "title", "=", "'TimeMoc'", ",", "view", "=", "(", "None", ",", "None", ")", ")", ":", "from", "matplotlib", ".", "colors", "import", "LinearSegmentedColormap", "import", "matplotlib", ".", "pyplot", "as", "plt", "if", "self", ".", "_interval_set", ".", "empty", "(", ")", ":", "print", "(", "'Nothing to print. This TimeMoc object is empty.'", ")", "return", "plot_order", "=", "15", "if", "self", ".", "max_order", ">", "plot_order", ":", "plotted_moc", "=", "self", ".", "degrade_to_order", "(", "plot_order", ")", "else", ":", "plotted_moc", "=", "self", "min_jd", "=", "plotted_moc", ".", "min_time", ".", "jd", "if", "not", "view", "[", "0", "]", "else", "view", "[", "0", "]", ".", "jd", "max_jd", "=", "plotted_moc", ".", "max_time", ".", "jd", "if", "not", "view", "[", "1", "]", "else", "view", "[", "1", "]", ".", "jd", "if", "max_jd", "<", "min_jd", ":", "raise", "ValueError", "(", "\"Invalid selection: max_jd = {0} must be > to min_jd = {1}\"", ".", "format", "(", "max_jd", ",", "min_jd", ")", ")", "fig1", "=", "plt", ".", "figure", "(", "figsize", "=", "(", "9.5", ",", "5", ")", ")", "ax", "=", "fig1", ".", "add_subplot", "(", "111", ")", "ax", ".", "set_xlabel", "(", "'iso'", ")", "ax", ".", "get_yaxis", "(", ")", ".", "set_visible", "(", "False", ")", "size", "=", "2000", "delta", "=", "(", "max_jd", "-", "min_jd", ")", "/", "size", "min_jd_time", "=", "min_jd", "ax", ".", "set_xticks", "(", "[", "0", ",", "size", "]", ")", "ax", ".", "set_xticklabels", "(", "Time", "(", "[", "min_jd_time", ",", "max_jd", "]", ",", "format", "=", "'jd'", ",", "scale", "=", "'tdb'", ")", ".", "iso", ",", "rotation", "=", "70", ")", "y", "=", "np", ".", "zeros", "(", "size", ")", "for", "(", "s_time_us", ",", "e_time_us", ")", "in", "plotted_moc", ".", "_interval_set", ".", "_intervals", ":", "s_index", "=", "int", "(", "(", "s_time_us", "/", "TimeMOC", ".", "DAY_MICRO_SEC", "-", "min_jd_time", ")", "/", "delta", ")", "e_index", "=", "int", "(", "(", "e_time_us", "/", "TimeMOC", ".", "DAY_MICRO_SEC", "-", "min_jd_time", ")", "/", "delta", ")", "y", "[", "s_index", ":", "(", "e_index", "+", "1", ")", "]", "=", "1.0", "# hack in case of full time mocs.", "if", "np", ".", "all", "(", "y", ")", ":", "y", "[", "0", "]", "=", "0", "z", "=", "np", ".", "tile", "(", "y", ",", "(", "int", "(", "size", "//", "10", ")", ",", "1", ")", ")", "plt", ".", "title", "(", "title", ")", "color_map", "=", "LinearSegmentedColormap", ".", "from_list", "(", "'w2r'", ",", "[", "'#fffff0'", ",", "'#aa0000'", "]", ")", "color_map", ".", "set_under", "(", "'w'", ")", "color_map", ".", "set_bad", "(", "'gray'", ")", "plt", ".", "imshow", "(", "z", ",", "interpolation", "=", "'bilinear'", ",", "cmap", "=", "color_map", ")", "def", "on_mouse_motion", "(", "event", ")", ":", "for", "txt", "in", "ax", ".", "texts", ":", "txt", ".", "set_visible", "(", "False", ")", "text", "=", "ax", ".", "text", "(", "0", ",", "0", ",", "\"\"", ",", "va", "=", "\"bottom\"", ",", "ha", "=", "\"left\"", ")", "time", "=", "Time", "(", "event", ".", "xdata", "*", "delta", "+", "min_jd_time", ",", "format", "=", "'jd'", ",", "scale", "=", "'tdb'", ")", "tx", "=", "'{0}'", ".", "format", "(", "time", ".", "iso", ")", "text", ".", "set_position", "(", "(", "event", ".", "xdata", "-", "50", ",", "700", ")", ")", "text", ".", "set_rotation", "(", "70", ")", "text", ".", "set_text", "(", "tx", ")", "cid", "=", "fig1", ".", "canvas", ".", "mpl_connect", "(", "'motion_notify_event'", ",", "on_mouse_motion", ")", "plt", ".", "show", "(", ")" ]
Plot the TimeMoc in a time window. This method uses interactive matplotlib. The user can move its mouse through the plot to see the time (at the mouse position). Parameters ---------- title : str, optional The title of the plot. Set to 'TimeMoc' by default. view : (`~astropy.time.Time`, `~astropy.time.Time`), optional Define the view window in which the observations are plotted. Set to (None, None) by default (i.e. all the observation time window is rendered).
[ "Plot", "the", "TimeMoc", "in", "a", "time", "window", "." ]
09472cabe537f6bfdb049eeea64d3ea57b391c21
https://github.com/cds-astro/mocpy/blob/09472cabe537f6bfdb049eeea64d3ea57b391c21/mocpy/tmoc/tmoc.py#L391-L474
train
rbarrois/mpdlcd
mpdlcd/mpdhooks.py
MPDHook.handle
def handle(self, client, subhooks=()): """Handle a new update. Fetches new data from the client, then compares it to the previous lookup. Returns: (bool, new_data): whether changes occurred, and the new value. """ new_data = self.fetch(client) # Holds the list of updated fields. updated = {} if not subhooks: # We always want to compare to previous values. subhooks = [self.name] for subhook in subhooks: new_key = self.extract_key(new_data, subhook) if new_key != self.previous_keys.get(subhook): updated[subhook] = new_key if updated: logger.debug("Hook %s: data changed from %r to %r", self.name, self.previous_keys, updated) self.previous_keys.update(updated) return (True, new_data) return (False, None)
python
def handle(self, client, subhooks=()): """Handle a new update. Fetches new data from the client, then compares it to the previous lookup. Returns: (bool, new_data): whether changes occurred, and the new value. """ new_data = self.fetch(client) # Holds the list of updated fields. updated = {} if not subhooks: # We always want to compare to previous values. subhooks = [self.name] for subhook in subhooks: new_key = self.extract_key(new_data, subhook) if new_key != self.previous_keys.get(subhook): updated[subhook] = new_key if updated: logger.debug("Hook %s: data changed from %r to %r", self.name, self.previous_keys, updated) self.previous_keys.update(updated) return (True, new_data) return (False, None)
[ "def", "handle", "(", "self", ",", "client", ",", "subhooks", "=", "(", ")", ")", ":", "new_data", "=", "self", ".", "fetch", "(", "client", ")", "# Holds the list of updated fields.", "updated", "=", "{", "}", "if", "not", "subhooks", ":", "# We always want to compare to previous values.", "subhooks", "=", "[", "self", ".", "name", "]", "for", "subhook", "in", "subhooks", ":", "new_key", "=", "self", ".", "extract_key", "(", "new_data", ",", "subhook", ")", "if", "new_key", "!=", "self", ".", "previous_keys", ".", "get", "(", "subhook", ")", ":", "updated", "[", "subhook", "]", "=", "new_key", "if", "updated", ":", "logger", ".", "debug", "(", "\"Hook %s: data changed from %r to %r\"", ",", "self", ".", "name", ",", "self", ".", "previous_keys", ",", "updated", ")", "self", ".", "previous_keys", ".", "update", "(", "updated", ")", "return", "(", "True", ",", "new_data", ")", "return", "(", "False", ",", "None", ")" ]
Handle a new update. Fetches new data from the client, then compares it to the previous lookup. Returns: (bool, new_data): whether changes occurred, and the new value.
[ "Handle", "a", "new", "update", "." ]
85f16c8cc0883f8abb4c2cc7f69729c3e2f857da
https://github.com/rbarrois/mpdlcd/blob/85f16c8cc0883f8abb4c2cc7f69729c3e2f857da/mpdlcd/mpdhooks.py#L68-L96
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/text/text.py
_text_to_vbo
def _text_to_vbo(text, font, anchor_x, anchor_y, lowres_size): """Convert text characters to VBO""" # Necessary to flush commands before requesting current viewport because # There may be a set_viewport command waiting in the queue. # TODO: would be nicer if each canvas just remembers and manages its own # viewport, rather than relying on the context for this. canvas = context.get_current_canvas() canvas.context.flush_commands() text_vtype = np.dtype([('a_position', np.float32, 2), ('a_texcoord', np.float32, 2)]) vertices = np.zeros(len(text) * 4, dtype=text_vtype) prev = None width = height = ascender = descender = 0 ratio, slop = 1. / font.ratio, font.slop x_off = -slop # Need to make sure we have a unicode string here (Py2.7 mis-interprets # characters like "•" otherwise) if sys.version[0] == '2' and isinstance(text, str): text = text.decode('utf-8') # Need to store the original viewport, because the font[char] will # trigger SDF rendering, which changes our viewport # todo: get rid of call to glGetParameter! orig_viewport = canvas.context.get_viewport() for ii, char in enumerate(text): glyph = font[char] kerning = glyph['kerning'].get(prev, 0.) * ratio x0 = x_off + glyph['offset'][0] * ratio + kerning y0 = glyph['offset'][1] * ratio + slop x1 = x0 + glyph['size'][0] y1 = y0 - glyph['size'][1] u0, v0, u1, v1 = glyph['texcoords'] position = [[x0, y0], [x0, y1], [x1, y1], [x1, y0]] texcoords = [[u0, v0], [u0, v1], [u1, v1], [u1, v0]] vi = ii * 4 vertices['a_position'][vi:vi+4] = position vertices['a_texcoord'][vi:vi+4] = texcoords x_move = glyph['advance'] * ratio + kerning x_off += x_move ascender = max(ascender, y0 - slop) descender = min(descender, y1 + slop) width += x_move height = max(height, glyph['size'][1] - 2*slop) prev = char # Also analyse chars with large ascender and descender, otherwise the # vertical alignment can be very inconsistent for char in 'hy': glyph = font[char] y0 = glyph['offset'][1] * ratio + slop y1 = y0 - glyph['size'][1] ascender = max(ascender, y0 - slop) descender = min(descender, y1 + slop) height = max(height, glyph['size'][1] - 2*slop) if orig_viewport is not None: canvas.context.set_viewport(*orig_viewport) # Tight bounding box (loose would be width, font.height /.asc / .desc) width -= glyph['advance'] * ratio - (glyph['size'][0] - 2*slop) dx = dy = 0 if anchor_y == 'top': dy = -ascender elif anchor_y in ('center', 'middle'): dy = -(height / 2 + descender) elif anchor_y == 'bottom': dy = -descender # Already referenced to baseline # elif anchor_y == 'baseline': # dy = -descender if anchor_x == 'right': dx = -width elif anchor_x == 'center': dx = -width / 2. vertices['a_position'] += (dx, dy) vertices['a_position'] /= lowres_size return vertices
python
def _text_to_vbo(text, font, anchor_x, anchor_y, lowres_size): """Convert text characters to VBO""" # Necessary to flush commands before requesting current viewport because # There may be a set_viewport command waiting in the queue. # TODO: would be nicer if each canvas just remembers and manages its own # viewport, rather than relying on the context for this. canvas = context.get_current_canvas() canvas.context.flush_commands() text_vtype = np.dtype([('a_position', np.float32, 2), ('a_texcoord', np.float32, 2)]) vertices = np.zeros(len(text) * 4, dtype=text_vtype) prev = None width = height = ascender = descender = 0 ratio, slop = 1. / font.ratio, font.slop x_off = -slop # Need to make sure we have a unicode string here (Py2.7 mis-interprets # characters like "•" otherwise) if sys.version[0] == '2' and isinstance(text, str): text = text.decode('utf-8') # Need to store the original viewport, because the font[char] will # trigger SDF rendering, which changes our viewport # todo: get rid of call to glGetParameter! orig_viewport = canvas.context.get_viewport() for ii, char in enumerate(text): glyph = font[char] kerning = glyph['kerning'].get(prev, 0.) * ratio x0 = x_off + glyph['offset'][0] * ratio + kerning y0 = glyph['offset'][1] * ratio + slop x1 = x0 + glyph['size'][0] y1 = y0 - glyph['size'][1] u0, v0, u1, v1 = glyph['texcoords'] position = [[x0, y0], [x0, y1], [x1, y1], [x1, y0]] texcoords = [[u0, v0], [u0, v1], [u1, v1], [u1, v0]] vi = ii * 4 vertices['a_position'][vi:vi+4] = position vertices['a_texcoord'][vi:vi+4] = texcoords x_move = glyph['advance'] * ratio + kerning x_off += x_move ascender = max(ascender, y0 - slop) descender = min(descender, y1 + slop) width += x_move height = max(height, glyph['size'][1] - 2*slop) prev = char # Also analyse chars with large ascender and descender, otherwise the # vertical alignment can be very inconsistent for char in 'hy': glyph = font[char] y0 = glyph['offset'][1] * ratio + slop y1 = y0 - glyph['size'][1] ascender = max(ascender, y0 - slop) descender = min(descender, y1 + slop) height = max(height, glyph['size'][1] - 2*slop) if orig_viewport is not None: canvas.context.set_viewport(*orig_viewport) # Tight bounding box (loose would be width, font.height /.asc / .desc) width -= glyph['advance'] * ratio - (glyph['size'][0] - 2*slop) dx = dy = 0 if anchor_y == 'top': dy = -ascender elif anchor_y in ('center', 'middle'): dy = -(height / 2 + descender) elif anchor_y == 'bottom': dy = -descender # Already referenced to baseline # elif anchor_y == 'baseline': # dy = -descender if anchor_x == 'right': dx = -width elif anchor_x == 'center': dx = -width / 2. vertices['a_position'] += (dx, dy) vertices['a_position'] /= lowres_size return vertices
[ "def", "_text_to_vbo", "(", "text", ",", "font", ",", "anchor_x", ",", "anchor_y", ",", "lowres_size", ")", ":", "# Necessary to flush commands before requesting current viewport because", "# There may be a set_viewport command waiting in the queue.", "# TODO: would be nicer if each canvas just remembers and manages its own", "# viewport, rather than relying on the context for this.", "canvas", "=", "context", ".", "get_current_canvas", "(", ")", "canvas", ".", "context", ".", "flush_commands", "(", ")", "text_vtype", "=", "np", ".", "dtype", "(", "[", "(", "'a_position'", ",", "np", ".", "float32", ",", "2", ")", ",", "(", "'a_texcoord'", ",", "np", ".", "float32", ",", "2", ")", "]", ")", "vertices", "=", "np", ".", "zeros", "(", "len", "(", "text", ")", "*", "4", ",", "dtype", "=", "text_vtype", ")", "prev", "=", "None", "width", "=", "height", "=", "ascender", "=", "descender", "=", "0", "ratio", ",", "slop", "=", "1.", "/", "font", ".", "ratio", ",", "font", ".", "slop", "x_off", "=", "-", "slop", "# Need to make sure we have a unicode string here (Py2.7 mis-interprets", "# characters like \"•\" otherwise)", "if", "sys", ".", "version", "[", "0", "]", "==", "'2'", "and", "isinstance", "(", "text", ",", "str", ")", ":", "text", "=", "text", ".", "decode", "(", "'utf-8'", ")", "# Need to store the original viewport, because the font[char] will", "# trigger SDF rendering, which changes our viewport", "# todo: get rid of call to glGetParameter!", "orig_viewport", "=", "canvas", ".", "context", ".", "get_viewport", "(", ")", "for", "ii", ",", "char", "in", "enumerate", "(", "text", ")", ":", "glyph", "=", "font", "[", "char", "]", "kerning", "=", "glyph", "[", "'kerning'", "]", ".", "get", "(", "prev", ",", "0.", ")", "*", "ratio", "x0", "=", "x_off", "+", "glyph", "[", "'offset'", "]", "[", "0", "]", "*", "ratio", "+", "kerning", "y0", "=", "glyph", "[", "'offset'", "]", "[", "1", "]", "*", "ratio", "+", "slop", "x1", "=", "x0", "+", "glyph", "[", "'size'", "]", "[", "0", "]", "y1", "=", "y0", "-", "glyph", "[", "'size'", "]", "[", "1", "]", "u0", ",", "v0", ",", "u1", ",", "v1", "=", "glyph", "[", "'texcoords'", "]", "position", "=", "[", "[", "x0", ",", "y0", "]", ",", "[", "x0", ",", "y1", "]", ",", "[", "x1", ",", "y1", "]", ",", "[", "x1", ",", "y0", "]", "]", "texcoords", "=", "[", "[", "u0", ",", "v0", "]", ",", "[", "u0", ",", "v1", "]", ",", "[", "u1", ",", "v1", "]", ",", "[", "u1", ",", "v0", "]", "]", "vi", "=", "ii", "*", "4", "vertices", "[", "'a_position'", "]", "[", "vi", ":", "vi", "+", "4", "]", "=", "position", "vertices", "[", "'a_texcoord'", "]", "[", "vi", ":", "vi", "+", "4", "]", "=", "texcoords", "x_move", "=", "glyph", "[", "'advance'", "]", "*", "ratio", "+", "kerning", "x_off", "+=", "x_move", "ascender", "=", "max", "(", "ascender", ",", "y0", "-", "slop", ")", "descender", "=", "min", "(", "descender", ",", "y1", "+", "slop", ")", "width", "+=", "x_move", "height", "=", "max", "(", "height", ",", "glyph", "[", "'size'", "]", "[", "1", "]", "-", "2", "*", "slop", ")", "prev", "=", "char", "# Also analyse chars with large ascender and descender, otherwise the", "# vertical alignment can be very inconsistent", "for", "char", "in", "'hy'", ":", "glyph", "=", "font", "[", "char", "]", "y0", "=", "glyph", "[", "'offset'", "]", "[", "1", "]", "*", "ratio", "+", "slop", "y1", "=", "y0", "-", "glyph", "[", "'size'", "]", "[", "1", "]", "ascender", "=", "max", "(", "ascender", ",", "y0", "-", "slop", ")", "descender", "=", "min", "(", "descender", ",", "y1", "+", "slop", ")", "height", "=", "max", "(", "height", ",", "glyph", "[", "'size'", "]", "[", "1", "]", "-", "2", "*", "slop", ")", "if", "orig_viewport", "is", "not", "None", ":", "canvas", ".", "context", ".", "set_viewport", "(", "*", "orig_viewport", ")", "# Tight bounding box (loose would be width, font.height /.asc / .desc)", "width", "-=", "glyph", "[", "'advance'", "]", "*", "ratio", "-", "(", "glyph", "[", "'size'", "]", "[", "0", "]", "-", "2", "*", "slop", ")", "dx", "=", "dy", "=", "0", "if", "anchor_y", "==", "'top'", ":", "dy", "=", "-", "ascender", "elif", "anchor_y", "in", "(", "'center'", ",", "'middle'", ")", ":", "dy", "=", "-", "(", "height", "/", "2", "+", "descender", ")", "elif", "anchor_y", "==", "'bottom'", ":", "dy", "=", "-", "descender", "# Already referenced to baseline", "# elif anchor_y == 'baseline':", "# dy = -descender", "if", "anchor_x", "==", "'right'", ":", "dx", "=", "-", "width", "elif", "anchor_x", "==", "'center'", ":", "dx", "=", "-", "width", "/", "2.", "vertices", "[", "'a_position'", "]", "+=", "(", "dx", ",", "dy", ")", "vertices", "[", "'a_position'", "]", "/=", "lowres_size", "return", "vertices" ]
Convert text characters to VBO
[ "Convert", "text", "characters", "to", "VBO" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/text/text.py#L132-L207
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/text/text.py
TextureFont._load_char
def _load_char(self, char): """Build and store a glyph corresponding to an individual character Parameters ---------- char : str A single character to be represented. """ assert isinstance(char, string_types) and len(char) == 1 assert char not in self._glyphs # load new glyph data from font _load_glyph(self._font, char, self._glyphs) # put new glyph into the texture glyph = self._glyphs[char] bitmap = glyph['bitmap'] # convert to padded array data = np.zeros((bitmap.shape[0] + 2*self._spread, bitmap.shape[1] + 2*self._spread), np.uint8) data[self._spread:-self._spread, self._spread:-self._spread] = bitmap # Store, while scaling down to proper size height = data.shape[0] // self.ratio width = data.shape[1] // self.ratio region = self._atlas.get_free_region(width + 2, height + 2) if region is None: raise RuntimeError('Cannot store glyph') x, y, w, h = region x, y, w, h = x + 1, y + 1, w - 2, h - 2 self._renderer.render_to_texture(data, self._atlas, (x, y), (w, h)) u0 = x / float(self._atlas.shape[1]) v0 = y / float(self._atlas.shape[0]) u1 = (x+w) / float(self._atlas.shape[1]) v1 = (y+h) / float(self._atlas.shape[0]) texcoords = (u0, v0, u1, v1) glyph.update(dict(size=(w, h), texcoords=texcoords))
python
def _load_char(self, char): """Build and store a glyph corresponding to an individual character Parameters ---------- char : str A single character to be represented. """ assert isinstance(char, string_types) and len(char) == 1 assert char not in self._glyphs # load new glyph data from font _load_glyph(self._font, char, self._glyphs) # put new glyph into the texture glyph = self._glyphs[char] bitmap = glyph['bitmap'] # convert to padded array data = np.zeros((bitmap.shape[0] + 2*self._spread, bitmap.shape[1] + 2*self._spread), np.uint8) data[self._spread:-self._spread, self._spread:-self._spread] = bitmap # Store, while scaling down to proper size height = data.shape[0] // self.ratio width = data.shape[1] // self.ratio region = self._atlas.get_free_region(width + 2, height + 2) if region is None: raise RuntimeError('Cannot store glyph') x, y, w, h = region x, y, w, h = x + 1, y + 1, w - 2, h - 2 self._renderer.render_to_texture(data, self._atlas, (x, y), (w, h)) u0 = x / float(self._atlas.shape[1]) v0 = y / float(self._atlas.shape[0]) u1 = (x+w) / float(self._atlas.shape[1]) v1 = (y+h) / float(self._atlas.shape[0]) texcoords = (u0, v0, u1, v1) glyph.update(dict(size=(w, h), texcoords=texcoords))
[ "def", "_load_char", "(", "self", ",", "char", ")", ":", "assert", "isinstance", "(", "char", ",", "string_types", ")", "and", "len", "(", "char", ")", "==", "1", "assert", "char", "not", "in", "self", ".", "_glyphs", "# load new glyph data from font", "_load_glyph", "(", "self", ".", "_font", ",", "char", ",", "self", ".", "_glyphs", ")", "# put new glyph into the texture", "glyph", "=", "self", ".", "_glyphs", "[", "char", "]", "bitmap", "=", "glyph", "[", "'bitmap'", "]", "# convert to padded array", "data", "=", "np", ".", "zeros", "(", "(", "bitmap", ".", "shape", "[", "0", "]", "+", "2", "*", "self", ".", "_spread", ",", "bitmap", ".", "shape", "[", "1", "]", "+", "2", "*", "self", ".", "_spread", ")", ",", "np", ".", "uint8", ")", "data", "[", "self", ".", "_spread", ":", "-", "self", ".", "_spread", ",", "self", ".", "_spread", ":", "-", "self", ".", "_spread", "]", "=", "bitmap", "# Store, while scaling down to proper size", "height", "=", "data", ".", "shape", "[", "0", "]", "//", "self", ".", "ratio", "width", "=", "data", ".", "shape", "[", "1", "]", "//", "self", ".", "ratio", "region", "=", "self", ".", "_atlas", ".", "get_free_region", "(", "width", "+", "2", ",", "height", "+", "2", ")", "if", "region", "is", "None", ":", "raise", "RuntimeError", "(", "'Cannot store glyph'", ")", "x", ",", "y", ",", "w", ",", "h", "=", "region", "x", ",", "y", ",", "w", ",", "h", "=", "x", "+", "1", ",", "y", "+", "1", ",", "w", "-", "2", ",", "h", "-", "2", "self", ".", "_renderer", ".", "render_to_texture", "(", "data", ",", "self", ".", "_atlas", ",", "(", "x", ",", "y", ")", ",", "(", "w", ",", "h", ")", ")", "u0", "=", "x", "/", "float", "(", "self", ".", "_atlas", ".", "shape", "[", "1", "]", ")", "v0", "=", "y", "/", "float", "(", "self", ".", "_atlas", ".", "shape", "[", "0", "]", ")", "u1", "=", "(", "x", "+", "w", ")", "/", "float", "(", "self", ".", "_atlas", ".", "shape", "[", "1", "]", ")", "v1", "=", "(", "y", "+", "h", ")", "/", "float", "(", "self", ".", "_atlas", ".", "shape", "[", "0", "]", ")", "texcoords", "=", "(", "u0", ",", "v0", ",", "u1", ",", "v1", ")", "glyph", ".", "update", "(", "dict", "(", "size", "=", "(", "w", ",", "h", ")", ",", "texcoords", "=", "texcoords", ")", ")" ]
Build and store a glyph corresponding to an individual character Parameters ---------- char : str A single character to be represented.
[ "Build", "and", "store", "a", "glyph", "corresponding", "to", "an", "individual", "character" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/text/text.py#L72-L108
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/text/text.py
FontManager.get_font
def get_font(self, face, bold=False, italic=False): """Get a font described by face and size""" key = '%s-%s-%s' % (face, bold, italic) if key not in self._fonts: font = dict(face=face, bold=bold, italic=italic) self._fonts[key] = TextureFont(font, self._renderer) return self._fonts[key]
python
def get_font(self, face, bold=False, italic=False): """Get a font described by face and size""" key = '%s-%s-%s' % (face, bold, italic) if key not in self._fonts: font = dict(face=face, bold=bold, italic=italic) self._fonts[key] = TextureFont(font, self._renderer) return self._fonts[key]
[ "def", "get_font", "(", "self", ",", "face", ",", "bold", "=", "False", ",", "italic", "=", "False", ")", ":", "key", "=", "'%s-%s-%s'", "%", "(", "face", ",", "bold", ",", "italic", ")", "if", "key", "not", "in", "self", ".", "_fonts", ":", "font", "=", "dict", "(", "face", "=", "face", ",", "bold", "=", "bold", ",", "italic", "=", "italic", ")", "self", ".", "_fonts", "[", "key", "]", "=", "TextureFont", "(", "font", ",", "self", ".", "_renderer", ")", "return", "self", ".", "_fonts", "[", "key", "]" ]
Get a font described by face and size
[ "Get", "a", "font", "described", "by", "face", "and", "size" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/text/text.py#L119-L125
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/util/fourier.py
stft
def stft(x, n_fft=1024, step=512, fs=2*np.pi, window='hann'): """Compute the STFT Parameters ---------- x : array-like 1D signal to operate on. ``If len(x) < n_fft``, x will be zero-padded to length ``n_fft``. n_fft : int Number of FFT points. Much faster for powers of two. step : int | None Step size between calculations. If None, ``n_fft // 2`` will be used. fs : float The sample rate of the data. window : str | None Window function to use. Can be ``'hann'`` for Hann window, or None for no windowing. Returns ------- stft : ndarray Spectrogram of the data, shape (n_freqs, n_steps). See also -------- fft_freqs """ x = np.asarray(x, float) if x.ndim != 1: raise ValueError('x must be 1D') if window is not None: if window not in ('hann',): raise ValueError('window must be "hann" or None') w = np.hanning(n_fft) else: w = np.ones(n_fft) n_fft = int(n_fft) step = max(n_fft // 2, 1) if step is None else int(step) fs = float(fs) zero_pad = n_fft - len(x) if zero_pad > 0: x = np.concatenate((x, np.zeros(zero_pad, float))) n_freqs = n_fft // 2 + 1 n_estimates = (len(x) - n_fft) // step + 1 result = np.empty((n_freqs, n_estimates), np.complex128) for ii in range(n_estimates): result[:, ii] = np.fft.rfft(w * x[ii * step:ii * step + n_fft]) / n_fft return result
python
def stft(x, n_fft=1024, step=512, fs=2*np.pi, window='hann'): """Compute the STFT Parameters ---------- x : array-like 1D signal to operate on. ``If len(x) < n_fft``, x will be zero-padded to length ``n_fft``. n_fft : int Number of FFT points. Much faster for powers of two. step : int | None Step size between calculations. If None, ``n_fft // 2`` will be used. fs : float The sample rate of the data. window : str | None Window function to use. Can be ``'hann'`` for Hann window, or None for no windowing. Returns ------- stft : ndarray Spectrogram of the data, shape (n_freqs, n_steps). See also -------- fft_freqs """ x = np.asarray(x, float) if x.ndim != 1: raise ValueError('x must be 1D') if window is not None: if window not in ('hann',): raise ValueError('window must be "hann" or None') w = np.hanning(n_fft) else: w = np.ones(n_fft) n_fft = int(n_fft) step = max(n_fft // 2, 1) if step is None else int(step) fs = float(fs) zero_pad = n_fft - len(x) if zero_pad > 0: x = np.concatenate((x, np.zeros(zero_pad, float))) n_freqs = n_fft // 2 + 1 n_estimates = (len(x) - n_fft) // step + 1 result = np.empty((n_freqs, n_estimates), np.complex128) for ii in range(n_estimates): result[:, ii] = np.fft.rfft(w * x[ii * step:ii * step + n_fft]) / n_fft return result
[ "def", "stft", "(", "x", ",", "n_fft", "=", "1024", ",", "step", "=", "512", ",", "fs", "=", "2", "*", "np", ".", "pi", ",", "window", "=", "'hann'", ")", ":", "x", "=", "np", ".", "asarray", "(", "x", ",", "float", ")", "if", "x", ".", "ndim", "!=", "1", ":", "raise", "ValueError", "(", "'x must be 1D'", ")", "if", "window", "is", "not", "None", ":", "if", "window", "not", "in", "(", "'hann'", ",", ")", ":", "raise", "ValueError", "(", "'window must be \"hann\" or None'", ")", "w", "=", "np", ".", "hanning", "(", "n_fft", ")", "else", ":", "w", "=", "np", ".", "ones", "(", "n_fft", ")", "n_fft", "=", "int", "(", "n_fft", ")", "step", "=", "max", "(", "n_fft", "//", "2", ",", "1", ")", "if", "step", "is", "None", "else", "int", "(", "step", ")", "fs", "=", "float", "(", "fs", ")", "zero_pad", "=", "n_fft", "-", "len", "(", "x", ")", "if", "zero_pad", ">", "0", ":", "x", "=", "np", ".", "concatenate", "(", "(", "x", ",", "np", ".", "zeros", "(", "zero_pad", ",", "float", ")", ")", ")", "n_freqs", "=", "n_fft", "//", "2", "+", "1", "n_estimates", "=", "(", "len", "(", "x", ")", "-", "n_fft", ")", "//", "step", "+", "1", "result", "=", "np", ".", "empty", "(", "(", "n_freqs", ",", "n_estimates", ")", ",", "np", ".", "complex128", ")", "for", "ii", "in", "range", "(", "n_estimates", ")", ":", "result", "[", ":", ",", "ii", "]", "=", "np", ".", "fft", ".", "rfft", "(", "w", "*", "x", "[", "ii", "*", "step", ":", "ii", "*", "step", "+", "n_fft", "]", ")", "/", "n_fft", "return", "result" ]
Compute the STFT Parameters ---------- x : array-like 1D signal to operate on. ``If len(x) < n_fft``, x will be zero-padded to length ``n_fft``. n_fft : int Number of FFT points. Much faster for powers of two. step : int | None Step size between calculations. If None, ``n_fft // 2`` will be used. fs : float The sample rate of the data. window : str | None Window function to use. Can be ``'hann'`` for Hann window, or None for no windowing. Returns ------- stft : ndarray Spectrogram of the data, shape (n_freqs, n_steps). See also -------- fft_freqs
[ "Compute", "the", "STFT" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/util/fourier.py#L8-L56
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/util/fourier.py
fft_freqs
def fft_freqs(n_fft, fs): """Return frequencies for DFT Parameters ---------- n_fft : int Number of points in the FFT. fs : float The sampling rate. """ return np.arange(0, (n_fft // 2 + 1)) / float(n_fft) * float(fs)
python
def fft_freqs(n_fft, fs): """Return frequencies for DFT Parameters ---------- n_fft : int Number of points in the FFT. fs : float The sampling rate. """ return np.arange(0, (n_fft // 2 + 1)) / float(n_fft) * float(fs)
[ "def", "fft_freqs", "(", "n_fft", ",", "fs", ")", ":", "return", "np", ".", "arange", "(", "0", ",", "(", "n_fft", "//", "2", "+", "1", ")", ")", "/", "float", "(", "n_fft", ")", "*", "float", "(", "fs", ")" ]
Return frequencies for DFT Parameters ---------- n_fft : int Number of points in the FFT. fs : float The sampling rate.
[ "Return", "frequencies", "for", "DFT" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/util/fourier.py#L59-L69
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/line/arrow.py
ArrowVisual.set_data
def set_data(self, pos=None, color=None, width=None, connect=None, arrows=None): """Set the data used for this visual Parameters ---------- pos : array Array of shape (..., 2) or (..., 3) specifying vertex coordinates. color : Color, tuple, or array The color to use when drawing the line. If an array is given, it must be of shape (..., 4) and provide one rgba color per vertex. Can also be a colormap name, or appropriate `Function`. width: The width of the line in px. Line widths > 1px are only guaranteed to work when using 'agg' method. connect : str or array Determines which vertices are connected by lines. * "strip" causes the line to be drawn with each vertex connected to the next. * "segments" causes each pair of vertices to draw an independent line segment * numpy arrays specify the exact set of segment pairs to connect. arrows : array A Nx4 matrix where each row contains the x and y coordinate of the first and second vertex of the arrow body. Remember that the second vertex is used as center point for the arrow head, and the first vertex is only used for determining the arrow head orientation. """ if arrows is not None: self._arrows = arrows self._arrows_changed = True LineVisual.set_data(self, pos, color, width, connect)
python
def set_data(self, pos=None, color=None, width=None, connect=None, arrows=None): """Set the data used for this visual Parameters ---------- pos : array Array of shape (..., 2) or (..., 3) specifying vertex coordinates. color : Color, tuple, or array The color to use when drawing the line. If an array is given, it must be of shape (..., 4) and provide one rgba color per vertex. Can also be a colormap name, or appropriate `Function`. width: The width of the line in px. Line widths > 1px are only guaranteed to work when using 'agg' method. connect : str or array Determines which vertices are connected by lines. * "strip" causes the line to be drawn with each vertex connected to the next. * "segments" causes each pair of vertices to draw an independent line segment * numpy arrays specify the exact set of segment pairs to connect. arrows : array A Nx4 matrix where each row contains the x and y coordinate of the first and second vertex of the arrow body. Remember that the second vertex is used as center point for the arrow head, and the first vertex is only used for determining the arrow head orientation. """ if arrows is not None: self._arrows = arrows self._arrows_changed = True LineVisual.set_data(self, pos, color, width, connect)
[ "def", "set_data", "(", "self", ",", "pos", "=", "None", ",", "color", "=", "None", ",", "width", "=", "None", ",", "connect", "=", "None", ",", "arrows", "=", "None", ")", ":", "if", "arrows", "is", "not", "None", ":", "self", ".", "_arrows", "=", "arrows", "self", ".", "_arrows_changed", "=", "True", "LineVisual", ".", "set_data", "(", "self", ",", "pos", ",", "color", ",", "width", ",", "connect", ")" ]
Set the data used for this visual Parameters ---------- pos : array Array of shape (..., 2) or (..., 3) specifying vertex coordinates. color : Color, tuple, or array The color to use when drawing the line. If an array is given, it must be of shape (..., 4) and provide one rgba color per vertex. Can also be a colormap name, or appropriate `Function`. width: The width of the line in px. Line widths > 1px are only guaranteed to work when using 'agg' method. connect : str or array Determines which vertices are connected by lines. * "strip" causes the line to be drawn with each vertex connected to the next. * "segments" causes each pair of vertices to draw an independent line segment * numpy arrays specify the exact set of segment pairs to connect. arrows : array A Nx4 matrix where each row contains the x and y coordinate of the first and second vertex of the arrow body. Remember that the second vertex is used as center point for the arrow head, and the first vertex is only used for determining the arrow head orientation.
[ "Set", "the", "data", "used", "for", "this", "visual" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/line/arrow.py#L191-L227
train
anjishnu/ask-alexa-pykit
examples/twitter/twitter.py
strip_html
def strip_html(text): """ Get rid of ugly twitter html """ def reply_to(text): replying_to = [] split_text = text.split() for index, token in enumerate(split_text): if token.startswith('@'): replying_to.append(token[1:]) else: message = split_text[index:] break rply_msg = "" if len(replying_to) > 0: rply_msg = "Replying to " for token in replying_to[:-1]: rply_msg += token+"," if len(replying_to)>1: rply_msg += 'and ' rply_msg += replying_to[-1]+". " return rply_msg + " ".join(message) text = reply_to(text) text = text.replace('@', ' ') return " ".join([token for token in text.split() if ('http:' not in token) and ('https:' not in token)])
python
def strip_html(text): """ Get rid of ugly twitter html """ def reply_to(text): replying_to = [] split_text = text.split() for index, token in enumerate(split_text): if token.startswith('@'): replying_to.append(token[1:]) else: message = split_text[index:] break rply_msg = "" if len(replying_to) > 0: rply_msg = "Replying to " for token in replying_to[:-1]: rply_msg += token+"," if len(replying_to)>1: rply_msg += 'and ' rply_msg += replying_to[-1]+". " return rply_msg + " ".join(message) text = reply_to(text) text = text.replace('@', ' ') return " ".join([token for token in text.split() if ('http:' not in token) and ('https:' not in token)])
[ "def", "strip_html", "(", "text", ")", ":", "def", "reply_to", "(", "text", ")", ":", "replying_to", "=", "[", "]", "split_text", "=", "text", ".", "split", "(", ")", "for", "index", ",", "token", "in", "enumerate", "(", "split_text", ")", ":", "if", "token", ".", "startswith", "(", "'@'", ")", ":", "replying_to", ".", "append", "(", "token", "[", "1", ":", "]", ")", "else", ":", "message", "=", "split_text", "[", "index", ":", "]", "break", "rply_msg", "=", "\"\"", "if", "len", "(", "replying_to", ")", ">", "0", ":", "rply_msg", "=", "\"Replying to \"", "for", "token", "in", "replying_to", "[", ":", "-", "1", "]", ":", "rply_msg", "+=", "token", "+", "\",\"", "if", "len", "(", "replying_to", ")", ">", "1", ":", "rply_msg", "+=", "'and '", "rply_msg", "+=", "replying_to", "[", "-", "1", "]", "+", "\". \"", "return", "rply_msg", "+", "\" \"", ".", "join", "(", "message", ")", "text", "=", "reply_to", "(", "text", ")", "text", "=", "text", ".", "replace", "(", "'@'", ",", "' '", ")", "return", "\" \"", ".", "join", "(", "[", "token", "for", "token", "in", "text", ".", "split", "(", ")", "if", "(", "'http:'", "not", "in", "token", ")", "and", "(", "'https:'", "not", "in", "token", ")", "]", ")" ]
Get rid of ugly twitter html
[ "Get", "rid", "of", "ugly", "twitter", "html" ]
a47c278ca7a60532bbe1a9b789f6c37e609fea8b
https://github.com/anjishnu/ask-alexa-pykit/blob/a47c278ca7a60532bbe1a9b789f6c37e609fea8b/examples/twitter/twitter.py#L169-L190
train
anjishnu/ask-alexa-pykit
examples/twitter/twitter.py
post_tweet
def post_tweet(user_id, message, additional_params={}): """ Helper function to post a tweet """ url = "https://api.twitter.com/1.1/statuses/update.json" params = { "status" : message } params.update(additional_params) r = make_twitter_request(url, user_id, params, request_type='POST') print (r.text) return "Successfully posted a tweet {}".format(message)
python
def post_tweet(user_id, message, additional_params={}): """ Helper function to post a tweet """ url = "https://api.twitter.com/1.1/statuses/update.json" params = { "status" : message } params.update(additional_params) r = make_twitter_request(url, user_id, params, request_type='POST') print (r.text) return "Successfully posted a tweet {}".format(message)
[ "def", "post_tweet", "(", "user_id", ",", "message", ",", "additional_params", "=", "{", "}", ")", ":", "url", "=", "\"https://api.twitter.com/1.1/statuses/update.json\"", "params", "=", "{", "\"status\"", ":", "message", "}", "params", ".", "update", "(", "additional_params", ")", "r", "=", "make_twitter_request", "(", "url", ",", "user_id", ",", "params", ",", "request_type", "=", "'POST'", ")", "print", "(", "r", ".", "text", ")", "return", "\"Successfully posted a tweet {}\"", ".", "format", "(", "message", ")" ]
Helper function to post a tweet
[ "Helper", "function", "to", "post", "a", "tweet" ]
a47c278ca7a60532bbe1a9b789f6c37e609fea8b
https://github.com/anjishnu/ask-alexa-pykit/blob/a47c278ca7a60532bbe1a9b789f6c37e609fea8b/examples/twitter/twitter.py#L274-L283
train
anjishnu/ask-alexa-pykit
examples/twitter/twitter.py
make_twitter_request
def make_twitter_request(url, user_id, params={}, request_type='GET'): """ Generically make a request to twitter API using a particular user's authorization """ if request_type == "GET": return requests.get(url, auth=get_twitter_auth(user_id), params=params) elif request_type == "POST": return requests.post(url, auth=get_twitter_auth(user_id), params=params)
python
def make_twitter_request(url, user_id, params={}, request_type='GET'): """ Generically make a request to twitter API using a particular user's authorization """ if request_type == "GET": return requests.get(url, auth=get_twitter_auth(user_id), params=params) elif request_type == "POST": return requests.post(url, auth=get_twitter_auth(user_id), params=params)
[ "def", "make_twitter_request", "(", "url", ",", "user_id", ",", "params", "=", "{", "}", ",", "request_type", "=", "'GET'", ")", ":", "if", "request_type", "==", "\"GET\"", ":", "return", "requests", ".", "get", "(", "url", ",", "auth", "=", "get_twitter_auth", "(", "user_id", ")", ",", "params", "=", "params", ")", "elif", "request_type", "==", "\"POST\"", ":", "return", "requests", ".", "post", "(", "url", ",", "auth", "=", "get_twitter_auth", "(", "user_id", ")", ",", "params", "=", "params", ")" ]
Generically make a request to twitter API using a particular user's authorization
[ "Generically", "make", "a", "request", "to", "twitter", "API", "using", "a", "particular", "user", "s", "authorization" ]
a47c278ca7a60532bbe1a9b789f6c37e609fea8b
https://github.com/anjishnu/ask-alexa-pykit/blob/a47c278ca7a60532bbe1a9b789f6c37e609fea8b/examples/twitter/twitter.py#L333-L338
train
anjishnu/ask-alexa-pykit
examples/twitter/twitter.py
geo_search
def geo_search(user_id, search_location): """ Search for a location - free form """ url = "https://api.twitter.com/1.1/geo/search.json" params = {"query" : search_location } response = make_twitter_request(url, user_id, params).json() return response
python
def geo_search(user_id, search_location): """ Search for a location - free form """ url = "https://api.twitter.com/1.1/geo/search.json" params = {"query" : search_location } response = make_twitter_request(url, user_id, params).json() return response
[ "def", "geo_search", "(", "user_id", ",", "search_location", ")", ":", "url", "=", "\"https://api.twitter.com/1.1/geo/search.json\"", "params", "=", "{", "\"query\"", ":", "search_location", "}", "response", "=", "make_twitter_request", "(", "url", ",", "user_id", ",", "params", ")", ".", "json", "(", ")", "return", "response" ]
Search for a location - free form
[ "Search", "for", "a", "location", "-", "free", "form" ]
a47c278ca7a60532bbe1a9b789f6c37e609fea8b
https://github.com/anjishnu/ask-alexa-pykit/blob/a47c278ca7a60532bbe1a9b789f6c37e609fea8b/examples/twitter/twitter.py#L350-L357
train
anjishnu/ask-alexa-pykit
examples/twitter/twitter.py
read_out_tweets
def read_out_tweets(processed_tweets, speech_convertor=None): """ Input - list of processed 'Tweets' output - list of spoken responses """ return ["tweet number {num} by {user}. {text}.".format(num=index+1, user=user, text=text) for index, (user, text) in enumerate(processed_tweets)]
python
def read_out_tweets(processed_tweets, speech_convertor=None): """ Input - list of processed 'Tweets' output - list of spoken responses """ return ["tweet number {num} by {user}. {text}.".format(num=index+1, user=user, text=text) for index, (user, text) in enumerate(processed_tweets)]
[ "def", "read_out_tweets", "(", "processed_tweets", ",", "speech_convertor", "=", "None", ")", ":", "return", "[", "\"tweet number {num} by {user}. {text}.\"", ".", "format", "(", "num", "=", "index", "+", "1", ",", "user", "=", "user", ",", "text", "=", "text", ")", "for", "index", ",", "(", "user", ",", "text", ")", "in", "enumerate", "(", "processed_tweets", ")", "]" ]
Input - list of processed 'Tweets' output - list of spoken responses
[ "Input", "-", "list", "of", "processed", "Tweets", "output", "-", "list", "of", "spoken", "responses" ]
a47c278ca7a60532bbe1a9b789f6c37e609fea8b
https://github.com/anjishnu/ask-alexa-pykit/blob/a47c278ca7a60532bbe1a9b789f6c37e609fea8b/examples/twitter/twitter.py#L374-L380
train
anjishnu/ask-alexa-pykit
examples/twitter/twitter.py
search_for_tweets_about
def search_for_tweets_about(user_id, params): """ Search twitter API """ url = "https://api.twitter.com/1.1/search/tweets.json" response = make_twitter_request(url, user_id, params) return process_tweets(response.json()["statuses"])
python
def search_for_tweets_about(user_id, params): """ Search twitter API """ url = "https://api.twitter.com/1.1/search/tweets.json" response = make_twitter_request(url, user_id, params) return process_tweets(response.json()["statuses"])
[ "def", "search_for_tweets_about", "(", "user_id", ",", "params", ")", ":", "url", "=", "\"https://api.twitter.com/1.1/search/tweets.json\"", "response", "=", "make_twitter_request", "(", "url", ",", "user_id", ",", "params", ")", "return", "process_tweets", "(", "response", ".", "json", "(", ")", "[", "\"statuses\"", "]", ")" ]
Search twitter API
[ "Search", "twitter", "API" ]
a47c278ca7a60532bbe1a9b789f6c37e609fea8b
https://github.com/anjishnu/ask-alexa-pykit/blob/a47c278ca7a60532bbe1a9b789f6c37e609fea8b/examples/twitter/twitter.py#L417-L421
train
federico123579/Trading212-API
tradingAPI/saver.py
Saver.add_val
def add_val(self, val): """add value in form of dict""" if not isinstance(val, type({})): raise ValueError(type({})) self.read() self.config.update(val) self.save()
python
def add_val(self, val): """add value in form of dict""" if not isinstance(val, type({})): raise ValueError(type({})) self.read() self.config.update(val) self.save()
[ "def", "add_val", "(", "self", ",", "val", ")", ":", "if", "not", "isinstance", "(", "val", ",", "type", "(", "{", "}", ")", ")", ":", "raise", "ValueError", "(", "type", "(", "{", "}", ")", ")", "self", ".", "read", "(", ")", "self", ".", "config", ".", "update", "(", "val", ")", "self", ".", "save", "(", ")" ]
add value in form of dict
[ "add", "value", "in", "form", "of", "dict" ]
0fab20b71a2348e72bbe76071b81f3692128851f
https://github.com/federico123579/Trading212-API/blob/0fab20b71a2348e72bbe76071b81f3692128851f/tradingAPI/saver.py#L55-L61
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/io/mesh.py
read_mesh
def read_mesh(fname): """Read mesh data from file. Parameters ---------- fname : str File name to read. Format will be inferred from the filename. Currently only '.obj' and '.obj.gz' are supported. Returns ------- vertices : array Vertices. faces : array | None Triangle face definitions. normals : array Normals for the mesh. texcoords : array | None Texture coordinates. """ # Check format fmt = op.splitext(fname)[1].lower() if fmt == '.gz': fmt = op.splitext(op.splitext(fname)[0])[1].lower() if fmt in ('.obj'): return WavefrontReader.read(fname) elif not format: raise ValueError('read_mesh needs could not determine format.') else: raise ValueError('read_mesh does not understand format %s.' % fmt)
python
def read_mesh(fname): """Read mesh data from file. Parameters ---------- fname : str File name to read. Format will be inferred from the filename. Currently only '.obj' and '.obj.gz' are supported. Returns ------- vertices : array Vertices. faces : array | None Triangle face definitions. normals : array Normals for the mesh. texcoords : array | None Texture coordinates. """ # Check format fmt = op.splitext(fname)[1].lower() if fmt == '.gz': fmt = op.splitext(op.splitext(fname)[0])[1].lower() if fmt in ('.obj'): return WavefrontReader.read(fname) elif not format: raise ValueError('read_mesh needs could not determine format.') else: raise ValueError('read_mesh does not understand format %s.' % fmt)
[ "def", "read_mesh", "(", "fname", ")", ":", "# Check format", "fmt", "=", "op", ".", "splitext", "(", "fname", ")", "[", "1", "]", ".", "lower", "(", ")", "if", "fmt", "==", "'.gz'", ":", "fmt", "=", "op", ".", "splitext", "(", "op", ".", "splitext", "(", "fname", ")", "[", "0", "]", ")", "[", "1", "]", ".", "lower", "(", ")", "if", "fmt", "in", "(", "'.obj'", ")", ":", "return", "WavefrontReader", ".", "read", "(", "fname", ")", "elif", "not", "format", ":", "raise", "ValueError", "(", "'read_mesh needs could not determine format.'", ")", "else", ":", "raise", "ValueError", "(", "'read_mesh does not understand format %s.'", "%", "fmt", ")" ]
Read mesh data from file. Parameters ---------- fname : str File name to read. Format will be inferred from the filename. Currently only '.obj' and '.obj.gz' are supported. Returns ------- vertices : array Vertices. faces : array | None Triangle face definitions. normals : array Normals for the mesh. texcoords : array | None Texture coordinates.
[ "Read", "mesh", "data", "from", "file", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/io/mesh.py#L13-L43
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/io/mesh.py
write_mesh
def write_mesh(fname, vertices, faces, normals, texcoords, name='', format='obj', overwrite=False, reshape_faces=True): """ Write mesh data to file. Parameters ---------- fname : str Filename to write. Must end with ".obj" or ".gz". vertices : array Vertices. faces : array | None Triangle face definitions. normals : array Normals for the mesh. texcoords : array | None Texture coordinates. name : str Name of the object. format : str Currently only "obj" is supported. overwrite : bool If the file exists, overwrite it. reshape_faces : bool Reshape the `faces` array to (Nf, 3). Set to `False` if you need to write a mesh with non triangular faces. """ # Check file if op.isfile(fname) and not overwrite: raise IOError('file "%s" exists, use overwrite=True' % fname) # Check format if format not in ('obj'): raise ValueError('Only "obj" format writing currently supported') WavefrontWriter.write(fname, vertices, faces, normals, texcoords, name, reshape_faces)
python
def write_mesh(fname, vertices, faces, normals, texcoords, name='', format='obj', overwrite=False, reshape_faces=True): """ Write mesh data to file. Parameters ---------- fname : str Filename to write. Must end with ".obj" or ".gz". vertices : array Vertices. faces : array | None Triangle face definitions. normals : array Normals for the mesh. texcoords : array | None Texture coordinates. name : str Name of the object. format : str Currently only "obj" is supported. overwrite : bool If the file exists, overwrite it. reshape_faces : bool Reshape the `faces` array to (Nf, 3). Set to `False` if you need to write a mesh with non triangular faces. """ # Check file if op.isfile(fname) and not overwrite: raise IOError('file "%s" exists, use overwrite=True' % fname) # Check format if format not in ('obj'): raise ValueError('Only "obj" format writing currently supported') WavefrontWriter.write(fname, vertices, faces, normals, texcoords, name, reshape_faces)
[ "def", "write_mesh", "(", "fname", ",", "vertices", ",", "faces", ",", "normals", ",", "texcoords", ",", "name", "=", "''", ",", "format", "=", "'obj'", ",", "overwrite", "=", "False", ",", "reshape_faces", "=", "True", ")", ":", "# Check file", "if", "op", ".", "isfile", "(", "fname", ")", "and", "not", "overwrite", ":", "raise", "IOError", "(", "'file \"%s\" exists, use overwrite=True'", "%", "fname", ")", "# Check format", "if", "format", "not", "in", "(", "'obj'", ")", ":", "raise", "ValueError", "(", "'Only \"obj\" format writing currently supported'", ")", "WavefrontWriter", ".", "write", "(", "fname", ",", "vertices", ",", "faces", ",", "normals", ",", "texcoords", ",", "name", ",", "reshape_faces", ")" ]
Write mesh data to file. Parameters ---------- fname : str Filename to write. Must end with ".obj" or ".gz". vertices : array Vertices. faces : array | None Triangle face definitions. normals : array Normals for the mesh. texcoords : array | None Texture coordinates. name : str Name of the object. format : str Currently only "obj" is supported. overwrite : bool If the file exists, overwrite it. reshape_faces : bool Reshape the `faces` array to (Nf, 3). Set to `False` if you need to write a mesh with non triangular faces.
[ "Write", "mesh", "data", "to", "file", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/io/mesh.py#L46-L80
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/app/backends/_pyglet.py
_set_config
def _set_config(config): """Set gl configuration""" pyglet_config = pyglet.gl.Config() pyglet_config.red_size = config['red_size'] pyglet_config.green_size = config['green_size'] pyglet_config.blue_size = config['blue_size'] pyglet_config.alpha_size = config['alpha_size'] pyglet_config.accum_red_size = 0 pyglet_config.accum_green_size = 0 pyglet_config.accum_blue_size = 0 pyglet_config.accum_alpha_size = 0 pyglet_config.depth_size = config['depth_size'] pyglet_config.stencil_size = config['stencil_size'] pyglet_config.double_buffer = config['double_buffer'] pyglet_config.stereo = config['stereo'] pyglet_config.samples = config['samples'] return pyglet_config
python
def _set_config(config): """Set gl configuration""" pyglet_config = pyglet.gl.Config() pyglet_config.red_size = config['red_size'] pyglet_config.green_size = config['green_size'] pyglet_config.blue_size = config['blue_size'] pyglet_config.alpha_size = config['alpha_size'] pyglet_config.accum_red_size = 0 pyglet_config.accum_green_size = 0 pyglet_config.accum_blue_size = 0 pyglet_config.accum_alpha_size = 0 pyglet_config.depth_size = config['depth_size'] pyglet_config.stencil_size = config['stencil_size'] pyglet_config.double_buffer = config['double_buffer'] pyglet_config.stereo = config['stereo'] pyglet_config.samples = config['samples'] return pyglet_config
[ "def", "_set_config", "(", "config", ")", ":", "pyglet_config", "=", "pyglet", ".", "gl", ".", "Config", "(", ")", "pyglet_config", ".", "red_size", "=", "config", "[", "'red_size'", "]", "pyglet_config", ".", "green_size", "=", "config", "[", "'green_size'", "]", "pyglet_config", ".", "blue_size", "=", "config", "[", "'blue_size'", "]", "pyglet_config", ".", "alpha_size", "=", "config", "[", "'alpha_size'", "]", "pyglet_config", ".", "accum_red_size", "=", "0", "pyglet_config", ".", "accum_green_size", "=", "0", "pyglet_config", ".", "accum_blue_size", "=", "0", "pyglet_config", ".", "accum_alpha_size", "=", "0", "pyglet_config", ".", "depth_size", "=", "config", "[", "'depth_size'", "]", "pyglet_config", ".", "stencil_size", "=", "config", "[", "'stencil_size'", "]", "pyglet_config", ".", "double_buffer", "=", "config", "[", "'double_buffer'", "]", "pyglet_config", ".", "stereo", "=", "config", "[", "'stereo'", "]", "pyglet_config", ".", "samples", "=", "config", "[", "'samples'", "]", "return", "pyglet_config" ]
Set gl configuration
[ "Set", "gl", "configuration" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/app/backends/_pyglet.py#L118-L137
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/gloo/program.py
Program.set_shaders
def set_shaders(self, vert, frag): """ Set the vertex and fragment shaders. Parameters ---------- vert : str Source code for vertex shader. frag : str Source code for fragment shaders. """ if not vert or not frag: raise ValueError('Vertex and fragment code must both be non-empty') # pre-process shader code for #include directives vert, frag = preprocess(vert), preprocess(frag) # Store source code, send it to glir, parse the code for variables self._shaders = vert, frag self._glir.command('SHADERS', self._id, vert, frag) # All current variables become pending variables again for key, val in self._user_variables.items(): self._pending_variables[key] = val self._user_variables = {} # Parse code (and process pending variables) self._parse_variables_from_code()
python
def set_shaders(self, vert, frag): """ Set the vertex and fragment shaders. Parameters ---------- vert : str Source code for vertex shader. frag : str Source code for fragment shaders. """ if not vert or not frag: raise ValueError('Vertex and fragment code must both be non-empty') # pre-process shader code for #include directives vert, frag = preprocess(vert), preprocess(frag) # Store source code, send it to glir, parse the code for variables self._shaders = vert, frag self._glir.command('SHADERS', self._id, vert, frag) # All current variables become pending variables again for key, val in self._user_variables.items(): self._pending_variables[key] = val self._user_variables = {} # Parse code (and process pending variables) self._parse_variables_from_code()
[ "def", "set_shaders", "(", "self", ",", "vert", ",", "frag", ")", ":", "if", "not", "vert", "or", "not", "frag", ":", "raise", "ValueError", "(", "'Vertex and fragment code must both be non-empty'", ")", "# pre-process shader code for #include directives", "vert", ",", "frag", "=", "preprocess", "(", "vert", ")", ",", "preprocess", "(", "frag", ")", "# Store source code, send it to glir, parse the code for variables", "self", ".", "_shaders", "=", "vert", ",", "frag", "self", ".", "_glir", ".", "command", "(", "'SHADERS'", ",", "self", ".", "_id", ",", "vert", ",", "frag", ")", "# All current variables become pending variables again", "for", "key", ",", "val", "in", "self", ".", "_user_variables", ".", "items", "(", ")", ":", "self", ".", "_pending_variables", "[", "key", "]", "=", "val", "self", ".", "_user_variables", "=", "{", "}", "# Parse code (and process pending variables)", "self", ".", "_parse_variables_from_code", "(", ")" ]
Set the vertex and fragment shaders. Parameters ---------- vert : str Source code for vertex shader. frag : str Source code for fragment shaders.
[ "Set", "the", "vertex", "and", "fragment", "shaders", ".", "Parameters", "----------", "vert", ":", "str", "Source", "code", "for", "vertex", "shader", ".", "frag", ":", "str", "Source", "code", "for", "fragment", "shaders", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/gloo/program.py#L134-L159
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/gloo/program.py
Program._parse_variables_from_code
def _parse_variables_from_code(self): """ Parse uniforms, attributes and varyings from the source code. """ # Get one string of code with comments removed code = '\n\n'.join(self._shaders) code = re.sub(r'(.*)(//.*)', r'\1', code, re.M) # Regexp to look for variable names var_regexp = ("\s*VARIABLE\s+" # kind of variable "((highp|mediump|lowp)\s+)?" # Precision (optional) "(?P<type>\w+)\s+" # type "(?P<name>\w+)\s*" # name "(\[(?P<size>\d+)\])?" # size (optional) "(\s*\=\s*[0-9.]+)?" # default value (optional) "\s*;" # end ) # Parse uniforms, attributes and varyings self._code_variables = {} for kind in ('uniform', 'attribute', 'varying', 'const'): regex = re.compile(var_regexp.replace('VARIABLE', kind), flags=re.MULTILINE) for m in re.finditer(regex, code): gtype = m.group('type') size = int(m.group('size')) if m.group('size') else -1 this_kind = kind if size >= 1: # uniform arrays get added both as individuals and full for i in range(size): name = '%s[%d]' % (m.group('name'), i) self._code_variables[name] = kind, gtype, name, -1 this_kind = 'uniform_array' name = m.group('name') self._code_variables[name] = this_kind, gtype, name, size # Now that our code variables are up-to date, we can process # the variables that were set but yet unknown. self._process_pending_variables()
python
def _parse_variables_from_code(self): """ Parse uniforms, attributes and varyings from the source code. """ # Get one string of code with comments removed code = '\n\n'.join(self._shaders) code = re.sub(r'(.*)(//.*)', r'\1', code, re.M) # Regexp to look for variable names var_regexp = ("\s*VARIABLE\s+" # kind of variable "((highp|mediump|lowp)\s+)?" # Precision (optional) "(?P<type>\w+)\s+" # type "(?P<name>\w+)\s*" # name "(\[(?P<size>\d+)\])?" # size (optional) "(\s*\=\s*[0-9.]+)?" # default value (optional) "\s*;" # end ) # Parse uniforms, attributes and varyings self._code_variables = {} for kind in ('uniform', 'attribute', 'varying', 'const'): regex = re.compile(var_regexp.replace('VARIABLE', kind), flags=re.MULTILINE) for m in re.finditer(regex, code): gtype = m.group('type') size = int(m.group('size')) if m.group('size') else -1 this_kind = kind if size >= 1: # uniform arrays get added both as individuals and full for i in range(size): name = '%s[%d]' % (m.group('name'), i) self._code_variables[name] = kind, gtype, name, -1 this_kind = 'uniform_array' name = m.group('name') self._code_variables[name] = this_kind, gtype, name, size # Now that our code variables are up-to date, we can process # the variables that were set but yet unknown. self._process_pending_variables()
[ "def", "_parse_variables_from_code", "(", "self", ")", ":", "# Get one string of code with comments removed", "code", "=", "'\\n\\n'", ".", "join", "(", "self", ".", "_shaders", ")", "code", "=", "re", ".", "sub", "(", "r'(.*)(//.*)'", ",", "r'\\1'", ",", "code", ",", "re", ".", "M", ")", "# Regexp to look for variable names", "var_regexp", "=", "(", "\"\\s*VARIABLE\\s+\"", "# kind of variable", "\"((highp|mediump|lowp)\\s+)?\"", "# Precision (optional)", "\"(?P<type>\\w+)\\s+\"", "# type", "\"(?P<name>\\w+)\\s*\"", "# name", "\"(\\[(?P<size>\\d+)\\])?\"", "# size (optional)", "\"(\\s*\\=\\s*[0-9.]+)?\"", "# default value (optional)", "\"\\s*;\"", "# end", ")", "# Parse uniforms, attributes and varyings", "self", ".", "_code_variables", "=", "{", "}", "for", "kind", "in", "(", "'uniform'", ",", "'attribute'", ",", "'varying'", ",", "'const'", ")", ":", "regex", "=", "re", ".", "compile", "(", "var_regexp", ".", "replace", "(", "'VARIABLE'", ",", "kind", ")", ",", "flags", "=", "re", ".", "MULTILINE", ")", "for", "m", "in", "re", ".", "finditer", "(", "regex", ",", "code", ")", ":", "gtype", "=", "m", ".", "group", "(", "'type'", ")", "size", "=", "int", "(", "m", ".", "group", "(", "'size'", ")", ")", "if", "m", ".", "group", "(", "'size'", ")", "else", "-", "1", "this_kind", "=", "kind", "if", "size", ">=", "1", ":", "# uniform arrays get added both as individuals and full", "for", "i", "in", "range", "(", "size", ")", ":", "name", "=", "'%s[%d]'", "%", "(", "m", ".", "group", "(", "'name'", ")", ",", "i", ")", "self", ".", "_code_variables", "[", "name", "]", "=", "kind", ",", "gtype", ",", "name", ",", "-", "1", "this_kind", "=", "'uniform_array'", "name", "=", "m", ".", "group", "(", "'name'", ")", "self", ".", "_code_variables", "[", "name", "]", "=", "this_kind", ",", "gtype", ",", "name", ",", "size", "# Now that our code variables are up-to date, we can process", "# the variables that were set but yet unknown.", "self", ".", "_process_pending_variables", "(", ")" ]
Parse uniforms, attributes and varyings from the source code.
[ "Parse", "uniforms", "attributes", "and", "varyings", "from", "the", "source", "code", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/gloo/program.py#L184-L222
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/gloo/program.py
Program.bind
def bind(self, data): """ Bind a VertexBuffer that has structured data Parameters ---------- data : VertexBuffer The vertex buffer to bind. The field names of the array are mapped to attribute names in GLSL. """ # Check if not isinstance(data, VertexBuffer): raise ValueError('Program.bind() requires a VertexBuffer.') # Apply for name in data.dtype.names: self[name] = data[name]
python
def bind(self, data): """ Bind a VertexBuffer that has structured data Parameters ---------- data : VertexBuffer The vertex buffer to bind. The field names of the array are mapped to attribute names in GLSL. """ # Check if not isinstance(data, VertexBuffer): raise ValueError('Program.bind() requires a VertexBuffer.') # Apply for name in data.dtype.names: self[name] = data[name]
[ "def", "bind", "(", "self", ",", "data", ")", ":", "# Check", "if", "not", "isinstance", "(", "data", ",", "VertexBuffer", ")", ":", "raise", "ValueError", "(", "'Program.bind() requires a VertexBuffer.'", ")", "# Apply", "for", "name", "in", "data", ".", "dtype", ".", "names", ":", "self", "[", "name", "]", "=", "data", "[", "name", "]" ]
Bind a VertexBuffer that has structured data Parameters ---------- data : VertexBuffer The vertex buffer to bind. The field names of the array are mapped to attribute names in GLSL.
[ "Bind", "a", "VertexBuffer", "that", "has", "structured", "data", "Parameters", "----------", "data", ":", "VertexBuffer", "The", "vertex", "buffer", "to", "bind", ".", "The", "field", "names", "of", "the", "array", "are", "mapped", "to", "attribute", "names", "in", "GLSL", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/gloo/program.py#L224-L238
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/gloo/program.py
Program._process_pending_variables
def _process_pending_variables(self): """ Try to apply the variables that were set but not known yet. """ # Clear our list of pending variables self._pending_variables, pending = {}, self._pending_variables # Try to apply it. On failure, it will be added again for name, data in pending.items(): self[name] = data
python
def _process_pending_variables(self): """ Try to apply the variables that were set but not known yet. """ # Clear our list of pending variables self._pending_variables, pending = {}, self._pending_variables # Try to apply it. On failure, it will be added again for name, data in pending.items(): self[name] = data
[ "def", "_process_pending_variables", "(", "self", ")", ":", "# Clear our list of pending variables", "self", ".", "_pending_variables", ",", "pending", "=", "{", "}", ",", "self", ".", "_pending_variables", "# Try to apply it. On failure, it will be added again", "for", "name", ",", "data", "in", "pending", ".", "items", "(", ")", ":", "self", "[", "name", "]", "=", "data" ]
Try to apply the variables that were set but not known yet.
[ "Try", "to", "apply", "the", "variables", "that", "were", "set", "but", "not", "known", "yet", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/gloo/program.py#L240-L247
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/gloo/program.py
Program.draw
def draw(self, mode='triangles', indices=None, check_error=True): """ Draw the attribute arrays in the specified mode. Parameters ---------- mode : str | GL_ENUM 'points', 'lines', 'line_strip', 'line_loop', 'triangles', 'triangle_strip', or 'triangle_fan'. indices : array Array of indices to draw. check_error: Check error after draw. """ # Invalidate buffer (data has already been sent) self._buffer = None # Check if mode is valid mode = check_enum(mode) if mode not in ['points', 'lines', 'line_strip', 'line_loop', 'triangles', 'triangle_strip', 'triangle_fan']: raise ValueError('Invalid draw mode: %r' % mode) # Check leftover variables, warn, discard them # In GLIR we check whether all attributes are indeed set for name in self._pending_variables: logger.warn('Variable %r is given but not known.' % name) self._pending_variables = {} # Check attribute sizes attributes = [vbo for vbo in self._user_variables.values() if isinstance(vbo, DataBuffer)] sizes = [a.size for a in attributes] if len(attributes) < 1: raise RuntimeError('Must have at least one attribute') if not all(s == sizes[0] for s in sizes[1:]): msg = '\n'.join(['%s: %s' % (str(a), a.size) for a in attributes]) raise RuntimeError('All attributes must have the same size, got:\n' '%s' % msg) # Get the glir queue that we need now canvas = get_current_canvas() assert canvas is not None # Associate canvas canvas.context.glir.associate(self.glir) # Indexbuffer if isinstance(indices, IndexBuffer): canvas.context.glir.associate(indices.glir) logger.debug("Program drawing %r with index buffer" % mode) gltypes = {np.dtype(np.uint8): 'UNSIGNED_BYTE', np.dtype(np.uint16): 'UNSIGNED_SHORT', np.dtype(np.uint32): 'UNSIGNED_INT'} selection = indices.id, gltypes[indices.dtype], indices.size canvas.context.glir.command('DRAW', self._id, mode, selection) elif indices is None: selection = 0, attributes[0].size logger.debug("Program drawing %r with %r" % (mode, selection)) canvas.context.glir.command('DRAW', self._id, mode, selection) else: raise TypeError("Invalid index: %r (must be IndexBuffer)" % indices) # Process GLIR commands canvas.context.flush_commands()
python
def draw(self, mode='triangles', indices=None, check_error=True): """ Draw the attribute arrays in the specified mode. Parameters ---------- mode : str | GL_ENUM 'points', 'lines', 'line_strip', 'line_loop', 'triangles', 'triangle_strip', or 'triangle_fan'. indices : array Array of indices to draw. check_error: Check error after draw. """ # Invalidate buffer (data has already been sent) self._buffer = None # Check if mode is valid mode = check_enum(mode) if mode not in ['points', 'lines', 'line_strip', 'line_loop', 'triangles', 'triangle_strip', 'triangle_fan']: raise ValueError('Invalid draw mode: %r' % mode) # Check leftover variables, warn, discard them # In GLIR we check whether all attributes are indeed set for name in self._pending_variables: logger.warn('Variable %r is given but not known.' % name) self._pending_variables = {} # Check attribute sizes attributes = [vbo for vbo in self._user_variables.values() if isinstance(vbo, DataBuffer)] sizes = [a.size for a in attributes] if len(attributes) < 1: raise RuntimeError('Must have at least one attribute') if not all(s == sizes[0] for s in sizes[1:]): msg = '\n'.join(['%s: %s' % (str(a), a.size) for a in attributes]) raise RuntimeError('All attributes must have the same size, got:\n' '%s' % msg) # Get the glir queue that we need now canvas = get_current_canvas() assert canvas is not None # Associate canvas canvas.context.glir.associate(self.glir) # Indexbuffer if isinstance(indices, IndexBuffer): canvas.context.glir.associate(indices.glir) logger.debug("Program drawing %r with index buffer" % mode) gltypes = {np.dtype(np.uint8): 'UNSIGNED_BYTE', np.dtype(np.uint16): 'UNSIGNED_SHORT', np.dtype(np.uint32): 'UNSIGNED_INT'} selection = indices.id, gltypes[indices.dtype], indices.size canvas.context.glir.command('DRAW', self._id, mode, selection) elif indices is None: selection = 0, attributes[0].size logger.debug("Program drawing %r with %r" % (mode, selection)) canvas.context.glir.command('DRAW', self._id, mode, selection) else: raise TypeError("Invalid index: %r (must be IndexBuffer)" % indices) # Process GLIR commands canvas.context.flush_commands()
[ "def", "draw", "(", "self", ",", "mode", "=", "'triangles'", ",", "indices", "=", "None", ",", "check_error", "=", "True", ")", ":", "# Invalidate buffer (data has already been sent)", "self", ".", "_buffer", "=", "None", "# Check if mode is valid", "mode", "=", "check_enum", "(", "mode", ")", "if", "mode", "not", "in", "[", "'points'", ",", "'lines'", ",", "'line_strip'", ",", "'line_loop'", ",", "'triangles'", ",", "'triangle_strip'", ",", "'triangle_fan'", "]", ":", "raise", "ValueError", "(", "'Invalid draw mode: %r'", "%", "mode", ")", "# Check leftover variables, warn, discard them", "# In GLIR we check whether all attributes are indeed set", "for", "name", "in", "self", ".", "_pending_variables", ":", "logger", ".", "warn", "(", "'Variable %r is given but not known.'", "%", "name", ")", "self", ".", "_pending_variables", "=", "{", "}", "# Check attribute sizes", "attributes", "=", "[", "vbo", "for", "vbo", "in", "self", ".", "_user_variables", ".", "values", "(", ")", "if", "isinstance", "(", "vbo", ",", "DataBuffer", ")", "]", "sizes", "=", "[", "a", ".", "size", "for", "a", "in", "attributes", "]", "if", "len", "(", "attributes", ")", "<", "1", ":", "raise", "RuntimeError", "(", "'Must have at least one attribute'", ")", "if", "not", "all", "(", "s", "==", "sizes", "[", "0", "]", "for", "s", "in", "sizes", "[", "1", ":", "]", ")", ":", "msg", "=", "'\\n'", ".", "join", "(", "[", "'%s: %s'", "%", "(", "str", "(", "a", ")", ",", "a", ".", "size", ")", "for", "a", "in", "attributes", "]", ")", "raise", "RuntimeError", "(", "'All attributes must have the same size, got:\\n'", "'%s'", "%", "msg", ")", "# Get the glir queue that we need now", "canvas", "=", "get_current_canvas", "(", ")", "assert", "canvas", "is", "not", "None", "# Associate canvas", "canvas", ".", "context", ".", "glir", ".", "associate", "(", "self", ".", "glir", ")", "# Indexbuffer", "if", "isinstance", "(", "indices", ",", "IndexBuffer", ")", ":", "canvas", ".", "context", ".", "glir", ".", "associate", "(", "indices", ".", "glir", ")", "logger", ".", "debug", "(", "\"Program drawing %r with index buffer\"", "%", "mode", ")", "gltypes", "=", "{", "np", ".", "dtype", "(", "np", ".", "uint8", ")", ":", "'UNSIGNED_BYTE'", ",", "np", ".", "dtype", "(", "np", ".", "uint16", ")", ":", "'UNSIGNED_SHORT'", ",", "np", ".", "dtype", "(", "np", ".", "uint32", ")", ":", "'UNSIGNED_INT'", "}", "selection", "=", "indices", ".", "id", ",", "gltypes", "[", "indices", ".", "dtype", "]", ",", "indices", ".", "size", "canvas", ".", "context", ".", "glir", ".", "command", "(", "'DRAW'", ",", "self", ".", "_id", ",", "mode", ",", "selection", ")", "elif", "indices", "is", "None", ":", "selection", "=", "0", ",", "attributes", "[", "0", "]", ".", "size", "logger", ".", "debug", "(", "\"Program drawing %r with %r\"", "%", "(", "mode", ",", "selection", ")", ")", "canvas", ".", "context", ".", "glir", ".", "command", "(", "'DRAW'", ",", "self", ".", "_id", ",", "mode", ",", "selection", ")", "else", ":", "raise", "TypeError", "(", "\"Invalid index: %r (must be IndexBuffer)\"", "%", "indices", ")", "# Process GLIR commands", "canvas", ".", "context", ".", "flush_commands", "(", ")" ]
Draw the attribute arrays in the specified mode. Parameters ---------- mode : str | GL_ENUM 'points', 'lines', 'line_strip', 'line_loop', 'triangles', 'triangle_strip', or 'triangle_fan'. indices : array Array of indices to draw. check_error: Check error after draw.
[ "Draw", "the", "attribute", "arrays", "in", "the", "specified", "mode", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/gloo/program.py#L404-L470
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/surface_plot.py
SurfacePlotVisual.set_data
def set_data(self, x=None, y=None, z=None, colors=None): """Update the data in this surface plot. Parameters ---------- x : ndarray | None 1D array of values specifying the x positions of vertices in the grid. If None, values will be assumed to be integers. y : ndarray | None 1D array of values specifying the x positions of vertices in the grid. If None, values will be assumed to be integers. z : ndarray 2D array of height values for each grid vertex. colors : ndarray (width, height, 4) array of vertex colors. """ if x is not None: if self._x is None or len(x) != len(self._x): self.__vertices = None self._x = x if y is not None: if self._y is None or len(y) != len(self._y): self.__vertices = None self._y = y if z is not None: if self._x is not None and z.shape[0] != len(self._x): raise TypeError('Z values must have shape (len(x), len(y))') if self._y is not None and z.shape[1] != len(self._y): raise TypeError('Z values must have shape (len(x), len(y))') self._z = z if (self.__vertices is not None and self._z.shape != self.__vertices.shape[:2]): self.__vertices = None if self._z is None: return update_mesh = False new_vertices = False # Generate vertex and face array if self.__vertices is None: new_vertices = True self.__vertices = np.empty((self._z.shape[0], self._z.shape[1], 3), dtype=np.float32) self.generate_faces() self.__meshdata.set_faces(self.__faces) update_mesh = True # Copy x, y, z data into vertex array if new_vertices or x is not None: if x is None: if self._x is None: x = np.arange(self._z.shape[0]) else: x = self._x self.__vertices[:, :, 0] = x.reshape(len(x), 1) update_mesh = True if new_vertices or y is not None: if y is None: if self._y is None: y = np.arange(self._z.shape[1]) else: y = self._y self.__vertices[:, :, 1] = y.reshape(1, len(y)) update_mesh = True if new_vertices or z is not None: self.__vertices[..., 2] = self._z update_mesh = True if colors is not None: self.__meshdata.set_vertex_colors(colors) update_mesh = True # Update MeshData if update_mesh: self.__meshdata.set_vertices( self.__vertices.reshape(self.__vertices.shape[0] * self.__vertices.shape[1], 3)) MeshVisual.set_data(self, meshdata=self.__meshdata)
python
def set_data(self, x=None, y=None, z=None, colors=None): """Update the data in this surface plot. Parameters ---------- x : ndarray | None 1D array of values specifying the x positions of vertices in the grid. If None, values will be assumed to be integers. y : ndarray | None 1D array of values specifying the x positions of vertices in the grid. If None, values will be assumed to be integers. z : ndarray 2D array of height values for each grid vertex. colors : ndarray (width, height, 4) array of vertex colors. """ if x is not None: if self._x is None or len(x) != len(self._x): self.__vertices = None self._x = x if y is not None: if self._y is None or len(y) != len(self._y): self.__vertices = None self._y = y if z is not None: if self._x is not None and z.shape[0] != len(self._x): raise TypeError('Z values must have shape (len(x), len(y))') if self._y is not None and z.shape[1] != len(self._y): raise TypeError('Z values must have shape (len(x), len(y))') self._z = z if (self.__vertices is not None and self._z.shape != self.__vertices.shape[:2]): self.__vertices = None if self._z is None: return update_mesh = False new_vertices = False # Generate vertex and face array if self.__vertices is None: new_vertices = True self.__vertices = np.empty((self._z.shape[0], self._z.shape[1], 3), dtype=np.float32) self.generate_faces() self.__meshdata.set_faces(self.__faces) update_mesh = True # Copy x, y, z data into vertex array if new_vertices or x is not None: if x is None: if self._x is None: x = np.arange(self._z.shape[0]) else: x = self._x self.__vertices[:, :, 0] = x.reshape(len(x), 1) update_mesh = True if new_vertices or y is not None: if y is None: if self._y is None: y = np.arange(self._z.shape[1]) else: y = self._y self.__vertices[:, :, 1] = y.reshape(1, len(y)) update_mesh = True if new_vertices or z is not None: self.__vertices[..., 2] = self._z update_mesh = True if colors is not None: self.__meshdata.set_vertex_colors(colors) update_mesh = True # Update MeshData if update_mesh: self.__meshdata.set_vertices( self.__vertices.reshape(self.__vertices.shape[0] * self.__vertices.shape[1], 3)) MeshVisual.set_data(self, meshdata=self.__meshdata)
[ "def", "set_data", "(", "self", ",", "x", "=", "None", ",", "y", "=", "None", ",", "z", "=", "None", ",", "colors", "=", "None", ")", ":", "if", "x", "is", "not", "None", ":", "if", "self", ".", "_x", "is", "None", "or", "len", "(", "x", ")", "!=", "len", "(", "self", ".", "_x", ")", ":", "self", ".", "__vertices", "=", "None", "self", ".", "_x", "=", "x", "if", "y", "is", "not", "None", ":", "if", "self", ".", "_y", "is", "None", "or", "len", "(", "y", ")", "!=", "len", "(", "self", ".", "_y", ")", ":", "self", ".", "__vertices", "=", "None", "self", ".", "_y", "=", "y", "if", "z", "is", "not", "None", ":", "if", "self", ".", "_x", "is", "not", "None", "and", "z", ".", "shape", "[", "0", "]", "!=", "len", "(", "self", ".", "_x", ")", ":", "raise", "TypeError", "(", "'Z values must have shape (len(x), len(y))'", ")", "if", "self", ".", "_y", "is", "not", "None", "and", "z", ".", "shape", "[", "1", "]", "!=", "len", "(", "self", ".", "_y", ")", ":", "raise", "TypeError", "(", "'Z values must have shape (len(x), len(y))'", ")", "self", ".", "_z", "=", "z", "if", "(", "self", ".", "__vertices", "is", "not", "None", "and", "self", ".", "_z", ".", "shape", "!=", "self", ".", "__vertices", ".", "shape", "[", ":", "2", "]", ")", ":", "self", ".", "__vertices", "=", "None", "if", "self", ".", "_z", "is", "None", ":", "return", "update_mesh", "=", "False", "new_vertices", "=", "False", "# Generate vertex and face array", "if", "self", ".", "__vertices", "is", "None", ":", "new_vertices", "=", "True", "self", ".", "__vertices", "=", "np", ".", "empty", "(", "(", "self", ".", "_z", ".", "shape", "[", "0", "]", ",", "self", ".", "_z", ".", "shape", "[", "1", "]", ",", "3", ")", ",", "dtype", "=", "np", ".", "float32", ")", "self", ".", "generate_faces", "(", ")", "self", ".", "__meshdata", ".", "set_faces", "(", "self", ".", "__faces", ")", "update_mesh", "=", "True", "# Copy x, y, z data into vertex array", "if", "new_vertices", "or", "x", "is", "not", "None", ":", "if", "x", "is", "None", ":", "if", "self", ".", "_x", "is", "None", ":", "x", "=", "np", ".", "arange", "(", "self", ".", "_z", ".", "shape", "[", "0", "]", ")", "else", ":", "x", "=", "self", ".", "_x", "self", ".", "__vertices", "[", ":", ",", ":", ",", "0", "]", "=", "x", ".", "reshape", "(", "len", "(", "x", ")", ",", "1", ")", "update_mesh", "=", "True", "if", "new_vertices", "or", "y", "is", "not", "None", ":", "if", "y", "is", "None", ":", "if", "self", ".", "_y", "is", "None", ":", "y", "=", "np", ".", "arange", "(", "self", ".", "_z", ".", "shape", "[", "1", "]", ")", "else", ":", "y", "=", "self", ".", "_y", "self", ".", "__vertices", "[", ":", ",", ":", ",", "1", "]", "=", "y", ".", "reshape", "(", "1", ",", "len", "(", "y", ")", ")", "update_mesh", "=", "True", "if", "new_vertices", "or", "z", "is", "not", "None", ":", "self", ".", "__vertices", "[", "...", ",", "2", "]", "=", "self", ".", "_z", "update_mesh", "=", "True", "if", "colors", "is", "not", "None", ":", "self", ".", "__meshdata", ".", "set_vertex_colors", "(", "colors", ")", "update_mesh", "=", "True", "# Update MeshData", "if", "update_mesh", ":", "self", ".", "__meshdata", ".", "set_vertices", "(", "self", ".", "__vertices", ".", "reshape", "(", "self", ".", "__vertices", ".", "shape", "[", "0", "]", "*", "self", ".", "__vertices", ".", "shape", "[", "1", "]", ",", "3", ")", ")", "MeshVisual", ".", "set_data", "(", "self", ",", "meshdata", "=", "self", ".", "__meshdata", ")" ]
Update the data in this surface plot. Parameters ---------- x : ndarray | None 1D array of values specifying the x positions of vertices in the grid. If None, values will be assumed to be integers. y : ndarray | None 1D array of values specifying the x positions of vertices in the grid. If None, values will be assumed to be integers. z : ndarray 2D array of height values for each grid vertex. colors : ndarray (width, height, 4) array of vertex colors.
[ "Update", "the", "data", "in", "this", "surface", "plot", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/surface_plot.py#L52-L135
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py
ChainTransform.simplified
def simplified(self): """A simplified representation of the same transformation. """ if self._simplified is None: self._simplified = SimplifiedChainTransform(self) return self._simplified
python
def simplified(self): """A simplified representation of the same transformation. """ if self._simplified is None: self._simplified = SimplifiedChainTransform(self) return self._simplified
[ "def", "simplified", "(", "self", ")", ":", "if", "self", ".", "_simplified", "is", "None", ":", "self", ".", "_simplified", "=", "SimplifiedChainTransform", "(", "self", ")", "return", "self", ".", "_simplified" ]
A simplified representation of the same transformation.
[ "A", "simplified", "representation", "of", "the", "same", "transformation", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py#L99-L104
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py
ChainTransform.map
def map(self, coords): """Map coordinates Parameters ---------- coords : array-like Coordinates to map. Returns ------- coords : ndarray Coordinates. """ for tr in reversed(self.transforms): coords = tr.map(coords) return coords
python
def map(self, coords): """Map coordinates Parameters ---------- coords : array-like Coordinates to map. Returns ------- coords : ndarray Coordinates. """ for tr in reversed(self.transforms): coords = tr.map(coords) return coords
[ "def", "map", "(", "self", ",", "coords", ")", ":", "for", "tr", "in", "reversed", "(", "self", ".", "transforms", ")", ":", "coords", "=", "tr", ".", "map", "(", "coords", ")", "return", "coords" ]
Map coordinates Parameters ---------- coords : array-like Coordinates to map. Returns ------- coords : ndarray Coordinates.
[ "Map", "coordinates" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py#L134-L149
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py
ChainTransform.imap
def imap(self, coords): """Inverse map coordinates Parameters ---------- coords : array-like Coordinates to inverse map. Returns ------- coords : ndarray Coordinates. """ for tr in self.transforms: coords = tr.imap(coords) return coords
python
def imap(self, coords): """Inverse map coordinates Parameters ---------- coords : array-like Coordinates to inverse map. Returns ------- coords : ndarray Coordinates. """ for tr in self.transforms: coords = tr.imap(coords) return coords
[ "def", "imap", "(", "self", ",", "coords", ")", ":", "for", "tr", "in", "self", ".", "transforms", ":", "coords", "=", "tr", ".", "imap", "(", "coords", ")", "return", "coords" ]
Inverse map coordinates Parameters ---------- coords : array-like Coordinates to inverse map. Returns ------- coords : ndarray Coordinates.
[ "Inverse", "map", "coordinates" ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py#L151-L166
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py
ChainTransform.append
def append(self, tr): """ Add a new transform to the end of this chain. Parameters ---------- tr : instance of Transform The transform to use. """ self.transforms.append(tr) tr.changed.connect(self._subtr_changed) self._rebuild_shaders() self.update()
python
def append(self, tr): """ Add a new transform to the end of this chain. Parameters ---------- tr : instance of Transform The transform to use. """ self.transforms.append(tr) tr.changed.connect(self._subtr_changed) self._rebuild_shaders() self.update()
[ "def", "append", "(", "self", ",", "tr", ")", ":", "self", ".", "transforms", ".", "append", "(", "tr", ")", "tr", ".", "changed", ".", "connect", "(", "self", ".", "_subtr_changed", ")", "self", ".", "_rebuild_shaders", "(", ")", "self", ".", "update", "(", ")" ]
Add a new transform to the end of this chain. Parameters ---------- tr : instance of Transform The transform to use.
[ "Add", "a", "new", "transform", "to", "the", "end", "of", "this", "chain", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py#L181-L193
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py
ChainTransform.prepend
def prepend(self, tr): """ Add a new transform to the beginning of this chain. Parameters ---------- tr : instance of Transform The transform to use. """ self.transforms.insert(0, tr) tr.changed.connect(self._subtr_changed) self._rebuild_shaders() self.update()
python
def prepend(self, tr): """ Add a new transform to the beginning of this chain. Parameters ---------- tr : instance of Transform The transform to use. """ self.transforms.insert(0, tr) tr.changed.connect(self._subtr_changed) self._rebuild_shaders() self.update()
[ "def", "prepend", "(", "self", ",", "tr", ")", ":", "self", ".", "transforms", ".", "insert", "(", "0", ",", "tr", ")", "tr", ".", "changed", ".", "connect", "(", "self", ".", "_subtr_changed", ")", "self", ".", "_rebuild_shaders", "(", ")", "self", ".", "update", "(", ")" ]
Add a new transform to the beginning of this chain. Parameters ---------- tr : instance of Transform The transform to use.
[ "Add", "a", "new", "transform", "to", "the", "beginning", "of", "this", "chain", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py#L195-L207
train
glue-viz/glue-vispy-viewers
glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py
SimplifiedChainTransform.source_changed
def source_changed(self, event): """Generate a simplified chain by joining adjacent transforms. """ # bail out early if the chain is empty transforms = self._chain.transforms[:] if len(transforms) == 0: self.transforms = [] return # If the change signal comes from a transform that already appears in # our simplified transform list, then there is no need to re-simplify. if event is not None: for source in event.sources[::-1]: if source in self.transforms: self.update(event) return # First flatten the chain by expanding all nested chains new_chain = [] while len(transforms) > 0: tr = transforms.pop(0) if isinstance(tr, ChainTransform) and not tr.dynamic: transforms = tr.transforms[:] + transforms else: new_chain.append(tr) # Now combine together all compatible adjacent transforms cont = True tr = new_chain while cont: new_tr = [tr[0]] cont = False for t2 in tr[1:]: t1 = new_tr[-1] pr = t1 * t2 if (not t1.dynamic and not t2.dynamic and not isinstance(pr, ChainTransform)): cont = True new_tr.pop() new_tr.append(pr) else: new_tr.append(t2) tr = new_tr self.transforms = tr
python
def source_changed(self, event): """Generate a simplified chain by joining adjacent transforms. """ # bail out early if the chain is empty transforms = self._chain.transforms[:] if len(transforms) == 0: self.transforms = [] return # If the change signal comes from a transform that already appears in # our simplified transform list, then there is no need to re-simplify. if event is not None: for source in event.sources[::-1]: if source in self.transforms: self.update(event) return # First flatten the chain by expanding all nested chains new_chain = [] while len(transforms) > 0: tr = transforms.pop(0) if isinstance(tr, ChainTransform) and not tr.dynamic: transforms = tr.transforms[:] + transforms else: new_chain.append(tr) # Now combine together all compatible adjacent transforms cont = True tr = new_chain while cont: new_tr = [tr[0]] cont = False for t2 in tr[1:]: t1 = new_tr[-1] pr = t1 * t2 if (not t1.dynamic and not t2.dynamic and not isinstance(pr, ChainTransform)): cont = True new_tr.pop() new_tr.append(pr) else: new_tr.append(t2) tr = new_tr self.transforms = tr
[ "def", "source_changed", "(", "self", ",", "event", ")", ":", "# bail out early if the chain is empty", "transforms", "=", "self", ".", "_chain", ".", "transforms", "[", ":", "]", "if", "len", "(", "transforms", ")", "==", "0", ":", "self", ".", "transforms", "=", "[", "]", "return", "# If the change signal comes from a transform that already appears in", "# our simplified transform list, then there is no need to re-simplify.", "if", "event", "is", "not", "None", ":", "for", "source", "in", "event", ".", "sources", "[", ":", ":", "-", "1", "]", ":", "if", "source", "in", "self", ".", "transforms", ":", "self", ".", "update", "(", "event", ")", "return", "# First flatten the chain by expanding all nested chains", "new_chain", "=", "[", "]", "while", "len", "(", "transforms", ")", ">", "0", ":", "tr", "=", "transforms", ".", "pop", "(", "0", ")", "if", "isinstance", "(", "tr", ",", "ChainTransform", ")", "and", "not", "tr", ".", "dynamic", ":", "transforms", "=", "tr", ".", "transforms", "[", ":", "]", "+", "transforms", "else", ":", "new_chain", ".", "append", "(", "tr", ")", "# Now combine together all compatible adjacent transforms", "cont", "=", "True", "tr", "=", "new_chain", "while", "cont", ":", "new_tr", "=", "[", "tr", "[", "0", "]", "]", "cont", "=", "False", "for", "t2", "in", "tr", "[", "1", ":", "]", ":", "t1", "=", "new_tr", "[", "-", "1", "]", "pr", "=", "t1", "*", "t2", "if", "(", "not", "t1", ".", "dynamic", "and", "not", "t2", ".", "dynamic", "and", "not", "isinstance", "(", "pr", ",", "ChainTransform", ")", ")", ":", "cont", "=", "True", "new_tr", ".", "pop", "(", ")", "new_tr", ".", "append", "(", "pr", ")", "else", ":", "new_tr", ".", "append", "(", "t2", ")", "tr", "=", "new_tr", "self", ".", "transforms", "=", "tr" ]
Generate a simplified chain by joining adjacent transforms.
[ "Generate", "a", "simplified", "chain", "by", "joining", "adjacent", "transforms", "." ]
54a4351d98c1f90dfb1a557d1b447c1f57470eea
https://github.com/glue-viz/glue-vispy-viewers/blob/54a4351d98c1f90dfb1a557d1b447c1f57470eea/glue_vispy_viewers/extern/vispy/visuals/transforms/chain.py#L251-L295
train
dlecocq/nsq-py
nsq/util.py
pack_iterable
def pack_iterable(messages): '''Pack an iterable of messages in the TCP protocol format''' # [ 4-byte body size ] # [ 4-byte num messages ] # [ 4-byte message #1 size ][ N-byte binary data ] # ... (repeated <num_messages> times) return pack_string( struct.pack('>l', len(messages)) + ''.join(map(pack_string, messages)))
python
def pack_iterable(messages): '''Pack an iterable of messages in the TCP protocol format''' # [ 4-byte body size ] # [ 4-byte num messages ] # [ 4-byte message #1 size ][ N-byte binary data ] # ... (repeated <num_messages> times) return pack_string( struct.pack('>l', len(messages)) + ''.join(map(pack_string, messages)))
[ "def", "pack_iterable", "(", "messages", ")", ":", "# [ 4-byte body size ]", "# [ 4-byte num messages ]", "# [ 4-byte message #1 size ][ N-byte binary data ]", "# ... (repeated <num_messages> times)", "return", "pack_string", "(", "struct", ".", "pack", "(", "'>l'", ",", "len", "(", "messages", ")", ")", "+", "''", ".", "join", "(", "map", "(", "pack_string", ",", "messages", ")", ")", ")" ]
Pack an iterable of messages in the TCP protocol format
[ "Pack", "an", "iterable", "of", "messages", "in", "the", "TCP", "protocol", "format" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/util.py#L12-L20
train
dlecocq/nsq-py
nsq/util.py
hexify
def hexify(message): '''Print out printable characters, but others in hex''' import string hexified = [] for char in message: if (char in '\n\r \t') or (char not in string.printable): hexified.append('\\x%02x' % ord(char)) else: hexified.append(char) return ''.join(hexified)
python
def hexify(message): '''Print out printable characters, but others in hex''' import string hexified = [] for char in message: if (char in '\n\r \t') or (char not in string.printable): hexified.append('\\x%02x' % ord(char)) else: hexified.append(char) return ''.join(hexified)
[ "def", "hexify", "(", "message", ")", ":", "import", "string", "hexified", "=", "[", "]", "for", "char", "in", "message", ":", "if", "(", "char", "in", "'\\n\\r \\t'", ")", "or", "(", "char", "not", "in", "string", ".", "printable", ")", ":", "hexified", ".", "append", "(", "'\\\\x%02x'", "%", "ord", "(", "char", ")", ")", "else", ":", "hexified", ".", "append", "(", "char", ")", "return", "''", ".", "join", "(", "hexified", ")" ]
Print out printable characters, but others in hex
[ "Print", "out", "printable", "characters", "but", "others", "in", "hex" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/util.py#L31-L40
train
dlecocq/nsq-py
nsq/util.py
distribute
def distribute(total, objects): '''Generator for (count, object) tuples that distributes count evenly among the provided objects''' for index, obj in enumerate(objects): start = (index * total) / len(objects) stop = ((index + 1) * total) / len(objects) yield (stop - start, obj)
python
def distribute(total, objects): '''Generator for (count, object) tuples that distributes count evenly among the provided objects''' for index, obj in enumerate(objects): start = (index * total) / len(objects) stop = ((index + 1) * total) / len(objects) yield (stop - start, obj)
[ "def", "distribute", "(", "total", ",", "objects", ")", ":", "for", "index", ",", "obj", "in", "enumerate", "(", "objects", ")", ":", "start", "=", "(", "index", "*", "total", ")", "/", "len", "(", "objects", ")", "stop", "=", "(", "(", "index", "+", "1", ")", "*", "total", ")", "/", "len", "(", "objects", ")", "yield", "(", "stop", "-", "start", ",", "obj", ")" ]
Generator for (count, object) tuples that distributes count evenly among the provided objects
[ "Generator", "for", "(", "count", "object", ")", "tuples", "that", "distributes", "count", "evenly", "among", "the", "provided", "objects" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/util.py#L43-L49
train
Nukesor/pueue
pueue/daemon/queue.py
Queue.clean
def clean(self): """Clean queue items from a previous session. In case a previous session crashed and there are still some running entries in the queue ('running', 'stopping', 'killing'), we clean those and enqueue them again. """ for _, item in self.queue.items(): if item['status'] in ['paused', 'running', 'stopping', 'killing']: item['status'] = 'queued' item['start'] = '' item['end'] = ''
python
def clean(self): """Clean queue items from a previous session. In case a previous session crashed and there are still some running entries in the queue ('running', 'stopping', 'killing'), we clean those and enqueue them again. """ for _, item in self.queue.items(): if item['status'] in ['paused', 'running', 'stopping', 'killing']: item['status'] = 'queued' item['start'] = '' item['end'] = ''
[ "def", "clean", "(", "self", ")", ":", "for", "_", ",", "item", "in", "self", ".", "queue", ".", "items", "(", ")", ":", "if", "item", "[", "'status'", "]", "in", "[", "'paused'", ",", "'running'", ",", "'stopping'", ",", "'killing'", "]", ":", "item", "[", "'status'", "]", "=", "'queued'", "item", "[", "'start'", "]", "=", "''", "item", "[", "'end'", "]", "=", "''" ]
Clean queue items from a previous session. In case a previous session crashed and there are still some running entries in the queue ('running', 'stopping', 'killing'), we clean those and enqueue them again.
[ "Clean", "queue", "items", "from", "a", "previous", "session", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/queue.py#L56-L67
train
Nukesor/pueue
pueue/daemon/queue.py
Queue.clear
def clear(self): """Remove all completed tasks from the queue.""" for key in list(self.queue.keys()): if self.queue[key]['status'] in ['done', 'failed']: del self.queue[key] self.write()
python
def clear(self): """Remove all completed tasks from the queue.""" for key in list(self.queue.keys()): if self.queue[key]['status'] in ['done', 'failed']: del self.queue[key] self.write()
[ "def", "clear", "(", "self", ")", ":", "for", "key", "in", "list", "(", "self", ".", "queue", ".", "keys", "(", ")", ")", ":", "if", "self", ".", "queue", "[", "key", "]", "[", "'status'", "]", "in", "[", "'done'", ",", "'failed'", "]", ":", "del", "self", ".", "queue", "[", "key", "]", "self", ".", "write", "(", ")" ]
Remove all completed tasks from the queue.
[ "Remove", "all", "completed", "tasks", "from", "the", "queue", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/queue.py#L69-L74
train
Nukesor/pueue
pueue/daemon/queue.py
Queue.next
def next(self): """Get the next processable item of the queue. A processable item is supposed to have the status `queued`. Returns: None : If no key is found. Int: If a valid entry is found. """ smallest = None for key in self.queue.keys(): if self.queue[key]['status'] == 'queued': if smallest is None or key < smallest: smallest = key return smallest
python
def next(self): """Get the next processable item of the queue. A processable item is supposed to have the status `queued`. Returns: None : If no key is found. Int: If a valid entry is found. """ smallest = None for key in self.queue.keys(): if self.queue[key]['status'] == 'queued': if smallest is None or key < smallest: smallest = key return smallest
[ "def", "next", "(", "self", ")", ":", "smallest", "=", "None", "for", "key", "in", "self", ".", "queue", ".", "keys", "(", ")", ":", "if", "self", ".", "queue", "[", "key", "]", "[", "'status'", "]", "==", "'queued'", ":", "if", "smallest", "is", "None", "or", "key", "<", "smallest", ":", "smallest", "=", "key", "return", "smallest" ]
Get the next processable item of the queue. A processable item is supposed to have the status `queued`. Returns: None : If no key is found. Int: If a valid entry is found.
[ "Get", "the", "next", "processable", "item", "of", "the", "queue", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/queue.py#L76-L91
train
Nukesor/pueue
pueue/daemon/queue.py
Queue.read
def read(self): """Read the queue of the last pueue session or set `self.queue = {}`.""" queue_path = os.path.join(self.config_dir, 'queue') if os.path.exists(queue_path): queue_file = open(queue_path, 'rb') try: self.queue = pickle.load(queue_file) except Exception: print('Queue file corrupted, deleting old queue') os.remove(queue_path) self.queue = {} queue_file.close() else: self.queue = {}
python
def read(self): """Read the queue of the last pueue session or set `self.queue = {}`.""" queue_path = os.path.join(self.config_dir, 'queue') if os.path.exists(queue_path): queue_file = open(queue_path, 'rb') try: self.queue = pickle.load(queue_file) except Exception: print('Queue file corrupted, deleting old queue') os.remove(queue_path) self.queue = {} queue_file.close() else: self.queue = {}
[ "def", "read", "(", "self", ")", ":", "queue_path", "=", "os", ".", "path", ".", "join", "(", "self", ".", "config_dir", ",", "'queue'", ")", "if", "os", ".", "path", ".", "exists", "(", "queue_path", ")", ":", "queue_file", "=", "open", "(", "queue_path", ",", "'rb'", ")", "try", ":", "self", ".", "queue", "=", "pickle", ".", "load", "(", "queue_file", ")", "except", "Exception", ":", "print", "(", "'Queue file corrupted, deleting old queue'", ")", "os", ".", "remove", "(", "queue_path", ")", "self", ".", "queue", "=", "{", "}", "queue_file", ".", "close", "(", ")", "else", ":", "self", ".", "queue", "=", "{", "}" ]
Read the queue of the last pueue session or set `self.queue = {}`.
[ "Read", "the", "queue", "of", "the", "last", "pueue", "session", "or", "set", "self", ".", "queue", "=", "{}", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/queue.py#L93-L106
train
Nukesor/pueue
pueue/daemon/queue.py
Queue.write
def write(self): """Write the current queue to a file. We need this to continue an earlier session.""" queue_path = os.path.join(self.config_dir, 'queue') queue_file = open(queue_path, 'wb+') try: pickle.dump(self.queue, queue_file, -1) except Exception: print('Error while writing to queue file. Wrong file permissions?') queue_file.close()
python
def write(self): """Write the current queue to a file. We need this to continue an earlier session.""" queue_path = os.path.join(self.config_dir, 'queue') queue_file = open(queue_path, 'wb+') try: pickle.dump(self.queue, queue_file, -1) except Exception: print('Error while writing to queue file. Wrong file permissions?') queue_file.close()
[ "def", "write", "(", "self", ")", ":", "queue_path", "=", "os", ".", "path", ".", "join", "(", "self", ".", "config_dir", ",", "'queue'", ")", "queue_file", "=", "open", "(", "queue_path", ",", "'wb+'", ")", "try", ":", "pickle", ".", "dump", "(", "self", ".", "queue", ",", "queue_file", ",", "-", "1", ")", "except", "Exception", ":", "print", "(", "'Error while writing to queue file. Wrong file permissions?'", ")", "queue_file", ".", "close", "(", ")" ]
Write the current queue to a file. We need this to continue an earlier session.
[ "Write", "the", "current", "queue", "to", "a", "file", ".", "We", "need", "this", "to", "continue", "an", "earlier", "session", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/queue.py#L108-L116
train
Nukesor/pueue
pueue/daemon/queue.py
Queue.add_new
def add_new(self, command): """Add a new entry to the queue.""" self.queue[self.next_key] = command self.queue[self.next_key]['status'] = 'queued' self.queue[self.next_key]['returncode'] = '' self.queue[self.next_key]['stdout'] = '' self.queue[self.next_key]['stderr'] = '' self.queue[self.next_key]['start'] = '' self.queue[self.next_key]['end'] = '' self.next_key += 1 self.write()
python
def add_new(self, command): """Add a new entry to the queue.""" self.queue[self.next_key] = command self.queue[self.next_key]['status'] = 'queued' self.queue[self.next_key]['returncode'] = '' self.queue[self.next_key]['stdout'] = '' self.queue[self.next_key]['stderr'] = '' self.queue[self.next_key]['start'] = '' self.queue[self.next_key]['end'] = '' self.next_key += 1 self.write()
[ "def", "add_new", "(", "self", ",", "command", ")", ":", "self", ".", "queue", "[", "self", ".", "next_key", "]", "=", "command", "self", ".", "queue", "[", "self", ".", "next_key", "]", "[", "'status'", "]", "=", "'queued'", "self", ".", "queue", "[", "self", ".", "next_key", "]", "[", "'returncode'", "]", "=", "''", "self", ".", "queue", "[", "self", ".", "next_key", "]", "[", "'stdout'", "]", "=", "''", "self", ".", "queue", "[", "self", ".", "next_key", "]", "[", "'stderr'", "]", "=", "''", "self", ".", "queue", "[", "self", ".", "next_key", "]", "[", "'start'", "]", "=", "''", "self", ".", "queue", "[", "self", ".", "next_key", "]", "[", "'end'", "]", "=", "''", "self", ".", "next_key", "+=", "1", "self", ".", "write", "(", ")" ]
Add a new entry to the queue.
[ "Add", "a", "new", "entry", "to", "the", "queue", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/queue.py#L118-L129
train
Nukesor/pueue
pueue/daemon/queue.py
Queue.remove
def remove(self, key): """Remove a key from the queue, return `False` if no such key exists.""" if key in self.queue: del self.queue[key] self.write() return True return False
python
def remove(self, key): """Remove a key from the queue, return `False` if no such key exists.""" if key in self.queue: del self.queue[key] self.write() return True return False
[ "def", "remove", "(", "self", ",", "key", ")", ":", "if", "key", "in", "self", ".", "queue", ":", "del", "self", ".", "queue", "[", "key", "]", "self", ".", "write", "(", ")", "return", "True", "return", "False" ]
Remove a key from the queue, return `False` if no such key exists.
[ "Remove", "a", "key", "from", "the", "queue", "return", "False", "if", "no", "such", "key", "exists", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/queue.py#L131-L137
train
Nukesor/pueue
pueue/daemon/queue.py
Queue.restart
def restart(self, key): """Restart a previously finished entry.""" if key in self.queue: if self.queue[key]['status'] in ['failed', 'done']: new_entry = {'command': self.queue[key]['command'], 'path': self.queue[key]['path']} self.add_new(new_entry) self.write() return True return False
python
def restart(self, key): """Restart a previously finished entry.""" if key in self.queue: if self.queue[key]['status'] in ['failed', 'done']: new_entry = {'command': self.queue[key]['command'], 'path': self.queue[key]['path']} self.add_new(new_entry) self.write() return True return False
[ "def", "restart", "(", "self", ",", "key", ")", ":", "if", "key", "in", "self", ".", "queue", ":", "if", "self", ".", "queue", "[", "key", "]", "[", "'status'", "]", "in", "[", "'failed'", ",", "'done'", "]", ":", "new_entry", "=", "{", "'command'", ":", "self", ".", "queue", "[", "key", "]", "[", "'command'", "]", ",", "'path'", ":", "self", ".", "queue", "[", "key", "]", "[", "'path'", "]", "}", "self", ".", "add_new", "(", "new_entry", ")", "self", ".", "write", "(", ")", "return", "True", "return", "False" ]
Restart a previously finished entry.
[ "Restart", "a", "previously", "finished", "entry", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/queue.py#L139-L148
train
Nukesor/pueue
pueue/daemon/queue.py
Queue.switch
def switch(self, first, second): """Switch two entries in the queue. Return False if an entry doesn't exist.""" allowed_states = ['queued', 'stashed'] if first in self.queue and second in self.queue \ and self.queue[first]['status'] in allowed_states\ and self.queue[second]['status'] in allowed_states: tmp = self.queue[second].copy() self.queue[second] = self.queue[first].copy() self.queue[first] = tmp self.write() return True return False
python
def switch(self, first, second): """Switch two entries in the queue. Return False if an entry doesn't exist.""" allowed_states = ['queued', 'stashed'] if first in self.queue and second in self.queue \ and self.queue[first]['status'] in allowed_states\ and self.queue[second]['status'] in allowed_states: tmp = self.queue[second].copy() self.queue[second] = self.queue[first].copy() self.queue[first] = tmp self.write() return True return False
[ "def", "switch", "(", "self", ",", "first", ",", "second", ")", ":", "allowed_states", "=", "[", "'queued'", ",", "'stashed'", "]", "if", "first", "in", "self", ".", "queue", "and", "second", "in", "self", ".", "queue", "and", "self", ".", "queue", "[", "first", "]", "[", "'status'", "]", "in", "allowed_states", "and", "self", ".", "queue", "[", "second", "]", "[", "'status'", "]", "in", "allowed_states", ":", "tmp", "=", "self", ".", "queue", "[", "second", "]", ".", "copy", "(", ")", "self", ".", "queue", "[", "second", "]", "=", "self", ".", "queue", "[", "first", "]", ".", "copy", "(", ")", "self", ".", "queue", "[", "first", "]", "=", "tmp", "self", ".", "write", "(", ")", "return", "True", "return", "False" ]
Switch two entries in the queue. Return False if an entry doesn't exist.
[ "Switch", "two", "entries", "in", "the", "queue", ".", "Return", "False", "if", "an", "entry", "doesn", "t", "exist", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/queue.py#L150-L162
train
Nukesor/pueue
pueue/client/socket.py
receive_data
def receive_data(socket): """Receive an answer from the daemon and return the response. Args: socket (socket.socket): A socket that is connected to the daemon. Returns: dir or string: The unpickled answer. """ answer = b"" while True: packet = socket.recv(4096) if not packet: break answer += packet response = pickle.loads(answer) socket.close() return response
python
def receive_data(socket): """Receive an answer from the daemon and return the response. Args: socket (socket.socket): A socket that is connected to the daemon. Returns: dir or string: The unpickled answer. """ answer = b"" while True: packet = socket.recv(4096) if not packet: break answer += packet response = pickle.loads(answer) socket.close() return response
[ "def", "receive_data", "(", "socket", ")", ":", "answer", "=", "b\"\"", "while", "True", ":", "packet", "=", "socket", ".", "recv", "(", "4096", ")", "if", "not", "packet", ":", "break", "answer", "+=", "packet", "response", "=", "pickle", ".", "loads", "(", "answer", ")", "socket", ".", "close", "(", ")", "return", "response" ]
Receive an answer from the daemon and return the response. Args: socket (socket.socket): A socket that is connected to the daemon. Returns: dir or string: The unpickled answer.
[ "Receive", "an", "answer", "from", "the", "daemon", "and", "return", "the", "response", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/client/socket.py#L7-L23
train
Nukesor/pueue
pueue/client/socket.py
connect_socket
def connect_socket(root_dir): """Connect to a daemon's socket. Args: root_dir (str): The directory that used as root by the daemon. Returns: socket.socket: A socket that is connected to the daemon. """ # Get config directory where the daemon socket is located config_dir = os.path.join(root_dir, '.config/pueue') # Create Socket and exit with 1, if socket can't be created try: client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) socket_path = os.path.join(config_dir, 'pueue.sock') if os.path.exists(socket_path): client.connect(socket_path) else: print("Socket doesn't exist") raise Exception except: print("Error connecting to socket. Make sure the daemon is running") sys.exit(1) return client
python
def connect_socket(root_dir): """Connect to a daemon's socket. Args: root_dir (str): The directory that used as root by the daemon. Returns: socket.socket: A socket that is connected to the daemon. """ # Get config directory where the daemon socket is located config_dir = os.path.join(root_dir, '.config/pueue') # Create Socket and exit with 1, if socket can't be created try: client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) socket_path = os.path.join(config_dir, 'pueue.sock') if os.path.exists(socket_path): client.connect(socket_path) else: print("Socket doesn't exist") raise Exception except: print("Error connecting to socket. Make sure the daemon is running") sys.exit(1) return client
[ "def", "connect_socket", "(", "root_dir", ")", ":", "# Get config directory where the daemon socket is located", "config_dir", "=", "os", ".", "path", ".", "join", "(", "root_dir", ",", "'.config/pueue'", ")", "# Create Socket and exit with 1, if socket can't be created", "try", ":", "client", "=", "socket", ".", "socket", "(", "socket", ".", "AF_UNIX", ",", "socket", ".", "SOCK_STREAM", ")", "socket_path", "=", "os", ".", "path", ".", "join", "(", "config_dir", ",", "'pueue.sock'", ")", "if", "os", ".", "path", ".", "exists", "(", "socket_path", ")", ":", "client", ".", "connect", "(", "socket_path", ")", "else", ":", "print", "(", "\"Socket doesn't exist\"", ")", "raise", "Exception", "except", ":", "print", "(", "\"Error connecting to socket. Make sure the daemon is running\"", ")", "sys", ".", "exit", "(", "1", ")", "return", "client" ]
Connect to a daemon's socket. Args: root_dir (str): The directory that used as root by the daemon. Returns: socket.socket: A socket that is connected to the daemon.
[ "Connect", "to", "a", "daemon", "s", "socket", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/client/socket.py#L34-L58
train
dlecocq/nsq-py
nsq/response.py
Response.from_raw
def from_raw(conn, raw): '''Return a new response from a raw buffer''' frame_type = struct.unpack('>l', raw[0:4])[0] message = raw[4:] if frame_type == FRAME_TYPE_MESSAGE: return Message(conn, frame_type, message) elif frame_type == FRAME_TYPE_RESPONSE: return Response(conn, frame_type, message) elif frame_type == FRAME_TYPE_ERROR: return Error(conn, frame_type, message) else: raise TypeError('Unknown frame type: %s' % frame_type)
python
def from_raw(conn, raw): '''Return a new response from a raw buffer''' frame_type = struct.unpack('>l', raw[0:4])[0] message = raw[4:] if frame_type == FRAME_TYPE_MESSAGE: return Message(conn, frame_type, message) elif frame_type == FRAME_TYPE_RESPONSE: return Response(conn, frame_type, message) elif frame_type == FRAME_TYPE_ERROR: return Error(conn, frame_type, message) else: raise TypeError('Unknown frame type: %s' % frame_type)
[ "def", "from_raw", "(", "conn", ",", "raw", ")", ":", "frame_type", "=", "struct", ".", "unpack", "(", "'>l'", ",", "raw", "[", "0", ":", "4", "]", ")", "[", "0", "]", "message", "=", "raw", "[", "4", ":", "]", "if", "frame_type", "==", "FRAME_TYPE_MESSAGE", ":", "return", "Message", "(", "conn", ",", "frame_type", ",", "message", ")", "elif", "frame_type", "==", "FRAME_TYPE_RESPONSE", ":", "return", "Response", "(", "conn", ",", "frame_type", ",", "message", ")", "elif", "frame_type", "==", "FRAME_TYPE_ERROR", ":", "return", "Error", "(", "conn", ",", "frame_type", ",", "message", ")", "else", ":", "raise", "TypeError", "(", "'Unknown frame type: %s'", "%", "frame_type", ")" ]
Return a new response from a raw buffer
[ "Return", "a", "new", "response", "from", "a", "raw", "buffer" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/response.py#L19-L30
train
dlecocq/nsq-py
nsq/response.py
Response.pack
def pack(cls, data): '''Pack the provided data into a Response''' return struct.pack('>ll', len(data) + 4, cls.FRAME_TYPE) + data
python
def pack(cls, data): '''Pack the provided data into a Response''' return struct.pack('>ll', len(data) + 4, cls.FRAME_TYPE) + data
[ "def", "pack", "(", "cls", ",", "data", ")", ":", "return", "struct", ".", "pack", "(", "'>ll'", ",", "len", "(", "data", ")", "+", "4", ",", "cls", ".", "FRAME_TYPE", ")", "+", "data" ]
Pack the provided data into a Response
[ "Pack", "the", "provided", "data", "into", "a", "Response" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/response.py#L33-L35
train
dlecocq/nsq-py
nsq/response.py
Message.fin
def fin(self): '''Indicate that this message is finished processing''' self.connection.fin(self.id) self.processed = True
python
def fin(self): '''Indicate that this message is finished processing''' self.connection.fin(self.id) self.processed = True
[ "def", "fin", "(", "self", ")", ":", "self", ".", "connection", ".", "fin", "(", "self", ".", "id", ")", "self", ".", "processed", "=", "True" ]
Indicate that this message is finished processing
[ "Indicate", "that", "this", "message", "is", "finished", "processing" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/response.py#L86-L89
train
dlecocq/nsq-py
nsq/response.py
Message.req
def req(self, timeout): '''Re-queue a message''' self.connection.req(self.id, timeout) self.processed = True
python
def req(self, timeout): '''Re-queue a message''' self.connection.req(self.id, timeout) self.processed = True
[ "def", "req", "(", "self", ",", "timeout", ")", ":", "self", ".", "connection", ".", "req", "(", "self", ".", "id", ",", "timeout", ")", "self", ".", "processed", "=", "True" ]
Re-queue a message
[ "Re", "-", "queue", "a", "message" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/response.py#L91-L94
train
dlecocq/nsq-py
nsq/response.py
Message.handle
def handle(self): '''Make sure this message gets either 'fin' or 'req'd''' try: yield self except: # Requeue the message and raise the original exception typ, value, trace = sys.exc_info() if not self.processed: try: self.req(self.delay()) except socket.error: self.connection.close() raise typ, value, trace else: if not self.processed: try: self.fin() except socket.error: self.connection.close()
python
def handle(self): '''Make sure this message gets either 'fin' or 'req'd''' try: yield self except: # Requeue the message and raise the original exception typ, value, trace = sys.exc_info() if not self.processed: try: self.req(self.delay()) except socket.error: self.connection.close() raise typ, value, trace else: if not self.processed: try: self.fin() except socket.error: self.connection.close()
[ "def", "handle", "(", "self", ")", ":", "try", ":", "yield", "self", "except", ":", "# Requeue the message and raise the original exception", "typ", ",", "value", ",", "trace", "=", "sys", ".", "exc_info", "(", ")", "if", "not", "self", ".", "processed", ":", "try", ":", "self", ".", "req", "(", "self", ".", "delay", "(", ")", ")", "except", "socket", ".", "error", ":", "self", ".", "connection", ".", "close", "(", ")", "raise", "typ", ",", "value", ",", "trace", "else", ":", "if", "not", "self", ".", "processed", ":", "try", ":", "self", ".", "fin", "(", ")", "except", "socket", ".", "error", ":", "self", ".", "connection", ".", "close", "(", ")" ]
Make sure this message gets either 'fin' or 'req'd
[ "Make", "sure", "this", "message", "gets", "either", "fin", "or", "req", "d" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/response.py#L105-L123
train
dlecocq/nsq-py
nsq/response.py
Error.find
def find(cls, name): '''Find the exception class by name''' if not cls.mapping: # pragma: no branch for _, obj in inspect.getmembers(exceptions): if inspect.isclass(obj): if issubclass(obj, exceptions.NSQException): # pragma: no branch if hasattr(obj, 'name'): cls.mapping[obj.name] = obj klass = cls.mapping.get(name) if klass == None: raise TypeError('No matching exception for %s' % name) return klass
python
def find(cls, name): '''Find the exception class by name''' if not cls.mapping: # pragma: no branch for _, obj in inspect.getmembers(exceptions): if inspect.isclass(obj): if issubclass(obj, exceptions.NSQException): # pragma: no branch if hasattr(obj, 'name'): cls.mapping[obj.name] = obj klass = cls.mapping.get(name) if klass == None: raise TypeError('No matching exception for %s' % name) return klass
[ "def", "find", "(", "cls", ",", "name", ")", ":", "if", "not", "cls", ".", "mapping", ":", "# pragma: no branch", "for", "_", ",", "obj", "in", "inspect", ".", "getmembers", "(", "exceptions", ")", ":", "if", "inspect", ".", "isclass", "(", "obj", ")", ":", "if", "issubclass", "(", "obj", ",", "exceptions", ".", "NSQException", ")", ":", "# pragma: no branch", "if", "hasattr", "(", "obj", ",", "'name'", ")", ":", "cls", ".", "mapping", "[", "obj", ".", "name", "]", "=", "obj", "klass", "=", "cls", ".", "mapping", ".", "get", "(", "name", ")", "if", "klass", "==", "None", ":", "raise", "TypeError", "(", "'No matching exception for %s'", "%", "name", ")", "return", "klass" ]
Find the exception class by name
[ "Find", "the", "exception", "class", "by", "name" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/response.py#L134-L145
train
dlecocq/nsq-py
nsq/response.py
Error.exception
def exception(self): '''Return an instance of the corresponding exception''' code, _, message = self.data.partition(' ') return self.find(code)(message)
python
def exception(self): '''Return an instance of the corresponding exception''' code, _, message = self.data.partition(' ') return self.find(code)(message)
[ "def", "exception", "(", "self", ")", ":", "code", ",", "_", ",", "message", "=", "self", ".", "data", ".", "partition", "(", "' '", ")", "return", "self", ".", "find", "(", "code", ")", "(", "message", ")" ]
Return an instance of the corresponding exception
[ "Return", "an", "instance", "of", "the", "corresponding", "exception" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/response.py#L147-L150
train
corpusops/pdbclone
lib/pdb_clone/attach.py
parse_gdb_version
def parse_gdb_version(line): r"""Parse the gdb version from the gdb header. From GNU coding standards: the version starts after the last space of the first line. >>> DOCTEST_GDB_VERSIONS = [ ... r'~"GNU gdb (GDB) 7.5.1\n"', ... r'~"GNU gdb (Sourcery CodeBench Lite 2011.09-69) 7.2.50.20100908-cvs\n"', ... r'~"GNU gdb (GDB) SUSE (7.5.1-2.5.1)\n"', ... r'~"GNU gdb (GDB) Fedora (7.6-32.fc19)\n"', ... r'~"GNU gdb (GDB) 7.6.1.dummy\n"', ... ] >>> for header in DOCTEST_GDB_VERSIONS: ... print(parse_gdb_version(header)) 7.5.1 7.2.50.20100908 7.5.1 7.6 7.6.1 """ if line.startswith('~"') and line.endswith(r'\n"'): version = line[2:-3].rsplit(' ', 1) if len(version) == 2: # Strip after first non digit or '.' character. Allow for linux # Suse non conformant implementation that encloses the version in # brackets. version = ''.join(takewhile(lambda x: x.isdigit() or x == '.', version[1].lstrip('('))) return version.strip('.') return ''
python
def parse_gdb_version(line): r"""Parse the gdb version from the gdb header. From GNU coding standards: the version starts after the last space of the first line. >>> DOCTEST_GDB_VERSIONS = [ ... r'~"GNU gdb (GDB) 7.5.1\n"', ... r'~"GNU gdb (Sourcery CodeBench Lite 2011.09-69) 7.2.50.20100908-cvs\n"', ... r'~"GNU gdb (GDB) SUSE (7.5.1-2.5.1)\n"', ... r'~"GNU gdb (GDB) Fedora (7.6-32.fc19)\n"', ... r'~"GNU gdb (GDB) 7.6.1.dummy\n"', ... ] >>> for header in DOCTEST_GDB_VERSIONS: ... print(parse_gdb_version(header)) 7.5.1 7.2.50.20100908 7.5.1 7.6 7.6.1 """ if line.startswith('~"') and line.endswith(r'\n"'): version = line[2:-3].rsplit(' ', 1) if len(version) == 2: # Strip after first non digit or '.' character. Allow for linux # Suse non conformant implementation that encloses the version in # brackets. version = ''.join(takewhile(lambda x: x.isdigit() or x == '.', version[1].lstrip('('))) return version.strip('.') return ''
[ "def", "parse_gdb_version", "(", "line", ")", ":", "if", "line", ".", "startswith", "(", "'~\"'", ")", "and", "line", ".", "endswith", "(", "r'\\n\"'", ")", ":", "version", "=", "line", "[", "2", ":", "-", "3", "]", ".", "rsplit", "(", "' '", ",", "1", ")", "if", "len", "(", "version", ")", "==", "2", ":", "# Strip after first non digit or '.' character. Allow for linux", "# Suse non conformant implementation that encloses the version in", "# brackets.", "version", "=", "''", ".", "join", "(", "takewhile", "(", "lambda", "x", ":", "x", ".", "isdigit", "(", ")", "or", "x", "==", "'.'", ",", "version", "[", "1", "]", ".", "lstrip", "(", "'('", ")", ")", ")", "return", "version", ".", "strip", "(", "'.'", ")", "return", "''" ]
r"""Parse the gdb version from the gdb header. From GNU coding standards: the version starts after the last space of the first line. >>> DOCTEST_GDB_VERSIONS = [ ... r'~"GNU gdb (GDB) 7.5.1\n"', ... r'~"GNU gdb (Sourcery CodeBench Lite 2011.09-69) 7.2.50.20100908-cvs\n"', ... r'~"GNU gdb (GDB) SUSE (7.5.1-2.5.1)\n"', ... r'~"GNU gdb (GDB) Fedora (7.6-32.fc19)\n"', ... r'~"GNU gdb (GDB) 7.6.1.dummy\n"', ... ] >>> for header in DOCTEST_GDB_VERSIONS: ... print(parse_gdb_version(header)) 7.5.1 7.2.50.20100908 7.5.1 7.6 7.6.1
[ "r", "Parse", "the", "gdb", "version", "from", "the", "gdb", "header", "." ]
f781537c243a4874b246d43dbdef8c4279f0094d
https://github.com/corpusops/pdbclone/blob/f781537c243a4874b246d43dbdef8c4279f0094d/lib/pdb_clone/attach.py#L466-L497
train
corpusops/pdbclone
lib/pdb_clone/attach.py
spawn_gdb
def spawn_gdb(pid, address=DFLT_ADDRESS, gdb='gdb', verbose=False, ctx=None, proc_iut=None): """Spawn gdb and attach to a process.""" parent, child = socket.socketpair() proc = Popen([gdb, '--interpreter=mi', '-nx'], bufsize=0, stdin=child, stdout=child, stderr=STDOUT) child.close() connections = {} gdb = GdbSocket(ctx, address, proc, proc_iut, parent, verbose, connections) gdb.mi_command('-target-attach %d' % pid) gdb.cli_command('python import pdb_clone.bootstrappdb_gdb') asyncore.loop(map=connections) proc.wait() return gdb.error
python
def spawn_gdb(pid, address=DFLT_ADDRESS, gdb='gdb', verbose=False, ctx=None, proc_iut=None): """Spawn gdb and attach to a process.""" parent, child = socket.socketpair() proc = Popen([gdb, '--interpreter=mi', '-nx'], bufsize=0, stdin=child, stdout=child, stderr=STDOUT) child.close() connections = {} gdb = GdbSocket(ctx, address, proc, proc_iut, parent, verbose, connections) gdb.mi_command('-target-attach %d' % pid) gdb.cli_command('python import pdb_clone.bootstrappdb_gdb') asyncore.loop(map=connections) proc.wait() return gdb.error
[ "def", "spawn_gdb", "(", "pid", ",", "address", "=", "DFLT_ADDRESS", ",", "gdb", "=", "'gdb'", ",", "verbose", "=", "False", ",", "ctx", "=", "None", ",", "proc_iut", "=", "None", ")", ":", "parent", ",", "child", "=", "socket", ".", "socketpair", "(", ")", "proc", "=", "Popen", "(", "[", "gdb", ",", "'--interpreter=mi'", ",", "'-nx'", "]", ",", "bufsize", "=", "0", ",", "stdin", "=", "child", ",", "stdout", "=", "child", ",", "stderr", "=", "STDOUT", ")", "child", ".", "close", "(", ")", "connections", "=", "{", "}", "gdb", "=", "GdbSocket", "(", "ctx", ",", "address", ",", "proc", ",", "proc_iut", ",", "parent", ",", "verbose", ",", "connections", ")", "gdb", ".", "mi_command", "(", "'-target-attach %d'", "%", "pid", ")", "gdb", ".", "cli_command", "(", "'python import pdb_clone.bootstrappdb_gdb'", ")", "asyncore", ".", "loop", "(", "map", "=", "connections", ")", "proc", ".", "wait", "(", ")", "return", "gdb", ".", "error" ]
Spawn gdb and attach to a process.
[ "Spawn", "gdb", "and", "attach", "to", "a", "process", "." ]
f781537c243a4874b246d43dbdef8c4279f0094d
https://github.com/corpusops/pdbclone/blob/f781537c243a4874b246d43dbdef8c4279f0094d/lib/pdb_clone/attach.py#L503-L519
train
corpusops/pdbclone
lib/pdb_clone/attach.py
attach_loop
def attach_loop(argv): """Spawn the process, then repeatedly attach to the process.""" # Check if the pdbhandler module is built into python. p = Popen((sys.executable, '-X', 'pdbhandler', '-c', 'import pdbhandler; pdbhandler.get_handler().host'), stdout=PIPE, stderr=STDOUT) p.wait() use_xoption = True if p.returncode == 0 else False # Spawn the process. args = [sys.executable] if use_xoption: # Use SIGUSR2 as faulthandler is set on python test suite with # SIGUSR1. args.extend(['-X', 'pdbhandler=localhost 7935 %d' % signal.SIGUSR2]) args.extend(argv) proc = Popen(args) else: args.extend(argv) proc = Popen(args) # Repeatedly attach to the process using the '-X' python option or gdb. ctx = Context() error = None time.sleep(.5 + random.random()) while not error and proc.poll() is None: if use_xoption: os.kill(proc.pid, signal.SIGUSR2) connections = {} dev_null = io.StringIO() if PY3 else StringIO.StringIO() asock = AttachSocketWithDetach(connections, stdout=dev_null) asock.create_socket(socket.AF_INET, socket.SOCK_STREAM) connect_process(asock, ctx, proc) asyncore.loop(map=connections) else: error = spawn_gdb(proc.pid, ctx=ctx, proc_iut=proc) time.sleep(random.random()) if error and gdb_terminated(error): error = None if proc.poll() is None: proc.terminate() else: print('pdb-attach: program under test return code:', proc.wait()) result = str(ctx.result) if result: print(result) return error
python
def attach_loop(argv): """Spawn the process, then repeatedly attach to the process.""" # Check if the pdbhandler module is built into python. p = Popen((sys.executable, '-X', 'pdbhandler', '-c', 'import pdbhandler; pdbhandler.get_handler().host'), stdout=PIPE, stderr=STDOUT) p.wait() use_xoption = True if p.returncode == 0 else False # Spawn the process. args = [sys.executable] if use_xoption: # Use SIGUSR2 as faulthandler is set on python test suite with # SIGUSR1. args.extend(['-X', 'pdbhandler=localhost 7935 %d' % signal.SIGUSR2]) args.extend(argv) proc = Popen(args) else: args.extend(argv) proc = Popen(args) # Repeatedly attach to the process using the '-X' python option or gdb. ctx = Context() error = None time.sleep(.5 + random.random()) while not error and proc.poll() is None: if use_xoption: os.kill(proc.pid, signal.SIGUSR2) connections = {} dev_null = io.StringIO() if PY3 else StringIO.StringIO() asock = AttachSocketWithDetach(connections, stdout=dev_null) asock.create_socket(socket.AF_INET, socket.SOCK_STREAM) connect_process(asock, ctx, proc) asyncore.loop(map=connections) else: error = spawn_gdb(proc.pid, ctx=ctx, proc_iut=proc) time.sleep(random.random()) if error and gdb_terminated(error): error = None if proc.poll() is None: proc.terminate() else: print('pdb-attach: program under test return code:', proc.wait()) result = str(ctx.result) if result: print(result) return error
[ "def", "attach_loop", "(", "argv", ")", ":", "# Check if the pdbhandler module is built into python.", "p", "=", "Popen", "(", "(", "sys", ".", "executable", ",", "'-X'", ",", "'pdbhandler'", ",", "'-c'", ",", "'import pdbhandler; pdbhandler.get_handler().host'", ")", ",", "stdout", "=", "PIPE", ",", "stderr", "=", "STDOUT", ")", "p", ".", "wait", "(", ")", "use_xoption", "=", "True", "if", "p", ".", "returncode", "==", "0", "else", "False", "# Spawn the process.", "args", "=", "[", "sys", ".", "executable", "]", "if", "use_xoption", ":", "# Use SIGUSR2 as faulthandler is set on python test suite with", "# SIGUSR1.", "args", ".", "extend", "(", "[", "'-X'", ",", "'pdbhandler=localhost 7935 %d'", "%", "signal", ".", "SIGUSR2", "]", ")", "args", ".", "extend", "(", "argv", ")", "proc", "=", "Popen", "(", "args", ")", "else", ":", "args", ".", "extend", "(", "argv", ")", "proc", "=", "Popen", "(", "args", ")", "# Repeatedly attach to the process using the '-X' python option or gdb.", "ctx", "=", "Context", "(", ")", "error", "=", "None", "time", ".", "sleep", "(", ".5", "+", "random", ".", "random", "(", ")", ")", "while", "not", "error", "and", "proc", ".", "poll", "(", ")", "is", "None", ":", "if", "use_xoption", ":", "os", ".", "kill", "(", "proc", ".", "pid", ",", "signal", ".", "SIGUSR2", ")", "connections", "=", "{", "}", "dev_null", "=", "io", ".", "StringIO", "(", ")", "if", "PY3", "else", "StringIO", ".", "StringIO", "(", ")", "asock", "=", "AttachSocketWithDetach", "(", "connections", ",", "stdout", "=", "dev_null", ")", "asock", ".", "create_socket", "(", "socket", ".", "AF_INET", ",", "socket", ".", "SOCK_STREAM", ")", "connect_process", "(", "asock", ",", "ctx", ",", "proc", ")", "asyncore", ".", "loop", "(", "map", "=", "connections", ")", "else", ":", "error", "=", "spawn_gdb", "(", "proc", ".", "pid", ",", "ctx", "=", "ctx", ",", "proc_iut", "=", "proc", ")", "time", ".", "sleep", "(", "random", ".", "random", "(", ")", ")", "if", "error", "and", "gdb_terminated", "(", "error", ")", ":", "error", "=", "None", "if", "proc", ".", "poll", "(", ")", "is", "None", ":", "proc", ".", "terminate", "(", ")", "else", ":", "print", "(", "'pdb-attach: program under test return code:'", ",", "proc", ".", "wait", "(", ")", ")", "result", "=", "str", "(", "ctx", ".", "result", ")", "if", "result", ":", "print", "(", "result", ")", "return", "error" ]
Spawn the process, then repeatedly attach to the process.
[ "Spawn", "the", "process", "then", "repeatedly", "attach", "to", "the", "process", "." ]
f781537c243a4874b246d43dbdef8c4279f0094d
https://github.com/corpusops/pdbclone/blob/f781537c243a4874b246d43dbdef8c4279f0094d/lib/pdb_clone/attach.py#L521-L570
train
corpusops/pdbclone
lib/pdb_clone/attach.py
StatementLine.skip
def skip(self): """Skip this py-pdb command to avoid attaching within the same loop.""" line = self.line self.line = '' # 'line' is the statement line of the previous py-pdb command. if line in self.lines: if not self.skipping: self.skipping = True printflush('Skipping lines', end='') printflush('.', end='') return True elif line: self.lines.append(line) if len(self.lines) > 30: self.lines.popleft() return False
python
def skip(self): """Skip this py-pdb command to avoid attaching within the same loop.""" line = self.line self.line = '' # 'line' is the statement line of the previous py-pdb command. if line in self.lines: if not self.skipping: self.skipping = True printflush('Skipping lines', end='') printflush('.', end='') return True elif line: self.lines.append(line) if len(self.lines) > 30: self.lines.popleft() return False
[ "def", "skip", "(", "self", ")", ":", "line", "=", "self", ".", "line", "self", ".", "line", "=", "''", "# 'line' is the statement line of the previous py-pdb command.", "if", "line", "in", "self", ".", "lines", ":", "if", "not", "self", ".", "skipping", ":", "self", ".", "skipping", "=", "True", "printflush", "(", "'Skipping lines'", ",", "end", "=", "''", ")", "printflush", "(", "'.'", ",", "end", "=", "''", ")", "return", "True", "elif", "line", ":", "self", ".", "lines", ".", "append", "(", "line", ")", "if", "len", "(", "self", ".", "lines", ")", ">", "30", ":", "self", ".", "lines", ".", "popleft", "(", ")", "return", "False" ]
Skip this py-pdb command to avoid attaching within the same loop.
[ "Skip", "this", "py", "-", "pdb", "command", "to", "avoid", "attaching", "within", "the", "same", "loop", "." ]
f781537c243a4874b246d43dbdef8c4279f0094d
https://github.com/corpusops/pdbclone/blob/f781537c243a4874b246d43dbdef8c4279f0094d/lib/pdb_clone/attach.py#L242-L259
train
Nukesor/pueue
pueue/daemon/logger.py
Logger.rotate
def rotate(self, log): """Move the current log to a new file with timestamp and create a new empty log file.""" self.write(log, rotate=True) self.write({})
python
def rotate(self, log): """Move the current log to a new file with timestamp and create a new empty log file.""" self.write(log, rotate=True) self.write({})
[ "def", "rotate", "(", "self", ",", "log", ")", ":", "self", ".", "write", "(", "log", ",", "rotate", "=", "True", ")", "self", ".", "write", "(", "{", "}", ")" ]
Move the current log to a new file with timestamp and create a new empty log file.
[ "Move", "the", "current", "log", "to", "a", "new", "file", "with", "timestamp", "and", "create", "a", "new", "empty", "log", "file", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/logger.py#L72-L75
train
Nukesor/pueue
pueue/daemon/logger.py
Logger.write
def write(self, log, rotate=False): """Write the output of all finished processes to a compiled log file.""" # Get path for logfile if rotate: timestamp = time.strftime('-%Y%m%d-%H%M') logPath = os.path.join(self.log_dir, 'queue{}.log'.format(timestamp)) else: logPath = os.path.join(self.log_dir, 'queue.log') # Remove existing Log if os.path.exists(logPath): os.remove(logPath) log_file = open(logPath, 'w') log_file.write('Pueue log for executed Commands: \n \n') # Format, color and write log for key, logentry in log.items(): if logentry.get('returncode') is not None: try: # Get returncode color: returncode = logentry['returncode'] if returncode == 0: returncode = Color('{autogreen}' + '{}'.format(returncode) + '{/autogreen}') else: returncode = Color('{autored}' + '{}'.format(returncode) + '{/autored}') # Write command id with returncode and actual command log_file.write( Color('{autoyellow}' + 'Command #{} '.format(key) + '{/autoyellow}') + 'exited with returncode {}: \n'.format(returncode) + '"{}" \n'.format(logentry['command']) ) # Write path log_file.write('Path: {} \n'.format(logentry['path'])) # Write times log_file.write('Start: {}, End: {} \n' .format(logentry['start'], logentry['end'])) # Write STDERR if logentry['stderr']: log_file.write(Color('{autored}Stderr output: {/autored}\n ') + logentry['stderr']) # Write STDOUT if len(logentry['stdout']) > 0: log_file.write(Color('{autogreen}Stdout output: {/autogreen}\n ') + logentry['stdout']) log_file.write('\n') except Exception as a: print('Failed while writing to log file. Wrong file permissions?') print('Exception: {}'.format(str(a))) log_file.close()
python
def write(self, log, rotate=False): """Write the output of all finished processes to a compiled log file.""" # Get path for logfile if rotate: timestamp = time.strftime('-%Y%m%d-%H%M') logPath = os.path.join(self.log_dir, 'queue{}.log'.format(timestamp)) else: logPath = os.path.join(self.log_dir, 'queue.log') # Remove existing Log if os.path.exists(logPath): os.remove(logPath) log_file = open(logPath, 'w') log_file.write('Pueue log for executed Commands: \n \n') # Format, color and write log for key, logentry in log.items(): if logentry.get('returncode') is not None: try: # Get returncode color: returncode = logentry['returncode'] if returncode == 0: returncode = Color('{autogreen}' + '{}'.format(returncode) + '{/autogreen}') else: returncode = Color('{autored}' + '{}'.format(returncode) + '{/autored}') # Write command id with returncode and actual command log_file.write( Color('{autoyellow}' + 'Command #{} '.format(key) + '{/autoyellow}') + 'exited with returncode {}: \n'.format(returncode) + '"{}" \n'.format(logentry['command']) ) # Write path log_file.write('Path: {} \n'.format(logentry['path'])) # Write times log_file.write('Start: {}, End: {} \n' .format(logentry['start'], logentry['end'])) # Write STDERR if logentry['stderr']: log_file.write(Color('{autored}Stderr output: {/autored}\n ') + logentry['stderr']) # Write STDOUT if len(logentry['stdout']) > 0: log_file.write(Color('{autogreen}Stdout output: {/autogreen}\n ') + logentry['stdout']) log_file.write('\n') except Exception as a: print('Failed while writing to log file. Wrong file permissions?') print('Exception: {}'.format(str(a))) log_file.close()
[ "def", "write", "(", "self", ",", "log", ",", "rotate", "=", "False", ")", ":", "# Get path for logfile", "if", "rotate", ":", "timestamp", "=", "time", ".", "strftime", "(", "'-%Y%m%d-%H%M'", ")", "logPath", "=", "os", ".", "path", ".", "join", "(", "self", ".", "log_dir", ",", "'queue{}.log'", ".", "format", "(", "timestamp", ")", ")", "else", ":", "logPath", "=", "os", ".", "path", ".", "join", "(", "self", ".", "log_dir", ",", "'queue.log'", ")", "# Remove existing Log", "if", "os", ".", "path", ".", "exists", "(", "logPath", ")", ":", "os", ".", "remove", "(", "logPath", ")", "log_file", "=", "open", "(", "logPath", ",", "'w'", ")", "log_file", ".", "write", "(", "'Pueue log for executed Commands: \\n \\n'", ")", "# Format, color and write log", "for", "key", ",", "logentry", "in", "log", ".", "items", "(", ")", ":", "if", "logentry", ".", "get", "(", "'returncode'", ")", "is", "not", "None", ":", "try", ":", "# Get returncode color:", "returncode", "=", "logentry", "[", "'returncode'", "]", "if", "returncode", "==", "0", ":", "returncode", "=", "Color", "(", "'{autogreen}'", "+", "'{}'", ".", "format", "(", "returncode", ")", "+", "'{/autogreen}'", ")", "else", ":", "returncode", "=", "Color", "(", "'{autored}'", "+", "'{}'", ".", "format", "(", "returncode", ")", "+", "'{/autored}'", ")", "# Write command id with returncode and actual command", "log_file", ".", "write", "(", "Color", "(", "'{autoyellow}'", "+", "'Command #{} '", ".", "format", "(", "key", ")", "+", "'{/autoyellow}'", ")", "+", "'exited with returncode {}: \\n'", ".", "format", "(", "returncode", ")", "+", "'\"{}\" \\n'", ".", "format", "(", "logentry", "[", "'command'", "]", ")", ")", "# Write path", "log_file", ".", "write", "(", "'Path: {} \\n'", ".", "format", "(", "logentry", "[", "'path'", "]", ")", ")", "# Write times", "log_file", ".", "write", "(", "'Start: {}, End: {} \\n'", ".", "format", "(", "logentry", "[", "'start'", "]", ",", "logentry", "[", "'end'", "]", ")", ")", "# Write STDERR", "if", "logentry", "[", "'stderr'", "]", ":", "log_file", ".", "write", "(", "Color", "(", "'{autored}Stderr output: {/autored}\\n '", ")", "+", "logentry", "[", "'stderr'", "]", ")", "# Write STDOUT", "if", "len", "(", "logentry", "[", "'stdout'", "]", ")", ">", "0", ":", "log_file", ".", "write", "(", "Color", "(", "'{autogreen}Stdout output: {/autogreen}\\n '", ")", "+", "logentry", "[", "'stdout'", "]", ")", "log_file", ".", "write", "(", "'\\n'", ")", "except", "Exception", "as", "a", ":", "print", "(", "'Failed while writing to log file. Wrong file permissions?'", ")", "print", "(", "'Exception: {}'", ".", "format", "(", "str", "(", "a", ")", ")", ")", "log_file", ".", "close", "(", ")" ]
Write the output of all finished processes to a compiled log file.
[ "Write", "the", "output", "of", "all", "finished", "processes", "to", "a", "compiled", "log", "file", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/logger.py#L77-L129
train
Nukesor/pueue
pueue/daemon/logger.py
Logger.remove_old
def remove_old(self, max_log_time): """Remove all logs which are older than the specified time.""" files = glob.glob('{}/queue-*'.format(self.log_dir)) files = list(map(lambda x: os.path.basename(x), files)) for log_file in files: # Get time stamp from filename name = os.path.splitext(log_file)[0] timestamp = name.split('-', maxsplit=1)[1] # Get datetime from time stamp time = datetime.strptime(timestamp, '%Y%m%d-%H%M') now = datetime.now() # Get total delta in seconds delta = now - time seconds = delta.total_seconds() # Delete log file, if the delta is bigger than the specified log time if seconds > int(max_log_time): log_filePath = os.path.join(self.log_dir, log_file) os.remove(log_filePath)
python
def remove_old(self, max_log_time): """Remove all logs which are older than the specified time.""" files = glob.glob('{}/queue-*'.format(self.log_dir)) files = list(map(lambda x: os.path.basename(x), files)) for log_file in files: # Get time stamp from filename name = os.path.splitext(log_file)[0] timestamp = name.split('-', maxsplit=1)[1] # Get datetime from time stamp time = datetime.strptime(timestamp, '%Y%m%d-%H%M') now = datetime.now() # Get total delta in seconds delta = now - time seconds = delta.total_seconds() # Delete log file, if the delta is bigger than the specified log time if seconds > int(max_log_time): log_filePath = os.path.join(self.log_dir, log_file) os.remove(log_filePath)
[ "def", "remove_old", "(", "self", ",", "max_log_time", ")", ":", "files", "=", "glob", ".", "glob", "(", "'{}/queue-*'", ".", "format", "(", "self", ".", "log_dir", ")", ")", "files", "=", "list", "(", "map", "(", "lambda", "x", ":", "os", ".", "path", ".", "basename", "(", "x", ")", ",", "files", ")", ")", "for", "log_file", "in", "files", ":", "# Get time stamp from filename", "name", "=", "os", ".", "path", ".", "splitext", "(", "log_file", ")", "[", "0", "]", "timestamp", "=", "name", ".", "split", "(", "'-'", ",", "maxsplit", "=", "1", ")", "[", "1", "]", "# Get datetime from time stamp", "time", "=", "datetime", ".", "strptime", "(", "timestamp", ",", "'%Y%m%d-%H%M'", ")", "now", "=", "datetime", ".", "now", "(", ")", "# Get total delta in seconds", "delta", "=", "now", "-", "time", "seconds", "=", "delta", ".", "total_seconds", "(", ")", "# Delete log file, if the delta is bigger than the specified log time", "if", "seconds", ">", "int", "(", "max_log_time", ")", ":", "log_filePath", "=", "os", ".", "path", ".", "join", "(", "self", ".", "log_dir", ",", "log_file", ")", "os", ".", "remove", "(", "log_filePath", ")" ]
Remove all logs which are older than the specified time.
[ "Remove", "all", "logs", "which", "are", "older", "than", "the", "specified", "time", "." ]
f1d276360454d4dd2738658a13df1e20caa4b926
https://github.com/Nukesor/pueue/blob/f1d276360454d4dd2738658a13df1e20caa4b926/pueue/daemon/logger.py#L131-L152
train
dlecocq/nsq-py
nsq/http/__init__.py
wrap
def wrap(function, *args, **kwargs): '''Wrap a function that returns a request with some exception handling''' try: req = function(*args, **kwargs) logger.debug('Got %s: %s', req.status_code, req.content) if req.status_code == 200: return req else: raise ClientException(req.reason, req.content) except ClientException: raise except Exception as exc: raise ClientException(exc)
python
def wrap(function, *args, **kwargs): '''Wrap a function that returns a request with some exception handling''' try: req = function(*args, **kwargs) logger.debug('Got %s: %s', req.status_code, req.content) if req.status_code == 200: return req else: raise ClientException(req.reason, req.content) except ClientException: raise except Exception as exc: raise ClientException(exc)
[ "def", "wrap", "(", "function", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "try", ":", "req", "=", "function", "(", "*", "args", ",", "*", "*", "kwargs", ")", "logger", ".", "debug", "(", "'Got %s: %s'", ",", "req", ".", "status_code", ",", "req", ".", "content", ")", "if", "req", ".", "status_code", "==", "200", ":", "return", "req", "else", ":", "raise", "ClientException", "(", "req", ".", "reason", ",", "req", ".", "content", ")", "except", "ClientException", ":", "raise", "except", "Exception", "as", "exc", ":", "raise", "ClientException", "(", "exc", ")" ]
Wrap a function that returns a request with some exception handling
[ "Wrap", "a", "function", "that", "returns", "a", "request", "with", "some", "exception", "handling" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/http/__init__.py#L12-L24
train
dlecocq/nsq-py
nsq/http/__init__.py
json_wrap
def json_wrap(function, *args, **kwargs): '''Return the json content of a function that returns a request''' try: # Some responses have data = None, but they generally signal a # successful API call as well. response = json.loads(function(*args, **kwargs).content) if 'data' in response: return response['data'] or True else: return response except Exception as exc: raise ClientException(exc)
python
def json_wrap(function, *args, **kwargs): '''Return the json content of a function that returns a request''' try: # Some responses have data = None, but they generally signal a # successful API call as well. response = json.loads(function(*args, **kwargs).content) if 'data' in response: return response['data'] or True else: return response except Exception as exc: raise ClientException(exc)
[ "def", "json_wrap", "(", "function", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "try", ":", "# Some responses have data = None, but they generally signal a", "# successful API call as well.", "response", "=", "json", ".", "loads", "(", "function", "(", "*", "args", ",", "*", "*", "kwargs", ")", ".", "content", ")", "if", "'data'", "in", "response", ":", "return", "response", "[", "'data'", "]", "or", "True", "else", ":", "return", "response", "except", "Exception", "as", "exc", ":", "raise", "ClientException", "(", "exc", ")" ]
Return the json content of a function that returns a request
[ "Return", "the", "json", "content", "of", "a", "function", "that", "returns", "a", "request" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/http/__init__.py#L28-L39
train
dlecocq/nsq-py
nsq/http/__init__.py
ok_check
def ok_check(function, *args, **kwargs): '''Ensure that the response body is OK''' req = function(*args, **kwargs) if req.content.lower() != 'ok': raise ClientException(req.content) return req.content
python
def ok_check(function, *args, **kwargs): '''Ensure that the response body is OK''' req = function(*args, **kwargs) if req.content.lower() != 'ok': raise ClientException(req.content) return req.content
[ "def", "ok_check", "(", "function", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "req", "=", "function", "(", "*", "args", ",", "*", "*", "kwargs", ")", "if", "req", ".", "content", ".", "lower", "(", ")", "!=", "'ok'", ":", "raise", "ClientException", "(", "req", ".", "content", ")", "return", "req", ".", "content" ]
Ensure that the response body is OK
[ "Ensure", "that", "the", "response", "body", "is", "OK" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/http/__init__.py#L43-L48
train
dlecocq/nsq-py
nsq/http/__init__.py
BaseClient.get
def get(self, path, *args, **kwargs): '''GET the provided endpoint''' target = self._host.relative(path).utf8 if not isinstance(target, basestring): # on older versions of the `url` library, .utf8 is a method, not a property target = target() params = kwargs.get('params', {}) params.update(self._params) kwargs['params'] = params logger.debug('GET %s with %s, %s', target, args, kwargs) return requests.get(target, *args, **kwargs)
python
def get(self, path, *args, **kwargs): '''GET the provided endpoint''' target = self._host.relative(path).utf8 if not isinstance(target, basestring): # on older versions of the `url` library, .utf8 is a method, not a property target = target() params = kwargs.get('params', {}) params.update(self._params) kwargs['params'] = params logger.debug('GET %s with %s, %s', target, args, kwargs) return requests.get(target, *args, **kwargs)
[ "def", "get", "(", "self", ",", "path", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "target", "=", "self", ".", "_host", ".", "relative", "(", "path", ")", ".", "utf8", "if", "not", "isinstance", "(", "target", ",", "basestring", ")", ":", "# on older versions of the `url` library, .utf8 is a method, not a property", "target", "=", "target", "(", ")", "params", "=", "kwargs", ".", "get", "(", "'params'", ",", "{", "}", ")", "params", ".", "update", "(", "self", ".", "_params", ")", "kwargs", "[", "'params'", "]", "=", "params", "logger", ".", "debug", "(", "'GET %s with %s, %s'", ",", "target", ",", "args", ",", "kwargs", ")", "return", "requests", ".", "get", "(", "target", ",", "*", "args", ",", "*", "*", "kwargs", ")" ]
GET the provided endpoint
[ "GET", "the", "provided", "endpoint" ]
3ecacf6ab7719d38031179277113d875554a0c16
https://github.com/dlecocq/nsq-py/blob/3ecacf6ab7719d38031179277113d875554a0c16/nsq/http/__init__.py#L67-L77
train
MediaFire/mediafire-python-open-sdk
mediafire/uploader.py
decode_resumable_upload_bitmap
def decode_resumable_upload_bitmap(bitmap_node, number_of_units): """Decodes bitmap_node to hash of unit_id: is_uploaded bitmap_node -- bitmap node of resumable_upload with 'count' number and 'words' containing array number_of_units -- number of units we are uploading to define the number of bits for bitmap """ bitmap = 0 for token_id in range(int(bitmap_node['count'])): value = int(bitmap_node['words'][token_id]) bitmap = bitmap | (value << (0xf * token_id)) result = {} for unit_id in range(number_of_units): mask = 1 << unit_id result[unit_id] = (bitmap & mask) == mask return result
python
def decode_resumable_upload_bitmap(bitmap_node, number_of_units): """Decodes bitmap_node to hash of unit_id: is_uploaded bitmap_node -- bitmap node of resumable_upload with 'count' number and 'words' containing array number_of_units -- number of units we are uploading to define the number of bits for bitmap """ bitmap = 0 for token_id in range(int(bitmap_node['count'])): value = int(bitmap_node['words'][token_id]) bitmap = bitmap | (value << (0xf * token_id)) result = {} for unit_id in range(number_of_units): mask = 1 << unit_id result[unit_id] = (bitmap & mask) == mask return result
[ "def", "decode_resumable_upload_bitmap", "(", "bitmap_node", ",", "number_of_units", ")", ":", "bitmap", "=", "0", "for", "token_id", "in", "range", "(", "int", "(", "bitmap_node", "[", "'count'", "]", ")", ")", ":", "value", "=", "int", "(", "bitmap_node", "[", "'words'", "]", "[", "token_id", "]", ")", "bitmap", "=", "bitmap", "|", "(", "value", "<<", "(", "0xf", "*", "token_id", ")", ")", "result", "=", "{", "}", "for", "unit_id", "in", "range", "(", "number_of_units", ")", ":", "mask", "=", "1", "<<", "unit_id", "result", "[", "unit_id", "]", "=", "(", "bitmap", "&", "mask", ")", "==", "mask", "return", "result" ]
Decodes bitmap_node to hash of unit_id: is_uploaded bitmap_node -- bitmap node of resumable_upload with 'count' number and 'words' containing array number_of_units -- number of units we are uploading to define the number of bits for bitmap
[ "Decodes", "bitmap_node", "to", "hash", "of", "unit_id", ":", "is_uploaded" ]
8f1f23db1b16f16e026f5c6777aec32d00baa05f
https://github.com/MediaFire/mediafire-python-open-sdk/blob/8f1f23db1b16f16e026f5c6777aec32d00baa05f/mediafire/uploader.py#L117-L136
train
MediaFire/mediafire-python-open-sdk
mediafire/uploader.py
compute_hash_info
def compute_hash_info(fd, unit_size=None): """Get MediaFireHashInfo structure from the fd, unit_size fd -- file descriptor - expects exclusive access because of seeking unit_size -- size of a single unit Returns MediaFireHashInfo: hi.file -- sha256 of the whole file hi.units -- list of sha256 hashes for each unit """ logger.debug("compute_hash_info(%s, unit_size=%s)", fd, unit_size) fd.seek(0, os.SEEK_END) file_size = fd.tell() fd.seek(0, os.SEEK_SET) units = [] unit_counter = 0 file_hash = hashlib.sha256() unit_hash = hashlib.sha256() for chunk in iter(lambda: fd.read(HASH_CHUNK_SIZE_BYTES), b''): file_hash.update(chunk) unit_hash.update(chunk) unit_counter += len(chunk) if unit_size is not None and unit_counter == unit_size: # flush the current unit hash units.append(unit_hash.hexdigest().lower()) unit_counter = 0 unit_hash = hashlib.sha256() if unit_size is not None and unit_counter > 0: # leftover block units.append(unit_hash.hexdigest().lower()) fd.seek(0, os.SEEK_SET) return MediaFireHashInfo( file=file_hash.hexdigest().lower(), units=units, size=file_size )
python
def compute_hash_info(fd, unit_size=None): """Get MediaFireHashInfo structure from the fd, unit_size fd -- file descriptor - expects exclusive access because of seeking unit_size -- size of a single unit Returns MediaFireHashInfo: hi.file -- sha256 of the whole file hi.units -- list of sha256 hashes for each unit """ logger.debug("compute_hash_info(%s, unit_size=%s)", fd, unit_size) fd.seek(0, os.SEEK_END) file_size = fd.tell() fd.seek(0, os.SEEK_SET) units = [] unit_counter = 0 file_hash = hashlib.sha256() unit_hash = hashlib.sha256() for chunk in iter(lambda: fd.read(HASH_CHUNK_SIZE_BYTES), b''): file_hash.update(chunk) unit_hash.update(chunk) unit_counter += len(chunk) if unit_size is not None and unit_counter == unit_size: # flush the current unit hash units.append(unit_hash.hexdigest().lower()) unit_counter = 0 unit_hash = hashlib.sha256() if unit_size is not None and unit_counter > 0: # leftover block units.append(unit_hash.hexdigest().lower()) fd.seek(0, os.SEEK_SET) return MediaFireHashInfo( file=file_hash.hexdigest().lower(), units=units, size=file_size )
[ "def", "compute_hash_info", "(", "fd", ",", "unit_size", "=", "None", ")", ":", "logger", ".", "debug", "(", "\"compute_hash_info(%s, unit_size=%s)\"", ",", "fd", ",", "unit_size", ")", "fd", ".", "seek", "(", "0", ",", "os", ".", "SEEK_END", ")", "file_size", "=", "fd", ".", "tell", "(", ")", "fd", ".", "seek", "(", "0", ",", "os", ".", "SEEK_SET", ")", "units", "=", "[", "]", "unit_counter", "=", "0", "file_hash", "=", "hashlib", ".", "sha256", "(", ")", "unit_hash", "=", "hashlib", ".", "sha256", "(", ")", "for", "chunk", "in", "iter", "(", "lambda", ":", "fd", ".", "read", "(", "HASH_CHUNK_SIZE_BYTES", ")", ",", "b''", ")", ":", "file_hash", ".", "update", "(", "chunk", ")", "unit_hash", ".", "update", "(", "chunk", ")", "unit_counter", "+=", "len", "(", "chunk", ")", "if", "unit_size", "is", "not", "None", "and", "unit_counter", "==", "unit_size", ":", "# flush the current unit hash", "units", ".", "append", "(", "unit_hash", ".", "hexdigest", "(", ")", ".", "lower", "(", ")", ")", "unit_counter", "=", "0", "unit_hash", "=", "hashlib", ".", "sha256", "(", ")", "if", "unit_size", "is", "not", "None", "and", "unit_counter", ">", "0", ":", "# leftover block", "units", ".", "append", "(", "unit_hash", ".", "hexdigest", "(", ")", ".", "lower", "(", ")", ")", "fd", ".", "seek", "(", "0", ",", "os", ".", "SEEK_SET", ")", "return", "MediaFireHashInfo", "(", "file", "=", "file_hash", ".", "hexdigest", "(", ")", ".", "lower", "(", ")", ",", "units", "=", "units", ",", "size", "=", "file_size", ")" ]
Get MediaFireHashInfo structure from the fd, unit_size fd -- file descriptor - expects exclusive access because of seeking unit_size -- size of a single unit Returns MediaFireHashInfo: hi.file -- sha256 of the whole file hi.units -- list of sha256 hashes for each unit
[ "Get", "MediaFireHashInfo", "structure", "from", "the", "fd", "unit_size" ]
8f1f23db1b16f16e026f5c6777aec32d00baa05f
https://github.com/MediaFire/mediafire-python-open-sdk/blob/8f1f23db1b16f16e026f5c6777aec32d00baa05f/mediafire/uploader.py#L139-L184
train