Count/ReadBallots.pm

filter: Statementstmt 95.4% 84 / 88 Branchbran 79.1% 19 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 93.0% 120 / 129 CC 22 SCAR 17.7
CC
22
Cov
92%
read_ballots
11.0
write_range_ballots
6.6
read_range_ballots
5.0
CRAP 5.9
« Count/TextTableTiny.pm ↑ Summary Count/Matrix.pm »
1 package Vote::Count::ReadBallots;
2  
3 26 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶no warnings qw/experimental/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use Path::Tiny 0.108;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 26 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 # use Data::Dumper;
11  
12 # ABSTRACT: Read Ballots for Vote::Count. Toolkit for vote counting.
13  
14 26 our $VERSION='1.10';
15  
16 =head1 NAME
17  
18 Vote::Count::ReadBallots
19  
20 =head1 VERSION 1.10
21  
22 =head1 SYNOPSIS
23  
24 use Vote::Count::ReadBallots;
25  
26 my $data1 = read_ballots('t/data/data1.txt');
27  
28 =head1 Description
29  
30 Reads a file containing vote data. Retruns a HashRef of a Vote::Count BallotSet.
31  
32 All public methods are exported.
33  
34 =head1 BallotSet Data Structure
35  
36 ballots {
37 CHOCOLATE:MINTCHIP:VANILLA {
38 count 1,
39 votevalue 1, # needed for STV support
40 votes [
41 [0] "CHOCOLATE",
42 [1] "MINTCHIP",
43 [2] "VANILLA"
44 ]
45 },
46 },
47 choices {
48 CHOCOLATE 1,
49 MINTCHIP 1,
50 VANILLA 1
51 },
52 votescast 1,
53 comment "# Optional Comment",
54 options {
55 rcv 1
56 }
57  
58 =head1 Data File Format
59  
60 # This is a comment, optional.
61 :CHOICES:VANILLA:CHOCOLATE:STRAWBERRY:MINTCHIP:CARAMEL:RUMRAISIN
62 5:VANILLA:CHOCOLATE:STRAWBERRY
63 RUMRAISIN
64  
65 CHOICES must be defined before any vote lines. or an error will be thrown. CHOICES must only be defined once. These two rules are to protect against errors in manually prepared files.
66  
67 A data line may begin with a number or a choice. When there is no number the line is counted as being a single ballot. The number represents the number of ballots identical to that one; this notation will both dramatically shrink the data files and improve performance.
68  
69 =head2 read_ballots
70  
71 Reads a data file in the standard Vote::Count format and returns a BallotSet.
72  
73 =head2 write_ballots
74  
75 write_ballots( $BallotSet, $newfile);
76  
77 Write out a ballotset. Useful for creating a compressed version of a raw file.
78  
79 =head1 Range Ballots
80  
81 Range Ballots are supported in both JSON and YAML format. The read method doesn't perform validation like B<read_ballots> does.
82  
83 =head2 Range Ballot Format in JSON
84  
85 {
86 "choices": [
87 "TWEEDLEDEE",
88 "TWEEDLEDUM",
89 "HUMPTYDUMPTY"
90 ],
91 "ballots": [
92 {
93 "votes": {
94 "TWEEDLEDEE": 1,
95 "TWEEDLEDUM": 1,
96 "HUMPTYDUMPTY": 3
97 },
98 "count": 3
99 }
100 ],
101 "depth": 3
102 }
103  
104 =head2 read_range_ballots
105  
106 Requires a parameter of a JSON or YAML file. The second parameter may be either 'json' or 'yaml', defaulting to 'json'.
107  
108 my $BestFastFood = read_range_ballots('t/data/fastfood.range.json');
109 my $BestFastFood = read_range_ballots('t/data/fastfood.range.yml', 'yaml');
110  
111 =head2 write_range_ballots
112  
113 Takes three parameters, a BallotSet, a file location, and a value of 'json' or 'yaml'. The first two parameters are required, the third defaults to 'json'.
114  
115 write_range_ballots( $BestFastFood, '/tmp/fast.json', 'json' );
116  
117 =cut
118  
119 26 use Exporter::Easy ( EXPORT =>
120 [qw( read_ballots write_ballots read_range_ballots write_range_ballots)],
121 26 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 26 my $coder = Cpanel::JSON::XS->new->ascii->pretty;
124  
125 ▶sub _choices ( $choices ) {
Subroutine _choices: called CC=2 SCAR=6.9
126 104 my %C = ();
127 104 $choices =~ m/^\:CHOICES\:(.*)/;
128 104 for my $choice ( split /:/, $1 ) {
129 915 $C{$choice} = 1;
130 }
131 104 return \%C;
132 }
133  
134 ▶sub read_ballots( $filename ) {
Subroutine read_ballots: called CC=11 SCAR=24.0
Pod documented
135 103 my %data = (
136 'choices' => undef,
137 'ballots' => {},
138 'options' => { 'rcv' => 1 },
139 'votescast' => 0,
140 'comment' => ''
141 );
142 BALLOTREADLINES:
143 103 for my $line_raw ( path($filename)->lines({chomp => 1}) ) {
144 1881 ▶ if ( $line_raw =~ m/^\#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_raw =~ /^\#/u) 65 1816 1881
145 65 $data{'comment'} .= $line_raw;
146 65 next BALLOTREADLINES;
147 }
148 1816 ▶ if ( $line_raw =~ m/^\:CHOICES\:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_raw =~ /^\:CHOICES\:/u) 105 1711 1816
149 105 ▶ if ( $data{'choices'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{'choices'}) { } 2 103 105
150 2 croak("File $filename redefines CHOICES \n$line_raw\n");
151 }
152 103 else { $data{'choices'} = _choices($line_raw); }
153 103 next;
154 }
155 1711 my $line = $line_raw;
156 1711 ▶ next unless ( $line =~ /\w/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\w/u 15 1696 1711
157 1696 $line =~ s/^(\d+)\://;
158 1696 ▶ my $numbals = $1 ? $1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 1449 247 1696
159 1696 $data{'votescast'} += $numbals;
160 1696 ▶ if ( $data{'ballots'}{$line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{'ballots'}{$line}) { } 93 1603 1696
161 $data{'ballots'}{$line}{'count'} =
162 93 $data{'ballots'}{$line}{'count'} + $numbals;
163 }
164 else {
165 1603 my @votes = ();
166 1603 for my $choice ( split( /:/, $line ) ) {
167 5189 ▶ unless ( $data{'choices'}{$choice} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data{"choices"}{$choice}) 2 5187 5189
168 die "Choice: $choice is not in defined choice list: "
169 2 . join( ", ", keys( $data{'choices'}->%* ) )
170 . "\n -- $line\n";
171 }
172 5187 push @votes, $choice;
173 }
174 1601 $data{'ballots'}{$line}{'count'} = $numbals;
175 1601 $data{'ballots'}{$line}{'votes'} = \@votes;
176 }
177 }
178 99 for my $K ( keys $data{'ballots'}->%* ) { $data{'ballots'}{$K}{'votevalue'} = 1 }
179 99 return \%data;
180 }
181  
182 ▶sub write_ballots ( $BallotSet, $destination ) {
Subroutine write_ballots: called CC=4 SCAR=13.9
Pod documented
183 1 my @data = ('# Data rewritten in compressed form.');
184 1 my $choicelist = join( ':', sort keys( $BallotSet->{'choices'}->%* ) );
185 1 push @data, "CHOICES:$choicelist";
186 1 for my $k ( sort keys $BallotSet->{'ballots'}->%* ) {
187 5 my $cnt = $BallotSet->{'ballots'}{$k}{'count'};
188 5 push @data, "$cnt:$k";
189 }
190 1 ▶ for my $D (@data) { $D .= "\n" if $D !~ /\n$/ }
Branch: 1 branch
BranchTrueFalseTotal
unless $D =~ /\n$/u 7 0 7
191 1 path($destination)->spew(@data);
192 }
193  
194 1 ▶sub write_range_ballots ( $BallotSet, $destination, $format = 'json' ) {
Subroutine write_range_ballots: called CC=4 SCAR=18.9
Pod documented
195 1 $BallotSet->{'choices'} = [ sort keys $BallotSet->{'choices'}->%* ];
196 1 ▶ if ( $format eq 'json' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'json') { } 1 0 1
elsif ($format eq 'yaml') { } 0 0 0
197 1 path($destination)->spew( $coder->encode($BallotSet) );
198 }
199 elsif ( $format eq 'yaml' ) {
200 0 $BallotSet = Load path->($destination)->slurp;
201 0 path($destination)->spew( Dump $BallotSet);
202 }
203 0 else { die "invalid score ballot format $format." }
204 }
205  
206 18 ▶sub read_range_ballots ( $source, $format = 'json' ) {
Subroutine read_range_ballots: called CC=5 SCAR=16.2
Pod documented
207 18 my $BallotSet = undef;
208 18 ▶ if ( $format eq 'json' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'json') { } 15 3 18
elsif ($format eq 'yaml') { } 3 0 3
209 15 $BallotSet = $coder->decode( path($source)->slurp );
210 }
211 elsif ( $format eq 'yaml' ) {
212 3 $BallotSet = Load path($source)->slurp;
213 }
214 0 else { die "invalid score ballot format $format." }
215 18 $BallotSet->{'votescast'} = 0;
216 18 $BallotSet->{'options'} = { 'range' => 1, 'rcv' => 0 };
217 18 my @choices = $BallotSet->{'choices'}->@*;
218 18 $BallotSet->{'choices'} = { map { $_ => 1 } @choices };
219 18 for my $ballot ( $BallotSet->{'ballots'}->@* ) {
220 88 $BallotSet->{'votescast'} += $ballot->{'count'};
221 }
222 18 return $BallotSet;
223 }
224  
225 26 1;
226  
227 #buildpod
228 #FOOTER
229  
230 =pod
231  
232 BUG TRACKER
233  
234 L<https://github.com/brainbuz/Vote-Count/issues>
235  
236 AUTHOR
237  
238 John Karr (BRAINBUZ) brainbuz@cpan.org
239  
240 CONTRIBUTORS
241  
242 Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
243  
244 LICENSE
245  
246 This module is released under the GNU Public License Version 3. See license file for details. For more information on this license visit L<http://fsf.org>.
247  
248 =cut
249  
« Count/TextTableTiny.pm ↑ Summary Count/Matrix.pm »