Skip to content

Commit

Permalink
feat(ruff): enable tryceratops (#22382)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongbo-miao authored Jan 7, 2025
1 parent 4cfdd67 commit ccadd8f
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,13 @@ select = [
"I", # isort
"PGH", # pygrep-hooks
"PL", # Pylint
"TRY", # tryceratops
"UP", # pyupgrade
]
ignore = [
"E501",
"PLR0913",
"PLR0915",
"PLR2004",
"TRY003",
]
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def show_version_from_file(iads_config: Any, iads_config_path: Path) -> None:
try:
version = iads_config.VersionFromFile(iads_config_path)
logger.info(f"{version = }")
except Exception as e:
logger.exception(f"{iads_config_path = }, {e = }")
except Exception:
logger.exception(f"{iads_config_path = }")


def execute_query(iads_config: Any, query: str) -> None:
Expand All @@ -23,8 +23,8 @@ def execute_query(iads_config: Any, query: str) -> None:
if results:
for result in results:
logger.info(f"{result = }")
except Exception as e:
logger.exception(f"{e = }")
except Exception:
logger.exception("Failed to process IADS config")


def process_config(iads_config_path: Path) -> None:
Expand All @@ -41,8 +41,8 @@ def process_config(iads_config_path: Path) -> None:
execute_query(iads_config, "select Parameter from ParameterDefaults")

iads_config.Close(True)
except Exception as e:
logger.exception(f"{e = }")
except Exception:
logger.exception("Failed to close IADS config")
finally:
# Clean up COM resources
if iads_config:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ def get_iads_dataframe(
)
return df

except Exception as e:
logger.exception(f"{e = }")
except Exception:
logger.exception("Failed to process data")
return None

finally:
Expand Down
6 changes: 3 additions & 3 deletions machine-learning/dali/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


def download_sample_images(data_path: Path) -> None:
# Create main directory if it doesn not exist
# Create main directory if it does not exist
data_path.mkdir(parents=True, exist_ok=True)

# Create a class subdirectory (e.g., "class0")
Expand All @@ -30,8 +30,8 @@ def download_sample_images(data_path: Path) -> None:
if not filepath.exists():
logger.info(f"Downloading {url} to {filepath}")
urllib.request.urlretrieve(url, str(filepath))
except Exception as e:
logger.exception(f"Error downloading {url}: {e}")
except Exception:
logger.exception(f"Error downloading {url}")


@pipeline_def(batch_size=2, num_threads=2, device_id=None)
Expand Down
21 changes: 12 additions & 9 deletions machine-learning/hm-langgraph/applications/chat-pdf/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ def retrieve_context(
relevant_chunks = [chunks[idx] for idx in indices[0]]
state["context"] = "\n".join(relevant_chunks)
logger.info("Context retrieval completed")
return state
except Exception as e:
logger.error(f"Error in retrieve_context: {str(e)}", exc_info=True)
raise
else:
return state


def generate_answer(state: MessagesState) -> MessagesState:
Expand All @@ -127,18 +128,19 @@ def generate_answer(state: MessagesState) -> MessagesState:
],
)
state["answer"] = response.choices[0].message.content
return state
except Exception as e:
logger.error(f"Error in generate_answer: {str(e)}", exc_info=True)
raise
else:
return state


def create_graph(
index: faiss.IndexFlatIP,
chunks: list[str],
model: SentenceTransformer,
) -> Graph:
workflow = (
graph = (
Graph()
.add_node(
"retrieve", lambda state: retrieve_context(state, index, chunks, model)
Expand All @@ -148,7 +150,7 @@ def create_graph(
.set_entry_point("retrieve")
.set_finish_point("answer")
)
return workflow.compile()
return graph.compile()


def chat_with_pdf(pdf_path: Path, question: str) -> str:
Expand All @@ -163,14 +165,15 @@ def chat_with_pdf(pdf_path: Path, question: str) -> str:
)

# Create and run graph
workflow = create_graph(index, chunks, model)
for event in workflow.stream(initial_state):
graph = create_graph(index, chunks, model)
for event in graph.stream(initial_state):
if "answer" in event:
return event["answer"]
raise ValueError("No answer was found for the given question")
except Exception as e:
logger.error(f"Error in chat_with_pdf: {str(e)}")
except Exception:
logger.exception("Error in chat_with_pdf")
raise
else:
return "No answer was found for the given question"


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions machine-learning/stable-diffusion/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def generate_images(
path = output_dir / f"{timestamp}_{idx}.png"
image.save(path)

except Exception as e:
logger.exception(f"Error generating images: {e}")
except Exception:
logger.exception("Error generating images")


def main():
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ lint-cmake = { shell = "cmakelint $(git ls-files '**/CMakeLists.txt')" }
lint-matlab = { shell = "mh_style $(git ls-files '**/*.m')" }
lint-matlab-fix = { shell = "mh_style --fix $(git ls-files '**/*.m')" }
lint-python = { shell = "ruff format --check && ruff check" }
lint-python-fix = { shell = "ruff format && ruff check --fix" }
lint-python-fix = { shell = "ruff format && ruff check --fix --unsafe-fixes" }
lint-sql = "sqlfluff lint"
lint-sql-fix = "sqlfluff fix --force"
lint-vhdl = { shell = "vsg --filename $(git ls-files '**/*.vhd')" }
Expand Down

0 comments on commit ccadd8f

Please sign in to comment.