# Code Prepend Text

**URL:** <https://aiida.discourse.group/t/code-prepend-text/471>\
**Category:** General Usage\
**Created:** [September 27, 2024, 8:25am UTC](https://aiida.discourse.group/t/code-prepend-text/471 "2024-09-27T08:25:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minotakm](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/minotakm/32/184_2.png) [@Minotakm](https://aiida.discourse.group/u/Minotakm)\
**Post date:** [September 27, 2024, 8:25am UTC](https://aiida.discourse.group/t/code-prepend-text/471/1 "2024-09-27T08:25:51Z")

</div>

Hey All,

In the previous version, when I would like to do some changes in the prepend text of a code. I would load the code and then use the set\_prepend\_text() method. In the version (AiiDA v2.6.1) this method is deprecated and does not let me do any change. Is there a way to change the prepend text without having to redefine a new code?

---

<div class="post-metadata">

**Author:** ![agoscinski](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/agoscinski/32/147_2.png) [@agoscinski](https://aiida.discourse.group/u/agoscinski)\
**Post date:** [September 27, 2024, 8:58am UTC](https://aiida.discourse.group/t/code-prepend-text/471/2 "2024-09-27T08:58:40Z")

</div>

I think it is rather because your code is stored in the database and AiiDA does not allow you to change this property after is stored. See this code

```python
from aiida.orm import InstalledCode, load_computer
from aiida import load_profile
load_profile()

code = InstalledCode(
    computer=load_computer("localhost"),
    filepath_executable="/bin/bash",
    label="mycustom_code",
    default_calc_job_plugin="core.arithmetic.add",
    prepend_text="init"
)

print("code.prepend_text", code.prepend_text)
code.set_prepend_text("hello")
# recommended way now is to do it like this
# code.prepend_text = "hello"
# but this is not the problem
print("code.prepend_text", code.prepend_text)

print("\n\n")
code.store() # stored in database
try:
    code.set_prepend_text("hello")
except Exception as err:
    print("Error:\n", err) # Inmutable error

# same error also for this
try:
    code.prepend_text = "hello"
except Exception as err:
    print("Same error:\n", err) # Inmutable error

```

with output

```bash
code.prepend_text init
/Users/alexgo/miniconda3/lib/python3.12/site-packages/aiida/orm/nodes/data/code/legacy.py:405: AiidaDeprecationWarning: `Code.set_prepend_text` method is deprecated, use the `prepend_text` property instead. (this will be removed in v3)
  warn_deprecation(
code.prepend_text hello

Error:
 the attributes of a stored entity are immutable
Same error:
 the attributes of a stored entity are immutable

```

The only solution I see is to create a new code. You can use `code.clone`

```python
mynew_code = code.clone()
mynew_code.prepend_text = "my prepend text"

```

---

<div class="post-metadata">

**Author:** ![Minotakm](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/minotakm/32/184_2.png) [@Minotakm](https://aiida.discourse.group/u/Minotakm)\
**Post date:** [September 27, 2024, 9:32am UTC](https://aiida.discourse.group/t/code-prepend-text/471/3 "2024-09-27T09:32:11Z")

</div>

Yeah yeah, I got that. Still, previously I was able to do these changes even though the code was already stored in the database. Thanks for your help.

---

<div class="post-metadata">

**Author:** ![agoscinski](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/agoscinski/32/147_2.png) [@agoscinski](https://aiida.discourse.group/u/agoscinski)\
**Post date:** [September 27, 2024, 10:30am UTC](https://aiida.discourse.group/t/code-prepend-text/471/4 "2024-09-27T10:30:44Z")

</div>

Hm… not sure about this. If you look at the commit that changed the behavior (in 2022)

> <https://github.com/aiidateam/aiida-core/commit/a2f6d79ee519bcc9c9f8ae002a642d8643ce30fc#diff-25ad2ae0b36d1a125692946e4d41209e9e8dc4f8a74c9e3a604c9dfe20157e23L328>
>
> The \`Code\` plugin is used a lot and so cannot just be removed. Instead,
> we keep …it for now and have it subclass the new \`AbstractCode\`. This
> way, the class already has the interface that it will have once the
> deprecated interface is removed. This will allow users to start updating
> their use of it in a backwards-compatible manner.
> 
> The codebase is updated to replace the use of the deprecated interface
> with that of the new interface. No new instances of \`Code\` are created,
> only instances of the new \`RemoteCode\` and \`LocalCode\`.
> 
> When the time comes to drop the legacy \`Code\` class, all that is needed
> is a data migration that will update existing instances to either an
> instance of \`InstalledCode\` or \`PortableCode\` depending on the attributes
> that the instance defines.
> 
> The legacy methods and properties of \`Code\` plugin are deprecated.

You can see that it was before setting the attributes and not extras

```python
self.base.attributes.set('prepend_text', str(code))

```

which should not be mutable after storing.

---

<div class="post-metadata">

**Author:** ![Minotakm](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/minotakm/32/184_2.png) [@Minotakm](https://aiida.discourse.group/u/Minotakm)\
**Post date:** [September 27, 2024, 11:37am UTC](https://aiida.discourse.group/t/code-prepend-text/471/5 "2024-09-27T11:37:54Z")

</div>

Yeah, my bad ,actually it is computer that you can change the prepend text as you want.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/aiida/original/1X/c417197d3750949351575d03ed03f5e2ad32e649.png) [@system](https://aiida.discourse.group/u/system)\
**Post date:** [September 27, 2026, 11:38am UTC](https://aiida.discourse.group/t/code-prepend-text/471/6 "2026-09-27T11:38:06Z")

</div>

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