text_game_maker.parser package

Submodules

text_game_maker.parser.commands.add_commands(parser)
class text_game_maker.parser.parser.CharacterTrie

Bases: object

Simple trie structure where each node is a single character. Used to hold all action words for quick retrieval of the command object for a particular action word.

__init__()

x.__init__(…) initializes x; see help(type(x)) for signature

add_token(string, token)

Add an action word to the parser

Parameters:
  • string (str) – action word
  • token – object to set as token attribute of the last node of action word
dump_json()

Dump the whole trie as JSON-encoded text

Returns:JSON-encoded trie structure
Return type:str
get_children()

Return the text of all nodes below the node reached by the last run call. Used to generate action word suggestions when an action word is partially or incorrectly typed.

Returns:list of action words from all children nodes
Return type:[str]
iterate()

Iterate over all nodes in the trie that have non-None token attributes :return: iterator for all trie nodes with non-None tokens

run(input_string)
set_search_filter(callback)

Set function to filter token objects when iterating through the trie

Parameters:callback – callback function of the form callback(token), where token is the token attribute from a Node in the trie. If callback returns True, then this token object will be included in the objects returned by iteration. Otherwise, this token object will be exluded from the objects returned by iteration.
class text_game_maker.parser.parser.CharacterTrieNode(char, token=None, text=None)

Bases: object

A single node in a CharacterTrie

__init__(char, token=None, text=None)
Parameters:
  • char (str) – character for this node.
  • token – optional arbitrary object to store at this node.
  • text (str) – optional string to store at this node, currently used to hold the full matching text in the last node of a command. Allows for easy/quick iterating of all words in the trie.
class text_game_maker.parser.parser.Command(word_list, callback, desc, usage_fmt, hidden)

Bases: object

Container class for data needed to execute a particular game command

__init__(word_list, callback, desc, usage_fmt, hidden)
Parameters:
  • word_list (list) – list of action words that can trigger this parser command.
  • callback – callback function to be invoked when the player types something beginning with one of the action words. Callback should be of the form callback(player, word, remaining), where player is the text_game_maker.player.player.Player instance, word is the action word typed by the player as a string, and remaining is the remaining text following the action word as a string.
  • desc (str) – text that will be shown when player types “help” followed by one of the action words.
  • usage_fmt (str) – format string that will be used to show a usage example for each action word. The format string should contain a single string format character, which will be replaced with the action word e.g.``”%s <item>”`` where %s will be replaced with an action word.
  • hidden (bool) – if True, command can still be triggered normally by player input but will be excluded from “help” queries and parser suggestions.
help_text()

Get the help text with usage examples

Returns:generated help text and usage examples
Return type:str
class text_game_maker.parser.parser.CommandParser

Bases: text_game_maker.parser.parser.CharacterTrie

Thin wrapper around a CharacterTrie that adds some default commands

__init__()

x.__init__(…) initializes x; see help(type(x)) for signature

add_command(word_set, callback, help_text=None, usage_fmt=None, hidden=False)

Add a command to the parser.

Parameters:
  • word_set ([str]) – list of action words that can trigger command
  • callback – callback function to be invoked when the player types something beginning with one of the action words. Callback should be of the form callback(player, word, remaining), where player is the text_game_maker.player.player.Player instance, word is the action word typed by the player as a string, and remaining is the remaining text following the action word as a string.
  • help_text (str) – text that will be shown when player types “help” followed by one of the action words.
  • usage_fmt (str) – format string that will be used to show a usage example for each action word. The format string should contain a single string format character, which will be replaced with the action word e.g.``”%s <item>”`` where %s will be replaced with an action word.
  • hidden (bool) – if True, command can still be triggered normally by player input but will be excluded from “help” queries and parser suggestions.
add_event_handler(word, callback)

Add an event handler to run whenever a command is used, in addition to the regular handler for that command.

Parameters:
  • word (str) – action word associated with the command to add the event handler to.
  • callback – callback function to be invoked when the player types something beginning with one of the action words. Callback should be of the form callback(player, word, remaining), where player is the text_game_maker.player.player.Player instance, word is the action word typed by the player as a string, and remaining is the remaining text following the action word as a string.
clear_event_handler(word, callback)

Remove a previously added event handler for a command

Parameters:
  • word (str) – action word associated with the command to remove the event handler from.
  • callback – callback function for event handler to remove