_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q10800 | Base.to_dict | train | def to_dict(self):
"""
Convert attributes and properties to a dict,
so that it can be serialized.
"""
| python | {
"resource": ""
} |
q10801 | _HtmlHeaderNode.to_dict | train | def to_dict(self):
"""Convert self to a dict object for serialization."""
return {
'level': self.level,
'id': self.id,
'text': self.text,
| python | {
"resource": ""
} |
q10802 | HtmlTocParser.toc | train | def toc(self, depth=6, lowest_level=6):
"""
Get table of content of currently fed HTML string.
:param depth: the depth of TOC
:param lowest_level: the allowed lowest level of header tag
:return: a list representing the TOC
"""
depth = min(max(depth, 0), 6)
depth = 6 if depth == 0 else depth
lowest_level = min(max(lowest_level, 1), 6)
toc = self._root.to_dict()['children']
def traverse(curr_toc, dep, lowest_lvl, curr_depth=1):
if curr_depth > dep:
# clear all items of this depth and exit the recursion
curr_toc.clear()
return
items_to_remove = []
for item in curr_toc:
if item['level'] > lowest_lvl:
| python | {
"resource": ""
} |
q10803 | HtmlTocParser.toc_html | train | def toc_html(self, depth=6, lowest_level=6):
"""
Get TOC of currently fed HTML string in form of HTML string.
:param depth: the depth of TOC
:param lowest_level: the allowed lowest level of header tag
:return: an HTML string
"""
toc = self.toc(depth=depth, lowest_level=lowest_level)
if not toc:
return ''
def map_toc_list(toc_list):
result = ''
if toc_list:
result += '<ul>\n'
result += ''.join(
map(lambda x: '<li>'
'<a href="#{}">{}</a>{}'
| python | {
"resource": ""
} |
q10804 | HtmlTocParser._get_level | train | def _get_level(tag):
"""
Match the header level in the given tag name,
or None if it's not a header tag.
"""
| python | {
"resource": ""
} |
q10805 | MVisionProcess.requiredGPU_MB | train | def requiredGPU_MB(self, n):
"""Required GPU memory in MBytes
"""
from darknet.core import darknet_with_cuda
if (darknet_with_cuda()): # its using cuda
free = getFreeGPU_MB()
print("Yolo: requiredGPU_MB: required, free", n, free)
| python | {
"resource": ""
} |
q10806 | Inspect.typename | train | def typename(self):
'''
get the type of val
there are multiple places where we want to know if val is an object, or a string, or whatever,
this method allows us to find out that information
since -- 7-10-12
val -- mixed -- the value to check
return -- string -- the type
'''
t = 'DEFAULT'
# http://docs.python.org/2/library/types.html
# func_types = (
# types.FunctionType,
# types.BuiltinFunctionType,
# types.MethodType,
# types.UnboundMethodType,
# types.BuiltinFunctionType,
# types.BuiltinMethodType,
# classmethod
# )
if self.is_primitive():
t = 'DEFAULT'
elif self.is_dict():
t = 'DICT'
elif self.is_list():
t = 'LIST'
elif self.is_array():
t = 'ARRAY'
elif self.is_tuple():
t = | python | {
"resource": ""
} |
q10807 | Inspect.is_primitive | train | def is_primitive(self):
"""is the value a built-in type?"""
if is_py2:
return isinstance(
self.val,
(
types.NoneType,
types.BooleanType,
types.IntType,
types.LongType,
types.FloatType
| python | {
"resource": ""
} |
q10808 | Value._str_iterator | train | def _str_iterator(self, iterator, name_callback=None, prefix="\n", left_paren='[', right_paren=']', depth=0):
'''
turn an iteratable value into a string representation
iterator -- iterator -- the value to be iterated through
name_callback -- callback -- if not None, a function that will take the key of each iteration
prefix -- string -- what will be prepended to the generated value
left_paren -- string -- what will open the generated value
right_paren -- string -- what will close the generated value
depth -- integer -- how deep into recursion we are
return -- string
'''
indent = 1 if depth > 0 else 0
s = []
s.append('{}{}'.format(prefix, self._add_indent(left_paren, indent)))
s_body = []
for k, v in iterator:
k = k if name_callback is None else name_callback(k)
v = Value(v, depth+1)
try:
# TODO -- right here we should check some flag or something to
| python | {
"resource": ""
} |
q10809 | ObjectValue._get_src_file | train | def _get_src_file(self, val, default='Unknown'):
'''
return the source file path
since -- 7-19-12
val -- mixed -- the value whose path you want
return -- string -- the path, or something like 'Unknown' if you can't find the path
'''
path = default
try:
# http://stackoverflow.com/questions/6761337/inspect-getfile-vs-inspect-getsourcefile
# first try and get the actual source file
source_file = inspect.getsourcefile(val)
if not source_file:
# get the raw file since | python | {
"resource": ""
} |
q10810 | handle_decode_replace | train | def handle_decode_replace(e):
"""this handles replacing bad characters when printing out
http://www.programcreek.com/python/example/3643/codecs.register_error
http://bioportal.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/codecs/index.html
| python | {
"resource": ""
} |
q10811 | MovementDetector.reset | train | def reset(self):
"""Reset analyzer state
"""
self.prevframe = None
| python | {
"resource": ""
} |
q10812 | rule | train | def rule(rules, strict_slashes=False, api_func=None, *args, **kwargs):
"""
Add a API route to the 'api' blueprint.
:param rules: rule string or string list
:param strict_slashes: same to Blueprint.route, but default value is False
:param api_func: a function that returns a JSON serializable object
or a Flask Response, or raises ApiException
| python | {
"resource": ""
} |
q10813 | GPUHandler.findXScreens | train | def findXScreens(self):
qapp = QtCore.QCoreApplication.instance()
if not qapp: # QApplication has not been started
return
screens = qapp.screens()
"""
let's find out which screens are virtual
screen, siblings:
One big virtual desktop:
A [A, B, C]
B [A, B, C]
C [A, B, C]
A & B in one xscreen, C in another:
A [A, B]
B [A, B]
C [C]
"""
virtual_screens = set()
for screen in screens: | python | {
"resource": ""
} |
q10814 | MyGui.generateMethods | train | def generateMethods(self):
"""Generate some member functions
"""
for i in range(1, 5):
# adds member function grid_ixi_slot(self)
| python | {
"resource": ""
} |
q10815 | MyGui.QCapsulate | train | def QCapsulate(self, widget, name, blocking = False, nude = False):
"""Helper function that encapsulates QWidget into a QMainWindow
"""
class QuickWindow(QtWidgets.QMainWindow):
class Signals(QtCore.QObject):
close = QtCore.Signal()
show = QtCore.Signal()
def __init__(self, blocking = False, parent = None, nude = False):
super().__init__(parent)
self.propagate = True # send signals or not
self.setStyleSheet(style.main_gui)
if (blocking):
self.setWindowModality(QtCore.Qt.ApplicationModal)
if (nude):
# http://doc.qt.io/qt-5/qt.html#WindowType-enum
# TODO: create a widget for a proper splashscreen (omitting X11 and centering manually)
# self.setWindowFlags(QtCore.Qt.Popup) # Qt 5.9+ : setFlags()
# self.setWindowFlags(QtCore.Qt.SplashScreen | QtCore.Qt.WindowStaysOnTopHint)
self.setWindowFlags(QtCore.Qt.Dialog)
self.signals = self.Signals()
| python | {
"resource": ""
} |
q10816 | MyGui.make_grid_slot | train | def make_grid_slot(self, n, m):
"""Create a n x m video grid, show it and add it to the list of video containers
"""
def slot_func():
cont = container.VideoContainerNxM(gpu_handler=self.gpu_handler,
| python | {
"resource": ""
} |
q10817 | main_inject | train | def main_inject(args):
"""
mapped to pout.inject on the command line, makes it easy to make pout global
without having to actually import it in your python environment
.. since:: 2018-08-13
:param args: Namespace, the parsed CLI arguments passed into the application
:returns: int, the return code of the CLI
"""
ret = 0
try:
filepath = SiteCustomizeFile()
if filepath.is_injected():
logger.info("Pout has already been injected into {}".format(filepath))
else:
| python | {
"resource": ""
} |
q10818 | main_info | train | def main_info(args):
"""Just prints out info about the pout installation
.. since:: 2018-08-20
:param args: Namespace, the parsed CLI arguments passed into the application
:returns: int, the return code of the CLI
"""
if args.site_packages:
logger.info(SitePackagesDir())
else:
logger.info("Python executable: {}".format(sys.executable))
logger.info("Python version: {}".format(platform.python_version()))
logger.info("Python site-packages: {}".format(SitePackagesDir()))
logger.info("Python sitecustomize: {}".format(SiteCustomizeFile()))
# https://stackoverflow.com/questions/4152963/get-the-name-of-current-script-with-python
| python | {
"resource": ""
} |
q10819 | catch | train | def catch(func, *args, **kwargs):
"""
Call the supplied function with the supplied arguments,
catching and returning any exception that it throws.
Arguments:
func: the function to run.
*args: positional arguments to pass into the function.
**kwargs: keyword arguments to pass into the function.
Returns:
If | python | {
"resource": ""
} |
q10820 | time | train | def time(func, *args, **kwargs):
"""
Call the supplied function with the supplied arguments,
and return the total execution time as a float in seconds.
The precision of the returned value depends on the precision of
`time.time()` on your platform.
Arguments:
func: the function to run.
*args: positional arguments to pass into the function.
| python | {
"resource": ""
} |
q10821 | LmomDistrMixin.lmom_fit | train | def lmom_fit(self, data=[], lmom_ratios=[]):
"""
Fit the distribution function to the given data or given L-moments.
:param data: Data to use in calculating the distribution parameters
:type data: array_like
:param lmom_ratios: L-moments (ratios) l1, l2, t3, t4, .. to use in calculating the distribution parameters
:type lmom_ratios: array_like
:returns: Distribution parameters in `scipy` order, e.g. scale, loc, shape
:rtype: :class:`OrderedDict`
"""
n_min = self.numargs + 2
if len(data) > 0:
if len(data) <= n_min:
| python | {
"resource": ""
} |
q10822 | run | train | def run():
"""
Run all the test classes in the main module.
Returns: exit code as an integer.
The default behaviour (which may be overridden by plugins) is to return a 0
exit code if the test run succeeded, and 1 if it failed.
"""
plugin_list = | python | {
"resource": ""
} |
q10823 | run_with_plugins | train | def run_with_plugins(plugin_list):
"""
Carry out a test run with the supplied list of plugin instances.
The plugins are expected to identify the object to run.
Parameters:
plugin_list: a list of plugin instances (objects which implement some subset of PluginInterface)
Returns: exit code as an integer.
The default behaviour (which may be overridden by plugins) is to return a 0
| python | {
"resource": ""
} |
q10824 | lmom_ratios | train | def lmom_ratios(data, nmom=5):
"""
Estimate `nmom` number of L-moments from a sample `data`.
:param data: Sequence of (sample) data
:type data: list or array-like sequence
:param nmom: number of | python | {
"resource": ""
} |
q10825 | POEditorAPI._apiv1_run | train | def _apiv1_run(self, action, headers=None, **kwargs):
"""
Kept for backwards compatibility of this client
See "self.clear_reference_language"
"""
warnings.warn(
"POEditor API v1 is deprecated. Use POEditorAPI._run method to call API v2",
DeprecationWarning, stacklevel=2
)
| python | {
"resource": ""
} |
q10826 | POEditorAPI.list_projects | train | def list_projects(self):
"""
Returns the list of projects owned by user.
"""
data = self._run(
| python | {
"resource": ""
} |
q10827 | POEditorAPI.view_project_details | train | def view_project_details(self, project_id):
"""
Returns project's details.
"""
| python | {
"resource": ""
} |
q10828 | POEditorAPI.add_language_to_project | train | def add_language_to_project(self, project_id, language_code):
"""
Adds a new language to project
"""
self._run(
url_path="languages/add",
| python | {
"resource": ""
} |
q10829 | POEditorAPI.update_terms | train | def update_terms(self, project_id, data, fuzzy_trigger=None):
"""
Updates project terms. Lets you change the text, context, reference, plural and tags.
>>> data = [
{
"term": "Add new list",
"context": "",
"new_term": "Save list",
"new_context": "",
"reference": "\/projects",
"plural": "",
"comment": "",
"tags": [
"first_tag",
"second_tag"
]
},
{
| python | {
"resource": ""
} |
q10830 | POEditorAPI.update_terms_translations | train | def update_terms_translations(self, project_id, file_path=None,
language_code=None, overwrite=False,
sync_terms=False, tags=None, fuzzy_trigger=None):
"""
Updates terms translations
overwrite: set it to True if you want to overwrite translations
sync_terms: set it to True if you want to sync your terms (terms that
are not found in the uploaded file will be deleted from project
and the new ones added). Ignored if updating = translations
tags: Add tags to the project terms; available when updating terms or terms_translations;
| python | {
"resource": ""
} |
q10831 | POEditorAPI.list_contributors | train | def list_contributors(self, project_id=None, language_code=None):
"""
Returns the list of contributors
"""
data = self._run(
url_path="contributors/list",
| python | {
"resource": ""
} |
q10832 | POEditorAPI.remove_contributor | train | def remove_contributor(self, project_id, email, language):
"""
Removes a contributor
"""
| python | {
"resource": ""
} |
q10833 | parse_stations | train | def parse_stations(html):
"""
Strips JS code, loads JSON
"""
html = html.replace('SLs.sls=', | python | {
"resource": ""
} |
q10834 | parse_delay | train | def parse_delay(data):
"""
Prase the delay
"""
# parse data from the details view
rsp = requests.get(data['details'])
soup = BeautifulSoup(rsp.text, "html.parser")
# get departure delay
delay_departure_raw = soup.find('div', class_="routeStart").find('span', class_=["delay", "delayOnTime"])
if delay_departure_raw:
delay_departure = calculate_delay(data['departure'],
delay_departure_raw.text)
else:
delay_departure = 0
# get arrival delay
delay_arrival_raw = soup.find('div', class_=["routeEnd","routeEndAdditional"]).find('span', class_=["delay", "delayOnTime"])
if delay_arrival_raw:
delay_arrival = calculate_delay(data['arrival'],
| python | {
"resource": ""
} |
q10835 | calculate_delay | train | def calculate_delay(original, delay):
"""
Calculate the delay
"""
original = datetime.strptime(original, | python | {
"resource": ""
} |
q10836 | Schiene.stations | train | def stations(self, station, limit=10):
"""
Find stations for given queries
Args:
station (str): search query
limit (int): limit number of results
| python | {
"resource": ""
} |
q10837 | Schiene.connections | train | def connections(self, origin, destination, dt=datetime.now(), only_direct=False):
"""
Find connections between two stations
Args:
origin (str): origin station
destination (str): destination station
dt (datetime): date and time for query
only_direct (bool): only direct connections
"""
query = {
'S': origin,
'Z': destination,
'date': | python | {
"resource": ""
} |
q10838 | _scatter | train | def _scatter(sequence, n):
"""Scatters elements of ``sequence`` into ``n`` blocks."""
chunklen | python | {
"resource": ""
} |
q10839 | SuperTaskQueue.purge | train | def purge(self):
"""Deletes all tasks in the queue."""
try:
return self._api.purge()
except AttributeError:
while True:
lst = self.list()
if len(lst) == 0:
| python | {
"resource": ""
} |
q10840 | TaskQueue.delete | train | def delete(self, task_id):
"""Deletes a task from a TaskQueue."""
if isinstance(task_id, RegisteredTask):
task_id = task_id.id
def cloud_delete(api):
api.delete(task_id)
| python | {
"resource": ""
} |
q10841 | is_upload | train | def is_upload(action):
"""Checks if this should be a user upload
:param action:
:return: True if this is a file we intend to upload from the | python | {
"resource": ""
} |
q10842 | ArgParseNode.to_django | train | def to_django(self):
"""
This is a debug function to see what equivalent django models are being generated
"""
exclude = {'name', 'model'}
field_module = 'models'
django_kwargs = {}
if self.node_attrs['model'] == 'CharField':
django_kwargs['max_length'] = 255
| python | {
"resource": ""
} |
q10843 | str_dict_keys | train | def str_dict_keys(a_dict):
"""return a modified dict where all the keys that are anything but str get
converted to str.
E.g.
>>> result = str_dict_keys({u'name': u'Peter', u'age': 99, 1: 2})
>>> # can't compare whole dicts in doctests
>>> result['name']
u'Peter'
>>> result['age']
99
>>> result[1]
2
The reason for this is that in Python <= 2.6.4 doing
``MyClass(**{u'name': u'Peter'})`` would raise a TypeError
Note that only unicode types are converted to str types.
The reason for that is you might have a class that looks like this::
class Option(object):
def __init__(self, foo=None, bar=None, **kwargs):
...
And it's | python | {
"resource": ""
} |
q10844 | str_to_boolean | train | def str_to_boolean(input_str):
""" a conversion function for boolean
"""
if not isinstance(input_str, six.string_types): | python | {
"resource": ""
} |
q10845 | str_to_python_object | train | def str_to_python_object(input_str):
""" a conversion that will import a module and class name
"""
if not input_str:
return None
if six.PY3 and isinstance(input_str, six.binary_type):
input_str = to_str(input_str)
if not isinstance(input_str, six.string_types):
# gosh, we didn't get a string, we can't convert anything but strings
# we're going to assume that what we got is actually what was wanted
# as the output
return input_str
input_str = str_quote_stripper(input_str)
if '.' not in input_str and input_str in known_mapping_str_to_type:
| python | {
"resource": ""
} |
q10846 | str_to_classes_in_namespaces | train | def str_to_classes_in_namespaces(
template_for_namespace="cls%d",
name_of_class_option='cls',
instantiate_classes=False
):
"""take a comma delimited list of class names, convert each class name
into an actual class as an option within a numbered namespace. This
function creates a closure over a new function. That new function,
in turn creates a class derived from RequiredConfig. The inner function,
'class_list_converter', populates the InnerClassList with a Namespace for
each of the classes in the class list. In addition, it puts the each class
itself into the subordinate Namespace. The requirement discovery mechanism
of configman then reads the InnerClassList's requried config, pulling in
the namespaces and associated classes within.
For example, if we have a class list like this: "Alpha, Beta", then this
converter will add the following Namespaces and options to the
configuration:
"cls0" - the subordinate Namespace for Alpha
"cls0.cls" - the option containing the class Alpha itself
"cls1" - the subordinate Namespace for Beta
"cls1.cls" - the option containing the class Beta itself
Optionally, the 'class_list_converter' inner function can embue the
InnerClassList's subordinate namespaces with aggregates that will
instantiate classes from the class list. This is a convenience to the
programmer who would otherwise have to know ahead of time what the
namespace names were so that the classes could be instantiated within the
context of the correct namespace. Remember the user could completely
change the list of classes at run time, so prediction could be difficult.
"cls0" - the subordinate Namespace for Alpha
"cls0.cls" - the option containing the class Alpha itself
"cls0.cls_instance" - an instance of the class Alpha
"cls1" - the subordinate Namespace for Beta
"cls1.cls" - the option containing the class Beta itself
"cls1.cls_instance" - an instance of the class Beta
parameters:
template_for_namespace - a template for the names of the namespaces
that will contain the classes and their
associated required config options. The
namespaces will be numbered sequentially. By
default, they will be "cls1", "cls2", etc.
class_option_name - the name to be used for the class option within
the nested namespace. By default, it will choose:
"cls1.cls", "cls2.cls", etc.
instantiate_classes - a boolean to determine if there should be an
aggregator added to each namespace that
instantiates each class. If True, then each
Namespace will contain elements for the class, as
well as an aggregator that will instantiate the
class.
"""
# these are only used within this method. No need to pollute the module
# scope with them and avoid potential circular imports
from configman.namespace import Namespace
from configman.required_config import RequiredConfig
#--------------------------------------------------------------------------
def class_list_converter(class_list_str):
"""This function becomes the actual converter used by configman to
take a string and convert it into the nested sequence of Namespaces,
one for each class in the list. It does this by creating a proxy
class stuffed with its own 'required_config' that's dynamically
generated."""
if isinstance(class_list_str, six.string_types):
class_list = [x.strip() for x in class_list_str.split(',')]
if class_list == ['']:
class_list = []
else:
raise TypeError('must be derivative of %s' % six.string_types)
#======================================================================
class InnerClassList(RequiredConfig):
"""This nested class is a proxy list for the classes. It collects
all the config requirements for the listed classes and places them
each into their own Namespace.
"""
# we're dynamically creating a class here. The following block of
| python | {
"resource": ""
} |
q10847 | str_to_list | train | def str_to_list(
input_str,
item_converter=lambda x: x,
item_separator=',',
list_to_collection_converter=None,
):
""" a conversion function for list
"""
if not isinstance(input_str, six.string_types):
raise ValueError(input_str)
input_str = str_quote_stripper(input_str)
result = [
item_converter(x.strip())
| python | {
"resource": ""
} |
q10848 | arbitrary_object_to_string | train | def arbitrary_object_to_string(a_thing):
"""take a python object of some sort, and convert it into a human readable
string. this function is used extensively to convert things like "subject"
into "subject_key, function -> function_key, etc."""
# is it None?
if a_thing is None:
return ''
# is it already a string?
if isinstance(a_thing, six.string_types):
return a_thing
if six.PY3 and isinstance(a_thing, six.binary_type):
try:
return a_thing.decode('utf-8')
except UnicodeDecodeError:
pass
# does it have a to_str function?
try:
return a_thing.to_str()
except (AttributeError, KeyError, TypeError):
# AttributeError - no to_str function?
# KeyError - DotDict has no to_str?
# TypeError - problem converting
# nope, no to_str function
pass
# is this a type proxy?
try:
return arbitrary_object_to_string(a_thing.a_type)
except (AttributeError, KeyError, TypeError):
#
# nope, no a_type property
pass
# is it a built in?
try:
return known_mapping_type_to_str[a_thing]
except (KeyError, TypeError):
# nope, not a builtin
pass
# | python | {
"resource": ""
} |
q10849 | ConfigObjWithIncludes._expand_files | train | def _expand_files(self, file_name, original_path, indent=""):
"""This recursive function accepts a file name, opens the file and then
spools the contents of the file into a list, examining each line as it
does so. If it detects a line beginning with "+include", it assumes
the string immediately following is a file name. Recursing, the file
new file is openned and its contents are spooled into the accumulating
list."""
expanded_file_contents = []
with open(file_name) as f:
for a_line in f:
match = ConfigObjWithIncludes._include_re.match(a_line)
if match:
include_file = match.group(2)
include_file = os.path.join(
original_path,
| python | {
"resource": ""
} |
q10850 | ConfigObjWithIncludes._load | train | def _load(self, infile, configspec):
"""this overrides the original ConfigObj method of the same name. It
runs through the input file collecting lines into a list. When
completed, this method submits the list of lines to the super class'
function of the same name. ConfigObj proceeds, completely unaware
| python | {
"resource": ""
} |
q10851 | ValueSource.get_values | train | def get_values(self, config_manager, ignore_mismatches, obj_hook=DotDict):
"""Return a nested dictionary representing the values in the ini file.
In the case of this ValueSource implementation, both parameters are
dummies."""
if self.delayed_parser_instantiation:
try:
app = config_manager._get_option('admin.application')
source = "%s%s" % (app.value.app_name, file_name_extension)
self.config_obj = configobj.ConfigObj(source)
self.delayed_parser_instantiation = False
except | python | {
"resource": ""
} |
q10852 | ValueSource._write_ini | train | def _write_ini(source_dict, namespace_name=None, level=0, indent_size=4,
output_stream=sys.stdout):
"""this function prints the components of a configobj ini file. It is
recursive for outputing the nested sections of the ini file."""
options = [
value
for value in source_dict.values()
if isinstance(value, Option)
]
options.sort(key=lambda x: x.name)
indent_spacer = " " * (level * indent_size)
for an_option in options:
print("%s# %s" % (indent_spacer, an_option.doc),
file=output_stream)
option_value = to_str(an_option)
if an_option.reference_value_from:
print(
'%s# see "%s.%s" for the default or override it here' % (
indent_spacer,
an_option.reference_value_from,
an_option.name
),
file=output_stream
)
if an_option.likely_to_be_changed or an_option.has_changed:
option_format = '%s%s=%s\n'
else:
option_format = '%s#%s=%s\n'
if isinstance(option_value, six.string_types) and \
',' in option_value:
# quote lists unless they're already quoted
if option_value[0] not in '\'"':
option_value = '"%s"' % option_value
print(option_format % (indent_spacer, an_option.name,
option_value),
file=output_stream)
next_level = level + 1
namespaces = [
(key, value)
for key, value in source_dict.items()
if isinstance(value, Namespace)
]
namespaces.sort(key=ValueSource._namespace_reference_value_from_sort)
for key, namespace in namespaces:
next_level_spacer = " " * next_level * indent_size
| python | {
"resource": ""
} |
q10853 | configuration | train | def configuration(*args, **kwargs):
"""this function just instantiates a ConfigurationManager and returns
the configuration dictionary. It accepts all the same parameters as the
constructor for the ConfigurationManager class."""
try:
| python | {
"resource": ""
} |
q10854 | iteritems_breadth_first | train | def iteritems_breadth_first(a_mapping, include_dicts=False):
"""a generator that returns all the keys in a set of nested
Mapping instances. The keys take the form X.Y.Z"""
subordinate_mappings = []
for key, value in six.iteritems(a_mapping):
if isinstance(value, collections.Mapping):
subordinate_mappings.append((key, value))
if include_dicts:
yield key, value
else:
| python | {
"resource": ""
} |
q10855 | DotDict.keys_breadth_first | train | def keys_breadth_first(self, include_dicts=False):
"""a generator that returns all the keys in a set of nested
DotDict instances. The keys take the form X.Y.Z"""
namespaces = []
for key in self._key_order:
if isinstance(getattr(self, key), DotDict):
namespaces.append(key)
if include_dicts:
yield key
| python | {
"resource": ""
} |
q10856 | DotDict.assign | train | def assign(self, key, value):
"""an alternative method for assigning values to nested DotDict
instances. It accepts keys in the form of X.Y.Z. If any nested
DotDict instances don't yet exist, they will be created."""
key_split = key.split('.')
cur_dict = self
for k in key_split[:-1]:
try:
cur_dict = cur_dict[k]
except KeyError:
| python | {
"resource": ""
} |
q10857 | DotDict.parent | train | def parent(self, key):
"""when given a key of the form X.Y.Z, this method will return the
parent DotDict of the 'Z' key."""
parent_key = '.'.join(key.split('.')[:-1])
| python | {
"resource": ""
} |
q10858 | Option.set_default | train | def set_default(self, val, force=False):
"""this function allows a default to be set on an option that dosen't
have one. It is used when a base class defines an Option for use in
derived classes but cannot predict what value would useful to the
derived classes. This gives the derived classes the opportunity to
set a logical default appropriate for the derived class' context.
For example:
class A(RequiredConfig):
required_config = Namespace()
required_config.add_option(
'x',
default=None
)
class B(A):
A.required_config.x.set_default(68)
parameters:
val - the value for the default
force | python | {
"resource": ""
} |
q10859 | Option.copy | train | def copy(self):
"""return a copy"""
o = Option(
name=self.name,
default=self.default,
doc=self.doc,
from_string_converter=self.from_string_converter,
to_string_converter=self.to_string_converter,
value=self.value,
short_form=self.short_form,
exclude_from_print_conf=self.exclude_from_print_conf,
exclude_from_dump_conf=self.exclude_from_dump_conf,
| python | {
"resource": ""
} |
q10860 | ConfigurationManager.context | train | def context(self, mapping_class=DotDictWithAcquisition):
"""return a config as a context that calls close on every item when
it goes out of scope"""
config = None
try:
config = | python | {
"resource": ""
} |
q10861 | ConfigurationManager.output_summary | train | def output_summary(self, output_stream=sys.stdout):
"""outputs a usage tip and the list of acceptable commands.
This is useful as the output of the 'help' option.
parameters:
output_stream - an open file-like object suitable for use as the
target of a print function
"""
if self.app_name or self.app_description:
print('Application: ', end='', file=output_stream)
if self.app_name:
print(self.app_name, self.app_version, file=output_stream)
if self.app_description:
print(self.app_description, file=output_stream)
if self.app_name or self.app_description:
print('', file=output_stream)
names_list = self.get_option_names()
print(
"usage:\n%s [OPTIONS]... " % self.app_invocation_name,
end='', file=output_stream
)
bracket_count = 0
# this section prints the non-switch command line arguments
for key in names_list:
an_option = self.option_definitions[key]
if an_option.is_argument:
if an_option.default is None:
# there's no option, assume the user must set this
print(an_option.name, end='', file=output_stream)
elif (
inspect.isclass(an_option.value)
or inspect.ismodule(an_option.value)
):
# this is already set and it could have expanded, most
# likely this is a case where a sub-command has been
# loaded and we're looking to show the help for it.
# display show it as a constant already provided rather
# than as an option the user must provide
print(an_option.default, end='', file=output_stream)
else:
# this is an argument that the user may alternatively
# provide
print("[ %s" % an_option.name, end='', file=output_stream)
| python | {
"resource": ""
} |
q10862 | ConfigurationManager.write_conf | train | def write_conf(self, config_file_type, opener, skip_keys=None):
"""write a configuration file to a file-like object.
parameters:
config_file_type - a string containing a registered file type OR
a for_XXX module from the value_source
package. Passing in an string that is
unregistered will result in a KeyError
opener - a callable object or function that returns a file like
object that works as a context in a with statement."""
blocked_keys = self.keys_blocked_from_output
if skip_keys:
blocked_keys.extend(skip_keys)
if blocked_keys:
option_defs = self.option_definitions.safe_copy()
for a_blocked_key in blocked_keys:
try:
| python | {
"resource": ""
} |
q10863 | ConfigurationManager.log_config | train | def log_config(self, logger):
"""write out the current configuration to a log-like object.
parameters:
logger - a object that implements a method called 'info' with the
same semantics as the call to 'logger.info'"""
logger.info("app_name: %s", self.app_name)
logger.info("app_version: %s", self.app_version)
logger.info("current configuration:")
config = [(key, self.option_definitions[key].value)
for key in self.option_definitions.keys_breadth_first()
if key not in self.keys_blocked_from_output]
config.sort()
for key, val in config:
| python | {
"resource": ""
} |
q10864 | ConfigurationManager.get_option_names | train | def get_option_names(self):
"""returns a list of fully qualified option names.
returns:
a list of strings representing the Options in the source Namespace
list. Each item will be fully qualified with dot delimited
Namespace | python | {
"resource": ""
} |
q10865 | ConfigurationManager._create_reference_value_options | train | def _create_reference_value_options(self, keys, finished_keys):
"""this method steps through the option definitions looking for
alt paths. On finding one, it creates the 'reference_value_from' links
within the option definitions and populates it with copied options."""
# a set of known reference_value_from_links
set_of_reference_value_option_names = set()
for key in keys:
if key in finished_keys:
continue
an_option = self.option_definitions[key]
if an_option.reference_value_from:
fully_qualified_reference_name = '.'.join((
an_option.reference_value_from,
an_option.name
))
if fully_qualified_reference_name in keys:
continue # this referenced value has already been defined
# no need to repeat it - skip on to the next key
reference_option = an_option.copy()
reference_option.reference_value_from = None
reference_option.name = fully_qualified_reference_name
# wait, aren't we setting a fully qualified dotted name into
| python | {
"resource": ""
} |
q10866 | ConfigurationManager._overlay_expand | train | def _overlay_expand(self):
"""This method overlays each of the value sources onto the default
in each of the defined options. It does so using a breadth first
iteration, overlaying and expanding each level of the tree in turn.
As soon as no changes were made to any level, the loop breaks and the
work is done. The actual action of the overlay is to take the value
from the source and copy into the 'default' member of each Option
object.
"expansion" means converting an option value into its real type from
string. The conversion is accomplished by simply calling the
'set_value' method of the Option object. If the resultant type has its
own configuration options, bring those into the current namespace and
then proceed to overlay/expand those.
"""
new_keys_have_been_discovered = True # loop control, False breaks loop
finished_keys = set()
all_reference_values = {}
while new_keys_have_been_discovered: # loop until nothing more is done
# names_of_all_exsting_options holds a list of all keys in the
# option definitons in breadth first order using this form:
# [ 'x', 'y', 'z', 'x.a', 'x.b', 'z.a', 'z.b', 'x.a.j', 'x.a.k',
# 'x.b.h']
names_of_all_exsting_options = [
x for x
in self.option_definitions.keys_breadth_first()
if isinstance(self.option_definitions[x], Option)
]
new_keys_have_been_discovered = False # setup to break loop
# create alternate paths options
set_of_reference_value_option_names = \
self._create_reference_value_options(
names_of_all_exsting_options,
finished_keys
)
for a_ref_option_name in set_of_reference_value_option_names:
if a_ref_option_name not in all_reference_values:
all_reference_values[a_ref_option_name] = []
all_keys = list(set_of_reference_value_option_names) \
+ names_of_all_exsting_options
# previous versions of this method pulled the values from the
# values sources deeper within the following nested loops.
# that was not necessary and caused a lot of redundant work.
# the 'values_from_all_sources' now holds all the the values
# from each of the value sources.
values_from_all_sources = [
a_value_source.get_values(
self, # pass in the config_manager itself
True, # ignore mismatches
self.value_source_object_hook # build with this class
)
for a_value_source in self.values_source_list
]
# overlay process:
# fetch all the default values from the value sources before
# applying the from string conversions
for key in all_keys:
if key in finished_keys:
continue
#if not isinstance(an_option, Option):
# continue # aggregations and other types are ignored
# loop through all the value sources looking for values
# that match this current key.
if self.option_definitions[key].reference_value_from:
reference_value_from = (
self.option_definitions[key].reference_value_from
)
top_key = key.split('.')[-1]
self.option_definitions[key].default = (
self.option_definitions[reference_value_from]
[top_key].default
)
all_reference_values[
'.'.join((reference_value_from, top_key))
].append(
key
)
an_option = self.option_definitions[key]
if key in all_reference_values:
# make sure that this value gets propagated to keys
# even if the keys have already been overlaid
finished_keys -= set(
all_reference_values[key]
)
for val_src_dict in values_from_all_sources:
try:
# overlay the default with the new value from
# the value source. This assignment may come
# via acquisition, so the key given may not have
# been an exact match for what was returned.
an_option.has_changed = (
an_option.default != val_src_dict[key]
)
an_option.default = val_src_dict[key]
if key in all_reference_values:
# make sure that this value gets propagated to keys
# even if the keys have already been overlaid
finished_keys -= set(
all_reference_values[key]
)
except KeyError as x:
pass # okay, that source doesn't have this value
# expansion process:
# step through all the keys converting them to their proper
# types and bringing in any new keys in the process
for key in all_keys:
if key in finished_keys:
continue
# mark this key as having been seen and processed
finished_keys.add(key)
an_option = self.option_definitions[key]
#if not isinstance(an_option, Option):
# continue # aggregations, namespaces are ignored
# apply the from string conversion to make the real value
an_option.set_value(an_option.default)
# new values have been seen, don't let loop break
new_keys_have_been_discovered = True
try:
try:
# try to fetch new | python | {
"resource": ""
} |
q10867 | ConfigurationManager._check_for_mismatches | train | def _check_for_mismatches(self, known_keys):
"""check for bad options from value sources"""
for a_value_source in self.values_source_list:
try:
if a_value_source.always_ignore_mismatches:
continue
except AttributeError:
# ok, this values source doesn't have the concept
# always igoring mismatches, we won't tolerate mismatches
pass
# we want to fetch the keys from the value sources so that we can
# check for mismatches. Commandline value sources, are different,
# we never want to allow unmatched keys from the command line.
# By detecting if this value source is a command line source, we
# can employ the command line's own mismatch detection. The
# boolean 'allow_mismatches' controls application of the tollerance
# for mismatches.
if hasattr(a_value_source, 'command_line_value_source'):
allow_mismatches = False
else:
allow_mismatches = True
# make a set of all the keys from a value source in the form
# of strings like this: 'x.y.z'
value_source_mapping = a_value_source.get_values(
self,
allow_mismatches,
self.value_source_object_hook
)
value_source_keys_set = set([
k for k in
DotDict(value_source_mapping).keys_breadth_first()
])
# make a set of the keys that didn't match any of the known
# keys in the requirements
unmatched_keys = value_source_keys_set.difference(known_keys)
# some of the unmatched keys may actually be ok because the were
# used during acquisition.
# remove keys of the form 'y.z' if they match a known key of the
# form 'x.y.z'
| python | {
"resource": ""
} |
q10868 | ConfigurationManager._generate_config | train | def _generate_config(self, mapping_class):
"""This routine generates a copy of the DotDict based config"""
config = mapping_class()
self._walk_config_copy_values(
| python | {
"resource": ""
} |
q10869 | PGPooledTransaction.close | train | def close(self):
"""close all pooled connections"""
print("PGPooledTransaction - shutting down connection pool")
for name, conn in self.pool.iteritems():
| python | {
"resource": ""
} |
q10870 | find_action_name_by_value | train | def find_action_name_by_value(registry, target_action_instance):
"""the association of a name of an action class with a human readable
string is exposed externally only at the time of argument definitions.
This routine, when given a reference to argparse's internal action
registry and an action, will find that action and return the name under
which it was registered.
"""
| python | {
"resource": ""
} |
q10871 | get_args_and_values | train | def get_args_and_values(parser, an_action):
"""this rountine attempts to reconstruct the kwargs that were used in the
creation of an action object"""
args = inspect.getargspec(an_action.__class__.__init__).args
kwargs = dict(
(an_attr, getattr(an_action, an_attr))
for an_attr in args
if (
an_attr not in ('self', 'required')
and getattr(an_action, an_attr) is not None
)
)
action_name = find_action_name_by_value(
parser._optionals._registries,
| python | {
"resource": ""
} |
q10872 | SubparserFromStringConverter.add_namespace | train | def add_namespace(self, name, a_namespace):
"""as we build up argparse, the actions that define a subparser are
translated into configman options. Each of those options must be
tagged with the value of the subparse to which they correspond."""
# save a local copy of the namespace
self.namespaces[name] = a_namespace
# iterate through the namespace branding each of the options with the
| python | {
"resource": ""
} |
q10873 | ConfigmanSubParsersAction.add_parser | train | def add_parser(self, *args, **kwargs):
"""each time a subparser action is used to create a new parser object
we must save the original args & kwargs. In a later phase of
configman, we'll need to reproduce the subparsers exactly without
resorting to copying. We save the args & kwargs in the 'foreign_data'
section of the configman option that corresponds with the subparser
| python | {
"resource": ""
} |
q10874 | ArgumentParser.get_required_config | train | def get_required_config(self):
"""because of the exsistance of subparsers, the configman options
that correspond with argparse arguments are not a constant. We need
to produce a copy of the namespace rather than the actual embedded
namespace."""
required_config = Namespace()
# add current options to a copy of required config
for k, v in iteritems_breadth_first(self.required_config):
required_config[k] = v
# get any option found in any subparsers
try:
subparser_namespaces = (
self.configman_subparsers_option.foreign_data
.argparse.subprocessor_from_string_converter
)
subparsers = (
self._argparse_subparsers._configman_option.foreign_data
.argparse.subparsers
)
# each subparser needs to have its configman options set up
# in the subparser's configman option. This routine copies
| python | {
"resource": ""
} |
q10875 | ArgumentParser.add_subparsers | train | def add_subparsers(self, *args, **kwargs):
"""When adding a subparser, we need to ensure that our version of the
SubparserAction object is returned. We also need to create the
corresponding configman Option object for the subparser and pack it's
foreign data section with the original args & kwargs."""
kwargs['parser_class'] = self.__class__
kwargs['action'] = ConfigmanSubParsersAction
subparser_action = super(ArgumentParser, self).add_subparsers(
*args,
**kwargs
)
self._argparse_subparsers = subparser_action
if "dest" not in kwargs or kwargs['dest'] is None:
kwargs['dest'] = 'subcommand'
configman_name = kwargs['dest']
configman_default = None
configman_doc = kwargs.get('help', '')
subprocessor_from_string_converter = SubparserFromStringConverter()
configman_to_string = str
configman_is_argument = True
configman_not_for_definition = True
# it's finally time to create the configman Option object and add it
# to the required_config.
self.required_config.add_option(
name=configman_name,
default=configman_default,
doc=configman_doc,
from_string_converter=subprocessor_from_string_converter,
to_string_converter=configman_to_string,
is_argument=configman_is_argument,
| python | {
"resource": ""
} |
q10876 | sequence_to_string | train | def sequence_to_string(
a_list,
open_bracket_char='[',
close_bracket_char=']',
delimiter=", "
):
"""a dedicated function that turns a list into a comma delimited string
| python | {
"resource": ""
} |
q10877 | ValueSource.get_values | train | def get_values(self, config_manager, ignore_mismatches, obj_hook=DotDict):
"""This is the black sheep of the crowd of ValueSource implementations.
It needs to know ahead of time all of the parameters that it will need,
but we cannot give it. We may not know all the parameters because
not all classes may have been expanded yet. The two parameters allow
this ValueSource implementation to know what the parameters have
already been defined. The 'ignore_mismatches' parameter tells the
implementation if it can or cannot ignore extraneous commandline
options. The last time this function is called, it will be required
to test for illegal commandline options and respond accordingly.
Unlike many of the Value sources, this method cannot be "memoized".
The return result depends on an internal state within the parameter
'config_manager'. Any memoize decorator for this method would requrire
capturing that internal state in the memoize cache key.
"""
short_options_str, long_options_list = self.getopt_create_opts(
config_manager.option_definitions
)
try:
if ignore_mismatches:
fn = ValueSource.getopt_with_ignore
else:
fn = getopt.gnu_getopt
# here getopt looks through the command line arguments and
# consumes the defined switches. The things that are not
# consumed are then offered as the 'args' variable of the
# parent configuration_manager
getopt_options, config_manager.args = fn(self.argv_source,
short_options_str,
long_options_list)
except getopt.GetoptError as x: | python | {
"resource": ""
} |
q10878 | ChemometricsPLS_LDA._cummulativefit | train | def _cummulativefit(self, x, y):
"""
Measure the cumulative Regression sum of Squares for each individual component.
:param x: Data matrix to fit the PLS model.
:type x: numpy.ndarray, shape [n_samples, n_features]
:param y: Data matrix to fit the PLS model.
:type y: numpy.ndarray, shape [n_samples, n_features]
:return: dictionary object containing the total Regression Sum of Squares and the Sum of Squares
per components, for both the X and Y data blocks.
:rtype: dict
"""
if y.ndim == 1:
y = y.reshape(-1, 1)
if x.ndim == 1:
x = x.reshape(-1, 1)
xscaled = self.x_scaler.fit_transform(x)
yscaled = self.y_scaler.fit_transform(y)
ssx_comp = list()
ssy_comp = list()
# Obtain residual sum of squares for whole data set and per component
SSX = np.sum(xscaled ** 2)
SSY = np.sum(yscaled ** 2)
ssx_comp = list()
ssy_comp = list()
for curr_comp in range(1, | python | {
"resource": ""
} |
q10879 | _handle_zeros_in_scale | train | def _handle_zeros_in_scale(scale, copy=True):
"""
Makes sure that whenever scale is zero, we handle it correctly.
This happens in most scalers when we have constant features.
"""
# if we are fitting on 1D arrays, scale might be a scalar
if numpy.isscalar(scale):
if scale == .0:
| python | {
"resource": ""
} |
q10880 | ChemometricsScaler.fit | train | def fit(self, X, y=None):
"""
Compute the mean and standard deviation from a dataset to use in future scaling operations.
:param X: Data matrix to scale.
:type X: numpy.ndarray, shape [n_samples, n_features]
:param y: Passthrough for Scikit-learn ``Pipeline`` compatibility.
:type y: None
| python | {
"resource": ""
} |
q10881 | ChemometricsScaler.partial_fit | train | def partial_fit(self, X, y=None):
"""
Performs online computation of mean and standard deviation on X for later scaling.
All of X is processed as a single batch.
This is intended for cases when `fit` is
not feasible due to very large number of `n_samples`
or because X is read from a continuous stream.
The algorithm for incremental mean
and std is given in Equation 1.5a,b in Chan, Tony F., Gene H. Golub, and Randall J. LeVeque. "Algorithms
for computing the sample variance: Analysis and recommendations."
The American Statistician 37.3 (1983): 242-247
:param X: Data matrix to scale.
:type X: numpy.ndarray, shape [n_samples, n_features]
:param y: Passthrough for Scikit-learn ``Pipeline`` compatibility.
:type y: None
:return: Fitted object.
:rtype: pyChemometrics.ChemometricsScaler
"""
X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
warn_on_dtype=True, estimator=self, dtype=FLOAT_DTYPES)
# Even in the case of `with_mean=False`, we update the mean anyway
# This is needed for the incremental computation of the var
# See incr_mean_variance_axis and _incremental_mean_variance_axis
if sparse.issparse(X):
if self.with_mean:
raise ValueError(
"Cannot center sparse matrices: pass `with_mean=False` "
"instead. See docstring for motivation and alternatives.")
if self.with_std:
# First pass
if not hasattr(self, 'n_samples_seen_'):
self.mean_, self.var_ = mean_variance_axis(X, axis=0)
self.n_samples_seen_ = X.shape[0]
# Next passes
else:
self.mean_, self.var_, self.n_samples_seen_ = \
| python | {
"resource": ""
} |
q10882 | ChemometricsScaler.transform | train | def transform(self, X, y=None, copy=None):
"""
Perform standardization by centering and scaling using the parameters.
:param X: Data matrix to scale.
:type X: numpy.ndarray, shape [n_samples, n_features]
:param y: Passthrough for scikit-learn ``Pipeline`` compatibility.
:type y: None
:param bool copy: Copy the X matrix.
:return: Scaled version of the X data matrix.
:rtype: numpy.ndarray, shape [n_samples, n_features]
"""
| python | {
"resource": ""
} |
q10883 | ChemometricsScaler.inverse_transform | train | def inverse_transform(self, X, copy=None):
"""
Scale back the data to the original representation.
:param X: Scaled data matrix.
:type X: numpy.ndarray, shape [n_samples, n_features]
:param bool copy: Copy the X data matrix.
:return: X data matrix with the scaling operation reverted.
:rtype: numpy.ndarray, shape [n_samples, n_features]
"""
check_is_fitted(self, 'scale_')
copy = copy if copy is not None else self.copy
if sparse.issparse(X):
if self.with_mean:
raise ValueError(
"Cannot uncenter sparse matrices: pass `with_mean=False` "
"instead See docstring for motivation and alternatives.")
if not sparse.isspmatrix_csr(X):
X = X.tocsr()
| python | {
"resource": ""
} |
q10884 | _recurse_replace | train | def _recurse_replace(obj, key, new_key, sub, remove):
"""Recursive helper for `replace_by_key`"""
if isinstance(obj, list):
return [_recurse_replace(x, key, new_key, sub, remove) for x in obj]
| python | {
"resource": ""
} |
q10885 | replace_by_key | train | def replace_by_key(pif, key, subs, new_key=None, remove=False):
"""Replace values that match a key
Deeply traverses the pif object, looking for `key` and
replacing values in accordance with `subs`. If `new_key`
is set, the replaced values are assigned to that key. If
`remove` is `True`, the old `key` pairs are removed.
"""
if not new_key:
| python | {
"resource": ""
} |
q10886 | new_keypair | train | def new_keypair(key, value, ambig, unambig):
"""
Check new keypair against existing unambiguous dict
:param key: of pair
:param value: of pair
:param ambig: set of keys with ambig decoding
:param unambig: set of keys with unambig decoding
:return:
"""
if key in ambig:
| python | {
"resource": ""
} |
q10887 | add_child_ambig | train | def add_child_ambig(child_ambig, child_unambig, ambig, unambig):
"""
Add information about decodings of a child object
:param child_ambig: ambiguous set from child
:param child_unambig: unambiguous set from child
:param ambig: set of keys storing ambig decodings
:param unambig: dictionary storing unambiguous decodings
:return:
"""
| python | {
"resource": ""
} |
q10888 | get_client | train | def get_client(site=None):
"""Get a citrination client"""
if 'CITRINATION_API_KEY' not in environ:
raise ValueError("'CITRINATION_API_KEY' is not set as an environment variable")
if not site:
| python | {
"resource": ""
} |
q10889 | calculate_ideal_atomic_percent | train | def calculate_ideal_atomic_percent(pif):
"""
Calculates ideal atomic percents from a chemical formula string from a pif. Returns an appended pif with composition elements modified or added.
:param pif: a ChemicalSystem pif
:return: modified pif object
"""
if not isinstance(pif, ChemicalSystem):
return pif
if not pif.chemical_formula:
return pif
else:
expanded_formula_no_special_char = _expand_formula_(
pif.chemical_formula)
element_array = _create_emprical_compositional_array_(
expanded_formula_no_special_char)
appended_e_array = _add_atomic_percents_(element_array)
for e in appended_e_array:
# Checks if a Composition element decribing that element already
# exists.
if _get_element_in_pif_composition_(pif, e["symbol"]):
# If it exists, it removes the old Composition object, and
# inserts a new one with ideal atomic percent added.
in_pif = _get_element_in_pif_composition_(pif, e["symbol"])
| python | {
"resource": ""
} |
q10890 | calculate_ideal_weight_percent | train | def calculate_ideal_weight_percent(pif):
"""
Calculates ideal atomic weight percents from a chemical formula string from a pif. Returns an appended pif with composition elements modified or added.
:param pif: a ChemicalSystem pif
:return: modified pif object
"""
if not isinstance(pif, ChemicalSystem):
return pif
if not pif.chemical_formula:
return pif
else:
expanded_formula_no_special_char = _expand_formula_(
pif.chemical_formula)
element_array = _create_emprical_compositional_array_(
expanded_formula_no_special_char)
appended_e_array = _add_ideal_atomic_weights_(element_array)
a_array_with_pcts = _add_ideal_weight_percent_(appended_e_array)
for e in a_array_with_pcts:
# Checks if a Composition element decribing that element already
# exists.
if _get_element_in_pif_composition_(pif, e["symbol"]):
# If it exists, it removes the old Composition object, and
# inserts a new one with ideal atomic weight percent added
in_pif = _get_element_in_pif_composition_(pif, e["symbol"])
| python | {
"resource": ""
} |
q10891 | _expand_hydrate_ | train | def _expand_hydrate_(hydrate_pos, formula_string):
"""
Handles the expansion of hydrate portions of a chemical formula, and expands out the coefficent to all elements
:param hydrate_pos: the index in the formula_string of the · symbol
:param formula_string: the unexpanded formula string
:return: a formula string without the · character with the hydrate portion expanded out
"""
hydrate = formula_string[hydrate_pos + 1:]
hydrate_string = ""
multiplier = float(re.search(r'^[\d\.]+', hydrate).group())
element_array = re.findall('[A-Z][^A-Z]*', hydrate)
for e in element_array:
occurance_array = re.findall('[0-9][^0-9]*', e)
if len(occurance_array) == 0:
occurance_array.append(1)
for o in occurance_array:
| python | {
"resource": ""
} |
q10892 | _create_compositional_array_ | train | def _create_compositional_array_(expanded_chemical_formaula_string):
"""
Splits an expanded chemical formula string into an array of dictionaries containing information about each element
:param expanded_chemical_formaula_string: a clean (not necessarily emperical, but without any special characters) chemical formula string, as returned by _expand_formula_()
| python | {
"resource": ""
} |
q10893 | _add_ideal_atomic_weights_ | train | def _add_ideal_atomic_weights_(elemental_array):
"""
Uses elements.json to find the molar mass of the element in question, and then multiplies that by the occurances of the element.
Adds the "weight" property to each of the dictionaries in elemental_array
:param elemental_array: an array of dictionaries containing information about the elements in the system
:return: the appended elemental_array
"""
| python | {
"resource": ""
} |
q10894 | _add_ideal_weight_percent_ | train | def _add_ideal_weight_percent_(elemental_array):
"""
Adds the "weight_percent" property to each of the dictionaries in elemental_array
:param elemental_array: an array of dictionaries containing information about the elements in the system
:return: the appended elemental_array
| python | {
"resource": ""
} |
q10895 | _get_element_in_pif_composition_ | train | def _get_element_in_pif_composition_(pif, elemental_symbol):
"""
If the element in question if in the composition array in the pif, it returns that Composition object and the position in the composition array otherwise it returns False
:param pif: ChemicalSystem Pif in question
:param elemental_symbol: string of the atomic symbol of the element in question
:return: either False if not found in the composition or the Compositional object along with its index in the composition array in the | python | {
"resource": ""
} |
q10896 | parse_name_string | train | def parse_name_string(full_name):
"""
Parse a full name into a Name object
:param full_name: e.g. "John Smith" or "Smith, John"
:return: Name object
"""
name = Name()
if "," in full_name:
toks = full_name.split(",")
name.family = toks[0]
| python | {
"resource": ""
} |
q10897 | query_to_mdf_records | train | def query_to_mdf_records(query=None, dataset_id=None, mdf_acl=None):
"""Evaluate a query and return a list of MDF records
If a datasetID is specified by there is no query, a simple
whole dataset query is formed for the user
"""
if not query and not dataset_id:
raise ValueError("Either query or dataset_id must be specified")
if query and dataset_id:
raise ValueError("Both query and dataset_id were specified; pick one or the other.")
if not query:
query = PifSystemReturningQuery(
query=DataQuery(
dataset=DatasetQuery(
id=Filter(equal=dataset_id)
)
),
size = 10000 # Don't pull down all the results by default
)
client = get_client()
if not mdf_acl:
raise ValueError('Access controls (mdf_acl) must be specified. Use ["public"] for public access')
pif_result = client.pif_search(query)
if len(pif_result.hits) == 0:
return []
| python | {
"resource": ""
} |
q10898 | pif_to_mdf_record | train | def pif_to_mdf_record(pif_obj, dataset_hit, mdf_acl):
"""Convert a PIF into partial MDF record"""
| python | {
"resource": ""
} |
q10899 | _to_user_defined | train | def _to_user_defined(pif_obj):
"""Read the systems in the PIF to populate the user-defined portion"""
res = {}
# make a read view to flatten the hierarchy
rv = ReadView(pif_obj)
# Iterate over the keys in the read view
for k in rv.keys():
name, value = _extract_key_value(rv[k].raw)
# add any objects that can be extracted
if name and value is not None:
res[name] = value
# Grab interesting values not in the ReadView
pif = pif_obj.as_dictionary()
elements = {}
if pif.get("composition"):
for comp in pif["composition"]:
if comp.get("actualAtomicPercent"):
elements[comp["element"]] = float(comp["actualAtomicPercent"]["value"])
elif comp.get("actualWeightPercent"):
elements[comp["element"]] = float(comp["actualWeightPercent"]["value"])
if elements:
res["elemental_percent"] = elements
elif pif.get("chemicalFormula"):
symbol = ""
num = ""
# Chemical formulae are comprised of letters, numbers, and potentially characters we don't care about
for char in pif["chemicalFormula"]:
# Uppercase char indicates beginning of new symbol
if char.isupper():
# If there is already a | python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.