People who code: we want your input. Take the Survey

New answers tagged

1

OK. You still have undefined symbols but I'm going to push on. For your function signatures, consider PEP484 type hints. You've done this on, for example, get_max_terminal_width but not regex_2_find get_terminal_size returns a 2-tuple. Rather than [0], unpacking to columns, lines = get_terminal_size() is I think clearer. You've written word_wrap yourself; ...


2

Sample data vowels <- c("a", "e", "i", "o", "u") input <- c("aeiou", "uoiea", "aeiouc", "aeioo", "coeiauc1", "eu") Solution 1. Use reduce and & These create 5 vectors, but only have 2 loaded at a time. # 1A: with str_detect input[Reduce(...


2

Some minor tips: Add "^" and "$" to your regex to ensure your input string doesn't contain anything but a date No brackets are needed around the [M] - A simple M means the same thing Instead of checking for AM/PM with [A|P][M], keep the AM/PM characters together, like this: (AM|PM) - it makes it much more readable. Since it's impossible ...


4

This isn't so much a Python question as it is a RegEx question. The Python tag is almost irrelevant. '+' -vs- '*' You regex includes two alternates: (?:20\d\d#\d{1,3}(?:,\d{1,3})+) (?:20\d\d#\d{1,3}) The difference in these expressions is the first one include one or more repeats of ,\d{1,3}, where as the second does not include any repeats of that ... ......


1

Given that your focus is on readability and simplicity, rather than performance (which matters too), I would suggest using the netaddr module. Then all you need is something like: import netaddr if netaddr.valid_ipv4('192.168.0.1'): # do something However, it has to be installed, this is not a built-in package. The reason why I am mentioning this is ...


2

Return value The function is supposed to return a boolean, but it doesn't. It returns None or an exception. To avoid confusion, decide what to return and adjust the type hint accordingly. My suggestion is to return a boolean, as it is easier for the caller to deal with and avoids the additional argument expectFail. However, this might depend on your use case....


3

assert is for debugging/testing Don't use assert for runtime validation. Per the documentation, the assert statement assert expression is equivalent to: if __debug__: if not expression: raise AssertionError and if the interpreter is run with the -O flag, the assert statements are elided: ...the built-in variable debug is True under normal circumstances,...


4

As Linny noted, is_leading_zeros has an error because you want != intead of is not. My Python 3.8.2 actually says SyntaxWarning: "is not" with a literal. Did you mean "!="? on those two lines. But my bigger problem is with the name of the function: is_leading_zeros(s) is True when s doesn't have leading zeros (a docstring could have ...


5

When to use is not Your usage of is not is incorrect. You want to use == when you're testing equality, like when you check if s doesn't equal a '0'. You want to use is not when checking for identity, like when you're ensuring two variables are of the same object. def is_leading_zeros(s: str) -> bool: if s != '0': return s[0] != '0' return ...


Top 50 recent answers are included