473,903 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

creating many similar properties


I understand how to create a property like this:

class RC(object):
def _set_pwm(self, v):
self._pwm01 = v % 256
def _get_pwm(self):
return self._pwm01
pwm01 = property(_get_p wm, _set_pwm)
But what if I have a whole bunch of these pwm properties?

I made this:

class RC(object):
def _makeprop(name) :
prop = '_%s' % name
def _set(self, v):
v_new = v % 256
setattr(self, prop, v_new)
def _get(self):
return getattr(self, prop)
return property(_get, _set)

pwm01 = _makeprop('pwm0 1')
pwm02 = _makeprop('pwm0 2')
Not too bad, except for having to repeat the name.

I would like to just have a list of pwm names and
have them all set up like that. It would be nice if
each one was set to a default value of 127 also....

Any thoughts?

Thanks for your time.
Oct 18 '06
17 1965
Carl Banks wrote:
Devil's Advocate: he did say "hidden magic TO YOUR CLASS".

If you use a (real) metaclass, then you have the icky feeling of a
class permanently tainted by the unclean metaclass (even though the
metaclass does nothing other than touch the class dict upon creation);
whereas if you use Michele Simionato's hack, the icky feeling of using
a stack frame object goes away after the property is created: you are
left with a clean untainted class.
Yep, exactly.
Personally, the former doesn't make me feel icky at all.
Please, do this experiment: take all classes defined in the Python
standard library and add
to them a custom do-nothing metaclass. See what happens.
Michele Simionato

Oct 18 '06 #11
George Sakkis wrote:
There's a subtle common bug here: all _get and _set closures will refer
to the last property only. You have to remember to write "def
_set(self,v,pro p=prop)" and similarly for _get to do the right thing.
Sorry. My mistake.

By the way, I can't think of a case where the current behavior (i.e.
binding the last value only) is the desired one. Is this just an
implementation wart or am I missing something ?
def some_function(a ):
def printvars():
print "DEBUG: %r,%r,%r" % (a,b,i)
for i in some_range():
b = something(i)
printvars()

If you fix the value of the closure at function definition time,
printvars() above doesn't work. One way or another, someone's going to
get surprised. Better to let it be the experts.
Carl

Oct 18 '06 #12

Michele Simionato wrote:
Carl Banks wrote:
Devil's Advocate: he did say "hidden magic TO YOUR CLASS".

If you use a (real) metaclass, then you have the icky feeling of a
class permanently tainted by the unclean metaclass (even though the
metaclass does nothing other than touch the class dict upon creation);
whereas if you use Michele Simionato's hack, the icky feeling of using
a stack frame object goes away after the property is created: you are
left with a clean untainted class.

Yep, exactly.
Personally, the former doesn't make me feel icky at all.

Please, do this experiment: take all classes defined in the Python
standard library and add
to them a custom do-nothing metaclass. See what happens.
Do you expect the result to be better or worse than if you applied
stack frame hacks to the whole library?

Come on, I don't think anyone's under the impression we're being
indiscriminate here.
Carl Banks

(BTW, most of the standard library still uses old-style classes.)

Oct 18 '06 #13
Carl Banks wrote:
Come on, I don't think anyone's under the impression we're being
indiscriminate here.
Ok, but I don't think that in the case at hand we should recommend a
metaclass
solution.

Michele Simionato

Oct 18 '06 #14
Michele Simionato wrote:
Carl Banks wrote:
Come on, I don't think anyone's under the impression we're being
indiscriminate here.

Ok, but I don't think that in the case at hand we should recommend a
metaclass
solution.
You sound as if you're avoiding metaclasses just for the sake of
avoiding them, which is just as bad as using them for the sake of using
them.

Here's how I see it: either it's ok to fiddle with the class dict, or
it isn't. If it's ok, then a metaclass is the way to do it. If it's
not ok to fiddle with the class dict, then he should be using
__setattr__, or creating properties longhand. Messing with frames is
not the answer for production code.

....

Just for the hell of it, I decided to accept your challenge to run the
standard library with a different metaclass applied to all new-style
classes. I ran the 2.4.3 regression test, replacing the builtin object
with a class that used the mod256metaclass I presented in this thread.
The results:

229 tests OK.
34 tests failed:
test___all__ test_asynchat test_cgi test_cookielib test_copy
test_copy_reg test_cpickle test_decimal test_descr test_descrtut
test_email test_email_code cs test_httplib test_imaplib
test_inspect test_logging test_mailbox test_mimetools
test_mimetypes test_minidom test_pickle test_pyclbr
test_robotparse r test_sax test_sets test_socket test_socket_ssl
test_sundry test_timeout test_urllib test_urllib2 test_urllib2net
test_urllibnet test_xpickle

Not A-OK, but not exactly mass-pandemonium either. There were plenty
of modules used the the new base object and worked fine.

There were only two causes for failure:
1. A class attempting to use __weakref__ slot.
2. There were also a few metaclass conflicts.

IMO, neither of these failure modes argues against using a metaclass to
preprocess the class dict. The __weakref__ error is not applicable;
since it's an error to use it on any class with an instance dict. (In
fact, the metaclass wasn't even causing the error: the same error would
have occurred if I had replaced builtin object with an empty subclass
of itself, without the metaclass.)

The metaclass conflict would only occur in the present case only if
someone wanted to subclass it AND specify a different metaclass.
Arguing that metaclasses should be avoided just to guard against this
rare possibility is defensive to the extreme.

I did not uncover any kinds of subtle, unexpected behavior that can
occur when metaclasses do weird things. I know such things are
possible; how likely they are is another question. The tests I ran
didn't uncover any. So for now, the results of these tests don't seem
to support your point very well.

Carl
Appendix: Here's how I ran the test. I inserted the following code at
the top of Lib/test/regrtest.py, and
ran make test. I ran the tests on the Python 2.4.3 source tree, in
Linux.

=============== ========
import sys

class mod256metatype( type):
def __new__(metatyp e,name,bases,cl sdict):
print >sys.__stdout__ , \
"++++++++ Creating class %s of type mod256metatype" %
name
def makeprop(sym):
prop = '_%s' % sym
def _set(self,v):
setattr(self,pr op,v%256)
def _get(self):
return getattr(self,pr op)
return property(_get,_ set)
for sym in clsdict.get('__ mod256__',()):
clsdict[sym] = makeprop(sym)
return super(metatype
return type.__new__(me tatype,name,bas es,clsdict)

class _object:
__metaclass__ = mod256metatype

import __builtin__
__builtin__.obj ect = _object
=============== ========

Oct 18 '06 #15
Michele Simionato wrote:
George Sakkis wrote:
>>Why is this less hidden or magical than a metaclass ?


Because it does not use inheritance. It is not going to create
properties on subclasses without
you noticing it. Also, metaclasses are brittle: try to use them with
__slots__, or with non-standard
classes (i.e. extensions classes), or try to use multiple metaclasses.
I wrote a paper
about metaclasses abuses which should be published soon or later; you
can see the draft
here:
http://www.phyast.pitt.edu/~micheles...itializer.html
I am in the midst of reading this paper, and, while I don't share your
level of expertise, I modestly share your opinion that solving problems
with dynamically generated classes unnecessarily complicates code. For
example, the problem of the OP could be solved easily with a dictionary
and two functions used to access the dictionary and then, if making a
class was truely necessary, including these functions as members of the
class. But the OP mentioned something about "properties " and all hell
broke loose.

However, I think that what you are saying about metaclasses being
brittle relates more to implementation than language. In theory, these
should be equivalent:

(1) class Bob(object): pass
(2) Bob = type('Bob', (), {})

And indeed a cursory inspection of the resulting classes show that they
are indistinguishab le.

That they wouldn't be seems an implementation bug and perhaps that bug
should be fixed rather than promoting the avoidance of (2) because it
does not create classes that behave as number (1).

James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Oct 18 '06 #16
Carl Banks wrote:
You sound as if you're avoiding metaclasses just for the sake of
avoiding them, which is just as bad as using them for the sake of using
them.
Do you realize that you are effectively saying "avoiding a complex
tool in favor of a simpler one is just as bad as avoing the simple tool
in favor of the complex one" ?
Here's how I see it: either it's ok to fiddle with the class dict, or
it isn't. If it's ok, then a metaclass is the way to do it. If it's
not ok to fiddle with the class dict, then he should be using
__setattr__, or creating properties longhand. Messing with frames is
not the answer for production code.
I agree that messing with frames is not nice (however I should notice
that
this is how Zope interfaces are implemented, and they have been in
production use for years) but I disagree with your point about the
class dict. You should use a custom metaclass *only if you want to
mess with the class dict of all subclasses at each derivation*: this is

rarely the case, and definitely was not requested for the OP problem.
>
Just for the hell of it, I decided to accept your challenge to run the
standard library with a different metaclass applied to all new-style
classes. I ran the 2.4.3 regression test, replacing the builtin object
with a class that used the mod256metaclass I presented in this thread.
The results:

229 tests OK.
34 tests failed:
test___all__ test_asynchat test_cgi test_cookielib test_copy
test_copy_reg test_cpickle test_decimal test_descr test_descrtut
test_email test_email_code cs test_httplib test_imaplib
test_inspect test_logging test_mailbox test_mimetools
test_mimetypes test_minidom test_pickle test_pyclbr
test_robotparse r test_sax test_sets test_socket test_socket_ssl
test_sundry test_timeout test_urllib test_urllib2 test_urllib2net
test_urllibnet test_xpickle
34 tests failed, worse than I expected.
Not A-OK, but not exactly mass-pandemonium either. There were plenty
of modules used the the new base object and worked fine.

There were only two causes for failure:
1. A class attempting to use __weakref__ slot.
2. There were also a few metaclass conflicts.
Yes, this agree with my findings. I was curious to know if there were
additional issues.
IMO, neither of these failure modes argues against using a metaclass to
preprocess the class dict.
But they argue against using metaclasses in general, IMO! (or at least,
against using them for users that are not aware of all the potential
pittfalls).
The __weakref__ error is not applicable;
since it's an error to use it on any class with an instance dict. (In
fact, the metaclass wasn't even causing the error: the same error would
have occurred if I had replaced builtin object with an empty subclass
of itself, without the metaclass.)
Correct, this more of a problems of __slots__ that play havoc with
inheritance
than a problem of metaclasses.
The metaclass conflict would only occur in the present case only if
someone wanted to subclass it AND specify a different metaclass.
Arguing that metaclasses should be avoided just to guard against this
rare possibility is defensive to the extreme.
Not too extreme in my opinion. Real life example: I had a debugging
tool
using a custom metaclass, I tried to run it on Zope 2.7 classes and I
have got segmentation faults. In Zope 2.8 I get "only" metatype
conflicts, and to avoid that I had to rewrite the tool :-(
I did not uncover any kinds of subtle, unexpected behavior that can
occur when metaclasses do weird things. I know such things are
possible; how likely they are is another question. The tests I ran
didn't uncover any. So for now, the results of these tests don't seem
to support your point very well.
Well, this is a matter of opinion. In my opinion your tests support my
point pretty well, better than I expected ;)
>
Appendix: Here's how I ran the test. I inserted the following code at
the top of Lib/test/regrtest.py, and
ran make test. I ran the tests on the Python 2.4.3 source tree, in
Linux.

=============== ========
import sys

class mod256metatype( type):
def __new__(metatyp e,name,bases,cl sdict):
print >sys.__stdout__ , \
"++++++++ Creating class %s of type mod256metatype" %
name
def makeprop(sym):
prop = '_%s' % sym
def _set(self,v):
setattr(self,pr op,v%256)
def _get(self):
return getattr(self,pr op)
return property(_get,_ set)
for sym in clsdict.get('__ mod256__',()):
clsdict[sym] = makeprop(sym)
return super(metatype
return type.__new__(me tatype,name,bas es,clsdict)

class _object:
__metaclass__ = mod256metatype

import __builtin__
__builtin__.obj ect = _object
=============== ========
I used a similar approach. I added in sitecustomize.p y the following
lines:
import __builtin__

class chatty_creation (type):
"Print a message every time a class is created"
def __new__(mcl, name, bases, dic):
try:
cls = super(chatty_cr eation, mcl).__new__(mc l, name, bases,
dic)
except Exception, e:
print e
print 'Could not enhance class %s' % name
cls = type(name, tuple(b for b in bases if b is not
Object), dic)
# removing Object from the bases is enough only in the
trivial
# cases :-(
else:
print 'Creating class %s.%s' % (dic.get('__mod ule__'),
name)
return cls

class Object:
__metaclass__ = chatty_creation

__builtin__.obj ect = Object

Now it is impossible to run both Zope and Twisted due to metatype
conflicts.
I haven't looked in detail, but the first conflict in Twisted is due
to a metaclass-enhanced class which is setting properties. This is
the typical example of what I call metaclass *abuse* in my paper, since
the custom metaclass could have been avoided (for instance using George
Sakkis trick) and the conflict could have been avoided.

Now, I know how to solve the conflict
(http://aspn.activestate.com/ASPN/Coo.../Recipe/204197) but I
would rather avoid it altogether.

The problem with metaclasses is that you are adding magic to the
classes of
your USERS, and the users are known to play any kind of dirty tricks.
You
(speaking in general of you as the author of a framework) should strive
to keep things clean as much as possible.

I agree that the problems are rare: but just for this reason they are
prone to very subtle bugs, the hardest to find. And there is no
documentation of metaclass pittfall AFAIK :-(
Michele Simionato

Oct 19 '06 #17
James Stroud wrote:
However, I think that what you are saying about metaclasses being
brittle relates more to implementation than language. In theory, these
should be equivalent:

(1) class Bob(object): pass
(2) Bob = type('Bob', (), {})

And indeed a cursory inspection of the resulting classes show that they
are indistinguishab le.

That they wouldn't be seems an implementation bug and perhaps that bug
should be fixed rather than promoting the avoidance of (2) because it
does not create classes that behave as number (1).
You got something wrong ;)

'type' is the builtin metaclass, it works, I have nothing against it,
and (1) and (2) are *exactly*
equivalent. My gripe is against *custom* metaclasses, i.e. subclasses
of 'type'. The paper is
all about avoiding custom metaclasses and using 'type' instead (i.e.
use the __metaclass__
hook, but not custom metaclasses). It is the same trick used by George
Sakkis in this same
thread.

Michele Simionato

Oct 19 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8442
by: Simon Edwards | last post by:
Something thats been bugging me for a while... how do you create a namespace that has many children (namespaces) I.e system.io.blah.blah Iv'e done it by creating a class which contains another class. i can see the properties of the first class and the namespace of the second (inner class) but can't see the properties of the 2nd....
4
561
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum number of window handle, because if I reduce to about 2 instances of each window, it can run. But not 20 instances of each window. Does anyone know what the problem is? is it really because it exceeds the maximum number of window handle?
0
1545
by: Sergej Pioch | last post by:
Hello everybody, im trying hard to automate some tasks in a huge windows environment. This lend me to bigger problems while trying to create new global security groups within active directory. First I tried to add the groups as discribed in article "Creating Groups" at Platform SDK: System.DirectoryServices documentation. I figuerd out that something in the article was missing, because groupcreation ended in an error: "A constraint...
1
1233
by: Neil Robbins | last post by:
I have been wondering recently how I would go about creating a control that when selected from the toolbox for use in an application either autoran a wizard for providing it with properties or provided for the selection of an option from the properties window that would run a wizard. I suppose what I envisage would be similar to the AutoFormat option in the Properties menu for the DataGrid Control. Also how could one create something...
2
3063
by: Terrance | last post by:
Can someone please help with a problem that I'm having? I'm trying to create a linklabel in VB.NET 2003 at runtime when a user clicks a button. The app looks in a path for .doc and .pdf files and creates a linklabel for each file name. When running the app the first name is being created but only portions of the string is showing; and also it looks like the remaining is being created but I can't see them it's like their cut off. Here is the...
26
5387
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
0
1982
by: ronscottlangham | last post by:
I am working on a custom WCF EndpoingBehavior that will modify the messages coming in and out of my Service. I am having an error related to the modification of the message in the IEndpointBehavior.AfterReceiveRequest(ref Message request,...) method. In the method that creates a new message, I have something similar to ... /// xmlReader defined with new modified content.... /// 'message' is the original message...
1
1402
by: MMAS | last post by:
I've seen a similar post about this problem but still cannot find an automated solution. Below is the code I'm using to create a website (for IIS 6.0) via C#. once the site is created, it will not serve aspx pages. If I: 1. go to IIS Manager 2. view the properties for the site in question 3. go to the tab 4. click next to the Application Name textbox in the Application Settings area.
2
2332
by: adwest | last post by:
Forgive me if this is a foolish question. I "play" in Access and have only created a few rather simple relational databases. My knowledge and experience is limited. I have no formal training, just picked up a few books and have gone from there... I'm not sure how to relate my question using the accepted shorthand, so I'm afraid this will be a narrative description. I am using Access 2003. The database is for property management. I have...
0
11283
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10875
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10986
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7206
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6093
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3324
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.