{"id":1177,"date":"2024-03-05T05:28:23","date_gmt":"2024-03-05T05:28:23","guid":{"rendered":"https:\/\/snersbots.co.uk\/?p=1177"},"modified":"2024-03-05T05:30:50","modified_gmt":"2024-03-05T05:30:50","slug":"photo-app-add-a-module","status":"publish","type":"post","link":"https:\/\/snersbots.co.uk\/index.php\/photo-app-add-a-module\/","title":{"rendered":"Photo App &#8211; Add a Module"},"content":{"rendered":"\n<p>As I start adding more code, I want to separate different functionality into different python files. In Python, you call these separate files modules. I need to learn how to use modules in Python.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>The main script has got all the wxPython code. That probably needs to be separated out as well in due course. Using the <a href=\"https:\/\/en.wikipedia.org\/wiki\/SOLID\">SOLID<\/a> principle, each class should have a single responsibility. I need to add the code to walk through the files, but this needs to be in a separate class, and a separate python file.<\/p>\n\n\n\n<p>I&#8217;ve taken the code that walks through the files, and made a new class called FileWalker. It doesn&#8217;t have to be a class, but if you&#8217;re going to separate things out, a class seems to be the logical way to go. At this point I&#8217;m done with snake case. At first I thought it might be the &#8216;Python way&#8217; but there is so much code out there in both, I&#8217;m going to move back to my happy place.<\/p>\n\n\n\n<p>The watchout here, if you aren&#8217;t used to Python &#8211; the first parameter in a class method must be &#8216;self&#8217;, otherwise you will get some very strange error messages.<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#e9f1f7\"><code>import os\n\nclass FileWalker:\n  def walkThroughFiles(self, inPath, extensions = (\".jpg\", \".jpeg\")):\n    for (dirpath, dirnames, filenames) in os.walk(inPath):\n        for filename in filenames:\n            if filename.lower().endswith(extensions):\n              yield (filename, os.path.join(dirpath, filename))\n<\/code><\/pre>\n\n\n\n<p>This is all in a file called &#8216;traverseFiles.py&#8217; in the same folder as all the other python scripts.<\/p>\n\n\n\n<p>This post at <a href=\"https:\/\/www.makeuseof.com\/create-import-reuse-module-python\/\">Make Use Of<\/a> explained very nicely how to use code that is sitting in another file, in another &#8216;module&#8217;. <\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#e9f1f7\"><code>from traverseFiles import FileWalker\n...\ndef onCollectClicked(self, event):\n        fileWalker = FileWalker()\n        for fname in fileWalker.walkThroughFiles(self.photoFolder.GetValue()):\n            print(fname)<\/code><\/pre>\n\n\n\n<p>This is nice and tidy, and apart from the indents and colons, it&#8217;s starting to look more like other languages now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I start adding more code, I want to separate different functionality into different python files. In Python, you call these separate files modules. I need to learn how to use modules in Python.<\/p>\n","protected":false},"author":1,"featured_media":1184,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,8],"tags":[69,71],"class_list":["post-1177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-desktop-apps","category-software","tag-duplicate-photos","tag-python"],"_links":{"self":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=1177"}],"version-history":[{"count":7,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1177\/revisions"}],"predecessor-version":[{"id":1186,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1177\/revisions\/1186"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media\/1184"}],"wp:attachment":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=1177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=1177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=1177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}