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

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

24 statements  

1from __future__ import annotations 

2from datetime import datetime 

3import os 

4import sys 

5from random import choice 

6from typing import Any 

7 

8_tips: Any = { 

9 # (month, day) 

10 "every_year": { 

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

12 # European time: 

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

14 # announcement: 

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

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

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

18 # - in October 2001 I read this article: 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

38 # later on github as a gist: 

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

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

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

42 # much later. 

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

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

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

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

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

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

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

50 ( 

51 10, 

52 11, 

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

54 datetime.now().year - 2001 

55 ), 

56 ( 

57 12, 

58 9, 

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

60 datetime.now().year - 2001 

61 ), 

62 ( 

63 3, 

64 8, 

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

66 ( 

67 3, 

68 31, 

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

70 }, 

71 "random": [ 

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

73 "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.", 

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

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

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

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

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

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

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

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

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

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

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

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

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

87 ], 

88} 

89 

90if os.name == "nt": 

91 _tips["random"].extend( 

92 [ 

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

94 ] 

95 ) 

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

97 _tips["every_year"] = { 

98 k: v 

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

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

101 } 

102else: 

103 _tips["random"].extend( 

104 [ 

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

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

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

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

109 ] 

110 ) 

111 

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

113 _tips["random"].append( 

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

115 ) 

116 

117# Check if argcomplete is installed and add tip 

118try: 

119 import argcomplete 

120 

121 _tips["random"].append( 

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

123 ) 

124except ModuleNotFoundError: 

125 pass 

126 

127 

128def pick_tip() -> str: 

129 current_date = datetime.now() 

130 month, day = current_date.month, current_date.day 

131 

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

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

134 

135 return choice(_tips["random"])