suppose i have built a VObject called vobj
(e.g., built via vobject.readComponents(vcfStr)
) and want to add a new key:value pair to it:
print('k=%s v=%s' % (k,v))
try:
stmnt1 = "vobj.add('%s')" % (k)
print('stmnt1:"%s"' % stmnt1)
eval(stmnt1)
print('vobj after add\n'+20*'#'+'\n')
vobj.prettyPrint()
stmnt2 = "vobj.%s.value = '%s'" % (k,v)
print('\n'+20*'#'+('\nstmnt2:"%s"' % stmnt2))
eval(stmnt2)
except Exception as e:
print('wazzup?!',e)
all the extra prints and the try:except
are because i can't
make it work! here's the output produced:
k=bday v=1931-02-10
stmnt1:"vobj.add('bday')"
vobj after add
####################
VCARD
VERSION: 3.0
PRODID: -//Apple Inc.//Mac OS X 10.12.3//EN
N: Foo Bar
FN: Foo Bar
EMAIL: foo@bar.com
params for EMAIL:
TYPE ['INTERNET', 'WORK', 'pref']
...
BDAY:
####################
stmnt2:"vobj.bday.value = '1931-02-10'"
wazzup?! invalid syntax (<string>, line 1)
i have three specific questions:
VObject makes use of
object.attribute
"dot" notation, and the only way i've found to handle arbitrary key names is usingeval()
. there must be a more pythonic way?evaluation of the first statement
stmnt1
works, and changesvobj
as expected, producing an unbound slot forBDAY
. butstmnt2
fails with bad syntax and i don't know why.i have also tried
stmnt2 = "vobj.%s.value = ['%s']" % (k,v)
, making the value a list, because of the two alternatives on the VObject README:j.email.value = 'jeffrey@osafoundation.org' ... j.org.value = ['Open Source Applications Foundation']
does it matter whether a string atom or list is used?