Remove all header tuples for `key` and add a new one. The newly added key either appears at the end of the list if there was no entry or replaces the first one. Keyword arguments can specify additional parameters for the header value, with underscores converted to d
(self, key: str, value: t.Any, /, **kwargs: t.Any)
| 373 | self._list.clear() |
| 374 | |
| 375 | def set(self, key: str, value: t.Any, /, **kwargs: t.Any) -> None: |
| 376 | """Remove all header tuples for `key` and add a new one. The newly |
| 377 | added key either appears at the end of the list if there was no |
| 378 | entry or replaces the first one. |
| 379 | |
| 380 | Keyword arguments can specify additional parameters for the header |
| 381 | value, with underscores converted to dashes. See :meth:`add` for |
| 382 | more information. |
| 383 | |
| 384 | .. versionchanged:: 0.6.1 |
| 385 | :meth:`set` now accepts the same arguments as :meth:`add`. |
| 386 | |
| 387 | :param key: The key to be inserted. |
| 388 | :param value: The value to be inserted. |
| 389 | """ |
| 390 | if kwargs: |
| 391 | value = _options_header_vkw(value, kwargs) |
| 392 | |
| 393 | value_str = _str_header_value(value) |
| 394 | |
| 395 | if not self._list: |
| 396 | self._list.append((key, value_str)) |
| 397 | return |
| 398 | |
| 399 | iter_list = iter(self._list) |
| 400 | ikey = key.lower() |
| 401 | |
| 402 | for idx, (old_key, _) in enumerate(iter_list): |
| 403 | if old_key.lower() == ikey: |
| 404 | # replace first occurrence |
| 405 | self._list[idx] = (key, value_str) |
| 406 | break |
| 407 | else: |
| 408 | # no existing occurrences |
| 409 | self._list.append((key, value_str)) |
| 410 | return |
| 411 | |
| 412 | # remove remaining occurrences |
| 413 | self._list[idx + 1 :] = [t for t in iter_list if t[0].lower() != ikey] |
| 414 | |
| 415 | def setlist(self, key: str, values: cabc.Iterable[t.Any]) -> None: |
| 416 | """Remove any existing values for a header and add new ones. |