如何重命名导入的模块类中存在的方法(函数)?
总体思路:
#Current Generalized Code
from module import myImportedClass
X = myImportedClass.some_very_long_method_name_that_exists(*args).another_method(*args)
#Becomes Shorter Code
X = myImportedClass.SVLM(*args).AM(*args)
我有几个可以工作的网页抓取脚本,但是希望将长行代码重新编写为PEP8样式。我遇到的一个重复问题是详细的方法名称(尤其是来自Selenium webdriver模块的名称),这是一个导入的模块类。
下面是摘录,传达了我想做的事情。但是,搜索后,我不确定如何实现此目的。我是否需要在Python脚本中编写某种本地类/函数,以使别名的行为类似于原始方法名?
#Current Example with Long Imported Class Method Names
from selenium import webdriver
d = webdriver.Firefox()
def load_job_cards(counter, job_key, job_type, filestem='newjobs'):
posts = d.find_elements_by_xpath("//div[@class=' row result clickcard']")
job_names = [j.find_element_by_css_selector("a[class='turnstileLink']").get_attribute('title') for j in posts]
#...more code...
#Would Like Imported Class Method to be Renamed, e.g.:
def load_job_cards(counter, job_key, job_type, filestem='newjobs'):
posts = d.find_elements_by_xpath("//div[@class=' row result clickcard']")
job_names = [j.fcss("a[class='turnstileLink']").ga('title') for j in posts]
#...more code...
作为参考,导入的WebDriver类方法记录在以下脚本中:
答案 0 :(得分:2)
您可以继承myImportedClass
并定义新方法:
class MyNewClass(myImportedClass):
def SVLM(self, *args):
return self.another_method(args)
那你就可以做
from some_module import MyNewClass
MyNewClass(ARGS).SVLM(ARGS2)
根据您的示例,请注意,find_element_by_css_selector
是WebDriver
的方法,而get_attribute
是WebElement
的方法,因此您需要更新两个类...
但是!如果您确实需要使行更短并且代码更易读,请不要着急。新的类和方法名称可能会使使用/维护您的代码的人感到困惑。
我建议您仅修改代码,如下所示:
post_locator = "xpath", "//div[@class=' row result clickcard']"
link_locator = "css", "a[class='turnstileLink']"
def load_job_cards(counter, job_key, job_type, filestem='newjobs'):
posts = d.find_elements(*post_locator)
job_names = [j.find_element(*link_locator).get_attribute('title') for j in posts]
P.S。请注意,从执行代码中分离元素定位符是PageObject模式的基础,因此无论如何它都不是多余的
答案 1 :(得分:1)
在@Ashish Kamble关于函数指针的建议的基础上,我为我的特定情况找到了一些解决方案。我仍然没有弄清楚如何重命名函数以继承原始的class.method的属性基于现有的web元素,例如j.fcss("a[class='turnstileLink']").ga('title')
。
用另一个功能解决原始问题,但是我得到了:
from selenium import webdriver
d = webdriver.Firefox()
def find_css(element, css):
return element.find_element_by_css_selector(css)
def load_job_cards(counter, job_key, job_type, filestem='newjobs'):
posts = d.find_elements_by_xpath("//div[@class=' row result clickcard']")
#Breaking the Long Line with the New Function
css = "a[class='turnstileLink']"
job_names = [find_css(j, css).get_attribute('title') for j in posts]
#Other Code Where This is Also Useful
companies = [find_css(c, "span[class='company']").text for c in posts]
locations = [find_css(l, "span[class='location']").text for l in posts]
job_names = [slvm2(j, css, ga) for j in posts]
#Alt Solution 1
def find_css(element, css):
return element.find_element_by_css_selector(css)
def ga(element, attribute):
return element.get_attribute(attribute)
def load_job_cards(counter, job_key, job_type, filestem='newjobs'):
posts = d.find_elements_by_xpath("//div[@class=' row result clickcard']")
css = "a[class='turnstileLink']"
job_names = [ga(find_css(j, css), 'title') for j in posts]
#Alt Solution 2 (Less Generalizable)
def SVLM(j, css, ga):
return j.find_element_by_css_selector(css).get_attribute(ga)
def load_job_cards(counter, job_key, job_type, filestem='newjobs'):
posts = d.find_elements_by_xpath("//div[@class=' row result clickcard']")
css = "a[class='turnstileLink']"
job_names = [SVLM(j, css, 'title') for j in posts]
答案 2 :(得分:0)
我不知道如何重命名,但是使用函数指针可以在这里起作用, 将完整功能存储在变量中,然后调用新功能。
newFunName = Package.Module.Function
newFunName()
我希望这会对您有所帮助
from module import myImportedClass
def do(ar) myImportedClass.some_very_long_method_name_that_exists(ar).another_method(ar)
成为这样的短代码调用
X = do(**args)