done MAT-204 and MAT-205
This commit is contained in:
parent
248cea8a6a
commit
46fe8de98e
|
@ -1,26 +1,47 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# MAT-204:00020 - Introduction to Probability"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Import"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 26,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"# read file\n",
|
||||
"# Read the .csv file\n",
|
||||
"Dat = pd.read_csv(\"DataLoL.csv\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Total Game played\n",
|
||||
"# Total amount of game played\n",
|
||||
"total_game = len(Dat['blueWins']) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Questions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
|
@ -30,35 +51,36 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 28,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Probability of team blue destroyed more structure than team red, if team blue won (using Dat) is 0.06795131845841784\n",
|
||||
"Probability of team blue destroyed more structure than team red, if team blue won (using dat_blue_wins) is 0.06795131845841786\n"
|
||||
"Probability of blue team destroying more structures than the red team AND if blue team won (via Dat): 0.06795131845841784\n",
|
||||
"Probability of blue team destroying more structures than red team, if blue team won (vat dat_blue_wins): 0.06795131845841786\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# calculate probability using Dat\n",
|
||||
"# Probability using the Dat dataset\n",
|
||||
"prob_blue_wins_more_structure = len(Dat[(Dat['blueWins'] == 1) & (Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed'])]) / len(Dat) # P(A ∩ B)\n",
|
||||
"prob_blue_wins = len(Dat[Dat['blueWins'] == 1]) / total_game #P(A)\n",
|
||||
"\n",
|
||||
"print(\"Probability of team blue destroyed more structure than team red, if team blue won (using Dat) is \", prob_blue_wins_more_structure / prob_blue_wins)\n",
|
||||
"print(\"Probability of blue team destroying more structures than the red team AND if blue team won (via Dat): \", prob_blue_wins_more_structure / prob_blue_wins)\n",
|
||||
"\n",
|
||||
"# calculate probability using dat_blue_wins\n",
|
||||
"# filter game team blue wins\n",
|
||||
"# Probability using dat_blue_wins\n",
|
||||
"# Filter the dataset to only the records where the blue team won.\n",
|
||||
"dat_blue_wins = Dat[Dat['blueWins'] == 1]\n",
|
||||
"\n",
|
||||
"# filter game team blue win and destroyed more strcuture\n",
|
||||
"dat_blue_wins_more_structure = dat_blue_wins[dat_blue_wins['blueTowersDestroyed'] > dat_blue_wins['redTowersDestroyed']] # (A ∩ B)\n",
|
||||
"# Filter the dataset to the records where the blue team won and destroyed more structured. \n",
|
||||
" # Essentially calculating the intersection of A & B or (A ∩ B)\n",
|
||||
"dat_blue_wins_more_structure = dat_blue_wins[dat_blue_wins['blueTowersDestroyed'] > dat_blue_wins['redTowersDestroyed']]\n",
|
||||
"\n",
|
||||
"#Verify using dat_blue_wins\n",
|
||||
"# Verify the result using the dat_blue_wins set. \n",
|
||||
"prob_blue_wins_more_structure = len(dat_blue_wins_more_structure) / len(dat_blue_wins) # P(B|A) = P(A ∩ B) / P(A)\n",
|
||||
"print(\"Probability of team blue destroyed more structure than team red, if team blue won (using dat_blue_wins) is\", prob_blue_wins_more_structure)\n",
|
||||
"print(\"Probability of blue team destroying more structures than red team, if blue team won (vat dat_blue_wins): \", prob_blue_wins_more_structure)\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
|
@ -71,37 +93,37 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Probability of team blue wins, kills the dragon, kills the heralds and does the first kill is (Using chain rule) 0.037858082801903024\n",
|
||||
"Probability of team blue wins, kills the dragon, kills the heralds and does the first kill is (Using new dat set) 0.037858082801903024\n"
|
||||
"[Chain Rule] Probability of blue team wins, kills the dragon, kills the heralds and does first kill: 0.037858082801903024\n",
|
||||
"[Dat set] Probability of blue team wins, kills the dragon, kills the heralds and does first kill: 0.037858082801903024\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# calcucate probability team blue wins\n",
|
||||
"# Probability that the blue team wins\n",
|
||||
"dat_blue_wins = Dat[Dat['blueWins'] == 1]\n",
|
||||
"prob_blue_win = len(dat_blue_wins) / total_game\n",
|
||||
"\n",
|
||||
"# calculate probability team blue wins, kills the dragon\n",
|
||||
"# Probability that blue team wins and also kills the dragon\n",
|
||||
"dat_blue_wins_and_kills_dragons = dat_blue_wins[dat_blue_wins['blueDragons'] == 1]\n",
|
||||
"prob_blue_wins_and_kills_dragons = len(dat_blue_wins_and_kills_dragons) / len(dat_blue_wins) * prob_blue_win\n",
|
||||
"\n",
|
||||
"# calculate probability team blue wins, kills the dragon, kills the heralds\n",
|
||||
"# Probability that blue team wins, kills the dragon and kills the heralds. \n",
|
||||
"dat_blue_wins_and_kills_dragons_and_heralds = dat_blue_wins_and_kills_dragons[dat_blue_wins_and_kills_dragons['blueHeralds'] == 1]\n",
|
||||
"prob_blue_wins_and_kills_dragons_and_heralds = len(dat_blue_wins_and_kills_dragons_and_heralds) / len(dat_blue_wins_and_kills_dragons) * prob_blue_wins_and_kills_dragons\n",
|
||||
"\n",
|
||||
"# calculate probability team blue wins, kills the dragon, kills the heralds and does the first kill\n",
|
||||
"# Probability that blue team wins, kills the dragon, kills the heralds and does first kill\n",
|
||||
"dat_blue_wins_and_kills_dragons_and_heralds_and_firstblood = dat_blue_wins_and_kills_dragons_and_heralds[dat_blue_wins_and_kills_dragons_and_heralds['blueFirstBlood'] == 1]\n",
|
||||
"prob_blue_wins_and_kills_dragons_and_heralds_and_firstblood = len(dat_blue_wins_and_kills_dragons_and_heralds_and_firstblood) / len(dat_blue_wins_and_kills_dragons_and_heralds) * prob_blue_wins_and_kills_dragons_and_heralds\n",
|
||||
"\n",
|
||||
"print(\"Probability of team blue wins, kills the dragon, kills the heralds and does the first kill is (Using chain rule)\", prob_blue_wins_and_kills_dragons_and_heralds_and_firstblood)\n",
|
||||
"print(\"Probability of team blue wins, kills the dragon, kills the heralds and does the first kill is (Using new dat set)\", len(dat_blue_wins_and_kills_dragons_and_heralds_and_firstblood) / total_game)\n"
|
||||
"print(\"[Chain Rule] Probability of blue team wins, kills the dragon, kills the heralds and does first kill:\", prob_blue_wins_and_kills_dragons_and_heralds_and_firstblood)\n",
|
||||
"print(\"[Dat set] Probability of blue team wins, kills the dragon, kills the heralds and does first kill:\", len(dat_blue_wins_and_kills_dragons_and_heralds_and_firstblood) / total_game)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -113,55 +135,54 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"Dat['blueQuantileGold'] = pd.qcut(Dat['blueTotalGold'], 4, labels=False)"
|
||||
"# Divide the dataset Dat['blueTotalGold'] into four quantiles. \n",
|
||||
"Dat['blueQuantileGold'] = pd.qcut(Dat['blueTotalGold'], 4, labels=False) \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": 31,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"P(A) calculated using Law of Total Probability = 0.4990383642069036\n",
|
||||
"P(A) is 0.4990383642069035\n"
|
||||
"[Law of Total Probability] P(A) calculated: 0.4990383642069036\n",
|
||||
"[Event / Sample Space] P(A): 0.4990383642069035\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# filter data to each quantiles\n",
|
||||
"# Declare each variable for each quantiles \n",
|
||||
"dat_quantile_1 = Dat[Dat['blueQuantileGold'] == 0]\n",
|
||||
"dat_quantile_2 = Dat[Dat['blueQuantileGold'] == 1]\n",
|
||||
"dat_quantile_3 = Dat[Dat['blueQuantileGold'] == 2]\n",
|
||||
"dat_quantile_4 = Dat[Dat['blueQuantileGold'] == 3]\n",
|
||||
"\n",
|
||||
"# calculate a proportion of game in each quantiles\n",
|
||||
"# Proportion of the games in each quantiles\n",
|
||||
"quantile_1_prob = len(dat_quantile_1) / total_game\n",
|
||||
"quantile_2_prob = len(dat_quantile_2) / total_game\n",
|
||||
"quantile_3_prob = len(dat_quantile_3) / total_game\n",
|
||||
"quantile_4_prob = len(dat_quantile_4) / total_game\n",
|
||||
"\n",
|
||||
"# calculate team blue win rate in each quantiles\n",
|
||||
"# Rate that blue team wins for each quantiles\n",
|
||||
"quantile_1_win_rate = len(dat_quantile_1[dat_quantile_1['blueWins'] == 1]) / len(dat_quantile_1)\n",
|
||||
"quantile_2_win_rate = len(dat_quantile_2[dat_quantile_2['blueWins'] == 1]) / len(dat_quantile_2)\n",
|
||||
"quantile_3_win_rate = len(dat_quantile_3[dat_quantile_3['blueWins'] == 1]) / len(dat_quantile_3)\n",
|
||||
"quantile_4_win_rate = len(dat_quantile_4[dat_quantile_4['blueWins'] == 1]) / len(dat_quantile_4)\n",
|
||||
"\n",
|
||||
"#calculate probability team blue wins using law of total probability\n",
|
||||
"# Probability blue team wins via the Law of total probability\n",
|
||||
"prob_a = (quantile_1_prob * quantile_1_win_rate) + (quantile_2_prob * quantile_2_win_rate) + (quantile_3_prob * quantile_3_win_rate) + (quantile_4_prob * quantile_4_win_rate)\n",
|
||||
"print(\"P(A) calculated using Law of Total Probability = \", prob_a)\n",
|
||||
"print(\"[Law of Total Probability] P(A) calculated:\", prob_a)\n",
|
||||
"\n",
|
||||
"#Verify\n",
|
||||
"prob_blue_wins = len(Dat[Dat['blueWins'] == 1]) / total_game # calculate probability (event/sample space)\n",
|
||||
"print(\"P(A) is\", prob_blue_wins)\n",
|
||||
"\n",
|
||||
"\n"
|
||||
"# Verify the result using the probability calculated via event / sample space. \n",
|
||||
"prob_blue_wins = len(Dat[Dat['blueWins'] == 1]) / total_game\n",
|
||||
"print(\"[Event / Sample Space] P(A):\", prob_blue_wins)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -173,48 +194,46 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 32,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Probability of team blue wins given that it destroyed more structure than team red (using Bayes's theorem) is 0.7596371882086167\n",
|
||||
"Probability of team blue wins given that it destroyed more structure than team red (using Dat) is 0.7596371882086167\n"
|
||||
"[Bayes's Theorem] Probability of blue team wins given that it destroys more structures than the red team: 0.7596371882086167\n",
|
||||
"[P(A|B) using Dat] Probability of blue team winning given that it destroys more structures than the red team: 0.7596371882086167\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# calculate probability used in Bayes's theorem\n",
|
||||
"# Probability via Bayes's Theorem\n",
|
||||
"\n",
|
||||
"# P(A)\n",
|
||||
"# P(A) or the Probability that blue team wins.\n",
|
||||
"prob_blue_wins = len(Dat[Dat['blueWins'] == 1]) / total_game\n",
|
||||
"\n",
|
||||
"# P(B)\n",
|
||||
"# P(B) or the Probability that blue team destroys more structures than the red team. \n",
|
||||
"prob_blue_more_structure = len(Dat[Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed']]) / total_game\n",
|
||||
"\n",
|
||||
"# P(A ∩ B)\n",
|
||||
"# P(A ∩ B) or the Probability that blue team wins and also destroys more structures than the red team. \n",
|
||||
"dat_blue_more_structure = Dat[Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed']]\n",
|
||||
"prob_blue_wins_more_structure = len(dat_blue_more_structure[dat_blue_more_structure['blueWins'] == 1]) / total_game\n",
|
||||
"\n",
|
||||
"# P(B|A)\n",
|
||||
"prob_blue_more_structure_if_blue_wins = prob_blue_wins_more_structure / prob_blue_wins\n",
|
||||
"\n",
|
||||
"# calculate P(A|B) using Bayes's theorem\n",
|
||||
"print(\"Probability of team blue wins given that it destroyed more structure than team red (using Bayes's theorem) is \", prob_blue_more_structure_if_blue_wins * prob_blue_wins / prob_blue_more_structure)\n",
|
||||
"# P(A|B) using Bayes's theorem\n",
|
||||
"print(\"[Bayes's Theorem] Probability of blue team wins given that it destroys more structures than the red team:\", prob_blue_more_structure_if_blue_wins * prob_blue_wins / prob_blue_more_structure)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"#Verify using Dat\n",
|
||||
"\n",
|
||||
"# P(A ∩ B)\n",
|
||||
"# Verify using the Dat dataset\n",
|
||||
"# P(A ∩ B) (same events as above)\n",
|
||||
"prob_blue_wins_more_structure = len(Dat[(Dat['blueWins'] == 1) & (Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed'])]) / total_game \n",
|
||||
"\n",
|
||||
"# P(B)\n",
|
||||
"# P(B) (same events as above)\n",
|
||||
"prob_blue_more_structure = len(Dat[Dat['blueTowersDestroyed'] > Dat['redTowersDestroyed']]) / total_game\n",
|
||||
"\n",
|
||||
"# calculate P(A|B) using Dat\n",
|
||||
"print(\"Probability of team blue wins given that it destroyed more structure than team red (using Dat) is \", prob_blue_wins_more_structure / prob_blue_more_structure)\n",
|
||||
"print(\"[P(A|B) using Dat] Probability of blue team winning given that it destroys more structures than the red team:\", prob_blue_wins_more_structure / prob_blue_more_structure)\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
|
@ -222,7 +241,7 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"display_name": "env",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
|
@ -236,7 +255,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.6"
|
||||
"version": "3.12.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,247 @@
|
|||
<#
|
||||
.Synopsis
|
||||
Activate a Python virtual environment for the current PowerShell session.
|
||||
|
||||
.Description
|
||||
Pushes the python executable for a virtual environment to the front of the
|
||||
$Env:PATH environment variable and sets the prompt to signify that you are
|
||||
in a Python virtual environment. Makes use of the command line switches as
|
||||
well as the `pyvenv.cfg` file values present in the virtual environment.
|
||||
|
||||
.Parameter VenvDir
|
||||
Path to the directory that contains the virtual environment to activate. The
|
||||
default value for this is the parent of the directory that the Activate.ps1
|
||||
script is located within.
|
||||
|
||||
.Parameter Prompt
|
||||
The prompt prefix to display when this virtual environment is activated. By
|
||||
default, this prompt is the name of the virtual environment folder (VenvDir)
|
||||
surrounded by parentheses and followed by a single space (ie. '(.venv) ').
|
||||
|
||||
.Example
|
||||
Activate.ps1
|
||||
Activates the Python virtual environment that contains the Activate.ps1 script.
|
||||
|
||||
.Example
|
||||
Activate.ps1 -Verbose
|
||||
Activates the Python virtual environment that contains the Activate.ps1 script,
|
||||
and shows extra information about the activation as it executes.
|
||||
|
||||
.Example
|
||||
Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv
|
||||
Activates the Python virtual environment located in the specified location.
|
||||
|
||||
.Example
|
||||
Activate.ps1 -Prompt "MyPython"
|
||||
Activates the Python virtual environment that contains the Activate.ps1 script,
|
||||
and prefixes the current prompt with the specified string (surrounded in
|
||||
parentheses) while the virtual environment is active.
|
||||
|
||||
.Notes
|
||||
On Windows, it may be required to enable this Activate.ps1 script by setting the
|
||||
execution policy for the user. You can do this by issuing the following PowerShell
|
||||
command:
|
||||
|
||||
PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
|
||||
|
||||
For more information on Execution Policies:
|
||||
https://go.microsoft.com/fwlink/?LinkID=135170
|
||||
|
||||
#>
|
||||
Param(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[String]
|
||||
$VenvDir,
|
||||
[Parameter(Mandatory = $false)]
|
||||
[String]
|
||||
$Prompt
|
||||
)
|
||||
|
||||
<# Function declarations --------------------------------------------------- #>
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Remove all shell session elements added by the Activate script, including the
|
||||
addition of the virtual environment's Python executable from the beginning of
|
||||
the PATH variable.
|
||||
|
||||
.Parameter NonDestructive
|
||||
If present, do not remove this function from the global namespace for the
|
||||
session.
|
||||
|
||||
#>
|
||||
function global:deactivate ([switch]$NonDestructive) {
|
||||
# Revert to original values
|
||||
|
||||
# The prior prompt:
|
||||
if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) {
|
||||
Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt
|
||||
Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT
|
||||
}
|
||||
|
||||
# The prior PYTHONHOME:
|
||||
if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) {
|
||||
Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME
|
||||
Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME
|
||||
}
|
||||
|
||||
# The prior PATH:
|
||||
if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) {
|
||||
Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH
|
||||
Remove-Item -Path Env:_OLD_VIRTUAL_PATH
|
||||
}
|
||||
|
||||
# Just remove the VIRTUAL_ENV altogether:
|
||||
if (Test-Path -Path Env:VIRTUAL_ENV) {
|
||||
Remove-Item -Path env:VIRTUAL_ENV
|
||||
}
|
||||
|
||||
# Just remove VIRTUAL_ENV_PROMPT altogether.
|
||||
if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) {
|
||||
Remove-Item -Path env:VIRTUAL_ENV_PROMPT
|
||||
}
|
||||
|
||||
# Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether:
|
||||
if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) {
|
||||
Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force
|
||||
}
|
||||
|
||||
# Leave deactivate function in the global namespace if requested:
|
||||
if (-not $NonDestructive) {
|
||||
Remove-Item -Path function:deactivate
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Description
|
||||
Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the
|
||||
given folder, and returns them in a map.
|
||||
|
||||
For each line in the pyvenv.cfg file, if that line can be parsed into exactly
|
||||
two strings separated by `=` (with any amount of whitespace surrounding the =)
|
||||
then it is considered a `key = value` line. The left hand string is the key,
|
||||
the right hand is the value.
|
||||
|
||||
If the value starts with a `'` or a `"` then the first and last character is
|
||||
stripped from the value before being captured.
|
||||
|
||||
.Parameter ConfigDir
|
||||
Path to the directory that contains the `pyvenv.cfg` file.
|
||||
#>
|
||||
function Get-PyVenvConfig(
|
||||
[String]
|
||||
$ConfigDir
|
||||
) {
|
||||
Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg"
|
||||
|
||||
# Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue).
|
||||
$pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue
|
||||
|
||||
# An empty map will be returned if no config file is found.
|
||||
$pyvenvConfig = @{ }
|
||||
|
||||
if ($pyvenvConfigPath) {
|
||||
|
||||
Write-Verbose "File exists, parse `key = value` lines"
|
||||
$pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath
|
||||
|
||||
$pyvenvConfigContent | ForEach-Object {
|
||||
$keyval = $PSItem -split "\s*=\s*", 2
|
||||
if ($keyval[0] -and $keyval[1]) {
|
||||
$val = $keyval[1]
|
||||
|
||||
# Remove extraneous quotations around a string value.
|
||||
if ("'""".Contains($val.Substring(0, 1))) {
|
||||
$val = $val.Substring(1, $val.Length - 2)
|
||||
}
|
||||
|
||||
$pyvenvConfig[$keyval[0]] = $val
|
||||
Write-Verbose "Adding Key: '$($keyval[0])'='$val'"
|
||||
}
|
||||
}
|
||||
}
|
||||
return $pyvenvConfig
|
||||
}
|
||||
|
||||
|
||||
<# Begin Activate script --------------------------------------------------- #>
|
||||
|
||||
# Determine the containing directory of this script
|
||||
$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
|
||||
$VenvExecDir = Get-Item -Path $VenvExecPath
|
||||
|
||||
Write-Verbose "Activation script is located in path: '$VenvExecPath'"
|
||||
Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)"
|
||||
Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)"
|
||||
|
||||
# Set values required in priority: CmdLine, ConfigFile, Default
|
||||
# First, get the location of the virtual environment, it might not be
|
||||
# VenvExecDir if specified on the command line.
|
||||
if ($VenvDir) {
|
||||
Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values"
|
||||
}
|
||||
else {
|
||||
Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir."
|
||||
$VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/")
|
||||
Write-Verbose "VenvDir=$VenvDir"
|
||||
}
|
||||
|
||||
# Next, read the `pyvenv.cfg` file to determine any required value such
|
||||
# as `prompt`.
|
||||
$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir
|
||||
|
||||
# Next, set the prompt from the command line, or the config file, or
|
||||
# just use the name of the virtual environment folder.
|
||||
if ($Prompt) {
|
||||
Write-Verbose "Prompt specified as argument, using '$Prompt'"
|
||||
}
|
||||
else {
|
||||
Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value"
|
||||
if ($pyvenvCfg -and $pyvenvCfg['prompt']) {
|
||||
Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'"
|
||||
$Prompt = $pyvenvCfg['prompt'];
|
||||
}
|
||||
else {
|
||||
Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)"
|
||||
Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'"
|
||||
$Prompt = Split-Path -Path $venvDir -Leaf
|
||||
}
|
||||
}
|
||||
|
||||
Write-Verbose "Prompt = '$Prompt'"
|
||||
Write-Verbose "VenvDir='$VenvDir'"
|
||||
|
||||
# Deactivate any currently active virtual environment, but leave the
|
||||
# deactivate function in place.
|
||||
deactivate -nondestructive
|
||||
|
||||
# Now set the environment variable VIRTUAL_ENV, used by many tools to determine
|
||||
# that there is an activated venv.
|
||||
$env:VIRTUAL_ENV = $VenvDir
|
||||
|
||||
if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) {
|
||||
|
||||
Write-Verbose "Setting prompt to '$Prompt'"
|
||||
|
||||
# Set the prompt to include the env name
|
||||
# Make sure _OLD_VIRTUAL_PROMPT is global
|
||||
function global:_OLD_VIRTUAL_PROMPT { "" }
|
||||
Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT
|
||||
New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt
|
||||
|
||||
function global:prompt {
|
||||
Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) "
|
||||
_OLD_VIRTUAL_PROMPT
|
||||
}
|
||||
$env:VIRTUAL_ENV_PROMPT = $Prompt
|
||||
}
|
||||
|
||||
# Clear PYTHONHOME
|
||||
if (Test-Path -Path Env:PYTHONHOME) {
|
||||
Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME
|
||||
Remove-Item -Path Env:PYTHONHOME
|
||||
}
|
||||
|
||||
# Add the venv to the PATH
|
||||
Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH
|
||||
$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH"
|
|
@ -0,0 +1,70 @@
|
|||
# This file must be used with "source bin/activate" *from bash*
|
||||
# You cannot run it directly
|
||||
|
||||
deactivate () {
|
||||
# reset old environment variables
|
||||
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
|
||||
PATH="${_OLD_VIRTUAL_PATH:-}"
|
||||
export PATH
|
||||
unset _OLD_VIRTUAL_PATH
|
||||
fi
|
||||
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
|
||||
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
|
||||
export PYTHONHOME
|
||||
unset _OLD_VIRTUAL_PYTHONHOME
|
||||
fi
|
||||
|
||||
# Call hash to forget past commands. Without forgetting
|
||||
# past commands the $PATH changes we made may not be respected
|
||||
hash -r 2> /dev/null
|
||||
|
||||
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
|
||||
PS1="${_OLD_VIRTUAL_PS1:-}"
|
||||
export PS1
|
||||
unset _OLD_VIRTUAL_PS1
|
||||
fi
|
||||
|
||||
unset VIRTUAL_ENV
|
||||
unset VIRTUAL_ENV_PROMPT
|
||||
if [ ! "${1:-}" = "nondestructive" ] ; then
|
||||
# Self destruct!
|
||||
unset -f deactivate
|
||||
fi
|
||||
}
|
||||
|
||||
# unset irrelevant variables
|
||||
deactivate nondestructive
|
||||
|
||||
# on Windows, a path can contain colons and backslashes and has to be converted:
|
||||
if [ "${OSTYPE:-}" = "cygwin" ] || [ "${OSTYPE:-}" = "msys" ] ; then
|
||||
# transform D:\path\to\venv to /d/path/to/venv on MSYS
|
||||
# and to /cygdrive/d/path/to/venv on Cygwin
|
||||
export VIRTUAL_ENV=$(cygpath "/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env")
|
||||
else
|
||||
# use the path as-is
|
||||
export VIRTUAL_ENV="/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env"
|
||||
fi
|
||||
|
||||
_OLD_VIRTUAL_PATH="$PATH"
|
||||
PATH="$VIRTUAL_ENV/bin:$PATH"
|
||||
export PATH
|
||||
|
||||
# unset PYTHONHOME if set
|
||||
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
|
||||
# could use `if (set -u; : $PYTHONHOME) ;` in bash
|
||||
if [ -n "${PYTHONHOME:-}" ] ; then
|
||||
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
|
||||
unset PYTHONHOME
|
||||
fi
|
||||
|
||||
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
|
||||
_OLD_VIRTUAL_PS1="${PS1:-}"
|
||||
PS1="(env) ${PS1:-}"
|
||||
export PS1
|
||||
VIRTUAL_ENV_PROMPT="(env) "
|
||||
export VIRTUAL_ENV_PROMPT
|
||||
fi
|
||||
|
||||
# Call hash to forget past commands. Without forgetting
|
||||
# past commands the $PATH changes we made may not be respected
|
||||
hash -r 2> /dev/null
|
|
@ -0,0 +1,27 @@
|
|||
# This file must be used with "source bin/activate.csh" *from csh*.
|
||||
# You cannot run it directly.
|
||||
|
||||
# Created by Davide Di Blasi <davidedb@gmail.com>.
|
||||
# Ported to Python 3.3 venv by Andrew Svetlov <andrew.svetlov@gmail.com>
|
||||
|
||||
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate'
|
||||
|
||||
# Unset irrelevant variables.
|
||||
deactivate nondestructive
|
||||
|
||||
setenv VIRTUAL_ENV "/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env"
|
||||
|
||||
set _OLD_VIRTUAL_PATH="$PATH"
|
||||
setenv PATH "$VIRTUAL_ENV/bin:$PATH"
|
||||
|
||||
|
||||
set _OLD_VIRTUAL_PROMPT="$prompt"
|
||||
|
||||
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
|
||||
set prompt = "(env) $prompt"
|
||||
setenv VIRTUAL_ENV_PROMPT "(env) "
|
||||
endif
|
||||
|
||||
alias pydoc python -m pydoc
|
||||
|
||||
rehash
|
|
@ -0,0 +1,69 @@
|
|||
# This file must be used with "source <venv>/bin/activate.fish" *from fish*
|
||||
# (https://fishshell.com/). You cannot run it directly.
|
||||
|
||||
function deactivate -d "Exit virtual environment and return to normal shell environment"
|
||||
# reset old environment variables
|
||||
if test -n "$_OLD_VIRTUAL_PATH"
|
||||
set -gx PATH $_OLD_VIRTUAL_PATH
|
||||
set -e _OLD_VIRTUAL_PATH
|
||||
end
|
||||
if test -n "$_OLD_VIRTUAL_PYTHONHOME"
|
||||
set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME
|
||||
set -e _OLD_VIRTUAL_PYTHONHOME
|
||||
end
|
||||
|
||||
if test -n "$_OLD_FISH_PROMPT_OVERRIDE"
|
||||
set -e _OLD_FISH_PROMPT_OVERRIDE
|
||||
# prevents error when using nested fish instances (Issue #93858)
|
||||
if functions -q _old_fish_prompt
|
||||
functions -e fish_prompt
|
||||
functions -c _old_fish_prompt fish_prompt
|
||||
functions -e _old_fish_prompt
|
||||
end
|
||||
end
|
||||
|
||||
set -e VIRTUAL_ENV
|
||||
set -e VIRTUAL_ENV_PROMPT
|
||||
if test "$argv[1]" != "nondestructive"
|
||||
# Self-destruct!
|
||||
functions -e deactivate
|
||||
end
|
||||
end
|
||||
|
||||
# Unset irrelevant variables.
|
||||
deactivate nondestructive
|
||||
|
||||
set -gx VIRTUAL_ENV "/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env"
|
||||
|
||||
set -gx _OLD_VIRTUAL_PATH $PATH
|
||||
set -gx PATH "$VIRTUAL_ENV/bin" $PATH
|
||||
|
||||
# Unset PYTHONHOME if set.
|
||||
if set -q PYTHONHOME
|
||||
set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME
|
||||
set -e PYTHONHOME
|
||||
end
|
||||
|
||||
if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
|
||||
# fish uses a function instead of an env var to generate the prompt.
|
||||
|
||||
# Save the current fish_prompt function as the function _old_fish_prompt.
|
||||
functions -c fish_prompt _old_fish_prompt
|
||||
|
||||
# With the original prompt function renamed, we can override with our own.
|
||||
function fish_prompt
|
||||
# Save the return status of the last command.
|
||||
set -l old_status $status
|
||||
|
||||
# Output the venv prompt; color taken from the blue of the Python logo.
|
||||
printf "%s%s%s" (set_color 4B8BBE) "(env) " (set_color normal)
|
||||
|
||||
# Restore the return status of the previous command.
|
||||
echo "exit $old_status" | .
|
||||
# Output the original/"old" prompt.
|
||||
_old_fish_prompt
|
||||
end
|
||||
|
||||
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
|
||||
set -gx VIRTUAL_ENV_PROMPT "(env) "
|
||||
end
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from debugpy.server.cli import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from numpy.f2py.f2py2e import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from IPython import start_ipython
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(start_ipython())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from IPython import start_ipython
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(start_ipython())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from jupyter_core.command import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from jupyter_client.kernelapp import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from jupyter_client.kernelspecapp import KernelSpecApp
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(KernelSpecApp.launch_instance())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from jupyter_core.migrate import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from jupyter_client.runapp import RunApp
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(RunApp.launch_instance())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from jupyter_core.troubleshoot import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from numpy._configtool import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from pip._internal.cli.main import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from pip._internal.cli.main import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from pip._internal.cli.main import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/winsdominoes/Workspace/cmkl_assignments/fall-2024/math/mat-204/00020/env/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from pygments.cmdline import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -0,0 +1 @@
|
|||
/usr/bin/python
|
|
@ -0,0 +1 @@
|
|||
python
|
|
@ -0,0 +1 @@
|
|||
python
|
163
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/__init__.py
vendored
Normal file
163
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/__init__.py
vendored
Normal file
|
@ -0,0 +1,163 @@
|
|||
# PYTHON_ARGCOMPLETE_OK
|
||||
"""
|
||||
IPython: tools for interactive and parallel computing in Python.
|
||||
|
||||
https://ipython.org
|
||||
"""
|
||||
#-----------------------------------------------------------------------------
|
||||
# Copyright (c) 2008-2011, IPython Development Team.
|
||||
# Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu>
|
||||
# Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>
|
||||
# Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
|
||||
#
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
#
|
||||
# The full license is in the file COPYING.txt, distributed with this software.
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Imports
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
import sys
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Setup everything
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
# Don't forget to also update setup.py when this changes!
|
||||
if sys.version_info < (3, 10):
|
||||
raise ImportError(
|
||||
"""
|
||||
IPython 8.19+ supports Python 3.10 and above, following SPEC0.
|
||||
IPython 8.13+ supports Python 3.9 and above, following NEP 29.
|
||||
IPython 8.0-8.12 supports Python 3.8 and above, following NEP 29.
|
||||
When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
|
||||
Python 3.3 and 3.4 were supported up to IPython 6.x.
|
||||
Python 3.5 was supported with IPython 7.0 to 7.9.
|
||||
Python 3.6 was supported with IPython up to 7.16.
|
||||
Python 3.7 was still supported with the 7.x branch.
|
||||
|
||||
See IPython `README.rst` file for more information:
|
||||
|
||||
https://github.com/ipython/ipython/blob/main/README.rst
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Setup the top level names
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
from .core.getipython import get_ipython
|
||||
from .core import release
|
||||
from .core.application import Application
|
||||
from .terminal.embed import embed
|
||||
|
||||
from .core.interactiveshell import InteractiveShell
|
||||
from .utils.sysinfo import sys_info
|
||||
from .utils.frame import extract_module_locals
|
||||
|
||||
__all__ = ["start_ipython", "embed", "start_kernel", "embed_kernel"]
|
||||
|
||||
# Release data
|
||||
__author__ = '%s <%s>' % (release.author, release.author_email)
|
||||
__license__ = release.license
|
||||
__version__ = release.version
|
||||
version_info = release.version_info
|
||||
# list of CVEs that should have been patched in this release.
|
||||
# this is informational and should not be relied upon.
|
||||
__patched_cves__ = {"CVE-2022-21699", "CVE-2023-24816"}
|
||||
|
||||
|
||||
def embed_kernel(module=None, local_ns=None, **kwargs):
|
||||
"""Embed and start an IPython kernel in a given scope.
|
||||
|
||||
If you don't want the kernel to initialize the namespace
|
||||
from the scope of the surrounding function,
|
||||
and/or you want to load full IPython configuration,
|
||||
you probably want `IPython.start_kernel()` instead.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
module : types.ModuleType, optional
|
||||
The module to load into IPython globals (default: caller)
|
||||
local_ns : dict, optional
|
||||
The namespace to load into IPython user namespace (default: caller)
|
||||
**kwargs : various, optional
|
||||
Further keyword args are relayed to the IPKernelApp constructor,
|
||||
such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
|
||||
allowing configuration of the kernel (see :ref:`kernel_options`). Will only have an effect
|
||||
on the first embed_kernel call for a given process.
|
||||
"""
|
||||
|
||||
(caller_module, caller_locals) = extract_module_locals(1)
|
||||
if module is None:
|
||||
module = caller_module
|
||||
if local_ns is None:
|
||||
local_ns = caller_locals
|
||||
|
||||
# Only import .zmq when we really need it
|
||||
from ipykernel.embed import embed_kernel as real_embed_kernel
|
||||
real_embed_kernel(module=module, local_ns=local_ns, **kwargs)
|
||||
|
||||
def start_ipython(argv=None, **kwargs):
|
||||
"""Launch a normal IPython instance (as opposed to embedded)
|
||||
|
||||
`IPython.embed()` puts a shell in a particular calling scope,
|
||||
such as a function or method for debugging purposes,
|
||||
which is often not desirable.
|
||||
|
||||
`start_ipython()` does full, regular IPython initialization,
|
||||
including loading startup files, configuration, etc.
|
||||
much of which is skipped by `embed()`.
|
||||
|
||||
This is a public API method, and will survive implementation changes.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
argv : list or None, optional
|
||||
If unspecified or None, IPython will parse command-line options from sys.argv.
|
||||
To prevent any command-line parsing, pass an empty list: `argv=[]`.
|
||||
user_ns : dict, optional
|
||||
specify this dictionary to initialize the IPython user namespace with particular values.
|
||||
**kwargs : various, optional
|
||||
Any other kwargs will be passed to the Application constructor,
|
||||
such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
|
||||
allowing configuration of the instance (see :ref:`terminal_options`).
|
||||
"""
|
||||
from IPython.terminal.ipapp import launch_new_instance
|
||||
return launch_new_instance(argv=argv, **kwargs)
|
||||
|
||||
def start_kernel(argv=None, **kwargs):
|
||||
"""Launch a normal IPython kernel instance (as opposed to embedded)
|
||||
|
||||
`IPython.embed_kernel()` puts a shell in a particular calling scope,
|
||||
such as a function or method for debugging purposes,
|
||||
which is often not desirable.
|
||||
|
||||
`start_kernel()` does full, regular IPython initialization,
|
||||
including loading startup files, configuration, etc.
|
||||
much of which is skipped by `embed_kernel()`.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
argv : list or None, optional
|
||||
If unspecified or None, IPython will parse command-line options from sys.argv.
|
||||
To prevent any command-line parsing, pass an empty list: `argv=[]`.
|
||||
user_ns : dict, optional
|
||||
specify this dictionary to initialize the IPython user namespace with particular values.
|
||||
**kwargs : various, optional
|
||||
Any other kwargs will be passed to the Application constructor,
|
||||
such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
|
||||
allowing configuration of the kernel (see :ref:`kernel_options`).
|
||||
"""
|
||||
import warnings
|
||||
|
||||
warnings.warn(
|
||||
"start_kernel is deprecated since IPython 8.0, use from `ipykernel.kernelapp.launch_new_instance`",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
from ipykernel.kernelapp import launch_new_instance
|
||||
return launch_new_instance(argv=argv, **kwargs)
|
15
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/__main__.py
vendored
Normal file
15
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/__main__.py
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
# PYTHON_ARGCOMPLETE_OK
|
||||
# encoding: utf-8
|
||||
"""Terminal-based IPython entry point.
|
||||
"""
|
||||
# -----------------------------------------------------------------------------
|
||||
# Copyright (c) 2012, IPython Development Team.
|
||||
#
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
#
|
||||
# The full license is in the file COPYING.txt, distributed with this software.
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
from IPython import start_ipython
|
||||
|
||||
start_ipython()
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
87
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/conftest.py
vendored
Normal file
87
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/conftest.py
vendored
Normal file
|
@ -0,0 +1,87 @@
|
|||
import builtins
|
||||
import inspect
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
import sys
|
||||
import types
|
||||
|
||||
import pytest
|
||||
|
||||
# Must register before it gets imported
|
||||
pytest.register_assert_rewrite("IPython.testing.tools")
|
||||
|
||||
from .testing import tools
|
||||
|
||||
|
||||
def pytest_collection_modifyitems(items):
|
||||
"""This function is automatically run by pytest passing all collected test
|
||||
functions.
|
||||
|
||||
We use it to add asyncio marker to all async tests and assert we don't use
|
||||
test functions that are async generators which wouldn't make sense.
|
||||
"""
|
||||
for item in items:
|
||||
if inspect.iscoroutinefunction(item.obj):
|
||||
item.add_marker("asyncio")
|
||||
assert not inspect.isasyncgenfunction(item.obj)
|
||||
|
||||
|
||||
def get_ipython():
|
||||
from .terminal.interactiveshell import TerminalInteractiveShell
|
||||
if TerminalInteractiveShell._instance:
|
||||
return TerminalInteractiveShell.instance()
|
||||
|
||||
config = tools.default_config()
|
||||
config.TerminalInteractiveShell.simple_prompt = True
|
||||
|
||||
# Create and initialize our test-friendly IPython instance.
|
||||
shell = TerminalInteractiveShell.instance(config=config)
|
||||
return shell
|
||||
|
||||
|
||||
@pytest.fixture(scope='session', autouse=True)
|
||||
def work_path():
|
||||
path = pathlib.Path("./tmp-ipython-pytest-profiledir")
|
||||
os.environ["IPYTHONDIR"] = str(path.absolute())
|
||||
if path.exists():
|
||||
raise ValueError('IPython dir temporary path already exists ! Did previous test run exit successfully ?')
|
||||
path.mkdir()
|
||||
yield
|
||||
shutil.rmtree(str(path.resolve()))
|
||||
|
||||
|
||||
def nopage(strng, start=0, screen_lines=0, pager_cmd=None):
|
||||
if isinstance(strng, dict):
|
||||
strng = strng.get("text/plain", "")
|
||||
print(strng)
|
||||
|
||||
|
||||
def xsys(self, cmd):
|
||||
"""Replace the default system call with a capturing one for doctest.
|
||||
"""
|
||||
# We use getoutput, but we need to strip it because pexpect captures
|
||||
# the trailing newline differently from commands.getoutput
|
||||
print(self.getoutput(cmd, split=False, depth=1).rstrip(), end="", file=sys.stdout)
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
# for things to work correctly we would need this as a session fixture;
|
||||
# unfortunately this will fail on some test that get executed as _collection_
|
||||
# time (before the fixture run), in particular parametrized test that contain
|
||||
# yields. so for now execute at import time.
|
||||
#@pytest.fixture(autouse=True, scope='session')
|
||||
def inject():
|
||||
|
||||
builtins.get_ipython = get_ipython
|
||||
builtins._ip = get_ipython()
|
||||
builtins.ip = get_ipython()
|
||||
builtins.ip.system = types.MethodType(xsys, ip)
|
||||
builtins.ip.builtin_trap.activate()
|
||||
from .core import page
|
||||
|
||||
page.pager_page = nopage
|
||||
# yield
|
||||
|
||||
|
||||
inject()
|
12
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/consoleapp.py
vendored
Normal file
12
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/consoleapp.py
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
"""
|
||||
Shim to maintain backwards compatibility with old IPython.consoleapp imports.
|
||||
"""
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
from warnings import warn
|
||||
|
||||
warn("The `IPython.consoleapp` package has been deprecated since IPython 4.0."
|
||||
"You should import from jupyter_client.consoleapp instead.", stacklevel=2)
|
||||
|
||||
from jupyter_client.consoleapp import *
|
0
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/__init__.py
vendored
Normal file
0
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/__init__.py
vendored
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
267
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/alias.py
vendored
Normal file
267
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/alias.py
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
# encoding: utf-8
|
||||
"""
|
||||
System command aliases.
|
||||
|
||||
Authors:
|
||||
|
||||
* Fernando Perez
|
||||
* Brian Granger
|
||||
"""
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Copyright (C) 2008-2011 The IPython Development Team
|
||||
#
|
||||
# Distributed under the terms of the BSD License.
|
||||
#
|
||||
# The full license is in the file COPYING.txt, distributed with this software.
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Imports
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
from traitlets.config.configurable import Configurable
|
||||
from .error import UsageError
|
||||
|
||||
from traitlets import List, Instance
|
||||
from logging import error
|
||||
|
||||
import typing as t
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Utilities
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
# This is used as the pattern for calls to split_user_input.
|
||||
shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)')
|
||||
|
||||
def default_aliases() -> t.List[t.Tuple[str, str]]:
|
||||
"""Return list of shell aliases to auto-define.
|
||||
"""
|
||||
# Note: the aliases defined here should be safe to use on a kernel
|
||||
# regardless of what frontend it is attached to. Frontends that use a
|
||||
# kernel in-process can define additional aliases that will only work in
|
||||
# their case. For example, things like 'less' or 'clear' that manipulate
|
||||
# the terminal should NOT be declared here, as they will only work if the
|
||||
# kernel is running inside a true terminal, and not over the network.
|
||||
|
||||
if os.name == 'posix':
|
||||
default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
|
||||
('mv', 'mv'), ('rm', 'rm'), ('cp', 'cp'),
|
||||
('cat', 'cat'),
|
||||
]
|
||||
# Useful set of ls aliases. The GNU and BSD options are a little
|
||||
# different, so we make aliases that provide as similar as possible
|
||||
# behavior in ipython, by passing the right flags for each platform
|
||||
if sys.platform.startswith('linux'):
|
||||
ls_aliases = [('ls', 'ls -F --color'),
|
||||
# long ls
|
||||
('ll', 'ls -F -o --color'),
|
||||
# ls normal files only
|
||||
('lf', 'ls -F -o --color %l | grep ^-'),
|
||||
# ls symbolic links
|
||||
('lk', 'ls -F -o --color %l | grep ^l'),
|
||||
# directories or links to directories,
|
||||
('ldir', 'ls -F -o --color %l | grep /$'),
|
||||
# things which are executable
|
||||
('lx', 'ls -F -o --color %l | grep ^-..x'),
|
||||
]
|
||||
elif sys.platform.startswith('openbsd') or sys.platform.startswith('netbsd'):
|
||||
# OpenBSD, NetBSD. The ls implementation on these platforms do not support
|
||||
# the -G switch and lack the ability to use colorized output.
|
||||
ls_aliases = [('ls', 'ls -F'),
|
||||
# long ls
|
||||
('ll', 'ls -F -l'),
|
||||
# ls normal files only
|
||||
('lf', 'ls -F -l %l | grep ^-'),
|
||||
# ls symbolic links
|
||||
('lk', 'ls -F -l %l | grep ^l'),
|
||||
# directories or links to directories,
|
||||
('ldir', 'ls -F -l %l | grep /$'),
|
||||
# things which are executable
|
||||
('lx', 'ls -F -l %l | grep ^-..x'),
|
||||
]
|
||||
else:
|
||||
# BSD, OSX, etc.
|
||||
ls_aliases = [('ls', 'ls -F -G'),
|
||||
# long ls
|
||||
('ll', 'ls -F -l -G'),
|
||||
# ls normal files only
|
||||
('lf', 'ls -F -l -G %l | grep ^-'),
|
||||
# ls symbolic links
|
||||
('lk', 'ls -F -l -G %l | grep ^l'),
|
||||
# directories or links to directories,
|
||||
('ldir', 'ls -F -G -l %l | grep /$'),
|
||||
# things which are executable
|
||||
('lx', 'ls -F -l -G %l | grep ^-..x'),
|
||||
]
|
||||
default_aliases = default_aliases + ls_aliases
|
||||
elif os.name in ['nt', 'dos']:
|
||||
default_aliases = [('ls', 'dir /on'),
|
||||
('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'),
|
||||
('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
|
||||
('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'),
|
||||
]
|
||||
else:
|
||||
default_aliases = []
|
||||
|
||||
return default_aliases
|
||||
|
||||
|
||||
class AliasError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidAliasError(AliasError):
|
||||
pass
|
||||
|
||||
class Alias(object):
|
||||
"""Callable object storing the details of one alias.
|
||||
|
||||
Instances are registered as magic functions to allow use of aliases.
|
||||
"""
|
||||
|
||||
# Prepare blacklist
|
||||
blacklist = {'cd','popd','pushd','dhist','alias','unalias'}
|
||||
|
||||
def __init__(self, shell, name, cmd):
|
||||
self.shell = shell
|
||||
self.name = name
|
||||
self.cmd = cmd
|
||||
self.__doc__ = "Alias for `!{}`".format(cmd)
|
||||
self.nargs = self.validate()
|
||||
|
||||
def validate(self):
|
||||
"""Validate the alias, and return the number of arguments."""
|
||||
if self.name in self.blacklist:
|
||||
raise InvalidAliasError("The name %s can't be aliased "
|
||||
"because it is a keyword or builtin." % self.name)
|
||||
try:
|
||||
caller = self.shell.magics_manager.magics['line'][self.name]
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
if not isinstance(caller, Alias):
|
||||
raise InvalidAliasError("The name %s can't be aliased "
|
||||
"because it is another magic command." % self.name)
|
||||
|
||||
if not (isinstance(self.cmd, str)):
|
||||
raise InvalidAliasError("An alias command must be a string, "
|
||||
"got: %r" % self.cmd)
|
||||
|
||||
nargs = self.cmd.count('%s') - self.cmd.count('%%s')
|
||||
|
||||
if (nargs > 0) and (self.cmd.find('%l') >= 0):
|
||||
raise InvalidAliasError('The %s and %l specifiers are mutually '
|
||||
'exclusive in alias definitions.')
|
||||
|
||||
return nargs
|
||||
|
||||
def __repr__(self):
|
||||
return "<alias {} for {!r}>".format(self.name, self.cmd)
|
||||
|
||||
def __call__(self, rest=''):
|
||||
cmd = self.cmd
|
||||
nargs = self.nargs
|
||||
# Expand the %l special to be the user's input line
|
||||
if cmd.find('%l') >= 0:
|
||||
cmd = cmd.replace('%l', rest)
|
||||
rest = ''
|
||||
|
||||
if nargs==0:
|
||||
if cmd.find('%%s') >= 1:
|
||||
cmd = cmd.replace('%%s', '%s')
|
||||
# Simple, argument-less aliases
|
||||
cmd = '%s %s' % (cmd, rest)
|
||||
else:
|
||||
# Handle aliases with positional arguments
|
||||
args = rest.split(None, nargs)
|
||||
if len(args) < nargs:
|
||||
raise UsageError('Alias <%s> requires %s arguments, %s given.' %
|
||||
(self.name, nargs, len(args)))
|
||||
cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
|
||||
|
||||
self.shell.system(cmd)
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Main AliasManager class
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
class AliasManager(Configurable):
|
||||
default_aliases: List = List(default_aliases()).tag(config=True)
|
||||
user_aliases: List = List(default_value=[]).tag(config=True)
|
||||
shell = Instance(
|
||||
"IPython.core.interactiveshell.InteractiveShellABC", allow_none=True
|
||||
)
|
||||
|
||||
def __init__(self, shell=None, **kwargs):
|
||||
super(AliasManager, self).__init__(shell=shell, **kwargs)
|
||||
# For convenient access
|
||||
if self.shell is not None:
|
||||
self.linemagics = self.shell.magics_manager.magics["line"]
|
||||
self.init_aliases()
|
||||
|
||||
def init_aliases(self):
|
||||
# Load default & user aliases
|
||||
for name, cmd in self.default_aliases + self.user_aliases:
|
||||
if (
|
||||
cmd.startswith("ls ")
|
||||
and self.shell is not None
|
||||
and self.shell.colors == "NoColor"
|
||||
):
|
||||
cmd = cmd.replace(" --color", "")
|
||||
self.soft_define_alias(name, cmd)
|
||||
|
||||
@property
|
||||
def aliases(self):
|
||||
return [(n, func.cmd) for (n, func) in self.linemagics.items()
|
||||
if isinstance(func, Alias)]
|
||||
|
||||
def soft_define_alias(self, name, cmd):
|
||||
"""Define an alias, but don't raise on an AliasError."""
|
||||
try:
|
||||
self.define_alias(name, cmd)
|
||||
except AliasError as e:
|
||||
error("Invalid alias: %s" % e)
|
||||
|
||||
def define_alias(self, name, cmd):
|
||||
"""Define a new alias after validating it.
|
||||
|
||||
This will raise an :exc:`AliasError` if there are validation
|
||||
problems.
|
||||
"""
|
||||
caller = Alias(shell=self.shell, name=name, cmd=cmd)
|
||||
self.shell.magics_manager.register_function(caller, magic_kind='line',
|
||||
magic_name=name)
|
||||
|
||||
def get_alias(self, name):
|
||||
"""Return an alias, or None if no alias by that name exists."""
|
||||
aname = self.linemagics.get(name, None)
|
||||
return aname if isinstance(aname, Alias) else None
|
||||
|
||||
def is_alias(self, name):
|
||||
"""Return whether or not a given name has been defined as an alias"""
|
||||
return self.get_alias(name) is not None
|
||||
|
||||
def undefine_alias(self, name):
|
||||
if self.is_alias(name):
|
||||
del self.linemagics[name]
|
||||
else:
|
||||
raise ValueError('%s is not an alias' % name)
|
||||
|
||||
def clear_aliases(self):
|
||||
for name, _ in self.aliases:
|
||||
self.undefine_alias(name)
|
||||
|
||||
def retrieve_alias(self, name):
|
||||
"""Retrieve the command to which an alias expands."""
|
||||
caller = self.get_alias(name)
|
||||
if caller:
|
||||
return caller.cmd
|
||||
else:
|
||||
raise ValueError('%s is not an alias' % name)
|
492
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/application.py
vendored
Normal file
492
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/application.py
vendored
Normal file
|
@ -0,0 +1,492 @@
|
|||
# encoding: utf-8
|
||||
"""
|
||||
An application for IPython.
|
||||
|
||||
All top-level applications should use the classes in this module for
|
||||
handling configuration and creating configurables.
|
||||
|
||||
The job of an :class:`Application` is to create the master configuration
|
||||
object and then create the configurable objects, passing the config to them.
|
||||
"""
|
||||
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
import atexit
|
||||
from copy import deepcopy
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from traitlets.config.application import Application, catch_config_error
|
||||
from traitlets.config.loader import ConfigFileNotFound, PyFileConfigLoader
|
||||
from IPython.core import release, crashhandler
|
||||
from IPython.core.profiledir import ProfileDir, ProfileDirError
|
||||
from IPython.paths import get_ipython_dir, get_ipython_package_dir
|
||||
from IPython.utils.path import ensure_dir_exists
|
||||
from traitlets import (
|
||||
List, Unicode, Type, Bool, Set, Instance, Undefined,
|
||||
default, observe,
|
||||
)
|
||||
|
||||
if os.name == "nt":
|
||||
programdata = os.environ.get("PROGRAMDATA", None)
|
||||
if programdata is not None:
|
||||
SYSTEM_CONFIG_DIRS = [str(Path(programdata) / "ipython")]
|
||||
else: # PROGRAMDATA is not defined by default on XP.
|
||||
SYSTEM_CONFIG_DIRS = []
|
||||
else:
|
||||
SYSTEM_CONFIG_DIRS = [
|
||||
"/usr/local/etc/ipython",
|
||||
"/etc/ipython",
|
||||
]
|
||||
|
||||
|
||||
ENV_CONFIG_DIRS = []
|
||||
_env_config_dir = os.path.join(sys.prefix, 'etc', 'ipython')
|
||||
if _env_config_dir not in SYSTEM_CONFIG_DIRS:
|
||||
# only add ENV_CONFIG if sys.prefix is not already included
|
||||
ENV_CONFIG_DIRS.append(_env_config_dir)
|
||||
|
||||
|
||||
_envvar = os.environ.get('IPYTHON_SUPPRESS_CONFIG_ERRORS')
|
||||
if _envvar in {None, ''}:
|
||||
IPYTHON_SUPPRESS_CONFIG_ERRORS = None
|
||||
else:
|
||||
if _envvar.lower() in {'1','true'}:
|
||||
IPYTHON_SUPPRESS_CONFIG_ERRORS = True
|
||||
elif _envvar.lower() in {'0','false'} :
|
||||
IPYTHON_SUPPRESS_CONFIG_ERRORS = False
|
||||
else:
|
||||
sys.exit("Unsupported value for environment variable: 'IPYTHON_SUPPRESS_CONFIG_ERRORS' is set to '%s' which is none of {'0', '1', 'false', 'true', ''}."% _envvar )
|
||||
|
||||
# aliases and flags
|
||||
|
||||
base_aliases = {}
|
||||
if isinstance(Application.aliases, dict):
|
||||
# traitlets 5
|
||||
base_aliases.update(Application.aliases)
|
||||
base_aliases.update(
|
||||
{
|
||||
"profile-dir": "ProfileDir.location",
|
||||
"profile": "BaseIPythonApplication.profile",
|
||||
"ipython-dir": "BaseIPythonApplication.ipython_dir",
|
||||
"log-level": "Application.log_level",
|
||||
"config": "BaseIPythonApplication.extra_config_file",
|
||||
}
|
||||
)
|
||||
|
||||
base_flags = dict()
|
||||
if isinstance(Application.flags, dict):
|
||||
# traitlets 5
|
||||
base_flags.update(Application.flags)
|
||||
base_flags.update(
|
||||
dict(
|
||||
debug=(
|
||||
{"Application": {"log_level": logging.DEBUG}},
|
||||
"set log level to logging.DEBUG (maximize logging output)",
|
||||
),
|
||||
quiet=(
|
||||
{"Application": {"log_level": logging.CRITICAL}},
|
||||
"set log level to logging.CRITICAL (minimize logging output)",
|
||||
),
|
||||
init=(
|
||||
{
|
||||
"BaseIPythonApplication": {
|
||||
"copy_config_files": True,
|
||||
"auto_create": True,
|
||||
}
|
||||
},
|
||||
"""Initialize profile with default config files. This is equivalent
|
||||
to running `ipython profile create <profile>` prior to startup.
|
||||
""",
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class ProfileAwareConfigLoader(PyFileConfigLoader):
|
||||
"""A Python file config loader that is aware of IPython profiles."""
|
||||
def load_subconfig(self, fname, path=None, profile=None):
|
||||
if profile is not None:
|
||||
try:
|
||||
profile_dir = ProfileDir.find_profile_dir_by_name(
|
||||
get_ipython_dir(),
|
||||
profile,
|
||||
)
|
||||
except ProfileDirError:
|
||||
return
|
||||
path = profile_dir.location
|
||||
return super(ProfileAwareConfigLoader, self).load_subconfig(fname, path=path)
|
||||
|
||||
class BaseIPythonApplication(Application):
|
||||
name = "ipython"
|
||||
description = "IPython: an enhanced interactive Python shell."
|
||||
version = Unicode(release.version)
|
||||
|
||||
aliases = base_aliases
|
||||
flags = base_flags
|
||||
classes = List([ProfileDir])
|
||||
|
||||
# enable `load_subconfig('cfg.py', profile='name')`
|
||||
python_config_loader_class = ProfileAwareConfigLoader
|
||||
|
||||
# Track whether the config_file has changed,
|
||||
# because some logic happens only if we aren't using the default.
|
||||
config_file_specified = Set()
|
||||
|
||||
config_file_name = Unicode()
|
||||
@default('config_file_name')
|
||||
def _config_file_name_default(self):
|
||||
return self.name.replace('-','_') + u'_config.py'
|
||||
@observe('config_file_name')
|
||||
def _config_file_name_changed(self, change):
|
||||
if change['new'] != change['old']:
|
||||
self.config_file_specified.add(change['new'])
|
||||
|
||||
# The directory that contains IPython's builtin profiles.
|
||||
builtin_profile_dir = Unicode(
|
||||
os.path.join(get_ipython_package_dir(), u'config', u'profile', u'default')
|
||||
)
|
||||
|
||||
config_file_paths = List(Unicode())
|
||||
@default('config_file_paths')
|
||||
def _config_file_paths_default(self):
|
||||
return []
|
||||
|
||||
extra_config_file = Unicode(
|
||||
help="""Path to an extra config file to load.
|
||||
|
||||
If specified, load this config file in addition to any other IPython config.
|
||||
""").tag(config=True)
|
||||
@observe('extra_config_file')
|
||||
def _extra_config_file_changed(self, change):
|
||||
old = change['old']
|
||||
new = change['new']
|
||||
try:
|
||||
self.config_files.remove(old)
|
||||
except ValueError:
|
||||
pass
|
||||
self.config_file_specified.add(new)
|
||||
self.config_files.append(new)
|
||||
|
||||
profile = Unicode(u'default',
|
||||
help="""The IPython profile to use."""
|
||||
).tag(config=True)
|
||||
|
||||
@observe('profile')
|
||||
def _profile_changed(self, change):
|
||||
self.builtin_profile_dir = os.path.join(
|
||||
get_ipython_package_dir(), u'config', u'profile', change['new']
|
||||
)
|
||||
|
||||
add_ipython_dir_to_sys_path = Bool(
|
||||
False,
|
||||
"""Should the IPython profile directory be added to sys path ?
|
||||
|
||||
This option was non-existing before IPython 8.0, and ipython_dir was added to
|
||||
sys path to allow import of extensions present there. This was historical
|
||||
baggage from when pip did not exist. This now default to false,
|
||||
but can be set to true for legacy reasons.
|
||||
""",
|
||||
).tag(config=True)
|
||||
|
||||
ipython_dir = Unicode(
|
||||
help="""
|
||||
The name of the IPython directory. This directory is used for logging
|
||||
configuration (through profiles), history storage, etc. The default
|
||||
is usually $HOME/.ipython. This option can also be specified through
|
||||
the environment variable IPYTHONDIR.
|
||||
"""
|
||||
).tag(config=True)
|
||||
@default('ipython_dir')
|
||||
def _ipython_dir_default(self):
|
||||
d = get_ipython_dir()
|
||||
self._ipython_dir_changed({
|
||||
'name': 'ipython_dir',
|
||||
'old': d,
|
||||
'new': d,
|
||||
})
|
||||
return d
|
||||
|
||||
_in_init_profile_dir = False
|
||||
|
||||
profile_dir = Instance(ProfileDir, allow_none=True)
|
||||
|
||||
@default('profile_dir')
|
||||
def _profile_dir_default(self):
|
||||
# avoid recursion
|
||||
if self._in_init_profile_dir:
|
||||
return
|
||||
# profile_dir requested early, force initialization
|
||||
self.init_profile_dir()
|
||||
return self.profile_dir
|
||||
|
||||
overwrite = Bool(False,
|
||||
help="""Whether to overwrite existing config files when copying"""
|
||||
).tag(config=True)
|
||||
|
||||
auto_create = Bool(False,
|
||||
help="""Whether to create profile dir if it doesn't exist"""
|
||||
).tag(config=True)
|
||||
|
||||
config_files = List(Unicode())
|
||||
|
||||
@default('config_files')
|
||||
def _config_files_default(self):
|
||||
return [self.config_file_name]
|
||||
|
||||
copy_config_files = Bool(False,
|
||||
help="""Whether to install the default config files into the profile dir.
|
||||
If a new profile is being created, and IPython contains config files for that
|
||||
profile, then they will be staged into the new directory. Otherwise,
|
||||
default config files will be automatically generated.
|
||||
""").tag(config=True)
|
||||
|
||||
verbose_crash = Bool(False,
|
||||
help="""Create a massive crash report when IPython encounters what may be an
|
||||
internal error. The default is to append a short message to the
|
||||
usual traceback""").tag(config=True)
|
||||
|
||||
# The class to use as the crash handler.
|
||||
crash_handler_class = Type(crashhandler.CrashHandler)
|
||||
|
||||
@catch_config_error
|
||||
def __init__(self, **kwargs):
|
||||
super(BaseIPythonApplication, self).__init__(**kwargs)
|
||||
# ensure current working directory exists
|
||||
try:
|
||||
os.getcwd()
|
||||
except:
|
||||
# exit if cwd doesn't exist
|
||||
self.log.error("Current working directory doesn't exist.")
|
||||
self.exit(1)
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
# Various stages of Application creation
|
||||
#-------------------------------------------------------------------------
|
||||
|
||||
def init_crash_handler(self):
|
||||
"""Create a crash handler, typically setting sys.excepthook to it."""
|
||||
self.crash_handler = self.crash_handler_class(self)
|
||||
sys.excepthook = self.excepthook
|
||||
def unset_crashhandler():
|
||||
sys.excepthook = sys.__excepthook__
|
||||
atexit.register(unset_crashhandler)
|
||||
|
||||
def excepthook(self, etype, evalue, tb):
|
||||
"""this is sys.excepthook after init_crashhandler
|
||||
|
||||
set self.verbose_crash=True to use our full crashhandler, instead of
|
||||
a regular traceback with a short message (crash_handler_lite)
|
||||
"""
|
||||
|
||||
if self.verbose_crash:
|
||||
return self.crash_handler(etype, evalue, tb)
|
||||
else:
|
||||
return crashhandler.crash_handler_lite(etype, evalue, tb)
|
||||
|
||||
@observe('ipython_dir')
|
||||
def _ipython_dir_changed(self, change):
|
||||
old = change['old']
|
||||
new = change['new']
|
||||
if old is not Undefined:
|
||||
str_old = os.path.abspath(old)
|
||||
if str_old in sys.path:
|
||||
sys.path.remove(str_old)
|
||||
if self.add_ipython_dir_to_sys_path:
|
||||
str_path = os.path.abspath(new)
|
||||
sys.path.append(str_path)
|
||||
ensure_dir_exists(new)
|
||||
readme = os.path.join(new, "README")
|
||||
readme_src = os.path.join(
|
||||
get_ipython_package_dir(), "config", "profile", "README"
|
||||
)
|
||||
if not os.path.exists(readme) and os.path.exists(readme_src):
|
||||
shutil.copy(readme_src, readme)
|
||||
for d in ("extensions", "nbextensions"):
|
||||
path = os.path.join(new, d)
|
||||
try:
|
||||
ensure_dir_exists(path)
|
||||
except OSError as e:
|
||||
# this will not be EEXIST
|
||||
self.log.error("couldn't create path %s: %s", path, e)
|
||||
self.log.debug("IPYTHONDIR set to: %s", new)
|
||||
|
||||
def load_config_file(self, suppress_errors=IPYTHON_SUPPRESS_CONFIG_ERRORS):
|
||||
"""Load the config file.
|
||||
|
||||
By default, errors in loading config are handled, and a warning
|
||||
printed on screen. For testing, the suppress_errors option is set
|
||||
to False, so errors will make tests fail.
|
||||
|
||||
`suppress_errors` default value is to be `None` in which case the
|
||||
behavior default to the one of `traitlets.Application`.
|
||||
|
||||
The default value can be set :
|
||||
- to `False` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '0', or 'false' (case insensitive).
|
||||
- to `True` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '1' or 'true' (case insensitive).
|
||||
- to `None` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '' (empty string) or leaving it unset.
|
||||
|
||||
Any other value are invalid, and will make IPython exit with a non-zero return code.
|
||||
"""
|
||||
|
||||
|
||||
self.log.debug("Searching path %s for config files", self.config_file_paths)
|
||||
base_config = 'ipython_config.py'
|
||||
self.log.debug("Attempting to load config file: %s" %
|
||||
base_config)
|
||||
try:
|
||||
if suppress_errors is not None:
|
||||
old_value = Application.raise_config_file_errors
|
||||
Application.raise_config_file_errors = not suppress_errors;
|
||||
Application.load_config_file(
|
||||
self,
|
||||
base_config,
|
||||
path=self.config_file_paths
|
||||
)
|
||||
except ConfigFileNotFound:
|
||||
# ignore errors loading parent
|
||||
self.log.debug("Config file %s not found", base_config)
|
||||
pass
|
||||
if suppress_errors is not None:
|
||||
Application.raise_config_file_errors = old_value
|
||||
|
||||
for config_file_name in self.config_files:
|
||||
if not config_file_name or config_file_name == base_config:
|
||||
continue
|
||||
self.log.debug("Attempting to load config file: %s" %
|
||||
self.config_file_name)
|
||||
try:
|
||||
Application.load_config_file(
|
||||
self,
|
||||
config_file_name,
|
||||
path=self.config_file_paths
|
||||
)
|
||||
except ConfigFileNotFound:
|
||||
# Only warn if the default config file was NOT being used.
|
||||
if config_file_name in self.config_file_specified:
|
||||
msg = self.log.warning
|
||||
else:
|
||||
msg = self.log.debug
|
||||
msg("Config file not found, skipping: %s", config_file_name)
|
||||
except Exception:
|
||||
# For testing purposes.
|
||||
if not suppress_errors:
|
||||
raise
|
||||
self.log.warning("Error loading config file: %s" %
|
||||
self.config_file_name, exc_info=True)
|
||||
|
||||
def init_profile_dir(self):
|
||||
"""initialize the profile dir"""
|
||||
self._in_init_profile_dir = True
|
||||
if self.profile_dir is not None:
|
||||
# already ran
|
||||
return
|
||||
if 'ProfileDir.location' not in self.config:
|
||||
# location not specified, find by profile name
|
||||
try:
|
||||
p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
|
||||
except ProfileDirError:
|
||||
# not found, maybe create it (always create default profile)
|
||||
if self.auto_create or self.profile == 'default':
|
||||
try:
|
||||
p = ProfileDir.create_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
|
||||
except ProfileDirError:
|
||||
self.log.fatal("Could not create profile: %r"%self.profile)
|
||||
self.exit(1)
|
||||
else:
|
||||
self.log.info("Created profile dir: %r"%p.location)
|
||||
else:
|
||||
self.log.fatal("Profile %r not found."%self.profile)
|
||||
self.exit(1)
|
||||
else:
|
||||
self.log.debug("Using existing profile dir: %r", p.location)
|
||||
else:
|
||||
location = self.config.ProfileDir.location
|
||||
# location is fully specified
|
||||
try:
|
||||
p = ProfileDir.find_profile_dir(location, self.config)
|
||||
except ProfileDirError:
|
||||
# not found, maybe create it
|
||||
if self.auto_create:
|
||||
try:
|
||||
p = ProfileDir.create_profile_dir(location, self.config)
|
||||
except ProfileDirError:
|
||||
self.log.fatal("Could not create profile directory: %r"%location)
|
||||
self.exit(1)
|
||||
else:
|
||||
self.log.debug("Creating new profile dir: %r"%location)
|
||||
else:
|
||||
self.log.fatal("Profile directory %r not found."%location)
|
||||
self.exit(1)
|
||||
else:
|
||||
self.log.debug("Using existing profile dir: %r", p.location)
|
||||
# if profile_dir is specified explicitly, set profile name
|
||||
dir_name = os.path.basename(p.location)
|
||||
if dir_name.startswith('profile_'):
|
||||
self.profile = dir_name[8:]
|
||||
|
||||
self.profile_dir = p
|
||||
self.config_file_paths.append(p.location)
|
||||
self._in_init_profile_dir = False
|
||||
|
||||
def init_config_files(self):
|
||||
"""[optionally] copy default config files into profile dir."""
|
||||
self.config_file_paths.extend(ENV_CONFIG_DIRS)
|
||||
self.config_file_paths.extend(SYSTEM_CONFIG_DIRS)
|
||||
# copy config files
|
||||
path = Path(self.builtin_profile_dir)
|
||||
if self.copy_config_files:
|
||||
src = self.profile
|
||||
|
||||
cfg = self.config_file_name
|
||||
if path and (path / cfg).exists():
|
||||
self.log.warning(
|
||||
"Staging %r from %s into %r [overwrite=%s]"
|
||||
% (cfg, src, self.profile_dir.location, self.overwrite)
|
||||
)
|
||||
self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite)
|
||||
else:
|
||||
self.stage_default_config_file()
|
||||
else:
|
||||
# Still stage *bundled* config files, but not generated ones
|
||||
# This is necessary for `ipython profile=sympy` to load the profile
|
||||
# on the first go
|
||||
files = path.glob("*.py")
|
||||
for fullpath in files:
|
||||
cfg = fullpath.name
|
||||
if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False):
|
||||
# file was copied
|
||||
self.log.warning("Staging bundled %s from %s into %r"%(
|
||||
cfg, self.profile, self.profile_dir.location)
|
||||
)
|
||||
|
||||
|
||||
def stage_default_config_file(self):
|
||||
"""auto generate default config file, and stage it into the profile."""
|
||||
s = self.generate_config_file()
|
||||
config_file = Path(self.profile_dir.location) / self.config_file_name
|
||||
if self.overwrite or not config_file.exists():
|
||||
self.log.warning("Generating default config file: %r", (config_file))
|
||||
config_file.write_text(s, encoding="utf-8")
|
||||
|
||||
@catch_config_error
|
||||
def initialize(self, argv=None):
|
||||
# don't hook up crash handler before parsing command-line
|
||||
self.parse_command_line(argv)
|
||||
self.init_crash_handler()
|
||||
if self.subapp is not None:
|
||||
# stop here if subapp is taking over
|
||||
return
|
||||
# save a copy of CLI config to re-load after config files
|
||||
# so that it has highest priority
|
||||
cl_config = deepcopy(self.config)
|
||||
self.init_profile_dir()
|
||||
self.init_config_files()
|
||||
self.load_config_file()
|
||||
# enforce cl-opts override configfile opts:
|
||||
self.update_config(cl_config)
|
155
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/async_helpers.py
vendored
Normal file
155
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/async_helpers.py
vendored
Normal file
|
@ -0,0 +1,155 @@
|
|||
"""
|
||||
Async helper function that are invalid syntax on Python 3.5 and below.
|
||||
|
||||
This code is best effort, and may have edge cases not behaving as expected. In
|
||||
particular it contain a number of heuristics to detect whether code is
|
||||
effectively async and need to run in an event loop or not.
|
||||
|
||||
Some constructs (like top-level `return`, or `yield`) are taken care of
|
||||
explicitly to actually raise a SyntaxError and stay as close as possible to
|
||||
Python semantics.
|
||||
"""
|
||||
|
||||
import ast
|
||||
import asyncio
|
||||
import inspect
|
||||
from functools import wraps
|
||||
|
||||
_asyncio_event_loop = None
|
||||
|
||||
|
||||
def get_asyncio_loop():
|
||||
"""asyncio has deprecated get_event_loop
|
||||
|
||||
Replicate it here, with our desired semantics:
|
||||
|
||||
- always returns a valid, not-closed loop
|
||||
- not thread-local like asyncio's,
|
||||
because we only want one loop for IPython
|
||||
- if called from inside a coroutine (e.g. in ipykernel),
|
||||
return the running loop
|
||||
|
||||
.. versionadded:: 8.0
|
||||
"""
|
||||
try:
|
||||
return asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
# not inside a coroutine,
|
||||
# track our own global
|
||||
pass
|
||||
|
||||
# not thread-local like asyncio's,
|
||||
# because we only track one event loop to run for IPython itself,
|
||||
# always in the main thread.
|
||||
global _asyncio_event_loop
|
||||
if _asyncio_event_loop is None or _asyncio_event_loop.is_closed():
|
||||
_asyncio_event_loop = asyncio.new_event_loop()
|
||||
return _asyncio_event_loop
|
||||
|
||||
|
||||
class _AsyncIORunner:
|
||||
def __call__(self, coro):
|
||||
"""
|
||||
Handler for asyncio autoawait
|
||||
"""
|
||||
return get_asyncio_loop().run_until_complete(coro)
|
||||
|
||||
def __str__(self):
|
||||
return "asyncio"
|
||||
|
||||
|
||||
_asyncio_runner = _AsyncIORunner()
|
||||
|
||||
|
||||
class _AsyncIOProxy:
|
||||
"""Proxy-object for an asyncio
|
||||
|
||||
Any coroutine methods will be wrapped in event_loop.run_
|
||||
"""
|
||||
|
||||
def __init__(self, obj, event_loop):
|
||||
self._obj = obj
|
||||
self._event_loop = event_loop
|
||||
|
||||
def __repr__(self):
|
||||
return f"<_AsyncIOProxy({self._obj!r})>"
|
||||
|
||||
def __getattr__(self, key):
|
||||
attr = getattr(self._obj, key)
|
||||
if inspect.iscoroutinefunction(attr):
|
||||
# if it's a coroutine method,
|
||||
# return a threadsafe wrapper onto the _current_ asyncio loop
|
||||
@wraps(attr)
|
||||
def _wrapped(*args, **kwargs):
|
||||
concurrent_future = asyncio.run_coroutine_threadsafe(
|
||||
attr(*args, **kwargs), self._event_loop
|
||||
)
|
||||
return asyncio.wrap_future(concurrent_future)
|
||||
|
||||
return _wrapped
|
||||
else:
|
||||
return attr
|
||||
|
||||
def __dir__(self):
|
||||
return dir(self._obj)
|
||||
|
||||
|
||||
def _curio_runner(coroutine):
|
||||
"""
|
||||
handler for curio autoawait
|
||||
"""
|
||||
import curio
|
||||
|
||||
return curio.run(coroutine)
|
||||
|
||||
|
||||
def _trio_runner(async_fn):
|
||||
import trio
|
||||
|
||||
async def loc(coro):
|
||||
"""
|
||||
We need the dummy no-op async def to protect from
|
||||
trio's internal. See https://github.com/python-trio/trio/issues/89
|
||||
"""
|
||||
return await coro
|
||||
|
||||
return trio.run(loc, async_fn)
|
||||
|
||||
|
||||
def _pseudo_sync_runner(coro):
|
||||
"""
|
||||
A runner that does not really allow async execution, and just advance the coroutine.
|
||||
|
||||
See discussion in https://github.com/python-trio/trio/issues/608,
|
||||
|
||||
Credit to Nathaniel Smith
|
||||
"""
|
||||
try:
|
||||
coro.send(None)
|
||||
except StopIteration as exc:
|
||||
return exc.value
|
||||
else:
|
||||
# TODO: do not raise but return an execution result with the right info.
|
||||
raise RuntimeError(
|
||||
"{coro_name!r} needs a real async loop".format(coro_name=coro.__name__)
|
||||
)
|
||||
|
||||
|
||||
def _should_be_async(cell: str) -> bool:
|
||||
"""Detect if a block of code need to be wrapped in an `async def`
|
||||
|
||||
Attempt to parse the block of code, it it compile we're fine.
|
||||
Otherwise we wrap if and try to compile.
|
||||
|
||||
If it works, assume it should be async. Otherwise Return False.
|
||||
|
||||
Not handled yet: If the block of code has a return statement as the top
|
||||
level, it will be seen as async. This is a know limitation.
|
||||
"""
|
||||
try:
|
||||
code = compile(
|
||||
cell, "<>", "exec", flags=getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0)
|
||||
)
|
||||
return inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE
|
||||
except (SyntaxError, MemoryError):
|
||||
return False
|
70
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/autocall.py
vendored
Normal file
70
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/autocall.py
vendored
Normal file
|
@ -0,0 +1,70 @@
|
|||
# encoding: utf-8
|
||||
"""
|
||||
Autocall capabilities for IPython.core.
|
||||
|
||||
Authors:
|
||||
|
||||
* Brian Granger
|
||||
* Fernando Perez
|
||||
* Thomas Kluyver
|
||||
|
||||
Notes
|
||||
-----
|
||||
"""
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Copyright (C) 2008-2011 The IPython Development Team
|
||||
#
|
||||
# Distributed under the terms of the BSD License. The full license is in
|
||||
# the file COPYING, distributed as part of this software.
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Imports
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Code
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
class IPyAutocall(object):
|
||||
""" Instances of this class are always autocalled
|
||||
|
||||
This happens regardless of 'autocall' variable state. Use this to
|
||||
develop macro-like mechanisms.
|
||||
"""
|
||||
_ip = None
|
||||
rewrite = True
|
||||
def __init__(self, ip=None):
|
||||
self._ip = ip
|
||||
|
||||
def set_ip(self, ip):
|
||||
"""Will be used to set _ip point to current ipython instance b/f call
|
||||
|
||||
Override this method if you don't want this to happen.
|
||||
|
||||
"""
|
||||
self._ip = ip
|
||||
|
||||
|
||||
class ExitAutocall(IPyAutocall):
|
||||
"""An autocallable object which will be added to the user namespace so that
|
||||
exit, exit(), quit or quit() are all valid ways to close the shell."""
|
||||
rewrite = False
|
||||
|
||||
def __call__(self):
|
||||
self._ip.ask_exit()
|
||||
|
||||
class ZMQExitAutocall(ExitAutocall):
|
||||
"""Exit IPython. Autocallable, so it needn't be explicitly called.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
keep_kernel : bool
|
||||
If True, leave the kernel alive. Otherwise, tell the kernel to exit too
|
||||
(default).
|
||||
"""
|
||||
def __call__(self, keep_kernel=False):
|
||||
self._ip.keepkernel_on_exit = keep_kernel
|
||||
self._ip.ask_exit()
|
86
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/builtin_trap.py
vendored
Normal file
86
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/builtin_trap.py
vendored
Normal file
|
@ -0,0 +1,86 @@
|
|||
"""
|
||||
A context manager for managing things injected into :mod:`builtins`.
|
||||
"""
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
import builtins as builtin_mod
|
||||
|
||||
from traitlets.config.configurable import Configurable
|
||||
|
||||
from traitlets import Instance
|
||||
|
||||
|
||||
class __BuiltinUndefined(object): pass
|
||||
BuiltinUndefined = __BuiltinUndefined()
|
||||
|
||||
class __HideBuiltin(object): pass
|
||||
HideBuiltin = __HideBuiltin()
|
||||
|
||||
|
||||
class BuiltinTrap(Configurable):
|
||||
|
||||
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
|
||||
allow_none=True)
|
||||
|
||||
def __init__(self, shell=None):
|
||||
super(BuiltinTrap, self).__init__(shell=shell, config=None)
|
||||
self._orig_builtins = {}
|
||||
# We define this to track if a single BuiltinTrap is nested.
|
||||
# Only turn off the trap when the outermost call to __exit__ is made.
|
||||
self._nested_level = 0
|
||||
self.shell = shell
|
||||
# builtins we always add - if set to HideBuiltin, they will just
|
||||
# be removed instead of being replaced by something else
|
||||
self.auto_builtins = {'exit': HideBuiltin,
|
||||
'quit': HideBuiltin,
|
||||
'get_ipython': self.shell.get_ipython,
|
||||
}
|
||||
|
||||
def __enter__(self):
|
||||
if self._nested_level == 0:
|
||||
self.activate()
|
||||
self._nested_level += 1
|
||||
# I return self, so callers can use add_builtin in a with clause.
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
if self._nested_level == 1:
|
||||
self.deactivate()
|
||||
self._nested_level -= 1
|
||||
# Returning False will cause exceptions to propagate
|
||||
return False
|
||||
|
||||
def add_builtin(self, key, value):
|
||||
"""Add a builtin and save the original."""
|
||||
bdict = builtin_mod.__dict__
|
||||
orig = bdict.get(key, BuiltinUndefined)
|
||||
if value is HideBuiltin:
|
||||
if orig is not BuiltinUndefined: #same as 'key in bdict'
|
||||
self._orig_builtins[key] = orig
|
||||
del bdict[key]
|
||||
else:
|
||||
self._orig_builtins[key] = orig
|
||||
bdict[key] = value
|
||||
|
||||
def remove_builtin(self, key, orig):
|
||||
"""Remove an added builtin and re-set the original."""
|
||||
if orig is BuiltinUndefined:
|
||||
del builtin_mod.__dict__[key]
|
||||
else:
|
||||
builtin_mod.__dict__[key] = orig
|
||||
|
||||
def activate(self):
|
||||
"""Store ipython references in the __builtin__ namespace."""
|
||||
|
||||
add_builtin = self.add_builtin
|
||||
for name, func in self.auto_builtins.items():
|
||||
add_builtin(name, func)
|
||||
|
||||
def deactivate(self):
|
||||
"""Remove any builtins which might have been added by add_builtins, or
|
||||
restore overwritten ones to their previous values."""
|
||||
remove_builtin = self.remove_builtin
|
||||
for key, val in self._orig_builtins.items():
|
||||
remove_builtin(key, val)
|
||||
self._orig_builtins.clear()
|
||||
self._builtins_added = False
|
214
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/compilerop.py
vendored
Normal file
214
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/compilerop.py
vendored
Normal file
|
@ -0,0 +1,214 @@
|
|||
"""Compiler tools with improved interactive support.
|
||||
|
||||
Provides compilation machinery similar to codeop, but with caching support so
|
||||
we can provide interactive tracebacks.
|
||||
|
||||
Authors
|
||||
-------
|
||||
* Robert Kern
|
||||
* Fernando Perez
|
||||
* Thomas Kluyver
|
||||
"""
|
||||
|
||||
# Note: though it might be more natural to name this module 'compiler', that
|
||||
# name is in the stdlib and name collisions with the stdlib tend to produce
|
||||
# weird problems (often with third-party tools).
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Copyright (C) 2010-2011 The IPython Development Team.
|
||||
#
|
||||
# Distributed under the terms of the BSD License.
|
||||
#
|
||||
# The full license is in the file COPYING.txt, distributed with this software.
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Imports
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
# Stdlib imports
|
||||
import __future__
|
||||
from ast import PyCF_ONLY_AST
|
||||
import codeop
|
||||
import functools
|
||||
import hashlib
|
||||
import linecache
|
||||
import operator
|
||||
import time
|
||||
from contextlib import contextmanager
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Constants
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
# Roughly equal to PyCF_MASK | PyCF_MASK_OBSOLETE as defined in pythonrun.h,
|
||||
# this is used as a bitmask to extract future-related code flags.
|
||||
PyCF_MASK = functools.reduce(operator.or_,
|
||||
(getattr(__future__, fname).compiler_flag
|
||||
for fname in __future__.all_feature_names))
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Local utilities
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
def code_name(code, number=0):
|
||||
""" Compute a (probably) unique name for code for caching.
|
||||
|
||||
This now expects code to be unicode.
|
||||
"""
|
||||
hash_digest = hashlib.sha1(code.encode("utf-8")).hexdigest()
|
||||
# Include the number and 12 characters of the hash in the name. It's
|
||||
# pretty much impossible that in a single session we'll have collisions
|
||||
# even with truncated hashes, and the full one makes tracebacks too long
|
||||
return '<ipython-input-{0}-{1}>'.format(number, hash_digest[:12])
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Classes and functions
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
class CachingCompiler(codeop.Compile):
|
||||
"""A compiler that caches code compiled from interactive statements.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
codeop.Compile.__init__(self)
|
||||
|
||||
# Caching a dictionary { filename: execution_count } for nicely
|
||||
# rendered tracebacks. The filename corresponds to the filename
|
||||
# argument used for the builtins.compile function.
|
||||
self._filename_map = {}
|
||||
|
||||
def ast_parse(self, source, filename='<unknown>', symbol='exec'):
|
||||
"""Parse code to an AST with the current compiler flags active.
|
||||
|
||||
Arguments are exactly the same as ast.parse (in the standard library),
|
||||
and are passed to the built-in compile function."""
|
||||
return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)
|
||||
|
||||
def reset_compiler_flags(self):
|
||||
"""Reset compiler flags to default state."""
|
||||
# This value is copied from codeop.Compile.__init__, so if that ever
|
||||
# changes, it will need to be updated.
|
||||
self.flags = codeop.PyCF_DONT_IMPLY_DEDENT
|
||||
|
||||
@property
|
||||
def compiler_flags(self):
|
||||
"""Flags currently active in the compilation process.
|
||||
"""
|
||||
return self.flags
|
||||
|
||||
def get_code_name(self, raw_code, transformed_code, number):
|
||||
"""Compute filename given the code, and the cell number.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
raw_code : str
|
||||
The raw cell code.
|
||||
transformed_code : str
|
||||
The executable Python source code to cache and compile.
|
||||
number : int
|
||||
A number which forms part of the code's name. Used for the execution
|
||||
counter.
|
||||
|
||||
Returns
|
||||
-------
|
||||
The computed filename.
|
||||
"""
|
||||
return code_name(transformed_code, number)
|
||||
|
||||
def format_code_name(self, name):
|
||||
"""Return a user-friendly label and name for a code block.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
name : str
|
||||
The name for the code block returned from get_code_name
|
||||
|
||||
Returns
|
||||
-------
|
||||
A (label, name) pair that can be used in tracebacks, or None if the default formatting should be used.
|
||||
"""
|
||||
if name in self._filename_map:
|
||||
return "Cell", "In[%s]" % self._filename_map[name]
|
||||
|
||||
def cache(self, transformed_code, number=0, raw_code=None):
|
||||
"""Make a name for a block of code, and cache the code.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
transformed_code : str
|
||||
The executable Python source code to cache and compile.
|
||||
number : int
|
||||
A number which forms part of the code's name. Used for the execution
|
||||
counter.
|
||||
raw_code : str
|
||||
The raw code before transformation, if None, set to `transformed_code`.
|
||||
|
||||
Returns
|
||||
-------
|
||||
The name of the cached code (as a string). Pass this as the filename
|
||||
argument to compilation, so that tracebacks are correctly hooked up.
|
||||
"""
|
||||
if raw_code is None:
|
||||
raw_code = transformed_code
|
||||
|
||||
name = self.get_code_name(raw_code, transformed_code, number)
|
||||
|
||||
# Save the execution count
|
||||
self._filename_map[name] = number
|
||||
|
||||
# Since Python 2.5, setting mtime to `None` means the lines will
|
||||
# never be removed by `linecache.checkcache`. This means all the
|
||||
# monkeypatching has *never* been necessary, since this code was
|
||||
# only added in 2010, at which point IPython had already stopped
|
||||
# supporting Python 2.4.
|
||||
#
|
||||
# Note that `linecache.clearcache` and `linecache.updatecache` may
|
||||
# still remove our code from the cache, but those show explicit
|
||||
# intent, and we should not try to interfere. Normally the former
|
||||
# is never called except when out of memory, and the latter is only
|
||||
# called for lines *not* in the cache.
|
||||
entry = (
|
||||
len(transformed_code),
|
||||
None,
|
||||
[line + "\n" for line in transformed_code.splitlines()],
|
||||
name,
|
||||
)
|
||||
linecache.cache[name] = entry
|
||||
return name
|
||||
|
||||
@contextmanager
|
||||
def extra_flags(self, flags):
|
||||
## bits that we'll set to 1
|
||||
turn_on_bits = ~self.flags & flags
|
||||
|
||||
|
||||
self.flags = self.flags | flags
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
# turn off only the bits we turned on so that something like
|
||||
# __future__ that set flags stays.
|
||||
self.flags &= ~turn_on_bits
|
||||
|
||||
|
||||
def check_linecache_ipython(*args):
|
||||
"""Deprecated since IPython 8.6. Call linecache.checkcache() directly.
|
||||
|
||||
It was already not necessary to call this function directly. If no
|
||||
CachingCompiler had been created, this function would fail badly. If
|
||||
an instance had been created, this function would've been monkeypatched
|
||||
into place.
|
||||
|
||||
As of IPython 8.6, the monkeypatching has gone away entirely. But there
|
||||
were still internal callers of this function, so maybe external callers
|
||||
also existed?
|
||||
"""
|
||||
import warnings
|
||||
|
||||
warnings.warn(
|
||||
"Deprecated Since IPython 8.6, Just call linecache.checkcache() directly.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
linecache.checkcache()
|
3389
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/completer.py
vendored
Normal file
3389
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/completer.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
382
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/completerlib.py
vendored
Normal file
382
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/completerlib.py
vendored
Normal file
|
@ -0,0 +1,382 @@
|
|||
# encoding: utf-8
|
||||
"""Implementations for various useful completers.
|
||||
|
||||
These are all loaded by default by IPython.
|
||||
"""
|
||||
#-----------------------------------------------------------------------------
|
||||
# Copyright (C) 2010-2011 The IPython Development Team.
|
||||
#
|
||||
# Distributed under the terms of the BSD License.
|
||||
#
|
||||
# The full license is in the file COPYING.txt, distributed with this software.
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Imports
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
# Stdlib imports
|
||||
import glob
|
||||
import inspect
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from importlib import import_module
|
||||
from importlib.machinery import all_suffixes
|
||||
|
||||
|
||||
# Third-party imports
|
||||
from time import time
|
||||
from zipimport import zipimporter
|
||||
|
||||
# Our own imports
|
||||
from .completer import expand_user, compress_user
|
||||
from .error import TryNext
|
||||
from ..utils._process_common import arg_split
|
||||
|
||||
# FIXME: this should be pulled in with the right call via the component system
|
||||
from IPython import get_ipython
|
||||
|
||||
from typing import List
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Globals and constants
|
||||
#-----------------------------------------------------------------------------
|
||||
_suffixes = all_suffixes()
|
||||
|
||||
# Time in seconds after which the rootmodules will be stored permanently in the
|
||||
# ipython ip.db database (kept in the user's .ipython dir).
|
||||
TIMEOUT_STORAGE = 2
|
||||
|
||||
# Time in seconds after which we give up
|
||||
TIMEOUT_GIVEUP = 20
|
||||
|
||||
# Regular expression for the python import statement
|
||||
import_re = re.compile(r'(?P<name>[^\W\d]\w*?)'
|
||||
r'(?P<package>[/\\]__init__)?'
|
||||
r'(?P<suffix>%s)$' %
|
||||
r'|'.join(re.escape(s) for s in _suffixes))
|
||||
|
||||
# RE for the ipython %run command (python + ipython scripts)
|
||||
magic_run_re = re.compile(r'.*(\.ipy|\.ipynb|\.py[w]?)$')
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Local utilities
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
def module_list(path: str) -> List[str]:
|
||||
"""
|
||||
Return the list containing the names of the modules available in the given
|
||||
folder.
|
||||
"""
|
||||
# sys.path has the cwd as an empty string, but isdir/listdir need it as '.'
|
||||
if path == '':
|
||||
path = '.'
|
||||
|
||||
# A few local constants to be used in loops below
|
||||
pjoin = os.path.join
|
||||
|
||||
if os.path.isdir(path):
|
||||
# Build a list of all files in the directory and all files
|
||||
# in its subdirectories. For performance reasons, do not
|
||||
# recurse more than one level into subdirectories.
|
||||
files: List[str] = []
|
||||
for root, dirs, nondirs in os.walk(path, followlinks=True):
|
||||
subdir = root[len(path)+1:]
|
||||
if subdir:
|
||||
files.extend(pjoin(subdir, f) for f in nondirs)
|
||||
dirs[:] = [] # Do not recurse into additional subdirectories.
|
||||
else:
|
||||
files.extend(nondirs)
|
||||
|
||||
else:
|
||||
try:
|
||||
files = list(zipimporter(path)._files.keys()) # type: ignore
|
||||
except Exception:
|
||||
files = []
|
||||
|
||||
# Build a list of modules which match the import_re regex.
|
||||
modules = []
|
||||
for f in files:
|
||||
m = import_re.match(f)
|
||||
if m:
|
||||
modules.append(m.group('name'))
|
||||
return list(set(modules))
|
||||
|
||||
|
||||
def get_root_modules():
|
||||
"""
|
||||
Returns a list containing the names of all the modules available in the
|
||||
folders of the pythonpath.
|
||||
|
||||
ip.db['rootmodules_cache'] maps sys.path entries to list of modules.
|
||||
"""
|
||||
ip = get_ipython()
|
||||
if ip is None:
|
||||
# No global shell instance to store cached list of modules.
|
||||
# Don't try to scan for modules every time.
|
||||
return list(sys.builtin_module_names)
|
||||
|
||||
if getattr(ip.db, "_mock", False):
|
||||
rootmodules_cache = {}
|
||||
else:
|
||||
rootmodules_cache = ip.db.get("rootmodules_cache", {})
|
||||
rootmodules = list(sys.builtin_module_names)
|
||||
start_time = time()
|
||||
store = False
|
||||
for path in sys.path:
|
||||
try:
|
||||
modules = rootmodules_cache[path]
|
||||
except KeyError:
|
||||
modules = module_list(path)
|
||||
try:
|
||||
modules.remove('__init__')
|
||||
except ValueError:
|
||||
pass
|
||||
if path not in ('', '.'): # cwd modules should not be cached
|
||||
rootmodules_cache[path] = modules
|
||||
if time() - start_time > TIMEOUT_STORAGE and not store:
|
||||
store = True
|
||||
print("\nCaching the list of root modules, please wait!")
|
||||
print("(This will only be done once - type '%rehashx' to "
|
||||
"reset cache!)\n")
|
||||
sys.stdout.flush()
|
||||
if time() - start_time > TIMEOUT_GIVEUP:
|
||||
print("This is taking too long, we give up.\n")
|
||||
return []
|
||||
rootmodules.extend(modules)
|
||||
if store:
|
||||
ip.db['rootmodules_cache'] = rootmodules_cache
|
||||
rootmodules = list(set(rootmodules))
|
||||
return rootmodules
|
||||
|
||||
|
||||
def is_importable(module, attr: str, only_modules) -> bool:
|
||||
if only_modules:
|
||||
try:
|
||||
mod = getattr(module, attr)
|
||||
except ModuleNotFoundError:
|
||||
# See gh-14434
|
||||
return False
|
||||
return inspect.ismodule(mod)
|
||||
else:
|
||||
return not(attr[:2] == '__' and attr[-2:] == '__')
|
||||
|
||||
def is_possible_submodule(module, attr):
|
||||
try:
|
||||
obj = getattr(module, attr)
|
||||
except AttributeError:
|
||||
# Is possibly an unimported submodule
|
||||
return True
|
||||
except TypeError:
|
||||
# https://github.com/ipython/ipython/issues/9678
|
||||
return False
|
||||
return inspect.ismodule(obj)
|
||||
|
||||
|
||||
def try_import(mod: str, only_modules=False) -> List[str]:
|
||||
"""
|
||||
Try to import given module and return list of potential completions.
|
||||
"""
|
||||
mod = mod.rstrip('.')
|
||||
try:
|
||||
m = import_module(mod)
|
||||
except:
|
||||
return []
|
||||
|
||||
m_is_init = '__init__' in (getattr(m, '__file__', '') or '')
|
||||
|
||||
completions = []
|
||||
if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init:
|
||||
completions.extend( [attr for attr in dir(m) if
|
||||
is_importable(m, attr, only_modules)])
|
||||
|
||||
m_all = getattr(m, "__all__", [])
|
||||
if only_modules:
|
||||
completions.extend(attr for attr in m_all if is_possible_submodule(m, attr))
|
||||
else:
|
||||
completions.extend(m_all)
|
||||
|
||||
if m_is_init:
|
||||
file_ = m.__file__
|
||||
file_path = os.path.dirname(file_) # type: ignore
|
||||
if file_path is not None:
|
||||
completions.extend(module_list(file_path))
|
||||
completions_set = {c for c in completions if isinstance(c, str)}
|
||||
completions_set.discard('__init__')
|
||||
return list(completions_set)
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Completion-related functions.
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
def quick_completer(cmd, completions):
|
||||
r""" Easily create a trivial completer for a command.
|
||||
|
||||
Takes either a list of completions, or all completions in string (that will
|
||||
be split on whitespace).
|
||||
|
||||
Example::
|
||||
|
||||
[d:\ipython]|1> import ipy_completers
|
||||
[d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz'])
|
||||
[d:\ipython]|3> foo b<TAB>
|
||||
bar baz
|
||||
[d:\ipython]|3> foo ba
|
||||
"""
|
||||
|
||||
if isinstance(completions, str):
|
||||
completions = completions.split()
|
||||
|
||||
def do_complete(self, event):
|
||||
return completions
|
||||
|
||||
get_ipython().set_hook('complete_command',do_complete, str_key = cmd)
|
||||
|
||||
def module_completion(line):
|
||||
"""
|
||||
Returns a list containing the completion possibilities for an import line.
|
||||
|
||||
The line looks like this :
|
||||
'import xml.d'
|
||||
'from xml.dom import'
|
||||
"""
|
||||
|
||||
words = line.split(' ')
|
||||
nwords = len(words)
|
||||
|
||||
# from whatever <tab> -> 'import '
|
||||
if nwords == 3 and words[0] == 'from':
|
||||
return ['import ']
|
||||
|
||||
# 'from xy<tab>' or 'import xy<tab>'
|
||||
if nwords < 3 and (words[0] in {'%aimport', 'import', 'from'}) :
|
||||
if nwords == 1:
|
||||
return get_root_modules()
|
||||
mod = words[1].split('.')
|
||||
if len(mod) < 2:
|
||||
return get_root_modules()
|
||||
completion_list = try_import('.'.join(mod[:-1]), True)
|
||||
return ['.'.join(mod[:-1] + [el]) for el in completion_list]
|
||||
|
||||
# 'from xyz import abc<tab>'
|
||||
if nwords >= 3 and words[0] == 'from':
|
||||
mod = words[1]
|
||||
return try_import(mod)
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Completers
|
||||
#-----------------------------------------------------------------------------
|
||||
# These all have the func(self, event) signature to be used as custom
|
||||
# completers
|
||||
|
||||
def module_completer(self,event):
|
||||
"""Give completions after user has typed 'import ...' or 'from ...'"""
|
||||
|
||||
# This works in all versions of python. While 2.5 has
|
||||
# pkgutil.walk_packages(), that particular routine is fairly dangerous,
|
||||
# since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full
|
||||
# of possibly problematic side effects.
|
||||
# This search the folders in the sys.path for available modules.
|
||||
|
||||
return module_completion(event.line)
|
||||
|
||||
# FIXME: there's a lot of logic common to the run, cd and builtin file
|
||||
# completers, that is currently reimplemented in each.
|
||||
|
||||
def magic_run_completer(self, event):
|
||||
"""Complete files that end in .py or .ipy or .ipynb for the %run command.
|
||||
"""
|
||||
comps = arg_split(event.line, strict=False)
|
||||
# relpath should be the current token that we need to complete.
|
||||
if (len(comps) > 1) and (not event.line.endswith(' ')):
|
||||
relpath = comps[-1].strip("'\"")
|
||||
else:
|
||||
relpath = ''
|
||||
|
||||
#print("\nev=", event) # dbg
|
||||
#print("rp=", relpath) # dbg
|
||||
#print('comps=', comps) # dbg
|
||||
|
||||
lglob = glob.glob
|
||||
isdir = os.path.isdir
|
||||
relpath, tilde_expand, tilde_val = expand_user(relpath)
|
||||
|
||||
# Find if the user has already typed the first filename, after which we
|
||||
# should complete on all files, since after the first one other files may
|
||||
# be arguments to the input script.
|
||||
|
||||
if any(magic_run_re.match(c) for c in comps):
|
||||
matches = [f.replace('\\','/') + ('/' if isdir(f) else '')
|
||||
for f in lglob(relpath+'*')]
|
||||
else:
|
||||
dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)]
|
||||
pys = [f.replace('\\','/')
|
||||
for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') +
|
||||
lglob(relpath+'*.ipynb') + lglob(relpath + '*.pyw')]
|
||||
|
||||
matches = dirs + pys
|
||||
|
||||
#print('run comp:', dirs+pys) # dbg
|
||||
return [compress_user(p, tilde_expand, tilde_val) for p in matches]
|
||||
|
||||
|
||||
def cd_completer(self, event):
|
||||
"""Completer function for cd, which only returns directories."""
|
||||
ip = get_ipython()
|
||||
relpath = event.symbol
|
||||
|
||||
#print(event) # dbg
|
||||
if event.line.endswith('-b') or ' -b ' in event.line:
|
||||
# return only bookmark completions
|
||||
bkms = self.db.get('bookmarks', None)
|
||||
if bkms:
|
||||
return bkms.keys()
|
||||
else:
|
||||
return []
|
||||
|
||||
if event.symbol == '-':
|
||||
width_dh = str(len(str(len(ip.user_ns['_dh']) + 1)))
|
||||
# jump in directory history by number
|
||||
fmt = '-%0' + width_dh +'d [%s]'
|
||||
ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
|
||||
if len(ents) > 1:
|
||||
return ents
|
||||
return []
|
||||
|
||||
if event.symbol.startswith('--'):
|
||||
return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']]
|
||||
|
||||
# Expand ~ in path and normalize directory separators.
|
||||
relpath, tilde_expand, tilde_val = expand_user(relpath)
|
||||
relpath = relpath.replace('\\','/')
|
||||
|
||||
found = []
|
||||
for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*')
|
||||
if os.path.isdir(f)]:
|
||||
if ' ' in d:
|
||||
# we don't want to deal with any of that, complex code
|
||||
# for this is elsewhere
|
||||
raise TryNext
|
||||
|
||||
found.append(d)
|
||||
|
||||
if not found:
|
||||
if os.path.isdir(relpath):
|
||||
return [compress_user(relpath, tilde_expand, tilde_val)]
|
||||
|
||||
# if no completions so far, try bookmarks
|
||||
bks = self.db.get('bookmarks',{})
|
||||
bkmatches = [s for s in bks if s.startswith(event.symbol)]
|
||||
if bkmatches:
|
||||
return bkmatches
|
||||
|
||||
raise TryNext
|
||||
|
||||
return [compress_user(p, tilde_expand, tilde_val) for p in found]
|
||||
|
||||
def reset_completer(self, event):
|
||||
"A completer for %reset magic"
|
||||
return '-f -s in out array dhist'.split()
|
236
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/crashhandler.py
vendored
Normal file
236
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/crashhandler.py
vendored
Normal file
|
@ -0,0 +1,236 @@
|
|||
# encoding: utf-8
|
||||
"""sys.excepthook for IPython itself, leaves a detailed report on disk.
|
||||
|
||||
Authors:
|
||||
|
||||
* Fernando Perez
|
||||
* Brian E. Granger
|
||||
"""
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
|
||||
# Copyright (C) 2008-2011 The IPython Development Team
|
||||
#
|
||||
# Distributed under the terms of the BSD License. The full license is in
|
||||
# the file COPYING, distributed as part of this software.
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Imports
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
import sys
|
||||
import traceback
|
||||
from pprint import pformat
|
||||
from pathlib import Path
|
||||
|
||||
from IPython.core import ultratb
|
||||
from IPython.core.release import author_email
|
||||
from IPython.utils.sysinfo import sys_info
|
||||
from IPython.utils.py3compat import input
|
||||
|
||||
from IPython.core.release import __version__ as version
|
||||
|
||||
from typing import Optional
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Code
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
# Template for the user message.
|
||||
_default_message_template = """\
|
||||
Oops, {app_name} crashed. We do our best to make it stable, but...
|
||||
|
||||
A crash report was automatically generated with the following information:
|
||||
- A verbatim copy of the crash traceback.
|
||||
- A copy of your input history during this session.
|
||||
- Data on your current {app_name} configuration.
|
||||
|
||||
It was left in the file named:
|
||||
\t'{crash_report_fname}'
|
||||
If you can email this file to the developers, the information in it will help
|
||||
them in understanding and correcting the problem.
|
||||
|
||||
You can mail it to: {contact_name} at {contact_email}
|
||||
with the subject '{app_name} Crash Report'.
|
||||
|
||||
If you want to do it now, the following command will work (under Unix):
|
||||
mail -s '{app_name} Crash Report' {contact_email} < {crash_report_fname}
|
||||
|
||||
In your email, please also include information about:
|
||||
- The operating system under which the crash happened: Linux, macOS, Windows,
|
||||
other, and which exact version (for example: Ubuntu 16.04.3, macOS 10.13.2,
|
||||
Windows 10 Pro), and whether it is 32-bit or 64-bit;
|
||||
- How {app_name} was installed: using pip or conda, from GitHub, as part of
|
||||
a Docker container, or other, providing more detail if possible;
|
||||
- How to reproduce the crash: what exact sequence of instructions can one
|
||||
input to get the same crash? Ideally, find a minimal yet complete sequence
|
||||
of instructions that yields the crash.
|
||||
|
||||
To ensure accurate tracking of this issue, please file a report about it at:
|
||||
{bug_tracker}
|
||||
"""
|
||||
|
||||
_lite_message_template = """
|
||||
If you suspect this is an IPython {version} bug, please report it at:
|
||||
https://github.com/ipython/ipython/issues
|
||||
or send an email to the mailing list at {email}
|
||||
|
||||
You can print a more detailed traceback right now with "%tb", or use "%debug"
|
||||
to interactively debug it.
|
||||
|
||||
Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
|
||||
{config}Application.verbose_crash=True
|
||||
"""
|
||||
|
||||
|
||||
class CrashHandler(object):
|
||||
"""Customizable crash handlers for IPython applications.
|
||||
|
||||
Instances of this class provide a :meth:`__call__` method which can be
|
||||
used as a ``sys.excepthook``. The :meth:`__call__` signature is::
|
||||
|
||||
def __call__(self, etype, evalue, etb)
|
||||
"""
|
||||
|
||||
message_template = _default_message_template
|
||||
section_sep = '\n\n'+'*'*75+'\n\n'
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
app,
|
||||
contact_name: Optional[str] = None,
|
||||
contact_email: Optional[str] = None,
|
||||
bug_tracker: Optional[str] = None,
|
||||
show_crash_traceback: bool = True,
|
||||
call_pdb: bool = False,
|
||||
):
|
||||
"""Create a new crash handler
|
||||
|
||||
Parameters
|
||||
----------
|
||||
app : Application
|
||||
A running :class:`Application` instance, which will be queried at
|
||||
crash time for internal information.
|
||||
contact_name : str
|
||||
A string with the name of the person to contact.
|
||||
contact_email : str
|
||||
A string with the email address of the contact.
|
||||
bug_tracker : str
|
||||
A string with the URL for your project's bug tracker.
|
||||
show_crash_traceback : bool
|
||||
If false, don't print the crash traceback on stderr, only generate
|
||||
the on-disk report
|
||||
call_pdb
|
||||
Whether to call pdb on crash
|
||||
|
||||
Attributes
|
||||
----------
|
||||
These instances contain some non-argument attributes which allow for
|
||||
further customization of the crash handler's behavior. Please see the
|
||||
source for further details.
|
||||
|
||||
"""
|
||||
self.crash_report_fname = "Crash_report_%s.txt" % app.name
|
||||
self.app = app
|
||||
self.call_pdb = call_pdb
|
||||
#self.call_pdb = True # dbg
|
||||
self.show_crash_traceback = show_crash_traceback
|
||||
self.info = dict(app_name = app.name,
|
||||
contact_name = contact_name,
|
||||
contact_email = contact_email,
|
||||
bug_tracker = bug_tracker,
|
||||
crash_report_fname = self.crash_report_fname)
|
||||
|
||||
|
||||
def __call__(self, etype, evalue, etb):
|
||||
"""Handle an exception, call for compatible with sys.excepthook"""
|
||||
|
||||
# do not allow the crash handler to be called twice without reinstalling it
|
||||
# this prevents unlikely errors in the crash handling from entering an
|
||||
# infinite loop.
|
||||
sys.excepthook = sys.__excepthook__
|
||||
|
||||
# Report tracebacks shouldn't use color in general (safer for users)
|
||||
color_scheme = 'NoColor'
|
||||
|
||||
# Use this ONLY for developer debugging (keep commented out for release)
|
||||
#color_scheme = 'Linux' # dbg
|
||||
try:
|
||||
rptdir = self.app.ipython_dir
|
||||
except:
|
||||
rptdir = Path.cwd()
|
||||
if rptdir is None or not Path.is_dir(rptdir):
|
||||
rptdir = Path.cwd()
|
||||
report_name = rptdir / self.crash_report_fname
|
||||
# write the report filename into the instance dict so it can get
|
||||
# properly expanded out in the user message template
|
||||
self.crash_report_fname = report_name
|
||||
self.info['crash_report_fname'] = report_name
|
||||
TBhandler = ultratb.VerboseTB(
|
||||
color_scheme=color_scheme,
|
||||
long_header=1,
|
||||
call_pdb=self.call_pdb,
|
||||
)
|
||||
if self.call_pdb:
|
||||
TBhandler(etype,evalue,etb)
|
||||
return
|
||||
else:
|
||||
traceback = TBhandler.text(etype,evalue,etb,context=31)
|
||||
|
||||
# print traceback to screen
|
||||
if self.show_crash_traceback:
|
||||
print(traceback, file=sys.stderr)
|
||||
|
||||
# and generate a complete report on disk
|
||||
try:
|
||||
report = open(report_name, "w", encoding="utf-8")
|
||||
except:
|
||||
print('Could not create crash report on disk.', file=sys.stderr)
|
||||
return
|
||||
|
||||
with report:
|
||||
# Inform user on stderr of what happened
|
||||
print('\n'+'*'*70+'\n', file=sys.stderr)
|
||||
print(self.message_template.format(**self.info), file=sys.stderr)
|
||||
|
||||
# Construct report on disk
|
||||
report.write(self.make_report(traceback))
|
||||
|
||||
input("Hit <Enter> to quit (your terminal may close):")
|
||||
|
||||
def make_report(self,traceback):
|
||||
"""Return a string containing a crash report."""
|
||||
|
||||
sec_sep = self.section_sep
|
||||
|
||||
report = ['*'*75+'\n\n'+'IPython post-mortem report\n\n']
|
||||
rpt_add = report.append
|
||||
rpt_add(sys_info())
|
||||
|
||||
try:
|
||||
config = pformat(self.app.config)
|
||||
rpt_add(sec_sep)
|
||||
rpt_add('Application name: %s\n\n' % self.app_name)
|
||||
rpt_add('Current user configuration structure:\n\n')
|
||||
rpt_add(config)
|
||||
except:
|
||||
pass
|
||||
rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
|
||||
|
||||
return ''.join(report)
|
||||
|
||||
|
||||
def crash_handler_lite(etype, evalue, tb):
|
||||
"""a light excepthook, adding a small message to the usual traceback"""
|
||||
traceback.print_exception(etype, evalue, tb)
|
||||
|
||||
from IPython.core.interactiveshell import InteractiveShell
|
||||
if InteractiveShell.initialized():
|
||||
# we are in a Shell environment, give %magic example
|
||||
config = "%config "
|
||||
else:
|
||||
# we are not in a shell, show generic config
|
||||
config = "c."
|
||||
print(_lite_message_template.format(email=author_email, config=config, version=version), file=sys.stderr)
|
||||
|
1136
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/debugger.py
vendored
Normal file
1136
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/debugger.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1370
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/display.py
vendored
Normal file
1370
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/display.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
391
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/display_functions.py
vendored
Normal file
391
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/display_functions.py
vendored
Normal file
|
@ -0,0 +1,391 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""Top-level display functions for displaying object in different formats."""
|
||||
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
|
||||
from binascii import b2a_hex
|
||||
import os
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
__all__ = ['display', 'clear_output', 'publish_display_data', 'update_display', 'DisplayHandle']
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# utility functions
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _merge(d1, d2):
|
||||
"""Like update, but merges sub-dicts instead of clobbering at the top level.
|
||||
|
||||
Updates d1 in-place
|
||||
"""
|
||||
|
||||
if not isinstance(d2, dict) or not isinstance(d1, dict):
|
||||
return d2
|
||||
for key, value in d2.items():
|
||||
d1[key] = _merge(d1.get(key), value)
|
||||
return d1
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Main functions
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
class _Sentinel:
|
||||
def __repr__(self):
|
||||
return "<deprecated>"
|
||||
|
||||
|
||||
_sentinel = _Sentinel()
|
||||
|
||||
# use * to indicate transient is keyword-only
|
||||
def publish_display_data(
|
||||
data, metadata=None, source=_sentinel, *, transient=None, **kwargs
|
||||
):
|
||||
"""Publish data and metadata to all frontends.
|
||||
|
||||
See the ``display_data`` message in the messaging documentation for
|
||||
more details about this message type.
|
||||
|
||||
Keys of data and metadata can be any mime-type.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
data : dict
|
||||
A dictionary having keys that are valid MIME types (like
|
||||
'text/plain' or 'image/svg+xml') and values that are the data for
|
||||
that MIME type. The data itself must be a JSON'able data
|
||||
structure. Minimally all data should have the 'text/plain' data,
|
||||
which can be displayed by all frontends. If more than the plain
|
||||
text is given, it is up to the frontend to decide which
|
||||
representation to use.
|
||||
metadata : dict
|
||||
A dictionary for metadata related to the data. This can contain
|
||||
arbitrary key, value pairs that frontends can use to interpret
|
||||
the data. mime-type keys matching those in data can be used
|
||||
to specify metadata about particular representations.
|
||||
source : str, deprecated
|
||||
Unused.
|
||||
transient : dict, keyword-only
|
||||
A dictionary of transient data, such as display_id.
|
||||
"""
|
||||
from IPython.core.interactiveshell import InteractiveShell
|
||||
|
||||
if source is not _sentinel:
|
||||
warnings.warn(
|
||||
"The `source` parameter emit a deprecation warning since"
|
||||
" IPython 8.0, it had no effects for a long time and will "
|
||||
" be removed in future versions.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
display_pub = InteractiveShell.instance().display_pub
|
||||
|
||||
# only pass transient if supplied,
|
||||
# to avoid errors with older ipykernel.
|
||||
# TODO: We could check for ipykernel version and provide a detailed upgrade message.
|
||||
if transient:
|
||||
kwargs['transient'] = transient
|
||||
|
||||
display_pub.publish(
|
||||
data=data,
|
||||
metadata=metadata,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
|
||||
def _new_id():
|
||||
"""Generate a new random text id with urandom"""
|
||||
return b2a_hex(os.urandom(16)).decode('ascii')
|
||||
|
||||
|
||||
def display(
|
||||
*objs,
|
||||
include=None,
|
||||
exclude=None,
|
||||
metadata=None,
|
||||
transient=None,
|
||||
display_id=None,
|
||||
raw=False,
|
||||
clear=False,
|
||||
**kwargs,
|
||||
):
|
||||
"""Display a Python object in all frontends.
|
||||
|
||||
By default all representations will be computed and sent to the frontends.
|
||||
Frontends can decide which representation is used and how.
|
||||
|
||||
In terminal IPython this will be similar to using :func:`print`, for use in richer
|
||||
frontends see Jupyter notebook examples with rich display logic.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
*objs : object
|
||||
The Python objects to display.
|
||||
raw : bool, optional
|
||||
Are the objects to be displayed already mimetype-keyed dicts of raw display data,
|
||||
or Python objects that need to be formatted before display? [default: False]
|
||||
include : list, tuple or set, optional
|
||||
A list of format type strings (MIME types) to include in the
|
||||
format data dict. If this is set *only* the format types included
|
||||
in this list will be computed.
|
||||
exclude : list, tuple or set, optional
|
||||
A list of format type strings (MIME types) to exclude in the format
|
||||
data dict. If this is set all format types will be computed,
|
||||
except for those included in this argument.
|
||||
metadata : dict, optional
|
||||
A dictionary of metadata to associate with the output.
|
||||
mime-type keys in this dictionary will be associated with the individual
|
||||
representation formats, if they exist.
|
||||
transient : dict, optional
|
||||
A dictionary of transient data to associate with the output.
|
||||
Data in this dict should not be persisted to files (e.g. notebooks).
|
||||
display_id : str, bool optional
|
||||
Set an id for the display.
|
||||
This id can be used for updating this display area later via update_display.
|
||||
If given as `True`, generate a new `display_id`
|
||||
clear : bool, optional
|
||||
Should the output area be cleared before displaying anything? If True,
|
||||
this will wait for additional output before clearing. [default: False]
|
||||
**kwargs : additional keyword-args, optional
|
||||
Additional keyword-arguments are passed through to the display publisher.
|
||||
|
||||
Returns
|
||||
-------
|
||||
handle: DisplayHandle
|
||||
Returns a handle on updatable displays for use with :func:`update_display`,
|
||||
if `display_id` is given. Returns :any:`None` if no `display_id` is given
|
||||
(default).
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> class Json(object):
|
||||
... def __init__(self, json):
|
||||
... self.json = json
|
||||
... def _repr_pretty_(self, pp, cycle):
|
||||
... import json
|
||||
... pp.text(json.dumps(self.json, indent=2))
|
||||
... def __repr__(self):
|
||||
... return str(self.json)
|
||||
...
|
||||
|
||||
>>> d = Json({1:2, 3: {4:5}})
|
||||
|
||||
>>> print(d)
|
||||
{1: 2, 3: {4: 5}}
|
||||
|
||||
>>> display(d)
|
||||
{
|
||||
"1": 2,
|
||||
"3": {
|
||||
"4": 5
|
||||
}
|
||||
}
|
||||
|
||||
>>> def int_formatter(integer, pp, cycle):
|
||||
... pp.text('I'*integer)
|
||||
|
||||
>>> plain = get_ipython().display_formatter.formatters['text/plain']
|
||||
>>> plain.for_type(int, int_formatter)
|
||||
<function _repr_pprint at 0x...>
|
||||
>>> display(7-5)
|
||||
II
|
||||
|
||||
>>> del plain.type_printers[int]
|
||||
>>> display(7-5)
|
||||
2
|
||||
|
||||
See Also
|
||||
--------
|
||||
:func:`update_display`
|
||||
|
||||
Notes
|
||||
-----
|
||||
In Python, objects can declare their textual representation using the
|
||||
`__repr__` method. IPython expands on this idea and allows objects to declare
|
||||
other, rich representations including:
|
||||
|
||||
- HTML
|
||||
- JSON
|
||||
- PNG
|
||||
- JPEG
|
||||
- SVG
|
||||
- LaTeX
|
||||
|
||||
A single object can declare some or all of these representations; all are
|
||||
handled by IPython's display system.
|
||||
|
||||
The main idea of the first approach is that you have to implement special
|
||||
display methods when you define your class, one for each representation you
|
||||
want to use. Here is a list of the names of the special methods and the
|
||||
values they must return:
|
||||
|
||||
- `_repr_html_`: return raw HTML as a string, or a tuple (see below).
|
||||
- `_repr_json_`: return a JSONable dict, or a tuple (see below).
|
||||
- `_repr_jpeg_`: return raw JPEG data, or a tuple (see below).
|
||||
- `_repr_png_`: return raw PNG data, or a tuple (see below).
|
||||
- `_repr_svg_`: return raw SVG data as a string, or a tuple (see below).
|
||||
- `_repr_latex_`: return LaTeX commands in a string surrounded by "$",
|
||||
or a tuple (see below).
|
||||
- `_repr_mimebundle_`: return a full mimebundle containing the mapping
|
||||
from all mimetypes to data.
|
||||
Use this for any mime-type not listed above.
|
||||
|
||||
The above functions may also return the object's metadata alonside the
|
||||
data. If the metadata is available, the functions will return a tuple
|
||||
containing the data and metadata, in that order. If there is no metadata
|
||||
available, then the functions will return the data only.
|
||||
|
||||
When you are directly writing your own classes, you can adapt them for
|
||||
display in IPython by following the above approach. But in practice, you
|
||||
often need to work with existing classes that you can't easily modify.
|
||||
|
||||
You can refer to the documentation on integrating with the display system in
|
||||
order to register custom formatters for already existing types
|
||||
(:ref:`integrating_rich_display`).
|
||||
|
||||
.. versionadded:: 5.4 display available without import
|
||||
.. versionadded:: 6.1 display available without import
|
||||
|
||||
Since IPython 5.4 and 6.1 :func:`display` is automatically made available to
|
||||
the user without import. If you are using display in a document that might
|
||||
be used in a pure python context or with older version of IPython, use the
|
||||
following import at the top of your file::
|
||||
|
||||
from IPython.display import display
|
||||
|
||||
"""
|
||||
from IPython.core.interactiveshell import InteractiveShell
|
||||
|
||||
if not InteractiveShell.initialized():
|
||||
# Directly print objects.
|
||||
print(*objs)
|
||||
return
|
||||
|
||||
if transient is None:
|
||||
transient = {}
|
||||
if metadata is None:
|
||||
metadata={}
|
||||
if display_id:
|
||||
if display_id is True:
|
||||
display_id = _new_id()
|
||||
transient['display_id'] = display_id
|
||||
if kwargs.get('update') and 'display_id' not in transient:
|
||||
raise TypeError('display_id required for update_display')
|
||||
if transient:
|
||||
kwargs['transient'] = transient
|
||||
|
||||
if not objs and display_id:
|
||||
# if given no objects, but still a request for a display_id,
|
||||
# we assume the user wants to insert an empty output that
|
||||
# can be updated later
|
||||
objs = [{}]
|
||||
raw = True
|
||||
|
||||
if not raw:
|
||||
format = InteractiveShell.instance().display_formatter.format
|
||||
|
||||
if clear:
|
||||
clear_output(wait=True)
|
||||
|
||||
for obj in objs:
|
||||
if raw:
|
||||
publish_display_data(data=obj, metadata=metadata, **kwargs)
|
||||
else:
|
||||
format_dict, md_dict = format(obj, include=include, exclude=exclude)
|
||||
if not format_dict:
|
||||
# nothing to display (e.g. _ipython_display_ took over)
|
||||
continue
|
||||
if metadata:
|
||||
# kwarg-specified metadata gets precedence
|
||||
_merge(md_dict, metadata)
|
||||
publish_display_data(data=format_dict, metadata=md_dict, **kwargs)
|
||||
if display_id:
|
||||
return DisplayHandle(display_id)
|
||||
|
||||
|
||||
# use * for keyword-only display_id arg
|
||||
def update_display(obj, *, display_id, **kwargs):
|
||||
"""Update an existing display by id
|
||||
|
||||
Parameters
|
||||
----------
|
||||
obj
|
||||
The object with which to update the display
|
||||
display_id : keyword-only
|
||||
The id of the display to update
|
||||
|
||||
See Also
|
||||
--------
|
||||
:func:`display`
|
||||
"""
|
||||
kwargs['update'] = True
|
||||
display(obj, display_id=display_id, **kwargs)
|
||||
|
||||
|
||||
class DisplayHandle(object):
|
||||
"""A handle on an updatable display
|
||||
|
||||
Call `.update(obj)` to display a new object.
|
||||
|
||||
Call `.display(obj`) to add a new instance of this display,
|
||||
and update existing instances.
|
||||
|
||||
See Also
|
||||
--------
|
||||
|
||||
:func:`display`, :func:`update_display`
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, display_id=None):
|
||||
if display_id is None:
|
||||
display_id = _new_id()
|
||||
self.display_id = display_id
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s display_id=%s>" % (self.__class__.__name__, self.display_id)
|
||||
|
||||
def display(self, obj, **kwargs):
|
||||
"""Make a new display with my id, updating existing instances.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
obj
|
||||
object to display
|
||||
**kwargs
|
||||
additional keyword arguments passed to display
|
||||
"""
|
||||
display(obj, display_id=self.display_id, **kwargs)
|
||||
|
||||
def update(self, obj, **kwargs):
|
||||
"""Update existing displays with my id
|
||||
|
||||
Parameters
|
||||
----------
|
||||
obj
|
||||
object to display
|
||||
**kwargs
|
||||
additional keyword arguments passed to update_display
|
||||
"""
|
||||
update_display(obj, display_id=self.display_id, **kwargs)
|
||||
|
||||
|
||||
def clear_output(wait=False):
|
||||
"""Clear the output of the current cell receiving output.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
wait : bool [default: false]
|
||||
Wait to clear the output until new output is available to replace it."""
|
||||
from IPython.core.interactiveshell import InteractiveShell
|
||||
if InteractiveShell.initialized():
|
||||
InteractiveShell.instance().display_pub.clear_output(wait)
|
||||
else:
|
||||
print('\033[2K\r', end='')
|
||||
sys.stdout.flush()
|
||||
print('\033[2K\r', end='')
|
||||
sys.stderr.flush()
|
70
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/display_trap.py
vendored
Normal file
70
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/display_trap.py
vendored
Normal file
|
@ -0,0 +1,70 @@
|
|||
# encoding: utf-8
|
||||
"""
|
||||
A context manager for handling sys.displayhook.
|
||||
|
||||
Authors:
|
||||
|
||||
* Robert Kern
|
||||
* Brian Granger
|
||||
"""
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Copyright (C) 2008-2011 The IPython Development Team
|
||||
#
|
||||
# Distributed under the terms of the BSD License. The full license is in
|
||||
# the file COPYING, distributed as part of this software.
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Imports
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
import sys
|
||||
|
||||
from traitlets.config.configurable import Configurable
|
||||
from traitlets import Any
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Classes and functions
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
class DisplayTrap(Configurable):
|
||||
"""Object to manage sys.displayhook.
|
||||
|
||||
This came from IPython.core.kernel.display_hook, but is simplified
|
||||
(no callbacks or formatters) until more of the core is refactored.
|
||||
"""
|
||||
|
||||
hook = Any()
|
||||
|
||||
def __init__(self, hook=None):
|
||||
super(DisplayTrap, self).__init__(hook=hook, config=None)
|
||||
self.old_hook = None
|
||||
# We define this to track if a single BuiltinTrap is nested.
|
||||
# Only turn off the trap when the outermost call to __exit__ is made.
|
||||
self._nested_level = 0
|
||||
|
||||
def __enter__(self):
|
||||
if self._nested_level == 0:
|
||||
self.set()
|
||||
self._nested_level += 1
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
if self._nested_level == 1:
|
||||
self.unset()
|
||||
self._nested_level -= 1
|
||||
# Returning False will cause exceptions to propagate
|
||||
return False
|
||||
|
||||
def set(self):
|
||||
"""Set the hook."""
|
||||
if sys.displayhook is not self.hook:
|
||||
self.old_hook = sys.displayhook
|
||||
sys.displayhook = self.hook
|
||||
|
||||
def unset(self):
|
||||
"""Unset the hook."""
|
||||
sys.displayhook = self.old_hook
|
||||
|
336
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/displayhook.py
vendored
Normal file
336
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/displayhook.py
vendored
Normal file
|
@ -0,0 +1,336 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""Displayhook for IPython.
|
||||
|
||||
This defines a callable class that IPython uses for `sys.displayhook`.
|
||||
"""
|
||||
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
import builtins as builtin_mod
|
||||
import sys
|
||||
import io as _io
|
||||
import tokenize
|
||||
|
||||
from traitlets.config.configurable import Configurable
|
||||
from traitlets import Instance, Float
|
||||
from warnings import warn
|
||||
|
||||
# TODO: Move the various attributes (cache_size, [others now moved]). Some
|
||||
# of these are also attributes of InteractiveShell. They should be on ONE object
|
||||
# only and the other objects should ask that one object for their values.
|
||||
|
||||
class DisplayHook(Configurable):
|
||||
"""The custom IPython displayhook to replace sys.displayhook.
|
||||
|
||||
This class does many things, but the basic idea is that it is a callable
|
||||
that gets called anytime user code returns a value.
|
||||
"""
|
||||
|
||||
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
|
||||
allow_none=True)
|
||||
exec_result = Instance('IPython.core.interactiveshell.ExecutionResult',
|
||||
allow_none=True)
|
||||
cull_fraction = Float(0.2)
|
||||
|
||||
def __init__(self, shell=None, cache_size=1000, **kwargs):
|
||||
super(DisplayHook, self).__init__(shell=shell, **kwargs)
|
||||
cache_size_min = 3
|
||||
if cache_size <= 0:
|
||||
self.do_full_cache = 0
|
||||
cache_size = 0
|
||||
elif cache_size < cache_size_min:
|
||||
self.do_full_cache = 0
|
||||
cache_size = 0
|
||||
warn('caching was disabled (min value for cache size is %s).' %
|
||||
cache_size_min,stacklevel=3)
|
||||
else:
|
||||
self.do_full_cache = 1
|
||||
|
||||
self.cache_size = cache_size
|
||||
|
||||
# we need a reference to the user-level namespace
|
||||
self.shell = shell
|
||||
|
||||
self._,self.__,self.___ = '','',''
|
||||
|
||||
# these are deliberately global:
|
||||
to_user_ns = {'_':self._,'__':self.__,'___':self.___}
|
||||
self.shell.user_ns.update(to_user_ns)
|
||||
|
||||
@property
|
||||
def prompt_count(self):
|
||||
return self.shell.execution_count
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
# Methods used in __call__. Override these methods to modify the behavior
|
||||
# of the displayhook.
|
||||
#-------------------------------------------------------------------------
|
||||
|
||||
def check_for_underscore(self):
|
||||
"""Check if the user has set the '_' variable by hand."""
|
||||
# If something injected a '_' variable in __builtin__, delete
|
||||
# ipython's automatic one so we don't clobber that. gettext() in
|
||||
# particular uses _, so we need to stay away from it.
|
||||
if '_' in builtin_mod.__dict__:
|
||||
try:
|
||||
user_value = self.shell.user_ns['_']
|
||||
if user_value is not self._:
|
||||
return
|
||||
del self.shell.user_ns['_']
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
def quiet(self):
|
||||
"""Should we silence the display hook because of ';'?"""
|
||||
# do not print output if input ends in ';'
|
||||
|
||||
try:
|
||||
cell = self.shell.history_manager.input_hist_parsed[-1]
|
||||
except IndexError:
|
||||
# some uses of ipshellembed may fail here
|
||||
return False
|
||||
|
||||
return self.semicolon_at_end_of_expression(cell)
|
||||
|
||||
@staticmethod
|
||||
def semicolon_at_end_of_expression(expression):
|
||||
"""Parse Python expression and detects whether last token is ';'"""
|
||||
|
||||
sio = _io.StringIO(expression)
|
||||
tokens = list(tokenize.generate_tokens(sio.readline))
|
||||
|
||||
for token in reversed(tokens):
|
||||
if token[0] in (tokenize.ENDMARKER, tokenize.NL, tokenize.NEWLINE, tokenize.COMMENT):
|
||||
continue
|
||||
if (token[0] == tokenize.OP) and (token[1] == ';'):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def start_displayhook(self):
|
||||
"""Start the displayhook, initializing resources."""
|
||||
pass
|
||||
|
||||
def write_output_prompt(self):
|
||||
"""Write the output prompt.
|
||||
|
||||
The default implementation simply writes the prompt to
|
||||
``sys.stdout``.
|
||||
"""
|
||||
# Use write, not print which adds an extra space.
|
||||
sys.stdout.write(self.shell.separate_out)
|
||||
outprompt = 'Out[{}]: '.format(self.shell.execution_count)
|
||||
if self.do_full_cache:
|
||||
sys.stdout.write(outprompt)
|
||||
|
||||
def compute_format_data(self, result):
|
||||
"""Compute format data of the object to be displayed.
|
||||
|
||||
The format data is a generalization of the :func:`repr` of an object.
|
||||
In the default implementation the format data is a :class:`dict` of
|
||||
key value pair where the keys are valid MIME types and the values
|
||||
are JSON'able data structure containing the raw data for that MIME
|
||||
type. It is up to frontends to determine pick a MIME to to use and
|
||||
display that data in an appropriate manner.
|
||||
|
||||
This method only computes the format data for the object and should
|
||||
NOT actually print or write that to a stream.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
result : object
|
||||
The Python object passed to the display hook, whose format will be
|
||||
computed.
|
||||
|
||||
Returns
|
||||
-------
|
||||
(format_dict, md_dict) : dict
|
||||
format_dict is a :class:`dict` whose keys are valid MIME types and values are
|
||||
JSON'able raw data for that MIME type. It is recommended that
|
||||
all return values of this should always include the "text/plain"
|
||||
MIME type representation of the object.
|
||||
md_dict is a :class:`dict` with the same MIME type keys
|
||||
of metadata associated with each output.
|
||||
|
||||
"""
|
||||
return self.shell.display_formatter.format(result)
|
||||
|
||||
# This can be set to True by the write_output_prompt method in a subclass
|
||||
prompt_end_newline = False
|
||||
|
||||
def write_format_data(self, format_dict, md_dict=None) -> None:
|
||||
"""Write the format data dict to the frontend.
|
||||
|
||||
This default version of this method simply writes the plain text
|
||||
representation of the object to ``sys.stdout``. Subclasses should
|
||||
override this method to send the entire `format_dict` to the
|
||||
frontends.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
format_dict : dict
|
||||
The format dict for the object passed to `sys.displayhook`.
|
||||
md_dict : dict (optional)
|
||||
The metadata dict to be associated with the display data.
|
||||
"""
|
||||
if 'text/plain' not in format_dict:
|
||||
# nothing to do
|
||||
return
|
||||
# We want to print because we want to always make sure we have a
|
||||
# newline, even if all the prompt separators are ''. This is the
|
||||
# standard IPython behavior.
|
||||
result_repr = format_dict['text/plain']
|
||||
if '\n' in result_repr:
|
||||
# So that multi-line strings line up with the left column of
|
||||
# the screen, instead of having the output prompt mess up
|
||||
# their first line.
|
||||
# We use the prompt template instead of the expanded prompt
|
||||
# because the expansion may add ANSI escapes that will interfere
|
||||
# with our ability to determine whether or not we should add
|
||||
# a newline.
|
||||
if not self.prompt_end_newline:
|
||||
# But avoid extraneous empty lines.
|
||||
result_repr = '\n' + result_repr
|
||||
|
||||
try:
|
||||
print(result_repr)
|
||||
except UnicodeEncodeError:
|
||||
# If a character is not supported by the terminal encoding replace
|
||||
# it with its \u or \x representation
|
||||
print(result_repr.encode(sys.stdout.encoding,'backslashreplace').decode(sys.stdout.encoding))
|
||||
|
||||
def update_user_ns(self, result):
|
||||
"""Update user_ns with various things like _, __, _1, etc."""
|
||||
|
||||
# Avoid recursive reference when displaying _oh/Out
|
||||
if self.cache_size and result is not self.shell.user_ns['_oh']:
|
||||
if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
|
||||
self.cull_cache()
|
||||
|
||||
# Don't overwrite '_' and friends if '_' is in __builtin__
|
||||
# (otherwise we cause buggy behavior for things like gettext). and
|
||||
# do not overwrite _, __ or ___ if one of these has been assigned
|
||||
# by the user.
|
||||
update_unders = True
|
||||
for unders in ['_'*i for i in range(1,4)]:
|
||||
if not unders in self.shell.user_ns:
|
||||
continue
|
||||
if getattr(self, unders) is not self.shell.user_ns.get(unders):
|
||||
update_unders = False
|
||||
|
||||
self.___ = self.__
|
||||
self.__ = self._
|
||||
self._ = result
|
||||
|
||||
if ('_' not in builtin_mod.__dict__) and (update_unders):
|
||||
self.shell.push({'_':self._,
|
||||
'__':self.__,
|
||||
'___':self.___}, interactive=False)
|
||||
|
||||
# hackish access to top-level namespace to create _1,_2... dynamically
|
||||
to_main = {}
|
||||
if self.do_full_cache:
|
||||
new_result = '_%s' % self.prompt_count
|
||||
to_main[new_result] = result
|
||||
self.shell.push(to_main, interactive=False)
|
||||
self.shell.user_ns['_oh'][self.prompt_count] = result
|
||||
|
||||
def fill_exec_result(self, result):
|
||||
if self.exec_result is not None:
|
||||
self.exec_result.result = result
|
||||
|
||||
def log_output(self, format_dict):
|
||||
"""Log the output."""
|
||||
if 'text/plain' not in format_dict:
|
||||
# nothing to do
|
||||
return
|
||||
if self.shell.logger.log_output:
|
||||
self.shell.logger.log_write(format_dict['text/plain'], 'output')
|
||||
self.shell.history_manager.output_hist_reprs[self.prompt_count] = \
|
||||
format_dict['text/plain']
|
||||
|
||||
def finish_displayhook(self):
|
||||
"""Finish up all displayhook activities."""
|
||||
sys.stdout.write(self.shell.separate_out2)
|
||||
sys.stdout.flush()
|
||||
|
||||
def __call__(self, result=None):
|
||||
"""Printing with history cache management.
|
||||
|
||||
This is invoked every time the interpreter needs to print, and is
|
||||
activated by setting the variable sys.displayhook to it.
|
||||
"""
|
||||
self.check_for_underscore()
|
||||
if result is not None and not self.quiet():
|
||||
self.start_displayhook()
|
||||
self.write_output_prompt()
|
||||
format_dict, md_dict = self.compute_format_data(result)
|
||||
self.update_user_ns(result)
|
||||
self.fill_exec_result(result)
|
||||
if format_dict:
|
||||
self.write_format_data(format_dict, md_dict)
|
||||
self.log_output(format_dict)
|
||||
self.finish_displayhook()
|
||||
|
||||
def cull_cache(self):
|
||||
"""Output cache is full, cull the oldest entries"""
|
||||
oh = self.shell.user_ns.get('_oh', {})
|
||||
sz = len(oh)
|
||||
cull_count = max(int(sz * self.cull_fraction), 2)
|
||||
warn('Output cache limit (currently {sz} entries) hit.\n'
|
||||
'Flushing oldest {cull_count} entries.'.format(sz=sz, cull_count=cull_count))
|
||||
|
||||
for i, n in enumerate(sorted(oh)):
|
||||
if i >= cull_count:
|
||||
break
|
||||
self.shell.user_ns.pop('_%i' % n, None)
|
||||
oh.pop(n, None)
|
||||
|
||||
|
||||
def flush(self):
|
||||
if not self.do_full_cache:
|
||||
raise ValueError("You shouldn't have reached the cache flush "
|
||||
"if full caching is not enabled!")
|
||||
# delete auto-generated vars from global namespace
|
||||
|
||||
for n in range(1,self.prompt_count + 1):
|
||||
key = '_'+repr(n)
|
||||
try:
|
||||
del self.shell.user_ns_hidden[key]
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
del self.shell.user_ns[key]
|
||||
except KeyError:
|
||||
pass
|
||||
# In some embedded circumstances, the user_ns doesn't have the
|
||||
# '_oh' key set up.
|
||||
oh = self.shell.user_ns.get('_oh', None)
|
||||
if oh is not None:
|
||||
oh.clear()
|
||||
|
||||
# Release our own references to objects:
|
||||
self._, self.__, self.___ = '', '', ''
|
||||
|
||||
if '_' not in builtin_mod.__dict__:
|
||||
self.shell.user_ns.update({'_':self._,'__':self.__,'___':self.___})
|
||||
import gc
|
||||
# TODO: Is this really needed?
|
||||
# IronPython blocks here forever
|
||||
if sys.platform != "cli":
|
||||
gc.collect()
|
||||
|
||||
|
||||
class CapturingDisplayHook(object):
|
||||
def __init__(self, shell, outputs=None):
|
||||
self.shell = shell
|
||||
if outputs is None:
|
||||
outputs = []
|
||||
self.outputs = outputs
|
||||
|
||||
def __call__(self, result=None):
|
||||
if result is None:
|
||||
return
|
||||
format_dict, md_dict = self.shell.display_formatter.format(result)
|
||||
self.outputs.append({ 'data': format_dict, 'metadata': md_dict })
|
149
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/displaypub.py
vendored
Normal file
149
fall-2024/math/mat-204/00020/env/lib/python3.12/site-packages/IPython/core/displaypub.py
vendored
Normal file
|
@ -0,0 +1,149 @@
|
|||
"""An interface for publishing rich data to frontends.
|
||||
|
||||
There are two components of the display system:
|
||||
|
||||
* Display formatters, which take a Python object and compute the
|
||||
representation of the object in various formats (text, HTML, SVG, etc.).
|
||||
* The display publisher that is used to send the representation data to the
|
||||
various frontends.
|
||||
|
||||
This module defines the logic display publishing. The display publisher uses
|
||||
the ``display_data`` message type that is defined in the IPython messaging
|
||||
spec.
|
||||
"""
|
||||
|
||||
# Copyright (c) IPython Development Team.
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
|
||||
|
||||
import sys
|
||||
|
||||
from traitlets.config.configurable import Configurable
|
||||
from traitlets import List
|
||||
|
||||
# This used to be defined here - it is imported for backwards compatibility
|
||||
from .display_functions import publish_display_data
|
||||
|
||||
import typing as t
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Main payload class
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
class DisplayPublisher(Configurable):
|
||||
"""A traited class that publishes display data to frontends.
|
||||
|
||||
Instances of this class are created by the main IPython object and should
|
||||
be accessed there.
|
||||
"""
|
||||
|
||||
def __init__(self, shell=None, *args, **kwargs):
|
||||
self.shell = shell
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def _validate_data(self, data, metadata=None):
|
||||
"""Validate the display data.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
data : dict
|
||||
The formata data dictionary.
|
||||
metadata : dict
|
||||
Any metadata for the data.
|
||||
"""
|
||||
|
||||
if not isinstance(data, dict):
|
||||
raise TypeError('data must be a dict, got: %r' % data)
|
||||
if metadata is not None:
|
||||
if not isinstance(metadata, dict):
|
||||
raise TypeError('metadata must be a dict, got: %r' % data)
|
||||
|
||||
# use * to indicate transient, update are keyword-only
|
||||
def publish(self, data, metadata=None, source=None, *, transient=None, update=False, **kwargs) -> None:
|
||||
"""Publish data and metadata to all frontends.
|
||||
|
||||
See the ``display_data`` message in the messaging documentation for
|
||||
more details about this message type.
|
||||
|
||||
The following MIME types are currently implemented:
|
||||
|
||||
* text/plain
|
||||
* text/html
|
||||
* text/markdown
|
||||
* text/latex
|
||||
* application/json
|
||||
* application/javascript
|
||||
* image/png
|
||||
* image/jpeg
|
||||
* image/svg+xml
|
||||
|
||||
Parameters
|
||||
----------
|
||||
data : dict
|
||||
A dictionary having keys that are valid MIME types (like
|
||||
'text/plain' or 'image/svg+xml') and values that are the data for
|
||||
that MIME type. The data itself must be a JSON'able data
|
||||
structure. Minimally all data should have the 'text/plain' data,
|
||||
which can be displayed by all frontends. If more than the plain
|
||||
text is given, it is up to the frontend to decide which
|
||||
representation to use.
|
||||
metadata : dict
|
||||
A dictionary for metadata related to the data. This can contain
|
||||
arbitrary key, value pairs that frontends can use to interpret
|
||||
the data. Metadata specific to each mime-type can be specified
|
||||
in the metadata dict with the same mime-type keys as
|
||||
the data itself.
|
||||
source : str, deprecated
|
||||
Unused.
|
||||
transient : dict, keyword-only
|
||||
A dictionary for transient data.
|
||||
Data in this dictionary should not be persisted as part of saving this output.
|
||||
Examples include 'display_id'.
|
||||
update : bool, keyword-only, default: False
|
||||
If True, only update existing outputs with the same display_id,
|
||||
rather than creating a new output.
|
||||
"""
|
||||
|
||||
handlers: t.Dict = {}
|
||||
if self.shell is not None:
|
||||
handlers = getattr(self.shell, "mime_renderers", {})
|
||||
|
||||
for mime, handler in handlers.items():
|
||||
if mime in data:
|
||||
handler(data[mime], metadata.get(mime, None))
|
||||
return
|
||||
|
||||
if 'text/plain' in data:
|
||||
print(data['text/plain'])
|
||||
|
||||
def clear_output(self, wait=False):
|
||||
"""Clear the output of the cell receiving output."""
|
||||
print('\033[2K\r', end='')
|
||||
sys.stdout.flush()
|
||||
print('\033[2K\r', end='')
|
||||
sys.stderr.flush()
|
||||
|
||||
|
||||
class CapturingDisplayPublisher(DisplayPublisher):
|
||||
"""A DisplayPublisher that stores"""
|
||||
|
||||
outputs: List = List()
|
||||
|
||||
def publish(
|
||||
self, data, metadata=None, source=None, *, transient=None, update=False
|
||||
):
|
||||
self.outputs.append(
|
||||
{
|
||||
"data": data,
|
||||
"metadata": metadata,
|
||||
"transient": transient,
|
||||
"update": update,
|
||||
}
|
||||
)
|
||||
|
||||
def clear_output(self, wait=False):
|
||||
super(CapturingDisplayPublisher, self).clear_output(wait)
|
||||
|
||||
# empty the list, *do not* reassign a new list
|
||||
self.outputs.clear()
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue