Esempio n. 1
0
class TempArea(TestArea):
    """TempArea class is essentially similar to the TestArea class, with
    the only difference that the cwd is *not* changed into the newly
    created area.
    """

    _temp_area_alloc = UtilPrototype("void* temp_area_alloc( char* )",
                                     bind=False)
    _temp_area_alloc_relative = UtilPrototype(
        "void* temp_area_alloc_relative( char* , char* )", bind=False)

    def __init__(self, name, prefix=None, store_area=False):
        if prefix:
            if os.path.exists(prefix):
                c_ptr = self._temp_area_alloc_relative(prefix, name)
            else:
                raise IOError("The prefix path:%s must exist" % prefix)
        else:
            c_ptr = self._temp_area_alloc(name)
        super(TempArea, self).__init__(name,
                                       c_ptr=c_ptr,
                                       store_area=store_area)

    def get_cwd(self):
        """
        Since the TempArea class does *not* change the cwd this method
        just returns the ordinary os.getcwd().
        """
        return os.getcwd()

    def getPath(self):
        """
        Will return the full path to the temporary working area.
        """
        return self._get_cwd()
Esempio n. 2
0
class PermutationVector(BaseCClass):
    TYPE_NAME = "permutation_vector"
    _free = UtilPrototype("void   perm_vector_free( permutation_vector )")
    _size = UtilPrototype("int    perm_vector_get_size( permutation_vector )")
    _iget = UtilPrototype("int    perm_vector_iget( permutation_vector , int)")

    
    def __init__(self):
        raise NotImplementedError("Can not instantiate PermutationVector directly")


    def __len__(self):
        return self._size( )


    def __str__(self):
        s = "("
        for index in self:
            s += " %d" % index
        return s + ")"

    
    def __getitem__(self, index):
        if index < 0:
            index += len(self)

        if 0 <= index < len(self):
            return self._iget( index )
        else:
            raise IndexError("Invalid index:%d" % index)

        
    def free(self):
        self._free( )
Esempio n. 3
0
class Buffer(BaseCClass):
    _alloc = UtilPrototype("void* buffer_alloc(int)" , bind = False)
    _free = UtilPrototype("void buffer_free(buffer)")

    def __init__(self, size):
        super(Buffer, self).__init__(self._alloc(size))

    def free(self):
        self._free()
Esempio n. 4
0
class PathFormat(BaseCClass):
    TYPE_NAME = "path_fmt"
    _str  = UtilPrototype("char* path_fmt_get_fmt(path_fmt)")
    _free = UtilPrototype("void path_fmt_free(path_fmt)") 
    
    def __init__(self):
        raise NotImplementedError("Class can not be instantiated directly!")


    def __str__(self):
        return self._str( )


    def free(self):
        self._free( )
Esempio n. 5
0
class CThreadPool(BaseCClass):
    TYPE_NAME = "thread_pool"

    _alloc = UtilPrototype("void* thread_pool_alloc(int, bool)", bind=False)
    _free = UtilPrototype("void thread_pool_free(thread_pool)")
    _add_job = UtilPrototype(
        "void thread_pool_add_job(thread_pool, void*, void*)")
    _join = UtilPrototype("void thread_pool_join(thread_pool)")

    def __init__(self, pool_size, start=True):
        c_ptr = self._alloc(pool_size, start)
        super(CThreadPool, self).__init__(c_ptr)
        self.arg_list = []

    def addTaskFunction(self, name, lib, c_function_name):
        function = CThreadPool.lookupCFunction(lib, c_function_name)

        def wrappedFunction(arg):
            return self.addTask(function, arg)

        setattr(self, name, wrappedFunction)

    def addTask(self, cfunc, arg):
        """
        The function should come from CThreadPool.lookupCFunction().
        """
        if isinstance(arg, BaseCClass):
            arg_ptr = BaseCClass.from_param(arg)
        else:
            arg_ptr = arg

        self.arg_list.append(arg)
        self._add_job(cfunc, arg_ptr)

    def join(self):
        self._join()

    def free(self):
        self.join()
        self._free()

    @staticmethod
    def lookupCFunction(lib, name):
        if isinstance(lib, ctypes.CDLL):
            func = getattr(lib, name)
            return func
        else:
            raise TypeError("The lib argument must be of type ctypes.CDLL")
Esempio n. 6
0
class IntegerHash(Hash):
    _get_int = UtilPrototype("int hash_get_int(hash, char*)")
    _insert_int = UtilPrototype("void hash_insert_int(hash, char*, int)")

    def __init__(self):
        super(IntegerHash, self).__init__()

    def __setitem__(self, key, value):
        if isinstance(value, int):
            self._insert_int(key, value)
        else:
            raise ValueError("IntegerHash does not support type: %s" % value.__class__)

    def __getitem__(self, key):
        if key in self:
            return self._get_int(key)
        else:
            raise KeyError("Hash does not have key: %s" % key)
Esempio n. 7
0
class StringHash(Hash):
    _get_string = UtilPrototype("char* hash_get_string(hash, char*)")
    _insert_string = UtilPrototype("void hash_insert_string(hash, char*, char*)")

    def __init__(self):
        super(StringHash, self).__init__()

    def __setitem__(self, key, value):
        if isinstance(value, str):
            self._insert_string(key, value)
        else:
            raise ValueError("StringHash does not support type: %s" % value.__class__)

    def __getitem__(self, key):
        if key in self:
            return self._get_string(key)
        else:
            raise KeyError("Hash does not have key: %s" % key)
Esempio n. 8
0
class SubstitutionList(BaseCClass):
    TYPE_NAME = "subst_list"

    _alloc = UtilPrototype("void* subst_list_alloc(void*)" , bind = False)
    _free = UtilPrototype("void subst_list_free(subst_list)")
    _size = UtilPrototype("int subst_list_get_size(subst_list)")
    _get_key = UtilPrototype("char* subst_list_iget_key(subst_list, int)")
    _get_value = UtilPrototype("char* subst_list_iget_value(subst_list, int)")
    _get_doc_string = UtilPrototype("char* subst_list_iget_doc_string(subst_list, int)")
    _append_copy = UtilPrototype("void subst_list_append_copy(subst_list, char*, char*, char*)")

    def __init__(self):
        c_ptr = self._alloc(0)
        super(SubstitutionList, self).__init__(c_ptr)

    def __len__(self):
        return self._size()

    def addItem(self, key, value, doc_string=""):
        self._append_copy(key, value, doc_string)

    def __getitem__(self, index_or_key):
        if not isinstance(index_or_key, int):
            raise IndexError("Index must be a number!")

        if index_or_key < 0 or index_or_key >= len(self):
            raise IndexError("Index must be in the range: [%i, %i]" % (0, len(self) - 1))

        key = self._get_key(index_or_key)
        value = self._get_value(index_or_key)
        doc_string = self._get_doc_string(index_or_key)

        return key, value, doc_string

    def __iter__(self):
        index = 0
        while index < len(self):
            yield self[index]
            index += 1

    def __contains__(self, key):
        for kw, value, doc in self:
            if key == kw:
                return True
        return False

    def indexForKey(self, key):
        if not key in self:
            raise KeyError("Key '%s' not in substitution list!" % key)

        for index, key_val_doc in enumerate(self):
            if key == key_val_doc[0]:
                return index

        return None  # Should never happen!

    def free(self):
        self._free()
Esempio n. 9
0
class DoubleHash(Hash):
    _get_double = UtilPrototype("double hash_get_double(hash, char*)")
    _insert_double = UtilPrototype("void hash_insert_double(hash, char*, double)")

    def __init__(self):
        super(DoubleHash, self).__init__()

    def __setitem__(self, key, value):
        if isinstance(value, int):
            value = float(value)

        if isinstance(value, float):
            self._insert_double(key, value)
        else:
            raise ValueError("DoubleHash does not support type: %s" % value.__class__)

    def __getitem__(self, key):
        if key in self:
            return self._get_double( key)
        else:
            raise KeyError("Hash does not have key: %s" % key)
Esempio n. 10
0
class ArgPack(BaseCClass):
    TYPE_NAME = "arg_pack"

    _alloc = UtilPrototype("void* arg_pack_alloc()", bind=False)
    _append_int = UtilPrototype("void arg_pack_append_int(arg_pack, int)")
    _append_double = UtilPrototype(
        "void arg_pack_append_double(arg_pack, double)")
    _append_ptr = UtilPrototype("void arg_pack_append_ptr(arg_pack, void*)")

    _size = UtilPrototype("int arg_pack_size(arg_pack)")
    _free = UtilPrototype("void arg_pack_free(arg_pack)")

    def __init__(self, *args):
        c_ptr = self._alloc()
        super(ArgPack, self).__init__(c_ptr)
        self.child_list = []
        for arg in args:
            self.append(arg)

    def append(self, data):
        if isinstance(data, int):
            self._append_int(data)
        elif isinstance(data, float):
            self._append_double(data)
        elif isinstance(data, BaseCClass):
            self._append_ptr(BaseCClass.from_param(data))
            self.child_list.append(data)
        else:
            raise TypeError("Can only add int/double/basecclass")

    def __len__(self):
        return self._size()

    def free(self):
        self._free()
Esempio n. 11
0
class Log(BaseCClass):
    _get_filename = UtilPrototype("char* log_get_filename(log)")
    _reopen = UtilPrototype("void log_reopen(log, char*)")
    _get_level = UtilPrototype("int log_get_level(log)")
    _set_level = UtilPrototype("void log_set_level(log, int)")

    def __init__(self):
        raise NotImplementedError("Class can not be instantiated directly!")

    def get_filename(self):
        return self._get_filename()
        # return "ert_config.log"

    def reopen(self, filename):
        print "Logfile cannot be reopened"
        # cfunc.reopen( self , filename)

    def get_level(self):
        return self._get_level()

    def set_level(self, level):
        pass
Esempio n. 12
0
class Hash(BaseCClass):
    _alloc =      UtilPrototype("void* hash_alloc()" , bind = False)
    _free =       UtilPrototype("void hash_free(hash)")
    _size =       UtilPrototype("int hash_get_size(hash)")
    _keys =       UtilPrototype("stringlist_obj hash_alloc_stringlist(hash)")
    _has_key =    UtilPrototype("bool hash_has_key(hash, char*)")
    _get =        UtilPrototype("void* hash_get(hash, char*)")
    _insert_ref = UtilPrototype("void hash_insert_ref(hash, char*, void*)")

    """
    Base hash class that supports string:void* values
    """

    def __init__(self):
        c_ptr = self._alloc()
        super(Hash, self).__init__(c_ptr)

    def __len__(self):
        return self._size()

    def __getitem__(self, key):
        if self._has_key(key):
            return self._get(key)
        else:
            raise KeyError("Hash does not have key: %s" % key)

    def __setitem__(self, key, value):
        if isinstance(value, c_void_p):
            self._insert_ref(key, value)
        else:
            raise ValueError("Hash does not support type: %s" % value.__class__)

    def __contains__(self, key):
        """ @rtype: bool """
        return self._has_key(key)

    def __iter__(self):
        for key in self.keys():
            yield key

    def keys(self):
        """ @rtype: StringList """
        return self._keys()

    def free(self):
        self._free()

    def __str__(self):
        return str(["%s: %s" % (key, self[key]) for key in self.keys()])
Esempio n. 13
0
class RandomNumberGenerator(BaseCClass):
    TYPE_NAME = "rng"

    _rng_alloc = UtilPrototype(
        "void* rng_alloc(rng_alg_type_enum, rng_init_mode_enum)", bind=False)
    _free = UtilPrototype("void rng_free(rng)")
    _get_double = UtilPrototype("double rng_get_double(rng)")
    _get_int = UtilPrototype("int rng_get_int(rng, int)")
    _get_max_int = UtilPrototype("uint rng_get_max_int(rng)")
    _state_size = UtilPrototype("int rng_state_size(rng)")
    _set_state = UtilPrototype("void rng_set_state(rng , char*)")

    def __init__(self,
                 alg_type=RngAlgTypeEnum.MZRAN,
                 init_mode=RngInitModeEnum.INIT_CLOCK):
        assert isinstance(alg_type, RngAlgTypeEnum)
        assert isinstance(init_mode, RngInitModeEnum)

        c_ptr = self._rng_alloc(alg_type, init_mode)
        super(RandomNumberGenerator, self).__init__(c_ptr)

    def stateSize(self):
        return self._state_size()

    def setState(self, seed_string):
        state_size = self.stateSize()
        if len(seed_string) < state_size:
            raise ValueError(
                "The seed string must be at least %d characters long" %
                self.stateSize())
        self._set_state(seed_string)

    def getDouble(self):
        """ @rtype: float """
        return self._get_double()

    def getInt(self, max=None):
        """ @rtype: float """
        if max is None:
            max = self._get_max_int()

        return self._get_int(max)

    def free(self):
        self._free()
Esempio n. 14
0
class IntVector(VectorTemplate):
    default_format = "%d"

    _alloc = UtilPrototype("void*  int_vector_alloc( int , int )", bind=False)
    _create_active_list = UtilPrototype(
        "int_vector_obj string_util_alloc_active_list( char*)", bind=False)
    _create_value_list = UtilPrototype(
        "int_vector_obj string_util_alloc_value_list( char*)", bind=False)
    _alloc_copy = UtilPrototype(
        "int_vector_obj int_vector_alloc_copy( int_vector )")
    _strided_copy = UtilPrototype(
        "int_vector_obj int_vector_alloc_strided_copy( int_vector , int , int , int)"
    )
    _free = UtilPrototype("void   int_vector_free( int_vector )")
    _iget = UtilPrototype("int    int_vector_iget( int_vector , int )")
    _safe_iget = UtilPrototype(
        "int    int_vector_safe_iget( int_vector , int )")
    _iset = UtilPrototype("int    int_vector_iset( int_vector , int , int)")
    _size = UtilPrototype("int    int_vector_size( int_vector )")
    _append = UtilPrototype("void   int_vector_append( int_vector , int )")
    _idel_block = UtilPrototype(
        "void   int_vector_idel_block( int_vector , int , int )")
    _pop = UtilPrototype("int    int_vector_pop( int_vector )")
    _idel = UtilPrototype("void   int_vector_idel( int_vector , int )")
    _insert = UtilPrototype(
        "void   int_vector_insert( int_vector , int , int)")
    _lshift = UtilPrototype("void   int_vector_lshift( int_vector , int )")
    _rshift = UtilPrototype("void   int_vector_rshift( int_vector , int )")
    _fprintf = UtilPrototype(
        "void   int_vector_fprintf( int_vector , FILE , char* , char*)")
    _sort = UtilPrototype("void   int_vector_sort( int_vector )")
    _rsort = UtilPrototype("void   int_vector_rsort( int_vector )")
    _reset = UtilPrototype("void   int_vector_reset( int_vector )")
    _set_read_only = UtilPrototype(
        "void   int_vector_set_read_only( int_vector , bool )")
    _get_read_only = UtilPrototype(
        "bool   int_vector_get_read_only( int_vector )")
    _get_max = UtilPrototype("int    int_vector_get_max( int_vector )")
    _get_min = UtilPrototype("int    int_vector_get_min( int_vector )")
    _get_max_index = UtilPrototype(
        "int    int_vector_get_max_index( int_vector , bool)")
    _get_min_index = UtilPrototype(
        "int    int_vector_get_min_index( int_vector , bool)")
    _shift = UtilPrototype("void   int_vector_shift( int_vector , int )")
    _scale = UtilPrototype("void   int_vector_scale( int_vector , int )")
    _div = UtilPrototype("void   int_vector_div( int_vector , int )")
    _inplace_add = UtilPrototype(
        "void   int_vector_inplace_add( int_vector , int_vector )")
    _inplace_mul = UtilPrototype(
        "void   int_vector_inplace_mul( int_vector , int_vector )")
    _assign = UtilPrototype("void   int_vector_set_all( int_vector , int)")
    _memcpy = UtilPrototype(
        "void   int_vector_memcpy(int_vector , int_vector )")
    _set_default = UtilPrototype(
        "void   int_vector_set_default( int_vector , int)")
    _get_default = UtilPrototype("int    int_vector_get_default( int_vector )")
    _element_size = UtilPrototype(
        "int    int_vector_element_size( int_vector )")

    _permute = UtilPrototype(
        "void int_vector_permute(int_vector, permutation_vector)")
    _sort_perm = UtilPrototype(
        "permutation_vector_obj int_vector_alloc_sort_perm(int_vector)")
    _rsort_perm = UtilPrototype(
        "permutation_vector_obj int_vector_alloc_rsort_perm(int_vector)")
    _contains = UtilPrototype("bool int_vector_contains(int_vector, int)")
    _select_unique = UtilPrototype("void int_vector_select_unique(int_vector)")
    _element_sum = UtilPrototype("int int_vector_sum(int_vector)")
    _get_data_ptr = UtilPrototype("int* int_vector_get_ptr(int_vector)")
    _count_equal = UtilPrototype("int int_vector_count_equal(int_vector, int)")
    _init_range = UtilPrototype(
        "void int_vector_init_range(int_vector, int , int , int)")

    def __init__(self, default_value=0, initial_size=0):
        super(IntVector, self).__init__(default_value, initial_size)

    @classmethod
    def active_list(cls, range_string):
        """Will create a IntVector instance with the values from @range_string.

        The range_string input should be of the type "1,3-5,9,17",
        i.e. integer values separated by commas, and dashes to
        represent ranges. If the input string contains ANY invalid
        characters the returned active list will be empty:

           "1,4-7,10"  =>  {1,4,5,6,7,10}
           "1,4-7,10X" =>  {}
        
        The empty list will evaluate to false. The values in the input
        string are meant to indicate "active values", i.e. the output
        values are sorted and repeated values are only counted once:
        
           "1,1,7,2" => {1,2,7}
        
        """
        return cls._create_active_list(range_string)

    @classmethod
    def valueList(cls, range_string):
        """Will create a IntVecter of all the values in the @range_string.

        Will not sort the values, and not uniquiefy - in contrast to
        the active_list() method.

        """
        return cls._create_value_list(range_string)

    def count(self, value):
        """ @rtype: int """
        return self._count_equal(value)
Esempio n. 15
0
class DoubleVector(VectorTemplate):
    default_format = "%8.4f"

    _alloc = UtilPrototype("void*  double_vector_alloc( int , double )",
                           bind=False)
    _alloc_copy = UtilPrototype(
        "double_vector_obj   double_vector_alloc_copy( double_vector )")
    _strided_copy = UtilPrototype(
        "double_vector_obj   double_vector_alloc_strided_copy( double_vector , int , int , int)"
    )
    _free = UtilPrototype("void   double_vector_free( double_vector )")
    _iget = UtilPrototype("double double_vector_iget( double_vector , int )")
    _safe_iget = UtilPrototype(
        "double double_vector_safe_iget(double_vector , int )")
    _iset = UtilPrototype(
        "double double_vector_iset( double_vector , int , double)")
    _size = UtilPrototype("int    double_vector_size( double_vector )")
    _append = UtilPrototype(
        "void   double_vector_append( double_vector , double )")
    _idel_block = UtilPrototype(
        "void   double_vector_idel_block( double_vector , int , int )")
    _pop = UtilPrototype("double double_vector_pop( double_vector )")
    _idel = UtilPrototype("void   double_vector_idel( double_vector , int )")
    _lshift = UtilPrototype(
        "void   double_vector_lshift( double_vector , int )")
    _rshift = UtilPrototype(
        "void   double_vector_rshift( double_vector , int )")
    _insert = UtilPrototype(
        "void   double_vector_insert( double_vector , int , double)")
    _fprintf = UtilPrototype(
        "void   double_vector_fprintf( double_vector , FILE , char* , char*)")
    _sort = UtilPrototype("void   double_vector_sort( double_vector )")
    _rsort = UtilPrototype("void   double_vector_rsort( double_vector )")
    _reset = UtilPrototype("void   double_vector_reset( double_vector )")
    _get_read_only = UtilPrototype(
        "bool   double_vector_get_read_only( double_vector )")
    _set_read_only = UtilPrototype(
        "void   double_vector_set_read_only( double_vector , bool )")
    _get_max = UtilPrototype("double double_vector_get_max( double_vector )")
    _get_min = UtilPrototype("double double_vector_get_min( double_vector )")
    _get_max_index = UtilPrototype(
        "int    double_vector_get_max_index( double_vector , bool)")
    _get_min_index = UtilPrototype(
        "int    double_vector_get_min_index( double_vector , bool)")
    _shift = UtilPrototype(
        "void   double_vector_shift( double_vector , double )")
    _scale = UtilPrototype(
        "void   double_vector_scale( double_vector , double )")
    _div = UtilPrototype("void   double_vector_div( double_vector , double )")
    _inplace_add = UtilPrototype(
        "void   double_vector_inplace_add( double_vector , double_vector )")
    _inplace_mul = UtilPrototype(
        "void   double_vector_inplace_mul( double_vector , double_vector )")
    _assign = UtilPrototype(
        "void   double_vector_set_all( double_vector , double)")
    _memcpy = UtilPrototype(
        "void   double_vector_memcpy(double_vector , double_vector )")
    _set_default = UtilPrototype(
        "void   double_vector_set_default( double_vector , double)")
    _get_default = UtilPrototype(
        "double double_vector_get_default( double_vector )")
    _element_size = UtilPrototype(
        "int    double_vector_element_size( double_vector )")

    _permute = UtilPrototype(
        "void double_vector_permute(double_vector, permutation_vector)")
    _sort_perm = UtilPrototype(
        "permutation_vector_obj double_vector_alloc_sort_perm(double_vector)")
    _rsort_perm = UtilPrototype(
        "permutation_vector_obj double_vector_alloc_rsort_perm(double_vector)")
    _contains = UtilPrototype(
        "bool double_vector_contains(double_vector, double)")
    _select_unique = UtilPrototype(
        "void double_vector_select_unique(double_vector)")
    _element_sum = UtilPrototype("double double_vector_sum(double_vector)")
    _get_data_ptr = UtilPrototype(
        "double* double_vector_get_ptr(double_vector)")
    _count_equal = UtilPrototype(
        "int double_vector_count_equal(double_vector, double)")
    _init_range = UtilPrototype(
        "void double_vector_init_range(double_vector, double , double , double)"
    )

    def __init__(self, default_value=0, initial_size=0):
        super(DoubleVector, self).__init__(default_value, initial_size)
Esempio n. 16
0
class TestArea(BaseCClass):
    _test_area_alloc = UtilPrototype("void* test_work_area_alloc( char* )",
                                     bind=False)
    _test_area_alloc_relative = UtilPrototype(
        "void* test_work_area_alloc_relative( char* , char* )", bind=False)
    _free = UtilPrototype("void test_work_area_free( test_area )")
    _install_file = UtilPrototype(
        "void test_work_area_install_file( test_area , char* )")
    _copy_directory = UtilPrototype(
        "void test_work_area_copy_directory( test_area , char* )")
    _copy_file = UtilPrototype(
        "void test_work_area_copy_file( test_area , char* )")
    _copy_directory_content = UtilPrototype(
        "void test_work_area_copy_directory_content( test_area , char* )")
    _copy_parent_directory = UtilPrototype(
        "void test_work_area_copy_parent_directory( test_area , char* )")
    _copy_parent_content = UtilPrototype(
        "void test_work_area_copy_parent_content( test_area , char* )")
    _get_cwd = UtilPrototype("char* test_work_area_get_cwd( test_area )")
    _get_original_cwd = UtilPrototype(
        "char* test_work_area_get_original_cwd( test_area )")
    _set_store = UtilPrototype(
        "void test_work_area_set_store( test_area , bool)")

    def __init__(self, test_name, prefix=None, store_area=False, c_ptr=None):

        if c_ptr is None:
            if prefix:
                if os.path.exists(prefix):
                    c_ptr = self._test_area_alloc_relative(prefix, test_name)
                else:
                    raise IOError("The prefix path:%s must exist" % prefix)
            else:
                c_ptr = self._test_area_alloc(test_name)

        super(TestArea, self).__init__(c_ptr)
        self.set_store(store_area)

    def get_original_cwd(self):
        return self._get_original_cwd()

    def get_cwd(self):
        return self._get_cwd()

    def orgPath(self, path):
        if os.path.isabs(path):
            return path
        else:
            return os.path.abspath(os.path.join(self.get_original_cwd(), path))

    # All the methods install_file() , copy_directory(),
    # copy_parent_directory(), copy_parent_content(),
    # copy_directory_content() and copy_file() expect an input
    # argument which is relative to the original CWD - or absolute.

    def install_file(self, filename):
        if os.path.isfile(self.orgPath(filename)):
            self._install_file(filename)
        else:
            raise IOError("No such file:%s" % filename)

    def copy_directory(self, directory):
        if os.path.isdir(self.orgPath(directory)):
            self._copy_directory(directory)
        else:
            raise IOError("No such directory: %s" % directory)

    def copy_parent_directory(self, path):
        if os.path.exists(self.orgPath(path)):
            self._copy_parent_directory(path)
        else:
            raise IOError("No such file or directeory: %s" % path)

    def copy_parent_content(self, path):
        if os.path.exists(self.orgPath(path)):
            self._copy_parent_content(path)
        else:
            raise IOError("No such file or directeory: %s" % path)

    def copy_directory_content(self, directory):
        if os.path.isdir(self.orgPath(directory)):
            self._copy_directory_content(directory)
        else:
            raise IOError("No such directeory: %s" % path)

    def copy_file(self, filename):
        if os.path.isfile(self.orgPath(filename)):
            self._copy_file(filename)
        else:
            raise IOError("No such file:%s" % filename)

    def free(self):
        self._free()

    def set_store(self, store):
        self._set_store(store)

    def getFullPath(self, path):
        if not os.path.exists(path):
            raise IOError("Path not found:%s" % path)

        if os.path.isabs(path):
            raise IOError("Path:%s is already absolute" % path)

        return os.path.join(self.get_cwd(), path)
Esempio n. 17
0
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  ERT is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.
#
#  See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
#  for more details.
"""
Module with utility functions from util.c
"""

from ert.util import UtilPrototype

strcmp_int = UtilPrototype("int util_strcmp_int( char* , char* )")
"""
Function to compare strings with embedded integers.

Will use proper numeric comparison when comparing strings with
embedded numbers, i.e. "CASE-9" will follow after "CASE-10" when
sorting:

   >> l = ["CASE-9" , "CASE-10"]
   >> l.sort()
   >> print l
      ["CASE-10" , "CASE-9"]
   >> l.sort( strcmp_int )
   >> print l
      ["CASE-9" , "CASE-10"]
Esempio n. 18
0
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  ERT is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.
#
#  See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
#  for more details.
from collections import Sequence

from cwrap import PrototypeError
from ert.util import LLSQResultEnum, UtilPrototype, Matrix

quantile = UtilPrototype(
    "double statistics_empirical_quantile(double_vector, double)")
"""@type: (ert.util.DoubleVector, float)->float"""

quantile_sorted = UtilPrototype(
    "double statistics_empirical_quantile(double_vector, double)")
"""@type: (ert.util.DoubleVector, float)->float"""

try:
    _polyfit = UtilPrototype(
        "llsq_result_enum matrix_stat_polyfit(matrix, matrix, matrix, matrix)")
except PrototypeError:
    _polyfit = None


def polyfit(n, x, y, s=None):
    """
Esempio n. 19
0
from ert.util import UtilPrototype


def installAbortSignals():
    install_signals()


def updateAbortSignals():
    """
    Will install the util_abort_signal for all UNMODIFIED signals.
    """
    update_signals()


install_signals = UtilPrototype("void util_install_signals()")
update_signals = UtilPrototype("void util_update_signals()")
Esempio n. 20
0
class LookupTable(BaseCClass):
    _alloc = UtilPrototype("void* lookup_table_alloc_empty()", bind=False)
    _max = UtilPrototype("double lookup_table_get_max_value( lookup_table )")
    _min = UtilPrototype("double lookup_table_get_min_value( lookup_table )")
    _arg_max = UtilPrototype("double lookup_table_get_max_arg( lookup_table )")
    _arg_min = UtilPrototype("double lookup_table_get_min_arg( lookup_table )")
    _append = UtilPrototype(
        "void lookup_table_append( lookup_table , double , double )")
    _size = UtilPrototype("int lookup_table_get_size( lookup_table )")
    _interp = UtilPrototype(
        "double lookup_table_interp( lookup_table , double)")
    _free = UtilPrototype("void lookup_table_free( lookup_table )")
    _set_low_limit = UtilPrototype(
        "void lookup_table_set_low_limit( lookup_table , double)")
    _set_high_limit = UtilPrototype(
        "void lookup_table_set_high_limit( lookup_table , double)")
    _has_low_limit = UtilPrototype(
        "bool lookup_table_has_low_limit( lookup_table)")
    _has_high_limit = UtilPrototype(
        "bool lookup_table_has_high_limit( lookup_table)")

    def __init__(self, lower_limit=None, upper_limit=None):
        super(LookupTable, self).__init__(self._alloc())

        if not lower_limit is None:
            self.setLowerLimit(lower_limit)

        if not upper_limit is None:
            self.setUpperLimit(upper_limit)

    def getMaxValue(self):
        self.assertSize(1)
        return self._max()

    def getMinValue(self):
        self.assertSize(1)
        return self._min()

    def getMinArg(self):
        self.assertSize(1)
        return self._arg_min()

    def getMaxArg(self):
        self.assertSize(1)
        return self._arg_max()

    def assertSize(self, N):
        if len(self) < N:
            raise ValueError("Lookup table is too small")

    def __len__(self):
        return self._size()

    @property
    def size(self):
        return len(self)

    # Deprecated properties
    @property
    def max(self):
        return self.getMaxValue()

    @property
    def min(self):
        return self.getMinValue()

    @property
    def arg_max(self):
        return self.getMaxArg()

    @property
    def arg_min(self):
        return self.getMinArg()

    def setLowerLimit(self, value):
        self._set_low_limit(value)

    def hasLowerLimit(self):
        return self._has_low_limit()

    def setUpperLimit(self, value):
        self._set_high_limit(value)

    def hasUpperLimit(self):
        return self._has_high_limit()

    def interp(self, x):
        self.assertSize(2)
        if x < self.getMinArg():
            if not self.hasLowerLimit():
                raise ValueError(
                    "Interpolate argument:%g is outside valid interval: [%g,%g]"
                    % (x, self.getMinArg(), self.getMaxArg()))
        elif x > self.getMaxArg():
            if not self.hasUpperLimit():
                raise ValueError(
                    "Interpolate argument:%g is outside valid interval: [%g,%g]"
                    % (x, self.getMinArg(), self.getMaxArg()))

        return self._interp(x)

    def append(self, x, y):
        self._append(x, y)

    #todo: necessary???
    def __del__(self):
        self._free()

    def free(self):
        self._free()
Esempio n. 21
0
#  the Free Software Foundation, either version 3 of the License, or 
#  (at your option) any later version. 
#   
#  ERT is distributed in the hope that it will be useful, but WITHOUT ANY 
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or 
#  FITNESS FOR A PARTICULAR PURPOSE.   
#   
#  See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html> 
#  for more details. 


import ctypes

from ert.util import UtilPrototype

_free = UtilPrototype("void free(void*)")


def cStringObject(c_ptr):
    """The cStringObject function is a convenience function which creates a
    Python string copy, and discards the underlying C allocated storage
    for strings created with *alloc() functions in C.

    This function should not be invoked directly, only indirectly
    through the prototyping of the symbol 'cstring_obj'.

    """
    if c_ptr is not None:
        python_string = ctypes.c_char_p(c_ptr).value
        _free(c_ptr)
        return python_string
Esempio n. 22
0
class Matrix(BaseCClass):
    _matrix_alloc      = UtilPrototype("void*  matrix_alloc(int, int )" , bind = False)
    _matrix_alloc_identity = UtilPrototype("matrix_obj  matrix_alloc_identity( int )" , bind = False)
    _alloc_transpose   = UtilPrototype("matrix_obj  matrix_alloc_transpose(matrix)")
    _inplace_transpose = UtilPrototype("void        matrix_inplace_transpose(matrix)")
    _copy              = UtilPrototype("matrix_obj  matrix_alloc_copy(matrix)" )
    _sub_copy          = UtilPrototype("matrix_obj  matrix_alloc_sub_copy(matrix, int , int , int , int)" )
    _free              = UtilPrototype("void   matrix_free(matrix)")
    _iget              = UtilPrototype("double matrix_iget( matrix , int , int )")
    _iset              = UtilPrototype("void   matrix_iset( matrix , int , int , double)")
    _set_all           = UtilPrototype("void   matrix_scalar_set( matrix , double)")
    _scale_column      = UtilPrototype("void matrix_scale_column(matrix , int , double)")
    _scale_row         = UtilPrototype("void matrix_scale_row(matrix , int , double)")
    _copy_column       = UtilPrototype("void matrix_copy_column(matrix , matrix , int , int)" , bind = False)
    _rows              = UtilPrototype("int matrix_get_rows(matrix)")
    _columns           = UtilPrototype("int matrix_get_columns(matrix)")
    _equal             = UtilPrototype("bool matrix_equal(matrix, matrix)")
    _pretty_print      = UtilPrototype("void matrix_pretty_print(matrix, char*, char*)")
    _fprint            = UtilPrototype("void matrix_fprintf(matrix, char*, FILE)")
    _random_init       = UtilPrototype("void matrix_random_init(matrix, rng)")
    _dump_csv          = UtilPrototype("void matrix_dump_csv(matrix, char*)")
    
    # Requires BLAS!
    _alloc_matmul      = UtilPrototype("matrix_obj  matrix_alloc_matmul(matrix, matrix)" , bind = False)


    # Requires BLAS!
    @classmethod
    def matmul(cls, m1,m2):
        """
        Will return a new matrix which is matrix product of m1 and m2.
        """
        if m1.columns( ) == m2.rows( ):
            return cls._alloc_matmul( m1, m2)
        else:
            raise ValueError("Matrix size mismathc")
        
    
    def __init__(self, rows, columns, value=0):
        c_ptr = self._matrix_alloc(rows, columns)
        super(Matrix, self).__init__(c_ptr)
        self.setAll(value)

    def copy(self):
        return self._copy( )

    @classmethod
    def identity(cls, dim):
        """Returns a dim x dim identity matrix."""
        if dim < 1:
            raise ValueError('Identity matrix must have positive size, %d not allowed.' % dim)
        return cls._matrix_alloc_identity(dim)

    def subCopy(self, row_offset, column_offset, rows, columns):
        if row_offset < 0 or row_offset >= self.rows():
            raise ValueError("Invalid row offset")

        if column_offset < 0 or column_offset >= self.columns():
            raise ValueError("Invalid column offset")

        if row_offset + rows > self.rows():
            raise ValueError("Invalid rows")

        if column_offset + columns > self.columns():
            raise ValueError("Invalid columns")

        return self._sub_copy( row_offset , column_offset , rows , columns)

    
    def __str__(self):
        s = ""
        for i in range(self.rows()):
            s += "["
            for j in range(self.columns()):
                d = self._iget(i, j)
                s += "%6.3g " % d
            s += "]\n"
        return s

    def __getitem__(self, index_tuple):
        if not 0 <= index_tuple[0] < self.rows():
            raise IndexError("Expected 0 <= %d < %d" % (index_tuple[0], self.rows()))

        if not 0 <= index_tuple[1] < self.columns():
            raise IndexError("Expected 0 <= %d < %d" % (index_tuple[1], self.columns()))

        return self._iget(index_tuple[0], index_tuple[1])

    def __setitem__(self, index_tuple, value):
        if not 0 <= index_tuple[0] < self.rows():
            raise IndexError("Expected 0 <= %d < %d" % (index_tuple[0], self.rows()))

        if not 0 <= index_tuple[1] < self.columns():
            raise IndexError("Expected 0 <= %d < %d" % (index_tuple[1], self.columns()))

        return self._iset(index_tuple[0], index_tuple[1], value)

    def dims(self):
        return self._rows(), self._columns()

    def rows(self):
        """ @rtype: int """
        return self._rows()

    def transpose(self , inplace = False):
        """
        Will transpose the matrix. By default a transposed copy is returned.
        """
        if inplace:
            self._inplace_transpose( )
            return self
        else:
            return self._alloc_transpose( )
    

    def columns(self):
        """ @rtype: int """
        return self._columns()

    def __eq__(self, other):
        assert isinstance(other, Matrix)
        return self._equal(other)

    def scaleColumn(self, column, factor):
        if not 0 <= column < self.columns():
            raise IndexError("Expected column: [0,%d) got:%d" % (self.columns(), column))
        self._scale_column(column, factor)

    def scaleRow(self, row, factor):
        if not 0 <= row < self.rows():
            raise IndexError("Expected row: [0,%d) got:%d" % (self.rows(), row))
        self._scale_row(row, factor)

    def setAll(self, value):
        self._set_all(value)

    def copyColumn(self, target_column, src_column):
        columns = self.columns()
        if not 0 <= src_column < columns:
            raise ValueError("src column:%d invalid" % src_column)

        if not 0 <= target_column < columns:
            raise ValueError("target column:%d invalid" % target_column)

        if src_column != target_column:
            # The underlying C function accepts column copy between matrices.
            Matrix._copy_column(self, self, target_column, src_column)


    def dumpCSV(self , filename):
        self._dump_csv( filename )
        

    def prettyPrint(self, name, fmt="%6.3g"):
        self._pretty_print(name, fmt)

    def fprint(self , fileH , fmt = "%g "):
        """Will print ASCII representation of matrix.

        The fileH argument should point to an open Python
        filehandle. If you supply a fmt string it is important that it
        contains a separator, otherwise you might risk that elements
        overlap in the output. For the matrix:

                  [0 1 2]
              m = [3 4 5]
                  [6 7 8]

        The code:
    
        with open("matrix.txt" , "w") as f:
           m.fprintf( f )

         The file 'matrix.txt' will look like:

         0 1 2 
         3 4 5
         6 7 8

        """
        self._fprint( fmt , CFILE( fileH))

        
    def randomInit(self, rng):
        self._random_init(rng)

    def free(self):
        self._free()
Esempio n. 23
0
class Matrix(BaseCClass):
    _matrix_alloc = UtilPrototype("void*  matrix_alloc(int, int )", bind=False)
    _copy = UtilPrototype("matrix_obj  matrix_alloc_copy(matrix)")
    _free = UtilPrototype("void   matrix_free(matrix)")
    _iget = UtilPrototype("double matrix_iget( matrix , int , int )")
    _iset = UtilPrototype("void   matrix_iset( matrix , int , int , double)")
    _set_all = UtilPrototype("void   matrix_scalar_set( matrix , double)")
    _scale_column = UtilPrototype(
        "void matrix_scale_column(matrix , int , double)")
    _scale_row = UtilPrototype("void matrix_scale_row(matrix , int , double)")
    _copy_column = UtilPrototype(
        "void matrix_copy_column(matrix , matrix , int , int)", bind=False)
    _rows = UtilPrototype("int matrix_get_rows(matrix)")
    _columns = UtilPrototype("int matrix_get_columns(matrix)")
    _equal = UtilPrototype("bool matrix_equal(matrix, matrix)")
    _pretty_print = UtilPrototype(
        "void matrix_pretty_print(matrix, char*, char*)")
    _fprint = UtilPrototype("void matrix_fprintf(matrix, char*, FILE)")
    _random_init = UtilPrototype("void matrix_random_init(matrix, rng)")

    def __init__(self, rows, columns, value=0):
        c_ptr = self._matrix_alloc(rows, columns)
        super(Matrix, self).__init__(c_ptr)
        self.setAll(value)

    def copy(self):
        return self._copy()

    def __str__(self):
        s = ""
        for i in range(self.rows()):
            s += "["
            for j in range(self.columns()):
                d = self._iget(i, j)
                s += "%6.3g " % d
            s += "]\n"
        return s

    def __getitem__(self, index_tuple):
        if not 0 <= index_tuple[0] < self.rows():
            raise IndexError("Expected 0 <= %d < %d" %
                             (index_tuple[0], self.rows()))

        if not 0 <= index_tuple[1] < self.columns():
            raise IndexError("Expected 0 <= %d < %d" %
                             (index_tuple[1], self.columns()))

        return self._iget(index_tuple[0], index_tuple[1])

    def __setitem__(self, index_tuple, value):
        if not 0 <= index_tuple[0] < self.rows():
            raise IndexError("Expected 0 <= %d < %d" %
                             (index_tuple[0], self.rows()))

        if not 0 <= index_tuple[1] < self.columns():
            raise IndexError("Expected 0 <= %d < %d" %
                             (index_tuple[1], self.columns()))

        return self._iset(index_tuple[0], index_tuple[1], value)

    def dims(self):
        return self._rows(), self._columns()

    def rows(self):
        """ @rtype: int """
        return self._rows()

    def columns(self):
        """ @rtype: int """
        return self._columns()

    def __eq__(self, other):
        assert isinstance(other, Matrix)
        return self._equal(other)

    def scaleColumn(self, column, factor):
        if not 0 <= column < self.columns():
            raise IndexError("Expected column: [0,%d) got:%d" %
                             (self.columns(), column))
        self._scale_column(column, factor)

    def scaleRow(self, row, factor):
        if not 0 <= row < self.rows():
            raise IndexError("Expected row: [0,%d) got:%d" %
                             (self.rows(), row))
        self._scale_row(row, factor)

    def setAll(self, value):
        self._set_all(value)

    def copyColumn(self, target_column, src_column):
        columns = self.columns()
        if not 0 <= src_column < columns:
            raise ValueError("src column:%d invalid" % src_column)

        if not 0 <= target_column < columns:
            raise ValueError("target column:%d invalid" % target_column)

        if src_column != target_column:
            # The underlying C function accepts column copy between matrices.
            Matrix._copy_column(self, self, target_column, src_column)

    def prettyPrint(self, name, fmt="%6.3g"):
        self._pretty_print(name, fmt)

    def fprint(self, fileH, fmt="%g "):
        """Will print ASCII representation of matrix.

        The fileH argument should point to an open Python
        filehandle. If you supply a fmt string it is important that it
        contains a separator, otherwise you might risk that elements
        overlap in the output. For the matrix:

                  [0 1 2]
              m = [3 4 5]
                  [6 7 8]

        The code:
    
        with open("matrix.txt" , "w") as f:
           m.fprintf( f )

         The file 'matrix.txt' will look like:

         0 1 2 
         3 4 5
         6 7 8

        """
        self._fprint(fmt, CFILE(fileH))

    def randomInit(self, rng):
        self._random_init(rng)

    def free(self):
        self._free()
Esempio n. 24
0
class Version(object):
    _build_time = UtilPrototype("char* version_get_build_time()")
    _git_commit = UtilPrototype("char* version_get_git_commit()")
    _git_commit_short = UtilPrototype("char* version_get_git_commit_short()")
    _major_version = UtilPrototype("int version_get_major_ert_version()")
    _minor_version = UtilPrototype("int version_get_minor_ert_version()")
    _micro_version = UtilPrototype("char* version_get_micro_ert_version()")
    _is_devel = UtilPrototype("bool version_is_ert_devel_version()")

    def __init__(self, major, minor, micro):
        self.major = major
        self.minor = minor
        self.micro = micro
        try:
            self.micro_int = int(micro)
            self.is_devel = False
        except ValueError:
            self.micro_int = -1
            self.is_devel = True

    def isDevelVersion(self):
        return self.is_devel

    def versionString(self):
        return "%d.%d.%s" % (self.major, self.minor, self.micro)

    def versionTuple(self):
        return self.major, self.minor, self.micro

    def __cmpTuple(self):
        return self.major, self.minor, self.micro_int

    def __str__(self):
        return self.versionString()

    @cmp_method
    def __eq__(self, other):
        return self.versionTuple() == other.versionTuple()

    def __ne__(self, other):
        return not (self == other)

    def __hash__(self):
        return hash(self.versionTuple())

    # All development versions are compared with micro version == -1;
    # i.e.  the two versions Version(1,2,"Alpha") and
    # Version(1,2,"Beta") compare as equal in the >= and <= tests -
    # but not in the == test.

    @cmp_method
    def __ge__(self, other):
        return self.__cmpTuple() >= other.__cmpTuple()

    @cmp_method
    def __lt__(self, other):
        return not (self >= other)

    @cmp_method
    def __le__(self, other):
        return self.__cmpTuple() <= other.__cmpTuple()

    @cmp_method
    def __gt__(self, other):
        return not (self <= other)

    @classmethod
    def getBuildTime(cls):
        return cls._build_time()

    @classmethod
    def getGitCommit(cls, short=False):
        if not short:
            return cls._git_commit()
        else:
            return cls._git_commit_short()

    @classmethod
    def currentVersion(cls):
        major = cls._major_version()
        minor = cls._minor_version()
        micro = cls._micro_version()
        return Version(major, minor, micro)

    @classmethod
    def getVersion(cls):
        v = cls.currentVersion()
        return v.versionString()
Esempio n. 25
0
class UIReturn(BaseCClass):
    TYPE_NAME = "ui_return"
    _alloc = UtilPrototype("void* ui_return_alloc( ui_return_status )",
                           bind=False)
    _free = UtilPrototype("void ui_return_free(ui_return)")
    _get_status = UtilPrototype(
        "ui_return_status ui_return_get_status(ui_return)")
    _get_help = UtilPrototype("char* ui_return_get_help(ui_return)")
    _add_help = UtilPrototype("void ui_return_add_help(ui_return)")
    _add_error = UtilPrototype("void ui_return_add_error(ui_return)")
    _num_error = UtilPrototype("int ui_return_get_error_count(ui_return)")
    _last_error = UtilPrototype("char* ui_return_get_last_error(ui_return)")
    _first_error = UtilPrototype("char* ui_return_get_first_error(ui_return)")
    _iget_error = UtilPrototype("char* ui_return_iget_error(ui_return , int)")

    def __init__(self, status):
        c_ptr = self._alloc(status)
        super(UIReturn, self).__init__(c_ptr)

    def __nonzero__(self):
        if self.status() == UIReturnStatusEnum.UI_RETURN_OK:
            return True
        else:
            return False

    def __len__(self):
        return self._num_error()

    def __getitem__(self, index):
        if isinstance(index, int):
            if 0 <= index < len(self):
                return self._iget_error(index)
            else:
                raise IndexError
        else:
            raise TypeError("Lookup type must be integer")

    def iget_error(self, index):
        return self[index]

    def help_text(self):
        help_text = self._get_help()
        if help_text:
            return help_text
        else:
            return ""

    def add_help(self, help_text):
        self._add_help(help_text)

    def status(self):
        return self._get_status()

    def __assert_error(self):
        if self.status() == UIReturnStatusEnum.UI_RETURN_OK:
            raise ValueError(
                "Can not add error messages to object in state RETURN_OK")

    def add_error(self, error):
        self.__assert_error()
        self._add_error(error)

    def last_error(self):
        self.__assert_error()
        return self._last_error()

    def first_error(self):
        self.__assert_error()
        return self._first_error()

    def free(self):
        self._free()
Esempio n. 26
0
class CTime(BaseCValue):
    TYPE_NAME = "time_t"
    DATA_TYPE = ctypes.c_long
    _timezone = UtilPrototype("char* util_get_timezone()" , bind = False)
    _timegm = UtilPrototype("long util_make_datetime_utc(int, int, int, int, int, int)" , bind = False)

    def __init__(self, value):
        if isinstance(value, int):
            value = value
        elif isinstance(value, CTime):
            value = value.value()
        elif isinstance(value, datetime.datetime):
            value = CTime._timegm(value.second, value.minute, value.hour, value.day, value.month, value.year)
        elif isinstance(value, datetime.date):
            value = CTime._timegm(0, 0, 0, value.day, value.month, value.year)
        else:
            raise NotImplementedError("Can not convert class %s to CTime" % value.__class__)

        super(CTime, self).__init__(value)

    def ctime(self):
        """ @rtype: int """
        return self.value()

    def time(self):
        """Return this time_t as a time.gmtime() object"""
        return time.gmtime(self.value())

    def date(self):
        """Return this time_t as a datetime.date([year, month, day])"""
        return datetime.date(*self.time()[0:3])

    def datetime(self):
        return datetime.datetime(*self.time()[0:6])

    def __str__(self):
        return self.datetime().strftime("%Y-%m-%d %H:%M:%S%z")

    def __ge__(self, other):
        return self > other or self == other

    def __le__(self, other):
        return self < other or self == other

    def __gt__(self, other):
        if isinstance(other, CTime):
            return self.value() > other.value()
        elif isinstance(other, (int, datetime.datetime, datetime.date)):
            return self > CTime(other)
        else:
            raise TypeError("CTime does not support type: %s" % other.__class__)

    def __lt__(self, other):
        if isinstance(other, CTime):
            return self.value() < other.value()
        elif isinstance(other, (int, datetime.datetime, datetime.date)):
            return self < CTime(other)
        else:
            raise TypeError("CTime does not support type: %s" % other.__class__)

    def __ne__(self, other):
        return not self == other

    def __eq__(self, other):
        if isinstance(other, CTime):
            return self.value() == other.value()
        elif isinstance(other, (int, datetime.datetime, datetime.date)):
            return self == CTime(other)
        elif isinstance(other, type(None)):
            return False
        else:
            raise TypeError("CTime does not support type: %s" % other.__class__)

    def __imul__(self, other):
        value = int(self.value() * other)
        self.setValue(value)
        return self

    def __hash__(self):
        return hash(self.value())

    def __iadd__(self, other):
        if isinstance(other, CTime):
            self.setValue(self.value() + other.value())
            return self
        else:
            self.setValue(self.value() + CTime(other).value())
            return self

    def __add__(self, other):
        copy = CTime(self)
        copy += other
        return copy

    def __radd__(self, other):
        return self + other

    def __mul__(self, other):
        copy = CTime(self)
        copy *= other
        return copy

    def __rmul__(self, other):
        return self * other

    def timetuple(self):
        # this function is a requirement for comparing against datetime objects where the CTime is on the right side
        pass

    def __repr__(self):
        return "time_t value: %d [%s]" % (self.value(), str(self))

    @property
    def stripped(self):
        return time.strptime(self, "%Y-%m-%d %H:%M:S%")

    @classmethod
    def timezone(cls):
        """
         Returns the current timezone "in" C
         @rtype: str
        """
        return CTime._timezone()
Esempio n. 27
0
class TimeVector(VectorTemplate):
    TYPE_NAME = "time_t_vector"
    default_format = "%d"

    _alloc = UtilPrototype("void*   time_t_vector_alloc(int, time_t )",
                           bind=False)
    _alloc_copy = UtilPrototype(
        "time_t_vector_obj time_t_vector_alloc_copy(time_t_vector )")
    _strided_copy = UtilPrototype(
        "time_t_vector_obj time_t_vector_alloc_strided_copy(time_t_vector , int , int , int)"
    )
    _free = UtilPrototype("void   time_t_vector_free( time_t_vector )")
    _iget = UtilPrototype("time_t   time_t_vector_iget( time_t_vector , int )")
    _safe_iget = UtilPrototype(
        "time_t   time_t_vector_safe_iget( time_t_vector , int )")
    _iset = UtilPrototype(
        "time_t   time_t_vector_iset( time_t_vector , int , time_t)")
    _size = UtilPrototype("int      time_t_vector_size( time_t_vector )")
    _append = UtilPrototype(
        "void     time_t_vector_append( time_t_vector , time_t )")
    _idel_block = UtilPrototype(
        "void     time_t_vector_idel_block( time_t_vector , int , int )")
    _idel = UtilPrototype("void     time_t_vector_idel( time_t_vector , int )")
    _pop = UtilPrototype("time_t   time_t_vector_pop( time_t_vector )")
    _lshift = UtilPrototype(
        "void     time_t_vector_lshift( time_t_vector , int )")
    _rshift = UtilPrototype(
        "void     time_t_vector_rshift( time_t_vector , int )")
    _insert = UtilPrototype(
        "void     time_t_vector_insert( time_t_vector , int , time_t)")
    _fprintf = UtilPrototype(
        "void     time_t_vector_fprintf( time_t_vector , FILE , char* , char*)"
    )
    _sort = UtilPrototype("void     time_t_vector_sort( time_t_vector )")
    _rsort = UtilPrototype("void     time_t_vector_rsort( time_t_vector )")
    _reset = UtilPrototype("void     time_t_vector_reset( time_t_vector )")
    _set_read_only = UtilPrototype(
        "void     time_t_vector_set_read_only( time_t_vector , bool )")
    _get_read_only = UtilPrototype(
        "bool     time_t_vector_get_read_only( time_t_vector )")
    _get_max = UtilPrototype("time_t   time_t_vector_get_max( time_t_vector )")
    _get_min = UtilPrototype("time_t   time_t_vector_get_min( time_t_vector )")
    _get_max_index = UtilPrototype(
        "int      time_t_vector_get_max_index( time_t_vector , bool)")
    _get_min_index = UtilPrototype(
        "int      time_t_vector_get_min_index( time_t_vector , bool)")
    _shift = UtilPrototype(
        "void     time_t_vector_shift( time_t_vector , time_t )")
    _scale = UtilPrototype(
        "void     time_t_vector_scale( time_t_vector , time_t )")
    _div = UtilPrototype(
        "void     time_t_vector_div( time_t_vector , time_t )")
    _inplace_add = UtilPrototype(
        "void     time_t_vector_inplace_add( time_t_vector , time_t_vector )")
    _inplace_mul = UtilPrototype(
        "void     time_t_vector_inplace_mul( time_t_vector , time_t_vector )")
    _assign = UtilPrototype(
        "void     time_t_vector_set_all( time_t_vector , time_t)")
    _memcpy = UtilPrototype(
        "void     time_t_vector_memcpy(time_t_vector , time_t_vector )")
    _set_default = UtilPrototype(
        "void     time_t_vector_set_default( time_t_vector , time_t)")
    _get_default = UtilPrototype(
        "time_t   time_t_vector_get_default( time_t_vector )")
    _element_size = UtilPrototype(
        "int      time_t_vector_element_size( time_t_vector )")

    _permute = UtilPrototype(
        "void time_t_vector_permute(time_t_vector, permutation_vector)")
    _sort_perm = UtilPrototype(
        "permutation_vector_obj time_t_vector_alloc_sort_perm(time_t_vector)")
    _rsort_perm = UtilPrototype(
        "permutation_vector_obj time_t_vector_alloc_rsort_perm(time_t_vector)")
    _contains = UtilPrototype(
        "bool time_t_vector_contains(time_t_vector, time_t)")
    _select_unique = UtilPrototype(
        "void time_t_vector_select_unique(time_t_vector)")
    _element_sum = UtilPrototype("time_t time_t_vector_sum(time_t_vector)")
    _count_equal = UtilPrototype(
        "int time_t_vector_count_equal(time_t_vector, time_t)")
    _init_range = UtilPrototype(
        "void time_t_vector_init_range(time_t_vector, time_t , time_t , time_t)"
    )

    def __init__(self, default_value=None, initial_size=0):
        if default_value is None:
            super(TimeVector, self).__init__(CTime(0), initial_size)
        else:
            try:
                default = CTime(default_value)
            except:
                raise ValueError(
                    "default value invalid - must be type ctime() or date/datetime"
                )

            super(TimeVector, self).__init__(default, initial_size)

    @classmethod
    def parseTimeUnit(cls, deltaString):
        deltaRegexp = re.compile("(?P<num>\d*)(?P<unit>[dmy])", re.IGNORECASE)
        matchObj = deltaRegexp.match(deltaString)
        if matchObj:
            try:
                num = int(matchObj.group("num"))
            except:
                num = 1

            timeUnit = matchObj.group("unit").lower()
            return num, timeUnit
        else:
            raise TypeError(
                "The delta string must be on form \'1d\', \'2m\', \'Y\' for one day, two months or one year respectively"
            )

    def __str__(self):
        """
        Returns string representantion of vector.
        """
        string_list = []
        for d in self:
            string_list.append("%s" % d)

        return str(string_list)

    def append(self, value):
        self._append(CTime(value))

    def __contains__(self, value):
        return self._contains(CTime(value))

    def nextTime(self, num, timeUnit):
        currentTime = self[-1].datetime()
        hour = currentTime.hour
        minute = currentTime.minute
        second = currentTime.second

        if timeUnit == "d":
            td = datetime.timedelta(days=num)
            currentTime += td
        else:
            day = currentTime.day
            month = currentTime.month
            year = currentTime.year

            if timeUnit == "y":
                year += num
            else:
                month += num - 1
                (deltaYear, newMonth) = divmod(month, 12)
                month = newMonth + 1
                year += deltaYear
            currentTime = datetime.datetime(year, month, day, hour, minute,
                                            second)

        return currentTime

    def appendTime(self, num, timeUnit):
        next = self.nextTime(num, timeUnit)
        self.append(CTime(next))

    @classmethod
    def createRegular(cls, start, end, deltaString):
        """
        The last element in the vector will be <= end; i.e. if the
        question of whether the range is closed in the upper end
        depends on the commensurability of the [start,end] interval
        and the delta:

        createRegular(0 , 10 , delta=3) => [0,3,6,9]
        createRegular(0 , 10 , delta=2) => [0,2,4,6,8,10]
        """
        start = CTime(start)
        end = CTime(end)
        if start > end:
            raise ValueError("The time interval is invalid start is after end")

        (num, timeUnit) = cls.parseTimeUnit(deltaString)

        timeVector = TimeVector()
        currentTime = start
        while currentTime <= end:
            ct = CTime(currentTime)
            timeVector.append(ct)
            currentTime = timeVector.nextTime(num, timeUnit)

        return timeVector

    def getDataPtr(self):
        raise NotImplementedError(
            "The getDataPtr() function is not implemented for time_t vectors")
Esempio n. 28
0
class SchemaItem(BaseCClass):
    TYPE_NAME = "schema_item"

    _alloc = ConfigPrototype("void* config_schema_item_alloc( char* , bool )", bind=False)
    _free = ConfigPrototype("void config_schema_item_free( schema_item )")
    _iget_type = ConfigPrototype("config_content_type_enum config_schema_item_iget_type( schema_item, int)")
    _iset_type = ConfigPrototype("void config_schema_item_iset_type( schema_item , int , config_content_type_enum)")
    _set_argc_minmax = ConfigPrototype("void config_schema_item_set_argc_minmax( schema_item , int , int)")
    _add_alternative = ConfigPrototype("void config_schema_item_add_indexed_alternative(schema_item , int , char*)")
    _set_deprecated = ConfigPrototype("void config_schema_item_set_deprecated(schema_item ,  char*)")
    _valid_string = ConfigPrototype("bool config_schema_item_valid_string(config_content_type_enum ,  char*)", bind = False)
    _sscanf_bool = UtilPrototype("bool util_sscanf_bool( char* , bool*)", bind = False)
    
    def __init__(self, keyword, required=False):
        c_ptr = self._alloc(keyword, required)
        super(SchemaItem, self).__init__(c_ptr)

    @classmethod
    def validString(cls , value_type, value):
        return cls._valid_string( value_type , value )

        
    
    @classmethod
    def convert(cls, value_type, value_string):
        if cls.validString(value_type , value_string):
            if value_type == ContentTypeEnum.CONFIG_INT:
                return int(value_string)

            if value_type == ContentTypeEnum.CONFIG_FLOAT:
                return float(value_string)

            if value_type == ContentTypeEnum.CONFIG_BOOL:
                value = ctypes.c_bool()
                SchemaItem._sscanf_bool( value_string , ctypes.byref( value ))
                return value.value

            return value_string
        else:
            raise ValueError("Invalid string value: %s" % value_string)

        

    def iget_type( self, index):
        """ @rtype: ContentTypeEnum """
        return self._iget_type(index)

    def iset_type( self, index, schema_type ):
        """
        @type schema_type: ContentTypeEnum
        """
        assert isinstance(schema_type, ContentTypeEnum)
        self._iset_type(index, schema_type)

    def set_argc_minmax(self, minimum, maximum):
        self._set_argc_minmax(minimum, maximum)


    def initSelection(self , index , alternatives):
        for alt in alternatives:
            self.addAlternative( index , alt )

            
    def addAlternative(self , index , alt):
        self._add_alternative( index , alt )

    def setDeprecated(self , msg):
        """This method can be used to mark this item as deprecated.

        If the deprecated item is used in a configuration file the
        @msg will be added to the warnings of the ConfigContent
        object,
        """
        self._set_deprecated( msg )
        
        
    def free(self):
        self._free()
Esempio n. 29
0
class StringList(BaseCClass):
    TYPE_NAME = "stringlist"

    _alloc      = UtilPrototype("void* stringlist_alloc_new( )", bind = False)
    _free       = UtilPrototype("void stringlist_free(stringlist )")
    _append     = UtilPrototype("void stringlist_append_copy(stringlist , char* )")
    _iget       = UtilPrototype("char* stringlist_iget(stringlist , int )")
    _front      = UtilPrototype("char* stringlist_front( stringlist )")
    _back       = UtilPrototype("char* stringlist_back( stringlist )")
    _iget_copy  = UtilPrototype("char* stringlist_iget_copy(stringlist, int)")
    _iset       = UtilPrototype("void  stringlist_iset_copy( stringlist , int , char* )")
    _get_size   = UtilPrototype("int  stringlist_get_size( stringlist )")
    _contains   = UtilPrototype("bool stringlist_contains(stringlist , char*)")
    _equal      = UtilPrototype("bool stringlist_equal(stringlist , stringlist)")
    _sort       = UtilPrototype("void stringlist_python_sort( stringlist , int)")
    _pop        = UtilPrototype("char* stringlist_pop(stringlist)")
    _last       = UtilPrototype("char* stringlist_get_last(stringlist)")
    _find_first = UtilPrototype("int stringlist_find_first(stringlist, char*)")

    def __init__(self, initial=None):
        """
        Creates a new stringlist instance.
        
        Creates a new stringlist instance. The optional argument
        @initial should be an iterable of strings which will be the
        initial content of the StringList; the content will be copied
        from the initial list:

            S = StringList( initial = ["My" , "name" , "is", "John" , "Doe"] )

        If an element in the @initial argument is not a string the
        TypeError exception will be raised.

        If c_ptr argument is different from None, that should refer to
        an already created stringlist instance; this Python will take
        ownership of the underlying object.
        """

        c_ptr = self._alloc()
        super(StringList, self).__init__(c_ptr)

        if initial:
            for s in initial:
                if isinstance(s, StringType):
                    self.append(s)
                else:
                    raise TypeError("Item: %s not a string" % s)


    def __eq__(self , other):
        if len(self) == len(other):
            if isinstance( other , StringList):
                return self._equal(other)
            else:
                equal = True
                for index,s2 in enumerate(other):
                    if self[index] != s2:
                        equal = False
                        break
                return equal
        else:
            return False


    def __setitem__(self, index, value):
        if isinstance(index, IntType):
            length = len(self)
            if index < 0:
                # Will only wrap backwards once
                index = len(self) + index

            if index < 0 or index >= length:
                raise IndexError("index must be in range %d <= %d < %d" % (0, index, len(self)))
            if isinstance(value, StringType):
                self._iset(index, value)
            else:
                raise TypeError("Item: %s not string type" % value)


    def __getitem__(self, index):
        """
        Implements [] read operator on the stringlist.
        
        The __getitem__ method supports negative, i.e. from the right,
        indexing; but not slices.
        """
        if isinstance(index, IntType):
            length = len(self)
            if index < 0:
                index += length
            if index < 0 or index >= length:
                raise IndexError("index must be in range %d <= %d < %d" % (0, index, len(self)))
            else:
                return self._iget(index)
        else:
            raise TypeError("Index should be integer type")

    def __contains__(self, s):
        """
        Implements the 'in' operator.

        The 'in' check is based on string equality.
        """
        return self._contains(s)


    def __iadd__(self , other):
        if isinstance(other , str):
            raise TypeError("Can not add strings with + - use append()")
        for s in other:
            self.append( s )
        return self

    
    def __add__(self , other):
        copy = StringList( initial = self )
        copy += other
        return copy


    def __ior__(self , other):
        if isinstance(other , str):
            raise TypeError("Can not | with string.")
        for s in other:
            if not s in self:
                self.append( s )
        return self
    
                
    def __or__(self , other):
        copy = StringList( initial = self )
        copy |= other
        return copy

    
    
    def contains(self, s):
        """
        Checks if the list contains @s.

        Functionality also available through the 'in' builtin in
        Python.
        """
        return s in self


    def __len__(self):
        """
        The length of the list - used to support builtin len().
        """
        return self._get_size( )


    def __str__(self):
        """
        String representation of list; used when calling print."
        """
        buffer = "["
        length = len(self)
        for i in range(length):
            if i == length - 1:
                buffer += "\'%s\'" % self[i]
            else:
                buffer += "\'%s\'," % self[i]
        buffer += "]"
        return buffer


    def pop(self):
        """
        Will remove the last element from the list and return it. 
        
        Will raise IndexError if list is empty.
        """
        if len(self):
            return self._pop()
        else:
            raise IndexError("pop() failed - the list is empty")


    def append(self, s):
        """
        Appends a new string @s to list. If the input argument is not a
        string the string representation will be appended.
        """
        if isinstance(s, StringType):
            self._append(s)
        else:
            self._append(str(s))
            

    @property
    def strings(self):
        """
        The strings in as a normal Python list of strings.

        The content is copied, so the StringList() instance can safely go
        out of scope after the call has completed. Hmmmm - is that true?
        """
        slist = []
        for s in self:
            slist.append(s)
        return slist

    @property
    def last(self):
        """
        Will return the last element in list. Raise IndexError if empty.
        """
        if len(self) > 0:
            return self._last()
        else:
            raise IndexError("The list is empty")


    def sort(self, cmp_flag=0):
        """
        Will sort the list inplace.

        The string comparison can be altered with the value of the
        optional cmp_flag parameter:
        
             0 : Normal strcmp() string comparison
             1 : util_strcmp_int() string comparison
             2 : util_strcmp_float() string comparison

        """
        self._sort(cmp_flag)

    def index(self, value):
        """ @rtype: int """
        assert isinstance(value, str)
        return self._find_first( value)

    def free(self):
        self._free()


    def front(self):
        if len(self) > 0:
            return self._front()
        else:
            raise IndexError

    def back(self):
        if len(self) > 0:
            return self._back()
        else:
            raise IndexError
Esempio n. 30
0
class BoolVector(VectorTemplate):
    default_format = "%8d"

    _alloc = UtilPrototype("void*  bool_vector_alloc( int , bool )",
                           bind=False)
    _create_active_mask = UtilPrototype(
        "bool_vector_obj string_util_alloc_active_mask( char* )", bind=False)
    _active_list = UtilPrototype(
        "int_vector_obj bool_vector_alloc_active_list(bool_vector)",
        bind=False)
    _alloc_copy = UtilPrototype(
        "bool_vector_obj bool_vector_alloc_copy( bool_vector )")
    _update_active_mask = UtilPrototype(
        "bool string_util_update_active_mask(char*, bool_vector)", bind=False)

    _strided_copy = UtilPrototype(
        "bool_vector_obj bool_vector_alloc_strided_copy( bool_vector , int , int , int)"
    )
    _free = UtilPrototype("void   bool_vector_free( bool_vector )")
    _iget = UtilPrototype("bool   bool_vector_iget( bool_vector , int )")
    _safe_iget = UtilPrototype(
        "bool   bool_vector_safe_iget( bool_vector , int )")
    _iset = UtilPrototype("void   bool_vector_iset( bool_vector , int , bool)")
    _size = UtilPrototype("int    bool_vector_size( bool_vector )")
    _append = UtilPrototype("void   bool_vector_append( bool_vector , bool )")
    _idel_block = UtilPrototype(
        "void   bool_vector_idel_block( bool_vector , bool , bool )")
    _idel = UtilPrototype("void   bool_vector_idel( bool_vector , int )")
    _pop = UtilPrototype("bool   bool_vector_pop( bool_vector )")
    _lshift = UtilPrototype("void   bool_vector_lshift( bool_vector , int )")
    _rshift = UtilPrototype("void   bool_vector_rshift( bool_vector , int )")
    _insert = UtilPrototype(
        "void   bool_vector_insert( bool_vector , int , bool)")
    _fprintf = UtilPrototype(
        "void   bool_vector_fprintf( bool_vector , FILE , char* , char*)")
    _sort = UtilPrototype("void   bool_vector_sort( bool_vector )")
    _rsort = UtilPrototype("void   bool_vector_rsort( bool_vector )")
    _reset = UtilPrototype("void   bool_vector_reset( bool_vector )")
    _set_read_only = UtilPrototype(
        "void   bool_vector_set_read_only( bool_vector , bool )")
    _get_read_only = UtilPrototype(
        "bool   bool_vector_get_read_only( bool_vector )")
    _get_max = UtilPrototype("bool   bool_vector_get_max( bool_vector )")
    _get_min = UtilPrototype("bool   bool_vector_get_min( bool_vector )")
    _get_max_index = UtilPrototype(
        "int    bool_vector_get_max_index( bool_vector , bool)")
    _get_min_index = UtilPrototype(
        "int    bool_vector_get_min_index( bool_vector , bool)")
    _shift = UtilPrototype("void   bool_vector_shift( bool_vector , bool )")
    _scale = UtilPrototype("void   bool_vector_scale( bool_vector , bool )")
    _div = UtilPrototype("void   bool_vector_div( bool_vector , bool )")
    _inplace_add = UtilPrototype(
        "void   bool_vector_inplace_add( bool_vector , bool_vector )")
    _inplace_mul = UtilPrototype(
        "void   bool_vector_inplace_mul( bool_vector , bool_vector )")
    _assign = UtilPrototype("void   bool_vector_set_all( bool_vector , bool)")
    _memcpy = UtilPrototype(
        "void   bool_vector_memcpy(bool_vector , bool_vector )")
    _set_default = UtilPrototype(
        "void   bool_vector_set_default( bool_vector , bool)")
    _get_default = UtilPrototype(
        "bool   bool_vector_get_default( bool_vector )")
    _element_size = UtilPrototype(
        "int    bool_vector_element_size( bool_vector )")

    _permute = UtilPrototype(
        "void bool_vector_permute(bool_vector, permutation_vector)")
    _sort_perm = UtilPrototype(
        "permutation_vector_obj bool_vector_alloc_sort_perm(bool_vector)")
    _rsort_perm = UtilPrototype(
        "permutation_vector_obj bool_vector_alloc_rsort_perm(bool_vector)")

    _contains = UtilPrototype("bool bool_vector_contains(bool_vector, bool)")
    _select_unique = UtilPrototype(
        "void bool_vector_select_unique(bool_vector)")
    _element_sum = UtilPrototype("bool bool_vector_sum(bool_vector)")
    _get_data_ptr = UtilPrototype("bool* bool_vector_get_ptr(bool_vector)")
    _count_equal = UtilPrototype(
        "int bool_vector_count_equal(bool_vector, bool)")

    def __init__(self, default_value=False, initial_size=0):
        super(BoolVector, self).__init__(default_value, initial_size)

    def count(self, value=True):
        """ @rtype: int """
        return self._count_equal(self, value)

    @classmethod
    def createActiveMask(cls, range_string):
        """
        Will create a BoolVector instance with the values from @range_string.

        The range_string input should be of the type "1,3-5,9,17",
        i.e. integer values separated by commas, and dashes to
        represent ranges. If the input string contains ANY invalid
        characters the returned active list will be empty:

           "1,4-7,10"  =>  {F,T,F,F,T,T,T,T,F,F,T}
           "1,4-7,10X" =>  {}
        
        The empty list will evaluate to false
        @rtype: BoolVector
        """
        return cls._create_active_mask(range_string)

    @classmethod
    def active_mask(cls, range_string):
        """
        Will create a BoolVector instance with the values from @range_string.

        The range_string input should be of the type "1,3-5,9,17",
        i.e. integer values separated by commas, and dashes to
        represent ranges. If the input string contains ANY invalid
        characters the returned active list will be empty:

           "1,4-7,10"  =>  {F,T,F,F,T,T,T,T,F,F,T}
           "1,4-7,10X" =>  {}

        The empty list will evaluate to false
        @rtype: BoolVector
        """
        warnings.warn(
            "The active_mask(cls, rangs_string) method has been renamed: createActiveMask(cls, rangs_string)",
            DeprecationWarning)
        return cls._create_active_mask(range_string)

    def updateActiveMask(self, range_string):
        """
        Updates a bool vector based on a range string.
        @type range_string: str
        @type bool_vector: BoolVector
        @rtype: bool
        """
        return self._update_active_mask(range_string, self)

    @classmethod
    def createFromList(cls, size, source_list):
        """
        Allocates a bool vector from a Python list of indexes
        @rtype: BoolVector
        """
        bool_vector = BoolVector(False, size)

        for index in source_list:
            index = int(index)
            bool_vector[index] = True

        return bool_vector

    def createActiveList(self):
        """ @rtype: ert.util.IntVector """
        return self._active_list(self)