user3464644
user3464644

Reputation: 1

Need to print with spaces

I have to print name with spaces, can u help me please? I got the code like this:

class Perfil:
    def __init__(self,email,nome,cidade):
        self.email=email
        self.nome=nome
        self.cidade=cidade

    def __str__(self):
        return "Perfil de "+self.nome+" ""("+self.email+")"" de "+self.cidade

    def getCidade(self):
        return self.cidade

    def setCidade(self,novo):
        self.cidade=novo

    def getDominio(self):
        t=self.email.rpartition("@")
        return t[2]

    def limpaNome(self):
        new=""
        if self.nome.isalpha()==True:
            return self.nome
        else:
            for i in self.nome:
                if i.isalpha()==True:
                    new +=i
        return new

When i run the program:

>>> p=Perfil("[email protected]","Ze Car231los", "Porto")
>>> p.limpaNome()
'ZeCarlos'

I need a print like 'Ze Carlos' (with space)

Basically i need to wrote a program using abstract data types (class Profile) to save information for each user. Each object got the following attributes:

email
name
city

The class should have the following methods to manipulate the objects above

Method
__init__(self, email, name, city) - constructor
__str__(self)
getCity(self) - return the value of atribute city
getCity(self.new) - return the atribute city with a new value
getDomain(self) - example: [email protected] sugestion: use the method partition (i have to return mail.com only)
cleanName(self) - change the atribute name, deleting characters WICH are not alphabetic or spaces sugestion: use method isalpha

Upvotes: 0

Views: 61

Answers (2)

unutbu
unutbu

Reputation: 879591

If all you want to do is remove all occurrences of '0','1','2',...,'9' from the string, then you could use str.translate like this:

def limpaNome(self):
    return self.nome.translate({ord(c):None for c in '0123456789'})

Note that there is no need for getters/setters like this in Python:

def getCidade(self):
    return self.cidade

def setCidade(self,novo):
    self.cidade=novo

Instead, just let the user access/set the attribute directly: self.cidade. If, at some point, you'd like to run a function whenever the attribute is accessed or assigned to, then you can make cidade a property without having to change the usage syntax.


You could even make getDominio and limpaNome properties too:

@property
def dominio(self):
    t=self.email.rpartition("@")
    return t[2]

@property
def limpaNome(self):
    return self.nome.translate({ord(c):None for c in '0123456789'})

Notice you don't need paretheses when accessing or setting the property. The syntax looks the same as though lipaNome were a plain attribute:

>>> p=Perfil("[email protected]","Ze Car231los", "Porto")
>>> p.limpaNome
Ze Carllos
>>> p.dominio
mail.pt

Upvotes: 1

Adam Smith
Adam Smith

Reputation: 54213

import string

# ... the rest of your code
# ...

    def limpaNome(self):
        whitelist = set(string.ascii_uppercase+string.ascii_lowercase+" ")
        if self.nome.isalpha():
            return self.nome
        else:
            return ''.join(ch for ch in self.nome if ch in whitelist)

Or with regex:

import re

# ...
# ...

    def limpaNome(self):
        return re.sub(r"[^a-zA-Z ]",'',self.nome)

Note that if I were you, I'd do:

class Perfil:
    def __init__(self, email, nome, cidade):
        self.email = email
        self.cidade = cidade
        self.nome = limpaNome(nome)

Upvotes: 0

Related Questions