George Jones 1 year ago
parent
commit
d52c2878cf

+ 19 - 0
home/public/snippits/bash/error-functions.sh

@@ -0,0 +1,19 @@
+# Some error handling functions
+
+SCRIPTNAME=`echo "$0"|sed 's!.*/!!'`
+function info()  { echo `date +%c` "${SCRIPTNAME}"\: info: "$@" 1>&2; }
+function warn()  { echo `date +%c` "${SCRIPTNAME}"\: warning: "$@" 1>&2; }
+function error() { echo `date +%c` "${SCRIPTNAME}"\: error: "$@" 1>&2; }
+# Temporary debug messages
+#   DEBUG=1 debug foo
+function debug() { [[ -v DEBUG ]] && echo `date +%c` "${SCRIPTNAME}"\: debug: "$@" 1>&2 || true ; }
+function die()   {
+
+    exit_code=1
+    if [[ -v 2 ]]; then
+       exit_code="${1}"
+       shift;
+    fi
+
+    echo `date +%c` "${SCRIPTNAME}"\: fatal: "$@" 1>&2 && exit "${exit_code}";
+}

+ 26 - 0
home/public/snippits/bash/getopt-test.sh

@@ -0,0 +1,26 @@
+#! /bin/bash
+# getopts test
+
+# https://www.computerhope.com/unix/bash/getopts.htm
+
+while getopts :foo OPT; do
+    case $OPT in
+        f|+f)
+            echo $OPT
+            echo $ARGS
+            ;;
+        o|+o)
+
+            ;;
+        o|+o)
+            
+            ;;
+        *)
+            echo "usage: `basename $0` [+-foo} [--] ARGS..."
+            exit 2
+    esac
+done
+shift `expr $OPTIND - 1`
+OPTIND=1
+
+echo ARGS $*

+ 39 - 0
home/public/snippits/bash/scoping.sh

@@ -76,3 +76,42 @@ echo FOO outside blort after call to baz is $FOO
 # Commentary.  This seems like a "safe" way to
 #   lmit namespace pollution and side-effects in bash,
 #   as long as you don't find the fork/exec overhead.
+
+#
+# Figure how EXPORT works/when needed.
+#
+
+# inital state: FOO unbound
+unset FOO
+set -u
+echo $FOO
+#bash: FOO: unbound variable
+
+# FOO set to bar
+FOO=bar
+
+#
+function f { echo FOO is $FOO; }
+f
+#FOO is bar
+function g { (echo FOO in subprocess is$FOO) }
+g
+#FOO in subprocess isbar
+FOO=foo
+f
+#FOO is foo
+g
+#FOO in subprocess isfoo
+cat <<EOF
+#! /bin/bash
+set -u
+echo in h FOO is $FOO
+EOF
+chmod +x h
+unalias h
+./h
+#./h: line 3: FOO: unbound variable
+
+export FOO=qux
+./h
+# in h FOO is qux

+ 4 - 0
home/public/snippits/elisp/el.sh

@@ -0,0 +1,4 @@
+#!/usr/bin/env sh
+":";
+exec emacs --quick --script "$0" "$@"
+# -*- mode: emacs-lisp -*-

+ 9 - 0
home/public/snippits/org/babel-input-variables.org

@@ -0,0 +1,9 @@
+# This is an org babel example of input variables on the org line
+# https://emacs.stackexchange.com/questions/49092/passing-variable-into-a-org-babel-code-on-export
+
+#+begin_src python :var argument="default value" :results output
+print(argument)
+#+end_src
+
+#+RESULTS:
+: default value

+ 49 - 0
home/public/snippits/org/ob-cache-test.org

@@ -0,0 +1,49 @@
+Test results of reading cached results in OB blocks w/o re-exeuction
+
+Caveat: results have to be left justified if caching us used or
+results are not picked up
+
+* Output cached tables
+** Left Justified
+#+name: STUFF1
+#+begin_src shell  :results output table :exports results :cache yes
+echo a b c time
+echo 1 2 3 `date +%s`
+echo 4 5 6 0
+#+end_src
+
+#+RESULTS[ab9d0568e51adca45cdf65701d481a475d07b0e3]: STUFF1
+| a | b | c |       time |
+| 1 | 2 | 3 | 1503328650 |
+| 4 | 5 | 6 |          0 |
+
+** Not Left Justified
+  #+name: STUFF2
+  #+begin_src shell  :results output table :exports results :cache yes
+  echo d e f time
+  echo 1 2 3 `date +%s`
+  echo 4 5 6 0
+  #+end_src
+
+  #+RESULTS[395dcd437c9eb11f42e463c5333797c0007f0b9a]: STUFF2
+  | d | e | f |       time |
+  | 1 | 2 | 3 | 1503328653 |
+  | 4 | 5 | 6 |          0 |
+
+
+     #+BEGIN_SRC ipython :session foo  :var stuff1=STUFF1 :exports results
+stuff1
+     #+END_SRC
+
+     #+RESULTS:
+     | a | b | c |       time |
+     | 1 | 2 | 3 | 1503328650 |
+     | 4 | 5 | 6 |          0 |
+
+
+     #+BEGIN_SRC ipython :session foo  :var stuff2=STUFF2 :exports results
+stuff2
+     #+END_SRC
+
+     #+RESULTS:
+     : nil

+ 49 - 0
home/public/snippits/org/ob-cache-test2.org

@@ -0,0 +1,49 @@
+Test results of reading cached results in OB blocks w/o re-exeuction
+
+Caveat: results have to be left justified if caching us used or
+results are not picked up
+
+* Output cached tables
+** Left Justified
+#+name: STUFF1
+#+begin_src shell  :results output table :exports results :cache yes
+echo a b c time
+echo 1 2 3 `date +%s`
+echo 4 5 6 0
+#+end_src
+
+#+RESULTS[ef6aa4438671e0846be5916cf6ece1ffd8607b47]: STUFF1
+| a | b | c |       time |
+| 1 | 2 | 3 | 1668770452 |
+| 4 | 5 | 6 |          0 |
+
+** Not Left Justified
+  #+name: STUFF2
+  #+begin_src shell  :results output table :exports results :cache yes
+  echo d e f time
+  echo 1 2 3 `date +%s`
+  echo 4 5 6 0
+  #+end_src
+
+  #+RESULTS[2c709913df46c05a4be0fb1de00797677d9f4884]: STUFF2
+  | d | e | f |       time |
+  | 1 | 2 | 3 | 1668770460 |
+  | 4 | 5 | 6 |          0 |
+
+
+     #+BEGIN_SRC python :session foo  :var stuff1=STUFF1 :exports results
+stuff1
+     #+END_SRC
+
+     #+RESULTS:
+     | a | b | c |       time |
+     | 1 | 2 | 3 | 1668770452 |
+     | 4 | 5 | 6 |          0 |
+
+
+     #+BEGIN_SRC python :session foo  :var stuff2=STUFF2 :exports results
+stuff2
+     #+END_SRC
+
+     #+RESULTS:
+     : nil

+ 15 - 15
home/public/snippits/org/orgTables.org

@@ -1,18 +1,18 @@
   * Fun with name fields and recalculation in org tables
    See http://orgmode.org/org.html#Advanced-features
-  
-     |---+---------+--------+--------+--------+-------+------|
-     |   | Student | Prob 1 | Prob 2 | Prob 3 | Total | Note |
-     |---+---------+--------+--------+--------+-------+------|
-     | ! |         |     P1 |     P2 |     P3 |   Tot |      |
-     | # | Maximum |     11 |     15 |     25 |    51 | 10.2 |
-     | ^ |         |     m1 |     m2 |     m3 |    mt |      |
-     |---+---------+--------+--------+--------+-------+------|
-     | # | Peter   |     10 |      8 |     23 |    41 |  8.2 |
-     | # | Sam     |      2 |      4 |      3 |     9 |  1.8 |
-     |---+---------+--------+--------+--------+-------+------|
-     |   | Average |        |        |        |  25.0 |      |
-     | ^ |         |        |        |        |    at |      |
-     | $ | max=50  |        |        |        |       |      |
-     |---+---------+--------+--------+--------+-------+------|
+
+     |---+---------+--------+--------+--------+-------+-------|
+     |   | Student | Prob 1 | Prob 2 | Prob 3 | Total |  Note |
+     |---+---------+--------+--------+--------+-------+-------|
+     | ! |         |     P1 |     P2 |     P3 |   Tot |       |
+     | # | Maximum |     11 |     15 |   1000 |  1026 | 205.2 |
+     | ^ |         |     m1 |     m2 |     m3 |    mt |       |
+     |---+---------+--------+--------+--------+-------+-------|
+     | # | Peter   |     10 |      8 |    -10 |     8 |   1.6 |
+     | # | Sam     |      2 |      6 |      3 |    11 |   2.2 |
+     |---+---------+--------+--------+--------+-------+-------|
+     |   | Average |        |        |        |   9.5 |       |
+     | ^ |         |        |        |        |    at |       |
+     | $ | max=50  |        |        |        |       |       |
+     |---+---------+--------+--------+--------+-------+-------|
      #+TBLFM: $6=vsum($P1..$P3)::$7=10*$Tot/$max;%.1f::$at=vmean(@-II..@-I);%.1f

+ 47 - 0
home/public/snippits/pandas/apply_function_to_row.org

@@ -0,0 +1,47 @@
+https://www.geeksforgeeks.org/apply-function-to-every-row-in-a-pandas-dataframe/
+
+#+begin_src python :results output
+import pandas as pd
+import numpy as np
+
+def normalize(x, y):
+    x_new = ((x - np.mean([x, y])) /
+             (max(x, y) - min(x, y)))
+
+    # print(x_new)
+    return x_new
+
+def main():
+
+    # create a dictionary with three fields each
+    data = {
+        'X':[1, 2, 3],
+        'Y':[45, 65, 89] }
+
+    # Convert the dictionary into DataFrame
+    df = pd.DataFrame(data)
+    print("Original DataFrame:\n", df)
+
+    df['X'] = df.apply(lambda row : normalize(row['X'],
+                                  row['Y']), axis = 1)
+
+    print('\nNormalized:')
+    print(df)
+
+main()
+#+end_src
+
+#+RESULTS:
+#+begin_example
+Original DataFrame:
+    X   Y
+0  1  45
+1  2  65
+2  3  89
+
+Normalized:
+     X   Y
+0 -0.5  45
+1 -0.5  65
+2 -0.5  89
+#+end_example

+ 7 - 0
home/public/snippits/pandas/companies.csv

@@ -0,0 +1,7 @@
+# Sample company data
+
+Company,Division,profit
+Ford,Mercury,42
+Ford,Lincoln,100
+Chevy,Malabu,2
+Chevy,Pickups,4

+ 10 - 0
home/public/snippits/pandas/fred.csv

@@ -0,0 +1,10 @@
+name,date,income
+Fred,1999,99
+Barny,1999,100
+Wilma,1999,50
+Fred,2000,110
+Barny,1999,120
+Wilma,1999,55
+Fred,1999,200
+Barny,1999,130
+Wilma,1999,20

+ 40 - 0
home/public/snippits/pandas/plot.py

@@ -0,0 +1,40 @@
+#! /usr/bin/env python3
+# Read a CSV file and plot it
+#
+# https://stackoverflow.com/questions/40071096/how-to-plot-multiple-lines-in-one-figure-in-pandas-python-based-on-data-from-mul
+
+import pandas as pd
+import matplotlib.pyplot as plt
+from io import StringIO
+
+# CSV String with out headers
+csvString = """name,date,income
+Barny,1998-01-01,100
+Barny,1999-01-01,120
+Barny,2000-01-01,130
+Fred,1998-01-01,200
+Fred,1999-01-01,99
+Fred,2000-01-01,110
+Wilma,1996-01-01,20
+Wilma,1997-01-01,50
+Wilma,1998-01-01,55
+Wilma,1999-01-01,40
+"""
+
+# Convert String into StringIO
+csvStringIO = StringIO(csvString)
+# Load CSV String into DataFrame
+df = pd.read_csv(csvStringIO, sep=",")
+df.sort_values("date",inplace=True)
+print(df)
+
+fig,ax = plt.subplots()
+
+for name in list(list(set(df['name']))):
+    ax.plot(df[df.name==name].date,df[df.name==name].income,label=name)
+
+ax.set_xlabel("date")
+ax.set_ylabel("income")
+ax.legend(loc='best')
+
+plt.show()

+ 0 - 0
home/public/snippits/python/logging.py → home/public/snippits/python/myLogging.py


+ 18 - 0
home/public/snippits/python/plot2lines.py

@@ -0,0 +1,18 @@
+#! /usr/bin/env python3
+# https://pythonguides.com/python-plot-multiple-lines/
+# Importing packages
+import matplotlib.pyplot as plt
+
+# Define data values
+x = [7, 14, 21, 28, 35, 42, 49]
+y = [5, 12, 19, 21, 31, 27, 35]
+z = [3, 5, 11, 20, 15, 29, 31]
+
+# Plot a simple line chart
+plt.plot(x, y, 'g', label='Line y')
+
+# Plot another line on the same chart/graph
+plt.plot(x, z, 'r', label='Line z')
+
+plt.legend()
+plt.show()

+ 0 - 9
home/public/snippits/python/read_csv.py

@@ -5,15 +5,6 @@
 #
 
 import csv
-import sys
-
-files = sys.argv[1:]
-
-if len(files) == 0:
-    sys.stderr.write("Usage: read_csv.py file [file...]\n")
-    sys.exit(1)
-
-MAXLINES = 10000
 
 # # Define data
 # data = [