Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38376
| 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!newsfeed1.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.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; '"__main__":': 0.07; '__name__': 0.07; 'converts': 0.07; 'so?': 0.07; 'try:': 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; 'valueerror': 0.09; 'def': 0.10; '"_"': 0.16; '"_")': 0.16; '2.7.3': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:search': 0.16; 'string': 0.17; 'wrote:': 0.17; 'alternate': 0.17; 'thu,': 0.17; 'feb': 0.19; 'import': 0.21; 'http': 0.22; 'this:': 0.23; 'raise': 0.24; 'header:User-Agent:1': 0.26; '+0100': 0.27; 'i.e.': 0.27; 'replace': 0.27; 'regular': 0.27; 'header:X-Complaints-To:1': 0.28; 'rest': 0.28; 'subject:/': 0.28; 'once.': 0.29; 'protocols': 0.29; 'becomes': 0.30; 'curious': 0.33; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'done': 0.34; 'faster': 0.35; 'protocol': 0.35; 'there': 0.35; 'received:org': 0.36; 'except': 0.36; 'skip:u 20': 0.36; 'charset :us-ascii': 0.36; 'skip:p 20': 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'easily': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'remove': 0.61; 'skip:a 40': 0.61; 'more': 0.63; 'url:a': 0.72; '2013': 0.84; '85%': 0.84; 'otten': 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 13:04:55 -0800 |
| References | <20130206134105.9352d665e1f8d0719021fcf9@lavabit.com> <kevta5$ii3$1@ger.gmane.org> |
| 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.1458.1360271112.2939.python-list@python.org> (permalink) |
| Lines | 57 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1360271112 news.xs4all.nl 6960 [2001:888:2000:d::a6]:42230 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:38376 |
Show key headers only | View raw
On Thu, 07 Feb 2013 10:49:06 +0100
Peter Otten <__peter__@web.de> wrote:
> 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.
>
> Completely without regular expressions:
>
> import string
>
> ILLEGAL = "-:./?&="
> try:
> TRANS = string.maketrans(ILLEGAL, "_" * len(ILLEGAL))
> except AttributeError:
> # python 3
> TRANS = dict.fromkeys(map(ord, ILLEGAL), "_")
>
> PROTOCOLS = {"http", "https"}
>
> def url_to_file(url):
> protocol, sep, rest = url.partition("://")
> if protocol not in PROTOCOLS:
> raise ValueError
> return rest.translate(TRANS)
>
> if __name__ == "__main__":
> url = "http://alongnameofasite1234567.com/q?sports=run&a=1&b=1"
> print(url)
> print(url_to_file(url))
2.7.3 is 85% faster than 3.3.0
(no printing in my test)
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Curious to see alternate approach on a search/replace via regex rh <richard_hubbe11@lavabit.com> - 2013-02-07 13:04 -0800
csiph-web