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