SP API get inventory report returning empty quantity - amazon

SP API reportType GET_FLAT_FILE_OPEN_LISTINGS_DATA is returning quantity value empty
let report_document = await sellingPartner.callAPI({
operation:'getReportDocument',
endpoint:'reports',
path:{
reportDocumentId: reportDocumentID
}
});
let report = await sellingPartner.download(report_document,{json:true});
The response has sku,ASIN, price values. But quantity is showing as empty for the available stock and showing 0 for out of stock items.
{
sku: '202050',
asin: 'B071HT7TZ9',
price: '12508.00',
quantity: '',
'Business Price': '',
'Quantity Price Type': '',
'Quantity Lower Bound 1': '',
'Quantity Price 1': '',
'Quantity Lower Bound 2': '',
'Quantity Price 2': '',
'Quantity Lower Bound 3': '',
'Quantity Price 3': '',
'Quantity Lower Bound 4': '',
'Quantity Price 4': '',
'Quantity Lower Bound 5': '',
'Quantity Price 5': '',
'Progressive Price Type': '',
'Progressive Lower Bound 1': '',
'Progressive Price 1': '',
'Progressive Lower Bound 2': '',
'Progressive Price 2': '',
'Progressive Lower Bound 3': '',
'Progressive Price 3': ''
}
I cross verified and the available quantity is 2 for the ASIN. Am I missing something to get the quantity number?

Related

Converting matrix of strings to PyTorch tensor

I wanted to convert the following matrix into a PyTorch tensor:
[['SELF', '', '', '', ''],
['nsubj', 'SELF', '', '', ''],
['', 'compound', 'SELF', '', ''],
['dobj', '', '', 'SELF', ''],
['pobj', '', '', '', 'SELF']]
I wanted to have a boolean matrix where any position with a string other than empty would have a 1, otherwise 0. This should be easy, but I do not seem to find an answer that does not require to iterate through the matrix and build the tensor a cell at a time.
The solution I have:
size = len(sample["edges"])
edge_mask = torch.zeros([size, size])
for i, row in enumerate(sample["edges"]):
for j, v in enumerate(row):
if v != "":
edge_mask[i, j] = 1
You can convert it to a boolean array, then use torch.from_numpy followed with a convert to int:
torch.from_numpy(np.array(sample["edges"], dtype=bool)).to(int)

How to extract the nth element of a nested list, where each inner list contains x elements and returned as a dictionary?

I have the following nested list:
orders = [['Large', 'Latte', 2.45],
['',
'Frappes - Coffee',
2.75,
'',
'Cortado',
2.05,
'',
'Glass of milk',
0.7,
'',
'Speciality Tea - Camomile',
1.3,
'',
'Speciality Tea - Camomile',
1.3]]
Each inner list is n elements long, but always divisible by 3.
My issue is that I am trying to return a list of dictionaries by iterating through orders with the following:
[dict(size=i[0],product=i[1],price=i[2]) for i in orders]
However, that only returns the first element inside products[1]
returns [{'size': 'Large', 'product': 'Latte', 'price': 2.45},
{'size': '', 'product': 'Frappes - Coffee', 'price': 2.75}]
I tried doing a second loop but that also doesn't work.
I want my code to output the following:
[
[{'size': 'Large', 'product': 'Latte', 'price': 2.45}],
[{'size': '', 'product': 'Frappes - Coffee', 'price': 2.75},
{'size': '', 'product': 'Cortado', 'price': 2.05},
{'size': '', 'product': 'Glass of Milk', 'price': 0.7},
{'size': '', 'product': 'Speciality Tea - Camomile', 'price': 1.3},
{'size': '', 'product': 'Speciality Tea - Camomile', 'price': 1.3}]
]
If anyone could point me in the right direction it would be much appreciated!
You can iterate the sublists as chunk of size 3 and then make dict:
def chunks(lst, n):
"""Yield successive n-sized chunks from lst.
https://stackoverflow.com/questions/312443/how-do-you-split-a-
list-into-evenly-sized-chunks
"""
for i in range(0, len(lst), n):
yield lst[i:i + n]
[[dict(size=i[0],product=i[1],price=i[2])
for i in chunks(order, 3)]
for order in orders]
Output:
[[{'size': 'Large', 'product': 'Latte', 'price': 2.45}],
[{'size': '', 'product': 'Frappes - Coffee', 'price': 2.75},
{'size': '', 'product': 'Cortado', 'price': 2.05},
{'size': '', 'product': 'Glass of milk', 'price': 0.7},
{'size': '', 'product': 'Speciality Tea - Camomile', 'price': 1.3},
{'size': '', 'product': 'Speciality Tea - Camomile', 'price': 1.3}]]
The problem is orders is inconsistent. Fix it by changing it to make each new order a list would be the best solution
orders = [
["Large", "Latte", 2.45],
["", "Frappes - Coffee", 2.75],
["", "Cortado", 2.05],
["", "Glass of milk", 0.7],
["","Speciality Tea - Camomile",1.3],
["","Speciality Tea - Camomile",1.3]
]

Creating a dictionary inside another dictionary

Given the following data how can I create a dictionary where the keys are the names of the students, and the values are dictionaries where the key is the test and it´s value is the grade they got in it.
grades = [
['Students', 'Test 1', 'Test 2', 'Test 3'],
['Tomas', '100', '90', '80'],
['Marcos', '88', '99', '111'],
['Flavia', '45', '56', '67'],
['Ramon', '59', '61', '67'],
['Ursula', '73', '79', '83'],
['Federico', '89', '97', '101']
]
I tried doing this, but I don´t know why it´s not showing the grades correctly.
notas_dict={}
def dic(etiquets, notas):
for i in range(len(etiquets)):
notas_dict[etiquets[i]]=int(notas[i])
return notas_dict
dic(['Test 1','Test 2', 'Test 3'], ['100','80','90'] )
dic_final={}
for line in grades[1:]:
line_grades=[int(element) for element in line[1:]]
dic_final[line[0]]=dic(['Test 1','Test 2', 'Test 3'], line_grades)
print(dic_final)
The output should be :
{'Tomas': {'Test 1': 100, 'Test 2': 90, 'Test 3': 80}, 'Marcos': {'Test 1': 88, 'Test 2': 99, 'Test 3': 111}, 'Flavia': {'Test 1': 45, 'Test 2': 56, 'Test 3': 67}, 'Ramon': {'Test 1': 59, 'Test 2': 61, 'Test 3': 67}, 'Ursula': {'Test 1': 73, 'Test 2': 79, 'Test 3': 83}, 'Federico': {'Test 1': 89, 'Test 2': 97, 'Test 3': 101}}
You can use:
{i[0]:dict(zip(grades[0][1:],i[1:])) for i in grades[1:]}
results in:
{'Tomas': {'Test 1': '100', 'Test 2': '90', 'Test 3': '80'},
'Marcos': {'Test 1': '88', 'Test 2': '99', 'Test 3': '111'},
'Flavia': {'Test 1': '45', 'Test 2': '56', 'Test 3': '67'},
'Ramon': {'Test 1': '59', 'Test 2': '61', 'Test 3': '67'},
'Ursula': {'Test 1': '73', 'Test 2': '79', 'Test 3': '83'},
'Federico': {'Test 1': '89', 'Test 2': '97', 'Test 3': '101'}}
If you want to get grades as int:
{i[0]:dict(zip(grades[0][1:],list(map(int,i[1:])))) for i in grades[1:]}
create a dataframe then use to_records to create a list of tuples where each tuple is a row. You can then slice the tuple by index.
grades = [
['Students', 'Test 1', 'Test 2', 'Test 3'],
['Tomas', '100', '90', '80'],
['Marcos', '88', '99', '111'],
['Flavia', '45', '56', '67'],
['Ramon', '59', '61', '67'],
['Ursula', '73', '79', '83'],
['Federico', '89', '97', '101']
]
Columns=grades[0]
df=pd.DataFrame(columns=Columns)
for i in range(1, len(grades)):
df_length = len(df)
df.loc[df_length] = grades[i]
print(df.to_records())
output:
[(0, 'Tomas', '100', '90', '80') (1, 'Marcos', '88', '99', '111')
(2, 'Flavia', '45', '56', '67') (3, 'Ramon', '59', '61', '67')
(4, 'Ursula', '73', '79', '83') (5, 'Federico', '89', '97', '101')]
or
dict=df.T.to_dict()
for k,v in dict.items():
print(k,v['Students'],v['Test1'],v['Test2'],v['Test3'])

looping diffulties with 2 csv files

Ok this is the last question about csv files and looping.
So I with my loops I want to do the following.
This is the csv file of students I have made into lists.
File 1
['Needie Seagoon', '57', '', '83', '55', '78', '', '91', '73', '65', '56', '', '', '']
['Eccles', '', '98', '91', '80', '', '66', '', '', '', '77', '78', '48', '77']
['Bluebottle', '61', '', '88', '80', '60', '', '45', '52', '91', '85', '', '', '']
['Henry Crun', '92', '', '58', '50', '57', '', '67', '45', '77', '72', '', '', '']
['Minnie Bannister', '51', '', '97', '52', '53', '', '68', '58', '70', '69', '', '', '']
['Hercules Grytpype-Thynne', '', '78', '62', '75', '', '67', '', '', '', '48', '56', '89', '67']
['Count Jim Moriarty', '51', '', '68', '51', '66', '', '55', '72', '50', '74', '', '', '']
['Major Dennis Bloodnok', '', '54', '47', '59', '', '48', '', '', '', '66', '58', '53', '83']
I then have another csv file with the max scores of each course:
File 2
CITS1001 95
CITS1401 100
CITS1402 97
CITS2002 99
CITS2211 94
CITS2401 95
CITS3001 93
CITS3002 93
CITS3003 91
CITS3200 87
CITS3401 98
CITS3402 93
CITS3403 88
So what I want to do and have been trying very hard to achieve is try and divide each student score by the max score of the course.
so for each student, the value going horizontal, I want it to divide by the values of the other value vertically.
For example:
['Needie Seagoon', '57', '', '83', '55', '78', '', '91', '73', '65', '56', '', '', '']
I want 57/95 , skip, 83/100, 55/97... you get where I'm going?
I want to do this for every name In the file. This code might be familiar to some of you but I know I'm doing something wrong.
def normalise(students_file, units_list):
file1 = open(students_file, 'r')
data1 = file1.read().splitlines()
file2 = open(units_list, 'r')
data2 = file2.read().splitlines()
for line in data1:
line = line.split(",")
for row in data2:
row = row.split(",")
for n in range(1, len(row), 2):
for i in range(1, len(line), 1):
if line[i] == '' :
pass
else:
answer = int(line[1]) / int(row[n])
file1.close()
file2.close()
I'll show you some of the output(goes on for a very long time).
output:
1st loop
0.6
none
0.8736842105263158
0.5789473684210527
0.8210526315789474
none
0.9578947368421052
0.7684210526315789
0.6842105263157895
0.5894736842105263
none
none
none
2nd loop
0.57
none
0.83
0.55
0.78
none
0.91
0.73
0.65
0.56
none
none
I understand that I have readline() but when I do readlines(), I cant strip the /n as it doesn't allow me to and the end='' makes the code messy. This output is saying that every value in the students row is getting divided by 95 then looping back to the start and looping every value by 100 and so on. How can I make the first value divide by 95, second by 100 and so on.
Sorry for the long explanation/question but I get told to explain myself more.
thanks.

How to add a character into a chararray that already have character in ipython 3

In python 2.7 I can do...
>>> import numpy
>>> flag=numpy.chararray(10) + ' '
>>> flag
chararray(['', '', '', '', '', '', '', '', '', ''],
dtype='|S6')
>>> flag[5] = 'a'
>>> flag
chararray(['', '', '', '', '', 'a', '', '', '', ''],
dtype='|S6')
>>> flag[5]=flag[5]+'b'
>>> flag
chararray(['', '', '', '', '', 'ab', '', '', '', ''],
dtype='|S6')
But this did not word in python 3.....
BTW. How can I save the "flag" array with some number array in to a text file. Like
1 1
1 1
1 1
1 1
1 1
1 1 ab
1 1
1 1
1 1
1 1
I had used
np.savetxt but.... won't work....
many thx.....

Resources