siteIcon

Tech Novice Tools

Python Source Code

burgerIcon

Shyftgear

Python3 Source Code

Shyft was first written in 2019 in Python3 using the Eclipse IDE. (We have also used Pythonista on the iPad as a GREAT coding environment).

The JavaScript version was developed concurrently in our web-programming classes.

It was considerably easier to write in Python3!

Shyft: Eclipse Python3 Source Code
                            
#--- Header Comments ---
#Date: 9/10/19
#Coder: klp
#Path: 
#File: 
#Purpose: 
#Notes: 
#TODO: 

#--- Imports ---
import datetime #for current time
import getpass  #for username
import os       #for current path
import sys      #for versioning


#--- Global Variables ---

#--- Functions ---

def print_current_user():
    username = getpass.getuser()
    print('Coder: ' + username)
    #https://stackoverflow.com/questions/5137497
    /find-current-directory-and-files-directory
    dir_path = os.path.dirname(os.path.realpath(__file__))
    print(dir_path)
#end print_current_user function
    
def print_current_time():
    now = datetime.datetime.now()
    print(now)
#end print_current_time function
    
def print_simple_heading(heading, symbol, n):
    my_decoration = ''
    for _ in range(n):
        my_decoration += symbol
    my_heading = my_decoration + heading + my_decoration;
    print(my_heading)
#end print_simple_heading function

def make_string_from_char(ch, length):
    temp = ''
    for _ in range(length):
        temp += ch
    return temp
#end make_string_from_char function

#utilize default parameter values
def intro(title="Python App", stageNo='TBD', explanation=''):
    num_char = 5
    my_char = '='
    title = title + ": Stage " + str(stageNo)
    print_simple_heading(title, my_char, num_char)
    print_current_user()
    print_current_time()
    print("Version:", sys.version)
    if len(explanation) > 0:
        print("")
        print(explanation)
    border_length = len(title) + 2*num_char;
    border = make_string_from_char(my_char, border_length)
    print(border)  
#end intro function

#---My Functions---

def translate(msg, shift):
    vowels = "aeiou"
    consonants = "bcdfghjklmnpqrstvwxyz"
    alphabet = vowels + consonants
    alphabet = ''.join(sorted(alphabet))
    translatedMsg = ""
    for symbol in msg:
        tsymbol = symbol.lower()
        shiftedSymbol = symbol
        if tsymbol in alphabet:
            if tsymbol in vowels:
                ndx = vowels.find(tsymbol)
                shiftedNdx = (ndx + shift) % len(vowels)
                shiftedSymbol = vowels[shiftedNdx: shiftedNdx + 1]
            else:
                ndx = consonants.find(tsymbol)
                shiftedNdx = (ndx + shift) % len(consonants)
                shiftedSymbol = consonants[shiftedNdx: shiftedNdx + 1]
        
        if symbol == tsymbol.upper(): #original was upper case
            shiftedSymbol = shiftedSymbol.upper()
        #end alphabet block
        translatedMsg += shiftedSymbol
    #end for loop
    return translatedMsg
#end function translate

#--- Driver Code ---
def main():
    explanation = 
    'This app allows 2-way translation using the same function!'
    intro(stageNo=9, explanation=explanation)
    print("===Our Alien Language App===")

    defaultMsg = "What we learn with pleasure we never forget."
    while True: 
        msg = input("\nYour phrase: ('x' to exit) ==> ")
        if msg.lower() == 'x':
            break;
        if len(msg) == 0:
            msg = defaultMsg
        print("\nOriginal msg: ")
        print(msg)
        shift = input("Your shift: ==> ")
        shift = int(shift)
        print()
        translatedMsg = translate(msg, shift)
        
        print("Translated Message: ")
        print(translatedMsg)
        
        print("\nRe-Translated Message:")
        retranslatedMsg = translate(translatedMsg, -shift)
        print(retranslatedMsg)
    #end while loop
    print()
    print("Thanks for using our app!")
    
#end function main

if __name__ == '__main__':
    main()
    
'''
    =====Python App: Stage 9=====
Coder: kp
/Users/kp/Desktop/2019-2020_Devmt/kp19-HCSF-T1/
Demos/AlienLanguageSagaWS/
AlienLanguagePrj-S8_9
2019-09-10 07:34:15.031860
Version: 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 
[Clang 6.0 (clang-600.0.57)]

This app allows 2-way translation using the same function!
=============================
===Our Alien Language App===

Your phrase: ('x' to exit) ==> 

Original msg: 
What we learn with pleasure we never forget.
Your shift: ==> 6

Translated Message: 
Dpeb di sieyv dobp wsiezayi di viciy muynib.

Re-Translated Message:
What we learn with pleasure we never forget.

Your phrase: ('x' to exit) ==> Hello, World: 
We love computer science with Python!

Original msg: 
Hello, World: We love computer science with Python!
Your shift: ==> -3

Translated Message: 
Dohha, Sanhz: So haro yajleqon pyuokyo suqd Lvqdak!

Re-Translated Message:
Hello, World: We love computer science with Python!

Your phrase: ('x' to exit) ==> x

Thanks for using our app!
'''                        

Last update: 09/18/19