[myhdl-list] Re: range object
Brought to you by:
jandecaluwe
From: Beni C. <cb...@us...> - 2005-03-14 22:08:02
|
Haitao Zhang wrote: > On Thu, 03 Mar 2005 11:49:08 +0100, Jan Decaluwe <ja...@ja...> wrote: > >>Haitao Zhang wrote: >> >>> [...] However in Python the native slicing is limited to integer >>> arguments (?), so one has to define two named constants instead of >>> passing an indexing object. >> Before Python 2.3, builtin sequences didn't support slice objects (which also means they didn't support slicing with a step - ``a[start:stop:step]``). In 2.3 and later it works. But behavior of builtin sequence types doesn't say anything about the behavior of `intbv`. >>This is not correct. Consider: >> >>>>>from myhdl import intbv >>>>>a = intbv(0xffff) >>>>>CTRL_FIELD = slice(4, 2) >>>>>type(CTRL_FIELD) >> >><type 'slice'> >> >>>>>a[CTRL_FIELD] >> >>intbv(3L) >> >>>>>a[CTRL_FIELD] = 0 >>>>>hex(a) >> >>'0xFFF3L' >> This works in MyHDL 0.4.1 (0.4 used ``__getslice__`` methods). Incidentally, 0.4.1 requires Python 2.3 for several reasons. >>The slice object is not often used explicitly in Python code but >>probably deserves more attention. (Warning: toVerilog doesn't >>support his - as always, patches are welcome!). >> Also note that before Python 2.3, `slice` was a constuctor function distinct from the <type 'slice'> object (accessible as `types.SliceType`) which. In Python 2.3 they were merged so now `slice` is the type and is callable (like `int` and other builtin types). > > Thanks for the tip! This is exactly what I want for now. (Matlab > behaviour is probably an over kill unless one has no-contigous field, > which for hardware designer only happens under special circumstances). > I will see what I can do about toVerilog when I get that far. > > Haitao > >>>In Matlab one can index any vector with another vector. I don't see >>>why this shouldn't be doable in Python as well. Maybe just no need for >>>it, until the hardware designers come along? Is the simplest way to >>>overload __getslice__ to accept a tuple (defining range)? >> You can have the matlab behavior with Numeric arrays. `intbv` could easily be extended to support it if there is a real use for it. BTW, `intbv` presently silently ignores the step of the slice. To prevent bugs going unnoticed, it should raise an exception if ``key.step is not None`` (or properly support it). |