Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 31-37 of 37 total

Thai election ID checking

The online service here aims to help people
check where they are to vote. However, it can be
used as ID certification as well.
# nnnn is the id to be checked
http://www.dopa.go.th/cgi-bin/inqelect.sh?pid=nnnn

Here I make it into a function call.
import urllib, re
def getname(id):
  url = 'http://www.dopa.go.th/cgi-bin/inqelect.sh?pid=' + str(id)
  src = urllib.urlopen(url).read()
  pat = '<H3> *(.*?) *<'
  name = re.findall(pat, src)[0]  # don't use other info
  return name

print getname(5100900050063)  # show a name (one of my relatives)

Using multiple tags on Code Snippets

I spent a lot of time trying to select snippets with
contain multiple tags. (series60 and sql)
There is no '+' links I can click anymore.
After about 10 minutes of trial and error, I find out how.
This also answer another question on correct use of URL.
# for tag 'series60' AND 'sql'
http://www.bigbold.com/snippets/tags/series60/sql/

# this doesn't work
http://www.bigbold.com/snippets/tag/series60/sql/

# all of these work (single tag case)
http://bigbold.com/snippets/tag/series60/
http://bigbold.com/snippets/tags/series60/
http://www.bigbold.com/snippets/tag/series60/
http://www.bigbold.com/snippets/tags/series60/

My conclusion is that.
For a single tag, use '/tag/...'.
For multiple tags, use '/tags/...'.
The first is quicker to type, while the second is flexible in case you want to filter further.

collect-based URL encoder

url-encode: func [
    {URL-encode a string}
    data "String to encode"
    /local normal-char new-data
][
    normal-char: charset [
        #"A" - #"Z" #"a" - #"z"
        #"@" #"." #"*" #"-" #"_"
        #"0" - #"9"
    ]
    data: form data
    collect/into ch [
        forall data [
            ch: either find normal-char first data [first data] [
                rejoin ["%" to-string skip tail (to-hex to-integer first data) -2]
            ]
        ]
    ] copy ""
]

Open browswer from python

You can do it by starting browser app given url as a parameter.
import e32
apprun = 'z:\\system\\programs\\apprun.exe'
browser = 'z:\\System\\Apps\\Browser\\Browser.app'
url = 'http://www.google.com'

e32.start_exe(apprun, browser + ' "%s"' %url , 1)

Nick Burch also write a webbrowser.py module to allow
using opera and any other browswers.

Google free proxy

From Google Hack
http://www.google.com/translate?langpair=en|en&u=www.forbiddensite.com

Filter all URLs from standard input

Code:

Put the following in a file named url_scrape.py.
#!/usr/bin/env python
'''Prints a list of URLs that are found in standard input.

It will only find URLs between quotes ("" or '') and starting with http://
'''

import re
import sys

# Pattern for fully-qualified URLs:
url_pattern = re.compile('''["']http://[^+]*?['"]''')

# build list of all URLs found in standard input
s = sys.stdin.read()
all = url_pattern.findall(s)

# output all the URLs
for i in all:
    print i.strip('"').strip("'")


Example Usage:
wget -O - http://madphilosopher.ca/ | ./url_scrape.py | sort | uniq

Canonical path

This function transforms an HTTP request path (ie, $_SERVER['PATH_INFO']) into its canonical form, removing empty path elements and relative path elements (//, /./, /../). It assumes that there is a trailing slash on the path if it's a directory.

function canonical_path($path) {
    $canonical = preg_replace('|/\.?(?=/)|','',$path);
    while (($collapsed = preg_replace('|/[^/]+/\.\./|','/',$canonical,1)) !== $canonical) {
        $canonical = $collapsed;
    }
    $canonical = preg_replace('|^/\.\./|','/',$canonical);
    return $canonical;
}
« Newer Snippets
Older Snippets »
Showing 31-37 of 37 total