Generating data types automatically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Torsten Bronger

    #1

    Generating data types automatically

    Hallöchen!

    I have to generate a lot of data types (for ctypes by the way). An
    example is

    ViUInt32 = u_long
    ViPUInt32 = POINTER(ViUInt3 2)
    ViAUInt32 = ViPUInt32

    Therefore, I defined functions that should make my life easier:

    def generate_type_d ublett(visa_typ e, ctypes_type):
    visa_type_name = visa_type.__nam e__
    exec visa_type_name + "=" + ctypes_type.__n ame__
    exec "ViP" + visa_type_name[2:] + "=POINTER(" + visa_type_name + ")"

    def generate_type_t riplett(visa_ty pe, ctypes_type):
    generate_type_d ublett(visa_typ e, ctypes_type)
    visa_type_name = visa_type.__nam e__
    exec "ViA" + visa_type_name[2:] + "=" + "ViP" + visa_type_name[2:]

    generate_type_t riplett(ViUInt3 2, c_ulong)


    However, this doesn't work, probably because the defined type exist
    only locally within the function.

    What is a better (and working) method for this task?

    Thank you!

    Tschö,
    Torsten.

    --
    Torsten Bronger, aquisgrana, europa vetus
  • Torsten Bronger

    #2
    Re: Generating data types automatically

    Hallöchen!

    Torsten Bronger <bronger@physik .rwth-aachen.de> writes:
    [color=blue]
    > I have to generate a lot of data types (for ctypes by the way).
    > An example is
    >
    > ViUInt32 = u_long
    > ViPUInt32 = POINTER(ViUInt3 2)
    > ViAUInt32 = ViPUInt32
    >
    > Therefore, I defined functions that should make my life easier:
    >
    > [...]
    >
    > However, this doesn't work, probably because the defined type
    > exist only locally within the function.[/color]

    Okay this works:

    def generate_type_d ublett(visa_typ e, ctypes_type):
    return visa_type + "=" + ctypes_type + ";" + \
    "ViP" + visa_type[2:] + "=POINTER(" + visa_type + ")"

    def generate_type_t riplett(visa_ty pe, ctypes_type):
    return generate_type_d ublett(visa_typ e, ctypes_type) + ";" + \
    "ViA" + visa_type[2:] + "=" + "ViP" + visa_type[2:]

    exec generate_type_t riplett("ViUInt 32", "c_ulong" )
    ....


    Not very beautiful, though.

    Tschö,
    Torsten.

    --
    Torsten Bronger, aquisgrana, europa vetus

    Comment

    • Michael Hoffman

      #3
      Re: Generating data types automatically

      Torsten Bronger wrote:
      [color=blue]
      > def generate_type_d ublett(visa_typ e, ctypes_type):
      > visa_type_name = visa_type.__nam e__
      > exec visa_type_name + "=" + ctypes_type.__n ame__
      > exec "ViP" + visa_type_name[2:] + "=POINTER(" + visa_type_name + ")"[/color]

      You shouldn't need to use exec for this, and it is best to avoid its use.

      If you MUST do things this way, then you can add items to the globals()
      dictionary. See the library reference for more details. It's probably
      best to avoid globals() as well, although it's not as bad as exec/eval.

      Personally, I think it would be better to define your types like this:

      ViUInt32, ViPUInt32, ViAUInt32 = generate_type_t riplet(u_long)

      That way you will easily be able to find the initial definition of
      the object by searching and replacing. You'll also have to jump through
      fewer weird hoops to get the result you want.
      --
      Michael Hoffman

      Comment

      • Steven Bethard

        #4
        Re: Generating data types automatically

        Torsten Bronger wrote:[color=blue]
        > Okay this works:
        >
        > def generate_type_d ublett(visa_typ e, ctypes_type):
        > return visa_type + "=" + ctypes_type + ";" + \
        > "ViP" + visa_type[2:] + "=POINTER(" + visa_type + ")"
        >
        > def generate_type_t riplett(visa_ty pe, ctypes_type):
        > return generate_type_d ublett(visa_typ e, ctypes_type) + ";" + \
        > "ViA" + visa_type[2:] + "=" + "ViP" + visa_type[2:]
        >
        > exec generate_type_t riplett("ViUInt 32", "c_ulong" )
        > ...[/color]

        Exec is nasty. Can you create a dict and use an update to globals instead?

        py> def get_types(visa_ type_name, ctypes_type):
        .... pointer = ctypes.POINTER( ctypes_type)
        .... return {'Vi%s' % visa_type_name: ctypes_type,
        .... 'ViP%s' % visa_type_name: pointer,
        .... 'ViA%s' % visa_type_name: pointer}
        ....
        py> globals().updat e(get_types("UI nt32", ctypes.c_ulong) )
        py> ViUInt32
        <class 'ctypes.c_ulong '>
        py> ViPUInt32
        <class 'ctypes.LP_c_ul ong'>
        py> ViAUInt32
        <class 'ctypes.LP_c_ul ong'>

        STeVe

        Comment

        • Thomas Heller

          #5
          Re: Generating data types automatically

          Torsten Bronger <bronger@physik .rwth-aachen.de> writes:
          [color=blue]
          > Hallöchen!
          >
          > I have to generate a lot of data types (for ctypes by the way). An
          > example is
          >
          > ViUInt32 = u_long
          > ViPUInt32 = POINTER(ViUInt3 2)
          > ViAUInt32 = ViPUInt32
          >
          > Therefore, I defined functions that should make my life easier:
          >
          > def generate_type_d ublett(visa_typ e, ctypes_type):
          > visa_type_name = visa_type.__nam e__
          > exec visa_type_name + "=" + ctypes_type.__n ame__
          > exec "ViP" + visa_type_name[2:] + "=POINTER(" + visa_type_name + ")"
          >
          > def generate_type_t riplett(visa_ty pe, ctypes_type):
          > generate_type_d ublett(visa_typ e, ctypes_type)
          > visa_type_name = visa_type.__nam e__
          > exec "ViA" + visa_type_name[2:] + "=" + "ViP" + visa_type_name[2:]
          >
          > generate_type_t riplett(ViUInt3 2, c_ulong)
          >
          >
          > However, this doesn't work, probably because the defined type exist
          > only locally within the function.[/color]

          Others have answered your question already, but I would like to note one
          thing: The POINTER() function caches its results, so you could (and
          should, imo) write 'POINTER(ViUInt 32)' instead everywhere in your code.
          Calling POINTER(ViUInt3 2) *allways* returns the same type.

          Thomas

          Comment

          • Torsten Bronger

            #6
            Re: Generating data types automatically

            Hallöchen!

            Thomas Heller <theller@python .net> writes:
            [color=blue]
            > Torsten Bronger <bronger@physik .rwth-aachen.de> writes:
            >[color=green]
            >> I have to generate a lot of data types (for ctypes by the way).
            >> An example is
            >>
            >> ViUInt32 = u_long
            >> ViPUInt32 = POINTER(ViUInt3 2)
            >> ViAUInt32 = ViPUInt32
            >>
            >> [...][/color]
            >
            > Others have answered your question already, but I would like to
            > note one thing: The POINTER() function caches its results, so you
            > could (and should, imo) write 'POINTER(ViUInt 32)' instead
            > everywhere in your code. Calling POINTER(ViUInt3 2) *allways*
            > returns the same type.[/color]

            Actually I don't think that we will use them (ViP...) at all, since
            we'll make use of "byref" whereever they occur in an args list.

            But they are in the spec that we try to mimic, and as long as I
            don't know for sure that nobody uses them, they don't hurt.

            Tschö,
            Torsten.

            --
            Torsten Bronger, aquisgrana, europa vetus

            Comment

            Working...