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

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

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

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

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