I'm pulling a custom field from Klaviyo but sometimes it doesn't exist so there is no variable.
This works when the variable is present:
return {
'new_total': int(input_data['original_total']) + 1
}
But it errors out if the variable is blank.
What should the code look like in the zapier Python block to:
a) check if the variable exists as a number
b) set it to 0 if it doesn't
c) then increment it by 1 for the new_total?
Thanks!
Zapier's input_data variable, where you store all of the information you are working with in your code step, is a dictionary object. As such it has all of the methods of a Python dictionary available to it. The way in which you are currently accessing the values in your input_data dictionary returns an error when the provided key does not exist. As noted in Python's documentation:
d[key]
Return the item of d with key key. Raises a KeyError if key is not in the map
A more flexible means of accessing values in a dictionary when you are unsure of the presence of a specific key is to use d.get(key) the get method either returns None or a value you specify if the key cannot be found.
With your example you could modify it to be:
return {
'new_total' : int(input_data.get('original_total', 0)) + 1
}
In the above code if a key is not found a 0 will be returned and then incremented by 1. The 0 in the above code can be anything you specify, or you may also leave it blank to return None but this would lead to an error with your provided example.
You can read more about dictionaries here
Related
I am trying to iter through childs, but I get None for first key which should be e.g. id of case.
example of code
and this is result of code [None, {'-MaISrSqXnill-8W7sG1': {'datum': '22.5.2021', 'idtip_uplate': '11', 'iduplata': '1', 'novaca': '50', 'predmet': '1'}}]
For some reason he don't see first key
When you use numeric keys (like the 1) in your data, you run the risk that Firebase will try to be helpful and interpret those keys as indexes in an array. So in your data it sees it as an array of size 2, with no element at index 0 and the -M... element at index 1. And that's what the output you print shows.
To prevent Firebase from interpreting the keys as an array, ensure they start with a string value. So prefix them with a short known value, like "key1".
For more on this, also see: Best Practices: Arrays in Firebase.
Our task is create a table, and read values to the table using a loop. Print the values after the process is complete. - Create a table. - Read the number of values to be read to the table. - Read the values to the table using a loop. - Print the values in the table using another loop. for this we had written code as
local table = {}
for value in ipairs(table) do
io.read()
end
for value in ipairs(table) do
print(value)
end
not sure where we went wrong please help us. Our exception is
Input (stdin)
3
11
22
abc
Your Output (stdout)
~ no output ~
Expected Output
11
22
abc
Correct Code is
local table1 = {}
local x = io.read()
for line in io.lines() do
table.insert(table1, line)
end
for K, value in ipairs(table1) do
print(value)
end
Let's walk through this step-by-step.
Create a table.
Though the syntax is correct, table is a reserved pre-defined global name in Lua, and thus cannot should not be declared a variable name to avoid future issues. Instead, you'll need to want to use a different name. If you're insistent on using the word table, you'll have to distinguish it from the function global table. The easiest way to do this is change it to Table, as Lua is a case-sensitive language. Therefore, your table creation should look something like:
local Table = {}
Read values to the table using a loop.
Though Table is now established as a table, your for loop is only iterating through an empty table. It seems your goal is to iterate through the io.read() instead. But io.read() is probably not what you want here, though you can utilize a repeat loop if you wish to use io.read() via table.insert. However, repeat requires a condition that must be met for it to terminate, such as the length of the table reaching a certain amount (in your example, it would be until (#Table == 4)). Since this is a task you are given, I will not provide an example, but allow you to research this method and use it to your advantage.
Print the values after the process is complete.
You are on the right track with your printing loop. However, it must be noted that iterating through a table always returns two results, an index and a value. In your code, you would only return the index number, so your output would simply return:
1
2
3
4
If you are wanting the actual values, you'll need a placeholder for the index. Oftentimes, the placeholder for an unneeded variable in Lua is the underscore (_). Modify your for loop to account for the index, and you should be set.
Try modifying your code with the suggestions I've given and see if you can figure out how to achieve your end result.
Edited:
Thanks, Piglet, for corrections on the insight! I'd forgotten table itself wasn't a function, and wasn't reserved, but still bad form to use it as a variable name whether local or global. At least, it's how I was taught, but your comment is correct!
I have values in a table called Translation, that contains for every values per example:
=> {"fr"=>"Jaune", "de"=>"", "en"=>"", "bg"=>"", "hr"=>"", "es"=>"", "hu"=>"", "it"=>"", "lt"=>"", "lv"=>"", "nl"=>"", "pl"=>"", "pt"=>"", "ro"=>"", "cs"=>""}
and I'm looking to get the the number of the translation for each language:
I'm trying :
Translation.where("values->>'fr' IS NOT NULL").count
but it giving me 0, which is not correct, do anyone knows how to do it correctly?
The problem that you have is that the keys that don't have values, still exist in the json, so "is not null" will return all of them because the key exist. you have two options here, you can remove the empty keys from the database, so not null will work, or, change it to see if the key is empty
You can do it like this
Translation.where("values->>'fr' <> ''").count
and it will work with the structure that you have right now.
I'm creating a variable that will hold missing values from a specific variable. Currently, this works but it gives the missing a value a 1. How do I tell spss to print the respondent's ResponseID instead?
My code below:
COMPUTE Q_2_MIS = MISSING(Q_2).
EXECUTE.
Thanks
Your code returns value of 1 because the condition missing(q_2) is evaluated to TRUE.
Try this:
DO IF MISSING(Q_2).
COMPUTE Q_2_MIS = ResponseID .
END IF.
EXECUTE.
or (as per eli-k's comment) simply use IF:
IF MISSING(Q_2) Q_2_MIS = ResponseID .
EXECUTE.
Note that you might need to create the Q_2_MIS variable first, if you do not have it in your dataset.
Alternatively, if you want to print out the IDs of the respondents with missing in Q_2:
TEMPORARY.
SELECT IF missing(q_2).
LIST ResponseID q_2.
You will see a list of IDs in the SPSS Output, with a (blank) Q_2 next to each ID.
Is there any performance or any other difference between defining [] or put if absent
map["x"] = 123;
map.putifabsent("z", ()=> 123);
Aside from map['x'] = 123 will overwrite the value for key'x' if it is already in the map whereas map.putifAbsent('x', ()=> 123) will not ?
Note that putifAbsent returns the value corresponding to the provided key. You can use it to get the value for a key, generating and inserting a value 'on the fly' if the key in missing from the map (which is why its second argument is a function, not a straight value).
It strikes me that this was the use case for the method, but the name getValuePutIfAbsent was deemed too long.