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 the key argument of sorted or list.sort.

Parameters:

string (str) – The string to build a key for, which may contain numbers, e.g. "a12b".

Returns:

The text segments of string with integer values in between, e.g. ("a", 12, "b").

Return type:

tuple

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:

str

Raises:

FileNotFoundError – If input looks 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: object

Provide 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 with remove_observer, and invoke them all with notify.

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 callable is an observer.

Return type:

bool

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.

remove_observer(callable)[source]

Remove a callable from the set of observers.

Parameters:

callable (callable) – The observer to remove.

Raises:

KeyError – If callable is not an observer of this observable.

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: object

TagManager 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:

bool

_tagdict

The mapping of each tag to the set of objects it is applied to.

Type:

dict

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.

has_tags(obj, *tags)[source]

Check whether an object has all of the given tags.

Parameters:
  • obj (hashable) – The object to check.

  • *tags – The tags to look for.

Returns:

The flag that is True if obj has every tag in tags.

Return type:

bool

Raises:

KeyError – If a tag does not exist and silent is False.

intersection(*tags)[source]

Return all objects that have all of the given tags.

Parameters:

*tags – The tags to look for.

Returns:

The objects that have every tag in tags. Empty if no tags are given.

Return type:

set

Raises:

KeyError – If a tag does not exist and silent is False.

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 obj is not hashable.

tags(obj)[source]

Return all tags on an object.

Parameters:

obj (hashable) – The object to look up.

Returns:

The tags applied to obj.

Return type:

list of str

union(*tags)[source]

Return all objects that have any of the given tags.

Parameters:

*tags – The tags to look for.

Returns:

The objects that have at least one tag in tags. Empty if no tags are given.

Return type:

set

Raises:

KeyError – If a tag does not exist and silent is False.

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 from obj.

Raises:

KeyError – If a given tag does not apply to obj and silent is 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.

verify_tags(*tags)[source]

Check that all of the given tags exist.

This ignores silent.

Parameters:

*tags – The tags to check.

Returns:

True when every tag exists.

Return type:

bool

Raises:

KeyError – If a tag does not exist.

diffpy.srfit.util.argbinders module

Functions for binding arguments of callable objects.

class diffpy.srfit.util.argbinders.bind2nd(func, arg1)[source]

Bases: object

Freeze the second argument of a callable to a constant.

Calling the instance as bound(a, *args, **kwargs) calls func(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.

Parameters:

name (str) – The string to check.

Returns:

The flag that is True if name is a valid name.

Return type:

bool

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 name is not a valid name, as defined by is_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: object

Callable 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:

weakref.ref

_class

The type of the object the method is bound to. This is only used for pickling.

Type:

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 f has 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 f is a bound method. When f is a plain function or already a WeakBoundMethod, f itself, and fallback is ignored.

Return type:

WeakBoundMethod or callable