<commit_before>from __future__ import print_function

import ctypes
import sys

class PyObject(ctypes.Structure):
    '''ctypes representation of PyObject_HEAD'''
    
    _fields_ = (
        ('ob_refcnt', ctypes.c_size_t),
        ('ob_type', ctypes.c_void_p),
    )

class SofaPySPtr(ctypes.Structure):
    '''ctypes representation of sofa PySPtr'''
    _fields_ = (
        ('head', PyObject),
        ('obj', ctypes.c_void_p),
    )

class DataPointer(ctypes.Structure):
    '''result type for c function get_data_pointer'''
    _fields_ = (
        ('ptr', ctypes.c_void_p),
        ('size', ctypes.c_uint),
    )
    
    
def sofa_pointer( obj ):
    '''returns pointer to wrapped sofa object as a ctype.c_void_p'''

    # obj -> pyobject*
    pyobj = ctypes.c_void_p(id(obj))

    # pyobject* -> pysptr
    pysptr = ctypes.cast( pyobj, ctypes.POINTER(SofaPySPtr)).contents

    # obtain wrapped object
    sofaobj = pysptr.obj
    
    return sofaobj


def load_dll():
    '''load symbols from compliant dll. you need to load compliant plugin first.'''

    ext = 'so'
    prefix = 'lib'

    import platform
    system = platform.system()

    if system == 'Windows':
        ext = 'dll'
    elif system == 'Darwin':
        ext = 'dylib'

    dll_name = '{0}Compliant.{1}'.format(prefix, ext)

    global dll
    try:
        dll = ctypes.CDLL(dll_name)
    except:
        # try debug
        dll_name = '{0}Compliant_d.{1}'.format(prefix, ext)
        dll = ctypes.CDLL(dll_name)

    # dll.get_data_pointer.restype = DataPointer
    # dll.get_data_pointer.argtypes = (ctypes.c_void_p, )

    global py_callback_type
    py_callback_type = ctypes.CFUNCTYPE(None, ctypes.c_int)

    dll.set_py_callback.restype = None
    dll.set_py_callback.argtypes = (ctypes.c_void_p, py_callback_type)



# TODO add more basic types
# check that type sizes are equivalent with c++ sizes
ctypeFromName = {
    'double': ctypes.c_double,
    'float': ctypes.c_float,
    'bool': ctypes.c_bool,
    'char': ctypes.c_char,
    'uchar': ctypes.c_ubyte,
    'short': ctypes.c_short,
    'ushort': ctypes.c_ushort,
    'int': ctypes.c_int,
    'uint': ctypes.c_uint,
    'long': ctypes.c_long,
    'ulong': ctypes.c_ulong,
}


import numpy as np
from numpy import ctypeslib

def as_numpy( data ):
    '''maps data content as a numpy array'''

    ptr, shape, typename = data.getValueVoidPtr()

    type = ctypeFromName.get(typename,None)
    if not type: raise Exception("can't map data of type " + typename)

    array = ctypes.cast( ctypes.c_void_p(ptr), ctypes.POINTER(type))
    return ctypeslib.as_array(array, shape )






# convenience
def numpy_data(obj, name):
    data = obj.findData(name)
    return as_numpy(data)




def numpy_property(*name):

    @property
    def res(self):<commit_msg>[Compliant.tool] an other "as_numpy" function !<commit_after>from __future__ import print_function

import ctypes
import sys

class PyObject(ctypes.Structure):
    '''ctypes representation of PyObject_HEAD'''
    
    _fields_ = (
        ('ob_refcnt', ctypes.c_size_t),
        ('ob_type', ctypes.c_void_p),
    )

class SofaPySPtr(ctypes.Structure):
    '''ctypes representation of sofa PySPtr'''
    _fields_ = (
        ('head', PyObject),
        ('obj', ctypes.c_void_p),
    )

class DataPointer(ctypes.Structure):
    '''result type for c function get_data_pointer'''
    _fields_ = (
        ('ptr', ctypes.c_void_p),
        ('size', ctypes.c_uint),
    )
    
    
def sofa_pointer( obj ):
    '''returns pointer to wrapped sofa object as a ctype.c_void_p'''

    # obj -> pyobject*
    pyobj = ctypes.c_void_p(id(obj))

    # pyobject* -> pysptr
    pysptr = ctypes.cast( pyobj, ctypes.POINTER(SofaPySPtr)).contents

    # obtain wrapped object
    sofaobj = pysptr.obj
    
    return sofaobj


def load_dll():
    '''load symbols from compliant dll. you need to load compliant plugin first.'''

    ext = 'so'
    prefix = 'lib'

    import platform
    system = platform.system()

    if system == 'Windows':
        ext = 'dll'
    elif system == 'Darwin':
        ext = 'dylib'

    dll_name = '{0}Compliant.{1}'.format(prefix, ext)

    global dll
    try:
        dll = ctypes.CDLL(dll_name)
    except:
        # try debug
        dll_name = '{0}Compliant_d.{1}'.format(prefix, ext)
        dll = ctypes.CDLL(dll_name)

    # dll.get_data_pointer.restype = DataPointer
    # dll.get_data_pointer.argtypes = (ctypes.c_void_p, )

    global py_callback_type
    py_callback_type = ctypes.CFUNCTYPE(None, ctypes.c_int)

    dll.set_py_callback.restype = None
    dll.set_py_callback.argtypes = (ctypes.c_void_p, py_callback_type)



# TODO add more basic types
# check that type sizes are equivalent with c++ sizes
ctypeFromName = {
    'double': ctypes.c_double,
    'float': ctypes.c_float,
    'bool': ctypes.c_bool,
    'char': ctypes.c_char,
    'uchar': ctypes.c_ubyte,
    'short': ctypes.c_short,
    'ushort': ctypes.c_ushort,
    'int': ctypes.c_int,
    'uint': ctypes.c_uint,
    'long': ctypes.c_long,
    'ulong': ctypes.c_ulong,
}


import numpy as np
from numpy import ctypeslib

def as_numpy( data ):
    '''maps data content as a numpy array'''

    Sofa.msg_deprecated("Compliant.tools", "as_numpy is deprecated, use SofaPython.SofaNumpy instead")

    ptr, shape, typename = data.getValueVoidPtr()

    type = ctypeFromName.get(typename,None)
    if not type: raise Exception("can't map data of type " + typename)

    array = ctypes.cast( ctypes.c_void_p(ptr), ctypes.POINTER(type))
    return ctypeslib.as_array(array, shape )






# convenience
def numpy_data(obj, name):
    data = obj.findData(name)
    return as_numpy(data)




def numpy_property(*name):

    @property
    def res(self):
{'fraction_non_alphanumeric': 0.12238128491620112, 'fraction_numerical': 0.0013966480446927375, 'mean_word_length': 3.000698324022346, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7554627', 'n_tokens_mistral': 2085, 'n_tokens_neox': 2014, 'n_words': 463}