Skip to content

Commit 4cc4d45

Browse files
committed
review items (NO_JIRA))
1 parent b0b231c commit 4cc4d45

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

scripts/refcodes_with_properties/EntryPropertyCalculator.py

+42-11
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@
1515
import ccdc.io
1616

1717
_filter_classes = {}
18+
19+
1820
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.
2123
'''
2224
if cls.name() in _filter_classes:
2325
raise ValueError(f"a class with the name {cls.name()} is already registered. Use a different name")
2426

2527
_filter_classes[cls.name()] = cls
2628

29+
2730
def filter(name):
2831
return _filter_classes[name]
2932

33+
3034
def helptext():
3135
''' Get help text
3236
'''
@@ -41,15 +45,16 @@ class _Filter(object):
4145

4246
@staticmethod
4347
def name():
44-
raise NotImplementedError # override this
48+
raise NotImplementedError # override this
4549

4650
@staticmethod
4751
def helptext():
48-
raise NotImplementedError # override this
52+
raise NotImplementedError # override this
4953

5054
@staticmethod
5155
def argument_pair():
52-
raise NotImplementedError # override this
56+
raise NotImplementedError # override this
57+
5358

5459
class _ComparativeFilter(_Filter):
5560
def __init__(self, args):
@@ -60,7 +65,7 @@ def __init__(self, args):
6065
self.expected_value = value
6166

6267
def value(self):
63-
raise NotImplementedError # override this
68+
raise NotImplementedError # override this
6469

6570
def __call__(self,theobject):
6671
value = self.value(theobject)
@@ -71,8 +76,8 @@ class _RangeFilter(_Filter):
7176
def __init__(self, args):
7277

7378
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])
7681

7782
def value(self):
7883
raise NotImplementedError # override this
@@ -81,9 +86,10 @@ def __call__(self,theobject):
8186
value = self.value(theobject)
8287
return value >= self.minimum and value <= self.maximum
8388

89+
8490
class AllowedAtomicNumbersFilter(_Filter):
8591
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()]
8793

8894
@staticmethod
8995
def name():
@@ -105,7 +111,7 @@ def __call__(self,entry):
105111

106112
class MustContainAtomicNumbersFilter(_Filter):
107113
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()]
109115

110116
@staticmethod
111117
def name():
@@ -132,8 +138,10 @@ def __call__(self,entry):
132138
except:
133139
return False
134140

141+
135142
register(MustContainAtomicNumbersFilter)
136143

144+
137145
class OrganicFilter(_ComparativeFilter):
138146
def __init__(self, args):
139147
super(self.__class__,self).__init__(args)
@@ -149,8 +157,10 @@ def helptext():
149157
def value(self,entry):
150158
return entry.is_organic
151159

160+
152161
register(OrganicFilter)
153162

163+
154164
class PolymericFilter(_ComparativeFilter):
155165
def __init__(self, args):
156166
super(self.__class__,self).__init__(args)
@@ -166,8 +176,10 @@ def helptext():
166176
def value(self,entry):
167177
return entry.is_polymeric
168178

179+
169180
register(PolymericFilter)
170181

182+
171183
class AllHaveSitesFilter(_ComparativeFilter):
172184
def __init__(self, args):
173185
super(self.__class__,self).__init__(args)
@@ -186,8 +198,10 @@ def value(self,entry):
186198
except:
187199
return False
188200

201+
189202
register(AllHaveSitesFilter)
190203

204+
191205
class DisorderedFilter(_ComparativeFilter):
192206
def __init__(self, args):
193207

@@ -204,6 +218,7 @@ def helptext():
204218
def value(self,entry):
205219
return entry.has_disorder
206220

221+
207222
register(DisorderedFilter)
208223

209224

@@ -226,8 +241,10 @@ def value(self,entry):
226241
except TypeError:
227242
return 0.0
228243

244+
229245
register(AtomicWeightFilter)
230246

247+
231248
class AtomCountFilter(_RangeFilter):
232249
def __init__(self,args):
233250
super(self.__class__,self).__init__(args)
@@ -247,8 +264,10 @@ def value(self,entry):
247264
except TypeError:
248265
return 0
249266

267+
250268
register(AtomCountFilter)
251269

270+
252271
class RotatableBondFilter(_RangeFilter):
253272
def __init__(self,args):
254273
super(self.__class__,self).__init__(args)
@@ -268,8 +287,10 @@ def value(self,entry):
268287
except TypeError:
269288
return 0
270289

290+
271291
register(RotatableBondFilter)
272292

293+
273294
class DonorCountFilter(_RangeFilter):
274295
def __init__(self,args):
275296
super(self.__class__,self).__init__(args)
@@ -289,8 +310,10 @@ def value(self,entry):
289310
except TypeError:
290311
return 0
291312

313+
292314
register(DonorCountFilter)
293315

316+
294317
class AcceptorCountFilter(_RangeFilter):
295318
def __init__(self,args):
296319
super(self.__class__,self).__init__(args)
@@ -310,8 +333,10 @@ def value(self,entry):
310333
except TypeError:
311334
return 0
312335

336+
313337
register(AcceptorCountFilter)
314338

339+
315340
class ComponentCountFilter(_RangeFilter):
316341
def __init__(self,args):
317342
super(self.__class__,self).__init__(args)
@@ -330,6 +355,7 @@ def value(self,entry):
330355
except TypeError:
331356
return 0
332357

358+
333359
register(ComponentCountFilter)
334360

335361

@@ -348,6 +374,7 @@ def helptext():
348374
def value(self,entry):
349375
return entry.crystal.z_prime
350376

377+
351378
register(ZPrimeFilter)
352379

353380

@@ -366,8 +393,10 @@ def helptext():
366393
def value(self,entry):
367394
return entry.r_factor
368395

396+
369397
register(RfactorFilter)
370398

399+
371400
class SpacegroupNumberFilter(_RangeFilter):
372401
def __init__(self,args):
373402
super(self.__class__,self).__init__(args)
@@ -383,8 +412,10 @@ def helptext():
383412
def value(self,entry):
384413
return entry.crystal.spacegroup_number_and_setting[0]
385414

415+
386416
register(SpacegroupNumberFilter)
387417

418+
388419
class FilterEvaluation(object):
389420
def __init__(self):
390421
self._methods = []
@@ -412,6 +443,7 @@ def values(self,entry):
412443
pass
413444
return values
414445

446+
415447
def parse_control_file(lines):
416448
evaluator = FilterEvaluation()
417449
for line in lines:
@@ -423,7 +455,6 @@ def parse_control_file(lines):
423455
return evaluator
424456

425457

426-
427458
import unittest
428459

429460
class TestFiltering(unittest.TestCase):

0 commit comments

Comments
 (0)