Python to C++ translation?

2005-07-18 Thread Mangabasi
Hi there,

I need to translate the following code (rather something similar) to
C++.  I have been studying C++ for the last two days but I could not
find an easy way to do the following Python snippet.


class A:
def __init__(self, x):
self.x = x
def methodA():
pass # Ignore the details
class B:
def __init__(self, x):
self.x = x
def methodB():
def methodB():
pass # Ignore the details
class C:
def __init__(self, A, B):
self.A = A
self.B = B

a = A(5)
b = B(5.5)
c = C(a, b)
print c.A.x
print c.B.x
#so far I can do it in C++
#how do I do the following in C++?
d = C(b, a)
print d.A.x
print d.B.x


Basically I want A and B to be instances of different classes if
necessary like the above code.

#include 
class A {
public:
int x;
A( ) {
}
A(int _x) {
x = _x;
}
};

class B {
public:
double x;
B( ) {
}
B(double _x) {
x = _x;
}
};

class C {
public:
A *propA;
B *propB;

C(A &_propA, B &_propB) {
propA = &_propA;
propB = &_propB;
}


};

main( ) {
A a = A(42);
B b = B(23.0);
C c1 = C(a, b);
cout << c1.propA->x << "\n";
cout << c1.propB->x << "\n";
}

How do I make this work for

C c2 = C(b, a)

as well?

Thank you in advance, I know this is somehow offtopic in the Python
group but I would not dare asking this in the C++ groups.

Abusing the civility of this grouply yours...

Peace

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


Establishing socket connections to Synthesis Toolkit

2007-04-23 Thread Mangabasi
Howdy,

I would like to use the Synthesis Toolkit for a demo.  I downloaded
the STK from http://ccrma.stanford.edu/software/stk/index.html.  It
seems very powerful and user friendly.  There are bindings for socket
connections and TCL gui examples.  I would like to get one of the demo
samples work with Python/wxPython.  I am including the TCL code for
the drums demo (this was the shortest one).

Has anybody converted this to wxPython?  If yes, would you be kind
enough to share the code?

If not,

How do I establish a socket connection for localhost and port 2001 in
Python?

And how do I send commands to this connection once it is established?

Thank you in advance,

Mangabasi






# Tcl/Tk Drum GUI for the Synthesis Toolkit (STK)

# Set initial control values
set press 127
set outID "stdout"
set commtype "stdout"

# Turn down the reverb
puts $outID "ControlChange0.0  1  44.0  0.0"

# Configure main window
wm title . "STK Drum Controller"
wm iconname . "drum"
. config -bg black

# Configure "communications" menu
menu .menu -tearoff 0
menu .menu.communication -tearoff 0
.menu add cascade -label "Communication" -menu .menu.communication \
-underline 0
.menu.communication add radio -label "Console" -variable commtype \
-value "stdout" -command { setComm }
.menu.communication add radio -label "Socket" -variable commtype \
-value "socket" -command { setComm }
. configure -menu .menu

# Configure slider
scale .bPressure -from 0 -to 128 -length 100 \
-command {changePress } -variable press\
-orient horizontal -label "Velocity" \
-tickinterval 64 -showvalue true -bg grey66

pack .bPressure -pady 5 -padx 5

# Configure buttons
frame .buttons -bg black
frame .buttons.left -bg black
frame .buttons.right -bg black

button .buttons.left.bass -text Bass  -bg grey66 \
-command { playDrum 36 } -width 7
button .buttons.left.snare -text Snare  -bg grey66 \
-command { playDrum 38 } -width 7
button .buttons.left.tomlo -text LoTom  -bg grey66 \
-command { playDrum 41 } -width 7
button .buttons.left.tommid -text MidTom  -bg grey66 \
-command { playDrum 45 } -width 7
button .buttons.left.tomhi -text HiTom  -bg grey66 \
-command { playDrum 50 } -width 7
button .buttons.left.homer -text Homer -bg grey66 \
-command { playDrum 90 } -width 7
button .buttons.right.hat -text Hat  -bg grey66 \
-command { playDrum 42 } -width 7
button .buttons.right.ride -text Ride -bg grey66 \
-command { playDrum 46 } -width 7
button .buttons.right.crash -text Crash -bg grey66 \
-command { playDrum 49 } -width 7
button .buttons.right.cowbel -text CowBel -bg grey66 \
-command { playDrum 56 } -width 7
button .buttons.right.tamb -text Tamb -bg grey66 \
-command { playDrum 54 } -width 7
button .buttons.right.homer -text Homer -bg grey66 \
-command { playDrum 90 } -width 7

pack .buttons.left.bass -pady 5
pack .buttons.left.snare -pady 5
pack .buttons.left.tomlo -pady 5
pack .buttons.left.tommid -pady 5
pack .buttons.left.tomhi -pady 5
pack .buttons.left.homer -pady 5
pack .buttons.right.hat -pady 5
pack .buttons.right.ride -pady 5
pack .buttons.right.crash -pady 5
pack .buttons.right.cowbel -pady 5
pack .buttons.right.tamb -pady 5
pack .buttons.right.homer -pady 5

pack .buttons.left -side left -pady 5 -padx 5
pack .buttons.right -side right -pady 5 -padx 5
pack .buttons -pady 5 -padx 5

# Configure exit button
button .exit -text "Exit Program" -bg grey66 -command myExit
pack .exit -side bottom -pady 20

#bind all  {
bind .  {
noteOn $pitch $press
}

# Bind an X windows "close" event with the Exit routine
bind .  +myExit

proc myExit {} {
global outID
puts $outID [format "ExitProgram"]
flush $outID
close $outID
exit
}

proc playDrum {value}   {
global press
global outID
puts $outID [format "NoteOn   0.0 1 %i %f" $value $press]
flush $outID
}

proc changePress {value} {
global press
set press $value
}

# Socket connection procedure
set d .socketdialog

proc setComm {} {
global outID
global commtype
global d
if {$commtype == "stdout"} {
if { [string compare "stdout" $outID] } {
set i [tk_dialog .dialog "Break 
Socket Connection?" {You are
about to break an existing socket connection ... is this what you want
to do?} "" 0 Cancel OK]
switch $i {
0 {set commtype 
"socket"}
1 {close $outID
   set outID

Inheriting from Python list object(type?)

2007-05-23 Thread Mangabasi
Howdy,

I would like to create a Point class that lets me use Point instances
like the following example.

>>> p = Point(3, 4)
>>> p.x
3
>>> p.y
4
>>> p.z
1
>>> p[0]
3
>>> p[1]
4
>>> p[1] = 5
>>> p.y
5
>>>

other than the x, y, z attributes, these instances should behave like
regular Python lists.  I have created something like :

class Point:
def __init__(self, x, y, z = 1):
self.list = [x, y, z]

def __repr__(self):
return str(self.list)

def __str__(self):
return str(self.list)

def __getattr__(self, name):
if name == 'x':
return self.list[0]
elif name == 'y':
return self.list[1]
elif name == 'z':
return self.list[2]
else:
return self.__dict__[name]

def __setattr__(self, name, value):
if name == 'x':
self.list[0] = value
elif name == 'y':
self.list[1] = value
elif name == 'z':
self.list[2] = value
else:
self.__dict__[name] = value

def __getitem__(self, key):
 return self.list[key]

def __setitem__(self, key, value):
self.list[key] = value

def __getslice__(self, i, j):
return self.list[i : j]

def __setslice__(self, i, j, s):
self.list[i : j] = s

def __contains__(self, obj):
if obj in self.list:
return True
else:
return False


There must be a way to inherit from the list type without having to
redefine all the methods and attributes that regular lists have.

i.e.

class Point(list):
   ...


Can someone provide an example?

Thanx in advance

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


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Mangabasi
On May 23, 12:47 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
> On 23 May 2007 09:58:36 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > There must be a way to inherit from the list type without having to
> > redefine all the methods and attributes that regular lists have.
>
> Like this:
>
> class Point(list):
> def __init__(self, x, y, z = 1):
> list.__init__(self, [x, y, z])
>
> def __getattr__(self, name):
> if name == 'x': return self[0]
> if name == 'y': return self[1]
> if name == 'z': return self[2]
>
> def __setattr__(self, name, value):
> if name == 'x': self[0] = value
> if name == 'y': self[1] = value
> if name == 'z': self[2] = value
>
> Does that show you what you need?
>
> --
> Jerry

Hi Jerry,

It is very close.  It worked for many operations except when I tried

>>> from numpy import array
>>> p = Point(4,5)
>>> a = array(p)
Traceback (most recent call last):
  File "", line 1, in ?
ValueError: invalid __array_struct__
>>> a = array([4, 5, 1])
>>>


I can define an __array__ method for this to work but I am wondering
if we can make this
behave like a real list?

Thanks for your help.

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


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Mangabasi
On May 23, 12:47 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
> On 23 May 2007 09:58:36 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > There must be a way to inherit from the list type without having to
> > redefine all the methods and attributes that regular lists have.
>
> Like this:
>
> class Point(list):
> def __init__(self, x, y, z = 1):
> list.__init__(self, [x, y, z])
>
> def __getattr__(self, name):
> if name == 'x': return self[0]
> if name == 'y': return self[1]
> if name == 'z': return self[2]
>
> def __setattr__(self, name, value):
> if name == 'x': self[0] = value
> if name == 'y': self[1] = value
> if name == 'z': self[2] = value
>
> Does that show you what you need?
>
> --
> Jerry

Hi Jerry,

It is very close.  It worked for many operations except when I tried

>>> from numpy import array
>>> p = Point(4,5)
>>> a = array(p)
Traceback (most recent call last):
  File "", line 1, in ?
ValueError: invalid __array_struct__
>>> a = array([4, 5, 1])
>>>


I can define an __array__ method for this to work but I am wondering
if we can make this
behave like a real list?

Thanks for your help.

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


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Mangabasi
On May 23, 12:47 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
> On 23 May 2007 09:58:36 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > There must be a way to inherit from the list type without having to
> > redefine all the methods and attributes that regular lists have.
>
> Like this:
>
> class Point(list):
> def __init__(self, x, y, z = 1):
> list.__init__(self, [x, y, z])
>
> def __getattr__(self, name):
> if name == 'x': return self[0]
> if name == 'y': return self[1]
> if name == 'z': return self[2]
>
> def __setattr__(self, name, value):
> if name == 'x': self[0] = value
> if name == 'y': self[1] = value
> if name == 'z': self[2] = value
>
> Does that show you what you need?
>
> --
> Jerry

Hi Jerry,

It is very close.  It worked for many operations except when I tried

>>> from numpy import array
>>> p = Point(4,5)
>>> a = array(p)
Traceback (most recent call last):
  File "", line 1, in ?
ValueError: invalid __array_struct__
>>> a = array([4, 5, 1])
>>>


I can define an __array__ method for this to work but I am wondering
if we can make this
behave like a real list?

Thanks for your help.

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


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Mangabasi
On May 23, 12:47 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
> On 23 May 2007 09:58:36 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > There must be a way to inherit from the list type without having to
> > redefine all the methods and attributes that regular lists have.
>
> Like this:
>
> class Point(list):
> def __init__(self, x, y, z = 1):
> list.__init__(self, [x, y, z])
>
> def __getattr__(self, name):
> if name == 'x': return self[0]
> if name == 'y': return self[1]
> if name == 'z': return self[2]
>
> def __setattr__(self, name, value):
> if name == 'x': self[0] = value
> if name == 'y': self[1] = value
> if name == 'z': self[2] = value
>
> Does that show you what you need?
>
> --
> Jerry

Somebody else emailed me another solution.

This is what he suggested:

class Point(list):
def __init__(self,x,y):
super(Point, self).__init__()
self.x = x
self.y = y

When I modified this to:

class Point(list):
def __init__(self,x,y):
super(Point, self).__init__([x, y])
self.x = x
self.y = y

It worked.


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


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Mangabasi
On May 23, 1:43 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
> On 23 May 2007 11:31:56 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > When I modified this to:
>
> > class Point(list):
> > def __init__(self,x,y):
> > super(Point, self).__init__([x, y])
> > self.x = x
> > self.y = y
>
> > It worked.
>
> Are you sure?
>
> >>> p = Point(10, 20)
> >>> p
> [10, 20]
> >>> p.x
> 10
> >>> p.x = 15
> >>> p
> [10, 20]
> >>> p[0]
> 10
> >>> p.x
> 15
>
> That doesn't look like what you were asking for in the original post.
> I'm afraid I don't know anything about numpy arrays or what special
> attributes an object may need to be put into a numpy array though.
>
> --
> Jerry

You are right.  I did not include the whole story in my last post.
This is the real code I used and so far it worked.  I am still testing
it though.  Toes and fingers crossed!

class Point(list):
def __init__(self, x, y, z = 1):
super(Point, self).__init__([x, y, z])
self.x = x
self.y = y
self.z = z

def __getattr__(self, name):
if name == 'x': return self[0]
if name == 'y': return self[1]
if name == 'z': return self[2]

def __setattr__(self, name, value):
if name == 'x': self[0] = value
if name == 'y': self[1] = value
if name == 'z': self[2] = value

Thanks for the correction.

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


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Mangabasi
On May 23, 1:43 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
> On 23 May 2007 11:31:56 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > When I modified this to:
>
> > class Point(list):
> > def __init__(self,x,y):
> > super(Point, self).__init__([x, y])
> > self.x = x
> > self.y = y
>
> > It worked.
>
> Are you sure?
>
> >>> p = Point(10, 20)
> >>> p
> [10, 20]
> >>> p.x
> 10
> >>> p.x = 15
> >>> p
> [10, 20]
> >>> p[0]
> 10
> >>> p.x
> 15
>
> That doesn't look like what you were asking for in the original post.
> I'm afraid I don't know anything about numpy arrays or what special
> attributes an object may need to be put into a numpy array though.
>
> --
> Jerry

This is the winner:

class Point(list):
def __init__(self, x, y, z = 1):
super(Point, self).__init__([x, y, z])
self.x = x
self.y = y
self.z = z

def __getattr__(self, name):
if name == 'x':
return self[0]
elif name == 'y':
return self[1]
elif name == 'z':
return self[2]
else:
return self.__dict__[name]


def __setattr__(self, name, value):
if name == 'x':
self[0] = value
elif name == 'y':
self[1] = value
elif name == 'z':
self[2] = value
else:
self.__dict__[name] = value

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


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Mangabasi
On May 23, 2:19 pm, Lyosha <[EMAIL PROTECTED]> wrote:
> On May 23, 12:07 pm, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > On May 23, 1:43 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
>
> > > On 23 May 2007 11:31:56 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > > > When I modified this to:
>
> > > > class Point(list):
> > > > def __init__(self,x,y):
> > > > super(Point, self).__init__([x, y])
> > > > self.x = x
> > > > self.y = y
>
> > > > It worked.
>
> > > Are you sure?
>
> > > >>> p = Point(10, 20)
> > > >>> p
> > > [10, 20]
> > > >>> p.x
> > > 10
> > > >>> p.x = 15
> > > >>> p
> > > [10, 20]
> > > >>> p[0]
> > > 10
> > > >>> p.x
> > > 15
>
> > > That doesn't look like what you were asking for in the original post.
> > > I'm afraid I don't know anything about numpy arrays or what special
> > > attributes an object may need to be put into a numpy array though.
>
> > > --
> > > Jerry
>
> > This is the winner:
>
> > class Point(list):
> > def __init__(self, x, y, z = 1):
> > super(Point, self).__init__([x, y, z])
> > self.x = x
> > self.y = y
> > self.z = z
>
> [...]
>
> http://docs.python.org/dev/whatsnew/node3.htmlannounces named tuples
> in python2.6.  This is not what you want since tuples are immutable,
> but you might get some inspiration from their implementation.  Or
> maybe not.

This looks very interesting.  Currently I am using ver. 2.4.  In the
future I will consider this.  Tuples may be better when I have to deal
with a lot of points.  For my test cases lists vs. tuples are not
making a big difference yet.  I have a hunch that I will end up with
tuples though.

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


Re: Inheriting from Python list object(type?)

2007-05-23 Thread Mangabasi
On May 23, 2:24 pm, Lyosha <[EMAIL PROTECTED]> wrote:
> On May 23, 12:19 pm, Lyosha <[EMAIL PROTECTED]> wrote:
>
>
>
> > On May 23, 12:07 pm, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > > On May 23, 1:43 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
>
> > > > On 23 May 2007 11:31:56 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
>
> > > > > When I modified this to:
>
> > > > > class Point(list):
> > > > > def __init__(self,x,y):
> > > > > super(Point, self).__init__([x, y])
> > > > > self.x = x
> > > > > self.y = y
>
> > > > > It worked.
>
> > > > Are you sure?
>
> > > > >>> p = Point(10, 20)
> > > > >>> p
> > > > [10, 20]
> > > > >>> p.x
> > > > 10
> > > > >>> p.x = 15
> > > > >>> p
> > > > [10, 20]
> > > > >>> p[0]
> > > > 10
> > > > >>> p.x
> > > > 15
>
> > > > That doesn't look like what you were asking for in the original post.
> > > > I'm afraid I don't know anything about numpy arrays or what special
> > > > attributes an object may need to be put into a numpy array though.
>
> > > > --
> > > > Jerry
>
> > > This is the winner:
>
> > > class Point(list):
> > > def __init__(self, x, y, z = 1):
> > > super(Point, self).__init__([x, y, z])
> > > self.x = x
> > > self.y = y
> > > self.z = z
>
> > [...]
>
> >http://docs.python.org/dev/whatsnew/node3.htmlannouncesnamed tuples
> > in python2.6.  This is not what you want since tuples are immutable,
> > but you might get some inspiration from their implementation.  Or
> > maybe not.
>
> Dude, google groups suck!  They say "an error has occurred" and the
> message is happily posted.

Tell me about it!  :)

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


Calling Fortran from Python

2007-04-03 Thread Mangabasi
Howdy,

I have been trying to call the following Fortran function from Python
(using Windows XP, Compaq Fortran and Python 2.4).  I tried F2Py,
Pyfort and calldll with no success.

I think I came very close with calldll.  Here is a short summary

Fortran code:

  SUBROUTINE SAMPLE(IERR1,IERR2,AIN,AOUT)
C
C   SIMPLE SAMPLE OF A DLL
C
  !DEC$ATTRIBUTES DLLEXPORT :: SAMPLE
  !DEC$ATTRIBUTES ALIAS:'SAMPLE' :: SAMPLE
  INTEGER,INTENT(OUT) :: IERR1,IERR2
  REAL*8,INTENT(IN) :: AIN(*)
  REAL*8,INTENT(OUT) :: AOUT(*)
C
C *** SET MAXIMUM EXPECTED ELEMENTS OF ARRAY AIN AND AOUT
C
  PARAMETER (MAXNVIN=101,MAXNVOUT=200)
C
C *** SET ERROR INDICATORS TO ZERO
C
  IERR1=0
  IERR2=0
C
C *** GET NUMBER OF INPUT VALUES
C
  NVIN=AIN(1)
C *** IF NUMBER EXCEEDS MAXIMUM EXPECTED SET ERRORS AND RETURN
  IF(NVIN .GT. MAXNVIN) THEN
IERR1=1
IERR2=1
RETURN
  ENDIF
C
C *** SET NUMBER OF OUTPUT VALUES
C
  NVOUT=2*NVIN
C *** IF NUMBER EXCEEDS MAXIMUM EXPECTED SET ERRORS AND RETURN
  IF(NVOUT .GT. MAXNVOUT) THEN
IERR1=1
IERR2=2
RETURN
  ENDIF
C
C *** INITIALIZE AOUT INDEX
C
  JOUT=0
C
C *** COMPUTE TWO OUTPUT VALUES FOR EACH INPUT VALUE
C
  DO I=1,NVIN
JOUT=JOUT+1
AOUT(JOUT)=2.*AIN(I+1)
JOUT=JOUT+1
AOUT(JOUT)=3.*AIN(I+1)
  END DO
  RETURN
  END

compiled it to a dll and called this dll from another Fortran program
with success, so this tells me that dll is OK.

This is how I tried to call it from python 2.4

import calldll
handle = calldll.load_library('c:/sample_dll.dll')
addr = calldll.get_proc_address(handle, 'SAMPLE')
#so far so good,  I got a handle and address
e1 = 0
e2 = 0
ain = [2, 3, 4]
aout = [ ]
calldll.call_foreign_function(addr, 'hhll', 'l',(e1, e2,ain,aout))
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: an integer is required

Has anyone provide a similar example with Pyfort, F2Py or calldll?

Thanks in advance.

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


Re: Calling Fortran from Python

2007-04-04 Thread Mangabasi
Robert,

Thanks for your prompt response.  I think I got a lot closer but no
cigar yet.

This is the output

C:\fortrandll>f2py -c -m sample sample.pyf sample.for
numpy_info:
  FOUND:
define_macros = [('NUMERIC_VERSION', '"\\"24.2\\""')]
include_dirs = ['C:\\Python24\\include']

running build
running config_fc
running build_src
building extension "sample" sources
creating c:\docume~1\fb\locals~1\temp\tmpcosvgv
creating c:\docume~1\fb\locals~1\temp\tmpcosvgv\src
f2py: sample.pyf
Reading fortran codes...
Reading file 'sample.pyf'
Post-processing...
Block: sample
Block: sample
Post-processing (stage 2)...
Building modules...
Building module "sample"...
Constructing wrapper function "sample"...
  ierr1,ierr2,aout = sample(ain)
Wrote C/API module "sample" to file "c:\docume~1\fb
\locals~1\temp\tmpcos
vgv\src/samplemodule.c"
  adding 'c:\docume~1\fb\locals~1\temp\tmpcosvgv\src\fortranobject.c'
to sources
.
  adding 'c:\docume~1\fb\locals~1\temp\tmpcosvgv\src' to include_dirs.
copying C:\python24\lib\site-packages\f2py2e\src\fortranobject.c -> c:
\docume~1\
fb\locals~1\temp\tmpcosvgv\src
copying C:\python24\lib\site-packages\f2py2e\src\fortranobject.h -> c:
\docume~1\
fb\locals~1\temp\tmpcosvgv\src
running build_ext
No module named msvccompiler in scipy_distutils, trying from
distutils..
error: The .NET Framework SDK needs to be installed before building
extensions f
or Python.

I think I have a problem with distutils' msvccompiler.py.  It may be
the MacroExpander in distutils guessing the visual studio path
incorrectly or something related to the registry keys.  Is there a way
to specify the C compiler path to f2py so that it does not rely on the
distutils?  Or maybe I am totally off base here, I don't know.

Any thoughts?


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


Re: Calling Fortran from Python

2007-04-04 Thread Mangabasi
There may be a way to finish this without having to deal with
distutils.
F2py created three files so far

samplemodule.c
fortranobject.h
fortranobject.c

Is there a way to create the sample.pyd from these files?

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


Re: Calling Fortran from Python

2007-04-04 Thread Mangabasi
On Apr 4, 12:22 pm, Robert Kern <[EMAIL PROTECTED]> wrote:
> Mangabasi wrote:
> > Robert,
>
> > Thanks for your prompt response.  I think I got a lot closer but no
> > cigar yet.
>
> > This is the output
>
> > C:\fortrandll>f2py -c -m sample sample.pyf sample.for
> > numpy_info:
> >   FOUND:
> > define_macros = [('NUMERIC_VERSION', '"\\"24.2\\""')]
> > include_dirs = ['C:\\Python24\\include']
>
> > running build
> > running config_fc
> > running build_src
> > building extension "sample" sources
> > creating c:\docume~1\fb\locals~1\temp\tmpcosvgv
> > creating c:\docume~1\fb\locals~1\temp\tmpcosvgv\src
> > f2py: sample.pyf
> > Reading fortran codes...
> > Reading file 'sample.pyf'
> > Post-processing...
> > Block: sample
> > Block: sample
> > Post-processing (stage 2)...
> > Building modules...
> > Building module "sample"...
> > Constructing wrapper function "sample"...
> >   ierr1,ierr2,aout = sample(ain)
> > Wrote C/API module "sample" to file "c:\docume~1\fb
> > \locals~1\temp\tmpcos
> > vgv\src/samplemodule.c"
> >   adding 'c:\docume~1\fb\locals~1\temp\tmpcosvgv\src\fortranobject.c'
> > to sources
> > .
> >   adding 'c:\docume~1\fb\locals~1\temp\tmpcosvgv\src' to include_dirs.
> > copying C:\python24\lib\site-packages\f2py2e\src\fortranobject.c -> c:
> > \docume~1\
> > fb\locals~1\temp\tmpcosvgv\src
> > copying C:\python24\lib\site-packages\f2py2e\src\fortranobject.h -> c:
> > \docume~1\
> > fb\locals~1\temp\tmpcosvgv\src
> > running build_ext
> > No module named msvccompiler in scipy_distutils, trying from
> > distutils..
> > error: The .NET Framework SDK needs to be installed before building
> > extensions f
> > or Python.
>
> > I think I have a problem with distutils' msvccompiler.py.  It may be
> > the MacroExpander in distutils guessing the visual studio path
> > incorrectly or something related to the registry keys.  Is there a way
> > to specify the C compiler path to f2py so that it does not rely on the
> > distutils?  Or maybe I am totally off base here, I don't know.
>
> What C and Fortran compilers are you trying to use? You can look at f2py's 
> help
> for flags that you can use to help control where the compilers get picked up,
> but you can't avoid distutils.
>
> --
> Robert Kern
>
> "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- Hide quoted text -
>
> - Show quoted text -

I am using Visual Studio 6.0 and Compaq Visual Fortran 6.6.

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


Re: Calling Fortran from Python

2007-04-04 Thread Mangabasi
On Apr 4, 4:39 pm, Robert Kern <[EMAIL PROTECTED]> wrote:
> Mangabasi wrote:
> > I am using Visual Studio 6.0 and Compaq Visual Fortran 6.6.
>
> Ah. You can't use VS6 with that version of Python. I believe you need the .NET
> SDK 2003.
>
> You could also use gcc, but I'm not sure if that will work well with Compaq
> Visual Fortran; you might have to use gfortran.
>
>  http://www.develer.com/oss/GccWinBinaries
>
> --
> Robert Kern
>
> "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

Would Python 2.5 work with Visual Studio 6.6?

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


Re: Calling Fortran from Python

2007-04-04 Thread Mangabasi
On Apr 4, 5:48 pm, Robert Kern <[EMAIL PROTECTED]> wrote:
> Mangabasi wrote:
> > Would Python 2.5 work with Visual Studio 6.6?
>
> No.
>
> --
> Robert Kern
>
> "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

I will try the GCC then.  It is a shame that I could not get calldll
to work.  It was very simple to use.  I think I am making a mistake
with the argument types but not sure.

Thanks for your help, it is greatly appreciated.

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


Re: Calling Fortran from Python

2007-04-05 Thread Mangabasi
On Apr 4, 10:10 pm, Lenard Lindstrom <[EMAIL PROTECTED]> wrote:
> Mangabasi wrote:
> > On Apr 4, 5:48 pm, Robert Kern <[EMAIL PROTECTED]> wrote:
> >> Mangabasi wrote:
> >>> Would Python 2.5 work with Visual Studio 6.6?
> >> No.
>
> >> --
> >> Robert Kern
>
> >> "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
>
> > I will try the GCC then.  It is a shame that I could not get calldll
> > to work.  It was very simple to use.  I think I am making a mistake
> > with the argument types but not sure.
>
> > Thanks for your help, it is greatly appreciated.
>
> Did you try ctypes?
>
>  >>> from ctypes import *
>  >>> sample=cdll.sample.sample_
>  >>> sample.restype=None
>  >>> sample.argtypes=[POINTER(c_int), POINTER(c_int), POINTER(c_double),
> POINTER(c_double)]
>  >>> e1 = c_int(0)
>  >>> e2 = c_int(0)
>  >>> ain = (c_double*3)(2.0, 3.0, 4.0)
>  >>> aout = (c_double*4)()
>  >>> sample(e1, e2, ain, aout)
>  >>> aout[:]
> [6.0, 9.0, 8.0, 12.0]
>  >>> e1.value
> 0
>  >>> e2.value
> 0
>
> I compile the SAMPLE example with mingw g77 3.4.5:
>
> g77 -shared -o sample.dll sample.for
>
> I had to take out the "INTENT(OUT)"s because g77 didn't like them. And
> "SAMPLE" became "sample_" in the dll. Also note that argument passing to
> Fortran subroutines is strictly pass-by-reference. Thus the ain pointer.
>
> Lenard Lindstrom- Hide quoted text -
>
> - Show quoted text -

Lenard,

Now I tried it as you suggested.  I did not install G77 yet.  I tried
it with the dll I already had.  Something interesting happened:

>>> from ctypes import *
>>> sample=cdll.sample_dll.SAMPLE
>>> sample.restype=None
>>> sample.argtypes=[POINTER(c_int), POINTER(c_int), POINTER(c_double), 
>>> POINTER(c_double)]
>>> sample.argtypes=[POINTER(c_int), POINTER(c_int), POINTER(c_double), 
>>> POINTER(c_double)]
>>> e1 = c_int(-10)
>>> e2 = c_int(-10)
>>> ain = (c_double*3)(2.0, 3.0, 4.0)
>>> ain = (c_double*3)(2.0, 3.0, 4.0)
>>> aout = (c_double*4)()
>>> aout = (c_double*4)()
>>> sample(e1, e2, ain, aout)
Traceback (most recent call last):
  File "", line 1, in ?
ValueError: Procedure called with not enough arguments (16 bytes
missing) or wrong calling convention
>>> aout[:]
[6.0, 9.0, 8.0, 12.0]

I got an error message and the expected answer!  Any guesses?

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


Re: Calling Fortran from Python

2007-04-05 Thread Mangabasi
On Apr 4, 10:10 pm, Lenard Lindstrom <[EMAIL PROTECTED]> wrote:
> Mangabasi wrote:
> > On Apr 4, 5:48 pm, Robert Kern <[EMAIL PROTECTED]> wrote:
> >> Mangabasi wrote:
> >>> Would Python 2.5 work with Visual Studio 6.6?
> >> No.
>
> >> --
> >> Robert Kern
>
> >> "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
>
> > I will try the GCC then.  It is a shame that I could not get calldll
> > to work.  It was very simple to use.  I think I am making a mistake
> > with the argument types but not sure.
>
> > Thanks for your help, it is greatly appreciated.
>
> Did you try ctypes?
>
>  >>> from ctypes import *
>  >>> sample=cdll.sample.sample_
>  >>> sample.restype=None
>  >>> sample.argtypes=[POINTER(c_int), POINTER(c_int), POINTER(c_double),
> POINTER(c_double)]
>  >>> e1 = c_int(0)
>  >>> e2 = c_int(0)
>  >>> ain = (c_double*3)(2.0, 3.0, 4.0)
>  >>> aout = (c_double*4)()
>  >>> sample(e1, e2, ain, aout)
>  >>> aout[:]
> [6.0, 9.0, 8.0, 12.0]
>  >>> e1.value
> 0
>  >>> e2.value
> 0
>
> I compile the SAMPLE example with mingw g77 3.4.5:
>
> g77 -shared -o sample.dll sample.for
>
> I had to take out the "INTENT(OUT)"s because g77 didn't like them. And
> "SAMPLE" became "sample_" in the dll. Also note that argument passing to
> Fortran subroutines is strictly pass-by-reference. Thus the ain pointer.
>
> Lenard Lindstrom- Hide quoted text -
>
> - Show quoted text -

A little bit of googling solved the problem. instead of

>>> sample = cdll.sample_dll.SAMPLE

I used


>>> sample = windll.sample_dll.SAMPLE

and now it seems to be working without error messages.

Thanks a lot.


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


Re: detecting property modification

2007-12-21 Thread Mangabasi
On Dec 21, 12:40 pm, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
> On Fri, 21 Dec 2007 09:26:36 -0800 (PST), Mangabasi <[EMAIL PROTECTED]> wrote:
> >Howdy,
>
> >I think it is easier to explain my question with a short example:
>
> >class Body:
> >    def __init__(self, pos):
> >        self.__dict__['pos'] = pos
>
> >    def __setattr__(self, name, value):
> >        if name == 'pos':
> >            print 'pos changed to', value
>
> >       self.__dict__[name] = value
>
> >>>> b = Body([0, 0])
> >>>> b.pos = [3, 4]
> >>>> pos changed to [3, 4]
> >>>> b.pos[0] = 5 # How do I detect this in the Body?
>
> >I would like to print 'pos changed to [5, 4]' when pos is modified
> >with direct access to the pos object (i.e. b.pos[0] = 5)
>
> >My first instinct is to modify the Body's __dict__  by using
> >metaclasses.
> >I don't have any experience with meta classes but from what I remember
> >this should be possible but I thought there could be a simpler way to
> >do this.
>
> >What is the Pythonic way to do this?  Can anybody provide a short
> >example for either case (with or without metaclasses).
>
> You don't need metaclasses.  You do need a wrapper around the list
> so that you can intercept calls to __setitem__ (and whatever else)
> which are made onto it.
>
> You might also consider making Body new-style (subclass object) and
> using a property instead of a __setattr__ implementation.  You still
> need a wrapper, but the implementation of the rest of the feature
> should be simpler.
>
> Jean-Paul- Hide quoted text -
>
> - Show quoted text -

Hi Jean-Paul,

Sorry, I should have spelled this out in my post but I did not.  For
several reasons I do not wish to couple the pos object with the Body
instances.  In my case, I did not want pos objects (in my example they
were lists but they can be other objects as well) to care about their
parents.  So the question is:  How do we detect this in the Body?

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


detecting property modification

2007-12-21 Thread Mangabasi
Howdy,

I think it is easier to explain my question with a short example:

class Body:
def __init__(self, pos):
self.__dict__['pos'] = pos

def __setattr__(self, name, value):
if name == 'pos':
print 'pos changed to', value

   self.__dict__[name] = value

>>> b = Body([0, 0])
>>> b.pos = [3, 4]
>>> pos changed to [3, 4]
>>> b.pos[0] = 5 # How do I detect this in the Body?
>>>


I would like to print 'pos changed to [5, 4]' when pos is modified
with direct access to the pos object (i.e. b.pos[0] = 5)


My first instinct is to modify the Body's __dict__  by using
metaclasses.
I don't have any experience with meta classes but from what I remember
this should be possible but I thought there could be a simpler way to
do this.

What is the Pythonic way to do this?  Can anybody provide a short
example for either case (with or without metaclasses).

Regards


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


Re: detecting property modification

2007-12-21 Thread Mangabasi
On Dec 21, 1:11 pm, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
> On Fri, 21 Dec 2007 09:49:51 -0800 (PST), Mangabasi <[EMAIL PROTECTED]> wrote:
> > [snip]
>
> >Hi Jean-Paul,
>
> >Sorry, I should have spelled this out in my post but I did not.  For
> >several reasons I do not wish to couple the pos object with the Body
> >instances.  In my case, I did not want pos objects (in my example they
> >were lists but they can be other objects as well) to care about their
> >parents.  So the question is:  How do we detect this in the Body?
>
> You can't.  If you have this:
>
>     b = Body([1, 2])
>     b.pos[0] = 3
>
> Then Body gets asked for the value of the attribute; then, that
> object (a list in the case of this example) gets told to change
> its 0th element to 3.  There is no way for Body to get in the
> way of the second message, except by doing what I said - adding
> a wrapper around the list so that it _can_ get in the way.
>
> This doesn't require the list to know anything about Body, though.
> _Body_ should apply the wrapper.
>
> For example, new-style:
>
>     class Body(object):
>         def get_pos(self):
>             return ChangeNoticingListWrapper(self._pos)
>         def set_pos(self, value):
>             self._pos = value
>         pos = property(get_pos, set_pos)
>         def __init__(self, pos):
>             self.pos = pos
>
>     class ChangeNoticingListWrapper(object):
>         def __init__(self, wrapped):
>             self.wrapped = wrapped
>         def __setitem__(self, index, value):
>             print 'Changing', index, 'to', value
>             self.wrapped[index] = value
>
>     b = Body([1, 2])
>     b.pos[0] = 3
>
> Jean-Paul


Hi Jean-Paul,

I see what you are saying but my contrived example is not doing a good
job explaining the real issue I am having here.

When you say "The Body gets asked for the value of the attribute" that
means that Body's __dict__ is being asked to provide a value
corresponding to its 'pos' key, right?  Now I may want to ask a more
specific question here.  How do I modify Body's __dict__ so that I can
perform a task when __getitem__ method of the __dict__ is called?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: detecting property modification

2007-12-21 Thread Mangabasi
On Dec 21, 4:46 pm, Bruno Desthuilliers
<[EMAIL PROTECTED]> wrote:
> Mangabasi a écrit :
> (snip)
>
>
>
> > When you say "The Body gets asked for the value of the attribute" that
> > means that Body's __dict__ is being asked to provide a value
> > corresponding to its 'pos' key, right?
>
> Wrong. That means that attribute lookup rules are invoked, which may *or
> not* end up calling the Body *intance*'s __dict__ __setitem__ method to
> be called. Anyway:
>
> >  Now I may want to ask a more
> > specific question here.  How do I modify Body's __dict__ so that I can
> > perform a task when __getitem__ method of the __dict__ is called?
>
> This still won't solve your problem. What you want to trace are method
> calls on the objects returned by attribute lookup on instances of Body -
> not calls to instances __dict__ methods. In your example, what you want
> to trace are calls to pos.__getitem__, not calls to b.__dict__.__getitem__.
>
> And if you what you really want is to trace any mutator call to any
> arbitrary object that may become an attribute of b, then I'm afraid
> you're in for a hard time...

Now I am curious about these "attribute lookup rules".
My understanding was:

When an attribute name is accessed in an instance

First __getattribute__(self, name) is called.  This guy looks into the
instance's dictionary for the name, if not found then looks into the
parent class's dictionary, if not found searches the base classes, if
not found checks __getattr__, still not found raises an
AttributeError.

Does this look right?  If not, are these attribute lookup rules
documented somewhere?  I have not been paying much attention to the
new style classes, have things changed with the introduction of these
new style classes?

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