# -------------------------------------------------------------------------------------------------
# http://www.reddit.com/r/dailyprogrammer/comments/2onyoq/2014128_challenge_192_easy_carry_adding/
# --- PREAMBLE ---
# implements carry addition for arbitrary sums of natural numbers and (terminating) decimal numbers
# ---letting python do the addition and inferring the carries after the fact is of course much
# simpler, but it detracts from the part of the challenge that interested me
import sys

# -------------------------------------------------------------------------------------------------
# --- THE CARRY ADDER FUNCTIONS ---
def int_carry_add(num_list):
	"""expects an iterable of non-negative integers; returns the carries and the sum"""
	max_wid     = len(max((str(num) for num in num_list), key=len))
	summands    = [("{:>" + str(max_wid) + "}").format(num) for num in num_list]
	carries     = [0] * max_wid
	partial_sum = [sum((0 if num[i] == " " else int(num[i])) for num in summands)
		for i in reversed(range(max_wid))]

	j = 0
	while not set(partial_sum) <= set(range(10)):
		carries[j] += partial_sum[j] // 10
		cur_num = "".join(reversed(str(partial_sum[j])))
		partial_sum[j] = 0
		for k in range(len(cur_num)):
			try:
				partial_sum[j + k] += int(cur_num[k])
			except IndexError:
				partial_sum.append(int(cur_num[k]))
				carries.append(0)
		j += 1

	carries = list(reversed(carries[:-1]))
	total   = int("".join(str(num) for num in reversed(partial_sum)))
	return carries, total

def dec_carry_add(num_list, n_digits=10):
	"""
	expects an iterable of non-negative terminating decimals; returns the left carries (those
	before the decimal point), right carries (those after the decimal point) and the sum
	"""
	summands               = [int(10 ** n_digits * round(num, n_digits)) for num in num_list]
	int_carries, int_total = int_carry_add(summands)

	return int_carries[:1 - n_digits], int_carries[1 - n_digits:], round(
		int_total / (10 ** n_digits), n_digits)

# -------------------------------------------------------------------------------------------------
# --- THE CARRY PRINTER FUNCTION ---
def print_sum(num_list, n_digits=3):
	"""
	performs a carry-add on num_list and prints the result with nice formatting; n_digits specifies
	the number of digits past the decimal point to worry about, and setting it to zero makes the
	function treat the input as integers rather than floats
	"""
	*carries, total = dec_carry_add(num_list, n_digits)

	check = sum(float(num) for num in num_list)
	assert abs(total - check) < 1 / (10 ** n_digits), ("With summands {} the sum equals {} " +
		"(using pythons '+'), but our carry add gave {} instead!").format(
			", ".join(str(num) for num in num_list), check, total)

	short = (max(len(str(term)) for term in set(carries[0]) | set(carries[1])) == 1)
	for index, car in enumerate(carries):
		carries[index] = ("" if short else ", ").join((str(num) if num != 0 else
			(" " if short else "0") for num in car))

	width        = len(str(int(total))) + n_digits + (1 if n_digits else 0)
	add_scheme_1 = "   {:>" + str(width) + "." + str(n_digits) + "f}"
	add_scheme_2 = " + {:>" + str(width) + "." + str(n_digits) + "f}"
	total_scheme = " = {:." + str(n_digits) + "f}"
	carry_scheme = ("   {}" + (" " if n_digits else "") + "{}") if short else "carries: {}, {}"
	bar          = "   " + "-" * width

	for num in num_list[:-1]:
		print(add_scheme_1.format(num))
	print(add_scheme_2.format(num_list[-1]))
	print(bar)
	print(total_scheme.format(total))
	print(bar)
	print(carry_scheme.format(*carries))

# -------------------------------------------------------------------------------------------------
# --- MAIN FUNCTION ---
def main():
	# grab the data, perform the carry addition; if no arguments passed, use the example data
	*summands, precision = sys.argv[1:] if len(sys.argv[1:]) > 0 else ["444", "8860", "175", "0"]
	for term in set(summands) | {precision}:
		assert term.isdigit(), "The term {} is not a non-negative integer!".format(term)
	summands = [int(term) for term in summands]
	precision = int(precision)

	print_sum(summands, precision)

if __name__ == "__main__":
	main()


#coding: UTF-8
import math
import pandas as pd
import datetime as dt
import numpy as np
import cupy as cp
import matplotlib.pylab as plt
import chainer
from chainer import cuda, Function, gradient_check, Variable, optimizers, optimizer, serializers, utils, Link, Chain, ChainList
import chainer.functions as F
import chainer.links as L
import csv

class LSTM(Chain):
    def __init__(self, in_size, hidden_size, out_size):
        # クラスの初期化
        # :param in_size: 入力層のサイズ
        # :param hidden_size: 隠れ層のサイズ
        # :param out_size: 出力層のサイズ
        super(LSTM, self).__init__(
            xh = L.Linear(in_size, hidden_size),
            hh = L.LSTM(hidden_size, hidden_size),
            hy =  L.Linear(hidden_size, out_size)
        )

    def __call__(self, x, t=None, train=False):
        # 順伝播の計算を行う関数
        # :param x: 入力値
        # :param t: 正解の予測値
        # :param train: 学習かどうか
        # :return: 計算した損失 or 予測値
        x = Variable(x)
        if train:
            t = Variable(t)
        h = self.xh(x)
        h = self.hh(h)
        y = self.hy(h)
        if train:
            return F.mean_squared_error(y, t)
        else:
            return y.data

    def reset(self):
        # 勾配の初期化とメモリの初期化
        self.cleargrads()
        self.hh.reset_state()

# 学習

EPOCH_NUM = 5000
HIDDEN_SIZE = 32
BATCH_ROW_SIZE = 100 # 分割した時系列をいくつミニバッチに取り込むか
BATCH_COL_SIZE = 100 # ミニバッチで分割する時系列数

def get_data(x,t,i):
    #教師データ
    train_x, train_t = [], []
    path = "C:/Users/odyssey8942/Documents/get_car_data/TeacherData/ (" +str(i+1)+ ").csv"
    csv_file = open(path, "r", encoding="utf_8", errors="", newline="\n" )
    f = csv.reader(csv_file, delimiter=" ", doublequote=True, lineterminator="\r\n", quotechar='"', skipinitialspace=True)

    #教師データ変換
    for row in f:
        train_x.append(row[6:8])
        train_t.append(row[14:15])

    del train_x[len(train_x)-10:]
    del train_x[:len(train_x)-20]
    del train_t[:len(train_t)-20]

    train_x = np.array(train_x, dtype="float32")
    train_t = np.array(train_t, dtype="float32")

    return train_x,train_t

in_size = 2
out_size = 1

# モデルの定義
model = LSTM(in_size=in_size, hidden_size=HIDDEN_SIZE, out_size=out_size)
optimizer = optimizers.Adam()
optimizer.setup(model)

# 学習開始
print("Train")
st = dt.datetime.now()
for epoch in range(EPOCH_NUM):
    x,t = [],[]
    x,t = get_data(x,t,epoch%10)
    loss = 0
    total_loss = 0
    model.reset() # 勾配とメモリの初期化
    print(epoch%10)
    for i in range(len(x)):
        if(math.isnan(x[i][0])):
            x[i][0] = 0.0
        if(math.isnan(x[i][1])):
            x[i][1] = 0.0
        #print(x[i][0],x[i][1])
        #print(i,x[i])
        loss += model(x=np.array(x[i],dtype="float32").reshape(1,2), t=np.array(t[i],dtype="float32").reshape(1,1), train=True)
    loss.backward()
    loss.unchain_backward()
    total_loss += loss.data
    optimizer.update()
    if (epoch+1) % 100 == 0:
        ed = dt.datetime.now()
        print("epoch:\t{}\ttotal loss:\t{}\ttime:\t{}".format(epoch+1, total_loss, ed-st))
        st = dt.datetime.now()

# 予測

print("\nPredict")
predict = np.empty(0)
test_x = []
test_t = []
test_path = "C:/Users/odyssey8942/Documents/get_car_data/TeacherData/ (4990).csv"

csv_file = open(test_path, "r", encoding="utf_8", errors="", newline="\n" )
f = csv.reader(csv_file, delimiter=" ", doublequote=True, lineterminator="\r\n", quotechar='"', skipinitialspace=True)
flag = 0

for row in f:
    test_x.append(row[6:8])
    test_t.append(row[14:15])
del test_x[0:10]
del test_t[0:10]
test_x = np.array(test_x, dtype="float32")
test_t = np.array(test_t, dtype="float32")

for x in test_x:
    x=x.reshape(1,2)
    if(math.isnan(x[0][0])):
        x[0][0] = 0
    if(math.isnan(x[0][1])):
        x[0][1] = 0
    y = model(x=x,train=False)
    predict = np.append(predict, y)

N = len(test_t)
plt.plot(range(N), test_t, color="red", label="t")
plt.plot(range(N), predict, color="blue", label="y")
plt.legend(loc="upper left")
plt.show()
"""
# 予測

print("\nPredict")
predict = np.empty(0) # 予測時系列
inseq_size = 50
inseq = train_data[:inseq_size] # 予測直前までの時系列
for _ in range(N - inseq_size):
    model.reset() # メモリを初期化
    for i in inseq: # モデルに予測直前までの時系列を読み込ませる
        x = np.array([[i]], dtype="float32")
        print(x.shape)
        y = model(x=x, train=False)
    predict = np.append(predict, y) # 最後の予測値を記録
    # モデルに読み込ませる予測直前時系列を予測値で更新する
    inseq = np.delete(inseq, 0)
    inseq = np.append(inseq, y)

plt.plot(range(N+1), train_data, color="red", label="t")
plt.plot(range(inseq_size+1, N+1), predict, color="blue", label="y")
plt.legend(loc="upper left")
plt.show()
#"""


import wx
from wx.lib import sized_controls
from pprint import pprint as pp
from pydispatch import dispatcher
from include.Base import Base
from include.EditMenu import EditMenu

import include.sb_settings as sb_settings
sbc = sb_settings.sbc

class ComboBoxDialog(sized_controls.SizedDialog, Base, EditMenu):

	def __init__(self, parent,   title, data, default):
		Base.__init__(self, parent)
		super(ComboBoxDialog, self).__init__(parent, title=title,  pos=wx.DefaultPosition)

		pane = self.GetContentsPane()

		h_pane = sized_controls.SizedPanel(pane)
		h_pane.SetSizerType('horizontal')
		h_pane.SetSizerProps(align='center')
		wx.StaticText(h_pane, -1, "Font size:", (50, 50), (75, 18))

		self.cb=cb = wx.ComboBox(h_pane, 500, default, (90, 50),
						 (160, -1), data,
						 wx.CB_DROPDOWN
						 #| wx.TE_PROCESS_ENTER
						 #| wx.CB_SORT
						 )
		b_ok = wx.Button(h_pane, wx.ID_OK, label='Select')
		#b_ok.Bind(wx.EVT_BUTTON, self.onSelect)
		self.gen_bind(wx.EVT_BUTTON, b_ok, self.onSelect, ())
		b_sel = wx.Button(h_pane, wx.ID_CANCEL, label='Cancel')
		#b_sel.Bind(wx.EVT_BUTTON, self.onSelect)
		self.gen_bind(wx.EVT_BUTTON, b_sel, self.onSelect, ())

		self.Fit()
		EditMenu.__init__(self, globals())
	def GetValue(self):
		return self.cb.GetValue()
	def onSelect(self, event, params):
		if self.IsModal():
			result= event.EventObject.Id

			self.EndModal(result)
		else:
			self.Close(result)

if __name__ == '__main__':
	app = wx.App(False)
	dlg = FontSizeDialog(None, title='Layout')
	result = dlg.ShowModal()
	if result == wx.ID_OK:
		print('OK')
	elif result == wx.ID_CANCEL:
		print('Cancel 1')
	dlg.Destroy()
	app.MainLoop()


import numpy as np
from mapf.Grid import *
from mapf.CBS import *
from gridgym.Visualiser import *
import time

def main():
	size = 16
	N = 5
	grid = np.random.rand(size, size) < 0.3

	env = Grid(grid)

	startend = get_positions(grid, 2*N)
	starts = [tuple(start) for start in startend[:N,:]]
	ends = [tuple(end) for end in startend[N:,:]]

	paths = cbs(env, starts, ends)

	visualiser = Visualiser()

	if paths is None:
		visualiser.render(grid=grid, positions=np.array(starts), goals=np.array(ends))
	else:
		for i in range(len(paths)):
			if paths[i] is None:
				paths[i] = [starts[i]]
		maxlength = max([len(path) for path in paths])
		for i in range(maxlength):
			positions = np.zeros((len(paths),2)).astype(int)
			for j in range(len(paths)):
				if i < len(paths[j]):
					positions[j,:] = paths[j][i]
				else:
					positions[j,:] = paths[j][-1]
			goals = np.array(ends)
			visualiser.render(grid=grid, positions=positions, goals=goals)
			time.sleep(0.3)

	import pdb; pdb.set_trace()

def get_positions(grid, N):
	valid_i, valid_j = np.where(~grid)
	idxs = np.random.choice(len(valid_i), N, replace=False)
	pos_i = valid_i[idxs]
	pos_j = valid_j[idxs]
	return np.stack((pos_i, pos_j), axis=1)

if __name__ == '__main__':
	main()


"""
MDB.

https://github.com/GII/MDB
"""

# Standard imports
import importlib

# Library imports
import rospy
from rospy.numpy_msg import numpy_msg

class Node(object):
    """An MDB element."""

    def __init__(
        self, ident=None, node_type=None, threshold=0.1, ltm=None, ros_node_prefix=None, ros_data_prefix=None, **kwargs
    ):
        """Init attributes when a new object is created."""
        self.ident = ident
        self.type = node_type
        self.perception = None
        self.activation = 0.0
        self.threshold = threshold
        self.neighbors = []
        self.ltm = ltm
        self.ros_node_prefix = ros_node_prefix
        self.ros_data_prefix = ros_data_prefix
        self.init_ros()

    @staticmethod
    def class_from_classname(class_name):
        """Return a class object from a class name."""
        module_string, _, class_string = class_name.rpartition(".")
        node_module = importlib.import_module(module_string)
        node_class = getattr(node_module, class_string)
        return node_class

    def init_ros(self):
        """Create publishers and make subscriptions."""
        if self.ros_node_prefix is not None:
            self.node_topic = rospy.get_param(self.ros_node_prefix + "_topic")
            self.node_message = self.class_from_classname(rospy.get_param(self.ros_node_prefix + "_msg"))
            self.node_publisher = rospy.Publisher(self.node_topic, self.node_message, latch=True, queue_size=0)
        if self.ros_data_prefix is not None:
            self.data_topic = rospy.get_param(self.ros_data_prefix + "_topic")
            self.data_message = numpy_msg(self.class_from_classname(rospy.get_param(self.ros_data_prefix + "_msg")))
            self.data_publisher = rospy.Publisher(self.data_topic, self.data_message, latch=True, queue_size=0)
        self.publish(first_time=True)

    def __getstate__(self):
        """Return the object to be serialize with PyYAML as the result of removing the unpicklable entries."""
        state = self.__dict__.copy()
        if self.ros_node_prefix is not None:
            del state["node_publisher"]
        if self.ros_data_prefix is not None:
            del state["data_publisher"]
        return state

    def publish(self, message=None, first_time=False):
        """Publish node information."""
        if not message:
            message = self.node_message()
        if first_time:
            message.command = "new"
        else:
            message.command = "update"
        message.id = self.ident
        message.neighbor_ids = [node.ident for node in self.neighbors]
        message.neighbor_types = [node.type for node in self.neighbors]
        if isinstance(self.activation, list):
            message.activation = max(self.activation)
        else:
            message.activation = self.activation
        message.execute_service = ""
        message.get_service = ""
        message.class_name = ""
        message.language = ""
        self.node_publisher.publish(message)

    def calc_activation(self, perception=None):
        """Calculate the new activation value."""
        raise NotImplementedError

    def update_activation(self, **kwargs):
        """Calculate a new activation value for each perception."""
        perception = kwargs.pop("perception")
        self.perception = []
        self.activation = []
        for i in range(max([len(sensor) for sensor in perception.values()])):
            perception_line = {}
            for sensor, value in perception.items():
                sid = i % len(value)
                perception_line[sensor + str(sid)] = value[sid]
            self.perception.append(perception_line)
            activation_value = self.calc_activation(perception_line)
            if activation_value < self.threshold:
                activation_value = 0.0
            self.activation.append(activation_value)
            rospy.logdebug(self.type + " activation for " + self.ident + " = " + str(activation_value))
        self.publish()


from flask import Flask, render_template, url_for
import RPi.GPIO as GPIO

app = Flask(__name__)

outputPin = [17, 18, 14, 4]
inputPin = outputPin

@app.route("/toggle/<pin>")
def toggle(pin):
	pin = int(pin)
	if GPIO.input(outputPin[pin]) == GPIO.HIGH:
		GPIO.output(outputPin[pin], GPIO.LOW)
	else:
		GPIO.output(outputPin[pin], GPIO.HIGH)
	return 'ok'

@app.route("/turnOnAll")
def turnAllLightOn():
	for x in range(len(outputPin)):
		GPIO.output(outputPin[x], GPIO.HIGH)
	return 'ok'

@app.route("/turnOffAll")
def turnAllLightOff():
	for x in range(len(outputPin)):
		GPIO.output(outputPin[x], GPIO.LOW)
	return 'ok'

@app.route("/getdata")
def getdata():
	pin = []
	for x in range(len(outputPin)):
		if GPIO.input(outputPin[x]) == GPIO.HIGH :
			pin.append(x)
	return json.dumps(pin)

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)

for x in range(len(outputPin)):
	GPIO.setup(outputPin[x], GPIO.OUT)

if __name__ == '__main__':
	turnAllLightOn()
	app.run(debug=True)

#21, 20 input switch


# For a given grid and item name it finds all the items in it
# Return the locations
def get_grid_items(grid, item):
    items = []
    for row in range(0, grid.shape[0]):
        for col in range(0, grid.shape[1]):
            if grid[row, col] == item:
                items.append([row, col])
    return items


# coding: utf8
from util import *
import urllib2
import lxml.html
cur = connect_db()
url = 'http://www.4gamer.net/rss/index.xml'
response = urllib2.urlopen(url)
html = lxml.html.fromstring(response.read())
items = html.xpath('//item')
data = []
for item in items:
    title = item.xpath('title')[0].text
    description = item.xpath('description')[0].text
    date = item.xpath('date')[0].text
    url = lxml.etree.tostring(item.xpath("link")[0]).replace("<link/>", "").replace("\n", "")
    thumbnail = "%sTN/001.jpg"%url
    datum = (title,
            description.replace("\n", "") if description else "",
            "4Gamer.net",
            date,
            url,
            thumbnail)
    data.append(datum)
cur.executemany("INSERT IGNORE INTO article (title, description, source, pubdate, url, thumbnail) VALUES (%s, %s, %s, %s, %s, %s)", data)


"""
<Started>
  July 2013

<Author>
  Savvas Savvides <savvas@purdue.edu>

<Purpose>

"""

import sys

DEBUG = False

class ParsingClass:
  def __repr__(self):
    return "<" + self.__class__.__name__ + " " + str(self.value) + ">"

  def __str__(self):
    return str(self.value)

# This class is used to wrap all arguments for which a specific type is not yet
# implemented.
class UnimplementedType(ParsingClass):
  def __init__(self, string_args):
    self.value = string_args.pop(0)

# This object is used to indicate that a system call did not return the expected
# value. This can happen for instance when a system call has an error, in which
# case values of structures are not returned.
# Example (fstat on success and on error):
# 19243 fstat64(3, {st_mode=S_IFREG|0644, st_size=98710, ...}) = 0 19243
# fstatfs(3, 0xbff476cc) = -1 EBADF (Bad file descriptor)
#
# The equality operator is overridden and only returns true if the  other object
# is of the same type. Less than and Greater than operators are also overridden
# and immediately raise an exception if used, since no such operations are
# allowed with this object.
class MissingValue(ParsingClass):
  def __init__(self, ev, string_args):
    self.expected_value = ev
    try:
      # this can happen when a structure is not dereferenced.
      self.given_value = string_args[0]
    except IndexError:
      # This case can occur when a value is missing entirely
      # Example open definition and strace output:
      #
      # int open(const char *pathname, int flags, mode_t mode);
      # 14037 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
      #
      # Note how the parser expects a third argument for mode.
      self.given_value = None

  # override equality
  def __eq__(self, other):
    return type(other) is type(self)

  # override inequality
  def __ne__(self, other):
    return not self.__eq__(other)

  # override less than
  def __lt__(self, other):
    raise Exception("Comparing object of type " + type(other) +
                    " with " + self.__class__.__name__ + " object.")

  # greater than behaves exactly like less than.
  def __gt__(self, other):
    return self.__lt__(other)

  # override representation and string
  def __repr__(self):
    return "<" + self.__class__.__name__ + " expected_value: " + \
           str(self.expected_value) + ", given_value: " + \
           str(self.given_value) + ">"

class Int(ParsingClass):
  def __init__(self, string_args):
    temp_value = string_args.pop(0)

    # it is possible that a number is enclosed in square brackets '[]'. This
    # notation is used by strace to indicate pointer variables eg socklen_t
    # *addrlen
    if temp_value.startswith("[") and temp_value.endswith("]"):
      # remove the square brackets
      temp_value = temp_value[1:-1]

    try:
      temp_value = int(temp_value)
    except ValueError:
      raise Exception("Unexpected format when parsing Int:", temp_value)

    self.value = temp_value

class FileDescriptor(ParsingClass):
  def __init__(self, string_args):
    fd = string_args.pop(0)
    try:
      fd = int(fd)
    except ValueError:
      raise Exception("Unexpected format when parsing FileDescriptor:", fd)

    self.value = fd

class Filepath(ParsingClass):
  def __init__(self, string_args):
    self.value = string_args.pop(0)

class Flags(ParsingClass):
  def __init__(self, string_args):
    self.value = _string_to_flags(string_args.pop(0))

class SockFamily(ParsingClass):
  """
  A SockFamily object can only appear as part of the Sockaddr object.
  """
  def __init__(self, value):
    if "sa_family=" not in value:
      raise Exception("Unexpected argument when parsing SockFamily object: " +
                      value)

    # get the part that comes after the 'sa_family=" label
    value = value[value.find("sa_family=")+10:]

    # a basic test.
    if not value.startswith("AF_") and not value.startswith("PF_"):
      raise Exception("Unknown Socket family: " + value)
    self.value = value

class SockPort(ParsingClass):
  """
  A SockPort object can only appear as part of the Sockaddr object.

  sin_port=htons(25588)
  """
  def __init__(self, value):
    if "sin_port=htons(" not in value:
      raise Exception("Unexpected argument when parsing SockPort object: " +
                      value)

    # get the part that comes between "sin_port=htons(" and ")". The remaining
    # value should be a number.
    try:
      value = int(value[value.find("sin_port=htons(")+15:value.rfind(")")])
    except:
      raise Exception("Unexpected argument when parsing SockPort object " +
                      value)

    self.value = value

class SockIP(ParsingClass):
  """
  A SockPort object can only appear as part of the Sockaddr object.

  sin_addr=inet_addr("127.0.0.1")
  """
  def __init__(self, value):
    if "sin_addr=inet_addr(\"" not in value:
      raise Exception("Unexpected argument when parsing SockIP object: " +
                      value)

    # get the part that comes between sin_addr=inet_addr(" and ").
    try:
      value = value[value.find("sin_addr=inet_addr(\"")+20:value.rfind("\")")]
    except:
      raise Exception("Unexpected argument when parsing SockIP object " + value)

    # Let's check if the value we have is indeed an IP address.
    import socket
    try:
      socket.inet_aton(value)
    except socket.error:
      raise Exception("Value is not a valid IP address: " + value)

    self.value = value

class SockPath(ParsingClass):
  """
  A SockPath object can only appear as part of the Sockaddr object.
  """

  def __init__(self, value):
    # three types of address paths (see "man 7 unix" for more information)
    #
    # unnamed:
    # 14039 getsockname(3, {sa_family=AF_FILE, NULL}, [2]) = 0
    # pathname:
    # 14037 connect(4, {sa_family=AF_FILE, path="/var/run/nscd/socket"}, 110) = -1 ENOENT
    # abstract:
    # 14037 connect(6, {sa_family=AF_FILE, path=@"/tmp/.X11-unix/X0"}, 20 )= 0
    if value == "NULL":
      self.type = "unnamed"
      self.value = value
    elif value.startswith("path=@\""):
      self.type = "pathname"
      self.value = value[value.find("path=@\"")+7:value.rfind("\"}")]
    elif value.startswith("path="):
      self.type = "abstract"
      self.value = value[value.find("path=\"")+6:value.rfind("\"}")]
    else:
      raise Exception("Unexpected value when parsing SockPath object: " + value)

  def __repr__(self):
    return "<" + self.__class__.__name__ + " type: " + str(self.type) + \
           " value: " + str(self.value) + ">"

class Sockaddr(ParsingClass):
  def __init__(self, string_args):
    self.value = None

    """
    Examples showing strace output of syscalls including the sockaddr structure:

    14039 getsockname(3, {sa_family=AF_FILE, NULL}, [2]) = 0
    14039 connect(3, {sa_family=AF_FILE, path=@"/tmp/.X11-unix/X0"}, 20)
    19176 bind(3, {sa_family=AF_INET, sin_port=htons(25588), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
    """

    # if the system call has an error, its structures are not dereferenced so we
    # get the hex number of the memory location of the structure. In this case
    # we return immediately leaving the self.value of the object to None, so
    # that we can later identify this condition.
    if string_args[0].startswith("0x"):
      return

    # let's consume all the string_args that belong to the sockaddr
    # structure.
    sockaddr_args = []
    sockaddr_args.append(string_args.pop(0))

    # the first argument of sockaddr should start with a '{'
    assert sockaddr_args[0].startswith("{"), "First argument of sockaddr " + "structure does not start with a '{'"

    # and the last one should end with a '}'
    while True:
      sockaddr_args.append(string_args.pop(0))

      if sockaddr_args[-1].endswith("}"):
        break

    # Lets use these arguments to construct the value of the Sockaddr object.
    self.value = []

    # first item must show the sock address family
    sa_family = sockaddr_args.pop(0)
    self.value.append(SockFamily(sa_family))

    if "_FILE" in sa_family:
      # sockaddr should include a path.
      # 14037 connect(6, {sa_family=AF_FILE, path=@"/tmp/.X11-unix/X0"}, 20 )= 0
      self.value.append(SockPath(sockaddr_args.pop(0)))

      # there should be no more items in the sockaddr_args list
      assert len(sockaddr_args) == 0, "Additional arguments found when " + "parsing SockPath of Sockaddr object: " + str(sockaddr_args)
    else:
      # sockaddr should include IP and port
      # 7123  bind(3, {sa_family=AF_INET, sin_port=htons(25588),
      #                        sin_addr=inet_addr("127.0.0.1")}, 16) = 0
      self.value.append(SockPort(sockaddr_args.pop(0)))
      self.value.append(SockIP(sockaddr_args.pop(0)))

      # there should be no more items in the sockaddr_args list
      assert len(sockaddr_args) == 0, "Additional arguments found when " + "parsing IP and port of Sockaddr object: " + str(sockaddr_args)

def _string_to_flags(flags_string):
  """
  Transforms a string to a list of flags.
  """
  flags_list = []

  # if no flags are set a zero is given. Return an empty list to indicate this.
  if flags_string == '0':
    return flags_list

  # A a list of flags can also contain a mode in its numeric value.
  #
  # Example:
  # 19243 fstat64(3, {st_mode=S_IFREG|0644, st_size=98710, ...}) = 0
  #
  # go through all flags and translate numeric mode to flags.
  for flag in flags_string.split("|"):
    if flag.isdigit():
      # if number it must be a mode. translate it to flags.
      flags_list += _mode_to_flags(int(flag))

    else:
      flags_list.append(flag)

  return flags_list

def _mode_to_flags(mode):
  """
  Transforms a number representing a mode to a list of flags.
  """
  mode = int(str(mode).strip(0))

  mode_flags = {
    777: 'S_IRWXA', 700: 'S_IRWXU', 400: 'S_IRUSR', 200: 'S_IWUSR',
    100: 'S_IXUSR', 70: 'S_IRWXG', 40: 'S_IRGRP', 20: 'S_IWGRP',
    10: 'S_IXGRP', 7: 'S_IRWXO', 4: 'S_IROTH', 2: 'S_IWOTH',
    1: 'S_IXOTH'}

  list_of_flags = []
  # check for all permissions.
  if mode == 777:
    list_of_flags.append(mode_flags[777])
  else:
    # deal with each entity (digit) at a time (user then group
    # then other)
    mode_string = str(mode)
    # used to translate eg 7 to 700 for user.
    entity_position = 10**(len(mode_string)-1)
    for mode_character in str(mode):
      try:
        entity = int(mode_character)
      except ValueError:
        raise Exception("Unexpected mode format: " + str(mode))

      # is the mode in the correct format?
      if entity < 0 or entity > 7:
        raise Exception("Unexpected mode format: " + str(mode))

      # check if current entity has all permissions
      if entity == 7:
        list_of_flags.append(mode_flags[entity*entity_position])
      else:
        # check entity for each flag.
        for flag in mode_flags:
          if flag > 7*entity_position or flag < entity_position:
            continue
          compare = int(str(flag).strip('0'))
          if entity & compare == compare:
            list_of_flags.append(mode_flags[flag])

      entity_position /= 10
  return list_of_flags

def _get_parsing_class(syscall_name, definition_parameter):
  """
  Examine the definition type to figure out which class should be used to
  describe this argument.
  """
  if definition_parameter.type == "char" and definition_parameter.pointer:
    # char pointer type
    if "path" in definition_parameter.name or "filename" in definition_parameter.name:
      # argument is a file path.
      return Filepath

  elif definition_parameter.type == "int" or definition_parameter.type.endswith("_t"):
    # number type
    if "fd" in definition_parameter.name:
      # argument is a file descriptor
      return FileDescriptor

    elif ("flag" in definition_parameter.name or
         "mode" in definition_parameter.name or
         "prot" in definition_parameter.name or
         "domain" in definition_parameter.name):
      # argument is a set of flags.
      return Flags

    elif ("socket" in syscall_name and definition_parameter.type == "int" and
          "type" in definition_parameter.name):
      return Flags

    else:
      return Int

  elif definition_parameter.type == "sockaddr":
    # argument is a sockaddr
    return Sockaddr

  sys.stderr.write("No CLASS for: '" + definition_parameter.type + " " +
                   definition_parameter.name + "' created yet :(\n")

  return UnimplementedType

def _cast_syscall_arg(syscall_name, definition_parameter, string_args):
  # if the string_args list is empty, then the value is missing.
  if len(string_args) == 0:
    return MissingValue(definition_parameter, string_args)

  parsing_class = _get_parsing_class(syscall_name, definition_parameter)

  arg = parsing_class(string_args)

  if arg.value == None:
    # if the value of the argument is None, it means that the expected value was
    # not found. This can occur when a system call has an error in which case
    # structure and pointer values are not dereferenced, and instead we get the
    # hex value of its memory location.
    # Example:
    # 14037 recv(6, 0xb7199058, 4096, 0)      = -1 EAGAIN
    return MissingValue(definition_parameter, string_args)

  return arg

def cast_args(syscall_name, syscall_type, syscall_definitions, string_args):
  # we will consume these args (pop them off the list) so let's make a fresh
  # copy of them to avoid messing with the original list.
  string_args = string_args[:]

  if DEBUG:
    print("Syscall Name:", syscall_name)

  # find the syscall definition for this syscall.
  syscall_definition = None
  for sd in syscall_definitions:
    if syscall_name == sd.name:
      syscall_definition = sd
      break

  casted_args = []
  for definition_parameter in syscall_definition.definition.parameters:
    if DEBUG:
      print("Definition Parameter:", definition_parameter)

    # for system calls that are unfinished we don't need to consider the
    # definition arguments beyond the number of arguments the unfinished syscall
    # includes. If this was not here, for every definition parameter for which
    # an argument is not provided in the unfinished system call a MissingValue
    # object would be used. This is not a bad idea (could even be usefull in
    # some cases) but it is a bit cleaner if we just ignore the missing
    # arguments.
    if syscall_type == "unfinished" and len(string_args) == 0:
      break

    ca = _cast_syscall_arg(syscall_name, definition_parameter, string_args)
    casted_args.append(ca)

  # Since not all arguments have a type corresponding to them (yet), and
  # because some argument values are part of the same syscall parameter (eg
  # structures may have multiple values) it is possible that there are still
  # arguments in the string_args list that are not consumed. Let's wrap them
  # in an UnimplementedType class and append them at the end of the
  # casted_args list.
  while len(string_args) > 0:
    # remember that casting to a class automatically consumes items from the
    # given list
    casted_args.append(UnimplementedType(string_args))

  casted_args = tuple(casted_args)

  return casted_args


import const as c
import evaluate, deevaluate, error
import pdb

def summarize_results(rules):
	rules = rules.items()
	to_ret = None
	for ru, val in rules:
		if to_ret == None:
			to_ret = (ru, val)
		else:
			if to_ret[1] in [c.TRUE, c.FALSE]:
				if val in [c.TRUE, c.FALSE] and val != to_ret[1]:
					raise error.ConflictingRules(ru, to_ret[0])
					break
			elif val in [c.TRUE, c.FALSE, c.UNDEFINED]:
				to_ret = (ru, val)
	return to_ret[1]

class Fact:
	def evaluate(self, facts):
		if self.evaluated:
			return self.value
		elif self.is_busy:
			return c.UNDEFINED
		else:
			self.is_busy = True
			true_rules = {}
			if len(self.depends_on) > 0:
				for rule in self.depends_on:
					if evaluate.exec_rpn(rule.condition, facts) == c.TRUE:
						true_rules[rule] = deevaluate.reverse_rpn(rule.implication, facts, self.name)
				self.is_busy = False
				if len(true_rules) > 0:
					self.value = summarize_results(true_rules)
					return self.value
				else:
					return self.value
			else:
				self.is_busy = False
				self.evaluated = True
				return self.value

	def __init__(self, name):
		self.name = name
		self.value = c.DEFAULT
		self.depends_on = []
		self.is_busy = False
		self.evaluated = False


#!/opt/libreoffice5.2/program/python
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
from helper import Elem, ElemProp, ElemPropLoc
from config import getConfig
import os
class ElemLeaf(Elem):  # node-type=Leaf。オプションページのロードマップコントロールの小項目になる。
	def __init__(self, c, attrs):  # 辞書attrのkeyは"Name"	, "Label", "GroupId", "GroupIndex", "Id"。 valueはすべて文字列でいれる。
		if not "Id" in attrs:  # IdがなければNameを代用する。Idを拡張機能Idと同じにすると拡張機能マネージャーでオプションボタンが表示される。
			attrs["Id"] = attrs["Name"]
		attrs["OptionsPage"] = "%origin%/dialogs/optionsdialog.xdl"  # オプションダイアログxdlファイル名。UnoControlDialogオブジェクトになる。
		attrs["EventHandlerService"] = c["ExtentionID"]  # オプションダイアログの呼び出しと共に呼び出す実装サービス名。
		super().__init__("node", {'oor:name': attrs.pop("Name"), "oor:op": "fuse"})
		for key, val in attrs.items():
			if key=="Label":
				self.append(ElemPropLoc(key, val))
			else:
				self.append(ElemProp(key, val))
class ElemNode(Elem):  # node-type=Node
	def __init__(self, c, attrs, *, leaves=None):  # 辞書attrのkeyはName, Label, OptionsPage, AllModules, GroupId, GroupIndex。 valueはすべて文字列でいれる。
		super().__init__("node", {'oor:name': attrs.pop("Name"), "oor:op": "fuse"})
		for key, val in attrs.items():
			if key=="Label":
				self.append(ElemPropLoc(key, val))
			else:
				self.append(ElemProp(key, val))
		if leaves is not None:
			self.append(Elem("node", {'oor:name': "Leaves"}))  # セットノードLeaves
			for leaf in leaves:
				self[-1].append(leaf)  #  node-type=LeafをセットノードLeavesに追加。
class ElemOrderdNode(Elem):  # node-type=OrderdNode
	def __init__(self, name, index=None):  # nodeはノード名。
		super().__init__("node", {'oor:name': name, "oor:op": "fuse"})
		if index is not None:
			self.append(ElemProp("Index", index))  # 位置の指定。省略可。
class ElemModeule(Elem):  # node-type=Module
	def __init__(self, name, nodes):  # moduleはオプションダイアログを表示させるモジュールを指定。有効にするためにはnode-type=NodeのAllModulesをfalseにする必要がある。
		super().__init__("node", {'oor:name': name, "oor:op": "fuse"})
		self.append(Elem("node", {'oor:name': "Nodes"}))  # セットノードNodes
		for node in nodes:
			self[-1].append(node)  #  node-type=OrderdNodeをセットノードNodesに追加。
class ElemSingleOption(Elem):  #  node-type=SingleOption
	def __init__(self, name):
		super().__init__("node", {'oor:name': name, "oor:op": "fuse"})
		self.append(ElemProp("Hide", "true"))
class ElemOptionsPage(Elem):  #  node-type=OptionsPage
	def __init__(self, name, *, hide=None, singleoptions=None):
		super().__init__("node", {'oor:name': name, "oor:op": "fuse"})
		if hide is not None:
			self.append(ElemProp("Hide", "true"))
		if singleoptions is not None:
			self.append(Elem("node", {'oor:name': "Options"}))  # セットノードOptions。
			for singleoption in singleoptions:
				self[-1].append(singleoption)  #  node-type=SingleOptionをセットノードOptionsに追加。
class ElemOptionsGroup(Elem):  #  node-type=OptionsGroup
	def __init__(self, name, *, hide=None, optionspages=None):
		super().__init__("node", {'oor:name': name, "oor:op": "fuse"})
		if hide is not None:
			self.append(ElemProp("Hide", "true"))
		if optionspages is not None:
			self.append(Elem("node", {'oor:name': "Pages"}))  # セットノードPages。
			for optionspage in optionspages:
				self[-1].append(optionspage)  #  node-type=OptionsPageをセットノードPagesに追加。
def createOptionsDialogXcu(c):  #Creation of OptionsDialog.xcu
	os.chdir(c["src_path"])  # srcフォルダに移動。
	filename =  "OptionsDialog.xcu"
	c["backup"](filename)  # すでにあるファイルをバックアップ
	with open(filename, "w", encoding="utf-8") as f:  # OptionsDialog.xcuファイルの作成
		root = Elem("oor:component-data", {"oor:name": "OptionsDialog", "oor:package": "org.openoffice.Office", "xmlns:oor": "http://openoffice.org/2001/registry", "xmlns:xs": "http://www.w3.org/2001/XMLSchema", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"})  # 根の要素を作成。

		#オプションページの作成。拡張機能IDのc["ExtentionID"]をNameに使った時はLeafを一つにしないと拡張機能マネージャーでフリーズする。
		root.append(Elem("node", {'oor:name': "Nodes"}))  # セットノードNodesを追加。
		leaf = ElemLeaf(c, {"Name": c["ExtentionID"], "Label": {"en-US": "Maximum Paper Size", "ja-JP": "最大用紙サイズ"}})  # node-type=Leaf。小項目の設定。Idを拡張機能Idと同じc["ExtentionID"]にする。
		name = "{}.Node1".format(c["ExtentionID"])  # ロードマップコントロールに表示させる大項目のID。ユニークの必要があると考えるので拡張機能IDにくっつける。
		node = ElemNode(c, {"Name": name, "Label": {"en-US": "Extensions Example", "ja-JP": "拡張機能の例"}, "AllModules": "false"}, leaves=(leaf,))  # node-type=Node。大項目の設定。
		root[-1].append(node)  #  node-type=NodeをセットノードNodesに追加。

		# オプションページを表示させるモジュールを限定する。
		root.append(Elem("node", {'oor:name': "Modules"}))  # セットノードModulesを追加。
		orderednode = ElemOrderdNode(name)  #  node-type=OrderdNode。node-type=NodeのNameで表示させる大項目を指定。
		node = ElemModeule("com.sun.star.text.TextDocument", (orderednode,))  # node-type=Module。表示させるモジュールを指定。第一引数はhttps://wiki.openoffice.org/wiki/Framework/Article/Options_Dialog_Configurationのいずれか。
		root[-1].append(node)  #  node-type=ModuleをセットノードModulesに追加。

		# 一部のオプションページを非表示にする。
		root.append(Elem("node", {'oor:name': "OptionsDialogGroups"}))  # セットノードOptionsDialogGroupを追加。
		security = ElemOptionsPage("Security", hide=True)  #  node-type=OptionsPage。非表示にする小項目。セキュリティー。
		memory = ElemOptionsPage("Memory", hide=True)  #  node-type=OptionsPage。非表示にする小項目。メモリー。
		productname = ElemOptionsGroup("ProductName", optionspages=(security, memory))  #  node-type=OptionsGroup。非表示にする大項目。hide=Trueにしていないときは第2引数の小項目のみ非表示にする。
		root[-1].append(productname)  #  node-type=OptionsGroupをセットノードOptionsDialogGroupsに追加。
		internet = ElemOptionsGroup("Internet", hide=True)  #  node-type=OptionsGroup。非表示にする大項目。Internet。
		root[-1].append(internet)  #  node-type=OptionsGroupをセットノードOptionsDialogGroupsに追加。

		tree = ET.ElementTree(root)  # 根要素からxml.etree.ElementTree.ElementTreeオブジェクトにする。
		tree.write(f.name, "utf-8", True)  # xml_declarationを有効にしてutf-8でファイルに出力する。
	print("{} has been created.".format(filename))
if __name__ == "__main__":
	createOptionsDialogXcu(getConfig(False))  # バックアップをとるときはTrue


### Bokeh Plotting for PocketNC ###

import socket
import numpy as np
import threading
import sys
import pickle

from bokeh.plotting import figure, curdoc
from bokeh.driving import linear
from bokeh.models import SingleIntervalTicker, LinearAxis
import random

# Globals
lastNdxReceived = 0
#socket_pnc = 0
pncCommHost = '0.0.0.0'
pncCommPort = 6666
pncIntf = 0

# Data Storage
#plotData = 0
#plotData = dict(Time=[], queueLengths=[], xPosns=[])
pncData = 0

class pncCommListener(threading.Thread):
    def __init__(self, conn):
        super(pncCommListener, self).__init__()
        #self.tcqPlotData = []
        self.needUpdate = 0
        self.conn = conn
        self.data = ""
        print('pncComm thread started')

    def run(self):
        while True:
            (bytes_received, rec_address) = self.conn.recvfrom(65536)
            #dataArray = pickle.loads(bytes_received)
            dataDict = pickle.loads(bytes_received)
            #print(repr(dataDict))
            self.data = dataDict
            self.needUpdate = 1
            #string_received = bytes_received.decode("utf-8")
            #print(string_received)
            #self.data = self.data + dataArray

            #if self.data.endswith(u"&"):
            #    print('Received complete string', string_received)
            #    #process_data_points(self.data)
            #    self.data = ""
            #    self.needUpdate = 1

    def initPlots(self):
        print('initializing plots')

    def send_msg(self,msg):
        self.conn.send(msg)

    def close(self):
        self.conn.close()

def init():
    global pncIntf#, pncSocket, pncCommHost, pncCommPort
    try:
        #establish reader from pncComm
        pncSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        #pncSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        pncSocket.bind((pncCommHost,pncCommPort))
    except socket.error:
        print ('Failed to create feedback socket')
        #pncSocket.close()
        sys.exit()

    pncIntf = pncCommListener(pncSocket)
    pncIntf.start()

#update_lock = threading.Lock()
queueLength = figure(title="Buffer Fill Level", plot_width=800, plot_height=400)
r1 = queueLength.line([], [], color="firebrick", line_width=2)

threadTime = figure(title="Z-Axis Encoder", plot_width=800, plot_height=400)
r2 = threadTime.line([], [], color="firebrick", line_width=2)
ticker = SingleIntervalTicker(interval=5, num_minor_ticks=10)
xaxis = LinearAxis(ticker=ticker)
yaxis = LinearAxis(ticker=ticker)

ds1 = r1.data_source
ds2 = r2.data_source

@linear()
def update(step):
    #print(step)
    if pncIntf.needUpdate:
        #print('need to update plots')
        queue_data = pncIntf.data['TCQ']
        encoder_data = pncIntf.data['Encoder']
        #encoder_len = len(encoder_data)

        pncIntf.needUpdate = 0
        ds1.data = dict(x = np.arange(len(queue_data)), y = queue_data)
        ds1.trigger('data', ds1.data, ds1.data)
        ds2.data = dict(x = np.arange(len(encoder_data)), y = encoder_data)
        ds2.trigger('data', ds2.data, ds2.data)

    #ds1.trigger('data', ds1.data, ds1.data)
    #ds2.trigger('data', ds2.data, ds2.data)

curdoc().add_root(queueLength)
curdoc().add_root(threadTime)
init()

# Add a periodic callback to be run every 500 milliseconds
curdoc().add_periodic_callback(update, 100)
curdoc().title = "PocketNC Interface"


################################################################################
# COLUMNS_URANUS.py: Column definitions for Uranus geometry tables
################################################################################

PLANET = "URANUS"
PLANET_RING = PLANET + ":RING"
PLANET_ANSA = PLANET + ":ANSA"
PLANET_LIMB = PLANET + ":LIMB"

################################################################################
# Create a list of body IDs
################################################################################

planet_body = oops.Body.lookup(PLANET)
moon_bodies = planet_body.select_children("REGULAR")

MOON_NAMES   = [moon.name for moon in moon_bodies]
SYSTEM_NAMES = [PLANET] + MOON_NAMES
# NAME_LENGTH  = max([len(name) for name in MOON_NAMES])
NAME_LENGTH = 12

# Maintain a list of translations for target names

TRANSLATIONS = {}

################################################################################
# *COLUMN description tuples are
#
#   (backplane_key, (masker, shadower, face), alt_format)
#
# where...
#
#   backplane_key   tuple passed to Backplane.evaluate().
#
#   masker          a string indicating which bodies obscure the surface. It is
#                   constructed by concatenating any of these characters:
#                       "P" = let the planet mask the surface;
#                       "R" = let the rings mask the surface;
#                       "M" = let the moon mask the surface.
#
#   shadower        a string indicating which bodies shadow the surface. It is
#                   constructed by concatenating any of these characters:
#                       "P" = let the planet shadow the surface;
#                       "R" = let the rings shadow the surface;
#                       "M" = let the moon shadow the surface.
#
#   face            a string indicating which face of the surface to include:
#                       "D" = include only the day side of the body;
#                       "N" = include only the night side of the body;
#                       ""  = include both faces of the body.
#
#   alt_format      if present, this is an extra tag used to identify the output
#                   format of the column.
#                       "-180" = use the range (-180,180) instead of (0,360).
#
################################################################################

MOONX = "moonx"                 # Indicates a placeholder for an arbitrary moon

SKY_COLUMNS = [
    (("right_ascension",        ()),                        ("",  "",  "")),
    (("declination",            ()),                        ("",  "",  ""))]

RING_COLUMNS = [
    (("ring_radius",            PLANET_RING),               ("PM", "P", "")),
    (("resolution",             PLANET_RING, "v"),          ("PM", "P", "")),
    (("ring_radial_resolution", PLANET_RING),               ("PM", "P", "")),
    (("distance",               PLANET_RING),               ("PM", "P", "")),
    (("ring_longitude",         PLANET_RING, "aries"),      ("PM", "P", "")),
    (("ring_longitude",         PLANET_RING, "node"),       ("PM", "P", "")),
    (("ring_longitude",         PLANET_RING, "sha"),        ("PM", "",  "")),
    (("ring_longitude",         PLANET_RING, "obs", -180),  ("PM", "P", ""), "-180"),
    (("ring_azimuth",           PLANET_RING, "obs"),        ("PM", "P", "")),
    (("phase_angle",            PLANET_RING),               ("PM", "P", "")),
    (("ring_incidence_angle",   PLANET_RING),               ("PM", "P", "")),
    (("ring_incidence_angle",   PLANET_RING, "prograde"),   ("PM", "P", "")),
    (("ring_emission_angle",    PLANET_RING),               ("PM", "P", "")),
    (("ring_emission_angle",    PLANET_RING, "prograde"),   ("PM", "P", "")),
    (("ring_elevation",         PLANET_RING, "sun"),        ("PM", "P", "")),
    (("ring_elevation",         PLANET_RING, "obs"),        ("PM", "P", "")),
    (("ring_elevation",         PLANET_RING, "sun", False), ("PM", "P", "")),
    (("ring_elevation",         PLANET_RING, "obs", False), ("PM", "P", ""))]

RING_GRIDLESS_COLUMNS = [
    (("center_distance",        PLANET_RING, "obs"),        ("",   "",  "" )),
    (("ring_sub_solar_longitude",
                                PLANET_RING, "aries"),      ("",   "",  "" )),
    (("ring_sub_observer_longitude",
                                PLANET_RING, "aries"),      ("",   "",  "" )),
    (("ring_sub_solar_longitude",
                                PLANET_RING, "node"),       ("",   "",  "" )),
    (("ring_sub_observer_longitude",
                                PLANET_RING, "node"),       ("",   "",  "" )),
    (("center_phase_angle",     PLANET_RING),               ("",   "",  "" )),
    (("ring_center_incidence_angle",
                                PLANET_RING),               ("",   "",  "" )),
    (("ring_center_incidence_angle",
                                PLANET_RING, "prograde"),   ("",   "",  "" )),
    (("ring_center_emission_angle",
                                PLANET_RING),               ("",   "",  "" )),
    (("ring_center_emission_angle",
                                PLANET_RING, "prograde"),   ("",   "",  "" )),
    (("sub_solar_latitude",     PLANET_RING),               ("",   "",  "" )),
    (("sub_observer_latitude",  PLANET_RING),               ("",   "",  "" ))]

ANSA_COLUMNS = [
    (("ansa_radius",            PLANET_ANSA),               ("PM", "P", "")),
    (("ansa_altitude",          PLANET_ANSA),               ("PM", "P", "")),
    (("ansa_radial_resolution", PLANET_ANSA),               ("PM", "P", "")),
    (("distance",               PLANET_ANSA),               ("PM", "P", "")),
    (("ansa_longitude",         PLANET_ANSA, "aries"),      ("PM", "P", "")),
    (("ansa_longitude",         PLANET_ANSA, "node"),       ("PM", "P", "")),
    (("ansa_longitude",         PLANET_ANSA, "sha"),        ("PM", "P", ""))]

PLANET_COLUMNS = [
    (("latitude",               PLANET, "centric"),         ("M", "",  "D")),
    (("latitude",               PLANET, "graphic"),         ("M", "",  "D")),
    (("longitude",              PLANET, "iau", "west"),     ("M", "",  "D")),
    (("longitude",              PLANET, "iau", "east"),     ("M", "",  "D")),
    (("longitude",              PLANET, "sha", "east"),     ("M", "",  "" )),
    (("longitude",              PLANET, "obs", "west", -180),
                                                            ("M", "",  "D"), "-180"),
    (("longitude",              PLANET, "obs", "east", -180),
                                                            ("M", "",  "D"), "-180"),
    (("finest_resolution",      PLANET),                    ("M", "",  "D")),
    (("coarsest_resolution",    PLANET),                    ("M", "",  "D")),
    (("distance",               PLANET),                    ("M", "",  "" )),
    (("phase_angle",            PLANET),                    ("M", "",  "" )),
    (("incidence_angle",        PLANET),                    ("M", "",  "" )),
    (("emission_angle",         PLANET),                    ("M", "",  "" ))]

PLANET_GRIDLESS_COLUMNS = [
    (("sub_solar_latitude",     PLANET, "centric"),         ("",   "",  "" )),
    (("sub_solar_latitude",     PLANET, "graphic"),         ("",   "",  "" )),
    (("sub_observer_latitude",  PLANET, "centric"),         ("",   "",  "" )),
    (("sub_observer_latitude",  PLANET, "graphic"),         ("",   "",  "" )),
    (("sub_solar_longitude",    PLANET, "iau", "west"),     ("",   "",  "" )),
    (("sub_solar_longitude",    PLANET, "iau", "east"),     ("",   "",  "" )),
    (("sub_observer_longitude", PLANET, "iau", "west"),     ("",   "",  "" )),
    (("sub_observer_longitude", PLANET, "iau", "east"),     ("",   "",  "" )),
    (("center_resolution",      PLANET, "u"),               ("",   "",  "" )),
    (("center_distance",        PLANET, "obs"),             ("",   "",  "" )),
    (("center_phase_angle",     PLANET),                    ("",   "",  "" ))]

MOON_COLUMNS = [
    (("latitude",               MOONX, "centric"),          ("P",  "",  "D")),
    (("latitude",               MOONX, "graphic"),          ("P",  "",  "D")),
    (("longitude",              MOONX, "iau", "west"),      ("P",  "",  "D")),
    (("longitude",              MOONX, "iau", "east"),      ("P",  "",  "D")),
    (("longitude",              MOONX, "sha", "east"),      ("P",  "",  "" )),
    (("longitude",              MOONX, "obs", "west", -180),("P",  "",  "D"), "-180"),
    (("longitude",              MOONX, "obs", "east", -180),("P",  "",  "D"), "-180"),
    (("finest_resolution",      MOONX),                     ("P",  "",  "D")),
    (("coarsest_resolution",    MOONX),                     ("P",  "",  "D")),
    (("distance",               MOONX),                     ("P",  "",  "" )),
    (("phase_angle",            MOONX),                     ("P",  "",  "" )),
    (("incidence_angle",        MOONX),                     ("P",  "",  "" )),
    (("emission_angle",         MOONX),                     ("P",  "",  "" ))]

MOON_GRIDLESS_COLUMNS = [
    (("sub_solar_latitude",     MOONX, "centric"),          ("",   "",  "" )),
    (("sub_solar_latitude",     MOONX, "graphic"),          ("",   "",  "" )),
    (("sub_observer_latitude",  MOONX, "centric"),          ("",   "",  "" )),
    (("sub_observer_latitude",  MOONX, "graphic"),          ("",   "",  "" )),
    (("sub_solar_longitude",    MOONX, "iau", "west"),      ("",   "",  "" )),
    (("sub_solar_longitude",    MOONX, "iau", "east"),      ("",   "",  "" )),
    (("sub_observer_longitude", MOONX, "iau", "west"),      ("",   "",  "" )),
    (("sub_observer_longitude", MOONX, "iau", "east"),      ("",   "",  "" )),
    (("center_resolution",      MOONX, "u"),                ("",   "",  "" )),
    (("center_distance",        MOONX, "obs"),              ("",   "",  "" )),
    (("center_phase_angle",     MOONX),                     ("",   "",  "" ))]

SUN_COLUMNS = [
    (("latitude",               "SUN", "centric"),          ("",  "",  "" )),
    (("latitude",               "SUN", "graphic"),          ("",  "",  "" )),
    (("longitude",              "SUN", "iau", "west"),      ("",  "",  "" )),
    (("longitude",              "SUN", "iau", "east"),      ("",  "",  "" )),
    (("longitude",              meta.NULL, "sha", "east"),  ("",  "",  "" )),
    (("longitude",              "SUN", "obs", "west", -180),("",  "",  "" ), "-180"),
    (("longitude",              "SUN", "obs", "east", -180),("",  "",  "" ), "-180"),
    (("finest_resolution",      "SUN"),                     ("",  "",  "" )),
    (("coarsest_resolution",    "SUN"),                     ("",  "",  "" )),
    (("distance",               "SUN"),                     ("",  "",  "" )),
    (("phase_angle",            meta.NULL),                 ("",  "",  "" )),
    (("incidence_angle",        meta.NULL),                 ("",  "",  "" )),
    (("emission_angle",         meta.NULL),                 ("",  "",  "" ))]

SUN_GRIDLESS_COLUMNS = [
    (("sub_solar_latitude",     meta.NULL, "centric"),      ("",   "",  "" )),
    (("sub_solar_latitude",     meta.NULL, "graphic"),      ("",   "",  "" )),
    (("sub_observer_latitude",  "SUN", "centric"),          ("",   "",  "" )),
    (("sub_observer_latitude",  "SUN", "graphic"),          ("",   "",  "" )),
    (("sub_solar_longitude",    meta.NULL, "iau", "west"),  ("",   "",  "" )),
    (("sub_solar_longitude",    meta.NULL, "iau", "east"),  ("",   "",  "" )),
    (("sub_observer_longitude", "SUN", "west"),             ("",   "",  "" )),
    (("sub_observer_longitude", "SUN", "east"),             ("",   "",  "" )),
    (("center_resolution",      "SUN", "u"),                ("",   "",  "" )),
    (("center_distance",        "SUN", "obs"),              ("",   "",  "" )),
    (("center_phase_angle",     meta.NULL),                 ("",   "",  "" ))]

TEST_COLUMNS = [
    (("right_ascension",        ()),                        ("",   "",  "")),
    (("declination",            ()),                        ("",   "",  "")),
    (("ring_radius",            PLANET_RING),               ("P",  "P", "")),
    (("ring_radial_resolution", PLANET_RING),               ("P",  "P", "")),
    (("ring_longitude",         PLANET_RING, "node"),       ("P",  "P", "")),
    (("ring_longitude",         PLANET_RING, "obs"),        ("P",  "P", "")),
    (("ring_longitude",         PLANET_RING, "sha"),        ("P",  "P", "")),
    (("phase_angle",            PLANET_RING),               ("P",  "P", "")),
    (("incidence_angle",        PLANET_RING),               ("P",  "P", "")),
    (("emission_angle",         PLANET_RING),               ("P",  "P", "")),
    (("distance",               PLANET_RING),               ("P",  "",  "")),
    (("where_inside_shadow",    PLANET_RING, PLANET),       ("P",  "",  "")),
    (("where_in_front",         PLANET_RING, PLANET),       ("P",  "",  "")),
    (("where_antisunward",      PLANET_RING),               ("P",  "",  "")),
    (("ansa_radius",            PLANET_ANSA),               ("P",  "",  "")),
    (("ansa_altitude",          PLANET_ANSA),               ("P",  "",  "")),
    (("ansa_longitude",         PLANET_ANSA, "node"),       ("P",  "",  "")),
    (("ansa_longitude",         PLANET_ANSA, "sha"),        ("P",  "",  "")),
    (("distance",               PLANET_ANSA),               ("P",  "",  "")),
    (("ansa_radial_resolution", PLANET_ANSA),               ("P",  "",  ""))]

# Assemble the column lists for each type of file for the rings and for Saturn

RING_SUMMARY_COLUMNS  = (SKY_COLUMNS + RING_COLUMNS + ANSA_COLUMNS +
                         RING_GRIDLESS_COLUMNS)
RING_DETAILED_COLUMNS = RING_COLUMNS

PLANET_SUMMARY_COLUMNS  = PLANET_COLUMNS + PLANET_GRIDLESS_COLUMNS
PLANET_DETAILED_COLUMNS = PLANET_COLUMNS

# Create a dictionary for the columns of each moon

MOON_SUMMARY_COLUMNS  = MOON_COLUMNS + MOON_GRIDLESS_COLUMNS
MOON_DETAILED_COLUMNS = MOON_COLUMNS

MOON_SUMMARY_DICT  = meta.replacement_dict(MOON_SUMMARY_COLUMNS,
                                           MOONX, MOON_NAMES)
MOON_DETAILED_DICT = meta.replacement_dict(MOON_DETAILED_COLUMNS,
                                           MOONX, MOON_NAMES)

# Treat the Sun as a moon for purposes of metadata

SUN_SUMMARY_COLUMNS  = SUN_COLUMNS + SUN_GRIDLESS_COLUMNS
SUN_DETAILED_COLUMNS = SUN_COLUMNS

MOON_SUMMARY_DICT["SUN"]  = SUN_SUMMARY_COLUMNS
MOON_DETAILED_DICT["SUN"] = SUN_DETAILED_COLUMNS

################################################################################
# Define the tiling for detailed listings
#
# The first item in the list defines a region to test for a suitable pixel
# count. The remaining items define a sequence of tiles to use in a
# detailed tabulation.
################################################################################

RING_AZ = ("ring_azimuth", PLANET_RING, "obs")

RING_TILES = [
    ("where_all",                                   # mask over remaining tiles
        ("where_in_front", PLANET_RING, PLANET),
        ("where_outside_shadow", PLANET_RING, PLANET),
        ("where_below", ("ring_radius", PLANET_RING), 51604.)),
    ("where_between", RING_AZ, 0.15 * np.pi, 0.40 * np.pi),
    ("where_between", RING_AZ, 0.40 * np.pi, 0.60 * np.pi),
    ("where_between", RING_AZ, 0.60 * np.pi, 0.85 * np.pi),
    ("where_between", RING_AZ, 0.85 * np.pi, 1.15 * np.pi),
    ("where_between", RING_AZ, 1.15 * np.pi, 1.40 * np.pi),
    ("where_between", RING_AZ, 1.40 * np.pi, 1.60 * np.pi),
    ("where_between", RING_AZ, 1.60 * np.pi, 1.85 * np.pi),
    ("where_any",
        ("where_below", RING_AZ, 0.15 * np.pi),
        ("where_above", RING_AZ, 1.85 * np.pi)),
]

OUTER_RING_TILES = [
    ("where_all",                                   # mask over remaining tiles
        ("where_in_front", PLANET_RING, PLANET),
        ("where_above", ("ring_radius", PLANET_RING), 51604.)),
    ("where_between", RING_AZ, 0.15 * np.pi, 0.40 * np.pi),
    ("where_between", RING_AZ, 0.40 * np.pi, 0.60 * np.pi),
    ("where_between", RING_AZ, 0.60 * np.pi, 0.85 * np.pi),
    ("where_between", RING_AZ, 0.85 * np.pi, 1.15 * np.pi),
    ("where_between", RING_AZ, 1.15 * np.pi, 1.40 * np.pi),
    ("where_between", RING_AZ, 1.40 * np.pi, 1.60 * np.pi),
    ("where_between", RING_AZ, 1.60 * np.pi, 1.85 * np.pi),
    ("where_any",
        ("where_below", RING_AZ, 0.15 * np.pi),
        ("where_above", RING_AZ, 1.85 * np.pi)),
]

PLANET_TILES = [
    ("where_sunward", PLANET),                      # mask over remaining tiles
    ("where_below",   ("latitude", PLANET), -70. * oops.RPD),
    ("where_between", ("latitude", PLANET), -70. * oops.RPD, -50. * oops.RPD),
    ("where_between", ("latitude", PLANET), -50. * oops.RPD, -30. * oops.RPD),
    ("where_between", ("latitude", PLANET), -30. * oops.RPD, -10. * oops.RPD),
    ("where_between", ("latitude", PLANET), -10. * oops.RPD,  10. * oops.RPD),
    ("where_between", ("latitude", PLANET),  10. * oops.RPD,  30. * oops.RPD),
    ("where_between", ("latitude", PLANET),  30. * oops.RPD,  50. * oops.RPD),
    ("where_between", ("latitude", PLANET),  50. * oops.RPD,  70. * oops.RPD),
    ("where_above",   ("latitude", PLANET),  70. * oops.RPD)
]

MOON_TILES = [
    ("where_all", ("where_in_front", MOONX, PLANET), # mask over remaining tiles
                  ("where_sunward",  MOONX)),
    ("where_below",   ("latitude", MOONX), -70. * oops.RPD),
    ("where_between", ("latitude", MOONX), -70. * oops.RPD, -50. * oops.RPD),
    ("where_between", ("latitude", MOONX), -50. * oops.RPD, -30. * oops.RPD),
    ("where_between", ("latitude", MOONX), -30. * oops.RPD, -10. * oops.RPD),
    ("where_between", ("latitude", MOONX), -10. * oops.RPD,  10. * oops.RPD),
    ("where_between", ("latitude", MOONX),  10. * oops.RPD,  30. * oops.RPD),
    ("where_between", ("latitude", MOONX),  30. * oops.RPD,  50. * oops.RPD),
    ("where_between", ("latitude", MOONX),  50. * oops.RPD,  70. * oops.RPD),
    ("where_above",   ("latitude", MOONX),  70. * oops.RPD)
]

MOON_TILE_DICT = {}
for name in (MOON_NAMES + ["SUN"]):
    MOON_TILE_DICT[name] = meta.replace(MOON_TILES, MOONX, name)

################################################################################


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 12/6/19 1:58 PM
# @Author : xuezhi.zhang
# @File : utils.py
import base64

def base64_decode(base64_encode_str):
    """
    利用 base64.urlsafe_b64decode 对字符串解码
    :param base64_encode_str:
    :return:
    """
    if base64_encode_str:
        need_padding = len(base64_encode_str) % 4
        if need_padding != 0:
            missing_padding = 4 - need_padding
            base64_encode_str += '=' * missing_padding
        base64_encode_str = base64.urlsafe_b64decode(base64_encode_str).decode(
            'utf-8')
    if not isinstance(base64_encode_str, str):
        base64_encode_str = str(base64_encode_str, encoding='utf-8')
    return base64_encode_str

def base64_encode(input, padding=False):
    """
    利用 base64.urlsafe_b64decode 对字符串编码
    :param input: base64_encode_str
    :return:
    """
    assert isinstance(input, str), 'the type of input is not str.'
    if len(input) == 0:
        return ''
    input_encode_utf8 = input.encode('utf-8')
    input_encode_base64 = base64.urlsafe_b64encode(input_encode_utf8)
    if padding:
        need_padding = len(input_encode_base64) % 4
        if need_padding != 0:
            missing_padding = 4 - need_padding
            input_encode_base64 += '=' * missing_padding
    if not isinstance(input_encode_base64, str):
        input_encode_base64 = str(input_encode_base64, encoding='utf-8')
    return input_encode_base64

def dict_type_default():
    '''
    protocol_param <- protoparam
    obfs_param <- obfsparam
    server_port <- port
    :return:
    '''
    conf = {
        "group": "",
        "method": "",
        "obfs": "",
        "obfs_param": "",
        "password": "",
        "protocol": "",
        "protocol_param": "",
        "remarks": "",
        "server": "",
        "server_port": "",
    }
    return conf

def dict_type_url():
    """
    protocol_param -> protoparam
    obfs_param -> obfsparam
    server_port -> port
    :return:
    """
    conf = {
        "group": "",
        "method": "",
        "obfs": "",
        "obfsparam": "",
        "password": "",
        "port": "",
        "protocol": "",
        "protoparam": "",
        "remarks": "",
        "server": "",
    }
    return conf

def dict_type_default2url(conf):
    assert isinstance(conf, dict), "input is not dict."
    # obfs_param
    conf['obfsparam'] = conf['obfs_param']
    conf.pop('obfs_param')
    # protocol_param
    conf['protoparam'] = conf['protocol_param']
    conf.pop('protocol_param')
    # server_port
    conf['port'] = conf['server_port']
    conf.pop('server_port')
    return conf

def dict_type_url2default(conf):
    assert isinstance(conf, dict), "input is not dict."
    # obfs_param
    conf['obfs_param'] = conf['obfsparam']
    conf.pop('obfsparam')
    # protocol_param
    conf['protocol_param'] = conf['protoparam']
    conf.pop('protoparam')
    # server_port
    conf['server_port'] = conf['port']
    conf.pop('port')
    return conf


__author__ = 'Dazdingo'

from pytz import timezone
import simplejson as json
import ConfigParser
import sys
from datetime import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

config_parser = ConfigParser.ConfigParser()
config_parser.read('server.cfg')
time_zone = config_parser.get('common', 'time_zone')

def document2json(document):
    for key, value in document._data:
        document[key] = str(document[key])
    return json.dumps(document)

def collection2json(collection):
    temp = list()
    for document in collection:
        for key, value in document._data:
            document[key] = str(document[key])
        temp.append(document)
    return json.dumps(temp)

def get_time_stamp():
    dt_now = datetime.now(timezone(time_zone))
    import time
    return str(int(time.mktime(dt_now.timetuple())))

def item2json(item):
    return json.dumps(item._data)


from sklearn.manifold import TSNE
import pickle
import numpy as np
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import zero_one_loss

#runs PCA to n components and then runs t-SNE to 2 components, plotting the resulting matrix
matrix_in = open('./data/5k_title_matrix.pkl', 'rb')
((matrix, ids), Y), desc = pickle.load(matrix_in)
matrix_in.close()

scaler = StandardScaler()

scaler.fit(matrix)
matrixp = scaler.transform(matrix)
pca = PCA(n_components = 1000) # retains 95% of variance -- we can change this (this can also be the number of components/dimensions we want, eg. n_components = 3)
pca.fit(matrixp)
# print(pca.n_components_)

matrix_pca = pca.transform(matrix)
print("cumulative variation for 1000 components is " + str(np.sum(pca.explained_variance_ratio_)) )

tsne = TSNE(n_components = 2)
matrix_tsne = tsne.fit_transform(matrix_pca)
print('fitted')

for i in range(len(matrix_tsne)):
    #isolating values to analyze, change numbers as needed
    if matrix_pca[i][0] > 15 and matrix_pca[i][0] < 15.02 and matrix_pca[i][1] < -18.5 and matrix_pca[i][1] > -18.6:
        print(f"index {i} has a value of {matrix_tsne[i]}")

targets = [0,1]
colors = ['r', 'b']
for target, color in zip(targets,colors):
    inds_to_keep = []
    for i in range(len(Y)):
        if Y[i][2] == target:
            inds_to_keep.append(i)
    plt.scatter(matrix_tsne[inds_to_keep, 0], matrix_tsne[inds_to_keep, 1], alpha = 0.4,
            c = color, s = 20)
print("computed")
plt.xlabel('component 1')
plt.ylabel('component 2')

plt.show()

pickle_out = open("./data/title_tsne_experiment.pkl", 'wb')
pickle.dump((matrix_tsne, Y), pickle_out)
pickle_out.close()


from __future__ import print_function
def initialize():
    try:
        import os, subprocess
        from socket import socket
        from sys import platform
        from google_auth_oauthlib.flow import InstalledAppFlow
        from google.auth.transport.requests import Request
        from email.mime.text import MIMEText
        import pickle
        import os.path
        from bs4 import BeautifulSoup
        import base64
        import email
        from googleapiclient.discovery import build
        from apiclient import errors
        from httplib2 import Http
        from googleapiclient import discovery, errors
        from oauth2client import file, client, tools
        from google.oauth2 import service_account
        import base64
    except:
        import os, subprocess
        install_pydeps_command = "pip install -r requirements.txt"
        install_sysdeps_command = "cd lib/ && chmod +x linux_install.sh &&  sh linux_install.sh"
        install_deps_prompt = input ("Couldn't find some dependencies. Try to install them? (Y/n) ")
        if "y" in install_deps_prompt.lower():
            os.system(install_pydeps_command)
            os.system(install_sysdeps_command)
            # _ = subprocess.call('clear' if os.name =='posix' else 'cls',shell= True)
            print("Done. Re-run gmail-controller to continue!")
            exit()
        else:
            # _ = subprocess.call('clear' if os.name =='posix' else 'cls',shell= True)
            print("Exiting...")
            exit()

APPLICATION_NAME = 'gmail-controller'
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.modify', 'https://mail.google.com/']

def clear():
    import os, subprocess
    _ = subprocess.call('clear' if os.name =='posix' else 'cls',shell= True)

def removeHtml(text):
    import re
    TAG_RE = re.compile(r'<[^>]+>')
    return TAG_RE.sub('', text)

def getTokenFilename():
    import os
    keyword = 'client_secret'
    for filename in os.listdir('.'):
        if keyword in filename:
            return filename

def getKeyFilename():
    import os
    keyword = 'controller'
    for filename in os.listdir('.'):
        if keyword in filename and '.json' in filename:
            return filename

def sendEmail():
    clear()
    import pickle
    import os,time
    from google_auth_oauthlib.flow import Flow, InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
    from google.auth.transport.requests import Request
    import base64
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText

    def createService(client_secret_file, api_name, api_version, *scopes):
        CLIENT_SECRET_FILE = client_secret_file
        API_SERVICE_NAME = api_name
        API_VERSION = api_version
        SCOPES = [scope for scope in scopes[0]]
        cred = None
        pickle_file = f'token.pickle'

        if os.path.exists(pickle_file):
            with open(pickle_file, 'rb') as token:
                cred = pickle.load(token)

        if not cred or not cred.valid:
            if cred and cred.expired and cred.refresh_token:
                cred.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
                cred = flow.run_local_server()

            with open(pickle_file, 'wb') as token:
                pickle.dump(cred, token)

        try:
            service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
            print('Connected to Gmail!')
            return service
        except Exception as e:
            print('Unable to connect.')
            exit()
            return None

    def convert_to_RFC_datetime(year=1900, month=1, day=1, hour=0, minute=0):
        dt = datetime.datetime(year, month, day, hour, minute, 0).isoformat() + 'Z'
        return dt

    CLIENT_SECRET_FILE = getTokenFilename()
    API_NAME = 'gmail'
    API_VERSION = 'v1'
    service = createService(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

    # Email UI
    time.sleep(3)
    print ("____________________________________________")
    print ("     NEW EMAIL   ")
    print ("____________________________________________")
    to = input(" > To email ID: ")
    print ("____________________________________________")
    subject = input ("   Subject: ")
    print("")
    message = input ("   Message: ")

    mimeMessage = MIMEMultipart()
    mimeMessage['to'] = to
    mimeMessage['subject'] = subject
    mimeMessage.attach(MIMEText(message, 'plain'))
    raw_string = base64.urlsafe_b64encode(mimeMessage.as_bytes()).decode()

    message = service.users().messages().send(userId='me', body={'raw': raw_string}).execute()
    clear()
    print("Email sent to " + to + "!")
    displayMenu()

def getInbox():
    from googleapiclient.discovery import build
    from google_auth_oauthlib.flow import InstalledAppFlow
    from google.auth.transport.requests import Request
    import pickle
    from bs4 import BeautifulSoup
    import os.path
    import base64, time
    import email

    id_list = []
    subject_list = []
    sender_list = []
    message_list = []

    creds = None
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)

    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(getTokenFilename(), SCOPES)
            creds = flow.run_local_server(port=0)

        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('gmail', 'v1', credentials=creds)
    result = service.users().messages().list(userId='me').execute()
    messages = result.get('messages')
    clear()
    print("Getting emails")
    count=0

    time.sleep(3)

    for msg in messages:
        print("Getting email #" + str(count))
        count+=1
        txt = service.users().messages().get(userId='me', id=msg['id']).execute()
        try:
            payload = txt['payload']
            headers = payload['headers']
            subject = None
            for d in headers:
                if d['name'] == 'Subject':
                    subject = d['value']
                if d['name'] == 'From':
                    sender = d['value']

            parts = payload.get('parts')[0]
            data = parts['body']['data']
            data = data.replace("-","+").replace("_","/")
            decoded_data = base64.b64decode(data)
            soup = BeautifulSoup(decoded_data , "lxml")
            message = removeHtml(str(soup.body()))
            subject_list.append(subject)
            sender_list.append(sender)
            message_list.append(message)
            id_list.append(msg['id'])

        except:
            print ("Error! Couldn't get email #" + str(count) + ". Retrying...")

    clear()
    print ("____________________________________________")
    print ("     INBOX   ")
    print ("____________________________________________")
    count = 0
    for message in message_list:
        try:
            print ("____________________________________________")
            print (" Message #" + str(count+1))
            #print (" Message #" + str(count+1) + str(status_list[count+1]))
            print("")
            print (" From: " + sender_list[count+1])
            print("")
            print (" Subject: " + subject_list[count+1])

        except:
            pass

        count +=1

    print ("\n\n********************************************")
    try:
        message_number = int(input(" > Enter message number to view: "))
        clear()
        print ("____________________________________________")
        print ("     INBOX   ")
        print ("____________________________________________")
        print (" From: " + sender_list[message_number])
        print ("____________________________________________")
        print (" Subject: " + subject_list[message_number])
        print("")
        print(message_list[message_number])

        choices = '''
_______________________________________________
> press 'r' to mark as read
  press 'd' to delete
  press 'u' to mark as unread
_______________________________________________
'''
        choice = input(choices + " Enter a choice: ")
        print ("____________________________________________")

        if 'r' in choice.lower():
            service.users().messages().modify(userId='me', id=id_list[message_number], body={'removeLabelIds': ['UNREAD']}).execute()

        elif 'd' in choice.lower():
            service.users().messages().delete(userId='me', id=id_list[message_number]).execute()

        elif 'u' in choice.lower():
            service.users().messages().modify(userId='me', id=id_list[message_number], body={'addLabelIds': ['UNREAD']}).execute()

        else:
            clear()
            print ("Invalid input. Exiting...")
            exit()

        clear()
        displayMenu()

    except:
        print ("An error occurred")
        exit()

def displayMenu():
    print("__________________________________________")
    print("|             Gmail Controller           |")
    print("|             Soumya Badgandi            |")
    print("|----------------------------------------|")
    print("| >  Press '1' to SEND an email          |")
    print("| >  Press '2' to READ emails            |")
    print("|----------------------------------------|")
    try:
        choice = input("  + Enter a choice to continue: ").lower()
        if choice == '1' or choice == 'send':
            sendEmail()
        elif choice == '2' or choice == 'read':
            getInbox()
        else:
            exit()

    except:
        exit()

clear()
initialize()
displayMenu()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import admin_app.core.utils.models

class Migration(migrations.Migration):

    dependencies = [
        ('goods', '0002_goods_title'),
    ]

    operations = [
        migrations.CreateModel(
            name='Offer',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('is_best', models.BooleanField(default=False, verbose_name='\u041b\u0443\u0447\u0448\u0435\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435')),
                ('shop_title', models.TextField(null=True, verbose_name='\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430', blank=True)),
                ('price', models.TextField(null=True, verbose_name='\u0426\u0435\u043d\u0430(\u0441\u043e \u0441\u043a\u0438\u0434\u043a\u043e\u0439)', blank=True)),
                ('old_price', models.TextField(null=True, verbose_name='\u0426\u0435\u043d\u0430 \u0431\u0435\u0437 \u0441\u043a\u0438\u0434\u043a\u0438', blank=True)),
                ('url', models.URLField(null=True, verbose_name='\u0426\u0435\u043d\u0430 \u0431\u0435\u0437 \u0441\u043a\u0438\u0434\u043a\u0438', blank=True)),
            ],
            bases=(admin_app.core.utils.models.CommonModelMixin, models.Model),
        ),
        migrations.AlterModelOptions(
            name='goods',
            options={'ordering': ('title',), 'verbose_name': '\u0422\u043e\u0432\u0430\u0440', 'verbose_name_plural': '\u0422\u043e\u0432\u0430\u0440\u044b'},
        ),
        migrations.RemoveField(
            model_name='goods',
            name='URL',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='age_from',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='age_to',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='buying_price',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='delivery',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='local_delivery_cost',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='model',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='old_price',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='pickup',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='price',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='store',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='type_prefix',
        ),
        migrations.RemoveField(
            model_name='goods',
            name='weight',
        ),
        migrations.AlterField(
            model_name='goods',
            name='detmir_id',
            field=models.IntegerField(unique=True, verbose_name='Id \u0432 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435 \u041f\u0440\u0430\u0439\u0441\u0430'),
        ),
        migrations.AlterField(
            model_name='goods',
            name='gender',
            field=models.IntegerField(null=True, verbose_name='\u041f\u043e\u043b', choices=[(0, '\u041c\u0443\u0436\u0441\u043a\u043e\u0439'), (1, '\u0416\u0435\u043d\u0441\u043a\u0438\u0439')]),
        ),
        migrations.AlterField(
            model_name='goods',
            name='hidden',
            field=models.BooleanField(default=False, verbose_name='\u0421\u043a\u0440\u044b\u0442\u044b\u0442\u044c \u0442\u043e\u0432\u0430\u0440'),
        ),
        migrations.AddField(
            model_name='offer',
            name='good',
            field=models.ForeignKey(related_name='offers', to='goods.Goods'),
        ),
    ]


import json
import logging

import aiohttp
from pymongo import MongoClient

import datetime, time
import requests, hashlib
from requests import RequestException
from urllib.parse import urlencode

from dquant.config import cfg
from dquant.constants import Constants
from dquant.markets.market import Market

class OkexFutureRest(Market):
    def __init__(self, meta_code):
        base_currency, market_currency, symbol, contract_type = self.parse_meta(meta_code)
        super().__init__(base_currency, market_currency, meta_code, cfg.get_float_config(Constants.OKEX_FEE))
        self.apikey = cfg.get_config(Constants.OKEX_APIKEY)
        self.apisec = cfg.get_config(Constants.OKEX_APISEC)
        self.contract_type = contract_type
        self.symbol = symbol
        self.base_url = Constants.OKEX_FUTURE_REST_BASE
        self.timeout = Constants.OK_HTTP_TIMEOUT

    async def get_ticker(self):
        depth = await self.get_depth()
        if not depth:
            return None
        res = {'ask': {'price': 0, 'amount': 0}, 'bid': {'price': 0, 'amount': 0}}
        if len(depth['asks']) > 0:
            res['ask'] = depth['asks'][0]
        if len(depth["bids"]) > 0:
            res['bid'] = depth['bids'][0]
        return res

    # 轮询获取depth
    async def get_depth(self):
        params = {"symbol": self.symbol, "contract_type": self.contract_type}
        while True:
            try:
                res = await self.request(Constants.OKEX_FUTURE_DEPTH_RESOURCE_REST, params, "get")
                list_of_ask = self.okex_depth_format(res, "asks")
                list_of_ask.reverse()
                list_of_bid = self.okex_depth_format(res, "bids")
                return {"asks": list_of_ask, 'bids': list_of_bid}
            except Exception:
                logging.exception("http error")

    # 格式化depth数据
    def okex_depth_format(self, res, flag):
        result_list = []
        for ticker in res[flag]:
            result_list.append({
                'price': ticker[0],
                'amount': ticker[1]
            })
        return result_list

    async def long(self, amount, price='', lever_rate='10'):
        res = await self.okex_request(price=price, amount=amount, type='1', api_url=Constants.OKEX_FUTURE_TRADE_REST)
        return res

    async def short(self, amount, price='', lever_rate='10'):
        res = await self.okex_request(price=price, amount=amount, type='2', api_url=Constants.OKEX_FUTURE_TRADE_REST)
        return res

    async def close_long(self, amount, price=''):
        res = await self.okex_request(price=price, amount=amount, type='3', api_url=Constants.OKEX_FUTURE_TRADE_REST)
        return res

    async def close_short(self, amount, price=''):
        res = await self.okex_request(price=price, amount=amount, type='4', api_url=Constants.OKEX_FUTURE_TRADE_REST)
        return res

    async def delete_order(self, order_id, tillOK=True):
        '''
        :param order_id:
        :return: {'result': True, 'order_id': '14435081666'}
        '''
        while True:
            try:
                res = await self.okex_request(order_id=order_id, api_url=Constants.OKEX_FUTURE_DELETE_ORDER_REST)
                if 'result' in res:
                    if res['result'] == True:
                        return res
                if tillOK ==True:
                    continue
                else:
                    logging.error(res)
                    break
            except Exception as ex:
                logging.exception("message")
                if tillOK:
                    continue
                return None

    async def get_account(self, coin=[]):
        '''
        :return:{"info": {"btc": {"account_rights": 1,"keep_deposit": 0,"profit_real": 3.33,"profit_unreal": 0,"risk_rate": 10000},"ltc": {"account_rights": 2,"keep_deposit": 2.22,"profit_real": 3.33,"profit_unreal": 2,"risk_rate": 10000},"result": true}
        '''
        res = await self.okex_request(api_url=Constants.OKEX_FUTURE_USERINFO_REST)
        if res['result'] is True:
            if coin:
                ret = {}
                for c in coin:
                    if c.lower() in res["info"]:
                        ret[c.upper()] = res["info"][c.lower()]['balance']
                    else:
                        ret[c.upper()] = 0.0
                return ret
            else:
                return res["info"]

    # 向API发送请求
    async def okex_request(self, api_url, **kwargs):
        '''
        :param price: 默认对手价
        :param amount: 最小为1
        :param type: 1:开多 2:开空 3:平多 4:平空, 'delete_order':取消订单
        :param lever_rate: 杠杆倍数 value:10\20 默认10
        :param match_price: 是否为对手价 0:不是  1:是,当取值为1时,price无效。这里根据price是否为空判断。
        :param contract_type: 合约类型: this_week:当周   next_week:下周   quarter:季度
        :return: {"order_id":986,"result":true}
        '''
        params = {}

        if api_url is Constants.OKEX_FUTURE_DELETE_ORDER_REST:
            order_id = kwargs.get('order_id', None)
            params = {'api_key': self.apikey, 'symbol': self.symbol, 'contract_type': self.contract_type, 'order_id': order_id}

        elif api_url is Constants.OKEX_FUTURE_TRADE_REST:
            params = {'api_key': self.apikey, 'symbol': self.symbol, 'contract_type': self.contract_type, 'amount': str(kwargs.get('amount')),
                      'type': str(kwargs.get('type')), 'match_price': '1', 'lever_rate': str(kwargs.get('lever_rate', 10))}
            price = kwargs.get('price', None)
            if price:
                params['price'] = str(price)
                params['match_price'] = '0'

        elif api_url is Constants.OKEX_FUTURE_USERINFO_REST:
            params = {'api_key': self.apikey}

        params['sign'] = self.buildMySign(params, self.apisec)
        res = await self.request(api_url, params=params, type='post')
        return res

    async def request(self, resource, params, type):
        headers = {
            "Content-type": "application/x-www-form-urlencoded",
        }
        async with aiohttp.ClientSession() as session:
            if type == "post":
                temp_params = urlencode(params)
                async with session.post(url=self.base_url + resource, data=temp_params, timeout=self.timeout, headers=headers) as res:
                    if res.headers['CONTENT-TYPE'] == 'application/json':
                        return await res.json()
                    else:
                        text = await res.text()
                        return json.loads(text)

            elif type == "get":
                async with session.get(url=self.base_url + resource, params=params, timeout=self.timeout) as res:
                    if res.headers['CONTENT-TYPE'] == 'application/json':
                        return await res.json()
                    else:
                        text = await res.text()
                        return json.loads(text)

    def buildMySign(self, params, secretKey):
        sign = ''
        for key in sorted(params.keys()):
            sign += key + '=' + str(params[key]) + '&'
        data = sign + 'secret_key=' + secretKey
        return hashlib.md5(data.encode("utf8")).hexdigest().upper()

    def parse_meta(self, meta_code):
        meta_table = {"btc_usd_this_week": ("btc", "usd", "btc_usd", "this_week"),
                      "btc_usd_next_week": ("btc", "usd", "btc_usd", "next_week"),
                      "btc_usd_quarter": ("btc", "usd", "btc_usd", "quarter"),
                      "eth_usd_this_week": ("eth", "usd", "eth_usd", "this_week"),}
        return meta_table[meta_code]


import os
import sys

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import tensorflow as tf
from sklearn import preprocessing
from tensorflow import keras

sns.set()

def load_data():
    """
    返回读取的文件中的数据，并进行归一化
    """
    path = os.path.join(sys.path[0], 'Data')
    path_data = os.path.join(path, 'all_samples_net.npy')

    data_ori = np.load(path_data)
    X_ori = data_ori[:, :2].copy()
    Y_ori = data_ori[:, 2:].copy()
    x_scale = preprocessing.MinMaxScaler()
    y_scale = preprocessing.MinMaxScaler()
    X = x_scale.fit_transform(X_ori)
    Y = y_scale.fit_transform(Y_ori)

    data = {}
    data['X'] = X
    data['Y'] = Y

    return data

def build_model(input_dim,out_dim,units_lists = None):
    """
    建立一个4层神经层的model
    """
    inputs = keras.Input(shape=(input_dim,))
    if units_lists is None:
        units_lists = [100,200,100,100]
    x = inputs
    for units in units_lists:
        x = keras.layers.Dense(units,activation='relu')(x)
    predictions = keras.layers.Dense(out_dim, activation='tanh')(x)
    model = keras.Model(inputs,predictions)
    model.summary()
    return model

def plot_history(history):
    """
    画出训练之后得到的结果
    """
    plt.figure()
    plt.xlabel('Epoch')
    plt.ylabel('Mean Abs Error')
    plt.plot(history.epoch, np.array(history.history['mean_absolute_error']),label='Train Loss')
    plt.plot(history.epoch, np.array(history.history['val_mean_absolute_error']), label = 'Val loss')
    plt.legend()

def get_gradients(model,trainingExample):
    """
    得到模型在trainingExample对输入的梯度
    """
    outputTensor = model.output
    gradients = tf.gradients(outputTensor, model.input)
    sess = keras.backend.get_session()
    evaluated_gradients = sess.run(gradients,feed_dict={model.input:trainingExample})
    return evaluated_gradients

if __name__ == '__main__':
    data = load_data()
    X,Y = data['X'],data['Y']

    train = 1
    if train:
        model = build_model(X.shape[1], Y.shape[1])
        model.summary()
        keras.utils.plot_model(model, to_file=os.path.join(sys.path[0], 'Net/model2.png'))
        # 设置训练方式
        model.compile(tf.train.RMSPropOptimizer(0.01), loss=tf.losses.mean_squared_error, metrics=['mae'])

        # 进行训练
        early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=50)  # 20代测试误差没有改进就退出
        history = model.fit(X, Y, batch_size=1024, epochs=1000, validation_split=0.1, callbacks=[early_stop])
        model.save(os.path.join(sys.path[0], 'Net/model2.h5'))
        # 训练结果展示
        plot_history(history)
        plt.show()
    else:
        model = keras.models.load_model(os.path.join(sys.path[0], 'Net/model2.h5'))
        model.summary()
        for _ in range(100):
            print(get_gradients(model,X))


# XOX Oyunu
#		Kitapta yer alan XOX oyunu iki kisinin karsilikli oynayabilecegi sekilde duzenlenmis. Sizden bu oyunu
#		kullanicinin bilgisayara karsi oynayabilecegi versiyonunu yapmanizi istiyoruz. Ayrica gelistireceginiz
#		algoritma sayesinde bilgisayarin tamamen rastgele hamleler yapmasindan ziyade mantikli hamleler yapmasini
#		saglamanizi istiyoruz. Ornegin bilgisayarin "O" hamlesini yaptigini varsayalim:
#					X O _
#					_ X _
#					_ _ _
#
#		seklinde olusan bir durumda hamle sirasi bilgisayarda ve bilgisayar kaybetmemek icin sag-alt koseye "O"
#		koymalidir.
#
#
#		Farkli bir ihtimal:
#					O X X
#					O _ X
#					_ _ _
#
#		boyle bir durumda da hamle sirasi bilgisayarda ve bilgisayar kazanma hamlesi olarak sol-alt koseye "O" koyarak
#		oyunu bitirmelidir.

liste1 = ["","",""]
liste2 = ["","",""]
liste3 = ["","",""]

kazanan = False

print("\n X-O-X OYUNUNA HOSGELDINIZ!!!")
devam1 = input("\nDevam etmek için Enter'a  basınız.\n")
while kazanan == False:
    secim = input("'X' yada 'O' seçimi yapınız!\n")
    yerSecimi = int(input("Secimizi yapmak için sayilardan birinizi seçiniz ...\n"))

    if (yerSecimi == 7):
            liste1[0] = secim

    elif(yerSecimi == 8):
            liste1[1] = secim

    elif(yerSecimi == 9):
            liste1[2] = secim

    elif(yerSecimi == 4):
            liste2[0] = secim

    elif(yerSecimi == 5):
            liste2[1] = secim

    elif(yerSecimi == 6):
            liste2[2] = secim

    elif(yerSecimi == 1):
            liste3[0] = secim

    elif(yerSecimi == 2):
            liste3[1] = secim

    elif(yerSecimi == 3):
            liste3[2] = secim

    else:
        print("Kordinatlamalar için 0 haricindeki rakamları kullanınız!")

    print(liste1)
    print(liste2)
    print(liste3)

    if (liste1[0] == liste1[1] == liste1[2]):

        if(liste1 == ["","",""]):
            kazanan = False
        elif(liste2 == ["","",""]):
            kazanan = False
        elif(liste3 == ["","",""]):
            kazanan = False
        else:
            kazanan = True
            print("Tebrikler Kazandınız!")

    elif (liste2[0] == liste2[1] == liste2[2]):

        if (liste1 == ["", "", ""]):
            kazanan = False
        elif (liste2 == ["", "", ""]):
            kazanan = False
        elif (liste3 == ["", "", ""]):
            kazanan = False
        else:
            kazanan = True
            print("Tebrikler Kazandınız!")

    elif (liste3[0] == liste3[1] == liste3[2]):

        if (liste1 == ["", "", ""]):
            kazanan = False
        elif (liste2 == ["", "", ""]):
            kazanan = False
        elif (liste3 == ["", "", ""]):
            kazanan = False
        else:
            kazanan = True
            print("Tebrikler Kazandınız!")

    elif (liste1[0] == liste2[0] == liste3[0] != ""):

        if (liste1[1] == liste2[1] == liste3[1] == "" ):
            kazanan = False
        elif (liste1[2] == liste2[2] == liste3[2] == ""):
            kazanan = False
        else:
            kazanan = True
            print("Tebrikler Kazandınız!")

    elif (liste1[1] == liste2[1] == liste3[1] != ""):

        if (liste1[0] == liste2[0] == liste3[0] == "" ):
            kazanan = False
        elif (liste1[2] == liste2[2] == liste3[2] == ""):
            kazanan = False
        else:
            kazanan = True
            print("Tebrikler Kazandınız!")

    elif (liste1[2] == liste2[2] == liste3[2] != ""):

        if (liste1[0] == liste2[0] == liste3[0] == "" ):
            kazanan = False
        elif (liste1[1] == liste2[1] == liste3[1] == ""):
            kazanan = False
        else:
            kazanan = True
            print("Tebrikler Kazandınız!")

    elif (liste1[0] == liste2[1] == liste3[2] != ""):
        if(liste1[0] == "" and liste2[1] == "" and liste3[2] == ""):
            kazanan = False
        else:
            kazanan = True
            print("Tebrikler Kazandınız!")

    elif (liste1[2] == liste2[1] == liste3[0] != ""):
        if(liste1[2] == "" and liste2[1] == "" and liste3[0]):
            kazanan = False
        else:
            kazanan = True
            print("Tebrikler Kazandınız!")

    else:
        if(liste1[0] != "" and liste1[1] != "" and liste1[2] != ""):
            if (liste2[0] != "" and liste2[1] != "" and liste2[2] != ""):
                if (liste3[0] != "" and liste3[1] != "" and liste3[2] != ""):
                    print ("Berabere!")
                    kazanan = True
            else:
                kazanan = False
        else:
            kazanan = False
    devam2 = input("Kaldığınız yerden devam etmek için Enter'a basınız!\n ")


import math
import pygame
#import const
#import animation
import importlib
const= importlib.import_module("17-3const")
animation= importlib.import_module("17-3animation")

class Stone(animation.Animation):
	def __init__(self):
		self.spriteImage= "17-3stone.png"
		self.spriteWidth= 8
		self.spriteHeight= 8
		self.spriteColumns= 4
		self.fps= 20
		self.state= const.STONE_READY
		self.InitAnimation()

	def update(self):
		self.CalcNextFrame()

		rect= (self.spriteWidth*self.currentFrame, 0, self.spriteWidth, self.spriteHeight)
		self.image.blit( self.spriteSheet, (0, 0), rect)
		self.image.set_colorkey(pygame.color.Color(255, 0, 255))

	def Setup(self, initialPos, power, direction):
		self.initialPos= initialPos
		self.rect.x= initialPos[0]
		self.rect.y= initialPos[1]
		self.power= power
		self.direction= direction
		self.state= const.STONE_FLY

	def Move(self, time, space, DecrementStones):
		pos= self.CalculatePosition(time, const.g, self.direction)
		pos= self.MapPosition(self.initialPos[0], self.initialPos[1], pos[0], pos[1])
		self.rect.x= pos[0]
		self.rect.y= pos[1]

		if pos[0]>space[0] or pos[1]>space[1]:
			self.state= const.STONE_READY
			DecrementStones()

	def CalculatePosition(self, t, g, direction):
		r= math.radians(direction)
		x= self.power*math.cos(r)*t
		y= self.power*math.sin(r)*t - 0.5*g*math.pow(t, 2)

		return (int(x), int(y))

	def MapPosition(self, x, y, newX, newY):
		return (x+newX, y+(newY*-1))


def majorityElement(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    import collections
    collection_dict = dict(collections.Counter(nums))
    ret = list()
    for key in collection_dict:
        if collection_dict[key] > len(nums) / 3:
            ret.append(key)
    return ret

nums_1 = [1, 2, 1]
nums_2 = [1,1,1,3,3,2,2,2]

print(majorityElement(nums_2))