Project

General

Profile

1
# Runnable object
2

    
3
import copy
4

    
5
import eval_
6

    
7
class Runnable:
8
    '''A picklable function call'''
9
    
10
    def __init__(self, func, *args, **kw_args):
11
        self.func = func
12
        self.args = args
13
        self.kw_args = kw_args
14
    
15
    def __call__(self): return self.func(*self.args, **self.kw_args)
16
    
17
    def __getstate__(self):
18
        state = copy.copy(self.__dict__) # shallow copy
19
        state['func'] = eval_.func2name(state['func']) # make func picklable
20
        return state
21
    
22
    def __setstate__(self, state):
23
        state['func'] = eval_.name2func(*state['func'])
24
        self.__dict__ = state
(3-3/31)