Opened 12 years ago

Closed 12 years ago

#18300 closed Cleanup/optimization (wontfix)

Don't repeat yourself? Are you sure about that?

Reported by: chris.uzal@… Owned by: nobody
Component: Forms Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Knowing that my business types would each have a specific discount, I created the following tuple:

BUSINESS_TYPES = (

('Ad Agency', 'Ad Agency', 0), # value, label, open rate discount
('Business Card Display', 'Business Card Display', 0),
('Local Business', 'Local Business', 0.5),
('Legal Office', 'Legal Office', 0.7),
('Classified Display', 'Classified Display', 0.7),
('Nonprofit', 'Nonprofit', 0.6),

)

My thinking is that it handles my drop down box and I can use it later to calculate the discount.

When I go to sync the database. I get:

Error: One or more models did not validate:
contrato.contrato: "business_type": "choices" should be a sequence of two-tuples.
contrato.contrato: "business_type": "choices" should be a sequence of two-tuples.
contrato.contrato: "business_type": "choices" should be a sequence of two-tuples.
contrato.contrato: "business_type": "choices" should be a sequence of two-tuples.
contrato.contrato: "business_type": "choices" should be a sequence of two-tuples.
contrato.contrato: "business_type": "choices" should be a sequence of two-tuples.

W8y? The only concern of the drop down are elements and zero and one. If I want to create extra spots in the sequence, it should be okay because i am dryer than thou. :-)

The problem was solved by using the following:

choices = [(elem[0], elem[1]) for elem in BUSINESS_TYPES]

Changing the way you read the tuple would make it more DRY.


Change History (2)

comment:1 by Aymeric Augustin, 12 years ago

Checking that CHOICES is a sequence of two tuples is useful to catch programming errors.

In your example, I suppose you'll to have to turn your sequence into a dict to make lookups by business type, so I don't really see the benefits of such a data structure. And there's a one liner to turn your custom datastructure into something acceptable (even shorter than your example: [elem[:2] for elem in BUSINESS_TYPES])

To sum up I'm not convinced to relax this check.

comment:2 by Luke Plant, 12 years ago

Resolution: wontfix
Status: newclosed

I agree with Augustin - it is so easy to manipulate datastructures like this in Python that there is no need to relax the definition of the input data to choices.

Note: See TracTickets for help on using tickets.
Back to Top