<s> """<STR_LIT>""" <EOL> from math import sqrt , pi <EOL> from django . contrib . gis . geos import Point , LineString <EOL> from django . contrib . humanize . templatetags . humanize import intcomma <EOL> from django . utils import simplejson as json <EOL> from django . utils . translation import ugettext as _ <EOL> from django . template import Template , Context <EOL> from decimal import Decimal <EOL> from copy import copy <EOL> import random <EOL> from django . db . models import Q <EOL> import operator <EOL> import itertools <EOL> import redis <EOL> from django . conf import settings <EOL> from redisutils import key_gen <EOL> redis_settings = settings . KEY_VALUE_STORE <EOL> class CalculatorBase ( object ) : <EOL> """<STR_LIT>""" <EOL> result = None <EOL> arg_dict = { } <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . result = None <EOL> self . arg_dict = { } <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def sortkey ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> return self . result [ '<STR_LIT:value>' ] <EOL> return self . result <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> return self . template ( '<STR_LIT>' ) ; <EOL> if not self . result is None and '<STR_LIT>' in self . result : <EOL> return self . result [ '<STR_LIT>' ] <EOL> return self . empty_html_result <EOL> empty_html_result = '<STR_LIT>' % _ ( '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> def json ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> output = { '<STR_LIT:result>' : self . result [ '<STR_LIT:value>' ] } <EOL> else : <EOL> output = { '<STR_LIT:result>' : None } <EOL> return json . dumps ( output , use_decimal = True ) <EOL> def template ( self , template , context = None ) : <EOL> """<STR_LIT>""" <EOL> t = Template ( template ) <EOL> c = Context ( { '<STR_LIT:result>' : self . result } ) <EOL> if context is not None : <EOL> c . update ( context ) <EOL> return t . render ( c ) <EOL> def percentage ( self , span = False ) : <EOL> """<STR_LIT>""" <EOL> if span : <EOL> t = Template ( '<STR_LIT>' ) <EOL> else : <EOL> t = Template ( '<STR_LIT>' ) <EOL> c = Context ( { '<STR_LIT>' : self . result [ '<STR_LIT:value>' ] * <NUM_LIT:100> } ) <EOL> return t . render ( c ) <EOL> def get_value ( self , argument , district = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ( argtype , argval ) = self . arg_dict [ argument ] <EOL> except : <EOL> return None <EOL> value = None <EOL> if argtype == '<STR_LIT>' : <EOL> value = argval <EOL> if isinstance ( argval , dict ) and '<STR_LIT:value>' in argval : <EOL> value = Decimal ( argval [ '<STR_LIT:value>' ] ) <EOL> else : <EOL> try : <EOL> value = Decimal ( value ) <EOL> except : <EOL> pass <EOL> elif argtype == '<STR_LIT>' and not district is None : <EOL> add_subject = True <EOL> if argval . startswith ( '<STR_LIT:->' ) : <EOL> add_subject = False <EOL> argval = argval [ <NUM_LIT:1> : ] <EOL> cc = district . computedcharacteristic_set . filter ( subject__name = argval ) <EOL> if cc . count ( ) > <NUM_LIT:0> : <EOL> value = cc [ <NUM_LIT:0> ] . number if add_subject else - cc [ <NUM_LIT:0> ] . number <EOL> return value <EOL> class Schwartzberg ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> districts = [ ] <EOL> if '<STR_LIT>' in kwargs : <EOL> districts = [ kwargs [ '<STR_LIT>' ] ] <EOL> if districts [ <NUM_LIT:0> ] . geom . empty : <EOL> return <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = True ) <EOL> else : <EOL> return <EOL> num = <NUM_LIT:0> <EOL> compactness = <NUM_LIT:0> <EOL> for district in districts : <EOL> if district . district_id == <NUM_LIT:0> : <EOL> continue <EOL> if district . geom . empty : <EOL> continue <EOL> if district . geom . length == <NUM_LIT:0> : <EOL> continue <EOL> r = sqrt ( district . geom . area / pi ) <EOL> circumference = <NUM_LIT:2> * pi * r <EOL> compactness += circumference / district . geom . length <EOL> num += <NUM_LIT:1> <EOL> self . result = { '<STR_LIT:value>' : ( compactness / num ) if num > <NUM_LIT:0> else <NUM_LIT:0> } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> return self . percentage ( ) <EOL> else : <EOL> return _ ( '<STR_LIT>' ) <EOL> class Roeck ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> rec = <NUM_LIT:0> <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> random . seed ( ) <EOL> districts = [ ] <EOL> if '<STR_LIT>' in kwargs : <EOL> districts = [ kwargs [ '<STR_LIT>' ] ] <EOL> if districts [ <NUM_LIT:0> ] . geom . empty : <EOL> return <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = True ) <EOL> else : <EOL> return <EOL> num = <NUM_LIT:0> <EOL> compactness = <NUM_LIT:0> <EOL> for district in districts : <EOL> if district . district_id == <NUM_LIT:0> : <EOL> continue <EOL> if district . geom . empty : <EOL> continue <EOL> hull = map ( lambda x : Point ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] ) , list ( district . geom . convex_hull . coords [ <NUM_LIT:0> ] ) ) <EOL> disk = self . minidisk ( hull ) <EOL> cir_area = pi * disk . r * disk . r <EOL> compactness += district . geom . area / cir_area <EOL> num += <NUM_LIT:1> <EOL> try : <EOL> self . result = { '<STR_LIT:value>' : compactness / num if num > <NUM_LIT:0> else <NUM_LIT:0> } <EOL> except : <EOL> self . result = { '<STR_LIT:value>' : _ ( '<STR_LIT>' ) } <EOL> class Circle : <EOL> """<STR_LIT>""" <EOL> cx = None <EOL> cy = None <EOL> r = None <EOL> def __init__ ( self , pts ) : <EOL> """<STR_LIT>""" <EOL> if len ( pts ) == <NUM_LIT:1> : <EOL> self . cx = pts [ <NUM_LIT:0> ] . coords [ <NUM_LIT:0> ] <EOL> self . cy = pts [ <NUM_LIT:0> ] . coords [ <NUM_LIT:1> ] <EOL> self . r = <NUM_LIT:0> <EOL> elif len ( pts ) == <NUM_LIT:2> : <EOL> ls = LineString ( pts ) <EOL> self . cx = ls . centroid . x <EOL> self . cy = ls . centroid . y <EOL> self . r = ls . length / <NUM_LIT:2> <EOL> elif len ( pts ) == <NUM_LIT:3> : <EOL> try : <EOL> ( p1 , p2 , p3 , ) = self . deperpendicularize ( pts ) <EOL> except ValueError : <EOL> ls = LineString ( pts ) <EOL> self . cx = ls . centroid . x <EOL> self . cy = ls . centroid . y <EOL> self . r = ls . length / <NUM_LIT:2> <EOL> return <EOL> if abs ( p2 . coords [ <NUM_LIT:0> ] - p1 . coords [ <NUM_LIT:0> ] ) == <NUM_LIT:0> and abs ( p3 . coords [ <NUM_LIT:1> ] - p2 . coords [ <NUM_LIT:1> ] ) == <NUM_LIT:0> : <EOL> self . cx = ( p2 . coords [ <NUM_LIT:0> ] + p3 . coords [ <NUM_LIT:0> ] ) / <NUM_LIT:2> <EOL> self . cy = ( p1 . coords [ <NUM_LIT:1> ] + p2 . coords [ <NUM_LIT:1> ] ) / <NUM_LIT:2> <EOL> ls = LineString ( [ p1 , Point ( self . cx , self . cy ) ] ) <EOL> self . r = ls . length <EOL> return <EOL> m1 = ( p2 . coords [ <NUM_LIT:1> ] - p1 . coords [ <NUM_LIT:1> ] ) / ( p2 . coords [ <NUM_LIT:0> ] - p1 . coords [ <NUM_LIT:0> ] ) <EOL> m2 = ( p3 . coords [ <NUM_LIT:1> ] - p2 . coords [ <NUM_LIT:1> ] ) / ( p3 . coords [ <NUM_LIT:0> ] - p2 . coords [ <NUM_LIT:0> ] ) <EOL> self . cx = ( m1 * m2 * ( p1 . coords [ <NUM_LIT:1> ] - p3 . coords [ <NUM_LIT:1> ] ) + m2 * ( p1 . coords [ <NUM_LIT:0> ] + p2 . coords [ <NUM_LIT:0> ] ) - m1 * ( p2 . coords [ <NUM_LIT:0> ] + p3 . coords [ <NUM_LIT:0> ] ) ) / ( <NUM_LIT:2> * ( m2 - m1 ) ) <EOL> self . cy = - <NUM_LIT:1> * ( self . cx - ( p1 . coords [ <NUM_LIT:0> ] + p2 . coords [ <NUM_LIT:0> ] ) / <NUM_LIT> ) / m1 + ( p1 . coords [ <NUM_LIT:1> ] + p2 . coords [ <NUM_LIT:1> ] ) / <NUM_LIT> <EOL> lsR = LineString ( pts [ <NUM_LIT:0> ] , ( self . cx , self . cy , ) ) <EOL> self . r = lsR . length <EOL> else : <EOL> self . cx = None <EOL> self . cy = None <EOL> self . r = None <EOL> def deperpendicularize ( self , pts ) : <EOL> """<STR_LIT>""" <EOL> if not self . isperpendicular ( pts [ <NUM_LIT:0> ] , pts [ <NUM_LIT:1> ] , pts [ <NUM_LIT:2> ] ) : <EOL> return ( pts [ <NUM_LIT:0> ] , pts [ <NUM_LIT:1> ] , pts [ <NUM_LIT:2> ] , ) <EOL> if not self . isperpendicular ( pts [ <NUM_LIT:0> ] , pts [ <NUM_LIT:2> ] , pts [ <NUM_LIT:1> ] ) : <EOL> return ( pts [ <NUM_LIT:0> ] , pts [ <NUM_LIT:2> ] , pts [ <NUM_LIT:1> ] , ) <EOL> if not self . isperpendicular ( pts [ <NUM_LIT:1> ] , pts [ <NUM_LIT:0> ] , pts [ <NUM_LIT:2> ] ) : <EOL> return ( pts [ <NUM_LIT:1> ] , pts [ <NUM_LIT:0> ] , pts [ <NUM_LIT:2> ] , ) <EOL> if not self . isperpendicular ( pts [ <NUM_LIT:1> ] , pts [ <NUM_LIT:2> ] , pts [ <NUM_LIT:0> ] ) : <EOL> return ( pts [ <NUM_LIT:1> ] , pts [ <NUM_LIT:2> ] , pts [ <NUM_LIT:0> ] , ) <EOL> if not self . isperpendicular ( pts [ <NUM_LIT:2> ] , pts [ <NUM_LIT:1> ] , pts [ <NUM_LIT:0> ] ) : <EOL> return ( pts [ <NUM_LIT:2> ] , pts [ <NUM_LIT:1> ] , pts [ <NUM_LIT:0> ] , ) <EOL> if not self . isperpendicular ( pts [ <NUM_LIT:2> ] , pts [ <NUM_LIT:0> ] , pts [ <NUM_LIT:1> ] ) : <EOL> return ( pts [ <NUM_LIT:2> ] , pts [ <NUM_LIT:0> ] , pts [ <NUM_LIT:1> ] , ) <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def isperpendicular ( self , pt1 , pt2 , pt3 ) : <EOL> """<STR_LIT>""" <EOL> dy1 = pt2 . coords [ <NUM_LIT:1> ] - pt1 . coords [ <NUM_LIT:1> ] <EOL> dx1 = pt2 . coords [ <NUM_LIT:0> ] - pt1 . coords [ <NUM_LIT:0> ] <EOL> dy2 = pt3 . coords [ <NUM_LIT:1> ] - pt2 . coords [ <NUM_LIT:1> ] <EOL> dx2 = pt3 . coords [ <NUM_LIT:0> ] - pt2 . coords [ <NUM_LIT:0> ] <EOL> if abs ( dx1 ) == <NUM_LIT:0> and abs ( dy2 ) == <NUM_LIT:0> : <EOL> return False <EOL> elif abs ( dy1 ) == <NUM_LIT:0> : <EOL> return True <EOL> elif abs ( dy2 ) == <NUM_LIT:0> : <EOL> return True <EOL> elif abs ( dx1 ) == <NUM_LIT:0> : <EOL> return True <EOL> elif abs ( dx2 ) == <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> def contains ( self , pt ) : <EOL> """<STR_LIT>""" <EOL> ls = LineString ( [ pt , Point ( self . cx , self . cy ) ] ) <EOL> return ls . length <= self . r <EOL> def minidisk ( self , points ) : <EOL> """<STR_LIT>""" <EOL> self . rec = <NUM_LIT:0> <EOL> shuffled = copy ( points [ <NUM_LIT:1> : ] ) <EOL> random . shuffle ( shuffled ) <EOL> return self . b_minidisk ( shuffled , len ( shuffled ) , [ None , None , None ] , <NUM_LIT:0> ) <EOL> def b_minidisk ( self , points , npts , boundary , nbnd ) : <EOL> """<STR_LIT>""" <EOL> if npts == <NUM_LIT:1> and nbnd == <NUM_LIT:0> : <EOL> disk = Roeck . Circle ( [ points [ <NUM_LIT:0> ] ] ) <EOL> elif npts == <NUM_LIT:1> and nbnd == <NUM_LIT:1> : <EOL> disk = Roeck . Circle ( [ points [ <NUM_LIT:0> ] , boundary [ <NUM_LIT:0> ] ] ) <EOL> elif npts == <NUM_LIT:0> and nbnd == <NUM_LIT:2> : <EOL> disk = Roeck . Circle ( boundary [ <NUM_LIT:0> : <NUM_LIT:2> ] ) <EOL> elif nbnd == <NUM_LIT:3> : <EOL> disk = Roeck . Circle ( boundary ) <EOL> else : <EOL> self . rec += <NUM_LIT:1> <EOL> disk = self . b_minidisk ( points , npts - <NUM_LIT:1> , boundary , nbnd ) <EOL> if not disk . contains ( points [ npts - <NUM_LIT:1> ] ) : <EOL> boundary [ nbnd ] = points [ npts - <NUM_LIT:1> ] <EOL> self . rec += <NUM_LIT:1> <EOL> disk = self . b_minidisk ( points , npts - <NUM_LIT:1> , boundary , nbnd + <NUM_LIT:1> ) <EOL> self . rec -= <NUM_LIT:1> <EOL> return disk <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> return self . percentage ( ) <EOL> else : <EOL> return _ ( "<STR_LIT>" ) <EOL> class PolsbyPopper ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> districts = [ ] <EOL> if '<STR_LIT>' in kwargs : <EOL> districts = [ kwargs [ '<STR_LIT>' ] ] <EOL> if districts [ <NUM_LIT:0> ] . geom . empty : <EOL> return <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = True ) <EOL> else : <EOL> return <EOL> num = <NUM_LIT:0> <EOL> compactness = <NUM_LIT:0> <EOL> for district in districts : <EOL> if district . district_id == <NUM_LIT:0> : <EOL> continue <EOL> if district . geom . empty : <EOL> continue <EOL> perimeter = <NUM_LIT:0> <EOL> for poly in district . geom : <EOL> for linestring in poly : <EOL> perimeter += linestring . length <EOL> compactness += <NUM_LIT:4> * pi * district . geom . area / perimeter / perimeter <EOL> num += <NUM_LIT:1> <EOL> self . result = { '<STR_LIT:value>' : compactness / num } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> return self . percentage ( ) <EOL> else : <EOL> return _ ( "<STR_LIT>" ) <EOL> class LengthWidthCompactness ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> districts = [ ] <EOL> if '<STR_LIT>' in kwargs : <EOL> districts = [ kwargs [ '<STR_LIT>' ] ] <EOL> if districts [ <NUM_LIT:0> ] . geom . empty : <EOL> return <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = True ) <EOL> else : <EOL> return <EOL> num = <NUM_LIT:0> <EOL> compactness = <NUM_LIT:0> <EOL> for district in districts : <EOL> if district . district_id == <NUM_LIT:0> : <EOL> continue <EOL> if district . geom . empty : <EOL> continue <EOL> bbox = district . geom . extent <EOL> lw = ( bbox [ <NUM_LIT:3> ] - bbox [ <NUM_LIT:1> ] ) / ( bbox [ <NUM_LIT:2> ] - bbox [ <NUM_LIT:0> ] ) <EOL> if lw > <NUM_LIT:1> : <EOL> lw = <NUM_LIT:1> / lw <EOL> compactness += lw <EOL> num += <NUM_LIT:1> <EOL> self . result = { '<STR_LIT:value>' : compactness / num if num > <NUM_LIT:0> else <NUM_LIT:0> } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> return self . percentage ( ) <EOL> else : <EOL> return _ ( "<STR_LIT>" ) <EOL> class SumValues ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> districts = [ ] <EOL> if '<STR_LIT>' in kwargs : <EOL> districts = [ kwargs [ '<STR_LIT>' ] ] <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> elif '<STR_LIT:list>' in kwargs : <EOL> lst = kwargs [ '<STR_LIT:list>' ] <EOL> self . result = { '<STR_LIT:value>' : reduce ( lambda x , y : x + y , lst ) } <EOL> return <EOL> else : <EOL> return <EOL> sumvals = <NUM_LIT:0> <EOL> for district in districts : <EOL> argnum = <NUM_LIT:1> <EOL> while ( '<STR_LIT>' % argnum ) in self . arg_dict : <EOL> number = self . get_value ( '<STR_LIT>' % argnum , district ) <EOL> if not number is None : <EOL> sumvals += number <EOL> argnum += <NUM_LIT:1> <EOL> if self . get_value ( '<STR_LIT:target>' ) is not None : <EOL> target = self . get_value ( '<STR_LIT:target>' ) <EOL> self . result = { '<STR_LIT:value>' : "<STR_LIT>" % ( sumvals , target ) } <EOL> else : <EOL> self . result = { '<STR_LIT:value>' : sumvals } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> return self . template ( <EOL> '<STR_LIT>' ) <EOL> return self . empty_html_result <EOL> class Percent ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> district = None <EOL> if '<STR_LIT>' in kwargs : <EOL> district = kwargs [ '<STR_LIT>' ] <EOL> num = self . get_value ( '<STR_LIT>' , district ) <EOL> den = self . get_value ( '<STR_LIT>' , district ) <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> num = <NUM_LIT:0> <EOL> den = <NUM_LIT:0> <EOL> for district in districts : <EOL> if district . district_id == <NUM_LIT:0> : <EOL> continue <EOL> tmpnum = self . get_value ( '<STR_LIT>' , district ) <EOL> tmpden = self . get_value ( '<STR_LIT>' , district ) <EOL> if tmpnum is None or tmpden is None : <EOL> continue <EOL> den += tmpden <EOL> num += tmpnum <EOL> else : <EOL> return <EOL> if num is None or den is None or den == <NUM_LIT:0> : <EOL> return <EOL> try : <EOL> self . result = { '<STR_LIT:value>' : num / den } <EOL> except : <EOL> if den [ '<STR_LIT:value>' ] == <NUM_LIT:0> : <EOL> self . result = { '<STR_LIT:value>' : <NUM_LIT:0> } <EOL> else : <EOL> self . result = { '<STR_LIT:value>' : num / den [ '<STR_LIT:value>' ] } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> return self . percentage ( ) <EOL> return self . empty_html_result <EOL> class Threshold ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> districts = [ ] <EOL> if '<STR_LIT>' in kwargs : <EOL> districts = [ kwargs [ '<STR_LIT>' ] ] <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> else : <EOL> return <EOL> count = <NUM_LIT:0> <EOL> for district in districts : <EOL> val = self . get_value ( '<STR_LIT:value>' , district ) <EOL> thr = self . get_value ( '<STR_LIT>' , district ) <EOL> if val is None or thr is None : <EOL> continue <EOL> if float ( val ) > float ( thr ) : <EOL> count += <NUM_LIT:1> <EOL> self . result = { '<STR_LIT:value>' : count } <EOL> class Range ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> districts = None <EOL> if '<STR_LIT>' in kwargs : <EOL> districts = [ kwargs [ '<STR_LIT>' ] ] <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> else : <EOL> return <EOL> count = <NUM_LIT:0> <EOL> if '<STR_LIT>' in self . arg_dict : <EOL> apply_num_members = int ( self . arg_dict [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) == <NUM_LIT:1> <EOL> else : <EOL> apply_num_members = False <EOL> for district in districts : <EOL> if district . district_id == <NUM_LIT:0> : <EOL> continue <EOL> val = self . get_value ( '<STR_LIT:value>' , district ) <EOL> if apply_num_members and district . num_members > <NUM_LIT:1> : <EOL> val = float ( val ) / district . num_members <EOL> minval = self . get_value ( '<STR_LIT>' , district ) <EOL> maxval = self . get_value ( '<STR_LIT>' , district ) <EOL> if val is None or minval is None or maxval is None : <EOL> continue <EOL> if float ( val ) > float ( minval ) and float ( val ) < float ( maxval ) : <EOL> count += <NUM_LIT:1> <EOL> self . result = { '<STR_LIT:value>' : count } <EOL> class Contiguity ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> districts = [ ] <EOL> if '<STR_LIT>' in kwargs : <EOL> districts = [ kwargs [ '<STR_LIT>' ] ] <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = True ) <EOL> else : <EOL> return <EOL> if '<STR_LIT>' in self . arg_dict : <EOL> allow_single = int ( self . arg_dict [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) == <NUM_LIT:1> <EOL> else : <EOL> allow_single = False <EOL> count = <NUM_LIT:0> <EOL> for district in districts : <EOL> if len ( district . geom ) == <NUM_LIT:1> and district . district_id != <NUM_LIT:0> : <EOL> count += <NUM_LIT:1> <EOL> else : <EOL> if district . district_id == <NUM_LIT:0> : <EOL> if '<STR_LIT>' in kwargs : <EOL> continue <EOL> elif len ( district . geom ) == <NUM_LIT:1> : <EOL> count += <NUM_LIT:1> <EOL> continue <EOL> if len ( district . geom ) == <NUM_LIT:0> : <EOL> count += <NUM_LIT:1> <EOL> continue <EOL> overrides = district . get_contiguity_overrides ( ) <EOL> if allow_single or len ( overrides ) > <NUM_LIT:0> : <EOL> union = district . geom [ <NUM_LIT:0> ] <EOL> remaining = district . geom [ <NUM_LIT:1> : ] <EOL> contiguous = True <EOL> while ( len ( remaining ) > <NUM_LIT:0> ) : <EOL> if len ( district . geom ) == <NUM_LIT:0> : <EOL> continue <EOL> match_in_pass = False <EOL> for geom in remaining : <EOL> linked = False <EOL> if allow_single and geom . touches ( union ) : <EOL> linked = True <EOL> else : <EOL> for override in overrides : <EOL> o = override . override_geounit . geom <EOL> c = override . connect_to_geounit . geom <EOL> if ( geom . contains ( o ) and union . contains ( c ) ) or ( geom . contains ( c ) and union . contains ( o ) ) : <EOL> linked = True <EOL> overrides . remove ( override ) <EOL> break <EOL> if linked : <EOL> remaining . remove ( geom ) <EOL> union = geom . union ( union ) <EOL> match_in_pass = True <EOL> if not match_in_pass : <EOL> contiguous = False <EOL> break <EOL> if contiguous : <EOL> count += <NUM_LIT:1> <EOL> self . result = { '<STR_LIT:value>' : count } <EOL> try : <EOL> target = self . get_value ( '<STR_LIT:target>' ) <EOL> if target != None : <EOL> self . result = { '<STR_LIT:value>' : _ ( '<STR_LIT>' ) % { '<STR_LIT:value>' : count , '<STR_LIT:target>' : target } } <EOL> except : <EOL> pass <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> if type ( self . result [ '<STR_LIT:value>' ] ) == int : <EOL> if self . result [ '<STR_LIT:value>' ] == <NUM_LIT:1> : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return CalculatorBase . html ( self ) <EOL> return '<STR_LIT>' % self . result <EOL> class AllContiguous ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs : <EOL> return <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> calc = Contiguity ( ) <EOL> calc . compute ( ** kwargs ) <EOL> self . result = { '<STR_LIT:value>' : ( len ( districts ) - <NUM_LIT:1> ) == calc . result [ '<STR_LIT:value>' ] } <EOL> class NonContiguous ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs : <EOL> return <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> calc = Contiguity ( ) <EOL> calc . compute ( ** kwargs ) <EOL> count = len ( districts ) - calc . result <EOL> try : <EOL> target = self . get_value ( '<STR_LIT:target>' ) <EOL> if target != None : <EOL> self . result = { '<STR_LIT:value>' : _ ( '<STR_LIT>' ) % { '<STR_LIT:value>' : count , '<STR_LIT:target>' : target } } <EOL> except : <EOL> self . result = { '<STR_LIT:value>' : count } <EOL> class Interval ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> districts = [ ] <EOL> bounds = [ ] <EOL> target = Decimal ( str ( self . get_value ( '<STR_LIT:target>' ) ) ) <EOL> if '<STR_LIT>' in self . arg_dict : <EOL> apply_num_members = int ( self . arg_dict [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) == <NUM_LIT:1> <EOL> else : <EOL> apply_num_members = False <EOL> if '<STR_LIT>' in kwargs : <EOL> districts = [ kwargs [ '<STR_LIT>' ] ] <EOL> argnum = <NUM_LIT:1> <EOL> while ( '<STR_LIT>' % argnum ) in self . arg_dict : <EOL> bound = Decimal ( str ( self . get_value ( '<STR_LIT>' % argnum ) ) ) <EOL> bounds . append ( target + ( target * bound ) ) <EOL> bounds . append ( target - ( target * bound ) ) <EOL> argnum += <NUM_LIT:1> <EOL> bounds . sort ( ) <EOL> for district in districts : <EOL> value = self . get_value ( '<STR_LIT>' , district ) <EOL> if value == None : <EOL> return <EOL> if apply_num_members and district . num_members > <NUM_LIT:1> : <EOL> value = value / district . num_members <EOL> for idx , bound in enumerate ( bounds ) : <EOL> if value < bound : <EOL> self . result = { <EOL> '<STR_LIT:index>' : idx , <EOL> '<STR_LIT:value>' : value , <EOL> '<STR_LIT>' : self . arg_dict [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> } <EOL> return <EOL> self . result = { <EOL> '<STR_LIT:index>' : len ( bounds ) , <EOL> '<STR_LIT:value>' : value , <EOL> '<STR_LIT>' : self . arg_dict [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> } <EOL> return <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = True ) <EOL> argnum = <NUM_LIT:1> <EOL> min_bound = None <EOL> while ( '<STR_LIT>' % argnum ) in self . arg_dict : <EOL> bound = Decimal ( str ( self . get_value ( '<STR_LIT>' % argnum ) ) ) <EOL> if ( not min_bound ) or ( bound < min_bound ) : <EOL> min_bound = bound <EOL> argnum += <NUM_LIT:1> <EOL> max_bound = target + ( target * min_bound ) <EOL> min_bound = target - ( target * min_bound ) <EOL> count = <NUM_LIT:0> <EOL> for district in districts : <EOL> value = self . get_value ( '<STR_LIT>' , district ) <EOL> if apply_num_members and district . num_members > <NUM_LIT:1> : <EOL> value = float ( value ) / district . num_members <EOL> if value != None and value >= min_bound and value < max_bound : <EOL> count += <NUM_LIT:1> <EOL> self . result = { '<STR_LIT:value>' : count } <EOL> else : <EOL> return <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> if '<STR_LIT:index>' in self . result and '<STR_LIT>' in self . result : <EOL> interval = self . result [ '<STR_LIT:index>' ] <EOL> interval_class = "<STR_LIT>" % interval if interval >= <NUM_LIT:0> else '<STR_LIT>' <EOL> t = '<STR_LIT>' '<STR_LIT>' <EOL> c = { '<STR_LIT:class>' : interval_class } <EOL> return self . template ( t , c ) <EOL> return self . empty_html_result <EOL> class Equivalence ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs or not '<STR_LIT>' in kwargs : <EOL> return <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> if len ( districts ) == <NUM_LIT:0> : <EOL> return <EOL> if '<STR_LIT>' in self . arg_dict : <EOL> apply_num_members = int ( self . arg_dict [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) == <NUM_LIT:1> <EOL> else : <EOL> apply_num_members = False <EOL> min_d = <NUM_LIT> <EOL> max_d = <NUM_LIT:0> <EOL> for district in districts : <EOL> if district . district_id == <NUM_LIT:0> : <EOL> continue <EOL> tmpval = self . get_value ( '<STR_LIT:value>' , district ) <EOL> if apply_num_members and district . num_members > <NUM_LIT:1> : <EOL> tmpval = float ( tmpval ) / district . num_members <EOL> if not tmpval is None : <EOL> min_d = min ( float ( tmpval ) , min_d ) <EOL> max_d = max ( float ( tmpval ) , max_d ) <EOL> self . result = { '<STR_LIT:value>' : max_d - min_d } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> return self . template ( '<STR_LIT>' ) <EOL> return _ ( '<STR_LIT>' ) <EOL> class RepresentationalFairness ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> else : <EOL> return <EOL> dems = <NUM_LIT:0> <EOL> reps = <NUM_LIT:0> <EOL> for district in districts : <EOL> dem = self . get_value ( '<STR_LIT>' , district ) <EOL> rep = self . get_value ( '<STR_LIT>' , district ) <EOL> if dem is None or rep is None : <EOL> continue <EOL> dem = float ( dem ) <EOL> rep = float ( rep ) <EOL> if dem == <NUM_LIT:0.0> and rep == <NUM_LIT:0.0> : <EOL> continue <EOL> dem_pi = dem / ( rep + dem ) <EOL> if dem_pi > <NUM_LIT> : <EOL> dems += <NUM_LIT:1> <EOL> else : <EOL> rep_pi = rep / ( rep + dem ) <EOL> if rep_pi > <NUM_LIT> : <EOL> reps += <NUM_LIT:1> <EOL> self . result = { '<STR_LIT:value>' : dems - reps } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> sort = abs ( self . result [ '<STR_LIT:value>' ] ) <EOL> party = _ ( '<STR_LIT>' ) if self . result [ '<STR_LIT:value>' ] > <NUM_LIT:0> else _ ( '<STR_LIT>' ) <EOL> if sort == <NUM_LIT:0> : <EOL> return '<STR_LIT>' % _ ( '<STR_LIT>' ) <EOL> else : <EOL> return '<STR_LIT>' % ( party , sort ) <EOL> return '<STR_LIT>' % _ ( '<STR_LIT>' ) <EOL> def json ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> sort = abs ( self . result [ '<STR_LIT:value>' ] ) <EOL> party = _ ( '<STR_LIT>' ) if self . result [ '<STR_LIT:value>' ] > <NUM_LIT:0> else _ ( '<STR_LIT>' ) <EOL> output = { '<STR_LIT:result>' : '<STR_LIT>' % ( party , sort ) } <EOL> else : <EOL> output = { '<STR_LIT:result>' : None } <EOL> return json . dumps ( output ) <EOL> def sortkey ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> return abs ( self . result [ '<STR_LIT:value>' ] ) <EOL> return <NUM_LIT> <EOL> class Competitiveness ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs : <EOL> return <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> try : <EOL> difference = float ( self . get_value ( '<STR_LIT>' ) ) <EOL> low = <NUM_LIT> - difference <EOL> high = <NUM_LIT> + difference <EOL> except : <EOL> low = <NUM_LIT> <EOL> high = <NUM_LIT> <EOL> fair = <NUM_LIT:0> <EOL> for district in districts : <EOL> if district . district_id == <NUM_LIT:0> : <EOL> continue <EOL> tmpdem = self . get_value ( '<STR_LIT>' , district ) <EOL> tmprep = self . get_value ( '<STR_LIT>' , district ) <EOL> if tmpdem is None or tmprep is None : <EOL> continue <EOL> dem = float ( tmpdem ) <EOL> rep = float ( tmprep ) <EOL> if dem == <NUM_LIT:0.0> and rep == <NUM_LIT:0.0> : <EOL> continue <EOL> pidx = dem / ( dem + rep ) <EOL> if pidx > low and pidx < high : <EOL> fair += <NUM_LIT:1> <EOL> self . result = { '<STR_LIT:value>' : fair } <EOL> class CountDistricts ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs or not '<STR_LIT:target>' in self . arg_dict : <EOL> return <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> target = int ( self . get_value ( '<STR_LIT:target>' ) ) <EOL> self . result = { '<STR_LIT:value>' : ( len ( districts ) - <NUM_LIT:1> ) == target } <EOL> class AllBlocksAssigned ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs : <EOL> return <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> threshold = self . get_value ( '<STR_LIT>' ) <EOL> if not threshold : <EOL> threshold = <NUM_LIT:100> <EOL> geounits = plan . get_unassigned_geounits ( threshold = threshold , version = version ) <EOL> self . result = { '<STR_LIT:value>' : len ( geounits ) == <NUM_LIT:0> } <EOL> class Equipopulation ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs : <EOL> return <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> inrange = Range ( ) <EOL> inrange . arg_dict = self . arg_dict <EOL> inrange . compute ( ** kwargs ) <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> try : <EOL> target = self . get_value ( '<STR_LIT:target>' ) <EOL> validation = self . get_value ( '<STR_LIT>' ) <EOL> if validation != None : <EOL> self . result = { '<STR_LIT:value>' : inrange . result [ '<STR_LIT:value>' ] == ( len ( districts ) - <NUM_LIT:1> ) } <EOL> elif target != None : <EOL> self . result = { '<STR_LIT:value>' : _ ( '<STR_LIT>' ) % { '<STR_LIT:value>' : inrange . result [ '<STR_LIT:value>' ] , '<STR_LIT:target>' : target } } <EOL> else : <EOL> self . result = inrange . result <EOL> except : <EOL> self . result = inrange . result <EOL> class MajorityMinority ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs : <EOL> return <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> if '<STR_LIT>' in self . arg_dict : <EOL> apply_num_members = int ( self . arg_dict [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) == <NUM_LIT:1> <EOL> else : <EOL> apply_num_members = False <EOL> districtcount = <NUM_LIT:0> <EOL> for district in districts : <EOL> pop = self . get_value ( '<STR_LIT>' , district ) <EOL> if pop is None : <EOL> continue <EOL> den = float ( pop ) <EOL> argnum = <NUM_LIT:1> <EOL> exceeds = False <EOL> while ( '<STR_LIT>' % argnum ) in self . arg_dict : <EOL> minor = self . get_value ( '<STR_LIT>' % argnum , district ) <EOL> argnum += <NUM_LIT:1> <EOL> if minor is None : <EOL> continue <EOL> num = float ( minor ) <EOL> try : <EOL> threshold = float ( self . get_value ( '<STR_LIT>' , district ) ) <EOL> except : <EOL> threshold = <NUM_LIT:0.5> <EOL> if den != <NUM_LIT:0> and num / den > threshold : <EOL> exceeds = True <EOL> break <EOL> if exceeds : <EOL> if apply_num_members : <EOL> districtcount += district . num_members <EOL> else : <EOL> districtcount += <NUM_LIT:1> <EOL> self . result = { '<STR_LIT:value>' : districtcount } <EOL> try : <EOL> target = self . get_value ( '<STR_LIT:target>' ) <EOL> validation = self . get_value ( '<STR_LIT>' ) <EOL> if validation != None : <EOL> self . result = { '<STR_LIT:value>' : districtcount >= Decimal ( validation ) } <EOL> elif target != None : <EOL> self . result = { '<STR_LIT:value>' : _ ( '<STR_LIT>' ) % { '<STR_LIT:value>' : districtcount , '<STR_LIT:target>' : target } } <EOL> except : <EOL> pass <EOL> class MultiMember ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . result = { '<STR_LIT:value>' : False } <EOL> if not '<STR_LIT>' in kwargs : <EOL> return <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> legbod = plan . legislative_body <EOL> if legbod . multi_members_allowed : <EOL> min_dist_mems = legbod . min_multi_district_members <EOL> max_dist_mems = legbod . max_multi_district_members <EOL> min_multi_dists = legbod . min_multi_districts <EOL> max_multi_dists = legbod . max_multi_districts <EOL> min_plan_mems = legbod . min_plan_members <EOL> max_plan_mems = legbod . max_plan_members <EOL> total_members = <NUM_LIT:0> <EOL> total_multi_dists = <NUM_LIT:0> <EOL> for d in districts : <EOL> if d . district_id == <NUM_LIT:0> : <EOL> continue <EOL> total_members += d . num_members <EOL> if ( d . num_members > <NUM_LIT:1> ) : <EOL> total_multi_dists += <NUM_LIT:1> <EOL> if ( d . num_members < min_dist_mems ) or ( d . num_members > max_dist_mems ) : <EOL> return <EOL> if ( total_multi_dists < min_multi_dists ) or ( total_multi_dists > max_multi_dists ) : <EOL> return <EOL> if ( total_members < min_plan_mems ) or ( total_members > max_plan_mems ) : <EOL> return <EOL> self . result = { '<STR_LIT:value>' : True } <EOL> class Average ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> districts = [ ] <EOL> if '<STR_LIT:list>' in kwargs : <EOL> arg_list = kwargs [ '<STR_LIT:list>' ] <EOL> filtered = filter ( lambda x : not x is None , arg_list ) <EOL> if len ( filtered ) == <NUM_LIT:0> : <EOL> return <EOL> reduced = reduce ( lambda x , y : x + y , filtered ) <EOL> self . result = { '<STR_LIT:value>' : reduced / len ( filtered ) } <EOL> elif '<STR_LIT>' in kwargs : <EOL> districts . append ( kwargs [ '<STR_LIT>' ] ) <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = False ) <EOL> else : <EOL> return <EOL> if not '<STR_LIT:list>' in kwargs : <EOL> total = <NUM_LIT:0.0> <EOL> count = <NUM_LIT:0.0> <EOL> for district in districts : <EOL> if district . district_id == <NUM_LIT:0> : <EOL> continue <EOL> count += <NUM_LIT:1> <EOL> argnum = <NUM_LIT:0> <EOL> argsum = <NUM_LIT:0.0> <EOL> while ( '<STR_LIT>' % ( argnum + <NUM_LIT:1> , ) ) in self . arg_dict : <EOL> argnum += <NUM_LIT:1> <EOL> number = self . get_value ( '<STR_LIT>' % argnum , district ) <EOL> if not number is None : <EOL> argsum += float ( number ) <EOL> total += argsum / argnum <EOL> if count == <NUM_LIT:0> : <EOL> self . result = None <EOL> return <EOL> self . result = { '<STR_LIT:value>' : total / count } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> if type ( self . result [ '<STR_LIT:value>' ] ) == float : <EOL> return self . percentage ( span = True ) <EOL> else : <EOL> return CalculatorBase . html ( self ) <EOL> return self . empty_html_result <EOL> class Comments ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs : <EOL> return <EOL> district = kwargs [ '<STR_LIT>' ] <EOL> typetags = filter ( lambda tag : tag . name [ : <NUM_LIT:4> ] == '<STR_LIT:type>' , district . tags ) <EOL> typetags = map ( lambda tag : tag . name [ <NUM_LIT:5> : ] , typetags ) <EOL> self . result = { '<STR_LIT>' : typetags } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . result <EOL> class CommunityTypeCounter ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> district = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> return <EOL> version = None <EOL> if '<STR_LIT:version>' in kwargs : <EOL> version = kwargs [ '<STR_LIT:version>' ] <EOL> self . result = { '<STR_LIT:value>' : _ ( '<STR_LIT>' ) } <EOL> if '<STR_LIT>' in kwargs : <EOL> try : <EOL> self . result = { '<STR_LIT:value>' : district . count_community_type_union ( kwargs [ '<STR_LIT>' ] , version = version ) } <EOL> except : <EOL> pass <EOL> class CommunityTypeCompatible ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs : <EOL> return <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in kwargs : <EOL> pversion = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> pversion = plan . version <EOL> if '<STR_LIT>' in kwargs : <EOL> cversion = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> cversion = None <EOL> districts = plan . get_districts_at_version ( pversion , include_geom = True ) <EOL> if not '<STR_LIT>' in kwargs : <EOL> self . result = { '<STR_LIT:value>' : '<STR_LIT>' } <EOL> return <EOL> ctype = kwargs [ '<STR_LIT:type>' ] <EOL> if not ctype . startswith ( '<STR_LIT>' ) : <EOL> ctype = '<STR_LIT>' + ctype <EOL> community_id = kwargs [ '<STR_LIT>' ] <EOL> alltypes = None <EOL> for district in districts : <EOL> if district . is_unassigned : <EOL> continue <EOL> tmpset = district . get_community_type_union ( community_id , version = cversion ) <EOL> if alltypes is None : <EOL> alltypes = tmpset <EOL> else : <EOL> alltypes = alltypes & tmpset <EOL> alltypes = map ( lambda x : str ( x . name ) , alltypes ) <EOL> self . result = { '<STR_LIT:value>' : ( ctype in alltypes ) } <EOL> class SplitCounter ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs : <EOL> return <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = self . get_value ( '<STR_LIT:version>' ) <EOL> if version is None : <EOL> version = plan . version <EOL> target = self . get_value ( '<STR_LIT>' ) <EOL> inverse = self . get_value ( '<STR_LIT>' ) == <NUM_LIT:1> <EOL> only_total = ( self . get_value ( '<STR_LIT>' ) == <NUM_LIT:1> ) <EOL> splits = plan . compute_splits ( target , version = version , inverse = inverse ) <EOL> self . result = { <EOL> '<STR_LIT:value>' : len ( splits [ '<STR_LIT>' ] ) if only_total else splits , <EOL> '<STR_LIT>' : only_total <EOL> } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> render = '<STR_LIT>' <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> r = self . result [ '<STR_LIT:value>' ] <EOL> if self . result [ '<STR_LIT>' ] : <EOL> return '<STR_LIT>' % r <EOL> total_split_districts = len ( set ( i [ <NUM_LIT:0> ] for i in r [ '<STR_LIT>' ] ) ) <EOL> if r [ '<STR_LIT>' ] : <EOL> template = '<STR_LIT>' % _ ( '<STR_LIT>' '<STR_LIT>' ) <EOL> else : <EOL> template = '<STR_LIT>' % _ ( '<STR_LIT>' '<STR_LIT>' ) <EOL> render += template % { '<STR_LIT>' : '<STR_LIT>' if r [ '<STR_LIT>' ] else '<STR_LIT>' , '<STR_LIT>' : r [ '<STR_LIT>' ] , '<STR_LIT:result>' : total_split_districts } <EOL> render += '<STR_LIT>' % ( _ ( '<STR_LIT>' ) , len ( r [ '<STR_LIT>' ] ) ) <EOL> render += '<STR_LIT>' % ( r [ '<STR_LIT>' ] . capitalize ( ) , r [ '<STR_LIT>' ] . capitalize ( ) if r [ '<STR_LIT>' ] else r [ '<STR_LIT>' ] . capitalize ( ) ) <EOL> for s in r [ '<STR_LIT>' ] : <EOL> render += '<STR_LIT>' % ( s [ '<STR_LIT>' ] , s [ '<STR_LIT>' ] ) <EOL> render += '<STR_LIT>' <EOL> render += '<STR_LIT>' <EOL> return render <EOL> class DistrictSplitCounter ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs : <EOL> return <EOL> district = kwargs [ '<STR_LIT>' ] <EOL> if district . geom . empty : <EOL> return <EOL> geolevel_id = self . get_value ( '<STR_LIT>' ) <EOL> num_splits = district . count_splits ( geolevel_id ) <EOL> self . result = { '<STR_LIT:value>' : num_splits } <EOL> class ConvexHullRatio ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> districts = [ ] <EOL> if '<STR_LIT>' in kwargs : <EOL> districts = [ kwargs [ '<STR_LIT>' ] ] <EOL> if districts [ <NUM_LIT:0> ] . geom . empty : <EOL> return <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version , include_geom = True ) <EOL> else : <EOL> return <EOL> num = <NUM_LIT:0> <EOL> ratios = <NUM_LIT:0.0> <EOL> for district in districts : <EOL> if district . geom . empty or district . geom . length == <NUM_LIT:0> or district . district_id == <NUM_LIT:0> : <EOL> continue <EOL> ratios += district . geom . area / district . geom . convex_hull . area <EOL> num += <NUM_LIT:1> <EOL> self . result = { '<STR_LIT:value>' : ( ratios / num ) if num > <NUM_LIT:0> else <NUM_LIT:0> } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> return self . percentage ( ) <EOL> else : <EOL> return _ ( '<STR_LIT>' ) <EOL> class Adjacency ( CalculatorBase ) : <EOL> """<STR_LIT>""" <EOL> def _district_calculator ( self , district ) : <EOL> def sum_query ( query ) : <EOL> return sum ( map ( lambda x : float ( x ) if x else <NUM_LIT:0> , self . r . mget ( query ) ) ) <EOL> geounit_ids = [ geo [ <NUM_LIT:1> ] for geo in district . get_base_geounits ( ) ] <EOL> geounit_ids . sort ( ) <EOL> geounit_id_combos = itertools . combinations ( geounit_ids , <NUM_LIT:2> ) <EOL> redis_query = [ ] <EOL> total = <NUM_LIT:0> <EOL> num_queries = <NUM_LIT:0> <EOL> for count , ids in enumerate ( geounit_id_combos ) : <EOL> num_queries += <NUM_LIT:1> <EOL> if count % <NUM_LIT> == <NUM_LIT:0> and count > <NUM_LIT:0> : <EOL> total += sum_query ( redis_query ) <EOL> redis_query = [ ] <EOL> redis_query . append ( key_gen ( ** { '<STR_LIT>' : ids [ <NUM_LIT:0> ] , '<STR_LIT>' : ids [ <NUM_LIT:1> ] } ) ) <EOL> total += sum_query ( redis_query ) <EOL> if num_queries == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> return total / num_queries <EOL> def compute ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> redis_host = kwargs . get ( '<STR_LIT:host>' , redis_settings [ '<STR_LIT>' ] ) <EOL> redis_port = int ( kwargs . get ( '<STR_LIT:port>' , redis_settings [ '<STR_LIT>' ] ) ) <EOL> redis_db = int ( kwargs . get ( '<STR_LIT>' , redis_settings [ '<STR_LIT>' ] ) ) <EOL> self . r = redis . StrictRedis ( host = redis_host , port = redis_port , db = redis_db ) <EOL> districts = [ ] <EOL> if '<STR_LIT>' in kwargs : <EOL> districts = [ kwargs [ '<STR_LIT>' ] ] <EOL> if districts [ <NUM_LIT:0> ] . geom . empty : <EOL> return <EOL> elif '<STR_LIT>' in kwargs : <EOL> plan = kwargs [ '<STR_LIT>' ] <EOL> version = kwargs [ '<STR_LIT:version>' ] if '<STR_LIT:version>' in kwargs else plan . version <EOL> districts = plan . get_districts_at_version ( version ) <EOL> score = <NUM_LIT:0> <EOL> if len ( districts ) == <NUM_LIT:1> : <EOL> score = <NUM_LIT:0> if districts [ <NUM_LIT:0> ] . district_id == <NUM_LIT:0> else self . _district_calculator ( districts [ <NUM_LIT:0> ] ) <EOL> elif len ( districts ) > <NUM_LIT:1> : <EOL> district_scores = [ ] <EOL> for district in districts : <EOL> if district . district_id == <NUM_LIT:0> : <EOL> continue <EOL> district_score = self . _district_calculator ( district ) <EOL> district_scores . append ( district_score ) <EOL> region = districts [ <NUM_LIT:0> ] . plan . legislative_body . region . name <EOL> region_key = key_gen ( ** { '<STR_LIT>' : region } ) <EOL> region_score = float ( self . r . get ( region_key ) ) <EOL> num_districts = len ( district_scores ) <EOL> for district in district_scores : <EOL> numerator = ( district - region_score / num_districts ) ** <NUM_LIT:2> <EOL> denominator = ( region_score / num_districts ) ** <NUM_LIT:2> <EOL> score += numerator / denominator <EOL> self . result = { '<STR_LIT:value>' : score } <EOL> def html ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . result is None and '<STR_LIT:value>' in self . result : <EOL> t = Template ( '<STR_LIT>' ) <EOL> c = Context ( { '<STR_LIT>' : self . result [ '<STR_LIT:value>' ] } ) <EOL> return t . render ( c ) <EOL> else : <EOL> return _ ( '<STR_LIT>' ) </s>
