Python Code Generator: Templating Engine
This challenge asks you to build a simple code generator in Python. Code generators are incredibly useful for automating repetitive tasks, creating boilerplate code, and generating configuration files based on templates. Your generator will take a template string and a dictionary of variables, and produce a new string with the variables substituted into the template.
Problem Description
You are tasked with creating a Python function called generate_code that takes two arguments: a template string and a variables dictionary. The template string will contain placeholders enclosed in double curly braces {{variable_name}}. The variables dictionary will map variable names (strings) to their corresponding values. The function should replace each placeholder in the template string with the value from the variables dictionary. If a placeholder is not found in the dictionary, it should be left as is in the output string.
Key Requirements:
- The function must handle multiple placeholders within the template string.
- The function must correctly substitute values of different data types (strings, numbers, booleans) into the template.
- If a placeholder is not found in the variables dictionary, the placeholder itself should remain in the output string.
- The function should be robust and handle potential errors gracefully (e.g., invalid input types).
Expected Behavior:
The generate_code function should return a new string with all placeholders replaced by their corresponding values from the variables dictionary. The original template string and variables dictionary should remain unchanged.
Edge Cases to Consider:
- Empty template string.
- Empty variables dictionary.
- Placeholders with invalid variable names (e.g., containing spaces or special characters).
- Nested curly braces (e.g.,
{{{{variable}}}}). These should be treated as literal characters. - Placeholders that are substrings of other placeholders (e.g., template:
{{long_var}}, variables:{'long_var': 'value', 'long': 'other_value'}). Only the longest matching placeholder should be replaced.
Examples
Example 1:
Input: template = "Hello, {{name}}! You are {{age}} years old.", variables = {"name": "Alice", "age": 30}
Output: "Hello, Alice! You are 30 years old."
Explanation: The placeholders `{{name}}` and `{{age}}` are replaced with their corresponding values from the variables dictionary.
Example 2:
Input: template = "The value is {{value}}.", variables = {"value": 123}
Output: "The value is 123."
Explanation: The placeholder `{{value}}` is replaced with the integer value 123.
Example 3:
Input: template = "Is it {{is_active}}?", variables = {"is_active": True}
Output: "Is it True?"
Explanation: The placeholder `{{is_active}}` is replaced with the boolean value True.
Example 4:
Input: template = "Missing variable: {{missing}}", variables = {"existing": "value"}
Output: "Missing variable: {{missing}}"
Explanation: The placeholder `{{missing}}` is not found in the variables dictionary, so it remains unchanged.
Example 5:
Input: template = "Nested braces: {{{{variable}}}}", variables = {"variable": "value"}
Output: "Nested braces: {{variable}}"
Explanation: Nested curly braces are treated as literal characters.
Constraints
- The template string will be a string.
- The variables dictionary will be a dictionary with string keys and values of any type.
- The length of the template string can be up to 1000 characters.
- The number of variables in the dictionary can be up to 50.
- The function should execute in under 0.1 seconds for typical inputs.
Notes
Consider using string formatting techniques or regular expressions to implement the code generator. Pay close attention to edge cases, especially those involving missing variables and nested curly braces. Prioritize readability and maintainability in your code. Think about how you can handle potential errors gracefully, such as invalid input types. Remember to only replace the longest matching placeholder if there are overlapping placeholders.