I have been trying to figure out what is the best performance wise and also best practice to get data from a dict using Python. I currently have a data that looks like this:
data = {
"cart": {
"id": 123456,
"currency": "GBP",
"name": None,
"rows": [
{
"id": 7079540,
"unitPrice": 1089,
"unitVat": 217.8,
"quantity": 1,
"pass": None,
"extraData": {
"isLoggedIn": True,
"source": "product"
},
"product": {
"id": 279533,
"thumbnail": "\/images\/product\/279533?trim&h=80",
"name": "Seagate Game Drive f\u00f6r PS4 4TB",
"subTitle": "Extern H\u00e5rddisk 2,5\" \/ USB 3.0 \/ 4 TB",
"shippingClass": {
"id": 1,
"order": 1,
"prices": [
{
"price": 0,
"shippingMethodId": 1,
"isFixedPrice": False,
"maximumPackageSizeId": 2
},
{
"price": 0,
"shippingMethodId": 2,
"isFixedPrice": False,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 3,
"isFixedPrice": False,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 4,
"isFixedPrice": False,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 19,
"isFixedPrice": True,
"maximumPackageSizeId": 2
},
{
"price": 0,
"shippingMethodId": 20,
"isFixedPrice": False,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 21,
"isFixedPrice": False,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 22,
"isFixedPrice": True,
"maximumPackageSizeId": None
},
{
"price": 0,
"shippingMethodId": 25,
"isFixedPrice": True,
"maximumPackageSizeId": 2
},
{
"price": 0,
"shippingMethodId": 26,
"isFixedPrice": True,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 27,
"isFixedPrice": True,
"maximumPackageSizeId": 2
},
{
"price": 0,
"shippingMethodId": 28,
"isFixedPrice": True,
"maximumPackageSizeId": 2
}
]
},
"packageSizeId": 1,
"isShippable": True,
"release": {
"timestamp": 1512450000,
"format": "Y-m-d"
},
"regularPrice": {
"price": "1199.00",
"currency": "SEK",
"vat": 239.8,
"type": None,
"endAt": "2022-03-12",
"maxQtyPerCustomer": None
},
"isRevenue": True,
"stock": {
"web": 51,
"supplier": None,
"displayCap": "50",
"1": 51,
"2": 0,
"5": 10,
"8": 4,
"9": 4,
"10": 10,
"11": 7,
"14": 2,
"15": 23,
"16": 0,
"19": 0,
"20": 0,
"21": 0,
"22": 4,
"23": 0,
"26": 4,
"27": 18,
"28": 1
},
"meta": [
],
"statusCodes": [
],
"packageVolume": 879750,
"packageWeight": 340,
"minimumRankLevel": 0,
"packageMeasurements": [
170,
115,
45
]
},
"insurance": {
"id": 339702,
"name": "testing Care Pack (12 m\u00e5nader)",
"price": 289,
"provider": 1,
"length": 12
}
}
]
},
"corrections": [
{
"type": "PRICE_CHANGED",
"rowId": None,
"price": {
"was": 0,
"shouldBe": "1089.00"
}
}
],
"errors": [
]
}
and I am currently trying to get specific data that I store into a dict and return the dict data. This is what I have done:
import pendulum
TESTING_STORES = {
'web': 'Webblager',
'supplier': 'Hos leverantör',
'1': 'Hello World',
}
data = {
"cart": {
"id": 2656218,
"currency": "SEK",
"name": None,
"rows": [
{
"id": 7079540,
"unitPrice": 1089,
"unitVat": 217.8,
"quantity": 1,
"pass": None,
"extraData": {
"isLoggedIn": True,
"source": "product"
},
"product": {
"id": 279533,
"thumbnail": "\/images\/product\/279533?trim&h=80",
"name": "Seagate Game Drive f\u00f6r PS4 4TB",
"subTitle": "Extern H\u00e5rddisk 2,5\" \/ USB 3.0 \/ 4 TB",
"shippingClass": {
"id": 1,
"order": 1,
"prices": [
{
"price": 0,
"shippingMethodId": 1,
"isFixedPrice": False,
"maximumPackageSizeId": 2
},
{
"price": 0,
"shippingMethodId": 2,
"isFixedPrice": False,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 3,
"isFixedPrice": False,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 4,
"isFixedPrice": False,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 19,
"isFixedPrice": True,
"maximumPackageSizeId": 2
},
{
"price": 0,
"shippingMethodId": 20,
"isFixedPrice": False,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 21,
"isFixedPrice": False,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 22,
"isFixedPrice": True,
"maximumPackageSizeId": None
},
{
"price": 0,
"shippingMethodId": 25,
"isFixedPrice": True,
"maximumPackageSizeId": 2
},
{
"price": 0,
"shippingMethodId": 26,
"isFixedPrice": True,
"maximumPackageSizeId": 7
},
{
"price": 0,
"shippingMethodId": 27,
"isFixedPrice": True,
"maximumPackageSizeId": 2
},
{
"price": 0,
"shippingMethodId": 28,
"isFixedPrice": True,
"maximumPackageSizeId": 2
}
]
},
"packageSizeId": 1,
"isShippable": True,
"release": {
"timestamp": 1512450000,
"format": "Y-m-d"
},
"regularPrice": {
"price": "1199.00",
"currency": "SEK",
"vat": 239.8,
"type": None,
"endAt": "2022-03-12",
"maxQtyPerCustomer": None
},
"isRevenue": True,
"stock": {
"web": 51,
"supplier": None,
"displayCap": "50",
"1": 51,
"2": 0,
"5": 10,
"8": 4,
"9": 4,
"10": 10,
"11": 7,
"14": 2,
"15": 23,
"16": 0,
"19": 0,
"20": 0,
"21": 0,
"22": 4,
"23": 0,
"26": 4,
"27": 18,
"28": 1
},
"meta": [
],
"statusCodes": [
],
"packageVolume": 879750,
"packageWeight": 340,
"minimumRankLevel": 0,
"packageMeasurements": [
170,
115,
45
]
},
"insurance": {
"id": 339702,
"name": "testing Care Pack (12 m\u00e5nader)",
"price": 289,
"provider": 1,
"length": 12
}
}
]
},
"corrections": [
{
"type": "PRICE_CHANGED",
"rowId": None,
"price": {
"was": 0,
"shouldBe": "1089.00"
}
}
],
"errors": [
]
}
# --------------------------------------------------------------- #
# GET JSON DATA
# --------------------------------------------------------------- #
pageData = {
'title': 'Unknown',
'image': 'Not found',
'price': 'Not found',
'sizes': {}
}
def get_data():
if doc := data.get('cart', {}).get('rows')[0].get('product', {}):
# ---------------------------- ALL ---------------------------- #
pageData.update({
'title': doc.get('name'),
'image': f"https://cdn.testing.com/images/product/{doc.get('id')}?trim&w=231",
'price': doc.get('regularPrice', {}).get('price', {}),
})
# ---------------------------- Release date ---------------------------- #
if release_date := doc.get('release', {}).get('timestamp', {}):
if not pendulum.from_timestamp(release_date, tz='Europe/Stockholm').is_past():
pageData['release_date'] = release_date
# ---------------------------- Sizes ---------------------------- #
if sizes := doc.get('stock'):
pageData['sizes'] = {TESTING_STORES[store]: stock for store, stock in sizes.items() if
store in TESTING_STORES and stock}
if status := sizes.get('orders', {}).get('CL', {}):
if not status.get('status', 0) == -1:
pageData['sizes']['Potential InStock'] = status.get('ordered')
# ---------------------------- Campaign ---------------------------- #
if doc.get('regularPrice', {}).get('type', '') == 'campaign':
pageData['sizes']['InStock'] = None
# ---------------------------- minimumRankLevel ---------------------------- #
if minimumRankLevel := doc.get('minimumRankLevel', {}):
pageData['minimumRankLevel'] = minimumRankLevel
return pageData
if __name__ == '__main__':
data = get_data()
print(data)
The script itself works but I do also feel like I have done it incorrectly by not applying the best practice and I do know that using .get()
is not the best performance compare to if in
but also I do not know e.g. what if its a nested data I want to use.. how to apply it etc etc... my goal is to make the script to have the best performance wise and trying to keep it as simple as possible as well.
I wonder, if there is anything I could improve in my code?