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 ( 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:

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. 
			self: self
		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
			print('> New Database created at: ' + self.filepath)
			print('> Mounting Database at: '+ self.filepath)
			print("|\n| Number of records: "+ str( len(json.loads(self.unpack())['records'])) )
	def Insert(self, dict):
		'''Inserts key and name with pointer to folder holding content
			self: self
			dict: A dict that is to be inserted into cache
			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
			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
			self: self
			query: a dict that is used to lookup and match cache data
						"Name": "Ian",
					 	"Age": 24
			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=(',', ': ')))
	def Delete(self):
	def pack(self, dict):
		Packs Json DB Item
		f = open(self.filepath+'/cache.txt', 'r')
		cache = json.loads(
		f = open(self.filepath+'/cache.txt', 'w')
		f.write(json.dumps(cache, sort_keys=True, indent=4, separators=(',', ': ')))
	def unpack(self):
		Unpacks Json DB Item
		f = open(self.filepath+'/cache.txt', 'r')
		data =
		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

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s