In ruamel how to start a string with '*' with no quotes like any other string - pyyaml

yaml = ruamel.yaml.YAML()
yaml.indent(mapping=4)
test_yaml_file = open("test.yaml")
test_file = yaml.load(test_yaml_file)
# test = LiteralScalarString('*clvm')
test = "*testing"
test_file['test_perf'] = test
with open("test.yaml", 'w') as changed_file:
yaml.dump(test_file, changed_file)
In this the expected output was
test_perf: *testing
but the output has been
test_perf: '*testing'
how to achieve this using ruamel?

Your scalar starts with a *, which is used in YAML to indicate an alias node. To prevent *testing to be interpreted as an alias during loading (even though the corresponding anchor (&testing) is not specified in the document), the scalar must be quoted or represented as a literal or folded block scalar.
So there is no way to prevent the quotes from happening apart from choosing to represent the scalar as literal or folded block scalar (where you don't get the quotes, but do get the | resp. >)
You should not worry about these quotes, because after loading you'll again have the string *testing and not something that all of a sudden has extra (unwanted) quotes).
There are other characters that have special meaning in YAML (&, !, etc.) and when indicated at the beginning of a scalar cause the scalar to be quoted. What the dump routine actually does is dump the string and read it back and if that results in a different value, the dumper knows that quoting is needed. This also works with strings like 2022-01-28, which when read back result in a date, such strings get quoted automatically when dumped as well (same for strings that look like floats, integers, true/false values).

Related

What do these characters inside the parentheses of a paremeter expansion (${(…)}) do?

hi am new to zsh and am trying to create multi-line prompt and came across this line of code:
local pad=${(pl.$pad_len.. .)}
My 1st question is what is the pl inside the parentheses? Is it a command or operator or a flag(s)?
And my 2nd question is what are the dots that follow $pad_len?
Those are Zsh parameter expansion flags.
l.$pad_len. makes the given (in this case, empty) string exactly $pad_len long, either by truncating it from the left or by padding it on the left with spaces.
l.$pad_len.. . does the same as the above, but specifies explicitly to use the space character for padding — which is unnecessary, since the default is to pad with spaces.
The .s here are arbitrary separators used to enclose each argument for the preceding flag. It doesn’t matter which (matching pair of) punctuation characters you use for this, as long they enclose each argument in pairs. So, l:$pad_len:: : and l<$pad_len>< > do the exact same thing.
p makes l support print escape codes in the second argument — which is unnecessary, since we don’t use any here.
So, a shorter way to write this would be
local pad=${(l.$pad_len.)}
If you want to do this operation on a non-empty string, you can either pass the name of a variable
local foo=bar
local pad=${(l.$pad_len.)foo}
or pass a literal string with :-
local pad=${(l.$pad_len.):-bar}

SQLite3 regexp performance

How performant is the SQLite3 REGEXP operator?
For simplicity, assume a simple table with a single column pattern and an index
CREATE TABLE `foobar` (`pattern` TEXT);
CREATE UNIQUE INDEX `foobar_index` ON `foobar`(`pattern`);
and a query like
SELECT * FROM `foobar` WHERE `pattern` REGEXP 'foo.*'
I have been trying to compare and understand the output from EXPLAIN and it seems to be similar to using LIKE except it will be using regexp for matching. However, I am not fully sure how to read the output from EXPLAIN and I'm not getting a grasp of how performant it will be.
I understand it will be slow compared to a indexed WHERE `pattern` = 'foo' query but is it slower/similar to LIKE?
sqlite does not optimize WHERE ... REGEXP ... to use indexes. x REGEXP y is simply a function call; it's equivalent to regexp(x,y). Also note that not all installations of sqlite have a regexp function defined so using it (or the REGEXP operator) is not very portable. LIKE/GLOB on the other hand can take advantage of indexes for prefix queries provided that some additional conditions are met:
The right-hand side of the LIKE or GLOB must be either a string literal or a parameter bound to a string literal that does not begin with a wildcard character.
It must not be possible to make the LIKE or GLOB operator true by having a numeric value (instead of a string or blob) on the left-hand side. This means that either:
the left-hand side of the LIKE or GLOB operator is the name of an indexed column with TEXT affinity, or
the right-hand side pattern argument does not begin with a minus sign ("-") or a digit.
This constraint arises from the fact that numbers do not sort in lexicographical order. For example: 9<10 but '9'>'10'.
The built-in functions used to implement LIKE and GLOB must not have been overloaded using the sqlite3_create_function() API.
For the GLOB operator, the column must be indexed using the built-in BINARY collating sequence.
For the LIKE operator, if case_sensitive_like mode is enabled then the column must indexed using BINARY collating sequence, or if case_sensitive_like mode is disabled then the column must indexed using built-in NOCASE collating sequence.
If the ESCAPE option is used, the ESCAPE character must be ASCII, or a single-byte character in UTF-8.

Teradata remove enclosing single quotes from variable

I need to replace single quotes in a string of numbers and use in a WHERE IN clause. for example, I have
WHERE Group_ID IN (''4532','3422','1289'')
The criteria within parenthesis is being passed as a parameter, so I have no control over that. I tried using :
WHERE Group_ID IN (REGEXP_REPLACE(''4532','3422','1289'', '[']', ' ',1,0,i))
also tried using OReplace
WHERE Group_ID IN (OReplace(''4532','3422','1289'', '[']', ' '))
but get the same error:
[Teradata Database] [3707] Syntax error, expected something like ','
between a string or a Unicode character literal and the integer '4532'.
Please suggest how to remove the single enclosing quotes or even removing all single quotes should work as well.
The string ''4532','3422','1289'' you are using is incorrect because it contains non-escaped single quotes. This is a syntax error in SQL. In this particular form, no matter what function you use to fix it or which RDBMS you use, it will result in error with standard SQL.
Functions in the SQL cannot fix syntax errors. REGEXP_REPLACE and OReplace never get executed because the query never enters the execution state. It never goes past the SQL syntax parser.
To see the error from perspective of the SQL parser, you may break the string in to multiple parts
'' -- SQL Parser sees this as a starting and ending quote and hence an empty string
4532 -- Now comes what appears to SQL parser as an integer value
',' -- Now this is a pair of quotes containing a single comma
3422 -- Again an integer
',' -- Again a comma
1289 -- Again integer
'' -- Again emtpy string
This amalgam of strings and numbers will not mean anything to the SQL parser and will result in an error.
Fix
The fix is to properly escape the data. Single quotes must be escaped using another preceding single quote. So correct string in this scenario becomes '''4532'',''3422'',''1289'''
Another thing is that the OReplace usage (once syntax is fixed) is like OReplace(yourStringValueHere, '''', ' ')) Observe the usage of escaped single quote here. Two outer quotes are for the string start and end. First inner quote is the escape character and second inner quote is the actual data passed to the function.

R: Handling strings with a single or odd number of quotation marks

Consider the following string:
"><script>alert(1);</script>
I wish to assign this string (or a string like it) to an R variable.
If I try to do this the traditional way, such as:
x <- as.character("><script>alert(1);</script>)
the command fails due to the presence of the single quotation mark within the string itself. Is there a method to get round this complication without manipulating the string entirely?
It is important to keep the integrity of the string intact. For example, a method to get round this problem would be to change the quotation mark to an apostrophe, or to delete the quotation mark; but this is unacceptable.
Cheers

How exactly does an array get urlencoded?

Many languages allow one to pass an array of values through the url. I need to , for various reasons, directly construct the url by hand. How is an array of values urlencoded?
It looks like the content in the form of MIME-Type: application/x-www-form-urlencoded.
This is the default content type. Forms submitted with this content type must be encoded as follows:
Control names and values are escaped. Space characters are replaced by +, and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by %HH, a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., %0D%0A).
The control names/values are listed in the order they appear in the document. The name is separated from the value by = and name/value pairs are separated from each other by &.
Which is used for the POST. To do it for the GET, you'll have to append a ? after your URL, and the rest is almost equal. In the comments, mdma states, that the URL may not contain a + for a space character. Instead use %20.
So an array of values:
http://localhost/someapp/?0=zero&1=valueone%20withspace&2=etc&3=etc
Often there is some functionality in libraries that will do the URL encoding for you (point 1). Point two is easily implementable by looping over your array, building the string, appending the index, =, the URL encoded value and when it's not the last entry an &.

Resources