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 during instantiation:

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

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

# You can also use different paths
dc2, result2 = Dotc(staff_data, _pathget='staff.coders.1')
print(result2)  # jeremie

dc3, result3 = Dotc(staff_data, _pathget='staff.coders.2')
print(result3)  # 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 during instantiation
processor, first_coder = Dotc(staff_data, _pathget='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, _pathget=None, **kwargs) - Create a new Dotc object
  • When _pathget is provided, returns (instance, result) tuple

Instance Methods

  • 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.1.tar.gz (509.3 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.1-py3-none-any.whl (499.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dotc-0.4.1.tar.gz
  • Upload date:
  • Size: 509.3 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.1.tar.gz
Algorithm Hash digest
SHA256 7b026b0875cbb7e8f2cd8d68f8bea09db8003f5adaeabbb267f84621aaf1d66f
MD5 c892923c59683e13b737e2a0da638e81
BLAKE2b-256 898340837112554d5aa920f1808ed9581bb16452648e7acbc5e3501e0f413aba

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dotc-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 499.9 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8e68f566a99b7b8e992b9fe773c2c905f5267ff3b5166c982e077a35f3815eea
MD5 9f37f251e940bba9af12850c5ffb3ab8
BLAKE2b-256 7a696db4bf6345eeac77d5f4f4a103dc329ec525404f2d58ca772d433bdf1d66

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