Browse Source

Port cython and C code to python 3

tags/nilmdb-2.0.0
Jim Paris 2 years ago
parent
commit
55119a3e07
7 changed files with 29 additions and 14 deletions
  1. +4
    -0
      nilmdb/server/interval.pyx
  2. +2
    -0
      nilmdb/server/rbtree.pxd
  3. +1
    -0
      nilmdb/server/rbtree.pyx
  4. +16
    -10
      nilmdb/server/rocket.c
  5. +3
    -1
      nilmdb/utils/lrucache.py
  6. +2
    -2
      nilmdb/utils/serializer.py
  7. +1
    -1
      tests/test_bulkdata.py

+ 4
- 0
nilmdb/server/interval.pyx View File

@@ -1,3 +1,5 @@
# cython: language_level=2

"""Interval, IntervalSet

The Interval implemented here is just like
@@ -60,6 +62,8 @@ cdef class Interval:

def __cmp__(self, Interval other):
"""Compare two intervals. If non-equal, order by start then end"""
def cmp(a, b):
return (a > b) - (a < b)
return cmp(self.start, other.start) or cmp(self.end, other.end)

cpdef intersects(self, Interval other):


+ 2
- 0
nilmdb/server/rbtree.pxd View File

@@ -1,3 +1,5 @@
# cython: language_level=2

cdef class RBNode:
cdef public object obj
cdef public double start, end


+ 1
- 0
nilmdb/server/rbtree.pyx View File

@@ -1,5 +1,6 @@
# cython: profile=False
# cython: cdivision=True
# cython: language_level=2

"""
Jim Paris <jim@jtan.com>


+ 16
- 10
nilmdb/server/rocket.c View File

@@ -138,7 +138,7 @@ static void Rocket_dealloc(Rocket *self)
fclose(self->file);
self->file = NULL;
}
self->ob_type->tp_free((PyObject *)self);
Py_TYPE(self)->tp_free((PyObject *)self);
}

static PyObject *Rocket_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
@@ -239,7 +239,7 @@ static PyObject *Rocket_get_file_size(Rocket *self)
return NULL;
}
}
return PyInt_FromLong(self->file_size);
return PyLong_FromLong(self->file_size);
}

/****
@@ -585,7 +585,7 @@ static PyObject *Rocket_extract_string(Rocket *self, PyObject *args)
str[len++] = '\n';
}

PyObject *pystr = PyString_FromStringAndSize(str, len);
PyObject *pystr = PyBytes_FromStringAndSize(str, len);
free(str);
return pystr;
err:
@@ -748,7 +748,7 @@ static PyMethodDef Rocket_methods[] = {
};

static PyTypeObject RocketType = {
PyObject_HEAD_INIT(NULL)
PyVarObject_HEAD_INIT(NULL, 0)

.tp_name = "rocket.Rocket",
.tp_basicsize = sizeof(Rocket),
@@ -773,17 +773,23 @@ static PyMethodDef module_methods[] = {
{ NULL },
};

PyMODINIT_FUNC
initrocket(void)
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
.m_name = "rocker",
.m_doc = "Rocket data parsing and formatting module",
.m_size = -1,
.m_methods = module_methods,
};

PyMODINIT_FUNC PyInit_rocket(void)
{
PyObject *module;

RocketType.tp_new = PyType_GenericNew;
if (PyType_Ready(&RocketType) < 0)
return;
return NULL;

module = Py_InitModule3("rocket", module_methods,
"Rocket data parsing and formatting module");
module = PyModule_Create(&moduledef);
Py_INCREF(&RocketType);
PyModule_AddObject(module, "Rocket", (PyObject *)&RocketType);

@@ -792,5 +798,5 @@ initrocket(void)
PyModule_AddObject(module, "ParseError", ParseError);
add_parseerror_codes(module);

return;
return module;
}

+ 3
- 1
nilmdb/utils/lrucache.py View File

@@ -59,8 +59,10 @@ def lru_cache(size = 10, onremove = None, keys = slice(None)):
"cache key length")

def cache_remove_all():
nonlocal cache
for key in cache:
evict(cache.pop(key))
evict(cache[key])
cache = collections.OrderedDict()

def cache_info():
return (func.cache_hits, func.cache_misses)


+ 2
- 2
nilmdb/utils/serializer.py View File

@@ -69,7 +69,7 @@ def serializer_proxy(obj_or_type):
def __init__(self, obj_or_type, *args, **kwargs):
self.__object = obj_or_type
try:
if type(obj_or_type) in (type, type):
if type(obj_or_type) == type:
classname = obj_or_type.__name__
else:
classname = obj_or_type.__class__.__name__
@@ -110,7 +110,7 @@ def serializer_proxy(obj_or_type):
to serializer_proxy. Otherwise, pass the call through."""
ret = SerializerCallProxy(self.__call_queue,
self.__object, self)(*args, **kwargs)
if type(self.__object) in (type, type):
if type(self.__object) == type:
# Instantiation
self.__object = ret
return self


+ 1
- 1
tests/test_bulkdata.py View File

@@ -8,7 +8,7 @@ import itertools

from testutil.helpers import *

testdb = "tests/bulkdata-testdb"
testdb = b"tests/bulkdata-testdb"

import nilmdb.server.bulkdata
from nilmdb.server.bulkdata import BulkData


Loading…
Cancel
Save