473,947 Members | 34,050 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pending: A Mixin class for testing

Here is a Mix-in class I just built for testing.
It is quite simple, but illustrates how Mixins
can be used.

class Pending(object) :
_pending = iter(())
def __new__(class_, *args, **kwargs):
try:
return class_._pending .next()
except StopIteration:
return super(Pending, class_).__new__ (class_,
*args, **kwargs)

Now for the use:

class Float(Pending, float): pass

Float._pending = iter(range(4,7) )

print [Float(x*(x+1)//2) for x in range(6)]

Possibly by using itertools functions such as chain as in:
Klass._pending = itertools.chain (generate_some( ), Klass._pending)
you can inject fixed values to simplify testing.

Or something like this:

class SomeTrickyClass (...): # new-style only (derived from object)
...

class Hacked(Pending, SomeTrickyClass ):
_pending = sample_value_ge nerator()

TempHold, SomeTrickyClass = SomeTrickyClass , Hacked
try:
<do the test>
finally:
SomeTrickyClass = TempHold
--Scott David Daniels
sc***********@a cm.org
Nov 26 '06 #1
1 1515
Scott David Daniels wrote:
Here is a Mix-in class I just built for testing.
It is quite simple, but illustrates how Mixins
can be used.

class Pending(object) :
_pending = iter(())
def __new__(class_, *args, **kwargs):
try:
return class_._pending .next()
except StopIteration:
return super(Pending, class_).__new__ (class_,
*args, **kwargs)

Now for the use:

class Float(Pending, float): pass

Float._pending = iter(range(4,7) )

print [Float(x*(x+1)//2) for x in range(6)]

Possibly by using itertools functions such as chain as in:
Klass._pending = itertools.chain (generate_some( ), Klass._pending)
you can inject fixed values to simplify testing.
If you're willing to allow for issubclass() to fail you can do without a
mixin:

from itertools import chain, repeat

def preload(class_, iterable):
items = chain(
(lambda *args, **kw: item for item in iterable), repeat(class_))
return lambda *args, **kw: items.next()(*a rgs, **kw)

float = preload(float, "ABC")
print [float(x) for x in range(6)]
issubclass(floa t, float) # TypeError

Argh :-)

Peter
Nov 27 '06 #2

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

Similar topics

0
1635
by: zimba | last post by:
Hello ! If somebody is interested, here is a small hack I've done today. There are still some curious effects, but I'm pretty satisfied by the results, since PHP is not very flexible. Let me know what you think, I'm looking into talking about somethin ;)
5
2749
by: Udo Gleich | last post by:
Hi, I try to implement mixin classes. Thats why I need to make a new class at runtime. --tmp.py------------------------------------- import new class K1(object):
1
2589
by: Mac | last post by:
I have a MixIn class which defines a method foo(), and is then mixed in with another class by being prepended to that class's __bases__ member, thus overriding that class's definition of foo(). In my application though it is necessary for the MixIn's foo() to call the overridden foo(). How can I do this? My current hack is to do this: def foo(): # MixIn's method orig_bases = self.__class__.__bases__
0
1342
by: Paolino | last post by:
I had always been negative on the boldeness of python on insisting that unbound methods should have been applied only to its im_class instances. Anyway this time I mixed in rightly, so I post this for comments. ###### looking for a discovery .Start ################# class _Mixin(object): def __init__(self,main,instance,*args,**kwargs): # do mixin businnes main.__reinit__(self,instance) # the caveated interface
0
345
by: barnesc | last post by:
>So mixins are just a sub-class of sub-classing? > >I've just found this: > > >A mixin class is a parent class that is inherited from - but not as >a means of specialization. Typically, the mixin will export services to a >child class, but no semantics will be implied about the child "being a >kind of" the parent. >
6
2495
by: Alex Hunsley | last post by:
I know that I can catch access to unknown attributes with code something like the following: class example: def __getattr__(self, name): if name == 'age': return __age else: raise AttributeError
3
1498
by: Ed Leafe | last post by:
In Dabo, we create cursor classes that combine the backend-specific dbapi cursor class with our own mixin class that adds framework- specific behaviors. This has been working well for a couple of years now with many different backends, but today I'm getting errors with our Firebird class. I've checked the kinterbasdb site, and found nothing there that was helpful. The error reads: TypeError: Error when calling the metaclass bases type...
2
2220
by: ish | last post by:
I think this is more of a style question than anything else... I'm doing a C++ wrapper around a C event library I have and one of the items is a timer class, I'm also using this task to learn C++. Is it cleaner to have users subclass my Timer class and implement the on_timeout() method? Or should the user use a mixin and provide the mixin to my Timer class? The subclass method kinda looks like this..
1
1439
by: Ole Nielsby | last post by:
Given these 3 classes class A {virtual void a(){}}; class B {virtual void b(){}}; class C: public A, public B {}; I want the offset of B in C, as a size_t value, and preferably as a constant expression. I got a solution that seems to work on VC9Express:
0
11573
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
11156
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
11346
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,...
0
10690
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7429
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
6331
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4945
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3542
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.