0

I have this code:

import numpy as np

class B():
    def __init__(self,a,b):
        self.a = a
        self.b = b

    def __repr__(self):
        return 'B({0},{1})'.format(self.a,self.b)

class test():

    def __init__(self,name,method, measures=None):
        self.name = name
        self.method = method
        self.measures = measures

    def __repr__(self):
        return 'test({0.name},{0.method},{1})'.format(self,self.measures)

    def A(self,thevalues,another):

            if self.method == 'S' and another != self.name: 
                yield self.S(thevalues)

            if self.method == 'V':
                yield self.V(thevalues)

    def S(self, thevalues):
        # Change thevalues and return the updated 
        thevalues = np.array(thevalues)
        new_values = thevalues + 3
        yield new_values

    def V(self,thevalues):
        yield thevalues
        #return np.array(thevalues)

class basic():

    def __init__(self, name, thelist):
        self.name = name
        self.thelist = thelist

    def startp(self):
        values = []
        for i in self.thelist:
            if i.name == self.name and i.method != 'S':
                # Just fill some values to server as input to `A`
                for j in range(4):
                    values.append(j)
            # The `A` function will determine which functions inside 
            # `test` it will call
            yield i.A(values,self.name)

Starting from the bottom, up :

I am calling the startp function in order to stat the process.

I am initializing and filling the values list when certain criteria are met (name and method) in order to pass it to the functions A and S.

Then, I use yield in order to start calling A function for every object in list.

The A function checks and runs theS or V functions when certain criteria are met.

Using this data:

b1 = np.array([B(1,2), B(3,4)])
b2 = np.array([B(5,6), B(7,8)])
b3 = np.array([B(9,10), B(11,12)])

alist = [ test('a','V',b1), test('b','S',b2), test('c','S',b3)]
obs = basic('a',alist).startp()
for i in obs:
   for j in i:
       print(j)

Inside V function, if I use return np.array(thevalues), I am receiving:

[0 1 2 3]
<generator object test.S at 0x7f74fc1c0b48>
[3 4 5 6]
<generator object test.S at 0x7f74fc1c0bf8>
[3 4 5 6]

If I use yield thevalues, I am receiving:

<generator object test.V at 0x7f74ec99d678>
<generator object test.S at 0x7f74fc1c0bf8>
[3 4 5 6]
<generator object test.S at 0x7f74ec99d678>
[3 4 5 6]

But if I use:

for i in obs:
    for j in i:
        #print(j)
        for k in j:
            print(k)

I can access the V generated values but the result is:

[0, 1, 2, 3]
[3 4 5 6]
3
4
5
6
[3 4 5 6]
3
4
5
6

What I want is , if it possible, to receive only one array with the results:

[ [0 1 2 3],
  [3 4 5 6],
  [3 4 5 6]
]

And I must keep the yield statements in startp ( basically one yield is fine, I just use 2 yield because I want to pass values to S function)

George
  • 5,808
  • 15
  • 83
  • 160
  • What is your desired output? `[0,1,2,3]` or `[3,4,5,6]` ? – AsheKetchum Feb 22 '17 at 20:38
  • @AsheKetchum:Please check the end of post, I am writting what output I want.I want one array as I say.Thanks – George Feb 22 '17 at 20:41
  • an array of 3 arrays? – AsheKetchum Feb 22 '17 at 21:41
  • @AsheKetchum: One 3x4 array as you can see (preferable using `yield thevalues` in `V`) – George Feb 22 '17 at 21:54
  • There is no way to keep *the* yield in `startp()` and meet your requirement of yielding a single list of lists. You could keep *a* yield in `startp()` would this be ok? – Stephen Rauch Feb 23 '17 at 01:15
  • @StephenRauch:Yes, it would be ok to keep one yield in `startp`.But the problem is that I must supply to the `S` function the `values`,that's why I have the second yield.If there is a way to do this with one yield only (call the `A` function which decides to call the `S` or `V`) that's better of course. – George Feb 23 '17 at 06:38
  • @StephenRauch:Please note that I updated my post.( I moved the yield i.A(values,self.name) ,one tab to the left because it was wrong and I removed the `i.S(values)` from `startp`) and it changes the results , generating more objects – George Feb 23 '17 at 09:15

1 Answers1

0

Finally , I used:

def startp(self):
        values = []
        result = []
        for i in self.thelist:
            if i.name == self.name and i.method != 'S':
                # Just fill some values to server as input to `A`
                 for j in range(4):
                    values.append(j)

            values = np.array(values)
            result.extend(i.A(values,self.name))

        result = np.array(result)
        return result

and I am receiving:

temp = []
for i in obs:
    for j in i:
        temp.extend(j)

temp = np.array(temp)
print(temp.shape)

(3,4)
George
  • 5,808
  • 15
  • 83
  • 160