Python3 | SQL request failed - sqlite

So. Simple request SELECT * FROM access WHERE {arg}=TRUE, where arg = "friends_grinder".
File "/root/python/NaVK/source/main/FriendsGrinderEngine.py", line 51, in __routine
users = ADB.get_authorized_users('friends_grinder')
File "/root/python/NaVK/source/database/AccessDB.py", line 30, in get_authorized_users
users = self.__conn.cursor().execute(f'SELECT * FROM access WHERE {arg}=TRUE').fetchall()
sqlite3.OperationalError: no such column: TRUE
I don't get this error on windows. But I get it on Ubuntu.
friends_grinder column is BOOLEAN.

From the documentation:
Beginning with SQLite 3.23.0 (2018-04-02), SQLite recognizes the identifiers "TRUE" and "FALSE" as boolean literals, if and only if those identifiers are not already used for some other meaning. If there already exists columns or tables or other objects named TRUE or FALSE, then for the sake of backwards compatibility, the TRUE and FALSE identifiers refer to those other objects, not to the boolean values.
The boolean identifiers TRUE and FALSE are usually just aliases for the integer values 1 and 0, respectively. However, if TRUE or FALSE occur on the right-hand side of an IS operator, then they form new unary postfix operators "IS TRUE" and "IS FALSE" which test the boolean value of the operand on the left.
If you're getting no such column: TRUE as an error, that means the version of sqlite being used on that system is too old to recognize the identifier. Upgrade, or use 1 instead.

Related

Robot FW : Collections library : "Copy Dictionary" : How to make a shallow copy of a compound dictionary?

Consider the following code:
In Utils.py:
#keyword
def get_compound_dictionary():
"""
https://docs.python.org/3/library/copy.html
An example compound dictionary
"""
return {'key1': 'value1', 'deep_dict': {'key2': 'value2'}}
In collection-library-tests.robot
*** Settings ***
Documentation A test suite utilizing all collection library keywords
Library Collections
Library Utils.py
# To run:
# robot --pythonpath Resources --noncritical failure-expected -d Results/ Tests/collection-
library-tests.robot
*** Test Cases ***
Use "Copy Dictionary" : Shallow Copy
${compound_python_dictionary} = get compound dictionary
&{shallow_copy} = Copy Dictionary ${compound_python_dictionary} deepcopy=False
# if we modify the contained objects (i.e. deep_dict) through the shallow_copy,
# the original compound_python_dictionary will see the changes in the contained objects
Set To Dictionary ${shallow_copy}[deep_dict] key2=modified
Log ${shallow_copy}
Log ${compound_python_dictionary}
Should Be Equal ${compound_python_dictionary}[deep_dict][key2] modified # fails, why?
The goal is stated in the test case as:
if we modify the contained objects (i.e. deep_dict) through the shallow_copy,
the original compound_python_dictionary will see the changes in the contained objects
Expected Result
Should Be Equal ${compound_python_dictionary}[deep_dict][key2] modified # passes
Observed Result
Note that I am using Robot FW version: Robot Framework 3.1.2 (Python
3.7.4 on linux)
Acc.to the documentation about Copy Dictionary:
The deepcopy argument controls should the returned dictionary be a
shallow or deep copy. By default returns a shallow copy, but that can be
changed by giving deepcopy a true value (see Boolean arguments). This > is a new option in Robot Framework 3.1.2. Earlier versions always
returned shallow copies.
Acc.to the documentation about Boolean Arguments:
Some keywords accept arguments that are handled as Boolean values true or false. If such an argument is given as a string, it is considered false if it is an empty string or equal to FALSE, NONE, NO, OFF or 0, case-insensitively. Other strings are considered true regardless their value.
Note also that i tried also deepcopy=${False}, which yielded the same observed result.
The problem is not with the RF keyword (it very seldom is, they have extensive UT), but with the way you call it, namely this argument:
deepcopy=False
You may be thinking you are passing a boolean value, but in fact you are passing the string "False".
Inside the keyword's implementation there is this branching:
if deepcopy:
return copy.deepcopy(dictionary)
, and as a non-empty string evaluates to True, you are in fact getting a deep copy.
This is the way to pass a real False:
deepcopy=${False}

R: hunspell always returns false

I have just started diving into NLP and want to use hunspell in order to perform tokenization. However, until now I was not able to use hunspell properly, since it returns "false" everytime I use the function "hunspell_check".
I installed hunspell serveral times and checked, whether dictionaries are actually present (they are). Also, I tried different functions of hunspell (like "hunspell()"), but they do not work either. Interestingly, I cannot find an error message of any kind.
> hunspell_check("work")
[1] FALSE
> dictionary(lang = "en_US")
<hunspell dictionary>
affix: C:\Users\NilsKlähn\Documents\R\win-library\3.6\hunspell\dict\en_US.aff
dictionary: C:\Users\NilsKlähn\Documents\R\win-library\3.6\hunspell\dict\en_US.dic
encoding: ISO8859-1
wordchars: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ
added: 0 custom words
I expect the function hunspell_check("work") to return true, instead of false, since it is spelled correctly. The dictionary seems to be alright though.

UNIX - Until loop condition expects integer from a string?

So, I have some code which is supposed to check whether a pre-check sequence has completed before I begin patching a server. I'm doing this by using an until loop and as the condition, I have a flag set to true. When the flag is set to false that means that the pre-checks are done and I can now proceed out of the until loop.
The problem is I get stuck in an infinite loop and I get an error message saying [: true: integer expression expected
I continually call a URL to conduct my checks, and it returns a file which I save. An example extract from that file would be:
inProgress":true,"status":"IN_PROGRESS","preCheckMessages":[],"precheckResultItems":[]}
The part I need to extract from this is the value from the inProgress field (which could be 'true' or 'false'). I then use this value to compare to my flag. So I extract the value from the inProgress field.
The section of code causing me an issue is as follows:
inProgress="true"
until [ "$inProgress" -eq "false" ]
do
long_URL_goes_here > jobIdCheck.txt
inProgress=$(grep -o 'inProgress.*$' jobIdCheck.txt)
word="inProgress\":"
tempVar=${inProgress##${word}}
inProgress=${tempVar%%,*}
echo inProgress value = ${inProgress}
done
echo "Pre-checks complete. Ready to apply patch"
This is where I hit the infinite loop with the error message saying [: true: integer expression expected. (I never see the "Pre-checks complete message")
But I don't understand why it would be expecting an integer? Surely the string comparison should suffice?
Is the fact that the value true in the inProgress field ISN'T encapsulated in double quotes important?
Any help will be greatly received.

Why does file.exists return FALSE despite being in list.files?

I have a file x which appears in a directory, as proved by list.files
x <- "./data-raw/paths/calculate-route-by-FROM-TO/2017-05-2075--FROM-Flinders-Street-Railway-Station--Melbourne-Victoria-3004--VIC--TO-Melbourne-Sports-Aquatic-Centre--30-Aughtie-Dr--Melbourne-VIC-3206--VIC-csv"
x %in% list.files("./data-raw/paths", recursive = TRUE, full.names = TRUE)
# [1] TRUE
file.exists(x)
# [1] FALSE
Note that x lacks an extension, but I could not find such a caveat in ?file.exists:
file.exists returns a logical vector indicating whether the files named by its argument exist. (Here ‘exists’ is in the sense of the system's stat call: a file will be reported as existing only if you have the permissions needed by stat. Existence can also be checked by file.access, which might use different permissions and so obtain a different result. Note that the existence of a file does not imply that it is readable: for that use file.access.) What constitutes a ‘file’ is system-dependent, but should include directories. (However, directory names must not include a trailing backslash or slash on Windows.) Note that if the file is a symbolic link on a Unix-alike, the result indicates if the link points to an actual file, not just if the link exists. Lastly, note the different function exists which checks for existence of R objects.
The only hint from the documentation was that access restrictions might cause FALSE, and indeed file.access(x) is -1 but I appear to have access to that file (as well as other very similar files which are not so affected).
Windows has an upper limit on the length of a filename (260 characters), which x exceeded. Shortening the file resulted in file.exists returning TRUE.

Parsing booleans in Robot Framework

The Robot Framework User Guide, section 6.6 Boolean arguments, says:
Many keywords in Robot Framework standard libraries accept arguments
that are handled as Boolean values true or false. If such an argument
is given as a string, it is considered false if it is either empty or
case-insensitively equal to false or no. Other strings are considered
true regardless their value, and other argument types are tested using
same rules as in Python.
How do I replicate this behavior in my own user keywords?
The build-in keyword Convert To Boolean is stricter:
Converts the given item to Boolean true or false.
Handles strings True and False (case-insensitive) as expected,
otherwise returns item's truth value using Python's bool() method.
There are two functions in robot.utils for dealing with boolean arguments - is_truthy and is_falsy. DateTime uses is_falsy. To behave like that library, you could simple call the same function used by those libraries. Below is an implementation of is_falsy in Robot syntax and en example keyword using it to convert arguments. You could also convert the arguments as needed using the same evaluate statement and avoid inter-dependencies.
*** Test Cases ***
Boolean
[Template] Some Keyword
truE
${42}
FAlsE
no
${0}
*** Keywords ***
Some Keyword
[Arguments] ${option}
${option as bool} Is Truthy ${option}
Log To Console ${option} -> ${option as bool}
Is Truthy
[Arguments] ${arg}
${arg as bool} Evaluate robot.utils.is_truthy($arg) modules=robot
[Return] ${arg as bool}

Resources