Skip to content

Commit

Permalink
FIX: fixed some plots not showing in the console
Browse files Browse the repository at this point in the history
expressions like "arr[anything].plot(arg=anything)" were mistakenly considered as inplace updates
  • Loading branch information
gdementen committed Aug 1, 2024
1 parent 641f091 commit e6cc2a6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
40 changes: 4 additions & 36 deletions doc/source/changes/version_0_34_6.rst.inc
Original file line number Diff line number Diff line change
@@ -1,41 +1,9 @@
.. py:currentmodule:: larray_editor

Syntax changes
^^^^^^^^^^^^^^

* renamed ``MappingEditor.old_method_name()`` to :py:obj:`MappingEditor.new_method_name()` (closes :editor_issue:`1`).

* renamed ``old_argument_name`` argument of :py:obj:`MappingEditor.method_name()` to ``new_argument_name``.


Backward incompatible changes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* other backward incompatible changes


New features
^^^^^^^^^^^^

* added a feature (see the :ref:`miscellaneous section <misc_editor>` for details).

* added another feature in the editor (closes :editor_issue:`1`).

.. note::

- It works for foo bar !
- It does not work for foo baz !


.. _misc_editor:

Miscellaneous improvements
^^^^^^^^^^^^^^^^^^^^^^^^^^

* improved something.


Fixes
^^^^^

* fixed something (closes :editor_issue:`1`).
* fixed some console plots (again) because a "fix" in the partially done
0.34.5 release introduced other problems. Sadly, these new problems were
discovered after the release process for 0.34.5 was started, hence the
partial release.
16 changes: 10 additions & 6 deletions larray_editor/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@

REOPEN_LAST_FILE = object()

assignment_pattern = re.compile(r'[^\[\]]+[^=]=[^=].+')
setitem_pattern = re.compile(r'(\w+)(\.i|\.iflat|\.points|\.ipoints)?\[.+\][^=]*=[^=].+')
history_vars_pattern = re.compile(r'_i?\d+')
ASSIGNMENT_PATTERN = re.compile(r'[^\[\]]+[^=]=[^=].+')
SUBSET_UPDATE_PATTERN = re.compile(r'(\w+)'
r'(\.i|\.iflat|\.points|\.ipoints)?'
r'\[.+\]\s*'
r'([-+*/%&|^><]|//|\*\*|>>|<<)?'
r'=\s*[^=].*')
HISTORY_VARS_PATTERN = re.compile(r'_i?\d+')
# XXX: add all scalars except strings (from numpy or plain Python)?
# (long) strings are not handled correctly so should NOT be in this list
# tuple, list
Expand Down Expand Up @@ -668,7 +672,7 @@ def delete_current_item(self):
def line_edit_update(self):
import larray as la
last_input = self.eval_box.text()
if assignment_pattern.match(last_input):
if ASSIGNMENT_PATTERN.match(last_input):
context = self.data._objects.copy()
exec(last_input, la.__dict__, context)
varname = self.update_mapping(context)
Expand All @@ -690,13 +694,13 @@ def ipython_cell_executed(self):
ip_keys = {'In', 'Out', '_', '__', '___', '__builtin__', '_dh', '_ih', '_oh', '_sh', '_i', '_ii', '_iii',
'exit', 'get_ipython', 'quit'}
# '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__',
clean_ns = {k: v for k, v in user_ns.items() if k not in ip_keys and not history_vars_pattern.match(k)}
clean_ns = {k: v for k, v in user_ns.items() if k not in ip_keys and not HISTORY_VARS_PATTERN.match(k)}

# user_ns['_i'] is not updated yet (refers to the -2 item)
# 'In' and '_ih' point to the same object (but '_ih' is supposed to be the non-overridden one)
cur_input_num = len(user_ns['_ih']) - 1
last_input = user_ns['_ih'][-1]
setitem_match = setitem_pattern.match(last_input)
setitem_match = SUBSET_UPDATE_PATTERN.match(last_input)
if setitem_match:
varname = setitem_match.group(1)
# setitem to (i)python special variables do not concern us
Expand Down
32 changes: 32 additions & 0 deletions larray_editor/tests/test_inplace_modification_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from larray_editor.editor import SUBSET_UPDATE_PATTERN


def test_pattern():
assert SUBSET_UPDATE_PATTERN.match('arr1[1] = 2')
assert SUBSET_UPDATE_PATTERN.match('arr1[1]= 2')
assert SUBSET_UPDATE_PATTERN.match('arr1[1]=2')
assert SUBSET_UPDATE_PATTERN.match("arr1['a'] = arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[func(mapping['a'])] = arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1.i[0, 0] = arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1.iflat[0, 0] = arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1.points[0, 0] = arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1.ipoints[0, 0] = arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] += arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] -= arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] *= arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] /= arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] %= arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] //= arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] **= arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] &= arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] |= arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] ^= arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] >>= arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0] <<= arr2")
assert SUBSET_UPDATE_PATTERN.match("arr1[0]") is None
assert SUBSET_UPDATE_PATTERN.match("arr1.method()") is None
assert SUBSET_UPDATE_PATTERN.match("arr1[0].method()") is None
assert SUBSET_UPDATE_PATTERN.match("arr1[0].method(arg=thing)") is None
assert SUBSET_UPDATE_PATTERN.match("arr1[0].method(arg==thing)") is None
# this test fails but I don't think it is possible to fix it with regex
# assert SUBSET_UPDATE_PATTERN.match("arr1[func('[]=0')].method()") is None

0 comments on commit e6cc2a6

Please sign in to comment.