Michael DeWitt fed6b3e34d v0.1.25
2014-04-10 14:11:56 -07:00

97 lines
2.5 KiB
Python

"""A wrapper for lists."""
import apifunction
import computedobject
import customfunction
import ee_exception
# Using lowercase function naming to match the JavaScript names.
# pylint: disable=g-bad-name
class List(computedobject.ComputedObject):
"""An object to represent lists."""
_initialized = False
def __init__(self, arg):
"""Construct a list wrapper.
This constuctor accepts the following args:
1) A bare list.
2) A ComputedObject returning a list.
Args:
arg: The list to wrap.
Raises:
ee_exception.EEException: On bad input.
"""
self.initialize()
if isinstance(arg, (list, tuple)):
super(List, self).__init__(None, None)
self._list = arg
elif isinstance(arg, computedobject.ComputedObject):
super(List, self).__init__(arg.func, arg.args, arg.varName)
self._list = None
else:
raise ee_exception.EEException(
'Invalid argument specified for ee.List(): %s' % arg)
@classmethod
def initialize(cls):
"""Imports API functions to this class."""
if not cls._initialized:
apifunction.ApiFunction.importApi(cls, 'List', 'List')
cls._initialized = True
@classmethod
def reset(cls):
"""Removes imported API functions from this class."""
apifunction.ApiFunction.clearApi(cls)
cls._initialized = False
@staticmethod
def name():
return 'List'
def encode(self, opt_encoder=None):
if isinstance(self._list, (list, tuple)):
return [opt_encoder(elem) for elem in self._list]
else:
return super(List, self).encode(opt_encoder)
def map(self, algorithm):
"""Maps an algorithm over a list.
Args:
algorithm: The operation to map over the list. A Python function that
receives an object returns one. The function is called only once
and the result is captured as a description, so it cannot perform
imperative operations or rely on external state.
Returns:
The mapped list.
Raises:
ee_exception.EEException: if algorithm is not a function.
"""
if not callable(algorithm):
raise ee_exception.EEException(
'Can\'t map non-callable object: %s' % algorithm)
signature = {
'name': '',
'returns': 'Object',
'args': [{
'name': None,
'type': 'Object',
}]
}
return self._cast(apifunction.ApiFunction.apply_('List.map', {
'list': self,
'baseAlgorithm': customfunction.CustomFunction(signature, algorithm)
}))