If it won't be simple, it simply won't be. [source code] by Miki Tebeka, CEO, 353Solutions

Tuesday, September 23, 2008

"Disabling" an image

Sometimes you want to mark a button image as "disabled". The usual method is to have two images and display the "disabled" state image when disabled.

However you can use the image opacity the mark is as disabled as well:


<html>
<head>
<title>Dimmer</title>
<style>
.disabled {
filter: alpha(opacity=50);
-moz-opacity: 0.50;
opacity: 0.50;
}
</style>
</head>
<body>
<center>
<p>Show how to "dim" an image, marking it disabled</p>
<img src="image.png" id="image" /> <br />
<button onclick="disable();">Disable</button>
<button onclick="enable();">Enable</button>
</center>
</body>
<script src="jquery.js"></script>
<script>
function disable() {
$('#image').addClass('disabled');
}

function enable() {
$('#image').removeClass('disabled');
}
</script>
</html>

Thursday, September 18, 2008

Destktop Web Application


It's very easy have the browser window as your user interface, even on desktop applications.

Below is a small phone book demo.

General Design:
  • Have a web server serve the main search page
  • Run an AJAX query and display the results
  • fork the server so the program will return

webphone.py (AKA the web server)

index.html (AKA the GUI)

pymodver

Which version of module do I have?

#!/usr/bin/env python
'''Find python module version'''

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"

def valueof(v):
if callable(v):
try:
return v()
except Exception:
return None
return v

def load_module(module_name):
module = __import__(module_name)

# __import__("a.b") will give us a
if ("." in module_name):
names = module_name.split(".")[1:]
while names:
name = names.pop(0)
module = getattr(module, name)

return module

def find_module_version(module_name):
module = load_module(module_name)
attrs = set(dir(module))

for known in ("__version__", "version"):
if known in attrs:
v = valueof(getattr(module, known))
if v:
return v

for attr in attrs:
if "version" in attr.lower():
v = getattr(module, attr)
if not v:
continue
v = valueof(v)
if v:
return v

def main(argv=None):
if argv is None:
import sys
argv = sys.argv

from optparse import OptionParser

parser = OptionParser("usage: %prog MODULE_NAME")

opts, args = parser.parse_args(argv[1:])
if len(args) != 1:
parser.error("wrong number of arguments") # Will exit

module_name = args[0]

try:
version = find_module_version(module_name)
except ImportError, e:
raise SystemExit("error: can't import %s (%s)" % (module_name, e))

if version:
print version
else:
raise SystemExit("error: can't find version for %s" % module_name)

if __name__ == "__main__":
main()

Tuesday, September 16, 2008

Exit Gracefully

When your program is terminated by a signal, the atexit handlers are not called.

A short solution:

Sunday, September 07, 2008

"unpack" updated

Updated the code to unpack and added view functionality to it.

Thursday, September 04, 2008

putclip

A "cross platform" command line utility to place things in the clipboard.
(On linux uses xsel)
#!/usr/bin/env python
'''Place stuff in clipboard - multi platform'''

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"

from os import popen
from sys import platform

COMMDANDS = { # platform -> command
"darwin" : "pbcopy",
"linux2" : "xsel -i",
"cygwin" : "/bin/putclip",
}

def putclip(text):
command = COMMDANDS[platform]
popen(command, "w").write(text)

def main(argv=None):
if argv is None:
import sys
argv = sys.argv

from optparse import OptionParser
from sys import stdin

parser = OptionParser("%prog [PATH]")

opts, args = parser.parse_args(argv[1:])

if len(args) not in (0, 1):
parser.error("wrong number of arguments") # Will exit

if platform not in COMMDANDS:
message = "error: don't know how to handle clipboard on %s" % platform
raise SystemExit(message)

if (not args) or (args[0] == "-"):
info = stdin
else:
try:
infile = args[0]
info = open(infile)
except IOError, e:
raise SystemExit("error: can't open %s - %s" % (infile, e))

try:
putclip(info.read())
except OSError, e:
raise SystemExit("error: %s" % e)

if __name__ == "__main__":
main()

Blog Archive