microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.72k stars 766 forks source link

Using with SageMath #58

Closed darkl closed 4 years ago

darkl commented 4 years ago

I am using VSCode with SageMath Jupyter notebooks. I followed these instructions for that. It works well, but I was wondering if I could use Pylance for intellisense and autocomplete? For instance, I would like to see documentation for the methods of the SageMath objects. Currently I don't.

Thanks for your help Elad

savannahostrowski commented 4 years ago

Hey @darkl! Could you provide a little more information about your dev environment (e.g. Pylance version, VSCode version etc)? This will help us debug. Additionally, could you check if this reproduces on the latest version, 2020.7.0?

darkl commented 4 years ago

Pylance version: v2020.7.0 VSCode Python extension version: v2020.6.91350 VSCode version: 1.46.1 (system setup) Windows 10 Version 1909 (OS Build 18363.900) SageMath info: SageMath version 9.0, Release Date: 2020-01-01, Using Python 3.7.3

It doesn't seem to generate intellisense/documentation with the latest version.

Thanks!

jakebailey commented 4 years ago

From that guide, this looks to be a completely different notebook/language type than Python, no? It relies on patching the Python extension to treat the Sage stuff as python, when it actually isn't. I'm not sure that this is a supported scenario, as the Python extension's notebook support is currently only suited for Python code (and contains many workarounds to get rich features from regular Python code editing into the notebook view).

Perhaps @microsoft/datascience-vsc-team may be more able to define what's expected here.

darkl commented 4 years ago

I wouldn't say it's a completely different language, SageMath is an extension of Python, and any code written in Python in theory should run of Sage. Since the source code of SageMath is written in Python, I did expect to be able to get intellisense/documentation for methods. I would remark that listing of the methods works well after initializing an object of some type (much better than the web browser interface), but the trouble is that no parameters intellisense/documentation is provided.

image image

jakebailey commented 4 years ago

I wouldn't say it's a completely different language, SageMath is an extension of Python, and any code written in Python in theory should run of Sage.

Fair enough, thanks for the correction.

but the trouble is that no parameters intellisense/documentation is provided.

I think you may just have the tooltip hidden. If you hover over the selected completion line, does a ">" appear where you can expand the tooltip to get more info? VS Code unfortunately hides this info by default.

jakebailey commented 4 years ago

For example:

image

Can be expanded to: image

By clicking the ">" or hitting Ctrl+Space.

joyceerhl commented 4 years ago

the Python extension's notebook support is currently only suited for Python code

Dev from microsoft/datascience-vsc-team here, just wanted to add for context that we are working on support for multiple language kernels in our notebook experience, tracked in https://github.com/microsoft/vscode-python/pull/12111 and https://github.com/microsoft/vscode-python/issues/11919 :)

darkl commented 4 years ago

For example:

By clicking the ">" or hitting Ctrl+Space.

Unfortunately, there is no ">" and Ctrl+Space just shows all the variables/functions available in the scope.

jakebailey commented 4 years ago

The key thing here is that SageMath is a superset of Python. If you look at the original example, the code in its entirety is:

finiteField = GF(25)
finiteField.

But where did GF come from? It's a new builtin function that Sage offers that doesn't exist in Python. But Pylance only knows about Python, not Sage. This code wouldn't run as-is in Python without something to bring GF into scope.

The reason this appears to work at all is because the notebook code calls both the kernel and Pylance to get completions, then merges their info together. The kernel has the live data, and because you've patched Sage in as the kernel, it knows how to answer the question and gets a result. Pylance answers "IDK", and the frontend just uses what the kernel answered, which currently isn't as rich as what the language server offers for normal Python code. Perhaps this could change in general (the notebook code recently changed to allow some of the live data to come through), but that's a different discussion about how to handle non-Python languages.

Python code runs in Sage, but that doesn't imply the Sage can be read as Python, and Pylance only knows about Python.

Daniel-Khodabakhsh commented 3 years ago

Hey @darkl, I'm the one who wrote the instructions you linked for getting Sage to work with VSCode's Jupyter. Just wanted to chime in and see if this information would help!

I don't use Pylance myself but I suspect the issue you're seeing is actually a Pylance <-> VSCode Jupyter issue, and not one specific to Sage.

In any case, it should be possible to use Pylance with Sage if you use Python modules. If you have Sage notebook running locally, this can be accomplished by:

  1. Creating Python source files on your local disk (your Python module).
  2. Referencing the local files by adding it to your notebook's sys.path list of directories.
  3. Enabling the IPython autoreload extension (this is for convenience, otherwise the kernel needs to be restarted after every module change).
  4. Importing the python module in Sage notebook.

1. Creating the Python module

The Python module should at least have a __init__.py file in every importable folder level. Here is a sample file structure:

/path/to/your/python/modules/mymodule
  - __init__.py (0 bytes)
  - implementation.py
  - submodule/
    - __init__.py (0 bytes)
    - more_implementation.py

To use Sage functions in your source files, all you have to do is import sage.all as illustrated in the following snippet:

# /path/to/your/python/modules/mymodule/implementation.py

from sage.all import *

x = var('x') # Using sage's `var` function.
y = sin(x) # Checking the type of `y` here will give you the sage `Expression` type
z = 3.4 # In a Sage notebook, this `z` would be a `RealNumber`, however you will find that it is a `float`.
        # This is a drawback of using the module pattern but the solution is using the assignment example below:
z2 = RealNumber(3.4) # Use the `RealNumber()` object constructor (and `Integer()` etc.) to assign literals.

To address linting in VSCode, you'll need to specify the path to the Sage Python library in the VSCode settings.json:

    "python.autoComplete.extraPaths": [
        "/path/to/your/SageMath-9.2/local/lib/python3.8/site-packages"
    ]

Pylance should probably read this config as well, otherwise you'll need to provide similar paths to Pylance's configs.

With these source files, you should be able to use Pylance with Sage.

2 & 3. Setting up Sage notebook for Python module usage

To accomplish, I have the following as my first cell of my Sage notebook:

%load_ext autoreload
%autoreload 2

rootPath = '/path/to/your/python/modules'

if rootPath not in sys.path:
    sys.path.append(rootPath)

4. Importing your Python module in your Sage notebook

Now, in your Sage notebook, you can import your module within one of your cells and use its code:

from mymodule.implementation import z2
z2 + 3 # This will print out '6.4'

Notes:

Most Sage functionality will be imported from sage.all and you can also import specific functionality as well. However some Sage functionality for some reason don't show up in sage.all and need to be imported explicitly. Examples of this include find_root which can be imported via from sage.numerical.optimize import find_root and Graphics3d via from sage.plot.plot3d.base import Graphics3d. If you find yourself in a situation where a functionality isn't imported by sage.all you can explore the /path/to/your/SageMath-9.2/local/lib/python3.8/site-packages/sage folder to find the function or class which reveals which import path you should use (for example, find_root is located in the /path/to/your/SageMath-9.2/local/lib/python3.8/site-packages/sage/numerical/optimize.py file so the import path is from sage.numerical.optimize import find_root).

darkl commented 3 years ago

Hi @Daniel-Khodabakhsh, thanks for the detailed reply! I will give it a try!

Elad

ali-ameri commented 3 years ago

Alice splits the M-Series message into several parts using Shamir's Threshold Secret Sharing scheme and distributes it between Bob, Carol and Oscar. Retrieve the M message using SageMath.

p = 98610077260239398222090992029437657450932580712058628707362467948344492057365826464402904054263162278966613878648712696840664890977708159054505909356614775685384800153427070438012782055016614367597928500125115244063783154699188478772541892057527093871947446448825987166591681265594660523952919275117854741557 s1 = (1, 103175228602002609504909099105570798409981722654967834891784369314283143033019713027395797203935249359246550201673544653203162887759554421288274936487652494664133905741862353429481721958488073554241895988248652726657854808451772801256073750812441905802880735186263097448666587256784092799227558907574626490558) s2 = (2, 259419973384447516546480698959214955851218886234158575228853178836817375028962453539906405658878847066987065702450235568406503370363978669630304650688025095502152818700198318748835264923980315248864977804325707550285677311533451436944379560073031712065009345556209096892115348533534621011309298287251546724990) s3 = (3, 468734234347334721124714799560932472323711490737572221011206428567602695987828221537531825364830793123221546502330072745610021447813272745026089142601117802514056738875007895958060628951595891675246298731770409381134181599487651474171905902894691620338285785675807468359411420964666599144804707045683021689454)

ali-ameri commented 3 years ago

Hello my friends I want to solve this question if possible

ali-ameri commented 3 years ago

ali.group2891@yahoo.com