Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing class to define multiple variables in python
    primarykey
    data
    text
    <p>I'm still new to python and this is probably going be one of those (stupid) boring questions. However, any help will be much appreciated. I'm programing something that involves many variables and I've decided to use a class to encapsulate all variables (hopefully making it easier to "read" for me as time passes), but it's not working as I thought it will. So, without further ado here is a part of the class that captures the gist.</p> <pre><code>import numpy as np class variable: def __init__(self, length): self.length = length # time length` def state_dynamic(self): length = self.length return np.zeros((2, np.size(length))) def state_static(self): length = self.length return np.zeros((2, np.size(length))) def control_dynamic(self): length = self.length return np.zeros((2, np.size(length))) def control_static(self): length = self.length return np.zeros((2, np.size(length))) def scheduling(self): length = self.length return np.zeros(np.size(length)) def disturbance(self): length = self.length dummy = np.random.normal(0., 0.1, np.size(length)) for i in range(20): dummy[i+40] = np.random.normal(0., 0.01) + 1. dummy[80:100] = 0. return dummy </code></pre> <p>I've also tried this one:</p> <pre><code>import numpy as np class variable: def __init__(self, type_1, type_2, length): self.type_1 = type_1 # belongs to set {state, control, scheduling, disturbance} self.type_2 = type_2 # belongs to set {static, dynamic, none} self.length = length # time length def type_v(self): type_1 = self.type_1 type_2 = self.type_2 length = self.length if type_1 == 'state' and type_2 == 'dynamic': return np.zeros((2, np.size(length))) elif type_1 == 'state' and type_2 == 'static': return np.zeros((2, np.size(length))) elif type_1 == 'control' and type_2 == 'dynamic': return np.zeros((2, np.size(length))) elif type_1 == 'control' and type_2 == 'static': return np.zeros((2, np.size(length))) elif type_1 == 'scheduling' and type_2 == 'none': return np.zeros(np.size(length)) elif type_1 == 'disturbance' and type_2 == 'none': dummy = np.random.normal(0., 0.1, np.size(length)) for i in range(20): dummy[i+40] = np.random.normal(0., 0.01) + 1. dummy[80:100] = 0. return dummy </code></pre> <p>Now, using the first one (the outcome is the same for the second class as well), when I write the following, say: </p> <pre><code>In [2]: time = np.linspace(0,10,100) In [5]: v = variable(time) In [6]: v1 = v.state_dynamic In [7]: v1.size --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /home/&lt;ipython-input-7-e6a5d17aeb75&gt; in &lt;module&gt;() ----&gt; 1 v1.size AttributeError: 'function' object has no attribute 'size' In [8]: v2 = variable(np.size(time)).state_dynamic In [9]: v2 Out[9]: &lt;bound method variable.state_dynamic of &lt;__main__.variable instance at 0x3ad0a28&gt;&gt; In [10]: v1[0,0] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /home/&lt;ipython-input-10-092bc2b9f982&gt; in &lt;module&gt;() ----&gt; 1 v1[0,0] TypeError: 'instancemethod' object has no attribute '__getitem__' </code></pre> <p>I was hoping that by writing</p> <pre><code>variable(length).state_dynamic </code></pre> <p>I'll access</p> <pre><code>np.zeros((2, np.size(length))) </code></pre> <p>Anyway, if I made something utterly stupid please let me know :) and feel free to give any kind of advice. Thank you in advance for your time and kind attention. Best regards.</p> <p>EDIT #1:</p> <p>@wheaties:</p> <p>Thank you for a quick reply and help :)</p> <p>What I'm currently trying to do is the following. I have to plot several "variables", e.g., state, control, dropouts, scheduling and disturbances. All the variables depend on three parameters, namely, dynamic, static and horizon. Further, state and control are <code>np.zeros((2, np.size(length)))</code>, dropouts and scheduling are <code>np.zeros(np.size(length))</code> and disturbance has specific form (see above). Initially, I declared them in the script and the list is very long and looks ugly. I use these variables to store responses of dynamical systems considered and to plot them. I don't know if this is a good way of doing this and if you have any suggestion please share. </p> <p>Thanks again for your help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload