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

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer There are plug-ins available for this, such as StartExplorer, Eclipse… May 11, 2026 at 3:55 pm
  • added an answer I would improve that little bit: private _xyz as cXYZ… May 11, 2026 at 3:55 pm
  • added an answer You almost had it -- just drop the quotes around… May 11, 2026 at 3:55 pm

Related Questions

I have a php page that displays rows from a mysql db as a
I have an HTML form that submits to a PHP page which initiates a
I am writing a Perl script that is searching for a term in large
I am currently writing a deployment script in MSBUILD, and after downloading several extensions,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.