4

I've this python descriptor:

# Date Descriptor
class DateAttribute():
    def __init__(self, value=None):
        self.value = value
    def __get__(self, instance, value):
        return self.value
    def __set__(self, instance, value):
        if type(value) is not datetime.date: 
            raise TypeError('A date value is expected')
        self.value = value

and a class D that use this descriptor:

class D:
    thisdate = DateAttribute()

I use this class as:

x = D()
x.thisdate = datetime.date(2012, 9, 12)

I wish to extend the descriptor to give me formatted results in some ways. Es.

x.thisdate.format1
>>> '2012 9 12'
x.thisdate.format2
>>> '2012___9___12'
x.thisdate.format3
>>> '2012####9####12'
.....

I could do this ?

Thanks

4
  • 1
    Only if the descriptor returns an object with the format1, format2 etc... attribute (or properties). I.e. x.thisdate will not return a string Commented Sep 12, 2013 at 16:55
  • (The suggestions to just not do this with a descriptor are better.) Commented Sep 12, 2013 at 16:57
  • 1
    Also, if your descriptor is your actual use case, you could just use Enthought Traits. Commented Sep 12, 2013 at 16:58
  • 1
    Unless you have affirmative reason to use a descriptor, just use an attribute: x.thisdate = datetime.date(…). As you have it, you've made a class DateAttribute that does absolutely nothing. A poor reason for using a descriptor is "because that's how I had to do it in Java/C++ and I haven't understood why Python doesn't have private instance variables." Commented Sep 12, 2013 at 17:11

1 Answer 1

4

Add the formatX properties to your class and format accordingly:

class DateAttribute:
    ...

    @property
    def format1 (self): return self.value.strftime (SOMEFORMAT)

    @property
    def format2 (self): return self.value.strftime (SOMEOTHERFORMAT)

For the strftime format strings, see the datetime documentation.

Works like this:

thisdate = DateAttribute (datetime.date (2012, 9, 12) )
print (thisdate.format1)

EDIT:

#! /usr/bin/python3

import datetime

class DateAttribute:
    def __init__(self, value=None):
        print ('initing')
        self.value = value

    def __get__(self, instance, value):
        print ('getting')
        if not self.value: return self.value
        return FormattedDate (self.value.year, self.value.month, self.value.day)

    def __set__(self, instance, value):
        print ('setting')
        if not isinstance (value, datetime.date): raise TypeError('A date value is expected')
        self.value = value


class FormattedDate (datetime.date):
    @property
    def format1 (self): return self.strftime ('%Y==%m==%d')

class D:
    thisdate = DateAttribute ()

d = D ()
print (d.thisdate)
d.thisdate = datetime.date (2013, 1, 1)
print (d.thisdate)
print (d.thisdate.format1)

Produces this output:

initing
getting
None
setting
getting
2013-01-01
getting
2013==01==01
Sign up to request clarification or add additional context in comments.

9 Comments

If I use your code: x.thisdate.format I receive AttributeError: 'datetime.date' object has no attribute 'format1'
and then how should I choose?
Ok. In your example you use property when descritor is initialized. But i I want to use them in an instance of class D ?
I run your code ad receive: initing <__main__.DateAttribute instance at 0x7f116737c0e0> 2013-01-01 Traceback (most recent call last): File "aaa.py", line 32, in <module> print (d.thisdate.format1) AttributeError: 'datetime.date' object has no attribute 'format1'
The exact code (second snippet) runs as expected on my machine using Python 3.2.4 (default, May 10 2013, 08:57:38) [GCC 4.7.3] on linux2.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.