3720
Comment:
|
3726
|
Deletions are marked like this. | Additions are marked like this. |
Line 16: | Line 16: |
. If you are one of ''[http://www.gnu.org/software/emacs/ those people]'' you'll want to visit http://www.sagemath.org/emacs/. | . If you are one of ''[http://www.gnu.org/software/emacs/ those Emacs people]'' you'll want to visit [javascript:void(0);/*1214779611252*/ Emacs]. |
Line 33: | Line 33: |
* '''Determining if your script is attached or being run at the command line''' Say you have a script in which some functions are defined. Sometimes you want to attach the file to an interactive session and play with the functions, uh, interactively. But perhaps you'd like the script to do something else when run at the command line; perhaps it should calculate a bunch of values and save them to a file. In other words, you want the standard {{{if __name__ == '__main__'}}} Python idiom. How to get that? One way is to look at {{{sys.argv}}} and compare it to {{{SAGE_TMP}}}. When you attach a file to a running session, the preparsed version of the script gets put into the directory {{{SAGE_TMP}}} (in recent versions of Sage, at least) and {{{sys.argv}}} for that script starts with {{{SAGE_TMP}}}. Put the following code in your script: |
* '''Determining if your script is attached or being run at the command line''' Say you have a script in which some functions are defined. Sometimes you want to attach the file to an interactive session and play with the functions, uh, interactively. But perhaps you'd like the script to do something else when run at the command line; perhaps it should calculate a bunch of values and save them to a file. In other words, you want the standard {{{if __name__ == '__main__'}}} Python idiom. How to get that? One way is to look at {{{sys.argv}}} and compare it to {{{SAGE_TMP}}}. When you attach a file to a running session, the preparsed version of the script gets put into the directory {{{SAGE_TMP}}} (in recent versions of Sage, at least) and {{{sys.argv}}} for that script starts with {{{SAGE_TMP}}}. Put the following code in your script: |
Line 46: | Line 41: |
* '''Cython''': Note that if you have an `.spyx` file that you want to `load` or `attach` in `sage`, and you want to link it against some library (GMP, NTL, etc), or possibly compile with `g++` instead of `gcc`, you need to add something like the following to the top of the file: {{{ |
* '''Cython''': Note that if you have an `.spyx` file that you want to `load` or `attach` in `sage`, and you want to link it against some library (GMP, NTL, etc), or possibly compile with `g++` instead of `gcc`, you need to add something like the following to the top of the file: {{{ |
Tips for Using SAGE
GeoGebra [http://www.geogebra.org/cms/ GeoGebra] is a free and multi-platform dynamic mathematics software for schools that joins geometry, algebra and calculus. Here's how you can embed it in the SAGE notebook.
In GeoGebra: Create some geometric construction or plot an algebraic expression. See [http://www.geogebra.org/cms/index.php?option=com_content&task=blogcategory&id=69&Itemid=56 this page] for more details.
In GeoGebra: File> Export> Dynamic worksheet.
Open the .html just exported with a text editor and copy the <applet> section.
In SAGE notebook: Data> Upload the .jar and .ggb files just exported.
In SAGE notebook: Edit> Paste the <applet> section.
In the <applet> section, change codebase="./data/", you can also set some of the flags to true.
- Save changes, and you're done.
Vim filetypes To get Vim to use Python syntax highlighting, indentation, and so on for .sage files, put the following in $VIM/filetype.vim:
augroup filetypedetect au! BufRead,BufNewFile *.sage,*.spyx,*.pyx setfiletype python augroup END
If you are one of [http://www.gnu.org/software/emacs/ those Emacs people] you'll want to visit [javascript:void(0);/*1214779611252*/ Emacs].
Autocompletion for the bash shell So, you'd like bash to autocomplete Sage files, but you want to somehow exclude those .py files that have been autogenerated by Sage. Here's a snippet that will do that for you. Put this somewhere where it will get sourced, such as your .bashrc, .bash_profile, or whatever. Often bash is set up so that it will automatically pull in completions defined in ~/.bash_completion. Here's the code:
_sagecomp() { local cur files COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" files="$(compgen -o dirnames -X '!*.@(sage|?(s)pyx|py)' -f ${cur})" if [[ "$files" ]] then COMPREPLY=($(egrep -L '^# This file was \*autogenerated\* from the file .*\.sage' $files)) fi } complete -F _sagecomp sage
See [http://www.debian-administration.org/articles/317 this article] for more information. --DanDrake
Determining if your script is attached or being run at the command line Say you have a script in which some functions are defined. Sometimes you want to attach the file to an interactive session and play with the functions, uh, interactively. But perhaps you'd like the script to do something else when run at the command line; perhaps it should calculate a bunch of values and save them to a file. In other words, you want the standard if __name__ == '__main__' Python idiom. How to get that? One way is to look at sys.argv and compare it to SAGE_TMP. When you attach a file to a running session, the preparsed version of the script gets put into the directory SAGE_TMP (in recent versions of Sage, at least) and sys.argv for that script starts with SAGE_TMP. Put the following code in your script:
import sys import os def is_attached(): return os.path.split(sys.argv[0])[0] == os.path.normpath(SAGE_TMP)
Another way which is perhaps nicer but not quite as portable is to define a boolean in the file$HOME/init.sage, which is sourced on interactive startup; then you could test for that boolean in your script. --DanDrake
Cython: Note that if you have an .spyx file that you want to load or attach in sage, and you want to link it against some library (GMP, NTL, etc), or possibly compile with g++ instead of gcc, you need to add something like the following to the top of the file:
#clang c++ #clib ntl