Commits

Anonymous committed 34cbcd9

added an example that converts from python collections to objc collections.

Comments (0)

Files changed (2)

pyobjc/Examples/WebServicesTool/main-bin-python.m

   NSString *pythonBinPath = [[NSUserDefaults standardUserDefaults] stringForKey: @"PythonBinPath"];
   pythonBinPath = pythonBinPath ? pythonBinPath : @"/usr/bin/python";
   [pythonBinPath retain];
-  pythonBinPathPtr = [pythonBinPath cString];
+  pythonBinPathPtr = [pythonBinPath UTF8String];
   
   NSString *mainPyFile = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"PrincipalPythonFile"];
   NSString *mainPyPath = nil;
     [NSException raise: NSInternalInconsistencyException
                 format: @"%s:%d pyobjc_main() Failed to find main python entry point for application.  Exiting.", __FILE__, __LINE__];
   [mainPyPath retain];
-  mainPyPathPtr = [mainPyPath cString];
+  mainPyPathPtr = [mainPyPath UTF8String];
 
   childArgv[0] = argv[0];
   childArgv[1] = mainPyPathPtr;

pyobjc/Examples/pydict-to-objcdict.py

+import sys
+import objc
+import pprint
+
+from Foundation import *
+
+input = { 'a' : [1, 2, 3, 4], 'b' : 'c', 'd' : 3, 'e' : None, 'f' : [1, {2 : 3, 'a' : 'b'}, [3, 4]], 'g' : {'h' : 'i'}, 'j' : (1, 2, 3, 'k', 'l')}
+
+print "Converting (Python Collection):"
+pprint.pprint(input)
+
+from types import *
+def convertPyContainerToObjCContainer(aPyContainer):
+    containerType = type(aPyContainer)
+    if containerType == DictType:
+        collection = NSMutableDictionary.dictionary()
+        for aKey in aPyContainer:
+            convertedValue = convertPyContainerToObjCContainer( aPyContainer[aKey] )
+            collection.setObject_forKey_( convertedValue , aKey )
+        return collection
+    elif containerType in [TupleType, ListType]:
+        collection = NSMutableArray.array()
+        for aValue in aPyContainer:
+            convertedValue = convertPyContainerToObjCContainer( aValue )
+            collection.addObject_( convertedValue  )
+        return collection
+    elif containerType in StringTypes:
+        return aPyContainer # bridge will convert to NSString
+    elif containerType == IntType:
+        return NSNumber.numberWithInt_( aPyContainer )
+    elif containerType == LongType:
+        return NSNumber.numberWithLong_( aPyContainer )
+    elif containerType == FloatType:
+        return NSNumber.numberWithFloat_( aPyContainer )
+    elif containerType == NoneType:
+        return NSNull.null()
+    else:
+        raise "UnrecognizedTypeException", "Type %s encountered in python collection;  don't know how to convert." % containerType
+
+convertedCollection = convertPyContainerToObjCContainer(input)
+
+print
+print
+print "Converted (ObjC collection):"
+print "%s" % convertedCollection.description()
+
+