contextmanagers¶
Classes
An abstract context manager class. |
- class abjad.contextmanagers.ContextManager[source]¶
An abstract context manager class.
Attributes Summary
Gets repr.
Special methods
Context managers
Filesystem state context manager. |
|
A context manager for forbidding score updates. |
|
A context manager that does nothing. |
|
A context manager for printing progress indications. |
|
A context manager for capturing stdout and stderr output. |
|
A temporary directory context manager. |
|
A context manager for temporarily changing the current working directory. |
|
A timing context manager. |
- class abjad.contextmanagers.FilesystemState(keep=None, remove=None)[source]¶
Filesystem state context manager.
Attributes Summary
Backs up filesystem assets.
Restores filesytem assets and removes backups; also removes paths in remove list.
Gets asset paths to restore on exit.
Gets paths to remove on exit.
Special methods
- __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
Enters context manager.
Exits context manager.
Gets component.
Is true when context manager should update offsets on enter.
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.
-
(
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
Enters context manager and does nothing.
Exits context manager and does nothing.
Special methods
-
(
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
Enters progress indicator.
Exits progress indicator.
Gets interpreter representation of context manager.
Advances the progress indicator's progress count.
Is true if progress indicator prints in red when its progress goes above zero.
Gets message of progress indicator.
Gets progress.
Gets total count.
Is true if progress indicator prints status.
Special methods
- 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
Enters redirected streams context manager.
Exits redirected streams context manager.
Gets interpreter representation of context manager.
Gets stderr of context manager.
Gets stdout of context manager.
Special methods
- __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
Enters context manager.
Exits context manager.
Gets parent directory.
Gets temporary directory.
Special methods
- __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
Enters context manager and changes to
directory
.Exits context manager and returns to original working directory.
Gets interpreter representation of context manager.
Gets temporary directory of context manager.
Gets original directory of context manager.
Is true if context manager prints verbose messages on entrance and exit.
Special methods
- __exit__(exc_type, exc_value, traceback)[source]¶
Exits context manager and returns to original working directory.
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 anywith
block.Attributes Summary
Enters context manager.
Exit context manager.
Elapsed time.
Timer enter message.
Timer exit message.
Is true when timer should print continuously from background.
Start time of timer.
Stop time of timer.
Gets total time message.
Is true if timer should print messages.
Special methods
-
(
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.
-
(