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 1-4 of 4 total  RSS 

Windows XP System Variables

// Windows XP Default System Variables

%SystemDrive%  		C:

%SystemRoot% 		C:\WINNT, C:\WINDOWS

%SystemDirectory% 	C:\WINNT\System32, C:\WINDOWS\System32

%WinDir% 		C:\WINNT, C:\WINDOWS, C:\WINNT\Program Files

%ComSpec% 		C:\WINNT\system32\cmd.exe

%Temp% 			C:\DOCUME~1\Usr\LOCALS~1\Temp from C:\Documents and Settings\Usr\Local Settings\Temp

%HOMEDRIVE% 		C: The drive letter associated with the user's home directory

%HOMEPATH% 		The path to the user's home directory (excluding drive): \Documents and Settings\Guest

%OS% 			Windows_NT -> The operating system the user is running

%USERDOMAIN% 		The name of the domain that contains the user's account

%USERNAME% 		The user's name

Default Sort Order For Rails models

// provide a default sort order in case an order by clause isn't defined in the find clause
// Place this code somewhere in your model's class file.

        def self.find(*args)
          order_arg = args.collect do |arg|
            if arg.kind_of? Hash 
              if arg.keys[0] == :order
                arg
              end
            end
          end

          if order_arg.compact.empty?
            args << {:order=>"place order by clause here e.g. 'name asc'"}
          end
          
          super
        end

Default dictionary

I always want to do this, like I did in Perl.
>> d = DefaultDict(0)
>> d[key] += 1     # no need to use d.get or d.setdefault

>> d = DefaultDict([])  # similarly with list value
>> d[key].append(item)

See the implementation by Peter Norvig (of AI & Google fame)
here

Counting characters in string

>>> s = 'a;jfkd;aflhakfhaskfjalghlakfhfnkjafyksd'
>>> cnt = {}
>>> for c in s:
	cnt[c] = cnt.get(c,0) + 1

>>> print cnt
{'a': 7, 'd': 2, 'g': 1, 'f': 7, 'h': 4, 'k': 6, 'j': 3, 'l': 3, 'n': 1, 's': 2, 'y': 1, ';': 2}

This can be used to count any distribution.
Note the use of dict.get(key,default) to set to 0
if the key is not avaiable.
If this were perl, I would just do a
cnt[c] += 1

But python will give an error instead of returning 0.
It's not too bad, though.
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS