Skip to content

Commit 11b158a

Browse files
committed
a lot of fixings and test cases
1 parent 16e049f commit 11b158a

File tree

8 files changed

+715
-324
lines changed

8 files changed

+715
-324
lines changed

MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include *.h
22
include *.c
33
include LICENSE
4-
include README.rst
4+
include README.rst
5+
include ctool.pyi

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PYTHON_VERSION=$(shell which python | sed "s/\/bin\/python/\/include/" | xargs l
2222

2323
.PHONE:
2424
compile:
25-
gcc -DNDEBUG -g -fwrapv -O3 -Wall -Wextra -std=c99 -arch x86_64 \
25+
gcc -DNDEBUG -g -fwrapv -O3 -Wall -Wextra -std=c99 \
2626
-I$(PYTHON_HOME)/include \
2727
-I$(PYTHON_HOME)/include/$(PYTHON_VERSION) \
2828
-c ctoolsmodule.c -o build/ctools.o

ctools.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ PyDoc_STRVAR(jump_consistent_hash__doc__,
1515
:rtype: int\n");
1616

1717
static PyObject*
18-
Ctools__jump_hash(PyObject* self, PyObject* args)
18+
Ctools__jump_hash(PyObject *Py_UNUSED(ignored), PyObject* args)
1919
{
2020
uint64_t key;
2121
int32_t num_buckets;
@@ -42,7 +42,7 @@ PyDoc_STRVAR(strhash__doc__,
4242
:rtype: int\n");
4343

4444
static PyObject*
45-
Ctools__strhash(PyObject* self, PyObject* args)
45+
Ctools__strhash(PyObject *Py_UNUSED(ignored), PyObject* args)
4646
{
4747
const char* s;
4848
if (!PyArg_ParseTuple(args, "s", &s))
@@ -70,7 +70,7 @@ PyDoc_STRVAR(int8_to_datetime__doc__,
7070
:rtype: datetime.datetime\n");
7171

7272
static PyObject*
73-
Ctools__int8_to_datetime(PyObject* self, PyObject* date_integer)
73+
Ctools__int8_to_datetime(PyObject *Py_UNUSED(ignored), PyObject* date_integer)
7474
{
7575
register long date = PyLong_AsLong(date_integer);
7676
if (date > 99990101 || date < 101) {

ctools.pyi

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,79 @@
11
from datetime import datetime
2-
from typing import Any, Mapping, Iterable, Tuple
2+
from typing import Any, Mapping, Iterable, Tuple, Callable, Optional
33

44
def jump_consistent_hash(key: int, num_bucket: int) -> int: pass
55

66
def strhash(s: str) -> int: ...
77

88
def int8_to_datetime(date_integer: int) -> datetime: ...
99

10+
dict.setdefault()
1011

1112
class LFUCache:
1213

1314
def __init__(self, capacity: int) -> None: ...
1415

15-
def get(self, key, default=None) -> Any: ...
16-
17-
def pop(self, key, default=None) -> Any: ...
18-
19-
def setdefault(self, key, default=None) -> Any: ...
20-
21-
def update(self, map: Mapping, **kwargs) -> None: ...
22-
23-
def keys(self) -> Iterable: ...
24-
25-
def values(self) -> Iterable: ...
16+
def get(self, key, default=None):
17+
""" Return the value for key if key is in the cache, else default. """
18+
pass
2619

27-
def items(self) -> Iterable[Tuple]: ...
20+
def pop(self, key, default=None): # real signature unknown; restored from __doc__
21+
"""
22+
Cache.pop(k[,default]) -> v, remove specified key and return the corresponding value.
23+
If key is not found, default is returned
24+
"""
25+
pass
2826

29-
def clear(self): ...
27+
def setdefault(self, *args, **kwargs): # real signature unknown
28+
"""
29+
Insert key with a value of default if key is not in the dictionary.
3030
31-
def __contains__(self, *args, **kwargs): # real signature unknown
32-
""" True if the dictionary has the specified key, else False. """
31+
Return the value for key if key is in the dictionary, else default.
32+
"""
3333
pass
3434

35-
def __delitem__(self, *args, **kwargs): # real signature unknown
36-
""" Delete self[key]. """
35+
def update(self, mp: Optional[Mapping] = None, **kwargs): # known special case of dict.update
36+
"""
37+
Cache.update([mp, ]**kwargs) -> None. Update Cache from dict/iterable mp and kwargs.
38+
If mp is present then does: for k in mp: Cache[k] = mp[k]
39+
In either case, this is followed by: for k in kwargs: Cache[k] = kwargs[k]
40+
"""
3741
pass
3842

39-
def __eq__(self, *args, **kwargs): # real signature unknown
40-
""" Return self==value. """
43+
def keys(self) -> Iterable:
44+
"""Return key list."""
4145
pass
4246

43-
def __getattribute__(self, *args, **kwargs): # real signature unknown
44-
""" Return getattr(self, name). """
47+
def values(self) -> Iterable:
48+
"""Return value list."""
4549
pass
4650

47-
def __getitem__(self, y): # real signature unknown; restored from __doc__
48-
""" x.__getitem__(y) <==> x[y] """
51+
def items(self) -> Iterable[Tuple]:
52+
"""Return (k, v) pairs list."""
4953
pass
5054

51-
def __ge__(self, *args, **kwargs): # real signature unknown
52-
""" Return self>=value. """
55+
def clear(self):
56+
""" Cache.clear() -> None. Remove all items from Cache. """
5357
pass
5458

55-
def __gt__(self, *args, **kwargs): # real signature unknown
56-
""" Return self>value. """
59+
def __contains__(self, key): # real signature unknown
60+
""" True if the dictionary has the specified key, else False. """
5761
pass
5862

59-
def __iter__(self, *args, **kwargs): # real signature unknown
60-
""" Implement iter(self). """
63+
def __delitem__(self, key): # real signature unknown
64+
""" Delete self[key]. """
6165
pass
6266

63-
def __len__(self, *args, **kwargs): # real signature unknown
64-
""" Return len(self). """
67+
def __setitem__(self, key, value): # real signature unknown
68+
""" self[key] = value. """
6569
pass
6670

67-
def __le__(self, *args, **kwargs): # real signature unknown
68-
""" Return self<=value. """
71+
def __getitem__(self, key): # real signature unknown; restored from __doc__
72+
""" Return self[key] """
6973
pass
7074

71-
def __lt__(self, *args, **kwargs): # real signature unknown
72-
""" Return self<value. """
75+
def __len__(self, *args, **kwargs): # real signature unknown
76+
""" Return len(self). """
7377
pass
7478

7579
def evict(self) -> None: ...
@@ -80,4 +84,10 @@ class LFUCache:
8084

8185
def lfu(self) -> Any: ...
8286

83-
def lfu_of(self, key: Any) -> int: ...
87+
def setnx(self, key, callback: Callable[[], Any]):
88+
"""
89+
Insert key with a value of callback() if key is not in the dictionary.
90+
91+
Return the value for key if key is in the dictionary, else callback().
92+
"""
93+
pass

ctoolsmodule.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,19 @@ PyMODINIT_FUNC
2727
PyInit_ctools(void)
2828
{
2929
PyDateTime_IMPORT;
30-
31-
PyObject* m = PyModule_Create(&ctools_module);
32-
if (m == NULL)
30+
if (PyType_Ready(&LFUCacheType) < 0)
3331
return NULL;
3432

35-
LFUValueType.tp_new = PyType_GenericNew;
36-
if (PyType_Ready(&LFUValueType) < 0)
33+
if (PyType_Ready(&LFUWrapperType) < 0)
3734
return NULL;
3835

39-
LFUCacheType.tp_new = PyType_GenericNew;
40-
if (PyType_Ready(&LFUCacheType) < 0)
36+
PyObject* m = PyModule_Create(&ctools_module);
37+
if (m == NULL)
4138
return NULL;
4239

43-
Py_INCREF(&LFUValueType);
40+
Py_INCREF(&LFUWrapperType);
4441
Py_INCREF(&LFUCacheType);
4542
PyModule_AddObject(m, "LFUCache", (PyObject*)&LFUCacheType);
43+
PyModule_AddObject(m, "LFUWrapper", (PyObject*)&LFUWrapperType);
4644
return m;
4745
}

0 commit comments

Comments
 (0)