text stringlengths 0 828 |
|---|
# value (dict), we need to check if it's value |
# is equivalent to None. |
if child_num != 0 and not (isinstance(e, dict) and not all(e.values())): |
# At least one child was None, we have to create |
# a new parent-node, which will not be empty. |
sub_elem = etree.SubElement(node, k) |
_to_etree(e, sub_elem) |
else: |
_to_etree(v, etree.SubElement(node, k)) |
elif etree.iselement(d): |
# Supports the case, when we got an empty child and want to recreate it. |
etree.SubElement(d, node) |
else: |
raise AttributeError('Argument is neither dict nor basestring.') |
_to_etree(d, root) |
return root" |
4012,"def objwalk(self, obj, path=(), memo=None): |
""""""Traverse a dictionary recursively and save path |
Taken from: |
http://code.activestate.com/recipes/577982-recursively-walk-python-objects/ |
"""""" |
# dual python 2/3 compatability, inspired by the ""six"" library |
string_types = (str, unicode) if str is bytes else (str, bytes) |
iteritems = lambda mapping: getattr(mapping, 'iteritems', mapping.items)() |
if memo is None: |
memo = set() |
iterator = None |
if isinstance(obj, Mapping): |
iterator = iteritems |
elif isinstance(obj, (Sequence, Set)) and not isinstance(obj, string_types): |
iterator = enumerate |
if iterator: |
if id(obj) not in memo: |
memo.add(id(obj)) |
for path_component, value in iterator(obj): |
for result in self.objwalk(value, path + (path_component,), memo): |
yield result |
memo.remove(id(obj)) |
else: |
yield path, obj" |
4013,"def set_cache_dir(directory): |
""""""Set the directory to cache JSON responses from most API endpoints. |
"""""" |
global cache_dir |
if directory is None: |
cache_dir = None |
return |
if not os.path.exists(directory): |
os.makedirs(directory) |
if not os.path.isdir(directory): |
raise ValueError(""not a directory"") |
cache_dir = directory" |
4014,"def create_element_tree(elem_or_name=None, text=None, **attribute_kwargs): |
"""""" |
Creates an ElementTree from elem_or_name, updated it with text and attributes. |
If elem_or_name is None, a permanently empty ElementTree is returned. |
:param elem_or_name: an Element or the name of the root element tag |
:param text: optional text with which to update the root element |
:param attribute_kwargs: optional attributes to add to the root element |
:return: a new ElementTree with the specified root and attributes |
"""""" |
if elem_or_name is None: |
return ElementTree() |
is_elem = isinstance(elem_or_name, ElementType) |
element = elem_or_name if is_elem else Element(elem_or_name) |
if text is not None: |
element.text = text |
element.attrib.update(attribute_kwargs) |
return ElementTree(element)" |
4015,"def clear_children(parent_to_parse, element_path=None): |
"""""" |
Clears only children (not text or attributes) from the parsed parent |
or named element. |
"""""" |
element = get_element(parent_to_parse, element_path) |
if element is None: |
return parent_to_parse |
else: |
elem_txt = element.text |
elem_atr = element.attrib |
element.clear() |
element.text = elem_txt |
element.attrib = elem_atr |
return element" |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.