Skip to content

Commit cdd35df

Browse files
committed
Add reprs
1 parent c64ff0d commit cdd35df

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

sonyflake_turbo.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,56 @@ static PyObject *sonyflake_next(struct sonyflake_state *self) {
271271
return PyLong_FromUnsignedLongLong(sonyflake_id);
272272
}
273273

274+
static PyObject *sonyflake_repr(struct sonyflake_state *self) {
275+
PyObject *s, *args_list = PyList_New(self->machine_ids_len + 1);
276+
277+
if (!args_list) {
278+
return NULL;
279+
}
280+
281+
s = PyUnicode_FromFormat("start_time=%ld", (long) self->start_time.tv_sec);
282+
283+
if (!s) {
284+
Py_DECREF(args_list);
285+
return NULL;
286+
}
287+
288+
PyList_SetItem(args_list, self->machine_ids_len, s);
289+
290+
for (Py_ssize_t i = 0; i < self->machine_ids_len; i++) {
291+
s = PyUnicode_FromFormat("%u", (unsigned) self->machine_ids[i]);
292+
293+
if (!s) {
294+
Py_DECREF(args_list);
295+
return NULL;
296+
}
297+
298+
PyList_SetItem(args_list, i, s);
299+
}
300+
301+
s = PyUnicode_FromString(", ");
302+
303+
if (!s) {
304+
Py_DECREF(args_list);
305+
return NULL;
306+
}
307+
308+
PyObject *args_str = PyUnicode_Join(s, args_list);
309+
310+
Py_DECREF(args_list);
311+
Py_DECREF(s);
312+
313+
if (!args_str) {
314+
return NULL;
315+
}
316+
317+
s = PyUnicode_FromFormat("SonyFlake(%U)", args_str);
318+
319+
Py_DECREF(args_str);
320+
321+
return s;
322+
}
323+
274324
PyDoc_STRVAR(sonyflake_doc,
275325
"SonyFlake(*machine_id, start_time=None)\n--\n\n"
276326
"SonyFlake ID generator implementation that combines multiple ID generators into one to improve throughput.\n"
@@ -286,6 +336,7 @@ static PyType_Slot sonyflake_type_slots[] = {
286336
{Py_tp_new, sonyflake_new},
287337
{Py_tp_init, sonyflake_init},
288338
{Py_tp_doc, sonyflake_doc},
339+
{Py_tp_repr, sonyflake_repr},
289340
{0, 0},
290341
};
291342

@@ -357,6 +408,13 @@ static PyObject *machine_id_lcg_next(struct machine_id_lcg_state *self) {
357408
return PyLong_FromLong(machine_id_lcg_atomic(&self->machine_id));
358409
}
359410

411+
static PyObject *machine_id_lcg_repr(struct machine_id_lcg_state *self) {
412+
return PyUnicode_FromFormat(
413+
"MachineIDLCG(%u)",
414+
atomic_load_explicit(&self->machine_id, memory_order_relaxed)
415+
);
416+
}
417+
360418
static PyObject *machine_id_lcg_call(struct machine_id_lcg_state *self, PyObject *args, PyObject *kwargs) {
361419
return machine_id_lcg_next(self);
362420
}
@@ -376,6 +434,7 @@ static PyType_Slot machine_id_lcg_slots[] = {
376434
{Py_tp_new, machine_id_lcg_new},
377435
{Py_tp_call, machine_id_lcg_call},
378436
{Py_tp_doc, machine_id_lcg_doc},
437+
{Py_tp_repr, machine_id_lcg_repr},
379438
{0, 0},
380439
};
381440

tests/test_sonyflake_turbo.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ def test_sonyflake_iter() -> None:
6262
assert sorted(ids) == ids
6363

6464

65+
def test_sonyflake_repr() -> None:
66+
sf = SonyFlake(0x0000, 0x7F7F, 0xFFFF, start_time=1749081600)
67+
68+
assert repr(sf) == "SonyFlake(0, 32639, 65535, start_time=1749081600)"
69+
70+
6571
@mark.skipif(
6672
not sysconfig.get_config_var("Py_GIL_DISABLED"),
6773
reason="Requires Python with GIL disabled",
@@ -92,3 +98,9 @@ def test_machine_id_lcg() -> None:
9298

9399
assert not (set(ids_seq) - set(ids_rng))
94100
assert ids_seq != ids_rng
101+
102+
103+
def test_machine_id_lcg_repr() -> None:
104+
lcg = MachineIDLCG(57243)
105+
106+
assert repr(lcg) == "MachineIDLCG(51966)"

0 commit comments

Comments
 (0)