mirror of
https://github.com/django-components/django-components.git
synced 2025-08-10 17:28:00 +00:00

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
25 lines
700 B
Python
25 lines
700 B
Python
from typing import Any, Callable, List
|
|
|
|
# Global counter to ensure that all IDs generated by `gen_id` WILL be unique
|
|
_id = 0
|
|
|
|
|
|
def gen_id(length: int = 5) -> str:
|
|
"""Generate a unique ID that can be associated with a Node"""
|
|
# Global counter to avoid conflicts
|
|
global _id
|
|
_id += 1
|
|
|
|
# Pad the ID with `0`s up to 4 digits, e.g. `0007`
|
|
return f"{_id:04}"
|
|
|
|
|
|
def find_last_index(lst: List, predicate: Callable[[Any], bool]) -> Any:
|
|
for r_idx, elem in enumerate(reversed(lst)):
|
|
if predicate(elem):
|
|
return len(lst) - 1 - r_idx
|
|
return -1
|
|
|
|
|
|
def is_str_wrapped_in_quotes(s: str) -> bool:
|
|
return s.startswith(('"', "'")) and s[0] == s[-1] and len(s) >= 2
|