<jupyter_start><jupyter_text>Tour of magics Magics are special functions that are useful in the context of interactive sessions. They can create or show files, recover commands from history etc. More on magics in the IPython docs. Before we dig into more magics. Some Python code for a warmup:<jupyter_code>print("Xeus(tm) inside!")
a = 2+2
print(a)
def foo(): pass<jupyter_output><empty_output><jupyter_text>Line magics Line magics start with a single percent (`%`) sign. For example, to show current directory.<jupyter_code>%pwd<jupyter_output><empty_output><jupyter_text>They can also take arguments:<jupyter_code>%cd ..<jupyter_output><empty_output><jupyter_text>and their result can be assigned to a variable:<jupyter_code>current_path = %pwd
print(current_path)<jupyter_output><empty_output><jupyter_text>Cell magics Cell magics start with double percent sign (`%%`). Like line magics they can take arguments, but also they use the content of the cell below:<jupyter_code>%%writefile test.txt

This will create a new file. 
With the text that you see here.<jupyter_output><empty_output><jupyter_text>Shell escape magics You can also call shell commands by prefixing them with an exclamation mark (`!`). For example, unix `cat` command displays content of a file.<jupyter_code>!cat test.txt<jupyter_output><empty_output><jupyter_text>User-defined magics You can also define your own (line or cell) magics:<jupyter_code>from IPython.core.magic import (
    register_line_magic, register_cell_magic)

@register_line_magic
def lmagic(line):
    return line

@register_cell_magic
def cmagic(line, body):
    return line + body
%lmagic me
%%cmagic me

hello<jupyter_output><empty_output><jupyter_text>Magic land You can also install and load magics from elsewhere:<jupyter_code>!pip install example_magic
%load_ext example_magic
%abra Helo<jupyter_output><empty_output><jupyter_text>Help with magic To show the list of available magics and some description, use `%magic` magic:<jupyter_code>%magic<jupyter_output><empty_output><jupyter_text>History magics  `%history` show all executed commands<jupyter_code>%history<jupyter_output><empty_output><jupyter_text>show last two commands<jupyter_code>%history -l 2<jupyter_output><empty_output><jupyter_text>search for commands containing print<jupyter_code>%history -g print<jupyter_output><empty_output><jupyter_text>`%recall` `%recall` magic inserts a new  cell with a command from history<jupyter_code>%recall 2
a = 2+2<jupyter_output><empty_output><jupyter_text>`%rerun` `%rerun` repeats the execution of the command<jupyter_code>%rerun 3<jupyter_output><empty_output><jupyter_text>Execution magics  `%timeit` `%timeit` measures execution time, it can be both line and cell magics<jupyter_code>import time
%%timeit 

time.sleep(0.1)
%timeit 2+2<jupyter_output><empty_output><jupyter_text>Quick help `?function` or `function?` shows docstring for a function or object. It can be at any line in the cell. In a classic notebook it opens a pager at the bottom of the window.<jupyter_code>print("hello world")
?print<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09026205111614365, 'fraction_numerical': 0.0035587188612099642, 'mean_word_length': 7.588888888888889, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '204577', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1051, 'n_tokens_neox': 1025, 'n_words': 305, 'path': 'notebooks/xeus-magics.ipynb', 'repo_name': 'chronitis/xeus-python', 'size': '3091'}