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
- __enter__() FilesystemState [source]
Backs up filesystem assets.
- __exit__(exg_type, exc_value, trackeback) None [source]
Restores filesytem assets and removes backups; also removes paths in remove list.
-
(
ContextManager
).__repr__() str 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__() ForbidUpdate [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)
-
(
ContextManager
).__repr__() str 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.
- update_on_exit
Is true when context manager should update offsets on exit.
Set to 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__() str 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
- __enter__() ProgressIndicator [source]
Enters progress indicator.
- overridden __repr__() str [source]
Gets interpreter representation of context manager.
>>> context_manager = abjad.ProgressIndicator() >>> context_manager <ProgressIndicator()>
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.
- message
Gets message of progress indicator.
- progress
Gets progress.
- total
Gets total count.
- verbose
Is true if progress indicator prints status.
- 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
- __enter__() RedirectedStreams [source]
Enters redirected streams context manager.
- overridden __repr__() str [source]
Gets interpreter representation of context manager.
>>> context_manager = abjad.RedirectedStreams() >>> context_manager <RedirectedStreams()>
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) None [source]
Exits context manager.
Deletes previously created temporary directory.
-
(
ContextManager
).__repr__() str Gets repr.
Read-only properties
- parent_directory
Gets parent directory.
- temporary_directory
Gets temporary directory.
- 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
- __enter__() TemporaryDirectoryChange [source]
Enters context manager and changes to
directory
.
- __exit__(exc_type, exc_value, traceback) None [source]
Exits context manager and returns to original working directory.
Read-only properties
- 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.
- 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.02991938591003418
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.029968976974487305 0.05989813804626465 0.09048700332641602 0.12091708183288574 0.15134000778198242
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__() str Gets repr.
Read-only properties
- 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.
Returns time.
- stop_time
Stop time of timer.
Returns time.
- total_time_message
Gets total time message.
Truncated to the nearest second.
- verbose
Is true if timer should print messages.
-
(