Why is it necessary to trim before comparing string data? [duplicate] - el

I have a command button like below.
<h:commandButton value="Accept orders" action="#{acceptOrdersBean.acceptOrder}"
styleClass="button" rendered="#{product.orderStatus=='N' }"></h:commandButton>
even when the product.orderStatus value is equal to 'N' the command button is not displayed in my page.
Here product.orderStatus is a character property.

This is, unfortunately, expected behavior. In EL, anything in quotes like 'N' is always treated as a String and a char property value is always treated as a number. The char is in EL represented by its Unicode codepoint, which is 78 for N.
There are two workarounds:
Use String#charAt(), passing 0, to get the char out of a String in EL. Note that this works only if your environment supports EL 2.2. Otherwise you need to install JBoss EL.
<h:commandButton ... rendered="#{product.orderStatus eq 'N'.charAt(0)}">
Use the char's numeric representation in Unicode, which is 78 for N. You can figure out the right Unicode codepoint by System.out.println((int) 'N').
<h:commandButton ... rendered="#{product.orderStatus eq 78}">
The real solution, however, is to use an enum:
public enum OrderStatus {
N, X, Y, Z;
}
with
private OrderStatus orderStatus; // +getter
then you can use exactly the desired syntax in EL:
<h:commandButton ... rendered="#{product.orderStatus eq 'N'}">
Additional bonus is that enums enforce type safety. You won't be able to assign an aribtrary character like ☆ or 웃 as order status value.

Related

Can't get key of object that is numeric

I'm working with an API that returns an array of objects. I can get all the keys, but two of those have numbers as keys, and I cannot get it. Give me an error.
I really dont know why I can not get it those keys.
Is there something different due to are numbers?
BTW Im using axios.
If you're using dot notation, you should change to bracket notation to access properties start by a number.
The code below uses dot notation, it throws an error
const test = {"1h" : "test value"};
console.log(test.1h); // error
Why :
In the object.property syntax, the property must be a valid JavaScript
identifier.
An identifier is a sequence of characters in the code that identifies a variable, function, or property.
In JavaScript, identifiers are case-sensitive and can contain Unicode letters, $, _, and digits (0-9), but may not start with a digit.
The code below uses bracket notation, works fine
const test = {"1h" : "test value"};
console.log(test["1h"]); // works
Why :
In the object[property_name] syntax, the property_name is just a
string or Symbol. So, it can be any string, including '1foo', '!bar!',
or even ' ' (a space).
Check out the document here

Call for global variable in JS block of Selenium Webdriver test (Python)

I have a string of numbers set by user. Defined in the beginning of the Webdriver test:
numbers = input("prompt")
Then I need to enter value of this variable by JS code like this:
driver.execute_script("document.getElementsByName('phone')[0].value=***")
Where instead of *** I need the value of "numbers" variable. How should I properly insert it to make it work?
Here is what you want to do.
numbers = input("prompt")
driver.execute_script("document.getElementsByName('phone')[0].value={}".format(numbers))
The documentation link:
https://docs.python.org/3/library/string.html
And a snip-it from the docs:
The field_name itself begins with an arg_name that is either a number or a keyword. If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, … in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, … will be automatically inserted in that order. Because arg_name is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings '10' or ':-]') within a format string. The arg_name can be followed by any number of index or attribute expressions. An expression of the form '.name' selects the named attribute using getattr(), while an expression of the form '[index]' does an index lookup using getitem().
Changed in version 3.1: The positional argument specifiers can be omitted for str.format(), so '{} {}'.format(a, b) is equivalent to '{0} {1}'.format(a, b).
OR
numbers = input("prompt")
driver.execute_script("document.getElementsByName('phone')[0].value=%s" % numbers)
See examples of both here:
https://pyformat.info/
If your python variable's value is simple string without single quotes or special characters, you can simply use:
driver.execute_script("document.getElementsByName('phone')[0].value='" +
python_variable + "'");
If it has quote marks in it, or special characters that need escaping, or if it's not a string at all, you need to obtain JavaScript string representation of your Python variable's value. json.dumps will handle all the necessary formatting and escaping for you, appropriate to the type of your variable:
from json import dumps
driver.execute_script("document.getElementsByName('phone')[0].value=" +
dumps(python_variable))

What is the difference between ', ` and |, and when should they be used?

I've seen strings written like in these three ways:
lv_str = 'test'
lv_str2 = `test`
lv_str3 = |test|
The only thing I've notice so far is that ' trims whitespaces sometimes, while ` preserves them.
I just recently found | - don't know much about it yet.
Can someone explain, or post a good link here when which of these ways is used best and if there are even more ways?
|...| denotes ABAP string templates.
With string templates we can create a character string using texts, embedded expressions and control characters.
ABAP Docu
Examples
Use ' to define character-typed literals and non-integer numbers:
CONSTANTS some_chars TYPE char30 VALUE 'ABC'.
CONSTANTS some_number TYPE fltp VALUE '0.78'.
Use ` to define string-typed literals:
CONSTANTS some_constant TYPE string VALUE `ABC`.
Use | to assemble text:
DATA(message) = |Received HTTP code { status_code } with message { text }|.
This is an exhaustive list of the ways ABAP lets you define character sequences.
To answer the "when should they be used" part of the question:
` and | are useful if trailing spaces are needed (they are ignored with ', cf this blog post for more information, be careful SCN renders today the quotes badly so the post is confusing) :
DATA(arrival) = `Hello ` && `world`.
DATA(departure) = |Good | && |bye|.
Use string templates (|) rather than the combination of ` and && for an easier reading (it remains very subjective, I tend to prefer |; with my keyboard, | is easier to obtain too) :
DATA(arrival) = `Dear ` && mother_name && `, thank you!`.
DATA(departure) = |Bye { mother_name }, thank you!|.
Sometimes you don't have the choice: if a String data object is expected at a given position then you must use ` or |. There are many other cases.
In all other cases, I prefer to use ' (probably because I obtain it even more easily with my keyboard than |).
Although the other answers are helpful they do not mention the most important difference between 'and `.
A character chain defined with a single quote will be defined as type C with exactly the length of the chain even including white spaces at the beginning and the end of the character sequence.
So this one 'TEST' will get exactly the type C LENGTH 4.
wherever such a construct `TEST` will evaluate always to type string.
This is very important for example in such a case.
REPORT zutest3.
DATA i TYPE i VALUE 2.
DATA(l_test1) = COND #( WHEN i = 1 THEN 'ACT3' ELSE 'ACTA4').
DATA(l_test2) = COND #( WHEN i = 1 THEN `ACT3` ELSE `ACTA4`).
WRITE l_test1.
WRITE l_test2.

Constants (constant variables) in ALFA

The OASIS Working Draft 01 for ALFA (alfa-for-xacml-v1.0-wd01) of 10 March 2015 says about constant values
3.15 Constant Values
Constant values can appear in the policy expressions. ALFA supports constants of type strings, integers,
doubles and Booleans directly. Strings are quoted with single or
double quotes. Integers consist of a number and optionally a minus
sign. Double consists of a number with a decimal dot and optionally a
minus sign. Booleans consist of the value true and false, without
quotes. Other datatypes are represented using a string followed by a
colon and the name of the datatype..
What that means is, you can use constant values like in that example (while report is the constant value):
target clause requestedType == "report"
But the thing is, once the ALFA files grow and you have written the constant value report all over, you might want to change the constant value into let's say my.company.attributes.medicalReport. In order to do that you have to find and replace all occurrences of the constant value.
Therefore (for the sake of avoiding redundancy) constants have been invented in other languages, where you define smth. like
const string REPORT_TYPE = "my.company.attributes.medicalReport"
or even more performant:
const integer REPORT_TYPE_ID = 3
or even more elegant:
const enum SUBJECT_TYPES { PATIENT, USER, EXAM, REPORT }
With those constants being defined I could write my target like:
target clause requestedType == REPORT_TYPE_ID
Does ALFA support constants or is there a way to "emulate" them (maybe a function that returns the desired value)?
Not yet! It is definitely a feature we want to have. We've had similar requests so stay tuned.

Objective C: Parsing JSON string

I have a string data which I need to parse into a dictionary object. Here is my code:
NSString *barcode = [NSString stringWithString:#"{\"OTP\": 24923313, \"Person\": 100000000000112, \"Coupons\": [ 54900012445, 499030000003, 00000005662 ] }"];
NSLog(#"%#",[barcode objectFromJSONString]);
In this log, I get NULL result. But if I pass only one value in Coupons, I get the results. How to get all three values ?
00000005662 might not be a proper integer number as it's prefixed by zeroes (which means it's octal, IIRC). Try removing them.
Cyrille is right, here is the autoritative answer:
The application/json Media Type for JavaScript Object Notation (JSON): 2.4 Numbers
The representation of numbers is similar to that used in most programming languages. A number contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part.
Octal and hex forms are not allowed. Leading zeros are not allowed.