-
Notifications
You must be signed in to change notification settings - Fork 4
/
tasks.py
121 lines (92 loc) · 3.42 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import json, logging , time, requests, re, os,math,cairo
from typing import *
from dotenv import load_dotenv
# global resource paths
IMAGE_PATH='res/img'
FONT_PATH='res/fonts'
JS_PATH='res/js'
# Load any environment variables from .env
load_dotenv()
#configure logging
logging.basicConfig(filename='log.txt', level=logging.INFO)
#create a surface to draw pacman
# surface=cairo.ImageSurface(cairo.FORMAT_RGB24,600,400)
# ctx=cairo.Context(surface)
# ctx.set_source_rgb(.33, .33, .33)
# ctx.stroke()
def list_to_json(word_list:list,file_name:str)->None:
# Convert list to JSON array
json_array = {str(i): word for i, word in enumerate(word_list)}
output_file=f'{file_name}.json'
# Open the file in write mode
with open(output_file, 'w') as output_file:
# Write the JSON data to the file
json.dump(json_array, output_file, indent=2)
# Log a message with a timestamp
timestamp = time.ctime()
log_message = f"{timestamp} - File {file_name}.json created successfully"
logging.info(log_message)
def read_json(file_name:str)->Dict[int,str]:
# read the file path
#change to the dict that works
with open(f'{file_name}.json') as json_file:
data = json.load(json_file)
return data
# this function removes html tags from a string
def remove_html_tags(text):
"""Remove html tags from a string"""
clean = re.compile('<.*?>')
return re.sub(clean, '', text)
# this function defines a word using wordnik api
def defineWord(word:str):
#wordnik only accesses word as lowercase
word=word.lower()
# Access your wordnik API key
wordnik_api_key = os.getenv("WORDNIK_API_KEY")
url='https://api.wordnik.com/v4/word.json/{}/definitions'.format(word)
params={
'limit':200,
'partOfSpeech':'noun',
'includeRelated':'false',
'sourceDictionaries':'wiktionary',
'useCanonical':'false',
'includeTags':'false',
'api_key':f'{wordnik_api_key}'
}
response=requests.get(url,params=params)
try:
return remove_html_tags(response.json()[0]['text'])
except KeyError:
return response.json()['message']
# # this function takes custom circle params
# #the context is passed because this function is called from outside this function
# def draw_circle(x, y, r,start_angle:int,end_angle:int,ctx=ctx,)->None :
# # ,red=.33, green=.67, blue=0
# # Set the circle's center coordinates (x, y) and radius (r)
# #start angle is where your arc starts
# #end angle is where your arc ends
# ctx.arc(x, y, r, (start_angle)* math.pi, (end_angle)* math.pi)
# ctx.set_source_rgb(1, 0, 0)
# ctx.fill()
#the functions are for integrating cairo and pygame
#dont use them for now
def returnSurface(width=800,height=600 ):
# width, height = 800, 600
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
return surface
def drawCairoSurface():
# width, height = 800, 600
surface = returnSurface()
context = cairo.Context(surface)
return context
def drawShield(context):
context.set_source_rgb(0, 0, 1) # Set color to blue
context.set_line_width(5)
# Draw shield shape
context.move_to(400, 300) # Move to center
for angle in range(-45, 46, 10):
x = 400 + 30 * math.cos(math.radians(angle))
y = 300 + 30 * math.sin(math.radians(angle))
context.line_to(x, y)
context.close_path()
context.fill()