# Write a callable that accepts a cls as an argument
def tools(cls):
# Write functions that accept "self: object" as an argument.
def simplistic_attribute_count(self: object) -> int:
"""Returns the number of attributes."""
return len(self.__dict__)
def docs(self: object) -> str:
"""Returns the docstring for the class."""
return self.__doc__
# Attach the functions as methods
cls.simplistic_attribute_count = simplistic_attribute_count
cls.docs = docs
# Return the modified class
return cls
Let's test it out:
@tools
class A:
"""Docstring for testing the tools decorator"""
a = A()
a.one = 1
assert a.simplistic_attribute_count() == 1
assert a.docs() == 'Docstring for testing the tools decorator'
Next up, how to do this while passing in arguments!
]]>How well did I match my resolutions for 2023?
What I plan to do in the coming year
WARNING: Do this on branches of main, not on the main branch itself. More on that at the end.
In our example the change we want to amend is 3 commits back, do this to find it:
git rebase -i HEAD~3
That will bring up a screen that looks like this in your text editor:
pick ec0fb5e0333c Configure omnitron galactic retroverter
pick 6e7f323681f1 Activate the anvil generator
pick 0001757cea8f Wind up clockwork battery
pick 849f7c453458 Solidify the electricity
# Rebase dd341c1572..64c2cbbd76 onto dd34dc1572 (4 commands)
On the third line, change the word, pick
to edit
. It should now look like this:
pick ec0fb5e0333c Configure omnitron galactic retroverter
pick 6e7f323681f1 Activate the anvil generator
edit 0001757cea8f Wind up clockwork battery # <-- changed line
pick 849f7c453458 Solidify the electricity
# Rebase dd341c1572..64c2cbbd76 onto dd34dc1572 (4 commands)
Save and close the file.
Now make changes to code files and save them.
Run these commands to have git recognize the changes:
git commit --all --amend --no-edit
git rebase --continue
If you have a remote repo (aka github or gitlab) you will need to run git push --force
.
Before you do this understand this rewrites the SHA-1s from the point of change forward. Any old SHA-1 representatives of those commits are gone. Hence this should happen on a branch away from master, preferably in a pull request.