Skip to content

Reference

Roger Chen edited this page Feb 6, 2016 · 10 revisions

Useful Python functions

Generate a list of class logins

import string
import itertools

def generate_login_list(num, minimum_length=2):
    """
    Generates a list of class logins, starting with 2-letter logins. Does not include logins that
    start with 't' or 'r'.

    >>> generate_login_list(3)
    ['aa', 'ab', 'ac']

    """
    accounts = []
    alph = string.lowercase
    non_reserved = lambda account: account[0] not in ('t', 'r')
    for length in itertools.count(minimum_length):
        accounts.extend(map(''.join, filter(non_reserved, itertools.product(alph, repeat=length))))
        if len(accounts) >= num:
            return accounts[:num]

Feature: The IPython shell (autograder-shell)

If you run ob2 with the --ipython switch, it will open a IPython shell for you to run administrative tasks. As an example:

#!/bin/bash

# Use our virtualenv
source "/srv/virtualenv/ob2/bin/activate"

# Get into the ob2 directory
cd "/srv/ob2/"

# Start ob2 with --ipython, using our autograder configuration directories
python -m ob2 --ipython -C "/srv/autograder/" -C "/srv/ob2_production_config/"

Once inside the ob2 shell, you can import and run a lot of the ob2 functions, which will be helpful for various administrative tasks. For example, here is how you might get a list of repos in your GitHub organization:

# The _get_github_admin function is already imported from ob2.util.github_api
github = _get_github_admin()

org = github.organization(config.github_organization)
repos = list(org.iter_repos())

Feature: Staff accounts

There is a super column in the users table, to allow you to distinguish staff accounts from student accounts. A non-zero value of super has the following effects:

  • Shows "(Staff)" next to the user's name in the TA interface
  • Excludes grades from statistics (ranking, histogram, etc)

Student accounts should have a super value of 0.

Here is an example query that could be used to create a staff account:

INSERT INTO users (id, name, sid, login, github, email, super, grouplimit, photo)
VALUES (1, "Peter Perfect", "1620000", "ta", "peterperfect162", "cs162@eecs.berkeley.edu", 1, 0, NULL);

Creating groups manually

There are only 2 steps to creating a group manually:

  • Modify the database
  • Create the GitHub repository for the group

You can just insert new groups into the groupsusers table, but make sure to update the group_next_group_number as well!

-- Create a new group named "group11" with 2 users.
INSERT INTO groupsusers (`group`, user) VALUES ("group11", 123), ("group11", 321);

-- Update the auto-incrementing "group_next_group_number" value
-- NOTE: This value is the LARGEST EXISTING GROUP NUMBER. The name is misleading, sorry.
UPDATE options SET value = 11 WHERE key = "group_next_group_number" LIMIT 1;

Then, use the autograder-shell to create their GitHub repo;

from ob2.util.github_api import _assign_repo

# Assigns a repo named "group11" to 2 collaborators
# The "group11" repo will be created if it doesn't exist.
# The GitHub users "username1" and "username2" will be added to the repo
# Existing collaborators will NOT be deleted.
_assign_repo("group11", ["username1", "username2"])

Adding a member to an existing group

You can add a member to an existing group with 2 steps:

  • Insert them into the database
  • Add them to the GitHub repo
-- Add a user with ID 123 to group11
INSERT INTO groupsusers (`group`, user) VALUES ("group11", 123);

Then, use the autograder-shell to give them access to the existing GitHub repo;

from ob2.util.github_api import _assign_repo

# Assigns a repo named "group11" to 1 student with GitHub username "rogerhub"
# Existing collaborators will NOT be deleted.
_assign_repo("group11", ["rogerhub"])

Clone this wiki locally