Non hashable object (without __dict__)

2011-04-20 Thread Arthur Mc Coy
Hello,


I have a C++ application, I used SWIG to call the python code. I pass
myModule.myObject object to the method of python code and what to
store it in JSON format.

The problem is when I do:

o = myModule.myObject()


inside python, the o object has __dict__ property, but if I take the
passed object o like:

def myMethod(self, objects):
for o in objects:
print o.__dict__


it fails saying no SwigPyObject does not have __dict__ property. The
only way to use o properties is to call it by name like here:

print o.MyProperty


But in order to use json.dump( objects, file, default =
self.MyJSONEncode ) I MUST to pass to MyJSONEncode method an object
that has __dict__ as its property.



So, what is the best way to correct my objects, which are coming from C
++ code to make them available for putting in JSON format ?


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


How to get python class serializable and well parsed by JSON

2011-03-11 Thread Arthur Mc Coy
Hi people,

I've created very nice post here:
http://stackoverflow.com/questions/5274690/how-to-get-python-class-serializable-and-well-parsed-by-json-custom-encoder-deco

Yes, there are solutions over the internet, but nothing works for my
custom types.
If you could answer, please read it.

Be happy!
Arthur
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get python class serializable and well parsed by JSON

2011-03-11 Thread Arthur Mc Coy
Common guys, help me, sweet heart to finish my job and go with my
friend in the bar. Today we have a blues night :) Love it!

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


Re: How to get python class serializable and well parsed by JSON

2011-03-11 Thread Arthur Mc Coy
Currently trying to pick up my ava http://en.gravatar.com/christinasanders21
for StackOverflow, which site I found very good for my questions,
didn't I?
Yes, and there are problem when adding a picture. Web admins should be
careful.
Can anyone explain me my problem ? :

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


c++ data types in python script

2011-03-06 Thread Arthur Mc Coy
Hi people,

I've used SWIG module to embed python inside c++ app. I pass a list of
objects (with lots of different properties of types string, float,
custom types like URL, Software and finally of list of strings).

Now I'm in python. URL and Software has str() method that converts
their value to string recognizable by JSON. But the problem is with
list of strings.

So, as I said I passed std::liststd::string myObjects to python
function.
Then I iterate it and for each object in myObjects I create a python
copy (serialize it) to be able to put into JSON format and store in
appropriate file.

object has property benchmarks of type liststring.
I do:
...
class PythonObject:
def __init__(self, object):
self.benchmarks = list()
for s in object.benchmarks:
self.benchmarks.append(s)
...
and it fails, also I do:
...
class PythonObject:
def __init__(self, object):
self.benchmarks = [unicode(s) for s in object.benchmarks]
...
and it fails, also I do:
...
class PythonObject:
def __init__(self, object):
for s in object.benchmarks:
print s[0] + s[1] + s[2]
print type(s)
...
and it fails printing
   wor
   type 'str'
   Segmentation fault (core dumped)
$
also I do:
...
class PythonObject:
def __init__(self, object):
self.benchmarks = unicode(object.benchmarks)
...
and it does not fail, instead it puts in JSON this string:
...
benchmarks: mymodule.StringList; proxy of Swig Object of type
'std::list std::string, std::allocator std::string   *' at
0xb63ed4e8,
...
but it is not what I need

What I'm trying to stress is that c++ objects should be converted
(serialized) before putting them into json. Otherwise type errors
occur and process fails.


I love learning python and hope somebody may suggest me or tell
something.

Thank you all anyway!

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


Re: c++ data types in python script

2011-03-06 Thread Arthur Mc Coy
Great!

The solution is to use self.benchmarks = list(object.benchmarks).

Now I'm battling with time_t type. C++ time_t converts to python int
but it causes memore leaks due to destructor absence. I'm trying to
figure it out. If anyone know, please share your thoughts.

Be happy :)

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


python data types in c++ code

2011-03-06 Thread Arthur Mc Coy
Hi all,


Ok, I managed to work with c++ data types in python and can store
serialize c++ objects to store in json.

Now the task is backward. I wrote a c++ code to get the list of
objects using again python interface. The list of objects is returned.
PyList.

But I can't see how to convert PyObject as element of PyList to c++
data type or even how to convert or iterate PyList which is PyObject
too.


Please, whisper me the proper way I will go for.
Be happy :)

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


Re: python data types in c++ code

2011-03-06 Thread Arthur Mc Coy
Ok people,


I do:
for (Py_ssize_t i = 0; i PyList_Size(py_list); ++i) {
 PuObject* obj = PyList_GetItem(py_list, i);
 if (obj != NULL) {
   // howto get obj properties of different types
(map, list, string, int, bool) ?
 }
}

Very clear code. The question is in the comment :)
Let's play more,
it's like jazz,
let's explore,
Baby baby baby bass


Keep yourselves,
Arthur
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python data types in c++ code

2011-03-06 Thread Arthur Mc Coy
Stefan, great suggestion !!

I will definitely bookmark this page and consider later. But my
project dictate me use SWIG. I'm almost near the happy subend. Very
curios on question I asked in previous message.

Dear all, I love you too much :)

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


Re: python data types in c++ code

2011-03-06 Thread Arthur Mc Coy
Stephan, you are lead developer over there :))) It's marketing,
however very nice solution.. I will propose my bosses to rediscover
their world assumptions. You know, they are still using SVN, they are
very loosely coupled to the past. I have to wash their brains...

Meantime still battling with PyObject properties which are so diverse
and disperse that I don't know...


Keep talking guys


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


Re: python data types in c++ code

2011-03-06 Thread Arthur Mc Coy
In short, the answer is to use PyObject dictionary ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python simplejson decoding

2011-03-04 Thread Arthur Mc Coy
Good day people,


So I have python file which can handle json data to put and get back
it from a file say objects.json. Great.

Now I want to run this code from within C++ application. I used swig
to wrap the C++ class, which wants to call python code. It works fine,
because when I import native python modules like simplejson or os.path
the operation is successfull.

But how to import my custom python code (not native builtin modules) ?
There are following ways:
- import as python module
- call python file and its functions

I prefer second way because I do not know how to define my custom
module.

If you know some examples, let me know please. Yes, I'm googling and
reading off docs, but have not yet understood them.


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


import python module from C++ code

2011-03-04 Thread Arthur Mc Coy
Hi all,


I have a C++ application. I have a .cpp file which is not a main
program, but a class where I want to call python script
(doSomething.py file).

I'm using embed python like in a tutorial here:
http://www.codeproject.com/KB/cpp/embedpython_1.aspx

But the tutorial is bad. It does not explain howto create python
module which they call in their example. doSomething.py file contains
two classes, one of them I use externally (its functions).

So I need to PyImport_Import(py_module) by name of this py file, but
when I try to do that it fails. Please, give me some examples.


If needed, I can attach my code as well.
Thank you, waiting for anybody's response!
Be happy.

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


Re: import python module from C++ code

2011-03-04 Thread Arthur Mc Coy
Yes, I did. Here the link
http://docs.python.org/extending/extending.html#providing-a-c-api-for-an-extension-module

It does not cover .py file embeding. So it is not my case.

On Fri, Mar 4, 2011 at 7:03 PM, Santoso Wijaya santoso.wij...@gmail.comwrote:

 Have you read the doc [1] on extending/embedding Python?

 ~/santa

 [1] http://docs.python.org/extending/

 On Fri, Mar 4, 2011 at 8:27 AM, Arthur Mc Coy 1984docmc...@gmail.comwrote:

 Hi all,


 I have a C++ application. I have a .cpp file which is not a main
 program, but a class where I want to call python script
 (doSomething.py file).

 I'm using embed python like in a tutorial here:
 http://www.codeproject.com/KB/cpp/embedpython_1.aspx

 But the tutorial is bad. It does not explain howto create python
 module which they call in their example. doSomething.py file contains
 two classes, one of them I use externally (its functions).

 So I need to PyImport_Import(py_module) by name of this py file, but
 when I try to do that it fails. Please, give me some examples.


 If needed, I can attach my code as well.
 Thank you, waiting for anybody's response!
 Be happy.

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



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


Re: import python module from C++ code

2011-03-04 Thread Arthur Mc Coy
The problem is how to write python module under SWIG for C++
application

Nor SWIG documentation neither embeding python documentation does not
answer to this.


If you can help, please, share your idea.

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


Re: import python module from C++ code

2011-03-04 Thread Arthur Mc Coy
Using simple words: I have mymodule.py file and c++ application.
mymodule.py file contains two classes A and B. A class is a custom data
type. B class contains functions putJSON, getJSON which I want to run from
within C++ code. These functions are members of class B. So I need to import
mymodule.py inside C++ app and call its members.

Documentation does not explain this or it is written in a way that hard to
catch out.

I guess SWIG can help me to create a python module interface, but I can't do
that since SWIG documentation is also poor in my case.

Thank you for any help

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


Re: import python module from C++ code

2011-03-04 Thread Arthur Mc Coy
Still need the answer to the question:  howto embed given python file
(which contains python class and its members) into the c++
application ? 

I have to pass the arguments from c++ to python and back so I need to
do conversions. They are ok. Fails PyImport_Import(my_module) call
saying No module called mymodule. Then I need to create a mymodule
based on given python file (its name mymodule.py). Can it be achieved
using SWIG ?

I'm very curious and looking for some feedback if possible :)
Be happy.

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


python simplejson decoding

2011-03-02 Thread Arthur Mc Coy
Hi all,



I'm trying an example (in attached file, I mean the bottom of this
message).

First, I create a list of 3 objects. Then I do:


PutJSONObjects(objects)
objects = GetJSONObjects()
PutJSONObjects(objects, objects2.json)


1) PutJSONObjects(objects) method creates objects.json file (by
default). It works fine.
2) Then objects = GetJSONObjects() method get the file contents and
return.

3) Finally the script fails on the third method
PutJSONObjects(objects, objects2.json)
saying: AttributeError: 'dict' object has no attribute '__dict__'


That is true, because objects returned by GetJSONObjects() is not a
list of objects, but simple string

So here is the question, please, how should I DECODE .json file into
list of python objects so that I will be able to put the copy of these
objects into a new file called objects2.json ?

simplejson docs are hard to follow - without examples.



Please, help me. Be happy!

Arthur



# Python-JSON method for caching my objects.

import simplejson as json
import os.path
from datetime import datetime


def GetJSONObjects():

# determine the json data file path
filename = objects.json
filepath = /home/docmccoy/Documents/ + filename
if os.path.isfile(filepath):
filename = filepath
f = open(filename, 'r')
objects = json.load( f )
print objects
else:
objects = list()

return objects


def PutJSONObjects(objects, filename = objects.json):

# determine the json data file path
filepath = /home/docmccoy/Documents/ + filename
if os.path.isfile(filepath):
filename = filepath

f = open(filename, 'w')
json.dump([o.__dict__ for o in objects], f, indent = 4 * ' ' )


class MyObject:
def __init__(self, ID, url, category, osfamily, createDate):
self.id = ID
self.url = url
self.category = category
self.osfamily = osfamily
self.createDate = createDate


o1 = MyObject(1, http://google.com;, search, linux,
unicode(datetime.now()))
o2 = MyObject(2, http://localhost;, mycomp, None,
unicode(datetime.now()))
o3 = MyObject(3, http://milan.com;, football, windows,
unicode(datetime.now()))

objects = list()
objects.append(o1)
objects.append(o2)
objects.append(o3)

PutJSONObjects(objects)
objects = GetJSONObjects()
PutJSONObjects(objects, objects2.json)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arbitrary precision linear algebra

2011-03-02 Thread Arthur Mc Coy
What do you mean by arbitrary precision ? Each method of calculating
of something has its own precision...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arbitrary precision linear algebra

2011-03-02 Thread Arthur Mc Coy
On Mar 2, 5:26 pm, Ben123 ben.is.loca...@gmail.com wrote:
 On Mar 2, 9:04 am, Arthur Mc Coy 1984docmc...@gmail.com wrote:

  What do you mean by arbitrary precision ? Each method of calculating
  of something has its own precision...

 If you are unfamiliar with arbitrary precision, I'm referring 
 tohttp://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

 Suppose I find the eigenvalues of a matrix and the eigenvalues range
 from 1 to 0.0001. This can be handled by numpy in Python because the
 smallest eigenvalue is larger than then numerical precision of 1E-19.
 However, if the range of eigenvalues is 1 to 1E-40, then I will need
 to increase the precision of all calculations leading up to finding
 the eigenvalues.

 I am working with complex valued matrices and I expect to get real
 eigenvalues back (based on the physics of the system). The precision
 of numpy is apparent from the imaginary component of the eigenvalues I
 find, currently 1E-19 or 1E-20. I need better precision for small
 eigenvalues.

 In case you are curious, the complex-valued matrices are 20x20.

 Thanks

You probably have to change the method of finding eigenvalues.
Which one do you use? Power or algebraic ?
Do you use Gaussian method to simplify matrices ?

Languages can't support infinitely large or small numbers, so try to
multiply the inner variables by 10^n to increase their values if this
will not involve on the method. For example, I did this when was
calculating geometric means of computer benchmarks.
In such way you will be storing the number of zeros as n.

Yes, interesting what are you calculating.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python simplejson decoding

2011-03-02 Thread Arthur Mc Coy
Hi Peter,


I implemented my decoder using your approach. Very positive.

But that is for simple objects. My objects have nested lists. For
example MyObject has property (member) called benchmarks, which is the
list of defined benchmarks. I'm not sure if obj.__dict__.update will
help me to copy nested information. I will see later. Now, when the
testing environment is ready, I go for real world application.


Thank you!

Kostia

On Mar 2, 4:24 pm, Peter Otten __pete...@web.de wrote:
 Arthur Mc Coy wrote:
  Hi all,

  I'm trying an example (in attached file, I mean the bottom of this
  message).

  First, I create a list of 3 objects. Then I do:

  PutJSONObjects(objects)
  objects = GetJSONObjects()
  PutJSONObjects(objects, objects2.json)

  1) PutJSONObjects(objects) method creates objects.json file (by
  default). It works fine.
  2) Then objects = GetJSONObjects() method get the file contents and
  return.

  3) Finally the script fails on the third method
  PutJSONObjects(objects, objects2.json)
  saying: AttributeError: 'dict' object has no attribute '__dict__'

  That is true, because objects returned by GetJSONObjects() is not a
  list of objects, but simple string

  So here is the question, please, how should I DECODE .json file into
  list of python objects so that I will be able to put the copy of these
  objects into a new file called objects2.json ?

  simplejson docs are hard to follow - without examples.

 I suggest that you use json instead which is part of the standard library
 since Python 2.6. The documentation is here:

 http://docs.python.org/library/json.html

 If you know that there are only MyObject instances you need a function to
 construct such a MyObject instance from a dictionary. You can then recreate
 the objects with

 objects = [object_from_dict(d) for d in json.load(f)]

 or, if all dictionaries correspond to MyObject instances

 objects = json.load(f, object_hook=object_from_dict)

 A general implementation for old-style objects (objects that don't derive
 from object) is a bit messy:

 # idea copied from pickle.py
 class Empty:
     pass

 def object_from_dict(d):
     obj = Empty()
     obj.__class__ = MyObject
     obj.__dict__.update((str(k), v) for k, v in d.iteritems()) # *
     return obj

 If you are willing to make MyClass a newstyle class with

 class MyObject(object):
     # ...

 the function can be simplified to

 def object_from_dict(d):
     obj = object.__new__(MyObject)
     obj.__dict__.update((str(k), v) for k, v in d.iteritems()) # *
     return obj

 (*) I don't know if unicode attribute names can do any harm,
 obj.__dict__.update(d) might work as well.

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


Re: arbitrary precision linear algebra

2011-03-02 Thread Arthur Mc Coy
 Are you saying python cares whether I express a number as 0.001 or
 scaled by 10^5 to read 100? If this is the case, I'm still stuck. I
 need the full range of eigenvalues from 1 to 1E-300, so the entire
 range could be scaled by 1E300 but I would still need better precision
 than 1E19

If python package can't compute 1E-300, then you can't use it.
You can try to split your task onto small subtasks, which can be
handled by specific python package.

Then you need to think of the way to do that. Mathematically. If no
way, then you can leave your task alone.
Programming is limiting, that is true.

I have heard about grid computing. You may find scientists working on
grid and ask how do they split their tasks.
But I think they do not an answer as well.

Also google uses its matrix to rank web pages. It computes the maximum
eigenvalue from the matrix which contain near zero entries too. Maybe
you can find how do they store those values.

Sorry, can't help anymore. I also have computing problems which I
can't yet solve :)
-- 
http://mail.python.org/mailman/listinfo/python-list