Module: pyutils

Simple basic utils borrowed from Python builtins and libraries.

Both the builtins commands and the standard functions are implemented as functions.

Classes

AssertionError
CustomError
dict
KeyError
RuntimeError
ValueError

Members


<inner, constant> False :boolean

Alias of Javascript false.

Type:
  • boolean

<inner, constant> None :null

Alias of Javascript null.

Type:
  • null

<inner, constant> True :boolean

Alias of Javascript true.

Type:
  • boolean

<inner, constant> TYPE_ARRAY :string

Javascript native object type: Array

Type:
  • string
Default Value:
  • Array

<inner, constant> TYPE_BIGINT :string

Javascript native basic type: bigint

Type:
  • string
Default Value:
  • bigint

<inner, constant> TYPE_BOOLEAN :string

Javascript native basic type: boolean

Type:
  • string
Default Value:
  • boolean

<inner, constant> TYPE_FUNCTION :string

Javascript native basic type: function

Type:
  • string
Default Value:
  • function

<inner, constant> TYPE_NULL :string

Javascript native object type: null

Type:
  • string
Default Value:
  • null

<inner, constant> TYPE_NUMBER :string

Javascript native basic type: number

Type:
  • string
Default Value:
  • number

<inner, constant> TYPE_OBJECT :string

Javascript native object type: Object

Type:
  • string
Default Value:
  • Object

<inner, constant> TYPE_PY_DICT :string

Javascript reimplementation of Python type: dict {@ref dict}

Type:
  • string
Default Value:
  • dict

<inner, constant> TYPE_STRING :string

Javascript native basic type: string

Type:
  • string
Default Value:
  • string

<inner, constant> TYPE_STRING_OBJECT :string

Javascript native object type: String

Type:
  • string
Default Value:
  • String

<inner, constant> TYPE_SYMBOL :string

Javascript native basic type: symbol

Type:
  • string
Default Value:
  • symbol

<inner, constant> TYPE_UNDEFINED :string

Javascript native basic type: undefined

Type:
  • string
Default Value:
  • undefined

Methods


<inner> abs(x)

Return the absolute value of a number. The argument may be an integer or a floating point number or some other object that defines __abs__(). If x defines __abs__(), abs(x) returns x.__abs__().

Note: It has no specific support for complex numbers yet.

Python equivalent: https://docs.python.org/3/library/functions.html#abs

Parameters:
Name Type Description
x
Properties
Name Type Argument Description
__abs__ function <optional>
<nullable>
Returns:
Type
number

<inner> all(iterable)

Return True if all elements of the iterable are true (or if the iterable is empty).

Python equivalent: https://docs.python.org/3.9/library/functions.html#all

Parameters:
Name Type Description
iterable iterable.<*>
Returns:
Type
boolean

<inner> any(iterable)

Return True if any element of the iterable is true. If the iterable is empty, return False.

Python equivalent: https://docs.python.org/3/library/functions.html#any

Parameters:
Name Type Description
iterable iterable.<*>
Returns:
Type
boolean

<inner> ascii(object)

As {@ref repr}, return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by {@ref repr} using \x, \u or \U escapes.

Parameters:
Name Type Description
object
Returns:
Type
string

<inner> assert(condition, message, error_constructor)

Convenient way to insert debugging assertions into a program.

Parameters:
Name Type Default Description
condition boolean

Condition to test

message string null

Message to display in case of failure

error_constructor ErrorConstructor

Type of error to display


<inner> eval_(expression, globals, locals)

The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can be any mapping object. The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local namespace. If the globals dictionary is present and does not contain a value for the key __builtins__, a reference to the dictionary of the built-in module builtins is inserted under that key before expression is parsed. This means that expression normally has full access to the standard builtins module and restricted environments are propagated. If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed with the globals and locals in the environment where module:pyutils~eval_ is called. Note, module:pyutils~eval_ does not have access to the nested scopes (non-locals) in the enclosing environment.

The return value is the result of the evaluated expression. Syntax errors are reported as exceptions.

Example:

Parameters:
Name Type Default Description
expression
globals null
locals null
Returns:

TODO: to complete

Type
*
Example
let x = 1;
eval_("x + 1");
// returns 2

<inner> in_(value, sequence)

Checks whether the value is contained in the given sequence. Uses strict equality. If an object is provided it checks against its values.

Parameters:
Name Type Description
value *

The value to check for membership

sequence Iterable.<*>

Sequence withing to search the value

Returns:
Type
boolean

<inner> len(obj)

Parameters:
Name Type Description
obj
Returns:
Type
number

<inner> print(args)

Print function. So far works like an alias to console.log.

Parameters:
Name Type Argument Description
args <repeatable>

<generator, inner> range(start, stop, step)

Returns a range iterator in the given range.

Parameters:
Name Type Argument Default Description
start number

The starting value. If no

stop number <nullable>
null
step number <nullable>
1
Throws:

ValueError - If the step is 0


<inner> repr(object)

Return a string containing a printable representation of an object. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval, otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.

Note: Javascript does not provide a consistent memory reference of the object, and us such this function doesn't provide one either. Also, for type symbol it does not attempt to provide a string yielding such object, because a user might expect it to use it as the original object, when that's not the case, given the nature ot the symbol type.

Python equivalent: https://docs.python.org/3/library/functions.html#repr

Parameters:
Name Type Argument Description
object
Properties
Name Type Argument Description
__repr__ function <optional>
obect.name string <optional>
Returns:
Type
string

<inner> type(obj)

Provides the type of an object in the most sensible way.

Parameters:
Name Type Description
obj *

Any kind of object.

Returns:
Type
string | * | "undefined" | "object" | "boolean" | "number" | "string" | "function" | "symbol" | "bigint"