#!/usr/bin/env/python import math import random class sources: def __init__(self): self.available_sources = ['file', 'sine', 'cosine', 'random', 'zeros'] self.corresponding_methods = ['read_file','gen_sine', 'gen_cosine', 'gen_random_data', 'gen_zeros'] def get_sources_list(self): return self.available_sources def read_file(self, file_name, delimiter): try: my_file = open(file_name, 'r') except IOError: print "no file named " + file_name return [] #return an empty packet data_from_file = my_file.read() #read the file as a big string my_file.close() #done reading the file. process the string from now on data_from_file.strip('\n') #\n's will not be tollerated data_from_file.strip('[') #['s will not be tollerated data_from_file.strip(']') #]'s will not be tollerated data_from_file = data_from_file.split(delimiter) #break string into a list return data_from_file def gen_sine(self, freq, len): '''freq is frequency in cycles per packet. len is the length of the packet. type is the data type to return (e.g., "float", "short", "int")''' count = 0 sine = [] while count < len: sine.append(math.sin(freq*2*math.pi*count/len)) count = count + 1 return sine def gen_cosine(self, freq, len): '''freq is frequency in cycles per packet. len is the length of the packet. type is the data type to return (e.g., "float", "short", "int")''' count = 0 cosine = [] while count < len: cosine.append(math.cos(freq*2*math.pi*count/len)) count = count + 1 return cosine def gen_random_data(self, len, rand_type): '''the rand_type variable defines which function call to use in the random module''' data = [] rand_type = rand_type.lower() #makes comparison case insensitive if rand_type == 'random': count = 0 while count < len: data.append(random.random()) count = count + 1 elif rand_type == 'uniform': count = 0 while count < len: data.append(random.uniform()) count = count + 1 else: print "random methods other than random and uniform are not supported in sources.random_data" return data def gen_zeros(self, len): '''returns a list of zeros''' data = [] for n in range(len): data.append(0) return data