Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/IPython/core/payload.py: 42%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

19 statements  

1"""Payload system for IPython. 

2 

3Authors: 

4 

5* Fernando Perez 

6* Brian Granger 

7""" 

8 

9#----------------------------------------------------------------------------- 

10# Copyright (C) 2008-2011 The IPython Development Team 

11# 

12# Distributed under the terms of the BSD License. The full license is in 

13# the file COPYING, distributed as part of this software. 

14#----------------------------------------------------------------------------- 

15 

16#----------------------------------------------------------------------------- 

17# Imports 

18#----------------------------------------------------------------------------- 

19 

20from traitlets.config.configurable import Configurable 

21from traitlets import List 

22 

23#----------------------------------------------------------------------------- 

24# Main payload class 

25#----------------------------------------------------------------------------- 

26 

27class PayloadManager(Configurable): 

28 _payload: List = List([]) 

29 

30 def write_payload(self, data, single=True): 

31 """Include or update the specified `data` payload in the PayloadManager. 

32 

33 If a previous payload with the same source exists and `single` is True, 

34 it will be overwritten with the new one. 

35 """ 

36 

37 if not isinstance(data, dict): 

38 raise TypeError('Each payload write must be a dict, got: %r' % data) 

39 

40 if single and 'source' in data: 

41 source = data['source'] 

42 for i, pl in enumerate(self._payload): 

43 if 'source' in pl and pl['source'] == source: 

44 self._payload[i] = data 

45 return 

46 

47 self._payload.append(data) 

48 

49 def read_payload(self): 

50 return self._payload 

51 

52 def clear_payload(self): 

53 self._payload = []