# Python scripting questions

**URL:** <https://community.dreamfactory.com/t/python-scripting-questions/2724>\
**Category:** Server-Side Scripting\
**Created:** [May 17, 2016, 10:25pm UTC](https://community.dreamfactory.com/t/python-scripting-questions/2724 "2016-05-17T22:25:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marksim](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@marksim](https://community.dreamfactory.com/u/marksim)\
**Post date:** [May 17, 2016, 10:25pm UTC](https://community.dreamfactory.com/t/python-scripting-questions/2724/1 "2016-05-17T22:25:25Z")

</div>

We’ve been trying out some of the Python scripting, which is currently available in 2.1.2 (in beta).

Just wanted to point out a few things we found (not sure if some of the behavior is because still in beta or not).

In a table POST pre-process script, we have the following:

```
verb = event['request']['method']; # always returns POST in this example
payload = event['request']['payload'];
records = payload['resource']; #all records in the post
record = records[0]; # first record

```

1. Verifying fieldnames in the post

The following code using payload - similar to the tutorial code - doesn’t work:

```
if 'organization_id' not in payload: # this doesn't work
       raise ValueError('payload: Missing field organization_id');

```

Looking in the specific record does work:

```
    if 'organization_id' not in record:
        raise ValueError('record: Missing field organization_id');

```

1. Best practice for referencing a specific key/value in a record?

If you directly try to access a key- e.g., lastname = record[‘lastname’] - it works if the key exists. If not, the script stops and the post is completed (any other code is ignored and no error is generated). Is this going to be the default behavior or should this error be caught?

The above can be prevented by checking the key first, but this does have to be done for every field:

```
if 'lastname' in record:
    lastname = record['lastname'];

```

Is there a better way to obtain a specific record key?

1. Integer/numeric values

If a POST has an integer/numeric value, it becomes a string in the payload. For example, we post {“organization\_id” =99} and in the script, need to convert to an integer:

```
if int(record['organization_id'] or -1) < 0 :
    raise ValueError('ERROR id is less than 0 ');

```

Should all values be string?

Thanks,

Mark

---

<div class="post-metadata">

**Author:** ![aislam](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/aislam/32/421_2.png) [@aislam](https://community.dreamfactory.com/u/aislam)\
**Post date:** [May 18, 2016, 5:08am UTC](https://community.dreamfactory.com/t/python-scripting-questions/2724/2 "2016-05-18T05:08:52Z")

</div>

In 2.1.2 `event` and `platform` are both Python [dictionary](https://docs.python.org/2/tutorial/datastructures.html#dictionaries) type. You need to use them as regular dictionary. So, `organization_id` is not going to exist in `payload` but will exist in `payload['resource']`. In the up coming release of DF2, `event` and `platform` will be Python [bunch](https://pypi.python.org/pypi/bunch/1.0.1) type. This will allow easy access to values using dot (.) notation like in JavaScript. You will be able to do the following

```
verb = event.request.method; # always returns POST in this example
payload = event.request.payload;
records = payload.resource; #all records in the post
record = records[0]; # first record

```

Directly accessing a key that doesn’t exist from Python dictionary will throw error, therefore, you need to check the key before accessing it. Unfortunately there isn’t a better way to do this currently or at least I don’t know of one. You could do this in one line like below which is a bit shorter but not really a better way.

```
lastname = record['last name'] if('lastname' in record) else '';

```

All values coming from PHP side are going to be string.

---

<div class="post-metadata">

**Author:** ![marksim](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@marksim](https://community.dreamfactory.com/u/marksim)\
**Post date:** [May 18, 2016, 1:29pm UTC](https://community.dreamfactory.com/t/python-scripting-questions/2724/3 "2016-05-18T13:29:04Z")

</div>

Aislam,

Thanks for the information! Just to be clear, when event is made into bunch type, will the dictionary style code we need to use now still work since bunch is subclass of dict?

---

<div class="post-metadata">

**Author:** ![aislam](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/aislam/32/421_2.png) [@aislam](https://community.dreamfactory.com/u/aislam)\
**Post date:** [May 18, 2016, 4:33pm UTC](https://community.dreamfactory.com/t/python-scripting-questions/2724/4 "2016-05-18T16:33:47Z")

</div>

Yes, the dictionary style code will still work with bunch type.
