Skip to content

issue 10: view several sessions same time #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions larray_editor/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
REOPEN_LAST_FILE = object()

assignment_pattern = re.compile('[^\[\]]+[^=]=[^=].+')
getitem_pattern = re.compile('(\w+)\[(.+?)\].*')
getattr_pattern = re.compile('(\w+)\.(\w+).*')
setitem_pattern = re.compile('(\w+)\[(.+?)\].*[^=]=[^=].+')
setattr_pattern = re.compile('(\w+)\.(\w+)[^\w=]+=[^=].+')
history_vars_pattern = re.compile('_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
Expand Down Expand Up @@ -873,38 +873,34 @@ def ipython_cell_executed(self):
self.select_array_item(last_input)
return

# check if expression of the kind '<varname>[(...)] (...)' or '<varname>.<attribute> (...)'
varname = itemname = None
m = getitem_pattern.match(last_input)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know whether it is actually needed to check for setXXX patterns explicitly (previously the idea was to have setXXX for free from the getXXX pattern IIRC), but not checking at all for the getXXX pattern will introduce a slight regression AFAICT : it will not select the array (in the tree widget) if you type "income['M']" but will select it if you type "income". Did you intentionally remove that feature or was it an oversight?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe, I just checked and this didn't work previously. 😉 I wonder if we should support that or not.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arr1 = ndtest((3, 4))
arr2 = ndtest((3, 4, 5))
arr1['b0']

# check if expression of the kind '<varname>[(...)] = (...)' or '<varname>.<attribute> = (...)'
varname = None
m = setitem_pattern.match(last_input)
if m:
varname = m.group(1)
itemname = m.group(2).replace("'", "").replace('"', '')
m = getattr_pattern.match(last_input)

m = setattr_pattern.match(last_input)
if m:
varname = m.group(1)
itemname = m.group(2)

if varname:
# otherwise it should have failed at this point, but let us be sure
if varname in clean_ns:
var = clean_ns[varname]
if _display_in_treewidget(varname, var):
# check if var is a dictionary or session
if isinstance(var, EXPANDABLE_OBJ):
if itemname in var.keys() and _display_in_grid(itemname, var[itemname]):
if '=' not in last_input:
self.select_array_item(itemname, varname)
else:
# force to update object
self.update_mapping(clean_ns, changed_expandable_obj_keys=[varname])
else:
self.update_mapping(clean_ns)
else:
# check if displayable in grid
if _display_in_grid(varname, var):
# XXX: this completely refreshes the array, including detecting scientific & ndigits,
# which might not be what we want in this case
self.select_array_item(varname)
else:
# maybe updating an existing array of a session/dict or
# adding a new array to the session/dict
self.update_mapping(clean_ns)
arrayname = m.group(2).replace('"', '').replace("'", '')
self.select_array_item(arrayname, varname)
else:
# not (get/set)(item/attribute) => assume expr or normal assignment
# not set(item/attribute) => assume expr or normal assignment
# any statement can contain a call to a function which updates globals
# this will select (or refresh) the "first" changed array
self.update_mapping(clean_ns)
Expand Down
65 changes: 65 additions & 0 deletions larray_editor/tests/test_regex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from larray_editor.editor import setitem_pattern, setattr_pattern


def test_setitem():
# new array
input = 'data = ndtest(10)'
m = setitem_pattern.match(input)
assert m is None

# update array
input = 'data[:] = 0'
varname, selection = setitem_pattern.match(input).groups()
assert varname == 'data'
assert selection == ':'

# testing array
input = 'data[2010:2012] == data2[2010:2012]'
m = setitem_pattern.match(input)
assert m is None

# session - new array
input = 'ses["data"] = ndtest(10)'
varname, selection = setitem_pattern.match(input).groups()
assert varname == 'ses'
assert selection == '"data"'

# session - update array
input = 'ses["data"][:] = 0'
varname, selection = setitem_pattern.match(input).groups()
assert varname == 'ses'
assert selection == '"data"'

# session - testing array
input = 'ses["data"] == ses2["data"]'
m = setitem_pattern.match(input)
assert m is None


def test_setattr():
# new array
input = 'data = ndtest(10)'
m = setattr_pattern.match(input)
assert m is None

# update array metadata
input = 'data.meta.title = "my array"'
m = setattr_pattern.match(input)
assert m is None

# session - new array
input = 'ses.data = ndtest(10)'
varname, attrname = setattr_pattern.match(input).groups()
assert varname == 'ses'
assert attrname == 'data'

# session - update array
input = 'ses.data[:] = 0'
varname, attrname = setattr_pattern.match(input).groups()
assert varname == 'ses'
assert attrname == 'data'

# session - update array metadata
input = 'ses.data.meta.title = "my array"'
m = setattr_pattern.match(input)
assert m is None