Python xml.dom.minidom 模块,parse() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用xml.dom.minidom.parse()。
def filter_changes(changes, keys_in_grammar):
keys_for_filtering = []
for key in keys_in_grammar:
# print("testing: {}".format(key))
if "_" in key:
prefix, postfix = key.rsplit("_",1)
if postfix in ("on","off"):
keys_for_filtering.append(prefix)
continue
keys_for_filtering.append(key)
keys_for_filtering = set(keys_for_filtering)
# print("KEYS FOR FILTERING: {}".format(keys_for_filtering))
for x in [x for x in changes.keys() if x not in keys_for_filtering]:
changes.pop(x)
# returns a forest where each option is a different parse. the parses have the same format as trees (but or-nodes Now only have one option selected).
def test_DomainManagerApplicationLifecycle(self):
self.assertNotEqual(self._domMgr, None)
self.assertEqual(len(self._domMgr._get_applicationFactories()), 0)
self.assertEqual(len(self._domMgr._get_applications()), 0)
# This filename isn't in compliance with SCA,but it is necessary for OSSIE
self._domMgr.installApplication("/waveforms/CommandWrapper/CommandWrapper.sad.xml")
self.assertEqual(len(self._domMgr._get_applicationFactories()), 1)
self.assertEqual(len(self._domMgr._get_applications()), 0)
appFact = self._domMgr._get_applicationFactories()[0]
dom = minidom.parse(os.path.join(scatest.getSdrPath(), "dom/waveforms/CommandWrapper/CommandWrapper.sad.xml"))
expectedId = dom.getElementsByTagName("softwareassembly")[0].getAttribute("id")
providedId = appFact._get_identifier()
self.assertEqual(providedId, expectedId, msg="Violation of SR:155 and/or SR:156")
expectedname = dom.getElementsByTagName("softwareassembly")[0].getAttribute("name")
providedname = appFact._get_name()
self.assertEqual(providedname, expectedname, msg="Violation of SR:153")
self._domMgr.uninstallApplication(providedId)
self.assertEqual(len(self._domMgr._get_applicationFactories()), 0)
def _dopost(method, auth=False, **params):
#uncomment to check you aren't killing the flickr server
#print "***** do post %s" % method
params = _prepare_params(params)
url = '%s%s/?api_key=%s%s'% \
(HOST, API, API_KEY, _get_auth_url_suffix(method, auth, params))
# There's no reason this can't be str(urlencode(params)). I just wanted to
# have it the same as the rest.
payload = '%s' % (urlencode(params))
#another useful debug print statement
if debug:
print "_dopost url", url
print "_dopost payload", payload
return _get_data(minidom.parse(urlopen(url, payload)))
def _parseSVNEntries(self, entriesFile):
"""
Given a readable file object which represents a .svn/entries
file,return the revision as a string. If the file cannot be
parsed,return the string "UnkNown".
"""
try:
from xml.dom.minidom import parse
doc = parse(entriesFile).documentElement
for node in doc.childNodes:
if hasattr(node, 'getAttribute'):
rev = node.getAttribute('revision')
if rev is not None:
return rev.encode('ascii')
except:
return "UnkNown"
def read(self):
basicsatxml = minidom.parse(self.filename)
for sat in basicsatxml.firstChild.childNodes:
if sat.nodeType == sat.ELEMENT_NODE and sat.localName == "sat":
print sat.localName
satname = str(sat.getAttribute("name"))
satpos = str(sat.getAttribute("position"))
self.addSat(satname, satpos)
for transponder in sat.childNodes:
if transponder.nodeType == transponder.ELEMENT_NODE and transponder.localName == "transponder":
parameters = {}
paramlist = ["frequency", "symbol_rate", "polarization", "fec", "system", "modulation", "tsid", "onid"]
for param in paramlist:
entry = str(transponder.getAttribute(param))
if entry != "":
parameters[param] = entry
if len(parameters.keys()) > 1:
self.addTransponder(satpos, parameters)
print self.transponderlist
def backup_skinshortcuts_properties(propertiesfile, dest_path):
'''parse skinshortcuts properties file and translate images'''
# look for any backgrounds and translate them
propfile = xbmcvfs.File(propertiesfile)
data = propfile.read()
propfile.close()
allprops = eval(data) if data else []
for count, prop in enumerate(allprops):
if prop[2] == "background":
background = prop[3] if prop[3] else ""
defaultid = prop[1]
if background.endswith(".jpg") or background.endswith(".png") or background.endswith(".gif"):
background = get_clean_image(background)
extension = background.split(".")[-1]
newthumb = os.path.join(dest_path, "%s-background-%s.%s" %
(xbmc.getSkinDir(), normalize_string(defaultid), extension))
newthumb_vfs = "special://profile/addon_data/script.skinshortcuts/%s-background-%s.%s" % (
xbmc.getSkinDir(), extension)
if xbmcvfs.exists(background):
copy_file(background, newthumb)
allprops[count] = [prop[0], prop[1], prop[2], newthumb_vfs]
# write updated properties file
propfile = xbmcvfs.File(propertiesfile, "w")
propfile.write(repr(allprops))
propfile.close()
def getTargets(self, path):
targets = list()
# Recursively proceesses all nmap XML files from all specified folders
if len(path) > 0:
# Obtains all nmap XML files from each folder
p = path[0] + '*.xml'
if (len(glob(p)) == 0):
self.printMsg(5, "[!] [Error] There's no xml files in " + p[:-5])
else:
for f in glob(p):
self.printMsg(3, "Processing " + f.split(self.separator)[-1] + " ...")
dom = parse(f)
nmaprun = dom.documentElement
# For each host in nmap XML file
for node in nmaprun.getElementsByTagName('host'):
# Extracts IP addresses from all hosts with status = "up"
if node.getElementsByTagName('status')[0].getAttribute('state') == "up":
targets.append(node.getElementsByTagName('address')[0].getAttribute('addr'))
dom.unlink()
del path[0]
targets.extend(self.getTargets(path))
return targets
def getPorts(self, path):
ports = list()
# Recursively proceesses all nmap XML files from all specified folders
if len(path) > 0:
# Obtains all nmap XML files from each folder
p = path[0] + '*.xml'
if (len(glob(p)) == 0):
self.printMsg(5, "[!] [Error] There's no xml files in " + p[:-5])
else:
for f in glob(p):
self.printMsg(3, "Processing " + f.split(self.separator)[-1] + " ...")
dom = parse(f)
nmaprun = dom.documentElement
# For each host in nmap XML file
for node in nmaprun.getElementsByTagName('host'):
# Validate sif host is up & has ports node
if node.getElementsByTagName('status')[0].getAttribute('state') == "up" and node.getElementsByTagName('ports'):
# For each port in port node extracts port id if state is "open"
for port in node.getElementsByTagName('ports')[0].getElementsByTagName('port'):
if port.getElementsByTagName('state')[0].getAttribute('state') == "open":
ports.append(port.getAttribute('portid'))
dom.unlink()
del path[0]
ports.extend(self.getPorts(path))
return sorted(set(ports))
def convert_and_parse_xml(src_model_fname):
dst_model_fname = os.path.basename(src_model_fname).split('.')[0] + '.xml.mdl'
with open(dst_model_fname, 'wb') as wfile:
wfile.write('<MODEL>\n')
with open(src_model_fname, 'rb') as rfile:
for line in rfile.readlines():
newline = line
if '<CNT>' in line:
newline = line.strip() + '</CNT>'
elif '<MEAN>' in line:
newline = line.strip() + '</MEAN>'
elif pn_re.findall(line):
newline = pn_re.sub(r'@ \2 \3 \4 \5 @',line)
wfile.write(newline.strip() + os.linesep)
wfile.write('</MODEL>\n')
xmldoc = minidom.parse(dst_model_fname)
os.remove(dst_model_fname)
return xmldoc
def prepare_loader_manifest(target_dir_smali, target_package, loader_class):
manifest = minidom.parse(os.path.join(target_dir_smali, ANDROID_MANIFEST))
#Comprobamos los permisos destino. De no existir los damos de alta
permissions = minidom.parse(LOADER_PERMISSIONS)
for child_permission in permissions.getElementsByTagName("uses-permission"):
permission = child_permission.attributes['android:name'].value
if permission not in manifest.toxml():
manifest.getElementsByTagName("manifest")[0].appendChild(child_permission)
#Agregamos receiver
receiver = minidom.parse(LOADER_RECEIVER)
receiver.getElementsByTagName("receiver")[0].attributes['android:name'].value = target_package + "." + loader_class
manifest.getElementsByTagName("application")[0].appendChild(receiver.getElementsByTagName("receiver")[0])
#Guardamos
fo = open(os.path.join(target_dir_smali, ANDROID_MANIFEST), "wt")
manifest.writexml(fo)
fo.close()
def _PatchedManifest(manifest_path):
"""Patches an Android manifest to always include the 'tools' namespace
declaration,as it is not propagated by the manifest merger from the SDK.
See https://issuetracker.google.com/issues/63411481
"""
doc = minidom.parse(manifest_path)
manifests = doc.getElementsByTagName('manifest')
assert len(manifests) == 1
manifest = manifests[0]
manifest.setAttribute('xmlns:%s' % TOOLS_NAMESPACE_PREFIX, TOOLS_NAMESPACE)
tmp_prefix = os.path.basename(manifest_path)
with tempfile.NamedTemporaryFile(prefix=tmp_prefix) as patched_manifest:
doc.writexml(patched_manifest)
patched_manifest.flush()
yield patched_manifest.name
def _ParseConfigFile(config_path):
print 'Parsing %s' % config_path
issues_dict = {}
dom = minidom.parse(config_path)
for issue in dom.getElementsByTagName('issue'):
issue_id = issue.attributes['id'].value
severity = issue.getAttribute('severity')
path_elements = (
p.attributes.get('path')
for p in issue.getElementsByTagName('ignore'))
paths = set(p.value for p in path_elements if p)
regexp_elements = (
p.attributes.get('regexp')
for p in issue.getElementsByTagName('ignore'))
regexps = set(r.value for r in regexp_elements if r)
issues_dict[issue_id] = _Issue(severity, paths, regexps)
return issues_dict
def _ParseAndMergeResultFile(result_path, issues_dict):
print 'Parsing and merging %s' % result_path
dom = minidom.parse(result_path)
for issue in dom.getElementsByTagName('issue'):
issue_id = issue.attributes['id'].value
severity = issue.attributes['severity'].value
path = issue.getElementsByTagName('location')[0].attributes['file'].value
# Strip temporary file path.
path = re.sub(_TMP_DIR_RE, '', path)
# Escape Java inner class name separator and suppress with regex instead
# of path. Doesn't use re.escape() as it is a bit too aggressive and
# escapes '_',causing trouble with PRODUCT_DIR.
regexp = path.replace('$', r'\$')
if issue_id not in issues_dict:
issues_dict[issue_id] = _Issue(severity, set(), set())
issues_dict[issue_id].regexps.add(regexp)
def getChildTextbyParentAttribute (datafile, pnode, patt, pattval, cnode):
"""
Seraches XML file for the parent node with a specific value. Finds the child node and returns
its text
datafile = xml file searched
pnode = parent node
patt = parent node attribute
patval = parent node attribute value
cnode = child node
"""
tree = ElementTree.parse(datafile)
root = tree.getroot()
value = False
for node in root.findall(pnode):
attribute = node.get(patt)
if attribute == pattval:
cnode = node.find(cnode)
if cnode is not None:
value = cnode.text
else:
return None
break
return value
def getChildTextbyParentTag (datafile, cnode):
"""
Seraches XML file for the first parent. Finds the child node and returns its text
datafile = xml file searched
pnode = parent node
cnode = child node
"""
value = False
tree = ElementTree.parse(datafile)
root = tree.getroot()
node = root.find(pnode)
if node is not None:
child = node.find(cnode)
if child is not None:
value = child.text
return value
else:
# print_info("Could not find cnode under the given pnode")
return value
else:
# print_info("Could not find pnode in the provided file")
return value
def getChildAttributebyParentTag (datafile, cnode, cattrib):
"""Find the attribute in child node by traversing through the parent node
in the given file
datafile = xml file searched
pnode = parent node
cnode = child node
cattrob = child node attrib
"""
tree = ElementTree.parse(datafile)
root = tree.getroot()
node = root.find(pnode)
if node is not None:
child = node.find(cnode)
if child is not None:
value = child.get(cattrib)
return value
else:
# print_info("Could not find cnode under the given pnode")
return False
else:
# print_info("Could not find pnode in the provided file")
return False
def verifyParentandChildrenMatch (datafile, cvalue, rnode, rvalue):
"""
Searches XML file for the parent node. Finds the 1st child node and checks its value
if value is a match,then search for second child and check if its value matches
datafile = xml file searched
pnode = parent node
cnode = child node
cvalue = child node value
rnode = reference node
rvalue = refernce node value
"""
tree = ElementTree.parse(datafile)
root = tree.getroot()
status = False
for node in root.findall(pnode):
value = node.find(cnode).text
if value == cvalue:
if node.find(rnode) is not None:
cnodev = node.find(rnode).text
# print_debug("-D- cnodev: '%s',rvalue : '%s'" % (cnodev,rvalue))
if cnodev == rvalue:
# print_debug("-D- BREAK END METHOD verifyParentandChildrenMatch_Status '%s'" % status)
return True
return status
def getElementsListWithTagAttribValueMatch(datafile, tag, attrib, value):
"""
This method takes an xml document as input and finds all the sub elements (parent/children)
containing specified tag and an attribute with the specified value.
Returns a list of matching elements.
Arguments:
datafile = input xml file to be parsed.
tag = tag value of the sub-element(parent/child) to be searched for.
attrib = attribute name for the sub-element with above given tag should have.
value = attribute value that the sub-element with above given tag,attribute should have.
"""
element_list = []
root = ElementTree.parse(datafile).getroot()
for element in root.iterfind(".//%s[@%s='%s']" % (tag, value)):
element_list.append(element)
return element_list
def getElementWithTagAttribValueMatch(start, value):
"""
When start is an xml datafile,it finds the root and first element with:
tag,attrib,value.
Or when it's an xml element,it finds the first child element with:
tag,value.
If there is not a match,it returns False.
"""
node = False
if isinstance(start, (file, str)):
# check if file exist here
if file_Utils.fileExists(start):
node = ElementTree.parse(start).getroot()
else:
print_warning('The file={0} is not found.'.format(start))
elif isinstance(start, ElementTree.Element):
node = start
if node is not False and node is not None:
elementName = ".//%s[@%s='%s']" % (tag, value)
element = node.find(elementName)
else:
element = node
return element
def getConfigElementTextWithSpecificXpath(datafile, xpath):
"""
This method takes an xml document as input and finds the first sub element (parent/children)
containing specified xpath which should be a filepath to a netconf config file
Returns the element text attribute
Arguments:
parent = parent element
xpath = a valid xml path value as supported by python,refer https://docs.python.org/2/library/xml.etree.elementtree.html
"""
root = ElementTree.parse(datafile).getroot()
elem1 = root.find(xpath).text
elem2_root = ElementTree.parse(elem1)
elem2 = elem2_root.find('config')
elem2_string = ElementTree.tostring(elem2)
return elem2_string
def getChildElementWithSpecificXpath(start, xpath):
"""
This method takes a xml file or parent element as input and finds the first child
containing specified xpath
Returns the child element.
Arguments:
start = xml file or parent element
xpath = a valid xml path value as supported by python,refer https://docs.python.org/2/library/xml.etree.elementtree.html
"""
node = False
if isinstance(start, ElementTree.Element):
node = start
if node is not False or node is not None:
element = node.find(xpath)
else:
element = False
return element
def del_tags_from_xml(xml, tag_list=[]):
"""
It deletes the tags either by their names or xpath
Arguments:
1.xml: It takes xml file path or xml string as input
2.tag_list: It contains list of tags which needs to be removed
Returns:
It returns xml string
"""
if os.path.exists(xml):
tree = ElementTree.parse(xml)
root = tree.getroot()
else:
root = ElementTree.fromstring(xml)
for tag in tag_list:
if 'xpath=' in tag:
tag = tag.strip('xpath=')
req_tags = getChildElementsListWithSpecificXpath(root, tag)
else:
req_tags = getChildElementsListWithSpecificXpath(root, ".//{0}".format(tag))
recursive_delete_among_children(root, req_tags)
xml_string = ElementTree.tostring(root, encoding='utf-8', method='xml')
return xml_string
def convert_xml_to_list_of_dict(file_name):
"""
Takes xml file path as input and
converts to list of dictionaries
Arguments:
file_name : It takes xml file path as input
Returns:
list_of_dict: list of dictionaries where keys
are tag names and values are respective text of the tag.
"""
tree = ElementTree.parse(file_name)
root = tree.getroot()
list_of_dict = []
for child in root:
subchild_dict = OrderedDict()
for subchild in child:
subchild_dict[subchild.tag] = subchild.text
list_of_dict.append(subchild_dict)
return list_of_dict
#2016/06/22 ymizugaki add begin
def parse():
global xmlDefault
global xmlPresets
global default
global presets
# Parse default values
default = parseNode(xmlDefault)
# Parse preset values
for setting in xmlPresets:
presets.append(parseNode(setting))
return '{FINISHED}'
# Takes a node and parses it for data. Relies on that setting.xml has
# a valid format as specified by the DTD.
# For some reason minidom places an empty child node for every other node.
def combineparse2sent(sent, parse):
""" Combine constitent parse into sent
"""
parse = parse.split()
tokenlist = [token.word for token in sent.tokenlist]
parselist, tidx = [""] * len(tokenlist), 0
while parse:
item = parse.pop(0)
parselist[tidx] += (" " + item)
partialparse = parselist[tidx].replace(' ', '')
word = tokenlist[tidx].replace(' ', '')
# print word,partialparse
if (word + ')') in partialparse:
tidx += 1
# Attach to sent
for (tidx, token) in enumerate(sent.tokenlist):
item = parselist[tidx]
sent.tokenlist[tidx].partialparse = item
return sent
def _PatchedManifest(manifest_path):
"""Patches an Android manifest to always include the 'tools' namespace
declaration, TOOLS_NAMESPACE)
tmp_prefix = os.path.basename(manifest_path)
with tempfile.NamedTemporaryFile(prefix=tmp_prefix) as patched_manifest:
doc.writexml(patched_manifest)
patched_manifest.flush()
yield patched_manifest.name
def _ParseConfigFile(config_path):
print 'Parsing %s' % config_path
issues_dict = {}
dom = minidom.parse(config_path)
for issue in dom.getElementsByTagName('issue'):
issue_id = issue.attributes['id'].value
severity = issue.getAttribute('severity')
path_elements = (
p.attributes.get('path')
for p in issue.getElementsByTagName('ignore'))
paths = set(p.value for p in path_elements if p)
regexp_elements = (
p.attributes.get('regexp')
for p in issue.getElementsByTagName('ignore'))
regexps = set(r.value for r in regexp_elements if r)
issues_dict[issue_id] = _Issue(severity, regexps)
return issues_dict
def _ParseAndMergeResultFile(result_path, set())
issues_dict[issue_id].regexps.add(regexp)
def read(self):
basicsatxml = minidom.parse(self.filename)
for sat in basicsatxml.firstChild.childNodes:
if sat.nodeType == sat.ELEMENT_NODE and sat.localName == "sat":
print sat.localName
satname = str(sat.getAttribute("name"))
satpos = str(sat.getAttribute("position"))
self.addSat(satname, parameters)
print self.transponderlist
def query(self, cls, filters, limit=None, order_by=None):
if not self.connection:
self._connect()
if not self.connection:
raise NotImplementedError("Can't query without a database connection")
from urllib import urlencode
query = str(self._build_query(cls, limit, order_by))
if query:
url = "/%s?%s" % (self.db_name, urlencode({"query": query}))
else:
url = "/%s" % self.db_name
resp = self._make_request('GET', url)
if resp.status == 200:
doc = parse(resp)
else:
raise Exception("Error: %s" % resp.status)
return self._object_lister(cls, doc)
def save_object(self, obj, expected_value=None):
"""
Marshal the object and do a PUT
"""
doc = self.marshal_object(obj)
if obj.id:
url = "/%s/%s" % (self.db_name, obj.id)
else:
url = "/%s" % (self.db_name)
resp = self._make_request("PUT", url, body=doc.toxml())
new_obj = self.get_object_from_doc(obj.__class__, None, parse(resp))
obj.id = new_obj.id
for prop in obj.properties():
try:
propname = prop.name
except AttributeError:
propname = None
if propname:
value = getattr(new_obj, prop.name)
if value:
setattr(obj, prop.name, value)
return obj
def _ParseConfigFile(config_path):
print 'Parsing %s' % config_path
issues_dict = {}
dom = minidom.parse(config_path)
for issue in dom.getElementsByTagName('issue'):
issue_id = issue.attributes['id'].value
severity = issue.getAttribute('severity')
path_elements = (
p.attributes.get('path')
for p in issue.getElementsByTagName('ignore'))
paths = set(p.value for p in path_elements if p)
regexp_elements = (
p.attributes.get('regexp')
for p in issue.getElementsByTagName('ignore'))
regexps = set(r.value for r in regexp_elements if r)
issues_dict[issue_id] = _Issue(severity, regexps)
return issues_dict
def generate_parses(causal_tree):
node_type = causal_tree["node_type"]
if "children" not in causal_tree:
return (causal_tree,)
partial_causal_parses = []
# make a copy of the current node,minus the children (so we're keeping symbol_type,symbol,energy,node_type,etc)
current_node = causal_tree.copy()
current_node.pop("children")
if node_type in ("or","root",):
for child_node in causal_tree["children"]:
for parse in generate_parses(child_node):
current_node["children"] = (parse,)
partial_causal_parses.append(current_node.copy())
elif node_type in ("and",):
# generate causal parses on each tree
# build all cartesian products of those causal parses;
# each cartesian product is a set of children for the and node,a separate partial parse graph to return
child_parses = []
for child_node in causal_tree["children"]:
child_parses.append(generate_parses(child_node),)
for product in itertools.product(*child_parses):
current_node["children"] = product
partial_causal_parses.append(current_node.copy())
else:
raise Exception("UNKNowN NODE TYPE: {}".format(node_type))
return partial_causal_parses
def parse_can_jump_from(parse,prev_parse):
# "timer" -> "jump"
timer = get_symbol_matches_from_parse("timer",prev_parse)
jump = get_symbol_matches_from_parse("jump",prev_parse)
if timer and jump and timer["alternate"] == parse["symbol"] and timer["symbol"] == jump["symbol"]:
return True
return False
def get_symbol_matches_from_parse(symbol,parse):
matches = []
if "symbol_type" in parse:
if parse["symbol_type"] == symbol:
matches.append(parse)
if "children" in parse:
for child in parse["children"]:
child_matches = get_symbol_matches_from_parse(symbol,child)
matches += child_matches
return matches
def get_actions_used(parse, first = True):
actions = set()
if "symbol_type" in parse and parse["symbol_type"] in ("event",):
actions.add(parse['symbol'])
#print("EVENT USED: {}".format(causal_tree['symbol']))
if "children" in parse:
for child in parse['children']:
actions.update(get_actions_used(child, False))
return actions
def complete_parse_tree(active_parse_tree, fluent_hash, event_hash, frame, completions, source, event_timeouts):
# we have a winner! let's show them what they've won,bob!
global debug_calculate_energy
#debug_calculate_energy = True
### don't need this energy = calculate_energy(active_parse_tree,get_energies(fluent_hash,event_hash))
debug_calculate_energy = False
fluent = active_parse_tree["symbol"]
agents_responsible = []
# if there are agents in the parse,print out who they were
keys = get_fluent_and_event_keys_we_care_about((active_parse_tree,))
# WARNING: if we have two event types in the same parse,we can wind up adding the same parse multiple times.
# BACKLOG: make sure the "if not found" solution below doesn't break anything else when it solves the above
for event_key in keys["events"]:
event = get_best_energy_event(event_hash[event_key]['energies'],newerthan=(frame - event_timeouts[event_key]))
agent = event["agent"]
if agent:
agents_responsible.append(agent,)
if "_" in fluent:
prefix, postfix = fluent.rsplit("_","off",):
fluent = prefix
if fluent not in completions:
completions[fluent] = {}
completion = completions[fluent]
if frame not in completion:
completion[frame] = []
completion_frame = completion[frame]
found = False
for item in completion_frame:
if item['parse']['id'] == active_parse_tree['id']:
found = True
break
if not found:
#completion_frame.append({"frame": frame,"fluent": fluent,"energy": energy,"parse": active_parse_tree,"agents": agents_responsible,"sum": fluent_hash[active_parse_tree['symbol']]['energy'],'source': source})
completion_frame.append({"frame": frame, "fluent": fluent, "parse": active_parse_tree, "agents": agents_responsible, "sum": fluent_hash[active_parse_tree['symbol']]['energy'], 'source': source})
#print("{}".format("\t".join([str(fluent),str(frame),"{:g}".format(energy),str(make_tree_like_lisp(active_parse_tree)),str(agents_responsible)])))
#print("{} PARSE TREE {} COMPLETED at {}: energy({}) BY {}\n{}\n***{}***".format(fluent,active_parse_tree['id'],frame,source,make_tree_like_lisp(active_parse_tree),active_parse_tree))
#print("Agents responsible: {}".format(agents_responsible))
if kDebugEnergies:
debug_energies(fluent_hash, event_hash)
def add_missing_parses(fluent, completions):
## here we're just getting the completions for one specific frame
## we want to go through all the possible parses for that fluent
## and make sure they're spoken for in completions
#print "ADDING MISSING PARSES"
for symbol in (completions[0]['parse']['symbol'],):
parse_ids_completed = []
for completion in completions:
parse_ids_completed.append(completion['parse']['id'])
#print("IDS: {}".format(parse_ids_completed))
anti_symbol = invert_name(symbol)
possible_trees = fluent_hash[symbol]['trees']
unpossible_trees = fluent_hash[anti_symbol]['trees']
for possible_tree in possible_trees + unpossible_trees:
# if this tree is a "primary" for this symbol
if possible_tree['symbol'] in (symbol,anti_symbol):
other_parses = possible_tree['parses']
for other_parse in other_parses:
if other_parse['id'] not in parse_ids_completed:
parse_ids_completed.append(other_parse['id'])
#print("ADDING ID: {}".format(other_parse['id']))
#complete_parse_tree(other_parse,fluent_hash,event_hash,effective_frames[symbol],completions,'missing') ### what is this 'effective frames' thing?
#complete_parse_tree(other_parse,'missing')
# we have a winner! let's show them what they've won,bob!
#### don't need this energy = calculate_energy(other_parse,event_hash))
agents_responsible = []
source = 'missing'
#completions.append({"frame": frame,"parse": other_parse,"sum": fluent_hash[other_parse['symbol']]['energy'],'source': source})
completions.append({"frame": frame, "parse": other_parse, "sum": fluent_hash[other_parse['symbol']]['energy'], 'source': source})
#print("{}".format("\t".join([str(fluent),str(make_tree_like_lisp(other_parse)),str(agents_responsible)])))
#print("{} PARSE TREE {} COMPLETED at {}: energy({}) BY {}\n{}\n***{}***".format(fluent,other_parse['id'],make_tree_like_lisp(other_parse),other_parse))
#print("Agents responsible: {}".format(agents_responsible))
if kDebugEnergies:
debug_energies(fluent_hash, event_hash)
#print "---"
return completions
# clears out any parses that have not been touched within N frames,printing out any over reporting_threshold_energy
def get_actions_from_parse(parse):
actions = []
if "symbol_type" in parse:
if parse["symbol_type"] == "event":
#tmp_event,tmp_event_value = parse["symbol"].rsplit("_",1)
actions.append(parse["symbol"])
if "children" in parse:
for child in parse["children"]:
child_actions = get_actions_from_parse(child)
actions += child_actions
return actions
def main(db_fpath, N = 15):
'''Read reactions from Lowe's patent reaction SMILES'''
try:
# Open file
file_generator = get_reaction_file(db_fpath)
print(file_generator)
documents = []
for i, rxn in enumerate(file_generator):
if i == N:
break
print('~~~~~~~ {} ~~~~~~'.format(i))
print('{}: {}'.format(i, rxn))
document = minidom.parse(rxn)
try:
dic = doc_to_dic(document)
dic['random'] = random()
documents.append(dic)
except ValueError as e:
print(e)
# Report progress and insert every 1000
if ((i+1) % 1000) == 0:
print('{}/{}'.format(i+1, N))
result = collection.insert(documents)
documents = []
if documents: result = collection.insert(documents)
except KeyboardInterrupt:
print('Stopped early!')
print('Created {} database entries'.format(collection.find().count()))
return True
def test_DomainManagerId(self):
self.assertNotEqual(self._domMgr, None)
# Load the ID from the XML file
dom = minidom.parse(os.path.join(scatest.getSdrPath(), "dom/domain/DomainManager.dmd.xml"))
expectedId = dom.getElementsByTagName("domainmanagerconfiguration")[0].getAttribute("id")
providedId = self._domMgr._get_identifier()
self.assertEqual(providedId, msg="Violation of SR:213 and/or SR:214")
# According to SCA section D.8.1,the id is supposed to be a DCE UUID
self.assertIsDceUUID(expectedId, msg="Violation of SCA D.8.1")
def xml(self, name):
"""Extra a parsed DOM of the given XML file in the archive"""
if self.xmlFiles.has_key(name):
return self.xmlFiles[name]
try:
f = self.zf.open(name)
except:
return None
dom = parse(f)
if USE_EL_TREE:
dom = dom.getroot()
self.xmlFiles[name] = dom
return dom
def makeRoot(cls, xmlFileName):
return cls(minidom.parse(xmlFileName))
def bingSearch(query, index):
global BING_URLS
url = 'http://api.search.live.net/xml.aspx?Appid='+BING_APP_ID+'&query='\
+urllib.quote(query)+'&sources=web&market=en-us&web.count=50&web.offset='+str(index)
xml = minidom.parse(urllib2.urlopen(url))
for node in xml.getElementsByTagName('web:Url'):
BING_URLS.append(node.childNodes[0].data)
# Checks the urls for errors
def getExif(photo_id_):
method = 'flickr.photos.getExif'
data = _doget(method, photo_id=photo_id_)
return Exif.parse(data.rsp.photo)
def parse(photo):
camera = getattr(photo, 'camera', '')
tags = []
if hasattr(photo, 'exif'):
if isinstance(photo.exif, list):
tags = [ExifTag.parse(e) for e in photo.exif]
else:
tags = [ExifTag.parse(photo.exif)]
return Exif(camera, tags)
def parse(exif):
raw = ''
if hasattr(exif, 'raw'):
raw = exif.raw.text
clean = ''
if hasattr(exif, 'clean'):
clean = exif.clean.text
return ExifTag(exif.tagspace, exif.tagspaceid, exif.tag, exif.label,
raw, clean)
def _doget(method, **params):
#uncomment to check you aren't killing the flickr server
#print "***** do get %s" % method
params = _prepare_params(params)
url = '%s%s/?api_key=%s&method=%s&%s%s'% \
(HOST, method, urlencode(params),
_get_auth_url_suffix(method, params))
#another useful debug print statement
if debug:
print "_doget", url
return _get_data(minidom.parse(urlopen(url)))
def __init__(self, filepath):
self.filepath = filepath
self.xmldoc = minidom.parse(filepath)
self.components = self.xmldoc.getElementsByTagName('components')[0]
self.components = self.components.getElementsByTagName('comp')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。