Skip to content

Instantly share code, notes, and snippets.

Python 101

Introduction

Welcome to Python 101 at PyOhio 2013!

Python 2.x or Python 3.x?

@myano
myano / goo.gl.py
Created November 13, 2013 21:05 — forked from ikks/goo.gl.py
#Given to the public domain
#No warranties
import urllib2
import simplejson
def shorturl(urltoshorten):
"""Compress the URL using goo.gl take a look at https://developers.google.com/url-shortener/v1/getting_started
>>> shorturl('http://igor.tamarapatino.org')
'http://goo.gl/FxHOn'
@myano
myano / gist:7435249
Last active December 28, 2015 03:29

This demonstrates how different regular expression engines handle catastrophic expressions. Python's "re" module doesn't seem to have any type of backtracking timeout.

awk

awk 4.0.1

» awk --version
GNU Awk 4.0.1

» time echo aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | awk '{ gsub("^(([a-z])+.)+A-Z+$", "test"); print}'

@myano
myano / fake_data.py
Last active December 20, 2015 07:29
#!/usr/bin/env python
'''
This file contains fake data which may be useful in
demonstrating basic principles in Python.
'''
import random
colors = [
'red',

My model for assessing trust with people.

  1. Random strangers
  2. Acquaintances
  3. Most friends that are more than acquaintances
  4. Close friends
  5. Only me
@myano
myano / gist:5844270
Last active December 18, 2015 20:59

Rules for ##uno on freenode

  • No additional bots in the channel. Only ChanServ and jenni are allowed.
  • No automated scripts. (eg: No automatic /me's or messages when you go "away")
  • No personal insults.
  • No cheating.
  • Do not highlight nicks unless you are conversing with said person or persons.
  • Do not spam the channel with non-relevant commands to the current game.
  • Do not beg/insist the ops ban somebody.
@myano
myano / gitio.md
Last active December 16, 2015 23:39

Create git.io Short URLs in Python

git.io is an awesome URL shortener provided by Github. It works when shortening URLs from *.github.com/* to a small https://git.io/ link.

requests

>>> import requests
@myano
myano / join.py
Last active December 14, 2015 03:29
If you have saved your buffers in WeeChat with "/layout save", this script will print out an easy to copy/paste "/join #channel1,#channel2" that you can paste into WeeChat to join channels. If you have a lot of channels (more than 100). I would recommend joining only about 75-100 channels at a time.
#!/usr/bin/env python
import copy
f = open('weechat.conf', 'r')
formats = ['/join ', '/query ']
networks = {
@myano
myano / gist:5016700
Last active December 14, 2015 02:49
This will convert a given password "foobar" into the SHA output that Apache uses for it's .htpasswd files. You can take the output of this command and simply append it with a "username_here:{SHA}" and then place it into a .htpasswd is pointing at. This is useful if you want someone to generate a password for you but they don't have Apache instal…

Without salt:

$ echo -n "foobar" | sha1sum | cut -d' ' -f1 | xxd -r -p | base64

With salt:

$ USR="yourname";PWD="foobar";SALT="$(openssl rand -base64 3)";SHA1=$(printf "$PWD$SALT" | openssl dgst -binary -sha1 | sed 's#$#'"$SALT"'#' | base64); echo "$USR:{SSHA}$SHA1"

@ikks
ikks / goo.gl.py
Created May 31, 2012 19:43
Using goo.gl from python 2.X Where X is 6 or more
#Given to the public domain
#No warranties
import urllib2
import simplejson
def shorturl(urltoshorten):
"""Compress the URL using goo.gl take a look at https://developers.google.com/url-shortener/v1/getting_started
>>> shorturl('http://igor.tamarapatino.org')
'http://goo.gl/FxHOn'