Re: [Tutor] calling setters of superclasses
On 12/18/2010 2:06 AM, Peter Otten wrote: I don't think /how/ you are trying it is stupid though I'm not so sure about /what/ . Thank you all for very helpful suggestions. It took me a while to chew on this before I could respond. I learned a lot about descriptors and their interactions with properties that I hadn't fully understood before. Peter and Alan's advice to create a check method that is overridden in subclasses makes sense in order to avoid the naming conflicts. And I also like Hugo's idea of applying separate descriptor classes to handle the constraints introduced. That seems to be a flexible way of doing things. As far as the /what/, my example given was obviously contrived. I'm really trying to create classes for 2D envelopes that describe the bounding extent of spatial data. I have both Envelope and RasterEnvelope classes - the former being just a bounding box around any spatial data, the latter additionally specifying a raster cell size and being able to discern rows, columns, etc. I had been using the setter to do bounds checking on the Envelope class (e.g. make sure x_min isn't bigger than x_max, etc. and rolling back changes if so). For the RasterEnvelope class, I first wanted to call the Envelope bounds checking and then to adjust rows/columns if a bigger extent was requested. But you've successfully scared me away from using properties (in a hierarchical way at least) and I've been able to get what I need by just defining __setattr__ in both classes. Whether I did that correctly is a story for another thread ... matt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] classes for setting 2D envelopes
Hi all, I've been working on creating 2D bounding box (envelope) classes to describe spatial data. Variations of these are available in other spatial libraries (e.g. Shapely), although I haven't found envelopes specific to raster data that also specifies cell size. Could be I just haven't found them yet. I have two classes (Envelope and RasterEnvelope). Envelope specifies a bounding box and does error checking (through __setattr__) when a coordinate is changed. RasterEnvelope additionally specifies a cell size (along with n_rows and n_cols), does the bounds checking on a coordinate change and adjusts the spatial envelope accordingly (again through __setattr__). I've posted the code (and some unit tests) here: http://pastebin.com/Twf3RjWa So far things work, but I have a nagging feeling that there's too much work devoted to changing coordinates. In another thread I posted a while back (http://mail.python.org/pipermail/tutor/2010-August/077940.html), Steven D'Aprano recommended using immutable types for point coordinate data and I'm guessing the advice might be applicable here as well? If so, I'm a slow learner ;) There are also (at least) a couple of design flaws right now that could be remedied if I put more work into it: - A RasterEnvelope is 'pinned' by its upper-left coordinate and only changes to x_min or y_max will cause changes in this coordinate (by design but probably a limitation) - Changing cell size currently doesn't change the corresponding window because there is no specification as to whether n_cols/n_rows should change or x_max/y_min should change. Both these probably suggest creating new RasterEnvelope instances any time a coordinate changes? Any feedback would be welcome, so that I don't devote too much more time down a bad route. thanks, matt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with arrays
On 5/4/2011 10:30 AM, Patrick P. wrote: Hello, I hope this is the right way to ask my question, if not, sorry to bother you. Maybe you can tell me who to ask. Ok so here is my code [snip] A = np.array([[m111,m121], [m211,m221]]) B = np.array([[m112,m122], [m212,m222]]) print(np.dot(A,B)) Traceback (most recent call last): File "C:/Python26/helpfiletest.py", line 36, in print(np.dot(A,B)) ValueError: objects are not aligned - I get it why theres and error message, because it's trying to multiply a 2x2x100 array with another 2x2x100 array and doesn't know how to do that. What I actually want is 100 2x2 arrays and then mutliply them individually. Can anyone help me with that? As Alan said, you'll likely get good help on the numpy listserv, but I think this might be what you want, although I'm not sure it's the tersest way to express it: # Swap axes twice on A and B to make them (100, 2, 2) A_new = np.swapaxes(np.swapaxes(A, 0, 2), 1, 2) B_new = np.swapaxes(np.swapaxes(B, 0, 2), 1, 2) # Multiply the 2x2 arrays together using list comprehension C = np.array([np.dot(a, b) for a, b in zip(A_new, B_new)]) print C I'm certain there is a more clever way (ie. some function within numpy) to do that last part. matt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] overriding instance attributes with keywords
On 8/14/2012 5:28 AM, eryksun wrote: On Tue, Aug 14, 2012 at 6:01 AM, eryksun wrote: Right, I overlooked classes with __slots__ and that override __new__ and other special methods. copy() is the better and more customizable solution. If copying is good enough, then this should work: from copy import copy def copy_with_overrides(obj1, **kwds): obj2 = copy(obj1) for k, v in kwds.items(): if hasattr(obj2, k): setattr(obj2, k, v) return obj2 Thanks to you both for really helpful advice. A quick follow-up question - would you want to create a deepcopy of obj1 in the above example if your obj1 contained other objects? I think that Steven's class method gets around this? thanks, matt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] creating a subclass from superclass without __init__
Is it possible to create a subclass of a superclass that doesn't have an __init__ and is only created through another class. Here is an example of what isn't working: class Spam(object): def __new__(cls, *args): return super(Spam, cls).__new__(cls, args) def __init__(self): raise AttributeError('Cannot create Spam') def test(self): print 'This is a Spam class' class SpamMaker(object): def make_spam(self): return Spam.__new__(Spam) class SubSpam(Spam): def __new__(cls, *args): return SpamMaker().make_spam() def test(self): print 'This is a SubSpam class' b = SpamMaker().make_spam() b.test() c = SubSpam() c.test() prints This is a Spam class This is a Spam class I know that I'm not creating the SubSpam instance correctly, but I have no idea how to do it. It's probably something very obvious that I'm missing. My real use case is using the Python bindings to GDAL and trying to create a subclass of gdal.Band which can't be instantiated directly. thanks, matt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a subclass from superclass without __init__
On 8/24/2012 12:01 PM, Steven D'Aprano wrote: That's not a use-case. A use-case is a real-world problem that you are trying to solve. You have skipped the problem and jumped straight to what you think is the solution: "create a subclass which can't be instantiated directly". I can't imagine any problem where the solution to the problem depends on whether or not you instantiated a subclass using: Spam(*args) or SpamMaker.make(*args) Clearly my explanation was pretty bad. I'll start with my real-world problem instead, although it may be outside the scope of this list (thus my lame attempt to abstract it). There are two classes of interest in GDAL - a Dataset which typically describes a geospatial raster file and Band which described a single band from the Dataset. gdal.Band just raises an AttributeError within __init__, but a gdal.Band instance can be created using gdal.Dataset.GetRasterBand(bandNum). I had wanted to create additional methods on gdal.Band that would allow some GIS type operations (e.g. overloading __add__), thus I thought I would need a subclass of gdal.Band. So I was unsure of how to get an instance of my subclass when the only way I knew how to create an instance of the superclass (gdal.Band) was through the call to GetRasterBand(). Does that make any sense in relation to my contrived example? In my example, I didn't really want to create a Spam instance from the SubSpam __new__, I just didn't know how to return a SubSpam instance that was forced to go through SpamMaker.make_spam(). matt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor