python-ubercode-utils 2.0.0__tar.gz → 2.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. python_ubercode_utils-2.0.3/PKG-INFO +29 -0
  2. python_ubercode_utils-2.0.3/python_ubercode_utils.egg-info/PKG-INFO +29 -0
  3. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/setup.py +2 -2
  4. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/test/test_data.py +11 -11
  5. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/ubercode/utils/data.py +4 -4
  6. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/ubercode/utils/dataframe.py +19 -0
  7. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/ubercode/utils/environment.py +59 -0
  8. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/ubercode/utils/logging.py +1 -1
  9. python-ubercode-utils-2.0.0/PKG-INFO +0 -29
  10. python-ubercode-utils-2.0.0/python_ubercode_utils.egg-info/PKG-INFO +0 -29
  11. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/LICENSE +0 -0
  12. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/MANIFEST.in +0 -0
  13. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/README.md +0 -0
  14. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/python_ubercode_utils.egg-info/SOURCES.txt +0 -0
  15. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/python_ubercode_utils.egg-info/dependency_links.txt +0 -0
  16. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/python_ubercode_utils.egg-info/not-zip-safe +0 -0
  17. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/python_ubercode_utils.egg-info/top_level.txt +0 -0
  18. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/setup.cfg +0 -0
  19. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/test/test_convert.py +0 -0
  20. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/test/test_cursor.py +0 -0
  21. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/test/test_dataframe.py +0 -0
  22. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/test/test_environment.py +0 -0
  23. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/test/test_logging.py +0 -0
  24. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/test/test_urls.py +0 -0
  25. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/ubercode/__init__.py +0 -0
  26. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/ubercode/utils/__init__.py +0 -0
  27. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/ubercode/utils/convert.py +0 -0
  28. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/ubercode/utils/cursor.py +0 -0
  29. {python-ubercode-utils-2.0.0 → python_ubercode_utils-2.0.3}/ubercode/utils/urls.py +0 -0
@@ -0,0 +1,29 @@
1
+ Metadata-Version: 2.1
2
+ Name: python_ubercode_utils
3
+ Version: 2.0.3
4
+ Summary: Core python utilities for all apps
5
+ Home-page: https://github.com/sstacha/python-ubercode-utils
6
+ Author: Steve Stacha
7
+ Author-email: sstacha@gmail.com
8
+ License: MIT
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Topic :: Utilities
14
+ Requires-Python: >=3.8
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+
18
+ # python-ubercode-utils
19
+ Extracting common python utilities re-used between all projects. The intent is to have minimal dependencies
20
+ so the library can be used by django settings without circular references. I also have color logging class for
21
+ jupyter notebooks. I will have a couple of libraries that will extend this functionality. Scan the test cases in the
22
+ tests folder for common use cases.
23
+
24
+ python-utils-core:
25
+ - basic conversion helper utilities
26
+ - color logging without dependencies
27
+ - manipulating urls and their parameters
28
+ - helper classes to make working with xml and json data easier
29
+ - minimal helper classes to convert database cursor results to dictionaries or tuples
@@ -0,0 +1,29 @@
1
+ Metadata-Version: 2.1
2
+ Name: python-ubercode-utils
3
+ Version: 2.0.3
4
+ Summary: Core python utilities for all apps
5
+ Home-page: https://github.com/sstacha/python-ubercode-utils
6
+ Author: Steve Stacha
7
+ Author-email: sstacha@gmail.com
8
+ License: MIT
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Topic :: Utilities
14
+ Requires-Python: >=3.8
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+
18
+ # python-ubercode-utils
19
+ Extracting common python utilities re-used between all projects. The intent is to have minimal dependencies
20
+ so the library can be used by django settings without circular references. I also have color logging class for
21
+ jupyter notebooks. I will have a couple of libraries that will extend this functionality. Scan the test cases in the
22
+ tests folder for common use cases.
23
+
24
+ python-utils-core:
25
+ - basic conversion helper utilities
26
+ - color logging without dependencies
27
+ - manipulating urls and their parameters
28
+ - helper classes to make working with xml and json data easier
29
+ - minimal helper classes to convert database cursor results to dictionaries or tuples
@@ -3,8 +3,8 @@ import setuptools
3
3
  with open("README.md", "r") as fh:
4
4
  long_description = fh.read()
5
5
 
6
- setuptools.setup(name='python-ubercode-utils',
7
- version='2.0.0',
6
+ setuptools.setup(name='python_ubercode_utils',
7
+ version='2.0.3',
8
8
  description='Core python utilities for all apps',
9
9
  long_description=long_description,
10
10
  long_description_content_type="text/markdown",
@@ -1,11 +1,11 @@
1
1
  import unittest
2
2
  from pathlib import Path
3
3
 
4
- from ubercode.utils.data import JSON
5
- from ubercode.utils.data import XML
4
+ from ubercode.utils.data import JsonData
5
+ from ubercode.utils.data import XmlData
6
6
 
7
7
 
8
- class TestJSON(unittest.TestCase):
8
+ class TestJsonData(unittest.TestCase):
9
9
 
10
10
  # -------- common usages ----------
11
11
  def test_JSON(self):
@@ -48,10 +48,10 @@ class TestJSON(unittest.TestCase):
48
48
  }
49
49
  """
50
50
  # test we can construct from a json string
51
- json = JSON(json_string=json_string)
51
+ json = JsonData(json_string=json_string)
52
52
  self.assertEqual(len(json.data['people']), 3)
53
53
  # test we can construct by chaining and reading file
54
- json2 = JSON().from_json_file(str(file_path))
54
+ json2 = JsonData().from_json_file(str(file_path))
55
55
  self.assertEqual(json.data, json2.data)
56
56
  # test encoding
57
57
  json_string = """
@@ -89,7 +89,7 @@ class TestJSON(unittest.TestCase):
89
89
  ]
90
90
  }
91
91
  """
92
- json = JSON(json_string=json_string, encode_ampersands=True)
92
+ json = JsonData(json_string=json_string, encode_ampersands=True)
93
93
  self.assertEqual(len(json.data['people']), 3)
94
94
  first_name = json.data['people'][0]['firstName']
95
95
  self.assertEqual(first_name, "Joe & Baker")
@@ -101,7 +101,7 @@ class TestJSON(unittest.TestCase):
101
101
  self.assertEqual(str(json), result)
102
102
 
103
103
 
104
- class TestXML(unittest.TestCase):
104
+ class TestXmlData(unittest.TestCase):
105
105
 
106
106
  # -------- common usages ----------
107
107
  def test_XML(self):
@@ -121,15 +121,15 @@ class TestXML(unittest.TestCase):
121
121
  </contacts>
122
122
  """
123
123
  # test we can construct from an xml string
124
- xml = XML(xml_string=xml_string)
124
+ xml = XmlData(xml_string=xml_string)
125
125
  # NOTE: because we used a multiline string we need to strip the extra newlines before and after <contacts>
126
126
  self.assertEqual(str(xml), xml_string.strip())
127
127
  # normal string doesn't need stripping
128
128
  xml_compact_string = "<contacts><contact><name>Buggs Bunny</name></contact><contact><name>Daffy Duck</name></contact></contacts>"
129
- xml2 = XML(xml_compact_string)
129
+ xml2 = XmlData(xml_compact_string)
130
130
  self.assertEqual(str(xml2), xml_compact_string)
131
131
  # test we can create using the from_xml_string() method chaining
132
- xml3 = XML().from_xml_string(xml_compact_string)
132
+ xml3 = XmlData().from_xml_string(xml_compact_string)
133
133
  self.assertEqual(str(xml2), str(xml3))
134
134
  # test that method chaining after constructor overrides the value in place
135
135
  self.assertNotEqual(str(xml), str(xml2))
@@ -153,7 +153,7 @@ class TestXML(unittest.TestCase):
153
153
  </contact>
154
154
  </contacts>
155
155
  """
156
- xml = XML(xml_string=xml_string, encode_ampersands=True)
156
+ xml = XmlData(xml_string=xml_string, encode_ampersands=True)
157
157
  xml_dict = xml.to_dict()
158
158
  self.assertEqual(xml_dict['contacts']['contact'][0]['@attr'], '1')
159
159
 
@@ -9,7 +9,7 @@ import xml.etree.ElementTree as Etree
9
9
  from collections import defaultdict
10
10
 
11
11
 
12
- class JSON:
12
+ class JsonData:
13
13
  """ simple json class to encapsulate basic json operations """
14
14
  def __init__(self, json_string: str or None = None, encode_ampersands: bool = False):
15
15
  # data is core python objects (list, dict, object, etc) from the core python JSON.loads
@@ -49,7 +49,7 @@ class JSON:
49
49
  return str(self.data)
50
50
 
51
51
 
52
- class XML:
52
+ class XmlData:
53
53
  """ simple xml class to encapsulate basic xml operations using build in python ETree """
54
54
  def __init__(self, xml_string: str or None = None, encode_ampersands: bool = False):
55
55
  # data is core python ElementTree object
@@ -95,7 +95,7 @@ class XML:
95
95
  output to dict
96
96
  :return: dict
97
97
  """
98
- return XML.tree_to_dict(self.data)
98
+ return XmlData.tree_to_dict(self.data)
99
99
 
100
100
  @staticmethod
101
101
  def tree_to_dict(t: Etree) -> dict:
@@ -108,7 +108,7 @@ class XML:
108
108
  children = list(t)
109
109
  if children:
110
110
  dd = defaultdict(list)
111
- for dc in map(XML.tree_to_dict, children):
111
+ for dc in map(XmlData.tree_to_dict, children):
112
112
  for k, v in dc.items():
113
113
  dd[k].append(v)
114
114
  d = {t.tag: {k: v[0] if len(v) == 1 else v for k, v in dd.items()}}
@@ -1,7 +1,26 @@
1
1
  """ common utilities for working with dataframes"""
2
2
  from typing import Any
3
3
  from . import logging
4
+ from datetime import datetime
4
5
 
6
+ default_date_formats = {
7
+ 'date': '%Y-%m-%d',
8
+ 'datetime': '%Y-%m-%d %H:%M:%S',
9
+ 'datetimemilli': '%Y-%m-%d %H:%M:%S.%f'
10
+ }
11
+
12
+ def to_date_str(date_string: str or None, date_col: str, date_field_map: dict, date_formats: dict) -> str or None:
13
+ if date_string == 'None' or date_string == 'NaT' or not date_string:
14
+ return None
15
+ if '.' in date_string:
16
+ dt = datetime.strptime(date_string, date_formats['datetimemilli'])
17
+ elif ':' in date_string:
18
+ dt = datetime.strptime(date_string, date_formats['datetime'])
19
+ else:
20
+ dt = datetime.strptime(date_string, date_formats['date'])
21
+ if not dt:
22
+ return None
23
+ return dt.strftime(date_formats[date_field_map[date_col]])
5
24
 
6
25
  # extend the logging to include log.dataframe()
7
26
  # NOTE: making dataframe type Any, so we don't have to include pandas but intended use is dataframe
@@ -6,6 +6,7 @@ import os
6
6
  import time
7
7
  from datetime import datetime
8
8
  from typing import Any, Tuple
9
+ from pathlib import Path
9
10
  from ubercode.utils.logging import ColorLogger
10
11
  from ubercode.utils import convert
11
12
 
@@ -223,3 +224,61 @@ class Environment:
223
224
  self._logger.warn(
224
225
  f"{db_parts[0]}[{db_parts[1]}][{db_parts[2]}] has a database or property naming issue!")
225
226
  return db_dict
227
+
228
+ class FauxApp:
229
+ def __init__(self, logger: ColorLogger = None, notebook_path: Path = Path(), default_dict: dict = None) -> None:
230
+ self._logger = logger if logger else _utils_settings_logger
231
+ self.notebook_path = notebook_path.resolve()
232
+ self.app_path = os.path.dirname(self.notebook_path)
233
+ self.project_path = os.path.dirname(self.app_path)
234
+ self.instance_path = os.path.join(self.project_path, 'instance')
235
+ self.config = default_dict or dict(
236
+ SECRET_KEY = 'localmachine',
237
+ LOG_LEVEL = 'DEBUG',
238
+ DEBUG = True,
239
+ APP_DIR = self.app_path,
240
+ PROJECT_DIR = self.project_path,
241
+ DATABASE_DEBUG = False,
242
+ SA_URL_APP = f'sqlite+pysqlite:///{os.path.join(self.instance_path, "nbsync.sqlite3")}',
243
+ SA_URL_SRC_LOCAL = f'sqlite+pysqlite:///{os.path.join(self.instance_path, "src.sqlite3")}',
244
+ SA_URL_DST_LOCAL = f'sqlite+pysqlite:///{os.path.join(self.instance_path, "dst.sqlite3")}',
245
+ )
246
+
247
+ def from_mapping(self, mapping: dict) -> None:
248
+ self.config = self.config | mapping
249
+
250
+ def from_pyfile(self, config_file: str = '~/conf/nbsync.cfg') -> None:
251
+ # read the config file into dict if exists then merge
252
+ abs_cfg = os.path.expanduser(config_file)
253
+ try:
254
+ with open(abs_cfg, 'r') as fp:
255
+ for line in fp:
256
+ line = line.strip()
257
+ if line.startswith('#') or not line:
258
+ continue
259
+ # Split only on the first '=' to allow '=' in the value
260
+ try:
261
+ key, val = line.split('=', 1)
262
+ self.config[key.strip().strip("'").strip('"')] = val.strip().strip("'").strip('"')
263
+ except ValueError:
264
+ # Handle lines that might not have an '='
265
+ continue
266
+ except FileNotFoundError:
267
+ self._logger.debug(f'[{config_file}] does not exist')
268
+
269
+ def from_prefixed_env(self, prefix: str = 'UC'):
270
+ # read environment variables with the given prefix and merge into config
271
+ prefix_len = len(prefix) + 1 # +1 for the underscore
272
+ for key, value in os.environ.items():
273
+ if key.startswith(f'{prefix}_'):
274
+ config_key = key[prefix_len:] # remove the prefix and underscore
275
+ self.config[config_key] = value
276
+ # lastly, attempt to convert 'true'/'false' to boolean
277
+ if value.lower() == 'true':
278
+ self.config[config_key] = True
279
+ elif value.lower() == 'false':
280
+ self.config[config_key] = False
281
+
282
+ def __repr__(self):
283
+ return convert.obj_to_str(self)
284
+
@@ -183,7 +183,7 @@ class ColorLogger:
183
183
  c_msg = str(msg)
184
184
  if self.color_output and color:
185
185
  c_msg = color + c_msg + TermColor.ENDC
186
- if msg == self.repeat_msg:
186
+ if str(msg) == self.repeat_msg:
187
187
  # the first time we start repeating track the indent level
188
188
  if not self.repeat_cnt:
189
189
  if indent is not None:
@@ -1,29 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: python-ubercode-utils
3
- Version: 2.0.0
4
- Summary: Core python utilities for all apps
5
- Home-page: https://github.com/sstacha/python-ubercode-utils
6
- Author: Steve Stacha
7
- Author-email: sstacha@gmail.com
8
- License: MIT
9
- Description: # python-ubercode-utils
10
- Extracting common python utilities re-used between all projects. The intent is to have minimal dependencies
11
- so the library can be used by django settings without circular references. I also have color logging class for
12
- jupyter notebooks. I will have a couple of libraries that will extend this functionality. Scan the test cases in the
13
- tests folder for common use cases.
14
-
15
- python-utils-core:
16
- - basic conversion helper utilities
17
- - color logging without dependencies
18
- - manipulating urls and their parameters
19
- - helper classes to make working with xml and json data easier
20
- - minimal helper classes to convert database cursor results to dictionaries or tuples
21
-
22
- Platform: UNKNOWN
23
- Classifier: Development Status :: 3 - Alpha
24
- Classifier: Programming Language :: Python :: 3
25
- Classifier: License :: OSI Approved :: MIT License
26
- Classifier: Operating System :: OS Independent
27
- Classifier: Topic :: Utilities
28
- Requires-Python: >=3.8
29
- Description-Content-Type: text/markdown
@@ -1,29 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: python-ubercode-utils
3
- Version: 2.0.0
4
- Summary: Core python utilities for all apps
5
- Home-page: https://github.com/sstacha/python-ubercode-utils
6
- Author: Steve Stacha
7
- Author-email: sstacha@gmail.com
8
- License: MIT
9
- Description: # python-ubercode-utils
10
- Extracting common python utilities re-used between all projects. The intent is to have minimal dependencies
11
- so the library can be used by django settings without circular references. I also have color logging class for
12
- jupyter notebooks. I will have a couple of libraries that will extend this functionality. Scan the test cases in the
13
- tests folder for common use cases.
14
-
15
- python-utils-core:
16
- - basic conversion helper utilities
17
- - color logging without dependencies
18
- - manipulating urls and their parameters
19
- - helper classes to make working with xml and json data easier
20
- - minimal helper classes to convert database cursor results to dictionaries or tuples
21
-
22
- Platform: UNKNOWN
23
- Classifier: Development Status :: 3 - Alpha
24
- Classifier: Programming Language :: Python :: 3
25
- Classifier: License :: OSI Approved :: MIT License
26
- Classifier: Operating System :: OS Independent
27
- Classifier: Topic :: Utilities
28
- Requires-Python: >=3.8
29
- Description-Content-Type: text/markdown