15
15
import ccdc .io
16
16
17
17
_filter_classes = {}
18
+
19
+
18
20
def register (cls ):
19
- ''' Register a filter class to use in the script.
20
- :param cls: the class to register
21
+ ''' Register a filter class to use in the script.
22
+ :param cls: the class to register.
21
23
'''
22
24
if cls .name () in _filter_classes :
23
25
raise ValueError (f"a class with the name { cls .name ()} is already registered. Use a different name" )
24
26
25
27
_filter_classes [cls .name ()] = cls
26
28
29
+
27
30
def filter (name ):
28
31
return _filter_classes [name ]
29
32
33
+
30
34
def helptext ():
31
35
''' Get help text
32
36
'''
@@ -41,15 +45,16 @@ class _Filter(object):
41
45
42
46
@staticmethod
43
47
def name ():
44
- raise NotImplementedError # override this
48
+ raise NotImplementedError # override this
45
49
46
50
@staticmethod
47
51
def helptext ():
48
- raise NotImplementedError # override this
52
+ raise NotImplementedError # override this
49
53
50
54
@staticmethod
51
55
def argument_pair ():
52
- raise NotImplementedError # override this
56
+ raise NotImplementedError # override this
57
+
53
58
54
59
class _ComparativeFilter (_Filter ):
55
60
def __init__ (self , args ):
@@ -60,7 +65,7 @@ def __init__(self, args):
60
65
self .expected_value = value
61
66
62
67
def value (self ):
63
- raise NotImplementedError # override this
68
+ raise NotImplementedError # override this
64
69
65
70
def __call__ (self ,theobject ):
66
71
value = self .value (theobject )
@@ -71,8 +76,8 @@ class _RangeFilter(_Filter):
71
76
def __init__ (self , args ):
72
77
73
78
parts = [ p .strip () for p in args .split () ]
74
- self .minimum = eval (parts [0 ])
75
- self .maximum = eval (parts [1 ])
79
+ self .minimum = float (parts [0 ])
80
+ self .maximum = float (parts [1 ])
76
81
77
82
def value (self ):
78
83
raise NotImplementedError # override this
@@ -81,9 +86,10 @@ def __call__(self,theobject):
81
86
value = self .value (theobject )
82
87
return value >= self .minimum and value <= self .maximum
83
88
89
+
84
90
class AllowedAtomicNumbersFilter (_Filter ):
85
91
def __init__ (self ,args ):
86
- self .allowed_atomic_numbers = [eval (x ) for x in args .strip ().split ()]
92
+ self .allowed_atomic_numbers = [int (x ) for x in args .strip ().split ()]
87
93
88
94
@staticmethod
89
95
def name ():
@@ -105,7 +111,7 @@ def __call__(self,entry):
105
111
106
112
class MustContainAtomicNumbersFilter (_Filter ):
107
113
def __init__ (self ,args ):
108
- self .must_have_atomic_numbers = [eval (x ) for x in args .strip ().split ()]
114
+ self .must_have_atomic_numbers = [int (x ) for x in args .strip ().split ()]
109
115
110
116
@staticmethod
111
117
def name ():
@@ -132,8 +138,10 @@ def __call__(self,entry):
132
138
except :
133
139
return False
134
140
141
+
135
142
register (MustContainAtomicNumbersFilter )
136
143
144
+
137
145
class OrganicFilter (_ComparativeFilter ):
138
146
def __init__ (self , args ):
139
147
super (self .__class__ ,self ).__init__ (args )
@@ -149,8 +157,10 @@ def helptext():
149
157
def value (self ,entry ):
150
158
return entry .is_organic
151
159
160
+
152
161
register (OrganicFilter )
153
162
163
+
154
164
class PolymericFilter (_ComparativeFilter ):
155
165
def __init__ (self , args ):
156
166
super (self .__class__ ,self ).__init__ (args )
@@ -166,8 +176,10 @@ def helptext():
166
176
def value (self ,entry ):
167
177
return entry .is_polymeric
168
178
179
+
169
180
register (PolymericFilter )
170
181
182
+
171
183
class AllHaveSitesFilter (_ComparativeFilter ):
172
184
def __init__ (self , args ):
173
185
super (self .__class__ ,self ).__init__ (args )
@@ -186,8 +198,10 @@ def value(self,entry):
186
198
except :
187
199
return False
188
200
201
+
189
202
register (AllHaveSitesFilter )
190
203
204
+
191
205
class DisorderedFilter (_ComparativeFilter ):
192
206
def __init__ (self , args ):
193
207
@@ -204,6 +218,7 @@ def helptext():
204
218
def value (self ,entry ):
205
219
return entry .has_disorder
206
220
221
+
207
222
register (DisorderedFilter )
208
223
209
224
@@ -226,8 +241,10 @@ def value(self,entry):
226
241
except TypeError :
227
242
return 0.0
228
243
244
+
229
245
register (AtomicWeightFilter )
230
246
247
+
231
248
class AtomCountFilter (_RangeFilter ):
232
249
def __init__ (self ,args ):
233
250
super (self .__class__ ,self ).__init__ (args )
@@ -247,8 +264,10 @@ def value(self,entry):
247
264
except TypeError :
248
265
return 0
249
266
267
+
250
268
register (AtomCountFilter )
251
269
270
+
252
271
class RotatableBondFilter (_RangeFilter ):
253
272
def __init__ (self ,args ):
254
273
super (self .__class__ ,self ).__init__ (args )
@@ -268,8 +287,10 @@ def value(self,entry):
268
287
except TypeError :
269
288
return 0
270
289
290
+
271
291
register (RotatableBondFilter )
272
292
293
+
273
294
class DonorCountFilter (_RangeFilter ):
274
295
def __init__ (self ,args ):
275
296
super (self .__class__ ,self ).__init__ (args )
@@ -289,8 +310,10 @@ def value(self,entry):
289
310
except TypeError :
290
311
return 0
291
312
313
+
292
314
register (DonorCountFilter )
293
315
316
+
294
317
class AcceptorCountFilter (_RangeFilter ):
295
318
def __init__ (self ,args ):
296
319
super (self .__class__ ,self ).__init__ (args )
@@ -310,8 +333,10 @@ def value(self,entry):
310
333
except TypeError :
311
334
return 0
312
335
336
+
313
337
register (AcceptorCountFilter )
314
338
339
+
315
340
class ComponentCountFilter (_RangeFilter ):
316
341
def __init__ (self ,args ):
317
342
super (self .__class__ ,self ).__init__ (args )
@@ -330,6 +355,7 @@ def value(self,entry):
330
355
except TypeError :
331
356
return 0
332
357
358
+
333
359
register (ComponentCountFilter )
334
360
335
361
@@ -348,6 +374,7 @@ def helptext():
348
374
def value (self ,entry ):
349
375
return entry .crystal .z_prime
350
376
377
+
351
378
register (ZPrimeFilter )
352
379
353
380
@@ -366,8 +393,10 @@ def helptext():
366
393
def value (self ,entry ):
367
394
return entry .r_factor
368
395
396
+
369
397
register (RfactorFilter )
370
398
399
+
371
400
class SpacegroupNumberFilter (_RangeFilter ):
372
401
def __init__ (self ,args ):
373
402
super (self .__class__ ,self ).__init__ (args )
@@ -383,8 +412,10 @@ def helptext():
383
412
def value (self ,entry ):
384
413
return entry .crystal .spacegroup_number_and_setting [0 ]
385
414
415
+
386
416
register (SpacegroupNumberFilter )
387
417
418
+
388
419
class FilterEvaluation (object ):
389
420
def __init__ (self ):
390
421
self ._methods = []
@@ -412,6 +443,7 @@ def values(self,entry):
412
443
pass
413
444
return values
414
445
446
+
415
447
def parse_control_file (lines ):
416
448
evaluator = FilterEvaluation ()
417
449
for line in lines :
@@ -423,7 +455,6 @@ def parse_control_file(lines):
423
455
return evaluator
424
456
425
457
426
-
427
458
import unittest
428
459
429
460
class TestFiltering (unittest .TestCase ):
0 commit comments