Re: Best way to handle class attribute as a space separated string?

2009-12-21 Thread Margie Roginski
Ok, well just thought maybe I was missing some  python builtin or
django method that others were using.  Looks like it's just something
that needs be coded up and encapsulated.

Thanks for your comments!

Margie

On Dec 21, 2:03 pm, Kieran Brownlees  wrote:
> Not very practical but fun to make and good for wtf moments when
> reading old code :)
>
> self.attrs['class'] = ' '.join([x for x in self.attr.get('class') and
> self.attr['class'].split(' ') or []] + [self.pastClass])
>
> Could make it a little shorter with two lines:
> e, a, p = self.attrs.get('class'), self.attrs, self.pastClass
> a['class'] = ' '.join([x for x in e and e.split(' ') or []] + [p])
>
> Could fit the final version in with three tabs and it's only 79 chars!
>
> Kieran
>
> On Dec 22, 10:32 am, Margie Roginski  wrote:
>
> > I have a variety of places in my code where I add a class to a
> > widget.  For example, I have a render() function for a DateWidget that
> > contains this code, which adds a special class if the date is in the
> > past.
>
> >                 if date < datetime.datetime.now():
> >                     if self.attrs.get("class"):
> >                         self.attrs["class"] += " " + self.pastClass
> >                     else:
> >                         self.attrs["class"] = self.pastClass
>
> > This checks if there's already a class attribute and if there is,
> > appends a space and then the string in self.pastClass, and if tehre is
> > not, just creates the class attribute containing self.pastClass.
>
> > This seems like a lot of code to do something really simple and I feel
> > like I'm repeating it in various places.  I'm wondering if there is
> > some better way that folks deal with this little nit?
>
> > Margie

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Re: Best way to handle class attribute as a space separated string?

2009-12-21 Thread Shawn Milochik
I'd use a dictionary or list. If it has to persist, use simplejson to  
serialize it and create a couple of functions to wrap the JSON stuff.

Then you can use property() to simplify it further.

Shawn

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Best way to handle class attribute as a space separated string?

2009-12-21 Thread Margie Roginski
I have a variety of places in my code where I add a class to a
widget.  For example, I have a render() function for a DateWidget that
contains this code, which adds a special class if the date is in the
past.

if date < datetime.datetime.now():
if self.attrs.get("class"):
self.attrs["class"] += " " + self.pastClass
else:
self.attrs["class"] = self.pastClass

This checks if there's already a class attribute and if there is,
appends a space and then the string in self.pastClass, and if tehre is
not, just creates the class attribute containing self.pastClass.

This seems like a lot of code to do something really simple and I feel
like I'm repeating it in various places.  I'm wondering if there is
some better way that folks deal with this little nit?

Margie

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.