In scons call a custom function using builder - scons

I have a custom function which I want to call using builder object
def generate_action(target, source, env):
print "TRIDIP:::::::I am in"
return None
Now I created a builder
env['BUILDERS']['generateAction'] = Builder(action = generate_action)
action = env.generateAction()
As you see, I have not pass any arugument and I don't want to pass any argument for which my custom function generate_action() is not called. I want to call the function without any argument.

You have to call the Builder, specifying the result file you depend on as source. You can't leave the "target=" and "source=" parameters out at the same time, because SCons has to know where in the total build graph this step fits in. Your SConscript should look something like (untested, and from the top of my head):
# env = Environment()
env['BUILDERS']['generateAction'] = Builder(action = generate_action)
# creates your other result file
myResult = env.someOtherBuilder('out.foo', 'in.txt')
# calls your generate_action method
action = env.generateAction('dummy.txt', myResult)
Note, how this will always call your "generateAction" Builder, because the "dummy.txt" is never created, so the target is never up-to-date.
Finally, my guess is that this answer won't really help and lead you into more trouble. When people try to call custom methods at build time they're usually using SCons in a wrong way...most of the time because they don't have the correct mental model to understand how SCons works. You might want to read up on some basics in the UserGuide ( http://scons.org/doc/production/HTML/scons-user.html ) or ask your further questions on our User mailing list at scons-users#scons.org (see also http://scons.org/lists.php ).

Related

Optionally use component functions added in VertexAI python SDK

I am using vertex ai's python SDK and it's built on top of Kubeflow pipelines. In it, you supposedly can do this:
train_op = (sklearn_classification_train(
train_data = data_op.outputs['train_out']
).
set_cpu_limit(training_cpu_limit).
set_memory_limit(training_memory_limit).
add_node_selector_constraint(training_node_selector).
set_gpu_limit(training_gpu_limit)
)
where you can add these functions (set_cpu_limit, set_memory_limit, add_node_selector, and set_gpu_limit) onto your component. I've haven't used this syntax before.
How I can optionally use each 'sub function' only if the variables are specified each function?
For example, if training_gpu_limit isn't set, I don't want to execute set_gpu_limit on the component.
These functions are not appended to the function, but to the component.
In your code, if you do print(type(train_op)) it will be an object type component. So there is no way to add parameters to the function which will influence behavior of the component.
You can do it in pipeline function by changing code to:
train_op = sklearn_classification_train(train_data = data_op.outputs['train_out'])
if training_cpu_limit:
train_op.set_cpu_limit(training_cpu_limit)
# etc

Building a good class method

I've built a class to ask a user a question, based on a type.
class Question:
def __init__(self, subject):
self.subject = subject
self.question = f"Enter the {subject} to be created. You may end this by typing 'DONE':\n"
self.still_needed = True
def ask_question(self):
ans_list = []
running = True
while running:
var = input(f"Enter {self.subject}?\n")
if var.lower() == 'done':
running = False
else:
ans_list.append(var)
return ans_list
The idea is to have a question model, to create lists of items.
This seems to work well with the following code in main.
roles = Question(subject="role").ask_question()
This creates a list from the Queue Class and uses it's method ask question to generate the list. As far as I can tell the object is then destroyed, as it's not saved to a variable.
My question, being new to Python and OOP is, does this seem like a solid and non-confusing way, or should I refractor? If so, what does the community suggest?
MY OPINION
I guess it depends on you. For one, one of the main purposes of using a class is to create an instance with it later on. Classes are objects ,or "categories" as I like to call them, that you use when there are distinctive types of instances in your project.
Given your code snippet, I can't really suggest anything, I don't know the usage of self.question and self.still_needed. However, if I were to base my opinion on just this part: roles = Question(subject="role").ask_question(), then I'd definitely go with using a function instead. As you've said,
As far as I can tell the object is then destroyed, as it's not saved
to a variable.
ALTERNATIVE SOLUTION
Use decorators → the one with # symbol
In this case, #staticmethod is the way to go!
What are staticmethods? The staticmethod decorator is a way to create a function in a class. So instead of it becoming a method, it can be treated as a function (without self parameter). This also means that a static method bounds to the class rather than its object. Consequently, static methods do not depend on objects (hence, you don't need to create an object for you to use it). Example:
class SomeMathStuff():
#staticmethod
def AddSomeNumbers(iterable):
return sum(iterable)
result = SomeMathStuff.AddSomeNumbers([1, 2, 3])
# result = 6
As you can see, I did not need to create an object, instead I just needed to call its class to use it. Word of warning, most Python programmers argue that this is the un-Pythonic way, but I wouldn't worry too much about it. Hell, even I use these methods sometimes. In my defense, this is a good and efficient way to organize your project. With this, you can apply class methods globally and you can "categorize" them in certain classes you find suitable.
Anyway, this is all I have! I apologize if I misinformed you in any way.
ADDITIONAL INFROMATION ... in case I wasn't the best teacher
https://www.programiz.com/python-programming/methods/built-in/staticmethod
Difference between staticmethod and classmethod
https://softwareengineering.stackexchange.com/questions/171296/staticmethod-vs-module-level-function

Get all classes from a package

I'd like a (platform independent) way to list all classes from a package.
A possible way would be get a list of all classes known by Haxe, then filtering through it.
I made a macro to help with just this. It's in the compiletime haxelib.
haxelib install compiletime
And then add it to your build (hxml file):
-lib compiletime
And then use it to import your classes:
// All classes in this package, including sub packages.
var testcases = CompileTime.getAllClasses("my.package");
// All classes in this package, not including sub packages.
var testcases = CompileTime.getAllClasses("my.package",false);
// All classes that extend (or implement) TestCase
var testcases = CompileTime.getAllClasses(TestCase);
// All classes in a package that extend TestCase
var testcases = CompileTime.getAllClasses("my.package",TestCase);
And then you can iterate over them:
for ( testClass in testcases ) {
var test = Type.createInstance( testClass, [] );
}
Please note, if you never have "import some.package.SomeClass" in your code, that class will never be included, because of dead code elimination. So if you want to make sure it gets included, even if you never explicitly call it in your code, you can do something like this:
CompileTime.importPackage( "mygame.levels" );
CompileTime.getAllClasses( "mygame.levels", GameLevel );
How it works
CompileTime.getAllClasses is a macro, and what it does is:
Waits until compilation is finished, and we know all of the types / classes in our app.
Go through each one, and see if it is in the specified package
See also if it extends (or implements) the specified class/interface
If it does, add the class name to some special metadata - #classLists(...) metadata on the CompileTimeClassList file, containing the names of all the matching classes.
At runtime, use the metadata, together with Type.resolveClass(...) to create a list of all matching types.
This is one way to store and retrieve the information: https://gist.github.com/back2dos/c9410ed3ed476ecc1007
Beyond that you could use haxe -xml to get the type information you want, then transform it as needed (use the parser from haxe.rtti to handle the data) and embed the JSON encoded result with -resource theinfo.json (accessed through haxe.Resource).
As a side note: there are chances you'll be better off not having any automation and just add the classes to an array manually. Imagine you have somepackage.ClassA, somepackage.ClassB, ... then you can do
import somepackage.*;
//...
static var CLASSES:Array<Class<Dynamic>> = [ClassA, ClassB, ...];
It gives you more flexibility as whatever you want to do, you can always add 3rd party classes, which may not necessarily be in the same package and you can also choose to not use a class without having to delete it.

In Visualization Toolkit, which types of objects need Update() and Modified() to be called and when?

I'm looking at some VTK code which may not be working correctly. Here's a snippet:
vtkSmartPointer<vtkCamera> cam = vtkSmartPointer<vtkCamera>::New();
cam->SetFocalPoint(0, 0, 0);
cam->SetViewUp(perp[0], perp[1], perp[2]);
cam->SetPosition(first_cam_pos);
cam->SetViewAngle(20);
cam->Modified();
It seems to me that the call to Modified() shouldn't be necessary, that calling the four Set functions should automatically signal that the camera has been modified.
Indeed, the Kitware VTK camera example doesn't use Modified() for the camera.
vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
camera->SetPosition(0, 0, 20);
camera->SetFocalPoint(0, 0, 0);
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->SetActiveCamera(camera);
In other cases, the potentially not-working VTK code I'm looking at uses Update() to manually update — not for the camera object, but elsewhere. Again, I think this is probably not necessary; but clearly Update() and Modified() are there for some reason.
Is there some rule for determining when Modified() and Update() need to be called and when they don't? Are there certain types of objects that need them and certain types that don't? Or is it related to the types of functions that are called on them?
I'm using VTK 6.1, but I'd love to get a general answer if there's some historical context here.
Update() is required when you want to use an object before the pipeline updates it for you. An example is:
vtkSmartPointer<vtkXMLPolyDataReader> reader = \
vtkSmartPointer<vtkPolyDataReader>::New();
reader->SetFileName("myfile.vtp");
// At this point, the reader hasn't yet read the file, so the
// following line with result in polydata being null (or
// something like that)
vtkPolyData* badPolydata = reader->GetOutput();
// However, once you tell the reader "update right now, don't wait
// for the pipeline to update you" with:
reader->Update();
// you can now get access to the data it has read:
vtkPolyData* goodPolydata = reader->GetOutput();
If, on the other hand, you are going to take the reader, attach it to a mapper, attach the mapper to an actor, and display the actor in the renderwindow, then at the time when the renderer says "Ok, now I need the data that drives this whole chain", the pipeline will go back and call Update() on the reader. This is the whole reason for/benefit of the pipeline execution model.
Modified() is required when you want to inform the pipeline "on the next pass, you need to re-process this object". This is done internally by most of the Set* functions, so I guess you just have to look at the implementation to see if Modified() is getting called or not by whatever function you've called that you expect to take effect the next pass through the pipeline.

IDynamicObject implementation ignores multiple property invocations

I've implemented IDynamicObject in C# 4, return a custom MetaObject subclass that does simple property getter/setter dispatch to a Dictionary. Not rocket science.
If I do this:
dynamic foo = new DynamicFoo();
foo.Name = "Joe";
foo.Name = "Fred";
Console.WriteLine(foo.Name);
Then 'Joe' is printed to the console... the second call to the 'Name' setter is never invoked (never steps into my custom dispatcher code at all).
I know the DLR does callsite caching, but I assumed that wouldn't apply here. Anyone know what's going on?
Whatever MetaObject you're returning from (Bind)SetMember will be cached and re-used in this case. You have 2 dynamic sites doing sets. The 1st call will cache the result in an L2 cache which the 2nd site will pick up before asking you to produce a new rule.
So whatever MetaObject you're returning needs to include an expression tree that will update the value. For example it should do something like:
return new MetaObject(
Expression.AssignProperty(this.Expression, value.Expression),
Restrictions.TypeRestriction(this.Expression, this.Value.GetType());

Resources