Overwrite = False#WARNING: ERROR-PRONE
PicOverwrite = True
Debuging = False
Character = ""
from time import time
from math import floor
SpecialAffixes = {
	"HOT":"hot",
	"COLD":"cold",
	"PIG": "Pig",
	"VERYLOW": "very low",
	"BURNT": "burnt",#----------Not needed, except stupid mangrove
	"INGROUND": "sleeping",
	"ITEM": "held",
	#"ACTIVE": "active",#----------Not needed
	#"STUMP": "stump",
	"PLANTED": "planted",
	#"WITHERED": "withered",#----------Not needed
	"NOHONEY": "no honey",
	"SOMEHONEY": "some honey",
	"FULLHONEY": "full of honey",
	"DONE": "finished",
	"COOKING_LONG":"cooking, long time left",
	"COOKING_SHORT":"cooking, short time left",
	"NEEDSFERTILIZER": "needs fertilizer",
	"DRYINGINRAIN": "drying in rain",
	"WATER": "in water",
	"LOWFUEL": "low fuel",
	"VALID": "full",
	"GEM": "missing gem",
	"SAND": "sand",
	"LONG": "stage 1",
	"MEDIUM": "stage 2",
	"SOON": "stage 3",
	"HAUNTED_GROUND": "ready",
	"HAUNTED_POCKET": "ready, held",
	"CHOPPED": "stump",
	"BLOOM": "blooming",
	"WEBBED": "webbed",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
}
PicAffixes = [
	"Stump",
	"Withered",
	"Burnt",
	"Picked",
	"Planted",
	"Sleeping",
	"Naked",
	"Dug",
	"Barren",
	"Naked",
	"No Honey",
	"Some Honey",
	"Full Of Honey",
	"Stage 1",
	"Stage 2",
	"Stage 3",
	"Ready, Held",
	"Ready",
	"Blooming",
	"Webbed",
	"Sand",
	#"",
	#"",
	#"",
	#"",
	#"",
	#"",

]
Cleaning = {
	"(picked up)": "(held)",
	"(in the ground)": "(planted)",
	"(burned out)": "(out)",
	"(burnt out)": "(out)",
	"(run out)": "(out)",
	" (unpicked)":"",
	"(picked, the remains on the ground)": "(picked)",
	"(inventory)": "(held)",
	" (placed)": "",
	"(turned off)": "(off)",
	"(turned on)": "(on)",
	"Mangrove Tree ": "Mangrove ",
	"Octopus Chest ": "Octo Chest ",
	"Cooked Wobster": "Delicious Wobster",
	"Watermelon (cooked)": "Grilled Watermelon",
	"Carrot (picked)": "Carrot",
	"Roasted Seeds ": "Toasted Seeds ",
	"(summer)": "(empty)",#walruscamp
	"(full with honey)": "(full of honey)",
	"Crock Pot ": "Crock Pot (empty) ",
	"Ash ": "Ashes ",
	"Divining Rod Base": "Divining Rod Holder",
	"Wormhole": "Worm Hole",
	"Bird Egg": "Egg",
	"Spider Hat": "Spiderhat",
	"Star-Sky": "Star-sky",
	"(shaved)": "(naked)",
	"(following)": "(follower)",
	"Eyeplant": "Eyeplant",
	"Lureplant": "Meat Bulb",
	"Quacken Tentacles": "Quacken Tentacle",
	"(closed)": "",
	"Bernie ": "Bernie (active) ",
	"Water Balloon": "Waterballoon",
	"Socket (empty)": "Socket (missing gem)",
	"Focus (incomplete)": "Focus (missing gem)",
	"Focus (charged)": "Focus (full)",
	#"*Animal Tracks":"*{{Pic32|Shown track}}Animal Track",
	"(depleted)": "(sand)",
	"Abigail's flower": "Abigail's Flower",
	"Elephant Cactus (active)": "Prickly Elephant Cactus",
	"Elephant Cactus (stump)": "Elephant Cactus Stump",
	"(chopped)": "(stump)",
	"(unpicked, sleeping)": "(sleeping)",#mushrooms
	"Cut Grass Tuft": "Cut Grass",
	" ": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
}
SpecialNounChange = {
	"Grass": "Grass Tuft",
	"Improved Farm": "Basic and Improved Farm",
	"Basic Farm": "Basic and Improved Farm",
	"Net": "Bug Net",
	"Pig Man": "Pig",
	"Gem Stand": "Telelocator Socket",
	#"Prickly Elephant Cactus": "Elephant Cactus",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
}
SpecialPrefixes = {
	"DISEASED": "Diseased ",
	"POISON": "Poison ",
	"DEAD": "Dead ",#idk if actually doing anything
	"LIMITED": "Sick ",#1  way worm hole
	"WINTER": "Winter ",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
	#"": "",
}
PicRename = {

	"Lumpy Evergreen":"A Lumpy Evergreen",
	"The End is Nigh!":"The End is Nigh",
	"Sandbag":"Sand Bag Structure",
	"Sandbag Held":"Sand Bag",
	"Mangrove":"Mangrove Tree",
	"Mangrove Burnt":"Mangrove Tree Burnt",
	"Basalt":"Basalt1",
	"Walrus Camp":"Walrus Camp Winter",
	"Merm Hut":"Merm House Shipwrecked",
	"Slot Machine":"Slotmachine",
	"Octo Chest":"Octopus Chest Build",
	"Poisonous Hole":"Poison Hole",
	"X Marks the Spot":"X Marks The Spot",
	"Blue Mushtree":"Mushtree",
	"Red Mushtree":"MushtreeRed",
	"Green Mushtree":"MushtreeGreen",
	"Splumonkey Pod":"Monkey Totem",
	"Ancient Statue":"Ancient",
	"Relic":"Relic Chair",
	"Clockwork Rook":"Rook",
	"Damaged Bishop":"Damaged bishop",
	"Damaged Rook":"Damaged Clockwork",
	"Ghost":"Ghost Build",
	"Wee MacTusk":"WeeTusk",
	"Abigail":"Abigail build",
	"Delicious Wobster":"Cooked Wobster",
	"Roasted Coconut":"Cooked Coconut",
	"Roasted Coffee Beans":"Cooked Coffee Beans",
	"Webber's Skull":"Webberskull",
	"Maxwell's Phonograph":"Gramophone",
	"Maxwell's Mosquito Trap":"Minetranspattempt2",
	"Charcoal Boulder":"Rock charcoal",
	"Burnt Ash Tree":"Ash Tree",
	"Deadly Feast":"Meaty Stew",
	"Maxwell's Head":"Maxwell Head",
	"Basic and Improved Farm Growing":"Basic Farm}}{{Pic32|Turbo Farm Plot",#hax
	"Pick/Axe":"PickSlashAxe",
	"Juicy Berry Bush":"Berry bush Juicy",
	"Berry Bush Dug":"Berry Bush Item",
	"Crabbit Den":"Rabbit Hole",
	"Large Ornate Chest":"Ornate Chest",
	"Fishermerm's Hut":"Mermhouse Fisher",
	"Plugged Sinkhole":"Sinkhole",
	"Elephant Cactus":"Withered Elephant Cactus",
	"Prickly Elephant Cactus":"Elephant Cactus",
	"Elephant Cactus Stump":"Elephant Cactus Stump 2",
	"Eye Plant":"Eyeplant",
	"Meat Bulb":"Lureplant",
	"Star-sky":"Star-Sky",
	"Extra-Adorable Lavae":"Lavae",
	"Grass":"Grass Tuft",
	"Telelocator Socket":"Telelocator_Focus",
	"Sea Worther":"Sea worther",
	"Burnt":"",
	"Overheating":"",
	"Freezing":"",
	"Sandbag Held":"Sand Bag",
	"Crock Pot Empty": "Crock Pot",
	"Evergreen Burnt":       "Burnt Evergreen",
	"Lumpy Evergreen Burnt": "Burnt Evergreen",
	"Twiggy Tree": "Twiggy Tree Build",
	"Twiggy Tree Stump":     "Evergreen Stump",
	"Lumpy Evergreen Stump": "Evergreen Stump",
	"Wildbore Head Burnt": "Pig Head Burnt",
	"Merm Head Burnt":     "Pig Head Burnt",
	"Soaked Candle": "Wine Bottle Candle",
	"shadow body":"shadow body collection icon",
	"Juicy Berry Bush Dug": "Dug Juicy Berry Bush",
	"Red Mushroom Picked":   "Mushroom Picked",
	"Green Mushroom Picked": "Mushroom Picked",
	"Blue Mushroom Picked":  "Mushroom Picked",
	"Red Mushroom Sleeping":   "Mushroom Sleeping",
	"Green Mushroom Sleeping": "Mushroom Sleeping",
	"Blue Mushroom Sleeping":  "Mushroom Sleeping",
	"Bee Box No Honey": "Bee Box Level 0",
	"Bee Box Some Honey": "Bee Box Level 1",
	"Bee Box Full Of Honey": "Bee Box Level 3",
	"Piratihatitator Burnt": "Prestihatitator Burnt",
	"Abigail's Flower Stage 1": "Abigail Flower",
	"Abigail's Flower Stage 2": "Abigail Flower2",
	"Abigail's Flower Stage 3": "Abigail Flower2",
	"Abigail's Flower Ready, Held": "Abigail Flower Haunted",
	"Abigail's Flower Ready": "Abigail Flower Haunted",
	"Broken Clockworks": "Broken clockworks",
	"Bernie Broken": "DeadBernie",
	#"": "",
	#"": "",

}
def CreateSkinDict(Source, Character):
	Source = open(Source)
	Character = Character.lower()
	SkinDict = {}
	for line in Source.readlines():
		if line.startswith('msgctxt "STRINGS.SKIN_NAMES.body_' + Character + "_"):
			Entry = line[34+len(Character):-2]
			Name = Entry.replace("_"," ")
			Name = Name.title()+" Set"
			SkinDict[Entry] = Name

	return SkinDict
def CreateSkinQuoteDict(Source, Character):
	if Character == "WIGFRID":
		Character = "WATHGRITHR"
	SkinDict = CreateSkinDict(Source, Character)
	Character = Character.lower()
	Source = open(Source)
	SkinQuoteDict = {}
	Entry = None
	for line in Source.readlines():
		if Entry:
			PicName = Entry + " body"
			for Case in PicRename:
					if PicName == Case:
						PicName = PicRename[Case]
						if Debuging:
							print("Renamed =>", PicName)
						break
			PicName =  Character.title()+" "+PicName
			#if Debuging:
				#print("{}_{} PicName: {}".format(Character,Entry, PicName))
			try:
				SkinQuoteDict[SkinDict[Entry]] = [line[6:-1], PicName]
			except:
				pass
			Entry = None
		elif line.startswith('msgctxt "STRINGS.SKIN_QUOTES.{}'.format(Character)):
			Entry = line[30 + len(Character):-2]
	try:
		del SkinQuoteDict[""]
	except:
		pass
	return SkinQuoteDict

def CreateNameDict(Source):
	Source = open(Source)
	NameDict = {}
	Entry = None
	for line in Source.readlines():
		if Entry:
			Entry = Entry.replace("WORMHOLE_LIMITED_1","WORMHOLE_LIMITED")
			Entry = Entry.replace("SLOW_","")#farms have same quotes
			Entry = Entry.replace("FAST_","")
			NameDict[Entry] = line[7:-2]
			Entry = None

		elif line.startswith('msgctxt "STRINGS.NAMES.'):
			Entry = line[23:-2]
		elif line.startswith('msgctxt "STRINGS.DIRECTIONS.'):
			Entry = line[28:-2]
		elif line.startswith('msgctxt "STRINGS.SKIN_NAMES.body_'):
			Entry = line[28:-2]
	#print(NameDict)
	return NameDict

def CreateQuoteDict(Source, Character):
	if Character == "WILSON": #no Quotes are actually labelled for Wilson, he is the generic.
		Character = "GENERIC"
	elif Character == "WIGFRID":
		Character = "WATHGRITHR"
	NameDict = CreateNameDict(Source)
	#if Debuging:
		#print(NameDict)
	Character = Character.upper()
	Source = open(Source)
	QuoteDict = {}
	Entry = None
	for line in Source.readlines():
		if Entry:
			if Debuging:
				print ("––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––"), print(), print("Entry:",Entry)
			Prefix = ""
			Affix = ""
			PBase = ""
			ABase = ""
			Dug = False
			PicName = ""

	#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

			if Entry[len(Entry)-7:] == "GENERIC":
				Entry = Entry[:-8]
				if Debuging:
					print("Entry =>", Entry)

			if Entry.startswith("DESCRIBE"):
				Entry = Entry[9:]
				if Debuging:
					print("Entry =>", Entry)

			elif Entry.startswith("ANNOUNCE"):
				Entry = Entry[9:]
				if Debuging:
					print("Entry =>", Entry)
			if Entry.startswith("DUG"):
				Entry = Entry[4:]
				Affix = "held"
				Dug = True#-----distinction from non-Dug verison
				if Debuging:
					print("Entry =>", Entry)

	#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

			for Case in SpecialAffixes:
				if Entry[len(Entry) - (len(Case) + 1):] == "." + Case or Entry[len(Entry) - (len(Case) + 1):] == "_" + Case:
					Entry = Entry[:len(Entry) - len(Case)-1]
					Affix += SpecialAffixes[Case]
					ABase = Affix.title()
					if Debuging:
						print("Affix: {} => {}".format(Case,SpecialAffixes[Case]))
						print("ABase:", ABase)
						print("Entry =>", Entry)
					break

			for Case in SpecialPrefixes:
				if (Entry[len(Entry) - (len(Case) + 1):] == "." + Case or Entry[len(Entry) - (len(Case) + 1):] == "_" + Case) and Entry[:len(Entry) - len(Case)-1] in NameDict:#----------if ends with preffix and without preffix has IGN
					Entry = Entry[:len(Entry) - (len(Case) + 1)]
					Prefix = Prefix + SpecialPrefixes[Case]
					PBase = Prefix.title()
					if Debuging:
						print("Prefix: {} => {}".format(Case,SpecialPrefixes[Case]))
						print("PBase:", PBase)
						print("Entry =>", Entry)
					break

	#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

			if Entry not in NameDict:
				if Entry.find(".") != -1:#-----NORMAL AFFIX GENERATOR
					i = Entry.find(".")
					Affix += Entry[i+1:]#-----also removes "."
					if Affix not in SpecialAffixes:
						try:
							Affix = NameDict[Affix]
						except KeyError:
							Affix = Affix.lower()
					Entry = Entry[:i]

			if Affix:
				if not ABase:
					ABase = Affix.title()
				Affix = " ({})".format(Affix)
				if Debuging:
					print("Affix:", Affix)
					print("ABase:", ABase)
					print("Entry =>", Entry)

	#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

	#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

			if Entry in NameDict:
				#print(" NameDict[Entry]:",  NameDict[Entry])
				if NameDict[Entry] in SpecialNounChange:
					if Debuging:
						print("SNC-Entry: {} => {}".format(NameDict[Entry], SpecialNounChange[NameDict[Entry]]))
					NameDict[Entry] = SpecialNounChange[NameDict[Entry]]

				if Debuging:
						print("######In Dictionary######")
						print("IGN:", NameDict[Entry])
	#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
				if Entry != "COMPASS":
					if ABase:
						if Dug:
							ABase = "Dug"
						elif ABase == "Barren":
							ABase = "Withered"
						PicName = NameDict[Entry] + " " + ABase
						if Debuging:
							print("PicName:", PicName)
						if PicName in PicRename:
							PicName = PicRename[PicName]
							if Debuging:
								print("PicName {} => {}".format(NameDict[Entry] + " " + ABase, PicName))
						elif ABase in PicAffixes:
							pass
						else:
							if NameDict[Entry] in PicRename:
								PicName = PicRename[NameDict[Entry]]
								if Debuging:
									print("PicName:", PicName)
							else:
								PicName = NameDict[Entry]
							if Debuging:
								print("PicName:", PicName)
					elif PBase:
						PicName = PBase + NameDict[Entry]
						if Debuging:
							print("PicName:", PicName)
						if PicName in PicRename:
							PicName = PicRename[PicName]
							if Debuging:
								print("PicName {} => {}".format(PBase + NameDict[Entry], PicName))
					else:
						PicName = NameDict[Entry]
						if Debuging:
							print("PicName:", PicName)
						if PicName in PicRename:
							PicName = PicRename[PicName]
							if Debuging:
								print("PicName {} => {}".format(NameDict[Entry], PicName))
	#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
					QuoteDict[Prefix + NameDict[Entry]+ Affix] = [line[6:-1], PicName]
					if Debuging:
						print("'Pair': [{}|{}|{}]".format(Prefix + NameDict[Entry] + Affix, QuoteDict[Prefix + NameDict[Entry] + Affix][0], QuoteDict[Prefix + NameDict[Entry] + Affix][1]))
						print()
			else:
				if Debuging:
					print("====Not in Dictionary====")
				Entry = Entry.replace("_"," ")
				Entry = Entry.replace("."," ")
				Entry = Entry.replace("\"","'")
				Entry = Entry.title()
				if Debuging:
					print("Cleaned Action:", Prefix + Entry + Affix)
				QuoteDict[Prefix + Entry + Affix] = [line[6:-1], PicName]
				if Debuging:
					print("Pair: [{}|{}]".format(Prefix + Entry + Affix, QuoteDict[Prefix + Entry + Affix][0]))
					print()
			Entry = None
	#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
		elif line.startswith('msgctxt "STRINGS.CHARACTERS.{}'.format(Character)):#----------sets up for next cycle; quote is on next line not this one
			Entry = line[29 + len(Character):-2]
	#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
	try:
		del QuoteDict[""]
	except:
		pass
	#print(QuoteDict)
	if Debuging:
		print("************************************************************"), print()
	return QuoteDict
def CreateIcon(PicName):
	return "{}{}{}".format("{{Pic32|", PicName, "}}")
def EnterQuotes(Source, Character, InputFile, OutputFile):

	print("============================================================")
	print("Using Source: {}".format(Source))
	Character = Character.upper()
	if Character == "MAXWELL":
		Character = "WAXWELL"
	QuoteDict = dict(list(CreateQuoteDict(Source, Character).items()) + list(CreateSkinQuoteDict(Source, Character).items()))
	Used = []
	Input = open(InputFile)
	Output = open(OutputFile, "w")
	lIndex = 0
	#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
	if not Debuging:
		print("Creating file", end="",flush=True)
	for line in Input.readlines():
		lIndex += 1
		line = line.replace(" "," ")
		for Case in Cleaning:
			line = line.replace(Case+"-", Cleaning[Case]+"-")
		#print(">>>",line)
		for key in QuoteDict:
			if line.find("*"+key+"-") != -1 or line.find("}"+key+"-") != -1 or line.find("} "+key+"-") != -1 or line.find("]"+key+"-") != -1 or line.find("] "+key+"-") != -1:
				Icon = ""
				temp = QuoteDict[key][0]
				if Character == "WATHGRITHR" or Character == "WIGFRID":
					temp = temp.replace("o", "ö")
					temp = temp.replace("O", "Ö")
				Quote = temp

				PicName = QuoteDict[key][1]
				if PicName:
					Icon = CreateIcon(PicName)
						#print ("Name: {}|Quote: {}|PicName: {}".format(key, Quote, QuoteDict[key][1]))

		#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
				line = line.replace("*{}-".format(key),"*{}{}-".format(Icon, key))
				if Overwrite:
					if line.find("*{}-".format(key)) != -1 or line.find("] {}-".format(key)) != -1 or line.find("]{}-".format(key)) != -1 or line.find("{} {}-".format("}", key)) != -1 or line.find("{}{}-".format("}", key)) != -1:

						line = "*{}{}- {}\n".format(Icon, key, Quote)
						if Debuging:
								print("––––––––––")
								print()
								print("{}:".format(key))
								print(line)
						break

				elif line.find("] {}-\n".format(key)) != -1 or line.find("]{}-\n".format(key)) != -1 or line.find("] {}- \n".format(key)) != -1 or line.find("]{}- \n".format(key)) != -1 or \
				line.find("{} {}-\n".format("}", key)) != -1 or line.find("{}{}-\n".format("}", key)) != -1 or line.find("{} {}- \n".format("}", key)) != -1 or line.find("{}{}- \n".format("}", key)) != -1 or \
				line.find("*{}-\n".format(key)) != -1 or line.find("*{}- \n".format(key)) != -1:#cant use line.find("{}- \n".format(key)) b/c 'Endothermic Fire' would also trigger 'Fire' etc.

					line = "*{}{}- {}\n".format(Icon, key, Quote)
					if Debuging:
								print("––––––––––")
								print()
								print("{}:".format(key))
								print(line)
					break

				elif PicOverwrite:
					if PicName and Icon and line.find("*{}{}-".format(Icon, key)) == -1:
						IconStart = line.find("{{Pic32|")
						IconEnd = line[IconStart:].find("}}")
						if IconStart != -1 and IconEnd != -1:
							CurrentIcon = line[IconStart:IconEnd+IconStart+2]
							CurrentPic = line[IconStart+8:IconEnd+IconStart]
							if Debuging:
								print("––––––––––")
								print()
								print("{}:".format(key))
								print(line)
								print("Icon:", Icon)
								print("CurrentIcon:", line[IconStart:IconEnd+IconStart+2])
							if CurrentPic in QuoteDict or CurrentPic in PicRename.values():
									line = line.replace(CurrentIcon, Icon)
									if Debuging:
										print("Replaced '{}' pic with '{}'".format(CurrentPic, PicName)),print()
										print(line)
									break
							else:
								if Debuging:
									print("{} not found".format(CurrentPic))
									print("Keeping CurrentIcon"),print()

		#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

		Output.write(line)

		if not Debuging and lIndex%100 == 0:#----------the progress "bar"
			print('.', end="",flush=True)
	else:
		if Debuging:
			Unused = open("unused_" + Character.lower() + ".txt", "a")
			Unused.write("––––––––––––––––––––––––––––––––––––––––\n")
			UnusedR = open("unused_" + Character.lower() + ".txt", "r")

			for key in QuoteDict:
				if Input.read().find("*{}- {}".format(key, QuoteDict[key])) == -1:
					if UnusedR.read().find("*{}- {}\n".format(key, QuoteDict[key])) == -1:
						Unused.write("*{}- {}\n".format(key, QuoteDict[key]))
		if not Debuging:
			print("Complete")
		print('Exported to "{}"'.format(OutputFile))

Character = input("Character (file should also be named Character.txt):")
Start = floor(time())
#EnterQuotes("strings.pot",input("Character DebugSpawn: "), input("Import file (e.x. file.txt) :"), input("Export file (e.x. file.txt) :"))
EnterQuotes("stringsDST.pot",Character, Character + ".txt", Character + "temp.txt")
EnterQuotes("strings.pot",Character, Character + "temp.txt", Character + "_Export.txt")
print("============================================================")
Elapsed = [floor((floor(time())-Start)/60), (floor(time())-Start)%60]
if Debuging:
	print("Completed in {} seconds".format(Elapsed[1]))
print("Done!")


import os, sys, json
########### In Built  ##############

import config
import db.get_conn as get_conn
conn_obj    = get_conn.DB()
############  custom imports   #############

class TemplateConfig:

    def insert_output_config(self, ijson):
        company_id = ijson['company_id']
        project_id  = ijson['project_id']
        template_id = ijson['template_id']
        parsed_data = ijson['data']
        tabs        = parsed_data.get('tabs', {})
        map_d   = {
                    '1' : 'PH',
                    '2' : 'G',
                    }
        conf_d  = {}
        for c in tabs.get('Col Conf', []):
            conf_d[c]   = 1

        config_path = config.Config.project_config.format(project_id)
        if not os.path.exists(config_path):
            os.system('mkdir -p {0}'.format(config_path))
        config_db = os.path.join(config_path, 'config.db')
        conn, cur   = conn_obj.sqlite_connection(config_db)

        crt_stmt = """ CREATE TABLE IF NOT EXISTS output_configuration(row_id INTEGER PRIMARY KEY AUTOINCREMENT, template_id TEXT, export_config TEXT, file_name TEXT, zip_file_name TEXT); """
        cur.execute(crt_stmt)

        read_qry = """ SELECT template_id FROM output_configuration; """
        cur.execute(read_qry)
        t_data = cur.fetchall()
        check_data = {str(row_data[0]) for row_data in t_data}
        parsed_data['e_c']['col_config']    = conf_d
        export_config, file_name, zip_file_name = map(json.dumps, [parsed_data['e_c'], parsed_data['f_c'], parsed_data['zf_c']])

        if str(template_id) in check_data:
            update_tup = [(export_config, file_name, zip_file_name, template_id)]
            update_stmt = """ UPDATE output_configuration SET export_config=?, file_name=?, zip_file_name=? WHERE template_id=?; """
            cur.executemany(update_stmt, update_tup)
            conn.commit()

        elif template_id not in check_data:
            insert_rows = []
            insert_tup = (template_id, export_config, file_name, zip_file_name)
            insert_rows.append(insert_tup)
            if insert_rows:
                insert_stmt = """ INSERT INTO output_configuration(template_id, export_config, file_name, zip_file_name) VALUES(?, ?, ?, ?); """
                cur.executemany(insert_stmt, insert_rows)
                conn.commit()
        conn.close()
        res = [{'message':'done'}]
        return res

    def read_template_info(self, ijson):
        company_id = ijson['company_id']
        project_id  = ijson['project_id']
        template_id = ijson['template_id']

        config_path = config.Config.project_config.format(project_id)
        if not os.path.exists(config_path):
            os.system('mkdir -p {0}'.format(config_path))
        config_db = os.path.join(config_path, 'config.db')
        conn, cur   = conn_obj.sqlite_connection(config_db)
        read_qry = """ SELECT export_config, file_name, zip_file_name FROM output_configuration WHERE template_id='{0}'; """.format(template_id)
        cur.execute(read_qry)
        t_data  = cur.fetchone()
        conn.close()

        data = {}
        if t_data:
            export_config, file_name, zip_file_name = map(json.loads, t_data)
            data = {'e_c':export_config, 'f_c':file_name, 'zf_c':zip_file_name}
        res = [{'message':'done', 'data':data}]
        return res

if __name__ == '__main__':
    tc_Obj = TemplateConfig()


import argparse
import os

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

from datasetinsights.visualization.object_detection import (
    render_object_detection_layout,
)
from datasetinsights.visualization.overview import overview

this_dir = os.path.dirname(os.path.abspath(__file__))
css_file = os.path.join(this_dir, "stylesheet.css")

app = dash.Dash(__name__, external_stylesheets=[css_file],)
app.config.suppress_callback_exceptions = True

def main_layout(data_root):
    """ Method for generating main app layout.

    Args:
        data_root(str): path to the dataset.

    Returns:
        html layout: main layout design with tabs for overview statistics
            and object detection.
    """
    app_layout = html.Div(
        [
            html.H1(
                children="Dataset Insights",
                style={
                    "textAlign": "center",
                    "padding": 20,
                    "background": "lightgrey",
                },
            ),
            html.Div(
                [
                    dcc.Tabs(
                        id="page_tabs",
                        value="dataset_overview",
                        children=[
                            dcc.Tab(
                                label="Overview", value="dataset_overview",
                            ),
                            dcc.Tab(
                                label="Object Detection",
                                value="object_detection",
                            ),
                        ],
                    ),
                    html.Div(id="main_page_tabs"),
                ]
            ),
        ]
    )
    return app_layout

@app.callback(
    Output("main_page_tabs", "children"), [Input("page_tabs", "value")]
)
def render_content(tab):
    if tab == "dataset_overview":
        return overview(data_root)
    elif tab == "object_detection":
        return render_object_detection_layout(data_root)

def check_path(path):
    if os.path.isdir(path):
        return path
    else:
        raise ValueError(f"Path {path} not found")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--data-root", help="Path to the data root")
    args = parser.parse_args()
    data_root = check_path(args.data_root)
    app.layout = main_layout(data_root)
    app.run_server(debug=True)


from flask import Flask, render_template, request, redirect, Blueprint, url_for
from flask_login import current_user
from werkzeug.utils import secure_filename
from app import app
from helpers import *
from models.base_model import BaseModel
from models.user import User
from models.image import Image
import datetime

images_blueprint = Blueprint('images',
                            __name__,
                            template_folder='templates/images')

ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

# to create new post/upload images
def create_gallery_image(output):
    gallery_pic = Image(user_id=current_user.id, image_name = output)
    gallery_pic.save()

# to update profile image
def update_profile_pic(output):
    pic = User.update(profile_image_name = output).where(User.id == current_user.id)
    pic.execute()

# to send the file from the user’s computer directly to the bucket
@images_blueprint.route("/<id>", methods=["POST"])
def upload_file(id):
	# A
    if "user_file" not in request.files and "image_file" not in request.files:
        return "No file in request.files"

	# B If the key is in the object, we save it in a variable called file.
    if "user_file" in request.files:
        file = request.files["user_file"]
    if "image_file" in request.files:
        file = request.files["image_file"]
    """
        These attributes are also available
        file.filename               # The actual name of the file
        file.content_type
        file.content_length
        file.mimetype
    """
	# C. We check the filename attribute on the object and if it’s empty,
    # it means the user sumbmitted an empty form, so we return an error message.
    if file.filename == "":
        return "Please select a file"

	# D. we check that there is a file and that it has an allowed filetype
    if file and allowed_file(file.filename):
        file.filename = secure_filename(file.filename)
        output = upload_file_to_s3(file, app.config['S3_BUCKET'])
        if "user_file" in request.files:
            update_profile_pic(output)
            return redirect(url_for("users.edit", id=id))
        if "image_file" in request.files:
            create_gallery_image(output)
            return redirect(url_for("users.show", username=current_user.username))
    else:
        return redirect("/<id>")

# user.profile_image_name # stored in database but returns just the path without domain

# function to upload a file to S3
def upload_file_to_s3(file, bucket_name, acl="public-read"):

    """
    Docs: http://boto3.readthedocs.io/en/latest/guide/s3.html
    """
    u_name = secure_filename(str(current_user.id) + file.filename + str(datetime.datetime.now()))

    try:

        s3.upload_fileobj(
            file,
            bucket_name,
            u_name,
            ExtraArgs={
                "ACL": acl,
                "ContentType": file.content_type
            }
        )

    except Exception as e:
        # This is a catch all exception, edit this part to fit your needs.
        print("Something Happened: ", e)
        return e
    return u_name


#!/usr/bin/python3
import os
import time
import sys
sys.path.append('../..')
import shutil
import pickle
import numpy as np
import random
import tensorflow as tf
import matplotlib.pyplot as plt
from source.utils.utils import pad_sequences
from source.utils.utils import sparse_tuple_from
from source.utils.utils import EditDistance

class ctc_model():
    def __init__(self, save_dir='./model', run_name='default'):
        super(ctc_model, self).__init__()
        self.model_dir = save_dir
        self.save_dir = save_dir + '/' + run_name
        if not os.path.exists(save_dir): os.makedirs(save_dir)
        if not os.path.exists(self.save_dir): os.makedirs(self.save_dir)
        self.graphs_dir = save_dir + '/graphs' + '/' + run_name
        self.conv_kernels = [10]
        self.conv_strides = [10]
        self.num_filters = 10
        self.pooling_size = 1
        self.cnn_hidden_nums = 256

    def setup(self, lexicon, num_features, use_cnn=False, num_hidden=128, num_layers=1, mlp_hiddens=[512], learning_rate=1e-2, max_len=400):
        self.lexicon = lexicon
        self.dictionary = []
        self.delimiter = '|'
        self.num_features = num_features
        self.max_len = max_len
        self.mlp_hiddens = mlp_hiddens
        self.learning_rate = learning_rate
        self.setup_model(use_cnn=use_cnn, num_hidden=num_hidden, num_layers=num_layers)

    def setup_model(self, use_cnn=False, num_hidden=128, num_layers=1):
        self.num_hidden = num_hidden
        self.num_layers = num_layers
        self.num_phns = len(self.lexicon)
        self.num_classes = self.num_phns+1 # +1 for blank states in CTC
        self.keep_prob = tf.Variable(1.0, trainable = False)

        self.sess = tf.Session()
        self.model_input = tf.placeholder(tf.float32, [None, self.max_len, self.num_features])
        self.model_targets = tf.sparse_placeholder(tf.int32)
        self.model_seqlen = tf.placeholder(tf.int32, [None])

        if use_cnn:
            self.model_matricized_x = tf.reshape(self.model_input, [-1, self.num_features])
            self.model_matricized_unary_scores, dim = self.cnn_model(self.model_matricized_x , self.num_features, self.num_phns)
            self.model_unary_scores = tf.reshape(self.model_matricized_unary_scores,[-1, self.max_len, dim])
            inputs = self.model_unary_scores
        else:
            inputs = self.model_input

        # cell = tf.nn.rnn_cell.LSTMCell(self.num_hidden, state_is_tuple=True)
        # cell = tf.nn.rnn_cell.DropoutWrapper(cell=cell, output_keep_prob=0.8)
        # rnn_layers = [tf.nn.rnn_cell.BasicRNNCell(self.num_hidden) for _ in range(self.num_layers)]
        rnn_layers = [tf.contrib.rnn.GRUCell(self.num_hidden) for _ in range(self.num_layers)]
        stack = tf.nn.rnn_cell.MultiRNNCell(rnn_layers, state_is_tuple=True)
        outputs, _ = tf.nn.dynamic_rnn(stack, inputs, self.model_seqlen, dtype=tf.float32)
        # outputs, _ = tf.nn.bidirectional_dynamic_rnn(tf.contrib.rnn.GRUCell(self.num_hidden), tf.contrib.rnn.GRUCell(self.num_hidden), inputs, self.model_seqlen, dtype=tf.float32)
        # outputs = tf.concat(outputs, 2)

        # rnn_layers2 = [tf.nn.rnn_cell.LSTMCell(self.num_hidden, state_is_tuple=True) for _ in range(self.num_layers)]
        # stack2 = tf.nn.rnn_cell.MultiRNNCell(rnn_layers2, state_is_tuple=True)
        # outputs2, _ = tf.nn.dynamic_rnn(stack2, outputs, self.model_seqlen, dtype=tf.float32)

        shape = tf.shape(inputs)
        batch_s, max_timesteps = shape[0], shape[1]
        # outputs = tf.concat([outputs,outputs2], 2)
        outputs = tf.reshape(outputs, [-1, self.num_hidden])

        # W = tf.Variable(tf.truncated_normal([self.num_hidden, self.num_classes], stddev=0.1))
        # b = tf.Variable(tf.constant(0., shape=[self.num_classes]))
        # logits = tf.matmul(outputs, W) + b
        logits, _ = self.mlp_model(outputs , self.num_hidden, self.num_classes, self.mlp_hiddens)
        logits = tf.reshape(logits, [batch_s, self.max_len, self.num_classes])
        self.logits = logits
        self.ctc_probs = tf.reshape(tf.nn.softmax(logits), [batch_s, self.max_len, self.num_classes])
        logits = tf.transpose(logits, (1, 0, 2))
        loss = tf.nn.ctc_loss(self.model_targets, logits, self.model_seqlen)
        self.cost = tf.reduce_mean(loss)

        # optimizer = tf.train.MomentumOptimizer(1e-02, 0.9).minimize(cost)
        self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.cost)
        self.decoded, log_prob = tf.nn.ctc_greedy_decoder(logits, self.model_seqlen)
        # self.decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits,sequence_length=self.model_seqlen,beam_width=3,merge_repeated=False, top_paths=1)
        self.ler = tf.reduce_mean(tf.edit_distance(tf.cast(self.decoded[0], tf.int32), self.model_targets))

        self.saver = tf.train.Saver(max_to_keep=None)
        tf.summary.scalar('ler',self.ler)
        tf.summary.scalar('cost',self.cost)
        self.summeries = tf.summary.merge_all()
        self.writer = tf.summary.FileWriter(self.graphs_dir)
        self.num_of_params = np.sum([np.prod(v.get_shape().as_list()) for v in tf.trainable_variables()])
        print('num_of_params',self.num_of_params)

    def define_paths(self, inputs_train_path, outputs_train_path, inputs_test_path, outputs_test_path):
        self.inputs_train_path = inputs_train_path
        self.outputs_train_path = outputs_train_path
        self.inputs_test_path = inputs_test_path
        self.outputs_test_path = outputs_test_path

    def batch_append(self, batch):
        txt = '_' + str(batch)
        return txt

    def restore_model(self, epoch=-1):
        epoch_here = epoch
        if os.path.isdir(self.save_dir):
            if epoch_here == -1:
                with open(self.save_dir+'/last_epoch', "rb") as fp:   # Unpickling
                    epoch_here = pickle.load(fp)
            self.saver.restore(self.sess, self.model_path_for(epoch))
        else:
            print("path", self.save_dir, "not found!")

    def train_model(self, num_epochs=20, num_batches=10, num_batches_test=1, keep_prob=1, data_usage=1, valid_proportion=0.1, valid_patience=5, continue_model=False):

        last_epoch = 0
        if continue_model:
            with open(self.save_dir+'/last_epoch', "rb") as fp:   # Unpickling
                last_epoch = pickle.load(fp) + 1
            self.restore_model(epoch=last_epoch-1)
        else:
            self.sess.run(tf.global_variables_initializer())
            shutil.rmtree(self.graphs_dir); self.writer = tf.summary.FileWriter(self.graphs_dir)

        for curr_epoch in range(last_epoch, num_epochs):
            train_cost = train_ler = 0
            all_batches_lengths = 0
            batches = list(range(0,num_batches))
            random.shuffle(batches)
            for batch in batches[1:]:
                sys.stdout.write("\repoch %d " % int(curr_epoch+1))
                sys.stdout.write("%d%%" % int(100*batches.index(batch)/num_batches))
                with open(self.inputs_train_path + self.batch_append(batch), "rb") as fp:   # Unpickling
                    batch_train_inputs = pickle.load(fp)
                with open(self.outputs_train_path + self.batch_append(batch), "rb") as fp:   # Unpickling
                    batch_train_targets = sparse_tuple_from(pickle.load(fp))
                # batch_train_inputs = self.add_noise_to_batch(batch_train_inputs)
                batch_train_inputs, batch_train_seq_len = pad_sequences(batch_train_inputs, maxlen=self.max_len)
                feed = {self.model_input: batch_train_inputs, self.model_targets: batch_train_targets, self.model_seqlen: batch_train_seq_len, self.keep_prob: keep_prob}
                batch_cost, _ = self.sess.run([self.cost, self.optimizer], feed)
                train_cost += batch_cost*len(batch_train_seq_len)
                train_ler += self.sess.run(self.ler, feed_dict=feed)*len(batch_train_seq_len)
                all_batches_lengths += len(batch_train_seq_len)
            train_cost /= all_batches_lengths
            train_ler /= all_batches_lengths
            test_ler = self.test_ler(num_batches_test)
            log = "Epoch {}/{}, train_cost = {:.3f}, train_ler = {:.3f}, test_ler = {:.3f}"
            sys.stdout.write("\r")
            print(log.format(curr_epoch+1, num_epochs, train_cost, train_ler, test_ler))
            self.write_summary_for(curr_epoch, self.inputs_train_path + self.batch_append(batches[0]), self.outputs_train_path + self.batch_append(batches[0]))
            self.save_model(curr_epoch)

    def add_noise_to_batch(self, batch):
        result = batch
        for i,utter in enumerate(batch):
            for j,feat in enumerate(utter):
                noise = np.random.normal(loc=0,scale=0.075,size=len(feat))
                result[i][j] = result[i][j]+noise
        return result

    def save_model(self, epoch, keep_prob=1):
        self.saver.save(self.sess, self.model_path_for(epoch))
        with open(self.save_dir+'/last_epoch', "wb") as fp:   #Pickling
            pickle.dump(epoch, fp)

    def model_path_for(self, epoch):
        models_path = self.save_dir+'/model'+'_'+str(epoch)
        if not os.path.exists(models_path): os.makedirs(models_path)
        model_path = models_path +'/'+'model'+'.ckpt'
        return model_path

    def write_summary_for(self, epoch, inputs_path, outputs_path):
        with open(inputs_path, "rb") as fp:   # Unpickling
            inputs = pickle.load(fp)
        with open(outputs_path, "rb") as fp:   # Unpickling
            outputs = pickle.load(fp)
        batch_test_inputs, batch_test_seq_len = pad_sequences(inputs, maxlen=self.max_len)
        batch_test_targets = sparse_tuple_from(outputs)
        feed = {self.model_input: batch_test_inputs, self.model_targets: batch_test_targets, self.model_seqlen: batch_test_seq_len, self.keep_prob: 1}
        _, summ = self.sess.run([self.ler,self.summeries], feed_dict=feed)
        self.writer.add_summary(summ, global_step = epoch+1)
        _, summ = self.sess.run([self.cost,self.summeries], feed_dict=feed)
        self.writer.add_summary(summ, global_step = epoch+1)

    def test_ler(self, num_batches):
        all_batches_lengths = 0
        test_ler = 0
        for batch in range(num_batches):
            sys.stdout.write("\rtesting progress %d%%" % int(100*batch/num_batches))
            with open(self.inputs_test_path + self.batch_append(batch), "rb") as fp:   # Unpickling
                batch_test_inputs = pickle.load(fp)
            with open(self.outputs_test_path + self.batch_append(batch), "rb") as fp:   # Unpickling
                batch_test_outputs = pickle.load(fp)
            batch_len = len(batch_test_outputs)
            all_batches_lengths += batch_len
            test_ler += self.test_model(batch_test_inputs, batch_test_outputs)*batch_len
        sys.stdout.write("\r")
        test_ler /= all_batches_lengths
        return test_ler

    def test_model(self, inputs, outputs, keep_prob=1):
        batch_test_inputs, batch_test_seq_len = pad_sequences(inputs, maxlen=self.max_len)
        batch_test_targets = sparse_tuple_from(outputs)
        feed = {self.model_input: batch_test_inputs, self.model_targets: batch_test_targets, self.model_seqlen: batch_test_seq_len, self.keep_prob: keep_prob}
        d = self.sess.run(self.decoded[0], feed_dict=feed)
        dense_decoded = tf.sparse_tensor_to_dense(d, default_value=-1).eval(session=self.sess)
        error = self.sess.run(self.ler, feed_dict=feed)
        return error

    def print_outputs(self, inputs, outputs, keep_prob=1):
        batch_test_inputs, batch_test_seq_len = pad_sequences(inputs, maxlen=self.max_len)
        batch_test_targets = sparse_tuple_from(outputs)
        feed = {self.model_input: batch_test_inputs, self.model_targets: batch_test_targets, self.model_seqlen: batch_test_seq_len, self.keep_prob: keep_prob}
        d = self.sess.run(self.decoded[0], feed_dict=feed)
        dense_decoded = tf.sparse_tensor_to_dense(d, default_value=-1).eval(session=self.sess)
        all_originals = []
        all_decodeds = []
        for i, seq in enumerate(dense_decoded):
            seq = [s for s in seq if s != -1]
            all_originals.append(self.number_to_lex(outputs[i]))
            all_decodeds.append(self.number_to_lex(seq))
        return [self.no_delimiters(array) for array in all_originals], [self.no_delimiters(array) for array in all_decodeds]

    def no_delimiters(self, array):
        result = []
        for a in array:
            if a != self.delimiter:
                result.append(a)
        return result

    def number_to_lex(self, sequence):
        lex_seq = []
        for i in range(len(sequence)):
            if sequence[i] < len(self.lexicon):
                lex_seq.append(self.lexicon[sequence[i]])
            else:
                continue
                # lex_seq.append(' ')
        # lex_seq = [self.lexicon[sequence[i]] for i in range(len(sequence))]
        return lex_seq

    def mlp_model(self, theInput, num_features, num_tags, hidden_layers):
        print('theInput',theInput.get_shape().as_list())
        Weight = self.weight_variable([num_features, hidden_layers[0]])
        Bias = self.bias_variable([hidden_layers[0]])
        y_out = tf.nn.relu(tf.matmul(theInput, Weight) + Bias)
        y_out = tf.nn.dropout(y_out, keep_prob=self.keep_prob)
        for i in range(len(hidden_layers)-1):
            Weight = self.weight_variable([hidden_layers[i], hidden_layers[i+1]])
            Bias = self.bias_variable([hidden_layers[i+1]])
            y_out = tf.nn.relu(tf.matmul(y_out, Weight) + Bias)
            y_out = tf.nn.dropout(y_out, keep_prob=self.keep_prob)
        Weight = self.weight_variable([hidden_layers[len(hidden_layers)-1], num_tags])
        Bias = self.bias_variable([num_tags])
        y_out = tf.matmul(y_out, Weight) + Bias
        # y_out = tf.nn.softmax(y_out)
        dim = np.prod(y_out.get_shape().as_list()[1:])
        print('y_out',y_out.get_shape().as_list())
        return y_out, dim

    def weight_variable(self, shape):
        initial = tf.random_normal(shape,mean=0.0,stddev=0.1)
        return tf.Variable(initial)

    def bias_variable(self, shape):
        initial = tf.random_normal(shape,mean=0.1,stddev=0.1)
        return tf.Variable(initial)

    def cnn_model(self, theInput, num_features, num_tags):
        conv_kernels = self.conv_kernels
        conv_strides = self.conv_strides
        num_filters = self.num_filters
        pooling_size = self.pooling_size

        x_reshaped = tf.reshape(theInput, [-1, num_features, 1, 1])
        print(x_reshaped.get_shape().as_list())

        conv = tf.layers.conv2d(inputs=x_reshaped,
                                 filters=num_filters,
                                 kernel_size=[conv_kernels[0], 1],
                                 name='conv0',
                                 strides=[conv_strides[0], 1],
                                 activation=tf.nn.relu,
                                 kernel_initializer=tf.random_normal_initializer(0,0.1),
                                 padding='valid')

        pool = tf.layers.max_pooling2d(inputs=conv,
                                 pool_size=[pooling_size, 1],
                                 strides=[pooling_size, 1],
                                 padding='valid')
        pool = tf.nn.relu(pool)
        print(pool.get_shape().as_list())

        for i in range(1,len(conv_kernels)):
            conv = tf.layers.conv2d(inputs=pool,
                                     filters=num_filters,
                                     kernel_size=[conv_kernels[i], 1],
                                     strides=[conv_strides[i], 1],
                                     activation=tf.nn.relu,
                                     kernel_initializer=tf.random_normal_initializer(0,0.1),
                                     padding='valid')

            pool = tf.layers.max_pooling2d(inputs=conv,
                                     pool_size=[pooling_size, 1],
                                     strides=[pooling_size, 1],
                                     padding='valid')
            pool = tf.nn.relu(pool)
            print(pool.get_shape().as_list())

        dim = np.prod(pool.get_shape().as_list()[1:])
        pool_flat = tf.reshape(pool, [-1, dim])
        # return pool_flat, dim

        size = self.cnn_hidden_nums
        Weight = self.weight_variable([dim, size])
        Bias = self.bias_variable([size])
        y_out = (tf.matmul(pool_flat, Weight) + Bias)
        y_out = tf.nn.dropout(y_out, keep_prob=self.keep_prob)

        return y_out, size


from Framework.infrastructure.builder import check_building_page_title
from Framework.utility.Constants import BuildingType, TroopType, get_TROOPS, get_XPATH, get_projectLogger
from Framework.utility.SeleniumWebScraper import SWS

logger = get_projectLogger()
TROOPS = get_TROOPS()
XPATH = get_XPATH()

def train_hero(sws : SWS, tpType : TroopType):
    """
    Trains a hero.

    Parameters:
        - sws (SWS): Selenium Web Scraper.
        - tpType (TroopType): Denotes troop.

    Returns:
        - True if the operation is successful, False otherwise.
    """
    status = False
    if check_building_page_title(sws, BuildingType.HeroMansion):
        if not sws.isVisible(XPATH.HERO_EXISTING):
            if sws.clickElement(XPATH.HERO_TRAIN_BTN % TROOPS[tpType].name, refresh=True):
                logger.success(f'In function train_hero: The {TROOPS[tpType].name} hero was trained.')
                status = True
            else:
                logger.error('In function train_hero: Failed to press train')
        else:
            logger.warning("In function train_hero: Can't train another hero as there is already one existing.")
    else:
        logger.error("In function train_hero: Not Hero's Mansion screen")
    return status

def name_hero(sws : SWS, newName : str):
    """
    Gives a name to the hero. (should be called after train_hero)

    Parameters:
        - sws (SWS): Selenium Web Scraper.
        - newName (str): Denotes hero name.

    Returns:
        - True if the operation is successful, False otherwise.
    """
    status = False
    if sws.sendKeys(XPATH.HERO_NAME, None, waitFor=True):
        if sws.sendKeys(XPATH.HERO_NAME, newName):
            logger.success(f'In name_hero: The hero is named now "{newName}".')
            if sws.clickElement(XPATH.HERO_SAVE_NAME):
                status = True
                logger.success('In name_hero: The hero name is now saved.')
            else:
                logger.error('In name_hero: Couldn\'t push save name button.')
        else:
            logger.error('In name_hero: Couldn\'t send the new hero name in the textbox.')
    else:
        logger.error('In name_hero: Couldn\'t send the new hero name in the textbox.')
    return status


import _io
from django import forms
from file_resubmit.admin import AdminResubmitImageWidget
from PIL import Image
from note.models import Note
from smedialink.settings import MEDIA_ROOT

class NoteCreationForm(forms.ModelForm):
    class Meta:
        model=Note
        fields=('text','image',)

    image = forms.ImageField(
        required=False,
        label='Изображение',
        widget=AdminResubmitImageWidget(
            attrs={'class': 'form-control', 'data-validation': 'mime size', 'data-validation-allowing': 'jpg, png, gif',
                   'data-validation-max-size': '20Mb',
                   'data-validation-error-msg-size': 'Вы не можете загружать изображения больше 20Мб',
                   'data-validation-error-msg-mime': 'Вы можете загружать только изображения.'}
        )
    )

    text = forms.CharField(
        label="Текст",
        required=False,
        widget=forms.Textarea(
            attrs={'class': 'form-control', 'rows': '6'}
        )
    )

    def clean_image(self):
        correct_width,correct_height=True,True
        start_img=self.cleaned_data['image']
        if not start_img is None:
            img = Image.open(start_img)
            width, height = img.size
            if not (320<=width<=2048):
                correct_width=False
            if not (240<=height<=1968):
                correct_height=False
            if correct_height and correct_width:
                return start_img
            elif correct_height==False and correct_width:
                fheight=int((width*3)/4)
                if (240<=fheight<=1968):
                    img.resize((width, fheight), Image.ANTIALIAS)
                    return start_img
                else:
                    raise forms.ValidationError("Форамат изображения должен быть от 320х240 до 2048х1968")
            # elif correct_height and correct_width==False:
            #     pass
            else:
                raise forms.ValidationError("Форамат изображения должен быть от 320х240 до 2048х1968")

    def save(self, commit=True,pil=None):
        note = super(NoteCreationForm, self).save(commit=False)
        if commit:
            note.save()
        return note

class SentNoteForm (forms.Form):

    recipient_email = forms.EmailField(
        label='Email получателя',
        widget=forms.TextInput(
            attrs={'placeholder': 'Email получателя', 'class': 'form-control', 'data-validation': 'email'}),
    )


import asyncio
import hashlib
import time

import pytest
from async_generator import async_generator, yield_

import aiomisc

class HashServer(aiomisc.service.TCPServer):
    async def handle_client(
        self, reader: asyncio.StreamReader,
        writer: asyncio.StreamWriter
    ):
        hasher = hashlib.md5()

        chunk = await reader.read(65534)
        while chunk:
            hasher.update(chunk)
            writer.write(hasher.digest())
            chunk = await reader.read(65534)

        writer.close()
        await writer.wait_closed()

@pytest.fixture()
def server_port(aiomisc_unused_port_factory) -> int:
    return aiomisc_unused_port_factory()

@pytest.fixture()
def service(
    loop: asyncio.AbstractEventLoop,
    server_port, localhost,
) -> HashServer:
    return HashServer(port=server_port, address=localhost)

@pytest.fixture()
def services(service: HashServer):
    return [service]

@pytest.fixture()
@async_generator
async def proxy(tcp_proxy, localhost, server_port):
    async with tcp_proxy(localhost, server_port, buffered=True) as proxy:
        await yield_(proxy)

async def test_proxy_client(proxy):
    reader, writer = await proxy.create_client()
    payload = b"Hello world"

    writer.write(payload)
    hash = await asyncio.wait_for(reader.readexactly(16), timeout=1)

    assert hash == hashlib.md5(payload).digest()

async def test_proxy_client_close(proxy):
    reader, writer = await proxy.create_client()
    payload = b"Hello world"

    writer.write(payload)
    hash = await asyncio.wait_for(reader.readexactly(16), timeout=1)

    assert hash == hashlib.md5(payload).digest()

    assert not reader.at_eof()
    await proxy.disconnect_all()

    assert await asyncio.wait_for(reader.read(), timeout=1) == b""
    assert reader.at_eof()

async def test_proxy_client_slow(proxy):
    delay = 0.1
    proxy.set_delay(delay, delay)

    reader, writer = await proxy.create_client()
    payload = b"Hello world"

    delta = -time.monotonic()
    writer.write(payload)
    hash = await asyncio.wait_for(reader.readexactly(16), timeout=2)
    delta += time.monotonic()

    assert delta >= delay, [c.delay for c in proxy.clients]

    assert hash == hashlib.md5(payload).digest()

async def test_proxy_client_with_processor(proxy):
    processed_request = b"Never say hello"

    proxy.set_content_processors(
        lambda _: processed_request,
        lambda chunk: chunk[::-1],
    )

    reader, writer = await proxy.create_client()
    payload = b"Hello world"

    writer.write(payload)
    hash = await asyncio.wait_for(reader.read(16), timeout=2)

    assert hash == hashlib.md5(processed_request).digest()[::-1]


batch_size = 16
epochs = 200
lr = 0.01
momentum = 0.9
no_cuda =False
cuda_id = '0'
seed = 1
log_interval = 10
l2_decay = 5e-4
class_num = 10
param = 0.3
bottle_neck = True
root_path = "D:/zcm/dataset/dataset/"
source_name       = "DE_6205_3HP/train"
target_train_name = "DE_6205_1HP/val"
target_test_name  = "DE_6205_1HP/test"


#!/bin/python3

def validate_html(html):
    '''
    This function performs a limited version of html validation by
    checking whether every opening tag has a corresponding closing tag.

    >>> validate_html('example')
    True
    >>> validate_html('example')
    False
    '''
    if len(html) == 0:
        return True
    else:
        extracted_html = _extract_tags(html)
        stack = []
        char = "/"
        if len(extracted_html) == 0:
            return False
        else:
            for tag in extracted_html:
                if char not in tag:
                    stack.append(tag)
                else:
                    if len(stack) == 0:
                        return False
                    tag_new = tag.replace("/", "")
                    if stack[-1] == tag_new:
                        stack.pop()
                    else:
                        return False
            return len(stack) == 0

def _extract_tags(html):
    '''
    This is a helper function for `validate_html`.
    By convention in Python, helper functions that are not meant
    to be used directly by the user are prefixed with an underscore.

    This function returns a list of all the html tags contained
    in the input string, stripping out all text not contained
    within angle brackets.

    >>> _extract_tags('Python rocks!')
    ['', '']
    '''
    html_tags = []
    tag = ""
    flag = 0
    for symbol in html:
        if symbol == "<":
            flag = 1
        if flag == 1:
            tag += symbol
        if symbol == ">":
            html_tags.append(tag)
            tag = ""
            flag = 0
    return html_tags


# -*- coding: utf-8 -*-

import os

from flask import render_template, redirect, url_for, request, g
from flask import send_from_directory
from flask_login import login_user, logout_user, current_user, login_required

from app import app, login_manager
from app import db
from .models import User, Party

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

def validateAndAdd(id):
    cur_user = User.query.filter_by(id_number=current_user.id_number).all()
    party = Party.query.filter_by(id=id).first()
    party.vote_count += 1
    for user in cur_user:
        user.voted = True
    db.session.commit()

@app.route('/', methods=['GET'])
@app.route('/index', methods=['GET', 'POST'])
@login_required
def index():
    error = None
    if request.method == 'POST':
        validateAndAdd(request.form['party_name'])
        return redirect(url_for('login'))
    g.user = current_user  # global user parameter used by flask framwork
    parties = Party.query.all()
    return render_template('index.html', title='Home', user=g.user, parties=parties, error=error)

@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        ## Validate user
        first_name = request.form['first_name']
        last_name = request.form['last_name']
        id_number = request.form['id_number']
        check_db = User.query.filter_by(id_number=id_number).all()
        if check_db == None:
            error = u'המצביע אינו מופיע בבסיס הנתונים'
        else:
            for user in check_db:
                if first_name == user.first_name and last_name == user.last_name and id_number == user.id_number:
                    if user.voted:
                        error = u'ניתן להצביע פעם אחת בלבד'
                        return render_template('login.html', error=error)

                    else:
                        # user = User.query.filter_by(id_number=id_number).first()
                        login_user(user)  ## built in 'flask login' method that creates a user session
                        return redirect(url_for('index'))

            error = u'המצביע אינו מופיע בבסיס הנתונים'
    if error is not None:
        return render_template('login.html', error=error), 404

    return render_template('login.html', error=error), 200

## will handle the logout request
@app.route('/logout')
@login_required
def logout():
    logout_user()  ## built in 'flask login' method that deletes the user session
    return redirect(url_for('index'))

## secret page that shows the user name
@app.route('/secret', methods=['GET'])
@login_required
def secret():
    return 'This is a secret page. You are logged in as {} {} {}'.format(current_user.first_name,
                                                                         current_user.last_name,
                                                                         current_user.id_number)

## will handle the site icon - bonus 2 points for creative new icon
@app.route('/favicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'), 'Realfavicon.ico',
                               mimetype='image/vnd.microsoft.icon')


from django.conf.urls import patterns, include, url

__author__ = 'Stanislav Filin'

urlpatterns = patterns('',
                       url(r'^login/', 'loginsys.views.login'),
                       url(r'^logout/', 'loginsys.views.logout'),
                       url(r'^register/', 'loginsys.views.register'),
                       url(r'^profile/', 'loginsys.views.profile'),
                       url(r'^experiment/', 'loginsys.views.experiment'),
                       )


from distutils import log

from weaverbird.backends.sql_translator.steps.utils.query_transformation import (
    build_selection_query,
)
from weaverbird.backends.sql_translator.types import (
    SQLPipelineTranslator,
    SQLQuery,
    SQLQueryDescriber,
    SQLQueryRetriever,
)
from weaverbird.pipeline.steps import UnpivotStep

def translate_unpivot(
    step: UnpivotStep,
    query: SQLQuery,
    index: int,
    sql_query_retriever: SQLQueryRetriever = None,
    sql_query_describer: SQLQueryDescriber = None,
    sql_translate_pipeline: SQLPipelineTranslator = None,
) -> SQLQuery:
    query_name = f'UNPIVOT_STEP_{index}'

    log.debug(
        '############################################################'
        f'query_name: {query_name}\n'
        '------------------------------------------------------------'
        f'step: {step}\n'
        f'query.transformed_query: {query.transformed_query}\n'
        f'query.metadata_manager.query_metadata: {query.metadata_manager.retrieve_query_metadata()}\n'
    )
    unpivot_query = f"""SELECT {', '.join(step.keep + [step.unpivot_column_name.upper(),
                                                       step.value_column_name.upper()])}\
 FROM {query.query_name} UNPIVOT({step.value_column_name.upper()} FOR {step.unpivot_column_name.upper()} IN\
 {str(step.unpivot).replace('[', '(').replace(']', ')').replace("'", '')}"""

    transformed_query = f"""{query.transformed_query}, {query_name} AS ({unpivot_query}))"""
    unpivotted_value_column_type = query.metadata_manager.retrieve_query_metadata_column_by_name(
        step.unpivot[0]
    ).type
    query.metadata_manager.remove_query_metadata_columns(
        [
            c
            for c in query.metadata_manager.retrieve_query_metadata_columns().keys()
            if c not in step.keep
        ]
    )
    query.metadata_manager.add_query_metadata_column(
        column_name=step.unpivot_column_name, column_type='str'
    )
    query.metadata_manager.add_query_metadata_column(
        column_name=step.value_column_name,
        # We infer that type of value column will be the same as first unpivoted column's type.
        # If the provided columns to unpivot have mixed types the unpivot will fail.
        column_type=unpivotted_value_column_type,
    )

    new_query = SQLQuery(
        query_name=query_name,
        transformed_query=transformed_query,
        selection_query=build_selection_query(
            query.metadata_manager.retrieve_query_metadata_columns(), query_name
        ),
        metadata_manager=query.metadata_manager,
    )

    log.debug(
        '------------------------------------------------------------'
        f'SQLquery: {new_query.transformed_query}'
        '############################################################'
    )

    return new_query


import os
from gtts import gTTS

def iTTS(string):

	language = 'pt-br'

	mytext = string

	myobj = gTTS(text = mytext, lang = language, slow = False)

	myobj.save("welcome.mp3")
	os.system("mpg321 welcome.mp3")
	print("Insperina:"+mytext)


"""
The Flight Director app provides a Flight Director Application to control a
vehicle's basic axes of motion.

Apps:
    FlightDirector  Grants the user control of a vehicle's basic axes.
"""

from pyliner.app import App
from pyliner.action import ACTION_AXIS_SET, ACTION_SEND_COMMAND
from pyliner.intent import Intent
from pyliner.telemetry import ManualSetpoint

class FlightDirector(App):
    """The FlightDirector allows for manual, direct control of a vehicle's axes.

     The X, Y, Z, and Yaw (R) axes are controlled via a ManualSetpoint command.
     """
    def __init__(self, strict_set=False):
        super(FlightDirector, self).__init__()
        self._x = self._y = self._z = self._r = 0.0
        self.strict_set = strict_set

    def __call__(self, x=None, y=None, z=None, r=None):
        if x is not None:
            self.x = x
        if y is not None:
            self.y = y
        if z is not None:
            self.z = z
        if r is not None:
            self.r = r

    def receive(self, intent):
        if intent.action == ACTION_AXIS_SET:
            axis, value = intent.data
            self(**{axis: value})

    def attach(self, vehicle_wrapper):
        super(FlightDirector, self).attach(vehicle_wrapper)
        self.vehicle.callback = self.receive

    def detach(self):
        self.vehicle.callback = None
        super(FlightDirector, self).detach()

    def _send_telemetry(self):
        mod_z = self.z / 2.0 + 0.5  # [-1, 1] -> [0, 1]
        self.vehicle.broadcast(Intent(
            action=ACTION_SEND_COMMAND,
            data=ManualSetpoint(
                X=self._x, Y=self._y, Z=mod_z, R=self._r,
                PosctlSwitch=1, GearSwitch=1, ArmSwitch=1)
        ))

    @property
    def r(self):
        return self._r

    @r.setter
    def r(self, value):
        if self.strict_set and (value > 1 or value < -1):
            raise ValueError('Cannot assign R outside of [-1, 1].')
        self._r = value
        self._send_telemetry()

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, value):
        if self.strict_set and (value > 1 or value < -1):
            raise ValueError('Cannot assign X outside of [-1, 1].')
        self._x = value
        self._send_telemetry()

    @property
    def y(self):
        return self._y

    @y.setter
    def y(self, value):
        if self.strict_set and (value > 1 or value < -1):
            raise ValueError('Cannot assign Y outside of [-1, 1].')
        self._y = value
        self._send_telemetry()

    @property
    def z(self):
        return self._z

    @z.setter
    def z(self, value):
        if self.strict_set and (value > 1 or value < -1):
            raise ValueError('Cannot assign Z outside of [-1, 1].')
        self._z = value
        self._send_telemetry()

    def zero(self):
        self.x = self.y = self.z = self.r = 0.0
        self._send_telemetry()


# -*- coding: utf-8 -*-
"""
Created on Sun Aug 19 14:25:20 2018

@author: Hemant Rathore
"""

# 6. Timeseries Analysis using Seasonal ARIMA

## Case Study - Seasonal ARIMA Model for Industrial production of electric and gas utilities
## in the United States, from the years 1985–2018, with our frequency being Monthly.

import os

os.chdir("D:\Data Science Training\Data")

import pandas as pd

import numpy as np

energy_data = pd.read_csv("IPG_Dataset.csv")

energy_data.head(10)

energy_data.describe()

# Plot the TS & Notice trend,seasonality,heteroscedasticity in TS

energy_data.plot(figsize=(12,12))

# Decompose TS

from statsmodels.tsa.seasonal import seasonal_decompose

result = seasonal_decompose(energy_data.PRODUCTION, model='multiplicative',freq=12)

from pylab import rcParams

rcParams['figure.figsize'] = 10,10

result.plot()

## Summary

# Hetro.- yes
# trend- yes
# Seasonality - yes

# 1. Variance is increasing so take log to remove heteroscedasticity

import numpy as np

import pandas as pd

energy = energy_data.PRODUCTION

log_energy = np.log(energy)

log_energy.plot()
energy.plot()

# 2 Detrend TS by differencing

from pandas import Series

d_log_energy = Series.diff(log_energy,periods=1)

d_log_energy.plot()

# 3 remove Seasonality by taking seasonal difference

dd_log_energy = Series.diff(d_log_energy,periods=12)

dd_log_energy.plot(figsize=(10,5))

## Model Selection

# ARIMA(p,d,q)[non seasonal], (p,d,q)[12][seasonal]

#(p,1,q)(p,1,q)[12]

# Analyze ACF (MA) and PACF(AR) to conclude model

dd_log_energy.isna()

dd_log_energy.dropna(inplace=True)

import statsmodels.api as sm
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10,10))

ax = fig.add_subplot()
fig = sm.graphics.tsa.plot_acf(dd_log_energy, lags=50, ax=ax)

# non seasonal : MA(2) as non seasonal is cutting  off after 2 lags (ignore 0th lag)
# seasonal : MA(2) cutting off after 2 lags

#(p,1,2)(p,1,2)[12]

fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot()
fig = sm.graphics.tsa.plot_pacf(dd_log_energy, lags=50, ax=ax)

# non seasonal : AR(2) as cutting off after 2 lags
# seasonal : AR(0) because its tailing off

#(2,1,2)(0,1,2)[12]

arima_model = sm.tsa.statespace.SARIMAX(energy,
                                            order=(2,1,2),
                                            seasonal_order=(0,1,2,12),
                                            enforce_stationarity=False,
                                            enforce_invertibility=False)

arima_model_fit = arima_model.fit()

arima_model_fit.save

pred = arima_model_fit.get_prediction(start = 900, end = 967, dynamic=False)

ax = energy.plot(label='observed')
pred.predicted_mean.plot(ax=ax, label='forecast', alpha=.7)

plt.legend()

ax = energy[850:1000].plot(label='observed',figsize=(15,10),color='red')
pred.predicted_mean.plot(ax=ax, label='forecast', alpha=.7,color='blue')

plt.legend()

Topic13/Cognixia - Data Science with Python - Machine Learning Case Study 6.py


#!/usr/bin/env python3
#author:Alnk(李成果)
import sys,os,time

def search(str1):
    """查询函数"""
    backend_str1 = "backend %s" %str1
    line_list = []                                              #待显示的列表
    with open("haproxy","r",encoding="utf-8") as f1:
        flag = False                                            #标识位
        for line in f1:
            line = line.strip()
            if backend_str1 == line:
                flag = True
                continue
            if flag and line.startswith("backend"):
                flag = False
                break
            if flag and line:
                line_list.append(line)
    if line_list:
        print("查询结果如下：")
        print("\t",line_list,"\n")
    return line_list

def add(arg):
    """添加函数"""
    dic_arg = eval(arg)                                     #把字符串转为字典
    backend = dic_arg.get("backend")                        #获取用户输入的域名
    backend_title = "backend %s"%backend                    #组装backend + 域名
    record_list = search(backend)                           #获取search函数返回值，为一个列表
    if record_list:
        print("%s 该域名已经存在了呀，不要再重复添加了\n" %backend)
    else:
        #组装要添加的记录到record_list列表
        record_list.append(backend_title)
        current_str = "server %s weight %s maxconn %s" %(dic_arg["record"]["server"],dic_arg["record"]["weight"],dic_arg["record"]["maxconn"])
        record_list.append(current_str)
        #添加记录
        with open("haproxy","r") as read_file,open("ha_new","w") as write_file:
            for line in read_file:
                write_file.write(line)
            for i in record_list:
                if i.startswith("backend"):
                    write_file.write("\n"+i+"\n")
                else:
                    write_file.write("%s %s"%(" "*8,i))
    tag = time.time()
    ha_back = "ha_back-%s" %tag
    print("============",ha_back)
    os.rename("haproxy",ha_back)
    os.rename("ha_new", "haproxy")
    print("添加成功\n")
    return True

def remove(dict_info):
    """删除函数"""
    record_list = []
    dict_info = eval(dict_info)                             #把字符串转为字典
    backend = dict_info.get("backend")                      #获取用户输入的域名
    backend_title = "backend %s" % backend                  # 组装backend + 域名
    record_list.append(backend_title)
    current_record = "server %s weight %s maxconn %s" %(dict_info["record"]["server"],dict_info["record"]["weight"],dict_info["record"]["maxconn"])
    record_list.append(current_record)
    with open("haproxy","r") as read_file,open("ha_new","w") as write_file:
        for line in read_file:
            if line.strip() not in record_list:
                write_file.write(line)
    tag = time.time()
    ha_back = "ha_back-%s" %tag
    os.rename("haproxy",ha_back)
    os.rename("ha_new", "haproxy")
    return True

if __name__ == "__main__":
    tag = time.time()
    ha_back = "ha_back-%s" % tag
    print(ha_back)
    while True:
        print("热烈欢迎大佬操作 haproxy 文件...\n")
        print("1.查询\n2.添加\n3.删除\n4.退出")
        num = input("输入编号>>>:")
        if num == "1":
            str1 = input("请输入你想查询的域名>>>：")
            search(str1)
        elif num == "2":
            arg = input("请输入你要增加的记录>>>:")
            add(arg)
        elif num == "3":
            dict_info = input("请输入你要删除的记录>>>:")
            remove(dict_info)
        elif num =="4":
            sys.exit("系统已经退出")
        else:
            print("输入有误，请重新输入\n")


#!/usr/bin/python
import os
from sqlite3 import connect as sqlconn

inConn = sqlconn('nsdevman.bbtrac.sqlite3')
inCursor = inConn.cursor()
outConn = sqlconn('nsdevman.blank.sqlite3')
outCursor = outConn.cursor()

for inTable, outTable in (('nsaabill_billmember', 'nsaabill_dbbillmember'),
                          ('nsaabill_billmonthcache', 'nsaabill_dbbillmonthcache'),
                          ('nsproj_project', 'nsproj_dbproject'),
                          ('nsroot_attrdatetime', 'nsroot_dbattrdatetime'),
                          ('nsroot_attrinteger', 'nsroot_dbattrinteger'),
                          ('nsroot_attrmember', 'nsroot_dbattrmember'),
                          ('nsroot_attrpositiveinteger', 'nsroot_dbattrpositiveinteger'),
                          ('nsroot_attrstring', 'nsroot_dbattrstring'),
                          ('nsroot_commentcachedtext', 'nsroot_dbcommentcachedtext'),
                          ('nsroot_commenttext', 'nsroot_dbcommenttext'),
                          ('nsroot_entity', 'nsroot_dbentity'),
                          ('nsroot_idscope', 'nsroot_dbidscope'),
                          ('nsroot_idvalue', 'nsroot_dbidvalue'),
                          ('nsroot_member', 'nsroot_dbmember'),
                          ('nsroot_memberprefs', 'nsroot_dbmemberprefs'),
                          ('nsroot_memberroles', 'nsroot_dbmemberroles'),
                          ('nsroot_perm', 'nsroot_dbperm'),
                          ('nsroot_role', 'nsroot_dbrole'),
                          ('nsroot_roleperms', 'nsroot_dbroleperms'),
                          ('nstool_randombatch', 'nstool_dbrandombatch')):

    rows = list(inCursor.execute('SELECT * FROM %s' % inTable))
    if rows == []: continue
    print('From %s to %s with %u records' % (inTable, outTable, len(rows)))
    outCursor.execute('DELETE FROM %s' % outTable)
    sql = 'INSERT INTO %s VALUES (%s)' % (outTable, ', '.join(['?'] * len(rows[0])))
    outCursor.executemany(sql, rows)
outConn.commit()


f= open("/share/Ecoli/GCA_000005845.2_ASM584v2_genomic.fna", "r")
next(f)
dnaSeq= f.read()
genome=""
startIndex=0
endIndex=0
length= len(dnaSeq)
for x in range(length):
        if(dnaSeq[x]!="\n"):
                genome+=dnaSeq[x]
frags=0
size=0
lengthns= len(genome)
fragment=""
for t in range(11):
	for l in range(t,lengthns-11+t,11):
		part= genome[l:l+11]
		size+=11
		if(part[0:3]=="GAC" and part[8:11]=="GTC"):
			endIndex=l
			size-=11
			frags+=1
			startIndex=l+11
			size=0
print(frags)


# coding: utf-8

"""
    ChronoSheets API

      ChronoSheets is a flexible timesheet solution for small to medium businesses, it is free for small teams of up to 5 and there are iOS and Android apps available.  Use the ChronoSheets API to create your own custom integrations.  Before starting, sign up for a ChronoSheets account at http://tsheets.xyz/signup.

  # noqa: E501

    OpenAPI spec version: v1

    Generated by: https://github.com/swagger-api/swagger-codegen.git
"""

import pprint
import re  # noqa: F401

import six

class CSTimeSlot(object):
    """NOTE: This class is auto generated by the swagger code generator program.

    Do not edit the class manually.
    """

    """
    Attributes:
      swagger_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    swagger_types = {
        'day_type': 'str',
        'usual_hour_id': 'int',
        'start_hour': 'int',
        'start_minute': 'int',
        'end_hour': 'int',
        'end_minute': 'int',
        'is_valid': 'bool'
    }

    attribute_map = {
        'day_type': 'DayType',
        'usual_hour_id': 'UsualHourId',
        'start_hour': 'StartHour',
        'start_minute': 'StartMinute',
        'end_hour': 'EndHour',
        'end_minute': 'EndMinute',
        'is_valid': 'IsValid'
    }

    def __init__(self, day_type=None, usual_hour_id=None, start_hour=None, start_minute=None, end_hour=None, end_minute=None, is_valid=None):  # noqa: E501
        """CSTimeSlot - a model defined in Swagger"""  # noqa: E501

        self._day_type = None
        self._usual_hour_id = None
        self._start_hour = None
        self._start_minute = None
        self._end_hour = None
        self._end_minute = None
        self._is_valid = None
        self.discriminator = None

        if day_type is not None:
            self.day_type = day_type
        if usual_hour_id is not None:
            self.usual_hour_id = usual_hour_id
        if start_hour is not None:
            self.start_hour = start_hour
        if start_minute is not None:
            self.start_minute = start_minute
        if end_hour is not None:
            self.end_hour = end_hour
        if end_minute is not None:
            self.end_minute = end_minute
        if is_valid is not None:
            self.is_valid = is_valid

    @property
    def day_type(self):
        """Gets the day_type of this CSTimeSlot.  # noqa: E501

        :return: The day_type of this CSTimeSlot.  # noqa: E501
        :rtype: str
        """
        return self._day_type

    @day_type.setter
    def day_type(self, day_type):
        """Sets the day_type of this CSTimeSlot.

        :param day_type: The day_type of this CSTimeSlot.  # noqa: E501
        :type: str
        """
        allowed_values = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]  # noqa: E501
        if day_type not in allowed_values:
            raise ValueError(
                "Invalid value for `day_type` ({0}), must be one of {1}"  # noqa: E501
                .format(day_type, allowed_values)
            )

        self._day_type = day_type

    @property
    def usual_hour_id(self):
        """Gets the usual_hour_id of this CSTimeSlot.  # noqa: E501

        :return: The usual_hour_id of this CSTimeSlot.  # noqa: E501
        :rtype: int
        """
        return self._usual_hour_id

    @usual_hour_id.setter
    def usual_hour_id(self, usual_hour_id):
        """Sets the usual_hour_id of this CSTimeSlot.

        :param usual_hour_id: The usual_hour_id of this CSTimeSlot.  # noqa: E501
        :type: int
        """

        self._usual_hour_id = usual_hour_id

    @property
    def start_hour(self):
        """Gets the start_hour of this CSTimeSlot.  # noqa: E501

        :return: The start_hour of this CSTimeSlot.  # noqa: E501
        :rtype: int
        """
        return self._start_hour

    @start_hour.setter
    def start_hour(self, start_hour):
        """Sets the start_hour of this CSTimeSlot.

        :param start_hour: The start_hour of this CSTimeSlot.  # noqa: E501
        :type: int
        """

        self._start_hour = start_hour

    @property
    def start_minute(self):
        """Gets the start_minute of this CSTimeSlot.  # noqa: E501

        :return: The start_minute of this CSTimeSlot.  # noqa: E501
        :rtype: int
        """
        return self._start_minute

    @start_minute.setter
    def start_minute(self, start_minute):
        """Sets the start_minute of this CSTimeSlot.

        :param start_minute: The start_minute of this CSTimeSlot.  # noqa: E501
        :type: int
        """

        self._start_minute = start_minute

    @property
    def end_hour(self):
        """Gets the end_hour of this CSTimeSlot.  # noqa: E501

        :return: The end_hour of this CSTimeSlot.  # noqa: E501
        :rtype: int
        """
        return self._end_hour

    @end_hour.setter
    def end_hour(self, end_hour):
        """Sets the end_hour of this CSTimeSlot.

        :param end_hour: The end_hour of this CSTimeSlot.  # noqa: E501
        :type: int
        """

        self._end_hour = end_hour

    @property
    def end_minute(self):
        """Gets the end_minute of this CSTimeSlot.  # noqa: E501

        :return: The end_minute of this CSTimeSlot.  # noqa: E501
        :rtype: int
        """
        return self._end_minute

    @end_minute.setter
    def end_minute(self, end_minute):
        """Sets the end_minute of this CSTimeSlot.

        :param end_minute: The end_minute of this CSTimeSlot.  # noqa: E501
        :type: int
        """

        self._end_minute = end_minute

    @property
    def is_valid(self):
        """Gets the is_valid of this CSTimeSlot.  # noqa: E501

        :return: The is_valid of this CSTimeSlot.  # noqa: E501
        :rtype: bool
        """
        return self._is_valid

    @is_valid.setter
    def is_valid(self, is_valid):
        """Sets the is_valid of this CSTimeSlot.

        :param is_valid: The is_valid of this CSTimeSlot.  # noqa: E501
        :type: bool
        """

        self._is_valid = is_valid

    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.swagger_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        return pprint.pformat(self.to_dict())

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, CSTimeSlot):
            return False

        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        return not self == other


from twistar.dbobject import DBObject
import datetime
import pytz

from twisted.internet.defer import inlineCallbacks

class Device(DBObject):
    """LoRa device class

    Model representing a LoRa device.

    Attributes:
        devaddr (int): 32 bit end device address (DevAddr)
        deveui (int): Global end-device ID (IEEE EUI64)
        nwkskey (int): 128 bit network session key (NwkSKey)
        appskey (int): 128 bit application session key (AppSKey)
        tx_chan (int): Transmit channel number
        tx_datr (str): Transmit data rate
        adr_datr (str): Target ADR data rate
        gw_addr (str): The gateway IP address
        fcntup (int): Uplink frame  counter received from the device
        fcntdown (int): Downlink frame counter sent to to the device
        fcnterror (bool): Frame count error flag
        snr_pointer (int): Pointer to the next ADR measure
        snr_average (float): Calculated ADR average SNR
        snr1 (float): SNR reading 1
        snr2 (float): SNR reading 2
        snr3 (float): SNR reading 3
        snr4 (float): SNR reading 4
        snr5 (float): SNR reading 5
        snr6 (float): SNR reading 6
        snr7 (float): SNR reading 7
        snr8 (float): SNR reading 8
        snr9 (float): SNR reading 9
        snr10 (float): SNR reading 10
        snr11 (float): SNR reading 11
        created (str): Timestamp when the device object is created
        updated (str): Timestamp when the device object is updated
    """

    TABLENAME = 'devices'

    def beforeCreate(self):
        """Twistar method called before a new object is created.

         Returns:
            True on success. If False is returned, then the object is
            not saved in the database.
        """
        self.created = datetime.datetime.now(tz=pytz.utc).isoformat()
        return True

    def beforeSave(self):
        """Twistar method called before an existing object is saved.

        This method is called after beforeCreate when an object is being
        created, and after beforeUpdate when an existing object
        (whose id is not None) is being saved.

        Returns:
            True on success. If False is returned, then the object is
            not saved in the database.
        """
        self.updated = datetime.datetime.now(tz=pytz.utc).isoformat()
        return True

    @inlineCallbacks
    def update(self, *args, **kwargs):
        """Updates the object with a variable list of attributes

        """
        if kwargs is not None:
            yield self.refresh()
            for key, value in kwargs.iteritems():
                if hasattr(self, key):
                    setattr(self, key, value)
            yield self.save()

    def resetFrameCount(self):
        """Reset frame count parameters

        """
        self.fcntup = 0
        self.fcntdown = 0
        self.fcnterror = False

    def checkFrameCount(self, fcntup, maxfcntgap, relaxed):
        """Sync fcntup counter with received value

        The value received must have incremented compared to current
        counter value and must be less than the gap value specified
        by MAX_FCNT_GAP after considering rollovers. Otherwise,
        too many frames have been lost.

        Args:
            fcntup (int): Received fcntup value
            maxfcntgap (int): MAX_FCNT_GAP, band specific
            relaxed (bool): frame count relaxed flag

        Returns:
            True if fcntup is within the limit, otherwise False.

        """
        # Relxed mode. If fcntup = 1 then set fcntdown to zero
        # and the device fntup to 1.
        if relaxed and fcntup == 1:
            self.fcntdown = 0
            self.fcntup = 1
            self.fcnterror = False
        elif fcntup > (self.fcntup + maxfcntgap):
            self.fcnterror = True
        elif fcntup < self.fcntup and (65535 - self.fcntup + fcntup) > maxfcntgap:
            self.fcnterror = True
        else:
            self.fcntup = fcntup
            self.fcnterror = False

        return not self.fcnterror

    def updateSNR(self, lsnr):
        """Update Device SNR measures

        Updates the most recent received device SNR measure.
        We keep 11 samples for each device, and use the
        snr_pointer attribute to maintain the current sample
        index.

        Args:
            lsnr (float): Latest link SNR measure.

        """
        # Check we have a SNR measure
        if lsnr is None:
            return

        if self.snr_pointer is None:
            self.snr_pointer = 1

        # Update the current SNR reading
        snr = str('snr' + str(self.snr_pointer))
        setattr(self, snr, lsnr)

        # Average SNR readings
        self.averageSNRs()

        # Increment/rotate the pointer
        self.snr_pointer = self.snr_pointer % 11 + 1

    def averageSNRs(self):
        """Calculates the average of the last six SNR readings.
        If we do not have six valid readings, set average to
        None.

        Args:
            None
        """
        # Get the last 6 SNR readings
        if self.snr_pointer >= 6:
            measures = range(self.snr_pointer - 5, self.snr_pointer + 1)
        else:
            measures = range(1, self.snr_pointer + 1)
            measures.extend(range(self.snr_pointer + 6, 12))
        snrs = []
        for i in measures:
            attr = 'snr' + str(i)
            snr = getattr(self, attr) if hasattr(self, attr) else None
            snrs.append(snr)

        # Calculate the average
        self.snr_average = sum(snrs)/len(snrs) if not None in snrs else None

    def getADRDatarate(self, band, margin):
        """Determine the optimal datarate that will achieve the
        objective margin in the given band.

        We assume each increase in datarate step (e.g. DR0 to DR1) requires
        an additional 3dB in SNR.

        Args:
            band (Band): Band in use
            margin (float): Target margin in dB

        Returns:
            Optimal datarate as a string on success, otherwise None.
        """
        if not hasattr(self, 'snr_average') or self.snr_average is None:
            return None

        # Target thresholds that the average must exceed. Note range(0,4) refers
        # to the first four (upstream) indices of the band.datarate list. These
        # are DR0, DR1, DR2, DR3
        thresholds = [float(i) * 3.0 + margin for i in range(0,4)]

        # If we have an average SNR less than the lowest threshold, return the lowest DR
        if self.snr_average < thresholds[0]:
            return band.datarate[0]

        # Find the index of lowest threshold that the SNR average just exceeds
        i = [n for n,v in enumerate(thresholds) if self.snr_average >= v][-1]
        return band.datarate[i]


# coding: utf-8
from telethon import TelegramClient, events
from time import sleep
import os, re
from subprocess import getoutput

#id number and api hash for log in the account
api_hash = ''
api_id = ''

#Instance of client
client = TelegramClient('{}/test'.format(os.environ['HOME']), api_id, api_hash)

@client.on(events.NewMessage(chats='PresidenteAMLO'))
async def messageH(event):
    testing = event.raw_text
    if re.search('(?i).?conferencia.*prensa.*matutina.?', testing):
        testing = re.search('https://you.*', testing)
        if testing:
            testing = testing.group(0)
            testing = testing.split()
            testing = testing[0]
            minutes = 0
            while minutes < 180:
                sleep(60)
                minutes += 1
            os.system('youtube-dl -Uicvxf bestaudio -R 50 -o \%\(title\)s\.\%\(ext\)s --write-thumbnail --audio-format mp3 --embed-thumbnail --add-metadata --write-description {}'.format(testing))
            testing = getoutput('youtube-dl -se {}'.format(testing))
            await client.send_file('lamaaudio', '{}/{}.mp3'.format(os.environ['HOME'], testing))

client.start()
client.run_until_disconnected()


from utils import *
import tensorflow as tf
import numpy as np
from loadData import DataSet
import random

class GANNet(object):

    def __init__(self, category='Mnist', vis_step=10, Train_Epochs=200, batch_size=128, z_size=100, lr=0.001,
                 history_dir='./', checkpoint_dir='./', logs_dir='./', gen_dir='./'):
        self.test = False

        self.category = category
        self.epoch = Train_Epochs
        self.img_size = 28 if (category == 'Fashion-Mnist' or category == 'Mnist') else 64
        self.batch_size = batch_size
        self.z_size = z_size
        self.vis_step = vis_step

        self.lr = lr
        self.channel = 1 if (category == 'Fashion-Mnist' or category == 'Mnist') else 3

        self.history_dir = history_dir
        self.checkpoint_dir = checkpoint_dir
        self.logs_dir = logs_dir
        self.gen_dir = gen_dir

        self.z = tf.placeholder(tf.float32, shape=[self.batch_size, self.z_size], name='latent')

        self.x = tf.placeholder(tf.float32, shape=[self.batch_size, self.img_size, self.img_size, self.channel],
                                name='image')

    def build_Model(self):
        self.gen = self.Generator(self.z, reuse=False)
        self.D_x = self.Discriminator(self.x, reuse=False)
        self.D_gen = self.Discriminator(self.gen, reuse=True)

        """
        Loss and Optimizer
        """
        self.d_loss_real = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_x, labels=tf.ones_like(self.D_x)))
        self.d_loss_fake = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_gen, labels=tf.zeros_like(self.D_gen)))
        self.g_loss = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_gen, labels=tf.ones_like(self.D_gen)))

        self.d_loss = self.d_loss_real + self.d_loss_fake

        self.g_var = [var for var in tf.trainable_variables() if var.name.startswith('Gen')]
        self.d_var = [var for var in tf.trainable_variables() if var.name.startswith('Dis')]

        self.d_optim = tf.train.AdamOptimizer(self.lr) \
            .minimize(self.d_loss, var_list=self.d_var)
        self.g_optim = tf.train.AdamOptimizer(self.lr) \
            .minimize(self.g_loss, var_list=self.g_var)
        """
        Logs
        """
        tf.summary.scalar('g_loss', tf.reduce_mean(self.g_loss))
        tf.summary.scalar('d_loss', tf.reduce_mean(self.d_loss))
        # TODO showing specifically
        # tf.summary.histogram('hyper params', self.hyper_var)
        self.summary_op = tf.summary.merge_all()

    def Generator(self, z, reuse=False):
        with tf.variable_scope('Gen', reuse=reuse):
            if self.category == 'Fashion-Mnist' or self.category == 'Mnist':
                z = tf.reshape(z, [-1, self.z_size])

                fc1 = tf.layers.dense(inputs=z, units=1024, name='fc1')

                fc1 = tf.nn.leaky_relu(tf.contrib.layers.batch_norm(fc1, is_training=True))

                fc2 = tf.layers.dense(inputs=fc1, units=6272, name='fc2')

                fc2 = tf.nn.leaky_relu(tf.contrib.layers.batch_norm(fc2, is_training=True))

                fc2 = tf.reshape(fc2, [self.batch_size, 7, 7, 128])

                dc1 = deconv2d(fc2, (self.batch_size, self.img_size // 2, self.img_size // 2, 128), kernal=(5, 5),
                               name='dc1')
                dc1 = tf.contrib.layers.batch_norm(dc1, is_training=True)
                dc1 = tf.nn.leaky_relu(dc1)

                dc2 = deconv2d(dc1, (self.batch_size, self.img_size // 1, self.img_size // 1, 1), kernal=(5, 5),
                               name='dc2')

                output = tf.nn.tanh(dc2)

            return output

    def Discriminator(self, x, reuse=False):
        with tf.variable_scope('Dis', reuse=reuse):
            if self.category == 'Fashion-Mnist' or self.category == 'Mnist':

                c1 = tf.nn.leaky_relu(conv2d(x, 1, name='c1'))

                c2 = tf.contrib.layers.batch_norm(conv2d(c1, 64, name='c2'), is_training=True)
                c2 = tf.nn.leaky_relu(c2)
                c2 = tf.reshape(c2, [self.batch_size, -1])

                fc1 = tf.layers.dense(inputs=c2, units=1024, name='fc1')
                fc1 = tf.nn.leaky_relu(tf.contrib.layers.batch_norm(fc1, is_training=True))

                output = tf.layers.dense(inputs=fc1, units=1, name='fc2')

            return output

    def train(self, sess):
        self.build_Model()

        data = DataSet(img_size=self.img_size, batch_size=self.batch_size, category=self.category)

        sess.run(tf.global_variables_initializer())

        saver = tf.train.Saver(max_to_keep=10)

        writer = tf.summary.FileWriter(self.logs_dir, sess.graph)

        start = 0
        latest_checkpoint = tf.train.latest_checkpoint(self.checkpoint_dir)

        if latest_checkpoint:
            latest_checkpoint.split('-')
            start = int(latest_checkpoint.split('-')[-1])
            saver.restore(sess, latest_checkpoint)
            print('Loading checkpoint {}.'.format(latest_checkpoint))

        tf.get_default_graph().finalize()

        # latent_gen = np.random.normal(size=(len(data), self.z_size))

        for epoch in range(start + 1, self.epoch):
            num_batch = int(len(data) / self.batch_size)
            d_losses = []
            g_losses = []
            for step in range(num_batch):
                obs = data.NextBatch(step)
                # z = latent_gen[step * self.batch_size: (step + 1) * self.batch_size].copy()
                z = np.random.normal(size=(self.batch_size, self.z_size))

                d_loss, _ = sess.run([self.d_loss, self.d_optim], feed_dict={self.z: z, self.x: obs})
                d_losses.append(d_loss)
                # writer.add_summary(summary, global_step=epoch)

                g_loss, _ = sess.run([self.g_loss, self.g_optim], feed_dict={self.z: z})
                g_losses.append(g_loss)
                # writer.add_summary(summary, global_step=epoch)

                # _ = sess.run(self.g_optim, feed_dict={self.z: z})

            print(epoch, " dis Loss: ", np.mean(d_losses), " gen loss: ", np.mean(g_losses))
            if epoch % self.vis_step == 0:
                self.visualize(saver, sess, len(data), epoch, np.random.normal(size=(len(data), self.z_size)), data)

    def visualize(self, saver, sess, num_data, epoch, latent_gen, data):
        saver.save(sess, "%s/%s" % (self.checkpoint_dir, 'model.ckpt'), global_step=epoch)
        idx = random.randint(0, int(num_data / self.batch_size) - 1)
        z = latent_gen[idx * self.batch_size: (idx + 1) * self.batch_size]

        """
        Generation
        """
        # obs = data.NextBatch(idx, test=True)
        z = np.random.normal(size=(self.batch_size, self.z_size))
        sys = sess.run(self.gen, feed_dict={self.z: z})
        sys = np.array((sys + 1) * 127.5, dtype=np.float)
        path = self.gen_dir + 'epoch' + str(epoch) + 'gens.jpg'
        show_in_one(path, sys, column=16, row=8)