contextmanagers

digraph InheritanceGraph { graph [bgcolor=transparent, color=lightsteelblue2, fontname=Arial, fontsize=10, outputorder=edgesfirst, overlap=prism, penwidth=2, rankdir=LR, splines=spline, style="dashed, rounded", truecolor=true ]; node [colorscheme=pastel19, fontname=Arial, fontsize=10, height=0, penwidth=2, shape=box, style="filled, rounded", width=0 ]; edge [color=lightslategrey, penwidth=1 ]; subgraph "cluster_abjad.contextmanagers" { graph [label="abjad.contextmanagers"]; node [color=1]; "abjad.contextmanagers.ContextManager" [URL="../api/abjad/contextmanagers.html#abjad.contextmanagers.ContextManager", color=black, fontcolor=white, label="Context\nManager", target=_top]; "abjad.contextmanagers.FilesystemState" [URL="../api/abjad/contextmanagers.html#abjad.contextmanagers.FilesystemState", color=black, fontcolor=white, label="Filesystem\nState", target=_top]; "abjad.contextmanagers.ContextManager" -> "abjad.contextmanagers.FilesystemState" [minlen=1]; "abjad.contextmanagers.ForbidUpdate" [URL="../api/abjad/contextmanagers.html#abjad.contextmanagers.ForbidUpdate", color=black, fontcolor=white, label="Forbid\nUpdate", target=_top]; "abjad.contextmanagers.ContextManager" -> "abjad.contextmanagers.ForbidUpdate" [minlen=2]; "abjad.contextmanagers.NullContextManager" [URL="../api/abjad/contextmanagers.html#abjad.contextmanagers.NullContextManager", color=black, fontcolor=white, label="Null\nContext\nManager", target=_top]; "abjad.contextmanagers.ContextManager" -> "abjad.contextmanagers.NullContextManager" [minlen=1]; "abjad.contextmanagers.ProgressIndicator" [URL="../api/abjad/contextmanagers.html#abjad.contextmanagers.ProgressIndicator", color=black, fontcolor=white, label="Progress\nIndicator", target=_top]; "abjad.contextmanagers.ContextManager" -> "abjad.contextmanagers.ProgressIndicator" [minlen=2]; "abjad.contextmanagers.RedirectedStreams" [URL="../api/abjad/contextmanagers.html#abjad.contextmanagers.RedirectedStreams", color=black, fontcolor=white, label="Redirected\nStreams", target=_top]; "abjad.contextmanagers.ContextManager" -> "abjad.contextmanagers.RedirectedStreams" [minlen=1]; "abjad.contextmanagers.TemporaryDirectory" [URL="../api/abjad/contextmanagers.html#abjad.contextmanagers.TemporaryDirectory", color=black, fontcolor=white, label="Temporary\nDirectory", target=_top]; "abjad.contextmanagers.ContextManager" -> "abjad.contextmanagers.TemporaryDirectory" [minlen=2]; "abjad.contextmanagers.TemporaryDirectoryChange" [URL="../api/abjad/contextmanagers.html#abjad.contextmanagers.TemporaryDirectoryChange", color=black, fontcolor=white, label="Temporary\nDirectory\nChange", target=_top]; "abjad.contextmanagers.ContextManager" -> "abjad.contextmanagers.TemporaryDirectoryChange" [minlen=1]; "abjad.contextmanagers.Timer" [URL="../api/abjad/contextmanagers.html#abjad.contextmanagers.Timer", color=black, fontcolor=white, label=Timer, target=_top]; "abjad.contextmanagers.ContextManager" -> "abjad.contextmanagers.Timer" [minlen=2]; } subgraph cluster_builtins { graph [label=builtins]; node [color=2]; "builtins.object" [URL="https://docs.python.org/3.10/library/functions.html#object", label=object, target=_top]; } "builtins.object" -> "abjad.contextmanagers.ContextManager" [minlen=1]; }


Classes

ContextManager

An abstract context manager class.

class abjad.contextmanagers.ContextManager[source]

An abstract context manager class.


Attributes Summary

__repr__

Gets repr.


Special methods

overridden __repr__()[source]

Gets repr.


Context managers

FilesystemState

Filesystem state context manager.

ForbidUpdate

A context manager for forbidding score updates.

NullContextManager

A context manager that does nothing.

ProgressIndicator

A context manager for printing progress indications.

RedirectedStreams

A context manager for capturing stdout and stderr output.

TemporaryDirectory

A temporary directory context manager.

TemporaryDirectoryChange

A context manager for temporarily changing the current working directory.

Timer

A timing context manager.

class abjad.contextmanagers.FilesystemState(keep=None, remove=None)[source]

Filesystem state context manager.


Attributes Summary

__enter__

Backs up filesystem assets.

__exit__

Restores filesytem assets and removes backups; also removes paths in remove list.

keep

Gets asset paths to restore on exit.

remove

Gets paths to remove on exit.


Special methods

__enter__()[source]

Backs up filesystem assets.

Returns none.

__exit__(exg_type, exc_value, trackeback)[source]

Restores filesytem assets and removes backups; also removes paths in remove list.

Returns none.

(ContextManager).__repr__()

Gets repr.


Read-only properties

keep

Gets asset paths to restore on exit.

Returns tuple.

remove

Gets paths to remove on exit.

Returns tuple.

class abjad.contextmanagers.ForbidUpdate(component=None, update_on_enter=True, update_on_exit=None)[source]

A context manager for forbidding score updates.

>>> staff = abjad.Staff("c'8 d'8 ~ d'2 e'4")
>>> with abjad.ForbidUpdate(component=staff):
...     for note in staff[:]:
...         pitch_1 = note.written_pitch
...         pitch_2 = pitch_1 + abjad.NamedInterval("M3")
...         pitches = [pitch_1, pitch_2]
...         chord = abjad.Chord(pitches, note.written_duration)
...         abjad.mutate.replace(note, chord)
... 
>>> abjad.wf.wellformed(staff)
True
>>> abjad.show(staff)  

Attributes Summary

__enter__

Enters context manager.

__exit__

Exits context manager.

component

Gets component.

update_on_enter

Is true when context manager should update offsets on enter.

update_on_exit

Is true when context manager should update offsets on exit.


Special methods

__enter__()[source]

Enters context manager.

REGRESSION. Indicators need to be updated after swap; context manager updates indicators before forbidding further updates:

>>> staff = abjad.Staff(r"\times 1/1 { c'4 d' }")
>>> abjad.attach(abjad.Clef("alto"), staff[0][0])
>>> container = abjad.Container()
>>> abjad.mutate.swap(staff[0], container)
>>> with abjad.ForbidUpdate(staff):
...     for note in staff[0]:
...         print(note)
...         print(abjad.get.effective(note, abjad.Clef))
... 
Note("c'4")
Clef(name='alto', hide=False)
Note("d'4")
Clef(name='alto', hide=False)

Returns context manager.

__exit__(exc_type, exc_value, traceback)[source]

Exits context manager.

Returns none.

(ContextManager).__repr__()

Gets repr.


Read-only properties

component

Gets component.

Set to component or none.

Returns component or none.

update_on_enter

Is true when context manager should update offsets on enter.

Set to true, false or none.

Returns true, false or none.

update_on_exit

Is true when context manager should update offsets on exit.

Set to true, false or none.

Returns true, false or none.

class abjad.contextmanagers.NullContextManager[source]

A context manager that does nothing.


Attributes Summary

__enter__

Enters context manager and does nothing.

__exit__

Exits context manager and does nothing.


Special methods

__enter__()[source]

Enters context manager and does nothing.

__exit__(exc_type, exc_value, traceback)[source]

Exits context manager and does nothing.

(ContextManager).__repr__()

Gets repr.

class abjad.contextmanagers.ProgressIndicator(message='', total=None, verbose=True, is_warning=None)[source]

A context manager for printing progress indications.


Attributes Summary

END

RED

__enter__

Enters progress indicator.

__exit__

Exits progress indicator.

__repr__

Gets interpreter representation of context manager.

advance

Advances the progress indicator's progress count.

is_warning

Is true if progress indicator prints in red when its progress goes above zero.

message

Gets message of progress indicator.

progress

Gets progress.

total

Gets total count.

verbose

Is true if progress indicator prints status.


Special methods

__enter__()[source]

Enters progress indicator.

__exit__(exc_type, exc_value, traceback)[source]

Exits progress indicator.

overridden __repr__()[source]

Gets interpreter representation of context manager.

>>> context_manager = abjad.ProgressIndicator()
>>> context_manager
<ProgressIndicator()>

Returns string.


Methods

advance()[source]

Advances the progress indicator’s progress count. Overwrites the current terminal line with the progress indicators message and new count.


Read-only properties

is_warning

Is true if progress indicator prints in red when its progress goes above zero.

Returns true or false.

message

Gets message of progress indicator.

Returns string.

progress

Gets progress.

Returns integer.

total

Gets total count.

Returns integer or none.

verbose

Is true if progress indicator prints status.

Returns true or false.

class abjad.contextmanagers.RedirectedStreams(stdout=None, stderr=None)[source]

A context manager for capturing stdout and stderr output.

>>> abjad.RedirectedStreams()
<RedirectedStreams()>
>>> from io import StringIO
>>> string_io = StringIO()
>>> with abjad.RedirectedStreams(stdout=string_io):
...     print("hello, world!")
... 
>>> result = string_io.getvalue()
>>> string_io.close()
>>> print(result)
hello, world!


Attributes Summary

__enter__

Enters redirected streams context manager.

__exit__

Exits redirected streams context manager.

__repr__

Gets interpreter representation of context manager.

stderr

Gets stderr of context manager.

stdout

Gets stdout of context manager.


Special methods

__enter__()[source]

Enters redirected streams context manager.

Returns none.

__exit__(exc_type, exc_value, traceback)[source]

Exits redirected streams context manager.

Returns none.

overridden __repr__()[source]

Gets interpreter representation of context manager.

>>> context_manager = abjad.RedirectedStreams()
>>> context_manager
<RedirectedStreams()>

Returns string.


Read-only properties

stderr

Gets stderr of context manager.

stdout

Gets stdout of context manager.

class abjad.contextmanagers.TemporaryDirectory(parent_directory=None)[source]

A temporary directory context manager.


Attributes Summary

__enter__

Enters context manager.

__exit__

Exits context manager.

parent_directory

Gets parent directory.

temporary_directory

Gets temporary directory.


Special methods

__enter__()[source]

Enters context manager.

Creates and returns path to a temporary directory.

__exit__(exc_type, exc_value, traceback)[source]

Exits context manager.

Deletes previously created temporary directory.

(ContextManager).__repr__()

Gets repr.


Read-only properties

parent_directory

Gets parent directory.

Returns string.

temporary_directory

Gets temporary directory.

Returns string.

class abjad.contextmanagers.TemporaryDirectoryChange(directory=None, verbose=None)[source]

A context manager for temporarily changing the current working directory.


Attributes Summary

__enter__

Enters context manager and changes to directory.

__exit__

Exits context manager and returns to original working directory.

__repr__

Gets interpreter representation of context manager.

directory

Gets temporary directory of context manager.

original_directory

Gets original directory of context manager.

verbose

Is true if context manager prints verbose messages on entrance and exit.


Special methods

__enter__()[source]

Enters context manager and changes to directory.

__exit__(exc_type, exc_value, traceback)[source]

Exits context manager and returns to original working directory.

overridden __repr__()[source]

Gets interpreter representation of context manager.

Returns string.


Read-only properties

directory

Gets temporary directory of context manager.

Returns string.

original_directory

Gets original directory of context manager.

Returns string.

verbose

Is true if context manager prints verbose messages on entrance and exit.

Returns true or false.

class abjad.contextmanagers.Timer(exit_message=None, enter_message=None, print_continuously_from_background=False, verbose=True)[source]

A timing context manager.

>>> timer = abjad.Timer()
>>> with timer:
...     for _ in range(1000000):
...         x = 1 + 1
... 
>>> timer.elapsed_time  
0.027534961700439453

The timer can also be accessed from within the with block:

>>> with abjad.Timer() as timer:  
...     for _ in range(5):
...         for _ in range(1000000):
...             x = 1 + 1
...         print(timer.elapsed_time)
... 
0.027554988861083984
0.05520200729370117
0.08285117149353027
0.11129188537597656
0.13967108726501465

Timers can be reused between with blocks. They will reset their clock on entering any with block.


Attributes Summary

__enter__

Enters context manager.

__exit__

Exit context manager.

elapsed_time

Elapsed time.

enter_message

Timer enter message.

exit_message

Timer exit message.

print_continuously_from_background

Is true when timer should print continuously from background.

start_time

Start time of timer.

stop_time

Stop time of timer.

total_time_message

Gets total time message.

verbose

Is true if timer should print messages.


Special methods

__enter__()[source]

Enters context manager.

Returns context manager.

__exit__(exc_type, exc_value, traceback)[source]

Exit context manager.

Returns none.

(ContextManager).__repr__()

Gets repr.


Read-only properties

elapsed_time

Elapsed time.

Return float or none.

enter_message

Timer enter message.

Returns string.

exit_message

Timer exit message.

Returns string.

print_continuously_from_background

Is true when timer should print continuously from background.

Returns true or false.

start_time

Start time of timer.

Returns time.

stop_time

Stop time of timer.

Returns time.

total_time_message

Gets total time message.

Truncated to the nearest second.

Returns string.

verbose

Is true if timer should print messages.

Returns true or false.