Categories
Mastering Development

How does this recursive Python code work?

I came across this recursive function that’s supposed flatten a dictionary: def flatten(data, prefix=”, separator=’.’): """Flattens a nested dict structure. """ if not isinstance(data, dict): return {prefix: data} if prefix else data result = {} for (key, value) in data.items(): result.update(flatten(value,_get_new_prefix(prefix, key, separator), separator=separator)) return result def _get_new_prefix(prefix, key, separator): return (separator.join((prefix, str(key))) if prefix […]

Categories
Mastering Development

How to compare two foreach loop data

I am trying to compare two foreach loop data and display relevant data in a div. As in the following script its comparing data for only first row. More details: I want to track a user arrival between two time ranges of the current date, I am fetching user name and,city and arrival time from […]

Categories
Mastering Development

Is there any way to render value on the basis of clicked item from an array in react using router?

My List comp simply renders me the list of a cricketer from a json file on click of a button from Home comp. When I am clicking on the button I am able to see list of cricketer name but unable to obtain detailList comp on clicking list results. Please help me since I am […]

Categories
Mastering Development

Push objects from MongoDB to an array located in React Hook

I’m struggling to understand the logic behind adding an object to an array defined within a React hook. I am getting an object from my database which returns a JSON, which you can see at the bottom of this post. I am trying to access that data using axios, which is connected to my backend […]

Categories
Mastering Development

Duplicates string in dictionary of json file cannot be parsed onto pair of key/value

I have problem to parse some of string associate with the ‘details’ key. the value of details key have duplicates string that should be extract as a pair of key/value. This is sample of json data { “response”: { “client_log”: { “data”: [ { “login”: “AAAAAAAAAAAAAA”, “state”: “MC”, “details”: “Please find report below:\r\n\r\n——Report Information——\r\n\r\nEmail Id: […]

Categories
Development

API response data array count is lesser than actual count in Rest Assured Java

I need to count the API response data array. Below is the code for the count of the array. ArrayList<Map<String,Object>> jsonList = response.jsonPath().get(“data”); int i = jsonList.get(0).size(); System.out.println(“Data Count in response : ” +i); The response of the API is below { “response”: { “code”: 200, “status”: “success”, “alert”: [ { “message”: “Success”, “type”: “success”, […]

Categories
Development

Guzzle POST request always returns 400 Bad Request

I have been trying to make a simple POST request to an endpoint with a payload using Guzzle but I always get 400 Bad Request returned. I can make the same request in Postman and it works. Also, If I make the request using cURL it works. Can anyone tell from my code what I […]

Categories
Development Django

How to return only the relevant related objects in Django REST Framework

I am trying to create an API such that when I give it a keyword it will return all the cinemas that have movies containing said keyword. Right now I am using: queryset = Cinema.objects.filter(movies__title__icontains = keyword) My serializer is: class CinemaSerializer(serializers.ModelSerializer): class Meta: model = Cinema fields = (‘id’, ‘name’, ‘city’, ‘movies’) depth = […]

Categories
Development

Expression Type ‘String’ cannot be used as an index type

I started learning typescript, so I’m creating situations to resolve, I’m getting error not sure why, see code above: interface IObjct { name: string, city: string, age: number, } const john = { name: “John”, city: “London”, age: 24 }; const jerry = { name: “Jerry”, city: “Rome”, age: 43 }; function filterPerson(arr: Array<IObjct>, term: […]

Categories
Development

How can I convert an array to an object by splitting strings?

I have an array like below: [“gender-m”, “age-20”, “city-london”, “lang-en”, “support-home”] I tried to generate a JSON object: {“gender”:”m”, “age”:”20″, “city”:”london”, “lang”:”en”, “support”:”home”} One solution I can think of is using FOR loop to make it, but I am sure there are elegant solutions for this. Any suggestions, please help me.