Last active
October 17, 2020 09:20
-
-
Save muthugit/63ea7b5f9bfc2e4116c784811f629b8d to your computer and use it in GitHub Desktop.
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
#!/usr/local/bin/python | |
# coding: utf-8 | |
__author__ = 'muthugit' | |
import re | |
class Conditional_Formatter: | |
"""Find the invalid if statements and replace with the correct one | |
:param file_path: A valid file path of the python file | |
:type file_path: String | |
:returns: Formatted code | |
:rtype: String | |
:example: | |
>>> obj = Conditional_Formatter('dummy.py') | |
>>> new_code = obj.new_code | |
.. note:: | |
This will be replaced | |
>>> if (status): | |
pass | |
To | |
>>> if status: | |
pass | |
""" | |
def __init__(self, file_path): | |
self.file_path = file_path | |
self.replace_string = {} | |
with open(file_path, 'r') as file: | |
self.content = file.read() | |
input_file = open(file_path, 'r') | |
self.lines = input_file.readlines() | |
self.convert() | |
def __get_valid_format(self): | |
for line in self.lines: | |
string = line.strip() | |
if string.startswith('if'): | |
r1 = re.findall(r"(?<=[(])(.*)(?=[)])", string) | |
if r1: | |
if '(' not in r1[0]: | |
self.replace_string[string] = 'if {}:'.format(r1[0]) | |
return self.replace_string | |
def apply_to_code(self): | |
"""Apply the converted statements to the existing code""" | |
for str in self.replace_string: | |
self.content = self.content.replace(str, self.replace_string[str]) | |
self.new_code = self.content | |
return self.content | |
def convert(self): | |
"""Convert""" | |
self.__get_valid_format() | |
self.apply_to_code() | |
if __name__ == "__main__": | |
obj = Conditional_Formatter('dummy.py') | |
print("======== CONVERTED CODE ============") | |
print(obj.new_code) |
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
status = True | |
second = 1 | |
if status: | |
print("Yes") | |
if (status): | |
print("Yes") | |
if (status == True): | |
print("Sub statement") | |
if (status and second==1): | |
print('yes') | |
if (status) and (second == 1): | |
print("yes") | |
if (status and second == 'a'): | |
print("yes") |
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
======== CONVERTED CODE ============ | |
status = True | |
second = 1 | |
if status: | |
print("Yes") | |
if status: | |
print("Yes") | |
if status == True: | |
print("Sub statement") | |
if status and second==1: | |
print('yes') | |
if (status) and (second == 1): | |
print("yes") | |
if status and second == 'a': | |
print("yes") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment