traits.util
Package¶
Utility functions, part of the Traits project.
traits.util.async_trait_wait
Module¶
- traits.util.async_trait_wait.wait_for_condition(condition, obj, trait, timeout=None)[source]¶
Wait until the given condition is true, re-evaluating on trait change.
This is intended for use in multithreading situations where traits can be modified from a different thread than the calling thread.
Wait until condition is satisfied. Raise a RuntimeError if condition is not satisfied within the given timeout.
condition is a callback function that will be called with obj as its single argument. It should return a boolean indicating whether the condition is satisfied or not.
timeout gives the maximum time in seconds to wait for the condition to become true. The default value of None indicates no timeout.
(obj, trait) give an object and trait to listen to for indication of a possible change: whenever the trait changes, the condition is re-evaluated. The condition will also be evaluated on entering this function.
Note that in cases of unusual timing it’s possible for the condition to be evaluated one more time after the
wait_for_condition
call has returned.
traits.util.camel_case
Module¶
Defines utility functions for operating on camel case names.
traits.util.clean_strings
Module¶
Provides functions that mange strings to avoid characters that would be problematic in certain situations.
traits.util.deprecated
Module¶
A decorator for marking methods/functions as deprecated.
traits.util.home_directory
Module¶
traits.util.resource
Module¶
Utility functions for managing and finding resources (e.g. images, files).
get_path : Returns the absolute path of a class or instance
- create_unique_nameCreates a name with a given prefix that is not in a
given list of existing names. The separator between the prefix and the rest of the name can also be specified (default is a ‘_’)
traits.util.import_symbol
Module¶
A function to import symbols.
- traits.util.import_symbol.import_symbol(symbol_path)[source]¶
Import the symbol defined by the specified symbol path.
Examples
import_symbol(‘tarfile:TarFile’) -> TarFile import_symbol(‘tarfile:TarFile.open’) -> TarFile.open
To allow compatibility with old-school traits symbol names we also allow all-dotted paths, but in this case you can only import top-level names from the module.
import_symbol(‘tarfile.TarFile’) -> TarFile
traits.util.toposort
Module¶
A simple topological sort on a dictionary graph.
traits.util.trait_documenter
Module¶
A Trait Documenter (Subclassed from the autodoc ClassLevelDocumenter)
- class traits.util.trait_documenter.TraitDocumenter(directive: DocumenterBridge, name: str, indent: str = '')[source]¶
Bases:
ClassLevelDocumenter
Specialized Documenter subclass for trait attributes.
The class defines a new documenter that recovers the trait definition signature of module level and class level traits.
To use the documenter, append the module path in the extension attribute of the conf.py.
- objtype = 'traitattribute'¶
name by which the directive is called (auto…) and the default generated directive name
- directivetype = 'attribute'¶
- member_order = 60¶
order if autodoc_member_order is set to ‘groupwise’
- priority = 12¶
priority if multiple documenters return True from can_document_member
- classmethod can_document_member(member, membername, isattr, parent)[source]¶
Check that the documented member is a trait instance.
- traits.util.trait_documenter.trait_definition(*, cls, trait_name)[source]¶
Retrieve the portion of the source defining a Trait attribute.
For example, given a class:
class MyModel(HasStrictTraits) foo = List(Int, [1, 2, 3])
trait_definition(cls=MyModel, trait_name="foo")
returns"List(Int, [1, 2, 3])"
.- Parameters:
cls (MetaHasTraits) – Class being documented.
trait_name (str) – Name of the trait being documented.
- Returns:
The portion of the source containing the trait definition. For example, for a class trait defined as
"my_trait = Float(3.5)"
, the returned string will contain"Float(3.5)"
.- Return type:
- Raises:
ValueError – If trait_name doesn’t appear as a class-level variable in the source.
traits.util.event_tracer
Module¶
Record trait change events in single and multi-threaded environments.
- class traits.util.event_tracer.SentinelRecord[source]¶
Sentinel record to separate groups of chained change event dispatches.
- class traits.util.event_tracer.ChangeMessageRecord(time, indent, name, old, new, class_name)[source]¶
Message record for a change event dispatch.
- time¶
Time stamp in UTC.
- indent¶
Depth level in a chain of trait change dispatches.
- name¶
The name of the trait that changed
- old¶
The old value.
- new¶
The new value.
- class_name¶
The name of the class that the trait change took place.
- class traits.util.event_tracer.CallingMessageRecord(time, indent, handler, source)[source]¶
Message record for a change handler call.
- time¶
Time stamp in UTC.
- indent¶
Depth level of the call in a chain of trait change dispatches.
- handler¶
The traits change handler that is called.
- source¶
The source file where the handler was defined.
- class traits.util.event_tracer.ExitMessageRecord(time, indent, handler, exception)[source]¶
Message record for returning from a change event dispatch.
- time¶
Time stamp in UTC.
- indent¶
Depth level of the exit in a chain of trait change dispatch.
- handler¶
The traits change handler that is called.
- exception¶
The exception type (if one took place)
- class traits.util.event_tracer.RecordContainer[source]¶
A simple record container.
This class is commonly used to hold records from a single thread.
- class traits.util.event_tracer.MultiThreadRecordContainer[source]¶
A container of record containers that are used by separate threads.
Each record container is mapped to a thread name id. When a RecordContainer does not exist for a specific thread a new empty RecordContainer will be created on request.
- class traits.util.event_tracer.ChangeEventRecorder(container)[source]¶
A single thread trait change event recorder.
- Parameters:
container (MultiThreadRecordContainer) – A container to store the records for each trait change.
- container¶
A container to store the records for each trait change.
- class traits.util.event_tracer.MultiThreadChangeEventRecorder(container)[source]¶
A thread aware trait change recorder.
The class manages multiple ChangeEventRecorders which record trait change events for each thread in a separate file.
- Parameters:
container (MultiThreadChangeEventRecorder) – The container of RecordContainers to keep the trait change records for each thread.
- container¶
The container of RecordContainers to keep the trait change records for each thread.
- traits.util.event_tracer.record_events()[source]¶
Multi-threaded trait change event tracer.
Example
This will install a tracer that will record all events that occur from setting of some_trait on the my_model instance:
>>> from trace_recorder import record_events >>> with record_events() as change_event_container: ... my_model.some_trait = True >>> change_event_container.save_to_directory('C:\dev\trace')
The results will be stored in one file per running thread in the directory ‘C:devtrace’. The files are named after the thread being traced.