Skip to content

Instantly share code, notes, and snippets.

View rsperl's full-sized avatar

Richard rsperl

  • North Carolina, United States
View GitHub Profile
@rsperl
rsperl / database.py
Last active July 25, 2022 14:05
Connect to microsoft sql server from python #snippet
import pymssql
server = 'hostname.domainname'
database = 'mydb'
username = 'myuser'
password = 'mypass'
conn = pymssql.connect(server, username, password, database)
cursor = conn.cursor(as_dict=True)
cursor.execute('select 6 * 7 as [Result];')
@rsperl
rsperl / read_excel_with_panda.py
Last active January 31, 2023 15:10
Reading excel files with python and panda #snippet
#!/usr/bin/env python
# src: http://pandas.pydata.org/pandas-docs/stable/io.html#io-excel-reader
import pandas as pd
# get a dataframe from xlsx object
xlsx = pd.ExcelFile('path_to_file.xls')
df = pd.read_excel(xlsx, 'Sheet1')
@rsperl
rsperl / log_levels.md
Last active July 25, 2022 13:33
what log levels mean #python #go #perl #shell #snippet

What Log Levels Mean

src: https://blog.bordum.dk/logging-tutorial-python.html

  • fatal: wake me up on Sunday morning at 4am
  • error: apologize to the user and raise a ticket
  • warn: make a note in case it happens again
  • info: everything is fine, just checking in
  • debug: fill filesystem with stack traces
@rsperl
rsperl / sort_objects.py
Last active July 25, 2022 13:59
sort a list of objects #snippet
people = [ ... list of people objects ... ]
people_by_age = sorted(people, key=lambda person: person.age)
@rsperl
rsperl / run_external_command.py
Last active July 25, 2022 13:32
Run an external command and get stdout, stderr, and return code #snippet
import subprocess
date = subprocess.Popen(['date', '+%c'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = date.communicate()
print("stdout: {}".format(out))
print("stderr: {}".format(err))
print("exit code: {}".format(date.returncode))
@rsperl
rsperl / download_and_read_webpage.py
Last active July 25, 2022 13:58
download a webpage and read the contents #nosnippet
import urllib
f = urllib.urlopen(day_url)
html = f.read()
@rsperl
rsperl / parse_xml.py
Last active July 25, 2022 14:08
parse xml #nosnippet
import xml.dom
try:
dom = xml.dom.minidom.parseString(xml_content)
except ExpatError, e:
print >> sys.stderr, 'Invalid XML:', e
for p_node in dom.getElementsByTagName('p'):
print 'class of paragraph', p.attributes['class']
for child in p_node.childNodes:
print 'XML child node:', child
@rsperl
rsperl / find_all_matches.py
Last active July 25, 2022 14:14
Search a text for a pattern and do something with every match #nosnippet
import re
text = "bug 1: first bug\nbug 2: duplicate of bug 1\nbug 999: the last bug"
for m in re.finditer('bug (\d+):', text):
print 'bug number: '+ m.group(1)
@rsperl
rsperl / large_file_md5sum.py
Last active July 25, 2022 14:09
Compute the md5sum of a file without loading it all in memory #nosnippet
import hashlib
f = open(filepath)
digest = hashlib.md5()
while True:
buf = f.read(4096)
if buf == "":
break
digest.update(buf)
f.close()
print digest.hexdigest()
@rsperl
rsperl / sqlite_access.py
Last active July 25, 2022 13:53
using sqlite #snippet
import sqlite3 as dbapi2
db = dbapi2.connect('mydb.sqlite')
cur1 = db.cursor()
cur1.execute('CREATE TABLE friends (name VARCHAR(255), phone int)')
cur1.commit()
cur1 = db.cursor()
cur1.execute('SELECT phone FROM friends WHERE name = ?', ['Fred']) # does proper quoting
for (phone) in cur1: