I'd like to create a generic proxy mechanism which could intercept any access to the object. Basically for a function it would look like this:
proxy <- function (fn) function (...) {
do.call (fn, list (...));
}
This way I could for example easily add logging. Playing around with assigning formals I can even get it to look like the passed in original function, however this doesn't work for anything that's not a function, is it possible to extend this concept for other types? Some way I could wrap a number and receive a callback anytime it's accessed
Related
I´m building some bokeh plots functions that, depending on the structured of the supplied data, will have different quantity of Select widgets.
This way, I have a list called 'filters' that contains each Select widget as a list element:
filter = [Select1, Select2, ....]
I´m assingning to each of these function a same callback 'update_dropdown', as code bellow. For the last filter, I finally assign a callback for 'update_plot'.
for f in filters[:-1]:
f.on_change('value', update_dropdown)
filters[-1].on_change('value', update_plot)
However, for this strategy to be effective I need to get which of the filters has triggered the 'update_dropdown' callback inside the 'update_dropdown' function for it to update the other filters accordingly. IS it possible to accomplish it?
I would expect something like this:
def update_dropdown(attr, old, new, FILTER_WHO_TRIGGERED_THE_CALLBACK):
#doing some stuff here accordingly to the variable FILTER_WHO_TRIGGERED_THE_CALLBACK
Thanks!!!!
There's no way to do it directly - you have to create a separate callback for each filter.
Something like:
def bind_cb_obj(cb_obj, cb):
def wrapped(attr, old, new):
cb(cb_obj, attr, old, new)
return wrapped
filters[-1].on_change('value', bind_cb_obj(filters[-1], update_plot))
In my application I have multiple variables that need to be accessed globally from the different functions of my script:
var a=1,b=2,c, ...;
Where "c" undefined at the beginning and takes value produced by some of the functions during scripts execution.
In order to declare them from within my "main" function I'm trying to use window object:
window.a=1;
window.b=2;
window.c;
This works, however I'm not sure if such approach is correct.
And is there is a way to avoid creation multiple window objects for each variable and combine them into more compact structure? Something like:
window.a=1,.b=2,.c;//---of course-this doesn't work
I was asking this question today too. It used to be the way when I last wrote javascript (mid-90s) and still seems to be the case see this resource
defmodule HelloWeb.Router do
use HelloWeb, :router
scope "/", HelloWeb do
get("/", PageController, :index)
end
end
In this example, why isn't the controller's function passed directly, like get("/", PageController.index)?
I think there are two reasons for this. The first is in how the module name becomes known. In your example, you have a scope for HelloWeb, and then a get endpoint in PageController. This actually calls the HelloWeb.PageController module.
The second reason, which I think is more important here, is because the function is not actually called directly. When you use HelloWeb, :controller, it defines an action/2 function for you in your module. This action/2 function is what gets called. It just calls the function that you specified.
This may seem like it is needlessly complex. But it allows us to define your own action/2 function within your module to override the one provided by phoenix. Maybe all of the actions within a given module need some record(s) pulled from a database. This is where you could do that and put it within the assigns of the connection.
I have function that is using unity interception to log the method time. The problem comes when I want to log deeper info like database call time, backend time etc.
So my method (let say, M1) calls some other method(M2) that inturn call some other method and so on to finally call a dbMethod that calls db. I am able to log time for all functions individually but for final aggregation in my log server , it would be helpful if I can find for which M1 call, how much time the dbServer Method took.
So is there some property like threadId that I can use that remain same during the nested calls so that I can use them in final aggregation (for joining M1 and dbMethod log data)? I would like that unique value to be different in different invokation of M1.
For anyone out there that is facing same problem, I solved it using CallContext class. Just add a request Id in outermost call and it will propagate to all the inner nested call.
I have implemented AJAX for calling server side function using client side function call i.e Calling server side function using javascript tag by using PageMethod in javascript.
I have few question regarding the same implementation.
function OnBlurCall()
{
chk(ControlId, SpanMsgId, FunctionName)// where all these parameters are obtained from programmer.
}
function chk(ControlId,SpanMsgId, FunctionName)
{
PageMethods.FunctionName(ControlId.value,onSuccess,onFail,SpanMsgId); // I want to replace actual value of FunctionName i.e Something given below
PageMethods.CheckUserNameAvailability(ControlId.value,onSuccess,onFail,SpanMsgId);
}
function onSuccess(result,MsgId,methodname)
{
MsgId.innerHTML=result;
}
function onFail(error,MsgId,methodname)
{
MsgId.innerHTML=error;
}
Ques 1. How many parameters can i add to this function. Is it there some limit that i can send only 3 parameters. Please give some details how it is working.
Ques 2. I want to make above code reusabe in javascript such that I add all the 3 functions in master page. But the problem is that i can add two functions into javascript at this time. But for adding 3rd function i have to send function name also as a parameter.
PageMethods.FunctionName(ControlId.value,onSuccess,onFail,SpanMsgId);
This FunctionName will be the name of server side function that a developer want to call.
Can i do this. ?
You can generically check for arguments; while in the function method, the arguments array has a list of all the arguments for that method. Check this out: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Functions_and_function_scope/Arguments
To invoke dynamically, you could consider using the WebServiceProxy: http://www.asp.net/ajax/documentation/live/clientreference/Sys.Net/WebServiceProxyClass/WebServiceProxyInvokeMethod.aspx