import xml.etree.ElementTree as etree
import re

def indent(elem, level=0):
    """Indents an etree element so printing that element is actually human-readable"""
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

def debug_print_tree(elem):
    indent(elem)
    etree.dump(elem)

class Package(object):
    def __init__(self, weight_in_ozs, length, width, height, value=0, require_signature=False):
        self.weight = weight_in_ozs / 16
        self.length = length
        self.width = width
        self.height = height
        self.value = value
        self.require_signature = require_signature

class Address(object):
    def __init__(self, name, address, city, state, zip, country, address2='', phone='', email='', is_residence=True, company_name=''):
        self.company_name = company_name
        self.name = name
        self.address1 = address
        self.address2 = address2
        self.city = city
        self.state = state
        self.zip = str(zip).split('-')[0]
        self.country = country
        self.phone = re.sub('[^0-9]*', '', str(phone)) if phone else ''
        self.email = email
        self.is_residence = is_residence
    
    def __eq__(self, other):
        return vars(self) == vars(other)
    
    def __repr__(self):
        street = self.address1
        if self.address2:
            street += '\n' + self.address2
        return '%s\n%s\n%s, %s %s %s' % (self.name, street, self.city, self.state, self.zip, self.country)
@@ -1183,24 +1183,30 @@
 company_name
+ or ''
 %0A        sel
@@ -1218,16 +1218,22 @@
 e = name
+ or ''
 %0A       
@@ -1256,16 +1256,22 @@
  address
+ or ''
 %0A       
@@ -1287,32 +1287,38 @@
 ress2 = address2
+ or ''
 %0A        self.ci
@@ -1326,16 +1326,22 @@
 y = city
+ or ''
 %0A       
@@ -1359,16 +1359,22 @@
  = state
+ or ''
 %0A       
@@ -1407,16 +1407,31 @@
 ('-')%5B0%5D
+ if zip else ''
 %0A       
@@ -1453,16 +1453,22 @@
  country
+ or ''
 %0A       
@@ -1558,16 +1558,22 @@
  = email
+ or ''
 %0A       
@@ -1605,16 +1605,25 @@
 esidence
+ or False
 %0A    %0A  

Make sure we always get strings for Address