Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 12th, 2003, 04:57 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 9 sec
Reputation Power: 13
python equivalents

I am converting a perl module for filtering form input into a python equivalent, and need the python equivalent of the perl 'tr' operator. I came up with some routines (below), but wondered if there was a better way?

These are the some of the perl routines:

Code:

sub squeeze_ws {
    my $str = shift;
    $str =~ tr/ / /s;
    return $str;
}

sub force_int {
    my $str = shift; 
    $str =~ tr/0-9//cd;
    return $str;
}

sub force_alpha {
    my $str = shift; 
    $str =~ tr/0-9a-zA-Z//cd;
    return $str;
}

sub force_char {
    my $str = shift; 
    $str =~ tr/a-zA-Z//cd;
    return $str;
}


And here is the python equivalent:

Code:
class Filter(object):
    def __init__(self):
        pass

    def chop_blanks(s):
        return s.strip()

    def squeeze_ws(s):
        s = s.expandtabs()
        a = s.split(' ')
        return ' '.join([i for i in a if i and not i.isspace()])

    def force_int(s):
        return Filter.trcd(s, range(0,9))

    def force_alpha(s):
        chars = "abcdefghijklmnopqrstuvwxyz" \
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
                "0123456789"
        return Filter.trcd(s, chars)

    def force_char(s):
        chars = "abcdefghijklmnopqrstuvwxyz" \
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        return Filter.trcd(s, chars)

    def trcd(s, keep):
        allchars = string.maketrans('', '')
        delchars = ''.join([c for c in allchars if c not in keep])
        return s.translate(allchars, delchars)

    chop_blanks = staticmethod(chop_blanks)
    # ...

Any thoughts?

Reply With Quote
  #2  
Old March 12th, 2003, 06:22 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 513 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
Look into the re module. It should do all you need.

Also, this page is really handy if you, like me, are coming from Perl to Python:

http://starship.python.net/~da/jak/cookbook.html

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > python equivalents

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap