diff --git a/.hgignore b/.hgignore new file mode 100644 index 0000000..2663aec --- /dev/null +++ b/.hgignore @@ -0,0 +1,2 @@ +syntax: glob +.settings diff --git a/.pydevproject b/.pydevproject index 4e723c5..45aef39 100644 --- a/.pydevproject +++ b/.pydevproject @@ -3,7 +3,7 @@ Default -python 2.6 +python 2.7 /aspn/src /aspn/tktable diff --git a/src/game_of_life.py b/src/game_of_life.py new file mode 100644 index 0000000..07ffc82 --- /dev/null +++ b/src/game_of_life.py @@ -0,0 +1,199 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + + +import numpy as np + +""" +This program is an extensible Conway's game of life. It allows to define +different type of grid (for example 2D or 3D) and more complex rules. +Each grid inherits an Abstract grid that implement the method (next()) +to pass for the next configuration. +Furthermore, each element can be whatever type. In this +example I designed Grid2DBool that represent the simple Conway's game of life, +but could be possible to develop and easily implement more complex grids and +rules. + +Note: +The demo save also the animation in a file .mp4 and plot it through pyplot. +The demo could take long time because of storing all the configurations before +showing the animation. Therefore, the visualization can be improved using other +libraries (as wxpython) that paint the configuration of the grid once it's +created. +With a more complex view it's convenient to apply MVC pattern declaring the +model AbstractGrid as the Observable class. + +@author Filippo Squillace +@date 02/12/2011 +@version 0.0.5 +""" + +class AbstractGrid(): + """ + This class represents the abstract grid that implement + the template method to generate the next configuration. The rules are + definied in the abstract method next_state() and it's not + implemented in this class because depends on the structure of the matrix + and the type of elements in the grid. + """ + def __init__(self): + self.matrix = np.array([], dtype=bool) + + def __str__(self): + return self.matrix.__str__() + + ####### Abstract methods ######## + def next_state(self, coords, el): + raise NotImplementedError() + def is_done(self): + raise NotImplementedError() + ################################ + + def add_element(self, coords, el): + self.matrix[coords] = el + + def next(self): + # copy the matrix + old_matrix = self.matrix.copy() + itr = self.matrix.flat + coords = itr.coords + for el in itr: + old_matrix[coords] = self.next_state(coords, el) + coords = itr.coords + + # copy all the modifications + self.matrix = old_matrix + + +class Grid2D(AbstractGrid): + + def __init__(self, n, m, typ=bool): + AbstractGrid.__init__(self) + self.n = n + self.m = m + self.matrix = np.array([None for x in range(n*m)], dtype=typ).reshape(n,m) + +class Grid2DBool(Grid2D): + """ + Represents the classical Conway's game of life with 2D grid + and each element can be either True (alive) or Fase (death) + Params: + n - number of rows + m - number of columns + """ + def __init__(self, n, m): + Grid2D.__init__(self, n, m, bool) + + def add_element(self, x, y): + t = (x, y) + Grid2D.add_element(self, t, True) + + def next_state(self, coords, el): + # Gets all information from the neighbors + (x, y) = coords + neighbors = 0 + if x==0: + x1=0 + else: + x1=x-1 + if x==self.n-1: + x2=self.n-1 + else: + x2=x+1 + + if y==0: + y1=0 + else: + y1=y-1 + if y==self.m-1: + y2=self.m-1 + else: + y2=y+1 + + for n in self.matrix[x1:x2+1, y1:y2+1].flat: + if n: + neighbors = neighbors + 1 + + # Excludes the main element + if el: + neighbors = neighbors - 1 + + if el: # el alives + if neighbors==2 or neighbors==3: + return True + if neighbors<2 or neighbors>3: + return False + else: # el death + if neighbors==3: + return True + + def is_done(self): + return not self.matrix.max() # there is no True + + +def light_spaceship(g, x, y, invert=False): + """ + Puts the lightweight spaceship right in the grid starting from icoords + """ + if not invert: + g.add_element(x,y) + g.add_element(x+2,y) + g.add_element(x+3,y+1) + g.add_element(x+3,y+2) + g.add_element(x+3,y+3) + g.add_element(x+3,y+4) + g.add_element(x+2,y+4) + g.add_element(x+1,y+4) + g.add_element(x,y+3) + else: + g.add_element(x,y) + g.add_element(x+2,y) + g.add_element(x+3,y-1) + g.add_element(x+3,y-2) + g.add_element(x+3,y-3) + g.add_element(x+3,y-4) + g.add_element(x+2,y-4) + g.add_element(x+1,y-4) + g.add_element(x,y-3) + + +if __name__ == '__main__': + + import matplotlib.pyplot as plt + import matplotlib.animation as animation + + n = 50 + + g = Grid2DBool(n, n) + + light_spaceship(g, 5,2) + light_spaceship(g, 25,2) + light_spaceship(g, 45,2) + light_spaceship(g, 5,40, True) + light_spaceship(g, 25,40, True) + light_spaceship(g, 45,40, True) + + + + x = np.arange(0, n+1) + y = np.arange(0, n+1) + X,Y = np.meshgrid(x,y) + ims = [] + ims.append((plt.pcolor(X, Y, g.matrix),)) + + counter = 0 + while(not g.is_done() and counter < 100): + g.next() + ims.append((plt.pcolor(X, Y, g.matrix),)) + counter = counter + 1 + + fig = plt.figure(1) + im_ani = animation.ArtistAnimation(fig, ims, interval=2,\ + repeat_delay=3000,\ + blit=True) + + im_ani.save('im.mp4') + plt.axis([0, n, n, 0]) + plt.axis('off') + plt.show() + \ No newline at end of file diff --git a/src/stock_info.py b/src/stock_info.py new file mode 100644 index 0000000..faebfa8 --- /dev/null +++ b/src/stock_info.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python +# +# Copyright (c) 2007-2008, Corey Goldberg (corey@goldb.org) +# +# license: GNU LGPL +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# Made into class by Alexander Wallar on December 17, 2011 + +import urllib + +class StockInfo: + """ + Constructor + """ + def __init__(self, __symbol): + self.symbol = __symbol + + def __request(self, stat): + url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (self.symbol, stat) + return urllib.urlopen(url).read().strip().strip('"') + + def get_all(self): + """ + Get all available quote data for the given ticker symbol. + Returns a dictionary. + """ + values = self.__request(self.symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7').split(',') + data = {} + data['price'] = values[0] + data['change'] = values[1] + data['volume'] = values[2] + data['avg_daily_volume'] = values[3] + data['stock_exchange'] = values[4] + data['market_cap'] = values[5] + data['book_value'] = values[6] + data['ebitda'] = values[7] + data['dividend_per_share'] = values[8] + data['dividend_yield'] = values[9] + data['earnings_per_share'] = values[10] + data['52_week_high'] = values[11] + data['52_week_low'] = values[12] + data['50day_moving_avg'] = values[13] + data['200day_moving_avg'] = values[14] + data['price_earnings_ratio'] = values[15] + data['price_earnings_growth_ratio'] = values[16] + data['price_sales_ratio'] = values[17] + data['price_book_ratio'] = values[18] + data['short_ratio'] = values[19] + return data + + get_price = lambda self: float(self.__request('l1')) + get_change = lambda self: float(self.__request('c1')) + get_volume = lambda self: float(self.__request('v')) + get_avg_daily_volume = lambda self: float(self.__request('a2')) + get_stock_exchange = lambda self: float(self.__request('x')) + get_market_cap = lambda self: float(self.__request('j1')) + get_book_value = lambda self: float(self.__request('b4')) + get_ebitda = lambda self: float(self.__request('j4')) + get_dividend_per_share = lambda self: float(self.__request('d')) + get_dividend_yield = lambda self: float(self.__request('y')) + get_earnings_per_share = lambda self: float(self.__request('e')) + get_52_week_high = lambda self: float(self.__request('k')) + get_52_week_low = lambda self: float(self.__request('j')) + get_50day_moving_avg = lambda self: float(self.__request('m3')) + get_200day_moving_avg = lambda self: float(self.__request('m4')) + get_price_earnings_ratio = lambda self: float(self.__request('r')) + get_price_earnings_growth_ratio = lambda self: float(self.__request('r5')) + get_price_sales_ratio = lambda self: float(self.__request('p5')) + get_price_book_ratio = lambda self: float(self.__request('p6')) + get_short_ratio = lambda self: float(self.__request('s7')) + + def get_historical_prices(self, start_date, end_date): + """ + Get historical prices for the given ticker symbol. + Date format is 'YYYYMMDD' + + Returns a nested list. + """ + url = 'http://ichart.yahoo.com/table.csv?s=%s&' % self.symbol + \ + 'd=%s&' % str(int(end_date[4:6]) - 1) + \ + 'e=%s&' % str(int(end_date[6:8])) + \ + 'f=%s&' % str(int(end_date[0:4])) + \ + 'g=d&' + \ + 'a=%s&' % str(int(start_date[4:6]) - 1) + \ + 'b=%s&' % str(int(start_date[6:8])) + \ + 'c=%s&' % str(int(start_date[0:4])) + \ + 'ignore=.csv' + days = urllib.urlopen(url).readlines() + data = [day[:-2].split(',') for day in days] + return data