MCPcopy Create free account
hub / github.com/python-hyper/uritemplate / expand

Method expand

uritemplate/variable.py:453–531  ·  view source on GitHub ↗

Expand the variable in question. Using ``var_dict`` and the previously parsed defaults, expand this variable and subvariables. :param dict var_dict: dictionary of key-value pairs to be used during expansion :returns: dict(variable=value) Example

(
        self, var_dict: t.Optional[VariableValueDict] = None
    )

Source from the content-addressed store, hash-verified

451 return self.operator.quote(value)
452
453 def expand(
454 self, var_dict: t.Optional[VariableValueDict] = None
455 ) -> t.Mapping[str, str]:
456 """Expand the variable in question.
457
458 Using ``var_dict`` and the previously parsed defaults, expand this
459 variable and subvariables.
460
461 :param dict var_dict: dictionary of key-value pairs to be used during
462 expansion
463 :returns: dict(variable=value)
464
465 Examples::
466
467 # (1)
468 v = URIVariable('/var')
469 expansion = v.expand({'var': 'value'})
470 print(expansion)
471 # => {'/var': '/value'}
472
473 # (2)
474 v = URIVariable('?var,hello,x,y')
475 expansion = v.expand({'var': 'value', 'hello': 'Hello World!',
476 'x': '1024', 'y': '768'})
477 print(expansion)
478 # => {'?var,hello,x,y':
479 # '?var=value&hello=Hello%20World%21&x=1024&y=768'}
480
481 """
482 return_values = []
483 if var_dict is None:
484 return {self.original: self.original}
485
486 for name, opts in self.variables:
487 value = var_dict.get(name, None)
488 if not value and value != "" and name in self.defaults:
489 value = self.defaults[name]
490
491 if value is None:
492 continue
493
494 expanded = None
495 if (
496 self.operator == Operator.path_segment
497 or self.operator == Operator.label_with_dot_prefix
498 ):
499 expansion = self._label_path_expansion
500 elif (
501 self.operator == Operator.form_style_query
502 or self.operator == Operator.form_style_query_continuation
503 ):
504 expansion = self._query_expansion
505 elif self.operator == Operator.path_style_parameter:
506 expansion = self._semi_path_expansion
507 else:
508 expansion = self._string_expansion
509 # match self.operator:
510 # case Operator.path_segment | Operator.label_with_dot_prefix:

Callers 3

expandFunction · 0.45
test_expandMethod · 0.45
_testMethod · 0.45

Calls 2

variable_prefixMethod · 0.80
expansion_separatorMethod · 0.80

Tested by 2

test_expandMethod · 0.36
_testMethod · 0.36