简体   繁体   English

'builtin_function_or_method'列出

[英]'builtin_function_or_method' to list

I understand the fact that Python is a dynamic language, but the below code is bothering me. 我了解Python是一种动态语言的事实,但是下面的代码困扰着我。

I have the below simple program , which has some helper function to wrap command execution. 我有下面的简单程序,它具有一些辅助功能来包装命令执行。

EventLoaderToVerticaHelper is a helper class with two methods, so when I call "get_filenames_from_hdfs_with_filter" it should throw an error or return a list of String. EventLoaderToVerticaHelper是带有两个方法的帮助程序类,因此,当我调用“ get_filenames_from_hdfs_with_filter”时,它应该引发错误或返回String列表。

class EventLoaderToVerticaHelper:
    def __init__(self):
        pass

    @staticmethod
    def execute_command(cmd):
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        error_lines = p.stderr.readlines()
        if len(error_lines) > 0:
            raise error_lines
        return p.stdout.readlines

    @staticmethod
    def get_filenames_from_hdfs_with_filter(folder, filetype):
        cmd = "hdfs dfs -ls {0}/*.{1} |  awk '{print $8}'".replace("{0}", folder).replace("{1}", filetype)
        return EventLoaderToVerticaHelper.execute_command(cmd) 

The below code uses the above helper, 下面的代码使用上面的帮助器,

from src.EventLoaderToVerticaHelper import EventLoaderToVerticaHelper
if __name__ == '__main__':
     filelist = EventLoaderToVerticaHelper.get_filenames_from_hdfs_with_filter("/user/cloudera/testfiles", "csv")
     for s in filelist:
        print s

When I run the above program I get below error. 当我运行上述程序时,出现以下错误。 I'm sure that return type if List[Str] 我确定如果List [Str]返回类型

Traceback (most recent call last):
  File "/home/cloudera/PycharmProjects/vertical-event-loader/src/EventLoaderToVertica.py", line 29, in <module>
for s in filelist:
TypeError: 'builtin_function_or_method' object is not iterable

I know that I'm expecting it to behave kind of a Typed language ... I want to method to return List[Str], when there is a exception I want to terminate the program. 我知道我期望它表现出某种类型化的语言的行为...当出现异常情况时,我想方法返回List [Str]以终止程序。

How can I achieve this, I tried with return type and other things but no luck. 我怎么能做到这一点,我尝试了返回类型和其他东西,但没有运气。

return p.stdout.readlines

Should be instead 应该代替

return p.stdout.readlines()

Note that you did call the readlines correctly two lines above, when you read from stderr. 请注意,从stderr读取时,您确实正确地在上面两行调用了readlines。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM