Skip to content

Instantly share code, notes, and snippets.

@nmagee
Last active October 12, 2026 14:02
Show Gist options
  • Select an option

  • Save nmagee/bcac46afda77240aae7f9168f3f29307 to your computer and use it in GitHub Desktop.

Select an option

Save nmagee/bcac46afda77240aae7f9168f3f29307 to your computer and use it in GitHub Desktop.

Python Classes (Object Definitions)

https://l.uvasds.sh/828
class User:
    def __init__(self, username, email, age):
        self.username = username
        self.email = email
        self.age = age

    def __repr__(self):
        return f"User(username={self.username!r}, email={self.email!r}, age={self.age})"
# or simpler
from dataclasses import dataclass

@dataclass
class User:
    username: str
    email: str
    age: int
# Example usage
user = User("alice", "alice@example.com", 30)
print(user)           # User(username='alice', email='alice@example.com', age=30)
print(user.username)  # alice

Load JSON into DuckDB

Simple JSON

Create a simple table in DuckDB and populate a couple of documents with JSON:

duckdb my.db
CREATE TABLE raw_events (id INTEGER, payload JSON);

INSERT INTO raw_events VALUES
  (1, '{"user": "alice", "tags": ["a", "b"], "meta": {"score": 42}}'),
  (2, '{"user": "bob", "tags": ["c", "b"], "meta": {"score": 38}}');

Select out specific JSON tree elements:

  SELECT
      id,
      payload->>'user'              AS user,
      (payload->>'$.meta.score')::INT AS score
  FROM raw_events;

Remote JSON

The base API call for GitHub user events:

https://api.github.com/users/torvalds/events/public

Let's import some of these events into a DuckDB database:

duckdb my.db
CREATE TABLE user_events AS
   SELECT * FROM read_json('https://api.github.com/users/torvalds/events/public');

Now inspect that table and count records.

Next, import more records by page:

INSERT INTO user_events
  SELECT * FROM read_json('https://api.github.com/users/torvalds/events/public?page=2');

Iterate that command repeatedly through at least page 5.

Inspect your table and count records.

  CREATE OR REPLACE TABLE torvalds AS
  SELECT id,
         type,
         actor.login   AS actor,
         repo.name     AS repo,
         payload.ref   AS ref,
         payload.head  AS head_sha,
         created_at
  FROM user_events;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment