code stringlengths 52 7.75k | docs stringlengths 1 5.85k |
|---|---|
def command():
return (
OneOrMore(
Word(approved_printables+' ').setResultsName('command',
listAllMatches=True) ^
Grammar.__command_input_output.setResultsName('_in',
listAllMatches=True)
)
) | Grammar for commands found in the overall input files. |
def listen_to_event_updates():
def callback(event):
print('Event:', event)
client.create_event_subscription(instance='simulator', on_data=callback)
sleep(5) | Subscribe to events. |
def get_current_scene_node():
c = cmds.namespaceInfo(':', listOnlyDependencyNodes=True, absoluteName=True, dagPath=True)
l = cmds.ls(c, type='jb_sceneNode', absoluteName=True)
if not l:
return
else:
for n in sorted(l):
if not cmds.listConnections("%s.reftrack" % n, d=Fal... | Return the name of the jb_sceneNode, that describes the current scene or None if there is no scene node.
:returns: the full name of the node or none, if there is no scene node
:rtype: str | None
:raises: None |
def updateSpec(self, *args, **kwargs):
if args[0] is None:
self.specPlot.clearImg()
elif isinstance(args[0], basestring):
self.specPlot.fromFile(*args, **kwargs)
else:
self.specPlot.updateData(*args,**kwargs) | Updates the spectrogram. First argument can be a filename,
or a data array. If no arguments are given, clears the spectrograms.
For other arguments, see: :meth:`SpecWidget.updateData<sparkle.gui.plotting.pyqtgraph_widgets.SpecWidget.updateData>` |
def showSpec(self, fname):
if not self.specPlot.hasImg() and fname is not None:
self.specPlot.fromFile(fname) | Draws the spectrogram if it is currently None |
def updateSpiketrace(self, xdata, ydata, plotname=None):
if plotname is None:
plotname = self.responsePlots.keys()[0]
if len(ydata.shape) == 1:
self.responsePlots[plotname].updateData(axeskey='response', x=xdata, y=ydata)
else:
self.responsePlots[plo... | Updates the spike trace
:param xdata: index values
:type xdata: numpy.ndarray
:param ydata: values to plot
:type ydata: numpy.ndarray |
def addRasterPoints(self, xdata, repnum, plotname=None):
if plotname is None:
plotname = self.responsePlots.keys()[0]
ydata = np.ones_like(xdata)*repnum
self.responsePlots[plotname].appendData('raster', xdata, ydata) | Add a list (or numpy array) of points to raster plot,
in any order.
:param xdata: bin centers
:param ydata: rep number |
def updateSignal(self, xdata, ydata, plotname=None):
if plotname is None:
plotname = self.responsePlots.keys()[0]
self.responsePlots[plotname].updateData(axeskey='stim', x=xdata, y=ydata) | Updates the trace of the outgoing signal
:param xdata: time points of recording
:param ydata: brain potential at time points |
def setXlimits(self, lims):
# update all "linked", plots
self.specPlot.setXlim(lims)
for plot in self.responsePlots.values():
plot.setXlim(lims)
# ridiculous...
sizes = self.splittersw.sizes()
if len(sizes) > 1:
if self.badbadbad:
... | Sets the X axis limits of the trace plot
:param lims: (min, max) of x axis, in same units as data
:type lims: (float, float) |
def setNreps(self, nreps):
for plot in self.responsePlots.values():
plot.setNreps(nreps) | Sets the number of reps before the raster plot resets |
def specAutoRange(self):
trace_range = self.responsePlots.values()[0].viewRange()[0]
vb = self.specPlot.getViewBox()
vb.autoRange(padding=0)
self.specPlot.setXlim(trace_range) | Auto adjusts the visible range of the spectrogram |
def interpret_header(self):
# handle special cases since date-obs field changed names
if 'DATE_OBS' in self.header:
self.date = self.header['DATE_OBS']
elif 'DATE-OBS' in self.header:
self.date = self.header['DATE-OBS']
else:
raise Exception("... | Read pertinent information from the image headers,
especially location and radius of the Sun to calculate the default thematic map
:return: setes self.date, self.cy, self.cx, and self.sun_radius_pixel |
def save(self):
out = Outgest(self.output, self.selection_array.astype('uint8'), self.headers, self.config_path)
out.save()
out.upload() | Save as a FITS file and attempt an upload if designated in the configuration file |
def on_exit(self):
answer = messagebox.askyesnocancel("Exit", "Do you want to save as you quit the application?")
if answer:
self.save()
self.quit()
self.destroy()
elif answer is None:
pass # the cancel action
else:
sel... | When you click to exit, this function is called, prompts whether to save |
def make_gui(self):
self.option_window = Toplevel()
self.option_window.protocol("WM_DELETE_WINDOW", self.on_exit)
self.canvas_frame = tk.Frame(self, height=500)
self.option_frame = tk.Frame(self.option_window, height=300)
self.canvas_frame.pack(side=tk.LEFT, fill=tk.BOTH... | Setups the general structure of the gui, the first function called |
def configure_threecolor_image(self):
order = {'red': 0, 'green': 1, 'blue': 2}
self.image = np.zeros((self.shape[0], self.shape[1], 3))
for color, var in self.multicolorvars.items():
channel = var.get() # determine which channel should be plotted as this color
... | configures the three color image according to the requested parameters
:return: nothing, just updates self.image |
def configure_singlecolor_image(self, scale=False):
# determine which channel to use
self.image = self.data[self.singlecolorvar.get()]
# scale the image by requested power
self.image = np.power(self.image, self.singlecolorpower.get())
# adjust the percentile thresholds... | configures the single color image according to the requested parameters
:return: nothing, just updates self.image |
def updateArray(self, array, indices, value):
lin = np.arange(array.size)
new_array = array.flatten()
new_array[lin[indices]] = value
return new_array.reshape(array.shape) | updates array so that pixels at indices take on value
:param array: (m,n) array to adjust
:param indices: flattened image indices to change value
:param value: new value to assign
:return: the changed (m,n) array |
def onlasso(self, verts):
p = path.Path(verts)
ind = p.contains_points(self.pix, radius=1)
self.history.append(self.selection_array.copy())
self.selection_array = self.updateArray(self.selection_array,
ind,
... | Main function to control the action of the lasso, allows user to draw on data image and adjust thematic map
:param verts: the vertices selected by the lasso
:return: nothin, but update the selection array so lassoed region now has the selected theme, redraws canvas |
def onpress(self, event):
if event.key == 'c': # clears all the contours
for patch in self.region_patches:
patch.remove()
self.region_patches = []
self.fig.canvas.draw_idle()
elif event.key == "u": # undo a label
self.undobutton_... | Reacts to key commands
:param event: a keyboard event
:return: if 'c' is pressed, clear all region patches |
def make_options_frame(self):
self.tab_frame = ttk.Notebook(self.option_frame, width=800)
self.tab_configure = tk.Frame(self.tab_frame)
self.tab_classify = tk.Frame(self.tab_frame)
self.make_configure_tab()
self.make_classify_tab()
self.tab_frame.add(self.tab_co... | make the frame that allows for configuration and classification |
def disable_multicolor(self):
# disable the multicolor image
for color in ['red', 'green', 'blue']:
self.multicolorscales[color].config(state=tk.DISABLED, bg='grey')
self.multicolorframes[color].config(bg='grey')
self.multicolorlabels[color].config(bg='grey')... | swap from the multicolor image to the single color image |
def update_button_action(self):
if self.mode.get() == 3: # threecolor
self.configure_threecolor_image()
elif self.mode.get() == 1: # singlecolor
self.configure_singlecolor_image()
else:
raise ValueError("mode can only be singlecolor or threecolor")
... | when update button is clicked, refresh the data preview |
def make_configure_tab(self):
# Setup the choice between single and multicolor
modeframe = tk.Frame(self.tab_configure)
self.mode = tk.IntVar()
singlecolor = tk.Radiobutton(modeframe, text="Single color", variable=self.mode,
value=1, command=... | initial set up of configure tab |
def make_classify_tab(self):
self.pick_frame = tk.Frame(self.tab_classify)
self.pick_frame2 = tk.Frame(self.tab_classify)
self.solar_class_var = tk.IntVar()
self.solar_class_var.set(0) # initialize to unlabeled
buttonnum = 0
frame = [self.pick_frame, self.pick_... | initial set up of classification tab |
def undobutton_action(self):
if len(self.history) > 1:
old = self.history.pop(-1)
self.selection_array = old
self.mask.set_data(old)
self.fig.canvas.draw_idle() | when undo is clicked, revert the thematic map to the previous state |
def change_class(self):
self.toolbarcenterframe.config(text="Draw: {}".format(self.config.solar_class_name[self.solar_class_var.get()])) | "on changing the classification label, update the "draw" text |
def draw_circle(self, center, radius, array, value, mode="set"):
ri, ci = draw.circle(center[0], center[1],
radius=radius,
shape=array.shape)
if mode == "add":
array[ri, ci] += value
elif mode == "set":
ar... | Draws a circle of specified radius on the input array and fills it with specified value
:param center: a tuple for the center of the circle
:type center: tuple (x,y)
:param radius: how many pixels in radius the circle is
:type radius: int
:param array: image to draw circle on
... |
def draw_annulus(self, center, inner_radius, outer_radius, array, value, mode="set"):
if mode == "add":
self.draw_circle(center, outer_radius, array, value)
self.draw_circle(center, inner_radius, array, -value)
elif mode == "set":
ri, ci, existing = self.draw... | Draws an annulus of specified radius on the input array and fills it with specified value
:param center: a tuple for the center of the annulus
:type center: tuple (x,y)
:param inner_radius: how many pixels in radius the interior empty circle is, where the annulus begins
:type inner_radiu... |
def draw_default(self, inside=5, outside=15):
# fill everything with empty outer space
if 'outer_space' in self.config.solar_class_index:
self.selection_array[:, :] = self.config.solar_class_index['outer_space']
elif 'empty_outer_space' in self.config.solar_class_index:
... | Draw suggested sun disk, limb, and empty background
:param inside: how many pixels from the calculated solar disk edge to go inward for the limb
:param outside: how many pixels from the calculated solar disk edge to go outward for the limb
:return: updates the self.selection_array |
def values(self):
self.vals['nfft'] = self.ui.nfftSpnbx.value()
self.vals['window'] = str(self.ui.windowCmbx.currentText()).lower()
self.vals['overlap'] = self.ui.overlapSpnbx.value()
return self.vals | Gets the parameter values
:returns: dict of inputs:
| *'nfft'*: int -- length, in samples, of FFT chunks
| *'window'*: str -- name of window to apply to FFT chunks
| *'overlap'*: float -- percent overlap of windows |
def run(config, max_jobs, output=sys.stdout, job_type='local',
report_type='text', shell='/bin/bash', temp='.metapipe', run_now=False):
if max_jobs == None:
max_jobs = cpu_count()
parser = Parser(config)
try:
command_templates = parser.consume()
except ValueError as e:
... | Create the metapipe based on the provided input. |
def make_submit_job(shell, output, job_type):
run_cmd = [shell, output]
submit_command = Command(alias=PIPELINE_ALIAS, cmds=run_cmd)
submit_job = get_job(submit_command, job_type)
submit_job.make()
return submit_job | Preps the metapipe main job to be submitted. |
def yaml(modules_to_register: Iterable[Any] = None, classes_to_register: Iterable[Any] = None) -> ruamel.yaml.YAML:
# Defein a round-trip yaml object for us to work with. This object should be imported by other modules
# NOTE: "typ" is a not a typo. It stands for "type"
yaml = ruamel.yaml.YAML(typ = "r... | Create a YAML object for loading a YAML configuration.
Args:
modules_to_register: Modules containing classes to be registered with the YAML object. Default: None.
classes_to_register: Classes to be registered with the YAML object. Default: None.
Returns:
A newly creating YAML object, co... |
def register_classes(yaml: ruamel.yaml.YAML, classes: Optional[Iterable[Any]] = None) -> ruamel.yaml.YAML:
# Validation
if classes is None:
classes = []
# Register the classes
for cls in classes:
logger.debug(f"Registering class {cls} with YAML")
yaml.register_class(cls)
... | Register externally defined classes. |
def register_module_classes(yaml: ruamel.yaml.YAML, modules: Optional[Iterable[Any]] = None) -> ruamel.yaml.YAML:
# Validation
if modules is None:
modules = []
# Extract the classes from the modules
classes_to_register = set()
for module in modules:
module_classes = [member[1] ... | Register all classes in the given modules with the YAML object.
This is a simple helper function. |
def numpy_to_yaml(representer: Representer, data: np.ndarray) -> Sequence[Any]:
return representer.represent_sequence(
"!numpy_array",
data.tolist()
) | Write a numpy array to YAML.
It registers the array under the tag ``!numpy_array``.
Use with:
.. code-block:: python
>>> yaml = ruamel.yaml.YAML()
>>> yaml.representer.add_representer(np.ndarray, yaml.numpy_to_yaml)
Note:
We cannot use ``yaml.register_class`` because it won'... |
def numpy_from_yaml(constructor: Constructor, data: ruamel.yaml.nodes.SequenceNode) -> np.ndarray:
# Construct the contained values so that we properly construct int, float, etc.
# We just leave this to YAML because it already stores this information.
values = [constructor.construct_object(n) for n in ... | Read an array from YAML to numpy.
It reads arrays registered under the tag ``!numpy_array``.
Use with:
.. code-block:: python
>>> yaml = ruamel.yaml.YAML()
>>> yaml.constructor.add_constructor("!numpy_array", yaml.numpy_from_yaml)
Note:
We cannot use ``yaml.register_class`` ... |
def enum_to_yaml(cls: Type[T_EnumToYAML], representer: Representer, data: T_EnumToYAML) -> ruamel.yaml.nodes.ScalarNode:
return representer.represent_scalar(
f"!{cls.__name__}",
f"{str(data)}"
) | Encodes YAML representation.
This is a mixin method for writing enum values to YAML. It needs to be added to the enum
as a classmethod. See the module docstring for further information on this approach and how
to implement it.
This method writes whatever is used in the string representation of the YAM... |
def enum_from_yaml(cls: Type[T_EnumFromYAML], constructor: Constructor, node: ruamel.yaml.nodes.ScalarNode) -> T_EnumFromYAML:
# mypy doesn't like indexing to construct the enumeration.
return cls[node.value] | Decode YAML representation.
This is a mixin method for reading enum values from YAML. It needs to be added to the enum
as a classmethod. See the module docstring for further information on this approach and how
to implement it.
Note:
This method assumes that the name of the enumeration value w... |
def is_error(self):
try:
if self._task.is_alive():
if len(self._task.stderr.readlines()) > 0:
self._task.join()
self._write_log()
return True
except AttributeError:
pass
return False | Checks to see if the job errored out. |
def _get_current_ids(self, source=True, meta=True, spectra=True, spectra_annotation=True):
# get the cursor for the database connection
c = self.c
# Get the last uid for the spectra_info table
if source:
c.execute('SELECT max(id) FROM library_spectra_source')
... | Get the current id for each table in the database
Args:
source (boolean): get the id for the table "library_spectra_source" will update self.current_id_origin
meta (boolean): get the id for the table "library_spectra_meta" will update self.current_id_meta
spectra (boolean): ... |
def _parse_files(self, msp_pth, chunk, db_type, celery_obj=False):
if os.path.isdir(msp_pth):
c = 0
for folder, subs, files in sorted(os.walk(msp_pth)):
for msp_file in sorted(files):
msp_file_pth = os.path.join(folder, msp_file)
... | Parse the MSP files and insert into database
Args:
msp_pth (str): path to msp file or directory [required]
db_type (str): The type of database to submit to (either 'sqlite', 'mysql' or 'django_mysql') [required]
chunk (int): Chunks of spectra to parse data (useful to control... |
def _parse_lines(self, f, chunk, db_type, celery_obj=False, c=0):
old = 0
for i, line in enumerate(f):
line = line.rstrip()
if i == 0:
old = self.current_id_meta
self._update_libdata(line)
if self.current_id_meta > old:
... | Parse the MSP files and insert into database
Args:
f (file object): the opened file object
db_type (str): The type of database to submit to (either 'sqlite', 'mysql' or 'django_mysql') [required]
chunk (int): Chunks of spectra to parse data (useful to control memory usage) [... |
def get_compound_ids(self):
cursor = self.conn.cursor()
cursor.execute('SELECT inchikey_id FROM metab_compound')
self.conn.commit()
for row in cursor:
if not row[0] in self.compound_ids:
self.compound_ids.append(row[0]) | Extract the current compound ids in the database. Updates the self.compound_ids list |
def _store_compound_info(self):
other_name_l = [name for name in self.other_names if name != self.compound_info['name']]
self.compound_info['other_names'] = ' <#> '.join(other_name_l)
if not self.compound_info['inchikey_id']:
self._set_inchi_pcc(self.compound_info['pubchem_... | Update the compound_info dictionary with the current chunk of compound details
Note that we use the inchikey as unique identifier. If we can't find an appropiate inchikey we just use
a random string (uuid4) suffixed with UNKNOWN |
def _store_meta_info(self):
# In the mass bank msp files, sometimes the precursor_mz is missing but we have the neutral mass and
# the precursor_type (e.g. adduct) so we can calculate the precursor_mz
if not self.meta_info['precursor_mz'] and self.meta_info['precursor_type'] and \
... | Update the meta dictionary with the current chunk of meta data details |
def _parse_spectra_annotation(self, line):
if re.match('^PK\$NUM_PEAK(.*)', line, re.IGNORECASE):
self.start_spectra_annotation = False
return
saplist = line.split()
sarow = (
self.current_id_spectra_annotation,
float(saplist[self.spectr... | Parse and store the spectral annotation details |
def _parse_spectra(self, line):
if line in ['\n', '\r\n', '//\n', '//\r\n', '', '//']:
self.start_spectra = False
self.current_id_meta += 1
self.collect_meta = True
return
splist = line.split()
if len(splist) > 2 and not self.ignore_addi... | Parse and store the spectral details |
def _set_inchi_pcc(self, in_str, pcp_type, elem):
if not in_str:
return 0
try:
pccs = pcp.get_compounds(in_str, pcp_type)
except pcp.BadRequestError as e:
print(e)
return 0
except pcp.TimeoutError as e:
print(e)
... | Check pubchem compounds via API for both an inchikey and any available compound details |
def _get_other_names(self, line):
m = re.search(self.compound_regex['other_names'][0], line, re.IGNORECASE)
if m:
self.other_names.append(m.group(1).strip()) | Parse and extract any other names that might be recorded for the compound
Args:
line (str): line of the msp file |
def _parse_meta_info(self, line):
if self.mslevel:
self.meta_info['ms_level'] = self.mslevel
if self.polarity:
self.meta_info['polarity'] = self.polarity
for k, regexes in six.iteritems(self.meta_regex):
for reg in regexes:
m = re.s... | Parse and extract all meta data by looping through the dictionary of meta_info regexs
updates self.meta_info
Args:
line (str): line of the msp file |
def _parse_compound_info(self, line):
for k, regexes in six.iteritems(self.compound_regex):
for reg in regexes:
if self.compound_info[k]:
continue
m = re.search(reg, line, re.IGNORECASE)
if m:
self.compo... | Parse and extract all compound data by looping through the dictionary of compound_info regexs
updates self.compound_info
Args:
line (str): line of the msp file |
def line(line_def, **kwargs):
def replace(s):
return "(%s)" % ansi.aformat(s.group()[1:], attrs=["bold", ])
return ansi.aformat(
re.sub('@.?', replace, line_def),
**kwargs) | Highlights a character in the line |
def try_and_error(*funcs):
def validate(value):
exc = None
for func in funcs:
try:
return func(value)
except (ValueError, TypeError) as e:
exc = e
raise exc
return validate | Apply multiple validation functions
Parameters
----------
``*funcs``
Validation functions to test
Returns
-------
function |
def validate_text(value):
possible_transform = ['axes', 'fig', 'data']
validate_transform = ValidateInStrings('transform', possible_transform,
True)
tests = [validate_float, validate_float, validate_str,
validate_transform, dict]
if isinstance... | Validate a text formatoption
Parameters
----------
value: see :attr:`psyplot.plotter.labelplotter.text`
Raises
------
ValueError |
def validate_none(b):
if isinstance(b, six.string_types):
b = b.lower()
if b is None or b == 'none':
return None
else:
raise ValueError('Could not convert "%s" to None' % b) | Validate that None is given
Parameters
----------
b: {None, 'none'}
None or string (the case is ignored)
Returns
-------
None
Raises
------
ValueError |
def validate_axiscolor(value):
validate = try_and_error(validate_none, validate_color)
possible_keys = {'right', 'left', 'top', 'bottom'}
try:
value = dict(value)
false_keys = set(value) - possible_keys
if false_keys:
raise ValueError("Wrong keys (%s)!" % (', '.join(... | Validate a dictionary containing axiscolor definitions
Parameters
----------
value: dict
see :attr:`psyplot.plotter.baseplotter.axiscolor`
Returns
-------
dict
Raises
------
ValueError |
def validate_cbarpos(value):
patt = 'sh|sv|fl|fr|ft|fb|b|r'
if value is True:
value = {'b'}
elif not value:
value = set()
elif isinstance(value, six.string_types):
for s in re.finditer('[^%s]+' % patt, value):
warn("Unknown colorbar position %s!" % s.group(), Run... | Validate a colorbar position
Parameters
----------
value: bool or str
A string can be a combination of 'sh|sv|fl|fr|ft|fb|b|r'
Returns
-------
list
list of strings with possible colorbar positions
Raises
------
ValueError |
def validate_cmap(val):
from matplotlib.colors import Colormap
try:
return validate_str(val)
except ValueError:
if not isinstance(val, Colormap):
raise ValueError(
"Could not find a valid colormap!")
return val | Validate a colormap
Parameters
----------
val: str or :class:`mpl.colors.Colormap`
Returns
-------
str or :class:`mpl.colors.Colormap`
Raises
------
ValueError |
def validate_cmaps(cmaps):
cmaps = {validate_str(key): validate_colorlist(val) for key, val in cmaps}
for key, val in six.iteritems(cmaps):
cmaps.setdefault(key + '_r', val[::-1])
return cmaps | Validate a dictionary of color lists
Parameters
----------
cmaps: dict
a mapping from a colormap name to a list of colors
Raises
------
ValueError
If one of the values in `cmaps` is not a color list
Notes
-----
For all items (listname, list) in `cmaps`, the reverse... |
def validate_lineplot(value):
if value is None:
return value
elif isinstance(value, six.string_types):
return six.text_type(value)
else:
value = list(value)
for i, v in enumerate(value):
if v is None:
pass
elif isinstance(v, six.st... | Validate the value for the LinePlotter.plot formatoption
Parameters
----------
value: None, str or list with mixture of both
The value to validate |
def validate_err_calc(val):
try:
val = validate_float(val)
except (ValueError, TypeError):
pass
else:
if val <= 100 and val >= 0:
return val
raise ValueError("Percentiles for the error calculation must lie "
"between 0 and 100, not %s... | Validation function for the
:attr:`psy_simple.plotter.FldmeanPlotter.err_calc` formatoption |
def visit_GpxModel(self, gpx_model, *args, **kwargs):
result = OrderedDict()
put_scalar = lambda name, json_name=None: self.optional_attribute_scalar(result, gpx_model, name, json_name)
put_list = lambda name, json_name=None: self.optional_attribute_list(result, gpx_model, name, json_n... | Render a GPXModel as a single JSON structure. |
def visit_Metadata(self, metadata, *args, **kwargs):
result = OrderedDict()
put_scalar = lambda name, json_name=None: self.optional_attribute_scalar(result, metadata, name, json_name)
put_list = lambda name, json_name=None: self.optional_attribute_list(result, metadata, name, json_name)... | Render GPX Metadata as a single JSON structure. |
def has_option(section, name):
cfg = ConfigParser.SafeConfigParser({"working_dir": "/tmp", "debug": "0"})
cfg.read(CONFIG_LOCATIONS)
return cfg.has_option(section, name) | Wrapper around ConfigParser's ``has_option`` method. |
def get(section, name):
cfg = ConfigParser.SafeConfigParser({"working_dir": "/tmp", "debug": "0"})
cfg.read(CONFIG_LOCATIONS)
val = cfg.get(section, name)
return val.strip("'").strip('"') | Wrapper around ConfigParser's ``get`` method. |
def run(**options):
with Dotfile(options) as conf:
if conf['context'] is None:
msg = "No context file has been provided"
LOGGER.error(msg)
raise RuntimeError(msg)
if not os.path.exists(conf['context_path']):
msg = "Context file {} not found".forma... | _run_
Run the dockerstache process to render templates
based on the options provided
If extend_context is passed as options it will be used to
extend the context with the contents of the dictionary provided
via context.update(extend_context) |
def make_key(table_name, objid):
key = datastore.Key()
path = key.path_element.add()
path.kind = table_name
path.name = str(objid)
return key | Create an object key for storage. |
def write_rec(table_name, objid, data, index_name_values):
with DatastoreTransaction() as tx:
entity = tx.get_upsert()
entity.key.CopyFrom(make_key(table_name, objid))
prop = entity.property.add()
prop.name = 'id'
prop.value.string_value = objid
prop = entity.... | Write (upsert) a record using a tran. |
def extract_entity(found):
obj = dict()
for prop in found.entity.property:
obj[prop.name] = prop.value.string_value
return obj | Copy found entity to a dict. |
def read_rec(table_name, objid):
req = datastore.LookupRequest()
req.key.extend([make_key(table_name, objid)])
for found in datastore.lookup(req).found:
yield extract_entity(found) | Generator that yields keyed recs from store. |
def read_by_indexes(table_name, index_name_values=None):
req = datastore.RunQueryRequest()
query = req.query
query.kind.add().name = table_name
if not index_name_values:
index_name_values = []
for name, val in index_name_values:
queryFilter = query.filter.property_filter
... | Index reader. |
def delete_table(table_name):
to_delete = [
make_key(table_name, rec['id'])
for rec in read_by_indexes(table_name, [])
]
with DatastoreTransaction() as tx:
tx.get_commit_req().mutation.delete.extend(to_delete) | Mainly for testing. |
def get_commit_req(self):
if not self.commit_req:
self.commit_req = datastore.CommitRequest()
self.commit_req.transaction = self.tx
return self.commit_req | Lazy commit request getter. |
def find_one(self, cls, id):
db_result = None
for rec in read_rec(cls.get_table_name(), id):
db_result = rec
break # Only read the first returned - which should be all we get
if not db_result:
return None
obj = cls.from_data(db_result['value... | Required functionality. |
def find_all(self, cls):
final_results = []
for db_result in read_by_indexes(cls.get_table_name(), []):
obj = cls.from_data(db_result['value'])
final_results.append(obj)
return final_results | Required functionality. |
def find_by_index(self, cls, index_name, value):
table_name = cls.get_table_name()
index_name_vals = [(index_name, value)]
final_results = []
for db_result in read_by_indexes(table_name, index_name_vals):
obj = cls.from_data(db_result['value'])
final_res... | Required functionality. |
def save(self, obj):
if not obj.id:
obj.id = uuid()
index_names = obj.__class__.index_names() or []
index_dict = obj.indexes() or {}
index_name_values = [
(key, index_dict.get(key, ''))
for key in index_names
]
write_rec(
... | Required functionality. |
def call(command, stdin=None, stdout=subprocess.PIPE, env=os.environ, cwd=None,
shell=False, output_log_level=logging.INFO, sensitive_info=False):
if not sensitive_info:
logger.debug("calling command: %s" % command)
else:
logger.debug("calling command with sensitive information")
... | Better, smarter call logic |
def whitespace_smart_split(command):
return_array = []
s = ""
in_double_quotes = False
escape = False
for c in command:
if c == '"':
if in_double_quotes:
if escape:
s += c
escape = False
else:
... | Split a command by whitespace, taking care to not split on
whitespace within quotes.
>>> whitespace_smart_split("test this \\\"in here\\\" again")
['test', 'this', '"in here"', 'again'] |
def skip(stackframe=1):
def trace(frame, event, args):
raise ContextSkipped
sys.settrace(lambda *args, **kwargs: None)
frame = sys._getframe(stackframe + 1)
frame.f_trace = trace | Must be called from within `__enter__()`. Performs some magic to have a
#ContextSkipped exception be raised the moment the with context is entered.
The #ContextSkipped must then be handled in `__exit__()` to suppress the
propagation of the exception.
> Important: This function does not raise an exception by it... |
def sync(self):
phase = _get_phase(self._formula_instance)
self.logger.info("%s %s..." % (phase.verb.capitalize(), self.feature_name))
message = "...finished %s %s." % (phase.verb, self.feature_name)
result = getattr(self, phase.name)()
if result or phase in (PHASE.INSTA... | execute the steps required to have the
feature end with the desired state. |
def linear_insert(self, item, priority):
with self.lock:
self_data = self.data
rotate = self_data.rotate
maxlen = self._maxlen
length = len(self_data)
count = length
# in practice, this is better than doing a rotate(-1) every
# loop and getting self.dat... | Linear search. Performance is O(n^2). |
def binary_insert(self, item, priority):
with self.lock:
self_data = self.data
rotate = self_data.rotate
maxlen = self._maxlen
length = len(self_data)
index = 0
min = 0
max = length - 1
while max - min > 10:
mid = (min + max) // 2
... | Traditional binary search. Performance: O(n log n) |
def isloaded(self, name):
if name is None:
return True
if isinstance(name, str):
return (name in [x.__module__ for x in self])
if isinstance(name, Iterable):
return set(name).issubset([x.__module__ for x in self])
return False | Checks if given hook module has been loaded
Args:
name (str): The name of the module to check
Returns:
bool. The return code::
True -- Loaded
False -- Not Loaded |
def hook(self, function, dependencies=None):
if not isinstance(dependencies, (Iterable, type(None), str)):
raise TypeError("Invalid list of dependencies provided!")
# Tag the function with its dependencies
if not hasattr(function, "__deps__"):
function.__deps__ ... | Tries to load a hook
Args:
function (func): Function that will be called when the event is called
Kwargs:
dependencies (str): String or Iterable with modules whose hooks should be called before this one
Raises:
:class:TypeError
Note that the depend... |
def parse_from_json(json_str):
try:
message_dict = json.loads(json_str)
except ValueError:
raise ParseError("Mal-formed JSON input.")
upload_keys = message_dict.get('uploadKeys', False)
if upload_keys is False:
raise ParseError(
"uploadKeys does not exist. At mi... | Given a Unified Uploader message, parse the contents and return a
MarketOrderList or MarketHistoryList instance.
:param str json_str: A Unified Uploader message as a JSON string.
:rtype: MarketOrderList or MarketHistoryList
:raises: MalformedUploadError when invalid JSON is passed in. |
def encode_to_json(order_or_history):
if isinstance(order_or_history, MarketOrderList):
return orders.encode_to_json(order_or_history)
elif isinstance(order_or_history, MarketHistoryList):
return history.encode_to_json(order_or_history)
else:
raise Exception("Must be one of Mark... | Given an order or history entry, encode it to JSON and return.
:type order_or_history: MarketOrderList or MarketHistoryList
:param order_or_history: A MarketOrderList or MarketHistoryList instance to
encode to JSON.
:rtype: str
:return: The encoded JSON string. |
def event_subscriber(event):
def wrapper(method):
Registry.register_event(event.name, event, method)
return wrapper | Register a method, which gets called when this event triggers.
:param event: the event to register the decorator method on. |
def dispatch_event(event, subject='id'):
def wrapper(method):
def inner_wrapper(*args, **kwargs):
resource = method(*args, **kwargs)
if isinstance(resource, dict):
subject_ = resource.get(subject)
data = resource
else:
... | Dispatch an event when the decorated method is called.
:param event: the event class to instantiate and dispatch.
:param subject_property: the property name to get the subject. |
def add(self, classifier, threshold, begin=None, end=None):
boosted_machine = bob.learn.boosting.BoostedMachine()
if begin is None: begin = 0
if end is None: end = len(classifier.weak_machines)
for i in range(begin, end):
boosted_machine.add_weak_machine(classifier.weak_machines[i], classifie... | Adds a new strong classifier with the given threshold to the cascade.
**Parameters:**
classifier : :py:class:`bob.learn.boosting.BoostedMachine`
A strong classifier to add
``threshold`` : float
The classification threshold for this cascade step
``begin``, ``end`` : int or ``None``
... |
def create_from_boosted_machine(self, boosted_machine, classifiers_per_round, classification_thresholds=-5.):
indices = list(range(0, len(boosted_machine.weak_machines), classifiers_per_round))
if indices[-1] != len(boosted_machine.weak_machines): indices.append(len(boosted_machine.weak_machines))
self... | Creates this cascade from the given boosted machine, by simply splitting off strong classifiers that have classifiers_per_round weak classifiers.
**Parameters:**
``boosted_machine`` : :py:class:`bob.learn.boosting.BoostedMachine`
The strong classifier to split into a regular cascade.
``classifiers_... |
def generate_boosted_machine(self):
strong = bob.learn.boosting.BoostedMachine()
for machine, index in zip(self.cascade, self.indices):
weak = machine.weak_machines
weights = machine.weights
for i in range(len(weak)):
strong.add_weak_machine(weak[i], weights[i])
return strong | generate_boosted_machine() -> strong
Creates a single strong classifier from this cascade by concatenating all strong classifiers.
**Returns:**
``strong`` : :py:class:`bob.learn.boosting.BoostedMachine`
The strong classifier as a combination of all classifiers in this cascade. |
def save(self, hdf5):
# write the cascade to file
hdf5.set("Thresholds", self.thresholds)
for i in range(len(self.cascade)):
hdf5.create_group("Classifier_%d" % (i+1))
hdf5.cd("Classifier_%d" % (i+1))
self.cascade[i].save(hdf5)
hdf5.cd("..")
hdf5.create_group("FeatureExtract... | Saves this cascade into the given HDF5 file.
**Parameters:**
``hdf5`` : :py:class:`bob.io.base.HDF5File`
An HDF5 file open for writing |
def load(self, hdf5):
# write the cascade to file
self.thresholds = hdf5.read("Thresholds")
self.cascade = []
for i in range(len(self.thresholds)):
hdf5.cd("Classifier_%d" % (i+1))
self.cascade.append(bob.learn.boosting.BoostedMachine(hdf5))
hdf5.cd("..")
hdf5.cd("FeatureExtra... | Loads this cascade from the given HDF5 file.
**Parameters:**
``hdf5`` : :py:class:`bob.io.base.HDF5File`
An HDF5 file open for reading |
def check(ctx, repository, config):
ctx.obj = Repo(repository=repository, config=config) | Check commits. |
def message(obj, commit='HEAD', skip_merge_commits=False):
from ..kwalitee import check_message
options = obj.options
repository = obj.repository
if options.get('colors') is not False:
colorama.init(autoreset=True)
reset = colorama.Style.RESET_ALL
yellow = colorama.Fore.YEL... | Check the messages of the commits. |
def get_obj_subcmds(obj):
subcmds = []
for label in dir(obj.__class__):
if label.startswith("_"):
continue
if isinstance(getattr(obj.__class__, label, False), property):
continue
rvalue = getattr(obj, label)
if not callable(rvalue) or not is_cmd(rvalu... | Fetch action in callable attributes which and commands
Callable must have their attribute 'command' set to True to
be recognised by this lookup.
Please consider using the decorator ``@cmd`` to declare your
subcommands in classes for instance. |
def get_module_resources(mod):
path = os.path.dirname(os.path.realpath(mod.__file__))
prefix = kf.basename(mod.__file__, (".py", ".pyc"))
if not os.path.exists(mod.__file__):
import pkg_resources
for resource_name in pkg_resources.resource_listdir(mod.__name__, ''):
if res... | Return probed sub module names from given module |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.