Browse Source

Merge branch 'master' of git.galthub.com:gmj/home.public.snippits

George Jones 6 months ago
parent
commit
dd418f6867

+ 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 $*

+ 40 - 0
home/public/snippits/bash/learned-form-shellcheck.sh

@@ -0,0 +1,40 @@
+#! /bin/bash
+# Examples of fixing problems ponted out by shellcheck
+
+
+# These are things learned from running shellcheck on my bash
+# https://github.com/koalaman/shellcheck
+
+# https://www.shellcheck.net/wiki/Directive
+
+# Principals:
+#   - Favor readability (e.g. over POSIX compliance)
+#   - Don't define anything when a built-in works
+#   - Use any automathic type/variable/safety/syntax check availabe
+
+# Favor readability over POSIX compliance
+#
+#   shellcheck disable=SC3046 # "source" is more readable than "."
+#   shellcheck disable=SC2112 # "function" is more readable than "()"
+
+# This is bash, be safe
+set -u
+set -e
+
+# BAD, old
+# source ~/lib/bash/bashutils.sh || `: # Use my library if available`\
+#     `: fall back to echo`
+#     alias info=echo && \
+#     alias warn=echo && \
+#     alias error=echo
+#
+# GOOD, new, fixed
+# shellcheck disable=SC1090  # Clean up seperatly
+source ~/lib/bash/bashutils.sh || : Use my library if available \
+    : else fall back to echo && \
+    function info() { echo "$@"; } && \
+    function warn() { echo "$@"; } && \
+    function error() { echo "$@"; } && \
+    function announce() { echo "$@"; }
+
+announce defined alternatives

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

@@ -0,0 +1,117 @@
+#! /bin/bash
+# explore/demonstrate scoping in bash
+
+unset FOO
+
+#
+# Testing scoping of variables to/from functions
+#
+
+
+# Q: Do functions inherit variables from outer scope?
+#
+FOO=FOO-outside
+function baz { echo FOO is $FOO; }
+baz
+# output is "FOO-outside".
+#
+# A: yes.  $FOO comes from outer scope
+
+# Q: when does variable expansion happen
+#
+FOO=FOO-outside2
+baz
+# output is "FOO-outside2"
+#
+# A: expansion of $FOO hapens at baz runtime, not when defined.
+
+# Q: are variables global?
+#
+function blort { FOO=foo-inside; echo FOO inside is $FOO; }
+echo $FOO
+# FOO is "inside before calling the function"
+blort
+# ouput is "FOO inside is foo-inside"
+echo $FOO
+# output is "foo-inside"
+#
+# A: scope of of variableis global
+
+#
+# Exporting variables to subshells
+#
+$ export -p | grep FOO
+$ # FOO is not exported
+
+$ FOO=bar
+$ export -p | grep FOO
+$ # FOO is still not exported
+
+$ export FOO=baz
+$ export -p | grep FOO
+declare -x FOO="baz"
+# Q: but as we see below, non-exported variables are
+#    inherited by subproceses (probably as a conseuqence
+#    of fork(2).  When are exports neeed?
+
+
+#
+# Testing scoping of varibles in subshells
+#
+
+# Q: are variable in a sub-shell local to that shell?
+unset FOO
+FOO=bar
+function blort {
+    (
+        echo FOO inside blort is $FOO;
+        FOO=foo-inside;
+        echo FOO inside blort is $FOO after setting
+    )
+}
+blort
+echo FOO outside blort after call to baz is $FOO
+# A: yes. FOO did not change outside the function.
+#
+# 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

+ 248 - 0
home/public/snippits/bash/variables-and-scoping.org

@@ -0,0 +1,248 @@
+Some examples of scoping in bash.    Because I forget.   This makes it clear.
+
+
+* Print shell verion
+#+begin_src bash :exports both :results output :session
+date
+echo SHELL is $SHELL
+echo BASH_VERSION is $BASH_VERSION
+#+end_src
+
+#+RESULTS:
+: Thu Nov 17 03:49:42 AM EST 2022
+: SHELL is /bin/bash
+: BASH_VERSION is 5.1.16(1)-release
+
+# echo SHELL is $SHELL
+# echo BASH_VERSION is $BASH_VERSION
+
+* Setup - Don't ask about resetting the session                    :noexport:
+
+  Pay no attention to that man behind the curtain.
+
+  This is emacs lisp foo that turn of prompting when killing the
+  buffer running the shell.
+
+#+BEGIN_SRC elisp
+  (setq kill-buffer-query-functions
+    (remq 'process-kill-buffer-query-function
+           kill-buffer-query-functions))
+#+END_SRC
+
+#+RESULTS:
+
+* Reset the session :results output                                :noexport:
+
+  Kill the TEST session.   Variables, PID should reset.
+
+  https://emacs.stackexchange.com/questions/5293/how-to-force-an-org-babel-session-to-reset-or-initialize
+  #+begin_src bash :exports both :results output :session  (if (get-buffer "TEST") (if (kill-buffer "TEST") (print "TEST") (print "TEST")) (print "TEST"))
+
+  PS1="$ "
+  date
+  echo TEST session killed
+  echo PID is $$
+
+  [[ -v SET_IN_FIRST_BLOCK ]] && \
+    echo SET_IN_FIRST_BLOCK is $SET_IN_FIRST_BLOCK || \
+        echo SET_IN_FIRST_BLOCK is not defined
+  #+end_src
+
+  #+RESULTS:
+  :
+  : gmj@mx:$ Thu Nov 17 02:38:41 AM EST 2022
+  : TEST session killed
+  : PID is 88632
+  : gmj@mx:$ > > SET_IN_FIRST_BLOCK is not defined
+
+
+
+* Test/demonstrate scoping of variables in bash functions
+** Code
+#+begin_src bash :exports both :results output :session
+date
+echo
+
+FOO=FOO-set-outside-functon
+function baz { echo inside baz FOO is $FOO; FOO=FOO-set-inside-inside; }
+baz
+echo FOO is $FOO
+
+#+end_src
+** Conclusions
+   Bash variables are simply global.
+
+
+#+RESULTS:
+: Thu Nov 17 03:44:59 AM EST 2022
+: inside baz FOO is FOO-set-outside-functon
+: FOO is FOO-set-inside-inside
+
+
+* Test/demonstrate scoping of variables in bash functions using subprocesses
+** Code
+#+begin_src bash :exports both :results output :session
+date
+echo
+
+FOO=FOO-set-outside-functon
+function baz { (echo inside baz FOO is $FOO; FOO=FOO-set-inside-inside;) }
+echo
+baz
+echo
+echo FOO is $FOO
+
+#+end_src
+
+#+RESULTS:
+: Thu Nov 17 03:52:35 AM EST 2022
+:
+:
+: inside baz FOO is FOO-set-outside-functon
+:
+: FOO is FOO-set-outside-functon
+
+** Conclusions
+
+   Running in a sub-shell, the function receives copies (via fork(2))
+   of global variables, but then they, and any variables defined in
+   the function remain local to the function.  This is better.   Only
+   downside is creation of a process  ... heavy-weight operation.  OK
+   if not used in heavy processing
+
+
+#+RESULTS:
+: Thu Nov 17 03:44:59 AM EST 2022
+: inside baz FOO is FOO-set-outside-functon
+: FOO is FOO-set-inside-inside
+
+
+
+
+* Test/demonstrate scoping of EXPORT variables in bash
+** Code
+#+begin_src bash :exports both :results output :session
+date
+
+
+# set FOO to known state
+unset FOO
+FOO=bar
+
+# expand FOO in "normal" function
+function f { echo In f, FOO is $FOO; }
+f
+
+# expand FOO in "subprocess" function
+function g { (echo In g, FOO in subprocess is $FOO) }
+g
+
+# expand FOO in a separate command/script
+cat > h <<'EOF'
+#! /bin/bash
+set -u
+[[ -v FOO ]] && echo in h FOO is $FOO || echo in h FOO is not defined
+EOF
+chmod +x h
+./h
+# not defined
+
+# export it, now seen via ENV
+export FOO
+./h
+
+#+end_src
+
+#+RESULTS:
+: Thu Nov 17 04:14:03 AM EST 2022
+: In f, FOO is bar
+: In g, FOO in subprocess is bar
+: in h FOO is not defined
+: in h FOO is bar
+
+** Conclusions
+
+   EXPORTED variables are visible to other commands.
+
+
+
+
+* Raw stuff - move up
+# #
+# # Exporting variables to subshells
+# #
+# $ export -p | grep FOO
+# $ # FOO is not exported
+
+# $ FOO=bar
+# $ export -p | grep FOO
+# $ # FOO is still not exported
+
+# $ export FOO=baz
+# $ export -p | grep FOO
+# declare -x FOO="baz"
+# # Q: but as we see below, non-exported variables are
+# #    inherited by subproceses (probably as a conseuqence
+# #    of fork(2).  When are exports neeed?
+
+
+# #
+# # Testing scoping of varibles in subshells
+# #
+
+# # Q: are variable in a sub-shell local to that shell?
+# unset FOO
+# FOO=bar
+# function blort {
+#     (
+#         echo FOO inside blort is $FOO;
+#         FOO=foo-inside;
+#         echo FOO inside blort is $FOO after setting
+#     )
+# }
+# blort
+# echo FOO outside blort after call to baz is $FOO
+# # A: yes. FOO did not change outside the function.
+# #
+# # 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 -*-

+ 80 - 0
home/public/snippits/grep/grep-patterns.org

@@ -0,0 +1,80 @@
+#+TITLE: Grep Patterns ... things I never knew.
+
+This was an exercise to understand GNU grep regular expressions and
+options.
+
+https://man7.org/linux/man-pages/man1/grep.1.html
+
+#+begin_example sh
+
+#
+# The normal use cases
+#
+
+# A file to grep
+$ cat foo.txt
+foo
+foo foo
+foo bar
+foo bar baz
+foo|bar|baz
+
+# basic regexs (-G) by default. '|' NOT special.
+$ grep 'bar|baz' foo.txt
+foo|bar|baz
+
+# same
+$ grep -e 'bar|baz' foo.txt
+foo|bar|baz
+
+# basic regexps.  '\' to make '|' "special"
+$ grep  'bar\|baz' foo.txt
+foo bar
+foo bar baz
+foo|bar|baz
+
+# extended expressions. '|' IS special.
+$ grep -E 'bar|baz' foo.txt
+foo bar
+foo bar baz
+foo|bar|baz
+
+#
+# The suprizing truth about "-e|--regexp"
+#
+
+# -e (and --regex ) specify PATTERNs, NOT regexps
+#
+# "-e 'bar|baz'" specifies 'bar|baz'  as a PATTERN NOT a regexp.
+#
+# The (non-)interpretation of patterns as regexps
+# comes from the use of -G (default), -E, -F or -P
+#
+# In this example it is a basic regex because -G is implied.
+#
+$ grep -e 'bar|baz' foo.txt
+foo|bar|baz
+
+
+#
+# newlines to separate patterns.  Who knew?
+#
+
+# per the man page:
+# NAME
+#        grep - print lines that match patterns
+#
+# SYNOPSIS
+#        grep [OPTION...] PATTERNS [FILE...]
+#
+# DESCRIPTION
+#        grep  searches  for  PATTERNS  in  each  FILE.  PATTERNS is one or more
+#        patterns separated by newline characters,
+#                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+#
+
+$ grep `printf 'bar\nbaz'` foo.txt
+foo.txt:foo bar
+foo.txt:foo bar baz
+foo.txt:foo|bar|baz
+#+end_example

+ 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

+ 78 - 0
home/public/snippits/org/babel-sh.org

@@ -0,0 +1,78 @@
+Demonstrate use of sessions with bash in org babel.
+
+* Simple command, set a variable
+
+  Get basics about the shell, etc.  Set a varable
+
+#+begin_src bash :exports both :results output :session TEST
+date
+echo SHELL is $SHELL
+echo BASH_VERSION is $BASH_VERSION
+echo PID is $$
+SET_IN_FIRST_BLOCK=something
+#+end_src
+
+#+RESULTS:
+: Wed Nov 16 07:45:29 AM EST 2022
+: SHELL is /bin/bash
+: BASH_VERSION is 5.1.16(1)-release
+: PID is 73788
+
+* Simple command, read variable set
+
+  Get pid. Should be same as previous example.
+
+  See if the variable exists
+
+#+begin_src bash :exports both :results output :session TEST
+date
+echo PID is $$
+[[ -v SET_IN_FIRST_BLOCK ]] && \
+    echo SET_IN_FIRST_BLOCK is $SET_IN_FIRST_BLOCK || \
+        echo SET_IN_FIRST_BLOCK is not defined
+#+end_src
+
+#+RESULTS:
+: Wed Nov 16 07:45:32 AM EST 2022
+: PID is 73788
+: > > SET_IN_FIRST_BLOCK is something
+
+
+* Setup - Don't ask about resetting the session
+
+  Pay no attention to that man behind the curtain.
+
+  This is emacs lisp foo that turn of prompting when killing the
+  buffer running the shell.
+
+#+BEGIN_SRC elisp
+  (setq kill-buffer-query-functions
+    (remq 'process-kill-buffer-query-function
+           kill-buffer-query-functions))
+#+END_SRC
+
+#+RESULTS:
+
+* Reset the session :results output
+
+  Kill the TEST session.   Variables, PID should reset.
+
+  https://emacs.stackexchange.com/questions/5293/how-to-force-an-org-babel-session-to-reset-or-initialize
+  #+begin_src bash :exports both :results output :session  (if (get-buffer "TEST") (if (kill-buffer "TEST") (print "TEST") (print "TEST")) (print "TEST"))
+
+  PS1="$ "
+  date
+  echo TEST session killed
+  echo PID is $$
+
+  [[ -v SET_IN_FIRST_BLOCK ]] && \
+    echo SET_IN_FIRST_BLOCK is $SET_IN_FIRST_BLOCK || \
+        echo SET_IN_FIRST_BLOCK is not defined
+  #+end_src
+
+  #+RESULTS:
+  :
+  : gmj@mx:$ Wed Nov 16 07:45:39 AM EST 2022
+  : TEST session killed
+  : PID is 73860
+  : gmj@mx:$ > > SET_IN_FIRST_BLOCK is not defined

+ 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()

+ 13 - 0
home/public/snippits/python/is_defined.py

@@ -0,0 +1,13 @@
+del foo
+
+try:
+    print(f"Take 1: foo is defined, value {foo}")
+except NameError:
+    print("Take 1: foo isn't defined")
+    foo="foo"
+
+try:
+    print(f"Take 2: foo is defined, value {foo}")
+except NameError:
+    print("Take 2: foo isn't defined")
+    foo="foo"

+ 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 = [

+ 11 - 0
home/public/snippits/sed/sed-example.org

@@ -0,0 +1,11 @@
+#+begin_example
+echo 'abc :results output :eval no' | sed -E 's#.*(results [[:alnum:]]{1,}).*#\1#'
+#+end_example
+
+This demonstrates the following uses of GNU sed:
+
+- Extended regexps :: =-E=
+- Substitution :: =s#OLD#NEW#=
+- Character classes :: =[[:alnum:]]=
+- "bounds" :: ={1,0}= (could be =+=)
+- replacement :: =s#(FOO)#\1#=