Example #1
0
def strategy_exists(user, name):
    conn.ping(True)
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    cursor.execute(
        "select id, name from code_info where name=%s and user_id=%s",
        (name, user.user_id))
    return cursor.fetchone() is not None
Example #2
0
def get_strategy_list(user):
    """
    获取用户编写的策略列表
    :param user
    """
    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)
    cur.execute("select id from code_info where name=%s and user_id=%s", (
        "demo",
        user.user_id,
    ))
    row = cur.fetchone()
    if not row:
        demo_ = os.path.join(os.path.dirname(__file__), '..', '..', 'demo.py')
        file = codecs.open(demo_, mode='r', encoding="utf8")
        content = file.read()
        cur.execute(
            "insert into code_info (name, user_id, content) value (%s, %s, %s)",
            ("demo", user.user_id, content))
        file.close()
    cur.execute("select id, name from code_info where user_id=%s and stat=0",
                (user.user_id, ))
    code_list = cur.fetchall()
    conn.commit()
    return code_list
Example #3
0
def delete_strategy(user, name):
    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)
    if name == 'demo':
        cur.execute("update code_info set stat=1 where user_id=%s and name=%s", (user.user_id, name))
    else:
        cur.execute("delete from code_info where user_id=%s and name=%s", (user.user_id, name))
    conn.commit()
Example #4
0
def get_competition_list(user):
    __check_user(user)

    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)

    cur.execute("select id, name from strategy_competition where user_id=%s", (user.user_id,))
    code_list = cur.fetchall()
    conn.commit()
    return code_list
Example #5
0
def remove_competition(user, name):
    __check_user(user)

    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)

    sql = "delete from strategy_competition where name=%s and user_id=%s"
    cur.execute(sql, (name, user.user_id))

    conn.commit()
Example #6
0
def update_competition_content(user, name, content):
    __check_user(user)

    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)

    sql = "update strategy_competition set content=%s where name=%s and user_id=%s"
    cur.execute(sql, (content, name, user.user_id))

    conn.commit()
Example #7
0
def delete_strategy(user, name):
    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)
    if name == 'demo':
        cur.execute("update code_info set stat=1 where user_id=%s and name=%s",
                    (user.user_id, name))
    else:
        cur.execute("delete from code_info where user_id=%s and name=%s",
                    (user.user_id, name))
    conn.commit()
Example #8
0
def get_strategy(user, code_name):
    """
    根据代码名称,获取该段策略代码
    :param user: 用户对象
    :param code_name: 代码名称
    :return 返回一个code对象
    """
    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)
    cur.execute("select name, content from code_info where user_id=%s and name=%s and stat=0", (user.user_id, code_name))
    row = cur.fetchone();
    if row:
        return Code(code_name, content=row["content"])
    return None
Example #9
0
def get_competition(user, code_name):
    """
    根据代码名称,获取该段策略代码
    :param user: 用户对象
    :param code_name: 代码名称
    :return 返回一个code对象
    """
    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)
    cur.execute("select name, content from strategy_competition where user_id=%s and name=%s",
                (user.user_id, code_name))
    row = cur.fetchone()
    if row:
        return Code(code_name, content=row["content"])
    return None
Example #10
0
def add_competition(user, code):
    if not code.name:
        raise ValueError("code.name is empty.")

    if not check_competition_limit(user):
        raise KeyError("beyond competition limit.")

    __check_user(user)

    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)

    cur.execute("insert into strategy_competition (name, user_id, content) value (%s, %s, %s)",
                (code.name, user.user_id, code.content))

    conn.commit()
Example #11
0
def codeDownload():
    """
    下载所有参赛策略
    """
    global strategyPath
    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)
    cur.execute("select * from strategy_competition")
    row=cur.fetchall()
    for line in row:
        filename=str(line["name"])+"&"+str(line["id"])+"&"+str(line["user_id"])+".py"
        content=line["content"]
        outFilePath=strategyPath+"/"+filename
        output=open(outFilePath, "w")
        output.write(content)
        output.close()
    print("下载完毕,策略总数:",len(row))
Example #12
0
def save_code(user, code):
    """
    保存用户代码
    :param user: 用户对象
    :param code: Code对象
    """
    if not code.name:
        raise ValueError("code.name is empty")
    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)
    if not strategy_exists(user, code.name):  # 如果是第一次创建,则记录相关信息
        cur.execute("insert into code_info (name, user_id, content) value (%s, %s, %s)",
                    (code.name, user.user_id, code.content))
    else:
        cur.execute("update code_info set content=%s where name=%s and user_id=%s",
                    (code.content, code.name, user.user_id))
    conn.commit()
Example #13
0
def codeDownload():
    """
    下载所有参赛策略
    """
    global strategyPath
    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)
    cur.execute("select * from strategy_competition")
    row = cur.fetchall()
    for line in row:
        filename = str(line["name"]) + "&" + str(line["id"]) + "&" + str(
            line["user_id"]) + ".py"
        content = line["content"]
        outFilePath = strategyPath + "/" + filename
        output = open(outFilePath, "w")
        output.write(content)
        output.close()
    print("下载完毕,策略总数:", len(row))
Example #14
0
def get_strategy_list(user):
    """
    获取用户编写的策略列表
    :param user
    """
    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)
    cur.execute("select id from code_info where name=%s and user_id=%s", ("demo", user.user_id,))
    row = cur.fetchone()
    if not row:
        demo_ = os.path.join(os.path.dirname(__file__), '..', '..', 'demo.py')
        file = codecs.open(demo_, mode='r', encoding="utf8")
        content = file.read()
        cur.execute("insert into code_info (name, user_id, content) value (%s, %s, %s)", ("demo", user.user_id, content))
        file.close()
    cur.execute("select id, name from code_info where user_id=%s and stat=0", (user.user_id,))
    code_list = cur.fetchall()
    conn.commit()
    return code_list
Example #15
0
def save_code(user, code):
    """
    保存用户代码
    :param user: 用户对象
    :param code: Code对象
    """
    if not code.name:
        raise ValueError("code.name is empty")
    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)
    if not strategy_exists(user, code.name):  # 如果是第一次创建,则记录相关信息
        cur.execute(
            "insert into code_info (name, user_id, content) value (%s, %s, %s)",
            (code.name, user.user_id, code.content))
    else:
        cur.execute(
            "update code_info set content=%s where name=%s and user_id=%s",
            (code.content, code.name, user.user_id))
    conn.commit()
Example #16
0
def update_competition_config(user, old_name, new_name):
    """
    修改参赛组合的设置
    Parameters
    ----------
    user: 修改的用户
    old_name: 要修改参赛策略名称
    new_name: 参赛策略新名称
    -------

    """
    __check_user(user)

    if not new_name:
        return

    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)

    sql = "update strategy_competition set name=%s where name=%s and user_id=%s"
    cur.execute(sql, (new_name, old_name, user.user_id))

    conn.commit()
Example #17
0
def get_competition_count(user):
    conn.ping(True)
    cur = conn.cursor(pymysql.cursors.DictCursor)
    cur.execute("select count(name) as num from strategy_competition where user_id=%s", (user.user_id,))
    row = cur.fetchone()
    return row["num"]
Example #18
0
def strategy_exists(user, name):
    conn.ping(True)
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    cursor.execute("select id, name from code_info where name=%s and user_id=%s", (name, user.user_id))
    return cursor.fetchone() is not None