Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Example of an init.sage file forcing one to use Python3-style print

Views: 359
1
# -*- coding: utf-8 -*-
2
3
### Encourage Python3 good habits even when using Python2-based Sage
4
5
from __future__ import division, absolute_import, print_function
6
from six import iteritems, iterkeys, itervalues
7
from six.moves import range
8
9
### Set colors depending on terminal background color
10
11
# %colors NoColor # to disable syntax coloring
12
# %colors LightBG # works well with light background
13
%colors Linux # works well with dark background
14
15
### Custom banner, displays even when starting Sage in quiet mode with `sage -q`
16
17
print("\n# SageMath {}, released {}, based on Python {}.{}.{}.\n"
18
.format(sage.version.version, sage.version.date, *sys.version_info[:3]) +
19
"""
20
sage: # Setup from init.sage: Python3-related imports, and a color choice
21
22
sage: from __future__ import division, absolute_import, print_function
23
sage: from six import iteritems, iterkeys, itervalues
24
sage: from six.moves import range
25
26
sage: %colors Linux # works well with dark background
27
""")
28
29
### Hints from https://wiki.sagemath.org/Python3-compatible%20code
30
31
# To check Python 3 syntax errors in every file changed in the current branch:
32
#
33
# git diff --name-only develop your-new-branch | xargs -n 1 python3 -m py_compile
34
#
35
# To check Python 3 syntax errors in every Python file of Sage library --
36
# see https://groups.google.com/d/topic/sage-devel/dwEABlLOWqI/discussion
37
#
38
# find src/sage -name '*.py' | xargs -n 1 python3 -m py_compile
39
40