To get a dictionary as a table in markdown - python

So I have written the below code
paa_required_data = {
"Case age": paa_pending_since,
"PAA Case ID": all_paa_cases,
"PAA Owner": data[data["Status"] == "Pending Amazon Action"].Owner.to_list(),
}
mac_required_data = {
"Case age": mac_pending_since,
"Mac Case ID": all_mac_cases,
"MAC Owner": data[data["Status"] == "Merchant Action Completed"].Owner.to_list(),
}
paa_new_data = pandas.DataFrame(paa_required_data).sort_values(by=['Case age'], ascending=False).to_string(index=False)
mac_new_data = pandas.DataFrame(mac_required_data).sort_values(by=['Case age'], ascending=False).to_string(index=False)
chime_msg = f"/md Hello #Present \n" \
f"Good morning. I request you to kindly work on these PAA and MAC cases on priority and get them " \
f"resolved at the earliest.\n" \
f"### PAA Cases as of {datetime.date.today()}\n" \
f"{paa_new_data}\n" \
f"### MAC Cases as of {datetime.date.today()}\n" \
f"{mac_new_data}\n" \
All the values in the dictionary are lists. The chime_msg is the message sent to chime through webhooks. I want to use tables in markdown to get something like this
Case age
PAA Case ID
PAA Owner
value 1
value 2
Value 3
But when I try to use the .to_markdown() after sorting the dataframe, I get an error saying that 'str' object has no attribute 'to_markdown' . Also when I use .to_markdown without sorting, the output in chime is |---😐:--- (gets converted to an emoji)
Can anyone help me getting the table output shown above?

Related

Python JSON assign data from API

I have a py file to read data from Wordpress API and pass values to another fields of other API. When values are singles, i have no problem, but i don't know how make that:
When i read one field from the API, the states values, comes with code instead the text value. For example, when the text value in Wordpress is Barcelona, returns B, and i'll need that the value returned will be Barcelona.
One example of code with simple fields values:
oClienteT["Direcciones"] = []
oClienteT["Telefono"] = oClienteW["billing"]["phone"]
oClienteT["NombreFiscal"] = oClienteW["first_name"] " " oClienteW["last_name"]
oClienteT["Direcciones"].append( {
"Codigo" : oClienteW["id"],
"Nombre" : oClienteW["billing"]["first_name"],
"Apellidos" : oClienteW["billing"]["last_name"],
"Direccion" : oClienteW["billing"]["address_1"],
"Direccion2" : oClienteW["billing"]["address_2"],
"Poblacion" : oClienteW["billing"]["state"],
"Provincia" : oClienteW["billing"]["city"]
})
When billing city is Madrid and billing state is madrid, Wordpress returns Madrid and M
I need tell thst when Madrid, returns Madrid, and so on.
Make sure to convert to a JSON object before accessing fields (data = json.loads(json_str))
response = { "billing": { "address_1": "C/GUSTAVO ADOLFO BECQUER, 4", "city": "SEVILLA", "state": "SE"}}
print(response["billing"].get("address_1", None))
I've just got solved it:
def initProvincias(self):
self.aProvincias['C'] = 'A Coruña'
self.aProvincias['VI'] = 'Álava'
def getProvincia(self , sCod ):
if not sCod in self.aProvincias:
_logger.info("PROVINCIA NO ENCONTRADA "+str(sCod))
return ""
return self.aProvincias[sCod]
"Provincia" : self.getProvincia( oClienteW["shipping"]["state"] ),

How to update a document with new attribute

I'm using ArangoDB in order to update a document with a new attribute.
My steps are as follows:
Get the document and store it in a variable
doc = self.db.get_document(document_name="document_name")
Iterate over the doc, and add to it the attribute, for e.g.:
for idx, movie in enumerate(doc):
print(movie)
query = 'UPSERT { movie_id: #movie_id, scene_element: #scene_element} INSERT\
{ movie_id: #movie_id, scene_element: #scene_element, index: #index, \
_key: #_key, _id: #_id, _rev: #_rev, url_path: #url_path, captions: #captions, ref: #ref, \
experts: #experts, groundtruth: #groundtruth, base: #base, source: #source, File: #File } UPDATE \
{ index: #index \
} IN document_name'
movie["index"] = idx
self.pdb.aql.execute(query, bind_vars=movie)
And this works, I'm trying to use this query differently without the INSERT command. Why something like this doesn't work:
query = 'UPSERT { movie_id: #movie_id, scene_element: #scene_element} UPDATE \
{ index: #index \
} IN document_name'
Finding the correct movie_id and scene_element, and just updating it with new index.
The error I'm getting is invalid syntax, and it asks for me to put the INSERT command.

Add Values into Nested Objects in PowerShell like you can in Python

I am looking use PowerShell to output some JSON that looks like this for use with a Python script:
{
"run_date": "2020-08-27",
"total_queries": 4,
"number_results": 3,
"number_warnings": 1,
"number_errors": 5,
"build_url": "https://some-url.com",
"queries":{
"query_a":{
"database_a": "102 rows",
"database_b": "Error: See pipeline logs for details"
},
"query_b": "No results",
"query_c": {
"database_a": "Warning: Number of results exceeded maximum threshold - 6509 rows",
"database_c": "Error: See pipeline logs for details",
"database_d": "Error: See pipeline logs for details"
}
} }
(Ignore the above closing bracket, it won't format properly on here for some reason).
I am using a foreach loop within PowerShell to run each of these queries sequentially depending on which databases they need to be ran on.
I know in Python I can create a template of the JSON like so:
options = {
'run_date': os.environ['SYSTEM_PIPELINESTARTTIME'].split()[0],
'total_queries': 0,
'number_results': 0,
'number_warnings': 0,
'number_errors': 0,
'build_url': 'options = {
'run_date': os.environ['SYSTEM_PIPELINESTARTTIME'].split()[0],
'total_hunts': 0,
'number_results': 0,
'number_warnings': 0,
'number_errors': 0,
'build_url': 'https://some-url.com',
'queries': {} }
and then use something like:
options['queries'][filename][database] = '{} rows'.format(len(data))
To add data into the Python dictionaries.
I've tried using nested PSCustomObjects but I end up with a conflict when different queries are being ran on the same database, so its trying to add a value to the PSCustomObject with the same Key. I would like to know if there is a nice 'native' way to do this in PowerShell like there is in Python.
Turns out I was just being a bit of an idiot and not remembering how to work with PowerShell objects.
Ended up first adding all the query names into the parent object like so:
foreach($name in $getqueries){
$notiObj.queries | Add-Member -NotePropertyName $name.BaseName -NotePropertyValue ([PSCustomObject]#{})}
Then adding in info about the queries themselves within the loop:
$notificationObj.queries.$queryName | Add-Member -NotePropertyName $database -NotePropertyValue "$($dataTable.Rows.Count) Rows"
If the required end-result is a Json file, there is actually no need to work with complex (and rather fat) [PSCustomObject] types. Instead you might just use a [HashTable] (or an ordered dictionary by just prefixing the hash table, like: [Ordered]#{...})
To convert hash tables from your Json file, use the ConvertFrom-Json -AsHashTable parameter (introduced in PowerShell 6.0).
To build a template (or just understand the PowerShell format), you might want to use this ConvertTo-Expression cmdlet:
$Json | ConvertFrom-Json -AsHashTable | ConvertTo-Expression
#{
'number_errors' = 5
'number_warnings' = 1
'queries' = #{
'query_b' = 'No results'
'query_a' = #{
'database_a' = '102 rows'
'database_b' = 'Error: See pipeline logs for details'
}
'query_c' = #{
'database_a' = 'Warning: Number of results exceeded maximum threshold - 6509 rows'
'database_d' = 'Error: See pipeline logs for details'
'database_c' = 'Error: See pipeline logs for details'
}
}
'build_url' = 'https://some-url.com'
'run_date' = '2020-08-27'
'number_results' = 3
'total_queries' = 4
}
Meaning you can assign this template to $Options as follows:
$Options = #{
'number_errors' = 5
'number_warnings' = 1
'queries' = #{ ...
And easily change your properties in your nested objects, like:
$Options.Queries.query_c.database_d = 'Changed'
Or add a new property to a nested object:
$Options.Queries.query_a.database_c = 'Added'
Which result in:
$Options | ConvertTo-Json
{
"run_date": "2020-08-27",
"queries": {
"query_a": {
"database_c": "Added",
"database_b": "Error: See pipeline logs for details",
"database_a": "102 rows"
},
"query_b": "No results",
"query_c": {
"database_c": "Error: See pipeline logs for details",
"database_d": "Changed",
"database_a": "Warning: Number of results exceeded maximum threshold - 6509 rows"
}
},
"number_results": 3,
"build_url": "https://some-url.com",
"total_queries": 4,
"number_errors": 5,
"number_warnings": 1
}

Grabbing Specific Value from JSON Data API

I am fully aware there are other topics on this on this website, but the solutions do not work for me.
I am trying to grab the 'last' value from this API address:
https://cryptohub.online/api/market/ticker/PLSR/
In Python, I have tried many different scripts, and tried hard myself but I always end up getting a "KeyError" although "last" is in the API. Can anyone help me?
Your data in response has structure:
$ curl https://cryptohub.online/api/market/ticker/PLSR/ | json_pp
{
"BTC_PLSR" : {
"baseVolume" : 0.00772783,
"lowestAsk" : 0.00019999,
"percentChange" : -0.0703703704,
"quoteVolume" : 83.77319071,
"last" : 0.000251,
"id" : 78,
"highestBid" : 5e-05,
"high24hr" : 0.000251,
"low24hr" : 1.353e-05,
"isFrozen" : "0"
}
}
i.e. dictionary inside dictionary, so to extract values you need:
data = response.json()["BTC_PLSR"]
visitors = data["id"]
UPDATE on comment:
Im not sure I understand you, I did a simple test and it works fine:
$ python3 << EOF
> import requests
> url = 'https://cryptohub.online/api/market/ticker/PLSR/'
> response = requests.get(url)
> data = response.json()['BTC_PLSR']
> print('ID ==> ', data['id'])
> EOF
ID ==> 78
As you can see line print('ID ==> ', data['id']) returns output ID ==> 78
Test source code:
import requests
url = 'https://cryptohub.online/api/market/ticker/PLSR/'
response = requests.get(url)
data = response.json()['BTC_PLSR']
print('ID ==> ', data['id'])

Parsing through JSON file with python and selecting multiple values on certain conditions

I have this JSON file.
{
"reviewers":[
{
"user":{
"name":"keyname",
"emailAddress":"John#email",
"id":3821,
"displayName":"John Doe",
"active":true,
"slug":"jslug",
"type":"NORMAL",
"link":{
"url":"/users/John",
"rel":"self"
},
},
"role":"REVIEWER",
"approved":true
},
{
"user":{
"name":"keyname2",
"emailAddress":"Harry#email",
"id":6306,
"displayName":"Harry Smith",
"active":true,
"slug":"slug2",
"link":{
"type":"NORMAL",
"url":"/users/Harry",
"rel":"self"
},
},
"role":"REVIEWER",
"approved":false
}
],
}
Initially, I was using a snippet of code that would go through and grab the full names of the reviewers.
def get_reviewers(json):
reviewers = ""
for key in json["reviewers"]:
reviewers += key["user"]["displayName"] + ", "
reviewers = reviewers[:-2]
return reviewers
which would return "John Doe, Harry Smith". However, now I'm trying to get it so that the script will return a (A) next to the name of the user if their tag equals true "approved"=true.
So for example the code above would get the names, then see that John's approved tag is true and Harry's is false, then return "John Doe(A), Harry Smith". I'm just not sure where to even begin to do this. Can anyone point me in the right direction?
This is what I've been trying so far but obviously it isn't working as I'd like it to.
def get_reviewers(stash_json):
reviewers = ""
for key in stash_json["reviewers"]:
if stash_json["reviewers"][0]["approved"] == true:
reviewers += key["user"]["displayName"] + "(A)" + ", "
else:
reviewers += key["user"]["displayName"] + ", "
reviewers = reviewers[:-2]
return reviewers
which outputs Jason Healy(A), Joan Reyes(A)
This is what my stash_json outputs when put through pprint.
You probably want something along the lines of this:
def get_reviewers(stash_json):
reviewers = ""
for item in stash_json["reviewers"]:
if item["approved"]:
reviewers += item["user"]["displayName"] + "(A)" + ", "
else:
reviewers += item["user"]["displayName"] + ", "
reviewers = reviewers[:-2]
return reviewers
I think part of your confusion comes from the fact that "reviewers" is a list of dict elements, and each dict element has a key-value approved, but also a key "user" which value itself is another dict.
Read the JSON file carefully, and for debugging purposes, use plenty of
print(...)
print(type(...)) # whether something is a dict, list, str, bool etc
or
from pprint import pprint # pretty printing
pprint(...)
This looks like a good place to use join and list comprehension:
def get_reviewers(stash_json):
return ", ".join([item['user']['displayName'] + ('(A)' if item['approved'] else '') for item in stash_json['reviewers']])

Categories

Resources