view V4/synastry.py @ 16:b11cff4b7f83

implement compatibility scores
author DaveM
date Mon, 05 Mar 2018 14:22:09 +0000
parents 50a95089414d
children ae220e89cb3a
line wrap: on
line source
import random
import csv
import re
import pdb

class compatibility:
	def __init__(self):
		self.rules = []
		self.uniqueID = 0
		print 'Warning, need to fix [Vertex, Ascendant House, None Location] issues'

	def addRule(self,planetTuple,aspectList,score):
		rule = compatibilityRule(self.uniqueID,planetTuple,aspectList,score)
		self.rules.append(rule)
		self.uniqueID += 1

	def findRule(self,planet):
		ruleList = [r for r in self.rules if r.planet == planet]
		print ruleList
		return ruleList

	def readFile(self,filename):
		text = []
		with open(filename) as fp:
			line = fp.readline()
			text.append(line.strip())
			while line:
				line = fp.readline()
				text.append(line.strip())
		return text	

	def parseCompatRules(self,filename):
		comList = self.readFile(filename)
		# compat = {}
		for com in comList:
			c = com.split(',')
			# print c
			if len(c) == 4:
				if len(c[1]) > 1:
					aspectList = []
					planetTuple = tuple(sorted((c[0].lower(),c[2].lower())))
					score = c[3]
					for subset in c[1].split(' '):
						aspectList.append(subset)
				elif len(c[1]) == 0 :
					if 'house' not in c[2]:
						print 'some error where rule is not house'
					planetTuple = (c[0].lower(),'house')
					houseNo = re.findall('\d+', c[2])
					if len(houseNo) != 1:
						print 'multiple house numbers found - ERROR'
					aspectList = int(houseNo[0])
					score = int(c[3])
			self.addRule(planetTuple,aspectList,score)
		# return compat
		# pdb.set_trace()

	def calcCompatibility(self, horiscope):
		score = 0
		for r in self.rules:
			if 'vertex' in r.planet:
				# print 'ERROR - catch Vertex issue'
				pass
			elif r.planet[1]  == 'house' and r.planet[0] == 'asc':
				# print 'ERROR - catch Ascendant House issue'
				pass
			elif horiscope.planets[r.planet[0]].angleA is None:
				# print 'Error - None Location'
				return None
			elif r.planet[1] == 'house':
				if horiscope.isInHouse(r.planet[0],r.aspect):
					score += r.score
			else:
				# print 'query standard synastry'
				aspect,variance = horiscope.calcAspect(r.planet[0],r.planet[1])
				if aspect is not None:
					if aspect in r.aspect:
						score += r.score
		return score



class compatibilityRule:
	def __init__(self,uniqueId,planetTuple,aspectList,score):
		self.id = uniqueId
		self.planet = planetTuple
		self.aspect = aspectList
		self.score = score

class Person:
	url = 'https://horoscopes.astro-seek.com/calculate-love-compatibility/'
	def __init__(self,personDict):
		# self. = planetPositions()
		self.id = personDict['ID']
		self.dob = personDict['DOB']
		self.tob = personDict['TOB']
		self.cob = personDict['COB']
		self.p_dob = personDict['pDOB']
		self.p_tob = personDict['pTOB']
		self.p_cob = personDict['pCOB']
		self.horiscope = None

	def identifyIssues(self):
		if self.id is None:
			self.issue = 'id'
		elif self.dob is None:
			self.issue = 'dob'
		elif self.tob is None:
			self.issue = 'tob'
		elif self.cob is None:
			self.issue = 'cob'
		elif self.p_dob is None:
			self.issue = 'p_dob'
		elif self.p_tob is None:
			self.issue = 'p_tob'
		elif self.p_cob is None:
			self.issue = 'p_cob'
		else:
			self.issue = None
		return self.issue

	def makePayload(self):
		if type(self.cob) is str:
			cob_0 = float(self.cob.split(',')[0][1:])
			cob_1 = float(self.cob.split(',')[1])
			self.cob = (cob_0,cob_1)
		if type(self.p_cob) is str:
			pcob_0 = float(self.p_cob.split(',')[0][1:])
			pcob_1 = float(self.p_cob.split(',')[1])
			self.p_cob = (pcob_0,pcob_1)
		if type(self.dob) is str:
			self.dob = self.dob[1:-1].split(',')
		if type(self.p_dob) is str:
			self.p_dob = self.p_dob[1:-1].split(',')
		if type(self.tob) is str:
			self.tob = self.tob[1:-1].split(',')
		if type(self.p_tob) is str:
			self.p_tob = self.p_tob[1:-1].split(',')
		# pdb.set_trace()

		# print dataDict['pDOB']

		self.payload = {'send_calculation':'1', #Req
			'muz_narozeni_den':self.dob[0],
			'muz_narozeni_mesic':self.dob[1],
			'muz_narozeni_rok':self.dob[2],
			'muz_narozeni_hodina':self.tob[0],
			'muz_narozeni_minuta':self.tob[1],
			'muz_narozeni_city':'',
			'muz_narozeni_mesto_hidden':'Manually+place%3A+%C2%B0%27N%2C+%C2%B0%27E',#auto
			'muz_narozeni_stat_hidden':'XX',
			'muz_narozeni_podstat_kratky_hidden':'',
			'muz_narozeni_podstat_hidden':'',
			'muz_narozeni_podstat2_kratky_hidden':'',
			'muz_narozeni_podstat3_kratky_hidden':'',
			'muz_narozeni_input_hidden':'',
			'muz_narozeni_sirka_stupne':str(abs(self.cob[0])).split('.')[0],
			'muz_narozeni_sirka_minuty':str(float('0.'+str(self.cob[0]).split('.')[1])*60).split('.')[0],
			'muz_narozeni_sirka_smer': '1' if self.cob[0]<0 else '0', #address N Dir (0':'N',1':'S)
			'muz_narozeni_delka_stupne':str(abs(self.cob[1])).split('.')[0], #address E - Main
			'muz_narozeni_delka_minuty':str(float('0.'+str(self.cob[1]).split('.')[1])*60).split('.')[0],
			'muz_narozeni_delka_smer': '1' if self.cob[1]<0 else '0', #address E Dir (0':'E',1':'W)
			'muz_narozeni_timezone_form':'auto',
			'muz_narozeni_timezone_dst_form':'auto',
			'send_calculation':'1',
			'zena_narozeni_den':self.p_dob[0],
			'zena_narozeni_mesic':self.p_dob[1],
			'zena_narozeni_rok':self.p_dob[2],
			'zena_narozeni_hodina':self.p_tob[0],
			'zena_narozeni_minuta':self.p_tob[1],
			'zena_narozeni_city':'',
			'zena_narozeni_mesto_hidden':'Manually+place%3A+%C2%B0%27N%2C+%C2%B0%27E',
			'zena_narozeni_stat_hidden':'XX',
			'zena_narozeni_podstat_kratky_hidden':'',
			'zena_narozeni_podstat_hidden':'',
			'zena_narozeni_podstat2_kratky_hidden':'',
			'zena_narozeni_podstat3_kratky_hidden':'',
			'zena_narozeni_input_hidden':'',
			'zena_narozeni_sirka_stupne':str(abs(self.p_cob[0])).split('.')[0],
			'zena_narozeni_sirka_minuty':str(float('0.'+str(self.p_cob[0]).split('.')[1])*60).split('.')[0],
			'zena_narozeni_sirka_smer': '1' if self.p_cob[0]<0 else '0',
			'zena_narozeni_delka_stupne':str(abs(self.p_cob[1])).split('.')[0],
			'zena_narozeni_delka_minuty':str(float('0.'+str(self.p_cob[1]).split('.')[1])*60).split('.')[0],
			'zena_narozeni_delka_smer': '1' if self.p_cob[1]<0 else '0',
			'zena_narozeni_timezone_form':'auto',
			'zena_narozeni_timezone_dst_form':'auto',
			'switch_interpretations':'0',
			'house_system':'placidus',
			'uhel_orbis':'#tabs_redraw'}


class planetRelation:
	noHouseList = ['asc','ic','dsc','mc','asc/mc','sun/moon']
	def __init__(self,planetName):
		self.name = planetName
		self.angleA = None
		self.angleB = None
		if planetName not in planetRelation.noHouseList:
			self.houseA = None
			self.houseB = None

	def setLocation(self,A,B):
		self.angleA = float('.'.join(A.encode('ascii','replace').split('?'))[:-1])
		self.angleB = float('.'.join(B.encode('ascii','replace').split('?'))[:-1])
		# print self.angleA,self.angleB

	def setHouse(self,A,B):
		self.houseA = int(A.encode('ascii','ignore'))
		self.houseB = int(B.encode('ascii','ignore'))
		# print self.houseA,self.houseB

	def test_random(self):
		self.angleA = random.random()*360
		self.angleB = random.random()*360


class planetPositions:
	aspectDict = {'conjunction':0,'semi-square':45,'sextile':60,'square':90,'trine':120,'opposite':180}
	planetNames = ['sun','moon','mercury','venus','mars','jupiter','saturn','uranus','neptune','pluto','node','lilith','chiron','asc','ic','dsc','mc','asc/mc','sun/moon']
	def __init__(self):
		self.planets = {}
		for p in planetPositions.planetNames:
			self.planets[p] = planetRelation(p)
		self.aspect = {}

	def test_random(self):
		for planet in self.planets:
			self.planets[planet].test_random()

	def calcAngle(self,componentA,componentB):
		self.angle = max(self.planets[componentA].angleA,self.planets[componentB].angleB) - min(self.planets[componentA].angleA,self.planets[componentB].angleB)
		self.angleRange = self.angle-10,self.angle+10

	def calcAspect(self,componentA,componentB):
		self.calcAngle(componentA,componentB)
		for aspect in planetPositions.aspectDict:
			if self.angleRange[0] < planetPositions.aspectDict[aspect] and self.angleRange[1] > planetPositions.aspectDict[aspect]:
				aspectDiff = abs(self.angle - planetPositions.aspectDict[aspect])
				self.aspect[componentA,componentB] = (aspect,aspectDiff)
				return aspect,aspectDiff
		return None,None

	def isInHouse(self,component,houseNo):
		# print (self.planets[component].houseA,self.planets[component].houseB,houseNo)
		if (self.planets[component].houseA == houseNo) or (self.planets[component].houseB == houseNo):
			return True
		return False
		
	def calcAllAspects(self):
		for p1 in planetPositions.planetNames:
			for p2 in planetPositions.planetNames:
				self.calcAspect(p1,p2)

	def printPositions(self):
		for p in planetPositions.planetNames:
			if p in planetRelation.noHouseList:
				print p,self.planets[p].angleA,self.planets[p].angleB
			else:
				print p,self.planets[p].angleA,self.planets[p].angleB,self.planets[p].houseA,self.planets[p].houseB