Convert the UNTL elements structure into an ETD_MS structure.

    kwargs can be passed to the function for certain effects.
359339
def untlpy2etd_ms(untl_elements, **kwargs):
    
    degree_children = {}
    date_exists = False
    seen_creation = False
    # Make the root element.
    etd_ms_root = ETD_MS_CONVERSION_DISPATCH['thesis']()
    for element in untl_elements.children:
        etd_ms_element = None
        # Convert the UNTL element to etd_ms where applicable.
        if element.tag in ETD_MS_CONVERSION_DISPATCH:
            # Create the etd_ms_element if the element's content
            # is stored in children nodes.
            if element.children:
                etd_ms_element = ETD_MS_CONVERSION_DISPATCH[element.tag](
                    qualifier=element.qualifier,
                    children=element.children,
                )
            # If we hit a degree element, make just that one.
            elif element.tag == 'degree':
                # Make a dict of the degree children information.
                if element.qualifier in ['name',
                                         'level',
                                         'discipline',
                                         'grantor']:
                    degree_children[element.qualifier] = element.content
            # For date elements, limit to first instance of creation date.
            elif element.tag == 'date':
                if element.qualifier == 'creation':
                    # If the root already has a date, delete the child.
                    for child in etd_ms_root.children:
                        if child.tag == 'date':
                            del child
                            if not seen_creation:
                                date_exists = False
                    seen_creation = True
                    if not date_exists:
                        # Create the etd_ms element.
                        etd_ms_element = ETD_MS_CONVERSION_DISPATCH[element.tag](
                            qualifier=element.qualifier,
                            content=element.content,
                        )
                        date_exists = True
            # It is a normal element.
            elif element.tag not in ['date', 'degree']:
                # Create the etd_ms_element.
                etd_ms_element = ETD_MS_CONVERSION_DISPATCH[element.tag](
                    qualifier=element.qualifier,
                    content=element.content,
                )
            # Add the element to the structure if the element exists.
            if etd_ms_element:
                etd_ms_root.add_child(etd_ms_element)
        if element.tag == 'meta':
            # Initialize ark to False because it may not exist yet.
            ark = False
            # Iterate through children and look for ark.
            for i in etd_ms_root.children:
                if i.tag == 'identifier' and i.content.startswith(
                    'http://digital.library.unt.edu/'
                ):
                    ark = True
            # If the ark doesn't yet exist, try and create it.
            if not ark:
                # Reset for future tests.
                ark = False
                if element.qualifier == 'ark':
                    ark = element.content
                if ark is not None:
                    # Create the ark identifier element and add it.
                    ark_identifier = ETD_MS_CONVERSION_DISPATCH['identifier'](
                        ark=ark,
                    )
                    etd_ms_root.add_child(ark_identifier)
    # If children exist for the degree, make a degree element.
    if degree_children:
        degree_element = ETD_MS_CONVERSION_DISPATCH['degree']()
        # When we have all the elements stored, add the children to the
        # degree node.
        degree_child_element = None
        for k, v in degree_children.iteritems():
            # Create the individual classes for degrees.
            degree_child_element = ETD_MS_DEGREE_DISPATCH[k](
                content=v,
            )
            # If the keys in degree_children are valid,
            # add it to the child.
            if degree_child_element:
                degree_element.add_child(degree_child_element)
        etd_ms_root.add_child(degree_element)
    return etd_ms_root
904932
def pytype_to_ctype(t):
    
    if isinstance(t, List):
        return 'pythonic::types::list<{0}>'.format(
            pytype_to_ctype(t.__args__[0])
        )
    elif isinstance(t, Set):
        return 'pythonic::types::set<{0}>'.format(
            pytype_to_ctype(t.__args__[0])
        )
    elif isinstance(t, Dict):
        tkey, tvalue = t.__args__
        return 'pythonic::types::dict<{0},{1}>'.format(pytype_to_ctype(tkey),
                                                       pytype_to_ctype(tvalue))
    elif isinstance(t, Tuple):
        return 'decltype(pythonic::types::make_tuple({0}))'.format(
            ", ".join('std::declval<{}>()'.format(pytype_to_ctype(p))
                      for p in t.__args__)
        )
    elif isinstance(t, NDArray):
        dtype = pytype_to_ctype(t.__args__[0])
        ndim = len(t.__args__) - 1
        shapes = ','.join(('long'
                           if s.stop == -1 or s.stop is None
                           else 'std::integral_constant<long, {}>'.format(
                               s.stop)
                           ) for s in t.__args__[1:])
        pshape = 'pythonic::types::pshape<{0}>'.format(shapes)
        arr = 'pythonic::types::ndarray<{0},{1}>'.format(
                    dtype, pshape)
        if t.__args__[1].start == -1:
            return 'pythonic::types::numpy_texpr<{0}>'.format(arr)
        elif any(s.step is not None and s.step < 0 for s in t.__args__[1:]):
            slices = ", ".join(['pythonic::types::normalized_slice'] * ndim)
            return 'pythonic::types::numpy_gexpr<{0},{1}>'.format(arr, slices)
        else:
            return arr
    elif isinstance(t, Pointer):
        return 'pythonic::types::pointer<{0}>'.format(
            pytype_to_ctype(t.__args__[0])
        )
    elif isinstance(t, Fun):
        return 'pythonic::types::cfun<{0}({1})>'.format(
            pytype_to_ctype(t.__args__[-1]),
            ", ".join(pytype_to_ctype(arg) for arg in t.__args__[:-1]),
        )
    elif t in PYTYPE_TO_CTYPE_TABLE:
        return PYTYPE_TO_CTYPE_TABLE[t]
    else:
        raise NotImplementedError("{0}:{1}".format(type(t), t))
464517
def _struct_to_values(self, structure, source_code):
        
        # iterate on source code until all values are finded
        # if a value is not foundable, 
        #   (ie its identificator is not in source code)
        #   it will be replaced by associated neutral value
        iter_source_code = itertools.cycle(source_code)
        values = []
        for lexem_type in (l for l in structure if l is not 'D'):
            if lexem_type is LEXEM_TYPE_CONDITION:
                new_value = self._next_condition_lexems(
                    iter_source_code, len(source_code)
                )
            else:
                new_value = self._next_lexem(
                    lexem_type, iter_source_code, len(source_code)
                )
            # if values is unvalid:
            #   association with the right neutral value
            if new_value is None:
                if lexem_type in (LEXEM_TYPE_PREDICAT, LEXEM_TYPE_CONDITION):
                    new_value = self.neutral_value_condition
                else:
                    new_value = self.neutral_value_action
            values.append(new_value)

        return values
1279375
def concat_namedtuples(*tup, **kwargs):
    '''
    
    '''
    name = kwargs['name'] if 'name' in kwargs else None

    # filter out empty elements
    filtered = [i for i in filter(None, tup)]
    if filtered:
        if len(filtered) == 1:
            return filtered[0]
        else:
            fields = chain(*(t._fields for t in filtered))
            values = chain(*(t for t in filtered))
            if not name:
                name = 'SDMXNamedTuple'
            ConcatType = namedtuple_factory(name, fields)
            return ConcatType(*values)
    else:
        return ()
1328630
def _assemble_conversion(self, stmt):
        """"""
        pybel_lists = ([], [])
        for pybel_list, agent_list in \
                            zip(pybel_lists, (stmt.obj_from, stmt.obj_to)):
            for agent in agent_list:
                node = _get_agent_grounding(agent)
                # TODO check for missing grounding?
                pybel_list.append(node)

        rxn_node_data = reaction(
            reactants=pybel_lists[0],
            products=pybel_lists[1],
        )
        obj_node = self.model.add_node_from_data(rxn_node_data)
        obj_edge = None  # TODO: Any edge information possible here?
        # Add node for controller, if there is one
        if stmt.subj is not None:
            subj_attr, subj_edge = _get_agent_node(stmt.subj)
            subj_node = self.model.add_node_from_data(subj_attr)
            edge_data_list = _combine_edge_data(pc.DIRECTLY_INCREASES,
                                           subj_edge, obj_edge, stmt.evidence)
            for edge_data in edge_data_list:
                self.model.add_edge(subj_node, obj_node, **edge_data)
1108057
def pytype_to_deps_hpp(t):
    """"""
    if isinstance(t, List):
        return {'list.hpp'}.union(pytype_to_deps_hpp(t.__args__[0]))
    elif isinstance(t, Set):
        return {'set.hpp'}.union(pytype_to_deps_hpp(t.__args__[0]))
    elif isinstance(t, Dict):
        tkey, tvalue = t.__args__
        return {'dict.hpp'}.union(pytype_to_deps_hpp(tkey),
                                  pytype_to_deps_hpp(tvalue))
    elif isinstance(t, Tuple):
        return {'tuple.hpp'}.union(*[pytype_to_deps_hpp(elt)
                                     for elt in t.__args__])
    elif isinstance(t, NDArray):
        out = {'ndarray.hpp'}
        # it's a transpose!
        if t.__args__[1].start == -1:
            out.add('numpy_texpr.hpp')
        return out.union(pytype_to_deps_hpp(t.__args__[0]))
    elif isinstance(t, Pointer):
        return {'pointer.hpp'}.union(pytype_to_deps_hpp(t.__args__[0]))
    elif isinstance(t, Fun):
        return {'cfun.hpp'}.union(*[pytype_to_deps_hpp(a) for a in t.__args__])
    elif t in PYTYPE_TO_CTYPE_TABLE:
        return {'{}.hpp'.format(t.__name__)}
    else:
        raise NotImplementedError("{0}:{1}".format(type(t), t))
337806
async def msetnx(self, *args, **kwargs):
        
        if args:
            if len(args) != 1 or not isinstance(args[0], dict):
                raise RedisError('MSETNX requires **kwargs or a single '
                                 'dict arg')
            kwargs.update(args[0])
        items = []
        for pair in iteritems(kwargs):
            items.extend(pair)
        return await self.execute_command('MSETNX', *items)
907690
def des(clas,keyblob,valblob):
    ""
    raise NotImplementedError("don't use tuples, it breaks __eq__. this function probably isn't used in real life")
    raw_keyvals=msgpack.loads(keyblob)
    (namespace,version),keyvals=raw_keyvals[:2],raw_keyvals[2:]
    if namespace!=clas.NAMESPACE or version!=clas.VERSION:
      raise TypeError('des_mismatch got %s want %s'%((namespace,version),(clas.NAMESPACE,clas.VERSION)))
    vals=tuple(msgpack.loads(valblob))
    clas.type_check(zip(*clas.KEY)[1],keyvals,'KEY')
    clas.type_check(zip(*clas.VALUE)[1],vals,'VALUE')
    return clas(tuple(keyvals),vals)
237092
def parsed_function_to_ast(parsed: Parsed, parsed_key):
    """"""

    sub = parsed[parsed_key]

    subtree = {
        "type": "Function",
        "span": sub["span"],
        "function": {
            "name": sub["name"],
            "name_span": sub["name_span"],
            "parens_span": sub.get("parens_span", []),
        },
    }

    args = []
    for arg in parsed[parsed_key].get("args", []):

        # pdb.set_trace()

        if arg["type"] == "Function":
            args.append(parsed_function_to_ast(parsed, arg["span"]))
        elif arg["type"] == "NSArg":
            args.append(
                {
                    "arg": arg["arg"],
                    "type": arg["type"],
                    "span": arg["span"],
                    "nsarg": {
                        "ns": arg["ns"],
                        "ns_val": arg["ns_val"],
                        "ns_span": arg["ns_span"],
                        "ns_val_span": arg["ns_val_span"],
                    },
                }
            )
        elif arg["type"] == "StrArg":
            args.append({"arg": arg["arg"], "type": arg["type"], "span": arg["span"]})

    subtree["args"] = copy.deepcopy(args)

    return subtree
163245
def _translate_struct(inner_dict):
    """"""
    try:
        optional = inner_dict['optional'].items()
        required = inner_dict['required'].items()
    except KeyError as ex:
        raise DeserializationError("Missing key: {}".format(ex))
    except AttributeError as ex:
        raise DeserializationError(
            "Invalid Structure: {}".format(inner_dict))

    val_dict = {k: _translate(v) for k, v in required}
    val_dict.update({Optional(k): _translate(v) for k, v in optional})
    return val_dict
241595
def to_etree(self):
        
        layers_attrib_val = ' '.join('//@layers.{}'.format(layer_id)
                                     for layer_id in self.layers)

        attribs = {
            '{{{pre}}}type'.format(pre=NAMESPACES['xsi']): self.xsi_type,
            'layers': layers_attrib_val}

        E = ElementMaker()
        node = E('nodes', attribs)
        label_elements = (label.to_etree() for label in self.labels)
        node.extend(label_elements)
        return node
1147776
def from_dict(cls, ctx):
        ''
        ctx = Context(ctx)
        s = cls()
        ContextFlags = ctx['ContextFlags']
        setattr(s, 'ContextFlags', ContextFlags)
        if (ContextFlags & CONTEXT_DEBUG_REGISTERS) == CONTEXT_DEBUG_REGISTERS:
            for key in s._ctx_debug:
                setattr(s, key, ctx[key])
        if (ContextFlags & CONTEXT_FLOATING_POINT) == CONTEXT_FLOATING_POINT:
            fsa = ctx['FloatSave']
            s.FloatSave = FLOATING_SAVE_AREA.from_dict(fsa)
        if (ContextFlags & CONTEXT_SEGMENTS) == CONTEXT_SEGMENTS:
            for key in s._ctx_segs:
                setattr(s, key, ctx[key])
        if (ContextFlags & CONTEXT_INTEGER) == CONTEXT_INTEGER:
            for key in s._ctx_int:
                setattr(s, key, ctx[key])
        if (ContextFlags & CONTEXT_CONTROL) == CONTEXT_CONTROL:
            for key in s._ctx_ctrl:
                setattr(s, key, ctx[key])
        if (ContextFlags & CONTEXT_EXTENDED_REGISTERS) == CONTEXT_EXTENDED_REGISTERS:
            er = ctx['ExtendedRegisters']
            for index in compat.xrange(0, MAXIMUM_SUPPORTED_EXTENSION):
                s.ExtendedRegisters[index] = er[index]
        return s
727780
def from_etree(
    el, node=None, node_cls=None,
    tagsub=functools.partial(re.sub, r'\{.+?\}', ''),
    Node=Node):
    '''
    '''
    node_cls = node_cls or Node
    if node is None:
        node = node_cls()
    tag = tagsub(el.tag)
    attrib = dict((tagsub(k), v) for (k, v) in el.attrib.items())
    node.update(attrib, tag=tag)

    if el.text:
        node['text'] = el.text
    for child in el:
        child = from_etree(child, node_cls=node_cls)
        node.append(child)
    if el.tail:
        node['tail'] = el.tail
    return node
1297449
def create_element_dict(self):
        """"""
        untl_dict = {}
        # Loop through all UNTL elements in the Python object.
        for element in self.children:
            # If an entry for the element list hasn't been made in the
            # dictionary, start an empty element list.
            if element.tag not in untl_dict:
                untl_dict[element.tag] = []
            # Create a dictionary to put the element into.
            # Add any qualifier.
            element_dict = {}
            if element.qualifier is not None:
                element_dict['qualifier'] = element.qualifier
            # Add any children that have content.
            if len(element.contained_children) > 0:
                child_dict = {}
                for child in element.children:
                    if child.content is not None:
                        child_dict[child.tag] = child.content
                # Set the element's content as the dictionary
                # of children elements.
                element_dict['content'] = child_dict
            # The element has content, but no children.
            elif element.content is not None:
                element_dict['content'] = element.content
            # Append the dictionary element to the element list.
            untl_dict[element.tag].append(element_dict)

        return untl_dict
1368893
def func2md(self, func, clsname=None, names=None, depth=3):
        
        section = "#" * depth
        if names is None:
            names = [func.__name__]

        funcname = ", ".join(names)
        escfuncname = ", ".join(["`%s`" % funcname if funcname.startswith("_") else funcname for funcname in names])
        header = "%s%s" % ("%s." % clsname if clsname else "", escfuncname)

        path = self.get_src_path(func)
        doc = self.doc2md(func)

        args, kwargs = [], []
        spec = getargspec(func)
        vargsname, kwargsname = spec.varargs, spec.keywords
        vargs = list(make_iter(spec.args)) if spec.args else []
        defaults = list(make_iter(spec.defaults)) if spec.defaults else []

        while vargs:
            if vargs and vargs[0] == "self":
                args.append(vargs.pop(0))
            elif len(vargs) > len(defaults):
                args.append(vargs.pop(0))
            else:
                default = defaults.pop(0)
                if isinstance(default, str):
                    default = "\"%s\"" % default
                else:
                    default = "%s" % str(default)

                kwargs.append((vargs.pop(0), default))

        if args:
            args = ", ".join("%s" % arg for arg in args)
        if kwargs:
            kwargs = ", ".join("%s=%s" % kwarg for kwarg in kwargs)
            if args:
                kwargs = ", " + kwargs
        if vargsname:
            vargsname = "*%s" % vargsname
            if args or kwargs:
                vargsname = ", " + vargsname
        if kwargsname:
            kwargsname = "**%s" % kwargsname
            if args or kwargs or vargsname:
                kwargsname = ", " + kwargsname

        _FUNCDEF = "{funcname}({args}{kwargs}{vargs}{vkwargs})"
        funcdef = _FUNCDEF.format(funcname=funcname,
                                  args=args or "",
                                  kwargs=kwargs or "",
                                  vargs=vargsname or "",
                                  vkwargs=kwargsname or "")

        # split the function definition if it is too long
        lmax = 90
        if len(funcdef) > lmax:
            # wrap in the args list
            split = funcdef.split("(", 1)
            # we gradually build the string again
            rest = split[1]
            args = rest.split(", ")

            funcname = "(".join(split[:1]) + "("
            lline = len(funcname)
            parts = []
            for arg in args:
                larg = len(arg)
                if larg > lmax - 5:
                    # not much to do if arg is so long
                    parts.append(arg)
                elif lline + larg > lmax:
                    # the next arg is too long, break the line
                    parts.append("\\\n    " + arg)
                    lline = 0
                else:
                    parts.append(arg)
                lline += len(parts[-1])
            funcdef = funcname + ", ".join(parts)

        # build the signature
        string = FUNC_TEMPLATE.format(section=section,
                                      header=header,
                                      funcdef=funcdef,
                                      path=path,
                                      doc=doc if doc else "*No documentation found.*")
        return string
678586
def events_to_sax(events, dest):
    
    name_stack = []

    for ev_type, *ev_args in events:
        if ev_type == "start":
            name = (ev_args[0], ev_args[1])
            dest.startElementNS(name, None, ev_args[2])
            name_stack.append(name)
        elif ev_type == "end":
            name = name_stack.pop()
            dest.endElementNS(name, None)
        elif ev_type == "text":
            dest.characters(ev_args[0])
105449
def from_etree(cls, etree_element):
        
        ins = SaltElement.from_etree(etree_element)
        # TODO: this looks dangerous, ask Stackoverflow about it!
        ins.__class__ = SaltEdge.mro()[0]  # convert SaltElement into SaltEdge
        ins.layers = get_layer_ids(etree_element)
        ins.source = get_node_id(etree_element, 'source')
        ins.target = get_node_id(etree_element, 'target')
        return ins