<commit_before>from django.shortcuts import render

from go.datasets.lga_suburb_list import process
from go.datasets.attractions.process import selectrandom2attractions
from go.datasets.events.process import selectrandom2events
from go.datasets.red_light_camera_notices.process import getRedLightFinesBySuburb
from go.datasets.crime.process import getCrimeRankStats
from go.datasets.census_preproc.process import getCostOfLiving
from go.datasets.travel_method.process import getPercentCommutersByType
import random

def index(request):
    westernSydneyLGAs = ['Auburn', 'Bankstown', 'Blacktown', 'Blue Mountains', 'Camden', 'Campbelltown', 'Fairfield', 'Hawkesbury', 'The Hills', 'Holroyd', 'Liverpool', 'Parramatta', 'Penrith', 'Wollondilly']
    context = {}
    suburbsToLGA, lgaToRegion, suburbToPostcode = process.process()
    compare = ' '.join(w.capitalize() for w in request.GET.get('compare', '').strip().split())    # trim and title capitalise
    nwss = sorted([suburb for suburb in suburbsToLGA.keys() for lga in suburbsToLGA[suburb] if lga not in westernSydneyLGAs])
    westernSydneySuburbs = [suburb for suburb in suburbsToLGA.keys() for lga in suburbsToLGA[suburb] if lga in westernSydneyLGAs]
    compareSuburbs = findMatchingSuburbs(compare, suburbsToLGA, lgaToRegion)
    isWesternSydney = False
    for i in compareSuburbs:
        if i in westernSydneySuburbs:
            isWesternSydney = True

    allsuburbs = sorted(list(set(suburbsToLGA.keys())))

    context['non_ws_suburbs'] = str(getUniqueItems(nwss)).replace("'", '"')
    context['autocomplete'] = str(getUniqueItems(allsuburbs)).replace("'", '"')
    context['compare'] = compare
    context['isWesternSydney'] = isWesternSydney
    context['info'] = getcompareinfo(compare, suburbsToLGA, lgaToRegion, westernSydneyLGAs)
    matchedLGAs = findMatchingLGAs(compare, suburbsToLGA, lgaToRegion)

    westernSydneyPostcodes = list(set([suburbToPostcode[i] for i in westernSydneySuburbs if i in suburbToPostcode]))

    try:
        postCode = suburbToPostcode[findMatchingSuburbs(compare, suburbsToLGA, lgaToRegion)[0]]
        context['costOfLiving'] = getCostOfLiving(compare, postCode, westernSydneyPostcodes, suburbToPostcode)
    except:
        pass

    context['crimerank'] = getCrimeRank(matchedLGAs, westernSydneyLGAs)

    x = getAttractions()
    attractions = []
    for i in x:
        attractions.append({"url":x[i], "place":i})
    context['randomattractions'] = attractions

    x = getEvents()
    events = []
    for i in x:
        events.append({"url":x[i], "place":i})
    context['randomevents'] = events
    context['redlightfines'] = getRedLightFines(compare, findMatchingSuburbs(compare, suburbsToLGA, lgaToRegion), westernSydneySuburbs)
    context['gogreen'] = getGreenTransport(compare, matchedLGAs, westernSydneyLGAs)
    return render(request, 'go/index.html', context)

def getUniqueItems(seq):
    seen = set()
    seen_add = seen.add
    return [ x for x in seq if not (x in seen or seen_add(x))]

# Get an LGA given the input; if the input is a suburb find its LGA
def findMatchingLGAs(compare, suburbToLGA, lgaToRegion):
    if compare in lgaToRegion:
        return [compare]
    elif compare in suburbToLGA:
        return suburbToLGA[compare]
    return []

# Get a list of suburbs given the input; if the input is an LGA find all its suburbs
def findMatchingSuburbs(compare, suburbToLGA, lgaToRegion):
    if compare in suburbToLGA:
        return [compare]
    elif compare in lgaToRegion:
        return getSuburbsInLGA(compare, suburbToLGA)
    return []

def getSuburbsInLGA(lga, suburbToLGA):
    return [s for s in suburbToLGA.keys() for l in suburbToLGA[s] if l == lga]

def getcompareinfo(compare, suburbToLGA, lgaToRegion, westernSydneyLGAs):
    if compare == '':
        return ''

    out = compare + ' is a '

    type = None
    lgas = []
    lgaStr = ''
    region = ''
    if compare in lgaToRegion and compare in suburbToLGA:
        type = 'suburb and local government area'
        lgas = [compare]
        region = lgaToRegion[compare]
    elif compare in lgaToRegion:
        type = 'local government area'
        lgas = [compare]
        region = lgaToRegion[compare]
    elif compare in suburbToLGA:
        type = 'suburb'
        lgas = suburbToLGA[compare]
        lgaStr = ' in the local government area'
        if len(lgas) == 1:
            lgaStr += ' of ' + lgas[0]
        else:
            lgaStr += 's of ' + ', '.join(lgas[:-1]) + ' and ' + lgas[-1]
        lgaStr += ','
        region = lgaToRegion[lgas[0]]
    if type is None:
        return ''

    out += type + lgaStr + ' in the ' + region + ' region.'

    if len(lgas) > 0 and lgas[0] in westernSydneyLGAs:
        out += ' It is part of Western Sydney!'

    return out

import random

def getMedianRent(compare, LGAs, westernSydneyLGAs):
    medianRent, _ = getMedianWeeklyRent(LGAs)
    medianRentWest, lowest5WestLGA = getMedianWeeklyRent(westernSydneyLGAs)
    if medianRent is None or medianRentWest is None:
        return None

    place = 'Western Sydney'
    # See if the rent for Western Sydney is lower. If not, find a low-rent area in Western Sydney to compare
    randomLowestRentLGAInTheWest = random.choice(list(lowest5WestLGA.keys()))
    if len(lowest5WestLGA) > 0 and medianRentWest >= medianRent and lowest5WestLGA[randomLowestRentLGAInTheWest] < medianRent:
        place = randomLowestRentLGAInTheWest
        medianRentWest = lowest5WestLGA[randomLowestRentLGAInTheWest]
    if medianRentWest >= medianRent:
        return None

    out = {}
    out['heading'] = 'Affordable living'
    out['text'] = ['The median weekly rent for ' + place + ' is $' + str(medianRentWest) +
                   ', compared to $' + str(medianRent) + ' in ' + compare + '.',
                   'That\'s an annual saving of $' + str((medianRent - medianRentWest) * 52) + '!']
    return out

def getAttractions():
    attractions = selectrandom2attractions()
    #out = 'You can also check out the great Western Sydney attractions: '
    #for place,url in attractions.iteritems():
    #    out += "" + place + "" + ","
    return attractions

def getEvents():
    events = selectrandom2events()
    #out = 'You can also check out the great Western Sydney attractions: '
    #for place,url in attractions.iteritems():
    #    out += "" + place + "" + ","
    return events

def getCrimeRank(matchedLGAs, westernSydneyLGAs):
    random.shuffle(westernSydneyLGAs)
    outputStr = "Did you know that {0} has a significantly worse issue with {2} (Ranked {1}th) than {3} (Ranked {4}th)?"
    dataset = getCrimeRankStats()
    for matchedLGA in matchedLGAs:
        if matchedLGA in dataset:
            matchedLGARank = dataset[matchedLGA]
            for westernSydneyLGA in westernSydneyLGAs:
                if westernSydneyLGA in dataset:
                    westernSydneyLGARank = dataset[westernSydneyLGA]
                    #compare the rank
                    if matchedLGARank[2].strip() != '-' and matchedLGARank[2].strip() != 'nc' and \
                        westernSydneyLGARank[2].strip() != '-' and westernSydneyLGARank[2].strip() != 'nc' and \
                        float(matchedLGARank[2]) < float(westernSydneyLGARank[2]):
                        #return matchedLGA+":"+str(matchedLGARank[2])+" vs "+westernSydneyLGA + ":" + str(westernSydneyLGARank[2])
                        # + " " + str(matchedLGARank) + " " + str(westernSydneyLGARank)
                        return outputStr.format(matchedLGA,matchedLGARank[2],"personal stealing", westernSydneyLGA, westernSydneyLGARank[2])
                    elif matchedLGARank[5].strip() != '-' and matchedLGARank[5].strip() != 'nc' and \
                        westernSydneyLGARank[5].strip() != '-' and westernSydneyLGARank[5].strip() != 'nc' and \
                        float(matchedLGARank[5]) < float(westernSydneyLGARank[5]):
                        return outputStr.format(matchedLGA,matchedLGARank[5],"stealing from motor vehicle", westernSydneyLGA, westernSydneyLGARank[5])
                    elif matchedLGARank[8].strip() != '-' and matchedLGARank[8].strip() != 'nc' and \
                        westernSydneyLGARank[8].strip() != '-' and westernSydneyLGARank[8].strip() != 'nc' and \
                        float(matchedLGARank[8]) < float(westernSydneyLGARank[8]):
                        return outputStr.format(matchedLGA,matchedLGARank[8],"robbery", westernSydneyLGA, westernSydneyLGARank[8])
                    elif matchedLGARank[11].strip() != '-' and matchedLGARank[11].strip() != 'nc' and \
                        westernSydneyLGARank[11].strip() != '-' and westernSydneyLGARank[11].strip() != 'nc' and \
                        float(matchedLGARank[11]) < float(westernSydneyLGARank[11]):
                        return outputStr.format(matchedLGA,matchedLGARank[11],"non-domestic assault violence", westernSydneyLGA, westernSydneyLGARank[11])
                    elif matchedLGARank[14].strip() != '-' and matchedLGARank[14].strip() != 'nc' and \
                        westernSydneyLGARank[14].strip() != '-' and westernSydneyLGARank[14].strip() != 'nc' and \
                        float(matchedLGARank[14]) < float(westernSydneyLGARank[14]):
                        return outputStr.format(matchedLGA,matchedLGARank[14],"domestic assault violence", westernSydneyLGA, westernSydneyLGARank[14])
                    elif matchedLGARank[17].strip() != '-' and matchedLGARank[17].strip() != 'nc' and \
                        westernSydneyLGARank[17].strip() != '-' and westernSydneyLGARank[17].strip() != 'nc' and \
                        float(matchedLGARank[17]) < float(westernSydneyLGARank[17]):
                        return outputStr.format(matchedLGA,matchedLGARank[17],"sexual offences", westernSydneyLGA, westernSydneyLGARank[17])

    return "Congrats, your area is just as safe as Western Sydney in 2013."

def getRedLightFines(compare, suburbs, westernSydneySuburbs):
    if len(suburbs) == 0:
        return None
    fines = getRedLightFinesBySuburb()
    avgnumber, avgfine = getRedLightFinesForSuburbs(suburbs, fines)
    avgnumberwest, avgfinewest = getRedLightFinesForSuburbs(westernSydneySuburbs, fines)
    if avgnumber is None:
        return None

    out = {}
    if avgnumber > avgnumberwest:
        out['heading'] = 'Safer driving'
        out['text'] = ['The average annual count of red light offences for Western Sydney is ' + str(avgnumberwest) +
                       ', as opposed to to ' + str(avgnumber) + ' in ' + compare + '.']
        return out
    elif avgfine > avgfinewest:
        out['heading'] = 'Affordable'
        out['text'] = ['The average red light fine for Western Sydney is $' + str(avgfinewest) +
                       ', as opposed to $' + str(avgfine) + ' in ' + compare + '.',
                       'You save $' + str(avgfine - avgfinewest) + '!']
        return out
    return None

def getRedLightFinesForSuburbs(suburbs, fines):
    totalnumber = 0
    totalfines = 0
    suburbsfound = 0
    for suburb in suburbs:
        if suburb in fines:
            totalnumber += fines[suburb][0]
            totalfines += fines[suburb][1]
            suburbsfound += 1
    if suburbsfound > 0:
        return totalnumber / suburbsfound, totalfines / suburbsfound
    return None, None

def getGreenTransport(compare, LGAs, westernSydneyLGAs):
    pctbike = getPercentCommutersByType(LGAs, 'bikeorwalk')
    pctpublic = getPercentCommutersByType(LGAs, 'publictransport')
    pctbikewest = getPercentCommutersByType(westernSydneyLGAs, 'bikeorwalk')
    pctpublicwest = getPercentCommutersByType(westernSydneyLGAs, 'publictransport')
    winratiobike = 0
    if pctbikewest > 0:
        winratiobike = (pctbikewest - pctbike) / pctbikewest
    winratiopublic = 0
    if pctpublicwest > 0:
        winratiopublic = (pctpublicwest - pctpublic) / pctpublicwest
    if winratiobike <= 0 and winratiopublic <= 0:
        return None
    out = {}
    out['heading'] = 'Sustainable living'
    if winratiobike > winratiopublic:
        out['text'] = [('%.1f' % (pctbikewest * 100)) + '% of Western Sydneysiders bicycled or walked to work.',
                       'That\'s more than the ' + ('%.1f' % (pctbike * 100)) + '% in ' + compare + '!']
    else:
        out['text'] = [('%.1f' % (pctpublicwest * 100)) + '% of Western Sydneysiders took public transport to work.',
                       'That\'s more than the ' + ('%.1f' % (pctpublic * 100)) + '% in ' + compare + '!']
    return out
<commit_msg>Fixing up some crime descriptions<commit_after>from django.shortcuts import render

from go.datasets.lga_suburb_list import process
from go.datasets.attractions.process import selectrandom2attractions
from go.datasets.events.process import selectrandom2events
from go.datasets.red_light_camera_notices.process import getRedLightFinesBySuburb
from go.datasets.crime.process import getCrimeRankStats
from go.datasets.census_preproc.process import getCostOfLiving
from go.datasets.travel_method.process import getPercentCommutersByType
import random

def index(request):
    westernSydneyLGAs = ['Auburn', 'Bankstown', 'Blacktown', 'Blue Mountains', 'Camden', 'Campbelltown', 'Fairfield', 'Hawkesbury', 'The Hills', 'Holroyd', 'Liverpool', 'Parramatta', 'Penrith', 'Wollondilly']
    context = {}
    suburbsToLGA, lgaToRegion, suburbToPostcode = process.process()
    compare = ' '.join(w.capitalize() for w in request.GET.get('compare', '').strip().split())    # trim and title capitalise
    nwss = sorted([suburb for suburb in suburbsToLGA.keys() for lga in suburbsToLGA[suburb] if lga not in westernSydneyLGAs])
    westernSydneySuburbs = [suburb for suburb in suburbsToLGA.keys() for lga in suburbsToLGA[suburb] if lga in westernSydneyLGAs]
    compareSuburbs = findMatchingSuburbs(compare, suburbsToLGA, lgaToRegion)
    isWesternSydney = False
    for i in compareSuburbs:
        if i in westernSydneySuburbs:
            isWesternSydney = True

    allsuburbs = sorted(list(set(suburbsToLGA.keys())))

    context['non_ws_suburbs'] = str(getUniqueItems(nwss)).replace("'", '"')
    context['autocomplete'] = str(getUniqueItems(allsuburbs)).replace("'", '"')
    context['compare'] = compare
    context['isWesternSydney'] = isWesternSydney
    context['info'] = getcompareinfo(compare, suburbsToLGA, lgaToRegion, westernSydneyLGAs)
    matchedLGAs = findMatchingLGAs(compare, suburbsToLGA, lgaToRegion)

    westernSydneyPostcodes = list(set([suburbToPostcode[i] for i in westernSydneySuburbs if i in suburbToPostcode]))

    try:
        postCode = suburbToPostcode[findMatchingSuburbs(compare, suburbsToLGA, lgaToRegion)[0]]
        context['costOfLiving'] = getCostOfLiving(compare, postCode, westernSydneyPostcodes, suburbToPostcode)
    except:
        pass

    context['crimerank'] = getCrimeRank(matchedLGAs, westernSydneyLGAs)

    x = getAttractions()
    attractions = []
    for i in x:
        attractions.append({"url":x[i], "place":i})
    context['randomattractions'] = attractions

    x = getEvents()
    events = []
    for i in x:
        events.append({"url":x[i], "place":i})
    context['randomevents'] = events
    context['redlightfines'] = getRedLightFines(compare, findMatchingSuburbs(compare, suburbsToLGA, lgaToRegion), westernSydneySuburbs)
    context['gogreen'] = getGreenTransport(compare, matchedLGAs, westernSydneyLGAs)
    return render(request, 'go/index.html', context)

def getUniqueItems(seq):
    seen = set()
    seen_add = seen.add
    return [ x for x in seq if not (x in seen or seen_add(x))]

# Get an LGA given the input; if the input is a suburb find its LGA
def findMatchingLGAs(compare, suburbToLGA, lgaToRegion):
    if compare in lgaToRegion:
        return [compare]
    elif compare in suburbToLGA:
        return suburbToLGA[compare]
    return []

# Get a list of suburbs given the input; if the input is an LGA find all its suburbs
def findMatchingSuburbs(compare, suburbToLGA, lgaToRegion):
    if compare in suburbToLGA:
        return [compare]
    elif compare in lgaToRegion:
        return getSuburbsInLGA(compare, suburbToLGA)
    return []

def getSuburbsInLGA(lga, suburbToLGA):
    return [s for s in suburbToLGA.keys() for l in suburbToLGA[s] if l == lga]

def getcompareinfo(compare, suburbToLGA, lgaToRegion, westernSydneyLGAs):
    if compare == '':
        return ''

    out = compare + ' is a '

    type = None
    lgas = []
    lgaStr = ''
    region = ''
    if compare in lgaToRegion and compare in suburbToLGA:
        type = 'suburb and local government area'
        lgas = [compare]
        region = lgaToRegion[compare]
    elif compare in lgaToRegion:
        type = 'local government area'
        lgas = [compare]
        region = lgaToRegion[compare]
    elif compare in suburbToLGA:
        type = 'suburb'
        lgas = suburbToLGA[compare]
        lgaStr = ' in the local government area'
        if len(lgas) == 1:
            lgaStr += ' of ' + lgas[0]
        else:
            lgaStr += 's of ' + ', '.join(lgas[:-1]) + ' and ' + lgas[-1]
        lgaStr += ','
        region = lgaToRegion[lgas[0]]
    if type is None:
        return ''

    out += type + lgaStr + ' in the ' + region + ' region.'

    if len(lgas) > 0 and lgas[0] in westernSydneyLGAs:
        out += ' It is part of Western Sydney!'

    return out

import random

def getMedianRent(compare, LGAs, westernSydneyLGAs):
    medianRent, _ = getMedianWeeklyRent(LGAs)
    medianRentWest, lowest5WestLGA = getMedianWeeklyRent(westernSydneyLGAs)
    if medianRent is None or medianRentWest is None:
        return None

    place = 'Western Sydney'
    # See if the rent for Western Sydney is lower. If not, find a low-rent area in Western Sydney to compare
    randomLowestRentLGAInTheWest = random.choice(list(lowest5WestLGA.keys()))
    if len(lowest5WestLGA) > 0 and medianRentWest >= medianRent and lowest5WestLGA[randomLowestRentLGAInTheWest] < medianRent:
        place = randomLowestRentLGAInTheWest
        medianRentWest = lowest5WestLGA[randomLowestRentLGAInTheWest]
    if medianRentWest >= medianRent:
        return None

    out = {}
    out['heading'] = 'Affordable living'
    out['text'] = ['The median weekly rent for ' + place + ' is $' + str(medianRentWest) +
                   ', compared to $' + str(medianRent) + ' in ' + compare + '.',
                   'That\'s an annual saving of $' + str((medianRent - medianRentWest) * 52) + '!']
    return out

def getAttractions():
    attractions = selectrandom2attractions()
    #out = 'You can also check out the great Western Sydney attractions: '
    #for place,url in attractions.iteritems():
    #    out += "" + place + "" + ","
    return attractions

def getEvents():
    events = selectrandom2events()
    #out = 'You can also check out the great Western Sydney attractions: '
    #for place,url in attractions.iteritems():
    #    out += "" + place + "" + ","
    return events

def getCrimeRank(matchedLGAs, westernSydneyLGAs):
    random.shuffle(westernSydneyLGAs)
    outputStr = "Did you know that {0} has a significantly worse issue with {2} (Ranked {1}th) than {3} (Ranked {4}th)?"
    dataset = getCrimeRankStats()
    for matchedLGA in matchedLGAs:
        if matchedLGA in dataset:
            matchedLGARank = dataset[matchedLGA]
            for westernSydneyLGA in westernSydneyLGAs:
                if westernSydneyLGA in dataset:
                    westernSydneyLGARank = dataset[westernSydneyLGA]
                    #compare the rank
                    if matchedLGARank[2].strip() != '-' and matchedLGARank[2].strip() != 'nc' and \
                        westernSydneyLGARank[2].strip() != '-' and westernSydneyLGARank[2].strip() != 'nc' and \
                        float(matchedLGARank[2]) < float(westernSydneyLGARank[2]):
                        #return matchedLGA+":"+str(matchedLGARank[2])+" vs "+westernSydneyLGA + ":" + str(westernSydneyLGARank[2])
                        # + " " + str(matchedLGARank) + " " + str(westernSydneyLGARank)
                        return outputStr.format(matchedLGA,matchedLGARank[2],"personal theft", westernSydneyLGA, westernSydneyLGARank[2])
                    elif matchedLGARank[5].strip() != '-' and matchedLGARank[5].strip() != 'nc' and \
                        westernSydneyLGARank[5].strip() != '-' and westernSydneyLGARank[5].strip() != 'nc' and \
                        float(matchedLGARank[5]) < float(westernSydneyLGARank[5]):
                        return outputStr.format(matchedLGA,matchedLGARank[5],"theft from a motor vehicle", westernSydneyLGA, westernSydneyLGARank[5])
                    elif matchedLGARank[8].strip() != '-' and matchedLGARank[8].strip() != 'nc' and \
                        westernSydneyLGARank[8].strip() != '-' and westernSydneyLGARank[8].strip() != 'nc' and \
                        float(matchedLGARank[8]) < float(westernSydneyLGARank[8]):
                        return outputStr.format(matchedLGA,matchedLGARank[8],"robbery", westernSydneyLGA, westernSydneyLGARank[8])
                    elif matchedLGARank[11].strip() != '-' and matchedLGARank[11].strip() != 'nc' and \
                        westernSydneyLGARank[11].strip() != '-' and westernSydneyLGARank[11].strip() != 'nc' and \
                        float(matchedLGARank[11]) < float(westernSydneyLGARank[11]):
                        return outputStr.format(matchedLGA,matchedLGARank[11],"non-domestic assault", westernSydneyLGA, westernSydneyLGARank[11])
                    elif matchedLGARank[14].strip() != '-' and matchedLGARank[14].strip() != 'nc' and \
                        westernSydneyLGARank[14].strip() != '-' and westernSydneyLGARank[14].strip() != 'nc' and \
                        float(matchedLGARank[14]) < float(westernSydneyLGARank[14]):
                        return outputStr.format(matchedLGA,matchedLGARank[14],"domestic assault", westernSydneyLGA, westernSydneyLGARank[14])
                    elif matchedLGARank[17].strip() != '-' and matchedLGARank[17].strip() != 'nc' and \
                        westernSydneyLGARank[17].strip() != '-' and westernSydneyLGARank[17].strip() != 'nc' and \
                        float(matchedLGARank[17]) < float(westernSydneyLGARank[17]):
                        return outputStr.format(matchedLGA,matchedLGARank[17],"sexual offences", westernSydneyLGA, westernSydneyLGARank[17])

    return "Congrats, your area is just as safe as Western Sydney in 2013."

def getRedLightFines(compare, suburbs, westernSydneySuburbs):
    if len(suburbs) == 0:
        return None
    fines = getRedLightFinesBySuburb()
    avgnumber, avgfine = getRedLightFinesForSuburbs(suburbs, fines)
    avgnumberwest, avgfinewest = getRedLightFinesForSuburbs(westernSydneySuburbs, fines)
    if avgnumber is None:
        return None

    out = {}
    if avgnumber > avgnumberwest:
        out['heading'] = 'Safer driving'
        out['text'] = ['The average annual count of red light offences for Western Sydney is ' + str(avgnumberwest) +
                       ', as opposed to to ' + str(avgnumber) + ' in ' + compare + '.']
        return out
    elif avgfine > avgfinewest:
        out['heading'] = 'Affordable'
        out['text'] = ['The average red light fine for Western Sydney is $' + str(avgfinewest) +
                       ', as opposed to $' + str(avgfine) + ' in ' + compare + '.',
                       'You save $' + str(avgfine - avgfinewest) + '!']
        return out
    return None

def getRedLightFinesForSuburbs(suburbs, fines):
    totalnumber = 0
    totalfines = 0
    suburbsfound = 0
    for suburb in suburbs:
        if suburb in fines:
            totalnumber += fines[suburb][0]
            totalfines += fines[suburb][1]
            suburbsfound += 1
    if suburbsfound > 0:
        return totalnumber / suburbsfound, totalfines / suburbsfound
    return None, None

def getGreenTransport(compare, LGAs, westernSydneyLGAs):
    pctbike = getPercentCommutersByType(LGAs, 'bikeorwalk')
    pctpublic = getPercentCommutersByType(LGAs, 'publictransport')
    pctbikewest = getPercentCommutersByType(westernSydneyLGAs, 'bikeorwalk')
    pctpublicwest = getPercentCommutersByType(westernSydneyLGAs, 'publictransport')
    winratiobike = 0
    if pctbikewest > 0:
        winratiobike = (pctbikewest - pctbike) / pctbikewest
    winratiopublic = 0
    if pctpublicwest > 0:
        winratiopublic = (pctpublicwest - pctpublic) / pctpublicwest
    if winratiobike <= 0 and winratiopublic <= 0:
        return None
    out = {}
    out['heading'] = 'Sustainable living'
    if winratiobike > winratiopublic:
        out['text'] = [('%.1f' % (pctbikewest * 100)) + '% of Western Sydneysiders bicycled or walked to work.',
                       'That\'s more than the ' + ('%.1f' % (pctbike * 100)) + '% in ' + compare + '!']
    else:
        out['text'] = [('%.1f' % (pctpublicwest * 100)) + '% of Western Sydneysiders took public transport to work.',
                       'That\'s more than the ' + ('%.1f' % (pctpublic * 100)) + '% in ' + compare + '!']
    return out
<|endoftext|>


<commit_before>import datetime

from django.db import models
from django.contrib.comments.models import Comment
from django.contrib.auth.models import User
from django.contrib.contenttypes import generic
from django.db.models import permalink

from tagging.models import Tag, TaggedItem
from tagging.fields import TagField

from managers import PostManager, PostImageManager

class Series(models.Model):
    """
    Series of Posts
    """
    title = models.CharField(max_length=255)
    slug = models.SlugField(unique=True)
    summary = models.TextField(blank=True)
    preface = models.TextField(blank=True)
    created_on = models.DateTimeField(default=datetime.datetime.now)

    @models.permalink
    def get_absolute_url(self):
        """
        Returns the absolute url
        """
        return ('blog.views.series_detail', [self.slug,])

    class Meta:
        ordering = ('-created_on',)
        verbose_name_plural = 'series'

    def __unicode__(self):
        return self.title

class Category(models.Model):
    """
    Category
    """
    title = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    summary = models.TextField(blank=True)

    @models.permalink
    def get_absolute_url(self):
        """
        Returns the absolute url
        """
        return ('blog.views.category_detail', [self.slug,])

    class Meta:
        ordering = ('title',)
        verbose_name_plural = 'categories'

    def __unicode__(self):
        return self.title

class Post(models.Model):
    """
    Post
    """
    title = models.CharField(max_length=255)
    slug = models.SlugField(unique_for_date='publish_date')
    author = models.ForeignKey(User)
    publish_date = models.DateTimeField(default=datetime.datetime.now)
    last_modified = models.DateTimeField(default=datetime.datetime.now)
    is_published = models.BooleanField(default=True)
    is_featured = models.BooleanField(default=False,)
    allow_comments = models.BooleanField(default=True)
    categories = models.ManyToManyField(Category, blank=True, related_name='posts')
    series = models.ForeignKey(Series, blank=True, null=True, related_name='posts')
    tags = TagField()
    meta_keywords = models.CharField(max_length=255, blank=True)
    summary = models.TextField(help_text='Also doubles as the meta description')
    content = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)

    comments = generic.GenericRelation(Comment, object_id_field="object_pk")

    @models.permalink
    def get_absolute_url(self):
        """
        Returns the absolute url
        """
        return ('blog.views.post_detail', ['%04d' % self.publish_date.year, '%02d' % self.publish_date.month, '%02d' % self.publish_date.day, self.slug,])

    # Set the manager
    objects = PostManager()

    class Meta:
        ordering = ('-publish_date',)
        get_latest_by = 'publish_date'

    def __unicode__(self):
        if self.is_published:
            return self.title
        else:
            return '%s (DRAFT)' % (self.title,)

    @property
    def post_categories_string(self):
        """
        Returns the post categories in string format
        """
        return ', '.join([c.title for c in self.categories.all()])

    def get_previous_post(self):
        """
        Gets the previous post by publish_date
        """
        return self.get_previous_by_publish_date(is_published=True, publish_date__lt=datetime.datetime.now)

    def get_next_post(self):
        """
        Gets the next post by publish_date
        """
        return self.get_next_by_publish_date(is_published=True, publish_date__lt=datetime.datetime.now)

class PostImage(models.Model):
    """
    Blog Post Image
    """
    post = models.ForeignKey(Post, related_name="images")
    title = models.CharField(max_length=255)
    summary = models.TextField(blank=True)
    image = models.ImageField(upload_to="apps/blogyall/images")
    gallery_position = models.PositiveIntegerField(blank=True, null=True, help_text="Post Images without a Gallery Position will not appear in the post's gallery images")

    objects = PostImageManager()

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ('post', 'gallery_position', 'title',)
<commit_msg>added google ping capability to post save<commit_after>import datetime

from django.db import models
from django.contrib.comments.models import Comment
from django.contrib.auth.models import User
from django.contrib.contenttypes import generic
from django.contrib.sitemaps import ping_google
from django.db.models import permalink
from django.conf import settings

from tagging.models import Tag, TaggedItem
from tagging.fields import TagField

from managers import PostManager, PostImageManager

class Series(models.Model):
    """
    Series of Posts
    """
    title = models.CharField(max_length=255)
    slug = models.SlugField(unique=True)
    summary = models.TextField(blank=True)
    preface = models.TextField(blank=True)
    created_on = models.DateTimeField(default=datetime.datetime.now)

    @models.permalink
    def get_absolute_url(self):
        """
        Returns the absolute url
        """
        return ('blog.views.series_detail', [self.slug,])

    class Meta:
        ordering = ('-created_on',)
        verbose_name_plural = 'series'

    def __unicode__(self):
        return self.title

class Category(models.Model):
    """
    Category
    """
    title = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    summary = models.TextField(blank=True)

    @models.permalink
    def get_absolute_url(self):
        """
        Returns the absolute url
        """
        return ('blog.views.category_detail', [self.slug,])

    class Meta:
        ordering = ('title',)
        verbose_name_plural = 'categories'

    def __unicode__(self):
        return self.title

class Post(models.Model):
    """
    Post
    """
    title = models.CharField(max_length=255)
    slug = models.SlugField(unique_for_date='publish_date')
    author = models.ForeignKey(User)
    publish_date = models.DateTimeField(default=datetime.datetime.now)
    last_modified = models.DateTimeField(default=datetime.datetime.now)
    is_published = models.BooleanField(default=True)
    is_featured = models.BooleanField(default=False,)
    allow_comments = models.BooleanField(default=True)
    categories = models.ManyToManyField(Category, blank=True, related_name='posts')
    series = models.ForeignKey(Series, blank=True, null=True, related_name='posts')
    tags = TagField()
    meta_keywords = models.CharField(max_length=255, blank=True)
    summary = models.TextField(help_text='Also doubles as the meta description')
    content = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)

    comments = generic.GenericRelation(Comment, object_id_field="object_pk")

    @models.permalink
    def get_absolute_url(self):
        """
        Returns the absolute url
        """
        return ('blog.views.post_detail', ['%04d' % self.publish_date.year, '%02d' % self.publish_date.month, '%02d' % self.publish_date.day, self.slug,])

    # Set the manager
    objects = PostManager()

    def save(self, *args, **kwargs):
        if (getattr(settings, 'BLOG_PING_GOOGLE', False) == True) and (getattr(settings, 'DEBUG', True) == False):
            if self.is_published and self.publish_date <= datetime.datetime.now():
                if not self.pk:
                    try:
                        ping_google()
                    except:
                        pass
                else:
                    old_post = Post.objects.get(pk=self.pk)
                    if old_post.is_published == False:
                        try:
                            ping_google()
                        except:
                            pass
        super(Post, self).save(*args, **kwargs)

    class Meta:
        ordering = ('-publish_date',)
        get_latest_by = 'publish_date'

    def __unicode__(self):
        if self.is_published:
            return self.title
        else:
            return '%s (DRAFT)' % (self.title,)

    @property
    def post_categories_string(self):
        """
        Returns the post categories in string format
        """
        return ', '.join([c.title for c in self.categories.all()])

    def get_previous_post(self):
        """
        Gets the previous post by publish_date
        """
        return self.get_previous_by_publish_date(is_published=True, publish_date__lt=datetime.datetime.now)

    def get_next_post(self):
        """
        Gets the next post by publish_date
        """
        return self.get_next_by_publish_date(is_published=True, publish_date__lt=datetime.datetime.now)

class PostImage(models.Model):
    """
    Blog Post Image
    """
    post = models.ForeignKey(Post, related_name="images")
    title = models.CharField(max_length=255)
    summary = models.TextField(blank=True)
    image = models.ImageField(upload_to="apps/blogyall/images")
    gallery_position = models.PositiveIntegerField(blank=True, null=True, help_text="Post Images without a Gallery Position will not appear in the post's gallery images")

    objects = PostImageManager()

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ('post', 'gallery_position', 'title',)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Detect new translatable fields in all models and sync database structure.

You will need to execute this command in two cases:

    1. When you add new languages to settings.LANGUAGES.
    2. When you add new translatable fields to your models.

Credits: Heavily inspired by django-transmeta's sync_transmeta_db command.
"""
from optparse import make_option

from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.management.color import no_style
from django.db import connection, transaction
from django.db.models import get_models

from modeltranslation.translator import translator, NotRegistered
from modeltranslation.utils import build_localized_fieldname

def ask_for_confirmation(sql_sentences, model_full_name):
    print '\nSQL to synchronize "%s" schema:' % model_full_name
    for sentence in sql_sentences:
        print '   %s' % sentence
    while True:
        prompt = ('\nAre you sure that you want to execute the previous SQL: '
                  '(y/n) [n]: ')
        answer = raw_input(prompt).strip()
        if answer == '':
            return False
        elif answer not in ('y', 'n', 'yes', 'no'):
            print 'Please answer yes or no'
        elif answer == 'y' or answer == 'yes':
            return True
        else:
            return False

def print_missing_langs(missing_langs, field_name, model_name):
    print 'Missing languages in "%s" field from "%s" model: %s' % (
        field_name, model_name, ", ".join(missing_langs))

class Command(BaseCommand):
    help = ('Detect new translatable fields or new available languages and '
            'sync database structure')
    option_list = BaseCommand.option_list + (
        make_option('--noinput', action='store_false', dest='interactive', default=True,
            help='If provided, no prompts will be issued to the user and the database will be updated.'
        ),
    )

    def handle(self, *args, **options):
        """
        Command execution.
        """
        self.cursor = connection.cursor()
        self.introspection = connection.introspection
        interactive = options.get('interactive', True)

        all_models = get_models()
        found_missing_fields = False
        for model in all_models:
            try:
                options = translator.get_options_for_model(model)
                # options returns full-wide spectrum of localized fields but
                # we only to synchronize the local fields attached to the model.
                local_field_names = [field.name for field
                                     in model._meta.local_fields]
                translatable_fields = [field for field
                                       in options.localized_fieldnames
                                       if field in local_field_names]
                model_full_name = '%s.%s' % (model._meta.app_label,
                                             model._meta.module_name)
                db_table = model._meta.db_table
                for field_name in translatable_fields:
                    missing_langs = list(
                        self.get_missing_languages(field_name, db_table))
                    if missing_langs:
                        found_missing_fields = True
                        print_missing_langs(
                            missing_langs, field_name, model_full_name)
                        sql_sentences = self.get_sync_sql(
                            field_name, missing_langs, model)
                        execute_sql = not interactive or ask_for_confirmation(
                            sql_sentences, model_full_name)
                        if execute_sql:
                            print 'Executing SQL...',
                            for sentence in sql_sentences:
                                self.cursor.execute(sentence)
                            print 'Done'
                        else:
                            print 'SQL not executed'
            except NotRegistered:
                pass

        transaction.commit_unless_managed()

        if not found_missing_fields:
            print 'No new translatable fields detected'

    def get_table_fields(self, db_table):
        """
        Gets table fields from schema.
        """
        db_table_desc = self.introspection.get_table_description(
            self.cursor, db_table)
        return [t[0] for t in db_table_desc]

    def get_missing_languages(self, field_name, db_table):
        """
        Gets only missings fields.
        """
        db_table_fields = self.get_table_fields(db_table)
        for lang_code, lang_name in settings.LANGUAGES:
            if build_localized_fieldname(
                field_name, lang_code) not in db_table_fields:
                yield lang_code

    def get_sync_sql(self, field_name, missing_langs, model):
        """
        Returns SQL needed for sync schema for a new translatable field.
        """
        qn = connection.ops.quote_name
        style = no_style()
        sql_output = []
        db_table = model._meta.db_table
        for lang in missing_langs:
            new_field = build_localized_fieldname(field_name, lang)
            f = model._meta.get_field(new_field)
            col_type = f.db_type(connection)
            field_sql = [style.SQL_FIELD(qn(f.column)),
                         style.SQL_COLTYPE(col_type)]
            # column creation
            sql_output.append(
                "ALTER TABLE %s ADD COLUMN %s;" % (
                    qn(db_table), ' '.join(field_sql)))
            if not f.null and lang == settings.LANGUAGE_CODE:
                sql_output.append("ALTER TABLE %s MODIFY COLUMN %s %s %s;" % \
                                  (qn(db_table), qn(f.column), col_type,
                                  style.SQL_KEYWORD('NOT NULL')))
        return sql_output
<commit_msg>Added verbosity output handling.<commit_after># -*- coding: utf-8 -*-
"""
Detect new translatable fields in all models and sync database structure.

You will need to execute this command in two cases:

    1. When you add new languages to settings.LANGUAGES.
    2. When you add new translatable fields to your models.

Credits: Heavily inspired by django-transmeta's sync_transmeta_db command.
"""
from optparse import make_option

from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.management.color import no_style
from django.db import connection, transaction
from django.db.models import get_models

from modeltranslation.translator import translator, NotRegistered
from modeltranslation.utils import build_localized_fieldname

def ask_for_confirmation(sql_sentences, model_full_name):
    print '\nSQL to synchronize "%s" schema:' % model_full_name
    for sentence in sql_sentences:
        print '   %s' % sentence
    while True:
        prompt = ('\nAre you sure that you want to execute the previous SQL: '
                  '(y/n) [n]: ')
        answer = raw_input(prompt).strip()
        if answer == '':
            return False
        elif answer not in ('y', 'n', 'yes', 'no'):
            print 'Please answer yes or no'
        elif answer == 'y' or answer == 'yes':
            return True
        else:
            return False

def print_missing_langs(missing_langs, field_name, model_name):
    print 'Missing languages in "%s" field from "%s" model: %s' % (
        field_name, model_name, ", ".join(missing_langs))

class Command(BaseCommand):
    help = ('Detect new translatable fields or new available languages and '
            'sync database structure')
    option_list = BaseCommand.option_list + (
        make_option('--noinput', action='store_false', dest='interactive', default=True,
            help='If provided, no prompts will be issued to the user and the database will be updated.'
        ),
    )

    def handle(self, *args, **options):
        """
        Command execution.
        """
        self.cursor = connection.cursor()
        self.introspection = connection.introspection
        self.verbosity = int(options.get('verbosity', 1))
        self.interactive = options.get('interactive', True)

        all_models = get_models()
        found_missing_fields = False
        for model in all_models:
            try:
                options = translator.get_options_for_model(model)
                # options returns full-wide spectrum of localized fields but
                # we only to synchronize the local fields attached to the model.
                local_field_names = [field.name for field
                                     in model._meta.local_fields]
                translatable_fields = [field for field
                                       in options.localized_fieldnames
                                       if field in local_field_names]
                model_full_name = '%s.%s' % (model._meta.app_label,
                                             model._meta.module_name)
                db_table = model._meta.db_table
                for field_name in translatable_fields:
                    missing_langs = list(
                        self.get_missing_languages(field_name, db_table))
                    if missing_langs:
                        found_missing_fields = True
                        if self.verbosity:
                            print_missing_langs(
                                missing_langs, field_name, model_full_name)
                        sql_sentences = self.get_sync_sql(
                            field_name, missing_langs, model)
                        if (not self.interactive or
                            ask_for_confirmation(sql_sentences, model_full_name)):
                            if self.verbosity:
                                print 'Executing SQL...',
                            for sentence in sql_sentences:
                                self.cursor.execute(sentence)
                            if self.verbosity:
                                print 'Done'
                        else:
                            print 'SQL not executed'
            except NotRegistered:
                pass

        transaction.commit_unless_managed()

        if not found_missing_fields and self.verbosity:
            print 'No new translatable fields detected'

    def get_table_fields(self, db_table):
        """
        Gets table fields from schema.
        """
        db_table_desc = self.introspection.get_table_description(
            self.cursor, db_table)
        return [t[0] for t in db_table_desc]

    def get_missing_languages(self, field_name, db_table):
        """
        Gets only missings fields.
        """
        db_table_fields = self.get_table_fields(db_table)
        for lang_code, lang_name in settings.LANGUAGES:
            if build_localized_fieldname(
                field_name, lang_code) not in db_table_fields:
                yield lang_code

    def get_sync_sql(self, field_name, missing_langs, model):
        """
        Returns SQL needed for sync schema for a new translatable field.
        """
        qn = connection.ops.quote_name
        style = no_style()
        sql_output = []
        db_table = model._meta.db_table
        for lang in missing_langs:
            new_field = build_localized_fieldname(field_name, lang)
            f = model._meta.get_field(new_field)
            col_type = f.db_type(connection)
            field_sql = [style.SQL_FIELD(qn(f.column)),
                         style.SQL_COLTYPE(col_type)]
            # column creation
            sql_output.append(
                "ALTER TABLE %s ADD COLUMN %s;" % (
                    qn(db_table), ' '.join(field_sql)))
            if not f.null and lang == settings.LANGUAGE_CODE:
                sql_output.append("ALTER TABLE %s MODIFY COLUMN %s %s %s;" % \
                                  (qn(db_table), qn(f.column), col_type,
                                  style.SQL_KEYWORD('NOT NULL')))
        return sql_output
<|endoftext|>


<commit_before># gozerlib/utils/log.py
#
#

""" log module. """

## basic imports

import logging
import logging.handlers
import os

## defines

LEVELS = {'debug': logging.DEBUG,
          'info': logging.INFO,
          'warning': logging.WARNING,
          'warn': logging.WARNING,
          'error': logging.ERROR,
          'critical': logging.CRITICAL}

if not os.path.isdir("/var/log/jsonbot"): LOGDIR = os.getcwd() + os.sep + "jsonbot.logs"
else: LOGDIR = "/var/log/jsonbot"

try:
    if not os.path.isdir(LOGDIR): os.mkdir(LOGDIR)
except: pass

format = "%(asctime)s - %(levelname)s - %(message)s - <%(threadName)s+%(module)s-%(funcName)s:%(lineno)s>"

## setloglevel function

def setloglevel(level_name):
    """ set loglevel to level_name. """
    level = LEVELS.get(level_name, logging.NOTSET)
    root = logging.getLogger("")
    if root.handlers:
        for handler in root.handlers: root.removeHandler(handler)
    logging.basicConfig(level=level, format=format)
    #filehandler = logging.handlers.TimedRotatingFileHandler(LOGDIR + os.sep + "jsonbot.log", 'midnight')
    #formatter = logging.Formatter(format)
    #filehandler.setFormatter(formatter)
    #root.addHandler(filehandler)
    root.setLevel(level)
    logging.info("loglevel is %s (%s)" % (str(level), level_name))
<commit_msg>re-add logging to files<commit_after># gozerlib/utils/log.py
#
#

""" log module. """

## basic imports

import logging
import logging.handlers
import os
import getpass

## defines

LEVELS = {'debug': logging.DEBUG,
          'info': logging.INFO,
          'warning': logging.WARNING,
          'warn': logging.WARNING,
          'error': logging.ERROR,
          'critical': logging.CRITICAL}

if not os.path.isdir("/var/log/jsonbot"): LOGDIR = os.getcwd() + os.sep + "jsonbot.logs"
else: LOGDIR = "/var/log/jsonbot" + os.sep + getpass.getuser()

try:
    if not os.path.isdir(LOGDIR): os.mkdir(LOGDIR)
except: pass

format = "%(asctime)s - %(levelname)s - %(message)s - <%(threadName)s+%(module)s-%(funcName)s:%(lineno)s>"

filehandler = logging.handlers.TimedRotatingFileHandler(LOGDIR + os.sep + "jsonbot.log", 'midnight')
formatter = logging.Formatter(format)
filehandler.setFormatter(formatter)

## setloglevel function

def setloglevel(level_name):
    """ set loglevel to level_name. """
    level = LEVELS.get(level_name, logging.NOTSET)
    root = logging.getLogger("")
    if root.handlers:
        for handler in root.handlers: root.removeHandler(handler)
    logging.basicConfig(level=level, format=format)
    root.addHandler(filehandler)
    root.setLevel(level)
    logging.info("loglevel is %s (%s)" % (str(level), level_name))
<|endoftext|>


<commit_before>from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.views.generic import TemplateView
from django.views import generic,View
from django.core.files.base import ContentFile
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator

import json
from carton.cart import Cart
from shop.models import *

def add(request):
    cart = Cart(request.session)
    product = Product.objects.get(id=request.GET.get('id'))
    cart.add(product, price=product.price)
    return redirect('/shopping-cart/show')

def remove(request):
    cart = Cart(request.session)
    product = Product.objects.get(id=request.GET.get('id'))
    if product in cart:
        cart.remove(product)
    return redirect('/shopping-cart/show')

def remove_single(request):
    cart = Cart(request.session)
    product = Product.objects.get(id=request.GET.get('id'))
    if product in cart:
        cart.remove_single(product)
    return redirect('/shopping-cart/show')

def show(request):
    #cart = Cart(request.session)
    return render(request, 'shopping/shopping-cart.html')

class CheckoutView(View):
    template_name = "shopping/checkout.html"

    def get(self,request):
        return render(request,self.template_name,{})

    @method_decorator(login_required)
    def post(self, request):
        forms = {}
        user = MyUser.objects.get(id=request.user.id)
        for key in request.POST.keys():
            forms[key] = request.POST.get(key, '')
        cart = Cart(request.session)
        o = Order(status='P',
            total=cart.total,
            address=forms['address_line_1'],
            postcode=forms['postalcode'],
            name_oncard=forms['card-name'],
            card_number=forms['card-number'],
            expire_date=forms['exp-month'],
            expire_year=forms['exp-year'],
            card_cvc=forms['cvc'],
            member_id=user)
        temp = ContentFile(json.dumps(cart.cart_serializable))
        file_name = str(o.id) + ".json"
        o.product_list.save(file_name,temp)
        o.save()
        cart.clear()
        return HttpResponse(o.id)

<commit_msg>Fix order.html<commit_after>from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.views.generic import TemplateView
from django.views import generic,View
from django.core.files.base import ContentFile
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator

import json
from carton.cart import Cart
from shop.models import *

def add(request):
    cart = Cart(request.session)
    product = Product.objects.get(id=request.GET.get('id'))
    cart.add(product, price=product.price)
    return redirect('/shopping-cart/show')

def remove(request):
    cart = Cart(request.session)
    product = Product.objects.get(id=request.GET.get('id'))
    if product in cart:
        cart.remove(product)
    return redirect('/shopping-cart/show')

def remove_single(request):
    cart = Cart(request.session)
    product = Product.objects.get(id=request.GET.get('id'))
    if product in cart:
        cart.remove_single(product)
    return redirect('/shopping-cart/show')

def show(request):
    #cart = Cart(request.session)
    return render(request, 'shopping/shopping-cart.html')

class CheckoutView(View):
    template_name = "shopping/checkout.html"

    def get(self,request):
        return render(request,self.template_name,{})

    @method_decorator(login_required)
    def post(self, request):
        forms = {}
        user = MyUser.objects.get(id=request.user.id)
        for key in request.POST.keys():
            forms[key] = request.POST.get(key, '')
        cart = Cart(request.session)
        o = Order(status='P',
            total=cart.total,
            address=forms['address_line_1'],
            postcode=forms['postalcode'],
            name_oncard=forms['card-name'],
            card_number=forms['card-number'],
            expire_date=forms['exp-month'],
            expire_year=forms['exp-year'],
            card_cvc=forms['cvc'],
            member_id=user)
        temp = ContentFile(json.dumps(cart.cart_serializable))
        file_name = str(o.id) + ".json"
        o.product_list.save(file_name,temp)
        o.save()
        cart.clear()
        return redirect('/member/')

<|endoftext|>


<commit_before># Michael A. Alcorn (malcorn@redhat.com)
# A (modified) implementation of the Recurrent Convolutional Neural Network (RCNN) found in [1].
# [1] Siwei, L., Xu, L., Kang, L., and Zhao, J. 2015. Recurrent convolutional
#         neural networks for text classification. In AAAI, pp. 2267-2273.
#         http://www.aaai.org/ocs/index.php/AAAI/AAAI15/paper/view/9745

import gensim
import numpy as np
import random
import socket
import string
import sys
import time

from keras.layers import Dense, Input, Lambda, merge, LSTM, TimeDistributed
from keras.layers.embeddings import Embedding
from keras.models import Model

word2vec = gensim.models.Word2Vec.load("word2vec.gensim")
# We add an additional row of zeros to the embeddings matrix to represent unseen words and the NULL token.
embeddings = np.zeros((word2vec.syn0.shape[0] + 1, word2vec.syn0.shape[1]), dtype = "float32")
embeddings[:word2vec.syn0.shape[0]] = word2vec.syn0

MAX_TOKENS = word2vec.syn0.shape[0]
embedding_dim = word2vec.syn0.shape[1]
hidden_dim_1 = 200
hidden_dim_2 = 100
NUM_CLASSES = 10

document = Input(shape = (None, ), dtype = "int32")
left_context = Input(shape = (None, ), dtype = "int32")
right_context = Input(shape = (None, ), dtype = "int32")

embedder = Embedding(MAX_TOKENS + 1, embedding_dim, weights = [embeddings], trainable = False)
doc_embedding = embedder(document)
l_embedding = embedder(left_context)
r_embedding = embedder(right_context)

# I use LSTM RNNs instead of vanilla RNNs as described in the paper.
forward = LSTM(hidden_dim_1, return_sequences = True)(l_embedding) # See equation (1).
backward = LSTM(hidden_dim_1, return_sequences = True, go_backwards = True)(r_embedding) # See equation (2).
together = merge([forward, doc_embedding, backward], mode = "concat", concat_axis = 2) # See equation (3).

semantic = TimeDistributed(Dense(hidden_dim_2, activation = "tanh"))(together) # See equation (4).

# Keras provides its own max-pooling layers, but they cannot handle variable length input
# (as far as I can tell). As a result, I define my own max-pooling layer here.
pool_rnn = Lambda(lambda x: x.max(axis = 1), output_shape = (hidden_dim_2, ))(semantic) # See equation (5).

output = Dense(NUM_CLASSES, input_dim = hidden_dim_2, activation = "sigmoid")(pool_rnn) # See equations (6) and (7).

model = Model(input = [document, left_context, right_context], output = prob)
model.compile(optimizer = "adadelta", loss = "categorical_crossentropy", metrics = ["accuracy"])

doc_as_array = np.array([[1, 2, 3, 4]])
# We shift the document to the right to obtain the left-side contexts.
left_context_as_array = np.array([[MAX_TOKENS, 1, 2, 3]])
# We shift the document to the left to obtain the right-side contexts.
right_context_as_array = np.array([[2, 3, 4, MAX_TOKENS]])
target = np.array([NUM_CLASSES * [0]])
target(3) = 1

history = model.fit([doc_as_array, left_context_as_array, right_context_as_array], target, nb_epoch = 1, verbose = 0)
loss = history.history["loss"][0]
<commit_msg>Wrong variable name for output.<commit_after># Michael A. Alcorn (malcorn@redhat.com)
# A (modified) implementation of the Recurrent Convolutional Neural Network (RCNN) found in [1].
# [1] Siwei, L., Xu, L., Kang, L., and Zhao, J. 2015. Recurrent convolutional
#         neural networks for text classification. In AAAI, pp. 2267-2273.
#         http://www.aaai.org/ocs/index.php/AAAI/AAAI15/paper/view/9745

import gensim
import numpy as np
import random
import socket
import string
import sys
import time

from keras.layers import Dense, Input, Lambda, merge, LSTM, TimeDistributed
from keras.layers.embeddings import Embedding
from keras.models import Model

word2vec = gensim.models.Word2Vec.load("word2vec.gensim")
# We add an additional row of zeros to the embeddings matrix to represent unseen words and the NULL token.
embeddings = np.zeros((word2vec.syn0.shape[0] + 1, word2vec.syn0.shape[1]), dtype = "float32")
embeddings[:word2vec.syn0.shape[0]] = word2vec.syn0

MAX_TOKENS = word2vec.syn0.shape[0]
embedding_dim = word2vec.syn0.shape[1]
hidden_dim_1 = 200
hidden_dim_2 = 100
NUM_CLASSES = 10

document = Input(shape = (None, ), dtype = "int32")
left_context = Input(shape = (None, ), dtype = "int32")
right_context = Input(shape = (None, ), dtype = "int32")

embedder = Embedding(MAX_TOKENS + 1, embedding_dim, weights = [embeddings], trainable = False)
doc_embedding = embedder(document)
l_embedding = embedder(left_context)
r_embedding = embedder(right_context)

# I use LSTM RNNs instead of vanilla RNNs as described in the paper.
forward = LSTM(hidden_dim_1, return_sequences = True)(l_embedding) # See equation (1).
backward = LSTM(hidden_dim_1, return_sequences = True, go_backwards = True)(r_embedding) # See equation (2).
together = merge([forward, doc_embedding, backward], mode = "concat", concat_axis = 2) # See equation (3).

semantic = TimeDistributed(Dense(hidden_dim_2, activation = "tanh"))(together) # See equation (4).

# Keras provides its own max-pooling layers, but they cannot handle variable length input
# (as far as I can tell). As a result, I define my own max-pooling layer here.
pool_rnn = Lambda(lambda x: x.max(axis = 1), output_shape = (hidden_dim_2, ))(semantic) # See equation (5).

output = Dense(NUM_CLASSES, input_dim = hidden_dim_2, activation = "sigmoid")(pool_rnn) # See equations (6) and (7).

model = Model(input = [document, left_context, right_context], output = output)
model.compile(optimizer = "adadelta", loss = "categorical_crossentropy", metrics = ["accuracy"])

doc_as_array = np.array([[1, 2, 3, 4]])
# We shift the document to the right to obtain the left-side contexts.
left_context_as_array = np.array([[MAX_TOKENS, 1, 2, 3]])
# We shift the document to the left to obtain the right-side contexts.
right_context_as_array = np.array([[2, 3, 4, MAX_TOKENS]])
target = np.array([NUM_CLASSES * [0]])
target(3) = 1

history = model.fit([doc_as_array, left_context_as_array, right_context_as_array], target, nb_epoch = 1, verbose = 0)
loss = history.history["loss"][0]
<|endoftext|>


<commit_before>'''
Dependency --
'''
# vim:sw=4:et

from __future__ import generators

import gobject
import pango
import diacanvas
import gaphor
import gaphor.UML as UML
from gaphor.diagram import initialize_item

from relationship import RelationshipItem

STEREOTYPE_OPEN = '\xc2\xab' # '<<'
STEREOTYPE_CLOSE = '\xc2\xbb' # '>>'

class DependencyItem(RelationshipItem):
    """This class represents all types of dependencies.

    Normally a dependency looks like a dashed line woth an arrow head.
    The dependency can have a stereotype attached to it, stating the kind of
    dependency we're dealing with. The dependency kind can only be changed if
    the dependency is not connected to two items.

    In the special case of an Implementation dependency, where one end is
    connected to an InterfaceItem, the line is drawn as a solid line without
    arrowhead.
    """

    FONT = 'sans 10'

    dependency_popup_menu = (
	'separator',
	'Dependency type', (
	    'DependencyTypeDependency',
	    'DependencyTypeUsage',
	    'DependencyTypeRealization',
	    'DependencyTypeImplementation')
    )

    def __init__(self, id=None):
	self.dependency_type = UML.Dependency

        RelationshipItem.__init__(self, id)

        font = pango.FontDescription(self.FONT)
        self._stereotype = diacanvas.shape.Text()
        self._stereotype.set_font_description(font)
        self._stereotype.set_wrap_mode(diacanvas.shape.WRAP_NONE)
        self._stereotype.set_markup(False)

        self.set(head_fill_color=0, head_a=0.0, head_b=15.0, head_c=6.0, head_d=6.0)
        self._set_dashed()

    def save(self, save_func):
	RelationshipItem.save(self, save_func)
	save_func('dependency_type', self.dependency_type.__name__)

    def load(self, name, value):
	if name == 'dependency_type':
	    self.set_dependency_type(getattr(UML, value))
	else:
	    RelationshipItem.load(self, name, value)

    def get_popup_menu(self):
        if self.subject:
            return self.popup_menu
        else:
            return self.popup_menu + self.dependency_popup_menu

    def get_dependency_type(self):
	return self.dependency_type

    def set_dependency_type(self, dependency_type):
	self.dependency_type = dependency_type

	if dependency_type is UML.Usage:
	    self._stereotype.set_text(STEREOTYPE_OPEN + 'use' + STEREOTYPE_CLOSE)
	elif dependency_type is UML.Realization:
	    self._stereotype.set_text(STEREOTYPE_OPEN + 'realize' + STEREOTYPE_CLOSE)
	elif dependency_type is UML.Implementation:
	    self._stereotype.set_text(STEREOTYPE_OPEN + 'implements' + STEREOTYPE_CLOSE)
	else:
	    self._stereotype.set_text('')

    def _set_dashed(self):
        """Display a depenency as a dashed arrow, with optional stereotype.
        """
        if not self.get_property('has_head'):
            self.set(dash=(7.0, 5.0), has_head=1)
            self.set_dependency_type(self.dependency_type)

    def _set_solid(self):
        """Make a solid line, as used when a dependency is connected to an
        Interface.
        """
        if self.get_property('has_head'):
            self.set(dash=None, has_head=0)
            self._stereotype.set_text('')

    def update_label(self, p1, p2):
        w, h = self._stereotype.to_pango_layout(True).get_pixel_size()

        x = p1[0] > p2[0] and w + 2 or -2
        x = (p1[0] + p2[0]) / 2.0 - x
        y = p1[1] <= p2[1] and h or 0
        y = (p1[1] + p2[1]) / 2.0 - y

	self._stereotype.set_pos((x, y))

	return x, y, w, h

    def on_update (self, affine):
        RelationshipItem.on_update(self, affine)
        handles = self.handles
        middle = len(handles)/2
        b1 = self.update_label(handles[middle-1].get_pos_i(),
                                 handles[middle].get_pos_i())

        b2 = self.bounds
        self.set_bounds((min(b1[0], b2[0]), min(b1[1], b2[1]),
                         max(b1[2] + b1[0], b2[2]), max(b1[3] + b1[1], b2[3])))

    def on_shape_iter(self):
	for s in RelationshipItem.on_shape_iter(self):
	    yield s
	yield self._stereotype

    # Gaphor Connection Protocol

    def find_relationship(self, head_subject, tail_subject):
        """See RelationshipItem.find_relationship().
        """
        return self._find_relationship(head_subject, tail_subject,
                                       ('supplier', 'supplierDependency'),
                                       ('client', 'clientDependency'))

        for supplier in head_subject.supplierDependency:
            #print 'supplier', supplier, supplier.client, tail_subject
            if tail_subject in supplier.client:
                # Check if the dependency is not already in our diagram
                for item in supplier.presentation:
                    if item.canvas is self.canvas and item is not self:
                        break
                else:
                    return supplier

    def allow_connect_handle(self, handle, connecting_to):
        """See RelationshipItem.allow_connect_handle().
        """
        try:
            return isinstance(connecting_to.subject, UML.NamedElement)
        except AttributeError:
            return 0

    def confirm_connect_handle (self, handle):
        """See RelationshipItem.confirm_connect_handle().

        In case of an Implementation, the head should be connected to an
        Interface and the tail to a BehavioredClassifier.

        TODO: Should Class also inherit from BehavioredClassifier?
        """
        #print 'confirm_connect_handle', handle, self.subject

        c1 = self.handles[0].connected_to
        c2 = self.handles[-1].connected_to
        if c1 and c2:
            s1 = c1.subject
            s2 = c2.subject
            relation = self.find_relationship(s1, s2)
            if not relation:
                relation = gaphor.resource(UML.ElementFactory).create(self.dependency_type)
                relation.supplier = s1
                relation.client = s2
            self.subject = relation

    def confirm_disconnect_handle (self, handle, was_connected_to):
        """See RelationshipItem.confirm_disconnect_handle().
        """
        #print 'confirm_disconnect_handle', handle
        if self.subject:
            del self.subject

initialize_item(DependencyItem, UML.Dependency)
<commit_msg>*** empty log message ***<commit_after>'''
Dependency --
'''
# vim:sw=4:et

from __future__ import generators

import gobject
import pango
import diacanvas
import gaphor
import gaphor.UML as UML
from gaphor.diagram import initialize_item

from relationship import RelationshipItem

STEREOTYPE_OPEN = '\xc2\xab' # '<<'
STEREOTYPE_CLOSE = '\xc2\xbb' # '>>'

class DependencyItem(RelationshipItem):
    """This class represents all types of dependencies.

    Normally a dependency looks like a dashed line woth an arrow head.
    The dependency can have a stereotype attached to it, stating the kind of
    dependency we're dealing with. The dependency kind can only be changed if
    the dependency is not connected to two items.

    In the special case of an Implementation dependency, where one end is
    connected to an InterfaceItem: the line is drawn as a solid line without
    arrowhead.
    """

    FONT = 'sans 10'

    dependency_popup_menu = (
	'separator',
	'Dependency type', (
	    'DependencyTypeDependency',
	    'DependencyTypeUsage',
	    'DependencyTypeRealization',
	    'DependencyTypeImplementation')
    )

    def __init__(self, id=None):
	self.dependency_type = UML.Dependency

        RelationshipItem.__init__(self, id)

        font = pango.FontDescription(self.FONT)
        self._stereotype = diacanvas.shape.Text()
        self._stereotype.set_font_description(font)
        self._stereotype.set_wrap_mode(diacanvas.shape.WRAP_NONE)
        self._stereotype.set_markup(False)

        self.set(head_fill_color=0, head_a=0.0, head_b=15.0, head_c=6.0, head_d=6.0)
        self._set_line_style()

    def save(self, save_func):
	RelationshipItem.save(self, save_func)
	save_func('dependency_type', self.dependency_type.__name__)

    def load(self, name, value):
	if name == 'dependency_type':
	    self.set_dependency_type(getattr(UML, value))
	else:
	    RelationshipItem.load(self, name, value)

    def get_popup_menu(self):
        if self.subject:
            return self.popup_menu
        else:
            return self.popup_menu + self.dependency_popup_menu

    def get_dependency_type(self):
	return self.dependency_type

    def set_dependency_type(self, dependency_type):
	self.dependency_type = dependency_type
        self._set_line_style()

    def _set_stereotype_text(self):
        dependency_type = self.dependency_type

	if dependency_type is UML.Usage:
	    self._stereotype.set_text(STEREOTYPE_OPEN + 'use' + STEREOTYPE_CLOSE)
	elif dependency_type is UML.Realization:
	    self._stereotype.set_text(STEREOTYPE_OPEN + 'realize' + STEREOTYPE_CLOSE)
	elif dependency_type is UML.Implementation:
	    self._stereotype.set_text(STEREOTYPE_OPEN + 'implements' + STEREOTYPE_CLOSE)
	else:
	    self._stereotype.set_text('')

    def _set_line_style(self, c1=None):
        """Display a depenency as a dashed arrow, with optional stereotype.
        """
        c1 = c1 or self.handles[0].connected_to
        if c1 and self.dependency_type is UML.Implementation and isinstance(c1.subject, UML.Interface):
            if self.get_property('has_head'):
                print 'settinh'
                self.set(dash=None, has_head=0)
                print 'done;'
                self._stereotype.set_text('')
        else:
            if not self.get_property('has_head'):
                self.set(dash=(7.0, 5.0), has_head=1)
                self._set_stereotype_text()

    def update_label(self, p1, p2):
        w, h = self._stereotype.to_pango_layout(True).get_pixel_size()

        x = p1[0] > p2[0] and w + 2 or -2
        x = (p1[0] + p2[0]) / 2.0 - x
        y = p1[1] <= p2[1] and h or 0
        y = (p1[1] + p2[1]) / 2.0 - y

	self._stereotype.set_pos((x, y))

	return x, y, w, h

    def on_update (self, affine):
        RelationshipItem.on_update(self, affine)
        handles = self.handles
        middle = len(handles)/2
        b1 = self.update_label(handles[middle-1].get_pos_i(),
                                 handles[middle].get_pos_i())

        b2 = self.bounds
        self.set_bounds((min(b1[0], b2[0]), min(b1[1], b2[1]),
                         max(b1[2] + b1[0], b2[2]), max(b1[3] + b1[1], b2[3])))

    def on_shape_iter(self):
	for s in RelationshipItem.on_shape_iter(self):
	    yield s
	yield self._stereotype

    # Gaphor Connection Protocol

    def find_relationship(self, head_subject, tail_subject):
        """See RelationshipItem.find_relationship().
        """
        return self._find_relationship(head_subject, tail_subject,
                                       ('supplier', 'supplierDependency'),
                                       ('client', 'clientDependency'))

        for supplier in head_subject.supplierDependency:
            #print 'supplier', supplier, supplier.client, tail_subject
            if tail_subject in supplier.client:
                # Check if the dependency is not already in our diagram
                for item in supplier.presentation:
                    if item.canvas is self.canvas and item is not self:
                        break
                else:
                    return supplier

    def allow_connect_handle(self, handle, connecting_to):
        """See RelationshipItem.allow_connect_handle().
        """
        try:
            return isinstance(connecting_to.subject, UML.NamedElement)
        except AttributeError:
            return 0

    def confirm_connect_handle (self, handle):
        """See RelationshipItem.confirm_connect_handle().

        In case of an Implementation, the head should be connected to an
        Interface and the tail to a BehavioredClassifier.

        TODO: Should Class also inherit from BehavioredClassifier?
        """
        #print 'confirm_connect_handle', handle, self.subject
        c1 = self.handles[0].connected_to
        self._set_line_style(c1)

        c2 = self.handles[-1].connected_to
        if c1 and c2:
            s1 = c1.subject
            s2 = c2.subject
            relation = self.find_relationship(s1, s2)
            if not relation:
                relation = gaphor.resource(UML.ElementFactory).create(self.dependency_type)
                relation.supplier = s1
                relation.client = s2
            self.subject = relation

    def confirm_disconnect_handle (self, handle, was_connected_to):
        """See RelationshipItem.confirm_disconnect_handle().
        """
        #print 'confirm_disconnect_handle', handle
        self._set_line_style()
        if self.subject:
            del self.subject

initialize_item(DependencyItem, UML.Dependency)
<|endoftext|>


<commit_before>from django.db import models
from django.contrib.auth.models import User

from hadrian.utils import slugs
from ckeditor.fields import RichTextField
from taggit.managers import TaggableManager

from .managers import PostManager

class Post(models.Model):
    title = models.CharField(blank=False, max_length=450)
    slug = models.SlugField(unique=True)
    image = models.ImageField(blank=True, upload_to='blog/images', null=True)
    body = RichTextField()
    excerpt = models.TextField(blank=True, null=True)
    meta_description = models.CharField(blank=True, max_length=350, help_text='Meta Description for SEO')
    author = models.ForeignKey(User)
    published_date = models.DateTimeField()
    published = models.BooleanField()

    tags = TaggableManager()

    objects = PostManager()

    def __unicode__(self):
        return self.title

    @property
    def get_author_display(self):
        return "%s %s" % (self.author.first_name, self.author.last_name)

    def save(self, *args, **kwargs):
        slugs.unique_slugify(self, self.title)
        super(Post, self).save(*args, **kwargs)

    @models.permalink
    def get_absolute_url(self):
        return ('post_detail_view', {}, {'slug': self.slug})

<commit_msg>Add sort by publish date.<commit_after>from django.db import models
from django.contrib.auth.models import User

from hadrian.utils import slugs
from ckeditor.fields import RichTextField
from taggit.managers import TaggableManager

from .managers import PostManager

class Post(models.Model):
    title = models.CharField(blank=False, max_length=450)
    slug = models.SlugField(unique=True)
    image = models.ImageField(blank=True, upload_to='blog/images', null=True)
    body = RichTextField()
    excerpt = models.TextField(blank=True, null=True)
    meta_description = models.CharField(blank=True, max_length=350, help_text='Meta Description for SEO')
    author = models.ForeignKey(User)
    published_date = models.DateTimeField()
    published = models.BooleanField()

    tags = TaggableManager()

    objects = PostManager()

    def __unicode__(self):
        return self.title

    @property
    def get_author_display(self):
        return "%s %s" % (self.author.first_name, self.author.last_name)

    def save(self, *args, **kwargs):
        slugs.unique_slugify(self, self.title)
        super(Post, self).save(*args, **kwargs)

    @models.permalink
    def get_absolute_url(self):
        return ('post_detail_view', {}, {'slug': self.slug})

    class Meta:
    	ordering = ['-published_date']

<|endoftext|>


<commit_before>import math

# Fixup for Python3
try:
    xrange
except:
    xrange = range

_64_1 = int('1'*64, 2)

def _hashlist(hash, num):
    # Takes a 128-bit hash and creates 'num' number of hashes.
    hash = int(hash, 16)
    l = hash >> 64
    r = hash & _64_1
    return list(
                map(
                    lambda x: (l << 64) | (x*r),
                    xrange(1, num+1)
                )
           )

class BloomFilter(object):
    def __init__(self, count=10000, error=0.01):
        self.count = count # Count(n)
        self.error = 0.01 # Error(p)
        self.nbits = (-1*count*math.log(error))/(math.log(2)**2)
        self.nbits = int(math.ceil(self.nbits))
        self.nbits += (self.nbits % 8) # Bits (m)
        self.functions =  int(math.ceil(
                                (self.nbits/count) * math.log(2)))# Functions(k)

        self.bitarray = bytearray([0] * int(self.nbits/8))
        self.items = 0

    def add(self, hash):
        hashlist = _hashlist(hash, self.functions)

        for hash in hashlist:
            x = hash % self.nbits
            self.bitarray[x//8] |= 1<<(x%8)

        self.items += 1

    def contains(self, hash):
        hashlist = _hashlist(hash, self.functions)

        for hash in hashlist:
            x = hash % self.nbits
            if not (self.bitarray[x//8] & 1<<(x%8)):
                return False

        return True

    def __contains__(self, hash):
        return self.contains(hash)
<commit_msg>Add license and author information<commit_after>__author__ = "Suresh Sundriyal"
__version__ = "0.0.1"
__license__ = "BSD 2-clause"

import math

# Fixup for Python3
try:
    xrange
except:
    xrange = range

_64_1 = int('1'*64, 2)

def _hashlist(hash, num):
    # Takes a 128-bit hash and creates 'num' number of hashes.
    hash = int(hash, 16)
    l = hash >> 64
    r = hash & _64_1
    return list(
                map(
                    lambda x: (l << 64) | (x*r),
                    xrange(1, num+1)
                )
           )

class BloomFilter(object):
    def __init__(self, count=10000, error=0.01):
        self.count = count # Count(n)
        self.error = 0.01 # Error(p)
        self.nbits = (-1*count*math.log(error))/(math.log(2)**2)
        self.nbits = int(math.ceil(self.nbits))
        self.nbits += (self.nbits % 8) # Bits (m)
        self.functions =  int(math.ceil(
                                (self.nbits/count) * math.log(2)))# Functions(k)

        self.bitarray = bytearray([0] * int(self.nbits/8))
        self.items = 0

    def add(self, hash):
        hashlist = _hashlist(hash, self.functions)

        for hash in hashlist:
            x = hash % self.nbits
            self.bitarray[x//8] |= 1<<(x%8)

        self.items += 1

    def contains(self, hash):
        hashlist = _hashlist(hash, self.functions)

        for hash in hashlist:
            x = hash % self.nbits
            if not (self.bitarray[x//8] & 1<<(x%8)):
                return False

        return True

    def __contains__(self, hash):
        return self.contains(hash)
<|endoftext|>


<commit_before># TODO: add tests where variables are preapplied to constraints
# TODO: add tests for feasible and infeasible constraints
# TODO: Compute eps and M based on x and A, B, dt
# TODO: encode STL robustness metric
# TODO: make inital conditions part of phi
# TODO: implement IIS via slacks
# TODO: weight IIS slacks based priority
# TODO: Look into using SMT
# TODO: Add constraint that x < M

from __future__ import division

from itertools import product, chain, starmap
import operator as op
from math import ceil
from collections import defaultdict, Counter, namedtuple
from functools import partial, singledispatch

import pulp as lp
import funcy as fn
from funcy import cat, mapcat, pluck, group_by, drop, walk_values, compose

import stl
from blustl import game
from blustl.game import Game
from blustl.constraint_kinds import Kind as K, Kind
from blustl.constraint_kinds import Category as C

DEFAULT_NAME = 'controller_synth'

Result = namedtuple("Result", ["feasible", "model", "cost", "solution"])

def z(x:"SL", i:int, g:Game):
    # TODO: come up with better function name
    cat = C.Bool if isinstance(x, stl.LinEq) else C.Real
    if isinstance(x, stl.LinEq):
        prefix = "q"
    else:
        prefix = "z"
    kwargs = {"name": "{}{}".format(prefix, i)}
    if str(x[0]) in g.model.bounds:
        lo, hi = g.model.bounds.get(str(x[0]))
        kwargs = {"lowBound": lo, "upBound": hi, "name": f"{x[0]}_{x[1]}"}
    return lp.LpVariable(cat=cat.value, **kwargs)

def add_constr(model, constr, kind:K, i:int):
    name = "{}{}".format(kind.name, i)
    model.addConstraint(constr, name=name)

def sl_to_milp(phi:"SL", g:Game, assigned=None, p1=True):
    """STL -> MILP"""
    # TODO: port to new Signal Logic based API
    # TODO: optimize away top level Ands
    model = lp.LpProblem(DEFAULT_NAME, lp.LpMaximize)
    lp_vars = set(game.vars_in_phi(phi))

    nodes = set(stl.walk(phi))
    store = {x: z(x, i, g) for i, x in enumerate(nodes | lp_vars)}

    stl_constr = cat(encode(phi, store) for phi in nodes)
    constraints = chain(
        stl_constr,
        [(store[phi] == 1, K.ASSERT_FEASIBLE)] # Assert Feasibility
    )

    for i, (constr, kind) in enumerate(constraints):
        add_constr(model, constr, kind, i)

    # TODO: support alternative objective functions
    model.setObjective(store[phi])

    return model, store

@singledispatch
def encode(psi, s):
    raise NotImplementedError(psi)

@encode.register(stl.LinEq)
def _(psi, s:dict):
    x = sum(float(term.coeff)*s[(term.id, term.time)] for term in psi.terms)
    if psi.op == "=":
        yield x == psi.const, K.PRED_EQ
    else:
        z_t = s[psi]

        M = 1000  # TODO
        # TODO: come up w. better value for eps
        eps = 0.01

        mu = x - psi.const if psi.op in ("<", "<=", "=") else psi.const -x
        yield -mu <= M * z_t - eps, K.PRED_UPPER
        yield mu <= M * (1 - z_t) - eps, K.PRED_LOWER

@encode.register(stl.Neg)
def _(phi, s:dict):
    yield s[phi] == 1 - s[phi.arg], K.NEG

def encode_op(phi:"SL", s:dict, *, k:Kind, isor:bool):
    z_phi = s[phi]
    elems = [s[psi] for psi in phi.args]
    rel, const = (op.ge, 0) if isor else (op.le, 1 - len(elems))

    for e in elems:
        yield rel(z_phi, e), k[0]
    yield rel(const + sum(elems), z_phi), k[1]

encode.register(stl.Or)(partial(encode_op, k=(K.OR, K.OR_TOTAL), isor=True))
encode.register(stl.And)(partial(encode_op, k=(K.AND, K.AND_TOTAL), isor=False))

def encode_and_run(phi, g):
    model, store = sl_to_milp(phi, g)
    status = lp.LpStatus[model.solve(lp.solvers.COIN())]

    if status in ('Infeasible', 'Unbounded'):
        return Result(False, model, None, None)

    elif status == "Optimal":
        f = lambda x: x[0][0]
        f2 = lambda x: (tuple(map(int, x[0][1:].split('_'))), x[1])
        f3 = compose(tuple, sorted, partial(map, f2))
        variables = {v: (k[1], k[0], v) for k, v in store.items()
                     if not isinstance(k[0], tuple)}

        sol = filter(None, map(variables.get, model.variables()))
        sol = fn.group_by(op.itemgetter(0), sol)
        sol = {t: {y[1]: y[2].value() for y in x} for t, x in sol.items()}
        cost = model.objective.value()
        return Result(True, model, cost, sol)
    else:
        raise NotImplementedError(status)
<commit_msg>propogate epsilon testing to solver<commit_after># TODO: add tests where variables are preapplied to constraints
# TODO: add tests for feasible and infeasible constraints
# TODO: Compute eps and M based on x and A, B, dt
# TODO: encode STL robustness metric
# TODO: make inital conditions part of phi
# TODO: implement IIS via slacks
# TODO: weight IIS slacks based priority
# TODO: Look into using SMT
# TODO: Add constraint that x < M

from __future__ import division

from itertools import product, chain, starmap
import operator as op
from math import ceil
from collections import defaultdict, Counter, namedtuple
from functools import partial, singledispatch

import pulp as lp
import funcy as fn
from funcy import cat, mapcat, pluck, group_by, drop, walk_values, compose

import stl
from blustl import game
from blustl.game import Game
from blustl.constraint_kinds import Kind as K, Kind
from blustl.constraint_kinds import Category as C

DEFAULT_NAME = 'controller_synth'

Result = namedtuple("Result", ["feasible", "model", "cost", "solution"])

def z(x:"SL", i:int, g:Game):
    # TODO: come up with better function name
    cat = C.Bool if isinstance(x, stl.LinEq) else C.Real
    if isinstance(x, stl.LinEq):
        prefix = "q"
    else:
        prefix = "z"
    kwargs = {"name": "{}{}".format(prefix, i)}
    if str(x[0]) in g.model.bounds:
        lo, hi = g.model.bounds.get(str(x[0]))
        kwargs = {"lowBound": lo, "upBound": hi, "name": f"{x[0]}_{x[1]}"}
    return lp.LpVariable(cat=cat.value, **kwargs)

def add_constr(model, constr, kind:K, i:int):
    name = "{}{}".format(kind.name, i)
    model.addConstraint(constr, name=name)

def sl_to_milp(phi:"SL", g:Game, assigned=None, p1=True):
    """STL -> MILP"""
    # TODO: port to new Signal Logic based API
    # TODO: optimize away top level Ands
    model = lp.LpProblem(DEFAULT_NAME, lp.LpMaximize)
    lp_vars = set(game.vars_in_phi(phi))

    nodes = set(stl.walk(phi))
    store = {x: z(x, i, g) for i, x in enumerate(nodes | lp_vars)}

    stl_constr = cat(encode(phi, store) for phi in nodes)
    constraints = chain(
        stl_constr,
        [(store[phi] == 1, K.ASSERT_FEASIBLE)] # Assert Feasibility
    )

    for i, (constr, kind) in enumerate(constraints):
        add_constr(model, constr, kind, i)

    # TODO: support alternative objective functions
    model.setObjective(store[phi])

    return model, store

@singledispatch
def encode(psi, s):
    raise NotImplementedError(psi)

@encode.register(stl.LinEq)
def _(psi, s:dict):
    x = sum(float(term.coeff)*s[(term.id, term.time)] for term in psi.terms)
    if psi.op == "=":
        yield x == psi.const, K.PRED_EQ
    else:
        z_t = s[psi]

        M = 1000  # TODO
        # TODO: come up w. better value for eps
        compare = op.lt if psi.op in ("<", ">=") else op.le
        mu = x - psi.const if psi.op in ("<", "<=") else psi.const -x
        yield compare(-mu, M * z_t ), K.PRED_UPPER
        yield compare(mu, M * (1 - z_t)) , K.PRED_LOWER

@encode.register(stl.Neg)
def _(phi, s:dict):
    yield s[phi] == 1 - s[phi.arg], K.NEG

def encode_op(phi:"SL", s:dict, *, k:Kind, isor:bool):
    z_phi = s[phi]
    elems = [s[psi] for psi in phi.args]
    rel, const = (op.ge, 0) if isor else (op.le, 1 - len(elems))

    for e in elems:
        yield rel(z_phi, e), k[0]
    yield rel(const + sum(elems), z_phi), k[1]

encode.register(stl.Or)(partial(encode_op, k=(K.OR, K.OR_TOTAL), isor=True))
encode.register(stl.And)(partial(encode_op, k=(K.AND, K.AND_TOTAL), isor=False))

def encode_and_run(phi, g):
    model, store = sl_to_milp(phi, g)
    status = lp.LpStatus[model.solve(lp.solvers.COIN())]

    if status in ('Infeasible', 'Unbounded'):
        return Result(False, model, None, None)

    elif status == "Optimal":
        f = lambda x: x[0][0]
        f2 = lambda x: (tuple(map(int, x[0][1:].split('_'))), x[1])
        f3 = compose(tuple, sorted, partial(map, f2))
        variables = {v: (k[1], k[0], v) for k, v in store.items()
                     if not isinstance(k[0], tuple)}

        sol = filter(None, map(variables.get, model.variables()))
        sol = fn.group_by(op.itemgetter(0), sol)
        sol = {t: {y[1]: y[2].value() for y in x} for t, x in sol.items()}
        cost = model.objective.value()
        return Result(True, model, cost, sol)
    else:
        raise NotImplementedError(status)
<|endoftext|>


<commit_before>import re

import feedgenerator
import pytz
import tweepy
from flask import Blueprint

from twootfeed import param, twitter_api

twitter_bp = Blueprint('twitter', __name__)

def format_tweet(i):
    tweet = {'text': i.full_text, 'screen_name': i.user.screen_name,
             'profile_image_url': i.user.profile_image_url_https,
             'user_name': i.user.name, 'user_url': i.user.url,
             'id_str': i.id_str, 'created_at': i.created_at, 'source': i.source,
             'retweets': i.retweet_count, 'favorites': i.favorite_count}
    tweet['tweet_url'] = 'https://twitter.com/' + tweet[
        'user_name'] + '/status/' + tweet['id_str']

    tweet['htmltext'] = '

   ' + tweet['user_name'] + \
        ': ' + tweet['text'] + \
        '
Source: ' + tweet[
            'source'] + ''

    user_mentionslist = i.entities.get('user_mentions')
    for user in user_mentionslist:
        if user != '':
            tweet['htmltext'] = re.sub(
                ('@' + user.get('screen_name')),
                ('@'
                 + user.get('screen_name') + ''),
                tweet['htmltext'])

    hashtaglist = i.entities.get('hashtags')
    for hashtag in hashtaglist:
        if hashtag != '':
            tweet['htmltext'] = re.sub(
                ('#' + hashtag.get('text')),
                ('#'
                 + hashtag.get('text') + ''),
                tweet['htmltext']
            )

    urllist = i.entities.get('urls')
    for url in urllist:
        if url != '':
            tweet['htmltext'] = re.sub(
                (url.get('url')),
                (''
                 + url.get('display_url')
                 + ''),
                tweet['htmltext'])
    try:
        medialist = i.extended_entities.get('media')
    except AttributeError:
        pass
    else:
        if medialist is not None:
            tweet['htmltext'] = tweet['htmltext'] + '
 '
            for media in medialist:
                if media != '':
                    if media.get('type') == 'photo':
                        tweet['htmltext'] = re.sub(
                            media.get('url'), '',
                            tweet['htmltext']
                        )
                        tweet['htmltext'] = tweet['htmltext'] \
                                            + ' ' \
                                              ' ' \
                                            + ''

    location = i.place
    if location is not None:
        tweet['htmltext'] = tweet['htmltext'] \
                            + '
Location: ' \
                            + location.full_name + ''

    tweet['htmltext'] = tweet['htmltext'] + '
 ♻ : ' + str(
        tweet['retweets']) + ', ' + '♥ : ' + str(
        tweet['favorites']) + '

'

    return tweet

@twitter_bp.route('/<query_feed>', methods=['GET'])
@twitter_bp.route('/tweets/<query_feed>', methods=['GET'])
def tweetfeed(query_feed):
    """ generate a rss feed from parsed twitter search """

    if twitter_api:
        buffered = []

        for i in tweepy.Cursor(twitter_api.search,
                               q=query_feed,
                               tweet_mode='extended').items():
            try:
                i.full_text
            except Exception:
                break
            else:
                try:
                    retweeted_status = i.retweeted_status

                except Exception:
                    retweeted_status = False

                if not retweeted_status:  # only the original tweets
                    tweet = format_tweet(i)
                    buffered.append(tweet.copy())

        utc = pytz.utc
        f = feedgenerator.Rss201rev2Feed(
            title=param['twitter']['title'] + '"' + query_feed + '"',
            link=param['twitter']['link'] + query_feed,
            description=param['twitter']['description'],
            language=param['feed']['language'],
            author_name=param['feed']['author_name'],
            feed_url=param['feed']['feed_url'])

        for tweet in buffered:
            f.add_item(
                title=tweet['screen_name']
                + ' ('
                + tweet['user_name'] + '): '
                + tweet['text'],
                link=tweet['tweet_url'],
                pubdate=utc.localize(tweet['created_at']).astimezone(
                    pytz.timezone(param['feed']['timezone'])),
                description=tweet['htmltext'])

        xml = f.writeString('UTF-8')
    else:
        xml = 'error - Twitter parameters not defined'

    return xml
<commit_msg>refactor (remove useless loop) in twitter route<commit_after>import re

import pytz
import tweepy
from flask import Blueprint

from twootfeed import param, twitter_api
from twootfeed.utils.feed_generation import generate_feed

twitter_bp = Blueprint('twitter', __name__)

def format_tweet(i):
    tweet = {'text': i.full_text, 'screen_name': i.user.screen_name,
             'profile_image_url': i.user.profile_image_url_https,
             'user_name': i.user.name, 'user_url': i.user.url,
             'id_str': i.id_str, 'created_at': i.created_at, 'source': i.source,
             'retweets': i.retweet_count, 'favorites': i.favorite_count}
    tweet['tweet_url'] = 'https://twitter.com/' + tweet[
        'user_name'] + '/status/' + tweet['id_str']

    tweet['htmltext'] = '

   ' + tweet['user_name'] + \
        ': ' + tweet['text'] + \
        '
Source: ' + tweet[
            'source'] + ''

    user_mentionslist = i.entities.get('user_mentions')
    for user in user_mentionslist:
        if user != '':
            tweet['htmltext'] = re.sub(
                ('@' + user.get('screen_name')),
                ('@'
                 + user.get('screen_name') + ''),
                tweet['htmltext'])

    hashtaglist = i.entities.get('hashtags')
    for hashtag in hashtaglist:
        if hashtag != '':
            tweet['htmltext'] = re.sub(
                ('#' + hashtag.get('text')),
                ('#'
                 + hashtag.get('text') + ''),
                tweet['htmltext']
            )

    urllist = i.entities.get('urls')
    for url in urllist:
        if url != '':
            tweet['htmltext'] = re.sub(
                (url.get('url')),
                (''
                 + url.get('display_url')
                 + ''),
                tweet['htmltext'])
    try:
        medialist = i.extended_entities.get('media')
    except AttributeError:
        pass
    else:
        if medialist is not None:
            tweet['htmltext'] = tweet['htmltext'] + '
 '
            for media in medialist:
                if media != '':
                    if media.get('type') == 'photo':
                        tweet['htmltext'] = re.sub(
                            media.get('url'), '',
                            tweet['htmltext']
                        )
                        tweet['htmltext'] = tweet['htmltext'] \
                                            + ' ' \
                                              ' ' \
                                            + ''

    location = i.place
    if location is not None:
        tweet['htmltext'] = tweet['htmltext'] \
                            + '
Location: ' \
                            + location.full_name + ''

    tweet['htmltext'] = tweet['htmltext'] + '
 ♻ : ' + str(
        tweet['retweets']) + ', ' + '♥ : ' + str(
        tweet['favorites']) + '

'

    return tweet

@twitter_bp.route('/<query_feed>', methods=['GET'])
@twitter_bp.route('/tweets/<query_feed>', methods=['GET'])
def tweetfeed(query_feed):
    """ generate a rss feed from parsed twitter search """

    if twitter_api:
        feed_title = param['twitter']['title'] + '"' + query_feed + '"'
        feed_link = param['twitter']['link'] + query_feed
        f = generate_feed(feed_title, feed_link, param)
        utc = pytz.utc

        for i in tweepy.Cursor(twitter_api.search,
                               q=query_feed,
                               tweet_mode='extended').items():
            try:
                i.full_text
            except Exception:
                break
            else:
                try:
                    retweeted_status = i.retweeted_status

                except Exception:
                    retweeted_status = False

                if not retweeted_status:  # only the original tweets
                    tweet = format_tweet(i)
                    f.add_item(
                        title=tweet['screen_name']
                        + ' ('
                        + tweet['user_name'] + '): '
                        + tweet['text'],
                        link=tweet['tweet_url'],
                        pubdate=utc.localize(tweet['created_at']).astimezone(
                                pytz.timezone(param['feed']['timezone'])),
                        description=tweet['htmltext'])

        xml = f.writeString('UTF-8')
    else:
        xml = 'error - Twitter parameters not defined'

    return xml
<|endoftext|>