-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
96 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
use clap::builder::Styles; | ||
|
||
#[::linkme::distributed_slice] | ||
pub static PARAMS: [(&str, &str)]; | ||
|
||
pub fn generate_params_help() -> String { | ||
let mut params: HashMap<String, HashSet<String>> = HashMap::default(); | ||
for kv in PARAMS { | ||
params | ||
.entry(kv.0.to_string()) | ||
.and_modify(|s| { | ||
s.insert(kv.1.to_string()); | ||
}) | ||
.or_insert(HashSet::from([kv.1.to_string()])); | ||
} | ||
let mut params: Vec<_> = params | ||
.iter() | ||
.map(|kv| { | ||
let mut descs = Vec::from_iter(kv.1.iter().map(|x| x.clone())); | ||
descs.sort(); | ||
(kv.0.clone(), descs.join("\n\t")) | ||
}) | ||
.collect(); | ||
params.sort_by_key(|x| x.0.clone()); | ||
|
||
let styles = Styles::default(); | ||
let header = styles.get_header(); | ||
let literal = styles.get_literal(); | ||
format!( | ||
"{}Hyperparameters:{}\n", | ||
header.render(), | ||
header.render_reset() | ||
) + ¶ms | ||
.iter() | ||
.map(|kv| { | ||
format!( | ||
" {}{}{}\n\t{}", | ||
literal.render(), | ||
kv.0, | ||
literal.render_reset(), | ||
kv.1 | ||
) | ||
}) | ||
.collect::<Vec<String>>() | ||
.join("\n\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use hyperparameter::PARAMS; | ||
use linkme::distributed_slice; | ||
|
||
#[test] | ||
fn test_cli() { | ||
#[distributed_slice(PARAMS)] | ||
static param1: (&str, &str) = ( | ||
"key1", "val1" | ||
); | ||
|
||
assert!(PARAMS.len()==1); | ||
|
||
for kv in PARAMS { | ||
println!("{} => {}", kv.0, kv.1); | ||
} | ||
} |