Related
Here is part of the data file in json format, containing 1 entry. I need to find the 20 compounds (key:pretty_formula) with the highest eij_max values. I appreciate the help, I apologise if this is a basic question, I am new to all this.
{
"num_results": 3402,
"valid_response": true,
"criteria": "{\"piezo\": {\"$ne\": null}}",
"properties": "[\"pretty_formula\", \"piezo\", \"eij_max\", \"band_gap\", \"energy\"]",
"response": [
{
"pretty_formula": "KMg(PO3)3",
"piezo": {
"eij_max": 0.09337344787836212,
"piezoelectric_tensor": [
[
0.0,
0.0,
0.0,
0.0,
0.0,
0.0660250018229951
],
[
0.06602500182299509,
-0.06602499453211871,
0.0,
0.0,
0.0,
0.0
],
[
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
]
],
"v_max": [
-0.0,
1.0,
0.0
]
},
"eij_max": null,
"band_gap": 4.9474,
"energy": -193.18215352
},
{
"pretty_formula": "Sr2CuSi2O7",
You can easily do something like this to access the eij_max:
import json
# Opening JSON file
f = open('data.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data['response']['piezo']['eij_max']
print(i)
# Closing file
f.close()
So, I am working on a project in which the user gives inputs in the json file and the parser reads data from the json file and then creates a data structure to which gets updated to the inputs mentioned in the data file.
My json file(input_file.json5) looks like this:
{
"clock_frequency": 25000,
"Triggering_Mode": "positive_edge_triggered",
"Mode": "Offline",
"overshoot": 0.05,
"duty_cycle": 0.5,
"amplitude/high_level": 1,
"offset/low_level": 0
}
The data structure(data_struc.py) looks like this:
Parameters={
"Global_parameters": {
"frequency": 3000,
"Triggering_Mode": "positive_edge_triggered"
},
"Executor_param": {
"Mode": "Offline"
},
"Waveform_Settings": {
"overshoot": 0.05,
"duty_cycle": 0.5,
"amplitude/high_level": 1,
"offset/low_level": 0,
}
}
The code for the parser is:
import json5
from data_struc import Parameters
class Parser(object):
def read_input_file(self, path_name, file_name):
input_file = open(path_name + file_name + '.json5')
data = json5.load(input_file)
print(Parameters['Global_parameters'])
parameters = self.parse_parameters(data)
input_file.close()
return parameters
def parser_parameters(self, data):
parameter = {
"Global_parameters": {
"frequency": data[clock_frequency]
"Triggering_Mode": data[Triggering_Mode]
}
}
return parameter
I want to pass data as a parameter to the function and I want to update the contents of the data structure using the value of the data(passed as dictionary) to the function. How do I implement the function parser_parameters?
Here is a one-liner to map the data to a schema if you can change the schema, you could also just go and grab the keys instead of creating a list of items to match. This formats the data to the schema based on matching keys:
EDIT: added 'Data' tag to the schema and output for nested list data
schema = {
'Global_parameters': [
'clock_frequency', # I noticed you had this as just 'clock' in your desired outuput
'Triggering_Mode'
],
'Executor_param': [
'Mode'
],
'Waveform_Settings': [
'overshoot',
'duty_cycle',
'amplitude/high_level',
'offset/low_level'
],
'Data': {
'Packet'
}
}
data = {
"clock_frequency": 25000,
"Triggering_Mode": "positive_edge_triggered",
"Mode": "Offline",
"overshoot": 0.05,
"duty_cycle": 0.5,
"amplitude/high_level": 1,
"offset/low_level": 0,
"Packet": [
{"time_index":0.1, "data":0x110},
{"time_index":1.21, "data":123},
{"time_index":2.0, "data": 0x45}
]
}
# "one line" nested dict comprehension
data_structured = {k0: {k1: v1 for k1, v1 in data.items() if k1 in v0} # in v0.keys() if you are using the structure you have above
for k0, v0 in schema.items()}
import json
print(json.dumps(data_structured, indent=4)) # pretty print in json format
Output:
{
"Global_parameters": {
"clock_frequency": 25000,
"Triggering_Mode": "positive_edge_triggered"
},
"Executor_param": {
"Mode": "Offline"
},
"Waveform_Settings": {
"overshoot": 0.05,
"duty_cycle": 0.5,
"amplitude/high_level": 1,
"offset/low_level": 0
},
"Data": {
"Packet": [
{
"time_index": 0.1,
"data": 272
},
{
"time_index": 1.21,
"data": 123
},
{
"time_index": 2.0,
"data": 69
}
]
}
}
So for a school project I have to read in some data through an api. So far everything (kinda) works, but when I try to read in some data underneath a couple of links, I get keyerrors.
I would not mind posting the entire list, but it is mainly in dutch.
import json
from urllib.request import urlopen
with urlopen("http://api.buienradar.nl/data/public/2.0/jsonfeed ") as response:
source = response.read()
data = json.loads(source)
#pirnt(json.dumps(data, indent =2))
for item in data['actual']['stationmeasurements']:
del item['iconurl']
del item['graphUrl']
station = item['stationname']
#regio = item['regio]
stationid = item['winddirection']
print(station, stationid)
with open('uitlezen_buienradar.json', 'w') as f:
json.dump(data, f , indent=2)
This is a part of the list:
"$id": "1",
"buienradar": {
"$id": "2",
"copyright": "(C)opyright Buienradar / RTL. Alle rechten voorbehouden",
"terms": "Deze feed mag vrij worden gebruikt onder voorwaarde van bronvermelding buienradar.nl inclusief een hyperlink naar https://www.buienradar.nl. Aan de feed kunnen door gebruikers of andere personen geen rechten worden ontleend."
},
"actual": {
"$id": "3",
"actualradarurl": "https://api.buienradar.nl/image/1.0/RadarMapNL?w=500&h=512",
"sunrise": "2019-10-04T07:45:00",
"sunset": "2019-10-04T19:11:00",
"stationmeasurements": [
{
"$id": "4",
"stationid": 6391,
"stationname": "Meetstation Arcen",
"lat": 51.5,
"lon": 6.2,
"regio": "Venlo",
"timestamp": "2019-10-04T02:30:00",
"weatherdescription": "Zwaar bewolkt",
"iconurl": "https://www.buienradar.nl/resources/images/icons/weather/30x30/cc.png",
"graphUrl": "https://www.buienradar.nl/nederland/weerbericht/weergrafieken/cc",
"winddirection": "ZO",
"temperature": 10.0,
"groundtemperature": 9.9,
"feeltemperature": 9.6,
"windgusts": 2.4,
"windspeed": 1.5,
"windspeedBft": 1,
"humidity": 93.0,
"precipitation": 0.0,
"sunpower": 0.0,
"rainFallLast24Hour": 4.5,
"rainFallLastHour": 0.0,
"winddirectiondegrees": 143
After 'iconurl' and 'graphurl' it just spews out errors.
Use dict.get(key), then there will be no exception if the key does not exist. Incomplete data in your items ;)
import json
from urllib.request import urlopen
with urlopen("http://api.buienradar.nl/data/public/2.0/jsonfeed ") as response:
source = response.read()
data = json.loads(source)
#pirnt(json.dumps(data, indent =2))
for item in data['actual']['stationmeasurements']:
del item['iconurl']
del item['graphUrl']
station = item.get('stationname')
#regio = item['regio]
stationid = item.get('winddirection')
print(station, stationid)
with open('uitlezen_buienradar.json', 'w') as f:
json.dump(data, f , indent=2)
I have JSON file. I have parsed it and I have extracted some data which are classes and code smells. Now I should calculate the number of smells on each class. I tried this with an example of code smells and it return for me the number of this smell in all the json file.
this is a part of the Json file beacause it's too long
{
"methods": [
{
"parametersTypes": [
"Bundle"
],
"sourceFile": {
"file": {
"path": "/mnt/c/shortrain-master/app/src/main/java/com/nirhart/shortrain/MainActivity.java"
}
},
"metricsValues": {
"ParameterCount": 1.0,
"NumberOfAccessedVariables": 9.0,
"ChangingClasses": 0.0,
"CouplingDispersion": 0.5,
"MethodLinesOfCode": 21.0,
"MaxNesting": 0.0,
"CyclomaticComplexity": 1.0,
"MaxCallChain": 2.0,
"ChangingMethods": 0.0,
"CouplingIntensity": 4.0
},
"fullyQualifiedName": "com.nirhart.shortrain.MainActivity.onCreate",
"smells": []
},
{
"parametersTypes": [],
"sourceFile": {
"file": {
"path": "/mnt/c/shortrain-master/app/src/main/java/com/nirhart/shortrain/MainActivity.java"
}
},
"metricsValues": {
"ParameterCount": 0.0,
"NumberOfAccessedVariables": 2.0,
"ChangingClasses": 1.0,
"CouplingDispersion": 0.0,
"MethodLinesOfCode": 6.0,
"MaxNesting": 0.0,
"CyclomaticComplexity": 1.0,
"MaxCallChain": 6.0,
"ChangingMethods": 3.0,
"CouplingIntensity": 0.0
},
"fullyQualifiedName": "com.nirhart.shortrain.MainActivity.finishActivity",
"smells": [
{
"name": "MessageChain",
"reason": "MAX_CALL_CHAIN = 6.0",
"startingLine": 54,
"endingLine": 66
}
]
},
{
"parametersTypes": [
"View"
],
"sourceFile": {
"file": {
"path": "/mnt/c/shortrain-master/app/src/main/java/com/nirhart/shortrain/MainActivity.java"
}
},
"metricsValues": {
"ParameterCount": 1.0,
"NumberOfAccessedVariables": 4.0,
"ChangingClasses": 0.0,
"CouplingDispersion": 1.0,
"MethodLinesOfCode": 6.0,
"MaxNesting": 1.0,
"CyclomaticComplexity": 3.0,
"MaxCallChain": 1.0,
"ChangingMethods": 0.0,
"CouplingIntensity": 2.0
},
"fullyQualifiedName": "com.nirhart.shortrain.MainActivity.onClick",
"smells": []
},
{
"parametersTypes": [],
"sourceFile": {
"file": {
"path": "/mnt/c/shortrain-master/app/src/main/java/com/nirhart/shortrain/MainActivity.java"
}
},
"metricsValues": {
"ParameterCount": 0.0,
"NumberOfAccessedVariables": 0.0,
"ChangingClasses": 0.0,
"CouplingDispersion": 1.0,
"MethodLinesOfCode": 3.0,
"MaxNesting": 0.0,
"CyclomaticComplexity": 1.0,
"MaxCallChain": 1.0,
"ChangingMethods": 0.0,
"CouplingIntensity": 1.0
},
"fullyQualifiedName": "com.nirhart.shortrain.MainActivity.onBackPressed",
"smells": []
}
],
"sourceFile": {
"file": {
"path": "/mnt/c/shortrain-master/app/src/main/java/com/nirhart/shortrain/MainActivity.java"
}
},
"metricsValues": {
"ClassLinesOfCode": 40.0,
"OverrideRatio": null,
"WeighOfClass": 1.0,
"LCOM2": 0.5,
"TightClassCohesion": 0.0,
"LCOM3": 0.6666666666666666,
"NumberOfAccessorMethods": 0.0,
"WeightedMethodCount": 6.0,
"IsAbstract": 0.0,
"PublicFieldCount": 0.0
},
"fullyQualifiedName": "com.nirhart.shortrain.MainActivity",
"smells": []
},
]
This is my code:
import pandas as pd
import json
all_smells=['LazyClass','ComplexClass','LongParameterList','FeatureEnvy','LongMethod','GodClass','MessageChain']
with open('/content/result_smells.json') as handle:
dictdump = json.loads(handle.read())
my_map = {}
for elem in dictdump :
my_map[elem["fullyQualifiedName"]] = []
#adding all class smells
for class_smell in elem["smells"] :
my_map[elem["fullyQualifiedName"]].append(class_smell)
#adding all methods smells
for method in elem["methods"] :
for method_smell in method["smells"] :
my_map[elem["fullyQualifiedName"]].append(method_smell)
for elem in my_map :
print(elem)
for smell in my_map[elem] :
print(smell["name"])
This is the result : the name of class and the smells on it
com.nirhart.shortrain.MainActivity
MessageChain
com.nirhart.shortrain.path.PathParser
ComplexClass
FeatureEnvy
LongParameterList
LongParameterList
LongMethod
com.nirhart.shortrain.path.PathPoint
LazyClass
LongParameterList
com.nirhart.shortrain.path.TrainPath
FeatureEnvy
com.nirhart.shortrain.rail.RailActionActivity
FeatureEnvy
LongMethod
com.nirhart.shortrain.rail.RailInfo
com.nirhart.shortrain.train.TrainActionActivity
ComplexClass
SpaghettiCode
LongMethod
LongMethod
IntensiveCoupling
I try to calculate the number of the MessageChain smell in com.nirhart.shortrain.MainActivity class which is one it return for me 5 which is the MessageChain smell in all the json file
this is my code:
x=0
for elem in my_map :
print(elem)
for smell in my_map[elem] :
if smell["name"]=='MessageChain':
x+=1
Then I need to put all results on a CSV to analyse it.
this is an exemple of csv file with one smell
A Python Counter() can be used to simplyfy counting the smells, and a csv.DictWriter() can be used to then write the resulting dictionary holding all of the counts. For example:
from collections import Counter
import csv
import json
all_smells = ['LazyClass', 'ComplexClass', 'LongParameterList', 'FeatureEnvy', 'LongMethod', 'GodClass', 'MessageChain']
my_map = {}
with open('result_smells.json') as f_json:
json_data = json.load(f_json)
for entry in json_data:
my_map[entry["fullyQualifiedName"]] = []
#adding all class smells
for class_smell in entry["smells"] :
my_map[entry["fullyQualifiedName"]].append(class_smell)
#adding all methods smells
for method in entry["methods"] :
for method_smell in method["smells"] :
my_map[entry["fullyQualifiedName"]].append(method_smell)
with open('output.csv', 'w', newline='') as f_output:
csv_output = csv.DictWriter(f_output, fieldnames=["NameOfClass", *all_smells], extrasaction='ignore', restval='NaN')
csv_output.writeheader()
for elem in my_map :
smell_counts = Counter()
for smell in my_map[elem] :
smell_counts[smell["name"]] += 1
smell_counts['NameOfClass'] = elem
csv_output.writerow(smell_counts)
Giving you an output CSV file looking like:
NameOfClass,LazyClass,ComplexClass,LongParameterList,FeatureEnvy,LongMethod,GodClass,MessageChain
com.nirhart.shortrain.MainActivity,NaN,NaN,NaN,NaN,NaN,NaN,1
com.nirhart.shortrain.path.PathParser,NaN,1,2,1,1,NaN,NaN
com.nirhart.shortrain.path.PathPoint,1,NaN,1,NaN,NaN,NaN,NaN
com.nirhart.shortrain.path.TrainPath,NaN,NaN,NaN,1,NaN,NaN,NaN
com.nirhart.shortrain.rail.RailActionActivity,NaN,NaN,NaN,1,1,NaN,NaN
com.nirhart.shortrain.rail.RailInfo,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.train.TrainActionActivity,NaN,1,1,1,2,NaN,2
com.nirhart.shortrain.train.TrainDirection,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.train.TrainView,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.tutorial.TutorialFragment,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.tutorial.TutorialFragment.OnNextSlideClicked,1,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.tutorial.TutorialViewPagerAdapter,1,NaN,NaN,1,NaN,NaN,NaN
com.nirhart.shortrain.utils.ShortcutsUtils,NaN,NaN,1,NaN,NaN,NaN,2
I have this json object and i am trying to make it into a python list but i am getting some characters along that i don't need
import json
data = {
"products": [
{
"product_cp": 100.0,
"product_sp": 120.0,
"product_name": "coke",
},
{
"product_cp": 100.5,
"product_sp": 120.0,
"product_name": "fanta",
},
{
"product_cp": 70.5,
"product_sp": 100.5,
"product_name": "pepsi",
}
]
}
data = json.dumps(data)
print(data)
print('\v')
data = json.loads(data)
data_list = list(data['products'])
when i do:
print(data_list)
i get:
[{u'product_cp': 100.0, u'product_sp': 120.0, u'product_name': u'coke'},{u'product_cp': 100.5, u'product_sp': 120.0, u'product_name': u'fanta'}, {u'product_cp': 70.5, u'product_sp': 100.5, u'product_name': u'pepsi'}]
please how i do i make it so that {,[,},] an 'u characters doesn't show up?
To print individual product details without the syntactical overhead({,[,],}) you can use a nested loop:
for product in data["products"]:
for info in product:
print("%s: %s" % (info, product[info]))
print()
The output is:
product_cp: 100.0
product_sp: 120.0
product_name: coke
product_cp: 100.5
product_sp: 120.0
product_name: fanta
product_cp: 70.5
product_sp: 100.5
product_name: pepsi
try this code under python 3.6, i am a python rookie as well.
jsonobject = json.dumps(data)
jsonobjectToString = json.loads(jsonobject)
for level1 in jsonobjectToString["products"]:
str = level1["product_cp"],level1["product_sp"],level1["product_name"]
resultresp.append(str)
print(resultresp)
===================
[(100.0, 120.0, 'coke'), (100.5, 120.0, 'fanta'), (70.5, 100.5, 'pepsi')]