You can simplify the code by creating a function to wrap the creation of the stylable container and button. This way, you can reduce redundancy. Here's a shorter version:
This approach keeps the code concise and maintains clarity by encapsulating the repetitive logic in a function.
User
Assistant
User
give ruff code for formatting arguments
Assistant
In Python, you can use Ruff to check for formatting issues in your code, including function arguments. However, Ruff is primarily a linter and does not directly modify code for formatting like Black or autopep8 would. But, you can write code in a style that Ruff would support regarding function arguments, especially concerning PEP 8 style guidelines.
please simplify the code to less number of lines, and optimize it
code
with inputs_section:
repo_container = st.container()
repo_url = repo_container.text_input('Enter BitBucket repo link here:', value=st.session_state.utils.repo_url)
cloning_output = repo_container.container()
repo_path = f"cloned_repos/{repo_url.split('/')[-1]}/" # TODO: capture proper repo name
st.session_state.utils.update(repo_url=repo_url, repo_path=repo_path)
if repo_url and os.path.isdir(repo_path) and not st.session_state.utils.repo_cloned:
with cloning_output:
st.session_state.utils.repo_exists()
elif repo_url and not os.path.isdir(repo_path):
st.session_state.utils.clone_repo()
file_selection_section, method_selection_section = st.columns([0.5, 0.5], gap='small', vertical_alignment='top')
with file_selection_section:
st.markdown(
"<p style='font-size: 14px;'>Select the file you want to test :</p>",
unsafe_allow_html=True,
)
if repo_path != 'cloned_repos//' and os.path.isdir(repo_path):
repo_tree_data = make_repo_tree_structure(repo_path)
disabled_treeview = False
search_visible = True
selected_keys = [] if st.session_state.utils.file_path is None else [st.session_state.utils.file_path]
else:
repo_tree_data = []
disabled_treeview = True
search_visible = False
selected_keys = []
file_path = st_ant_tree(
treeData=repo_tree_data,
key='file_select',
allowClear=False,
bordered=True,
max_height=400,
filterTreeNode=True,
multiple=False,
placeholder='Choose a File',
showArrow=True,
showSearch=search_visible,
treeCheckable=False,
width_dropdown='100%',
disabled=disabled_treeview,
maxTagCount=5,
)
st.session_state.utils.update(file_path=file_path)
if file_path is not None and file_path.endswith('.py'):
file_path = file_path.replace('\\', '/')
file_name = os.path.basename(file_path)
packages, complete_methods = st.session_state.utils.process_file(file_path)
st.session_state.utils.update(
file_name=file_name,
packages=packages,
complete_methods=complete_methods,
)
elif file_path is not None:
st.warning('Select a .py file')
with method_selection_section:
st.markdown(
"<p style='font-size: 14px;'>Select the method/function you want to test :</p>",
unsafe_allow_html=True,
)
if file_path and not disabled_treeview:
function_tree_data = transform_module_to_tree(st.session_state.utils.complete_methods)
disabled_treeview = False
search_visible = True
else:
function_tree_data = []
disabled_treeview = True
search_visible = False
module_name = st_ant_tree(
treeData=function_tree_data,
key='module_select',
allowClear=False,
bordered=True,
max_height=400,
filterTreeNode=True,
multiple=False,
placeholder='Choose a method/function',
showArrow=True,
showSearch=search_visible,
treeCheckable=False,
width_dropdown='100%',
disabled=disabled_treeview,
maxTagCount=5,
)
if module_name is not None:
method_name, method_definition = find_selected_method(nested_dict=complete_methods, target_key=module_name)
pprint(f'{method_name}, {method_definition}')
st.session_state.utils.update(method_name=method_name, method_definition=method_definition)
objective = st.text_area('Provide objective of the function (optional)')
sample_data = st.text_area('Provide sample data for the function (optional)')
st.session_state.utils.update(objective=objective, sample_data=sample_data)
scenario_button = sac.buttons(
['Generate Test Scenarios'],
index=None,
align='left',
variant='filled',
color='#7443ee',
return_index=True,
)
Certainly! You can streamline the code by grouping repetitive tasks into functions, reducing the number of lines while maintaining clarity and functionality. Here's a more optimized version of your code:
python
def update_repo_state(repo_url):
repo_path = f"cloned_repos/{repo_url.split('/')[-1]}/"
st.session_state.utils.update(repo_url=repo_url, repo_path=repo_path)
return repo_path
def handle_repo_cloning(repo_url, repo_path):
if repo_url:
if os.path.isdir(repo_path) and not st.session_state.utils.repo_cloned:
st.session_state.utils.repo_exists()
elif not os.path.isdir(repo_path):
st.session_state.utils.clone_repo()
def create_tree_selection(tree_data, placeholder, disabled, search_visible):
return st_ant_tree(
treeData=tree_data,
key=placeholder.replace(" ", "_").lower() + '_select',
allowClear=False,
bordered=True,
max_height=400,
filterTreeNode=True,
multiple=False,
placeholder=placeholder,
showArrow=True,
showSearch=search_visible,
treeCheckable=False,
width_dropdown='100%',
disabled=disabled,
maxTagCount=5,
)
def update_function_data(file_path):
if file_path and file_path.endswith('.py'):
file_path = file_path.replace('\\', '/')
file_name = os.path.basename(file_path)
packages, complete_methods = st.session_state.utils.process_file(file_path)
st.session_state.utils.update(file_name=file_name, packages=packages, complete_methods=complete_methods)
return complete_methods
elif file_path is not None:
st.warning('Select a .py file')
return None
with inputs_section:
repo_container = st.container()
repo_url = repo_container.text_input('Enter BitBucket repo link here:', value=st.session_state.utils.repo_url)
repo_path = update_repo_state(repo_url)
handle_repo_cloning(repo_url, repo_path)
file_selection_section, method_selection_section = st.columns([0.5, 0.5], gap='small', vertical_alignment='top')
with file_selection_section:
st.markdown("<p style='font-size: 14px;'>Select the file you want to test :</p>", unsafe_allow_html=True)
repo_tree_data = make_repo_tree_structure(repo_path) if repo_path != 'cloned_repos//' and os.path.isdir(repo_path) else []
file_path = create_tree_selection(repo_tree_data, 'Choose a File', not repo_tree_data, bool(repo_tree_data))
st.session_state.utils.update(file_path=file_path)
complete_methods = update_function_data(file_path)
with method_selection_section:
st.markdown("<p style='font-size: 14px;'>Select the method/function you want to test :</p>", unsafe_allow_html=True)
function_tree_data = transform_module_to_tree(complete_methods) if complete_methods else []
module_name = create_tree_selection(function_tree_data, 'Choose a method/function', not function_tree_data, bool(function_tree_data))
if module_name is not None:
method_name, method_definition = find_selected_method(nested_dict=complete_methods, target_key=module_name)
st.session_state.utils.update(method_name=method_name, method_definition=method_definition)
objective = st.text_area('Provide objective of the function (optional)')
sample_data = st.text_area('Provide sample data for the function (optional)')
st.session_state.utils.update(objective=objective, sample_data=sample_data)
scenario_button = sac.buttons(
['Generate Test Scenarios'],
index=None,
align='left',
variant='filled',
color='#7443ee',
return_index=True,
)
Key Changes Made:
Functions: Created helper functions (update_repo_state, handle_repo_cloning, create_tree_selection, and update_function_data) to reduce redundancy and improve readability.
Streamlined Logic: Removed unnecessary checks by passing boolean values and simplified the conditional logic in file and function selection.
Consistent Usage: Kept the user prompts and container management fluid while minimizing repetitive setups for similar tasks.
This optimized version should be cleaner and more maintainable while preserving the original functionality.
Here’s an example of how to format function arguments appropriately:
To use Ruff to check your code formatting for argument styles:
Install Ruff: If you haven't already, install it via pip:
bash
pip install ruff
Run Ruff: You can use it to lint your Python files:
bash
ruff check your_file.py
Read the Documentation: Check Ruff's documentation for more linting rules and usage: Ruff Documentation.
Conclusion
If you're looking to ensure that your function arguments are formatted correctly, follow the above examples and guidelines, then use Ruff to lint your code. Ruff will help point out issues that deviate from these formatting practices, ensuring cleaner code.