Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Python Scripting to format data

LegacyForum
LegacyForum Posts: 1,664 ✭✭
edited December 2016 in SOAtest
Hey guys, I got this script for doing a format, but I get an error that I can't reconcile. The error states that The Method Takes only one Argument. I've looked for the ValueMap function in the API docs but can't find it. Does anything look out of the ordinary for this script?

CODE
def formatPhoneNumber(input, context):
  # get the phone number from the data bank in the form of 9122000107 for example.
  # Change the string "Test 3: prefix" to point to the column that the test1 data bank puts the phone number in
  value = context.getValue("Generated Data Source", "Test 3: prefix");

  # parse the number to get all the sub components
  areaCode = (str(value))[0:2]
  phoneNumber1 = (str(value))[3:5]
  phoneNumber2 = (str(value))[6:9]

  # Store the formatted phone number into the map of data banked values.
  # Modify the string "Test 1: x" to make it point to the data banked column name. In my test case, the phone number
  # was stored under the name "Test 1: x"
  valueMap = context.getGeneratedDataSourceValues()
  valueMap.put("Test 3: prefix", "(%s)%s-%s" % (areaCode, phoneNumber1, phoneNumber2))

Method takes only one argument.


Thanks in advance,

LeapTester
Tagged:

Comments

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    LeapTester,

    Everything looks o.k., I was able to run your script without any problems. See if you have any problems running the attached SOAtest project file.


    One thing to notice in this project, I slightly modified your script. It now looks like:

    CODE
    def formatPhoneNumber(input, context):
     # get the phone number from the data bank in the form of 9122000107 for example.
     # Change the string "Test 3: prefix" to point to the column that the test1 data bank puts the phone number in
     value = context.getValue("Generated Data Source", "Test 1: itemId");

     # parse the number to get all the sub components
     areaCode = (str(value))[0:3]
     phoneNumber1 = (str(value))[3:6]
     phoneNumber2 = (str(value))[6:10]

     # Store the formatted phone number into the map of data banked values.
     # Modify the string "Test 1: x" to make it point to the data banked column name. In my test case, the phone number
     # was stored under the name "Test 1: x"
     valueMap = context.getGeneratedDataSourceValues()
     valueMap.put("Test 1: itemId", "(%s)%s-%s" % (areaCode, phoneNumber1, phoneNumber2))


    Notice the index for areaCode, phoneNumber1, and phoneNumber2 is slightly different - the end index is not inclusive and was updated to reflect this. I'm not sure on the details of the error message that you were getting, so I suppose this was causing a problem somewhere. Regardless, you would probably have noticed the error on your own anyway.


    Let me know if this example project runs o.k. If it does, perhaps you can send me your project file and I'll be glad to take a look.

    -Mike
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    the only difference between our setup is that you've used a method tool to do the number formatting. What I did was place the script in the service method input which didn't work?

    very curious...

    but if I copy your workflow (soap client-xmlDB --> method --> paramerterized soap client input) the script works.
    the workflow I was doing was (soap client-xmlDB --> scripted soap client input) the script fails.

    very strange, but i'll take it.

    Thanks dude,

    LeapTester
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Hi LeapTester,

    Now that you've given a bit more detail about your setup, I can tell you that the scripted input does not take the same number of arguments as the Method Tool scripts. So in other words, if you are using a scripted input, you would have to define the method with the following signature:

    CODE
    def foo(context):

    This is because the scripted input does not take any "input" parameters, instead, you are defining the input yourself with the script that you will write.

    In addition to this, you do not have access to the Generated Data Source values in the same way that you would with a Method Tool. Instead, you would have to use get() and put() calls to store values in the Context. An example of this is seen in the following forum post:

    http://forums.parasoft.com/index.php?act=ST&f=44&t=972


    If the seperate Method Tool works for you, I would recommend sticking with that.

    -Mike
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Hey Mike I think you wrote this script for us a while back and I?ve been playing with it to allow use with inputs from a writable data source as well as a Generated Data Source. Is this possible? Below is what I I have so far.

    Thanks once again for your assistance,

    Best,

    LeapTester


    CODE
    def formatPhoneNumber(input, context):
    # get the phone number from the data source in the form of 9122000107
    # change the string from data source Phone numbers: mdn (col)
    # get the phone number from the data bank in the form of 9122000107 for example.
    # Change the string "Test 3: prefix" to point to the column that the test1 data bank puts the phone number in
    value = context.getValue("Generated Data Source", "Test 2: prefix");
    mdnValue = context.getValue("AccountData", "mdn");

    # parse the number to get all the sub components
    areaCode = (str(value))[0:3]
    phoneNumber1 = (str(value))[3:6]
    phoneNumber2 = (str(value))[6:10]

    areaCode2 = (str(mdnValue))[0:3]
    phoneNumber11 = (str(mdnValue))[3:6]
    phoneNumber22 = (str(mdnValue))[6:10]

    # Store the formatted phone number into the map of data banked values.
    # Modify the string "Test 3: prefix" to make it point to the data banked column name. In my test case, the phone number
    # was stored under the name "Test 3: prefix"
    valueMap = context.getGeneratedDataSourceValues()
    valueMap.put("Test 2: prefix", "(%s)%s-%s" % (areaCode, phoneNumber1, phoneNumber2))

    valueMap2 = context.getAccountDataValues()
    valueMap2.put("mdn", "(%s)%s-%s" % (areaCode2, phoneNumber11, phoneNumber22))  
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Curtis,

    You should be able to get the generated data sources ok as you have done, but you'll need to add one thing to get the writable data source values. In the python implementation, add a second method called addDataSources which returns an array of the needed data source names.
    CODE
    def addDataSources():
       return "AccountData" This is needed so that you can do this: mdnValue = context.getValue("AccountData", "mdn");

    If you need to use multiple data sources in a single method, you can do something like the following:
    CODEdef addDataSources():
       return ["AccountData", "OtherDataSourceName"]

    Also, instead of doing this: valueMap2 = context.getAccountDataValues()
    valueMap2.put("mdn", "(%s)%s-%s" % (areaCode2, phoneNumber11, phoneNumber22)) You'll want to do something like, CODEcontext.put("mdn", "(%s)%s-%s" % (areaCode2, phoneNumber11, phoneNumber22))

    Test this out and see if your able to get the behavior your looking for.

    -Mike
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    worked like a champ sir!!

    thanks again,

    Curtis