This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Exercise 13 (and Solution) | |
#Write a program that asks the user how many Fibonnaci numbers to generate and then generates them. Take this opportunity to think about how you can use functions. Make sure to ask the user to enter the number of numbers in the sequence to generate.(Hint: The Fibonnaci seqence is a sequence of numbers where the next number in the sequence is the sum of the previous two numbers in the sequence. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, …) | |
def fib(n): | |
a = [] | |
if n == 1: | |
a.append(1) | |
return a | |
else: | |
a = [1, 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
def writeToJSONFile(path, fileName, data): | |
filePathNameWExt = './' + path + '/' + fileName + '.json' | |
with open(filePathNameWExt, 'w') as fp: | |
json.dump(data, fp) | |
# Example | |
data = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
PWD = os.getcwd() | |
my_file = os.path.join(PWD, 'data.txt') | |
with open(my_file) as f: | |
for line in f: | |
print(line) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
def isActive(daemon): | |
command = "systemctl is-active " + daemon + " > tmp" | |
os.system(command) | |
with open('tmp') as tmp: | |
tmp = tmp.read() | |
if "active" in tmp: | |
os.remove('tmp') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import subprocess | |
PWD = os.getcwd() | |
file_name = 'data.txt' | |
file_path = os.path.join(PWD, file_name) | |
def replaceTextInFile(file_path, oldText, newText): | |
# Read in the file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import json | |
import os | |
PWD = os.getcwd() | |
file_path = os.path.join(PWD, 'distros.js') | |
with open(file_path) as f: | |
distros_dict = json.load(f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prRed(prt): print("\033[91m {}\033[00m" .format(prt)) | |
def prGreen(prt): print("\033[92m {}\033[00m" .format(prt)) | |
def prYellow(prt): print("\033[93m {}\033[00m" .format(prt)) | |
def prLightPurple(prt): print("\033[94m {}\033[00m" .format(prt)) | |
def prPurple(prt): print("\033[95m {}\033[00m" .format(prt)) | |
def prCyan(prt): print("\033[96m {}\033[00m" .format(prt)) | |
def prLightGray(prt): print("\033[97m {}\033[00m" .format(prt)) | |
def prBlack(prt): print("\033[98m {}\033[00m" .format(prt)) | |
prGreen("Hello world") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
SSH_PUBLIC_KEY='insert_your_ssh_public_key_here' | |
function add_ssh_public_key() { | |
cd | |
mkdir -p .ssh | |
chmod 700 .ssh | |
echo "$SSH_PUBLIC_KEY" >> .ssh/authorized_keys | |
chmod 600 .ssh/authorized_keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
def getIp(): | |
import socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.connect(("google.com",80)) | |
out = (s.getsockname()[0]) | |
s.close() | |
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
print(sys.argv) | |
------- | |
import argparse | |
parser = argparse.ArgumentParser("simple_example") | |
parser.add_argument("counter", help="An integer will be increased by 1 and printed.", type=int) | |
args = parser.parse_args() | |
print(args.counter + 1) |
OlderNewer