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


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

Using tuple as parameter to a function

Started byCecil Westerhof <Cecil@decebal.nl>
First post2015-11-09 14:40 +0100
Last post2015-11-09 18:54 +0100
Articles 6 — 3 participants

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


Contents

  Using tuple as parameter to a function Cecil Westerhof <Cecil@decebal.nl> - 2015-11-09 14:40 +0100
    Re: Using tuple as parameter to a function Chris Angelico <rosuav@gmail.com> - 2015-11-10 00:58 +1100
      Re: Using tuple as parameter to a function marco.nawijn@colosso.nl - 2015-11-09 06:57 -0800
      Re: Using tuple as parameter to a function Cecil Westerhof <Cecil@decebal.nl> - 2015-11-09 16:01 +0100
        Re: Using tuple as parameter to a function Chris Angelico <rosuav@gmail.com> - 2015-11-10 02:24 +1100
          Re: Using tuple as parameter to a function Cecil Westerhof <Cecil@decebal.nl> - 2015-11-09 18:54 +0100

#98522 — Using tuple as parameter to a function

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-11-09 14:40 +0100
SubjectUsing tuple as parameter to a function
Message-ID<87io5b5m1b.fsf@Equus.decebal.nl>
At the moment I have the following calls:
    do_stress_test(  1, 100)
    do_stress_test(  2, 100)
    do_stress_test(  5, 100)
    do_stress_test( 10, 100)
    do_stress_test( 20, 100)
    do_stress_test( 40, 100)

In principal I want to change it to something like:
    do_stress_test('sqlite',    1, 100)
    do_stress_test('postgres',  1, 100)
    do_stress_test('sqlite',    2, 100)
    do_stress_test('postgres',  2, 100)
    do_stress_test('sqlite',    5, 100)
    do_stress_test('postgres',  5, 100)
    do_stress_test('sqlite',   10, 100)
    do_stress_test('postgres', 10, 100)
    do_stress_test('sqlite',   20, 100)
    do_stress_test('postgres', 20, 100)
    do_stress_test('sqlite',   40, 100)
    do_stress_test('postgres', 40, 100)

But that would not be very dry.

I was thinking about something like:
    values = (( 1, 100), ( 2, 100), ( 5, 100),
               10, 100), (20, 100), (40, 100))
    for value in values:
        do_stress_test('sqlite',   ???)
        do_stress_test('postgres', ???)

Is this possible? If so: what do I put at the place of the '???'?

I could change the second and third parameter to a tuple as the second
parameter, but I prefer three parameters if that would be possible.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [next] | [standalone]


#98524

FromChris Angelico <rosuav@gmail.com>
Date2015-11-10 00:58 +1100
Message-ID<mailman.175.1447077487.16136.python-list@python.org>
In reply to#98522
On Tue, Nov 10, 2015 at 12:40 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
> I was thinking about something like:
>     values = (( 1, 100), ( 2, 100), ( 5, 100),
>                10, 100), (20, 100), (40, 100))
>     for value in values:
>         do_stress_test('sqlite',   ???)
>         do_stress_test('postgres', ???)
>
> Is this possible? If so: what do I put at the place of the '???'?
>
> I could change the second and third parameter to a tuple as the second
> parameter, but I prefer three parameters if that would be possible.

Easy! Just unpack the tuple. Two options:

# Unpack in the loop
    for count, size in values:
        do_stress_test('sqlite', count, size)
        do_stress_test('postgres', count, size)

# Unpack in the function call
    for value in values:
        do_stress_test('sqlite', *value)
        do_stress_test('postgres', *value)

Either will work. For what you're doing here, I'd be inclined to the
first option, so you can give the values appropriate names (I'm
completely guessing here that they might be some sort of iteration
count and pool size; use names that make sense to your program); the
other option looks uglier in this particular instance, though it's a
more direct answer to your question.

This is one of Python's best-kept secrets, I think. It's not easy to
stumble on it, but it's so handy once you know about it.

ChrisA

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


#98530

Frommarco.nawijn@colosso.nl
Date2015-11-09 06:57 -0800
Message-ID<aca9a260-6c2c-43f6-a7f7-6dee71eb5f51@googlegroups.com>
In reply to#98524
On Monday, November 9, 2015 at 2:58:21 PM UTC+1, Chris Angelico wrote:
> On Tue, Nov 10, 2015 at 12:40 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
> > I was thinking about something like:
> >     values = (( 1, 100), ( 2, 100), ( 5, 100),
> >                10, 100), (20, 100), (40, 100))
> >     for value in values:
> >         do_stress_test('sqlite',   ???)
> >         do_stress_test('postgres', ???)
> >
> > Is this possible? If so: what do I put at the place of the '???'?
> >
> > I could change the second and third parameter to a tuple as the second
> > parameter, but I prefer three parameters if that would be possible.
> 
> Easy! Just unpack the tuple. Two options:
> 
> # Unpack in the loop
>     for count, size in values:
>         do_stress_test('sqlite', count, size)
>         do_stress_test('postgres', count, size)
> 
> # Unpack in the function call
>     for value in values:
>         do_stress_test('sqlite', *value)
>         do_stress_test('postgres', *value)
> 
> Either will work. For what you're doing here, I'd be inclined to the
> first option, so you can give the values appropriate names (I'm
> completely guessing here that they might be some sort of iteration
> count and pool size; use names that make sense to your program); the
> other option looks uglier in this particular instance, though it's a
> more direct answer to your question.
> 
> This is one of Python's best-kept secrets, I think. It's not easy to
> stumble on it, but it's so handy once you know about it.
> 
> ChrisA

If the two numbers are actually conceptually connected, you could
consider passing them as a namedtuple. The function would then 
receive two parameters instead of three. Something like the
following (expanding on ChrisA's example):

from collections import namedtuple

StressParameters = namedtuple('StressParameters', ('count', 'size'))

def do_stress_test(db_backend, stress_parameters):
   # Some useful stuff...
   count = stress_parameters.count
   size = stress_parameters.size
   # More useful stuff

parameters = [StressParameters(1, 100), StressParameters(2,100)]

for p in parameters:
    do_stress_test('sqlite', p)
    do_stress_test('postgres', p)

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


#98534

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-11-09 16:01 +0100
Message-ID<87egfz5i9x.fsf@Equus.decebal.nl>
In reply to#98524
On Monday  9 Nov 2015 14:58 CET, Chris Angelico wrote:

> On Tue, Nov 10, 2015 at 12:40 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>> I was thinking about something like:
>> values = (( 1, 100), ( 2, 100), ( 5, 100),
>> 10, 100), (20, 100), (40, 100))
>> for value in values:
>> do_stress_test('sqlite',   ???)
>> do_stress_test('postgres', ???)
>>
>> Is this possible? If so: what do I put at the place of the '???'?
>>
>> I could change the second and third parameter to a tuple as the
>> second parameter, but I prefer three parameters if that would be
>> possible.
>
> Easy! Just unpack the tuple. Two options:
>
> # Unpack in the loop
> for count, size in values:
> do_stress_test('sqlite', count, size)
> do_stress_test('postgres', count, size)
>
> # Unpack in the function call
> for value in values:
> do_stress_test('sqlite', *value)
> do_stress_test('postgres', *value)
>
> Either will work. For what you're doing here, I'd be inclined to the
> first option, so you can give the values appropriate names (I'm
> completely guessing here that they might be some sort of iteration
> count and pool size; use names that make sense to your program); the
> other option looks uglier in this particular instance, though it's a
> more direct answer to your question.
>
> This is one of Python's best-kept secrets, I think. It's not easy to
> stumble on it, but it's so handy once you know about it.

I remembered the second one, but did not know how it was done. That
was why I asked about it. But the first option is even better. I now
use:
    values = ((  1, 100), (  2, 100), (  5, 100),
              ( 10, 100), ( 20, 100), ( 40, 100),
              #( 80, 100), (160, 100),
              #(160, 200), (160, 400), (160, 800),
          )
    for no_of_threads, no_of_records in values:
        do_stress_test('postgres', no_of_threads, no_of_records)
        do_stress_test('sqlite',   no_of_threads, no_of_records)

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

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


#98536

FromChris Angelico <rosuav@gmail.com>
Date2015-11-10 02:24 +1100
Message-ID<mailman.180.1447082701.16136.python-list@python.org>
In reply to#98534
On Tue, Nov 10, 2015 at 2:01 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>     for no_of_threads, no_of_records in values:

Beautiful! Though I'd shorten the names to just "threads" and
"records"; saying "number of X" isn't usually necessary, plus, the
general principle of Huffman coding your names recommends using
something shorter for a short-lived iteration variable. But otherwise,
that's exactly what I'd be looking at - descriptive and clear.

ChrisA

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


#98547

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-11-09 18:54 +0100
Message-ID<874mgv5a99.fsf@Equus.decebal.nl>
In reply to#98536
On Monday  9 Nov 2015 16:24 CET, Chris Angelico wrote:

> On Tue, Nov 10, 2015 at 2:01 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>> for no_of_threads, no_of_records in values:
>
> Beautiful! Though I'd shorten the names to just "threads" and
> "records"; saying "number of X" isn't usually necessary, plus, the
> general principle of Huffman coding your names recommends using
> something shorter for a short-lived iteration variable.

Done.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [standalone]


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


csiph-web