Lists¶
This section describes the following classes:
dragonfly.grammar.list.ListBase
– the base list classdragonfly.grammar.list.List
– sub-class of Python’s built-inlist
type. It can be updated and modified without reloading a grammar.dragonfly.grammar.list.DictList
– sub-class of Python’s built-indict
type. It can be updated and modified without reloading a grammar.
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.-
extend
(*args, **kwargs)[source]¶ L.extend(iterable) – extend list by appending elements from the iterable
-
pop
([index]) → item -- remove and return item at index (default last).[source]¶ Raises IndexError if list is empty or index is out of range.
-
remove
(*args, **kwargs)[source]¶ L.remove(value) – remove first occurrence of value. Raises ValueError if the value is not present.
-
sort
(*args, **kwargs)[source]¶ L.sort(cmp=None, key=None, reverse=False) – stable sort IN PLACE; cmp(x, y) -> -1, 0, 1
-
count
(value) → integer -- return number of occurrences of value¶
-
grammar
¶ Set-once access to a list’s grammar object.
-
index
(value[, start[, stop]]) → integer -- 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.-
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.
-
update
([E, ]**F) → None. Update D from dict/iterable E and F.[source]¶ If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, 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
(k[, d]) → D[k] if k in D, else d. d defaults to None.¶
-
grammar
¶ Set-once access to a list’s grammar object.
-
has_key
(k) → True if D has a key k, else False¶
-
items
() → list of D's (key, value) pairs, as 2-tuples¶
-
iteritems
() → an iterator over the (key, value) items of D¶
-
iterkeys
() → an iterator over the keys of D¶
-
itervalues
() → an iterator over the values of D¶
-
keys
() → list of 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
() → list of D's values¶
-
viewitems
() → a set-like object providing a view on D's items¶
-
viewkeys
() → a set-like object providing a view on D's keys¶
-
viewvalues
() → 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)})