<s> import gc <EOL> from django . core . urlresolvers import get_resolver <EOL> def get_url_pattern ( urlname , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> patterns = get_resolver ( None ) . reverse_dict . getlist ( urlname ) <EOL> if not args : <EOL> return '<STR_LIT>' % patterns [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> for pattern in patterns : <EOL> if pattern [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] == args : <EOL> return '<STR_LIT>' % pattern [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> def cached_property ( func ) : <EOL> """<STR_LIT>""" <EOL> def _set_cache ( self ) : <EOL> cache_attr = "<STR_LIT>" % func . __name__ <EOL> try : <EOL> return getattr ( self , cache_attr ) <EOL> except AttributeError : <EOL> value = func ( self ) <EOL> setattr ( self , cache_attr , value ) <EOL> return value <EOL> def _del_cache ( self ) : <EOL> cache_attr = "<STR_LIT>" % func . __name__ <EOL> try : <EOL> delattr ( self , cache_attr ) <EOL> except AttributeError : <EOL> pass <EOL> return property ( _set_cache , fdel = _del_cache ) <EOL> def immutable_property ( func ) : <EOL> """<STR_LIT>""" <EOL> def _set_attr ( self , value ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % func . __name__ ) <EOL> def _get_attr ( self ) : <EOL> return func ( self ) <EOL> def _del_attr ( self ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % func . __name__ ) <EOL> return property ( fget = _get_attr , fset = _set_attr , fdel = _del_attr ) <EOL> def key_sort ( l , * keys ) : <EOL> """<STR_LIT>""" <EOL> l = list ( l ) <EOL> for key in keys : <EOL> if key . startswith ( '<STR_LIT:->' ) : <EOL> reverse = True <EOL> key = key [ <NUM_LIT:1> : ] <EOL> else : <EOL> reverse = False <EOL> attrs = key . split ( '<STR_LIT:.>' ) <EOL> def fun ( x ) : <EOL> for attr in attrs : <EOL> x = getattr ( x , attr ) <EOL> if isinstance ( x , basestring ) : <EOL> x = x . lower ( ) <EOL> return x <EOL> l . sort ( key = fun , reverse = reverse ) <EOL> return l <EOL> def size_human ( size ) : <EOL> """<STR_LIT>""" <EOL> if size : <EOL> _abbrevs = [ <EOL> ( <NUM_LIT:1> << <NUM_LIT:50> L , '<STR_LIT:P>' ) , <EOL> ( <NUM_LIT:1> << <NUM_LIT> L , '<STR_LIT:T>' ) , <EOL> ( <NUM_LIT:1> << <NUM_LIT:30> L , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:1> << <NUM_LIT:20> L , '<STR_LIT:M>' ) , <EOL> ( <NUM_LIT:1> << <NUM_LIT:10> L , '<STR_LIT:k>' ) , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' ) ] <EOL> for factor , suffix in _abbrevs : <EOL> if size > factor : <EOL> break <EOL> if factor == <NUM_LIT:1> : <EOL> return "<STR_LIT>" % ( size , suffix ) <EOL> else : <EOL> return "<STR_LIT>" % ( float ( size ) / float ( factor ) , suffix ) <EOL> def restructured_table ( column_names , column_ids , object_list , truncate_len = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> single_cell_border = "<STR_LIT:+>" + ( truncate_len + <NUM_LIT:2> ) * "<STR_LIT:->" <EOL> border = len ( column_names ) * single_cell_border + "<STR_LIT:+>" <EOL> table = "<STR_LIT:\n>" + border + "<STR_LIT:\n>" <EOL> for column in column_names : <EOL> table += "<STR_LIT>" % column [ : truncate_len ] <EOL> table += "<STR_LIT>" + border + "<STR_LIT:\n>" <EOL> for obj in object_list : <EOL> for i in column_ids : <EOL> levels = i . split ( "<STR_LIT:.>" ) <EOL> attr = obj <EOL> for l in levels : <EOL> attr = getattr ( attr , l ) <EOL> table += "<STR_LIT>" % str ( attr ) [ : truncate_len ] <EOL> table += "<STR_LIT>" <EOL> table += border + "<STR_LIT:\n>" <EOL> return table <EOL> def paginate ( qs , start , end ) : <EOL> """<STR_LIT>""" <EOL> if start is None and end is None : <EOL> return ( qs , "<STR_LIT>" ) <EOL> int_msg = "<STR_LIT>" <EOL> neg_msg = "<STR_LIT>" <EOL> if start is not None : <EOL> try : <EOL> start = int ( start ) - <NUM_LIT:1> <EOL> if start < <NUM_LIT:0> : <EOL> return ( None , neg_msg % "<STR_LIT:start>" ) <EOL> except ValueError , TypeError : <EOL> return ( None , int_msg % "<STR_LIT:start>" ) <EOL> if end is not None : <EOL> try : <EOL> end = int ( end ) - <NUM_LIT:1> <EOL> if end < <NUM_LIT:0> : <EOL> return ( None , neg_msg % "<STR_LIT:end>" ) <EOL> except ValueError , TypeError : <EOL> return ( None , int_msg % "<STR_LIT:end>" ) <EOL> if start is None : <EOL> return ( qs [ : end ] , "<STR_LIT>" ) <EOL> elif end is None : <EOL> return ( qs [ start : ] , "<STR_LIT>" ) <EOL> else : <EOL> return ( qs [ start : end ] , "<STR_LIT>" ) <EOL> def queryset_iterator ( queryset , chunksize = <NUM_LIT:1000> ) : <EOL> """<STR_LIT>""" <EOL> pk = <NUM_LIT:0> <EOL> last_pk = queryset . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . pk <EOL> queryset = queryset . order_by ( '<STR_LIT>' ) <EOL> while pk < last_pk : <EOL> for row in queryset . filter ( pk__gt = pk ) [ : chunksize ] : <EOL> pk = row . pk <EOL> yield row <EOL> gc . collect ( ) </s>
