Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

#!/usr/bin/env python3 

 

class MagicFormatter: 

 

    def __init__(self, args=None, kwargs=None, level=1): 

        self.args = args or () 

        self.kwargs = kwargs or {} 

        self.level = level 

 

    def __call__(self, *args, **kwargs): 

        return self.__class__(args, kwargs, self.level) 

 

    def __ror__(self, operand): 

        import inspect 

 

        # Make sure the operand is a string. 

        if not isinstance(operand, str): 

            raise TypeError("'{}' is not a string", repr(operand)) 

 

        # Inspect variables from the source frame. 

        frame = inspect.stack()[self.level][0] 

 

        # Collect all the variables in the scope of the calling code, so they  

        # can be substituted into the message. 

        kwargs = {} 

        kwargs.update(frame.f_globals) 

        kwargs.update(frame.f_locals) 

        kwargs.update(self.kwargs) 

 

        return operand.format(*self.args, **kwargs) 

 

 

 

fmt = MagicFormatter()