| Download
Example of an init.sage file forcing one to use Python3-style print
Project: cocalc-sagemath-dev-slelievre
Path: sagemath-tips/init.sage
Views: 359# -*- coding: utf-8 -*-12### Encourage Python3 good habits even when using Python2-based Sage34from __future__ import division, absolute_import, print_function5from six import iteritems, iterkeys, itervalues6from six.moves import range78### Set colors depending on terminal background color910# %colors NoColor # to disable syntax coloring11# %colors LightBG # works well with light background12%colors Linux # works well with dark background1314### Custom banner, displays even when starting Sage in quiet mode with `sage -q`1516print("\n# SageMath {}, released {}, based on Python {}.{}.{}.\n"17.format(sage.version.version, sage.version.date, *sys.version_info[:3]) +18"""19sage: # Setup from init.sage: Python3-related imports, and a color choice2021sage: from __future__ import division, absolute_import, print_function22sage: from six import iteritems, iterkeys, itervalues23sage: from six.moves import range2425sage: %colors Linux # works well with dark background26""")2728### Hints from https://wiki.sagemath.org/Python3-compatible%20code2930# To check Python 3 syntax errors in every file changed in the current branch:31#32# git diff --name-only develop your-new-branch | xargs -n 1 python3 -m py_compile33#34# To check Python 3 syntax errors in every Python file of Sage library --35# see https://groups.google.com/d/topic/sage-devel/dwEABlLOWqI/discussion36#37# find src/sage -name '*.py' | xargs -n 1 python3 -m py_compile383940