Wrapping Origen::Parameters define_params method - origen-sdk

I would like to wrap the define_params method so the user can pass in an array versus a symbol, so binning configuration can be setup for multiple test insertions at once :
scan.define_binning [:ws1, :ws2] do |config|
config.chain.softbin = 'bbxxx'
config.chain.bin = 16
config.logic.softbin = 'bbxxx'
config.logic.bin = 5
end
How would you wrap around the define_params method?
Thanks!

You would be as well submitting an update to Origen to make define_params accept an array of parameter names, but to answer the question (note this is untested):
def define_binning(names, options = {}, &block)
Array(names).each do |name|
define_params(name, options.dup, &block)
end
end
That forces names to an array, then for each name it calls the define_params method with the same options/block.
options.dup is used in case the underlying method does something like options.delete(:blah) and this ensures that each call definitely gets the same set of options passed to it.

Related

How to reference a class from a string which is part of a variable 3.7

I have just read a text file and extracted a string and stored it as a variable. This string also happens to be the name of a class I want to reference in order to use in a function for example. The "which_class" variable is the whichever class was stored in the file
I tried passing the which_class variable in as a parameter to the function. Removing the quotations seems to make it work but I am unsure how to do this.
class needed_for_func_one():
multiplier = 1.23
class needed_for_func_two():
multiplier = 1.15
def random_function(which_class):
print(123 * which_class.multiplier)
PSEUDO CODE
READ FROM FILE STORE STRING AS "which_class"
which_class = "needed_for_func_two"
random_function(which_class)
But this didn't work it just gave me an attribute error
the eval function could help you here.
random_function(eval(whichClass))
However, you should probably rethink whether you really want to it that way or if there is a much cleaner solution.
I think your question is related to this one
How you call the function depends if it is a global function or if it is inside an object.
globals()['call_this_function']() # this is probably what you need
or
getattr(from_this_object, 'call_this_function')()
first, to use a class you need an object of a class.
so if what you read is a name of the class or any other thing it does not matter, just use an if statement to decide what is inside that variable so-called "which_class".
then create an object like :
if which_class=="needed_for_func_one":
newObject = needed_for_func_one()
elseif which_class=="needed_for_func_two":
newObject = needed_for_func_two()
then use the print like :
print(123 * newObject.multiplier )

Jmeter - Loop through the array defined from 'User Defined Variables'

I try to Loop scenario for 2 times fetching data from array.
My use case is: I want to create user, for multiple countries, so instead of coping the case, I want to implement loop logic.
So far a tried like, but without success:
from here I want to fetch data
//JSR223 PreProcessor
String[] varArray = {"US", "UK"};
idx = Integer.parseInt(vars.get("loopCounter"))-1;
vars.put("myVariable", varArray[idx]);
counter logic:
Whole script, which is chain of multiple API calls, and i want to use fetch data, only a single call.
Result:
I got null, if I try to use fetch variable.
Script37.groovy: 1: unexpected token: US # line 1, column 22.
String[] varArray = {"US", "UK"};
Define countries variable via User Defined Variables like:
In the Loop Controller you can dynamically get the array length via __groovy() function as:
${__groovy(vars.get('countries').split().size(),)}
Wherever you want inside the Loop Controller you can reference the "current" country for the given loop as:
${__groovy(vars.get('countries').split()[vars.get('__jm__Loop Controller__idx') as int],)}
You need to use square brackets for java array when using Apache Groovy:
String[] varArray = ["US", "UK"];
If you were using Beanshell/Java scripting language, you code will work
String[] varArray = {"US", "UK"};

Groovy Closure reuse vs rehydrate copy

In the DSL page of groovy they show this
def email(Closure cl) {
def email = new EmailSpec()
def code = cl.rehydrate(email, this, this)
code.resolveStrategy = Closure.DELEGATE_ONLY
code()
}
Why are they calling rehydrate instead of just assigning the delegate to the closure:
def email(Closure cl) {
def email = new EmailSpec()
cl.delegate = email
cl.resolveStrategy = Closure.DELEGATE_ONLY
cl()
}
In other words, why do we need a copy of the closure instead of reusing the one given. I don't necessarily see a problem with using rehydrate but I also don't see the need, which tells me there's something I'm not understanding
I imagine it returns a copy rather than reusing the same closure in order to stay idempotent/safe in case you still need a reference to the old closure.
As #tim_yates mentioned, the rehydrate method sets the delegate, owner, and thisObject, whereas your second example only sets the delegate. It's not that the rehydrate method does anything magical, it's just a convenience method so you don't have to set all three properties individually/line-by-line.
I also believe rehydrate is meant to work with its partner method dehydrate, which returns a copy of the closure with those three fields cleared (allowing rehydrate to easily re-set them).

How to replace construction variables without executing the action in SCons?

I have the following action:
act = SCons.Action.Action('$ACTIONVAR', 'Executing a dummy action')
env['EXTENSION'] = '.err'
env['ACTIONVAR'] = '${SOURCE.filebase}$EXTENSION'
I want to have the value of action var depending on different target and sources.
What I want to achieve could be similar to this:
obj = env.Execute(act('file.o', 'file.c'))
print 'Str: ' + str(obj) #this should print 'file.err'
Is it possible to get the value without executing the action ?
You are searching for the env.subst() method. Please check the MAN page for a description of its exact syntax and functionality.

When using Metatables with Lua, is there a way for me to determine if the value is being set or being retrieved?

I've set up a "psuedo-oop" system inside of my script that lets me better interact with the UserData from my application. My system works remarkably good except that it doesn't pass references, it passes values. Because of this, if I say something like:
World.Body.Parent=World.Body2
Nothing actually happens because it never actually sets the Bodys parent to the other body. Instead, it kind of simplifies to this:
World=World.Body2
(Because World is the parent of the Body so it is returned). Now, if I do something like this, on the other hand:
print(World.Body.Parent.Type)
==> World
Because it correctly got the World object (being the parent of the Body).
Now with this all in mind, is there some way to make sure it is passed more by reference instead of actually sending the object? Any ideas would be appreciated!
Here is the relevant source code that I'm using:
local target=_G
function AddService(service)
Blank=(function(...) return end)
CreateTemporaryIndex=(function(obj)
local env_meta={
__index=(function(this, key)
if obj[key]~=nil and obj[key]~=key then
if type(obj[key]) ~= "userdata" then
return obj[key]
else
local r,i=pcall(function() Blank(obj[key].Type) end)
if r then
return CreateTemporaryIndex(obj[key])
else
return (function(...) local arg={...} table.remove(arg,1) return obj[key](obj,unpack(arg)) end)
end
end
else
local ofObj=obj:Child(key)
if ofObj~=nil then
return CreateTemporaryIndex(ofObj)
end
end
return nil
end)
}
local nRe={}
setmetatable(nRe,env_meta)
return nRe
end)
target[service.Name]=CreateTemporaryIndex(service)
end
AddService(__Environment.World)
AddService(__Environment.Players)
AddService(__Environment.Lighting)
AddService(__Environment.Environment)
The __index metamethod is only called when accessing properties. In order to implement a custom setter you will need to define the __newindex metamethod as well. You can find more info in the section of the Lua manual that I linked to.
That said, if I were you I would think again whether all of this proxy table complication is really needed. For an example of one of the little corner cases that you might have not covered, iterating over your environments with pairs and ipairs will fail unless you add __ipairs and __pairs metamethods as well.

Resources