text:
{-# LANGUAGE OverloadedStrings #-}
-- | Tests of C bindings.
-- Just a few unitary tests.
module C where
import System.NanoMsg.C.NanoMsg
import System.NanoMsg.C.NanoMsgStruct
import Test.HUnit
import System.NanoMsg.C.NanoMsg
import System.NanoMsg.C.NanoMsgStruct
import Control.Concurrent(forkIO)
import Control.Concurrent.MVar(newEmptyMVar)
import Control.Concurrent.MVar(putMVar)
import Control.Concurrent.MVar(takeMVar)
import Control.Concurrent(forkOS)
import Foreign.C.String(peekCStringLen)
import Foreign.C.String(newCString)
import Foreign.C.String(newCAStringLen)
import Foreign.C.String(peekCString)
import qualified Data.ByteString as BS
import Data.ByteString.Internal(ByteString(PS))
import Data.List(foldl')
import Control.Monad(foldM_)
import Foreign
import Foreign.C.Types
import Control.Concurrent(threadDelay)

testNnSocket :: IO Test
testNnSocket = do
   -- testing nn_socket (c level : return a generic socket)
   eSockpub <- nnSocket AF_SP NN_PUB
   sockpub <- getEr eSockpub

   eSocksub <- nnSocket AF_SP NN_SUB
   socksub <- getEr eSockpub

   eSockpub2 <- nnSocket AF_SP_RAW NN_PUB
   eSocksub2 <- nnSocket AF_SP_RAW NN_SUB

   return $ "Socket creation" ~:
     [ isRight eSockpub ~? "Failed socket init"
     , isRight eSocksub ~? "Failed socket init"
     , Left EAFNOSUPPORT ~=? eSockpub2
     , Left EAFNOSUPPORT ~=? eSocksub2
     ]

testNnClose :: IO Test
testNnClose = do
   s' <- nnSocket AF_SP NN_PUB >>= getEr
   c1 <- nnClose s'
   c2 <- nnClose s'
   return $ "Socket close" ~:
     [ Nothing    ~=? c1
     , Just EBADF ~=? c2
     ]

testNnOptions :: IO Test
testNnOptions = do
  sockpub <- nnSocket AF_SP NN_PUB >>= getEr

  let cintsize = sizeOf (undefined :: CInt)
  (err, val, size) <- allocaBytes cintsize (\p -> nnGetsockopt sockpub NN_SOL_SOCKET NN_DOMAIN p cintsize)
  v1 <- case err of
     Nothing -> peek (castPtr val :: Ptr CInt) >>= (return . (cIntToEnum :: CInt -> AddressFamilies))
  (err, val, size) <- allocaBytes cintsize (\p -> nnGetsockopt sockpub NN_SOL_SOCKET NN_PROTOCOL p cintsize)
  v2 <- case err of
     Nothing -> peek (castPtr val :: Ptr CInt) >>= (return . (cIntToEnum :: CInt -> NnProtocol))
     Just e ->  undefined
  let linger = 446654
  let clinger = fromIntegral linger :: CInt
  alloca (\p -> poke p clinger >> nnSetsockopt sockpub NN_SOL_SOCKET NN_LINGER (castPtr p) (fromIntegral (sizeOf clinger))) >>= getErr

  (err, val, size) <- allocaBytes cintsize (\p -> nnGetsockopt sockpub NN_SOL_SOCKET NN_LINGER p cintsize)
  v3 <- case err of
     Nothing -> peekInt (castPtr val :: Ptr CInt)
     Just e -> undefined
  return $ "Socket options" ~:
     [ AF_SP  ~=? v1
     , NN_PUB ~=? v2
     , linger ~=? v3
     ]

testNnBindConnect :: IO Test
testNnBindConnect = do
   sockpush <- nnSocket AF_SP NN_PUSH >>= getEr -- ok
   sockpair1 <- nnSocket AF_SP NN_PAIR >>= getEr -- ok
   sockpair2 <- nnSocket AF_SP NN_PAIR >>= getEr -- ok
   sockpair1' <- nnSocket AF_SP NN_PAIR >>= getEr -- ok
   sockpair2' <- nnSocket AF_SP NN_PAIR >>= getEr -- ok

   eppush <- nnBind sockpush "dummy://localhost:8080"
   eppush' <- nnBind sockpair1 "tcp://localhost/8080"
   eppair1 <- nnBind sockpair1' "inproc://test"
   eppair1' <- getEr eppair1
   eppair2 <- nnBind sockpair2 "inproc://test"
   nnShutdown sockpair1' eppair1'
   eppair3 <- nnBind sockpair2 "inproc://test"
   eppair2' <- nnConnect sockpair1 "inproc://test"
   nnClose sockpair1
   nnClose sockpair1'
   nnClose sockpair2
   nnClose sockpair2'
   return $ "Socket Bind/connect" ~:
     [ Left EPROTONOSUPPORT ~=? eppush
     , Left EINVAL ~=? eppush'
     , isRight eppair1 ~? "Socket Bind error"
     , Left EADDRINUSE ~=? eppair2
     , isRight eppair3 ~? "Socket shutdown error"
     , isRight eppair2' ~? "Socket connect error"
     ]

initPairs = do
   sockpair1 <- nnSocket AF_SP NN_PAIR >>= getEr -- ok
   sockpair2 <- nnSocket AF_SP NN_PAIR >>= getEr -- ok
   nnBind sockpair1 "inproc://test1"
   nnConnect sockpair2 "inproc://test1"
   return (sockpair1,sockpair2)

cintsize = sizeOf (undefined :: CInt)

testNnSendReceiveStatic :: IO Test
testNnSendReceiveStatic = do
   (sockpair1,sockpair2) <- initPairs
--   threadDelay 200000
   si <- alloca (\p -> poke p (3 :: CInt) >> nnSend' sockpair2 (castPtr p) cintsize [] >>= getEr)
   if si == cintsize then return () else putStrLn "Size pb in send"
   v1 <- alloca (\p -> nnRecv' sockpair1 (castPtr p) cintsize [] >>= getEr >> peekInt (p :: Ptr CInt)) -- TODO peek with returned size!!

   pv <- mallocForeignPtr :: IO (ForeignPtr CInt)
   withForeignPtr pv (\pv' -> poke pv' (fromIntegral 4))
   si <- nnSend sockpair2 (castForeignPtr pv) cintsize [] >>= getEr
   if si == cintsize then return () else putStrLn "Size pb in send"
   p <- mallocForeignPtr :: IO (ForeignPtr CInt)
   nnRecv sockpair1 (castForeignPtr p) cintsize [] >>= getEr
   v2 <- withForeignPtr p peekInt
   nnClose sockpair1
   nnClose sockpair2
   return $ "Send Receive static" ~:
     [ 3 ~=? v1
     , 4 ~=? v2
     ]

testNnSendReceiveDyn :: IO Test
testNnSendReceiveDyn = do
   (sockpair1,sockpair2) <- initPairs
   let m = "Hello wordk" :: String
   let l = length m
   p <- nnAllocmsg l 0 -- TODO link on bytestring to do unsafe zero copy -- here does not make sense
   p <- getEr p
   foldM_  (\a c -> pokeByteOff p a c >> return (a + 1)) 0 m
   m' <- peekCString (castPtr p)
   r  <- nnSendDyn sockpair2 (castPtr p) [] -- TODO bracket it ??
   si <- getEr r
   case r of Left _ -> nnFreemsg p
             Right _ -> return Nothing

   (sr, v) <- nnRecvDyn' sockpair1 []
   si1 <- getEr sr
   r1 <- peekCStringLen ((castPtr v),  si1)
   let m2 = "Hello word2k" :: String
   let l2 = length m2
   p2 <- nnAllocmsg (l2) 0 -- TODO link on bytestring to do unsafe zero copy -- here does not make sense
   p2 <- getEr p2
   foldM_  (\a c -> pokeByteOff p2 a c >> return (a + 1)) 0 m2
   r <- nnSendDyn sockpair2 (castPtr p2) []
   si <- getEr r
   case r of Left _ -> nnFreemsg p2
             Right _ -> return Nothing
   (sr, v) <- nnRecvDyn sockpair1 []
   si2 <- getEr sr
   r2 <- withForeignPtr (castForeignPtr v) (\p' ->  peekCStringLen (p',  si2))
   nnClose sockpair1
   nnClose sockpair2
   return $ "Send Receive dynamic" ~:
     [ m ~=? m'
     , (length m,m) ~=? (si1,r1)
     , (length m2,m2) ~=? (si2,r2)
     ]

testNnCmsg :: IO Test
testNnCmsg = do
   (v1, l1) <- newCAStringLen "Hello"
   (v2, l2) <- newCAStringLen "World"
   let iovec1 = NNIoVec (castPtr v1) (fromIntegral l1)
   let iovec2 = NNIoVec (castPtr v2) (fromIntegral l2)
   let nbvec = 2
   iovec <- mallocArray nbvec
   pokeArray iovec [iovec1,iovec2]
--   cmsghdr <- newNnCmsghdr
   let cmsghdr = NNCMsgHdr 3 1 1 -- level 1 type 1 size 0 only, dynamic length only
   nncdata <- ptrToCData [(cmsghdr, "123")] -- TODO implement api which create hdr
   ptcdata <- malloc :: IO (Ptr CInt) -- any ptr TODO in api alloca or foreignfree
   poke (castPtr ptcdata) (castPtr nncdata)
   let msghdr = NNMsgHdr (castPtr iovec) (fromIntegral nbvec) (castPtr ptcdata) 1 -- Not implemented yet in NNMsg
   let msghdr = NNMsgHdr (castPtr iovec) (fromIntegral nbvec) nullPtr 0 -- Not implemented yet in NNMsg
   cmsg <- cmsgFirstHdr msghdr
   let r = case cmsg of
                        Nothing -> True
                        Just _  -> False
   return $ "Cmsg macro" ~:
    [ r ~? "Fail read write in cmsg"
    ]

testNnSendReceiveMsg :: IO Test
testNnSendReceiveMsg = do
   (sockpair1,sockpair2) <- initPairs
   (v1, l1) <- newCAStringLen "Hello"
   (v2, l2) <- newCAStringLen "World"
   let iovec1 = NNIoVec (castPtr v1) (fromIntegral l1)
   let iovec2 = NNIoVec (castPtr v2) (fromIntegral l2)
   let nbvec = 2
   iovec <- mallocArray nbvec
   pokeArray iovec [iovec1,iovec2]
   let msghdr = NNMsgHdr (castPtr iovec) (fromIntegral nbvec) nullPtr 0 -- Not implemented yet in NNMsg
   si <- nnSendmsg sockpair2 msghdr [] >>= getEr

   (v1R, l1R) <- newCAStringLen "aaaaaaa" --Size is too long = wrong get content out of buffer
   (v2R, l2R) <- newCAStringLen "hhh" -- Size is too short = ok some content is discard
   let iovec1R = NNIoVec (castPtr v1R) (fromIntegral l1R)
   let iovec2R = NNIoVec (castPtr v2R) (fromIntegral l2R)
   iovecR <- mallocArray nbvec
   pokeArray iovecR [iovec1R,iovec2R]
   let msghdrRec = NNMsgHdr (castPtr iovecR) (fromIntegral nbvec) nullPtr 0
   -- overwright in existing struct
   si1 <- nnRecvmsg sockpair1 msghdrRec [] >>= getEr
   r1 <- peekCString v1R
   r2 <- peekCStringLen (v2R, (fromIntegral l2R))

   -- Dynamic MSG alloc
   let m3 = "Hello word3k." :: String
   let l3 = length m3
   p3' <- nnAllocmsg (l3) 0 -- TODO link on bytestring to do unsafe zero copy -- here does not make sense
   p3 <- getEr p3'
   ptS <- malloc :: IO (Ptr (Ptr CChar)) -- any ptr TODO in api alloca or foreignfree
   poke (castPtr ptS) p3
   foldM_  (\a c -> pokeByteOff (castPtr p3) a c >> return (a + 1)) 0 m3
   let iovec3 = NNIoVec (castPtr ptS) (fromIntegral nN_MSG)
   iovecA3 <- mallocArray 1
   pokeArray iovecA3 [iovec3]
   let msghdr3 = NNMsgHdr (castPtr iovecA3) 1 nullPtr 0

   si <- nnSendmsg sockpair2 msghdr3 [] >>= getEr
    --TODO
   ptR <- malloc :: IO (Ptr (Ptr CChar)) -- any ptr TODO in api alloca or foreignfree
   let iovec3R = NNIoVec (castPtr ptR) (fromIntegral nN_MSG)
   iovec3AR <- mallocArray 1 -- only one element everytime -> use simple ptr alloca
   pokeArray iovec3AR [iovec3R]
   let msghdrRec3 = NNMsgHdr (castPtr iovec3AR) (fromIntegral 1) nullPtr 0
   si <- nnRecvmsg sockpair1 msghdrRec3 [] >>= getEr
   p <- peek ptR
   r3 <- peekCStringLen (p, (fromIntegral si))
   nnFreemsg (castPtr p) -- TODO bracket??
   -- on api do not new but allocaArray or foreignfree (given fix msg length)
   free ptS
   free ptR
--   free ptcdata
   free v1
   free v2
   free iovec
   free v1R
   free v2R
   free iovecR

   nnClose sockpair1
   nnClose sockpair2
   return $ "Send Receive MessagMessage" ~:
     [ 10 ~=? si1
     , "HelloWo" ~=? r1
     , "rld" ~=? r2
     , m3 ~=? r3
     ]

-- TODO quick check it from api
testNnSendReceiveMsgHdr :: IO Test
testNnSendReceiveMsgHdr = do
   (sockpair1,sockpair2) <- initPairs
   (v1, l1) <- newCAStringLen "Dummy"
   let iovec1 = NNIoVec (castPtr v1) (fromIntegral l1)
   let nbvec = 1
   iovec <- mallocArray nbvec
   pokeArray iovec [iovec1]
   let hdrorig = [("123", 1, 5),("54321", 2, 1)]
   let nbhd = fromIntegral $ length hdrorig
   nncdata <- newMSgHdr hdrorig >>= getEr
   ptcdata <- malloc :: IO (Ptr (Ptr (NNCMsgHdr)))
   poke ptcdata nncdata

   let msghdr = NNMsgHdr (castPtr iovec) (fromIntegral nbvec) (castPtr ptcdata) nbhd
   si <- nnSendmsg sockpair2 msghdr [] >>= getEr

   (v1R, l1R) <- newCAStringLen "aaaaa" --Size is too long = wrong get content out of buffer
   let iovec1R = NNIoVec (castPtr v1R) (fromIntegral l1R)
   iovecR <- mallocArray nbvec
   pokeArray iovecR [iovec1R]

   ptcdataR <- malloc :: IO (Ptr (Ptr (NNCMsgHdr)))
   let msghdrRec = NNMsgHdr (castPtr iovecR) (fromIntegral nbvec) (castPtr ptcdataR) nbhd
   -- overwright in existing struct
   si1 <- nnRecvmsg sockpair1 msghdrRec [] >>= getEr
   r1 <- peekCStringLen (v1R, (fromIntegral l1R))

   ptch' <- peek ptcdataR
   hdrs <- getMSgHdr ptch' (fromIntegral nbhd) >>= getEr

--   free ptcdata
   free v1
   free iovec
   free v1R
   free iovecR

   free ptcdata
   free ptcdataR

   nnClose sockpair1
   nnClose sockpair2
   return $ "Send Receive MessagMessageHdr" ~:
     [ hdrorig ~=? hdrs
     ]

isRight (Right _) = True
isRight (Left _) = False
isLeft (Left _) = True
isLeft (Right _) = False

getEr (Left a) = showEr a >> return undefined
getEr (Right b) = return b
getErr (Just a) = showEr a
getErr Nothing = return ()
showEr i = do
  label <- nnStrerror i
  putStrLn $ "Error " ++ show i ++ " = " ++ label

ptrToCData :: [(NNCMsgHdr, BS.ByteString)] -> IO (Ptr ()) -- TODO transform to with fn or foreignfree returned
ptrToCData  a = do
  let totle = foldl' (\acc (h, bs)-> acc + BS.length bs + sizeOf h) 0 a -- TODO do not use BS.length
  ptr <- mallocBytes (totle)

  foldM_ (pokeEl (ptr)) 0 a  >> return (ptr)
    where pokeEl :: Ptr () -> Int -> (NNCMsgHdr, BS.ByteString) -> IO Int
          pokeEl ptr offset (hdr, bs@(PS pbs pof ple)) = do
                                        pokeElemOff (castPtr ptr) offset hdr
                                        --pokeElemOff ptr (offset + sizeOf hdr) bs
                                        withForeignPtr pbs $ \pbs' -> copyBytes ((castPtr ptr) `plusPtr` (offset + sizeOf hdr)) ((castPtr pbs') `plusPtr` pof) (ple - pof) -- TODO use unsafe bytestring api instead
                                        return (offset + sizeOf hdr + (ple - pof))


text:
<?php

namespace spec\Sylius\Bundle\SandboxBundle\Entity;

use PHPSpec2\ObjectBehavior;

/**
 * Tax rate model spec.
 *
 * @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
 */
class TaxRate extends ObjectBehavior
{
    function it_should_be_initializable()
    {
        $this->shouldHaveType('Sylius\Bundle\SandboxBundle\Entity\TaxRate');
    }

    function it_should_be_a_Sylius_tax_rate()
    {
        $this->shouldImplement('Sylius\Bundle\TaxationBundle\Model\TaxRateInterface');
    }

    function it_should_extend_Sylius_tax_rate_entity()
    {
        $this->shouldHaveType('Sylius\Bundle\TaxationBundle\Entity\TaxRate');
    }

    function it_should_not_have_zone_defined_by_default()
    {
        $this->getZone()->shouldReturn(null);
    }

    /**
     * @param Sylius\Bundle\AddressingBundle\Model\ZoneInterface $zone
     */
    function it_should_allow_defining_zone($zone)
    {
        $this->setZone($zone);
        $this->getZone()->shouldReturn($zone);
    }

    /**
     * @param Sylius\Bundle\AddressingBundle\Model\ZoneInterface $zone
     */
    function it_should_allow_undefining_zone($zone)
    {
        $this->setZone($zone);
        $this->getZone()->shouldReturn($zone);

        $this->setZone(null);
        $this->getZone()->shouldReturn(null);
    }
}

redpajama_set_name: RedPajamaGithub


text:
package com.datadiff.web;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class MongoJaksonConverter
{
    @Autowired
    ObjectMapper objectMapper;

    public DBObject convert(JsonNode input) throws IOException {
        return (DBObject) JSON.parse(objectMapper.writeValueAsString(input));
    }

    public JsonNode convert(DBObject input) throws IOException {
        return objectMapper.readTree(input.toString());
    }
}


text:
import lan
import collect_id as ci

class LoopIndices(lan.NodeVisitor):
    """ Finds loop indices, the start and end values of the
        indices and creates a mapping from a loop index to
        the ForLoop AST node that is indexes.
    """

    def __init__(self, depth_limit=2):
        self.index = list()
        self.depth_limit = depth_limit
        self.depth = 0

    def visit_ForLoop(self, node):
        id_init = ci.Ids()
        id_init.visit(node.init)
        id_inc = ci.Ids()
        id_inc.visit(node.inc)
        self.index.extend(id_init.ids.intersection(id_inc.ids))
        self.depth += 1
        if self.depth < self.depth_limit:
            self.visit(node.compound)

    @property
    def grid_indices(self):
        return self.index

def get_grid_indices(ast):
    par_dim = get_par_dim(ast)
    loop_indices = LoopIndices(par_dim)
    loop_indices.visit(ast)
    return loop_indices.grid_indices

class LoopLimit(lan.NodeVisitor):
    """ Finds loop indices, the start and end values of the
        indices and creates a mapping from a loop index to
        the ForLoop AST node that is indexes.
    """

    def __init__(self):
        self.upper_limit = dict()
        self.lower_limit = dict()

    def visit_ForLoop(self, node):
        id_vis = ci.Ids()
        id_vis.visit(node.init)
        ids = list(id_vis.ids)

        self.visit(node.compound)
        try:
            self.upper_limit[ids[0]] = node.cond.rval.name
            self.lower_limit[ids[0]] = node.init.rval.value
        except AttributeError:
            self.upper_limit[ids[0]] = 'Unknown'
            self.lower_limit[ids[0]] = 'Unknown'

def get_loop_limits(ast):
    loop_limits = LoopLimit()
    loop_limits.visit(ast)
    return loop_limits.lower_limit, loop_limits.upper_limit

class FindInnerLoops(lan.NodeVisitor):
    """ Finds loop indices, the start and end values of the
        indices and creates a mapping from a loop index to
        the ForLoop AST node that is indexes.
    """

    def __init__(self):
        self.Loops = dict()
        self.par_dim = None
        self.GridIndices = list()

    def collect(self, ast):
        self.par_dim = get_par_dim(ast)
        col_li = LoopIndices(self.par_dim)
        col_li.visit(ast)
        self.GridIndices = col_li.grid_indices
        self.visit(ast)

    def visit_ForLoop(self, node):
        name = node.init.lval.name.name
        if name not in self.GridIndices:
            self.Loops[name] = node

        self.visit(node.compound)

def get_inner_loops(ast):
    find_inner_loops = FindInnerLoops()
    find_inner_loops.collect(ast)
    return find_inner_loops.Loops

def get_inner_loops_indices(ast):
    loops = get_inner_loops(ast)
    return loops.keys()

class FindPerfectForLoop(lan.NodeVisitor):
    """ Performs simple checks to decide if we have 1D or 2D
    parallelism, i.e. if we have a perfect loops nest of size one
    or two.
    """

    def __init__(self):
        self.depth = 0
        self.ast = None
        self.inner = None

    def visit_ForLoop(self, node):
        self.ast = node
        self.inner = node
        self.depth += 1
        loopstats = node.compound.statements
        if len(loopstats) == 1:
            if isinstance(loopstats[0], lan.ForLoop):
                self.depth += 1
                self.inner = loopstats[0]

    @property
    def outer(self):
        return self.ast

class FindParDim(lan.NodeVisitor):
    def __init__(self):
        self.par_dim = 0

    def collect(self, ast):
        self.visit(ast)

    def visit_ParDim(self, node):
        self.par_dim = node.par_dim

def get_par_dim(ast):
    perfect_for_loop = FindPerfectForLoop()
    perfect_for_loop.visit(ast)
    find_par_dim = FindParDim()
    find_par_dim.collect(ast)
    if find_par_dim.par_dim is None:
        return perfect_for_loop.depth
    else:
        return find_par_dim.par_dim

def get_local(ast, dev='CPU'):
    local = dict()
    local['name'] = 'LSIZE'
    local['size'] = ['64']
    par_dim = get_par_dim(ast)
    if par_dim == 1:
        local['size'] = ['256']
        if dev == 'CPU':
            local['size'] = ['16']
    else:
        local['size'] = ['16', '16']
        if dev == 'CPU':
            local['size'] = ['4', '4']
    return local


text:
"""http://en.wikipedia.org/wiki/Eight_queens_puzzle
"""

import sys

def solve(size, queens=None):
    queens = queens or []
    col = len(queens)

    if col == size:
        return queens

    for row in range(size):
        # Check if a queen is on the same row.
        if row in queens:
            continue

        # Check if a queen is on the same diagonal.
        same_diag = False
        for (other_col, other_row) in enumerate(queens):
            norm_row = row - other_row
            norm_col = col - other_col

            if norm_col == norm_row or norm_col == -norm_row:
                same_diag = True
                break

        if same_diag:
            continue

        solution = solve(size, queens + [row])

        if solution is not None:
            return solution

    return None

if __name__ == '__main__':
    size = int(sys.argv[1])
    solution = solve(size)

    board = [['_'] * size for i in range(size)]

    for col, row in enumerate(solution):
        board[row][col] = 'X'

    for row in board:
        print ' '.join(row)


text:
title:  Fugleburet
date:  09/07/2022
---

### Bakgrunnsstoff
2 Mos 14, 2 Mos 15,22–27, 2 Mos 17,1–7, Ordsp 3, Luk 4,1–13, 1 Pet 1,6–9

>
Minnevers

> Derfor kan dere juble av glede, selv om dere nå en kort tid, om så må være, har det tungt i mange slags prøvelser (1 Pet 1,6).

I fullt dagslys, og når den hører musikken i andre stemmer, vil ikke burfuglen synge den sangen som dens eier prøver å lære den. Den lærer litt her og der, men aldri en fullstendig melodi. Men mesteren dekker til buret og setter det der fuglen bare kan lytte til den ene sangen den skal synge. I mørket prøver den om og om igjen å synge sangen, til den er innlært. Så tas fuglen frem, og siden kan den alltid synge den sangen i dagslyset. Slik behandler Gud sine barn. Han har en sang å lære oss, og når vi har lært den i lidelsens skygge, kan vi alltid synge den.» – _The Ministry of Healing_, side 472.

Legg merke til at det er mesteren selv som setter fuglen i mørket.

Det er lett å forstå at Satan påfører smerte, men ville Gud selv aktivt være med på å føre oss inn i smeltedigler hvor vi opplever forvirring eller smerte?

**Ukeoversikt**

Hvilke eksempler kan du tenke på i Bibelen der Gud selv leder mennesker ut i opplevelser han vet vil inkludere lidelse? Hva tror du var de nye sangene han ville de skulle synge?


text:
.mx_ViewSource pre {
    text-align: left;
}


text:
"""Classification and regression using Stochastic Gradient Descent (SGD)."""

import numpy as np
import scipy.sparse as sp

from abc import ABCMeta, abstractmethod
import warnings

from ..externals.joblib import Parallel, delayed

from .base import LinearClassifierMixin, SparseCoefMixin
from ..base import BaseEstimator, RegressorMixin
from ..feature_selection.from_model import _LearntSelectorMixin
from ..utils import (array2d, atleast2d_or_csr, check_arrays, deprecated,
                     column_or_1d)
from ..utils.extmath import safe_sparse_dot
from ..utils.multiclass import _check_partial_fit_first_call
from ..externals import six

from .sgd_fast import plain_sgd as plain_sgd
from ..utils.seq_dataset import ArrayDataset, CSRDataset
from ..utils import compute_class_weight
from .sgd_fast import Hinge
from .sgd_fast import SquaredHinge
from .sgd_fast import Log
from .sgd_fast import ModifiedHuber
from .sgd_fast import SquaredLoss
from .sgd_fast import Huber
from .sgd_fast import EpsilonInsensitive
from .sgd_fast import SquaredEpsilonInsensitive

LEARNING_RATE_TYPES = {"constant": 1, "optimal": 2, "invscaling": 3,
                       "pa1": 4, "pa2": 5}

PENALTY_TYPES = {"none": 0, "l2": 2, "l1": 1, "elasticnet": 3}

SPARSE_INTERCEPT_DECAY = 0.01
"""For sparse data intercept updates are scaled by this decay factor to avoid
intercept oscillation."""

DEFAULT_EPSILON = 0.1
"""Default value of ``epsilon`` parameter. """

class BaseSGD(six.with_metaclass(ABCMeta, BaseEstimator, SparseCoefMixin)):
    """Base class for SGD classification and regression."""

    def __init__(self, loss, penalty='l2', alpha=0.0001, C=1.0,
                 l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=False,
                 verbose=0, epsilon=0.1, random_state=None,
                 learning_rate="optimal", eta0=0.0, power_t=0.5,
                 warm_start=False):
        self.loss = loss
        self.penalty = penalty
        self.learning_rate = learning_rate
        self.epsilon = epsilon
        self.alpha = alpha
        self.C = C
        self.l1_ratio = l1_ratio
        self.fit_intercept = fit_intercept
        self.n_iter = n_iter
        self.shuffle = shuffle
        self.random_state = random_state
        self.verbose = verbose
        self.eta0 = eta0
        self.power_t = power_t
        self.warm_start = warm_start

        self._validate_params()

        self.coef_ = None
        # iteration count for learning rate schedule
        # must not be int (e.g. if ``learning_rate=='optimal'``)
        self.t_ = None

    def set_params(self, *args, **kwargs):
        super(BaseSGD, self).set_params(*args, **kwargs)
        self._validate_params()
        return self

    @abstractmethod
    def fit(self, X, y):
        """Fit model."""

    def _validate_params(self):
        """Validate input params. """
        if not isinstance(self.shuffle, bool):
            raise ValueError("shuffle must be either True or False")
        if self.n_iter <= 0:
            raise ValueError("n_iter must be > zero")
        if not (0.0 <= self.l1_ratio <= 1.0):
            raise ValueError("l1_ratio must be in [0, 1]")
        if self.alpha < 0.0:
            raise ValueError("alpha must be >= 0")
        if self.learning_rate in ("constant", "invscaling"):
            if self.eta0 <= 0.0:
                raise ValueError("eta0 must be > 0")

        # raises ValueError if not registered
        self._get_penalty_type(self.penalty)
        self._get_learning_rate_type(self.learning_rate)

        if self.loss not in self.loss_functions:
            raise ValueError("The loss %s is not supported. " % self.loss)

    def _init_t(self, loss_function):
        """Initialize iteration counter attr ``t_``.

        If ``self.learning_rate=='optimal'`` initialize ``t_`` such that
        ``eta`` at first sample equals ``self.eta0``.
        """
        self.t_ = 1.0
        if self.learning_rate == "optimal":
            typw = np.sqrt(1.0 / np.sqrt(self.alpha))
            # computing eta0, the initial learning rate
            eta0 = typw / max(1.0, loss_function.dloss(-typw, 1.0))
            # initialize t such that eta at first sample equals eta0
            self.t_ = 1.0 / (eta0 * self.alpha)

    def _get_loss_function(self, loss):
        """Get concrete ``LossFunction`` object for str ``loss``. """
        try:
            loss_ = self.loss_functions[loss]
            loss_class, args = loss_[0], loss_[1:]
            if loss in ('huber', 'epsilon_insensitive',
                        'squared_epsilon_insensitive'):
                args = (self.epsilon, )
            return loss_class(*args)
        except KeyError:
            raise ValueError("The loss %s is not supported. " % loss)

    def _get_learning_rate_type(self, learning_rate):
        try:
            return LEARNING_RATE_TYPES[learning_rate]
        except KeyError:
            raise ValueError("learning rate %s "
                             "is not supported. " % learning_rate)

    def _get_penalty_type(self, penalty):
        penalty = str(penalty).lower()
        try:
            return PENALTY_TYPES[penalty]
        except KeyError:
            raise ValueError("Penalty %s is not supported. " % penalty)

    def _validate_sample_weight(self, sample_weight, n_samples):
        """Set the sample weight array."""
        if sample_weight is None:
            # uniform sample weights
            sample_weight = np.ones(n_samples, dtype=np.float64, order='C')
        else:
            # user-provided array
            sample_weight = np.asarray(sample_weight, dtype=np.float64,
                                       order="C")
        if sample_weight.shape[0] != n_samples:
            raise ValueError("Shapes of X and sample_weight do not match.")
        return sample_weight

    def _set_coef(self, coef_):
        """Make sure that coef_ is fortran-style and 2d.

        Fortran-style memory layout is needed to ensure that computing
        the dot product between input ``X`` and ``coef_`` does not trigger
        a memory copy.
        """
        self.coef_ = np.asfortranarray(array2d(coef_))

    def _allocate_parameter_mem(self, n_classes, n_features, coef_init=None,
                                intercept_init=None):
        """Allocate mem for parameters; initialize if provided."""
        if n_classes > 2:
            # allocate coef_ for multi-class
            if coef_init is not None:
                coef_init = np.asarray(coef_init, order="C")
                if coef_init.shape != (n_classes, n_features):
                    raise ValueError("Provided coef_ does not match dataset. ")
                self.coef_ = coef_init
            else:
                self.coef_ = np.zeros((n_classes, n_features),
                                      dtype=np.float64, order="C")

            # allocate intercept_ for multi-class
            if intercept_init is not None:
                intercept_init = np.asarray(intercept_init, order="C")
                if intercept_init.shape != (n_classes, ):
                    raise ValueError("Provided intercept_init "
                                     "does not match dataset.")
                self.intercept_ = intercept_init
            else:
                self.intercept_ = np.zeros(n_classes, dtype=np.float64,
                                           order="C")
        else:
            # allocate coef_ for binary problem
            if coef_init is not None:
                coef_init = np.asarray(coef_init, dtype=np.float64,
                                       order="C")
                coef_init = coef_init.ravel()
                if coef_init.shape != (n_features,):
                    raise ValueError("Provided coef_init does not "
                                     "match dataset.")
                self.coef_ = coef_init
            else:
                self.coef_ = np.zeros(n_features, dtype=np.float64, order="C")

            # allocate intercept_ for binary problem
            if intercept_init is not None:
                intercept_init = np.asarray(intercept_init, dtype=np.float64)
                if intercept_init.shape != (1,) and intercept_init.shape != ():
                    raise ValueError("Provided intercept_init "
                                     "does not match dataset.")
                self.intercept_ = intercept_init.reshape(1,)
            else:
                self.intercept_ = np.zeros(1, dtype=np.float64, order="C")

def _check_fit_data(X, y):
    """Check if shape of input data matches. """
    n_samples, _ = X.shape
    if n_samples != y.shape[0]:
        raise ValueError("Shapes of X and y do not match.")

def _make_dataset(X, y_i, sample_weight):
    """Create ``Dataset`` abstraction for sparse and dense inputs.

    This also returns the ``intercept_decay`` which is different
    for sparse datasets.
    """
    if sp.issparse(X):
        dataset = CSRDataset(X.data, X.indptr, X.indices, y_i, sample_weight)
        intercept_decay = SPARSE_INTERCEPT_DECAY
    else:
        dataset = ArrayDataset(X, y_i, sample_weight)
        intercept_decay = 1.0
    return dataset, intercept_decay

def _prepare_fit_binary(est, y, i):
    """Initialization for fit_binary.

    Returns y, coef, intercept.
    """
    y_i = np.ones(y.shape, dtype=np.float64, order="C")
    y_i[y != est.classes_[i]] = -1.0

    if len(est.classes_) == 2:
        coef = est.coef_.ravel()
        intercept = est.intercept_[0]
    else:
        coef = est.coef_[i]
        intercept = est.intercept_[i]

    return y_i, coef, intercept

def fit_binary(est, i, X, y, alpha, C, learning_rate, n_iter,
               pos_weight, neg_weight, sample_weight):
    """Fit a single binary classifier.

    The i'th class is considered the "positive" class.
    """
    y_i, coef, intercept = _prepare_fit_binary(est, y, i)
    assert y_i.shape[0] == y.shape[0] == sample_weight.shape[0]
    dataset, intercept_decay = _make_dataset(X, y_i, sample_weight)

    penalty_type = est._get_penalty_type(est.penalty)
    learning_rate_type = est._get_learning_rate_type(learning_rate)

    return plain_sgd(coef, intercept, est.loss_function,
                     penalty_type, alpha, C, est.l1_ratio,
                     dataset, n_iter, int(est.fit_intercept),
                     int(est.verbose), int(est.shuffle), est.random_state,
                     pos_weight, neg_weight,
                     learning_rate_type, est.eta0,
                     est.power_t, est.t_, intercept_decay)

class BaseSGDClassifier(six.with_metaclass(ABCMeta, BaseSGD,
                                           LinearClassifierMixin)):

    loss_functions = {
        "hinge": (Hinge, 1.0),
        "squared_hinge": (SquaredHinge, 1.0),
        "perceptron": (Hinge, 0.0),
        "log": (Log, ),
        "modified_huber": (ModifiedHuber, ),
        "squared_loss": (SquaredLoss, ),
        "huber": (Huber, DEFAULT_EPSILON),
        "epsilon_insensitive": (EpsilonInsensitive, DEFAULT_EPSILON),
        "squared_epsilon_insensitive": (SquaredEpsilonInsensitive,
                                        DEFAULT_EPSILON),
    }

    @abstractmethod
    def __init__(self, loss="hinge", penalty='l2', alpha=0.0001, l1_ratio=0.15,
                 fit_intercept=True, n_iter=5, shuffle=False, verbose=0,
                 epsilon=DEFAULT_EPSILON, n_jobs=1, random_state=None,
                 learning_rate="optimal", eta0=0.0, power_t=0.5,
                 class_weight=None, warm_start=False, seed=None):

        if seed is not None:
            warnings.warn("Parameter 'seed' was renamed to 'random_state' for"
                          " consistency and will be removed in 0.15",
                          DeprecationWarning)
            random_state = seed

        super(BaseSGDClassifier, self).__init__(loss=loss, penalty=penalty,
                                                alpha=alpha, l1_ratio=l1_ratio,
                                                fit_intercept=fit_intercept,
                                                n_iter=n_iter, shuffle=shuffle,
                                                verbose=verbose,
                                                epsilon=epsilon,
                                                random_state=random_state,
                                                learning_rate=learning_rate,
                                                eta0=eta0, power_t=power_t,
                                                warm_start=warm_start)
        self.class_weight = class_weight
        self.classes_ = None
        self.n_jobs = int(n_jobs)

    @property
    @deprecated("Parameter 'seed' was renamed to 'random_state' for"
                " consistency and will be removed in 0.15")
    def seed(self):
        return self.random_state

    def _partial_fit(self, X, y, alpha, C,
                     loss, learning_rate, n_iter,
                     classes, sample_weight,
                     coef_init, intercept_init):
        X = atleast2d_or_csr(X, dtype=np.float64, order="C")
        y = column_or_1d(y, warn=True)

        n_samples, n_features = X.shape
        _check_fit_data(X, y)

        self._validate_params()
        _check_partial_fit_first_call(self, classes)

        n_classes = self.classes_.shape[0]

        # Allocate datastructures from input arguments
        y_ind = np.searchsorted(self.classes_, y)   # XXX use a LabelBinarizer?
        self._expanded_class_weight = compute_class_weight(self.class_weight,
                                                           self.classes_,
                                                           y_ind)
        sample_weight = self._validate_sample_weight(sample_weight, n_samples)

        if self.coef_ is None or coef_init is not None:
            self._allocate_parameter_mem(n_classes, n_features,
                                         coef_init, intercept_init)

        self.loss_function = self._get_loss_function(loss)
        if self.t_ is None:
            self._init_t(self.loss_function)

        # delegate to concrete training procedure
        if n_classes > 2:
            self._fit_multiclass(X, y, alpha=alpha, C=C,
                                 learning_rate=learning_rate,
                                 sample_weight=sample_weight, n_iter=n_iter)
        elif n_classes == 2:
            self._fit_binary(X, y, alpha=alpha, C=C,
                             learning_rate=learning_rate,
                             sample_weight=sample_weight, n_iter=n_iter)
        else:
            raise ValueError("The number of class labels must be "
                             "greater than one.")

        self.t_ += n_iter * n_samples

        return self

    def _fit(self, X, y, alpha, C, loss, learning_rate,
             coef_init=None, intercept_init=None, class_weight=None,
             sample_weight=None):
        if hasattr(self, "classes_"):
            self.classes_ = None

        if class_weight is not None:
            warnings.warn("Using 'class_weight' as a parameter to the 'fit'"
                          " method is deprecated and will be removed in 0.13. "
                          "Set it on initialization instead.",
                          DeprecationWarning, stacklevel=2)

            self.class_weight = class_weight

        X = atleast2d_or_csr(X, dtype=np.float64, order="C")
        n_samples, n_features = X.shape

        # labels can be encoded as float, int, or string literals
        # np.unique sorts in asc order; largest class id is positive class
        classes = np.unique(y)

        if self.warm_start and self.coef_ is not None:
            if coef_init is None:
                coef_init = self.coef_
            if intercept_init is None:
                intercept_init = self.intercept_
        else:
            self.coef_ = None
            self.intercept_ = None

        # Clear iteration count for multiple call to fit.
        self.t_ = None

        self._partial_fit(X, y, alpha, C, loss, learning_rate, self.n_iter,
                          classes, sample_weight, coef_init, intercept_init)

        # fitting is over, we can now transform coef_ to fortran order
        # for faster predictions
        self._set_coef(self.coef_)

        return self

    def _fit_binary(self, X, y, alpha, C, sample_weight,
                    learning_rate, n_iter):
        """Fit a binary classifier on X and y. """
        coef, intercept = fit_binary(self, 1, X, y, alpha, C,
                                     learning_rate, n_iter,
                                     self._expanded_class_weight[1],
                                     self._expanded_class_weight[0],
                                     sample_weight)
        # need to be 2d
        self.coef_ = coef.reshape(1, -1)
        # intercept is a float, need to convert it to an array of length 1
        self.intercept_ = np.atleast_1d(intercept)

    def _fit_multiclass(self, X, y, alpha, C, learning_rate,
                        sample_weight, n_iter):
        """Fit a multi-class classifier by combining binary classifiers

        Each binary classifier predicts one class versus all others. This
        strategy is called OVA: One Versus All.
        """
        # Use joblib to fit OvA in parallel
        result = Parallel(n_jobs=self.n_jobs, verbose=self.verbose)(
            delayed(fit_binary)(self, i, X, y, alpha, C, learning_rate,
                                n_iter, self._expanded_class_weight[i], 1.,
                                sample_weight)
            for i in range(len(self.classes_)))

        for i, (coef, intercept) in enumerate(result):
            self.coef_[i] = coef
            self.intercept_[i] = intercept

    def partial_fit(self, X, y, classes=None, sample_weight=None):
        """Fit linear model with Stochastic Gradient Descent.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Subset of the training data

        y : numpy array of shape [n_samples]
            Subset of the target values

        classes : array, shape = [n_classes]
            Classes across all calls to partial_fit.
            Can be obtained by via `np.unique(y_all)`, where y_all is the
            target vector of the entire dataset.
            This argument is required for the first call to partial_fit
            and can be omitted in the subsequent calls.
            Note that y doesn't need to contain all labels in `classes`.

        sample_weight : array-like, shape = [n_samples], optional
            Weights applied to individual samples.
            If not provided, uniform weights are assumed.

        Returns
        -------
        self : returns an instance of self.
        """
        return self._partial_fit(X, y, alpha=self.alpha, C=1.0, loss=self.loss,
                                 learning_rate=self.learning_rate, n_iter=1,
                                 classes=classes, sample_weight=sample_weight,
                                 coef_init=None, intercept_init=None)

    def fit(self, X, y, coef_init=None, intercept_init=None,
            class_weight=None, sample_weight=None):
        """Fit linear model with Stochastic Gradient Descent.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training data

        y : numpy array of shape [n_samples]
            Target values

        coef_init : array, shape = [n_classes,n_features]
            The initial coefficients to warm-start the optimization.

        intercept_init : array, shape = [n_classes]
            The initial intercept to warm-start the optimization.

        sample_weight : array-like, shape = [n_samples], optional
            Weights applied to individual samples.
            If not provided, uniform weights are assumed.

        Returns
        -------
        self : returns an instance of self.
        """
        return self._fit(X, y, alpha=self.alpha, C=1.0,
                         loss=self.loss, learning_rate=self.learning_rate,
                         coef_init=coef_init, intercept_init=intercept_init,
                         class_weight=class_weight,
                         sample_weight=sample_weight)

class SGDClassifier(BaseSGDClassifier, _LearntSelectorMixin):
    """Linear classifiers (SVM, logistic regression, a.o.) with SGD training.

    This estimator implements regularized linear models with stochastic
    gradient descent (SGD) learning: the gradient of the loss is estimated
    each sample at a time and the model is updated along the way with a
    decreasing strength schedule (aka learning rate). SGD allows minibatch
    (online/out-of-core) learning, see the partial_fit method.

    This implementation works with data represented as dense or sparse arrays
    of floating point values for the features. The model it fits can be
    controlled with the loss parameter; by default, it fits a linear support
    vector machine (SVM).

    The regularizer is a penalty added to the loss function that shrinks model
    parameters towards the zero vector using either the squared euclidean norm
    L2 or the absolute norm L1 or a combination of both (Elastic Net). If the
    parameter update crosses the 0.0 value because of the regularizer, the
    update is truncated to 0.0 to allow for learning sparse models and achieve
    online feature selection.

    Parameters
    ----------
    loss : str, 'hinge', 'log', 'modified_huber', 'squared_hinge',\
                'perceptron', or a regression loss: 'squared_loss', 'huber',\
                'epsilon_insensitive', or 'squared_epsilon_insensitive'
        The loss function to be used. Defaults to 'hinge', which gives a
        linear SVM.
        The 'log' loss gives logistic regression, a probabilistic classifier.
        'modified_huber' is another smooth loss that brings tolerance to
        outliers as well as probability estimates.
        'squared_hinge' is like hinge but is quadratically penalized.
        'perceptron' is the linear loss used by the perceptron algorithm.
        The other losses are designed for regression but can be useful in
        classification as well; see SGDRegressor for a description.

    penalty : str, 'l2' or 'l1' or 'elasticnet'
        The penalty (aka regularization term) to be used. Defaults to 'l2'
        which is the standard regularizer for linear SVM models. 'l1' and
        'elasticnet' migh bring sparsity to the model (feature selection)
        not achievable with 'l2'.

    alpha : float
        Constant that multiplies the regularization term. Defaults to 0.0001

    l1_ratio : float
        The Elastic Net mixing parameter, with 0 <= l1_ratio <= 1.
        l1_ratio=0 corresponds to L2 penalty, l1_ratio=1 to L1.
        Defaults to 0.15.

    fit_intercept: bool
        Whether the intercept should be estimated or not. If False, the
        data is assumed to be already centered. Defaults to True.

    n_iter: int, optional
        The number of passes over the training data (aka epochs).
        Defaults to 5.

    shuffle: bool, optional
        Whether or not the training data should be shuffled after each epoch.
        Defaults to False.

    random_state: int seed, RandomState instance, or None (default)
        The seed of the pseudo random number generator to use when
        shuffling the data.

    verbose: integer, optional
        The verbosity level

    epsilon: float
        Epsilon in the epsilon-insensitive loss functions; only if `loss` is
        'huber', 'epsilon_insensitive', or 'squared_epsilon_insensitive'.
        For 'huber', determines the threshold at which it becomes less
        important to get the prediction exactly right.
        For epsilon-insensitive, any differences between the current prediction
        and the correct label are ignored if they are less than this threshold.

    n_jobs: integer, optional
        The number of CPUs to use to do the OVA (One Versus All, for
        multi-class problems) computation. -1 means 'all CPUs'. Defaults
        to 1.

    learning_rate : string, optional
        The learning rate:
        constant: eta = eta0
        optimal: eta = 1.0/(t+t0) [default]
        invscaling: eta = eta0 / pow(t, power_t)

    eta0 : double
        The initial learning rate [default 0.01].

    power_t : double
        The exponent for inverse scaling learning rate [default 0.5].

    class_weight : dict, {class_label : weight} or "auto" or None, optional
        Preset for the class_weight fit parameter.

        Weights associated with classes. If not given, all classes
        are supposed to have weight one.

        The "auto" mode uses the values of y to automatically adjust
        weights inversely proportional to class frequencies.

    warm_start : bool, optional
        When set to True, reuse the solution of the previous call to fit as
        initialization, otherwise, just erase the previous solution.

    Attributes
    ----------
    `coef_` : array, shape = [1, n_features] if n_classes == 2 else [n_classes,
    n_features]
        Weights assigned to the features.

    `intercept_` : array, shape = [1] if n_classes == 2 else [n_classes]
        Constants in decision function.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn import linear_model
    >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
    >>> Y = np.array([1, 1, 2, 2])
    >>> clf = linear_model.SGDClassifier()
    >>> clf.fit(X, Y)
    ... #doctest: +NORMALIZE_WHITESPACE
    SGDClassifier(alpha=0.0001, class_weight=None, epsilon=0.1, eta0=0.0,
            fit_intercept=True, l1_ratio=0.15, learning_rate='optimal',
            loss='hinge', n_iter=5, n_jobs=1, penalty='l2', power_t=0.5,
            random_state=None, shuffle=False,
            verbose=0, warm_start=False)
    >>> print(clf.predict([[-0.8, -1]]))
    [1]

    See also
    --------
    LinearSVC, LogisticRegression, Perceptron

    """

    def __init__(self, loss="hinge", penalty='l2', alpha=0.0001, l1_ratio=0.15,
                 fit_intercept=True, n_iter=5, shuffle=False, verbose=0,
                 epsilon=DEFAULT_EPSILON, n_jobs=1, random_state=None,
                 learning_rate="optimal", eta0=0.0, power_t=0.5,
                 class_weight=None, warm_start=False, seed=None):
        super(SGDClassifier, self).__init__(
            loss=loss, penalty=penalty, alpha=alpha, l1_ratio=l1_ratio,
            fit_intercept=fit_intercept, n_iter=n_iter, shuffle=shuffle,
            verbose=verbose, epsilon=epsilon, n_jobs=n_jobs,
            random_state=random_state, learning_rate=learning_rate, eta0=eta0,
            power_t=power_t, class_weight=class_weight, warm_start=warm_start,
            seed=seed)

    def predict_proba(self, X):
        """Probability estimates.

        Multiclass probability estimates are derived from binary (one-vs.-rest)
        estimates by simple normalization, as recommended by Zadrozny and
        Elkan.

        Binary probability estimates for loss="modified_huber" are given by
        (clip(decision_function(X), -1, 1) + 1) / 2.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]

        Returns
        -------
        array, shape = [n_samples, n_classes]
            Returns the probability of the sample for each class in the model,
            where classes are ordered as they are in `self.classes_`.

        References
        ----------
        Zadrozny and Elkan, "Transforming classifier scores into multiclass
        probability estimates", SIGKDD'02,
        http://www.research.ibm.com/people/z/zadrozny/kdd2002-Transf.pdf

        The justification for the formula in the loss="modified_huber"
        case is in the appendix B in:
        http://jmlr.csail.mit.edu/papers/volume2/zhang02c/zhang02c.pdf
        """
        if self.loss == "log":
            return self._predict_proba_lr(X)

        elif self.loss == "modified_huber":
            binary = (len(self.classes_) == 2)
            scores = self.decision_function(X)

            if binary:
                prob2 = np.ones((scores.shape[0], 2))
                prob = prob2[:, 1]
            else:
                prob = scores

            np.clip(scores, -1, 1, prob)
            prob += 1.
            prob /= 2.

            if binary:
                prob2[:, 0] -= prob
                prob = prob2
            else:
                # the above might assign zero to all classes, which doesn't
                # normalize neatly; work around this to produce uniform
                # probabilities
                prob_sum = prob.sum(axis=1)
                all_zero = (prob_sum == 0)
                if np.any(all_zero):
                    prob[all_zero, :] = 1
                    prob_sum[all_zero] = len(self.classes_)

                # normalize
                prob /= prob_sum.reshape((prob.shape[0], -1))

            return prob

        else:
            raise NotImplementedError("predict_(log_)proba only supported when"
                                      " loss='log' or loss='modified_huber' "
                                      "(%r given)" % self.loss)

    def predict_log_proba(self, X):
        """Log of probability estimates.

        When loss="modified_huber", probability estimates may be hard zeros
        and ones, so taking the logarithm is not possible.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        T : array-like, shape = [n_samples, n_classes]
            Returns the log-probability of the sample for each class in the
            model, where classes are ordered as they are in
            `self.classes_`.
        """
        return np.log(self.predict_proba(X))

class BaseSGDRegressor(BaseSGD, RegressorMixin):

    loss_functions = {
        "squared_loss": (SquaredLoss, ),
        "huber": (Huber, DEFAULT_EPSILON),
        "epsilon_insensitive": (EpsilonInsensitive, DEFAULT_EPSILON),
        "squared_epsilon_insensitive": (SquaredEpsilonInsensitive,
                                        DEFAULT_EPSILON),
    }

    @abstractmethod
    def __init__(self, loss="squared_loss", penalty="l2", alpha=0.0001,
                 l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=False,
                 verbose=0, epsilon=DEFAULT_EPSILON, random_state=None,
                 learning_rate="invscaling", eta0=0.01, power_t=0.25,
                 warm_start=False):
        super(BaseSGDRegressor, self).__init__(loss=loss, penalty=penalty,
                                               alpha=alpha, l1_ratio=l1_ratio,
                                               fit_intercept=fit_intercept,
                                               n_iter=n_iter, shuffle=shuffle,
                                               verbose=verbose,
                                               epsilon=epsilon,
                                               random_state=random_state,
                                               learning_rate=learning_rate,
                                               eta0=eta0, power_t=power_t,
                                               warm_start=False)

    def _partial_fit(self, X, y, alpha, C, loss, learning_rate,
                     n_iter, sample_weight,
                     coef_init, intercept_init):
        X, y = check_arrays(X, y, sparse_format="csr", copy=False,
                            check_ccontiguous=True, dtype=np.float64)
        y = column_or_1d(y, warn=True)

        n_samples, n_features = X.shape
        _check_fit_data(X, y)

        self._validate_params()

        # Allocate datastructures from input arguments
        sample_weight = self._validate_sample_weight(sample_weight, n_samples)

        if self.coef_ is None:
            self._allocate_parameter_mem(1, n_features,
                                         coef_init, intercept_init)

        self._fit_regressor(X, y, alpha, C, loss, learning_rate,
                            sample_weight, n_iter)

        self.t_ += n_iter * n_samples

        return self

    def partial_fit(self, X, y, sample_weight=None):
        """Fit linear model with Stochastic Gradient Descent.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Subset of training data

        y : numpy array of shape [n_samples]
            Subset of target values

        sample_weight : array-like, shape = [n_samples], optional
            Weights applied to individual samples.
            If not provided, uniform weights are assumed.

        Returns
        -------
        self : returns an instance of self.
        """
        return self._partial_fit(X, y, self.alpha, C=1.0,
                                 loss=self.loss,
                                 learning_rate=self.learning_rate, n_iter=1,
                                 sample_weight=sample_weight,
                                 coef_init=None, intercept_init=None)

    def _fit(self, X, y, alpha, C, loss, learning_rate, coef_init=None,
             intercept_init=None, sample_weight=None):
        if self.warm_start and self.coef_ is not None:
            if coef_init is None:
                coef_init = self.coef_
            if intercept_init is None:
                intercept_init = self.intercept_
        else:
            self.coef_ = None
            self.intercept_ = None

        # Clear iteration count for multiple call to fit.
        self.t_ = None

        return self._partial_fit(X, y, alpha, C, loss, learning_rate,
                                 self.n_iter, sample_weight,
                                 coef_init, intercept_init)

    def fit(self, X, y, coef_init=None, intercept_init=None,
            sample_weight=None):
        """Fit linear model with Stochastic Gradient Descent.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training data

        y : numpy array of shape [n_samples]
            Target values

        coef_init : array, shape = [n_features]
            The initial coefficients to warm-start the optimization.

        intercept_init : array, shape = [1]
            The initial intercept to warm-start the optimization.

        sample_weight : array-like, shape = [n_samples], optional
            Weights applied to individual samples (1. for unweighted).

        Returns
        -------
        self : returns an instance of self.
        """
        return self._fit(X, y, alpha=self.alpha, C=1.0,
                         loss=self.loss, learning_rate=self.learning_rate,
                         coef_init=coef_init,
                         intercept_init=intercept_init,
                         sample_weight=sample_weight)

    def decision_function(self, X):
        """Predict using the linear model

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]

        Returns
        -------
        array, shape = [n_samples]
           Predicted target values per element in X.
        """
        X = atleast2d_or_csr(X)
        scores = safe_sparse_dot(X, self.coef_) + self.intercept_
        return scores.ravel()

    def predict(self, X):
        """Predict using the linear model

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]

        Returns
        -------
        array, shape = [n_samples]
           Predicted target values per element in X.
        """
        return self.decision_function(X)

    def _fit_regressor(self, X, y, alpha, C, loss, learning_rate,
                       sample_weight, n_iter):
        dataset, intercept_decay = _make_dataset(X, y, sample_weight)

        loss_function = self._get_loss_function(loss)
        penalty_type = self._get_penalty_type(self.penalty)
        learning_rate_type = self._get_learning_rate_type(learning_rate)

        if self.t_ is None:
            self._init_t(loss_function)

        self.coef_, intercept = plain_sgd(self.coef_,
                                          self.intercept_[0],
                                          loss_function,
                                          penalty_type,
                                          alpha, C,
                                          self.l1_ratio,
                                          dataset,
                                          n_iter,
                                          int(self.fit_intercept),
                                          int(self.verbose),
                                          int(self.shuffle),
                                          self.random_state,
                                          1.0, 1.0,
                                          learning_rate_type,
                                          self.eta0, self.power_t, self.t_,
                                          intercept_decay)

        self.intercept_ = np.atleast_1d(intercept)

class SGDRegressor(BaseSGDRegressor, _LearntSelectorMixin):
    """Linear model fitted by minimizing a regularized empirical loss with SGD

    SGD stands for Stochastic Gradient Descent: the gradient of the loss is
    estimated each sample at a time and the model is updated along the way with
    a decreasing strength schedule (aka learning rate).

    The regularizer is a penalty added to the loss function that shrinks model
    parameters towards the zero vector using either the squared euclidean norm
    L2 or the absolute norm L1 or a combination of both (Elastic Net). If the
    parameter update crosses the 0.0 value because of the regularizer, the
    update is truncated to 0.0 to allow for learning sparse models and achieve
    online feature selection.

    This implementation works with data represented as dense numpy arrays of
    floating point values for the features.

    Parameters
    ----------
    loss : str, 'squared_loss', 'huber', 'epsilon_insensitive', \
                or 'squared_epsilon_insensitive'
        The loss function to be used. Defaults to 'squared_loss' which refers
        to the ordinary least squares fit. 'huber' modifies 'squared_loss' to
        focus less on getting outliers correct by switching from squared to
        linear loss past a distance of epsilon. 'epsilon_insensitive' ignores
        errors less than epsilon and is linear past that; this is the loss
        function used in SVR. 'squared_epsilon_insensitive' is the same but
        becomes squared loss past a tolerance of epsilon.

    penalty : str, 'l2' or 'l1' or 'elasticnet'
        The penalty (aka regularization term) to be used. Defaults to 'l2'
        which is the standard regularizer for linear SVM models. 'l1' and
        'elasticnet' migh bring sparsity to the model (feature selection)
        not achievable with 'l2'.

    alpha : float
        Constant that multiplies the regularization term. Defaults to 0.0001

    l1_ratio : float
        The Elastic Net mixing parameter, with 0 <= l1_ratio <= 1.
        l1_ratio=0 corresponds to L2 penalty, l1_ratio=1 to L1.
        Defaults to 0.15.

    fit_intercept: bool
        Whether the intercept should be estimated or not. If False, the
        data is assumed to be already centered. Defaults to True.

    n_iter: int, optional
        The number of passes over the training data (aka epochs).
        Defaults to 5.

    shuffle: bool, optional
        Whether or not the training data should be shuffled after each epoch.
        Defaults to False.

    random_state: int seed, RandomState instance, or None (default)
        The seed of the pseudo random number generator to use when
        shuffling the data.

    verbose: integer, optional
        The verbosity level.

    epsilon: float
        Epsilon in the epsilon-insensitive loss functions; only if `loss` is
        'huber', 'epsilon_insensitive', or 'squared_epsilon_insensitive'.
        For 'huber', determines the threshold at which it becomes less
        important to get the prediction exactly right.
        For epsilon-insensitive, any differences between the current prediction
        and the correct label are ignored if they are less than this threshold.

    learning_rate : string, optional
        The learning rate:
        constant: eta = eta0
        optimal: eta = 1.0/(t+t0)
        invscaling: eta = eta0 / pow(t, power_t) [default]

    eta0 : double, optional
        The initial learning rate [default 0.01].

    power_t : double, optional
        The exponent for inverse scaling learning rate [default 0.25].

    warm_start : bool, optional
        When set to True, reuse the solution of the previous call to fit as
        initialization, otherwise, just erase the previous solution.

    Attributes
    ----------
    `coef_` : array, shape = [n_features]
        Weights asigned to the features.

    `intercept_` : array, shape = [1]
        The intercept term.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn import linear_model
    >>> n_samples, n_features = 10, 5
    >>> np.random.seed(0)
    >>> y = np.random.randn(n_samples)
    >>> X = np.random.randn(n_samples, n_features)
    >>> clf = linear_model.SGDRegressor()
    >>> clf.fit(X, y)
    SGDRegressor(alpha=0.0001, epsilon=0.1, eta0=0.01, fit_intercept=True,
           l1_ratio=0.15, learning_rate='invscaling', loss='squared_loss',
           n_iter=5, penalty='l2', power_t=0.25, random_state=None,
           shuffle=False, verbose=0, warm_start=False)

    See also
    --------
    Ridge, ElasticNet, Lasso, SVR

    """
    def __init__(self, loss="squared_loss", penalty="l2", alpha=0.0001,
                 l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=False,
                 verbose=0, epsilon=DEFAULT_EPSILON, random_state=None,
                 learning_rate="invscaling", eta0=0.01, power_t=0.25,
                 warm_start=False):
        super(SGDRegressor, self).__init__(loss=loss, penalty=penalty,
                                           alpha=alpha, l1_ratio=l1_ratio,
                                           fit_intercept=fit_intercept,
                                           n_iter=n_iter, shuffle=shuffle,
                                           verbose=verbose,
                                           epsilon=epsilon,
                                           random_state=random_state,
                                           learning_rate=learning_rate,
                                           eta0=eta0, power_t=power_t,
                                           warm_start=False)


text:
<?php

namespace Dafiti\Correios\Facade;

use Dafiti\Correios\Entity;

/**
 * Este método retorna uma faixa de numeração de autorização de postagem (e-
 * ticket) a ser gerenciada no sistema proprietário para o serviço de logística
 * reversa. Para cada número se faz necessário calcular o dígito verificador,
 * podendo ser consumido o método calcularDigitoVerificador() ou ainda a sua
 * implementação local, conforme exemplo * do Anexo 07.
 * Recomendado utilizar este método apenas de forma contigencial.
 *
 * @uses FacadeInterface
 *
 * @author Flávio Briz <flavio.briz@dafiti.com.br>
 *'@license MIT
 */
class SolicitarRange extends FacadeInterface
{
    protected $rules = [
        [
            ['tipo'], 'Constant', ['values' => ['AP', 'LR', 'LE', 'LS', 'LV']],
        ], [
            ['usuario', 'senha', 'codAdministrativo', 'contrato', 'tipo', 'quantidade'], 'Mandatory', [],
        ],
    ];

    public function __construct($adapter, $tipo = null, $servico = null, $quantidade = 1)
    {
        $cfg = $adapter->getConfig();
        $request = new Entity\RequestObject([
            'usuario' => $cfg->getUsuario(),
            'senha' => $cfg->getSenha(),
            'codAdministrativo' => $cfg->getCodAdministrativo(),
            'contrato' => $cfg->getContrato(),
            'tipo' => $tipo,
            'servico' => $servico,
            'quantidade' => $quantidade,
        ]);

        parent::__construct($adapter, $request);
    }

    public function call()
    {
        return $this->getAdapter()->call(
            'solicitarRange',
            $this->getRequest()
        );
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright 2017 Jiaheng Ge

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->
<resources>

    <dimen name="list_item_content_padding_horizontal">16dp</dimen>
    <dimen name="list_item_content_padding_vertical">12dp</dimen>
    <dimen name="list_item_elevation">2dp</dimen>

    <dimen name="login_sunegg_margin_top">48dp</dimen>
    <dimen name="login_sunegg_margin_bottom">-64dp</dimen>
    <dimen name="login_title_margin_top">24dp</dimen>
    <dimen name="login_button_margin_top">24dp</dimen>
    <dimen name="login_button_margin_bottom">24dp</dimen>

    <dimen name="avatar_size_24dp">24dp</dimen>
    <dimen name="avatar_size_32dp">32dp</dimen>
    <dimen name="avatar_size_96dp">96dp</dimen>

    <dimen name="text_size_xxl">24sp</dimen>
    <dimen name="text_size_xl">20sp</dimen>
    <dimen name="text_size_l">18sp</dimen>
    <dimen name="text_size_m">16sp</dimen>
    <dimen name="text_size_s">14sp</dimen>
    <dimen name="text_size_xs">12sp</dimen>

    <item name="line_spacing_multiplier" format="float" type="dimen">1.2</item>

    <dimen name="touch_raise">9dp</dimen>
    <dimen name="card_elevation">2dp</dimen>
    <dimen name="about_header_padding">32dp</dimen>
    <dimen name="more_list_horizontal_inset">8dp</dimen>
    <dimen name="more_list_vertical_inset">16dp</dimen>

</resources>


text:
/**
 * This file is part of the CernVM File System.
 */

#include "swissknife_lease.h"

#include <algorithm>
#include <vector>

#include "gateway_util.h"
#include "swissknife_lease_curl.h"
#include "swissknife_lease_json.h"
#include "util/logging.h"
#include "util/posix.h"
#include "util/string.h"

namespace {

bool CheckParams(const swissknife::CommandLease::Parameters& p) {
  if (p.action != "acquire" && p.action != "drop") {
    return false;
  }

  return true;
}

}  // namespace

namespace swissknife {

enum LeaseError {
  kLeaseSuccess,
  kLeaseBusy,
  kLeaseFailure,
  kLeaseParamError,
  kLeaseKeyParseError,
  kLeaseCurlInitError,
  kLeaseFileOpenError,
  kLeaseCurlReqError,
};

CommandLease::~CommandLease() {}

ParameterList CommandLease::GetParams() const {
  ParameterList r;
  r.push_back(Parameter::Mandatory('u', "repo service url"));
  r.push_back(Parameter::Mandatory('a', "action (acquire or drop)"));
  r.push_back(Parameter::Mandatory('k', "key file"));
  r.push_back(Parameter::Mandatory('p', "lease path"));
  return r;
}

int CommandLease::Main(const ArgumentList& args) {
  Parameters params;

  params.repo_service_url = *(args.find('u')->second);
  params.action = *(args.find('a')->second);
  params.key_file = *(args.find('k')->second);

  params.lease_path = *(args.find('p')->second);
  std::vector<std::string> tokens = SplitString(params.lease_path, '/');
  const std::string lease_fqdn = tokens.front();

  if (!CheckParams(params)) {
    return kLeaseParamError;
  }

  // Initialize curl
  if (curl_global_init(CURL_GLOBAL_ALL)) {
    return kLeaseCurlInitError;
  }

  std::string key_id;
  std::string secret;
  if (!gateway::ReadKeys(params.key_file, &key_id, &secret)) {
    LogCvmfs(kLogCvmfs, kLogStderr, "Error reading key file %s.",
             params.key_file.c_str());
    return kLeaseKeyParseError;
  }

  LeaseError ret = kLeaseSuccess;
  if (params.action == "acquire") {
    CurlBuffer buffer;
    if (MakeAcquireRequest(key_id, secret, params.lease_path,
                           params.repo_service_url, &buffer)) {
      std::string session_token;
      LeaseReply rep = ParseAcquireReply(buffer, &session_token);
      switch (rep) {
        case kLeaseReplySuccess:
          {
            const std::string token_file_name =
              "/var/spool/cvmfs/" + lease_fqdn + "/session_token";

            if (!SafeWriteToFile(session_token, token_file_name, 0600)) {
              LogCvmfs(kLogCvmfs, kLogStderr, "Error opening file: %s",
                  std::strerror(errno));
              ret = kLeaseFileOpenError;
            }
          }
          break;
        case kLeaseReplyBusy:
          return kLeaseBusy;
          break;
        case kLeaseReplyFailure:
        default:
          return kLeaseFailure;
      }
    } else {
      ret = kLeaseCurlReqError;
    }
  } else if (params.action == "drop") {
    // Try to read session token from repository scratch directory
    std::string session_token;
    std::string token_file_name =
        "/var/spool/cvmfs/" + lease_fqdn + "/session_token";
    FILE* token_file = std::fopen(token_file_name.c_str(), "r");
    if (token_file) {
      GetLineFile(token_file, &session_token);
      LogCvmfs(kLogCvmfs, kLogDebug, "Read session token from file: %s",
               session_token.c_str());

      CurlBuffer buffer;
      if (MakeEndRequest("DELETE", key_id, secret, session_token,
                         params.repo_service_url, "", &buffer)) {
        if (kLeaseReplySuccess == ParseDropReply(buffer)) {
          std::fclose(token_file);
          if (unlink(token_file_name.c_str())) {
            LogCvmfs(kLogCvmfs, kLogStderr,
                     "Warning - Could not delete session token file.");
          }
          return kLeaseSuccess;
        } else {
          LogCvmfs(kLogCvmfs, kLogStderr, "Could not drop active lease");
          ret = kLeaseFailure;
        }
      } else {
        LogCvmfs(kLogCvmfs, kLogStderr, "Error making DELETE request");
        ret = kLeaseCurlReqError;
      }

      std::fclose(token_file);
    } else {
      LogCvmfs(kLogCvmfs, kLogStderr, "Error reading session token from file");
      ret = kLeaseFileOpenError;
    }
  }

  return ret;
}

}  // namespace swissknife


text:
from __future__ import unicode_literals

import requests
from django.conf import settings
from django.db import models
from django.http import QueryDict
from six.moves.urllib.parse import unquote_plus

from paypal.standard.conf import POSTBACK_ENDPOINT, SANDBOX_POSTBACK_ENDPOINT
from paypal.standard.models import PayPalStandardBase

# ### Todo: Move this logic to conf.py:
# if paypal.standard.pdt is in installed apps
# ... then check for this setting in conf.py
class PayPalSettingsError(Exception):
    """Raised when settings are incorrect."""

try:
    IDENTITY_TOKEN = settings.PAYPAL_IDENTITY_TOKEN
except:
    raise PayPalSettingsError(
        "You must set PAYPAL_IDENTITY_TOKEN in settings.py. Get this token by enabling PDT in your PayPal account.")

class PayPalPDT(PayPalStandardBase):
    format = u"<PDT: %s %s>"

    amt = models.DecimalField(max_digits=64, decimal_places=2, default=0, blank=True, null=True)
    cm = models.CharField(max_length=255, blank=True)
    sig = models.CharField(max_length=255, blank=True)
    tx = models.CharField(max_length=255, blank=True)
    st = models.CharField(max_length=32, blank=True)

    class Meta:
        db_table = "paypal_pdt"
        verbose_name = "PayPal PDT"

    def _postback(self):
        """
        Perform PayPal PDT Postback validation.
        Sends the transaction ID and business token to PayPal which responses with
        SUCCESS or FAILED.

        """
        return requests.post(self.get_endpoint(),
                             data=dict(cmd="_notify-synch", at=IDENTITY_TOKEN, tx=self.tx)).content

    def get_endpoint(self):
        if getattr(settings, 'PAYPAL_TEST', True):
            return SANDBOX_POSTBACK_ENDPOINT
        else:
            return POSTBACK_ENDPOINT

    def _verify_postback(self):
        # ### Now we don't really care what result was, just whether a flag was set or not.
        from paypal.standard.pdt.forms import PayPalPDTForm

        response_list = self.response.split('\n')
        response_dict = {}
        for i, line in enumerate(response_list):
            unquoted_line = unquote_plus(line).strip()
            if i == 0:
                self.st = unquoted_line
            else:
                if self.st != "SUCCESS":
                    self.set_flag(line)
                    break
                try:
                    if not unquoted_line.startswith(' -'):
                        k, v = unquoted_line.split('=')
                        response_dict[k.strip()] = v.strip()
                except ValueError:
                    pass

        qd = QueryDict('', mutable=True)
        qd.update(response_dict)
        qd.update(dict(ipaddress=self.ipaddress, st=self.st, flag_info=self.flag_info, flag=self.flag,
                       flag_code=self.flag_code))
        pdt_form = PayPalPDTForm(qd, instance=self)
        pdt_form.save(commit=False)

    def __repr__(self):
        return '<PayPalPDT id:{0}>'.format(self.id)

    def __str__(self):
        return "PayPalPDT: {0}".format(self.id)


text:
=head1 NAME

Bio::AlignIO::prodom - prodom sequence input/output stream

=head1 SYNOPSIS

Do not use this module directly.  Use it via the L<Bio::AlignIO> class.

=head1 DESCRIPTION

This object can transform L<Bio::Align::AlignI> objects to and from prodom flat
file databases.

=head1 FEEDBACK

=head2 Support

Please direct usage questions or support issues to the mailing list:

I<bioperl-l@bioperl.org>

rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.

=head2 Reporting Bugs

Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution.  Bug reports can be submitted via the
web:

  https://redmine.open-bio.org/projects/bioperl/

=head1 AUTHORS - Peter Schattner

Email: schattner@alum.mit.edu

=head1 APPENDIX

The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _

=cut

# Let the code begin...

package Bio::AlignIO::prodom;
use strict;

use base qw(Bio::AlignIO);

=head2 next_aln

 Title   : next_aln
 Usage   : $aln = $stream->next_aln()
 Function: returns the next alignment in the stream.
 Returns : L<Bio::Align::AlignI> object
 Args    : NONE

=cut

sub next_aln {
    my $self = shift;
    my $entry;
    my ($acc, $fake_id, $start, $end, $seq, $add, %names);

    my $aln =  Bio::SimpleAlign->new(-source => 'prodom');

    while( $entry = $self->_readline) {

       if ($entry =~ /^AC\s+(\S+)\s*$/) {         #ps 9/12/00
	   $aln->id( $1 );
       }
       elsif ($entry =~ /^AL\s+(\S+)\|(\S+)\s+(\d+)\s+(\d+)\s+\S+\s+(\S+)\s*$/){    #ps 9/12/00
	   $acc=$1;
	   $fake_id=$2;  # Accessions have _species appended
	   $start=$3;
	   $end=$4;
	   $seq=$5;

	   $names{'fake_id'} = $fake_id;

	   $add = Bio::LocatableSeq->new('-seq'      => $seq,
					 '-id'       => $acc,
					 '-start'    => $start,
					 '-end'      => $end,
					 '-alphabet' => $self->alphabet,
			       );

	   $aln->add_seq($add);
       }
       elsif ($entry =~ /^CO/) {
	   # the consensus line marks the end of the alignment part of the entry
	   last;
       }
	}

   return $aln if $aln->num_sequences;
   return;
}

=head2 write_aln

 Title   : write_aln
 Usage   : $stream->write_aln(@aln)
 Function: writes the $aln object into the stream in prodom format  ###Not yet implemented!###
 Returns : 1 for success and 0 for error
 Args    : L<Bio::Align::AlignI> object

=cut

sub write_aln {
    my ($self,@aln) = @_;
    $self->throw_not_implemented();
}

1;


text:
A broadcaster and receiver for COFA computers

![A grid of screens observing a supernova](http://pulsar.dermah.org/supernova.jpg)

## What?

This is a bunch of scripts that was used to more easily run [PULSAR](http://pulsar.dermah.org) on computers at COFA (UNSW Art & Design). Basically, client scripts wait for a signal from the master script, upon which they run a Terminal command. Upon a second signal, the client script kills the terminal command and waits for more signals.

These scripts are very much tailored to run on COFA computers. I don't recommend using them without severe modifications.

In more detail here's what happens:

- A bootstrap script asks for the client computer's position in a screen array (Column and Row)
- The bootstrap sctipt downloads and extracts [node](http://nodejs.org)
- The bootstrap script runs the `observer.js` node script
- `observer.js` waits for a UDP packet from the master `supernova.js` script
- When it recieves a packet, the observer launches chrome. The next time it recieves a packet, it kills chrome.

## How?

Steps to get this up and running at COFA:

- Put `supernova.app` on your Desktop for easy access when logging in to a computer.
  - It also helps to make this app a startup item so that it runs as soon as you log in
- Copy `node-bootstrap.sh` to your home directory (i.e. the `~` directory)
  - Comment out lines 9 to 14 if you don't need to provide arguments to the observer script
- Copy `observer.js` to your home directory (i.e. the `~` directory)
  - You can change line 47 to be whatever terminal command or script that you want.
- Log in to all your client computers and run `supernova.app`
- Run the app on your master computer but kill the process after it starts observing.
- On the master computer, run `./node supernova.js {url}` where `{url}` is the URL of where you want all the computers to look when a supernova occurs.
- Press any key to open chrome on all computers. Press any key to kill chrome everywhere. Press Control-C to kill `supernova.js` everywhere


text:
<?php

class Upgrade_3_0_13 extends MIDASUpgrade
{
  public function preUpgrade()
    {
    $this->addTableField('metadatadocumentvalue', 'metadatavalue_id', 'bigint(20)', 'serial',false);
    $this->addTableField('metadatavalue', 'metadatavalue_id', 'bigint(20)', 'serial',false);
    }

  public function mysql()
    {
    $this->addTablePrimaryKey('metadatadocumentvalue', 'metadatavalue_id');
    $this->addTablePrimaryKey('metadatavalue', 'metadatavalue_id');
    $this->db->query("ALTER TABLE `metadatadocumentvalue` CHANGE `metadatavalue_id`
                      `metadatavalue_id` BIGINT( 20 ) NOT NULL AUTO_INCREMENT");
    $this->db->query("ALTER TABLE `metadatavalue` CHANGE `metadatavalue_id`
                      `metadatavalue_id` BIGINT( 20 ) NOT NULL AUTO_INCREMENT");
    }

  public function pgsql()
    {
    }

  public function postUpgrade()
    {
    }
}
?>


text:
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"net/http"
	"strings"

	corev2 "github.com/sensu/sensu-go/api/core/v2"
	"github.com/sensu/sensu-plugins-go-library/sensu"
)
// HandlerConfig represents plugin configuration settings.
type HandlerConfig struct {
	sensu.PluginConfig
	AlertaEndpoint       string
	AlertaAPIKey         string
	Environment          string
}

const (
	endpointURL = "endpoint-url"
	apiKey      = "api-key"
	environment = "environment"

	defaultEndpointURL string = "http://localhost:8080"
)

var (
	config = HandlerConfig{
		PluginConfig: sensu.PluginConfig{
			Name:     "sensu-alerta-handler",
			Short:    "The Sensu Go Alerta handler for event forwarding",
			Timeout:  10,
			Keyspace: "sensu.io/plugins/alerta/config",
		},
	}

	alertaConfigOptions = []*sensu.PluginConfigOption{
		{
			Path:      endpointURL,
			Env:       "ALERTA_ENDPOINT",
			Argument:  endpointURL,
			Shorthand: "",
			Default:   defaultEndpointURL,
			Usage:     "API endpoint URL",
			Value:     &config.AlertaEndpoint,
		},
		{
			Path:      apiKey,
			Env:       "ALERTA_API_KEY",
			Argument:  apiKey,
			Shorthand: "K",
			Default:   "",
			Usage:     "API key for authenticated access",
			Value:     &config.AlertaAPIKey,
		},
		{
			Path:      environment,
			Argument:  environment,
			Shorthand: "E",
			Default:   "",
			Usage:     "Environment eg. Production, Development",
			Value:     &config.Environment,
		},
	}
)

func main() {
  goHandler := sensu.NewGoHandler(&config.PluginConfig, alertaConfigOptions, checkArgs, sendAlert)
  goHandler.Execute()
}

func checkArgs(event *corev2.Event) error {
	if !event.HasCheck() {
		return fmt.Errorf("event does not contain check")
	}
	return nil
}

// Alert represents an event to be sent to Alerta.
type Alert struct {
	Resource    string `json:"resource"`
	Event       string `json:"event"`
	Environment string `json:"environment"`
	Severity    string `json:"severity"`
	Correlate   []string `json:"correlate,omitempty"`
	Status      string `json:"status"`
	Service     []string `json:"service"`
	Group       string `json:"group"`
	Value       string `json:"value"`
	Text        string `json:"text"`
	Tags        []string `json:"tags,omitempty"`
	Attributes  map[string]string `json:"attributes,omitempty"`
	Origin      string `json:"origin"`
	Type        string `json:"type"`
	CreateTime  string `json:"createTime,omitempty"`
	Timeout     int `json:"timeout"`
	RawData     string `json:"rawData"`
}

// TODO(satterly) could make these severity lookups configurable

func eventSeverity(event *corev2.Event) string {
	switch event.Check.Status {
	case 0:
		return "normal"
	case 2:
		return "critical"
	default:
		return "warning"
	}
}

func sendAlert(event *corev2.Event) error {
	environment := config.Environment
	if environment == "" {
		environment = event.Entity.Namespace
	}

	var attributes = make(map[string]string)
	// Alerta does not allow periods or dollar symbols in attribute names
	var replacer = strings.NewReplacer(".", "_", "$", "_")
	for key, value := range event.Entity.Labels {
		attributes[replacer.Replace(key)] = value
	}
	for key, value := range event.Check.Labels {
		if _, ok := attributes[replacer.Replace(key)]; ok {
			attributes[replacer.Replace(key)] = attributes[replacer.Replace(key)] + "/" + value
		} else {
			attributes[replacer.Replace(key)] = value
		}
	}

	hostname, _ := os.Hostname()
	data := &Alert{
		Resource: event.Entity.Name,
		Event: event.Check.Name,
		Environment: environment,
		Severity: eventSeverity(event),
		Service: []string{"Sensu"},
		Group: event.Check.Namespace,
		Value: event.Check.State,
		Text: event.Check.Output,
		Attributes: attributes,
		Origin: fmt.Sprintf("sensu-go/%s", hostname),
		Type: "sensuAlert",
		RawData: event.Entity.String(),
	}

	buf := new(bytes.Buffer)
	err := json.NewEncoder(buf).Encode(data)
	if err != nil {
		return err
	}
	url := fmt.Sprintf("%s/alert?api-key=%s", config.AlertaEndpoint, config.AlertaAPIKey)

	resp, err := http.Post(url, "application/json", buf)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	_, _ = fmt.Fprintf(os.Stdout, "response: %s\n", body)

	var r map[string]interface{}
	err = json.Unmarshal(body, &r)
	if err != nil {
		return err
	}
	return nil
}


text:
package net.metadata.dataspace.data.sequencer;

import net.metadata.dataspace.data.access.FullNameDao;
import net.metadata.dataspace.data.model.context.FullName;
import net.metadata.dataspace.util.DaoHelper;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Author: alabri
 * Date: 15/04/11
 * Time: 4:56 PM
 */
public class FullNameSequencer extends AbstractAtomicSquencer {

    public FullNameSequencer(FullNameDao fullNameDao) {
        FullName fullName = fullNameDao.getMostRecentInserted();
        if (fullName == null) {
            atomicInteger = new AtomicInteger(0);
        } else {
            //fullNameDao.refresh(fullName);
            final int BASE_THIRTY_ONE = 31;
            String uriKey = fullName.getUriKey();
            atomicInteger = new AtomicInteger(DaoHelper.fromOtherBaseToDecimal(BASE_THIRTY_ONE, uriKey));
        }
    }

}