Re: Making functions callable

2014-03-26 Thread Anthony Hawkes
Thanks Guys, especially Javier! I've spent so much time trawling Google for 
this info!

On Thursday, 27 March 2014 04:57:30 UTC+8, Anthony Hawkes wrote:
>
> Hi Guys,
>
> I'm running into a problem with django(I guess this would also affect 
> Python in general) where if I create a view eg
> def view(year=today.year())
> The year is never re-evaluated until the server is reloaded/restarted
>
> I'm trying to figure out how to make a callable method accessible as a 
> property if this is even possible to try and rectify this(not sure if this 
> is even the correct approach). I've had a look at some magic methods but 
> can't figure it out.
>
> Basically I want to make eg
>
> def year(something):
> return 'blah'
>
> Accessible using object.year as well as object.year()
>
> Any pointers/ideas etc?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c02b9898-fbcc-41d3-a982-0b00f06e011f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Making functions callable

2014-03-26 Thread Javier Guerra Giraldez
On Wed, Mar 26, 2014 at 5:53 PM, Anthony  wrote:
> I've yet to test it but I read a few people stating to use lambdas
> variables/properties. Is it definitely the case that it will only evaluate
> once?


from the python docs (2.7):

Default parameter values are evaluated when the function definition is
executed. This means that the expression is evaluated once, when the
function is defined, and that the same “pre-computed” value is used
for each call. This is especially important to understand when a
default parameter is a mutable object, such as a list or a dictionary:
if the function modifies the object (e.g. by appending an item to a
list), the default value is in effect modified. This is generally not
what was intended. A way around this is to use None as the default,
and explicitly test for it in the body of the function.

-- 
Javier

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFkDaoQimq3%2B6b4mpT3qdWvP%3DfgDM9STEcLLdN3mx1MHTRaGkQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Making functions callable

2014-03-26 Thread John DeRosa
Not sure what is going on without seeing the code. But something is None when 
it should be a string, as the exception is telling you.

It's often easiest to try out problem code interactively in Python, and zero in 
on the problem.

John


On Mar 26, 2014, at 3:53 PM, Anthony  wrote:

> Thanks John,
> 
> I've yet to test it but I read a few people stating to use lambdas 
> variables/properties. Is it definitely the case that it will only evaluate 
> once?
> 
> Either way I've embarked on the mission to make methods return as properties 
> and have found @property decorator to fit my needs, the issue was I was 
> calling Object.property not ObjectI().property, However I want to apply this 
> functionality to all my methods and am using:
> 
> __getattribute__
> 
> def __getattribute__(self, item):
> super(DateCalc, self).__getattribute__(item)
> 
> But I've run into the problem that my __init__ properties don't seem to be 
> initialised eg I get
> self._date_actual = datetime.strptime("{Y}-{m}-{d}".format(Y=year, m=month, 
> d=day), self._format_global).date() TypeError: must be string, not None
> 
> Anyone have an idea about what's going on?
> 
> 
> On Thu, Mar 27, 2014 at 5:07 AM, John DeRosa  wrote:
> For the default value to work as you expect, do this:
> 
> def view(request, year=None):
> if year is None:
> year = today.year()
> 
> 
> Kwarg defaults are evaluated when the module is interpreted for the first 
> time.
> 
> John
> 
> 
> On Mar 26, 2014, at 1:57 PM, Anthony Hawkes  wrote:
> 
>> Hi Guys,
>> 
>> I'm running into a problem with django(I guess this would also affect Python 
>> in general) where if I create a view eg
>> def view(year=today.year())
>> The year is never re-evaluated until the server is reloaded/restarted
>> 
>> I'm trying to figure out how to make a callable method accessible as a 
>> property if this is even possible to try and rectify this(not sure if this 
>> is even the correct approach). I've had a look at some magic methods but 
>> can't figure it out.
>> 
>> Basically I want to make eg
>> 
>> def year(something):
>> return 'blah'
>> 
>> Accessible using object.year as well as object.year()
>> 
>> Any pointers/ideas etc?
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users+unsubscr...@googlegroups.com.
>> 
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/e7adace8-ffc9-4f77-b970-e73bed5ae8e1%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
> 
> 
> -- 
> You received this message because you are subscribed to a topic in the Google 
> Groups "Django users" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/django-users/gcx1oTBAZlA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to 
> django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/FE30EAD3-E463-4379-B11C-970D86703AC3%40ipstreet.com.
> 
> For more options, visit https://groups.google.com/d/optout.
> 
> 
> 
> -- 
> Anthony Hawkes
> E-Mail: lifesillus...@gmail.com
> Ph: 0400 372 260
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/CABqJoT1rSB4YeHQHiZH_4KNqMuub1THHyuzAvOCjas_Drz94sg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7BECE858-26BB-402C-A5DA-5A613F14FE1F%40ipstreet.com.
For more options, visit https://groups.google.com/d/optout.


Re: Making functions callable

2014-03-26 Thread Mike Dewhirst

On 27/03/2014 7:57am, Anthony Hawkes wrote:

Hi Guys,

I'm running into a problem with django(I guess this would also affect
Python in general) where if I create a view eg
def view(year=today.year())
The year is never re-evaluated until the server is reloaded/restarted

I'm trying to figure out how to make a callable method accessible as a
property if this is even possible to try and rectify this(not sure if
this is even the correct approach). I've had a look at some magic
methods but can't figure it out.

Basically I want to make eg

def year(something):
 return 'blah'


You should be able to pass the callable as an argument. It then gets 
evaluated each time it is called ...


Here is one I use ... from time to time :)

def when():
return datetime.now(tz=pytz.utc)




Accessible using object.year as well as object.year()

Any pointers/ideas etc?

--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to django-users+unsubscr...@googlegroups.com
.
To post to this group, send email to django-users@googlegroups.com
.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/e7adace8-ffc9-4f77-b970-e73bed5ae8e1%40googlegroups.com
.
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/53335B04.4040701%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.


Re: Making functions callable

2014-03-26 Thread Anthony
Thanks John,

I've yet to test it but I read a few people stating to use lambdas
variables/properties. Is it definitely the case that it will only evaluate
once?

Either way I've embarked on the mission to make methods return as
properties and have found @property decorator to fit my needs, the issue
was I was calling Object.property not ObjectI().property, However I want to
apply this functionality to all my methods and am using:

__getattribute__

def __getattribute__(self, item):
super(DateCalc, self).__getattribute__(item)

But I've run into the problem that my __init__ properties don't seem to be
initialised eg I get
self._date_actual = datetime.strptime("{Y}-{m}-{d}".format(Y=year, m=month,
d=day), self._format_global).date() TypeError: must be string, not None

Anyone have an idea about what's going on?


On Thu, Mar 27, 2014 at 5:07 AM, John DeRosa  wrote:

> For the default value to work as you expect, do this:
>
> def view(request, year=None):
> if year is None:
> year = today.year()
>
>
> Kwarg defaults are evaluated when the module is interpreted for the first
> time.
>
> John
>
>
> On Mar 26, 2014, at 1:57 PM, Anthony Hawkes 
> wrote:
>
> Hi Guys,
>
> I'm running into a problem with django(I guess this would also affect
> Python in general) where if I create a view eg
> def view(year=today.year())
> The year is never re-evaluated until the server is reloaded/restarted
>
> I'm trying to figure out how to make a callable method accessible as a
> property if this is even possible to try and rectify this(not sure if this
> is even the correct approach). I've had a look at some magic methods but
> can't figure it out.
>
> Basically I want to make eg
>
> def year(something):
> return 'blah'
>
> Accessible using object.year as well as object.year()
>
> Any pointers/ideas etc?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
>
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e7adace8-ffc9-4f77-b970-e73bed5ae8e1%40googlegroups.com
> .
> For more options, visit https://groups.google.com/d/optout.
>
>
>  --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/gcx1oTBAZlA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/FE30EAD3-E463-4379-B11C-970D86703AC3%40ipstreet.com
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Anthony Hawkes
E-Mail: lifesillus...@gmail.com
Ph: 0400 372 260

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABqJoT1rSB4YeHQHiZH_4KNqMuub1THHyuzAvOCjas_Drz94sg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Making functions callable

2014-03-26 Thread John DeRosa
For the default value to work as you expect, do this:

def view(request, year=None):
if year is None:
year = today.year()


Kwarg defaults are evaluated when the module is interpreted for the first time.

John


On Mar 26, 2014, at 1:57 PM, Anthony Hawkes  wrote:

> Hi Guys,
> 
> I'm running into a problem with django(I guess this would also affect Python 
> in general) where if I create a view eg
> def view(year=today.year())
> The year is never re-evaluated until the server is reloaded/restarted
> 
> I'm trying to figure out how to make a callable method accessible as a 
> property if this is even possible to try and rectify this(not sure if this is 
> even the correct approach). I've had a look at some magic methods but can't 
> figure it out.
> 
> Basically I want to make eg
> 
> def year(something):
> return 'blah'
> 
> Accessible using object.year as well as object.year()
> 
> Any pointers/ideas etc?
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/e7adace8-ffc9-4f77-b970-e73bed5ae8e1%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/FE30EAD3-E463-4379-B11C-970D86703AC3%40ipstreet.com.
For more options, visit https://groups.google.com/d/optout.


Making functions callable

2014-03-26 Thread Anthony Hawkes
Hi Guys,

I'm running into a problem with django(I guess this would also affect 
Python in general) where if I create a view eg
def view(year=today.year())
The year is never re-evaluated until the server is reloaded/restarted

I'm trying to figure out how to make a callable method accessible as a 
property if this is even possible to try and rectify this(not sure if this 
is even the correct approach). I've had a look at some magic methods but 
can't figure it out.

Basically I want to make eg

def year(something):
return 'blah'

Accessible using object.year as well as object.year()

Any pointers/ideas etc?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e7adace8-ffc9-4f77-b970-e73bed5ae8e1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.