Skip to content

Commit

Permalink
Merge pull request #437 from soof-golan/soof-obj-id-generation
Browse files Browse the repository at this point in the history
Generate ids with itertools.count
  • Loading branch information
aconchillo authored Sep 2, 2024
2 parents 7c342f7 + 5c0f5a1 commit aba5f89
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/pipecat/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,39 @@
#
# SPDX-License-Identifier: BSD 2-Clause License
#
import collections
import itertools

from threading import Lock

_COUNTS = {}
_COUNTS_MUTEX = Lock()

_ID = 0
_ID_MUTEX = Lock()
_COUNTS = collections.defaultdict(itertools.count)
_ID = itertools.count()


def obj_id() -> int:
global _ID, _ID_MUTEX
with _ID_MUTEX:
_ID += 1
return _ID
"""
Generate a unique id for an object.
>>> obj_id()
0
>>> obj_id()
1
>>> obj_id()
2
"""
return next(_ID)


def obj_count(obj) -> int:
global _COUNTS, COUNTS_MUTEX
name = obj.__class__.__name__
with _COUNTS_MUTEX:
if name not in _COUNTS:
_COUNTS[name] = 0
else:
_COUNTS[name] += 1
return _COUNTS[name]
"""Generate a unique id for an object.
>>> obj_count(object())
0
>>> obj_count(object())
1
>>> new_type = type('NewType', (object,), {})
>>> obj_count(new_type())
0
"""
return next(_COUNTS[obj.__class__.__name__])


def exp_smoothing(value: float, prev_value: float, factor: float) -> float:
Expand Down

0 comments on commit aba5f89

Please sign in to comment.