Package wtf :: Module webutil :: Class Query
[hide private]
[frames] | no frames]

Class Query

source code

object --+
         |
        Query

Class for query string parsing and modification (stolen from svnmailer)
Instance Methods [hide private]
 
__init__(self, query=u'', delim='&', decode=None)
Initialization
source code
str
__str__(self)
Returns the query as string again
source code
 
__unicode__(self)
Unicode representation (just ascii decoded str() value)
source code
bool
__contains__(self, key)
Returns whether key occurs in the query as parameter name
source code
list
__getitem__(self, key)
Returns the value list for parameter named key
source code
 
__setitem__(self, key, value)
Replace all occurences of key with the new one
source code
 
replace(self, **kwargs)
Conveniently replace multiple key value pairs at once
source code
 
remove(self, keys)
Removes certain parameters from the query if present
source code
 
add(self, toadd)
Adds certain key value pairs to the query
source code
 
modify(self, remove=None, add=None, replace=None)
Summarizes certain query modification methods
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Class Variables [hide private]
_sre.SRE_Pattern _QUERYRE = re.compile(r'[&;]')
Regex for splitting a query string on possible delimiters (& and ;)
  _unicode = False
Instance Variables [hide private]
unicode _delim
The delimiter to use for reconstructing the query string
list _keyorder
Original order of the keys (['key', ...])
dict _query_dict
Dictionary of key->valuelist pairs ({'key': ['val1', 'val2'], ...})
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, query=u'', delim='&', decode=None)
(Constructor)

source code 
Initialization
Parameters:
  • query (unicode or Query) - The query string to store
  • delim (unicode) - The delimiter for reconstructing the query
  • decode (callable) - Parameter decoder
Overrides: object.__init__

__str__(self)
(Informal representation operator)

source code 
Returns the query as string again
Returns: str
The query as string (type depends on the input)
Overrides: object.__str__

__contains__(self, key)
(In operator)

source code 
Returns whether key occurs in the query as parameter name
Parameters:
  • key (unicode) - The key to lookup
Returns: bool
Does key occur?

__getitem__(self, key)
(Indexing operator)

source code 

Returns the value list for parameter named key

Don't modify the returned list without adjusting _keyorder, too. At best don't modify it directly at all :)

Parameters:
  • key (unicode) - The key to lookup
Returns: list
The value list (['val1', 'val2', ...])
Raises:
  • KeyError - The key does not exist

__setitem__(self, key, value)
(Index assignment operator)

source code 
Replace all occurences of key with the new one
Parameters:
  • key (unicode) - key to replace
  • value (unicode) - value to set

replace(self, **kwargs)

source code 
Conveniently replace multiple key value pairs at once
Parameters:
  • kwargs (dict) - key value pairs (unicode/unicode)

remove(self, keys)

source code 

Removes certain parameters from the query if present

Non-present parameters are silently ignored

Parameters:
  • keys (sequence) - The names of the parameters to remove

add(self, toadd)

source code 
Adds certain key value pairs to the query
Parameters:
  • toadd (iterable) - A sequence of key-value-pairs (((u'key', u'value), ...))

modify(self, remove=None, add=None, replace=None)

source code 

Summarizes certain query modification methods

replace is a convenience parameter, it's actually a combination of remove and add. The order of processing is:

  1. append the replace parameters to remove and add
  2. apply remove
  3. apply add
Parameters:
  • remove (sequence) - parameters to remove (see Query.remove method)
  • add (sequence) - parameters to add (see Query.add method)
  • replace (sequence) - parameters to override (see Query.add for the format)