<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from urlparse import urlparse, urlunparse
import urllib2
import urllib
import httplib
import socket
import json
import math

def fetch_raw(url, kwargs=None):
    """
    Fetch something from a web page, optionally encoding keyword arguments
    """
    urlparsed = urlparse(url)
    urlparsed = urlparsed[0:4] + tuple([urllib.urlencode(kwargs)]) + urlparsed[5:]
    return urllib2.urlopen(urlunparse(urlparsed)).read()

def fetch(url, kwargs):
    """
    Fetch a JSON object from a web page, encoding keyword parameters
    """
    result = json.loads(fetch_raw(url, kwargs))
    # a 'exception' is when the server signals the client that something
    # went wrong
    if isinstance(result, dict) and 'exception' in result:
        raise RobotException(result['exception'])
    return result

def fetch_persist(url, kwargs=None):
    """
    Fetch a JSON object from a web page, retrying in case of timeouts
    """
    try:
        return fetch(url, kwargs)
    except (urllib2.URLError, httplib.BadStatusLine), e:
        if (isinstance(e, httplib.BadStatusLine) or
            isinstance(e.reason, socket.timeout)):
            return fetch_persist(url, kwargs)
        else:
            raise e

def url_concat(*parts):
    """
    splice a URL together; just concatenate it really
    """
    parts = list(parts)
    # remove trailing slash
    for i, path in enumerate(parts):
        if path.strip().endswith('/'):
            parts[i] = path.strip()[:-1]
    return '/'.join(parts)

class Robot(object):
    """
    A robot held on the client side.
    """
    @classmethod
    def connect(cls, slot_url, **kwargs):
        kwargs['connect'] = 't'
        return Robot(slot_url, fetch_persist(slot_url, kwargs))

    def __init__(self, url, data):
        # see: courier/robot.py Robot.__json__
        self.url = url
        self.name = data['name']
        self.armor = data['armor']
        self.heat = data['heat']
        self._turret_rotation = None

    def __str__(self):
        return "<Robot '%s' (%s armor)>" % (self.name, self.armor)

    def turn(self, amount):
        """
        Steer ourselves by amount (relative)
        """
        return fetch_persist(self.url, {'turn': 't', 'amount': math.radians(amount)})

    def turn_abs(self, angle):
        """
        Steer ourselves so that we're facing the given angle
        """
        self.turn(math.radians(angle - self.compass()))

    def get_throttle(self):
        return fetch_persist(self.url, {'throttle': 't'})
    def set_throttle(self, amount):
        """
        Set our throttle to a percentage between -50 and 100.
        """
        return fetch_persist(self.url, {'throttle': 't', 'amount': amount})
    throttle = property(get_throttle, set_throttle)

    def compass(self):
        return math.degrees(fetch_persist(self.url, {'rotation': 't'}))

    def locate(self):
        return fetch_persist(self.url, {'location': 't'})

    def scan(self, angle):
        assert -90 <= angle <= 90
        return fetch_persist(self.url, {'scan_robots': 't', 'angle': math.radians(angle)})

    def scan_wall(self):
        return fetch_persist(self.url, {'scan_wall': 't'})

    def set_turret_rotation(self, angle):
        self._turret_rotation = angle
        return fetch_persist(self.url, {'turret_rotate': 't', 'angle':
            math.radians(angle)})
    def get_turret_rotation(self):
        if self._turret_rotation is None:
            self._turret_rotation = math.degrees(fetch_persist(self.url, {'turret_rotate': 't'}))
        return self._turret_rotation
    turret_rotation = property(get_turret_rotation, set_turret_rotation)

class Match(object):
    @classmethod
    def register_new(cls, url, **kwargs):
        """
        Register a match, then return a URL of that match.
        """
        kwargs['register'] = 't'
        new_match = fetch(url_concat(url, 'matches'), kwargs)
        m = cls.from_url(url_concat(url, 'matches', new_match['match']))
        m.auth_code = new_match['auth_code']
        return m

    @classmethod
    def from_url(cls, url):
        try:
            data = fetch(url, {'info': 't'})
        except RobotException:
            return False
        if not (isinstance(data, dict) and 'started' in data and 'init_time' in
                data and 'gametime' in data and 'public' in data):
            return False<commit_msg>client no longer breaks when we hand it a perfectly good robot URL instead of a match URL<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from urlparse import urlparse, urlunparse
import urllib2
import urllib
import httplib
import socket
import json
import math

def fetch_raw(url, kwargs=None):
    """
    Fetch something from a web page, optionally encoding keyword arguments
    """
    urlparsed = urlparse(url)
    urlparsed = urlparsed[0:4] + tuple([urllib.urlencode(kwargs)]) + urlparsed[5:]
    return urllib2.urlopen(urlunparse(urlparsed)).read()

def fetch(url, kwargs):
    """
    Fetch a JSON object from a web page, encoding keyword parameters
    """
    result = json.loads(fetch_raw(url, kwargs))
    # a 'exception' is when the server signals the client that something
    # went wrong
    if isinstance(result, dict) and 'exception' in result:
        raise RobotException(result['exception'])
    return result

def fetch_persist(url, kwargs=None):
    """
    Fetch a JSON object from a web page, retrying in case of timeouts
    """
    try:
        return fetch(url, kwargs)
    except (urllib2.URLError, httplib.BadStatusLine), e:
        if (isinstance(e, httplib.BadStatusLine) or
            isinstance(e.reason, socket.timeout)):
            return fetch_persist(url, kwargs)
        else:
            raise e

def url_concat(*parts):
    """
    splice a URL together; just concatenate it really
    """
    parts = list(parts)
    # remove trailing slash
    for i, path in enumerate(parts):
        if path.strip().endswith('/'):
            parts[i] = path.strip()[:-1]
    return '/'.join(parts)

class Robot(object):
    """
    A robot held on the client side.
    """
    @classmethod
    def connect(cls, slot_url, **kwargs):
        kwargs['connect'] = 't'
        return Robot(slot_url, fetch_persist(slot_url, kwargs))

    def __init__(self, url, data):
        # see: courier/robot.py Robot.__json__
        self.url = url
        self.name = data['name']
        self.armor = data['armor']
        self.heat = data['heat']
        self._turret_rotation = None

    def __str__(self):
        return "<Robot '%s' (%s armor)>" % (self.name, self.armor)

    def turn(self, amount):
        """
        Steer ourselves by amount (relative)
        """
        return fetch_persist(self.url, {'turn': 't', 'amount': math.radians(amount)})

    def turn_abs(self, angle):
        """
        Steer ourselves so that we're facing the given angle
        """
        self.turn(math.radians(angle - self.compass()))

    def get_throttle(self):
        return fetch_persist(self.url, {'throttle': 't'})
    def set_throttle(self, amount):
        """
        Set our throttle to a percentage between -50 and 100.
        """
        return fetch_persist(self.url, {'throttle': 't', 'amount': amount})
    throttle = property(get_throttle, set_throttle)

    def compass(self):
        return math.degrees(fetch_persist(self.url, {'rotation': 't'}))

    def locate(self):
        return fetch_persist(self.url, {'location': 't'})

    def scan(self, angle):
        assert -90 <= angle <= 90
        return fetch_persist(self.url, {'scan_robots': 't', 'angle': math.radians(angle)})

    def scan_wall(self):
        return fetch_persist(self.url, {'scan_wall': 't'})

    def set_turret_rotation(self, angle):
        self._turret_rotation = angle
        return fetch_persist(self.url, {'turret_rotate': 't', 'angle':
            math.radians(angle)})
    def get_turret_rotation(self):
        if self._turret_rotation is None:
            self._turret_rotation = math.degrees(fetch_persist(self.url, {'turret_rotate': 't'}))
        return self._turret_rotation
    turret_rotation = property(get_turret_rotation, set_turret_rotation)

class Match(object):
    @classmethod
    def register_new(cls, url, **kwargs):
        """
        Register a match, then return a URL of that match.
        """
        kwargs['register'] = 't'
        new_match = fetch(url_concat(url, 'matches'), kwargs)
        m = cls.from_url(url_concat(url, 'matches', new_match['match']))
        m.auth_code = new_match['auth_code']
        return m

    @classmethod
    def from_url(cls, url):
        try:
            data = fetch(url, {'info': 't'})
        except (RobotException, urllib2.HTTPError):
            return False
        if not (isinstance(data, dict) and 'started' in data and 'init_time' in
                data and 'gametime' in data and 'public' in data):
            return False
{'fraction_non_alphanumeric': 0.12576652915598172, 'fraction_numerical': 0.0039023302486341843, 'mean_word_length': 2.715824357912179, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6571207', 'n_tokens_mistral': 2835, 'n_tokens_neox': 2768, 'n_words': 757}