Fred Damstra [afs macbook] 3 years ago
parent
commit
19d2b4a0a8
4 changed files with 36 additions and 2 deletions
  1. 2 2
      examples/argparse_example.py
  2. 10 0
      examples/file.py
  3. 17 0
      examples/logging.py
  4. 7 0
      examples/regular_expressions.py

+ 2 - 2
examples/argparse_example.py

@@ -5,9 +5,9 @@ import argparse
 
 
 if __name__ == "__main__":
-    parser = argparse.ArgumentParser()
+    parser = argparse.ArgumentParser(description='A program that shows how argparse works'))
     parser.add_argument('printstring', help='A positional argument of a string to print')
-    parser.add_argument('--square', help='Print the square of this number.', type=int)
+    parser.add_argument('-s', '--square', help='Print the square of this number.', type=int)
     parser.add_argument('--cube', help='Print the cube of this number, or else the cube of 11.', type=int, default=11)
     parser.add_argument('--oneorzero', help='Only accepts a 1 or a 0', type=int, choices=[0, 1])
     parser.add_argument('--verbose', help='Verbose output.', action='store_true')

+ 10 - 0
examples/file.py

@@ -0,0 +1,10 @@
+
+
+try:
+    with open(file, "r") as configfile:
+        for line in configfile:
+            print line
+
+
+except:
+    print(f'Could not open file {file} for reading.')

+ 17 - 0
examples/logging.py

@@ -0,0 +1,17 @@
+import logging
+
+logger = logging.getLogger()
+
+# Log to stdout:
+handler = logging.StreamHandler()
+logger.addHandler(handler)
+
+# Do not log aws
+# Turn off debugging for AWS
+for module in [ 'boto3', 'botocore', 'nose', 's3transfer', 'urllib3', 'urllib3.connectionpool' ]:
+        l = logging.getLogger(module)
+        l.setLevel(logging.INFO)
+
+logger.setLevel(logging.DEBUG) # Debug
+logger.debug(f'Profiles to gather: {json.dumps(profiles)}')
+

+ 7 - 0
examples/regular_expressions.py

@@ -0,0 +1,7 @@
+import re
+
+match = re.search('^\[profile (.*)\]', line)
+if match:
+    # We have a match
+    if 'test' in match.group(1):
+        # the match includes the string 'test'