[CentralOH] Number of buckets
timothy spencer
smashing_good_show at hotmail.com
Mon Dec 8 16:43:32 CET 2014
I see what you mean by doing nothing gracefully. This is much better and cleaner looking code. This exercise has shown me that I need to get my information and training from a variety of sources.
Aside from his actual code, I think he is presenting the information to his readers to prime us for learning about OOP; getting us accustomed to what the "x.funct(y, z)" and "from X get Y" paradigm mean. I got Lutz's book the other day, so I will be starting that soon enough too.
> From: centraloh-request at python.org
> Subject: CentralOH Digest, Vol 92, Issue 7
> To: centraloh at python.org
> Date: Mon, 8 Dec 2014 12:00:01 +0100
>
> Send CentralOH mailing list submissions to
> centraloh at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/centraloh
> or, via email, send a message with subject or body 'help' to
> centraloh-request at python.org
>
> You can reach the person managing the list at
> centraloh-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of CentralOH digest..."
>
>
> Today's Topics:
>
> 1. The enumerate() solution (timothy spencer)
> 2. Re: The enumerate() solution: Lessons Learned: Rules of Thumb
> (jep200404 at columbus.rr.com)
> 3. Re: Python Parsing Puzzle (Erik Welch)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sun, 7 Dec 2014 09:31:12 -0800
> From: timothy spencer <smashing_good_show at hotmail.com>
> To: "centraloh at python.org" <centraloh at python.org>
> Subject: [CentralOH] The enumerate() solution
> Message-ID: <SNT148-W33A936A66846F6733D10F8CB670 at phx.gbl>
> Content-Type: text/plain; charset="iso-8859-1"
>
> I think I have figured out how to list all the desired elements using enumerate(). I set the number of buckets to 8, and here is what I did:
>
> def dump(aMap):
> for index, bucket in enumerate(aMap):
> for i, slot in enumerate(bucket):
> print(index, i, slot)
>
> What printed out then was the index for the bucket, the index for the slot, and then the key/value pair in the slot. It doesn't appear that the 'if' statements that Zed uses are necessary in my code. I am guessing it's because I am not having 'bucket' printed out in the first for-loop.
>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/centraloh/attachments/20141207/fbf5d081/attachment-0001.html>
>
> ------------------------------
>
> Message: 2
> Date: Sun, 7 Dec 2014 13:42:33 -0500
> From: jep200404 at columbus.rr.com
> To: "Mailing list for Central Ohio Python User Group \(COhPy\)"
> <centraloh at python.org>
> Subject: Re: [CentralOH] The enumerate() solution: Lessons Learned:
> Rules of Thumb
> Message-ID: <20141207134233.6aed8d1f.jep200404 at columbus.rr.com>
> Content-Type: text/plain; charset=US-ASCII
>
> On Sun, 7 Dec 2014 09:31:12 -0800, timothy spencer <smashing_good_show at hotmail.com> wrote:
>
> > def dump(aMap):
> > for index, bucket in enumerate(aMap):
> > for i, slot in enumerate(bucket):
> > print(index, i, slot)
>
> > I think I have figured out how to list all the desired elements
> > using enumerate().
>
> Yes, you did figure it out! Congratulations!
> Your new code is much much better than your earlier code.
>
> There is a programming rule of thumb:
>
> If it seems too complicated, it probably is.
>
> Your old and new code demonstrate this.
>
> > It doesn't appear that the 'if' statements that Zed uses are
> > necessary in my code.
>
> Some if statements are needed, some are not.
> Certainly, you don't need an if statement in your dump().
> It is common to need to do nothing in a program.
> Zed had an unnecessary if statement for such[1]. That was not graceful.
> The code you ended up with _is_ graceful.
> Without any extra code to handle the situation when you
> need to do nothing, your code handles it gracefully.
> Your code illustrates another programming rule of thumb:
>
> Do nothing gracefully.
>
> Python often makes it easy to do nothing gracefully.
> Over time, you should understand more of how Python's
> design often makes it easy to do nothing gracefully.
>
> Earlier[2], I pointed out some weaknesses in ex39_test.py.
>
> On Tue, 25 Nov 2014 09:15:45 -0500, jep200404 at columbus.rr.com wrote:
>
> > Most of the free on-line tutorials have weaknesses.
> > I would say that the documentation is a bit thin,
> > and his code should be refactored. You should always
> > be looking for ways that the code could be improved.
> > What other opportunities for improvement do you see?
>
> His list function is shown as a top level function in
> ex39_test.py. That function clobbers the builtin list
> function. That creeps me out.
>
> Here's another programming rule of thumb:
>
> Don't stop at the first bug.
>
> It's broader than just about actual bugs, it's also about
> ugly awkward code, even if it works. Whenever you see one
> bad thing in code, look for more, especially for similar
> kinds of badness. Programmers tend to be consistent.
> After seeing a few ugly things in someone's code,
> I usually find many more.
>
> > I set the number of buckets to 8, ...
>
> That was good for debugging. It's also good for learning.
> With fewer buckets, the number of collisions increase,
> which is one of the more interesting (albeit undesirable)
> things that dictionaries deal with. That Zed had 256
> buckets is poor, especially since his code is for teaching.
>
> [1] line 62 of ex39_test.py from
> Exercise 39: Dictionaries, Oh Lovely Dictionaries
> http://learnpythonthehardway.org/book/ex39.html
>
> [2] Zed's ex39
> https://mail.python.org/pipermail/centraloh/2014-November/002217.html
>
>
> ------------------------------
>
> Message: 3
> Date: Sun, 7 Dec 2014 14:34:40 -0600
> From: Erik Welch <erik.n.welch at gmail.com>
> To: "Mailing list for Central Ohio Python User Group (COhPy)"
> <centraloh at python.org>
> Subject: Re: [CentralOH] Python Parsing Puzzle
> Message-ID:
> <CAGbNH0p8rYepmB1FwFzV_HNOZjhc_H9FWWh2ksUdtayM5QbkZQ at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Thanks for posting the puzzle, Eric. It's great to see the different
> approaches people are taking.
>
> Here's my solution using regular expressions and pandas (which I'm sure
> could be cleaned up and simplified):
>
> http://nbviewer.ipython.org/url/pastebin.com/raw.php/%3Fi%3DS7fRWFwS
>
> Cheers,
> Erik
>
> On Sat, Dec 6, 2014 at 11:37 PM, Thomas Winningham <winningham at gmail.com>
> wrote:
>
> > I was curious about ast as well. I played with it once one time. So it is
> > true that parsing then shouldn't evaluate the code? I'm trying to think if
> > there are there any other possible "gotchas" ? I guess I should just read
> > more. I always figured things like Hy and PyPy make extensive use of
> > concepts in this general area. I played once on time with parser generators
> > in Java until I found a different solution for whatever I was doing. I
> > guess had I ever taken a compiler class I may know better how to Google
> > these things, or even post about them, heh.
> > On Dec 6, 2014 8:58 PM, "iynaix" <iynaix at gmail.com> wrote:
> >
> >> My solution: http://pastebin.com/nbGprTtV
> >>
> >> Quick explanation:
> >>
> >> As the formulas are basically valid python, minus undefined identifiers,
> >> I sanitize the formulas a little, then feed the formulas into the python
> >> ast module (https://docs.python.org/2/library/ast.html).
> >>
> >> Some quick walking of the parse tree yields the needed numbers and
> >> coefficients, which is then placed into a dict. Parsing an ast does not
> >> trigger an eval, so it should be safe, if that is a concern. After that
> >> it's just python data structure manipulation, pretty straightforward.
> >>
> >> This was pretty fun! Always wanted to play with the python ast module,
> >> but never had a problem to apply it on.
> >>
> >> Cheers,
> >> Xianyi
> >>
> >> On Sun, Dec 7, 2014 at 4:34 AM, <jep200404 at columbus.rr.com> wrote:
> >>
> >>> On Sat, 6 Dec 2014 10:59:21 -0500, Eric Floehr <eric at intellovations.com>
> >>> wrote:
> >>>
> >>> > How would you solve this problem?
> >>>
> >>> Release early, release often: http://colug.net/python/dojo/20141206/
> >>>
> >>> I would normally use grep, awk, sed, awk, tr, and friends,
> >>> but here's some very sloppy Python code to start things.
> >>>
> >>> What program, ala indent or cb, do folks like for cleaning up
> >>> Python code?
> >>> _______________________________________________
> >>> CentralOH mailing list
> >>> CentralOH at python.org
> >>> https://mail.python.org/mailman/listinfo/centraloh
> >>>
> >>
> >>
> >> _______________________________________________
> >> CentralOH mailing list
> >> CentralOH at python.org
> >> https://mail.python.org/mailman/listinfo/centraloh
> >>
> >>
> > _______________________________________________
> > CentralOH mailing list
> > CentralOH at python.org
> > https://mail.python.org/mailman/listinfo/centraloh
> >
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/centraloh/attachments/20141207/170d36f8/attachment-0001.html>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh
>
>
> ------------------------------
>
> End of CentralOH Digest, Vol 92, Issue 7
> ****************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20141208/2942d527/attachment.html>
More information about the CentralOH
mailing list