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


Groups > comp.lang.python > #38350

Re: Curious to see alternate approach on a search/replace via regex

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <thebalancepro@gmail.com>
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; 'case.': 0.05; 'bytes.': 0.07; 'converts': 0.07; 'so?': 0.07; 'python': 0.09; 'differently.': 0.09; 'filename,': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'nick': 0.16; 'subject:search': 0.16; 'urllib.parse': 0.16; 'translation': 0.16; 'wrote:': 0.17; 'alternate': 0.17; 'skip:u 30': 0.17; 'version.': 0.17; 'translate': 0.20; 'import': 0.21; 'http': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'i.e.': 0.27; 'replace': 0.27; 'subject:/': 0.28; 'once.': 0.29; 'becomes': 0.30; 'thursday,': 0.30; '(and': 0.32; 'print': 0.32; 'skip:s 30': 0.33; 'curious': 0.33; 'changed': 0.34; 'received:google.com': 0.34; 'text': 0.34; 'done': 0.34; 'faster': 0.35; 'protocol': 0.35; 'table': 0.35; 'there': 0.35; 'method': 0.36; 'subject:: ': 0.38; 'easier': 0.38; 'skip:l 20': 0.38; 'easily': 0.39; 'little': 0.39; 'your': 0.60; 'remove': 0.61; 'skip:a 40': 0.61; 'more': 0.63; 'due': 0.66; 'url:a': 0.72; '2013': 0.84; 'subject:via': 0.84; 'step.': 0.91
X-Received by 10.49.71.135 with SMTP id v7mr65046qeu.28.1360241602889; Thu, 07 Feb 2013 04:53:22 -0800 (PST)
Newsgroups comp.lang.python
Date Thu, 7 Feb 2013 04:53:22 -0800 (PST)
In-Reply-To <mailman.1425.1360186878.2939.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=124.168.207.54; posting-account=ZAg6xAoAAAAmY8bBi3VzYjWntm8Ct1P8
References <mailman.1425.1360186878.2939.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 124.168.207.54
MIME-Version 1.0
Subject Re: Curious to see alternate approach on a search/replace via regex
From Nick Mellor <thebalancepro@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.1449.1360246767.2939.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360246767 news.xs4all.nl 6984 [2001:888:2000:d::a6]:49612
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38350

Show key headers only | View raw


Hi RH,

translate methods might be faster (and a little easier to read) for your use case. Just precompute and re-use the translation table punct_flatten.

Note that the translate method has changed somewhat for Python 3 due to the separation of text from bytes. The is a Python 3 version.

from urllib.parse import urlparse

flattened_chars = "./&=?"
punct_flatten = str.maketrans(flattened_chars, '_' * len(flattened_chars))
parts = urlparse('http://alongnameofasite1234567.com/q?sports=run&a=1&b=1')
unflattened = parts.netloc + parts.path + parts.query
flattened = unflattened.translate(punct_flatten)
print (flattened)

Cheers,

Nick

On Thursday, 7 February 2013 08:41:05 UTC+11, rh  wrote:
> I am curious to know if others would have done this differently. And if so
> 
> how so?
> 
> 
> 
> This converts a url to a more easily managed filename, stripping the
> 
> http protocol off. 
> 
> 
> 
> This:
> 
>  
> 
> http://alongnameofasite1234567.com/q?sports=run&a=1&b=1
> 
> 
> 
> becomes this:
> 
> 
> 
> alongnameofasite1234567_com_q_sports_run_a_1_b_1
> 
> 
> 
> 
> 
> def u2f(u):
> 
>     nx = re.compile(r'https?://(.+)$')
> 
>     u = nx.search(u).group(1)
> 
>     ux = re.compile(r'([-:./?&=]+)')
> 
>     return ux.sub('_', u)
> 
> 
> 
> One alternate is to not do the compile step. There must also be a way to
> 
> do it all at once. i.e. remove the protocol and replace the chars.

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


Thread

Curious to see alternate approach on a search/replace via regex rh <richard_hubbe11@lavabit.com> - 2013-02-06 13:41 -0800
  Re: Curious to see alternate approach on a search/replace via regex Roy Smith <roy@panix.com> - 2013-02-06 16:54 -0500
  Re: Curious to see alternate approach on a search/replace via regex Nick Mellor <thebalancepro@gmail.com> - 2013-02-07 04:53 -0800
    Re: Curious to see alternate approach on a search/replace via regex rh <richard_hubbe11@lavabit.com> - 2013-02-07 21:47 -0800
      Re: Curious to see alternate approach on a search/replace via regex Nick Mellor <thebalancepro@gmail.com> - 2013-02-08 00:53 -0800
      Re: Curious to see alternate approach on a search/replace via regex Nick Mellor <thebalancepro@gmail.com> - 2013-02-08 00:53 -0800
  Re: Curious to see alternate approach on a search/replace via regex Nick Mellor <thebalancepro@gmail.com> - 2013-02-07 04:53 -0800

csiph-web