Exemple #1
0
    def calculateCrimeScore(self):
        ''' Calculates the crime score by multiplying
        the category ratings by their user-given scores to obtain
        a 'weighted' numerator. This number is normalized
        by dividing by the total number of crimes in the
        dataset multiplied by the highest possible amount of points
        to make the CrimeScore between 1 and 100.

        (for more info, see the methodology section of our README.html)
    '''
        numerator = 0
        categories = self.ratingsHash.keys()
        totalCrimes = self.getTotalCrimesInDatabase()

        for category in categories:
            # weightedCategoryScore = 0
            if category in self.ratingsHash:
                categoryScore = self.getScoreForCategory(category)

                fetcher = CrimeDataFetcher()
                numberOfCrimesInCategory = fetcher.getNumberOfCrimesInCategory(
                    category)

                weightedCategoryScore = categoryScore * numberOfCrimesInCategory

            numerator += weightedCategoryScore

        quotient = float(numerator) / (totalCrimes * self.MAX_SCORE)

        # Multiply by 100 and round to remove the decimal:
        return round(quotient * 100)
Exemple #2
0
def getDataAsHTML(crimeID):
    ''' Returns the crime info from the PSQL database and a google maps map,
        formatted into an HTML String. Will be placed directly into the output
        String of 'getPageAsHTML()'

        The embedded map is retrieved from a template file.
    '''

    outputString = ''
    try:
        dataFetcher = CrimeDataFetcher()

        currentCrimeData = dataFetcher.getCrimeFromID(crimeID)
        headers = ['Crime ID','Category','Description','Day of Week','Date','District','Resolution','X','Y']
        outputString += CrimrHTMLBuilder.getHTMLVertTable(headers,currentCrimeData)

        xCoordinateFromTable = currentCrimeData[7]
        yCoordinateFromTable = currentCrimeData[8]

        # Google map embed:
        mapEmbed = CrimrHTMLBuilder.getTemplate('googleMapEmbed')
        # Fill in the coordinates:
        mapEmbed = mapEmbed.replace('[[X_COORDINATE]]', str(xCoordinateFromTable))
        mapEmbed = mapEmbed.replace('[[Y_COORDINATE]]', str(yCoordinateFromTable))

        outputString += mapEmbed

    except Exception, e:
        outputString += 'Connection error: %s' % e
def getCategoryList():
  ''' Returns a list of the categories of crime
      (using CrimeDataFetcher to query the database)
  '''
  fetcher = CrimeDataFetcher()
  categories = fetcher.getListOfCategories()
  return categories
Exemple #4
0
def getCategoryList():
    ''' Returns a list of the categories of crime
      (using CrimeDataFetcher to query the database)
  '''
    fetcher = CrimeDataFetcher()
    categories = fetcher.getListOfCategories()
    return categories
Exemple #5
0
def getDataAsHTML(crimeID):
    ''' Returns the crime info from the PSQL database and a google maps map,
        formatted into an HTML String. Will be placed directly into the output
        String of 'getPageAsHTML()'

        The embedded map is retrieved from a template file.
    '''

    outputString = ''
    try:
        dataFetcher = CrimeDataFetcher()

        currentCrimeData = dataFetcher.getCrimeFromID(crimeID)
        headers = [
            'Crime ID', 'Category', 'Description', 'Day of Week', 'Date',
            'District', 'Resolution', 'X', 'Y'
        ]
        outputString += CrimrHTMLBuilder.getHTMLVertTable(
            headers, currentCrimeData)

        xCoordinateFromTable = currentCrimeData[7]
        yCoordinateFromTable = currentCrimeData[8]

        # Google map embed:
        mapEmbed = CrimrHTMLBuilder.getTemplate('googleMapEmbed')
        # Fill in the coordinates:
        mapEmbed = mapEmbed.replace('[[X_COORDINATE]]',
                                    str(xCoordinateFromTable))
        mapEmbed = mapEmbed.replace('[[Y_COORDINATE]]',
                                    str(yCoordinateFromTable))

        outputString += mapEmbed

    except Exception, e:
        outputString += 'Connection error: %s' % e
Exemple #6
0
  def calculateCrimeScore(self):
    ''' Calculates the crime score by multiplying
        the category ratings by their user-given scores to obtain
        a 'weighted' numerator. This number is normalized
        by dividing by the total number of crimes in the
        dataset multiplied by the highest possible amount of points
        to make the CrimeScore between 1 and 100.

        (for more info, see the methodology section of our README.html)
    '''
    numerator = 0
    categories = self.ratingsHash.keys()
    totalCrimes = self.getTotalCrimesInDatabase()

    for category in categories:
      # weightedCategoryScore = 0
      if category in self.ratingsHash:
        categoryScore = self.getScoreForCategory(category)

        fetcher = CrimeDataFetcher()
        numberOfCrimesInCategory = fetcher.getNumberOfCrimesInCategory(category)

        weightedCategoryScore = categoryScore * numberOfCrimesInCategory

      numerator += weightedCategoryScore

    quotient = float(numerator)/(totalCrimes * self.MAX_SCORE)

    # Multiply by 100 and round to remove the decimal:
    return round(quotient*100)
Exemple #7
0
    def getTotalCrimesInDatabase(self):
        '''Returns the total number of crimes (for all categories)'''
        total = 0
        fetcher = CrimeDataFetcher()
        for category in self.ratingsHash.keys():
            total += fetcher.getNumberOfCrimesInCategory(category)

        return total
Exemple #8
0
  def getTotalCrimesInDatabase(self):
    '''Returns the total number of crimes (for all categories)'''
    total = 0
    fetcher = CrimeDataFetcher()
    for category in self.ratingsHash.keys():
      total += fetcher.getNumberOfCrimesInCategory(category)

    return total
Exemple #9
0
def getFormAsHTML(parameters):
	'''
	Returns valid HTML as a string which represents the search form
	Will be placed directly into the output String of 'getPageAsHTML(parameters)'

	Uses an HTML template for the most part (assembles one form due for scalability purposes)
	'''

	html = CrimrHTMLBuilder.getTemplate('mainSearchForm')
	# Most of the search form options are for static elements of the dataset that
	#  aren't likely to change (e.g. days of week, districts). Thus they are
	#  hardcoded into the template file.

	# However, due to potential for new categories in future updates to the dataset,
	#  the category select form options are assembled by grabbing all the categories
	#  from the database:
	fetcher = CrimeDataFetcher()
	categories = fetcher.getListOfCategories()
	categoryFormString = ''
	for category in categories:
		categoryFormString += '<option value="%s" id="%s">%s</option>' % (category.lower(),category.lower(),category.title())
	html = html.replace('[[CATEGORIES_SELECT_FORM_OPTIONS]]', categoryFormString)

	#Preserve Search Form on page refresh
	#Keep values for the dropdown menus
	lowerDistricts = ["tenderloin", "central", "bayview", "ingleside", "mission", "northern", "park", "southern", "taraval", "richmond"]
	if 'district' in parameters and parameters['district'] in lowerDistricts:
		strToReplace = 'value="%s"' % parameters['district']
		replacementString = 'selected="selected" value="%s"' % parameters['district']
		html = html.replace(strToReplace, replacementString)

	lowerCats = []
	for s in categories:
		lowerCats.append(s.lower())
	if 'category' in parameters and parameters['category'] in lowerCats:
		strToReplace = 'value="%s"' % parameters['category']
		replacementString = 'selected="selected" value="%s"' % parameters['category']
		html = html.replace(strToReplace, replacementString)

	lowerDaysOfWeek = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
	if 'day' in parameters and parameters['day'] in lowerDaysOfWeek:
		strToReplace = 'value="%s"' % parameters['day']
		replacementString = 'selected="selected" value="%s"' % parameters['day']
		html = html.replace(strToReplace, replacementString)

	lowerResolutions = ['*all*', '*resolved*', '*unresolved*', 'arrest', 'book', 'cite', 'psychopathic', 'not prosecute']
	if 'resolution' in parameters and parameters['resolution'] in lowerResolutions:
		strToReplace = 'value="%s"' % parameters['resolution']
		replacementString = 'selected="selected" value="%s"' % parameters['resolution']
		html = html.replace(strToReplace, replacementString)

	# Keep the search box filled in:
	html = html.replace('[[SEARCH]]',parameters['search'])

	return html
Exemple #10
0
def getSearchResultsAsHTML(parameters):
	''' Returns the search results from the PSQL database, formatted into an HTML String
		Will be placed directly into the output String of 'getPageAsHTML(parameters)'
	'''
	outputString = ''
	dataFetcher = CrimeDataFetcher()
	try:
		outputTable = dataFetcher.getCrimesForSearch(parameters)
		if outputTable is not None:	#print nothing if there was no search
			headers = ['Crime ID','Category','Description','Day of Week','Date','District','Resolution','X','Y']
			outputString += CrimrHTMLBuilder.getHTMLTable(headers,outputTable)
		else:
			outputString += 'Use the controls above to get searching'
	except Exception, e:
		outputString +=  'Connection/Cursor Error: %s' % e
Exemple #11
0
def getSearchResultsAsHTML(parameters):
    ''' Returns the search results from the PSQL database, formatted into an HTML String
		Will be placed directly into the output String of 'getPageAsHTML(parameters)'
	'''
    outputString = ''
    dataFetcher = CrimeDataFetcher()
    try:
        outputTable = dataFetcher.getCrimesForSearch(parameters)
        if outputTable is not None:  #print nothing if there was no search
            headers = [
                'Crime ID', 'Category', 'Description', 'Day of Week', 'Date',
                'District', 'Resolution', 'X', 'Y'
            ]
            outputString += CrimrHTMLBuilder.getHTMLTable(headers, outputTable)
        else:
            outputString += 'Use the controls above to get searching'
    except Exception, e:
        outputString += 'Connection/Cursor Error: %s' % e
Exemple #12
0
def getParametersFromFormOrDefaults():
    ''' This method will return an int parameter to identify a crime

        By default, this will return an ID of a random crime from the database, if
        a search cgi parameter is given and is valid, it will return the ID of that
        crime.
    '''
    dataFetcher = CrimeDataFetcher()
    #default is a random unsolved crime
    parameter = dataFetcher.getRandomUnsolvedCrimeID()
    try:
        form = cgi.FieldStorage()
        if 'search' in form:
            tempID = cleanInput(form['search'].value)
            #check if the ID is valid!
            if dataFetcher.getCrimeFromID(tempID):
                parameter = tempID
    except Exception, e:
        pass
Exemple #13
0
def getParametersFromFormOrDefaults():
    ''' This method will return an int parameter to identify a crime

        By default, this will return an ID of a random crime from the database, if
        a search cgi parameter is given and is valid, it will return the ID of that
        crime.
    '''
    dataFetcher = CrimeDataFetcher()
    #default is a random unsolved crime
    parameter = dataFetcher.getRandomUnsolvedCrimeID()
    try:
        form = cgi.FieldStorage()
        if 'search' in form:
            tempID = cleanInput(form['search'].value)
            #check if the ID is valid!
            if dataFetcher.getCrimeFromID(tempID):
                parameter = tempID
    except Exception, e:
        pass
Exemple #14
0
def getFormAsHTML(parameters):
    '''
	Returns valid HTML as a string which represents the search form
	Will be placed directly into the output String of 'getPageAsHTML(parameters)'

	Uses an HTML template for the most part (assembles one form due for scalability purposes)
	'''

    html = CrimrHTMLBuilder.getTemplate('mainSearchForm')
    # Most of the search form options are for static elements of the dataset that
    #  aren't likely to change (e.g. days of week, districts). Thus they are
    #  hardcoded into the template file.

    # However, due to potential for new categories in future updates to the dataset,
    #  the category select form options are assembled by grabbing all the categories
    #  from the database:
    fetcher = CrimeDataFetcher()
    categories = fetcher.getListOfCategories()
    categoryFormString = ''
    for category in categories:
        categoryFormString += '<option value="%s" id="%s">%s</option>' % (
            category.lower(), category.lower(), category.title())
    html = html.replace('[[CATEGORIES_SELECT_FORM_OPTIONS]]',
                        categoryFormString)

    #Preserve Search Form on page refresh
    #Keep values for the dropdown menus
    lowerDistricts = [
        "tenderloin", "central", "bayview", "ingleside", "mission", "northern",
        "park", "southern", "taraval", "richmond"
    ]
    if 'district' in parameters and parameters['district'] in lowerDistricts:
        strToReplace = 'value="%s"' % parameters['district']
        replacementString = 'selected="selected" value="%s"' % parameters[
            'district']
        html = html.replace(strToReplace, replacementString)

    lowerCats = []
    for s in categories:
        lowerCats.append(s.lower())
    if 'category' in parameters and parameters['category'] in lowerCats:
        strToReplace = 'value="%s"' % parameters['category']
        replacementString = 'selected="selected" value="%s"' % parameters[
            'category']
        html = html.replace(strToReplace, replacementString)

    lowerDaysOfWeek = [
        'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday',
        'saturday'
    ]
    if 'day' in parameters and parameters['day'] in lowerDaysOfWeek:
        strToReplace = 'value="%s"' % parameters['day']
        replacementString = 'selected="selected" value="%s"' % parameters['day']
        html = html.replace(strToReplace, replacementString)

    lowerResolutions = [
        '*all*', '*resolved*', '*unresolved*', 'arrest', 'book', 'cite',
        'psychopathic', 'not prosecute'
    ]
    if 'resolution' in parameters and parameters[
            'resolution'] in lowerResolutions:
        strToReplace = 'value="%s"' % parameters['resolution']
        replacementString = 'selected="selected" value="%s"' % parameters[
            'resolution']
        html = html.replace(strToReplace, replacementString)

    # Keep the search box filled in:
    html = html.replace('[[SEARCH]]', parameters['search'])

    return html