wolframclient.utils package¶
Submodules¶
wolframclient.utils.api module¶
wolframclient.utils.asyncio module¶
wolframclient.utils.datastructures module¶
- class wolframclient.utils.datastructures.Association[source]¶
Bases:
OrderedDictA
OrderedDictthat serializes to an Association
- class wolframclient.utils.datastructures.Settings[source]¶
Bases:
dictDictionary subclass enabling attribute lookup/assignment of keys/values.
For example:
>>> m = Settings({'foo': 'bar'}) >>> m.foo 'bar' >>> m.foo = 'not bar' >>> m['foo'] 'not bar'
Settingsobjects also provide.first()which acts like.get()but accepts multiple keys as arguments, and returns the value of the first hit, e.g.:>>> m = Settings({'foo': 'bar', 'biz': 'baz'}) >>> m.first('wrong', 'incorrect', 'foo', 'biz') 'bar'
- class wolframclient.utils.datastructures.immutabledict[source]¶
Bases:
dicthashable dict implementation, suitable for use as a key into other dicts.
>>> h1 = immutabledict({"apples": 1, "bananas":2}) >>> h2 = immutabledict({"bananas": 3, "mangoes": 5}) >>> h1+h2 immutabledict(apples=1, bananas=3, mangoes=5) >>> d1 = {} >>> d1[h1] = "salad" >>> d1[h1] 'salad' >>> d1[h2] Traceback (most recent call last): ... KeyError: immutabledict(bananas=3, mangoes=5)
- based on answers from
http://stackoverflow.com/questions/1151658/python-hashable-dicts
- clear() None. Remove all items from D.¶
- pop(k[, d]) v, remove specified key and return the corresponding value.¶
If the key is not found, return the default if given; otherwise, raise a KeyError.
- popitem(*args, **opts)¶
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
- setdefault(*args, **opts)¶
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
- update([E, ]**F) None. Update D from dict/iterable E and F.¶
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
wolframclient.utils.debug module¶
wolframclient.utils.decorators module¶
- class wolframclient.utils.decorators.cached_property(func, name=None)[source]¶
Bases:
objectDecorator that converts a method with a single self argument into a property cached on the instance.
Optional
nameargument allows you to make cached properties of other methods. (e.g. url = cached_property(get_absolute_url, name=’url’) )
- wolframclient.utils.decorators.to_dict(fn)¶
- wolframclient.utils.decorators.to_tuple(fn)¶
wolframclient.utils.dispatch module¶
- class wolframclient.utils.dispatch.Dispatch[source]¶
Bases:
objectA method dispatcher class allowing for multiple implementations of a function. Each implementation is associated to a specific input type.
Implementations are registered with the annotation
dispatch().The Dispatch class is callable, it behaves as a function that uses the implementation corresponding to the input parameter.
When a type is a subtype, the type and its parents are checked in the order given by
__mro__(method resolution order).Example: method
resolve()applied to an instance ofcollections.OrderedDict, check for the first implementation to match withcollections.OrderedDict, then withdict, and ultimately toobject.Once the mapping is determined, it is cached for later use.
- as_method()[source]¶
Return the dispatch as a class method.
Create a new dispatcher:
dispatch = Dispatcher()
Use the dispatcher as a class method:
class MyClass(object): myMethod = dispatch.as_method()
Call the class method:
o = MyClass() o.myMethod(arg, *args, **kwargs)
- dispatch(*args, **opts)[source]¶
Annotate a function and map it to a given set of type(s).
Declare an implementation to use on
bytearrayinput:@dispatcher.dispatch(bytearray) def my_func(...)
The default implementation is associated with
object. Set a default:@dispatcher.dispatch(object) def my_default_func(...)
A tuple can be used as input to associate more than one type with a function. Declare a function used for both
bytesandbytearray:@dispatcher.dispatch((bytes, bytearray)) def my_func(...)
Implementation must be unique. By default, registering the same combination of types will raise an error. Set replace_existing to
Trueto update the current mapping. Or, set keep_existing toTrueto ignore duplicate registration and keep the existing mapping.
- register(function, types=<class 'object'>, keep_existing=False, replace_existing=False)[source]¶
Equivalent to annotation
dispatch()but as a function.
- update(dispatch, **opts)[source]¶
Update current mapping with the one from dispatch.
dispatch can be a Dispatch instance or a
dict. **opts are passed toregister()
wolframclient.utils.encoding module¶
- wolframclient.utils.encoding.concatenate_bytes(iterable_of_bytes, /)¶
Concatenate any number of bytes objects.
The bytes whose method is called is inserted in between each pair.
The result is returned as a new bytes object.
Example: b’.’.join([b’ab’, b’pq’, b’rs’]) -> b’ab.pq.rs’.
wolframclient.utils.environment module¶
wolframclient.utils.externalevaluate module¶
- class wolframclient.utils.externalevaluate.ExternalEvaluateConsumer(routes_registry={}, objects_registry={}, globals_registry={})[source]¶
Bases:
WXFConsumerNumpy- builtin_routes = {'Bool': <function Bool>, 'Call': <function Call>, 'Cast': <function Cast>, 'DelReference': <function DelReference>, 'Eval': <function Eval>, 'FromArrow': <function FromArrow>, 'FromComplex': <function FromComplex>, 'FromGregorianDay': <function FromGregorianDay>, 'FromMissing': <function FromMissing>, 'FromRational': <function FromRational>, 'FromTodayTime': <function FromTodayTime>, 'FromUnixTime': <function FromUnixTime>, 'GetAttribute': <function GetAttribute>, 'GetItem': <function GetItem>, 'GetReference': <function GetReference>, 'Import': <function Import>, 'Len': <function Len>, 'MethodCall': <function MethodCall>, 'Partial': <function Partial>, 'Set': <function Set>, 'SetAttribute': <function SetAttribute>, 'SetItem': <function SetItem>}¶
- consume_function(*args, **kwargs)[source]¶
Consume a
WXFTokenof type function.Return a
listif the head is symbol List, otherwise returns the result ofbuild_function()applied to the head and arguments.Usually custom parsing rules target Functions, but not List. To do so, it is recommended to override
build_function().
- hook_symbol¶
Provide a convenient way to build objects representing arbitrary Wolfram Language expressions through the use of attributes.
This class is conveniently instantiated at startup as
wl,GlobalandSystem. It should be instantiated only to represent many symbols belonging to the same specific context.Example:
>>> dev = WLSymbolFactory('Developer') >>> dev.PackedArrayQ Developer`PackedArrayQ
Alternative:
>>> wl.Developer.PackedArrayQ Developer`PackedArrayQ
- class wolframclient.utils.externalevaluate.SocketWriter(socket)[source]¶
Bases:
object- keep_listening¶
Provide a convenient way to build objects representing arbitrary Wolfram Language expressions through the use of attributes.
This class is conveniently instantiated at startup as
wl,GlobalandSystem. It should be instantiated only to represent many symbols belonging to the same specific context.Example:
>>> dev = WLSymbolFactory('Developer') >>> dev.PackedArrayQ Developer`PackedArrayQ
Alternative:
>>> wl.Developer.PackedArrayQ Developer`PackedArrayQ
- wolframclient.utils.externalevaluate.start_zmq_instance(port=None, write_to_stdout=True, **opts)[source]¶
- wolframclient.utils.externalevaluate.start_zmq_loop(message_limit=inf, exception_class=None, routes_registry={}, **opts)[source]¶
wolframclient.utils.functional module¶
wolframclient.utils.importutils module¶
- class wolframclient.utils.importutils.API(importer=<function safe_import_string>, **mapping)[source]¶
Bases:
object