python 2 vs. 3 in practice

im still not entirely pleased with python 3, nor the transitions in the way syntax and encoding are handled. im not alone when i say 2 is better in many ways.

here are most of the changes that turned the python2 version of fig into something that runs in python3:

 

print  ->  print("")

print stuff.whatever().here() + etc,  ->      
print(stuff.whatever().here() + etc,)

etc.keys()  ->  list(etc.keys())

outfile.write("#!/usr/bin/env python" + "\n# encoding: utf-8\n")
  ->  outfile.write("#!/usr/bin/env python3" + "\n# encoding: utf-8\n")

p = map(str, p) ; p.reverse()  ->  p = list(map(str, p)) ; p.reverse()

raw_input() -> input()

from urllib import urlopen  ->  from urllib import request ; urlopen = request.urlopen

import sys  ->  import sys, codecs

addtoout[0] = """import sys, os  ->  addtoout[0] = """import sys, os, io

from sys import stdin, stdout
  ->  from sys import stdout
  ->  stdin=io.open(sys.stdin.fileno(),'r',encoding='latin-1')

unichr(  ->  chr(

 

def figprint(p): print p  ->

def figprint(p): 
    if type(p) == str:
        try: print(bytes(p,encoding="latin-1").decode("utf-8"), flush=True)
        except UnicodeDecodeError: print(p, flush=True)
        except UnicodeEncodeError: print(p, flush=True) 
    else:
        print(p)

 

def figprints(p): stdout.write(str(p)) ; sys.stdout.flush()  ->

def figprints(p): 
    try: stdout.write(bytes(str(p),encoding="latin-1").decode("utf-8"))
    except UnicodeDecodeError: stdout.write(str(p))
    except UnicodeEncodeError: stdout.write(str(p))
    sys.stdout.flush()

 

figfilehandles[x] = open(x[:], s.lower())
  ->  figfilehandles[x] = open(x[:], s.lower(), encoding="latin-1")

figfilehandles[x] = fileinput.input(x[:])
  ->  figfilehandles[x] = fileinput.input(x[:],mode="rb")

 

figfilehandles[s].write(str(x) + sep)  ->

try: figfilehandles[s].write(bytes(str(x) + sep,encoding="latin-1").decode("utf-8"))
except UnicodeDecodeError: figfilehandles[s].write(str(x) + sep)
except UnicodeEncodeError: figfilehandles[s].write(str(x) + sep)

 

p = figfilehandles[s][figfilecounters[s]].replace(chr(13),
  ->  p = figfilehandles[s][figfilecounters[s]].decode("latin-1").replace(chr(13), 

x = open(s).read().replace(chr(13) + chr(10), chr(10)).replace(chr(13), 
x = open(s,"rb").read().decode("latin-1").replace(chr(13) + chr(10), chr(10)).replace(chr(13), 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s