view 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
line wrap: on
line source

def runcmd(args):
    """
    Split args into a list, run this command, and return its output.
    Raise RuntimeError if the command does not return 0.
    """
    import subprocess
    print args
    import string
    proc = subprocess.Popen(string.split(args), stdout=subprocess.PIPE)
    output = proc.communicate()[0]
    if proc.returncode != 0:
        import exceptions
        raise exceptions.RuntimeError
    return output

def sign(i, assertions=True):
    """
    + or - 1
    @precondition: i != 0
    """
    if assertions:
        assert i != 0
    else:
        if i == 0: return 0

    return +1 if i > 0 else -1

def unique_elements_list_intersection(list1,list2):
    """
    Return the unique elements that are in both list1 and list2
    (repeated elements in listi will not be duplicated in the result).
    This should run in O(n1+n2) where n1=|list1|, n2=|list2|.
    """
    return list(set.intersection(set(list1),set(list2)))