OOP noob question: Mixin properties

2012-12-12 Thread Micky Hulse
Dear Python Santa gurus, ;D

I have this Django mixin:



...which is used to override render_to_response() so I can output a
JSON response (the above code is pretty much straight from the Django
docs: 
).

The JSONResponseMixin() gets added to my view class like so:

class Api(JSONResponseMixin, BaseDetailView):
# ...

Within my the mixins.py file, at the top of the file, I've added these
constants:

CACHE_TIMEOUT = 86400 # 24 hours.
CACHE_NAME = 'ad_manager_api'

Question(s):

I'd like to convert those constants to properties and make my
JSONResponseMixin() class more generic and portable.

Django aside, could someone provide OOP Python examples of how I could
instantiate a mixin Class and set/override its properties before
passing data to said mixin?

I hope you don't mind that this question involves Django... I'm just
looking to improve my core Python skills (so, generic Python examples
would be cool).

Many thanks in advance!

Cheers,
Micky
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OOP noob question: Mixin properties

2012-12-13 Thread Micky Hulse
On Wed, Dec 12, 2012 at 7:49 PM, Micky Hulse  wrote:
> I hope you don't mind that this question involves Django... I'm just
> looking to improve my core Python skills (so, generic Python examples
> would be cool).

Just experimenting:

<https://gist.github.com/4279705>

I think that will help me achieve my desired end result.

I'm open to feedback.

Cheers!
M
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OOP noob question: Mixin properties

2012-12-14 Thread Micky Hulse
Hi Steven!!! Thanks so much for the pro help, I really do appreciate it. :)

On Thu, Dec 13, 2012 at 4:53 PM, Steven D'Aprano
 wrote:
> Indentation is important. Please don't remove it. I've added it back in
> below:

Yikes! Sorry about that. I won't do that again in the future. :(

Thanks for adding the indentation back. :)

> The use of @property here doesn't give you any benefit (except, maybe, if
> you are paid by the line of code). Better to just expose the "cache"
> attribute directly, and likewise for the others. If, in the future, you
> need to turn them into computed properties, you can easily do so. But for
> now:

Ah, great to know! Thank you for the clarification, I needed that.

> class JSONResponseMixin(object):
> def __init__(self):
> self.cache = False
> self.cache_timeout = 86400
> self.cache_key = None
>
> and you're done.

Beautiful! That's nice. Very simple and clean (I had a feeling my
@decorator version was getting a bit "wordy").

>> Simply doing this:
>> class JSONResponseMixin(object):
>> cache = False
>> cache_timeout = 86400 # 24 hours.
>> cache_key = None
>> ...works just as good! Not to mention, it's less verbose.
> In my code above, the cache attributes are defined on a per-instance
> basis. Each instance will have its own set of three cache* attributes. In
> the version directly above, there is a single set of cache* attributes
> shared by all JSONResponseMixin instances.

Thank you for pointing this out! I had not realized that this was the
case. I'm still kinda new to OOP in Python and Python in general (I'm
learning it through Django).

> Will this make any practical difference? Probably not. Most importantly,
> if you write:
> instance = Api()  # class includes JSONResponseMixin
> instance.cache = True
> then the assignment to an attribute will create a non-shared per-instance
> attribute which overrides the class-level shared attribute. This is
> almost certainly the behaviour that you want. So this is a good example
> of using class attributes to implement shared default state.

Whoa, that's cool!

> There is one common place where the use of shared class attributes can
> lead to surprising results: if the class attribute is a mutable object
> such as a list or a dict, it may not behave the way you expect. That is
> because only *assignment* creates a new per-instance attribute:
> instance.attribute = "spam spam spam"  # overrides the class attribute
> while modifying the attribute in place does not:
> instance.shared_list.append("spam spam spam")
> # every instance of the class will now see the change

Exactly the type of details I was hoping to learn. Thank you so much
for taking the time to explain things in such detail and easy to
understand way. I really appreciate it. :)

> Apart from that Gotcha, the use of shared class attributes to implement
> default behaviour is perfectly acceptable.

Awesome! That's good to know.

> I actually prefer a functional response, up to the point where I'm
> passing so many arguments to functions that I can't keep track of what's
> what. Then I refactor into a class.

Ah, well that's good to know. There's a part of me that wants to
revert back to passing arguments to self.render_to_response(...);
after all, there's only 3 of them. On the flip side, if I hadn't
explored other options, I wouldn't have learned anything new.

Thanks a billion Steven! I owe you one.

Have a great holiday.

Cheers,
Micky
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Framework for a beginner

2012-04-11 Thread Micky Hulse
Hello,

I've got more experience with PHP stuff, but for Python, I would
recommend checking out Django "getting started":



... to see if you like it.

Having used a few PHP frameworks and CMSs, I really dig that Django
has a built-in admin; I am in the same boat as you for when it comes
to "rebuild my portfolio using python and a framework", and I am happy
to use the Django admin as it saves me the headache of having to build
one myself.

For hosting (more than Django too) I highly recommend WebFaction:



The one-click install process is awesome. Great support too. Cheap
with lots of control and room to grow.

Hope that helps!

Cheers,
Micky
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replace pip installed package with latest git version?

2011-09-14 Thread Micky Hulse
Hello,

On Wed, Sep 14, 2011 at 1:44 AM, Alec Taylor  wrote:
> I've installed a version of python-creole from pip. How do I upgrade
> it to the latest git version?

Not sure if you got an answer yet, but this is how I would do it:

sudo pip install --upgrade
git+git://github.com/jedie/python-creole.git#egg=python-creole

The "sudo" may or may not be required.



Hths.

Cheers,
Micky
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replace pip installed package with latest git version?

2011-09-14 Thread Micky Hulse
On Wed, Sep 14, 2011 at 9:58 AM, Micky Hulse  wrote:
> Not sure if you got an answer yet, but this is how I would do it:
> sudo pip install --upgrade
> git+git://github.com/jedie/python-creole.git#egg=python-creole

Having read your message more closely, it sounds like you did not
install the package originally form github? If that's the case, I
don't think using --upgrade will be of any help. Sorry 'bout that.

I suppose you would have to uninstall the original PIP version:

sudo pip uninstall egg-name.egg (or just the package name)

... and then:

sudo pip install -e
git+git://github.com/jedie/python-creole.git#egg=python-creole

Although, I am not sure if that is the best way to do it.

Crawling back into my hole now. :)

Micky
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Motion Tracking with Python

2011-09-29 Thread Micky Hulse
That's really cool! Thanks for sharing! :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Programing Language: latitude-longitude-decimalize

2011-11-29 Thread Micky Hulse
Last time I did this was using AS3. The format I used was DMS:

GPSLongitude: 122,42,47.79
GPSLongitudeRef: W
GPSLatitude: 45,30,30.390001198897014
GPSLatitudeRef: N

Here's the method:



Not shown in above code: If West longitude or South latitude I would
make that DD (decimal degree) value negative.

Anyway, that was a fun learning experience! :)
-- 
http://mail.python.org/mailman/listinfo/python-list