-
Notifications
You must be signed in to change notification settings - Fork 0
types
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.
Booleans are identified by the singletons True
and False
.
True;
Floating-point numbers are identified simply by specifying a decimal-dotted number.
-1.234;
Integers are non-floating-point numbers.
5;
Strings are provided through the String
class, which is a sub-type of Container
, described later in this article.
None is a special singleton that serves much the same purpose as NULL in many other languages.
None;
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.
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.
Converts v
into a float.
If conversion fails, None is returned.
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.
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.
Prismscript offers general-purpose data-structures to ease the process of managing information.
Containers are abstract types that provide common functionality to their children.
The number of items in this container.
Returns a shallow copy of this container.
Returns True if item
is one of this container's keys.
Dictionaries provide script-writers with hashmap-based key-value stores. In a Python context, they extend dict.
- dict
Container
types.Dictionary();
types.Dictionary(items=[
[1, 2],
[3, 4]
]); #Initialised with 1->2 and 3->4
Returns the value associated with key
.
If no value is found, default
is returned instead.
Associates key
with value
.
Any previous value referenced by key
is overwritten.
Disassociates the value associated with key
.
If key
is not in the dictionary, this is a no-op.
Returns a sequence containing all items as [key, value] subsequences in arbitrary order.
Returns a sequence containing all keys in arbitrary order.
Returns a sequence containing all values in arbitrary order.
Values are not guaranteed to be unique.
Sets provide script-writers with efficient, order-agnostic mathematical sets. In a Python context, they extend set.
- set
Container
types.Set();
types.Set(items=[1, 2, 3, 3, 4]); #Any Python sequence can be used. Here, one of the 3s will be dropped.
Adds item
to this set.
If item
was already in this set, this is a no-op.
Removes item
from this set.
If item
was not in this set, this is a no-op.
Returns this set's items as a sequence.
Returns a set containing the difference between this set and other_set
.
Returns a set containing the intersection between this set and other_set
.
Returns a set containing the union between this set and other_set
.
Sequences provide script-writers with the functionality of lists and arrays. In a Python context, they extend list.
- list
Container
[];
[1, 'b', 3.25];
types.Sequence();
types.Sequence(items="Hello!"); #Any Python sequence will be split into elements.
Adds item
to the end of this sequence.
Adds item
to the start of this sequence.
Returns the item at index
, zero-based.
Inserts item
at index
, zero-based.
Removes the item at index
, zero-based.
Removes and returns the item at the start of this sequence.
Removes and returns the item at index
, zero-based, in this sequence.
Removes and returns the item at the end of this sequence.
Reverses the order of items in this sequence.
Nothing is returned.
Randomises the order of items in this sequence.
Nothing is returned.
Reorders the items in this sequence by ascending value.
Nothing is returned.
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.
Strings provide structured sequences of character data. Most notably, they are used to encode text.
- unicode (or str on py3k)
Container
"I am a string";
'So am I!';
Removes whitespace (or the members of characters
) from both ends.
Removes whitespace (or the members of characters
) from the left end.
Removes whitespace (or the members of characters
) from the right end.
An alias for strip()
.
An alias for lstrip()
.
An alias for rstrip()
.
Reverses the string.
Returns True if substring
occurs within the string.
Returns the number of times substring
occurs within the string.
Replaces up to limit
occurrences of old
with new
.
If limit
is 0, all instances are replaced.
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.
Returns True if the string ends with end
.
end
may be a sequence of strings to test for multiple possible endings simultaneously.
Returns True if the string starts with start
.
start
may be a sequence of strings to test for multiple possible beginnings simultaneously.
Returns the string without any characters from unwanted
.
unwanted
may be either a string or a sequence of single-character strings.
Returns the string with only characters from wanted
.
wanted
may be either a string or a sequence of single-character strings.
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.
Returns the string in lowercase.
Returns the string in uppercase.
Returns True if the string is non-empty and all of its characters are alphabetical.
Returns True if the string is non-empty and all of its characters are alphanumeric.
Returns True if the string is non-empty and all of its characters are digits.
Returns True if the string is non-empty and it contains a single number, possibly decimal, optionally prefixed with a negative sign.
Returns True if the string is a match for the regex given as r
.
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'
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.
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
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)
A boolean value, set after the thread has finished, that indicates whether an exception occurred.
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.
A boolean value that indicates whether the thread has finished running. In general, calling join()
is more convenient and efficient.
Blocks until the thread has finished running, ensuring that its result is available when operation resumes.
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
types.Lock();
A boolean value, indicating whether the lock is currently held by a thread.
An alias for acquired
.
Places a hold on the lock or blocks until the lock is obtainable.
An alias for acquire()
.
Releases a hold on the lock.
An alias for `release()
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.