ipython parallel push custom object - parallel-processing

I am unable to send object to direct view workers.
Here is what I want to do:
class Test:
def __init__(self):
self.id = 'ddfdf'
from IPython.parallel import Client
rc = Client()
dv = rc[:]
t = Test()
dv['t'] = t
print dv['t']
NameError: name 't' is not defined
This would work if I try to push pandas object or any of the build in objects.
What is the way to do it with custom object?
I tried:
dv['Test'] = Test
dv['t'] = t
print dv['t']
UnpicklingError: NEWOBJ class argument isn't a type object

For interactively defined classes (in __main__), you do need to push the class definition, or use dill. But even this doesn't appear to work for old-style classes, which is a bug in IPython's handling of old-style classes[1]. This code works fine if you use a new-style class:
class Test(object):
...
instead of on old-style class. Note that old-style classes are not available in Python 3.
It's generally a good idea to always use new-style classes anyway.

Related

Spring Kotlin - Changed object to class and got error saying "classifier does not have a companion object and thus must be initialized"

I changed my code from this
object SomeHelper{}
to this
#Component
class SomeHelper{}
In my test class I wrote something like this:
class SomeHelperTester{
val cut = SomeHelper
//...
}
which used to work fine when SomeHelper was an object, but now the line val cut = SomeHelper is underlined with an error saying Classifier SomeHelper does not have a companion object, and thus must be initialized. How can I make this line of code work?
just add () behind it, like
val cut = SomeHelper()
This is down to the difference between an object (a singleton, with exactly one instance), and a class (of which you can create as many instances as you like).
When you declare a variable with an initialiser (as you do with cut), the initialiser needs to evaluate to an object. When SomeHelper was declared as an object, then it evaluates to the one and only instance, which works fine as an initialiser. But when it's declared as a class, then the class name alone doesn't mean anything.
What you need to do is to create an instance of the class, by calling its constructor. In this case, since you haven't specified a constructor, you get the default one which takes no parameters. So you can simply call it by appending an empty pair of brackets, and use that as the initialiser:
val cut = SomeHelper()

Enum in Modules

I am trying to use enums inside modules but without success:
# module for testing enums
module EE
export EnumTest
#enum EnumTest uu ii dd
end # module
I did importall EE in the repl. After this I checked the presence of the enum values with instances(EE.EnumTest) with the result
(uu::EE.EnumTest = 0, ii::EE.EnumTest = 1, dd::EE.EnumTest = 2)
(as expected).
When I am trying to return/print the value with uu::EE.EnumTest it is just throwing an exception UndefVarError. I don't expect this. How to use the value?
That export statement in your module just makes the names you list available for use outside the module. You only listed EnumTest — the overall type of the enum — and none of the instances.
You can access the instances by fully-qualifying them with the module name (EE.uu) or by adding them to the export list (export EnumTest, uu, ii, dd) and using EE. Note that importall isn't needed here and is deprecated in favor of using in cases like these in 0.7.

Swift: error: use of instance member on type

I'm trying to build a swift script but I got stuck with this error:
./1.swift:10:11: error: use of instance member 'thisIsmyFunction' on type 'myScript'; did you mean to use a value of type 'myScript' instead?
myScript.thisIsmyFunction()
~~~~~~~~ ^
Here is my code:
#!/usr/bin/swift
import Foundation
class myScript {
func thisIsmyFunction() {
print("Do something in there!")
}
}
myScript.thisIsmyFunction()
What I'm trying to do is access to the function and execute the print.
Any of you knows what I'm doing wrong?
I'll really appreciate your help.
You can only call an instance method on an instance of a class. For example, you would have to create an instance of myScript, then call it:
let script = myScript()
script.thisIsmyFunction()
You can also choose to make thisIsmyFunction a class method (which is formally known as a "type method" in Swift), and call it like the way you are doing right now:
class func thisIsmyFunction() {...}
Note the class modifier in front of func. Of course, this means you can't access self inside the function, because there is no longer an instance of the class.
For more information, see the Swift documentation on methods.
Aside: Swift classes should start with capital letters.

nashorn replace Java.type with binding

To invoke Java from JS you can use Java.type. Is there a way to bind a java class in the Bindings?
So replace:
scriptEngine.eval("Java.type('my.own.AwesomeObj')");
with something like:
Bindings bindings = new SimpleBindings();
bindings.put("AwesomeObj", my.own.AwesomeObj.class);
scriptEngine.setBindings(bingings, ScriptContext.GLOBAL_SCOPE);
I am working on a framework where I want to make a lot of classes available for the js scripts, and preferably not use a string concatenation and an eval. Currently it throws an exception with message: AwesomeObj is not a function, what makes sense.
Nashorn distinguishes a type from a java.lang.Class instance, just like Java does (in Java language, my.own.AwesomeObj is a type, while my.own.AwesomeObj.class is an instance of java.lang.Class. You can use a type to access static members, or as a constructor. You can't use a Class object for that purpose. The bad news is, you can't directly obtain the object that Nashorn uses for representing types; it's an instance of jdk.internal.dynalink.beans.StaticClass and it lives in a restricted package. However, you can evaluate it in script with
engine.eval("Java.type('my.own.AwesomeObj')");
and put the result of that in the bindings. Incidentally, within Nashorn, if you put the Class object into bindings under name AwesomeObjClass, you can use the synthetic property .static to obtain the type, e.g.:
var AwesomeObj = AwesomeObjClass.static;
In this sense, .static on a Class object is the dual of .class on a type object (.static obviously doesn't exist in Java, where types aren't reified as runtime objects).
var stringType = Java.type("java.lang.String");
var stringClass = stringType.class
print(stringClass instanceof java.lang.Class); // true
print(stringType === stringClass.static); // true
Hope this helps.
since Java 9 you can use jdk.dynalink.beans.StaticClass.forClass as it is not internal anymore:
Bindings bindings = engine.createBindings();
bindings.put("AwesomeObj", StaticClass.forClass(my.own.AwesomeObj.class));
then you can utilize the binding in the JS code with :
var awesome = new AwesomeObj();
In your top-level script of your framework do:
var AwesomeObj = Java.type("my.own.AwesomeObj");

PySide: How to override virtual method on object created from .ui file?

I am trying to use QWizard::validateCurrentPage from PySide.
My wizard class is loaded from .ui file using PySide.QtUiTools.QUiLoader
I created a function that supposed to override build-in QWizard::validateCurrentPage:
def validateDataPage(self):
return False
Now I am trying to override build-in method like this:
self.wizard = uiloader.load("registrationwizard.ui")
f = types.MethodType(validateDataPage,
self.wizard,
QtGui.QWizard)
self.wizard.validateCurrentPage = f
I see in debugger that validateCurrentPage is replaced:
self.wizard.validateCurrentPage
<bound method QWizard.validateDataPage of <PySide.QtGui.QWizard object at 0x04CC31C0>>
I can call it from debugger, but it is not called when I click "next" page.
Am I doing something wrong of it is not possible to override virtual functions when object is already constructed?
I'm pretty sure it should work since your way is the canonical way for Adding a Method to an Existing Object in Python.
I made a small example and it seems to work for me.
import types
from PySide import QtGui
def overload(self):
print(self.validateCurrentPage)
return False
app = QtGui.QApplication([])
wizard = QtGui.QWizard()
wizard.validateCurrentPage = types.MethodType(overload, wizard)
wizard.addPage(QtGui.QWizardPage())
wizard.addPage(QtGui.QWizardPage())
wizard.show()
app.exec_()
prints
<bound method QWizard.overload of <PySide.QtGui.QWizard object at xx>>

Resources