##// END OF EJS Templates
Copy EvalFormatter and FullEvalFormatter from prompt-manager branch.
Thomas Kluyver -
Show More
@@ -597,12 +597,12 b' def wrap_paragraphs(text, ncols=80):'
597 return out_ps
597 return out_ps
598
598
599
599
600
601 class EvalFormatter(Formatter):
600 class EvalFormatter(Formatter):
602 """A String Formatter that allows evaluation of simple expressions.
601 """A String Formatter that allows evaluation of simple expressions.
603
602
604 Any time a format key is not found in the kwargs,
603 Note that this version interprets a : as specifying a format string (as per
605 it will be tried as an expression in the kwargs namespace.
604 standard string formatting), so if slicing is required, you must explicitly
605 create a slice.
606
606
607 This is to be used in templating cases, such as the parallel batch
607 This is to be used in templating cases, such as the parallel batch
608 script templates, where simple arithmetic on arguments is useful.
608 script templates, where simple arithmetic on arguments is useful.
@@ -614,16 +614,35 b' class EvalFormatter(Formatter):'
614 In [2]: f.format('{n//4}', n=8)
614 In [2]: f.format('{n//4}', n=8)
615 Out[2]: '2'
615 Out [2]: '2'
616
616
617 In [3]: f.format('{list(range(3))}')
617 In [3]: f.format("{greeting[slice(2,4)]}", greeting="Hello")
618 Out[3]: '[0, 1, 2]'
618 Out [3]: 'll'
619 """
620 def get_field(self, name, args, kwargs):
621 v = eval(name, kwargs)
622 return v, name
623
624 class FullEvalFormatter(Formatter):
625 """A String Formatter that allows evaluation of simple expressions.
626
627 Any time a format key is not found in the kwargs,
628 it will be tried as an expression in the kwargs namespace.
629
630 Note that this version allows slicing using [1:2], so you cannot specify
631 a format string. Use :class:`EvalFormatter` to permit format strings.
632
633 Examples
634 --------
635
636 In [1]: f = FullEvalFormatter()
637 In [2]: f.format('{n//4}', n=8)
638 Out[2]: '2'
639
640 In [3]: f.format('{list(range(5))[2:4]}')
641 Out[3]: '[2, 3]'
619
642
620 In [4]: f.format('{3*2}')
643 In [4]: f.format('{3*2}')
621 Out[4]: '6'
644 Out[4]: '6'
622 """
645 """
623
624 # should we allow slicing by disabling the format_spec feature?
625 allow_slicing = True
626
627 # copied from Formatter._vformat with minor changes to allow eval
646 # copied from Formatter._vformat with minor changes to allow eval
628 # and replace the format_spec code with slicing
647 # and replace the format_spec code with slicing
629 def _vformat(self, format_string, args, kwargs, used_args, recursion_depth):
648 def _vformat(self, format_string, args, kwargs, used_args, recursion_depth):
@@ -642,10 +661,9 b' class EvalFormatter(Formatter):'
642 # this is some markup, find the object and do
661 # this is some markup, find the object and do
643 # the formatting
662 # the formatting
644
663
645 if self.allow_slicing and format_spec:
664 if format_spec:
646 # override format spec, to allow slicing:
665 # override format spec, to allow slicing:
647 field_name = ':'.join([field_name, format_spec])
666 field_name = ':'.join([field_name, format_spec])
648 format_spec = ''
649
667
650 # eval the contents of the field for the object
668 # eval the contents of the field for the object
651 # to be formatted
669 # to be formatted
@@ -654,12 +672,8 b' class EvalFormatter(Formatter):'
654 # do any conversion on the resulting object
672 # do any conversion on the resulting object
655 obj = self.convert_field(obj, conversion)
673 obj = self.convert_field(obj, conversion)
656
674
657 # expand the format spec, if needed
658 format_spec = self._vformat(format_spec, args, kwargs,
659 used_args, recursion_depth-1)
660
661 # format the object and append to the result
675 # format the object and append to the result
662 result.append(self.format_field(obj, format_spec))
676 result.append(self.format_field(obj, ''))
663
677
664 return ''.join(result)
678 return ''.join(result)
665
679
General Comments 0
You need to be logged in to leave comments. Login now