{"id":1118,"date":"2022-09-19T12:00:00","date_gmt":"2022-09-19T19:00:00","guid":{"rendered":"https:\/\/letsbuildroboticswithshadow8472.com\/?p=1118"},"modified":"2022-09-19T11:00:43","modified_gmt":"2022-09-19T18:00:43","slug":"i-upgraded-my-python-discord-bot-week4","status":"publish","type":"post","link":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/2022\/09\/19\/i-upgraded-my-python-discord-bot-week4\/","title":{"rendered":"I Upgraded My Python Discord Bot (Week4)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Good Morning from my Robotics Lab! This is Shadow_8472 and today, the month of bot development continues. Be sure to catch the previous posts before reading this one. Let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Iteration vs. Recursion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">My first major milestone this week was coding a recursive dice tally function to replace the iterative one. Iteration and recursion are two approaches to solving the same types of problems.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The experience of iteration is the more native one to think in. Your program has a path to walk that loops back in on itself as needed. Its downside is that this path can grow beyond the comprehension of the programmer fairly quickly when using in loops within loops within loops if need be. From experience, I found myself guessing more often than not when I was finishing the iterative tally function, and even then I had to teach myself to indent debug text.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get the idea of recursion, imagine having an army of clones. Each recursion clone has a tiny job, but half that job is working out what it\u2019s supposed to do, and most of the time, that it\u2019s subdividing the problem and summoning one or more clones to work on each one. Recursion has a higher learning curve, but it\u2019s a lot simpler to maintain. I was able to identify and squash a bug involving my pip tally missing secondary pips within seconds to a minute as opposed to fixing the same bug in minutes to an hour and a lot print() statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code Polishing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The more time I spend on this project, the more I discover or rediscover tidier ways to code. It\u2019s not always a hundred lines crunched down into two or three, but it can still feel validating to get a whole chunk of debug code onto a single line I can easily comment out.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In terms of memorable condensations this week, I had two functions \u2013cleanInput() and tokenizeInput() (the later of which underwent around an eighteen:three line reduction last week)\u2013 in my dice engine file used for turning raw input into a format my roller can understand. Upon closer inspection, they combined neatly. Later, I solved a bug introduced when adding support for counting dice sets against the total [subtraction] by re-arranging lines of code from tokenizeInput in before lines from cleanInput.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another thing that\u2019s been bothering me has been the inability to pass off the context from Discord messages to other functions. As far back as the first stint on this bot, I wanted a function so I wouldn\u2019t have to repeat myself in-code when sending a copy of returned messages to the command line. I achieved this with a function that logs, then returns strings I pass it.<\/p>\n\n\n\n<pre class=\"wp-block-verse\">print(\u201cHello World!\u201d)<br>await ctx.send(\u201cHello World!\u201d)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">becomes<\/p>\n\n\n\n<pre class=\"wp-block-verse\">await ctx.send(broadcast(\u201cHello World!\u201d))<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Furthermore, I also played around with the \u2018or\u2019 operator. As I began work on implementing more features, of a trio of lines used for grabbing a user\u2019s server nickname \u2013 or baring one, a user\u2019s global account name. The way Python works, I was able to use<\/p>\n\n\n\n<pre class=\"wp-block-verse\">(ctx.message.author.nick or ctx.message.author.name)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">straight in my broadcast liner function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">My general philosophy for this project has been to come up with a creative approach instead of just patching problems as they pop up. The readability of code will go down sharply as efforts to limit the amount of guesswork patching are neglected. As it stands right now with the next section\u2019s features unfinished, I\u2019d say this should be something I can come back to in a few months and make sense of faster than recoding it from scratch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Pythonic Way<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Change of plan: implementing a save system can wait. While researching save file systems, I happened across an article on askpython.com about so-called \u201cpythonic coding\u201d [1]. Conventions change over time. I\u2019ve noticed this first hand over this project as my style has noticeably drifted as I\u2019ve consulted resources from over the past decade. Looks like I\u2019ll be going through my code again and bringing my code up to standard where I can.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">My first task was converting to using fstrings (f-strings \u2013 I cannot find anyone saying for sure what the f stands for, but I will be thinking of it as \u201cformat\u201d until someone corrects me) when putting variables into strings. It went pretty smoothly, though I did have to be careful about using single vs double quotes when dealing with string data, such as keys in a dictionary. I\u2019m not going for total conversion. If a print statement contains just one string and one variable, I can separate them with a comma. Anything more, and it\u2019s time for fstrings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The enumerate() function for getting loop indexes was one I\u2019ve read about before, but didn\u2019t appreciate until now. Similar to fstrings, enumerate has its place \u2013 specifically anywhere I used pointer variables. Now that I look at my code, my most annoying use of pointers was in that ugly function I\u2019ve already replaced. Nevertheless, I was able to combine a pointer and its supporting code into for-loop statements. Similarly, I employed range() in a while-loop being used for a similar indexing job.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The AskPython article also brought up naming conventions, but I\u2019m putting a hard pass on that one for this project. Variable names are fragile, and only work if they\u2019re all converted at once. I\u2019m not up to that level of potential breakage right now.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Takeaway<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Code takes a lot of work to maintain and ensure readability. Add a seemingly innocent feature or consolidate chunks of code that belonged together all along, and you\u2019re liable for something breaking and going unnoticed. I\u2019ve lost track of all the bugs I\u2019ve squashed, but I try to commit it to Git whenever I have a stable version, documenting my progress since my last commit. I still haven\u2019t looked into using its other features yet, but it\u2019s good practice making improved quality commits, and I know that\u2019s worth something, even if it doesn\u2019t feel like it yet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final Question<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Have you ever taken up [part of] a set of standards for projects, even mid-project?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I look forward hearing your answers on in the comments below or on my <a href=\"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/discord-invite\/\" target=\"_blank\" rel=\"noreferrer noopener\">Discord server<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Work Cited<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">[1] pankaj, \u201cPythonic Way of Writing Code,\u201d [Online]. Available: <a href=\"https:\/\/www.askpython.com\/python\/examples\/pythonic-way-of-coding\">https:\/\/www.askpython.com\/python\/examples\/pythonic-way-of-coding<\/a>. [Accessed Sept. 19, 2022].<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Good Morning from my Robotics Lab! This is Shadow_8472 and today, the month of bot development continues. Be sure to catch the previous posts before reading this one. Let\u2019s get started! Iteration vs. Recursion My first major milestone this week was coding a recursive dice tally function to replace the iterative one. Iteration and recursion &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/2022\/09\/19\/i-upgraded-my-python-discord-bot-week4\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;I Upgraded My Python Discord Bot (Week4)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[92],"tags":[158,157],"class_list":["post-1118","post","type-post","status-publish","format-standard","hentry","category-discord-bot","tag-discord-bot","tag-python"],"_links":{"self":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts\/1118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/comments?post=1118"}],"version-history":[{"count":1,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts\/1118\/revisions"}],"predecessor-version":[{"id":1119,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/posts\/1118\/revisions\/1119"}],"wp:attachment":[{"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/media?parent=1118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/categories?post=1118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/letsbuildroboticswithshadow8472.com\/index.php\/wp-json\/wp\/v2\/tags?post=1118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}