working with classes, inheritance, _str_ returns and a list

2017-01-15 Thread David D
I am creating a parent class and a child class.  I am inheriting from the 
parent with an additional attribute in the child class.  I am using __str__ to 
return the information.  When I run the code, it does exactly what I want, it 
returns the __str__ information.  This all works great. 

BUT

1) I want what is returned to be appended to a list (the list will be my 
database)
2) I append the information to the list that I created
3) Whenever I print the list, I get a memory location

So how do I take the information that is coming out of the child class (as a 
__str__ string), and keep it as a string so I can append it to the list?

pseudo code

allcars=[]

parent class()
   def init (self, model, wheels, doors)
  self.model= model etc

child class (parent)
   def init(self, model, wheels, doors, convertible)
   super(child, self).__init__(model, wheels, doors)
   self.convertible = convertible

   def __str__(self):
return "model: " + self.model + etc

car1= child(model, wheels, doors, convertible)
print car1



Here is where it goes wrong for me


allcars.append(car1)

I am sure I am making a silly mistake in here somewhere...





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


Re: working with classes, inheritance, _str_ returns and a list

2017-01-15 Thread Chris Angelico
On Mon, Jan 16, 2017 at 6:58 AM, David D  wrote:
> I am creating a parent class and a child class.  I am inheriting from the 
> parent with an additional attribute in the child class.  I am using __str__ 
> to return the information.  When I run the code, it does exactly what I want, 
> it returns the __str__ information.  This all works great.
>
> BUT
>
> 1) I want what is returned to be appended to a list (the list will be my 
> database)
> 2) I append the information to the list that I created
> 3) Whenever I print the list, I get a memory location

Technically it's an identity, not a memory location, but yes, I know
what you mean here.

What's happening is that printing a list actually prints the *repr* of
an object, rather than its str. The easiest change is to rename your
__str__ function to __repr__; if you don't need them to be different,
define __repr__ and it'll be used for printing as well.

By the way, you'll find that Python 3 makes some things with class
definitions a bit easier. There are less traps to potentially fall
into. I strongly suggest using the latest Python (currently 3.6) or
something close to it (3.5 is in a lot of Linux distributions).

All the best!

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem while building Python 3.6 from source.

2017-01-15 Thread Michael S
Thanks Thomas and ChrisA!

@Thomas: I don't know.. Unfortunately I am not as skilled as to be
able to find out..

@ChrisA: I tried your advice and think it worked. So, I just used $
./configure and then $ make. Then I was not sure, whether it had
worked so I used $ make test and got a fairly long result. At the end
there was a line like "test successful". Nevertheless not all of the
tests were successful.

So, now I got some questions:

1) Why did not all of the tests in $ make test succeed? But the end
line was "test successful". That confuses me.

2) This is more general. In order to get the build-dependencies I used
# apt-get build-dep python3.4. I also googled for the build
dependencies but did not find anything. So, how could I actually
figure out the build dependencies for Python3.6?


On Fri, Jan 13, 2017 at 7:19 PM, Chris Angelico  wrote:
> On Sat, Jan 14, 2017 at 5:00 AM, Michael S  wrote:
>> Hello,
>> I am new to this mailing-list and I really don't know whether this
>> mail should belong to python-dev. Please tell me, if so.
>
> Hi and welcome! This kind of thing is best on this list initially.
>
>> Unfortunately, I have got the following problem: I wanted to build and
>> install Python 3.6 from source but did not succeed.
>> To clarify my situation, I got as an operating system Debian jessie
>> 8.6 and I used the xz compressed source tarball from
>> https://www.python.org/downloads/release/python-360/.
>> Concerning the build dependencies: I just executed:
>> $ sudo apt-get build-dep python3.4 (since 3.6 and 3.5 did not work).
>
> That should be fine; the build dependencies of Python don't tend to
> change frequently. Jessie shipped with Python 3.4 but nothing newer,
> so there won't be packages for python3.5 or python3.6.
>
>> Then I executed ./configure --enable-optimizations and make -j4 (I got 4 
>> cores).
>> The output of make ended like:
>> 'make: *** [profile-opt] Error 2'.
>
> That just means that something went wrong. You'd have to scroll up to
> find the actual cause of the error.
>
>> I had redirected the output and error of the configure and make commands via
>> $ make -j4 &> /home/username/make_output.txt.
>> Nevertheless I got an error to the console:
>> '*** Error in ./python'" free(): invalid next size (normal):
>> 0x015bdf90 ***'.
>> Due to these error messages (this one and the one at the end of make)
>> I think the build was not successful.
>>
>> How to solve this problem?
>>
>> Of course I could send you the output and error files.
>
> The first thing I'd do would be to try a non-optimized build. Set your
> current build tree aside and re-extract into a new directory (that
> way, when you go back to playing with optimized builds, you don't have
> to redo the work), and run configure with no arguments. I'd also be
> inclined to run make with no arguments; there've been issues with
> parallel builds in enough projects that I've gotten into the habit of
> "problem? do it the slow way". If that build also fails, scroll up a
> bit and find where stuff failed.
>
> Are you familiar with building programs from source? If not, the best
> solution might be to post the entire log, but ideally, you should be
> able to skim through the last part of the log and report the actual
> problem that's cropping up.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


nfldb API

2017-01-15 Thread Thufir Hawat

I'm looking at nfldb:

pip install --user nfldb

which is on github:

https://github.com/BurntSushi/nfldb/wiki/More-examples


Coming at this from Java, how would I hook into nfldb with Java?  Or, 
perhaps, run the code in Jython?


I'm sure there's a general solution or approach, would appreciate any 
pointers.  Perhaps simply using JNI to leverage the work that's gone into 
this library already.




thanks,

Thufir
--
https://mail.python.org/mailman/listinfo/python-list


Re: Problem while building Python 3.6 from source.

2017-01-15 Thread Chris Angelico
On Mon, Jan 16, 2017 at 7:55 AM, Michael S  wrote:
> @ChrisA: I tried your advice and think it worked. So, I just used $
> ./configure and then $ make. Then I was not sure, whether it had
> worked so I used $ make test and got a fairly long result. At the end
> there was a line like "test successful". Nevertheless not all of the
> tests were successful.

Can you run:

$ ./python

That should fire up the newly-built interpreter.

> So, now I got some questions:
>
> 1) Why did not all of the tests in $ make test succeed? But the end
> line was "test successful". That confuses me.

Not sure - I'd have to see what the actual output was. Most likely
there were some tests skipped; not all tests apply on all platforms.
For instance, there are a number of Windows-specific tests, which
won't be running on your system. Also, Python includes a number of
optional modules, and if you don't have their dependencies, you get a
Python that's fully functional with the exception of that/those
module(s).

> 2) This is more general. In order to get the build-dependencies I used
> # apt-get build-dep python3.4. I also googled for the build
> dependencies but did not find anything. So, how could I actually
> figure out the build dependencies for Python3.6?

They don't often change between versions. Certainly between 3.4 and
3.6 you should have no problems. It's only an issue if you're porting
to a brand new platform or something, and then you have to do the
usual dance of "compile, read the error, grab a new library, rinse and
repeat".

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: working with classes, inheritance, _str_ returns and a list

2017-01-15 Thread Erik

Hi,

On 15/01/17 19:58, David D wrote:

I am creating a parent class and a child class.  I am inheriting from
the parent with an additional attribute in the child class.  I am
using __str__ to return the information.  When I run the code, it
does exactly what I want, it returns the __str__ information.  This
all works great.

BUT

1) I want what is returned to be appended to a list (the list will be
my database) 2) I append the information to the list that I created
3) Whenever I print the list, I get a memory location

So how do I take the information that is coming out of the child
class (as a __str__ string), and keep it as a string so I can append
it to the list?


[snip]


Here is where it goes wrong for me

allcars.append(car1)


This adds the object (of the child or parent class) to the list. If you
_really_ want the *string* returned by __str__() to be appended to the
list then you would do:

allcars.append(str(car1))

(The str() function returns what the object's __str__() method returns
if it has one - otherwise it will return SOME sort of string
representation of your object, but you can't rely on the format of that).

I'm a bit confused though as to why you would want to create that object
only store its __str__() value and discard the object itself.

E.
--
https://mail.python.org/mailman/listinfo/python-list


Re: working with classes, inheritance, _str_ returns and a list

2017-01-15 Thread Frank Millman
"David D"  wrote in message 
news:4f0680eb-2678-4ea2-b622-a6cd5a19e...@googlegroups.com...


I am creating a parent class and a child class.  I am inheriting from the 
parent with an additional attribute in the child class.  I am using 
__str__ to return the information.  When I run > the code, it does exactly 
what I want, it returns the __str__ information.  This all works great.


BUT

1) I want what is returned to be appended to a list (the list will be my 
database)

2) I append the information to the list that I created
3) Whenever I print the list, I get a memory location



You have been given an explanation, and a couple of workarounds.

Here is another possible workaround, which may help depending on how you 
actually print the list -


If you are saying -
   for item in list:
   print(item)

you can say instead -
 for item in list:
   print(str(item))

HTH

Frank Millman


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


Sockets: IPPROTO_IP not supported

2017-01-15 Thread Peter Pearson
Trying to sniff Ethernet packets, I do this:

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)

but it results in this:

$ sudo python3 sniff_survey.py 
Traceback (most recent call last):
  File "sniff_survey.py", line 118, in 
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
  File "/usr/lib/python3.2/socket.py", line 94, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
socket.error: [Errno 93] Protocol not supported

Anybody know what I'm doing wrong?  (Python 3.2.3 under Debian 3.2.84-1.)

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: working with classes, inheritance, _str_ returns and a list

2017-01-15 Thread Frank Millman

"Frank Millman"  wrote in message news:o5hlh4$1sb$1...@blaine.gmane.org...


If you are saying -
for item in list:
print(item)

you can say instead -
  for item in list:
print(str(item))



This is not correct, sorry.

print(item) will automatically print the string representation of item, so 
it makes no difference.


The principle is still correct, though.

If you want to convert what you call the memory address of the item to the 
string representation, just wrap it in str(...)


Frank


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


Re: working with classes, inheritance, _str_ returns and a list

2017-01-15 Thread Frank Millman

"Frank Millman"  wrote in message news:o5hnbq$q36$1...@blaine.gmane.org...


"Frank Millman"  wrote in message news:o5hlh4$1sb$1...@blaine.gmane.org...
>
> If you are saying -
> for item in list:
> print(item)
>
> you can say instead -
>   for item in list:
> print(str(item))
>

This is not correct, sorry.

print(item) will automatically print the string representation of item, so 
it makes no difference.


The principle is still correct, though.

If you want to convert what you call the memory address of the item to the 
string representation, just wrap it in str(...)




I keep thinking of something else just after I have posted - sorry about 
that.


When you say you print the list, maybe you are literally doing the 
following -

   print(list)

In that case, the solution is to turn it into a list comprehension, and 
apply str() to each item -

   print([str(item) for item in list])

Frank


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