[libre-riscv-dev] simple-barrel-processor is up
Luke Kenneth Casson Leighton
lkcl at lkcl.net
Sun Mar 10 12:26:47 GMT 2019
On Sun, Mar 10, 2019 at 11:44 AM Jacob Lifshay <programmerjake at gmail.com> wrote:
> I'm working on implementing a simple barrel processor as a way of learning
> nmigen and python.
good idea.
> It's available at:
> https://salsa.debian.org/Kazan-team/simple-barrel-processor
https://salsa.debian.org/Kazan-team/simple-barrel-processor/blob/master/instruction/format.py
that will prove extremely useful no matter what the [RISCV] processor.
note that at line 30, there's an "official" way to do that: the
direct assignment can result in... issues. i'm not familiar with why.
line 9 and 10 aren't needed, and if they're not needed, then neither
are 26 and 27.
this whole module looks like it's going to be all static methods. if
that's the case, the "self" in the __init__ can go, and the __init__
assignment can be done similar to this:
def tell(msg):
print(msg)
if __name__ == '__main__':
jimmy = Patient('Jimmy')
Patient.tell = staticmethod(tell)
jimmy.tell('Goodbye')
which you'll see here:
https://stackoverflow.com/questions/14645353/dynamically-added-static-methods-to-a-python-class
so, that would be... at line 30:
fmt.__init__ = staticmethod(__init__)
where "self" is removed from both line 9 and line 25 (and the use of
Super has to go). and if you absolutely do need Format.__init__, call
it like this:
Format.__init__(instruction, config), making sure that between 8 and 9
you put in an @staticmethod decorator.
that's if it's needed at all. yes you can create a base class which
has no __init__ at all. it will be given one anyway, as all python3
classes derive automatically from a class called "object".
also, you don't need to name the arguments as keyword arguments at
lines 27, 29, 77, 78, 91, 92, 93 and so on: all those can go.
naming keyword arguments is discouraged by convention when they are
already non-keywords, as naming is only needed when there are defaults
added to a class and you wish to override the defaults. in doing so
you can skip or even reorder any of the defaulted-keyword-arguments:
class Cls:
def __init__(self, arg1, arg2, arg3=5, arg4="hello")
x = Cls(1,2, arg4="goodbye")
y = Cls(1,2)
z = Cls(x,y, arg4="before_arg3_is_ok", arg3="order_of_kw_doesn't_matter")
l.
More information about the libre-riscv-dev
mailing list