klayout icon indicating copy to clipboard operation
klayout copied to clipboard

Minor improvements to klayout python package

Open thomaslima opened this issue 3 years ago • 2 comments

Hello. I have been in the process of refactoring some of the code in https://github.com/lightwave-lab/zeropdk, and in the process I have made a few improvements to the database API. I also have fixed a few issues with the stub files (e.g. distinguishing Sequence from Iterators). Type hinting is now top notch and helped me detect bugs I hadn't foreseen.

  • Implemented deepcopy for Point/Vector objects.
  • Changed exception for wrong argument type in method call from RuntimeError to TypeError.
  • Implemented p - v = p operation, where p:Point and v: Vector, matching p + v = p
  • Distinguished between Iterator and Sequence in Python stubs.
    • Methods like object.each() typically returns an iterator, which can't be indexed in python.
    • Methods like object.merge() returns an actual sequence/list-like object that can be indexed like list[0].
  • For operators such as +, -, *, | etc., return NotImplemented when the operand is unknown.
    • For example p * x, where p: Point and x: Unknown, would yield TypeError if x is not a float.
    • Now, it returns NotImplemented, so that python calls x.rmul(p).
    • This is useful when x is a numpy array. The example above returns an ndarray of points.
  • Disabled rmul for Trans objects. (Not commutative)
  • Enabled GSI_ALIAS_INSPECT to activate repr.
  • Implemented setstate and getstate for objects that had from_s, to_s and assign defined. Useful for pickling and serialization, as well as deep copying.
  • Implemented copy for objects with dup() methods.
  • Annotating return type vector as List instead of Sequence.

thomaslima avatar Sep 15 '22 21:09 thomaslima

Hi Thomas,

thanks for the patch. It actually isn't "minor" :)

A few comments:

  • You're right about the non-commutative nature of transformation multiplication. But I don't like to see a name match - I'll never remember to add more classes here if required. I'd rather go for an annotation (e.g. of "*!" as "non-commutative *"), but as of now we can leave it like this. I'll change it when porting the patch to master.
  • I see that "Point-like" objects get a special handling through "convert_type_error_to_not_implemented". Why not generalize - i.e. mapping all non-implemented operators to "NotImplemented" for all classes?
  • In general "dup" is always a deep copy, so does it make sense to map "dup" to "deepcopy" always?

Best regards,

Matthias

klayoutmatthias avatar Sep 17 '22 21:09 klayoutmatthias

Hi Matthias,

Thanks for reviewing my PR! Sorry for the late reply.

  • You're right about the non-commutative nature of transformation multiplication. But I don't like to see a name match - I'll never remember to add more classes here if required. I'd rather go for an annotation (e.g. of "*!" as "non-commutative *"), but as of now we can leave it like this. I'll change it when porting the patch to master.

I think that having rmul is a "nice to have" feature. In python, it's not usually expected from libraries. I would prefer if you picked an annotation for commutative operations instead. You can take your time with this, I think it pays to get it right for both master and 0.27.

  • I see that "Point-like" objects get a special handling through "convert_type_error_to_not_implemented". Why not generalize - i.e. mapping all non-implemented operators to "NotImplemented" for all classes?

You are right. I was lazy and only used it in the classes I use most often. Box, for example, is missing. I'll patch it.

  • In general "dup" is always a deep copy, so does it make sense to map "dup" to "deepcopy" always?

I didn't know dup always did a deep copy, especially for the Instance and the ParentInstArray classes. Could you check this link for the requirements for implementing the __deepcopy__ memo argument? Namely, is there any chance that objects contain references to itself, creating a recursive copying issue? For this reason, deepcopy has a second argument called memo to store objects already copied.

Thanks again!

thomaslima avatar Sep 26 '22 20:09 thomaslima

Sorry for taking so long for the merge and thanks for the effort you spent!

Matthias

klayoutmatthias avatar Oct 19 '22 19:10 klayoutmatthias