12 examples of how to improve code with @dataclass

As part of the course “Python Developer. Basic ” prepared a translation of useful material for you.



We also invite everyone to an
open webinar on the topic "Three whales: map (), filter () and zip ()" . Can you write code that requires loops but no loops? Can. Could it be faster than if we were using loops in Python? Can. To implement the plan, you need to know the words "callback", "iterator" and "lambda". It will be difficult, but interesting. Join us.






We add clustering algorithms using scikit-learn, Keras and others packages to Photonai. We'll show you how to @dataclass



improve your Python code with 12 examples . For this, we use code from the Photonai package for Machine Learning.





Upgrade to Python 3.7 or later





@dataclass



Python 3.7. Python 3.7 Docker-, /.bashrc_profile



/bashrc.txt



.





devdir='<path-to-projects>/photon/photonai/dockerSeasons/dev/'
testdir='<path-to-projects>/photon/photonai/dockerSeasons/test/'
echo $devdir
echo $testdir
export testdir
export devdir
#
alias updev="(cd $devdir; docker-compose up) &"
alias downdev="(cd $devdir; docker-compose down) &"
alias builddev="(cd $devdir; docker-compose build) &"
#
alias uptest="(cd $testdir; docker-compose up) & "
alias downtest="(cd $testdir; docker-compose down) &"
alias buildtest="cd $testdir; docker-compose build) &"
      
      



/bashrc.txt



touch/bashrc.txt



. ( MacOS Linux Unix.)





: ˜/.bashrc_profile



˜/bashrc.txt



, . 





Docker, . 





: Docker GitHub.





Python – . Python 3.5 (PEP 484). , , Python. , Python .





( , ) , .





Python 3.7 @dataclass



.





@dataclass



. , :





  1. https://medium.com/swlh/future-proof-your-python-code-20ef2b75e9f5





  2. https://realpython.com/python-type-checking/





  3. https://docs.python.org/3/library/typing.html





@dataclass

@dataclass



Python 3.7. , def







, ? ( ) , . , , .





: pandas, , @jit



numba.





@dataclass



def



5 init()



, repr()



, str



, eq()



, hash()



.





: , .





, 5 . @dataclass



, .





photon/photonai/base/hyperpipe.py



, @dataclass.





### Example #1

class Data:
    def __init__(self, X=None, y=None, kwargs=None):
        self.X = X
        self.y = y
        self.kwargs = kwargs
      
      



1, =>





from dataclasses import dataclass
from typing import Dict
import numpy as np
@dataclass
class Data:
    X: np.ndarray = None  # The field declaration: X
    y: np.array = None    # The field declaration: y
    kwargs: Dict = None   # The field declaration: kwargs
      
      



: , . any



, .





eq()



?





### Example #2

data1 = Data()
data2 = Data()
data1 == data1
      
      



2, =>





True
      
      



! repr()



str



?





### Example #3

print(data1)
data1
      
      



, =>





Data(X=None, y=None, kwargs=None)
Data(X=None, y=None, kwargs=None)
      
      



! hash()



init



?





Example #4

@dataclass(unsafe_hash=True)
class Data:
    X: np.ndarray = None
    y: np.array = None
    kwargs: Dict = None
        
data3 = Data(1,2,3)
{data3:1}
      
      



4, =>





{Data(X=1, y=2, kwargs=3): 1}
      
      



!





: init



(X, y, kwargs). , , Python 3.7.





: init()



, repr()



, str



  eq()



True, hash()



False.





inspect



, .





### Example #5

from inspect import signature
print(signature(data3.__init__))
      
      



5, =>





(X: numpy.ndarray = None, y: <built-in function array> = None, 
kwargs: Dict = None) -> None
      
      



!





photon/photonai/base/hyperpipe.py







### Example #6

class CrossValidation:

    def __init__(self, inner_cv, outer_cv,
                 eval_final_performance, test_size,
                 calculate_metrics_per_fold,
                 calculate_metrics_across_folds):
        self.inner_cv = inner_cv
        self.outer_cv = outer_cv
        self.eval_final_performance = eval_final_performance
        self.test_size = test_size
        self.calculate_metrics_per_fold = calculate_metrics_per_fold
        self.calculate_metrics_across_folds =
            calculate_metrics_across_folds

        self.outer_folds = None
        self.inner_folds = dict()Example #6 Output=>
      
      



6, =>





from dataclasses import dataclass
@dataclass
class CrossValidation:
    inner_cv: int
    outer_cv: int
    eval_final_performance: bool = True
    test_size: float = 0.2
    calculate_metrics_per_fold: bool = True
    calculate_metrics_across_folds: bool = False
Note:(Example #6) As any signature, keyword arguments fields with default values must be declared last.
Note:(Example #6)  class CrossValidation: Readability has increased substantially by using @dataclass and type hinting.
      
      



### Example #7
cv1 = CrossValidation()
      
      



7, =>





TypeError: __init__() missing 2 required positional arguments: 'inner_cv' and 'outer_cv'
Note:(Example #7) inner_cv and outer_cv are positional arguments. With any signature, you declare a non-default field after a default one. (Hint: If this were allowed, inheritance from a parent class breaks.)((Why? Goggle interview question #666.))
      
      



### Example #8
cv1 = CrossValidation(1,2)
cv2 = CrossValidation(1,2)
cv3 = CrossValidation(3,2,test_size=0.5)
print(cv1)
cv3
      
      



8, =>





CrossValidation(inner_cv=1, outer_cv=2, eval_final_performance=True, test_size=0.2, calculate_metrics_per_fold=True, calculate_metrics_across_folds=False)
CrossValidation(inner_cv=3, outer_cv=2, eval_final_performance=True, test_size=0.5, calculate_metrics_per_fold=True, calculate_metrics_across_folds=False)
      
      



### Example #9
cv1 == cv2
      
      



9, =>





True
      
      



### Example #10

cv1 == cv3
      
      



10, =>





False
      
      



### Example #11
from inspect import signature
print(signature(cv3.__init__))
cv3
      
      



11, =>





(inner_cv: int, outer_cv: int, eval_final_performance: bool = True, test_size: float = 0.2, calculate_metrics_per_fold: bool = True, calculate_metrics_across_folds: bool = False) -> None
CrossValidation(inner_cv=3, outer_cv=2, eval_final_performance=True, test_size=0.5, calculate_metrics_per_fold=True, calculate_metrics_across_folds=False)
Note: (Example #11) The inspect function shows the signature of the class object while the__str__ default shows the instance state variables and their values.
      
      



!





, :





self.outer_folds = None
self.inner_folds = dict()
      
      



, . , @dataclass



. .





, post-init



, @dataclass



. post_init



init



, @dataclass



. . 





CrossValidation



:





### Example 12
from dataclasses import dataclass
@dataclass
class CrossValidation:
    inner_cv: int
    outer_cv: int
    eval_final_performance: bool = True
    test_size: float = 0.2
    calculate_metrics_per_fold: bool = True
    calculate_metrics_across_folds: bool = False
    def __post_init__(self):
        self.outer_folds = None
        self.inner_folds = dict()
      
      







@dataclass



:





  1. https://realpython.com/python-data-classes/





  2. https://blog.usejournal.com/new-buzzword-in-python-is-here-dataclasses-843dd1d372a5





12 « » , @dataclass



Photonai Machine Learning. , @dataclass







. , , .





@dataclass



, Python .





: Photonai GitHub.





@dataclass



. , photonai.






«Python Developer. Basic».



« : map(), filter() zip()».








All Articles