code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
msg = aioxmpp.stanza.Message(
to=self.to,
from_=self.sender,
type_=aioxmpp.MessageType.CHAT,
)
msg.body[None] = self.body
# Send metadata using xep-0004: Data Forms (https://xmpp.org/extensions/xep-0004.html)
if len(self.metadata):
... | def prepare(self) | Returns an aioxmpp.stanza.Message built from the Message and prepared to be sent.
Returns:
aioxmpp.stanza.Message: the message prepared to be sent | 3.568941 | 3.485494 | 1.023941 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((hostname, 0))
return s.getsockname()[1] | def unused_port(hostname) | Return a port that is unused on the current host. | 1.796214 | 1.776591 | 1.011045 |
await runner.setup()
agent.web.server = aioweb.TCPSite(runner, hostname, port)
await agent.web.server.start()
logger.info(f"Serving on http://{hostname}:{port}/") | async def start_server_in_loop(runner, hostname, port, agent) | Listens to http requests and sends them to the webapp.
Args:
runner: AppRunner to process the http requests
hostname: host name to listen from.
port: port to listen from.
agent: agent that owns the web app. | 3.874291 | 3.711265 | 1.043927 |
self.hostname = hostname if hostname else "localhost"
if port:
self.port = port
elif not self.port:
self.port = unused_port(self.hostname)
if templates_path:
self.loaders.insert(0, jinja2.FileSystemLoader(templates_path))
self._set... | def start(self, hostname=None, port=None, templates_path=None) | Starts the web interface.
Args:
hostname (str, optional): host name to listen from. (Default value = None)
port (int, optional): port to listen from. (Default value = None)
templates_path (str, optional): path to look for templates. (Default value = None) | 4.220976 | 4.464782 | 0.945394 |
if raw:
fn = controller
else:
fn = self._prepare_controller(controller, template)
self.app.router.add_get(path, fn) | def add_get(self, path, controller, template, raw=False) | Setup a route of type GET
Args:
path (str): URL to listen to
controller (coroutine): the coroutine to handle the request
template (str): the template to render the response or None if it is a JSON response
raw (bool): indicates if post-processing (jinja, json, etc) is ne... | 3.562404 | 4.076369 | 0.873916 |
if template:
fn = aiohttp_jinja2.template(template_name=template)(controller)
else:
fn = self._parse_json_response(controller)
return fn | def _prepare_controller(self, controller, template) | Wraps the controller wether to render a jinja template or to return a json response (if template is None)
Args:
controller (coroutine): the coroutine to be wrapped
template (str): the name of the template or None
Returns:
coroutine: a wrapped coroutine of the control... | 7.09617 | 5.723289 | 1.239876 |
self.agent = agent
self.queue = asyncio.Queue(loop=self.agent.loop)
self.presence = agent.presence
self.web = agent.web | def set_agent(self, agent) | Links behaviour with its owner agent
Args:
agent (spade.agent.Agent): the agent who owns the behaviour | 5.118396 | 5.577527 | 0.917682 |
if self.template:
return self.template.match(message)
return True | def match(self, message: Message) -> bool | Matches a message with the behaviour's template
Args:
message(spade.message.Message): the message to match with
Returns:
bool: wheter the messaged matches or not | 7.484697 | 5.931438 | 1.261869 |
self.agent.set(name, value) | def set(self, name: str, value: Any) -> None | Stores a knowledge item in the agent knowledge base.
Args:
name (str): name of the item
value (Any): value of the item | 18.133505 | 7.401462 | 2.44999 |
self.agent.submit(self._start())
self.is_running = True | def start(self) | starts behaviour in the event loop | 15.880044 | 11.807118 | 1.344955 |
self.agent._alive.wait()
try:
await self.on_start()
except Exception as e:
logger.error("Exception running on_start in behaviour {}: {}".format(self, e))
self.kill(exit_code=e)
await self._step()
self._is_done.clear() | async def _start(self) | Start coroutine. runs on_start coroutine and then
runs the _step coroutine where the body of the behaviour
is called. | 6.758598 | 4.668417 | 1.447728 |
self._force_kill.set()
if exit_code is not None:
self._exit_code = exit_code
logger.info("Killing behavior {0} with exit code: {1}".format(self, exit_code)) | def kill(self, exit_code: Any = None) | Stops the behaviour
Args:
exit_code (object, optional): the exit code of the behaviour (Default value = None) | 4.509064 | 3.97882 | 1.133267 |
if self._done() or self.is_killed():
return self._exit_code
else:
raise BehaviourNotFinishedException | def exit_code(self) -> Any | Returns the exit_code of the behaviour.
It only works when the behaviour is done or killed,
otherwise it raises an exception.
Returns:
object: the exit code of the behaviour | 10.223904 | 5.771909 | 1.771321 |
while not self._done() and not self.is_killed():
try:
await self._run()
await asyncio.sleep(0) # relinquish cpu
except Exception as e:
logger.error("Exception running behaviour {}: {}".format(self, e))
self.kill(ex... | async def _step(self) | Main loop of the behaviour.
checks whether behaviour is done or killed,
ortherwise it calls run() coroutine. | 3.415324 | 2.70841 | 1.261007 |
if not msg.sender:
msg.sender = str(self.agent.jid)
logger.debug(f"Adding agent's jid as sender to message: {msg}")
await self.agent.container.send(msg, self)
msg.sent = True
self.agent.traces.append(msg, category=str(self)) | async def send(self, msg: Message) | Sends a message.
Args:
msg (spade.message.Message): the message to be sent. | 6.835614 | 6.127772 | 1.115514 |
if timeout:
coro = self.queue.get()
try:
msg = await asyncio.wait_for(coro, timeout=timeout)
except asyncio.TimeoutError:
msg = None
else:
try:
msg = self.queue.get_nowait()
except asynci... | async def receive(self, timeout: float = None) -> Union[Message, None] | Receives a message for this behaviour.
If timeout is not None it returns the message or "None"
after timeout is done.
Args:
timeout (float): number of seconds until return
Returns:
spade.message.Message: a Message or None | 1.798833 | 2.166511 | 0.830291 |
if value < 0:
raise ValueError("Period must be greater or equal than zero.")
self._period = timedelta(seconds=value) | def period(self, value: float) | Set the period.
Args:
value (float): seconds | 4.963177 | 5.670765 | 0.875222 |
if not issubclass(state.__class__, State):
raise AttributeError("state must be subclass of spade.behaviour.State")
self._states[name] = state
if initial:
self.current_state = name | def add_state(self, name: str, state: State, initial: bool = False) | Adds a new state to the FSM.
Args:
name (str): the name of the state, which is used as its identifier.
state (spade.behaviour.State): The state class
initial (bool, optional): wether the state is the initial state or not. (Only one initial state is allowed) (Default value = False) | 4.691043 | 3.360365 | 1.395992 |
self._transitions[source].append(dest) | def add_transition(self, source: str, dest: str) | Adds a transition from one state to another.
Args:
source (str): the name of the state from where the transition starts
dest (str): the name of the state where the transition ends | 11.69897 | 8.799541 | 1.329498 |
if dest not in self._states or source not in self._states:
raise NotValidState
elif dest not in self._transitions[source]:
raise NotValidTransition
return True | def is_valid_transition(self, source: str, dest: str) -> bool | Checks if a transitions is registered in the FSM
Args:
source (str): the source state name
dest (str): the destination state name
Returns:
bool: wether the transition is valid or not | 4.941098 | 4.500643 | 1.097865 |
graph = "digraph finite_state_machine { rankdir=LR; node [fixedsize=true];"
for origin, dest in self._transitions.items():
origin = origin.replace(" ", "_")
for d in dest:
d = d.replace(" ", "_")
graph += "{0} -> {1};".format(origin, d)
... | def to_graphviz(self) -> str | Converts the FSM behaviour structure to Graphviz syntax
Returns:
str: the graph in Graphviz syntax | 3.353281 | 3.239137 | 1.035239 |
'''Retrieve a window given its name or function handle.
Parameters
----------
name_or_function : str or callable
If a function, returns `name_or_function(**kwargs)`.
If a string, and it matches the name of one of the defined
filter functions, the corresponding function is calle... | def get_filter(name_or_function, **kwargs) | Retrieve a window given its name or function handle.
Parameters
----------
name_or_function : str or callable
If a function, returns `name_or_function(**kwargs)`.
If a string, and it matches the name of one of the defined
filter functions, the corresponding function is called with ... | 3.794114 | 1.409223 | 2.692345 |
'''Retrieve a pre-computed filter.
Parameters
----------
filter_name : str
The key of the filter, e.g., 'kaiser_fast'
Returns
-------
half_window : np.ndarray
The right wing of the interpolation filter
precision : int > 0
The number of samples between zero-cros... | def load_filter(filter_name) | Retrieve a pre-computed filter.
Parameters
----------
filter_name : str
The key of the filter, e.g., 'kaiser_fast'
Returns
-------
half_window : np.ndarray
The right wing of the interpolation filter
precision : int > 0
The number of samples between zero-crossings o... | 4.649641 | 1.752094 | 2.653762 |
signature = inspect.signature(func)
return [k
for k, v in signature.parameters.items()
if v.default is not inspect.Parameter.empty or
v.kind != inspect.Parameter.POSITIONAL_OR_KEYWORD] | def default_values_of(func) | Return the defaults of the function `func`. | 3.056693 | 2.722693 | 1.122672 |
defaults = default_values_of(func)
args = arguments_of(func)
if defaults:
args = args[:-len(defaults)]
return args | def required_arguments(func) | Return all arguments of a function that do not have a default value. | 5.990284 | 4.778828 | 1.253505 |
merged_style = merge_styles([DEFAULT_STYLE, style])
validator = build_validator(validate)
def get_prompt_tokens():
return [("class:qmark", qmark),
("class:question", ' {} '.format(message))]
p = PromptSession(get_prompt_tokens,
style=merged_style,
... | def text(message: Text,
default: Text = "",
validate: Union[Type[Validator],
Callable[[Text], bool],
None] = None, # noqa
qmark: Text = DEFAULT_QUESTION_PREFIX,
style: Optional[Style] = None,
**kwargs: Any) -> Question | Prompt the user to enter a free text message.
This question type can be used to prompt the user for some text input.
Args:
message: Question text
default: Default value will be returned if the user just hits
enter.
validate: Require the entered valu... | 4.609854 | 5.325562 | 0.865609 |
if self.should_skip_question:
return self.default
try:
sys.stdout.flush()
return await self.unsafe_ask_async(patch_stdout)
except KeyboardInterrupt:
print("\n{}\n".format(kbi_msg))
return None | async def ask_async(self,
patch_stdout: bool = False,
kbi_msg: str = DEFAULT_KBI_MESSAGE) -> Any | Ask the question using asyncio and return user response. | 5.184104 | 4.548072 | 1.139847 |
if self.should_skip_question:
return self.default
try:
return self.unsafe_ask(patch_stdout)
except KeyboardInterrupt:
print("\n{}\n".format(kbi_msg))
return None | def ask(self,
patch_stdout: bool = False,
kbi_msg: str = DEFAULT_KBI_MESSAGE) -> Any | Ask the question synchronously and return user response. | 5.516662 | 4.629075 | 1.191742 |
if patch_stdout:
with prompt_toolkit.patch_stdout.patch_stdout():
return self.application.run()
else:
return self.application.run() | def unsafe_ask(self, patch_stdout: bool = False) -> Any | Ask the question synchronously and return user response.
Does not catch keyboard interrupts. | 4.605752 | 3.904461 | 1.179613 |
self.should_skip_question = condition
self.default = default
return self | def skip_if(self, condition: bool, default: Any = None) -> 'Question' | Skip the question if flag is set and return the default instead. | 6.019691 | 4.901636 | 1.228099 |
if not utils.ACTIVATED_ASYNC_MODE:
await utils.activate_prompt_toolkit_async_mode()
if patch_stdout:
# with prompt_toolkit.patch_stdout.patch_stdout():
return await self.application.run_async().to_asyncio_future()
else:
return await self... | async def unsafe_ask_async(self, patch_stdout: bool = False) -> Any | Ask the question using asyncio and return user response.
Does not catch keyboard interrupts. | 5.662023 | 5.41754 | 1.045128 |
default_container = ps.layout.container
default_buffer_window = \
default_container.get_children()[0].content.get_children()[1].content
assert isinstance(default_buffer_window, Window)
# this forces the main window to stay as small as possible, avoiding
# empty lines in selections
... | def _fix_unecessary_blank_lines(ps: PromptSession) -> None | This is a fix for additional empty lines added by prompt toolkit.
This assumes the layout of the default session doesn't change, if it
does, this needs an update. | 10.141758 | 8.323491 | 1.21845 |
ps = PromptSession(get_prompt_tokens, reserve_space_for_menu=0, **kwargs)
_fix_unecessary_blank_lines(ps)
return Layout(HSplit([
ps.layout.container,
ConditionalContainer(
Window(ic),
filter=~IsDone()
)
])) | def create_inquirer_layout(
ic: InquirerControl,
get_prompt_tokens: Callable[[], List[Tuple[Text, Text]]],
**kwargs) -> Layout | Create a layout combining question and inquirer selection. | 6.534598 | 6.083591 | 1.074135 |
if isinstance(c, Choice):
return c
elif isinstance(c, str):
return Choice(c, c)
else:
return Choice(c.get('name'),
c.get('value'),
c.get('disabled', None),
c.get('checked')... | def build(c: Union[Text, 'Choice', Dict[Text, Any]]) -> 'Choice' | Create a choice object from different representations. | 2.634748 | 2.66104 | 0.99012 |
return text.text(message, default, validate, qmark, style,
is_password=True, **kwargs) | def password(message: Text,
default: Text = "",
validate: Union[Type[Validator],
Callable[[Text], bool],
None] = None, # noqa
qmark: Text = DEFAULT_QUESTION_PREFIX,
style: Optional[Style] = None,
... | Question the user to enter a secret text not displayed in the prompt.
This question type can be used to prompt the user for information
that should not be shown in the command line. The typed text will be
replaced with `*`.
Args:
message: Question text
default: Defau... | 6.128166 | 7.630172 | 0.803149 |
if choices is None or len(choices) == 0:
raise ValueError('A list of choices needs to be provided.')
if use_shortcuts and len(choices) > len(InquirerControl.SHORTCUT_KEYS):
raise ValueError('A list with shortcuts supports a maximum of {} '
'choices as this is the m... | def select(message: Text,
choices: List[Union[Text, Choice, Dict[Text, Any]]],
default: Optional[Text] = None,
qmark: Text = DEFAULT_QUESTION_PREFIX,
style: Optional[Style] = None,
use_shortcuts: bool = False,
use_indicator: bool = False,
**kw... | Prompt the user to select one item from the list of choices.
The user can only select one option.
Args:
message: Question text
choices: Items shown in the selection, this can contain `Choice` or
or `Separator` objects or simple items as strings. Passing
`Choi... | 3.01674 | 3.039133 | 0.992632 |
return Form(*(FormField(k, q) for k, q in kwargs.items())) | def form(**kwargs: Question) | Create a form with multiple questions.
The parameter name of a question will be the key for the answer in
the returned dict. | 9.520675 | 13.125999 | 0.72533 |
if isinstance(questions, dict):
questions = [questions]
answers = answers or {}
for question_config in questions:
# import the question
if 'type' not in question_config:
raise PromptParameterException('type')
if 'name' not in question_config:
r... | def prompt(questions: List[Dict[Text, Any]],
answers: Optional[Dict[Text, Any]] = None,
patch_stdout: bool = False,
true_color: bool = False,
kbi_msg: Text = DEFAULT_KBI_MESSAGE,
**kwargs) | Prompt the user for input on all the questions. | 2.712409 | 2.668319 | 1.016524 |
merged_style = merge_styles([DEFAULT_STYLE, style])
status = {'answer': None}
def get_prompt_tokens():
tokens = []
tokens.append(("class:qmark", qmark))
tokens.append(("class:question", ' {} '.format(message)))
if status['answer'] is not None:
answer = '... | def confirm(message: Text,
default: bool = True,
qmark: Text = DEFAULT_QUESTION_PREFIX,
style: Optional[Style] = None,
**kwargs: Any) -> Question | Prompt the user to confirm or reject.
This question type can be used to prompt the user for a confirmation
of a yes-or-no question. If the user just hits enter, the default
value will be returned.
Args:
message: Question text
default: Default value will be returned i... | 2.543222 | 2.642639 | 0.96238 |
return select.select(message, choices, default, qmark, style,
use_shortcuts=True,
**kwargs) | def rawselect(message: Text,
choices: List[Union[Text, Choice, Dict[Text, Any]]],
default: Optional[Text] = None,
qmark: Text = DEFAULT_QUESTION_PREFIX,
style: Optional[Style] = None,
**kwargs: Any) -> Question | Ask the user to select one item from a list of choices using shortcuts.
The user can only select one option.
Args:
message: Question text
choices: Items shown in the selection, this can contain `Choice` or
or `Separator` objects or simple items as strings. Pass... | 6.463267 | 10.950788 | 0.59021 |
apps = get_installed_apps()
connection = self.connection.connection.alias
keyspace = self.connection.connection.keyspace
for app in apps:
self._cql_models[app.__name__] = get_cql_models(
app, connection=connection, keyspace=keyspace) | def _discover_models(self) | Return a dict containing a list of cassandra.cqlengine.Model classes
within installed App. | 5.874783 | 4.261144 | 1.378687 |
all_models = list(chain.from_iterable(self.cql_models.values()))
tables = [model.column_family_name(include_keyspace=False)
for model in all_models]
return tables | def django_table_names(self, only_existing=False, **kwargs) | Returns a list of all table names that have associated cqlengine models
and are present in settings.INSTALLED_APPS. | 5.750349 | 4.816297 | 1.193936 |
# Avoid migration code being executed
if cursor:
return []
connection = self.connection.connection
keyspace_name = connection.keyspace
if not connection.cluster.schema_metadata_enabled and \
keyspace_name not in connection.cluster.metadata.ke... | def table_names(self, cursor=None, **kwargs) | Returns all table names in current keyspace | 4.193095 | 3.77596 | 1.110471 |
for models in self.connection.introspection.cql_models.values():
for model in models:
model.__keyspace__ = keyspace | def set_models_keyspace(self, keyspace) | Set keyspace for all connection models | 7.394964 | 6.096973 | 1.212891 |
return self.model(
session_key=self._get_or_create_session_key(),
session_data=self.encode(data),
expire_date=self.get_expiry_date(),
) | def create_model_instance(self, data) | Return a new instance of the session model object, which represents the
current session state. Intended to be used for saving the session data
to the database.
:param data: | 4.643691 | 3.955486 | 1.173987 |
if self.session_key is None:
return self.create()
data = self._get_session(no_load=must_create)
obj = self.create_model_instance(data)
obj.save() | def save(self, must_create=False) | Saves the current session data to the database. If 'must_create' is
True, a database error will be raised if the saving operation doesn't
create a *new* entry (as opposed to possibly updating an existing
entry).
:param must_create: | 5.597275 | 6.774402 | 0.826239 |
from importlib import import_module
for app_name in settings.INSTALLED_APPS:
try:
import_module('.management', app_name)
except SystemError:
# We get SystemError if INSTALLED_APPS contains the
# name of a class rather tha... | def _import_management() | Import the 'management' module within each installed app, to register
dispatcher events. | 4.972851 | 4.73415 | 1.050421 |
if django.VERSION >= (1, 7):
from django.apps import apps
return [a.models_module for a in apps.get_app_configs()
if a.models_module is not None]
else:
from django.db import models
return models.get_apps() | def get_installed_apps() | Return list of all installed apps | 2.332401 | 2.257026 | 1.033396 |
from .models import DjangoCassandraModel
models = []
single_cassandra_connection = len(list(get_cassandra_connections())) == 1
is_default_connection = connection == DEFAULT_DB_ALIAS or \
single_cassandra_connection
for name, obj in inspect.getmembers(app):
cql_model_types = (
... | def get_cql_models(app, connection=None, keyspace=None) | :param app: django models module
:param connection: connection name
:param keyspace: keyspace
:return: list of all cassandra.cqlengine.Model within app that should be
synced to keyspace. | 3.177635 | 3.112829 | 1.020819 |
from django.db import connections
for alias in connections:
engine = connections[alias].settings_dict.get('ENGINE', '')
if engine == 'django_cassandra_engine':
yield alias, connections[alias] | def get_cassandra_connections() | :return: List of tuples (db_alias, connection) for all cassandra
connections in DATABASES dict. | 3.332091 | 2.744583 | 1.214061 |
for alias, conn in get_cassandra_connections():
if conn.connection.default:
return alias, conn
return list(get_cassandra_connections())[0] | def get_default_cassandra_connection() | Return first default cassandra connection
:return: | 6.21391 | 6.621999 | 0.938374 |
for _alias, connection in get_cassandra_connections():
if alias is not None:
if alias == _alias:
return connection
elif name is not None:
if name == connection.settings_dict['NAME']:
return connection
else:
return conn... | def get_cassandra_connection(alias=None, name=None) | :return: cassandra connection matching alias or name or just first found. | 2.764229 | 2.679268 | 1.03171 |
self._change_cassandra_engine_name('django.db.backends.dummy')
try:
super(Command, self).handle(*args, **options)
finally:
self._change_cassandra_engine_name('django_cassandra_engine') | def handle(self, *args, **options) | Pretend django_cassandra_engine to be dummy database backend
with no support for migrations. | 4.243947 | 2.478718 | 1.712154 |
for table in tables:
qs = "TRUNCATE {}".format(table)
self.connection.connection.execute(qs)
return [] | def sql_flush(self, style, tables, sequences, allow_cascade=False) | Truncate all existing tables in current keyspace.
:returns: an empty list | 5.353479 | 6.446927 | 0.830392 |
pk_field_names = tuple(f.name for f in model._get_primary_key_columns())
def append_field(field_name):
if field_name not in real_field_names:
real_field_names.append(field_name)
real_field_names = []
for name in field_names:
if name == 'pk':
for real_pk_fie... | def convert_pk_field_names_to_real(model, field_names) | Convert field names including 'pk' to the real field names:
>>> convert_pk_field_names_to_real(['pk', 'another_field'])
['real_pk_field', 'another_field'] | 2.041292 | 2.218511 | 0.920118 |
getattr(self, self._private_fields_name).append(field)
self._expire_cache(reverse=True)
self._expire_cache(reverse=False) | def add_field(self, field, **kwargs) | Add each field as a private field. | 8.160274 | 6.540664 | 1.247622 |
methods_to_add = (
django_field_methods.value_from_object,
django_field_methods.value_to_string,
django_field_methods.get_attname,
django_field_methods.get_cache_name,
django_field_methods.pre_save,
django_field_methods.get_prep_va... | def _give_columns_django_field_attributes(self) | Add Django Field attributes to each cqlengine.Column instance.
So that the Django Options class may interact with it as if it were
a Django Field. | 2.116593 | 2.05044 | 1.032263 |
if name == 'pk':
return cls._meta.get_field(cls._meta.pk.name)
return cls._columns[name] | def _get_column(cls, name) | Based on cqlengine.models.BaseModel._get_column.
But to work with 'pk' | 5.023652 | 4.096198 | 1.226418 |
next = request.POST.get('next', request.GET.get('next',
request.META.get('HTTP_REFERER', None)))
if not next:
next = request.path
return next | def _get_next(request) | The part that's the least straightforward about views in this module is
how they determine their redirects after they have finished computation.
In short, they will try and determine the next place to go in the
following order:
1. If there is a variable named ``next`` in the *POST* parameters, the
... | 2.780921 | 3.189746 | 0.871831 |
EXIF_ORIENTATION_STEPS = {
1: [],
2: ['FLIP_LEFT_RIGHT'],
3: ['ROTATE_180'],
4: ['FLIP_TOP_BOTTOM'],
5: ['ROTATE_270', 'FLIP_LEFT_RIGHT'],
6: ['ROTATE_270'],
7: ['ROTATE_90', 'FLIP_LEFT_RIGHT'],
8: ['ROTATE_... | def transpose_image(self, image) | Transpose based on EXIF information.
Borrowed from django-imagekit:
imagekit.processors.Transpose | 2.056371 | 1.952463 | 1.053219 |
if isinstance(user_or_username, get_user_model()):
user_or_username = get_username(user_or_username)
key = six.u('%s_%s_%s') % (prefix, user_or_username, size)
return six.u('%s_%s') % (slugify(key)[:100],
hashlib.md5(force_bytes(key)).hexdigest()) | def get_cache_key(user_or_username, size, prefix) | Returns a cache key consisten of a username and image size. | 2.875692 | 2.90023 | 0.991539 |
if not settings.AVATAR_CACHE_ENABLED:
def decorator(func):
return func
return decorator
def decorator(func):
def cached_func(user, size=None, **kwargs):
prefix = func.__name__
cached_funcs.add(prefix)
key = get_cache_key(user, size or... | def cache_result(default_size=settings.AVATAR_DEFAULT_SIZE) | Decorator to cache the result of functions that take a ``user`` and a
``size`` value. | 2.392862 | 2.368182 | 1.010421 |
sizes = set(settings.AVATAR_AUTO_GENERATE_SIZES)
if size is not None:
sizes.add(size)
for prefix in cached_funcs:
for size in sizes:
cache.delete(get_cache_key(user, size, prefix)) | def invalidate_cache(user, size=None) | Function to be called when saving or changing an user's avatars. | 4.348341 | 3.992032 | 1.089255 |
alt = six.text_type(user)
url = reverse('avatar_render_primary', kwargs={'user': user, 'size': size})
return ( %
(url, alt, size, size)) | def primary_avatar(user, size=settings.AVATAR_DEFAULT_SIZE) | This tag tries to get the default avatar for a user without doing any db
requests. It achieve this by linking to a special view that will do all the
work for us. If that special view is then cached by a CDN for instance,
we will avoid many db calls. | 9.291212 | 10.440113 | 0.889953 |
'''
Generate a public token for sandbox testing.
:param str institution_id:
:param [str] initial_products:
:param str webhook:
'''
options = _options or {}
if webhook is not None:
options['webhook'] = webhook
transacti... | def create(self,
institution_id,
initial_products,
_options=None,
webhook=None,
transactions__start_date=None,
transactions__end_date=None,
) | Generate a public token for sandbox testing.
:param str institution_id:
:param [str] initial_products:
:param str webhook: | 2.442744 | 1.829457 | 1.335229 |
'''
Return accounts and transactions for an item.
(`HTTP docs <https://plaid.com/docs/api/#transactions>`__)
The transactions in the response are paginated -- compare the number of
transactions received so far against response['total_transactions'] to
determine whether t... | def get(self,
access_token,
start_date,
end_date,
_options=None,
account_ids=None,
count=None,
offset=None,
) | Return accounts and transactions for an item.
(`HTTP docs <https://plaid.com/docs/api/#transactions>`__)
The transactions in the response are paginated -- compare the number of
transactions received so far against response['total_transactions'] to
determine whether to fetch another page... | 3.026066 | 1.259172 | 2.403218 |
'''
Create an error of the right class from an API response.
:param response dict Response JSON
'''
cls = PLAID_ERROR_TYPE_MAP.get(response['error_type'], PlaidError)
return cls(response['error_message'],
response['error_type'],
... | def from_response(response) | Create an error of the right class from an API response.
:param response dict Response JSON | 5.228945 | 2.966356 | 1.76275 |
'''
Create an asset report.
:param [str] access_tokens: A list of access tokens, one token for
each Item to be included in the Asset
Report.
:param int days_requested: Days of transaction history re... | def create(self,
access_tokens,
days_requested,
options=None) | Create an asset report.
:param [str] access_tokens: A list of access tokens, one token for
each Item to be included in the Asset
Report.
:param int days_requested: Days of transaction history requested
... | 4.309103 | 1.461546 | 2.948319 |
'''
Create a new, refreshed asset report based on an existing asset report.
:param str asset_report_token: The existing Asset Report's asset
report token.
:param int days_requested: Days of transaction history
... | def refresh(self,
asset_report_token,
days_requested,
options=None) | Create a new, refreshed asset report based on an existing asset report.
:param str asset_report_token: The existing Asset Report's asset
report token.
:param int days_requested: Days of transaction history
... | 3.839099 | 1.477182 | 2.598935 |
'''
Retrieves an asset report.
:param str asset_report_token: The asset report token for the
asset report you created.
:param bool include_insights: An optional boolean specifying
whether ... | def get(self,
asset_report_token,
include_insights=False) | Retrieves an asset report.
:param str asset_report_token: The asset report token for the
asset report you created.
:param bool include_insights: An optional boolean specifying
whether we should retrieve t... | 4.534155 | 1.555583 | 2.914762 |
'''Make a post request with client_id and secret key.'''
post_data = {
'client_id': self.client_id,
'secret': self.secret,
}
post_data.update(data)
return self._post(path, post_data, is_json) | def post(self, path, data, is_json=True) | Make a post request with client_id and secret key. | 3.328311 | 2.469205 | 1.347928 |
'''Make a post request requiring no auth.'''
return self._post(path, data, is_json) | def post_public(self, path, data, is_json=True) | Make a post request requiring no auth. | 10.886977 | 5.342875 | 2.037663 |
'''Make a post request using a public key.'''
post_data = {
'public_key': self.public_key
}
post_data.update(data)
return self._post(path, post_data, is_json) | def post_public_key(self, path, data, is_json=True) | Make a post request using a public key. | 3.385743 | 3.079648 | 1.099393 |
'''
Fetch all Plaid institutions, using /institutions/all.
:param int count: Number of institutions to fetch.
:param int offset: Number of institutions to skip.
'''
options = _options or {}
return self.client.post('/institutions/get', {
... | def get(self, count, offset=0, _options=None) | Fetch all Plaid institutions, using /institutions/all.
:param int count: Number of institutions to fetch.
:param int offset: Number of institutions to skip. | 4.536448 | 2.090951 | 2.169562 |
'''
Fetch a single institution by id.
:param str institution_id:
'''
options = _options or {}
return self.client.post_public_key('/institutions/get_by_id', {
'institution_id': institution_id,
'options': options,
}) | def get_by_id(self, institution_id, _options=None) | Fetch a single institution by id.
:param str institution_id: | 4.881257 | 3.641219 | 1.340556 |
'''
Search all institutions by name.
:param str query: Query against the full list of
institutions.
:param [str] products: Filter FIs by available products. Optional.
'''
options = _options or {}
return self.cli... | def search(self, query, _options={}, products=None) | Search all institutions by name.
:param str query: Query against the full list of
institutions.
:param [str] products: Filter FIs by available products. Optional. | 7.58118 | 2.458407 | 3.083777 |
'''
Retrieve account and routing numbers for checking and savings accounts.
(`HTTP docs <https://plaid.com/docs/api/#auth>`__)
:param str access_token:
:param [str] account_ids: A list of account_ids to retrieve for
the item. Op... | def get(self,
access_token,
_options=None,
account_ids=None) | Retrieve account and routing numbers for checking and savings accounts.
(`HTTP docs <https://plaid.com/docs/api/#auth>`__)
:param str access_token:
:param [str] account_ids: A list of account_ids to retrieve for
the item. Optional. | 4.814608 | 1.817686 | 2.648756 |
if isinstance(file_path_or_buffer, six.string_types):
with open(file_path_or_buffer, 'rb') as f:
return __file_to_dataframe(f, schema, **kwargs)
else:
return __file_to_dataframe(file_path_or_buffer, schema, **kwargs) | def read_avro(file_path_or_buffer, schema=None, **kwargs) | Avro file reader.
Args:
file_path_or_buffer: Input file path or file-like object.
schema: Avro schema.
**kwargs: Keyword argument to pandas.DataFrame.from_records.
Returns:
Class of pd.DataFrame. | 1.815956 | 2.035071 | 0.892331 |
if schema is None:
schema = __schema_infer(df)
open_mode = 'wb' if not append else 'a+b'
if isinstance(file_path_or_buffer, six.string_types):
with open(file_path_or_buffer, open_mode) as f:
fastavro.writer(f, schema=schema,
records=df.to_dict('... | def to_avro(file_path_or_buffer, df, schema=None, codec='null', append=False) | Avro file writer.
Args:
file_path_or_buffer:
Output file path or file-like object.
df: pd.DataFrame.
schema: Dict of Avro schema.
If it's set None, inferring schema.
append: Boolean to control if will append to existing file
codec: A string indicating... | 2.098104 | 2.157275 | 0.972572 |
self.n += w
if len(self) == 0:
self._add_centroid(Centroid(x, w))
return
S = self._find_closest_centroids(x)
while len(S) != 0 and w > 0:
j = choice(list(range(len(S))))
c_j = S[j]
q = self._compute_centroid_quantil... | def update(self, x, w=1) | Update the t-digest with value x and weight w. | 4.352075 | 4.085862 | 1.065155 |
for x in values:
self.update(x, w)
self.compress()
return | def batch_update(self, values, w=1) | Update the t-digest with an iterable of values. This assumes all points have the
same weight. | 6.70834 | 5.134424 | 1.306542 |
if not (0 <= p <= 100):
raise ValueError("p must be between 0 and 100, inclusive.")
p = float(p)/100.
p *= self.n
c_i = None
t = 0
if p == 0:
return self.C.min_item()[1].mean
for i, key in enumerate(self.C.keys()):
... | def percentile(self, p) | Computes the percentile of a specific value in [0,100]. | 2.855312 | 2.830459 | 1.00878 |
t = 0
N = float(self.n)
if len(self) == 1: # only one centroid
return int(x >= self.C.min_key())
for i, key in enumerate(self.C.keys()):
c_i = self.C[key]
if i == len(self) - 1:
delta = (c_i.mean - self.C.prev_item(key)[1].m... | def cdf(self, x) | Computes the cdf of a specific value, ie. computes F(x) where F denotes
the CDF of the distribution. | 4.005075 | 4.074811 | 0.982886 |
if not (p1 < p2):
raise ValueError("p1 must be between 0 and 100 and less than p2.")
min_count = p1 / 100. * self.n
max_count = p2 / 100. * self.n
trimmed_sum = trimmed_count = curr_count = 0
for i, c in enumerate(self.C.values()):
next_count = ... | def trimmed_mean(self, p1, p2) | Computes the mean of the distribution between the two percentiles p1 and p2.
This is a modified algorithm than the one presented in the original t-Digest paper. | 2.634289 | 2.495607 | 1.05557 |
centroids = []
for key in self.C.keys():
tree_values = self.C.get_value(key)
centroids.append({'m':tree_values.mean, 'c':tree_values.count})
return centroids | def centroids_to_list(self) | Returns a Python list of the TDigest object's Centroid values. | 5.07012 | 4.215997 | 1.202591 |
return {'n':self.n, 'delta':self.delta, 'K':self.K, 'centroids':self.centroids_to_list()} | def to_dict(self) | Returns a Python dictionary of the TDigest and internal Centroid values.
Or use centroids_to_list() for a list of only the Centroid values. | 6.464594 | 3.163805 | 2.043297 |
self.delta = dict_values.get('delta', self.delta)
self.K = dict_values.get('K', self.K)
self.update_centroids_from_list(dict_values['centroids'])
return self | def update_from_dict(self, dict_values) | Updates TDigest object with dictionary values.
The digest delta and K values are optional if you would like to update them,
but the n value is not required because it is computed from the centroid weights.
For example, you can initalize a new TDigest:
digest = TDigest()
The... | 3.33191 | 2.487129 | 1.339661 |
[self.update(value['m'], value['c']) for value in list_values]
return self | def update_centroids_from_list(self, list_values) | Add or update Centroids from a Python list.
Any existing centroids in the digest object are appropriately updated.
Example:
digest.update_centroids([{'c': 1.0, 'm': 1.0}, {'c': 1.0, 'm': 2.0}, {'c': 1.0, 'm': 3.0}]) | 8.512331 | 7.800449 | 1.091262 |
if isspmatrix(matrix):
return sparse_allclose(matrix, matrix.transpose())
return np.allclose(matrix, matrix.T) | def is_undirected(matrix) | Determine if the matrix reprensents a directed graph
:param matrix: The matrix to tested
:returns: boolean | 4.996195 | 9.54945 | 0.523192 |
for i in range(matrix.shape[0]):
if isspmatrix(matrix):
col = find(matrix[:,i])[2]
else:
col = matrix[:,i].T.tolist()[0]
coeff = max( Fraction(c).limit_denominator().denominator for c in col )
matrix[:,i] *= coeff
return matrix | def convert_to_adjacency_matrix(matrix) | Converts transition matrix into adjacency matrix
:param matrix: The matrix to be converted
:returns: adjacency matrix | 6.583613 | 8.01639 | 0.821269 |
if isspmatrix(matrix):
delta = dok_matrix(matrix.shape)
else:
delta = np.zeros(matrix.shape)
for i in clusters :
for j in permutations(i, 2):
delta[j] = 1
return delta | def delta_matrix(matrix, clusters) | Compute delta matrix where delta[i,j]=1 if i and j belong
to same cluster and i!=j
:param matrix: The adjacency matrix
:param clusters: The clusters returned by get_clusters
:returns: delta matrix | 3.713074 | 3.797228 | 0.977838 |
matrix = convert_to_adjacency_matrix(matrix)
m = matrix.sum()
if isspmatrix(matrix):
matrix_2 = matrix.tocsr(copy=True)
else :
matrix_2 = matrix
if is_undirected(matrix):
expected = lambda i,j : (( matrix_2[i,:].sum() + matrix[:,i].sum() )*
... | def modularity(matrix, clusters) | Compute the modularity
:param matrix: The adjacency matrix
:param clusters: The clusters returned by get_clusters
:returns: modularity value | 3.903414 | 4.197409 | 0.929958 |
# make a networkx graph from the adjacency matrix
graph = nx.Graph(matrix)
# map node to cluster id for colors
cluster_map = {node: i for i, cluster in enumerate(clusters) for node in cluster}
colors = [cluster_map[i] for i in range(len(graph.nodes()))]
# if colormap not specified... | def draw_graph(matrix, clusters, **kwargs) | Visualize the clustering
:param matrix: The unprocessed adjacency matrix
:param clusters: list of tuples containing clusters as returned
by 'get_clusters'
:param kwargs: Additional keyword arguments to be passed to
networkx.draw_networkx | 3.520653 | 3.531262 | 0.996996 |
c = np.abs(a - b) - rtol * np.abs(b)
# noinspection PyUnresolvedReferences
return c.max() <= atol | def sparse_allclose(a, b, rtol=1e-5, atol=1e-8) | Version of np.allclose for use with sparse matrices | 3.903954 | 3.853239 | 1.013162 |
if isspmatrix(matrix):
return normalize(matrix.power(power))
return normalize(np.power(matrix, power)) | def inflate(matrix, power) | Apply cluster inflation to the given matrix by raising
each element to the given power.
:param matrix: The matrix to be inflated
:param power: Cluster inflation parameter
:returns: The inflated matrix | 6.491347 | 8.233554 | 0.788402 |
if isspmatrix(matrix):
return matrix ** power
return np.linalg.matrix_power(matrix, power) | def expand(matrix, power) | Apply cluster expansion to the given matrix by raising
the matrix to the given power.
:param matrix: The matrix to be expanded
:param power: Cluster expansion parameter
:returns: The expanded matrix | 4.865572 | 6.669152 | 0.729564 |
shape = matrix.shape
assert shape[0] == shape[1], "Error, matrix is not square"
if isspmatrix(matrix):
new_matrix = matrix.todok()
else:
new_matrix = matrix.copy()
for i in range(shape[0]):
new_matrix[i, i] = loop_value
if isspmatrix(matrix):
return new_ma... | def add_self_loops(matrix, loop_value) | Add self-loops to the matrix by setting the diagonal
to loop_value
:param matrix: The matrix to add loops to
:param loop_value: Value to use for self-loops
:returns: The matrix with self-loops | 2.413348 | 2.839844 | 0.849817 |
if isspmatrix(matrix):
pruned = dok_matrix(matrix.shape)
pruned[matrix >= threshold] = matrix[matrix >= threshold]
pruned = pruned.tocsc()
else:
pruned = matrix.copy()
pruned[pruned < threshold] = 0
# keep max value in each column. same behaviour for dense/spars... | def prune(matrix, threshold) | Prune the matrix so that very small edges are removed.
The maximum value in each column is never pruned.
:param matrix: The matrix to be pruned
:param threshold: The value below which edges will be removed
:returns: The pruned matrix | 2.890798 | 2.866343 | 1.008532 |
if isspmatrix(matrix1) or isspmatrix(matrix2):
return sparse_allclose(matrix1, matrix2)
return np.allclose(matrix1, matrix2) | def converged(matrix1, matrix2) | Check for convergence by determining if
matrix1 and matrix2 are approximately equal.
:param matrix1: The matrix to compare with matrix2
:param matrix2: The matrix to compare with matrix1
:returns: True if matrix1 and matrix2 approximately equal | 2.978349 | 4.538366 | 0.65626 |
# Expansion
matrix = expand(matrix, expansion)
# Inflation
matrix = inflate(matrix, inflation)
return matrix | def iterate(matrix, expansion, inflation) | Run a single iteration (expansion + inflation) of the mcl algorithm
:param matrix: The matrix to perform the iteration on
:param expansion: Cluster expansion factor
:param inflation: Cluster inflation factor | 3.200221 | 4.009157 | 0.798228 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.