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

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

23 statements  

1from datetime import datetime 

2import os 

3import sys 

4from random import choice 

5from typing import Any 

6 

7_tips: Any = { 

8 # (month, day) 

9 "every_year": { 

10 (1, 1): "Happy new year!", 

11 # European time: 

12 # [2/8/25, 23:28:24] Fernando Perez: Hi! Yes, this was my first public 

13 # announcement: 

14 # [2/8/25, 23:28:25] Fernando Perez: 

15 # https://mail.python.org/pipermail/python-list/2001-December/093408.html 

16 # [2/8/25, 23:28:55] Fernando Perez: All that started two months earlier 

17 # - in October 2001 I read this article: 

18 # [2/8/25, 23:28:55] Fernando Perez: 

19 # https://web.archive.org/web/20011202000624/http://www.onlamp.com/pub/a/python/2001/10/11/pythonnews.html 

20 # [2/8/25, 23:29:05] Fernando Perez: Which is also archived here: 

21 # [2/8/25, 23:29:05] Fernando Perez: 

22 # https://docstore.mik.ua/orelly/weblinux2/orn/interactive_python.html 

23 # [2/8/25, 23:29:48] Fernando Perez: That article put me on the trail of 

24 # LazyPython, and I also found out (can’t remember where) about IPP 

25 # (Interactive Python Prompt), another similar projecd by Janko Hauser. 

26 # [2/8/25, 23:30:20] Fernando Perez: A also read an article around that 

27 # time, about new features in Python 2.0, which spoke of how 

28 # sys.displayhook could be programmed to call any object you wanted. 

29 # [2/8/25, 23:31:01] Fernando Perez: Those things together gave me the 

30 # idea of implementing a stateful object, the “IPython Prompt”, that 

31 # could store a cache of old results, Mathematica-style, and that could 

32 # have all kinds of other useful tricks up its sleeve. 

33 # [2/8/25, 23:31:17] Fernando Perez: I can’t remember if I did the 

34 # prompt stuff first and then read about LazyPython/IPP. 

35 # [2/8/25, 23:31:41] Fernando Perez: I do know that, implementation 

36 # wise, at first I just did the tiny IPython 0.0.1 that I posted much 

37 # later on github as a gist: 

38 # [2/8/25, 23:31:55] Fernando Perez: 

39 # https://gist.github.com/fperez/1579699 

40 # [2/8/25, 23:32:03] Fernando Perez: But I only shared that publicly 

41 # much later. 

42 # [2/8/25, 23:33:19] Fernando Perez: I did find out about IPP/LazyPython 

43 # sometime in October, contacted Janko and Nathan who told me to use 

44 # their code at will but said they were busy with other things, and got 

45 # cranking for a few mad weeks on what became the IPython 0.2.0 that I 

46 # posted about in that comp.lang.python thread of December 2001. 

47 # [2/8/25, 23:33:52] Fernando Perez: That period from Oct 11 to Dec 9 

48 # 2001 was maniacal coding, with very little sleep 🙂 

49 ( 

50 10, 

51 11, 

52 ): "IPython's first line of code was written {} years ago by Fernando Pérez".format( 

53 datetime.now().year - 2001 

54 ), 

55 ( 

56 12, 

57 9, 

58 ): "IPython 0.0.2 was announced {} years ago: https://mail.python.org/pipermail/python-list/2001-December/093408.html".format( 

59 datetime.now().year - 2001 

60 ), 

61 ( 

62 3, 

63 8, 

64 ): "Today is International Women's Day: https://www.internationalwomensday.com/", 

65 ( 

66 3, 

67 31, 

68 ): "Happy International Transgender Day of Visibility! You are valid. You matter. https://en.wikipedia.org/wiki/International_Transgender_Day_of_Visibility", 

69 }, 

70 "random": [ 

71 "Use `F2` or %edit with no arguments to open an empty editor with a temporary file.", 

72 "Run your doctests from within IPython for development and debugging. The special %doctest_mode command toggles a mode where the prompt, output and exceptions display matches as closely as possible that of the default Python interpreter.", 

73 "You can use `files = !ls *.png`", 

74 "Use the IPython.lib.demo.Demo class to load any Python script as an interactive demo.", 

75 "Put a ';' at the end of a line to suppress the printing of output.", 

76 "You can use Ctrl-O to force a new line in terminal IPython", 

77 "Use `object?` to see the help on `object`, `object??` to view its source", 

78 "`?` alone on a line will brings up IPython's help", 

79 "You can use `%hist` to view history, see the options with `%history?`", 

80 "You can change the editing mode of IPython to behave more like vi, or emacs.", 

81 "IPython 9.0+ has hooks to integrate AI/LLM completions.", 

82 "Use `%timeit` or `%%timeit`, and the `-r`, `-n`, and `-o` options to easily profile your code.", 

83 "Use `ipython --help-all | less` to view all the IPython configuration options.", 

84 "Use `--theme`, or the `%colors` magic to change IPython's themes and colors.", 

85 "The `%timeit` magic has a `-o` flag, which returns the results, making it easy to plot. See `%timeit?`.", 

86 ], 

87} 

88 

89if os.name == "nt": 

90 _tips["random"].extend( 

91 [ 

92 "We can't show you all tips on Windows as sometimes Unicode characters crash the Windows console, please help us debug it." 

93 ] 

94 ) 

95 # unicode may crash windows console, so we filter out tips with non-ASCII characters. 

96 _tips["every_year"] = { 

97 k: v 

98 for k, v in _tips["every_year"].items() 

99 if all(ord(char) < 128 for char in v) 

100 } 

101else: 

102 _tips["random"].extend( 

103 [ 

104 "You can use LaTeX or Unicode completion, `\\alpha<tab>` will insert the α symbol.", 

105 "You can find how to type a LaTeX symbol by back-completing it, eg `\\θ<tab>` will expand to `\\theta`.", 

106 "You can find how to type a Unicode symbol by back-completing it, eg `\\Ⅷ<tab>` will expand to `\\ROMAN NUMERAL EIGHT`.", 

107 "IPython supports combining unicode identifiers, eg F\\vec<tab> will become F⃗, useful for physics equations. Play with \\dot \\ddot and others.", 

108 ] 

109 ) 

110 

111if sys.version_info < (3, 12): 

112 _tips["random"].append( 

113 "IPython support for Python versions outside of SPEC-0 is funded by the D.E. Shaw group: https://deshaw.com" 

114 ) 

115 

116# Check if argcomplete is installed and add tip 

117try: 

118 import argcomplete 

119 

120 _tips["random"].append( 

121 "Run `activate-global-python-argcomplete` from your shell to enable CLI completion for IPython" 

122 ) 

123except ModuleNotFoundError: 

124 pass 

125 

126 

127def pick_tip() -> str: 

128 current_date = datetime.now() 

129 month, day = current_date.month, current_date.day 

130 

131 if (month, day) in _tips["every_year"]: 

132 return _tips["every_year"][(month, day)] 

133 

134 return choice(_tips["random"])