Probability Value for Integer Field oTree - probability-distribution

for an experiment I want to program that a certain choice results in a probability distribution of an outcome. Currently I have:
Action = models.IntegerField(
choices=[
[1, 'altruistic (2:0.5,0:0.3,-2:0.2)'],
[2, 'selfish (2:0.3,0:0.4,-2:0.3)']
]
I tried:
Action = models.IntegerField(
choices=[
[random.choices([2,0,-2],[0.5,0.3,0.3]), 'altruistic (2:0.5,0:0.3,-2:0.2)'],
[random.choices([2,0,-2],[0.3,0.4,0.3]), 'selfish (2:0.3,0:0.4,-2:0.3)']
]
)
which gives me an Internal Server Error.
For the experiment I need the random number from the experiment, and the given choice. The given choice I should access with
player.get_field_display('Action')
However in the current state with :
{% extends "global/Page.html" %}{% load otree %}

{% block title %}
Treffen Sie ihre Entscheidung
{% endblock %}

{% block content %}
Bitte treffen Sie ihre Entscheidung. Spieler 1 hat entschieden {{player.get_field_display('Action')}} an Sie zu verteilen. Dies resultierte in {{ group.Action }}
{%formfields%}
{% next_button %}
{% endblock %}
this results in
TemplateSyntaxError: Unparsable argument '‘’Action’’'. Arguments must be valid Python literals. (line 29, in "player.get_field_display(<span")
Has anyone an idea for these flaws?

Related

(Nearly.js) How can I import files which content I want to be the input of my grammars as in LaTex?

What I want:
First, assume I am able to successfully parse the following with my grammar.ne:
\begin{chapter}
...content
\end{chapter}
The desired behavior is to be able to make my grammar.ne file be able to parse a source file containing the above text. For example, in LaTex one can write something like this:
\chapter{filename}
The problem is simply that I don't really know how to do it. But I'll explain what I am trying.
What I am trying
% chapter grammar works well
chapter -> "\\begin{junior}" _ chapterContent _ "\\end{junior}" {% (data) => data[2] %}
% chapterTag below is able to read file and return a string.
% What I don't know how to do is making the grammar parse such a string as chapter
chapterTag -> "\\chapter{" anyCharacters "}"
{% (data) => {
// Assume function readfile exists.
const juniorText = readfile(data[1]);
// How do I tell Nearley to parse this string as a chapter?
return juniorText;
}
%}
If you've used LaTex before, you know what the content of the filename with the chapter would be (\begin{chapter} ... \end{chapter}).

Conditional logic on the same line in Adobe Document Generation is not recognizing template tags

I have a json field that is either absent or is set to true. But in my document I want it to show up as "Yes" or "No". I tried the following conditional expression ():
{% conditional-section expr(`my_field` = true) %}Yes{% end-section %}{% conditional-section expr(`my_field`!= true) %}No{% end-section %}
But when I call the Adobe Document Generation API, the PDF I get still has these template tags. For some reason it is not being detected. How can I achieve this?
I know I asked a question above in the comment, but I think I can take a stab at answering this now. So right now there is not a way to say "if a value is NOT present or present and equal to yes." There's a few ways you can handle this, but I think the easiest would be to change your data. To be clear, I don't mean change your database or anything, but keep in mind that before you call our API, you can massage the data a bit. So if the absence of my_field means yes, I'd do something like this (JavaScript):
if(!mydata.my_field) {
mydata.my_field = 'yes';
}
You could then do something like this:
{% conditional-section expr(my_field = "yes") %}
Yes
{% end-section %}
{% conditional-section expr(my_field = "no") %}
No
{% end-section %}
However, this will not be on the same line. As I said, this is a known issue. If you need it as such, again, I'd use the idea of massaging your data first. You could do something like this (again, JavaScript, but could be done in any language):
if(!mydata.my_field) {
mydata.my_field = 'yes';
}
if(mydata.my_field === 'yes') mydata.my_field_value = 'Yes';
else mydata.my_field_value = 'No';
All I did there was, based on the value of my_field, set another variable. In your Word template you can then simplify even more by just using {{my_field_value}}
With Document Generation being so flexible, you've got multiple different ways of solving a problem.
I have found this works for me:
{{my_field ? "Yes" : "No"}}

Volt iteration comparison with promise

If I'm iterating through an ArrayModel using .each_with_index, is there a way to make rendering decisions based on the current index compared with the results of a Promise (at the moment, this comparison returns an error about comparing a Numeric with a Promise)?
In practice, I have a list that renders ten items by default, and a user can ask to render 20, 30, etc. If I have the user selection change the query, then the entire list re-renders, which is slow. If I have their selection change {{ if index < selected_limit }} from false to true, then only the newly-showing items re-render. But this only works if comparison of index against a Promise (selected_limit) can be done. Is there a way to do that?
Yes, if bindings can take promises, so what you can do is return a new promise that resolves to true or false and the if binding will update once that promises resolves.
{{ store.todo.each_with_index do |todo, index| }}
{{ if todo.selected_limit.then {|limit| index < limit } }}
....
{{ end }}
{{ end }}
Let me know if that makes since. Calling .then on the promise will pass in the value as an argument once it resolves, but the result of .then {...} will be a new promise.
You should be able to use .value for the comparison, that's how I did it in a project myself.
{{ store.players.each do |player| }}
{{ unless player == current_player.value }}
<table class='player'>
<tr><td>{{ player.name }}</td></tr>
<tr><td><button e-click='add_vote(player.id)'>+1</button></td></tr>
</table>
{{end}}
{{end}}

Write multiple statements in swig expression tag?

i am tring to write multiple line in my swig expression tag but conditions are not evaluting,
but if i write all the statements in different tag then it is working.
Please find following example :
{% set promo_class = "whatone" %}
{% if (loop.index % 2) == 0 %}
{% set promo_class= promo_class +" mhxxl"; %}
{% endif %}
Following is not working for me, while above one is
{%
set promo_class = "whatone";
if ( loop.index % 2 ) == 0
set promo_class= promo_class +" mhxxl";
endif;
%}
Block statements are not part of the Swig Template language. Nowhere in the documentation does it show that as an example or an available syntax style.

Compile dust template with line breaks

Compiling a such template with dustc:
$ cat <<EOF | ./node_modules/.bin/dustc -
<p>Hi there!</p>
<p>I'm a {! dust !} template.</p>
EOF
outputs:
(function(){dust.register("-",body_0);function body_0(chk,ctx){return chk.write("<p>Hi there!</p><p>I'm a template.</p>");}return body_0;})();
but without \n between lines, eg: "<p>Hi there!</p>\n<p>I'm a template.</p>"
Is there any way to change this?
Thank you
You can use {~n} to create line breaks in your Dust templates. It's is especially useful within <pre> tags.
You can disable whitespace compression with
dust.optimizers.format = function(ctx, node) { return node };
Precompiling with gulp-dust, there's a preserveWhitespace option that does just that:
var compile=require('gulp-dust');
// ...
gulp.src('templates/**/*.dust')
.pipe(compile({ preserveWhitespace: true }))
// ...

Resources