PrePAN

Sign in to PrePAN

Profile

djerius@github

GitHub: djerius PAUSE ID: djerius Bitbucket: djerius

User's Modules

Hash::Wrap (formerly Return::Object) create lightweight on-the-fly objects from hashes

UPDATE

This module is now known as Hash::Wrap

The text below represents the original module, Return::Object. Please see the github repo for the current documentation.

DESCRIPTION

This module provides routines which encapsulate a hash as an object. The object provides methods for keys in the hash; attempting to access a non-existent key via a method will cause an exception.

The impetus for this was to encapsulate data returned from a subroutine or method (hence the name). Returning a bare hash can lead to bugs if there are typos in hash key names when accessing the hash.

It is not necessary for the hash to be fully populated when the object is created. The underlying hash may be manipulated directly, and changes will be reflected in the object's methods. To prevent this, consider using the lock routines in Hash::Util on the object after creation.

Only hash keys which are legal method names will be accessible via object methods.

Object construction and constructor customization

By default Object::Return exports a return_object constructor which, given a hashref, blesses it directly into the Return::Object::Class class.

The constructor may be customized to change which class the object is instantiated from, and how it is constructed from the data. Return::Object uses Exporter::Tiny to perform the customization. For example,

use Return::Object
  return_object => { -as => 'return_cloned_object',
                     -clone => 1 };

will create a version of return_object which clones the passed hash and is imported as return_cloned_object. To import it under the original name, return_object, leave out the -as option.

The following options are available to customize the constructor.

  • -as => subroutine name

    This is optional, and imports the customized version of return_object with the given name.

  • -class => class name

    The object will be blessed into the specified class. If the class should be created on the fly, specify the -create option. See "Object Classes" for what is expected of the object classes. This defaults to Object::Return::Class.

  • -create => boolean

    If true, and -class is specified, a class with the given name will be created.

  • -copy => boolean

    If true, the object will store the data in a shallow copy of the hash. By default, the object uses the hash directly.

  • -clone => boolean

    If true, the object will store the data in a deep copy of the hash, made with "dclone" in Storeable. By default, the object uses the hash directly.

Object Classes

An object class has the following properties:

  • The class must be a subclass of Return::Object::Base.
  • The class typically does not provide any methods, as they would mask a hash key of the same name.
  • The class need not have a constructor. If it does, it is passed a hashref which it should bless as the actual object. For example:

    package My::Result;
    use parent 'Return::Object::Base';
    
    
    sub new {
      my  ( $class, $hash ) = @_;
      return bless $hash, $class;
    }
    

    This excludes having a hash key named new.

Return::Object::Base provides an empty DESTROY method, a can method, and an AUTOLOAD method. They will mask hash keys with the same names.

BUGS AND LIMITATIONS

You can make new bug reports, and view existing ones, through the web interface at https://rt.cpan.org/Public/Dist/Display.html?Name=Return-Object.

SEE ALSO

Please see those modules/websites for more information related to this module.

AUTHOR

Diab Jerius

COPYRIGHT AND LICENSE

This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.

This is free software, licensed under:

The GNU General Public License, Version 3, June 2007

djerius@github 3 comments

Types::PDL PDL types using Type::Tiny

DESCRIPTION

This module provides Type::Tiny compatible types for PDL.

Types

Piddle

Allows an object blessed into the class PDL, e.g.

validate( [pdl], Piddle );

Piddle accepts the following parameters ("Parameters"):

empty
ndims
ndims_min
ndims_max

Parameters

Some types take optional parameters which add additional constraints on the object. For example, to indicate that only empty piddles are accepted:

validate( [pdl], Piddle[ empty => 1 ] );

The available parameters are:

  • empty

    This accepts a boolean value; if true the piddle must be empty (i.e. the isempty method returns true), if false, it must not be empty.

  • null

    This accepts a boolean value; if true the piddle must be a null piddle, if false, it must not be null.

  • ndims

    This specifies a fixed number of dimensions which the piddle must have. Don't mix use this with ndims_min or ndims_max.

  • ndims_min

    The minimum number of dimensions the piddle may have. Don't specify this with ndims.

  • ndims_max

    The maximum number of dimensions the piddle may have. Don't specify this with ndims.

djerius@github 0 comments

Type::TinyX::Facets Easily create a facet parameterized Type::Tiny type

DESCRIPTION

Type::TinyX::Facets make it easy to create parameterized types with facets.

Type::Tiny allows definition of types which can accept parameters:

use Types::Standard -types;

my $t1 = Array[Int];
my $t2 = Tuple[Int, HashRef];

This defines $t1 as an array of integers. and $t2 as a tuple of two elements, an integer and a hash.

Parameters are passed as a list to the parameterized constraint generation machinery, and there is great freedom in how they may be interpreted.

This module makes it easy to create a parameterized type which takes name - value pairs or,facets. (The terminology is taken from Types::XSD::Lite, upon which this module owes its existence.)

THANKS

djerius@github 0 comments

Net::Object::Peer "Network" with pub/sub + direct peer-to-peer messaging

Net::Object::Peer is a Moo Role which implements a publish/subscribe peer-to-peer messaging system, based upon Beam::Emitter. Objects in the network may broadcast events to all subscribers or may send events to a particular subscriber.

Subscriptions and unsubscriptions are tracked and messages will be sent to affected objects upon request.

While Net::Object::Peer is designed around the concept of nodes being objects with methods as event handlers, it retains Beam::Emitter's ability to register code references as well.

Net::Object::Peer::Cookbook provides some recipes.

I'm not terribly enamored of the module name; suggestions for alternatives would be welcome!

djerius@github 0 comments

Sub::QuoteX::Utils Sugar for Sub::Quote

Sub::QuoteX::Utils provides a simplified interface to the process of combining Sub::Quote compatible code references with new code.

Sub::Quote provides a number of routines to make code more performant by inlining separate chunks of code into a single compiled subroutine.

When a chunk of code is compiled into a subroutine by Sub::Quote::quote_sub(), Sub::Quote keeps track of the code and any captured variables used to construct that subroutine, so that new code can be added to the original code and the results compiled into a new subroutine.

Sub::QuoteX::Utils makes that latter process a little easier.

djerius@github 0 comments

PDLx::Algorithm::Center Various ways of centering a dataset

This module collects various algorithms for determining the center of a dataset into one place. It accepts data stored as PDL variables (piddles)

Currently it contains a single function, sigma_clip, which provides an iterative algorithm which successively removes outliers by clipping those whose distances from the current center are greater than a given number of standard deviations.

sigma_clip finds the center of a data set by:

  1. ignoring the data whose distance to the current center is a specified number of standard deviations
  2. calculating a new center by performing a (weighted) centroid of the remaining data
  3. calculating the standard deviation of the distance from the data to the center
  4. repeat at step 1 until either a convergence tolerance has been met or the iteration limit has been exceeded

The initial center may be explicitly specified, or may be calculated by performing a (weighted) centroid of the data.

The initial standard deviation is calculated using the initial center and either the entire dataset, or from a clipped region about the initial center.

sigma_clip can center sparse (e.g., input is a list of coordinates) or dense datasets (input is a hyper-rectangle) with or without weights. It accepts a mask which directs it to use only certain elements in the dataset.

The coordinates may be transformed using (PDL::Transform)[https://metacpan.org/pod/PDL::Transform]. This is mostly useful for dense datasets, where coordinates are generated from the indices of the passed hyper-rectangle. This functionality is not currently documented, as tests for it have not yet been written.

More information is available at the github repo page, https://github.com/djerius/PDLx-Algorithm-Center

djerius@github 0 comments

Module::Build::Pluggable::AuthorTests Plugin to Module::Build to add author tests

This Module::Build::Pluggable plugin adds an authortest action which recursively runs tests in both the normal test directory t, as well as in author-only test directories (by default xt ).

To specify alternate author-only test directories, pass the "test_dirs" option when loading the module, e.g.

use Module::Build::Pluggable ( AuthorTests =>
                                { test_dirs => 'xtt' } );

"test_dirs" will accept either a scalar or an array of directories.

To run the tests,

./Build authortest

djerius@github 0 comments

MooX::Cmd::ChainedOptions Easy access to options up the chain of commands

When used instead of MooX::Options in applications constructed with MooX::Cmd, this class adds attributes to a command object which provide access to command line options passed to commands higher up the command chain.

For example (using the above code which creates an application and a command), the following command line

app --app-opt=FOO cmd --cmd-opt=BAR

would result in the MyApp::Cmd::cmd object having an attribute called app_opt which contains FOO.

Without this module, the MyApp::Cmd::cmd object would have to search through the chain of commands (passed to the execute method, or available via the command_chainmethod)looking for the app_opt attribute.

I'm afraid the name may be confused with "chained" method approaches, but MooX::Cmd uses the "chain" terminology to describe the hierarchy of commands, so I thought I'd stick to that.

Any alternate suggestions?

Thanks, Diab

djerius@github 0 comments

MooX::TaggedAttributes Add tags w/ values to Moo attributes

This module allows one to apply tags with values to attributes in Moo classes and Roles. Why?

Well, in my case I want to able to label attributes so that I can programatically find those with a particular label and operate on them.

For example, let's say that my class has attributes which are PDL objects (or piddles, as we call 'em), and I want to apply a transform just to those objects. Without tags, I'd have to know ahead of time which attributes were PDL objects, and which weren't. That might get complicated if I compose a bunch of roles (especially onto objects at run time).

With tagged attributes, I can do this:

package PDL::Tag;
use Moo::Role;
use MooX::TaggedAttributes -tags => 'piddle';

package MyClass;
use Moo;
use PDL::Tag;

has a => ( is => 'ro' );
has b => ( is => 'ro', piddle => 1 );

my $obj = MyClass->new( ... );

# search for attributes marked as piddles:
my @piddles = keys %{ $obj->_tags->{piddle} };

# and now do some Cool PDL stuff on 'em
$obj->$_->rotate(-1) for @piddles.

I could scan the attributes looking for a particular type, but tags are more elegant, and since they are arbitrary, can encode more information. The tag values are also arbitrary, so you can do anything you want with 'em.

djerius@github 3 comments

Data::Record::Serialize encode and write records with record munging

I found myself in the situation where I needed to output a stream of records (where a record is a set of fields composed of key, scalar value pairs) in a variety of formats (json, yaml, sqlite db) and needed to programmatically select a subset of the fields, sometimes format the values for human readability, etc.

This module stands out from others (well, I found one, Data::Serializer) in that it

  • allows one to rename fields
  • provides type information to encoders that can use it (such as writing to a database)
  • formats fields based on name or type
  • numify values that have been inadvertently not numified
  • allows encoders to write enclosing metadata (such as header records)

The module is only usable with "flat" data structures (no nested hashes or arrays) which are homogeneous in structure.

djerius@github 0 comments