Pyramid Chameleon Tal:condition 'Not' issue - pyramid

I am trying to display conditional text in a Pyramid Chameleon template. Basically, checking if the dictionary key 'maxed_out_alerts' is empty (false) or has a string 'yes' in it.
<p tal:condition="not:maxed_out_alerts"><h3>Maxed Out.</h3></p>
<p tal:condition="maxed_out_alerts"><h3>Not Maxed Out</h3></p>
When 'maxed_out_alerts' is an empty string, 'Maxed Out' is only displayed (correctly). However, If 'maxed_out_alerts' contains 'yes' string both 'Maxed Out' and "Not Maxed Out' are displayed (incorrectly).
It seems that the NOT is always evaluated to a true condition. It should display one or the other messages not both. What am I doing wrong? thanks

For TAL conditionals in python you can say python: and then use a python syntax conditional
<p tal:condition="python:len(maxed_out_alerts) > 0"><h3>Maxed Out.</h3></p>

It could help if you save boolean state in a boolean variable. By storing this information in a string you run into such problems you are facing right now. That's what builtin python types are made for - use them.
As a pyramid developer I would advice to move the logic to evaluate the current value of maxed_out_alerts into a string into a view method and pass the computed string in a dictionary to the renderer/template. This way you can even create tests for the view logic - any pyramid tutorial, simple or advanced shows you how to do that.
A good start for any simple logic - imagine logic gets more complicated or you even have to translate the text for the template.
#view_config(name="yourname", renderer='templates/yourtemplate.pt')
def myview(request):
"""
#get boolean state from model
#could be that you want to have it the other way round
#or do it by using python ternary operator - a if test else b
if model['maxed_out_alerts'] == True:
maxed_out_alerts = 'Maxed Out'
else:
maxed_out_alerts = 'Not Maxed Out'
return dict(maxed_out_alerts = maxed_out_alerts)
In your Template
<h3 tal:content="maxed_out_alerts">text for maxed out alerts</h3>
or
<h3>${maxed_out_alerts}</h3>

Related

Access specific index of array with Pug

How can you display a specific item in an array with Pug? For example:
each answer in answers
li!= answer.Response
Will display each item in the array. But, say I wanted just the the third item or, better yet, pass a variable for a specific index to display. What is the syntax for this?
- const indexIwant = 2;
if answers && answers.length>indexIwant
li=answers[indexIwant]
You need to ensure answers is not null and has at least the number of items to include the indexed item you want.
Another thing: don't use != unless you know exactly what data you are handling.
The simplest way to access a specific index of an array in pug:
-const meals = ["breakfast", "lunch", "dinner"]
-const favoriteDishes = ["coffee & doughnut salad","cheese danish soup","red wine","banana split sandwich"]
-const sides = ["ranch dressing","chutney","ketchup","chocolate sauce"]
p I reckon I will fix myself a hefty helping of #{favoriteDishes[2]} for #{meals[0]} with a side of #{sides[2]}.
Considering that indentation and whitespace is everything in jade / pug, this works :))

What is the difference between 'page_source' and 'find_element_by_tag_name("body").text'?

Trying to find whether a text is present on UI login page (web-page).
I could verify it by 'driver.page_source()' and driver.find_element_by_tag_name("body").text
driver.page_source()
text = "abcd"
page_source = driver.execute_script("return document.body.innerHTML;")
if text in page_source:
return True
else:
return False
driver.find_element_by_tag_name("body").text
text = "abcd"
value = text in self.browser.find_element_by_tag_name("body").text
if value:
return True
else:
return False
What's the difference between method1 and method2 ?
Which one is preferred to do the required task ?
Which is faster ?
Or anySelenium-UI methods to be used ?
Any help would be appreciated. Looking for valuable inputs.
Any idea on this ? Any help here ?
Page source will give all the text including HTML tags, styles etc. as you have written yourself in execute script to return the innerHTML. So, all HTML code will be returned which obviously will contain the text too. You can also get the whole html with selenium too instead of using JavaScript executor by browser.page_source.
On the other hand, browser.find_element_by_tag_name("body").text will return all the text you see on the page without html tags.
To me, the 2nd method should be preferred and faster because you will have string of smaller length(without un-necessary html tags) and the actual text you are interested in.

How to structure optional parts of a function?

Apologies for the horrible title, but I do not know the proper wording for it, which is part of the problem.
Currently I am making some functions in python with a general structure like this:
def printfunction(parameter1, parameter2, option1=False,option2=False):
print(f"Stuff that always happens+{parameter1}")
if option1 == False and option2 == False:
print(f"Basic Functionality+{parameter2}")
if option1 == True:
print("Option1")
if option2 == True:
print("Option2"
if option1 == True and option2 == True:
print(f"Option 1 and 2+{parameter2}")
So I would like a function that executes it's basic functionality, but if one or multiple of a set of Boolean keywords are set to True, I want it to execute some other functionality (or the original plus some new) and with only 2 "option" keywords the way I did it above works perfectly fine, but if one wants a lot of options like this, the function gets filled with "if option==False/True" statements, making very messy code.
So my question is: Is there a more efficient way of having a function that can execute multiple options? While sharing the initial parameters and some parts of the function.
Or am I just doing this all wrong and should I use a class with different methods?
Thanks in advance for any reply

Processing Split (server)

I am doing 2player game and when I get informations from server, it's in format "topic;arg1;arg2" so if I am sending positions it's "PlayerPos;x;y".
I then use split method with character ";".
But then... I even tried to write it on screen "PlayerPos" was written right, but it cannot be gained through if.
This is how I send info on server:
server.write("PlayerPos;"+player1.x+";"+player1.y);
And how I accept it on client:
String Get=client.readString();
String [] Getted = split(Get, ';');
fill(0);
text(Get,20,20);
text(Getted[0],20,40);
if(Getted[0]=="PlayerPos"){
text("HERE",20,100);
player1.x=parseInt(Getted[1]);
player1.x=parseInt(Getted[2]);
}
It writes me "PlayerPos;200;200" on screen, even "PlayerPos" under it. But it never writes "HERE" and it never makes it into the if.
Where is my mistake?
Don't use == when comparing String values. Use the equals() function instead:
if(Getted[0].equals("PlayerPos")){
From the Processing reference:
To compare the contents of two Strings, use the equals() method, as in if (a.equals(b)), instead of if (a == b). A String is an Object, so comparing them with the == operator only compares whether both Strings are stored in the same memory location. Using the equals() method will ensure that the actual contents are compared. (The troubleshooting reference has a longer explanation.)

In Watir, how to get the full text, from a portion of text?

I have a portion of HTML that looks similar to:
<table><tbody><tr>
<td><div> Text Goes Here </div></td>
<td> ... rest of table
There are no IDs, no Titles, no descriptors of any kind to easily identify the div that contains the text.
When an error occurs on the page, the error is inserted into the location where "Text Goes Here" is at (no text is present unless an error occurs). Each error contains the word "valid".
Examples: "The form must contain a valid name" or "Invalid date range selected"
I currently have the Watir code looking like this:
if browser.frame(:index => 0).text.includes? "valid"
msg = # need to get full text of message
return msg
else
return true
end
Is there any way to get the full text in a situation like this?
Basically: return the full text of the element that contains the text "valid" ?
Using: Watir 2.0.4 , Webdriver 0.4.1
Given the structure you provided, since divs are so often used I would be inclined to look for the table cell using a regular expression as Dave shows in his answer. Unless you have a lot of nested tables, it is more likely to return just the text you want.
Also if 'valid' may appear elsewhere then you might want to provide a slightly larger sample of the text to look for
. browser(:cell => /valid/).text
Try this
return browser.div(:text => /valid/).text
or
return browser.table.div(:text => /valid/).text
if the valid is not found, it should return nil.

Resources