module: common.utils

Common utilities.

Note

Many of these functions are ad hoc and may change between versions.

class common.utils.DateTimeJSONEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None)

Bases: json.encoder.JSONEncoder

JSON encoder that can handle datetime instances

default(obj)
common.utils.all_aggregations(queryset, key)

Performs all available aggregations on a queryset

common.utils.camel_to_underscore(name)

Convert CamelCase names to camel_case style

common.utils.captcha_random_chars()

Return a string to use for a CAPTCHA

common.utils.celery_available()

Return true if celery is available

common.utils.chunk_list_generator(lst, chunksize)

Generator that chunks a list lst into sublists of size chunksize

common.utils.compute_entropy(values)

Return the shannon entropy of a list of values.

Parameters:values – observations that will be processed into a histogram
common.utils.compute_label_reward(label)

compute the per-label reward

common.utils.create_image_grid_qset(qset, image_attr, ncols=40, size=64, padding=0, show_progress=False, downsample_ratio=1, max_qset_size=None)

Greate a grid of images from a queryset

common.utils.create_image_pair_grid_list(image_pairs, ncols=64, size=64, padding=0, show_progress=False, downsample_ratio=1)

Greate a grid of images from a list of files

common.utils.dict_max_value(d)

Return the (key, value) with the maximum value. This is the fastest way of doing this: http://stackoverflow.com/questions/268272/getting-key-with-maximum-value-in-dictionary

common.utils.dict_union(a, b)

Return the union of two dictionaries without editing either. If a key exists in both dictionaries, the second value is used.

common.utils.dump_model_csv_view(app_name, model_name)
common.utils.dump_queryset_to_static_csv(queryset, values, filename, dirname='generated')

Create a CSV in the staticfiles of a queryset. Values is a list of fields to grab. Optionally, you may specify a rename a field by specifying a tuple instead of a string as (field_name, csv_name). The path is relative to the staticfiles root. The URL of the file is returned.

common.utils.estimate_count_fast(app, model)

postgres really sucks at full table counts, this is a faster version see: http://wiki.postgresql.org/wiki/Slow_Counting and http://chase-seibert.github.com/blog/2012/06/01/djangopostgres-optimize-count-by-replacing-with-an-estimate.html

common.utils.get_celery_worker_status()

Detects if working

common.utils.get_content_tuple(instance)

Return (content type id, object id)

common.utils.get_content_type_id(instance)

Return the ContentType id associated with instance

common.utils.get_foreign_key(model_class, field_name)

Return the foreign keym model for model_class.field_name

common.utils.get_opensurfaces_storage()
common.utils.get_upload_dir(obj, attr, time=None)

Returns the directory used to save obj.attr files

common.utils.group_iterable_by_attr(iterable, attr)

Returns a dictionary mapping { val : [elements such that e.attr = val] }

common.utils.has_foreign_key(model_class, field_name)

Return true if model_class has a foreign key with a given name

common.utils.html_error_response(request, error)
common.utils.import_module(name)

Import a module by name and return it

common.utils.import_modules(module_names)

Import a list of modules by name and return them

common.utils.json_error_response(error)

Return a HttpResponse containing the JSON message {"result": "error", "message": error}

common.utils.json_response(d)

Return a HttpResponse containing the JSON message d union {"result": "success"}

common.utils.json_success_response()

Return a HttpResponse containing the JSON message {"message": "success", "result": "success"}

common.utils.md5sum(filename_or_file)

Returns the md5 hash of a file

common.utils.pickle_zip(fname, obj)

Pickle and gzip obj to disk with filename fname

common.utils.prepare_votes_bar(obj, obj_votes_attr, obj_result_attr, vote_attr, name)
common.utils.prepare_votes_bar_impl(votes, score, name)
common.utils.progress_bar(l, show_progress=True)

Returns an iterator for a list or queryset that renders a progress bar with a countdown timer

common.utils.queryset_progress_bar(queryset)

Returns an iterator for a queryset that renders a progress bar with a countdown timer

common.utils.random_lowercase_str(N)

Return a random lowercase string of length N

common.utils.recursive_dict_exclude(d, excludes)

Recursively exclude a key from a nested dictionary

common.utils.recursive_sum(x)

Recursively sums together numbers contained in x. Supports: int, float, dict, list, tuple, str, unicode, and json-encoded strings.

common.utils.rgb_to_hex(r, g, b)

Convert (r, g, b) in range [0.0, 1.0] to "RRGGBB" hex string.

common.utils.run_async_if_celery(task, *args, **kwargs)

Run async if celery is online, else run serially

common.utils.save_obj_attr_base64_image(obj, attr, screenshot, suffix='', format='png', save=True)

Saves a screenshot to a file field (attr) on a model instance (obj). The filename is a hash of the image contant plus an optional suffix.

common.utils.save_obj_attr_file(obj, attr, filename, content, save=True, time=None)

Saves a file onto an attribute of an object

common.utils.save_obj_attr_image(obj, attr, img, suffix='', format='png', save=True)

Saves a PIL image to a file field (attr) on a model instance (obj). The filename is a hash of the image contant plus an optional suffix.

common.utils.scale_dict_values(d, scale, exclude=[])

Return a new dictionary with numeric values multiplied by scale.

Parameters:exclude – collection of items to exclude.
common.utils.single_instance_task(timeout=43200)

Decorator that ensures that a celery task is only run once. Default timeout is 12 hours.

See: http://stackoverflow.com/questions/4095940/running-unique-tasks-with-celery See: http://ask.github.com/celery/cookbook/tasks.html#ensuring-a-task-is-only-executed-one-at-a-time

Note

This only works if all tasks share the same django cache.

common.utils.to_int_percent(items)

Converts a list of numbers to a list of integers that exactly sum to 100.

common.utils.todo_view(request)

Renders a placeholder view

common.utils.unique_instance_name(obj)

Return a unique alpha-numeric string for a model instance (base 62). The name is generated from the current time, the object id (or count if not created), its content type id, and the server MAC address.

common.utils.unit_interval_infinite_generator()

Never-ending generator that yields all floats in the range [0, 1], in the order:

0, 1, 1/2, 1/4, 3/4, 1/8, 3/8, 5/8, 7/8, 1/16, 3/16, 5/16, ...

The same value is never returned twice, and eventually all floating point numbers in the range [0, 1] are returned. The point of this generator is to sweep a range, iteratively increasing the density of the sweep, so that if you want to stop the sweep early, you have still swept the majority of the range.

Returns:floats in the range [0, 1].
common.utils.unpickle_zip(fname)

Unpickle a gzipped file from disk with filename fname