[pystatsmodels] What am I doing wrong. must me something simple

28 views
Skip to first unread message

Vincent Davis

unread,
Apr 16, 2010, 4:22:42 PM4/16/10
to pystat...@googlegroups.com
I am attempting to ad a function to SimpleTable but I keep getting an error that self is not defined.

def multi_col_stubs(self):
                if self.txt_fmt['stubcols']:
                        #only allowing 2 columns for now
                        self.raw_data = [zip(self.stubs[i], self.raw_data[i])
                                         for i in range(len(self.raw_data[i]))]
                        print self.raw_data
 multi_col_stubs()

I even tried
print self.raw_data

I must not be seeing something, my dyslexia must be bad today :)

I pushed this if you what to see the rest.



josef...@gmail.com

unread,
Apr 16, 2010, 4:27:15 PM4/16/10
to pystat...@googlegroups.com
On Fri, Apr 16, 2010 at 4:22 PM, Vincent Davis <vin...@vincentdavis.net> wrote:
I am attempting to ad a function to SimpleTable but I keep getting an error that self is not defined.

def multi_col_stubs(self):
                if self.txt_fmt['stubcols']:
                        #only allowing 2 columns for now
                        self.raw_data = [zip(self.stubs[i], self.raw_data[i])
                                         for i in range(len(self.raw_data[i]))]
                        print self.raw_data
 multi_col_stubs()

It's a method with one argument self, so either you call it with an instance self.multi_col_stubs() or add
an argument multi_col_stubs(something)

It might be easier to put test calls in the __main__.

Cheers,

Josef
 

Skipper Seabold

unread,
Apr 16, 2010, 4:31:51 PM4/16/10
to pystat...@googlegroups.com
On Fri, Apr 16, 2010 at 4:27 PM, <josef...@gmail.com> wrote:
>
>
> On Fri, Apr 16, 2010 at 4:22 PM, Vincent Davis <vin...@vincentdavis.net> wrote:
>>
>> I am attempting to ad a function to SimpleTable but I keep getting an error that self is not defined.
>> def multi_col_stubs(self):
>>                 if self.txt_fmt['stubcols']:
>>                         #only allowing 2 columns for now
>>                         self.raw_data = [zip(self.stubs[i], self.raw_data[i])
>>                                          for i in range(len(self.raw_data[i]))]
>>                         print self.raw_data
>>  multi_col_stubs()
>
> It's a method with one argument self, so either you call it with an instance self.multi_col_stubs() or add
> an argument multi_col_stubs(something)
>
> It might be easier to put test calls in the __main__.

Does all of this make sense? Can clarify more if not. Also, the
__init__ line (line 32) that you took from two lines to one should be
two lines. We try to follow pep 8 as much as possible and this means
that no lines should be over 80 characters long.
http://www.python.org/dev/peps/pep-0008/

Skipper


--
Subscription settings: http://groups.google.com/group/pystatsmodels/subscribe?hl=en

Vincent Davis

unread,
Apr 16, 2010, 4:42:15 PM4/16/10
to pystat...@googlegroups.com
It's a method with one argument self, so either you call it with an instance self.multi_col_stubs() or add
an argument multi_col_stubs(something)

Still no go.

NameError: name 'self' is not defined
File "/Users/vmd/Dropbox/statsmodels/testing.py", line 10, in <module>
  import statsmodels.formatting as f
File "/Users/vmd/Dropbox/statsmodels/statsmodels/scikits/statsmodels/formatting.py", line 8, in <module>
  class SimpleTable:
File "/Users/vmd/Dropbox/statsmodels/statsmodels/scikits/statsmodels/formatting.py", line 104, in SimpleTable
  self.multi_col_stubs()

Shouldn't <print self.raw_data> by it's self work?

NameError: name 'self' is not defined
File "/Users/vmd/Dropbox/statsmodels/testing.py", line 10, in <module>
  import statsmodels.formatting as f
File "/Users/vmd/Dropbox/statsmodels/statsmodels/scikits/statsmodels/formatting.py", line 8, in <module>
  class SimpleTable:
File "/Users/vmd/Dropbox/statsmodels/statsmodels/scikits/statsmodels/formatting.py", line 97, in SimpleTable
  print self.raw_data

Also, the
__init__ line (line 32) that you took from two lines to one should be
two lines.
I know, just did that to get a better look, I'll review the formatting before I am done. Thanks for the link to the pep 8, I have been meaning to take a closer look

Skipper Seabold

unread,
Apr 16, 2010, 4:49:44 PM4/16/10
to pystat...@googlegroups.com
On Fri, Apr 16, 2010 at 4:42 PM, Vincent Davis <vin...@vincentdavis.net> wrote:
>>
>> It's a method with one argument self, so either you call it with an instance self.multi_col_stubs() or add
>> an argument multi_col_stubs(something)
>
> Still no go.
> NameError: name 'self' is not defined
> File "/Users/vmd/Dropbox/statsmodels/testing.py", line 10, in <module>
>   import statsmodels.formatting as f
> File "/Users/vmd/Dropbox/statsmodels/statsmodels/scikits/statsmodels/formatting.py", line 8, in <module>
>   class SimpleTable:
> File "/Users/vmd/Dropbox/statsmodels/statsmodels/scikits/statsmodels/formatting.py", line 104, in SimpleTable
>   self.multi_col_stubs()
> Shouldn't <print self.raw_data> by it's self work?
> NameError: name 'self' is not defined
> File "/Users/vmd/Dropbox/statsmodels/testing.py", line 10, in <module>
>   import statsmodels.formatting as f
> File "/Users/vmd/Dropbox/statsmodels/statsmodels/scikits/statsmodels/formatting.py", line 8, in <module>
>   class SimpleTable:
> File "/Users/vmd/Dropbox/statsmodels/statsmodels/scikits/statsmodels/formatting.py", line 97, in SimpleTable
>   print self.raw_data

Can you provide an example. See how I changed your file below.

skipper@linux-desktop:~/nipy/statsmodels-vincent/scikits/statsmodels$
bzr diff formatting.py
=== modified file 'scikits/statsmodels/formatting.py'
--- scikits/statsmodels/formatting.py 2010-04-16 20:21:42 +0000
+++ scikits/statsmodels/formatting.py 2010-04-16 20:47:14 +0000
@@ -72,14 +72,14 @@

def __str__(self):
return self.as_text()
- print self.raw_data
+
def multi_col_stubs(self):
if self.txt_fmt['stubcols']:
#only allowing 2 columns for now
self.raw_data = [zip(self.stubs[i], self.raw_data[i])
for i in range(len(self.raw_data[i]))]
print self.raw_data
- multi_col_stubs()
+

def _format_rows(self, tablestrings, fmt_dict):
"""Return: list of strings,
@@ -396,3 +396,7 @@
stubcols = 2)
######### end: default formats ##############

+if __name__ == "__main__":
+ import numpy as np
+ ex = SimpleTable(np.arange(10))

Now when I do

ex.multi_col_stubs()

I get

KeyError: 'stubcols'

Because I didn't set up the instance properly, but if you can get a
failing example in the __name__ == "__main__" part, I can have a
closer look.

Vincent Davis

unread,
Apr 16, 2010, 5:05:18 PM4/16/10
to pystat...@googlegroups.com
Got to go for now, Will look at it later, Thanks

Mike

unread,
Apr 17, 2010, 7:21:31 AM4/17/10
to pystatsmodels
self is only going to exist INSIDE your multi_col_stubs, so your line
print self.raw_data won't see a self.

self isn't actually special, you could define your function

def multi_col_stubs(rubbish):

and python would carry on happily, using rubbish instead of self
(DON'T do this - it's a bad idea for readability - I'm just pointing
out that self isn't a special term outside of a function - it only
exists because you define it in the signature). Your print command it
outside the sig so it doesn't know a "self" variable.

On another note - this class looks like an old style class - which I
think will need changing to inherit (object) for python 3?


On 16 Apr, 22:05, Vincent Davis <vinc...@vincentdavis.net> wrote:
> Got to go for now, Will look at it later, Thanks
>
>   *Vincent Davis
> 720-301-3003 *
> vinc...@vincentdavis.net
>  my blog <http://vincentdavis.net> |
> LinkedIn<http://www.linkedin.com/in/vincentdavis>
>
> On Fri, Apr 16, 2010 at 2:49 PM, Skipper Seabold <jsseab...@gmail.com>wrote:
>
> > On Fri, Apr 16, 2010 at 4:42 PM, Vincent Davis <vinc...@vincentdavis.net>

Skipper Seabold

unread,
Apr 17, 2010, 3:45:24 PM4/17/10
to pystat...@googlegroups.com
On Sat, Apr 17, 2010 at 7:21 AM, Mike <m.j.a...@googlemail.com> wrote:
> self is only going to exist INSIDE your multi_col_stubs, so your line
> print self.raw_data won't see a self.
>
> self isn't actually special, you could define your function
>
> def multi_col_stubs(rubbish):
>
> and python would carry on happily, using rubbish instead of self
> (DON'T do this - it's a bad idea for readability - I'm just pointing
> out that self isn't a special term outside of a function - it only
> exists because you define it in the signature).  Your print command it
> outside the sig so it doesn't know a "self" variable.
>
> On another note - this class looks like an old style class - which I
> think will need changing to inherit (object) for python 3?
>

Good catch. This should be changed.
Reply all
Reply to author
Forward
0 new messages