Skip to main content

Kenyan Counties

A lightweight, performant Python package providing data for all 47 counties, constituencies, and wards in Kenya. Optimized for speed with zero I/O overhead at runtime.

Installation

# Core only
pip install kenyan-counties

# With FastAPI / Pydantic support
pip install kenyan-counties[fastapi]

# With Django support
pip install kenyan-counties[django]

# Everything
pip install kenyan-counties[all]

Usage

Core API

from kenyan_counties import (
    get_all_counties,
    get_county_by_code,
    get_county_by_name,
    get_constituencies_for_county,
    get_wards_for_constituency,
)

# All 47 counties
counties = get_all_counties()

# Lookup by code (1-47)
mombasa = get_county_by_code(1)
print(mombasa)  # <County: Mombasa (1)>

# Lookup by name (case-insensitive)
nairobi = get_county_by_name("nairobi")
print(nairobi.code)  # 47

# Constituencies in a county
constituencies = get_constituencies_for_county(1)

# Wards in a constituency
wards = get_wards_for_constituency(1, "Changamwe")
print(wards[0].name)  # Port Reitz

FastAPI

from fastapi import FastAPI
from kenyan_counties.fastapi import router

app = FastAPI()
app.include_router(router)

The router exposes ready-to-use endpoints. To add custom logic, import the Pydantic models and core functions directly:

from kenyan_counties.fastapi import CountyModel, ConstituencyModel, WardModel
from kenyan_counties.core import get_county_by_name, get_wards_for_constituency

@app.get("/search", response_model=CountyModel)
def search_county(name: str):
    county = get_county_by_name(name)
    if not county:
        raise HTTPException(status_code=404, detail="County not found")
    return CountyModel(
        code=county.code,
        name=county.name,
        constituencies=[
            ConstituencyModel(
                name=c.name,
                wards=[WardModel(name=w.name) for w in c.wards]
            )
            for c in county.constituencies
        ]
    )

This adds the following endpoints:

Method Path Description
GET /counties/ List all 47 counties
GET /counties/{code} Get a county by code
GET /counties/{code}/constituencies List constituencies for a county

Django

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "kenyan_counties",
]

Run migrations and seed the database:

python manage.py migrate
python manage.py load_kenya_counties

This creates County, Constituency, and Ward records you can query via the Django ORM.

Using the models in views or serializers:

from kenyan_counties.models import County, Constituency, Ward

# In a view
def county_list(request):
    counties = County.objects.all()
    ...

# In a DRF serializer
class WardSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ward
        fields = ["id", "name"]

class ConstituencySerializer(serializers.ModelSerializer):
    wards = WardSerializer(many=True, read_only=True)

    class Meta:
        model = Constituency
        fields = ["id", "name", "wards"]

class CountySerializer(serializers.ModelSerializer):
    constituencies = ConstituencySerializer(many=True, read_only=True)

    class Meta:
        model = County
        fields = ["code", "name", "constituencies"]

License

MIT

Metadata

Release files for kenyan-counties 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kenyan-counties 0.3.1
File Size Uploaded
kenyan_counties-0.3.1.tar.gz 17.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kenyan-counties 0.3.1
File Interpreter ABI Platform
kenyan_counties-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 36.5 kB

Release files / kenyan_counties-0.3.1.tar.gz

Download URL kenyan_counties-0.3.1.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f6fbba74b45066f9148b01a9cdb907b4637305578586eeb1962ed038fc2ca59a
BLAKE2b-256 checksum
How to use checksums
042b0e2c6f4c93da86e8bf139d6133c890feb7596a0faf91f0dec45f6abfe473
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.13.5 Darwin/25.2.0

Release files / kenyan_counties-0.3.1-py3-none-any.whl

Download URL kenyan_counties-0.3.1-py3-none-any.whl
Size 19.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8feacd1c246367ad80bd667cc682afcc6d76b9d4be5ec1d373b6372bad06adb
BLAKE2b-256 checksum
How to use checksums
84ee1bf2e5d040737b0754a5e267d76613114382347b3ae1faa3436ac4af5950
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.13.5 Darwin/25.2.0

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page