def _format_dict(_dict): if not is_dict(_dict): return _dict for k, v in _dict.items(): # If the value is a dict if is_dict(v): # and only have one value if len(v) == 1: _dict[k] = next(iter(v.values())) else: if len(_dict) == 1: _dict = v return _dict
def match_renames(_col_names): """ Get a list fo columns and return the renamed version. :param _col_names: :return: """ _renamed_columns = [] _actions = df.get_meta("transformations.actions") _rename = _actions.get("rename") def get_name(_col_name): c = _rename.get(_col_name) # The column has not been rename. Get the actual column name if c is None: c = _col_name return c if _rename: # if a list if is_list_of_str(_col_names): for _col_name in _col_names: # The column name has been changed. Get the new name _renamed_columns.append(get_name(_col_name)) # if a dict if is_dict(_col_names): for _col1, _col2 in _col_names.items(): _renamed_columns.append({get_name(_col1): get_name(_col2)}) else: _renamed_columns = _col_names return _renamed_columns
def dataframe(self, data, cols=None, rows=None, pdf=None, n_partitions=1, *args, **kwargs): """ Helper to create dataframe: :param cols: List of Tuple with name, data type and a flag to accept null :param rows: List of Tuples with the same number and types that cols :param pdf: a pandas dataframe :param n_partitions: :return: Dataframe """ if is_dict(data): data = pd.DataFrame(data) df = VaexDataFrame(data) return df
def create(self, obj, method, suffix=None, output="df", additional_method=None, *args, **kwargs): """ This is a helper function that output python tests for Spark DataFrames. :param obj: Object to be tested :param method: Method to be tested :param suffix: The test name will be create using the method param. suffix will add a string in case you want to customize the test name. :param output: can be a 'df' or a 'json' :param additional_method: :param args: Arguments to be used in the method :param kwargs: Keyword arguments to be used in the functions :return: """ buffer = [] def add_buffer(value): buffer.append("\t" + value) # Create name name = [] if method is not None: name.append(method.replace(".", "_")) if additional_method is not None: name.append(additional_method) if suffix is not None: name.append(suffix) test_name = "_".join(name) func_test_name = "test_" + test_name + "()" print("Creating {test} test function...".format(test=func_test_name)) logger.print(func_test_name) if not output == "dict": add_buffer("@staticmethod\n") func_test_name = "test_" + test_name + "()" else: func_test_name = "test_" + test_name + "(self)" filename = test_name + ".test" add_buffer("def " + func_test_name + ":\n") source = "source_df" if obj is None: # Use the main df df_func = self.df elif isinstance(obj, pyspark.sql.dataframe.DataFrame): source_df = "\tsource_df=op.create.df(" + obj.export() + ")\n" df_func = obj add_buffer(source_df) else: source = get_var_name(obj) df_func = obj # Process simple arguments _args = [] for v in args: if is_str(v): _args.append("'" + v + "'") elif is_numeric(v): _args.append(str(v)) elif is_list(v): if is_list_of_strings(v): lst = ["'" + x + "'" for x in v] elif is_list_of_numeric(v) or is_list_of_tuples(v): lst = [str(x) for x in v] elif is_list_of_tuples(v): lst = [str(x) for x in v] _args.append('[' + ','.join(lst) + ']') elif is_dict(v): _args.append(json.dumps(v)) elif is_function(v): _args.append(v.__qualname__) else: # _args.append(get_var_name(v)) _args.append(str(v)) # else: # import marshal # code_string = marshal.dumps(v.__code__) # add_buffer("\tfunction = '" + code_string + "'\n") # import marshal, types # # code = marshal.loads(code_string) # func = types.FunctionType(code, globals(), "some_func_name") print(_args) _args = ','.join(_args) _kwargs = [] # print(_args) # Process keywords arguments for k, v in kwargs.items(): if is_str(v): v = "'" + v + "'" _kwargs.append(k + "=" + str(v)) # Separator if we have positional and keyword arguments separator = "" if (not is_list_empty(args)) & (not is_list_empty(kwargs)): separator = "," if method is None: add_buffer("\tactual_df = source_df\n") else: am = "" if additional_method: am = "." + additional_method + "()" add_buffer("\tactual_df =" + source + "." + method + "(" + _args + separator + ','.join(_kwargs) + ")" + am + "\n") # Apply function to the dataframe if method is None: df_result = self.op.create.df(*args, **kwargs) else: # Here we construct the method to be applied to the source object for f in method.split("."): df_func = getattr(df_func, f) df_result = df_func(*args, **kwargs) # Additional Methods if additional_method is not None: df_result = getattr(df_result, additional_method)() if output == "df": df_result.table() expected = "\texpected_df = op.create.df(" + df_result.export( ) + ")\n" elif output == "json": print(df_result) if is_str(df_result): df_result = "'" + df_result + "'" else: df_result = str(df_result) add_buffer("\tactual_df =json_enconding(actual_df)\n") expected = "\texpected_value =json_enconding(" + df_result + ")\n" elif output == "dict": print(df_result) expected = "\texpected_value =" + str(df_result) + "\n" else: expected = "\t\n" add_buffer(expected) # Output if output == "df": add_buffer( "\tassert (expected_df.collect() == actual_df.collect())\n") elif output == "json": add_buffer("\tassert(expected_value == actual_df)\n") elif output == "dict": add_buffer( "\tself.assertDictEqual(deep_sort(expected_value), deep_sort(actual_df))\n" ) filename = self.path + "//" + filename if not os.path.exists(os.path.dirname(filename)): try: os.makedirs(os.path.dirname(filename)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise # Write file test_file = open(filename, 'w', encoding='utf-8') for b in buffer: test_file.write(b)