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 4040176
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:43:07+00:00 2026-05-20T12:43:07+00:00

It turned out that this conditional block keeps on repeating itself in my code.

  • 0

It turned out that this conditional block keeps on repeating itself in my code. Any other way to make my life easier? Of course, the body to be executed for a condition differs.

if self.datatype == "string":
    t = "z"
elif self.datatype == "double":
    t = "d"
elif self.datatype == "number":
    t = "i"
elif self.datatype == "blob":
    t = "z"
else:
    raise EntParEx("Unknown datatype" + self.datatype)

……more code using the same conditional

def emit_cpp_def(self):
    s = ""
    e = ""
    if self.datatype == "string":
        s += "static const int " + self.lenvar + " = " + self.length + ";"
        s += "\nchar"
        e += "[" + self.lenvar + " + 2" + "]"
    elif self.datatype == "double":
        s += "double"
    elif self.datatype == "number":
        s += "int"
    elif self.datatype == "blob":
        s += "char*"
    else:
        raise EntParEx("Unknown datatype" + self.datatype)

    s += " " + self.cpp_membername;
    s += e
    s += ";"
    return s;

def emit_cursor_def_code(self):
    if self.datatype == "blob":
        return ""

    ret = "nvl(" + self.db_fieldname + ", "
    #TODO: Add default value loading!
    if self.datatype == "string":
        ret += "\' \'"
    elif self.datatype == "double":
        ret += "-1.0"
    elif self.datatype == "number":
        ret += "-1"
    else:
        raise EntParEx("Unknown datatype" + self.datatype)
    ret += "), "
    return ret

EDIT:
I think what I need is something like running a specific function for each type. Unfortunately I’m not that versed in python. Can that be done? i.e.

switch_datatype(function_string(), function_integer(), ...etc)

Is this worse?

  • 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. Editorial Team
    Editorial Team
    2026-05-20T12:43:08+00:00Added an answer on May 20, 2026 at 12:43 pm

    If it’s the exact same conditional, stick it in a method and call it where you need it. Otherwise, define the dictionaries somewhere and use whichever you need.

    datatypes = {'string': 'z', 'double': 'd', 'number': 'i', 'blob': 'z'}
    t = datatypes[self.datatype]
    

    You may catch the KeyError and raise a domain exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: This turned into a blog post, with updated links and code, over at
Yesterday I was playing with jQGrid plugin and ASP.NET. Everything turned out well, my
A Google search turned up software that performs the same functions as cron, but
I code with treat warnings as errors turned on and XML Documentation turned on.
How can I modify a SharePoint site so that versioning is turned on by
I have a treeview control in a Windows Forms project that has checkboxes turned
I recently turned on Windows Firewall logging on my computer and started tracking incoming
At some point in time I turned on a setting in Visual Studio 2005
CFMX 8 Enterprise I have turned on the Use J2EE session variables setting under
Is the asp.net membership system used over wcf (transport security turned on) enough for

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.