How to dynamically build a Cheetah3 placeholder variable name? - cheetah

The bash shell lets you do this with ${!variablename}. This will retrieve the bash variable whose name is contained within the variable named 'variablename'. I'm looking for a way to do that same thing with Cheetah3 (not with necessarily the same syntax, of course - but I am looking for some way to achieve that capability within Cheetah3 itself.)
Here's an example using bash to show what I mean:
% ONE="1"
% TWO="2"
% CHOICE="ONE"
% echo ${!CHOICE}
1
% CHOICE="TWO"
% echo ${!CHOICE}
2

I'm not sure it's possible. My advice is to use a dictionary:
$variables = {
"ONE": "1",
"TWO": "2",
}
$CHOICE = "ONE"
echo $variables[$CHOICE] # -> 1
$CHOICE = "TWO"
echo $variables[$CHOICE] # -> 2

Related

How to get readable Data::Printer output inside neovim?

I'm not sure what I need to tweak here. When I exec my Perl file from within neovim I get unreadable output. I'm using Data::Printer 1.000004
nvim --version
NVIM v0.7.0-dev+1135-gfdea15723
cat ~/.dataprinter
array_max = 5000
end_separator = 1
filters = DB, DateTime, JSON, URI
hash_separator = ' => '
index = 1
scalar_quotes = '
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw( state );
use List::SomeUtils qw( part );
my $i = 0;
my #part = part { $i++ % 2 } 1 .. 8;
use DDP;
p #part;
~
~
~
~
~
~
~
~
~
~
~
~
~
~
:call ExecFile()
:!"/Users/olafalders/Documents/perl/part.pl"
^[[0;38;2;102;217;239m[^[[0m
^[[0;38;2;161;187;197m[0] ^[[0m^[[0;38;2;102;217;239m[^[[0m
^[[0;38;2;161;187;197m[0] ^[[0m^[[0;38;2;247;140;106m1^[[0m^[[0;38;2;102;217;239m,^[[0m
^[[0;38;2;161;187;197m[1] ^[[0m^[[0;38;2;247;140;106m3^[[0m^[[0;38;2;102;217;239m,^[[0m
^[[0;38;2;161;187;197m[2] ^[[0m^[[0;38;2;247;140;106m5^[[0m^[[0;38;2;102;217;239m,^[[0m
^[[0;38;2;161;187;197m[3] ^[[0m^[[0;38;2;247;140;106m7^[[0m^[[0;38;2;102;217;239m,^[[0m
^[[0;38;2;102;217;239m]^[[0m^[[0;38;2;102;217;239m,^[[0m
^[[0;38;2;161;187;197m[1] ^[[0m^[[0;38;2;102;217;239m[^[[0m
^[[0;38;2;161;187;197m[0] ^[[0m^[[0;38;2;247;140;106m2^[[0m^[[0;38;2;102;217;239m,^[[0m
^[[0;38;2;161;187;197m[1] ^[[0m^[[0;38;2;247;140;106m4^[[0m^[[0;38;2;102;217;239m,^[[0m
^[[0;38;2;161;187;197m[2] ^[[0m^[[0;38;2;247;140;106m6^[[0m^[[0;38;2;102;217;239m,^[[0m
^[[0;38;2;161;187;197m[3] ^[[0m^[[0;38;2;247;140;106m8^[[0m^[[0;38;2;102;217;239m,^[[0m
^[[0;38;2;102;217;239m]^[[0m^[[0;38;2;102;217;239m,^[[0m
^[[0;38;2;102;217;239m]^[[0m
The actual output should look like:
[
[0] [
[0] 1,
[1] 3,
[2] 5,
[3] 7,
],
[1] [
[0] 2,
[1] 4,
[2] 6,
[3] 8,
],
]
Data::Printer describes itself as:
colored & full-featured pretty print of Perl data structures and objects
The garbage you see is the escape sequences used by the package to colorise the output, which is not understood by the shell in which the script is executed.
To solve your problem at the script/environment level, I'd suggest reading the documentation of the package, which mentions a colored property and a ANSI_COLORS_DISABLED environment variable. It should have been your first move.
To solve it at the editor level, I'd suggest using the built-in :help :terminal, which supports ANSI colours, instead of :!, which doesn't.
And, by the way, the quickfix window is not involved, here.

Jenkins DSL workflow for performing subtraction

I have written a jenkins script where i want build no and previous build no.For that i am performing a subtraction operation.
i want to perform a subtraction between two variables in jenkins DSL script.
But i am not able to subtract.Ex i want to perform a=b-1 ....
the result what i am getting is always value of b.it does not perform subtraction.
It just assigns value of b to a.
Below is want i want :
build_num = "3"
pre_build_num = build_num-"1"
and result what i want should be pre_build_num = "2".
But i am getting pre_build_num = "3".
any help?
The problem is that your build_num variable is a string and the minus operator for strings removes a part of a string. It doesn't subtract numbers.
Some examples:
"three" - "th" = "ree"
"three" - "ree" = "th"`
To subtract your build_num variable you have to convert it to an integer first:
pre_build_num = (build_num as int) - 1

Spirit Qi conditional parsing

I am writing a pdf parsing library.
Once upon a time, I had an input to parse like this one:
1 0 obj
(anything)
endobj
I've created parsing rule for the outer container and then separate rule for the inner object:
CONTAINER_PARSER %=
number >> number >> "obj" >> OBJECT_PARSER >> "endobj";
OBJECT_PARSER %= number | value | ...
This worked without any problems. But, for various reasons a I had to redesign the rules, so that both container values belongs to the object itself.
The container itself is only optional. Meaning, the previous code and the following denotes the same object, without the additional container info:
(anything)
I had 2 ideas, how to solve this problem, but it seems to me, that both are incompatible with Qi approach.
Alternative parser
I wanted to tell the parser, to parse either value contained inside obj - endobj, or to parse only the value.
start %=
(
object_number
>> generation_number
>> qi::lit("obj")
>> object
> qi::lit("endobj")
) | object;
// I intentionally missed some semantic actions assigning the values to the object,
because it is out of the scope of my problem
I didn't manage to make this work, because both parts of the alternation has the same exposed attribute, and the compiler was confused.
Optional approach
I've tried to tell the parser, that the former container is only optional to the parsed value.
start %=
-(
object_number
>> generation_number
>> qi::lit("obj")
)
>> object
> -qi::lit("endobj");
Problem with this approach is, that the last part "endobj" has to be present, if the first part is present as well.
The solution might be trivial, but I was really not able to figure it out from either code, documentation and stackoverflow answers.
UPDATE After the comment:
start =
(
( object_number >> generation_number
| qi::attr(1) > qi::attr(0) // defaults
) >> "obj" >> object > "endobj"
| qi::attr(1) >> qi::attr(0) >> object
)
;
Assuming you're not interested in the (optional) numbers:
start =
-qi::omit [ object_number >> generation_number ]
>> "obj" >> object > "endobj"
;
If you are interested and have suitable defaults:
start =
( object_number >> generation_number
| qi::attr(1) > qi::attr(0) // defaults
)
>> "obj" >> object > "endobj"
;
Of course, you could
alter the recipient type to expect optional<int> for the object_numbers so you could simply -object_number >> -generation_number; This would be kinda sloppy since it also allows "1 obj (anything) endobj"
alter the recipient type to be a variant:
boost::variant<simple_object, object_contaier>
in this case your AST matches the "alternative" approach (first one) from your question

Redis - Lua tables as return values - why is this not working

When I run this code through redis EVAL it return no results. Any idea why this is not working?
redis-cli EVAL "$(cat bug.lua)" 0
bug.lua
local retv = {}
retv["test"] = 1000
return retv
If I initialize the table that value alone gets printed.
$ cat bug.lua
--!/usr/bin/env lua
local retv = {"This", "is", "a", "bug" }
retv["test"] = 1000
return retv
$ redis-cli EVAL "$(cat bug.lua)" 2 a b
1) "This"
2) "is"
3) "a"
4) "bug"
If you refer to the Redis EVAL documentation you can see what are the rules Redis uses to convert a Lua table into a Redis reply:
Lua table (array) -> Redis multi bulk reply (truncated to the first
nil inside the Lua array if any)
Lua table with a single ok field -> Redis status reply
Lua table with a single err field -> Redis error reply
So except with special cases 2 and 3, Redis assumes your table is a sequence (i.e list) which means it reads retv[1], retv[2], ... until it encounters a nil element (here is the corresponding source code section).
This explains why retv["test"] is ignored in your case.
If you change your code with:
local retv = {"This", "is", "a", "bug" }
retv[5] = 1000
return retv
Then this additional element gets returned:
1) "This"
2) "is"
3) "a"
4) "bug"
5) (integer) 1000
Answer from #deltheil is valid.
Remember though that you can use cjson library to pack tables and pass them to consumers.
Your lua file:
local retv = {"This", "is", "a", "bug" }
retv["test"] = 1000
return cjson.encode(retv)
Command:
redis-cli EVAL "$(cat bug.lua)" 0
Result:
"{\"1\":\"This\",\"2\":\"is\",\"3\":\"a\",\"4\":\"bug\",\"test\":1000}"

How to PARSE a sequence of items where items not in blocks are handled like single element blocks?

I've got a situation where I want an equivalence, such that:
[
{Foo}
http://example.com/some/stuff.html
separator
]
...is handled just as if you had written:
[
[{Foo}]
[http://example.com/some/stuff.html]
[separator]
]
Adding a little to the complexity is that if you put the item in a block, then it can have arguments:
[
[{Foo} /some-refinement]
[http://example.com/some/stuff.html {stuff caption} 3]
[separator dashed-line]
]
I'd like a PARSE-based engine that can run the same handler for {Foo}, [{Foo}], and [{Foo} /some-refinement] (let's call it STRING-HANDLER), and have it merely invoked with the right number of parameters.
To write this without PARSE is easy... a single element is wrapped in a temporary block (in the case it's not a block). Then the first item is tested in a CASE statement. But I'd like to convert this to be PARSE-based, where one branch uses INTO while another does not, without repeating code.
It will need to support nesting, so you might wind up processing something like:
[http://example.com/some/stuff.html [{Foo} /some-refinement] 3]
I hope the following can be the basis for your solution.
The following performs exactly the same in both R2 and R3. PARSE's 'into operation is VERY different between the two so I put a simple guard [.here.: block! :.here.] which fixes different bug situations in both platforms.
I used hook functions which allow to cleanly separate the data browsing from the data evaluation. If you look closely, you will notice that the =enter-block?=: rule is global and that the code which switches its meaning is setup BEFORE running the emit-value function... so in some cases, you might actually want to use emit-value to setup a different rule.
note that I'm not assuming any kind of known structure as your explanation seems to be meant for unstructured datasets.
Also note that test B is setup as a string, so we can use the wrapper directly on string input data:
rebol [
author: "Maxim Olivier-Adlhoch"
date: 2014-02-08
license: "public domain"
]
A: [
[{Foo}]
[http://example.com/some/stuff.html]
[separator]
]
B: {[
{Foo}
http://example.com/some/stuff.html
separator
]}
C: [
[{Foo} /some-refinement]
[http://example.com/some/stuff.html {stuff caption} 3]
[separator dashed-line]
]
D: [http://example.com/some/stuff.html [{Foo} /some-refinement] 3]
depth: ""
enter-block: func [][
prin depth
print "["
append depth "^-"
]
quit-block: func [][
remove depth
prin depth
print "]"
]
emit-value: func [value][
prin depth
probe value
]
=enter-block?=: none
=block=: [
(
=enter-block?=: [into =block=] ; we enter blocks by default
enter-block
)
some [
.here.: block! :.here. ; only enter blocks (R3/R2 compatible)
(if 1 = length? .value.: first .here. [ =enter-block?=: [skip] emit-value first .value. ])
=enter-block?=
| set .value. skip ( emit-value .value. )
]
(quit-block)
]
STRING-HANDLER: func [data][
if string? data [
data: load data
]
parse data =block=
]
STRING-HANDLER A
STRING-HANDLER B
STRING-HANDLER C
STRING-HANDLER D
ask "press enter to quit ..."

Resources