Completed
Push — master ( a74470...c8bc69 )
by Andrei
58s
created

package_builder.__create_package()   A

Complexity

Conditions 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
"""!
2
3
@brief Pyclustering package that is used to exchange between python core and 'ccore'.
4
5
@authors Andrei Novikov ([email protected])
6
@date 2014-2018
7
@copyright GNU Public License
8
9
@cond GNU_PUBLIC_LICENSE
10
    PyClustering is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
    
15
    PyClustering is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
    
20
    You should have received a copy of the GNU General Public License
21
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
@endcond
23
24
"""
25
26
27
28
from ctypes import *;
0 ignored issues
show
Unused Code introduced by
memmove was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint32 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
byref was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int64 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
pydll was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
DEFAULT_MODE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_wchar_p was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
CDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
PyDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_byte was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
string_at was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint16 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
RTLD_GLOBAL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
RTLD_LOCAL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
LibraryLoader was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
cdll was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_longlong was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
ArgumentError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int32 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_longdouble was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
sizeof was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_wchar was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
windll was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
BigEndianStructure was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint64 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
create_string_buffer was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
DllCanUnloadNow was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
py_object was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
resize was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_voidp was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
pythonapi was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
oledll was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ssize_t was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
WinError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_char_p was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
Union was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
GetLastError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ulonglong was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
FormatError was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
LittleEndianStructure was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
set_last_error was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ubyte was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
alignment was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_bool was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_short was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
kind was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
set_errno was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
addressof was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
CFUNCTYPE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int16 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
HRESULT was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
SetPointerType was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
create_unicode_buffer was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_char was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
WinDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
WINFUNCTYPE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_uint8 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
get_last_error was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
get_errno was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
ARRAY was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
memset was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
PYFUNCTYPE was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
Array was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_int8 was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
wstring_at was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_buffer was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
OleDLL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
c_ushort was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
DllGetClassObject was imported with wildcard, but is not used.
Loading history...
29
30
import collections;
31
import numpy;
0 ignored issues
show
Configuration introduced by
The import numpy could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
32
33
34
35
class pyclustering_package(Structure):
36
    """!
37
    @brief pyclustering_package description in memory.
38
    @details Represents following C++ structure:
39
    
40
            typedef struct pyclustering_package {
41
                std::size_t      size;
42
                unsigned int     type;
43
                void *           data;
44
            }
45
    """
46
    
47
    _fields_ = [ ("size", c_size_t),
48
                 ("type", c_uint),
49
                 ("data", POINTER(c_void_p)) ];
50
51
52
53
class pyclustering_type_data:
54
    """!
55
    @brief Contains constants that defines type of package.
56
    
57
    """
58
    
59
    PYCLUSTERING_TYPE_INT               = 0x00;
60
    PYCLUSTERING_TYPE_UNSIGNED_INT      = 0x01;
61
    PYCLUSTERING_TYPE_FLOAT             = 0x02;
62
    PYCLUSTERING_TYPE_DOUBLE            = 0x03;
63
    PYCLUSTERING_TYPE_LONG              = 0x04;
64
    PYCLUSTERING_TYPE_UNSIGNED_LONG     = 0x05;
65
    PYCLUSTERING_TYPE_LIST              = 0x06;
66
    PYCLUSTERING_TYPE_SIZE_T            = 0x07;
67
    PYCLUSTERING_TYPE_UNDEFINED         = 0x08;
68
69
    __CTYPE_PYCLUSTERING_MAP = { 
70
        c_int                           : PYCLUSTERING_TYPE_INT,
71
        c_uint                          : PYCLUSTERING_TYPE_UNSIGNED_INT,
72
        c_float                         : PYCLUSTERING_TYPE_FLOAT,
73
        c_double                        : PYCLUSTERING_TYPE_DOUBLE,
74
        c_long                          : PYCLUSTERING_TYPE_LONG,
75
        c_ulong                         : PYCLUSTERING_TYPE_UNSIGNED_LONG,
76
        POINTER(pyclustering_package)   : PYCLUSTERING_TYPE_LIST,
77
        c_size_t                        : PYCLUSTERING_TYPE_SIZE_T,
78
        None                            : PYCLUSTERING_TYPE_UNDEFINED
79
    }
80
81
    __PYCLUSTERING_CTYPE_MAP = {
82
        PYCLUSTERING_TYPE_INT             : c_int,
83
        PYCLUSTERING_TYPE_UNSIGNED_INT    : c_uint,
84
        PYCLUSTERING_TYPE_FLOAT           : c_float,
85
        PYCLUSTERING_TYPE_DOUBLE          : c_double,
86
        PYCLUSTERING_TYPE_LONG            : c_long,
87
        PYCLUSTERING_TYPE_UNSIGNED_LONG   : c_ulong,
88
        PYCLUSTERING_TYPE_LIST            : POINTER(pyclustering_package),
89
        PYCLUSTERING_TYPE_SIZE_T          : c_size_t,
90
        PYCLUSTERING_TYPE_UNDEFINED       : None
91
    }
92
93
    @staticmethod
94
    def get_ctype(pyclustering_package_type):
95
        """!
96
        @return (ctype) Return ctype that corresponds to pyclustering type data.
97
        
98
        """
99
        return pyclustering_type_data.__PYCLUSTERING_CTYPE_MAP[pyclustering_package_type];
100
101
    @staticmethod
102
    def get_pyclustering_type(data_ctype):
103
        """!
104
        @return (unit) Return pyclustering data type that corresponds to ctype.
105
        
106
        """
107
        return pyclustering_type_data.__CTYPE_PYCLUSTERING_MAP[data_ctype];
108
109
110
class package_builder:
111
    """!
112
    @brief Package builder provides service to create 'pyclustering_package' from data that is stored in 'list' container.
113
114
    """
115
    def __init__(self, dataset, c_data_type):
116
        """!
117
        @brief Initialize package builder object by dataset.
118
        
119
        @param[in] dataset (list): Data that should be packed in 'pyclustering_package'.
120
        @param[in] c_data_type (ctype.type): If specified than specified data type is used for data storing in package. 
121
        
122
        """
123
        self.__dataset = dataset;
124
        self.__c_data_type = c_data_type;
125
126
127
    def create(self):
128
        """!
129
        @brief Performs packing procedure of the data to the package.
130
        
131
        @return (pointer) ctype-pointer to pyclustering package.
132
        
133
        """
134
        return self.__create_package(self.__dataset);
135
136
137
    def __is_container_type(self, value):
138
        return isinstance(value, collections.Iterable);
139
140
141
    def __get_type(self, pyclustering_data_type):
142
        if (self.__c_data_type is None):
143
            return pyclustering_data_type;
144
        
145
        return self.__c_data_type;
146
147
148
    def __create_package(self, dataset):
149
        dataset_package = pyclustering_package();
150
        
151
        if (isinstance(dataset, numpy.matrix)):
152
            return self.__create_package_numpy_matrix(dataset_package, dataset);
153
        
154
        dataset_package.size = len(dataset);
155
    
156
        if (len(dataset) == 0):
157
            dataset_package.type = pyclustering_type_data.PYCLUSTERING_TYPE_UNDEFINED;
158
            dataset_package.data = None;
159
    
160
            return pointer(dataset_package);
161
    
162
        c_data_type = self.__fill_type(dataset_package, dataset);
163
        self.__fill_data(dataset_package, c_data_type, dataset);
164
        
165
        return pointer(dataset_package);
166
167
168
    def __fill_dataset_type(self, dataset_package, dataset):
169
        if (self.__is_container_type(dataset[0])):
170
            dataset_package.type = pyclustering_type_data.PYCLUSTERING_TYPE_LIST;
171
        
172
        elif (isinstance(dataset[0], int)):
173
            dataset_package.type = pyclustering_type_data.PYCLUSTERING_TYPE_LONG;
174
        
175
        elif (isinstance(dataset[0], float)):
176
            dataset_package.type = pyclustering_type_data.PYCLUSTERING_TYPE_DOUBLE;
177
        
178
        else:
179
            raise NameError("Not supported type of pyclustering package.");
180
        
181
        return pyclustering_type_data.get_ctype(dataset_package.type);
182
183
184
    def __fill_specify_type(self, dataset_package):
185
        dataset_package.type = pyclustering_type_data.get_pyclustering_type(self.__c_data_type);
186
        return self.__c_data_type;
187
188
189
    def __fill_type(self, dataset_package, dataset):
190
        if (self.__is_container_type(dataset[0])):
191
            dataset_package.type = pyclustering_type_data.PYCLUSTERING_TYPE_LIST;
192
            return None;
193
        
194
        if (self.__c_data_type is None):
195
            return self.__fill_dataset_type(dataset_package, dataset);
196
        
197
        return self.__fill_specify_type(dataset_package);
198
199
200
    def __fill_data(self, dataset_package, c_data_type, dataset):
201
        if (dataset_package.type == pyclustering_type_data.PYCLUSTERING_TYPE_LIST):
202
            package_data = (POINTER(pyclustering_package) * len(dataset))();
203
            for index in range(len(dataset)):
204
                package_data[index] = self.__create_package(dataset[index]);
205
            
206
            dataset_package.data = cast(package_data, POINTER(c_void_p));
207
        else:
208
            array_object = (c_data_type * len(dataset))(*dataset);
209
            dataset_package.data = cast(array_object, POINTER(c_void_p));
210
211
212
    def __create_package_numpy_matrix(self, dataset_package, dataset):
213
        (rows, cols) = dataset.shape;
214
        
215
        dataset_package.size = rows;
216
        dataset_package.type = pyclustering_type_data.PYCLUSTERING_TYPE_LIST;
217
        
218
        package_data = (POINTER(pyclustering_package) * rows)();
219
        for row_index in range(rows):
220
            array_package = pyclustering_package();
221
            array_package.size = cols;
222
            array_package.type = pyclustering_type_data.get_pyclustering_type(self.__c_data_type);
223
            
224
            array_object = (self.__c_data_type * cols)();
225
            for col_index in range(cols):
226
                array_object[col_index] = self.__c_data_type(dataset[row_index, col_index]);
227
        
228
            array_package.data = cast(array_object, POINTER(c_void_p));
229
            package_data[row_index] = pointer(array_package);
230
            
231
        dataset_package.data = cast(package_data, POINTER(c_void_p));
232
        return pointer(dataset_package);
233
234
235
236
class package_extractor:
237
    """!
238
    @brief Package extractor provides servies to unpack pyclustering package.
239
    
240
    """
241
    def __init__(self, package_pointer):
242
        """!
243
        @brief Initialize package extractor object by ctype-pointer to 'pyclustering_package'.
244
        
245
        @param[in] package_pointer (pointer): ctype-pointer to 'pyclustering_package' that should be used for unpacking.
246
        
247
        """
248
        self.__package_pointer = package_pointer;
249
250
251
    def extract(self):
252
        """!
253
        @brief Performs unpacking procedure of the pyclustering package to the data.
254
        
255
        @return (list) Extracted data from the pyclustering package.
256
        
257
        """
258
        return self.__extract_data(self.__package_pointer);
259
260
261
    def __extract_data(self, ccore_package_pointer):
262
        if (ccore_package_pointer == 0):
263
            return [];
264
        
265
        pointer_package = cast(ccore_package_pointer, POINTER(pyclustering_package));
266
        return self.__unpack_pointer_data(pointer_package);
267
    
268
    
269
    def __unpack_data(self, pointer_package, pointer_data, type_package):
270
        result = [];
271
        
272
        for index in range(0, pointer_package[0].size):
273
            if (type_package == pyclustering_type_data.PYCLUSTERING_TYPE_LIST):
274
                pointer_package = cast(pointer_data[index], (POINTER(pyclustering_package)));
275
                result.append(self.__extract_data(pointer_package));
276
            
277
            else:
278
                result.append(pointer_data[index]);
279
        
280
        return result;
281
282
283
    def __unpack_pointer_data(self, pointer_package):
284
        type_package = pointer_package[0].type;
285
        
286
        if (pointer_package[0].size == 0):
287
            return [];
288
        
289
        pointer_data = cast(pointer_package[0].data, POINTER(pyclustering_type_data.get_ctype(type_package)));
290
        return self.__unpack_data(pointer_package, pointer_data, type_package);
291