33from traceback import print_exc
44from .topic import Topic
55from .socketwrapper import SocketWrapper
6- from typing import Callable
7-
8- class DotDict (dict ):
9- """ A dictionary subclass supporting dot.notation.
10-
11- Example usage:
12- `foo = DotDict({'bar': 'baz'})`\n
13- `print(foo.bar)` output 'baz'
14-
15- Set attributes:
16- `foo.bar = 'foo'`
17-
18- Delete attributes:
19- `del foo.bar`
20-
21- Convert a `Dict` to a `DotDict` using `DotDict(dict)`. However, it does not convert
22- nested dictionaries. Instead use `OpenSpaceApi.toDotDict()`
23- `foo = {"bar": "baz"}`\n
24- `foo = DotDict(foo)` """
25-
26- __getattr__ = dict .__getitem__
27- __setattr__ = dict .__setitem__
28- __delattr__ = dict .__delitem__
29-
30- def __dir__ (self ):
31- return dir (dict ) + list (self .keys ())
6+ from typing import Callable , NamedTuple
7+ from collections import namedtuple
328
339class Api :
3410 """ Construct an instance of the OpenSpace API. \n
@@ -346,7 +322,7 @@ async def executeLuaFunction(self, function: str, args, getReturnValue = True):
346322 else :
347323 topic .cancel ()
348324
349- async def library (self , multiReturn : bool ) -> DotDict :
325+ async def library (self , multiReturn : bool ) -> NamedTuple :
350326 """ Get an object representing the OpenSpace lua libarary. \n
351327 :param multiReturn - whether the library should return the raw lua tables.
352328 If this value is true, the 1-indexed lua table will be returned as a dict
@@ -395,16 +371,20 @@ async def fun(*args):
395371 else :
396372 subPyLibrary [func ['name' ]] = generateAsyncSingleRetFunction (fullFunctionName )
397373
398- return self .toDotDict (pyLibrary )
374+ return self .toNamedTuple (pyLibrary , libraryName )
399375
400- def toDotDict (self , dictionary : dict ) -> DotDict :
401- """ Recursively converts a `dictionary` to a `DotDict `. """
376+ def toNamedTuple (self , content : dict , name : str = "namedtuple" ) -> NamedTuple :
377+ """ Recursively converts a `dictionary` to a `namedtuple `. """
402378
403- for k , v in dictionary .items ():
379+ T = namedtuple (name , content .keys ())
380+ values = []
381+ for k , v in content .items ():
404382 if isinstance (v , dict ):
405- dictionary [k ] = self .toDotDict (v )
383+ values .append (self .toNamedTuple (v , k ))
384+ else :
385+ values .append (v )
406386
407- return DotDict ( dictionary )
387+ return T ( * values )
408388
409389 async def singleReturnLibrary (self ):
410390 """ Get an object representing the OpenSpace lua library. \n
@@ -418,4 +398,4 @@ async def multiReturnLibrary(self):
418398 :return - The lua library, mapped to async python functions. The values returned
419399 by the async functions will be the entire lua tables, with 1-indexed values. """
420400
421- return await self .library (True )
401+ return await self .library (True )
0 commit comments