working on it ...

Filters

Explore Public Snippets

Sort by

Found 80k snippets

    public by lmontealegre modified Aug 22, 2017  97  0  2  0

    CONSTANT Definitions for Database server and SQL scripts

    Standard for defining connections and SQL definitions
    #=================================================================
    # Database Server Connection String
    # Database = AGENTS  Server = <IP>
    #=================================================================
    CNN_AGENTS = {'Server':'<IP>', 'User':'<Name>', 'Password':'<Pwd>', 'Database':'<DBName>'}
    
    #=================================================================
    # SQL Script Definition to Get Gran Total Agent counts
    # Database = AGENTS  Server = <IP>
    #=================================================================
    SQL_WR0TOTALCNTS = '''
    Select
      Count(SOL.SOL_KEY) As NoRec,
      Count(Distinct SOL.AGT_SOL_NO) As CntSOL,
      Sum(SOL.AGT_SOL_NOFILE) As SumSOL_NF,
      Sum(SOL.AGT_SOL_DWLERR) As SumSOL_DWLERR,
      Sum(SOL.AGT_SOL_OK) As SumSOL_OK,
      Sum(SOL.AGT_SOL_DWLOK) As SumSOL_DWLOK,
      Min(Cast(SOL.AGT_DTE_POSTDATE As datetime)) As Min_POSTDATE,
      Max(Cast(SOL.AGT_DTE_POSTDATE As datetime)) As Max_POSTDATE,
      DateDiff(day, Min(Cast(SOL.AGT_DTE_POSTDATE As datetime)), Max(Cast(SOL.AGT_DTE_POSTDATE As datetime))) As DateRange,
      Count(Distinct Cast(SOL.AGT_DTE_POSTDATE As datetime)) As ProcDates,
      Sum(Case When Len(IsNull(SOL.AGT_SOL_DWLPATH, '')) > 0 Then 1 Else 0
      End) As DwlOK,
      Sum((Convert(bigint,SOL.AGT_SOL_DWLBYTE)) / (1048576)) As MB,
      Count(Distinct SOL.AGT_DTE_EVT) As Cnt_RAN_EVT
    From
      AGENTS.dbo.DIBBS_AGN_SOL SOL
    '''
    
    #=================================================================
    # SQL Script Definition to Get Gran Total Agent counts
    # Database = AGENTS  Server = <IP> To be designed
    #=================================================================
    SQL_SelectedDates = '''
    Select
      Count(SOL.SOL_KEY) As NoRec,
      Count(Distinct SOL.AGT_SOL_NO) As CntSOL,
      Sum(SOL.AGT_SOL_NOFILE) As SumSOL_NF,
      Sum(SOL.AGT_SOL_DWLERR) As SumSOL_DWLERR,
      Sum(SOL.AGT_SOL_OK) As SumSOL_OK,
      Sum(SOL.AGT_SOL_DWLOK) As SumSOL_DWLOK,
      Min(Cast(SOL.AGT_DTE_POSTDATE As datetime)) As Min_POSTDATE,
      Max(Cast(SOL.AGT_DTE_POSTDATE As datetime)) As Max_POSTDATE,
      DateDiff(day, Min(Cast(SOL.AGT_DTE_POSTDATE As datetime)), Max(Cast(SOL.AGT_DTE_POSTDATE As datetime))) As DateRange,
      Count(Distinct Cast(SOL.AGT_DTE_POSTDATE As datetime)) As ProcDates,
      Sum(Case When Len(IsNull(SOL.AGT_SOL_DWLPATH, '')) > 0 Then 1 Else 0
      End) As DwlOK,
      Sum((Convert(bigint,SOL.AGT_SOL_DWLBYTE)) / (1048576)) As MB,
      Count(Distinct SOL.AGT_DTE_EVT) As Cnt_RAN_EVT
    From
      AGENTS.dbo.DIBBS_AGN_SOL SOL
    '''

    public by snip2code modified Aug 13, 2017  51  0  2  0

    First Snippet: How to play with Snip2Code

    This is the first example of a snippet: - the title represents in few words which is the exact issue the snippet resolves; it can be something like the name of a method; - the description (this field) is an optional field where you can add interesting information regarding the snippet; something like the comment on the head of a method; - the c
    /* place here the actual content of your snippet. 
       It should be code or pseudo-code. 
       The less dependencies from external stuff, the better! */

    public by ozmartian modified Dec 18, 2016  2035  9  4  0

    Python 3 docstring

    w/ UTF-8 encoding
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-                        

    public by marceloviana modified Nov 10, 2016  2340  1  4  0

    Resolver problema de acentuação no python

    
    import sys 
    reload(sys)
    sys.setdefaultencoding('utf-8')
    

    public by marceloviana modified Oct 4, 2016  3366  3  4  1

    Python acessando banco de dados SQL Server

    ----------------INSTALAR--------------------------
    apt-get install python freetds-common
    apt-get install python-pymssql
    
    apt-get purge python-pymssql
    apt-get install freetds-dev
    pip install Cython
    pip install pymssql
    
    --------------------TESTE.PY------------------------------
    import pymssql
    
    conn=pymssql.connect(host='xx.xx.xx.xx', user='sa', password='xxxxxxxx', database='teste')
    cur=conn.cursor()	
    cur.execute("SELECT  nomes from usuarios")
    
    for row in cur:
    	print row
    
    conn.commit()	
    conn.close()
    
    	
    

    public by te777 modified Sep 24, 2016  3078  3  5  0

    Hackerrank Count Strings solution python

    Hackerrank Count Strings solution python
    '''
    Created on Aug 7, 2016
    
    AutomataTheory.py
    
    @author: Siddharthasahu @ https://github.com/siddharthasahu/automata-from-regex
    
    Automata build from from regex to DFA taken from there. Count Strings (Matrix class) added
    '''
    # You can test with input " 1 ((((ab)|a)*)|(((aa)|(bb))*)) 368046388 "
    
    #The result of the code executed in main should be 198110437 with the test input
    
    #import fileinput
    #from os import popen
    #import time
    #import sys, traceback
    
    class Automata:
        """class to represent an Automata"""
    
        def __init__(self, language = set(['a', 'b'])):
            self.states = set()
            self.startstate = None
            self.finalstates = []
            self.transitions = dict()
            self.language = language
    
        @staticmethod
        def epsilon():
            return ":e:"
    
        def setstartstate(self, state):
            self.startstate = state
            self.states.add(state)
    
        def addfinalstates(self, state):
            if isinstance(state, int):
                state = [state]
            for s in state:
                if s not in self.finalstates:
                    self.finalstates.append(s)
    
        def addtransition(self, fromstate, tostate, inp):
            if isinstance(inp, str):
                inp = set([inp])
            self.states.add(fromstate)
            self.states.add(tostate)
            if fromstate in self.transitions:
                if tostate in self.transitions[fromstate]:
                    self.transitions[fromstate][tostate] = self.transitions[fromstate][tostate].union(inp)
                else:
                    self.transitions[fromstate][tostate] = inp
            else:
                self.transitions[fromstate] = {tostate : inp}
    
        def addtransition_dict(self, transitions):
            for fromstate, tostates in transitions.items():
                for state in tostates:
                    self.addtransition(fromstate, state, tostates[state])
    
        def gettransitions(self, state, key):
            if isinstance(state, int):
                state = [state]
            trstates = set()
            for st in state:
                if st in self.transitions:
                    for tns in self.transitions[st]:
                        if key in self.transitions[st][tns]:
                            trstates.add(tns)
            return trstates
    
        def getEClose(self, findstate):
            allstates = set()
            states = set([findstate])
            while len(states)!= 0:
                state = states.pop()
                allstates.add(state)
                if state in self.transitions:
                    for tns in self.transitions[state]:
                        if Automata.epsilon() in self.transitions[state][tns] and tns not in allstates:
                            states.add(tns)
            return allstates
    
        def display(self):
            print ("states:", self.states)
            print ("start state: ", self.startstate)
            print ("final states:", self.finalstates)
            print ("transitions:")
            for fromstate, tostates in self.transitions.items():
                for state in tostates:
                    for char in tostates[state]:
                        print ("  ",fromstate, "->", state, "on '"+char+"'",)
                print
    
        def getPrintText(self):
            text = "language: {" + ", ".join(self.language) + "}\n"
            text += "states: {" + ", ".join(map(str,self.states)) + "}\n"
            text += "start state: " + str(self.startstate) + "\n"
            text += "final states: {" + ", ".join(map(str,self.finalstates)) + "}\n"
            text += "transitions:\n"
            linecount = 5
            for fromstate, tostates in self.transitions.items():
                for state in tostates:
                    for char in tostates[state]:
                        text += "    " + str(fromstate) + " -> " + str(state) + " on '" + char + "'\n"
                        linecount +=1
            return [text, linecount]
    
        def newBuildFromNumber(self, startnum):
            translations = {}
            for i in list(self.states):
                translations[i] = startnum
                startnum += 1
            rebuild = Automata(self.language)
            rebuild.setstartstate(translations[self.startstate])
            rebuild.addfinalstates(translations[self.finalstates[0]])
            for fromstate, tostates in self.transitions.items():
                for state in tostates:
                    rebuild.addtransition(translations[fromstate], translations[state], tostates[state])
            return [rebuild, startnum]
    
        def newBuildFromEquivalentStates(self, equivalent, pos):
            # Added "sorted" next line
            rebuild = Automata(sorted(self.language))
            for fromstate, tostates in self.transitions.items():
                for state in tostates:
                    rebuild.addtransition(pos[fromstate], pos[state], tostates[state])
            rebuild.setstartstate(pos[self.startstate])
            for s in self.finalstates:
                rebuild.addfinalstates(pos[s])
            return rebuild
    
        def getDotFile(self):
            dotFile = "digraph DFA {\nrankdir=LR\n"
            if len(self.states) != 0:
                dotFile += "root=s1\nstart [shape=point]\nstart->s%d\n" % self.startstate
                for state in self.states:
                    if state in self.finalstates:
                        dotFile += "s%d [shape=doublecircle]\n" % state
                    else:
                        dotFile += "s%d [shape=circle]\n" % state
                for fromstate, tostates in self.transitions.items():
                    for state in tostates:
                        for char in tostates[state]:
                            dotFile += 's%d->s%d [label="%s"]\n' % (fromstate, state, char)
            dotFile += "}"
            return dotFile
    
    class BuildAutomata:
        """class for building e-nfa basic structures"""
    
        @staticmethod
        def basicstruct(inp):
            state1 = 1
            state2 = 2
            basic = Automata()
            basic.setstartstate(state1)
            basic.addfinalstates(state2)
            basic.addtransition(1, 2, inp)
            return basic
    
        @staticmethod
        def plusstruct(a, b):
            [a, m1] = a.newBuildFromNumber(2)
            [b, m2] = b.newBuildFromNumber(m1)
            state1 = 1
            state2 = m2
            plus = Automata()
            plus.setstartstate(state1)
            plus.addfinalstates(state2)
            plus.addtransition(plus.startstate, a.startstate, Automata.epsilon())
            plus.addtransition(plus.startstate, b.startstate, Automata.epsilon())
            plus.addtransition(a.finalstates[0], plus.finalstates[0], Automata.epsilon())
            plus.addtransition(b.finalstates[0], plus.finalstates[0], Automata.epsilon())
            plus.addtransition_dict(a.transitions)
            plus.addtransition_dict(b.transitions)
            return plus
    
        @staticmethod
        def dotstruct(a, b):
            [a, m1] = a.newBuildFromNumber(1)
            [b, m2] = b.newBuildFromNumber(m1)
            state1 = 1
            state2 = m2-1
            dot = Automata()
            dot.setstartstate(state1)
            dot.addfinalstates(state2)
            dot.addtransition(a.finalstates[0], b.startstate, Automata.epsilon())
            dot.addtransition_dict(a.transitions)
            dot.addtransition_dict(b.transitions)
            return dot
    
        @staticmethod
        def starstruct(a):
            [a, m1] = a.newBuildFromNumber(2)
            state1 = 1
            state2 = m1
            star = Automata()
            star.setstartstate(state1)
            star.addfinalstates(state2)
            star.addtransition(star.startstate, a.startstate, Automata.epsilon())
            star.addtransition(star.startstate, star.finalstates[0], Automata.epsilon())
            star.addtransition(a.finalstates[0], star.finalstates[0], Automata.epsilon())
            star.addtransition(a.finalstates[0], a.startstate, Automata.epsilon())
            star.addtransition_dict(a.transitions)
            return star
    
    
    class DFAfromNFA:
        """class for building dfa from e-nfa and minimise it"""
    
        def __init__(self, nfa):
            self.buildDFA(nfa)
            self.minimise()
    
        def getDFA(self):
            return self.dfa
        
        def getAllStates(self):
            return self.allstates
        
        def getTransitions(self):
            return self.trstates
    
        def getMinimisedDFA(self):
            return self.minDFA
    
        def displayDFA(self):
            self.dfa.display()
    
        def displayMinimisedDFA(self):
            self.minDFA.display()
    
        def buildDFA(self, nfa):
            allstates = dict()
            eclose = dict()
            count = 1
            state1 = nfa.getEClose(nfa.startstate)
            eclose[nfa.startstate] = state1
            dfa = Automata(nfa.language)
            dfa.setstartstate(count)
            states = [[state1, count]]
            allstates[count] = state1
            count +=  1
            while len(states) != 0:
                [state, fromindex] = states.pop()
                # Next line was originally "for char in dfa.language:"
                # But returned "char" in random order, since dfa.language is a set,
                # so dfa.finalstates result was not consistent
                for char in sorted(dfa.language):
                    trstates = nfa.gettransitions(state, char)
                    for s in list(trstates)[:]:
                        if s not in eclose:
                            eclose[s] = nfa.getEClose(s)
                        trstates = trstates.union(eclose[s])
                    if len(trstates) != 0:
                        if trstates not in allstates.values():
                            states.append([trstates, count])
                            allstates[count] = trstates
                            toindex = count
                            count +=  1
                        else:
                            toindex = [k for k, v in allstates.items() if v  ==  trstates][0]
                        dfa.addtransition(fromindex, toindex, char)
       
            for value, state in allstates.items():
                if nfa.finalstates[0] in state:
                    dfa.addfinalstates(value)
     
            self.dfa = dfa
            self.allstates = allstates
            self.trstates = trstates 
    
        def acceptsString(self, string):
            currentstate = self.dfa.startstate
            for ch in string:
                if ch==":e:":
                    continue
                st = list(self.dfa.gettransitions(currentstate, ch))
                if len(st) == 0:
                    return False
                currentstate = st[0]
            if currentstate in self.dfa.finalstates:
                return True
            return False
    
        def minimise(self):
            states = list(self.dfa.states)
            n = len(states)
            unchecked = dict()
            count = 1
            distinguished = []
            equivalent = dict(zip(range(len(states)), [{s} for s in states]))
            pos = dict(zip(states,range(len(states))))
            for i in range(n-1):
                for j in range(i+1, n):
                    if not ([states[i], states[j]] in distinguished or [states[j], states[i]] in distinguished):
                        eq = 1
                        toappend = []
                        # Added "sorted" next line
                        for char in sorted(self.dfa.language):
                            s1 = self.dfa.gettransitions(states[i], char)
                            s2 = self.dfa.gettransitions(states[j], char)
                            if len(s1) != len(s2):
                                eq = 0
                                break
                            if len(s1) > 1:
                                raise BaseException("Multiple transitions detected in DFA")
                            elif len(s1) == 0:
                                continue
                            s1 = s1.pop()
                            s2 = s2.pop()
                            if s1 != s2:
                                if [s1, s2] in distinguished or [s2, s1] in distinguished:
                                    eq = 0
                                    break
                                else:
                                    toappend.append([s1, s2, char])
                                    eq = -1
                        if eq == 0:
                            distinguished.append([states[i], states[j]])
                        elif eq == -1:
                            s = [states[i], states[j]]
                            s.extend(toappend)
                            unchecked[count] = s
                            count += 1
                        else:
                            p1 = pos[states[i]]
                            p2 = pos[states[j]]
                            if p1 != p2:
                                st = equivalent.pop(p2)
                                for s in st:
                                    pos[s] = p1
                                equivalent[p1] = equivalent[p1].union(st)
            newFound = True
            while newFound and len(unchecked) > 0:
                newFound = False
                toremove = set()
                for p, pair in list(unchecked.items()):  #.items():
                    for tr in pair[2:]:
                        if [tr[0], tr[1]] in distinguished or [tr[1], tr[0]] in distinguished:
                            unchecked.pop(p)
                            distinguished.append([pair[0], pair[1]])
                            newFound = True
                            break
            for pair in unchecked.values():
                p1 = pos[pair[0]]
                p2 = pos[pair[1]]
                if p1 != p2:
                    st = equivalent.pop(p2)
                    for s in st:
                        pos[s] = p1
                    equivalent[p1] = equivalent[p1].union(st)
            if len(equivalent) == len(states):
                self.minDFA = self.dfa
            else:
                self.minDFA = self.dfa.newBuildFromEquivalentStates(equivalent, pos)
    
    class NFAfromRegex:
        """class for building e-nfa from regular expressions"""
    
        def __init__(self, regex):
            self.star = '*'
            # Next line was originally "self.plus = '+'
            # Changed to '|' to fit aplication of regex for program
            self.plus = '|'
            self.dot = '.'
            self.openingBracket = '('
            self.closingBracket = ')'
            self.operators = [self.plus, self.dot]
            self.regex = regex
            self.alphabet = [chr(i) for i in range(65,91)]
            self.alphabet.extend([chr(i) for i in range(97,123)])
            self.alphabet.extend([chr(i) for i in range(48,58)])
            self.buildNFA()
    
        def getNFA(self):
            return self.nfa
    
        def displayNFA(self):
            self.nfa.display()
    
        def buildNFA(self):
            language = set()
            self.stack = []
            self.automata = []
            previous = "::e::"
            for char in self.regex:
                if char in self.alphabet:
                    language.add(char)
                    if previous != self.dot and (previous in self.alphabet or previous in [self.closingBracket,self.star]):
                        self.addOperatorToStack(self.dot)
                    self.automata.append(BuildAutomata.basicstruct(char))
                elif char  ==  self.openingBracket:
                    if previous != self.dot and (previous in self.alphabet or previous in [self.closingBracket,self.star]):
                        self.addOperatorToStack(self.dot)
                    self.stack.append(char)
                elif char  ==  self.closingBracket:
                    if previous in self.operators:
                        raise BaseException("Error processing '%s' after '%s'" % (char, previous))
                    while(1):
                        if len(self.stack) == 0:
                            raise BaseException("Error processing '%s'. Empty stack" % char)
                        o = self.stack.pop()
                        if o == self.openingBracket:
                            break
                        elif o in self.operators:
                            self.processOperator(o)
                elif char == self.star:
                    if previous in self.operators or previous  == self.openingBracket or previous == self.star:
                        raise BaseException("Error processing '%s' after '%s'" % (char, previous))
                    self.processOperator(char)
                elif char in self.operators:
                    if previous in self.operators or previous  == self.openingBracket:
                        raise BaseException("Error processing '%s' after '%s'" % (char, previous))
                    else:
                        self.addOperatorToStack(char)
                else:
                    raise BaseException("Symbol '%s' is not allowed" % char)
                previous = char
            while len(self.stack) != 0:
                op = self.stack.pop()
                self.processOperator(op)
            if len(self.automata) > 1:
                print (self.automata)
                raise BaseException("Regex could not be parsed successfully")
            self.nfa = self.automata.pop()
            self.nfa.language = language
    
        def addOperatorToStack(self, char):
            while(1):
                if len(self.stack) == 0:
                    break
                top = self.stack[len(self.stack)-1]
                if top == self.openingBracket:
                    break
                if top == char or top == self.dot:
                    op = self.stack.pop()
                    self.processOperator(op)
                else:
                    break
            self.stack.append(char)
    
        def processOperator(self, operator):
            if len(self.automata) == 0:
                raise BaseException("Error processing operator '%s'. Stack is empty" % operator)
            if operator == self.star:
                a = self.automata.pop()
                self.automata.append(BuildAutomata.starstruct(a))
            elif operator in self.operators:
                if len(self.automata) < 2:
                    raise BaseException("Error processing operator '%s'. Inadequate operands" % operator)
                a = self.automata.pop()
                b = self.automata.pop()
                if operator == self.plus:
                    self.automata.append(BuildAutomata.plusstruct(b,a))
                elif operator == self.dot:
                    self.automata.append(BuildAutomata.dotstruct(b,a))
    
    """def drawGraph(automata, file = ""):
        From https://github.com/max99x/automata-editor/blob/master/util.py
        f = popen(r"dot -Tpng -o graph%s.png" % file, 'w')
        try:
            f.write(automata.getDotFile())
        except:
            raise BaseException("Error creating graph")
        finally:
            f.close()"""
    
    def isInstalled(program):
        """From http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python"""
        import os
        def is_exe(fpath):
            return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
        fpath, fname = os.path.split(program)
        if fpath:
            if is_exe(program) or is_exe(program+".exe"):
                return True
        else:
            for path in os.environ["PATH"].split(os.pathsep):
                exe_file = os.path.join(path, program)
                if is_exe(exe_file) or is_exe(exe_file+".exe"):
                    return True
        return False
    
    class Matrix:
        
        def __init__(self, DFA, allstates, transitions, length):
            self.length = length
            self.DFA = DFA
            self.allstates = allstates
            dim = 0
     
            for value, _state in allstates.items():
                if value != 0:
                    dim += 1
            dim += 1
            self.dim = dim
            self.matrix= self.createMatrix( self.DFA, self.allstates, transitions, self.dim)
            
        def getDim(self):
            return self.dim
            
        def createMatrix(self, DFA, allstates, transitions, dim):
     
            matrix = [[0]*(dim) for _ in range(dim)]
            for from_state, to_state in transitions.items():
                for to_state, _char in to_state.items():
                    matrix[from_state-1][to_state-1] = 1 
           
            return matrix   
            
        def count_paths(self, DFA, dim, length):
            modulo = 1000000007
            result = Matrix.pow2(self, length, self.matrix, dim, modulo)
            count = 0
                  
            for i in range(dim):
                for j in range(dim):
                    if i == 0:
                        for k in DFA.finalstates:
                            k -= 1
                            if j == k:
                                count += result[i][j]
                                
            return count % modulo
    
        def copy(self, dim):
            my_matrix = [[0]*(dim) for _ in range(dim)]
            for i in range(dim):
                for j in range(dim):
                    my_matrix[i][j] = self.matrix[i][j]
                    
            return my_matrix
    
        def printMatrix(self, matrix, dim):
            for i in range(dim):
                print(matrix[i])
    
        def pow2(self, power, matrix, dim, mod=None):
            
            result1 = [[0]*(dim) for _ in range(dim)]
            for i in range(dim):
                result1[i][i] = 1
            while power > 0:
                if power & 1:
                    result1 = Matrix.mat_square_mult(self, result1, matrix, dim, mod);
                power >>= 1;
                if power > 0:
                    matrix = Matrix.mat_square_mult(self, matrix, matrix, dim, mod);
            matrix = result1 
                   
            return matrix
    
        def mat_square_mult(self, matrix, other, dim, mod=None):
            result = [[0]*(dim) for _ in range(dim)]
            for i in range(dim):
                for j in range(dim):
                    index = 0
                    for k in range(dim):
                        index += (matrix[i][k] * other[k][j]) % mod
    
                        result[i][j] = index
            return result
    
    def count_strings(regex, length):
    
        nfaObj = NFAfromRegex(regex)
        nfa = nfaObj.getNFA()
        dfaObj = DFAfromNFA(nfa)
        dfa = dfaObj.getDFA()
        #minDFA = dfaObj.getMinimisedDFA()
        #print ("\nNFA: ")
        #nfaObj.displayNFA()
        #print ("\nDFA: ")
        #dfaObj.displayDFA()
        #print ("\nMinimised DFA: ")
        #dfaObj.displayMinimisedDFA()
        #result = None
        transitions = dfa.transitions
        allstates = dfaObj.getAllStates()
        resultObj = Matrix(dfa, allstates, transitions, length) 
        dim = resultObj.getDim()
        result = Matrix.count_paths(resultObj, dfa, dim, length)
        
        #The result should be 198110437 for the test input
    
        
        print (result)
        
        
        
    def main():
        
        cases = int(input().strip())
        for _i in range(cases):
            in_line = input().strip().split()
            regex = in_line[0]
            length = int(in_line[1])
            count_strings(regex, length)
        
    if __name__  ==  '__main__':
        main()

    public by vladimirov1 modified Aug 21, 2016  112  0  2  0

    From http://web.stanford.edu/~mwaskom/software/seaborn/examples/many_facets.html

    From http://web.stanford.edu/~mwaskom/software/seaborn/examples/many_facets.html: Seaborn example.py
    import numpy as np
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set(style="ticks")
    
    # Create a dataset with many short random walks
    rs = np.random.RandomState(4)
    pos = rs.randint(-1, 2, (20, 5)).cumsum(axis=1)
    pos -= pos[:, 0, np.newaxis]
    step = np.tile(range(5), 20)
    walk = np.repeat(range(20), 5)
    df = pd.DataFrame(np.c_[pos.flat, step, walk],
                      columns=["position", "step", "walk"])
    
    # Initialize a grid of plots with an Axes for each walk
    grid = sns.FacetGrid(df, col="walk", hue="walk", col_wrap=5, size=1.5)
    
    # Draw a horizontal line to show the starting point
    grid.map(plt.axhline, y=0, ls=":", c=".5")
    
    # Draw a line plot to show the trajectory of each random walk
    grid.map(plt.plot, "step", "position", marker="o", ms=4)
    
    # Adjust the tick positions and labels
    grid.set(xticks=np.arange(5), yticks=[-3, 3],
             xlim=(-.5, 4.5), ylim=(-3.5, 3.5))
    
    # Adjust the arrangement of the plots
    grid.fig.tight_layout(w_pad=1)
    
    
    

    public by vladimirov1 modified Aug 20, 2016  40  1  1  0

    From http://flexx.readthedocs.io/en/v0.2/examples/ui/with_bokeh.py.html

    From http://flexx.readthedocs.io/en/v0.2/examples/ui/with_bokeh.py.html: Flexx Bokeh.py
    """
    Example demonstrating a Bokeh plot in Flexx, using a Phosphor dock panel
    layout. Includes client-side interaction with sliders.
    """
    
    import numpy as np
    
    from bokeh.plotting import figure
    
    from flexx import app, ui, react
    
    src = 'https://github.com/zoofIO/flexx/blob/master/examples/ui/with_bokeh.py'
    
    # Plot 1
    N = 1000
    x = np.random.normal(0, np.pi, N)
    y = np.sin(x) + np.random.normal(0, 0.2, N)
    TOOLS = "pan,wheel_zoom,box_zoom,reset,box_select"
    p1 = figure(tools=TOOLS)
    p1.scatter(x, y, alpha=0.1, nonselection_alpha=0.1)
    
    # Plot2
    t = np.linspace(0, 6.5, 100)
    p2 = figure(tools=TOOLS)
    p2.line(t, np.sin(t))
    p3 = figure(tools=TOOLS)
    p3.line(t, np.cos(t))
    
    class Example(ui.Widget):
    
        def init(self):
    
            with ui.DockPanel():
                self.plot1 = ui.BokehWidget(plot=p1, title='Scatter')
                with ui.VBox(title='Sine'):
                    with ui.FormLayout():
                        self.amp = ui.Slider(title='Amplitude', max=2, value=1)
                        self.freq = ui.Slider(title='Frequency', max=10, value=5)
                        self.phase = ui.Slider(title='Phase', max=3, value=1)
                    with ui.Widget(style='overflow-y:auto;', flex=1):
                        self.plot2 = ui.BokehWidget(plot=p2)
                        self.plot3 = ui.BokehWidget(plot=p3)
                # Add some colorful panels just for fun
                ui.Label(title='Info', text='Source is <a href="%s">%s</a>' % (src, src))
                ui.Widget(style='background:#0a0;', title='green')
                ui.Widget(style='background:#00a;', title='blue')
    
        class JS:
    
            @react.connect('amp.value', 'freq.value', 'phase.value')
            def _update_sine(self, amp, freq, phase):
                # Get reference to line glyph, can this be done easier?
                glyph = None
                plot = self.plot2.plot()
                if plot:
                    for ren in plot.renderers.values():
                        if ren.glyph:
                            glyph = ren.glyph
                            break
                # Update
                if glyph:
                    glyph.y = [amp*Math.sin(x*freq+phase) for x in glyph.x]  # noqa
                    plot.render()
    
    
    if __name__ == '__main__':
        m = app.launch(Example)
        app.run()
    
    
    

    public by moyashi modified Jul 5, 2016  97  0  1  0

    dropboxlogin

    dropboxlogin: dropboxloginv2.py
    #!python3
    # YOU NEED TO INSERT YOUR APP KEY AND SECRET BELOW!
    # Go to dropbox.com/developers/apps to create an app.
    
    from __future__ import absolute_import
    from __future__ import print_function
    app_key = 'YOUR_APP_KEY'
    app_secret = 'YOUR_APP_SECRET'
    
    import webbrowser
    import dropbox
    import keychain
    
    def get_access_token():
    	access_token = keychain.get_password('dropboxv2', app_key)
    	if access_token:
    		return access_token
    	auth_flow = dropbox.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
    	authorize_url = auth_flow.start()
    	print("1. Go to: " + authorize_url)
    	print("2. Click \"Allow\" (you might have to log in first).")
    	print("3. Copy the authorization code.")
    	webbrowser.open(authorize_url, modal=True)
    	auth_code = input("Enter the authorization code here: ").strip()
    	try:
    	    access_token, user_id = auth_flow.finish(auth_code)
    	except Exception as e:
    	    print('Error: %s' % (e,))
    	    return
    	keychain.set_password('dropboxv2', app_key, access_token)
    	return access_token
    
    def get_client():
    	access_token = get_access_token()
    	dbx = dropbox.Dropbox(access_token)
    	return dbx
    
    def main():
    	# Demo if started run as a script...
    	# Just print the account info to verify that the authentication worked:
    	print('Getting account info...')
    	dropbox_client = get_client()
    	account_info = dropbox_client.users_get_current_account()
    	print('linked account:', account_info)
    
    if __name__ == '__main__':
        main()
    
    
    

    public by codemonkey85 modified May 5, 2016  174  1  2  0

    Access Trello data from python / Pythonista

    Access Trello data from python / Pythonista: Trello-Pythonista.py
    # coding: utf-8
    
    # Note to self: do proper authorization at: https://github.com/sarumont/py-trello/blob/master/trello/util.py
    
    import sys
    if sys.argv == [] or len(sys.argv) == 1:
        sys.exit()
    
    clArgs = sys.argv[1].split(',')
    
    if clArgs == [] or len(clArgs) != 4:
        sys.exit()
    
    # Must pass in Trello API Key, Trello App Token, Trello boardId, and Trello username
    appKey = clArgs[0]
    appToken = clArgs[1]
    boardId = clArgs[2]
    userName = clArgs[3]
    
    import platform
    
    if platform.system() == 'Darwin':
        if platform.machine().startswith('iP'):
            # we're running on an iOS device
            import console
            import clipboard
            import webbrowser
    
    import urllib2
    import urllib
    import json
    
    # to properly encode unicode characters in JSON objects
    def encode_obj(in_obj):
        def encode_list(in_list):
            out_list = []
            for el in in_list:
                out_list.append(encode_obj(el))
            return out_list
        def encode_dict(in_dict):
            out_dict = {}
            for k, v in in_dict.iteritems():
                out_dict[k] = encode_obj(v)
            return out_dict
        if isinstance(in_obj, unicode):
            return in_obj.encode('utf-8')
        elif isinstance(in_obj, list):
            return encode_list(in_obj)
        elif isinstance(in_obj, tuple):
            return tuple(encode_list(in_obj))
        elif isinstance(in_obj, dict):
            return encode_dict(in_obj)
    	return in_obj
    
    myCards = []
    
    args = {
    'fields': 'id',
    'key': appKey,
    'token': appToken
    }
    
    memberUrl = "https://api.trello.com/1/members/%s?%s" % (userName, urllib.urlencode((args)))
    
    try:
        data = urllib2.urlopen(memberUrl)
    except urllib2.HTTPError as inst:
        raise Exception("Key or Token incorrect")
    
    member = json.loads(data.read())
    
    args = {
    'fields': 'id,name,cards',
    'key': appKey,
    'token': appToken,
    'filter': 'open'
    }
    
    listsUrl = "https://api.trello.com/1/boards/%s/lists?%s" % (boardId, urllib.urlencode((args)))
    
    try:
        data = urllib2.urlopen(listsUrl)
    except urllib2.HTTPError as inst:
        raise Exception("Key or Token incorrect")
    
    lists = json.loads(data.read())
    
    args = {
    'fields': 'all',
    'key': appKey,
    'token': appToken
    }
    
    # in case there are lists on the Trello board you don't care about - skipping lists can speed up parsing operations
    listsToExclude = []
    
    gen = (x for x in lists if x['name'] not in listsToExclude)
    for li in gen:
        cardsUrl = "https://api.trello.com/1/lists/%s/cards?%s" % (li['id'], urllib.urlencode((args)))
    
        try:
            cardsData = urllib2.urlopen(cardsUrl)
        except urllib2.HTTPError as inst:
            raise Exception("Key or Token incorrect")
    
        cards = json.loads(cardsData.read())
    
        gen = (x for x in cards if member['id'] in x['idMembers'])
    
        for card in gen:
            myCards.append(card)
    
    newline = '\n'
    comma = ','
    spaceHashtag = ' #'
    spaceAtSign = ' @'
    myTasks = []
    myJsonTasks = []
    
    for card in myCards:
        cardNum = card['idShort']
        cardName = card['name']
        cardLabels = card['labels']
        listId = card['idList']
    
        listGen = (x for x in lists if x['id'] == listId)
        listName = ''
        for li in listGen:
            listName = li['name']
    
        taskTitle = "Card #%s - %s" % (cardNum, cardName)
    
        labelNames = []
        labelNamesNoSpaces = []
        for label in cardLabels:
            labelName = label['name']
            labelNames.append(labelName)
            labelNamesNoSpaces.append(labelName.replace(" ","_"))
    
        hashtaglabels = spaceHashtag.join(labelNamesNoSpaces)
        atsignlabels = spaceAtSign.join(labelNamesNoSpaces)
    
        if hashtaglabels != "":
         # for Wunderlist
         hashtaglabels = '#' + hashtaglabels
         # for Todoist
        if atsignlabels != "":
         atsignlabels = '@' + atsignlabels
    
        taskData = {
        'Title': taskTitle,
        'shortUrl': card['shortUrl'],
        'desc': card['desc'],
        'idShort': cardNum,
        'labels': cardLabels,
        'pos': card['pos'],
        'name': cardName,
        'labelString': comma.join(labelNames),
        'labelStringHashtag': hashtaglabels,
        'labelStringAtSign': atsignlabels,
        'closed': card['closed'],
        'dateLastActivity': card['dateLastActivity'],
        'due': card['due'],
        'idList': listId,
        'ListName': listName,
        'subscribed': card['subscribed'],
        'url': card['url'],
        'FullData': card
        }
    
    # Fields I've left out:
    #   badges
    #   checkItemStates
    #   descData
    #   idAttachmentCover
    #   idBoard
    #   idChecklists
    #   idLabels
    #   idMembers
    #   idMembersVoted
    #   manualCoverAttachment
    #   shortLink
    #   email
    
        myTasks.append(taskData)
        myJsonTasks.append(urllib.urlencode(taskData).replace('+','%20'))
    
    if platform.system() == 'Darwin':
        if platform.machine().startswith('iP'):
            clipboard.set(newline.join(myJsonTasks))
            webbrowser.open('workflow://')
    else:
        # this is for me to dump the data into C#
        jsonTasks = json.dumps(myTasks)
        print jsonTasks
    
    
    
    • Public Snippets
    • Channels Snippets