Restructure dictionary (Python 2.5)
Hello, I have a dictionary that has strings as keys and for each key the
associated value is a list of strings. Many of the lists contain only
one element and a given element may appear for more than one key.
What I need to do now is create a new dictionary where the strings in
the list are keys and their values is a list of which keys in the "old"
dictionary that contained them.
So I want to go from:
aa:[a, b, c]
bb:[c, d]
to
a:[aa]
b:[aa]
c[aa, bb]
d:[bb]
I tried this code:
old_dict = {'xyz':['a','b','c'],'baz':['c','d']}
new_dict = {}
for dkey, vallist in old_dict.iteritems():
for val in vallist:
if val in new_dict:
theset = new_dict[val]
theset.add(dkey)
new_dict[val] = theset
else:
new_dict[val] = set([dkey])
print new_dict
Which yields the output {'a': set(['xyz']), 'c': set(['xyz', 'baz']),
'b': set(['xyz']), 'd': set(['baz'])}, so it seems to work but I have a
feeling this is a crappy solution that's underusing Python. Please
enlighten me of a better way!
- Fencer
--
http://mail.python.org/mailman/listinfo/python-list
create boolean
Hi, I need a boolean b to be true if the variable n is not None and not an empty list, otherwise b should be false. I ended up with: b = n is not None and not not n which seems to work but is that normally how you would do it? It can be assumed that n is always None or a list that might be empty - Fencer -- http://mail.python.org/mailman/listinfo/python-list
What should I use for testing a web service?
Hello, I need to write a test program for a web service. The web service publishes its method through a wsdl-file. I recall using SOAPpy in the past and as a I remember it, it was very easy to "load" the WSDL-file and call web service methods. But is SOAPpy really maintained anymore? Someone mentioned ZSI, but that too seems to be abadonware and my first impression was it's complicated to do what was so easy in SOAPpy (at least as I recall it). So I guess my question is: what is a mature, actively-maintained SOAP/WSDL library that is easy to use for the scenario I have (which is testing an already existing service)? - Fencer -- http://mail.python.org/mailman/listinfo/python-list
Re: suds problem
On 2010-01-06 19:33, Fencer wrote: Hello, I just started using suds to use web services. First I tried suds with a very simple web service I had written and was running myself. That worked fine. Then I tried to use the web services provided by KEGG: http://soap.genome.jp/KEGG.wsdl But I get a SAXParseException due to a supposed mis-matched tag when I try to consume that wsdl with suds. I checked the wsdl in oxygene and it claims it's valid. What is the problem here? My test program is below and it's very simple: from suds.client import Client url = 'http://soap.genome.jp/KEGG.wsdl' client = Client(url) print client - Fencer Same problem with reactome: http://www.reactome.org:8080/caBIOWebApp/services/caBIOService?wsdl I suppose I'm mis-using suds, maybe some setting that wasn't needed for my simple web service but is needed for kegg and reactome. - Fencer -- http://mail.python.org/mailman/listinfo/python-list
Re: suds problem
On 2010-01-06 20:02, Fencer wrote: On 2010-01-06 19:33, Fencer wrote: Hello, I just started using suds to use web services. First I tried suds with a very simple web service I had written and was running myself. That worked fine. Then I tried to use the web services provided by KEGG: http://soap.genome.jp/KEGG.wsdl But I get a SAXParseException due to a supposed mis-matched tag when I try to consume that wsdl with suds. I checked the wsdl in oxygene and it claims it's valid. What is the problem here? My test program is below and it's very simple: from suds.client import Client url = 'http://soap.genome.jp/KEGG.wsdl' client = Client(url) print client - Fencer Same problem with reactome: http://www.reactome.org:8080/caBIOWebApp/services/caBIOService?wsdl I suppose I'm mis-using suds, maybe some setting that wasn't needed for my simple web service but is needed for kegg and reactome. - Fencer Seems to be a known problem regarding suds. :( A work-around was suggested to remove some cache files but that didn't solve it for me. Appreciate other ideas! - Fencer -- http://mail.python.org/mailman/listinfo/python-list
suds problem
Hello, I just started using suds to use web services. First I tried suds with a very simple web service I had written and was running myself. That worked fine. Then I tried to use the web services provided by KEGG: http://soap.genome.jp/KEGG.wsdl But I get a SAXParseException due to a supposed mis-matched tag when I try to consume that wsdl with suds. I checked the wsdl in oxygene and it claims it's valid. What is the problem here? My test program is below and it's very simple: from suds.client import Client url = 'http://soap.genome.jp/KEGG.wsdl' client = Client(url) print client - Fencer -- http://mail.python.org/mailman/listinfo/python-list
How do I access what's in this module?
Hello, look at this lxml documentation page: http://codespeak.net/lxml/api/index.html How do I access the functions and variables listed? I tried from lxml.etree import ElementTree and the import itself seems to pass without complaint by the python interpreter but I can't seem to access anything in ElementTree, not the functions or variables. What is the proper way to import that module? For example: >>> from lxml.etree import ElementTree >>> ElementTree.dump(None) Traceback (most recent call last): File "", line 1, in Also, can I access those items that begin with an underscore if I get the import sorted? - Fencer -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I access what's in this module?
On 2010-01-08 04:40, John Machin wrote: For example: >>> from lxml.etree import ElementTree >>> ElementTree.dump(None) Traceback (most recent call last): File "", line 1, in lxml.etree is a module. ElementTree is effectively a class. The error message that you omitted to show us might have given you a clue. But I did show the error message? It's just above what you just wrote. I try to include all relevant information in my posts. Using pommy slang like "sorted" in an IT context has the potential to confuse your transatlantic correspondents :-) Ah, of course! :-) Can access? Yes. Should access? The usual Python convention is that an object whose name begins with an underscore should be accessed only via a documented interface (or, at your own risk, if you think you know what you are doing). It turns out I no longer want to access anything in there but I thank you for your information nontheless. HTH, John - Fencer -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I access what's in this module?
On 2010-01-08 05:01, John Machin wrote: Error message should appear after line starting with "File". Above excerpt taken from google groups; identical to what shows in http://news.gmane.org/gmane.comp.python.general ... what are you looking at? With Windows XP and Python 2.5.4 I get: Traceback (most recent call last): File "", line 1, in AttributeError: 'builtin_function_or_method' object has no attribute 'dump' I'm sorry, I managed to leave out that last line by mistake! My bad. I didn't spot that in my first reply to you because I was under the impression that you hadn't seen the tiniest part of traceback. As you neatly pointed out earlier, it's easy to become confused when communicating. :) It turns out I no longer want to access anything in there but I thank you for your information nontheless. You're welcome -- the advice on _methods is portable :-) I will look more closely at what other advice you write, I must confess I didn't actually do that because, as I mentioned, I no longer had any interest in accessing the module and I was busy (and still am) with another problem. Thanks John! - Fencer -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3's adoption
On 2010-01-28 17:03, Mitchell L Model wrote: I have been working with Python 3 for over a year. I used it in writing my book "Bioinformatics Programming Using Python" (http://oreilly.com/catalog/9780596154509). That book sounds very interesting, even though I am more interested in the bioinformatic parts, I already know some python. I'll show my boss the link, thanks! - Fencer -- http://mail.python.org/mailman/listinfo/python-list
Keeping console window open
Hello, I need to write a simple utility program that will be used under
Windows. I want to write the utility in python and it will be run by
double-clicking the the .py-file.
I put a raw_input('Press enter to exit) at the end so the console window
wouldn't just disappear when the program is finished.
Anyway, I wrote a few lines of code and when I first tried to run it by
double-clicking the .py-file the console window still disappeared right
away. So, in order to see what was happening, I ran it from a shell and
it turned out to be a missing import. My question is how can I trap
errors encountered by the interpreter (if that is the right way to put
it) in order to keep the console window open so one has a chance to see
the error message?
- Fencer
--
http://mail.python.org/mailman/listinfo/python-list
Re: Keeping console window open
Scott David Daniels wrote:
To be a trifle more explicit, turn:
...
if __name__ == '__main__':
main()
into:
...
if __name__ == '__main__':
try:
main()
except Exception, why:
print 'Failed:', why
import sys, traceback
traceback.print_tb(sys.exc_info()[2])
raw_input('Leaving: ')
Note that building your script like this also allows you to
open the interpretter, and type:
import mymodule
mymodule.main()
in order to examine how it runs.
Thanks alot, this was exactly what I was looking for!
--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list
Problem with join in__str__() in class (newbie)
Hello, I've written two classes. One class describes experts: experts
has a unique ID and a name. An expert knows topics and other experts. A
topic is described by my other class and includes a unique ID and a
name. Now I have a problem with the __str__ method in my Expert class:
def __str__(self):
output = '%s:%s' % (self.expert_id, self.name)
output += '\nKnown topics: %s' % (', '.join(str(self.topics)))
# print 'Known experts: '
# for e in self.known_experts:
# print '%s:%s' % (e.expert_id, e.name)
return output
self.topics is a list of objects of type Topic.
self.known_experts is a list of objects of type Expert, specifically the
experts known by the given expert.
When I print an object of type Expert, the output is not what I want. If
the expert knows only one topic, say polemics, the output is:
e2:Carla
Known topics: t, 5, :, P, o, l, e, m, i, c, s
If the expert knows two topics, say Polemics and The Parthenon, then the
output is:
e2:Carla
Known topics: [, <, _, _, m, a, i, n, _, _, ., T, o, p, i, c, , i, n,
s, t, a, n, c, e, , a, t, , 0, x, 0, 2, 2, 2, D, 0, 8, 0, >, ]
This is not what I want. :) I want the output like this:
e2:Carla
Known topics: t5:Polemics, t6:The Parthenon
Also, notice the code I've commented out. If I can get the join above to
work (with your help) my next question is how to present the known
experts in a comma separated list with only expert_id and name? I can't
use the normal __str__() method (the one I'm writing here) because it
prints too much information. Does that mean a join is out of the question?
Thanks for any replies!
- Fencer
--
http://mail.python.org/mailman/listinfo/python-list
Re: Problem with join in__str__() in class (newbie)
jon rascal wrote:
You're turning your list into a string -- try this:
', '.join([str(x) for x in self.topics])
Thanks for your quick reply, unfortunately it didn't quite work for me.
Say topics contain two topics: polemics, and the parthenon I get this
output:
e2:Carla
Known topics: t5:Polemics
only the first topic is printed. If topics only contain a single topic I
get this error:
Traceback (most recent call last):
File "C:\Users\fencer\workspace\Find Expert\src\find_expert.py", line
57, in
print experts[1]
File "C:\Users\fencer\workspace\Find Expert\src\find_expert.py", line
21, in __str__
output += '\nKnown topics: %s' % (', '.join([str(x) for x in
self.topics]))
TypeError: iteration over non-sequence
What did I do wrong?
- Fencer
--
http://mail.python.org/mailman/listinfo/python-list
Re: Problem with join in__str__() in class (newbie)
MRAB wrote: Try printing self.topics. It should always be a list of topics. Ah, yes, that made me find a bug when I was creating the Expert objects: the lists of known topics were not created properly. I should have posted more code I suppose! Thanks for the help, this problem has now been solved. I guess I can't use a join to print the known experts as I described in my first post. - Fencer -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with join in__str__() in class (newbie)
Piet van Oostrum wrote: [snip] Thanks for your detailed reply! - Fencer -- http://mail.python.org/mailman/listinfo/python-list
Sending email
Hello, I'm using Python version 2.6.2 and I discovered it has built-in
libraries for sending email (imports smtplib and email). On the web I
discovered how to send mail through smtp.gmail.com:
mail_server = smtplib.SMTP()
mail_server.connect('smtp.gmail.com')
mail_server.ehlo()
mail_server.starttls()
mail_server.ehlo()
mail_server.login('username', 'password')
msg = MIMEText('Some message text')
msg['Subject'] = 'Some subject'
msg['From'] = 'Mr Underhill'
msg['To'] = 'someemailaddress'
me = 'myemailaddress'
mail_server.sendmail(me, ['someemailaddress'], msg.as_string())
That seems to work just fine but for the messages I will be sending I
don't want to use my private GMail account.
We have an SMTP server at work, and I checked its settings in the
Thunderbird mail client and it's using SSL over port 465, so a bit
different from how GMail's SMTP server is configured in Thunderbird.
I altered my code slightly to account for this:
mail_server = smtplib.SMTP_SSL()
mail_server.connect('mysmtpserver.at.work', 465)
Everything I left untouched. However, it doesn't work:
Traceback (most recent call last):
File "C:\Users\fencer\workspace\Send Email\src\send_email.py", line
16, in
mail_server.ehlo()
File "C:\Python26\lib\smtplib.py", line 382, in ehlo
self.putcmd(self.ehlo_msg, name or self.local_hostname)
File "C:\Python26\lib\smtplib.py", line 318, in putcmd
self.send(str)
File "C:\Python26\lib\smtplib.py", line 310, in send
raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
As you can see, I'm on Windows Vista.
What do I need to change in my code to fix this problem? Thunderbird
manages to do it! :) Ideally, I would like send an email that the
recipient can't reply to, but if doesn't work, it's fine too. I want to
use this to send automated emails to students with account information
for a course.
- Fencer
--
http://mail.python.org/mailman/listinfo/python-list
Re: Sending email
7stud wrote: [snip] Thanks for your reply. After consulting the sysadmins here I was able to get it to work. - Fencer -- http://mail.python.org/mailman/listinfo/python-list
