Pokazywanie postów oznaczonych etykietą python. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą python. Pokaż wszystkie posty

czwartek, 11 września 2014

Python: rename file in Windows

Observation: function os.remove under Windows doesn't allow to overwrite an existing file. Python from Cygwin works properly.

Here is a workaround:

def rename_file(old, new):
    if sys.platform != 'win32':
        os.rename(new, old)
    else:
        os.remove(old)
        os.rename(new, old)

sobota, 9 kwietnia 2011

pyDAWG

pyDAWG is a python module implementing Directed Acyclic Word Graph, that allow to store set of words in a compacted way. DAWGs are much smaller then tries, while sharing the main advantage of tries - linear time to check if word is present in a set.

The main module is a C extension, there is also a pure python code.

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.

niedziela, 27 lutego 2011