4545 parse_date ,
4646 parse_date_range ,
4747)
48- from hdx .utilities .dictandlist import merge_two_dictionaries , write_list_to_csv
48+ from hdx .utilities .dictandlist import merge_two_dictionaries
4949from hdx .utilities .downloader import Download
5050from hdx .utilities .loader import load_json
5151from hdx .utilities .path import script_dir_plus_file
52- from hdx .utilities .saver import save_json
52+ from hdx .utilities .saver import save_iterable , save_json
5353from hdx .utilities .typehint import ListTuple , ListTupleDict
5454from hdx .utilities .uuid import is_valid_uuid
5555
@@ -2619,11 +2619,12 @@ def generate_resource_from_rows(
26192619 self ,
26202620 folder : str ,
26212621 filename : str ,
2622- rows : List [ListTupleDict ],
2622+ rows : Iterable [ListTupleDict ],
26232623 resourcedata : Dict ,
26242624 headers : Optional [ListTuple [str ]] = None ,
2625+ format : str = "csv" ,
26252626 encoding : Optional [str ] = None ,
2626- ) -> "Resource" :
2627+ ) -> Optional [ "Resource" ] :
26272628 """Write rows to csv and create resource, adding it to the dataset.
26282629 The headers argument is either a row number (rows start counting at
26292630 1), or the actual headers defined as a list of strings. If not set, all
@@ -2632,72 +2633,27 @@ def generate_resource_from_rows(
26322633 Args:
26332634 folder (str): Folder to which to write file containing rows
26342635 filename (str): Filename of file to write rows
2635- rows (List [ListTupleDict]): List of rows in dict or list form
2636+ rows (Iterable [ListTupleDict]): List of rows in dict or list form
26362637 resourcedata (Dict): Resource data
26372638 headers (Optional[ListTuple[str]]): List of headers. Defaults to None.
2639+ format (str): Format to write. Defaults to csv.
26382640 encoding (Optional[str]): Encoding to use. Defaults to None (infer encoding).
26392641
26402642 Returns:
2641- Resource: The created resource
2643+ Optional[ Resource] : The created resource or None if not created
26422644 """
26432645 filepath = join (folder , filename )
2644- write_list_to_csv (filepath , rows , columns = headers , encoding = encoding )
2646+ res = save_iterable (
2647+ filepath , rows , columns = headers , format = format , encoding = encoding
2648+ )
2649+ if not res :
2650+ return None
26452651 resource = res_module .Resource (resourcedata )
2646- resource .set_format ("csv" )
2652+ resource .set_format (format )
26472653 resource .set_file_to_upload (filepath )
26482654 self .add_update_resource (resource )
26492655 return resource
26502656
2651- def generate_qc_resource_from_rows (
2652- self ,
2653- folder : str ,
2654- filename : str ,
2655- rows : List [Dict ],
2656- resourcedata : Dict ,
2657- hxltags : Dict [str , str ],
2658- columnname : str ,
2659- qc_identifiers : ListTuple [str ],
2660- headers : Optional [ListTuple [str ]] = None ,
2661- encoding : Optional [str ] = None ,
2662- ) -> Optional ["Resource" ]:
2663- """Generate QuickCharts rows by cutting down input rows by relevant
2664- identifiers and optionally restricting to certain columns. Output to
2665- csv and create resource, adding it to the dataset.
2666-
2667- Args:
2668- folder (str): Folder to which to write file containing rows
2669- filename (str): Filename of file to write rows
2670- rows (List[Dict]): List of rows in dict form
2671- resourcedata (Dict): Resource data
2672- hxltags (Dict[str,str]): Header to HXL hashtag mapping
2673- columnname (str): Name of column containing identifier
2674- qc_identifiers (ListTuple[str]): List of ids to match
2675- headers (Optional[ListTuple[str]]): List of headers to output. Defaults to None (all headers).
2676- encoding (Optional[str]): Encoding to use. Defaults to None (infer encoding).
2677-
2678- Returns:
2679- Optional[Resource]: The created resource or None
2680- """
2681- qc_rows = []
2682- for row in rows :
2683- if row [columnname ] in qc_identifiers :
2684- if headers :
2685- qcrow = {x : row [x ] for x in headers }
2686- else :
2687- qcrow = row
2688- qc_rows .append (qcrow )
2689- if len (qc_rows ) == 0 :
2690- return None
2691- qc_rows .insert (0 , hxltags )
2692- return self .generate_resource_from_rows (
2693- folder ,
2694- filename ,
2695- qc_rows ,
2696- resourcedata ,
2697- headers = headers ,
2698- encoding = encoding ,
2699- )
2700-
27012657 def generate_resource_from_iterable (
27022658 self ,
27032659 headers : ListTuple [str ],
0 commit comments