通过 2to3 将 selenium.py 转换为 python 3

发布于 2024-10-29 19:40:40 字数 6563 浏览 5 评论 0原文

我正在尝试转换用 python 2 编写的 selenium.py。

如下所示,我将 selenium.py 复制到 C:\Python32\Tools\Scripts>执行后

2to3.py selenium.py

我发现文件是相同的。我做错了什么?或者从哪里可以下载转换后的文件?

C:\Python32\Tools\Scripts>2to3.py selenium.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored selenium.py
--- selenium.py (original)
+++ selenium.py (refactored)
@@ -18,8 +18,8 @@

 # This file has been automatically generated via XSL

-import httplib
-import urllib
+import http.client
+import urllib.request, urllib.parse, urllib.error
 import re

 class selenium:
@@ -186,34 +186,34 @@
         self.extensionJs = extensionJs

     def start(self):
-        print (self.browserURL)
+        print((self.browserURL))
         result = self.get_string("getNewBrowserSession", [self.browserStartComm
and, self.browserURL, self.extensionJs])
         try:
             self.sessionId = result
         except ValueError:
-            raise Exception, result
+            raise Exception(result)

     def stop(self):
         self.do_command("testComplete", [])
         self.sessionId = None

     def do_command(self, verb, args):
-        conn = httplib.HTTPConnection(self.host, self.port)
-        body = u'cmd=' + urllib.quote_plus(unicode(verb).encode('utf-8'))
+        conn = http.client.HTTPConnection(self.host, self.port)
+        body = 'cmd=' + urllib.parse.quote_plus(str(verb).encode('utf-8'))
         for i in range(len(args)):
-            body += '&' + unicode(i+1) + '=' + urllib.quote_plus(unicode(args[i
]).encode('utf-8'))
+            body += '&' + str(i+1) + '=' + urllib.parse.quote_plus(str(args[i])
.encode('utf-8'))
         if (None != self.sessionId):
-            body += "&sessionId=" + unicode(self.sessionId)
+            body += "&sessionId=" + str(self.sessionId)
         headers = {"Content-Type": "application/x-www-form-urlencoded; charset=
utf-8"}
         conn.request("POST", "/selenium-server/driver/", body, headers)

         response = conn.getresponse()
         #print response.status, response.reason
-        data = unicode(response.read(), "UTF-8")
+        data = str(response.read(), "UTF-8")
         result = response.reason
         #print "Selenium Result: " + repr(data) + "\n\n"
         if (not data.startswith('OK')):
-            raise Exception, data
+            raise Exception(data)
         return data

     def get_string(self, verb, args):
@@ -255,7 +255,7 @@
             return True
         if ("false" == boolstr):
             return False
-        raise ValueError, "result is neither 'true' nor 'false': " + boolstr
+        raise ValueError("result is neither 'true' nor 'false': " + boolstr)

     def get_boolean_array(self, verb, args):
         boolarr = self.get_string_array(verb, args)
@@ -266,7 +266,7 @@
             if ("false" == boolstr):
                 boolarr[i] = False
                 continue
-            raise ValueError, "result is neither 'true' nor 'false': " + boolar
r[i]
+            raise ValueError("result is neither 'true' nor 'false': " + boolarr
[i])
         return boolarr


RefactoringTool: Files that need to be modified:
RefactoringTool: selenium.py

C:\Python32\Tools\Scripts>2to3.py selenium.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored selenium.py
--- selenium.py (original)
+++ selenium.py (refactored)
@@ -18,8 +18,8 @@

 # This file has been automatically generated via XSL

-import httplib
-import urllib
+import http.client
+import urllib.request, urllib.parse, urllib.error
 import re

 class selenium:
@@ -186,34 +186,34 @@
         self.extensionJs = extensionJs

     def start(self):
-        print (self.browserURL)
+        print((self.browserURL))
         result = self.get_string("getNewBrowserSession", [self.browserStartComm
and, self.browserURL, self.extensionJs])
         try:
             self.sessionId = result
         except ValueError:
-            raise Exception, result
+            raise Exception(result)

     def stop(self):
         self.do_command("testComplete", [])
         self.sessionId = None

     def do_command(self, verb, args):
-        conn = httplib.HTTPConnection(self.host, self.port)
-        body = u'cmd=' + urllib.quote_plus(unicode(verb).encode('utf-8'))
+        conn = http.client.HTTPConnection(self.host, self.port)
+        body = 'cmd=' + urllib.parse.quote_plus(str(verb).encode('utf-8'))
         for i in range(len(args)):
-            body += '&' + unicode(i+1) + '=' + urllib.quote_plus(unicode(args[i
]).encode('utf-8'))
+            body += '&' + str(i+1) + '=' + urllib.parse.quote_plus(str(args[i])
.encode('utf-8'))
         if (None != self.sessionId):
-            body += "&sessionId=" + unicode(self.sessionId)
+            body += "&sessionId=" + str(self.sessionId)
         headers = {"Content-Type": "application/x-www-form-urlencoded; charset=
utf-8"}
         conn.request("POST", "/selenium-server/driver/", body, headers)

         response = conn.getresponse()
         #print response.status, response.reason
-        data = unicode(response.read(), "UTF-8")
+        data = str(response.read(), "UTF-8")
         result = response.reason
         #print "Selenium Result: " + repr(data) + "\n\n"
         if (not data.startswith('OK')):
-            raise Exception, data
+            raise Exception(data)
         return data

     def get_string(self, verb, args):
@@ -255,7 +255,7 @@
             return True
         if ("false" == boolstr):
             return False
-        raise ValueError, "result is neither 'true' nor 'false': " + boolstr
+        raise ValueError("result is neither 'true' nor 'false': " + boolstr)

     def get_boolean_array(self, verb, args):
         boolarr = self.get_string_array(verb, args)
@@ -266,7 +266,7 @@
             if ("false" == boolstr):
                 boolarr[i] = False
                 continue
-            raise ValueError, "result is neither 'true' nor 'false': " + boolar
r[i]
+            raise ValueError("result is neither 'true' nor 'false': " + boolarr
[i])
         return boolarr


RefactoringTool: Files that need to be modified:
RefactoringTool: selenium.py

I'm trying to convert selenium.py written in python 2.

As you see below I copied selenium.py to C:\Python32\Tools\Scripts> and after execution

2to3.py selenium.py

I see that the file is the same. What Have I done wrong? Or from where I can download this file converted?

C:\Python32\Tools\Scripts>2to3.py selenium.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored selenium.py
--- selenium.py (original)
+++ selenium.py (refactored)
@@ -18,8 +18,8 @@

 # This file has been automatically generated via XSL

-import httplib
-import urllib
+import http.client
+import urllib.request, urllib.parse, urllib.error
 import re

 class selenium:
@@ -186,34 +186,34 @@
         self.extensionJs = extensionJs

     def start(self):
-        print (self.browserURL)
+        print((self.browserURL))
         result = self.get_string("getNewBrowserSession", [self.browserStartComm
and, self.browserURL, self.extensionJs])
         try:
             self.sessionId = result
         except ValueError:
-            raise Exception, result
+            raise Exception(result)

     def stop(self):
         self.do_command("testComplete", [])
         self.sessionId = None

     def do_command(self, verb, args):
-        conn = httplib.HTTPConnection(self.host, self.port)
-        body = u'cmd=' + urllib.quote_plus(unicode(verb).encode('utf-8'))
+        conn = http.client.HTTPConnection(self.host, self.port)
+        body = 'cmd=' + urllib.parse.quote_plus(str(verb).encode('utf-8'))
         for i in range(len(args)):
-            body += '&' + unicode(i+1) + '=' + urllib.quote_plus(unicode(args[i
]).encode('utf-8'))
+            body += '&' + str(i+1) + '=' + urllib.parse.quote_plus(str(args[i])
.encode('utf-8'))
         if (None != self.sessionId):
-            body += "&sessionId=" + unicode(self.sessionId)
+            body += "&sessionId=" + str(self.sessionId)
         headers = {"Content-Type": "application/x-www-form-urlencoded; charset=
utf-8"}
         conn.request("POST", "/selenium-server/driver/", body, headers)

         response = conn.getresponse()
         #print response.status, response.reason
-        data = unicode(response.read(), "UTF-8")
+        data = str(response.read(), "UTF-8")
         result = response.reason
         #print "Selenium Result: " + repr(data) + "\n\n"
         if (not data.startswith('OK')):
-            raise Exception, data
+            raise Exception(data)
         return data

     def get_string(self, verb, args):
@@ -255,7 +255,7 @@
             return True
         if ("false" == boolstr):
             return False
-        raise ValueError, "result is neither 'true' nor 'false': " + boolstr
+        raise ValueError("result is neither 'true' nor 'false': " + boolstr)

     def get_boolean_array(self, verb, args):
         boolarr = self.get_string_array(verb, args)
@@ -266,7 +266,7 @@
             if ("false" == boolstr):
                 boolarr[i] = False
                 continue
-            raise ValueError, "result is neither 'true' nor 'false': " + boolar
r[i]
+            raise ValueError("result is neither 'true' nor 'false': " + boolarr
[i])
         return boolarr


RefactoringTool: Files that need to be modified:
RefactoringTool: selenium.py

C:\Python32\Tools\Scripts>2to3.py selenium.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored selenium.py
--- selenium.py (original)
+++ selenium.py (refactored)
@@ -18,8 +18,8 @@

 # This file has been automatically generated via XSL

-import httplib
-import urllib
+import http.client
+import urllib.request, urllib.parse, urllib.error
 import re

 class selenium:
@@ -186,34 +186,34 @@
         self.extensionJs = extensionJs

     def start(self):
-        print (self.browserURL)
+        print((self.browserURL))
         result = self.get_string("getNewBrowserSession", [self.browserStartComm
and, self.browserURL, self.extensionJs])
         try:
             self.sessionId = result
         except ValueError:
-            raise Exception, result
+            raise Exception(result)

     def stop(self):
         self.do_command("testComplete", [])
         self.sessionId = None

     def do_command(self, verb, args):
-        conn = httplib.HTTPConnection(self.host, self.port)
-        body = u'cmd=' + urllib.quote_plus(unicode(verb).encode('utf-8'))
+        conn = http.client.HTTPConnection(self.host, self.port)
+        body = 'cmd=' + urllib.parse.quote_plus(str(verb).encode('utf-8'))
         for i in range(len(args)):
-            body += '&' + unicode(i+1) + '=' + urllib.quote_plus(unicode(args[i
]).encode('utf-8'))
+            body += '&' + str(i+1) + '=' + urllib.parse.quote_plus(str(args[i])
.encode('utf-8'))
         if (None != self.sessionId):
-            body += "&sessionId=" + unicode(self.sessionId)
+            body += "&sessionId=" + str(self.sessionId)
         headers = {"Content-Type": "application/x-www-form-urlencoded; charset=
utf-8"}
         conn.request("POST", "/selenium-server/driver/", body, headers)

         response = conn.getresponse()
         #print response.status, response.reason
-        data = unicode(response.read(), "UTF-8")
+        data = str(response.read(), "UTF-8")
         result = response.reason
         #print "Selenium Result: " + repr(data) + "\n\n"
         if (not data.startswith('OK')):
-            raise Exception, data
+            raise Exception(data)
         return data

     def get_string(self, verb, args):
@@ -255,7 +255,7 @@
             return True
         if ("false" == boolstr):
             return False
-        raise ValueError, "result is neither 'true' nor 'false': " + boolstr
+        raise ValueError("result is neither 'true' nor 'false': " + boolstr)

     def get_boolean_array(self, verb, args):
         boolarr = self.get_string_array(verb, args)
@@ -266,7 +266,7 @@
             if ("false" == boolstr):
                 boolarr[i] = False
                 continue
-            raise ValueError, "result is neither 'true' nor 'false': " + boolar
r[i]
+            raise ValueError("result is neither 'true' nor 'false': " + boolarr
[i])
         return boolarr


RefactoringTool: Files that need to be modified:
RefactoringTool: selenium.py

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的鱼塘能养鲲 2024-11-05 19:40:40

2to3 实用程序的官方文档对此进行了介绍: http://docs.python.org/library /2to3.html

默认情况下,它只打印差异以供检查(并可能保存为补丁)。

您必须提供 -w 选项才能实际覆盖原始文件。

This is covered in the official documentation for the 2to3 utility: http://docs.python.org/library/2to3.html

By default, it just prints the diff for review (and possibly saving as a patch).

You must provide the -w option to actually overwrite the original file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文