我正在构建一个Python类,该类可用于PySpark数据帧或配置单元表.输入数据可以是table\u name的str
或DataFrame
.Python的最佳实践是什么?我想这样做,以便它根据用例灵活.唯一的区别在于数据应该如何传递给类(作为表或Spark数据帧),其他一切都是一样的.
参见以下示例:
class DataPipeline:
def __init__(self, data):
if isinstance(data, str):
self.df = spark.read.table(data)
elif isinstance(data, DataFrame):
self.df = data
else:
raise ValueError("some error")
def process_data(self):
# do something with the self.df here