Module backtrader.utils.autodict
Expand source code
#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015-2023 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, defaultdict
from .py3 import values as py3lvalues
def Tree():
return defaultdict(Tree)
class AutoDictList(dict):
def __missing__(self, key):
value = self[key] = list()
return value
class DotDict(dict):
# If the attribut is not found in the usual places try the dict itself
def __getattr__(self, key):
if key.startswith('__'):
return super(DotDict, self).__getattr__(key)
return self[key]
class AutoDict(dict):
_closed = False
def _close(self):
self._closed = True
for key, val in self.items():
if isinstance(val, (AutoDict, AutoOrderedDict)):
val._close()
def _open(self):
self._closed = False
def __missing__(self, key):
if self._closed:
raise KeyError
value = self[key] = AutoDict()
return value
def __getattr__(self, key):
if False and key.startswith('_'):
raise AttributeError
return self[key]
def __setattr__(self, key, value):
if False and key.startswith('_'):
self.__dict__[key] = value
return
self[key] = value
class AutoOrderedDict(OrderedDict):
_closed = False
def _close(self):
self._closed = True
for key, val in self.items():
if isinstance(val, (AutoDict, AutoOrderedDict)):
val._close()
def _open(self):
self._closed = False
def __missing__(self, key):
if self._closed:
raise KeyError
# value = self[key] = type(self)()
value = self[key] = AutoOrderedDict()
return value
def __getattr__(self, key):
if key.startswith('_'):
raise AttributeError
return self[key]
def __setattr__(self, key, value):
if key.startswith('_'):
self.__dict__[key] = value
return
self[key] = value
# Define math operations
def __iadd__(self, other):
if type(self) != type(other):
return type(other)() + other
return self + other
def __isub__(self, other):
if type(self) != type(other):
return type(other)() - other
return self - other
def __imul__(self, other):
if type(self) != type(other):
return type(other)() * other
return self + other
def __idiv__(self, other):
if type(self) != type(other):
return type(other)() // other
return self + other
def __itruediv__(self, other):
if type(self) != type(other):
return type(other)() / other
return self + other
def lvalues(self):
return py3lvalues(self)
Functions
def Tree()
-
Expand source code
def Tree(): return defaultdict(Tree)
Classes
class AutoDict (*args, **kwargs)
-
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
Expand source code
class AutoDict(dict): _closed = False def _close(self): self._closed = True for key, val in self.items(): if isinstance(val, (AutoDict, AutoOrderedDict)): val._close() def _open(self): self._closed = False def __missing__(self, key): if self._closed: raise KeyError value = self[key] = AutoDict() return value def __getattr__(self, key): if False and key.startswith('_'): raise AttributeError return self[key] def __setattr__(self, key, value): if False and key.startswith('_'): self.__dict__[key] = value return self[key] = value
Ancestors
- builtins.dict
class AutoDictList (*args, **kwargs)
-
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
Expand source code
class AutoDictList(dict): def __missing__(self, key): value = self[key] = list() return value
Ancestors
- builtins.dict
class AutoOrderedDict (*args, **kwargs)
-
Dictionary that remembers insertion order
Expand source code
class AutoOrderedDict(OrderedDict): _closed = False def _close(self): self._closed = True for key, val in self.items(): if isinstance(val, (AutoDict, AutoOrderedDict)): val._close() def _open(self): self._closed = False def __missing__(self, key): if self._closed: raise KeyError # value = self[key] = type(self)() value = self[key] = AutoOrderedDict() return value def __getattr__(self, key): if key.startswith('_'): raise AttributeError return self[key] def __setattr__(self, key, value): if key.startswith('_'): self.__dict__[key] = value return self[key] = value # Define math operations def __iadd__(self, other): if type(self) != type(other): return type(other)() + other return self + other def __isub__(self, other): if type(self) != type(other): return type(other)() - other return self - other def __imul__(self, other): if type(self) != type(other): return type(other)() * other return self + other def __idiv__(self, other): if type(self) != type(other): return type(other)() // other return self + other def __itruediv__(self, other): if type(self) != type(other): return type(other)() / other return self + other def lvalues(self): return py3lvalues(self)
Ancestors
- collections.OrderedDict
- builtins.dict
Subclasses
- backtrader.dataseries._Bar
- TradeHistory
Methods
def lvalues(self)
-
Expand source code
def lvalues(self): return py3lvalues(self)
class DotDict (*args, **kwargs)
-
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
Expand source code
class DotDict(dict): # If the attribut is not found in the usual places try the dict itself def __getattr__(self, key): if key.startswith('__'): return super(DotDict, self).__getattr__(key) return self[key]
Ancestors
- builtins.dict