Okay, so I just recently got into lua and find myself stuck with the following:
I have the function peripheral.getNames() (which is a custom function)
it will return a table with the structure key,value, whereas key is always a number and starts from 1 and value will be what the function finds (it searches for devices connected to it)
In my example it creates a table which looks like this
1 herp
2 derp
3 monitor_1
4 morederp
I can print the values with the following
local pgn = peripherals.getNames()
for key,value in pairs(pgn) do
setCursorPos(1,key)
write(value)
end
end
this will output the corresponding value of the table at key on my display like this
herp
derp
monitor_1
morederp
now, I try to filter my results so it only prints something if value contains 'monitor'
I tried to achive this with
for key,value in pairs(pgn) do
if string.match(value, monitor) then
#dostuff
end
end
but it always returns 'bad argument: string expected, got nil'
so obviously string.match either does not accept 'value' or, value is not a string
so i tried converting value first
for key,value in pairs(pgn) do
value = tostring(value)
if ....
#dostuff
end
end
but it still throws the same error
Do any of you have an idea how i might either get string.match to accept 'value' or if there is another method to check the contents of 'value' for a pattern while in this for loop?
The error message is talking about the variable monitor, which is not defined and so has a nil value.
Try string.match(value, "monitor").
Related
I am trying to write a xquery to get the Value for a specific name .Below is the request payload: i.e. if the Name ="ID" then get the
"Value" for that tag (i.e.1000000000.)If the Name="User" get the "Value" for that tag( ie."US").
<not:Items xmlns:v5="http://www.example.com"
xmlns:com="http://commom.com
xmlns:not="http://services.not.com"
xmlns:com1="http://common1.com">
<not:Array>
<com1:Item>
<v5:List>
<com:extensionsItem>
<com:Name>ID</com:Name>
<com:Value>1000000000</com:Value>
</com:extensionsItem>
<com:extensionsItem>
<com:Name>User</com:Name>
<com:Value>US</com:Value>
</com:extensionsItem>
</v5:List>
</com1:Item>
</not:Array>
</not:Items>
I tried the options below:
<ns2:ID>{fn:data($Items/not:Array/com1:Item/v5:List/com:extensionsItem[1]/com:Value)}<ID>
This statement works . But I cannot assure that ID will always come in first element in the array of List.So i want a statement that will work even if ID comes in any other
place in the array and I can retrieve the value.
Thanks in advance
I think you simply want to apply a predicate $Items/not:Array/com1:Item/v5:List/com:extensionsItem[com:Name = 'ID']/com:Value
I'm trying to return a value in Google sheets.
This is done using an Index Match as follows, which does work:
=iferror(index(Data!B:B, match(B5339,Data!G:G,0)),"Not Found")
I'd now like to expand this, so that if this first test fails, try looking up that same data in another sheet....
=iferror(if(index(Data!B:B, match(B5340,Data!G:G,0),if(index(HeadOfficeLeads!B:B, match(B5340,HeadOfficeLeads!A:A,0))))),"Not found")
This outputs the fail msg of "Not Found".
However, although the first test is indeed false, the second test is true (this second data set does in fact hold a match).
NB - the data containing this correct match on the 2nd sheet is created by a UNIQUE ( FILTER, FWIW....
For some reason, it doesnt look like the second IF statement is being run - and the whole thing doesnt work, giving the error "Wrong number of arguments".
I have a feeling the argument issue is that the first test doesnt have an "if false" clause - but believe the "IFERROR" parent should handle this?
If not, where would I put the "if false clause" for the IF's?
You don't need any if, because iferror already contains an if statement in its logic (as its name suggests). Here is an example of nested iferror statements, simplified for clarity:
=iferror(match("a", A1:A5, 0), iferror(match("a", B1:B5", 0), "not found"))
This will return the position of "a" in column A, if it's there; otherwise, it will return its position in column B if it's there, otherwise it returns "not found".
Works the same with index or anything else around match function.
I am trying to just do a basic INSERT operation to a PostgreSQL database through Python via the Psycopg2 module. I have read a great many of the questions already posted regarding this subject as well as the documentation but I seem to have done something uniquely wrong and none of the fixes seem to work for my code.
#API CALL + JSON decoding here
x = 0
for item in ulist:
idValue = list['members'][x]['name']
activeUsers.append(str(idValue))
x += 1
dbShell.executemany("""INSERT INTO slickusers (username) VALUES (%s)""", activeUsers
)
The loop creates a list of strings that looks like this when printed:
['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo']
I am just trying to have the code INSERT these strings as 1 row each into the table.
The error specified when running is:
TypeError: not all arguments converted during string formatting
I tried changing the INSERT to:
dbShell.executemany("INSERT INTO slackusers (username) VALUES (%s)", (activeUsers,) )
But that seems like it's merely treating the entire list as a single string as it yields:
psycopg2.DataError: value too long for type character varying(30)
What am I missing?
First in the code you pasted:
x = 0
for item in ulist:
idValue = list['members'][x]['name']
activeUsers.append(str(idValue))
x += 1
Is not the right way to accomplish what you are trying to do.
first list is a reserved word in python and you shouldn't use it as a variable name. I am assuming you meant ulist.
if you really need access to the index of an item in python you can use enumerate:
for x, item in enumerate(ulist):
but, the best way to do what you are trying to do is something like
for item in ulist: # or list['members'] Your example is kinda broken here
activeUsers.append(str(item['name']))
Your first try was:
['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo']
Your second attempt was:
(['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo'], )
What I think you want is:
[['b2ong'], ['dune'], ['drble'], ['drars'], ['feman'], ['got'], ['urbo']]
You could get this many ways:
dbShell.executemany("INSERT INTO slackusers (username) VALUES (%s)", [ [a] for a in activeUsers] )
or event better:
for item in ulist: # or list['members'] Your example is kinda broken here
activeUsers.append([str(item['name'])])
dbShell.executemany("""INSERT INTO slickusers (username) VALUES (%s)""", activeUsers)
I am trying to check if an entry is already in the database and add the entry if it isn't but the if statement never runs even though the condition is met.
userCurs.execute("SELECT EXISTS(SELECT 1 FROM images WHERE imageLink=?)", (image,))
exists = userCurs.fetchone()
if exists is None:
addImage(userName,image,'','')
else:
print '--------> image skipped'
print userCurs.fetchone()
I get this output:
--------> image skipped
None
--------> image skipped
None
and no entries are made to the database
The SQL statement:
SELECT EXISTS(SELECT 1 FROM images WHERE imageLink=?)
will always return a single row. The value of the column in that row will be the result of the EXISTS() function, which will be TRUE if any matches are found, or FALSE otherwise.
Since your original code only tested for the existence of a row in the result, and the full statement will always return exactly one row, you get the behavior you see.
What I think you should do is have your query return a rowset that has zero rows if no matches are found, and one (or more) rows if matches are found:
userCurs.execute("SELECT 1 FROM images WHERE imageLink=?", (image,))
Now your original test should work - if no imageLinks match the query, then there will be no rows in the result, so the first fetchone() will return a null object as you expect.
Of course, as several others have mentioned, you should ony call fetchone() once per row since it moves the cursor.
Solved it by changing the code to
userCurs.execute("SELECT EXISTS(SELECT 1 FROM images WHERE imageLink=?)", (image,))
exists = userCurs.fetchone()
if exists[0] == 0:
addImage(userName,image,'','')
else:
print '--------> image skipped'
print exists
As people have said the calling fetchone() twice gives different results the actual value of exists was (0,)
Don't call cursor.fetchone() again; the next row is always empty. Reuse the variable. You are fetching a (0,) or (1,) tuple; you could use tuple assignment to extract the flag value (note the comma on exits, =):
exists, = userCurs.fetchone()
if not exists:
addImage(userName, image, '', '')
else:
print '--------> image skipped'
print exists
Now exists will be set to 0 or 1, rather than (0,) or (1,) and the if not exists: test will not pass for the 0 case.
How would I do:
FinancialStatements.objects.get(statement_id=statement_id)
or SalesStatements.objects.get(statement_id=statement_id)
The result will always yield one result.
I ended up using the try/except route here:
try:
statement_object = FinancialStatements.objects.get(statement_id=statement_id)
except FinancialStatements.DoesNotExist:
statement_object = SalesStatements.objects.get(statement_id=statement_id)
Why not simply do:
result = (FinancialStatements.objects.filter(statement_id=statement_id) or
SalesStatements.objects.filter(statement_id=statement_id))
This should work, because filter returns a list - and an empty list if no entries match. An empty list evaluates to false in python's boolean logic, e.g. try running:
print [] or "hello"
(Just as a check, compare print ["Hi"] or "hello")
So, if the first query returns empty, the second will then be run. However, if the first matches anything, this will be result and the second query will be ignored.
Addendum: result will then be of a list type - you'll need to extract the (one and only) element with result[0].