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


Groups > comp.lang.python > #38407

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

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'case.': 0.05; 'removes': 0.05; 'builtin': 0.07; 'bytes.': 0.07; 'completeness': 0.07; 'converts': 0.07; 'so?': 0.07; 'python': 0.09; 'differently.': 0.09; 'filename,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'looked': 0.10; 'def': 0.10; "wouldn't": 0.11; 'library': 0.15; 'did:': 0.16; 'len': 0.16; 'nick': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sees': 0.16; 'stuff.': 0.16; 'subject:search': 0.16; 'urllib': 0.16; 'urllib.parse': 0.16; 'translation': 0.16; 'wrote:': 0.17; 'alternate': 0.17; 'skip:u 30': 0.17; 'specify': 0.17; 'thu,': 0.17; 'version.': 0.17; 'feb': 0.19; 'translate': 0.20; 'import': 0.21; 'http': 0.22; "i'd": 0.22; 'cheers,': 0.23; 'this:': 0.23; "haven't": 0.23; 'idea': 0.24; 'header:User-Agent:1': 0.26; 'i.e.': 0.27; 'replace': 0.27; 'header:X-Complaints-To:1': 0.28; 'subject:/': 0.28; 'once.': 0.29; "i'm": 0.29; 'maybe': 0.29; 'becomes': 0.30; 'query': 0.30; 'thursday,': 0.30; 'point': 0.31; '(and': 0.32; 'print': 0.32; 'skip:s 30': 0.33; 'curious': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'changed': 0.34; 'text': 0.34; 'done': 0.34; 'faster': 0.35; 'protocol': 0.35; 'table': 0.35; 'there': 0.35; 'list.': 0.35; 'received:org': 0.36; 'but': 0.36; 'wanted': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'subject:: ': 0.38; 'easier': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'easily': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'remove': 0.61; 'skip:a 40': 0.61; 'more': 0.63; 'due': 0.66; 'url:a': 0.72; '2013': 0.84; 'received:sd.cox.net': 0.84; 'subject:via': 0.84; 'step.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From rh <richard_hubbe11@lavabit.com>
Subject Re: Curious to see alternate approach on a search/replace via regex
Date Thu, 7 Feb 2013 21:47:03 -0800
References <mailman.1425.1360186878.2939.python-list@python.org> <14f76032-8753-4e17-897c-447242180e69@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host ip68-227-87-145.sb.sd.cox.net
User-Agent dsodnetnin
X-Mailer EZnn0.37p
X-Newsreader EZnn0.37p
X-Gmane-NNTP-Posting-Host EZnn0.37p
Original-Received from slem by 1.1 with local
X-No-Archive yes
Archive no
X-Archive expiry=11
X-Archive encrypt
X-Operating-System Barebones_6.1
X-Gmane-NNTP-Posting-Host 192.168.1.1
X-NNTP-Posting-Host 192.168.1.1
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>
Newsgroups comp.lang.python
Message-ID <mailman.1476.1360302430.2939.python-list@python.org> (permalink)
Lines 91
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360302430 news.xs4all.nl 6881 [2001:888:2000:d::a6]:43104
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38407

Show key headers only | View raw


On Thu, 7 Feb 2013 04:53:22 -0800 (PST)
Nick Mellor <thebalancepro@gmail.com> wrote:

> 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)

I like the idea of using a library but since I'm learning python I wanted
to try out the regex stuff. I haven't looked but I'd think that urllib might
(should?) have a builtin so that one wouldn't have to specify the 
flattened_chars list. I'm sure there's a name for those chars but I don't know
it. Maybe just punctuation??

Also my version converts the ? into _ but urllib sees that as the query
separator and removes it. Just point this out for completeness sake.

This would mimic what I did:
unflattened = parts.netloc + parts.path + '_' + parts.query

> 
> 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 | Next 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