Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Django Concatenate a string depending on whether that string exists

I'm creating a property on a Django model called "address". I want address to consist of the concatenation of a number of fields I have on my model. The problem is that not all instances of this model will have values for all of these fields. So, I want to concatenate only those fields that have values.

What is the best/most Pythonic way to do this?

Here are the relevant fields from the model:

house = models.IntegerField('House Number', null=True, blank=True)
suf = models.CharField('House Number Suffix', max_length=1, null=True, blank=True)
unit = models.CharField('Address Unit', max_length=7, null=True, blank=True)
stex = models.IntegerField('Address Extention', null=True, blank=True)
stdir = models.CharField('Street Direction', max_length=254, null=True, blank=True)
stnam = models.CharField('Street Name', max_length=30, null=True, blank=True)
stdes = models.CharField('Street Designation', max_length=3, null=True, blank=True)
stdessuf = models.CharField('Street Designation Suffix',max_length=1, null=True, blank=True)

I could just do something like this:

def _get_address(self):
    return "%s %s %s %s %s %s %s %s" % (self.house, self.suf, self.unit, self.stex, self.stdir, self.stname, self.stdes, self.stdessuf)

but then there would be extra blank spaces in the result.

I could do a series of if statements and concatenate within each, but that seems ugly.

What's the best way to handle this situation?

Thanks.

like image 530
Douglas Meehan Avatar asked Jul 01 '12 21:07

Douglas Meehan


1 Answers

parts = (1, 'a', 'b', 2, 'c', 'd', None, 'f')
# parts = (self.house, self.suf, self.unit, 
#           self.stex, self.stdir, self.stname, 
#           self.stdes, self.stdessuf)
' '.join(str(part) for part in parts if part is not None)
# '1 a b 2 c d e f'

You just do a list comp, and check if any value is not None, and also convert them to a string so they join properly regardless of type.

like image 180
jdi Avatar answered Sep 23 '22 23:09

jdi