Thread: Python snippets

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2

    Python snippets


    Hey, I have a general "best practises" question for you guys.
    I'm playing around a bit, making unittests and python snippets for later use, partly because I want to learn and partly because they might come in handy later.

    So I took an old function I've made, which adds two tuples of the same length together, wrote some tests for it. Then figured out I wanted to be able to add tuples of diffrent lengths together, then figured out i wanted to add lists and tuples together, then I thought why must it be adding, couldn't it be multiplicating if I pass a variable to it. and so on and so on.

    So my question is: Where is the magic border between simple and complex? I've heard in several talks on youtube that functions should only do one thing and be as simple as they can be. When am I crossing the line?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Writing many code snippets helps me learn a computer language and develop techniques that work for me to "get the job done".

    The notion that a great many of these snippets will be useful later, and easier to find and adapt to current requirement than to rewrite hasn't worked well.

    Have you written a catalog indexing system?

    As a FORTRAN programmer using 72 character fixed width source format I adhered to short code lines. For a couple decades now emacs presents 150 characters without wrapping (194 on monitor of the day, a 14" diagonal in a quite readable font). A 72 character rule is gone. FORTRAN still limits me to 132 characters.
    Code:
    function complicated_transformation(arguments):
        delegate_to_subtask
        delegate_to_another_step
        I_m_short
    Splitting tasks into reusable general purpose chunks is the genius of unix, the legacy of Ritchie, Thompson, Kernighan, undoubtedly others. I do not know the historical roots of the unix pipeline. Or were tr, sed, all the core utilities invented by these guys?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    No, I haven't written a catalog index system, I dont really have anough snippets for that yet. Currently reading through all python I've written and checking if there's any useful functions I could use or derive ideas from.

    So I'm just sorting them in categories in a document for now, like "String Handling" and such.

    I use 80 characters myself, I'm used to it and it just works for me. I feel it makes my code easier to read(for the most part).

IMN logo majestic logo threadwatch logo seochat tools logo