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


Groups > comp.lang.python > #54902

Re: Hostrange expansion

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"""': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'i.e.,': 0.16; 'itertools': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'started:': 0.16; 'wrote:': 0.18; 'library': 0.18; '>>>': 0.22; 'import': 0.22; 'accepted.': 0.22; 'header:User-Agent:1': 0.23; 'expanded': 0.24; 'header:X -Complaints-To:1': 0.27; 'expanding': 0.29; 'dom': 0.31; 'me?': 0.32; 'there': 0.35; 'in:': 0.36; 'should': 0.36; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'range': 0.61; 'name': 0.63; 'sam': 0.68; 'containing': 0.69; 'skip:r 30': 0.69
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Hostrange expansion
Date Fri, 27 Sep 2013 19:45:16 +0200
Organization None
References <CAC97zZ88AbQ497=uK0qG5NTPbQEfBXfmKVGFtEJHs_FDa=bFqw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b06e.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.388.1380303869.18130.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380303869 news.xs4all.nl 15964 [2001:888:2000:d::a6]:41848
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54902

Show key headers only | View raw


Sam Giraffe wrote:

> I need some help in expanding a hostrange as in: h[1-100].domain.com
> should get expanded into a list containing h1.domain.com to
> h100.domain.com. Is there a library that can do this for me? I also need
> to valid the range before I expand it, i.e., h[1*100].domain.com should
> not be accept, or other formats should not be accepted.

To get you started:

import re
import itertools

def to_range(s, sep="-"):
    """
    >>> to_range("9-11")
    ['9', '10', '11']
    """
    lo, hi = s.split(sep)
    return [str(i) for i in range(int(lo), int(hi)+1)]

def explode(s):
    """
    >>> list(explode("h[3-5].com"))
    ['h3.com', 'h4.com', 'h5.com']
    """
    parts = re.compile(r"(\[\d+-\d+\])").split(s)
    parts[0::2] = [[p] for p in parts[0::2]]
    parts[1::2] = [to_range(p[1:-1]) for p in parts[1::2]]
    return ("".join(p) for p in itertools.product(*parts))

if __name__ == "__main__":
    dom = "h[1-3]x[9-11].com"
    print(dom)
    for name in explode(dom):
        print("    {}".format(name))

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


Thread

Re: Hostrange expansion Peter Otten <__peter__@web.de> - 2013-09-27 19:45 +0200

csiph-web