Skip to content

Instantly share code, notes, and snippets.

@jberger
Created June 25, 2012 19:12
Show Gist options
  • Save jberger/2990606 to your computer and use it in GitHub Desktop.
Save jberger/2990606 to your computer and use it in GitHub Desktop.
PDL::_data_printer
sub _data_printer {
my ($self, $props) = @_;
################################################
# Get Data, Build Structure #
# add new things as [ tag => data ] to @data #
################################################
my @data;
# type
push @data, [ Type => $self->type->realctype ];
# shape
push @data, [ Shape => $self->shape ];
# min and max
my ($min, $max) = $self->minmax;
push @data, [ Min => $min ];
push @data, [ Max => $max ];
# bad?
my $bad_flag = $self->badflag;
$self->check_badflag;
push @data, [ Badflag => ( $bad_flag ? "Yes" : "No" ) ];
push @data, [ 'Has Bads' => ( $self->badflag ? "Yes" : "No" ) ];
$self->badflag($bad_flag);
#####################
# Format the Output #
#####################
$props ||= {};
my $indent = defined $props->{indent} ? $props->{indent} : 4;
my $max_tag_length = List::Util::max map { length $_->[0] } @data;
my $tag_format = ' ' x $indent . '%-' . $max_tag_length . 's : ';
my @formatted =
map { sprintf($tag_format, $_->[0]) . $_->[1] }
@data;
my $data = ref($self) . " {\n";
$data .= join "\n", @formatted;
$data .= "\n}\n";
return $data;
}
# data_printer.t (gist name is for highlighting)
use strict;
use warnings;
use Test::More;
use PDL;
if ( eval { use Data::Printer; 1 } ) {
plan tests => 6;
} else {
plan skip_all => 'Test requires Data::Printer';
}
my $pdl = sequence(10,10);
my $ddp = p $pdl;
like( $ddp, qr/PDL/, "contains the class name PDL" );
like( $ddp, qr/Type\s+: double/, "displays type" );
like( $ddp, qr/Shape\s+: \[10 10\]/, "displays shape" );
like( $ddp, qr/Min\s+: 0/, "displays minimum" );
like( $ddp, qr/Max\s+: 99/, "displays maximum" );
like( $ddp, qr/Bad\s+: No/, "displays badflag status" );
use strict;
use warnings;
use PDL;
use Data::Printer;
my $pdl = sequence(10,10);
p $pdl;
__END__
PDL {
Type : double
Shape : [10 10]
Min : 0
Max : 99
Bad : No
Has Bads : No
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment