Skip to main content

Improved Python Code Samples

· 5 min read

The generated code samples are now idiomatic with no errors. This improves the developer experience by allowing developers to run the code samples without any error in their applications and to get started quickly.

Details

This release introduces significant improvements to the code samples provided for your API. We've made several updates to the code samples to ensure that they are up-to-date, easier to understand, and more user-friendly. The new code samples are designed to help developers get started with your API quickly and easily, without having to spend a lot of time reading documentation or trying to figure out how to use the API.

What Has Changed?

To ensure compliance with the required standards, the code samples are analyzed and linted using tools mypy and ruff. The configurations used for these tools are publicly available here.

  • Removed unused imports for models and utility classes

  • Fixed bugs and errors in the code samples to ensure that they work as expected

  • Fixed linting issues in the updated code samples to ensure that they comply with the best practices in terms of coding styles and standards

Parameters Declaration Section Improvements

Model Initialization

Made the model's field initialization in-line which ensures that the object structure is intact, enhancing the code readability.

model = Employee(
name='john',
hired_at=APIHelper.HttpDateTime.from_value('Mon, 15 Jun 2009 20:45:30 GMT').datetime,
boss=Person(
name='doe',
age=25
)
)

Array Initialization

Example 1
number_array = [
210,
211,
212
]
Example 2
number_array_of_map = [
{
"key0": 216
},
{
"key0": 217,
"key1": 218
}
]

Map Initialization

Example 1
number_map = {
"key0": 110,
"key1": 111,
"key2": 112
}
Example 2
number_map_of_array = {
"key0": [
0,
1
],
"key1": [
1,
2,
3
],
"key2": [
2
]
}

Multi-dimensional Array Initialization

numbers_2_d_array = [
[
158,
258
],
[
698,
14
]
]

Imports Section Improvements

Previously, imports were added even if they were not required in any other sections, but now we are adding imports on demand for optional parameters.

from tester.models.suite_code import SuiteCode
from tester.tester_client import TesterClient
from tester.configuration import Environment

client = TesterClient(
environment=Environment.PRODUCTION,
suites =SuiteCode.SPADES
)

After removing the Optional parameter environment from the above code sample, it would look like:

# By removing the Environment parameter, Environment import is also removed from import section

#import section
from tester.models.suite_code import SuiteCode
from tester.tester_client import TesterClient

client = TesterClient(
suites =SuiteCode.SPADES
)

Client Initialization Section Improvements

Addressed the trailing comma after the last configuration parameter when initializing the client.

client = TesterClient(
suites=SuiteCode.HEARTS
)

API Call Section Improvements

API call is now reformatted to make it style-complaint.

try:
result = response_types_controller.send(
param1,
param2,
param3
)
print(result)
except APIException as e:
print(e)

A Comparison of Complete Code Sample

The following code shows the complete comparison between the code sample generation for initializing an employee model as an input parameter for ResponseTypesController Send endpoint.

Employee Model

"employee_model": {
"name": "john",
"hiredAt": "Mon, 15 Jun 2009 20:45:30 GMT",
"salary": null,
"boss": {
"name": "doe",
"age": 25
}
}
from tester.exceptions.api_exception import APIException
from tester.api_helper import APIHelper
from tester.models.employee import Employee
from tester.models.person import Person
from tester.tester_client import TesterClient

client = TesterClient()

response_types_controller = client.response_types
model = Employee(
name='john',
hired_at=APIHelper.HttpDateTime.from_value('Mon, 15 Jun 2009 20:45:30 GMT').datetime,
boss=Person(
name='doe',
age=25
)
)

try:
result = response_types_controller.send(model)
print(result)
except APIException as e:
print(e)

Bugs Fixed

  • Fixed an issue with DateTime initialization for Unix Timestamp formats
  • Fixed multiple indentation issues in parameters initialization section
  • Fixed an issue with OAuth 2.0 token initialization
  • Fixed issues with File and Binary type request parameters initialization