Thursday, August 28, 2014

Re: Constants values and best practices

Hi Julian

Python 3 introduces enums, which should make this easy:
https://docs.python.org/3/library/enum.html
In my Python 2 projects, I define a class to club together related
constants. Something like,

class PurchaseState(object):
EGGS = 'eggs'
FOO = 'foo'
BAR = 'bar'

and then do

if purchase.state == PurchaseState.EGGS:
# do stuff

Note that I prefer using string values for my constants over integer
values, as it makes debugging far more pleasant.

Cheers

On Thu, Aug 28, 2014 at 10:33 PM, Julo Waks <ifjulo@gmail.com> wrote:
> Good day!
> How are you?
> I have the following situation:
>
> In more than one django project I come across the situation where I need to
> define a model that has a State.
> So what I usually do is:
>
> # Pseudo code here
>
> def State (models.Model):
> name = models.CharField ()
>
> def Purchase (models.Model):
> # Fields various (name, descrption, etc)
> State = models.ForgeinKey (State)
> # More fields ...
>
> The problem arises when I want to do things like:
>
> if (Purchase.State_id == 1):
>
> # Do stuff
>
>
> my way to fix it so far was to define a const.py and do something like:
>
> if (== Purchase.State_id Const.STATE_EGGS):
>
> # Do stuff
>
> Because the state 1 is "eggs", but because I add in my local, but if another
> dev adds in staging, as that uniformity is maintained?
> It should create a process that creates the updates as new constants was
> defined?
> There are best practices for this?
>
> Ideas? Advice?
>
> Thank you very much for reading =)
>
> regards,
> Julian.
>
> --
> 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+unsubscribe@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/CAKSEZ1EfdSiaNwd6ZxL0NUhfn3HM39zdbg1CVkRXNyObwLxjcA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Yati Sagade

Software Engineer at mquotient

Twitter: @yati_itay | Github: yati-sagade

Organizing member of TEDx EasternMetropolitanBypass
http://www.ted.com/tedx/events/4933
https://www.facebook.com/pages/TEDx-EasternMetropolitanBypass/337763226244869

--
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+unsubscribe@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/CANpY%2BHkT%2BwPAT6gh%2BDoDQ418x83BYvnV%2BQs3goY23LYCcSX5yw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment