Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / historic / 2003 / pycon / intrinsics-lightning / intrinsics-lightning
1 #!/usr/bin/python
2
3 from slides import *
4 from twslides import *
5
6 class PythonSource:
7     def __init__(self, content):
8         self.content = content 
9     def toHTML(self):
10         return '<pre class="python">%s</pre>' % (self.content,)
11
12 lecture = Lecture(
13     "Changing the Type of Literals",
14     Slide("New-style classes",
15         Bullet("In 2.2+, built-in types can be subclassed"),
16         Bullet("These can be created explicitly by using their name", SubBullet(
17             Bullet("For example, an int subclass that displays itself in roman numerals"),
18             Bullet("print RomanNumeral(13) -> XIII"),
19         )),
20     ),
21     Slide("Literals are less accessable",
22         Bullet("When you write [] or 7, the list or int type is instantiated"),
23         Bullet("This behavior seems inaccessable"),
24         Bullet("While this makes for more readable code, it limits the scope of possible evil"),
25     ),
26     Slide("Throw in an extension module...",
27         Bullet("intrinsics.so exposes one function, 'replace'"),
28         Bullet("It takes two arguments", SubBullet(
29             Bullet("A type object to replace"),
30             Bullet("The type object with which to replace it"),
31         )),
32         Bullet("Magic is performed, and the new type is now used whenever the old one would have been"),
33     ),
34     Slide("An example",
35         PythonSource("""\
36 class RomanNumeral(int):
37     def __str__(self):
38         # Regular code for formatting roman numerals
39
40 old_int = intrinsics.replace(int, RomanNumeral)
41 print 13
42 """
43         ),
44         Bullet("The output is simply the roman numerals XIII"),
45     ),
46     Slide("intrinsics.c - The replacement",
47         PRE("""\
48 PyObject*
49 intrinsics_replace(PyObject* self, PyObject* args) {
50     static PyTypeObject* const types[] = {
51         &PyInt_Type,    &PyLong_Type,         &PyFloat_Type, &PyComplex_Type,
52         &PyBool_Type,   &PyBaseObject_Type,   &PyDict_Type,  &PyTuple_Type,
53         &PyBuffer_Type, &PyClassMethod_Type,  &PyEnum_Type,  &PyProperty_Type,
54         &PyList_Type,   &PyStaticMethod_Type, &PySlice_Type, &PySuper_Type,
55         &PyType_Type,   &PyRange_Type,        &PyFile_Type,  &PyUnicode_Type,
56         &PyString_Type,
57         NULL
58     };
59
60     int i = 0;
61     PyObject *old, *new;
62     PyTypeObject* space;
63
64     if (!PyArg_ParseTuple(args, "OO:replace", &old, &new))
65         return NULL;
66 """
67         ),
68     ),
69     Slide("intrinsics.c - The actual replacement",
70         PRE("""\
71     while (types[i]) {
72         if (types[i] == (PyTypeObject*)old) {
73             space = PyObject_New(PyTypeObject, &PyType_Type);
74             *space = *(types[i]);
75             *(types[i]) = *(PyTypeObject*)new;
76             break;
77         }
78         ++i;
79     }
80     if (!types[i]) {
81         PyErr_SetString(replace_error, "unknown type");
82         return NULL;
83     }
84     Py_INCREF(new);
85     Py_INCREF(space);
86     return (PyObject*)space;
87 }
88 """
89         ),
90     ),
91     Slide("This is the wrong answer",
92         Bullet("The right answer is to add more flexibility to the Python compiler"),
93         Bullet("This is a lot less code, though"),
94     )
95 )
96
97 lecture.renderHTML(".", "intrinsics-lightning-%d.html", css="stylesheet.css")