Difference between revisions of "Python Code Example Files"
Russ hensel (talk | contribs) (Created page with "= What/Why = When I learn a new thing in Python I often keep an example file. I can then use it for reference and/or copy code out of it for other uses. I have a few "tricks"...") |
Russ hensel (talk | contribs) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
When I learn a new thing in Python I often keep an example file. I can then use it for reference and/or copy code out of it for other uses. I have a few "tricks" that I will describe here. | When I learn a new thing in Python I often keep an example file. I can then use it for reference and/or copy code out of it for other uses. I have a few "tricks" that I will describe here. | ||
I like to have many examples in one file to make them easy to review, and find the right example. Comments can be used to turn on/off some of the code. I use a technique to make it easy to control | I like to have many examples in one file to make them easy to review, and find the right example. Comments can be used to turn on/off some of the code. I use a technique to make it easy to control | ||
− | examples by commenting a single line: put each example in a function and then comment in/out the function call. | + | examples by commenting a single line: put each example in a function and then comment in/out the function call. |
+ | |||
+ | = Moved = | ||
+ | |||
+ | I am beginning to move the actual code to: | ||
+ | |||
+ | *'''[https://github.com/russ-hensel/python_example_code russ-hensel/python_example_code: python_example_code ]''' | ||
+ | |||
+ | an there is a better wiki for this topic there. | ||
= How = | = How = | ||
I think this is best seen with a commented example file. The example is very simple, the central point is the technique of using subroutines. You should be able to copy the code into your IDE of choice and run it. | I think this is best seen with a commented example file. The example is very simple, the central point is the technique of using subroutines. You should be able to copy the code into your IDE of choice and run it. | ||
+ | I am pretty far out of compliancee with PEP 8, but it is my file so I format it the way I want. Run it through Black if you want. Formatting is not the point here. | ||
+ | <pre> | ||
+ | |||
+ | # -*- coding: utf-8 -*- | ||
+ | |||
+ | """" | ||
+ | this is a template/example file for booleans | ||
+ | ( it is commented also as a guide to the subroutine technique for example files ) | ||
+ | |||
+ | |||
+ | In python pretty much everything has a truthiness which is "boolean like"" | ||
+ | |||
+ | |||
+ | Try searching on: | ||
+ | True | ||
+ | truthy | ||
+ | .... | ||
+ | |||
+ | Author: Russ Hensel You can reach me ( EMAIL ) at: | ||
+ | no_spam_please_666 at comcast dot net (REFORMAT FOR A VALID EMAIL ADDRESS). | ||
+ | This address is just for first contact, I will respond with a better | ||
+ | address after first contact. | ||
+ | |||
+ | """ | ||
+ | |||
+ | |||
+ | # a helper function is not an example but will be used by an example | ||
+ | |||
+ | # ------------ helper function ------------------ | ||
+ | def is_truthy( a_value ): | ||
+ | """ | ||
+ | what is says: is a_value truthy or not? | ||
+ | """ | ||
+ | if a_value: | ||
+ | return "truthy" | ||
+ | |||
+ | else: | ||
+ | return "Not truthy" | ||
+ | |||
+ | # ------------ helper function ------------------ | ||
+ | def eval_me( exp ): | ||
+ | """ | ||
+ | print and evalueate exp | ||
+ | """ | ||
+ | # print( msg ) | ||
+ | print( f"eval of {exp} is {eval(exp)}") | ||
+ | |||
+ | |||
+ | |||
+ | # all example functions start with ex_ and right after the function definition | ||
+ | # is a function call. | ||
+ | # try to make name discriptive | ||
+ | |||
+ | |||
+ | # ---------------------------------------- | ||
+ | def ex_is_truthy(): | ||
+ | |||
+ | # I alsays print something so I know which example is running | ||
+ | print( """ | ||
+ | ================ ex_is_truthy(): =============== | ||
+ | test to see what simple expressions have what truthyness | ||
+ | """ ) | ||
+ | |||
+ | exp = "is_truthy( 0 )" | ||
+ | |||
+ | test_me = [ "True", "False", "0", "1", '"TRUE"', '"FALSE"', "1 < 2" ] | ||
+ | |||
+ | for i_test in test_me: | ||
+ | |||
+ | exp = f"is_truthy( {i_test} )" | ||
+ | eval_me( exp) | ||
+ | |||
+ | # call to function, comment out to eliminate the entire example | ||
+ | |||
+ | ex_is_truthy() | ||
+ | |||
+ | |||
+ | # ---------------------------------------- | ||
+ | def ex_case_statement_using_if(): | ||
+ | print( """ | ||
+ | ================ ex_two(): =============== | ||
+ | just show another example | ||
+ | """ ) | ||
+ | |||
+ | x = 3 | ||
+ | |||
+ | if x == 1: | ||
+ | print( "x is one" ) | ||
+ | elif x == 2: | ||
+ | print( "x is two" ) | ||
+ | elif x == 3: | ||
+ | print( "x is three" ) | ||
+ | elif x == 4: | ||
+ | print( "x is four" ) | ||
+ | else: | ||
+ | print( "what else could x be?" ) | ||
+ | |||
+ | #ex_case_statement_using_if() | ||
− | |||
− | + | """ | |
+ | typically I would have many more examples in the file.... | ||
+ | """ | ||
</pre> | </pre> |
Latest revision as of 07:00, 4 February 2023
What/Why[edit]
When I learn a new thing in Python I often keep an example file. I can then use it for reference and/or copy code out of it for other uses. I have a few "tricks" that I will describe here. I like to have many examples in one file to make them easy to review, and find the right example. Comments can be used to turn on/off some of the code. I use a technique to make it easy to control examples by commenting a single line: put each example in a function and then comment in/out the function call.
Moved[edit]
I am beginning to move the actual code to:
an there is a better wiki for this topic there.
How[edit]
I think this is best seen with a commented example file. The example is very simple, the central point is the technique of using subroutines. You should be able to copy the code into your IDE of choice and run it. I am pretty far out of compliancee with PEP 8, but it is my file so I format it the way I want. Run it through Black if you want. Formatting is not the point here.
# -*- coding: utf-8 -*- """" this is a template/example file for booleans ( it is commented also as a guide to the subroutine technique for example files ) In python pretty much everything has a truthiness which is "boolean like"" Try searching on: True truthy .... Author: Russ Hensel You can reach me ( EMAIL ) at: no_spam_please_666 at comcast dot net (REFORMAT FOR A VALID EMAIL ADDRESS). This address is just for first contact, I will respond with a better address after first contact. """ # a helper function is not an example but will be used by an example # ------------ helper function ------------------ def is_truthy( a_value ): """ what is says: is a_value truthy or not? """ if a_value: return "truthy" else: return "Not truthy" # ------------ helper function ------------------ def eval_me( exp ): """ print and evalueate exp """ # print( msg ) print( f"eval of {exp} is {eval(exp)}") # all example functions start with ex_ and right after the function definition # is a function call. # try to make name discriptive # ---------------------------------------- def ex_is_truthy(): # I alsays print something so I know which example is running print( """ ================ ex_is_truthy(): =============== test to see what simple expressions have what truthyness """ ) exp = "is_truthy( 0 )" test_me = [ "True", "False", "0", "1", '"TRUE"', '"FALSE"', "1 < 2" ] for i_test in test_me: exp = f"is_truthy( {i_test} )" eval_me( exp) # call to function, comment out to eliminate the entire example ex_is_truthy() # ---------------------------------------- def ex_case_statement_using_if(): print( """ ================ ex_two(): =============== just show another example """ ) x = 3 if x == 1: print( "x is one" ) elif x == 2: print( "x is two" ) elif x == 3: print( "x is three" ) elif x == 4: print( "x is four" ) else: print( "what else could x be?" ) #ex_case_statement_using_if() """ typically I would have many more examples in the file.... """