-
Notifications
You must be signed in to change notification settings - Fork 146
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
Issue #249 - Add strip to get() and getall() #260
base: master
Are you sure you want to change the base?
Changes from all commits
fbdb881
cbc6e89
02b09b0
4185e19
ff23fdb
37dfd36
cbe7042
43ba89f
7ce8789
b044b76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,30 +200,36 @@ def re_first( | |
return el | ||
return default | ||
|
||
def getall(self) -> List[str]: | ||
def getall(self, *, strip: bool = False) -> List[str]: | ||
""" | ||
Call the ``.get()`` method for each element is this list and return | ||
their results flattened, as a list of strings. | ||
""" | ||
return [x.get() for x in self] | ||
data = [x.get() for x in self] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can just pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. x is a Selector, at that point neither default nor strip exist anymore There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. About adding the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Aren’t you also adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That ended up looking harder than it should when I tried. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you are changing Selector.get() to have Also if you have issues with typing I can hep, I'm working on improving parsel typing right now.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, the purpose of those overloads is to show that the type of the return value depends on whether
I believe just adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also, it seems a little unintuitive to be using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, you are right.
Yeah, it's probably unusual to write
Not so sure about default=, but maybe there are use cases where it's useful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can keep this as is, strip for Selector.get() is less useful and can be implemented separately. |
||
if strip: | ||
return [x.strip() if x else x for x in data] | ||
return data | ||
|
||
extract = getall | ||
|
||
@typing.overload | ||
def get(self, default: None = None) -> Optional[str]: | ||
def get(self, default: None = None, strip: bool = ...) -> Optional[str]: | ||
pass | ||
|
||
@typing.overload | ||
def get(self, default: str) -> str: | ||
def get(self, default: str, strip: bool = ...) -> str: | ||
pass | ||
|
||
def get(self, default: Optional[str] = None) -> Optional[str]: | ||
def get( | ||
self, default: Optional[str] = None, strip: bool = False | ||
) -> Optional[str]: | ||
""" | ||
Return the result of ``.get()`` for the first element in this list. | ||
If the list is empty, return the default value. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new option needs to be mentioned in the docstring |
||
""" | ||
for x in self: | ||
return x.get() | ||
value = x.get() | ||
return value.strip() if strip and value else value | ||
return default | ||
|
||
extract_first = get | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the new option needs to be mentioned in the docstring.