Open mattip opened 3 months ago
For Path, there are a few different forms, the repr, the str and the printed version of the str with the escape characters processed.
from pathlib import Path
documents = Path.home()
repr(documents)
str(documents)
print(str(documents))
The repr has the advantage as it gives a clear indicator in how to instantiate a new Path.
However displaying the printed version of the str form, where the escape characters are processed has the advantage in that it matches what is typically displayed in the Operating System and is consistent to the file paths seen elsewhere in Spyder:
It would also be useful if the variable explorer supported editing a PathLib instance, similar to the way it does for a str instance. This would allow a path to be pasted into the field from Windows Explorer:
Extra points for doing the same with SimpleNamespace
Hi @mattip and @PhilipYip1988 thank you for the feedback and ideas! I think improving support for pathlib.Path
over the Variable Explorer makes sense :+1:
Checking I think there was a similar issue to this one (#12683). Maybe could it be worthy to address pathlib.Path
support although a more general approach to add support for custom types needs to be done (https://github.com/spyder-ide/spyder/issues/7092 and https://github.com/spyder-ide/spyder/issues/6338)? 🤔
What do you think @spyder-ide/core-developers ?
Hi @mattip and @PhilipYip1988 thank you for the feedback and ideas! I think improving support for
pathlib.Path
over the Variable Explorer makes sense 👍Checking I think there was a similar issue to this one (#12683). Maybe could it be worthy to address
pathlib.Path
support although a more general approach to add support for custom types needs to be done (#7092 and #6338)? 🤔What do you think @spyder-ide/core-developers ?
Not too sure how easy it is to implement, but perhaps Spyder can copy the behaviour of the JupyterLab Variable Inspector for datatypes that are not fully supported by Spyder? i.e. variables that can only be viewed and not explored/modified graphically.
The formal representation for datatypes is a bit more useful than "object or ... module."
For example some relatively common builtins classes frozenset and bytearray, the collections module classes and the datetime64 and timedelta64 classes from numpy:
from pathlib import Path
from collections import defaultdict, deque, Counter
from collections import namedtuple, UserString, UserDict, UserList
import numpy as np
unique = frozenset({1, 2, 3, 4, 5})
barray = bytearray(b'hello')
default = defaultdict(str, {'a': '', 'b': '', 'c': ''})
counts = Counter(('hello world!'))
double_ended_queue = deque([1, 2, 3, 4, 5], 7)
DateTuple = namedtuple('DateTuple', ['day', 'month', 'year'],
defaults=(26, 8, 2024))
day1 = DateTuple(day=29)
text = UserString('hello world!')
class CustomText(UserString):
def center_and_upper(self):
return self.upper().center(20, '#')
text2 = CustomText('hello')
text2.center_and_upper()
windows = Path(r'C:\Windows')
day2 = np.datetime64('2024-08-26')
timediff1 = np.timedelta64(12, 'h') + np.timedelta64(12, 's')
dates = np.arange(np.datetime64('2024-08-21'),
np.datetime64('2024-08-26'),
np.timedelta64(1, 'D'))
timediffs = np.arange(np.timedelta64(0, 'h'),
np.timedelta64(1, 'h'),
np.timedelta64(10, 's'))
In Spyder some of the collections datatypes can be viewed as they are essentially treated as a subclass of a builtins... For example a namedtuple displays as a tuple, and a Counter displays as a dict... So the expected behaviour is almost there for some of these classes...
An improvement would be for the namedtuple to display the repr on the Variable Explorer matching the JupyterLab Variable Inspector. The expanded view would ideally display the field names in the order specified beside the value for each field, if this class was even further supported. #20688
For the Counter, the repr should also be used on the Variable Explorer. The expanded view should list the data by value (highest to lowest). The expanded view for the dict and defaultdict should use the insertion order #9136.
The deque should display its repr on the Variable Explorer and if given extended support should display similarly to a list.
The frozenset should display its repr on the Variable Explorer and should display like a set in the expanded view. #21858.
Clicking into the datetime64 and timedelta64 instances shows a more sensible representation, which should probably be the default behaviour. The variable explorer does not support an nd.array of these datatypes however if these are cast into a pandas Series, then they display as expected... Not sure if the behaviour of these can be updated to match the pandas Series by default. #10423
Issue Report Checklist
conda update spyder
(orpip
, if not using Anaconda)jupyter qtconsole
(if console-related)spyder --reset
Problem Description
When I look at the value of a PathLib WindowsPath variable, it only shows "WindowsPath object of Pathlib moduled" and not the repr() of the pathlib
What steps reproduce the problem?
1.
What is the expected output? What do you see instead?
I would like to see the path
Paste Traceback/Error Below (if applicable)
Versions
Dependencies