Code snippets - Online AWE Prompt

Copy paste the code snippets below in the online prompt to see the result.

1. Simple Query

Query for the number of the BiasFrame's for a specific instrument.

from astro.main.BiasFrame import BiasFrame
instrument_name = 'WFI'
query = BiasFrame.instrument.name == instrument_name
print "%s has %d BiasFrame's" % (instrument_name, len(query))

2. Defining and Using Methods

Define a method which does the square of a number plus one. Then iterate over a list of values calling this method for each value.

def square_plus_one(num) :
    return num*num+1

for i in range(10) :
    print i, square_plus_one(i)

3. Plotting

Plot x versus y, where x = [0,24] and y = x * x. The resulting figure is saved to a file called x_square.png and is saved in your personal directory. Click the 'files' link on the bottom right in the online prompt to see all files.

import pylab
x = range(25)
y = [i*i for i in x]
pylab.plot(x, y)
pylab.savefig('x_square.png', dpi=100)

More code snippets to come ! Please mail me for suggestions !!