<commit_before>#
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from optparse import SUPPRESS_HELP
from color import Coloring
from command import PagedCommand
from git_command import GitCommand

class GrepColoring(Coloring):
  def __init__(self, config):
    Coloring.__init__(self, config, 'grep')
    self.project = self.printer('project', attr='bold')

class Grep(PagedCommand):
  common = True
  helpSummary = "Print lines matching a pattern"
  helpUsage = """
%prog {pattern | -e pattern} [<project>...]
"""
  helpDescription = """
Search for the specified patterns in all project files.

Options
-------

The following options can appear as often as necessary to express
the pattern to locate:

 -e PATTERN
 --and, --or, --not, -(, -)

Further, the -r/--revision option may be specified multiple times
in order to scan multiple trees.  If the same file matches in more
than one tree, only the first result is reported, prefixed by the
revision name it was found under.

Examples
-------

Look for a line that has '#define' and either 'MAX_PATH or 'PATH_MAX':

  repo grep -e '#define' --and -\( -e MAX_PATH -e PATH_MAX \)

Look for a line that has 'NODE' or 'Unexpected' in files that
contain a line that matches both expressions:

  repo grep --all-match -e NODE -e Unexpected

"""

  def _Options(self, p):
    def carry(option,
              opt_str,
              value,
              parser):
      pt = getattr(parser.values, 'cmd_argv', None)
      if pt is None:
        pt = []
        setattr(parser.values, 'cmd_argv', pt)

      if opt_str == '-(':
        pt.append('(')
      elif opt_str == '-)':
        pt.append(')')
      else:
        pt.append(opt_str)

      if value is not None:
        pt.append(value)

    g = p.add_option_group('Sources')
    g.add_option('--cached',
                 action='callback', callback=carry,
                 help='Search the index, instead of the work tree')
    g.add_option('-r','--revision',
                 dest='revision', action='append', metavar='TREEish',
                 help='Search TREEish, instead of the work tree')

    g = p.add_option_group('Pattern')
    g.add_option('-e',
                 action='callback', callback=carry,
                 metavar='PATTERN', type='str',
                 help='Pattern to search for')
    g.add_option('-i', '--ignore-case',
                 action='callback', callback=carry,
                 help='Ignore case differences')
    g.add_option('-a','--text',
                 action='callback', callback=carry,
                 help="Process binary files as if they were text")
    g.add_option('-I',
                 action='callback', callback=carry,
                 help="Don't match the pattern in binary files")
    g.add_option('-w', '--word-regexp',
                 action='callback', callback=carry,
                 help='Match the pattern only at word boundaries')
    g.add_option('-v', '--invert-match',
                 action='callback', callback=carry,
                 help='Select non-matching lines')
    g.add_option('-G', '--basic-regexp',
                 action='callback', callback=carry,
                 help='Use POSIX basic regexp for patterns (default)')
    g.add_option('-E', '--extended-regexp',
                 action='callback', callback=carry,
                 help='Use POSIX extended regexp for patterns')
    g.add_option('-F', '--fixed-strings',
                 action='callback', callback=carry,
                 help='Use fixed strings (not regexp) for pattern')

    g = p.add_option_group('Pattern Grouping')
    g.add_option('--all-match',
                 action='callback', callback=carry,
                 help='Limit match to lines that have all patterns')
    g.add_option('--and', '--or', '--not',
                 action='callback', callback=carry,
                 help='Boolean operators to combine patterns')
    g.add_option('-(','-)',
                 action='callback', callback=carry,
                 help='Boolean operator grouping')

    g = p.add_option_group('Output')
    g.add_option('-n',
                 action='callback', callback=carry,
                 help='Prefix the line number to matching lines')
    g.add_option('-C',
                 action='callback', callback=carry,
                 metavar='CONTEXT', type='str',
                 help='Show CONTEXT lines around match')
    g.add_option('-B',
                 action='callback', callback=carry,
                 metavar='CONTEXT', type='str',
                 help='Show CONTEXT lines before match')
    g.add_option('-A',
                 action='callback', callback=carry,
                 metavar='CONTEXT', type='str',
                 help='Show CONTEXT lines after match')
    g.add_option('-l','--name-only','--files-with-matches',
                 action='callback', callback=carry,
                 help='Show only file names containing matching lines')
    g.add_option('-L','--files-without-match',
                 action='callback', callback=carry,
                 help='Show only file names not containing matching lines')

  def Execute(self, opt, args):
    out = GrepColoring(self.manifest.manifestProject.config)

    cmd_argv = ['grep']
    if out.is_on:
      cmd_argv.append('--color')
    cmd_argv.extend(getattr(opt,'cmd_argv',[]))

    if '-e' not in cmd_argv:
      if not args:
        self.Usage()
      cmd_argv.append('-e')
      cmd_argv.append(args[0])
      args = args[1:]

    projects = self.GetProjects(args)

    full_name = False
    if len(projects) > 1:
      cmd_argv.append('--full-name')
      full_name = True

    have_rev = False
    if opt.revision:
      if '--cached' in cmd_argv:
        print >>sys.stderr,\
          'fatal: cannot combine --cached and --revision'
        sys.exit(1)
      have_rev = True
      cmd_argv.extend(opt.revision)
    cmd_argv.append('--')

    bad_rev = False
    have_match = False

    for project in projects:
      p = GitCommand(project,
                     cmd_argv,
                     bare = False,
                     capture_stdout = True,
                     capture_stderr = True)
      if p.Wait() != 0:
        # no results
        #
        if p.stderr:
          if have_rev and 'fatal: ambiguous argument' in p.stderr:
            bad_rev = True
          else:
            out.project('--- project %s ---' % project.relpath)
            out.nl()
            out.write(p.stderr)
            out.nl()
        continue
      have_match = True

      # We cut the last element, to avoid a blank line.
      #
      r = p.stdout.split('\n')
      r = r[0:-1]

      if have_rev and full_name:
        for line in r:
          rev, line = line.split(':', 1)
          out.write(rev)
          out.write(':')
          out.project(project.relpath)
          out.write('/')
          out.write(line)
          out.nl()
      elif full_name:
        for line in r:
          out.project(project.relpath)
          out.write('/')
          out.write(line)
          out.nl()
      else:
        for line in r:
          print line

    if have_match:
      sys.exit(0)
    elif have_rev and bad_rev:
      for r in opt.revision:
        print >>sys.stderr, "error: can't search revision %s" % r
      sys.exit(1)
    else:
      sys.exit(1)
<commit_msg>Clarify the option section header in 'repo help grep'<commit_after>#
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from optparse import SUPPRESS_HELP
from color import Coloring
from command import PagedCommand
from git_command import GitCommand

class GrepColoring(Coloring):
  def __init__(self, config):
    Coloring.__init__(self, config, 'grep')
    self.project = self.printer('project', attr='bold')

class Grep(PagedCommand):
  common = True
  helpSummary = "Print lines matching a pattern"
  helpUsage = """
%prog {pattern | -e pattern} [<project>...]
"""
  helpDescription = """
Search for the specified patterns in all project files.

Boolean Options
---------------

The following options can appear as often as necessary to express
the pattern to locate:

 -e PATTERN
 --and, --or, --not, -(, -)

Further, the -r/--revision option may be specified multiple times
in order to scan multiple trees.  If the same file matches in more
than one tree, only the first result is reported, prefixed by the
revision name it was found under.

Examples
-------

Look for a line that has '#define' and either 'MAX_PATH or 'PATH_MAX':

  repo grep -e '#define' --and -\( -e MAX_PATH -e PATH_MAX \)

Look for a line that has 'NODE' or 'Unexpected' in files that
contain a line that matches both expressions:

  repo grep --all-match -e NODE -e Unexpected

"""

  def _Options(self, p):
    def carry(option,
              opt_str,
              value,
              parser):
      pt = getattr(parser.values, 'cmd_argv', None)
      if pt is None:
        pt = []
        setattr(parser.values, 'cmd_argv', pt)

      if opt_str == '-(':
        pt.append('(')
      elif opt_str == '-)':
        pt.append(')')
      else:
        pt.append(opt_str)

      if value is not None:
        pt.append(value)

    g = p.add_option_group('Sources')
    g.add_option('--cached',
                 action='callback', callback=carry,
                 help='Search the index, instead of the work tree')
    g.add_option('-r','--revision',
                 dest='revision', action='append', metavar='TREEish',
                 help='Search TREEish, instead of the work tree')

    g = p.add_option_group('Pattern')
    g.add_option('-e',
                 action='callback', callback=carry,
                 metavar='PATTERN', type='str',
                 help='Pattern to search for')
    g.add_option('-i', '--ignore-case',
                 action='callback', callback=carry,
                 help='Ignore case differences')
    g.add_option('-a','--text',
                 action='callback', callback=carry,
                 help="Process binary files as if they were text")
    g.add_option('-I',
                 action='callback', callback=carry,
                 help="Don't match the pattern in binary files")
    g.add_option('-w', '--word-regexp',
                 action='callback', callback=carry,
                 help='Match the pattern only at word boundaries')
    g.add_option('-v', '--invert-match',
                 action='callback', callback=carry,
                 help='Select non-matching lines')
    g.add_option('-G', '--basic-regexp',
                 action='callback', callback=carry,
                 help='Use POSIX basic regexp for patterns (default)')
    g.add_option('-E', '--extended-regexp',
                 action='callback', callback=carry,
                 help='Use POSIX extended regexp for patterns')
    g.add_option('-F', '--fixed-strings',
                 action='callback', callback=carry,
                 help='Use fixed strings (not regexp) for pattern')

    g = p.add_option_group('Pattern Grouping')
    g.add_option('--all-match',
                 action='callback', callback=carry,
                 help='Limit match to lines that have all patterns')
    g.add_option('--and', '--or', '--not',
                 action='callback', callback=carry,
                 help='Boolean operators to combine patterns')
    g.add_option('-(','-)',
                 action='callback', callback=carry,
                 help='Boolean operator grouping')

    g = p.add_option_group('Output')
    g.add_option('-n',
                 action='callback', callback=carry,
                 help='Prefix the line number to matching lines')
    g.add_option('-C',
                 action='callback', callback=carry,
                 metavar='CONTEXT', type='str',
                 help='Show CONTEXT lines around match')
    g.add_option('-B',
                 action='callback', callback=carry,
                 metavar='CONTEXT', type='str',
                 help='Show CONTEXT lines before match')
    g.add_option('-A',
                 action='callback', callback=carry,
                 metavar='CONTEXT', type='str',
                 help='Show CONTEXT lines after match')
    g.add_option('-l','--name-only','--files-with-matches',
                 action='callback', callback=carry,
                 help='Show only file names containing matching lines')
    g.add_option('-L','--files-without-match',
                 action='callback', callback=carry,
                 help='Show only file names not containing matching lines')

  def Execute(self, opt, args):
    out = GrepColoring(self.manifest.manifestProject.config)

    cmd_argv = ['grep']
    if out.is_on:
      cmd_argv.append('--color')
    cmd_argv.extend(getattr(opt,'cmd_argv',[]))

    if '-e' not in cmd_argv:
      if not args:
        self.Usage()
      cmd_argv.append('-e')
      cmd_argv.append(args[0])
      args = args[1:]

    projects = self.GetProjects(args)

    full_name = False
    if len(projects) > 1:
      cmd_argv.append('--full-name')
      full_name = True

    have_rev = False
    if opt.revision:
      if '--cached' in cmd_argv:
        print >>sys.stderr,\
          'fatal: cannot combine --cached and --revision'
        sys.exit(1)
      have_rev = True
      cmd_argv.extend(opt.revision)
    cmd_argv.append('--')

    bad_rev = False
    have_match = False

    for project in projects:
      p = GitCommand(project,
                     cmd_argv,
                     bare = False,
                     capture_stdout = True,
                     capture_stderr = True)
      if p.Wait() != 0:
        # no results
        #
        if p.stderr:
          if have_rev and 'fatal: ambiguous argument' in p.stderr:
            bad_rev = True
          else:
            out.project('--- project %s ---' % project.relpath)
            out.nl()
            out.write(p.stderr)
            out.nl()
        continue
      have_match = True

      # We cut the last element, to avoid a blank line.
      #
      r = p.stdout.split('\n')
      r = r[0:-1]

      if have_rev and full_name:
        for line in r:
          rev, line = line.split(':', 1)
          out.write(rev)
          out.write(':')
          out.project(project.relpath)
          out.write('/')
          out.write(line)
          out.nl()
      elif full_name:
        for line in r:
          out.project(project.relpath)
          out.write('/')
          out.write(line)
          out.nl()
      else:
        for line in r:
          print line

    if have_match:
      sys.exit(0)
    elif have_rev and bad_rev:
      for r in opt.revision:
        print >>sys.stderr, "error: can't search revision %s" % r
      sys.exit(1)
    else:
      sys.exit(1)
<|endoftext|>


<commit_before>#  Copyright 2017 Giovanni Fusco
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
#  Author: Giovanni Fusco - giofusco@gmail.com

import numpy as np
import colorsys
import pyglitch.core as pgc
from numba import jit, prange

PADDING_RANDOM = 100
PADDING_CIRCULAR = 101
PADDING_COLOR = 102
DIRECTION_LEFT = 104
DIRECTION_RIGHT = 105
DIRECTION_UP = 106
DIRECTION_DOWN = 107

# TODO: handle different kinds of paddings
def shift_rows(I, start, num_rows, offset, padding, color=(0,0,0)):
    pad = None
    if padding == PADDING_CIRCULAR:
        rows = np.roll(I[start:start+num_rows-1,0:-1], offset, axis=1)
        I[start:start + num_rows - 1, 0:-1] = rows
    return I

def swap_channels_at(I, x, y, w, h, channel_idxs):
    patch = pgc.get_patch(I, x, y, w, h)
    if patch is not None:
        swap_channels(patch[4], channel_idxs)
        # patch_swapped = (int(x), int(y), int(w), int(h), img_swapped)
        pgc.put_patch_in_place(I, patch)
    return I

def swap_channels(I, channel_idxs):
    c0 = I[:-1, :-1, channel_idxs[0]].copy()
    c1 = I[:-1, :-1, channel_idxs[1]].copy()
    c2 = I[:-1, :-1, channel_idxs[2]].copy()
    I[:-1, :-1, 0] = c0
    I[:-1, :-1, 1] = c1
    I[:-1, :-1, 2] = c2
    return I

def shift_channel_hor_at(I, x, y, w, h, channel_idx, offset):
    patch = pgc.get_patch(I, x, y, w, h)
    if patch is not None:
        shift_channel_hor(patch[4], channel_idx, offset)
        pgc.put_patch_in_place(I, patch)
    return I

def shift_channel_hor(I, channel_idx, offset):
    rows = np.roll(I[:-1,:-1,channel_idx], offset, axis=1)
    I[:-1, :-1, channel_idx] = rows
    return I

def shift_channel_ver(I, channel_idx, offset):
    cols = np.roll(I[:-1,:-1,channel_idx], offset, axis=0)
    I[:-1, :-1, channel_idx] = cols
    return I

def shift_channel_ver_at(I, x, y, w, h, channel_idx, offset):
    patch = pgc.get_patch(I, x, y, w, h)
    if patch is not None:
        shift_channel_ver(patch[4], channel_idx, offset)
        pgc.put_patch_in_place(I, patch)
    return I

def saturate_channel_at(I, x, y, w, h, channel_idx):
    """saturate a channel in a specific image patch"""
    patch = pgc.get_patch(I, x, y, w, h)
    if patch is not None:
        saturate_channel(patch[4], channel_idx)
        pgc.put_patch_in_place(I, patch)
    return I

def saturate_channel(I, channel_idx):
    """"saturates a channel of the image"""
    set_channel_value(I, channel_idx, 255)
    return I

def set_channel_value(I, channel_idx, value):
    if channel_idx < pgc.num_channels(I):
        I[:-1,:-1,channel_idx] = value
    return I

def posterize(I, num_bins, normalize=True):
    if normalize:
        I = rescale_image(I)
    bin_size = int(255/num_bins)
    I = 255-(255 * np.round(I/255*num_bins)/num_bins)
    return I.astype(np.uint8)

def pixel_sort_brighter_than_rgb_vert(I, r, g, b, strict=False, sorting_order=('h', 'l', 'v'), iterations=8):
    I = pixel_sort_brighter_than_rgb(pgc.rotate_right(I), r, g, b,
                                     strict=False, sorting_order=('h', 'l', 'v'), iterations=8)
    I = pgc.rotate_left(I)
    return I

@jit
def pixel_sort_brighter_than_rgb(I, r, g, b, strict=False, sorting_order=('h', 'l', 'v'), iterations=8):
    """begin sorting when it finds a pixel which is not (r,g,b) in the column or row,
        and will stop sorting when it finds a (r,g,b) pixel"""
    for row in prange(0, pgc.height(I)):
        if strict:
            from_idx = np.argwhere((I[row, :-1, pgc.CH_RED] > r) & (I[row, :-1, pgc.CH_GREEN] > g)
                                   & (I[row, :-1, pgc.CH_BLUE] > b))
        else:
            from_idx = np.argwhere((I[row, :-1, pgc.CH_RED] > r) | (I[row, :-1, pgc.CH_GREEN] > g)
                                   | (I[row, :-1, pgc.CH_BLUE] > b))

        to_idx = np.argwhere((I[row, :-1, pgc.CH_RED] <= r) & (I[row, :-1, pgc.CH_GREEN] <= g)
                             & (I[row, :-1, pgc.CH_BLUE] <= b))

        if from_idx.size > 0 and to_idx.size > 0:
            i = from_idx[0][0]
            matches = np.argwhere(to_idx > i)
            while not matches.size == 0:
                j = to_idx[matches[0][0]][0]
                I_hlv = _rgb2hlv(I[row, i:j, 0:3], iterations)
                sort_idx = np.argsort(I_hlv, order=sorting_order)
                I[row, i:j] = I[row, i+sort_idx]
                matches_i = np.argwhere(from_idx > j)
                if matches_i.size == 0:
                    break
                else:
                    i = from_idx[matches_i[0][0]][0]
                    matches = np.argwhere(to_idx > i)
    return I

# not sure it's necessary
def rescale_image_rgb(I):
    I_r = rescale_image(I[:,:,0])
    I_g = rescale_image(I[:, :, 1])
    I_b = rescale_image(I[:, :, 2])
    I = np.dstack([I_r, I_g, I_b])
    return I

def rescale_image(I):
    if (I.min() < 0 or I.max() > 255):
        I = (I - I.min()) * (255 / (I.max() - I.min()))
        I = 255-I.round()
    return I

# from http://www.alanzucconi.com/2015/09/30/colour-sorting/
@jit
def _rgb2hlv(I_rgb, repetitions=1):
    I_hlv = []
    for p in prange(0,len(I_rgb)):
        r = I_rgb[p, pgc.CH_RED]
        g = I_rgb[p, pgc.CH_GREEN]
        b = I_rgb[p, pgc.CH_BLUE]

        lum = np.sqrt(.241 * r + .691 * g + .068 * b)

        h, s, v = colorsys.rgb_to_hsv(r, g, b)

        h2 = int(h * repetitions)
        lum2 = int(lum * repetitions)
        v2 = int(v * repetitions)

        if h2 % 2 == 1:
            v2 = repetitions - v2
            lum = repetitions - lum2

        I_hlv.append((h2, lum, v2))

    return np.array(I_hlv, dtype=[('h', '<i4'), ('l', '<i4'), ('v', '<i4')])
<commit_msg>fixed an issue in image rescaling<commit_after>#  Copyright 2017 Giovanni Fusco
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
#  Author: Giovanni Fusco - giofusco@gmail.com

import numpy as np
import colorsys
import pyglitch.core as pgc
from numba import jit, prange

PADDING_RANDOM = 100
PADDING_CIRCULAR = 101
PADDING_COLOR = 102
DIRECTION_LEFT = 104
DIRECTION_RIGHT = 105
DIRECTION_UP = 106
DIRECTION_DOWN = 107

# TODO: handle different kinds of paddings
def shift_rows(I, start, num_rows, offset, padding, color=(0,0,0)):
    pad = None
    if padding == PADDING_CIRCULAR:
        rows = np.roll(I[start:start+num_rows-1,0:-1], offset, axis=1)
        I[start:start + num_rows - 1, 0:-1] = rows
    return I

def swap_channels_at(I, x, y, w, h, channel_idxs):
    patch = pgc.get_patch(I, x, y, w, h)
    if patch is not None:
        swap_channels(patch[4], channel_idxs)
        # patch_swapped = (int(x), int(y), int(w), int(h), img_swapped)
        pgc.put_patch_in_place(I, patch)
    return I

def swap_channels(I, channel_idxs):
    c0 = I[:-1, :-1, channel_idxs[0]].copy()
    c1 = I[:-1, :-1, channel_idxs[1]].copy()
    c2 = I[:-1, :-1, channel_idxs[2]].copy()
    I[:-1, :-1, 0] = c0
    I[:-1, :-1, 1] = c1
    I[:-1, :-1, 2] = c2
    return I

def shift_channel_hor_at(I, x, y, w, h, channel_idx, offset):
    patch = pgc.get_patch(I, x, y, w, h)
    if patch is not None:
        shift_channel_hor(patch[4], channel_idx, offset)
        pgc.put_patch_in_place(I, patch)
    return I

def shift_channel_hor(I, channel_idx, offset):
    rows = np.roll(I[:-1,:-1,channel_idx], offset, axis=1)
    I[:-1, :-1, channel_idx] = rows
    return I

def shift_channel_ver(I, channel_idx, offset):
    cols = np.roll(I[:-1,:-1,channel_idx], offset, axis=0)
    I[:-1, :-1, channel_idx] = cols
    return I

def shift_channel_ver_at(I, x, y, w, h, channel_idx, offset):
    patch = pgc.get_patch(I, x, y, w, h)
    if patch is not None:
        shift_channel_ver(patch[4], channel_idx, offset)
        pgc.put_patch_in_place(I, patch)
    return I

def saturate_channel_at(I, x, y, w, h, channel_idx):
    """saturate a channel in a specific image patch"""
    patch = pgc.get_patch(I, x, y, w, h)
    if patch is not None:
        saturate_channel(patch[4], channel_idx)
        pgc.put_patch_in_place(I, patch)
    return I

def saturate_channel(I, channel_idx):
    """"saturates a channel of the image"""
    set_channel_value(I, channel_idx, 255)
    return I

def set_channel_value(I, channel_idx, value):
    if channel_idx < pgc.num_channels(I):
        I[:-1,:-1,channel_idx] = value
    return I

def posterize(I, num_bins, normalize=True):
    if normalize:
        I = rescale_image(I)
    bin_size = int(255/num_bins)
    I = (255 * np.round(I/255*num_bins)/num_bins)
    return I.astype(np.uint8)

def pixel_sort_brighter_than_rgb_vert(I, r, g, b, strict=False, sorting_order=('h', 'l', 'v'), iterations=8):
    I = pixel_sort_brighter_than_rgb(pgc.rotate_right(I), r, g, b,
                                     strict=False, sorting_order=('h', 'l', 'v'), iterations=8)
    I = pgc.rotate_left(I)
    return I

@jit
def pixel_sort_brighter_than_rgb(I, r, g, b, strict=False, sorting_order=('h', 'l', 'v'), iterations=8):
    """begin sorting when it finds a pixel which is not (r,g,b) in the column or row,
        and will stop sorting when it finds a (r,g,b) pixel"""
    for row in prange(0, pgc.height(I)):
        if strict:
            from_idx = np.argwhere((I[row, :-1, pgc.CH_RED] > r) & (I[row, :-1, pgc.CH_GREEN] > g)
                                   & (I[row, :-1, pgc.CH_BLUE] > b))
        else:
            from_idx = np.argwhere((I[row, :-1, pgc.CH_RED] > r) | (I[row, :-1, pgc.CH_GREEN] > g)
                                   | (I[row, :-1, pgc.CH_BLUE] > b))

        to_idx = np.argwhere((I[row, :-1, pgc.CH_RED] <= r) & (I[row, :-1, pgc.CH_GREEN] <= g)
                             & (I[row, :-1, pgc.CH_BLUE] <= b))

        if from_idx.size > 0 and to_idx.size > 0:
            i = from_idx[0][0]
            matches = np.argwhere(to_idx > i)
            while not matches.size == 0:
                j = to_idx[matches[0][0]][0]
                I_hlv = _rgb2hlv(I[row, i:j, 0:3], iterations)
                sort_idx = np.argsort(I_hlv, order=sorting_order)
                I[row, i:j] = I[row, i+sort_idx]
                matches_i = np.argwhere(from_idx > j)
                if matches_i.size == 0:
                    break
                else:
                    i = from_idx[matches_i[0][0]][0]
                    matches = np.argwhere(to_idx > i)
    return I

# not sure it's necessary
def rescale_image_rgb(I):
    I_r = rescale_image(I[:,:,0])
    I_g = rescale_image(I[:, :, 1])
    I_b = rescale_image(I[:, :, 2])
    I = np.dstack([I_r, I_g, I_b])
    return  I.astype(np.uint8)

def rescale_image(I):
    if (I.min() < 0 or I.max() > 255):
        I = (I - I.min()) * (255 / (I.max() - I.min()))
        I = I.round()
    return I.astype(np.uint8)

# from http://www.alanzucconi.com/2015/09/30/colour-sorting/
@jit
def _rgb2hlv(I_rgb, repetitions=1):
    I_hlv = []
    for p in prange(0,len(I_rgb)):
        r = I_rgb[p, pgc.CH_RED]
        g = I_rgb[p, pgc.CH_GREEN]
        b = I_rgb[p, pgc.CH_BLUE]

        lum = np.sqrt(.241 * r + .691 * g + .068 * b)

        h, s, v = colorsys.rgb_to_hsv(r, g, b)

        h2 = int(h * repetitions)
        lum2 = int(lum * repetitions)
        v2 = int(v * repetitions)

        if h2 % 2 == 1:
            v2 = repetitions - v2
            lum = repetitions - lum2

        I_hlv.append((h2, lum, v2))

    return np.array(I_hlv, dtype=[('h', '<i4'), ('l', '<i4'), ('v', '<i4')])
<|endoftext|>


<commit_before>import rethinkdb as r
from tornado import ioloop, gen
from search import handle_query

r.set_loop_type("tornado")

@gen.coroutine
def print_changes(q):
    conn = yield r.connect(host="localhost", port=28015)
    feed = yield q.run(conn)
    while (yield feed.fetch_next()):
        change = yield feed.next()
        print(change)
        content = str(change)
        message = sendgrid.Mail()

        message.add_to(email)
        message.add_to('tim@insideyourgovernment.com')
        message.set_subject('Table change')
        message.set_text(content)
        message.set_html(content.replace('\n', '
'))
        message.set_from('tim@insideyourgovernment.com')
        p = r.db('nonpublic').table('third_party_creds').get('sendgrid'
        sg = sendgrid.SendGridClient(username, password)
        print sg.send(message)

@gen.coroutine
def main():
    q = handle_query({'table': 'test_table'}, run=False).changes()
    ioloop.IOLoop.current().add_callback(print_changes, q)

if __name__ == "__main__":
    ioloop.IOLoop.current().run_sync(main)
    ioloop.IOLoop.current().start()<commit_msg>auto commit<commit_after>import rethinkdb as r
from tornado import ioloop, gen
from search import handle_query

r.set_loop_type("tornado")

@gen.coroutine
def print_changes(q):
    conn = yield r.connect(host="localhost", port=28015)
    feed = yield q.run(conn)
    while (yield feed.fetch_next()):
        change = yield feed.next()
        print(change)
        content = str(change)
        message = sendgrid.Mail()

        message.add_to(email)
        message.add_to('tim@insideyourgovernment.com')
        message.set_subject('Table change')
        message.set_text(content)
        message.set_html(content.replace('\n', '
'))
        message.set_from('tim@insideyourgovernment.com')
        p = r.db('nonpublic').table('third_party_creds').get('sendgrid')
        p
        sg = sendgrid.SendGridClient(username, password)
        print sg.send(message)

@gen.coroutine
def main():
    q = handle_query({'table': 'test_table'}, run=False).changes()
    ioloop.IOLoop.current().add_callback(print_changes, q)

if __name__ == "__main__":
    ioloop.IOLoop.current().run_sync(main)
    ioloop.IOLoop.current().start()<|endoftext|>


<commit_before>from lxml import etree
from lxml import objectify
from collections import defaultdict
from sunspec_smdx import SMDX, SMDXPoint
import datetime
import hashlib, os, logging

# logger = logging.getLogger('ped.ssd')

class SunSpecData(object):
  element_name = 'sunSpecData'

  def __init__(self, element):
    '''Initialize a SunSpecData object given the XML Element
    '''
    self.element = element
    if self.element is None:
      self.exists = False
      return
    else: self.exists = True

    # check for sunSpecData version, assume '1' if absent
    v = self.element.get('v')
    if (v is not None): self.version = int(v)
    else: self.version = 1

    self.device_records = list()
    for d_record in self.element.iter(DeviceRecord.element_name):
      self.device_records.append(DeviceRecord(d_record))

  def parse(self):
    logging.debug("SunSpecData.parse()")
    for dr in self.device_records:
      dr.parse()

    print self.get_time_series_points('TmpBOM')

  def get_points(self):
    all_points = list()
    for dr in self.device_records:
      for model in dr.models:
        all_points = all_points + model.points
    return all_points

  def get_matching_points(self, point_id):
    '''Get a list of points which match the given point_id
    '''
    points = list()
    logging.debug("SunSpecData.get_matching_points() looking for point_id:"+str(point_id))
    for dr in self.device_records:
      models = dr.models
      if models is not None:
        for m in models:
          plist = m.get_matching_points(point_id)
          if (plist is not None) and (len(plist) > 0):
            points = points + plist
    return points

  def get_points_in_period(self, startTime, endTime):
    '''Get a list of points which are between the startTime and endTime.

       Arguments:
       startTime -- the datetime describing the beginning of the period
       endTime   -- the datetime describing the end of the period

       Return:
       a dictionary of lists -- the key is the time and the list contains Points
    '''
    now = datetime.datetime.now()
    if endTime is None:
      endTime = now
    if startTime is None:
      startTime = now

    logging.info("SunSpecData.get_points_in_period: " + str(startTime) +" > "+ str(endTime))

    points = self.get_points()
    period_points = defaultdict(list)
    for p in points:
      if startTime < p.time < endTime:
        period_points[p.time].append(p) # << BROKEN - the key is the same for multiple points with the same time
    print 'period points: ', period_points
    period_points = sorted(period_points.keys())

  def get_time_series_points(self, point_id=None):
    '''Get a list of points as a time keyed dictionary.
    '''
    logging.info("SunSpecData.get_time_series_points() point_id:" + str(point_id))
    ts_points = defaultdict(list)
    if point_id is None:
      points = self.get_points()
    else:
      points = self.get_matching_points(point_id)

    for p in points:
      ts_points[p.time].append(p) # << BROKEN - the key is the same for multiple points with the same time
    return ts_points

  def tostring(self):
    s = "SunSpecData v:" + str(self.version)
    s += "xml: " + etree.tostring(self.element, pretty_print=True)
    return s

class DeviceRecord(object):
  element_name = 'd'

  def __init__(self, element):
    element = element
    if element is None:
      logging.warning("DeviceRecord.__init__() element provided is None")
      return

    self.manufacturer = get_attr(element, 'man')
    self.model = get_attr(element, 'mod')
    self.serial_number = get_attr(element, 'sn')
    self.logger_id = get_attr(element, 'lid')
    self.logger_id_namespace = get_attr(element, 'ns')
    self.device_interface_id = get_attr(element, 'if')
    self.device_id = get_attr(element, 'id')
    self.correlation_id = get_attr(element, 'cid')

    self._determine_id()
    time = get_attr(element, 't')
    if (time is None) or (time is ''):
      raise SunSpecDataException("DeviceRecord 't' attribute is required.")
    else:
      # SunSpecData <d ... t="YYYY-MM-DDThh:mm:ssZ"
      self.time = datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%SZ')

    self.models = list()
    for m in element.iter(Model.element_name):
        m_id = m.get('id')
        if (m_id is not None):
          logging.info("DeviceRecord.__init__() adding model_id: " + m_id)
          self.models.append(Model(m, m_id, self.time))

  def _determine_id(self):
    self.device_id_type = None
    gs = None
    if (len(self.model) > 0 and len(self.manufacturer) > 0 and len(self.serial_number) > 0):
      gs = "" + self.serial_number + self.model + self.manufacturer
      self.device_id_type = 'Common Block'
    elif (len(self.logger_id) > 0 and len(self.device_id) > 0):
      gs = "" + self.device_id + self.logger_id
      self.device_id_type = 'Logger-specific, user-assigned'

    if gs is None:
      s = "A Device must have a globally unique identifier, either man: mod: sn: or lid: id:"
      raise SunSpecDataException(s)

    self.guid = int(hashlib.md5(gs).hexdigest(), 16)
    return

  def parse(self):
    logging.debug("SunSpecData.DeviceRecord.parse()")
    for model in self.models:
      model.parse()

  def tostring(self):
    s  = "Device" + os.linesep
    s += " id_type:'" + self.device_id_type + "' guid:" + str(self.guid) + os.linesep
    s += " man:" + self.manufacturer + " mod:" + self.model + " sn:" + self.serial_number + os.linesep
    s += " lid:" + self.logger_id + " ns:" + self.logger_id_namespace
    s += " id:"  + self.device_id + os.linesep
    s += " if:"  + self.device_interface_id + " cid:" + self.correlation_id

    return s

class Model(object):
  element_name = 'm'

  def __init__(self, element, id, dr_time):
    self.dr_time = dr_time
    self.points = list()
    if (element is None):
      raise SunSpecDataException("SunSpec model element is required for a Model")
    else:
      self.element = element

    if (id is None):
      raise SunSpecDataException("SunSpec model element must have an id attribute")
    else:
      self.id = int(id)

    self.smdx = SMDX(element, id)
    logging.info('Model constructed for model_id:' + str(id))

  def parse(self):
    logging.debug("Model.parse() model_id:" + str(self.id))

    logging.info("Model.parse() instantiating Points")
    for p in self.element.iter(Point.element_name):
      self.points.append(Point(self, p))

    smdx_points = self.smdx.get_points()
    logging.info("Model.parse() adding units from SMDXPoints to Points")
    for p in self.points:
      if p.id in smdx_points:
        logging.info("Point.id:" + str(p.id) + " found in SMDX.model_id: " + str(self.id))
        p.unit = smdx_points[p.id].units
      else:
        logging.warning("Point.id:"+ str(p.id) +
                        " exists but shouldn't for SMDX.model_id: " + str(self.id))

    mand_met = False
    if (len(self._has_mandatory_points()) == 0):
      mand_met = True
    logging.debug("Model.parse() has all mandatory points? " + str(mand_met))

  def get_matching_points(self, point_id):
    '''Get a list of points which match the given point_id
    '''
    points = list()
    logging.info("Model.get_matching_points() looking for point_id:"+str(point_id))
    for p in self.points:
      logging.debug("Model.get_matching_points() p.id:"+p.id +" given point_id:"+point_id)
      if p.id == point_id:
        points.append(p)

    return points

  def _has_mandatory_points(self):
    man_points = self.smdx.get_mandatory_points()
    points = self.points
    logging.debug("Model._has_mandatory_points()")
    missing_mand = {}

    for sp in man_points.itervalues():
      if sp.mandatory:
        found = False
        for p in points:
          if sp.id == p.id:
            print "Found mandatory point", p.id
            found = True
            break

        if not found:
          logging.warning("Model.id:" + str(self.id) +
                          " missing mandatory SMDXPoint.id:" + sp.id)
          missing_mand[sp.id] = sp

    return missing_mand

  def set_metadata(self, element):

    return

  def tostring(self):
    return ''.join(['Model.id:', str(self.id), ' dr_time:', str(self.dr_time)])

class Point(object):
  element_name = 'p'

  def __init__(self, model, element):
    self.model = model
    element = element
    self.id    = element.get('id')
    self.x     = element.get('x')
    self.text  = element.text
    self.units = None

    time = element.get('t')
    if time is None:
      self.time = model.dr_time
    else:
      # SunSpecData <p ... t="YYYY-MM-DDThh:mm:ssZ"
      self.time = datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%SZ')

  def tostring(self):
    return ''.join(['Point.id:', self.id, ' x:', str(self.x), ' text:', str(self.text),
                    ' units:', str(self.units), ' t:', str(self.time)])

class SunSpecDataException(Exception):

  def __init__(self, argument):
    super(SunSpecDataException, self).__init__(argument)
    self.argument = argument

####################
# Utility functions
def get_attr(element, attr_name):
  attr_value = ''
  if (element is None):
    return attr_value
  elif (element.get(attr_name) is not None):
    attr_value = element.get(attr_name)

  return attr_value
<commit_msg>finishes up the ability to get a Point of a type in a time range<commit_after>from lxml import etree
from lxml import objectify
from collections import defaultdict
from sunspec_smdx import SMDX, SMDXPoint
import datetime
import hashlib, os, logging
import sys

# logger = logging.getLogger('ped.ssd')

class SunSpecData(object):
  element_name = 'sunSpecData'

  def __init__(self, element):
    '''Initialize a SunSpecData object given the XML Element
    '''
    self.element = element
    if self.element is None:
      self.exists = False
      return
    else: self.exists = True

    # check for sunSpecData version, assume '1' if absent
    v = self.element.get('v')
    if (v is not None): self.version = int(v)
    else: self.version = 1

    self.device_records = list()
    for d_record in self.element.iter(DeviceRecord.element_name):
      self.device_records.append(DeviceRecord(d_record))

  def parse(self):
    logging.debug("SunSpecData.parse()")
    for dr in self.device_records:
      dr.parse()

  def get_points(self):
    all_points = list()
    for dr in self.device_records:
      for model in dr.models:
        all_points = all_points + model.points
    return all_points

  def get_matching_points(self, point_id):
    '''Get a list of points which match the given point_id
    '''
    points = list()
    logging.debug("SunSpecData.get_matching_points() looking for point_id:"+str(point_id))
    for dr in self.device_records:
      models = dr.models
      if models is not None:
        for m in models:
          plist = m.get_matching_points(point_id)
          if (plist is not None) and (len(plist) > 0):
            points = points + plist
    return points

  def get_points_in_period(self, startTime, endTime, point_id='All'):
    '''Get a list of points which are between the startTime and endTime.

       Arguments:
       startTime -- the datetime describing the beginning of the period
       endTime   -- the datetime describing the end of the period
       point_id  -- the id of the Points to retrieve within the period

       Return:
       Points in the period as a time keyed dictionary of Points in a list
    '''
    now = datetime.datetime.now()
    if endTime is None:
      endTime = now
    if startTime is None:
      startTime = now

    logging.info("SunSpecData.get_points_in_period: " + str(startTime) +" > "+ str(endTime))

    if point_id is 'All':
      points = self.get_points()
    else:
      points = self.get_matching_points(point_id)

    period_points = defaultdict(list)
    for p in points:
      if startTime < p.time < endTime:
        period_points[p.time].append(p)
    print 'period points: ', period_points
    period_points = sorted(period_points.keys())

  def get_time_keyed_points(self, point_id='All'):
    '''Get a Point.time keyed dictionary with Points possessing the same time in a list.

       Arguments:
       point_id -- the id of the Points to retrieve as a time keyed dict (default: All)

       Return:
       Points as a time keyed dictionary of Points in a list
    '''
    logging.info("SunSpecData.get_time_series_points() point_id:" + str(point_id))
    ts_points = defaultdict(list)
    if point_id is 'All':
      points = self.get_points()
    else:
      points = self.get_matching_points(point_id)

    for p in points:
      ts_points[p.time].append(p)
    return ts_points

  def tostring(self):
    s = "SunSpecData v:" + str(self.version)
    s += "xml: " + etree.tostring(self.element, pretty_print=True)
    return s

class DeviceRecord(object):
  element_name = 'd'

  def __init__(self, element):
    element = element
    if element is None:
      logging.warning("DeviceRecord.__init__() element provided is None")
      return

    self.manufacturer = get_attr(element, 'man')
    self.model = get_attr(element, 'mod')
    self.serial_number = get_attr(element, 'sn')
    self.logger_id = get_attr(element, 'lid')
    self.logger_id_namespace = get_attr(element, 'ns')
    self.device_interface_id = get_attr(element, 'if')
    self.device_id = get_attr(element, 'id')
    self.correlation_id = get_attr(element, 'cid')

    self._determine_id()
    time = get_attr(element, 't')
    if (time is None) or (time is ''):
      raise SunSpecDataException("DeviceRecord 't' attribute is required.")
    else:
      # SunSpecData <d ... t="YYYY-MM-DDThh:mm:ssZ"
      self.time = datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%SZ')

    self.models = list()
    for m in element.iter(Model.element_name):
        m_id = m.get('id')
        if (m_id is not None):
          logging.info("DeviceRecord.__init__() adding model_id: " + m_id)
          self.models.append(Model(m, m_id, self.time))

  def _determine_id(self):
    self.device_id_type = None
    gs = None
    if (len(self.model) > 0 and len(self.manufacturer) > 0 and len(self.serial_number) > 0):
      gs = "" + self.serial_number + self.model + self.manufacturer
      self.device_id_type = 'Common Block'
    elif (len(self.logger_id) > 0 and len(self.device_id) > 0):
      gs = "" + self.device_id + self.logger_id
      self.device_id_type = 'Logger-specific, user-assigned'

    if gs is None:
      s = "A Device must have a globally unique identifier, either man: mod: sn: or lid: id:"
      raise SunSpecDataException(s)

    self.guid = int(hashlib.md5(gs).hexdigest(), 16)
    return

  def parse(self):
    logging.debug("SunSpecData.DeviceRecord.parse()")
    for model in self.models:
      model.parse()

  def tostring(self):
    s  = "Device" + os.linesep
    s += " id_type:'" + self.device_id_type + "' guid:" + str(self.guid) + os.linesep
    s += " man:" + self.manufacturer + " mod:" + self.model + " sn:" + self.serial_number + os.linesep
    s += " lid:" + self.logger_id + " ns:" + self.logger_id_namespace
    s += " id:"  + self.device_id + os.linesep
    s += " if:"  + self.device_interface_id + " cid:" + self.correlation_id

    return s

class Model(object):
  element_name = 'm'

  def __init__(self, element, id, dr_time):
    self.dr_time = dr_time
    self.points = list()
    if (element is None):
      raise SunSpecDataException("SunSpec model element is required for a Model")
    else:
      self.element = element

    if (id is None):
      raise SunSpecDataException("SunSpec model element must have an id attribute")
    else:
      self.id = int(id)

    self.smdx = SMDX(element, id)
    logging.info('Model constructed for model_id:' + str(id))

  def parse(self):
    logging.debug("Model.parse() model_id:" + str(self.id))

    logging.info("Model.parse() instantiating Points")
    for p in self.element.iter(Point.element_name):
      self.points.append(Point(self, p))

    smdx_points = self.smdx.get_points()
    logging.info("Model.parse() adding units from SMDXPoints to Points")
    for p in self.points:
      if p.id in smdx_points:
        logging.info("Point.id:" + str(p.id) + " found in SMDX.model_id: " + str(self.id))
        p.unit = smdx_points[p.id].units
      else:
        logging.warning("Point.id:"+ str(p.id) +
                        " exists but shouldn't for SMDX.model_id: " + str(self.id))

    mand_met = False
    if (len(self._has_mandatory_points()) == 0):
      mand_met = True
    logging.debug("Model.parse() has all mandatory points? " + str(mand_met))

  def get_matching_points(self, point_id):
    '''Get a list of points which match the given point_id
    '''
    points = list()
    logging.info("Model.get_matching_points() looking for point_id:"+str(point_id))
    for p in self.points:
      logging.debug("Model.get_matching_points() p.id:"+p.id +" given point_id:"+point_id)
      if p.id == point_id:
        points.append(p)

    return points

  def _has_mandatory_points(self):
    man_points = self.smdx.get_mandatory_points()
    points = self.points
    logging.debug("Model._has_mandatory_points()")
    missing_mand = {}

    for sp in man_points.itervalues():
      if sp.mandatory:
        found = False
        for p in points:
          if sp.id == p.id:
            logging.info("Found mandatory point:", str(p.id))
            found = True
            break

        if not found:
          logging.warning("Model.id:" + str(self.id) +
                          " missing mandatory SMDXPoint.id:" + sp.id)
          missing_mand[sp.id] = sp

    return missing_mand

  def set_metadata(self, element):

    return

  def tostring(self):
    return ''.join(['Model.id:', str(self.id), ' dr_time:', str(self.dr_time)])

class Point(object):
  element_name = 'p'

  def __init__(self, model, element):
    self.model = model
    element = element
    self.id    = element.get('id')
    self.x     = element.get('x')
    self.text  = element.text
    self.units = None

    time = element.get('t')
    if time is None:
      self.time = model.dr_time
    else:
      # SunSpecData <p ... t="YYYY-MM-DDThh:mm:ssZ"
      self.time = datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%SZ')

  def tostring(self):
    return ''.join(['Point.id:', self.id, ' x:', str(self.x), ' text:', str(self.text),
                    ' units:', str(self.units), ' t:', str(self.time)])

class SunSpecDataException(Exception):

  def __init__(self, argument):
    super(SunSpecDataException, self).__init__(argument)
    self.argument = argument

####################
# Utility functions
def get_attr(element, attr_name):
  attr_value = ''
  if (element is None):
    return attr_value
  elif (element.get(attr_name) is not None):
    attr_value = element.get(attr_name)

  return attr_value
<|endoftext|>


<commit_before>""" Utilities for modifying strings"""

import os
import warnings
import re
import functools

def preprocess(string):
    """
    Preprocesses a string, by replacing ${VARNAME} with
    os.environ['VARNAME']

    Parameters
    ----------
    string: the str object to preprocess

    Returns
    -------
    the preprocessed string
    """

    split = string.split('${')

    rval = [split[0]]

    for candidate in split[1:]:
        subsplit = candidate.split('}')

        if len(subsplit) < 2:
            raise ValueError('Open ${ not followed by } before ' \
                    + 'end of string or next ${ in "' \
                    + string + '"')

        varname = subsplit[0]

        if varname == 'PYLEARN2_TRAIN_FILE_NAME':
            warnings.warn("PYLEARN2_TRAIN_FILE_NAME is deprecated, use PYLEARN2_TRAIN_FILE_FULL_STEM")

        try:
            val = os.environ[varname]
        except KeyError:
            if varname == 'PYLEARN2_DATA_PATH':
                raise EnvironmentVariableError("You need to define your PYLEARN2_DATA_PATH environment variable. If you are using a computer at LISA, this should be set to /data/lisa/data")
            if varname == 'PYLEARN2_VIEWER_COMMAND':
                raise EnvironmentVariableError(
"""
PYLEARN2_VIEWER_COMMAND not defined.

pylearn2 uses an external program to display images. Because different systems have different
image programs available, pylearn2 requires the user to specify what image viewer program to
use.

You need to choose an image viewer program that pylearn2 should use. Then tell pylearn2 to use
that image viewer program by defining your PYLEARN2_VIEWER_COMMAND environment variable.

You need to choose PYLEARN_VIEWER_COMMAND such that running

${PYLEARN2_VIEWER_COMMAND} image.png

in a command prompt on your machine will do the following:
    -open an image viewer in a new process.
    -not return until you have closed the image.

Acceptable commands include:
    gwenview
    eog --new-instance

This is assuming that you have gwenview or a version of eog that supports --new-instance
installed on your machine. If you don't, install one of those, or figure out a command
that has the above properties that is available from your setup.

On most linux setups, you can define your environment variable by adding this line to your
~/.bashrc file:

export PYLEARN2_VIEWER_COMMAND="eog --new-instance"

Don't forget that changes from your .bashrc file won't apply until you run

source ~/.bashrc

or open a new terminal window. If you're seeing this from an ipython notebook
you'll need to restart the ipython notebook, or maybe modify os.environ from
an ipython cell.
""")

            raise

        rval.append(val)

        rval.append('}'.join(subsplit[1:]))

    rval = ''.join(rval)

    return rval

class EnvironmentVariableError(Exception):
    """ An exception raised when a required environment variable is not defined """

    def __init__(self, *args):
        super(EnvironmentVariableError,self).__init__(*args)

def find_number(s):
    """ s is a string
        returns None if there are no numbers in the string
        otherwise returns the range of characters occupied by the first
        number in the string """

    r = re.search('-?\d+[.e]?\d*',s)
    if r is not None:
        return r.span(0)
    return None

def tokenize_by_number(s):
    """ splits a string into a list of tokens
        each is either a string containing no numbers
        or a float """

    r = find_number(s)

    if r == None:
        return [ s ]
    else:
        tokens = []
        if r[0] > 0:
            tokens.append(s[0:r[0]])
        tokens.append( float(s[r[0]:r[1]]) )
        if r[1] < len(s):
            tokens.extend(tokenize_by_number(s[r[1]:]))
        return tokens
    assert False #line should be unreached

def number_aware_alphabetical_cmp(str1, str2):
    """ cmp function for sorting a list of strings by alphabetical order, but with
        numbers sorted numerically.

        i.e., foo1, foo2, foo10, foo11
        instead of foo1, foo10
    """

    def flatten_tokens(tokens):
        l = []
        for token in tokens:
            if isinstance(token, str):
                for char in token:
                    l.append(char)
            else:
                assert isinstance(token, float)
                l.append(token)
        return l

    seq1 = flatten_tokens(tokenize_by_number(str1))
    seq2 = flatten_tokens(tokenize_by_number(str2))

    l = min(len(seq1),len(seq2))

    i = 0

    while i < l:
        if seq1[i] < seq2[i]:
            return -1
        elif seq1[i] > seq2[i]:
            return 1
        i += 1

    if len(seq1) < len(seq2):
        return -1
    elif len(seq1) > len(seq2):
        return 1

    return 0

#key for sorting strings alphabetically with numbers
number_aware_alphabetical_key = functools.cmp_to_key(number_aware_alphabetical_cmp)

def match(wrong, candidates):
    """
        wrong: a mispelling
        candidates: a set of correct words

        returns a guess of which candidate is the right one

        This should be used with a small number of candidates and a high potential
        edit distance.
        ie, use it to correct a wrong filename in a directory, wrong class name
        in a module, etc. Don't use it to correct small typos of freeform natural
        language words.
    """

    assert len(candidates) > 0

    # Current implementation tries all candidates and outputs the one
    # with the min score
    # Could try to do something smarter

    def score(w1,w2):
        # Current implementation returns negative dot product of
        # the two words mapped into a feature space by mapping phi
        # w -> [ phi(w1), .1 phi(first letter of w), .1 phi(last letter of w) ]
        # Could try to do something smarter

        w1 = w1.lower()
        w2 = w2.lower()

        def phi(w):
            # Current feature mapping is to the vector of counts of
            # all letters and two-letter sequences
            # Could try to do something smarter
            rval = {}

            for i in xrange(len(w)):
                l = w[i]
                rval[l] = rval.get(l,0.) + 1.
                if i < len(w)-1:
                    b = w[i:i+2]
                    rval[b] = rval.get(b,0.) + 1.

            return rval

        d1 = phi(w1)
        d2 = phi(w2)

        def mul(d1, d2):
            rval = 0

            for key in set(d1).union(d2):
                rval += d1.get(key,0) * d2.get(key,0)

            return rval

        tot_score = mul(phi(w1),phi(w2)) / float(len(w1)*len(w2)) + \
            0.1 * mul(phi(w1[0:1]), phi(w2[0:1])) + \
            0.1 * mul(phi(w1[-1:]), phi(w2[-1:]))

        return  tot_score

    scored_candidates = [ (-score(wrong, candidate), candidate)
            for candidate in candidates ]

    scored_candidates.sort()

    return scored_candidates[0][1]
<commit_msg>added more info to the environment variables essay<commit_after>""" Utilities for modifying strings"""

import os
import warnings
import re
import functools

def preprocess(string):
    """
    Preprocesses a string, by replacing ${VARNAME} with
    os.environ['VARNAME']

    Parameters
    ----------
    string: the str object to preprocess

    Returns
    -------
    the preprocessed string
    """

    split = string.split('${')

    rval = [split[0]]

    for candidate in split[1:]:
        subsplit = candidate.split('}')

        if len(subsplit) < 2:
            raise ValueError('Open ${ not followed by } before ' \
                    + 'end of string or next ${ in "' \
                    + string + '"')

        varname = subsplit[0]

        if varname == 'PYLEARN2_TRAIN_FILE_NAME':
            warnings.warn("PYLEARN2_TRAIN_FILE_NAME is deprecated, use PYLEARN2_TRAIN_FILE_FULL_STEM")

        try:
            val = os.environ[varname]
        except KeyError:
            if varname == 'PYLEARN2_DATA_PATH':
                raise EnvironmentVariableError("You need to define your PYLEARN2_DATA_PATH environment variable. If you are using a computer at LISA, this should be set to /data/lisa/data")
            if varname == 'PYLEARN2_VIEWER_COMMAND':
                raise EnvironmentVariableError(
"""
PYLEARN2_VIEWER_COMMAND not defined. PLEASE READ THE FOLLOWING MESSAGE CAREFULLy
TO SET UP THIS ENVIRONMENT VARIABLE:

pylearn2 uses an external program to display images. Because different systems have different
image programs available, pylearn2 requires the user to specify what image viewer program to
use.

You need to choose an image viewer program that pylearn2 should use. Then tell pylearn2 to use
that image viewer program by defining your PYLEARN2_VIEWER_COMMAND environment variable.

You need to choose PYLEARN_VIEWER_COMMAND such that running

${PYLEARN2_VIEWER_COMMAND} image.png

in a command prompt on your machine will do the following:
    -open an image viewer in a new process.
    -not return until you have closed the image.

Acceptable commands include:
    gwenview
    eog --new-instance

This is assuming that you have gwenview or a version of eog that supports --new-instance
installed on your machine. If you don't, install one of those, or figure out a command
that has the above properties that is available from your setup.

On most linux setups, you can define your environment variable by adding this line to your
~/.bashrc file:

export PYLEARN2_VIEWER_COMMAND="eog --new-instance"

*** YOU MUST INCLUDE THE WORD "export". DO NOT JUST ASSIGN TO THE ENVIRONMENT VARIABLE ***
If you do not include the word "export", the environment variable will be set in your
bash shell, but will not be visible to processes that you launch from it, like the python
interpreter.

Don't forget that changes from your .bashrc file won't apply until you run

source ~/.bashrc

or open a new terminal window. If you're seeing this from an ipython notebook
you'll need to restart the ipython notebook, or maybe modify os.environ from
an ipython cell.
""")

            raise

        rval.append(val)

        rval.append('}'.join(subsplit[1:]))

    rval = ''.join(rval)

    return rval

class EnvironmentVariableError(Exception):
    """ An exception raised when a required environment variable is not defined """

    def __init__(self, *args):
        super(EnvironmentVariableError,self).__init__(*args)

def find_number(s):
    """ s is a string
        returns None if there are no numbers in the string
        otherwise returns the range of characters occupied by the first
        number in the string """

    r = re.search('-?\d+[.e]?\d*',s)
    if r is not None:
        return r.span(0)
    return None

def tokenize_by_number(s):
    """ splits a string into a list of tokens
        each is either a string containing no numbers
        or a float """

    r = find_number(s)

    if r == None:
        return [ s ]
    else:
        tokens = []
        if r[0] > 0:
            tokens.append(s[0:r[0]])
        tokens.append( float(s[r[0]:r[1]]) )
        if r[1] < len(s):
            tokens.extend(tokenize_by_number(s[r[1]:]))
        return tokens
    assert False #line should be unreached

def number_aware_alphabetical_cmp(str1, str2):
    """ cmp function for sorting a list of strings by alphabetical order, but with
        numbers sorted numerically.

        i.e., foo1, foo2, foo10, foo11
        instead of foo1, foo10
    """

    def flatten_tokens(tokens):
        l = []
        for token in tokens:
            if isinstance(token, str):
                for char in token:
                    l.append(char)
            else:
                assert isinstance(token, float)
                l.append(token)
        return l

    seq1 = flatten_tokens(tokenize_by_number(str1))
    seq2 = flatten_tokens(tokenize_by_number(str2))

    l = min(len(seq1),len(seq2))

    i = 0

    while i < l:
        if seq1[i] < seq2[i]:
            return -1
        elif seq1[i] > seq2[i]:
            return 1
        i += 1

    if len(seq1) < len(seq2):
        return -1
    elif len(seq1) > len(seq2):
        return 1

    return 0

#key for sorting strings alphabetically with numbers
number_aware_alphabetical_key = functools.cmp_to_key(number_aware_alphabetical_cmp)

def match(wrong, candidates):
    """
        wrong: a mispelling
        candidates: a set of correct words

        returns a guess of which candidate is the right one

        This should be used with a small number of candidates and a high potential
        edit distance.
        ie, use it to correct a wrong filename in a directory, wrong class name
        in a module, etc. Don't use it to correct small typos of freeform natural
        language words.
    """

    assert len(candidates) > 0

    # Current implementation tries all candidates and outputs the one
    # with the min score
    # Could try to do something smarter

    def score(w1,w2):
        # Current implementation returns negative dot product of
        # the two words mapped into a feature space by mapping phi
        # w -> [ phi(w1), .1 phi(first letter of w), .1 phi(last letter of w) ]
        # Could try to do something smarter

        w1 = w1.lower()
        w2 = w2.lower()

        def phi(w):
            # Current feature mapping is to the vector of counts of
            # all letters and two-letter sequences
            # Could try to do something smarter
            rval = {}

            for i in xrange(len(w)):
                l = w[i]
                rval[l] = rval.get(l,0.) + 1.
                if i < len(w)-1:
                    b = w[i:i+2]
                    rval[b] = rval.get(b,0.) + 1.

            return rval

        d1 = phi(w1)
        d2 = phi(w2)

        def mul(d1, d2):
            rval = 0

            for key in set(d1).union(d2):
                rval += d1.get(key,0) * d2.get(key,0)

            return rval

        tot_score = mul(phi(w1),phi(w2)) / float(len(w1)*len(w2)) + \
            0.1 * mul(phi(w1[0:1]), phi(w2[0:1])) + \
            0.1 * mul(phi(w1[-1:]), phi(w2[-1:]))

        return  tot_score

    scored_candidates = [ (-score(wrong, candidate), candidate)
            for candidate in candidates ]

    scored_candidates.sort()

    return scored_candidates[0][1]
<|endoftext|>


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

"""
Script to write GW Input for VASP and ABINIT.
"""

from __future__ import division

__author__ = "Michiel van Setten"
__copyright__ = " "
__version__ = "0.9"
__maintainer__ = "Michiel van Setten"
__email__ = "mjvansetten@gmail.com"
__date__ = "Oct 23, 2013"

import os
import os.path

from pymatgen.io.gwsetup.GWdatastructures import GWSpecs

MODULE_DIR = os.path.dirname(os.path.abspath(__file__))

if __name__ == "__main__":
    spec_in = GWSpecs()
    spec_in.update_interactive()
    spec_in.test()
    spec_in.write_to_file('spec.in')
    spec_in.loop_structures('i')<commit_msg>debugging<commit_after>#!/usr/bin/env python

"""
Script to write GW Input for VASP and ABINIT.
"""

from __future__ import division

__author__ = "Michiel van Setten"
__copyright__ = " "
__version__ = "0.9"
__maintainer__ = "Michiel van Setten"
__email__ = "mjvansetten@gmail.com"
__date__ = "Oct 23, 2013"

import os
import os.path

from pymatgen.io.gwsetup.GWdatastructures import GWSpecs

MODULE_DIR = os.path.dirname(os.path.abspath(__file__))

if __name__ == "__main__":
    spec_in = GWSpecs()
    try:
        spec_in.read_from_file('spec.in')
    except (IOError, OSError):
        pass
    spec_in.update_interactive()
    spec_in.test()
    spec_in.write_to_file('spec.in')
    spec_in.loop_structures('i')<|endoftext|>


<commit_before># coding: utf-8
# Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.

import os
from pymatgen.util.testing import PymatgenTest
from pymatgen.io.atat import Mcsqs
from pymatgen.core.structure import Structure

test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..",
                        "test_files", "mcsqs")

class AtatTest(PymatgenTest):

    def test_mcsqs_import(self):
        test_string = """1.000000 0.000000 0.000000
0.000000 1.000000 0.000000
0.000000 0.000000 1.000000
0.000000 -1.000000 -2.000000
2.000000 -1.000000 0.000000
-1.000000 -1.000000 1.000000
0.000000 -2.000000 -1.000000 Mn
1.000000 -2.000000 -1.000000 Mn
0.000000 -1.000000 -1.000000 Mn
-0.000000 -2.000000 0.000000 Mn
1.000000 -2.000000 0.000000 Mn
0.000000 -1.000000 0.000000 Mn
1.000000 -1.000000 0.000000 Fe
1.000000 -3.000000 -1.000000 Mn
0.500000 -1.500000 -0.500000 Sr
1.500000 -1.500000 -0.500000 Ca
-0.500000 -1.500000 0.500000 Ca
0.500000 -1.500000 0.500000 Ca
1.500000 -2.500000 -1.500000 Ca
0.500000 -1.500000 -1.500000 Sr
0.500000 -2.500000 -0.500000 Sr
-0.500000 -1.500000 -0.500000 Ca
0.000000 -1.500000 -1.000000 O
1.000000 -1.500000 -1.000000 O
1.000000 -2.500000 0.000000 O
-0.000000 -1.500000 0.000000 O
1.000000 -1.500000 0.000000 O
0.000000 -0.500000 0.000000 O
0.000000 -2.500000 -1.000000 O
1.000000 -2.500000 -1.000000 O
0.500000 -2.000000 -1.000000 O
1.500000 -2.000000 -1.000000 O
0.500000 -1.000000 -1.000000 O
0.500000 -2.000000 0.000000 O
-0.500000 -1.000000 0.000000 O
0.500000 -1.000000 0.000000 O
1.500000 -1.000000 0.000000 O
-0.500000 -2.000000 -1.000000 O
0.000000 -2.000000 -0.500000 O
1.000000 -2.000000 -0.500000 O
0.000000 -1.000000 -0.500000 O
1.000000 -1.000000 -0.500000 O
1.000000 -2.000000 0.500000 O
0.000000 -1.000000 0.500000 O
1.000000 -2.000000 -1.500000 O
0.000000 -1.000000 -1.500000 O
"""

        s = Mcsqs.structure_from_string(test_string)

        self.assertEqual(s.composition.formula, "Sr3 Ca5 Mn7 Fe1 O24")
        self.assertAlmostEqual(s.lattice.a, 2.2360679775)
        self.assertAlmostEqual(s.lattice.b, 2.2360679775)
        self.assertAlmostEqual(s.lattice.c, 1.73205080757)

    def test_mcsqs_export(self):
        s = self.get_structure('SrTiO3')
        s.replace_species({'Sr2+': {'Sr2+': 0.5, 'Ca2+': 0.5}})

        ref_string = """3.905000 0.000000 0.000000
-0.000000 3.905000 0.000000
0.000000 0.000000 3.905000
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
0.500000 0.500000 0.500000 Sr=0.5,Ca=0.5
0.000000 0.000000 0.000000 Ti=1.0
0.000000 0.000000 0.500000 O=1.0
0.000000 0.500000 0.000000 O=1.0
0.500000 0.000000 0.000000 O=1.0"""

        self.assertEqual(Mcsqs(s).to_string(), ref_string)

    def test_mcsqs_cif(self):

        # cif file from str2cif (utility distributed with atat)
        struc_from_cif = Structure.from_file(os.path.join(test_dir, "bestsqs_nacl.cif"))

        # output file directly from mcsqs
        struc_from_out = Structure.from_file(os.path.join(test_dir, "bestsqs_nacl.out"))

        self.assertTrue(struc_from_cif.matches(struc_from_out))
        self.assertArrayAlmostEqual(struc_from_out.lattice.parameters, struc_from_cif.lattice.parameters,
                                    decimal=4)<commit_msg>Added missing line<commit_after># coding: utf-8
# Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.

import os
from pymatgen.util.testing import PymatgenTest
from pymatgen.io.atat import Mcsqs
from pymatgen.core.structure import Structure

test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..",
                        "test_files", "mcsqs")

class AtatTest(PymatgenTest):

    def test_mcsqs_import(self):
        test_string = """1.000000 0.000000 0.000000
0.000000 1.000000 0.000000
0.000000 0.000000 1.000000
0.000000 -1.000000 -2.000000
2.000000 -1.000000 0.000000
-1.000000 -1.000000 1.000000
0.000000 -2.000000 -1.000000 Mn
1.000000 -2.000000 -1.000000 Mn
0.000000 -1.000000 -1.000000 Mn
-0.000000 -2.000000 0.000000 Mn
1.000000 -2.000000 0.000000 Mn
0.000000 -1.000000 0.000000 Mn
1.000000 -1.000000 0.000000 Fe
1.000000 -3.000000 -1.000000 Mn
0.500000 -1.500000 -0.500000 Sr
1.500000 -1.500000 -0.500000 Ca
-0.500000 -1.500000 0.500000 Ca
0.500000 -1.500000 0.500000 Ca
1.500000 -2.500000 -1.500000 Ca
0.500000 -1.500000 -1.500000 Sr
0.500000 -2.500000 -0.500000 Sr
-0.500000 -1.500000 -0.500000 Ca
0.000000 -1.500000 -1.000000 O
1.000000 -1.500000 -1.000000 O
1.000000 -2.500000 0.000000 O
-0.000000 -1.500000 0.000000 O
1.000000 -1.500000 0.000000 O
0.000000 -0.500000 0.000000 O
0.000000 -2.500000 -1.000000 O
1.000000 -2.500000 -1.000000 O
0.500000 -2.000000 -1.000000 O
1.500000 -2.000000 -1.000000 O
0.500000 -1.000000 -1.000000 O
0.500000 -2.000000 0.000000 O
-0.500000 -1.000000 0.000000 O
0.500000 -1.000000 0.000000 O
1.500000 -1.000000 0.000000 O
-0.500000 -2.000000 -1.000000 O
0.000000 -2.000000 -0.500000 O
1.000000 -2.000000 -0.500000 O
0.000000 -1.000000 -0.500000 O
1.000000 -1.000000 -0.500000 O
1.000000 -2.000000 0.500000 O
0.000000 -1.000000 0.500000 O
1.000000 -2.000000 -1.500000 O
0.000000 -1.000000 -1.500000 O
"""

        s = Mcsqs.structure_from_string(test_string)

        self.assertEqual(s.composition.formula, "Sr3 Ca5 Mn7 Fe1 O24")
        self.assertAlmostEqual(s.lattice.a, 2.2360679775)
        self.assertAlmostEqual(s.lattice.b, 2.2360679775)
        self.assertAlmostEqual(s.lattice.c, 1.73205080757)

    def test_mcsqs_export(self):
        s = self.get_structure('SrTiO3')
        s.replace_species({'Sr2+': {'Sr2+': 0.5, 'Ca2+': 0.5}})

        ref_string = """3.905000 0.000000 0.000000
-0.000000 3.905000 0.000000
0.000000 0.000000 3.905000
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
0.500000 0.500000 0.500000 Sr=0.5,Ca=0.5
0.000000 0.000000 0.000000 Ti=1.0
0.000000 0.000000 0.500000 O=1.0
0.000000 0.500000 0.000000 O=1.0
0.500000 0.000000 0.000000 O=1.0"""

        self.assertEqual(Mcsqs(s).to_string(), ref_string)

    def test_mcsqs_cif(self):

        # cif file from str2cif (utility distributed with atat)
        struc_from_cif = Structure.from_file(os.path.join(test_dir, "bestsqs_nacl.cif"))

        # output file directly from mcsqs
        struc_from_out = Structure.from_file(os.path.join(test_dir, "bestsqs_nacl.out"))

        self.assertTrue(struc_from_cif.matches(struc_from_out))
        self.assertArrayAlmostEqual(struc_from_out.lattice.parameters, struc_from_cif.lattice.parameters,
                                    decimal=4)
<|endoftext|>


<commit_before>#!/usr/bin/python
"""
PyMercadopagoHandler Class:
==================
"""
import requests
import json
from exceptions import NoAccessTokenError, UndefinedResponseError

class Handler:

    orders = list()
    notifications = list()
    TOKEN_GENERATION_STATUS_EXPECTED = 200
    CREATE_ITEM_STATUS_EXPECTED = 201
    result = None

    def __init__(self, client_id, client_secret):
        self.url_base = 'https://api.mercadolibre.com'
        self.url_oauth_token = "%s/oauth/token" % self.url_base
        self.url_preference = "%s/checkout/preferences?access_token=" \
        % self.url_base

        self.url_status_preference = "%s/checkout/preferences/ping" \
        % self.url_base

        self.client_id = client_id
        self.client_secret = client_secret
        if self.client_id == '' or self.client_secret == '':
            raise NoAccessTokenError()

        self.access_token = self.get_access_token()

        if not self.access_token:
            raise NoAccessTokenError()

        self.result = list()

    def post_data(self, data, rcode, url, utype):

        if utype == 'json':
            headers = {'Content-type': 'application/json',
                    'Accept': 'application/json'}
            data = json.dumps(data)
        else:
            headers = {'Content-type': 'application/x-www-form-urlencoded',
                    'Accept': 'application/json'}

        r = requests.post(url, data=data, headers=headers)
        if r.status_code == rcode:
            return r.content
        else:
            raise UndefinedResponseError(r)

        #Raisear error post data

    def get_access_token(self):
        data = {
                'grant_type': 'client_credentials',
                'client_id': self.client_id,
                'client_secret': self.client_secret
                }
        url = self.url_oauth_token
        response = self.post_data(data, self.TOKEN_GENERATION_STATUS_EXPECTED,
                                  url, 'text')
        if response:
            resp_dict = json.loads(response)
            return resp_dict['access_token']
        return False

    def get_or_create_item(self, data):

        url = "%s%s" % (self.url_preference, self.access_token)
        preference = self.post_data(data, self.CREATE_ITEM_STATUS_EXPECTED,
                                    url, 'json')
        if preference:
            return json.loads(preference)
        return False

    def push_orders(self, orders):
        for order in orders:
            result = self.get_or_create_item(order.to_dict())
            self.result.append(MPPreference(result))

class MPOrder:
    items = None
    payer = None
    external_reference = ''
    suscription_plan_id = ''
    id = ''
    collector_id = ''
    init_point = ''
    expires = ''
    expiration_date_to = ''
    expiration_date_from = ''
    back_urls = None

    def __init__(self, external_reference, internal_id, collector_id):

        #At constructor should be all required fields
        self.external_reference = external_reference
        self.collector_id = collector_id
        self.id = internal_id
        self.items = list()
        self.payer = MPPayer()

    def add_item(self, item):
        if self.items == None:
            self.items = list()

        self.items.append(item)

    def add_payer(self, payer):
        self.payer = payer

    def add_successUrl(self, url):
        if self.back_urls == None:
            self.back_urls = MPBackUrls
        self.back_ulrs.success = url

    def add_pending_url(self, url):
        if self.back_urls == None:
            self.back_urls = MPBackUrls()
        self.back_ulrs.pending = url

    def to_dict(self):

        return json.loads(str(self))

    def __repr__(self):
        return_value = '{"external_reference":"' + \
            self.external_reference + '",'

        return_value += '"items":[{'

        for item in self.items:
            if item.id != '':
                return_value += '"id":'
                return_value += '"' + item.id + '",'

            return_value += '"title":'
            return_value += '"' + item.title + '"'

            if item.description != '':
                return_value += ',"description":'
                return_value += '"' + item.description + '"'

            return_value += ',"quantity":'
            return_value += str(item.quantity)

            return_value += ',"unit_price":'
            return_value += str(item.unit_price)

            return_value += ',"currency_id":'
            return_value += '"' + str(item.currency_id) + '"'

            if item.picture_url != '':
                return_value += ',"picture_url":'
                return_value += '"' + item.picture_url + '"'

            return_value += '}]'

        if self.payer != None:
            return_value = return_value + ',"payer":{'
            if self.payer.name != '':
                return_value += '"name":' + '"' + self.payer.name + '"'
            if self.payer.surname != '':
                return_value += ',"surname":' + '"' + self.payer.surname + '"'
            if self.payer.email != '':
                return_value += ',"email":' + '"' + self.payer.email + '"'

            return_value += '}'

        if self.back_urls != None:
            return_value += ',"back_urls":{'
            return_value += '"success":"' + self.back_urls.success + '"'
            return_value += ',"pending":"' + self.back_urls.pending + '"'
            return_value += '}'

        return_value += '}'

        return return_value

class MPItem:

    id = ''
    title = ''
    description = ''
    quantity = 0
    unit_price = 0
    currency_id = ''
    picture_url = ''

    def __init__(self, title, quantity, unit_price, currency_id):
        self.title = title
        self.quantity = quantity
        self.unit_price = unit_price
        self.currency_id = currency_id

class MPPayer:

    name = ''
    surname = ''
    email = ''

class MPBackUrls:

    pending = ''
    success = ''

class MPPreference:
    collector_id = ''
    init_point = ''
    back_urls = None
    expiration_date_from = ''
    date_created = ''
    subscription_plan_id = ''
    id = ''
    expires = ''
    expiration_date_to = ''
    external_reference = ''
    payer = None
    items = None

    def __init__(
            self, result):
        self.collector_id = result['collector_id']
        self.init_point = result['init_point']
        self.back_urls = result['back_urls']
        self.expiration_date_from = result['expiration_date_from']
        self.date_created = result['date_created']
        self.subscription_plan_id = result['subscription_plan_id']
        self.id = result['id']
        self.expires = result['expires']
        self.expiration_date_to = result['expiration_date_to']
        self.external_reference = result['external_reference']
        self.payer = result['payer']
        self.items = result['items']

    def __repr__(self):
        return 'Collector_id: ' + str(self.collector_id) + \
         ' Preference_id:' + self.id + ' Init_point:' + self.init_point
<commit_msg>add exclude payment methods members<commit_after>#!/usr/bin/python
"""
PyMercadopagoHandler Class:
==================
"""
import requests
import json
from exceptions import NoAccessTokenError, UndefinedResponseError

class Handler:

    orders = list()
    notifications = list()
    TOKEN_GENERATION_STATUS_EXPECTED = 200
    CREATE_ITEM_STATUS_EXPECTED = 201
    result = None

    def __init__(self, client_id, client_secret):
        self.url_base = 'https://api.mercadolibre.com'
        self.url_oauth_token = "%s/oauth/token" % self.url_base
        self.url_preference = "%s/checkout/preferences?access_token=" \
        % self.url_base

        self.url_status_preference = "%s/checkout/preferences/ping" \
        % self.url_base

        self.client_id = client_id
        self.client_secret = client_secret
        if self.client_id == '' or self.client_secret == '':
            raise NoAccessTokenError()

        self.access_token = self.get_access_token()

        if not self.access_token:
            raise NoAccessTokenError()

        self.result = list()

    def post_data(self, data, rcode, url, utype):

        if utype == 'json':
            headers = {'Content-type': 'application/json',
                    'Accept': 'application/json'}
            data = json.dumps(data)
        else:
            headers = {'Content-type': 'application/x-www-form-urlencoded',
                    'Accept': 'application/json'}

        r = requests.post(url, data=data, headers=headers)
        if r.status_code == rcode:
            return r.content
        else:
            raise UndefinedResponseError(r)

        #Raisear error post data

    def get_access_token(self):
        data = {
                'grant_type': 'client_credentials',
                'client_id': self.client_id,
                'client_secret': self.client_secret
                }
        url = self.url_oauth_token
        response = self.post_data(data, self.TOKEN_GENERATION_STATUS_EXPECTED,
                                  url, 'text')
        if response:
            resp_dict = json.loads(response)
            return resp_dict['access_token']
        return False

    def get_or_create_item(self, data):

        url = "%s%s" % (self.url_preference, self.access_token)
        preference = self.post_data(data, self.CREATE_ITEM_STATUS_EXPECTED,
                                    url, 'json')
        if preference:
            return json.loads(preference)
        return False

    def push_orders(self, orders):
        for order in orders:
            result = self.get_or_create_item(order.to_dict())
            self.result.append(MPPreference(result))

class MPOrder:
    items = None
    payer = None
    external_reference = ''
    suscription_plan_id = ''
    id = ''
    collector_id = ''
    init_point = ''
    expires = ''
    expiration_date_to = ''
    expiration_date_from = ''
    back_urls = None

    def __init__(self, external_reference, internal_id, collector_id):

        #At constructor should be all required fields
        self.external_reference = external_reference
        self.collector_id = collector_id
        self.id = internal_id
        self.items = list()
        self.payer = MPPayer()

    def add_item(self, item):
        if self.items == None:
            self.items = list()

        self.items.append(item)

    def add_payer(self, payer):
        self.payer = payer

    def add_successUrl(self, url):
        if self.back_urls == None:
            self.back_urls = MPBackUrls
        self.back_ulrs.success = url

    def add_pending_url(self, url):
        if self.back_urls == None:
            self.back_urls = MPBackUrls()
        self.back_ulrs.pending = url

    def to_dict(self):

        return json.loads(str(self))

    def __repr__(self):
        return_value = '{"external_reference":"' + \
            self.external_reference + '",'

        return_value += '"items":[{'

        for item in self.items:
            if item.id != '':
                return_value += '"id":'
                return_value += '"' + item.id + '",'

            return_value += '"title":'
            return_value += '"' + item.title + '"'

            if item.description != '':
                return_value += ',"description":'
                return_value += '"' + item.description + '"'

            return_value += ',"quantity":'
            return_value += str(item.quantity)

            return_value += ',"unit_price":'
            return_value += str(item.unit_price)
New
            return_value += ',"currency_id":'
            return_value += '"' + str(item.currency_id) + '"'

            if item.picture_url != '':
                return_value += ',"picture_url":'
                return_value += '"' + item.picture_url + '"'

            return_value += '}]'

        if self.payer != None:
            return_value = return_value + ',"payer":{'
            if self.payer.name != '':
                return_value += '"name":' + '"' + self.payer.naNew me + '"'
            if self.payer.surname != '':
                return_value += ',"surname":' + '"' + self.payer.surname + '"'
            if self.payer.email != '':
                return_value += ',"email":' + '"' + self.payer.email + '"'

            return_value += '}'

        if self.back_urls != None:
            return_value += ',"back_urls":{'
            return_value += '"success":"' + self.back_urls.success + '"'
            return_value += ',"pending":"' + self.back_urls.pending + '"'
            return_value += '}'

        return_value += '}'

        return return_value

class MPItem:

    id = ''
    title = ''
    description = ''
    quantity = 0
    unit_price = 0
    currency_id = ''
    picture_url = ''

    def __init__(self, title, quantity, unit_price, currency_id):
        self.title = title
        self.quantity = quantity
        self.unit_price = unit_price
        self.currency_id = currency_id

class MPPayer:

    name = ''
    surname = ''
    email = ''

    def __init__(self, name, surname, email):
        self.name = name
        self.surname = surname
        self.email = email

class MPBackUrls:

    pending = ''
    success = ''

class MPPreference:
    collector_id = ''
    init_point = ''
    back_urls = None
    expiration_date_from = ''
    date_created = ''
    subscription_plan_id = ''
    id = ''
    expires = ''
    expiration_date_to = ''
    external_reference = ''
    payer = None
    items = None

    def __init__(
            self, result):
        self.collector_id = result['collector_id']
        self.init_point = result['init_point']
        self.back_urls = result['back_urls']
        self.expiration_date_from = result['expiration_date_from']
        self.date_created = result['date_created']
        self.subscription_plan_id = result['subscription_plan_id']
        self.id = result['id']
        self.expires = result['expires']
        self.expiration_date_to = result['expiration_date_to']
        self.external_reference = result['external_reference']
        self.payer = result['payer']
        self.items = result['items']

    def __repr__(self):
        return 'Collector_id: ' + str(self.collector_id) + \
         ' Preference_id:' + self.id + ' Init_point:' + self.init_point

class MPPaymentMethods
    excluded_payment_types = ''
    excluded_payment_methods = ''
    installments = 0

    def __init__(self, excluded_payment_types, excluded_payment_methods,
        installments):
        self.excluded_payment_types = excluded_payment_types
        self.excluded_payment_methods = excluded_payment_methods
        self.installments = installments

<|endoftext|>


<commit_before>"""
Contains the python specific FileManager.
"""
import ast
import re
from pyqode.core.api import TextBlockHelper
from pyqode.core.managers import FileManager

class PyFileManager(FileManager):
    """
    Extends file manager to override detect_encoding. With python, we can
    detect encoding by reading the two first lines of a file and extracting its
    encoding tag.

    """
    #: True to fold import statements on open.
    fold_imports = True

    #: True to fold docstring on open
    fold_docstrings = True

    def detect_encoding(self, path):
        """
        For the implementation of encoding definitions in Python, look at:
        - http://www.python.org/dev/peps/pep-0263/

        .. note:: code taken and adapted from
            ```jedi.common.source_to_unicode.detect_encoding```
        """
        with open(path, 'rb') as file:
            source = file.read()
            # take care of line encodings (not in jedi)
            source = source.replace(b'\r', b'')
            source_str = str(source).replace('\\n', '\n')

        byte_mark = ast.literal_eval(r"b'\xef\xbb\xbf'")
        if source.startswith(byte_mark):
            # UTF-8 byte-order mark
            return 'utf-8'

        first_two_lines = re.match(r'(?:[^\n]*\n){0,2}', source_str).group(0)
        possible_encoding = re.search(r"coding[=:]\s*([-\w.]+)",
                                      first_two_lines)
        if possible_encoding:
            return possible_encoding.group(1)
        return 'UTF-8'

    def open(self, path, encoding=None, use_cached_encoding=True):
        if encoding is None:
            encoding = self.detect_encoding(path)
        super(PyFileManager, self).open(
            path, encoding=encoding, use_cached_encoding=use_cached_encoding)
        try:
            folding_panel = self.editor.panels.get('FoldingPanel')
        except KeyError:
            pass
        else:
            # fold imports and/or docstrings
            blocks_to_fold = []
            sh = self.editor.syntax_highlighter

            if self.fold_imports and sh.import_statements:
                blocks_to_fold += sh.import_statements
            if self.fold_docstrings and sh.docstrings:
                blocks_to_fold += sh.docstrings

            for block in blocks_to_fold:
                if TextBlockHelper.is_fold_trigger(block):
                    folding_panel.toggle_fold_trigger(block)

    def clone_settings(self, original):
        super(PyFileManager, self).clone_settings(original)
        self.fold_docstrings = original.fold_docstrings
        self.fold_imports = original.fold_imports
<commit_msg>PyFileManager: set fold imports/docstring to false by default<commit_after>"""
Contains the python specific FileManager.
"""
import ast
import re
from pyqode.core.api import TextBlockHelper
from pyqode.core.managers import FileManager

class PyFileManager(FileManager):
    """
    Extends file manager to override detect_encoding. With python, we can
    detect encoding by reading the two first lines of a file and extracting its
    encoding tag.

    """
    #: True to fold import statements on open.
    fold_imports = False

    #: True to fold docstring on open
    fold_docstrings = False

    def detect_encoding(self, path):
        """
        For the implementation of encoding definitions in Python, look at:
        - http://www.python.org/dev/peps/pep-0263/

        .. note:: code taken and adapted from
            ```jedi.common.source_to_unicode.detect_encoding```
        """
        with open(path, 'rb') as file:
            source = file.read()
            # take care of line encodings (not in jedi)
            source = source.replace(b'\r', b'')
            source_str = str(source).replace('\\n', '\n')

        byte_mark = ast.literal_eval(r"b'\xef\xbb\xbf'")
        if source.startswith(byte_mark):
            # UTF-8 byte-order mark
            return 'utf-8'

        first_two_lines = re.match(r'(?:[^\n]*\n){0,2}', source_str).group(0)
        possible_encoding = re.search(r"coding[=:]\s*([-\w.]+)",
                                      first_two_lines)
        if possible_encoding:
            return possible_encoding.group(1)
        return 'UTF-8'

    def open(self, path, encoding=None, use_cached_encoding=True):
        if encoding is None:
            encoding = self.detect_encoding(path)
        super(PyFileManager, self).open(
            path, encoding=encoding, use_cached_encoding=use_cached_encoding)
        try:
            folding_panel = self.editor.panels.get('FoldingPanel')
        except KeyError:
            pass
        else:
            # fold imports and/or docstrings
            blocks_to_fold = []
            sh = self.editor.syntax_highlighter

            if self.fold_imports and sh.import_statements:
                blocks_to_fold += sh.import_statements
            if self.fold_docstrings and sh.docstrings:
                blocks_to_fold += sh.docstrings

            for block in blocks_to_fold:
                if TextBlockHelper.is_fold_trigger(block):
                    folding_panel.toggle_fold_trigger(block)

    def clone_settings(self, original):
        super(PyFileManager, self).clone_settings(original)
        self.fold_docstrings = original.fold_docstrings
        self.fold_imports = original.fold_imports
<|endoftext|>


<commit_before>PACKAGE_VERSION  = '4.0.13'  # version of the client package
PROTOCOL_VERSION = '1.4'     # protocol version requested

# The hash of the Electrum mnemonic seed must begin with this
SEED_PREFIX      = '01'      # Standard wallet, Electrum seed

def seed_prefix(seed_type):
    assert seed_type in ('standard', 'electrum')
    return SEED_PREFIX

import re

_RX_NORMALIZER = re.compile(r'(\.0+)*$')
_RX_VARIANT_TOKEN_PARSE = re.compile(r'^(\d+)(.+)$')

def normalize_version(v):
    """Used for PROTOCOL_VERSION normalization, e.g '1.4.0' -> (1,4) """
    return tuple(int(x) for x in _RX_NORMALIZER.sub('', v.strip()).split("."))

def parse_package_version(pvstr):
    """ Basically returns a tuple of the normalized version plus the 'variant'
    string at the end. Eg '3.3.0' -> (3, 3, ''), '3.2.2CS' -> (3, 2, 2, 'CS'),
    etc.

    Some more examples:
            '3.3.5CS' -> (3, 3, 5, 'CS')
            '3.4.5_iOS' -> (3, 4, 5, '_iOS')
            '3.3.5' -> (3, 3, 5, '')
            '3.3' -> (3, 3, '')
            '3.3.0' -> (3, 3, '')
            '   3.2.2.0 ILikeSpaces ' -> (3, 2, 2, 'ILikeSpaces')
    Note how 0 fields at the end of the version get normalized with the 0 lopped off:
            '3.3.0' -> (3, 3, '')
            '3.5.0.0.0' -> (3, 5, '')
            '3.5.0.0.0_iOS' -> (3, 5, '_iOS')
    ... and, finally: The last element is *always* going to be present as
    a string, the 'variant'. The 'variant' will be the empty string '' if
    this is the default Electron Cash. If you don't like this heterogeneity of
    types in a tuple, take the retVal[:-1] slice of the array to toss it
    (or just use normalize_version above).
    """
    def raise_(e=None):
        exc = ValueError('Failed to parse package version for: "{}"'.format(pvstr))
        if e: raise exc from e
        else: raise exc
    toks = [x.strip() for x in pvstr.split(".")]
    if not toks:
        raise_()
    if toks[-1].isdigit():
        # Missing 'variant' at end.. add the default '' variant.
        toks.append('')
    else:
        # had 'variant' at end, parse it.
        m = _RX_VARIANT_TOKEN_PARSE.match(toks[-1])
        if m:
            # pop off end and...
            toks[-1:] = [m.group(1), # add the digit portion back (note it's still a str at this point)
                         m.group(2).strip()] # add the leftovers as the actual variant
        else:
            raise_()
    try:
        # make sure everything but the last element is an int.
        toks[:-1] = [int(x) for x in toks[:-1]]
    except ValueError as e:
        raise_(e)
    # .. and.. finally: Normalize it! (lopping off zeros at the end)
    toks[:-1] = normalize_version('.'.join(str(t) for t in toks[:-1]))
    return tuple(toks)
<commit_msg>Bumped version to 4.0.14<commit_after>PACKAGE_VERSION  = '4.0.14'  # version of the client package
PROTOCOL_VERSION = '1.4'     # protocol version requested

# The hash of the Electrum mnemonic seed must begin with this
SEED_PREFIX      = '01'      # Standard wallet, Electrum seed

def seed_prefix(seed_type):
    assert seed_type in ('standard', 'electrum')
    return SEED_PREFIX

import re

_RX_NORMALIZER = re.compile(r'(\.0+)*$')
_RX_VARIANT_TOKEN_PARSE = re.compile(r'^(\d+)(.+)$')

def normalize_version(v):
    """Used for PROTOCOL_VERSION normalization, e.g '1.4.0' -> (1,4) """
    return tuple(int(x) for x in _RX_NORMALIZER.sub('', v.strip()).split("."))

def parse_package_version(pvstr):
    """ Basically returns a tuple of the normalized version plus the 'variant'
    string at the end. Eg '3.3.0' -> (3, 3, ''), '3.2.2CS' -> (3, 2, 2, 'CS'),
    etc.

    Some more examples:
            '3.3.5CS' -> (3, 3, 5, 'CS')
            '3.4.5_iOS' -> (3, 4, 5, '_iOS')
            '3.3.5' -> (3, 3, 5, '')
            '3.3' -> (3, 3, '')
            '3.3.0' -> (3, 3, '')
            '   3.2.2.0 ILikeSpaces ' -> (3, 2, 2, 'ILikeSpaces')
    Note how 0 fields at the end of the version get normalized with the 0 lopped off:
            '3.3.0' -> (3, 3, '')
            '3.5.0.0.0' -> (3, 5, '')
            '3.5.0.0.0_iOS' -> (3, 5, '_iOS')
    ... and, finally: The last element is *always* going to be present as
    a string, the 'variant'. The 'variant' will be the empty string '' if
    this is the default Electron Cash. If you don't like this heterogeneity of
    types in a tuple, take the retVal[:-1] slice of the array to toss it
    (or just use normalize_version above).
    """
    def raise_(e=None):
        exc = ValueError('Failed to parse package version for: "{}"'.format(pvstr))
        if e: raise exc from e
        else: raise exc
    toks = [x.strip() for x in pvstr.split(".")]
    if not toks:
        raise_()
    if toks[-1].isdigit():
        # Missing 'variant' at end.. add the default '' variant.
        toks.append('')
    else:
        # had 'variant' at end, parse it.
        m = _RX_VARIANT_TOKEN_PARSE.match(toks[-1])
        if m:
            # pop off end and...
            toks[-1:] = [m.group(1), # add the digit portion back (note it's still a str at this point)
                         m.group(2).strip()] # add the leftovers as the actual variant
        else:
            raise_()
    try:
        # make sure everything but the last element is an int.
        toks[:-1] = [int(x) for x in toks[:-1]]
    except ValueError as e:
        raise_(e)
    # .. and.. finally: Normalize it! (lopping off zeros at the end)
    toks[:-1] = normalize_version('.'.join(str(t) for t in toks[:-1]))
    return tuple(toks)
<|endoftext|>


<commit_before>from sd import sdiv
<commit_msg>build<commit_after>from sd import sd
<|endoftext|>