Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to set object parameters nicely?

Reply
Thread Tools

How to set object parameters nicely?

 
 
inhahe
Guest
Posts: n/a
 
      12-03-2009
On Thu, Dec 3, 2009 at 9:32 AM, inhahe <(E-Mail Removed)> wrote:
> On Thu, Dec 3, 2009 at 9:16 AM, Lie Ryan <(E-Mail Removed)> wrote:
>> On 12/2/2009 10:26 AM, allen.fowler wrote:
>>>
>>> I've tried this, but have found two issues:
>>>
>>> 1) I can't set default values.
>>> 2) I can't set required values.
>>>
>>> In both of the above cases, if the object is created without the
>>> "exact" dict() I expect, all the assumption my methods make about what
>>> is available in "self" fall apart.
>>>

>>

>
> def __init__(self, required1, required2, default1='d1', default2='d2',
> **kwargs):
> * *for par in kwargs:
> ** * *self.setattr(par, kwargs[par])
> * self.required1 = required1
> * self.required2 = required2
> * self.default1 = default1
> * self.default2 = default2
>
> or
>
> def __init__(self, required1, required2, default1='d1', default2='d2',
> **kwargs):
> * *for par in kwargs:
> ** * *self.setattr(par, kwargs[par])
> * self.required1 = required1
> * self.required2 = required2
> * self.default1 = default1
> * self.default2 = default2
>


(sorry, sent the same code twice. i was going to do it a different way
and realized it would be too magically)
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      12-03-2009
allen.fowler a écrit :
(snip)
> In this case, and I am trying to create a number of ORM-like objects.
> (Though, there is no database involved.)
>
> So, instances of these classes are acting as records that are shuttled
> around in the system, and the object's properties are acting as
> values. The parameters are (mostly) orthogonal, but do need defaults,
> and some must be required.


You could specify the names, defaults and validations required at an
upper level then automate the whole thing, ie (thinking out loud):


class Field(object):
def __init__(self, name,required=False,
default=None,target=None,validate=None):
self._name = name
self._required = required
self._default = default
self._target = target or name
self._validate = validate or lambda x: x

def validate(self, value):
""" _validate is supposed to raise a ValueError if not ok.
it can also do any required conversion, formatting etc
"""
return self._validate(value)

def set(self, instance, **kw):
value = kw.get(self._name, None)
if value is None
if self.required:
raise ValueError("argument %s is required" % self._name)
else:
value = self._default
value = self.validate(value)
setattr(instance, self._target, value)


class Record(object):
def __init__(self, **kw):
if not hasattr(self, "_fields"):
raise AttributeError("Record subclasses must define _fields")
for field in self._fields:
field.set(self, **kw)

class Foo(Record):
_fields = (
Field("bar", True, validate=lambda v : v > 1),
Field("baaz", default=42)
)



NB : totally untested code, so it will of course contains at least one
major and obvious bug / typo / whatever !-)

You could go further using an even more declarative API based on a fancy
custom metaclass and descriptors (for Fields) etc etc - and that even
might be the RightThing(tm) to do if you have more than moderatly
complex needs, but for simple cases the above should work fine without
going over the top with black magic.

HTH
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Command Object, Parameters, Set Cursor Type. etc? +Bob+ ASP General 4 11-29-2006 05:51 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
does a "parameters"-parameter overwrite the "parameters"-object? Florian Loitsch Javascript 11 03-15-2005 03:33 PM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM



Advertisments