0

I have a long condition which makes it possible to check if a field has been modified in order to call a function :

    if self.half_pension or not self.half_pension or self.half_pension_occasional \
        or not self.half_pension_occasional or self.half_pension_id != self._origin.half_pension_id \
        or self.half_pension_begin_date != self._origin.half_pension_begin_date \
        or self.half_pension_end_date != self._origin.half_pension_end_date \
        or self.half_pension_monday != self._origin.half_pension_monday \
        or self.half_pension_tuesday != self._origin.half_pension_tuesday \
        or self.half_pension_thursday != self._origin.half_pension_thursday \
        or self.half_pension_friday != self._origin.half_pension_friday \
        or self.half_pension_responsible_partner != self._origin.half_pension_responsible_partner:
            monstringxmlhalfpension = self.get_data_xml_for_halfpension(idUsager)
            resp_halfpension = c.service.XmlAjouterUnEvenement(Synchrone=1, donneesXml=monstringxmlhalfpension)

This is meant to be used within Odoo 10.

Is there a way to shorten this condition? Thank you

norok2
  • 25,683
  • 4
  • 73
  • 99
PseudoWithK
  • 321
  • 1
  • 19

3 Answers3

1

Please be aware that these conditions are always true:

if self.half_pension or not self.half_pension \
   or self.half_pension_occasional or not self.half_pension_occasional

A boolean value is either true or false, so the expression above is a tautology: a condition that's always true. To put it another way: there is no way for the if statement in the question to be false.

Also, notice that a condition for self.half_pension_wednesday is missing, I don't know if that's intentional.

Having said that - you seem to be comparing all the fields in an object, how about overriding __eq__ and __ne__ so two objects are equal if and only if all of the fields you're interested in are the same? and conversely, they're different if just one of the fields is different. With that, your condition can be as simple as:

if self != self._origin:
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Sorry, i have a test with self != self._origin but don't works. I have a error : if self.half_pension and self != self._origin: AttributeError: 'ecole.partner.school' object has no attribute '_origin' – PseudoWithK Dec 10 '18 at 15:54
  • First you have to _override_ `__eq__` and `__ne__`, it's not as simple as writing `self != self._origin`. Second, make sure you're in the right object, if it complains of not finding `_origin` is because you're in an incorrect place. – Óscar López Dec 10 '18 at 16:34
  • This should get you started: https://stackoverflow.com/questions/390250/elegant-ways-to-support-equivalence-equality-in-python-classes – Óscar López Dec 10 '18 at 16:38
  • Also, it's impossible to help you more without an [MVCE](https://stackoverflow.com/help/mcve). I can't reproduce your error, so I cannot help you. – Óscar López Dec 10 '18 at 17:01
1

Considering this:

self.SOMETHING1 != self._origin.SOMETHING1 
or self.SOMETHING2 != self._origin.SOMETHING2
or self.SOMETHING3 != self._origin.SOMETHING3

it can be shortened by using getattr:

attrs = ['SOMETHING1', 'SOMETHING2', 'SOMETHING3']
any(getattr(self, attr) != getattr(self._origin, attr) for attr in attrs)

Of course, you could also create your attrs dynamically, e.g. by using:

attrs = [attr for attr in dir(self) if attr.startswith('half_pension')]

or something along these lines. You have to walk the extra mile yourself since you did not provide a MVCE and we have no info on what is inside self.

norok2
  • 25,683
  • 4
  • 73
  • 99
0

Try using boolean valiables instead:

begin_date_error = self.half_pension_begin_date != self._origin.half_pension_begin_date 
mond_error = self.half_pension_monday != self._origin.half_pension_monday 

etc.

You can even agregate some of your or conditions if needed (remenber, 'or'is associative) , like :

week_error = mond_error or tue_error or wed_error or thu_error or fri_error

etc.

This means that in the end, your condition will include these variable names and not whole conditions and will therefore be much shorter.

Matina G
  • 1,452
  • 2
  • 14
  • 28