Skip to content
Back to skills

Cascade Type Conversion

ASecurity

For flexible parsing: try multiple type conversions in order, graceful fallback from specific to general types.

  • 4 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 7, 2026
ai-agentspythonperformance

Security analysis

A100/100

Scanned November 7, 2026

npx -y skills add jimmc414/claude-code-plugin-marketplace --skill cascade-type-conversion --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Cascade Type Conversion?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Cascade Type Conversion
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/jimmc414-cascade-type-conversion/badge)](https://www.skillsdirectory.com/skills/jimmc414-cascade-type-conversion)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cascade-type-conversion
description: "For flexible parsing: try multiple type conversions in order, graceful fallback from specific to general types."
---

# cascade-type-conversion

## When to Use
- Parsing mixed-type data
- User input that could be various types
- "Best effort" type inference
- Atom/literal parsing in interpreters

## When NOT to Use
- Known fixed types
- Type ambiguity is a bug, not a feature
- Performance-critical inner loops

## The Pattern

Try conversions from most specific to most general, catching failures.

```python
def parse_value(s):
    """Parse string as most specific type possible."""
    # Try int first
    try:
        return int(s)
    except ValueError:
        pass

    # Try float
    try:
        return float(s)
    except ValueError:
        pass

    # Try bool
    if s.lower() in ('true', 'false'):
        return s.lower() == 'true'

    # Fall back to string
    return s
```

## Example (from pytudes)

```python
# Lisp atom parsing (lispy.py)
def atom(token):
    """Numbers become numbers; #t/#f are booleans; strings stay strings."""
    if token == '#t':
        return True
    elif token == '#f':
        return False
    elif token[0] == '"':
        return token[1:-1]  # Strip quotes

    try:
        return int(token)
    except ValueError:
        try:
            return float(token)
        except ValueError:
            try:
                return complex(token.replace('i', 'j', 1))
            except ValueError:
                return Sym(token)

# Simpler version (lis.py)
def atom(token):
    """Numbers become numbers; every other token is a symbol."""
    try:
        return int(token)
    except ValueError:
        try:
            return float(token)
        except ValueError:
            return Symbol(token)

# Advent of Code parsing (AdventUtils.ipynb)
def atom(text: str):
    """Parse text into a single float or int or str."""
    try:
        x = float(text)
        return round(x) if x.is_integer() else x
    except ValueError:
        return text.strip()

# Version compatibility (beal.py)
try:
    from math import gcd       # Python 3.6+
except ImportError:
    from fractions import gcd  # Older Python
```

## Key Principles
1. **Specific to general**: Try int before float before string
2. **Catch ValueError**: Standard exception for failed conversions
3. **Chain try/except**: Each failure falls through to next
4. **Final fallback**: Always have a catch-all (usually string)
5. **Return in try block**: Conversion succeeded, return immediately

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.