Skip to content

Instantly share code, notes, and snippets.

View gchavez2's full-sized avatar

Gustavo gchavez2

View GitHub Profile
for k,v in sorted(foo.items()):
print k, v
for k in sorted(foo.keys()):
print k, foo[k]
More at: https://stackoverflow.com/questions/364519/in-python-how-do-i-iterate-over-a-dictionary-in-sorted-key-order
import json
with open('json_file.json') as f:
data = json.load(f)
print(data)
"""
y2k
19016 -> 01/23/2020
0 = 12/31/1967
1 = 01/01/1968
"""
import sys
from datetime import datetime, timedelta
@gchavez2
gchavez2 / internetSpeed.sh
Created January 25, 2020 06:51
Test internet speed
curl https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -
# Silent version
#curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -
@gchavez2
gchavez2 / Bash.sh
Created September 26, 2019 05:14
Output to terminal and file
p 15count_coverage.py 2>&1 | tee out.txt
nme = ["aparna", "pankaj", "sudhir", "Geeku"]
deg = ["MBA", "BCA", "M.Tech", "MBA"]
scr = [90, 40, 80, 98]
# New from dictionary
dict = {'name': nme, 'degree': deg, 'score': scr}
df = pd.DataFrame(dict)
clean_df.to_csv(OUTPUT_FILENAME, index=False)
# New dataframe from subset of pandas dataframe
@gchavez2
gchavez2 / Histogram_Python_list.py
Last active August 16, 2019 22:12
histogram_of_list.py
# Histogram of words per document in the first page
import matplotlib.pyplot as plt
plt.style.use("ggplot")
plt.hist([s for s in words_per_doc], bins=10)
plt.xlabel('Documents')
plt.ylabel('Number of words')
plt.show()
@gchavez2
gchavez2 / Dataframe_exploration.py
Last active August 16, 2019 22:33
Dataframe exploration
df = pd.read_csv(FILENAME, names=["id", "title", "text"],
escapechar='\\', encoding='utf-8', header=0)
print(df.describe()) # Summary statistics (count, unique, top, freq) for every column of the dataframe
print(df.info()) # Information about each column on the dataframe, and memory usage
print(df.head()) # Show first 5 rows of dataframe
print(df.head(3)) # Show first 3 rows of dataframe
# Head with only a few columns, with random sampling of 10 rows
@gchavez2
gchavez2 / free.py
Created July 2, 2019 17:49
Report available RAM memory with Python
#!/usr/bin/python
import subprocess
import re
# Get process info
ps = subprocess.Popen(['ps', '-caxm', '-orss,comm'],
stdout=subprocess.PIPE).communicate()[0].decode()
vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[
0].decode()