Пример #1
0
    def test_dec2pct(self):

        d = Decimal("0.232385436346")
        s = formatting.dec2pct(d)
        self.assertEqual(s, "23.24%")

        d = Decimal("23.3023234235")
        s = formatting.dec2pct(d, raw=True)
        self.assertEqual(s, "23.30%")
Пример #2
0
    def load_data(self, request, bounds, **args):

        resp = self._base_response(bounds)

        if "census-tract" in bounds:

            tract = bounds["census-tract"][0]
            results = Tract.objects.filter(
                state=tract['metadata']['STATEFP10'],
                county=tract['metadata']['COUNTYFP10'],
                tract=tract['metadata']['TRACTCE10'])

            if results:

                t = results[0]

                # median value

                if t.B25077_001E:

                    resp["data"].update({
                        "owned": {
                            "median_value": dec2curr(t.B25077_001E, whole=True),
                        }
                    })

                # rental pct

                try:

                    rental_pct = t.B25003_003E / (t.B25003_002E + t.B25003_003E)

                    resp["data"].update({
                        "rentals": {
                            "median_cost": dec2curr(t.B25058_001E, whole=True),
                            "pct": dec2pct(rental_pct, whole=True),
                            "svg": viz.svg_piechart(rental_pct, 50, 50, 35)
                        }
                    })

                except:
                    pass

                # transportation

                trans_total = t.B08301_001E

                if trans_total:

                    resp["data"].update({
                        "transport": {
                            "total": dec2num(trans_total, whole=True),
                            "auto_pct": dec2num((t.B08301_002E / trans_total) * 100, whole=True),
                            "public_pct": dec2num((t.B08301_010E / trans_total) * 100, whole=True),
                            "bike_pct": dec2num((t.B08301_018E / trans_total) * 100, whole=True),
                            "walk_pct": dec2num((t.B08301_019E / trans_total) * 100, whole=True),
                        }
                    })

        return resp
Пример #3
0
    def load_data(self, request, bounds, **args):

        resp = self._base_response(bounds)

        if "census-tract" in bounds:

            tract = bounds["census-tract"][0]
            results = Tract.objects.filter(
                state=tract["metadata"]["STATEFP10"],
                county=tract["metadata"]["COUNTYFP10"],
                tract=tract["metadata"]["TRACTCE10"],
            )

            if results:

                t = results[0]

                # ethnicity

                # top_ancestry = max(
                #     ((k, getattr(t, k, None)) for k in ("B04003_%03dE" % i for i in range(2, 108))),
                #     key=lambda x: x[1]
                # )

                # resp["data"].update({
                #     "ethnicity": {
                #         "total": dec2num(top_ancestry[1], whole=True),
                #         "pct": dec2pct(top_ancestry[1] / t.B04003_001E),
                #         "name": CENSUS_FIELDS[top_ancestry[0]],
                #     },
                # })

                # update avarage age in years, months

                (years, months) = utils.age2ym(t.B01002_001E)

                if years is not None and months is not None:

                    resp["data"].update({"average_age": {"years": years, "months": months}})

                # relative age distribution

                total = t.B01001_001E

                males = []
                females = []

                for i in range(3, 26):
                    males.append(getattr(t, "B01001_%03dE" % i))
                    females.append(getattr(t, "B01001_%03dE" % (i + 24)))

                total = max([max(males), max(females)])

                if total:

                    males = [dec2pct(m / total, whole=True) for m in males]
                    females = [dec2pct(f / total, whole=True) for f in females]

                    resp["data"].update({"age_distribution": {"female": females, "male": males}})

                # under five years of age

                if t.B11005_001E:

                    resp["data"].update(
                        {
                            "under_five": {
                                "total": dec2num(t.B01001_003E + t.B01001_027E, whole=True),
                                "per_household": dec2num((t.B01001_003E + t.B01001_027E) / t.B11005_001E),
                                "households": dec2num(t.B11005_001E, whole=True),
                            }
                        }
                    )

                # gender

                if t.B01001_001E:

                    resp["data"].update(
                        {
                            "gender": {
                                "male_pct": dec2pct(t.B01001_002E / t.B01001_001E),
                                "female_pct": dec2pct(t.B01001_026E / t.B01001_001E),
                            }
                        }
                    )

                # update rest of the stuff

                if t.B19001_001E:

                    resp["data"].update(
                        {
                            "average_household_income": {
                                "total": dec2curr(t.B19013_001E),
                                "pct_gt_100k": dec2pct(
                                    (t.B19001_014E + t.B19001_015E + t.B19001_016E + t.B19001_017E) / t.B19001_001E
                                ),
                                "pct_60k_100k": dec2pct((t.B19001_012E + t.B19001_013E) / t.B19001_001E),
                                "pct_30k_60k": dec2pct(
                                    (t.B19001_007E + t.B19001_008E + t.B19001_009E + t.B19001_010E + t.B19001_011E)
                                    / t.B19001_001E
                                ),
                                "pct_10k_30k": dec2pct(
                                    (t.B19001_003E + t.B19001_004E + t.B19001_005E + t.B19001_006E) / t.B19001_001E
                                ),
                                "pct_lt_10k": dec2pct((t.B19001_002E) / t.B19001_001E),
                            }
                        }
                    )

        if "census-zcta" in bounds:

            zcta = bounds["census-zcta"][0]

            try:

                c = Contribution.objects.get(zipcode=zcta["metadata"]["ZCTA5CE10"])

                total = c.democrats + c.republicans

                resp["data"].update(
                    {
                        "contributions": {
                            "r": dec2curr(c.republicans, whole=True),
                            "r_pct": dec2pct(c.republicans / total),
                            "r_raw": int(c.republicans),
                            "d": dec2curr(c.democrats, whole=True),
                            "d_pct": dec2pct(c.democrats / total),
                            "d_raw": int(c.democrats),
                        }
                    }
                )

            except:
                pass  # eh, don't really care

        return resp
Пример #4
0
    def load_data(self, request, bounds, **args):

        resp = self._base_response(bounds)

        if "census-tract" in bounds:

            tract = bounds["census-tract"][0]
            results = Tract.objects.filter(
                state=tract['metadata']['STATEFP10'],
                county=tract['metadata']['COUNTYFP10'],
                tract=tract['metadata']['TRACTCE10'])

            if results:

                t = results[0]

                # median value

                if t.B25077_001E:

                    resp["data"].update({
                        "owned": {
                            "median_value": dec2curr(t.B25077_001E,
                                                     whole=True),
                        }
                    })

                # rental pct

                try:

                    rental_pct = t.B25003_003E / (t.B25003_002E +
                                                  t.B25003_003E)

                    resp["data"].update({
                        "rentals": {
                            "median_cost": dec2curr(t.B25058_001E, whole=True),
                            "pct": dec2pct(rental_pct, whole=True),
                            "svg": viz.svg_piechart(rental_pct, 50, 50, 35)
                        }
                    })

                except:
                    pass

                # transportation

                trans_total = t.B08301_001E

                if trans_total:

                    resp["data"].update({
                        "transport": {
                            "total":
                            dec2num(trans_total, whole=True),
                            "auto_pct":
                            dec2num((t.B08301_002E / trans_total) * 100,
                                    whole=True),
                            "public_pct":
                            dec2num((t.B08301_010E / trans_total) * 100,
                                    whole=True),
                            "bike_pct":
                            dec2num((t.B08301_018E / trans_total) * 100,
                                    whole=True),
                            "walk_pct":
                            dec2num((t.B08301_019E / trans_total) * 100,
                                    whole=True),
                        }
                    })

        return resp
Пример #5
0
    def load_data(self, request, bounds, **args):

        resp = self._base_response(bounds)

        if "census-tract" in bounds:

            tract = bounds["census-tract"][0]
            results = Tract.objects.filter(
                state=tract['metadata']['STATEFP10'],
                county=tract['metadata']['COUNTYFP10'],
                tract=tract['metadata']['TRACTCE10'])

            if results:

                t = results[0]

                # ethnicity

                # top_ancestry = max(
                #     ((k, getattr(t, k, None)) for k in ("B04003_%03dE" % i for i in range(2, 108))),
                #     key=lambda x: x[1]
                # )

                # resp["data"].update({
                #     "ethnicity": {
                #         "total": dec2num(top_ancestry[1], whole=True),
                #         "pct": dec2pct(top_ancestry[1] / t.B04003_001E),
                #         "name": CENSUS_FIELDS[top_ancestry[0]],
                #     },
                # })

                # update avarage age in years, months

                (years, months) = utils.age2ym(t.B01002_001E)

                if years is not None and months is not None:

                    resp["data"].update(
                        {"average_age": {
                            "years": years,
                            "months": months,
                        }})

                # relative age distribution

                total = t.B01001_001E

                males = []
                females = []

                for i in range(3, 26):
                    males.append(getattr(t, "B01001_%03dE" % i))
                    females.append(getattr(t, "B01001_%03dE" % (i + 24)))

                total = max([max(males), max(females)])

                if total:

                    males = [dec2pct(m / total, whole=True) for m in males]
                    females = [dec2pct(f / total, whole=True) for f in females]

                    resp["data"].update({
                        "age_distribution": {
                            "female": females,
                            "male": males,
                        }
                    })

                # under five years of age

                if t.B11005_001E:

                    resp["data"].update({
                        "under_five": {
                            "total":
                            dec2num(t.B01001_003E + t.B01001_027E, whole=True),
                            "per_household":
                            dec2num((t.B01001_003E + t.B01001_027E) /
                                    t.B11005_001E),
                            "households":
                            dec2num(t.B11005_001E, whole=True),
                        }
                    })

                # gender

                if t.B01001_001E:

                    resp["data"].update({
                        "gender": {
                            "male_pct": dec2pct(t.B01001_002E / t.B01001_001E),
                            "female_pct":
                            dec2pct(t.B01001_026E / t.B01001_001E),
                        },
                    })

                # update rest of the stuff

                if t.B19001_001E:

                    resp["data"].update({
                        "average_household_income": {
                            "total":
                            dec2curr(t.B19013_001E),
                            "pct_gt_100k":
                            dec2pct((t.B19001_014E + t.B19001_015E +
                                     t.B19001_016E + t.B19001_017E) /
                                    t.B19001_001E),
                            "pct_60k_100k":
                            dec2pct((t.B19001_012E + t.B19001_013E) /
                                    t.B19001_001E),
                            "pct_30k_60k":
                            dec2pct(
                                (t.B19001_007E + t.B19001_008E + t.B19001_009E
                                 + t.B19001_010E + t.B19001_011E) /
                                t.B19001_001E),
                            "pct_10k_30k":
                            dec2pct((t.B19001_003E + t.B19001_004E +
                                     t.B19001_005E + t.B19001_006E) /
                                    t.B19001_001E),
                            "pct_lt_10k":
                            dec2pct((t.B19001_002E) / t.B19001_001E),
                        },
                    })

        if "census-zcta" in bounds:

            zcta = bounds["census-zcta"][0]

            try:

                c = Contribution.objects.get(
                    zipcode=zcta['metadata']['ZCTA5CE10'])

                total = c.democrats + c.republicans

                resp['data'].update({
                    "contributions": {
                        "r": dec2curr(c.republicans, whole=True),
                        "r_pct": dec2pct(c.republicans / total),
                        "r_raw": int(c.republicans),
                        "d": dec2curr(c.democrats, whole=True),
                        "d_pct": dec2pct(c.democrats / total),
                        "d_raw": int(c.democrats),
                    },
                })

            except:
                pass  # eh, don't really care

        return resp