wn.util¶
Wn utility classes.
- wn.util.synset_id_formatter(fmt='{prefix}-{offset:08}-{pos}', **kwargs)¶
Return a function for formatting synset ids.
The fmt argument can be customized. It will be formatted using any other keyword arguments given to this function and any given to the resulting function. By default, the format string expects a
prefix
string argument for the namespace (such as a lexicon id), anoffset
integer argument (such as a WNDB offset), and apos
string argument.- Parameters
fmt (str) – A Python format string
**kwargs – Keyword arguments for the format string.
- Return type
Example
>>> pwn_synset_id = synset_id_formatter(prefix='pwn') >>> pwn_synset_id(offset=1174, pos='n') 'pwn-00001174-n'
- class wn.util.ProgressHandler(*, message='', count=0, total=0, refresh_interval=0, unit='', status='', file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>)¶
An interface for updating progress in long-running processes.
Long-running processes in Wn, such as
wn.download()
andwn.add()
, call to a progress handler object as they go. The default progress handler used by Wn isProgressBar
, which updates progress by formatting and printing a textual bar to stderr. TheProgressHandler
class may be used directly, which does nothing, or users may create their own subclasses for, e.g., updating a GUI or some other handler.The initialization parameters, except for
file
, are stored in akwargs
member and may be updated after the handler is created through theset()
method. Theupdate()
method is the primary way a counter is updated. Theflash()
method is sometimes called for simple messages. When the process is complete, theclose()
method is called, optionally with a message.- Parameters
- kwargs¶
A dictionary storing the updateable parameters for the progress handler. The keys are:
- close()¶
Close the progress handler.
This might be useful for closing file handles or cleaning up resources.
- Return type
None
- flash(message)¶
Issue a message unrelated to the current counter.
This may be useful for multi-stage processes to indicate the move to a new stage, or to log unexpected situations.
- Parameters
message (str) –
- Return type
None
- set(**kwargs)¶
Update progress handler parameters.
Calling this method also runs
update()
with an increment of 0, which causes a refresh of any indicator without changing the counter.- Return type
None
- update(n=1, force=False)¶
Update the counter with the increment value n.
This method should update the
count
key ofkwargs
with the increment value n. After this, it is expected to update some user-facing progress indicator.If force is
True
, any indicator will be refreshed regardless of the value of the refresh interval.
- class wn.util.ProgressBar(*, message='', count=0, total=0, refresh_interval=0, unit='', status='', file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>)¶
A
ProgressHandler
subclass for printing a progress bar.Example
>>> p = ProgressBar(message='Progress: ', total=10, unit=' units') >>> p.update(3) Progress: [######### ] (3/10 units)
See
format()
for a description of how the progress bar is formatted.- Parameters
- FMT = '\r{message}{bar}{counter}{status}'¶
The default formatting template.
- close()¶
Print a newline so the last printed bar remains on screen.
- Return type
None
- flash(message)¶
Overwrite the progress bar with message.
- Parameters
message (str) –
- Return type
None
- format()¶
Format and return the progress bar.
The bar is is formatted according to
FMT
, using variables fromkwargs
and two computed variables:bar
: visualization of the progress bar, empty whentotal
is 0counter
: display ofcount
,total
, andunits
>>> p = ProgressBar(message='Progress', count=2, total=10, unit='K') >>> p.format() '\rProgress [###### ] (2/10K) ' >>> p = ProgressBar(count=2, status='Counting...') >>> p.format() '\r (2) Counting...'
- Return type