# Repetead code across various modules

**URL:** <https://discuss.tryton.org/t/repetead-code-across-various-modules/1123>\
**Category:** Developer\
**Created:** [February 8, 2019, 8:50am UTC](https://discuss.tryton.org/t/repetead-code-across-various-modules/1123 "2019-02-08T08:50:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![josesalvador](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/josesalvador/32/602_2.png) [@josesalvador](https://discuss.tryton.org/u/josesalvador)\
**Post date:** [February 8, 2019, 8:50am UTC](https://discuss.tryton.org/t/repetead-code-across-various-modules/1123/1 "2019-02-08T08:50:17Z")

</div>

In advance excuse my ignorance please, I just want to understand and learn.

A very last commit to tryton (Lock records when processing) caught my attention around that code:

```
+ @classmethod
+ def __lock(cls, records):
+ from trytond.tools import grouped_slice, reduce_ids
+ from sql import Literal, For
+ transaction = Transaction()
+ database = transaction.database
+ connection = transaction.connection
+ table = cls. __table__ ()
+
+ if database.has_select_for():
+ for sub_records in grouped_slice(records):
+ where = reduce_ids(table.id, sub_records)
+ query = table.select(
+ Literal(1), where=where, for_=For('UPDATE', nowait=True))
+ with connection.cursor() as cursor:
+ cursor.execute(*query)
+ else:
+ database.lock(connection, cls._table)
+

```

It’s the same useful code repeated across account\_payment\_stripe, purchase and, at least, sale.

I would like to understand why that code can not be extracted/refactored to be shared among those modules instead to repeat it in each one.

Does not it fit well in another place of tryton’s framework??

---

<div class="post-metadata">

**Author:** ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)\
**Post date:** [February 8, 2019, 9:02am UTC](https://discuss.tryton.org/t/repetead-code-across-various-modules/1123/2 "2019-02-08T09:02:39Z")

</div>

> [@josesalvador](#):
>
> I would like to understand why that code can not be extracted/refactored to be shared among those modules instead to repeat it in each one.
> 
> Does not it fit well in another place of tryton’s framework??

The code was repeated in order to be able to backport to older versions. On trunk version a [new function has introduced to lock the records.](http://hg.tryton.org/trytond/rev/0ac9a6f5748b) This function is generic and can be used to lock any records of any model.

---

<div class="post-metadata">

**Author:** ![josesalvador](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/josesalvador/32/602_2.png) [@josesalvador](https://discuss.tryton.org/u/josesalvador)\
**Post date:** [February 8, 2019, 9:11am UTC](https://discuss.tryton.org/t/repetead-code-across-various-modules/1123/3 "2019-02-08T09:11:25Z")

</div>

What a pleasure read that answer.

Tryton rocks!!!

Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)\
**Post date:** [March 10, 2019, 9:11am UTC](https://discuss.tryton.org/t/repetead-code-across-various-modules/1123/4 "2019-03-10T09:11:32Z")

</div>

Closed on March 10, 2019, 9:11am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
