473,467 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Create 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_generator()

TempHold, SomeTrickyClass = SomeTrickyClass, Hacked
try:
<do the test>
finally:
SomeTrickyClass = TempHold
--Scott David Daniels
sc***********@acm.org
Nov 26 '06 #1
1 1480
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()(*args, **kw)

float = preload(float, "ABC")
print [float(x) for x in range(6)]
issubclass(float, 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
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...
5
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
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...
0
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...
0
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...
6
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...
3
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...
2
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++....
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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 ...

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.