Skip to main content

DOTC (like Yahtzee) - Access Nested Dicts and Lists via Dots

Project description

DOTC 🎯

Access nested Python data structures using simple dot notation

Transform complex nested dictionaries and lists into easily navigable objects with dot notation access.

Quick Start

pip install dotc
from dotc import Dotc

# Your nested data
data = {
    'user': {
        'name': 'Alice',
        'scores': [85, 92, 78]
    }
}

# Convert to dot-accessible object
d = Dotc(data)

# Access with simple dots
print(d.user.name)      # Alice
print(d.user.scores._0) # 85

✨ Core Features

🔍 Simple Dot Access

Access nested data structures naturally:

data = {'a': 1, 'b': {'c': 3, 'd': [4, 5, 6]}}
d = Dotc(data)

d.a           # Returns: 1
d.b.c         # Returns: 3
d.b.d._0      # Returns: 4 (list access with _index)

🚀 Instant Results with Spawn

Get both the object and a specific value in one call:

# Get object and value simultaneously
staff_data = {
    'staff': {
        'coders': ['mike', 'jeremie', 'trey', 'donnie']
    }
}

# Method 1: During instantiation with _pathget
dc, result = Dotc(staff_data, _pathget='staff.coders.0')
print(result)  # mike

# Method 2: Using spawn method
dc = Dotc(staff_data)
dc, result = dc.spawn('staff.coders.1')
print(result)  # jeremie

# Method 3: Using factory method
dc, result = Dotc.create_with_result(staff_data, 'staff.coders.2')
print(result)  # trey

🎯 Programmatic Path Access with __call__

Use the call method to get values by path after instantiation:

staff_data = {
    'staff': {
        'coders': ['mike', 'jeremie', 'trey', 'donnie']
    }
}

# Create with initial path extraction
dc, result = Dotc(staff_data, _pathget='staff.coders.0')
print(result)  # mike

# Then use __call__ for additional path queries
result2 = dc('staff.coders.1')  # jeremie
result3 = dc('staff.coders.3')  # donnie

🛡️ Safe Access with Defaults

Never worry about missing keys:

d = Dotc({'a': {'b': 1}})

d.a.b         # Returns: 1
d.a.missing   # Returns: None (default)
d.x.y.z       # Returns: None (safe traversal)

🔄 Full Data Resolution

Get the complete resolved data structure:

d = Dotc({'a': 1, 'b': {'c': [2, 3]}})

d._           # Returns: {'a': 1, 'b': {'c': [2, 3]}}
d.b._         # Returns: {'c': [2, 3]}
d.b.c._       # Returns: [2, 3]

📖 Tutorial

Basic Usage

from dotc import Dotc

# Simple dictionary
data = {'name': 'John', 'age': 30}
d = Dotc(data)
print(d.name)  # John
print(d.age)   # 30

Nested Dictionaries

data = {
    'person': {
        'details': {
            'name': 'Jane',
            'location': 'NYC'
        }
    }
}

d = Dotc(data)
print(d.person.details.name)      # Jane
print(d.person.details.location)  # NYC

Working with Lists

data = {
    'fruits': ['apple', 'banana', 'cherry'],
    'numbers': [1, 2, 3, 4, 5]
}

d = Dotc(data)
print(d.fruits._0)    # apple
print(d.fruits._1)    # banana
print(d.numbers._4)   # 5

Mixed Nested Structures

data = {
    'users': [
        {'name': 'Alice', 'scores': [95, 87]},
        {'name': 'Bob', 'scores': [78, 92]}
    ]
}

d = Dotc(data)
print(d.users._0.name)        # Alice
print(d.users._0.scores._0)   # 95
print(d.users._1.scores._1)   # 92

🎯 Advanced Features

Inspection and Debugging

d = Dotc({'a': 1, 'b': {'c': [1, 2, 3]}})

# Basic inspection
d._show()

# Verbose inspection
d._show(v=1)

# Inspect specific parts
d._show(d.b, v=1)

Using DataPath for Programmatic Access

For cases where you need programmatic path traversal:

from dotc import DataPath

data = {'a': {'b': [1, 2, 3]}}
d = Dotc(data)

dp = DataPath()
result = dp.get('a.b.0', d)  # Returns: 1

# Works with regular Python objects too
result = dp.get('a.b.0', data)  # Returns: 1

🔧 Configuration Options

d = Dotc(
    data={'a': 1},
    node='custom_name',      # Custom node name
    default='N/A',           # Custom default value
    _strict=1,              # Raise errors for missing keys
    _debug=1                # Enable debug output
)

🎪 Use Cases

Configuration Management

config = {
    'database': {'host': 'localhost', 'port': 5432},
    'api': {'timeout': 30, 'retries': 3}
}

cfg = Dotc(config)
db_host = cfg.database.host      # localhost
api_timeout = cfg.api.timeout    # 30

JSON API Response Handling

api_response = {
    'data': {
        'user': {'id': 123, 'profile': {'email': 'user@example.com'}}
    }
}

resp = Dotc(api_response)
email = resp.data.user.profile.email  # user@example.com

Data Processing Pipelines

# Get object and extract value in one step
staff_data = {
    'staff': {
        'coders': ['mike', 'jeremie', 'trey', 'donnie'],
        'metrics': {'team_size': 4, 'experience': 'senior'}
    }
}

# Extract initial value and setup for more queries
processor, first_coder = Dotc.create_with_result(staff_data, 'staff.coders.0')
print(f"Lead developer: {first_coder}")  # Lead developer: mike

# Use the same object for additional queries
team_size = processor('staff.metrics.team_size')
print(f"Team size: {team_size}")  # Team size: 4

📚 API Reference

Class Methods

  • Dotc(data, node=None, default=None, **kwargs) - Create a new Dotc object
  • Dotc.create_with_result(data, pathget, **kwargs) - Create and extract value simultaneously

Instance Methods

  • obj.spawn(pathget) - Returns (self, result) tuple
  • obj(path) - Get value at path programmatically
  • obj._ - Get fully resolved data structure
  • obj._show(verbosity=0) - Inspect object structure

Utility Classes

  • DataPath.get(path, obj, default) - Static path traversal

DOTC - Making nested data navigation as easy as ABC! 🎯

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

dotc-0.4.0.tar.gz (510.1 kB view details)

Uploaded Source

Built Distribution

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

dotc-0.4.0-py3-none-any.whl (500.5 kB view details)

Uploaded Python 3

File details

Details for the file dotc-0.4.0.tar.gz.

File metadata

  • Download URL: dotc-0.4.0.tar.gz
  • Upload date:
  • Size: 510.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for dotc-0.4.0.tar.gz
Algorithm Hash digest
SHA256 3b4e2a23fb9f6a7666ef33cdf6161c3ef9c2853097a08af0d3267fa4423ff043
MD5 fc0b48359d0d54dfdf9f61aa846e72c0
BLAKE2b-256 b3c234949d586f2e7f90df5351bbd0ad389557247f4b8e270952f1389201a17f

See more details on using hashes here.

File details

Details for the file dotc-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: dotc-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 500.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for dotc-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba490d3afbcc1501776ca5d9de1995ac908c861cf071d21758b3c790b6d29dd2
MD5 2eacdf321ff21b10175a5ca2cec147a0
BLAKE2b-256 337ef49a37d06f39cec20d45b0c3ecc7976270b7ec92cc3eab2355bc7bfaa4cf

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