How can I convert NSDictionary to Python dict?

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

However, I'm having a hard time figuring out how to do the same using the Python C API. In particular, I'm not sure how best to convert an NSDictionary (which can contain integers, strings, booleans, or all of the above) to a format that I can then pass Python as keyword arguments.

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

Edit : just to clarify, I am converting my existing class that used to be Python to Objective-C and I am having a hard time figuring out how to go from NSDictionary in Objective-C to Python Dictionary that I can pass when I call remaining Python scripts. The Objective-C class is basically just a Python loader, but I am not familiar with the Python C API and am having a hard time figuring out where to look for examples or functions to help me.

+2


source to share


1 answer


Oh, it looks like I misunderstood your question. Well, going in the other direction is not at all like that. This should be (at least the beginning) of the function you are looking for (I haven't tested it thoroughly, so beware of 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 can also take a look at the Python / C API Reference if you haven't already.

+2


source







All Articles