Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up Date: Sun, 31 Jan 2016 16:23:18 +1100 Lines: 34 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de mCrL4Jp+apz4HX7whf+LcA22T4XDcyD2f2dmiCa9Hx0Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; 'failing': 0.05; 'cc:addr:python-list': 0.09; 'none.': 0.09; 'jan': 0.11; 'def': 0.13; 'subject: \n ': 0.15; '(there': 0.16; '2016': 0.16; 'doing:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'parameter,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'stick': 0.18; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '31,': 0.22; 'libraries': 0.22; 'ones.': 0.22; 'parse': 0.22; 'parsing': 0.22; 'header:In-Reply- To:1': 0.24; "doesn't": 0.26; 'message-id:@mail.gmail.com': 0.27; 'typically': 0.29; "i'm": 0.30; "i'd": 0.31; 'option': 0.31; 'subject:all': 0.32; 'wrap': 0.33; 'except': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'fail': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'doing': 0.38; 'received:209': 0.38; 'stuff': 0.38; 'several': 0.38; 'sure': 0.39; 'some': 0.40; 'your': 0.60; 'default': 0.61; 'different': 0.63; 'here': 0.66; 'obvious': 0.76; 'chrisa': 0.84; 'pain': 0.84; 'to:none': 0.91; 'that),': 0.91; 'try.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Qx0okdAi6h5Q+efBox4oLXkHIG2uEqxluhz+1FNpk8c=; b=Au7WC6phhcUzy5WUlnbs8343d6CYZ2GgmHCJgU8rpIZAQ6w6Wn5c7aVC98pso/C08q sK0BXTfJt0cc/MEHD01pnI2YbcSkh+dhC0H2ONDBUUMIXzC5vuylsiLoTYhpQGnD0B06 ZKbWlbRooDmP2zE3/wSjnYxxtesSIGpLBZBueaBI9JLgA2evWowxoYzfjSyl4CXxIoaB rmU3lQGK0HIs7E4sGAxW1AUZOVqBfh2UvFCoSxgU7Jz640begqK13I38QkNDHANxdkDn SRZ3OCeoIg9KJhfhrNv1RNMf5Sf1WAJ4NwFxt9wuSGYXBtNY59iPO5BEcEfrkVmStJXA u36g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=Qx0okdAi6h5Q+efBox4oLXkHIG2uEqxluhz+1FNpk8c=; b=aoUne/2m9ots1nD0I/mquBe9l+SUI9GY/OkiRQtEY2LdVSwKDhqb8ZD1NLubXEvojv fT9b+K7z4YhYDUWzi40NlkxgMwhK3dwOvwdniQLp56798fMpwIIW3s77u5UYtE5X9LE3 JpQIYKClYOZS7kOLzkIRs7+GAtI3iEUqx9iCCsEiN7Q6MlDfdRg+9PHhuN4cjURGab7r HGdiq+YYnlrzJirO88MSmIoiNYR+V/k0wJjruiVQOwuRj1Au8tIOLOQ6XwyPmknHmJrT itmR3tMmcyO3NBHO1u8Pl9fC6Mlbu/6YyDGgllAz3ybZCOnjPKEkHElqfuY7aiobbmGj 2d9Q== X-Gm-Message-State: AG10YOSWjoL6QmWV0ZoqngD4wWQwJ+P/PBscEaAh7tYL+VbYSn5CiSg6VvY7mK7RmT3Qqw3iSLY2gFPE3dVmCw== X-Received: by 10.107.40.76 with SMTP id o73mr16057792ioo.157.1454217798169; Sat, 30 Jan 2016 21:23:18 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102334 On Sun, Jan 31, 2016 at 3:58 PM, Veek. M wrote: > I'm parsing html and i'm doing: > > x = root.find_class(... > y = root.find_class(.. > z = root.find_class(.. > > all 3 are likely to fail so typically i'd have to stick it in a try. This is > a huge pain for obvious reasons. > > try: > .... > except something: > x = 'default_1' > (repeat 3 times) > > Is there some other nice way to wrap this stuff up? I'm not sure what you're using to parse HTML here (there are several libraries for doing that), but the first thing I'd look for is an option to have it return a default if it doesn't find something - even if that default has to be (say) None. But failing that, you can always write your own wrapper: def find_class(root, ...): try: return root.find_class(...) except something: return 'default_1' Or have the default as a parameter, if it's different for the different ones. ChrisA