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.