Data management with Python

Right now I am working on some software to educate public speakers. One of the requirements for the software is that it records and stores data gathered in real time during a talk and be able to be accessed later for playback and data analysis. This data needs to be managed in such a way that I can easily search through it given a few tags and pull the corresponding information.

I’m developing in a program called TouchDesigner which uses python 3. In TouchDesigner COMP objects are easily extended using python classes so this is where I will start. Matthew Ragan (https://matthewragan.com) has a tremendously helpful tutorial on ‘Understanding Extensions’ here.

I will post the full code here with some comment but to see it in action download the file on Github: https://github.com/raganmd/TD-Examples/tree/master/shelanskey/Data_Management

import os
import string
import random
import json

class DB:
	def __init__(self):
		'''Initializes records cache if nothing exists.
		Looks in current filepath for a DB collection and creates one if it doesn't exist. 
		Preloads DB with json metadata. 
		
		Args: 
			self: self
		
		Output: 
		
		'''
		self.filepath = parent().par.Path + '/db' 			#grab path from parents custom parameters
		if not os.path.isdir(self.filepath):				#check to see if that path exists
			os.makedirs(self.filepath)						#make new directory if it doesn't
			
			f = open(self.filepath+'/cache.txt', "w")		#create a file called cache
			jsonPrescriptData = {}
			jsonPrescriptData['meta'] = "DB Record Cache"
			jsonPrescriptData['records'] = [] 
			f.write(json.dumps(jsonPrescriptData))     		#load it with metadata
			f.close()
			
			print('> New Database created at: ' + self.filepath)
		else:
			print('> Mounting Database at: '+ self.filepath)
			print("|\n| Number of records: "+ str( len(json.loads(self.unpack())['records'])) )
		return
		
	def Insert(self, dict):
		'''Inserts key and name with pointer to folder holding content
		
		Args:
			self: self
			dict: A dict that is to be inserted into cache
		
		Output: 
			returns path to new folder
		'''
		mypath = self.filepath								
		key = self.keygen()									#create unique key
		newFolder = mypath + '/records/' + key				
		if not os.path.isdir(newFolder):					#check if it exists
			os.makedirs(newFolder)							#create new
			dict['Key'] = key								#pack cache record with directory name
			self.pack(dict)
												
			print("> Successfully created record!")
			print("|\n| Unique Identifier: " +key)
			print("| Record data: \n"+ json.dumps(dict, sort_keys=True, indent=4, separators=(',', ': ')))
		return newFolder
	
	
	def Query(self, query):
		'''Queries cache for records that match search parameters
		
		Args:
			self: self
			query: a dict that is used to lookup and match cache data
					{
						"Name": "Ian",
					 	"Age": 24
					}
		
		Output:
			prints query results.
		'''
		cache = json.loads(self.unpack())
		for key in list(query.keys()):
			data = query[key]
			cache["records"] = [item for item in cache["records"] if key in item and item[key] == data]
		
		print("Query Results: \n" + json.dumps(cache, sort_keys=True, indent=4, separators=(',', ': ')))
		return
	
		
	def Delete(self):
		return
	
	
	def pack(self, dict):
		'''
		Packs Json DB Item
		'''
		f = open(self.filepath+'/cache.txt', 'r')
		cache = json.loads(f.read())
		f.close()
		
		cache['records'].append(dict)
		f = open(self.filepath+'/cache.txt', 'w')
		f.write(json.dumps(cache, sort_keys=True, indent=4, separators=(',', ': ')))
		f.close()
		return
		
		
	def unpack(self):
		'''
		Unpacks Json DB Item
		'''
		f = open(self.filepath+'/cache.txt', 'r')
		data = f.read()
		f.close()
		return data
	
	
	def keygen(self, size=6, chars=string.ascii_uppercase + string.digits):
		'''
		Generates key for folder.  
		'''
		return ''.join(random.choice(chars) for _ in range(size))

 

Leave a Reply