?: operator used in C. But by some fortune, I ran across this Python snippet:("False", "True")[var == True]which does exactly the same as what you would write in C
var ? "True" : "False"What a relief!
Eric's lab notes gone stellar!
?: operator used in C. But by some fortune, I ran across this Python snippet:("False", "True")[var == True]var ? "True" : "False"([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]*\.?[0-9]+)?)
([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]*\.?[0-9]+)?)([a-zA-Z]*)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
#define NC 10
#define ALONGSTRING "A long string of nonsense that would've been truncated."
size_t len;
char *string = calloc(NC, sizeof(*string));
len = snprintf(string, NC, ALONGSTRING);
printf("Truncated string: '%s'\n", string);
if(len >= NC) {
string = realloc(string, (len + 1) * sizeof(*string));
snprintf(string, len + 1, ALONGSTRING);
}
printf("Non-truncated string: '%s'\n", string);
free(string);
return 0;
}
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
PyObject *globals, *list, *name, *item, *mp;
Py_ssize_t i, n;
Py_Initialize();
PyRun_SimpleString("a = 'some text'; b = 123.456");
mp = PyImport_AddModule("__main__");
list = PyObject_Dir(mp);
n = PyList_Size(list);
printf("n=%d\n", n);
for(i = 0; i < n; i++) {
name = PyList_GetItem(list, i);
item = PyObject_GetAttr(mp, name);
if(PyString_Check(item))
printf("list[%d] (string): '%s' = '%s'\n", i, PyString_AsString(name), PyString_AsString(item));
else if(PyFloat_Check(item))
printf("list[%d] (float): '%s' = %g\n", i, PyString_AsString(name), PyFloat_AsDouble(item));
}
Py_Finalize();
return 0;
}
gcc embed_python.c -I/usr/include/python2.5 -lpython2.5
S.I. versus c.g.s. Units by Jason Harlow