Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 67509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:16:16+00:00 2026-05-10T19:16:16+00:00

I am writing a script at the moment that will grab certain information from

  • 0

I am writing a script at the moment that will grab certain information from HTML using dom4j.

Since Python/Jython does not have a native switch statement I decided to use a whole bunch of if statements that call the appropriate method, like below:

if type == 'extractTitle':     extractTitle(dom) if type == 'extractMetaTags':     extractMetaTags(dom) 

I will be adding more depending on what information I want to extract from the HTML and thought about taking the dictionary approach which I found elsewhere on this site, example below:

{     'extractTitle':    extractTitle,     'extractMetaTags': extractMetaTags }[type](dom) 

I know that each time I run the script the dictionary will be built, but at the same time if I were to use the if statements the script would have to check through all of them until it hits the correct one. What I am really wondering, which one performs better or is generally better practice to use?

Update: @Brian – Thanks for the great reply. I have a question, if any of the extract methods require more than one object, e.g.

handle_extractTag(self, dom, anotherObject) # Do something 

How would you make the appropriate changes to the handle method to implemented this? Hope you know what I mean 🙂

Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:16:16+00:00Added an answer on May 10, 2026 at 7:16 pm

    To avoid specifying the tag and handler in the dict, you could just use a handler class with methods named to match the type. Eg

    class  MyHandler(object):     def handle_extractTitle(self, dom):         # do something      def handle_extractMetaTags(self, dom):         # do something      def handle(self, type, dom):         func = getattr(self, 'handle_%s' % type, None)         if func is None:             raise Exception('No handler for type %r' % type)         return func(dom) 

    Usage:

     handler = MyHandler()  handler.handle('extractTitle', dom) 

    Update:

    When you have multiple arguments, just change the handle function to take those arguments and pass them through to the function. If you want to make it more generic (so you don’t have to change both the handler functions and the handle method when you change the argument signature), you can use the *args and **kwargs syntax to pass through all received arguments. The handle method then becomes:

    def handle(self, type, *args, **kwargs):     func = getattr(self, 'handle_%s' % type, None)     if func is None:         raise Exception('No handler for type %r' % type)     return func(*args, **kwargs) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I amm writing a little python script that will grab information from VMs of
I'm writing a script in Python that saves attachments from Gmail, only from unseen
I'm writing a script in python which basically queries WMI and updates the information
I'm writing a script that will loop through a range of numbers, build a
I'm writing a script that has to move some file around, but unfortunately it
I'm writing a script in PHP that compares people together based on the number
First post, so here goes. I'm writing a script that does intelligent search and
I'm writing a Python backup script and I need to find the oldest file
I'm writing a script to automate some command line commands in Python. At the
I'm writing a sendmail script in PHP at the moment. It's largely based around

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.