Data/Frame/IO/CSV.pm

filter: Statementstmt 94.5% 156 / 165 Branchbran 67.0% 55 / 82 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 95.8% 23 / 24 Podpod 100% 2 / 2 totaltotal 84.4% 239 / 283 CC 46 SCAR 28.7
CC
46
Cov
85%
from_csv
32.7
to_csv
18.3
__ANON__
7.0
CRAP 17.7
↑ Summary Data/Frame/PDL.pm »
1 package Data::Frame::IO::CSV;
2 11 $Data::Frame::IO::CSV::VERSION = '0.006005';
3 # ABSTRACT: Partial class for data frame's conversion from/to CSV
4  
5 11 ▶use Data::Frame::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use PDL::Core qw(pdl null);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use PDL::Primitive ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use PDL::Factor ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use PDL::SV ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use PDL::DateTime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use PDL::Types ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 ▶use List::AllUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use Ref::Util qw(is_plain_arrayref is_plain_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use Scalar::Util qw(openhandle looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use Type::Params;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use Types::Standard qw(Any ArrayRef CodeRef Enum HashRef Map Maybe Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use Types::PDL qw(Piddle);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 11 ▶use Data::Frame::Util qw(guess_and_convert_to_pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 ▶use Data::Frame::Types qw(DataType);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 19 ▶classmethod from_csv ($file, :$header=true, :$sep=",", :$quote='"',
Subroutine from_csv: called CC=26 SCAR=34.9
Pod documented
29 :$na=[qw(NA BAD)], :$col_names=undef, :$row_names=undef,
30 Map[Str, DataType] :$dtype={},
31 :$strings_as_factors=false
32 19 ▶ ) {
Branch: 10 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for classmethod from_csv (expected 2, got ' . @_ . ')') 0 19 19
@_ > 2 and (@_ - 2) % 2 and Function::Parameters::_croak("Odd number of paired arguments for classmethod from_csv") 0 19 19
exists ${__rest}{'header'} ? : 14 5 19
exists ${__rest}{'sep'} ? : 0 19 19
exists ${__rest}{'quote'} ? : 0 19 19
exists ${__rest}{'na'} ? : 1 18 19
exists ${__rest}{'dtype'} ? : 6 13 19
exists ${__rest}{'strings_as_factors'} ? : 0 19 19
keys %{__rest} and Function::Parameters::_croak('In classmethod from_csv: no such named parameter: ' . join(", ", sort(keys %{__rest}))) 0 19 19
Type::Tiny::XS::AUTO::TC5($dtype) or Function::Parameters::_croak('In classmethod from_csv: parameter 8 ($dtype): ' . ${my $v = bless( {"_build_coercion" => sub { package Type::Tiny; use warnings; use strict; my $coercion = shift(); my $built = $self->coercion_generator->($self, $P, @args); $coercion->add_type_coercions(@{$built->type_coercion_map;}) if $built; $coercion->freeze;},"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Types::Standard::Map; no warnings; use strict; my $hash = shift(); ($keys->check($_) or return) foreach (keys %$hash); ($values->check($_) or return) foreach (values %$hash); return !0;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/x86_64-linux/Function/Parameters.pm","line" => 98,"package" => "Function::Parameters"},"deprecated" => !!0,"display_name" => "Map[Str,DataType]","inlined" => sub { package Types::Standard::Map; no warnings; use strict; my $h = $_[1]; return "$xsubname($h)" if $xsubname and not $Type::Tiny::AvoidCallbacks; my $p = &Types::Standard::HashRef()->inline_check($h); my $k_check = $k->inline_check('$k'); my $v_check = $v->inline_check('$v'); "$p and do { " . 'my $ok = 1; ' . "for my \$v (values %{$h}) { " . "(\$ok = 0, last) unless $v_check " . '}; ' . "for my \$k (keys %{$h}) { " . "(\$ok = 0, last) unless $k_check " . '}; ' . '$ok ' . '}';},"name" => "__ANON__","parameters" => [bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; Type::Tiny::XS::Str($_[0]) or _failed_check(9, 'Str', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Str","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Types::Standard; no warnings; use strict; ref \$_ eq 'SCALAR' or ref \(my $val = $_) eq 'SCALAR';},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 291,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Str","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; use warnings; use strict; $Type::Tiny::AvoidCallbacks ? goto $orig_inlined : "$xsubname($_[1])";},"library" => "Types::Standard","name" => "Str","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; defined $_[0] and not ref $_[0] or _failed_check(8, 'Value', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Value","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Types::Standard; no warnings; use strict; not ref $_;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 276,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Value","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; "defined($_[1]) and not ref($_[1])";},"library" => "Types::Standard","name" => "Value","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; defined $_[0] or _failed_check(7, 'Defined', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Defined","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complementary_type" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; defined $_[0] and _failed_check(6, 'Undef', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Undef","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complementary_type" => {},"constraint" => sub { package Types::Standard; no warnings; use strict; not defined $_;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 254,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Undef","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; "!defined($_[1])";},"library" => "Types::Standard","name" => "Undef","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; '???'; $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Item","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Type::Tiny; use warnings; use strict; !0;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 226,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Item","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; '!!1';},"library" => "Types::Standard","name" => "Item","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; '???'; $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => do{my $o},"display_name" => "to_Any","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complement_name" => "None","constraint" => do{my $o},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 220,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Any","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; '!!1';},"library" => "Types::Standard","name" => "Any","type_default" => sub { package Types::Standard; no warnings; use strict; return undef;},"uniq" => 1}, 'Type::Tiny' ),"uniq" => 2}, 'Type::Tiny' ),"type_default" => sub { package Types::Standard; no warnings; use strict; return undef;},"uniq" => 6}, 'Type::Tiny' ),"constraint" => sub { package Types::Standard; no warnings; use strict; defined $_;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 262,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Defined","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; "defined($_[1])";},"library" => "Types::Standard","name" => "Defined","parent" => {},"uniq" => 7}, 'Type::Tiny' ),"uniq" => 8}, 'Type::Tiny' ),"sorter" => sub { package Types::Standard; no warnings; use strict; $_[0] cmp $_[1];},"type_default" => sub { package Types::Standard; no warnings; use strict; return '';},"uniq" => 9}, 'Type::Tiny' ),bless( {"_overload_coderef" => sub { package Sub::Defer; use warnings; use strict; $undeferred ||= undefer_sub($deferred_info->[4]); goto \&$undeferred;},"_overload_coderef_no_rebuild" => 1,"_overrides_assert_return" => !!0,"compiled_type_constraint" => sub ;,"constraint" => do{my $o},"definition_context" => {"file" => "/home/cpancover/.cpan/build/Data-Frame-0.006005-0/blib/lib/Data/Frame/Types.pm","line" => 27,"package" => "Data::Frame::Types"},"deprecated" => !!0,"display_name" => "DataType","exception_class" => "Error::TypeTiny::Assertion","library" => "Data::Frame::Types","name" => "DataType","parent" => bless( {"coercion" => bless( {"_compiled_type_constraint_check" => do{my $o},"frozen" => 1,"name" => "__ANON__","type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 1069,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Enum[\"ushort\",\"long\",\"indx\",\"longlong\",\"float\",\"double\",\"string\",\"factor\",\"logical\",\"datetime\"]","exception_class" => "Error::TypeTiny::Assertion","name" => "__ANON__","uniq" => 87,"unique_values" => ["datetime","double","factor","float","indx","logical","long","longlong","string","ushort"],"values" => ["ushort","long","indx","longlong","float","double","string","factor","logical","datetime"]}, 'Type::Tiny::Enum' ),"uniq" => 88}, 'Type::Tiny' )],"parent" => bless( {"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; Ref::Util::XS::is_plain_hashref($_[0]) or _failed_check(26, 'Map', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Map","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"coercion_generator" => sub { package Types::Standard::Map; no warnings; use strict; my($parent, $child, $kparam, $vparam) = @_; return unless $kparam->has_coercion or $vparam->has_coercion; my $kcoercable_item = $kparam->has_coercion ? $kparam->coercion->_source_type_union : $kparam; my $vcoercable_item = $vparam->has_coercion ? $vparam->coercion->_source_type_union : $vparam; my $C = 'Type::Coercion'->new('type_constraint', $child); if (!$kparam->has_coercion || $kparam->coercion->can_be_inlined and !$vparam->has_coercion || $vparam->coercion->can_be_inlined and $kcoercable_item->can_be_inlined and $vcoercable_item->can_be_inlined) { $C->add_type_coercions($parent, &Types::Standard::Stringable(sub { my @code; push @code, 'do { my ($orig, $return_orig, %new) = ($_, 0);'; push @code, 'for (keys %$orig) {'; push @code, sprintf('++$return_orig && last unless (%s);', $kcoercable_item->inline_check('$_')); push @code, sprintf('++$return_orig && last unless (%s);', $vcoercable_item->inline_check('$orig->{$_}')); push @code, sprintf('$new{(%s)} = (%s);', $kparam->has_coercion ? $kparam->coercion->inline_coercion('$_') : '$_', $vparam->has_coercion ? $vparam->coercion->inline_coercion('$orig->{$_}') : '$orig->{$_}'); push @code, '}'; push @code, '$return_orig ? $orig : \\%new'; push @code, '}'; join $", @code; } )); } else { $C->add_type_coercions($parent, sub { my $value = @_ ? $_[0] : $_; my %new; foreach my $k (keys %$value) { return $value unless $kcoercable_item->check($k) and $vcoercable_item->check($value->{$k}); $new{$kparam->has_coercion ? $kparam->coerce($k) : $k} = $vparam->has_coercion ? $vparam->coerce($value->{$k}) : $value->{$k}; } return \%new; } ); } return $C;},"compiled_type_constraint" => do{my $o},"constraint" => do{my $o},"constraint_generator" => sub { package Types::Standard::Map; no warnings; use strict; return $meta->get_type('Map') unless @_; Type::Tiny::check_parameter_count_for_parameterized_type('Types::Standard', 'Map', \@_, 2, 2); my($keys, $values) = @_; Types::TypeTiny::is_TypeTiny($keys) or _croak "First parameter to Map[`k,`v] expected to be a type constraint; got $keys"; Types::TypeTiny::is_TypeTiny($values) or _croak "Second parameter to Map[`k,`v] expected to be a type constraint; got $values"; my @xsub; do { my(@known) = map({my $known = Type::Tiny::XS::is_known($_->compiled_check); defined $known ? $known : ();} $keys, $values); if (@known == 2) { my $xsub = Type::Tiny::XS::get_coderef_for(sprintf('Map[%s,%s]', @known)); push @xsub, $xsub if $xsub; } }; sub { my $hash = shift(); ($keys->check($_) or return) foreach (keys %$hash); ($values->check($_) or return) foreach (values %$hash); return !0; } , @xsub;},"deep_explanation" => sub { package Types::Standard::Map; no warnings; use strict; require B; my($type, $value, $varname) = @_; my($kparam, $vparam) = @{$type->parameters;}; foreach my $k (sort keys %$value) { unless ($kparam->check($k)) { return [sprintf('"%s" constrains each key in the hash with "%s"', $type, $kparam), @{$kparam->validate_explain($k, sprintf('key %s->{%s}', $varname, B::perlstring($k)));}]; } unless ($vparam->check($value->{$k})) { return [sprintf('"%s" constrains each value in the hash with "%s"', $type, $vparam), @{$vparam->validate_explain($value->{$k}, sprintf('%s->{%s}', $varname, B::perlstring($k)));}]; } } return;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 675,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Map","exception_class" => "Error::TypeTiny::Assertion","inline_generator" => sub { package Types::Standard::Map; no warnings; use strict; my($k, $v) = @_; return unless $k->can_be_inlined and $v->can_be_inlined; my $xsubname; do { my(@known) = map({my $known = Type::Tiny::XS::is_known($_->compiled_check); defined $known ? $known : ();} $k, $v); if (@known == 2) { $xsubname = Type::Tiny::XS::get_subname_for(sprintf('Map[%s,%s]', @known)); } }; return sub { my $h = $_[1]; return "$xsubname($h)" if $xsubname and not $Type::Tiny::AvoidCallbacks; my $p = &Types::Standard::HashRef()->inline_check($h); my $k_check = $k->inline_check('$k'); my $v_check = $v->inline_check('$v'); "$p and do { " . 'my $ok = 1; ' . "for my \$v (values %{$h}) { " . "(\$ok = 0, last) unless $v_check " . '}; ' . "for my \$k (keys %{$h}) { " . "(\$ok = 0, last) unless $k_check " . '}; ' . '$ok ' . '}'; } ;},"library" => "Types::Standard","my_methods" => {"hashref_allows_key" => sub { package Types::Standard::Map; no warnings; use strict; my $self = shift(); my($key) = @_; return Types::Standard::is_Str($key) if $self == &Types::Standard::Map(); my $map = $self->find_parent(sub { $_->has_parent and $_->parent == &Types::Standard::Map(); } ); my($kcheck, $vcheck) = @{$map->parameters;}; ($kcheck || &Types::Standard::Any())->check($key);},"hashref_allows_value" => sub { package Types::Standard::Map; no warnings; use strict; my $self = shift(); my($key, $value) = @_; return !1 unless $self->my_hashref_allows_key($key); return !0 if $self == &Types::Standard::Map(); my $map = $self->find_parent(sub { $_->has_parent and $_->parent == &Types::Standard::Map(); } ); my($kcheck, $vcheck) = @{$map->parameters;}; ($kcheck || &Types::Standard::Any())->check($key) and ($vcheck || &Types::Standard::Any())->check($value);}},"name" => "Map","name_generator" => sub { package Type::Tiny; use warnings; use strict; defined $_ and s/[\x00-\x1F]//gms foreach (my($s, @a) = @_); sprintf '%s[%s]', $s, join(',', map((!defined($_) ? 'undef' : (!ref($_) && /\W/ ? B::perlstring($_) : $_)), @a));},"parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; Ref::Util::XS::is_plain_hashref($_[0]) or _failed_check(22, 'HashRef', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_HashRef","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"coercion_generator" => bless( ["HashRef","coercion_generator"], 'Types::Standard::LazyLoad' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Types::Standard; no warnings; use strict; ref $_ eq 'HASH';},"constraint_generator" => bless( ["HashRef","constraint_generator"], 'Types::Standard::LazyLoad' ),"deep_explanation" => bless( ["HashRef","deep_explanation"], 'Types::Standard::LazyLoad' ),"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 544,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "HashRef","exception_class" => "Error::TypeTiny::Assertion","inline_generator" => bless( ["HashRef","inline_generator"], 'Types::Standard::LazyLoad' ),"inlined" => sub { package Types::Standard; no warnings; use strict; !$Type::Tiny::AvoidCallbacks ? "Ref::Util::XS::is_plain_hashref($_[1])" : "ref($_[1]) eq 'HASH'";},"library" => "Types::Standard","my_methods" => {"hashref_allows_key" => bless( ["HashRef","hashref_allows_key"], 'Types::Standard::LazyLoad' ),"hashref_allows_value" => bless( ["HashRef","hashref_allows_value"], 'Types::Standard::LazyLoad' )},"name" => "HashRef","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; not ref $_[0] and _failed_check(16, 'Ref', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Ref","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Types::Standard; no warnings; use strict; ref $_;},"constraint_generator" => sub { package Types::Standard; no warnings; use strict; return $meta->get_type('Ref') unless @_; Type::Tiny::check_parameter_count_for_parameterized_type('Types::Standard', 'Ref', \@_, 1); my $reftype = shift(); $reftype =~ /^(SCALAR|ARRAY|HASH|CODE|REF|GLOB|LVALUE|FORMAT|IO|VSTRING|REGEXP|Regexp)$/i or _croak "Parameter to Ref[`a] expected to be a Perl ref type; got $reftype"; $reftype = "$reftype"; return sub { ref $_[0] and builtin::reftype($_[0]) eq $reftype; } ;},"deep_explanation" => sub { package Types::Standard; no warnings; use strict; require B; my($type, $value, $varname) = @_; my $param = $type->parameters->[0]; return if $type->check($value); my $reftype = builtin::reftype($value); return [sprintf('"%s" constrains reftype(%s) to be equal to %s', $type, $varname, B::perlstring($param)), sprintf('reftype(%s) is %s', $varname, defined $reftype ? B::perlstring($reftype) : 'undef')];},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 438,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Ref","exception_class" => "Error::TypeTiny::Assertion","inline_generator" => sub { package Types::Standard; no warnings; use strict; my $reftype = shift(); return sub { my $v = $_[1]; &$maybe_load_modules('Scalar::Util', "ref($v) and Scalar::Util::reftype($v) eq q($reftype)"); } ;},"inlined" => sub { package Types::Standard; no warnings; use strict; "!!ref($_[1])";},"library" => "Types::Standard","name" => "Ref","parent" => {},"uniq" => 16}, 'Type::Tiny' ),"type_default" => sub { package Types::Standard; no warnings; use strict; return {};},"type_default_generator" => sub { package Types::Standard; no warnings; use strict; return $Type::Tiny::parameterize_type->type_default if @_ < 2; return undef;},"uniq" => 22}, 'Type::Tiny' ),"type_default_generator" => sub { package Types::Standard; no warnings; use strict; return $Type::Tiny::parameterize_type->type_default;},"uniq" => 26}, 'Type::Tiny' ),"type_default" => do{my $o},"uniq" => 95}, 'Type::Tiny' );$v->{"coercion"}{"type_constraint"} = $v;$v->{"compiled_type_constraint"} = $v->{"coercion"}{"_compiled_type_constraint_check"};$v->{"parameters"}[0]{"coercion"}{"type_constraint"} = $v->{"parameters"}[0];$v->{"parameters"}[0]{"compiled_type_constraint"} = $v->{"parameters"}[0]{"coercion"}{"_compiled_type_constraint_check"};$v->{"parameters"}[0]{"parent"}{"coercion"}{"type_constraint"} = $v->{"parameters"}[0]{"parent"};$v->{"parameters"}[0]{"parent"}{"compiled_type_constraint"} = $v->{"parameters"}[0]{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parameters"}[0]{"parent"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parameters"}[0]{"parent"}{"parent"};$v->{"parameters"}[0]{"parent"}{"parent"}{"compiled_type_constraint"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"coercion"}{"type_constraint"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"};$v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"compiled_type_constraint"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"complementary_type"} = $v->{"parameters"}[0]{"parent"}{"parent"};$v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"};$v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"compiled_type_constraint"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"};$v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"compiled_type_constraint"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"constraint"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"constraint"};$v->{"parameters"}[0]{"parent"}{"parent"}{"parent"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"};$v->{"parameters"}[1]{"constraint"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"constraint"};$v->{"parameters"}[1]{"parent"}{"coercion"}{"_compiled_type_constraint_check"} = $v->{"parameters"}[1]{"compiled_type_constraint"};$v->{"parameters"}[1]{"parent"}{"coercion"}{"type_constraint"} = $v->{"parameters"}[1]{"parent"};$v->{"parameters"}[1]{"parent"}{"compiled_type_constraint"} = $v->{"parameters"}[1]{"compiled_type_constraint"};$v->{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"};$v->{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"constraint"} = $v->{"parameters"}[0]{"parent"}{"parent"}{"complementary_type"}{"parent"}{"constraint"};$v->{"parent"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"parent"};$v->{"parent"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"parent"}{"parent"};$v->{"parent"}{"parent"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"parent"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"parent"}{"parent"} = $v->{"parameters"}[0]{"parent"}{"parent"};$v->{"type_default"} = $v->{"parent"}{"parent"}{"type_default"}; \$v}->get_message($dtype)) 19 0 19
Condition33%1/3
@_ > 2 and (@_ - 2) % 2
!ll&&!rl&&r
0190
MC/DC0%0/2
@_ > 2 and (@_ - 2) % 2
@_ > 2 (@_ - 2) % 2
Truth table33%1/3
A and B
A= @_ > 2B= (@_ - 2) % 2
ABresult
0X0
100
111
33 state $check = Type::Params::compile(
34 19 ▶ ( ArrayRef [Str] )->plus_coercions( Any, sub { [$_] } ) );
Subroutine __ANON__: called CC=1 SCAR=0.0
35 19 ($na) = $check->($na);
36  
37 # TODO
38 146 ▶ my $check_name = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 146 my ($name) = @_;
40 146 return $name;
41 19 };
42  
43 19 my $csv = Text::CSV->new(
44 {
45 binary => 1,
46 auto_diag => 1,
47 sep => $sep,
48 quote => $quote
49 }
50 );
51  
52 19 my $fh = openhandle($file);
53 19 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 19 0 19
54 19 ▶ open $fh, "<:encoding(utf8)", "$file" or die "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<:encoding(utf8)", "$file" or die "${file}: $!" 19 0 19
55 }
56 19 my @col_names;
57 19 ▶ if ( defined $col_names ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col_names) { } 0 19 19
58 0 @col_names = $col_names->flatten;
59 }
60 else {
61 19 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 19 0 19
62 19 eval {
63 # suppress possible warning message on parsing header
64 19 $csv->auto_diag(0);
65 19 $csv->header( $fh, { munge_column_names => 'none' } );
66 13 @col_names = $csv->column_names;
67 };
68 19 $csv->auto_diag(1); # restore auto_diag
69 19 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 6 13 19
70  
71 # rewind as first line read by $csv->header
72 6 seek( $fh, 0, 0 );
73 6 my $first_row = $csv->getline($fh);
74 6 @col_names = @$first_row;
75 }
76 }
77 }
78  
79 # if first column has no header, we take this first column as row names.
80 19 my $row_names_from_first_column = ( length( $col_names[0] ) == 0 );
81 19 ▶ if ($row_names_from_first_column) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_names_from_first_column) 6 13 19
82 6 shift @col_names;
83 }
84 19 @col_names = map { $check_name->($_) } @col_names;
85  
86 19 my %columns = map { $_ => [] } @col_names;
87  
88 19 my @row_names;
89 19 my $rows = $csv->getline_all($fh);
90  
91 19 ▶ my $offset = $row_names_from_first_column ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$row_names_from_first_column ? : 6 13 19
92 19 for my $row (@$rows) {
93 72803 ▶ if ($row_names_from_first_column) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_names_from_first_column) 192 72611 72803
94 192 push @row_names, $row->[0];
95 }
96 72803 for my $i ( 0 .. $#col_names ) {
97 657587 my $col = $col_names[$i];
98 657587 push @{ $columns{$col} }, $row->[ $i + $offset ];
99 }
100 }
101  
102 19 ▶ if ($row_names_from_first_column) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_names_from_first_column) { } 6 13 19
103 6 $row_names = \@row_names;
104 }
105 else {
106 13 ▶ if ( defined $row_names ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row_names) 0 13 13
107 0 ▶ if ( looks_like_number($row_names) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($row_names)) 0 0 0
108 0 my $col_index = int($row_names);
109 0 $row_names = $columns{ $col_names[$col_index] };
110 }
111 }
112 }
113  
114 19 state $additional_type_to_piddle = {
115 datetime => \&Data::Frame::Util::_datetime_from_arrayref,
116 factor => \&Data::Frame::Util::_factor_from_arrayref,
117 logical => \&Data::Frame::Util::_logical_from_arrayref,
118 pdlsv => \&Data::Frame::Util::_pdlsv_from_arrayref,
119 };
120  
121 19 my $package_pdl_core = Package::Stash->new('PDL::Core');
122 146 ▶ my $to_piddle = sub {
Subroutine __ANON__: called CC=5 SCAR=19.4
123 146 my ($name) = @_;
124 146 my $x = $columns{$name};
125  
126 146 ▶ if ( my $type = $dtype->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $dtype->{$name}) { } 7 139 146
127 7 my $piddle_maker = $additional_type_to_piddle->{$type};
128 7 ▶ unless ($piddle_maker) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($piddle_maker) 0 7 7
129 0 ▶ if ( my $f = $package_pdl_core->get_symbol("&$type") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $package_pdl_core->get_symbol("&$type")) 0 0 0
130 0 ▶ $piddle_maker = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 my ( $x, $na ) = @_;
132 0 Data::Frame::Util::_numeric_from_arrayref( $x, $na,
133 $f );
134 0 };
135 }
136 }
137 7 ▶ if ($piddle_maker) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piddle_maker) { } 7 0 7
138 7 return $piddle_maker->( $x, $na );
139 }
140 else {
141 0 die "Invalid data type '$type'";
142 }
143 }
144 else {
145 139 return guess_and_convert_to_pdl(
146 $x,
147 na => $na,
148 strings_as_factors => $strings_as_factors
149 );
150 }
151 19 };
152  
153 my $df = $class->new(
154 columns => [
155 map {
156 19 ▶ $_ => $to_piddle->($_),
Branch: 1 branch
BranchTrueFalseTotal
$row_names ? : 6 13 19
157 } @col_names
158 ],
159 ( $row_names ? ( row_names => $row_names ) : () ),
160 );
161  
162 19 return $df;
163 11 }
164  
165  
166 3 ▶method to_csv ($file, :$sep=',', :$quote='"', :$na='NA',
Subroutine to_csv: called CC=17 SCAR=29.1
Pod documented
167 3 ▶ :$col_names=true, :$row_names=true) {
Branch: 8 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method to_csv (expected 2, got ' . @_ . ')') 0 3 3
@_ > 2 and (@_ - 2) % 2 and Function::Parameters::_croak("Odd number of paired arguments for method to_csv") 0 3 3
exists ${__rest}{'sep'} ? : 0 3 3
exists ${__rest}{'quote'} ? : 0 3 3
exists ${__rest}{'na'} ? : 1 2 3
exists ${__rest}{'col_names'} ? : 0 3 3
exists ${__rest}{'row_names'} ? : 2 1 3
keys %{__rest} and Function::Parameters::_croak('In method to_csv: no such named parameter: ' . join(", ", sort(keys %{__rest}))) 0 3 3
Condition66%2/3
@_ > 2 and (@_ - 2) % 2
!ll&&!rl&&r
120
MC/DC0%0/2
@_ > 2 and (@_ - 2) % 2
@_ > 2 (@_ - 2) % 2
Truth table66%2/3
A and B
A= @_ > 2B= (@_ - 2) % 2
ABresult
0X0
100
111
168 3 my $csv = Text::CSV->new(
169 {
170 binary => 1,
171 auto_diag => 1,
172 sep => $sep,
173 quote => $quote,
174 eol => "\n",
175 }
176 );
177  
178 3 my $fh = openhandle($file);
179 3 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 3 0 3
180 3 ▶ open $fh, ">", "$file" or die "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", "$file" or die "${file}: $!" 3 0 3
181 }
182  
183 3 ▶ my $row_names_data = $row_names ? $self->row_names : undef;
Branch: 1 branch
BranchTrueFalseTotal
$row_names ? : 1 2 3
184 3 ▶ if ($col_names) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_names) 3 0 3
185 3 ▶ my @header = ( ( $row_names ? '' : () ), @{ $self->names } );
Branch: 1 branch
BranchTrueFalseTotal
$row_names ? : 1 2 3
186 3 $csv->print( $fh, \@header );
187 }
188  
189 # a hash to store isbad info for each column
190 3 my %isbad = map { $_ => $self->column($_)->isbad; } ( $self->names->flatten );
191  
192 3 ▶ for ( my $i = 0 ; $i < $self->nrow ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->nrow) 188 3 191
193 my @row = (
194 ( $row_names ? $row_names_data->at($i) : () ),
195 (
196 map {
197 $self->_format_cell(
198 $self->column($_), $i,
199 na => $na,
200 1288 isbad => $isbad{$_}
201 );
202 188 ▶ } @{ $self->names }
Branch: 1 branch
BranchTrueFalseTotal
$row_names ? : 32 156 188
203 )
204 );
205 188 $csv->print( $fh, \@row );
206 }
207 11 }
208  
209 11 1;
210  
211 __END__
↑ Summary Data/Frame/PDL.pm »