vvoelz / biceps

Bayesian inference of conformational populations
https://github.com/vvoelz/biceps
Other
12 stars 3 forks source link

simple shape analogy example #44

Closed robraddi closed 6 years ago

robraddi commented 6 years ago

#!/usr/bin/env python

#import triangle
import numpy as np

"""This is the shape program analogy.
"""

####################### New Page ############################################

class Shape:
    '''

    '''
    def __init__(self, nSides, nVerticies,Name):
        self.nSides = nSides
        self.nVerticies = nVerticies
        self.Name = Name

####################### New Page ############################################

class Insert(Shape):
    '''The Posterior Sampler class could be seen as this method

    '''

    def __init__(self, nSides, nVerticies, Name, color):
        Shape.__init__(self,nSides, nVerticies,Name)
        self.color = color

    def Draw(self,height,width):
        '''This Method inside the Insert class is the same as Sample
        inside PosteriorSampler.

        '''

        print 'Name:',self.Name
        print 'Sides:',self.nSides
        print 'Verticies:',self.nVerticies
        print 'Color:',self.color
        print 'Height:',height
        print 'Width:',width

InsertShape = Insert(color='blue',nSides=3, nVerticies=3,Name='Triangle')
InsertShape.Draw(height='0.4', width='0.3')
vvoelz commented 6 years ago

I would alter this analogy in the following way:

#!/usr/bin/env python

#import triangle
import numpy as np

### BTW, you can define many classes in the same file! ###

class Shape:
    '''
A parent class for all shapes.

    '''
    def __init__(self, nSides, nVertices,Name):
        self.nSides = nSides
        self.nVertices = nVertices
        self.Name = Name

class Triangle(Shape):
    '''A derived class for a triangle.'''

    def __init__(self, nSides=3, nVertices=3, Name='Triangle', color='red'):
        self.nSides = nSides
        self.nVertices = nVertices
        self.Name = Name
        self.color = color

    def draw(self, height='0.4', width='0.3'):
        '''Draw the shape.'''

        print 'Name:', self.Name
        print 'Sides:', self.nSides
        print 'Vertices:', self.nVertices
        print 'Color:', self.color
        print 'Height:', height
        print 'Width:', width

# Main program

# let's draw a patriotic picture
my_shapes = []
my_shapes.append( Triangle() )
my_shapes.append( Triangle(color='white') )
my_shapes.append( Triangle(color='blue') )

# now we render the picture -- happy 4th!
for s in my_shapes:
    s.draw()

Yay it works!

CST11352:Desktop suvince$ python shapes.py
Name: Triangle
Sides: 3
Vertices: 3
Color: red
Height: 0.4
Width: 0.3
Name: Triangle
Sides: 3
Vertices: 3
Color: white
Height: 0.4
Width: 0.3
Name: Triangle
Sides: 3
Vertices: 3
Color: blue
Height: 0.4
Width: 0.3
vvoelz commented 6 years ago

p.s. let's talk about this more at our meeting -- there are other ways we can improve this too

robraddi commented 6 years ago

This is understood. I always appreciate your help, Vince.