#๐Ÿ”’ Using logic to replace value

56 messages ยท Page 1 of 1 (latest)

silver forge
#
import re

raw_data = "John Doe"

results = lambda: re.search("^[A-Z][a-z]+ [A-Z][a-z]+$", raw_data)
invalid = [None]

name = (results() or invalid)[0]
print(name)

Ye or nay?

honest pumiceBOT
#

@silver forge

Python help channel opened

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.

twin laurel
#

!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]
honest pumiceBOT
twin laurel
#

what do you want to do?

edgy jay
#

!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)
honest pumiceBOT
twin laurel
#

That actually sounds reasonable

#

though if you have more than 1 group (or named groups), it's going to be a pain

silver forge
#

Sorry, fixed it above.

#

I was thinking it balances readability and laziness.

twin laurel
#

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)

silver forge
#

So overall, readable?

twin laurel
#

Yeah I would say it's good

silver forge
#

I would use it in a function but whatever.

twin laurel
#

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

silver forge
#
"" or None
None and ""

Also is doing stuff like this strange?

twin laurel
#

that's totally fine

#

though I don't remember seeing and used like that

silver forge
#

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.

edgy jay
#

(None and "") == ""

silver forge
#

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-

honest pumiceBOT
#

aiohttp/multipart.py line 137

encoding = encoding or "utf-8"```
silver forge
#

I've never seen it done before outside my work. XD

twin laurel
silver forge
#

I actually hate it.

#

XD

#

I need to learn more about it though.

twin laurel
#

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)

silver forge
#

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.

twin laurel
#

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)
honest pumiceBOT
silver forge
#

Another shortcut I didn't know python had: :=.

#

Python seems to have ^ but not xor.

#

Unless it has another word for it.

twin laurel
#

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

silver forge
twin laurel
#

!e

def foo():
    print("foo")
    return 5

def bar():
    print("bar")
    return 6

x = foo() or bar()
print(x)
honest pumiceBOT
honest pumiceBOT
#
Python help channel closed

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.