piątek, 8 kwietnia 2011

Python: C extensions - sequence-like object

If class have to support standard len() function or operator in, then must be a sequence-like. This require variable of type PySequenceMethods, that store addresses of proper functions. Finally address of this structure have to be assigned to tp_as_sequence member of main PyTypeObject variable.

Here is sample code:

static PySequenceMethods class_seq;

static PyTypeObject class_type_dsc = {
 ...
};

ssize_t
classmeth_len(PyObject* self) {
 if (not error)
  return sequence_size;
 else
  return -1;
}

int
classmeth_contains(PyObject* self, PyObject* value) {
 if (not error) {
  if (value in self)
   return 1;
  else
   return 0;
 }
 else
  return -1;
}


PyMODINIT_FUNC
PyInit_module() {
 class_seq.sq_length   = classmeth_len;
 class_seq.sq_contains = classmeth_contains;

 class_type_dsc.tp_as_sequence = &class_seq;

 ...
}

wtorek, 29 marca 2011

pyahocorasick

Python module implementing Aho-Corasick algorithm has been released. C extension (for Py3k) and pure python code are available.

poniedziałek, 28 marca 2011

Internal memory fragmentation

In previous post I've advertised my text about trie representations.

Depending on particular representation internal memory fragmentation vary from 25% to 46% (in GNU libc). In other words if trie should occupy 100MB then in the worst case real memory usage is around 200MB. I've never suppose that fragmentation could be so significant.

When quite simple memory pools were used, then internal fragmentation has been cut down to 1-2%! Impressive.

niedziela, 27 lutego 2011

środa, 23 lutego 2011

Grafika komputerowa I

Całkiem udany skrypt do grafiki komputerowej autorstwa Przemysława Kiciaka udostępnił UW w ramach swojego portalu z materiałami dydaktycznymi. Obszerna tematyka, więc miejscami skrótowo i ogólnikowo, ale ogólnie wygląda zachęcająco.

niedziela, 10 października 2010

PIC Language

Yesterday I made my first diagram with PIC (see Wikipedia) and... I felt in love. Language is just perfect, intuitive and powerful. Try it and you will never use any popular drawing software.