pycrossword  0.4
Pure-Python implementation of a crossword puzzle generator and editor
Public Member Functions | Public Attributes | Private Member Functions | List of all members
pycross.crossword.Wordgrid Class Reference

Core crossword implementation - a grid of characters + internal Word objects. More...

Public Member Functions

def __init__ (self, data, data_type='grid', info=CWInfo(), on_reset=None, on_clear=None, on_change=None, on_clear_word=None, on_putchar=None)
 Constructor initializes the words and grid from the given grid data. More...
 
def initialize (self, data, data_type='grid')
 Initializes the internal char grid and words collection from given data. More...
 
def validate (self, grid)
 Checks if the grid is appropriate. More...
 
def reset (self, grid=None, update_internal_strings=False)
 Reconstructs the internal grid from the given grid data. More...
 
def from_words (self, words, update_internal_strings=False)
 Constructs the internal grid, dimensions and words from the given collection of Word objects. More...
 
def grid_from_file (self, gridfile)
 Util function that reads a text file into a list of strings suitable as internal grid data. More...
 
def from_file (self, filename, file_format=None)
 Initializes grid data from a crossword file, text file, or JSON-formatter Word collection dump. More...
 
def to_file (self, filename, file_format=None)
 Exports the crossword grid to a file. More...
 
def update_word_strings (self)
 Updates the internal strings for each word in grid. More...
 
def is_complete (self)
 Checks if all the words are completed (have no blanks left). More...
 
def remove_row (self, row)
 Removes the given row from grid. More...
 
def remove_column (self, col)
 Removes the given column from grid. More...
 
def add_row (self, index=-1, char=BLANK)
 Inserts a new row after the one given by index. More...
 
def add_column (self, index=-1, char=BLANK)
 Inserts a new column after the one given by 'index'. More...
 
def reflect_bottom (self, mirror=True, reverse=True, border='')
 Duplicates the current grid by reflecting its cells downwards. More...
 
def reflect_top (self, mirror=True, reverse=True, border='')
 Duplicates the current grid by reflecting its cells upwards. More...
 
def reflect_right (self, mirror=True, reverse=True, border='')
 Duplicates the current grid by reflecting its cells to the right. More...
 
def reflect_left (self, mirror=True, reverse=True, border='')
 Duplicates the current grid by reflecting its cells to the left. More...
 
def reflect (self, direction='d', mirror=True, reverse=True, border='')
 Duplicates the current grid by reflecting its cells in a given direction. More...
 
def intersects_of (self, word, word_coord_tuples=True)
 Finds all words intersecting the given word. More...
 
def find_incomplete (self, method='most-complete', exclude=None)
 Retrieves a next incomplete word (fully or partially blank). More...
 
def count_incomplete (self)
 Counts incomplete words (those containing at least one crossword::BLANK). More...
 
def get_word_str (self, w)
 Gets the text of a Word object in the grid. More...
 
def is_word_complete (self, w)
 Checks if a word is complete (has no blanks). More...
 
def is_word_blank (self, w)
 Checks if a word is blank (consists entirely of crossword::BLANK characters). More...
 
def find (self, word)
 Finds a Word object in the Wordgrid::words collection. More...
 
def find_by_coord (self, coord, start_coord=True)
 Retrieves words intersecting the given coordinate. More...
 
def find_by_coord_dir (self, coord, direction)
 Gets a word by its start coordinate and direction. More...
 
def find_by_str (self, str_word)
 Gets a word by its text representation. More...
 
def find_by_num_dir (self, num, direction)
 Gets a word by its sequential number and direction. More...
 
def find_by_clue (self, clue)
 Gets a word by its clue text. More...
 
def get_char (self, coord)
 Gets the text character stored in the given grid coordinate. More...
 
def put_char (self, coord, char)
 Puts a character into a given coordinate (replacing the existing one). More...
 
def clear (self)
 Clears all the words in the collection. More...
 
def change_word (self, word, str new_word)
 Replaces the text representation of a given word. More...
 
def clear_word (self, word, force_clear=False)
 Clears the given word making its characters blank. More...
 
def sort (self)
 Sorts the words collection by the word coordinates. More...
 
def print_word (self, w)
 Gets a human-readable representation of a word. More...
 
def print_words (self)
 Serializes (converts) the grid into a single human-readable string. More...
 
def print_clues (self)
 Serializes (converts) all clues into a single human-readable string. More...
 
def word_list (self, strings=True)
 Returns the collection (list) of all words in grid. More...
 
def tostr (self)
 Concatenates Wordgrid::grid into a single newline-delimited string. More...
 
def update_stats (self)
 Updates Wordgrid::stats dict with current handy statistics. More...
 
def save (self)
 Saves all words to Wordgrid::old_words to be able to restore() later. More...
 
def restore (self)
 Restores words from Wordgrid::old_words written by save(). More...
 
def __contains__ (self, word)
 The in operator overload: checks if words contain a given word. More...
 
def __bool__ (self)
 Convenience for is_complete(). More...
 
def __len__ (self)
 Python len() overload: returns number of words in grid. More...
 
def __str__ (self)
 Python str() convertion overload: represents a pretty output of the grid. More...
 

Public Attributes

 info
 CWInfo crossword meta info, such as title, author, etc. More...
 
 on_reset
 callback function triggered when the grid is reset via reset() More...
 
 on_clear
 callback function triggered when the grid is cleared via clear() More...
 
 on_change
 callback function triggered when a word is changed via change_word() More...
 
 on_clear_word
 callback function triggered when a word is cleared via clear_word() More...
 
 on_putchar
 callback function triggered when a charater is set in the grid via put_char() More...
 
 old_words
 backup of Wordgrid::words used in save() and restore() More...
 
 words
 internal words list (list of Word objects) More...
 
 grid
 internal 2-dimensional character grid, e.g. More...
 
 width
 number of columns in grid More...
 
 height
 number of rows in grid More...
 
 stats
 stats dictionary - various handy stats like grid dimensions, word count etc. More...
 

Private Member Functions

def _parse_ipuz (self, filename)
 Util function to parse IPUZ files. More...
 
def _save_ipuz (self, filename, ipuz_version='2', ipuz_kind='1')
 Util function to save the grid and info to an IPUZ file. More...
 
def _parse_xpf (self, filename)
 Util function to parse XPF files. More...
 
def _save_xpf (self, filename, xpf_version='1.0')
 Util function to save the grid and info to an XPF file. More...
 
def _strip_html (self, text)
 Util function: converts HTML to plain text. More...
 
def _validate_char (self, char)
 Validates characters to use in the crossword grid. More...
 
def _validate_coord (self, coord)
 Checks if the given coordinate lies within the grid dimensions. More...
 
def _cell_count (self, condition=None)
 Counts grid cells that satisfy a given condition. More...
 
def _word_count (self, condition=None)
 Counts words that satisfy a given condition. More...
 
def _word_lengths (self)
 Returns an array of word lengths. More...
 

Detailed Description

Core crossword implementation - a grid of characters + internal Word objects.

This class provides most of the methods required to operate with crosswords on the low level, such as adding/deleting words, file I/O, replacing characters, checking word completeness, counting grid stats and so on. Low-level methods work on the internal character grid, i.e. with individual characters. Higher-level methods work with Word objects (i.e. words). The class implements a 2-way correspondence between characters and words.

Constructor & Destructor Documentation

◆ __init__()

def pycross.crossword.Wordgrid.__init__ (   self,
  data,
  data_type = 'grid',
  info = CWInfo(),
  on_reset = None,
  on_clear = None,
  on_change = None,
  on_clear_word = None,
  on_putchar = None 
)

Constructor initializes the words and grid from the given grid data.

Parameters
datalist | str the input data to initialize the grid from; can be any of:
  • list list of grid rows, each row being itself either a concatenated string, e.g. '**_*', or a list of characters, e.g. ['_', '*', '_', '*']
  • list list of Word objects
  • str a single newline-delimited string representing the whole grid, see crossword::DEFAULT_GRID
  • str full path to an XPF or IPUZ crossword file, or a simple text file containing the cw grid
data_typestr hint used by the program to process the 'data' argument, can be any of:
  • 'grid' = data is a list or str-type grid
  • 'words' = data is a list of Word objects
  • 'file' = data is a file path
infoCWInfo crossword meta information, default = CWInfo default constructor
on_resetcallable callback function triggered when the grid is reset via reset(). Callback parameters are:
on_clearcallable callback function triggered when the grid is cleared via clear() Callback parameters are:
  • Wordgrid pointer to the calling class instance
on_changecallable callback function triggered when a word is changed via change_word() Callback parameters are:
  • Wordgrid pointer to the calling class instance
  • Word Word object being changed
  • str word's text before the change
on_clear_wordcallable callback function triggered when a word is cleared via clear_word() Callback parameters are:
  • Wordgrid pointer to the calling class instance
  • Word Word object being cleared
  • str word's text before clearing
on_putcharcallable callback function triggered when a charater is set in the grid via put_char() Callback parameters are:
  • Wordgrid pointer to the calling class instance
  • Coord | 2-tuple coordinate of the character
  • str character's previous value
  • str character's new value
Exceptions
crossword::CWErrorwrong 'data_type' value

Member Function Documentation

◆ __bool__()

def pycross.crossword.Wordgrid.__bool__ (   self)

Convenience for is_complete().

Returns
bool True if grid is complete, False otherwise

◆ __contains__()

def pycross.crossword.Wordgrid.__contains__ (   self,
  word 
)

The in operator overload: checks if words contain a given word.

Parameters
wordWord | str the Word object or its text representation to find in the grid
Returns
bool True if found, False otherwise
Exceptions
crossword::CWErrorword argument is incorrect

◆ __len__()

def pycross.crossword.Wordgrid.__len__ (   self)

Python len() overload: returns number of words in grid.

Returns
int number of words in grid

◆ __str__()

def pycross.crossword.Wordgrid.__str__ (   self)

Python str() convertion overload: represents a pretty output of the grid.

Returns
str grid as tabular-formatted text
See also
print_words(), print_clues(), tostr()

◆ _cell_count()

def pycross.crossword.Wordgrid._cell_count (   self,
  condition = None 
)
private

Counts grid cells that satisfy a given condition.

Parameters
conditioncallable callback function - the condition to satisfy. The callback's arguments are (in the sequential order):
  • int grid row index
  • int grid column index The callback returns True if the condition for the given (y, x) coordinate is satisfied and False otherwise. If 'condition' is None, all cells will be counted.
Returns
int number of cells that meet the given condition

◆ _parse_ipuz()

def pycross.crossword.Wordgrid._parse_ipuz (   self,
  filename 
)
private

Util function to parse IPUZ files.

Resets Wordgrid::grid and Wordgrid::info from the file data.

Parameters
filenamestr path to the source file (*.ipuz)
See also
http://www.ipuz.org/

◆ _parse_xpf()

def pycross.crossword.Wordgrid._parse_xpf (   self,
  filename 
)
private

Util function to parse XPF files.

Resets Wordgrid::grid and Wordgrid::info from the file data.

Parameters
filenamestr path to the source file (*.xpf)
See also
https://www.xwordinfo.com/XPF/

◆ _save_ipuz()

def pycross.crossword.Wordgrid._save_ipuz (   self,
  filename,
  ipuz_version = '2',
  ipuz_kind = '1' 
)
private

Util function to save the grid and info to an IPUZ file.

Exports data from Wordgrid::grid and Wordgrid::info to the file. Since IPUZ files are JSON-formatted, json.dump() is used for exporting.

Parameters
filenamestr path to the source file (*.ipuz)
ipuz_versionstr IPUZ API version (=2)
ipuz_kindstr IPUZ puzzle kind (=crossword)
See also
http://www.ipuz.org/

◆ _save_xpf()

def pycross.crossword.Wordgrid._save_xpf (   self,
  filename,
  xpf_version = '1.0' 
)
private

Util function to save the grid and info to an XPF file.

Exports data from Wordgrid::grid and Wordgrid::info to the file. Since XPF files are XML-formatted, ElementTree (DOM) is used for exporting.

Parameters
filenamestr path to the source file (*.xpf)
xpf_versionstr XPF API version (=1.0)
See also
https://www.xwordinfo.com/XPF/

◆ _strip_html()

def pycross.crossword.Wordgrid._strip_html (   self,
  text 
)
private

Util function: converts HTML to plain text.

Parameters
textstr HTML-formatted text
Returns
str plain text

◆ _validate_char()

def pycross.crossword.Wordgrid._validate_char (   self,
  char 
)
private

Validates characters to use in the crossword grid.

Only [a-z] (in any language, not ony ASCII!), crossword::BLANK, crossword::FILLER, and crossword::FILLER2 characters are allowed.

Parameters
charstr character to validate
Exceptions
crossword::CWErrorinvalid character

◆ _validate_coord()

def pycross.crossword.Wordgrid._validate_coord (   self,
  coord 
)
private

Checks if the given coordinate lies within the grid dimensions.

Parameters
coord2-tuple coordinate to validate
Exceptions
crossword::CWErrorcoordinate out of range

◆ _word_count()

def pycross.crossword.Wordgrid._word_count (   self,
  condition = None 
)
private

Counts words that satisfy a given condition.

Parameters
conditioncallable callback function - the condition to satisfy. The callback takes a single argument - a Word object and returns True if the condition is satisfied and False otherwise. If 'condition' is None, all words will be counted.
Returns
int number of words that meet the given condition

◆ _word_lengths()

def pycross.crossword.Wordgrid._word_lengths (   self)
private

Returns an array of word lengths.

Returns
list of int array of word lengths

◆ add_column()

def pycross.crossword.Wordgrid.add_column (   self,
  index = -1,
  char = BLANK 
)

Inserts a new column after the one given by 'index'.

If index == -1, appends a column after the last one.

Warning
Use with care! Destroys word structure.
Parameters
indexint index of column after which a new one will be inserted
charstr fill character for new column (default = crossword::BLANK)

◆ add_row()

def pycross.crossword.Wordgrid.add_row (   self,
  index = -1,
  char = BLANK 
)

Inserts a new row after the one given by index.

If index == -1, appends a row after the last one.

Warning
Use with care! Destroys word structure.
Parameters
indexint index of row after which a new one will be inserted
charstr fill character for new row (default = crossword::BLANK)

◆ change_word()

def pycross.crossword.Wordgrid.change_word (   self,
  word,
str  new_word 
)

Replaces the text representation of a given word.

The Wordgrid::on_change callback is called after the replacement.

Parameters
wordWord the Word object to replace the text for
new_wordstr the new text for the word, e.g. "father"
Exceptions
crossword::CWErrorword not found in grid or new text has incorrect length (different from the given word's length)

◆ clear()

def pycross.crossword.Wordgrid.clear (   self)

Clears all the words in the collection.

This method effectively puts crossword::BLANK into all non-blocked cells of the grid. The internal text representations of each Word object are not updated - call update_word_strings() after this operation if required. The Wordgrid::on_clear callback is called after clearing.

◆ clear_word()

def pycross.crossword.Wordgrid.clear_word (   self,
  word,
  force_clear = False 
)

Clears the given word making its characters blank.

Parameters
wordWord the Word object to clear
force_clearbool if True, ALL the characters in the word will be replaced by crossword::BLANK, regardless of intersecting words, if any. Otherwise (if False, which is the default), only the free characters will be cleared (that is, those not intersecting with other words). The Wordgrid::on_clear_word callback is called after clearing.

◆ count_incomplete()

def pycross.crossword.Wordgrid.count_incomplete (   self)

Counts incomplete words (those containing at least one crossword::BLANK).

Returns
int number of incomplete words

◆ find()

def pycross.crossword.Wordgrid.find (   self,
  word 
)

Finds a Word object in the Wordgrid::words collection.

Parameters
wordWord the Word object to look for
Returns
Word | None the found Word object or None if not found

◆ find_by_clue()

def pycross.crossword.Wordgrid.find_by_clue (   self,
  clue 
)

Gets a word by its clue text.

Warning
The search will return the FIRST word corresponding to the given clue. If the crossword contains more than one such clues (which it shouldn't, of course!) - all these other matches will be ignored!
Parameters
cluestr the clue text to search for, e.g. "US largest state"
Returns
Word | None the found Word object or None if not found

◆ find_by_coord()

def pycross.crossword.Wordgrid.find_by_coord (   self,
  coord,
  start_coord = True 
)

Retrieves words intersecting the given coordinate.

The method retrieves at most 2 words (across and down) for a given coordinate in the grid.

Parameters
coord2-tuple the (x, y) coordinate in the grid to find words for
start_coordbool if True (default), only those words that start in that coordinate will be returned; otherwise, all intersecting words are returned
Returns
dict a dict of found words in the format:
{'h': Word|None, 'v': Word|None}
where:
  • 'h' = Word object intersecting the coordinate in the Across (horizontal) direction (None if not found)
  • 'v' = Word object intersecting the coordinate in the Down (vertical) direction (None if not found)

◆ find_by_coord_dir()

def pycross.crossword.Wordgrid.find_by_coord_dir (   self,
  coord,
  direction 
)

Gets a word by its start coordinate and direction.

Parameters
coord2-tuple the start coordinate of the word looked for
directionstr the word's direction: 'h' = 'horizonal' or 'v' = 'vertical'
Returns
Word | None the found Word object or None if not found

◆ find_by_num_dir()

def pycross.crossword.Wordgrid.find_by_num_dir (   self,
  num,
  direction 
)

Gets a word by its sequential number and direction.

Parameters
numint the word's sequential number as stored in Word::num
directionstr the word's direction: 'h' = 'horizonal' or 'v' = 'vertical'
Returns
Word | None the found Word object or None if not found

◆ find_by_str()

def pycross.crossword.Wordgrid.find_by_str (   self,
  str_word 
)

Gets a word by its text representation.

Warning
The search will return the FIRST word corresponding to the given text. If the crossword contains more than one such word (which it shouldn't, of course!) - all these other matches will be ignored!
Parameters
str_wordstr the text to search for, e.g. "father"
Returns
Word | None the found Word object or None if not found

◆ find_incomplete()

def pycross.crossword.Wordgrid.find_incomplete (   self,
  method = 'most-complete',
  exclude = None 
)

Retrieves a next incomplete word (fully or partially blank).

Parameters
methodstr governs the search algorithm; it can be one of:
  • 'first-incomplete' (default): the first incomplete word will be returned
  • 'most-complete': the first word having the least blanks will be returned
  • 'most-incomplete': the first word having the most blanks will be returned (i.e. fully blank word)
  • 'random': a random incomplete word will be returned
excludecallable allows excluding words from search. It accepts a single argument - a Word object, and returns True to exclude it and False otherwise
Returns
Word | None a next incomplete Word object in the grid, or None if no such words are found

◆ from_file()

def pycross.crossword.Wordgrid.from_file (   self,
  filename,
  file_format = None 
)

Initializes grid data from a crossword file, text file, or JSON-formatter Word collection dump.

Parameters
filenamestr path to the source file The file type can be any of:
file_formatstr hint to tell the program the file format (must be 'xpf', 'ipuz', 'json' or None). If None, the file type will be guessed from the file extension.

◆ from_words()

def pycross.crossword.Wordgrid.from_words (   self,
  words,
  update_internal_strings = False 
)

Constructs the internal grid, dimensions and words from the given collection of Word objects.

This is essentially the reverse of reset(), which constructs words from a given grid structure.

Parameters
wordsiterable collection of source Word objects used to populate the grid
update_internal_stringsbool tells the function to update each Word object's string representation
Exceptions
crossword::CWErrorNone 'words' argument is not initialized or inappropriate

◆ get_char()

def pycross.crossword.Wordgrid.get_char (   self,
  coord 
)

Gets the text character stored in the given grid coordinate.

Parameters
coord2-tuple the grid coordinate
Returns
str the single character contained in that coordinate
Exceptions
crossword::CWErrorcoordinate out of range

◆ get_word_str()

def pycross.crossword.Wordgrid.get_word_str (   self,
  w 
)

Gets the text of a Word object in the grid.

Since the internal structure of Wordgrid is a matrix of characters rather than an array of words, word strings (text representations) are not available at runtime and must be requested with this method.

Parameters
wWord the Word object to get the text for
Returns
str the text representation of the word (e.g. "father")
See also
update_word_strings()

◆ grid_from_file()

def pycross.crossword.Wordgrid.grid_from_file (   self,
  gridfile 
)

Util function that reads a text file into a list of strings suitable as internal grid data.

Parameters
gridfilestr path to the source text file
Returns
list of str grid rows as a list of concatenated strings

◆ initialize()

def pycross.crossword.Wordgrid.initialize (   self,
  data,
  data_type = 'grid' 
)

Initializes the internal char grid and words collection from given data.

See also
See arguments and description in init()
Exceptions
crossword::CWErrorwrong 'data_type' value

◆ intersects_of()

def pycross.crossword.Wordgrid.intersects_of (   self,
  word,
  word_coord_tuples = True 
)

Finds all words intersecting the given word.

Parameters
wordWord the given Word object for which intersects are to be searched
word_coord_tuplesbool if True, the results will include both the intersecting words and the intersect coordinates (as list of 2-tuples); otherwise, only the list of intersecting Word objects is returned
Returns
list list of Word objects or (Word, coord) tuples depending on word_coord_tuples

◆ is_complete()

def pycross.crossword.Wordgrid.is_complete (   self)

Checks if all the words are completed (have no blanks left).

Returns
bool True if grid contains no blanks, False otherwise

◆ is_word_blank()

def pycross.crossword.Wordgrid.is_word_blank (   self,
  w 
)

Checks if a word is blank (consists entirely of crossword::BLANK characters).

Parameters
wWord the Word object
Returns
bool True if the word has no characters other than crossword::BLANK, False otherwise
See also
is_word_complete() - the reverse method

◆ is_word_complete()

def pycross.crossword.Wordgrid.is_word_complete (   self,
  w 
)

Checks if a word is complete (has no blanks).

Parameters
wWord the Word object
Returns
bool True if the word has no crossword::BLANK characters, False otherwise
See also
is_word_blank() - the reverse method

◆ print_clues()

def pycross.crossword.Wordgrid.print_clues (   self)

Serializes (converts) all clues into a single human-readable string.

Returns
str the text representation of the clues in the format:
   [word_number]   word_direction      clue text
   [word_number]   word_direction      clue text
   ...
 

◆ print_word()

def pycross.crossword.Wordgrid.print_word (   self,
  w 
)

Gets a human-readable representation of a word.

Parameters
wWord the Word object
Returns
str a string formatted like so: '(coord_x, coord_y) dir word_text'
Exceptions
crossword::CWErrorword is not found in grid

◆ print_words()

def pycross.crossword.Wordgrid.print_words (   self)

Serializes (converts) the grid into a single human-readable string.

Returns
str the text representation of the crossword grid in a tabular format, e.g.
   Num       Coord       Value
   ----------------------------
   ACROSS:
   ----------------------------
   1         (0, 0)      father
   5         (0, 10)     quit
   9         (0, 15)     storm
   ----------------------------
   DOWN:
   ----------------------------
   ...
 

◆ put_char()

def pycross.crossword.Wordgrid.put_char (   self,
  coord,
  char 
)

Puts a character into a given coordinate (replacing the existing one).

The Wordgrid::on_putchar callback is called after putting the character.

Parameters
coord2-tuple the grid coordinate to write to
charstr the character to put
Exceptions
crossword::CWErrorinvalid character

◆ reflect()

def pycross.crossword.Wordgrid.reflect (   self,
  direction = 'd',
  mirror = True,
  reverse = True,
  border = '' 
)

Duplicates the current grid by reflecting its cells in a given direction.

Parameters
directionstr the direction to duplicate / reflect the current grid:
  • 'd[own]' = downwards
  • 'u[p]' = upwards
  • 'r[ight]' = to the right
  • 'l[eft]' = to the left
mirrorbool if True (default), reflection will be mirror-like (symmetrical against bottom line).
reversebool if True (default), reflection will be inverted from left to right.
borderstr fill pattern for a border (row or column) between the existing and reflected blocks of cells.

If both mirror and reflect are False, a simle copy-paste duplication will be performed. If border is an empty string (default), no extra row will be put between the existing and the new (reflected) cells. Otherwise, border is a 2-character pattern for the extra row, e.g. '* ' means sequence of crossword::FILLER and crossword::BLANK, '**' means all filled, etc.

◆ reflect_bottom()

def pycross.crossword.Wordgrid.reflect_bottom (   self,
  mirror = True,
  reverse = True,
  border = '' 
)

Duplicates the current grid by reflecting its cells downwards.

See also
reflect() for description of arguments.

◆ reflect_left()

def pycross.crossword.Wordgrid.reflect_left (   self,
  mirror = True,
  reverse = True,
  border = '' 
)

Duplicates the current grid by reflecting its cells to the left.

See also
reflect() for description of arguments.

◆ reflect_right()

def pycross.crossword.Wordgrid.reflect_right (   self,
  mirror = True,
  reverse = True,
  border = '' 
)

Duplicates the current grid by reflecting its cells to the right.

See also
reflect() for description of arguments.

◆ reflect_top()

def pycross.crossword.Wordgrid.reflect_top (   self,
  mirror = True,
  reverse = True,
  border = '' 
)

Duplicates the current grid by reflecting its cells upwards.

See also
reflect() for description of arguments.

◆ remove_column()

def pycross.crossword.Wordgrid.remove_column (   self,
  col 
)

Removes the given column from grid.

Warning
Use with care! Destroys word structure.
Parameters
colint index of column to delete

◆ remove_row()

def pycross.crossword.Wordgrid.remove_row (   self,
  row 
)

Removes the given row from grid.

Warning
Use with care! Destroys word structure.
Parameters
rowint index of row to delete

◆ reset()

def pycross.crossword.Wordgrid.reset (   self,
  grid = None,
  update_internal_strings = False 
)

Reconstructs the internal grid from the given grid data.

The following class members are fully re-initialized:

  • Wordgrid::words - the collection of Word objects
  • Wordgrid::grid - the internal grid pattern OR the existing self.grid if == None
  • Wordgrid::width - the horizontal size (in cells)
  • Wordgrid::height - the vertical size (in cells)
    Parameters
    gridlist | str crossword grid – see 'data' argument in init() If None, Wordgrid::grid will be used, if initialized (otherwise, an exception will be raised)
    update_internal_stringsbool tells the function to update each Word object's string representation
    Exceptions
    crossword::CWErrorempty / null 'grid' argument and Wordgrid::grid not initialized

◆ restore()

def pycross.crossword.Wordgrid.restore (   self)

Restores words from Wordgrid::old_words written by save().

◆ save()

def pycross.crossword.Wordgrid.save (   self)

Saves all words to Wordgrid::old_words to be able to restore() later.

◆ sort()

def pycross.crossword.Wordgrid.sort (   self)

Sorts the words collection by the word coordinates.

The sorting is performed in the assending order: first on rows, then on columns. This corresponds to the typical crossword word numeration.

◆ to_file()

def pycross.crossword.Wordgrid.to_file (   self,
  filename,
  file_format = None 
)

Exports the crossword grid to a file.

See also
Description of arguments in from_file()

◆ tostr()

def pycross.crossword.Wordgrid.tostr (   self)

Concatenates Wordgrid::grid into a single newline-delimited string.

Returns
str the concatenated grid string, e.g.
   father*__it*__
   _*_*_*act_v_ty
   ...
 

◆ update_stats()

def pycross.crossword.Wordgrid.update_stats (   self)

Updates Wordgrid::stats dict with current handy statistics.

◆ update_word_strings()

def pycross.crossword.Wordgrid.update_word_strings (   self)

Updates the internal strings for each word in grid.

◆ validate()

def pycross.crossword.Wordgrid.validate (   self,
  grid 
)

Checks if the grid is appropriate.

The grid must:

◆ word_list()

def pycross.crossword.Wordgrid.word_list (   self,
  strings = True 
)

Returns the collection (list) of all words in grid.

Parameters
stringsbool if True (default) the words' texts are returned, otherwise the Word objects
Returns
list list of str or Word objects

Member Data Documentation

◆ grid

pycross.crossword.Wordgrid.grid

internal 2-dimensional character grid, e.g.

 [['f', 'a', 't', 'h', 'e', 'r', '*', '_', 'o', 'm'], [...]] 

◆ height

pycross.crossword.Wordgrid.height

number of rows in grid

◆ info

pycross.crossword.Wordgrid.info

CWInfo crossword meta info, such as title, author, etc.

◆ old_words

pycross.crossword.Wordgrid.old_words

backup of Wordgrid::words used in save() and restore()

◆ on_change

pycross.crossword.Wordgrid.on_change

callback function triggered when a word is changed via change_word()

◆ on_clear

pycross.crossword.Wordgrid.on_clear

callback function triggered when the grid is cleared via clear()

◆ on_clear_word

pycross.crossword.Wordgrid.on_clear_word

callback function triggered when a word is cleared via clear_word()

◆ on_putchar

pycross.crossword.Wordgrid.on_putchar

callback function triggered when a charater is set in the grid via put_char()

◆ on_reset

pycross.crossword.Wordgrid.on_reset

callback function triggered when the grid is reset via reset()

◆ stats

pycross.crossword.Wordgrid.stats

stats dictionary - various handy stats like grid dimensions, word count etc.

◆ width

pycross.crossword.Wordgrid.width

number of columns in grid

◆ words

pycross.crossword.Wordgrid.words

internal words list (list of Word objects)


The documentation for this class was generated from the following file: