#๐ Using logic to replace value
56 messages ยท Page 1 of 1 (latest)
@silver forge
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
!e
Depends on what you mean by ye and nay
import re
raw_data = "John Doe"
results = lambda: re.search("^[A-Z][a-z]+$", raw_data)
invalid = [None]
name = (results or invalid)[0]
:x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 8, in <module>
003 | name = (results or invalid)[0]
004 | ~~~~~~~~~~~~~~~~~~~~^^^
005 | TypeError: 'function' object is not subscriptable
what do you want to do?
!e
import re
raw_data = "John Doe"
results = lambda: re.search("^[A-Z][a-z]+$", raw_data)
invalid = [None]
name = (results() or invalid)[0]
print(name)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
None
That actually sounds reasonable
though if you have more than 1 group (or named groups), it's going to be a pain
I kinda wish re returned this kind of object instead of None ```py
class NullMatch:
def getitem(self, idx):
return None
def groupdict(self):
return {}
...
def __bool__(self):
return False
@wraith skiff you win this time
(he likes null objects, but I haven't found them useful before, probably)
So overall, readable?
Yeah I would say it's good
I would use it in a function but whatever.
if you had more than 1 group or had named groups, maybe a simple None check would be better (or a custom object like the above if you have this pattern appear a lot)
actually, in that case defaultdict(lambda: None) could work
"" or None
None and ""
Also is doing stuff like this strange?
Because never seen anyone use it but me.
When I was in college my professor ignored it and I never got to ask, so I kept using it.
(None and "") == ""
I can't remember some of the other weird habits I had, but it's probably specific to C++.
I had some strange ones, but no one every said anything. -shrug-
It's commonly used when you want to replace None with a different kind of default
https://github.com/aio-libs/aiohttp/blob/98eec45100822cc1092b7ea1fc9f734912cd2c82/aiohttp/multipart.py#L137
aiohttp/multipart.py line 137
encoding = encoding or "utf-8"```
I've never seen it done before outside my work. XD
Since you seem to like regexes, maybe check out https://grep.app/ ๐
You can make a query like https://grep.app/search?q= or &case=true&filter[lang][0]=Python and see how something is used in popular projects
For example, you can skip all matches for "express or implied" and "or agreed to in writing" with a pattern like or ([^ia]|i[^m][^p]|a[^g][^r])
the website is using a pretty restrictive regex flavour (for speed), but in Python flavour you'd do [ ]or[ ](?!implied)(?!agreed to)
Reading regex makes me want to break things.
Keeps things short, but it's annoying to read.
I use it and think, "okay it's done, let's forget about it".
It is what it is and I get to be lazy, kinda.
I think it's good if you don't overuse the more advanced parts, and recognize some similarities (like how (?!), (?<), (?!<), (?P<foo>) all share the (?...) shape)
Also you can make them verbose
!e
import re
pat = re.compile(r"""
\sor\s
(?!implied) # skip MIT License
(?!agreed to) # skip Apache 2.0 License
""", re.VERBOSE)
print(pat)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
re.compile('\n\\sor\\s\n(?!implied) # skip MIT License\n(?!agreed to) # skip Apache 2.0 License\n', re.VERBOSE)
Another shortcut I didn't know python had: :=.
Python seems to have ^ but not xor.
Unless it has another word for it.
well, a xor b is just bool(a) != bool(b)
or and and are useful because they're lazy, but xor can't be made lazy
What do you mean it can't be made easy? They have ^, but not in the form of xor.
or and and short-circuit if the result is already known
!e
def foo():
print("foo")
return 5
def bar():
print("bar")
return 6
x = foo() or bar()
print(x)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | foo
002 | 5
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.