Skip to content

Commit

Permalink
fix: small changes to comply with ruff linting
Browse files Browse the repository at this point in the history
  • Loading branch information
AngRodrigues committed Sep 25, 2024
1 parent 8b096ce commit 9ca31de
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 75 deletions.
4 changes: 2 additions & 2 deletions LoopProjectFile/LoopProjectFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def OpenProjectFile(filename, readOnly=True, verbose=False):

# Quick check to see if openable
try:
with open(filename, 'rb') as f:
with open(filename, 'rb'):
if (verbose):
print(f"File {filename} opened successfully.", file=sys.stderr)
except Exception as e:
Expand Down Expand Up @@ -809,7 +809,7 @@ def ConvertDataFrame(df, dtype):
if isinstance(df, pandas.DataFrame):
return numpy.array(df.to_records(index=False).tolist(), dtype=dtype)
else:
raise NotADataFrame
raise TypeError("Input is not a DataFrame")


def CheckFileIsLoopProjectFile(filename, verbose=False):
Expand Down
69 changes: 3 additions & 66 deletions LoopProjectFile/LoopProjectFileUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def ElementFromDataframe(loopFilename, df, element, loopCompoundType):
" does not match\n Compound type:",
loopCompoundType.names,
)
raise Exception(f"In dataframe columns do not match compound type")
raise Exception("In dataframe columns do not match compound type")
try:
struct = LoopProjectFile.ConvertDataFrame(df, loopCompoundType)
resp = LoopProjectFile.Set(loopFilename, element, data=struct)
Expand Down Expand Up @@ -138,7 +138,7 @@ def FromCsv(loopFilename, importPath, overwrite=False):
os.remove(loopFilename)
else:
print(loopFilename, "already exists and overwrite not set", file=sys.stderr)
raise Exception(f"already exists and overwrite not set")
raise Exception("already exists and overwrite not set")

importPath = importPath.replace("\\", "/")
if importPath[-1] != "/" and importPath[-1] != "\\":
Expand Down Expand Up @@ -306,7 +306,7 @@ def ElementToDataframe(loopFilename, element, loopCompoundType):
columns = list(loopCompoundType.names)
df = pandas.DataFrame.from_records(resp["value"], columns=columns)
for name in columns:
if type(loopCompoundType[name]) != numpy.dtypes.VoidDType:
if type(loopCompoundType[name]) is not numpy.dtypes.VoidDType:
df[name] = df[name].astype(loopCompoundType[name])
df = df.map(lambda x: x.decode() if isinstance(x, bytes) else x)
# df.set_index(columns[0], inplace=True)
Expand Down Expand Up @@ -535,69 +535,6 @@ def handleLoopProjectFile(file, shared_path="/shared"):
raise Exception("No file was provided for upload.")


def handleCSVlist(files, loopFilename, shared_path="/shared"):
if not loopFilename:
raise Exception("loopFilename is required")
if not files:
raise Exception("No CSV files provided")

loop_file_path = os.path.join(shared_path, loopFilename)
saved_files = []

for file_storage in files.getlist("file"):
if file_storage and file_storage.filename.endswith(".csv"):
filepath = os.path.join(shared_path, file_storage.filename)
file_storage.save(filepath)
saved_files.append(filepath)

try:
FromCsv(loop_file_path, shared_path)
except Exception as conversion_error:
for csv_file in saved_files:
try:
os.remove(os.path.join(shared_path, csv_file))
except Exception as e:
print(f"Failed to delete CSV file {csv_file}: {e}")
if os.path.exists(loop_file_path):
try:
os.remove(loop_file_path)
except Exception as e:
print(f"Failed to delete project file {loop_file_path}: {e}")
raise conversion_error
else:
for csv_file in saved_files:
try:
os.remove(os.path.join(shared_path, csv_file))
except Exception as e:
print(f"Failed to delete CSV file {csv_file}: {e}")

return "success", f"{loopFilename} is created and saved successfully"


def handleLoopProjectFile(file, shared_path="/shared"):
if file:
filename = file.filename

if not filename.endswith(".loop3d"):
filename += ".loop3d"

filepath = os.path.join(shared_path, filename)

if os.path.exists(filepath):
raise Exception(f"File {filename} already exists in the shared path.")

file.save(filepath)

if not LoopProjectFile.CheckFileValid(filepath):
os.remove(filepath)
raise Exception("Uploaded file is not a valid LoopProjectFile.")

return

else:
raise Exception("No file was provided for upload.")


def handleCSVlist(files, loopFilename, shared_path="/shared"):
if not loopFilename:
raise Exception("loopFilename is required")
Expand Down
11 changes: 5 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ quiet-level = 3

[tool.ruff]
exclude = ['.git', 'pycache__', 'build', 'dist', 'doc/examples', 'doc/_build']
external = ["E131", "D102", "D105"]
lint.external = ["E131", "D102", "D105"]
line-length = 100
indent-width = 4
target-version = 'py39'

[tool.ruff.lint]
ignore = [
lint.ignore = [
# whitespace before ':'
"E203",
# line break before binary operator
Expand All @@ -117,9 +116,9 @@ ignore = [
# 'from module import *' used; unable to detect undefined names
"F403",
]
fixable = ["ALL"]
unfixable = []
extend-select = ["B007", "B010", "C4", "F", "NPY", "PGH004", "RSE", "RUF100"]
lint.fixable = ["ALL"]
lint.unfixable = []
lint.extend-select = ["B007", "B010", "C4", "F", "NPY", "PGH004", "RSE", "RUF100"]

[tool.ruff.lint.flake8-comprehensions]
allow-dict-calls-with-keyword-arguments = true
1 change: 0 additions & 1 deletion tests/test_projectfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from LoopProjectFile import ProjectFile
import pytest
# import pandas as pd
# import numpy as np

Expand Down

0 comments on commit 9ca31de

Please sign in to comment.