diffpy.srfit.util package
Utilities and constants used throughout SrFit.
- diffpy.srfit.util.sortKeyForNumericString(s)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.util.sort_key_for_numeric_string instead.
- diffpy.srfit.util.sort_key_for_numeric_string(string)[source]
Return a sort key that orders the numbers in a string by value.
The string is split into text and integer segments, so
"a2"sorts before"a10". Signs, decimal points and exponents are ignored. Use it as thekeyargument ofsortedorlist.sort.
Submodules
diffpy.srfit.util.inpututils module
Input utilities.
- diffpy.srfit.util.inpututils.convert_input_to_string(input)[source]
Return the contents of an open file, a file name, or a string.
This lets a method accept its input in any of those forms. A string that is an existing file, or that is a single line shorter than 80 characters, is treated as a file name. Any other string is returned unchanged.
- Parameters:
input (file-like or str) – The open file-like object, the name of a file, or the text itself.
- Returns:
The contents of the input as a string.
- Return type:
- Raises:
FileNotFoundError – If
inputlooks like a file name but the file does not exist.
- diffpy.srfit.util.inpututils.inputToString(input)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.util.inpututils.convert_input_to_string instead.
diffpy.srfit.util.observable module
- class diffpy.srfit.util.observable.Observable(**kwds)[source]
Bases:
objectProvide notification support for classes that maintain dynamic associations with multiple clients.
Observers, i.e. clients of the observable, register event handlers that will be invoked to notify them whenever something interesting happens to the observable. The nature of what is being observed is defined by Observable descendants and their managers. For example, instances of pyre.calc.Node are observable by other nodes whose value depends on them so that the dependents can be notified about value changes and forced to recompute their own value.
The event handlers are callables that take a tuple starting with the observable instance as their single argument. Register them with
add_observer, remove them withremove_observer, and invoke them all withnotify.- addObserver(callable)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.util.observable.Observable.add_observer instead.
- add_observer(callable)[source]
Add a callable to the set of observers.
Bound methods are held by weak reference, and are removed automatically once their object is deleted.
- Parameters:
callable (callable) – The observer to call when this observable notifies.
- hasObserver(callable)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.util.observable.Observable.has_observer instead.
- has_observer(callable)[source]
Check whether a callable is in the set of observers.
- Parameters:
callable (callable) – The observer to look for.
- Returns:
The flag that is True if
callableis an observer.- Return type:
- notify(other=())[source]
Notify all observers.
- Parameters:
other (tuple, optional) – The extra objects passed to each observer after this observable. Default is an empty tuple.
- removeObserver(callable)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.util.observable.Observable.remove_observer instead.
diffpy.srfit.util.tagmanager module
TagManager class.
The TagManager class takes hashable objects and assigns tags to them. Objects can then be easily referenced via their assigned tags.
- class diffpy.srfit.util.tagmanager.TagManager[source]
Bases:
objectTagManager class.
Manage tags on hashable objects. Tags are strings that carry metadata.
- silent
The flag that is True (default) to treat an unknown tag as having no objects. If False, an unknown tag raises a KeyError.
- Type:
- alltags()[source]
Return all tags managed by the TagManager.
- Returns:
The tags that have been applied to any object.
- Return type:
dict_keys
- hasTags(obj, *tags)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.util.tagmanager.TagManager.has_tags instead.
- tag(obj, *tags)[source]
Tag an object.
Tags are stored as strings.
- Parameters:
obj (hashable) – The object to tag.
*tags – The tags to apply to
obj.
- Raises:
TypeError – If
objis not hashable.
- untag(obj, *tags)[source]
Remove tags from an object.
- Parameters:
obj (hashable) – The object to untag.
*tags – The tags to remove from
obj. If none are given, all tags are removed fromobj.
- Raises:
KeyError – If a given tag does not apply to
objandsilentis False.
- verifyTags(*tags)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.util.tagmanager.TagManager.verify_tags instead.
diffpy.srfit.util.argbinders module
Functions for binding arguments of callable objects.
- class diffpy.srfit.util.argbinders.bind2nd(func, arg1)[source]
Bases:
objectFreeze the second argument of a callable to a constant.
Calling the instance as
bound(a, *args, **kwargs)callsfunc(a, arg1, *args, **kwargs).- Parameters:
func (callable) – The callable whose second argument is frozen.
arg1 – The value passed as the second argument of
func.
diffpy.srfit.util.nameutils module
Name utilities.
- diffpy.srfit.util.nameutils.isIdentifier(s)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.util.nameutils.is_identifier instead.
- diffpy.srfit.util.nameutils.is_identifier(name)[source]
Check whether a string is a valid name.
A valid name starts with a letter or underscore and contains only letters, digits and underscores.
- diffpy.srfit.util.nameutils.validateName(name)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.util.nameutils.validate_name instead.
- diffpy.srfit.util.nameutils.validate_name(name)[source]
Raise an error if a string is not a valid name.
- Parameters:
name (str) – The string to check.
- Raises:
ValueError – If
nameis not a valid name, as defined byis_identifier.
diffpy.srfit.util.weakrefcallable module
Picklable storage of callable objects using weak references.
- class diffpy.srfit.util.weakrefcallable.WeakBoundMethod(f, fallback=None)[source]
Bases:
objectCallable wrapper to a bound method stored as a weak reference.
This stores a bound method without keeping its object alive, and can call a fallback function once that object has been deleted.
- function
The unbound function extracted from the wrapped bound method.
- Type:
FunctionType
- fallback
The plain function called when the object holding the bound method has been deleted. It is called with this wrapper as the first argument, followed by any positional and keyword arguments passed for the bound method, and can be used to deregister this wrapper.
- Type:
FunctionType or None
- _wref
The weak reference to the object the wrapped method is bound to.
- Type:
- fallback
- function
- diffpy.srfit.util.weakrefcallable.weak_ref(f, fallback=None)[source]
Create weak-reference wrapper to a bound method.
- Parameters:
f (callable) – The object-bound method or plain function to wrap.
fallback (FunctionType, optional) – The plain function called when the object holding
fhas been deleted. It is called with the wrapper as the first argument, followed by the positional and keyword arguments passed for the bound method, and can be used to deregister the wrapper. Default is None.
- Returns:
The weak wrapper when
fis a bound method. Whenfis a plain function or already a WeakBoundMethod,fitself, andfallbackis ignored.- Return type:
WeakBoundMethod or callable