text:
using Geocoding.Contracts;
using Geocoding.Core;
using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Geocoding.IO
{
    /// <summary>
    /// Represents a reader for CSV files.
    /// </summary>
    public class CsvFileReader
    {
        private readonly Logger _logger;

        /// <summary>
        /// Creates a new reader instance.
        /// </summary>
        public CsvFileReader()
        {
            _logger = LogManager.GetCurrentClassLogger();
        }

        /// <summary>
        /// The record builder for adding and updating the properties.
        /// </summary>
        public IRecordBuilder RecordBuilder { get; set; }

        /// <summary>
        /// The value factory for creating the property values.
        /// </summary>
        public IValueFactory ValueFactory { get; set; }

        /// <summary>
        /// Reads a CSV file using the specified delimiter.
        /// </summary>
        /// <param name="fileInfo">The CSV file to read.</param>
        /// <param name="readHeader"><c>true</c> if the first row contains the header.</param>
        /// <param name="delimiter">The delimiter for the values.</param>
        /// <returns>A reference dataset.</returns>
        public ReferenceDataset ReadFile(FileInfo fileInfo, bool readHeader, char delimiter)
        {
            var dataset = new ReferenceDataset();
            var header = new List<string>();
            using (var reader = new StreamReader(fileInfo.OpenRead()))
            {
                string line;
                const int ChunkSize = 1000000;
                while (null != (line = reader.ReadLine()))
                {
                    var splittedLine = line.Split(delimiter);
                    if (!header.Any())
                    {
                        // Read the header
                        if (readHeader)
                        {
                            foreach (var headerValue in splittedLine)
                            {
                                header.Add(headerValue);
                            }
                            continue;
                        }
                        else
                        {
                            for (var index = 0; index < splittedLine.Length; index++)
                            {
                                header.Add(string.Format(@"FIELD{0}", index + 1));
                            }
                        }
                    }

                    if (header.Count == splittedLine.Length)
                    {
                        try
                        {
                            // Read the values
                            IReferenceRecord record = new ReferenceRecord();
                            for (var index = 0; index < header.Count; index++)
                            {
                                var key = header[index];
                                var value = splittedLine[index];
                                if (null != ValueFactory)
                                {
                                    value = ValueFactory.CreateValue(value);
                                }
                                if (null != RecordBuilder)
                                {
                                    record = RecordBuilder.BuildRecord(record, key, value);
                                }
                                else if (!record.Properties.ContainsKey(key))
                                {
                                    record.Properties.Add(key, value);
                                }
                            }

                            // Add the record
                            dataset.Records.Add(record);
                            if (0 == dataset.Records.Count % ChunkSize)
                            {
                                _logger.Info(@"{0} records created.", dataset.Records.Count);
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(@"Error during record creation! '{0}'", ex.Message);
                        }
                    }
                }
            }
            return dataset;
        }

        /// <summary>
        /// Streams all records of a CSV file using the specified delimiter.
        /// </summary>
        /// <param name="fileInfo">The CSV file to read.</param>
        /// <param name="readHeader"><c>true</c> if the first row contains the header.</param>
        /// <param name="delimiter">The delimiter for the values.</param>
        /// <returns>The records of this file.</returns>
        public IEnumerable<IReferenceRecord> StreamFile(FileInfo fileInfo, bool readHeader, char delimiter)
        {
            var header = new List<string>();
            using (var reader = new StreamReader(fileInfo.OpenRead()))
            {
                string line;
                const int ChunkSize = 1000000;
                while (null != (line = reader.ReadLine()))
                {
                    var splittedLine = line.Split(delimiter);
                    if (!header.Any())
                    {
                        // Read the header
                        if (readHeader)
                        {
                            foreach (var headerValue in splittedLine)
                            {
                                header.Add(headerValue);
                            }
                            continue;
                        }
                        else
                        {
                            for (var index = 0; index < splittedLine.Length; index++)
                            {
                                header.Add(string.Format(@"FIELD{0}", index + 1));
                            }
                        }
                    }

                    if (header.Count == splittedLine.Length)
                    {
                        // Read the values
                        IReferenceRecord record = new ReferenceRecord();
                        for (var index = 0; index < header.Count; index++)
                        {
                            var key = header[index];
                            var value = splittedLine[index];
                            if (null != ValueFactory)
                            {
                                value = ValueFactory.CreateValue(value);
                            }
                            if (null != RecordBuilder)
                            {
                                record = RecordBuilder.BuildRecord(record, key, value);
                            }
                            else if (!record.Properties.ContainsKey(key))
                            {
                                record.Properties.Add(key, value);
                            }
                        }

                        // Stream the record
                        yield return record;
                    }
                }
            }
        }
    }
}


text:
Proof of concept implementations that simulate main functionalities
of Docker.
Intended only to deep knowledge about GNU/Linux containers not for real world
usage.

Different tools for container management (Docker, rkt) on GNU/Linux are basically
"wrappers on steroids" around specific Linux kernel capabilities, most notably ones
are:

- chroots
- namespaces
- cgroups

# Note
For safety only execute this code inside a test VM.

redpajama_set_name: RedPajamaGithub


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dominion.Cards.Treasure;
using Dominion.Rules;
using Dominion.Rules.CardTypes;

namespace Dominion.Cards.Actions
{
    public class CountingHouse : Card, IActionCard
    {
        public CountingHouse() : base(5)
        {
        }

        public void Play(TurnContext context)
        {
            var coppers = context.ActivePlayer.Discards.OfType<Copper>().ToList();

            if (coppers.Count > 0)
            {
                context.Game.Log.LogMessage("{0} put {1} Copper from their discard pile into their hand",
                                            context.ActivePlayer.Name, coppers.Count);

                foreach (var copper in coppers)
                    copper.MoveTo(context.ActivePlayer.Hand);
            }
            else
            {
                context.Game.Log.LogMessage("Counting house did nothing - no Copper in discard pile.");
            }

        }
    }
}


text:
import implicitRoles from './implicitRoles';

/**
 * Returns an element's implicit role given its attributes and type.
 * Some elements only have an implicit role when certain props are defined.
 *
 * @param type - The node's tagName.
 * @param attributes - The collection of attributes on the node.
 * @returns {String} - String representing the node's implicit role or '' if it doesn't exist.
 */
export default function getImplicitRole(type, attributes) {
  const normalizedType = type.toUpperCase();

  if (implicitRoles[normalizedType]) {
    return implicitRoles[normalizedType](attributes);
  }

  return '';
}


text:
// Generated from /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/rt.jar

#pragma once

#include <fwd-POI.hpp>
#include <java/lang/fwd-POI.hpp>
#include <java/util/regex/fwd-POI.hpp>
#include <java/util/regex/Pattern_Node.hpp>

struct default_init_tag;

class java::util::regex::Pattern_UnixDollar final
    : public Pattern_Node
{

public:
    typedef Pattern_Node super;

public: /* package */
    bool multiline {  };

protected:
    void ctor(bool mul);

public: /* package */
    bool match(Matcher* matcher, int32_t i, ::java::lang::CharSequence* seq) override;
    bool study(Pattern_TreeInfo* info) override;

    // Generated
    Pattern_UnixDollar(bool mul);
protected:
    Pattern_UnixDollar(const ::default_init_tag&);

public:
    static ::java::lang::Class *class_();

private:
    virtual ::java::lang::Class* getClass0();
};


text:
layout: page
title: Tracker Cliffs Enterprises Trade Fair
date: 2016-05-24
author: Alan Bell
tags: weekly links, java
status: published
summary: Vestibulum ante ipsum primis in faucibus orci.
banner: images/banner/office-01.jpg
booking:
  startDate: 05/14/2018
  endDate: 05/19/2018
  ctyhocn: ALOIAHX
  groupCode: TCETF
published: true
---
Etiam eleifend sem id bibendum efficitur. Integer sit amet sapien in velit ullamcorper sagittis quis sed mauris. Proin posuere sem quis nisl maximus iaculis. Proin id magna aliquam, vulputate nisl vel, dictum purus. Etiam tempus metus placerat, auctor risus in, gravida dui. Mauris a varius lectus, nec volutpat turpis. Vivamus sollicitudin augue augue, nec rhoncus leo tincidunt porttitor. Vivamus iaculis, nunc semper cursus vulputate, tortor nibh porta augue, id venenatis nibh magna varius ex. Aenean molestie hendrerit dui venenatis tincidunt. Pellentesque pellentesque consequat quam quis interdum.

* Morbi tincidunt ex sit amet augue congue, nec fringilla elit tristique
* In luctus dolor in mi scelerisque, sit amet placerat elit rutrum
* Ut et risus a magna venenatis finibus sit amet vitae odio
* Mauris eget arcu varius, lacinia erat a, congue nisi
* Etiam dapibus nunc vel scelerisque cursus.

Nulla maximus ipsum vel enim lacinia, ac pharetra orci ullamcorper. Phasellus ut mattis lorem. Fusce imperdiet dui ut nisi iaculis, semper sodales augue luctus. Nulla ullamcorper luctus mi vel convallis. Fusce tristique, lectus ut blandit lobortis, libero tortor viverra mi, eget cursus metus dui ac risus. Ut ultricies dictum libero. Nulla dapibus ipsum eu tortor imperdiet, at finibus odio lacinia. Praesent dapibus nisi et magna convallis ornare sed non nulla. Aliquam vel ullamcorper enim, a rutrum arcu.


text:
[![Latest Stable Version](https://poser.pugx.org/pomm-project/foundation/v/stable)](https://packagist.org/packages/pomm-project/foundation) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/pomm-project/Foundation/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/pomm-project/Foundation/?branch=master) [![Build Status](https://travis-ci.org/pomm-project/Foundation.svg?branch=master)](https://travis-ci.org/pomm-project/Foundation) [![Monthly Downloads](https://poser.pugx.org/pomm-project/foundation/d/monthly.png)](https://packagist.org/packages/pomm-project/foundation) [![License](https://poser.pugx.org/pomm-project/foundation/license.svg)](https://packagist.org/packages/pomm-project/foundation)

This is the **experimental branch** of the  foundation component of [Pomm Project](http://www.pomm-project.org).

## What is Foundation ?

Foundation is the main block of Pomm database framework. It makes clients able to communicate either with the database or with each others through a session. One of these clients -- the query manager -- can make Foundation to be used as DBAL replacement. If you are looking for a library to use PostgreSQL in your web development, you might want to look at [Pomm's model manager](https://github.com/pomm-project/ModelManager). If you want to create a custom database access layer or just perform SQL queries, Foundation is the right tool.

Foundation provides out of the box:

 * Converters (all built-in Postgresql types are supported + arrays, HStore etc.) see [this SO comment](http://stackoverflow.com/questions/31643297/pg-query-result-contains-strings-instead-of-integer-numeric/31740990#31740990).
 * Prepared Queries.
 * Parametrized queries.
 * Seekable iterators on results.
 * LISTEN / NOTIFY asynchronous messages support.
 * Service manager for easy integration with dependency injection containers.

[See more with code examples on this blog post](http://www.pomm-project.org/news/a-short-focus-on-pomm-s-foundation.html).

## Requirements

This branch is tested against the following configurations:
 * PHP 5.6, 7.0, 7.1, 7.2, 7.3 and uses mod-pgsql only (not PDO)
 * Postgresql 9.4

Pomm might work with older versions of PHP or Postgres but this is not tested and can be broken any time.

## Installation

Pomm components are available on [packagist](https://packagist.org/packages/pomm-project/) using [composer](https://packagist.org/). To install and use Pomm's foundation, add a require line to `"pomm-project/foundation"` in your `composer.json` file.

**Note:** It is important the PHP configuration file defines the correct [timezone](http://php.net/manual/en/datetime.configuration.php) setting. Pomm also sets the PostgreSQL connection to this timezone to prevent time shifts between the application and the database.

## Documentation

Foundation's documentation is available [either online](https://github.com/pomm-project/Foundation/blob/master/documentation/foundation.rst) or directly in the `documentation` folder of the project.

## Tests

This package uses [Atoum](https://github.com/atoum/atoum) as unit test framework. The tests are located in `sources/tests`. The test suite needs to access the database to ensure that read and write operations are made in a consistent manner. You need to set up a database for that and fill the `sources/tests/config.php` file with the according DSN. For convenience, Foundation provides two classes that extend `Atoum` with a `Session`:

 * `PommProject\Foundation\Tester\VanillaSessionAtoum`
 * `PommProject\Foundation\Tester\FoundationSessionAtoum`

Making your test class to extend one of these will grant them with a `buildSession` method that returns a newly created session. Clients of these classes must implement a `initializeSession(Session $session)` method (even a blank one). It is often a good idea to provide a fixture class as a session client, this method is the right place to register it.

## Known bugs

Unfortunately there is a bug we can not fix easily or without degrading performances of the whole stack:
 * The `ConvertedResultIterator` can not recognize custom composite types when they are defined in schemas other than `public`. This is because the `pg_type` function does not return the schema the type belongs to. There are not turns around unless the schema is inspected manually by issuing a lot of queries. (see #53)


text:
'use strict';

/**
 * Module dependencies.
 */
var mean = require('meanio'),
  compression = require('compression'),
  morgan = require('morgan'),
  consolidate = require('consolidate'),
  express = require('express'),
  helpers = require('view-helpers'),
  flash = require('connect-flash'),
  modRewrite = require('connect-modrewrite'),
  seo = require('mean-seo'),
  config = mean.loadConfig();

module.exports = function(app, db) {

  app.set('showStackError', true);

  // Prettify HTML
  app.locals.pretty = true;

  // cache=memory or swig dies in NODE_ENV=production
  app.locals.cache = 'memory';

  // Should be placed before express.static
  // To ensure that all assets and data are compressed (utilize bandwidth)
  app.use(compression({
    // Levels are specified in a range of 0 to 9, where-as 0 is
    // no compression and 9 is best compression, but slowest
    level: 9
  }));

  // Enable compression on bower_components
  app.use('/bower_components', express.static(config.root + '/bower_components'));

  // Only use logger for development environment
  if (process.env.NODE_ENV === 'development') {
    app.use(morgan('dev'));
  }

  // assign the template engine to .html files
  app.engine('html', consolidate[config.templateEngine]);

  // set .html as the default extension
  app.set('view engine', 'html');

  // Dynamic helpers
  app.use(helpers(config.app.name));

  // Connect flash for flash messages
  app.use(flash());

  app.use(modRewrite([
    '!^/api/.*|\\_getModules|\\.html|\\.js|\\.css|\\.swf|\\.jp(e?)g|\\.png|\\.gif|\\.svg|\\.eot|\\.ttf|\\.woff|\\.pdf$ / [L]'
  ]));

  app.use(seo());
};


text:
<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

// load config
require_once(dirname(__FILE__).'/config.php');

// get zoo instance
$zoo = App::getInstance('zoo');

// set zoo icon
$zoo->set('icon', 'zoo.png');

// add css, js
$zoo->document->addScript('libraries:jquery/jquery-ui.custom.min.js');
$zoo->document->addStylesheet('libraries:jquery/jquery-ui.custom.css');
$zoo->document->addScript('libraries:jquery/plugins/timepicker/timepicker.js');
$zoo->document->addStylesheet('libraries:jquery/plugins/timepicker/timepicker.css');
$zoo->document->addScript('assets:js/accordionmenu.js');
$zoo->document->addScript('assets:js/placeholder.js');
$zoo->document->addScript('assets:js/default.js');
$zoo->document->addStylesheet('assets:css/ui.css');

// add behavior modal
$zoo->html->_('behavior.modal', 'a.modal');

// init vars
$controller = $zoo->request->getWord('controller');
$task       = $zoo->request->getWord('task');
$group      = $zoo->request->getString('group');

// check access level
if (!$zoo->user->canManage() ||
	(($controller === 'new' || $controller === 'manager') && !$zoo->user->isAdmin())) {
    return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
}

// does the zoo require to be updated?
if ($zoo->update->required() && $controller != 'update') {
	$zoo->system->application->redirect($zoo->link(array('controller' => 'update'), false));
}

// check for ZOO extension dependencies
$zoo->dependency->check();

// cache writable ?
if (!($cache_path = $zoo->path->path('cache:')) || !is_writable($cache_path)) {
	$zoo->error->raiseNotice(0, sprintf("Zoo cache folder is not writable! Please check directory permissions (%s)", $cache_path));
}

// media folders writable ?
foreach (array_merge(array(''), $zoo->path->dirs('media:zoo', true)) as $dir) {
	if (!is_writable($zoo->path->path('media:zoo/'.$dir))) {
		$zoo->error->raiseNotice(0, sprintf("Zoo media folder is not writable! Please check directory permissions (%s)", $zoo->path->path('media:zoo/'.$dir)));
	}
}

// change application
if ($id = $zoo->request->getInt('changeapp')) {
	$zoo->system->application->setUserState('com_zooapplication', $id);
}

// load application
$application = $zoo->zoo->getApplication();

// set default controller
if (!$controller) {
	$controller = $application ? 'item' : 'new';
	$zoo->request->setVar('controller', $controller);
}

// build menu
$menu = $zoo->menu->get('nav');

// add "app" menu items
if ($instances = $zoo->table->application->all(array('order' => 'name'))) {
	foreach ($instances as $instance) {
		$instance->addMenuItems($menu);
	}
}

// add "new" and "manager" menu item
if ($zoo->user->isAdmin()) {
	$new = $zoo->object->create('AppMenuItem', array('new', ' ', $zoo->link(array('controller' => 'new')), array('class' => 'new')));
	$manager = $zoo->object->create('AppMenuItem', array('manager', ' ', $zoo->link(array('controller' => 'manager')), array('class' => 'config')));
	$menu->addChild($new);
	$menu->addChild($manager);
}

if ($controller == 'new' && $task == 'add' && $group) {
	// add info item
	$new->addChild($zoo->object->create('AppMenuItem', array('new', $zoo->object->create('Application')->setGroup($group)->getMetaData('name'))));
}

if ($controller == 'manager' && $group) {
	// add info item
	$link = $zoo->link(array('controller' => 'manager', 'task' => 'types', 'group' => $group));
	$info = $zoo->object->create('AppMenuItem', array('manager-types', $zoo->object->create('Application')->setGroup($group)->getMetaData('name'), $link));
	$info->addChild($zoo->object->create('AppMenuItem', array('manager-types', 'Types', $link)));
	$info->addChild($zoo->object->create('AppMenuItem', array('manager-info', 'Info', $zoo->link(array('controller' => 'manager', 'task' => 'info', 'group' => $group)))));
	$manager->addChild($info);
}

try {

	if ($application) {

		// dispatch current application
		$application->dispatch();

	} else {

		// dispatch app
		$zoo->dispatch();
	}

} catch (AppException $e) {
	$zoo->error->raiseError(500, $e);
}


text:
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#include "queue.h"

struct _queue {
	const char **queue;
	size_t end;
	size_t write;
	size_t read;
	size_t len;
	pthread_mutex_t lock;
};

/**
 * Allocates a new queue structure with capacity to hold size elements.
 */
queue *
queue_new(size_t size)
{
	queue *ret = malloc(sizeof(queue));

	if (ret == NULL)
		return NULL;

	ret->queue = malloc(sizeof(char *) * size);
	if (ret->queue == NULL) {
		free(ret);
		return NULL;
	}

	memset(ret->queue, 0, sizeof(char *) * size);
	ret->end = size;
	ret->read = ret->write = 0;
	ret->len = 0;
	pthread_mutex_init(&ret->lock, NULL);

	return ret;
}

/**
 * Frees up allocated resources in use by the queue.  This doesn't take
 * into account any consumers at all.  That is, the caller needs to
 * ensure noone is using the queue any more.
 */
void
queue_destroy(queue *q)
{
	q->len = 0;
	pthread_mutex_destroy(&q->lock);
	free(q->queue);
	free(q);
}

/**
 * Enqueues the string pointed to by p at queue q.  If the queue is
 * full, the oldest entry is dropped.  For this reason, enqueuing will
 * never fail.  This function assumes the pointer p is a private copy
 * for this queue, and hence will be freed once processed.
 */
void
queue_enqueue(queue *q, const char *p)
{
	/* queue normal:
	 * |=====-----------------------------|  4
	 * ^    ^
	 * r    w
	 * queue wrap:
	 * |===---------------------------====|  6
	 *    ^                           ^
	 *    w                           r
	 * queue full
	 * |==================================| 23
	 *          ^
	 *         w+r
	 */
	pthread_mutex_lock(&q->lock);
	if (q->len == q->end) {
		if (q->read == q->end)
			q->read = 0;
		free((char *)(q->queue[q->read]));
		q->read++;
		q->len--;
	}
	if (q->write == q->end)
		q->write = 0;
	q->queue[q->write] = p;
	q->write++;
	q->len++;
	pthread_mutex_unlock(&q->lock);
}

/**
 * Returns the oldest entry in the queue.  If there are no entries, NULL
 * is returned.  The caller should free the returned string.
 */
const char *
queue_dequeue(queue *q)
{
	const char *ret;
	pthread_mutex_lock(&q->lock);
	if (q->len == 0) {
		pthread_mutex_unlock(&q->lock);
		return NULL;
	}
	if (q->read == q->end)
		q->read = 0;
	ret = q->queue[q->read++];
	q->len--;
	pthread_mutex_unlock(&q->lock);
	return ret;
}

/**
 * Returns at most len elements from the queue.  Attempts to use a
 * single lock to read a vector of elements from the queue to minimise
 * effects of locking.  Returns the number of elements stored in ret.
 * The caller is responsible for freeing elements from ret, as well as
 * making sure it is large enough to store len elements.
 */
size_t
queue_dequeue_vector(const char **ret, queue *q, size_t len)
{
	size_t i;

	pthread_mutex_lock(&q->lock);
	if (q->len == 0) {
		pthread_mutex_unlock(&q->lock);
		return 0;
	}
	if (len > q->len)
		len = q->len;
	for (i = 0; i < len; i++) {
		if (q->read == q->end)
			q->read = 0;
		ret[i] = q->queue[q->read++];
	}
	q->len -= len;
	pthread_mutex_unlock(&q->lock);

	return len;
}

/**
 * Puts the entry p at the front of the queue, instead of the end, if
 * there is space available in the queue.  Returns 0 when no space is
 * available, non-zero otherwise.  Like queue_enqueue,
 * queue_putback assumes pointer p points to a private copy for the
 * queue.
 */
char
queue_putback(queue *q, const char *p)
{
	pthread_mutex_lock(&q->lock);
	if (q->len == q->end) {
		pthread_mutex_unlock(&q->lock);
		return 0;
	}

	if (q->read == 0)
		q->read = q->end;
	q->read--;
	q->queue[q->read] = p;
	q->len++;
	pthread_mutex_unlock(&q->lock);

	return 1;
}

/**
 * Returns the (approximate) size of entries waiting to be read in the
 * queue.  The returned value cannot be taken accurate with multiple
 * readers/writers concurrently in action.  Hence it can only be seen as
 * mere hint about the state of the queue.
 */
inline size_t
queue_len(queue *q)
{
	return q->len;
}

/**
 * Returns the (approximate) size of free entries in the queue.  The
 * same conditions as for queue_len apply.
 */
inline size_t
queue_free(queue *q)
{
	return q->end - q->len;
}

/**
 * Returns the size of the queue.
 */
inline size_t
queue_size(queue *q)
{
	return q->end;
}


text:
import logging

from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.contrib import messages

from braintree import Customer
from django_common.http import JsonResponse
from django_common.helper import form_errors_serialize
from django_common.decorators import ssl_required

from django_braintree.forms import UserCCDetailsForm
from django_braintree.models import UserVault

BAD_CC_ERROR_MSG = 'Oops! Doesn\'t seem like your Credit Card details are correct. Please re-check and try again.'

@ssl_required()
@login_required
def payments_billing(request, template='django_braintree/payments_billing.html'):
    """
    Renders both the past payments that have occurred on the users credit card, but also their CC information on file
    (if any)
    """
    d = {}

    if request.method == 'POST':
        # Credit Card is being changed/updated by the user
        form = UserCCDetailsForm(request.user, True, request.POST)
        if form.is_valid():
            response = form.save()
            if response.is_success:
                messages.add_message(request, messages.SUCCESS, 'Your credit card information has been securely saved.')
                return JsonResponse()
            else:
                return JsonResponse(success=False, errors=[BAD_CC_ERROR_MSG])

        return JsonResponse(success=False, data={'form': form_errors_serialize(form)})
    else:
        if UserVault.objects.is_in_vault(request.user):
            try:
                response = Customer.find(UserVault.objects.get_user_vault_instance_or_none(request.user).vault_id)
                d['current_cc_info'] = response.credit_cards[0]
            except Exception, e:
                logging.error('Unable to get vault information for user from braintree. %s' % e)
        d['cc_form'] = UserCCDetailsForm(request.user)

    return render(request, template, d)


text:
from __future__ import unicode_literals, absolute_import, print_function

from wordfilter import Wordfilter
import json
import re
import requests
import time
import lxml.html
from . import logger
from io import BytesIO

log = logger.get("common")

MEDIAWIKI_API = "https://commons.wikimedia.org/w/api.php"

HEADERS = {"User-Agent":  "picdescbot, http://github.com/elad661/picdescbot"}

supported_formats = re.compile('\.(png|jpe?g|gif)$', re.I)
word_filter = Wordfilter()

# I really don't want the bot to show this kind of imagery!
word_filter.add_words(['nazi', 'hitler', 'reich'])

# I can't trust Microsoft's algorithm to not be racist, so I should probably
# make the bot avoid posting images with the following words in them.
# I'm not using wordfilter here because it would over-filter in some cases.
# also filter "gun" because this is not the kind of content I want the bot to post
# This is matched only against the caption generated by CVAPI.
extra_filter = {'ape', 'apes', 'monkey', 'monkeys', 'gun'}

# Blacklisted phrases (instead of words) to blacklist certain phrases
# in the wikimedia description
blacklisted_phrases = {'comic strip', 'logo', 'biblical illustration', 'church',
                       'historical document', 'donald trump'}

# Blacklist some categories, just in case. These are matched on a substring
# basis, against the page's categories and the titles of the wikipages using
# the picture.
category_blacklist = ['september 11', 'hitler', 'nazi', 'antisemit', 'libel',
                      'apartheid', 'racism', 'lynching', 'cartoons',
                      'holocaust', 'auschwitz', 'stereotypes', 'flags', 'porn',
                      'homophobia', 'transphobia', 'logos',
                      'scans from google books', 'little nemo',
                      'stolperstein', 'songbird specimens', 'terror',
                      'bible illustrations', 'jesuit symbols',
                      'christian symbols', 'symbols of religion',
                      'symbols of islam', 'jewish symbols', 'pistols',
                      'corpse', 'victim', 'ultrasound', 'donald trump']

# Gender neutralization helps prevent accidental transphobic juxtapositions
# which can occur when CVAPI uses gendered words in the description, but their
# gender detection is wrong. Computers shouldn't try to detect gender, and
# always be neautral. You can't know someone's gender just by how they look!
gendered_words = {'woman': 'person',
                  'man': 'person',
                  'women': 'people',
                  'man\'s': 'person\'s',
                  'woman\'s': 'person\'s',
                  'mans': 'persons',
                  'womans': 'persons',
                  'men': 'people',
                  'guy': 'person',
                  'boy': 'person',
                  'girl': 'person',
                  'boys': 'people',
                  'girls': 'people',
                  'lady': 'person',
                  'ladies': 'people',
                  'gentleman': 'person',
                  'gentlemen': 'people',
                  'female': '',
                  'male': '',
                  'she': 'they',
                  # It's probably more likely to say "woman and her phone" than
                  # "someone gives a phone to her", so their is probably better
                  # here. Would need more complex parsing to know for sure.
                  'her': 'their',
                  'hers': 'theirs',
                  'herself': 'themself',
                  'he': 'they',
                  'him': 'them',
                  # It's more likely to give "man and his phone" than "this
                  # phone is his", so "their" is better here than "theirs"
                  'his': 'their',
                  'himself': 'themself'}

def gender_neutralize(phrase):
    "Replace gendered words in the phrase with neutral ones"
    neutralized = []
    for word in phrase.lower().split():
        if word in gendered_words:
            word = gendered_words[word]
        if word != '':
            neutralized.append(word)
    neutralized = ' '.join(neutralized)
    if neutralized != phrase:
        log.info('Gender neutralized: "{0}" => "{1}"'.format(phrase, neutralized))
    return neutralized

tags_blacklist = {'text', 'screenshot', 'military', 'church'}

def tag_blacklisted(tags):
    for tag in tags:
        if tag in tags_blacklist:
            return True
    return False

def is_blacklisted(caption):
    """ Check caption for forbidden words"""
    if "a suit and tie" in caption:
        return True
    if word_filter.blacklisted(caption):
        return True
    for word in caption.split():
        if word in extra_filter:
            return True
    return False

def remove_html_tags(text):
    """ Remove all HTML tags (and properties) from a string """
    return ' '.join(lxml.html.fromstring(text).itertext())

def log_discarded(url, reason, description=None):
    line = "Discarded {0} because of {1}".format(url, reason)
    if description is not None:
        line += ' - "{0}"'.format(description)
    log.warning(line)

def get_picture(filename=None):
    """Get a picture from Wikimedia Commons. A random picture will be returned if filename is not specified
    Returns None when the result is bad"""
    params = {"action": "query",
              "prop": "imageinfo|categories|globalusage",
              "iiprop": "url|size|extmetadata|mediatype",
              "iiurlheight": "1080",
              "format": "json"}
    if filename is None:
        params['generator'] = 'random'
        params['grnnamespace'] = '6'
    else:
        params['titles'] = 'File:%s' % filename

    response = requests.get(MEDIAWIKI_API,
                            params=params,
                            headers=HEADERS).json()
    page = list(response['query']['pages'].values())[0]  # This API is ugly
    imageinfo = page['imageinfo'][0]
    url = imageinfo['url']
    extra_metadata = imageinfo['extmetadata']

    # check that the file is actually a picture
    if imageinfo['mediatype'] != "BITMAP":
        return None

    # Make sure the picture is big enough
    if imageinfo['width'] <= 50 or imageinfo['height'] <= 50:
        return None

    # Make sure the format is supported
    if not supported_formats.search(url):
        return None

    # We got a picture, now let's verify we can use it.
    if word_filter.blacklisted(page['title']):  # Check file name for bad words
        log_discarded(url, 'badword in page title: "{0}"'.format(page['title']))
        return None
    # Check picture title for bad words
    if word_filter.blacklisted(extra_metadata['ObjectName']['value']):
        log_discarded(url, 'badword in picture title: "{0}"'.format(extra_metadata['ObjectName']['value']))
        return None
    # Check restrictions for more bad words
    if word_filter.blacklisted(extra_metadata['Restrictions']['value']):
        log_discarded(url, 'badword in restrictions: "{0}"'.format(extra_metadata['Restrictions']['value']))
        return None

    # Check file description for bad words
    if 'ImageDescription' in extra_metadata:
        cleaned_description = remove_html_tags(extra_metadata['ImageDescription']['value'])
        if word_filter.blacklisted(cleaned_description):
            log_discarded(url, 'badword in image description: "{0}"'.format(cleaned_description))
            return None

        for phrase in blacklisted_phrases:
            if phrase in cleaned_description.lower().strip():
                log_discarded(url, 'blacklisted phrase "{0}" found in description "{1}"'.format(phrase, cleaned_description))
                return None

    # The mediawiki API is awful, there's another list of categories which
    # is not the same as the one requested by asking for "categories".
    # Fortunately it's still in the API response, under extmetadata.

    extra_categories = extra_metadata['Categories']['value'].lower()

    for blacklisted_category in category_blacklist:
        for category in page['categories']:
            if blacklisted_category in category['title'].lower():
                log_discarded(url, 'blacklisted category "{0}"'.format(category['title']))
                return None

        if blacklisted_category in extra_categories:
            log_discarded(url, 'blacklisted category "{0}" (in extra)'.format(blacklisted_category))
            return None

    # TODO check parent categories for each category in metadata,
    # and compare them against the blacklist too. This will require
    # extra API calls

    # if the picture is used in any wikipage with unwanted themes, we probably
    # don't want to use it.
    for wikipage in page['globalusage']:
        if word_filter.blacklisted(wikipage['title'].lower()):
            log_discarded(url, 'page usage "{0}"'.format(wikipage['title']))
            return None
        for blacklisted_category in category_blacklist:
            if blacklisted_category in wikipage['title']:  # substring matching
                log_discarded(url, 'page usage "{0}"'.format(wikipage['title']))
                return None
    return imageinfo

class CVAPIClient(object):
    "Microsoft Cognitive Services Client"
    def __init__(self, apikey, endpoint):
        self.apikey = apikey
        self.endpoint = endpoint + '/analyze'

    def describe_picture(self, url):
        "Get description for a picture using Microsoft Cognitive Services"
        params = {'visualFeatures': 'Description,Adult'}
        json = {'url': url}
        headers = {'Content-Type': 'application/json',
                   'Ocp-Apim-Subscription-Key': self.apikey}

        result = None
        retries = 0

        while retries < 15 and not result:
            response = requests.post(self.endpoint, json=json, params=params,
                                     headers=headers)
            if response.status_code == 429:
                log.error("Error from mscognitive: %s" % (response.json()))
                if retries < 15:
                    time.sleep(2)
                    retries += 1
                else:
                    log.error('failed after retrying!')

            elif response.status_code == 200 or response.status_code == 201:
                result = response.json() if response.content else None
            else:
                log.error("Error code: %d" % (response.status_code))
                log.error("url: %s" % url)
                try:
                    log.error(response.json())
                except:
                    log.error(response.text)
                retries += 1
                sleep = 20 + retries*4
                log.info("attempt: {0}, sleeping for {1}".format(retries, sleep))
                time.sleep(sleep)

        return result

    def get_picture_and_description(self, filename=None, max_retries=20):
        "Get a picture and a description. Retries until a usable result is produced or max_retries is reached."
        pic = None
        retries = 0
        while retries <= max_retries:  # retry max 20 times, until we get something good
            while pic is None:
                pic = get_picture(filename)
                if pic is None:
                    # We got a bad picture, let's wait a bit to be polite to the API server
                    time.sleep(1)
            url = pic['url']
            # Use a scaled-down image if the original is too big
            if pic['size'] > 3000000 or pic['width'] > 8192 or pic['height'] > 8192:
                url = pic['thumburl']

            result = self.describe_picture(url)

            if result is not None:
                description = result['description']
                adult = result['adult']
                if not adult['isAdultContent'] and not adult['isRacyContent']:  # no nudity and such
                    if len(description['captions']) > 0:
                        caption = description['captions'][0]['text']
                        caption = gender_neutralize(caption)
                        if not is_blacklisted(caption):
                            if not tag_blacklisted(description['tags']):
                                return Result(caption,
                                              description['tags'], url,
                                              pic['descriptionshorturl'])
                            else:
                                log_discarded(url, "tag blacklist", caption)
                                log.warning('tags: %s' % description['tags'])
                        else:
                            log_discarded(url, "caption blacklist", caption)
                    else:
                        log.warning("No caption for url: {0}".format(url))
                else:
                    log_discarded(url, "adult content", description['captions'])
            retries += 1
            log.warning("Not good, retrying...")
            pic = None
            time.sleep(3)  # sleep to be polite to the API servers

        raise Exception("Maximum retries exceeded, no good picture")

class NonClosingBytesIO(BytesIO):
    """" Like BytesIO, but doesn't close so easily.
    To prevent tweepy from closing the picture on error, this class requires
    to be specifically closed by adding a boolean parameter to the close() method.
    """

    def close(self, really=False):
        """ Close the BytesIO object, but only if you're really sure """
        if really:
            return super().close()

class Result(object):
    "Represents a picture and its description"
    def __init__(self, caption, tags, url, source_url):
        self.caption = caption
        self.tags = tags
        self.url = url
        self.source_url = source_url

    def download_picture(self):
        "Returns a BytesIO object for an image URL"
        retries = 0
        picture = None
        log.info("downloading " + self.url)
        while retries <= 20:
            if retries > 0:
                log.info('Trying again...')

            try:
                response = requests.get(self.url, headers=HEADERS)
            except requests.exceptions.RequestException as e:
                log.exception(e)
                response = None

            if response is not None and response.status_code == 200:
                picture = NonClosingBytesIO(response.content)
                return picture
            else:
                log.error("Fetching picture failed: " + response.status_code)
                retries += 1
                time.sleep(3)
        log.error("Maximum retries exceeded when downloading a picture")
        raise Exception("Maximum retries exceeded when downloading a picture")


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title>H-Index</title>
<meta name="date" content="2017-06-13">
<link rel="stylesheet" type="text/css" href="../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Skip navigation links

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev Letter

Next Letter

Frames

No Frames

All Classes

A B C D E F G H I J L M N O P Q R S T U V W X Y

H

handleButtonClick_GraphPane(MouseEvent) - Method in class application.MainController

handleMenuItem_About(ActionEvent) - Method in class application.MainController

handleMenuItem_AnimationSettings(ActionEvent) - Method in class application.MainController

handleMenuItem_Close(ActionEvent) - Method in class application.MainController

handleMenuItem_Debug(ActionEvent) - Method in class application.MainController

handleMenuItem_Delete(ActionEvent) - Method in class application.MainController

handleMenuItem_NextStep(ActionEvent) - Method in class application.MainController

handleMenuItem_OpenFile(ActionEvent) - Method in class application.MainController

handleMenuItem_RandomGraph(ActionEvent) - Method in class application.MainController

handleMenuItem_RunAnimation(ActionEvent) - Method in class application.MainController

handleMenuItem_Save(ActionEvent) - Method in class application.MainController

Gestisce il salvataggio normale

handleMenuItem_SaveAs(ActionEvent) - Method in class application.MainController

Gestisce il salvataggio "personalizzato"

handleMenuItem_Stop(ActionEvent) - Method in class application.MainController

handleMouseMove_GraphPane(MouseEvent) - Method in class application.MainController

hashCode() - Method in class model.arrow.Arrow

head - Variable in class model.queue.Queue

A B C D E F G H I J L M N O P Q R S T U V W X Y

Skip navigation links

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev Letter

Next Letter

Frames

No Frames

All Classes