Count/ReadBallots.pm

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