morelists 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
morelists/__init__.py CHANGED
@@ -1,10 +1,29 @@
1
1
  import time
2
+ import json
2
3
 
3
4
  class GameList():
4
- def __init__(self, list = {}, expirationList = {}, flippedList = {}, freeze = False, deltaTime = 0):
5
- self.list = list
6
- self.expirationList = expirationList
7
- self.flippedList = flippedList
5
+ def __init__(self, list = None, expirationList = None, flippedList = None, freeze = False, deltaTime = 0):
6
+ """
7
+ Creates a new GameList
8
+
9
+ Useful when you want to add, subtract, multiply, and divide items
10
+ inside a list to calculate strength of effect or other things.
11
+ Comes with a built-in expiration date for easy use.
12
+
13
+ Perfect for game engine with changing stats like adding buffs and debuffs.
14
+
15
+ Should not receive any arguments without experienced knowledge of the library.
16
+
17
+ Keyword arguments:
18
+ list -- the starting list (default None)
19
+ expirationList -- the starting list tracking expiration dates (default None)
20
+ flippedList -- inverted list, used for faster lookup (default None)
21
+ freeze -- preserve the list and stops the built-in expiration date deletor (default False)
22
+ deltaTime -- tells the list how much behind it is in time. Used in updateToPresent() (default 0)
23
+ """
24
+ self.list: dict = list if list != None else {}
25
+ self.expirationList: dict = expirationList if expirationList != None else {}
26
+ self.flippedList: dict = flippedList if flippedList != None else {}
8
27
 
9
28
  self.addValue = 0
10
29
  self.subtractValue = 0
@@ -19,17 +38,48 @@ class GameList():
19
38
  self.deltaTime = deltaTime
20
39
 
21
40
 
41
+ def safeSum(self):
42
+ """
43
+ Returns the sum in a safe matter.
44
+
45
+ Dont use this, please access the sum by using .sum
46
+ """
47
+ try:
48
+ return (self.addValue - self.subtractValue) * self.multiplyValue / self.divideValue
49
+ except ZeroDivisionError:
50
+ print("[WARNING]: GameList can't sum the list because of a ZeroDivisionError. Defaulted the sum value to be 0.")
51
+ return 0
52
+
53
+
22
54
  def add(self, item, expires = -1):
55
+ """
56
+ Adds a item to the list.
57
+
58
+ Keyword arguments:
59
+ item -- item you wish to add. Format: {"name":"nameHere", "type":"add/subtract/multiply/divide", "value":float}
60
+ expires -- in seconds on how long until it expires and gets deleted. Set it to -1 to allow it to exist forever (default -1)
61
+ """
62
+ immortal = True if expires == -1 else False
23
63
  perf_counter = time.time()
24
- if expires != -1:
25
- expires += perf_counter
64
+ expires += perf_counter
26
65
  self.list[expires] = {"name":item.get("name", ""), "type":item.get("type", "add"), "value":item.get("value", 0)}
66
+
67
+
27
68
  if self.list[expires]["type"] not in ["add", "subtract", "multiply", "divide"]:
28
- self.list[expires]["type"] = "add"
69
+ print(f"[WARNING]: GameList only supports add/subtract/multiply/divide types. Your input type: {self.list[expires]["type"]}. Defaulting to add type.")
70
+ self.list[expires]["type"] = "add"
71
+
72
+ if not "value" in item:
73
+ defaultValue = 0 if self.list[expires]["type"] in ["add", "subtract"] else 1
74
+ self.list[expires]["value"] = defaultValue
75
+ print(f"[WARNING]: GameList uses the key 'value' to store the value of each item. You seem to have not given it a value. Defaulted to {defaultValue}.")
76
+
77
+ if self.list[expires]["name"] == "":
78
+ print(f'[WARNING]: GameList uses the key \'name\' to track items. You seem to have not given it a value. This will make it hard to track and pop at a later time (before its expiration date). Defaulted to "".')
29
79
 
30
- if expires != -1:
80
+ if not immortal:
31
81
  self.expirationList[expires] = self.list[expires]
32
- self.flippedList[str(self.list[expires])] = expires
82
+ self.flippedList[json.dumps(self.list[expires], sort_keys=True)] = expires
33
83
 
34
84
  if item["type"] == "add":
35
85
  self.addValue += item["value"]
@@ -40,16 +90,25 @@ class GameList():
40
90
  elif item["type"] == "divide":
41
91
  self.divideValue += (item["value"] - 1)
42
92
 
43
- object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, (self.addValue - self.subtractValue) * self.multiplyValue / self.divideValue))
93
+ object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, self.safeSum()))
44
94
 
45
95
  def unsafeAdd(self, item, expires = -1):
96
+ """
97
+ Adds a item to the list using a more unsafe method.
98
+
99
+ Only use this if you know what you are doing!
100
+
101
+ Keyword arguments:
102
+ item -- item you wish to add. Format: {"name":"nameHere", "type":"add/subtract/multiply/divide", "value":float}
103
+ expires -- in seconds on how long until it expires and gets deleted. Set it to -1 to allow it to exist forever (default -1)
104
+ """
105
+ immortal = True if expires == -1 else False
46
106
  perf_counter = time.time()
47
- if expires != -1:
48
- expires += perf_counter
107
+ expires += perf_counter
49
108
  self.list[expires] = item
50
- if expires != -1:
109
+ if not immortal:
51
110
  self.expirationList[expires] = self.list[expires]
52
- self.flippedList[str(item)] = expires
111
+ self.flippedList[json.dumps(item, sort_keys=True)] = expires
53
112
 
54
113
  if item["type"] == "add":
55
114
  self.addValue += item["value"]
@@ -60,10 +119,15 @@ class GameList():
60
119
  elif item["type"] == "divide":
61
120
  self.divideValue += (item["value"] - 1)
62
121
 
63
- object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, (self.addValue - self.subtractValue) * self.multiplyValue / self.divideValue))
122
+ object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, self.safeSum()))
64
123
 
65
124
 
66
- def calculateSum(self):
125
+ def calculateSumWithoutUpdate(self):
126
+ """
127
+ Calculates the sum of the list without checking for expired items
128
+
129
+ Use only if you want to access the sum without updating
130
+ """
67
131
  self.addValue = 0
68
132
  self.subtractValue = 0
69
133
  self.multiplyValue = 1
@@ -77,11 +141,20 @@ class GameList():
77
141
  self.multiplyValue += (item["value"] - 1)
78
142
  elif item["type"] == "divide":
79
143
  self.divideValue += (item["value"] - 1)
80
- return (self.addValue - self.subtractValue) * self.multiplyValue / self.divideValue
144
+
145
+ return self.safeSum()
81
146
 
82
147
 
83
148
 
84
149
  def update(self):
150
+ """
151
+ Updates the list and removes any expires items.
152
+
153
+ This is already called upon when accessing the sum or history.
154
+ This function doesn't need to be by the user.
155
+ """
156
+
157
+ # update() ignore the reeze flag because it's assumed the user is calling it with the purpose of updating it manually
85
158
  try:
86
159
  expiration = min(self.expirationList.keys())
87
160
  while expiration < time.time():
@@ -94,75 +167,180 @@ class GameList():
94
167
  else:
95
168
  self.divideValue -= (self.list[expiration]["value"] - 1)
96
169
 
97
- del self.flippedList[str(self.list[expiration])]
170
+ del self.flippedList[json.dumps(self.list[expiration], sort_keys=True)]
98
171
  del self.list[expiration]
99
172
 
100
- object.__getattribute__(self, "history").append((expiration, self.list, self.expirationList, self.flippedList, (self.addValue - self.subtractValue) * self.multiplyValue / self.divideValue))
173
+ object.__getattribute__(self, "history").append((expiration, self.list, self.expirationList, self.flippedList, self.safeSum()))
101
174
  expiration = min(self.expirationList.keys())
102
- except ValueError:
103
- pass
175
+ except ValueError as e:
176
+ print(f"[WARNING]: While updating the list, a new error appeared: {e}")
104
177
 
105
178
  def pause(self):
179
+ """
180
+ Prevents the list from auto-updating and prevents deletions of expired items without user input.
181
+ """
106
182
  self.freeze = True
107
183
 
108
184
  def resume(self):
185
+ """
186
+ Resumes the list to auto-update and delete expired items automatically.
187
+ """
109
188
  self.freeze = False
110
189
 
111
190
  def updateToPresent(self):
112
- pass
191
+ """
192
+ Updates each item in the list to match if the list was in the present. Setting deltaTime changes how much this affects the list.
193
+
194
+ Only use this if you are experienced and know what you are doing.
195
+ """
196
+ newList = {}
197
+ for key, value in self.list.items():
198
+ newList[key + self.deltaTime] = value
199
+ self.deltaTime = 0
113
200
 
114
201
  def restoreState(self, t) -> "GameList":
202
+ """
203
+ Creates a new GameList from the history of the list. Useful if you want to track previous sum values and such.
204
+
205
+ Keyword arguments:
206
+ t -- the time you want the new list to be based on
207
+ """
115
208
  self.update()
116
209
  lastItem = None
117
210
  for item in self.history:
118
211
  if item[0] < t:
119
212
  lastItem = item
213
+ else:
214
+ break
120
215
 
121
216
  if lastItem == None:
122
217
  return GameList()
123
218
  else:
124
219
  return GameList(lastItem[1], lastItem[2], False, time.time() - lastItem[0])
125
220
 
221
+ def getOldSums(self, t0, t1) -> list[tuple]:
222
+ """
223
+ Creates a list of tuples with the sum value from t0 -> t1.
224
+
225
+ Format: [(time, sum_value), ...]
226
+
227
+ Keyword arguments:
228
+ t0 -- the time you want the list to start
229
+ t1 -- the time you want the list to end
230
+ """
231
+ self.update()
232
+ items = []
233
+ for item in self.history:
234
+ if item[0] < t0:
235
+ items = [(item[0], item[4])]
236
+ elif item[0] < t1:
237
+ items.append((item[0], item[4]))
238
+ else:
239
+ break
240
+
241
+ return items
126
242
 
127
243
 
128
244
  def pop(self, name):
245
+ """
246
+ Pops a item from the list with the closest expiration date to the current time
247
+
248
+ Keyword arguments:
249
+ name -- the name you gave the item in the list
250
+ """
129
251
  perf_counter = time.time()
130
- pops = [value for value in self.list.values() if value["name"] == name]
131
- pops.sort(key=lambda a: a["expires"])
252
+ pops = [(key, value) for key, value in self.list.items() if value["name"] == name]
253
+ pops.sort(key=lambda a: a[0])
132
254
  if pops:
133
- del self.list[self.flippedList[str(pops[0])]]
134
- if self.flippedList[str(pops[0])] in self.expirationList: del self.expirationList[self.flippedList[str(pops[0])]]
135
- del self.flippedList[str(pops[0])]
136
- self.calculateSum()
137
- object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, (self.addValue - self.subtractValue) * self.multiplyValue / self.divideValue))
255
+ stringedList = json.dumps(pops[0][1], sort_keys=True)
256
+
257
+ item = self.list[self.flippedList[stringedList]]
258
+ del self.list[self.flippedList[stringedList]]
259
+ if self.flippedList[stringedList] in self.expirationList: del self.expirationList[self.flippedList[stringedList]]
260
+ del self.flippedList[stringedList]
261
+
262
+ if item["type"] == "add":
263
+ self.addValue -= item["value"]
264
+ elif item["type"] == "subtract":
265
+ self.subtractValue -= item["value"]
266
+ elif item["type"] == "multiply":
267
+ self.multiplyValue -= (item["value"] - 1)
268
+ else:
269
+ self.divideValue -= (item["value"] - 1)
270
+
271
+ object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, self.safeSum()))
138
272
 
139
273
  def popAny(self, name):
274
+ """
275
+ Pops a item from the list with no regards for expiration date
276
+
277
+ Keyword arguments:
278
+ name -- the name you gave the item in the list
279
+ """
140
280
  perf_counter = time.time()
141
281
  pops = [value for value in self.list.values() if value["name"] == name]
142
282
  if pops:
143
- del self.list[self.flippedList[str(pops[0])]]
144
- if self.flippedList[str(pops[0])] in self.expirationList: del self.expirationList[self.flippedList[str(pops[0])]]
145
- del self.flippedList[str(pops[0])]
146
- self.calculateSum()
147
- object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, (self.addValue - self.subtractValue) * self.multiplyValue / self.divideValue))
283
+ stringedList = json.dumps(pops[0], sort_keys=True)
284
+
285
+ item = self.list[self.flippedList[stringedList]]
286
+ del self.list[self.flippedList[stringedList]]
287
+ if self.flippedList[stringedList] in self.expirationList: del self.expirationList[self.flippedList[stringedList]]
288
+ del self.flippedList[stringedList]
289
+
290
+ if item["type"] == "add":
291
+ self.addValue -= item["value"]
292
+ elif item["type"] == "subtract":
293
+ self.subtractValue -= item["value"]
294
+ elif item["type"] == "multiply":
295
+ self.multiplyValue -= (item["value"] - 1)
296
+ else:
297
+ self.divideValue -= (item["value"] - 1)
298
+
299
+ object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, self.safeSum()))
148
300
 
149
301
  def popAll(self, name):
302
+ """
303
+ Pops every item from the list with the given name
304
+
305
+ Keyword arguments:
306
+ name -- the name you gave the item(s) in the list
307
+ """
150
308
  perf_counter = time.time()
151
309
  pops = [value for value in self.list.values() if value["name"] == name]
152
310
  if pops:
153
311
  for x in range(len(pops)):
154
- del self.list[self.flippedList[str(pops[x])]]
155
- if self.flippedList[str(pops[x])] in self.expirationList: del self.expirationList[self.flippedList[str(pops[x])]]
156
- del self.flippedList[str(pops[x])]
157
- self.calculateSum()
158
- object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, (self.addValue - self.subtractValue) * self.multiplyValue / self.divideValue))
312
+ stringedList = json.dumps(pops[x], sort_keys=True)
313
+
314
+
315
+ item = self.list[self.flippedList[stringedList]]
316
+ del self.list[self.flippedList[stringedList]]
317
+ if self.flippedList[stringedList] in self.expirationList: del self.expirationList[self.flippedList[stringedList]]
318
+ del self.flippedList[stringedList]
319
+
320
+ if item["type"] == "add":
321
+ self.addValue -= item["value"]
322
+ elif item["type"] == "subtract":
323
+ self.subtractValue -= item["value"]
324
+ elif item["type"] == "multiply":
325
+ self.multiplyValue -= (item["value"] - 1)
326
+ else:
327
+ self.divideValue -= (item["value"] - 1)
328
+
329
+ object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, self.safeSum()))
159
330
 
160
331
  def remove(self, item):
332
+ """
333
+ Removes a specific item from the list (if it exists)
334
+
335
+ Keyword arguments:
336
+ name -- the name you gave the item(s) in the list
337
+ """
161
338
  perf_counter = time.time()
162
- if self.flippedList.get(str(item), None):
163
- del self.list[self.flippedList[str(item)]]
164
- if self.flippedList[str(item)] in self.expirationList: del self.expirationList[self.flippedList[str(item)]]
165
- del self.flippedList[str(item)]
339
+ stringedList = json.dumps(item, sort_keys=True)
340
+ if self.flippedList.get(stringedList, None):
341
+ del self.list[self.flippedList[stringedList]]
342
+ if self.flippedList[stringedList] in self.expirationList: del self.expirationList[self.flippedList[stringedList]]
343
+ del self.flippedList[stringedList]
166
344
 
167
345
  if item["type"] == "add":
168
346
  self.addValue -= item["value"]
@@ -173,14 +351,23 @@ class GameList():
173
351
  else:
174
352
  self.divideValue -= (item["value"] - 1)
175
353
 
176
- object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, (self.addValue - self.subtractValue) * self.multiplyValue / self.divideValue))
354
+ object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, self.safeSum()))
177
355
 
178
356
  def unsafeRemove(self, item):
357
+ """
358
+ Removes a specific item from the list (if it exists)
359
+
360
+ More unsafe compared to remove(item), so use only if you know what you are doing!
361
+
362
+ Keyword arguments:
363
+ name -- the name you gave the item(s) in the list
364
+ """
179
365
  perf_counter = time.time()
180
366
  if item in self.list.values():
181
- del self.list[dict(self.flippedList[str(item)])]
182
- if dict(self.flippedList[str(item)]) in self.expirationList: del self.expirationList[dict(self.flippedList[str(item)])]
183
- del self.flippedList[str(item)]
367
+ stringedList = json.dumps(item, sort_keys=True)
368
+ del self.list[dict(self.flippedList[stringedList])]
369
+ if dict(self.flippedList[stringedList]) in self.expirationList: del self.expirationList[dict(self.flippedList[stringedList])]
370
+ del self.flippedList[stringedList]
184
371
 
185
372
  if item["type"] == "add":
186
373
  self.addValue -= item["value"]
@@ -191,16 +378,28 @@ class GameList():
191
378
  else:
192
379
  self.divideValue -= (item["value"] - 1)
193
380
 
194
- object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, (self.addValue - self.subtractValue) * self.multiplyValue / self.divideValue))
381
+ object.__getattribute__(self, "history").append((perf_counter, self.list, self.expirationList, self.flippedList, self.safeSum()))
195
382
 
196
383
  def __getattribute__(self, name):
384
+ """
385
+ Retrieves attributes from the class
386
+
387
+ Updates the list if you grab the sum value or the history data
388
+
389
+ Keyword arguments:
390
+ name -- the name of the attribute you want to grab
391
+ """
197
392
  if name == "sum":
198
393
  if not object.__getattribute__(self, "freeze"):
199
394
  self.update()
200
- return (object.__getattribute__(self, "addValue") -
201
- object.__getattribute__(self, "subtractValue")) * \
202
- object.__getattribute__(self, "multiplyValue") / \
203
- object.__getattribute__(self, "divideValue")
395
+ try:
396
+ return (object.__getattribute__(self, "addValue") -
397
+ object.__getattribute__(self, "subtractValue")) * \
398
+ object.__getattribute__(self, "multiplyValue") / \
399
+ object.__getattribute__(self, "divideValue")
400
+ except ZeroDivisionError:
401
+ print("[WARNING]: While retrieving the sum, a ZeroDivisionError showed up. Defaulting to 0")
402
+ return 0
204
403
  elif name == "history":
205
404
  if not object.__getattribute__(self, "freeze"):
206
405
  self.update()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: morelists
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: A small and easy list you can add together like a math equation and has a expiration date for items. Mostly useful for games with stats from multiple sources. More lists will come soon.
5
5
  Home-page: https://github.com/EmanuelNorsk/enlist
6
6
  Author: Emanuel Odén Hesselroth
@@ -0,0 +1,6 @@
1
+ morelists/__init__.py,sha256=nnk03JRnoaIVtFaqlPhIZFiyLzW2fdKBkA1gAJsBjv8,17063
2
+ morelists-0.1.3.dist-info/licenses/LICENSE,sha256=-ASFHlrne1rk8zV57Qj01X2JB-D67ZHPMv1PtQhrbN8,32
3
+ morelists-0.1.3.dist-info/METADATA,sha256=AC3NZ-WtGgffMMiY7sk9ER9pxh8CVUGVq-Uj36yxgPA,681
4
+ morelists-0.1.3.dist-info/WHEEL,sha256=pxyMxgL8-pra_rKaQ4drOZAegBVuX-G_4nRHjjgWbmo,91
5
+ morelists-0.1.3.dist-info/top_level.txt,sha256=Zd7NosYzor-RcH_aD86FXJa3fQzWunYA4_FQS3Yodqo,10
6
+ morelists-0.1.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (78.1.1)
2
+ Generator: setuptools (79.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,6 +0,0 @@
1
- morelists/__init__.py,sha256=2ADwt9v-X0M-agPoRLKfELfbIAzxxcjtSvyL_ltJIeo,9344
2
- morelists-0.1.1.dist-info/licenses/LICENSE,sha256=-ASFHlrne1rk8zV57Qj01X2JB-D67ZHPMv1PtQhrbN8,32
3
- morelists-0.1.1.dist-info/METADATA,sha256=2AmxOSbe5kT0sziBKdoebqVI9DCtjqAx4i6A2gSGt8Q,681
4
- morelists-0.1.1.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
5
- morelists-0.1.1.dist-info/top_level.txt,sha256=Zd7NosYzor-RcH_aD86FXJa3fQzWunYA4_FQS3Yodqo,10
6
- morelists-0.1.1.dist-info/RECORD,,