Python for ios interpreter [duplicate]

2024/10/14 7:16:22

Possible Duplicate:
Python or Ruby Interpreter on iOS

I just discovered this apps pypad and python for ios

They have like an interpreter an editor

So which app would you recomend

But most importantly, how does this interpreter work, and where can i see an example of how the obj c and python get to work togheter?

Thanks!

Answer

I am the sole creator of Python for iOS so that is of course what I would recommend, but a good indicator for your personal decision is the reviews & ratings of each App. It took me weeks to figure out how to properly integrate python into Objective-c for this App but here is the best resource to get you started (keep in mind that ObjC is just a superset of C):

http://docs.python.org/c-api/


Also, here is an example of calling a function defined in myModule. The equivient python would be:

import myModule
pValue = myModule.doSomething()
print pValue

In Objective-c:

#include <Python.h>- (void)example {PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;NSString *nsString;// Initialize the Python InterpreterPy_Initialize();// Build the name objectpName = PyString_FromString("myModule");// Load the module objectpModule = PyImport_Import(pName);// pDict is a borrowed reference pDict = PyModule_GetDict(pModule);// pFunc is also a borrowed reference pFunc = PyDict_GetItemString(pDict, "doSomething");if (PyCallable_Check(pFunc)) {pValue = PyObject_CallObject(pFunc, NULL);if (pValue != NULL) {if (PyObject_IsInstance(pValue, (PyObject *)&PyUnicode_Type)) {nsString = [NSString stringWithCharacters:((PyUnicodeObject *)pValue)->str length:((PyUnicodeObject *) pValue)->length];} else if (PyObject_IsInstance(pValue, (PyObject *)&PyBytes_Type)) {nsString = [NSString stringWithUTF8String:((PyBytesObject *)pValue)->ob_sval];} else {/* Handle a return value that is neither a PyUnicode_Type nor a PyBytes_Type */}Py_XDECREF(pValue);} else {PyErr_Print();}} else {PyErr_Print();}// Clean upPy_XDECREF(pModule);Py_XDECREF(pName);// Finish the Python InterpreterPy_Finalize();NSLog(@"%@", nsString);
}

For much more documentation check out: Extending and Embedding the Python Interpreter

https://en.xdnf.cn/q/69440.html

Related Q&A

Detect when multiprocessing queue is empty and closed

Lets say I have two processes: a reader and a writer. How does the writer detect when the reader has finished writing values?The multiprocessing module has a queue with a close method that seems custo…

imshow and histogram2d: cant get them to work

Im learning Python and this is my first question here. Ive read other topics related to the usage of imshow but didnt find anything useful. Sorry for my bad English.I have plotted a set of points here,…

3D Waterfall Plot with Colored Heights

Im trying to visualise a dataset in 3D which consists of a time series (along y) of x-z data, using Python and Matplotlib.Id like to create a plot like the one below (which was made in Python: http://a…

python xlsxwriter: Keep header in excel when adding a table

I have a panda dataframe that I write to a xslx file, and would like to add a table over that data. I would also like to keep the headers that I have already written, instead of adding them again. Is t…

Unexpected tokens in !DOCTYPE html in PyCharm Community Edition

I am new in using PyCharm but I am loving it gradually. I am getting a red underline on <!DOCTYPE html> and the error is "Unexpected Token".Why PyCharm shows it? I cant understand.

Indexing a numpy array with a numpy array of indexes [duplicate]

This question already has answers here:Indexing a numpy array with a list of tuples(2 answers)Index multidimensional array with index array(1 answer)Closed 5 years ago.I have a 3D numpy array data and …

Input redirection with python

I have the following program to test input redirection in Python.a = int(raw_input("Enter a number: ")) b = raw_input("Enter a string: ") print "number entered = ", a prin…

TypeError: can only concatenate str (not numpy.int64) to str

I want to print the variable based on the index number based on the following dataset:Here I used the following code:import pandas as pdairline = pd.read_csv("AIR-LINE.csv")pnr = input("…

Saving scatterplot animations with matplotlib produces blank video file

I am having a very similar problem to this questionbut the suggested solution doesnt work for me. I have set up an animated scatter plot using the matplotlib animation module. This works fine when it i…

How to group near-duplicate values in a pandas dataframe?

If there are duplicate values in a DataFrame pandas already provides functions to replace or drop duplicates. In many experimental datasets on the other hand one might have near duplicates. How can one…