Python: Checking Type of Variable

isinstance() seems to be the preferred way to check the type of a Python variable. It checks if the variable (object) is an instance of the class object being checked against.

# Variables of different types
i = 1
f = 0.1
s = "Hell"
l = [0, 1, 2]
d = {0:"Zero", 1:"One"}
t = (0, 1, 2)
b = True
n = None

All of the following return True:

isinstance(i, int)
isinstance(f, float)
isinstance(s, str)
isinstance(l, list)
isinstance(d, dict)
isinstance(t, tuple)

True and False are an instance of bool, so:

isinstance(b, bool)

What about None? For that, there is always:

n is None
Advertisements

Windows: Shutdown or Restart from Command-Line

While connected to a computer over RDP, the Shutdown or Restart options are not available in the Start menu.

To shutdown from command-line:

$ shutdown /s /t 0

To restart from command-line:

$ shutdown /r /t 0

PS: Curious to know why Shutdown/Restart are not available over RDP? See “Logoff” and “Shutdown” Are Missing from the Start Menu When You Use Remote Desktop.

Windows Explorer: Why the Refresh?

Bharat was wondering why Windows Explorer needed a Refresh option. Certainly, a modern file viewer is aware of the changes to the files it is displaying without requiring a manual poke?! After all, Apple is so confident of the auto-refresh of Finder on OS X that it does not have the Refresh option! 🙂

Not being able to figure out the reason, I asked Raymond Chen. Raymond, with his fantastic The Old New Thing blog has become the wise man on top of the Windows history mountain. Raymond replied pretty quickly:

Probably for similar reasons to the ones that cause people to write OSX extensions, eg. http://lifehacker.com/252956/download-of-the-day-refresh-finder-mac.

Not all network drives broadcast updates — I imagine that would be a performance nightmare in some cases — so you sometimes might need to refresh manually.

Windows (at least since Vista — it’s been too long since I used XP to remember) does do auto-refresh, which works most of the time and about as well as the one introduced in (IIRC) OSX 10.4 in my experience.

Windows: ClearType on RDP

ClearType is not turned on by default over Remote Desktop Protocol (RDP). Thus, if you connect to a computer using Remote Desktop Connection (RDC) you will notice aliased fonts, quite ugly in my opinion without ClearType enabled.

If you are connecting to a Vista or Windows 7 computer, enable Experience → Font smoothing in the RDC dialog.

If you are connecting to a Windows XP computer, a bit more work is needed. Apply the following Registry entries on that computer:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations]
"AllowFontAntiAlias"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp]
"AllowFontAntiAlias"=dword:00000001

Or just place these lines in a file named CleartypeOnRDP.reg file and execute it.

Windows 7: Registration Entries (.reg) File

If a normal (or Standard) User tries to execute a Registration Entries (.reg) file on Windows 7, he is not elevated to an Administrator login.

Instead, the Registry Editor throws up this error:

The error is misleading because actually the operation is not denied because the keys are open by the system. Rather, it just needs Administrator privileges.

To execute the .reg file, open a command prompt with Administrator privileges and use the regedit command to execute the file:

C:\> regedit MyRegEntries.reg

Python: Dictionary Iteration

The default iteration through a dictionary, returns the keys:

adict = {10:"Ten", 7:"Seven", 12:"Twelve"}

for i in adict:
    print(i)

Output:

10
12
7

(Note that the order of the keys is different from the order in the initialization. This is normal behaviour for a dictionary, where order of the elements should not matter or be relied upon.)

Explicit iteration of the dictionary keys, values and (key, value) pairs can also be done:

# Iterate keys
for k in adict.keys():
    print(k)

# Iterate values
for v in adict.values():
    print(v)

# Iterate (key, value) pairs
for (k, v) in adict.items():
    print(k, ":", v)

Output:

10
12
7
Ten
Twelve
Seven
10 : Ten
12 : Twelve
7 : Seven

Windows: Installing Fonts as Standard User

A Standard User cannot possibly have the permissions to install fonts for the entire Windows system. So, what I mean is how can a Standard User elevate himself to an Administrator to install fonts?

The Run As trick does not work for Fonts. If you Shift + right-click on the Fonts shortcut, you do not see any option to execute it as Administrator or any other user. Neither is Fonts a Control Panel (.cpl) file, so cannot be launched by the runas.exe command-line program.

The Fonts dialog is actually just the C:\Windows\Fonts directory being presented in a special way by Windows Explorer. That gives us a way to install fonts as a Standard User.

Launch Windows Explorer with Administrator privileges. Type in C:\Windows\Fonts in the Address bar. You can now install fonts.