Skip to content

Instantly share code, notes, and snippets.

@lambdamusic
lambdamusic / Snipplr-24676.scm
Created February 7, 2013 21:28
Scheme: scheme: check for optional arguments
(define-macro (p r . otherstuff)
`(if (null? (list ,@otherstuff))
(print ,r)
(print ,@otherstuff)))
(p 'c 'ciao)
@lambdamusic
lambdamusic / Snipplr-28264.bash
Created February 7, 2013 21:28
Bash: Updating Textmate Getbundles
cd ~/Library/Application\ Support/TextMate/Bundles
svn co http://svn.textmate.org/trunk/Review/Bundles/GetBundles.tmbundle/
@lambdamusic
lambdamusic / Snipplr-64394.txt
Created February 7, 2013 21:28
HTML5 Page Structure
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Your Website</title>
</head>
<body>
@lambdamusic
lambdamusic / Snipplr-25245.py
Created February 7, 2013 21:27
Python: Python: using Map
# build a dictionary that maps the ordinals from 32 to 255
# to their ASCII character equivalents eg. 33: '!'
# (note that 32 and 160 are spaces)
# Python24 by vegaseat 10may2005
import operator
print "ASCII values of characters:"
print '-'*27 # print 27 dashes, cosmetic
@lambdamusic
lambdamusic / Snipplr-51688.py
Created February 7, 2013 21:28
Django: Remove duplicates from a list
def remove_duplicates(seq, idfun=None):
# order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
# in old Python versions:
# if seen.has_key(marker)
@lambdamusic
lambdamusic / Python Open url.py
Last active December 12, 2015 07:08
Python Open url
# if you are on the internet you can access the HTML code of a
# given web site
# using the urlopen() method/function from the module urllib2
 
import urllib2
 
urlStr = 'http://www.python.org/'
try:
fileHandle = urllib2.urlopen(urlStr)
str1 = fileHandle.read()
@lambdamusic
lambdamusic / Snipplr-25254.py
Created February 7, 2013 21:27
Python: Python: Functions and parameters
## Functions are objects in the Python language and the parameters that are passed are really "applied" to the function object.
To create a function, use the def functionname(parameters): statement, and then define the function in the following code block. Once the function has been defined, you can call it by specifying the function name and passing the appropriate parameters.
def fun(name, location, year=2006):
print "%s/%s/%d" % (name, location, year)
## The first example shows the function being called by passing the parameter values in order. Notice that the year parameter has a default value set in the function definition, which means that this parameter can be omitted and the default value will be used.
>>>fun("Teag", "San Diego")
@lambdamusic
lambdamusic / Snipplr-25251.py
Created February 7, 2013 21:27
Python: Python: module and class namespaces
## The module namespace is created when a module is imported and the objects within the module are read. The module namespace can be accessed using the .__dict__ attribute of the module object. Objects in the module namespace can be accessed directly using the module name and dot "." syntax. The example shows this by calling the localtime() function of the time module:
>>>import time
>>>print time.__dict__
{'ctime': <built-in function ctime>,
'clock': <built-in function clock>,
... 'localtime': <built-in function localtime>}
>>> print time.localtime()
(2006, 8, 10, 14, 32, 39, 3, 222, 1)
@lambdamusic
lambdamusic / Snipplr-48617.py
Created February 7, 2013 21:27
Python: RDFlib: access a triple store in 5 lines of code
ed@rorty:~$ python
Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rdflib
>>> graph = rdflib.Graph('Sleepycat')
>>> graph.open('my-store', create=True)
>>> graph.parse('http://dbpedia.org/resource/Semantic_Web')
>>> for object in graph.objects(predicate=rdflib.namespace.OWL.sameAs):
... print object
@lambdamusic
lambdamusic / Snipplr-37160.py
Created February 7, 2013 21:28
Python: Reload code in console
import test
test.answer_to_life_the_universe_and_everything
# Whoops that's not right, I'd better edit the module....
reload(test)
test.answer_to_life_the_universe_and_everything
842