Python class problem

Python class problem

Postby jukka » 2016-06-02 13:47:42

I have a Dog class, which i got from the python documentation. Code for that is:

Code: Select all
class Dog:

    def __init__(self, name):
        self.name = name
        self.tricks = []    # creates a new empty list for each dog

    def add_trick(self, trick):
        self.tricks.append(trick)


I have the following peach-call:

Code: Select all
def tt():
    import techila
    from Dog import Dog
    d = Dog('Fido')
    d.add_trick('This was a locally taught trick')   
    result = techila.peach(funcname = d.add_trick,
                           sdkroot = 'C:/techila',
                           params = ['<param>'],     
                           peachvector = range(1, 11)       
                           )

    d.tricks


Both files (Dog.py and tt.py) are in the same directory. When i run tt(), i get the following error:

Code: Select all
>>> tt()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tt.py", line 9, in tt
    peachvector = range(1, 11)
  File "C:\xxxsite-packages\techila\__init__.py", line 700, in peach
    lib.techila_peach_setName(ph, funcname)
ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: Don't know how to convert parameter 2
>>>


Who can I fix this, I don't know where to start?
jukka
 
Posts: 13
Joined: 2013-09-20 11:48:12

Re: Python class problem

Postby kari » 2016-06-03 06:42:44

You are calling the add_trick which is an instance method of Dog class. Currently that is not possible with Techila peach. You could try using some kind of a wrapper function. You also need to pass the d object to peach, e.g. with params.

Also your add_trick doesn't return anything. Therefore even if this would work your results would be empty.

Can you explain what exactly you are trying to achieve?
kari
Techila Staff
Techila Staff
 
Posts: 48
Joined: 2012-07-18 11:14:09

Re: Python class problem

Postby kari » 2016-06-03 06:47:51

Just a note,

according to PEP 8, all module names should be all lowercase

https://www.python.org/dev/peps/pep-000 ... dule-names

So you should have dog.py and

Code: Select all
    from dog import Dog
kari
Techila Staff
Techila Staff
 
Posts: 48
Joined: 2012-07-18 11:14:09

Re: Python class problem

Postby jukka » 2016-06-03 07:17:17

I put my code in a wrapper and now it seems to work ok. Just need to combine the results somehow..now theyre all getting prepended with the 'This was a locally taught trick'.

If you are interested, the code that worked for me was:

Code: Select all
def wf(d,idx):
    from Dog import Dog
    d.add_trick(idx)
    return(d)

def tt():
    import techila
    from Dog import Dog
    d = Dog('Fido')
    d.add_trick('This was a locally taught trick')   
    result = techila.peach(funcname = wf,
                           sdkroot = 'C:/techila',
                           params = [d,'<param>'],
                           datafiles = ['Dog.py'],
                           peachvector = range(1, 11)       
                           )

    return(result)
jukka
 
Posts: 13
Joined: 2013-09-20 11:48:12


Return to Python

cron