• Instructions to Ask a Question

    Click on the "Ask a Question" button and select the application for which you would like to ask questions.

    We have 5 different products namely - Pabbly Connect, Pabbly Subscription Billing, Pabbly Email Marketing, Pabbly Form Builder, Pabbly Email Verification.

    The turnaround time is 24 hrs (Business Hours - 10.00 AM to 6.00 PM IST, Except Saturday and Sunday). So your kind patience will be highly appreciated!

    🚀🚀Exclusive Discount Offer

    Just in case you're looking for any ongoing offers on Pabbly, you can check the one-time offers listed below. You just need to pay once and use the application forever -
     

    🔥 Pabbly Connect One Time Plan for $249 (🏆Lifetime Access) -  View offer 

    🔥 Pabbly Subscription Billing One Time Plan for $249 (🏆Lifetime Access) - View offer

Python Code Issue

Hi Support,
I am really not happy with Python code step.
I am facing many with Pabbly Python code. I am using Pycharm editor to manage my Python code. The code runs smoothly in my pycharm. When I paste and save the same code, it runs sometimes on "Save and Send Test Request" submit. Python code indentation also unintended when I refresh and open the same step again. However, If I press "Save and Send Test Request" button again it throws an error "Internal server" or sometimes an indentation error not sure why. It is a very frustrating and time-consuming process. I wasted my whole day on it.

import requests
import datetime
import json

SCIENER_CLIENT_ID = '{{ScienerClientId}}'
SCIENER_CLIENT_SECRET = '{{ScienerClientSecret}}'
SCIENER_USERNAME = '{{ScienerUsername}}'
SCIENER_PASSWORD = '{{ScienerPassword}}'


def convert_date_to_unix_ms(date_str, date_format):
try:
date_obj = datetime.datetime.strptime(date_str, date_format)
unix_time = date_obj.timestamp()
milliseconds = int(unix_time * 1000)
return milliseconds
except ValueError:
raise ValueError("Invalid date string or format")


def get_current_unix_ms():
current_time = datetime.datetime.now()
unix_timestamp = current_time.timestamp()
return int(unix_timestamp * 1000)


def sciener_get_lock_id_from_alias(access_token, lock_alias):
api_url = "https://euapi.sciener.com/v3/lock/list"
data = {
"clientId": SCIENER_CLIENT_ID,
"accessToken": access_token,
"lockAlias": lock_alias,
"pageNo": 1,
"pageSize": 20,
"date": get_current_unix_ms(),
}
api_response = requests.get(api_url, params=data)
if api_response.status_code == 200:
lock_list = api_response.json()["list"]
if lock_list:
return lock_list[0]["lockId"]
else:
return False


def sciener_set_custom_passcode(access_token, keyboard_pwd, keyboard_pwd_name, lock_alias, start_date, end_date):
lock_id = sciener_get_lock_id_from_alias(access_token, lock_alias)

if lock_id:
api_url = "https://euapi.sciener.com/v3/keyboardPwd/add"
data = {
"clientId": SCIENER_CLIENT_ID,
"accessToken": access_token,
"lockId": lock_id,
"keyboardPwd": keyboard_pwd,
"keyboardPwdName": keyboard_pwd_name,
"startDate": start_date,
"endDate": end_date,
"addType": 2,
"date": get_current_unix_ms(),
}
api_response = requests.post(api_url, data=data, headers={})
if api_response.status_code == 200:
return api_response.json()


reservation_date_format = "%d/%m/%Y %H:%M:%S"
counter = 0
rcode = ''
rooms = json.loads('')
reserved_rooms = json.loads('')
passcode_creation = ''
new_passcode = ''
sciener_access_token = ''
for reserved_room in reserved_rooms:
for room in rooms:
if room["id"] == reserved_room["id_zak_room"]:
start_dt = reserved_room["dfrom"] + ' ' + "14:55:00"
if passcode_creation == "now":
start_dt_ms = get_current_unix_ms()
else:
start_dt_ms = convert_date_to_unix_ms(start_dt, reservation_date_format)

end_dt_ms = int(start_dt_ms + 11 * 60 * 60 * 1000)
sciener_set_custom_passcode(sciener_access_token, new_passcode, rcode, 101, start_dt_ms, end_dt_ms)
counter += 1

print(json.dumps({"success": "true", "passcode": new_passcode}))

Code unintended code
Screenshot from 2024-04-19 19-15-16.png


Please help me to fix all those problem so I can focus more on production instead of wasting my time on indentation. How I can check the detail error if I get "Internal server error"?


Thanks,
Jitesh
 

Supreme

Well-known member
Staff member
The API you're trying to run on Code by Pabbly is not supported there. Kindly try converting it into a REST API format and then use it with API by Pabbly. That should work for your needs.


Further, we currently have only a limited library of Python on the platform. You can check on the following thread.

 
Thanks!
I already split Python code and integrated the API with the Pabbly API. I still don't have an answer as to why Python code indentation is lost upon "Save or "Save and Test Request"
 

Supreme

Well-known member
Staff member
I already split Python code and integrated the API with the Pabbly API. I still don't have an answer as to why Python code indentation is lost upon "Save or "Save and Test Request"
Because the library you are trying to import in the code is actually not supported on the Code by Pabbly.

You can check the further details here -

 
Is it my Editor/IDE issue or Pabbly code editor issue? I use your recommended editor Pycharm which is well-known for Python programming.
 

Supreme

Well-known member
Staff member
If you're attempting to execute the API request on Code By Pabbly, I regret to inform you that it won't function as expected.
 
New Python code just does basic things. It doesn't have REST API integration at all. Did you get a chance to look at my workflow? If you have time then please check my above given workflow. As I mentioned in my previous comment, I already split my Python code and I am using Pabbly API action to perform REST API.
Do you think Python code indentation is related to REST API integration? It must be something else. Can you check my Python code in the above workflow? If everything looks good to you let me know.
 

Supreme

Well-known member
Staff member
Hey @jitesh_tandel

We have tested your used code on this web app and we are getting the following error - https://www.online-python.com/

Traceback (most recent call last): File "main.py", line 82, in <module> rooms = json.loads('{{ZAKRooms}}') File "/usr/lib/python3.8/json/__init__.py", line 357, in loads return _default_decoder.decode(s) File "/usr/lib/python3.8/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.8/json/decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) ** Process exited - Return Code: 1 **

So, can you please correct once which can be run on that application that we can try to process on Code By Pabbly at our end?
 

Supreme

Well-known member
Staff member
Additionally, could you please test this code on the web application available at https://www.online-python.com/ and input all the required information, including the client ID, secret, username, and password? After running the code successfully there, copy it and paste it into Code By Pabbly.

import requests import datetime import json SCIENER_CLIENT_ID = '{{ScienerClientId}}' SCIENER_CLIENT_SECRET = '{{ScienerClientSecret}}' SCIENER_USERNAME = '{{ScienerUsername}}' SCIENER_PASSWORD = '{{ScienerPassword}}' def convert_date_to_unix_ms(date_str, date_format): try: date_obj = datetime.datetime.strptime(date_str, date_format) unix_time = date_obj.timestamp() milliseconds = int(unix_time * 1000) return milliseconds except ValueError: raise ValueError("Invalid date string or format") def get_current_unix_ms(): current_time = datetime.datetime.now() unix_timestamp = current_time.timestamp() return int(unix_timestamp * 1000) def sciener_get_lock_id_from_alias(access_token, lock_alias): api_url = "[URL]https://euapi.sciener.com/v3/lock/list[/URL]" data = { "clientId": SCIENER_CLIENT_ID, "accessToken": access_token, "lockAlias": lock_alias, "pageNo": 1, "pageSize": 20, "date": get_current_unix_ms(), } api_response = requests.get(api_url, params=data) if api_response.status_code == 200: lock_list = api_response.json()["list"] if lock_list: return lock_list[0]["lockId"] return False def sciener_set_custom_passcode(access_token, keyboard_pwd, keyboard_pwd_name, lock_alias, start_date, end_date): lock_id = sciener_get_lock_id_from_alias(access_token, lock_alias) if lock_id: api_url = "[URL]https://euapi.sciener.com/v3/keyboardPwd/add[/URL]" data = { "clientId": SCIENER_CLIENT_ID, "accessToken": access_token, "lockId": lock_id, "keyboardPwd": keyboard_pwd, "keyboardPwdName": keyboard_pwd_name, "startDate": start_date, "endDate": end_date, "addType": 2, "date": get_current_unix_ms(), } api_response = [URL='http://requests.post']requests.post[/URL](api_url, data=data) if api_response.status_code == 200: return api_response.json() reservation_date_format = "%d/%m/%Y %H:%M:%S" counter = 0 rcode = '16486504' passcode_creation = 'now' reserved_rooms = json.loads('[{"id_zak_room":37983,"id_zak_reservation_room":20609298,"door_code":null,"id_zak_room_type":13227,"dfrom":"07\/12\/2023","dto":"08\/12\/2023","occupancy":{"adults":2,"teens":0,"children":0,"babies":0},"product_id":"1095","rate_id":"111","customers":[{"checkin":null,"departed":"08/12/2023","checkout":"08/12/2023","id":22008640}]}]') rooms = json.loads('{{ZAKRooms}}') new_passcode = generate_random_passcode() sciener_access_token = 'e1a51bea94648f213db804828b23db5f' for reserved_room in reserved_rooms: for room in rooms: if room["id"] == reserved_room["id_zak_room"]: start_dt = reserved_room["dfrom"] + ' ' + "14:55:00" if passcode_creation == "now": start_dt_ms = get_current_unix_ms() else: start_dt_ms = convert_date_to_unix_ms(start_dt, reservation_date_format) end_dt_ms = int(start_dt_ms + 11 * 60 * 60 * 1000) sciener_set_custom_passcode( sciener_access_token, new_passcode, rcode, 101, start_dt_ms, end_dt_ms ) counter += 1 print(json.dumps({"success": "true", "passcode": new_passcode}))
 
I can do the test. I don't mind at all but again my concern is why the source code looks unindent. I always paste indented source code and the pabbly makes my code unindent. If I need to make changes directly in the Pabbly code editor/textarea, the source code looks like a mess.
It is not like with the above code. It also happens with the small code. Check the below code. Python prefers formatted code. Am I doing any mistake? Do I need to use any other editor than Pycharm, or Visual Studio?

from datetime import datetime
import json

def convert_date_to_unix_ms(date_str, date_format):
date_obj = datetime.strptime(date_str, date_format)
unix_time = date_obj.timestamp()
milliseconds = int(unix_time * 1000)
return milliseconds

def get_current_unix_ms():
return int({{pc_unix_timestamp}} * 1000)

def zak_find_lock_id(key, value):
zak_rooms = {{ZAKRooms}}
for obj in zak_rooms:
if obj.get(key) == value:
return obj["lock_id"]
return {{blank}}

send_access_code = '1. Passcode Creation : now'
if send_access_code == "now":
start_dt_ms = get_current_unix_ms()
else:
start_dt = "8. Dfrom : 07/12/2023" + " 14:55:00"
start_dt_ms = convert_date_to_unix_ms(start_dt, "%d/%m/%Y %H:%M:%S")

end_dt = "8. Dto : 08/12/2023" + " 20:00:00"
end_dt_ms = convert_date_to_unix_ms(end_dt, "%d/%m/%Y %H:%M:%S")
lock_id = zak_find_lock_id("id", 8. Id Zak Room : 37983)
output = json.dumps({"lock_id": lock_id, "start_date": start_dt_ms, "end_date": end_dt_ms, "date": get_current_unix_ms()})
 
Can you please try that once and share the screenshot on the same? So that we can process it further.
(valid in site for 24 hours only)
Ignore above code. I have very very small python code now. As per your recommendation, I split my python into chunks of small codes. The below given code is copied from the python text editor. I also attached screenshot of the same source code. The below code is working pretty well so we do't need need to run in the other online python editor. I still don't have answer why the Pabbly python text editor breaks source code indentation. Do you have any solution on it? I will close this ticket immediately once you provide me the solution.
import json


current_time_ms = int({{pc_unix_timestamp}} * 1000)
send_access_code = ''
if send_access_code == "now":
start_dt_ms = current_time_ms
else:
start_dt_ms = 7. Result : 1701957300000

end_dt_ms = 8. Result : 1702062000000
output = json.dumps({"start_date": start_dt_ms, "end_date": end_dt_ms, "date": current_time_ms})

Screenshot from 2024-04-30 23-56-52.png
 
Ignore above code. I have very very small python code now. As per your recommendation, I split my python into chunks of small codes. The below given code is copied from the python text editor. I also attached screenshot of the same source code. The below code is working pretty well so we do't need need to run in the other online python editor. I still don't have answer why the Pabbly python text editor breaks source code indentation. Do you have any solution on it? I will close this ticket immediately once you provide me the solution.


View attachment 41651
Here is the recorded video for the same issue.
 
Top