-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable_checker.py
95 lines (78 loc) · 3.46 KB
/
variable_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import keyword
import re
from rich.console import Console
from rich.text import Text
from pyfiglet import Figlet
from rich.live import Live
import time
# Initialize rich console
console = Console()
# Create a function to print a dynamic ASCII banner with animation
def print_animated_banner():
banner_text = "pyVChecker"
f = Figlet(font='slant')
for i in range(1, len(banner_text)+1):
banner = f.renderText(banner_text[:i])
console.clear()
console.print(Text(banner, style="bold cyan"))
time.sleep(0.1)
# Check if variable name is valid
def check_variable_name(variable_name):
pattern = r'^[A-Za-z_]\w*$'
# Check if the variable name is a Python keyword
if keyword.iskeyword(variable_name):
return Text(f"Invalid variable: '{variable_name}' is a Python keyword.", style="bold red")
# Check if it matches the valid pattern
if re.match(pattern, variable_name):
return Text(f"Correct: '{variable_name}' is a valid variable name!", style="bold green")
else:
return Text(f"Invalid variable: '{variable_name}' does not follow variable naming rules.", style="bold yellow")
# Suggest modifications to correct an invalid variable name
def suggest_corrections(variable_name):
if not variable_name[0].isalpha() and variable_name[0] != '_':
variable_name = '_' + variable_name # Prefix an underscore if the first character is invalid
# Remove any invalid characters from the name
variable_name = re.sub(r'[^A-Za-z0-9_]', '', variable_name)
return variable_name
# Handle multiple variable checks
def check_multiple_variables():
console.print(Text("Enter variable names separated by commas:", style="bold magenta"))
variable_names = console.input().split(',')
for var in variable_names:
var = var.strip() # Remove leading/trailing spaces
result = check_variable_name(var)
console.print(result)
if "Invalid" in result.plain:
suggestion = suggest_corrections(var)
console.print(Text(f"Suggested valid variable: '{suggestion}'", style="bold cyan"))
# Main function to run the tool
def main():
# Display animated banner
print_animated_banner()
# Ask if user wants to check multiple variables
choice = console.input("[bold magenta]Do you want to check multiple variable names? (yes/no): [/bold magenta] ").strip().lower()
if choice == 'yes':
check_multiple_variables()
else:
# Single variable check
variable_name = console.input("[bold magenta]Enter a variable name to check: [/bold magenta] ")
result = check_variable_name(variable_name)
console.print(result)
# Provide suggestions if invalid
if "Invalid" in result.plain:
suggestion = suggest_corrections(variable_name)
console.print(Text(f"Suggested valid variable: '{suggestion}'", style="bold cyan"))
# Instructions on how to create valid variables
console.print("""
[bold yellow]How to create a valid variable: [/bold yellow]
1. Start with a letter or underscore (_).
2. Use only letters, numbers, and underscores.
3. Avoid using Python keywords like 'if', 'while', etc.
4. Remember that variable names are case-sensitive.
[bold green]Example of valid variables: [/bold green]
- my_var
- _myVariable
- user123
""")
if __name__ == "__main__":
main()