Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

# -*- coding: utf-8 -*- 

 

__all__ = ["DataPipe"] 

__author__ = "DboyLiao <qmalliao@gmail.com>" 

__license__ = "MIT" 

__version__ = ("0", "9") 

 

from ._metaclass import PipeMeta 

from functools import wraps 

from .exceptions import ExecutionContextError 

from ._utils import _exec_context 

 

class DataPipe(object): 

 

    __metaclass__ = PipeMeta 

 

    def __new__(cls, data, *args, **kwargs): 

        obj = super(DataPipe, cls).__new__(cls) 

        obj._under_execution_context = False 

        return obj 

 

    def __init__(self, data, *args, **kwargs): 

        self._data = data 

 

    @property 

    def data(self): 

        return self._data 

 

    @data.setter 

    def data(self, value): 

        if not self._under_execution_context: 

            raise ExecutionContextError("data can not be modified outside of execution of methods.") 

 

        if not isinstance(value, type(self.data)): 

            raise TypeError("The data should be of type {}: {} is given.".format(type(self.data), type(value))) 

 

        self._data = value 

 

    def register(self, fun): 

 

        @wraps(fun) 

        def wrapped(*args, **kwargs): 

 

            self._under_execution_context = True 

            new_data = fun(self.data, *args, **kwargs) 

            self.data = new_data 

            return self 

 

        setattr(self, fun.__name__, wrapped)