Quick PlayFramework / Groovy - templates

Why does the following code always emit 'yes' (in HTML) even when sel has a different value other than 'audio'? (I've logged the values of sel and confirmed different values.)
This is my day 1 on Play. So, I may be doing something really stupid.
${sel='audio'?'yes':'no'}

I don't know the Groovy templates that well, but I think it should be like this:
${sel=='audio'?'yes':'no'}
Your code:
${sel='audio'?'yes':'no'}
means; if 'audio' (a string that is not null will always be "true") then sel = 'yes' otherwise 'no', so it will never be 'no'.

You might want to look at the yesNo extension: http://www.playframework.org/documentation/1.2.1/javaextensions#yesNo

Related

How can I create a checkbox filter on c++

I'm having a problem that seems way more easy than it is. I think its more of an algorithmic problem than a coding one.
EDIT:
Imagine you have a database with a name and N boolean parameters, like if the person is blonde or not, if the person likes baseball or not, if person have a smartphone or not etc...
How could you print the name of someone that likes baseball AND is blonde, but doesn't matter if any of the other N parameters are true of false? How can I do that without having to write a test for every single of the (N^2)-1 possibilities?
I created a dictionary that maps a string to a struct with 4 boolean variables and some strings.
I want the user to select which booleans are important to them and return only the information that is true for all variable that he chose.
Something like a checkbox which you can use to filter a column in excel.
For instance if a user chose variables 1 and 2, I would like to know if there is a better way to return the result rather than testing every one of the 16 possibilities, like:
void filter(map<string, Mystruct> Mydictionary, bool bool1, bool bool2, bool bool3, bool bool4){
if(bool1 == true && bool2 == true && bool3 == false && bool4==false){
cout << Mydictionary.bool1Info << Mydictionary.bool2Info
if(bool1 == true && bool2 == false && bool3 == false && bool4==false)
...
Even more, for me its only important to test the booleans that the user picked up, so even if he didn't choose boolean3, it's not important to test if its true or false.
Any ideas?
I would be very glad if anyone could help me with this one
I can help you with your algorithm part ,
instead of checking all 16 possibilities just check 4 conditions separately for bool1,2,3,4 and print their info if they are true.
This way you can complete your task with only 4 if statements.
Hope this answers your query.

Assign nested function to variable with parameter

disclaimer: My title may not be accurate as far as what I would like to accomplish, but I can update if someone can correct my terminology
I have 2 functions, each with a separate purpose and usable on its own, but occasionally I would like to combine the two to perform both actions at once and return a single result, and to do this I would like to assign to a variable name
I know I can create a 3rd function that does basically what I want as it is really simple.. though it's become a bit of a challenge to myself to find a way of doing this
def str2bool(string):
return string.lower() in ("yes", "true", "t", "1")
def get_setting(string):
if string == 'cat':
return 'yes'
else:
return 'no'
VALID_BOOL = str2bool(get_setting)
print VALID_BOOL('cat')
So basically I would like to assign the combination of the 2 functions to a variable that I can call and pass in the string parameter to evaluate
In my real world code, get_setting() would retrieve a user setting and return the value, I would then like to test that value and return it as a boolean
Again I know I can just create a 3rd function that would get the value and do the quick test.. but this is more for learning to see if it can be done as I'm trying to do.. and so far my different variations of assigning and calling aren't working, is it even possible or would it turn too complex?
Using lambda is easy, but i don't know if it is exactly what you are looking for.
Example:
f = lambda astring : str2bool(get_setting(astring))
Outputs:
>>> f('cat')
True

I use == to compare two strings of ':‘ in python, why I get the result of False

This is the python code:
nr_sign=':'
with open("1.txt") as f:
lines=f.readlines()
print lines[0].split(' ')[0]==nr_sign
I have checked that the lines[0].split(' ')[0] is ':' and the type is string, which is the same with nr_sign. But why the result shows lines[0].split(' ')[0]==nr_sign is false? How can I compare this?
Thanks very much!
Yeah, more info is needed here. If you could post the line being compared, the the lines[0].split... , that might help troubleshoot it. Simply put, you're getting a result of False because the two strings aren't equal. (Sorry, I get a "duh" here, but you did ask why you get False for the result)

Crystal Reports Else If statement

I can't figure out why this if statement won't work.
I have a DateTime field DATEFROM and a String parameter (it HAS to be String) periodEnd.
I would like to calculate percentages depending if these two dates have 1, 2, 3 or more years difference.
When I use this formula I get either "100%" or "-" and never the other two options. It's like CR calculates the first IF and if it's true then: "100%" but if it's false, it never checks for the rest of the Else Ifs and goes dirreclty to Else
StringVar percentage:="";
If (cDate({?periodEnd})-{TABLE.DATEFROM})<=1 Then
percentage:="100%"
Else If (cDate({?periodEnd})-{TABLE.DATEFROM})<=2 Then
percentage:="66%"
Else If (cDate({?periodEnd})-{TABLE.DATEFROM})<=3 Then
percentage:="33%"
Else
percentage:="-"
Any idea?
a) I assume you already made sure your periodend format matches with what cdate interprets?
If you just subtract them, Crystal by default returns the number of days between.
Two ways you can do year:
datediff("yyyy",cDate({?periodEnd},{TABLE.DATEFROM})
or
year(cDate({?periodEnd})-year({TABLE.DATEFROM})
b) You've also no doubt accounted for when your {TABLE.DATEFROM} is greater than cDate({?periodEnd} and you have a negative result?
Not sure if the following is the behavior you would want, but I'm throwing it in for your information
ABS(datediff("yyyy",cDate({?periodEnd},{TABLE.DATEFROM}))
**make sure you check the help file for the datediff codes ("yyyy" etc) as they are not quite instinctive and can trip you up when you think you're using the obvious one

Django Query in a loop fails for no good reason

I have this code:
msgs = int(post['time_in_weeks'])
for i in range(msgs):
tip_msg = Tip.objects.get(week_number=i)
it always results in an error saying that no values could be found.
week_number is an integer field. When I input the value of i directly,
the query works.
When i print out the value of i I get the expected values.
Any input at all would be seriously appreciated.
Thanks.
The range function will give you a zero based list of numbers up to and excluding msgs. I guess there is no Tip with week_number=0.
Also, to limit the number of queries you could do this:
for tip in Tip.objects.filter(week_number__lt=msgs):
#do something
or, if you want specific weeks:
weeks=(1,3,5)
for tip in Tip.objects.filter(week_number__in=weeks):
#do something