id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
51
19.8k
code_tokens
list
docstring
stringlengths
3
17.3k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
87
242
16,800
Karaage-Cluster/karaage
karaage/plugins/kgusage/graphs.py
get_institute_trend_graph_url
def get_institute_trend_graph_url(institute, start, end): """ Institute trend graph for machine category. """ filename = get_institute_trend_graph_filename(institute, start, end) urls = { 'graph_url': urlparse.urljoin(GRAPH_URL, filename + ".png"), 'data_url': urlparse.urljoin(GRAPH_URL, filename + ".csv"), } return urls
python
def get_institute_trend_graph_url(institute, start, end): filename = get_institute_trend_graph_filename(institute, start, end) urls = { 'graph_url': urlparse.urljoin(GRAPH_URL, filename + ".png"), 'data_url': urlparse.urljoin(GRAPH_URL, filename + ".csv"), } return urls
[ "def", "get_institute_trend_graph_url", "(", "institute", ",", "start", ",", "end", ")", ":", "filename", "=", "get_institute_trend_graph_filename", "(", "institute", ",", "start", ",", "end", ")", "urls", "=", "{", "'graph_url'", ":", "urlparse", ".", "urljoin"...
Institute trend graph for machine category.
[ "Institute", "trend", "graph", "for", "machine", "category", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgusage/graphs.py#L150-L159
16,801
Karaage-Cluster/karaage
karaage/plugins/kgusage/graphs.py
get_institutes_trend_graph_urls
def get_institutes_trend_graph_urls(start, end): """ Get all institute trend graphs. """ graph_list = [] for institute in Institute.objects.all(): urls = get_institute_trend_graph_url(institute, start, end) urls['institute'] = institute graph_list.append(urls) return graph_list
python
def get_institutes_trend_graph_urls(start, end): graph_list = [] for institute in Institute.objects.all(): urls = get_institute_trend_graph_url(institute, start, end) urls['institute'] = institute graph_list.append(urls) return graph_list
[ "def", "get_institutes_trend_graph_urls", "(", "start", ",", "end", ")", ":", "graph_list", "=", "[", "]", "for", "institute", "in", "Institute", ".", "objects", ".", "all", "(", ")", ":", "urls", "=", "get_institute_trend_graph_url", "(", "institute", ",", ...
Get all institute trend graphs.
[ "Get", "all", "institute", "trend", "graphs", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgusage/graphs.py#L162-L171
16,802
Karaage-Cluster/karaage
karaage/people/managers.py
PersonManager._create_user
def _create_user( self, username, email, short_name, full_name, institute, password, is_admin, **extra_fields): """Creates a new active person. """ # Create Person person = self.model( username=username, email=email, short_name=short_name, full_name=full_name, is_admin=is_admin, institute=institute, **extra_fields ) person.set_password(password) person.save() return person
python
def _create_user( self, username, email, short_name, full_name, institute, password, is_admin, **extra_fields): # Create Person person = self.model( username=username, email=email, short_name=short_name, full_name=full_name, is_admin=is_admin, institute=institute, **extra_fields ) person.set_password(password) person.save() return person
[ "def", "_create_user", "(", "self", ",", "username", ",", "email", ",", "short_name", ",", "full_name", ",", "institute", ",", "password", ",", "is_admin", ",", "*", "*", "extra_fields", ")", ":", "# Create Person", "person", "=", "self", ".", "model", "("...
Creates a new active person.
[ "Creates", "a", "new", "active", "person", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/people/managers.py#L31-L46
16,803
Karaage-Cluster/karaage
karaage/people/managers.py
PersonManager.create_user
def create_user( self, username, email, short_name, full_name, institute, password=None, **extra_fields): """ Creates a new ordinary person. """ return self._create_user( username=username, email=email, short_name=short_name, full_name=full_name, institute=institute, password=password, is_admin=False, **extra_fields)
python
def create_user( self, username, email, short_name, full_name, institute, password=None, **extra_fields): return self._create_user( username=username, email=email, short_name=short_name, full_name=full_name, institute=institute, password=password, is_admin=False, **extra_fields)
[ "def", "create_user", "(", "self", ",", "username", ",", "email", ",", "short_name", ",", "full_name", ",", "institute", ",", "password", "=", "None", ",", "*", "*", "extra_fields", ")", ":", "return", "self", ".", "_create_user", "(", "username", "=", "...
Creates a new ordinary person.
[ "Creates", "a", "new", "ordinary", "person", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/people/managers.py#L48-L56
16,804
Karaage-Cluster/karaage
karaage/people/managers.py
PersonManager.create_superuser
def create_superuser( self, username, email, short_name, full_name, institute, password, **extra_fields): """ Creates a new person with super powers. """ return self._create_user( username=username, email=email, institute=institute, password=password, short_name=short_name, full_name=full_name, is_admin=True, **extra_fields)
python
def create_superuser( self, username, email, short_name, full_name, institute, password, **extra_fields): return self._create_user( username=username, email=email, institute=institute, password=password, short_name=short_name, full_name=full_name, is_admin=True, **extra_fields)
[ "def", "create_superuser", "(", "self", ",", "username", ",", "email", ",", "short_name", ",", "full_name", ",", "institute", ",", "password", ",", "*", "*", "extra_fields", ")", ":", "return", "self", ".", "_create_user", "(", "username", "=", "username", ...
Creates a new person with super powers.
[ "Creates", "a", "new", "person", "with", "super", "powers", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/people/managers.py#L58-L66
16,805
Karaage-Cluster/karaage
karaage/plugins/kgapplications/templatetags/applications.py
do_application_actions_plus
def do_application_actions_plus(parser, token): """ Render actions available with extra text. """ nodelist = parser.parse(('end_application_actions',)) parser.delete_first_token() return ApplicationActionsPlus(nodelist)
python
def do_application_actions_plus(parser, token): nodelist = parser.parse(('end_application_actions',)) parser.delete_first_token() return ApplicationActionsPlus(nodelist)
[ "def", "do_application_actions_plus", "(", "parser", ",", "token", ")", ":", "nodelist", "=", "parser", ".", "parse", "(", "(", "'end_application_actions'", ",", ")", ")", "parser", ".", "delete_first_token", "(", ")", "return", "ApplicationActionsPlus", "(", "n...
Render actions available with extra text.
[ "Render", "actions", "available", "with", "extra", "text", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgapplications/templatetags/applications.py#L80-L84
16,806
daler/trackhub
trackhub/upload.py
run
def run(cmds, **kwargs): """ Wrapper around subprocess.run, with unicode decoding of output. Additional kwargs are passed to subprocess.run. """ proc = sp.Popen(cmds, bufsize=-1, stdout=sp.PIPE, stderr=sp.STDOUT, close_fds=sys.platform != 'win32') for line in proc.stdout: print(line[:-1].decode()) retcode = proc.wait() if retcode: raise sp.CalledProcessError(retcode, cmds)
python
def run(cmds, **kwargs): proc = sp.Popen(cmds, bufsize=-1, stdout=sp.PIPE, stderr=sp.STDOUT, close_fds=sys.platform != 'win32') for line in proc.stdout: print(line[:-1].decode()) retcode = proc.wait() if retcode: raise sp.CalledProcessError(retcode, cmds)
[ "def", "run", "(", "cmds", ",", "*", "*", "kwargs", ")", ":", "proc", "=", "sp", ".", "Popen", "(", "cmds", ",", "bufsize", "=", "-", "1", ",", "stdout", "=", "sp", ".", "PIPE", ",", "stderr", "=", "sp", ".", "STDOUT", ",", "close_fds", "=", ...
Wrapper around subprocess.run, with unicode decoding of output. Additional kwargs are passed to subprocess.run.
[ "Wrapper", "around", "subprocess", ".", "run", "with", "unicode", "decoding", "of", "output", "." ]
e4655f79177822529f80b923df117e38e28df702
https://github.com/daler/trackhub/blob/e4655f79177822529f80b923df117e38e28df702/trackhub/upload.py#L18-L30
16,807
daler/trackhub
trackhub/upload.py
symlink
def symlink(target, linkname): """ Create a symlink to `target` called `linkname`. Converts `target` and `linkname` to absolute paths; creates `dirname(linkname)` if needed. """ target = os.path.abspath(target) linkname = os.path.abspath(linkname) if not os.path.exists(target): raise ValueError("target {} not found".format(target)) link_dir = os.path.dirname(linkname) if not os.path.exists(link_dir): os.makedirs(link_dir) run(['ln', '-s', '-f', target, linkname]) return linkname
python
def symlink(target, linkname): target = os.path.abspath(target) linkname = os.path.abspath(linkname) if not os.path.exists(target): raise ValueError("target {} not found".format(target)) link_dir = os.path.dirname(linkname) if not os.path.exists(link_dir): os.makedirs(link_dir) run(['ln', '-s', '-f', target, linkname]) return linkname
[ "def", "symlink", "(", "target", ",", "linkname", ")", ":", "target", "=", "os", ".", "path", ".", "abspath", "(", "target", ")", "linkname", "=", "os", ".", "path", ".", "abspath", "(", "linkname", ")", "if", "not", "os", ".", "path", ".", "exists...
Create a symlink to `target` called `linkname`. Converts `target` and `linkname` to absolute paths; creates `dirname(linkname)` if needed.
[ "Create", "a", "symlink", "to", "target", "called", "linkname", "." ]
e4655f79177822529f80b923df117e38e28df702
https://github.com/daler/trackhub/blob/e4655f79177822529f80b923df117e38e28df702/trackhub/upload.py#L33-L50
16,808
daler/trackhub
trackhub/upload.py
upload
def upload(host, user, local_dir, remote_dir, rsync_options=RSYNC_OPTIONS): """ Upload a file or directory via rsync. Parameters ---------- host : str or None If None, omit the host part and just transfer locally user : str or None If None, omit the user part local_dir : str If a directory, a trailing "/" will be added. remote_dir : str If a directory, a trailing "/" will be added. """ if user is None: user = "" else: user = user + "@" if host is None or host == 'localhost': host = "" else: host = host + ":" if not local_dir.endswith('/'): local_dir = local_dir + '/' if not remote_dir.endswith('/'): remote_dir = remote_dir + '/' remote_string = '{user}{host}{remote_dir}'.format(**locals()) cmds = ['rsync'] cmds += shlex.split(rsync_options) cmds += [local_dir, remote_string] run(cmds) return [remote_string]
python
def upload(host, user, local_dir, remote_dir, rsync_options=RSYNC_OPTIONS): if user is None: user = "" else: user = user + "@" if host is None or host == 'localhost': host = "" else: host = host + ":" if not local_dir.endswith('/'): local_dir = local_dir + '/' if not remote_dir.endswith('/'): remote_dir = remote_dir + '/' remote_string = '{user}{host}{remote_dir}'.format(**locals()) cmds = ['rsync'] cmds += shlex.split(rsync_options) cmds += [local_dir, remote_string] run(cmds) return [remote_string]
[ "def", "upload", "(", "host", ",", "user", ",", "local_dir", ",", "remote_dir", ",", "rsync_options", "=", "RSYNC_OPTIONS", ")", ":", "if", "user", "is", "None", ":", "user", "=", "\"\"", "else", ":", "user", "=", "user", "+", "\"@\"", "if", "host", ...
Upload a file or directory via rsync. Parameters ---------- host : str or None If None, omit the host part and just transfer locally user : str or None If None, omit the user part local_dir : str If a directory, a trailing "/" will be added. remote_dir : str If a directory, a trailing "/" will be added.
[ "Upload", "a", "file", "or", "directory", "via", "rsync", "." ]
e4655f79177822529f80b923df117e38e28df702
https://github.com/daler/trackhub/blob/e4655f79177822529f80b923df117e38e28df702/trackhub/upload.py#L53-L91
16,809
daler/trackhub
trackhub/upload.py
local_link
def local_link(local_fn, remote_fn, staging): """ Creates a symlink to a local staging area. The link name is built from `remote_fn`, but the absolute path is put inside the staging directory. Example ------- If we have the following initial setup:: cwd="/home/user" local="data/sample1.bw" remote="/hubs/hg19/a.bw" staging="__staging__" Then this function does the equivalent of:: mkdir -p __staging__/hubs/hg19 ln -sf \\ /home/user/data/sample1.bw \\ /home/user/__staging__/hubs/hg19/a.bw """ linkname = os.path.join(staging, remote_fn.lstrip(os.path.sep)) return symlink(local_fn, linkname)
python
def local_link(local_fn, remote_fn, staging): linkname = os.path.join(staging, remote_fn.lstrip(os.path.sep)) return symlink(local_fn, linkname)
[ "def", "local_link", "(", "local_fn", ",", "remote_fn", ",", "staging", ")", ":", "linkname", "=", "os", ".", "path", ".", "join", "(", "staging", ",", "remote_fn", ".", "lstrip", "(", "os", ".", "path", ".", "sep", ")", ")", "return", "symlink", "("...
Creates a symlink to a local staging area. The link name is built from `remote_fn`, but the absolute path is put inside the staging directory. Example ------- If we have the following initial setup:: cwd="/home/user" local="data/sample1.bw" remote="/hubs/hg19/a.bw" staging="__staging__" Then this function does the equivalent of:: mkdir -p __staging__/hubs/hg19 ln -sf \\ /home/user/data/sample1.bw \\ /home/user/__staging__/hubs/hg19/a.bw
[ "Creates", "a", "symlink", "to", "a", "local", "staging", "area", "." ]
e4655f79177822529f80b923df117e38e28df702
https://github.com/daler/trackhub/blob/e4655f79177822529f80b923df117e38e28df702/trackhub/upload.py#L94-L119
16,810
daler/trackhub
trackhub/upload.py
stage
def stage(x, staging): """ Stage an object to the `staging` directory. If the object is a Track and is one of the types that needs an index file (bam, vcfTabix), then the index file will be staged as well. Returns a list of the linknames created. """ linknames = [] # Objects that don't represent a file shouldn't be staged non_file_objects = ( track.ViewTrack, track.CompositeTrack, track.AggregateTrack, track.SuperTrack, genome.Genome, ) if isinstance(x, non_file_objects): return linknames # If it's an object representing a file, then render it. # # Track objects don't represent files, but their documentation does linknames.append(x.render(staging)) if hasattr(x, 'source') and hasattr(x, 'filename'): def _stg(x, ext=''): # A remote track hosted elsewhere does not need staging. This is # defined by a track with a url, but no source or filename. if ( x.source is None and x.filename is None and getattr(x, 'url', None) is not None ): return linknames.append( local_link(x.source + ext, x.filename + ext, staging) ) _stg(x) if isinstance(x, track.Track): if x.tracktype == 'bam': _stg(x, ext='.bai') if x.tracktype == 'vcfTabix': _stg(x, ext='.tbi') if isinstance(x, track.CompositeTrack): if x._html: _stg(x._html) return linknames
python
def stage(x, staging): linknames = [] # Objects that don't represent a file shouldn't be staged non_file_objects = ( track.ViewTrack, track.CompositeTrack, track.AggregateTrack, track.SuperTrack, genome.Genome, ) if isinstance(x, non_file_objects): return linknames # If it's an object representing a file, then render it. # # Track objects don't represent files, but their documentation does linknames.append(x.render(staging)) if hasattr(x, 'source') and hasattr(x, 'filename'): def _stg(x, ext=''): # A remote track hosted elsewhere does not need staging. This is # defined by a track with a url, but no source or filename. if ( x.source is None and x.filename is None and getattr(x, 'url', None) is not None ): return linknames.append( local_link(x.source + ext, x.filename + ext, staging) ) _stg(x) if isinstance(x, track.Track): if x.tracktype == 'bam': _stg(x, ext='.bai') if x.tracktype == 'vcfTabix': _stg(x, ext='.tbi') if isinstance(x, track.CompositeTrack): if x._html: _stg(x._html) return linknames
[ "def", "stage", "(", "x", ",", "staging", ")", ":", "linknames", "=", "[", "]", "# Objects that don't represent a file shouldn't be staged", "non_file_objects", "=", "(", "track", ".", "ViewTrack", ",", "track", ".", "CompositeTrack", ",", "track", ".", "Aggregate...
Stage an object to the `staging` directory. If the object is a Track and is one of the types that needs an index file (bam, vcfTabix), then the index file will be staged as well. Returns a list of the linknames created.
[ "Stage", "an", "object", "to", "the", "staging", "directory", "." ]
e4655f79177822529f80b923df117e38e28df702
https://github.com/daler/trackhub/blob/e4655f79177822529f80b923df117e38e28df702/trackhub/upload.py#L122-L176
16,811
daler/trackhub
trackhub/upload.py
stage_hub
def stage_hub(hub, staging=None): """ Stage a hub by symlinking all its connected files to a local directory. """ linknames = [] if staging is None: staging = tempfile.mkdtemp() for obj, level in hub.leaves(base.HubComponent, intermediate=True): linknames.extend(stage(obj, staging)) return staging, linknames
python
def stage_hub(hub, staging=None): linknames = [] if staging is None: staging = tempfile.mkdtemp() for obj, level in hub.leaves(base.HubComponent, intermediate=True): linknames.extend(stage(obj, staging)) return staging, linknames
[ "def", "stage_hub", "(", "hub", ",", "staging", "=", "None", ")", ":", "linknames", "=", "[", "]", "if", "staging", "is", "None", ":", "staging", "=", "tempfile", ".", "mkdtemp", "(", ")", "for", "obj", ",", "level", "in", "hub", ".", "leaves", "("...
Stage a hub by symlinking all its connected files to a local directory.
[ "Stage", "a", "hub", "by", "symlinking", "all", "its", "connected", "files", "to", "a", "local", "directory", "." ]
e4655f79177822529f80b923df117e38e28df702
https://github.com/daler/trackhub/blob/e4655f79177822529f80b923df117e38e28df702/trackhub/upload.py#L180-L190
16,812
daler/trackhub
trackhub/upload.py
upload_hub
def upload_hub(hub, host, remote_dir, user=None, port=22, rsync_options=RSYNC_OPTIONS, staging=None): """ Renders, stages, and uploads a hub. """ hub.render() if staging is None: staging = tempfile.mkdtemp() staging, linknames = stage_hub(hub, staging=staging) local_dir = os.path.join(staging) upload(host, user, local_dir=local_dir, remote_dir=remote_dir, rsync_options=rsync_options) return linknames
python
def upload_hub(hub, host, remote_dir, user=None, port=22, rsync_options=RSYNC_OPTIONS, staging=None): hub.render() if staging is None: staging = tempfile.mkdtemp() staging, linknames = stage_hub(hub, staging=staging) local_dir = os.path.join(staging) upload(host, user, local_dir=local_dir, remote_dir=remote_dir, rsync_options=rsync_options) return linknames
[ "def", "upload_hub", "(", "hub", ",", "host", ",", "remote_dir", ",", "user", "=", "None", ",", "port", "=", "22", ",", "rsync_options", "=", "RSYNC_OPTIONS", ",", "staging", "=", "None", ")", ":", "hub", ".", "render", "(", ")", "if", "staging", "is...
Renders, stages, and uploads a hub.
[ "Renders", "stages", "and", "uploads", "a", "hub", "." ]
e4655f79177822529f80b923df117e38e28df702
https://github.com/daler/trackhub/blob/e4655f79177822529f80b923df117e38e28df702/trackhub/upload.py#L193-L203
16,813
Karaage-Cluster/karaage
karaage/projects/xmlrpc.py
get_project_members
def get_project_members(machine, project_id): """ Returns list of usernames given a project id """ try: project = Project.objects.get(pid=project_id) except Project.DoesNotExist: return 'Project not found' return [x.username for x in project.group.members.all()]
python
def get_project_members(machine, project_id): try: project = Project.objects.get(pid=project_id) except Project.DoesNotExist: return 'Project not found' return [x.username for x in project.group.members.all()]
[ "def", "get_project_members", "(", "machine", ",", "project_id", ")", ":", "try", ":", "project", "=", "Project", ".", "objects", ".", "get", "(", "pid", "=", "project_id", ")", "except", "Project", ".", "DoesNotExist", ":", "return", "'Project not found'", ...
Returns list of usernames given a project id
[ "Returns", "list", "of", "usernames", "given", "a", "project", "id" ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/projects/xmlrpc.py#L28-L37
16,814
Karaage-Cluster/karaage
karaage/projects/xmlrpc.py
get_projects
def get_projects(machine): """ Returns list of project ids """ query = Project.active.all() return [x.pid for x in query]
python
def get_projects(machine): query = Project.active.all() return [x.pid for x in query]
[ "def", "get_projects", "(", "machine", ")", ":", "query", "=", "Project", ".", "active", ".", "all", "(", ")", "return", "[", "x", ".", "pid", "for", "x", "in", "query", "]" ]
Returns list of project ids
[ "Returns", "list", "of", "project", "ids" ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/projects/xmlrpc.py#L42-L48
16,815
Karaage-Cluster/karaage
karaage/projects/xmlrpc.py
get_project
def get_project(username, project, machine_name=None): """ Used in the submit filter to make sure user is in project """ try: account = Account.objects.get( username=username, date_deleted__isnull=True) except Account.DoesNotExist: return "Account '%s' not found" % username if project is None: project = account.default_project else: try: project = Project.objects.get(pid=project) except Project.DoesNotExist: project = account.default_project if project is None: return "None" if account.person not in project.group.members.all(): project = account.default_project if project is None: return "None" if account.person not in project.group.members.all(): return "None" return project.pid
python
def get_project(username, project, machine_name=None): try: account = Account.objects.get( username=username, date_deleted__isnull=True) except Account.DoesNotExist: return "Account '%s' not found" % username if project is None: project = account.default_project else: try: project = Project.objects.get(pid=project) except Project.DoesNotExist: project = account.default_project if project is None: return "None" if account.person not in project.group.members.all(): project = account.default_project if project is None: return "None" if account.person not in project.group.members.all(): return "None" return project.pid
[ "def", "get_project", "(", "username", ",", "project", ",", "machine_name", "=", "None", ")", ":", "try", ":", "account", "=", "Account", ".", "objects", ".", "get", "(", "username", "=", "username", ",", "date_deleted__isnull", "=", "True", ")", "except",...
Used in the submit filter to make sure user is in project
[ "Used", "in", "the", "submit", "filter", "to", "make", "sure", "user", "is", "in", "project" ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/projects/xmlrpc.py#L52-L84
16,816
Karaage-Cluster/karaage
karaage/projects/xmlrpc.py
get_users_projects
def get_users_projects(user): """ List projects a user is part of """ person = user projects = person.projects.filter(is_active=True) return 0, [x.pid for x in projects]
python
def get_users_projects(user): person = user projects = person.projects.filter(is_active=True) return 0, [x.pid for x in projects]
[ "def", "get_users_projects", "(", "user", ")", ":", "person", "=", "user", "projects", "=", "person", ".", "projects", ".", "filter", "(", "is_active", "=", "True", ")", "return", "0", ",", "[", "x", ".", "pid", "for", "x", "in", "projects", "]" ]
List projects a user is part of
[ "List", "projects", "a", "user", "is", "part", "of" ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/projects/xmlrpc.py#L89-L95
16,817
Karaage-Cluster/karaage
karaage/common/simple.py
redirect_to
def redirect_to(request, url, permanent=True, query_string=False, **kwargs): r""" Redirect to a given URL. The given url may contain dict-style string formatting, which will be interpolated against the params in the URL. For example, to redirect from ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following URLconf:: urlpatterns = patterns('', (r'^foo/(?P<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}), ) If the given url is ``None``, a HttpResponseGone (410) will be issued. If the ``permanent`` argument is False, then the response will have a 302 HTTP status code. Otherwise, the status code will be 301. If the ``query_string`` argument is True, then the GET query string from the request is appended to the URL. """ args = request.META.get('QUERY_STRING', '') if url is not None: if kwargs: url = url % kwargs if args and query_string: url = "%s?%s" % (url, args) klass = (permanent and HttpResponsePermanentRedirect or HttpResponseRedirect) return klass(url) else: logger.warning( 'Gone: %s', request.path, extra={ 'status_code': 410, 'request': request }) return HttpResponseGone()
python
def redirect_to(request, url, permanent=True, query_string=False, **kwargs): r""" Redirect to a given URL. The given url may contain dict-style string formatting, which will be interpolated against the params in the URL. For example, to redirect from ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following URLconf:: urlpatterns = patterns('', (r'^foo/(?P<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}), ) If the given url is ``None``, a HttpResponseGone (410) will be issued. If the ``permanent`` argument is False, then the response will have a 302 HTTP status code. Otherwise, the status code will be 301. If the ``query_string`` argument is True, then the GET query string from the request is appended to the URL. """ args = request.META.get('QUERY_STRING', '') if url is not None: if kwargs: url = url % kwargs if args and query_string: url = "%s?%s" % (url, args) klass = (permanent and HttpResponsePermanentRedirect or HttpResponseRedirect) return klass(url) else: logger.warning( 'Gone: %s', request.path, extra={ 'status_code': 410, 'request': request }) return HttpResponseGone()
[ "def", "redirect_to", "(", "request", ",", "url", ",", "permanent", "=", "True", ",", "query_string", "=", "False", ",", "*", "*", "kwargs", ")", ":", "args", "=", "request", ".", "META", ".", "get", "(", "'QUERY_STRING'", ",", "''", ")", "if", "url"...
r""" Redirect to a given URL. The given url may contain dict-style string formatting, which will be interpolated against the params in the URL. For example, to redirect from ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following URLconf:: urlpatterns = patterns('', (r'^foo/(?P<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}), ) If the given url is ``None``, a HttpResponseGone (410) will be issued. If the ``permanent`` argument is False, then the response will have a 302 HTTP status code. Otherwise, the status code will be 301. If the ``query_string`` argument is True, then the GET query string from the request is appended to the URL.
[ "r", "Redirect", "to", "a", "given", "URL", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/common/simple.py#L55-L98
16,818
Karaage-Cluster/karaage
karaage/common/passwords.py
assert_strong_password
def assert_strong_password(username, password, old_password=None): """Raises ValueError if the password isn't strong. Returns the password otherwise.""" # test the length try: minlength = settings.MIN_PASSWORD_LENGTH except AttributeError: minlength = 12 if len(password) < minlength: raise ValueError( "Password must be at least %s characters long" % minlength) if username is not None and username in password: raise ValueError("Password contains username") return _assert_password(password, old_password)
python
def assert_strong_password(username, password, old_password=None): # test the length try: minlength = settings.MIN_PASSWORD_LENGTH except AttributeError: minlength = 12 if len(password) < minlength: raise ValueError( "Password must be at least %s characters long" % minlength) if username is not None and username in password: raise ValueError("Password contains username") return _assert_password(password, old_password)
[ "def", "assert_strong_password", "(", "username", ",", "password", ",", "old_password", "=", "None", ")", ":", "# test the length", "try", ":", "minlength", "=", "settings", ".", "MIN_PASSWORD_LENGTH", "except", "AttributeError", ":", "minlength", "=", "12", "if",...
Raises ValueError if the password isn't strong. Returns the password otherwise.
[ "Raises", "ValueError", "if", "the", "password", "isn", "t", "strong", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/common/passwords.py#L50-L67
16,819
Karaage-Cluster/karaage
karaage/datastores/ldap.py
_lookup
def _lookup(cls: str) -> LdapObjectClass: """ Lookup module.class. """ if isinstance(cls, str): module_name, _, name = cls.rpartition(".") module = importlib.import_module(module_name) try: cls = getattr(module, name) except AttributeError: raise AttributeError("%s reference cannot be found" % cls) return cls
python
def _lookup(cls: str) -> LdapObjectClass: if isinstance(cls, str): module_name, _, name = cls.rpartition(".") module = importlib.import_module(module_name) try: cls = getattr(module, name) except AttributeError: raise AttributeError("%s reference cannot be found" % cls) return cls
[ "def", "_lookup", "(", "cls", ":", "str", ")", "->", "LdapObjectClass", ":", "if", "isinstance", "(", "cls", ",", "str", ")", ":", "module_name", ",", "_", ",", "name", "=", "cls", ".", "rpartition", "(", "\".\"", ")", "module", "=", "importlib", "."...
Lookup module.class.
[ "Lookup", "module", ".", "class", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/ldap.py#L55-L64
16,820
Karaage-Cluster/karaage
karaage/datastores/ldap.py
DataStore.save_account
def save_account(self, account: Account) -> None: """ Account was saved. """ person = account.person if self._primary_group == 'institute': lgroup = self._get_group(person.institute.group.name) elif self._primary_group == 'default_project': if account.default_project is None: lgroup = self._get_group(self._default_primary_group) else: lgroup = self._get_group(account.default_project.group.name) else: raise RuntimeError("Unknown value of PRIMARY_GROUP.") if account.default_project is None: default_project = "none" else: default_project = account.default_project.pid try: luser = self._get_account(account.username) changes = changeset(luser, {}) new_user = False except ObjectDoesNotExist: new_user = True luser = self._account_class() changes = changeset(luser, { 'uid': account.username }) changes = changes.merge({ 'gidNumber': lgroup['gidNumber'], 'givenName': person.first_name, 'sn': person.last_name, 'telephoneNumber': _str_or_none(person.telephone), 'mail': _str_or_none(person.email), 'title': _str_or_none(person.title), 'o': person.institute.name, 'cn': person.full_name, 'default_project': default_project, 'loginShell': account.shell, 'locked': account.is_locked() }) save(changes, database=self._database) if new_user: # add all groups for group in account.person.groups.all(): self.add_account_to_group(account, group)
python
def save_account(self, account: Account) -> None: person = account.person if self._primary_group == 'institute': lgroup = self._get_group(person.institute.group.name) elif self._primary_group == 'default_project': if account.default_project is None: lgroup = self._get_group(self._default_primary_group) else: lgroup = self._get_group(account.default_project.group.name) else: raise RuntimeError("Unknown value of PRIMARY_GROUP.") if account.default_project is None: default_project = "none" else: default_project = account.default_project.pid try: luser = self._get_account(account.username) changes = changeset(luser, {}) new_user = False except ObjectDoesNotExist: new_user = True luser = self._account_class() changes = changeset(luser, { 'uid': account.username }) changes = changes.merge({ 'gidNumber': lgroup['gidNumber'], 'givenName': person.first_name, 'sn': person.last_name, 'telephoneNumber': _str_or_none(person.telephone), 'mail': _str_or_none(person.email), 'title': _str_or_none(person.title), 'o': person.institute.name, 'cn': person.full_name, 'default_project': default_project, 'loginShell': account.shell, 'locked': account.is_locked() }) save(changes, database=self._database) if new_user: # add all groups for group in account.person.groups.all(): self.add_account_to_group(account, group)
[ "def", "save_account", "(", "self", ",", "account", ":", "Account", ")", "->", "None", ":", "person", "=", "account", ".", "person", "if", "self", ".", "_primary_group", "==", "'institute'", ":", "lgroup", "=", "self", ".", "_get_group", "(", "person", "...
Account was saved.
[ "Account", "was", "saved", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/ldap.py#L97-L144
16,821
Karaage-Cluster/karaage
karaage/datastores/ldap.py
DataStore.delete_account
def delete_account(self, account): """ Account was deleted. """ try: luser = self._get_account(account.username) groups = luser['groups'].load(database=self._database) for group in groups: changes = changeset(group, {}) changes = group.remove_member(changes, luser) save(changes, database=self._database) delete(luser, database=self._database) except ObjectDoesNotExist: # it doesn't matter if it doesn't exist pass
python
def delete_account(self, account): try: luser = self._get_account(account.username) groups = luser['groups'].load(database=self._database) for group in groups: changes = changeset(group, {}) changes = group.remove_member(changes, luser) save(changes, database=self._database) delete(luser, database=self._database) except ObjectDoesNotExist: # it doesn't matter if it doesn't exist pass
[ "def", "delete_account", "(", "self", ",", "account", ")", ":", "try", ":", "luser", "=", "self", ".", "_get_account", "(", "account", ".", "username", ")", "groups", "=", "luser", "[", "'groups'", "]", ".", "load", "(", "database", "=", "self", ".", ...
Account was deleted.
[ "Account", "was", "deleted", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/ldap.py#L146-L159
16,822
Karaage-Cluster/karaage
karaage/datastores/ldap.py
DataStore.set_account_password
def set_account_password(self, account, raw_password): """ Account's password was changed. """ luser = self._get_account(account.username) changes = changeset(luser, { 'password': raw_password, }) save(changes, database=self._database)
python
def set_account_password(self, account, raw_password): luser = self._get_account(account.username) changes = changeset(luser, { 'password': raw_password, }) save(changes, database=self._database)
[ "def", "set_account_password", "(", "self", ",", "account", ",", "raw_password", ")", ":", "luser", "=", "self", ".", "_get_account", "(", "account", ".", "username", ")", "changes", "=", "changeset", "(", "luser", ",", "{", "'password'", ":", "raw_password"...
Account's password was changed.
[ "Account", "s", "password", "was", "changed", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/ldap.py#L161-L167
16,823
Karaage-Cluster/karaage
karaage/datastores/ldap.py
DataStore.add_account_to_group
def add_account_to_group(self, account, group): """ Add account to group. """ lgroup: OpenldapGroup = self._get_group(group.name) person: OpenldapAccount = self._get_account(account.username) changes = changeset(lgroup, {}) changes = lgroup.add_member(changes, person) save(changes, database=self._database)
python
def add_account_to_group(self, account, group): lgroup: OpenldapGroup = self._get_group(group.name) person: OpenldapAccount = self._get_account(account.username) changes = changeset(lgroup, {}) changes = lgroup.add_member(changes, person) save(changes, database=self._database)
[ "def", "add_account_to_group", "(", "self", ",", "account", ",", "group", ")", ":", "lgroup", ":", "OpenldapGroup", "=", "self", ".", "_get_group", "(", "group", ".", "name", ")", "person", ":", "OpenldapAccount", "=", "self", ".", "_get_account", "(", "ac...
Add account to group.
[ "Add", "account", "to", "group", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/ldap.py#L174-L181
16,824
Karaage-Cluster/karaage
karaage/datastores/ldap.py
DataStore.save_group
def save_group(self, group): """ Group was saved. """ # If group already exists, take over existing group rather then error. try: lgroup = self._get_group(group.name) changes = changeset(lgroup, {}) except ObjectDoesNotExist: lgroup = self._group_class() changes = changeset(lgroup, { 'cn': group.name, }) changes = changes.merge({ 'description': group.description }) save(changes, database=self._database)
python
def save_group(self, group): # If group already exists, take over existing group rather then error. try: lgroup = self._get_group(group.name) changes = changeset(lgroup, {}) except ObjectDoesNotExist: lgroup = self._group_class() changes = changeset(lgroup, { 'cn': group.name, }) changes = changes.merge({ 'description': group.description }) save(changes, database=self._database)
[ "def", "save_group", "(", "self", ",", "group", ")", ":", "# If group already exists, take over existing group rather then error.", "try", ":", "lgroup", "=", "self", ".", "_get_group", "(", "group", ".", "name", ")", "changes", "=", "changeset", "(", "lgroup", ",...
Group was saved.
[ "Group", "was", "saved", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/ldap.py#L215-L230
16,825
Karaage-Cluster/karaage
karaage/datastores/ldap.py
DataStore.delete_group
def delete_group(self, group): """ Group was deleted. """ try: lgroup = self._get_group(group.name) delete(lgroup, database=self._database) except ObjectDoesNotExist: # it doesn't matter if it doesn't exist pass
python
def delete_group(self, group): try: lgroup = self._get_group(group.name) delete(lgroup, database=self._database) except ObjectDoesNotExist: # it doesn't matter if it doesn't exist pass
[ "def", "delete_group", "(", "self", ",", "group", ")", ":", "try", ":", "lgroup", "=", "self", ".", "_get_group", "(", "group", ".", "name", ")", "delete", "(", "lgroup", ",", "database", "=", "self", ".", "_database", ")", "except", "ObjectDoesNotExist"...
Group was deleted.
[ "Group", "was", "deleted", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/ldap.py#L232-L239
16,826
Karaage-Cluster/karaage
karaage/datastores/ldap.py
DataStore.get_group_details
def get_group_details(self, group): """ Get the group details. """ result = {} try: lgroup = self._get_group(group.name) lgroup = preload(lgroup, database=self._database) except ObjectDoesNotExist: return result for i, j in lgroup.items(): if j is not None: result[i] = j return result
python
def get_group_details(self, group): result = {} try: lgroup = self._get_group(group.name) lgroup = preload(lgroup, database=self._database) except ObjectDoesNotExist: return result for i, j in lgroup.items(): if j is not None: result[i] = j return result
[ "def", "get_group_details", "(", "self", ",", "group", ")", ":", "result", "=", "{", "}", "try", ":", "lgroup", "=", "self", ".", "_get_group", "(", "group", ".", "name", ")", "lgroup", "=", "preload", "(", "lgroup", ",", "database", "=", "self", "."...
Get the group details.
[ "Get", "the", "group", "details", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/ldap.py#L246-L259
16,827
mozilla/python_moztelemetry
moztelemetry/stats.py
_rank
def _rank(sample): """ Assign numeric ranks to all values in the sample. The ranks begin with 1 for the smallest value. When there are groups of tied values, assign a rank equal to the midpoint of unadjusted rankings. E.g.:: >>> rank({3: 1, 5: 4, 9: 1}) {3: 1.0, 5: 3.5, 9: 6.0} """ rank = 1 ranks = {} for k in sorted(sample.keys()): n = sample[k] ranks[k] = rank + (n - 1) / 2 rank += n return ranks
python
def _rank(sample): rank = 1 ranks = {} for k in sorted(sample.keys()): n = sample[k] ranks[k] = rank + (n - 1) / 2 rank += n return ranks
[ "def", "_rank", "(", "sample", ")", ":", "rank", "=", "1", "ranks", "=", "{", "}", "for", "k", "in", "sorted", "(", "sample", ".", "keys", "(", ")", ")", ":", "n", "=", "sample", "[", "k", "]", "ranks", "[", "k", "]", "=", "rank", "+", "(",...
Assign numeric ranks to all values in the sample. The ranks begin with 1 for the smallest value. When there are groups of tied values, assign a rank equal to the midpoint of unadjusted rankings. E.g.:: >>> rank({3: 1, 5: 4, 9: 1}) {3: 1.0, 5: 3.5, 9: 6.0}
[ "Assign", "numeric", "ranks", "to", "all", "values", "in", "the", "sample", "." ]
09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb
https://github.com/mozilla/python_moztelemetry/blob/09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb/moztelemetry/stats.py#L14-L35
16,828
mozilla/python_moztelemetry
moztelemetry/stats.py
_tie_correct
def _tie_correct(sample): """ Returns the tie correction value for U. See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.tiecorrect.html """ tc = 0 n = sum(sample.values()) if n < 2: return 1.0 # Avoid a ``ZeroDivisionError``. for k in sorted(sample.keys()): tc += math.pow(sample[k], 3) - sample[k] tc = 1 - tc / (math.pow(n, 3) - n) return tc
python
def _tie_correct(sample): tc = 0 n = sum(sample.values()) if n < 2: return 1.0 # Avoid a ``ZeroDivisionError``. for k in sorted(sample.keys()): tc += math.pow(sample[k], 3) - sample[k] tc = 1 - tc / (math.pow(n, 3) - n) return tc
[ "def", "_tie_correct", "(", "sample", ")", ":", "tc", "=", "0", "n", "=", "sum", "(", "sample", ".", "values", "(", ")", ")", "if", "n", "<", "2", ":", "return", "1.0", "# Avoid a ``ZeroDivisionError``.", "for", "k", "in", "sorted", "(", "sample", "....
Returns the tie correction value for U. See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.tiecorrect.html
[ "Returns", "the", "tie", "correction", "value", "for", "U", "." ]
09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb
https://github.com/mozilla/python_moztelemetry/blob/09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb/moztelemetry/stats.py#L38-L55
16,829
mozilla/python_moztelemetry
moztelemetry/stats.py
ndtr
def ndtr(a): """ Returns the area under the Gaussian probability density function, integrated from minus infinity to x. See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.ndtr.html#scipy.special.ndtr """ sqrth = math.sqrt(2) / 2 x = float(a) * sqrth z = abs(x) if z < sqrth: y = 0.5 + 0.5 * math.erf(x) else: y = 0.5 * math.erfc(z) if x > 0: y = 1 - y return y
python
def ndtr(a): sqrth = math.sqrt(2) / 2 x = float(a) * sqrth z = abs(x) if z < sqrth: y = 0.5 + 0.5 * math.erf(x) else: y = 0.5 * math.erfc(z) if x > 0: y = 1 - y return y
[ "def", "ndtr", "(", "a", ")", ":", "sqrth", "=", "math", ".", "sqrt", "(", "2", ")", "/", "2", "x", "=", "float", "(", "a", ")", "*", "sqrth", "z", "=", "abs", "(", "x", ")", "if", "z", "<", "sqrth", ":", "y", "=", "0.5", "+", "0.5", "*...
Returns the area under the Gaussian probability density function, integrated from minus infinity to x. See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.ndtr.html#scipy.special.ndtr
[ "Returns", "the", "area", "under", "the", "Gaussian", "probability", "density", "function", "integrated", "from", "minus", "infinity", "to", "x", "." ]
09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb
https://github.com/mozilla/python_moztelemetry/blob/09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb/moztelemetry/stats.py#L58-L75
16,830
mozilla/python_moztelemetry
moztelemetry/stats.py
mann_whitney_u
def mann_whitney_u(sample1, sample2, use_continuity=True): """ Computes the Mann-Whitney rank test on both samples. Each sample is expected to be of the form:: {1: 5, 2: 20, 3: 12, ...} Returns a named tuple with: ``u`` equal to min(U for sample1, U for sample2), and ``p`` equal to the p-value. """ # Merge dictionaries, adding values if keys match. sample = sample1.copy() for k, v in sample2.items(): sample[k] = sample.get(k, 0) + v # Create a ranking dictionary using same keys for lookups. ranks = _rank(sample) sum_of_ranks = sum([sample1[k] * ranks[k] for k, v in sample1.items()]) n1 = sum(sample1.values()) n2 = sum(sample2.values()) # Calculate Mann-Whitney U for both samples. u1 = sum_of_ranks - (n1 * (n1 + 1)) / 2 u2 = n1 * n2 - u1 tie_correction = _tie_correct(sample) if tie_correction == 0: raise ValueError('All provided sample values are identical.') sd_u = math.sqrt(tie_correction * n1 * n2 * (n1 + n2 + 1) / 12.0) mean_rank = n1 * n2 / 2.0 + 0.5 * use_continuity z = abs((max(u1, u2) - mean_rank) / sd_u) return mwu_result(min(u1, u2), ndtr(-z))
python
def mann_whitney_u(sample1, sample2, use_continuity=True): # Merge dictionaries, adding values if keys match. sample = sample1.copy() for k, v in sample2.items(): sample[k] = sample.get(k, 0) + v # Create a ranking dictionary using same keys for lookups. ranks = _rank(sample) sum_of_ranks = sum([sample1[k] * ranks[k] for k, v in sample1.items()]) n1 = sum(sample1.values()) n2 = sum(sample2.values()) # Calculate Mann-Whitney U for both samples. u1 = sum_of_ranks - (n1 * (n1 + 1)) / 2 u2 = n1 * n2 - u1 tie_correction = _tie_correct(sample) if tie_correction == 0: raise ValueError('All provided sample values are identical.') sd_u = math.sqrt(tie_correction * n1 * n2 * (n1 + n2 + 1) / 12.0) mean_rank = n1 * n2 / 2.0 + 0.5 * use_continuity z = abs((max(u1, u2) - mean_rank) / sd_u) return mwu_result(min(u1, u2), ndtr(-z))
[ "def", "mann_whitney_u", "(", "sample1", ",", "sample2", ",", "use_continuity", "=", "True", ")", ":", "# Merge dictionaries, adding values if keys match.", "sample", "=", "sample1", ".", "copy", "(", ")", "for", "k", ",", "v", "in", "sample2", ".", "items", "...
Computes the Mann-Whitney rank test on both samples. Each sample is expected to be of the form:: {1: 5, 2: 20, 3: 12, ...} Returns a named tuple with: ``u`` equal to min(U for sample1, U for sample2), and ``p`` equal to the p-value.
[ "Computes", "the", "Mann", "-", "Whitney", "rank", "test", "on", "both", "samples", "." ]
09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb
https://github.com/mozilla/python_moztelemetry/blob/09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb/moztelemetry/stats.py#L81-L118
16,831
mozilla/python_moztelemetry
moztelemetry/spark.py
get_pings
def get_pings(sc, app=None, build_id=None, channel=None, doc_type='saved_session', fraction=1.0, schema=None, source_name='telemetry', source_version='4', submission_date=None, version=None): """ Returns a RDD of Telemetry submissions for a given filtering criteria. :param sc: an instance of SparkContext :param app: an application name, e.g.: "Firefox" :param channel: a channel name, e.g.: "nightly" :param version: the application version, e.g.: "40.0a1" :param build_id: a build_id or a range of build_ids, e.g.: "20150601000000" or ("20150601000000", "20150610999999") :param submission_date: a submission date or a range of submission dates, e.g: "20150601" or ("20150601", "20150610") :param source_name: source name, set to "telemetry" by default :param source_version: source version, set to "4" by default :param doc_type: ping type, set to "saved_session" by default :param schema: (deprecated) version of the schema to use :param fraction: the fraction of pings to return, set to 1.0 by default """ if schema: print("The 'schema' parameter is deprecated. " "Version 4 is now the only schema supported.") if schema != "v4": raise ValueError("Invalid schema version") dataset = Dataset.from_source('telemetry') filters = ( ('docType', doc_type), ('sourceName', source_name), ('sourceVersion', source_version), ('appName', app), ('appUpdateChannel', channel), ('appVersion', version), ) for key, condition in filters: if condition and condition != '*': dataset = dataset.where(**{key: condition}) # build_id and submission_date can be either strings or tuples or lists, # so they deserve a special treatment special_cases = dict(appBuildId=build_id, submissionDate=submission_date) def range_compare(min_val, max_val, val): return min_val <= val <= max_val for key, value in iteritems(special_cases): if value is not None and value != '*': if isinstance(value, string_types): condition = value elif isinstance(value, (list, tuple)) and len(value) == 2: start, end = value condition = partial(range_compare, start, end) else: raise ValueError(('{} must be either a string or a 2 elements ' 'list/tuple'. format(key))) dataset = dataset.where(**{key: condition}) return dataset.records(sc, sample=fraction)
python
def get_pings(sc, app=None, build_id=None, channel=None, doc_type='saved_session', fraction=1.0, schema=None, source_name='telemetry', source_version='4', submission_date=None, version=None): if schema: print("The 'schema' parameter is deprecated. " "Version 4 is now the only schema supported.") if schema != "v4": raise ValueError("Invalid schema version") dataset = Dataset.from_source('telemetry') filters = ( ('docType', doc_type), ('sourceName', source_name), ('sourceVersion', source_version), ('appName', app), ('appUpdateChannel', channel), ('appVersion', version), ) for key, condition in filters: if condition and condition != '*': dataset = dataset.where(**{key: condition}) # build_id and submission_date can be either strings or tuples or lists, # so they deserve a special treatment special_cases = dict(appBuildId=build_id, submissionDate=submission_date) def range_compare(min_val, max_val, val): return min_val <= val <= max_val for key, value in iteritems(special_cases): if value is not None and value != '*': if isinstance(value, string_types): condition = value elif isinstance(value, (list, tuple)) and len(value) == 2: start, end = value condition = partial(range_compare, start, end) else: raise ValueError(('{} must be either a string or a 2 elements ' 'list/tuple'. format(key))) dataset = dataset.where(**{key: condition}) return dataset.records(sc, sample=fraction)
[ "def", "get_pings", "(", "sc", ",", "app", "=", "None", ",", "build_id", "=", "None", ",", "channel", "=", "None", ",", "doc_type", "=", "'saved_session'", ",", "fraction", "=", "1.0", ",", "schema", "=", "None", ",", "source_name", "=", "'telemetry'", ...
Returns a RDD of Telemetry submissions for a given filtering criteria. :param sc: an instance of SparkContext :param app: an application name, e.g.: "Firefox" :param channel: a channel name, e.g.: "nightly" :param version: the application version, e.g.: "40.0a1" :param build_id: a build_id or a range of build_ids, e.g.: "20150601000000" or ("20150601000000", "20150610999999") :param submission_date: a submission date or a range of submission dates, e.g: "20150601" or ("20150601", "20150610") :param source_name: source name, set to "telemetry" by default :param source_version: source version, set to "4" by default :param doc_type: ping type, set to "saved_session" by default :param schema: (deprecated) version of the schema to use :param fraction: the fraction of pings to return, set to 1.0 by default
[ "Returns", "a", "RDD", "of", "Telemetry", "submissions", "for", "a", "given", "filtering", "criteria", "." ]
09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb
https://github.com/mozilla/python_moztelemetry/blob/09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb/moztelemetry/spark.py#L70-L129
16,832
mozilla/python_moztelemetry
moztelemetry/spark.py
get_pings_properties
def get_pings_properties(pings, paths, only_median=False, with_processes=False, histograms_url=None, additional_histograms=None): """ Returns a RDD of a subset of properties of pings. Child histograms are automatically merged with the parent histogram. If one of the paths points to a keyedHistogram name without supplying the actual key, returns a dict of all available subhistograms for that property. :param with_processes: should separate parent and child histograms be included as well? :param paths: paths to properties in the payload, with levels separated by "/". These can be supplied either as a list, eg. ["application/channel", "payload/info/subsessionStartDate"], or as the values of a dict keyed by custom identifiers, eg. {"channel": "application/channel", "ssd": "payload/info/subsessionStartDate"}. :param histograms_url: see histogram.Histogram constructor :param additional_histograms: see histogram.Histogram constructor The returned RDD contains a dict for each ping with the required properties as values, keyed by the original paths (if 'paths' is a list) or the custom identifier keys (if 'paths' is a dict). """ if isinstance(pings.first(), binary_type): pings = pings.map(lambda p: json.loads(p.decode('utf-8'))) if isinstance(paths, str): paths = [paths] # Use '/' as dots can appear in keyed histograms if isinstance(paths, dict): paths = [(prop_name, path.split("/")) for prop_name, path in iteritems(paths)] else: paths = [(path, path.split("/")) for path in paths] return pings.map(lambda p: _get_ping_properties(p, paths, only_median, with_processes, histograms_url, additional_histograms)) \ .filter(lambda p: p)
python
def get_pings_properties(pings, paths, only_median=False, with_processes=False, histograms_url=None, additional_histograms=None): if isinstance(pings.first(), binary_type): pings = pings.map(lambda p: json.loads(p.decode('utf-8'))) if isinstance(paths, str): paths = [paths] # Use '/' as dots can appear in keyed histograms if isinstance(paths, dict): paths = [(prop_name, path.split("/")) for prop_name, path in iteritems(paths)] else: paths = [(path, path.split("/")) for path in paths] return pings.map(lambda p: _get_ping_properties(p, paths, only_median, with_processes, histograms_url, additional_histograms)) \ .filter(lambda p: p)
[ "def", "get_pings_properties", "(", "pings", ",", "paths", ",", "only_median", "=", "False", ",", "with_processes", "=", "False", ",", "histograms_url", "=", "None", ",", "additional_histograms", "=", "None", ")", ":", "if", "isinstance", "(", "pings", ".", ...
Returns a RDD of a subset of properties of pings. Child histograms are automatically merged with the parent histogram. If one of the paths points to a keyedHistogram name without supplying the actual key, returns a dict of all available subhistograms for that property. :param with_processes: should separate parent and child histograms be included as well? :param paths: paths to properties in the payload, with levels separated by "/". These can be supplied either as a list, eg. ["application/channel", "payload/info/subsessionStartDate"], or as the values of a dict keyed by custom identifiers, eg. {"channel": "application/channel", "ssd": "payload/info/subsessionStartDate"}. :param histograms_url: see histogram.Histogram constructor :param additional_histograms: see histogram.Histogram constructor The returned RDD contains a dict for each ping with the required properties as values, keyed by the original paths (if 'paths' is a list) or the custom identifier keys (if 'paths' is a dict).
[ "Returns", "a", "RDD", "of", "a", "subset", "of", "properties", "of", "pings", ".", "Child", "histograms", "are", "automatically", "merged", "with", "the", "parent", "histogram", "." ]
09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb
https://github.com/mozilla/python_moztelemetry/blob/09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb/moztelemetry/spark.py#L133-L172
16,833
mozilla/python_moztelemetry
moztelemetry/spark.py
get_one_ping_per_client
def get_one_ping_per_client(pings): """ Returns a single ping for each client in the RDD. THIS METHOD IS NOT RECOMMENDED: The ping to be returned is essentially selected at random. It is also expensive as it requires data to be shuffled around. It should be run only after extracting a subset with get_pings_properties. """ if isinstance(pings.first(), binary_type): pings = pings.map(lambda p: json.loads(p.decode('utf-8'))) filtered = pings.filter(lambda p: "clientID" in p or "clientId" in p) if not filtered: raise ValueError("Missing clientID/clientId attribute.") if "clientID" in filtered.first(): client_id = "clientID" # v2 else: client_id = "clientId" # v4 return filtered.map(lambda p: (p[client_id], p)) \ .reduceByKey(lambda p1, p2: p1) \ .map(lambda p: p[1])
python
def get_one_ping_per_client(pings): if isinstance(pings.first(), binary_type): pings = pings.map(lambda p: json.loads(p.decode('utf-8'))) filtered = pings.filter(lambda p: "clientID" in p or "clientId" in p) if not filtered: raise ValueError("Missing clientID/clientId attribute.") if "clientID" in filtered.first(): client_id = "clientID" # v2 else: client_id = "clientId" # v4 return filtered.map(lambda p: (p[client_id], p)) \ .reduceByKey(lambda p1, p2: p1) \ .map(lambda p: p[1])
[ "def", "get_one_ping_per_client", "(", "pings", ")", ":", "if", "isinstance", "(", "pings", ".", "first", "(", ")", ",", "binary_type", ")", ":", "pings", "=", "pings", ".", "map", "(", "lambda", "p", ":", "json", ".", "loads", "(", "p", ".", "decode...
Returns a single ping for each client in the RDD. THIS METHOD IS NOT RECOMMENDED: The ping to be returned is essentially selected at random. It is also expensive as it requires data to be shuffled around. It should be run only after extracting a subset with get_pings_properties.
[ "Returns", "a", "single", "ping", "for", "each", "client", "in", "the", "RDD", "." ]
09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb
https://github.com/mozilla/python_moztelemetry/blob/09ddf1ec7d953a4308dfdcb0ed968f27bd5921bb/moztelemetry/spark.py#L176-L200
16,834
Karaage-Cluster/karaage
karaage/common/decorators.py
admin_required
def admin_required(function=None): """ Decorator for views that checks that the user is an administrator, redirecting to the log-in page if necessary. """ def check_perms(user): # if user not logged in, show login form if not user.is_authenticated: return False # if this site doesn't allow admin access, fail if settings.ADMIN_IGNORED: raise PermissionDenied # check if the user has admin rights if not user.is_admin: raise PermissionDenied return True actual_decorator = user_passes_test(check_perms, login_url=_login_url) if function: return actual_decorator(function) return actual_decorator
python
def admin_required(function=None): def check_perms(user): # if user not logged in, show login form if not user.is_authenticated: return False # if this site doesn't allow admin access, fail if settings.ADMIN_IGNORED: raise PermissionDenied # check if the user has admin rights if not user.is_admin: raise PermissionDenied return True actual_decorator = user_passes_test(check_perms, login_url=_login_url) if function: return actual_decorator(function) return actual_decorator
[ "def", "admin_required", "(", "function", "=", "None", ")", ":", "def", "check_perms", "(", "user", ")", ":", "# if user not logged in, show login form", "if", "not", "user", ".", "is_authenticated", ":", "return", "False", "# if this site doesn't allow admin access, fa...
Decorator for views that checks that the user is an administrator, redirecting to the log-in page if necessary.
[ "Decorator", "for", "views", "that", "checks", "that", "the", "user", "is", "an", "administrator", "redirecting", "to", "the", "log", "-", "in", "page", "if", "necessary", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/common/decorators.py#L30-L50
16,835
Karaage-Cluster/karaage
karaage/datastores/slurm.py
SlurmDataStore._read_output
def _read_output(self, command): """ Read CSV delimited input from Slurm. """ cmd = [] cmd.extend(self._prefix) cmd.extend([self._path, "-iP"]) cmd.extend(command) command = cmd logger.debug("Cmd %s" % command) null = open('/dev/null', 'w') process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=null) null.close() results = [] reader = csv.reader(_input_csv(process.stdout), delimiter=str("|")) try: headers = next(reader) logger.debug("<-- headers %s" % headers) except StopIteration: logger.debug("Cmd %s headers not found" % command) headers = [] for row in reader: _output_csv(row) logger.debug("<-- row %s" % row) this_row = {} i = 0 for i in range(0, len(headers)): key = headers[i] value = row[i] this_row[key] = value results.append(this_row) process.stdout.close() retcode = process.wait() if retcode != 0: logger.error("<-- Cmd %s returned %d (error)" % (command, retcode)) raise subprocess.CalledProcessError(retcode, command) if len(headers) == 0: logger.error("Cmd %s didn't return any headers." % command) raise RuntimeError("Cmd %s didn't return any headers." % command) logger.debug("<-- Returned: %d (good)" % retcode) return results
python
def _read_output(self, command): cmd = [] cmd.extend(self._prefix) cmd.extend([self._path, "-iP"]) cmd.extend(command) command = cmd logger.debug("Cmd %s" % command) null = open('/dev/null', 'w') process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=null) null.close() results = [] reader = csv.reader(_input_csv(process.stdout), delimiter=str("|")) try: headers = next(reader) logger.debug("<-- headers %s" % headers) except StopIteration: logger.debug("Cmd %s headers not found" % command) headers = [] for row in reader: _output_csv(row) logger.debug("<-- row %s" % row) this_row = {} i = 0 for i in range(0, len(headers)): key = headers[i] value = row[i] this_row[key] = value results.append(this_row) process.stdout.close() retcode = process.wait() if retcode != 0: logger.error("<-- Cmd %s returned %d (error)" % (command, retcode)) raise subprocess.CalledProcessError(retcode, command) if len(headers) == 0: logger.error("Cmd %s didn't return any headers." % command) raise RuntimeError("Cmd %s didn't return any headers." % command) logger.debug("<-- Returned: %d (good)" % retcode) return results
[ "def", "_read_output", "(", "self", ",", "command", ")", ":", "cmd", "=", "[", "]", "cmd", ".", "extend", "(", "self", ".", "_prefix", ")", "cmd", ".", "extend", "(", "[", "self", ".", "_path", ",", "\"-iP\"", "]", ")", "cmd", ".", "extend", "(",...
Read CSV delimited input from Slurm.
[ "Read", "CSV", "delimited", "input", "from", "Slurm", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/slurm.py#L118-L167
16,836
Karaage-Cluster/karaage
karaage/datastores/slurm.py
SlurmDataStore.get_project
def get_project(self, projectname): """ Get the project details from Slurm. """ cmd = ["list", "accounts", "where", "name=%s" % projectname] results = self._read_output(cmd) if len(results) == 0: return None elif len(results) > 1: logger.error( "Command returned multiple results for '%s'." % projectname) raise RuntimeError( "Command returned multiple results for '%s'." % projectname) the_result = results[0] the_project = the_result["Account"] if projectname.lower() != the_project.lower(): logger.error( "We expected projectname '%s' " "but got projectname '%s'." % (projectname, the_project)) raise RuntimeError( "We expected projectname '%s' " "but got projectname '%s'." % (projectname, the_project)) return the_result
python
def get_project(self, projectname): cmd = ["list", "accounts", "where", "name=%s" % projectname] results = self._read_output(cmd) if len(results) == 0: return None elif len(results) > 1: logger.error( "Command returned multiple results for '%s'." % projectname) raise RuntimeError( "Command returned multiple results for '%s'." % projectname) the_result = results[0] the_project = the_result["Account"] if projectname.lower() != the_project.lower(): logger.error( "We expected projectname '%s' " "but got projectname '%s'." % (projectname, the_project)) raise RuntimeError( "We expected projectname '%s' " "but got projectname '%s'." % (projectname, the_project)) return the_result
[ "def", "get_project", "(", "self", ",", "projectname", ")", ":", "cmd", "=", "[", "\"list\"", ",", "\"accounts\"", ",", "\"where\"", ",", "\"name=%s\"", "%", "projectname", "]", "results", "=", "self", ".", "_read_output", "(", "cmd", ")", "if", "len", "...
Get the project details from Slurm.
[ "Get", "the", "project", "details", "from", "Slurm", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/slurm.py#L194-L217
16,837
Karaage-Cluster/karaage
karaage/datastores/slurm.py
SlurmDataStore.get_users_in_project
def get_users_in_project(self, projectname): """ Get list of users in project from Slurm. """ cmd = ["list", "assoc", "where", "account=%s" % projectname] results = self._read_output(cmd) user_list = [] for result in results: if result["User"] != "": user_list.append(result["User"]) return user_list
python
def get_users_in_project(self, projectname): cmd = ["list", "assoc", "where", "account=%s" % projectname] results = self._read_output(cmd) user_list = [] for result in results: if result["User"] != "": user_list.append(result["User"]) return user_list
[ "def", "get_users_in_project", "(", "self", ",", "projectname", ")", ":", "cmd", "=", "[", "\"list\"", ",", "\"assoc\"", ",", "\"where\"", ",", "\"account=%s\"", "%", "projectname", "]", "results", "=", "self", ".", "_read_output", "(", "cmd", ")", "user_lis...
Get list of users in project from Slurm.
[ "Get", "list", "of", "users", "in", "project", "from", "Slurm", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/slurm.py#L219-L228
16,838
Karaage-Cluster/karaage
karaage/datastores/slurm.py
SlurmDataStore.get_projects_in_user
def get_projects_in_user(self, username): """ Get list of projects in user from Slurm. """ cmd = ["list", "assoc", "where", "user=%s" % username] results = self._read_output(cmd) project_list = [] for result in results: project_list.append(result["Account"]) return project_list
python
def get_projects_in_user(self, username): cmd = ["list", "assoc", "where", "user=%s" % username] results = self._read_output(cmd) project_list = [] for result in results: project_list.append(result["Account"]) return project_list
[ "def", "get_projects_in_user", "(", "self", ",", "username", ")", ":", "cmd", "=", "[", "\"list\"", ",", "\"assoc\"", ",", "\"where\"", ",", "\"user=%s\"", "%", "username", "]", "results", "=", "self", ".", "_read_output", "(", "cmd", ")", "project_list", ...
Get list of projects in user from Slurm.
[ "Get", "list", "of", "projects", "in", "user", "from", "Slurm", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/datastores/slurm.py#L230-L238
16,839
daler/trackhub
trackhub/trackdb.py
TrackDb.add_tracks
def add_tracks(self, track): """ Add a track or iterable of tracks. Parameters ---------- track : iterable or Track Iterable of :class:`Track` objects, or a single :class:`Track` object. """ from trackhub import BaseTrack if isinstance(track, BaseTrack): self.add_child(track) self._tracks.append(track) else: for t in track: self.add_child(t) self._tracks.append(t)
python
def add_tracks(self, track): from trackhub import BaseTrack if isinstance(track, BaseTrack): self.add_child(track) self._tracks.append(track) else: for t in track: self.add_child(t) self._tracks.append(t)
[ "def", "add_tracks", "(", "self", ",", "track", ")", ":", "from", "trackhub", "import", "BaseTrack", "if", "isinstance", "(", "track", ",", "BaseTrack", ")", ":", "self", ".", "add_child", "(", "track", ")", "self", ".", "_tracks", ".", "append", "(", ...
Add a track or iterable of tracks. Parameters ---------- track : iterable or Track Iterable of :class:`Track` objects, or a single :class:`Track` object.
[ "Add", "a", "track", "or", "iterable", "of", "tracks", "." ]
e4655f79177822529f80b923df117e38e28df702
https://github.com/daler/trackhub/blob/e4655f79177822529f80b923df117e38e28df702/trackhub/trackdb.py#L75-L93
16,840
Karaage-Cluster/karaage
karaage/common/trace.py
loggable
def loggable(obj): """Return "True" if the obj implements the minimum Logger API required by the 'trace' decorator. """ if isinstance(obj, logging.Logger): return True else: return (inspect.isclass(obj) and inspect.ismethod(getattr(obj, 'debug', None)) and inspect.ismethod(getattr(obj, 'isEnabledFor', None)) and inspect.ismethod(getattr(obj, 'setLevel', None)))
python
def loggable(obj): if isinstance(obj, logging.Logger): return True else: return (inspect.isclass(obj) and inspect.ismethod(getattr(obj, 'debug', None)) and inspect.ismethod(getattr(obj, 'isEnabledFor', None)) and inspect.ismethod(getattr(obj, 'setLevel', None)))
[ "def", "loggable", "(", "obj", ")", ":", "if", "isinstance", "(", "obj", ",", "logging", ".", "Logger", ")", ":", "return", "True", "else", ":", "return", "(", "inspect", ".", "isclass", "(", "obj", ")", "and", "inspect", ".", "ismethod", "(", "getat...
Return "True" if the obj implements the minimum Logger API required by the 'trace' decorator.
[ "Return", "True", "if", "the", "obj", "implements", "the", "minimum", "Logger", "API", "required", "by", "the", "trace", "decorator", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/common/trace.py#L131-L141
16,841
Karaage-Cluster/karaage
karaage/common/trace.py
_formatter_self
def _formatter_self(name, value): """Format the "self" variable and value on instance methods. """ __mname = value.__module__ if __mname != '__main__': return '%s = <%s.%s object at 0x%x>' \ % (name, __mname, value.__class__.__name__, id(value)) else: return '%s = <%s object at 0x%x>' \ % (name, value.__class__.__name__, id(value))
python
def _formatter_self(name, value): __mname = value.__module__ if __mname != '__main__': return '%s = <%s.%s object at 0x%x>' \ % (name, __mname, value.__class__.__name__, id(value)) else: return '%s = <%s object at 0x%x>' \ % (name, value.__class__.__name__, id(value))
[ "def", "_formatter_self", "(", "name", ",", "value", ")", ":", "__mname", "=", "value", ".", "__module__", "if", "__mname", "!=", "'__main__'", ":", "return", "'%s = <%s.%s object at 0x%x>'", "%", "(", "name", ",", "__mname", ",", "value", ".", "__class__", ...
Format the "self" variable and value on instance methods.
[ "Format", "the", "self", "variable", "and", "value", "on", "instance", "methods", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/common/trace.py#L245-L254
16,842
Karaage-Cluster/karaage
karaage/common/trace.py
_formatter_class
def _formatter_class(name, value): """Format the "klass" variable and value on class methods. """ __mname = value.__module__ if __mname != '__main__': return "%s = <type '%s.%s'>" % (name, __mname, value.__name__) else: return "%s = <type '%s'>" % (name, value.__name__)
python
def _formatter_class(name, value): __mname = value.__module__ if __mname != '__main__': return "%s = <type '%s.%s'>" % (name, __mname, value.__name__) else: return "%s = <type '%s'>" % (name, value.__name__)
[ "def", "_formatter_class", "(", "name", ",", "value", ")", ":", "__mname", "=", "value", ".", "__module__", "if", "__mname", "!=", "'__main__'", ":", "return", "\"%s = <type '%s.%s'>\"", "%", "(", "name", ",", "__mname", ",", "value", ".", "__name__", ")", ...
Format the "klass" variable and value on class methods.
[ "Format", "the", "klass", "variable", "and", "value", "on", "class", "methods", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/common/trace.py#L257-L264
16,843
Karaage-Cluster/karaage
karaage/common/trace.py
get_formatter
def get_formatter(name): """Return the named formatter function. See the function "set_formatter" for details. """ if name in ('self', 'instance', 'this'): return af_self elif name == 'class': return af_class elif name in ('named', 'param', 'parameter'): return af_named elif name in ('default', 'optional'): return af_default # elif name in ('anonymous', 'arbitrary', 'unnamed'): # return af_anonymous elif name in ('keyword', 'pair', 'pairs'): return af_keyword else: raise ValueError('unknown trace formatter %r' % name)
python
def get_formatter(name): if name in ('self', 'instance', 'this'): return af_self elif name == 'class': return af_class elif name in ('named', 'param', 'parameter'): return af_named elif name in ('default', 'optional'): return af_default # elif name in ('anonymous', 'arbitrary', 'unnamed'): # return af_anonymous elif name in ('keyword', 'pair', 'pairs'): return af_keyword else: raise ValueError('unknown trace formatter %r' % name)
[ "def", "get_formatter", "(", "name", ")", ":", "if", "name", "in", "(", "'self'", ",", "'instance'", ",", "'this'", ")", ":", "return", "af_self", "elif", "name", "==", "'class'", ":", "return", "af_class", "elif", "name", "in", "(", "'named'", ",", "'...
Return the named formatter function. See the function "set_formatter" for details.
[ "Return", "the", "named", "formatter", "function", ".", "See", "the", "function", "set_formatter", "for", "details", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/common/trace.py#L285-L302
16,844
Karaage-Cluster/karaage
karaage/common/trace.py
__lookup_builtin
def __lookup_builtin(name): """Lookup the parameter name and default parameter values for builtin functions. """ global __builtin_functions if __builtin_functions is None: builtins = dict() for proto in __builtins: pos = proto.find('(') name, params, defaults = proto[:pos], list(), dict() for param in proto[pos + 1:-1].split(','): pos = param.find('=') if not pos < 0: param, value = param[:pos], param[pos + 1:] try: defaults[param] = __builtin_defaults[value] except KeyError: raise ValueError( 'builtin function %s: parameter %s: ' 'unknown default %r' % (name, param, value)) params.append(param) builtins[name] = (params, defaults) __builtin_functions = builtins try: params, defaults = __builtin_functions[name] except KeyError: params, defaults = tuple(), dict() __builtin_functions[name] = (params, defaults) print( "Warning: builtin function %r is missing prototype" % name, file=sys.stderr) return len(params), params, defaults
python
def __lookup_builtin(name): global __builtin_functions if __builtin_functions is None: builtins = dict() for proto in __builtins: pos = proto.find('(') name, params, defaults = proto[:pos], list(), dict() for param in proto[pos + 1:-1].split(','): pos = param.find('=') if not pos < 0: param, value = param[:pos], param[pos + 1:] try: defaults[param] = __builtin_defaults[value] except KeyError: raise ValueError( 'builtin function %s: parameter %s: ' 'unknown default %r' % (name, param, value)) params.append(param) builtins[name] = (params, defaults) __builtin_functions = builtins try: params, defaults = __builtin_functions[name] except KeyError: params, defaults = tuple(), dict() __builtin_functions[name] = (params, defaults) print( "Warning: builtin function %r is missing prototype" % name, file=sys.stderr) return len(params), params, defaults
[ "def", "__lookup_builtin", "(", "name", ")", ":", "global", "__builtin_functions", "if", "__builtin_functions", "is", "None", ":", "builtins", "=", "dict", "(", ")", "for", "proto", "in", "__builtins", ":", "pos", "=", "proto", ".", "find", "(", "'('", ")"...
Lookup the parameter name and default parameter values for builtin functions.
[ "Lookup", "the", "parameter", "name", "and", "default", "parameter", "values", "for", "builtin", "functions", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/common/trace.py#L431-L463
16,845
Karaage-Cluster/karaage
karaage/plugins/kgapplications/views/common.py
application_list
def application_list(request): """ a user wants to see all applications possible. """ if util.is_admin(request): queryset = Application.objects.all() else: queryset = Application.objects.get_for_applicant(request.user) q_filter = ApplicationFilter(request.GET, queryset=queryset) table = ApplicationTable(q_filter.qs.order_by("-expires")) tables.RequestConfig(request).configure(table) spec = [] for name, value in six.iteritems(q_filter.form.cleaned_data): if value is not None and value != "": name = name.replace('_', ' ').capitalize() spec.append((name, value)) return render( template_name="kgapplications/application_list.html", context={ 'table': table, 'filter': q_filter, 'spec': spec, 'title': "Application list", }, request=request)
python
def application_list(request): if util.is_admin(request): queryset = Application.objects.all() else: queryset = Application.objects.get_for_applicant(request.user) q_filter = ApplicationFilter(request.GET, queryset=queryset) table = ApplicationTable(q_filter.qs.order_by("-expires")) tables.RequestConfig(request).configure(table) spec = [] for name, value in six.iteritems(q_filter.form.cleaned_data): if value is not None and value != "": name = name.replace('_', ' ').capitalize() spec.append((name, value)) return render( template_name="kgapplications/application_list.html", context={ 'table': table, 'filter': q_filter, 'spec': spec, 'title': "Application list", }, request=request)
[ "def", "application_list", "(", "request", ")", ":", "if", "util", ".", "is_admin", "(", "request", ")", ":", "queryset", "=", "Application", ".", "objects", ".", "all", "(", ")", "else", ":", "queryset", "=", "Application", ".", "objects", ".", "get_for...
a user wants to see all applications possible.
[ "a", "user", "wants", "to", "see", "all", "applications", "possible", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgapplications/views/common.py#L38-L65
16,846
Karaage-Cluster/karaage
karaage/plugins/kgapplications/views/common.py
profile_application_list
def profile_application_list(request): """ a logged in user wants to see all his pending applications. """ config = tables.RequestConfig(request, paginate={"per_page": 5}) person = request.user my_applications = Application.objects.get_for_applicant(person) my_applications = ApplicationTable(my_applications, prefix="mine-") config.configure(my_applications) requires_attention = Application.objects.requires_attention(request) requires_attention = ApplicationTable(requires_attention, prefix="attn-") config.configure(requires_attention) return render( template_name='kgapplications/profile_applications.html', context={ 'person': request.user, 'my_applications': my_applications, 'requires_attention': requires_attention, }, request=request)
python
def profile_application_list(request): config = tables.RequestConfig(request, paginate={"per_page": 5}) person = request.user my_applications = Application.objects.get_for_applicant(person) my_applications = ApplicationTable(my_applications, prefix="mine-") config.configure(my_applications) requires_attention = Application.objects.requires_attention(request) requires_attention = ApplicationTable(requires_attention, prefix="attn-") config.configure(requires_attention) return render( template_name='kgapplications/profile_applications.html', context={ 'person': request.user, 'my_applications': my_applications, 'requires_attention': requires_attention, }, request=request)
[ "def", "profile_application_list", "(", "request", ")", ":", "config", "=", "tables", ".", "RequestConfig", "(", "request", ",", "paginate", "=", "{", "\"per_page\"", ":", "5", "}", ")", "person", "=", "request", ".", "user", "my_applications", "=", "Applica...
a logged in user wants to see all his pending applications.
[ "a", "logged", "in", "user", "wants", "to", "see", "all", "his", "pending", "applications", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgapplications/views/common.py#L69-L89
16,847
Karaage-Cluster/karaage
karaage/plugins/kgapplications/views/common.py
application_detail
def application_detail(request, application_id, state=None, label=None): """ A authenticated used is trying to access an application. """ application = base.get_application(pk=application_id) state_machine = base.get_state_machine(application) return state_machine.process(request, application, state, label)
python
def application_detail(request, application_id, state=None, label=None): application = base.get_application(pk=application_id) state_machine = base.get_state_machine(application) return state_machine.process(request, application, state, label)
[ "def", "application_detail", "(", "request", ",", "application_id", ",", "state", "=", "None", ",", "label", "=", "None", ")", ":", "application", "=", "base", ".", "get_application", "(", "pk", "=", "application_id", ")", "state_machine", "=", "base", ".", ...
A authenticated used is trying to access an application.
[ "A", "authenticated", "used", "is", "trying", "to", "access", "an", "application", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgapplications/views/common.py#L130-L134
16,848
Karaage-Cluster/karaage
karaage/plugins/kgapplications/views/common.py
application_unauthenticated
def application_unauthenticated(request, token, state=None, label=None): """ An somebody is trying to access an application. """ application = base.get_application(secret_token=token) if application.expires < datetime.datetime.now(): return render( template_name='kgapplications/common_expired.html', context={'application': application}, request=request) roles = {'is_applicant', 'is_authorised'} # redirect user to real url if possible. if request.user.is_authenticated: if request.user == application.applicant: url = base.get_url( request, application, roles, label) return HttpResponseRedirect(url) state_machine = base.get_state_machine(application) return state_machine.process( request, application, state, label, roles)
python
def application_unauthenticated(request, token, state=None, label=None): application = base.get_application(secret_token=token) if application.expires < datetime.datetime.now(): return render( template_name='kgapplications/common_expired.html', context={'application': application}, request=request) roles = {'is_applicant', 'is_authorised'} # redirect user to real url if possible. if request.user.is_authenticated: if request.user == application.applicant: url = base.get_url( request, application, roles, label) return HttpResponseRedirect(url) state_machine = base.get_state_machine(application) return state_machine.process( request, application, state, label, roles)
[ "def", "application_unauthenticated", "(", "request", ",", "token", ",", "state", "=", "None", ",", "label", "=", "None", ")", ":", "application", "=", "base", ".", "get_application", "(", "secret_token", "=", "token", ")", "if", "application", ".", "expires...
An somebody is trying to access an application.
[ "An", "somebody", "is", "trying", "to", "access", "an", "application", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgapplications/views/common.py#L137-L157
16,849
Karaage-Cluster/karaage
karaage/plugins/kgusage/usage.py
get_institute_usage
def get_institute_usage(institute, start, end): """Return a tuple of cpu hours and number of jobs for an institute for a given period Keyword arguments: institute -- start -- start date end -- end date """ try: cache = InstituteCache.objects.get( institute=institute, date=datetime.date.today(), start=start, end=end) return cache.cpu_time, cache.no_jobs except InstituteCache.DoesNotExist: return 0, 0
python
def get_institute_usage(institute, start, end): try: cache = InstituteCache.objects.get( institute=institute, date=datetime.date.today(), start=start, end=end) return cache.cpu_time, cache.no_jobs except InstituteCache.DoesNotExist: return 0, 0
[ "def", "get_institute_usage", "(", "institute", ",", "start", ",", "end", ")", ":", "try", ":", "cache", "=", "InstituteCache", ".", "objects", ".", "get", "(", "institute", "=", "institute", ",", "date", "=", "datetime", ".", "date", ".", "today", "(", ...
Return a tuple of cpu hours and number of jobs for an institute for a given period Keyword arguments: institute -- start -- start date end -- end date
[ "Return", "a", "tuple", "of", "cpu", "hours", "and", "number", "of", "jobs", "for", "an", "institute", "for", "a", "given", "period" ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgusage/usage.py#L35-L50
16,850
Karaage-Cluster/karaage
karaage/plugins/kgusage/usage.py
get_project_usage
def get_project_usage(project, start, end): """Return a tuple of cpu hours and number of jobs for a project for a given period Keyword arguments: project -- start -- start date end -- end date """ try: cache = ProjectCache.objects.get( project=project, date=datetime.date.today(), start=start, end=end) return cache.cpu_time, cache.no_jobs except ProjectCache.DoesNotExist: return 0, 0
python
def get_project_usage(project, start, end): try: cache = ProjectCache.objects.get( project=project, date=datetime.date.today(), start=start, end=end) return cache.cpu_time, cache.no_jobs except ProjectCache.DoesNotExist: return 0, 0
[ "def", "get_project_usage", "(", "project", ",", "start", ",", "end", ")", ":", "try", ":", "cache", "=", "ProjectCache", ".", "objects", ".", "get", "(", "project", "=", "project", ",", "date", "=", "datetime", ".", "date", ".", "today", "(", ")", "...
Return a tuple of cpu hours and number of jobs for a project for a given period Keyword arguments: project -- start -- start date end -- end date
[ "Return", "a", "tuple", "of", "cpu", "hours", "and", "number", "of", "jobs", "for", "a", "project", "for", "a", "given", "period" ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgusage/usage.py#L53-L69
16,851
Karaage-Cluster/karaage
karaage/plugins/kgusage/usage.py
get_person_usage
def get_person_usage(person, project, start, end): """Return a tuple of cpu hours and number of jobs for a person in a specific project Keyword arguments: person -- project -- The project the usage is from start -- start date end -- end date """ try: cache = PersonCache.objects.get( person=person, project=project, date=datetime.date.today(), start=start, end=end) return cache.cpu_time, cache.no_jobs except PersonCache.DoesNotExist: return 0, 0
python
def get_person_usage(person, project, start, end): try: cache = PersonCache.objects.get( person=person, project=project, date=datetime.date.today(), start=start, end=end) return cache.cpu_time, cache.no_jobs except PersonCache.DoesNotExist: return 0, 0
[ "def", "get_person_usage", "(", "person", ",", "project", ",", "start", ",", "end", ")", ":", "try", ":", "cache", "=", "PersonCache", ".", "objects", ".", "get", "(", "person", "=", "person", ",", "project", "=", "project", ",", "date", "=", "datetime...
Return a tuple of cpu hours and number of jobs for a person in a specific project Keyword arguments: person -- project -- The project the usage is from start -- start date end -- end date
[ "Return", "a", "tuple", "of", "cpu", "hours", "and", "number", "of", "jobs", "for", "a", "person", "in", "a", "specific", "project" ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgusage/usage.py#L72-L88
16,852
Karaage-Cluster/karaage
karaage/plugins/kgusage/usage.py
get_machine_usage
def get_machine_usage(machine, start, end): """Return a tuple of cpu hours and number of jobs for a machine for a given period Keyword arguments: machine -- start -- start date end -- end date """ try: cache = MachineCache.objects.get( machine=machine, date=datetime.date.today(), start=start, end=end) return cache.cpu_time, cache.no_jobs except MachineCache.DoesNotExist: return 0, 0
python
def get_machine_usage(machine, start, end): try: cache = MachineCache.objects.get( machine=machine, date=datetime.date.today(), start=start, end=end) return cache.cpu_time, cache.no_jobs except MachineCache.DoesNotExist: return 0, 0
[ "def", "get_machine_usage", "(", "machine", ",", "start", ",", "end", ")", ":", "try", ":", "cache", "=", "MachineCache", ".", "objects", ".", "get", "(", "machine", "=", "machine", ",", "date", "=", "datetime", ".", "date", ".", "today", "(", ")", "...
Return a tuple of cpu hours and number of jobs for a machine for a given period Keyword arguments: machine -- start -- start date end -- end date
[ "Return", "a", "tuple", "of", "cpu", "hours", "and", "number", "of", "jobs", "for", "a", "machine", "for", "a", "given", "period" ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgusage/usage.py#L91-L108
16,853
Karaage-Cluster/karaage
karaage/plugins/kgusage/usage.py
get_machine_category_usage
def get_machine_category_usage(start, end): """Return a tuple of cpu hours and number of jobs for a given period Keyword arguments: start -- start date end -- end date """ cache = MachineCategoryCache.objects.get( date=datetime.date.today(), start=start, end=end) return cache
python
def get_machine_category_usage(start, end): cache = MachineCategoryCache.objects.get( date=datetime.date.today(), start=start, end=end) return cache
[ "def", "get_machine_category_usage", "(", "start", ",", "end", ")", ":", "cache", "=", "MachineCategoryCache", ".", "objects", ".", "get", "(", "date", "=", "datetime", ".", "date", ".", "today", "(", ")", ",", "start", "=", "start", ",", "end", "=", "...
Return a tuple of cpu hours and number of jobs for a given period Keyword arguments: start -- start date end -- end date
[ "Return", "a", "tuple", "of", "cpu", "hours", "and", "number", "of", "jobs", "for", "a", "given", "period" ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgusage/usage.py#L111-L124
16,854
Karaage-Cluster/karaage
karaage/plugins/kgapplications/views/project.py
get_applicant_from_email
def get_applicant_from_email(email): """ Get applicant from email address. If the person exists, return (person, True) If multiple matches, return (None, True) Otherwise create applicant and return (applicant, False) """ try: applicant = Person.active.get(email=email) existing_person = True except Person.DoesNotExist: applicant = Applicant.objects.create(email=email) existing_person = False except Person.MultipleObjectsReturned: applicant = None existing_person = False return applicant, existing_person
python
def get_applicant_from_email(email): try: applicant = Person.active.get(email=email) existing_person = True except Person.DoesNotExist: applicant = Applicant.objects.create(email=email) existing_person = False except Person.MultipleObjectsReturned: applicant = None existing_person = False return applicant, existing_person
[ "def", "get_applicant_from_email", "(", "email", ")", ":", "try", ":", "applicant", "=", "Person", ".", "active", ".", "get", "(", "email", "=", "email", ")", "existing_person", "=", "True", "except", "Person", ".", "DoesNotExist", ":", "applicant", "=", "...
Get applicant from email address. If the person exists, return (person, True) If multiple matches, return (None, True) Otherwise create applicant and return (applicant, False)
[ "Get", "applicant", "from", "email", "address", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgapplications/views/project.py#L48-L67
16,855
Karaage-Cluster/karaage
karaage/plugins/kgapplications/views/project.py
_send_invitation
def _send_invitation(request, project): """ The logged in project leader OR administrator wants to invite somebody. """ form = forms.InviteUserApplicationForm(request.POST or None) if request.method == 'POST': if form.is_valid(): email = form.cleaned_data['email'] applicant, existing_person = get_applicant_from_email(email) # If applicant is None then there were multiple persons found. if applicant is None: return render( template_name='kgapplications/' 'project_common_invite_multiple.html', context={'form': form, 'email': email}, request=request) if existing_person and 'existing' not in request.POST: return render( template_name='kgapplications/' 'project_common_invite_existing.html', context={'form': form, 'person': applicant}, request=request) application = form.save(commit=False) application.applicant = applicant application.project = project application.save() state_machine = get_application_state_machine() response = state_machine.start(request, application) return response return render( template_name='kgapplications/project_common_invite_other.html', context={'form': form, 'project': project, }, request=request)
python
def _send_invitation(request, project): form = forms.InviteUserApplicationForm(request.POST or None) if request.method == 'POST': if form.is_valid(): email = form.cleaned_data['email'] applicant, existing_person = get_applicant_from_email(email) # If applicant is None then there were multiple persons found. if applicant is None: return render( template_name='kgapplications/' 'project_common_invite_multiple.html', context={'form': form, 'email': email}, request=request) if existing_person and 'existing' not in request.POST: return render( template_name='kgapplications/' 'project_common_invite_existing.html', context={'form': form, 'person': applicant}, request=request) application = form.save(commit=False) application.applicant = applicant application.project = project application.save() state_machine = get_application_state_machine() response = state_machine.start(request, application) return response return render( template_name='kgapplications/project_common_invite_other.html', context={'form': form, 'project': project, }, request=request)
[ "def", "_send_invitation", "(", "request", ",", "project", ")", ":", "form", "=", "forms", ".", "InviteUserApplicationForm", "(", "request", ".", "POST", "or", "None", ")", "if", "request", ".", "method", "==", "'POST'", ":", "if", "form", ".", "is_valid",...
The logged in project leader OR administrator wants to invite somebody.
[ "The", "logged", "in", "project", "leader", "OR", "administrator", "wants", "to", "invite", "somebody", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgapplications/views/project.py#L70-L106
16,856
Karaage-Cluster/karaage
karaage/plugins/kgapplications/views/project.py
send_invitation
def send_invitation(request, project_id=None): """ The logged in project leader wants to invite somebody to their project. """ project = None if project_id is not None: project = get_object_or_404(Project, id=project_id) if project is None: if not is_admin(request): return HttpResponseForbidden('<h1>Access Denied</h1>') else: if not project.can_edit(request): return HttpResponseForbidden('<h1>Access Denied</h1>') return _send_invitation(request, project)
python
def send_invitation(request, project_id=None): project = None if project_id is not None: project = get_object_or_404(Project, id=project_id) if project is None: if not is_admin(request): return HttpResponseForbidden('<h1>Access Denied</h1>') else: if not project.can_edit(request): return HttpResponseForbidden('<h1>Access Denied</h1>') return _send_invitation(request, project)
[ "def", "send_invitation", "(", "request", ",", "project_id", "=", "None", ")", ":", "project", "=", "None", "if", "project_id", "is", "not", "None", ":", "project", "=", "get_object_or_404", "(", "Project", ",", "id", "=", "project_id", ")", "if", "project...
The logged in project leader wants to invite somebody to their project.
[ "The", "logged", "in", "project", "leader", "wants", "to", "invite", "somebody", "to", "their", "project", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgapplications/views/project.py#L110-L128
16,857
Karaage-Cluster/karaage
karaage/plugins/kgapplications/views/project.py
new_application
def new_application(request): """ A new application by a user to start a new project. """ # Note default kgapplications/index.html will display error if user logged # in. if not settings.ALLOW_REGISTRATIONS: return render( template_name='kgapplications/project_common_disabled.html', context={}, request=request) roles = {'is_applicant', 'is_authorised'} if not request.user.is_authenticated: attrs, _ = saml.parse_attributes(request) defaults = {'email': attrs['email']} form = forms.UnauthenticatedInviteUserApplicationForm( request.POST or None, initial=defaults) if request.method == 'POST': if form.is_valid(): email = form.cleaned_data['email'] applicant, existing_person = get_applicant_from_email(email) # If applicant is None then there were multiple persons found. # This should never happen as the # UnauthenticatedInviteUserApplicationForm form disallows # existing users applying unauthenticated. assert applicant is not None # Similarly existing_person should always be False here. assert not existing_person application = ProjectApplication() application.applicant = applicant application.save() state_machine = get_application_state_machine() state_machine.start(request, application, roles) # we do not show unauthenticated users the application at this # stage. url = reverse('index') return HttpResponseRedirect(url) return render( template_name='kgapplications/' 'project_common_invite_unauthenticated.html', context={'form': form, }, request=request) else: if request.method == 'POST': person = request.user application = ProjectApplication() application.applicant = person application.save() state_machine = get_application_state_machine() response = state_machine.start(request, application, roles) return response return render( template_name='kgapplications/' 'project_common_invite_authenticated.html', context={}, request=request)
python
def new_application(request): # Note default kgapplications/index.html will display error if user logged # in. if not settings.ALLOW_REGISTRATIONS: return render( template_name='kgapplications/project_common_disabled.html', context={}, request=request) roles = {'is_applicant', 'is_authorised'} if not request.user.is_authenticated: attrs, _ = saml.parse_attributes(request) defaults = {'email': attrs['email']} form = forms.UnauthenticatedInviteUserApplicationForm( request.POST or None, initial=defaults) if request.method == 'POST': if form.is_valid(): email = form.cleaned_data['email'] applicant, existing_person = get_applicant_from_email(email) # If applicant is None then there were multiple persons found. # This should never happen as the # UnauthenticatedInviteUserApplicationForm form disallows # existing users applying unauthenticated. assert applicant is not None # Similarly existing_person should always be False here. assert not existing_person application = ProjectApplication() application.applicant = applicant application.save() state_machine = get_application_state_machine() state_machine.start(request, application, roles) # we do not show unauthenticated users the application at this # stage. url = reverse('index') return HttpResponseRedirect(url) return render( template_name='kgapplications/' 'project_common_invite_unauthenticated.html', context={'form': form, }, request=request) else: if request.method == 'POST': person = request.user application = ProjectApplication() application.applicant = person application.save() state_machine = get_application_state_machine() response = state_machine.start(request, application, roles) return response return render( template_name='kgapplications/' 'project_common_invite_authenticated.html', context={}, request=request)
[ "def", "new_application", "(", "request", ")", ":", "# Note default kgapplications/index.html will display error if user logged", "# in.", "if", "not", "settings", ".", "ALLOW_REGISTRATIONS", ":", "return", "render", "(", "template_name", "=", "'kgapplications/project_common_di...
A new application by a user to start a new project.
[ "A", "new", "application", "by", "a", "user", "to", "start", "a", "new", "project", "." ]
2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9
https://github.com/Karaage-Cluster/karaage/blob/2f4c8b4e2d728b3fcbb151160c49000f1c04f5c9/karaage/plugins/kgapplications/views/project.py#L131-L191
16,858
asottile/setuptools-golang
setuptools_golang.py
_get_ldflags
def _get_ldflags(): """Determine the correct link flags. This attempts dummy compiles similar to how autotools does feature detection. """ # windows gcc does not support linking with unresolved symbols if sys.platform == 'win32': # pragma: no cover (windows) prefix = getattr(sys, 'real_prefix', sys.prefix) libs = os.path.join(prefix, str('libs')) return str('-L{} -lpython{}{}').format(libs, *sys.version_info[:2]) cc = subprocess.check_output(('go', 'env', 'CC')).decode('UTF-8').strip() with _tmpdir() as tmpdir: testf = os.path.join(tmpdir, 'test.c') with io.open(testf, 'w') as f: f.write('int f(int); int main(void) { return f(0); }\n') for lflag in LFLAGS: # pragma: no cover (platform specific) try: subprocess.check_call((cc, testf, lflag), cwd=tmpdir) return lflag except subprocess.CalledProcessError: pass else: # pragma: no cover (platform specific) # wellp, none of them worked, fall back to gcc and they'll get a # hopefully reasonable error message return LFLAG_GCC
python
def _get_ldflags(): # windows gcc does not support linking with unresolved symbols if sys.platform == 'win32': # pragma: no cover (windows) prefix = getattr(sys, 'real_prefix', sys.prefix) libs = os.path.join(prefix, str('libs')) return str('-L{} -lpython{}{}').format(libs, *sys.version_info[:2]) cc = subprocess.check_output(('go', 'env', 'CC')).decode('UTF-8').strip() with _tmpdir() as tmpdir: testf = os.path.join(tmpdir, 'test.c') with io.open(testf, 'w') as f: f.write('int f(int); int main(void) { return f(0); }\n') for lflag in LFLAGS: # pragma: no cover (platform specific) try: subprocess.check_call((cc, testf, lflag), cwd=tmpdir) return lflag except subprocess.CalledProcessError: pass else: # pragma: no cover (platform specific) # wellp, none of them worked, fall back to gcc and they'll get a # hopefully reasonable error message return LFLAG_GCC
[ "def", "_get_ldflags", "(", ")", ":", "# windows gcc does not support linking with unresolved symbols", "if", "sys", ".", "platform", "==", "'win32'", ":", "# pragma: no cover (windows)", "prefix", "=", "getattr", "(", "sys", ",", "'real_prefix'", ",", "sys", ".", "pr...
Determine the correct link flags. This attempts dummy compiles similar to how autotools does feature detection.
[ "Determine", "the", "correct", "link", "flags", ".", "This", "attempts", "dummy", "compiles", "similar", "to", "how", "autotools", "does", "feature", "detection", "." ]
a4951e05efc0cf6b9df42a6ff63ac45e0b7b664d
https://github.com/asottile/setuptools-golang/blob/a4951e05efc0cf6b9df42a6ff63ac45e0b7b664d/setuptools_golang.py#L42-L68
16,859
dwavesystems/penaltymodel
penaltymodel_maxgap/penaltymodel/maxgap/interface.py
get_penalty_model
def get_penalty_model(specification): """Factory function for penaltymodel_maxgap. Args: specification (penaltymodel.Specification): The specification for the desired penalty model. Returns: :class:`penaltymodel.PenaltyModel`: Penalty model with the given specification. Raises: :class:`penaltymodel.ImpossiblePenaltyModel`: If the penalty cannot be built. Parameters: priority (int): -100 """ # check that the feasible_configurations are spin feasible_configurations = specification.feasible_configurations if specification.vartype is dimod.BINARY: feasible_configurations = {tuple(2 * v - 1 for v in config): en for config, en in feasible_configurations.items()} # convert ising_quadratic_ranges to the form we expect ising_quadratic_ranges = specification.ising_quadratic_ranges quadratic_ranges = {(u, v): ising_quadratic_ranges[u][v] for u, v in specification.graph.edges} bqm, gap = generate(specification.graph, feasible_configurations, specification.decision_variables, specification.ising_linear_ranges, quadratic_ranges, specification.min_classical_gap, None) # unspecified smt solver try: ground = max(feasible_configurations.values()) except ValueError: ground = 0.0 # if empty return pm.PenaltyModel.from_specification(specification, bqm, gap, ground)
python
def get_penalty_model(specification): # check that the feasible_configurations are spin feasible_configurations = specification.feasible_configurations if specification.vartype is dimod.BINARY: feasible_configurations = {tuple(2 * v - 1 for v in config): en for config, en in feasible_configurations.items()} # convert ising_quadratic_ranges to the form we expect ising_quadratic_ranges = specification.ising_quadratic_ranges quadratic_ranges = {(u, v): ising_quadratic_ranges[u][v] for u, v in specification.graph.edges} bqm, gap = generate(specification.graph, feasible_configurations, specification.decision_variables, specification.ising_linear_ranges, quadratic_ranges, specification.min_classical_gap, None) # unspecified smt solver try: ground = max(feasible_configurations.values()) except ValueError: ground = 0.0 # if empty return pm.PenaltyModel.from_specification(specification, bqm, gap, ground)
[ "def", "get_penalty_model", "(", "specification", ")", ":", "# check that the feasible_configurations are spin", "feasible_configurations", "=", "specification", ".", "feasible_configurations", "if", "specification", ".", "vartype", "is", "dimod", ".", "BINARY", ":", "feasi...
Factory function for penaltymodel_maxgap. Args: specification (penaltymodel.Specification): The specification for the desired penalty model. Returns: :class:`penaltymodel.PenaltyModel`: Penalty model with the given specification. Raises: :class:`penaltymodel.ImpossiblePenaltyModel`: If the penalty cannot be built. Parameters: priority (int): -100
[ "Factory", "function", "for", "penaltymodel_maxgap", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_maxgap/penaltymodel/maxgap/interface.py#L25-L66
16,860
dwavesystems/penaltymodel
penaltymodel_cache/penaltymodel/cache/database_manager.py
insert_feasible_configurations
def insert_feasible_configurations(cur, feasible_configurations, encoded_data=None): """Insert a group of feasible configurations into the cache. Args: cur (:class:`sqlite3.Cursor`): An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement. feasible_configurations (dict[tuple[int]): The set of feasible configurations. Each key should be a tuple of variable assignments. The values are the relative energies. encoded_data (dict, optional): If a dictionary is provided, it will be populated with the serialized data. This is useful for preventing encoding the same information many times. Examples: >>> feasible_configurations = {(-1, -1): 0.0, (+1, +1): 0.0} >>> with pmc.cache_connect(':memory:') as cur: ... pmc.insert_feasible_configurations(cur, feasible_configurations) """ if encoded_data is None: encoded_data = {} if 'num_variables' not in encoded_data: encoded_data['num_variables'] = len(next(iter(feasible_configurations))) if 'num_feasible_configurations' not in encoded_data: encoded_data['num_feasible_configurations'] = len(feasible_configurations) if 'feasible_configurations' not in encoded_data or 'energies' not in encoded_data: encoded = {_serialize_config(config): en for config, en in feasible_configurations.items()} configs, energies = zip(*sorted(encoded.items())) encoded_data['feasible_configurations'] = json.dumps(configs, separators=(',', ':')) encoded_data['energies'] = json.dumps(energies, separators=(',', ':')) insert = """ INSERT OR IGNORE INTO feasible_configurations( num_variables, num_feasible_configurations, feasible_configurations, energies) VALUES ( :num_variables, :num_feasible_configurations, :feasible_configurations, :energies); """ cur.execute(insert, encoded_data)
python
def insert_feasible_configurations(cur, feasible_configurations, encoded_data=None): if encoded_data is None: encoded_data = {} if 'num_variables' not in encoded_data: encoded_data['num_variables'] = len(next(iter(feasible_configurations))) if 'num_feasible_configurations' not in encoded_data: encoded_data['num_feasible_configurations'] = len(feasible_configurations) if 'feasible_configurations' not in encoded_data or 'energies' not in encoded_data: encoded = {_serialize_config(config): en for config, en in feasible_configurations.items()} configs, energies = zip(*sorted(encoded.items())) encoded_data['feasible_configurations'] = json.dumps(configs, separators=(',', ':')) encoded_data['energies'] = json.dumps(energies, separators=(',', ':')) insert = """ INSERT OR IGNORE INTO feasible_configurations( num_variables, num_feasible_configurations, feasible_configurations, energies) VALUES ( :num_variables, :num_feasible_configurations, :feasible_configurations, :energies); """ cur.execute(insert, encoded_data)
[ "def", "insert_feasible_configurations", "(", "cur", ",", "feasible_configurations", ",", "encoded_data", "=", "None", ")", ":", "if", "encoded_data", "is", "None", ":", "encoded_data", "=", "{", "}", "if", "'num_variables'", "not", "in", "encoded_data", ":", "e...
Insert a group of feasible configurations into the cache. Args: cur (:class:`sqlite3.Cursor`): An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement. feasible_configurations (dict[tuple[int]): The set of feasible configurations. Each key should be a tuple of variable assignments. The values are the relative energies. encoded_data (dict, optional): If a dictionary is provided, it will be populated with the serialized data. This is useful for preventing encoding the same information many times. Examples: >>> feasible_configurations = {(-1, -1): 0.0, (+1, +1): 0.0} >>> with pmc.cache_connect(':memory:') as cur: ... pmc.insert_feasible_configurations(cur, feasible_configurations)
[ "Insert", "a", "group", "of", "feasible", "configurations", "into", "the", "cache", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_cache/penaltymodel/cache/database_manager.py#L147-L193
16,861
dwavesystems/penaltymodel
penaltymodel_cache/penaltymodel/cache/database_manager.py
_decode_config
def _decode_config(c, num_variables): """inverse of _serialize_config, always converts to spin.""" def bits(c): n = 1 << (num_variables - 1) for __ in range(num_variables): yield 1 if c & n else -1 n >>= 1 return tuple(bits(c))
python
def _decode_config(c, num_variables): def bits(c): n = 1 << (num_variables - 1) for __ in range(num_variables): yield 1 if c & n else -1 n >>= 1 return tuple(bits(c))
[ "def", "_decode_config", "(", "c", ",", "num_variables", ")", ":", "def", "bits", "(", "c", ")", ":", "n", "=", "1", "<<", "(", "num_variables", "-", "1", ")", "for", "__", "in", "range", "(", "num_variables", ")", ":", "yield", "1", "if", "c", "...
inverse of _serialize_config, always converts to spin.
[ "inverse", "of", "_serialize_config", "always", "converts", "to", "spin", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_cache/penaltymodel/cache/database_manager.py#L239-L246
16,862
dwavesystems/penaltymodel
penaltymodel_cache/penaltymodel/cache/database_manager.py
insert_ising_model
def insert_ising_model(cur, nodelist, edgelist, linear, quadratic, offset, encoded_data=None): """Insert an Ising model into the cache. Args: cur (:class:`sqlite3.Cursor`): An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement. nodelist (list): The nodes in the graph. edgelist (list): The edges in the graph. linear (dict): The linear bias associated with each node in nodelist. quadratic (dict): The quadratic bias associated with teach edge in edgelist. offset (float): The constant offset applied to the ising problem. encoded_data (dict, optional): If a dictionary is provided, it will be populated with the serialized data. This is useful for preventing encoding the same information many times. """ if encoded_data is None: encoded_data = {} # insert graph and partially populate encoded_data with graph info insert_graph(cur, nodelist, edgelist, encoded_data=encoded_data) # need to encode the biases if 'linear_biases' not in encoded_data: encoded_data['linear_biases'] = _serialize_linear_biases(linear, nodelist) if 'quadratic_biases' not in encoded_data: encoded_data['quadratic_biases'] = _serialize_quadratic_biases(quadratic, edgelist) if 'offset' not in encoded_data: encoded_data['offset'] = offset if 'max_quadratic_bias' not in encoded_data: encoded_data['max_quadratic_bias'] = max(itervalues(quadratic)) if 'min_quadratic_bias' not in encoded_data: encoded_data['min_quadratic_bias'] = min(itervalues(quadratic)) if 'max_linear_bias' not in encoded_data: encoded_data['max_linear_bias'] = max(itervalues(linear)) if 'min_linear_bias' not in encoded_data: encoded_data['min_linear_bias'] = min(itervalues(linear)) insert = \ """ INSERT OR IGNORE INTO ising_model( linear_biases, quadratic_biases, offset, max_quadratic_bias, min_quadratic_bias, max_linear_bias, min_linear_bias, graph_id) SELECT :linear_biases, :quadratic_biases, :offset, :max_quadratic_bias, :min_quadratic_bias, :max_linear_bias, :min_linear_bias, graph.id FROM graph WHERE num_nodes = :num_nodes AND num_edges = :num_edges AND edges = :edges; """ cur.execute(insert, encoded_data)
python
def insert_ising_model(cur, nodelist, edgelist, linear, quadratic, offset, encoded_data=None): if encoded_data is None: encoded_data = {} # insert graph and partially populate encoded_data with graph info insert_graph(cur, nodelist, edgelist, encoded_data=encoded_data) # need to encode the biases if 'linear_biases' not in encoded_data: encoded_data['linear_biases'] = _serialize_linear_biases(linear, nodelist) if 'quadratic_biases' not in encoded_data: encoded_data['quadratic_biases'] = _serialize_quadratic_biases(quadratic, edgelist) if 'offset' not in encoded_data: encoded_data['offset'] = offset if 'max_quadratic_bias' not in encoded_data: encoded_data['max_quadratic_bias'] = max(itervalues(quadratic)) if 'min_quadratic_bias' not in encoded_data: encoded_data['min_quadratic_bias'] = min(itervalues(quadratic)) if 'max_linear_bias' not in encoded_data: encoded_data['max_linear_bias'] = max(itervalues(linear)) if 'min_linear_bias' not in encoded_data: encoded_data['min_linear_bias'] = min(itervalues(linear)) insert = \ """ INSERT OR IGNORE INTO ising_model( linear_biases, quadratic_biases, offset, max_quadratic_bias, min_quadratic_bias, max_linear_bias, min_linear_bias, graph_id) SELECT :linear_biases, :quadratic_biases, :offset, :max_quadratic_bias, :min_quadratic_bias, :max_linear_bias, :min_linear_bias, graph.id FROM graph WHERE num_nodes = :num_nodes AND num_edges = :num_edges AND edges = :edges; """ cur.execute(insert, encoded_data)
[ "def", "insert_ising_model", "(", "cur", ",", "nodelist", ",", "edgelist", ",", "linear", ",", "quadratic", ",", "offset", ",", "encoded_data", "=", "None", ")", ":", "if", "encoded_data", "is", "None", ":", "encoded_data", "=", "{", "}", "# insert graph and...
Insert an Ising model into the cache. Args: cur (:class:`sqlite3.Cursor`): An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement. nodelist (list): The nodes in the graph. edgelist (list): The edges in the graph. linear (dict): The linear bias associated with each node in nodelist. quadratic (dict): The quadratic bias associated with teach edge in edgelist. offset (float): The constant offset applied to the ising problem. encoded_data (dict, optional): If a dictionary is provided, it will be populated with the serialized data. This is useful for preventing encoding the same information many times.
[ "Insert", "an", "Ising", "model", "into", "the", "cache", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_cache/penaltymodel/cache/database_manager.py#L249-L313
16,863
dwavesystems/penaltymodel
penaltymodel_cache/penaltymodel/cache/database_manager.py
_serialize_linear_biases
def _serialize_linear_biases(linear, nodelist): """Serializes the linear biases. Args: linear: a interable object where linear[v] is the bias associated with v. nodelist (list): an ordered iterable containing the nodes. Returns: str: base 64 encoded string of little endian 8 byte floats, one for each of the biases in linear. Ordered according to nodelist. Examples: >>> _serialize_linear_biases({1: -1, 2: 1, 3: 0}, [1, 2, 3]) 'AAAAAAAA8L8AAAAAAADwPwAAAAAAAAAA' >>> _serialize_linear_biases({1: -1, 2: 1, 3: 0}, [3, 2, 1]) 'AAAAAAAAAAAAAAAAAADwPwAAAAAAAPC/' """ linear_bytes = struct.pack('<' + 'd' * len(linear), *[linear[i] for i in nodelist]) return base64.b64encode(linear_bytes).decode('utf-8')
python
def _serialize_linear_biases(linear, nodelist): linear_bytes = struct.pack('<' + 'd' * len(linear), *[linear[i] for i in nodelist]) return base64.b64encode(linear_bytes).decode('utf-8')
[ "def", "_serialize_linear_biases", "(", "linear", ",", "nodelist", ")", ":", "linear_bytes", "=", "struct", ".", "pack", "(", "'<'", "+", "'d'", "*", "len", "(", "linear", ")", ",", "*", "[", "linear", "[", "i", "]", "for", "i", "in", "nodelist", "]"...
Serializes the linear biases. Args: linear: a interable object where linear[v] is the bias associated with v. nodelist (list): an ordered iterable containing the nodes. Returns: str: base 64 encoded string of little endian 8 byte floats, one for each of the biases in linear. Ordered according to nodelist. Examples: >>> _serialize_linear_biases({1: -1, 2: 1, 3: 0}, [1, 2, 3]) 'AAAAAAAA8L8AAAAAAADwPwAAAAAAAAAA' >>> _serialize_linear_biases({1: -1, 2: 1, 3: 0}, [3, 2, 1]) 'AAAAAAAAAAAAAAAAAADwPwAAAAAAAPC/'
[ "Serializes", "the", "linear", "biases", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_cache/penaltymodel/cache/database_manager.py#L316-L337
16,864
dwavesystems/penaltymodel
penaltymodel_cache/penaltymodel/cache/database_manager.py
_serialize_quadratic_biases
def _serialize_quadratic_biases(quadratic, edgelist): """Serializes the quadratic biases. Args: quadratic (dict): a dict of the form {edge1: bias1, ...} where each edge is of the form (node1, node2). edgelist (list): a list of the form [(node1, node2), ...]. Returns: str: base 64 encoded string of little endian 8 byte floats, one for each of the edges in quadratic. Ordered by edgelist. Example: >>> _serialize_quadratic_biases({(0, 1): -1, (1, 2): 1, (0, 2): .4}, ... [(0, 1), (1, 2), (0, 2)]) 'AAAAAAAA8L8AAAAAAADwP5qZmZmZmdk/' """ # assumes quadratic is upper-triangular or reflected in edgelist quadratic_list = [quadratic[(u, v)] if (u, v) in quadratic else quadratic[(v, u)] for u, v in edgelist] quadratic_bytes = struct.pack('<' + 'd' * len(quadratic), *quadratic_list) return base64.b64encode(quadratic_bytes).decode('utf-8')
python
def _serialize_quadratic_biases(quadratic, edgelist): # assumes quadratic is upper-triangular or reflected in edgelist quadratic_list = [quadratic[(u, v)] if (u, v) in quadratic else quadratic[(v, u)] for u, v in edgelist] quadratic_bytes = struct.pack('<' + 'd' * len(quadratic), *quadratic_list) return base64.b64encode(quadratic_bytes).decode('utf-8')
[ "def", "_serialize_quadratic_biases", "(", "quadratic", ",", "edgelist", ")", ":", "# assumes quadratic is upper-triangular or reflected in edgelist", "quadratic_list", "=", "[", "quadratic", "[", "(", "u", ",", "v", ")", "]", "if", "(", "u", ",", "v", ")", "in", ...
Serializes the quadratic biases. Args: quadratic (dict): a dict of the form {edge1: bias1, ...} where each edge is of the form (node1, node2). edgelist (list): a list of the form [(node1, node2), ...]. Returns: str: base 64 encoded string of little endian 8 byte floats, one for each of the edges in quadratic. Ordered by edgelist. Example: >>> _serialize_quadratic_biases({(0, 1): -1, (1, 2): 1, (0, 2): .4}, ... [(0, 1), (1, 2), (0, 2)]) 'AAAAAAAA8L8AAAAAAADwP5qZmZmZmdk/'
[ "Serializes", "the", "quadratic", "biases", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_cache/penaltymodel/cache/database_manager.py#L340-L362
16,865
dwavesystems/penaltymodel
penaltymodel_cache/penaltymodel/cache/database_manager.py
iter_ising_model
def iter_ising_model(cur): """Iterate over all of the Ising models in the cache. Args: cur (:class:`sqlite3.Cursor`): An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement. Yields: tuple: A 5-tuple consisting of: list: The nodelist for a graph in the cache. list: the edgelist for a graph in the cache. dict: The linear biases of an Ising Model in the cache. dict: The quadratic biases of an Ising Model in the cache. float: The constant offset of an Ising Model in the cache. """ select = \ """ SELECT linear_biases, quadratic_biases, num_nodes, edges, offset FROM ising_model, graph WHERE graph.id = ising_model.graph_id; """ for linear_biases, quadratic_biases, num_nodes, edges, offset in cur.execute(select): nodelist = list(range(num_nodes)) edgelist = json.loads(edges) yield (nodelist, edgelist, _decode_linear_biases(linear_biases, nodelist), _decode_quadratic_biases(quadratic_biases, edgelist), offset)
python
def iter_ising_model(cur): select = \ """ SELECT linear_biases, quadratic_biases, num_nodes, edges, offset FROM ising_model, graph WHERE graph.id = ising_model.graph_id; """ for linear_biases, quadratic_biases, num_nodes, edges, offset in cur.execute(select): nodelist = list(range(num_nodes)) edgelist = json.loads(edges) yield (nodelist, edgelist, _decode_linear_biases(linear_biases, nodelist), _decode_quadratic_biases(quadratic_biases, edgelist), offset)
[ "def", "iter_ising_model", "(", "cur", ")", ":", "select", "=", "\"\"\"\n SELECT linear_biases, quadratic_biases, num_nodes, edges, offset\n FROM ising_model, graph\n WHERE graph.id = ising_model.graph_id;\n \"\"\"", "for", "linear_biases", ",", "quadratic_biases"...
Iterate over all of the Ising models in the cache. Args: cur (:class:`sqlite3.Cursor`): An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement. Yields: tuple: A 5-tuple consisting of: list: The nodelist for a graph in the cache. list: the edgelist for a graph in the cache. dict: The linear biases of an Ising Model in the cache. dict: The quadratic biases of an Ising Model in the cache. float: The constant offset of an Ising Model in the cache.
[ "Iterate", "over", "all", "of", "the", "Ising", "models", "in", "the", "cache", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_cache/penaltymodel/cache/database_manager.py#L365-L399
16,866
dwavesystems/penaltymodel
penaltymodel_cache/penaltymodel/cache/database_manager.py
_decode_linear_biases
def _decode_linear_biases(linear_string, nodelist): """Inverse of _serialize_linear_biases. Args: linear_string (str): base 64 encoded string of little endian 8 byte floats, one for each of the nodes in nodelist. nodelist (list): list of the form [node1, node2, ...]. Returns: dict: linear biases in a dict. Examples: >>> _decode_linear_biases('AAAAAAAA8L8AAAAAAADwPwAAAAAAAAAA', [1, 2, 3]) {1: -1.0, 2: 1.0, 3: 0.0} >>> _decode_linear_biases('AAAAAAAA8L8AAAAAAADwPwAAAAAAAAAA', [3, 2, 1]) {1: 0.0, 2: 1.0, 3: -1.0} """ linear_bytes = base64.b64decode(linear_string) return dict(zip(nodelist, struct.unpack('<' + 'd' * (len(linear_bytes) // 8), linear_bytes)))
python
def _decode_linear_biases(linear_string, nodelist): linear_bytes = base64.b64decode(linear_string) return dict(zip(nodelist, struct.unpack('<' + 'd' * (len(linear_bytes) // 8), linear_bytes)))
[ "def", "_decode_linear_biases", "(", "linear_string", ",", "nodelist", ")", ":", "linear_bytes", "=", "base64", ".", "b64decode", "(", "linear_string", ")", "return", "dict", "(", "zip", "(", "nodelist", ",", "struct", ".", "unpack", "(", "'<'", "+", "'d'", ...
Inverse of _serialize_linear_biases. Args: linear_string (str): base 64 encoded string of little endian 8 byte floats, one for each of the nodes in nodelist. nodelist (list): list of the form [node1, node2, ...]. Returns: dict: linear biases in a dict. Examples: >>> _decode_linear_biases('AAAAAAAA8L8AAAAAAADwPwAAAAAAAAAA', [1, 2, 3]) {1: -1.0, 2: 1.0, 3: 0.0} >>> _decode_linear_biases('AAAAAAAA8L8AAAAAAADwPwAAAAAAAAAA', [3, 2, 1]) {1: 0.0, 2: 1.0, 3: -1.0}
[ "Inverse", "of", "_serialize_linear_biases", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_cache/penaltymodel/cache/database_manager.py#L402-L421
16,867
dwavesystems/penaltymodel
penaltymodel_cache/penaltymodel/cache/database_manager.py
_decode_quadratic_biases
def _decode_quadratic_biases(quadratic_string, edgelist): """Inverse of _serialize_quadratic_biases Args: quadratic_string (str) : base 64 encoded string of little endian 8 byte floats, one for each of the edges. edgelist (list): a list of edges of the form [(node1, node2), ...]. Returns: dict: J. A dict of the form {edge1: bias1, ...} where each edge is of the form (node1, node2). Example: >>> _decode_quadratic_biases('AAAAAAAA8L8AAAAAAADwP5qZmZmZmdk/', ... [(0, 1), (1, 2), (0, 2)]) {(0, 1): -1.0, (0, 2): 0.4, (1, 2): 1.0} """ quadratic_bytes = base64.b64decode(quadratic_string) return {tuple(edge): bias for edge, bias in zip(edgelist, struct.unpack('<' + 'd' * (len(quadratic_bytes) // 8), quadratic_bytes))}
python
def _decode_quadratic_biases(quadratic_string, edgelist): quadratic_bytes = base64.b64decode(quadratic_string) return {tuple(edge): bias for edge, bias in zip(edgelist, struct.unpack('<' + 'd' * (len(quadratic_bytes) // 8), quadratic_bytes))}
[ "def", "_decode_quadratic_biases", "(", "quadratic_string", ",", "edgelist", ")", ":", "quadratic_bytes", "=", "base64", ".", "b64decode", "(", "quadratic_string", ")", "return", "{", "tuple", "(", "edge", ")", ":", "bias", "for", "edge", ",", "bias", "in", ...
Inverse of _serialize_quadratic_biases Args: quadratic_string (str) : base 64 encoded string of little endian 8 byte floats, one for each of the edges. edgelist (list): a list of edges of the form [(node1, node2), ...]. Returns: dict: J. A dict of the form {edge1: bias1, ...} where each edge is of the form (node1, node2). Example: >>> _decode_quadratic_biases('AAAAAAAA8L8AAAAAAADwP5qZmZmZmdk/', ... [(0, 1), (1, 2), (0, 2)]) {(0, 1): -1.0, (0, 2): 0.4, (1, 2): 1.0}
[ "Inverse", "of", "_serialize_quadratic_biases" ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_cache/penaltymodel/cache/database_manager.py#L424-L444
16,868
dwavesystems/penaltymodel
penaltymodel_cache/penaltymodel/cache/database_manager.py
insert_penalty_model
def insert_penalty_model(cur, penalty_model): """Insert a penalty model into the database. Args: cur (:class:`sqlite3.Cursor`): An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement. penalty_model (:class:`penaltymodel.PenaltyModel`): A penalty model to be stored in the database. Examples: >>> import networkx as nx >>> import penaltymodel.core as pm >>> import dimod >>> graph = nx.path_graph(3) >>> decision_variables = (0, 2) >>> feasible_configurations = {(-1, -1): 0., (+1, +1): 0.} >>> spec = pm.Specification(graph, decision_variables, feasible_configurations, dimod.SPIN) >>> linear = {v: 0 for v in graph} >>> quadratic = {edge: -1 for edge in graph.edges} >>> model = dimod.BinaryQuadraticModel(linear, quadratic, 0.0, vartype=dimod.SPIN) >>> widget = pm.PenaltyModel.from_specification(spec, model, 2., -2) >>> with pmc.cache_connect(':memory:') as cur: ... pmc.insert_penalty_model(cur, widget) """ encoded_data = {} linear, quadratic, offset = penalty_model.model.to_ising() nodelist = sorted(linear) edgelist = sorted(sorted(edge) for edge in penalty_model.graph.edges) insert_graph(cur, nodelist, edgelist, encoded_data) insert_feasible_configurations(cur, penalty_model.feasible_configurations, encoded_data) insert_ising_model(cur, nodelist, edgelist, linear, quadratic, offset, encoded_data) encoded_data['decision_variables'] = json.dumps(penalty_model.decision_variables, separators=(',', ':')) encoded_data['classical_gap'] = penalty_model.classical_gap encoded_data['ground_energy'] = penalty_model.ground_energy insert = \ """ INSERT OR IGNORE INTO penalty_model( decision_variables, classical_gap, ground_energy, feasible_configurations_id, ising_model_id) SELECT :decision_variables, :classical_gap, :ground_energy, feasible_configurations.id, ising_model.id FROM feasible_configurations, ising_model, graph WHERE graph.edges = :edges AND graph.num_nodes = :num_nodes AND ising_model.graph_id = graph.id AND ising_model.linear_biases = :linear_biases AND ising_model.quadratic_biases = :quadratic_biases AND ising_model.offset = :offset AND feasible_configurations.num_variables = :num_variables AND feasible_configurations.num_feasible_configurations = :num_feasible_configurations AND feasible_configurations.feasible_configurations = :feasible_configurations AND feasible_configurations.energies = :energies; """ cur.execute(insert, encoded_data)
python
def insert_penalty_model(cur, penalty_model): encoded_data = {} linear, quadratic, offset = penalty_model.model.to_ising() nodelist = sorted(linear) edgelist = sorted(sorted(edge) for edge in penalty_model.graph.edges) insert_graph(cur, nodelist, edgelist, encoded_data) insert_feasible_configurations(cur, penalty_model.feasible_configurations, encoded_data) insert_ising_model(cur, nodelist, edgelist, linear, quadratic, offset, encoded_data) encoded_data['decision_variables'] = json.dumps(penalty_model.decision_variables, separators=(',', ':')) encoded_data['classical_gap'] = penalty_model.classical_gap encoded_data['ground_energy'] = penalty_model.ground_energy insert = \ """ INSERT OR IGNORE INTO penalty_model( decision_variables, classical_gap, ground_energy, feasible_configurations_id, ising_model_id) SELECT :decision_variables, :classical_gap, :ground_energy, feasible_configurations.id, ising_model.id FROM feasible_configurations, ising_model, graph WHERE graph.edges = :edges AND graph.num_nodes = :num_nodes AND ising_model.graph_id = graph.id AND ising_model.linear_biases = :linear_biases AND ising_model.quadratic_biases = :quadratic_biases AND ising_model.offset = :offset AND feasible_configurations.num_variables = :num_variables AND feasible_configurations.num_feasible_configurations = :num_feasible_configurations AND feasible_configurations.feasible_configurations = :feasible_configurations AND feasible_configurations.energies = :energies; """ cur.execute(insert, encoded_data)
[ "def", "insert_penalty_model", "(", "cur", ",", "penalty_model", ")", ":", "encoded_data", "=", "{", "}", "linear", ",", "quadratic", ",", "offset", "=", "penalty_model", ".", "model", ".", "to_ising", "(", ")", "nodelist", "=", "sorted", "(", "linear", ")...
Insert a penalty model into the database. Args: cur (:class:`sqlite3.Cursor`): An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement. penalty_model (:class:`penaltymodel.PenaltyModel`): A penalty model to be stored in the database. Examples: >>> import networkx as nx >>> import penaltymodel.core as pm >>> import dimod >>> graph = nx.path_graph(3) >>> decision_variables = (0, 2) >>> feasible_configurations = {(-1, -1): 0., (+1, +1): 0.} >>> spec = pm.Specification(graph, decision_variables, feasible_configurations, dimod.SPIN) >>> linear = {v: 0 for v in graph} >>> quadratic = {edge: -1 for edge in graph.edges} >>> model = dimod.BinaryQuadraticModel(linear, quadratic, 0.0, vartype=dimod.SPIN) >>> widget = pm.PenaltyModel.from_specification(spec, model, 2., -2) >>> with pmc.cache_connect(':memory:') as cur: ... pmc.insert_penalty_model(cur, widget)
[ "Insert", "a", "penalty", "model", "into", "the", "database", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_cache/penaltymodel/cache/database_manager.py#L447-L514
16,869
dwavesystems/penaltymodel
penaltymodel_core/penaltymodel/core/classes/penaltymodel.py
PenaltyModel.from_specification
def from_specification(cls, specification, model, classical_gap, ground_energy): """Construct a PenaltyModel from a Specification. Args: specification (:class:`.Specification`): A specification that was used to generate the model. model (:class:`dimod.BinaryQuadraticModel`): A binary quadratic model that has ground states that match the feasible_configurations. classical_gap (numeric): The difference in classical energy between the ground state and the first excited state. Must be positive. ground_energy (numeric): The minimum energy of all possible configurations. Returns: :class:`.PenaltyModel` """ # Author note: there might be a way that avoids rechecking all of the values without # side-effects or lots of repeated code, but this seems simpler and more explicit return cls(specification.graph, specification.decision_variables, specification.feasible_configurations, specification.vartype, model, classical_gap, ground_energy, ising_linear_ranges=specification.ising_linear_ranges, ising_quadratic_ranges=specification.ising_quadratic_ranges)
python
def from_specification(cls, specification, model, classical_gap, ground_energy): # Author note: there might be a way that avoids rechecking all of the values without # side-effects or lots of repeated code, but this seems simpler and more explicit return cls(specification.graph, specification.decision_variables, specification.feasible_configurations, specification.vartype, model, classical_gap, ground_energy, ising_linear_ranges=specification.ising_linear_ranges, ising_quadratic_ranges=specification.ising_quadratic_ranges)
[ "def", "from_specification", "(", "cls", ",", "specification", ",", "model", ",", "classical_gap", ",", "ground_energy", ")", ":", "# Author note: there might be a way that avoids rechecking all of the values without", "# side-effects or lots of repeated code, but this seems simpler an...
Construct a PenaltyModel from a Specification. Args: specification (:class:`.Specification`): A specification that was used to generate the model. model (:class:`dimod.BinaryQuadraticModel`): A binary quadratic model that has ground states that match the feasible_configurations. classical_gap (numeric): The difference in classical energy between the ground state and the first excited state. Must be positive. ground_energy (numeric): The minimum energy of all possible configurations. Returns: :class:`.PenaltyModel`
[ "Construct", "a", "PenaltyModel", "from", "a", "Specification", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_core/penaltymodel/core/classes/penaltymodel.py#L175-L202
16,870
dwavesystems/penaltymodel
penaltymodel_cache/penaltymodel/cache/interface.py
get_penalty_model
def get_penalty_model(specification, database=None): """Factory function for penaltymodel_cache. Args: specification (penaltymodel.Specification): The specification for the desired penalty model. database (str, optional): The path to the desired sqlite database file. If None, will use the default. Returns: :class:`penaltymodel.PenaltyModel`: Penalty model with the given specification. Raises: :class:`penaltymodel.MissingPenaltyModel`: If the penalty model is not in the cache. Parameters: priority (int): 100 """ # only handles index-labelled nodes if not _is_index_labelled(specification.graph): relabel_applied = True mapping, inverse_mapping = _graph_canonicalization(specification.graph) specification = specification.relabel_variables(mapping, inplace=False) else: relabel_applied = False # connect to the database. Note that once the connection is made it cannot be # broken up between several processes. if database is None: conn = cache_connect() else: conn = cache_connect(database) # get the penalty_model with conn as cur: try: widget = next(iter_penalty_model_from_specification(cur, specification)) except StopIteration: widget = None # close the connection conn.close() if widget is None: raise pm.MissingPenaltyModel("no penalty model with the given specification found in cache") if relabel_applied: # relabel the widget in-place widget.relabel_variables(inverse_mapping, inplace=True) return widget
python
def get_penalty_model(specification, database=None): # only handles index-labelled nodes if not _is_index_labelled(specification.graph): relabel_applied = True mapping, inverse_mapping = _graph_canonicalization(specification.graph) specification = specification.relabel_variables(mapping, inplace=False) else: relabel_applied = False # connect to the database. Note that once the connection is made it cannot be # broken up between several processes. if database is None: conn = cache_connect() else: conn = cache_connect(database) # get the penalty_model with conn as cur: try: widget = next(iter_penalty_model_from_specification(cur, specification)) except StopIteration: widget = None # close the connection conn.close() if widget is None: raise pm.MissingPenaltyModel("no penalty model with the given specification found in cache") if relabel_applied: # relabel the widget in-place widget.relabel_variables(inverse_mapping, inplace=True) return widget
[ "def", "get_penalty_model", "(", "specification", ",", "database", "=", "None", ")", ":", "# only handles index-labelled nodes", "if", "not", "_is_index_labelled", "(", "specification", ".", "graph", ")", ":", "relabel_applied", "=", "True", "mapping", ",", "inverse...
Factory function for penaltymodel_cache. Args: specification (penaltymodel.Specification): The specification for the desired penalty model. database (str, optional): The path to the desired sqlite database file. If None, will use the default. Returns: :class:`penaltymodel.PenaltyModel`: Penalty model with the given specification. Raises: :class:`penaltymodel.MissingPenaltyModel`: If the penalty model is not in the cache. Parameters: priority (int): 100
[ "Factory", "function", "for", "penaltymodel_cache", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_cache/penaltymodel/cache/interface.py#L16-L68
16,871
dwavesystems/penaltymodel
penaltymodel_cache/penaltymodel/cache/interface.py
cache_penalty_model
def cache_penalty_model(penalty_model, database=None): """Caching function for penaltymodel_cache. Args: penalty_model (:class:`penaltymodel.PenaltyModel`): Penalty model to be cached. database (str, optional): The path to the desired sqlite database file. If None, will use the default. """ # only handles index-labelled nodes if not _is_index_labelled(penalty_model.graph): mapping, __ = _graph_canonicalization(penalty_model.graph) penalty_model = penalty_model.relabel_variables(mapping, inplace=False) # connect to the database. Note that once the connection is made it cannot be # broken up between several processes. if database is None: conn = cache_connect() else: conn = cache_connect(database) # load into the database with conn as cur: insert_penalty_model(cur, penalty_model) # close the connection conn.close()
python
def cache_penalty_model(penalty_model, database=None): # only handles index-labelled nodes if not _is_index_labelled(penalty_model.graph): mapping, __ = _graph_canonicalization(penalty_model.graph) penalty_model = penalty_model.relabel_variables(mapping, inplace=False) # connect to the database. Note that once the connection is made it cannot be # broken up between several processes. if database is None: conn = cache_connect() else: conn = cache_connect(database) # load into the database with conn as cur: insert_penalty_model(cur, penalty_model) # close the connection conn.close()
[ "def", "cache_penalty_model", "(", "penalty_model", ",", "database", "=", "None", ")", ":", "# only handles index-labelled nodes", "if", "not", "_is_index_labelled", "(", "penalty_model", ".", "graph", ")", ":", "mapping", ",", "__", "=", "_graph_canonicalization", ...
Caching function for penaltymodel_cache. Args: penalty_model (:class:`penaltymodel.PenaltyModel`): Penalty model to be cached. database (str, optional): The path to the desired sqlite database file. If None, will use the default.
[ "Caching", "function", "for", "penaltymodel_cache", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_cache/penaltymodel/cache/interface.py#L71-L99
16,872
dwavesystems/penaltymodel
penaltymodel_core/penaltymodel/core/interface.py
get_penalty_model
def get_penalty_model(specification): """Retrieve a PenaltyModel from one of the available factories. Args: specification (:class:`.Specification`): The specification for the desired PenaltyModel. Returns: :class:`.PenaltyModel`/None: A PenaltyModel as returned by the highest priority factory, or None if no factory could produce it. Raises: :exc:`ImpossiblePenaltyModel`: If the specification describes a penalty model that cannot be built by any factory. """ # Iterate through the available factories until one gives a penalty model for factory in iter_factories(): try: pm = factory(specification) except ImpossiblePenaltyModel as e: # information about impossible models should be propagated raise e except FactoryException: # any other type of factory exception, continue through the list continue # if penalty model was found, broadcast to all of the caches. This could be done # asynchronously for cache in iter_caches(): cache(pm) return pm return None
python
def get_penalty_model(specification): # Iterate through the available factories until one gives a penalty model for factory in iter_factories(): try: pm = factory(specification) except ImpossiblePenaltyModel as e: # information about impossible models should be propagated raise e except FactoryException: # any other type of factory exception, continue through the list continue # if penalty model was found, broadcast to all of the caches. This could be done # asynchronously for cache in iter_caches(): cache(pm) return pm return None
[ "def", "get_penalty_model", "(", "specification", ")", ":", "# Iterate through the available factories until one gives a penalty model", "for", "factory", "in", "iter_factories", "(", ")", ":", "try", ":", "pm", "=", "factory", "(", "specification", ")", "except", "Impo...
Retrieve a PenaltyModel from one of the available factories. Args: specification (:class:`.Specification`): The specification for the desired PenaltyModel. Returns: :class:`.PenaltyModel`/None: A PenaltyModel as returned by the highest priority factory, or None if no factory could produce it. Raises: :exc:`ImpossiblePenaltyModel`: If the specification describes a penalty model that cannot be built by any factory.
[ "Retrieve", "a", "PenaltyModel", "from", "one", "of", "the", "available", "factories", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_core/penaltymodel/core/interface.py#L37-L74
16,873
dwavesystems/penaltymodel
penaltymodel_core/penaltymodel/core/interface.py
iter_factories
def iter_factories(): """Iterate through all factories identified by the factory entrypoint. Yields: function: A function that accepts a :class:`.Specification` and returns a :class:`.PenaltyModel`. """ # retrieve all of the factories with factories = (entry.load() for entry in iter_entry_points(FACTORY_ENTRYPOINT)) # sort the factories from highest priority to lowest. Any factory with unknown priority # gets assigned priority -1000. for factory in sorted(factories, key=lambda f: getattr(f, 'priority', -1000), reverse=True): yield factory
python
def iter_factories(): # retrieve all of the factories with factories = (entry.load() for entry in iter_entry_points(FACTORY_ENTRYPOINT)) # sort the factories from highest priority to lowest. Any factory with unknown priority # gets assigned priority -1000. for factory in sorted(factories, key=lambda f: getattr(f, 'priority', -1000), reverse=True): yield factory
[ "def", "iter_factories", "(", ")", ":", "# retrieve all of the factories with", "factories", "=", "(", "entry", ".", "load", "(", ")", "for", "entry", "in", "iter_entry_points", "(", "FACTORY_ENTRYPOINT", ")", ")", "# sort the factories from highest priority to lowest. An...
Iterate through all factories identified by the factory entrypoint. Yields: function: A function that accepts a :class:`.Specification` and returns a :class:`.PenaltyModel`.
[ "Iterate", "through", "all", "factories", "identified", "by", "the", "factory", "entrypoint", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_core/penaltymodel/core/interface.py#L100-L114
16,874
dwavesystems/penaltymodel
penaltymodel_mip/penaltymodel/mip/generation.py
generate_bqm
def generate_bqm(graph, table, decision, linear_energy_ranges=None, quadratic_energy_ranges=None, min_classical_gap=2, precision=7, max_decision=8, max_variables=10, return_auxiliary=False): """Get a binary quadratic model with specific ground states. Args: graph (:obj:`~networkx.Graph`): Defines the structure of the generated binary quadratic model. table (iterable): Iterable of valid configurations (of spin-values). Each configuration is a tuple of variable assignments ordered by `decision`. decision (list/tuple): The variables in the binary quadratic model which have specified configurations. linear_energy_ranges (dict, optional): Dict of the form {v: (min, max, ...} where min and max are the range of values allowed to v. The default range is [-2, 2]. quadratic_energy_ranges (dict, optional): Dict of the form {(u, v): (min, max), ...} where min and max are the range of values allowed to (u, v). The default range is [-1, 1]. min_classical_gap (float): The minimum energy gap between the highest feasible state and the lowest infeasible state. precision (int, optional, default=7): Values returned by the optimization solver are rounded to `precision` digits of precision. max_decision (int, optional, default=4): Maximum number of decision variables allowed. The algorithm is valid for arbitrary sizes of problem but can be extremely slow. max_variables (int, optional, default=4): Maximum number of variables allowed. The algorithm is valid for arbitrary sizes of problem but can be extremely slow. return_auxiliary (bool, optional, False): If True, the auxiliary configurations are returned for each configuration in table. Returns: If return_auxiliary is False: :obj:`dimod.BinaryQuadraticModel`: The binary quadratic model. float: The classical gap. If return_auxiliary is True: :obj:`dimod.BinaryQuadraticModel`: The binary quadratic model. float: The classical gap. dict: The auxiliary configurations, keyed on the configurations in table. Raises: ImpossiblePenaltyModel: If the penalty model cannot be built. Normally due to a non-zero infeasible gap. """ # Developer note: This function is input checking and output formatting. The logic is # in _generate_ising if not isinstance(graph, nx.Graph): raise TypeError("expected input graph to be a NetworkX Graph.") if not set().union(*table).issubset({-1, 1}): raise ValueError("expected table to be spin-valued") if not isinstance(decision, list): decision = list(decision) # handle iterables if not all(v in graph for v in decision): raise ValueError("given graph does not match the variable labels in decision variables") num_var = len(decision) if any(len(config) != num_var for config in table): raise ValueError("number of decision variables does not match all of the entires in the table") if len(decision) > max_decision: raise ValueError(("The table is too large. Note that larger models can be built by setting " "max_decision to a higher number, but generation could be extremely slow.")) if len(graph) > max_variables: raise ValueError(("The graph is too large. Note that larger models can be built by setting " "max_variables to a higher number, but generation could be extremely slow.")) if linear_energy_ranges is None: linear_energy_ranges = defaultdict(lambda: (-2, 2)) if quadratic_energy_ranges is None: quadratic_energy_ranges = defaultdict(lambda: (-1, 1)) if not isinstance(table, Mapping): table = {config: 0. for config in table} h, J, offset, gap, aux = _generate_ising(graph, table, decision, min_classical_gap, linear_energy_ranges, quadratic_energy_ranges) bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN) bqm.add_variables_from((v, round(bias, precision)) for v, bias in h.items()) bqm.add_interactions_from((u, v, round(bias, precision)) for (u, v), bias in J.items()) bqm.add_offset(round(offset, precision)) if return_auxiliary: return bqm, round(gap, precision), aux else: return bqm, round(gap, precision)
python
def generate_bqm(graph, table, decision, linear_energy_ranges=None, quadratic_energy_ranges=None, min_classical_gap=2, precision=7, max_decision=8, max_variables=10, return_auxiliary=False): # Developer note: This function is input checking and output formatting. The logic is # in _generate_ising if not isinstance(graph, nx.Graph): raise TypeError("expected input graph to be a NetworkX Graph.") if not set().union(*table).issubset({-1, 1}): raise ValueError("expected table to be spin-valued") if not isinstance(decision, list): decision = list(decision) # handle iterables if not all(v in graph for v in decision): raise ValueError("given graph does not match the variable labels in decision variables") num_var = len(decision) if any(len(config) != num_var for config in table): raise ValueError("number of decision variables does not match all of the entires in the table") if len(decision) > max_decision: raise ValueError(("The table is too large. Note that larger models can be built by setting " "max_decision to a higher number, but generation could be extremely slow.")) if len(graph) > max_variables: raise ValueError(("The graph is too large. Note that larger models can be built by setting " "max_variables to a higher number, but generation could be extremely slow.")) if linear_energy_ranges is None: linear_energy_ranges = defaultdict(lambda: (-2, 2)) if quadratic_energy_ranges is None: quadratic_energy_ranges = defaultdict(lambda: (-1, 1)) if not isinstance(table, Mapping): table = {config: 0. for config in table} h, J, offset, gap, aux = _generate_ising(graph, table, decision, min_classical_gap, linear_energy_ranges, quadratic_energy_ranges) bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN) bqm.add_variables_from((v, round(bias, precision)) for v, bias in h.items()) bqm.add_interactions_from((u, v, round(bias, precision)) for (u, v), bias in J.items()) bqm.add_offset(round(offset, precision)) if return_auxiliary: return bqm, round(gap, precision), aux else: return bqm, round(gap, precision)
[ "def", "generate_bqm", "(", "graph", ",", "table", ",", "decision", ",", "linear_energy_ranges", "=", "None", ",", "quadratic_energy_ranges", "=", "None", ",", "min_classical_gap", "=", "2", ",", "precision", "=", "7", ",", "max_decision", "=", "8", ",", "ma...
Get a binary quadratic model with specific ground states. Args: graph (:obj:`~networkx.Graph`): Defines the structure of the generated binary quadratic model. table (iterable): Iterable of valid configurations (of spin-values). Each configuration is a tuple of variable assignments ordered by `decision`. decision (list/tuple): The variables in the binary quadratic model which have specified configurations. linear_energy_ranges (dict, optional): Dict of the form {v: (min, max, ...} where min and max are the range of values allowed to v. The default range is [-2, 2]. quadratic_energy_ranges (dict, optional): Dict of the form {(u, v): (min, max), ...} where min and max are the range of values allowed to (u, v). The default range is [-1, 1]. min_classical_gap (float): The minimum energy gap between the highest feasible state and the lowest infeasible state. precision (int, optional, default=7): Values returned by the optimization solver are rounded to `precision` digits of precision. max_decision (int, optional, default=4): Maximum number of decision variables allowed. The algorithm is valid for arbitrary sizes of problem but can be extremely slow. max_variables (int, optional, default=4): Maximum number of variables allowed. The algorithm is valid for arbitrary sizes of problem but can be extremely slow. return_auxiliary (bool, optional, False): If True, the auxiliary configurations are returned for each configuration in table. Returns: If return_auxiliary is False: :obj:`dimod.BinaryQuadraticModel`: The binary quadratic model. float: The classical gap. If return_auxiliary is True: :obj:`dimod.BinaryQuadraticModel`: The binary quadratic model. float: The classical gap. dict: The auxiliary configurations, keyed on the configurations in table. Raises: ImpossiblePenaltyModel: If the penalty model cannot be built. Normally due to a non-zero infeasible gap.
[ "Get", "a", "binary", "quadratic", "model", "with", "specific", "ground", "states", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_mip/penaltymodel/mip/generation.py#L14-L124
16,875
dwavesystems/penaltymodel
penaltymodel_lp/penaltymodel/lp/interface.py
get_penalty_model
def get_penalty_model(specification): """Factory function for penaltymodel-lp. Args: specification (penaltymodel.Specification): The specification for the desired penalty model. Returns: :class:`penaltymodel.PenaltyModel`: Penalty model with the given specification. Raises: :class:`penaltymodel.ImpossiblePenaltyModel`: If the penalty cannot be built. Parameters: priority (int): -100 """ # check that the feasible_configurations are spin feasible_configurations = specification.feasible_configurations if specification.vartype is dimod.BINARY: feasible_configurations = {tuple(2 * v - 1 for v in config): en for config, en in iteritems(feasible_configurations)} # convert ising_quadratic_ranges to the form we expect ising_quadratic_ranges = specification.ising_quadratic_ranges quadratic_ranges = {(u, v): ising_quadratic_ranges[u][v] for u, v in specification.graph.edges} try: bqm, gap = generate_bqm(specification.graph, feasible_configurations, specification.decision_variables, linear_energy_ranges=specification.ising_linear_ranges, quadratic_energy_ranges=quadratic_ranges, min_classical_gap=specification.min_classical_gap) except ValueError: raise pm.exceptions.FactoryException("Specification is for too large of a model") return pm.PenaltyModel.from_specification(specification, bqm, gap, 0.0)
python
def get_penalty_model(specification): # check that the feasible_configurations are spin feasible_configurations = specification.feasible_configurations if specification.vartype is dimod.BINARY: feasible_configurations = {tuple(2 * v - 1 for v in config): en for config, en in iteritems(feasible_configurations)} # convert ising_quadratic_ranges to the form we expect ising_quadratic_ranges = specification.ising_quadratic_ranges quadratic_ranges = {(u, v): ising_quadratic_ranges[u][v] for u, v in specification.graph.edges} try: bqm, gap = generate_bqm(specification.graph, feasible_configurations, specification.decision_variables, linear_energy_ranges=specification.ising_linear_ranges, quadratic_energy_ranges=quadratic_ranges, min_classical_gap=specification.min_classical_gap) except ValueError: raise pm.exceptions.FactoryException("Specification is for too large of a model") return pm.PenaltyModel.from_specification(specification, bqm, gap, 0.0)
[ "def", "get_penalty_model", "(", "specification", ")", ":", "# check that the feasible_configurations are spin", "feasible_configurations", "=", "specification", ".", "feasible_configurations", "if", "specification", ".", "vartype", "is", "dimod", ".", "BINARY", ":", "feasi...
Factory function for penaltymodel-lp. Args: specification (penaltymodel.Specification): The specification for the desired penalty model. Returns: :class:`penaltymodel.PenaltyModel`: Penalty model with the given specification. Raises: :class:`penaltymodel.ImpossiblePenaltyModel`: If the penalty cannot be built. Parameters: priority (int): -100
[ "Factory", "function", "for", "penaltymodel", "-", "lp", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_lp/penaltymodel/lp/interface.py#L13-L49
16,876
dwavesystems/penaltymodel
penaltymodel_lp/penaltymodel/lp/generation.py
get_item
def get_item(dictionary, tuple_key, default_value): """Grab values from a dictionary using an unordered tuple as a key. Dictionary should not contain None, 0, or False as dictionary values. Args: dictionary: Dictionary that uses two-element tuple as keys tuple_key: Unordered tuple of two elements default_value: Value that is returned when the tuple_key is not found in the dictionary """ u, v = tuple_key # Grab tuple-values from dictionary tuple1 = dictionary.get((u, v), None) tuple2 = dictionary.get((v, u), None) # Return the first value that is not {None, 0, False} return tuple1 or tuple2 or default_value
python
def get_item(dictionary, tuple_key, default_value): u, v = tuple_key # Grab tuple-values from dictionary tuple1 = dictionary.get((u, v), None) tuple2 = dictionary.get((v, u), None) # Return the first value that is not {None, 0, False} return tuple1 or tuple2 or default_value
[ "def", "get_item", "(", "dictionary", ",", "tuple_key", ",", "default_value", ")", ":", "u", ",", "v", "=", "tuple_key", "# Grab tuple-values from dictionary", "tuple1", "=", "dictionary", ".", "get", "(", "(", "u", ",", "v", ")", ",", "None", ")", "tuple2...
Grab values from a dictionary using an unordered tuple as a key. Dictionary should not contain None, 0, or False as dictionary values. Args: dictionary: Dictionary that uses two-element tuple as keys tuple_key: Unordered tuple of two elements default_value: Value that is returned when the tuple_key is not found in the dictionary
[ "Grab", "values", "from", "a", "dictionary", "using", "an", "unordered", "tuple", "as", "a", "key", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_lp/penaltymodel/lp/generation.py#L14-L31
16,877
dwavesystems/penaltymodel
penaltymodel_maxgap/penaltymodel/maxgap/theta.py
limitReal
def limitReal(x, max_denominator=1000000): """Creates an pysmt Real constant from x. Args: x (number): A number to be cast to a pysmt constant. max_denominator (int, optional): The maximum size of the denominator. Default 1000000. Returns: A Real constant with the given value and the denominator limited. """ f = Fraction(x).limit_denominator(max_denominator) return Real((f.numerator, f.denominator))
python
def limitReal(x, max_denominator=1000000): f = Fraction(x).limit_denominator(max_denominator) return Real((f.numerator, f.denominator))
[ "def", "limitReal", "(", "x", ",", "max_denominator", "=", "1000000", ")", ":", "f", "=", "Fraction", "(", "x", ")", ".", "limit_denominator", "(", "max_denominator", ")", "return", "Real", "(", "(", "f", ".", "numerator", ",", "f", ".", "denominator", ...
Creates an pysmt Real constant from x. Args: x (number): A number to be cast to a pysmt constant. max_denominator (int, optional): The maximum size of the denominator. Default 1000000. Returns: A Real constant with the given value and the denominator limited.
[ "Creates", "an", "pysmt", "Real", "constant", "from", "x", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_maxgap/penaltymodel/maxgap/theta.py#L26-L39
16,878
dwavesystems/penaltymodel
penaltymodel_maxgap/penaltymodel/maxgap/theta.py
Theta.from_graph
def from_graph(cls, graph, linear_energy_ranges, quadratic_energy_ranges): """Create Theta from a graph and energy ranges. Args: graph (:obj:`networkx.Graph`): Provides the structure for Theta. linear_energy_ranges (dict): A dict of the form {v: (min, max), ...} where min and max are the range of values allowed to v. quadratic_energy_ranges (dict): A dict of the form {(u, v): (min, max), ...} where min and max are the range of values allowed to (u, v). Returns: :obj:`.Theta` """ get_env().enable_infix_notation = True # not sure why we need this here theta = cls.empty(dimod.SPIN) theta.add_offset(Symbol('offset', REAL)) def Linear(v): """Create a Symbol for the linear bias including the energy range constraints.""" bias = Symbol('h_{}'.format(v), REAL) min_, max_ = linear_energy_ranges[v] theta.assertions.add(LE(bias, limitReal(max_))) theta.assertions.add(GE(bias, limitReal(min_))) return bias def Quadratic(u, v): """Create a Symbol for the quadratic bias including the energy range constraints.""" bias = Symbol('J_{},{}'.format(u, v), REAL) if (v, u) in quadratic_energy_ranges: min_, max_ = quadratic_energy_ranges[(v, u)] else: min_, max_ = quadratic_energy_ranges[(u, v)] theta.assertions.add(LE(bias, limitReal(max_))) theta.assertions.add(GE(bias, limitReal(min_))) return bias for v in graph.nodes: theta.add_variable(v, Linear(v)) for u, v in graph.edges: theta.add_interaction(u, v, Quadratic(u, v)) return theta
python
def from_graph(cls, graph, linear_energy_ranges, quadratic_energy_ranges): get_env().enable_infix_notation = True # not sure why we need this here theta = cls.empty(dimod.SPIN) theta.add_offset(Symbol('offset', REAL)) def Linear(v): """Create a Symbol for the linear bias including the energy range constraints.""" bias = Symbol('h_{}'.format(v), REAL) min_, max_ = linear_energy_ranges[v] theta.assertions.add(LE(bias, limitReal(max_))) theta.assertions.add(GE(bias, limitReal(min_))) return bias def Quadratic(u, v): """Create a Symbol for the quadratic bias including the energy range constraints.""" bias = Symbol('J_{},{}'.format(u, v), REAL) if (v, u) in quadratic_energy_ranges: min_, max_ = quadratic_energy_ranges[(v, u)] else: min_, max_ = quadratic_energy_ranges[(u, v)] theta.assertions.add(LE(bias, limitReal(max_))) theta.assertions.add(GE(bias, limitReal(min_))) return bias for v in graph.nodes: theta.add_variable(v, Linear(v)) for u, v in graph.edges: theta.add_interaction(u, v, Quadratic(u, v)) return theta
[ "def", "from_graph", "(", "cls", ",", "graph", ",", "linear_energy_ranges", ",", "quadratic_energy_ranges", ")", ":", "get_env", "(", ")", ".", "enable_infix_notation", "=", "True", "# not sure why we need this here", "theta", "=", "cls", ".", "empty", "(", "dimod...
Create Theta from a graph and energy ranges. Args: graph (:obj:`networkx.Graph`): Provides the structure for Theta. linear_energy_ranges (dict): A dict of the form {v: (min, max), ...} where min and max are the range of values allowed to v. quadratic_energy_ranges (dict): A dict of the form {(u, v): (min, max), ...} where min and max are the range of values allowed to (u, v). Returns: :obj:`.Theta`
[ "Create", "Theta", "from", "a", "graph", "and", "energy", "ranges", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_maxgap/penaltymodel/maxgap/theta.py#L55-L112
16,879
dwavesystems/penaltymodel
penaltymodel_maxgap/penaltymodel/maxgap/theta.py
Theta.to_bqm
def to_bqm(self, model): """Given a pysmt model, return a bqm. Adds the values of the biases as determined by the SMT solver to a bqm. Args: model: A pysmt model. Returns: :obj:`dimod.BinaryQuadraticModel` """ linear = ((v, float(model.get_py_value(bias))) for v, bias in self.linear.items()) quadratic = ((u, v, float(model.get_py_value(bias))) for (u, v), bias in self.quadratic.items()) offset = float(model.get_py_value(self.offset)) return dimod.BinaryQuadraticModel(linear, quadratic, offset, dimod.SPIN)
python
def to_bqm(self, model): linear = ((v, float(model.get_py_value(bias))) for v, bias in self.linear.items()) quadratic = ((u, v, float(model.get_py_value(bias))) for (u, v), bias in self.quadratic.items()) offset = float(model.get_py_value(self.offset)) return dimod.BinaryQuadraticModel(linear, quadratic, offset, dimod.SPIN)
[ "def", "to_bqm", "(", "self", ",", "model", ")", ":", "linear", "=", "(", "(", "v", ",", "float", "(", "model", ".", "get_py_value", "(", "bias", ")", ")", ")", "for", "v", ",", "bias", "in", "self", ".", "linear", ".", "items", "(", ")", ")", ...
Given a pysmt model, return a bqm. Adds the values of the biases as determined by the SMT solver to a bqm. Args: model: A pysmt model. Returns: :obj:`dimod.BinaryQuadraticModel`
[ "Given", "a", "pysmt", "model", "return", "a", "bqm", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_maxgap/penaltymodel/maxgap/theta.py#L114-L132
16,880
dwavesystems/penaltymodel
penaltymodel_maxgap/penaltymodel/maxgap/smt.py
SpinTimes
def SpinTimes(spin, bias): """Define our own multiplication for bias times spins. This allows for cleaner log code as well as value checking. Args: spin (int): -1 or 1 bias (:class:`pysmt.shortcuts.Symbol`): The bias Returns: spins * bias """ if not isinstance(spin, int): raise TypeError('spin must be an int') if spin == -1: return Times(Real((-1, 1)), bias) # -1 / 1 elif spin == 1: # identity return bias else: raise ValueError('expected spins to be -1., or 1.')
python
def SpinTimes(spin, bias): if not isinstance(spin, int): raise TypeError('spin must be an int') if spin == -1: return Times(Real((-1, 1)), bias) # -1 / 1 elif spin == 1: # identity return bias else: raise ValueError('expected spins to be -1., or 1.')
[ "def", "SpinTimes", "(", "spin", ",", "bias", ")", ":", "if", "not", "isinstance", "(", "spin", ",", "int", ")", ":", "raise", "TypeError", "(", "'spin must be an int'", ")", "if", "spin", "==", "-", "1", ":", "return", "Times", "(", "Real", "(", "("...
Define our own multiplication for bias times spins. This allows for cleaner log code as well as value checking. Args: spin (int): -1 or 1 bias (:class:`pysmt.shortcuts.Symbol`): The bias Returns: spins * bias
[ "Define", "our", "own", "multiplication", "for", "bias", "times", "spins", ".", "This", "allows", "for", "cleaner", "log", "code", "as", "well", "as", "value", "checking", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_maxgap/penaltymodel/maxgap/smt.py#L33-L53
16,881
dwavesystems/penaltymodel
penaltymodel_maxgap/penaltymodel/maxgap/smt.py
_elimination_trees
def _elimination_trees(theta, decision_variables): """From Theta and the decision variables, determine the elimination order and the induced trees. """ # auxiliary variables are any variables that are not decision auxiliary_variables = set(n for n in theta.linear if n not in decision_variables) # get the adjacency of the auxiliary subgraph adj = {v: {u for u in theta.adj[v] if u in auxiliary_variables} for v in theta.adj if v in auxiliary_variables} # get the elimination order that minimizes treewidth tw, order = dnx.treewidth_branch_and_bound(adj) ancestors = {} for n in order: ancestors[n] = set(adj[n]) # now make v simplicial by making its neighborhood a clique, then # continue neighbors = adj[n] for u, v in itertools.combinations(neighbors, 2): adj[u].add(v) adj[v].add(u) for v in neighbors: adj[v].discard(n) del adj[n] roots = {} nodes = {v: {} for v in ancestors} for vidx in range(len(order) - 1, -1, -1): v = order[vidx] if ancestors[v]: for u in order[vidx + 1:]: if u in ancestors[v]: # v is a child of u nodes[u][v] = nodes[v] # nodes[u][v] = children of v break else: roots[v] = nodes[v] # roots[v] = children of v return roots, ancestors
python
def _elimination_trees(theta, decision_variables): # auxiliary variables are any variables that are not decision auxiliary_variables = set(n for n in theta.linear if n not in decision_variables) # get the adjacency of the auxiliary subgraph adj = {v: {u for u in theta.adj[v] if u in auxiliary_variables} for v in theta.adj if v in auxiliary_variables} # get the elimination order that minimizes treewidth tw, order = dnx.treewidth_branch_and_bound(adj) ancestors = {} for n in order: ancestors[n] = set(adj[n]) # now make v simplicial by making its neighborhood a clique, then # continue neighbors = adj[n] for u, v in itertools.combinations(neighbors, 2): adj[u].add(v) adj[v].add(u) for v in neighbors: adj[v].discard(n) del adj[n] roots = {} nodes = {v: {} for v in ancestors} for vidx in range(len(order) - 1, -1, -1): v = order[vidx] if ancestors[v]: for u in order[vidx + 1:]: if u in ancestors[v]: # v is a child of u nodes[u][v] = nodes[v] # nodes[u][v] = children of v break else: roots[v] = nodes[v] # roots[v] = children of v return roots, ancestors
[ "def", "_elimination_trees", "(", "theta", ",", "decision_variables", ")", ":", "# auxiliary variables are any variables that are not decision", "auxiliary_variables", "=", "set", "(", "n", "for", "n", "in", "theta", ".", "linear", "if", "n", "not", "in", "decision_va...
From Theta and the decision variables, determine the elimination order and the induced trees.
[ "From", "Theta", "and", "the", "decision", "variables", "determine", "the", "elimination", "order", "and", "the", "induced", "trees", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_maxgap/penaltymodel/maxgap/smt.py#L56-L98
16,882
dwavesystems/penaltymodel
penaltymodel_maxgap/penaltymodel/maxgap/smt.py
Table.energy_upperbound
def energy_upperbound(self, spins): """A formula for an upper bound on the energy of Theta with spins fixed. Args: spins (dict): Spin values for a subset of the variables in Theta. Returns: Formula that upper bounds the energy with spins fixed. """ subtheta = self.theta.copy() subtheta.fix_variables(spins) # ok, let's start eliminating variables trees = self._trees if not trees: # if there are no variables to eliminate, then the offset of # subtheta is the exact value and we can just return it assert not subtheta.linear and not subtheta.quadratic return subtheta.offset energy = Plus(self.message_upperbound(trees, {}, subtheta), subtheta.offset) return energy
python
def energy_upperbound(self, spins): subtheta = self.theta.copy() subtheta.fix_variables(spins) # ok, let's start eliminating variables trees = self._trees if not trees: # if there are no variables to eliminate, then the offset of # subtheta is the exact value and we can just return it assert not subtheta.linear and not subtheta.quadratic return subtheta.offset energy = Plus(self.message_upperbound(trees, {}, subtheta), subtheta.offset) return energy
[ "def", "energy_upperbound", "(", "self", ",", "spins", ")", ":", "subtheta", "=", "self", ".", "theta", ".", "copy", "(", ")", "subtheta", ".", "fix_variables", "(", "spins", ")", "# ok, let's start eliminating variables", "trees", "=", "self", ".", "_trees", ...
A formula for an upper bound on the energy of Theta with spins fixed. Args: spins (dict): Spin values for a subset of the variables in Theta. Returns: Formula that upper bounds the energy with spins fixed.
[ "A", "formula", "for", "an", "upper", "bound", "on", "the", "energy", "of", "Theta", "with", "spins", "fixed", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_maxgap/penaltymodel/maxgap/smt.py#L133-L157
16,883
dwavesystems/penaltymodel
penaltymodel_maxgap/penaltymodel/maxgap/smt.py
Table.energy
def energy(self, spins, break_aux_symmetry=True): """A formula for the exact energy of Theta with spins fixed. Args: spins (dict): Spin values for a subset of the variables in Theta. break_aux_symmetry (bool, optional): Default True. If True, break the aux variable symmetry by setting all aux variable to 1 for one of the feasible configurations. If the energy ranges are not symmetric then this can make finding models impossible. Returns: Formula for the exact energy of Theta with spins fixed. """ subtheta = self.theta.copy() subtheta.fix_variables(spins) # we need aux variables av = next(self._auxvar_counter) auxvars = {v: Symbol('aux{}_{}'.format(av, v), BOOL) for v in subtheta.linear} if break_aux_symmetry and av == 0: # without loss of generality, we can assume that the aux variables are all # spin-up for one configuration self.assertions.update(set(auxvars.values())) trees = self._trees if not trees: # if there are no variables to eliminate, then the offset of # subtheta is the exact value and we can just return it assert not subtheta.linear and not subtheta.quadratic return subtheta.offset energy = Plus(self.message(trees, {}, subtheta, auxvars), subtheta.offset) return energy
python
def energy(self, spins, break_aux_symmetry=True): subtheta = self.theta.copy() subtheta.fix_variables(spins) # we need aux variables av = next(self._auxvar_counter) auxvars = {v: Symbol('aux{}_{}'.format(av, v), BOOL) for v in subtheta.linear} if break_aux_symmetry and av == 0: # without loss of generality, we can assume that the aux variables are all # spin-up for one configuration self.assertions.update(set(auxvars.values())) trees = self._trees if not trees: # if there are no variables to eliminate, then the offset of # subtheta is the exact value and we can just return it assert not subtheta.linear and not subtheta.quadratic return subtheta.offset energy = Plus(self.message(trees, {}, subtheta, auxvars), subtheta.offset) return energy
[ "def", "energy", "(", "self", ",", "spins", ",", "break_aux_symmetry", "=", "True", ")", ":", "subtheta", "=", "self", ".", "theta", ".", "copy", "(", ")", "subtheta", ".", "fix_variables", "(", "spins", ")", "# we need aux variables", "av", "=", "next", ...
A formula for the exact energy of Theta with spins fixed. Args: spins (dict): Spin values for a subset of the variables in Theta. break_aux_symmetry (bool, optional): Default True. If True, break the aux variable symmetry by setting all aux variable to 1 for one of the feasible configurations. If the energy ranges are not symmetric then this can make finding models impossible. Returns: Formula for the exact energy of Theta with spins fixed.
[ "A", "formula", "for", "the", "exact", "energy", "of", "Theta", "with", "spins", "fixed", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_maxgap/penaltymodel/maxgap/smt.py#L159-L194
16,884
dwavesystems/penaltymodel
penaltymodel_maxgap/penaltymodel/maxgap/smt.py
Table.message
def message(self, tree, spins, subtheta, auxvars): """Determine the energy of the elimination tree. Args: tree (dict): The current elimination tree spins (dict): The current fixed spins subtheta (dict): Theta with spins fixed. auxvars (dict): The auxiliary variables for the given spins. Returns: The formula for the energy of the tree. """ energy_sources = set() for v, children in tree.items(): aux = auxvars[v] assert all(u in spins for u in self._ancestors[v]) # build an iterable over all of the energies contributions # that we can exactly determine given v and our known spins # in these contributions we assume that v is positive def energy_contributions(): yield subtheta.linear[v] for u, bias in subtheta.adj[v].items(): if u in spins: yield SpinTimes(spins[u], bias) plus_energy = Plus(energy_contributions()) minus_energy = SpinTimes(-1, plus_energy) # if the variable has children, we need to recursively determine their energies if children: # set v to be positive spins[v] = 1 plus_energy = Plus(plus_energy, self.message(children, spins, subtheta, auxvars)) spins[v] = -1 minus_energy = Plus(minus_energy, self.message(children, spins, subtheta, auxvars)) del spins[v] # we now need a real-valued smt variable to be our message m = FreshSymbol(REAL) ancestor_aux = {auxvars[u] if spins[u] > 0 else Not(auxvars[u]) for u in self._ancestors[v]} plus_aux = And({aux}.union(ancestor_aux)) minus_aux = And({Not(aux)}.union(ancestor_aux)) self.assertions.update({LE(m, plus_energy), LE(m, minus_energy), Implies(plus_aux, GE(m, plus_energy)), Implies(minus_aux, GE(m, minus_energy)) }) energy_sources.add(m) return Plus(energy_sources)
python
def message(self, tree, spins, subtheta, auxvars): energy_sources = set() for v, children in tree.items(): aux = auxvars[v] assert all(u in spins for u in self._ancestors[v]) # build an iterable over all of the energies contributions # that we can exactly determine given v and our known spins # in these contributions we assume that v is positive def energy_contributions(): yield subtheta.linear[v] for u, bias in subtheta.adj[v].items(): if u in spins: yield SpinTimes(spins[u], bias) plus_energy = Plus(energy_contributions()) minus_energy = SpinTimes(-1, plus_energy) # if the variable has children, we need to recursively determine their energies if children: # set v to be positive spins[v] = 1 plus_energy = Plus(plus_energy, self.message(children, spins, subtheta, auxvars)) spins[v] = -1 minus_energy = Plus(minus_energy, self.message(children, spins, subtheta, auxvars)) del spins[v] # we now need a real-valued smt variable to be our message m = FreshSymbol(REAL) ancestor_aux = {auxvars[u] if spins[u] > 0 else Not(auxvars[u]) for u in self._ancestors[v]} plus_aux = And({aux}.union(ancestor_aux)) minus_aux = And({Not(aux)}.union(ancestor_aux)) self.assertions.update({LE(m, plus_energy), LE(m, minus_energy), Implies(plus_aux, GE(m, plus_energy)), Implies(minus_aux, GE(m, minus_energy)) }) energy_sources.add(m) return Plus(energy_sources)
[ "def", "message", "(", "self", ",", "tree", ",", "spins", ",", "subtheta", ",", "auxvars", ")", ":", "energy_sources", "=", "set", "(", ")", "for", "v", ",", "children", "in", "tree", ".", "items", "(", ")", ":", "aux", "=", "auxvars", "[", "v", ...
Determine the energy of the elimination tree. Args: tree (dict): The current elimination tree spins (dict): The current fixed spins subtheta (dict): Theta with spins fixed. auxvars (dict): The auxiliary variables for the given spins. Returns: The formula for the energy of the tree.
[ "Determine", "the", "energy", "of", "the", "elimination", "tree", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_maxgap/penaltymodel/maxgap/smt.py#L196-L253
16,885
dwavesystems/penaltymodel
penaltymodel_maxgap/penaltymodel/maxgap/smt.py
Table.message_upperbound
def message_upperbound(self, tree, spins, subtheta): """Determine an upper bound on the energy of the elimination tree. Args: tree (dict): The current elimination tree spins (dict): The current fixed spins subtheta (dict): Theta with spins fixed. Returns: The formula for the energy of the tree. """ energy_sources = set() for v, subtree in tree.items(): assert all(u in spins for u in self._ancestors[v]) # build an iterable over all of the energies contributions # that we can exactly determine given v and our known spins # in these contributions we assume that v is positive def energy_contributions(): yield subtheta.linear[v] for u, bias in subtheta.adj[v].items(): if u in spins: yield Times(limitReal(spins[u]), bias) energy = Plus(energy_contributions()) # if there are no more variables in the order, we can stop # otherwise we need the next message variable if subtree: spins[v] = 1. plus = self.message_upperbound(subtree, spins, subtheta) spins[v] = -1. minus = self.message_upperbound(subtree, spins, subtheta) del spins[v] else: plus = minus = limitReal(0.0) # we now need a real-valued smt variable to be our message m = FreshSymbol(REAL) self.assertions.update({LE(m, Plus(energy, plus)), LE(m, Plus(Times(energy, limitReal(-1.)), minus))}) energy_sources.add(m) return Plus(energy_sources)
python
def message_upperbound(self, tree, spins, subtheta): energy_sources = set() for v, subtree in tree.items(): assert all(u in spins for u in self._ancestors[v]) # build an iterable over all of the energies contributions # that we can exactly determine given v and our known spins # in these contributions we assume that v is positive def energy_contributions(): yield subtheta.linear[v] for u, bias in subtheta.adj[v].items(): if u in spins: yield Times(limitReal(spins[u]), bias) energy = Plus(energy_contributions()) # if there are no more variables in the order, we can stop # otherwise we need the next message variable if subtree: spins[v] = 1. plus = self.message_upperbound(subtree, spins, subtheta) spins[v] = -1. minus = self.message_upperbound(subtree, spins, subtheta) del spins[v] else: plus = minus = limitReal(0.0) # we now need a real-valued smt variable to be our message m = FreshSymbol(REAL) self.assertions.update({LE(m, Plus(energy, plus)), LE(m, Plus(Times(energy, limitReal(-1.)), minus))}) energy_sources.add(m) return Plus(energy_sources)
[ "def", "message_upperbound", "(", "self", ",", "tree", ",", "spins", ",", "subtheta", ")", ":", "energy_sources", "=", "set", "(", ")", "for", "v", ",", "subtree", "in", "tree", ".", "items", "(", ")", ":", "assert", "all", "(", "u", "in", "spins", ...
Determine an upper bound on the energy of the elimination tree. Args: tree (dict): The current elimination tree spins (dict): The current fixed spins subtheta (dict): Theta with spins fixed. Returns: The formula for the energy of the tree.
[ "Determine", "an", "upper", "bound", "on", "the", "energy", "of", "the", "elimination", "tree", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_maxgap/penaltymodel/maxgap/smt.py#L255-L303
16,886
dwavesystems/penaltymodel
penaltymodel_maxgap/penaltymodel/maxgap/smt.py
Table.set_energy
def set_energy(self, spins, target_energy): """Set the energy of Theta with spins fixed to target_energy. Args: spins (dict): Spin values for a subset of the variables in Theta. target_energy (float): The desired energy for Theta with spins fixed. Notes: Add equality constraint to assertions. """ spin_energy = self.energy(spins) self.assertions.add(Equals(spin_energy, limitReal(target_energy)))
python
def set_energy(self, spins, target_energy): spin_energy = self.energy(spins) self.assertions.add(Equals(spin_energy, limitReal(target_energy)))
[ "def", "set_energy", "(", "self", ",", "spins", ",", "target_energy", ")", ":", "spin_energy", "=", "self", ".", "energy", "(", "spins", ")", "self", ".", "assertions", ".", "add", "(", "Equals", "(", "spin_energy", ",", "limitReal", "(", "target_energy", ...
Set the energy of Theta with spins fixed to target_energy. Args: spins (dict): Spin values for a subset of the variables in Theta. target_energy (float): The desired energy for Theta with spins fixed. Notes: Add equality constraint to assertions.
[ "Set", "the", "energy", "of", "Theta", "with", "spins", "fixed", "to", "target_energy", "." ]
b9d343233aea8df0f59cea45a07f12d0b3b8d9b3
https://github.com/dwavesystems/penaltymodel/blob/b9d343233aea8df0f59cea45a07f12d0b3b8d9b3/penaltymodel_maxgap/penaltymodel/maxgap/smt.py#L305-L317
16,887
dls-controls/pymalcolm
malcolm/modules/pandablocks/controllers/pandablocksmanagercontroller.py
PandABlocksManagerController._poll_loop
def _poll_loop(self): """At self.poll_period poll for changes""" next_poll = time.time() while True: next_poll += self._poll_period timeout = next_poll - time.time() if timeout < 0: timeout = 0 try: return self._stop_queue.get(timeout=timeout) except TimeoutError: # No stop, no problem pass try: self.handle_changes(self.client.get_changes()) except Exception: # TODO: should fault here? self.log.exception("Error while getting changes")
python
def _poll_loop(self): next_poll = time.time() while True: next_poll += self._poll_period timeout = next_poll - time.time() if timeout < 0: timeout = 0 try: return self._stop_queue.get(timeout=timeout) except TimeoutError: # No stop, no problem pass try: self.handle_changes(self.client.get_changes()) except Exception: # TODO: should fault here? self.log.exception("Error while getting changes")
[ "def", "_poll_loop", "(", "self", ")", ":", "next_poll", "=", "time", ".", "time", "(", ")", "while", "True", ":", "next_poll", "+=", "self", ".", "_poll_period", "timeout", "=", "next_poll", "-", "time", ".", "time", "(", ")", "if", "timeout", "<", ...
At self.poll_period poll for changes
[ "At", "self", ".", "poll_period", "poll", "for", "changes" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/modules/pandablocks/controllers/pandablocksmanagercontroller.py#L101-L118
16,888
dls-controls/pymalcolm
malcolm/core/serializable.py
camel_to_title
def camel_to_title(name): """Takes a camelCaseFieldName and returns an Title Case Field Name Args: name (str): E.g. camelCaseFieldName Returns: str: Title Case converted name. E.g. Camel Case Field Name """ split = re.findall(r"[A-Z]?[a-z0-9]+|[A-Z]+(?=[A-Z]|$)", name) ret = " ".join(split) ret = ret[0].upper() + ret[1:] return ret
python
def camel_to_title(name): split = re.findall(r"[A-Z]?[a-z0-9]+|[A-Z]+(?=[A-Z]|$)", name) ret = " ".join(split) ret = ret[0].upper() + ret[1:] return ret
[ "def", "camel_to_title", "(", "name", ")", ":", "split", "=", "re", ".", "findall", "(", "r\"[A-Z]?[a-z0-9]+|[A-Z]+(?=[A-Z]|$)\"", ",", "name", ")", "ret", "=", "\" \"", ".", "join", "(", "split", ")", "ret", "=", "ret", "[", "0", "]", ".", "upper", "(...
Takes a camelCaseFieldName and returns an Title Case Field Name Args: name (str): E.g. camelCaseFieldName Returns: str: Title Case converted name. E.g. Camel Case Field Name
[ "Takes", "a", "camelCaseFieldName", "and", "returns", "an", "Title", "Case", "Field", "Name" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/core/serializable.py#L55-L67
16,889
dls-controls/pymalcolm
malcolm/core/serializable.py
snake_to_camel
def snake_to_camel(name): """Takes a snake_field_name and returns a camelCaseFieldName Args: name (str): E.g. snake_field_name or SNAKE_FIELD_NAME Returns: str: camelCase converted name. E.g. capsFieldName """ ret = "".join(x.title() for x in name.split("_")) ret = ret[0].lower() + ret[1:] return ret
python
def snake_to_camel(name): ret = "".join(x.title() for x in name.split("_")) ret = ret[0].lower() + ret[1:] return ret
[ "def", "snake_to_camel", "(", "name", ")", ":", "ret", "=", "\"\"", ".", "join", "(", "x", ".", "title", "(", ")", "for", "x", "in", "name", ".", "split", "(", "\"_\"", ")", ")", "ret", "=", "ret", "[", "0", "]", ".", "lower", "(", ")", "+", ...
Takes a snake_field_name and returns a camelCaseFieldName Args: name (str): E.g. snake_field_name or SNAKE_FIELD_NAME Returns: str: camelCase converted name. E.g. capsFieldName
[ "Takes", "a", "snake_field_name", "and", "returns", "a", "camelCaseFieldName" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/core/serializable.py#L70-L81
16,890
dls-controls/pymalcolm
malcolm/core/serializable.py
Serializable.from_dict
def from_dict(cls, d, ignore=()): """Create an instance from a serialized version of cls Args: d(dict): Endpoints of cls to set ignore(tuple): Keys to ignore Returns: Instance of this class """ filtered = {} for k, v in d.items(): if k == "typeid": assert v == cls.typeid, \ "Dict has typeid %s but %s has typeid %s" % \ (v, cls, cls.typeid) elif k not in ignore: filtered[k] = v try: inst = cls(**filtered) except TypeError as e: raise TypeError("%s raised error: %s" % (cls.typeid, str(e))) return inst
python
def from_dict(cls, d, ignore=()): filtered = {} for k, v in d.items(): if k == "typeid": assert v == cls.typeid, \ "Dict has typeid %s but %s has typeid %s" % \ (v, cls, cls.typeid) elif k not in ignore: filtered[k] = v try: inst = cls(**filtered) except TypeError as e: raise TypeError("%s raised error: %s" % (cls.typeid, str(e))) return inst
[ "def", "from_dict", "(", "cls", ",", "d", ",", "ignore", "=", "(", ")", ")", ":", "filtered", "=", "{", "}", "for", "k", ",", "v", "in", "d", ".", "items", "(", ")", ":", "if", "k", "==", "\"typeid\"", ":", "assert", "v", "==", "cls", ".", ...
Create an instance from a serialized version of cls Args: d(dict): Endpoints of cls to set ignore(tuple): Keys to ignore Returns: Instance of this class
[ "Create", "an", "instance", "from", "a", "serialized", "version", "of", "cls" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/core/serializable.py#L162-L184
16,891
dls-controls/pymalcolm
malcolm/core/serializable.py
Serializable.lookup_subclass
def lookup_subclass(cls, d): """Look up a class based on a serialized dictionary containing a typeid Args: d (dict): Dictionary with key "typeid" Returns: Serializable subclass """ try: typeid = d["typeid"] except KeyError: raise FieldError("typeid not present in keys %s" % list(d)) subclass = cls._subcls_lookup.get(typeid, None) if not subclass: raise FieldError("'%s' not a valid typeid" % typeid) else: return subclass
python
def lookup_subclass(cls, d): try: typeid = d["typeid"] except KeyError: raise FieldError("typeid not present in keys %s" % list(d)) subclass = cls._subcls_lookup.get(typeid, None) if not subclass: raise FieldError("'%s' not a valid typeid" % typeid) else: return subclass
[ "def", "lookup_subclass", "(", "cls", ",", "d", ")", ":", "try", ":", "typeid", "=", "d", "[", "\"typeid\"", "]", "except", "KeyError", ":", "raise", "FieldError", "(", "\"typeid not present in keys %s\"", "%", "list", "(", "d", ")", ")", "subclass", "=", ...
Look up a class based on a serialized dictionary containing a typeid Args: d (dict): Dictionary with key "typeid" Returns: Serializable subclass
[ "Look", "up", "a", "class", "based", "on", "a", "serialized", "dictionary", "containing", "a", "typeid" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/core/serializable.py#L200-L218
16,892
dls-controls/pymalcolm
malcolm/core/process.py
Process.start
def start(self, timeout=None): """Start the process going Args: timeout (float): Maximum amount of time to wait for each spawned process. None means forever """ assert self.state == STOPPED, "Process already started" self.state = STARTING should_publish = self._start_controllers( self._controllers.values(), timeout) if should_publish: self._publish_controllers(timeout) self.state = STARTED
python
def start(self, timeout=None): assert self.state == STOPPED, "Process already started" self.state = STARTING should_publish = self._start_controllers( self._controllers.values(), timeout) if should_publish: self._publish_controllers(timeout) self.state = STARTED
[ "def", "start", "(", "self", ",", "timeout", "=", "None", ")", ":", "assert", "self", ".", "state", "==", "STOPPED", ",", "\"Process already started\"", "self", ".", "state", "=", "STARTING", "should_publish", "=", "self", ".", "_start_controllers", "(", "se...
Start the process going Args: timeout (float): Maximum amount of time to wait for each spawned process. None means forever
[ "Start", "the", "process", "going" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/core/process.py#L78-L91
16,893
dls-controls/pymalcolm
malcolm/core/process.py
Process.stop
def stop(self, timeout=None): """Stop the process and wait for it to finish Args: timeout (float): Maximum amount of time to wait for each spawned object. None means forever """ assert self.state == STARTED, "Process not started" self.state = STOPPING # Allow every controller a chance to clean up self._run_hook(ProcessStopHook, timeout=timeout) for s in self._spawned: if not s.ready(): self.log.debug( "Waiting for %s *%s **%s", s._function, s._args, s._kwargs) s.wait(timeout=timeout) self._spawned = [] self._controllers = OrderedDict() self._unpublished = set() self.state = STOPPED self.log.debug("Done process.stop()")
python
def stop(self, timeout=None): assert self.state == STARTED, "Process not started" self.state = STOPPING # Allow every controller a chance to clean up self._run_hook(ProcessStopHook, timeout=timeout) for s in self._spawned: if not s.ready(): self.log.debug( "Waiting for %s *%s **%s", s._function, s._args, s._kwargs) s.wait(timeout=timeout) self._spawned = [] self._controllers = OrderedDict() self._unpublished = set() self.state = STOPPED self.log.debug("Done process.stop()")
[ "def", "stop", "(", "self", ",", "timeout", "=", "None", ")", ":", "assert", "self", ".", "state", "==", "STARTED", ",", "\"Process not started\"", "self", ".", "state", "=", "STOPPING", "# Allow every controller a chance to clean up", "self", ".", "_run_hook", ...
Stop the process and wait for it to finish Args: timeout (float): Maximum amount of time to wait for each spawned object. None means forever
[ "Stop", "the", "process", "and", "wait", "for", "it", "to", "finish" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/core/process.py#L130-L150
16,894
dls-controls/pymalcolm
malcolm/core/process.py
Process.spawn
def spawn(self, function, *args, **kwargs): # type: (Callable[..., Any], *Any, **Any) -> Spawned """Runs the function in a worker thread, returning a Result object Args: function: Function to run args: Positional arguments to run the function with kwargs: Keyword arguments to run the function with Returns: Spawned: Something you can call wait(timeout) on to see when it's finished executing """ assert self.state != STOPPED, "Can't spawn when process stopped" spawned = Spawned(function, args, kwargs) self._spawned.append(spawned) self._spawn_count += 1 # Filter out things that are ready to avoid memory leaks if self._spawn_count > SPAWN_CLEAR_COUNT: self._clear_spawn_list() return spawned
python
def spawn(self, function, *args, **kwargs): # type: (Callable[..., Any], *Any, **Any) -> Spawned assert self.state != STOPPED, "Can't spawn when process stopped" spawned = Spawned(function, args, kwargs) self._spawned.append(spawned) self._spawn_count += 1 # Filter out things that are ready to avoid memory leaks if self._spawn_count > SPAWN_CLEAR_COUNT: self._clear_spawn_list() return spawned
[ "def", "spawn", "(", "self", ",", "function", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "# type: (Callable[..., Any], *Any, **Any) -> Spawned", "assert", "self", ".", "state", "!=", "STOPPED", ",", "\"Can't spawn when process stopped\"", "spawned", "=", ...
Runs the function in a worker thread, returning a Result object Args: function: Function to run args: Positional arguments to run the function with kwargs: Keyword arguments to run the function with Returns: Spawned: Something you can call wait(timeout) on to see when it's finished executing
[ "Runs", "the", "function", "in", "a", "worker", "thread", "returning", "a", "Result", "object" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/core/process.py#L152-L172
16,895
dls-controls/pymalcolm
malcolm/core/process.py
Process.add_controller
def add_controller(self, controller, timeout=None): # type: (Controller, float) -> None """Add a controller to be hosted by this process Args: controller (Controller): Its controller timeout (float): Maximum amount of time to wait for each spawned object. None means forever """ assert controller.mri not in self._controllers, \ "Controller already exists for %s" % controller.mri self._controllers[controller.mri] = controller controller.setup(self) if self.state: should_publish = self._start_controllers([controller], timeout) if self.state == STARTED and should_publish: self._publish_controllers(timeout)
python
def add_controller(self, controller, timeout=None): # type: (Controller, float) -> None assert controller.mri not in self._controllers, \ "Controller already exists for %s" % controller.mri self._controllers[controller.mri] = controller controller.setup(self) if self.state: should_publish = self._start_controllers([controller], timeout) if self.state == STARTED and should_publish: self._publish_controllers(timeout)
[ "def", "add_controller", "(", "self", ",", "controller", ",", "timeout", "=", "None", ")", ":", "# type: (Controller, float) -> None", "assert", "controller", ".", "mri", "not", "in", "self", ".", "_controllers", ",", "\"Controller already exists for %s\"", "%", "co...
Add a controller to be hosted by this process Args: controller (Controller): Its controller timeout (float): Maximum amount of time to wait for each spawned object. None means forever
[ "Add", "a", "controller", "to", "be", "hosted", "by", "this", "process" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/core/process.py#L179-L195
16,896
dls-controls/pymalcolm
malcolm/core/process.py
Process.block_view
def block_view(self, mri): # type: (str) -> Block """Get a Block view from a Controller with given mri""" controller = self.get_controller(mri) block = controller.block_view() return block
python
def block_view(self, mri): # type: (str) -> Block controller = self.get_controller(mri) block = controller.block_view() return block
[ "def", "block_view", "(", "self", ",", "mri", ")", ":", "# type: (str) -> Block", "controller", "=", "self", ".", "get_controller", "(", "mri", ")", "block", "=", "controller", ".", "block_view", "(", ")", "return", "block" ]
Get a Block view from a Controller with given mri
[ "Get", "a", "Block", "view", "from", "a", "Controller", "with", "given", "mri" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/core/process.py#L210-L215
16,897
dls-controls/pymalcolm
malcolm/modules/builtin/controllers/basiccontroller.py
BasicController.update_title
def update_title(self, _, info): # type: (object, TitleInfo) -> None """Set the label of the Block Meta object""" with self._lock: self._block.meta.set_label(info.title)
python
def update_title(self, _, info): # type: (object, TitleInfo) -> None with self._lock: self._block.meta.set_label(info.title)
[ "def", "update_title", "(", "self", ",", "_", ",", "info", ")", ":", "# type: (object, TitleInfo) -> None", "with", "self", ".", "_lock", ":", "self", ".", "_block", ".", "meta", ".", "set_label", "(", "info", ".", "title", ")" ]
Set the label of the Block Meta object
[ "Set", "the", "label", "of", "the", "Block", "Meta", "object" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/modules/builtin/controllers/basiccontroller.py#L18-L22
16,898
dls-controls/pymalcolm
malcolm/modules/builtin/controllers/basiccontroller.py
BasicController.update_health
def update_health(self, reporter, info): # type: (object, HealthInfo) -> None """Set the health attribute. Called from part""" with self.changes_squashed: alarm = info.alarm if alarm.is_ok(): self._faults.pop(reporter, None) else: self._faults[reporter] = alarm if self._faults: # Sort them by severity faults = sorted(self._faults.values(), key=lambda a: a.severity.value) alarm = faults[-1] text = faults[-1].message else: alarm = None text = "OK" self.health.set_value(text, alarm=alarm)
python
def update_health(self, reporter, info): # type: (object, HealthInfo) -> None with self.changes_squashed: alarm = info.alarm if alarm.is_ok(): self._faults.pop(reporter, None) else: self._faults[reporter] = alarm if self._faults: # Sort them by severity faults = sorted(self._faults.values(), key=lambda a: a.severity.value) alarm = faults[-1] text = faults[-1].message else: alarm = None text = "OK" self.health.set_value(text, alarm=alarm)
[ "def", "update_health", "(", "self", ",", "reporter", ",", "info", ")", ":", "# type: (object, HealthInfo) -> None", "with", "self", ".", "changes_squashed", ":", "alarm", "=", "info", ".", "alarm", "if", "alarm", ".", "is_ok", "(", ")", ":", "self", ".", ...
Set the health attribute. Called from part
[ "Set", "the", "health", "attribute", ".", "Called", "from", "part" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/modules/builtin/controllers/basiccontroller.py#L24-L42
16,899
dls-controls/pymalcolm
malcolm/modules/pandablocks/parts/pandablocksmaker.py
PandABlocksMaker.make_parts_for
def make_parts_for(self, field_name, field_data): """Create the relevant parts for this field Args: field_name (str): Short field name, e.g. VAL field_data (FieldData): Field data object """ typ = field_data.field_type subtyp = field_data.field_subtype if typ in ("read", "xadc"): writeable = False else: writeable = True if typ == "time" or typ in ("param", "read") and subtyp == "time": self._make_time_parts(field_name, field_data, writeable) elif typ == "write" and subtyp == "action": self._make_action_part(field_name, field_data) elif typ in ("param", "read", "write", "xadc"): self._make_param_part(field_name, field_data, writeable) elif typ == "bit_out": self._make_out(field_name, field_data, "bit") elif typ == "pos_out": self._make_out(field_name, field_data, "pos") self._make_scale_offset(field_name) self._make_out_capture(field_name, field_data) elif typ == "ext_out": self._make_out_capture(field_name, field_data) elif typ == "bit_mux": self._make_mux(field_name, field_data, "bit") self._make_mux_delay(field_name) elif typ == "pos_mux": self._make_mux(field_name, field_data, "pos") elif typ == "table": self._make_table(field_name, field_data) else: raise ValueError("Unknown type %r subtype %r" % (typ, subtyp))
python
def make_parts_for(self, field_name, field_data): typ = field_data.field_type subtyp = field_data.field_subtype if typ in ("read", "xadc"): writeable = False else: writeable = True if typ == "time" or typ in ("param", "read") and subtyp == "time": self._make_time_parts(field_name, field_data, writeable) elif typ == "write" and subtyp == "action": self._make_action_part(field_name, field_data) elif typ in ("param", "read", "write", "xadc"): self._make_param_part(field_name, field_data, writeable) elif typ == "bit_out": self._make_out(field_name, field_data, "bit") elif typ == "pos_out": self._make_out(field_name, field_data, "pos") self._make_scale_offset(field_name) self._make_out_capture(field_name, field_data) elif typ == "ext_out": self._make_out_capture(field_name, field_data) elif typ == "bit_mux": self._make_mux(field_name, field_data, "bit") self._make_mux_delay(field_name) elif typ == "pos_mux": self._make_mux(field_name, field_data, "pos") elif typ == "table": self._make_table(field_name, field_data) else: raise ValueError("Unknown type %r subtype %r" % (typ, subtyp))
[ "def", "make_parts_for", "(", "self", ",", "field_name", ",", "field_data", ")", ":", "typ", "=", "field_data", ".", "field_type", "subtyp", "=", "field_data", ".", "field_subtype", "if", "typ", "in", "(", "\"read\"", ",", "\"xadc\"", ")", ":", "writeable", ...
Create the relevant parts for this field Args: field_name (str): Short field name, e.g. VAL field_data (FieldData): Field data object
[ "Create", "the", "relevant", "parts", "for", "this", "field" ]
80ea667e4da26365a6cebc0249f52fdc744bd983
https://github.com/dls-controls/pymalcolm/blob/80ea667e4da26365a6cebc0249f52fdc744bd983/malcolm/modules/pandablocks/parts/pandablocksmaker.py#L49-L86