Skip to content
Neil Tallim edited this page Jul 14, 2015 · 1 revision

Introduction

Prismscript offers a small assortment of standard types to meet the needs of its users. When an external function is invoked, the returned object is marshalled into one of these formats, if necessary, to make it available for use by scripts.

Types

Literals

bool

Booleans are identified by the singletons True and False.

True;

float

Floating-point numbers are identified simply by specifying a decimal-dotted number.

-1.234;

int

Integers are non-floating-point numbers.

5;

string

Strings are provided through the String class, which is a sub-type of Container, described later in this article.

None

None is a special singleton that serves much the same purpose as NULL in many other languages.

None;

Literal conversion functions

Literals can be coaxed from one form to another, allowing anything to be represented as a string or boolean and permitting number-bearing strings to have their numeric value extracted.

All of the following functions are pre-defined in the types namespace, so, for example, to convert the string '512' into the integer 512, a statement like x = types.int(v='512'); would work.

bool(v)

Returns the boolean value of v.

Resolution occurs as normal for Python, with 0, 0.0, None, False, '', and [] returning False, and most other, non-empty, things returning True.

This function is not needed in conditional statements, since boolean-conversion occurs implicitly.

float(v)

Converts v into a float.

If conversion fails, None is returned.

int(v, base=None, is_char=False)

Converts v into an int.

If base is defined (2-36), it is used when converting the value.

x = types.int(v='-21'); //-21
x = types.int(v='5.21'); //5
x = types.int(v='0b10', base=2); //2; the 0b is optional.
x = types.int(v='0xd6', base=16); //214; the 0x is optional.
x = types.int(v='0o10', base=8); //8; the 0o is optional.

If is_char is defined and base is not defined, v, a single character, is converted to its integral ASCII value.

If conversion fails, None is returned.

string(v, int_base=None, is_char=False)

Converts v into a String.

If int_base is specified (2-36) and v is an integer, the returned string will be a common representation of that number in the specified base. If conversion fails, an exception is raised.

If is_char is defined and int_base is not defined, v, an ASCII-range integer, is converted into a single-character string.

Other conversions cannot fail, but the returned string is dependent upon Python's processing, so results may be unexpected if dealing with non-primitive types.

Data-structures

Prismscript offers general-purpose data-structures to ease the process of managing information.

Container

Containers are abstract types that provide common functionality to their children.

Attributes

length

The number of items in this container.

Methods

copy()

Returns a shallow copy of this container.

contains(item)

Returns True if item is one of this container's keys.

Dictionary

Dictionaries provide script-writers with hashmap-based key-value stores. In a Python context, they extend dict.

Ancestry

  • dict
  • Container

Declaration examples

types.Dictionary();
types.Dictionary(items=[
 [1, 2],
 [3, 4]
]); #Initialised with 1->2 and 3->4

Methods

get(key, default=None)

Returns the value associated with key.

If no value is found, default is returned instead.

put(key, value)

Associates key with value.

Any previous value referenced by key is overwritten.

remove(key)

Disassociates the value associated with key.

If key is not in the dictionary, this is a no-op.

get_items()

Returns a sequence containing all items as [key, value] subsequences in arbitrary order.

get_keys()

Returns a sequence containing all keys in arbitrary order.

get_values()

Returns a sequence containing all values in arbitrary order.

Values are not guaranteed to be unique.

Set

Sets provide script-writers with efficient, order-agnostic mathematical sets. In a Python context, they extend set.

Ancestry

  • set
  • Container

Declaration examples

types.Set();
types.Set(items=[1, 2, 3, 3, 4]); #Any Python sequence can be used. Here, one of the 3s will be dropped.

Methods

add(item)

Adds item to this set.

If item was already in this set, this is a no-op.

remove(item)

Removes item from this set.

If item was not in this set, this is a no-op.

get_items()

Returns this set's items as a sequence.

difference(other_set)

Returns a set containing the difference between this set and other_set.

intersection(other_set)

Returns a set containing the intersection between this set and other_set.

union(other_set)

Returns a set containing the union between this set and other_set.

Sequence

Sequences provide script-writers with the functionality of lists and arrays. In a Python context, they extend list.

Ancestry

  • list
  • Container

Declaration examples

[];
[1, 'b', 3.25];
types.Sequence();
types.Sequence(items="Hello!"); #Any Python sequence will be split into elements.

Methods

append(item)

Adds item to the end of this sequence.

prepend(item)

Adds item to the start of this sequence.

get(index)

Returns the item at index, zero-based.

insert(index, item)

Inserts item at index, zero-based.

remove(index)

Removes the item at index, zero-based.

pop_head()

Removes and returns the item at the start of this sequence.

pop_item(index)

Removes and returns the item at index, zero-based, in this sequence.

pop_tail()

Removes and returns the item at the end of this sequence.

reverse()

Reverses the order of items in this sequence.

Nothing is returned.

shuffle()

Randomises the order of items in this sequence.

Nothing is returned.

sort()

Reorders the items in this sequence by ascending value.

Nothing is returned.

slice(start=None, end=None)

Returns a subsection of this sequence, containing all items between start and end.

If either start or end is omitted, everything through to the appropriate end of the sequence is returned.

If both start and end are omitted, the sequence is copied.

If either value is negative, counting for that value occurs from the end of the sequence.

String

Strings provide structured sequences of character data. Most notably, they are used to encode text.

Ancestry

  • unicode (or str on py3k)
  • Container

Declaration examples

"I am a string";
'So am I!';

Methods

strip(characters=None)

Removes whitespace (or the members of characters) from both ends.

lstrip(characters=None)

Removes whitespace (or the members of characters) from the left end.

rstrip(characters=None)

Removes whitespace (or the members of characters) from the right end.

trim(characters=None)

An alias for strip().

ltrim(characters=None)

An alias for lstrip().

rtrim(characters=None)

An alias for rstrip().

reverse()

Reverses the string.

contains(substring)

Returns True if substring occurs within the string.

count(substring)

Returns the number of times substring occurs within the string.

replace(old, new, limit=0)

Replaces up to limit occurrences of old with new.

If limit is 0, all instances are replaced.

split(delimiter=' ', limit=0, from_left=True)

Returns up to limit tokens, split by delimiter.

If limit is 0, all possible tokens are split.

If from_left is False, splitting begins from the right side of the string.

ends_with(end)

Returns True if the string ends with end.

end may be a sequence of strings to test for multiple possible endings simultaneously.

starts_with(start)

Returns True if the string starts with start.

start may be a sequence of strings to test for multiple possible beginnings simultaneously.

filter(unwanted)

Returns the string without any characters from unwanted.

unwanted may be either a string or a sequence of single-character strings.

filter_except(wanted)

Returns the string with only characters from wanted.

wanted may be either a string or a sequence of single-character strings.

filter_unprintable()

Returns the string without any unprintable characters. This may be important if presenting a string to a very simple device where having something readable is more important than having everything.

lower()

Returns the string in lowercase.

upper()

Returns the string in uppercase.

is_alpha()

Returns True if the string is non-empty and all of its characters are alphabetical.

is_alphanumeric()

Returns True if the string is non-empty and all of its characters are alphanumeric.

is_digit()

Returns True if the string is non-empty and all of its characters are digits.

is_number()

Returns True if the string is non-empty and it contains a single number, possibly decimal, optionally prefixed with a negative sign.

is_regex_match(r)

Returns True if the string is a match for the regex given as r.

format(values)

Formats the string in the same manner as Python's C-derived str % tuple notation, only without mapping names.

x = "hello, %s".format(['Bob']); // x = 'hello, Bob'
slice(start=None, end=None)

Returns a subsection of this string, containing all items between start and end.

If either start or end is omitted, everything through to the appropriate end of the string is returned.

If both start and end are omitted, the string is copied.

If either value is negative, counting for that value occurs from the end of the string.

Thread

Threads allow for concurrent execution of functions, so that time-consuming tasks can be hidden behind interactive events for a more seamless presentation.

Thread support may be disabled in some interpreter contexts

Declaration examples

types.Thread(_f=api.synthesise, text="hello"); #External function form
types.Thread(_f='local_function', x=5); #Internal function form

_f is required and indicates the function to be threaded; functions defined in-script need to be specified as strings, while anything bound via scoping or assignment is provided as an identifier.

Any other arguments are passed to the function when it is invoked.

The function being invoked cannot use, at any point, the co-routine interface, since that requires interaction with the interpreter's controller and threads are purely behind-the-scenes. (And daemonic)

Attributes

exception

A boolean value, set after the thread has finished, that indicates whether an exception occurred.

result

Any value, set after the thread has finished, that contains the function's return-value. If an exception occurred, this will be an exception instance.

running

A boolean value that indicates whether the thread has finished running. In general, calling join() is more convenient and efficient.

Methods

join()

Blocks until the thread has finished running, ensuring that its result is available when operation resumes.

Lock

Locks allow for global variables to be shared between threads without race conditions. In general, you shouldn't need them, since threads will normally be fetching a resource or performing an external operation and their result is available as a property of the object.

Locks are re-entrant and must be released as many times as they are acquired. A lock need not be released by the same thread that acquired it, but it'sgenerally a good idea to do so.

Lock support may be disabled in some interpreter contexts

Declaration examples

types.Lock();

Attributes

acquired

A boolean value, indicating whether the lock is currently held by a thread.

locked

An alias for acquired.

Methods

acquire()

Places a hold on the lock or blocks until the lock is obtainable.

lock()

An alias for acquire().

release()

Releases a hold on the lock.

unlock()

An alias for `release()

release_dead(omit_current_thread)

A special-case function that can be used to restore a lock to working condition in the event of a thread that didn't clean up properly, meant for behind-the-scenes operation to avoid having lazy code lead to avoidable deadlocks.

If omit_current_thread, a boolean value, is set, the calling thread is considered dead for purposes of evaluation.

You should never need to use this method, since its use means you know your code has a logic error that can be fixed through structural revision.

If the lock is released by this operation, the offending thread instance is returned.

Clone this wiki locally