Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit d4b3597

Browse files
authored
Replace memory addresses with UUIDs to enhance the uniqueness of template_name (#210)
* replace memory addresses with UUIDs to enhance the uniqueness of template names * fix tests
1 parent 5406434 commit d4b3597

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

bluecellulab/cell/template.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import re
2222
import string
2323
from typing import NamedTuple, Optional
24+
import uuid
2425

2526
import neuron
2627

@@ -126,8 +127,8 @@ def load(self, template_filename: str) -> str:
126127
# templates load outside of bluecellulab
127128
template_name = "%s_bluecellulab" % template_name
128129
template_name = get_neuron_compliant_template_name(template_name)
129-
obj_address = hex(id(self))
130-
template_name = f"{template_name}_{obj_address}"
130+
unique_id = uuid.uuid4().hex
131+
template_name = f"{template_name}_{unique_id}"
131132

132133
template_content = re.sub(
133134
r"begintemplate\s*(\S*)",

tests/test_cell/test_core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import random
1818
import warnings
1919
from pathlib import Path
20+
from unittest.mock import patch
21+
import uuid
2022

2123
import neuron
2224
import numpy as np
@@ -47,13 +49,17 @@ def test_longname():
4749

4850

4951
@pytest.mark.v5
50-
def test_load_template():
52+
@patch('uuid.uuid4')
53+
def test_load_template(mock_uuid):
5154
"""Test the neuron template loading."""
55+
id = '12345678123456781234567812345678'
56+
mock_uuid.return_value = uuid.UUID(id)
57+
5258
hoc_path = parent_dir / "examples/cell_example1/test_cell.hoc"
5359
morph_path = parent_dir / "examples/cell_example1/test_cell.asc"
5460
template = NeuronTemplate(hoc_path, morph_path, "v5", None)
5561
template_name = template.template_name
56-
assert template_name == f"test_cell_bluecellulab_{hex(id(template))}"
62+
assert template_name == f"test_cell_bluecellulab_{id}"
5763

5864

5965
def test_shorten_and_hash_string():

tests/test_cell/test_template.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"""Unit tests for template.py module."""
1515

1616
from pathlib import Path
17-
from unittest.mock import Mock
17+
from unittest.mock import Mock, patch
18+
import uuid
1819

1920
import pytest
2021

@@ -44,11 +45,16 @@
4445
)
4546

4647

47-
def test_get_cell_with_bluepyopt_template():
48+
@patch('uuid.uuid4')
49+
def test_get_cell_with_bluepyopt_template(mock_uuid):
4850
"""Unit test for the get_cell method with bluepyopt_template."""
51+
52+
id = '12345678123456781234567812345678'
53+
mock_uuid.return_value = uuid.UUID(id)
4954
template = NeuronTemplate(hipp_hoc_path, hipp_morph_path, "bluepyopt", None)
5055
cell = template.get_cell(gid=None)
51-
assert cell.hname() == f"bACnoljp_bluecellulab_{(hex(id(template)))}[0]"
56+
57+
assert cell.hname() == f"bACnoljp_bluecellulab_{id}[0]"
5258

5359

5460
def test_neuron_template_init():

0 commit comments

Comments
 (0)