Browse Source

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

George Jones 1 year ago
parent
commit
db20da8aea

+ 11 - 0
home/public/snippits/python/days_ago.py

@@ -0,0 +1,11 @@
+# Getting dates as a string
+
+from datetime import date, timedelta
+
+def days_ago_string(days_ago: int = 0) -> str:
+  day = date.today() - timedelta(days=days_ago)
+  print(day)
+  return day.strftime('%Y-%m-%d')
+
+
+days_ago_string()

+ 101 - 0
home/public/snippits/python/expired.py

@@ -0,0 +1,101 @@
+#! /usr/bin/env python
+
+"""
+expired - check for expired domain names
+
+By default, take a domain name on the command line.
+
+Print expiration date.
+
+exit 0 none of the domains is expired
+
+exit 1 any of the domains is expired
+
+Usage:
+
+        expired [--all] [--help] [options]  domain
+
+
+"""
+# >>> import whois
+
+
+import whois
+import optparse
+import sys
+from dateutil import parser
+from datetime import date
+from datetime import datetime
+
+__author__ = 'George Jones'
+__maintainer__ = 'George Jones'
+__email__ = 'gmj@pobox.com'
+__version__ = '0.0.1'
+
+#
+# whois
+#
+
+def p_error(msg=None):
+    optp.print_help()
+    if msg:
+        optp.error(msg)
+    sys.exit(1)
+
+def parse_args(argv):
+    global optp
+    usage = """
+        %prog [--all] [options]  name [name...]
+        """
+
+    # Parse arguments.
+    optp = optparse.OptionParser(description=__doc__.strip(), version=__version__,
+                             usage=usage)
+
+    # Parse arguments
+    (opts, args) = optp.parse_args()
+
+    return opts, args
+
+def main():
+    global opts
+    opts, args = parse_args(sys.argv)
+
+    # Check for conflicting options here
+
+    if len(args) == 0:
+        p_error('Must supply at least one domain name')
+
+
+    # today = parser.parse("2010/01/01")
+
+    today = datetime.today()#.strftime('%Y-%m-%d')
+
+
+    any_expired = False
+
+    for arg in args:
+
+        domain = whois.query(arg)
+        dict = domain.__dict__
+
+        for key in dict.keys():
+            value = dict[key]
+            t = type(value)
+            #print(f"{key}: {value} : {t}"  )
+
+            if dict["expiration_date"] < today:
+                print(dict["name"] + " expired")
+                any_expired = True
+            else:
+                print(dict["name"] + " not expired")
+
+            break
+
+    if any_expired:
+        sys.exit(1)
+    else:
+        sys.exit(0)
+
+if __name__ == '__main__':
+    main()

+ 0 - 3
home/public/snippits/python/foo.txt

@@ -1,6 +1,3 @@
 foo
 bar
-foo
 baz
-foo
-

+ 170 - 0
home/public/snippits/python/optparse_example.py

@@ -0,0 +1,170 @@
+#! /usr/bin/env python
+
+"""
+Example of using optparse.
+
+ipAddr - command line wrapper to ipAddr
+
+By default, take a IPv4 addresses or CIDR blocks on command line.
+Print out various selected fields using ipAddr.
+
+Usage:
+
+        ipAddr.py [--all] [--help] [options]  cidr [cidr...]
+
+"""
+import ipaddr
+import optparse
+import sys
+
+__author__ = 'George Jones'
+__maintainer__ = 'George Jones'
+__email__ = 'gmj@pobox.com'
+__version__ = '0.0.1'
+
+#
+# ipAddr calculator
+#
+#
+#
+# or simply
+#
+#   sort -u FOO
+#
+# in python
+
+def p_error(msg=None):
+    optp.print_help()
+    if msg:
+        optp.error(msg)
+    sys.exit(1)
+
+def parse_args(argv):
+    global optp
+    usage = """
+        %prog [--all] [options]  cidr [cidr...]
+        """
+
+    # Parse arguments.
+    optp = optparse.OptionParser(description=__doc__.strip(), version=__version__,
+                             usage=usage)
+
+    # Flag for all known netblock info
+    optp.add_option('-A', '--all', help='Print all fields',
+                action='store_true')
+
+    # Flag for each
+    optp.add_option('-H', '--hostmask', help='print hostask',
+                    action='store_true', default=True)
+
+    optp.add_option('-N', '--netmask', help='print netask',
+                    action='store_true', default=True)
+
+    optp.add_option('-I','--ip', help='print ip', action='store_true', default=True)
+
+    optp.add_option('-K','--is_link_local', help='print is_link_local', action='store_true')
+
+    optp.add_option('-L','--is_loopback', help='print is_loopback', action='store_true')
+
+    optp.add_option('-M','--is_multicast', help='print is_multicast', action='store_true')
+
+    optp.add_option('-P','--is_private', help='print is_private', action='store_true')
+
+    optp.add_option('-R','--is_reserved', help='print is_reserved', action='store_true')
+
+    optp.add_option('-U','--is_unspecified', help='print is_unspecified', action='store_true')
+
+    optp.add_option('-S','--masked', help='print masked', action='store_true')
+
+    optp.add_option('-X','--max_prefixlen', help='print max_prefixlen', action='store_true')
+
+    optp.add_option('-W','--network', help='print network', action='store_true', default=True)
+
+    optp.add_option('-O','--numhosts', help='print numhosts', action='store_true', default=True)
+
+    optp.add_option('-F','--prefixlen', help='print prefixlen', action='store_true', default=True)
+
+    optp.add_option('-B','--subnet', help='print subnet', action='store_true')
+
+    optp.add_option('-T','--supernet', help='print supernet', action='store_true')
+
+    optp.add_option('-V','--ipversion', help='print version', action='store_true', default=True)
+
+    optp.add_option('--with_hostmask', help='print with_hostmask', action='store_true')
+
+    optp.add_option('--with_netmask', help='print with_netmask', action='store_true')
+
+    optp.add_option('--with_prefixlen', help='print with_prefixlen', action='store_true')
+
+
+    # Parse arguments
+    (opts, args) = optp.parse_args()
+
+    return opts, args
+
+def main():
+    global opts
+    opts, args = parse_args(sys.argv)
+
+    # Check for conflicting options here
+
+    if len(args) == 0:
+        p_error('Must supply at least one IP address')
+
+    for arg in args:
+
+        try:
+            net = ipaddr.IPv4Network(arg)
+        except (IPv4IpValidationError, IPv4NetmaskValidationError):
+            pass
+
+        print(("network:", net))
+
+
+        if opts.all or opts.hostmask:
+            print("  hostmask: ", net.hostmask)
+
+        if opts.all or opts.netmask:
+            print("  netmask: ", net.netmask)
+
+        if opts.all or opts.ip:
+            print("  ip:", net.ip)
+
+        if opts.all or opts.is_link_local:
+            print("  is_link_local: ", net.is_link_local)
+
+        if opts.all or opts.is_loopback:
+            print("  is_loopback: ", net.is_loopback)
+
+        if opts.all or opts.is_multicast:
+            print("  is_multicast: ", net.is_multicast)
+
+        if opts.all or opts.is_private:
+            print("  is_private: ", net.is_private)
+
+        if opts.all or opts.is_reserved:
+            print("  is_reserved: ", net.is_reserved)
+
+        if opts.all or opts.is_unspecified:
+            print("  is_unspecified: ", net.is_unspecified)
+
+        if opts.all or opts.max_prefixlen:
+            print("  max_prefixlen: ", net.max_prefixlen)
+
+        if opts.all or opts.network:
+            print("  network: ", net.network)
+
+        if opts.all or opts.numhosts:
+            print("  numhosts: ", net.numhosts)
+
+        if opts.all or opts.prefixlen:
+            print("  prefixlen: ", net.prefixlen)
+
+        if opts.all or opts.ipversion:
+            print("  ipversion: ", net.version)
+
+        if opts.all or opts.with_prefixlen:
+            print("  with_prefixlen: ", net.with_prefixlen)
+
+if __name__ == '__main__':
+    main()

+ 1 - 2
home/public/snippits/python/readAFile.py

@@ -8,5 +8,4 @@ import sys
 with open("foo.txt") as fileHandle:
     for line in fileHandle:
         line = line.strip()
-        print "line is /%s/" % (line)
-       
+        print(f"line is /{line}/")

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

@@ -0,0 +1,65 @@
+#! /usr/bin/env python
+# Sample of reading a CSV file
+#
+# https://stackoverflow.com/questions/41585078/how-do-i-read-and-write-csv-files-with-python
+#
+
+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 = [
+#     (1, "A towel,", 1.0),
+#     (42, " it says, ", 2.0),
+#     (1337, "is about the most ", -1),
+#     (0, "massively useful thing ", 123),
+#     (-2, "an interstellar hitchhiker can have.", 3),
+# ]
+
+# # Write CSV file
+# with open("test.csv", "wt") as fp:
+#     writer = csv.writer(fp, delimiter=",")
+#     # writer.writerow(["your", "header", "foo"])  # write header
+#     writer.writerows(data)
+
+# Read CSV file
+
+for file in files:
+    with open(file) as fp:
+        reader = csv.reader(fp, delimiter=",", quotechar='"')
+        # next(reader, None)  # skip the headers
+        # data_read = [row for row in reader]
+
+        # print(data_read)
+
+        lineno = 0
+        num_column_headrs = 0
+        print_first_n = 5
+
+        for row in reader:
+            lineno += 1
+
+            if lineno == 1:
+                num_column_headers = len(row)
+
+            len_row = len(row)
+            if len_row != num_column_headers:
+                sys.stderr.write(f"Bad coumn count: line {lineno} has {len_row} colums.  Expecting {num_column_headers}\n")
+                print(row)
+                sys.exit()
+
+            if lineno <= print_first_n:
+                print(row)
+
+            if lineno > MAXLINES:
+                break
+
+        sys.stderr.write(f"Done: read {lineno} lines.\n")

+ 11 - 0
home/public/snippits/sql/create_sample_table_inline.sql

@@ -0,0 +1,11 @@
+-- https://stackoverflow.com/questions/58121195/bigquery-create-small-sample-table-all-in-one-query
+
+with foo as (
+     SELECT * FROM UNNEST([
+       STRUCT(1 AS a, 'Alpha' AS b),
+       (2, 'Bravo'),
+       (3, 'Charlie'),
+       (4, 'Delta')
+       ])
+       )
+SELECT * FROM foo

+ 22 - 0
home/public/snippits/sql/split.sql

@@ -0,0 +1,22 @@
+-- Experiments with creating dummy records and splitting strings to create new columns
+--
+-- https://stackoverflow.com/questions/44056274/splitting-substrings-and-creating-new-columns-for-each-in-big-query
+
+-- Create a dummy record
+WITH dummy as (
+    SELECT "foo" as foo, 1 as bar, "a;b;c" as baz
+
+ )
+-- Split out a semicolon separatated field
+ ,splitup as (
+SELECT
+  foo,bar,split(baz,";") as baz_split
+  FROM dummy
+ )
+-- create new columns from the split values
+ SELECT foo,
+   bar,
+   baz_split[OFFSET(0)] AS thing1,
+   baz_split[OFFSET(1)] AS thing2,
+   baz_split[OFFSET(2)] AS thing3
+ from splitup