dumper
This file is meant to help dump information to files
Dumper
The dumper class is intended to help write information to files. Currently, the supported dump file types are: csv or json.
Attributes:
| Name | Type | Description |
|---|---|---|
file_name |
str
|
The name of the file to write data to. |
file_type |
str
|
The type of the file (either "csv" or "json") determined from the file name. |
Methods:
| Name | Description |
|---|---|
write |
Writes information to the specified output file based on the file type. |
_csv_write |
Writes information to a CSV file. |
_json_write |
Writes information to a JSON file. |
Example
CSV usage:
dumper = Dumper("populations.csv")
# Eugene, OR has a population of 175096
# Livermore, CA has a population of 86803
population_data = {
"City": ["Eugene", "Livermore"],
"State": ["OR", "CA"],
"Population": [175096, 86803]
}
dumper.write(population_data, "w") # Output will be written to populations.csv
Example
JSON usage:
Source code in merlin/common/dumper.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
__init__(file_name)
Initializes the Dumper class and validates the file type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
The name of the file to write data to. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file type is not supported. Supported types are CSV and JSON. |
Source code in merlin/common/dumper.py
write(info_to_write, fmode)
Writes information to the specified output file.
This method determines the file type and calls the appropriate method to write the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info_to_write
|
Dict
|
The information to write to the output file. |
required |
fmode
|
str
|
The file write mode ("w" for write, "a" for append, etc.). |
required |
Source code in merlin/common/dumper.py
dump_handler(dump_file, dump_info)
Handles the process of creating a Dumper object and writing data to an output file.
This function checks if the specified dump file exists to determine the appropriate file write mode (append or write). It then creates a Dumper object and writes the provided information to the file, logging the process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dump_file
|
str
|
The filepath to the file where data will be dumped. |
required |
dump_info
|
Dict
|
A dictionary containing the information to be written
to the |
required |