You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@subversion.apache.org by Max Bowsher <ma...@ukf.net> on 2003/08/23 10:43:22 UTC

[PATCH] [cvs2svn] Code cleanup - Use "for: ... else: ..." to avoid a temporary variable

* tools/cvs2svn/cvs2svn.py (SymbolicNameTracker.bump_rev_count):
    Use "for: ... else: ..." construct to render temporary variable
unnecessary.

Index: cvs2svn.py
===================================================================
--- cvs2svn.py (revision 6839)
+++ cvs2svn.py (working copy)
@@ -1310,20 +1310,17 @@
       entry_val[revlist_key] = [(rev, 1)]
     else:
       rev_counts = entry_val[revlist_key]
-      done = None
       for i in range(len(rev_counts)):
         this_rev, this_count = rev_counts[i]
         if rev == this_rev:
           rev_counts[i] = (this_rev, this_count + 1)
-          done = 1
           break
         elif this_rev > rev:
           if i > 0:
             i = i - 1
           rev_counts.insert(i, (rev, 1))
-          done = 1
           break
-      if not done:
+      else:
         rev_counts.append((rev, 1))
       entry_val[revlist_key] = rev_counts



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] [cvs2svn] Code cleanup - Use "for: ... else: ..." to avoid a temporary variable

Posted by Max Bowsher <ma...@ukf.net>.
mark benedetto king wrote:
> On Sat, Aug 23, 2003 at 07:03:09PM +0100, Max Bowsher wrote:
>>> Doesn't this change the semantics of the block?
>> 
>> No - The behaviour of "for: ... else: ..." is to execute the "else:"
>> unless the loop was exited by "break".
>> 
> 
> Ah.  That explains it.  I had jumped to the (wrong) conclusion that
> the else clause was for empty lists.

Understandable. It's not a very obviously named feature.
It helps to think of "for" as "search-for" and "break" as "found-it".

Max.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] [cvs2svn] Code cleanup - Use "for: ... else: ..." to avoid a temporary variable

Posted by mark benedetto king <mb...@lowlatency.com>.
On Sat, Aug 23, 2003 at 07:03:09PM +0100, Max Bowsher wrote:
> > Doesn't this change the semantics of the block?
> 
> No - The behaviour of "for: ... else: ..." is to execute the "else:" unless
> the loop was exited by "break".
> 

Ah.  That explains it.  I had jumped to the (wrong) conclusion that the
else clause was for empty lists.

--ben


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] [cvs2svn] Code cleanup - Use "for: ... else: ..." to avoid a temporary variable

Posted by Max Bowsher <ma...@ukf.net>.
mark benedetto king wrote:
> On Sat, Aug 23, 2003 at 11:43:22AM +0100, Max Bowsher wrote:
>> * tools/cvs2svn/cvs2svn.py (SymbolicNameTracker.bump_rev_count):
>>     Use "for: ... else: ..." construct to render temporary variable
>> unnecessary.
>>
>> Index: cvs2svn.py
>> ===================================================================
>> --- cvs2svn.py (revision 6839)
>> +++ cvs2svn.py (working copy)
>> @@ -1310,20 +1310,17 @@
>>        entry_val[revlist_key] = [(rev, 1)]
>>      else:
>>        rev_counts = entry_val[revlist_key]
>> -      done = None
>>        for i in range(len(rev_counts)):
>>          this_rev, this_count = rev_counts[i]
>>          if rev == this_rev:
>>            rev_counts[i] = (this_rev, this_count + 1)
>> -          done = 1
>>            break
>>          elif this_rev > rev:
>>            if i > 0:
>>              i = i - 1
>>            rev_counts.insert(i, (rev, 1))
>> -          done = 1
>>            break
>> -      if not done:
>> +      else:
>>          rev_counts.append((rev, 1))
>>        entry_val[revlist_key] = rev_counts
>>
>
> Doesn't this change the semantics of the block?

No - The behaviour of "for: ... else: ..." is to execute the "else:" unless
the loop was exited by "break".

>
> For example, what if rev_counts contains only pairs of the form (x,y)
> where x < rev?

Then the loop terminates by exhaustion of things to loop over, and executes
the "else:" clause.

Max.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] [cvs2svn] Code cleanup - Use "for: ... else: ..." to avoid a temporary variable

Posted by mark benedetto king <mb...@lowlatency.com>.
On Sat, Aug 23, 2003 at 11:43:22AM +0100, Max Bowsher wrote:
> * tools/cvs2svn/cvs2svn.py (SymbolicNameTracker.bump_rev_count):
>     Use "for: ... else: ..." construct to render temporary variable
> unnecessary.
> 
> Index: cvs2svn.py
> ===================================================================
> --- cvs2svn.py (revision 6839)
> +++ cvs2svn.py (working copy)
> @@ -1310,20 +1310,17 @@
>        entry_val[revlist_key] = [(rev, 1)]
>      else:
>        rev_counts = entry_val[revlist_key]
> -      done = None
>        for i in range(len(rev_counts)):
>          this_rev, this_count = rev_counts[i]
>          if rev == this_rev:
>            rev_counts[i] = (this_rev, this_count + 1)
> -          done = 1
>            break
>          elif this_rev > rev:
>            if i > 0:
>              i = i - 1
>            rev_counts.insert(i, (rev, 1))
> -          done = 1
>            break
> -      if not done:
> +      else:
>          rev_counts.append((rev, 1))
>        entry_val[revlist_key] = rev_counts
> 

Doesn't this change the semantics of the block?

For example, what if rev_counts contains only pairs of the form (x,y)
where x < rev?

--ben


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org