Saturday, July 31, 2010

python: using the glob module to perform simple wildcard expansion on a filesystem

import glob
glob.glob('c:\\music\\_singles\\*.mp3')

['c:\\music\\_singles\\a_time_long_forgotten_con.mp3',
'c:\\music\\_singles\\hellraiser.mp3',
'c:\\music\\_singles\\kairo.mp3',
'c:\\music\\_singles\\long_way_home1.mp3',
'c:\\music\\_singles\\sidewinder.mp3',
'c:\\music\\_singles\\spinning.mp3']
glob.glob('c:\\music\\_singles\\s*.mp3')

['c:\\music\\_singles\\sidewinder.mp3',
'c:\\music\\_singles\\spinning.mp3']



You can get a list of all of those with a single call to glob, by using two wildcards at once. One wildcard is the "*.mp3" (to match .mp3 files), and one wildcard is within the directory path itself, to match any subdirectory within "c:\music" like this:



glob.glob('c:\\music\\*\\*.mp3')



source

web services vs REST

The fundamental difference, therefore, between REST and document-style Web services is how the service consumer knows what to expect out of the service. Web services have contracts, defined in WSDL. Since Web services focus on the service, rather than on the resource, the consumer has clear visibility into the behavior of the various operations of the service, whereas in REST's resource-oriented perspective, we have visibility into the resources, but the behavior is implicit, since there is no contract that governs the behavior of each URI-identified resource.

For a very good introductory article on REST, check out the article here.

Thursday, July 29, 2010

how much physical memory is supported by 64 bit editions of Windows 7?

While the maximum RAM limit for 32-bit Windows 7 editions is 4GB, when it comes to the 64-bit editions, the amount of memory that the OS can address depends on which edition you are running.

Here are the upper RAM limits for the different editions of Windows 7:

  • Starter: 8GB
  • Home Basic: 8GB
  • Home Premium: 16GB
  • Professional: 192GB
  • Enterprise: 192GB
  • Ultimate: 192GB

source

Saturday, July 24, 2010

python: sharing global variables across modules

The canonical way to share information across modules within a single program is to create a special configuration module (often called config or cfg). Just import the configuration module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere.

For example:

File: config.py

x = 0  # Default value of the 'x' configuration setting

File: mod.py
import config 
config.x = 1

File: main.py
import config
import mod
print config.x

Module variables are also often used to implement the Singleton design pattern, for the same reason.

source

python: local vs global scope

What are the rules for local and global variables in Python?


In Python, variables that are only referenced (and not assigned a value) inside a function are implicitly global.

If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be a local.

If a variable is ever assigned a new value inside the function, the variable is implicitly local, and you need to explicitly declare it with the "global" keyword if you want to use the global context.

python: if __name__ == "__main__"

This is a simple "trick" so that python files can be used as both importable/reusable modules and also as standalone python programs.

From another perspective, this answers the common interview question,
"What considerations do you take while writing a python module? Or do you just write a module like a regular python program and then import it blindly? Don't you have to take some extra care/precautions while writing a python module?"


See this link for a simple and illustrative example:
http://pyfaq.infogami.com/tutor-what-is-if-name-main-for

linux: my ~/.screenrc file

This is the .screenrc file that has evolved as my current favorite over time - both for cygwin and native linux shells.

# the default shell when creating a new screen window
# ~/.bashrc is invoked whenever creating a new screen window
# so all my bash aliases are available from within screen too
shell -${SHELL}


# The shelltitle specified is an auto-title that would expect the prompt and the typed command to look something like the following:
#  $ fortune
# (it looks after the '$ ' for the command name). The window status would show the name "fortune" while the command was running, and revert to "bash" upon completion.
shelltitle "$ |bash"


#avoid startup message
startup_message off


hardstatus alwayslastline


#hardstatus string '%{= mK}%-Lw%{= KW}%50>%n%f* %t%{= mK}%+Lw%< %{= kG}%-=%D %d %M %Y %c:%s%{-}'
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m %{W}%c %{g}]'


# Use the function keys to immediately switch to corresponding windows
# (instead of having to press ^a0, ^a1 etc - which also still work, btw)
bindkey -k k1 select 1
bindkey -k k2 select 2
bindkey -k k3 select 3
bindkey -k k4 select 4
bindkey -k k5 select 5
bindkey -k k6 select 6
bindkey -k k7 select 7
bindkey -k k8 select 8
bindkey -k k9 select 9
bindkey -k k; select 10
bindkey -k F1 select 11
bindkey -k F2 select 12



# go to next/previous window using ctrl+ right and left arrows
# this interferes with word-back and word-ahead on ubuntu, hence disabled:
# bindkey ^[[1;5D prev
# bindkey ^[[1;5C next
# set the scrollback buffer to hold the last 5000 lines
defscrollback 5000
sources:

Thursday, July 22, 2010

python: how to get size of an object

use sys.getsizeof:

>>> import sys
>>> x = 2
>>> sys.getsizeof(x)
14
>>> sys.getsizeof(sys.getsizeof)
32
>>> sys.getsizeof('this')
38
>>> sys.getsizeof('this also')
48



source