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 value
error. While:

my $client = Client->Client('127.0.0.1',1982);

Gives me a

Object did not have Inline::Python::Object magic
error.

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?


In reply to Proper usage of Inline::Python by OverlordQ

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.