Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

When wrapping a Python reserved keyword as a parameter, use xxx_ instead of _xxx to ensure proper autocomplete functionality. #420

Open
lucemia opened this issue Jan 9, 2025 · 2 comments
Assignees

Comments

@lucemia
Copy link
Contributor

lucemia commented Jan 9, 2025

No description provided.

@lucemia lucemia self-assigned this Jan 9, 2025
@lucemia
Copy link
Contributor Author

lucemia commented Jan 26, 2025

the issue is that some parameters is start from number

@lucemia
Copy link
Contributor Author

lucemia commented Feb 3, 2025

When writing an FFMPEG wrapper in Python, handling parameters that conflict with Python keywords (e.g., pass) or those that start with numbers (e.g., 3d) requires a clean and intuitive naming convention. Here are some good approaches:

Recommended Naming Strategies

  1. Use a Trailing Underscore for Keywords

Python allows identifiers with a trailing underscore to avoid conflicts:

def ffmpeg_filter(pass_: str, format_: str):
return f"-filter:v format={format_} -filter:a pass={pass_}"

✅ Pros:
• Readable and follows Python’s naming conventions.
• Works well with autocompletion.

  1. Prefix Numbers with a Letter

For parameters that start with a number (e.g., 3d), prefixing with a letter (such as _ or n) ensures valid Python identifiers:

def ffmpeg_filter(n3d: str):
return f"-vf 3d={n3d}"

Alternatively, an underscore prefix:

def ffmpeg_filter(_3d: str):
return f"-vf 3d={_3d}"

✅ Pros:
• Keeps parameter names close to their original meaning.
• Avoids unnecessary transformations.

  1. Use a Dictionary for Flexible Parameter Naming

Instead of using Python parameter names directly, pass a dictionary where keys match the original FFMPEG parameter names:

def ffmpeg_filter(**kwargs):
return " ".join(f"-{k} {v}" for k, v in kwargs.items())

Usage

cmd = ffmpeg_filter(pass="fast", format="yuv420p", _3d="depth")

✅ Pros:
• Allows full flexibility without renaming parameters.
• Avoids conflicts with Python keywords.

❌ Cons:
• No IDE autocomplete for parameters.
• Requires manual dictionary handling.

  1. Use a Mapping Dictionary Internally

For a balance of flexibility and readability, use a mapping dictionary to translate Python-friendly names to FFMPEG’s parameter names:

FFMPEG_PARAMS = {
"pass_": "pass",
"format_": "format",
"n3d": "3d",
}

def ffmpeg_filter(**kwargs):
translated = {FFMPEG_PARAMS.get(k, k): v for k, v in kwargs.items()}
return " ".join(f"-{k} {v}" for k, v in translated.items())

Usage

cmd = ffmpeg_filter(pass_="fast", format_="yuv420p", n3d="depth")

✅ Pros:
• Keeps the original names recognizable.
• Avoids conflicts while allowing IDE autocomplete.
• Makes it explicit which parameters are handled.

Best Approach?

If you want readability and IDE autocomplete, I recommend trailing underscores for keywords (pass_, format_) and prefixing numbers with n or _ (n3d, _3d).

If you prefer maximum flexibility, use a dictionary-based approach to avoid conflicts altogether.

Would you like me to draft a reusable wrapper function for your use case? 🚀

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant