negmas.serialization

Implements serialization to and from strings and secondary storage.

negmas.serialization.deserialize(d: Any, deep=True, remove_type_field=True, keep_private=False, fallback_class_name: str | None = None, base_module: str = '', deep_ignore: bool = True, ignored_keys: tuple[str, ...] = (), python_class_identifier='__python_class__', type_marker: str = '__TYPE__:', path_marker: str = '__PATH__:', lambda_marker: bytes = b'__LAMBDAOBJ__:', function_marker: bytes = b'__FUNCTION_START__:', cloudpickle_marker: bytes = b'__CLOUDPICKLE_START__:', extra_paths: tuple[str | Path, ...] = (), extra_modules: tuple[str, ...] = (), type_name_adapter: Callable[[str], str] | None = None, path_adapter: Callable[[str], str] | None = None)[source]

Decodes a dict/object coming from serialize

Parameters:
  • d – The value to be decoded. If it is not a dict, it is returned as it is.

  • deep – If true, decode recursively

  • remove_type_field – If true the field called PYTHON_CLASS_IDENTIFIER will be removed if found.

  • keep_private – If given, private fields (starting with _) will be kept

  • fallback_class_name – If given, it is used as the fall-back type if PYTHON_CLASS_IDENTIFIER is not in the dict.

  • ignored_keys – Keys to ignore

  • deep_ignore – if given, ignored keys are ignored in all components recusrively when deep is specified

Remarks:

  • If the object is not a dict or if it has no PYTHON_CLASS_IDENTIFIER field and no fallback_class_name is given, the input d is returned as it is. It will not even be copied.

See also

serialize, PYTHON_CLASS_IDENTIFIER

negmas.serialization.dump(d: Any, file_name: str | PathLike | Path, sort_keys=True, compact=False) None[source]

Saves an object depending on the extension of the file given. If the filename given has no extension, DEFAULT_DUMP_EXTENSION will be used.

Parameters:
  • d – Object to save

  • file_name – file name

  • sort_keys – If true, the keys will be sorted before saving

  • compact – If given, a compact representation will be tried

Remarks:

  • Supported formats are json, yaml

  • If None is given, the file will be created but will be empty

  • Numpy arrays will be converted to lists before being dumped

negmas.serialization.load(file_name: str | PathLike | Path) Any[source]

Loads an object depending on the extension of the file given. If the filename given has no extension, DEFAULT_DUMP_EXTENSION will be used.

Parameters:

file_name – file name

Remarks:

  • Supported formats are json, yaml

  • If None is given, the file will be created but will be empty

negmas.serialization.serialize(value, deep=True, add_type_field=True, keep_private=False, ignore_methods=True, ignore_lambda=False, shorten_type_field=False, objmem=None, python_class_identifier='__python_class__')[source]

Encodes the given value as nothing more complex than simple dict of either dicts, lists or builtin numeric or string values. The resulting dictionary will be json serializable

Parameters:
  • value (giving the type of) – Any object

  • deep – Whether we should go deep in the encoding or do a shallow encoding

  • add_type_field – Whether to add a type field. If True, A field named PYTHON_CLASS_IDENTIFIER will be added

  • value

  • keep_private – Keeps fields starting with “_”

  • shorten_type_field – IF given, the type will be shortened to class name only if it starts with “negmas.”

Remarks:

  • All iterables are converted to lists when deep is true.

  • If the value object has a to_dict member, it will be called to do the conversion, otherwise its __dict__ or __slots__ member will be used.

See also

deserialize, PYTHON_CLASS_IDENTIFIER

negmas.serialization.to_flat_dict(value, deep=True, add_type_field=False, shorten_type_field=False, python_class_identifier='__python_class__') dict[str, Any][source]

Encodes the given value as a flat dictionary

Parameters:
  • value – The value to be converted to a flat dictionary

  • deep – Converting all sub-objects

  • add_type_field – If true, a special field for the object type will be added

  • shorten_type_field – If true, the type field will be shortened to just class name if it is defined in NegMAS

Returns: