-4

I wrote a simple command-line translator for linux user, using python2.x,

but it don't support python3, the code is less than 100 lines,

main.py:

#!/usr/bin/env python2
# entry of program,

import sys
import urllib2
from BeautifulSoup import BeautifulSoup
import ConfigParser
from os.path import expanduser

version = "v0.1" + " beta3"

# read config
googleDomain = "google.com"
targetLang = "en"
srcLang = "auto"

home = expanduser("~")
config = ConfigParser.RawConfigParser()
config.read(home + "/.config/pygtrans/config.ini")

if config.has_option("basic","google_domain"):
    tmpDomain = config.get("basic", "google_domain")
    if tmpDomain and (not tmpDomain.isspace()):
        googleDomain = tmpDomain

if config.has_option("basic","target_language"):
    tmpTargetLang = config.get("basic", "target_language")
    if tmpTargetLang and (not tmpTargetLang.isspace()):
        targetLang = tmpTargetLang

if config.has_option("basic","source_language"):
    tmpSrcLang = config.get("basic", "source_language")
    if tmpSrcLang and (not tmpSrcLang.isspace()):
        srcLang = tmpSrcLang

## param check
key = ""
if len(sys.argv) < 2:
    print("Please use following formats:\n\t%s" % ( "gtrans <input_string> [<target_language>] [<source_language>]"))
    print("\t%s" % ("gtrans (-v | -version)"))
    print("Common languages:\n\t%s\n" % ("en, zh, zh_TW, ja, fr, de,"))
    sys.exit(1)
elif sys.argv[1]=="-v" or sys.argv[1]=="--version":
    print "pygtrans - " + version
    sys.exit(0)
else:
    key = sys.argv[1]
    if len(sys.argv) >=3:
        targetLang = sys.argv[2]
    if len(sys.argv) >=4:
        srcLang = sys.argv[3]

## http request
resultId = "result_box"
userAgent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
headers = {'User-Agent':userAgent}
url = "http://translate." + googleDomain + "/?langpair=" + srcLang + "|" + targetLang + "&text=" + key
page = urllib2.urlopen(urllib2.Request(url, None, headers))

## result parse
soup = BeautifulSoup(page)
x = soup.body.find(id=resultId).text
print unicode(key, 'utf-8') + "\t->\t" + x

Any one can point out which functions has issue with python3.x ? Or has interesting to help fix it on github?


@Update:

After try for 1 hour, I am very dispointed by the python versions and lib versions, almost every lib I use has version issue, and a lot functions too, give up ... this don't feels like programming.

Eric
  • 22,183
  • 20
  • 145
  • 196
  • @Cyber Indentation issue fixed. – Eric Jan 13 '15 at 15:48
  • @Cyber is right: in Py3, `print` is a function (not a statement) so it needs to be **called** (args in parentheses after `print`). – Alex Martelli Jan 13 '15 at 15:49
  • 1
    Just use the [`2to3`](https://docs.python.org/2/library/2to3.html) tool... – Bakuriu Jan 13 '15 at 15:50
  • Any one tell me the reason to downvote? – Eric Jan 13 '15 at 15:52
  • 3
    When you hover on the downvote button it says "This question does not show research effort; ...". I believe it's clear that you didn't even try to search anything about porting code to python3 or writing code that runs on both versions, since there are **tons** of resources available about that (both on SO and outside SO). [note: I didn't downvote you]. – Bakuriu Jan 13 '15 at 15:54

1 Answers1

1

The biggest issue is

print "some string"

must now be

print("some string")

There have also been changes to the urllib module

Community
  • 1
  • 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 2
    @EricWang add `from __future__ import print_function` at the top of the file and you can use `print` as a function in python2 too. – Bakuriu Jan 13 '15 at 15:51