Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-27 Thread Edward K. Ream
On Sun, Nov 27, 2016 at 7:34 AM, rengel  wrote:

If you change 's.splitlines(True)'  to 's.splitlines(False) in g.
> ​splitLines​
> , your interface to g doesn't change at all.
>

​This change would be catastrophic.

If you change how lines are split, then you have to change how lines are
joined. That happens in gazillions of places.

EKR

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-27 Thread rengel
​True, but now I *do* understand why I am *not *going to make the changes!

>
> As elegant as changes the changes seem, my background mind has been 
> *screaming* at me that they are a bad, bad, bad idea.
>
> I inserted a new_lines switch in linescanner.py and actually starting 
> converting the code to the new way. And then, suddenly, I *did* 
> understand why my background mind was screaming.
>
> Using s.splitlines forks Leo's code. You could call it a hidden mode bit. 
> All future maintainers would have to *remember* that the importers *must* 
> use s.splitlines instead of g.splitLines.
>
> Sure, the new code i
> ​s ​
> better
> ​ locally​
> . But
> ​ global​
> consistency is 
> ​*way* ​
> more important.
> ​ Besides, the proposed changes have no practical impact, whereas effort 
> required to change and test the code are substantial.
>
> I've spent decades using g.splitLines.  It's *everywhere* in Leo's code. 
> ​I'm not about to start using two different conventions for lists of 
> lines.​
>
>
> It *might* have been been better to have used s.splitllines
> ​ instead of g.splitLines.  But no, s.splitlines's bool argument is 
> *another* mode bit. That bool would again fork pieces of Leo's code.
>
> *Summary*
>
> Leo's code handles lists of lines consistently everywhere. That should 
> remain a strict principle, especially for major components such as Leo's 
> importers.
>
> We'll just have to live with whatever slight inconveniences the global 
> consistency entails.
>
> It's ok to use s.splitlines in local situations, provided that doing so 
> has no global impacts.
>
> EKR
>


I don't see why you have to break the global consistency. If you change 
's.splitlines(True)'  to 's.splitlines(False) in g.splitLine, your 
interface to g doesn't change at all. The change would be strictly 
localized (as it should be). You don't need no mode switches for code 
forks. All the strip() commands that are scattered around the code, don't 
change their results, though they might be now superfluous. And you get the 
advantage that new code gets easier to write because you don't have to 
strip the newlines anymore. Of course this only holds if you don't need the 
newline characters somewhere, what I don't know.

Reinhard



-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-27 Thread rengel


On Sunday, November 27, 2016 at 12:33:36 PM UTC+1, Edward K. Ream wrote:
>
> On Sun, Nov 27, 2016 at 5:21 AM, rengel  > wrote:
> ​
>
> v._bodyString = '\n'.join(v._import_lines) + '\n'
>>
>
> No. It inserts a newline when v._import_lines is [].
>
> EKR
>

Well, this is a little longer, but still eliminates the comprehension and 
possibly hundreds of string additions.
 
v._bodyString = ('\n'.join(v._import_lines) + '\n') if v._import_lines else 
''


Reinhard



Enter code here...

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-27 Thread Edward K. Ream
​​On Sun, Nov 27, 2016 at 4:15 AM, Edward K. Ream 
wrote:

On Sat, Nov 26, 2016 at 12:50 PM, rengel 
> wrote:
>
> >
> ​ ​
> I still don't understand why you are struggling with the newline
> characters at all.
>
> ​Many thanks for this. You have uncovered a blind spot in my thinking.
>
​​
​True, but now I *do* understand why I am *not *going to make the changes!

As elegant as changes the changes seem, my background mind has been
*screaming* at me that they are a bad, bad, bad idea.

I inserted a new_lines switch in linescanner.py and actually starting
converting the code to the new way. And then, suddenly, I *did* understand
why my background mind was screaming.

Using s.splitlines forks Leo's code. You could call it a hidden mode bit.
All future maintainers would have to *remember* that the importers *must*
use s.splitlines instead of g.splitLines.

Sure, the new code i
​s ​
better
​ locally​
. But
​ global​
consistency is
​*way* ​
more important.
​ Besides, the proposed changes have no practical impact, whereas effort
required to change and test the code are substantial.

I've spent decades using g.splitLines.  It's *everywhere* in Leo's code.
​I'm not about to start using two different conventions for lists of lines.​


It *might* have been been better to have used s.splitllines
​ instead of g.splitLines.  But no, s.splitlines's bool argument is
*another* mode bit. That bool would again fork pieces of Leo's code.

*Summary*

Leo's code handles lists of lines consistently everywhere. That should
remain a strict principle, especially for major components such as Leo's
importers.

We'll just have to live with whatever slight inconveniences the global
consistency entails.

It's ok to use s.splitlines in local situations, provided that doing so has
no global impacts.

EKR

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-27 Thread Edward K. Ream
On Sun, Nov 27, 2016 at 5:21 AM, rengel  wrote:
​

v._bodyString = '\n'.join(v._import_lines) + '\n'
>

No. It inserts a newline when v._import_lines is [].

EKR

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-27 Thread rengel
That's the only per-importer change required! The code we have been 
discussing appears in the base Importer class. i.finalize_ivars, also in 
the base class, will restore trailing newlines like this:

>
> v._bodyString = ''.join[z+'\n' for z in v._import_lines]
>
> This is itself simpler than the existing code.
>
> May I suggest:

 
v._bodyString = '\n'.join(v._import_lines) + '\n'

This uses the C-code of Python and contains only one string addition.

Reinhard


-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-27 Thread Edward K. Ream
On Sat, Nov 26, 2016 at 12:50 PM, rengel 
wrote:

​> ​
I still don't understand why you are struggling with the newline characters
at all.

​Many thanks for this. You have uncovered a blind spot in my thinking.

So instead of clumsily preserving newlines *that it knows must end every
line*, each importer will simply remove newlines with s.splitlines(False).

That's the only per-importer change required! The code we have been
discussing appears in the base Importer class. i.finalize_ivars, also in
the base class, will restore trailing newlines like this:

v._bodyString = ''.join[z+'\n' for z in v._import_lines]

This is itself simpler than the existing code.

This is way too good to miss. Because the changes affect the base Importer
class, all new importers must be changed simultaneously.

Existing unit tests should catch any mistakes. To be doubly careful, I may
add a g.new switch to mark changed code. Maybe today...

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-26 Thread rengel
In short, we are getting to matters of style.  Imo, this:

>
>> return [self.lstrip_line(z) for z in lines]
>>
>> is slightly preferable to:
>>
>> return ['\n' if z.isspace() else z.lstrip() for z in lines]
>>
>>
> Furthermore, i.clean_blank_lines() uses i.lstrip_line():
>
> def clean_blank_lines(self, lines):
> '''Remove all blanks and tabs in all blank lines.'''
> return [self.lstrip_line(z) if z.isspace() else z for z in lines]
>
> Yes, we could replace the call to self.lstrip_line, but that would create 
> nested ternary operators, which I avoid on stylistic grounds.
>
>
I still don't understand why you are struggling with the newline characters 
at all. Do you need them for some internal processing? Otherwise the 
command 
lines = s.splitlines(False)
(s being the input string) gets rid of all trailing newline characters; and 
you can use lstrip() to do all the cleanup of the strings. You no longer 
need isspace or any case distictions to cleanup the lines:
def clean_blank_lines(self, lines):
'''Remove all blanks and tabs in all blank lines.'''
return [z.lstrip() for z in lines]
Or more concisely: You can skip the creation of the lines and do it all in 
one sweep:
def clean_blank_lines(self, s):
'''Remove all blanks and tabs in all blank lines.'''
assert s, g.callers()
return [z.lstrip() for z in s.splitlines(False)]

Reinhard
 

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-26 Thread Edward K. Ream
On Saturday, November 26, 2016 at 8:04:20 AM UTC-6, Edward K. Ream wrote:
 
>
> In short, we are getting to matters of style.  Imo, this:
>
> return [self.lstrip_line(z) for z in lines]
>
> is slightly preferable to:
>
> return ['\n' if z.isspace() else z.lstrip() for z in lines]
>
>
Furthermore, i.clean_blank_lines() uses i.lstrip_line():

def clean_blank_lines(self, lines):
'''Remove all blanks and tabs in all blank lines.'''
return [self.lstrip_line(z) if z.isspace() else z for z in lines]

Yes, we could replace the call to self.lstrip_line, but that would create 
nested ternary operators, which I avoid on stylistic grounds.

EKR

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-26 Thread Edward K. Ream
​​
On Sat, Nov 26, 2016 at 5:40 AM, Edward K. Ream  wrote:

> On Friday, November 25, 2016 at 7:59:56 AM UTC-6, rengel wrote:
>
>> Why use two functions, when one suffices?
>>
>> def strip_lws(s):
>>return '\n'.join([z.lstrip() for z in s.splitlines(False)])
>>
>
​> ​Beautiful.  I'm going to make this change today.

All conclusions ​are provisional pending unit tests, which just failed ;-)
The present definitions, which do work, are:

def strip_lws(self, lines):
'''Strip leading whitespace from all lines.'''
return [self.lstrip_line(z) for z in lines]

def lstrip_line(self, s):
'''
Delete leading whitespace, *without* deleting
the trailing newline!
'''
# This fixes a major bug in strip_lws.
assert s, g.callers()
return '\n' if s.isspace() else s.lstrip()

The following does *not* work:

def strip_lws(self, lines):
'''Strip leading whitespace from all lines.'''
return [z.lstrip() for z in lines]

but it could be recast:

def strip_lws(self, lines):
'''Strip leading whitespace from all lines.'''
return ['\n' if s.isspace() else s.lstrip() for z in lines]

And this has just passed all importer unit tests.  In short, we are getting
to matters of style.  Imo, this:

return [self.lstrip_line(z) for z in lines]

is slightly preferable to:

return ['\n' if z.isspace() else z.lstrip() for z in lines]


precisely *because* an "extra" method is involved.  The method's name
clarifies the code, and the method itself provides a place to put asserts
and traces, should they ever be needed.

Aside from eliminating gross performance bugs, performance differences
matter not at all.

Reinhard, this has been a great discussion. Seemingly minor differences and
distinctions are the foundation of big collapses in complexity.

EKR

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-26 Thread Edward K. Ream

On Saturday, November 26, 2016 at 5:40:22 AM UTC-6, Edward K. Ream wrote:

> Provided all tests pass, it's probably safe to define g.splitLines as:

def splitLines(s):
assert g.isString(s), (repr(s), g.callers())
return s.splitLines()

Oops.  The last line must be s.splitlines(True).

However, I'm not in a great hurry to make this change ;-)

EKR

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-26 Thread Edward K. Ream
On Friday, November 25, 2016 at 7:59:56 AM UTC-6, rengel wrote:

> Why use two functions, when one suffices? 
>
> def strip_lws(s):
>return '\n'.join([z.lstrip() for z in s.splitlines(False)])
>

Beautiful.  I'm going to make this change today.

The Importer class uses i.lstrip_line() in one other place:

def clean_blank_lines(self, lines):
'''Remove all blanks and tabs in all blank lines.'''
return [self.lstrip_line(z) if z.isspace() else z for z in lines]

But this could also be rewritten, asserting that every line ends in '\n':

def clean_blank_lines(self, lines):
'''Remove all blanks and tabs in all blank lines.'''
assert all([z.endswith('\n') for z in lines]), g.callers()
return ['\n' if z.isspace() else z for z in lines]

Leo uses g.splitLines(s) rather than s.splitlines(True).  Three characters 
shorter ;-)

Somehow I had it in my head they are different...

def splitLines(s):
if s:
return s.splitlines(True)
else:
return []

But they are identical, unless s isn't a string, in which case g.splitLines 
shouldn't be called!

Provided all tests pass, it's probably safe to define g.splitLines as:

def splitLines(s):
assert g.isString(s), (repr(s), g.callers())
return s.splitLines()

Thanks, Reinhard, for educating me about s.isspace() and s.splitlines().

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-25 Thread rengel


On Thursday, November 24, 2016 at 3:07:21 PM UTC+1, Edward K. Ream wrote:
>
> On Saturday, November 12, 2016 at 12:32:55 PM UTC-6, Terry Brown wrote:
>>
>> That tripped me up too, empty strings are OK if you're using 'not 
>> s.isspace()'.
>>
>
> ...
 

> The bug in i.check was serious.  In essence, it caused i.check to ignored 
> all blank lines!  And it masked a similar bug in the xml importer.
>
> While making i.check line-oriented (more about this in another post) I 
> came across this code:
>
> def strip_lws(self, lines):
>  '''Strip leading whitespace from all lines of s.'''
>  return ''.join([z.lstrip() for z in g.splitLines(s)])
>
> Because of our conversation here, I realized that this was wrong, wrong, 
> wrong. Thank you, Terry and Reinhard, for burning this issue into my 
> consciousness!
>
> Eventually, the code got replaced with:
>
> def strip_lws(self, lines):
> '''Strip leading whitespace from all lines of s.'''
> return [self.lstrip_line(z) for z in lines]
>
> All this goes to show that the i.check's helpers should be covered by unit 
> tests!
>
> Why use two functions, when one suffices? 

def strip_lws(s):
   return '\n'.join([z.lstrip() for z in s.splitlines(False)])

Just remove the lineends when splitting the string and add it in when 
rejoining them.



-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-24 Thread Edward K. Ream
On Saturday, November 12, 2016 at 12:32:55 PM UTC-6, Terry Brown wrote:
>
> That tripped me up too, empty strings are OK if you're using 'not 
> s.isspace()'.
>

It works both ways.  s.lstrip() can be wrong, wrong, wrong, as this 
morning's bug fix in i.check shows.

The new i.lstrip_line helper gets it right:

def lstrip_line(self, s):
'''Delete leading whitespace, *without* deleting the trailing 
newline!'''
# This fixes a major bug in i.strip_lws.
assert s, g.callers()
return '\n' if s.isspace() else s.lstrip()

The bug in i.check was serious.  In essence, it caused i.check to ignored 
all blank lines!  And it masked a similar bug in the xml importer.

While making i.check line-oriented (more about this in another post) I came 
across this code:

def strip_lws(self, lines):
 '''Strip leading whitespace from all lines of s.'''
 return ''.join([z.lstrip() for z in g.splitLines(s)])

Because of our conversation here, I realized that this was wrong, wrong, 
wrong. Thank you, Terry and Reinhard, for burning this issue into my 
consciousness!

Eventually, the code got replaced with:

def strip_lws(self, lines):
'''Strip leading whitespace from all lines of s.'''
return [self.lstrip_line(z) for z in lines]

All this goes to show that the i.check's helpers should be covered by unit 
tests!

Edward

>

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-13 Thread Edward K. Ream
On Sat, Nov 12, 2016 at 8:12 PM, Edward K. Ream  wrote:

> On Sat, Nov 12, 2016 at 11:10 AM, rengel 
> wrote:
>
>>
>> The code 'z.isspace()' fails if a line is completely empty.
>>
>
> ​Happily, all lines (except possibly the last) end with a newline :-)
>

​More to the point, no line, including the last line, is ever empty.

EKR

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-12 Thread Edward K. Ream
On Sat, Nov 12, 2016 at 11:10 AM, rengel 
wrote:

>
> The code 'z.isspace()' fails if a line is completely empty.
>

​Happily, all lines (except possibly the last) end with a newline :-)

EKR

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-12 Thread 'Terry Brown' via leo-editor
That tripped me up too, empty strings are OK if you're using 'not s.isspace()'.
Cheers -Terry

On November 12, 2016 11:10:50 AM CST, rengel  
wrote:
> 
>
>> Instead of testing:
>>>
>>
>>if not ''.join(lines).strip():
>>
>> the new code now tests:
>>
>> if all([z.isspace() for z in lines]):
>>  
>>
>What do you want to accomplish?
>The code 'z.isspace()' fails if a line is completely empty. Test:
>
>print(''.isspace()) # Fails
>>> print(' '.isspace())
>>> print('   '.isspace())
>>>
>>>
>#!/usr/bin/python
># -*- coding: UTF-8 -*-
>
>import re
>
># A test string, several blank lines
>s1 = """
>
>   abc   
>   
>   """
>
>s2 = """
>
> 
>
>   """
>
># Show the strings s1, s2
>print(s1, len(s1), s1.count('\n'))
>print(s2, len(s2), s2.count('\n'))
>
># Create lines from s1, s2
>lines1 = s1.split('\n')
>lines2 = s2.split('\n')
>
># Show the lines
>print(lines1)
>print(lines2)
>
># EKR test, fails if a line is empty (= contains only '\n')
>print(all([z.isspace() for z in lines1]))
>print(all([z.isspace() for z in lines2]))
>
># Using regular expression
>print(re.match('^\s*$(?s)', s1))
>print(re.match('^\s*$(?s)', s2))
>
>
>
>-- 
>You received this message because you are subscribed to the Google
>Groups "leo-editor" group.
>To unsubscribe from this group and stop receiving emails from it, send
>an email to leo-editor+unsubscr...@googlegroups.com.
>To post to this group, send email to leo-editor@googlegroups.com.
>Visit this group at https://groups.google.com/group/leo-editor.
>For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-12 Thread rengel
 

> Instead of testing:
>>
>
>if not ''.join(lines).strip():
>
> the new code now tests:
>
> if all([z.isspace() for z in lines]):
>  
>
What do you want to accomplish?
The code 'z.isspace()' fails if a line is completely empty. Test:

print(''.isspace()) # Fails
>> print(' '.isspace())
>> print('   '.isspace())
>>
>>
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import re

# A test string, several blank lines
s1 = """

   abc   
   
   """

s2 = """

 

   """

# Show the strings s1, s2
print(s1, len(s1), s1.count('\n'))
print(s2, len(s2), s2.count('\n'))

# Create lines from s1, s2
lines1 = s1.split('\n')
lines2 = s2.split('\n')

# Show the lines
print(lines1)
print(lines2)

# EKR test, fails if a line is empty (= contains only '\n')
print(all([z.isspace() for z in lines1]))
print(all([z.isspace() for z in lines2]))

# Using regular expression
print(re.match('^\s*$(?s)', s1))
print(re.match('^\s*$(?s)', s2))



-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-12 Thread Edward K. Ream

On Friday, November 11, 2016 at 10:46:12 AM UTC-6, Terry Brown wrote:

> As another aspect of this pattern, instead of testing, 
> > 
> >if ''.join(lines): 
> > 
> > the new code now tests: 
> > 
> > if all([z.isspace() for z in lines]): 
>
> Those don't seem equivalent


Oops.  I meant the following:

Instead of testing:

   if not ''.join(lines).strip():

the new code now tests:

if all([z.isspace() for z in lines]):

This makes the gain in efficiency all the clearer. The former generates 
*two* new strings which the GC must then deal with.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-11 Thread 'Terry Brown' via leo-editor
On Fri, 11 Nov 2016 08:12:57 -0800 (PST)
"Edward K. Ream"  wrote:

> On Thursday, November 10, 2016 at 12:48:28 PM UTC-6, Edward K. Ream
> wrote:
> >
> > Up until today, I have always tested for an empty string using not 
> > s.strip().  But Doh, this is an unnecessary stress on the GC.  
> > `s.isspace()` much faster and more pythonic.
> >  
> 
> As another aspect of this pattern, instead of testing,
> 
>if ''.join(lines):
> 
> the new code now tests:
> 
> if all([z.isspace() for z in lines]):

Those don't seem equivalent - the first is true if any of lines is
anything other than "", the second only if all lines are whitespace,
and none are "".  But I suspect these were typed into email, rather
than copy pasted from code?

Cheers -Terry

> This is much faster (it should run close to C speed) and generates
> *no* additional strings for the GC to recycle.
> 
> EKR
> 
> 

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


Re: A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-11 Thread Edward K. Ream
On Thursday, November 10, 2016 at 12:48:28 PM UTC-6, Edward K. Ream wrote:
>
> Up until today, I have always tested for an empty string using not 
> s.strip().  But Doh, this is an unnecessary stress on the GC.  
> `s.isspace()` much faster and more pythonic.
>

As another aspect of this pattern, instead of testing,

   if ''.join(lines):

the new code now tests:

if all([z.isspace() for z in lines]):

This is much faster (it should run close to C speed) and generates *no* 
additional strings for the GC to recycle.

EKR


-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.


A new pattern for me: `s.isspace()` instead of `not s.strip()`

2016-11-10 Thread Edward K. Ream
Up until today, I have always tested for an empty string using not 
s.strip().  But Doh, this is an unnecessary stress on the GC.  
`s.isspace()` much faster and more pythonic.

Rev 2de4669c replaces `(id).strip()` with `not id.isspace()` everywhere in 
the Import class, removing `not not` in two or three places.
 
This regex makes the substitution safely:

  Find:   ([\w_\.]+)\.strip\(\)
  To:  not \1.isspace()

At one point I thought I had found a bug, but "correcting" it caused a unit 
test to fail. Indeed, i.post_pass moves *whitespace* from p to back. A new 
comment should avoid future confusion.  All unit tests pass again.

It would be suicidal to attempt to make this changes in Leo's core--Leo's 
unit tests are not nearly good enough to catch blunders.  However, this is 
an important change to make for the importers, and now is the time to make 
it while everything is in flux.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.