OverlordQ has asked for the wisdom of the Perl Monks concerning the following question:
Greetings monks, I'm trying to leverage the use of Inline::Python to make usage of a python module that unfortunately, currently has no Perl equivalent. I (think) I understand the basic usage of importing a Python class, but I can't figure out how to properly reference the imported classes and objects without having to wrap everything.
For example, my current 'working' code is this:
#!/usr/bin/perl use strict; use warnings; my $client = Wrapper->new(); my @res = $client->get('space','key'); use Inline Python => <<'END_OF_PYTHON' import hyperdex.client class Wrapper: def __init__(self, host='127.0.0.1', port=1982): self.client = hyperdex.client.Client(host,port) def get(self, *args): self.client(get(args[0],args[1]) END_OF_PYTHON
It'd be nice if I could just do:
my $client = Client->new('127.0.0.1',1982); $client->get('space','key'); $client->search('space', { a => b, c => d });
Without having to create wrappers for each class method in the Python library I want to utilize.
I've tried:
my $client = new Client('127.0.0.1',1982); use Inline Python => <<'END_OF_PYTHON' import hyperdex.client as Client END_OF_PYTHON
That gives me a
Can't bless non-reference valueerror. While:
my $client = Client->Client('127.0.0.1',1982);
Gives me a
Object did not have Inline::Python::Object magicerror.
Are there any monks out there that could provide some insight to utilizing Inline::Python to DWIM?
EDIT: With some help from AnonyMonk and crashtest, I was able to at least get this to somewhat DWIW:
#!/usr/bin/perl use strict; use warnings; use Inline::Python qw(py_eval py_bind_class); use Data::Dumper; py_eval(<<'END'); from hyperdex.client import Client class MyClient(Client): def placeholder(self): print "Placeholder function" END py_bind_class("main::PerlClient","__main__","MyClient"); my $a = new PerlClient('127.0.0.1',1982); warn Dumper($a->get('space','1234567'));
It's less wrapping then before, but still seems like more wrapped than should be needed. Or I may be wrong?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Proper usage of Inline::Python
by Anonymous Monk on Oct 11, 2014 at 03:08 UTC | |
by OverlordQ (Hermit) on Oct 12, 2014 at 09:59 UTC | |
|
Re: Proper usage of Inline::Python
by crashtest (Curate) on Oct 11, 2014 at 16:57 UTC | |
by OverlordQ (Hermit) on Oct 12, 2014 at 10:01 UTC | |
by crashtest (Curate) on Oct 12, 2014 at 18:13 UTC | |
by OverlordQ (Hermit) on Oct 14, 2014 at 14:22 UTC |