earthengine-api/python/ee/dictionary.py
Google Earth Engine Authors aed64b3ffc v0.1.365
PiperOrigin-RevId: 557617293
2023-08-23 20:23:39 +00:00

69 lines
2.0 KiB
Python

#!/usr/bin/env python
"""A wrapper for dictionaries."""
from ee import apifunction
from ee import computedobject
class Dictionary(computedobject.ComputedObject):
"""An object to represent dictionaries."""
_initialized = False
# Tell pytype to not complain about dynamic attributes.
_HAS_DYNAMIC_ATTRIBUTES = True
def __init__(self, arg=None):
"""Construct a dictionary.
Args:
arg: This constructor accepts the following args:
1) Another dictionary.
2) A list of key/value pairs.
3) A null or no argument (producing an empty dictionary)
"""
self.initialize()
if isinstance(arg, dict):
super().__init__(None, None)
self._dictionary = arg
else:
self._dictionary = None
if (isinstance(arg, computedobject.ComputedObject)
and arg.func
and arg.func.getSignature()['returns'] == 'Dictionary'):
# If it's a call that's already returning a Dictionary, just cast.
super().__init__(arg.func, arg.args, arg.varName)
else:
# Delegate everything else to the server-side constructor.
super().__init__(apifunction.ApiFunction('Dictionary'), {'input': arg})
@classmethod
def initialize(cls) -> None:
"""Imports API functions to this class."""
if not cls._initialized:
apifunction.ApiFunction.importApi(cls, 'Dictionary', 'Dictionary')
cls._initialized = True
@classmethod
def reset(cls) -> None:
"""Removes imported API functions from this class."""
apifunction.ApiFunction.clearApi(cls)
cls._initialized = False
@staticmethod
def name() -> str:
return 'Dictionary'
def encode(self, opt_encoder=None):
if self._dictionary is not None:
return opt_encoder(self._dictionary)
else:
return super().encode(opt_encoder)
def encode_cloud_value(self, opt_encoder=None):
if self._dictionary is not None:
return {'valueReference': opt_encoder(self._dictionary)}
else:
return super().encode_cloud_value(opt_encoder)