how to fill a slot in alexa backend using python ask sdk - python

I need to fill the slot value of intent depending on some conditions.
I referred to the following documentation.
https://developer.amazon.com/en-US/docs/alexa/custom-skills/delegate-dialog-to-alexa.html#node_delegate_default_values_example
In this document, they do something like this.
// fromCity.value is empty if the user has not filled the slot. In this example,
// getUserDefaultCity() retrieves the user's default city from persistent storage.
if (!fromCity.value) {
currentIntent.slots.fromCity.value = getUserDefaultCity();
}
Similarly, I want to know to do this using the python ASK SDK. also how to return something similar to this?
// Return the Dialog.Delegate directive
return handlerInput.responseBuilder
.addDelegateDirective(currentIntent)
.getResponse();
thanks in advance!!

I finally found the solution for this.
from ask_sdk_model.dialog import delegate_directive
updateIntent = {
'name' : intent_name,
'confirmation_status' : 'NONE',
'slots' : handler_input.request_envelope.request.intent.slots
}
return handler_input.response_builder.add_directive(delegate_directive.DelegateDirective(updated_intent = updateIntent)).response

Related

Tableau API for Python, Get Parameter Value -- JS's .getParametersAsync() Equivalent for Python

I want to extract a workbook's currently set parameter values.
I've found reference of the desired request, as someone uses it in JS, but I can't find it anywhere in the REST API documentation, nor the documentation for tableau-api-lib nor tableauserverclient. Tableau API: Get parameter value
I can query workbooks just fine using either of the above referenced libraries, but is there a method I'm missing somewhere to get the parameter values?
Ideally I'd like to be able to modify them before a query, but getting what they're currently set at would be a nice start.
Javascript equivalent:
paramObjs = currentViz.getWorkbook().getParametersAsync();
paramObjs.then(function(paramObjs) {
for (var i = 0; i < paramObjs.length; i++) {
try {
var name = paramObjs[i].getName();
var value = paramObjs[i].getCurrentValue();
params[name] = value.value;
} catch (e) { }
}
});
From what it looks like, this task can not be done using Python.
The Workbook class here does not have any attributes/methods to get parameters
Tableau's REST API reference has no method/endpoint for that
Other API's like the Metadata and Hyper APIs have no connection/correlation with the task
A list of all Tableau packages/libraries is given here. Going through all Python libraries, there wasn't any method to fetch the parameters.
In JavaScript, they are actually creating visualizations, which allows them to query the visualization itself (currentViz.getWorkbook()). The same isn't applicable for Python I guess, which explains the missing support/APIs.

Sorting Terraform variables.tf by variable name using Python

I am working on creating a new VPC where I need to provide some variables as input.
All the variables are listed in variables.tf. The file is very long (I only copied couple of them here) and variables are defined in no particular order.
I need to find a Pythonic way to sort my variables.tf by variable name.
variable "region" {
description = "The region to use when creating resources"
type = string
default = "us-east-1"
}
variable "create_vpc" {
description = "Controls if VPC should be created"
type = bool
default = true
}
variable "name" {
description = "Name to be used, no default, required"
type = string
}
The sorted variables.tf should look like this:
variable "create_vpc" {
description = "Controls if VPC should be created"
type = bool
default = true
}
variable "name" {
description = "Name to be used, no default, required"
type = string
}
variable "region" {
description = "The region to use when creating resources"
type = string
default = "us-east-1"
}
"Pythonic" might be the wrong approach here - you are still comfortably sitting behind a python interpreter, but for better or worse (worse) you are playing by Terraform's rules. Check out the links below. Hashicorp "enables" python via their CDK, and there are several other projects out there on github.
Once you are up and running with something like that, and you have Terraform fully ported over to your Python setup, then you can start thinking pythonic. </IMO>
https://github.com/hashicorp/terraform-cdk
https://github.com/beelit94/python-terraform/blob/develop/python_terraform/terraform.py
Here's what I came across last year https://github.com/hashicorp/terraform/issues/12959 and is why I created https://gist.github.com/yermulnik/7e0cf991962680d406692e1db1b551e6 out of curiosity. Not Python but awk :shrugging:
Simple awk script to sort TF files. Not just variables, but any of 1st level resource definition blocks.

Trigger Cloud Function based on Firestore changes (Python)

Whenever there is change(created, modified, removed) in any document for a particular collection in Firestore, i want to capture the delta and publish it to pubsub.
I am trying all this in python.
Code Snippet :
ref = db.collection('country').document(city_id)
ref.update(body_json)
ref.on_snapshot(callback)
def callback(col_snapshot, changes, read_time):
for change in changes:
if change.type.name == 'ADDED':
print('ADDED')
elif change.type.name == 'MODIFIED':
print('MODIFIED')
elif change.type.name == 'REMOVED':
print('REMOVED')
print('end of callback')
Now, when i make changes in the firestore document, like :
Add new document - I get the print as ADDED and end of callback.
Modify existing document - I mostly get the print as ADDED but sometimes or rather once in 10 runs i get both ADDED and MODIFIED. followed by end of callback.
Remove any document - end of callback. Sometimes(rarely) both REMOVED and end of callback.
I am unable to understand this behaviour and don't know how to deal with this unable print executions.
The limitation is this is Node.js only, so Javascript only. I think it still applies and is worth you looking into, so sharing it here as despite your question being Python based it's the best option.
I think Cloud Functions would be great for you here. You can set triggers that will run functions on each of the things you've noted.
Added can be handled by onCreate.
Modified can be handled by onUpdate.
Removed - by onDelete.
You basically set up functions that look like this:
exports.updateUser = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
// Get an object representing the document
// e.g. {'name': 'Marie', 'age': 66}
const newValue = change.after.data();
// ...or the previous value before this update
const previousValue = change.before.data();
// access a particular field as you would any JS property
const name = newValue.name;
// perform desired operations ...
});
You're given the before and after states of the document for the example above, so you'll be able to not just know it was changed but what was changed.
The example code was straight from the documentation for Cloud Functions here.

Can i pass a variable which contains all parameters for a function to a function in python?If yes, then how?

Suppose there is the following function:
def write_form(username="", email="", username_error="", email_error="", password_error_one="", password_error_two="" ):
response.out.write(user_signup_form %{ "username": username,
"email": email,
"username_error" : username_error,
"email_error" : email_error,
"password_error_one" : password_error_one,
"password_error_two" : password_error_two })
And i want to call in the following manner:
s='user_username,user_email, email_error="email_error", password_error_one="password_error_1"'
write_form(s)
Instead of:
write_form(user_username,user_email, email_error="email_error", password_error_one="password_error_1")
Is it possible to do so?If yes how?
You can use a dictionary and then unpack it like this
s={"username":"username",
"email":"a#b.com",
"email_error":"email_error",
"password_error_one":"password_error_1"}
def write_form(username="", email="", username_error="", email_error="",
password_error_one="", password_error_two = ""):
print username, email, email_error, password_error_one, password_error_two
write_form(**s)
The classic solution to "my functions take a large number of parameters" is to move those parameters into an object, and make the function a method of that object.
It looks like you're processing web forms. There are a bunch of libraries that already exist to make this easier. Notably the Django framework has extensive libraries.

Python C API: Switch on PyObject type

I have some code to interface Python to C++ which works fine but every time I look at it I think there must be a better way to do it. On the C++ side there is a 'variant' type that can deal with a fixed range of basic types - int, real, string, vector of variants, etc. I have some code using the Python API to convert from the equivalent Python types. It looks something like this:
variant makeVariant(PyObject* value)
{
if (PyString_Check(value)) {
return PyString_AsString(value);
}
else if (value == Py_None) {
return variant();
}
else if (PyBool_Check(value)) {
return value == Py_True;
}
else if (PyInt_Check(value)) {
return PyInt_AsLong(value);
}
else if (PyFloat_Check(value)) {
return PyFloat_AsDouble(value);
}
// ... etc
The problem is the chained if-else ifs. It seems to be calling out for a switch statement, or a table or map of creation functions which is keyed by a type identifier. In other words I want to be able to write something like:
return createFunMap[typeID(value)](value);
Based on a skim of the API docs it wasn't obvious what the best way is to get the 'typeID' here directly. I see I can do something like this:
PyTypeObject* type = value->ob_type;
This apparently gets me quickly to the type information but what is the cleanest way to use that to relate to the limited set of types I am interested in?
In a way, I think you've answered your own question.
Somewhere, you're going to have to select functionality based on data. The way to do this in C is to use function pointers.
Create a map of object_type->function mappers... where each function has a clearly-defined interface.
variant PyBoolToVariant(PyObject *value) {
return value == Py_True;
}
Map<PyTypeObject*,variant* (PyObject*)> function_map;
function_map.add(PyBool, PyBoolToVariant);
Now your makeVariant can look like this.
variant makeVariant(PyObject *value) {
return (*function_map.get(value->ob_type))(value);
}
The hard part is going to be getting the syntax right for the Map object. Also, I'm assuming there is a Map object you can use that takes type parameters (<PyTypeObject*, variant*(PyObject*)).
I probably have not quite gotten the syntax correct for the second type of the Map. It should be a pointer to a function which takes one pointer and returns a pointer to a variant.
I hope this is helpful.

Categories

Resources