kagle_trainer.py 1.5 KB
Newer Older
X
xiexionghang 已提交
1 2 3 4
"""
Define A Trainer Base
"""
import abc
X
xiexionghang 已提交
5 6
import time

T
tangwei 已提交
7

X
xiexionghang 已提交
8
class Trainer(object):
X
xiexionghang 已提交
9 10
    """R
    """   
X
xiexionghang 已提交
11
    __metaclass__ = abc.ABCMeta
T
tangwei 已提交
12

X
xiexionghang 已提交
13
    def __init__(self, config):
X
xiexionghang 已提交
14 15
        """R
        """
X
xiexionghang 已提交
16 17 18 19
        self._status_processor = {}
        self._context = {'status': 'uninit', 'is_exit': False}
       
    def regist_context_processor(self, status_name, processor):
X
xiexionghang 已提交
20 21 22
        """
        regist a processor for specify status
        """
X
xiexionghang 已提交
23 24 25
        self._status_processor[status_name] = processor

    def context_process(self, context):
X
xiexionghang 已提交
26 27 28 29 30 31 32
        """
        select a processor to deal specify context
        Args:
            context : context with status
        Return:
            None : run a processor for this status
        """
X
xiexionghang 已提交
33 34 35 36 37 38
        if context['status'] in self._status_processor:
            self._status_processor[context['status']](context)
        else:
            self.other_status_processor(context)
    
    def other_status_processor(self, context):
X
xiexionghang 已提交
39 40 41 42 43
        """
        if no processor match context.status, use defalut processor
        Return:
            None, just sleep in base
        """
X
xiexionghang 已提交
44
        print('unknow context_status:%s, do nothing' % context['status'])
45
        time.sleep(60)
X
xiexionghang 已提交
46 47

    def reload_train_context(self):
X
xiexionghang 已提交
48 49 50
        """
        context maybe update timely, reload for update
        """
X
xiexionghang 已提交
51 52 53
        pass

    def run(self):
X
xiexionghang 已提交
54 55 56
        """
        keep running by statu context.
        """
X
xiexionghang 已提交
57 58 59 60 61
        while True:
            self.reload_train_context()
            self.context_process(self._context)
            if self._context['is_exit']:
                break