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

import json
import click
import os
import config
import sqlite3
import time

from flask import Flask
from flask import request
from flask import g
from flask import redirect
from flask import abort
from flask import render_template
from flask import flash
from werkzeug.exceptions import HTTPException

app = Flask(__name__)

# Load default config and override config from an environment variable
app.config.update(dict(
    DATABASE=os.path.join(app.root_path, config.DATABASE_PATH),
    DEBUG=True,
))


class JSONException(HTTPException):
    def __init__(self, message, status_code=None, response=None):
        self.message = message
        self.response = response

        if status_code is not None:
            self.code = status_code
        else:
            self.code = 400

    def get_body(self, environ):
        return json.dumps({
            "error": self.message,
            "code": self.code
        })

    def get_headers(self, environ):
        return [('Content-Type', 'application/json')]


ALPHABET = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"


def base62_encode(num, alphabet=ALPHABET):
    """Encode a number in Base X

    `num`: The number to encode
    `alphabet`: The alphabet to use for encoding
    """
    if (num == 0):
        return alphabet[0]
    arr = []
    base = len(alphabet)
    while num:
        rem = num % base
        num = num // base
        arr.append(alphabet[rem])
    arr.reverse()
    return ''.join(arr)


def base62_decode(string, alphabet=ALPHABET):
    """Decode a Base X encoded string into the number

    Arguments:
    - `string`: The encoded string
    - `alphabet`: The alphabet to use for encoding
    """
    base = len(alphabet)
    strlen = len(string)
    num = 0

    idx = 0
    for char in string:
        power = (strlen - (idx + 1))
        num += alphabet.index(char) * (base ** power)
        idx += 1

    return num


def connect_db():
    """Connects to the specific database."""
    def make_dicts(cursor, row):
        return dict((cursor.description[idx][0], value)
                    for idx, value in enumerate(row))

    rv = sqlite3.connect(app.config['DATABASE'])
    rv.row_factory = make_dicts
    return rv


def init_db():
    """Initializes the database."""
    db = get_db()
    with app.open_resource('schema/v1.sql', mode='r') as f:
        db.cursor().executescript(f.read())
    db.commit()


def get_db():
    """Opens a new database connection if there is none yet for the
    current application context.
    """
    if not hasattr(g, 'sqlite_db'):
        g.sqlite_db = connect_db()
    return g.sqlite_db


def lookup_url(link_id):
    db = get_db()
    cur = db.execute('SELECT url FROM urls WHERE key = ? LIMIT 1', (link_id,))
    link = cur.fetchone()
    if link is None:
        return None
    else:
        return link['url']


def lookup_stats(link_id):
    db = get_db()
    cur = db.execute('SELECT time FROM stats WHERE link_id = ?', (link_id,))
    link_stats = [x['time'] for x in cur.fetchall()]

    return link_stats


def save_url(url, wish=None):
    exists = None
    db = get_db()

    if wish is not None:
        exists = lookup_url(wish)<commit_msg>Removed uneccessary variable assignment<commit_after>#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import json
import click
import os
import config
import sqlite3
import time

from flask import Flask
from flask import request
from flask import g
from flask import redirect
from flask import abort
from flask import render_template
from flask import flash
from werkzeug.exceptions import HTTPException

app = Flask(__name__)

# Load default config and override config from an environment variable
app.config.update(dict(
    DATABASE=os.path.join(app.root_path, config.DATABASE_PATH),
    DEBUG=True,
))


class JSONException(HTTPException):
    def __init__(self, message, status_code=None, response=None):
        self.message = message
        self.response = response

        if status_code is not None:
            self.code = status_code
        else:
            self.code = 400

    def get_body(self, environ):
        return json.dumps({
            "error": self.message,
            "code": self.code
        })

    def get_headers(self, environ):
        return [('Content-Type', 'application/json')]


ALPHABET = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"


def base62_encode(num, alphabet=ALPHABET):
    """Encode a number in Base X

    `num`: The number to encode
    `alphabet`: The alphabet to use for encoding
    """
    if (num == 0):
        return alphabet[0]
    arr = []
    base = len(alphabet)
    while num:
        rem = num % base
        num = num // base
        arr.append(alphabet[rem])
    arr.reverse()
    return ''.join(arr)


def base62_decode(string, alphabet=ALPHABET):
    """Decode a Base X encoded string into the number

    Arguments:
    - `string`: The encoded string
    - `alphabet`: The alphabet to use for encoding
    """
    base = len(alphabet)
    strlen = len(string)
    num = 0

    idx = 0
    for char in string:
        power = (strlen - (idx + 1))
        num += alphabet.index(char) * (base ** power)
        idx += 1

    return num


def connect_db():
    """Connects to the specific database."""
    def make_dicts(cursor, row):
        return dict((cursor.description[idx][0], value)
                    for idx, value in enumerate(row))

    rv = sqlite3.connect(app.config['DATABASE'])
    rv.row_factory = make_dicts
    return rv


def init_db():
    """Initializes the database."""
    db = get_db()
    with app.open_resource('schema/v1.sql', mode='r') as f:
        db.cursor().executescript(f.read())
    db.commit()


def get_db():
    """Opens a new database connection if there is none yet for the
    current application context.
    """
    if not hasattr(g, 'sqlite_db'):
        g.sqlite_db = connect_db()
    return g.sqlite_db


def lookup_url(link_id):
    db = get_db()
    cur = db.execute('SELECT url FROM urls WHERE key = ? LIMIT 1', (link_id,))
    link = cur.fetchone()
    if link is None:
        return None
    else:
        return link['url']


def lookup_stats(link_id):
    db = get_db()
    cur = db.execute('SELECT time FROM stats WHERE link_id = ?', (link_id,))
    link_stats = [x['time'] for x in cur.fetchall()]

    return link_stats


def save_url(url, wish=None):
    db = get_db()

    if wish is not None:
        exists = lookup_url(wish)
{'fraction_non_alphanumeric': 0.10949464012251149, 'fraction_numerical': 0.008575803981623277, 'mean_word_length': 2.6835871404399323, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, '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': '2411929', 'n_tokens_mistral': 2132, 'n_tokens_neox': 2014, 'n_words': 626}