<commit_before>#!/usr/bin/env python3.1
# -*- coding: utf-8 -*-
#
# Copyright © 2009 Michael Lenzen <m.lenzen@gmail.com>
#
""" multiset - Also known as a bag or unordered tuple.

This module provides three classes:
	basemultiset - The superclass of multiset and frozen multiset.  It is both immutable
		and unhashable.
	multiset - A mutable (unhashable) multiset.
	frozenmultiset - A hashable (immutable) multiset.
"""

import heapq
from collections import MutableSet, Set, Hashable
from operator import itemgetter

class basemultiset(Set):
	""" Base class for multiset and frozenmultiset.	Is not mutable and not hashable, so there's
	no reason to use this instead of either multiset or frozenmultiset.
	"""
	## Basic object methods

	def __init__(self, iterable=None):
		""" Create a new basemultiset.  If iterable isn't given, is None or is empty then the
		set starts empty.  If iterable is a map, then it is assumed to be a map from elements
		to the number of times they should appear in the multiset.  Otherwise each element
		from iterable will be added to the multiset however many times it appears.

		>>> basemultiset()                     # create empty set
		basemultiset()
		>>> basemultiset('abracadabra')        # create from an Iterable
		basemultiset(('a', 'a', 'a', 'a', 'a', 'r', 'r', 'b', 'b', 'c', 'd'))
		"""
		self.__dict = dict()
		self.__size = 0
		if iterable:
			self += iterable

	def __repr__(self):
		""" The string representation is a call to the constructor given a tuple
		containing all of the elements.

		>>> ms = basemultiset()

		>>> ms == eval(ms.__repr__())
		True
		>>> ms = basemultiset('abracadabra')

		>>> ms == eval(ms.__repr__())
		True
		"""
		if self.__size == 0:
			return '{}()'.format(self.__class__.__name__)
		else:
			format = '{class_name}({tuple!r})'
			return format.format(class_name=self.__class__.__name__, tuple=tuple(self))

	def __str__(self):
		""" The printable string appears just like a set, except that each element
		is raised to the power of the multiplicity if it is greater than 1.

		>>> print(basemultiset())
		{}
		>>> print(basemultiset('abracadabra'))
		{'a'^5, 'r'^2, 'b'^2, 'c', 'd'}
		>>> basemultiset('abc').__str__() == set('abc').__str__()
		True
		"""
		if self.__size == 0:
			return '{}'
		else:
			format_single = '{elem!r}'
			format_mult = '{elem!r}^{mult}'
			strings = []
			for elem, mult in self.__dict.items():
				if mult > 1:
					strings.append(format_mult.format(elem=elem, mult=mult))
				else:
					strings.append(format_single.format(elem=elem))
			strings = tuple(strings)
			string = '{first}'.format(first=strings[0])
			for i in range(1,len(strings)):
				string = '{prev}, {next}'.format(prev=string, next=strings[i])
			string = '{{{}}}'.format(string)
			return string

	## Internal methods

	def __inc(self, elem, count=1):
		""" Increment the multiplicity of elem by count (if count <0 then decrement). """
		old_count = self.multiplicity(elem)
		new_count = max(0, old_count + count)
		if new_count == 0:
			try:
				del self.__dict[elem]
			except KeyError:
				pass
		else:
			self.__dict[elem] = new_count
		self.__size += new_count - old_count

	## New public methods (not overriding/implementing anything)

	def num_unique_elements(self):
		""" Returns the number of unique elements. """
		return len(self.__dict)

	def unique_elements(self):
		""" Returns a view of unique elements in this multiset. """
		return self.__dict.keys()

	def multiplicity(self, elem):
		""" Return the multiplicity of elem.  If elem is not in the set no Error is
		raised, instead 0 is returned.

		>>> ms = basemultiset('abracadabra')

		>>> ms.multiplicity('a')
		5
		>>> ms.multiplicity('x')
		0
		"""
		try:
			return self.__dict[elem]
		except KeyError:
			return 0

	def nlargest(self, n=None):
		""" List the n most common elements and their counts from the most
		common to the least.  If n is None, the list all element counts.

		>>> basemultiset('abracadabra').most_common()
		[('a', 5), ('r', 2), ('b', 2), ('c', 1), ('d', 1)]
		>>> basemultiset('abracadabra').most_common(2)
		[('a', 5), ('r', 2)]
		"""
		if n is None:
			return sorted(self.__dict.items(), key=itemgetter(1), reverse=True)
		else:
			return heapq.nlargest(n, self.__dict.items(), key=itemgetter(1))

	@classmethod
	def _from_map(cls, map):
		""" Creates a multiset from a dict of elem->count.  Each key in the dict
		is added if the value is > 0.

		>>> basemultiset._from_map({'a': 1, 'b': 2})
		basemultiset(('a', 'b', 'b'))
		"""
		out = cls()
		for elem, count in map.items():
			out.__inc(elem, count)
		return out

	def copy(self):
		""" Create a shallow copy of self.

		>>> basemultiset().copy() == basemultiset()
		True
		>>> abc = basemultiset('abc')

		>>> abc.copy() == abc
		True
		"""
		return self._from_map(self.__dict)

	## Alias methods - these methods are just names for other operations

	def cardinality(self):
		""" The total number of elements, including repeated memberships. """
		return len(self)

	def underlying_set(self):
		return unique_elements()

	def cartesian_product(self, other):
		return self * other

	def join(self, other):
		return self + other

	def sum(self, other):
		return self + other

	def difference(self, other):
		return self - other

	def symmetric_difference(self, other):
		return self ^ other

	def xor(self, other):
		return self ^ other

	def intersect(self, other):
		return self & other

	def union(self, other):
		return self | other

	## implementing Sized (inherited from Set) methods

	def __len__(self):
		""" Returns the cardinality of the multiset.

		>>> len(basemultiset())
		0
		>>> len(basemultiset('abc'))
		3
		>>> len(basemultiset('aaba'))
		4
		"""
		return self.__size

	## implementing Container (inherited from Set) methods

	def __contains__(self, elem):
		""" Returns the multiplicity of the element.

		>>> 'a' in basemultiset('bbac')
		True
		>>> 'a' in basemultiset()
		False
		>>> 'a' in basemultiset('missing letter')
		False
		"""
		return self.multiplicity(elem)

	## implementing Iterable (inherited from Set) methods

	def __iter__(self):
		""" Iterate through all elements, multiple copies will be returned if they exist. """
		for elem, count in self.__dict.items():
			for i in range(count):
				yield(elem)

	## implementing/overriding Set methods
	##
	## Also included here is __add__ even though its not overriding a Set method.
	## I figured it would be better to keep it with the other set operations.

	def __le__(self, other):
		"""
		TODO write test cases for __le__
		"""
		if not isinstance(other, basemultiset):
			if not isinstance(other, Iterable):
				return NotImplemented
			other = self._from_iterable(other)
		if len(self) > len(other):
			return False
		for elem in self.unique_elements():
			if self.multiplicity(elem) > other.multiplicity(elem):
				return False
		return True

	def __and__(self, other):
		""" Intersection is the minimum of corresponding counts. """
		if not isinstance(other, basemultiset):
			if not isinstance(other, Iterable):
				return NotImplemented
			other = self._from_iterable(other)
		values = dict()
		for elem in self.__dict:
			values[elem] = min(other.multiplicity(elem), self.multiplicity(elem))
		return self._from_map(values)

	def isdisjoint(self, other):
		"""  """
		if not isinstance(other, basemultiset):
			if not isinstance(other, Iterable):
				return NotImplemented
			other = self._from_iterable(other)
		return super.isdisjoint(self.unique_elements(), other.unique_elements())

	def __or__(self, other):
		""" Union is the maximum of all elements. """
		if not isinstance(other, basemultiset):
			if not isinstance(other, Iterable):
				return NotImplemented
			other = self._from_iterable(other)
		values = dict()
		for elem in self.unique_elements() | other.unique_elements():
			values[elem] = max(self.multiplicity(elem), other.multiplicity(elem))
		return self._from_map(values)

	def __add__(self, other):
		""" Sum of sets
		other can be any iterable.
		self + other = self & other + self | other """
		if not isinstance(other, Iterable):
			return NotImplemented
		out = self.copy()
		for elem in other:
			out.__inc(elem)
		return out

	def __sub__(self, other):
		""" Difference between the sets.
		other can be any iterable.
		For normal sets this is all s.t. x in self and x not in other.
		For multisets this is multiplicity(x) = max(0, self.multiplicity(x)-other.multiplicity(x))
		"""
		if not isinstance(other, Iterable):
			return NotImplemented
		out = self.copy()
		for elem in other:
			out.__inc(elem, -1)
		return out

	def __mul__(self, other):
		""" Cartesian product of the two sets.
		other can be any iterable.
		Both self and other must contain elements that can be added together.
		This should run in O(m*n+l) where:
			m is the number of unique elements in self
			n is the number of unique elements in other
			if other is a multiset:
				l is 0
			else:
				l is the len(other)
		The +l will only really matter when other is an iterable with MANY repeated elements
		For example: {'a'^2} * 'bbbbbbbbbbbbbbbbbbbbbbbbbb'
		The algorithm will be dominated by counting the 'b's
		"""
		if not isinstance(other, basemultiset):
			if not isinstance(other, Iterable):
				return NotImplemented
			other = self._from_iterable(other)
		values = dict()
		for elem, count in self.__dict:
			for other_elem, other_count in other.__dict:
				new_elem = elem + other_elem
				new_count = count * other_count
				values[new_elem] = new_count
		return _from_map(values)

	def __xor__(self, other):
		""" Symmetric difference between the sets.
		other can be any iterable.
		"""
		return (self - other) | (other - self)

class multiset(basemultiset, MutableSet):
	""" multiset is a Mutable basemultiset, thus not hashable and unusable for dict keys or in
	other sets.

	>>>
	"""
	def add(self, elem):
		self.__inc(elem, 1)

	def discard(self, elem):
		self.__inc(elem, -1)

	def clear(self):
		self.__dict = dict()
		self.__size = 0

class frozenmultiset(basemultiset, Hashable):
	""" frozenmultiset is a Hashable basemultiset, thus it is immutable and usable for dict keys
	"""
	def __hash__(self):
		""" Use the hash funtion inherited from somewhere.  For now this is from Set,
		I'm not sure that it works for collections with multiple elements.
		"""
		return self._hash()
<commit_msg>Some more unit tests and marked TODOs for multiset.<commit_after>#!/usr/bin/env python3.1
# -*- coding: utf-8 -*-
#
# Copyright © 2009 Michael Lenzen <m.lenzen@gmail.com>
#
""" multiset - Also known as a bag or unordered tuple.

This module provides three classes:
	basemultiset - The superclass of multiset and frozen multiset.  It is both immutable
		and unhashable.
	multiset - A mutable (unhashable) multiset.
	frozenmultiset - A hashable (immutable) multiset.
"""

import heapq
from collections import MutableSet, Set, Hashable, Iterable
from operator import itemgetter

class basemultiset(Set):
	""" Base class for multiset and frozenmultiset.	Is not mutable and not hashable, so there's
	no reason to use this instead of either multiset or frozenmultiset.
	"""
	## Basic object methods

	def __init__(self, iterable=None):
		""" Create a new basemultiset.  If iterable isn't given, is None or is empty then the
		set starts empty.  If iterable is a map, then it is assumed to be a map from elements
		to the number of times they should appear in the multiset.  Otherwise each element
		from iterable will be added to the multiset however many times it appears.

		>>> basemultiset()                     # create empty set
		basemultiset()
		>>> basemultiset('abracadabra')        # create from an Iterable
		basemultiset(('a', 'a', 'a', 'a', 'a', 'r', 'r', 'b', 'b', 'c', 'd'))
		"""
		self.__dict = dict()
		self.__size = 0
		if iterable:
			for elem in iterable:
				self.__inc(elem)

	def __repr__(self):
		""" The string representation is a call to the constructor given a tuple
		containing all of the elements.

		>>> ms = basemultiset()

		>>> ms == eval(ms.__repr__())
		True
		>>> ms = basemultiset('abracadabra')

		>>> ms == eval(ms.__repr__())
		True
		"""
		if self.__size == 0:
			return '{}()'.format(self.__class__.__name__)
		else:
			format = '{class_name}({tuple!r})'
			return format.format(class_name=self.__class__.__name__, tuple=tuple(self))

	def __str__(self):
		""" The printable string appears just like a set, except that each element
		is raised to the power of the multiplicity if it is greater than 1.

		>>> print(basemultiset())
		{}
		>>> print(basemultiset('abracadabra'))
		{'a'^5, 'r'^2, 'b'^2, 'c', 'd'}
		>>> basemultiset('abc').__str__() == set('abc').__str__()
		True
		"""
		if self.__size == 0:
			return '{}'
		else:
			format_single = '{elem!r}'
			format_mult = '{elem!r}^{mult}'
			strings = []
			for elem, mult in self.__dict.items():
				if mult > 1:
					strings.append(format_mult.format(elem=elem, mult=mult))
				else:
					strings.append(format_single.format(elem=elem))
			strings = tuple(strings)
			string = '{first}'.format(first=strings[0])
			for i in range(1,len(strings)):
				string = '{prev}, {next}'.format(prev=string, next=strings[i])
			string = '{{{}}}'.format(string)
			return string

	## Internal methods

	def __inc(self, elem, count=1):
		""" Increment the multiplicity of elem by count (if count <0 then decrement). """
		old_count = self.multiplicity(elem)
		new_count = max(0, old_count + count)
		if new_count == 0:
			try:
				del self.__dict[elem]
			except KeyError:
				pass
		else:
			self.__dict[elem] = new_count
		self.__size += new_count - old_count

	## New public methods (not overriding/implementing anything)

	def num_unique_elements(self):
		""" Returns the number of unique elements. """
		return len(self.__dict)

	def unique_elements(self):
		""" Returns a view of unique elements in this multiset. """
		return self.__dict.keys()

	def multiplicity(self, elem):
		""" Return the multiplicity of elem.  If elem is not in the set no Error is
		raised, instead 0 is returned.

		>>> ms = basemultiset('abracadabra')

		>>> ms.multiplicity('a')
		5
		>>> ms.multiplicity('x')
		0
		"""
		try:
			return self.__dict[elem]
		except KeyError:
			return 0

	def nlargest(self, n=None):
		""" List the n most common elements and their counts from the most
		common to the least.  If n is None, the list all element counts.

		>>> basemultiset('abracadabra').nlargest()
		[('a', 5), ('r', 2), ('b', 2), ('c', 1), ('d', 1)]
		>>> basemultiset('abracadabra').nlargest(2)
		[('a', 5), ('r', 2)]
		"""
		if n is None:
			return sorted(self.__dict.items(), key=itemgetter(1), reverse=True)
		else:
			return heapq.nlargest(n, self.__dict.items(), key=itemgetter(1))

	@classmethod
	def _from_map(cls, map):
		""" Creates a multiset from a dict of elem->count.  Each key in the dict
		is added if the value is > 0.

		>>> basemultiset._from_map({'a': 1, 'b': 2})
		basemultiset(('a', 'b', 'b'))
		"""
		out = cls()
		for elem, count in map.items():
			out.__inc(elem, count)
		return out

	def copy(self):
		""" Create a shallow copy of self.

		>>> basemultiset().copy() == basemultiset()
		True
		>>> abc = basemultiset('abc')

		>>> abc.copy() == abc
		True
		"""
		return self._from_map(self.__dict)

	## Alias methods - these methods are just names for other operations

	def cardinality(self):
		""" The total number of elements, including repeated memberships. """
		return len(self)

	def underlying_set(self):
		return unique_elements()

	def cartesian_product(self, other):
		return self * other

	def join(self, other):
		return self + other

	def sum(self, other):
		return self + other

	def difference(self, other):
		return self - other

	def symmetric_difference(self, other):
		return self ^ other

	def xor(self, other):
		return self ^ other

	def intersect(self, other):
		return self & other

	def union(self, other):
		return self | other

	## implementing Sized (inherited from Set) methods

	def __len__(self):
		""" Returns the cardinality of the multiset.

		>>> len(basemultiset())
		0
		>>> len(basemultiset('abc'))
		3
		>>> len(basemultiset('aaba'))
		4
		"""
		return self.__size

	## implementing Container (inherited from Set) methods

	def __contains__(self, elem):
		""" Returns the multiplicity of the element.

		>>> 'a' in basemultiset('bbac')
		True
		>>> 'a' in basemultiset()
		False
		>>> 'a' in basemultiset('missing letter')
		False
		"""
		return self.multiplicity(elem)

	## implementing Iterable (inherited from Set) methods

	def __iter__(self):
		""" Iterate through all elements, multiple copies will be returned if they exist. """
		for elem, count in self.__dict.items():
			for i in range(count):
				yield(elem)

	## implementing/overriding Set methods
	##
	## Also included here is __add__ even though its not overriding a Set method.
	## I figured it would be better to keep it with the other set operations.

	def __le__(self, other):
		"""

		TODO write test cases for __le__
		"""
		if not isinstance(other, basemultiset):
			if not isinstance(other, Iterable):
				return NotImplemented
			other = self._from_iterable(other)
		if len(self) > len(other):
			return False
		for elem in self.unique_elements():
			if self.multiplicity(elem) > other.multiplicity(elem):
				return False
		return True

	def __and__(self, other):
		""" Intersection is the minimum of corresponding counts.

		TODO write unit tests for and
		"""
		if not isinstance(other, basemultiset):
			if not isinstance(other, Iterable):
				return NotImplemented
			other = self._from_iterable(other)
		values = dict()
		for elem in self.__dict:
			values[elem] = min(other.multiplicity(elem), self.multiplicity(elem))
		return self._from_map(values)

	def isdisjoint(self, other):
		if not isinstance(other, basemultiset):
			if not isinstance(other, Iterable):
				return NotImplemented
			other = self._from_iterable(other)
		return super.isdisjoint(self.unique_elements(), other.unique_elements())

	def __or__(self, other):
		""" Union is the maximum of all elements.

		TODO write unit tests for or
		"""
		if not isinstance(other, basemultiset):
			if not isinstance(other, Iterable):
				return NotImplemented
			other = self._from_iterable(other)
		values = dict()
		for elem in self.unique_elements() | other.unique_elements():
			values[elem] = max(self.multiplicity(elem), other.multiplicity(elem))
		return self._from_map(values)

	def __add__(self, other):
		""" Sum of sets
		other can be any iterable.
		self + other = self & other + self | other

		TODO write unit tests for add
		"""
		if not isinstance(other, Iterable):
			return NotImplemented
		out = self.copy()
		for elem in other:
			out.__inc(elem)
		return out

	def __sub__(self, other):
		""" Difference between the sets.
		other can be any iterable.
		For normal sets this is all s.t. x in self and x not in other.
		For multisets this is multiplicity(x) = max(0, self.multiplicity(x)-other.multiplicity(x))

		TODO write tests for sub
		"""
		if not isinstance(other, Iterable):
			return NotImplemented
		out = self.copy()
		for elem in other:
			out.__inc(elem, -1)
		return out

	def __mul__(self, other):
		""" Cartesian product of the two sets.
		other can be any iterable.
		Both self and other must contain elements that can be added together.
		This should run in O(m*n+l) where:
			m is the number of unique elements in self
			n is the number of unique elements in other
			if other is a multiset:
				l is 0
			else:
				l is the len(other)
		The +l will only really matter when other is an iterable with MANY repeated elements
		For example: {'a'^2} * 'bbbbbbbbbbbbbbbbbbbbbbbbbb'
		The algorithm will be dominated by counting the 'b's

		>>> ms = basemultiset('aab')

		>>> ms * set('a')
		basemultiset(('aa', 'aa', 'ba'))
		>>> ms * set()
		basemultiset()
		"""
		if not isinstance(other, basemultiset):
			if not isinstance(other, Iterable):
				return NotImplemented
			other = self._from_iterable(other)
		values = dict()
		for elem, count in self.__dict.items():
			for other_elem, other_count in other.__dict.items():
				new_elem = elem + other_elem
				new_count = count * other_count
				values[new_elem] = new_count
		return self._from_map(values)

	def __xor__(self, other):
		""" Symmetric difference between the sets.
		other can be any iterable.

		TODO write unit tests for xor
		"""
		return (self - other) | (other - self)

class multiset(basemultiset, MutableSet):
	""" multiset is a Mutable basemultiset, thus not hashable and unusable for dict keys or in
	other sets.

	TODO write multiset add, discard and clear unit tests
	"""
	def add(self, elem):
		self.__inc(elem, 1)

	def discard(self, elem):
		self.__inc(elem, -1)

	def clear(self):
		self.__dict = dict()
		self.__size = 0

class frozenmultiset(basemultiset, Hashable):
	""" frozenmultiset is a Hashable basemultiset, thus it is immutable and usable for dict keys
	"""
	def __hash__(self):
		""" Use the hash funtion inherited from somewhere.  For now this is from Set,
		I'm not sure that it works for collections with multiple elements.

		TODO write unit tests for hash
		"""
		return self._hash()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
    pygments.lexers.ring
    ~~~~~~~~~~~~~~~~~~~~~~

    Lexer for the Ring language.

    :copyright: Copyright 2016, Mahmoud Fayed <msfclipper@yahoo.com>
"""

import re

from pygments.lexer import RegexLexer, include, default
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
    Number, Punctuation, Error

__all__ = ['RingLexer']

class RingLexer(RegexLexer):
    """
    For `Ring <http://Ring-code.org/>`_ source code.

    .. versionadded:: 1.5
    """

    name = 'Ring'
    aliases = ['ring', 'ring']
    filenames = ['*.ring', '*.rh']
    mimetypes = ['text/x-ring']

    flags = re.MULTILINE | re.IGNORECASE | re.UNICODE

    def underscorize(words):
        newWords = []
        new = ""
        for word in words:
            for ch in word:
                new += (ch + "_?")
            newWords.append(new)
            new = ""
        return "|".join(newWords)

    keywords = [
		'again','and','but','bye','call','case','catch','class','def','do','done',
		'else','elseif','end','exit','for','from','func','get','give','if','import',
		'in','load','loop','new','next','not','off','ok','on','or','other','package'
		'private','put','return','see','step','switch','to','try','while','changeringkeyword',
		'changeringoperator','loadsyntax'
    ]

    keywordsPseudo = [
        'null', 'true', 'false'
    ]

    opWords = [
        'and', 'or', 'not'
    ]

    types = [
        'list', 'string', 'stack', 'queue', 'tree', 'hashtable'
	]

    tokens = {
        'root': [
            (r'##.*$', String.Doc),
            (r'#.*$', Comment),
            (r'[*=><+\-/@$~&%!?|\\\[\]]', Operator),
            (r'\.\.|\.|,|\[\.|\.\]|\{\.|\.\}|\(\.|\.\)|\{|\}|\(|\)|:|\^|`|;',
             Punctuation),

            # Strings
            (r'(?:[\w]+)"', String, 'rdqs'),
            (r'"""', String, 'tdqs'),
            ('"', String, 'dqs'),

            # Char
            ("'", String.Char, 'chars'),

            # Keywords
            (r'(%s)\b' % underscorize(opWords), Operator.Word),
            (r'(p_?r_?o_?c_?\s)(?![(\[\]])', Keyword, 'funcname'),
            (r'(%s)\b' % underscorize(keywords), Keyword),
            (r'(%s)\b' % underscorize(['from', 'import', 'include']),
             Keyword.Namespace),
            (r'(v_?a_?r)\b', Keyword.Declaration),
            (r'(%s)\b' % underscorize(types), Keyword.Type),
            (r'(%s)\b' % underscorize(keywordsPseudo), Keyword.Pseudo),
            # Identifiers
            (r'\b((?![_\d])\w)(((?!_)\w)|(_(?!_)\w))*', Name),
            # Numbers
            (r'[0-9][0-9_]*(?=([e.]|\'f(32|64)))',
             Number.Float, ('float-suffix', 'float-number')),
            (r'0x[a-f0-9][a-f0-9_]*', Number.Hex, 'int-suffix'),
            (r'0b[01][01_]*', Number.Bin, 'int-suffix'),
            (r'0o[0-7][0-7_]*', Number.Oct, 'int-suffix'),
            (r'[0-9][0-9_]*', Number.Integer, 'int-suffix'),
            # Whitespace
            (r'\s+', Text),
            (r'.+$', Error),
        ],
        'chars': [
            (r'\\([\\abcefnrtvl"\']|x[a-f0-9]{2}|[0-9]{1,3})', String.Escape),
            (r"'", String.Char, '#pop'),
            (r".", String.Char)
        ],
        'strings': [
            (r'(?<!\$)\$(\d+|#|\w+)+', String.Interpol),
            (r'[^\\\'"$\n]+', String),
            # quotes, dollars and backslashes must be parsed one at a time
            (r'[\'"\\]', String),
            # unhandled string formatting sign
            (r'\$', String)
            # newlines are an error (use "nl" state)
        ],
        'dqs': [
            (r'\\([\\abcefnrtvl"\']|\n|x[a-f0-9]{2}|[0-9]{1,3})',
             String.Escape),
            (r'"', String, '#pop'),
            include('strings')
        ],
        'rdqs': [
            (r'"(?!")', String, '#pop'),
            (r'""', String.Escape),
            include('strings')
        ],
        'tdqs': [
            (r'"""(?!")', String, '#pop'),
            include('strings'),
            include('nl')
        ],
        'funcname': [
            (r'((?![\d_])\w)(((?!_)\w)|(_(?!_)\w))*', Name.Function, '#pop'),
            (r'`.+`', Name.Function, '#pop')
        ],
        'nl': [
            (r'\n', String)
        ],
        'float-number': [
            (r'\.(?!\.)[0-9_]*', Number.Float),
            (r'e[+-]?[0-9][0-9_]*', Number.Float),
            default('#pop')
        ],
        'float-suffix': [
            (r'\'f(32|64)', Number.Float),
            default('#pop')
        ],
        'int-suffix': [
            (r'\'i(32|64)', Number.Integer.Long),
            (r'\'i(8|16)', Number.Integer),
            default('#pop')
        ],
    }
<commit_msg>Update Documentation - lexer - language keywords.<commit_after># -*- coding: utf-8 -*-
"""
    pygments.lexers.ring
    ~~~~~~~~~~~~~~~~~~~~~~

    Lexer for the Ring language.

    :copyright: Copyright 2016, Mahmoud Fayed <msfclipper@yahoo.com>
"""

import re

from pygments.lexer import RegexLexer, include, default
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
    Number, Punctuation, Error

__all__ = ['RingLexer']

class RingLexer(RegexLexer):
    """
    For `Ring <http://Ring-code.org/>`_ source code.

    .. versionadded:: 1.5
    """

    name = 'Ring'
    aliases = ['ring', 'ring']
    filenames = ['*.ring', '*.rh']
    mimetypes = ['text/x-ring']

    flags = re.MULTILINE | re.IGNORECASE | re.UNICODE

    def underscorize(words):
        newWords = []
        new = ""
        for word in words:
            for ch in word:
                new += (ch + "_?")
            newWords.append(new)
            new = ""
        return "|".join(newWords)

    keywords = [
		'again','and','but','bye','call','case','catch','class','def','do','done',
		'else','elseif','end','exit','for','from','func','get','give','if','import',
		'in','load','loop','new','next','not','off','ok','on','or','other','package'
		'private','put','return','see','step','switch','to','try','while','changeringkeyword',
		'changeringoperator','loadsyntax','endfunc','endclass','endpackage'
    ]

    keywordsPseudo = [
        'null', 'true', 'false'
    ]

    opWords = [
        'and', 'or', 'not'
    ]

    types = [
        'list', 'string', 'stack', 'queue', 'tree', 'hashtable'
	]

    tokens = {
        'root': [
            (r'##.*$', String.Doc),
            (r'#.*$', Comment),
            (r'[*=><+\-/@$~&%!?|\\\[\]]', Operator),
            (r'\.\.|\.|,|\[\.|\.\]|\{\.|\.\}|\(\.|\.\)|\{|\}|\(|\)|:|\^|`|;',
             Punctuation),

            # Strings
            (r'(?:[\w]+)"', String, 'rdqs'),
            (r'"""', String, 'tdqs'),
            ('"', String, 'dqs'),

            # Char
            ("'", String.Char, 'chars'),

            # Keywords
            (r'(%s)\b' % underscorize(opWords), Operator.Word),
            (r'(p_?r_?o_?c_?\s)(?![(\[\]])', Keyword, 'funcname'),
            (r'(%s)\b' % underscorize(keywords), Keyword),
            (r'(%s)\b' % underscorize(['from', 'import', 'include']),
             Keyword.Namespace),
            (r'(v_?a_?r)\b', Keyword.Declaration),
            (r'(%s)\b' % underscorize(types), Keyword.Type),
            (r'(%s)\b' % underscorize(keywordsPseudo), Keyword.Pseudo),
            # Identifiers
            (r'\b((?![_\d])\w)(((?!_)\w)|(_(?!_)\w))*', Name),
            # Numbers
            (r'[0-9][0-9_]*(?=([e.]|\'f(32|64)))',
             Number.Float, ('float-suffix', 'float-number')),
            (r'0x[a-f0-9][a-f0-9_]*', Number.Hex, 'int-suffix'),
            (r'0b[01][01_]*', Number.Bin, 'int-suffix'),
            (r'0o[0-7][0-7_]*', Number.Oct, 'int-suffix'),
            (r'[0-9][0-9_]*', Number.Integer, 'int-suffix'),
            # Whitespace
            (r'\s+', Text),
            (r'.+$', Error),
        ],
        'chars': [
            (r'\\([\\abcefnrtvl"\']|x[a-f0-9]{2}|[0-9]{1,3})', String.Escape),
            (r"'", String.Char, '#pop'),
            (r".", String.Char)
        ],
        'strings': [
            (r'(?<!\$)\$(\d+|#|\w+)+', String.Interpol),
            (r'[^\\\'"$\n]+', String),
            # quotes, dollars and backslashes must be parsed one at a time
            (r'[\'"\\]', String),
            # unhandled string formatting sign
            (r'\$', String)
            # newlines are an error (use "nl" state)
        ],
        'dqs': [
            (r'\\([\\abcefnrtvl"\']|\n|x[a-f0-9]{2}|[0-9]{1,3})',
             String.Escape),
            (r'"', String, '#pop'),
            include('strings')
        ],
        'rdqs': [
            (r'"(?!")', String, '#pop'),
            (r'""', String.Escape),
            include('strings')
        ],
        'tdqs': [
            (r'"""(?!")', String, '#pop'),
            include('strings'),
            include('nl')
        ],
        'funcname': [
            (r'((?![\d_])\w)(((?!_)\w)|(_(?!_)\w))*', Name.Function, '#pop'),
            (r'`.+`', Name.Function, '#pop')
        ],
        'nl': [
            (r'\n', String)
        ],
        'float-number': [
            (r'\.(?!\.)[0-9_]*', Number.Float),
            (r'e[+-]?[0-9][0-9_]*', Number.Float),
            default('#pop')
        ],
        'float-suffix': [
            (r'\'f(32|64)', Number.Float),
            default('#pop')
        ],
        'int-suffix': [
            (r'\'i(32|64)', Number.Integer.Long),
            (r'\'i(8|16)', Number.Integer),
            default('#pop')
        ],
    }
<|endoftext|>


<commit_before>#!/usr/bin/python
"""
takes templated file .xxx.src and produces .xxx file  where .xxx is
.i or .c or .h, using the following template rules

/**begin repeat  -- on a line by itself marks the start of a repeated code
                    segment
/**end repeat**/ -- on a line by itself marks it's end

After the /**begin repeat and before the */, all the named templates are placed
these should all have the same number of replacements

Repeat blocks can be nested, with each nested block labeled with its depth,
i.e.
/**begin repeat1
 *....
 */
/**end repeat1**/

In the main body each replace will use one entry from the list of named replacements

 Note that all #..# forms in a block must have the same number of
   comma-separated entries.

Example:

    An input file containing

        /**begin repeat
         * #a = 1,2,3#
         * #b = 1,2,3#
         */

        /**begin repeat1
         * #c = ted, jim#
         */
        @a@, @b@, @c@
        /**end repeat1**/

        /**end repeat**/

    produces

        line 1 "template.c.src"

        /*
         *********************************************************************
         **       This file was autogenerated from a template  DO NOT EDIT!!**
         **       Changes should be made to the original source (.src) file **
         *********************************************************************
         */

        #line 9
        1, 1, ted

        #line 9
        1, 1, jim

        #line 9
        2, 2, ted

        #line 9
        2, 2, jim

        #line 9
        3, 3, ted

        #line 9
        3, 3, jim

"""

__all__ = ['process_str', 'process_file']

import os
import sys
import re

# names for replacement that are already global.
global_names = {}

# header placed at the front of head processed file
header =\
"""
/*
 *****************************************************************************
 **       This file was autogenerated from a template  DO NOT EDIT!!!!      **
 **       Changes should be made to the original source (.src) file         **
 *****************************************************************************
 */

"""
# Parse string for repeat loops
def parse_structure(astr, level):
    """
    The returned line number is from the beginning of the string, starting
    at zero. Returns an empty list if no loops found.

    """
    if level == 0 :
        loopbeg = "/**begin repeat"
        loopend = "/**end repeat**/"
    else :
        loopbeg = "/**begin repeat%d" % level
        loopend = "/**end repeat%d**/" % level

    ind = 0
    line = 0
    spanlist = []
    while 1:
        start = astr.find(loopbeg, ind)
        if start == -1:
            break
        start2 = astr.find("*/",start)
        start2 = astr.find("\n",start2)
        fini1 = astr.find(loopend,start2)
        fini2 = astr.find("\n",fini1)
        line += astr.count("\n", ind, start2+1)
        spanlist.append((start, start2+1, fini1, fini2+1, line))
        line += astr.count("\n", start2+1, fini2)
        ind = fini2
    spanlist.sort()
    return spanlist

def paren_repl(obj):
    torep = obj.group(1)
    numrep = obj.group(2)
    return ','.join([torep]*int(numrep))

parenrep = re.compile(r"[(]([^)]*)[)]\*(\d+)")
plainrep = re.compile(r"([^*]+)\*(\d+)")
def parse_values(astr):
    # replaces all occurrences of '(a,b,c)*4' in astr
    # with 'a,b,c,a,b,c,a,b,c,a,b,c'. Empty braces generate
    # empty values, i.e., ()*4 yields ',,,'. The result is
    # split at ',' and a list of values returned.
    astr = parenrep.sub(paren_repl, astr)
    # replaces occurences of xxx*3 with xxx, xxx, xxx
    astr = ','.join([plainrep.sub(paren_repl,x.strip())
                     for x in astr.split(',')])
    return astr.split(',')

stripast = re.compile(r"\n\s*\*?")
named_re = re.compile(r"#\s*(\w*)\s*=([^#]*)#")
def parse_loop_header(loophead) :
    """Find all named replacements in the header

    Returns a list of dictionaries, one for each loop iteration,
    where each key is a name to be substituted and the corresponding
    value is the replacement string.

    """
    # Strip out '\n' and leading '*', if any, in continuation lines.
    # This should not effect code previous to this change as
    # continuation lines were not allowed.
    loophead = stripast.sub("", loophead)
    # parse out the names and lists of values
    names = []
    reps = named_re.findall(loophead)
    nsub = None
    for rep in reps:
        name = rep[0]
        vals = parse_values(rep[1])
        size = len(vals)
        if nsub is None :
            nsub = size
        elif nsub != size :
            msg = "Mismatch in number of values:\n%s = %s" % (name, vals)
            raise ValueError, msg
        names.append((name,vals))

    # generate list of dictionaries, one for each template iteration
    dlist = []
    if nsub is None :
        raise ValueError, "No substitution variables found"
    for i in range(nsub) :
        tmp = {}
        for name,vals in names :
            tmp[name] = vals[i]
        dlist.append(tmp)
    return dlist

replace_re = re.compile(r"@([\w]+)@")
def parse_string(astr, env, level, line) :
    lineno = "#line %d\n" % line

    # local function for string replacement, uses env
    def replace(match):
        name = match.group(1)
        try :
            val = env[name]
        except KeyError, e :
            msg = 'line %d: %s'%(line, e)
            raise ValueError, msg
        return val

    code = [lineno]
    struct = parse_structure(astr, level)
    if struct :
        # recurse over inner loops
        oldend = 0
        newlevel = level + 1
        for sub in struct:
            pref = astr[oldend:sub[0]]
            head = astr[sub[0]:sub[1]]
            text = astr[sub[1]:sub[2]]
            oldend = sub[3]
            newline = line + sub[4]
            code.append(replace_re.sub(replace, pref))
            try :
                envlist = parse_loop_header(head)
            except ValueError, e :
                msg = "line %d: %s" % (newline, e)
                raise ValueError, msg
            for newenv in envlist :
                newenv.update(env)
                newcode = parse_string(text, newenv, newlevel, newline)
                code.extend(newcode)
        suff = astr[oldend:]
        code.append(replace_re.sub(replace, suff))
    else :
        # replace keys
        code.append(replace_re.sub(replace, astr))
    code.append('\n')
    return ''.join(code)

def process_str(astr):
    code = [header]
    code.extend(parse_string(astr, global_names, 0, 1))
    return ''.join(code)

include_src_re = re.compile(r"(\n|\A)#include\s*['\"]"
                            r"(?P<name>[\w\d./\\]+[.]src)['\"]", re.I)

def resolve_includes(source):
    d = os.path.dirname(source)
    fid = open(source)
    lines = []
    for line in fid.readlines():
        m = include_src_re.match(line)
        if m:
            fn = m.group('name')
            if not os.path.isabs(fn):
                fn = os.path.join(d,fn)
            if os.path.isfile(fn):
                print 'Including file',fn
                lines.extend(resolve_includes(fn))
            else:
                lines.append(line)
        else:
            lines.append(line)
    fid.close()
    return lines

def process_file(source):
    lines = resolve_includes(source)
    sourcefile = os.path.normcase(source).replace("\\","\\\\")
    try:
        code = process_str(''.join(lines))
    except ValueError, e:
        raise ValueError, '"%s", %s' % (sourcefile, e)
    return '#line 1 "%s"\n%s' % (sourcefile, code)

def unique_key(adict):
    # this obtains a unique key given a dictionary
    # currently it works by appending together n of the letters of the
    #   current keys and increasing n until a unique key is found
    # -- not particularly quick
    allkeys = adict.keys()
    done = False
    n = 1
    while not done:
        newkey = "".join([x[:n] for x in allkeys])
        if newkey in allkeys:
            n += 1
        else:
            done = True
    return newkey

if __name__ == "__main__":

    try:
        file = sys.argv[1]
    except IndexError:
        fid = sys.stdin
        outfile = sys.stdout
    else:
        fid = open(file,'r')
        (base, ext) = os.path.splitext(file)
        newname = base
        outfile = open(newname,'w')

    allstr = fid.read()
    try:
        writestr = process_str(allstr)
    except ValueError, e:
        raise ValueError, "file %s, %s" % (file, e)
    outfile.write(writestr)
<commit_msg>Instantiate exceptions using string argument.<commit_after>#!/usr/bin/python
"""
takes templated file .xxx.src and produces .xxx file  where .xxx is
.i or .c or .h, using the following template rules

/**begin repeat  -- on a line by itself marks the start of a repeated code
                    segment
/**end repeat**/ -- on a line by itself marks it's end

After the /**begin repeat and before the */, all the named templates are placed
these should all have the same number of replacements

Repeat blocks can be nested, with each nested block labeled with its depth,
i.e.
/**begin repeat1
 *....
 */
/**end repeat1**/

In the main body each replace will use one entry from the list of named replacements

 Note that all #..# forms in a block must have the same number of
   comma-separated entries.

Example:

    An input file containing

        /**begin repeat
         * #a = 1,2,3#
         * #b = 1,2,3#
         */

        /**begin repeat1
         * #c = ted, jim#
         */
        @a@, @b@, @c@
        /**end repeat1**/

        /**end repeat**/

    produces

        line 1 "template.c.src"

        /*
         *********************************************************************
         **       This file was autogenerated from a template  DO NOT EDIT!!**
         **       Changes should be made to the original source (.src) file **
         *********************************************************************
         */

        #line 9
        1, 1, ted

        #line 9
        1, 1, jim

        #line 9
        2, 2, ted

        #line 9
        2, 2, jim

        #line 9
        3, 3, ted

        #line 9
        3, 3, jim

"""

__all__ = ['process_str', 'process_file']

import os
import sys
import re

# names for replacement that are already global.
global_names = {}

# header placed at the front of head processed file
header =\
"""
/*
 *****************************************************************************
 **       This file was autogenerated from a template  DO NOT EDIT!!!!      **
 **       Changes should be made to the original source (.src) file         **
 *****************************************************************************
 */

"""
# Parse string for repeat loops
def parse_structure(astr, level):
    """
    The returned line number is from the beginning of the string, starting
    at zero. Returns an empty list if no loops found.

    """
    if level == 0 :
        loopbeg = "/**begin repeat"
        loopend = "/**end repeat**/"
    else :
        loopbeg = "/**begin repeat%d" % level
        loopend = "/**end repeat%d**/" % level

    ind = 0
    line = 0
    spanlist = []
    while 1:
        start = astr.find(loopbeg, ind)
        if start == -1:
            break
        start2 = astr.find("*/",start)
        start2 = astr.find("\n",start2)
        fini1 = astr.find(loopend,start2)
        fini2 = astr.find("\n",fini1)
        line += astr.count("\n", ind, start2+1)
        spanlist.append((start, start2+1, fini1, fini2+1, line))
        line += astr.count("\n", start2+1, fini2)
        ind = fini2
    spanlist.sort()
    return spanlist

def paren_repl(obj):
    torep = obj.group(1)
    numrep = obj.group(2)
    return ','.join([torep]*int(numrep))

parenrep = re.compile(r"[(]([^)]*)[)]\*(\d+)")
plainrep = re.compile(r"([^*]+)\*(\d+)")
def parse_values(astr):
    # replaces all occurrences of '(a,b,c)*4' in astr
    # with 'a,b,c,a,b,c,a,b,c,a,b,c'. Empty braces generate
    # empty values, i.e., ()*4 yields ',,,'. The result is
    # split at ',' and a list of values returned.
    astr = parenrep.sub(paren_repl, astr)
    # replaces occurences of xxx*3 with xxx, xxx, xxx
    astr = ','.join([plainrep.sub(paren_repl,x.strip())
                     for x in astr.split(',')])
    return astr.split(',')

stripast = re.compile(r"\n\s*\*?")
named_re = re.compile(r"#\s*(\w*)\s*=([^#]*)#")
def parse_loop_header(loophead) :
    """Find all named replacements in the header

    Returns a list of dictionaries, one for each loop iteration,
    where each key is a name to be substituted and the corresponding
    value is the replacement string.

    """
    # Strip out '\n' and leading '*', if any, in continuation lines.
    # This should not effect code previous to this change as
    # continuation lines were not allowed.
    loophead = stripast.sub("", loophead)
    # parse out the names and lists of values
    names = []
    reps = named_re.findall(loophead)
    nsub = None
    for rep in reps:
        name = rep[0]
        vals = parse_values(rep[1])
        size = len(vals)
        if nsub is None :
            nsub = size
        elif nsub != size :
            msg = "Mismatch in number of values:\n%s = %s" % (name, vals)
            raise ValueError(msg)
        names.append((name,vals))

    # generate list of dictionaries, one for each template iteration
    dlist = []
    if nsub is None :
        raise ValueError("No substitution variables found")
    for i in range(nsub) :
        tmp = {}
        for name,vals in names :
            tmp[name] = vals[i]
        dlist.append(tmp)
    return dlist

replace_re = re.compile(r"@([\w]+)@")
def parse_string(astr, env, level, line) :
    lineno = "#line %d\n" % line

    # local function for string replacement, uses env
    def replace(match):
        name = match.group(1)
        try :
            val = env[name]
        except KeyError, e :
            msg = 'line %d: no definition of key "%s"'%(line, name)
            raise ValueError(msg)
        return val

    code = [lineno]
    struct = parse_structure(astr, level)
    if struct :
        # recurse over inner loops
        oldend = 0
        newlevel = level + 1
        for sub in struct:
            pref = astr[oldend:sub[0]]
            head = astr[sub[0]:sub[1]]
            text = astr[sub[1]:sub[2]]
            oldend = sub[3]
            newline = line + sub[4]
            code.append(replace_re.sub(replace, pref))
            try :
                envlist = parse_loop_header(head)
            except ValueError, e :
                msg = "line %d: %s" % (newline, e)
                raise ValueError(msg)
            for newenv in envlist :
                newenv.update(env)
                newcode = parse_string(text, newenv, newlevel, newline)
                code.extend(newcode)
        suff = astr[oldend:]
        code.append(replace_re.sub(replace, suff))
    else :
        # replace keys
        code.append(replace_re.sub(replace, astr))
    code.append('\n')
    return ''.join(code)

def process_str(astr):
    code = [header]
    code.extend(parse_string(astr, global_names, 0, 1))
    return ''.join(code)

include_src_re = re.compile(r"(\n|\A)#include\s*['\"]"
                            r"(?P<name>[\w\d./\\]+[.]src)['\"]", re.I)

def resolve_includes(source):
    d = os.path.dirname(source)
    fid = open(source)
    lines = []
    for line in fid.readlines():
        m = include_src_re.match(line)
        if m:
            fn = m.group('name')
            if not os.path.isabs(fn):
                fn = os.path.join(d,fn)
            if os.path.isfile(fn):
                print 'Including file',fn
                lines.extend(resolve_includes(fn))
            else:
                lines.append(line)
        else:
            lines.append(line)
    fid.close()
    return lines

def process_file(source):
    lines = resolve_includes(source)
    sourcefile = os.path.normcase(source).replace("\\","\\\\")
    try:
        code = process_str(''.join(lines))
    except ValueError, e:
        raise ValueError('In "%s" loop at %s' % (sourcefile, e))
    return '#line 1 "%s"\n%s' % (sourcefile, code)

def unique_key(adict):
    # this obtains a unique key given a dictionary
    # currently it works by appending together n of the letters of the
    #   current keys and increasing n until a unique key is found
    # -- not particularly quick
    allkeys = adict.keys()
    done = False
    n = 1
    while not done:
        newkey = "".join([x[:n] for x in allkeys])
        if newkey in allkeys:
            n += 1
        else:
            done = True
    return newkey

if __name__ == "__main__":

    try:
        file = sys.argv[1]
    except IndexError:
        fid = sys.stdin
        outfile = sys.stdout
    else:
        fid = open(file,'r')
        (base, ext) = os.path.splitext(file)
        newname = base
        outfile = open(newname,'w')

    allstr = fid.read()
    try:
        writestr = process_str(allstr)
    except ValueError, e:
        raise ValueError("In %s loop at %s" % (file, e))
    outfile.write(writestr)
<|endoftext|>


<commit_before>import os
import re
import sys

from numpy.distutils.fcompiler import FCompiler
from numpy.distutils.exec_command import exec_command, find_executable
from numpy.distutils.misc_util import make_temp_file
from distutils import log

compilers = ['IBMFCompiler']

class IBMFCompiler(FCompiler):
    compiler_type = 'ibm'
    description = 'IBM XL Fortran Compiler'
    version_pattern =  r'(xlf\(1\)\s*|)IBM XL Fortran ((Advanced Edition |)Version |Enterprise Edition V)(?P<version>[^\s*]*)'
    #IBM XL Fortran Enterprise Edition V10.1 for AIX \nVersion: 10.01.0000.0004

    executables = {
        'version_cmd'  : ["<F77>", "-qversion"],
        'compiler_f77' : ["xlf"],
        'compiler_fix' : ["xlf90", "-qfixed"],
        'compiler_f90' : ["xlf90"],
        'linker_so'    : ["xlf95"],
        'archiver'     : ["ar", "-cr"],
        'ranlib'       : ["ranlib"]
        }

    def get_version(self,*args,**kwds):
        version = FCompiler.get_version(self,*args,**kwds)

        if version is None and sys.platform.startswith('aix'):
            # use lslpp to find out xlf version
            lslpp = find_executable('lslpp')
            xlf = find_executable('xlf')
            if os.path.exists(xlf) and os.path.exists(lslpp):
                s,o = exec_command(lslpp + ' -Lc xlfcmp')
                m = re.search('xlfcmp:(?P<version>\d+([.]\d+)+)', o)
                if m: version = m.group('version')

        xlf_dir = '/etc/opt/ibmcmp/xlf'
        if version is None and os.path.isdir(xlf_dir):
            # linux:
            # If the output of xlf does not contain version info
            # (that's the case with xlf 8.1, for instance) then
            # let's try another method:
            l = os.listdir(xlf_dir)
            l.sort()
            l.reverse()
            l = [d for d in l if os.path.isfile(os.path.join(xlf_dir,d,'xlf.cfg'))]
            if l:
                from distutils.version import LooseVersion
                self.version = version = LooseVersion(l[0])
        return version

    def get_flags(self):
        return ['-qextname']

    def get_flags_debug(self):
        return ['-g']

    def get_flags_linker_so(self):
        opt = []
        if sys.platform=='darwin':
            opt.append('-Wl,-bundle,-flat_namespace,-undefined,suppress')
        else:
            opt.append('-bshared')
        version = self.get_version(ok_status=[0,40])
        if version is not None:
            if sys.platform.startswith('aix'):
                xlf_cfg = '/etc/xlf.cfg'
            else:
                xlf_cfg = '/etc/opt/ibmcmp/xlf/%s/xlf.cfg' % version
            fo, new_cfg = make_temp_file(suffix='_xlf.cfg')
            log.info('Creating '+new_cfg)
            fi = open(xlf_cfg,'r')
            crt1_match = re.compile(r'\s*crt\s*[=]\s*(?P<path>.*)/crt1.o').match
            for line in fi.readlines():
                m = crt1_match(line)
                if m:
                    fo.write('crt = %s/bundle1.o\n' % (m.group('path')))
                else:
                    fo.write(line)
            fi.close()
            fo.close()
            opt.append('-F'+new_cfg)
        return opt

    def get_flags_opt(self):
        return ['-O3']

if __name__ == '__main__':
    log.set_verbosity(2)
    compiler = IBMFCompiler()
    compiler.customize()
    print(compiler.get_version())
<commit_msg>MAINT: update version match regexp for IBM AIX Fortran compilers.<commit_after>import os
import re
import sys

from numpy.distutils.fcompiler import FCompiler
from numpy.distutils.exec_command import exec_command, find_executable
from numpy.distutils.misc_util import make_temp_file
from distutils import log

compilers = ['IBMFCompiler']

class IBMFCompiler(FCompiler):
    compiler_type = 'ibm'
    description = 'IBM XL Fortran Compiler'
    version_pattern =  r'(xlf\(1\)\s*|)IBM XL Fortran ((Advanced Edition |)Version |Enterprise Edition V|for AIX, V)(?P<version>[^\s*]*)'
    #IBM XL Fortran Enterprise Edition V10.1 for AIX \nVersion: 10.01.0000.0004

    executables = {
        'version_cmd'  : ["<F77>", "-qversion"],
        'compiler_f77' : ["xlf"],
        'compiler_fix' : ["xlf90", "-qfixed"],
        'compiler_f90' : ["xlf90"],
        'linker_so'    : ["xlf95"],
        'archiver'     : ["ar", "-cr"],
        'ranlib'       : ["ranlib"]
        }

    def get_version(self,*args,**kwds):
        version = FCompiler.get_version(self,*args,**kwds)

        if version is None and sys.platform.startswith('aix'):
            # use lslpp to find out xlf version
            lslpp = find_executable('lslpp')
            xlf = find_executable('xlf')
            if os.path.exists(xlf) and os.path.exists(lslpp):
                s,o = exec_command(lslpp + ' -Lc xlfcmp')
                m = re.search('xlfcmp:(?P<version>\d+([.]\d+)+)', o)
                if m: version = m.group('version')

        xlf_dir = '/etc/opt/ibmcmp/xlf'
        if version is None and os.path.isdir(xlf_dir):
            # linux:
            # If the output of xlf does not contain version info
            # (that's the case with xlf 8.1, for instance) then
            # let's try another method:
            l = os.listdir(xlf_dir)
            l.sort()
            l.reverse()
            l = [d for d in l if os.path.isfile(os.path.join(xlf_dir,d,'xlf.cfg'))]
            if l:
                from distutils.version import LooseVersion
                self.version = version = LooseVersion(l[0])
        return version

    def get_flags(self):
        return ['-qextname']

    def get_flags_debug(self):
        return ['-g']

    def get_flags_linker_so(self):
        opt = []
        if sys.platform=='darwin':
            opt.append('-Wl,-bundle,-flat_namespace,-undefined,suppress')
        else:
            opt.append('-bshared')
        version = self.get_version(ok_status=[0,40])
        if version is not None:
            if sys.platform.startswith('aix'):
                xlf_cfg = '/etc/xlf.cfg'
            else:
                xlf_cfg = '/etc/opt/ibmcmp/xlf/%s/xlf.cfg' % version
            fo, new_cfg = make_temp_file(suffix='_xlf.cfg')
            log.info('Creating '+new_cfg)
            fi = open(xlf_cfg,'r')
            crt1_match = re.compile(r'\s*crt\s*[=]\s*(?P<path>.*)/crt1.o').match
            for line in fi.readlines():
                m = crt1_match(line)
                if m:
                    fo.write('crt = %s/bundle1.o\n' % (m.group('path')))
                else:
                    fo.write(line)
            fi.close()
            fo.close()
            opt.append('-F'+new_cfg)
        return opt

    def get_flags_opt(self):
        return ['-O3']

if __name__ == '__main__':
    log.set_verbosity(2)
    compiler = IBMFCompiler()
    compiler.customize()
    print(compiler.get_version())
<|endoftext|>


<commit_before>import os
import re
import sys

from numpy.distutils.fcompiler import FCompiler
from distutils import log

class IbmFCompiler(FCompiler):

    compiler_type = 'ibm'
    version_pattern =  r'(xlf\(1\)\s*|)IBM XL Fortran ((Advanced Edition |)Version |Enterprise Edition V)(?P<version>[^\s*]*)'
    #IBM XL Fortran Enterprise Edition V10.1 for AIX \nVersion: 10.01.0000.0004
    executables = {
        'version_cmd'  : ["xlf"],
        'compiler_f77' : ["xlf"],
        'compiler_fix' : ["xlf90", "-qfixed"],
        'compiler_f90' : ["xlf90"],
        'linker_so'    : ["xlf95"],
        'archiver'     : ["ar", "-cr"],
        'ranlib'       : ["ranlib"]
        }

    def get_version(self,*args,**kwds):
        version = FCompiler.get_version(self,*args,**kwds)

        if version is None:
            # Let's try version_cmd with -qversion flag that V10 supports:
            l = self.__class__.executables['version_cmd']
            if '-qversion' not in l:
                l.append('-qversion')
                version = FCompiler.get_version(self,*args,**kwds)
                if version is None:
                    l.remove('-qversion')

        xlf_dir = '/etc/opt/ibmcmp/xlf'
        if version is None and os.path.isdir(xlf_dir):
            # If the output of xlf does not contain version info
            # (that's the case with xlf 8.1, for instance) then
            # let's try another method:
            l = os.listdir(xlf_dir)
            l.sort()
            l.reverse()
            l = [d for d in l if os.path.isfile(os.path.join(xlf_dir,d,'xlf.cfg'))]
            if l:
                from distutils.version import LooseVersion
                self.version = version = LooseVersion(l[0])
        return version

    def get_flags(self):
        return ['-qextname']

    def get_flags_debug(self):
        return ['-g']

    def get_flags_linker_so(self):
        opt = []
        if sys.platform=='darwin':
            opt.append('-Wl,-bundle,-flat_namespace,-undefined,suppress')
        else:
            opt.append('-bshared')
        version = self.get_version(ok_status=[0,40])
        if version is not None:
            import tempfile
            xlf_cfg = '/etc/opt/ibmcmp/xlf/%s/xlf.cfg' % version
            new_cfg = tempfile.mktemp()+'_xlf.cfg'
            log.info('Creating '+new_cfg)
            fi = open(xlf_cfg,'r')
            fo = open(new_cfg,'w')
            crt1_match = re.compile(r'\s*crt\s*[=]\s*(?P<path>.*)/crt1.o').match
            for line in fi.readlines():
                m = crt1_match(line)
                if m:
                    fo.write('crt = %s/bundle1.o\n' % (m.group('path')))
                else:
                    fo.write(line)
            fi.close()
            fo.close()
            opt.append('-F'+new_cfg)
        return opt

    def get_flags_opt(self):
        return ['-O5']

if __name__ == '__main__':
    from distutils import log
    log.set_verbosity(2)
    from numpy.distutils.fcompiler import new_fcompiler
    #compiler = new_fcompiler(compiler='ibm')
    compiler = IbmFCompiler()
    compiler.customize()
    print compiler.get_version()
<commit_msg>Fixing xlf compiler for AIX with 9.x and 10.x version support (needs testing and checking that xlf 8.x compilers are still detected)<commit_after>import os
import re
import sys

from numpy.distutils.fcompiler import FCompiler
from distutils import log

class IbmFCompiler(FCompiler):

    compiler_type = 'ibm'
    version_pattern =  r'(xlf\(1\)\s*|)IBM XL Fortran ((Advanced Edition |)Version |Enterprise Edition V)(?P<version>[^\s*]*)'
    #IBM XL Fortran Enterprise Edition V10.1 for AIX \nVersion: 10.01.0000.0004
    executables = {
        'version_cmd'  : ["xlf","-qversion"],
        'compiler_f77' : ["xlf"],
        'compiler_fix' : ["xlf90", "-qfixed"],
        'compiler_f90' : ["xlf90"],
        'linker_so'    : ["xlf95"],
        'archiver'     : ["ar", "-cr"],
        'ranlib'       : ["ranlib"]
        }

    def get_version(self,*args,**kwds):
        version = FCompiler.get_version(self,*args,**kwds)

        if version is None:
            # Let's try version_cmd without -qversion flag that
            # xlf versions <=8.x don't have.
            version_cmd =  self.version_cmd
            orig_version_cmd = version_cmd[:]
            if '-qversion' in version_cmd:
                version_cmd.remove('-qversion')
                version = FCompiler.get_version(self,*args,**kwds)
                if version is None:
                    version_cmd[:] = orig_version_cmd

        xlf_dir = '/etc/opt/ibmcmp/xlf'
        if version is None and os.path.isdir(xlf_dir):
            # If the output of xlf does not contain version info
            # (that's the case with xlf 8.1, for instance) then
            # let's try another method:
            l = os.listdir(xlf_dir)
            l.sort()
            l.reverse()
            l = [d for d in l if os.path.isfile(os.path.join(xlf_dir,d,'xlf.cfg'))]
            if l:
                from distutils.version import LooseVersion
                self.version = version = LooseVersion(l[0])
        return version

    def get_flags(self):
        return ['-qextname']

    def get_flags_debug(self):
        return ['-g']

    def get_flags_linker_so(self):
        opt = []
        if sys.platform=='darwin':
            opt.append('-Wl,-bundle,-flat_namespace,-undefined,suppress')
        else:
            opt.append('-bshared')
        version = self.get_version(ok_status=[0,40])
        if version is not None:
            import tempfile
            if sys.platform.startswith('aix'):
                xlf_cfg = '/etc/xlf.cfg'
            else:
                xlf_cfg = '/etc/opt/ibmcmp/xlf/%s/xlf.cfg' % version
            new_cfg = tempfile.mktemp()+'_xlf.cfg'
            log.info('Creating '+new_cfg)
            fi = open(xlf_cfg,'r')
            fo = open(new_cfg,'w')
            crt1_match = re.compile(r'\s*crt\s*[=]\s*(?P<path>.*)/crt1.o').match
            for line in fi.readlines():
                m = crt1_match(line)
                if m:
                    fo.write('crt = %s/bundle1.o\n' % (m.group('path')))
                else:
                    fo.write(line)
            fi.close()
            fo.close()
            opt.append('-F'+new_cfg)
        return opt

    def get_flags_opt(self):
        return ['-O5']

if __name__ == '__main__':
    from distutils import log
    log.set_verbosity(2)
    from numpy.distutils.fcompiler import new_fcompiler
    #compiler = new_fcompiler(compiler='ibm')
    compiler = IbmFCompiler()
    compiler.customize()
    print compiler.get_version()
<|endoftext|>


<commit_before># Giles: channel_manager.py
# Copyright 2012 Phil Bordelon
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from channel import Channel

from giles.utils import name_is_valid

class ChannelManager(object):
    """The ChannelManager handles individuals connecting and disconnecting
    to the various channels, maintains the global channel, and so on.
    """

    def __init__(self, server):

        self.server = server

        # Set up the global channel.
        self.channels = [Channel("Global", persistent = True, notifications = False, gameable = False)]

    def add_channel(self, name, persistent = False, notifications = True, gameable = False, key = None):

        if not name_is_valid(name):
            return False

        # Make sure this isn't a duplicate.
        if self.has_channel(name):
            return False

        # Not a duplicate.  Make a new entry.  Like users, 'name' is for
        # comparison; the channel itself tracks its display name.
        self.channels.append(Channel(name, persistent, notifications, gameable, key))
        return self.channels[-1]

    def has_channel(self, name):

        lower_name = name.lower()
        for other in self.channels:
            if other.name == lower_name:
                return other

        return False

    def connect(self, player, name, key = None):

        success = False

        if type(name) == str and len(name) > 0:

            # Does this channel already exist?  If so, snag that.
            lower_name = name.lower()
            for channel in self.channels:
                if channel.name == lower_name:
                    success = channel.connect(player, key)

            if not success:

                # Huh.  All right; let's make it!
                new_channel = self.add_channel(name, key = key)
                if new_channel:

                    # Creation was successful.  Connect the player.
                    success = new_channel.connect(player, key)

        return success

    def disconnect(self, player, name):

        success = False

        if type(name) == str and len(name) > 0:

            lower_name = name.lower()
            for channel in self.channels:
                if channel.name == lower_name:
                    success = channel.disconnect(player)

        return success

    def remove_player(self, player):

        for channel in self.channels:
            if player in channel.listeners:
                channel.disconnect(player)

    def send(self, player, msg, name):

        success = False
        if type(name) == str and len(name) > 0:

            lower_name = name.lower()
            for channel in self.channels:
                if channel.name == lower_name:
                    success = channel.send(player, msg)

        return success

    def cleanup(self):

        # Remove any non-persistent channels with no listeners.
        for channel in self.channels:
            if not channel.persistent and len(channel.listeners) == 0:
                self.server.log.log("Deleting stale channel %s." % channel)
                self.channels.remove(channel)
                del channel
<commit_msg>channel_manager: log() utility function.<commit_after># Giles: channel_manager.py
# Copyright 2012 Phil Bordelon
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from channel import Channel

from giles.utils import name_is_valid

class ChannelManager(object):
    """The ChannelManager handles individuals connecting and disconnecting
    to the various channels, maintains the global channel, and so on.
    """

    def __init__(self, server):

        self.server = server

        # Set up the global channel.
        self.channels = [Channel("Global", persistent = True, notifications = False, gameable = False)]

    def log(self, message):
        self.server.log.log("[CM] %s" % message)

    def add_channel(self, name, persistent = False, notifications = True, gameable = False, key = None):

        if not name_is_valid(name):
            return False

        # Make sure this isn't a duplicate.
        if self.has_channel(name):
            return False

        # Not a duplicate.  Make a new entry.  Like users, 'name' is for
        # comparison; the channel itself tracks its display name.
        self.channels.append(Channel(name, persistent, notifications, gameable, key))
        return self.channels[-1]

    def has_channel(self, name):

        lower_name = name.lower()
        for other in self.channels:
            if other.name == lower_name:
                return other

        return False

    def connect(self, player, name, key = None):

        success = False

        if type(name) == str and len(name) > 0:

            # Does this channel already exist?  If so, snag that.
            lower_name = name.lower()
            for channel in self.channels:
                if channel.name == lower_name:
                    success = channel.connect(player, key)

            if not success:

                # Huh.  All right; let's make it!
                new_channel = self.add_channel(name, key = key)
                if new_channel:

                    # Creation was successful.  Connect the player.
                    success = new_channel.connect(player, key)

        return success

    def disconnect(self, player, name):

        success = False

        if type(name) == str and len(name) > 0:

            lower_name = name.lower()
            for channel in self.channels:
                if channel.name == lower_name:
                    success = channel.disconnect(player)

        return success

    def remove_player(self, player):

        for channel in self.channels:
            if player in channel.listeners:
                channel.disconnect(player)

    def send(self, player, msg, name):

        success = False
        if type(name) == str and len(name) > 0:

            lower_name = name.lower()
            for channel in self.channels:
                if channel.name == lower_name:
                    success = channel.send(player, msg)

        return success

    def cleanup(self):

        # Remove any non-persistent channels with no listeners.
        for channel in self.channels:
            if not channel.persistent and len(channel.listeners) == 0:
                self.log("Deleting stale channel %s." % channel)
                self.channels.remove(channel)
                del channel
<|endoftext|>


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

from __future__ import (print_function, with_statement)

import re
import sys
import os
import csv
from collections import namedtuple

import dulwich.repo
import click

TraceInfo = namedtuple('traceinfo', 'commit_id author committer date message bug_ids')
trigger_words = "bug|fix|fixing|pr|br|bz|bugzilla|issue|problem"
bugids = re.compile("(?:" + trigger_words + ")\\s*(?:id|[#=])?\\s*([0-9]{4,6})",
        flags=re.IGNORECASE)
plurals = re.compile("(?:" + trigger_words + ")s\\s*(?:id|[#=])?\\s*([0-9]{4,6})",
        flags=re.IGNORECASE)
idnumbers = re.compile("(?:id|[#=])\\s*([0-9]{4,6})", flags=re.IGNORECASE)
numbers = re.compile("([0-9]{4,6})", flags=re.IGNORECASE)
bugzillaURLs = re.compile("(?:http|https)://\S+/show_bug.cgi\?id=([0-9]{4,6})")

def detect(msg):
    ids = set()

    r = plurals.findall(msg)
    if r:
        # handle lists a bit differently
        lines = msg.split('\n')
        for line in lines:
            r = plurals.findall(line)
            if r:
                nr = numbers.findall(line)
                if nr:
                    ids.update(nr)

    r = bugids.findall(msg)
    if r:
        ids.update(r)

    r = bugzillaURLs.findall(msg)
    if r:
        ids.update(r)

    if len(ids) == 0:
        r = idnumbers.findall(msg)
        if r:
            ids.update(r)

    return tuple(ids)

def get_links(repo):

    for walk_entry in repo.get_walker():
        commit = walk_entry.commit

        trace = TraceInfo(
                author = commit.author
                , committer = commit.committer
                , commit_id = commit.id
                , date = commit.commit_time # + commit.commit_time_zone ?
                , message = commit.message
                , bug_ids = detect(commit.message)
                )

        yield trace

def process_humans(l, h, repo):
    # for each in humans_file,
    #   display commit
    #   display list of detected ids
    #   input corrected list
    #   append result to links_file

    for row in h:
        commit = repo[row[0]]
        items = row[1:]
        confirmed_ids = list()

        print()
        print(">> Processing commit:", commit.id)
        print(commit.message)

        print("---------------------------------------------------")
        print(">> Which are the valid ids?", items)
        while True and len(items) > 0:
            for index, item in enumerate(items):
                print("%d) %s" %(index, item))
            print("q) None of these")

            response = raw_input("? ")
            if response == 'q':
                break
            i = int(response)

            print("Adding (%d) %s to links file." % (i, items[i]))
            confirmed_ids.append(items[i])
            items.remove(items[i])

        if len(confirmed_ids) > 0:
            links.writerow([commit.id] + confirmed_ids)

@click.command()
@click.option('--process', is_flag=True)
@click.option('--links_file', default='links.csv',
              help='Output file for links')
@click.option('--humans_file', default='humans.csv',
              help='Output file for links humans need to check')
@click.argument('git_path', type=click.Path(exists=True))
def main(process, links_file, humans_file, git_path):
    repo = dulwich.repo.Repo(git_path)

    if process:
        with open(links_file, 'a') as l:
            links = csv.writer(l)
            with open(humans_file, 'r') as h:
                humans = csv.reader(h)
                process_humans(links, humans, repo)
    else:
        with open(links_file, 'w') as l:
            links = csv.writer(l)
            with open(humans_file, 'w') as h:
                humans = csv.writer(h)
                for each in get_links(repo):
                    if len(each.bug_ids) > 1:
                        humans.writerow((each.commit_id,) + each.bug_ids)
                    elif len(each.bug_ids) == 1:
                        links.writerow([each.commit_id, each.bug_ids[0]])
<commit_msg>Recover IDs of any length<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import (print_function, with_statement)

import re
import sys
import os
import csv
from collections import namedtuple

import dulwich.repo
import click

TraceInfo = namedtuple('traceinfo', 'commit_id author committer date message bug_ids')
trigger_words = "issue|problem|bug|fix|fixing|fixes|close|closes|pr|br|bz|bugzilla"
bugids = re.compile("(?:" + trigger_words + ")\\s*(?:id|[#=])?\\s*([0-9]+)",
        flags=re.IGNORECASE)
plurals = re.compile("(?:" + trigger_words + ")s\\s*(?:id|[#=])?\\s*([0-9]+)",
        flags=re.IGNORECASE)
idnumbers = re.compile("(?:id|[#=])\\s*([0-9]+)", flags=re.IGNORECASE)
numbers = re.compile("([0-9]+)", flags=re.IGNORECASE)
bugzillaURLs = re.compile("(?:http|https)://\S+/show_bug.cgi\?id=([0-9]+)")

def detect(msg):
    ids = set()

    r = plurals.findall(msg)
    if r:
        # handle lists a bit differently
        lines = msg.split('\n')
        for line in lines:
            r = plurals.findall(line)
            if r:
                nr = numbers.findall(line)
                if nr:
                    ids.update(nr)

    r = bugids.findall(msg)
    if r:
        ids.update(r)

    r = bugzillaURLs.findall(msg)
    if r:
        ids.update(r)

    if len(ids) == 0:
        r = idnumbers.findall(msg)
        if r:
            ids.update(r)

    return tuple(ids)

def get_links(repo):

    for walk_entry in repo.get_walker():
        commit = walk_entry.commit

        trace = TraceInfo(
                author = commit.author
                , committer = commit.committer
                , commit_id = commit.id
                , date = commit.commit_time # + commit.commit_time_zone ?
                , message = commit.message
                , bug_ids = detect(commit.message)
                )

        yield trace

def process_humans(l, h, repo):
    # for each in humans_file,
    #   display commit
    #   display list of detected ids
    #   input corrected list
    #   append result to links_file

    for row in h:
        commit = repo[row[0]]
        items = row[1:]
        confirmed_ids = list()

        print()
        print(">> Processing commit:", commit.id)
        print(commit.message)

        print("---------------------------------------------------")
        print(">> Which are the valid ids?", items)
        while True and len(items) > 0:
            for index, item in enumerate(items):
                print("%d) %s" %(index, item))
            print("q) None of these")

            response = raw_input("? ")
            if response == 'q':
                break
            i = int(response)

            print("Adding (%d) %s to links file." % (i, items[i]))
            confirmed_ids.append(items[i])
            items.remove(items[i])

        if len(confirmed_ids) > 0:
            links.writerow([commit.id] + confirmed_ids)

@click.command()
@click.option('--process', is_flag=True)
@click.option('--links_file', default='links.csv',
              help='Output file for links')
@click.option('--humans_file', default='humans.csv',
              help='Output file for links humans need to check')
@click.argument('git_path', type=click.Path(exists=True))
def main(process, links_file, humans_file, git_path):
    repo = dulwich.repo.Repo(git_path)

    if process:
        with open(links_file, 'a') as l:
            links = csv.writer(l)
            with open(humans_file, 'r') as h:
                humans = csv.reader(h)
                process_humans(links, humans, repo)
    else:
        with open(links_file, 'w') as l:
            links = csv.writer(l)
            with open(humans_file, 'w') as h:
                humans = csv.writer(h)
                for each in get_links(repo):
                    if len(each.bug_ids) > 1:
                        humans.writerow((each.commit_id,) + each.bug_ids)
                    elif len(each.bug_ids) == 1:
                        links.writerow([each.commit_id, each.bug_ids[0]])
<|endoftext|>


<commit_before>from github2.core import BaseData, GithubCommand, Attribute, DateAttribute

class PullRequest(BaseData):
    state = Attribute("The pull request state")
    base = Attribute("The base repo")
    head = Attribute("The head of the pull request")
    issue_user = Attribute("The user who created the pull request.")
    user = Attribute("The owner of the repo.")
    title = Attribute("The text of the pull request title.")
    body = Attribute("The text of the body.")
    position = Attribute("Floating point position of the pull request.")
    number = Attribute("Number of this request.")
    votes = Attribute("Number of votes for this request.")
    comments = Attribute("Number of comments made on this request.")
    diff_url = Attribute("The URL to the unified diff.")
    patch_url = Attribute("The URL to the downloadable patch.")
    labels = Attribute("A list of labels attached to the pull request.")
    html_url = Attribute("The URL to the pull request.")
    issue_created_at = DateAttribute("The date the issue for this pull request was opened.",
                                     format='iso')
    issue_updated_at = DateAttribute("The date the issue for this pull request was last updated.",
                                     format='iso')
    created_at = DateAttribute("The date when this pull request was created.",
                               format='iso')
    updated_at = DateAttribute("The date when this pull request was last updated.",
                               format='iso')
    closed_at = DateAttribute("The date when this pull request was closed",
                              format='iso')
    discussion = Attribute("Discussion thread for the pull request.")
    mergeable = Attribute("Whether the pull request can be merge cleanly")

    def __repr__(self):
        return "<PullRequest: %s>" % self.html_url

class PullRequests(GithubCommand):
    domain = "pulls"

    def create(self, project, base, head, title=None, body=None, issue=None):
        """Create a new pull request

        Pull requests can be created from scratch, or attached to an existing
        issue.  If an ``issue`` parameter is supplied the pull request is
        attached to that issue, else a new pull request is created.

        .. versionadded:: 0.4.0

        :param str project: Github project
        :param str base: branch changes should be pulled into
        :param str head: branch of the changes to be pulled
        :param str title: title for pull request
        :param str body: optional body for pull request
        :param str issue: existing issue to attach pull request to
        """
        post_data = {"base": base, "head": head}
        if issue:
            post_data["issue"] = issue
        elif title:
            post_data["title"] = title
            if body:
                post_data["body"] = body
        pull_request_data = [("pull[%s]" % k, v) for k, v in post_data.items()]
        return self.get_value(project, post_data=dict(pull_request_data),
            filter="pull", datatype=PullRequest)

    def show(self, project, number):
        """Show a single pull request

        :param str project: Github project
        :param int number: pull request number in the Github database
        """
        return self.get_value(project, str(number), filter="pull",
                              datatype=PullRequest)

    def list(self, project, state="open"):
        """List all pull requests for a project

        :param str project: Github project
        :param str state: can be either ``open`` or ``closed``
        """
        return self.get_values(project, state, filter="pulls",
                               datatype=PullRequest)
<commit_msg>Minor PEP-8 fix.<commit_after>from github2.core import BaseData, GithubCommand, Attribute, DateAttribute

class PullRequest(BaseData):
    state = Attribute("The pull request state")
    base = Attribute("The base repo")
    head = Attribute("The head of the pull request")
    issue_user = Attribute("The user who created the pull request.")
    user = Attribute("The owner of the repo.")
    title = Attribute("The text of the pull request title.")
    body = Attribute("The text of the body.")
    position = Attribute("Floating point position of the pull request.")
    number = Attribute("Number of this request.")
    votes = Attribute("Number of votes for this request.")
    comments = Attribute("Number of comments made on this request.")
    diff_url = Attribute("The URL to the unified diff.")
    patch_url = Attribute("The URL to the downloadable patch.")
    labels = Attribute("A list of labels attached to the pull request.")
    html_url = Attribute("The URL to the pull request.")
    issue_created_at = DateAttribute("The date the issue for this pull request was opened.",
                                     format='iso')
    issue_updated_at = DateAttribute("The date the issue for this pull request was last updated.",
                                     format='iso')
    created_at = DateAttribute("The date when this pull request was created.",
                               format='iso')
    updated_at = DateAttribute("The date when this pull request was last updated.",
                               format='iso')
    closed_at = DateAttribute("The date when this pull request was closed",
                              format='iso')
    discussion = Attribute("Discussion thread for the pull request.")
    mergeable = Attribute("Whether the pull request can be merge cleanly")

    def __repr__(self):
        return "<PullRequest: %s>" % self.html_url

class PullRequests(GithubCommand):
    domain = "pulls"

    def create(self, project, base, head, title=None, body=None, issue=None):
        """Create a new pull request

        Pull requests can be created from scratch, or attached to an existing
        issue.  If an ``issue`` parameter is supplied the pull request is
        attached to that issue, else a new pull request is created.

        .. versionadded:: 0.4.0

        :param str project: Github project
        :param str base: branch changes should be pulled into
        :param str head: branch of the changes to be pulled
        :param str title: title for pull request
        :param str body: optional body for pull request
        :param str issue: existing issue to attach pull request to
        """
        post_data = {"base": base, "head": head}
        if issue:
            post_data["issue"] = issue
        elif title:
            post_data["title"] = title
            if body:
                post_data["body"] = body
        pull_request_data = [("pull[%s]" % k, v) for k, v in post_data.items()]
        return self.get_value(project, post_data=dict(pull_request_data),
            filter="pull", datatype=PullRequest)

    def show(self, project, number):
        """Show a single pull request

        :param str project: Github project
        :param int number: pull request number in the Github database
        """
        return self.get_value(project, str(number), filter="pull",
                              datatype=PullRequest)

    def list(self, project, state="open"):
        """List all pull requests for a project

        :param str project: Github project
        :param str state: can be either ``open`` or ``closed``
        """
        return self.get_values(project, state, filter="pulls",
                               datatype=PullRequest)
<|endoftext|>


<commit_before>#!/usr/bin/env python

import psycopg2

"""
Querying neatly formatted information from a (postgreSQL) news database.
This file enables you to look at data in different ways.
To achieve any of these queryes, views have been made to simplyfy things.
You can read more about the views used here, in the DB-Views.md file.
"""

DBNAME = "news"

def connect(database_name="news"):
    """Connect to the PostgreSQL database.  Returns a database connection."""
    try:
        db = psycopg2.connect("dbname={}".format(database_name))
        c = db.cursor()
        return db, c
    except:
        print "Unable to connect to database"

def get_top3_articles():
    """Return articles from the database, most viewed first.
    'v_articleViews' is created in news.sql,
    it fetches how many times any article has been accessed."""
    db, c = connect()
    print "\nFetching top 3 articles, most popular first...\n"
    c.execute("""SELECT * FROM v_articleViews
                ORDER BY "Views" DESC
                LIMIT 3;""")
    for top3_articles in c:
        print str('{} {} {}'.format(top3_articles[0],
                                top3_articles[1],
                                "\n"))
    return
    db.close()

def get_top_authors():
    """Returns most popular article authors of all time, most popular first"""
    db, c = connect()
    print "\nFetching top authors, most popular first...\n"
    c.execute("""SELECT name AS "Authors name",
                total_views AS "Total number of article views"
                FROM authors, v_authorViews
                WHERE authors.id = v_authorViews.author
                ORDER BY v_authorViews.total_views DESC;""")
    for top_authors in c:
        print str('{} {} {} {}'.format(top_authors[0],
                                   "with",
                                   top_authors[1],
                                   "Views\n"))
    return
    db.close()

def error_requests():
    """Returns which days more than 1% of requests were errors"""
    db, c = connect()
    print "\nFetching which days more than 1% of requests were errors...\n"
    c.execute("""SELECT bad, to_char(day, 'Mon-MM YYYY')
                 FROM v_errorDays""")
    for which_days in c:
        print str("There were " + '{} {} {} {}'.format(which_days[0],
                                                   'bad requests',
                                                   which_days[1],
                                                   "\n"))
    return
    db.close()

if __name__ == '__main__':
    get_top3_articles()
    get_top_authors()
    error_requests()
<commit_msg>pep8 checkup<commit_after>#!/usr/bin/env python

import psycopg2

"""
Querying neatly formatted information from a (postgreSQL) news database.
This file enables you to look at data in different ways.
To achieve any of these queryes, views have been made to simplyfy things.
You can read more about the views used here, in the DB-Views.md file.
"""

DBNAME = "news"

def connect(database_name="news"):
    """Connect to the PostgreSQL database.  Returns a database connection."""
    try:
        db = psycopg2.connect("dbname={}".format(database_name))
        c = db.cursor()
        return db, c
    except:
        print "Unable to connect to database"

def get_top3_articles():
    """Return articles from the database, most viewed first.
    'v_articleViews' is created in news.sql,
    it fetches how many times any article has been accessed."""
    db, c = connect()
    print "\nFetching top 3 articles, most popular first...\n"
    c.execute("""SELECT * FROM v_articleViews
                ORDER BY "Views" DESC
                LIMIT 3;""")
    for top3_articles in c:
        print str('{} {} {}'.format(top3_articles[0],
                                    top3_articles[1],
                                    "\n"))
    return
    db.close()

def get_top_authors():
    """Returns most popular article authors of all time, most popular first"""
    db, c = connect()
    print "\nFetching top authors, most popular first...\n"
    c.execute("""SELECT name AS "Authors name",
                total_views AS "Total number of article views"
                FROM authors, v_authorViews
                WHERE authors.id = v_authorViews.author
                ORDER BY v_authorViews.total_views DESC;""")
    for top_authors in c:
        print str('{} {} {} {}'.format(top_authors[0],
                                       "with",
                                       top_authors[1],
                                       "Views\n"))
    return
    db.close()

def error_requests():
    """Returns which days more than 1% of requests were errors"""
    db, c = connect()
    print "\nFetching which days more than 1% of requests were errors...\n"
    c.execute("""SELECT bad, to_char(day, 'Mon-MM YYYY')
                 FROM v_errorDays""")
    for which_days in c:
        print str("There were " + '{} {} {} {}'.format(which_days[0],
                                                       'bad requests',
                                                       which_days[1],
                                                       "\n"))
    return
    db.close()

if __name__ == '__main__':
    get_top3_articles()
    get_top_authors()
    error_requests()
<|endoftext|>


<commit_before>import functools
import logging
import os

import tensorflow as tf
import gargparse
from gargparse import ARGS

def add_flag(name, *args, **kwargs):
  gargparse.add_argument("--" + name, *args, **kwargs)

def str_list(string):
  return string.split(',')

def int_list(string):
  return [int(num) for num in string.split(',')]

# Cluster (Distributed TensorFlow)

_list_of_hosts = "Comma-separated list of hostname:port pairs"
add_flag("ps-hosts", type=str_list, required=True, help=_list_of_hosts)
add_flag("worker-hosts", type=str_list, required=True, help=_list_of_hosts)
add_flag("job-name", required=True, help="'ps' or 'worker'")
add_flag("task-index", type=int, required=True, help="Task index within a job")

# Hyperparameters

add_flag("num-epochs", type=int, default=64)
add_flag("batch-size", type=int, default=64)
add_flag("dropout-prob", type=float, default=0)

def _read_words(filename):
  with open(filename) as file_:
    return sorted([line.strip() for line in file_.readlines()])

add_flag("word-file", dest="words", type=_read_words)
add_flag("rnn-cell", dest="_rnn_cell", default="ln_lstm")
add_flag("word-embedding-size", type=int, default=200)

# QA

add_flag("first-entity-index", type=int)
add_flag("last-entity-index", type=int)

# Data types

add_flag("float-type", type=(lambda name: getattr(tf, name)), default="float32")
add_flag("int-type", type=(lambda name: getattr(tf, name)), default="int32")

# Data files

add_flag("file-glob", required=True, help="File path glob to search data files")
add_flag("file-format", required=True, help="Data format of files")

# Others

def _set_up_logger(level):
  logging.getLogger().setLevel(getattr(logging, level.upper()))
  logging.basicConfig(format="%(asctime)s::PID-%(process)d:%(module)s:"
                             "%(funcName)s: %(message)s")

add_flag("log-level", default="info", type=_set_up_logger)
add_flag("log-dir",
         default="log",
         help="Directory containing checkpoint and event files")
add_flag("debug", action="store_true")
add_flag("num-cpus", type=int, default=os.cpu_count())
add_flag("num-threads-per-queue", type=int, default=2)
add_flag("batch-queue-capacity", type=int, default=2)
add_flag("length-boundaries", type=int_list)

def _cached_property(func):
  @property
  @functools.wraps(func)
  def wrapper(self):
    attr = "_cached_" + func.__name__

    if not hasattr(self, attr):
      setattr(self, attr, func(self))
    return getattr(self, attr)

  return wrapper

class _Flags:
  def __getattr__(self, name):
    try:
      return getattr(ARGS, name)
    except AttributeError:
      return object.__getattribute__(self, name)

  @_cached_property
  def word_indices(self):
    indices = { word: index + 2 for index, word in enumerate(ARGS.words) }
    indices.update({ '<NULL>': 0, '<UNKNOWN>': 1 })
    return indices

  @_cached_property
  def word_space_size(self):
    return len(self.word_indices)

  @property
  def rnn_cell(self):
    from .rnn import cell
    return getattr(cell, ARGS._rnn_cell)

  @property
  def filename_queue_capacity(self):
    return ARGS.batch_queue_capacity * ARGS.batch_size

FLAGS = _Flags()
<commit_msg>Use gru by default<commit_after>import functools
import logging
import os

import tensorflow as tf
import gargparse
from gargparse import ARGS

def add_flag(name, *args, **kwargs):
  gargparse.add_argument("--" + name, *args, **kwargs)

def str_list(string):
  return string.split(',')

def int_list(string):
  return [int(num) for num in string.split(',')]

# Cluster (Distributed TensorFlow)

_list_of_hosts = "Comma-separated list of hostname:port pairs"
add_flag("ps-hosts", type=str_list, required=True, help=_list_of_hosts)
add_flag("worker-hosts", type=str_list, required=True, help=_list_of_hosts)
add_flag("job-name", required=True, help="'ps' or 'worker'")
add_flag("task-index", type=int, required=True, help="Task index within a job")

# Hyperparameters

add_flag("num-epochs", type=int, default=64)
add_flag("batch-size", type=int, default=64)
add_flag("dropout-prob", type=float, default=0)

def _read_words(filename):
  with open(filename) as file_:
    return sorted([line.strip() for line in file_.readlines()])

add_flag("word-file", dest="words", type=_read_words)
add_flag("rnn-cell", dest="_rnn_cell", default="gru")
add_flag("word-embedding-size", type=int, default=200)

# QA

add_flag("first-entity-index", type=int)
add_flag("last-entity-index", type=int)

# Data types

add_flag("float-type", type=(lambda name: getattr(tf, name)), default="float32")
add_flag("int-type", type=(lambda name: getattr(tf, name)), default="int32")

# Data files

add_flag("file-glob", required=True, help="File path glob to search data files")
add_flag("file-format", required=True, help="Data format of files")

# Others

def _set_up_logger(level):
  logging.getLogger().setLevel(getattr(logging, level.upper()))
  logging.basicConfig(format="%(asctime)s::PID-%(process)d:%(module)s:"
                             "%(funcName)s: %(message)s")

add_flag("log-level", default="info", type=_set_up_logger)
add_flag("log-dir",
         default="log",
         help="Directory containing checkpoint and event files")
add_flag("debug", action="store_true")
add_flag("num-cpus", type=int, default=os.cpu_count())
add_flag("num-threads-per-queue", type=int, default=2)
add_flag("batch-queue-capacity", type=int, default=2)
add_flag("length-boundaries", type=int_list)

def _cached_property(func):
  @property
  @functools.wraps(func)
  def wrapper(self):
    attr = "_cached_" + func.__name__

    if not hasattr(self, attr):
      setattr(self, attr, func(self))
    return getattr(self, attr)

  return wrapper

class _Flags:
  def __getattr__(self, name):
    try:
      return getattr(ARGS, name)
    except AttributeError:
      return object.__getattribute__(self, name)

  @_cached_property
  def word_indices(self):
    indices = { word: index + 2 for index, word in enumerate(ARGS.words) }
    indices.update({ '<NULL>': 0, '<UNKNOWN>': 1 })
    return indices

  @_cached_property
  def word_space_size(self):
    return len(self.word_indices)

  @property
  def rnn_cell(self):
    from .rnn import cell
    return getattr(cell, ARGS._rnn_cell)

  @property
  def filename_queue_capacity(self):
    return ARGS.batch_queue_capacity * ARGS.batch_size

FLAGS = _Flags()
<|endoftext|>