def rCopy(d, f=identityConversion, discardNoneKeys=True, deepCopy=True):
 if deepCopy:
  d = copy.deepcopy(d)
 newDict = {}
 toCopy = [(k, v, newDict, ()) for (k, v) in d.iteritems()]
 while (len(toCopy) > 0):
  (k, v, d, prevKeys) = toCopy.pop()
  prevKeys = (prevKeys + (k,))
  if isinstance(v, dict):
   d[k] = dict()
   toCopy[0:0] = [(innerK, innerV, d[k], prevKeys) for (innerK, innerV) in v.iteritems()]
  else:
   newV = f(v, prevKeys)
   if ((not discardNoneKeys) or (newV is not None)):
    d[k] = newV
 return newDict

def rCopy ( d , f = identityConversion , discardNoneKeys = True , deepCopy = True ) : if deepCopy : d = copy . deepcopy ( d ) newDict = { } toCopy = [ ( k , v , newDict , ( ) ) for ( k , v ) in d . iteritems ( ) ] while ( len ( toCopy ) > 0 ) : ( k , v , d , prevKeys ) = toCopy . pop ( ) prevKeys = ( prevKeys + ( k , ) ) if isinstance ( v , dict ) : d [ k ] = dict ( ) toCopy [ 0 : 0 ] = [ ( innerK , innerV , d [ k ] , prevKeys ) for ( innerK , innerV ) in v . iteritems ( ) ] else : newV = f ( v , prevKeys ) if ( ( not discardNoneKeys ) or ( newV is not None ) ) : d [ k ] = newV return newDict
recursively copies a dict and returns the result .