Python: passing local variable reference as string - python-2.7

I use an interactive interpreter for data analysis, so I tend to just define functions and call them when I need them. In this case, I would like to call some function:
def function(var):
do stuff
ax.plot(x, y)
blah
ax.set_title('var')
where var in the function call is ndarray and var in set_title is the reference of the ndarray.
I'm currently implementing this with
def function(var, varstring):
do stuff
ax.plot(x, y)
blah
ax.set_title(varstring)
where var is foo and varstring is just 'foo' - an example call:
module.function(foo, 'foo')
now, my function takes many more variables and it's unwieldy to duplicate everything over and over again, but I can't figure out how to just get var as 'var' .. any attempt to do so provides me with a string of the ndarray values.

If you're working in the interpreter, the variables are likely in the global scope. Thus, you can access them using var = globals()[varstring] and then pass in the string name into each function.
Note that an object (var) has no knowledge of its names (varstring) which is why the global name needs to be passed to the function rather than the object itself.

you have to define your function in this way:
def function(varstring):
var = globals()[varstring]
# do stuff with `var` variable
ax.plot(x, y)
ax.set_title(varstring)
and then call module.function('foo')

Related

How do I get a custom element definition without instantiating it?

After looking at the custom element spec, it's not immediately obvious how I get a reference to a custom element definition without first instantiating it (which can be problematic). Is there a way to directly reference a custom element's prototype?
More concretely, if I have:
var proto = Object.create(HTMLElement.prototype);
proto.createdCallback = function() { // some heavy operation };
document.registerElement('x-foo', {prototype: proto});
At some point later, I would love to reference the prototype with something like:
// wish
var XFoo = document.getElementDefinition('x-foo');
But instead the only way I've come up with is:
// reality
var XFoo = document.createElement('x-foo').__proto__;
This is especially problematic when trying to write tests against heavy components - as there's no way to stub out the heavy behavior (with something like XFoo.createdCallback = // stub; before the original method is actually called.
If you have reference to the constructor of the custom element, you can use it to access the prototype.
var XFoo = document.registerElement('x-foo', {prototype: proto});
XFoo.prototype // this will give u access to the prototype object.
there's no way to stub out the heavy behavior
Use a function reference rather than an anonymous function:
proto.createdCallback = model.foo;
define it:
var model = {};
model.foo = function(){/*some heavy operation*/};
then stub it by redefining it:
var XModel = {};
XModel.foo = function(){/*stub*/};
and reference it in the test:
XFoo.createdCallback = XModel.foo;
References
AOP Aspect of JavaScript
AJAX Interception
Intro to Aspect Oriented Programming

Should I duplicate test data and assert data?

The question is probably best asked with a simple example:
var myObj = { name: 'John' };
var copiedObj = ObjectCopier.copy(myObj);
copiedObj.name.should.equal('John'); // Hard code 'John' twice
copiedObj.name.should.equal(myObj.name); // Reference the original value
Is one method preferred over the other? Assuming the value passed in is what I expect to be returned, is there any harm in the 2nd assert? Does it even matter?
In more complex cases you won't be able to duplicate an object completely - and you wouldn't want to. it would be better written this way:
var OBJ_NAME = 'John'
var myObj = { name: OBJ_NAME };
var copiedObj = ObjectCopier.copy(myObj);
copiedObj.name.should.equal(OBJ_NAME);
this way you're not duplicating any code/defines, and you can also make tests such as:
myObj.name.should.equal(OBJ_NAME);
to test for the object copier not changing the original object either (which either of your lines won't test for).

Invoke function by proxy

I am trying to create a cf component to proxy another one. At the moment the code looks like this: (stripped down for the sake of example):
public MyFuseboxProxy function init( Required any myFb ){
variables.myFusebox = arguments.myFb;
return this;
}
this.do = variables.proxy;
private any function proxy(){
var local.functionName = getFunctionCalledName();
var local.function = variables.myFusebox[local.functionName];
var local.returnVal = local.function( arguments );
...
}
As you can see, it's quite straight forward. I pass in my target object at initialisation then use the proxy method to intercept function calls. I am using cfscript, and don't want to use cfinvoke, so am using this approach.
I then call the proxy as follows:
var local.proxy = new ab.MyFuseboxProxy( myFusebox );
var local.dump = local.proxy.do ( action='display.body', contentvariable="body" );
However, when I execute the above code I get the following error:
The ACTION argument passed to the do function is not of type string.
If the component name is specified as a type of this argument, it is possible that either a definition file for the component cannot be
found or is not accessible.
The error occurred in C:/ColdFusion10/cfusion/wwwroot/fusebox5/myFusebox.cfc: line 301
The error is reported on the target component, so it seems like the function is being called, and the arguments passed through, but the type is not being preserved/recognised as a String.
Can anyone advise what I am doing wrong or how I can preserve the argument types?
Yup, I suspect instead of this:
var local.returnVal = local.function( arguments );
You mean this:
var local.returnVal = local.function(argumentCollection=arguments );
Your current code is passing the arguments as the first argument, rather than passing them as they were originally passed in.

Luabridge weak reference to LuaRef data

Consider the following example:
function Process()
local Container=NewContainer()
Container:On(EventType.Add,function()
Container:DoSomething()
end)
-- Does not Garbage Collect
end
In luabridge, I store the function() as LuaRef which extends the lifetime for the Container and it will not be GCed because it's a RefCountedObjectPtr
Here is a workaround that I use to use a weak table which works, but it looks ugly:
function Process()
local Container=NewContainer()
local ParamsTable={ Container=Container }
setmetatable(ParamsTable, { __mode = 'k' })
Container:On(EventType.Add,function()
ParamsTable.Container:DoSomething()
end)
-- Garbage Collects fine
end
Is there any way to have a LuaRef that functions similar to this? Or maybe there is another workaround?
Here is the way I approached this problem:
Create a wrapper class around C++ luabridge class (If you have class Display.A() in C++, create class A() in Lua)
Store a weak table inside that wrapper class (self.WeakTable={} and setmetatable(self.WeakTable, { __mode = 'k' }))
In the weak table, reference self: (self.WeakTable.self=self)
Pass self.WeakTable to C++ and store in as LuaRef - this will gc
Create a wrapper function like so:
Container:On(EventType.Add,function(WeakTableParams)
WeakTableParams.self.Callback();
end)

how to unit testing function that doesn't return value in phpunit?

so this is the class that i want to test. and specifically i just pick one of the function that i want to test. while var is a value returned from doing some function from classB bar is instance from classC and then do some function which pass some variables. for most of the hints/example, the function to be tested is return a value. so my question is, how to test that this particular function worked?
thanks.
class mA extends A {
...
function doSomething($foo) {
$var = doStuffFromClassB("hallo");
$bar = ClassC::instance();
$bar->doStuffFromClassC($var, $foo, "world");
}
}
If it's called doSomething and it doesn't indicate what it does by returning a value, then you can use mock objects to trace the interaction with the other objects.
See PhpUnit's documentation on mock objects. I guess in this case you want to verify that the doStuffFromClassC method is involved with the var from doStuffFromClassB.