diffpy.srfit.util package

Utilities and constants used throughout SrFit.

diffpy.srfit.util.sortKeyForNumericString(s)[source]

Compute key for sorting strings according to their integer numeric value.

Each string gets split to string and integer segments to create keys for comparison. Signs, decimal points and exponents are ignored. This function is intended as the key argument for the sorted or list.sort function.

Parameters:

s (str) – String which may have numeric components, e.g., “a12b”.

Returns:

Tuple of non-numeric segments intermixed with integer values.

Return type:

tuple

Submodules

diffpy.srfit.util.inpututils module

Input utilities.

diffpy.srfit.util.inpututils.inputToString(input)[source]

Convert input from various modes to a string.

This is useful when you want a method to accept a string, open file object or file name.

Parameters:

input – An open file-like object, name of a file or a string containing the input.

Returns the input in a string Raises IOError if the input is supected to be a file name, but the file cannot be found.

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 the observable instance as their single argument.

interface:

addObserver: registers its callable argument with the list of handlers to invoke removeObserver: remove an event handler from the list of handlers to invoke notify: invoke the registered handlers in the order in which they were registered

addObserver(callable)[source]

Add callable to the set of observers.

hasObserver(callable)[source]

True if callable is present in the set of observers.

notify(other=())[source]

Notify all observers.

removeObserver(callable)[source]

Remove callable from the set of observers.

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

Flag indicating whether to silently pass by when a tag cannot be found (bool, True). If this is False, then a KeyError will be thrown when a tag cannot be found.

_tagdict

A dictionary of tags to sets of tagged objects.

alltags()[source]

Get all tags managed by the TagManager.

hasTags(obj, *tags)[source]

Determine if an object has all passed tags.

Returns bool

intersection(*tags)[source]

Get all objects that have all of the passed tags.

Returns set

tag(obj, *tags)[source]

Tag an object.

Tags are stored as strings.

Parameters:
  • obj – Any hashable object to be untagged.

  • *tags – Tags to apply to obj.

Raises TypeError if obj is not hashable.

tags(obj)[source]

Get all tags on an object.

Returns list

union(*tags)[source]

Get all objects that have any of the passed tags.

Returns set

untag(obj, *tags)[source]

Remove tags from an object.

Parameters:
  • obj – Any hashable object to be untagged.

  • *tags – Tags to remove from obj. If this is empty, then all tags will be removed from obj.

Raises KeyError if a passed tag does not apply to obj and self.silent is False

verifyTags(*tags)[source]

Check that tags are all extant.

Raises KeyError if a passed tag does not exist. This ignores self.silent.

diffpy.srfit.util.argbinders module

Functions for binding arguments of callable objects.

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

Bases: object

Freeze second argument of a callable object to a given constant.

diffpy.srfit.util.nameutils module

Name utilities.

diffpy.srfit.util.nameutils.isIdentifier(s)[source]

Check to see if a python string is a valid identifier.

From http://code.activestate.com/recipes/413487/

diffpy.srfit.util.nameutils.validateName(name)[source]

Validate that a name is a valid identifier.

Raises ValueError if the name is invalid.

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.

Support storage of bound methods without keeping the associated objects alive forever. Provide facility for a fallback function to be used when method-related object is deleted.

function

the unbound function extracted from the wrapped bound method.

Type:

MethodType

fallback

plain function to be called when object holding the bound method gets deallocated. The fallback function is called with this object as the first argument followed by any positional and keyword arguments passed for the bound method. The fallback function can be used to deregister this wrapper.

Type:

FunctionType, optional

_wref

weak reference to the object the wrapped method is bound to.

Type:

weakref

_class

the type of the object to which the method is bound. 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) – object-bound method or a plain function.

  • fallback (FunctionType, optional) – plain function to be called when object holding f gets deallocated. The fallback function is called with the wrapper object as the first argument followed by positional and keyword arguments passed for the bound method. The fallback function can be used to deregister the wrapper.

Returns:

when f is a bound method. If f is a plain function or already of WeakBoundMethod type, return f and ignore the fallback argument.

Return type:

WeakBoundMethod