How can i use action_listen inside a custom action in Rasa - chatbot

I want to listen to user inside custom action and then continue that custom action.
Basically what i am looking for is.
I have a loop in custom action from 0 to 5.
for each value i want to take some input from user and continue that loop.
def Action():
for 0 to 5:
input = action_listen
// do something with input

You should use a form for this. A form loops over a set of slots that you define until all are filled.
Looping with action_listen in a regular action won't work because an action has only one run() method and the events are only added to the tracker once the run() method has returned, after which the action is completed (and you can't get back into it).
https://rasa.com/docs/rasa/core/forms/

Thanks for your response, but some tricks also works for me to do the same in custom action.
I returned the Form as FollowUpAction at each iteration and reduce the loop variable by one.
Now the Form will ask the user to get required slot (Do the needful with with information) and again Set the slot to None with SlotSet. Now return the Action as FollowUpAction in form.
In this way for each iteration Bot will take response from user.
global i = 0
class Action():
def run():
for i to 5:
return [FollowUpAction('ActionForm')]
class ActionForm():
def requiredslot():
return ['take_value']
def submit():
//Do needful with input
return [SlotSet("take_value", None), FollowUpAction("my_action")]

Related

How to pass args to the callback function in RPi.GPIO.add_event_detect()

I've tried help(RPi.GPIO.add_event_detect) and checked its official wiki with no answer to that. The official example is like:
GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback, bouncetime=200)
Is there any way to pass args to my callback function (other than the pin channel) ?
Updates:
What I'm trying to do is to stop/resume a camera&servo task via a button. My code is like:
active = [False]
GPIO.add_event_detect(channel, GPIO.BOTH, callback=button_callback)
while True:
if active[0]:
# camera&servo doing something which lasts seconds per loop
I want to pass the arg active to function button_callback in order to set it True/False. If I directly put GPIO.input(channel) in the while loop, I might miss the button input.
So how to pass that arg to the callback or is there any better way to achieve that?
You don't need to pass active to your callback function. The easiest solution for what you're doing is probably making active a global variable, so that your callback looks like this:
active = False
def button_callback(channel):
global active
active = GPIO.input(channel)
GPIO.add_event_detect(channel, GPIO.BOTH, callback=button_callback)
while True:
if active:
# camera&servo doing something which lasts seconds per loop
You're probably going to have to deal with debouncing your switch.
If you really want to pursue your original plan and pass the active list to your callback function, something like this should work:
def button_callback(channel, active):
active[0] = GPIO.input(channel)
GPIO.add_event_detect(channel, GPIO.BOTH,
callback=lambda channel: button_callback(channel, active))

What would be the best way to define a gatling a user flow using ChainBuilder?

I am new to Scala and Gatling and I am trying to figure out what would be the best way to define a user story and pass it a ChainBuilder to Gatling Scenario.
When I say user Story In my case I mean a flow that will consist of Login, many different calls and then a loop over another list of calls for the whole duration of the test.
I have created the following function to create a scenario:
def createScenario(name: String, feed: FeederBuilder, chains: ChainBuilder*): ScenarioBuilder = {
scenario(name).feed(feed).forever() {
exec(chains).pause(Config.pauseBetweenRequests)
}
}
And here is how I execute this function:
val scenario = createScenario(Config.testName, feeder.random,
setSessionParams(PARAM1, Config.param1),
setSessionParams(PARAM2, Config.param2),
login,
executeSomeCall1,
executeSomeCall2,
executeSomeCall3,
executeSomeCall4,
executeSomeCall5,
executeSomeCall6,
executeSomeCall7,
executeSomeCall8,
executeSomeCall9,
)
Here is an example of what executeSomeCall function looks like:
def executeSomeCall = {
exec(http("ET Call Home")
.post("/et/call/home")
.body(ElFileBody("/redFingerBody.json")).asJson
.check(status is 200))
}
My first question:
Is that the correct way to define a chain of rest calls and feed it to the scenario? I am asking that because what I see when I define a flow like that is that for some reason not all the my REST calls are actually executed. Weirdly enough, if I change the order of the calls it does work and all functions are called. (So I am definitely doing something wrong)
My second question:
How can I define an infinite loop within this flow? (Infinite for as long as the test is running)
So for example, I'd like the above flow to start and when it reaches executeSomeCall8, it will then loop executeSomeCall8 and executeSomeCall9 for the whole duration of the test.
I don't see why your calls would not be executed, however the way you're constructing your scenario is not that flexible. You can make use of chaining without requiring a createScenario() method.
That leads to your second question, when you have the scenario chained like:
val scn = scenario("something")
...
.exec(someCall7)
.forever(){
exec(sommeCall8)
.exec(someCall9)
}
...
where someCallN in my case look like:
val someCall = http("request name")
.get("/some/uri")
...
Note: foerever() is just an example, you can use other loop statements that suits your needs.
I hope it helps.

wxPython Unbind method returns True but doesn't Unbind

I am trying to Unbind the event wx.EVT_LEFT_DOWN from a TextCtrl window either
the first time the user clicks inside the text field, or
with the first character entered in the text field.
My initial attempt at (1.) failed (see commented code below) when I attempted an Unbind inside the method to which the event was bound. The TextCtrl field no longer allowed any text entry.
The attempt at (2.) is shown below, but does not produce the desired behavior even though I've tested the return value of the Unbind and it is True.
The desired behavior is that, the first time the user clicks in the window (TextCtrl), the default text should be cleared, but, once the user has entered text and clicks again in the window, the text entered should not be cleared. How can I achieve the desired behavior with the Unbind method?
I can think of other ways (e.g. count the number of times the user clicks inside the window and only clear the text if it is the first time), but these are less clean.
import wx
class FormTab(wx.Panel):
def __init__(self, *args, **kwargs):
super(FormTab, self).__init__(*args, **kwargs)
self.createControls()
self.bindEvents()
self.doLayout()
def createControls(self):
self.exeTextCtrl = wx.TextCtrl(self, value="Executable")
font=wx.Font(8,wx.DEFAULT,wx.ITALIC,wx.NORMAL)
self.exeTextCtrl.SetFont(font)
def bindEvents(self):
for control, event, handler in \
[(self.exeTextCtrl, wx.EVT_LEFT_DOWN, self.onExeReady),
(self.exeTextCtrl, wx.EVT_TEXT, self.onExeEntered)]:
control.Bind(event, handler)
def onExeReady(self, event):
self.exeTextCtrl.SetValue('')
font=wx.Font(8,wx.DEFAULT,wx.NORMAL,wx.NORMAL)
self.exeTextCtrl.SetFont(font)
# (1) attempt failed and made text field inoperable
# self.exeTextCtrl.Unbind(wx.EVT_LEFT_DOWN, self.onExeReady)
event.Skip()
def onExeEntered(self, event):
font=wx.Font(8,wx.DEFAULT,wx.NORMAL,wx.NORMAL)
self.exeTextCtrl.SetFont(font)
exclass.exe=event.GetString()
# (2) attempt doesn't unbind event (even though following returns True)
self.exeTextCtrl.Unbind(wx.EVT_LEFT_DOWN, self.onExeReady)
# etc...
Thank you.
From wx._core:
class EvtHandler(Object):
"""Proxy of C++ EvtHandler class"""
...
def Unbind(self, event, source=None, id=wx.ID_ANY, id2=wx.ID_ANY, handler=None):
"""
Disconnects the event handler binding for event from self.
Returns True if successful.
"""
In your example, change:
self.exeTextCtrl.Unbind(wx.EVT_LEFT_DOWN, self.onExeReady)
which leads to a AttributeError: 'function' object has no attribute 'GetId' (because you specified the handler self.onExeReady instead of a source parameter), to:
self.exeTextCtrl.Unbind(wx.EVT_LEFT_DOWN)
Admittedly, none of the following did work (did not work and returned False):
self.Unbind(wx.EVT_LEFT_DOWN, source=self.exeTextCtrl)
self.exeTextCtrl.Unbind(wx.EVT_LEFT_DOWN, self.exeTextCtrl)
self.exeTextCtrl.Unbind(wx.EVT_LEFT_DOWN, source=self.exeTextCtrl, handler=self.onExeReady)
Hope that helps.

YUI Datatable: rowsPerPageChage Event: Get Drop Down ID on change

I'm reposting this question from an earlier post because when I originally asked it, it was unclear. I wasn't sure what I actually needed, and that caused the responses I was getting to be not applicable.
Original: YUI Datatable - Get ID of DOM Element after page has loaded and use it in other YUI events
My question is this:
I have a YUI Datatable. I have a rows per page drop down. I need to create an event, or event handler, or utilize one that exists, that will handle the following:
When(ever) the drop down displaying the rows per page changes, the event handler should get the id of that drop down and pass it to another function to use as an argument. My datatable has two rowsPerPageDropDown (aka rppDD) elements. I need this functional for both of them.
Preferably, it would also do this when the page loads, but for now that's extra credit.
I know that the id of "top" rppDD element is currently "yui-pg0-0-rpp12" but my problem extends from the fact that if I add any code to this page, that id will change (to "-rpp13" or something similar). That throws off the rest of my functions. So I want to just grab the id (whatever it may be) at run time and use it that way rather than hard coding it in.
The second function will run immediately after the drop down changes. It will use the id of the drop down to get the currently selected value and assign it to a variable to be used with a third function.
Here's what I have so far: In the earlier thread, it was suggested that I use the event rowsPerPageChange. This works only to a certain degree. Placing a simple "hello world" alert within that event handler proves that it only alerts when the page loads. Whenever I click and change the drop down to something else it no longer alerts.
Below is the function(s) and code I'm using to test this:
var getRppValue = function() {
YAHOO.util.Event.addListener("rowsPerPageChange", getRppValue.prototype.handleOnAvailable());
}
var rppDD_ID = "";
getRppValue.prototype.handleOnAvailable = function() {
alert("hello world");
alert(this.id); //should alert "yui-pg0-0-rpp12"
//rppValue = this.id;
}
var rppEvent = new getRppValue();
Near as I can tell this event only fires/gets handled when the page initially loads. At that time, the first alert alerts "hello world" as expected. The second alert shows "undefined". The intention is that the second alert should alert whatever the id of the rppDD element is ("yui-pg0-0-rpp12").
For reference, the function I plan on passing the id into is as follows. It is intended as I mentioned above to assign the current value of [the|either|both] rppDD element(s) to the variable oRPP. It then calls the endDrag() function (which utilizes oRPP):
If I were to hard code the id it would be:
function getRPP_0(){oRPP = g("yui-pg0-0-rpp12").value;endDrag();};
I want it to by dynamic:
function getRPP_0(){oRPP = g(rppDD_ID).value;endDrag();};
Any help / suggestions are appreciated.
As we saw on the chat, you had this:
var onRPPChange0 = YAHOO.util.Event.addListener("yui-pg0-0-rpp12", "change", getRPP_0);
var onRPPChange1 = YAHOO.util.Event.addListener("yui-pg0-1-rpp24", "change", getRPP_1);
function getRPP_0(){oRPP = g("yui-pg0-0-rpp12").value;endDrag();};
function getRPP_1(){oRPP = g("yui-pg0-1-rpp24").value;endDrag();};
And you wanted to be able to access the element being changed in the function.
Fortunately, event handlers provide the element on which the event is happening in the this object.
So, this works:
var onRPPChange0 = YAHOO.util.Event.addListener(
"yui-pg0-0-rpp12", "change", function() {
// Notice that "this" is the element on which the event was fired
oRPP = this.value;
endDrag();
};
This way, you can generalize your function easily without hardcoding the IDs.

Squeryl: Run query explicitly

When I create a query in squeryl, it returns a Query[T] object. The query was not yet executed and will be, when I iterate over the Query object (Query[T] extends Iterable[T]).
Around the execution of a query there has to be either a transaction{} or a inTransaction{} block.
I'm just speaking of SELECT queries and transactions wouldn't be necessary, but the squeryl framework needs them.
I'd like to create a query in the model of my application and pass it directly to the view where a view helper in the template iterates over it and presents the data.
This is only possible when putting the transaction{} block in the controller (the controller includes the call of the template, so the template which does the iteration is also inside). It's not possible to put the transaction{} block in the model, because the model doesn't really execute the query.
But in my understanding the transaction has nothing to do with the controller. It's a decision of the model which database framework to use, how to use it and where to use transactions. So I want the transaction{} block to be in the model.
I know that I can - instead of returning the Query[T] instance - call Iterable[T].toList on this Query[T] object and then return the created list. Then the whole query is executed in the model and everything is fine. But I don't like this approach, because all the data requested from the database has to be cached in this list. I'd prefer a way where this data is directly passed to the view. I like the MySql feature of streaming the result set when it's large.
Is there any possibility? Maybe something like a function Query[T].executeNow() which sends the request to the database, is able to close the transaction, but still uses the MySQL streaming feature and receives the rest of the (selected and therefore fixed) result set when it's accessed? Because the result set is fixed in the moment of the query, closing the transaction shouldn't be a problem.
The general problem that I see here is that you try to combine the following two ideas:
lazy computation of data; here: database results
hiding the need for a post-processing action that must be triggered when the computation is done; here: hiding from your controller or view that the database session must be closed
Since your computation is lazy and since you are not obliged to perform it to the very end (here: to iterate over the whole result set), there is no obvious hook that could trigger the post-processing step.
Your suggestion of invoking Query[T].toList does not exhibit this problem, since the computation is performed to the very end, and requesting the last element of the result set can be used as a trigger for closing the session.
That said, the best I could come up with is the following, which is an adaptation of the code inside org.squeryl.dsl.QueryDsl._using:
class IterableQuery[T](val q: Query[T]) extends Iterable[T] {
private var lifeCycleState: Int = 0
private var session: Session = null
private var prevSession: Option[Session] = None
def start() {
assert(lifeCycleState == 0, "Queries may not be restarted.")
lifeCycleState = 1
/* Create a new session for this query. */
session = SessionFactory.newSession
/* Store and unbind a possibly existing session. */
val prevSession = Session.currentSessionOption
if(prevSession != None) prevSession.get.unbindFromCurrentThread
/* Bind newly created session. */
session.bindToCurrentThread
}
def iterator = {
assert(lifeCycleState == 1, "Query is not active.")
q.toStream.iterator
}
def stop() {
assert(lifeCycleState == 1, "Query is not active.")
lifeCycleState = 2
/* Unbind session and close it. */
session.unbindFromCurrentThread
session.close
/* Re-bind previous session, if it existed. */
if(prevSession != None) prevSession.get.bindToCurrentThread
}
}
Clients can use the query wrapper as follows:
var manualIt = new IterableQuery(booksQuery)
manualIt.start()
manualIt.foreach(println)
manualIt.stop()
// manualIt.foreach(println) /* Fails, as expected */
manualIt = new IterableQuery(booksQuery) /* Queries can be reused */
manualIt.start()
manualIt.foreach(b => println("Book: " + b))
manualIt.stop()
The invocation of manualIt.start() could already be done when the object is created, i.e., inside the constructor of IterableQuery, or before the object is passed to the controller.
However, working with resources (files, database connections, etc.) in such a way is very fragile, because the post-processing is not triggered in case of exceptions. If you look at the implementation of org.squeryl.dsl.QueryDsl._using you will see a couple of try ... finally blocks that are missing from IterableQuery.