Mercurial > pylearn
annotate common/misc.py @ 359:9e73e6dc9823
Improved runcmd
author | Joseph Turian <turian@iro.umontreal.ca> |
---|---|
date | Wed, 02 Jul 2008 15:42:42 -0400 |
parents | faece52be094 |
children | 0e3af3c53ac7 |
rev | line source |
---|---|
359 | 1 def runcmd(args): |
358
faece52be094
Added common.misc.runcmd
Joseph Turian <turian@iro.umontreal.ca>
parents:
357
diff
changeset
|
2 """ |
359 | 3 Split args into a list, run this command, and return its output. |
4 Raise RuntimeError if the command does not return 0. | |
358
faece52be094
Added common.misc.runcmd
Joseph Turian <turian@iro.umontreal.ca>
parents:
357
diff
changeset
|
5 """ |
359 | 6 import subprocess |
7 print args | |
8 import string | |
9 proc = subprocess.Popen(string.split(args), stdout=subprocess.PIPE) | |
10 output = proc.communicate()[0] | |
11 if proc.returncode != 0: | |
12 import exceptions | |
13 raise exceptions.RuntimeError | |
14 return output | |
358
faece52be094
Added common.misc.runcmd
Joseph Turian <turian@iro.umontreal.ca>
parents:
357
diff
changeset
|
15 |
355 | 16 def sign(i, assertions=True): |
17 """ | |
18 + or - 1 | |
19 @precondition: i != 0 | |
20 """ | |
21 if assertions: | |
22 assert i != 0 | |
23 else: | |
24 if i == 0: return 0 | |
25 | |
26 return +1 if i > 0 else -1 | |
27 | |
28 def unique_elements_list_intersection(list1,list2): | |
29 """ | |
30 Return the unique elements that are in both list1 and list2 | |
31 (repeated elements in listi will not be duplicated in the result). | |
32 This should run in O(n1+n2) where n1=|list1|, n2=|list2|. | |
33 """ | |
34 return list(set.intersection(set(list1),set(list2))) |