Editing Russ Python Tips and Techniques

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 7: Line 7:
 
* when division by 0 is useful
 
* when division by 0 is useful
 
* code is multiple files - scratch file
 
* code is multiple files - scratch file
 +
* msg, print( msg )
 
* moving to 3.6 use fstrings
 
* moving to 3.6 use fstrings
 
* example files with functions  [[Python Example Code]]
 
* example files with functions  [[Python Example Code]]
Line 12: Line 13:
 
* dict based case statements
 
* dict based case statements
 
* choose names for cut and paste
 
* choose names for cut and paste
 +
* spyder using comments for navigation
 
* mark your code with unusual string  * zzz in your code  
 
* mark your code with unusual string  * zzz in your code  
 
+
* make your names searchable
  
 
=== Move these to a good place in the outline ===
 
=== Move these to a good place in the outline ===
 
==== Python Installation ====
 
 
I have a separate page [[Category:Python Installation]] about installation an maintenance, the first part is of general interest, the second on particular issues I have encountered.  It may be useful to get some of my projects running [[Category:Python Projects]].
 
 
 
==== FStrings ====
 
 
 
FStrings are incredibly nice. New in 3.6, use it ( or 3.7.... ) They also are reputed to be really fast.  Use them.  At a later time I may have a bunch of examples, but for now just the basic statement:
 
 
<pre>
 
    joe  = "Joe Smith Jones Norton"
 
    msg  = f"Joe's name is actually {joe}"
 
    print( msg 0
 
</pre>
 
  
 
==== Comments for Navigation ====
 
==== Comments for Navigation ====
Line 62: Line 48:
 
If you use print for debugging you may want to scan your code of "print" to remove unwanted statements.  I do not delete them because sometimes I want them again for more debugging.  I change "print" to "#rint"
 
If you use print for debugging you may want to scan your code of "print" to remove unwanted statements.  I do not delete them because sometimes I want them again for more debugging.  I change "print" to "#rint"
 
to comment out the print but to stop it form showing up on searches.
 
to comment out the print but to stop it form showing up on searches.
 
 
==== Make Code Searchable ====
 
 
I like generic names to enable cut and paste but this runs against the idea of searchable code.  The idea is that unusual names are easy to search for.  It also helps if names are descriptive.  Why searchable? So you can find the code you want to work on.  One way to be both searchable and generic is to take instance references and make local ( or the reverse )  For example consider this tkinter code:
 
 
 
<pre>
 
        a_list    = list(self.sort_order_dict.keys())
 
        a_widget  =  ttk.Combobox( a_frame, values = a_list,  state='readonly')
 
        a_widget.grid( row = lrow + 1, column = lcol, rowspan = 1, columnspan = 3,  sticky = E + W + N + S )
 
        a_widget.set( a_list[0] )
 
        self.sort_order_widget = a_widget
 
        lcol  += 3
 
</pre>
 
 
The name a_widget is generic, but to see what it "really" is you have the none generic name "self.sort_order_widget"
 
 
If you do not see an easy way to do this with names use a comment.  Using zzzz.... is a short term way of doing this.
 
 
==== Dict Based Case Statements ====
 
 
 
Python does not have a case statement, but it does have if....elif which can be used and is only a little more verbose.  However if you have more than a few cases it can be cumbersome.  Using a dict gives a very compact and fast case statement.  Suppose you want to have a case statement based on strings:
 
 
 
<pre>
 
    # set up, ideally only done onece, perhaps at module or class level
 
    dispatch_dict  = { "one":    print_one,
 
                        "two":    print_two,
 
                        "three":  print_three,
 
                        }
 
 
    # set a case for an example call:
 
    key = "two"
 
 
    # the slightly verbose case statement
 
    function      = dispatch_dict[ key ]
 
    function()
 
</pre>
 
  
 
= Parameters for Applications =
 
= Parameters for Applications =
Line 206: Line 152:
  
 
= Copy and Paste =
 
= Copy and Paste =
some content may move to [[Copy and Paste Is Good]] or not
 
 
  
 
There are lots of arguments against copy and paste, but used carefully I think that it is extremely useful.  But used carefully.
 
There are lots of arguments against copy and paste, but used carefully I think that it is extremely useful.  But used carefully.

Please note that all contributions to OpenCircuits may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see OpenCircuits:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)