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


Groups > comp.lang.python > #99413

list slice and generators

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Pavlos Parissis <pavlos.parissis@gmail.com>
Newsgroups comp.lang.python
Subject list slice and generators
Date Tue, 24 Nov 2015 22:38:22 +0100
Lines 72
Message-ID <mailman.50.1448431814.20593.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="bjEfDi2Lv6fo1KgvlgmH80UHfkgbdSWs3"
X-Trace news.uni-berlin.de Zu9iYgMxlhGxGqiCDSXDmAyGHwqcY511Bd0xF2dUgnmQ==
Return-Path <pavlos.parissis@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'none)': 0.07; 'valueerror:': 0.07; "'0',": 0.09; 'metrics': 0.09; 'typeerror:': 0.09; 'bug': 0.10; 'def': 0.13; 'filename:fname piece:signature': 0.16; 'generators.': 0.16; 'received:192.168.0.103': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'twice.': 0.16; 'try:': 0.18; 'cheers,': 0.22; 'elements': 0.23; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'python2.7': 0.29; 'raise': 0.29; 'skip:[ 10': 0.31; 'problem': 0.33; 'message-id:@gmail.com': 0.34; 'except': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'replace': 0.35; 'received:74.125.82': 0.35; 'there': 0.36; 'possible': 0.36; 'skip:{ 10': 0.36; 'to:addr:python-list': 0.36; 'doing': 0.38; 'hi,': 0.38; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'avoid': 0.61; 'goal': 0.64; 'dangerous': 0.70
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type; bh=nFtg5QAGjfNhPhzrFF2bxLVpYn2KFnfyUQz4d08t1EQ=; b=uemuYXAd51oqjmOtUPtGIVPLgHDdzdYlhXsW1vWkzSQabLW1qkiRPpi6ziCtpq0/M7 1kAb2HXgOTVk1RxWKG25yHToFH8b38hDxOitezwJzBOSLNDp1FHe9112P+vqUtbU0LxS jLIHnCCOqZpGDynrc7kuxvLOeocELmC4bvaRLcWmCHXoqH45QCpnuLRFelqQY0cbeEja o9ZCO6dv2nZHlUH7SdDIX2rhxCk/dNvGWgH5rIkpxbUeGeaUEWoThaWeZpfJB+nefmUN 0kZ8Xq3s5NT858MSs8r/H61XktBgjT2m1eza7hME6bbUTPVtLLX1y999RW/sitpTPa/y xvCQ==
X-Received by 10.28.30.206 with SMTP id e197mr512137wme.97.1448401105229; Tue, 24 Nov 2015 13:38:25 -0800 (PST)
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0
X-Mailman-Approved-At Wed, 25 Nov 2015 01:10:13 -0500
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99413

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Hi,

Do you see any possible dangerous hidden bug in the below code(using
python2.7 and python3.4)?

My goal is to avoid go through the metrics list twice. But, I don't
know if there will be a problem with doing in place replace of list
elements using 2 generators.

# metrics = ['', '0', '10'....]
metrics = [x.metric(name) for x in self._server_per_proc]
metrics[:] = (converter(x) for x in metrics)
metrics[:] = (x for x in metrics if x is not None)

return calculate(name, metrics)


def calculate(name, metrics):
     if not metrics:
        return None

    if name in METRICS_SUM:
        return sum(metrics)
    elif name in METRICS_AVG:
        return int(sum(metrics)/len(metrics))
    else:
        raise ValueError("Unknown type of calculation for {}".format(name))


def converter(value):
    try:
        return int(float(value))
    except ValueError:
        return value.strip() or None
    except TypeError:
        return None


Cheers,
Pavlos

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

list slice and generators Pavlos Parissis <pavlos.parissis@gmail.com> - 2015-11-24 22:38 +0100

csiph-web