>From http://kschiess.github.io/parslet/transform.html

class MyTransform < Parslet::Transform

    rule('a') { 'b' }

  end
  MyTransform.new.apply('a') # => "b"

A Transform class, when applied, looks through it's known rules, and
applies any that match. Applying a rule replaces the matched object
with the result of the associated block. It has to match a whole
object, so you can't match a single key from a hash. You have to match
the whole hash. One way around this is to make your parser generate a
tree that is a little less flat. So.. if instead your output was

{..., :ctime => {time: "Thu Aug 29 18:09:26 2013"} }

You could then do something like this

class MyTransform < Parslet::Transform

    rule(:time => simple(:time)) { DateTime.parse(time) }

end
MyTransform.new.apply(your_tree)

This would result in a tree with the whole {time => ...} hash replaced
the a DateTime value. In this case:

{..., :ctime => #<DateTime: 2013-08-29T18:09:26+00:00 ...> }

I hope this helps.


---
"No man is an island... except Philip"


On Sat, Aug 31, 2013 at 9:30 PM, Ra <[email protected]> wrote:

> Dear Kaspar,
> your parselet is wonderful and I'm using it as part of a library for
> managing TORQUE/PBS, parselet is use mostly for extracting info form qstat.
> This is just an example of the object returned from my parser:
>
>  {:job_id=>"2763.spark.space.ad"@8,
>  :job_name=>"STDIN"@42,
>  :job_owner=>"[email protected]"@64,
>  :resources_used_cput=>"00:00:00"@116,
>  :resources_used_mem=>"3152kb"@150,
>  :resources_used_vmem=>"32528kb"@183,
>  :resources_used_walltime=>"00:07:40"@221,
>  :job_state=>"R"@246,
>  :queue=>"bio"@260,
>  :server=>"spark.ingm.ad"@277,
>  :checkpoint=>"u"@308,
>  :ctime=>"Thu Aug 29 18:09:26 2013"@322}
>
>
> some keys are "maybe" so I do not have the guarantees that all of them
> will be available to all the objects, btw that is fine because qstat not
> always report everything.
> I read the documentation many times but I did understand how to convert
> the parser's result:
>
> for instance, I'd like to have ctime as a DateTime object and
> resources_used_mem
> as a numeric ...
>
>
> could you give me some advice on how to correctely transform my output ?
>
> Cheers.
>
> --
> Raoul
>

Reply via email to