Skip to content

Commit

Permalink
Put data types into their own seperate package
Browse files Browse the repository at this point in the history
  • Loading branch information
theangrybagel committed Feb 22, 2021
1 parent 90c50e0 commit 5ac33f3
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 140 deletions.
7 changes: 7 additions & 0 deletions JSON4JSON/DataTypes/DefaultTypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .basics import anyType, arrayType, string, number, boolean
from .dictType import dictType
from .units import distance, time
def LoadDefaultDatatypes(j4jObject):
dts = [anyType, arrayType, dictType, distance, string, number, boolean, time]
for x in dts:
j4jObject.addDataType(x)
Empty file added JSON4JSON/DataTypes/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
78 changes: 78 additions & 0 deletions JSON4JSON/DataTypes/basics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from ..utils import seperate_string_number
import math





class string:
def __init__(self, ajson):
self.ajson = ajson
self.name = "string"
self.default = ""
#returns true/false, whether or not the data is this type
def matches(self, data):
if type(data) == str:
return True
return False
def convert(self, data, rule): #strings have no rules YET (todo: add min/max length)
return str(data)

class number:
def __init__(self, ajson):
self.name = "number"
self.ajson = ajson
self.default = 0
#returns true/false, whether or not the data is this type
def matches(self, data):
newData = data
try:
newData = float(data)
except:
return False
return type(newData) == float or type(newData) == int
def convert(self, data, rule):
value = float(data)
minval = self.ajson.getProperty(rule, "min", noneFound=-math.inf)
maxval = self.ajson.getProperty(rule, "max", noneFound=math.inf)
isInt = self.ajson.getProperty(rule, "int", noneFound=False)
if isInt:
value = int(value)
return min(max(value, minval), maxval)

class anyType:
def __init__(self, ajson):
self.name = "any"
self.ajson = ajson
self.default = None
#returns true/false, whether or not the data is this type
def matches(self, data):
return True
def convert(self, data, rule):
return data

class boolean:
def __init__(self, ajson):
self.name = "bool"
self.ajson = ajson
self.default = False
#returns true/false, whether or not the data is this type
def matches(self, data):
return type(data) == bool
def convert(self, data, rule):
return data == True
class arrayType:
def __init__(self, ajson):
self.name = "array"
self.ajson = ajson
self.default = []
#returns true/false, whether or not the data is this type
def matches(self, data):
if type(data) == list:
return True
return False
def convert(self, data, rules):#this ones different, since it's a dictionary, therefore it has multiple values of different types (including other dictionaries/objects)
for x,i in zip(data, range(0, len(data))):
#convert single
data[i] = self.ajson.convertSingle(x, rules['rule'])
return data
29 changes: 29 additions & 0 deletions JSON4JSON/DataTypes/dictType.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

class dictType:
def __init__(self, ajson):
self.name = "object"
self.ajson = ajson
self.default = {}
#returns true/false, whether or not the data is this type
def matches(self, data):
if type(data) == dict:
return True
return False
def convert(self, data, rules):#this ones different, since it's a dictionary, therefore it has multiple values of different types (including other dictionaries/objects)
oldRules = rules
rules = oldRules['rules']
for key in rules:
if key.startswith("//"): #ignore, this is a comment
continue
if key[0] == "@": #this overrides default settings
self.ajson.set_value(self.ajson.defaults, key.split("@")[1], rules[key])
continue
if key not in data:
self.ajson.log(f"Key \"{key}\" not in data file. Setting to default.", level=-2)
data[key] = self.ajson.getDefault(rules[key])
#check if this makes a variable. if so, set the variable to the value.
if "varSet" in rules[key]:
self.ajson.log(f"Setting variable {rules[key]['varSet']}", level=-1)
self.ajson.vars[rules[key]['varSet']] = data[key]
data[key] = self.ajson.convertSingle(data[key], rules[key])
return data
27 changes: 27 additions & 0 deletions JSON4JSON/DataTypes/units.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from .basics import seperate_string_number
class unit:
def __init__(self, ajson, name, default, units={}):
self.name = name
self.ajson = ajson
self.default = default
self.units = units
#returns true/false, whether or not the data is this type
def matches(self, data):
if type(data) == str:
if seperate_string_number(data)[1] in self.units:
return True
else:
self.ajson.log(f"Error: {self.name} does not recognize \"{seperate_string_number(data)[1]}\" as a valid unit.", level=5)
return False
def convert(self, data, rule):
value = seperate_string_number(data)
return self.units[value[1]] * float(value[0])


class time(unit):
def __init__(self, ajson):
super().__init__(ajson, "time", "0s", units={"":1, "sec": 1, "secs": 1, "s": 1, "m": 60, "min": 60, "mins": 60, "h": 60*60, "d": 60*60*24, "W": 60*60*24*7, "M": 60*60*24*7*30, "Y": 60*60*24*365})

class distance(unit):
def __init__(self, ajson):
super().__init__(ajson, "distance", "0m", units={"":1, "me": 1, "m": 1, "km": 1000, "ft": 3.280839895, "mi": 1000 * 0.621371, "cm": .001, "in": 3.280839895/12})
16 changes: 6 additions & 10 deletions JSON4JSON/JSON4JSON.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import json, os, sys, traceback
from .datatypes import DataTypes
from .utils import seperate_string_number
#region utils from the internet
#can't remember where I stole this from, somewhere on stack overflow.
#endregion
from .DataTypes import DefaultTypes

class JSON4JSON:
def __init__(self):
self.dataTypes = {}
Expand All @@ -16,12 +14,10 @@ def __init__(self):
"tracebackLogging": True
}
self.data = {} #this is the loaded and mostly filtered data
for x in DataTypes.allTypes:
xinstance = x(self)
self.dataTypes[xinstance.name] = xinstance



DefaultTypes.LoadDefaultDatatypes(self)
def addDataType(self, dataType):
dataTypeInstance = dataType(self)
self.dataTypes[dataTypeInstance.name] = dataTypeInstance
def load(self, jsonFile, ruleFile):
data = None
rules = None
Expand Down
Binary file modified JSON4JSON/__pycache__/JSON4JSON.cpython-39.pyc
Binary file not shown.
Binary file modified JSON4JSON/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
130 changes: 0 additions & 130 deletions JSON4JSON/datatypes.py

This file was deleted.

0 comments on commit 5ac33f3

Please sign in to comment.