Finding duplicates from array using Python - python

I'm quite new to Python and was wondering if there was a good way to create a new list of unduplicated users.
My problem is that I have something like this
[
{
"userId": "987654321",
"method": "CARD",
"lastDigits": "1234",
"type": "mc",
"first_name": "Leroy",
"last_name": "Jenkins",
"exp": "01/23"
},
{
"userId": "987654321",
"method": "PAYPAL",
"first_name": "Leroy",
"last_name": "Jenkins"
},
{
"userId": "123456789",
"method": "CARD",
"lastDigits": "4567",
"type": "visa",
"first_name": "Joe",
"last_name": "Bloggs",
"exp": "01/25"
},
{
"userId": "46513498000",
"method": "PAYPAL",
"first_name": "Betty",
"last_name": "White"
}
]
Basically I need to match when the userId has matched and keep the object when "method": "CARD"instead of PAYPAL then reconstruct essentially the same list again but minus the duplicate userId's when the user has both CARD and PAYPAL
::EDIT::
User can just have PAYPAL. and if it does just have PAYPAL, just return that
example output needed
[
{
"userId": "987654321",
"method": "CARD",
"lastDigits": "1234",
"type": "mc",
"first_name": "Leroy",
"last_name": "Jenkins",
"exp": "01/23"
},
{
"userId": "123456789",
"method": "CARD",
"lastDigits": "4567",
"type": "visa",
"first_name": "Joe",
"last_name": "Bloggs",
"exp": "01/25"
},
{
"userId": "46513498000",
"method": "PAYPAL",
"first_name": "Betty",
"last_name": "White"
}
]

This will perfectly work for your. Try and check it
mylist=[
{
"userId": "987654321",
"method": "CARD",
"lastDigits": "1234",
"type": "mc",
"first_name": "Leroy",
"last_name": "Jenkins",
"exp": "01/23"
},
{
"userId": "987654321",
"method": "PAYPAL",
"first_name": "Leroy",
"last_name": "Jenkins"
},
{
"userId": "123456789",
"method": "CARD",
"lastDigits": "4567",
"type": "visa",
"first_name": "Joe",
"last_name": "Bloggs",
"exp": "01/25"
},
{
"userId": "46513498000",
"method": "PAYPAL",
"first_name": "Betty",
"last_name": "White"
}
]
temp_list=[]
temp_id=[]
for x in mylist:
if int(x['userId']) not in temp_id:
temp_list.append(x)
temp_id.append(int(x["userId"]))
print(temp_list)

If I don't misunderstood your question then simple filtering will do the trick for you,
user_ids = []
final_users = []
for user in users:
user_ids.append(int(user['userId']))
if user['userId'] not in user_ids and user['method'] != 'PAYPAL':
final_users.append(user)
print(final_users)
Working Code: https://rextester.com/COBGVU63922

users = {}
for d in user_list:
uid = d["userId"]
# If user id not in users, we add it
if uid not in users:
users[uid] = d
# Otherwise we check if the already recorded method was "PAYPAL",
# if so we overwrite it.
elif users[uid]["method"] == "PAYPAL":
users[uid] = d
# To convert dict we just created back to list:
user_list = list(users.values())

Use defaultdict:
from collections import defaultdict
newdata = defaultdict(dict)
for item in data:
userid = newdata[item['userId']]
if userid == {} and item['method'] == 'CARD':
userid.update(item)
Output:
# newdata = list(newdata.values())
>>> newdata
[{'userId': '987654321',
'method': 'CARD',
'lastDigits': '1234',
'type': 'mc',
'first_name': 'Leroy',
'last_name': 'Jenkins',
'exp': '01/23'},
{'userId': '123456789',
'method': 'CARD',
'lastDigits': '4567',
'type': 'visa',
'first_name': 'Joe',
'last_name': 'Bloggs',
'exp': '01/25'}]

Related

Importing a JSON object to a Google Cloud SQL instance authentication error

I have been stuck on this for the last 2 days, first time in a long time I had to use stackoverflow. But I love this community
What I am trying to do?
I am trying to upload a JSON object to update a table in a cloud SQL instance.
Error I am getting
I have tried using a service account and 02Auth credentials but I still get these errors.
Code below
from pprint import pprint
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from google.oauth2 import service_account
import google.auth.credentials
credentials = GoogleCredentials.get_application_default()
quota_project_id = "alert-groove-360914"
scopes = ["https://www.googleapis.com/auth/cloud-platform","https://www.googleapis.com/auth/sqlservice.admin"]
#credentials, _ = google.auth.load_credentials_from_file(
# "alert-groove-360914-0d5821a7921c.json", scopes=scopes, quota_project_id=quota_project_id
# )
#credentials = service_account.Credentials.from_service_account_file("alert-groove-360914-0d5821a7921c.json")
service = discovery.build('sqladmin', 'v1beta4', credentials=credentials)
# Project ID of the project that contains the instance.
project = 'XXXXXXXX' # TODO: Update placeholder value.
# Cloud SQL instance ID. This does not include the project ID.
instance = 'employment-types' # TODO: Update placeholder value.
database_instance_body = {
"id": 3,
"first_name": "XXXXXXXX",
"last_name": "XXXXXXX",
"email": "XXXXXX28#gmail.com",
"mobile_number": "XXXXXXXXX",
"country_id": 224,
"new_user": False,
"status": 1,
"reason": 'null',
"duration": 'null',
"linkedin_id": 'null',
"sync_contacts": False,
"linkedin_sync": False,
"available": True,
"radar_visibility": 'null',
"details": {
"organization": "Foras",
"isSelfEmployed": False,
"gender_id": 2,
"location_id": 457,
"gender": 'null',
"about": "The best around and nothing will ever get me down",
"image": "https:\/\/foras.tedmob.com\/storage\/users\/image-8596.jpg",
"lat": "25.20082000253009",
"long": "55.280527271443894",
"Gender_Rel": "Male",
"job_title": "Engineer",
"Experience": "10+ years",
"Location": "Dubai"
},
"interests": [
{
"id": 106,
"title": "Nightlife",
"icon": "https:\/\/foras.tedmob.com\/storage\/interests\/basket-ball-5908.png",
"image": "https:\/\/foras.tedmob.com\/storage\/interests\/basket-ball-7960.png",
"category": {
"category": "Culture"
}
},
{
"id": 102,
"title": "Music",
"icon": "https:\/\/foras.tedmob.com\/storage\/interests\/basket-ball-5908.png",
"image": "https:\/\/foras.tedmob.com\/storage\/interests\/basket-ball-7960.png",
"category": {
"category": "Culture"
}
},
{
"id": 98,
"title": "Personal finance",
"icon": "https:\/\/foras.tedmob.com\/storage\/interests\/basket-ball-5908.png",
"image": "https:\/\/foras.tedmob.com\/storage\/interests\/basket-ball-7960.png",
"category": {
"category": "Finance"
}
},
{
"id": 101,
"title": "Crypto currency ",
"icon": "https:\/\/foras.tedmob.com\/storage\/interests\/basket-ball-5908.png",
"image": "https:\/\/foras.tedmob.com\/storage\/interests\/basket-ball-7960.png",
"category": {
"category": "Finance"
}
},
{
"id": 104,
"title": "Breaking",
"icon": "https:\/\/foras.tedmob.com\/storage\/interests\/basket-ball-5908.png",
"image": "https:\/\/foras.tedmob.com\/storage\/interests\/basket-ball-7960.png",
"category": {
"category": "Culture"
}
}
],
"languages": [],
"goals": [
{
"id": 2,
"title": "Find a new job",
"icon": "https:\/\/foras.tedmob.com\/storage\/goals\/find-a-new-job-6804.png",
"pivot": {
"user_id": 3,
"goal_id": 2
},
"category": {
"category": "Career"
}
},
{
"id": 11,
"title": "Explore a career change",
"icon": "https:\/\/foras.tedmob.com\/storage\/goals\/explore-a-career-change-5885.png",
"pivot": {
"user_id": 3,
"goal_id": 11
},
"category": {
"category": "Career"
}
},
{
"id": 12,
"title": "Find co-founders",
"icon": "https:\/\/foras.tedmob.com\/storage\/goals\/find-co-founders-2170.png",
"pivot": {
"user_id": 3,
"goal_id": 12
},
"category": {
"category": "Networking"
}
}
],
"country": {
"id": 224,
"name": "United Arab Emirates",
"iso": "AE",
"phone_code": "+971"
},
"academic_levels": [
{
"id": 4,
"title": "Master's degree",
"pivot": {
"user_id": 3,
"academic_level_id": 4
}
}
],
"experiences": [
{
"id": 1,
"title": "test1",
"company_name": "test",
"isCurrentlyWorking": True,
"start_date": "2021-06-15 00:00:00",
"end_date": "2022-09-13 00:00:00",
"end_position": True,
"headline": "test",
"industry": "test",
"description": "description",
"employment_type": 1
}
]
}
request = service.instances().update(project=project, instance=instance, body=database_instance_body)
request.execute()

How to assert data in a JSON array with Python

I am trying to automate some API endpoints, but the JSON response is an array of data. How can I assert a specific user with all his data inside that JSON array?
I am trying with:
assert {
"user": "test1",
"userName": "John Berner",
"userid": "1"
} in response.json()
The JSON response is:
{
"data": [
{
"user": "test1",
"userName": "John Berner",
"userid": "1"
},
{
"user": "test2",
"userName": "Nick Morris",
"userid": "2"
}
],
"metadata": {
"current_page": 1,
"pages": 1,
"per_page": 100,
"total": 2
}
}
Please try like this:
You can use loop over the data within any to perform this check.
contents = json.loads(apiresponse_data)
assert any(i['user'] == 'test1' for i in contents['data'])
If all the fields are in the response are part of your user_info you can do what you are thinking of doing -
# response = json.loads(api_response_data)
response = {
"data": [
{
"user": "test1",
"userName": "John Berner",
"userid": "1"
},
{
"user": "test2",
"userName": "Nick Morris",
"userid": "2"
}
],
"metadata": {
"current_page": 1,
"pages": 1,
"per_page": 100,
"total": 2
}
}
user_info = {
"user": "test1",
"userName": "John Berner",
"userid": "1"
}
assert user_info in response['data']
Above doesn't raise AssertionError because the user_info is there in the response['data']
You can also use following if you have decoded the json response already -
assert {
"user": "test1",
"userName": "John Berner",
"userid": "1"
} in response['data']

How to write POST and PUT request with realtion in strapi.io in Python

Can't solve problem with POST and PUT request in strapi.io. My Relation is 1 to many (a ticket can have 1 category, a category can have many tickets).
GET request looks like this:
{
"data": {
"id": 1,
"attributes": {
"first_name": "Alex",
"last_name": "Porter",
"phone": "+74955978965",
"company": "Test",
"entry_time": null,
"entered": false,
"email": "test#test.com",
"count_entry": 0,
"createdAt": "2022-05-06T10:21:22.753Z",
"updatedAt": "2022-05-20T11:43:57.707Z",
"publishedAt": "2022-05-06T10:21:23.680Z",
"photo": {
"data": null
},
"category": {
"data": {
"id": 1,
"attributes": {
"name": "Junior",
"createdAt": "2022-05-20T11:36:46.360Z",
"updatedAt": "2022-05-20T11:37:17.517Z",
"publishedAt": "2022-05-20T11:37:17.516Z"
}
}
}
}
},
"meta": {}
}
This is what my function looks like with a PUT request:
def edit_member(self, member_id, first_name, last_name, phone, category):
url = config.SERVER + f'api/members/{member_id}?populate=%2A'
payload = {
'data': {
"attributes": {
"first_name": first_name,
"last_name": last_name,
"phone": phone,
},
"category": {
"data": {
"attributes": {
"name": category
}
}
}
}
}
headers = {
'Authorization': f'Bearer {config.TOKEN}',
'Content-Type': 'application/json'
}
requests.request("PUT", url, headers=headers, json=payload)
As a result, the data appears on the strapi, but the relation is lost (remains empty). Tell me, please, where to look?
Thank you in advance.

How to compare two list of dictionary and update value from another [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
list_1 = [
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Owner" },
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "21", "name": "abc", "email": "abc#network.com", "type": "Employ" },
{ "id": "21", "name": "abc", "email": "abc#network.com", "type": "Manager" },
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "100", "name": "last", "email": "last#network.com", "type": "Manager" }
]
list_2 = [
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "21", "name": "abc", "email": "abc#network.com", "type": "Employ" },
{ "id": "52", "name": "abcded", "email": "abcded#network.com", "type": "Manager" }
]
A preference dictionary {'Owner': 1, 'Manager':2, 'employ':3, 'HR': 4 }
There are two list of dictionaries
list_1 is the primary_dictionary, list_2 is the secondary_dictionary
I need to update the role in list_1 dictionary if the 'email' present in secondary dictionary with respect to check in preference dictionary
In the end output i should contain 'type' as in preference dictionary
If any emails match I want to update list one at all places with that email to contain a new role under'type'. for example if 123#gmail.com was in a dictionary within list2 then I want to change every item in list1 that contains 123#gmail.com as the email to have a role that is determined by a selection from the preference dictionary.
Expected out
[
{'id': '11', 'name': 'son', 'email': 'n#network.com', 'type': 'Owner'},
{'id': '21', 'name': 'abc', 'email': 'abc#network.com', 'type': 'Manager'},
{"id": "100", "name": "last", "email": "last#network.com", "type": "Manager"}
]
Code is below
for each1 in list_1:
for each2 in list_2:
if each1['email'] == each2['email']:
# Update the list_1 dictionary with respect to preference
Given the following inputs:
list_1 = [
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Owner" },
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "21", "name": "abc", "email": "abc#network.com", "type": "Employ" },
{ "id": "21", "name": "abc", "email": "abc#network.com", "type": "Manager" },
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "100", "name": "last", "email": "last#network.com", "type": "Manager" }
]
list_2 = [
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "21", "name": "abc", "email": "abc#network.com", "type": "Employ" },
{ "id": "52", "name": "abcded", "email": "abcded#network.com", "type": "Manager" }
]
preference = {'Owner': 1, 'Manager':2, 'Employ':3, 'HR': 4 }
I would start by reshaping list_2 into something more useable. This will get rid of the duplicates leaving us just the "best" type for each email:
list_2_lookup = {}
for item in list_2:
key, value = item["email"], item["type"]
list_2_lookup.setdefault(key, value)
if preference[value] < preference[list_2_lookup[key]]:
list_2_lookup[key] = value
Then we can iterate over the items in the first list and use the lookup we just created. Note, this is a little more convoluted than might be needed as it is not clear from your question and your expected result what items from list_1 should actually appear in the output. I have tried to match your stated output.
result = {}
for item in list_1:
key = item["email"]
result.setdefault(key, item)
if preference[result[key]["type"]] > preference[item["type"]]:
result[key]["type"] = item["type"]
if preference[result[key]["type"]] > preference.get(list_2_lookup.get(key), 99):
result[key]["type"] = list_2_lookup.get(key)
At this point we can:
print(list(result.values()))
Giving us:
[
{'id': '11', 'name': 'son', 'email': 'n#network.com', 'type': 'Owner'},
{'id': '21', 'name': 'abc', 'email': 'abc#network.com', 'type': 'Manager'},
{'id': '100', 'name': 'last', 'email': 'last#network.com', 'type': 'Manager'}
]
Here is code that will do the following:
If any emails match update list one at all places with that email to contain a new role under 'type'. for example if 123#gmail.com was in a dictionary within list2 then it will change change every item in list1 that contains 123#gmail.com as the email to have a role that is determined by a selection from the preference dictionary.
list_1 = [
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Owner" },
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "21", "name": "abc", "email": "abc#network.com", "type": "Employ" },
{ "id": "21", "name": "abc", "email": "abc#network.com", "type": "Manager" },
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "100", "name": "last", "email": "last#network.com", "type": "Manager" }
]
list_2 = [
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "11", "name": "son", "email": "n#network.com", "type": "Manager" },
{ "id": "21", "name": "abc", "email": "abc#network.com", "type": "Employ" },
{ "id": "52", "name": "abcded", "email": "abcded#network.com", "type": "Manager" }
]
pref = {1: 'owner', 2: 'Manager', 3: 'employ', 4: 'HR' }
choice = 1
idx1 = -1
for each1 in list_1:
idx1 += 1
for each2 in list_2:
if each1['email'] == each2['email']:
print(list_1[idx1]['type'])
print(pref[choice])
list_1[idx1]['type'] = pref[choice]
print(list_1)

get_companies limiting locations output to 5?

I'm trying to get all the locations that a company in LinkedIn has, but it seems that it's limiting to only 5. What are the params that I need to change to increase the number of locations returned?
For example, my company, Slalom Consulting, has about 15 locations, but it's only returning 5. I can't get that second '_total' to increase above 5.
app.get_companies(company_ids=[166000], selectors=['name', 'locations'], params={})
{
"_total": 1,
"values": [
{
"_key": "166000",
"locations": {
"_total": 5,
"values": [
{
"contactInfo": {
"fax": "",
"phone1": "206 483 5700"
},
"address": {
"postalCode": "98104",
"city": "Seattle",
"street1": "Suite 1900"
}
},
{
"contactInfo": {
"fax": "310-322-6888",
"phone1": "310-322-6800"
},
"address": {
"postalCode": "90245",
"city": "El Segundo",
"street1": "Suite 600"
}
},
{
"contactInfo": {
"fax": "415-593-3451",
"phone1": "415-593-3450"
},
"address": {
"postalCode": "94105",
"city": "San Francisco",
"street1": "Suite 1550"
}
},
{
"contactInfo": {
"fax": "972-294-7301",
"phone1": "972-294-7300"
},
"address": {
"postalCode": "75024",
"city": "Plano",
"street1": "Suite 480"
}
},
{
"contactInfo": {
"fax": "312.329.0461",
"phone1": "312.329.0401"
},
"address": {
"postalCode": "60601",
"city": "Chicago",
"street1": "Suite 5300"
}
}
]
},
"name": "Slalom Consulting"
}
]
}
I know that I need to add some params, but not sure what I need to do to get locations more than 5.
from linkedin import linkedin
import json
import csv
##start connection to LinkedIn
CONSUMER_KEY = 'xxx'
CONSUMER_SECRET = 'xxx'
USER_TOKEN = 'xxx'
USER_SECRET = 'xxx'
RETURN_URL = '' # Not required for developer authentication
# Instantiate the developer authentication class
auth = linkedin.LinkedInDeveloperAuthentication(CONSUMER_KEY, CONSUMER_SECRET, USER_TOKEN, USER_SECRET,RETURN_URL, permissions=linkedin.PERMISSIONS.enums.values())
app = linkedin.LinkedInApplication(auth)
co = app.get_companies(company_ids=[166000], selectors=['name', 'locations'], params={})
print co

Categories

Resources