Re: Mental model of lookahead assertions

2020-02-28 Thread Jon Ribbens via Python-list
On 2020-02-27, Stefan Ram wrote: > One can count overlapping occurences as follows. > >|>>> print(len(findall('(?=aa)','cb'))) >|3 > > Every web page says that lookahead assertions do > not consume nor move the "current positio

Re: Mental model of lookahead assertions

2020-02-27 Thread Python
Stefan Ram wrote: One can count overlapping occurences as follows. |>>> print(len(findall('(?=aa)','cb'))) |3 Every web page says that lookahead assertions do not consume nor move the "current position". But what mental model can I make o

Re: Assertions

2017-09-22 Thread jladasky
On Thursday, September 21, 2017 at 9:29:19 AM UTC-7, Tobiah wrote: > Are these completely equivalent? > > def foo(thing): > > assert(thing > 0), "Thing must be greater than zero" > > > def foo(thing): > > if not (thing > 0): raise AssertionError("Thing must be greater than > z

Re: Assertions

2017-09-22 Thread Stephan Houben
Op 2017-09-22, Thomas Jollans schreef : > Just to make the implication explicit: > from math import nan nan is nan > True nan == nan > False nan != nan > True To add to the fun: >>> nan is nan True Stephan -- https://mail.python.org/mailman/listinfo/python-list

Re: Assertions

2017-09-22 Thread Thomas Jollans
On 2017-09-22 14:43, Steve D'Aprano wrote: > In the case of floating point NANs, they are unordered with respect to all > numbers. So for any number x, we always have: > > NAN == x > NAN < x > NAN > x > NAN <= x > NAN >= x > > all return False, and > > NAN != x > > return True. Just to make t

Re: Assertions

2017-09-22 Thread Steve D'Aprano
On Fri, 22 Sep 2017 10:03 pm, alister wrote: > On Fri, 22 Sep 2017 21:15:54 +1000, Steve D'Aprano wrote: > >> On Fri, 22 Sep 2017 08:50 pm, alister wrote: >> The bottom line is, if I saw if not (thing > 0): raise AssertionError(...) in a code review, I'd probably insis

Re: Assertions

2017-09-22 Thread Thomas Jollans
On 2017-09-22 14:03, alister via Python-list wrote: > On Fri, 22 Sep 2017 21:15:54 +1000, Steve D'Aprano wrote: >> On Fri, 22 Sep 2017 08:50 pm, alister wrote: >>> [snip] >>> >>> In a code review I would want the condition changed to be less noisy/ >>> confusing to the reader. >>> >>> if thing <=0:

Re: Assertions

2017-09-22 Thread alister via Python-list
On Fri, 22 Sep 2017 21:15:54 +1000, Steve D'Aprano wrote: > On Fri, 22 Sep 2017 08:50 pm, alister wrote: > >>> The bottom line is, if I saw >>> >>> if not (thing > 0): raise AssertionError(...) >>> >>> in a code review, I'd probably insist that either it be changed to use >>> `assert`, >>> or t

Re: Assertions

2017-09-22 Thread Steve D'Aprano
On Fri, 22 Sep 2017 08:50 pm, alister wrote: >> The bottom line is, if I saw >> >> if not (thing > 0): raise AssertionError(...) >> >> in a code review, I'd probably insist that either it be changed to use >> `assert`, >> or the exception be changed to ValueError, whichever better expresses >> t

Re: Assertions

2017-09-22 Thread alister via Python-list
e an assertion, because it > uses the assert keyword! > > The second is testing a condition, and if need be, raising an exception. > Which looks like an inappropriate exception. Why AssertionError? It > looks like it ought to be a ValueError. > > Assertions aren'

Re: Assertions

2017-09-21 Thread Chris Angelico
On Fri, Sep 22, 2017 at 3:49 AM, Steve D'Aprano wrote: > On Fri, 22 Sep 2017 03:31 am, Chris Angelico wrote: > >> Impressive. That means that, in 2.7, it's actually equivalent to: >> > def test3(): >> ... if not foo: raise AssertionError, "bar baz" > > That's nothing. In 1.5 (yes, *one* po

Re: Assertions

2017-09-21 Thread Steve D'Aprano
On Fri, 22 Sep 2017 03:31 am, Chris Angelico wrote: > On Fri, Sep 22, 2017 at 3:23 AM, Steve D'Aprano > wrote: >> That is definitely version-dependent, because I've just tried it and got >> different byte-code in Python 2.7. >> >> py> import dis >> py> def test1(): >> ... assert foo, "bar baz

Re: Assertions

2017-09-21 Thread Steve D'Aprano
ers, then I think we should say that the two above are very different. The first is an actual assertion. It has to be an assertion, because it uses the assert keyword! The second is testing a condition, and if need be, raising an exception. Which looks like an inappropriate exception. Why Asser

Re: Assertions

2017-09-21 Thread Chris Angelico
On Fri, Sep 22, 2017 at 3:23 AM, Steve D'Aprano wrote: > That is definitely version-dependent, because I've just tried it and got > different byte-code in Python 2.7. > > py> import dis > py> def test1(): > ... assert foo, "bar baz" > ... > py> def test2(): > ... if not foo: raise Assertio

Re: Assertions

2017-09-21 Thread Steve D'Aprano
On Fri, 22 Sep 2017 02:59 am, Ned Batchelder wrote: > On 9/21/17 12:29 PM, Tobiah wrote: >> Are these completely equivalent? [... assert, versus test and raise AssertionError ...] > Let's see: [...] > Yes, they are completely equivalent, compiling to precisely the same > bytecode. That is defin

Re: Assertions

2017-09-21 Thread Ned Batchelder
On 9/21/17 12:29 PM, Tobiah wrote: Are these completely equivalent? def foo(thing): assert(thing > 0), "Thing must be greater than zero" def foo(thing): if not (thing > 0): raise AssertionError("Thing must be greater than zero") Other than the fact that the assertion can

Assertions

2017-09-21 Thread Tobiah
Are these completely equivalent? def foo(thing): assert(thing > 0), "Thing must be greater than zero" def foo(thing): if not (thing > 0): raise AssertionError("Thing must be greater than zero") Other than the fact that the assertion can be turned off with -O? Thanks, Tob

Re: Assertions are bad, m'kay?

2014-03-08 Thread Steven D'Aprano
On Fri, 07 Mar 2014 16:15:36 -0800, Dan Stromberg wrote: > On Fri, Mar 7, 2014 at 3:11 AM, Steven D'Aprano > wrote: > > >> Assertions are not bad! They're just misunderstood and abused. > >> You should read this guy's blog post on when to use asser

Re: Assertions are bad, m'kay?

2014-03-07 Thread Irmen de Jong
On 8-3-2014 1:15, Dan Stromberg wrote: > On Fri, Mar 7, 2014 at 3:11 AM, Steven D'Aprano > wrote: > >> >> Assertions are not bad! They're just misunderstood and abused. > >> You should read this guy's blog post on when to use assert: >> >

Re: Assertions are bad, m'kay?

2014-03-07 Thread Ben Finney
Dan Stromberg writes: > BTW, what about: > > if value >= 3: >raise AssertionError('value must be >= 3') That would be very confusing, since it would only appear when the value is >= 3. Were you making some other point? -- \“If this is your first visit to the USSR, you are welcome

Re: Assertions are bad, m'kay?

2014-03-07 Thread Dan Stromberg
On Fri, Mar 7, 2014 at 3:11 AM, Steven D'Aprano wrote: > > Assertions are not bad! They're just misunderstood and abused. > You should read this guy's blog post on when to use assert: > > http://import-that.dreamwidth.org/676.html Nice article. BTW, what a

Re: Assertions are bad, m'kay?

2014-03-07 Thread Chris Angelico
On Fri, Mar 7, 2014 at 10:11 PM, Steven D'Aprano wrote: >> http://xkcd.com/1339/ >> >> Abusing assert for arg checking violates XKCD 1339. Write >> standards-compliant code! > > Assertions are not bad! They're just misunderstood and abused. > &g

Re: Assertions are bad, m'kay?

2014-03-07 Thread Steven D'Aprano
Write > standards-compliant code! Assertions are not bad! They're just misunderstood and abused. (By the way, assertions are not the same as assumptions. Asserts can be used to check that assumptions are correct, or to check the internal logic of your reasoning. Whereas assumptions ar

Assertions are bad, m'kay?

2014-03-06 Thread Chris Angelico
They produce the wrong exception type, they disappear when you least expect them, and now we have another reason not to use assert. http://xkcd.com/1339/ Abusing assert for arg checking violates XKCD 1339. Write standards-compliant code! ChrisA -- https://mail.python.org/mailman/listinfo/python

Re: Repeating assertions in regular expression

2012-01-03 Thread Devin Jeanpierre
> Put simply, it doesn't occur often enough to be worth it. The cost > outweighs the potential benefit. I don't buy it. You could backtrack instead of failing for \b+ and \b*, and it would be almost as fast as this optimization. -- Devin On Tue, Jan 3, 2012 at 1:57 PM, MRAB wrote: > On 03/01/20

Re: Repeating assertions in regular expression

2012-01-03 Thread MRAB
On 03/01/2012 09:45, Devin Jeanpierre wrote: \\b\\b and \\b{2} aren't equivalent ? This sounds suspiciously like a bug! Why the wording is "should never" ? Repeating a zero-width assertion is not forbidden, for instance : import re re.compile("\\b\\b\w+\\b\\b") <_sre.SRE_Pattern obje

Re: Repeating assertions in regular expression

2012-01-03 Thread Devin Jeanpierre
46 AM, candide wrote: > The regular expression HOWTO > (http://docs.python.org/howto/regex.html#more-metacharacters) explains the > following > > # -- > zero-width assertions should never be repeated, because if they match once > at a given

Repeating assertions in regular expression

2012-01-03 Thread candide
The regular expression HOWTO (http://docs.python.org/howto/regex.html#more-metacharacters) explains the following # -- zero-width assertions should never be repeated, because if they match once at a given location, they can obviously be matched an infinite number

Re: how to make all assertions in a unit test execute

2010-10-16 Thread Ben Finney
Terry Reedy writes: > The temptation to write unfactored duplicate code like this is a > negative of unittest. I don't see that it's especially attributable to the ‘unittest’ module. It's a common problem to be solved, and solutions are available. > The question is whether get_filenames gets th

Re: how to make all assertions in a unit test execute

2010-10-16 Thread Terry Reedy
nerstr) self.assertEquals(3,len(matching_names)) A side point unrelated to the question: your code will be easier to read if you follow PEP 8, specifically its recommendations regarding spaces around syntax (like ‘=’ and ‘,’). when I run the unittest, if the test fails at the firs

Re: how to make all assertions in a unit test execute

2010-10-16 Thread Ben Finney
Ben Finney writes: > Yes, that's by design. Each function in a TestCase subclass is a test > case […] Poorly described. Try this instead: Each test case in a TestCase subclass must be a separate function, whose name starts with ‘test’. Or you could see what the documentation says; it may descri

Re: how to make all assertions in a unit test execute

2010-10-16 Thread Ben Finney
mes(self.dirname,innerstr) > self.assertEquals(3,len(matching_names)) A side point unrelated to the question: your code will be easier to read if you follow PEP 8, specifically its recommendations regarding spaces around syntax (like ‘=’ and ‘,’). > when I run the unittest, if the tes

Re: how to make all assertions in a unit test execute

2010-10-16 Thread jimgardener
thanks Peter that was good advice jim On Oct 16, 1:44 pm, Peter Otten <__pete...@web.de> wrote: > Keep it simple ;) -- http://mail.python.org/mailman/listinfo/python-list

Re: how to make all assertions in a unit test execute

2010-10-16 Thread Peter Otten
a' > when I run the unittest, if the test fails at the first assertion,the > other assertions are not executed at all..How do I organize the test > so that all assertions can run? Keep it simple ;) def test_get_filenames_single_match(self): > innerstr='tom&#

how to make all assertions in a unit test execute

2010-10-16 Thread jimgardener
'jerry' matching_names=get_filenames(self.dirname,innerstr) self.assertEquals(3,len(matching_names)) when I run the unittest, if the test fails at the first assertion,the other assertions are not executed at all..How do I organize the test so that all assertions can run? thank

Re: Your impression of for-novice writings on assertions

2010-02-07 Thread David
ote: I've started on ch 3 of my beginner's intro to programming, now delving into the details of the Python language. It's just a few pages yet, file [03 asd.pdf] (no real title yet!) at http://tinyurl.com/programmingbookP3> which is at Google Docs. The first topic is about asserti

Re: Your impression of for-novice writings on assertions

2010-02-02 Thread Peter
l.com/programmingbookP3> which is at Google Docs. > > The first topic is about assertions and exceptions. I wonder whether this text > is easy or difficult to understand for a beginner. Or any improvements that > could be made. > > Cheers, > > - Alf The only iss

Re: Your impression of for-novice writings on assertions

2010-02-02 Thread Steve Holden
is at Google Docs. > > The first topic is about assertions and exceptions. I wonder whether > this text is easy or difficult to understand for a beginner. Or any > improvements that could be made. > I don't think it's helpful in Python to refer to "... variables, which

Re: Your impression of for-novice writings on assertions

2010-02-02 Thread Irmen de Jong
On 2-2-2010 21:54, Alf P. Steinbach wrote: I've started on ch 3 of my beginner's intro to programming, now delving into the details of the Python language. Alf, I think it's a good read so far. I just don't like the smilies that occur in the text. It's a book (or article) that I'm reading, no

Re: Your impression of for-novice writings on assertions

2010-02-02 Thread Raymond Hettinger
On Feb 2, 12:54 pm, "Alf P. Steinbach" wrote: > The first topic is about assertions and exceptions. I wonder whether this text > is easy or difficult to understand for a beginner. Or any improvements that > could be made. To my eyes it reads nicely. You may want to try it out

Your impression of for-novice writings on assertions

2010-02-02 Thread Alf P. Steinbach
I've started on ch 3 of my beginner's intro to programming, now delving into the details of the Python language. It's just a few pages yet, file [03 asd.pdf] (no real title yet!) at http://tinyurl.com/programmingbookP3> which is at Google Docs. The first topic is ab

Re: Assertions, challenges, and polite discourse

2010-01-06 Thread r0g
alex23 wrote: > r0g wrote: >> Well I think sometimes, for the sake of expediency and overall >> pleasantness, it's better to let the smaller things go: and if you just >> can't let them go then at least try and issue corrections in a friendly >> manner rather than a cold or pious one. > > The iro

Re: Assertions, challenges, and polite discourse

2010-01-06 Thread alex23
r0g wrote: > Well I think sometimes, for the sake of expediency and overall > pleasantness, it's better to let the smaller things go: and if you just > can't let them go then at least try and issue corrections in a friendly > manner rather than a cold or pious one. The irony, it is too rich... --

Re: Assertions, challenges, and polite discourse

2010-01-06 Thread r0g
Ben Finney wrote: > r0g writes: > >> Ben Finney wrote: >>> People sometimes get upset — on an immediate, irrational level — >>> when their assertions are challenged. There's no denying that >>> emotions entangle our discourse, and our interpretati

Re: Assertions, challenges, and polite discourse

2010-01-06 Thread Ben Finney
r0g writes: > Ben Finney wrote: > > People sometimes get upset — on an immediate, irrational level — > > when their assertions are challenged. There's no denying that > > emotions entangle our discourse, and our interpretation of the > > discourse of others. &

Re: Assertions, challenges, and polite discourse

2010-01-06 Thread r0g
why the assertion was not true). > That's a fair point I had never really considered. I'd wouldn't have been nearly as upset if you'd started your sentence with AssertionError :) > People sometimes get upset — on an immediate, irrational level — when > their ass

Assertions, challenges, and polite discourse (was: Exception as the primary error handling mechanism?)

2010-01-06 Thread Ben Finney
t question, but to an assertion. Every assertion expressed, though, implies the question “is this assertion true?”. It was that question that was answered “No” (followed by an explanation of why the assertion was not true). People sometimes get upset — on an immediate, irrational level — when thei

Re: python regex "negative lookahead assertions" problems

2009-11-23 Thread Helmut Jarausch
On 11/22/09 16:05, Helmut Jarausch wrote: On 11/22/09 14:58, Jelle Smet wrote: Hi List, I'm trying to match lines in python using the re module. The end goal is to have a regex which enables me to skip lines which have ok and warning in it. But for some reason I can't get negative lookaheads wo

Re: python regex "negative lookahead assertions" problems

2009-11-22 Thread MRAB
Tim Chase wrote: import re line='2009-11-22 12:15:441 lmqkjsfmlqshvquhsudfhqf qlsfh qsduidfhqlsiufh qlsiuf qldsfhqlsifhqlius dfh warning qlsfj lqshf lqsuhf lqksjfhqisudfh qiusdfhq iusfh' re.match('.*(?!warning)',line) <_sre.SRE_Match object at 0xb75b1598> I would expect that this would NOT

Re: python regex "negative lookahead assertions" problems

2009-11-22 Thread Helmut Jarausch
On 11/22/09 14:58, Jelle Smet wrote: Hi List, I'm trying to match lines in python using the re module. The end goal is to have a regex which enables me to skip lines which have ok and warning in it. But for some reason I can't get negative lookaheads working, the way it's explained in "http://

Re: python regex "negative lookahead assertions" problems

2009-11-22 Thread Tim Chase
import re line='2009-11-22 12:15:441 lmqkjsfmlqshvquhsudfhqf qlsfh qsduidfhqlsiufh qlsiuf qldsfhqlsifhqlius dfh warning qlsfj lqshf lqsuhf lqksjfhqisudfh qiusdfhq iusfh' re.match('.*(?!warning)',line) <_sre.SRE_Match object at 0xb75b1598> I would expect that this would NOT match as it's a neg

python regex "negative lookahead assertions" problems

2009-11-22 Thread Jelle Smet
Hi List, I'm trying to match lines in python using the re module. The end goal is to have a regex which enables me to skip lines which have ok and warning in it. But for some reason I can't get negative lookaheads working, the way it's explained in "http://docs.python.org/library/re.html";. Con

Re: assertions to validate function parameters

2007-01-27 Thread Steven D'Aprano
> >> debugging. >> >> > If it helps go ahead an use them. The world won't end if you use an >> > assertion in a less than ideal situation. And, after all, if someone >> > doesn't like it they can shut them off. >> >> Is there any way

Re: assertions to validate function parameters

2007-01-27 Thread Carl Banks
them. The world won't end if you use an > > assertion in a less than ideal situation. And, after all, if someone > > doesn't like it they can shut them off. > > Is there any way to have finer control of assertions than just passing -O > to the Python interpreter? Suppose I w

Re: assertions to validate function parameters

2007-01-27 Thread Steven D'Aprano
And, after all, if someone > doesn't like it they can shut them off. Is there any way to have finer control of assertions than just passing -O to the Python interpreter? Suppose I want to switch them off for certain modules but not others, am I out of luck? -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: assertions to validate function parameters

2007-01-27 Thread Carl Banks
; parameters. > > I'm curious how this does or doesn't fit into python's duck-typing > philosophy. The duck-typing thing fits into a wider philosophy of being liberal in what you accept. As you're constraining what a function accepts, it definitely goes against the

Re: assertions to validate function parameters

2007-01-27 Thread Carl Banks
ecent call last): > File "", line 1, in ? > ValueError: x must be equal to three but is 1 instead These are are verbose to the point of silliness, and usually not worth the effort since assertions are only supposed to check for ostensibly impossible conditions. Thus it shoul

Re: assertions to validate function parameters

2007-01-27 Thread Nick Craig-Wood
Matthew Woodcraft <[EMAIL PROTECTED]> wrote: > I have a question for you. Consider this function: > > def f(n): > """Return the largest natural power of 2 which does not exceed n.""" > if n < 1: > raise ValueError > i = 1 > while i <= n: > j = i > i

Re: assertions to validate function parameters

2007-01-26 Thread Steven D'Aprano
On Fri, 26 Jan 2007 18:28:32 +, Matthew Woodcraft wrote: > I have a question for you. Consider this function: > > def f(n): > """Return the largest natural power of 2 which does not exceed n.""" > if n < 1: > raise ValueError > i = 1 > while i <= n: > j = i >

Re: assertions to validate function parameters

2007-01-26 Thread Matthew Woodcraft
Steven D'Aprano <[EMAIL PROTECTED]> wrote: > The less your function does, the more constrained it is, the less > testing you have to do -- but the less useful it is, and the more work > you put onto the users of your function. Instead of saying something > like > a = MyNumericClass(1) > b = MyNum

Re: assertions to validate function parameters

2007-01-25 Thread Steven D'Aprano
On Thu, 25 Jan 2007 16:54:05 +, Matthew Wilson wrote: > Lately, I've been writing functions like this: > > def f(a, b): > > assert a in [1, 2, 3] > assert b in [4, 5, 6] > > The point is that I'm checking the type and the values of the > parameters. If somebody passes in a == MyNum

Re: assertions to validate function parameters

2007-01-25 Thread George Sakkis
service, etc.) or is it to be called only by you in a very controlled fashion ? In the first case, passing an invalid input is a *user error*, not a *programming error*. Assertions should be used for programming errors only to assert invariants, statements which should be correct no matter what

assertions to validate function parameters

2007-01-25 Thread Matthew Wilson
Lately, I've been writing functions like this: def f(a, b): assert a in [1, 2, 3] assert b in [4, 5, 6] The point is that I'm checking the type and the values of the parameters. I'm curious how this does or doesn't fit into python's duck-typing philosophy. I find that when I detect inv