Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; '"__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; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:search': 0.16; 'string': 0.17; 'wrote:': 0.17; 'alternate': 0.17; 'import': 0.21; 'http': 0.22; 'this:': 0.23; 'raise': 0.24; 'header:User-Agent:1': 0.26; '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; 'protocol': 0.35; 'there': 0.35; 'received:org': 0.36; 'except': 0.36; 'skip:u 20': 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; 'subject:via': 0.84; 'step.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Curious to see alternate approach on a search/replace via regex Date: Thu, 07 Feb 2013 10:49:06 +0100 Organization: None References: <20130206134105.9352d665e1f8d0719021fcf9@lavabit.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084aa2e.dip.t-dialin.net 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360230550 news.xs4all.nl 6887 [2001:888:2000:d::a6]:50007 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38341 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))