Skip to content

Instantly share code, notes, and snippets.

@matteoferla
Last active February 25, 2019 15:21
Show Gist options
  • Save matteoferla/939f28342c476772e2a6889eeb275861 to your computer and use it in GitHub Desktop.
Save matteoferla/939f28342c476772e2a6889eeb275861 to your computer and use it in GitHub Desktop.
a collection of monkeypatch methods that helps handle better the ET.Element instances from Uniprot XML files
__description__ = """
This is a collection of methods that helps handle better the ET.Element instances from Uniprot XML files.
They are monkeypatched to the class object itself.
The Element can be monkeypached by importing xmlschema, as opposed to using the __builtin__ workaround.
Basically, I am piggybacking my monkeypatch on it, meaning that I don't need to copypaste from SO.
* `ET.Element.ns_strip()` #returns the tag with the {namespace}
* `ET.Element.is_tag(value)` # boolean fx to check if tag == value
* `ET.Element.describe()` # prints the content of the elemtn for debugging, similarly to dump but better.
* `ET.Element.is_human()` # boolean fx to check if human or dancer
* `ET.Element.has_attr(key, opt_value)` # boolean fx to check if it has key and optionally it key has the given value
"""
#### Expanding element tree element...
class ElementalExpansion:
def ns_strip(self, ns='{http://uniprot.org/uniprot}'):
return self.tag.replace(ns, '').replace('ns0', '').replace('{', '').replace('}', '')
def is_tag(self, tag):
if self.ns_strip() == tag:
return True
else:
return False
def describe(self):
print('*' * 10)
print('element', self)
print('tag', self.tag)
print('text', self.text)
print('tail', self.tail)
print('attr', self.attrib)
print([child for child in self])
print('*' * 10)
def is_human(self):
for elem in self:
if elem.is_tag('organism'):
for organism_el in list(elem):
if organism_el.text == 'Human':
return True
else:
return False
def has_attr(self, key, value=None):
if key in self.attrib:
if not value:
return True
elif self.attrib[key] == value:
return True
return False
#### monkeypatching
# Only the Python XML Element can be monkeypatched, the C-implemented one cannot.
# eg. ET.parse or ET.fromstring will return the original.
# this is the hack from xmlschema.
import sys, importlib
sys.modules.pop('xml.etree.ElementTree', None)
sys.modules['_elementtree'] = None
ET = importlib.import_module('xml.etree.ElementTree')
########
ET.Element.ns_strip = ElementalExpansion.ns_strip
ET.Element.is_tag = ElementalExpansion.is_tag
ET.Element.describe = ElementalExpansion.describe
ET.Element.is_human = ElementalExpansion.is_human
ET.Element.has_attr = ElementalExpansion.has_attr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment