开发者

extending module dictionary in cpython to return arbitrarily named objects

Ive been tasked with implementing a python interface to our modelling software. I apologize but this is my first exposure to python, so its a slow process. Ive been working in CPython

Here's my problem. If a user types:

a = model_123456 (for example), I would like it to return the corresponding object. The problem is these objects are dynamically created in the software.

I do not want to populate the globals() dict with potentially thousands of these objects, if they're never accessed.

I do not want to implement it as a method eg: a = getmodel(123456) as we are striving for brevity, making most access through member attributes, not methods.

I've been investigating different solutions but none quite seem to be applicable or clear (eg "gettr on a module")

I think what I need to do is create a dictionary in my top level module, from which I can override the getitem method?

If thats the case, Im having trouble figuring out how to do this in CPython.

eg: m = PyModule_Create(&tdmodule)

Question 1: This already returns an object with tp_dict set. How would I set/override this slot otherwise?

Question 2: How do I override the getitem function of a PyDict object in CPython (or create a dictionary with that similar attribute)? Do I need to create a dictionary with a custom Mapping getsubscript method, or can I simply setattr on setitem?

Again my ap开发者_Python百科ologies if this question sounds confused and making lots of bad assumptions, it probably is.

Thanks for any clarification, Rob.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜