Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80382 > unrolled thread

How do i reduce this to a single function - the code is largely similar, just a direction of search toggle.

Started byVeek M <vek.m1234@gmail.com>
First post2014-11-07 16:46 +0630
Last post2014-11-07 15:18 +0000
Articles 8 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Veek M <vek.m1234@gmail.com> - 2014-11-07 16:46 +0630
    Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-07 16:18 -0500
    Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-07 22:22 +0000
    Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Chris Angelico <rosuav@gmail.com> - 2014-11-08 09:31 +1100
    Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Veek M <vek.m1234@gmail.com> - 2014-11-07 21:22 +0630
      Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Ned Batchelder <ned@nedbatchelder.com> - 2014-11-08 07:56 -0500
        Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Veek M <vek.m1234@gmail.com> - 2014-11-09 15:15 +0630
    Re: How do i reduce this to a single function - the code is largely similar, just a direction of search toggle. Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-07 15:18 +0000

#80382 — How do i reduce this to a single function - the code is largely similar, just a direction of search toggle.

FromVeek M <vek.m1234@gmail.com>
Date2014-11-07 16:46 +0630
SubjectHow do i reduce this to a single function - the code is largely similar, just a direction of search toggle.
Message-ID<m3i9lp$8fp$1@dont-email.me>
    def jump_to_blockD(self):
        end = len(self.b)
        row, col = self.w.cursor
        while row <= end:
            try:
                new_col = self.b[row].index('def')
                self.w.cursor = row, new_col
                break
            except ValueError:
                pass
            row += 1

    def jump_to_blockU(self):
        end = 0
        row, col = self.w.cursor
        while row >= end:
            try:
                new_col = self.b[row].rindex('def')
                self.w.cursor = row, new_col
                break
            except ValueError:
                pass
            row -= 1


[toc] | [next] | [standalone]


#80384

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2014-11-07 16:18 -0500
Message-ID<mailman.15576.1415395125.18130.python-list@python.org>
In reply to#80382
On Fri, Nov 7, 2014 at 5:16 AM, Veek M <vek.m1234@gmail.com> wrote:
>     def jump_to_blockD(self):
>         end = len(self.b)
>         row, col = self.w.cursor
>         while row <= end:
>             try:
>                 new_col = self.b[row].index('def')
>                 self.w.cursor = row, new_col
>                 break
>             except ValueError:
>                 pass
>             row += 1
>
>     def jump_to_blockU(self):
>         end = 0
>         row, col = self.w.cursor
>         while row >= end:
>             try:
>                 new_col = self.b[row].rindex('def')
>                 self.w.cursor = row, new_col
>                 break
>             except ValueError:
>                 pass
>             row -= 1
>
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list

add a direction parameter to the call, and test direction to change
the while test, and row increment/decrement

-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#80385

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-11-07 22:22 +0000
Message-ID<m3jgnn$s06$2@dont-email.me>
In reply to#80382
On Fri, 07 Nov 2014 21:22:22 +0630, Veek M wrote:

> Veek M wrote:
> 
> 
>>                 new_col = self.b[row].index('def') self.w.cursor = row,
>>                 new_col
> 
>>                 new_col = self.b[row].rindex('def')
>>                 self.w.cursor = row, new_col
> 
> There's also the different methods index vs rindex.

yes, those fall under point 2 of my earlier post. "something" and 
"something else" would be the complete means of calculating the value to 
assign to variable.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#80386

FromChris Angelico <rosuav@gmail.com>
Date2014-11-08 09:31 +1100
Message-ID<mailman.15577.1415399494.18130.python-list@python.org>
In reply to#80382
On Fri, Nov 7, 2014 at 9:16 PM, Veek M <vek.m1234@gmail.com> wrote:
>     def jump_to_blockD(self):
>         end = len(self.b)
>         row, col = self.w.cursor
>         while row <= end:
>             try:
>                 new_col = self.b[row].index('def')
>                 self.w.cursor = row, new_col
>                 break
>             except ValueError:
>                 pass
>             row += 1
>
>     def jump_to_blockU(self):
>         end = 0
>         row, col = self.w.cursor
>         while row >= end:
>             try:
>                 new_col = self.b[row].rindex('def')
>                 self.w.cursor = row, new_col
>                 break
>             except ValueError:
>                 pass
>             row -= 1

Start by translating this into for loops, rather than while, and then
it'll be much easier to put all the differences in the signature. Tell
me if this translation is faithful:

    def jump_to_blockD(self):
        for row in range(self.w.cursor[0], len(self.b)+1, 1):
            try:
                new_col = self.b[row].index('def')
                self.w.cursor = row, new_col
                break
            except ValueError:
                pass

    def jump_to_blockU(self):
        for row in range(self.w.cursor[0], -1, -1):
            try:
                new_col = self.b[row].rindex('def')
                self.w.cursor = row, new_col
                break
            except ValueError:
                pass

Try those two, see if they function the same as your original code. If
they do, you should be able to figure out how to merge them, in their
new form.

ChrisA

[toc] | [prev] | [next] | [standalone]


#80392

FromVeek M <vek.m1234@gmail.com>
Date2014-11-07 21:22 +0630
Message-ID<m3iprd$8aq$1@dont-email.me>
In reply to#80382
Veek M wrote:


>                 new_col = self.b[row].index('def')
>                 self.w.cursor = row, new_col

>                 new_col = self.b[row].rindex('def')
>                 self.w.cursor = row, new_col

There's also the different methods index vs rindex. Does this sort of thing 
justify two functions and associated infrastructure (it's for vim so 2 x 3 
mode lines in my .vimrc)

[toc] | [prev] | [next] | [standalone]


#80398

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-11-08 07:56 -0500
Message-ID<mailman.15581.1415451417.18130.python-list@python.org>
In reply to#80392
On 11/7/14 9:52 AM, Veek M wrote:
> Veek M wrote:
>
>
>>                  new_col = self.b[row].index('def')
>>                  self.w.cursor = row, new_col
>
>>                  new_col = self.b[row].rindex('def')
>>                  self.w.cursor = row, new_col
>
> There's also the different methods index vs rindex. Does this sort of thing
> justify two functions and associated infrastructure (it's for vim so 2 x 3
> mode lines in my .vimrc)
>

Stepping back a bit from the lines of code, why do you need to use 
.index in one case and .rindex in the other?  You are trying to find the 
"def" line in the editor buffer, either above the current point, or 
below it.  But the "def" will always be the first non-whitespace text on 
the line.   In fact, when searching upward, you could find this line:

     def get_default(self):

and you want to end up on the "def" token, not the "def" in 
"get_default".  .rindex isn't what you want in any case.  Use .index in 
both cases.

-- 
Ned Batchelder, http://nedbatchelder.com

[toc] | [prev] | [next] | [standalone]


#80412

FromVeek M <vek.m1234@gmail.com>
Date2014-11-09 15:15 +0630
Message-ID<m3nd2o$thl$1@dont-email.me>
In reply to#80398
Ned Batchelder wrote:

> On 11/7/14 9:52 AM, Veek M wrote:
> and you want to end up on the "def" token, not the "def" in

yep, bumped into this :) thanks!

[toc] | [prev] | [next] | [standalone]


#80393

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-11-07 15:18 +0000
Message-ID<m3insb$s06$1@dont-email.me>
In reply to#80382
On Fri, 07 Nov 2014 16:46:19 +0630, Veek M wrote:

(1) Pass a true or false parameter to the function as the direction of 
search toggle.

(2) replace the relevant assignments with something like:

variable = something if condition else something else

(3) Figuring out the while loop control is a bit trickier, the best I 
came up with was:

while direction and condition_a or (not direction) and condition_b

But I'm sure someone has something better

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web