diff --git a/benchmark/loops_test.go b/benchmark/loops_test.go index 396063d..484ebd0 100644 --- a/benchmark/loops_test.go +++ b/benchmark/loops_test.go @@ -12,7 +12,7 @@ var slice = make([]uint64, 500*500) func BenchmarkRangeReadSliceByIndex(b *testing.B) { for i := 0; i < b.N; i++ { - for j, _ := range slice { + for j := range slice { Val = slice[j] + 1 } } @@ -28,7 +28,7 @@ func BenchmarkRangeReadSliceByValue(b *testing.B) { func BenchmarkRangeWriteSlice(b *testing.B) { for i := 0; i < b.N; i++ { - for j, _ := range slice { + for j := range slice { slice[j] = Val + 1 } } diff --git a/cmd/root.go b/cmd/root.go index b5ec370..72a05df 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,19 +33,19 @@ import ( "github.com/spf13/viper" ) -var cfgFile string +var configurationFilePath string -const ErrorConfiguring = 1 -const ErrorReading = 2 -const ErrorBalancing = 3 -const ErrorDeliberating = 4 -const ErrorFormatting = 5 +const errorConfiguring = 1 +const errorReading = 2 +const errorBalancing = 3 +const errorDeliberating = 4 +const errorFormatting = 5 var rootCmd = &cobra.Command{ Use: "mj FILE", Version: version.GitSummary, - Short: "Rank proposals in Majority Judgment polls", - Long: `Resolve majority judgment polls from an input CSV. + Short: "Resolve and inspect Majority Judgment polls", + Long: `Resolve Majority Judgment polls from an input CSV. Say you have the following tally in a CSV file named example.csv: @@ -107,7 +107,7 @@ Gnuplots are meant to be piped as scripts to gnuplot http://www.gnuplot.info outputFormatter = &formatter.GnuplotOpinionFormatter{} } else { fmt.Printf("Chart `%s` is not supported. Supported charts: merit, opinion\n", chart) - os.Exit(ErrorConfiguring) + os.Exit(errorConfiguring) } } else if "gnuplot-merit" == format || "gnuplot_merit" == format { outputFormatter = &formatter.GnuplotMeritFormatter{} @@ -117,7 +117,7 @@ Gnuplots are meant to be piped as scripts to gnuplot http://www.gnuplot.info panic("todo: see issue https://github.com/MieuxVoter/majority-judgment-cli/issues/11") } else { fmt.Printf("Format `%s` is not supported. Supported formats: text, csv, json, yaml\n", format) - os.Exit(ErrorConfiguring) + os.Exit(errorConfiguring) } proposalsTallies := make([]*judgment.ProposalTally, 0, 10) @@ -147,7 +147,7 @@ Gnuplots are meant to be piped as scripts to gnuplot http://www.gnuplot.info _, tallies, proposals, grades, errReader := tallyReader.Read(&csvReader) if errReader != nil { fmt.Printf("Failed to read input: " + errReader.Error() + "\n") - os.Exit(ErrorReading) + os.Exit(errorReading) } maximumPrecisionScale := 1000000.0 @@ -186,7 +186,7 @@ Gnuplots are meant to be piped as scripts to gnuplot http://www.gnuplot.info if nil != amountOfJudgesErr || amountOfJudges < 0 { fmt.Printf("Unrecognized --judges amount `%s`. "+ "Use a positive integer, like so: --judges 42\n", amountOfJudgesStr) - os.Exit(ErrorConfiguring) + os.Exit(errorConfiguring) } if amountOfJudges > 0 { @@ -204,7 +204,7 @@ Gnuplots are meant to be piped as scripts to gnuplot http://www.gnuplot.info defaultGrade, defaultToErr := reader.ReadNumber(defaultTo) if nil != defaultToErr { fmt.Printf("Unrecognized --default grade `%s`.\n", defaultTo) - os.Exit(ErrorConfiguring) + os.Exit(errorConfiguring) } balancerErr = poll.BalanceWithStaticDefault(uint8(defaultGrade)) } else { @@ -212,14 +212,14 @@ Gnuplots are meant to be piped as scripts to gnuplot http://www.gnuplot.info } if balancerErr != nil { fmt.Println("Balancing Error:", balancerErr) - os.Exit(ErrorBalancing) + os.Exit(errorBalancing) } deliberator := &judgment.MajorityJudgment{} result, err := deliberator.Deliberate(poll) if err != nil { fmt.Println("Deliberation Error:", err) - os.Exit(ErrorDeliberating) + os.Exit(errorDeliberating) } desiredWidth, widthErr := strconv.Atoi(cmd.Flags().Lookup("width").Value.String()) @@ -241,7 +241,7 @@ Gnuplots are meant to be piped as scripts to gnuplot http://www.gnuplot.info ) if formatterErr != nil { fmt.Println("Formatter Error:", err) - os.Exit(ErrorFormatting) + os.Exit(errorFormatting) } fmt.Println(out) }, @@ -260,8 +260,8 @@ func init() { // Cobra supports persistent flags, which, if defined here, // will be global for your application. - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.mj.yaml)") - //rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") + rootCmd.PersistentFlags().StringVar(&configurationFilePath, "config", "", "config file (default is $HOME/.mj.yaml)") + //rootCmd.PersistentFlags().StringVar(&configurationFilePath, "config", "", "config file (default is $HOME/.cobra.yaml)") rootCmd.Flags().StringP("format", "f", "text", "desired format of the output") rootCmd.Flags().StringP("default", "d", "0", "default grade to use when unbalanced") rootCmd.Flags().StringP("width", "w", "79", "desired width, in characters") @@ -275,9 +275,9 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { - if cfgFile != "" { + if configurationFilePath != "" { // Use config file from the flag. - viper.SetConfigFile(cfgFile) + viper.SetConfigFile(configurationFilePath) } else { // Find home directory. home, err := os.UserHomeDir() diff --git a/reader/csv_tally.go b/reader/csv_tally.go index f6839ed..c9f2ba2 100644 --- a/reader/csv_tally.go +++ b/reader/csv_tally.go @@ -9,8 +9,14 @@ import ( "strings" ) +// CsvTallyReader reads a poll's tally in a CSV like so: +// Nutriscore, G, F, E, D, C, B, A +// Pizza, 4, 2, 3, 4, 5, 4, 1 +// Chips, 5, 3, 2, 4, 4, 3, 2 type CsvTallyReader struct{} +// Read the input CSV and return as much data as we can. +// Read does not fill the `judgments` because they are not in a tally CSV func (r CsvTallyReader) Read(input *io.Reader) ( judgments [][]int, tallies [][]float64, @@ -80,7 +86,7 @@ func (r CsvTallyReader) Read(input *io.Reader) ( proposals = append(proposals, strings.TrimSpace(row[0])) } else { j := len(proposals) - proposals = append(proposals, "Proposal "+Alphabet[j:j+1]) + proposals = append(proposals, "Proposal "+alphabet[j:j+1]) } // III.c Read the actual tallies diff --git a/reader/reader.go b/reader/reader.go index f9b3202..457494f 100644 --- a/reader/reader.go +++ b/reader/reader.go @@ -61,7 +61,7 @@ func ReadNumber(s string) (n float64, err error) { return strconv.ParseFloat(strings.TrimSpace(s), 64) } -const Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // GenerateDummyGradeNames generates dummy grade names in reverse alphabetical order func GenerateDummyGradeNames(thatMany int) (grades []string, err error) { @@ -69,11 +69,11 @@ func GenerateDummyGradeNames(thatMany int) (grades []string, err error) { err = fmt.Errorf("cannot generate negative amounts of grades (tried %d)", thatMany) return } - if thatMany > len(Alphabet) { - err = fmt.Errorf("no more than %d different grades can be generated (tried %d)", len(Alphabet), thatMany) + if thatMany > len(alphabet) { + err = fmt.Errorf("no more than %d different grades can be generated (tried %d)", len(alphabet), thatMany) return } - grades = strings.Split(Alphabet[0:thatMany], "") + grades = strings.Split(alphabet[0:thatMany], "") for i, j := 0, thatMany-1; i < j; i, j = i+1, j-1 { grades[i], grades[j] = grades[j], grades[i] }