Posts: 122 Threads: 24 Joined: Dec 2017 (Jun-12-2022, 01:56 PM)Larz60+ Wrote: Note: You can eliminate steps 7 and 8 as this will happen the first time MyPaths in imported into any of your scripts. I haven't tested outside of virtual environment, as, frankly, I always use a virtual environment ofr any new project, but this should work without. Thanks, and I understand that if the class 'MyPaths' is included in a Python script it will create the paths if not already present. But how do I use the class in another scripts, without having to have all the code for the class ? For example, in PHP, it is simply an "include" statement. Posts: 12,117 Threads: 494 Joined: Sep 2016 Jun-13-2022, 07:17 AM (This post was last modified: Jun-13-2022, 07:17 AM by Larz60+.) MyPaths.py should be put into a separate module, only one time. It is expected to be imported in any module that needs access to the path structure. I show it as a separate module in post 6 (first one) https://python-forum.io/thread-37451-pos...#pid158233. That code should be saved once as MyPaths.py. (rename, if you wish, filename and class name should be the same then import name needs to be the same as the new class name). Try the example in post 10. It shows exactly how to do this. The MyPaths.py module only has to be coded once, same with the virtual environment, once per project no mater how many modules it contains. There is a tutorial that i wrote, not for this purpose, see SqlPaths.py, and note that it's included in each module. https://python-forum.io/thread-24127.html One great side effect of keeping paths separate is that a path can be changed and all other code will immediately use the new path. Posts: 122 Threads: 24 Joined: Dec 2017 (Jun-13-2022, 07:17 AM)Larz60+ Wrote: MyPaths.py should be put into a separate module, only one time. It is expected to be imported in any module that needs access to the path structure. {snip} Thanks, I missed seeing from MyPaths import MyPaths and mpath = MyPaths() in the post at https://python-forum.io/thread-37451-pos...#pid158233 It works fine, thanks. :) Posts: 12,117 Threads: 494 Joined: Sep 2016 There's also a modification that can be applied that allows you use of this class when your source code spans multiple directories. If you have a need something like this, It's a simple modification and works well, let me know. Posts: 122 Threads: 24 Joined: Dec 2017 (Jun-13-2022, 11:04 AM)Larz60+ Wrote: There's also a modification that can be applied that allows you use of this class when your source code spans multiple directories. If you have a need something like this, It's a simple modification and works well, let me know. Yes thanks, if you can share that. Posts: 12,117 Threads: 494 Joined: Sep 2016 Jun-13-2022, 10:42 PM (This post was last modified: Jun-13-2022, 10:42 PM by Larz60+.) Warning: This is a hack There's probably a better way to do this, but I've used it for years and it suits my purposes: Also, probably won't work on MS Windows since I use symbolic links. Not sure, I'm pretty much A Linux only person. import os from pathlib import Path class MyPaths: def __init__(self, depth=0): os.chdir(os.path.abspath(os.path.dirname(__file__))) dir_depth = abs(depth) HomePath = Path(".") while dir_depth: HomePath = HomePath / ".." dir_depth -= 1 rootpath = HomePath / ".." self.datapath = rootpath / "data" self.datapath.mkdir(exist_ok=True) self.csvpath = self.datapath / "csv" self.csvpath.mkdir(exist_ok=True) self.docpath = rootpath / "docs" self.docpath.mkdir(exist_ok=True) self.jsonpath = self.datapath / "json" self.jsonpath.mkdir(exist_ok=True) self.htmlpath = self.datapath / "html" self.htmlpath.mkdir(exist_ok=True) self.pdfpath = self.datapath / 'PDF' self.pdfpath.mkdir(exist_ok=True) self.tmppath = self.datapath / "tmp" self.tmppath.mkdir(exist_ok=True) if __name__ == "__main__": MyPaths(depth=0)now if you have a source directory like: ├── data │ ├── csv │ ├── PDF │ └── tmp ├── docs ├── src │ ├── ProvingGround │ └── │ ├─ MyProg.py │ └── Create symbolic link to MyPaths.py here └── venv └── ... then to use MyPaths in MyProg.py, instanceiate like mpath = MyPaths(depth=1) if you have more directories below Proving Ground, increase size of depth accordingly EDIT: Note You most likely figured this out, depth is optional, 0 is assumed if not supplied. Posts: 122 Threads: 24 Joined: Dec 2017 (Jun-13-2022, 10:42 PM)Larz60+ Wrote: Warning: This is a hack There's probably a better way to do this, but I've used it for years and it suits my purposes: Also, probably won't work on MS Windows since I use symbolic links. Not sure, I'm pretty much A Linux only person. Great, thanks very much for your help. :) Posts: 7,398 Threads: 123 Joined: Sep 2016 Jun-14-2022, 12:46 AM (This post was last modified: Jun-14-2022, 12:46 AM by snippsat.) jehoshua Wrote:But how do I use the class in another scripts, without having to have all the code for the class ? For example, in PHP, it is simply an "include" statement. You should look at how packages works. Here a post with some examples that i have made. So you can make something homemade that work for you like Larz60+ do,if other shall use you package maybe need an other approach. My goal if make a package is to make import statement simple and work on all OS if other shall use my package. As a example look at Requests on simple import import requests. Here the most useful module are lifted up them import is simple as this. >>> import requests >>> >>> requests.get <function get at 0x000002AA430D5B40> >>> requests.post <function post at 0x000002AA430D5CF0> >>> requests.codes <lookup 'status_codes'> >>> requests.status_codes <module 'requests.status_codes' from 'C:\\Python310...'> >>> requests.head <function head at 0x000002AA430D5C60> Posts: 122 Threads: 24 Joined: Dec 2017 (Jun-14-2022, 12:46 AM)snippsat Wrote: You should look at how packages works. Here a post with some examples that i have made. So you can make something homemade that work for you like Larz60+ do,if other shall use you package maybe need an other approach. {snip} Great, thank you. Some interesting articles and examples to digest. |