重命名外部模块类方法

2024-09-30 00:23:09 发布

您现在位置:Python中文网/ 问答频道 /正文

问题:

如何重命名导入的模块类中存在的方法(函数)?你知道吗

总体思路:

#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风格。我遇到的一个重复问题是详细的方法名(特别是来自seleniumwebdriver模块),这是一个导入的模块类。你知道吗

下面是我想做的事情的摘录。然而,在寻找之后,我不确定如何达到这个目的。我是否需要在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类方法,以供参考:

https://github.com/SeleniumHQ/selenium/blob/18681a6c1b59434f2639c2c56383a58f1118f009/py/selenium/webdriver/firefox/webdriver.py


Tags: 模块方法函数脚本byseleniumargsjob
3条回答

我不知道如何重命名,但只要一个小技巧就可以在这里使用函数指针, 将完整函数存储在变量中,然后调用新函数。你知道吗

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)

在@ashishkamble关于函数指针的建议的基础上,我为我的具体案例找到了一些解决方案。我仍然没有弄清楚如何重命名函数来继承原来的函数类方法的属性,例如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]

您可以从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_selectorWebDriver的方法,而get_attributeWebElement的方法,因此需要更新两个类。。。你知道吗

但是!如果你真的需要让你的行更短,代码更可读,不要急着去做。新的类名和方法名可能会使使用/维护您的代码的人感到困惑。你知道吗

我建议您只需修改代码如下:

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]

请注意,将元素定位器与执行代码分离是PageObject模式的基础,因此无论如何它都不是多余的

相关问题 更多 >

    热门问题