code – Tools for representing JavaScript code

Tools for representing JavaScript code in BSON.

  • class bson.code.Code(code, scope=None, **kwargs)
  • Bases: str

BSON’s JavaScript code type.

Raises TypeError if code is not an instance ofbasestring (str in python 3) or _scope_is not None or an instance of dict.

Scope variables can be set by passing a dictionary as the scope_argument or by using keyword arguments. If a variable is set as akeyword argument it will override any setting for that variable inthe _scope dictionary.

Parameters:

  • code: A string containing JavaScript code to be evaluated or anotherinstance of Code. In the latter case, the scope of code becomes thisCode’s scope.
  • scope (optional): dictionary representing the scope in whichcode should be evaluated - a mapping from identifiers (asstrings) to values. Defaults to None. This is applied after anyscope associated with a given code above.
  • **kwargs (optional): scope variables can also be passed askeyword arguments. These are applied after scope and code.

Changed in version 3.4: The default value for scope is None instead of {}.

  • scope
  • Scope dictionary for this instance or None.