April 7, 2009, 2:53 pm
The WMI performance counters on Windows have a timestamp value in the field “Timestamp_Sys100NS”. To convert this to a more useable datetime use follow this example code:
epoch = datetime.datetime( 1601, 1, 1, 0, 0, 0 )
wmic = wmi.WMI( find_classes = False )
pd = wmic.Win32_PerfRawData_PerfOS_Processor( Name = "_Total" )[0]
ts = epoch + datetime.timedelta( microseconds = pd.Timestamp_Sys100NS / 10.0)
February 18, 2008, 2:49 pm
After cloning a virtual machine running Ubuntu Server 7.10 I found that it didn’t have any network available. Restarting networking gave:
sudo /etc/init.d/networking restart
* Reconfiguring network interfaces...
eth0: ERROR while getting interface flags: No such device
SIOCSIFADDR: No such device
eth0: ERROR while getting interface flags: No such device
SIOCSIFNETMASK: No such device
SIOCSIFBRDADDR: No such device
eth0: ERROR while getting interface flags: No such device
eth0: ERROR while getting interface flags: No such device
Failed to bring up eth0.
The problem lies in the fact that ethernet MAC addresses are cached. You need to remove a file to clear the cached value:
sudo rm /etc/udev/rules.d/70-persistent-net.rules
After a restart of your server you should have networking back again.
I also had to edit the following files to change the static ip addresses and hostname:
/etc/hosts - change ip address and hostnames
/etc/hostname - change hostname
/etc/network/interfaces - change ip address
January 28, 2008, 11:42 am
My previous post on CasPol is great for ordinary shares, but for DFS it doesn’t work. To solve this you need to add your share into the LocalIntranet zone by doing this:
caspol -machine -addgroup "LocalIntranet_Zone" -url "file:///<dfs share path>" FullTrust
January 25, 2008, 10:35 am
Blogs are great for jotting down the usage of commands that you use very rarely. This posting is one of these cases. (This is actually the second time I have made this note, but the first one got lost in wiki that was destroyed some time ago.)
The issue I had was that on a new machine I got the following error when running some .net code from a network share:
An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
After a little searching a remembered the answer. It was to use caspol like this:
caspol -machine -chggroup Internet_Zone FullTrust
November 21, 2007, 3:45 pm
Today I wanted to add, update and delete virtual directories on a number of IIS servers.
To connect to the IIS servers I use:
import win32com.client
locator = win32com.client.Dispatch( 'WbemScripting.SWbemLocator' )
server = locator.ConnectServer( server_name, 'root/MicrosoftIISv2' )
server.Security_.authenticationLevel = 6 # wbemAuthenticationLevelPkt
Then to add a virtual directory called ‘name’ that maps to directory called ‘directory’ you need:
vdir_class = server.Get( "IIsWebVirtualDirSetting" )
vdir_settings = vdir_class.SpawnInstance_()
vdir_settings.Name = 'W3SVC/1/ROOT/' + name
vdir_settings.Path = directory
vdir_settings.Put_()
To update a virtual directory you need:
vdir_settings = server.Get( "IIsWebVirtualDirSetting='%s'" % ( 'W3SVC/1/ROOT/' + name ) )
vdir_settings.Path = directory
vdir_settings.Put_()
Finally, to delete a virtual directory you need:
vdir = server.Get( "IIsWebVirtualDirSetting='%s'" % ( 'W3SVC/1/ROOT/' + name ) )
vdir.Delete_()
September 26, 2007, 2:07 pm
I have had a Dell 690 Workstation for a while now. It originally came with a pair of dual core Xeon processors, but some time ago Intel were kind enough to send me a pair of quad core Xeons, 2.66GHz X5350’s to be precise. I replaced the original processors with these new ones and everything worked fine until yesterday.
Yesterday I decided to upgrade the BIOS on the 690 from version A01 to A06. The Dell download worked fine and upgraded the BIOS but rebooting resulted, after the POST messages, in a message saying “Incompatible processor detected!” and a halted system! This was not good. I couldn’t even get into the BIOS setup with F2.
The trick to getting it working again was to remove the button cell battery on the motherboard for a minute or so and then replace it. When you power on the machine you will be asked to go into setup and set the date and time. After doing this my machine would boot with A06. The problem is that if you ever remove the power cable, or switch off at the wall, then your 690 won’t boot again and it will give the “Incompatible processor detected!” message again. I found this out the hard way. So while I had a working machine I downloaded the A01 version of the BIOS from ftp.us.dell.com and installed it. After this my machine is fully functional again. I don’t have the time at the moment to do a test of all BIOS versions between A01 and A06 to see where the problem starts.
August 13, 2007, 10:21 am
Using the SolidWorks API with Python is not as easy as it should be.
Generating modules from the type libraries is easy enough with makepy:
makepy.py sldworks.tlb
makepy.py swconst.tlb
As is using these to start up SolidWorks.
from win32com.client import gencache,Dispatch
from win32com.client import constants
sldworks = gencache.EnsureModule('{83A33D31-27C5-11CE-BFD4-00400513BB57}', 0, 15, 0) # sw2007
swconst = gencache.EnsureModule('{4687F359-55D0-4CD3-B6CF-2EB42C11F989}', 0, 15, 0) # sw2007
sw = sldworks.SldWorks( Dispatch( 'SldWorks.Application' ) )
But you soon hit problems when you want to open a model with OpenDoc6. This code:
model, errors, warnings = sw.OpenDoc6( 'cube.sldprt', constants.swDocPART, constants.swOpenDocOptions_Silent, '' )
triggers this exception:
pywintypes.com_error: (-2147352561, 'Parameter not optional.', None, None)
The problem turns out to be that the method generated with makepy has incorrect default values. In the definition below Errors and Warnings should have deafult values of pythoncom.Missing.
def OpenDoc6(self, FileName=defaultNamedNotOptArg, Type=defaultNamedNotOptArg, Options=defaultNamedNotOptArg, Configuration=defaultNamedNotOptArg, Errors=defaultNamedNotOptArg, Warnings=defaultNamedNotOptArg):
To fix this I have used the Python introspection methods to rewrite the default arguments.
import pythoncom
import new
method = getattr( sldworks.ISldWorks, 'OpenDoc6' )
arg_list = list( method.func_defaults )
arg_list[ -1 ] = pythoncom.Missing
arg_list[ -2 ] = pythoncom.Missing
new_function = new.function( method.func_code, method.func_globals, method.func_code.co_name, tuple( arg_list ) )
new_method = new.instancemethod( new_function, None, sldworks.ISldWorks )
setattr( sldworks.ISldWorks, 'OpenDoc6', new_method )
I have created a generic method that could be used to fix up more than just OpenDoc6. You can download it here: swfix.py It is used like this:
from win32com.client import gencache,Dispatch
from win32com.client import constants
from swfix import fix_swtlb
sldworks = gencache.EnsureModule('{83A33D31-27C5-11CE-BFD4-00400513BB57}', 0, 15, 0) # sw2007
swconst = gencache.EnsureModule('{4687F359-55D0-4CD3-B6CF-2EB42C11F989}', 0, 15, 0) # sw2007
fix_swtlb( sldworks )
sw = sldworks.SldWorks( Dispatch( 'SldWorks.Application' ) )
Currently it only fixes OpenDoc6, but I hope to include other methods over time. Of course help in doing this is most welcome.
August 11, 2007, 12:28 pm
I have my iPhoto library on a NFS mounted volume. In iPhoto ‘06 this worked fine. After upgrading to iLife ‘08, iPhoto would no longer open, giving me this error message:
The iPhoto Library is locked, on a locked disk, or you do not have permission to make changes to it.
It turned out that I did not have rpc.statd or rpc.lockd running on my FreeBSD NFS server. After I started these I was able to start iPhoto.
August 6, 2007, 2:34 pm
Recently I needed to create a image flipper that did a faded transition between the images. I used script.aculo.us as toolkit to do the transitions and came up with this javascript:
var SwapDelay = 5; // time between image changes
var FadeTime = 2; // duration of the transition
Event.observe(window, 'load', function() {
images = new Array(
'Images/image-01.png',
'Images/image-02.png',
'Images/image-03.png',
'Images/image-04.png',
'Images/image-05.png' );
var first_image = 0;
var last_image = images.length;
var current_image = 0;
$( 'headimg' ).src = images[ current_image ];
new PeriodicalExecuter( function( pe ) {
var this_image = current_image;
current_image++;
if ( current_image == last_image ) { current_image = first_image; }
// set the background to the current image...
$( 'header' ).setStyle( { backgroundImage: 'url(' + images[ this_image ] + ')' } )
// set the opacity of the headimg to fully transparent...
var fader = new Effect.Opacity( 'headimg',
{
duration: 0.25,
from : 1.0,
to : 0.0,
afterFinish : function(obj) {
// ...and once we have finished swap the image - stops some spurious flashing of images.
$( 'headimg' ).src = images[ current_image ];
}
} );
// now fade the new image in...
new Effect.Opacity('headimg', { duration: FadeTime, from : 0.0, to : 1.0 } );
}, SwapDelay);
});
All you need is an image tag with an id of “headimg”
<img id="headimg" alt="" />
I was quite pleased with the result.
August 3, 2007, 5:56 pm
To keep counters for an unknown set of keys I find myself often using the following code pattern:
counter = {}
try:
counter[ keyname ] += 1
except KeyError:
counter[ keyname ] = 1
If you have lots of counters then the code quickly bloats. One solution is to use the setdefault() function like this:
counter[ keyname ] = counter.setdefault( keyname, 0 ) + 1
I find this a bit ‘untidy’ so a second solution, which I prefer, is to use defaultdict from the collections module.
from collections import defaultdict
counter = defaultdict( int )
counter[ keyname ] += 1
Much cleaner.