Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Curious to see alternate approach on a search/replace via regex

Reply
Thread Tools

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

 
 
rh
Guest
Posts: n/a
 
      02-07-2013
On Thu, 07 Feb 2013 10:49:06 +0100
Peter Otten <(E-Mail Removed)> 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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Curious to see alternate approach on a search/replace via regex Demian Brecht Python 18 02-26-2013 07:20 PM
Curious to see alternate approach on a search/replace via regex rh Python 6 02-08-2013 08:53 AM
Re: Curious to see alternate approach on a search/replace via regex Peter Otten Python 0 02-07-2013 09:49 AM
Re: Curious to see alternate approach on a search/replace via regex MRAB Python 0 02-06-2013 11:11 PM
Re: Curious to see alternate approach on a search/replace via regex Demian Brecht Python 0 02-06-2013 10:33 PM



Advertisments