Skip to content

Instantly share code, notes, and snippets.

@Anthchirp
Created April 24, 2020 09:06
Show Gist options
  • Save Anthchirp/e926d72ef4f9cfaf0ae578d0973bc197 to your computer and use it in GitHub Desktop.
Save Anthchirp/e926d72ef4f9cfaf0ae578d0973bc197 to your computer and use it in GitHub Desktop.
#!/bin/bash
# find python classes that are declared once and then list files where the name is referenced
for candidate in \
$(grep --exclude-dir=".git" --exclude-dir="__pycache__" -hoPR '^ *class \K[a-zA-Z0-9_]*' . | \
sort | uniq -c | grep " 1 " | cut -c 9-)
do
REFERENCES=$(grep --exclude-dir=".git" --exclude-dir="__pycache__" -c -cR $candidate . | grep -v ":0")
if [ "$(wc -l <<< "$REFERENCES")" -eq "1" ]; then
if [[ $REFERENCES == *":1" ]]; then
echo class $candidate is an unused declaration in ${REFERENCES%:*}
elif [[ $candidate != "_"* ]]; then
echo class $candidate is a public name only referred to in ${REFERENCES%:*}
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment