Created
May 6, 2024 14:41
-
-
Save aashishtamsya/312ff6d40c4f9acdeed82fdb21e3d61d to your computer and use it in GitHub Desktop.
This Python script defines a function to filter dictionary keys that are in snake_case format. Snake_case is a naming convention where words are separated by underscores and all letters are lowercase, commonly used in Python for variable names and keys in JSON data. The script provides a function called `is_snake_case` to check if a string follo…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def is_snake_case(key): | |
""" | |
Check if a string is in snake_case format. | |
Parameters: | |
key (str): The string to check. | |
Returns: | |
bool: True if the string is in snake_case format, False otherwise. | |
""" | |
# Define the regex pattern for snake_case | |
pattern = r'^[a-z][a-z0-9_]*$' | |
# Check if the key matches the pattern | |
return bool(re.match(pattern, key)) | |
# Example usage: | |
keys = ['snake_case', 'CamelCase', 'kebab-case', 'valid_key_123', '123_invalid_key'] | |
for key in keys: | |
print(f'"{key}" is snake_case: {is_snake_case(key)}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment