Total Complexity | 10 |
Total Lines | 27 |
Duplicated Lines | 77.78 % |
Coverage | 68.18% |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | 1 | import os |
|
|
|||
2 | 1 | import pickle |
|
3 | 1 | from .meta_clf import MetaClf |
|
4 | |||
5 | |||
6 | 1 | View Code Duplication | class MetaSkLearnClf(MetaClf): |
7 | 1 | def __init__(self, classifier_instance, **kwargs): |
|
8 | 1 | super().__init__(classifier_instance=classifier_instance, **kwargs) |
|
9 | 1 | if kwargs['load_path'] is not None: self.load_model(kwargs['load_path']) |
|
10 | |||
11 | 1 | def load_model(self, load_path: str): |
|
12 | 1 | loading_prep = lambda string: f'model_dir/{load_path}/{string}' |
|
13 | 1 | self.emb.load(loading_prep('emb.pkl')) |
|
14 | 1 | with open(loading_prep('model.pkl'), 'rb') as f: |
|
15 | 1 | self.clf = pickle.load(f) |
|
16 | 1 | with open(loading_prep('scaler.pkl'), 'rb') as f: |
|
17 | 1 | self.scaler = pickle.load(f) |
|
18 | |||
19 | 1 | def save_model(self, save_path: str): |
|
20 | os.makedirs(f'model_dir/{save_path}', exist_ok=True) |
||
21 | saving_prep = lambda string: f'model_dir/{save_path}/{string}' |
||
22 | self.emb.save(saving_prep('emb.pkl')) |
||
23 | with open(saving_prep('scaler.pkl'), 'wb') as f: |
||
24 | pickle.dump(self.scaler, f, pickle.HIGHEST_PROTOCOL) |
||
25 | with open(saving_prep('model.pkl'), 'wb') as f: |
||
26 | pickle.dump(self.clf, f, pickle.HIGHEST_PROTOCOL) |
||
27 |