MCPcopy Create free account
hub / github.com/Flagsmith/flagsmith / iter_chunked_concat

Function iter_chunked_concat

api/util/util.py:49–83  ·  view source on GitHub ↗

Iterate over a list of strings and yield concatenated chunks separated by a delimiter, ensuring that the total length of each chunk does not exceed `max_len`.

(
    values: list[str],
    delimiter: str,
    max_len: int,
)

Source from the content-addressed store, hash-verified

47
48
49def iter_chunked_concat(
50 values: list[str],
51 delimiter: str,
52 max_len: int,
53) -> Generator[str, None, None]:
54 """
55 Iterate over a list of strings and yield concatenated chunks
56 separated by a delimiter, ensuring that the total length of each
57 chunk does not exceed `max_len`.
58 """
59 chunk: list[str] = []
60 current_len = 0
61
62 delimiter_len = len(delimiter)
63
64 for value in values:
65 value_len = len(value)
66 if not chunk:
67 # First item, no delimiter
68 chunk.append(value)
69 current_len = value_len
70 else:
71 # Calculate length if we add this string with delimiter
72 added_len = delimiter_len + value_len
73 if current_len + added_len <= max_len:
74 chunk.append(value)
75 current_len += added_len
76 else:
77 # Yield current chunk and start a new one
78 yield delimiter.join(chunk)
79 chunk = [value]
80 current_len = value_len
81
82 if chunk:
83 yield delimiter.join(chunk)
84
85
86def truncate(

Calls 1

appendMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…