tv_lib

Crates.iotv_lib
lib.rstv_lib
version0.1.2
sourcesrc
created_at2022-05-13 11:37:52.191982
updated_at2022-05-13 11:56:54.357072
descriptionA library port of tv cli
homepage
repositoryhttps://github.com/alexhallam/tv_lib
max_upload_size
id585800
size81,234
Alex Hallam (alexhallam)

documentation

README

Tidy Viewer Library (tv_lib)

This is a Rust library that allows users to grab csv text as formatted by the tv cli command line utility and use it as they see fit

tv lib example

fn main() {
    // define path
    let in_file_path: &str = "examples/data/uspop.csv";
    // one liner
    let vec_vec_str = tv_lib::print::read_csv(in_file_path);
    //print
    println!("{:?}", vec_vec_str[2][0..6].to_vec());
    println!("{:?}", vec_vec_str[3][0..6].to_vec());
}
["Population", "NA", "7610", "NA", "NA", "NA"]
["Latitude", "65.2", "60.5", "33.7", "31.7", "32.3"]

In this small example see the features ported from tv.

  1. Missing values are filled with NA
  2. Latitude is normally a decimal that goes out 7 places! See that the sig figs are set to only show one significant digit.

Remember, the goal is tv is about data visualization. In any practical calculation it is important to have a long decimal for lat/lon?

Installation

Include in Cargo.toml

tv-lib = "0.1.0"

Examples

Examples may be run with cargo run --example <file in examples dir>

The following are available

cargo run --example short_read_csv
cargo run --example print_csv_from_str
cargo run --example format_csv_from_str
cargo run --example read_then_format_csv_from_str
cargo run --example read_then_print_csv_from_str

Examples Explicit

Some times it is too hard to want to go into a directory to copy and paste a chunk of code 😉.

Format from strings

fn main() {
    // define path
    let in_file_path: &str = "examples/data/uspop.csv";
    // one liner
    let vec_vec_str = tv_lib::print::read_csv(in_file_path);
    //print
    println!("{:?}", vec_vec_str);
}
// format_from_csv_str
fn main() {
    let data = "\
City,State,Population,Latitude,Longitude
Davidsons Landing,AK,,65.2419444,-165.2716667
Kenai,AK,7610,60.5544444,-151.2583333
Oakman,AL,,33.7133333,-87.3886111";

    let vec_vec_str = tv_lib::print::format_from_csv_str(data);
    println!("{:?}",vec_vec_str)
}
// print_csv_from_str
fn main() {
    let data = "\
City,State,Population,Latitude,Longitude
Davidsons Landing,AK,,65.2419444,-165.2716667
Kenai,AK,7610,60.5544444,-151.2583333
Oakman,AL,,33.7133333,-87.3886111";
    tv_lib::print::print_from_csv_str(data);
}

Read CSV Then Format

use csv::Reader;
use csv::Writer;
use std::fs::File;

fn main() {
    let in_file_path: &str = "examples/data/uspop.csv";

    // csv::ReaderBuilder
    let mut r: Reader<File> = csv::ReaderBuilder::new()
        .has_headers(false)
        .delimiter(b',')
        .from_path(in_file_path)
        .unwrap();

    // Collect items in reader
    let rdr = r
        .records()
        .into_iter()
        .map(|x| x.expect("a csv record"))
        .collect::<Vec<_>>();

    // convert items to wtr
    let rows: usize = rdr.len();
    let mut wtr = Writer::from_writer(vec![]);
    for row in 0..rows {
        wtr.write_record(&rdr[row]).unwrap();
    }

    // convert wtr to string
    let data = String::from_utf8(wtr.into_inner().unwrap()).unwrap();

    // use tv_lib: print_from_csv_str
    let vec_vec_str = tv_lib::print::format_from_csv_str(data.as_str());

    println!("{:?}", vec_vec_str[1][0..6].to_vec());
    println!("{:?}", vec_vec_str[1][0..6].to_vec());
    println!("{:?}", vec_vec_str[2][0..6].to_vec());
    println!("{:?}", vec_vec_str[3][0..6].to_vec());
    println!("{:?}", vec_vec_str[4][0..6].to_vec());
}
use csv::Reader;
use csv::Writer;
use std::fs::File;

fn main() {

    // path
    let in_file_path: &str = "examples/data/uspop.csv";

    // csv::ReaderBuilder
    let mut r: Reader<File> = csv::ReaderBuilder::new()
        .has_headers(false)
        .delimiter(b',')
        .from_path(in_file_path)
        .unwrap();

    // Collect items in reader
    let rdr = r
        .records()
        .into_iter()
        .map(|x| x.expect("a csv record"))
        .collect::<Vec<_>>();

    // convert items to wtr
    let rows: usize = rdr.len();
    let mut wtr = Writer::from_writer(vec![]);
    for row in 0..rows {
        wtr.write_record(&rdr[row]).unwrap();
    }

    // convert wtr to string
    let data = String::from_utf8(wtr.into_inner().unwrap()).unwrap();

    // use tv_lib: print_from_csv_str
    tv_lib::print::print_from_csv_str(data.as_str());
}

Commit count: 4

cargo fmt