What happened to types.ClassType in python 3? - class

I have a script where I do some magic stuff to dynamically load a module, and instantiate the first class found in the module. But I can't use types.ClassType anymore in Python 3. What is the correct way to do this now?

I figured it out. It seems that classes are of type "type". Here is an example of how to distinguish between classes and other objects at runtime.
>>> class C: pass
...
>>> type(C)
<class 'type'>
>>> isinstance(C, type)
True
>>> isinstance('string', type)
False

It was used for classic classes. In Python 3 they're gone.
I suppose you could use something like:
issubclass(ClassName, object)

Related

Serialize a function of a class without serializing the class itself

I'm using pyro5, and I want to make a remote object function as a worker class, where a client can ask it to perform various tasks by sending it functions to execute.
For this to work, I need to serialize functions, but the serializers available through Pyro5 does not support serializing functions.
Instead, I intend to use Dill, and somehow send the already serialized function through Pyro5.
However, when trying to serialize a function, I receive errors which say that I can't serialize X, where X is the enclosing class where the function lives. How can I prevent this from happening?
Actually, I was not able to reproduce this in a small script with a class and a function. I am running the dill serializer in a Qt app, not sure if that's the problem?
Alternatively, does anyone have a better idea on how to approach this?
I'm the dill author. There are several ways to approach this that may work for you. It's hard to tell without more information on your class in question, so I'll have to make assumptions.
If assume you are working with a class that you are importing from an installed package, then you can do something like this:
>>> # Mock a unserializable builtin class "Foo"
>>> import ctypes
>>> PyObjectType = ctypes.py_object(lambda :None)
>>> class Foo(PyObjectType.__class__):
... def __init__(self, x):
... self.x = x
... def bar(self, y):
... return y + self.x
...
>>> f = Foo(1)
>>> f.bar(2)
3
>>> # serializing the method fails
>>> # dill.dumps(f.bar) #FAILS
>>> # so we create a wrapping function
>>> bar = lambda x:f.bar(x)
>>>
>>> import dill
>>> dill.dumps(bar)
b'\x80\x03cdill._dill\n_create_function\nq\x00(cdill._dill\n_create_code\nq\x01(K\x01K\x00K\x01K\x03KCC\nt\x00\xa0\x01|\x00\xa1\x01S\x00q\x02N\x85q\x03X\x01\x00\x00\x00fq\x04X\x03\x00\x00\x00barq\x05\x86q\x06X\x01\x00\x00\x00xq\x07\x85q\x08X\x07\x00\x00\x00<stdin>q\tX\x08\x00\x00\x00<lambda>q\nK\x01C\x00q\x0b))tq\x0cRq\rc__builtin__\n__main__\nh\nNNtq\x0eRq\x0f}q\x10}q\x11X\x0f\x00\x00\x00__annotations__q\x12}q\x13s\x86q\x14b.'
This works, as long as the class is available for import wherever you are going to use dill.loads to restore the function.
Or if you need to ship the class as well, you could also create a wrapping class (i.e. a derived class) that includes a __reduce__ method which informs dill how to serialize the class. https://docs.python.org/3/library/pickle.html#object.__reduce__
There are other approaches as well, but it would help to have more information on what you are dealing with.

Using struct from a module inside another module in Julia

I have found similar questions on SO but none of them seem to give an answer that works for my case.
I have a few modules, in one of them I create a mutable struct which I want to be able to use in the others. All files are at the same level:
file_module_A.jl
file_module_B.jl
file_module_C.jl
In file_module_A.jl:
module A
mutable struct MyType
variable
end
end
In file_module_B.jl:
module B
# I need to import MyType here
end
In file_module_C.jl:
module C
# I need to import MyType here
end
I have tried the followings without success:
Using directly: using .A doesn't work
I can't use: include("./file_module_A.jl") in both B and C because when they interact between each other I get the error can't convert from Main.B.A to Main.C.A since include includes a copy of the whole code
Any ideas? Thanks in advance!
You need to use using ..A. using .A means to look for A in the current module (B in the example below), and you need an extra . to step up one module level, to Main if you run the example in the REPL:
module A
mutable struct MyType
variable
end
end
module B
using ..A: MyType
end

Ipyparallel error with directview

When running this code, I have this error :
from ipyparallel import error, AsyncHubResult, DirectView as dv, Reference
#dv.parallel(block=True)
def np_std_par(x):
return np_std(x)
TypeError: unbound method parallel() must be called with
DirectView instance as first argument (got nothing instead)
How can I use the decorator ?
It sounds unclear.
dv, as written in this first code block (and above), is actually not a DirectView.
from ipyparallel import DirectView as dv
print(type(dv))
<class 'traitlets.traitlets.MetaHasTraits'>
DirectView doesn't need to be imported, instead, it should be created from a Client().
import ipyparallel
client = ipyparallel.Client()
dv = client[:]
print(type(dv))
<class 'ipyparallel.client.view.DirectView'>
Now the decorator will perform as expected. (Although, it looks like you might have to import np_std inside your function or use the require decorator, but that's another question altogether, I recommend working through the examples in the docs to become more familiar)

Scala JSR 223 importing types/classes

The following example fails because the definition for Stuff can't be found:
package com.example
import javax.script.ScriptEngineManager
object Driver5 extends App {
case class Stuff(s: String, d: Double)
val e = new ScriptEngineManager().getEngineByName("scala")
println(e.eval("""import Driver5.Stuff; Stuff("Hello", 3.14)"""))
}
I'm unable to find any import statement that allows me to use my own classes inside of the eval statement. Am I doing something wrong? How does one import classes to be used during eval?
EDIT: Clarified example code to elicit more direct answers.
The Scripting engine does not know the context. It surely can't access all the local variables and imports in the script, since they are not available in the classfiles. (Well, variable names may be optionally available as a debug information, but it is virtually impossible to use them for this purpose.)
I am not sure if there is a special API for that. Imports are different across various languages, so bringing an API that should fit them all can be difficult.
You should be able to add the imports to the eval-ed String. I am not sure if there is a better way to do this.

Transitively import foo._ in Scala

I'm using a utility library for dimensional analysis that i'd like to extend with my own units, and I'd like to be able to write
import my.util.units._
in files in my project. My thought was to define
package my.util
object units {
import squants._
[... other definitions ...]
}
and I expected import my.util.units._ to have the same effect as import squants._, plus the other definitions. But it seems importing units._ doesn't end up adding squants._ to the scope.
Is there a way to do this in scala?
We've dealt with this a little bit at work, and we've tried to resolve this a few ways. Here's an example of how we import rabbitmq types throughout scala-amqp:
package com.bostontechnologies
package object amqp {
type RabbitShutdownListener = com.rabbitmq.client.ShutdownListener
type RabbitConnection = com.rabbitmq.client.Connection
type RabbitChannel = com.rabbitmq.client.Channel
type RabbitConsumer = com.rabbitmq.client.Consumer
type RabbitAddress = com.rabbitmq.client.Address
...
}
So now when we import com.bostontechnologies.amqp._ we get access to the rabbitmq types that we've defined. I know it requires quite a bit of duplication, however we've found it to be somewhat useful, especially since it gives us granularity over type names.
Also, you don't need to use a package object, we mainly use it for convenience of automatically importing our types around a package. You could just use a normal object as well.
Imports are not transitive in Java or Scala. Probably the closest you are going to get to what you seek is to create an object (perhaps a package object) with a type definition for each type of interest.