如何将 NSDictionary 转换为 Python 字典?

发布于 2024-08-05 01:25:12 字数 504 浏览 4 评论 0原文

我有一个完全用 Python 使用 PyObjC 编写的插件,我需要将其核心类转换为 Objective-C。其中之一基本上只是加载一个 Python 模块并执行一个特定的函数,并向其传递关键字参数。在 PyObjC 中,这非常严重。

然而,我很难弄清楚如何使用 Python C API 来做同样的事情。特别是,我不确定如何最好地将 NSDictionary(可能包含整数、字符串、布尔值或以上所有内容)转换为一种格式,然后我可以将其作为关键字参数传递给 Python。

有人知道如何完成这样的事情吗?提前致谢!

编辑:只是为了澄清一下,我正在将以前的 Python 现有类转换为 Objective-C,并且无法弄清楚如何从 Objective-C 中的 NSDictionary 迁移到 Python 字典当我调用剩余的 Python 脚本时可以继续。 Objective-C 类基本上只是一个 Python 加载器,但我不熟悉 Python C API,并且无法弄清楚在哪里寻找对我有帮助的示例或函数。

I have a plugin written entirely in Python using PyObjC whose core classes I need to convert to Objective-C. One of them basically just loads up a Python module and executes a specific function, passing it keyword arguments. In PyObjC, this was extremely.

However, I'm having difficulty figuring out how to do the same thing using the Python C API. In particular, I'm unsure how best to convert an NSDictionary (which might hold integers, strings, booleans, or all of the above) into a format that I can then pass on to Python as keyword arguments.

Anyone have pointers on how to accomplish something like this? Thanks in advance!

Edit: just to clarify, I'm converting my existing class which was formerly Python into Objective-C, and am having trouble figuring out how to move from an NSDictionary in Objective-C to a Python dictionary I can pass on when I invoke the remaining Python scripts. The Objective-C class is basically just a Python loader, but I'm unfamiliar with the Python C API and am having trouble figuring out where to look for examples or functions that will help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2024-08-12 01:25:12

哦,看来我误解了你的问题。好吧,走向另一个方向并没有太大的不同。这应该是(至少是一个开始)您正在寻找的功能(不过我还没有彻底测试过它,所以要小心错误):

// Returns a new reference
PyObject *ObjcToPyObject(id object)
{
    if (object == nil) {
        // This technically doesn't need to be an extra case, 
        // but you may want to differentiate it for error checking
        return NULL;
    } else if ([object isKindOfClass:[NSString class]]) {
        return PyString_FromString([object UTF8String]);
    } else if ([object isKindOfClass:[NSNumber class]]) {
        // You could probably do some extra checking here if you need to
        // with the -objCType method.
        return PyLong_FromLong([object longValue]);
    } else if ([object isKindOfClass:[NSArray class]]) {
        // You may want to differentiate between NSArray (analagous to tuples) 
        // and NSMutableArray (analagous to lists) here.
        Py_ssize_t i, len = [object count];
        PyObject *list = PyList_New(len);
        for (i = 0; i < len; ++i) {
            PyObject *item = ObjcToPyObject([object objectAtIndex:i]);
            NSCAssert(item != NULL, @"Can't add NULL item to Python List");
            // Note that PyList_SetItem() "steals" the reference to the passed item.
            // (i.e., you do not need to release it)
            PyList_SetItem(list, i, item);
        }
        return list;
    } else if ([object isKindOfClass:[NSDictionary class]]) {
        PyObject *dict = PyDict_New();
        for (id key in object) {
            PyObject *pyKey = ObjcToPyObject(key);
            NSCAssert(pyKey != NULL, @"Can't add NULL key to Python Dictionary");
            PyObject *pyItem = ObjcToPyObject([object objectForKey:key]);
            NSCAssert(pyItem != NULL, @"Can't add NULL item to Python Dictionary");
            PyDict_SetItem(dict, pyKey, pyItem);
            Py_DECREF(pyKey);
            Py_DECREF(pyItem);
        }
        return dict;
    } else {
        NSLog(@"ObjcToPyObject() could not convert Obj-C object to PyObject.");
        return NULL;
    }
}

您可能还想看看 Python/C API 参考手册(如果您还没有)。

Oh, looks like I misunderstood your question. Well, going the other direction isn't terribly different. This should be (as least a start of) the function you're looking for (I haven't tested it thoroughly though, so beware of the bugs):

// Returns a new reference
PyObject *ObjcToPyObject(id object)
{
    if (object == nil) {
        // This technically doesn't need to be an extra case, 
        // but you may want to differentiate it for error checking
        return NULL;
    } else if ([object isKindOfClass:[NSString class]]) {
        return PyString_FromString([object UTF8String]);
    } else if ([object isKindOfClass:[NSNumber class]]) {
        // You could probably do some extra checking here if you need to
        // with the -objCType method.
        return PyLong_FromLong([object longValue]);
    } else if ([object isKindOfClass:[NSArray class]]) {
        // You may want to differentiate between NSArray (analagous to tuples) 
        // and NSMutableArray (analagous to lists) here.
        Py_ssize_t i, len = [object count];
        PyObject *list = PyList_New(len);
        for (i = 0; i < len; ++i) {
            PyObject *item = ObjcToPyObject([object objectAtIndex:i]);
            NSCAssert(item != NULL, @"Can't add NULL item to Python List");
            // Note that PyList_SetItem() "steals" the reference to the passed item.
            // (i.e., you do not need to release it)
            PyList_SetItem(list, i, item);
        }
        return list;
    } else if ([object isKindOfClass:[NSDictionary class]]) {
        PyObject *dict = PyDict_New();
        for (id key in object) {
            PyObject *pyKey = ObjcToPyObject(key);
            NSCAssert(pyKey != NULL, @"Can't add NULL key to Python Dictionary");
            PyObject *pyItem = ObjcToPyObject([object objectForKey:key]);
            NSCAssert(pyItem != NULL, @"Can't add NULL item to Python Dictionary");
            PyDict_SetItem(dict, pyKey, pyItem);
            Py_DECREF(pyKey);
            Py_DECREF(pyItem);
        }
        return dict;
    } else {
        NSLog(@"ObjcToPyObject() could not convert Obj-C object to PyObject.");
        return NULL;
    }
}

You may also want to take a look at the Python/C API Reference manual if you haven't already.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文