import-parent 0.0.1__py3-none-any.whl

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.
@@ -0,0 +1,29 @@
1
+ Metadata-Version: 2.4
2
+ Name: import_parent
3
+ Version: 0.0.1
4
+ Summary: Python package that allows a user to easily import local functions from parent folders.
5
+ Author-email: Michael Boerman <michaelboerman@hey.com>
6
+ Project-URL: Homepage, https://github.com/michaelboerman/import_parent
7
+ Project-URL: Issues, https://github.com/michaelboerman/import_parent/issues
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.8
12
+ Description-Content-Type: text/markdown
13
+
14
+ # import-parent
15
+
16
+ A small utility for importing Python modules using paths relative to the
17
+ calling script — without modifying your project structure.
18
+
19
+ Inspired by R's `here()`.
20
+
21
+ ---
22
+
23
+ ## Installation
24
+
25
+ `pip install import-parent`
26
+
27
+ ## Usage
28
+
29
+ `from import_parent import import_parent`
@@ -0,0 +1,5 @@
1
+ src.py,sha256=iQyUkuXY1FXlxDWoOIAy4KMPpP2lvihtXOZhXBowEVg,1457
2
+ import_parent-0.0.1.dist-info/METADATA,sha256=H2EUPfoXmNrN5QfHy2hpo94k_NUmKwt6J28A_eHtqvc,848
3
+ import_parent-0.0.1.dist-info/WHEEL,sha256=YCfwYGOYMi5Jhw2fU4yNgwErybb2IX5PEwBKV4ZbdBo,91
4
+ import_parent-0.0.1.dist-info/top_level.txt,sha256=74rtVfumQlgAPzR5_2CgYN24MB0XARCg0t-gzk6gTrM,4
5
+ import_parent-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ src
src.py ADDED
@@ -0,0 +1,58 @@
1
+ import os
2
+ import sys
3
+ import inspect
4
+ from typing import Union
5
+
6
+
7
+ def _get_caller_dir(stack_level: int = 2) -> str:
8
+ frame = inspect.stack()[stack_level]
9
+ caller_file = frame.filename
10
+ return os.path.dirname(os.path.abspath(caller_file))
11
+
12
+
13
+ def _count_levels(path: str) -> int:
14
+ parts = [p for p in path.replace("\\", "/").split("/") if p]
15
+ levels = sum(1 for p in parts if p == "..")
16
+
17
+ if levels == 0:
18
+ raise ValueError("Path must contain at least one '..'")
19
+
20
+ return levels
21
+
22
+
23
+ def import_parent(path: Union[str, int] = "..") -> str:
24
+ """
25
+ Add a parent directory (relative to the caller) to sys.path.
26
+
27
+ Parameters
28
+ ----------
29
+ path : str or int
30
+ '../..' style path or an integer number of levels.
31
+
32
+ Returns
33
+ -------
34
+ str
35
+ The directory added to sys.path
36
+ """
37
+ if isinstance(path, int):
38
+ if path <= 0:
39
+ raise ValueError("Integer path must be > 0")
40
+ levels_up = path
41
+ elif isinstance(path, str):
42
+ levels_up = _count_levels(path)
43
+ else:
44
+ raise TypeError("path must be a string or int")
45
+
46
+ caller_dir = _get_caller_dir()
47
+
48
+ target_dir = caller_dir
49
+ for _ in range(levels_up):
50
+ target_dir = os.path.dirname(target_dir)
51
+
52
+ if not os.path.isdir(target_dir):
53
+ raise FileNotFoundError(f"Resolved path does not exist: {target_dir}")
54
+
55
+ if target_dir not in sys.path:
56
+ sys.path.insert(0, target_dir)
57
+
58
+ return target_dir