本文整理汇总了Python中twisted.words.xish.domish.Element.addElement方法的典型用法代码示例。如果您正苦于以下问题:Python Element.addElement方法的具体用法?Python Element.addElement怎么用?Python Element.addElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.words.xish.domish.Element
的用法示例。
在下文中一共展示了Element.addElement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testStuff
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def testStuff(self):
d = EventDispatcher()
cb1 = CallbackTracker()
cb2 = CallbackTracker()
cb3 = CallbackTracker()
d.addObserver("/message/body", cb1.call)
d.addObserver("/message", cb1.call)
d.addObserver("/presence", cb2.call)
d.addObserver("//event/testevent", cb3.call)
msg = Element(("ns", "message"))
msg.addElement("body")
pres = Element(("ns", "presence"))
pres.addElement("presence")
d.dispatch(msg)
self.assertEquals(cb1.called, 2)
self.assertEquals(cb1.obj, msg)
self.assertEquals(cb2.called, 0)
d.dispatch(pres)
self.assertEquals(cb1.called, 2)
self.assertEquals(cb2.called, 1)
self.assertEquals(cb2.obj, pres)
self.assertEquals(cb3.called, 0)
d.dispatch(d, "//event/testevent")
self.assertEquals(cb3.called, 1)
self.assertEquals(cb3.obj, d)
d.removeObserver("/presence", cb2.call)
d.dispatch(pres)
self.assertEquals(cb2.called, 1)
示例2: test_fromElement_bad
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def test_fromElement_bad(self):
el = Element((collab.COLLAB_NS, 'wrong'))
el.addElement('runs', content='100')
def doIt():
p = simulation.Progress.fromElement(el)
self.assertRaises(simulation.InvalidProgressError, doIt)
示例3: send
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def send(self, to, body, subject=None, mtype=None, delay=None):
print 'start sending'
el = Element((None, "message"))
el.attributes["to"] = to
el.attributes["from"] = self.juser
el.attributes["id"] = '111111'
if(subject):
subj = el.addElement("subject")
subj.addContent(subject)
if(mtype):
el.attributes["type"] = mtype
if(delay):
x = el.addElement("x")
x.attributes["xmlns"] = "jabber:x:delay"
x.attributes["from"] = fro
x.attributes["stamp"] = delay
b = el.addElement("body")
b.addContent(body)
self.theXmlstream.send(el)
print 'done sending'
示例4: toElement
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def toElement(self):
el = Element((collab.COLLAB_NS, 'portfolio'))
el.addElement('name', content=self.name)
ass_el = el.addElement('assets')
for a in self.assets:
ass_el.addChild(a.toElement())
return el
示例5: sendErrorMessage
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def sendErrorMessage(pytrans, to, fro, etype, condition, explanation, body=None, el=None):
if el is None:
el = Element((None, "message"))
el.attributes["to"] = to
el.attributes["from"] = fro
el.attributes["type"] = "error"
error = el.addElement("error")
error.attributes["type"] = etype
error.attributes["code"] = str(utils.errorCodeMap[condition])
if condition:
desc = error.addElement(condition)
desc.attributes["xmlns"] = globals.XMPP_STANZAS
text = error.addElement("text")
text.attributes["xmlns"] = globals.XMPP_STANZAS
text.addContent(explanation)
bodywritten = False # no <body/> in message
for child in el.elements(): # try find in el
if child.name == 'body': # <body/> already in el
if body and len(body) > 0: # necessary add other info to it
body_txt = child.__str__()
child.addContent(body_txt + '\n' + body)
bodywritten = True
if not bodywritten and body and len(body) > 0: # <body/> in el don't found
b = el.addElement('body')
b.addContent(body)
pytrans.send(el)
示例6: getRandomPortfolio
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def getRandomPortfolio(min_size=10):
portfolio = Element((collab.COLLAB_NS, 'portfolio'))
#assets
assets = portfolio.addElement('assets')
no_assets = random.randrange(min_size,20)
no_issuers = int(no_assets/ratio)
for i in xrange(no_assets):
a = assets.addElement('asset')
a.addElement('id').addContent(str(i))
a.addElement('name').addContent('asset%i' % i)
a.addElement('default_probability').addContent(str(float(i)/div))
#issuers
issuers = portfolio.addElement('issuers')
for i in xrange(no_issuers):
iss = issuers.addElement('issuer')
iss.addElement('id').addContent(str(i))
iss.addElement('name').addContent('issuer%i' % i)
fs = iss.addElement('factors')
for j in xrange(random.randrange(1, 5)):
f = fs.addElement('factor')
f.addElement('name').addContent('factor%i' % j)
f.addElement('weight').addContent(str(float(j)/div))
#asset issuer map
aimap = portfolio.addElement('asset_issuer_map')
for i in xrange(no_assets):
a = aimap.addElement('asset')
a.addElement('id').addContent(str(i))
a.addElement('issuer').addElement('id').addContent(str(i%no_issuers))
return portfolio
示例7: getPortfolio
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def getPortfolio():
portfolio = Element((collab.COLLAB_NS, 'portfolio'))
#assets
assets = portfolio.addElement('assets')
for i in xrange(num_assets):
a = assets.addElement('asset')
a.addElement('id').addContent(str(i))
a.addElement('name').addContent('asset%i' % i)
a.addElement('default_probability').addContent(str(float(i)/div))
#issuers
issuers = portfolio.addElement('issuers')
for i in xrange(num_issuers):
iss = issuers.addElement('issuer')
iss.addElement('id').addContent(str(i))
iss.addElement('name').addContent('issuer%i' % i)
fs = iss.addElement('factors')
for j in xrange(num_factors):
f = fs.addElement('factor')
f.addElement('name').addContent('factor%i' % j)
f.addElement('weight').addContent(str(float(j)/div))
#asset issuer map
aimap = portfolio.addElement('asset_issuer_map')
for i in xrange(num_assets):
a = aimap.addElement('asset')
a.addElement('id').addContent(str(i))
a.addElement('issuer').addElement('id').addContent(str(i%num_issuers))
return portfolio
示例8: makePhotoElement
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def makePhotoElement(self):
""" Returns an XML Element that can be put into the vCard. """
photo = Element((None, "PHOTO"))
type = photo.addElement("TYPE")
type.addContent("image/png")
binval = photo.addElement("BINVAL")
binval.addContent(base64.encodestring(self.getImageData()).replace("\n", ""))
return photo
示例9: test_Issuer_fromElement_noFactors
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def test_Issuer_fromElement_noFactors(self):
expected = Element((collabNs, 'issuer'))
expected.addElement('name', content='iss')
def doIt():
i = portfolio.Issuer.fromElement(expected)
self.assertRaises(portfolio.InvalidIssuerError, doIt)
示例10: test_Issuer_fromElement_factorsButNoneInThere
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def test_Issuer_fromElement_factorsButNoneInThere(self):
expected = Element((collabNs, 'issuer'))
expected.addElement('name', content='iss')
fs_el = expected.addElement('factors')
i = portfolio.Issuer.fromElement(expected)
self.assertEquals(i.name, 'iss')
self.assertEquals(i.factors, set())
示例11: test_fromElement_noAssets
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def test_fromElement_noAssets(self):
el = Element((collabNs, 'portfolio'))
el.addElement('name', content='port')
def doIt():
p = portfolio.Portfolio.fromElement(el)
self.assertRaises(portfolio.InvalidPortfolioError, doIt)
示例12: test_fromElement_noAssetsInAssetsElement
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def test_fromElement_noAssetsInAssetsElement(self):
el = Element((collabNs, 'portfolio'))
el.addElement('name', content='port')
assets_el = el.addElement('assets')
p = portfolio.Portfolio.fromElement(el)
self.assertEquals(p.name, 'port')
self.assertEquals(p.assets, set())
示例13: test_toElement
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def test_toElement(self):
p = simulation.Progress(100)
el = p.toElement()
expected = Element((collab.COLLAB_NS, 'progress'))
expected.addElement('runs', content='100')
self.assertEquals(el.toXml(), expected.toXml())
示例14: test_fromElement_noWeight
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def test_fromElement_noWeight(self):
el = Element((collabNs, 'factor'))
el.addElement('name', content='fac')
def doIt():
f = portfolio.Factor.fromElement(el)
self.assertRaises(portfolio.InvalidFactorError, doIt)
示例15: test_fromElement
# 需要导入模块: from twisted.words.xish.domish import Element [as 别名]
# 或者: from twisted.words.xish.domish.Element import addElement [as 别名]
def test_fromElement(self):
el = Element((collabNs, 'factor'))
el.addElement('name', content='fac')
el.addElement('weight', content='0.5')
f = portfolio.Factor.fromElement(el)
self.assertEquals(f.name, 'fac')
self.assertEquals(f.weight, 0.5)