Why doesn't the values stored in my json file change? - json

I have buttons that are connected to keys in my json file. Using kivy's Clock module I check the value connected to the button and changes its colors. When the button turns red I want it's score value to turn 0 and be stored in the json file. When I have multiple buttons saved in the json file the score doesn't turn zero until I press one of the buttons and when I do press the button all the values except the first button turn 0, this is not what I want.
class MainApp(App):
def build(self): # build() returns an instance
self.store = JsonStore("streak.json") # file that stores the streaks:
Clock.schedule_interval(self.check_streak, 1 / 30.)
return presentation
def check_streak(self, dt):
for child in reversed(self.root.screen_two.ids.streak_zone.children):
name = child.text
with open("streak.json", "r") as read_file:
data = json.load(read_file)
for key in data.keys():
if key == name:
delay = data.get(key, {}).get('delay') # get value of nested key 'delay'
self.honey = data[key]['delta']
float(self.honey)
if delay > time.time() < self.honey: # early (yellow)
child.background_normal = ''
child.background_color = [1, 1, 0, 1]
child.unbind(on_press=self.add_score)
child.bind(on_press=self.early_click)
elif delay > time.time() > self.honey: # on time (green)
child.background_normal = ''
child.background_color = [0, 1, 0, 1]
child.unbind(on_press=self.early_click)
child.bind(on_press=self.add_score)
elif delay < time.time() > self.honey: # late (red)
child.background_normal = ''
child.background_color = [1, 0, 0, 1]
child.unbind(on_press=self.add_score)
child.unbind(on_press=self.early_click)
with open("streak.json", "r+") as f:
files = json.load(f)
files[child.text]['score'] = 0
f.seek(0)
json.dump(files, f, indent=4)
f.truncate()
json file:
{
"One": {
"action": "One",
"delay": 1558740875.58999,
"seconds": 60,
"score": 3,
"delta": 1558740815.58999,
"grace_sec": 120
},
"Two": {
"action": "Two",
"delay": 1558740752.0085213,
"seconds": 60,
"score": 0,
"delta": 1558740692.0085213,
"grace_sec": 120
},
"Three": {
"action": "Three",
"delay": 1558746820.4364505,
"seconds": 60,
"score": 0,
"delta": 1558740820.4364505,
"grace_sec": 6060
}
}
I want only the button that is red to change its score to 0 but both Two and Three change, even though only Two was red. Also, the score only changes when I press a green button, in this case, it was One. This is not what I want. I want the score to update its self using the Clock module.

case 1: I think time.time() is always greater both delay and honey and it enters third condition every time and sets score to zero.
Another option: Just keep the track of all the buttons that are red in a list and iterate them and update the json file at once.

created a separate function that uses clock to update the json file.
# change score to 0 and stores in json file
def score_gone(self, dt):
for child in self.root.screen_two.ids.streak_zone.children:
name = child.text
color = child.background_color
with open("streak.json", "r") as file:
read = json.load(file)
if color == [1, 0, 0, .95]: # red
if read[name]['score'] != 0: #stops slow down from Clock
with open("streak.json", "r+") as f: # fix score not reseting to 0
data = json.load(f)
data[name]['score'] = 0
f.seek(0)
json.dump(data, f, indent=4)
f.truncate()
elif read[name]['score'] == 0: #stops slow down from Clock
pass

Related

Trying to parse access.log

Good afternoon, I'm trying to find the top 10 ip in access.log (standard log of the Apache server).
There is a code like this:
import argparse
import json
import re
from collections import defaultdict, Counter
parser = argparse.ArgumentParser(description='parser script')
parser.add_argument('-f', dest='logfile', action='store', default='access.log')
args = parser.parse_args()
regul_ip = (r"^(?P<ips>.*?)")
regul_method = (r"\"(?P<request_method>GET|POST|PUT|DELETE|HEAD)")
def req_by_method():
dict_ip = defaultdict(lambda: {"GET": 0, "POST": 0, "PUT": 0, "DELETE": 0, "HEAD": 0})
with open(args.logfile) as file:
for index, line in enumerate(file.readlines()):
try:
ip = re.search(regul_ip, line).group()
method = re.search(regul_method, line).groups()[0]
return Counter(dict_ip).most_common(10)
except AttributeError:
pass
dict_ip[ip][method] += 1
print(json.dumps(dict_ip, indent=4))
with open("final_log.json", "w") as jsonfile:
json.dump(dict_ip, jsonfile, indent=5)
When the code is executed, I only get: []
How can I fix this code to make it work?
I also need to output to the final json file a set of such lines: "ip", "method", "status code", "url" and the duration of the request

Receiving TypeError: 'NoneType' object is not subscriptable at random in Python3

I'm writing a simple program in Python that iterates through various API JSON links, and compares values from each of the data sources. As I cycle through one of the JSON files, I call a function that calls a different JSON file based off of one of the values of the initial JSON file.
Here is my Python code:
import requests
import json
import urllib.request as request
from urllib.request import Request, urlopen
import time
# THINGS
'''
international API: https://arsonwarehouse.com/api/v1/foreign-stock
domestic API: https://api.torn.com/market/ ?selections=itemmarket&key=KEY
personal API: https://api.torn.com/user/2519609?selections=money&key=KEY
'''
capacity = 21
moneyOverride = False
totalCost: int
totalIncome: int
#Takes in the cost per item, multiplies by capacity, returns true if it's affordable
def checkAfford(costPer):
global totalCost
totalCost = capacity * costPer
if totalCost < money:
return True
else:
return False
#Puts together the API link with the ID number and calls pullJSON()
def checkDomestic(id):
jsonPULL = pullJSON(f"https://api.torn.com/market/{id}?selections=itemmarket&key=YET")
if jsonPULL == None:
print("error")
else:
return jsonPULL
#Requests the JSON from the server
def pullJSON(url):
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
with urlopen(req) as response:
if response.getcode() == 200:
source = response.read()
data = json.loads(source)
return data
else:
print('An error occurred while attempting to retrieve data from the API.')
internationalData = pullJSON("https://arsonwarehouse.com/api/v1/foreign-stock")
personalData = pullJSON("https://api.torn.com/user/2519609?selections=money&key=YET")
domesticData = checkDomestic(10)
#Money override
if moneyOverride == False:
money = personalData.get("money_onhand")
else:
money = 1000000000000000
print("onhand:", money)
i = 0
length = len(internationalData["items"])
print(length)
for x in internationalData["items"]:
time.sleep(0.5)
ident = x["item_id"]
domPrice = x["price"]
print("DOMESTIC: ID:", ident, " at price:", domPrice)
domestic = checkDomestic(ident).get("itemmarket")[1]
inPrice = domestic["cost"]
if inPrice == None:
print("error", domestic["ID"])
location = x["country"]
print("INTERNATIONAL: ID:", ident, " at price:", inPrice, "location: ", location)
print("end")
This code won't inherently work — I removed my API key from the links. However, I can show what the example JSON looks like.
This is what checkDomestic() pulls from:
{
"itemmarket": [
{
"ID": 101242786,
"cost": 17898,
"quantity": 1
},
{
"ID": 101242784,
"cost": 17898,
"quantity": 1
}
]
}
And this is what internationalData looks like:
{
"items": [
{
"item_id": 4,
"country": "South Africa",
"price": 750,
"stock": 247,
"reported_at": "2020-06-03T11:47:56Z"
},
{
"item_id": 8,
"country": "Mexico",
"price": 4200,
"stock": 59,
"reported_at": "2020-06-03T11:45:21Z"
}
]
}
I've searched around for an answer, but most questions have to do with the .sort() method. I should also mention that this happens at random—sometimes in the first 10 iterations, but never at the end, so it's not an out of range error. The JSON structures stay the same across.
A somewhat related question: I've completed this project before in Golang, and I noticed that in Go it finishes much, much, much faster than in Python. Is there some kind of redundancy that's making it take so long? I'm aware of the time.sleep() function I have in there-it was to make sure that it's not something with overcalling the API.
Any advice would be massively helpful!

is there a way to store list values inside a loop in python

I am trying to detect object using yolov3 and OpenCV with python
I want to store the coordinate of the detected object in JSON format but I am just getting the last coordinate how can I store all of my value in my list
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
y = max(y, labelSize[1])
cv2.rectangle(img, (x, y - round(1.5 * labelSize[1])), (x + round(1.5 * labelSize[0]), y + baseLine),
(255, 255, 255), cv2.FILLED)
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 255, 255), 1)
# print ("x1=",(left,right),"y:",top,bottom,label)
cv2.putText(img, label, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)
book = {
"frame_url": "frame",
"frame_width": width,
"frame_height": height,
"objeler": [
{
"tur": label,
"x0": x,
"y0": y,
"x1": x+w,
"y1": y+h
}]
}
s = json.dumps(book, indent=2)
with open("f.json", "w")as f:
f.write(s)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
And this is the output I am getting
{
"frame_url": "frame",
"frame_width": 768,
"frame_height": 360,
"objeler": [
{
"tur": "arac",
"x0": 273,
"y0": 256,
"x1": 299,
"y1": 269
}
]
}
You should create a list outside of the for loop. Then, in the loop, you append to that list. When the loop ends you convert the list to json.
A general example:
import json
# create an empty list
books = []
for x in range(10):
# add to the list
books.append({"key":x})
print(json.dumps(books))
Edit: question in comments.
You can use glob to get all filenames like this:
import glob
# location of images
folder = ""
# print all filenames of .jpg in folder
for filename in glob.glob(folder + "*.jpg"):
image = cv.imread(folder+filename)
# Process image...

How to read the text in excel cell and replace with someother value in json output using python?

My python code reads the excel sheet and converts it into a json file output. I have a column in the excel sheet, where the values are either "Planned" or "Unplanned".
1)In the json output, I want the Planned to be replaced with "1" and Unplanned to be replaced with "2" without changing anything in the excel file.
2)In the output I dont want "data" to appear.
3)In the excel, my Start time column value is like this "2018-11-16 08:00:00". I want the output to be "2018-11-16T08:00:00Z". Currently i am getting some garbage value.
Below is my code.
import xlrd, json, time, pytz, requests
from os import sys
from datetime import datetime, timedelta
from collections import OrderedDict
def json_from_excel():
excel_file = 'test.xlsx'
jsonfile = open('ExceltoJSON.json', 'w')
data = []
datestr = str(datetime.now().date())
loaddata = OrderedDict()
workbook = xlrd.open_workbook(excel_file)
worksheet = workbook.sheet_by_name('OMS-GX Data Extraction')
sheet = workbook.sheet_by_index(0)
for j in range(0, 6):
for i in range(1, 40):
temp = {}
temp["requestedStart"] = (sheet.cell_value(i,0)) #Start Time
temp["requestedComplete"] = (sheet.cell_value(i, 1)) #End Time
temp["location"] = (sheet.cell_value(i, 3)) #Station
temp["equipment"] = (sheet.cell_value(i, 4)) #Device Name
temp["switchOrderTypeID"] = (sheet.cell_value(i, 5)) #Outage Type
data.append(temp)
loaddata['data'] = data
json.dump(loaddata, jsonfile, indent=3, sort_keys=False)
jsonfile.write('\n')
return loaddata
if __name__ == '__main__':
data = json_from_excel()
Below is my sample output:
{
"data": [
{
"requestedStart": testtime,
"requestedComplete": testtime,
"location": "testlocation",
"equipment": "testequipment",
"switchOrderTypeID": "Planned"
},
{
"requestedStart": testtime,
"requestedComplete": testtime,
"location": "testlocation",
"equipment": "testequipment",
"switchOrderTypeID": "Unplanned"
}
]
}
Answer to the 1st question:
You may use conditional assignment.
temp["switchOrderTypeID"] = (1 if sheet.cell_value(i, 5) == "Planned" else 0)
Answer to the 2nd question:
Use loaddata = data which will be an array of the jsons without data as json key.
Answer to 3rd question:
from dateutil.parser import parse
t = "2018-11-16 08:00:00"
parse(t).strftime("%Y-%m-%dT%H:%M:%SZ")

Pygame Executable Crashing

I have a small animation code that is built using PyGame (and Python 2.7). I am able to build the executable folder with Pyinstaller, however when I try to run the executable it is crashing, although I am able to run the same files (i.e. source code) through my terminal without any problem.
import pygame
import exercise
import random as rd
import copy
import pandas as pd
import numpy as np
def animate():
debug_mode=False
done1 = False
dc_x = 30
dc_y = 30
plant_x = 110
plant_y = 30
supp_x = 190
supp_y = 30
WHITE = (255, 255, 255)
BLACK = (0, 0, 0) #arrows
RED = (255, 0, 0) # Box frames for summary of t, cost and lost sales
GREEN = (0, 255, 0) # Box frames for shipment amounts
DEEPSKYBLUE = (0,191,255) # arc to represent backup WIP inv
DOGERBLUE = (0,191,255) # arc to represent backup FG inv
INDIGO = (75,0,130) # DC/Supp up state
LAVENDER=(238, 130, 238) #DC/Supp down state
OLIVE=(128,128,0) #plant down state
YELLOWGREEN=(173,255,47) #plant up state
DARKYELLOW=(204,204,0)
pygame.init()
screen = pygame.display.set_mode((1000, 600))
clock = pygame.time.Clock()
grand_record={}
############ READ AND PREPARE INITIAL SCREEN for t==0 ################
PLANT_status, SUPP_status, DC_status, disruption, DC_BUp_Plan, Plant_BUp_Plan, Supp_BUp_Plan, Demand, P=exercise.init(debug_mode,False)
grand_record[0]={"DC":DC_status, "Plant":PLANT_status, "Supplier":SUPP_status}
basicFont = pygame.font.SysFont(None, 24)
largeFont = pygame.font.SysFont(None, 35)
pygame.display.set_caption('SCREAM!')
if debug_mode: R=1
else: R= rd.randint(0,25)
scenario_score={"cost":0, "service":0}
while not done1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done1 = True
#if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_DOWN:
# paused()
end_of_one_run=1
total_lost_customer=0
cumulative_inv_holding_cost=0
total_fixed_cost_paid=0
table_output=[]
chart_output=[]
header=["Week","Demand","Upstream shipment", "Shipment from regular DC", "Shipment from backup DC",
"FG Inv at Reg. DC", "FG Inv at B/up. DC", "FG Inv as B/up","LostSales","Start Order for b/up DC", "B/up DC starts","DC Disruption at","Disruption duration", "TotalAvailableWIPSupplyCap",
"Start Order for b/up Supplier", "B/up Supplier starts","Supplier Disruption at","Disruption duration", "Storage Cap Available", "Plant production",
"WIP Inv as B/up","Start Order for b/up Plant", "B/up Plant starts","Plant Disruption at","Disruption duration"]
table_output.append(header)
for t in range(1,P+1): # for the specified number of periods
screen.fill((0, 0, 0))
picture = pygame.image.load('Background.png').convert() #MITwallpaper
screen.blit(picture, [0, 0])
#import pdb; pdb.set_trace()
################################## MAKE CALCULATIONS FOR t>0 ################
grand_record,output=exercise.calculate(t, grand_record[t-1]["Plant"], grand_record[t-1]["Supplier"], grand_record[t-1]["DC"], disruption, DC_BUp_Plan, Plant_BUp_Plan, Supp_BUp_Plan,Demand[R*P+t],grand_record,debug_mode)
chart_output=exercise.prepare_chart_output(chart_output, grand_record)
output_loc=copy.deepcopy(output)
table_output.append(output_loc)
end_of_one_run=end_of_one_run+1
objectives=exercise.calculate_objectives(grand_record, DC_BUp_Plan, Plant_BUp_Plan, Supp_BUp_Plan, t)
lost_cust_text = largeFont.render("Service: "+str(objectives["Service"]), True, WHITE, BLACK)
lost_cust_textRect = lost_cust_text.get_rect()
lost_cust_textRect.centerx = 380
lost_cust_textRect.centery = 420
pygame.draw.rect(screen, RED, (lost_cust_textRect.left - 3, lost_cust_textRect.top - 3, lost_cust_textRect.width + 3, lost_cust_textRect.height + 3))
screen.blit(lost_cust_text, lost_cust_textRect)
inv_hold_text = largeFont.render("Inv.Holding Cost(k$): "+str(round(objectives["InvHoldingCost"]*0.001,1)), True, WHITE, BLACK)
inv_hold_textRect = inv_hold_text.get_rect()
inv_hold_textRect.centerx = 600
inv_hold_textRect.centery = 445
pygame.draw.rect(screen, RED, (inv_hold_textRect.left - 3, inv_hold_textRect.top - 3, inv_hold_textRect.width + 3, inv_hold_textRect.height + 3))
screen.blit(inv_hold_text, inv_hold_textRect)
startup_text = largeFont.render("Startup Cost(k$): "+str(round(objectives["Startup_cost"]*0.001,1)), True, WHITE, BLACK)
startup_textRect = startup_text.get_rect()
startup_textRect.centerx = 840
startup_textRect.centery = 470
pygame.draw.rect(screen, RED, (startup_textRect.left - 3, startup_textRect.top - 3, startup_textRect.width + 3, startup_textRect.height + 3))
screen.blit(startup_text, startup_textRect)
###################################################################################
if t<P:
pygame.display.flip()
clock.tick(8)
else:
#import pdb; pdb.set_trace()
table = pd.DataFrame(table_output)
table.to_excel("Details.xlsx",index=False)
#import pdb; pdb.set_trace()
done1 = True
################################ Want A PARETO Button ###################################
x=500
y=500
w=200
h=50
pygame.draw.rect(screen, DEEPSKYBLUE,(x,y,w,h))
buttontext = basicFont.render("Want A Pareto Solution?", True, BLACK , WHITE )
buttontext_rect=buttontext.get_rect()
buttontext_rect.centerx = (x+(w/2))
buttontext_rect.centery = (y+(h/2))
screen.blit(buttontext, buttontext_rect)
pygame.display.flip()
pygame.time.delay(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exercise.plotcharts(chart_output)
################################################################################
animate()
I had the same problem (also Python 2.7), and this did it for me:
pyinstaller usually has trouble with pygame.font.SysFont("some_font", 24)
Instead use pygame.font.Font("your_own_font.ttf", 24).
Download a font and either put it in the same folder as your file, or in a data folder.
I use VeraMono and put it in a data folder, so my code would be
...
basicFont = pygame.font.Font("data/VeraMono", 24)
largeFont = pygame.font.Font("data/VeraMono", 35)
pygame.display.set_caption('SCREAM!')
...
If you don't have a data/ folder added to your project yet, then add the following lines to your spec file (the spec file is created as soon as you run you pyinstaller the first time. It has the same name as your python file.)
# -*- mode: python -*-
block_cipher = None
# This entry is new!
added_files = [
( 'data', 'data'),
]
a = Analysis(['CleanTest.py'],
pathex=['/home/angela/Desktop/Arbeit/VisualTest'],
binaries=[],
datas=added_files, # add the new file to your data tree
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
...
Run pyinstaller <your_file>.spec, and hopefully it'll work.
my guess is that it crashes when trying to load the background image:
picture = pygame.image.load('Background.png').convert() #MITwallpaper
When pyinstaller compiles your code, that is all it compiles (including the imported dependencies). It creates the dist directory where you can run your executable and you are good to go.
But, by default, pyinstaller will not pull in external resources. So when you run the executable, it can't find your image because it is in a different directory. You can test this by manually copying the image to the folder where the exe is located and see if it works.
Also, you can run the exe and push the output into a debug file:
your_file.exe > debug.txt
This way you can capture the output from your program (including error messages).