biologyguy / Evopix

Pictures evolved
0 stars 0 forks source link

Working with the Evopic class #1

Closed biologyguy closed 10 years ago

biologyguy commented 10 years ago

Hey Jane,

When working with classes in general, here are two things to keep in mind. First, when you create a new object from that class, the init() function will be called automatically, and any arguments you pass into the object call will be handed off to init(). Second, there is a convention in Python that any methods beginning with an underscore, like _parse_evp(), are not meant to be used directly on an object. In a lot of other languages you have public and private variables/methods, but not in Python. Everything is public, so the leading underscore is a way of saying "I would have made this private if I could have, so use at your own risk".

To your Evopic usage questions specifically.

So the sexual_reproduction.py script is going to take two .evp genomes - at this stage Bob and Sue.

Yes ma'am. And I've just added a couple lines in the 'sandbox' at the bottom of breed.py that do just that.

I’ve got a string that is the path to the bob.evp file and have imported the Evopic class.

File paths are going to be replaced by calls to the MySQL database very shortly, but for now, yes.

Now I want to get the paths that make up bob.evp so I can start making changes to create a new evp with sue… I’m guessing I need the _parse_evp function, but I can’t seem to work out how to make it work. Any hints?

This is actually done automatically in Evopic.init(), so just by creating the new object, you will have access to all of the paths data. Try this:

bob = Evopic(<evp_string>)
for i in bob.paths:
    print(i)

print("\n")

for key in bob.paths[0]:
    print("%s\t:%s" % (key, bob.paths[0][key]))

Hopefully this will make it clear what's going on, but let me know if it isn't.

Not used to working with classes I guess! (at least in this kind of context).

Classes are awesome, once you get the hang of them :)

jhawkey commented 10 years ago

Okay, I've got it working now. Took a little more fiddling - I didn't realise that I had to actually open the file and read it for the Evopic class to work. I was trying to do this:

boy = Evopic(bob.evp)

When I actually ended up doing to make it work was this:

with open('bob.evp', 'r') as infile:
    boy = Evopic(infile.read())

It would be nice if I didn't have use .read(), but I don't think it really matters. First hurdle overcome! Things should theoretically get a bit more straightforward now.