Skip to content

Commit 8ce9e6a

Browse files
Merge pull request #3 from dawagner/fix-autocompletion
Use namedtuple instead of the custom DotDict
2 parents 14e3c84 + 5abbe24 commit 8ce9e6a

3 files changed

Lines changed: 20 additions & 40 deletions

File tree

example/example.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async def scaleEarth(value):
1919

2020
property = "Scene.Earth.Scale.Scale"
2121
data = await os.getProperty(property)
22-
data = os.toDotDict(data)
22+
data = os.toNamedTuple(data)
2323

2424
print(f"Current scale value: {data.Value}")
2525
os.setProperty(property, value)
@@ -33,15 +33,15 @@ async def subscribeToEarthScaleUpdates():
3333
while i < 3:
3434
print("Waiting for Earth scale update...")
3535
result = await os.nextValue(subscription)
36-
dic = os.toDotDict(result)
36+
dic = os.toNamedTuple(result)
3737
print(f"{dic.Description.Identifier} changed to {dic.Value}")
3838
i += 1
3939
subscription.cancel()
4040

4141
## Or using async for loop
4242
# async for future in subscription.iterator():
4343
# result = await future
44-
# dic = api.toDotDict(result)
44+
# dic = api.toNamedTuple(result)
4545
# print(f"{dic.Description.Identifier} changed to {dic.Value}")
4646
# if i > 3:
4747
# subscription.cancel()
@@ -166,4 +166,4 @@ async def mainLoop():
166166

167167
loop = asyncio.new_event_loop()
168168
loop.run_until_complete(mainLoop())
169-
loop.run_forever()
169+
loop.run_forever()

src/openspace/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .src.api import Api, DotDict
1+
from .src.api import Api
22

3-
__version__ = "0.1.2"
3+
__version__ = "0.1.2"

src/openspace/src/api.py

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,8 @@
33
from traceback import print_exc
44
from .topic import Topic
55
from .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

339
class 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

Comments
 (0)