forked from mementum/backtrader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetabase.py
More file actions
331 lines (253 loc) · 10.9 KB
/
metabase.py
File metadata and controls
331 lines (253 loc) · 10.9 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015-2020 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from collections import OrderedDict
import itertools
import sys
import backtrader as bt
from .utils.py3 import zip, string_types, with_metaclass
def findbases(kls, topclass):
retval = list()
for base in kls.__bases__:
if issubclass(base, topclass):
retval.extend(findbases(base, topclass))
retval.append(base)
return retval
def findowner(owned, cls, startlevel=2, skip=None):
# skip this frame and the caller's -> start at 2
for framelevel in itertools.count(startlevel):
try:
frame = sys._getframe(framelevel)
except ValueError:
# Frame depth exceeded ... no owner ... break away
break
# 'self' in regular code
self_ = frame.f_locals.get('self', None)
if skip is not self_:
if self_ is not owned and isinstance(self_, cls):
return self_
# '_obj' in metaclasses
obj_ = frame.f_locals.get('_obj', None)
if skip is not obj_:
if obj_ is not owned and isinstance(obj_, cls):
return obj_
return None
class MetaBase(type):
def doprenew(cls, *args, **kwargs):
return cls, args, kwargs
def donew(cls, *args, **kwargs):
_obj = cls.__new__(cls, *args, **kwargs)
return _obj, args, kwargs
def dopreinit(cls, _obj, *args, **kwargs):
return _obj, args, kwargs
def doinit(cls, _obj, *args, **kwargs):
_obj.__init__(*args, **kwargs)
return _obj, args, kwargs
def dopostinit(cls, _obj, *args, **kwargs):
return _obj, args, kwargs
def __call__(cls, *args, **kwargs):
cls, args, kwargs = cls.doprenew(*args, **kwargs)
_obj, args, kwargs = cls.donew(*args, **kwargs)
_obj, args, kwargs = cls.dopreinit(_obj, *args, **kwargs)
_obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
_obj, args, kwargs = cls.dopostinit(_obj, *args, **kwargs)
return _obj
class AutoInfoClass(object):
_getpairsbase = classmethod(lambda cls: OrderedDict())
_getpairs = classmethod(lambda cls: OrderedDict())
_getrecurse = classmethod(lambda cls: False)
@classmethod
def _derive(cls, name, info, otherbases, recurse=False):
# collect the 3 set of infos
# info = OrderedDict(info)
baseinfo = cls._getpairs().copy()
obasesinfo = OrderedDict()
for obase in otherbases:
if isinstance(obase, (tuple, dict)):
obasesinfo.update(obase)
else:
obasesinfo.update(obase._getpairs())
# update the info of this class (base) with that from the other bases
baseinfo.update(obasesinfo)
# The info of the new class is a copy of the full base info
# plus and update from parameter
clsinfo = baseinfo.copy()
clsinfo.update(info)
# The new items to update/set are those from the otherbase plus the new
info2add = obasesinfo.copy()
info2add.update(info)
clsmodule = sys.modules[cls.__module__]
newclsname = str(cls.__name__ + '_' + name) # str - Python 2/3 compat
# This loop makes sure that if the name has already been defined, a new
# unique name is found. A collision example is in the plotlines names
# definitions of bt.indicators.MACD and bt.talib.MACD. Both end up
# definining a MACD_pl_macd and this makes it impossible for the pickle
# module to send results over a multiprocessing channel
namecounter = 1
while hasattr(clsmodule, newclsname):
newclsname += str(namecounter)
namecounter += 1
newcls = type(newclsname, (cls,), {})
setattr(clsmodule, newclsname, newcls)
setattr(newcls, '_getpairsbase',
classmethod(lambda cls: baseinfo.copy()))
setattr(newcls, '_getpairs', classmethod(lambda cls: clsinfo.copy()))
setattr(newcls, '_getrecurse', classmethod(lambda cls: recurse))
for infoname, infoval in info2add.items():
if recurse:
recursecls = getattr(newcls, infoname, AutoInfoClass)
infoval = recursecls._derive(name + '_' + infoname,
infoval,
[])
setattr(newcls, infoname, infoval)
return newcls
def isdefault(self, pname):
return self._get(pname) == self._getkwargsdefault()[pname]
def notdefault(self, pname):
return self._get(pname) != self._getkwargsdefault()[pname]
def _get(self, name, default=None):
return getattr(self, name, default)
@classmethod
def _getkwargsdefault(cls):
return cls._getpairs()
@classmethod
def _getkeys(cls):
return cls._getpairs().keys()
@classmethod
def _getdefaults(cls):
return list(cls._getpairs().values())
@classmethod
def _getitems(cls):
return cls._getpairs().items()
@classmethod
def _gettuple(cls):
return tuple(cls._getpairs().items())
def _getkwargs(self, skip_=False):
l = [
(x, getattr(self, x))
for x in self._getkeys() if not skip_ or not x.startswith('_')]
return OrderedDict(l)
def _getvalues(self):
return [getattr(self, x) for x in self._getkeys()]
def __new__(cls, *args, **kwargs):
obj = super(AutoInfoClass, cls).__new__(cls, *args, **kwargs)
if cls._getrecurse():
for infoname in obj._getkeys():
recursecls = getattr(cls, infoname)
setattr(obj, infoname, recursecls())
return obj
class MetaParams(MetaBase):
def __new__(meta, name, bases, dct):
# Remove params from class definition to avoid inheritance
# (and hence "repetition")
newparams = dct.pop('params', ())
packs = 'packages'
newpackages = tuple(dct.pop(packs, ())) # remove before creation
fpacks = 'frompackages'
fnewpackages = tuple(dct.pop(fpacks, ())) # remove before creation
# Create the new class - this pulls predefined "params"
cls = super(MetaParams, meta).__new__(meta, name, bases, dct)
# Pulls the param class out of it - default is the empty class
params = getattr(cls, 'params', AutoInfoClass)
# Pulls the packages class out of it - default is the empty class
packages = tuple(getattr(cls, packs, ()))
fpackages = tuple(getattr(cls, fpacks, ()))
# get extra (to the right) base classes which have a param attribute
morebasesparams = [x.params for x in bases[1:] if hasattr(x, 'params')]
# Get extra packages, add them to the packages and put all in the class
for y in [x.packages for x in bases[1:] if hasattr(x, packs)]:
packages += tuple(y)
for y in [x.frompackages for x in bases[1:] if hasattr(x, fpacks)]:
fpackages += tuple(y)
cls.packages = packages + newpackages
cls.frompackages = fpackages + fnewpackages
# Subclass and store the newly derived params class
cls.params = params._derive(name, newparams, morebasesparams)
return cls
def donew(cls, *args, **kwargs):
clsmod = sys.modules[cls.__module__]
# import specified packages
for p in cls.packages:
if isinstance(p, (tuple, list)):
p, palias = p
else:
palias = p
pmod = __import__(p)
plevels = p.split('.')
if p == palias and len(plevels) > 1: # 'os.path' not aliased
setattr(clsmod, pmod.__name__, pmod) # set 'os' in module
else: # aliased and/or dots
for plevel in plevels[1:]: # recurse down the mod
pmod = getattr(pmod, plevel)
setattr(clsmod, palias, pmod)
# import from specified packages - the 2nd part is a string or iterable
for p, frompackage in cls.frompackages:
if isinstance(frompackage, string_types):
frompackage = (frompackage,) # make it a tuple
for fp in frompackage:
if isinstance(fp, (tuple, list)):
fp, falias = fp
else:
fp, falias = fp, fp # assumed is string
# complain "not string" without fp (unicode vs bytes)
pmod = __import__(p, fromlist=[str(fp)])
pattr = getattr(pmod, fp)
setattr(clsmod, falias, pattr)
for basecls in cls.__bases__:
setattr(sys.modules[basecls.__module__], falias, pattr)
# Create params and set the values from the kwargs
params = cls.params()
for pname, pdef in cls.params._getitems():
setattr(params, pname, kwargs.pop(pname, pdef))
# Create the object and set the params in place
_obj, args, kwargs = super(MetaParams, cls).donew(*args, **kwargs)
_obj.params = params
_obj.p = params # shorter alias
# Parameter values have now been set before __init__
return _obj, args, kwargs
class ParamsBase(with_metaclass(MetaParams, object)):
pass # stub to allow easy subclassing without metaclasses
class ItemCollection(object):
'''
Holds a collection of items that can be reached by
- Index
- Name (if set in the append operation)
'''
def __init__(self):
self._items = list()
self._names = list()
def __len__(self):
return len(self._items)
def append(self, item, name=None):
setattr(self, name, item)
self._items.append(item)
if name:
self._names.append(name)
def __getitem__(self, key):
return self._items[key]
def getnames(self):
return self._names
def getitems(self):
return zip(self._names, self._items)
def getbyname(self, name):
idx = self._names.index(name)
return self._items[idx]