Skip to content

Commit 616e487

Browse files
author
Timur Gilmullin
committed
#14: Increase code quality with the Codacy service
1 parent 3938cee commit 616e487

3 files changed

Lines changed: 29 additions & 48 deletions

File tree

FuzzyClassificator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def Version(onlyPrint=False):
2626
try:
2727
version = pkg_resources.get_distribution('FuzzyClassificator').version
2828

29-
except:
30-
version = 'unknown'
29+
except Exception:
30+
return 'unknown'
3131

3232
if onlyPrint:
3333
FCLogger.level = 50

FuzzyRoutines.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,18 @@ def DiapasonParser(diapason):
3232
for element in diapason.split(','):
3333
fullDiapason += [x for x in range(int(element.split('-')[0]), int(element.split('-')[-1]) + 1)]
3434

35-
except:
35+
except Exception:
3636
FCLogger.error('"{}" is not correct diapason string!'.format(diapason))
37-
fullDiapason = []
37+
return []
3838

39-
finally:
40-
return sorted(list(set(fullDiapason)))
39+
return sorted(list(set(fullDiapason)))
4140

4241

4342
def IsNumber(value):
4443
"""
45-
Return True if value is float or integer number.
44+
Return True if value is float or integer number.
4645
"""
47-
if not isinstance(value, bool) and (isinstance(value, int) or isinstance(value, float)):
48-
return True
49-
50-
else:
51-
return False
46+
return bool(not isinstance(value, bool) and (isinstance(value, int) or isinstance(value, float)))
5247

5348

5449
def IsCorrectFuzzyNumberValue(value):
@@ -297,13 +292,12 @@ def Hyperbolic(self, x):
297292
result = 1 / (1 + (a * (x - c)) ** b)
298293

299294
except:
300-
result = 0
301295
FCLogger.error(traceback.format_exc())
302296
FCLogger.error('Hyperbolic membership function use real inputs x and parameters a, b, c.')
303297
FCLogger.error('Your inputs: mju_hyperbolic({}, {}, {}, {})'.format(x, a, b, c))
298+
return 0
304299

305-
finally:
306-
return result
300+
return result
307301

308302
def Bell(self, x):
309303
"""
@@ -335,13 +329,12 @@ def Bell(self, x):
335329
self._parameters['b'] = bOld
336330

337331
except:
338-
result = 0
339332
FCLogger.error(traceback.format_exc())
340333
FCLogger.error('Bell membership function use real inputs x and parameters a, b, c.')
341334
FCLogger.error('Your inputs: mju_bell({}, {}, {}, {})'.format(x, a, b, c))
335+
return 0
342336

343-
finally:
344-
return result
337+
return result
345338

346339
def Parabolic(self, x):
347340
"""
@@ -366,13 +359,12 @@ def Parabolic(self, x):
366359
result = 1
367360

368361
except:
369-
result = 0
370362
FCLogger.error(traceback.format_exc())
371363
FCLogger.error('Parabolic membership function use real inputs x and parameters a, b.')
372364
FCLogger.error('Your inputs: mju_parabolic({}, {}, {})'.format(x, a, b))
365+
return 0
373366

374-
finally:
375-
return result
367+
return result
376368

377369
def Triangle(self, x):
378370
"""
@@ -398,13 +390,12 @@ def Triangle(self, x):
398390
result = 0
399391

400392
except:
401-
result = 0
402393
FCLogger.error(traceback.format_exc())
403394
FCLogger.error('Triangle membership function use real inputs x and parameters a, b, c.')
404395
FCLogger.error('Your inputs: mju_triangle({}, {}, {}, {})'.format(x, a, b, c))
396+
return 0
405397

406-
finally:
407-
return result
398+
return result
408399

409400
def Trapezium(self, x):
410401
"""
@@ -434,13 +425,12 @@ def Trapezium(self, x):
434425
result = 0
435426

436427
except:
437-
result = 0
438428
FCLogger.error(traceback.format_exc())
439429
FCLogger.error('Trapezium membership function use real inputs x and parameters a, b, c, d.')
440430
FCLogger.error('Your inputs: mju_trapezium({}, {}, {}, {}, {})'.format(x, a, b, c, d))
431+
return 0
441432

442-
finally:
443-
return result
433+
return result
444434

445435
def Exponential(self, x):
446436
"""
@@ -456,13 +446,12 @@ def Exponential(self, x):
456446
result = math.exp(1) ** (-0.5 * ((x - a) / b) ** 2)
457447

458448
except:
459-
result = 0
460449
FCLogger.error(traceback.format_exc())
461450
FCLogger.error('Exponential membership function use real inputs x and parameters a, b.')
462451
FCLogger.error('Your inputs: mju_exponential({}, {}, {})'.format(x, a, b))
452+
return 0
463453

464-
finally:
465-
return result
454+
return result
466455

467456
def Sigmoidal(self, x):
468457
"""
@@ -477,13 +466,12 @@ def Sigmoidal(self, x):
477466
result = 1 / (1 + math.exp(1) ** (-a * (x - b)))
478467

479468
except:
480-
result = 0
481469
FCLogger.error(traceback.format_exc())
482470
FCLogger.error('Sigmoidal membership function use real inputs x and parameters a, b.')
483471
FCLogger.error('Your inputs: mju_sigmoidal({}, {}, {})'.format(x, a, b))
472+
return 0
484473

485-
finally:
486-
return result
474+
return result
487475

488476
def Desirability(self, y):
489477
"""
@@ -495,13 +483,12 @@ def Desirability(self, y):
495483
result = math.exp(-math.exp(-y))
496484

497485
except:
498-
result = 0
499486
FCLogger.error(traceback.format_exc())
500487
FCLogger.error("Harrington's desirability membership function use only real input y without any parameters.")
501488
FCLogger.error('Your inputs: mju_desirability({})'.format(y))
489+
return 0
502490

503-
finally:
504-
return result
491+
return result
505492

506493

507494
class FuzzySet():
@@ -646,7 +633,7 @@ def __str__(self):
646633
for level in self._levels[1:]:
647634
allLevelsName += ', {}'.format(level['name'])
648635
allLevels += '\n {}'.format(str(level['fSet']))
649-
636+
650637
scaleView = '{} = {{{}}}{}'.format(self._name, allLevelsName, allLevels)
651638

652639
return scaleView

PyBrainLearning.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def DefuzRawData(self):
9494

9595
defuzData.append(defuzValues)
9696

97-
except:
97+
except Exception:
9898
defuzData = []
9999
FCLogger.error(traceback.format_exc())
100100
FCLogger.error('An error occurred while defuzzyficating values in raw data!')
@@ -747,8 +747,6 @@ def Train(self):
747747
"""
748748
Realize training mechanism.
749749
"""
750-
noTrainErrors = True # successful train flag
751-
752750
try:
753751
if self._epochs > 0:
754752
if self.trainer:
@@ -853,12 +851,11 @@ def Train(self):
853851
FCLogger.warning('Epoch of learning count is 0. Train not run!')
854852

855853
except:
856-
noTrainErrors = False
857854
FCLogger.error(traceback.format_exc())
858855
FCLogger.error('An error occurred while Training Fuzzy Network!')
856+
return False
859857

860-
finally:
861-
return noTrainErrors
858+
return True
862859

863860
def CreateReport(self, results=None, fuzzyOutput=True):
864861
"""
@@ -868,8 +865,6 @@ def CreateReport(self, results=None, fuzzyOutput=True):
868865
"""
869866
FCLogger.debug('Creating Classificate Report File...')
870867

871-
noReportCreationErrors = True # successful of Creating Report process flag
872-
873868
try:
874869
if not results:
875870
results = self.ClassificationResults(fullEval=True, needFuzzy=fuzzyOutput)
@@ -895,12 +890,11 @@ def CreateReport(self, results=None, fuzzyOutput=True):
895890
FCLogger.info('Classificate Report File created: ' + os.path.abspath(self.reportFile))
896891

897892
except:
898-
noReportCreationErrors = False
899893
FCLogger.error(traceback.format_exc())
900894
FCLogger.error('An error occurred while Classificate Report creating!')
895+
return False
901896

902-
finally:
903-
return noReportCreationErrors
897+
return True
904898

905899

906900
if __name__ == "__main__":

0 commit comments

Comments
 (0)