MCPcopy
hub / github.com/pallets/werkzeug / set

Method set

src/werkzeug/datastructures/headers.py:375–413  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.

Callers 15

send_fileFunction · 0.95
setlistMethod · 0.95
setdefaultMethod · 0.95
__setitem__Method · 0.95
updateMethod · 0.95
__init__Method · 0.45
__release_local__Method · 0.45
__setattr__Method · 0.45
__delattr__Method · 0.45
__release_local__Method · 0.45
pushMethod · 0.45
popMethod · 0.45

Calls 3

_options_header_vkwFunction · 0.85
_str_header_valueFunction · 0.85
appendMethod · 0.80

Tested by 7

__init__Method · 0.36
reset_context_varsFunction · 0.36
test_proxy_wrappedFunction · 0.36
test_set_argumentsMethod · 0.36
test_reject_newlinesMethod · 0.36
test_slicingMethod · 0.36
test_to_wsgi_listMethod · 0.36