December 31st, 2007 by exhuma.twn
Somethin that I need quite often is to create custom accessors and mutators for class-attributes. For example convert this:
class MyClass(object):
def __init__(self):
self.has_changes = False
self.some_attribute = False
into this:
class MyClass(object):
def __init__(self):
self.__has_changes = False
self.__some_attribute = False
def get_some_attribute(self):
"Accessor: some_attribute"
return self.__some_attribute
def set_some_attribute(self, input):
"Mutator: some_attribute"
self.__some_attribute = input
self.__has_changes = True
some_attribute = property(get_some_attribute, set_some_attribute)
def get_has_changes(self):
"Accessor: has_changes"
return self.__has_changes
has_changes = property(get_has_changes)
This particular example allows an easy tracking if a class contains changes. Without the need of calling myclass.get_some_attribute() or myclass.set_some_attribute(foo). You can simply do myclass.some_attribute = foo and the has_changes attribut will change accordingly.
If your class has many attributes, writing custom accessors and mutators can be tedious. So here’s a small Vim-mapping that get’s you started. Sure, you may still need to fine-tune some generated code, but the bulk is there.
<font color="#808bed">nmap</font> <font color="#c080d0"><</font><font color="#c080d0">F6</font><font color="#c080d0">></font> yyP<font color="#c080d0"><</font><font color="#c080d0">home</font><font color="#c080d0">></font>widef get_<font color="#c080d0"><</font><font color="#c080d0">end</font><font color="#c080d0">></font>(self):<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>yyP>>I"Accessor: <font color="#c080d0"><</font><font color="#c080d0">end</font><font color="#c080d0">></font>"<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">></font>yyP>>Ireturn self.__<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>o<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">></font>yyPIdef set_<font color="#c080d0"><</font><font color="#c080d0">end</font><font color="#c080d0">></font>(self, input):<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">></font>yyP>>I"Mutator: <font color="#c080d0"><</font><font color="#c080d0">end</font><font color="#c080d0">></font>"<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">></font>yyP>>Iself.__<font color="#c080d0"><</font><font color="#c080d0">end</font><font color="#c080d0">></font> = input<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>o<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">><</font><font color="#c080d0">home</font><font color="#c080d0">></font>wveyA = property(get_<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>pA, set_<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>pA)<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>o<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>
Put this into your vimrc, or (like I do) into the ~/.vim/ftplugin/python.vim file so it get’s only loaded for python files. Then you only need to write the attribute name of the class, put your cursor on that line, be sure to be in normal mode (hit a few time <esc>) 😉 and hit F6
If you want to change the shortcut, simply change the first parameter to this mapping line.
Posted in Python | No Comments »