Skip to main content

Python package for working with North Carolina State Board of Elections (NCSBE) historical election data

Project description

License npm version npm downloads

📊 Coverage

Lines Functions Statements Branches

NCSBE Election Data Library

NCSBE Lib is a JavaScript library created by the Daily Tar Heel engineering team for working with North Carolina State Board of Elections (NCSBE) historical election data. The NCSBE provides live election results by updating a TSV file every five minutes, accessible via periodic GET requests. This library streamlines the process of fetching, extracting, and parsing election data, turning it into a more useful and easy to work with structure.

How It Works

Since the NCSBE re-uploads the entire dataset as a fresh snapshot every 5 minutes, this library is designed to replace the full dataset upon each refresh. Users can utilize this library in one of two ways:

  • Basic Usage (No Database): Simply call refresh() every five minutes to update the dataset. The in-memory dataset will always reflect the latest results, so functions like listContests() and getCandidates() will automatically reflect new data.
  • Advanced Usage (With a Database): If you plan to store election data in your own database, you should detect changes before updating records to avoid redundant processing. We recommend hashing each record and only updating entries when their hash has changed.

🔗 How we approached "Advanced Usage"

Features

  • Retrieve election data for a specific date.
  • List available contests (races).
  • List counties where voting occurred for a given contest.
  • List precincts within a county for a specific contest.
  • Retrieve candidate information and vote totals.
  • Refresh election data periodically to get the latest updates.
  • Filter election results by candidate, county, or precinct.

Installation

Ensure you have TypeScript installed in your project. You can install the node module using:

pip install ncsbe-lib

Usage

Importing and Initializing

from ncsbe_lib.ncsbe import NCSBE

ncsbe = NCSBE('2024-11-05')
ncsbe.initialize()

Refresh Data

// Replace dataSet with the entirety of the newly fetched TSV file.
ncsbe.refresh();

"Refreshing" will replace the entire dataset. The NCSBE continuously re-uploads the ZIP file as a full snapshot rather than an incremental update. Because of this, you will need to detect changes in the data to avoid unnecessary updates if storing this information in a database.

We recommend hashing each record and only updating entries when their hash has changed. This ensures that unchanged records are not unnecessarily reprocessed, reducing database load and preventing redundant updates.

Optimizing Database Updates With Hashing

In fact, we ran into this very problem before making this library and solved it via hashing. In our Firestore database, we stored each contest name as the key of the root collection, then stored all of the county and candidate data in fields/subcollections of the primary collection. When we "refreshed" (replaced the old file/dataset with the new one), we looped through every contest, hashed all of the data it held. If the hash differed, we updated that contest and if not, we skip the entire contest. This way, we only update contests in our database that actually saw changes which greatly improved space and efficiency.

Below is some pseudocode similar to our approach. We wrote our own hashService, but you can implement this however you want, the logic follows just the same.

await ncsbe.refresh();
const allData = ncsbe.dataSet;

for (const contest of allData) {
  // Compute the hash for the current contest data. We used Node's built-in crypto module.
  const currentHash = hashService.computeHash(contest);

  // Get the previous hash from our database, stored in its own collection, keyed by name of the contest.
  const previousHash = await hashService.getPreviousHash(contestName);

  // Did anything change? If not, skip this contest.
  if (currentHash === previousHash) {
    logger.info(`No changes detected for contest '${contest.contestName}'.`);
    continue;
  }

  //...rest of the function handling updating database
}

Additional Documentation

For a complete API reference, including details on functions such as retrieving candidate information, filtering data, and fetching vote totals, see the Full Documentation in docs/ncsbe.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ncsbe_lib-1.2.2.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ncsbe_lib-1.2.2-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file ncsbe_lib-1.2.2.tar.gz.

File metadata

  • Download URL: ncsbe_lib-1.2.2.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.6 Darwin/24.1.0

File hashes

Hashes for ncsbe_lib-1.2.2.tar.gz
Algorithm Hash digest
SHA256 dc67b86fb0450a0e82b8f157c7ffa19435baf19d630fe2938e9111c6b6bc93f9
MD5 6b3dd356803188fe9d590a44fc674a10
BLAKE2b-256 d2bb06106016a60ca6bfe687f5de1f886ac5055206fc0162bf8f7f51ff931e13

See more details on using hashes here.

File details

Details for the file ncsbe_lib-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: ncsbe_lib-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.6 Darwin/24.1.0

File hashes

Hashes for ncsbe_lib-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 60b75e1642da4f537763b899da36eb90895f16a7361aeb5be79bbabb3037e718
MD5 3fa90fb8c708c5ccbda61202d341c35b
BLAKE2b-256 23cf088b5ecea56669ef326fcbf55ea73c22dadcc3441ecf34f781747d7c4e74

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page