Created
December 20, 2013 18:19
-
-
Save pratikmallya/8059015 to your computer and use it in GitHub Desktop.
A style guide to use for Python code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Of course, you should always follow: http://www.python.org/dev/peps/pep-0008/ | |
Here are some more style guideline to write a better, self-documented code | |
''' | |
''' | |
1. Function Descriptions: | |
Every function should have a comment string describing: | |
* input `Parameters` | |
* triggered `Events` | |
* exceptions that it `Raises` | |
* `Returns` variabled | |
example: | |
''' | |
def query_keys(self, **kwargs): | |
""" Query for keys matching metadata provided as keyword arguments | |
This provides a very simple querying interface that returns precise | |
matches with the metadata. If no arguments are supplied, the query | |
will return the complete set of keys for the key-value store. | |
This is equivalent to ``self.query(**kwargs).keys()``, but potentially | |
more efficiently implemented. | |
Parameters | |
---------- | |
kwargs : | |
Arguments where the keywords are metadata keys, and values are | |
possible values for that metadata item. | |
Returns | |
------- | |
result : iterable | |
An iterable of key-value store keys whose metadata matches all the | |
specified values for the specified metadata keywords. | |
""" | |
for key, value in self._store.items(): | |
metadata = value[1] | |
if all(metadata.get(arg) == value for arg, value in kwargs.items()): | |
yield key | |
def set_metadata(self, key, metadata): | |
""" Set new metadata for a given key in the key-value store. | |
This replaces the existing metadata set for the key with a new set of | |
metadata. If the key does not already exist, it tacitly creates an | |
empty data object. | |
Parameters | |
---------- | |
key : string | |
The key for the resource in the key-value store. They key is a unique | |
identifier for the resource within the key-value store. | |
metadata : dict | |
A dictionary of metadata to associate with the key. The dictionary | |
keys should be strings which are valid Python identifiers. | |
Events | |
------ | |
StoreSetEvent : | |
On successful completion of a transaction, a StoreSetEvent should be | |
emitted with the key & metadata | |
""" | |
data = self._store.get(key, ('', {}))[0] | |
self.set(key, StringValue(data=data, metadata=metadata)) | |
def get_metadata(self, key, select=None): | |
""" Retrieve the metadata for a given key in the key-value store. | |
Parameters | |
---------- | |
key : string | |
The key for the resource in the key-value store. They key is a unique | |
identifier for the resource within the key-value store. | |
select : iterable of strings or None | |
Which metadata keys to populate in the result. If unspecified, then | |
return the entire metadata dictionary. | |
Returns | |
------- | |
metadata : dict | |
A dictionary of metadata associated with the key. The dictionary | |
has keys as specified by the metadata_keys argument. | |
Raises | |
------ | |
KeyError : | |
This will raise a key error if the key is not present in the store, | |
and if any metadata key is requested which is not present in the | |
metadata. | |
""" | |
metadata = self._store[key][1] | |
if select is not None: | |
return dict((metadata_key, metadata[metadata_key]) | |
for metadata_key in select if metadata_key in metadata) | |
return metadata.copy() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment