Python – Test for Anagram

email me

def is_anagram(input1, input2):
input1 = input1.lower()
input2 = input2.lower()
return sorted(input1) == sorted(input2)

print("\x1b[2J")

print("iceman cinema")
print(is_anagram("iceman", "cinema"))
print "\n"

print("leaf tree")
print(is_anagram("leaf", "tree"))
print "\n"


Output

Python – Read Lines from Text File

email me

Tested on a Mac, 10.12.6.

import os.path

textFile = "/Users/Shared/TextFile.txt"
doesFileExist=os.path.isfile(textFile)
if doesFileExist == True:
file = open(textFile,"r")
lines=file.read().splitlines()
global line1
global line2
line1 = lines[0]
line2 = lines[1]
file.close()

 

 

Notes

https://docs.python.org/3/

 

Mode Description
‘r’ This is the default mode. It Opens file for reading.
‘w’ This Mode Opens file for writing.
If file does not exist, it creates a new file.
If file exists it truncates the file.
‘x’ Creates a new file. If file already exists, the operation fails.
‘a’ Open file in append mode.
If file does not exist, it creates a new file.
‘t’ This is the default mode. It opens in text mode.
‘b’ This opens in binary mode.
‘+’ This will open a file for reading and writing (updating)

Mac – Create a Daemon to Load App

email me

Step 1 – Create a plist file. Save as something like com.TheAppName.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>app.TheAppName</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/open</string>
<string>/Applications/TheAppName.app</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>LaunchOnlyOnce</key>
<false/>
<key>StartInterval</key>
<integer>3600</integer><!-- seconds -->
</dict>
</plist>

Step 2 – Copy plist to /System/Library/LaunchDaemons/

Step 3 – Load the daemon

sudo launchctl load -w /System/Library/LaunchDaemons/com.TheAppName.plist

Step 4 – Log off & Log on to test