WNYLUG.org

Western New York Linux Users Group

Our next meeting is June 12th at 6″30pm and will be a continued discussion and review of Python development on the Raspberry Pi. This meeting is the third part of this series and will be focusing on GPIO (General Purpose Input/Output).


This description will be updated as the meeting date gets closer.

  • Comments Off
  • Filed under: WNYLUG
  • Part 003 – Control Flow tutorials

    The following is a code snippet showing control flow basics that will be talked about at the upcoming WNYLUG meeting. The code snippet contains a sample if statement, for statement, for statement using the range function and the while statement.

    
    # the if statement
    runIfTest = 0
    
    if runIfTest == 1:
     x = 11
    
    if x < 10:
     print('x is less than 10')
     elif x == 10:
     print('x is equal to 10')
     elif x > 10:
     print('x is greater than 10')
     else:
     print('x is being weird right now')
    
    # the for statement
    runForTest = 0
    
    if runForTest == 1:
     words = ['Calling', 'Western', 'New', 'York', 'Linux', 'User', 'Group', 'Response?']
    
    for word in words:
     print(word)
    
    # the for statement with the range function
    runForRangeTest = 0
    
    if runForRangeTest == 1:
     for i in range(5):
     print(i)
    
    print('******************')
    
    for i in range(5, 10):
     print(i)
    
    print('******************')
    
    for i in range(0, 100, 25):
     print(i)
    
    print('******************')
    
    for i in range(-1, -5, -1):
     print(i)
    
    # the while statement
    runWhileTest = 0
    
    if runWhileTest == 1:
     x = 2
     limit = 100
    
    print('x is equal to ' + str(x))
    
    while x < limit:
     x = x * x
     print('x is now equal to ' + str(x))
     else:
     print('x hit or surpassed the limit of ' + str(limit))
    
    
  • Comments Off
  • Filed under: Python
  • Neal’s Emacs Cheat Sheet

    Emacs is my favorite simple text editor on Linux. To help everyone who is not used to Emacs here is a quick reference list of the commands I often use.

    First some terms.

    1. Files are opened as buffers. When the buffer is saved the file is updated with the changes contained in the buffer. Multiple buffers can be open.
    2. A window is viewer for display and editing a buffer. Multiple windows can be shown.

    Quick reference.

    1. open file: ctrl+x ctrl+f
    2. close file: ctrl+x k (press enter when prompted to close)
    3. save file: ctrl+x ctrl+s
    4. quit Emacs: ctrl+x ctrl+c
    5. split window into two windows top/bottom: ctrl+x 2
    6. split window into two windows left/right: ctrl+x 3
    7. close other split windows: ctrl+x 1
    8. move to next window: ctrl+x o
    9. list buffers: ctrl+x ctrl+b
    10. open Emacs shell: alt+x eshell
  • Comments Off
  • Filed under: WNYLUG
  • This post will be the first of many that will be following my adventures with developing code in Python on the Raspberry Pi. The intent of this series is to provide myself with motivation for making progress with my Raspberry Pi projects but also to provide material for a series of WNYLUG meetings. During this series I hope to gain more knowledge of Python development and share that knowledge with the WNYLUG members and anyone else that cares. Other topics will get discussed along the way such as Mercurial usage, Emacs usage, basic circuit design and whatever else comes up during the journey.

    Let’s begin!

    Part 001 – Hello World and source control

    To start with I needed to refresh my Python skills with some basic tutorials but before any coding could be done I needed to setup source control tools and an editor. There is nothing worse than manually versioning code, well losing your code is worse so source control!

    My preferred source control tool is Mercurial. A quick apt-get installed the tools for me.

    $ sudo apt-get install mercurial

    And there was no way that I would be doing extensive coding in nano so I needed to install Emacs. (Yes, I am an Emacs guy, sorry, deal with it, I love it, blame UB, thanks UB!)

    $ sudo apt-get install emacs

    Some quick Mercurial setup, I created the Mercurial configuration file in my home directory and added my username.

    $ emacs ~/.hgrc

    Added the following text to the file.

    [ui]
    username = Neal Chapman <my email address>

    And then saved the file and exited Emacs by pressing ctrl+x ctrl+c and pressing enter when prompted to save my file.

    I then created a series of folders in my home directory to hold my code and the repository that would be storing my code history.

    $ mkdir ~/projects

    $ mkdir ~/projects/python_playground

    I then initialized a repository in what would be the root folder for my code files.

    $ cd ~/projects/python_playground

    $ hg init

    And finally, it was time for some Hello World action!

    I prefer to write Python in script files and execute the script files as opposed to just playing in the command line interpreter. So I created my Hello World script file with Emacs.

    $ emacs hello_world.py

    In Emacs I entered a very simple single line of code.

    print("Hello World!")
    

    I then saved my file by pressing ctrl+x ctrl+s and then closed Emacs by pressing ctrl+x ctrl+c (this will be my last mention of how to save files and close Emacs).

    And the moment of truth, execution. To execute the my newly created Python script I ran the following command.

    $ python3 hello_world.py

    And the output was amazingly.

    Hello World!

    Simply put, the print function writes to standard output so this Python script is writing the string “Hello World!” to standard output. Working code is a beautiful thing but I couldn’t bask in my glory just yet, I had to commit my newly created file to source control. To commit my newly created, untracked file to the repository I first ran the add command.

    $ hg add

    I then committed the file with a commit message.

    $ hg commit -m “Committed hello world file.”

    OK, the code is safe in the repository until the next tutorial…

    Part 002 – Tutorials Abound

    So back into the tutorials and basics needed to be covered. Math, control flow, data structures, IO, exception handling and what not.

    I started with some basic math. I created a file named basic_math.py which contained the following lines.

    print(2)
    print("+")
    print(2)
    print("is")
    print(2 + 2)
    

    I then executed the file which generated the following output.

    2
    +
    2
    is
    4

    After adding that file to the Mercurial source control repository I made a small change to the file changing the addition to division.

    print(2)
    print("/")
    print(2)
    print("is")
    print(2 / 2)
    

    Which when executed gave me an output of.

    2
    /
    2
    is
    1

    So we can see that the print command can take more than just strings, it cause take expressions and evaluate the expression. To consolidate the code in preparation for several math samples I changed my script to this.

    print("math sample - addition")
    print("2 + 2 = " + str(2 + 2))
    print("math sample - division")
    print("2 / 2 = " + str(2 / 2))
    

    Which when executed gave me.

    math sample – addition
    2 + 2 = 4
    math sample – division
    2 / 2 = 1

    In this code the str function evaluates the passed in expression and attempts to convert it to a string. The static string and the string returned from str are then concatenated together and printed to standard output.

    The next set of samples used more advanced math functions that required the import of the math module. Changing my math script to the following.

    import math
    print("math sample - addition")
    print("2 + 2 = " + str(2 + 2))
    print("math sample - division")
    print("2 / 2 = " + str(2 / 2))
    print("math sample - power")
    print("2^2 = " + str(2**2))
    print("2^3 = " + str(2**3))
    print("2^4 = " + str(2**4))
    print("math sample - ceiling and floor")
    print("the ceiling of 2.375 = " + str(math.ceil(2.375)))
    print("the floor of 2.375 = " + str(math.floor(2.375)))
    

    Produced the following output.

    math sample – addition
    2 + 2 = 4
    math sample – division
    2 / 2 = 1
    math sample – power
    2^2 = 4
    2^3 = 8
    2^4 = 16
    math sample – ceiling and floor
    the ceiling of 2.375 = 3.0
    the floor of 2.375 = 2.0

    Enough math, onto control flow. Control flow is code that allows your code to make decisions. The following script asks the user for input, converts that input to an integer and saves it in a variable named x. It then evaluations that value and prints a string based on the value.

    x = int(input("Enter a number between 0 and 100: "))
    if x < 0:
      print("You failed and entered a number less 0, smooth move.")
    elif x > 100:
      print("This number is greater than 100, what are you doing?")
    else:
      print("You can follow directions! You entered: " + str(x))
    

    Here is one possible output path based on the user inputting a value of 34.

    $ python3 control_flow_basics.py
    Enter a number between 0 and 100: 34
    You can follow directions! You entered: 34

    I then added another form of control flow, looping using while. The following code will ask the user for a number and return the square root of that number until the user presses “q” to quit.

    import math
    x = input("Enter a number to return the square root, press q to quit: ")
    
    while x != "q":
      if x.isdigit():
        print("The square root of " + x + " is " + str(math.sqrt(int(x))))
      else:
        print("You entered an invalid input '" + x + "' please try agin")
    
    x = input("Enter a number to return the square root, press q to quit: ")
    
    print("Thanks for playing!")
    

    Output of a sample execution.

    $ python3 control_flow_basics.py
    Enter a number to return the square root, press q to quit: 81
    The square root of 81 is 9.0
    Enter a number to return the square root, press q to quit: 33
    The square root of 33 is 5.744562646538029
    Enter a number to return the square root, press q to quit: break it
    You entered an invalid input ‘break it’ please try agin
    Enter a number to return the square root, press q to quit: q
    Thanks for playing!

  • Comments Off
  • Filed under: Python, Raspberry Pi, Source Control, WNYLUG