You Are Here Home > May 2009

May 2009

highlight_string/highlight_file doesn’t work

Make sure you wrap the PHP code with <?php and ?>

<?php
highlight_string('<?php
// ... Some code
?>');
?>
highlight_string/highlight_file doesn’t work
Comments (0)   Filed under: PHP   Posted by: Codehead

Absolute Positioning an Element Relative To a Box/DIV

In order to do this, you must position the parent box relative, you don’t need to set the left/right or top/bottom necessarily, just a:

.ab-parent-box {
   position: relative;
}

Will do the trick…

Absolute Positioning an Element Relative To a Box/DIV
Comments (0)   Filed under: CSS   Posted by: Codehead

wPrime and Unexpected Error; Quitting

Right click on it and click “Run as Admin…”

wPrime and Unexpected Error; Quitting
Comments (0)   Filed under: Windows Vista   Posted by: Codehead

Send HTML emails

To send HTML emails, you will need special email headers:

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: FROM-EMAIL@YOURDOMAIN.COM\r\n"
                ."Reply-To: REPLY-EMAIL@YOURDOMAIN.COM";
mail('TO EMAIL ADDRESS', 'SUBJECT LINE', 'Message Body<br /><small>With some HTML</small>', $headers);
Send HTML emails
Comments (0)   Filed under: PHP   Posted by: Codehead

Generate a Unique String

$uid = uniqid();
Generate a Unique String
Comments (0)   Filed under: PHP   Posted by: Codehead

Check To See If a File Exists

import os
 
if os.path.exists(path_to_file):
   # Do stuff
Check To See If a File Exists
Comments (0)   Filed under: Python   Posted by: Codehead

Check To See If a File Exists

if (file_exists($path_to_file)) {
   // Do stuff
}
Check To See If a File Exists
Comments (0)   Filed under: PHP   Posted by: Codehead

Calling JavaScript Functions

import flash.external.ExternalInterface;
...
ExternalInterface.call("your_javascript_function()");

Get the return value:

var x:int = ExternalInterface.call("get_x()");
Calling JavaScript Functions
Comments (0)   Filed under: Actionscript   Posted by: Codehead

Missing from Python: kill-able threads and popens with timeouts…

It would be nice to be able to kill threads anytime you wanted, there are solutions to this but are not part of the Python library and I didn’t yet get a chance to try them… Actually there is only one promising solution to this.

The other thing I would love to have in Python is to be able to call popen with a second parameter which is a timeout, That is (obviously) popen would give up and return if the call was taking more than x number of seconds…

Missing from Python: kill-able threads and popens with timeouts…
Comments (0)   Filed under: Programming, Python   Posted by: Codehead

Python win32api and pywintypes.error: (5, ‘TerminateProcess’, ‘Access is denied.’)

I was working on a piece of code that was supposed to close a bunch of processes ‘with the same name’ right after each other and was getting this error message:
pywintypes.error: (5, ‘TerminateProcess’, ‘Access is denied.’)

The problem was that I was doing this in a loop and Windows didn’t get enough time to close the first process yet. To fix this, just add a time.sleep(0.5) in your loop and you will be all set…

I hope this helps :)

Here is the code I’m using (found in Python mailing list):

import time
import win32api, win32pdhutil, win32con
import win32pdh, string
 
# ***********************************************************************
# ***********************************************************************
def GetAllProcesses():
  object = "Process"
  items, instances = win32pdh.EnumObjectItems(None,None,object,
win32pdh.PERF_DETAIL_WIZARD)
  return instances
# ***********************************************************************
 
 
# ***********************************************************************
# ***********************************************************************
def GetProcessID ( name ) :
  object = "Process"
  items, instances = win32pdh.EnumObjectItems(None,None,object, win32pdh.PERF_DETAIL_WIZARD)
 
  val = None
  if name in instances :
    hq = win32pdh.OpenQuery()
    hcs = []
    item = "ID Process"
    path = win32pdh.MakeCounterPath( (None,object,name, None, 0, item) )
    hcs.append(win32pdh.AddCounter(hq, path))
    win32pdh.CollectQueryData(hq)
    time.sleep(0.01)
    win32pdh.CollectQueryData(hq)
 
    for hc in hcs:
      type, val = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG)
      win32pdh.RemoveCounter(hc)
    win32pdh.CloseQuery(hq)
    return val
# ***********************************************************************
 
# ***********************************************************************
# ***********************************************************************
def Kill_Process_pid ( pid ) :
  handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, pid) #get process handle
  win32api.TerminateProcess(handle, -1) #kill by handle
  win32api.CloseHandle(handle)        #close api
# ***********************************************************************
 
 
# ***********************************************************************
# ***********************************************************************
def Kill_Process ( name ) :
  pid = GetProcessID(name)
  if pid:
    try:
        Kill_Process_pid(pid)
        return True
    except:
        pass
  else:
    return False
# ***********************************************************************

I just modified the last function, it’s kind of funny I know but that works for me, then I call it like:

print 'Killing IEs...',
while Kill_Process('iexplore'):
   time.sleep(0.5)
print 'Done!'
Python win32api and pywintypes.error: (5, ‘TerminateProcess’, ‘Access is denied.’)
Comments (0)   Filed under: Programming, Python   Posted by: Codehead
Older Posts »