text:
from JumpScale import j

# import JSModel
import time
# from abc import ABCMeta, abstractmethod
import collections
import msgpack

import snappy

import re

# IMPORTANT: had to remove class for manipulating kvs object, class was more clear but 2 issues
# - this needs to be converted to a functional C module which can be reused in many languages, doing this in OO is much more complex
# - its slower, python is not very efficient with objects
# - if its too slow now we can go to cython module quite easily because is just a method
# - suggest to use nimlang to create c module

class KeyValueStoreBase:  # , metaclass=ABCMeta):
    '''KeyValueStoreBase defines a store interface.'''

    def __init__(self, namespace, name="", serializers=[], masterdb=None, cache=None, changelog=None):
        self.namespace = namespace
        self.name = name
        self.logger = j.logger.get('j.servers.kvs')
        self.serializers = serializers or list()
        self.unserializers = list(reversed(self.serializers))
        self.cache = cache
        self.changelog = changelog
        self.masterdb = masterdb
        self._schema = b""
        self.owner = j.application.owner
        self.inMem = False

    def __new__(cls, *args, **kwargs):
        '''
        Copies the doc strings (when available) from the base implementation
        '''

        attrs = iter(list(cls.__dict__.items()))

        for attrName, attr in attrs:
            if not attr.__doc__ and\
               hasattr(KeyValueStoreBase, attrName) and\
               not attrName.startswith('_')\
               and isinstance(attr, collections.Callable):

                baseAttr = getattr(KeyValueStoreBase, attrName)
                attr.__doc__ = baseAttr.__doc__

        return object.__new__(cls)

    @property
    def schema(self):
        return j.data.hash.bin2hex(self._schema).decode()

    @schema.setter
    def schema(self, val):
        """
        is any free text to define the schema used for this store category
        """
        if len(val) == 32:
            val = j.data.hash.hex2bin(val)
        elif len(val) != 16:
            raise j.exceptions.Input(message="schema needs to be 32 or 16 bytes",
                                     level=1, source="", tags="", msgpub="")
        if not j.data.types.bytes.check(val):
            raise j.exceptions.Input(message="schema needs to be in bytes", level=1, source="", tags="", msgpub="")
        self._schema = val

    @property
    def owner(self):
        """
        to define who owns this object, normally when you open a store, you set the owner if not the default
        """
        return j.data.hash.bin2hex(self._owner).decode()

    @owner.setter
    def owner(self, val):
        if len(val) == 32:
            val = j.data.hash.hex2bin(val)
        elif len(val) != 16:
            raise j.exceptions.Input(message="owner needs to be 32 or 16 bytes",
                                     level=1, source="", tags="", msgpub="")
        if not j.data.types.bytes.check(val):
            raise j.exceptions.Input(message="owner needs to be in bytes", level=1, source="", tags="", msgpub="")

        self._owner = val

    def _encode(self, val, expire=0, acl={}):

        # data = $type + $owner + $schema + $expire + $lengthacllist +
        # [acllist] + snappyencoded(val) + $crcOfAllPrevious

        #
        # serialize type  (is more like a version e.g. version 1.0)
        #

        # type of this encoding, to make sure we have backwards compatibility
        # type = 4bits:  $schemaYesNo,$expireYesNo,0,0 + 4 bit version of format now 0
        ttype = 0

        if self._schema != b"":
            ttype += 0b1000000

        if expire != 0:
            expire = j.data.time.getTimeEpoch() + expire
            ttype += 0b0100000
            expireb = expire.to_bytes(4, byteorder='big', signed=False)
        else:
            expireb = b""

        # data should be binary if not lets msgpack
        if j.data.types.bytes.check(val) == False:
            val = j.data.serializer.msgpack.dumps(val)
            ttype += 0b0010000

        typeb = ttype.to_bytes(1, byteorder='big', signed=False)

        aclb = j.servers.kvs._aclSerialze(acl)

        # TODO: *1 not sure this is ok, why str encode, what if its binary?
        # val = str.encode(val)
        val = snappy.compress(val)

        serialized = typeb + self._owner + self._schema + expireb + aclb + val

        # checksum
        crc = j.data.hash.crc32_string(serialized)
        crc = crc.to_bytes(4, byteorder='big', signed=False)

        return serialized + crc

    def _decode(self, data):
        """
        @return [val,owner="",schema="",expire=0,acl={}]
        """

        crcint = j.data.hash.crc32_string(data[:-4])
        crc = crcint.to_bytes(4, byteorder='big', signed=False)

        if not crc == data[-4:]:
            raise j.exceptions.Input(message="Invalid checksum (CRC), is this a valid object ?:%s" % data)

        #
        # parsing header
        #
        header = data[0]

        counter = 1
        owner = j.data.hash.bin2hex(data[counter:counter + 16]).decode()

        counter += 16

        if header & 0b1000000:
            # schema defined
            schema = j.data.hash.bin2hex(data[counter:counter + 16])
            counter += 16
        else:
            # no schema
            schema = ""

        if header & 0b0100000:
            # expire is set
            expire = int.from_bytes(data[counter:counter + 4], byteorder='big', signed=False)
            counter += 4
        else:
            expire = 0

        nrsecrets = int.from_bytes(data[counter:counter + 1], byteorder='big', signed=False)
        aclbin = data[counter:counter + 17 * nrsecrets + 1]
        counter += 17 * nrsecrets + 1

        acl = j.servers.kvs._aclUnserialze(aclbin)

        val = data[counter:-4]

        val = snappy.decompress(val)

        if header & 0b0010000:
            val = j.data.serializer.msgpack.loads(val)

        return (val, owner, schema, expire, acl)

    def set(self, key, value=None, expire=None, acl={}, secret=""):
        """
        @param secret, when not specified the owner will be used, allows to specify different secret than youw own owner key
        @param expire is seconds from now, when obj will expire
            if you want to set then needs to be an int>0 or 0

        """

        res = self.getraw(key, secret=secret, die=False, modecheck="w")

        if res != None:
            (valOld, owner, schemaOld, expireOld, aclOld) = res

            if schemaOld != self.schema:
                msg = "schema of this db instance should be same as what is found in db"
                raise j.exceptions.Input(message=msg, level=1)

            if expire == None:
                expire = expireOld

            if value == None:
                value = valOld

            acl.update(aclOld)

        else:
            if expire == None:
                expire = 0

            if value == None:
                raise j.exceptions.Input(message="value needs to be set (not None), key:%s" %
                                         key, level=1, source="", tags="", msgpub="")

        value2 = self.serialize(value)

        data = self._encode(value2, expire, acl)

        # the expire is non-generic as it only translates to the redis core does not break at the moment.
        # any _set should take expire to allow compatability.
        self._set(key, data, expire)

        # if self.cache != None:
        #     self.cache._set(key=key, category=category, value=value1)

    def exists(self, key, secret=""):
        try:
            res = self.get(key, secret=secret)
        except Exception as e:
            if "not allowed" in str(e):
                # exists but no access, should just return False
                # return False
                # raise j.exceptions.Input(message="Object '%s' does exist but I have not rights." %
                                        #  key, level=1, source="", tags="", msgpub="")
                return True
            if "Cannot find" in str(e):
                return False
            raise e
        return res != None

    def get(self, key, secret=""):
        '''
        Gets a key value pair from the store

        @param: key of the key value pair
        @type: String

        @return: value of the key value pair

        '''
        return self.getraw(key, secret, die=True)[0]

    def getraw(self, key, secret="", die=False, modecheck="r"):
        '''
        Gets a key value pair from the store

        @param: key of the key value pair
        @type: String

        @param: modecheck is r,w or d, normally get always needs to check on r but can overrule this, can be more than 1 e.g. rw

        @return: (val, owner, schema, expire, acl)

        '''

        # if self.cache != None:
        #     res = self.cache._get(key=key, category=category)  # get raw data
        #     if res != None:
        #         return self.unserialize(self._decode(res))

        data = self._get(key)

        if data is None:
            if die:
                raise j.exceptions.Input(message="Cannot find object: %s" % key, level=1, source="", tags="", msgpub="")
            else:
                return None

        if secret == "":
            secret = self.owner

        (val, owner, schema, expire, acl) = self._decode(data)

        if j.servers.kvs._aclCheck(acl, owner, secret, modecheck) is False:
            raise j.exceptions.Input(message="cannot get obj with key '%s' because mode '%s' is not allowed." % (
                key, modecheck), level=1, source="", tags="", msgpub="")

        val = self.unserialize(val)

        return (val, owner, schema, expire, acl)

    def delete(self, key, secret=""):

        val, owner, schema, expire, acl = self.getraw(key, secret=secret, modecheck='d', die=True)

        if secret is not None and secret != '' and owner != secret:
            raise j.exceptions.Input(message="Cannot delete object, only owner can delete an object",
                                     level=1, source="", tags="", msgpub="")

        self._delete(key=key)

    def serialize(self, value):
        for serializer in self.serializers:
            value = serializer.dumps(value)

        return value

    def unserialize(self, value):
        for serializer in self.unserializers:
            if value is not None:
                value = serializer.loads(value)

        return value

    def destroy(self):
        raise NotImplemented()

    def index(self, items, secret=""):
        """
        @param items is {indexitem:key}
            indexitem is e.g. $actorname:$state:$role (is a text which will be index to key)
                indexitems are always made lowercase
            key links to the object in the db
        ':' is not allowed in indexitem
        """
        if secret == "":
            secret = self.owner

        indexobj = self.getraw("index", die=False, secret=secret)

        if indexobj == None:
            ddict = {}
        else:
            ddict = msgpack.loads(indexobj)
        ddict.update(items)

        data2 = msgpack.dumps(ddict)
        self.set("index", data2, secret=secret)

    def index_remove(self, keys, secret=""):
        self.delete("index")

    def list(self, regex=".*", returnIndex=False, secret=""):
        """
        regex is regex on the index, will return matched keys
        e.g. .*:new:.* would match e.g. all obj with state new
        """

        indexobj = self.getraw("index", die=False, secret=secret)

        if indexobj == None:
            ddict = {}
        else:
            ddict = msgpack.loads(indexobj)

        res = set()
        for item, key in ddict.items():
            item = item.decode()
            key = key.decode()
            if re.match(regex, item) is not None:
                if returnIndex is False:
                    for key2 in key.split(","):
                        res.add(key2)
                else:
                    for key2 in key.split(","):
                        res.add((item, key2))
        return list(res)

# DO NOT LOOK AT BELOW RIGHT NOW IS FOR FUTURE

    # def checkChangeLog(self):
    #     pass

    #
    # def cacheSet(self, key, value, expirationInSecondsFromNow=120):
    #     ttime = j.data.time.getTimeEpoch()
    #     value = [ttime + expirationInSecondsFromNow, value]
    #     if key == "":
    #         key = j.data.idgenerator.generateGUID()
    #     self.set(category="cache", key=key, value=value)
    #     return key
    #
    # def cacheGet(self, key, deleteAfterGet=False):
    #     r = self.get("cache", key)
    #     if deleteAfterGet:
    #         self.delete("cache", key)
    #     return r[1]
    #
    # def cacheDelete(self, key):
    #     self.delete("cache", key)
    #
    # def cacheExists(self, key):
    #     return self.exists("cache", key)
    #
    # def cacheList(self):
    #
    #     if "cache" in self.listCategories():
    #         return self.list("cache")
    #     else:
    #         return []
    #
    # def cacheExpire(self):
    #     now = j.data.time.getTimeEpoch()
    #     for key in self.list():
    #         expiretime, val = self.get(key)
    #         if expiretime > now:
    #             self.delete("cache", key)
    #
    # @abstractmethod
    # def exists(self, category, key):
    #     '''
    #     Checks if a key value pair exists in the store.
    #
    #     @param: category of the key value pair
    #     @type: String
    #
    #     @param: key of the key value pair
    #     @type: String
    #
    #     @return: flag that states if the key value pair exists or not
    #     @rtype: Boolean
    #     '''
    #
    # @abstractmethod
    # def list(self, category, prefix):
    #     '''
    #     Lists the keys matching `prefix` in `category`.
    #
    #     @param category: category the keys should be in
    #     @type category: String
    #     @param prefix: prefix the keys should start with
    #     @type prefix: String
    #     @return: keys that match `prefix` in `category`.
    #     @rtype: List(String)
    #     '''
    #     raise j.exceptions.NotImplemented("list is only supported on selected db's")
    #
    # @abstractmethod
    # def listCategories(self):
    #     '''
    #     Lists the categories in this db.
    #
    #     @return: categories in this db
    #     @rtype: List(String)
    #     '''
    #
    # @abstractmethod
    # def _categoryExists(self, category):
    #     '''
    #     Checks if a category exists
    #
    #     @param category: category to check
    #     @type category: String
    #     @return: True if the category exists, False otherwise
    #     @rtype: Boolean
    #     '''
    #
    # def lock(self, locktype, info="", timeout=5, timeoutwait=0, force=False):
    #     """
    #     if locked will wait for time specified
    #     @param locktype of lock is in style machine.disk.import  (dot notation)
    #     @param timeout is the time we want our lock to last
    #     @param timeoutwait wait till lock becomes free
    #     @param info is info which will be kept in lock, can be handy to e.g. mention why lock taken
    #     @param force, if force will erase lock when timeout is reached
    #     @return None
    #     """
    #     category = "lock"
    #     lockfree = self._lockWait(locktype, timeoutwait)
    #     if not lockfree:
    #         if force == False:
    #             raise j.exceptions.RuntimeError("Cannot lock %s %s" % (locktype, info))
    #     value = [self.id, j.data.time.getTimeEpoch() + timeout, info]
    #     encodedValue = j.data.serializer.json.dumps(value)
    #     self.settest(category, locktype, encodedValue)
    #
    # def lockCheck(self, locktype):
    #     """
    #     @param locktype of lock is in style machine.disk.import  (dot notation)
    #     @return result,id,lockEnd,info  (lockEnd is time when lock times out, info is descr of lock, id is who locked)
    #                    result is False when free, True when lock is active
    #     """
    #     if self.exists("lock", locktype):
    #         encodedValue = self.get("lock", locktype)
    #         try:
    #             id, lockEnd, info = j.data.serializer.json.loads(encodedValue)
    #         except ValueError:
    #             self.logger.error("Failed to decode lock value")
    #             raise ValueError("Invalid lock type %s" % locktype)
    #
    #         if j.data.time.getTimeEpoch() > lockEnd:
    #             self.delete("lock", locktype)
    #             return False, 0, 0, ""
    #         value = [True, id, lockEnd, info]
    #         return value
    #     else:
    #         return False, 0, 0, ""
    #
    # def _lockWait(self, locktype, timeoutwait=0):
    #     """
    #     wait till lock free
    #     @return True when free, False when unable to free
    #     """
    #     locked, id, lockEnd, info = self.lockCheck(locktype)
    #     if locked:
    #         start = j.data.time.getTimeEpoch()
    #         if lockEnd + timeoutwait < start:
    #             # the lock was already timed out so is free
    #             return True
    #
    #         while True:
    #             now = j.data.time.getTimeEpoch()
    #             if now > start + timeoutwait:
    #                 return False
    #             if now > lockEnd:
    #                 return True
    #             time.sleep(0.1)
    #     return True
    #
    # def unlock(self, locktype, timeoutwait=0, force=False):
    #     """
    #     @param locktype of lock is in style machine.disk.import  (dot notation)
    #     """
    #     lockfree = self._lockWait(locktype, timeoutwait)
    #     if not lockfree:
    #         if force == False:
    #             raise j.exceptions.RuntimeError("Cannot unlock %s" % locktype)
    #     self.delete("lock", locktype)
    #
    # def incrementReset(self, incrementtype, newint=0):
    #     """
    #     @param incrementtype : type of increment is in style machine.disk.nrdisk  (dot notation)
    #     """
    #     self.set("increment", incrementtype, str(newint))
    #
    # def increment(self, incrementtype):
    #     """
    #     @param incrementtype : type of increment is in style machine.disk.nrdisk  (dot notation)
    #     """
    #     if not self.exists("increment", incrementtype):
    #         self.set("increment", incrementtype, "1")
    #         incr = 1
    #     else:
    #         rawOldIncr = self.get("increment", incrementtype)
    #         if not rawOldIncr.isdigit():
    #             raise ValueError("Increment type %s does not have a digit value: %s" % (incrementtype, rawOldIncr))
    #         oldIncr = int(rawOldIncr)
    #         incr = oldIncr + 1
    #         self.set("increment", incrementtype, str(incr))
    #     return incr
    #
    # def getNrRecords(self, incrementtype):
    #     if not self.exists("increment", incrementtype):
    #         self.set("increment", incrementtype, "1")
    #         incr = 1
    #     return int(self.get("increment", incrementtype))
    #
    # def settest(self, category, key, value):
    #     """
    #     if well stored return True
    #     """
    #     self.set(category, key, value)
    #     if self.get(category, key) == value:
    #         return True
    #     return False
    #
    # def _assertValidCategory(self, category):
    #     if not isinstance(category, str) or not category:
    #         raise ValueError('Invalid category, non empty string expected')
    #
    # def _assertValidKey(self, key):
    #     if not isinstance(key, str) or not key:
    #         raise ValueError('Invalid key, non empty string expected')
    #
    # def _assertExists(self, category, key):
    #     if not self.exists(category, key):
    #         errorMessage = 'Key value store doesnt have a value for key '\
    #             '"%s" in category "%s"' % (key, category)
    #         self.logger.error(errorMessage)
    #         raise KeyError(errorMessage)
    #
    # def _assertCategoryExists(self, category):
    #     if not self._categoryExists(category):
    #         errorMessage = 'Key value store doesn\'t have a category %s' % (category)
    #         self.logger.error(errorMessage)
    #         raise KeyError(errorMessage)
    #
    # def now(self):
    #     """
    #     return current time
    #     """
    #     return j.data.time.getTimeEpoch()
    #
    # def getModifySet(self, category, key, modfunction, **kwargs):
    #     """
    #     get value
    #     give as parameter to modfunction
    #     try to set by means of testset, if not succesfull try again, will use random function to maximize chance to set
    #     @param kwargs are other parameters as required (see usage in subscriber function)
    #     """
    #     counter = 0
    #     while counter < 30:
    #         data = self.get(category, key)
    #         data2 = modfunction(data)
    #         if self.settest(category, key, data2):
    #             break  # go out  of loop, could store well
    #         time.time.sleep(float(j.data.idgenerator.generateRandomInt(1, 10)) / 50)
    #         counter += 1
    #     return data2
    #
    # def subscribe(self, subscriberid, category, startid=0):
    #     """
    #     each subscriber is identified by a key
    #     in db there is a dict stored on key for category = category of this method
    #     value= dict with as keys the subscribers
    #     {"kristof":[lastaccessedTime,lastId],"pol":...}
    #
    #     """
    #     if not self.exists("subscribers", category):
    #         data = {subscriberid: [self.now(), startid]}
    #     else:
    #         if startid != 0:
    #             if not self.exists(category, startid):
    #                 raise j.exceptions.RuntimeError(
    #                     "Cannot find %s:%s in db, cannot subscribe, select valid startid" % (category, startid))
    #
    #             def modfunction(data, subscriberid, db, startid):
    #                 data[subscriberid] = [db.now(), startid]
    #                 return data
    #
    #             self.getModifySet("subscribers", category, modfunction,
    #                               subscriberid=subscriberid, db=self, startid=startid)
    #
    # def subscriptionGetNextItem(self, subscriberid, category, autoConfirm=True):
    #     """
    #     get next item from subscription
    #     returns
    #        False,None when no next
    #        True,the data when a next
    #     """
    #     if not self.exists("subscribers", category):
    #         raise j.exceptions.RuntimeError("cannot find subscription")
    #     data = self.get("subscribers", category)
    #     if subscriberid not in data:
    #         raise j.exceptions.RuntimeError("cannot find subscriber")
    #     lastaccesstime, lastid = data[subscriberid]
    #     lastid += 1
    #     if not self.exists(category, startid):
    #         return False, None
    #     else:
    #         return True, self.get(category, startid)
    #     if autoConfirm:
    #         self.subscriptionAdvance(subscriberid, category, lastid)
    #     return self.get(category, key)
    #
    # def subscriptionAdvance(self, subscriberid, category, lastProcessedId):
    #
    #     def modfunction(data, subscriberid, db, lastProcessedId):
    #         data[subscriberid] = [db.now(), lastProcessedId]
    #         return data
    #
    #     self.getModifySet("subscribers", category, modfunction, subscriberid=subscriberid,
    #                       db=self, lastProcessedId=lastProcessedId)
    #
    # def setDedupe(self, category, data):
    #     """
    #     will return unique key which references the data, if it exists or not
    #     """
    #     if data == "" or data == None:
    #         return ""
    #     if len(data) < 32:
    #         return data
    #     md5 = j.data.hash.md5_string(data)
    #     if not self.exists(category, md5):
    #         self.set(category, md5, data)
    #     return md5
    #
    # def getDedupe(self, category, key):
    #     if len(key) < 32:
    #         return key.encode()
    #     return self.get(category, key)


text:
class AddOmniAuthColumnsToUsers < ActiveRecord::Migration
  def change
    add_column :users, :provider, :string
  end
end

redpajama_set_name: RedPajamaGithub


text:
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using Inbox2.Core.Configuration;
using Inbox2.Framework.Extensions;
using Inbox2.Framework.Interop;

namespace Inbox2.Framework.VirtualMailBox
{
    public static class Inbox2Environment
    {

        /// <summary>
        /// Based on: http://andrewensley.com/2009/06/c-detect-windows-os-part-1/
        /// </summary>
        /// <returns></returns>
        public static string GetOperatingSystem()
        {
            //Get Operating system information.
            OperatingSystem os = System.Environment.OSVersion;
            //Get version information about the os.
            Version vs = os.Version;

            //Variable to hold our return value
            string operatingSystem = "";

            if (os.Platform == PlatformID.Win32Windows)
            {
                //This is a pre-NT version of Windows
                switch (vs.Minor)
                {
                    case 0:
                        operatingSystem = "95";
                        break;
                    case 10:
                        if (vs.Revision.ToString() == "2222A")
                            operatingSystem = "98SE";
                        else
                            operatingSystem = "98";
                        break;
                    case 90:
                        operatingSystem = "Me";
                        break;
                    default:
                        break;
                }
            }
            else if (os.Platform == PlatformID.Win32NT)
            {
                switch (vs.Major)
                {
                    case 3:
                        operatingSystem = "NT 3.51";
                        break;
                    case 4:
                        operatingSystem = "NT 4.0";
                        break;
                    case 5:
                        operatingSystem = vs.Minor == 0 ? "2000" : "XP";
                        break;
                    case 6:
                        operatingSystem = vs.Minor == 0 ? "Vista" : "7";
                        break;
                    default:
                        break;
                }
            }

            return operatingSystem;
        }
    }
}


text:
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Diagnostics.Contracts;
using System;

// Disable the "this variable is not used" warning as every field would imply it.
#pragma warning disable 0414
// Disable the "this variable is never assigned to".
#pragma warning disable 0067
// Disable the "this event is never assigned to".
#pragma warning disable 0649
// Disable the "this variable is never used".
#pragma warning disable 0169
// Disable the "new keyword not required" warning.
#pragma warning disable 0109
// Disable the "extern without DllImport" warning.
#pragma warning disable 0626
// Disable the "could hide other member" warning, can happen on certain properties.
#pragma warning disable 0108

namespace System.Windows
{
  public partial class TemplateBindingExtension : System.Windows.Markup.MarkupExtension
  {
    #region Methods and constructors
    public override Object ProvideValue(IServiceProvider serviceProvider)
    {
      return default(Object);
    }

    public TemplateBindingExtension()
    {
    }

    public TemplateBindingExtension(DependencyProperty property)
    {
    }
    #endregion

    #region Properties and indexers
    public System.Windows.Data.IValueConverter Converter
    {
      get
      {
        return default(System.Windows.Data.IValueConverter);
      }
      set
      {
      }
    }

    public Object ConverterParameter
    {
      get
      {
        return default(Object);
      }
      set
      {
      }
    }

    public DependencyProperty Property
    {
      get
      {
        return default(DependencyProperty);
      }
      set
      {
      }
    }
    #endregion
  }
}


text:
layout: docs
title: "Select"
section: "c3"
---
```tut:invisible
import com.datastax.driver.core.{Cluster, Session}
import com.weather.scalacass.ScalaSession

implicit val cluster = Cluster.builder.addContactPoint("localhost").build()
implicit val session: Session = cluster.connect()

val sSession: ScalaSession = ScalaSession("mykeyspace")
sSession.createKeyspace("replication = {'class':'SimpleStrategy', 'replication_factor' : 1}").execute()

case class MyTable(s: String, i: Int, l: Long)
val createStatement = sSession.createTable[MyTable]("mytable", 1, 0)
createStatement.execute()

val insertStatement = sSession.insert("mytable", MyTable("a str", 1234, 5678L))
insertStatement.execute()
```
# Select

Selects can retrieve the entire row, or only specific columns of the row. The query and the column specifier, if needed,
are both represented via case classes.

In addition, you can choose to retrieve only a single row from Cassandra, represented by an `Option[Row]` response,
where the Option will be `None` if no values were found that matched the query.

## Select Whole Row

For selects that will return the entire row:

```tut
case class Query(s: String)
```
##### Retrieve all rows matching the query
```tut
val selectStatement = sSession.selectStar("mytable", Query("a str"))
selectStatement.execute()
```
##### Retrieve a single row matching the query
```tut
val selectOneStatement = sSession.selectOneStar("mytable", Query("a str"))
selectOneStatement.execute()
```

## Select Columns

For selects that only retrieve certain columns of that row, specify the columns as a case class. However, you will not
actually use an instance of the case class in the statement, just pass it in as type parameter:

```tut
case class ColumnsToRetrieve(s: String, l: Long)
```
```tut
val selectColumnsStatement = sSession.select[ColumnsToRetrieve]("mytable", Query("a str"))
selectColumnsStatement.execute()

val selectColumnsOneStatement = sSession.selectOne[ColumnsToRetrieve]("mytable", Query("a str"))
selectColumnsOneStatement.execute()
```

## Allow Filtering

You can `ALLOW FILTERING` on the request (read more about ["allow filtering" here](https://www.datastax.com/dev/blog/allow-filtering-explained-2))

```tut
val selectAllowFiltering = selectStatement.allowFiltering
selectAllowFiltering.execute()

val selectOneAllowFiltering = selectOneStatement.allowFiltering
selectOneAllowFiltering.execute()
```

You can remove the allow filtering option:

```tut
val selectNoAllowFiltering = selectAllowFiltering.noAllowFiltering
selectNoAllowFiltering.execute()

val selectOneNoAllowFiltering = selectOneAllowFiltering.noAllowFiltering
selectOneNoAllowFiltering.execute()
```

## Limit

For queries that will return an iterator of responses (ie, not `selectOne` statements), you can impose a limit on the
number of responses:

```tut
val selectLimit = selectStatement.limit(100)
selectLimit.execute()
```

Finally, you can disable the imposed limit:

```tut
val selectNoLimit = selectLimit.noLimit
selectNoLimit.execute()
```

## Reading from the `Row`s

Scala-Cass provides a Scala-style method of extraction for `Row`, either into Scala values, or directly into case
classes.

* [Click here](/cass3/row-extraction.html) for a tutorial on how to extract values from `Row`
* [Click here](/cass3/type-mappings.html) for a mapping of Cassandra types to Scala types
```tut:invisible
sSession.dropKeyspace.execute()
sSession.close()
cluster.close()
```


text:
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;

public class HomeController : Controller
{
    // This is fine because of the global filter
    [HttpPost]
    public ActionResult Login()
    {
        return View();
    }

    // GOOD: Anti forgery token is validated
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UpdateDetails()
    {
        return View();
    }
}

public class AntiForgeryFilter : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        AntiForgery.Validate();
    }
}
public class UserApplication : HttpApplication
{
    public void Application_Start()
    {
        GlobalFilters.Filters.Add(new AntiForgeryFilter());
    }
}


text:
'use strict';
var removeNode = require('rocambole-node-remove');

module.exports = function (node) {
	if (node.type === 'DebuggerStatement') {
		removeNode(node);
	}
};


text:
import os
import shutil

###############################################################################
#       CONSTANTES / VARIABLES
###############################################################################

###############################################################################
#       CLASSES
###############################################################################

###############################################################################
#       FONCTIONS
###############################################################################

def creer_dossier_doublons(source, dossier):
	os.mkdir(source + "/" + dossier)

def copier_fichier(fi, source, dest):
		shutil.copy2(fi, str(source + "/" + dest))

def supprimer_fichier(fi, source):
	os.remove(source+"/"+fi)

def traiter_fichiers(liste, o, p):

	for i in range(len(liste.traitee)):

		if liste.doublons[i] != False :

			if o.supprimer_doublons  :
				if liste.doublons[i] == True :
					supprimer_fichier(liste[i], repertoire_source)

			else :
				if o.deplacer_orginal and o.deplacer_doublons :
						copier_fichier(fichier, repertoire_source, repertoire_destination)
						supprimer_fichier(fichier, repertoire_source)

				elif o.deplacer_doublons and not o.deplacer_original:
					if liste.doublons[i] == True :
						copier_fichier(fichier, repertoire_source, repertoire_destination)
						supprimer_fichier(fichier, repertoire_source)

				else:
					input("probleme")


text:
package org.wisdom.samples.interceptors;

import org.wisdom.api.annotations.Interception;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Just logged requests.
 */
@Interception
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Logged {

    boolean duration() default true;

}


text:
var expect = chai.expect;

describe("World", function() {
	beforeEach(function() {
		this.sandbox = sinon.sandbox.create();
		this.entity = sinon.createStubInstance(B);
		this.B = this.sandbox.stub(window, "B").returns(this.entity)
		this.world = new World();
	})

	afterEach(function() {
		this.sandbox.restore();
	})

	describe("initialize", function() {
		it("!? initializes entities & stores them in this.entities", function() {
			expect(this.world.entities[0]).to.eq(this.entity);
			expect(this.world.entities[1]).to.eq(this.entity);
		})
	})

	describe("#sense()", function() {
		it("returns all entities", function() {
			var response = this.world.sense(0);
			expect(response).to.include(this.entity)
			expect(response.length).to.equal(this.world.entities.length)
		})
	})
})


text:
using base::Time;
using base::TimeDelta;

namespace history {

class VisitSQLHandlerTest : public testing::Test {
 public:
  VisitSQLHandlerTest()
      : urls_sql_handler_(&history_db_),
        visit_sql_handler_(&history_db_) {
  }
  virtual ~VisitSQLHandlerTest() {}

 protected:
  virtual void SetUp() {
    // Get a temporary directory for the test DB files.
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    base::FilePath history_db_name = temp_dir_.path().AppendASCII(
        chrome::kHistoryFilename);
    ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name, NULL));
  }

  virtual void TearDown() {
  }

  HistoryDatabase history_db_;
  base::ScopedTempDir temp_dir_;
  UrlsSQLHandler urls_sql_handler_;
  VisitSQLHandler visit_sql_handler_;

 private:
  DISALLOW_COPY_AND_ASSIGN(VisitSQLHandlerTest);
};

// Insert a url with only url set to verify no visit was inserted in visit
// table.
TEST_F(VisitSQLHandlerTest, InsertURL) {
  HistoryAndBookmarkRow row;
  row.set_raw_url("http://google.com");
  row.set_url(GURL("http://google.com"));

  ASSERT_TRUE(urls_sql_handler_.Insert(&row));
  ASSERT_TRUE(visit_sql_handler_.Insert(&row));

  URLRow url_row;
  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));

  // Noting should be inserted to visit table.
  VisitVector visits;
  ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
  EXPECT_EQ(0u, visits.size());
}

// Insert a url with last visit time set to verify a visit was inserted.
TEST_F(VisitSQLHandlerTest, InsertURLWithLastVisitTime) {
  HistoryAndBookmarkRow row;
  row.set_raw_url("http://google.com");
  row.set_url(GURL("http://google.com"));
  row.set_last_visit_time(Time::Now());

  ASSERT_TRUE(urls_sql_handler_.Insert(&row));
  ASSERT_TRUE(visit_sql_handler_.Insert(&row));

  URLRow url_row;
  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));

  VisitVector visits;
  ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
  EXPECT_EQ(1u, visits.size());
  EXPECT_EQ(row.last_visit_time(), visits[0].visit_time);
}

// Insert a urls with created time to verify the a visit was inserted.
TEST_F(VisitSQLHandlerTest, InsertURLWithCreatedTime) {
  HistoryAndBookmarkRow row;
  row.set_raw_url("http://google.com");
  row.set_url(GURL("http://google.com"));
  row.set_title(UTF8ToUTF16("Google"));
  row.set_created(Time::Now());

  ASSERT_TRUE(urls_sql_handler_.Insert(&row));
  ASSERT_TRUE(visit_sql_handler_.Insert(&row));

  URLRow url_row;
  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));

  VisitVector visits;
  ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
  EXPECT_EQ(1u, visits.size());
  EXPECT_EQ(row.created(), visits[0].visit_time);
}

// Insert a URL with visit count as 1 to verify a visit was inserted.
TEST_F(VisitSQLHandlerTest, InsertURLWithVisitCount) {
  HistoryAndBookmarkRow row;
  row.set_raw_url("http://google.com");
  row.set_url(GURL("http://google.com"));
  row.set_visit_count(1);

  ASSERT_TRUE(urls_sql_handler_.Insert(&row));
  ASSERT_TRUE(visit_sql_handler_.Insert(&row));

  URLRow url_row;
  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));

  VisitVector visits;
  ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
  EXPECT_EQ(1u, visits.size());
  EXPECT_NE(Time(), visits[0].visit_time);
}

// Insert a URL with all values set to verify the visit rows
// were inserted correctly.
TEST_F(VisitSQLHandlerTest, Insert) {
  HistoryAndBookmarkRow row;
  row.set_raw_url("http://google.com");
  row.set_url(GURL("http://google.com"));
  row.set_visit_count(10);
  row.set_last_visit_time(Time::Now());
  row.set_created(Time::Now() - TimeDelta::FromDays(1));

  ASSERT_TRUE(urls_sql_handler_.Insert(&row));
  ASSERT_TRUE(visit_sql_handler_.Insert(&row));

  URLRow url_row;
  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));

  VisitVector visits;
  ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
  // 10 row were inserted.
  EXPECT_EQ(10u, visits.size());
  // The earlies one has created time set.
  EXPECT_EQ(row.created(), visits[0].visit_time);
  // The latest one has last visit time set.
  EXPECT_EQ(row.last_visit_time(), visits[9].visit_time);
}

// Test the case that both visit time and visit count updated.
TEST_F(VisitSQLHandlerTest, UpdateVisitTimeAndVisitCount) {
  HistoryAndBookmarkRow row;
  row.set_raw_url("http://google.com");
  row.set_url(GURL("http://google.com"));
  row.set_title(UTF8ToUTF16("Google"));
  row.set_visit_count(10);
  row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));

  ASSERT_TRUE(urls_sql_handler_.Insert(&row));
  ASSERT_TRUE(visit_sql_handler_.Insert(&row));

  URLRow url_row;
  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));

  HistoryAndBookmarkRow update_row;
  update_row.set_last_visit_time(Time::Now());
  update_row.set_visit_count(1);

  TableIDRow id;
  id.url_id = url_row.id();
  TableIDRows ids;
  ids.push_back(id);
  ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
  ASSERT_TRUE(visit_sql_handler_.Update(update_row, ids));

  VisitVector visits;
  ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
  EXPECT_EQ(1u, visits.size());
  EXPECT_EQ(update_row.last_visit_time(), visits[0].visit_time);
}

// Update visit count to zero to verify the visit rows of this url
// were removed.
TEST_F(VisitSQLHandlerTest, UpdateVisitCountZero) {
  HistoryAndBookmarkRow row;
  row.set_raw_url("http://google.com");
  row.set_url(GURL("http://google.com"));
  row.set_visit_count(10);
  row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));

  ASSERT_TRUE(urls_sql_handler_.Insert(&row));
  ASSERT_TRUE(visit_sql_handler_.Insert(&row));

  URLRow url_row;
  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));

  HistoryAndBookmarkRow update_row;
  update_row.set_visit_count(0);
  TableIDRow id;
  id.url_id = url_row.id();
  TableIDRows ids;
  ids.push_back(id);
  ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
  ASSERT_TRUE(visit_sql_handler_.Update(update_row, ids));

  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
  EXPECT_EQ(0, url_row.visit_count());
  // Last visit is reset.
  EXPECT_EQ(Time(), url_row.last_visit());
  VisitVector visits;
  ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
  EXPECT_EQ(0u, visits.size());
}

// Update both last visit time and created time to verify
// that visits row are updated correctly.
TEST_F(VisitSQLHandlerTest, UpdateBothTime) {
  HistoryAndBookmarkRow row;
  row.set_raw_url("http://google.com");
  row.set_url(GURL("http://google.com"));
  row.set_visit_count(10);
  row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));

  ASSERT_TRUE(urls_sql_handler_.Insert(&row));
  ASSERT_TRUE(visit_sql_handler_.Insert(&row));
  URLRow url_row;
  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));

  HistoryAndBookmarkRow update_row;
  update_row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(9));
  update_row.set_created(Time::Now() - TimeDelta::FromDays(10));
  TableIDRow id;
  id.url_id = url_row.id();
  TableIDRows ids;
  ids.push_back(id);
  ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
  ASSERT_TRUE(visit_sql_handler_.Update(update_row, ids));

  VisitVector visits;
  ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
  // Though both time are updated, visit count was increase by 1 because of
  // last visit time's change.
  EXPECT_EQ(11u, visits.size());
  EXPECT_EQ(update_row.created(), visits[0].visit_time);
  EXPECT_EQ(update_row.last_visit_time(), visits[10].visit_time);
}

// Update the visit count to verify the new visits are inserted.
TEST_F(VisitSQLHandlerTest, UpdateVisitCountIncreased) {
  HistoryAndBookmarkRow row;
  row.set_raw_url("http://google.com");
  row.set_url(GURL("http://google.com"));
  row.set_visit_count(10);
  row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));

  ASSERT_TRUE(urls_sql_handler_.Insert(&row));
  URLRow url_row;
  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
  EXPECT_EQ(row.url(), url_row.url());
  EXPECT_EQ(10, url_row.visit_count());
  EXPECT_EQ(row.last_visit_time(), url_row.last_visit());

  HistoryAndBookmarkRow update_row;
  update_row.set_visit_count(11);
  TableIDRow id;
  id.url_id = url_row.id();
  TableIDRows ids;
  ids.push_back(id);
  ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
  EXPECT_EQ(row.url(), url_row.url());
  EXPECT_EQ(11, url_row.visit_count());
  EXPECT_LT(row.last_visit_time(), url_row.last_visit());
}

TEST_F(VisitSQLHandlerTest, Delete) {
  HistoryAndBookmarkRow row;
  row.set_raw_url("http://google.com");
  row.set_url(GURL("http://google.com"));
  row.set_visit_count(10);
  row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));

  ASSERT_TRUE(urls_sql_handler_.Insert(&row));
  URLRow url_row;
  ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
  EXPECT_EQ(row.url(), url_row.url());
  EXPECT_EQ(10, url_row.visit_count());
  EXPECT_EQ(row.last_visit_time(), url_row.last_visit());

  TableIDRow id;
  id.url_id = url_row.id();
  TableIDRows ids;
  ids.push_back(id);
  ASSERT_TRUE(urls_sql_handler_.Delete(ids));
  EXPECT_FALSE(history_db_.GetURLRow(row.url_id(), &url_row));
}

}  // namespace history


text:
package org.iguanatool.search.evolve.mutation;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.iguanatool.search.randomnumbergenerator.NonRandomNumberGenerator;
import org.iguanatool.search.randomnumbergenerator.RandomNumberGenerator;
import org.iguanatool.search.solution.NumericalSolution;
import org.iguanatool.search.solution.NumericalSolutionType;
import org.junit.Assert;

/**
 * User: phil
 * Date: 01-Apr-2006
 * Time: 14:58:27
 */
public class MuhlenbeinMutationTest extends TestCase {

    NumericalSolutionType nvs;
    NumericalSolution ind;

    public void setUp() {
        double[] min = {0, 0, 0, 0, 0};
        double[] max = {5, 5, 5, 5, 5};
        int[] precision = {0, 0, 0, 0, 0};
        nvs = new NumericalSolutionType(5, min, max, precision);

        ind = new NumericalSolution(nvs);

        double[] genome1 = {1, 2, 3, 4, 5};
        ind.setElements(genome1);
    }

    public void testMuhlenbienMutation() {
        // mutation at last locus
        double[] doubles = {0.3, 0.3, 0.3, 0.3, 0.1,
                            0.5, 0.01, 0.01, 0.5, 0.5};
        boolean[] booleans = {true};
        RandomNumberGenerator r = new NonRandomNumberGenerator(booleans, doubles);
        MuhlenbeinMutation mm = new MuhlenbeinMutation(r, 0.1, 4);

        // 2^-1 = 0.5
        // 2^-2 = 0.25
        // 0.1 * 5 = 0.5
        // delta = -0.375
        // gene 5 = 4.625
        mm.mutate(ind);
        Assert.assertEquals("Ind gene locus 4",
                            4.625, ind.getRawElement(4), 0.001);
    }

    public static Test suite() {
        return new TestSuite(MuhlenbeinMutationTest.class);
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
}


text:
CFLAGS=-O2 -Wall -Wextra -pedantic -flto -I../../include -I../../deps/googletest/googletest/include -pthread --std=c++14
LFLAGS=-flto -lpthread -L../../deps/googletest/googletest -lgtest -lgtest_main -Wl,-rpath ../../ -L../../ -lMyEngine

FILES=$(wildcard *.cc)
HEADERS=$(wildcard ../../include/*.hpp)

test: $(FILES:.cc=.o) gtest
	$(CXX) $(FILES:.cc=.o) $(LFLAGS) -o test

check: test
	./test

%.o: %.cc $(HEADERS)
	$(CXX) $(CFLAGS) -c $< -o $@

clean:
	$(RM) *.o test

default: check gtest

../../deps/googletest/googletest/Makefile: ../../deps/googletest/CMakeLists.txt
	cd ../../deps/googletest/googletest; cmake .

../../deps/googletest:
	git submodule init ../../deps/googletest && git submodule update ../../deps/googletest

gtest: ../../deps/googletest/googletest ../../deps/googletest/googletest/Makefile
	$(MAKE) -C ../../deps/googletest/googletest


text:
- `node['oc-graphite']['carbon_cache']['enable']`: Enable or disable the carbon-cache service (default `true`)
- `node['oc-graphite']['carbon']['data_dir']`: The data directory for whisper files (default `/var/lib/graphite/whisper/`)
- `node['oc-graphite']['carbon']['user']`: The user for the carbon service (default `_graphite`)
- `node['oc-graphite']['web']['secret_key']`: The secret key for the Django app: (default `0aed5c39507562f4519c2d47515e8221`)
- `node['oc-graphite']['web']['time_zone']`: The time zone used for the Django app (default `America/Los_Angeles`)
- `node['oc-graphite']['web']['server']`: Which web server to use, `nginx` and `uwsgi` are currently supported (default `uwsgi`)
- `node['oc-graphite']['web']['seed_password']`: A seed password for graphite_web's admin user, this will not change after initialization (default `changeme`)
- `node['oc-graphite']['uwsgi']['listen_ip']`: The IP to listen on (default `0.0.0.0`)
- `node['oc-graphite']['uwsgi']['listen_port']`: The port to listen on (default `8080`)
- `node['oc-graphite']['nginx']['disable_node_vhost']`: Set to `true` to disable the default vhost dropped in place for nginx (default `false`)
- `node['oc-graphite']['nginx']['hostname']`: The hostname used by the nginx vhost, if left as nil the FQDN is used (default `nil`)

## Recipes

### default

The default recipe doesn't do anything because graphite doesn't really have a client.

### graphite_carbon

Sets up the carbon service (carbon-cache, carbon-relay and carbon-aggregator).  This recipe also manages the configs for storage schemas/aggregations.

### graphite_web

Sets up the graphite web interface as well as nginx/uwsgi.  This will include the `_uwsgi` recipe and will include the `_nginx` recipe if `node['oc-graphite']['web']['server']` is set to `nginx`.

### _uwsgi

Installs the `uwsgi` service and sets up/enables the graphite vhost.

### _nginx

Installs the `nginx` service and sets up/enables the graphite vhost.

## Usage

An example role:

```
{
  "name": "graphite",
  "description": "Graphite Server",
  "default_attributes": {
    "oc-graphite": {
      "web": {
        "secret_key": "This key is super secret!",
        "server": "nginx"
      },
      "nginx": {
        "disable_default_vhost": true,
        "hostname": "graphite.example.com"
      }
    }
  },
  "run_list": [
    "recipe[oc-graphite::graphite_carbon]",
    "recipe[oc-graphite::graphite_web]"
  ]
}
```
## Testing

The cookbook provides the following Rake tasks for testing:

    rake foodcritic                   # Lint Chef cookbooks
    rake integration                  # Alias for kitchen:all
    rake kitchen:all                  # Run all test instances
    rake kitchen:node-ubuntu-1204     # Run default-ubuntu-1204 test instance
    rake rubocop                      # Run RuboCop style and lint checks
    rake spec                         # Run ChefSpec examples
    rake test                         # Run all tests

## License and Author

- Author: Chef Operations
- Copyright (C) 2014, Chef Software, Inc

All rights reserved.


text:
<?php

namespace Film\Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Film\Bundle\Form\CategoryForm;
use Film\Bundle\Entity\Category;

class CategoryController extends Controller
{
    /**
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function categoriesAction()
    {
        $em = $this->getDoctrine()->getManager();
        $categories = $em->getRepository('Film\Bundle\Entity\Category')->findAll();

        return $this->render('FilmBundle:Category:categories.html.twig', array(
            'categories' => $categories,
        ));
    }

    /**
     * Create category form, submit form, create category
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function createAction()
    {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        $category = new Category();
        $form = $this->createForm(new CategoryForm(), $category, array(
            'action' => $this->generateUrl('category_create'),
            'method' => 'POST',
        ));
        if ('POST' == $request->getMethod()) {
            $form->handleRequest($request);
            if ($form->isValid()) {
                $em->persist($category);
                $em->flush();
                $this->get('session')->getFlashBag()->add(
                    'notice',
                    'Your category was saved!'
                );

                return $this->redirect($this->generateUrl('category_homepage'));
            }
        }

        return $this->render('FilmBundle:Category:form.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}


text:
from __future__ import unicode_literals
import frappe
from six.moves import html_parser as HTMLParser
import smtplib, quopri, json
from frappe import msgprint, throw, _, safe_decode
from frappe.email.smtp import SMTPServer, get_outgoing_email_account
from frappe.email.email_body import get_email, get_formatted_html, add_attachment
from frappe.utils.verified_command import get_signed_params, verify_request
from html2text import html2text
from frappe.utils import get_url, nowdate, encode, now_datetime, add_days, split_emails, cstr, cint
from frappe.utils.file_manager import get_file
from rq.timeouts import JobTimeoutException
from frappe.utils.scheduler import log
from six import text_type, string_types

class EmailLimitCrossedError(frappe.ValidationError): pass

def send(recipients=None, sender=None, subject=None, message=None, text_content=None, reference_doctype=None,
		reference_name=None, unsubscribe_method=None, unsubscribe_params=None, unsubscribe_message=None,
		attachments=None, reply_to=None, cc=[], bcc=[], message_id=None, in_reply_to=None, send_after=None,
		expose_recipients=None, send_priority=1, communication=None, now=False, read_receipt=None,
		queue_separately=False, is_notification=False, add_unsubscribe_link=1, inline_images=None,
		header=None, print_letterhead=False):
	"""Add email to sending queue (Email Queue)

	:param recipients: List of recipients.
	:param sender: Email sender.
	:param subject: Email subject.
	:param message: Email message.
	:param text_content: Text version of email message.
	:param reference_doctype: Reference DocType of caller document.
	:param reference_name: Reference name of caller document.
	:param send_priority: Priority for Email Queue, default 1.
	:param unsubscribe_method: URL method for unsubscribe. Default is `/api/method/frappe.email.queue.unsubscribe`.
	:param unsubscribe_params: additional params for unsubscribed links. default are name, doctype, email
	:param attachments: Attachments to be sent.
	:param reply_to: Reply to be captured here (default inbox)
	:param in_reply_to: Used to send the Message-Id of a received email back as In-Reply-To.
	:param send_after: Send this email after the given datetime. If value is in integer, then `send_after` will be the automatically set to no of days from current date.
	:param communication: Communication link to be set in Email Queue record
	:param now: Send immediately (don't send in the background)
	:param queue_separately: Queue each email separately
	:param is_notification: Marks email as notification so will not trigger notifications from system
	:param add_unsubscribe_link: Send unsubscribe link in the footer of the Email, default 1.
	:param inline_images: List of inline images as {"filename", "filecontent"}. All src properties will be replaced with random Content-Id
	:param header: Append header in email (boolean)
	"""
	if not unsubscribe_method:
		unsubscribe_method = "/api/method/frappe.email.queue.unsubscribe"

	if not recipients and not cc:
		return

	if isinstance(recipients, string_types):
		recipients = split_emails(recipients)

	if isinstance(cc, string_types):
		cc = split_emails(cc)

	if isinstance(bcc, string_types):
		bcc = split_emails(bcc)

	if isinstance(send_after, int):
		send_after = add_days(nowdate(), send_after)

	email_account = get_outgoing_email_account(True, append_to=reference_doctype, sender=sender)
	if not sender or sender == "Administrator":
		sender = email_account.default_sender

	check_email_limit(recipients)

	if not text_content:
		try:
			text_content = html2text(message)
		except HTMLParser.HTMLParseError:
			text_content = "See html attachment"

	if reference_doctype and reference_name:
		unsubscribed = [d.email for d in frappe.db.get_all("Email Unsubscribe", "email",
			{"reference_doctype": reference_doctype, "reference_name": reference_name})]

		unsubscribed += [d.email for d in frappe.db.get_all("Email Unsubscribe", "email",
			{"global_unsubscribe": 1})]
	else:
		unsubscribed = []

	recipients = [r for r in list(set(recipients)) if r and r not in unsubscribed]

	if cc:
		cc = [r for r in list(set(cc)) if r and r not in unsubscribed]

	if not recipients and not cc:
		# Recipients may have been unsubscribed, exit quietly
		return

	email_text_context = text_content

	should_append_unsubscribe = (add_unsubscribe_link
		and reference_doctype
		and (unsubscribe_message or reference_doctype=="Newsletter")
		and add_unsubscribe_link==1)

	unsubscribe_link = None
	if should_append_unsubscribe:
		unsubscribe_link = get_unsubscribe_message(unsubscribe_message, expose_recipients)
		email_text_context += unsubscribe_link.text

	email_content = get_formatted_html(subject, message,
		email_account=email_account, header=header,
		unsubscribe_link=unsubscribe_link)

	# add to queue
	add(recipients, sender, subject,
		formatted=email_content,
		text_content=email_text_context,
		reference_doctype=reference_doctype,
		reference_name=reference_name,
		attachments=attachments,
		reply_to=reply_to,
		cc=cc,
		bcc=bcc,
		message_id=message_id,
		in_reply_to=in_reply_to,
		send_after=send_after,
		send_priority=send_priority,
		email_account=email_account,
		communication=communication,
		add_unsubscribe_link=add_unsubscribe_link,
		unsubscribe_method=unsubscribe_method,
		unsubscribe_params=unsubscribe_params,
		expose_recipients=expose_recipients,
		read_receipt=read_receipt,
		queue_separately=queue_separately,
		is_notification = is_notification,
		inline_images = inline_images,
		header=header,
		now=now,
		print_letterhead=print_letterhead)

def add(recipients, sender, subject, **kwargs):
	"""Add to Email Queue"""
	if kwargs.get('queue_separately') or len(recipients) > 20:
		email_queue = None
		for r in recipients:
			if not email_queue:
				email_queue = get_email_queue([r], sender, subject, **kwargs)
				if kwargs.get('now'):
					email_queue(email_queue.name, now=True)
			else:
				duplicate = email_queue.get_duplicate([r])
				duplicate.insert(ignore_permissions=True)

				if kwargs.get('now'):
					send_one(duplicate.name, now=True)

			frappe.db.commit()
	else:
		email_queue = get_email_queue(recipients, sender, subject, **kwargs)
		if kwargs.get('now'):
			send_one(email_queue.name, now=True)

def get_email_queue(recipients, sender, subject, **kwargs):
	'''Make Email Queue object'''
	e = frappe.new_doc('Email Queue')
	e.priority = kwargs.get('send_priority')
	attachments = kwargs.get('attachments')
	if attachments:
		# store attachments with fid or print format details, to be attached on-demand later
		_attachments = []
		for att in attachments:
			if att.get('fid'):
				_attachments.append(att)
			elif att.get("print_format_attachment") == 1:
				att['lang'] = frappe.local.lang
				att['print_letterhead'] = kwargs.get('print_letterhead')
				_attachments.append(att)
		e.attachments = json.dumps(_attachments)

	try:
		mail = get_email(recipients,
			sender=sender,
			subject=subject,
			formatted=kwargs.get('formatted'),
			text_content=kwargs.get('text_content'),
			attachments=kwargs.get('attachments'),
			reply_to=kwargs.get('reply_to'),
			cc=kwargs.get('cc'),
			bcc=kwargs.get('bcc'),
			email_account=kwargs.get('email_account'),
			expose_recipients=kwargs.get('expose_recipients'),
			inline_images=kwargs.get('inline_images'),
			header=kwargs.get('header'))

		mail.set_message_id(kwargs.get('message_id'),kwargs.get('is_notification'))
		if kwargs.get('read_receipt'):
			mail.msg_root["Disposition-Notification-To"] = sender
		if kwargs.get('in_reply_to'):
			mail.set_in_reply_to(kwargs.get('in_reply_to'))

		e.message_id = mail.msg_root["Message-Id"].strip(" <>")
		e.message = cstr(mail.as_string())
		e.sender = mail.sender

	except frappe.InvalidEmailAddressError:
		# bad Email Address - don't add to queue
		frappe.log_error('Invalid Email ID Sender: {0}, Recipients: {1}'.format(mail.sender,
			', '.join(mail.recipients)), 'Email Not Sent')

	recipients = list(set(recipients + kwargs.get('cc', []) + kwargs.get('bcc', [])))
	e.set_recipients(recipients)
	e.reference_doctype = kwargs.get('reference_doctype')
	e.reference_name = kwargs.get('reference_name')
	e.add_unsubscribe_link = kwargs.get("add_unsubscribe_link")
	e.unsubscribe_method = kwargs.get('unsubscribe_method')
	e.unsubscribe_params = kwargs.get('unsubscribe_params')
	e.expose_recipients = kwargs.get('expose_recipients')
	e.communication = kwargs.get('communication')
	e.send_after = kwargs.get('send_after')
	e.show_as_cc = ",".join(kwargs.get('cc', []))
	e.show_as_bcc = ",".join(kwargs.get('bcc', []))
	e.insert(ignore_permissions=True)

	return e

def check_email_limit(recipients):
	# if using settings from site_config.json, check email limit
	# No limit for own email settings
	smtp_server = SMTPServer()

	if (smtp_server.email_account
		and getattr(smtp_server.email_account, "from_site_config", False)
		or frappe.flags.in_test):

		monthly_email_limit = frappe.conf.get('limits', {}).get('emails')
		daily_email_limit = cint(frappe.conf.get('limits', {}).get('daily_emails'))

		if frappe.flags.in_test:
			monthly_email_limit = 500
			daily_email_limit = 50

		if daily_email_limit:
			# get count of sent mails in last 24 hours
			today = get_emails_sent_today()
			if (today + len(recipients)) > daily_email_limit:
				throw(_("Cannot send this email. You have crossed the sending limit of {0} emails for this day.").format(daily_email_limit),
					EmailLimitCrossedError)

		if not monthly_email_limit:
			return

		# get count of mails sent this month
		this_month = get_emails_sent_this_month()

		if (this_month + len(recipients)) > monthly_email_limit:
			throw(_("Cannot send this email. You have crossed the sending limit of {0} emails for this month.").format(monthly_email_limit),
				EmailLimitCrossedError)

def get_emails_sent_this_month():
	return frappe.db.sql("""select count(name) from `tabEmail Queue` where
		status='Sent' and MONTH(creation)=MONTH(CURDATE())""")[0][0]

def get_emails_sent_today():
	return frappe.db.sql("""select count(name) from `tabEmail Queue` where
		status='Sent' and creation>DATE_SUB(NOW(), INTERVAL 24 HOUR)""")[0][0]

def get_unsubscribe_message(unsubscribe_message, expose_recipients):
	if unsubscribe_message:
		unsubscribe_html = '''{0}'''.format(unsubscribe_message)
	else:
		unsubscribe_link = '''{0}'''.format(_('Unsubscribe'))
		unsubscribe_html = _("{0} to stop receiving emails of this type").format(unsubscribe_link)

	html = """

				{0}

""".format(unsubscribe_html)

	if expose_recipients == "footer":
		text = "\n"
	else:
		text = ""
	text += "\n\n{unsubscribe_message}: \n".format(unsubscribe_message=unsubscribe_message)

	return frappe._dict({
		"html": html,
		"text": text
	})

def get_unsubcribed_url(reference_doctype, reference_name, email, unsubscribe_method, unsubscribe_params):
	params = {"email": email.encode("utf-8"),
		"doctype": reference_doctype.encode("utf-8"),
		"name": reference_name.encode("utf-8")}
	if unsubscribe_params:
		params.update(unsubscribe_params)

	query_string = get_signed_params(params)

	# for test
	frappe.local.flags.signed_query_string = query_string

	return get_url(unsubscribe_method + "?" + get_signed_params(params))

@frappe.whitelist(allow_guest=True)
def unsubscribe(doctype, name, email):
	# unsubsribe from comments and communications
	if not verify_request():
		return

	try:
		frappe.get_doc({
			"doctype": "Email Unsubscribe",
			"email": email,
			"reference_doctype": doctype,
			"reference_name": name
		}).insert(ignore_permissions=True)

	except frappe.DuplicateEntryError:
		frappe.db.rollback()

	else:
		frappe.db.commit()

	return_unsubscribed_page(email, doctype, name)

def return_unsubscribed_page(email, doctype, name):
	frappe.respond_as_web_page(_("Unsubscribed"),
		_("{0} has left the conversation in {1} {2}").format(email, _(doctype), name),
		indicator_color='green')

def flush(from_test=False):
	"""flush email queue, every time: called from scheduler"""
	# additional check
	check_email_limit([])

	auto_commit = not from_test
	if frappe.are_emails_muted():
		msgprint(_("Emails are muted"))
		from_test = True

	smtpserver_dict = frappe._dict()

	for email in get_queue():

		if cint(frappe.defaults.get_defaults().get("hold_queue"))==1:
			break

		if email.name:
			smtpserver = smtpserver_dict.get(email.sender)
			if not smtpserver:
				smtpserver = SMTPServer()
				smtpserver_dict[email.sender] = smtpserver

			send_one(email.name, smtpserver, auto_commit, from_test=from_test)

		# NOTE: removing commit here because we pass auto_commit
		# finally:
		# 	frappe.db.commit()
def get_queue():
	return frappe.db.sql('''select
			name, sender
		from
			`tabEmail Queue`
		where
			(status='Not Sent' or status='Partially Sent') and
			(send_after is null or send_after < %(now)s)
		order
			by priority desc, creation asc
		limit 500''', { 'now': now_datetime() }, as_dict=True)

def send_one(email, smtpserver=None, auto_commit=True, now=False, from_test=False):
	'''Send Email Queue with given smtpserver'''

	email = frappe.db.sql('''select
			name, status, communication, message, sender, reference_doctype,
			reference_name, unsubscribe_param, unsubscribe_method, expose_recipients,
			show_as_cc, add_unsubscribe_link, attachments
		from
			`tabEmail Queue`
		where
			name=%s
		for update''', email, as_dict=True)[0]

	recipients_list = frappe.db.sql('''select name, recipient, status from
		`tabEmail Queue Recipient` where parent=%s''',email.name,as_dict=1)

	if frappe.are_emails_muted():
		frappe.msgprint(_("Emails are muted"))
		return

	if cint(frappe.defaults.get_defaults().get("hold_queue"))==1 :
		return

	if email.status not in ('Not Sent','Partially Sent') :
		# rollback to release lock and return
		frappe.db.rollback()
		return

	frappe.db.sql("""update `tabEmail Queue` set status='Sending', modified=%s where name=%s""",
		(now_datetime(), email.name), auto_commit=auto_commit)

	if email.communication:
		frappe.get_doc('Communication', email.communication).set_delivery_status(commit=auto_commit)

	try:
		if not frappe.flags.in_test:
			if not smtpserver: smtpserver = SMTPServer()
			smtpserver.setup_email_account(email.reference_doctype, sender=email.sender)

		for recipient in recipients_list:
			if recipient.status != "Not Sent":
				continue

			message = prepare_message(email, recipient.recipient, recipients_list)
			if not frappe.flags.in_test:
				smtpserver.sess.sendmail(email.sender, recipient.recipient, encode(message))

			recipient.status = "Sent"
			frappe.db.sql("""update `tabEmail Queue Recipient` set status='Sent', modified=%s where name=%s""",
				(now_datetime(), recipient.name), auto_commit=auto_commit)

		#if all are sent set status
		if any("Sent" == s.status for s in recipients_list):
			frappe.db.sql("""update `tabEmail Queue` set status='Sent', modified=%s where name=%s""",
				(now_datetime(), email.name), auto_commit=auto_commit)
		else:
			frappe.db.sql("""update `tabEmail Queue` set status='Error', error=%s
				where name=%s""", ("No recipients to send to", email.name), auto_commit=auto_commit)
		if frappe.flags.in_test:
			frappe.flags.sent_mail = message
			return
		if email.communication:
			frappe.get_doc('Communication', email.communication).set_delivery_status(commit=auto_commit)

	except (smtplib.SMTPServerDisconnected,
			smtplib.SMTPConnectError,
			smtplib.SMTPHeloError,
			smtplib.SMTPAuthenticationError,
			JobTimeoutException):

		# bad connection/timeout, retry later

		if any("Sent" == s.status for s in recipients_list):
			frappe.db.sql("""update `tabEmail Queue` set status='Partially Sent', modified=%s where name=%s""",
				(now_datetime(), email.name), auto_commit=auto_commit)
		else:
			frappe.db.sql("""update `tabEmail Queue` set status='Not Sent', modified=%s where name=%s""",
				(now_datetime(), email.name), auto_commit=auto_commit)

		if email.communication:
			frappe.get_doc('Communication', email.communication).set_delivery_status(commit=auto_commit)

		# no need to attempt further
		return

	except Exception as e:
		frappe.db.rollback()

		if any("Sent" == s.status for s in recipients_list):
			frappe.db.sql("""update `tabEmail Queue` set status='Partially Errored', error=%s where name=%s""",
				(text_type(e), email.name), auto_commit=auto_commit)
		else:
			frappe.db.sql("""update `tabEmail Queue` set status='Error', error=%s
where name=%s""", (text_type(e), email.name), auto_commit=auto_commit)

		if email.communication:
			frappe.get_doc('Communication', email.communication).set_delivery_status(commit=auto_commit)

		if now:
			print(frappe.get_traceback())
			raise e

		else:
			# log to Error Log
			log('frappe.email.queue.flush', text_type(e))

def prepare_message(email, recipient, recipients_list):
	message = email.message
	if not message:
		return ""

	# Parse "Email Account" from "Email Sender"
	email_account = get_outgoing_email_account(raise_exception_not_set=False, sender=email.sender)
	if frappe.conf.use_ssl and email_account.track_email_status:
		# Using SSL => Publically available domain => Email Read Reciept Possible
		message = message.replace("", quopri.encodestring(''.format(frappe.local.site, email.communication).encode()).decode())
	else:
		# No SSL => No Email Read Reciept
		message = message.replace("", quopri.encodestring("".encode()).decode())

	if email.add_unsubscribe_link and email.reference_doctype: # is missing the check for unsubscribe message but will not add as there will be no unsubscribe url
		unsubscribe_url = get_unsubcribed_url(email.reference_doctype, email.reference_name, recipient,
		email.unsubscribe_method, email.unsubscribe_params)
		message = message.replace("", quopri.encodestring(unsubscribe_url.encode()).decode())

	if email.expose_recipients == "header":
		pass
	else:
		if email.expose_recipients == "footer":
			if isinstance(email.show_as_cc, string_types):
				email.show_as_cc = email.show_as_cc.split(",")
			email_sent_to = [r.recipient for r in recipients_list]
			email_sent_cc = ", ".join([e for e in email_sent_to if e in email.show_as_cc])
			email_sent_to = ", ".join([e for e in email_sent_to if e not in email.show_as_cc])

			if email_sent_cc:
				email_sent_message = _("This email was sent to {0} and copied to {1}").format(email_sent_to,email_sent_cc)
			else:
				email_sent_message = _("This email was sent to {0}").format(email_sent_to)
			message = message.replace("", quopri.encodestring(email_sent_message.encode()).decode())

		message = message.replace("", recipient)

	message = (message and message.encode('utf8')) or ''
	message = safe_decode(message)
	if not email.attachments:
		return message

	# On-demand attachments
	from email.parser import Parser

	msg_obj = Parser().parsestr(message)
	attachments = json.loads(email.attachments)

	for attachment in attachments:
		if attachment.get('fcontent'): continue

		fid = attachment.get("fid")
		if fid:
			fname, fcontent = get_file(fid)
			attachment.update({
				'fname': fname,
				'fcontent': fcontent,
				'parent': msg_obj
			})
			attachment.pop("fid", None)
			add_attachment(**attachment)

		elif attachment.get("print_format_attachment") == 1:
			attachment.pop("print_format_attachment", None)
			print_format_file = frappe.attach_print(**attachment)
			print_format_file.update({"parent": msg_obj})
			add_attachment(**print_format_file)

	return msg_obj.as_string()

def clear_outbox():
	"""Remove low priority older than 31 days in Outbox and expire mails not sent for 7 days.
	Called daily via scheduler.
	Note: Used separate query to avoid deadlock
	"""

	email_queues = frappe.db.sql_list("""select name from `tabEmail Queue`
		where priority=0 and datediff(now(), modified) > 31""")

	if email_queues:
		frappe.db.sql("""delete from `tabEmail Queue` where name in (%s)"""
			% ','.join(['%s']*len(email_queues)), tuple(email_queues))

		frappe.db.sql("""delete from `tabEmail Queue Recipient` where parent in (%s)"""
			% ','.join(['%s']*len(email_queues)), tuple(email_queues))

	frappe.db.sql("""
		update `tabEmail Queue`
		set status='Expired'
		where datediff(curdate(), modified) > 7 and status='Not Sent' and (send_after is null or send_after < %(now)s)""", { 'now': now_datetime() })


text:
import { GridConfig } from './grid/gridConfig';
import { GridColumnFactory } from './grid/gridColumnFactory';
import { ipcRenderer, remote } from 'electron';
import { MenuBar } from './menuBar';
import { Callback, CallbackType } from './event';
import { FileModel } from '../file/fileModel';
import { ExtractWrapper, GridRowValues } from '../helper/wrapper';
import { Grid } from './grid/grid';
import { ZipController } from '../7z/zipController';

export class Container {
  private readonly _domNode: HTMLElement;
  private _menuBar: MenuBar;
  private _zipController: ZipController;
  private _grid: Grid;
  // We need a container for the grid in order to center it.
  private _gridContainer: HTMLDivElement;
  private _gridConfig: GridConfig;
  private _archivePath: string = '';
  private _lastDestDir: string = '';

  private first: boolean = true;

  constructor() {
    this._domNode = document.createElement('div');
    this._domNode.className = 'container'
    this._menuBar = new MenuBar();
    this._menuBar.registerCallback(new Callback(CallbackType.EXTRACT, this.btnExtractCallback.bind(this)));
    this._domNode.appendChild(this._menuBar.getDomNode());
    this._domNode.addEventListener('click', this.containerClick)
    this._zipController = new ZipController();
    ipcRenderer.on('archive-path', this.populateGrid.bind(this));
    this.enableDisableButtons();
  }

  private containerClick(e: MouseEvent) {
    const grid = document.getElementById('grid');
    if (grid !== null) {
      grid.focus();
    }
  }

  private enableDisableButtons() {
    if (this._archivePath === '') {
      this._menuBar.disableBtnExtract();
    } else {
      this._menuBar.enableBtnExtract();
    }
  }

  private selectDestinationCallback(destDir: Array<string>) {
    if (destDir === void 0) {
      return;
    }
    this._lastDestDir = destDir[0];
    const selectedFiles = this._grid.getSelectedFilenames();
    console.log(selectedFiles);
    const extractWrapper = new ExtractWrapper(this._archivePath, destDir[0], selectedFiles);
    this._zipController.extractFiles(extractWrapper);
  }

  private btnExtractCallback() {
    let dir;
    if (this._lastDestDir === '') {
      dir = require('os').homedir();
    } else {
      dir = this._lastDestDir;
    }
    remote.dialog.showOpenDialog({
      title: 'Open archive',
      defaultPath: dir + '',
      filters: [
        { name: 'All Files', extensions: ['*'] }
      ],
      properties: ['openDirectory']
    }, this.selectDestinationCallback.bind(this));
  }

  public populateGrid(event, archivePath) {
    if (this._gridContainer) {
      this._domNode.removeChild(this._gridContainer);
    }
    this._gridConfig = new GridConfig();
    this._gridConfig.addColumn(new GridColumnFactory().setTitle('Name').setWidth(120).setSortable(true).build());
    this._gridConfig.addColumn(new GridColumnFactory().setTitle('Size').setWidth(20).setSortable(true).build());
    this._gridConfig.addColumn(new GridColumnFactory().setTitle('Compressed Size').setWidth(200).setSortable(false).build());
    this._grid = new Grid(this._gridConfig);
    this._grid.archivePath = archivePath;
    this._archivePath = archivePath;

    this._gridContainer = document.createElement('div');
    this._gridContainer.classList.add('grid-container');
    this._gridContainer.appendChild(this._grid.getDomNode());
    this._domNode.appendChild(this._gridContainer);
    this.enableDisableButtons();
  }

  public getDomNode(): HTMLElement {
    return this._domNode;
  }
}


text:
title: Counting Sundays
localeTitle: Подсчет воскресений
---
## Задача 19: подсчет воскресений

Это заглушка. [Помогите нашему сообществу расширить его](https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/project-euler/problem-19-counting-sundays/index.md) .

[Это руководство по быстрому стилю поможет вам принять ваш запрос на тягу](https://github.com/freecodecamp/guides/blob/master/README.md) .


text:
import math

def time_series_plot(data_sets, start_date=None, end_date=None, **kwargs):
    data = []
    legend = []

    if type(data_sets) != list:
        data_sets = [data_sets]

    for data_set in data_sets:
        ts = data_set.data if start_date is None else data_set.data[start_date:]
        ts = ts if end_date is None else ts[:end_date]
        data.append(build_data_object(ts))
        legend.append(data_set.legend)

    title = data_sets[0].name if len(data_sets) == 1 else ''

    return {'title': title, 'x_accessor': 'date', 'y_accessor': 'value',
            'data': data, 'legend': legend, **kwargs}

def add_rolling_mean(data_set, viz, window, start_date=None, end_date=None):
    ts = data_set.data if start_date is None else data_set.data[start_date:]
    ts = ts if end_date is None else ts[:end_date]

    rolling_mean_ts = ts.rolling(window=window).mean().dropna()
    data = build_data_object(rolling_mean_ts)
    viz['data'].append(data)
    viz['legend'].append('mean')

def add_rolling_std(data_set, viz, window, start_date=None, end_date=None):
    ts = data_set.data if start_date is None else data_set.data[start_date:]
    ts = ts if end_date is None else ts[:end_date]

    rolling_std_ts = ts.rolling(window=window).std().dropna()
    data = build_data_object(rolling_std_ts)
    viz['data'].append(data)
    viz['legend'].append('std')

def auto_correlation_plot(data_set, max_lag, **kwargs):
    z95 = 1.9599639845400540 # z-score for 0.95 of N(0,1)
    z99 = 2.5758293035489004 # z-score for 0.99 of N(0,1)
    nsamples = len(data_set.data)

    data = []
    for lag in range(1, max_lag):
        data.append({
            'lag': lag,
            'autocorrelation': data_set.data.autocorr(lag),
            'plus95conf': z95 / math.sqrt(nsamples),
            'plus99conf': z99 / math.sqrt(nsamples),
            'minus95conf': -z95 / math.sqrt(nsamples),
            'minus99conf': -z99 / math.sqrt(nsamples),
        })

    return {'title': 'ACF', 'data': data, 'x_accessor': 'lag',
            'y_accessor': ['autocorrelation', 'plus95conf', 'plus99conf',
            'minus95conf', 'minus99conf'], 'legend': ['autocorrelation',
            '+95% confidence', '+99% confidence', '-95% confidence',
            '-99% confidence'], 'colors': ['#DB4437', '#5C5C5C', '#3A3A3A',
            '#5C5C5C', '#3A3A3A'], 'x_label': 'Lag', 'y_label':
            'Autocorrelation', 'baselines': [{'value': 0.0}], **kwargs}

def forecasting_eval_plot(forecast, **kwargs):
    training_data = build_data_object(forecast.training_data)

    forecast_data = []
    for index, row in forecast.forcasted_data.iterrows():
        forecast_data.append({
            'date': index.to_pydatetime().strftime('%s'),
            'value': row['value'],
            'lower': row['lower bound'],
            'upper': row['upper bound']
        })

    viz = {'title': forecast.name, 'data': [training_data, forecast_data],
           'colors': ['#4040E8', '#DB4437'],
           'legend': ['training data', 'forecast'],
           'show_confidence_band': ['lower', 'upper']}

    if forecast.test_data is not None:
        test_data = build_data_object(forecast.test_data)
        viz['data'].append(test_data)
        viz['colors'].append('#05B378')
        viz['legend'].append('test data')

    if forecast.validation_split:
        viz['markers'] = [{'date': forecast.validation_split,
                           'label': 'validation split'}]

    return {**viz, **kwargs}

def build_data_object(time_series):
    data = []
    for index, value in time_series.iteritems():
        data.append({
            'date': index.to_pydatetime().strftime('%s'),
            'value': value
        })
    return data


text:
package com.android.cim.serv.view;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;

import com.android.cim.Constants.Config;
import com.android.cim.serv.TempCacheFilter;
import com.android.cim.serv.entity.GzipByteArrayEntity;
import com.android.cim.serv.entity.GzipFileEntity;
import com.android.cim.serv.support.GzipUtil;

import android.util.Log;

/**
 * 模板文件视图渲染
 * @author join
 */
public class TempView extends BaseView<String, Map<String, Object>> {

    static final String TAG = "TempView";
    static final boolean DEBUG = false || Config.DEV_MODE;

    /**
     * @details 默认charset为{@link Config#ENCODING}
     * @param data 模板上下文对象集合
     * @see BaseView#render(Object, Object)
     */
    @Override
    public HttpEntity render(HttpRequest request, final String tempFile,
            final Map<String, Object> data) throws IOException {
        if (Config.USE_GZIP && GzipUtil.getSingleton().isGZipSupported(request)) {
            if (Config.USE_FILE_CACHE && TempCacheFilter.isCacheTemp(tempFile)) {
                File cacheFile = new File(Config.FILE_CACHE_DIR, tempFile + Config.EXT_GZIP);
                return renderFromCacheGzipFile(cacheFile, tempFile, data);
            } else {
                if (DEBUG)
                    Log.d(TAG, "Directly return gzip stream for " + tempFile);
                String html = TempHandler.render(tempFile, data);
                return new GzipByteArrayEntity(html.getBytes(), false);
            }
        } else if (Config.USE_FILE_CACHE && TempCacheFilter.isCacheTemp(tempFile)) {
            File cacheFile = new File(Config.FILE_CACHE_DIR, tempFile);
            return renderFromCacheFile(cacheFile, tempFile, data);
        }
        String html = TempHandler.render(tempFile, data);
        return new StringEntity(html, Config.ENCODING);
    }

    private HttpEntity renderFromCacheGzipFile(File cacheFile, String tempFile,
            Map<String, Object> data) throws IOException {
        if (cacheFile.exists()) {
            if (DEBUG)
                Log.d(TAG, "Read from cache " + cacheFile);
        } else {
            String html = TempHandler.render(tempFile, data);
            writeStringToGzipFile(cacheFile, html);
            if (DEBUG)
                Log.d(TAG, "Cache to " + cacheFile + " and read it.");
        }
        return new GzipFileEntity(cacheFile, "text/html", true);
    }

    private void writeStringToGzipFile(File file, String data) throws IOException {
        FileOutputStream os = new FileOutputStream(file);
        InputStream is = new ByteArrayInputStream(data.getBytes());
        try {
            GzipUtil.getSingleton().gzip(is, os);
        } finally {
            os.close();
            is.close();
        }
    }

    private HttpEntity renderFromCacheFile(File cacheFile, String tempFile, Map<String, Object> data)
            throws IOException {
        if (cacheFile.exists()) {
            if (DEBUG)
                Log.d(TAG, "Read from cache " + cacheFile);
        } else {
            String html = TempHandler.render(tempFile, data);
            writeStringToFile(cacheFile, html);
            if (DEBUG)
                Log.d(TAG, "Cache to " + cacheFile + " and read it.");
        }
        return new FileEntity(cacheFile, "text/html");
    }

    private void writeStringToFile(File file, String data) throws IOException {
        file.getParentFile().mkdirs();
        file.createNewFile();
        FileWriter writer = new FileWriter(file);
        try {
            writer.write(data);
        } finally {
            writer.close();
        }
    }

}


text:
SorceryWand.configure do |config|
  ##submodules
  # list submodules that is wanted to be used
  # ex: config.submodules = ['password_archivable']
  # options: 'password_archivable'
  config.submodules = []
  ## password_archivable ##

  # total max password archived
  # default: 5
  # config.password_archivable_count = 5
  config.user_class = ''
end


text:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;

namespace Sleet
{
    public static class PushCommand
    {
        public const int DefaultBatchSize = 4096;

        public static async Task<bool> RunAsync(LocalSettings settings, ISleetFileSystem source, List<string> inputs, bool force, bool skipExisting, ILogger log)
        {
            var token = CancellationToken.None;
            var now = DateTimeOffset.UtcNow;
            var success = false;
            var perfTracker = source.LocalCache.PerfTracker;

            await log.LogAsync(LogLevel.Minimal, $"Reading feed {source.BaseURI.AbsoluteUri}");

            using (var timer = PerfEntryWrapper.CreateSummaryTimer("Total execution time: {0}", perfTracker))
            {
                // Partition package inputs to avoid reading 100K nuspecs at the same time.
                var packagePaths = GetPackagePaths(inputs);
                var inputBatches = packagePaths.Partition(DefaultBatchSize);
                ISleetFileSystemLock feedLock = null;

                try
                {
                    for (var i = 0; i < inputBatches.Count; i++)
                    {
                        var inputBatch = inputBatches[i];
                        if (inputBatches.Count > 1)
                        {
                            await log.LogAsync(LogLevel.Minimal, $"Pushing {inputBatch.Count} packages. Batch: {i + 1} / {inputBatches.Count}");
                        }

                        // Read packages before locking the feed the first time.
                        var packages = new List<PackageInput>(await GetPackageInputs(inputBatch, now, perfTracker, log));

                        if (feedLock == null)
                        {
                            string lockMessage = null;
                            if (packages.Count > 0)
                            {
                                lockMessage = $"Push of {packages[0].Identity.ToString()}";
                            }

                            // Create and initialize the feed if it is new.
                            // Lock the feed before running push.
                            feedLock = await SourceUtility.InitAndLock(settings, source, lockMessage, autoCreateBucket: true, autoInit: true, log: log, token: token);

                            // Validate source
                            await SourceUtility.ValidateFeedForClient(source, log, token);
                        }

                        // Push
                        success = await PushPackages(settings, source, packages, force, skipExisting, log, token);
                    }
                }
                finally
                {
                    // Unlock the feed
                    feedLock?.Dispose();
                }
            }

            // Write out perf summary
            await perfTracker.LogSummary(log);

            return success;
        }

        /// <summary>
        /// Push packages to a feed.
        /// This assumes the feed is already locked.
        /// </summary>
        public static async Task<bool> PushPackages(LocalSettings settings, ISleetFileSystem source, List<string> inputs, bool force, bool skipExisting, ILogger log, CancellationToken token)
        {
            var now = DateTimeOffset.UtcNow;
            var success = true;

            var packagePaths = GetPackagePaths(inputs);

            // Partition input files to avoid reading 100K nuspec files at once.
            foreach (var inputSegment in packagePaths.Partition(DefaultBatchSize))
            {
                var packages = new List<PackageInput>();

                // Get packages
                packages.AddRange(await GetPackageInputs(inputSegment, now, source.LocalCache.PerfTracker, log));

                // Add packages
                success &= await PushPackages(settings, source, packages, force, skipExisting, log, token);
            }

            return success;
        }

        /// <summary>
        /// Push packages to a feed.
        /// This assumes the feed is already locked.
        /// </summary>
        public static async Task<bool> PushPackages(LocalSettings settings, ISleetFileSystem source, List<PackageInput> packages, bool force, bool skipExisting, ILogger log, CancellationToken token)
        {
            var exitCode = true;
            var now = DateTimeOffset.UtcNow;

            // Verify no duplicate packages
            CheckForDuplicates(packages);

            // Get sleet.settings.json
            await log.LogAsync(LogLevel.Minimal, "Reading feed");
            var sourceSettings = await FeedSettingsUtility.GetSettingsOrDefault(source, log, token);

            // Settings context used for all operations
            var context = new SleetContext()
            {
                LocalSettings = settings,
                SourceSettings = sourceSettings,
                Log = log,
                Source = source,
                Token = token,
                PerfTracker = source.LocalCache.PerfTracker
            };

            await log.LogAsync(LogLevel.Verbose, "Reading existing package index");

            // Push
            var packageIndex = new PackageIndex(context);
            var existingPackageSets = await packageIndex.GetPackageSetsAsync();

            await PushPackages(packages, context, existingPackageSets, force, skipExisting, log);

            // Prune packages
            await PrunePackages(packages, context);

            // Save all
            await log.LogAsync(LogLevel.Minimal, $"Committing changes to {source.BaseURI.AbsoluteUri}");

            await source.Commit(log, token);

            if (exitCode)
            {
                await log.LogAsync(LogLevel.Minimal, "Successfully pushed packages.");
            }
            else
            {
                await log.LogAsync(LogLevel.Error, "Failed to push packages.");
            }

            return exitCode;
        }

        private static async Task PrunePackages(List<PackageInput> packages, SleetContext context)
        {
            // Run only if the package retention settings are present for the feed
            if (context.SourceSettings.RetentionMaxStableVersions > 0 && context.SourceSettings.RetentionMaxPrereleaseVersions > 0)
            {
                // Prune if prune is enabled for the feed
                var pruneContext = new RetentionPruneCommandContext();
                // Do not prune packages that were just pushed
                pruneContext.PinnedPackages.UnionWith(packages.Select(e => e.Identity));
                // Only prune package ids that were pushed
                pruneContext.PackageIds.UnionWith(packages.Select(e => e.Identity.Id));
                // Run prune against the local files
                await RetentionPruneCommand.PrunePackagesNoCommit(context, pruneContext);
            }
        }

        private static async Task PushPackages(List<PackageInput> packageInputs, SleetContext context, PackageSets existingPackageSets, bool force, bool skipExisting, ILogger log)
        {
            var toAdd = new List<PackageInput>();
            var toRemove = new List<PackageInput>();

            foreach (var package in packageInputs)
            {
                var packageString = $"{package.Identity.Id} {package.Identity.Version.ToFullString()}";

                if (package.IsSymbolsPackage)
                {
                    packageString += " Symbols";

                    if (!context.SourceSettings.SymbolsEnabled)
                    {
                        await log.LogAsync(LogLevel.Warning, $"Skipping {packageString}, to push symbols packages enable the symbols server on this feed.");

                        // Skip this package
                        continue;
                    }
                }

                var exists = false;

                if (package.IsSymbolsPackage)
                {
                    exists = existingPackageSets.Symbols.Exists(package.Identity);
                }
                else
                {
                    exists = existingPackageSets.Packages.Exists(package.Identity);
                }

                if (exists)
                {
                    if (skipExisting)
                    {
                        await log.LogAsync(LogLevel.Minimal, $"Skip existing package: {packageString}");
                        continue;
                    }
                    else if (force)
                    {
                        toRemove.Add(package);
                        await log.LogAsync(LogLevel.Information, $"Replace existing package: {packageString}");
                    }
                    else
                    {
                        throw new InvalidOperationException($"Package already exists: {packageString}.");
                    }
                }
                else
                {
                    await log.LogAsync(LogLevel.Minimal, $"Add new package: {packageString}");
                }

                // Add to list of packages to push
                toAdd.Add(package);
            }

            await log.LogAsync(LogLevel.Minimal, $"Processing feed changes");

            // Add/Remove packages
            var changeContext = SleetOperations.Create(existingPackageSets, toAdd, toRemove);
            await SleetUtility.ApplyPackageChangesAsync(context, changeContext);
        }

        /// <summary>
        /// Parse input arguments for nupkg paths.
        /// </summary>
        private static async Task<List<PackageInput>> GetPackageInputs(List<string> packagePaths, DateTimeOffset now, IPerfTracker perfTracker, ILogger log)
        {
            using (var timer = PerfEntryWrapper.CreateSummaryTimer("Loaded package nuspecs in {0}", perfTracker))
            {
                var tasks = packagePaths.Select(e => new Func<Task<PackageInput>>(() => GetPackageInput(e, log)));
                var packageInputs = await TaskUtils.RunAsync(tasks, useTaskRun: true, token: CancellationToken.None);
                var packagesSorted = packageInputs.OrderBy(e => e).ToList();
                return packagesSorted;
            }
        }

        private static List<string> GetPackagePaths(List<string> inputs)
        {
            // Check inputs
            if (inputs.Count < 1)
            {
                throw new ArgumentException("No packages found.");
            }

            // Get package inputs
            return inputs.SelectMany(GetFiles)
               .Distinct(PathUtility.GetStringComparerBasedOnOS())
               .ToList();
        }

        private static void CheckForDuplicates(List<PackageInput> packages)
        {
            PackageInput lastPackage = null;
            foreach (var package in packages.OrderBy(e => e))
            {
                if (package.Equals(lastPackage))
                {
                    throw new InvalidOperationException($"Duplicate packages detected for '{package.Identity}'.");
                }

                lastPackage = package;
            }
        }

        private static Task<PackageInput> GetPackageInput(string file, ILogger log)
        {
            // Validate package
            log.LogVerbose($"Reading {file}");
            PackageInput packageInput = null;

            try
            {
                // Read basic info from the package and verify that it isn't broken.
                packageInput = PackageInput.Create(file);
            }
            catch
            {
                log.LogError($"Invalid package '{file}'.");
                throw;
            }

            // Display a message for non-normalized packages
            if (packageInput.Identity.Version.ToString() != packageInput.Identity.Version.ToNormalizedString())
            {
                var message = $"Package '{packageInput.PackagePath}' does not contain a normalized version. Normalized: '{packageInput.Identity.Version.ToNormalizedString()}' Nuspec version: '{packageInput.Identity.Version.ToString()}'. See https://semver.org/ for details.";
                log.LogVerbose(message);
            }

            return Task.FromResult(packageInput);
        }

        private static IEnumerable<string> GetFiles(string input)
        {
            var inputFile = Path.GetFullPath(input);

            if (File.Exists(inputFile))
            {
                yield return inputFile;
            }
            else if (Directory.Exists(inputFile))
            {
                var directoryFiles = Directory.GetFiles(inputFile, "*.nupkg", SearchOption.AllDirectories);

                if (directoryFiles.Length < 1)
                {
                    throw new FileNotFoundException($"Unable to find nupkgs in '{inputFile}'.");
                }

                foreach (var file in directoryFiles)
                {
                    yield return file;
                }
            }
            else
            {
                throw new FileNotFoundException($"Unable to find '{inputFile}'.");
            }
        }
    }
}


text:
'use strict';

angular.module('transcript.app.security.confirm', ['ui.router'])

    .config(['$stateProvider', function($stateProvider) {
        $stateProvider.state('transcript.app.security.confirm', {
            views: {
                "page" : {
                    templateUrl: '/webapp/app/App/Security/Confirm/Confirm.html',
                        controller: 'AppSecurityConfirmCtrl'
                }
            },
            url: '/confirm/{token}',
            tfMetaTags: {
                title: 'Confirmation d\'inscription',
            },
            resolve: {
                confirmation: function(UserService, $transition$) {
                    return UserService.confirm($transition$.params().token);
                }
            }
        })
    }])

    .controller('AppSecurityConfirmCtrl', ['$log', '$rootScope','$scope', '$http', '$sce', '$state', 'confirmation', function($log, $rootScope, $scope, $http, $sce, $state, confirmation) {
        $scope.status = confirmation.code;

        if($rootScope.user !== undefined && $rootScope.user !== null) {
            $state.go('transcript.app.user.profile', {id: $rootScope.user.id});
        }
    }])
;