Skip to content

Instantly share code, notes, and snippets.

View phanthaihuan's full-sized avatar

Huan phanthaihuan

  • Ho Chi Minh
View GitHub Profile
#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]
@phanthaihuan
phanthaihuan / save-file.py
Created February 24, 2020 15:33 — forked from keithweaver/save-file.py
Save JSON file with Python
import json
def writeToJSONFile(path, fileName, data):
filePathNameWExt = './' + path + '/' + fileName + '.json'
with open(filePathNameWExt, 'w') as fp:
json.dump(data, fp)
# Example
data = {}
@phanthaihuan
phanthaihuan / readFile.py
Created May 13, 2020 14:15
How to read file in Python
#!/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)
@phanthaihuan
phanthaihuan / getOutput.py
Created May 13, 2020 15:32
Get the output of Linux command
#!/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')
@phanthaihuan
phanthaihuan / replaceTextInFile.py
Created May 13, 2020 15:56
How to replace text in file with Python
#!/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
@phanthaihuan
phanthaihuan / readJson.py
Created May 13, 2020 16:33
How to parse the content from Json with Python
#!/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)
@phanthaihuan
phanthaihuan / coloredText.py
Created May 13, 2020 16:44
How to print colored text to terminal in Python
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")
#!/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
@phanthaihuan
phanthaihuan / getIp.py
Created May 14, 2020 04:29
How to get private Ip and public Ip in Python
#!/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
@phanthaihuan
phanthaihuan / getArgv.py
Created May 14, 2020 04:33
How to access command line arguments in Python
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)