Save csv data as multiple files.

[?]
Apr 23, 2021, 10:22 AM
CUADTSHQNPGMWHIJCWXKHNNIY4UJOYZ7XBZA5CJ2VVJJAMSNT4BAC

Dependencies

Change contents

  • replacement in src/main.rs at line 5
    [3.59][2.0:10]()
    data,
    [3.59]
    [2.10]
    countries_with_data,
  • edit in src/main.rs at line 7
    [2.26]
    [3.59]
    save_data_csv,
    save_data_json,
    save_series_meta,
    save_unemployment_data,
    title,
    to_tag,
  • replacement in src/main.rs at line 22
    [3.244][3.244:333]()
    // let data = data("SVNCPHPLA01GPM");
    // fs::write("SVNCPHPLA01GPM.csv", &data);
    [3.244]
    [3.333]
    // save_data_csv("SVNCPHPLA01GPM");
    // save_series_meta("SVNCPHPLA01GPM");
  • edit in src/main.rs at line 56
    [3.1143]
    [3.1143]
    // println!("{}", Fred::tags_series("korea"));
    println!("{}", cpi_series(Country::SouthKorea));
  • replacement in src/main.rs at line 61
    [3.1144][3.1144:1208]()
    // println!("{}", Fred::tags_series("cpi;united kingdom"));
    [3.1144]
    [3.1208]
    // save_unemployment_data(&Country::SouthKorea);
    // for country in countries_with_data().iter() {
    // println!("\n{}", country.to_string());
    // save_unemployment_data(&country);
    // }
    // println!("{}", title("LFACTTTTKRA657N"));
  • replacement in src/main.rs at line 70
    [3.1209][2.27:83]()
    // println!("{}", cpi_series(Country::NewZealand));
    [3.1209]
    [2.83]
    // save_data_csv("LRHUTTTTNZA156N");
  • replacement in src/main.rs at line 72
    [2.84][2.84:148]()
    println!("{}", unemployment_series(Country::UnitedStates));
    [2.84]
    [3.1260]
    // println!("{}", to_tag("unemployment", &Country::SouthKorea));
  • edit in src/main.rs at line 74
    [3.1261]
    [3.292]
    // println!("{}", Fred::tags_series(&to_tag("cpi", &Country::SouthKorea)).seriess);
  • edit in src/lib.rs at line 6
    [3.477]
    [3.477]
    use std::fs;
  • edit in src/lib.rs at line 10
    [3.498]
    [3.498]
    use fred_api::response::SeriesItems;
  • replacement in src/lib.rs at line 14
    [3.531][2.149:186]()
    /// Print data for a given `Series`.
    [3.531]
    [3.606]
    /// Save FRED data to disk as json.
  • replacement in src/lib.rs at line 16
    [3.1290][3.1720:1749]()
    /// data("LRUNTTTTSIQ156S");
    [3.614]
    [3.1332]
    /// save_data_json("LRUNTTTTSIQ156S");
  • replacement in src/lib.rs at line 18
    [3.1340][3.1750:1791](),[3.1791][3.1394:1602](),[3.1394][3.1394:1602]()
    pub fn data(series_id: &str) -> String {
    let mut s = String::new();
    for obs in Fred::series_observations(series_id).observations {
    s.push_str(&format!(
    "{}, {}\n",
    obs.date,
    obs.value,
    ));
    [3.1340]
    [3.104]
    pub fn save_data_json(series_id: &str, path: &str) {
    let data = Fred::series_observations_json(series_id);
    let save_path = format!("./data/{}", path);
    fs::create_dir_all(&save_path).unwrap();
    fs::write(
    &format!(
    "{}/{}.json",
    save_path,
    series_id,
    ),
    &data,
    );
    }
    /// Save FRED data to disk as csv.
    /// ```
    /// save_data_csv("LRUNTTTTSIQ156S");
    /// ```
    /// To handle breaks the data is saved in a collection of files "LRUNTTTTSIQ156S_a.csv",
    /// "LRUNTTTTSIQ156S_b.csv" etc.
    ///
    pub fn save_data_csv(series_id: &str, path: &str) {
    let alphabet = "abcdefghijklmnopqrstuvwxyz";
    let mut chars = alphabet.chars();
    let mut contains_data = false;
    let save_path = format!("./data/{}", path);
    let mut data = String::new();
    for obs in Fred::series_observations(series_id).observations.iter() {
    let value: Result<f32, _> = obs.value.parse();
    match value {
    Ok(val) => {
    contains_data = true;
    data.push_str(&obs.to_string());
    data.push('\n');
    },
    Err(val) => {
    if contains_data {
    data.pop();
    fs::create_dir_all(&save_path).unwrap();
    fs::write(
    &format!(
    "{}/{}_{}.csv",
    save_path,
    series_id,
    chars.next().expect("Too many breaks"),
    ),
    &data,
    );
    data = String::new();
    contains_data = false;
    }
    },
    }
  • replacement in src/lib.rs at line 82
    [3.110][3.1603:1622]()
    s.pop();
    s
    [3.110]
    [3.1792]
    if contains_data {
    fs::create_dir_all(&save_path).unwrap();
    fs::write(
    &format!(
    "{}/{}_{}.csv",
    save_path,
    series_id,
    chars.next().expect("Too many breaks"),
    ),
    &data,
    );
    }
  • edit in src/lib.rs at line 97
    [3.1795]
    [2.187]
    /// Save FRED series metadata including title to disk as json.
    /// ```
    /// save_series_meta("LRUNTTTTSIQ156S");
    /// ```
    pub fn save_series_meta(series_id: &str, path: &str) {
    let data = Fred::series(series_id).to_string();
    fs::write(
    &format!(
    "./data/{}/{}.meta",
    path,
    series_id,
    ),
    &data,
    );
    }
  • replacement in src/lib.rs at line 391
    [3.12073][2.351:371](),[2.371][3.15267:15288](),[3.12073][3.15267:15288](),[3.15288][3.12142:12175](),[3.12142][3.12142:12175]()
    // TODO
    panic!()
    // deserialize error
    [3.12073]
    [3.12176]
    vec!(
    "Consumer Price Index: All Items for Korea",
    "Consumer Price Index: All items: Total: Total for the Republic of Korea",
    "Inflation, consumer prices for the Republic of Korea",
    "Consumer Price Index for Republic of Korea",
    "Consumer Price Index: Total All Items for the Republic of Korea",
    )
  • replacement in src/lib.rs at line 471
    [3.19240][2.414:469]()
    Fred::tags_series(&to_tag("cpi", country)).seriess
    [3.19240]
    [2.469]
    Fred::tags_series(&to_tag("cpi", &country)).seriess
  • replacement in src/lib.rs at line 480
    [2.635][2.635:692]()
    pub fn unemployment_series(country: Country) -> String {
    [2.635]
    [2.692]
    pub fn unemployment_series(country: &Country) -> SeriesItems {
    dbg!(country);
    dbg!(country.to_string());
  • edit in src/lib.rs at line 854
    [2.9799]
    [2.9799]
    ),
    )
    }
    Country::SouthKorea => {
    (
    vec!(
    "Male",
    "Female",
    "Youth",
    "15-24",
    "15-64",
    "25-54",
    "55-64",
    ),
    vec!(
    "Rate",
  • replacement in src/lib.rs at line 956
    [2.11856][2.11856:11879]()
    _ => panic!(),
    [2.11856]
    [2.11879]
    country => {
    println!("{:?}", country);
    panic!()
    }
  • replacement in src/lib.rs at line 961
    [2.11886][2.11886:11950]()
    Fred::tags_series(&to_tag("unemployment", country)).seriess
    [2.11886]
    [2.11950]
    Fred::tags_series(&to_tag("unemployment", &country)).seriess
  • edit in src/lib.rs at line 964
    [2.12029][2.12029:12050]()
    .to_string()
  • replacement in src/lib.rs at line 966
    [2.12053][2.12053:12104]()
    fn to_tag(tag: &str, country: Country) -> String {
    [2.12053]
    [3.15822]
    pub fn to_tag(tag: &str, country: &Country) -> String {
  • replacement in src/lib.rs at line 974
    [2.12137][2.12137:12170]()
    fn countries() -> Vec<Country> {
    [2.12137]
    [2.12170]
    /// Save csv data and meta data to file, for a given `Country`.
    pub fn save_unemployment_data(country: &Country) {
    for series in unemployment_series(country).iter() {
    let path = country.as_path();
    save_data_csv(&series.id, &path);
    save_series_meta(&series.id, &path);
    println!("{}", series.id)
    }
    }
    pub fn update_all_unemployment_data() {
    for country in countries_with_data().iter() {
    println!("\n{}", country.to_string());
    save_unemployment_data(&country);
    }
    }
    /// Return all the countries with good data as a `Vec`.
    pub fn countries_with_data() -> Vec<Country> {
  • replacement in src/lib.rs at line 1025
    [2.12957][3.15904:15909](),[3.15904][3.15904:15909]()
    [2.12957]
    pub fn title(series_id: &str) -> String {
    let mut s = String::new();
    for series in Fred::series(series_id).seriess.iter() {
    s.push_str(&series.title)
    }
    s
    }
  • edit in src/countries.rs at line 3
    [3.1656]
    [3.1656]
    #[derive(Clone, Copy, Debug)]
  • edit in src/countries.rs at line 246
    [3.5258]
    [3.5258]
    }
    impl Country {
    /// Return the country name in lowercase with underscores replacing spaces.
    pub fn as_path(&self) -> String {
    self.to_string().to_lowercase().replace(' ', "_")
    }
  • replacement in src/countries.rs at line 284
    [3.7111][3.16061:16131]()
    Country::SouthKorea => "South Korea",
    [3.7111]
    [3.7180]
    Country::SouthKorea => "Korea",