Beispiel #1
0
 def __init__(self, parms=(), args=(), outer=None):
     # Bind parm list to corresponding args, or single parm to list of args
     self.outer = outer
     if isa(parms, Symbol):
         self.update({parms:list(args)})
     else:
         if len(args) != len(parms):
             raise TypeError('expected %s, given %s, '
                             % (to_string(parms), to_string(args)))
         self.update(zip(parms,args))
Beispiel #2
0
def process_ticket_list(tickets):
    ticket_list = ""
    body = '{months}{days} since we replied to `{link}`\n'
    surpressed = 0

    for ticket_id in tickets:
        ticket = tickets[ticket_id]

        days = ticket['delta']['days']
        months = ticket['delta']['months']
        month_str = ""
        day_str = ""

        if months > 0:
            month_str = multi(" month", months)

        if days > 0:
            if months > 0:
                day_str = " and "
            day_str += multi(" day", days)

        if manager_run and str(ticket['group_id']) == group_id:
            manager_body = '{months}{days} since we replied to `{link}`. *Assignee was first notified {first_days} ago*\n'

            if 'first_notify' in ticket:
                first_notify = util.parse_time(ticket['first_notify'])
                diff = relativedelta(today, first_notify)
                first_day_str = multi(" day", diff.days)

                if diff.days >= notify_after_days or diff.months > 0:
                    link = zd.get_zendesk_url() + tickets_url + ticket_id
                    message = manager_body.format(months=month_str, days=day_str, link=link, first_days=first_day_str)

                    ticket_list += message
        else:
          if layover_days > 0 and 'last_notify' in ticket:
              last_notify = util.parse_time(ticket['last_notify'])
              diff = relativedelta(today, last_notify)

              if diff.days < layover_days and diff.months == 0:
                  surpressed += 1
                  continue

          if not 'first_notify' in ticket:
            ticket['first_notify'] = util.to_string(today)

          ticket['last_notify'] = util.to_string(today)

          link = zd.get_zendesk_url() + tickets_url + ticket_id
          ticket_list += body.format(months=month_str, days=day_str, link=link)

    if ticket_list and surpressed > 0:
        ticket_list += "_Plus " + str(surpressed) + " tickets surpressed._\n"

    return ticket_list
Beispiel #3
0
    def __str_helper(self, item):
        # nil has no contents
        if item is NIL:
            return u''

        if item.cdr is NIL:
            return util.to_string(item.car)

        if not isinstance(item.cdr, Cons):
            return util.to_string(item.car) + u' . ' + util.to_string(item.cdr)

        return util.to_string(item.car) + u' ' + self.__str_helper(item.cdr)
Beispiel #4
0
def compareTwoBinaryFiles(flags, filepaths, filelines):
    exitCode = 0
    if hasattr(difflib, 'diff_bytes'):
        # python 3.5 or newer
        diffs = difflib.diff_bytes(difflib.unified_diff,
                                   filelines[0],
                                   filelines[1],
                                   filepaths[0].encode(),
                                   filepaths[1].encode(),
                                   n=flags.num_context_lines)
        diffs = [diff.decode(errors="backslashreplace") for diff in diffs]
    else:
        # python 2.7
        if flags.unified_diff:
            func = difflib.unified_diff
        else:
            func = difflib.context_diff
        diffs = func(filelines[0],
                     filelines[1],
                     filepaths[0],
                     filepaths[1],
                     n=flags.num_context_lines)

    for diff in diffs:
        sys.stdout.write(to_string(diff))
        exitCode = 1
    return exitCode
Beispiel #5
0
def compareTwoTextFiles(flags, filepaths, filelines_bin, encoding):
    filelines = []
    for lines_bin in filelines_bin:
        lines = []
        for line_bin in lines_bin:
            line = line_bin.decode(encoding=encoding)
            lines.append(line)
        filelines.append(lines)

    exitCode = 0

    def compose2(f, g):
        return lambda x: f(g(x))

    f = lambda x: x
    if flags.strip_trailing_cr:
        f = compose2(lambda line: line.replace('\r\n', '\n'), f)
    if flags.ignore_all_space or flags.ignore_space_change:
        ignoreSpace = lambda line, separator: separator.join(line.split())
        ignoreAllSpaceOrSpaceChange = functools.partial(
            ignoreSpace, separator='' if flags.ignore_all_space else ' ')
        f = compose2(ignoreAllSpaceOrSpaceChange, f)

    for idx, lines in enumerate(filelines):
        filelines[idx] = [f(line) for line in lines]

    func = difflib.unified_diff if flags.unified_diff else difflib.context_diff
    for diff in func(filelines[0],
                     filelines[1],
                     filepaths[0],
                     filepaths[1],
                     n=flags.num_context_lines):
        sys.stdout.write(to_string(diff))
        exitCode = 1
    return exitCode
Beispiel #6
0
    def build_string(kind, name, spec):
        '''
        Build a string to display a typical callable in the interpreter. kind is
        the object type to use, name is the (optional) name to give this
        specific instance of the object, and spec is the ArgSpec object to
        use to build the arguments list.
        '''

        s = u'<' + unicode(kind)

        # set the function name if possible
        if name is not None:
            s += ' ' + unicode(name)

        s += ' ('

        # compose a list of all arg symbols
        a = []
        for arg_type, arg in spec:
            if arg_type == argspec.ArgSpec.REQUIRED:
                a.append(unicode(arg))
            elif arg_type == argspec.ArgSpec.OPTIONAL:
                arg, default = arg
                a.append('(' + unicode(arg) + ' ' + util.to_string(default) + ')')
            elif arg_type == argspec.ArgSpec.VARIADIC:
                a.append(unicode(arg))
                a.append(tokens.VARIADIC_ARG)
            else:
                raise ValueError('Unhandled arg type: ' + arg_type)

        s += ' '.join(a)
        s += ')>'

        return s
Beispiel #7
0
def add_globals(self):
    "Add some Scheme standard procedures."

    def display(x, port=None):
        if port is None:
            port = sys.stdout
        port.write(x if isa(x,str) else to_string(x))

    def callcc(proc):
        "Call proc with current continuation; escape only"
        ball = RuntimeWarning("Sorry, can't continue this continuation any longer.")
        def throw(retval):
            ball.retval = retval
            raise ball
        try:
            return proc(throw)
        except RuntimeWarning as w:
            if w is ball: return ball.retval
            else: raise w

    import math, cmath, operator as op
    self.update(vars(math))
    self.update(vars(cmath))
    self.update({
     '+':op.add, '-':op.sub, '*':op.mul, '/':op.div, 'not':op.not_,
     '>':op.gt, '<':op.lt, '>=':op.ge, '<=':op.le, '=':op.eq,
     'equal?':op.eq,
     'eq?':op.is_,
     'length':len,
     'cons':cons,
     'car':lambda x:x[0],
     'cdr':lambda x:x[1:],
     'cadr': lambda x:x[1],
     'append':op.add,
     'list':lambda *x:list(x),
     'list?': lambda x:isa(x,list),
     'null?':lambda x:x==[],
     'symbol?':lambda x: isa(x, Symbol),
     'boolean?':lambda x: isa(x, bool),
     'pair?':is_pair,
     'port?': lambda x:isa(x,file) or isa(x, InPort),
     'apply':lambda proc,l: proc(*l),
     # 'eval':lambda x: eval(expand(x)),
     # 'load':lambda fn: load(fn),
     'call/cc':callcc,
     'open-input-file':lambda f: InPort(open(f)),
     'close-input-port':lambda p: p.file.close(),
     'open-output-file':lambda f:open(f,'w'),
     'close-output-port':lambda p: p.close(),
     'eof-object?':lambda x:x is eof_object,
     'read-char':readchar,
     'read':read,
     'write':lambda x,port=sys.stdout:port.write(to_string(x)),
     'display':display,
     'sleep': gevent.sleep
    })
    return self
def pw_decode(s, password):
    if password is not None:
        secret = Hash(password)
        try:
            d = to_string(DecodeAES(secret, s), "utf8")
        except Exception:
            raise InvalidPassword()
        return d
    else:
        return s
Beispiel #9
0
def repl(prompt='lispy> ', inport=InPort(sys.stdin), out=sys.stdout):
    "A prompt-read-eval-print loop."
    sys.stderr.write("Lispy version 2.0\n")
    while True:
        try:
            if prompt: sys.stderr.write(prompt)
            x = parse(inport)
            if x is eof_object: return
            val = eval(x)
            if val is not None and out: print >> out, to_string(val)
        except Exception as e:
            print '%s: %s' % (type(e).__name__, e)
Beispiel #10
0
def pw_decode(data: str, password: Union[bytes, str, None], *,
              version: int) -> str:
    if password is None:
        return data
    if version not in KNOWN_PW_HASH_VERSIONS:
        raise UnexpectedPasswordHashVersion(version)
    data_bytes = bytes(base64.b64decode(data))
    # derive key from password
    secret = _hash_password(password, version=version)
    # decrypt given data
    try:
        d = to_string(DecodeAES_bytes(secret, data_bytes), "utf8")
    except Exception as e:
        raise InvalidPassword() from e
    return d
Beispiel #11
0
def get_rpc_credentials(config):
    rpc_user = config.get('rpcuser', None)
    rpc_password = config.get('rpcpassword', None)
    if rpc_user is None or rpc_password is None:
        rpc_user = '******'
        import ecdsa, base64
        bits = 128
        nbytes = bits // 8 + (bits % 8 > 0)
        pw_int = ecdsa.util.randrange(pow(2, bits))
        pw_b64 = base64.b64encode(int_to_bytes(pw_int, nbytes, 'big'), b'-_')
        rpc_password = to_string(pw_b64, 'ascii')
        config.set_key('rpcuser', rpc_user)
        config.set_key('rpcpassword', rpc_password, save=True)
    elif rpc_password == '':
        from .util import print_stderr
        print_stderr('WARNING: RPC authentication is disabled.')
    return rpc_user, rpc_password
Beispiel #12
0
def repl(prompt='lispy> ', inport=p.InPort(sys.stdin), out=sys.stdout):
    """A prompt-read-eval-print loop.
    """
    sys.stderr.write("Lispy version 2.0\n")
    while True:
        try:
            if prompt:
                sys.stderr.write(prompt)
            x = p.parse(inport)
            if x is p.eof_object:
                return
            val = e.eval(x)
            if val is not None and out:
                print >> out, to_string(val)
        except Exception, ex:
            print '{0}: {1}'.format(type(ex).__name__, ex)
            break
Beispiel #13
0
def repl(prompt='lispy> ', inport=p.InPort(sys.stdin), out=sys.stdout):
    """A prompt-read-eval-print loop.
    """
    sys.stderr.write("Lispy version 2.0\n")
    while True:
        try:
            if prompt:
                sys.stderr.write(prompt)
            x = p.parse(inport)
            if x is p.eof_object:
                return
            val = e.eval(x)
            if val is not None and out:
                print >> out, to_string(val)
        except Exception, ex:
            print '{0}: {1}'.format(type(ex).__name__, ex)
            break
Beispiel #14
0
    def cmd(self, source):
        '''
        The standard interpreter loop. Reads input, parses it, evaluates it, and
        writes the result to stdout. Continues to run until the user exits by
        sending an EOF.
        '''

        # exit when an EOF is received
        if isinstance(source, EOFError):
            sys.stdout.write(os.linesep)
            return True
        elif isinstance(source, KeyboardInterrupt):
            # clear the line and reset the source when an interrupt is received
            self.prompt = self.standard_prompt
            self.source = u''
            sys.stdout.write(os.linesep)
            return

        # otherwise, parse and evaluate the source code
        try:
            self.source += source

            # evaluate every entered expression sequentially
            for result in parse(tokens.tokenize(self.source)):
                self.stdout.write(util.to_string(evaluate(result, self.env)) +
                        os.linesep)

            # reset the prompt and source
            self.prompt = self.standard_prompt
            self.source = u''

        # allow the user to finish entering a correct expression
        except errors.ParserError:
            self.prompt = self.continue_prompt
            self.source += os.linesep

        # write all other problems and clear source
        except Exception, e:
            traceback.print_exc(file=self.stdout)

            # reset the source and prompt for the next parse
            self.source = u''
            self.prompt = self.standard_prompt
Beispiel #15
0
    def authenticate(self, headers):
        if self.rpc_password == '':
            # RPC authentication is disabled
            return

        auth_string = headers.get('Authorization', None)
        if auth_string is None:
            raise RPCAuthCredentialsMissing()

        (basic, _, encoded) = auth_string.partition(' ')
        if basic != 'Basic':
            raise RPCAuthUnsupportedType()

        encoded = util.to_bytes(encoded, 'utf8')
        credentials = util.to_string(b64decode(encoded), 'utf8')
        (username, _, password) = credentials.partition(':')
        if not (util.constant_time_compare(username, self.rpc_user)
                and util.constant_time_compare(password, self.rpc_password)):
            time.sleep(0.050)
            raise RPCAuthCredentialsInvalid()
Beispiel #16
0
def expand(num=None):
    """
    Display all of a MusicObject's information: songs, artists, and albums.

    Keyword arguments:
    num=None: Index of the MusicObject in the main window to be expanded.
    """
    global content

    if num is None:  # No argument.
        error_msg(outbar, 'Missing argument to play.')

    elif content is None:  # Nothing to expand.
        error_msg(outbar, 'Wrong context for expand.')

    else:
        try:
            num = int(num)

        except ValueError:  # num needs to be an int.
            error_msg(outbar, 'Invalid argument to play.')

        else:
            limit = int((main.getmaxyx()[0] - 6) / 3)
            opt = get_option(num, limit)

            if opt is not None:  # Valid input.
                addstr(outbar, 'Loading \'%s\'...' % to_string(opt))
                content = opt.collect(limit=limit)
                outbar.erase()
                outbar.refresh()

            else:  # num out of range.
                error_msg(
                    outbar, 'Invalid number. Valid between 1-%d.' %
                    sum([len(content[k]) for k in content.keys()]))
Beispiel #17
0
                    test_y.size(0))
            print(
                f"Epoch: {e} | train los: {loss.data.numpy()}, | test accuracy: {accuracy}"
            )
            # 训练模式
            cnn.train()

# 训练完的数据保存下来
# torch.save(cnn, 'net.pkl')  # 保存整个网络
# 只保存网络中的参数 (速度快, 占内存少)
# torch.save(cnn.state_dict(), "sxsz.pkl")

# 读取训练完成的数据
# 这种方式将会提取整个神经网络, 网络大的时候可能会比较慢.
# cnn = torch.load('sxsz.pkl')
# 这种方式将会提取所有的参数, 然后再放到你的新建网络中.
cnn.load_state_dict(torch.load("sxsz.pkl"))
#
img = Image.open("img/sx.png")
data_transforms = torchvision.transforms.Compose(
    [torchvision.transforms.ToTensor()])
x = torch.unsqueeze(data_transforms(img), 1) / 255.
test_output = cnn(x)
img.show()
# img = ToPILImage()(test_x[0])
# img.show()
# test_output = cnn(test_x[:10])
pred_y = torch.max(test_output, 1)[1].data.numpy()
to_string("手写图片训练", pred_y=pred_y, test_y=test_y[:10].numpy())
# img.show()
Beispiel #18
0
 def __str__(self):
     return "Imdb(url:" + to_string(self.url) + ",rank:" + to_string(
         self.rank) + ")"
Beispiel #19
0
 def display(x, port=None):
     if port is None:
         port = sys.stdout
     port.write(x if isa(x,str) else to_string(x))
Beispiel #20
0
 def __str__(self):
     return "ParentalRating(desc:" + to_string(
         self.desc) + ",min_age:" + to_string(self.min_age) + ")"
Beispiel #21
0
 def format_string(self, x):
     x = to_string(self.vocab, x)
     return {str(i): [v] for i, v in enumerate(x)}
 def __str__(self):
   return to_string(self.register)
Beispiel #23
0
def process_ticket_list(tickets, manager_lists):
    global group_managers
    ticket_list = ""
    body = '{months}{days} since we replied to `{link}`\n'
    surpressed = 0

    for ticket_id in tickets:
        ticket = tickets[ticket_id]

        days = ticket['delta']['days']
        months = ticket['delta']['months']
        month_str = ""
        day_str = ""

        if months > 0:
            month_str = multi(" month", months)

        if days > 0:
            if months > 0:
                day_str = " and "
            day_str += multi(" day", days)

        if str(ticket['group_id']) in group_managers:
            manager_body = '{months}{days} since we replied to `{link}`. *Assignee was first notified {first_days} ago*\n'
            manager = group_managers[str(ticket['group_id'])]

            if 'first_notify' in ticket:
                first_notify = util.parse_time(ticket['first_notify'])
                diff = relativedelta(today, first_notify)
                first_day_str = multi(" day", diff.days)

                if diff.days >= manager['notify_after_days'] or diff.months > 0:
                    link = zd.get_zendesk_url() + tickets_url + ticket_id
                    message = manager_body.format(months=month_str,
                                                  days=day_str,
                                                  link=link,
                                                  first_days=first_day_str)
                    manager_id = manager['user_id']

                    if manager_id in manager_lists:
                        manager_lists[manager_id] += message
                    else:
                        manager_lists[
                            manager_id] = 'Tickets that need updating for ' + manager[
                                'group_name'] + '\n' + message
            else:
                ticket['first_notify'] = util.to_string(today)

        if layover_days > 0 and 'last_notify' in ticket:
            last_notify = util.parse_time(ticket['last_notify'])
            diff = relativedelta(today, last_notify)

            if diff.days < layover_days and diff.months == 0:
                surpressed += 1
                continue

        ticket['last_notify'] = util.to_string(today)

        link = zd.get_zendesk_url() + tickets_url + ticket_id
        ticket_list += body.format(months=month_str, days=day_str, link=link)

    if ticket_list and surpressed > 0:
        ticket_list += "_Plus " + str(surpressed) + " tickets surpressed._"

    return ticket_list, manager_lists
    def load_data(self,
                  train,
                  test,
                  input_vars=None,
                  interpolation=0,
                  plot=False,
                  plot_dir=None):
        # train and test of form train = [[2013,5,6,7],[2014,5,6,7]]

        train_dates = list_dates(train)
        test_dates = list_dates(test)
        dates = train_dates + test_dates

        if input_vars is None:
            input_vars = self.ground_vars + self.height_grad_vars + \
                         self.height_level_vars
        else:
            input_vars = input_vars

        if interpolation == 0:
            pass
        else:
            if plot:
                if plot_dir:
                    save_dir = plot_dir
                else:
                    plot_dir = os.path.join(os.getcwd(), 'plots',
                                            'interpolation')
                    if not os.path.exists(plot_dir):
                        os.mkdir(plot_dir)

                    folder = 'interpolation_' + str(interpolation) + \
                             'train_dates_' + to_string(train) + \
                             'test_dates_' + to_string(test)

                    save_dir = os.path.join(plot_dir, folder)
                    if not os.path.exists(save_dir):
                        os.mkdir(save_dir)

            for date, var in product(dates, input_vars + ['PBLH']):

                print('fitting interpolation for ' + date + var)
                X = np.arange(self.data[date + var].shape[0])[:, None]
                y = self.data[date + var]

                gp = GPR(kernel=C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2)),
                         n_restarts_optimizer=9,
                         random_state=1337)
                gp.fit(X, y)

                x = np.linspace(0, X.shape[0],
                                X.shape[0] * interpolation)[:, None]
                y_pred, sigma = gp.predict(x, return_std=True)
                self.data[date + var] = y_pred

                if plot:
                    plt.clf()
                    plt.plot(X, y, 'r-', lw=2, label=u'real ' + var)
                    plt.plot(x,
                             y_pred,
                             'b-',
                             lw=1,
                             label=u'interpreted ' + var)
                    plt.plot(X, y, 'ro', label=u'real ' + var)
                    plt.plot(x, y_pred, 'bx', label=u'interpreted ' + var)
                    plt.fill(np.concatenate([x, x[::-1]]),
                             np.concatenate([
                                 y_pred - 1.9600 * sigma,
                                 (y_pred + 1.9600 * sigma)[::-1]
                             ]),
                             alpha=.5,
                             fc='b',
                             ec='None',
                             label='95% CI')
                    plt.xlabel(date)
                    plt.ylabel(var)
                    plt.legend(loc='best')
                    plt.savefig(os.path.join(save_dir, date + var + '.jpg'))
                    print('plotted : ' + date + var)

        for var in input_vars:
            train_list = [self.data[date + var] for date in train_dates]
            test_list = [self.data[date + var] for date in test_dates]
            self.data['train' + var] = np.concatenate(train_list)
            self.data['test' + var] = np.concatenate(test_list)

        train_inputs_list = [
            self.data['train' + var][:, None] for var in input_vars
        ]
        test_inputs_list = [
            self.data['test' + var][:, None] for var in input_vars
        ]

        y_train_list = [
            self.data[date + 'PBLH'][:, None] for date in train_dates
        ]
        y_test_list = [
            self.data[date + 'PBLH'][:, None] for date in test_dates
        ]

        x_train = np.concatenate(train_inputs_list, axis=1)
        y_train = np.concatenate(y_train_list)

        x_test = np.concatenate(test_inputs_list, axis=1)
        y_test = np.concatenate(y_test_list)

        return x_train, y_train, x_test, y_test
Beispiel #25
0
def enqueue(arg=None):
    """
    Display the current queue, or add an item to the queue.

    Keyword arguments:
    arg=None: Index of the MusicObject in the main window to add to
      the queue, 'c' to clear the queue, None to display the queue, or
      a space-delimited list of indices to add to the queue, i.e. '1 2 3'.

    Returns: True if an element was successfully inserted. To be used
      when inserting multiple elements.
    """
    global content

    if arg is None:
        if not queue:  # Nothing to display.
            error_msg(outbar, 'The queue is empty.')

        else:  # Display the queue.
            content = queue.collect()

    elif content is None:  # Nothing to queue.
        error_msg(outbar, 'Wrong context for queue.')

    else:
        if arg is 'c':  # Clear the queue.
            addstr(outbar, 'Cleared queue.')
            queue.clear()

        else:
            try:
                num = int(arg)

            except ValueError:
                # Check for multi-option argument.
                try:
                    nums = [int(i) for i in arg.split()]
                except ValueError:
                    error_msg(outbar, 'Invalid argument to queue.')
                else:  # Add all arguments to the queue.
                    count = 0
                    for num in nums:
                        count = count + 1 if enqueue(num) else count
                    addstr(
                        outbar, 'Added %d item%s to the queue.' %
                        (count, '' if count == 1 else 's'))

            else:
                opt = get_option(num)
                if opt is not None:
                    if opt['kind'] == 'artist':  # Artists can't be queued.
                        error_msg(
                            outbar,
                            'Can only add songs or albums to the queue.')

                    elif opt['id'] in queue.ids:  # Duplicate entry.
                        error_msg(
                            outbar,
                            '\'%s\' is already in the queue.' % to_string(opt))

                    else:  # Valid  input.
                        addstr(
                            outbar,
                            'Adding \'%s\' to the queue...' % to_string(opt))
                        queue.append(opt)
                        addstr(outbar,
                               'Added \'%s\' to the queue.' % to_string(opt))
                        return True

                else:  # num out of range.
                    error_msg(
                        outbar, 'Invalid number. Valid between 1-%d.' %
                        sum([len(content[k]) for k in content.keys()]))
        env[var] = eval(exp, env)

    # (lambda (var...) exp)
    # 引数var..を取り、式expに代入
    elif x[0] == "lambda":
        _, vars, exp = x
        return lambda *args: eval(exp, Env(vars, args, env))

    # (begin exp...)
    # expを逐次的に評価していく
    elif x[0] == "begin":
        val = None
        for exp in x[1:]:
            val = eval(exp, env)
        return val

    # (proc exp)
    # その他のシンボルは手続き処理として扱う
    else:
        exps = [eval(exp, env) for exp in x]
        proc = exps.pop(0)
        return proc(*exps)


if __name__ == "__main__":
    global_env = add_globals(Env())
    while True:
        val = eval(parse(input()), global_env)
        if val is not None:
            print(to_string(val))
Beispiel #27
0
 def __str__(self):
     return "ParentalRating(desc:" + to_string(self.desc)+ ",min_age:" + to_string(self.min_age) + ")" 
Beispiel #28
0
 def __str__(self):
     return "Imdb(url:" + to_string(self.url) + ",rank:" + to_string(self.rank) + ")" 
Beispiel #29
0
 def __str__(self):
     return "Channel(id:" + to_string(self.id) + ",name:" + to_string(self.name).encode('utf-8') +  ",icon_url:" + to_string(self.icon_url)+ ")"
Beispiel #30
0
 def __str__(self):
     return "Channel(id:" + to_string(self.id) + ",name:" + to_string(
         self.name).encode('utf-8') + ",icon_url:" + to_string(
             self.icon_url) + ")"