<commit_before>#!/usr/bin/env python3

import sys
import io
import os

import time
from time import sleep

import numpy
import matplotlib
from PIL import Image
from PyQt5.QtCore import *
import PyQt5.QtCore
from PyQt5.QtGui import *

from PyQt5.QtWidgets import *

from pyqt import FreakingQtImageViewer
import scipy.misc

from skimage import color
from skimage import transform as tf
#from skimage import data
from skimage.feature import canny
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter

from skimage.transform import hough_circle
from skimage.feature import peak_local_max
from skimage.draw import circle_perimeter
from skimage.util import img_as_ubyte

import scipy.misc

import picamera
import picamera.array

from cv import circle_detection
from _thread import start_new_thread

class DraughtsGameWindow(QWidget):
	def __init__(self):
		super().__init__()
		self.corners = []
		self.CELLS_PER_ROW = 8
		self.BLACK_COLOR = QColor(0,0,0,255)
		self.WHITE_COLOR = QColor(255,255,255,255)
		self.BLUE_COLOR = QColor(0,0,255,255)
		self.RED_COLOR = QColor(255,0,0,255)
		self.GREEN_COLOR = QColor(0,255,0,255)
		self.DEFAULT_PEN = QPen(self.BLUE_COLOR)
		self.DEFAULT_PEN.setWidth(20)
		self.PIX_PADDING = 75
		self.BORDER_RADIUS = 10

		self.initUI()

	def keyPressEvent(self, event):
		if event.key() == Qt.Key_Escape:
			self.close()
		elif event.key() == Qt.Key_C:
			start_new_thread(self.capture, (0, 0,))

	def initUI(self):

		self.WIDTH, self.HEIGHT, self.CELL_SIZE = screen_rect.width(), screen_rect.height(), screen_rect.width() / self.CELLS_PER_ROW
		self.pixmap = QPixmap(self.WIDTH-20,self.HEIGHT-20)

		self.upper_left = (self.PIX_PADDING + self.WIDTH / 2 - self.pixmap.height() / 2, self.PIX_PADDING)
		self.upper_right = (- self.PIX_PADDING + self.WIDTH / 2 + self.pixmap.height() / 2, self.PIX_PADDING + 0)
		self.lower_right = (-self.PIX_PADDING + self.WIDTH / 2 + self.pixmap.height() / 2, -self.PIX_PADDING + self.pixmap.height())
		self.lower_left = (self.PIX_PADDING + self.WIDTH / 2 - self.pixmap.height() / 2, -self.PIX_PADDING + self.pixmap.height())

		self.DRAW_SIZE = self.upper_right[0] - self.upper_left[0]
		print(self.DRAW_SIZE)

		self.painter = QPainter(self.pixmap)
		self.painter.setPen(self.DEFAULT_PEN)

		self.painter.fillRect(0, 0,self.WIDTH, self.HEIGHT, self.BLACK_COLOR)
		self.painter.drawEllipse(self.upper_left[0]-10,self.upper_left[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.WHITE_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.upper_right[0]-10,self.upper_right[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.RED_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.lower_right[0]-10,self.lower_right[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.GREEN_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.lower_left[0]-10,self.lower_left[1]-10,20,20)

		self.vbox = QVBoxLayout(self)

		self.lbl = QLabel(self)
		self.lbl.setPixmap(self.pixmap)
		self.vbox.addWidget(self.lbl)

		self.setLayout(self.vbox)
		self.vbox.setAlignment(Qt.AlignHCenter)
		self.showFullScreen()
		self.setWindowTitle('DraughtsCV')
		self.show()
		#capture()

	def checkCoords(self, img, coords):
		self.redC = []
		self.greenC = []
		self.blueC = []
		self.whiteC = []
		for index, coord in enumerate(coords):
			#print(img[coord[0], coord[1]]," - ", )
			pixel = img[coord[0], coord[1]]
			sum = 0
			sum += pixel[0]
			sum += pixel[1]
			sum += pixel[2]
			'''if sum > 300:
				print()
				#print("delete ", coord, ' with ', pixel)
			else:'''
			print("pixel: ", pixel, "@",coord)
			if pixel[0] > 150 and pixel[1] > 150 and pixel[2] > 150:
				self.whiteC.append(coord)
			elif pixel[0] > 110:
				self.redC.append(coord)
			elif pixel[1] > 110:
				self.greenC.append(coord)
			elif pixel[2] > 110:
				self.blueC.append(coord)

		#final_red = (int(numpy.sum(self.redC[0])/len(self.redC)),int(numpy.sum(self.redC[1])/len(self.redC[])))	#(sum(self.redC[0]), sum(self.redC[1]))
		final_white = numpy.average(self.whiteC, axis=0)
		final_red = numpy.average(self.redC, axis=0)
		final_green = numpy.average(self.greenC, axis=0)
		final_blue = numpy.average(self.blueC, axis=0)

		self.lbl.setPixmap(self.pixmap)
		if len(self.whiteC) == 0 or len(self.blueC) == 0 or len(self.greenC) == 0 or len(self.redC) == 0 :
			print('less than 4 corners for calibration detected, returning -1')
			return -1

		final_white = (int(final_white[1]),int(final_white[0]))
		final_red = (int(final_red[1]),int(final_red[0]))
		final_green = (int(final_green[1]),int(final_green[0]))
		final_blue = (int(final_blue[1]),int(final_blue[0]))

		print('avg blue ',final_blue)
		print('avg green',final_green)
		print('avg red ',final_red)
		print('avg white ',final_white)

		return numpy.array((final_blue, final_green, final_red, final_white))
		#return finalCoords

	def capture(self, ignore1, ignore2): # pythonmaster
		camera = picamera.PiCamera()
		res = -1
		#while True:
		with picamera.array.PiRGBArray(camera) as stream:
			camera.capture(stream, format='rgb')
			img = stream.array
			img, coords, circleDebug = circle_detection(img, 20, 25)
			im = Image.fromarray(img) #.convert('LA')
			input_img = im
			imDeb = Image.fromarray(circleDebug) #.convert('LA')
			im.save('./tmp.png')
			imDeb.save('./deb.png')

			res = self.checkCoords(img, coords)
			print("res ", res)
			if isinstance(res, (numpy.ndarray, numpy.generic) ):
				src = numpy.array((
					(0, 0), #upper left
					(0, 800), #lower left
					(800, 800), #lright
					(800, 0) #uright
				))

				transformer = tf.ProjectiveTransform()
				transformer.estimate(src, res)
				transformed_image = tf.warp(input_img, transformer, output_shape = (800,800))

				#print("Transformed Image: ",transformed_image)

				scipy.misc.imsave('./transformed.png', transformed_image)
				self.pixmap = QPixmap("checkers_randlos.jpg");
				self.lbl.setPixmap(self.pixmap)
				#break
			else: print('fuckshitsuck')
		camera.close()
		print(self.corners)

if __name__ == '__main__':

	app = QApplication(sys.argv)

	screen_rect = app.desktop().screenGeometry()
	viewer = DraughtsGameWindow() # init ui
	# capture
	sys.exit(app.exec_())<commit_msg>threading<commit_after>#!/usr/bin/env python3

import sys
import io
import os

import time
from time import sleep

import numpy
import matplotlib
from PIL import Image
from PyQt5.QtCore import *
import PyQt5.QtCore
from PyQt5.QtGui import *

from PyQt5.QtWidgets import *

from pyqt import FreakingQtImageViewer
import scipy.misc

from skimage import color
from skimage import transform as tf
#from skimage import data
from skimage.feature import canny
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter

from skimage.transform import hough_circle
from skimage.feature import peak_local_max
from skimage.draw import circle_perimeter
from skimage.util import img_as_ubyte

import scipy.misc

import picamera
import picamera.array

from cv import circle_detection
from _thread import start_new_thread

class DraughtsGameWindow(QWidget):
	def __init__(self):
		super().__init__()
		self.corners = []
		self.CELLS_PER_ROW = 8
		self.BLACK_COLOR = QColor(0,0,0,255)
		self.WHITE_COLOR = QColor(255,255,255,255)
		self.BLUE_COLOR = QColor(0,0,255,255)
		self.RED_COLOR = QColor(255,0,0,255)
		self.GREEN_COLOR = QColor(0,255,0,255)
		self.DEFAULT_PEN = QPen(self.BLUE_COLOR)
		self.DEFAULT_PEN.setWidth(20)
		self.PIX_PADDING = 75
		self.BORDER_RADIUS = 10

		self.initUI()

	def keyPressEvent(self, event):
		if event.key() == Qt.Key_Escape:
			self.close()
		elif event.key() == Qt.Key_C:
			start_new_thread(self.capture, (0, 0,))

	def initUI(self):

		self.WIDTH, self.HEIGHT, self.CELL_SIZE = screen_rect.width(), screen_rect.height(), screen_rect.width() / self.CELLS_PER_ROW
		self.pixmap = QPixmap(self.WIDTH-20,self.HEIGHT-20)

		self.upper_left = (self.PIX_PADDING + self.WIDTH / 2 - self.pixmap.height() / 2, self.PIX_PADDING)
		self.upper_right = (- self.PIX_PADDING + self.WIDTH / 2 + self.pixmap.height() / 2, self.PIX_PADDING + 0)
		self.lower_right = (-self.PIX_PADDING + self.WIDTH / 2 + self.pixmap.height() / 2, -self.PIX_PADDING + self.pixmap.height())
		self.lower_left = (self.PIX_PADDING + self.WIDTH / 2 - self.pixmap.height() / 2, -self.PIX_PADDING + self.pixmap.height())

		self.DRAW_SIZE = self.upper_right[0] - self.upper_left[0]
		print(self.DRAW_SIZE)

		self.painter = QPainter(self.pixmap)
		self.painter.setPen(self.DEFAULT_PEN)

		self.painter.fillRect(0, 0,self.WIDTH, self.HEIGHT, self.BLACK_COLOR)
		self.painter.drawEllipse(self.upper_left[0]-10,self.upper_left[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.WHITE_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.upper_right[0]-10,self.upper_right[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.RED_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.lower_right[0]-10,self.lower_right[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.GREEN_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.lower_left[0]-10,self.lower_left[1]-10,20,20)

		self.vbox = QVBoxLayout(self)
		self.vbox.setContentsMargins(240,112,240,112)
		self.lbl = QLabel(self)
		self.lbl.setPixmap(self.pixmap)
		self.vbox.addWidget(self.lbl)

		self.setLayout(self.vbox)
		self.vbox.setAlignment(Qt.AlignHCenter)
		self.showFullScreen()
		self.setWindowTitle('DraughtsCV')
		self.show()
		#capture()

	def checkCoords(self, img, coords):
		self.redC = []
		self.greenC = []
		self.blueC = []
		self.whiteC = []
		for index, coord in enumerate(coords):
			#print(img[coord[0], coord[1]]," - ", )
			pixel = img[coord[0], coord[1]]
			sum = 0
			sum += pixel[0]
			sum += pixel[1]
			sum += pixel[2]
			'''if sum > 300:
				print()
				#print("delete ", coord, ' with ', pixel)
			else:'''
			print("pixel: ", pixel, "@",coord)
			if pixel[0] > 150 and pixel[1] > 150 and pixel[2] > 150:
				self.whiteC.append(coord)
			elif pixel[0] > 110:
				self.redC.append(coord)
			elif pixel[1] > 110:
				self.greenC.append(coord)
			elif pixel[2] > 110:
				self.blueC.append(coord)

		#final_red = (int(numpy.sum(self.redC[0])/len(self.redC)),int(numpy.sum(self.redC[1])/len(self.redC[])))	#(sum(self.redC[0]), sum(self.redC[1]))
		final_white = numpy.average(self.whiteC, axis=0)
		final_red = numpy.average(self.redC, axis=0)
		final_green = numpy.average(self.greenC, axis=0)
		final_blue = numpy.average(self.blueC, axis=0)

		self.lbl.setPixmap(self.pixmap)
		if len(self.whiteC) == 0 or len(self.blueC) == 0 or len(self.greenC) == 0 or len(self.redC) == 0 :
			print('less than 4 corners for calibration detected, returning -1')
			return -1

		final_white = (int(final_white[1]),int(final_white[0]))
		final_red = (int(final_red[1]),int(final_red[0]))
		final_green = (int(final_green[1]),int(final_green[0]))
		final_blue = (int(final_blue[1]),int(final_blue[0]))

		print('avg blue ',final_blue)
		print('avg green',final_green)
		print('avg red ',final_red)
		print('avg white ',final_white)

		return numpy.array((final_blue, final_green, final_red, final_white))
		#return finalCoords

	def capture(self, ignore1, ignore2): # pythonmaster
		camera = picamera.PiCamera()
		res = -1
		#while True:
		with picamera.array.PiRGBArray(camera) as stream:
			camera.capture(stream, format='rgb')
			img = stream.array
			img, coords, circleDebug = circle_detection(img, 20, 25)
			im = Image.fromarray(img) #.convert('LA')
			input_img = im
			imDeb = Image.fromarray(circleDebug) #.convert('LA')
			im.save('./tmp.png')
			imDeb.save('./deb.png')

			res = self.checkCoords(img, coords)
			print("res ", res)
			if isinstance(res, (numpy.ndarray, numpy.generic) ):
				src = numpy.array((
					(0, 0), #upper left
					(0, 800), #lower left
					(800, 800), #lright
					(800, 0) #uright
				))

				transformer = tf.ProjectiveTransform()
				transformer.estimate(src, res)
				transformed_image = tf.warp(input_img, transformer, output_shape = (800,800))

				#print("Transformed Image: ",transformed_image)

				scipy.misc.imsave('./transformed.png', transformed_image)
				self.pixmap = QPixmap("checkers_randlos.jpg");
				self.lbl.setPixmap(self.pixmap)
				#break
			else: print('fuckshitsuck')
		camera.close()
		print(self.corners)

if __name__ == '__main__':

	app = QApplication(sys.argv)

	screen_rect = app.desktop().screenGeometry()
	viewer = DraughtsGameWindow() # init ui
	# capture
	sys.exit(app.exec_())<|endoftext|>


<commit_before>import math
import pandas as pd
from google.cloud import bigquery
from IPython.display import display

from learntools.core import *

# Setup
client = bigquery.Client()

# (1) TaxiDemand
avg_num_trips_query = """
                      WITH trips_by_day AS
                      (
                      SELECT DATE(trip_start_timestamp) AS trip_date,
                          COUNT(*) as num_trips
                      FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                      WHERE trip_start_timestamp >= '2016-01-01' AND trip_start_timestamp < '2018-01-01'
                      GROUP BY trip_date
                      ORDER BY trip_date
                      )
                      SELECT trip_date,
                          AVG(num_trips)
                          OVER (
                               ORDER BY trip_date
                               ROWS BETWEEN 15 PRECEDING AND 15 FOLLOWING
                               ) AS avg_num_trips
                      FROM trips_by_day
                      """
avg_num_trips_answer = client.query(avg_num_trips_query).result().to_dataframe()

# (2) CommunityArea
trip_number_query = """
                    SELECT pickup_community_area,
                        trip_start_timestamp,
                        trip_end_timestamp,
                        RANK()
                            OVER (
                                  PARTITION BY pickup_community_area
                                  ORDER BY trip_start_timestamp
                                 ) AS trip_number
                    FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                    WHERE DATE(trip_start_timestamp) = '2017-05-01'
                    """
trip_number_answer = client.query(trip_number_query).result().to_dataframe()

# (3) BreakTime
break_time_query = """
                   SELECT taxi_id,
                       trip_start_timestamp,
                       trip_end_timestamp,
                       TIMESTAMP_DIFF(
                           trip_start_timestamp,
                           LAG(trip_end_timestamp, 1) OVER (PARTITION BY taxi_id ORDER BY trip_start_timestamp),
                           MINUTE) as prev_break
                   FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                   WHERE DATE(trip_start_timestamp) = '2017-05-01'
                   """
break_time_answer = client.query(break_time_query).result().to_dataframe()

# (1)
class TaxiDemand(CodingProblem):
    _var = 'avg_num_trips_query'

    def check(self, query):
        assert (type(query) == str), ("You don't have a valid query yet. Try again.")
        # check 1: words appear in query
        assert ("over" in query.lower()), ('Your query is missing an **OVER** clause.')
        assert ("15" in query.lower()), ("Your window should include the current date, along with the preceding 15 days and the following 15 days.")
        assert ("avg" in query.lower()), ("Your query should calculate a rolling average.  For this, you need to use the **AVG()** function.")
        # EXPERIMENTAL: get results
        try:
            results = client.query(query).result().to_dataframe()
        except:
            assert False, "Your query didn't run."
        display(results.head())
        # check 2: column names
        lowered_colnames = [c.lower() for c in results.columns]
        assert ('trip_date' in results.columns), ("You didn't select the `trip_date` column. Try again.")
        assert ('avg_num_trips' in results.columns), ("You didn't select the `avg_num_trips` column. Try again.")
        # check 3: length of df
        assert (len(results) == len(avg_num_trips_answer)), ("Your results do not seem to have the correct dates.  You should have one entry "
                                                             "for each date from January 1, 2016, to December 31, 2017.  Note that the provided partial "
                                                             "query already selects the appropriate dates for you (in the `trips_by_day` column).")
        # check 4: check specific value in df
        # get a date to check
        first_date = list(avg_num_trips_answer['trip_date'])[0]
        # get corresponding value
        correct_number = avg_num_trips_answer.loc[avg_num_trips_answer['trip_date']==first_date]['avg_num_trips'].values[0]
        # want this to equal the corresponding value above
        check_number = results.loc[results['trip_date']==first_date]['avg_num_trips'].values[0]
        assert (int(check_number)==int(correct_number)), ("The results don't look right. Try again.")

    _solution = CS(\
"""
avg_num_trips_query = \"""
                      WITH trips_by_day AS
                      (
                      SELECT DATE(trip_start_timestamp) AS trip_date,
                          COUNT(*) as num_trips
                      FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                      WHERE trip_start_timestamp >= '2016-01-01' AND trip_start_timestamp < '2018-01-01'
                      GROUP BY trip_date
                      ORDER BY trip_date
                      )
                      SELECT trip_date,
                          AVG(num_trips)
                          OVER (
                               ORDER BY trip_date
                               ROWS BETWEEN 15 PRECEDING AND 15 FOLLOWING
                               ) AS avg_num_trips
                      FROM trips_by_day
                      \"""

avg_num_trips_result = client.query(avg_num_trips_query).result().to_dataframe()
"""
    )
    _hint = ("Use the **AVG()** function. Write an **OVER** clause with that orders the rows with the `trip_date` column and uses a window that "
             "includes the 15 preceding rows, the current row, and the following 15 rows.")

# (2)
class CommunityArea(CodingProblem):
    _vars = ['trip_number_query', 'trip_number_result']
    def check(self, query, results):
        assert (type(query) == str), ("You don't have a valid query yet. Try again.")
        # check 1: query contains certain words
        assert ('rank' in query.lower()), ("There are many different numbering functions that enumerate the rows in the input. "
                                           "For this exercise, please use the **RANK()** function.")
        # check 2: correct columns selected
        assert ('pickup_community_area' in set(results.columns)), ("You didn't select the `pickup_community_area` column.")
        assert ('trip_start_timestamp' in set(results.columns)), ("You didn't select the `trip_start_timestamp` column.")
        assert ('trip_end_timestamp' in set(results.columns)), ("You didn't select the `trip_end_timestamp` column.")
        assert ('trip_number' in set(results.columns)), ("You didn't select the `trip_number` column.")
        # check 3: length of df
        assert (len(results)==len(trip_number_answer)), ("Your answer does not have the correct number of rows.")
        # check 4: specific value
        correct_number = len(trip_number_answer.loc[trip_number_answer["trip_number"]==3])
        check_number = len(results.loc[results["trip_number"]==3])
        assert (int(check_number)==int(correct_number)), ("The results don't look right. Try again.")

    _solution = CS( \
"""
trip_number_query = \"""
                    SELECT pickup_community_area,
                        trip_start_timestamp,
                        trip_end_timestamp,
                        RANK()
                            OVER (
                                  PARTITION BY pickup_community_area
                                  ORDER BY trip_start_timestamp
                                 ) AS trip_number
                    FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                    WHERE DATE(trip_start_timestamp) = '2017-05-01'
                    \"""

trip_number_result = client.query(trip_number_query).result().to_dataframe()
"""
)
    _hint = ("You don't need a **GROUP BY** clause.  Use the **RANK()** function.  Your **OVER** clause should order the rows by the "
             "`trip_start_timestamp` column and break the data into partitions based on `pickup_community_area`.")

class BreakTime(CodingProblem):
    _vars = ['break_time_query', 'break_time_result']
    def check(self, query, results):
        assert (type(query) == str), ("You don't have a valid query yet. Try again.")
        # check 1: query contains certain words
        assert ('lag' in query.lower()), ("Use the **LAG()** function to pull the value for `trip_end_timestamp` from the previous row.")
        # check 2: correct columns selected
        assert ('taxi_id' in set(results.columns)), ("You didn't select the `taxi_id` column.")
        assert ('trip_start_timestamp' in set(results.columns)), ("You didn't select the `trip_start_timestamp` column.")
        assert ('trip_end_timestamp' in set(results.columns)), ("You didn't select the `trip_end_timestamp` column.")
        assert ('prev_break' in set(results.columns)), ("You didn't select the `prev_break` column.")
        # check 3: check values, length of dataframe
        assert (len(results)==len(break_time_answer)), ("Your answer does not have the correct number of rows.")
        # check 4: specific number
        id_to_check = list(break_time_answer["taxi_id"])[0]
        correct_ans = [int(i) for i in list(break_time_answer.loc[break_time_answer["taxi_id"] == id_to_check]["prev_break"]) if math.isnan(i)==False]
        submitted_ans = [int(i) for i in list(results.loc[results["taxi_id"] == id_to_check]["prev_break"]) if math.isnan(i)==False]
        if len(correct_ans) > 0:
          assert (min(correct_ans)==min(submitted_ans)), ("The results don't look right. Try again.")
          assert (max(correct_ans)==max(submitted_ans)), ("The results don't look right. Try again.")

    _solution = CS( \
"""
break_time_query = \"""
                   SELECT taxi_id,
                       trip_start_timestamp,
                       trip_end_timestamp,
                       TIMESTAMP_DIFF(
                           trip_start_timestamp,
                           LAG(trip_end_timestamp, 1) OVER (PARTITION BY taxi_id ORDER BY trip_start_timestamp),
                           MINUTE) as prev_break
                   FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                   WHERE DATE(trip_start_timestamp) = '2017-05-01'
                   \"""

break_time_result = client.query(break_time_query).result().to_dataframe()
"""
)
    _hint = ("The `TIMESTAMP_DIFF()` function takes three arguments, where the first (`trip_start_timestamp`) and the last (`MINUTE`) "
             "are provided for you.  This function provides the time difference (in minutes) of the timestamps in the first two arguments. "
             "You need only fill in the second argument, which should use the **LAG()** function to pull the timestamp corresponding to "
             "the end of the previous trip (for the same `taxi_id`).")

qvars = bind_exercises(globals(), [
    TaxiDemand,
    CommunityArea,
    BreakTime
    ],
    tutorial_id=-1,
    var_format='q_{n}',
    )

__all__ = list(qvars)
<commit_msg>edits to checking code<commit_after>import math
import pandas as pd
from google.cloud import bigquery
from IPython.display import display

from learntools.core import *

# Setup
client = bigquery.Client()

# (1) TaxiDemand
avg_num_trips_query = """
                      WITH trips_by_day AS
                      (
                      SELECT DATE(trip_start_timestamp) AS trip_date,
                          COUNT(*) as num_trips
                      FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                      WHERE trip_start_timestamp >= '2016-01-01' AND trip_start_timestamp < '2018-01-01'
                      GROUP BY trip_date
                      ORDER BY trip_date
                      )
                      SELECT trip_date,
                          AVG(num_trips)
                          OVER (
                               ORDER BY trip_date
                               ROWS BETWEEN 15 PRECEDING AND 15 FOLLOWING
                               ) AS avg_num_trips
                      FROM trips_by_day
                      """
avg_num_trips_answer = client.query(avg_num_trips_query).result().to_dataframe()

# (2) CommunityArea
trip_number_query = """
                    SELECT pickup_community_area,
                        trip_start_timestamp,
                        trip_end_timestamp,
                        RANK()
                            OVER (
                                  PARTITION BY pickup_community_area
                                  ORDER BY trip_start_timestamp
                                 ) AS trip_number
                    FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                    WHERE DATE(trip_start_timestamp) = '2017-05-01'
                    """
trip_number_answer = client.query(trip_number_query).result().to_dataframe()

# (3) BreakTime
break_time_query = """
                   SELECT taxi_id,
                       trip_start_timestamp,
                       trip_end_timestamp,
                       TIMESTAMP_DIFF(
                           trip_start_timestamp,
                           LAG(trip_end_timestamp, 1) OVER (PARTITION BY taxi_id ORDER BY trip_start_timestamp),
                           MINUTE) as prev_break
                   FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                   WHERE DATE(trip_start_timestamp) = '2017-05-01'
                   """
break_time_answer = client.query(break_time_query).result().to_dataframe()

def run_query(query):
    try:
        results = client.query(query).result().to_dataframe()
    except:
        assert False, "You don't have a valid query yet.  Try again."
    display(results.head())
    return results

# (1)
class TaxiDemand(CodingProblem):
    _var = 'avg_num_trips_query'

    def check(self, query):
        results = run_query(query)
        # check 1: words appear in query
        assert ("over" in query.lower()), ('Your query is missing an **OVER** clause.')
        assert ("15" in query.lower()), ("Your window should include the current date, along with the preceding 15 days and the following 15 days.")
        assert ("avg" in query.lower()), ("Your query should calculate a rolling average.  For this, you need to use the **AVG()** function.")
        # check 2: column names
        lowered_colnames = [c.lower() for c in results.columns]
        assert ('trip_date' in results.columns), ("You didn't select the `trip_date` column. Try again.")
        assert ('avg_num_trips' in results.columns), ("You didn't select the `avg_num_trips` column. Try again.")
        # check 3: length of df
        assert (len(results) == len(avg_num_trips_answer)), ("Your results do not seem to have the correct dates.  You should have one entry "
                                                             "for each date from January 1, 2016, to December 31, 2017.  Note that the provided partial "
                                                             "query already selects the appropriate dates for you (in the `trips_by_day` column).")
        # check 4: check specific value in df
        # get a date to check
        first_date = list(avg_num_trips_answer['trip_date'])[0]
        # get corresponding value
        correct_number = avg_num_trips_answer.loc[avg_num_trips_answer['trip_date']==first_date]['avg_num_trips'].values[0]
        # want this to equal the corresponding value above
        check_number = results.loc[results['trip_date']==first_date]['avg_num_trips'].values[0]
        assert (int(check_number)==int(correct_number)), ("The results don't look right. Try again.")

    _solution = CS(\
"""
avg_num_trips_query = \"""
                      WITH trips_by_day AS
                      (
                      SELECT DATE(trip_start_timestamp) AS trip_date,
                          COUNT(*) as num_trips
                      FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                      WHERE trip_start_timestamp >= '2016-01-01' AND trip_start_timestamp < '2018-01-01'
                      GROUP BY trip_date
                      ORDER BY trip_date
                      )
                      SELECT trip_date,
                          AVG(num_trips)
                          OVER (
                               ORDER BY trip_date
                               ROWS BETWEEN 15 PRECEDING AND 15 FOLLOWING
                               ) AS avg_num_trips
                      FROM trips_by_day
                      \"""
"""
    )
    _hint = ("Use the **AVG()** function. Write an **OVER** clause with that orders the rows with the `trip_date` column and uses a window that "
             "includes the 15 preceding rows, the current row, and the following 15 rows.")

# (2)
class CommunityArea(CodingProblem):
    _vars = ['trip_number_query', 'trip_number_result']
    def check(self, query, results):
        assert (type(query) == str), ("You don't have a valid query yet. Try again.")
        # check 1: query contains certain words
        assert ('rank' in query.lower()), ("There are many different numbering functions that enumerate the rows in the input. "
                                           "For this exercise, please use the **RANK()** function.")
        # check 2: correct columns selected
        assert ('pickup_community_area' in set(results.columns)), ("You didn't select the `pickup_community_area` column.")
        assert ('trip_start_timestamp' in set(results.columns)), ("You didn't select the `trip_start_timestamp` column.")
        assert ('trip_end_timestamp' in set(results.columns)), ("You didn't select the `trip_end_timestamp` column.")
        assert ('trip_number' in set(results.columns)), ("You didn't select the `trip_number` column.")
        # check 3: length of df
        assert (len(results)==len(trip_number_answer)), ("Your answer does not have the correct number of rows.")
        # check 4: specific value
        correct_number = len(trip_number_answer.loc[trip_number_answer["trip_number"]==3])
        check_number = len(results.loc[results["trip_number"]==3])
        assert (int(check_number)==int(correct_number)), ("The results don't look right. Try again.")

    _solution = CS( \
"""
trip_number_query = \"""
                    SELECT pickup_community_area,
                        trip_start_timestamp,
                        trip_end_timestamp,
                        RANK()
                            OVER (
                                  PARTITION BY pickup_community_area
                                  ORDER BY trip_start_timestamp
                                 ) AS trip_number
                    FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                    WHERE DATE(trip_start_timestamp) = '2017-05-01'
                    \"""

trip_number_result = client.query(trip_number_query).result().to_dataframe()
"""
)
    _hint = ("You don't need a **GROUP BY** clause.  Use the **RANK()** function.  Your **OVER** clause should order the rows by the "
             "`trip_start_timestamp` column and break the data into partitions based on `pickup_community_area`.")

class BreakTime(CodingProblem):
    _vars = ['break_time_query', 'break_time_result']
    def check(self, query, results):
        assert (type(query) == str), ("You don't have a valid query yet. Try again.")
        # check 1: query contains certain words
        assert ('lag' in query.lower()), ("Use the **LAG()** function to pull the value for `trip_end_timestamp` from the previous row.")
        # check 2: correct columns selected
        assert ('taxi_id' in set(results.columns)), ("You didn't select the `taxi_id` column.")
        assert ('trip_start_timestamp' in set(results.columns)), ("You didn't select the `trip_start_timestamp` column.")
        assert ('trip_end_timestamp' in set(results.columns)), ("You didn't select the `trip_end_timestamp` column.")
        assert ('prev_break' in set(results.columns)), ("You didn't select the `prev_break` column.")
        # check 3: check values, length of dataframe
        assert (len(results)==len(break_time_answer)), ("Your answer does not have the correct number of rows.")
        # check 4: specific number
        id_to_check = list(break_time_answer["taxi_id"])[0]
        correct_ans = [int(i) for i in list(break_time_answer.loc[break_time_answer["taxi_id"] == id_to_check]["prev_break"]) if math.isnan(i)==False]
        submitted_ans = [int(i) for i in list(results.loc[results["taxi_id"] == id_to_check]["prev_break"]) if math.isnan(i)==False]
        if len(correct_ans) > 0:
          assert (min(correct_ans)==min(submitted_ans)), ("The results don't look right. Try again.")
          assert (max(correct_ans)==max(submitted_ans)), ("The results don't look right. Try again.")

    _solution = CS( \
"""
break_time_query = \"""
                   SELECT taxi_id,
                       trip_start_timestamp,
                       trip_end_timestamp,
                       TIMESTAMP_DIFF(
                           trip_start_timestamp,
                           LAG(trip_end_timestamp, 1) OVER (PARTITION BY taxi_id ORDER BY trip_start_timestamp),
                           MINUTE) as prev_break
                   FROM `bigquery-public-data.chicago_taxi_trips.taxi_trips`
                   WHERE DATE(trip_start_timestamp) = '2017-05-01'
                   \"""

break_time_result = client.query(break_time_query).result().to_dataframe()
"""
)
    _hint = ("The `TIMESTAMP_DIFF()` function takes three arguments, where the first (`trip_start_timestamp`) and the last (`MINUTE`) "
             "are provided for you.  This function provides the time difference (in minutes) of the timestamps in the first two arguments. "
             "You need only fill in the second argument, which should use the **LAG()** function to pull the timestamp corresponding to "
             "the end of the previous trip (for the same `taxi_id`).")

qvars = bind_exercises(globals(), [
    TaxiDemand,
    CommunityArea,
    BreakTime
    ],
    tutorial_id=-1,
    var_format='q_{n}',
    )

__all__ = list(qvars)
<|endoftext|>


<commit_before>"""
Centrality pipes for quantifying node-based topology measurements

Created by: Ankit Khambhati

Change Log
----------
2016/03/06 - Implemented
"""

from __future__ import division
import numpy as np

from errors import check_type
from base import NodeTopoPipe

class EvecCentral(NodeTopoPipe):
    """
    EvecCentral class for computing eigenvector centrality of the nodes
    """

    def __init__(self):
        self = self

    def _pipe_as_flow(self, signal_packet):
        # Get signal_packet details
        hkey = signal_packet.keys()[0]
        adj = signal_packet[hkey]['data']

        # Add 1s along the diagonal to make positive definite
        adj[np.diag_indices_from(adj)] = 1

        # Compute eigenvalues and eigenvectors, ensure they are real
        eigval, eigvec = np.linalg.eig(adj)
        eigval = np.real(eigval)
        eigvec = np.real(eigvec)

        # Sort largest to smallest eigenvalue
        sorted_idx = np.argsort(eigval)[::-1]
        largest_idx = sorted_idx[0]
        centrality = np.abs(eigvec[:, largest_idx])
        centrality = centrality.reshape(-1, 1)

        # Dump into signal_packet
        new_packet = {}
        new_packet[hkey] = {
            'data': centrality,
            'meta': {
                'ax_0': signal_packet[hkey]['meta']['ax_0'],
                'time': signal_packet[hkey]['meta']['time']
            }
        }

        return new_packet
<commit_msg>Implemented degree centrality and synchronizing centrality<commit_after>"""
Centrality pipes for quantifying node-based topology measurements

Created by: Ankit Khambhati

Change Log
----------
2016/03/06 - Implemented
"""

from __future__ import division
import numpy as np

from base import NodeTopoPipe

class DegrCentral(NodeTopoPipe):
    """
    DegrCentral class for computing weighted degree centrality of the nodes
    """

    def __init__(self):
        self = self

    def _pipe_as_flow(self, signal_packet):
        # Get signal_packet details
        hkey = signal_packet.keys()[0]
        adj = signal_packet[hkey]['data']

        centrality = np.sum(adj, axis=0).reshape(-1, 1)

        # Dump into signal_packet
        new_packet = {}
        new_packet[hkey] = {
            'data': centrality,
            'meta': {
                'ax_0': signal_packet[hkey]['meta']['ax_0'],
                'time': signal_packet[hkey]['meta']['time']
            }
        }

        return new_packet

class EvecCentral(NodeTopoPipe):
    """
    EvecCentral class for computing eigenvector centrality of the nodes
    """

    def __init__(self):
        self = self

    def _pipe_as_flow(self, signal_packet):
        # Get signal_packet details
        hkey = signal_packet.keys()[0]
        adj = signal_packet[hkey]['data']

        # Add 1s along the diagonal to make positive definite
        adj[np.diag_indices_from(adj)] = 1

        # Compute eigenvalues and eigenvectors, ensure they are real
        eigval, eigvec = np.linalg.eig(adj)
        eigval = np.real(eigval)
        eigvec = np.real(eigvec)

        # Sort largest to smallest eigenvalue
        sorted_idx = np.argsort(eigval)[::-1]
        largest_idx = sorted_idx[0]
        centrality = np.abs(eigvec[:, largest_idx])
        centrality = centrality.reshape(-1, 1)

        # Dump into signal_packet
        new_packet = {}
        new_packet[hkey] = {
            'data': centrality,
            'meta': {
                'ax_0': signal_packet[hkey]['meta']['ax_0'],
                'time': signal_packet[hkey]['meta']['time']
            }
        }

        return new_packet

class SyncCentral(NodeTopoPipe):
    """
    SyncCentral class for computing synchronizing/desynchronizing centrality
    of the nodes
    """

    def __init__(self):
        self = self

    def _pipe_as_flow(self, signal_packet):
        def global_sync(adj_matr):
            """Compute synchronizability"""

            # Get the degree vector of the adj
            deg_vec = np.sum(adj_matr, axis=0)
            deg_matr = np.diag(deg_vec)

            # Laplacian
            lapl = deg_matr - adj_matr

            # Compute eigenvalues and eigenvectors, ensure they are real
            eigval, eigvec = np.linalg.eig(lapl)
            eigval = np.real(eigval)

            # Sort smallest to largest eigenvalue
            eigval = np.sort(eigval)
            sync = np.abs(eigval[1] / eigval[-1]).reshape(1, 1)

            return sync

        # Get signal_packet details
        hkey = signal_packet.keys()[0]
        adj = signal_packet[hkey]['data']

        centrality = []
        base_sync = global_sync(adj)
        for node_r in np.arange(adj.shape[0]):
            adj_mod = adj.copy()
            adj_mod = np.delete(adj_mod, (node_r), axis=0)
            adj_mod = np.delete(adj_mod, (node_r), axis=1)

            mod_sync = global_sync(adj_mod)
            centrality.append((mod_sync-base_sync) / base_sync)
        centrality = np.array(centrality).reshape(-1, 1)

        # Dump into signal_packet
        new_packet = {}
        new_packet[hkey] = {
            'data': centrality,
            'meta': {
                'ax_0': signal_packet[hkey]['meta']['ax_0'],
                'time': signal_packet[hkey]['meta']['time']
            }
        }

        return new_packet
<|endoftext|>


<commit_before>#!/usr/bin/evn python
# encoding: utf-8

from pprof.project import ProjectFactory, log
from pprof.settings import config
from group import PprofGroup

from os import path
from glob import glob
from plumbum import FG, local
from plumbum.cmd import cat

class Crocopat(PprofGroup):

    """ crocopat benchmark """

    class Factory:

        def create(self, exp):
            return Crocopat(exp, "crocopat", "verification")
    ProjectFactory.addFactory("Crocopat", Factory())

    def run_tests(self, experiment):
        from pprof.project import wrap

        exp = wrap(self.run_f)

        programs = glob(path.join(self.testdir, "programs", "*.rml"))
        projects = glob(path.join(self.testdir, "projects", "*.rsf"))
        for program in programs:
            for project in projects:
                (cat[project] | exp[program]) & FG(retcode=None)

    src_dir = "crocopat-2.1.4"
    src_file = src_dir + ".zip"
    src_uri = "http://crocopat.googlecode.com/files/" + src_file

    def download(self):
        from pprof.utils.downloader import Wget
        from plumbum.cmd import unzip

        crocopat_dir = path.join(self.builddir, self.src_dir)
        with local.cwd(self.builddir):
            Wget(self.src_uri, self.src_file)
            unzip(path.join(self.builddir, self.src_file))

    def configure(self):
        pass

    def build(self):
        from plumbum.cmd import make
        from pprof.utils.compiler import clang_cxx

        crocopat_dir = path.join(self.builddir, self.src_dir, "src")
        with local.cwd(crocopat_dir):
            cflags = self.cflags + ["-I.", "-ansi"]
            ldflags = self.ldflags + ["-L.", "-lrelbdd"]
            make["CXX=" + str(clang_cxx()),
                 "CFLAGS=" + " ".join(cflags),
                 "LFLAGS=" + " ".join(ldflags)] & FG
<commit_msg>crocopat: cleanup usage of compiler<commit_after>#!/usr/bin/evn python
# encoding: utf-8

from pprof.project import ProjectFactory, log
from pprof.settings import config
from group import PprofGroup

from os import path
from glob import glob
from plumbum import FG, local
from plumbum.cmd import cat

class Crocopat(PprofGroup):

    """ crocopat benchmark """

    class Factory:

        def create(self, exp):
            return Crocopat(exp, "crocopat", "verification")
    ProjectFactory.addFactory("Crocopat", Factory())

    def run_tests(self, experiment):
        from pprof.project import wrap

        exp = wrap(self.run_f)

        programs = glob(path.join(self.testdir, "programs", "*.rml"))
        projects = glob(path.join(self.testdir, "projects", "*.rsf"))
        for program in programs:
            for project in projects:
                (cat[project] | exp[program]) & FG(retcode=None)

    src_dir = "crocopat-2.1.4"
    src_file = src_dir + ".zip"
    src_uri = "http://crocopat.googlecode.com/files/" + src_file

    def download(self):
        from pprof.utils.downloader import Wget
        from plumbum.cmd import unzip

        crocopat_dir = path.join(self.builddir, self.src_dir)
        with local.cwd(self.builddir):
            Wget(self.src_uri, self.src_file)
            unzip(path.join(self.builddir, self.src_file))

    def configure(self):
        pass

    def build(self):
        from plumbum.cmd import make
        from pprof.utils.compiler import lt_clang_cxx

        crocopat_dir = path.join(self.builddir, self.src_dir, "src")
        with local.cwd(crocopat_dir):
            cflags = self.cflags + ["-I.", "-ansi"]
            ldflags = self.ldflags + ["-L.", "-lrelbdd"]
            with local.cwd(self.builddir):
                clang_cxx = lt_clang_cxx(cflags, ldflags)
            make("CXX=" + str(clang_cxx))
<|endoftext|>


<commit_before># Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tool for uploading Google Code issues to GitHub.

  Issue migration from Google Code to GitHub.
  This tools allows you to easily migrate your downloaded Google Code issues to
  GitHub.

  To use this tool see the documentation available at:
  https://code.google.com/p/support-tools/wiki/IssueExporterTool
"""

import argparse
import sys

import github_services
import issues

def ExportIssues(github_owner_username, github_repo_name, github_oauth_token,
                 issue_file_path, project_name, user_file_path, rate_limit):
  """Exports all issues for a given project."""
  github_service = github_services.GitHubService(
      github_owner_username, github_repo_name, github_oauth_token,
      rate_limit)
  issue_service = github_services.IssueService(github_service)
  user_service = github_services.UserService(github_service)

  issue_data = issues.LoadIssueData(issue_file_path, project_name)
  user_map = issues.LoadUserData(user_file_path, user_service)

  # Add a special "user_requesting_export" user, which comes in handy.
  user_map["user_requesting_export"] = github_owner_username

  issue_exporter = issues.IssueExporter(
      issue_service, user_service, issue_data, project_name, user_map)

  try:
    issue_exporter.Init()
    issue_exporter.Start()
    print "\nDone!\n"
  except IOError, e:
    print "[IOError] ERROR: %s" % e
  except issues.InvalidUserError, e:
    print "[InvalidUserError] ERROR: %s" % e

def main(args):
  """The main function.

  Args:
    args: The command line arguments.

  Raises:
    ProjectNotFoundError: The user passed in an invalid project name.
  """
  parser = argparse.ArgumentParser()
  parser.add_argument("--github_oauth_token", required=True,
                      help="You can generate an oauth token here: "
                      "https://github.com/settings/applications")
  parser.add_argument("--github_owner_username", required=True,
                      help="The project ownsers GitHub username")
  parser.add_argument("--github_repo_name", required=True,
                      help="The GitHub repository you wish to add the issues"
                      "to.")
  parser.add_argument("--issue_file_path", required=True,
                      help="The path to the file containing the issues from"
                      "Google Code.")
  parser.add_argument("--project_name", required=True,
                      help="The name of the Google Code project you wish to"
                      "export")
  parser.add_argument("--user_file_path", required=False,
                      help="The path to the file containing a mapping from"
                      "email address to github username.")
  parser.add_argument("--rate_limit", required=False, default="True",
                     help="Rate limit GitHub requests to not run into"
                     "anti-abuse limits.")
  parsed_args, _ = parser.parse_known_args(args)

  ExportIssues(
      parsed_args.github_owner_username, parsed_args.github_repo_name,
      parsed_args.github_oauth_token, parsed_args.issue_file_path,
      parsed_args.project_name, parsed_args.user_file_path,
      parsed_args.rate_limit)

if __name__ == "__main__":
  main(sys.argv)
<commit_msg>Fix spelling error<commit_after># Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tool for uploading Google Code issues to GitHub.

  Issue migration from Google Code to GitHub.
  This tools allows you to easily migrate your downloaded Google Code issues to
  GitHub.

  To use this tool see the documentation available at:
  https://code.google.com/p/support-tools/wiki/IssueExporterTool
"""

import argparse
import sys

import github_services
import issues

def ExportIssues(github_owner_username, github_repo_name, github_oauth_token,
                 issue_file_path, project_name, user_file_path, rate_limit):
  """Exports all issues for a given project."""
  github_service = github_services.GitHubService(
      github_owner_username, github_repo_name, github_oauth_token,
      rate_limit)
  issue_service = github_services.IssueService(github_service)
  user_service = github_services.UserService(github_service)

  issue_data = issues.LoadIssueData(issue_file_path, project_name)
  user_map = issues.LoadUserData(user_file_path, user_service)

  # Add a special "user_requesting_export" user, which comes in handy.
  user_map["user_requesting_export"] = github_owner_username

  issue_exporter = issues.IssueExporter(
      issue_service, user_service, issue_data, project_name, user_map)

  try:
    issue_exporter.Init()
    issue_exporter.Start()
    print "\nDone!\n"
  except IOError, e:
    print "[IOError] ERROR: %s" % e
  except issues.InvalidUserError, e:
    print "[InvalidUserError] ERROR: %s" % e

def main(args):
  """The main function.

  Args:
    args: The command line arguments.

  Raises:
    ProjectNotFoundError: The user passed in an invalid project name.
  """
  parser = argparse.ArgumentParser()
  parser.add_argument("--github_oauth_token", required=True,
                      help="You can generate an oauth token here: "
                      "https://github.com/settings/applications")
  parser.add_argument("--github_owner_username", required=True,
                      help="The project owner's GitHub username")
  parser.add_argument("--github_repo_name", required=True,
                      help="The GitHub repository you wish to add the issues"
                      "to.")
  parser.add_argument("--issue_file_path", required=True,
                      help="The path to the file containing the issues from"
                      "Google Code.")
  parser.add_argument("--project_name", required=True,
                      help="The name of the Google Code project you wish to"
                      "export")
  parser.add_argument("--user_file_path", required=False,
                      help="The path to the file containing a mapping from"
                      "email address to github username.")
  parser.add_argument("--rate_limit", required=False, default="True",
                     help="Rate limit GitHub requests to not run into"
                     "anti-abuse limits.")
  parsed_args, _ = parser.parse_known_args(args)

  ExportIssues(
      parsed_args.github_owner_username, parsed_args.github_repo_name,
      parsed_args.github_oauth_token, parsed_args.issue_file_path,
      parsed_args.project_name, parsed_args.user_file_path,
      parsed_args.rate_limit)

if __name__ == "__main__":
  main(sys.argv)
<|endoftext|>


<commit_before>#!/usr/bin/env python3

import sys
import io
import os

import time
from time import sleep

import numpy
import matplotlib
from PIL import Image
from PyQt5.QtCore import *
import PyQt5.QtCore
from PyQt5.QtGui import *

from PyQt5.QtWidgets import *

from pyqt import FreakingQtImageViewer
import scipy.misc

from skimage import color
from skimage import transform as tf
#from skimage import data
from skimage.feature import canny
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter

from skimage.transform import hough_circle
from skimage.feature import peak_local_max
from skimage.draw import circle_perimeter
from skimage.util import img_as_ubyte

import scipy.misc

import picamera
import picamera.array

from cv import circle_detection
from _thread import start_new_thread

class DraughtsGameWindow(QWidget):
	def __init__(self):
		super().__init__()
		self.corners = []
		self.CELLS_PER_ROW = 8
		self.BLACK_COLOR = QColor(0,0,0,255)
		self.WHITE_COLOR = QColor(255,255,255,255)
		self.BLUE_COLOR = QColor(0,0,255,255)
		self.RED_COLOR = QColor(255,0,0,255)
		self.GREEN_COLOR = QColor(0,255,0,255)
		self.DEFAULT_PEN = QPen(self.BLUE_COLOR)
		self.DEFAULT_PEN.setWidth(20)
		self.PIX_PADDING = 75
		self.BORDER_RADIUS = 10

		self.initUI()

	def keyPressEvent(self, event):
		if event.key() == Qt.Key_Escape:
			self.close()
		elif event.key() == Qt.Key_C:
			start_new_thread(self.capture, (0, 0,))
		elif event.key() == Qt.Key_X:
				print("p0")
				self.pixmap = QPixmap("checkers_randlos.jpg");
				print("p1")
				self.pixmap.scaledToWidth( 800 );
				print("p2")
				self.lbl.setPixmap(self.pixmap)
				print("p3")
				self.vbox.setContentsMargins(240,112,240,112)
				print("p4")
				self.lbl.repaint()

	def initUI(self):

		self.WIDTH, self.HEIGHT, self.CELL_SIZE = screen_rect.width(), screen_rect.height(), screen_rect.width() / self.CELLS_PER_ROW
		self.pixmap = QPixmap(self.WIDTH-20,self.HEIGHT-20)

		self.upper_left = (self.PIX_PADDING + self.WIDTH / 2 - self.pixmap.height() / 2, self.PIX_PADDING)
		self.upper_right = (- self.PIX_PADDING + self.WIDTH / 2 + self.pixmap.height() / 2, self.PIX_PADDING + 0)
		self.lower_right = (-self.PIX_PADDING + self.WIDTH / 2 + self.pixmap.height() / 2, -self.PIX_PADDING + self.pixmap.height())
		self.lower_left = (self.PIX_PADDING + self.WIDTH / 2 - self.pixmap.height() / 2, -self.PIX_PADDING + self.pixmap.height())

		self.DRAW_SIZE = self.upper_right[0] - self.upper_left[0]
		print(self.DRAW_SIZE)

		self.painter = QPainter(self.pixmap)
		self.painter.setPen(self.DEFAULT_PEN)

		self.painter.fillRect(0, 0,self.WIDTH, self.HEIGHT, self.BLACK_COLOR)
		self.painter.drawEllipse(self.upper_left[0]-10,self.upper_left[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.WHITE_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.upper_right[0]-10,self.upper_right[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.RED_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.lower_right[0]-10,self.lower_right[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.GREEN_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.lower_left[0]-10,self.lower_left[1]-10,20,20)

		self.vbox = QVBoxLayout(self)
		self.lbl = QLabel(self)
		self.lbl.setPixmap(self.pixmap)
		self.vbox.addWidget(self.lbl)

		self.setStyleSheet("background-color:black;")

		self.setLayout(self.vbox)
		self.vbox.setAlignment(Qt.AlignHCenter)
		self.showFullScreen()
		self.setWindowTitle('DraughtsCV')
		self.show()
		#capture()

	def checkCoords(self, img, coords):
		self.redC = []
		self.greenC = []
		self.blueC = []
		self.whiteC = []
		for index, coord in enumerate(coords):
			#print(img[coord[0], coord[1]]," - ", )
			pixel = img[coord[0], coord[1]]
			sum = 0
			sum += pixel[0]
			sum += pixel[1]
			sum += pixel[2]
			'''if sum > 300:
				print()
				#print("delete ", coord, ' with ', pixel)
			else:'''
			print("pixel: ", pixel, "@",coord)
			if pixel[0] > 150 and pixel[1] > 150 and pixel[2] > 150:
				self.whiteC.append(coord)
			elif pixel[0] > 110:
				self.redC.append(coord)
			elif pixel[1] > 110:
				self.greenC.append(coord)
			elif pixel[2] > 110:
				self.blueC.append(coord)

		#final_red = (int(numpy.sum(self.redC[0])/len(self.redC)),int(numpy.sum(self.redC[1])/len(self.redC[])))	#(sum(self.redC[0]), sum(self.redC[1]))
		final_white = numpy.average(self.whiteC, axis=0)
		final_red = numpy.average(self.redC, axis=0)
		final_green = numpy.average(self.greenC, axis=0)
		final_blue = numpy.average(self.blueC, axis=0)

		self.lbl.setPixmap(self.pixmap)
		if len(self.whiteC) == 0 or len(self.blueC) == 0 or len(self.greenC) == 0 or len(self.redC) == 0 :
			print('less than 4 corners for calibration detected, returning -1')
			return -1

		final_white = (int(final_white[1]),int(final_white[0]))
		final_red = (int(final_red[1]),int(final_red[0]))
		final_green = (int(final_green[1]),int(final_green[0]))
		final_blue = (int(final_blue[1]),int(final_blue[0]))

		print('avg blue ',final_blue)
		print('avg green',final_green)
		print('avg red ',final_red)
		print('avg white ',final_white)

		return numpy.array((final_blue, final_green, final_red, final_white))
		#return finalCoords

	def capture(self, ignore1, ignore2): # pythonmaster
		camera = picamera.PiCamera()
		res = -1
		#while True:
		with picamera.array.PiRGBArray(camera) as stream:
			camera.capture(stream, format='rgb')
			img = stream.array
			img, coords, circleDebug = circle_detection(img, 20, 25)
			im = Image.fromarray(img) #.convert('LA')
			input_img = im
			imDeb = Image.fromarray(circleDebug) #.convert('LA')
			im.save('./tmp.png')
			imDeb.save('./deb.png')

			res = self.checkCoords(img, coords)
			print("res ", res)
			if isinstance(res, (numpy.ndarray, numpy.generic) ):
				src = numpy.array((
					(0, 0), #upper left
					(0, 800), #lower left
					(800, 800), #lright
					(800, 0) #uright
				))

				transformer = tf.ProjectiveTransform()
				transformer.estimate(src, res)
				transformed_image = tf.warp(input_img, transformer, output_shape = (800,800))

				#print("Transformed Image: ",transformed_image)

				scipy.misc.imsave('./transformed.png', transformed_image)
				#break
			else: print('fuckshitsuck')
		camera.close()
		print(self.corners)

if __name__ == '__main__':

	app = QApplication(sys.argv)

	screen_rect = app.desktop().screenGeometry()
	viewer = DraughtsGameWindow() # init ui
	# capture
	sys.exit(app.exec_())<commit_msg>threading<commit_after>#!/usr/bin/env python3

import sys
import io
import os

import time
from time import sleep

import numpy
import matplotlib
from PIL import Image
from PyQt5.QtCore import *
import PyQt5.QtCore
from PyQt5.QtGui import *

from PyQt5.QtWidgets import *

from pyqt import FreakingQtImageViewer
import scipy.misc

from skimage import color
from skimage import transform as tf
#from skimage import data
from skimage.feature import canny
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter

from skimage.transform import hough_circle
from skimage.feature import peak_local_max
from skimage.draw import circle_perimeter
from skimage.util import img_as_ubyte

import scipy.misc

import picamera
import picamera.array

from cv import circle_detection
from _thread import start_new_thread

class DraughtsGameWindow(QWidget):
	def __init__(self):
		super().__init__()
		self.corners = []
		self.CELLS_PER_ROW = 8
		self.BLACK_COLOR = QColor(0,0,0,255)
		self.WHITE_COLOR = QColor(255,255,255,255)
		self.BLUE_COLOR = QColor(0,0,255,255)
		self.RED_COLOR = QColor(255,0,0,255)
		self.GREEN_COLOR = QColor(0,255,0,255)
		self.DEFAULT_PEN = QPen(self.BLUE_COLOR)
		self.DEFAULT_PEN.setWidth(20)
		self.PIX_PADDING = 75
		self.BORDER_RADIUS = 10

		self.initUI()

	def keyPressEvent(self, event):
		if event.key() == Qt.Key_Escape:
			self.close()
		elif event.key() == Qt.Key_C:
			start_new_thread(self.capture, (0, 0,))
		elif event.key() == Qt.Key_X:
				print("p0")
				self.pixmap = QPixmap("checkers_randlos.jpg")
				self.pixmap = self.pixmap.scaledToWidth( 800 )
				self.lbl.setPixmap(self.pixmap)
				self.vbox.setContentsMargins(240,112,240,112)
				self.lbl.repaint()

	def initUI(self):

		self.WIDTH, self.HEIGHT, self.CELL_SIZE = screen_rect.width(), screen_rect.height(), screen_rect.width() / self.CELLS_PER_ROW
		self.pixmap = QPixmap(self.WIDTH-20,self.HEIGHT-20)

		self.upper_left = (self.PIX_PADDING + self.WIDTH / 2 - self.pixmap.height() / 2, self.PIX_PADDING)
		self.upper_right = (- self.PIX_PADDING + self.WIDTH / 2 + self.pixmap.height() / 2, self.PIX_PADDING + 0)
		self.lower_right = (-self.PIX_PADDING + self.WIDTH / 2 + self.pixmap.height() / 2, -self.PIX_PADDING + self.pixmap.height())
		self.lower_left = (self.PIX_PADDING + self.WIDTH / 2 - self.pixmap.height() / 2, -self.PIX_PADDING + self.pixmap.height())

		self.DRAW_SIZE = self.upper_right[0] - self.upper_left[0]
		print(self.DRAW_SIZE)

		self.painter = QPainter(self.pixmap)
		self.painter.setPen(self.DEFAULT_PEN)

		self.painter.fillRect(0, 0,self.WIDTH, self.HEIGHT, self.BLACK_COLOR)
		self.painter.drawEllipse(self.upper_left[0]-10,self.upper_left[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.WHITE_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.upper_right[0]-10,self.upper_right[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.RED_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.lower_right[0]-10,self.lower_right[1]-10,20,20)
		self.DEFAULT_PEN.setColor(self.GREEN_COLOR)
		self.painter.setPen(self.DEFAULT_PEN)
		self.painter.drawEllipse(self.lower_left[0]-10,self.lower_left[1]-10,20,20)

		self.vbox = QVBoxLayout(self)
		self.lbl = QLabel(self)
		self.lbl.setPixmap(self.pixmap)
		self.vbox.addWidget(self.lbl)

		self.setStyleSheet("background-color:black;")

		self.setLayout(self.vbox)
		self.vbox.setAlignment(Qt.AlignHCenter)
		self.showFullScreen()
		self.setWindowTitle('DraughtsCV')
		self.show()
		#capture()

	def checkCoords(self, img, coords):
		self.redC = []
		self.greenC = []
		self.blueC = []
		self.whiteC = []
		for index, coord in enumerate(coords):
			#print(img[coord[0], coord[1]]," - ", )
			pixel = img[coord[0], coord[1]]
			sum = 0
			sum += pixel[0]
			sum += pixel[1]
			sum += pixel[2]
			'''if sum > 300:
				print()
				#print("delete ", coord, ' with ', pixel)
			else:'''
			print("pixel: ", pixel, "@",coord)
			if pixel[0] > 150 and pixel[1] > 150 and pixel[2] > 150:
				self.whiteC.append(coord)
			elif pixel[0] > 110:
				self.redC.append(coord)
			elif pixel[1] > 110:
				self.greenC.append(coord)
			elif pixel[2] > 110:
				self.blueC.append(coord)

		#final_red = (int(numpy.sum(self.redC[0])/len(self.redC)),int(numpy.sum(self.redC[1])/len(self.redC[])))	#(sum(self.redC[0]), sum(self.redC[1]))
		final_white = numpy.average(self.whiteC, axis=0)
		final_red = numpy.average(self.redC, axis=0)
		final_green = numpy.average(self.greenC, axis=0)
		final_blue = numpy.average(self.blueC, axis=0)

		self.lbl.setPixmap(self.pixmap)
		if len(self.whiteC) == 0 or len(self.blueC) == 0 or len(self.greenC) == 0 or len(self.redC) == 0 :
			print('less than 4 corners for calibration detected, returning -1')
			return -1

		final_white = (int(final_white[1]),int(final_white[0]))
		final_red = (int(final_red[1]),int(final_red[0]))
		final_green = (int(final_green[1]),int(final_green[0]))
		final_blue = (int(final_blue[1]),int(final_blue[0]))

		print('avg blue ',final_blue)
		print('avg green',final_green)
		print('avg red ',final_red)
		print('avg white ',final_white)

		return numpy.array((final_blue, final_green, final_red, final_white))
		#return finalCoords

	def capture(self, ignore1, ignore2): # pythonmaster
		camera = picamera.PiCamera()
		res = -1
		#while True:
		with picamera.array.PiRGBArray(camera) as stream:
			camera.capture(stream, format='rgb')
			img = stream.array
			img, coords, circleDebug = circle_detection(img, 20, 25)
			im = Image.fromarray(img) #.convert('LA')
			input_img = im
			imDeb = Image.fromarray(circleDebug) #.convert('LA')
			im.save('./tmp.png')
			imDeb.save('./deb.png')

			res = self.checkCoords(img, coords)
			print("res ", res)
			if isinstance(res, (numpy.ndarray, numpy.generic) ):
				src = numpy.array((
					(0, 0), #upper left
					(0, 800), #lower left
					(800, 800), #lright
					(800, 0) #uright
				))

				transformer = tf.ProjectiveTransform()
				transformer.estimate(src, res)
				transformed_image = tf.warp(input_img, transformer, output_shape = (800,800))

				#print("Transformed Image: ",transformed_image)

				scipy.misc.imsave('./transformed.png', transformed_image)
				#break
			else: print('fuckshitsuck')
		camera.close()
		print(self.corners)

if __name__ == '__main__':

	app = QApplication(sys.argv)

	screen_rect = app.desktop().screenGeometry()
	viewer = DraughtsGameWindow() # init ui
	# capture
	sys.exit(app.exec_())<|endoftext|>


<commit_before>from collections import deque
import hashlib
import os.path
try:
    import urllib2
except ImportError:
    import urllib.request as urllib2

from libearth.compat import binary
from libearth.feed import Feed
from libearth.feedlist import (Feed as FeedOutline,
                               FeedCategory as CategoryOutline, FeedList)
from libearth.parser.autodiscovery import autodiscovery, FeedUrlNotFoundError
from libearth.parser.heuristic import get_format
from libearth.schema import read, write

from flask import Flask, jsonify, render_template, request, url_for

app = Flask(__name__)

app.config.update(dict(
    REPOSITORY='repo/',
    OPML='earthreader.opml'
))

def feedlist_exists():
    REPOSITORY = app.config['REPOSITORY']
    OPML = app.config['OPML']
    if not os.path.isfile(REPOSITORY + OPML):
        return False
    return True

def get_feedlist():
    REPOSITORY = app.config['REPOSITORY']
    OPML = app.config['OPML']
    if not os.path.isfile(REPOSITORY + OPML):
        if not os.path.isdir(REPOSITORY):
            os.mkdir(REPOSITORY)
        feed_list = FeedList()
        feed_list.save_file(REPOSITORY + OPML)
    feed_list = FeedList(REPOSITORY + OPML)
    return feed_list

def get_hash(name):
    return hashlib.sha1(binary(name)).hexdigest()

def get_all_feeds(category, parent_categories=[]):
    feeds = []
    categories = []
    if parent_categories:
        feed_path = '/'.join(parent_categories)
    else:
        feed_path = '/'
    for child in category:
        if isinstance(child, FeedOutline):
            feed_id = get_hash(child.xml_url)
            feeds.append({
                'title': child.title,
                'entries_url': url_for(
                    'feed_entries',
                    category_id=feed_path,
                    feed_id=feed_id,
                    _external=True
                ),
                'remove_feed_url': url_for(
                    'delete_feed',
                    category_id=feed_path,
                    feed_id=feed_id,
                    _external=True
                )
            })
        elif isinstance(child, CategoryOutline):
            categories.append({
                'title': child.title,
                'feeds_url': url_for(
                    'feeds',
                    category_id=feed_path + '/' + child.title
                    if parent_categories else child.title,
                    _external=True
                ),
                'entries_url': url_for(
                    'category_entries',
                    category_id=feed_path + '/' + child.title
                    if parent_categories else child.title,
                    _external=True
                ),
                'add_feed_url': url_for(
                    'add_feed',
                    category_id=feed_path + '/' + child.title
                    if parent_categories else child.title,
                    _external=True
                ),
                'add_category_url': url_for(
                    'add_category',
                    category_id=feed_path + '/' + child.title
                ),
                'remove_category_url': url_for(
                    'delete_category',
                    category_id=feed_path + '/' + child.title
                    if parent_categories else child.title,
                    _external=True
                ),
            })
    return feeds, categories

def check_path_valid(category_id, return_category_parent=False):
    if category_id == '/':
        feed_list = get_feedlist()
        return feed_list, feed_list, None
    if return_category_parent:
        category_list = category_id.split('/')
        target = category_list.pop()
        categories = deque(category_list)
    else:
        target = None
        categories = deque(category_id.split('/'))
    feed_list = get_feedlist()
    cursor = feed_list
    while categories:
        is_searched = False
        looking_for = categories.popleft()
        for category in cursor:
            if category.text == looking_for:
                is_searched = True
                cursor = category
                break
        if not is_searched:
            return None, None, None
    return feed_list, cursor, target

def find_feed_in_opml(feed_id, category, parent_categories=[], result=[]):
    categories = []
    if parent_categories:
        feed_path = '/'.join(parent_categories)
    else:
        feed_path = '/'
    for child in category:
        if isinstance(child, FeedOutline):
            current_feed_id = hashlib.sha1(binary(child.xml_url)).hexdigest()
            if current_feed_id == feed_id:
                result.append(feed_path)
        elif isinstance(child, CategoryOutline):
            categories.append(child)
    for category in categories:
        find_feed_in_opml(
            feed_id,
            category,
            parent_categories.append(category.title)
            if parent_categories else [category.title],
            result
        )
    return result

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html')

@app.route('/feeds/', defaults={'category_id': '/'})
@app.route('/<path:category_id>/feeds/')
def feeds(category_id):
    feed_list, cursor, _ = check_path_valid(category_id)
    if not feed_list:
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    feeds, categories = get_all_feeds(cursor, [category_id])
    return jsonify(feeds=feeds, categories=categories)

@app.route('/feeds/', methods=['POST'], defaults={'category_id': '/'})
@app.route('/<path:category_id>/feeds/', methods=['POST'])
def add_feed(category_id):
    REPOSITORY = app.config['REPOSITORY']
    feed_list, cursor, _ = check_path_valid(category_id)
    if (not isinstance(cursor, CategoryOutline) and
            not isinstance(cursor, FeedList)):
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    try:
        url = request.form['url']
        f = urllib2.urlopen(url)
        document = f.read()
    except ValueError:
        r = jsonify(
            error='unreachable-url',
            message='Cannot connect to given url'
        )
        r.status_code = 400
        return r
    try:
        feed_url = autodiscovery(document, url)
    except FeedUrlNotFoundError:
        r = jsonify(
            error='unreachable-feed-url',
            message='Cannot find feed url'
        )
        r.status_code = 400
        return r
    if not feed_url == url:
        f.close()
        f = urllib2.urlopen(feed_url)
        xml = f.read()
    else:
        xml = document
    format = get_format(xml)
    result = format(xml, feed_url)
    feed = result[0]
    outline = FeedOutline('atom', feed.title.value, feed_url)
    for link in feed.links:
            if link.relation == 'alternate' and \
                    link.mimetype == 'text/html':
                outline.blog_url = link.uri
    cursor.append(outline)
    feed_list.save_file()
    file_name = get_hash(feed.id) + '.xml'
    with open(os.path.join(REPOSITORY, file_name), 'w') as f:
        for chunk in write(feed, indent='    ', canonical_order=True):
            f.write(chunk)
    return feeds(category_id)

@app.route('/', methods=['POST'], defaults={'category_id': '/'})
@app.route('/<path:category_id>/', methods=['POST'])
def add_category(category_id):
    feed_list, cursor, _ = check_path_valid(category_id)
    if (not isinstance(cursor, CategoryOutline) and
            not isinstance(cursor, FeedList)):
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r

    title = request.form['title']
    outline = CategoryOutline(title)
    cursor.append(outline)
    feed_list.save_file()
    return feeds(category_id)

@app.route('/<path:category_id>/', methods=['DELETE'])
def delete_category(category_id):
    feed_list, cursor, target = check_path_valid(category_id, True)
    if not cursor:
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    for child in cursor:
        if isinstance(child, CategoryOutline):
            if child.text == target:
                cursor.remove(child)
    feed_list.save_file()
    index = category_id.rfind('/')
    if index == -1:
        return feeds('/')
    else:
        return feeds(category_id[:index])

@app.route('/feeds/<feed_id>/', methods=['DELETE'],
           defaults={'category_id': '/'})
@app.route('/<path:category_id>/feeds/<feed_id>/', methods=['DELETE'])
def delete_feed(category_id, feed_id):
    REPOSITORY = app.config['REPOSITORY']
    feed_list, cursor, _ = check_path_valid(category_id)
    if not cursor:
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    target = None
    for feed in cursor:
        if isinstance(feed, FeedOutline):
            if feed_id == hashlib.sha1(binary(feed.xml_url)).hexdigest():
                target = feed
    if target:
        cursor.remove(target)
    else:
        r = jsonify(
            error='feed-not-found-in-path',
            message='Given feed does not exists in the path'
        )
        r.status_code = 400
        return r
    feed_list.save_file()
    if not find_feed_in_opml(feed_id, feed_list):
        os.remove(REPOSITORY + feed_id + '.xml')
    return feeds(category_id)

@app.route('/feeds/<feed_id>/entries/', defaults={'category_id': '/'})
@app.route('/<path:category_id>/feeds/<feed_id>/entries/')
def feed_entries(category_id, feed_id):
    if not check_path_valid(category_id)[0]:
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    REPOSITORY = app.config['REPOSITORY']
    try:
        with open(os.path.join(REPOSITORY, feed_id + '.xml')) as f:
            feed = read(Feed, f)
            entries = []
            for entry in feed.entries:
                entries.append({
                    'title': entry.title,
                    'entry_url': url_for(
                        'feed_entry',
                        category_id=category_id,
                        feed_id=feed_id,
                        entry_id=get_hash(entry.id),
                        _external=True
                    ),
                    'permalink': entry.id,
                    'updated': entry.updated_at.__str__(),
                    'feed': {
                        'title': feed.title,
                        'entries_url': url_for(
                            'feed_entries',
                            feed_id=get_hash(feed.id)
                        ),
                        'permalink': feed.id
                    }
                })
        return jsonify(
            title=feed.title,
            entries=entries
        )
    except IOError:
        r = jsonify(
            error='feed-not-found',
            message='Given feed does not exist'
        )
        r.status_code = 404
        return r

@app.route('/entries/', defaults={'category_id': '/'})
@app.route('/<path:category_id>/entries/')
def category_entries(category_id):
    REPOSITORY = app.config['REPOSITORY']
    lst, cursor, target = check_path_valid(category_id)
    if not cursor:
        r = jsonify(
            error='category-path-invalid',
            message='Given category was not found'
        )
        r.status_code = 404
        return r
    sorting_pool = []
    entries = []
    for child in cursor.get_all_feeds():
        feed_id = get_hash(child.xml_url)
        with open(os.path.join(
                REPOSITORY, feed_id + '.xml'
        )) as f:
            feed = read(Feed, f)
            for entry in feed.entries:
                sorting_pool.append((feed, entry))
    sorting_pool.sort(key=lambda entry: entry[1].updated_at, reverse=True)
    for feed, entry in sorting_pool:
        entries.append({
            'title': entry.title,
            'entry_url': url_for(
                'feed_entry',
                feed_id=get_hash(feed.id),
                entry_id=get_hash(entry.id),
                _external=True
            ),
            'permalink': entry.id,
            'updated': entry.updated_at.__str__(),
            'feed': {
                'title': feed.title,
                'entries_url': url_for(
                    'feed_entries',
                    feed_id=get_hash(feed.id)
                ),
                'permalink': feed.id
            }
        })
    return jsonify(
        title=category_id.split('/')[-1],
        entries=entries
    )

@app.route('/feeds/<feed_id>/entries/<entry_id>/',
           defaults={'category_id': '/'})
@app.route('/<path:category_id>/feeds/<feed_id>/entries/<entry_id>/')
def feed_entry(category_id, feed_id, entry_id):
    if not check_path_valid(category_id)[0]:
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    REPOSITORY = app.config['REPOSITORY']
    try:
        with open(os.path.join(REPOSITORY, feed_id + '.xml')) as f:
            feed = read(Feed, f)
            for entry in feed.entries:
                if entry_id == get_hash(entry.id):
                    return jsonify(
                        title=entry.title,
                        content=entry.content,
                        updated=entry.updated_at.__str__(),
                        permalink=entry.id,
                        feed={
                            'title': feed.title,
                            'entries_url': url_for(
                                'feed_entries',
                                feed_id=get_hash(feed.id),
                                _external=True
                            ),
                            'permalink': feed.id
                        }
                    )
            r = jsonify(
                error='entry-not-found',
                message='Given entry does not exist'
            )
            r.status_code = 404
            return r

    except IOError:
        r = jsonify(
            error='feed-not-found',
            message='Given feed does not exist'
        )
        r.status_code = 404
        return r
<commit_msg>get_all_feeds get the path as string, not list<commit_after>from collections import deque
import hashlib
import os.path
try:
    import urllib2
except ImportError:
    import urllib.request as urllib2

from libearth.compat import binary
from libearth.feed import Feed
from libearth.feedlist import (Feed as FeedOutline,
                               FeedCategory as CategoryOutline, FeedList)
from libearth.parser.autodiscovery import autodiscovery, FeedUrlNotFoundError
from libearth.parser.heuristic import get_format
from libearth.schema import read, write

from flask import Flask, jsonify, render_template, request, url_for

app = Flask(__name__)

app.config.update(dict(
    REPOSITORY='repo/',
    OPML='earthreader.opml'
))

def feedlist_exists():
    REPOSITORY = app.config['REPOSITORY']
    OPML = app.config['OPML']
    if not os.path.isfile(REPOSITORY + OPML):
        return False
    return True

def get_feedlist():
    REPOSITORY = app.config['REPOSITORY']
    OPML = app.config['OPML']
    if not os.path.isfile(REPOSITORY + OPML):
        if not os.path.isdir(REPOSITORY):
            os.mkdir(REPOSITORY)
        feed_list = FeedList()
        feed_list.save_file(REPOSITORY + OPML)
    feed_list = FeedList(REPOSITORY + OPML)
    return feed_list

def get_hash(name):
    return hashlib.sha1(binary(name)).hexdigest()

def get_all_feeds(category, path=None):
    feeds = []
    categories = []
    if not path:
        feed_path = '/'
    else:
        feed_path = path
    for child in category:
        if isinstance(child, FeedOutline):
            feed_id = get_hash(child.xml_url)
            feeds.append({
                'title': child.title,
                'entries_url': url_for(
                    'feed_entries',
                    category_id=feed_path,
                    feed_id=feed_id,
                    _external=True
                ),
                'remove_feed_url': url_for(
                    'delete_feed',
                    category_id=feed_path,
                    feed_id=feed_id,
                    _external=True
                )
            })
        elif isinstance(child, CategoryOutline):
            categories.append({
                'title': child.title,
                'feeds_url': url_for(
                    'feeds',
                    category_id=feed_path + '/' + child.title
                    if path else child.title,
                    _external=True
                ),
                'entries_url': url_for(
                    'category_entries',
                    category_id=feed_path + '/' + child.title
                    if path else child.title,
                    _external=True
                ),
                'add_feed_url': url_for(
                    'add_feed',
                    category_id=feed_path + '/' + child.title
                    if path else child.title,
                    _external=True
                ),
                'add_category_url': url_for(
                    'add_category',
                    category_id=feed_path + '/' + child.title
                ),
                'remove_category_url': url_for(
                    'delete_category',
                    category_id=feed_path + '/' + child.title
                    if path else child.title,
                    _external=True
                ),
            })
    return feeds, categories

def check_path_valid(category_id, return_category_parent=False):
    if category_id == '/':
        feed_list = get_feedlist()
        return feed_list, feed_list, None
    if return_category_parent:
        category_list = category_id.split('/')
        target = category_list.pop()
        categories = deque(category_list)
    else:
        target = None
        categories = deque(category_id.split('/'))
    feed_list = get_feedlist()
    cursor = feed_list
    while categories:
        is_searched = False
        looking_for = categories.popleft()
        for category in cursor:
            if category.text == looking_for:
                is_searched = True
                cursor = category
                break
        if not is_searched:
            return None, None, None
    return feed_list, cursor, target

def find_feed_in_opml(feed_id, category, parent_categories=[], result=[]):
    categories = []
    if parent_categories:
        feed_path = '/'.join(parent_categories)
    else:
        feed_path = '/'
    for child in category:
        if isinstance(child, FeedOutline):
            current_feed_id = hashlib.sha1(binary(child.xml_url)).hexdigest()
            if current_feed_id == feed_id:
                result.append(feed_path)
        elif isinstance(child, CategoryOutline):
            categories.append(child)
    for category in categories:
        find_feed_in_opml(
            feed_id,
            category,
            parent_categories.append(category.title)
            if parent_categories else [category.title],
            result
        )
    return result

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html')

@app.route('/feeds/', defaults={'category_id': '/'})
@app.route('/<path:category_id>/feeds/')
def feeds(category_id):
    feed_list, cursor, _ = check_path_valid(category_id)
    if not feed_list:
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    feeds, categories = get_all_feeds(cursor, category_id)
    return jsonify(feeds=feeds, categories=categories)

@app.route('/feeds/', methods=['POST'], defaults={'category_id': '/'})
@app.route('/<path:category_id>/feeds/', methods=['POST'])
def add_feed(category_id):
    REPOSITORY = app.config['REPOSITORY']
    feed_list, cursor, _ = check_path_valid(category_id)
    if (not isinstance(cursor, CategoryOutline) and
            not isinstance(cursor, FeedList)):
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    try:
        url = request.form['url']
        f = urllib2.urlopen(url)
        document = f.read()
    except ValueError:
        r = jsonify(
            error='unreachable-url',
            message='Cannot connect to given url'
        )
        r.status_code = 400
        return r
    try:
        feed_url = autodiscovery(document, url)
    except FeedUrlNotFoundError:
        r = jsonify(
            error='unreachable-feed-url',
            message='Cannot find feed url'
        )
        r.status_code = 400
        return r
    if not feed_url == url:
        f.close()
        f = urllib2.urlopen(feed_url)
        xml = f.read()
    else:
        xml = document
    format = get_format(xml)
    result = format(xml, feed_url)
    feed = result[0]
    outline = FeedOutline('atom', feed.title.value, feed_url)
    for link in feed.links:
            if link.relation == 'alternate' and \
                    link.mimetype == 'text/html':
                outline.blog_url = link.uri
    cursor.append(outline)
    feed_list.save_file()
    file_name = get_hash(feed.id) + '.xml'
    with open(os.path.join(REPOSITORY, file_name), 'w') as f:
        for chunk in write(feed, indent='    ', canonical_order=True):
            f.write(chunk)
    return feeds(category_id)

@app.route('/', methods=['POST'], defaults={'category_id': '/'})
@app.route('/<path:category_id>/', methods=['POST'])
def add_category(category_id):
    feed_list, cursor, _ = check_path_valid(category_id)
    if (not isinstance(cursor, CategoryOutline) and
            not isinstance(cursor, FeedList)):
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r

    title = request.form['title']
    outline = CategoryOutline(title)
    cursor.append(outline)
    feed_list.save_file()
    return feeds(category_id)

@app.route('/<path:category_id>/', methods=['DELETE'])
def delete_category(category_id):
    feed_list, cursor, target = check_path_valid(category_id, True)
    if not cursor:
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    for child in cursor:
        if isinstance(child, CategoryOutline):
            if child.text == target:
                cursor.remove(child)
    feed_list.save_file()
    index = category_id.rfind('/')
    if index == -1:
        return feeds('/')
    else:
        return feeds(category_id[:index])

@app.route('/feeds/<feed_id>/', methods=['DELETE'],
           defaults={'category_id': '/'})
@app.route('/<path:category_id>/feeds/<feed_id>/', methods=['DELETE'])
def delete_feed(category_id, feed_id):
    REPOSITORY = app.config['REPOSITORY']
    feed_list, cursor, _ = check_path_valid(category_id)
    if not cursor:
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    target = None
    for feed in cursor:
        if isinstance(feed, FeedOutline):
            if feed_id == hashlib.sha1(binary(feed.xml_url)).hexdigest():
                target = feed
    if target:
        cursor.remove(target)
    else:
        r = jsonify(
            error='feed-not-found-in-path',
            message='Given feed does not exists in the path'
        )
        r.status_code = 400
        return r
    feed_list.save_file()
    if not find_feed_in_opml(feed_id, feed_list):
        os.remove(REPOSITORY + feed_id + '.xml')
    return feeds(category_id)

@app.route('/feeds/<feed_id>/entries/', defaults={'category_id': '/'})
@app.route('/<path:category_id>/feeds/<feed_id>/entries/')
def feed_entries(category_id, feed_id):
    if not check_path_valid(category_id)[0]:
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    REPOSITORY = app.config['REPOSITORY']
    try:
        with open(os.path.join(REPOSITORY, feed_id + '.xml')) as f:
            feed = read(Feed, f)
            entries = []
            for entry in feed.entries:
                entries.append({
                    'title': entry.title,
                    'entry_url': url_for(
                        'feed_entry',
                        category_id=category_id,
                        feed_id=feed_id,
                        entry_id=get_hash(entry.id),
                        _external=True
                    ),
                    'permalink': entry.id,
                    'updated': entry.updated_at.__str__(),
                    'feed': {
                        'title': feed.title,
                        'entries_url': url_for(
                            'feed_entries',
                            feed_id=get_hash(feed.id)
                        ),
                        'permalink': feed.id
                    }
                })
        return jsonify(
            title=feed.title,
            entries=entries
        )
    except IOError:
        r = jsonify(
            error='feed-not-found',
            message='Given feed does not exist'
        )
        r.status_code = 404
        return r

@app.route('/entries/', defaults={'category_id': '/'})
@app.route('/<path:category_id>/entries/')
def category_entries(category_id):
    REPOSITORY = app.config['REPOSITORY']
    lst, cursor, target = check_path_valid(category_id)
    if not cursor:
        r = jsonify(
            error='category-path-invalid',
            message='Given category was not found'
        )
        r.status_code = 404
        return r
    sorting_pool = []
    entries = []
    for child in cursor.get_all_feeds():
        feed_id = get_hash(child.xml_url)
        with open(os.path.join(
                REPOSITORY, feed_id + '.xml'
        )) as f:
            feed = read(Feed, f)
            for entry in feed.entries:
                sorting_pool.append((feed, entry))
    sorting_pool.sort(key=lambda entry: entry[1].updated_at, reverse=True)
    for feed, entry in sorting_pool:
        entries.append({
            'title': entry.title,
            'entry_url': url_for(
                'feed_entry',
                feed_id=get_hash(feed.id),
                entry_id=get_hash(entry.id),
                _external=True
            ),
            'permalink': entry.id,
            'updated': entry.updated_at.__str__(),
            'feed': {
                'title': feed.title,
                'entries_url': url_for(
                    'feed_entries',
                    feed_id=get_hash(feed.id)
                ),
                'permalink': feed.id
            }
        })
    return jsonify(
        title=category_id.split('/')[-1],
        entries=entries
    )

@app.route('/feeds/<feed_id>/entries/<entry_id>/',
           defaults={'category_id': '/'})
@app.route('/<path:category_id>/feeds/<feed_id>/entries/<entry_id>/')
def feed_entry(category_id, feed_id, entry_id):
    if not check_path_valid(category_id)[0]:
        r = jsonify(
            error='category-path-invalid',
            message='Given category path is not valid'
        )
        r.status_code = 404
        return r
    REPOSITORY = app.config['REPOSITORY']
    try:
        with open(os.path.join(REPOSITORY, feed_id + '.xml')) as f:
            feed = read(Feed, f)
            for entry in feed.entries:
                if entry_id == get_hash(entry.id):
                    return jsonify(
                        title=entry.title,
                        content=entry.content,
                        updated=entry.updated_at.__str__(),
                        permalink=entry.id,
                        feed={
                            'title': feed.title,
                            'entries_url': url_for(
                                'feed_entries',
                                feed_id=get_hash(feed.id),
                                _external=True
                            ),
                            'permalink': feed.id
                        }
                    )
            r = jsonify(
                error='entry-not-found',
                message='Given entry does not exist'
            )
            r.status_code = 404
            return r

    except IOError:
        r = jsonify(
            error='feed-not-found',
            message='Given feed does not exist'
        )
        r.status_code = 404
        return r
<|endoftext|>


<commit_before>from __future__ import print_function
import sys

NUMBERS = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']

class GameState:
    def __init__(self,state):
        self.state = state

    def warning(self):
        print("WARNING: ", self.state, file=sys.stderr)

    def player(self):
        return self.state["players"][self.state[u"in_action"]]

    def get_cards(self):
        return self.player()[u"hole_cards"]

    def get_round(self):
        self.warning()
        return self.state[u"bet_index"]

    def get_stack(self):
        return self.player()[u"stack"]

    def is_dealer(self):
        if self.state[u"in_action"][u"dealer"] == 1:
            return True
        return False

    def buy_in(self):
        return self.state["current_buy_in"]

    def keep(self):
        return self.buy_in() - self.player()["bet"]

    def get_rank(self):
        return self.get_cards()[u"rank"]

    def get_highest_rank(self):
        cards = self.get_cards()
        rank0 = cards[0][u"rank"]
        rank1 = cards[1][u"rank"]
        if (NUMBERS.index(rank0) > NUMBERS.index(rank1)):
            return rank0
        else:
            return rank1

    def community_cards(self):
        return self.state[u"community_cards"]

    def have_pair_in_hand(self):
        return self.get_cards()[0]["rank"] == self.get_cards()[1]["rank"]
<commit_msg>better logs<commit_after>from __future__ import print_function
import sys

NUMBERS = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']

class GameState:
    def __init__(self,state):
        self.state = state
        self.warning()

    def warning(self):
        print("WARNING: ", self.state, file=sys.stderr)

    def player(self):
        return self.state["players"][self.state[u"in_action"]]

    def get_cards(self):
        return self.player()[u"hole_cards"]

    def get_round(self):
        return self.state[u"bet_index"]

    def get_stack(self):
        return self.player()[u"stack"]

    def is_dealer(self):
        if self.state[u"in_action"][u"dealer"] == 1:
            return True
        return False

    def buy_in(self):
        return self.state["current_buy_in"]

    def keep(self):
        return self.buy_in() - self.player()["bet"]

    def get_rank(self):
        return self.get_cards()[u"rank"]

    def get_highest_rank(self):
        cards = self.get_cards()
        rank0 = cards[0][u"rank"]
        rank1 = cards[1][u"rank"]
        if (NUMBERS.index(rank0) > NUMBERS.index(rank1)):
            return rank0
        else:
            return rank1

    def community_cards(self):
        return self.state[u"community_cards"]

    def have_pair_in_hand(self):
        return self.get_cards()[0]["rank"] == self.get_cards()[1]["rank"]
<|endoftext|>