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

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Run Script on Wake


    Hey guys trying to figure out the switch case for when a linux computer wakes/resumes from sleep. This is the bash script equivalent:

    Code:
    #!/bin/bash
    case $1 in
        hibernate)
            echo "Hey guy, we are going to suspend to disk!"
            ;;
        suspend)
            echo "Oh, this time we are doing a suspend to RAM. Cool!"
            ;;
        thaw)
            echo "Oh, suspend to disk is over, we are resuming..."
            ;;
        resume)
            echo "Hey, the suspend to RAM seems to be over..."
            ;;
        *)  echo "Somebody is calling me totally wrong."
            ;;
    esac
    I was seeing if this would work in my case (feeling it won't since python probably doesn't understand, hibernate, suspend, resume, thaw commands)

    Code:
    #!/usr/bin env python
    switch($1) {
      case hibernate:
            print "Hibernate"
            break;
        case suspend:
            print "Suspend"
            break;
        case resume:
            print "Resume"
            break;
        case thaw:
            print "Thaw"
            break;
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    python doesn't have a switch/case statement. Dictionaries are often used instead.

    Here are some runs of this program:

    $ which python3
    /usr/bin/python3
    $ chmod +x p.py
    $ ./p.py nope
    invalid!
    $ ./p.py thaw
    dictionary values can be any object
    $ ./p.py suspend
    suspend
    $ ./p.py
    invalid!
    Code:
    #!/usr/bin/python3
    
    import sys
    
    def thaw_function():
        return 'dictionary values can be any object'
    
    try:
        key = sys.argv[1]
    except IndexError:
        key = 'bad input'
    
    message_dictionary = dict(
        hibernate = 'hibernate', suspend = 'suspend', resume = 'Resume', thaw = thaw_function
        )
    
    if key in message_dictionary:
        message = message_dictionary[key]
    else:
        message = 'invalid!'
    
    
    if type(message) is str:
        print(message)
    elif callable(message):
        print(message())
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    Thank you for the reply. I'm a bit new to Python programming and will need to research the use of dictionaries.

    Would it be possible to explain to me, the use of dictionaries. I'm still a bit lost/confused as to how to properly use dictionaries and how it relates to using bash; scripting language I'm more familiar with.

    Thanks


    Originally Posted by b49P23TIvg
    python doesn't have a switch/case statement. Dictionaries are often used instead.

    Here are some runs of this program:

    $ which python3
    /usr/bin/python3
    $ chmod +x p.py
    $ ./p.py nope
    invalid!
    $ ./p.py thaw
    dictionary values can be any object
    $ ./p.py suspend
    suspend
    $ ./p.py
    invalid!
    Code:
    #!/usr/bin/python3
    
    import sys
    
    def thaw_function():
        return 'dictionary values can be any object'
    
    try:
        key = sys.argv[1]
    except IndexError:
        key = 'bad input'
    
    message_dictionary = dict(
        hibernate = 'hibernate', suspend = 'suspend', resume = 'Resume', thaw = thaw_function
        )
    
    if key in message_dictionary:
        message = message_dictionary[key]
    else:
        message = 'invalid!'
    
    
    if type(message) is str:
        print(message)
    elif callable(message):
        print(message())
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    A dictionary is akin to the gawk associative array.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo