diff --git a/README.md b/README.md index c7f9c4eb7..28c4eaf7a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ActiveState Code Recipes -Welcome to the ActiveState code recipes repo! We have migrated all of the great content from code.activestate.com to its new +Welcome to the ActiveState Code recipes repo! We have migrated all of the great content from code.activestate.com to its new forever-home here at GitHub. This makes it easier for everyone to submit new recipes, contribute code and integrate all the great information into their own projects. @@ -18,7 +18,7 @@ You can find the master index of all recipes over at the [wiki](https://github.c *Coming Soon: Search Functionality!* -## How do I submit a new recipe? +## How could I submit a new recipe? 1. Fork this repo. 2. Create your new recipe in the correct language subfolder (create a new folder if it doesn't already exist). diff --git a/recipes/Bash/578661_find__grep/recipe-578661.sh b/recipes/Bash/578661_find__grep/recipe-578661.sh index 3846e2bee..62773a86f 100644 --- a/recipes/Bash/578661_find__grep/recipe-578661.sh +++ b/recipes/Bash/578661_find__grep/recipe-578661.sh @@ -1,6 +1,6 @@ #!/usr/bin/env zsh -tmp_file="/tmp/_unsorted_find_n_grep" +tmp_file="$(mktemp)" echo "--- find <$2> in <$1>" > $tmp_file find . -follow -iname "$1" | while read file diff --git a/recipes/Bash/tree_alias/LICENSE.md b/recipes/Bash/tree_alias/LICENSE.md new file mode 100644 index 000000000..09dcacce0 --- /dev/null +++ b/recipes/Bash/tree_alias/LICENSE.md @@ -0,0 +1 @@ +This work is released to the public domain under the CC0 (Creative Commons) license terms: https://creativecommons.org/publicdomain/zero/1.0/legalcode \ No newline at end of file diff --git a/recipes/Bash/tree_alias/README.md b/recipes/Bash/tree_alias/README.md new file mode 100644 index 000000000..5d594ab7e --- /dev/null +++ b/recipes/Bash/tree_alias/README.md @@ -0,0 +1,8 @@ +### An alias for the tree command + +Author: Unknown +Submitter: Alan Young (harleypig) + +If the tree command does not exist on your system, this alias will fake it. + +I got this from a co-worker a long time ago. I don't remember when, or who it was. If it was you, let me know and I'll update this. diff --git a/recipes/Bash/tree_alias/tree b/recipes/Bash/tree_alias/tree new file mode 100644 index 000000000..d9282f262 --- /dev/null +++ b/recipes/Bash/tree_alias/tree @@ -0,0 +1,10 @@ +#!/bin/bash + +# I got this from a co-worker many moons ago. Unfortunately, I don't remember +# who. Basically, if a tree program is not installed, fake it with this. + +if ! command -v tree > /dev/null 2>&1; then + + alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'" + +fi diff --git a/recipes/JavaScript/576955_Incrementing_counter/recipe-576955.js b/recipes/JavaScript/576955_Incrementing_counter/recipe-576955.java similarity index 100% rename from recipes/JavaScript/576955_Incrementing_counter/recipe-576955.js rename to recipes/JavaScript/576955_Incrementing_counter/recipe-576955.java diff --git a/recipes/Python/278260_Dictionary_Tools/LICENSE.md b/recipes/Python/278260_Dictionary_Tools/LICENSE.md deleted file mode 100644 index 86fe1ce11..000000000 --- a/recipes/Python/278260_Dictionary_Tools/LICENSE.md +++ /dev/null @@ -1,3 +0,0 @@ -Copyright 2004 Runsun Pan - -The software is licensed according to the terms of the PSF (Python Software Foundation) license found here: http://www.python.org/psf/license/ \ No newline at end of file diff --git a/recipes/Python/278260_Dictionary_Tools/README.md b/recipes/Python/278260_Dictionary_Tools/README.md deleted file mode 100644 index 61a762b68..000000000 --- a/recipes/Python/278260_Dictionary_Tools/README.md +++ /dev/null @@ -1,6 +0,0 @@ -## Dictionary Tools -Originally published: 2004-04-14 14:06:08 -Last updated: 2004-04-14 14:06:08 -Author: Runsun Pan - -A collection of some dictionary tools \ No newline at end of file diff --git a/recipes/Python/278260_Dictionary_Tools/recipe-278260.py b/recipes/Python/278260_Dictionary_Tools/recipe-278260.py deleted file mode 100644 index 5a48beb42..000000000 --- a/recipes/Python/278260_Dictionary_Tools/recipe-278260.py +++ /dev/null @@ -1,291 +0,0 @@ -#======================================================================== - -def sumDict(d): - return reduce(lambda x,y:x+y, d.values()) - -#======================================================================== - -def avgDict(d): - return reduce(lambda x,y:x+y, d.values()) / (len(d)*1.0) - -#======================================================================== - -def mkDict(keyList, valueList, cond=None): - ''' - Make a new dict out of two lists. The first list provides keys, - the 2nd provides values. The cond is a function taking 2 arguments: - key and value. Example: lambda k,v:v%2==0 - A valid item means its cond(key, value) is true. Only valid items - are included in the returned dict. - - >>> a - [0, 1, 2, 3, 4] - - >>> b= [chr(x) for x in range(65,70)] - >>> b - ['A', 'B', 'C', 'D', 'E'] - - >>> mkDict(b,a) - {'A': 0, 'C': 2, 'B': 1, 'E': 4, 'D': 3} - - >>> mkDict(b,a,lambda x,y:y%2==0) - {'A': 0, 'C': 2, 'E': 4} - - Note: - mkDict(a,b) (without the conditional check) is equavilant to - dict(zip(a,b)), which is much faster. If you know there's no - conditional check, better use dict(zip(a,b)) instead. - ''' - - if cond==None: - return dict(zip(keyList, valueList)) - else: - return dict( [(k,v) for k,v in zip(keyList, valueList) if cond(k,v)] ) - -#======================================================================== - -def trimDict(aDict, cond=(lambda k,v:1)): - ''' Return a new dict in which its items whose cond(k,v) == true - are removed (discarded) from aDict. - The cond is a function taking 2 arguments: key and value - - >>> g - {'A': 0, 'C': 2, 'B': 1, 'E': 4, 'D': 3} - >>> trimDict(g, lambda x,y:y%2!=0) - {'A': 0, 'C': 2, 'E': 4} - - ''' - tmp={} - [tmp.setdefault(k,v) for k,v in aDict.items() if not cond(k,v)] - return tmp.copy() - -#======================================================================== - -def reDict(aDict, func, cond=(lambda k,v:1), delUnchanged=0): - ''' - Given aDict, return a new dict with valid items (= items whose - cond(key, value) is true) been modified by the function func. - if delUnchanged, then those invalid items are excluded from - the returned dict. - - >>> d - {'B': 1, 'D': 3} - >>> reDict(d, lambda x:x**2) - {'B': 1, 'D': 9} - >>> g - {'A': 0, 'C': 2, 'B': 1, 'E': 4, 'D': 3} - >>> reDict(g, lambda x:-x, lambda k,v: v%2==0) - {'A': 0, 'C': -2, 'B': 1, 'E': -4, 'D': 3} - >>> reDict(g, lambda x:-x, lambda k,v: v%2==0, delUnchanged=1) - {'A': 0, 'C': -2, 'E': -4} - - ''' - tmp={} - if delUnchanged: - [tmp.setdefault(k,func(v)) for k,v in aDict.items() if cond(k,v)] - else: - [tmp.setdefault(k,(cond(k,v) and func(v) or v)) for k,v in aDict.items()] - return tmp.copy() - -#======================================================================== - -def normDict(d, normalizeTo=1): - ''' Normalize the values of d by setting the largest value in d.values - to normalizeTo. The return dict will have it's values spreading - between 0~normalizeTo''' - ks = d.keys() - vs = d.values() - vMax = max(vs) - vs= [ x/(vMax*1.0)*normalizeTo for x in vs] - return dict(zip(ks, vs)) - -#======================================================================== - -def normDictSumTo(d, sumTo=1): - ''' Normalize the values of d to sumTo. The returned dict will - have value sum = sumTo - ''' - ks = d.keys() - vs = d.values() - sum = reduce(lambda x,y:x+y, vs) - vs= [ x/(sum*1.0)*sumTo for x in vs] - return dict(zip(ks, vs)) - -#======================================================================== - -def accumDict(d, normalizeTo=None): - ''' if d.values=[1,2,3,4,5]: - accumDict(d).values ==> [1, 3, 6, 10, 15] - if d.values=[0.25, 0.25, 0.25, 0.25] - accumDict(d).values ==> [0.25, 0.50, 0.75, 1.00] - - Required: odict() - Ordered Dictionary Class - Dave Benjamin - http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/161403 - ''' - ks = d.keys() - vs = d.values() - if normalizeTo: vs = normListSumTo(vs, sumTo=normalizeTo) - - r = range(1, len(vs)) - newList=[vs[0]] - for i in r: - newList.append( newList[-1]+ vs[i] ) - - odic = odict() - for i in range(len(newList)): - odic[ks[i]] =newList[i] - return odic - -#======================================================================== - -def fallInDict(val, aDict, baseValue=0): - ''' - oo: - { - 'a':3, - 'b':5, - 'c':2 - } - accumDict(oo): - { - 'a':3, - 'b':8, - 'c':10 - } - fallInDict(0, oo)=0 - fallInDict(1, oo)=0 - fallInDict(2, oo)=0 - fallInDict(3, oo)=0 - fallInDict(4, oo)=1 - fallInDict(5, oo)=1 - fallInDict(6, oo)=1 - fallInDict(7, oo)=1 - fallInDict(8, oo)=1 - fallInDict(9, oo)=2 - fallInDict(10, oo)=2 - - When val out of range: - - fallInDict(-1, oo)= -1 - fallInDict(11, oo)= -2 - fallInDict(12, oo)= -2 - ''' - - if valacd.values()[-1]: return -2 - - ra = range(len(acd)) - - for i in ra: - vInDict = acd.values()[i] - if val <= vInDict: break - - return aDict.keys()[i] - -#======================================================================== - -def randomPickDict(d): - ''' given a diictionary d, with all values are numbers, - randomly pick an item and return it's key according - to the percentage of its values''' - ks = d.keys() - vs = accumList(d.values(),1) - return ks[ findIndex( vs, random.random()) ] - -#======================================================================== - -def sumInDict(L): - ''' Given a list L: - [ {'S': [s11,s12,s13], 'V':[v11,v12,v13], ...}, - {'S': [s21,s22,s23], 'V':[v21,v22,v23], ...}, - {'S': [s31,s32,s33], 'V':[v31,v32,v33], ...}, - ... ] - - Return a dict: - {'S': [s1,s2,s3], 'V':[v1,v2,v3], ...} - - where s1 = s11 + s21 + s31 + ... - s2 = s12 + s22 + s32 + ... - ''' - keys = L[0].keys() - new={} - for k in keys: - new[k] = [] # Init 'S':[] - for x in L: # x = {'S': [s11,s12,s13], 'V':[v11,v12,v13], ...} - y = x[k] # y = [s11,s12,s13] - new[k].append(y) - - ''' new[k] = [ [s11,s12,s13],[s21,s22,s23],[s31,s32,s33], ...] - Then use sumInList=> {'S': [s1,s2,s3],'V': [v1,v2,v3],... }''' - new[k] = sumInList(new[k]) - return new - -#======================================================================== - -def avgInDict(L): - ''' Given a list L: - [ {'S': [s11,s12,s13], 'V':[v11,v12,v13], ...}, - {'S': [s21,s22,s23], 'V':[v21,v22,v23], ...}, - {'S': [s31,s32,s33], 'V':[v31,v32,v33], ...}, - ... ] - - Return a dict: - {'S': [s1,s2,s3], 'V':[v1,v2,v3], ...} - - where s1 = avg of (s11 + s21 + s31 + ...) - s2 = avg of (s12 + s22 + s32 + ...) - ''' - ''' First turn L into: - {'S': [ [s11,s12,s13],[s21,s22,s23],[s31,s32,s33], ...], - 'V': [ [v11,v12,v13],[v21,v22,v23],[v31,v32,v33], ...], - ... } ''' - - keys = L[0].keys() - new={} - for k in keys: - new[k] = [] # Init 'S':[] - for x in L: # x = {'S': [s11,s12,s13], 'V':[v11,v12,v13], ...} - y = x[k] # y = [s11,s12,s13] - new[k].append(y) - - ''' new[k] = [ [s11,s12,s13],[s21,s22,s23],[s31,s32,s33], ...] - Then use avgInList=> {'S': [s1,s2,s3],'V': [v1,v2,v3],... }''' - new[k] = avgInList(new[k]) - return new - -#======================================================================== - -def sumInDict2(D): - ''' Given a dict: - {'A':{'S': [s11,s12,s13], 'V':[v11,v12,v13], ...}, - 'B':{'S': [s21,s22,s23], 'V':[v21,v22,v23], ...}, - 'C':{'S': [s31,s32,s33], 'V':[v31,v32,v33], ...}, ... - } - Return a dict: - {'S': [s1,s2,s3], 'V':[v1,v2,v3], ...} - - where s1 = s11 + s21 + s31 + ... - s2 = s12 + s22 + s32 + ... - ''' - return sumInDict(D.values()) - -#======================================================================== - -def avgInDict2(D): - ''' Given a dict: - {'A':{'S': [s11,s12,s13], 'V':[v11,v12,v13], ...}, - 'B':{'S': [s21,s22,s23], 'V':[v21,v22,v23], ...}, - 'C':{'S': [s31,s32,s33], 'V':[v31,v32,v33], ...}, ... - } - Return a dict: - {'S': [s1,s2,s3], 'V':[v1,v2,v3], ...} - - where s1 = avg of (s11 + s21 + s31 + ...) - s2 = avg of (s12 + s22 + s32 + ...) - ''' - return avgInDict(D.values()) - -#======================================================================== diff --git a/recipes/Python/577468_Minimalistic_PostgreSQL_console/recipe-577468.py b/recipes/Python/577468_Minimalistic_PostgreSQL_console/recipe-577468.py index 53eb233f4..18c4e9769 100644 --- a/recipes/Python/577468_Minimalistic_PostgreSQL_console/recipe-577468.py +++ b/recipes/Python/577468_Minimalistic_PostgreSQL_console/recipe-577468.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import re import sys import psycopg2 import psycopg2.extras diff --git a/recipes/Python/578112_Bresenhams_line_algorithm/recipe-578112.py b/recipes/Python/578112_Bresenhams_line_algorithm/recipe-578112.py index 8838ad588..dffe22406 100644 --- a/recipes/Python/578112_Bresenhams_line_algorithm/recipe-578112.py +++ b/recipes/Python/578112_Bresenhams_line_algorithm/recipe-578112.py @@ -68,8 +68,8 @@ def _bresenhamlines(start, end, max_iter): def bresenhamline(start, end, max_iter=5): """ - Returns a list of points from (start, end] by ray tracing a line b/w the - points. + Returns a list of points from (start, end] by ray tracing a line between + the points. Parameters: start: An array of start points (number of points x dimension) end: An end points (1 x dimension)