471,334 Members | 1,433 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

adding a static class to another class

HI,

I m trying to start an api in a similar way to the djangic way of
Class.objects.all(). Ie objects is a "Manager" class.

So:

class Foo(object):
def __init__(self):
self.test = "NEE"

class Manager(object):
def __init__(self):
pass
def all(self):
return "COCONUTS"

Because of how some of the code is set up I cant use
metaclasses........so I try to use a decorator:

def addto(instance):
def decorator(f):
import new
f = new.instancemethod(f, instance, instance.__class__)
setattr(instance, "objects", f)
return f
return decorator

class Manager(object):
@addto(Foo)
def __init__(self):
.............

however this only binds the init method to the Foo.objects, so not
what I want. If I try using classmethod...then it just says the
Foo.objects doesnt exist.

Does anyone have any ideas how I can accomplish this using decorators?
And also preventing more than one Manager instance instantiated at one
time.

Many Thanks in advance,

Nathan
Sep 16 '07 #1
5 1195
On Sep 17, 1:14 am, "Nathan Harmston" <ratchetg...@googlemail.com>
wrote:
HI,

I m trying to start an api in a similar way to the djangic way of
Class.objects.all(). Ie objects is a "Manager" class.

So:

class Foo(object):
def __init__(self):
self.test = "NEE"

class Manager(object):
def __init__(self):
pass
def all(self):
return "COCONUTS"

Because of how some of the code is set up I cant use
metaclasses........so I try to use a decorator:

def addto(instance):
def decorator(f):
import new
f = new.instancemethod(f, instance, instance.__class__)
setattr(instance, "objects", f)
return f
return decorator

class Manager(object):
@addto(Foo)
def __init__(self):
.............

however this only binds the init method to the Foo.objects, so not
what I want. If I try using classmethod...then it just says the
Foo.objects doesnt exist.

Does anyone have any ideas how I can accomplish this using decorators?
And also preventing more than one Manager instance instantiated at one
time.

Many Thanks in advance,

Nathan
You want to use descriptors (classes that implement __get__, __set__,
__del__). Google for the particulars.

-- bjorn

Sep 16 '07 #2
Nathan Harmston wrote:
HI,

I m trying to start an api in a similar way to the djangic way of
Class.objects.all(). Ie objects is a "Manager" class.

So:

class Foo(object):
def __init__(self):
self.test = "NEE"

class Manager(object):
def __init__(self):
pass
def all(self):
return "COCONUTS"

Because of how some of the code is set up I cant use
metaclasses........so I try to use a decorator:

def addto(instance):
def decorator(f):
import new
f = new.instancemethod(f, instance, instance.__class__)
setattr(instance, "objects", f)
return f
return decorator

class Manager(object):
@addto(Foo)
def __init__(self):
.............

however this only binds the init method to the Foo.objects, so not
what I want. If I try using classmethod...then it just says the
Foo.objects doesnt exist.

Does anyone have any ideas how I can accomplish this using decorators?
And also preventing more than one Manager instance instantiated at one
time.

Many Thanks in advance,

Nathan
What you are trying to do is a little obscure to me, but I think you
want want to rebind Foo.objects to a new manager every time you
instantiate a Manager? This will do it:

class Foo(object):
def __init__(self):
self.test = "NEE"

class Manager(object):
def __init__(self):
pass
def all(self):
return "COCONUTS"

def addto(cls):
def decorator(f):
def _f(self, *args, **kwargs):
cls.objects = self
f(self, *args, **kwargs)
return _f
return decorator

class Manager(object):
@addto(Foo)
def __init__(self):
pass

Example:

pyclass Foo(object):
.... def __init__(self):
.... self.test = "NEE"
....
pyclass Manager(object):
.... def __init__(self):
.... pass
.... def all(self):
.... return "COCONUTS"
....
pydef addto(cls):
.... def decorator(f):
.... def _f(self, *args, **kwargs):
.... cls.objects = self
.... f(self, *args, **kwargs)
.... return _f
.... return decorator
....
pyclass Manager(object):
.... @addto(Foo)
.... def __init__(self):
.... pass
....
pyhasattr(Foo, 'objects')
False
pym = Manager()
pyprint m
<__main__.Manager object at 0x121b870>
pyprint Foo.objects
<__main__.Manager object at 0x121b870>
pyn = Manager()
pyprint n
<__main__.Manager object at 0x121b250>
pyprint Foo.objects
<__main__.Manager object at 0x121b250>
If you just want the class Foo to have the class Manager as its objects,
just do this:

Foo.objects = Manager

But I don't think that's what you want.

I'm not familiar with django, so if I haven't hit it, please elaborate.

James

Sep 17 '07 #3
Nathan Harmston wrote:
And also preventing more than one Manager instance instantiated at one
time.
Forgot to answer this. You want the singleton pattern:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/52558

James
Sep 17 '07 #4
James Stroud wrote:
Nathan Harmston wrote:
>And also preventing more than one Manager instance instantiated at one
time.

Forgot to answer this. You want the singleton pattern:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/52558
But not really a singleton now that I think about it...

class Singletonish(object):
""" A python singletonish """

class __impl(object):
""" Implementation of the singletonish interface """

def spam(self):
""" Test method, return singletonish id """
return id(self)

# storage for the instance reference
__instance = None

def __init__(self):
""" Create singletonish instance """
Singletonish.__instance = Singletonish.__impl()

def __getattr__(self, attr):
""" Delegate access to implementation """
if attr == '__id__':
return id(Singletonish.__instance)
return getattr(Singletonish.__instance, attr)

def __setattr__(self, attr, value):
""" Delegate access to implementation """
return setattr(Singletonish.__instance, attr, value)
In action:

pyclass Singletonish(object):
.... """ A python singletonish """
.... class __impl(object):
.... """ Implementation of the singletonish interface """
.... def spam(self):
.... """ Test method, return singletonish id """
.... return id(self)
.... # storage for the instance reference
.... __instance = None
.... def __init__(self):
.... """ Create singletonish instance """
.... Singletonish.__instance = Singletonish.__impl()
.... def __getattr__(self, attr):
.... """ Delegate access to implementation """
.... return getattr(Singletonish.__instance, attr)
.... def __setattr__(self, attr, value):
.... """ Delegate access to implementation """
.... return setattr(Singletonish.__instance, attr, value)
....
pys = Singletonish()
pyprint s.spam()
18727248
py>
pyt = Singletonish()
pyprint t.spam()
18682480
pyprint s.spam()
18682480
Of course t and s are not /really/ the same:
pyprint id(t)
18727056
pyprint id(s)
18727280
pyassert t is s
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
<type 'exceptions.AssertionError'>
But this shouldn't matter unless you have a special use case because the
implementation to all Singletonish objects are delegated to to the
latest __impl object and so behave identically.

James
Sep 17 '07 #5
Nathan Harmston a écrit :
HI,

I m trying to start an api in a similar way to the djangic way of
Class.objects.all(). Ie objects is a "Manager" class.

So:

class Foo(object):
def __init__(self):
self.test = "NEE"

class Manager(object):
def __init__(self):
pass
def all(self):
return "COCONUTS"

Because of how some of the code is set up I cant use
metaclasses........so I try to use a decorator:

def addto(instance):
def decorator(f):
import new
f = new.instancemethod(f, instance, instance.__class__)
setattr(instance, "objects", f)
return f
return decorator

class Manager(object):
@addto(Foo)
def __init__(self):
.............

however this only binds the init method to the Foo.objects, so not
what I want.
Indeed.
If I try using classmethod...then it just says the
Foo.objects doesnt exist.
You mean decorating Manager.__init__ with classmethod ???

I may be wrong, but I suspect you don't have a clear idea of what you're
doing here.
Does anyone have any ideas how I can accomplish this using decorators?
Yes : don't use a decorator !-)

Instead of asking for how to implement what you think is the solution,
you might be better explaining the problem you're trying to solve.
And also preventing more than one Manager instance instantiated at one
time.
Q&D:

class Singleton(object):
def __new__(cls):
if not hasattr(cls, '_inst'):
cls._inst = object.__new__(cls)
return cls._inst
Same remark as above...
Sep 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Samee Zahur | last post: by
13 posts views Thread by Rob Meade | last post: by
5 posts views Thread by Water Cooler v2 | last post: by
reply views Thread by rosydwin | last post: by

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.