:py:mod:`gnome.gnomeobject` =========================== .. py:module:: gnome.gnomeobject Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: gnome.gnomeobject.AddLogger gnome.gnomeobject.Refs gnome.gnomeobject.GnomeObjMeta gnome.gnomeobject.GnomeId Functions ~~~~~~~~~ .. autoapisummary:: gnome.gnomeobject.class_from_objtype gnome.gnomeobject.init_obj_log gnome.gnomeobject.combine_signatures gnome.gnomeobject.create_signatures Attributes ~~~~~~~~~~ .. autoapisummary:: gnome.gnomeobject.log gnome.gnomeobject.allowzip64 gnome.gnomeobject.SAVEFILE_VERSION .. py:data:: log .. py:data:: allowzip64 :value: False .. py:data:: SAVEFILE_VERSION :value: '5' .. py:function:: class_from_objtype(obj_type) object type must be a string in the gnome namespace: gnome.xxx.xxx .. py:function:: init_obj_log(obj, setLevel=logging.INFO) convenience function for initializing a logger with an object the logging.getLogger() will always return the same logger so calling this multiple times for same object is valid. By default adds a NullHandler() so we don't get errors if application using PyGnome hasn't configured a logging. .. py:class:: AddLogger(*args, **kwargs) Bases: :py:obj:`object` Mixin for including a logger .. py:property:: logger define attribute '_log'. If it doesn't exist, define it here. This is so we don't have to add it to all PyGnome classes - this property makes the logger available to each object. - default log_level is INFO .. py:property:: _pid returns os.getpid() as a string. Since we have multiple models, each running in its own process that is managed by multi_model_broadcast module, each debug log messages starts with os.getpid(). This function just returns a string that the gnome object can append to - don't want to keep typing this everywhere. .. py:attribute:: _log .. py:class:: Refs Bases: :py:obj:`dict` Class to store and handle references during saving/loading. Provides some convenience functions Initialize self. See help(type(self)) for accurate signature. .. py:method:: __setitem__(i, y) Set self[key] to value. .. py:method:: gen_default_name(obj) Goes through the dict, finds all objects of obj.obj_type stored, and provides a unique name by appending length+1 .. py:function:: combine_signatures(cls_or_func) Decorator to tag a function or class that needs a full function signature. CAUTION: Do not use this decorator on functions that do not pass kwargs up to super, or do so after alteration. Doing so will definitely give you a wrong signature. Use with care, verify your work. This may not work when applied to classes if they get imported in a particular sequence. If this occurs try applying this decorator to the __init__ directly. .. py:function:: create_signatures(cls, dct) Looks through the class for tagged functions, then builds and assigns signatures. Uses the MRO to find ancestor functions and merges the signatures. .. py:class:: GnomeObjMeta Bases: :py:obj:`type` .. py:class:: GnomeId(name=None, _appearance=None, *args, **kwargs) Bases: :py:obj:`AddLogger` A class for assigning a unique ID for an object .. py:property:: all_array_types Returns all the array types required by this object If this object contains or is composed of other gnome objects (Spill->Substance->Initializers for example) then override this function to ensure all array types get presented at the top level. See ``Spill`` for an example .. py:property:: id Override this method for more exotic forms of identification. :return: a unique ID assigned during construction .. py:property:: obj_type .. py:property:: name define as property in base class so all objects will have a name by default .. py:property:: _warn_pre standard text prepended to warning messages - not required for logging used by validate to prepend to message since it also returns a list of messages that were logged .. py:attribute:: _id .. py:attribute:: make_default_refs :value: True .. py:attribute:: _name .. py:attribute:: RTOL :value: 1e-05 .. py:attribute:: ATOL :value: 1e-38 .. py:method:: __create_new_id() Override this method for more exotic forms of identification. Used only for deep copy. Used to make a new object which is a copy of the original. .. py:method:: __deepcopy__(memo) The deepcopy implementation We need this, as we don't want the id of spill object and logger object copied, but do want everything else. Got the method from: http://stackoverflow.com/questions/3253439/python-copy-how-to-inherit-the-default-copying-behaviour Despite what that thread says for __copy__, the built-in deepcopy() ends up using recursion .. py:method:: __copy__() might as well have copy, too. .. py:method:: gather_ref_as(src, refs) Gathers refs from single or collection of GnomeId objects. :param src: GnomeId object or collection of GnomeId :param refs: dictionary of str->list of GnomeId :returns {'ref1': [list of GnomeId], 'ref2 : [list of GnomeId], ...} .. py:method:: _attach_default_refs(ref_dict) !!!IMPORTANT!!! If this object requires default references (self._req_refs exists), this function will use the name of the references as keys into a reference dictionary to get a list of satisfactory references (objects that have obj._ref_as == self._req_refs). It will then attach the first object in the reference list to that attribute on this object. This behavior can be overridden if the object needs more specific attachment behavior than simply 'first in line' In addition, this function SHOULD BE EXTENDED if this object should provide default references to any contained child objects. When doing so, please be careful to respect already existing references. The reference attachment system should only act if the requested reference 'is None' when the function is invoked. See Model._attach_default_refs() for an example. .. py:method:: validate_refs(refs=['wind', 'water', 'waves']) level is the logging level to use for messages. Default is 'warning' but if called from prepare_for_model_run, we want to use error and raise exception. .. py:method:: validate() All pygnome objects should be able to validate themselves. Many py_gnome objects reference other objects like wind, water, waves. These may not be defined when object is created so they can be None at construction time; however, they should reference valid objects when running in the model. If make_default_refs is True, then object is valid because the model will set these up at runtime. To raise an exception for missing references at runtime, directly call validate_refs(level='error') 'wind', 'water', 'waves' attributes also have special meaning. An object containing this attribute references the corresponding object. Logs warnings: :returns: a tuple of length two containing: (a list of messages that were logged, isvalid bool) If any references are missing and make_default_refs is False, object is not valid .. note: validate() only logs warnings since it designed to be used to validate before running model. To log these as errors during model run, invoke validate_refs() directly. .. py:method:: new_from_dict(dict_) :classmethod: creates a new object from dictionary This is base implementation and can be over-ridden by classes using this mixin .. py:method:: to_dict(json_=None) Returns a dictionary representation of this object. Uses the schema to determine which attributes are put into the dictionary. No extra processing is done to each attribute. They are presented as is. The ``json_`` parameter is ignored in this base class. 'save' is passed in when the schema is saving the object. This allows an override of this function to do any custom stuff necessary to prepare for saving. .. py:method:: update_from_dict(dict_, refs=None) .. py:method:: update(*args, **kwargs) .. py:method:: _attr_changed(current_value, received_value) :staticmethod: Checks if an attribute passed back in a ``dict_`` from client has changed. Returns True if changed, else False .. py:method:: _check_type(other) check basic type equality .. py:method:: __eq__(other) .. function:: __eq__(other) Since this class is designed as a mixin with one objective being to save _state of the object, then recreate a new object with the same _state. Defines a base implementation of __eq__ so an object before persistence can be compared with a new object created after it is persisted. It can be overridden by the class with which it is mixed. It looks at attributes defined in self._state and checks that the values match It uses allclose() check for floats and numpy arrays, to avoid floating point tolerances: set to: RTOL=1e-05, ATOL=1e-08 :param other: another GnomeObject used for comparison in obj1 == other NOTE: super is not used. .. py:method:: _diff(other, fail_early=False) Returns a list of differences between this GnomeObject and another GnomeObject :param other: other object to compare to. :param fail_early=False: If true, it will return on the first error .. py:method:: __ne__(other) Return self!=value. .. py:method:: serialize(options={}) Returns a json serialization of this object ("webapi" mode only) .. py:method:: deserialize(json_, refs=None) :classmethod: classmethod takes json structure as input, deserializes it using a colander schema then invokes the new_from_dict method to create an instance of the object described by the json schema. We also need to accept sparse json objects, in which case we will not treat them, but just send them back. .. py:method:: save(saveloc='.', refs=None, overwrite=True) Save object state as json to user specified saveloc :param saveloc: A directory, file path, open zipfile.ZipFile, or None. If a directory, it will place the zip file there, overwriting if specified. If a file path, it will write the file there as follows: If the file does not exist, it will create the zip archive there. If the saveloc is a zip file or ``zipfile.Zipfile`` object and overwrite is False, it will append there. Otherwise, it will overwrite the file if allowed. If set to None, this function will instead return an open ``zipfile.Zipfile`` object linked to a temporary file. The zip file will be named [object.name].zip if a directory is specified :param refs: dictionary of references to objects :param overwrite: If True, overwrites the file at the saveloc :returns (obj_json, saveloc, refs): ``obj_json`` is the json that is written to this object's file in the zipfile. For example if saving a Model named Model1, obj_json will contain the contents of the Model1.json in the save file. ``saveloc`` will be the string path passed in EXCEPT if None was passed in. In this case, it will be an open ``zipfile.ZipFile`` based on a temporary file. ``refs`` will be a dict containing all the objects that were saved in the save file, keyed by object id. It will also contain the reference to the object that called ``.save`` itself. .. py:method:: load(saveloc='.', filename=None, refs=None) :classmethod: Load an instance of this class from an archive or folder :param saveloc: Can be an open zipfile.ZipFile archive, a folder, or a filename. If it is an open zipfile or folder, it must contain a .json file that describes an instance of this object type. If 'filename' is not specified, it will load the first instance of this object discovered. If a filename, it must be a zip archive or a json file describing an object of this type. :param filename: If saveloc is an open zipfile or folder, this indicates the name of the file to be loaded. If saveloc is a filename, this parameter is ignored. :param refs: A dictionary of id -> object instances that will be used to complete references, if available.