AgentSkillsCN

python-case-conversion

借助pydash库,提供在Python中实现不同字符串大小写转换(camelCase、snake_case、kebab-case、PascalCase等)的指导。适用于用户要求转换字符串格式、按照命名规范重命名变量、对API响应进行转换,或处理不同编程语言的命名约定时使用。

SKILL.md
--- frontmatter
name: python-case-conversion
description: Provides guidance on converting strings between different cases in Python (camelCase, snake_case, kebab-case, PascalCase, etc.) using the pydash library. Use this skill when asked to convert string formats, rename variables to follow naming conventions, transform API responses, or work with different programming language conventions.

Python Case Conversion

Overview

This skill provides comprehensive guidance on converting strings between different case formats in Python using the pydash library. Convert between camelCase, snake_case, kebab-case, PascalCase, and many other formats commonly used in programming.

When to Use This Skill

Use this skill when:

  • Converting variable names between different naming conventions (e.g., Python's snake_case to JavaScript's camelCase)
  • Transforming API response field names to match local code conventions
  • Creating URL-friendly slugs from titles or descriptions
  • Formatting strings for display purposes (title case, start case, human case)
  • Adapting code between different programming languages with different naming conventions
  • Bulk renaming variables or fields to follow a style guide
  • Working with database column names and converting them to/from code variable names

Quick Start

To convert between string cases in Python, use the pydash library:

python
import pydash as _

# Convert to camelCase
_.camel_case("foo bar baz")  # "fooBarBaz"

# Convert to snake_case
_.snake_case("FooBarBaz")  # "foo_bar_baz"

# Convert to kebab-case
_.kebab_case("Foo Bar Baz")  # "foo-bar-baz"

# Convert to PascalCase
_.pascal_case("foo bar baz")  # "FooBarBaz"

# Create URL slug
_.slugify("Hello World!")  # "hello-world"

Common Conversion Tasks

Converting Variable Names Between Languages

When working with APIs or multi-language codebases:

python
import pydash as _

# Python to JavaScript
python_var = "user_email_address"
js_var = _.camel_case(python_var)  # "userEmailAddress"

# JavaScript to Python
js_field = "firstName"
python_field = _.snake_case(js_field)  # "first_name"

# Python to CSS
python_class = "primary_button"
css_class = _.kebab_case(python_class)  # "primary-button"

Creating URL Slugs

For blog posts, articles, or any URL-friendly strings:

python
import pydash as _

title = "My Amazing Blog Post!"
slug = _.slugify(title)  # "my-amazing-blog-post"

# With custom separator
slug = _.slugify(title, separator="_")  # "my_amazing_blog_post"

Display-Friendly Labels

Convert code variable names to user-friendly labels:

python
import pydash as _

# Database column to label
_.start_case("created_at")  # "Created At"
_.title_case("user_email")  # "User Email"

# Special handling for _id suffix
_.human_case("user_id")  # "User" (removes _id)

Bulk Transformations

When converting multiple fields at once:

python
import pydash as _

# Convert API response keys
api_response = {
    "firstName": "John",
    "lastName": "Doe",
    "emailAddress": "john@example.com"
}

python_response = {
    _.snake_case(key): value
    for key, value in api_response.items()
}
# Result: {"first_name": "John", "last_name": "Doe", "email_address": "john@example.com"}

Available Case Conversion Functions

For a complete reference of all available functions, see references/case_conversion_functions.md.

Key functions include:

  • camel_case() - Convert to camelCase (first letter lowercase)
  • pascal_case() - Convert to PascalCase (first letter uppercase)
  • snake_case() - Convert to snake_case (underscores)
  • kebab_case() - Convert to kebab-case (hyphens)
  • slugify() - Convert to URL-safe slug
  • title_case() - Convert to Title Case
  • start_case() - Convert to Start Case
  • human_case() - Convert to human-friendly format
  • upper_case() - Convert to UPPER CASE (with spaces)
  • lower_case() - Convert to lower case (with spaces)
  • separator_case(separator) - Convert using custom separator

Programming Convention Reference

Different contexts require different naming conventions:

ContextConventionFunction
Python variables/functionssnake_casesnake_case()
Python classesPascalCasepascal_case()
JavaScript variables/functionscamelCasecamel_case()
JavaScript classesPascalCasepascal_case()
CSS classeskebab-casekebab_case()
URLslowercase-with-hyphensslugify()
Database columnssnake_casesnake_case()
ConstantsSCREAMING_SNAKE_CASEsnake_case() + to_upper()
Display textTitle Casetitle_case() or start_case()

Installation

If pydash is not installed, install it with:

bash
pip install pydash

Detailed Reference

For comprehensive documentation including:

  • Complete function reference table with examples
  • Detailed usage patterns for each function
  • Unicode and special character handling
  • Advanced use cases and edge cases

See: references/case_conversion_functions.md