Re: How to call a Python Class?

2016-05-03 Thread Ben Finney
David Shi via Python-list  writes:

> I found a Python class within an Open Source software.
> I would like to use it in my own Python script.
> I tried to import it, but I got following message.

Your text is mangled in transit. Please post only plain text messages
(avoid HTML or other “rich” content), so the formatting survives.

> from intersection import *Traceback (most recent call last):  File 
> "", line 1, in     from intersection import *ImportError: 
> bad magic number in 'intersection': b'\x03\xf3\r\n'
> Can any one help?

If we can see the exact code you tried, perhaps.

-- 
 \  “Holy hole in a donut, Batman!” —Robin |
  `\   |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to call a Python Class?

2016-05-03 Thread Chris Angelico
On Wed, May 4, 2016 at 8:56 AM, David Shi via Python-list
 wrote:
> I found a Python class within an Open Source software.
> I would like to use it in my own Python script.
> I tried to import it, but I got following message.
> from intersection import *Traceback (most recent call last):  File 
> "", line 1, in from intersection import *ImportError: 
> bad magic number in 'intersection': b'\x03\xf3\r\n'
> Can any one help?
> Regards.

Did you get a .py file, or only a .pyc? Try deleting all .pyc files
that you downloaded, and try again.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


How to call a Python Class?

2016-05-03 Thread David Shi via Python-list
I found a Python class within an Open Source software.
I would like to use it in my own Python script.
I tried to import it, but I got following message.
from intersection import *Traceback (most recent call last):  File 
"", line 1, in     from intersection import *ImportError: 
bad magic number in 'intersection': b'\x03\xf3\r\n'
Can any one help?
Regards.
David
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to create an instance of a python class from C++

2014-03-12 Thread Stefan Behnel
Barry Scott, 11.03.2014 22:37:
> On 5 Mar 2014, at 00:14, Bill wrote:
>> I can't figure out how to create an instance
>> of a python class from 'C++':
> 
> Why not use pycxx from http://sourceforge.net/projects/cxx/?
> 
> This lib does all the heavy lifting for you for both python2 and python3.
> Has docs and examples.

Yes, tool support definitely helps here. I was going to suggest Cython
(also for obvious reasons), where the code that the OP posted would look
like this:

  def RegisterClass(class_decl):
  an = type(class_decl)()
  print(an.description())
  return 0

Clearly substantially simpler than the posted C code (and certainly safer,
faster and more correct) - although that doesn't really help me much with
understanding what the intention of this code is, looks rather weird...

Stefan


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to create an instance of a python class from C++

2014-03-11 Thread Barry Scott
On 5 Mar 2014, at 00:14, Bill  wrote:

> Hello:
> 
> I can't figure out how to create an instance
> of a python class from 'C++':
> 

Why not use pycxx from http://sourceforge.net/projects/cxx/?

This lib does all the heavy lifting for you for both python2 and python3.
Has docs and examples.

Barry
PyCXX maintainer.



> ( I am relatively new to Python so excuse some of
>  the following. )
> 
> In a .py file I create an ABC and then specialize it:
> 
>from MyMod import *
>from abc import ABCMeta, abstractmethod
> 
># Declare an abstract base class.
>class Base(metaclass=ABCMeta):
>"""Base class."""
>@abstractmethod
>def description(self):
>return "From the base class."
> 
># Declare a class that inerits from the base.
>class Derived(Base):
>"""Derived class."""
>def description(self):
>return "From the Derived."
> 
># Register the derived class.
>RegisterClass(Derived)
> 
> Then from 'C++' (my implementation of RegisterClass)
> I try to create an instance
> 
>static PyObject *
>RegisterClass( PyObject *, PyObject *args ) {   // This gets called ok.
> 
>PyObject *class_decl;
>if( ! PyArg_ParseTuple(args, "O", &class_decl) )
>return NULL;
>Py_INCREF(class_decl);
> 
>PyTypeObject *typ = class_decl->ob_type;
> 
>// Tried this.
>// PyObject *an = _PyObject_New(class_decl->ob_type); assert(an);
>// PyObject *desc = PyObject_CallMethod(an,"description",NULL); 
> assert(desc);
> 
>// Tried this.
>// PyObject *an = PyType_GenericNew((PyTypeObject 
> *)class_decl->ob_type, NULL, NULL); assert(an);
>// assert(class_decl); assert(class_decl->ob_type); 
> assert(class_decl->ob_type->tp_new);
> 
>// This returns something.
>assert(class_decl); assert(class_decl->ob_type); 
> assert(class_decl->ob_type->tp_new);
>PyObject *an_inst = 
> class_decl->ob_type->tp_new(class_decl->ob_type,NULL, NULL); assert(an_inst);
>assert(class_decl->ob_type->tp_init);
> 
>// This crashes.
>int ret = class_decl->ob_type->tp_init(an_inst,NULL, NULL); assert(ret 
> == 0);
>// PyObject_CallMethod(an_inst,"__init__",NULL);
>// PyObject *an_inst = PyObject_CallMethod(class_decl,"__new__",NULL); 
> assert(an_inst);
> 
>// Never get here.
>PyObject *desc = PyObject_CallMethod(an_inst,"description",NULL); 
> assert(desc);
>char *cp = _PyUnicode_AsString(desc);
>cerr << "Description:" << cp << endl;
> 
>return PyLong_FromLong(0);
>}
> 
>static PyMethodDef MyModMethods[] = {
>{ "RegisterClass", RegisterClass, METH_VARARGS, "Register class." },
>{  NULL,   NULL,  0, NULL }
>};
> 
>static struct PyModuleDef MyModMod = {
>   PyModuleDef_HEAD_INIT,
>   "MyMod",// name of module
>   NULL,// module documentation, may be NULL
>   -1,
>   MyModMethods,
>   NULL,
>   NULL,
>   NULL,
>   NULL
>};
> 
>PyMODINIT_FUNC
>PyInit_MyMod( void ) {
>PyObject* m = PyModule_Create(&MyModMod);
>if( m == NULL )
>return NULL;
>return m;
>}
> 
>int
>main( int, char ** ) {
> 
>PyImport_AppendInittab( "MyMod", PyInit_MyMod );
> 
>Py_Initialize();
> 
>const char *file_name = "z.py";
>FILE *fp = fopen(file_name,"r");
>if( fp ) {
>PyRun_SimpleFileExFlags(fp,file_name,1,0);
>}
> 
>Py_Finalize();
> 
>return 0;
>}
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to create an instance of a python class from C++

2014-03-05 Thread Gene Heskett
On Wednesday 05 March 2014 17:09:53 Grant Edwards did opine:

> On 2014-03-05, Alister  wrote:
> >>> Why are you creating an ABC?
> >> 
> >> Because it was the first binary computer that did calculations with
> >> electronic switching elements (gates), and it would be really cool to
> >> have one! The ABC also pioneered the use of capciators as
> >> regenerative storage elements (it's how DRAM still works today).
> >> 
> >> http://en.wikipedia.org/wiki/Atanasoff%E2%80%93Berry_Computer
> >> 
> >> It predated ENIAC, and it's clear that some of the features of ENIAC
> >> were inspired by the ABC after John Mauchly visited Iowa State and
> >> saw the ABC.
> > 
> > But it was not programmable
> 
> True.  It had only one program that was hard-wired into it when it was
> built as opposed to the external patch-cords and switches that were
> used on machines like Colossus and ENIAC to alter the wiring.
> 
> > the first programmable electronic computer was 'Colossus' which was
> > developed during WWII but remained classified by the UK govt for many
> > years afterwards
> > 
> > http://en.wikipedia.org/wiki/Colossus_computer

What machine was it that had about 12,000 12AU7 vacuum tubes in it for 
logic?  They had one of those, adapted to read the output of a bed of 
photocells installed in a Harris sheet fed press on the SUI campus in the 
later 1950's.  I saw it running once, grading the test score sheets from 
the Iowa Tests that were being used in lieu of the high price per seat S-B 
IQ test in the Iowa schools.  It was IIRC a somewhat difficult test when 
they threw it at me in the 7th grade a decade earlier, they claimed the 
test score were interchangeable with the S-B scores, but I somehow managed 
a 147 on it at the time.

Cheers, Gene
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page 

NOTICE: Will pay 100 USD for an HP-4815A defective but
complete probe assembly.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to create an instance of a python class from C++

2014-03-05 Thread Grant Edwards
On 2014-03-05, Alister  wrote:
>>
>>> Why are you creating an ABC?
>> 
>> Because it was the first binary computer that did calculations with
>> electronic switching elements (gates), and it would be really cool to
>> have one! The ABC also pioneered the use of capciators as regenerative
>> storage elements (it's how DRAM still works today).
>> 
>> http://en.wikipedia.org/wiki/Atanasoff%E2%80%93Berry_Computer
>> 
>> It predated ENIAC, and it's clear that some of the features of ENIAC
>> were inspired by the ABC after John Mauchly visited Iowa State and saw
>> the ABC.
>
> But it was not programmable

True.  It had only one program that was hard-wired into it when it was
built as opposed to the external patch-cords and switches that were
used on machines like Colossus and ENIAC to alter the wiring.

> the first programmable electronic computer was 'Colossus' which was
> developed during WWII but remained classified by the UK govt for many
> years afterwards 
>
> http://en.wikipedia.org/wiki/Colossus_computer

-- 
Grant Edwards   grant.b.edwardsYow! Hmmm ... A hash-singer
  at   and a cross-eyed guy were
  gmail.comSLEEPING on a deserted
   island, when ...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to create an instance of a python class from C++

2014-03-05 Thread Alister
On Wed, 05 Mar 2014 16:08:00 +, Grant Edwards wrote:

> On 2014-03-05, Ian Kelly  wrote:
>> On Tue, Mar 4, 2014 at 5:14 PM, Bill  wrote:
>>> Hello:
>>>
>>> I can't figure out how to create an instance of a python class from
>>> 'C++':
>>>
>>> ( I am relatively new to Python so excuse some of the following. )
>>>
>>> In a .py file I create an ABC and then specialize it:
>>
>> Why are you creating an ABC?
> 
> Because it was the first binary computer that did calculations with
> electronic switching elements (gates), and it would be really cool to
> have one! The ABC also pioneered the use of capciators as regenerative
> storage elements (it's how DRAM still works today).
> 
> http://en.wikipedia.org/wiki/Atanasoff%E2%80%93Berry_Computer
> 
> It predated ENIAC, and it's clear that some of the features of ENIAC
> were inspired by the ABC after John Mauchly visited Iowa State and saw
> the ABC.

But it was not programmable

the first programmable electronic computer was 'Colossus'
which was developed during WWII but remained classified by the UK govt 
for many years afterwards 

http://en.wikipedia.org/wiki/Colossus_computer





-- 
You are not dead yet.  But watch for further reports.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to create an instance of a python class from C++

2014-03-05 Thread Grant Edwards
On 2014-03-05, Ian Kelly  wrote:
> On Tue, Mar 4, 2014 at 5:14 PM, Bill  wrote:
>> Hello:
>>
>> I can't figure out how to create an instance
>> of a python class from 'C++':
>>
>> ( I am relatively new to Python so excuse some of the following. )
>>
>> In a .py file I create an ABC and then specialize it:
>
> Why are you creating an ABC?

Because it was the first binary computer that did calculations with
electronic switching elements (gates), and it would be really cool to
have one! The ABC also pioneered the use of capciators as regenerative
storage elements (it's how DRAM still works today).

http://en.wikipedia.org/wiki/Atanasoff%E2%80%93Berry_Computer

It predated ENIAC, and it's clear that some of the features of ENIAC
were inspired by the ABC after John Mauchly visited Iowa State and saw
the ABC.

-- 
Grant Edwards   grant.b.edwardsYow! I can't decide which
  at   WRONG TURN to make first!!
  gmail.comI wonder if BOB GUCCIONE
   has these problems!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to create an instance of a python class from C++

2014-03-05 Thread Bill
> 
> So far, so good.  The object that was passed in was the "Derived"
> class object.  Since you presumably only want class objects to be
> passed in, you might want to check that here using PyType_Check.
>
Yes. Will do.
> 
> > PyTypeObject *typ = class_decl->ob_type;
> 
> In Python, you instantiate a class by calling it.  You should do the 
> same in C, using PyObject_CallFunction.  But as above, note that you
> want to call class_decl, not class_decl->ob_type.
> 

Of course. That works.

Thanks.

Bill
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to create an instance of a python class from C++

2014-03-04 Thread Ian Kelly
On Tue, Mar 4, 2014 at 5:14 PM, Bill  wrote:
> Hello:
>
> I can't figure out how to create an instance
> of a python class from 'C++':
>
> ( I am relatively new to Python so excuse some of
>   the following. )
>
> In a .py file I create an ABC and then specialize it:

Why are you creating an ABC?  Most Python classes do not use them.
Maybe you have a reason for it, but it's irrelevant to what you're
currently trying to do.

> Then from 'C++' (my implementation of RegisterClass)
> I try to create an instance
>
> static PyObject *
> RegisterClass( PyObject *, PyObject *args ) {   // This gets called 
> ok.
>
> PyObject *class_decl;
> if( ! PyArg_ParseTuple(args, "O", &class_decl) )
> return NULL;
> Py_INCREF(class_decl);

So far, so good.  The object that was passed in was the "Derived"
class object.  Since you presumably only want class objects to be
passed in, you might want to check that here using PyType_Check.

> PyTypeObject *typ = class_decl->ob_type;

Okay, now if class_decl is the class object that was passed in, then
class_decl->ob_type is the *type* of that class object -- the
metaclass, which in this case would be ABCMeta.  You probably don't
need this, because you want to instantiate Derived, not ABCMeta.

> // Tried this.
> // PyObject *an = _PyObject_New(class_decl->ob_type); assert(an);
> // PyObject *desc = PyObject_CallMethod(an,"description",NULL); 
> assert(desc);

In Python, you instantiate a class by calling it.  You should do the
same in C, using PyObject_CallFunction.  But as above, note that you
want to call class_decl, not class_decl->ob_type.

PyObject_New doesn't do any initialization and is, I believe, meant to
be used when implementing types in C.
-- 
https://mail.python.org/mailman/listinfo/python-list


How to create an instance of a python class from C++

2014-03-04 Thread Bill
Hello:

I can't figure out how to create an instance
of a python class from 'C++':

( I am relatively new to Python so excuse some of
  the following. )

In a .py file I create an ABC and then specialize it:

from MyMod import *
from abc import ABCMeta, abstractmethod

# Declare an abstract base class.
class Base(metaclass=ABCMeta):
"""Base class."""
@abstractmethod
def description(self):
return "From the base class."

# Declare a class that inerits from the base.
class Derived(Base):
"""Derived class."""
def description(self):
return "From the Derived."

# Register the derived class.
RegisterClass(Derived)

Then from 'C++' (my implementation of RegisterClass)
I try to create an instance

static PyObject *
RegisterClass( PyObject *, PyObject *args ) {   // This gets called ok.

PyObject *class_decl;
if( ! PyArg_ParseTuple(args, "O", &class_decl) )
return NULL;
Py_INCREF(class_decl);

PyTypeObject *typ = class_decl->ob_type;

// Tried this.
// PyObject *an = _PyObject_New(class_decl->ob_type); assert(an);
// PyObject *desc = PyObject_CallMethod(an,"description",NULL); 
assert(desc);

// Tried this.
// PyObject *an = PyType_GenericNew((PyTypeObject 
*)class_decl->ob_type, NULL, NULL); assert(an);
// assert(class_decl); assert(class_decl->ob_type); 
assert(class_decl->ob_type->tp_new);

// This returns something.
assert(class_decl); assert(class_decl->ob_type); 
assert(class_decl->ob_type->tp_new);
PyObject *an_inst = 
class_decl->ob_type->tp_new(class_decl->ob_type,NULL, NULL); assert(an_inst);
assert(class_decl->ob_type->tp_init);

// This crashes.
int ret = class_decl->ob_type->tp_init(an_inst,NULL, NULL); assert(ret 
== 0);
// PyObject_CallMethod(an_inst,"__init__",NULL);
// PyObject *an_inst = PyObject_CallMethod(class_decl,"__new__",NULL); 
assert(an_inst);

// Never get here.
PyObject *desc = PyObject_CallMethod(an_inst,"description",NULL); 
assert(desc);
char *cp = _PyUnicode_AsString(desc);
cerr << "Description:" << cp << endl;

return PyLong_FromLong(0);
}

static PyMethodDef MyModMethods[] = {
{ "RegisterClass", RegisterClass, METH_VARARGS, "Register class." },
{  NULL,   NULL,  0, NULL }
};

static struct PyModuleDef MyModMod = {
   PyModuleDef_HEAD_INIT,
   "MyMod",// name of module
   NULL,// module documentation, may be NULL
   -1,
   MyModMethods,
   NULL,
   NULL,
   NULL,
   NULL
};

PyMODINIT_FUNC
PyInit_MyMod( void ) {
PyObject* m = PyModule_Create(&MyModMod);
if( m == NULL )
return NULL;
return m;
}

int
main( int, char ** ) {

PyImport_AppendInittab( "MyMod", PyInit_MyMod );

Py_Initialize();

const char *file_name = "z.py";
FILE *fp = fopen(file_name,"r");
if( fp ) {
PyRun_SimpleFileExFlags(fp,file_name,1,0);
}

Py_Finalize();

return 0;
}
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A Python class

2012-09-06 Thread Chris Angelico
On Fri, Sep 7, 2012 at 3:30 AM, Terry Reedy  wrote:
> On 9/6/2012 11:08 AM, Yves S. Garret wrote:
>
>> I'd like to know if there are any online Python classes offered
>> online from reliable institutions that you would recommend.
>
> Google 'online programming course python' for taught courses.
> At least 2 of MIT's self-guided OpenCourseWare courses use Python.

Note this subtle distinction. A Python course will (theoretically, at
least!) teach you how to write Python code. A Python class is a
collection of methods and stuff. A trivial and petty distinction,
perhaps, but when you go searching the web, you'll get better results
with the right word.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A Python class

2012-09-06 Thread Terry Reedy

On 9/6/2012 11:08 AM, Yves S. Garret wrote:


I'd like to know if there are any online Python classes offered
online from reliable institutions that you would recommend.


Google 'online programming course python' for taught courses.
At least 2 of MIT's self-guided OpenCourseWare courses use Python.

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2011/

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/

If you wanted to do one of those, you might find a partner by asking 
here. There might be a matchmaking site, but I could not find one.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Instantiate a python class object in C

2012-03-14 Thread Dids
Ok, I have it :)

PyImport_Import , PyModule_GetDict, PyDict_GetItemString and
PyObject_CallObject

Need to take a second look at cython when I have a spare cycle or 2.

Thanks for the the tip :)

A+
Dids,

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Instantiate a python class object in C

2012-03-14 Thread Stefan Behnel
Dids, 14.03.2012 14:46:
> Apologies if this was asked before, I couldn't find anything.
> 
> I have a class defined in a python file:
> for example:
> 
> class demo:
>   [ class definition goes here]
> 
> I'm writing a C extension.
> In the first function, I take an instance of the "demo" class and do
> my magic. It's working, all is good.
> 
> What I can't figure out is how to create a second C function that
> returns a new instance to the "demo" class to python.
> There must a be tutorial somewhere, but I can't find anything. I do
> not want to define a new python class in C.
> 
> Another example:
>This is working:
> demo_obj1 = demo()
> my_C_extension.function_1( demo_obj1 )  //working, all good.
> 
>This I can't figure out how to do:
> new_demo_obj = my_C_extension.function_2()

You should consider giving Cython a try. It will allow you to write normal
Python code for your C extension that it translates to efficient C code.
This is much easier than writing all of this by hand, especially when it
comes to classes. It will also optimise the code for you, so that you'll
often end up with faster code than what you'd manually write.

Stefan

-- 
http://mail.python.org/mailman/listinfo/python-list


Instantiate a python class object in C

2012-03-14 Thread Dids
Hi,

Apologies if this was asked before, I couldn't find anything.

I have a class defined in a python file:
for example:

class demo:
  [ class definition goes here]

I'm writing a C extension.
In the first function, I take an instance of the "demo" class and do
my magic. It's working, all is good.

What I can't figure out is how to create a second C function that
returns a new instance to the "demo" class to python.
There must a be tutorial somewhere, but I can't find anything. I do
not want to define a new python class in C.

Another example:
   This is working:
demo_obj1 = demo()
my_C_extension.function_1( demo_obj1 )  //working, all good.

   This I can't figure out how to do:
new_demo_obj = my_C_extension.function_2()

Thanks for reading,
Dids,
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a C pointer in a Python class instance

2009-10-06 Thread lallous
"Carl Banks"  wrote in message 
news:d50bba1e-b272-4e39-8a58-377531278...@z4g2000prh.googlegroups.com...

On Sep 30, 5:24 am, "lallous"  wrote:

Hello

After using the PyCObject, I cannot pickle the class anymore.
Any simple solution to this problem? (or resorting to __reduce__ is the 
only

solution?)



You can't pickle a CObject, you'd have to create a custom type (one
that implements one of the pickling methods) for that.  Or arrange for
whatever object contains the CObject to pack and unpack it manually.

Out of curiosity, what kind of data you storing in this CObject?
Maybe we can help you choose a better way to handle it at the C level.




I am wrapping a C++ pointer with the python object. That way I can tell with 
which C++ object a given python class instance is associated.


The thing is when developing, I need to pickle but I don't need the C++ 
pointer, so I solved the problem with conditional compilation:

- testing: pickle allowed and "This" is stored in the py object
- production code: no need to pickle and "this" and "pyobject" are bound

Thanks,
Elias 


--
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a C pointer in a Python class instance

2009-09-30 Thread sturlamolden
On 30 Sep, 19:03, Carl Banks  wrote:

> Second, CObjects do not have a __del__ method.  They call the supplied
> constructor from the type's tp_dealloc slot.  Use of the tp_dealloc
> slot does not, by itself, prevent cyclic GC.
>
> Bottom line is, the CObject's deallocator is as reliable as a custom
> type's tp_dealloc.

You are right. I did not look at the PyCObject_* API close enough.

I thought of wrapping the CObject with a Python class, and calling the
destructor from __del__. That would be less reliable.

S.M.






-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a C pointer in a Python class instance

2009-09-30 Thread Carl Banks
On Sep 29, 11:16 am, sturlamolden  wrote:
> On 29 Sep, 19:11, Carl Banks  wrote:
>
> > CObjects can be passed a C function as a deallocator; this should work
> > as reliably as a custom class deallocator.
>
> Except that __del__ prevents cyclic GC.

You are mistaken on two counts.

First of all, a CObject is not a container.  It can't prevent cyclic
GC because it's never a part of a cycle.

Second, CObjects do not have a __del__ method.  They call the supplied
constructor from the type's tp_dealloc slot.  Use of the tp_dealloc
slot does not, by itself, prevent cyclic GC.

Bottom line is, the CObject's deallocator is as reliable as a custom
type's tp_dealloc.


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a C pointer in a Python class instance

2009-09-30 Thread Carl Banks
On Sep 30, 5:24 am, "lallous"  wrote:
> Hello
>
> After using the PyCObject, I cannot pickle the class anymore.
> Any simple solution to this problem? (or resorting to __reduce__ is the only
> solution?)


You can't pickle a CObject, you'd have to create a custom type (one
that implements one of the pickling methods) for that.  Or arrange for
whatever object contains the CObject to pack and unpack it manually.

Out of curiosity, what kind of data you storing in this CObject?
Maybe we can help you choose a better way to handle it at the C level.


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a C pointer in a Python class instance

2009-09-30 Thread lallous

Hello

After using the PyCObject, I cannot pickle the class anymore.
Any simple solution to this problem? (or resorting to __reduce__ is the only 
solution?)


Thanks,
Elias

"Falcolas"  wrote in message 
news:9d3790aa-f7d9-4bb5-a81f-5428b2d60...@v25g2000yqk.googlegroups.com...

On Sep 29, 2:27 am, "lallous"  wrote:

Hello

From my C extension module I want to store a C pointer in a given 
PyObject.


The only way I figure how to do it is to use Py_BuildValues and store the
poiner casted to Py_ssize_t, thus:

Py_BuildValues("n", (Py_ssize_t)my_ptr)

Can it be done differently?

Regards,
Elias


You can use a "PyCObject_FromVoidPtr"

http://docs.python.org/c-api/cobject.html

PyArg_ParseTuple(args, "O", &pyVoidPointer);
castPointer = (type *) PyCObject_AsVoidPtr(pyVoidPointer);
return PyCObject_FromVoidPtr((void *) castPointer, NULL); 


--
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a C pointer in a Python class instance

2009-09-30 Thread lallous

Thanks everyone.

Finally, I used Falcolas suggestion and took into consideration 
sturlamolden's comments.


Regards,
Elias
"lallous"  wrote in message news:h9sgcn$iv...@aioe.org...

Hello

From my C extension module I want to store a C pointer in a given 
PyObject.


The only way I figure how to do it is to use Py_BuildValues and store the 
poiner casted to Py_ssize_t, thus:


Py_BuildValues("n", (Py_ssize_t)my_ptr)

Can it be done differently?

Regards,
Elias 


--
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a C pointer in a Python class instance

2009-09-29 Thread sturlamolden
On 29 Sep, 19:11, Carl Banks  wrote:

> CObjects can be passed a C function as a deallocator; this should work
> as reliably as a custom class deallocator.
>
> Carl Banks

Except that __del__ prevents cyclic GC.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a C pointer in a Python class instance

2009-09-29 Thread Carl Banks
On Sep 29, 9:42 am, sturlamolden  wrote:
> You can use PyCObject, or write your own extension type that wraps the
> pointer (very easy to to with Cython or Pyrex). The advantage of using
> an extension type is you have a guarantee from Python on the
> deallocator method being called (cdef __dealloc__ in Cython).

CObjects can be passed a C function as a deallocator; this should work
as reliably as a custom class deallocator.


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a C pointer in a Python class instance

2009-09-29 Thread sturlamolden
On 29 Sep, 10:27, "lallous"  wrote:
> Hello
>
> From my C extension module I want to store a C pointer in a given PyObject.
>
> The only way I figure how to do it is to use Py_BuildValues and store the
> poiner casted to Py_ssize_t,

Formally, you should cast the pointer to Py_intptr_t, as it has the
same size as void*. Py_ssize_t has the same size as size_t, but the C
standard does not mandate that sizeof(void*) == sizeof(size_t). In
fact there are segment and offset architectures where this is not
true. Casting a pointer to Py_ssize_t accidentally works if you have a
flat address space.


> Can it be done differently?

You can use PyCObject, or write your own extension type that wraps the
pointer (very easy to to with Cython or Pyrex). The advantage of using
an extension type is you have a guarantee from Python on the
deallocator method being called (cdef __dealloc__ in Cython). If the
pointer references a resource that needs to be closed, this is safer
than using a __del__ method in a Python class.








-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a C pointer in a Python class instance

2009-09-29 Thread Falcolas
On Sep 29, 2:27 am, "lallous"  wrote:
> Hello
>
> From my C extension module I want to store a C pointer in a given PyObject.
>
> The only way I figure how to do it is to use Py_BuildValues and store the
> poiner casted to Py_ssize_t, thus:
>
> Py_BuildValues("n", (Py_ssize_t)my_ptr)
>
> Can it be done differently?
>
> Regards,
> Elias

You can use a "PyCObject_FromVoidPtr"

http://docs.python.org/c-api/cobject.html

PyArg_ParseTuple(args, "O", &pyVoidPointer);
castPointer = (type *) PyCObject_AsVoidPtr(pyVoidPointer);
return PyCObject_FromVoidPtr((void *) castPointer, NULL);
-- 
http://mail.python.org/mailman/listinfo/python-list


Storing a C pointer in a Python class instance

2009-09-29 Thread lallous

Hello


From my C extension module I want to store a C pointer in a given PyObject.


The only way I figure how to do it is to use Py_BuildValues and store the 
poiner casted to Py_ssize_t, thus:


Py_BuildValues("n", (Py_ssize_t)my_ptr)

Can it be done differently?

Regards,
Elias 


--
http://mail.python.org/mailman/listinfo/python-list


Re: include a python class in another python script.

2006-08-15 Thread danielx

KraftDiner wrote:
> I have a class that is defined in a file called MyClass.py
>
> How do I use that class in another python script..
> import MyClass ?  (Does it need to be in a specific location?)

MyClass.py has to be on your "python path". Your python path is a list
of directories python will search (in order) when you do an import
statement. By default, your current working directory is the first
place Python will search. You can also customize your python path.
Check out this part of the docs for more info:

http://docs.python.org/inst/search-path.html#search-path

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: include a python class in another python script.

2006-08-15 Thread [EMAIL PROTECTED]
KraftDiner wrote:
> I have a class that is defined in a file called MyClass.py
>
> How do I use that class in another python script..
> import MyClass ?  (Does it need to be in a specific location?)

Same directory as the script that's importing it, or in the PYTHONPATH.

import sys
print sys.path

-- 
http://mail.python.org/mailman/listinfo/python-list


include a python class in another python script.

2006-08-15 Thread KraftDiner
I have a class that is defined in a file called MyClass.py

How do I use that class in another python script..
import MyClass ?  (Does it need to be in a specific location?)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using vim as a python class/module/function etc.. browser

2006-04-11 Thread Chris Jones
Daniel Nogradi wrote:
Of course, modern versions of Exuberant Ctags also support Python, too.
>>>
>>>I apt-installed this package but the man page is rather intimidating so
>>>I thought I might as well make sure I was going in the right direction.
>>
>>You will probably want to read the vim documentation on how to use ctags
>>from
>>vim. That will tell you all you need to know without extraneous cruft.
>>
>>
>>>Just need to verify that the stable version (sarge) is modern enough..
>>
>>It ought to be. It has supported Python for years and years.
> 
> 
> For browsing source code I found the folding feature of vim very
> useful. It collapses the body of function and class definitions into
> one line so you can have a general overview of definitions in the
> code. It is available from version 6 up and I recently wrote a vim
> plugin specifically for folding python source code. You can find it
> here: http://www.vim.org/scripts/script.php?script_id=1494

I'm currently re-evaluating my vim habits .. see what new stuff I can 
integrate so as to be more productive.. so it's probably a good time to 
look into the folding feature...

> 
> There is also an excellent vim plugin by Yegappan Lakshmanan for
> working with 'tags' files using ctags. It displays all your function
> and class definitions (from multiple files if you wish) in a narrow
> vertical window where you can easily jump to the file containing a
> chosen definition. This script is here:
> http://www.vim.org/scripts/script.php?script_id=273

this sounds very useful. Thanks very much for pointing me in the right 
direction.

> 
> HTH,
> Daniel

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using vim as a python class/module/function etc.. browser

2006-04-11 Thread Daniel Nogradi
> >>Of course, modern versions of Exuberant Ctags also support Python, too.
> >
> > I apt-installed this package but the man page is rather intimidating so
> > I thought I might as well make sure I was going in the right direction.
>
> You will probably want to read the vim documentation on how to use ctags
> from
> vim. That will tell you all you need to know without extraneous cruft.
>
> > Just need to verify that the stable version (sarge) is modern enough..
>
> It ought to be. It has supported Python for years and years.

For browsing source code I found the folding feature of vim very
useful. It collapses the body of function and class definitions into
one line so you can have a general overview of definitions in the
code. It is available from version 6 up and I recently wrote a vim
plugin specifically for folding python source code. You can find it
here: http://www.vim.org/scripts/script.php?script_id=1494

There is also an excellent vim plugin by Yegappan Lakshmanan for
working with 'tags' files using ctags. It displays all your function
and class definitions (from multiple files if you wish) in a narrow
vertical window where you can easily jump to the file containing a
chosen definition. This script is here:
http://www.vim.org/scripts/script.php?script_id=273

HTH,
Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using vim as a python class/module/function etc.. browser

2006-04-10 Thread Robert Kern
Chris Jones wrote:
> Robert Kern wrote:

>>Of course, modern versions of Exuberant Ctags also support Python, too.
> 
> I apt-installed this package but the man page is rather intimidating so 
> I thought I might as well make sure I was going in the right direction.

You will probably want to read the vim documentation on how to use ctags from
vim. That will tell you all you need to know without extraneous cruft.

> Just need to verify that the stable version (sarge) is modern enough..

It ought to be. It has supported Python for years and years.

-- 
Robert Kern
[EMAIL PROTECTED]

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using vim as a python class/module/function etc.. browser

2006-04-10 Thread Chris Jones
Robert Kern wrote:
> Chris Jones wrote:
> 
>>I'm trying to make sense of a python program and was wondering if vim 
>>has any python-oriented functionalities (apart from syntax highlighting) 
>>that would make it somewhat easier to browse the source code.
>>
>>What I had in mind is something that would let me use CTRL+] to 
>>automatically display whatever object is under the cursor (a bit like 
>>ctags for code written in C..)
>>
>>I have read somewhere about something called 'ptags' but could not find 
>>it in debian - and I'm not 100% sure it's really a python equivalent of 
>>ctags.
> 
> 
> On ptags:
> http://www.vim.org/tips/tip.php?tip_id=1188
> 
> Of course, modern versions of Exuberant Ctags also support Python, too.
> 

I apt-installed this package but the man page is rather intimidating so 
I thought I might as well make sure I was going in the right direction.

Just need to verify that the stable version (sarge) is modern enough..

Thanks..!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using vim as a python class/module/function etc.. browser

2006-04-10 Thread Robert Kern
Chris Jones wrote:
> I'm trying to make sense of a python program and was wondering if vim 
> has any python-oriented functionalities (apart from syntax highlighting) 
> that would make it somewhat easier to browse the source code.
> 
> What I had in mind is something that would let me use CTRL+] to 
> automatically display whatever object is under the cursor (a bit like 
> ctags for code written in C..)
> 
> I have read somewhere about something called 'ptags' but could not find 
> it in debian - and I'm not 100% sure it's really a python equivalent of 
> ctags.

On ptags:
http://www.vim.org/tips/tip.php?tip_id=1188

Of course, modern versions of Exuberant Ctags also support Python, too.

-- 
Robert Kern
[EMAIL PROTECTED]

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

-- 
http://mail.python.org/mailman/listinfo/python-list


using vim as a python class/module/function etc.. browser

2006-04-10 Thread Chris Jones
I'm trying to make sense of a python program and was wondering if vim 
has any python-oriented functionalities (apart from syntax highlighting) 
that would make it somewhat easier to browse the source code.

What I had in mind is something that would let me use CTRL+] to 
automatically display whatever object is under the cursor (a bit like 
ctags for code written in C..)

I have read somewhere about something called 'ptags' but could not find 
it in debian - and I'm not 100% sure it's really a python equivalent of 
ctags.

I'm not too keen on using a gui IDE and would much prefer to stick with 
vim if at all possible.

Any pointers or tips from python/vim folks welcome.

CJ
-- 
http://mail.python.org/mailman/listinfo/python-list