smoother python 2.7 to 3.x transition (scite as IDE)

copyright (C) qingfeng Xia 2011-2020 CC-BY-NC 4.0

I aim to write code in python script, most of time, single file script, to run on both 2.x and 3.x with my favorite light-weighted IDE scite. So here is my setup

1.default python 2.7 anaconda, setup the env var  and point to this file, for linux, set it in ‘.bashrc’

$HOME/.config/python/ put this in your .bashrc:
export PYTHONSTARTUP=$HOME/.config/python/python-startup.py ##########

python-startup.py#######

# -*- encoding:utf-8 -*-

"""
This startup script will make python 2.7 act like python 3.x
see full tutorial at:
http://python-future.org/compatible_idioms.html
by set up PYTHONSTARTUP env var to this file
`pip install future`  #so __future__ module is ready to use
`pip install six`       #dict int str are different between py2 and py3
convert python 2.x to 3.x by tools,
warning!!! this is an in-place conversion, backup your python 2.x script first!
on windows: python D:\Software\Anaconda\Tools\Scripts\2to3.py file_tobeconverted_py3.py -w
"""

from __future__ import print_function  #using script: 2to3 or futurize; print command still work without () for single var printing
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import

#comment out all these str-alias script, as it cause errror in  conda command `conda  update python`
#it is NOT a perfect solution by alias str->unicode, always using  u"any string"
#try:
#    str=unicode
#except:
#    pass

#for int to long, it is not critical for a single file script job
#dict, try using six module

# Python 2 and 3:
from io import BytesIO     # for handling byte strings
from io import StringIO    # for handling unicode strings
from io import open  #f = open('myfile.txt', encoding='utf-8')

# Python 2.7 and above
from collections import Counter, OrderedDict
#using request to deal with httplib or urllib

## import some common modules
#import sys, os, struct
#from math import *
#from pylab import *

     

  1. edit scite “python.properties” file and add the following at the end and use your own python3 path to replace : “D:\Software\Python3\pythonw” it should apply to windows and linux
#############ctrl+1####################
command.name.1.*.py=Syntax Check for python 2.x
command.1.*.py=python -c "import py_compile; py_compile.compile(r'$(FilePath)')"

############## 2to3 ######################
command.name.1.*.py=convert by 2to3 with backup
#backup first? the converted file will automatically loaded
if PLAT_WIN
command.1.*.py=copy "$(FileNameExt)" "$(FileNameExt).bak" && python D:\Software\Anaconda\Tools\Scripts\2to3.py "$(FileNameExt)" -w
if PLAT_GTK
command.1.*.py=cp "$(FileNameExt)" "$(FileNameExt)".bak && python D:\Software\Anaconda\Tools\Scripts\2to3.py "$(FileNameExt)" -w

############# add in tool menu item ########
command.name.3.*.py=run with python3
command.3.*.py=D:\Software\Python3\pythonw -u "$(FileNameExt)"
#command.subsystem.3.$(py)=2
#command.save.before.3.$(py)=2
#command.quiet.3.$(py)=1

##########################################################


CC-BY-NC 4.0 licensed free for non-commercial usage
Author: Qingfeng XIA
copyright (C) 2011-2020
http://www.iesensor.com
please keep the original link in your reference.
http://www.iesensor.com/blog/2015/09/06/smoother-python-2-7-to-3-x-transition-scite-as-ide/
This entry was posted in Programming. Bookmark the permalink.