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

Base class for word source objects. More...

Inheritance diagram for pycross.wordsrc.Wordsource:
pycross.wordsrc.DBWordsource pycross.wordsrc.MultiWordsource pycross.wordsrc.TextWordsource pycross.wordsrc.TextfileWordsource

Public Member Functions

def __init__ (self, max_fetch=None, shuffle=True)
 Constructor. More...
 
def isvalid (self)
 Checks if it is a valid word source. More...
 
def truncate (self, suggestions)
 Truncates the results by the threshold number stored in Wordsource::max_fetch. More...
 
def shuffle (self, suggestions)
 Shuffles the results randomly. More...
 
def fetch (self, word=None, blank=' ', pos=None, filter_func=None, shuffle=True, truncate=True)
 Fetches suggestions (as a list of strings) for a given word pattern (mask). More...
 
def check (self, word, pos=None, filter_func=None)
 Checks if a given word or word pattern is found in the word source. More...
 
def pop_word (self, suggestions)
 Retrieves the last suggestion (word) from the list of suggestions, removing that word from the original results. More...
 
def __repr__ (self)
 Python repr() overload. More...
 
def __bool__ (self)
 Python bool() overload. More...
 

Public Attributes

 max_fetch
 int maximum number of suggestions returned from the word source More...
 
 shuffle_words
 bool if True, fetched words will be shuffled More...
 
 active
 bool if True, this word source will be used; otherwise it will be ignored More...
 

Detailed Description

Base class for word source objects.

Provides core methods for fetching, shuffling, truncating the results and other bare-bones stuff.

Constructor & Destructor Documentation

◆ __init__()

def pycross.wordsrc.Wordsource.__init__ (   self,
  max_fetch = None,
  shuffle = True 
)

Constructor.

Parameters
max_fetchint maximum number of suggestions returned from the word source (None means no limit on suggestions, which may be time/resource consuming!)
shufflebool if True, fetched words will be shuffled

Reimplemented in pycross.wordsrc.MultiWordsource.

Member Function Documentation

◆ __bool__()

def pycross.wordsrc.Wordsource.__bool__ (   self)

Python bool() overload.

Returns
bool the result of isvalid()

◆ __repr__()

def pycross.wordsrc.Wordsource.__repr__ (   self)

Python repr() overload.

Returns
str human-readable representation of Wordsource object

◆ check()

def pycross.wordsrc.Wordsource.check (   self,
  word,
  pos = None,
  filter_func = None 
)

Checks if a given word or word pattern is found in the word source.

Parameters
wordstr the word (pattern) to find, e.g. 'f ther' or 'father'
posstr | iterable | None part(s) of speech to include in the results - see fetch()
filter_funccallable filtering callback function to exclude words from results - see fetch()
Returns
bool True if the given word (pattern) can be found in the source, False otherwise

Reimplemented in pycross.wordsrc.MultiWordsource.

◆ fetch()

def pycross.wordsrc.Wordsource.fetch (   self,
  word = None,
  blank = ' ',
  pos = None,
  filter_func = None,
  shuffle = True,
  truncate = True 
)

Fetches suggestions (as a list of strings) for a given word pattern (mask).

Parameters
wordstr | None the word pattern to find suggestions for, e.g. 'f th '
blankstr placeholder character for unknown (blank) letters (default = whitespace)
posstr | iterable | None part(s) of speech to include in the results:
  • str a single part of speech, e.g. 'N' (nouns) or 'V' (verbs)
  • iterable a collection of parts of speech, e.g. ('N', 'V') = nouns AND verbs
  • None (default) no part-of-speech filter: all words matching the pattern will be included
filter_funccallable filtering callback function to exclude words from results. Prototype is:
filter_func(word: str) -> bool
(returns True to include the word). If None (default), no filtering will be performed.
shufflebool True (default) to shuffle the results randomly
truncatebool True (default) to truncate the results by Wordsource::max_fetch
Returns
list list of strings - the words matching the given pattern

Reimplemented in pycross.wordsrc.MultiWordsource, pycross.wordsrc.TextWordsource, and pycross.wordsrc.DBWordsource.

◆ isvalid()

def pycross.wordsrc.Wordsource.isvalid (   self)

Checks if it is a valid word source.

Returns
bool True if valid, False if not valid

Reimplemented in pycross.wordsrc.MultiWordsource, pycross.wordsrc.TextWordsource, and pycross.wordsrc.DBWordsource.

◆ pop_word()

def pycross.wordsrc.Wordsource.pop_word (   self,
  suggestions 
)

Retrieves the last suggestion (word) from the list of suggestions, removing that word from the original results.

Returns
str last word from the suggestions or None if the suggestions list is empty

Reimplemented in pycross.wordsrc.MultiWordsource.

◆ shuffle()

def pycross.wordsrc.Wordsource.shuffle (   self,
  suggestions 
)

Shuffles the results randomly.

Parameters
suggestionslist list of suggested words str
Returns
list randomly shuffled list of suggested words or the original list if Wordsource::shuffle_words == False

◆ truncate()

def pycross.wordsrc.Wordsource.truncate (   self,
  suggestions 
)

Truncates the results by the threshold number stored in Wordsource::max_fetch.

Parameters
suggestionslist list of suggested words str
Returns
list truncated list of suggested words or the original list if Wordsource::max_fetch is None

Reimplemented in pycross.wordsrc.MultiWordsource.

Member Data Documentation

◆ active

pycross.wordsrc.Wordsource.active

bool if True, this word source will be used; otherwise it will be ignored

◆ max_fetch

pycross.wordsrc.Wordsource.max_fetch

int maximum number of suggestions returned from the word source

◆ shuffle_words

pycross.wordsrc.Wordsource.shuffle_words

bool if True, fetched words will be shuffled


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