1

I understand the fact that Python is a dynamic language, but the below code is bothering me.

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.

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]

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.

How can I achieve this, I tried with return type and other things but no luck.

Dave
  • 962
  • 5
  • 19
  • 44
  • 9
    You are returning the method `readlines` (`return p.stdout.readlines`), you aren't calling it. – Dimitris Fasarakis Hilliard Aug 22 '17 at 15:40
  • Jim Fasarakis Hilliard you answered the question first, how can mark your comment as answer ? – Dave Aug 22 '17 at 15:44
  • 1
    Also, what do your expect `raise error_lines` to do? The (first) argument to `raise` should be an exception object. – PM 2Ring Aug 22 '17 at 15:44
  • 3
    Checking for output to standard error isn't a good way to determine if a command succeeds. A command can fail without writing to standard error, and it can write diagnostic information while succeeding. – chepner Aug 22 '17 at 15:45
  • 1
    A class with no state and two static methods shouldn't be a class - it should be two module-level functions... – juanpa.arrivillaga Aug 22 '17 at 15:50
  • will check the output as well https://stackoverflow.com/questions/23420990/subprocess-check-output-return-code .. @chepner – Dave Aug 22 '17 at 15:56
  • Will take care of that ... @juanpa.arrivillaga – Dave Aug 22 '17 at 15:57

1 Answers1

5
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.

moshez
  • 36,202
  • 1
  • 22
  • 14