Lists

This section describes the following classes:

The List Updates section discusses possible performance issues with modifying Dragonfly lists and ways to avoid these issues altogether.

List classes

class ListBase(name)[source]

Base class for dragonfly list objects.

valid_types

The types of object at a Dragonfly list can contain.

name

Read-only access to a list’s name.

grammar

Set-once access to a list’s grammar object.

class List(name, *args, **kwargs)[source]

Wrapper for Python’s built-in list that supports automatic engine notification of changes.

Use ListRef elements in a grammar rule to allow matching speech to list items.

set(other)[source]

Set the contents of this list to the contents of another.

append(*args, **kwargs)[source]

Append object to the end of the list.

extend(*args, **kwargs)[source]

Extend list by appending elements from the iterable.

insert(*args, **kwargs)[source]

Insert object before index.

pop(*args, **kwargs)[source]

Remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

remove(*args, **kwargs)[source]

Remove first occurrence of value.

Raises ValueError if the value is not present.

reverse(*args, **kwargs)[source]

Reverse IN PLACE.

sort(*args, **kwargs)[source]

Stable sort IN PLACE.

clear()[source]

Remove all items from list.

copy()

Return a shallow copy of the list.

count()

Return number of occurrences of value.

grammar

Set-once access to a list’s grammar object.

index()

Return first index of value.

Raises ValueError if the value is not present.

name

Read-only access to a list’s name.

valid_types

The types of object at a Dragonfly list can contain.

class DictList(name, *args, **kwargs)[source]

Wrapper for Python’s built-in dict that supports automatic engine notification of changes. The object’s keys are used as the elements of the engine list, while use of the associated values is left to the user.

Use DictListRef elements in a grammar rule to allow matching speech to dictionary keys.

set(other)[source]

Set the contents of this dict to the contents of another.

clear() → None. Remove all items from D.[source]
fromkeys(*args, **kwargs)[source]

Create a new dictionary with keys from iterable and values set to value.

pop(k[, d]) → v, remove specified key and return the corresponding value.[source]

If key is not found, d is returned if given, otherwise KeyError is raised

popitem() → (k, v), remove and return some (key, value) pair as a[source]

2-tuple; but raise KeyError if D is empty.

setdefault(*args, **kwargs)[source]

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) → None. Update D from dict/iterable E and F.[source]

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

copy() → a shallow copy of D
get()

Return the value for key if key is in the dictionary, else default.

grammar

Set-once access to a list’s grammar object.

items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
name

Read-only access to a list’s name.

valid_types

The types of object at a Dragonfly list can contain.

values() → an object providing a view on D's values

List Updates

Lists are updated after each modifying operation, e.g. list.append(), list.remove(), dict[key] = value, dict.pop(), etc. This is fine for a few list modifications here and there, but is inefficient for adding / removing many items at once.

The simplest solution is to use the ListBase context manager:

# Do list modification inside a 'with' block to only do one list update
# at the end.
my_list = List("my_list")
with my_list:
    for x in range(50):
        my_list.append(str(x))

Some methods like list.extend() or dict.update() will also only update the list once afterwards:

# Add multiple list items using extend().
my_list = List("my_list")
my_list.extend([str(x) for x in range(50)])

# Add multiple dictionary keys using update().
dictionary = DictList("dictionary")
dictionary.update({str(x):x for x in range(50)})