Skip to main content

Add a function for formatting Python code

ID
5099ae6
date
2024-03-16 19:44:06+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
c891f71
message
Add a function for formatting Python code
changed files
2 files, 28 additions, 1 deletion

Changed files

fish_functions/README.md (3438) → fish_functions/README.md (3640)

diff --git a/fish_functions/README.md b/fish_functions/README.md
index 62d44ce..f7c16db 100644
--- a/fish_functions/README.md
+++ b/fish_functions/README.md
@@ -96,6 +96,15 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=functions)
   </dd>
 
   <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/fish_functions/pyfmt.fish">
+      <code>pyfmt.fish</code>
+    </a>
+  </dt>
+  <dd>
+    Run Python formatting over a directory
+  </dd>
+
+  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/fish_functions/reload_fish_config.fish">
       <code>reload_fish_config.fish</code>
     </a>
@@ -122,4 +131,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=functions)
     Create and activate a new virtual environment
   </dd>
 </dl>
-<!-- [[[end]]] (checksum: 2416bf573658cad59a5851b6623b6b8c) -->
\ No newline at end of file
+<!-- [[[end]]] (checksum: 4227fcb9e8480fc60a86505c13b16c42) -->
\ No newline at end of file

fish_functions/pyfmt.fish (0) → fish_functions/pyfmt.fish (559)

diff --git a/fish_functions/pyfmt.fish b/fish_functions/pyfmt.fish
new file mode 100644
index 0000000..f49f86a
--- /dev/null
+++ b/fish_functions/pyfmt.fish
@@ -0,0 +1,18 @@
+function pyfmt --description "Run Python formatting over a directory"
+	if test (count $argv) -eq 0
+		set root $PWD
+	else
+		set root $argv[1]
+	end
+	
+	black "$root"
+	
+	# E501 = line too long; anything up to 100-ish is fine in my book
+	# (the "ish" is intentional; see https://www.youtube.com/watch?v=wf-BqAjZb8M)
+	#
+	# E203/W503/W504 = this is where black and flake8 conflict, see https://black.readthedocs.io/en/stable/faq.html#why-are-flake8-s-e203-and-w503-violated
+	flake8 \
+		--ignore=E501,E203,W503 --extend-select=W504 \
+		--exclude .venv \
+		"$root"
+end
\ No newline at end of file