repository_name stringlengths 7 55 | func_path_in_repository stringlengths 4 223 | func_name stringlengths 1 134 | whole_func_string stringlengths 75 104k | language stringclasses 1 value | func_code_string stringlengths 75 104k | func_code_tokens listlengths 19 28.4k | func_documentation_string stringlengths 1 46.9k | func_documentation_tokens listlengths 1 1.97k | split_name stringclasses 1 value | func_code_url stringlengths 87 315 |
|---|---|---|---|---|---|---|---|---|---|---|
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.permissions | def permissions(cls, instance, db_session=None):
"""
returns all non-resource permissions based on what groups user
belongs and directly set ones for this user
:param instance:
:param db_session:
:return:
"""
db_session = get_db_session(db_session, instance)
query = db_session.query(
cls.models_proxy.GroupPermission.group_id.label("owner_id"),
cls.models_proxy.GroupPermission.perm_name.label("perm_name"),
sa.literal("group").label("type"),
)
query = query.filter(
cls.models_proxy.GroupPermission.group_id
== cls.models_proxy.UserGroup.group_id
)
query = query.filter(
cls.models_proxy.User.id == cls.models_proxy.UserGroup.user_id
)
query = query.filter(cls.models_proxy.User.id == instance.id)
query2 = db_session.query(
cls.models_proxy.UserPermission.user_id.label("owner_id"),
cls.models_proxy.UserPermission.perm_name.label("perm_name"),
sa.literal("user").label("type"),
)
query2 = query2.filter(cls.models_proxy.UserPermission.user_id == instance.id)
query = query.union(query2)
groups_dict = dict([(g.id, g) for g in instance.groups])
return [
PermissionTuple(
instance,
row.perm_name,
row.type,
groups_dict.get(row.owner_id) if row.type == "group" else None,
None,
False,
True,
)
for row in query
] | python | def permissions(cls, instance, db_session=None):
"""
returns all non-resource permissions based on what groups user
belongs and directly set ones for this user
:param instance:
:param db_session:
:return:
"""
db_session = get_db_session(db_session, instance)
query = db_session.query(
cls.models_proxy.GroupPermission.group_id.label("owner_id"),
cls.models_proxy.GroupPermission.perm_name.label("perm_name"),
sa.literal("group").label("type"),
)
query = query.filter(
cls.models_proxy.GroupPermission.group_id
== cls.models_proxy.UserGroup.group_id
)
query = query.filter(
cls.models_proxy.User.id == cls.models_proxy.UserGroup.user_id
)
query = query.filter(cls.models_proxy.User.id == instance.id)
query2 = db_session.query(
cls.models_proxy.UserPermission.user_id.label("owner_id"),
cls.models_proxy.UserPermission.perm_name.label("perm_name"),
sa.literal("user").label("type"),
)
query2 = query2.filter(cls.models_proxy.UserPermission.user_id == instance.id)
query = query.union(query2)
groups_dict = dict([(g.id, g) for g in instance.groups])
return [
PermissionTuple(
instance,
row.perm_name,
row.type,
groups_dict.get(row.owner_id) if row.type == "group" else None,
None,
False,
True,
)
for row in query
] | [
"def",
"permissions",
"(",
"cls",
",",
"instance",
",",
"db_session",
"=",
"None",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
",",
"instance",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"models_proxy",
".",
"GroupPermission",
".",
"group_id",
".",
"label",
"(",
"\"owner_id\"",
")",
",",
"cls",
".",
"models_proxy",
".",
"GroupPermission",
".",
"perm_name",
".",
"label",
"(",
"\"perm_name\"",
")",
",",
"sa",
".",
"literal",
"(",
"\"group\"",
")",
".",
"label",
"(",
"\"type\"",
")",
",",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"GroupPermission",
".",
"group_id",
"==",
"cls",
".",
"models_proxy",
".",
"UserGroup",
".",
"group_id",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"User",
".",
"id",
"==",
"cls",
".",
"models_proxy",
".",
"UserGroup",
".",
"user_id",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"User",
".",
"id",
"==",
"instance",
".",
"id",
")",
"query2",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"models_proxy",
".",
"UserPermission",
".",
"user_id",
".",
"label",
"(",
"\"owner_id\"",
")",
",",
"cls",
".",
"models_proxy",
".",
"UserPermission",
".",
"perm_name",
".",
"label",
"(",
"\"perm_name\"",
")",
",",
"sa",
".",
"literal",
"(",
"\"user\"",
")",
".",
"label",
"(",
"\"type\"",
")",
",",
")",
"query2",
"=",
"query2",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"UserPermission",
".",
"user_id",
"==",
"instance",
".",
"id",
")",
"query",
"=",
"query",
".",
"union",
"(",
"query2",
")",
"groups_dict",
"=",
"dict",
"(",
"[",
"(",
"g",
".",
"id",
",",
"g",
")",
"for",
"g",
"in",
"instance",
".",
"groups",
"]",
")",
"return",
"[",
"PermissionTuple",
"(",
"instance",
",",
"row",
".",
"perm_name",
",",
"row",
".",
"type",
",",
"groups_dict",
".",
"get",
"(",
"row",
".",
"owner_id",
")",
"if",
"row",
".",
"type",
"==",
"\"group\"",
"else",
"None",
",",
"None",
",",
"False",
",",
"True",
",",
")",
"for",
"row",
"in",
"query",
"]"
] | returns all non-resource permissions based on what groups user
belongs and directly set ones for this user
:param instance:
:param db_session:
:return: | [
"returns",
"all",
"non",
"-",
"resource",
"permissions",
"based",
"on",
"what",
"groups",
"user",
"belongs",
"and",
"directly",
"set",
"ones",
"for",
"this",
"user"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L37-L80 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.resources_with_perms | def resources_with_perms(
cls, instance, perms, resource_ids=None, resource_types=None, db_session=None
):
"""
returns all resources that user has perms for
(note that at least one perm needs to be met)
:param instance:
:param perms:
:param resource_ids: restricts the search to specific resources
:param resource_types:
:param db_session:
:return:
"""
# owned entities have ALL permissions so we return those resources too
# even without explict perms set
# TODO: implement admin superrule perm - maybe return all apps
db_session = get_db_session(db_session, instance)
query = db_session.query(cls.models_proxy.Resource).distinct()
group_ids = [gr.id for gr in instance.groups]
# if user has some groups lets try to join based on their permissions
if group_ids:
join_conditions = (
cls.models_proxy.GroupResourcePermission.group_id.in_(group_ids),
cls.models_proxy.Resource.resource_id
== cls.models_proxy.GroupResourcePermission.resource_id,
cls.models_proxy.GroupResourcePermission.perm_name.in_(perms),
)
query = query.outerjoin(
(cls.models_proxy.GroupResourcePermission, sa.and_(*join_conditions))
)
# ensure outerjoin permissions are correct -
# dont add empty rows from join
# conditions are - join ON possible group permissions
# OR owning group/user
query = query.filter(
sa.or_(
cls.models_proxy.Resource.owner_user_id == instance.id,
cls.models_proxy.Resource.owner_group_id.in_(group_ids),
cls.models_proxy.GroupResourcePermission.perm_name != None,
) # noqa
)
else:
# filter just by username
query = query.filter(cls.models_proxy.Resource.owner_user_id == instance.id)
# lets try by custom user permissions for resource
query2 = db_session.query(cls.models_proxy.Resource).distinct()
query2 = query2.filter(
cls.models_proxy.UserResourcePermission.user_id == instance.id
)
query2 = query2.filter(
cls.models_proxy.Resource.resource_id
== cls.models_proxy.UserResourcePermission.resource_id
)
query2 = query2.filter(
cls.models_proxy.UserResourcePermission.perm_name.in_(perms)
)
if resource_ids:
query = query.filter(
cls.models_proxy.Resource.resource_id.in_(resource_ids)
)
query2 = query2.filter(
cls.models_proxy.Resource.resource_id.in_(resource_ids)
)
if resource_types:
query = query.filter(
cls.models_proxy.Resource.resource_type.in_(resource_types)
)
query2 = query2.filter(
cls.models_proxy.Resource.resource_type.in_(resource_types)
)
query = query.union(query2)
query = query.order_by(cls.models_proxy.Resource.resource_name)
return query | python | def resources_with_perms(
cls, instance, perms, resource_ids=None, resource_types=None, db_session=None
):
"""
returns all resources that user has perms for
(note that at least one perm needs to be met)
:param instance:
:param perms:
:param resource_ids: restricts the search to specific resources
:param resource_types:
:param db_session:
:return:
"""
# owned entities have ALL permissions so we return those resources too
# even without explict perms set
# TODO: implement admin superrule perm - maybe return all apps
db_session = get_db_session(db_session, instance)
query = db_session.query(cls.models_proxy.Resource).distinct()
group_ids = [gr.id for gr in instance.groups]
# if user has some groups lets try to join based on their permissions
if group_ids:
join_conditions = (
cls.models_proxy.GroupResourcePermission.group_id.in_(group_ids),
cls.models_proxy.Resource.resource_id
== cls.models_proxy.GroupResourcePermission.resource_id,
cls.models_proxy.GroupResourcePermission.perm_name.in_(perms),
)
query = query.outerjoin(
(cls.models_proxy.GroupResourcePermission, sa.and_(*join_conditions))
)
# ensure outerjoin permissions are correct -
# dont add empty rows from join
# conditions are - join ON possible group permissions
# OR owning group/user
query = query.filter(
sa.or_(
cls.models_proxy.Resource.owner_user_id == instance.id,
cls.models_proxy.Resource.owner_group_id.in_(group_ids),
cls.models_proxy.GroupResourcePermission.perm_name != None,
) # noqa
)
else:
# filter just by username
query = query.filter(cls.models_proxy.Resource.owner_user_id == instance.id)
# lets try by custom user permissions for resource
query2 = db_session.query(cls.models_proxy.Resource).distinct()
query2 = query2.filter(
cls.models_proxy.UserResourcePermission.user_id == instance.id
)
query2 = query2.filter(
cls.models_proxy.Resource.resource_id
== cls.models_proxy.UserResourcePermission.resource_id
)
query2 = query2.filter(
cls.models_proxy.UserResourcePermission.perm_name.in_(perms)
)
if resource_ids:
query = query.filter(
cls.models_proxy.Resource.resource_id.in_(resource_ids)
)
query2 = query2.filter(
cls.models_proxy.Resource.resource_id.in_(resource_ids)
)
if resource_types:
query = query.filter(
cls.models_proxy.Resource.resource_type.in_(resource_types)
)
query2 = query2.filter(
cls.models_proxy.Resource.resource_type.in_(resource_types)
)
query = query.union(query2)
query = query.order_by(cls.models_proxy.Resource.resource_name)
return query | [
"def",
"resources_with_perms",
"(",
"cls",
",",
"instance",
",",
"perms",
",",
"resource_ids",
"=",
"None",
",",
"resource_types",
"=",
"None",
",",
"db_session",
"=",
"None",
")",
":",
"# owned entities have ALL permissions so we return those resources too",
"# even without explict perms set",
"# TODO: implement admin superrule perm - maybe return all apps",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
",",
"instance",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
")",
".",
"distinct",
"(",
")",
"group_ids",
"=",
"[",
"gr",
".",
"id",
"for",
"gr",
"in",
"instance",
".",
"groups",
"]",
"# if user has some groups lets try to join based on their permissions",
"if",
"group_ids",
":",
"join_conditions",
"=",
"(",
"cls",
".",
"models_proxy",
".",
"GroupResourcePermission",
".",
"group_id",
".",
"in_",
"(",
"group_ids",
")",
",",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"resource_id",
"==",
"cls",
".",
"models_proxy",
".",
"GroupResourcePermission",
".",
"resource_id",
",",
"cls",
".",
"models_proxy",
".",
"GroupResourcePermission",
".",
"perm_name",
".",
"in_",
"(",
"perms",
")",
",",
")",
"query",
"=",
"query",
".",
"outerjoin",
"(",
"(",
"cls",
".",
"models_proxy",
".",
"GroupResourcePermission",
",",
"sa",
".",
"and_",
"(",
"*",
"join_conditions",
")",
")",
")",
"# ensure outerjoin permissions are correct -",
"# dont add empty rows from join",
"# conditions are - join ON possible group permissions",
"# OR owning group/user",
"query",
"=",
"query",
".",
"filter",
"(",
"sa",
".",
"or_",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"owner_user_id",
"==",
"instance",
".",
"id",
",",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"owner_group_id",
".",
"in_",
"(",
"group_ids",
")",
",",
"cls",
".",
"models_proxy",
".",
"GroupResourcePermission",
".",
"perm_name",
"!=",
"None",
",",
")",
"# noqa",
")",
"else",
":",
"# filter just by username",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"owner_user_id",
"==",
"instance",
".",
"id",
")",
"# lets try by custom user permissions for resource",
"query2",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
")",
".",
"distinct",
"(",
")",
"query2",
"=",
"query2",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"UserResourcePermission",
".",
"user_id",
"==",
"instance",
".",
"id",
")",
"query2",
"=",
"query2",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"resource_id",
"==",
"cls",
".",
"models_proxy",
".",
"UserResourcePermission",
".",
"resource_id",
")",
"query2",
"=",
"query2",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"UserResourcePermission",
".",
"perm_name",
".",
"in_",
"(",
"perms",
")",
")",
"if",
"resource_ids",
":",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"resource_id",
".",
"in_",
"(",
"resource_ids",
")",
")",
"query2",
"=",
"query2",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"resource_id",
".",
"in_",
"(",
"resource_ids",
")",
")",
"if",
"resource_types",
":",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"resource_type",
".",
"in_",
"(",
"resource_types",
")",
")",
"query2",
"=",
"query2",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"resource_type",
".",
"in_",
"(",
"resource_types",
")",
")",
"query",
"=",
"query",
".",
"union",
"(",
"query2",
")",
"query",
"=",
"query",
".",
"order_by",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"resource_name",
")",
"return",
"query"
] | returns all resources that user has perms for
(note that at least one perm needs to be met)
:param instance:
:param perms:
:param resource_ids: restricts the search to specific resources
:param resource_types:
:param db_session:
:return: | [
"returns",
"all",
"resources",
"that",
"user",
"has",
"perms",
"for",
"(",
"note",
"that",
"at",
"least",
"one",
"perm",
"needs",
"to",
"be",
"met",
")"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L83-L157 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.groups_with_resources | def groups_with_resources(cls, instance):
"""
Returns a list of groups users belongs to with eager loaded
resources owned by those groups
:param instance:
:return:
"""
return instance.groups_dynamic.options(
sa.orm.eagerload(cls.models_proxy.Group.resources)
) | python | def groups_with_resources(cls, instance):
"""
Returns a list of groups users belongs to with eager loaded
resources owned by those groups
:param instance:
:return:
"""
return instance.groups_dynamic.options(
sa.orm.eagerload(cls.models_proxy.Group.resources)
) | [
"def",
"groups_with_resources",
"(",
"cls",
",",
"instance",
")",
":",
"return",
"instance",
".",
"groups_dynamic",
".",
"options",
"(",
"sa",
".",
"orm",
".",
"eagerload",
"(",
"cls",
".",
"models_proxy",
".",
"Group",
".",
"resources",
")",
")"
] | Returns a list of groups users belongs to with eager loaded
resources owned by those groups
:param instance:
:return: | [
"Returns",
"a",
"list",
"of",
"groups",
"users",
"belongs",
"to",
"with",
"eager",
"loaded",
"resources",
"owned",
"by",
"those",
"groups"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L160-L170 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.resources_with_possible_perms | def resources_with_possible_perms(
cls, instance, resource_ids=None, resource_types=None, db_session=None
):
"""
returns list of permissions and resources for this user
:param instance:
:param resource_ids: restricts the search to specific resources
:param resource_types: restricts the search to specific resource types
:param db_session:
:return:
"""
perms = resource_permissions_for_users(
cls.models_proxy,
ANY_PERMISSION,
resource_ids=resource_ids,
resource_types=resource_types,
user_ids=[instance.id],
db_session=db_session,
)
for resource in instance.resources:
perms.append(
PermissionTuple(
instance, ALL_PERMISSIONS, "user", None, resource, True, True
)
)
for group in cls.groups_with_resources(instance):
for resource in group.resources:
perms.append(
PermissionTuple(
instance, ALL_PERMISSIONS, "group", group, resource, True, True
)
)
return perms | python | def resources_with_possible_perms(
cls, instance, resource_ids=None, resource_types=None, db_session=None
):
"""
returns list of permissions and resources for this user
:param instance:
:param resource_ids: restricts the search to specific resources
:param resource_types: restricts the search to specific resource types
:param db_session:
:return:
"""
perms = resource_permissions_for_users(
cls.models_proxy,
ANY_PERMISSION,
resource_ids=resource_ids,
resource_types=resource_types,
user_ids=[instance.id],
db_session=db_session,
)
for resource in instance.resources:
perms.append(
PermissionTuple(
instance, ALL_PERMISSIONS, "user", None, resource, True, True
)
)
for group in cls.groups_with_resources(instance):
for resource in group.resources:
perms.append(
PermissionTuple(
instance, ALL_PERMISSIONS, "group", group, resource, True, True
)
)
return perms | [
"def",
"resources_with_possible_perms",
"(",
"cls",
",",
"instance",
",",
"resource_ids",
"=",
"None",
",",
"resource_types",
"=",
"None",
",",
"db_session",
"=",
"None",
")",
":",
"perms",
"=",
"resource_permissions_for_users",
"(",
"cls",
".",
"models_proxy",
",",
"ANY_PERMISSION",
",",
"resource_ids",
"=",
"resource_ids",
",",
"resource_types",
"=",
"resource_types",
",",
"user_ids",
"=",
"[",
"instance",
".",
"id",
"]",
",",
"db_session",
"=",
"db_session",
",",
")",
"for",
"resource",
"in",
"instance",
".",
"resources",
":",
"perms",
".",
"append",
"(",
"PermissionTuple",
"(",
"instance",
",",
"ALL_PERMISSIONS",
",",
"\"user\"",
",",
"None",
",",
"resource",
",",
"True",
",",
"True",
")",
")",
"for",
"group",
"in",
"cls",
".",
"groups_with_resources",
"(",
"instance",
")",
":",
"for",
"resource",
"in",
"group",
".",
"resources",
":",
"perms",
".",
"append",
"(",
"PermissionTuple",
"(",
"instance",
",",
"ALL_PERMISSIONS",
",",
"\"group\"",
",",
"group",
",",
"resource",
",",
"True",
",",
"True",
")",
")",
"return",
"perms"
] | returns list of permissions and resources for this user
:param instance:
:param resource_ids: restricts the search to specific resources
:param resource_types: restricts the search to specific resource types
:param db_session:
:return: | [
"returns",
"list",
"of",
"permissions",
"and",
"resources",
"for",
"this",
"user"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L173-L207 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.gravatar_url | def gravatar_url(cls, instance, default="mm", **kwargs):
"""
returns user gravatar url
:param instance:
:param default:
:param kwargs:
:return:
"""
# construct the url
hash = hashlib.md5(instance.email.encode("utf8").lower()).hexdigest()
if "d" not in kwargs:
kwargs["d"] = default
params = "&".join(
[
six.moves.urllib.parse.urlencode({key: value})
for key, value in kwargs.items()
]
)
return "https://secure.gravatar.com/avatar/{}?{}".format(hash, params) | python | def gravatar_url(cls, instance, default="mm", **kwargs):
"""
returns user gravatar url
:param instance:
:param default:
:param kwargs:
:return:
"""
# construct the url
hash = hashlib.md5(instance.email.encode("utf8").lower()).hexdigest()
if "d" not in kwargs:
kwargs["d"] = default
params = "&".join(
[
six.moves.urllib.parse.urlencode({key: value})
for key, value in kwargs.items()
]
)
return "https://secure.gravatar.com/avatar/{}?{}".format(hash, params) | [
"def",
"gravatar_url",
"(",
"cls",
",",
"instance",
",",
"default",
"=",
"\"mm\"",
",",
"*",
"*",
"kwargs",
")",
":",
"# construct the url",
"hash",
"=",
"hashlib",
".",
"md5",
"(",
"instance",
".",
"email",
".",
"encode",
"(",
"\"utf8\"",
")",
".",
"lower",
"(",
")",
")",
".",
"hexdigest",
"(",
")",
"if",
"\"d\"",
"not",
"in",
"kwargs",
":",
"kwargs",
"[",
"\"d\"",
"]",
"=",
"default",
"params",
"=",
"\"&\"",
".",
"join",
"(",
"[",
"six",
".",
"moves",
".",
"urllib",
".",
"parse",
".",
"urlencode",
"(",
"{",
"key",
":",
"value",
"}",
")",
"for",
"key",
",",
"value",
"in",
"kwargs",
".",
"items",
"(",
")",
"]",
")",
"return",
"\"https://secure.gravatar.com/avatar/{}?{}\"",
".",
"format",
"(",
"hash",
",",
"params",
")"
] | returns user gravatar url
:param instance:
:param default:
:param kwargs:
:return: | [
"returns",
"user",
"gravatar",
"url"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L210-L229 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.set_password | def set_password(cls, instance, raw_password):
"""
sets new password on a user using password manager
:param instance:
:param raw_password:
:return:
"""
# support API for both passlib 1.x and 2.x
hash_callable = getattr(
instance.passwordmanager, "hash", instance.passwordmanager.encrypt
)
password = hash_callable(raw_password)
if six.PY2:
instance.user_password = password.decode("utf8")
else:
instance.user_password = password
cls.regenerate_security_code(instance) | python | def set_password(cls, instance, raw_password):
"""
sets new password on a user using password manager
:param instance:
:param raw_password:
:return:
"""
# support API for both passlib 1.x and 2.x
hash_callable = getattr(
instance.passwordmanager, "hash", instance.passwordmanager.encrypt
)
password = hash_callable(raw_password)
if six.PY2:
instance.user_password = password.decode("utf8")
else:
instance.user_password = password
cls.regenerate_security_code(instance) | [
"def",
"set_password",
"(",
"cls",
",",
"instance",
",",
"raw_password",
")",
":",
"# support API for both passlib 1.x and 2.x",
"hash_callable",
"=",
"getattr",
"(",
"instance",
".",
"passwordmanager",
",",
"\"hash\"",
",",
"instance",
".",
"passwordmanager",
".",
"encrypt",
")",
"password",
"=",
"hash_callable",
"(",
"raw_password",
")",
"if",
"six",
".",
"PY2",
":",
"instance",
".",
"user_password",
"=",
"password",
".",
"decode",
"(",
"\"utf8\"",
")",
"else",
":",
"instance",
".",
"user_password",
"=",
"password",
"cls",
".",
"regenerate_security_code",
"(",
"instance",
")"
] | sets new password on a user using password manager
:param instance:
:param raw_password:
:return: | [
"sets",
"new",
"password",
"on",
"a",
"user",
"using",
"password",
"manager"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L232-L249 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.check_password | def check_password(cls, instance, raw_password, enable_hash_migration=True):
"""
checks string with users password hash using password manager
:param instance:
:param raw_password:
:param enable_hash_migration: if legacy hashes should be migrated
:return:
"""
verified, replacement_hash = instance.passwordmanager.verify_and_update(
raw_password, instance.user_password
)
if enable_hash_migration and replacement_hash:
if six.PY2:
instance.user_password = replacement_hash.decode("utf8")
else:
instance.user_password = replacement_hash
return verified | python | def check_password(cls, instance, raw_password, enable_hash_migration=True):
"""
checks string with users password hash using password manager
:param instance:
:param raw_password:
:param enable_hash_migration: if legacy hashes should be migrated
:return:
"""
verified, replacement_hash = instance.passwordmanager.verify_and_update(
raw_password, instance.user_password
)
if enable_hash_migration and replacement_hash:
if six.PY2:
instance.user_password = replacement_hash.decode("utf8")
else:
instance.user_password = replacement_hash
return verified | [
"def",
"check_password",
"(",
"cls",
",",
"instance",
",",
"raw_password",
",",
"enable_hash_migration",
"=",
"True",
")",
":",
"verified",
",",
"replacement_hash",
"=",
"instance",
".",
"passwordmanager",
".",
"verify_and_update",
"(",
"raw_password",
",",
"instance",
".",
"user_password",
")",
"if",
"enable_hash_migration",
"and",
"replacement_hash",
":",
"if",
"six",
".",
"PY2",
":",
"instance",
".",
"user_password",
"=",
"replacement_hash",
".",
"decode",
"(",
"\"utf8\"",
")",
"else",
":",
"instance",
".",
"user_password",
"=",
"replacement_hash",
"return",
"verified"
] | checks string with users password hash using password manager
:param instance:
:param raw_password:
:param enable_hash_migration: if legacy hashes should be migrated
:return: | [
"checks",
"string",
"with",
"users",
"password",
"hash",
"using",
"password",
"manager"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L252-L269 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.by_id | def by_id(cls, user_id, db_session=None):
"""
fetch user by user id
:param user_id:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(cls.model.id == user_id)
query = query.options(sa.orm.eagerload("groups"))
return query.first() | python | def by_id(cls, user_id, db_session=None):
"""
fetch user by user id
:param user_id:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(cls.model.id == user_id)
query = query.options(sa.orm.eagerload("groups"))
return query.first() | [
"def",
"by_id",
"(",
"cls",
",",
"user_id",
",",
"db_session",
"=",
"None",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"model",
".",
"id",
"==",
"user_id",
")",
"query",
"=",
"query",
".",
"options",
"(",
"sa",
".",
"orm",
".",
"eagerload",
"(",
"\"groups\"",
")",
")",
"return",
"query",
".",
"first",
"(",
")"
] | fetch user by user id
:param user_id:
:param db_session:
:return: | [
"fetch",
"user",
"by",
"user",
"id"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L301-L313 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.by_user_name_and_security_code | def by_user_name_and_security_code(cls, user_name, security_code, db_session=None):
"""
fetch user objects by user name and security code
:param user_name:
:param security_code:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(
sa.func.lower(cls.model.user_name) == (user_name or "").lower()
)
query = query.filter(cls.model.security_code == security_code)
return query.first() | python | def by_user_name_and_security_code(cls, user_name, security_code, db_session=None):
"""
fetch user objects by user name and security code
:param user_name:
:param security_code:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(
sa.func.lower(cls.model.user_name) == (user_name or "").lower()
)
query = query.filter(cls.model.security_code == security_code)
return query.first() | [
"def",
"by_user_name_and_security_code",
"(",
"cls",
",",
"user_name",
",",
"security_code",
",",
"db_session",
"=",
"None",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"sa",
".",
"func",
".",
"lower",
"(",
"cls",
".",
"model",
".",
"user_name",
")",
"==",
"(",
"user_name",
"or",
"\"\"",
")",
".",
"lower",
"(",
")",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"model",
".",
"security_code",
"==",
"security_code",
")",
"return",
"query",
".",
"first",
"(",
")"
] | fetch user objects by user name and security code
:param user_name:
:param security_code:
:param db_session:
:return: | [
"fetch",
"user",
"objects",
"by",
"user",
"name",
"and",
"security",
"code"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L333-L348 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.by_user_names | def by_user_names(cls, user_names, db_session=None):
"""
fetch user objects by user names
:param user_names:
:param db_session:
:return:
"""
user_names = [(name or "").lower() for name in user_names]
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(sa.func.lower(cls.model.user_name).in_(user_names))
# q = q.options(sa.orm.eagerload(cls.groups))
return query | python | def by_user_names(cls, user_names, db_session=None):
"""
fetch user objects by user names
:param user_names:
:param db_session:
:return:
"""
user_names = [(name or "").lower() for name in user_names]
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(sa.func.lower(cls.model.user_name).in_(user_names))
# q = q.options(sa.orm.eagerload(cls.groups))
return query | [
"def",
"by_user_names",
"(",
"cls",
",",
"user_names",
",",
"db_session",
"=",
"None",
")",
":",
"user_names",
"=",
"[",
"(",
"name",
"or",
"\"\"",
")",
".",
"lower",
"(",
")",
"for",
"name",
"in",
"user_names",
"]",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"sa",
".",
"func",
".",
"lower",
"(",
"cls",
".",
"model",
".",
"user_name",
")",
".",
"in_",
"(",
"user_names",
")",
")",
"# q = q.options(sa.orm.eagerload(cls.groups))",
"return",
"query"
] | fetch user objects by user names
:param user_names:
:param db_session:
:return: | [
"fetch",
"user",
"objects",
"by",
"user",
"names"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L351-L364 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.user_names_like | def user_names_like(cls, user_name, db_session=None):
"""
fetch users with similar names using LIKE clause
:param user_name:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(
sa.func.lower(cls.model.user_name).like((user_name or "").lower())
)
query = query.order_by(cls.model.user_name)
# q = q.options(sa.orm.eagerload('groups'))
return query | python | def user_names_like(cls, user_name, db_session=None):
"""
fetch users with similar names using LIKE clause
:param user_name:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(
sa.func.lower(cls.model.user_name).like((user_name or "").lower())
)
query = query.order_by(cls.model.user_name)
# q = q.options(sa.orm.eagerload('groups'))
return query | [
"def",
"user_names_like",
"(",
"cls",
",",
"user_name",
",",
"db_session",
"=",
"None",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"sa",
".",
"func",
".",
"lower",
"(",
"cls",
".",
"model",
".",
"user_name",
")",
".",
"like",
"(",
"(",
"user_name",
"or",
"\"\"",
")",
".",
"lower",
"(",
")",
")",
")",
"query",
"=",
"query",
".",
"order_by",
"(",
"cls",
".",
"model",
".",
"user_name",
")",
"# q = q.options(sa.orm.eagerload('groups'))",
"return",
"query"
] | fetch users with similar names using LIKE clause
:param user_name:
:param db_session:
:return: | [
"fetch",
"users",
"with",
"similar",
"names",
"using",
"LIKE",
"clause"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L367-L382 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.by_email | def by_email(cls, email, db_session=None):
"""
fetch user object by email
:param email:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model).filter(
sa.func.lower(cls.model.email) == (email or "").lower()
)
query = query.options(sa.orm.eagerload("groups"))
return query.first() | python | def by_email(cls, email, db_session=None):
"""
fetch user object by email
:param email:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model).filter(
sa.func.lower(cls.model.email) == (email or "").lower()
)
query = query.options(sa.orm.eagerload("groups"))
return query.first() | [
"def",
"by_email",
"(",
"cls",
",",
"email",
",",
"db_session",
"=",
"None",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
".",
"filter",
"(",
"sa",
".",
"func",
".",
"lower",
"(",
"cls",
".",
"model",
".",
"email",
")",
"==",
"(",
"email",
"or",
"\"\"",
")",
".",
"lower",
"(",
")",
")",
"query",
"=",
"query",
".",
"options",
"(",
"sa",
".",
"orm",
".",
"eagerload",
"(",
"\"groups\"",
")",
")",
"return",
"query",
".",
"first",
"(",
")"
] | fetch user object by email
:param email:
:param db_session:
:return: | [
"fetch",
"user",
"object",
"by",
"email"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L385-L398 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/user.py | UserService.users_for_perms | def users_for_perms(cls, perm_names, db_session=None):
"""
return users hat have one of given permissions
:param perm_names:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(
cls.models_proxy.User.id == cls.models_proxy.UserGroup.user_id
)
query = query.filter(
cls.models_proxy.UserGroup.group_id
== cls.models_proxy.GroupPermission.group_id
)
query = query.filter(cls.models_proxy.GroupPermission.perm_name.in_(perm_names))
query2 = db_session.query(cls.model)
query2 = query2.filter(
cls.models_proxy.User.id == cls.models_proxy.UserPermission.user_id
)
query2 = query2.filter(
cls.models_proxy.UserPermission.perm_name.in_(perm_names)
)
users = query.union(query2).order_by(cls.model.id)
return users | python | def users_for_perms(cls, perm_names, db_session=None):
"""
return users hat have one of given permissions
:param perm_names:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(
cls.models_proxy.User.id == cls.models_proxy.UserGroup.user_id
)
query = query.filter(
cls.models_proxy.UserGroup.group_id
== cls.models_proxy.GroupPermission.group_id
)
query = query.filter(cls.models_proxy.GroupPermission.perm_name.in_(perm_names))
query2 = db_session.query(cls.model)
query2 = query2.filter(
cls.models_proxy.User.id == cls.models_proxy.UserPermission.user_id
)
query2 = query2.filter(
cls.models_proxy.UserPermission.perm_name.in_(perm_names)
)
users = query.union(query2).order_by(cls.model.id)
return users | [
"def",
"users_for_perms",
"(",
"cls",
",",
"perm_names",
",",
"db_session",
"=",
"None",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"User",
".",
"id",
"==",
"cls",
".",
"models_proxy",
".",
"UserGroup",
".",
"user_id",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"UserGroup",
".",
"group_id",
"==",
"cls",
".",
"models_proxy",
".",
"GroupPermission",
".",
"group_id",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"GroupPermission",
".",
"perm_name",
".",
"in_",
"(",
"perm_names",
")",
")",
"query2",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
"query2",
"=",
"query2",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"User",
".",
"id",
"==",
"cls",
".",
"models_proxy",
".",
"UserPermission",
".",
"user_id",
")",
"query2",
"=",
"query2",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"UserPermission",
".",
"perm_name",
".",
"in_",
"(",
"perm_names",
")",
")",
"users",
"=",
"query",
".",
"union",
"(",
"query2",
")",
".",
"order_by",
"(",
"cls",
".",
"model",
".",
"id",
")",
"return",
"users"
] | return users hat have one of given permissions
:param perm_names:
:param db_session:
:return: | [
"return",
"users",
"hat",
"have",
"one",
"of",
"given",
"permissions"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/user.py#L419-L446 |
stephenmcd/gnotty | gnotty/bots/commands.py | CommandMixin.handle_joined | def handle_joined(self, connection, event):
"""
Store join times for current nicknames when we first join.
"""
nicknames = [s.lstrip("@+") for s in event.arguments()[-1].split()]
for nickname in nicknames:
self.joined[nickname] = datetime.now() | python | def handle_joined(self, connection, event):
"""
Store join times for current nicknames when we first join.
"""
nicknames = [s.lstrip("@+") for s in event.arguments()[-1].split()]
for nickname in nicknames:
self.joined[nickname] = datetime.now() | [
"def",
"handle_joined",
"(",
"self",
",",
"connection",
",",
"event",
")",
":",
"nicknames",
"=",
"[",
"s",
".",
"lstrip",
"(",
"\"@+\"",
")",
"for",
"s",
"in",
"event",
".",
"arguments",
"(",
")",
"[",
"-",
"1",
"]",
".",
"split",
"(",
")",
"]",
"for",
"nickname",
"in",
"nicknames",
":",
"self",
".",
"joined",
"[",
"nickname",
"]",
"=",
"datetime",
".",
"now",
"(",
")"
] | Store join times for current nicknames when we first join. | [
"Store",
"join",
"times",
"for",
"current",
"nicknames",
"when",
"we",
"first",
"join",
"."
] | train | https://github.com/stephenmcd/gnotty/blob/bea3762dc9cbc3cb21a5ae7224091cf027273c40/gnotty/bots/commands.py#L20-L26 |
stephenmcd/gnotty | gnotty/bots/commands.py | CommandMixin.handle_join | def handle_join(self, connection, event):
"""
Store join time for a nickname when it joins.
"""
nickname = self.get_nickname(event)
self.joined[nickname] = datetime.now() | python | def handle_join(self, connection, event):
"""
Store join time for a nickname when it joins.
"""
nickname = self.get_nickname(event)
self.joined[nickname] = datetime.now() | [
"def",
"handle_join",
"(",
"self",
",",
"connection",
",",
"event",
")",
":",
"nickname",
"=",
"self",
".",
"get_nickname",
"(",
"event",
")",
"self",
".",
"joined",
"[",
"nickname",
"]",
"=",
"datetime",
".",
"now",
"(",
")"
] | Store join time for a nickname when it joins. | [
"Store",
"join",
"time",
"for",
"a",
"nickname",
"when",
"it",
"joins",
"."
] | train | https://github.com/stephenmcd/gnotty/blob/bea3762dc9cbc3cb21a5ae7224091cf027273c40/gnotty/bots/commands.py#L29-L34 |
stephenmcd/gnotty | gnotty/bots/commands.py | CommandMixin.handle_quit | def handle_quit(self, connection, event):
"""
Store quit time for a nickname when it quits.
"""
nickname = self.get_nickname(event)
self.quit[nickname] = datetime.now()
del self.joined[nickname] | python | def handle_quit(self, connection, event):
"""
Store quit time for a nickname when it quits.
"""
nickname = self.get_nickname(event)
self.quit[nickname] = datetime.now()
del self.joined[nickname] | [
"def",
"handle_quit",
"(",
"self",
",",
"connection",
",",
"event",
")",
":",
"nickname",
"=",
"self",
".",
"get_nickname",
"(",
"event",
")",
"self",
".",
"quit",
"[",
"nickname",
"]",
"=",
"datetime",
".",
"now",
"(",
")",
"del",
"self",
".",
"joined",
"[",
"nickname",
"]"
] | Store quit time for a nickname when it quits. | [
"Store",
"quit",
"time",
"for",
"a",
"nickname",
"when",
"it",
"quits",
"."
] | train | https://github.com/stephenmcd/gnotty/blob/bea3762dc9cbc3cb21a5ae7224091cf027273c40/gnotty/bots/commands.py#L37-L43 |
stephenmcd/gnotty | gnotty/bots/commands.py | CommandMixin.timesince | def timesince(self, when):
"""
Returns human friendly version of the timespan between now
and the given datetime.
"""
units = (
("year", 60 * 60 * 24 * 365),
("week", 60 * 60 * 24 * 7),
("day", 60 * 60 * 24),
("hour", 60 * 60),
("minute", 60),
("second", 1),
)
delta = datetime.now() - when
total_seconds = delta.days * 60 * 60 * 24 + delta.seconds
parts = []
for name, seconds in units:
value = total_seconds / seconds
if value > 0:
total_seconds %= seconds
s = "s" if value != 1 else ""
parts.append("%s %s%s" % (value, name, s))
return " and ".join(", ".join(parts).rsplit(", ", 1)) | python | def timesince(self, when):
"""
Returns human friendly version of the timespan between now
and the given datetime.
"""
units = (
("year", 60 * 60 * 24 * 365),
("week", 60 * 60 * 24 * 7),
("day", 60 * 60 * 24),
("hour", 60 * 60),
("minute", 60),
("second", 1),
)
delta = datetime.now() - when
total_seconds = delta.days * 60 * 60 * 24 + delta.seconds
parts = []
for name, seconds in units:
value = total_seconds / seconds
if value > 0:
total_seconds %= seconds
s = "s" if value != 1 else ""
parts.append("%s %s%s" % (value, name, s))
return " and ".join(", ".join(parts).rsplit(", ", 1)) | [
"def",
"timesince",
"(",
"self",
",",
"when",
")",
":",
"units",
"=",
"(",
"(",
"\"year\"",
",",
"60",
"*",
"60",
"*",
"24",
"*",
"365",
")",
",",
"(",
"\"week\"",
",",
"60",
"*",
"60",
"*",
"24",
"*",
"7",
")",
",",
"(",
"\"day\"",
",",
"60",
"*",
"60",
"*",
"24",
")",
",",
"(",
"\"hour\"",
",",
"60",
"*",
"60",
")",
",",
"(",
"\"minute\"",
",",
"60",
")",
",",
"(",
"\"second\"",
",",
"1",
")",
",",
")",
"delta",
"=",
"datetime",
".",
"now",
"(",
")",
"-",
"when",
"total_seconds",
"=",
"delta",
".",
"days",
"*",
"60",
"*",
"60",
"*",
"24",
"+",
"delta",
".",
"seconds",
"parts",
"=",
"[",
"]",
"for",
"name",
",",
"seconds",
"in",
"units",
":",
"value",
"=",
"total_seconds",
"/",
"seconds",
"if",
"value",
">",
"0",
":",
"total_seconds",
"%=",
"seconds",
"s",
"=",
"\"s\"",
"if",
"value",
"!=",
"1",
"else",
"\"\"",
"parts",
".",
"append",
"(",
"\"%s %s%s\"",
"%",
"(",
"value",
",",
"name",
",",
"s",
")",
")",
"return",
"\" and \"",
".",
"join",
"(",
"\", \"",
".",
"join",
"(",
"parts",
")",
".",
"rsplit",
"(",
"\", \"",
",",
"1",
")",
")"
] | Returns human friendly version of the timespan between now
and the given datetime. | [
"Returns",
"human",
"friendly",
"version",
"of",
"the",
"timespan",
"between",
"now",
"and",
"the",
"given",
"datetime",
"."
] | train | https://github.com/stephenmcd/gnotty/blob/bea3762dc9cbc3cb21a5ae7224091cf027273c40/gnotty/bots/commands.py#L45-L67 |
stephenmcd/gnotty | gnotty/bots/commands.py | CommandMixin.version | def version(self, event):
"""
Shows version information.
"""
name = "%s.%s" % (self.__class__.__module__, self.__class__.__name__)
return "%s [%s]" % (settings.GNOTTY_VERSION_STRING, name) | python | def version(self, event):
"""
Shows version information.
"""
name = "%s.%s" % (self.__class__.__module__, self.__class__.__name__)
return "%s [%s]" % (settings.GNOTTY_VERSION_STRING, name) | [
"def",
"version",
"(",
"self",
",",
"event",
")",
":",
"name",
"=",
"\"%s.%s\"",
"%",
"(",
"self",
".",
"__class__",
".",
"__module__",
",",
"self",
".",
"__class__",
".",
"__name__",
")",
"return",
"\"%s [%s]\"",
"%",
"(",
"settings",
".",
"GNOTTY_VERSION_STRING",
",",
"name",
")"
] | Shows version information. | [
"Shows",
"version",
"information",
"."
] | train | https://github.com/stephenmcd/gnotty/blob/bea3762dc9cbc3cb21a5ae7224091cf027273c40/gnotty/bots/commands.py#L78-L83 |
stephenmcd/gnotty | gnotty/bots/commands.py | CommandMixin.commands | def commands(self, event):
"""
Lists all available commands.
"""
commands = sorted(self.commands_dict().keys())
return "Available commands: %s" % " ".join(commands) | python | def commands(self, event):
"""
Lists all available commands.
"""
commands = sorted(self.commands_dict().keys())
return "Available commands: %s" % " ".join(commands) | [
"def",
"commands",
"(",
"self",
",",
"event",
")",
":",
"commands",
"=",
"sorted",
"(",
"self",
".",
"commands_dict",
"(",
")",
".",
"keys",
"(",
")",
")",
"return",
"\"Available commands: %s\"",
"%",
"\" \"",
".",
"join",
"(",
"commands",
")"
] | Lists all available commands. | [
"Lists",
"all",
"available",
"commands",
"."
] | train | https://github.com/stephenmcd/gnotty/blob/bea3762dc9cbc3cb21a5ae7224091cf027273c40/gnotty/bots/commands.py#L86-L91 |
stephenmcd/gnotty | gnotty/bots/commands.py | CommandMixin.help | def help(self, event, command_name=None):
"""
Shows the help message for the bot. Takes an optional command name
which when given, will show help for that command.
"""
if command_name is None:
return ("Type !commands for a list of all commands. Type "
"!help [command] to see help for a specific command.")
try:
command = self.commands_dict()[command_name]
except KeyError:
return "%s is not a command" % command_name
argspec = getargspec(command)
args = argspec.args[2:]
defaults = argspec.defaults or []
for i in range(-1, -len(defaults) - 1, -1):
args[i] = "%s [default: %s]" % (args[i], defaults[i])
args = ", ".join(args)
help = getdoc(command).replace("\n", " ")
return "help for %s: (args: %s) %s" % (command_name, args, help) | python | def help(self, event, command_name=None):
"""
Shows the help message for the bot. Takes an optional command name
which when given, will show help for that command.
"""
if command_name is None:
return ("Type !commands for a list of all commands. Type "
"!help [command] to see help for a specific command.")
try:
command = self.commands_dict()[command_name]
except KeyError:
return "%s is not a command" % command_name
argspec = getargspec(command)
args = argspec.args[2:]
defaults = argspec.defaults or []
for i in range(-1, -len(defaults) - 1, -1):
args[i] = "%s [default: %s]" % (args[i], defaults[i])
args = ", ".join(args)
help = getdoc(command).replace("\n", " ")
return "help for %s: (args: %s) %s" % (command_name, args, help) | [
"def",
"help",
"(",
"self",
",",
"event",
",",
"command_name",
"=",
"None",
")",
":",
"if",
"command_name",
"is",
"None",
":",
"return",
"(",
"\"Type !commands for a list of all commands. Type \"",
"\"!help [command] to see help for a specific command.\"",
")",
"try",
":",
"command",
"=",
"self",
".",
"commands_dict",
"(",
")",
"[",
"command_name",
"]",
"except",
"KeyError",
":",
"return",
"\"%s is not a command\"",
"%",
"command_name",
"argspec",
"=",
"getargspec",
"(",
"command",
")",
"args",
"=",
"argspec",
".",
"args",
"[",
"2",
":",
"]",
"defaults",
"=",
"argspec",
".",
"defaults",
"or",
"[",
"]",
"for",
"i",
"in",
"range",
"(",
"-",
"1",
",",
"-",
"len",
"(",
"defaults",
")",
"-",
"1",
",",
"-",
"1",
")",
":",
"args",
"[",
"i",
"]",
"=",
"\"%s [default: %s]\"",
"%",
"(",
"args",
"[",
"i",
"]",
",",
"defaults",
"[",
"i",
"]",
")",
"args",
"=",
"\", \"",
".",
"join",
"(",
"args",
")",
"help",
"=",
"getdoc",
"(",
"command",
")",
".",
"replace",
"(",
"\"\\n\"",
",",
"\" \"",
")",
"return",
"\"help for %s: (args: %s) %s\"",
"%",
"(",
"command_name",
",",
"args",
",",
"help",
")"
] | Shows the help message for the bot. Takes an optional command name
which when given, will show help for that command. | [
"Shows",
"the",
"help",
"message",
"for",
"the",
"bot",
".",
"Takes",
"an",
"optional",
"command",
"name",
"which",
"when",
"given",
"will",
"show",
"help",
"for",
"that",
"command",
"."
] | train | https://github.com/stephenmcd/gnotty/blob/bea3762dc9cbc3cb21a5ae7224091cf027273c40/gnotty/bots/commands.py#L94-L114 |
stephenmcd/gnotty | gnotty/bots/commands.py | CommandMixin.uptime | def uptime(self, event, nickname=None):
"""
Shows the amount of time since the given nickname has been
in the channel. If no nickname is given, I'll use my own.
"""
if nickname and nickname != self.nickname:
try:
uptime = self.timesince(self.joined[nickname])
except KeyError:
return "%s is not in the channel" % nickname
else:
if nickname == self.get_nickname(event):
prefix = "you have"
else:
prefix = "%s has" % nickname
return "%s been here for %s" % (prefix, uptime)
uptime = self.timesince(self.joined[self.nickname])
return "I've been here for %s" % uptime | python | def uptime(self, event, nickname=None):
"""
Shows the amount of time since the given nickname has been
in the channel. If no nickname is given, I'll use my own.
"""
if nickname and nickname != self.nickname:
try:
uptime = self.timesince(self.joined[nickname])
except KeyError:
return "%s is not in the channel" % nickname
else:
if nickname == self.get_nickname(event):
prefix = "you have"
else:
prefix = "%s has" % nickname
return "%s been here for %s" % (prefix, uptime)
uptime = self.timesince(self.joined[self.nickname])
return "I've been here for %s" % uptime | [
"def",
"uptime",
"(",
"self",
",",
"event",
",",
"nickname",
"=",
"None",
")",
":",
"if",
"nickname",
"and",
"nickname",
"!=",
"self",
".",
"nickname",
":",
"try",
":",
"uptime",
"=",
"self",
".",
"timesince",
"(",
"self",
".",
"joined",
"[",
"nickname",
"]",
")",
"except",
"KeyError",
":",
"return",
"\"%s is not in the channel\"",
"%",
"nickname",
"else",
":",
"if",
"nickname",
"==",
"self",
".",
"get_nickname",
"(",
"event",
")",
":",
"prefix",
"=",
"\"you have\"",
"else",
":",
"prefix",
"=",
"\"%s has\"",
"%",
"nickname",
"return",
"\"%s been here for %s\"",
"%",
"(",
"prefix",
",",
"uptime",
")",
"uptime",
"=",
"self",
".",
"timesince",
"(",
"self",
".",
"joined",
"[",
"self",
".",
"nickname",
"]",
")",
"return",
"\"I've been here for %s\"",
"%",
"uptime"
] | Shows the amount of time since the given nickname has been
in the channel. If no nickname is given, I'll use my own. | [
"Shows",
"the",
"amount",
"of",
"time",
"since",
"the",
"given",
"nickname",
"has",
"been",
"in",
"the",
"channel",
".",
"If",
"no",
"nickname",
"is",
"given",
"I",
"ll",
"use",
"my",
"own",
"."
] | train | https://github.com/stephenmcd/gnotty/blob/bea3762dc9cbc3cb21a5ae7224091cf027273c40/gnotty/bots/commands.py#L117-L134 |
stephenmcd/gnotty | gnotty/bots/commands.py | CommandMixin.seen | def seen(self, event, nickname):
"""
Shows the amount of time since the given nickname was last
seen in the channel.
"""
try:
self.joined[nickname]
except KeyError:
pass
else:
if nickname == self.get_nickname(event):
prefix = "you are"
else:
prefix = "%s is" % nickname
return "%s here right now" % prefix
try:
seen = self.timesince(self.quit[nickname])
except KeyError:
return "%s has never been seen" % nickname
else:
return "%s was last seen %s ago" % (nickname, seen) | python | def seen(self, event, nickname):
"""
Shows the amount of time since the given nickname was last
seen in the channel.
"""
try:
self.joined[nickname]
except KeyError:
pass
else:
if nickname == self.get_nickname(event):
prefix = "you are"
else:
prefix = "%s is" % nickname
return "%s here right now" % prefix
try:
seen = self.timesince(self.quit[nickname])
except KeyError:
return "%s has never been seen" % nickname
else:
return "%s was last seen %s ago" % (nickname, seen) | [
"def",
"seen",
"(",
"self",
",",
"event",
",",
"nickname",
")",
":",
"try",
":",
"self",
".",
"joined",
"[",
"nickname",
"]",
"except",
"KeyError",
":",
"pass",
"else",
":",
"if",
"nickname",
"==",
"self",
".",
"get_nickname",
"(",
"event",
")",
":",
"prefix",
"=",
"\"you are\"",
"else",
":",
"prefix",
"=",
"\"%s is\"",
"%",
"nickname",
"return",
"\"%s here right now\"",
"%",
"prefix",
"try",
":",
"seen",
"=",
"self",
".",
"timesince",
"(",
"self",
".",
"quit",
"[",
"nickname",
"]",
")",
"except",
"KeyError",
":",
"return",
"\"%s has never been seen\"",
"%",
"nickname",
"else",
":",
"return",
"\"%s was last seen %s ago\"",
"%",
"(",
"nickname",
",",
"seen",
")"
] | Shows the amount of time since the given nickname was last
seen in the channel. | [
"Shows",
"the",
"amount",
"of",
"time",
"since",
"the",
"given",
"nickname",
"was",
"last",
"seen",
"in",
"the",
"channel",
"."
] | train | https://github.com/stephenmcd/gnotty/blob/bea3762dc9cbc3cb21a5ae7224091cf027273c40/gnotty/bots/commands.py#L137-L157 |
ergo/ziggurat_foundations | ziggurat_foundations/models/user.py | UserMixin.id | def id(self):
""" Unique identifier of user object"""
return sa.Column(sa.Integer, primary_key=True, autoincrement=True) | python | def id(self):
""" Unique identifier of user object"""
return sa.Column(sa.Integer, primary_key=True, autoincrement=True) | [
"def",
"id",
"(",
"self",
")",
":",
"return",
"sa",
".",
"Column",
"(",
"sa",
".",
"Integer",
",",
"primary_key",
"=",
"True",
",",
"autoincrement",
"=",
"True",
")"
] | Unique identifier of user object | [
"Unique",
"identifier",
"of",
"user",
"object"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/user.py#L29-L31 |
ergo/ziggurat_foundations | ziggurat_foundations/models/user.py | UserMixin.last_login_date | def last_login_date(self):
""" Date of user's last login """
return sa.Column(
sa.TIMESTAMP(timezone=False),
default=lambda x: datetime.utcnow(),
server_default=sa.func.now(),
) | python | def last_login_date(self):
""" Date of user's last login """
return sa.Column(
sa.TIMESTAMP(timezone=False),
default=lambda x: datetime.utcnow(),
server_default=sa.func.now(),
) | [
"def",
"last_login_date",
"(",
"self",
")",
":",
"return",
"sa",
".",
"Column",
"(",
"sa",
".",
"TIMESTAMP",
"(",
"timezone",
"=",
"False",
")",
",",
"default",
"=",
"lambda",
"x",
":",
"datetime",
".",
"utcnow",
"(",
")",
",",
"server_default",
"=",
"sa",
".",
"func",
".",
"now",
"(",
")",
",",
")"
] | Date of user's last login | [
"Date",
"of",
"user",
"s",
"last",
"login"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/user.py#L59-L65 |
ergo/ziggurat_foundations | ziggurat_foundations/models/user.py | UserMixin.security_code_date | def security_code_date(self):
""" Date of user's security code update """
return sa.Column(
sa.TIMESTAMP(timezone=False),
default=datetime(2000, 1, 1),
server_default="2000-01-01 01:01",
) | python | def security_code_date(self):
""" Date of user's security code update """
return sa.Column(
sa.TIMESTAMP(timezone=False),
default=datetime(2000, 1, 1),
server_default="2000-01-01 01:01",
) | [
"def",
"security_code_date",
"(",
"self",
")",
":",
"return",
"sa",
".",
"Column",
"(",
"sa",
".",
"TIMESTAMP",
"(",
"timezone",
"=",
"False",
")",
",",
"default",
"=",
"datetime",
"(",
"2000",
",",
"1",
",",
"1",
")",
",",
"server_default",
"=",
"\"2000-01-01 01:01\"",
",",
")"
] | Date of user's security code update | [
"Date",
"of",
"user",
"s",
"security",
"code",
"update"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/user.py#L77-L83 |
ergo/ziggurat_foundations | ziggurat_foundations/models/user.py | UserMixin.groups_dynamic | def groups_dynamic(self):
""" returns dynamic relationship for groups - allowing for
filtering of data """
return sa.orm.relationship(
"Group",
secondary="users_groups",
lazy="dynamic",
passive_deletes=True,
passive_updates=True,
) | python | def groups_dynamic(self):
""" returns dynamic relationship for groups - allowing for
filtering of data """
return sa.orm.relationship(
"Group",
secondary="users_groups",
lazy="dynamic",
passive_deletes=True,
passive_updates=True,
) | [
"def",
"groups_dynamic",
"(",
"self",
")",
":",
"return",
"sa",
".",
"orm",
".",
"relationship",
"(",
"\"Group\"",
",",
"secondary",
"=",
"\"users_groups\"",
",",
"lazy",
"=",
"\"dynamic\"",
",",
"passive_deletes",
"=",
"True",
",",
"passive_updates",
"=",
"True",
",",
")"
] | returns dynamic relationship for groups - allowing for
filtering of data | [
"returns",
"dynamic",
"relationship",
"for",
"groups",
"-",
"allowing",
"for",
"filtering",
"of",
"data"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/user.py#L89-L98 |
ergo/ziggurat_foundations | ziggurat_foundations/models/user.py | UserMixin.resources | def resources(self):
""" Returns all resources directly owned by user, can be used to assign
ownership of new resources::
user.resources.append(resource) """
return sa.orm.relationship(
"Resource",
cascade="all",
passive_deletes=True,
passive_updates=True,
backref="owner",
lazy="dynamic",
) | python | def resources(self):
""" Returns all resources directly owned by user, can be used to assign
ownership of new resources::
user.resources.append(resource) """
return sa.orm.relationship(
"Resource",
cascade="all",
passive_deletes=True,
passive_updates=True,
backref="owner",
lazy="dynamic",
) | [
"def",
"resources",
"(",
"self",
")",
":",
"return",
"sa",
".",
"orm",
".",
"relationship",
"(",
"\"Resource\"",
",",
"cascade",
"=",
"\"all\"",
",",
"passive_deletes",
"=",
"True",
",",
"passive_updates",
"=",
"True",
",",
"backref",
"=",
"\"owner\"",
",",
"lazy",
"=",
"\"dynamic\"",
",",
")"
] | Returns all resources directly owned by user, can be used to assign
ownership of new resources::
user.resources.append(resource) | [
"Returns",
"all",
"resources",
"directly",
"owned",
"by",
"user",
"can",
"be",
"used",
"to",
"assign",
"ownership",
"of",
"new",
"resources",
"::"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/user.py#L126-L138 |
ergo/ziggurat_foundations | ziggurat_foundations/models/group.py | GroupMixin.resources_dynamic | def resources_dynamic(self):
""" Returns all resources directly owned by group, can be used to assign
ownership of new resources::
user.resources.append(resource) """
return sa.orm.relationship(
"Resource",
cascade="all",
passive_deletes=True,
passive_updates=True,
lazy="dynamic",
) | python | def resources_dynamic(self):
""" Returns all resources directly owned by group, can be used to assign
ownership of new resources::
user.resources.append(resource) """
return sa.orm.relationship(
"Resource",
cascade="all",
passive_deletes=True,
passive_updates=True,
lazy="dynamic",
) | [
"def",
"resources_dynamic",
"(",
"self",
")",
":",
"return",
"sa",
".",
"orm",
".",
"relationship",
"(",
"\"Resource\"",
",",
"cascade",
"=",
"\"all\"",
",",
"passive_deletes",
"=",
"True",
",",
"passive_updates",
"=",
"True",
",",
"lazy",
"=",
"\"dynamic\"",
",",
")"
] | Returns all resources directly owned by group, can be used to assign
ownership of new resources::
user.resources.append(resource) | [
"Returns",
"all",
"resources",
"directly",
"owned",
"by",
"group",
"can",
"be",
"used",
"to",
"assign",
"ownership",
"of",
"new",
"resources",
"::"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/group.py#L99-L110 |
ergo/ziggurat_foundations | ziggurat_foundations/models/group.py | GroupMixin.validate_permission | def validate_permission(self, key, permission):
""" validates if group can get assigned with permission"""
if permission.perm_name not in self.__possible_permissions__:
raise AssertionError(
"perm_name is not one of {}".format(self.__possible_permissions__)
)
return permission | python | def validate_permission(self, key, permission):
""" validates if group can get assigned with permission"""
if permission.perm_name not in self.__possible_permissions__:
raise AssertionError(
"perm_name is not one of {}".format(self.__possible_permissions__)
)
return permission | [
"def",
"validate_permission",
"(",
"self",
",",
"key",
",",
"permission",
")",
":",
"if",
"permission",
".",
"perm_name",
"not",
"in",
"self",
".",
"__possible_permissions__",
":",
"raise",
"AssertionError",
"(",
"\"perm_name is not one of {}\"",
".",
"format",
"(",
"self",
".",
"__possible_permissions__",
")",
")",
"return",
"permission"
] | validates if group can get assigned with permission | [
"validates",
"if",
"group",
"can",
"get",
"assigned",
"with",
"permission"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/group.py#L113-L119 |
stephenmcd/gnotty | gnotty/bots/rss.py | RSSMixin.parse_feeds | def parse_feeds(self, message_channel=True):
"""
Iterates through each of the feed URLs, parses their items, and
sends any items to the channel that have not been previously
been parsed.
"""
if parse:
for feed_url in self.feeds:
feed = parse(feed_url)
for item in feed.entries:
if item["id"] not in self.feed_items:
self.feed_items.add(item["id"])
if message_channel:
message = self.format_item_message(feed, item)
self.message_channel(message)
return | python | def parse_feeds(self, message_channel=True):
"""
Iterates through each of the feed URLs, parses their items, and
sends any items to the channel that have not been previously
been parsed.
"""
if parse:
for feed_url in self.feeds:
feed = parse(feed_url)
for item in feed.entries:
if item["id"] not in self.feed_items:
self.feed_items.add(item["id"])
if message_channel:
message = self.format_item_message(feed, item)
self.message_channel(message)
return | [
"def",
"parse_feeds",
"(",
"self",
",",
"message_channel",
"=",
"True",
")",
":",
"if",
"parse",
":",
"for",
"feed_url",
"in",
"self",
".",
"feeds",
":",
"feed",
"=",
"parse",
"(",
"feed_url",
")",
"for",
"item",
"in",
"feed",
".",
"entries",
":",
"if",
"item",
"[",
"\"id\"",
"]",
"not",
"in",
"self",
".",
"feed_items",
":",
"self",
".",
"feed_items",
".",
"add",
"(",
"item",
"[",
"\"id\"",
"]",
")",
"if",
"message_channel",
":",
"message",
"=",
"self",
".",
"format_item_message",
"(",
"feed",
",",
"item",
")",
"self",
".",
"message_channel",
"(",
"message",
")",
"return"
] | Iterates through each of the feed URLs, parses their items, and
sends any items to the channel that have not been previously
been parsed. | [
"Iterates",
"through",
"each",
"of",
"the",
"feed",
"URLs",
"parses",
"their",
"items",
"and",
"sends",
"any",
"items",
"to",
"the",
"channel",
"that",
"have",
"not",
"been",
"previously",
"been",
"parsed",
"."
] | train | https://github.com/stephenmcd/gnotty/blob/bea3762dc9cbc3cb21a5ae7224091cf027273c40/gnotty/bots/rss.py#L30-L45 |
Xython/Linq.py | linq/standard/dict.py | ChunkBy | def ChunkBy(self: dict, f=None):
"""
[
{
'self': [1, 1, 3, 3, 1, 1],
'f': lambda x: x%2,
'assert': lambda ret: ret == [[1, 1], [3, 3], [1, 1]]
}
]
"""
if f is None:
return _chunk(self.items())
if is_to_destruct(f):
f = destruct_func(f)
return _chunk(self.items(), f) | python | def ChunkBy(self: dict, f=None):
"""
[
{
'self': [1, 1, 3, 3, 1, 1],
'f': lambda x: x%2,
'assert': lambda ret: ret == [[1, 1], [3, 3], [1, 1]]
}
]
"""
if f is None:
return _chunk(self.items())
if is_to_destruct(f):
f = destruct_func(f)
return _chunk(self.items(), f) | [
"def",
"ChunkBy",
"(",
"self",
":",
"dict",
",",
"f",
"=",
"None",
")",
":",
"if",
"f",
"is",
"None",
":",
"return",
"_chunk",
"(",
"self",
".",
"items",
"(",
")",
")",
"if",
"is_to_destruct",
"(",
"f",
")",
":",
"f",
"=",
"destruct_func",
"(",
"f",
")",
"return",
"_chunk",
"(",
"self",
".",
"items",
"(",
")",
",",
"f",
")"
] | [
{
'self': [1, 1, 3, 3, 1, 1],
'f': lambda x: x%2,
'assert': lambda ret: ret == [[1, 1], [3, 3], [1, 1]]
}
] | [
"[",
"{",
"self",
":",
"[",
"1",
"1",
"3",
"3",
"1",
"1",
"]",
"f",
":",
"lambda",
"x",
":",
"x%2",
"assert",
":",
"lambda",
"ret",
":",
"ret",
"==",
"[[",
"1",
"1",
"]",
"[",
"3",
"3",
"]",
"[",
"1",
"1",
"]]",
"}",
"]"
] | train | https://github.com/Xython/Linq.py/blob/ffb65f92f1df0d8161d5f835f5947554f6f33d72/linq/standard/dict.py#L74-L88 |
Xython/Linq.py | linq/standard/dict.py | GroupBy | def GroupBy(self: dict, f=None):
"""
[
{
'self': [1, 2, 3],
'f': lambda x: x%2,
'assert': lambda ret: ret[0] == [2] and ret[1] == [1, 3]
}
]
"""
if f and is_to_destruct(f):
f = destruct_func(f)
return _group_by(self.items(), f) | python | def GroupBy(self: dict, f=None):
"""
[
{
'self': [1, 2, 3],
'f': lambda x: x%2,
'assert': lambda ret: ret[0] == [2] and ret[1] == [1, 3]
}
]
"""
if f and is_to_destruct(f):
f = destruct_func(f)
return _group_by(self.items(), f) | [
"def",
"GroupBy",
"(",
"self",
":",
"dict",
",",
"f",
"=",
"None",
")",
":",
"if",
"f",
"and",
"is_to_destruct",
"(",
"f",
")",
":",
"f",
"=",
"destruct_func",
"(",
"f",
")",
"return",
"_group_by",
"(",
"self",
".",
"items",
"(",
")",
",",
"f",
")"
] | [
{
'self': [1, 2, 3],
'f': lambda x: x%2,
'assert': lambda ret: ret[0] == [2] and ret[1] == [1, 3]
}
] | [
"[",
"{",
"self",
":",
"[",
"1",
"2",
"3",
"]",
"f",
":",
"lambda",
"x",
":",
"x%2",
"assert",
":",
"lambda",
"ret",
":",
"ret",
"[",
"0",
"]",
"==",
"[",
"2",
"]",
"and",
"ret",
"[",
"1",
"]",
"==",
"[",
"1",
"3",
"]",
"}",
"]"
] | train | https://github.com/Xython/Linq.py/blob/ffb65f92f1df0d8161d5f835f5947554f6f33d72/linq/standard/dict.py#L92-L104 |
Xython/Linq.py | linq/standard/dict.py | Take | def Take(self: dict, n):
"""
[
{
'self': [1, 2, 3],
'n': 2,
'assert': lambda ret: list(ret) == [1, 2]
}
]
"""
for i, e in enumerate(self.items()):
if i == n:
break
yield e | python | def Take(self: dict, n):
"""
[
{
'self': [1, 2, 3],
'n': 2,
'assert': lambda ret: list(ret) == [1, 2]
}
]
"""
for i, e in enumerate(self.items()):
if i == n:
break
yield e | [
"def",
"Take",
"(",
"self",
":",
"dict",
",",
"n",
")",
":",
"for",
"i",
",",
"e",
"in",
"enumerate",
"(",
"self",
".",
"items",
"(",
")",
")",
":",
"if",
"i",
"==",
"n",
":",
"break",
"yield",
"e"
] | [
{
'self': [1, 2, 3],
'n': 2,
'assert': lambda ret: list(ret) == [1, 2]
}
] | [
"[",
"{",
"self",
":",
"[",
"1",
"2",
"3",
"]",
"n",
":",
"2",
"assert",
":",
"lambda",
"ret",
":",
"list",
"(",
"ret",
")",
"==",
"[",
"1",
"2",
"]",
"}",
"]"
] | train | https://github.com/Xython/Linq.py/blob/ffb65f92f1df0d8161d5f835f5947554f6f33d72/linq/standard/dict.py#L108-L122 |
Xython/Linq.py | linq/standard/dict.py | TakeIf | def TakeIf(self: dict, f):
"""
[
{
'self': [1, 2, 3],
'f': lambda e: e%2,
'assert': lambda ret: list(ret) == [1, 3]
}
]
"""
if is_to_destruct(f):
f = destruct_func(f)
return (e for e in self.items() if f(e)) | python | def TakeIf(self: dict, f):
"""
[
{
'self': [1, 2, 3],
'f': lambda e: e%2,
'assert': lambda ret: list(ret) == [1, 3]
}
]
"""
if is_to_destruct(f):
f = destruct_func(f)
return (e for e in self.items() if f(e)) | [
"def",
"TakeIf",
"(",
"self",
":",
"dict",
",",
"f",
")",
":",
"if",
"is_to_destruct",
"(",
"f",
")",
":",
"f",
"=",
"destruct_func",
"(",
"f",
")",
"return",
"(",
"e",
"for",
"e",
"in",
"self",
".",
"items",
"(",
")",
"if",
"f",
"(",
"e",
")",
")"
] | [
{
'self': [1, 2, 3],
'f': lambda e: e%2,
'assert': lambda ret: list(ret) == [1, 3]
}
] | [
"[",
"{",
"self",
":",
"[",
"1",
"2",
"3",
"]",
"f",
":",
"lambda",
"e",
":",
"e%2",
"assert",
":",
"lambda",
"ret",
":",
"list",
"(",
"ret",
")",
"==",
"[",
"1",
"3",
"]",
"}",
"]"
] | train | https://github.com/Xython/Linq.py/blob/ffb65f92f1df0d8161d5f835f5947554f6f33d72/linq/standard/dict.py#L126-L139 |
Xython/Linq.py | linq/standard/dict.py | TakeWhile | def TakeWhile(self: dict, f):
"""
[
{
'self': [1, 2, 3, 4, 5],
'f': lambda x: x < 4,
'assert': lambda ret: list(ret) == [1, 2, 3]
}
]
"""
if is_to_destruct(f):
f = destruct_func(f)
for e in self.items():
if not f(e):
break
yield e | python | def TakeWhile(self: dict, f):
"""
[
{
'self': [1, 2, 3, 4, 5],
'f': lambda x: x < 4,
'assert': lambda ret: list(ret) == [1, 2, 3]
}
]
"""
if is_to_destruct(f):
f = destruct_func(f)
for e in self.items():
if not f(e):
break
yield e | [
"def",
"TakeWhile",
"(",
"self",
":",
"dict",
",",
"f",
")",
":",
"if",
"is_to_destruct",
"(",
"f",
")",
":",
"f",
"=",
"destruct_func",
"(",
"f",
")",
"for",
"e",
"in",
"self",
".",
"items",
"(",
")",
":",
"if",
"not",
"f",
"(",
"e",
")",
":",
"break",
"yield",
"e"
] | [
{
'self': [1, 2, 3, 4, 5],
'f': lambda x: x < 4,
'assert': lambda ret: list(ret) == [1, 2, 3]
}
] | [
"[",
"{",
"self",
":",
"[",
"1",
"2",
"3",
"4",
"5",
"]",
"f",
":",
"lambda",
"x",
":",
"x",
"<",
"4",
"assert",
":",
"lambda",
"ret",
":",
"list",
"(",
"ret",
")",
"==",
"[",
"1",
"2",
"3",
"]",
"}",
"]"
] | train | https://github.com/Xython/Linq.py/blob/ffb65f92f1df0d8161d5f835f5947554f6f33d72/linq/standard/dict.py#L143-L159 |
Xython/Linq.py | linq/standard/dict.py | Drop | def Drop(self: dict, n):
"""
[
{
'self': [1, 2, 3, 4, 5],
'n': 3,
'assert': lambda ret: list(ret) == [1, 2]
}
]
"""
n = len(self) - n
if n <= 0:
yield from self.items()
else:
for i, e in enumerate(self.items()):
if i == n:
break
yield e | python | def Drop(self: dict, n):
"""
[
{
'self': [1, 2, 3, 4, 5],
'n': 3,
'assert': lambda ret: list(ret) == [1, 2]
}
]
"""
n = len(self) - n
if n <= 0:
yield from self.items()
else:
for i, e in enumerate(self.items()):
if i == n:
break
yield e | [
"def",
"Drop",
"(",
"self",
":",
"dict",
",",
"n",
")",
":",
"n",
"=",
"len",
"(",
"self",
")",
"-",
"n",
"if",
"n",
"<=",
"0",
":",
"yield",
"from",
"self",
".",
"items",
"(",
")",
"else",
":",
"for",
"i",
",",
"e",
"in",
"enumerate",
"(",
"self",
".",
"items",
"(",
")",
")",
":",
"if",
"i",
"==",
"n",
":",
"break",
"yield",
"e"
] | [
{
'self': [1, 2, 3, 4, 5],
'n': 3,
'assert': lambda ret: list(ret) == [1, 2]
}
] | [
"[",
"{",
"self",
":",
"[",
"1",
"2",
"3",
"4",
"5",
"]",
"n",
":",
"3",
"assert",
":",
"lambda",
"ret",
":",
"list",
"(",
"ret",
")",
"==",
"[",
"1",
"2",
"]",
"}",
"]"
] | train | https://github.com/Xython/Linq.py/blob/ffb65f92f1df0d8161d5f835f5947554f6f33d72/linq/standard/dict.py#L163-L180 |
Xython/Linq.py | linq/standard/dict.py | Skip | def Skip(self: dict, n):
"""
[
{
'self': [1, 2, 3, 4, 5],
'n': 3,
'assert': lambda ret: list(ret) == [4, 5]
}
]
"""
con = self.items()
for i, _ in enumerate(con):
if i == n:
break
return con | python | def Skip(self: dict, n):
"""
[
{
'self': [1, 2, 3, 4, 5],
'n': 3,
'assert': lambda ret: list(ret) == [4, 5]
}
]
"""
con = self.items()
for i, _ in enumerate(con):
if i == n:
break
return con | [
"def",
"Skip",
"(",
"self",
":",
"dict",
",",
"n",
")",
":",
"con",
"=",
"self",
".",
"items",
"(",
")",
"for",
"i",
",",
"_",
"in",
"enumerate",
"(",
"con",
")",
":",
"if",
"i",
"==",
"n",
":",
"break",
"return",
"con"
] | [
{
'self': [1, 2, 3, 4, 5],
'n': 3,
'assert': lambda ret: list(ret) == [4, 5]
}
] | [
"[",
"{",
"self",
":",
"[",
"1",
"2",
"3",
"4",
"5",
"]",
"n",
":",
"3",
"assert",
":",
"lambda",
"ret",
":",
"list",
"(",
"ret",
")",
"==",
"[",
"4",
"5",
"]",
"}",
"]"
] | train | https://github.com/Xython/Linq.py/blob/ffb65f92f1df0d8161d5f835f5947554f6f33d72/linq/standard/dict.py#L184-L199 |
aguinane/nem-reader | print_examples.py | print_meter_record | def print_meter_record(file_path, rows=5):
""" Output readings for specified number of rows to console """
m = nr.read_nem_file(file_path)
print('Header:', m.header)
print('Transactions:', m.transactions)
for nmi in m.readings:
for channel in m.readings[nmi]:
print(nmi, 'Channel', channel)
for reading in m.readings[nmi][channel][-rows:]:
print('', reading) | python | def print_meter_record(file_path, rows=5):
""" Output readings for specified number of rows to console """
m = nr.read_nem_file(file_path)
print('Header:', m.header)
print('Transactions:', m.transactions)
for nmi in m.readings:
for channel in m.readings[nmi]:
print(nmi, 'Channel', channel)
for reading in m.readings[nmi][channel][-rows:]:
print('', reading) | [
"def",
"print_meter_record",
"(",
"file_path",
",",
"rows",
"=",
"5",
")",
":",
"m",
"=",
"nr",
".",
"read_nem_file",
"(",
"file_path",
")",
"print",
"(",
"'Header:'",
",",
"m",
".",
"header",
")",
"print",
"(",
"'Transactions:'",
",",
"m",
".",
"transactions",
")",
"for",
"nmi",
"in",
"m",
".",
"readings",
":",
"for",
"channel",
"in",
"m",
".",
"readings",
"[",
"nmi",
"]",
":",
"print",
"(",
"nmi",
",",
"'Channel'",
",",
"channel",
")",
"for",
"reading",
"in",
"m",
".",
"readings",
"[",
"nmi",
"]",
"[",
"channel",
"]",
"[",
"-",
"rows",
":",
"]",
":",
"print",
"(",
"''",
",",
"reading",
")"
] | Output readings for specified number of rows to console | [
"Output",
"readings",
"for",
"specified",
"number",
"of",
"rows",
"to",
"console"
] | train | https://github.com/aguinane/nem-reader/blob/5405a5cba4bb8ebdad05c28455d12bb34a6d3ce5/print_examples.py#L4-L13 |
ergo/ziggurat_foundations | ziggurat_foundations/models/resource.py | ResourceMixin.users | def users(self):
""" returns all users that have permissions for this resource"""
return sa.orm.relationship(
"User",
secondary="users_resources_permissions",
passive_deletes=True,
passive_updates=True,
) | python | def users(self):
""" returns all users that have permissions for this resource"""
return sa.orm.relationship(
"User",
secondary="users_resources_permissions",
passive_deletes=True,
passive_updates=True,
) | [
"def",
"users",
"(",
"self",
")",
":",
"return",
"sa",
".",
"orm",
".",
"relationship",
"(",
"\"User\"",
",",
"secondary",
"=",
"\"users_resources_permissions\"",
",",
"passive_deletes",
"=",
"True",
",",
"passive_updates",
"=",
"True",
",",
")"
] | returns all users that have permissions for this resource | [
"returns",
"all",
"users",
"that",
"have",
"permissions",
"for",
"this",
"resource"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/resource.py#L98-L105 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/resource_tree_postgres.py | ResourceTreeServicePostgreSQL.from_resource_deeper | def from_resource_deeper(
cls, resource_id=None, limit_depth=1000000, db_session=None, *args, **kwargs
):
"""
This returns you subtree of ordered objects relative
to the start resource_id (currently only implemented in postgresql)
:param resource_id:
:param limit_depth:
:param db_session:
:return:
"""
tablename = cls.model.__table__.name
raw_q = """
WITH RECURSIVE subtree AS (
SELECT res.*, 1 AS depth, LPAD(res.ordering::CHARACTER VARYING, 7, '0') AS sorting,
res.resource_id::CHARACTER VARYING AS path
FROM {tablename} AS res WHERE res.resource_id = :resource_id
UNION ALL
SELECT res_u.*, depth+1 AS depth,
(st.sorting::CHARACTER VARYING || '/' || LPAD(res_u.ordering::CHARACTER VARYING, 7, '0') ) AS sorting,
(st.path::CHARACTER VARYING || '/' || res_u.resource_id::CHARACTER VARYING ) AS path
FROM {tablename} res_u, subtree st
WHERE res_u.parent_id = st.resource_id
)
SELECT * FROM subtree WHERE depth<=:depth ORDER BY sorting;
""".format(
tablename=tablename
) # noqa
db_session = get_db_session(db_session)
text_obj = sa.text(raw_q)
query = db_session.query(cls.model, "depth", "sorting", "path")
query = query.from_statement(text_obj)
query = query.params(resource_id=resource_id, depth=limit_depth)
return query | python | def from_resource_deeper(
cls, resource_id=None, limit_depth=1000000, db_session=None, *args, **kwargs
):
"""
This returns you subtree of ordered objects relative
to the start resource_id (currently only implemented in postgresql)
:param resource_id:
:param limit_depth:
:param db_session:
:return:
"""
tablename = cls.model.__table__.name
raw_q = """
WITH RECURSIVE subtree AS (
SELECT res.*, 1 AS depth, LPAD(res.ordering::CHARACTER VARYING, 7, '0') AS sorting,
res.resource_id::CHARACTER VARYING AS path
FROM {tablename} AS res WHERE res.resource_id = :resource_id
UNION ALL
SELECT res_u.*, depth+1 AS depth,
(st.sorting::CHARACTER VARYING || '/' || LPAD(res_u.ordering::CHARACTER VARYING, 7, '0') ) AS sorting,
(st.path::CHARACTER VARYING || '/' || res_u.resource_id::CHARACTER VARYING ) AS path
FROM {tablename} res_u, subtree st
WHERE res_u.parent_id = st.resource_id
)
SELECT * FROM subtree WHERE depth<=:depth ORDER BY sorting;
""".format(
tablename=tablename
) # noqa
db_session = get_db_session(db_session)
text_obj = sa.text(raw_q)
query = db_session.query(cls.model, "depth", "sorting", "path")
query = query.from_statement(text_obj)
query = query.params(resource_id=resource_id, depth=limit_depth)
return query | [
"def",
"from_resource_deeper",
"(",
"cls",
",",
"resource_id",
"=",
"None",
",",
"limit_depth",
"=",
"1000000",
",",
"db_session",
"=",
"None",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"tablename",
"=",
"cls",
".",
"model",
".",
"__table__",
".",
"name",
"raw_q",
"=",
"\"\"\"\n WITH RECURSIVE subtree AS (\n SELECT res.*, 1 AS depth, LPAD(res.ordering::CHARACTER VARYING, 7, '0') AS sorting,\n res.resource_id::CHARACTER VARYING AS path\n FROM {tablename} AS res WHERE res.resource_id = :resource_id\n UNION ALL\n SELECT res_u.*, depth+1 AS depth,\n (st.sorting::CHARACTER VARYING || '/' || LPAD(res_u.ordering::CHARACTER VARYING, 7, '0') ) AS sorting,\n (st.path::CHARACTER VARYING || '/' || res_u.resource_id::CHARACTER VARYING ) AS path\n FROM {tablename} res_u, subtree st\n WHERE res_u.parent_id = st.resource_id\n )\n SELECT * FROM subtree WHERE depth<=:depth ORDER BY sorting;\n \"\"\"",
".",
"format",
"(",
"tablename",
"=",
"tablename",
")",
"# noqa",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"text_obj",
"=",
"sa",
".",
"text",
"(",
"raw_q",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
",",
"\"depth\"",
",",
"\"sorting\"",
",",
"\"path\"",
")",
"query",
"=",
"query",
".",
"from_statement",
"(",
"text_obj",
")",
"query",
"=",
"query",
".",
"params",
"(",
"resource_id",
"=",
"resource_id",
",",
"depth",
"=",
"limit_depth",
")",
"return",
"query"
] | This returns you subtree of ordered objects relative
to the start resource_id (currently only implemented in postgresql)
:param resource_id:
:param limit_depth:
:param db_session:
:return: | [
"This",
"returns",
"you",
"subtree",
"of",
"ordered",
"objects",
"relative",
"to",
"the",
"start",
"resource_id",
"(",
"currently",
"only",
"implemented",
"in",
"postgresql",
")"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/resource_tree_postgres.py#L24-L58 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/resource_tree_postgres.py | ResourceTreeServicePostgreSQL.delete_branch | def delete_branch(cls, resource_id=None, db_session=None, *args, **kwargs):
"""
This deletes whole branch with children starting from resource_id
:param resource_id:
:param db_session:
:return:
"""
tablename = cls.model.__table__.name
# lets lock rows to prevent bad tree states
resource = ResourceService.lock_resource_for_update(
resource_id=resource_id, db_session=db_session
)
parent_id = resource.parent_id
ordering = resource.ordering
raw_q = """
WITH RECURSIVE subtree AS (
SELECT res.resource_id
FROM {tablename} AS res WHERE res.resource_id = :resource_id
UNION ALL
SELECT res_u.resource_id
FROM {tablename} res_u, subtree st
WHERE res_u.parent_id = st.resource_id
)
DELETE FROM resources where resource_id in (select * from subtree);
""".format(
tablename=tablename
) # noqa
db_session = get_db_session(db_session)
text_obj = sa.text(raw_q)
db_session.execute(text_obj, params={"resource_id": resource_id})
cls.shift_ordering_down(parent_id, ordering, db_session=db_session)
return True | python | def delete_branch(cls, resource_id=None, db_session=None, *args, **kwargs):
"""
This deletes whole branch with children starting from resource_id
:param resource_id:
:param db_session:
:return:
"""
tablename = cls.model.__table__.name
# lets lock rows to prevent bad tree states
resource = ResourceService.lock_resource_for_update(
resource_id=resource_id, db_session=db_session
)
parent_id = resource.parent_id
ordering = resource.ordering
raw_q = """
WITH RECURSIVE subtree AS (
SELECT res.resource_id
FROM {tablename} AS res WHERE res.resource_id = :resource_id
UNION ALL
SELECT res_u.resource_id
FROM {tablename} res_u, subtree st
WHERE res_u.parent_id = st.resource_id
)
DELETE FROM resources where resource_id in (select * from subtree);
""".format(
tablename=tablename
) # noqa
db_session = get_db_session(db_session)
text_obj = sa.text(raw_q)
db_session.execute(text_obj, params={"resource_id": resource_id})
cls.shift_ordering_down(parent_id, ordering, db_session=db_session)
return True | [
"def",
"delete_branch",
"(",
"cls",
",",
"resource_id",
"=",
"None",
",",
"db_session",
"=",
"None",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"tablename",
"=",
"cls",
".",
"model",
".",
"__table__",
".",
"name",
"# lets lock rows to prevent bad tree states",
"resource",
"=",
"ResourceService",
".",
"lock_resource_for_update",
"(",
"resource_id",
"=",
"resource_id",
",",
"db_session",
"=",
"db_session",
")",
"parent_id",
"=",
"resource",
".",
"parent_id",
"ordering",
"=",
"resource",
".",
"ordering",
"raw_q",
"=",
"\"\"\"\n WITH RECURSIVE subtree AS (\n SELECT res.resource_id\n FROM {tablename} AS res WHERE res.resource_id = :resource_id\n UNION ALL\n SELECT res_u.resource_id\n FROM {tablename} res_u, subtree st\n WHERE res_u.parent_id = st.resource_id\n )\n DELETE FROM resources where resource_id in (select * from subtree);\n \"\"\"",
".",
"format",
"(",
"tablename",
"=",
"tablename",
")",
"# noqa",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"text_obj",
"=",
"sa",
".",
"text",
"(",
"raw_q",
")",
"db_session",
".",
"execute",
"(",
"text_obj",
",",
"params",
"=",
"{",
"\"resource_id\"",
":",
"resource_id",
"}",
")",
"cls",
".",
"shift_ordering_down",
"(",
"parent_id",
",",
"ordering",
",",
"db_session",
"=",
"db_session",
")",
"return",
"True"
] | This deletes whole branch with children starting from resource_id
:param resource_id:
:param db_session:
:return: | [
"This",
"deletes",
"whole",
"branch",
"with",
"children",
"starting",
"from",
"resource_id"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/resource_tree_postgres.py#L61-L94 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/resource_tree_postgres.py | ResourceTreeServicePostgreSQL.from_parent_deeper | def from_parent_deeper(
cls, parent_id=None, limit_depth=1000000, db_session=None, *args, **kwargs
):
"""
This returns you subtree of ordered objects relative
to the start parent_id (currently only implemented in postgresql)
:param resource_id:
:param limit_depth:
:param db_session:
:return:
"""
if parent_id:
limiting_clause = "res.parent_id = :parent_id"
else:
limiting_clause = "res.parent_id is null"
tablename = cls.model.__table__.name
raw_q = """
WITH RECURSIVE subtree AS (
SELECT res.*, 1 AS depth, LPAD(res.ordering::CHARACTER VARYING, 7, '0') AS sorting,
res.resource_id::CHARACTER VARYING AS path
FROM {tablename} AS res WHERE {limiting_clause}
UNION ALL
SELECT res_u.*, depth+1 AS depth,
(st.sorting::CHARACTER VARYING || '/' || LPAD(res_u.ordering::CHARACTER VARYING, 7, '0') ) AS sorting,
(st.path::CHARACTER VARYING || '/' || res_u.resource_id::CHARACTER VARYING ) AS path
FROM {tablename} res_u, subtree st
WHERE res_u.parent_id = st.resource_id
)
SELECT * FROM subtree WHERE depth<=:depth ORDER BY sorting;
""".format(
tablename=tablename, limiting_clause=limiting_clause
) # noqa
db_session = get_db_session(db_session)
text_obj = sa.text(raw_q)
query = db_session.query(cls.model, "depth", "sorting", "path")
query = query.from_statement(text_obj)
query = query.params(parent_id=parent_id, depth=limit_depth)
return query | python | def from_parent_deeper(
cls, parent_id=None, limit_depth=1000000, db_session=None, *args, **kwargs
):
"""
This returns you subtree of ordered objects relative
to the start parent_id (currently only implemented in postgresql)
:param resource_id:
:param limit_depth:
:param db_session:
:return:
"""
if parent_id:
limiting_clause = "res.parent_id = :parent_id"
else:
limiting_clause = "res.parent_id is null"
tablename = cls.model.__table__.name
raw_q = """
WITH RECURSIVE subtree AS (
SELECT res.*, 1 AS depth, LPAD(res.ordering::CHARACTER VARYING, 7, '0') AS sorting,
res.resource_id::CHARACTER VARYING AS path
FROM {tablename} AS res WHERE {limiting_clause}
UNION ALL
SELECT res_u.*, depth+1 AS depth,
(st.sorting::CHARACTER VARYING || '/' || LPAD(res_u.ordering::CHARACTER VARYING, 7, '0') ) AS sorting,
(st.path::CHARACTER VARYING || '/' || res_u.resource_id::CHARACTER VARYING ) AS path
FROM {tablename} res_u, subtree st
WHERE res_u.parent_id = st.resource_id
)
SELECT * FROM subtree WHERE depth<=:depth ORDER BY sorting;
""".format(
tablename=tablename, limiting_clause=limiting_clause
) # noqa
db_session = get_db_session(db_session)
text_obj = sa.text(raw_q)
query = db_session.query(cls.model, "depth", "sorting", "path")
query = query.from_statement(text_obj)
query = query.params(parent_id=parent_id, depth=limit_depth)
return query | [
"def",
"from_parent_deeper",
"(",
"cls",
",",
"parent_id",
"=",
"None",
",",
"limit_depth",
"=",
"1000000",
",",
"db_session",
"=",
"None",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"parent_id",
":",
"limiting_clause",
"=",
"\"res.parent_id = :parent_id\"",
"else",
":",
"limiting_clause",
"=",
"\"res.parent_id is null\"",
"tablename",
"=",
"cls",
".",
"model",
".",
"__table__",
".",
"name",
"raw_q",
"=",
"\"\"\"\n WITH RECURSIVE subtree AS (\n SELECT res.*, 1 AS depth, LPAD(res.ordering::CHARACTER VARYING, 7, '0') AS sorting,\n res.resource_id::CHARACTER VARYING AS path\n FROM {tablename} AS res WHERE {limiting_clause}\n UNION ALL\n SELECT res_u.*, depth+1 AS depth,\n (st.sorting::CHARACTER VARYING || '/' || LPAD(res_u.ordering::CHARACTER VARYING, 7, '0') ) AS sorting,\n (st.path::CHARACTER VARYING || '/' || res_u.resource_id::CHARACTER VARYING ) AS path\n FROM {tablename} res_u, subtree st\n WHERE res_u.parent_id = st.resource_id\n )\n SELECT * FROM subtree WHERE depth<=:depth ORDER BY sorting;\n \"\"\"",
".",
"format",
"(",
"tablename",
"=",
"tablename",
",",
"limiting_clause",
"=",
"limiting_clause",
")",
"# noqa",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"text_obj",
"=",
"sa",
".",
"text",
"(",
"raw_q",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
",",
"\"depth\"",
",",
"\"sorting\"",
",",
"\"path\"",
")",
"query",
"=",
"query",
".",
"from_statement",
"(",
"text_obj",
")",
"query",
"=",
"query",
".",
"params",
"(",
"parent_id",
"=",
"parent_id",
",",
"depth",
"=",
"limit_depth",
")",
"return",
"query"
] | This returns you subtree of ordered objects relative
to the start parent_id (currently only implemented in postgresql)
:param resource_id:
:param limit_depth:
:param db_session:
:return: | [
"This",
"returns",
"you",
"subtree",
"of",
"ordered",
"objects",
"relative",
"to",
"the",
"start",
"parent_id",
"(",
"currently",
"only",
"implemented",
"in",
"postgresql",
")"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/resource_tree_postgres.py#L97-L136 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/resource_tree_postgres.py | ResourceTreeServicePostgreSQL.build_subtree_strut | def build_subtree_strut(self, result, *args, **kwargs):
"""
Returns a dictionary in form of
{node:Resource, children:{node_id: Resource}}
:param result:
:return:
"""
items = list(result)
root_elem = {"node": None, "children": OrderedDict()}
if len(items) == 0:
return root_elem
for _, node in enumerate(items):
new_elem = {"node": node.Resource, "children": OrderedDict()}
path = list(map(int, node.path.split("/")))
parent_node = root_elem
normalized_path = path[:-1]
if normalized_path:
for path_part in normalized_path:
parent_node = parent_node["children"][path_part]
parent_node["children"][new_elem["node"].resource_id] = new_elem
return root_elem | python | def build_subtree_strut(self, result, *args, **kwargs):
"""
Returns a dictionary in form of
{node:Resource, children:{node_id: Resource}}
:param result:
:return:
"""
items = list(result)
root_elem = {"node": None, "children": OrderedDict()}
if len(items) == 0:
return root_elem
for _, node in enumerate(items):
new_elem = {"node": node.Resource, "children": OrderedDict()}
path = list(map(int, node.path.split("/")))
parent_node = root_elem
normalized_path = path[:-1]
if normalized_path:
for path_part in normalized_path:
parent_node = parent_node["children"][path_part]
parent_node["children"][new_elem["node"].resource_id] = new_elem
return root_elem | [
"def",
"build_subtree_strut",
"(",
"self",
",",
"result",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"items",
"=",
"list",
"(",
"result",
")",
"root_elem",
"=",
"{",
"\"node\"",
":",
"None",
",",
"\"children\"",
":",
"OrderedDict",
"(",
")",
"}",
"if",
"len",
"(",
"items",
")",
"==",
"0",
":",
"return",
"root_elem",
"for",
"_",
",",
"node",
"in",
"enumerate",
"(",
"items",
")",
":",
"new_elem",
"=",
"{",
"\"node\"",
":",
"node",
".",
"Resource",
",",
"\"children\"",
":",
"OrderedDict",
"(",
")",
"}",
"path",
"=",
"list",
"(",
"map",
"(",
"int",
",",
"node",
".",
"path",
".",
"split",
"(",
"\"/\"",
")",
")",
")",
"parent_node",
"=",
"root_elem",
"normalized_path",
"=",
"path",
"[",
":",
"-",
"1",
"]",
"if",
"normalized_path",
":",
"for",
"path_part",
"in",
"normalized_path",
":",
"parent_node",
"=",
"parent_node",
"[",
"\"children\"",
"]",
"[",
"path_part",
"]",
"parent_node",
"[",
"\"children\"",
"]",
"[",
"new_elem",
"[",
"\"node\"",
"]",
".",
"resource_id",
"]",
"=",
"new_elem",
"return",
"root_elem"
] | Returns a dictionary in form of
{node:Resource, children:{node_id: Resource}}
:param result:
:return: | [
"Returns",
"a",
"dictionary",
"in",
"form",
"of",
"{",
"node",
":",
"Resource",
"children",
":",
"{",
"node_id",
":",
"Resource",
"}}"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/resource_tree_postgres.py#L139-L160 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/resource_tree_postgres.py | ResourceTreeServicePostgreSQL.path_upper | def path_upper(
cls, object_id, limit_depth=1000000, db_session=None, *args, **kwargs
):
"""
This returns you path to root node starting from object_id
currently only for postgresql
:param object_id:
:param limit_depth:
:param db_session:
:return:
"""
tablename = cls.model.__table__.name
raw_q = """
WITH RECURSIVE subtree AS (
SELECT res.*, 1 as depth FROM {tablename} res
WHERE res.resource_id = :resource_id
UNION ALL
SELECT res_u.*, depth+1 as depth
FROM {tablename} res_u, subtree st
WHERE res_u.resource_id = st.parent_id
)
SELECT * FROM subtree WHERE depth<=:depth;
""".format(
tablename=tablename
)
db_session = get_db_session(db_session)
q = (
db_session.query(cls.model)
.from_statement(sa.text(raw_q))
.params(resource_id=object_id, depth=limit_depth)
)
return q | python | def path_upper(
cls, object_id, limit_depth=1000000, db_session=None, *args, **kwargs
):
"""
This returns you path to root node starting from object_id
currently only for postgresql
:param object_id:
:param limit_depth:
:param db_session:
:return:
"""
tablename = cls.model.__table__.name
raw_q = """
WITH RECURSIVE subtree AS (
SELECT res.*, 1 as depth FROM {tablename} res
WHERE res.resource_id = :resource_id
UNION ALL
SELECT res_u.*, depth+1 as depth
FROM {tablename} res_u, subtree st
WHERE res_u.resource_id = st.parent_id
)
SELECT * FROM subtree WHERE depth<=:depth;
""".format(
tablename=tablename
)
db_session = get_db_session(db_session)
q = (
db_session.query(cls.model)
.from_statement(sa.text(raw_q))
.params(resource_id=object_id, depth=limit_depth)
)
return q | [
"def",
"path_upper",
"(",
"cls",
",",
"object_id",
",",
"limit_depth",
"=",
"1000000",
",",
"db_session",
"=",
"None",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"tablename",
"=",
"cls",
".",
"model",
".",
"__table__",
".",
"name",
"raw_q",
"=",
"\"\"\"\n WITH RECURSIVE subtree AS (\n SELECT res.*, 1 as depth FROM {tablename} res\n WHERE res.resource_id = :resource_id\n UNION ALL\n SELECT res_u.*, depth+1 as depth\n FROM {tablename} res_u, subtree st\n WHERE res_u.resource_id = st.parent_id\n )\n SELECT * FROM subtree WHERE depth<=:depth;\n \"\"\"",
".",
"format",
"(",
"tablename",
"=",
"tablename",
")",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"q",
"=",
"(",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
".",
"from_statement",
"(",
"sa",
".",
"text",
"(",
"raw_q",
")",
")",
".",
"params",
"(",
"resource_id",
"=",
"object_id",
",",
"depth",
"=",
"limit_depth",
")",
")",
"return",
"q"
] | This returns you path to root node starting from object_id
currently only for postgresql
:param object_id:
:param limit_depth:
:param db_session:
:return: | [
"This",
"returns",
"you",
"path",
"to",
"root",
"node",
"starting",
"from",
"object_id",
"currently",
"only",
"for",
"postgresql"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/resource_tree_postgres.py#L163-L195 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/resource_tree_postgres.py | ResourceTreeServicePostgreSQL.move_to_position | def move_to_position(
cls,
resource_id,
to_position,
new_parent_id=noop,
db_session=None,
*args,
**kwargs
):
"""
Moves node to new location in the tree
:param resource_id: resource to move
:param to_position: new position
:param new_parent_id: new parent id
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
# lets lock rows to prevent bad tree states
resource = ResourceService.lock_resource_for_update(
resource_id=resource_id, db_session=db_session
)
ResourceService.lock_resource_for_update(
resource_id=resource.parent_id, db_session=db_session
)
same_branch = False
# reset if parent is same as old
if new_parent_id == resource.parent_id:
new_parent_id = noop
if new_parent_id is not noop:
cls.check_node_parent(resource_id, new_parent_id, db_session=db_session)
else:
same_branch = True
if new_parent_id is noop:
# it is not guaranteed that parent exists
parent_id = resource.parent_id if resource else None
else:
parent_id = new_parent_id
cls.check_node_position(
parent_id, to_position, on_same_branch=same_branch, db_session=db_session
)
# move on same branch
if new_parent_id is noop:
order_range = list(sorted((resource.ordering, to_position)))
move_down = resource.ordering > to_position
query = db_session.query(cls.model)
query = query.filter(cls.model.parent_id == parent_id)
query = query.filter(cls.model.ordering.between(*order_range))
if move_down:
query.update(
{cls.model.ordering: cls.model.ordering + 1},
synchronize_session=False,
)
else:
query.update(
{cls.model.ordering: cls.model.ordering - 1},
synchronize_session=False,
)
db_session.flush()
db_session.expire(resource)
resource.ordering = to_position
# move between branches
else:
cls.shift_ordering_down(
resource.parent_id, resource.ordering, db_session=db_session
)
cls.shift_ordering_up(new_parent_id, to_position, db_session=db_session)
db_session.expire(resource)
resource.parent_id = new_parent_id
resource.ordering = to_position
db_session.flush()
return True | python | def move_to_position(
cls,
resource_id,
to_position,
new_parent_id=noop,
db_session=None,
*args,
**kwargs
):
"""
Moves node to new location in the tree
:param resource_id: resource to move
:param to_position: new position
:param new_parent_id: new parent id
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
# lets lock rows to prevent bad tree states
resource = ResourceService.lock_resource_for_update(
resource_id=resource_id, db_session=db_session
)
ResourceService.lock_resource_for_update(
resource_id=resource.parent_id, db_session=db_session
)
same_branch = False
# reset if parent is same as old
if new_parent_id == resource.parent_id:
new_parent_id = noop
if new_parent_id is not noop:
cls.check_node_parent(resource_id, new_parent_id, db_session=db_session)
else:
same_branch = True
if new_parent_id is noop:
# it is not guaranteed that parent exists
parent_id = resource.parent_id if resource else None
else:
parent_id = new_parent_id
cls.check_node_position(
parent_id, to_position, on_same_branch=same_branch, db_session=db_session
)
# move on same branch
if new_parent_id is noop:
order_range = list(sorted((resource.ordering, to_position)))
move_down = resource.ordering > to_position
query = db_session.query(cls.model)
query = query.filter(cls.model.parent_id == parent_id)
query = query.filter(cls.model.ordering.between(*order_range))
if move_down:
query.update(
{cls.model.ordering: cls.model.ordering + 1},
synchronize_session=False,
)
else:
query.update(
{cls.model.ordering: cls.model.ordering - 1},
synchronize_session=False,
)
db_session.flush()
db_session.expire(resource)
resource.ordering = to_position
# move between branches
else:
cls.shift_ordering_down(
resource.parent_id, resource.ordering, db_session=db_session
)
cls.shift_ordering_up(new_parent_id, to_position, db_session=db_session)
db_session.expire(resource)
resource.parent_id = new_parent_id
resource.ordering = to_position
db_session.flush()
return True | [
"def",
"move_to_position",
"(",
"cls",
",",
"resource_id",
",",
"to_position",
",",
"new_parent_id",
"=",
"noop",
",",
"db_session",
"=",
"None",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"# lets lock rows to prevent bad tree states",
"resource",
"=",
"ResourceService",
".",
"lock_resource_for_update",
"(",
"resource_id",
"=",
"resource_id",
",",
"db_session",
"=",
"db_session",
")",
"ResourceService",
".",
"lock_resource_for_update",
"(",
"resource_id",
"=",
"resource",
".",
"parent_id",
",",
"db_session",
"=",
"db_session",
")",
"same_branch",
"=",
"False",
"# reset if parent is same as old",
"if",
"new_parent_id",
"==",
"resource",
".",
"parent_id",
":",
"new_parent_id",
"=",
"noop",
"if",
"new_parent_id",
"is",
"not",
"noop",
":",
"cls",
".",
"check_node_parent",
"(",
"resource_id",
",",
"new_parent_id",
",",
"db_session",
"=",
"db_session",
")",
"else",
":",
"same_branch",
"=",
"True",
"if",
"new_parent_id",
"is",
"noop",
":",
"# it is not guaranteed that parent exists",
"parent_id",
"=",
"resource",
".",
"parent_id",
"if",
"resource",
"else",
"None",
"else",
":",
"parent_id",
"=",
"new_parent_id",
"cls",
".",
"check_node_position",
"(",
"parent_id",
",",
"to_position",
",",
"on_same_branch",
"=",
"same_branch",
",",
"db_session",
"=",
"db_session",
")",
"# move on same branch",
"if",
"new_parent_id",
"is",
"noop",
":",
"order_range",
"=",
"list",
"(",
"sorted",
"(",
"(",
"resource",
".",
"ordering",
",",
"to_position",
")",
")",
")",
"move_down",
"=",
"resource",
".",
"ordering",
">",
"to_position",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"model",
".",
"parent_id",
"==",
"parent_id",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"model",
".",
"ordering",
".",
"between",
"(",
"*",
"order_range",
")",
")",
"if",
"move_down",
":",
"query",
".",
"update",
"(",
"{",
"cls",
".",
"model",
".",
"ordering",
":",
"cls",
".",
"model",
".",
"ordering",
"+",
"1",
"}",
",",
"synchronize_session",
"=",
"False",
",",
")",
"else",
":",
"query",
".",
"update",
"(",
"{",
"cls",
".",
"model",
".",
"ordering",
":",
"cls",
".",
"model",
".",
"ordering",
"-",
"1",
"}",
",",
"synchronize_session",
"=",
"False",
",",
")",
"db_session",
".",
"flush",
"(",
")",
"db_session",
".",
"expire",
"(",
"resource",
")",
"resource",
".",
"ordering",
"=",
"to_position",
"# move between branches",
"else",
":",
"cls",
".",
"shift_ordering_down",
"(",
"resource",
".",
"parent_id",
",",
"resource",
".",
"ordering",
",",
"db_session",
"=",
"db_session",
")",
"cls",
".",
"shift_ordering_up",
"(",
"new_parent_id",
",",
"to_position",
",",
"db_session",
"=",
"db_session",
")",
"db_session",
".",
"expire",
"(",
"resource",
")",
"resource",
".",
"parent_id",
"=",
"new_parent_id",
"resource",
".",
"ordering",
"=",
"to_position",
"db_session",
".",
"flush",
"(",
")",
"return",
"True"
] | Moves node to new location in the tree
:param resource_id: resource to move
:param to_position: new position
:param new_parent_id: new parent id
:param db_session:
:return: | [
"Moves",
"node",
"to",
"new",
"location",
"in",
"the",
"tree"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/resource_tree_postgres.py#L198-L275 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/resource_tree_postgres.py | ResourceTreeServicePostgreSQL.shift_ordering_up | def shift_ordering_up(cls, parent_id, position, db_session=None, *args, **kwargs):
"""
Shifts ordering to "open a gap" for node insertion,
begins the shift from given position
:param parent_id:
:param position:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(cls.model.parent_id == parent_id)
query = query.filter(cls.model.ordering >= position)
query.update(
{cls.model.ordering: cls.model.ordering + 1}, synchronize_session=False
)
db_session.flush() | python | def shift_ordering_up(cls, parent_id, position, db_session=None, *args, **kwargs):
"""
Shifts ordering to "open a gap" for node insertion,
begins the shift from given position
:param parent_id:
:param position:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model)
query = query.filter(cls.model.parent_id == parent_id)
query = query.filter(cls.model.ordering >= position)
query.update(
{cls.model.ordering: cls.model.ordering + 1}, synchronize_session=False
)
db_session.flush() | [
"def",
"shift_ordering_up",
"(",
"cls",
",",
"parent_id",
",",
"position",
",",
"db_session",
"=",
"None",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"model",
".",
"parent_id",
"==",
"parent_id",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"model",
".",
"ordering",
">=",
"position",
")",
"query",
".",
"update",
"(",
"{",
"cls",
".",
"model",
".",
"ordering",
":",
"cls",
".",
"model",
".",
"ordering",
"+",
"1",
"}",
",",
"synchronize_session",
"=",
"False",
")",
"db_session",
".",
"flush",
"(",
")"
] | Shifts ordering to "open a gap" for node insertion,
begins the shift from given position
:param parent_id:
:param position:
:param db_session:
:return: | [
"Shifts",
"ordering",
"to",
"open",
"a",
"gap",
"for",
"node",
"insertion",
"begins",
"the",
"shift",
"from",
"given",
"position"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/resource_tree_postgres.py#L298-L315 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/resource_tree_postgres.py | ResourceTreeServicePostgreSQL.set_position | def set_position(cls, resource_id, to_position, db_session=None, *args, **kwargs):
"""
Sets node position for new node in the tree
:param resource_id: resource to move
:param to_position: new position
:param db_session:
:return:def count_children(cls, resource_id, db_session=None):
"""
db_session = get_db_session(db_session)
# lets lock rows to prevent bad tree states
resource = ResourceService.lock_resource_for_update(
resource_id=resource_id, db_session=db_session
)
cls.check_node_position(
resource.parent_id, to_position, on_same_branch=True, db_session=db_session
)
cls.shift_ordering_up(resource.parent_id, to_position, db_session=db_session)
db_session.flush()
db_session.expire(resource)
resource.ordering = to_position
return True | python | def set_position(cls, resource_id, to_position, db_session=None, *args, **kwargs):
"""
Sets node position for new node in the tree
:param resource_id: resource to move
:param to_position: new position
:param db_session:
:return:def count_children(cls, resource_id, db_session=None):
"""
db_session = get_db_session(db_session)
# lets lock rows to prevent bad tree states
resource = ResourceService.lock_resource_for_update(
resource_id=resource_id, db_session=db_session
)
cls.check_node_position(
resource.parent_id, to_position, on_same_branch=True, db_session=db_session
)
cls.shift_ordering_up(resource.parent_id, to_position, db_session=db_session)
db_session.flush()
db_session.expire(resource)
resource.ordering = to_position
return True | [
"def",
"set_position",
"(",
"cls",
",",
"resource_id",
",",
"to_position",
",",
"db_session",
"=",
"None",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"# lets lock rows to prevent bad tree states",
"resource",
"=",
"ResourceService",
".",
"lock_resource_for_update",
"(",
"resource_id",
"=",
"resource_id",
",",
"db_session",
"=",
"db_session",
")",
"cls",
".",
"check_node_position",
"(",
"resource",
".",
"parent_id",
",",
"to_position",
",",
"on_same_branch",
"=",
"True",
",",
"db_session",
"=",
"db_session",
")",
"cls",
".",
"shift_ordering_up",
"(",
"resource",
".",
"parent_id",
",",
"to_position",
",",
"db_session",
"=",
"db_session",
")",
"db_session",
".",
"flush",
"(",
")",
"db_session",
".",
"expire",
"(",
"resource",
")",
"resource",
".",
"ordering",
"=",
"to_position",
"return",
"True"
] | Sets node position for new node in the tree
:param resource_id: resource to move
:param to_position: new position
:param db_session:
:return:def count_children(cls, resource_id, db_session=None): | [
"Sets",
"node",
"position",
"for",
"new",
"node",
"in",
"the",
"tree"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/resource_tree_postgres.py#L318-L339 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/resource_tree_postgres.py | ResourceTreeServicePostgreSQL.check_node_parent | def check_node_parent(
cls, resource_id, new_parent_id, db_session=None, *args, **kwargs
):
"""
Checks if parent destination is valid for node
:param resource_id:
:param new_parent_id:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
new_parent = ResourceService.lock_resource_for_update(
resource_id=new_parent_id, db_session=db_session
)
# we are not moving to "root" so parent should be found
if not new_parent and new_parent_id is not None:
raise ZigguratResourceTreeMissingException("New parent node not found")
else:
result = cls.path_upper(new_parent_id, db_session=db_session)
path_ids = [r.resource_id for r in result]
if resource_id in path_ids:
raise ZigguratResourceTreePathException(
"Trying to insert node into itself"
) | python | def check_node_parent(
cls, resource_id, new_parent_id, db_session=None, *args, **kwargs
):
"""
Checks if parent destination is valid for node
:param resource_id:
:param new_parent_id:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
new_parent = ResourceService.lock_resource_for_update(
resource_id=new_parent_id, db_session=db_session
)
# we are not moving to "root" so parent should be found
if not new_parent and new_parent_id is not None:
raise ZigguratResourceTreeMissingException("New parent node not found")
else:
result = cls.path_upper(new_parent_id, db_session=db_session)
path_ids = [r.resource_id for r in result]
if resource_id in path_ids:
raise ZigguratResourceTreePathException(
"Trying to insert node into itself"
) | [
"def",
"check_node_parent",
"(",
"cls",
",",
"resource_id",
",",
"new_parent_id",
",",
"db_session",
"=",
"None",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"new_parent",
"=",
"ResourceService",
".",
"lock_resource_for_update",
"(",
"resource_id",
"=",
"new_parent_id",
",",
"db_session",
"=",
"db_session",
")",
"# we are not moving to \"root\" so parent should be found",
"if",
"not",
"new_parent",
"and",
"new_parent_id",
"is",
"not",
"None",
":",
"raise",
"ZigguratResourceTreeMissingException",
"(",
"\"New parent node not found\"",
")",
"else",
":",
"result",
"=",
"cls",
".",
"path_upper",
"(",
"new_parent_id",
",",
"db_session",
"=",
"db_session",
")",
"path_ids",
"=",
"[",
"r",
".",
"resource_id",
"for",
"r",
"in",
"result",
"]",
"if",
"resource_id",
"in",
"path_ids",
":",
"raise",
"ZigguratResourceTreePathException",
"(",
"\"Trying to insert node into itself\"",
")"
] | Checks if parent destination is valid for node
:param resource_id:
:param new_parent_id:
:param db_session:
:return: | [
"Checks",
"if",
"parent",
"destination",
"is",
"valid",
"for",
"node"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/resource_tree_postgres.py#L342-L366 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/resource_tree_postgres.py | ResourceTreeServicePostgreSQL.count_children | def count_children(cls, resource_id, db_session=None, *args, **kwargs):
"""
Counts children of resource node
:param resource_id:
:param db_session:
:return:
"""
query = db_session.query(cls.model.resource_id)
query = query.filter(cls.model.parent_id == resource_id)
return query.count() | python | def count_children(cls, resource_id, db_session=None, *args, **kwargs):
"""
Counts children of resource node
:param resource_id:
:param db_session:
:return:
"""
query = db_session.query(cls.model.resource_id)
query = query.filter(cls.model.parent_id == resource_id)
return query.count() | [
"def",
"count_children",
"(",
"cls",
",",
"resource_id",
",",
"db_session",
"=",
"None",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
".",
"resource_id",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"model",
".",
"parent_id",
"==",
"resource_id",
")",
"return",
"query",
".",
"count",
"(",
")"
] | Counts children of resource node
:param resource_id:
:param db_session:
:return: | [
"Counts",
"children",
"of",
"resource",
"node"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/resource_tree_postgres.py#L369-L379 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/resource_tree_postgres.py | ResourceTreeServicePostgreSQL.check_node_position | def check_node_position(
cls, parent_id, position, on_same_branch, db_session=None, *args, **kwargs
):
"""
Checks if node position for given parent is valid, raises exception if
this is not the case
:param parent_id:
:param position:
:param on_same_branch: indicates that we are checking same branch
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
if not position or position < 1:
raise ZigguratResourceOutOfBoundaryException(
"Position is lower than {}", value=1
)
item_count = cls.count_children(parent_id, db_session=db_session)
max_value = item_count if on_same_branch else item_count + 1
if position > max_value:
raise ZigguratResourceOutOfBoundaryException(
"Maximum resource ordering is {}", value=max_value
) | python | def check_node_position(
cls, parent_id, position, on_same_branch, db_session=None, *args, **kwargs
):
"""
Checks if node position for given parent is valid, raises exception if
this is not the case
:param parent_id:
:param position:
:param on_same_branch: indicates that we are checking same branch
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
if not position or position < 1:
raise ZigguratResourceOutOfBoundaryException(
"Position is lower than {}", value=1
)
item_count = cls.count_children(parent_id, db_session=db_session)
max_value = item_count if on_same_branch else item_count + 1
if position > max_value:
raise ZigguratResourceOutOfBoundaryException(
"Maximum resource ordering is {}", value=max_value
) | [
"def",
"check_node_position",
"(",
"cls",
",",
"parent_id",
",",
"position",
",",
"on_same_branch",
",",
"db_session",
"=",
"None",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"if",
"not",
"position",
"or",
"position",
"<",
"1",
":",
"raise",
"ZigguratResourceOutOfBoundaryException",
"(",
"\"Position is lower than {}\"",
",",
"value",
"=",
"1",
")",
"item_count",
"=",
"cls",
".",
"count_children",
"(",
"parent_id",
",",
"db_session",
"=",
"db_session",
")",
"max_value",
"=",
"item_count",
"if",
"on_same_branch",
"else",
"item_count",
"+",
"1",
"if",
"position",
">",
"max_value",
":",
"raise",
"ZigguratResourceOutOfBoundaryException",
"(",
"\"Maximum resource ordering is {}\"",
",",
"value",
"=",
"max_value",
")"
] | Checks if node position for given parent is valid, raises exception if
this is not the case
:param parent_id:
:param position:
:param on_same_branch: indicates that we are checking same branch
:param db_session:
:return: | [
"Checks",
"if",
"node",
"position",
"for",
"given",
"parent",
"is",
"valid",
"raises",
"exception",
"if",
"this",
"is",
"not",
"the",
"case"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/resource_tree_postgres.py#L382-L405 |
lapets/egcd | egcd/egcd.py | egcd | def egcd(b, n):
'''
Given two integers (b, n), returns (gcd(b, n), a, m) such that
a*b + n*m = gcd(b, n).
Adapted from several sources:
https://brilliant.org/wiki/extended-euclidean-algorithm/
https://rosettacode.org/wiki/Modular_inverse
https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
https://en.wikipedia.org/wiki/Euclidean_algorithm
>>> egcd(1, 1)
(1, 0, 1)
>>> egcd(12, 8)
(4, 1, -1)
>>> egcd(23894798501898, 23948178468116)
(2, 2437250447493, -2431817869532)
>>> egcd(pow(2, 50), pow(3, 50))
(1, -260414429242905345185687, 408415383037561)
'''
(x0, x1, y0, y1) = (1, 0, 0, 1)
while n != 0:
(q, b, n) = (b // n, n, b % n)
(x0, x1) = (x1, x0 - q * x1)
(y0, y1) = (y1, y0 - q * y1)
return (b, x0, y0) | python | def egcd(b, n):
'''
Given two integers (b, n), returns (gcd(b, n), a, m) such that
a*b + n*m = gcd(b, n).
Adapted from several sources:
https://brilliant.org/wiki/extended-euclidean-algorithm/
https://rosettacode.org/wiki/Modular_inverse
https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
https://en.wikipedia.org/wiki/Euclidean_algorithm
>>> egcd(1, 1)
(1, 0, 1)
>>> egcd(12, 8)
(4, 1, -1)
>>> egcd(23894798501898, 23948178468116)
(2, 2437250447493, -2431817869532)
>>> egcd(pow(2, 50), pow(3, 50))
(1, -260414429242905345185687, 408415383037561)
'''
(x0, x1, y0, y1) = (1, 0, 0, 1)
while n != 0:
(q, b, n) = (b // n, n, b % n)
(x0, x1) = (x1, x0 - q * x1)
(y0, y1) = (y1, y0 - q * y1)
return (b, x0, y0) | [
"def",
"egcd",
"(",
"b",
",",
"n",
")",
":",
"(",
"x0",
",",
"x1",
",",
"y0",
",",
"y1",
")",
"=",
"(",
"1",
",",
"0",
",",
"0",
",",
"1",
")",
"while",
"n",
"!=",
"0",
":",
"(",
"q",
",",
"b",
",",
"n",
")",
"=",
"(",
"b",
"//",
"n",
",",
"n",
",",
"b",
"%",
"n",
")",
"(",
"x0",
",",
"x1",
")",
"=",
"(",
"x1",
",",
"x0",
"-",
"q",
"*",
"x1",
")",
"(",
"y0",
",",
"y1",
")",
"=",
"(",
"y1",
",",
"y0",
"-",
"q",
"*",
"y1",
")",
"return",
"(",
"b",
",",
"x0",
",",
"y0",
")"
] | Given two integers (b, n), returns (gcd(b, n), a, m) such that
a*b + n*m = gcd(b, n).
Adapted from several sources:
https://brilliant.org/wiki/extended-euclidean-algorithm/
https://rosettacode.org/wiki/Modular_inverse
https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
https://en.wikipedia.org/wiki/Euclidean_algorithm
>>> egcd(1, 1)
(1, 0, 1)
>>> egcd(12, 8)
(4, 1, -1)
>>> egcd(23894798501898, 23948178468116)
(2, 2437250447493, -2431817869532)
>>> egcd(pow(2, 50), pow(3, 50))
(1, -260414429242905345185687, 408415383037561) | [
"Given",
"two",
"integers",
"(",
"b",
"n",
")",
"returns",
"(",
"gcd",
"(",
"b",
"n",
")",
"a",
"m",
")",
"such",
"that",
"a",
"*",
"b",
"+",
"n",
"*",
"m",
"=",
"gcd",
"(",
"b",
"n",
")",
".",
"Adapted",
"from",
"several",
"sources",
":",
"https",
":",
"//",
"brilliant",
".",
"org",
"/",
"wiki",
"/",
"extended",
"-",
"euclidean",
"-",
"algorithm",
"/",
"https",
":",
"//",
"rosettacode",
".",
"org",
"/",
"wiki",
"/",
"Modular_inverse",
"https",
":",
"//",
"en",
".",
"wikibooks",
".",
"org",
"/",
"wiki",
"/",
"Algorithm_Implementation",
"/",
"Mathematics",
"/",
"Extended_Euclidean_algorithm",
"https",
":",
"//",
"en",
".",
"wikipedia",
".",
"org",
"/",
"wiki",
"/",
"Euclidean_algorithm",
">>>",
"egcd",
"(",
"1",
"1",
")",
"(",
"1",
"0",
"1",
")",
">>>",
"egcd",
"(",
"12",
"8",
")",
"(",
"4",
"1",
"-",
"1",
")",
">>>",
"egcd",
"(",
"23894798501898",
"23948178468116",
")",
"(",
"2",
"2437250447493",
"-",
"2431817869532",
")",
">>>",
"egcd",
"(",
"pow",
"(",
"2",
"50",
")",
"pow",
"(",
"3",
"50",
"))",
"(",
"1",
"-",
"260414429242905345185687",
"408415383037561",
")"
] | train | https://github.com/lapets/egcd/blob/44fbab4a8eb04ad198526b4f14ddce6dff2bae77/egcd/egcd.py#L16-L42 |
jucacrispim/pylint-mongoengine | pylint_mongoengine/__init__.py | register | def register(linter):
"""Add the needed transformations and supressions.
"""
linter.register_checker(MongoEngineChecker(linter))
add_transform('mongoengine')
add_transform('mongomotor')
suppress_qs_decorator_messages(linter)
suppress_fields_attrs_messages(linter) | python | def register(linter):
"""Add the needed transformations and supressions.
"""
linter.register_checker(MongoEngineChecker(linter))
add_transform('mongoengine')
add_transform('mongomotor')
suppress_qs_decorator_messages(linter)
suppress_fields_attrs_messages(linter) | [
"def",
"register",
"(",
"linter",
")",
":",
"linter",
".",
"register_checker",
"(",
"MongoEngineChecker",
"(",
"linter",
")",
")",
"add_transform",
"(",
"'mongoengine'",
")",
"add_transform",
"(",
"'mongomotor'",
")",
"suppress_qs_decorator_messages",
"(",
"linter",
")",
"suppress_fields_attrs_messages",
"(",
"linter",
")"
] | Add the needed transformations and supressions. | [
"Add",
"the",
"needed",
"transformations",
"and",
"supressions",
"."
] | train | https://github.com/jucacrispim/pylint-mongoengine/blob/b873653d1224a5748f75dd507f492f8c60d95ce3/pylint_mongoengine/__init__.py#L25-L33 |
aguinane/nem-reader | nemreader/outputs.py | output_as_csv | def output_as_csv(file_name, nmi=None, output_file=None):
"""
Transpose all channels and output a csv that is easier
to read and do charting on
:param file_name: The NEM file to process
:param nmi: Which NMI to output if more than one
:param output_file: Specify different output location
:returns: The file that was created
"""
m = read_nem_file(file_name)
if nmi is None:
nmi = list(m.readings.keys())[0] # Use first NMI
channels = list(m.transactions[nmi].keys())
num_records = len(m.readings[nmi][channels[0]])
last_date = m.readings[nmi][channels[0]][-1].t_end
if output_file is None:
output_file = '{}_{}_transposed.csv'.format(
nmi, last_date.strftime('%Y%m%d'))
with open(output_file, 'w', newline='') as csvfile:
cwriter = csv.writer(
csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
heading_list = ['period_start', 'period_end']
for channel in channels:
heading_list.append(channel)
heading_list.append('quality_method')
cwriter.writerow(heading_list)
for i in range(0, num_records):
t_start = m.readings[nmi][channels[0]][i].t_start
t_end = m.readings[nmi][channels[0]][i].t_end
quality_method = m.readings[nmi][channels[0]][i].quality_method
row_list = [t_start, t_end]
for ch in channels:
val = m.readings[nmi][ch][i].read_value
row_list.append(val)
row_list.append(quality_method)
cwriter.writerow(row_list)
return output_file | python | def output_as_csv(file_name, nmi=None, output_file=None):
"""
Transpose all channels and output a csv that is easier
to read and do charting on
:param file_name: The NEM file to process
:param nmi: Which NMI to output if more than one
:param output_file: Specify different output location
:returns: The file that was created
"""
m = read_nem_file(file_name)
if nmi is None:
nmi = list(m.readings.keys())[0] # Use first NMI
channels = list(m.transactions[nmi].keys())
num_records = len(m.readings[nmi][channels[0]])
last_date = m.readings[nmi][channels[0]][-1].t_end
if output_file is None:
output_file = '{}_{}_transposed.csv'.format(
nmi, last_date.strftime('%Y%m%d'))
with open(output_file, 'w', newline='') as csvfile:
cwriter = csv.writer(
csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
heading_list = ['period_start', 'period_end']
for channel in channels:
heading_list.append(channel)
heading_list.append('quality_method')
cwriter.writerow(heading_list)
for i in range(0, num_records):
t_start = m.readings[nmi][channels[0]][i].t_start
t_end = m.readings[nmi][channels[0]][i].t_end
quality_method = m.readings[nmi][channels[0]][i].quality_method
row_list = [t_start, t_end]
for ch in channels:
val = m.readings[nmi][ch][i].read_value
row_list.append(val)
row_list.append(quality_method)
cwriter.writerow(row_list)
return output_file | [
"def",
"output_as_csv",
"(",
"file_name",
",",
"nmi",
"=",
"None",
",",
"output_file",
"=",
"None",
")",
":",
"m",
"=",
"read_nem_file",
"(",
"file_name",
")",
"if",
"nmi",
"is",
"None",
":",
"nmi",
"=",
"list",
"(",
"m",
".",
"readings",
".",
"keys",
"(",
")",
")",
"[",
"0",
"]",
"# Use first NMI",
"channels",
"=",
"list",
"(",
"m",
".",
"transactions",
"[",
"nmi",
"]",
".",
"keys",
"(",
")",
")",
"num_records",
"=",
"len",
"(",
"m",
".",
"readings",
"[",
"nmi",
"]",
"[",
"channels",
"[",
"0",
"]",
"]",
")",
"last_date",
"=",
"m",
".",
"readings",
"[",
"nmi",
"]",
"[",
"channels",
"[",
"0",
"]",
"]",
"[",
"-",
"1",
"]",
".",
"t_end",
"if",
"output_file",
"is",
"None",
":",
"output_file",
"=",
"'{}_{}_transposed.csv'",
".",
"format",
"(",
"nmi",
",",
"last_date",
".",
"strftime",
"(",
"'%Y%m%d'",
")",
")",
"with",
"open",
"(",
"output_file",
",",
"'w'",
",",
"newline",
"=",
"''",
")",
"as",
"csvfile",
":",
"cwriter",
"=",
"csv",
".",
"writer",
"(",
"csvfile",
",",
"delimiter",
"=",
"','",
",",
"quotechar",
"=",
"'\"'",
",",
"quoting",
"=",
"csv",
".",
"QUOTE_MINIMAL",
")",
"heading_list",
"=",
"[",
"'period_start'",
",",
"'period_end'",
"]",
"for",
"channel",
"in",
"channels",
":",
"heading_list",
".",
"append",
"(",
"channel",
")",
"heading_list",
".",
"append",
"(",
"'quality_method'",
")",
"cwriter",
".",
"writerow",
"(",
"heading_list",
")",
"for",
"i",
"in",
"range",
"(",
"0",
",",
"num_records",
")",
":",
"t_start",
"=",
"m",
".",
"readings",
"[",
"nmi",
"]",
"[",
"channels",
"[",
"0",
"]",
"]",
"[",
"i",
"]",
".",
"t_start",
"t_end",
"=",
"m",
".",
"readings",
"[",
"nmi",
"]",
"[",
"channels",
"[",
"0",
"]",
"]",
"[",
"i",
"]",
".",
"t_end",
"quality_method",
"=",
"m",
".",
"readings",
"[",
"nmi",
"]",
"[",
"channels",
"[",
"0",
"]",
"]",
"[",
"i",
"]",
".",
"quality_method",
"row_list",
"=",
"[",
"t_start",
",",
"t_end",
"]",
"for",
"ch",
"in",
"channels",
":",
"val",
"=",
"m",
".",
"readings",
"[",
"nmi",
"]",
"[",
"ch",
"]",
"[",
"i",
"]",
".",
"read_value",
"row_list",
".",
"append",
"(",
"val",
")",
"row_list",
".",
"append",
"(",
"quality_method",
")",
"cwriter",
".",
"writerow",
"(",
"row_list",
")",
"return",
"output_file"
] | Transpose all channels and output a csv that is easier
to read and do charting on
:param file_name: The NEM file to process
:param nmi: Which NMI to output if more than one
:param output_file: Specify different output location
:returns: The file that was created | [
"Transpose",
"all",
"channels",
"and",
"output",
"a",
"csv",
"that",
"is",
"easier",
"to",
"read",
"and",
"do",
"charting",
"on"
] | train | https://github.com/aguinane/nem-reader/blob/5405a5cba4bb8ebdad05c28455d12bb34a6d3ce5/nemreader/outputs.py#L11-L51 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/group.py | GroupService.get | def get(cls, group_id, db_session=None):
"""
Fetch row using primary key -
will use existing object in session if already present
:param group_id:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
return db_session.query(cls.model).get(group_id) | python | def get(cls, group_id, db_session=None):
"""
Fetch row using primary key -
will use existing object in session if already present
:param group_id:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
return db_session.query(cls.model).get(group_id) | [
"def",
"get",
"(",
"cls",
",",
"group_id",
",",
"db_session",
"=",
"None",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"return",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
".",
"get",
"(",
"group_id",
")"
] | Fetch row using primary key -
will use existing object in session if already present
:param group_id:
:param db_session:
:return: | [
"Fetch",
"row",
"using",
"primary",
"key",
"-",
"will",
"use",
"existing",
"object",
"in",
"session",
"if",
"already",
"present"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/group.py#L19-L29 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/group.py | GroupService.by_group_name | def by_group_name(cls, group_name, db_session=None):
"""
fetch group by name
:param group_name:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model).filter(cls.model.group_name == group_name)
return query.first() | python | def by_group_name(cls, group_name, db_session=None):
"""
fetch group by name
:param group_name:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model).filter(cls.model.group_name == group_name)
return query.first() | [
"def",
"by_group_name",
"(",
"cls",
",",
"group_name",
",",
"db_session",
"=",
"None",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"model",
")",
".",
"filter",
"(",
"cls",
".",
"model",
".",
"group_name",
"==",
"group_name",
")",
"return",
"query",
".",
"first",
"(",
")"
] | fetch group by name
:param group_name:
:param db_session:
:return: | [
"fetch",
"group",
"by",
"name"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/group.py#L32-L42 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/group.py | GroupService.get_user_paginator | def get_user_paginator(
cls,
instance,
page=1,
item_count=None,
items_per_page=50,
user_ids=None,
GET_params=None,
):
"""
returns paginator over users belonging to the group
:param instance:
:param page:
:param item_count:
:param items_per_page:
:param user_ids:
:param GET_params:
:return:
"""
if not GET_params:
GET_params = {}
GET_params.pop("page", None)
query = instance.users_dynamic
if user_ids:
query = query.filter(cls.models_proxy.UserGroup.user_id.in_(user_ids))
return SqlalchemyOrmPage(
query,
page=page,
item_count=item_count,
items_per_page=items_per_page,
**GET_params
) | python | def get_user_paginator(
cls,
instance,
page=1,
item_count=None,
items_per_page=50,
user_ids=None,
GET_params=None,
):
"""
returns paginator over users belonging to the group
:param instance:
:param page:
:param item_count:
:param items_per_page:
:param user_ids:
:param GET_params:
:return:
"""
if not GET_params:
GET_params = {}
GET_params.pop("page", None)
query = instance.users_dynamic
if user_ids:
query = query.filter(cls.models_proxy.UserGroup.user_id.in_(user_ids))
return SqlalchemyOrmPage(
query,
page=page,
item_count=item_count,
items_per_page=items_per_page,
**GET_params
) | [
"def",
"get_user_paginator",
"(",
"cls",
",",
"instance",
",",
"page",
"=",
"1",
",",
"item_count",
"=",
"None",
",",
"items_per_page",
"=",
"50",
",",
"user_ids",
"=",
"None",
",",
"GET_params",
"=",
"None",
",",
")",
":",
"if",
"not",
"GET_params",
":",
"GET_params",
"=",
"{",
"}",
"GET_params",
".",
"pop",
"(",
"\"page\"",
",",
"None",
")",
"query",
"=",
"instance",
".",
"users_dynamic",
"if",
"user_ids",
":",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"UserGroup",
".",
"user_id",
".",
"in_",
"(",
"user_ids",
")",
")",
"return",
"SqlalchemyOrmPage",
"(",
"query",
",",
"page",
"=",
"page",
",",
"item_count",
"=",
"item_count",
",",
"items_per_page",
"=",
"items_per_page",
",",
"*",
"*",
"GET_params",
")"
] | returns paginator over users belonging to the group
:param instance:
:param page:
:param item_count:
:param items_per_page:
:param user_ids:
:param GET_params:
:return: | [
"returns",
"paginator",
"over",
"users",
"belonging",
"to",
"the",
"group"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/group.py#L45-L77 |
ergo/ziggurat_foundations | ziggurat_foundations/models/services/group.py | GroupService.resources_with_possible_perms | def resources_with_possible_perms(
cls,
instance,
perm_names=None,
resource_ids=None,
resource_types=None,
db_session=None,
):
"""
returns list of permissions and resources for this group,
resource_ids restricts the search to specific resources
:param instance:
:param perm_names:
:param resource_ids:
:param resource_types:
:param db_session:
:return:
"""
db_session = get_db_session(db_session, instance)
query = db_session.query(
cls.models_proxy.GroupResourcePermission.perm_name,
cls.models_proxy.Group,
cls.models_proxy.Resource,
)
query = query.filter(
cls.models_proxy.Resource.resource_id
== cls.models_proxy.GroupResourcePermission.resource_id
)
query = query.filter(
cls.models_proxy.Group.id
== cls.models_proxy.GroupResourcePermission.group_id
)
if resource_ids:
query = query.filter(
cls.models_proxy.GroupResourcePermission.resource_id.in_(resource_ids)
)
if resource_types:
query = query.filter(
cls.models_proxy.Resource.resource_type.in_(resource_types)
)
if perm_names not in ([ANY_PERMISSION], ANY_PERMISSION) and perm_names:
query = query.filter(
cls.models_proxy.GroupResourcePermission.perm_name.in_(perm_names)
)
query = query.filter(
cls.models_proxy.GroupResourcePermission.group_id == instance.id
)
perms = [
PermissionTuple(
None, row.perm_name, "group", instance, row.Resource, False, True
)
for row in query
]
for resource in instance.resources:
perms.append(
PermissionTuple(
None, ALL_PERMISSIONS, "group", instance, resource, True, True
)
)
return perms | python | def resources_with_possible_perms(
cls,
instance,
perm_names=None,
resource_ids=None,
resource_types=None,
db_session=None,
):
"""
returns list of permissions and resources for this group,
resource_ids restricts the search to specific resources
:param instance:
:param perm_names:
:param resource_ids:
:param resource_types:
:param db_session:
:return:
"""
db_session = get_db_session(db_session, instance)
query = db_session.query(
cls.models_proxy.GroupResourcePermission.perm_name,
cls.models_proxy.Group,
cls.models_proxy.Resource,
)
query = query.filter(
cls.models_proxy.Resource.resource_id
== cls.models_proxy.GroupResourcePermission.resource_id
)
query = query.filter(
cls.models_proxy.Group.id
== cls.models_proxy.GroupResourcePermission.group_id
)
if resource_ids:
query = query.filter(
cls.models_proxy.GroupResourcePermission.resource_id.in_(resource_ids)
)
if resource_types:
query = query.filter(
cls.models_proxy.Resource.resource_type.in_(resource_types)
)
if perm_names not in ([ANY_PERMISSION], ANY_PERMISSION) and perm_names:
query = query.filter(
cls.models_proxy.GroupResourcePermission.perm_name.in_(perm_names)
)
query = query.filter(
cls.models_proxy.GroupResourcePermission.group_id == instance.id
)
perms = [
PermissionTuple(
None, row.perm_name, "group", instance, row.Resource, False, True
)
for row in query
]
for resource in instance.resources:
perms.append(
PermissionTuple(
None, ALL_PERMISSIONS, "group", instance, resource, True, True
)
)
return perms | [
"def",
"resources_with_possible_perms",
"(",
"cls",
",",
"instance",
",",
"perm_names",
"=",
"None",
",",
"resource_ids",
"=",
"None",
",",
"resource_types",
"=",
"None",
",",
"db_session",
"=",
"None",
",",
")",
":",
"db_session",
"=",
"get_db_session",
"(",
"db_session",
",",
"instance",
")",
"query",
"=",
"db_session",
".",
"query",
"(",
"cls",
".",
"models_proxy",
".",
"GroupResourcePermission",
".",
"perm_name",
",",
"cls",
".",
"models_proxy",
".",
"Group",
",",
"cls",
".",
"models_proxy",
".",
"Resource",
",",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"resource_id",
"==",
"cls",
".",
"models_proxy",
".",
"GroupResourcePermission",
".",
"resource_id",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"Group",
".",
"id",
"==",
"cls",
".",
"models_proxy",
".",
"GroupResourcePermission",
".",
"group_id",
")",
"if",
"resource_ids",
":",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"GroupResourcePermission",
".",
"resource_id",
".",
"in_",
"(",
"resource_ids",
")",
")",
"if",
"resource_types",
":",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"Resource",
".",
"resource_type",
".",
"in_",
"(",
"resource_types",
")",
")",
"if",
"perm_names",
"not",
"in",
"(",
"[",
"ANY_PERMISSION",
"]",
",",
"ANY_PERMISSION",
")",
"and",
"perm_names",
":",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"GroupResourcePermission",
".",
"perm_name",
".",
"in_",
"(",
"perm_names",
")",
")",
"query",
"=",
"query",
".",
"filter",
"(",
"cls",
".",
"models_proxy",
".",
"GroupResourcePermission",
".",
"group_id",
"==",
"instance",
".",
"id",
")",
"perms",
"=",
"[",
"PermissionTuple",
"(",
"None",
",",
"row",
".",
"perm_name",
",",
"\"group\"",
",",
"instance",
",",
"row",
".",
"Resource",
",",
"False",
",",
"True",
")",
"for",
"row",
"in",
"query",
"]",
"for",
"resource",
"in",
"instance",
".",
"resources",
":",
"perms",
".",
"append",
"(",
"PermissionTuple",
"(",
"None",
",",
"ALL_PERMISSIONS",
",",
"\"group\"",
",",
"instance",
",",
"resource",
",",
"True",
",",
"True",
")",
")",
"return",
"perms"
] | returns list of permissions and resources for this group,
resource_ids restricts the search to specific resources
:param instance:
:param perm_names:
:param resource_ids:
:param resource_types:
:param db_session:
:return: | [
"returns",
"list",
"of",
"permissions",
"and",
"resources",
"for",
"this",
"group",
"resource_ids",
"restricts",
"the",
"search",
"to",
"specific",
"resources"
] | train | https://github.com/ergo/ziggurat_foundations/blob/9eeec894d08e8d7defa60ddc04b63f69cd4cbeba/ziggurat_foundations/models/services/group.py#L80-L144 |
zeromake/aiosqlite3 | aiosqlite3/pool.py | create_pool | def create_pool(
database,
minsize=1,
maxsize=10,
echo=False,
loop=None,
**kwargs
):
"""
创建支持上下文管理的pool
"""
coro = _create_pool(
database=database,
minsize=minsize,
maxsize=maxsize,
echo=echo,
loop=loop,
**kwargs
)
return _PoolContextManager(coro) | python | def create_pool(
database,
minsize=1,
maxsize=10,
echo=False,
loop=None,
**kwargs
):
"""
创建支持上下文管理的pool
"""
coro = _create_pool(
database=database,
minsize=minsize,
maxsize=maxsize,
echo=echo,
loop=loop,
**kwargs
)
return _PoolContextManager(coro) | [
"def",
"create_pool",
"(",
"database",
",",
"minsize",
"=",
"1",
",",
"maxsize",
"=",
"10",
",",
"echo",
"=",
"False",
",",
"loop",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"coro",
"=",
"_create_pool",
"(",
"database",
"=",
"database",
",",
"minsize",
"=",
"minsize",
",",
"maxsize",
"=",
"maxsize",
",",
"echo",
"=",
"echo",
",",
"loop",
"=",
"loop",
",",
"*",
"*",
"kwargs",
")",
"return",
"_PoolContextManager",
"(",
"coro",
")"
] | 创建支持上下文管理的pool | [
"创建支持上下文管理的pool"
] | train | https://github.com/zeromake/aiosqlite3/blob/1a74a062507e2df8f833a70885e69dca0ab3e7e7/aiosqlite3/pool.py#L14-L33 |
zeromake/aiosqlite3 | aiosqlite3/pool.py | Pool.wait_closed | def wait_closed(self):
"""
Wait for closing all pool's connections.
"""
if self._closed:
return
if not self._closing:
raise RuntimeError(
".wait_closed() should be called "
"after .close()"
)
while self._free:
conn = self._free.popleft()
if not conn.closed:
yield from conn.close()
else:
# pragma: no cover
pass
with (yield from self._cond):
while self.size > self.freesize:
yield from self._cond.wait()
self._used.clear()
self._closed = True | python | def wait_closed(self):
"""
Wait for closing all pool's connections.
"""
if self._closed:
return
if not self._closing:
raise RuntimeError(
".wait_closed() should be called "
"after .close()"
)
while self._free:
conn = self._free.popleft()
if not conn.closed:
yield from conn.close()
else:
# pragma: no cover
pass
with (yield from self._cond):
while self.size > self.freesize:
yield from self._cond.wait()
self._used.clear()
self._closed = True | [
"def",
"wait_closed",
"(",
"self",
")",
":",
"if",
"self",
".",
"_closed",
":",
"return",
"if",
"not",
"self",
".",
"_closing",
":",
"raise",
"RuntimeError",
"(",
"\".wait_closed() should be called \"",
"\"after .close()\"",
")",
"while",
"self",
".",
"_free",
":",
"conn",
"=",
"self",
".",
"_free",
".",
"popleft",
"(",
")",
"if",
"not",
"conn",
".",
"closed",
":",
"yield",
"from",
"conn",
".",
"close",
"(",
")",
"else",
":",
"# pragma: no cover",
"pass",
"with",
"(",
"yield",
"from",
"self",
".",
"_cond",
")",
":",
"while",
"self",
".",
"size",
">",
"self",
".",
"freesize",
":",
"yield",
"from",
"self",
".",
"_cond",
".",
"wait",
"(",
")",
"self",
".",
"_used",
".",
"clear",
"(",
")",
"self",
".",
"_closed",
"=",
"True"
] | Wait for closing all pool's connections. | [
"Wait",
"for",
"closing",
"all",
"pool",
"s",
"connections",
"."
] | train | https://github.com/zeromake/aiosqlite3/blob/1a74a062507e2df8f833a70885e69dca0ab3e7e7/aiosqlite3/pool.py#L169-L192 |
zeromake/aiosqlite3 | aiosqlite3/pool.py | Pool.sync_close | def sync_close(self):
"""
同步关闭
"""
if self._closed:
return
while self._free:
conn = self._free.popleft()
if not conn.closed:
# pragma: no cover
conn.sync_close()
for conn in self._used:
if not conn.closed:
# pragma: no cover
conn.sync_close()
self._terminated.add(conn)
self._used.clear()
self._closed = True | python | def sync_close(self):
"""
同步关闭
"""
if self._closed:
return
while self._free:
conn = self._free.popleft()
if not conn.closed:
# pragma: no cover
conn.sync_close()
for conn in self._used:
if not conn.closed:
# pragma: no cover
conn.sync_close()
self._terminated.add(conn)
self._used.clear()
self._closed = True | [
"def",
"sync_close",
"(",
"self",
")",
":",
"if",
"self",
".",
"_closed",
":",
"return",
"while",
"self",
".",
"_free",
":",
"conn",
"=",
"self",
".",
"_free",
".",
"popleft",
"(",
")",
"if",
"not",
"conn",
".",
"closed",
":",
"# pragma: no cover",
"conn",
".",
"sync_close",
"(",
")",
"for",
"conn",
"in",
"self",
".",
"_used",
":",
"if",
"not",
"conn",
".",
"closed",
":",
"# pragma: no cover",
"conn",
".",
"sync_close",
"(",
")",
"self",
".",
"_terminated",
".",
"add",
"(",
"conn",
")",
"self",
".",
"_used",
".",
"clear",
"(",
")",
"self",
".",
"_closed",
"=",
"True"
] | 同步关闭 | [
"同步关闭"
] | train | https://github.com/zeromake/aiosqlite3/blob/1a74a062507e2df8f833a70885e69dca0ab3e7e7/aiosqlite3/pool.py#L194-L211 |
zeromake/aiosqlite3 | aiosqlite3/pool.py | Pool._fill_free_pool | def _fill_free_pool(self, override_min):
"""
iterate over free connections and remove timeouted ones
"""
while self.size < self.minsize:
self._acquiring += 1
try:
conn = yield from connect(
database=self._database,
echo=self._echo,
loop=self._loop,
**self._conn_kwargs
)
self._free.append(conn)
self._cond.notify()
finally:
self._acquiring -= 1
if self._free:
return
if override_min and self.size < self.maxsize:
self._acquiring += 1
try:
conn = yield from connect(
database=self._database,
echo=self._echo,
loop=self._loop,
**self._conn_kwargs
)
self._free.append(conn)
self._cond.notify()
finally:
self._acquiring -= 1 | python | def _fill_free_pool(self, override_min):
"""
iterate over free connections and remove timeouted ones
"""
while self.size < self.minsize:
self._acquiring += 1
try:
conn = yield from connect(
database=self._database,
echo=self._echo,
loop=self._loop,
**self._conn_kwargs
)
self._free.append(conn)
self._cond.notify()
finally:
self._acquiring -= 1
if self._free:
return
if override_min and self.size < self.maxsize:
self._acquiring += 1
try:
conn = yield from connect(
database=self._database,
echo=self._echo,
loop=self._loop,
**self._conn_kwargs
)
self._free.append(conn)
self._cond.notify()
finally:
self._acquiring -= 1 | [
"def",
"_fill_free_pool",
"(",
"self",
",",
"override_min",
")",
":",
"while",
"self",
".",
"size",
"<",
"self",
".",
"minsize",
":",
"self",
".",
"_acquiring",
"+=",
"1",
"try",
":",
"conn",
"=",
"yield",
"from",
"connect",
"(",
"database",
"=",
"self",
".",
"_database",
",",
"echo",
"=",
"self",
".",
"_echo",
",",
"loop",
"=",
"self",
".",
"_loop",
",",
"*",
"*",
"self",
".",
"_conn_kwargs",
")",
"self",
".",
"_free",
".",
"append",
"(",
"conn",
")",
"self",
".",
"_cond",
".",
"notify",
"(",
")",
"finally",
":",
"self",
".",
"_acquiring",
"-=",
"1",
"if",
"self",
".",
"_free",
":",
"return",
"if",
"override_min",
"and",
"self",
".",
"size",
"<",
"self",
".",
"maxsize",
":",
"self",
".",
"_acquiring",
"+=",
"1",
"try",
":",
"conn",
"=",
"yield",
"from",
"connect",
"(",
"database",
"=",
"self",
".",
"_database",
",",
"echo",
"=",
"self",
".",
"_echo",
",",
"loop",
"=",
"self",
".",
"_loop",
",",
"*",
"*",
"self",
".",
"_conn_kwargs",
")",
"self",
".",
"_free",
".",
"append",
"(",
"conn",
")",
"self",
".",
"_cond",
".",
"notify",
"(",
")",
"finally",
":",
"self",
".",
"_acquiring",
"-=",
"1"
] | iterate over free connections and remove timeouted ones | [
"iterate",
"over",
"free",
"connections",
"and",
"remove",
"timeouted",
"ones"
] | train | https://github.com/zeromake/aiosqlite3/blob/1a74a062507e2df8f833a70885e69dca0ab3e7e7/aiosqlite3/pool.py#L242-L274 |
stepank/pyws | src/pyws/functions/managers.py | FixedFunctionManager.add_function | def add_function(self, function):
"""
Adds the function to the list of registered functions.
"""
function = self.build_function(function)
if function.name in self.functions:
raise FunctionAlreadyRegistered(function.name)
self.functions[function.name] = function | python | def add_function(self, function):
"""
Adds the function to the list of registered functions.
"""
function = self.build_function(function)
if function.name in self.functions:
raise FunctionAlreadyRegistered(function.name)
self.functions[function.name] = function | [
"def",
"add_function",
"(",
"self",
",",
"function",
")",
":",
"function",
"=",
"self",
".",
"build_function",
"(",
"function",
")",
"if",
"function",
".",
"name",
"in",
"self",
".",
"functions",
":",
"raise",
"FunctionAlreadyRegistered",
"(",
"function",
".",
"name",
")",
"self",
".",
"functions",
"[",
"function",
".",
"name",
"]",
"=",
"function"
] | Adds the function to the list of registered functions. | [
"Adds",
"the",
"function",
"to",
"the",
"list",
"of",
"registered",
"functions",
"."
] | train | https://github.com/stepank/pyws/blob/ff39133aabeb56bbb08d66286ac0cc8731eda7dd/src/pyws/functions/managers.py#L44-L51 |
stepank/pyws | src/pyws/functions/managers.py | FixedFunctionManager.get_one | def get_one(self, context, name):
"""
Returns a function if it is registered, the context is ignored.
"""
try:
return self.functions[name]
except KeyError:
raise FunctionNotFound(name) | python | def get_one(self, context, name):
"""
Returns a function if it is registered, the context is ignored.
"""
try:
return self.functions[name]
except KeyError:
raise FunctionNotFound(name) | [
"def",
"get_one",
"(",
"self",
",",
"context",
",",
"name",
")",
":",
"try",
":",
"return",
"self",
".",
"functions",
"[",
"name",
"]",
"except",
"KeyError",
":",
"raise",
"FunctionNotFound",
"(",
"name",
")"
] | Returns a function if it is registered, the context is ignored. | [
"Returns",
"a",
"function",
"if",
"it",
"is",
"registered",
"the",
"context",
"is",
"ignored",
"."
] | train | https://github.com/stepank/pyws/blob/ff39133aabeb56bbb08d66286ac0cc8731eda7dd/src/pyws/functions/managers.py#L53-L60 |
opennode/waldur-core | waldur_core/core/monkeypatch.py | subfield_get | def subfield_get(self, obj, type=None):
"""
Verbatim copy from:
https://github.com/django/django/blob/1.9.13/django/db/models/fields/subclassing.py#L38
"""
if obj is None:
return self
return obj.__dict__[self.field.name] | python | def subfield_get(self, obj, type=None):
"""
Verbatim copy from:
https://github.com/django/django/blob/1.9.13/django/db/models/fields/subclassing.py#L38
"""
if obj is None:
return self
return obj.__dict__[self.field.name] | [
"def",
"subfield_get",
"(",
"self",
",",
"obj",
",",
"type",
"=",
"None",
")",
":",
"if",
"obj",
"is",
"None",
":",
"return",
"self",
"return",
"obj",
".",
"__dict__",
"[",
"self",
".",
"field",
".",
"name",
"]"
] | Verbatim copy from:
https://github.com/django/django/blob/1.9.13/django/db/models/fields/subclassing.py#L38 | [
"Verbatim",
"copy",
"from",
":",
"https",
":",
"//",
"github",
".",
"com",
"/",
"django",
"/",
"django",
"/",
"blob",
"/",
"1",
".",
"9",
".",
"13",
"/",
"django",
"/",
"db",
"/",
"models",
"/",
"fields",
"/",
"subclassing",
".",
"py#L38"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/monkeypatch.py#L14-L21 |
opennode/waldur-core | waldur_core/core/managers.py | GenericKeyMixin._preprocess_kwargs | def _preprocess_kwargs(self, initial_kwargs):
""" Replace generic key related attribute with filters by object_id and content_type fields """
kwargs = initial_kwargs.copy()
generic_key_related_kwargs = self._get_generic_key_related_kwargs(initial_kwargs)
for key, value in generic_key_related_kwargs.items():
# delete old kwarg that was related to generic key
del kwargs[key]
try:
suffix = key.split('__')[1]
except IndexError:
suffix = None
# add new kwargs that related to object_id and content_type fields
new_kwargs = self._get_filter_object_id_and_content_type_filter_kwargs(value, suffix)
kwargs.update(new_kwargs)
return kwargs | python | def _preprocess_kwargs(self, initial_kwargs):
""" Replace generic key related attribute with filters by object_id and content_type fields """
kwargs = initial_kwargs.copy()
generic_key_related_kwargs = self._get_generic_key_related_kwargs(initial_kwargs)
for key, value in generic_key_related_kwargs.items():
# delete old kwarg that was related to generic key
del kwargs[key]
try:
suffix = key.split('__')[1]
except IndexError:
suffix = None
# add new kwargs that related to object_id and content_type fields
new_kwargs = self._get_filter_object_id_and_content_type_filter_kwargs(value, suffix)
kwargs.update(new_kwargs)
return kwargs | [
"def",
"_preprocess_kwargs",
"(",
"self",
",",
"initial_kwargs",
")",
":",
"kwargs",
"=",
"initial_kwargs",
".",
"copy",
"(",
")",
"generic_key_related_kwargs",
"=",
"self",
".",
"_get_generic_key_related_kwargs",
"(",
"initial_kwargs",
")",
"for",
"key",
",",
"value",
"in",
"generic_key_related_kwargs",
".",
"items",
"(",
")",
":",
"# delete old kwarg that was related to generic key",
"del",
"kwargs",
"[",
"key",
"]",
"try",
":",
"suffix",
"=",
"key",
".",
"split",
"(",
"'__'",
")",
"[",
"1",
"]",
"except",
"IndexError",
":",
"suffix",
"=",
"None",
"# add new kwargs that related to object_id and content_type fields",
"new_kwargs",
"=",
"self",
".",
"_get_filter_object_id_and_content_type_filter_kwargs",
"(",
"value",
",",
"suffix",
")",
"kwargs",
".",
"update",
"(",
"new_kwargs",
")",
"return",
"kwargs"
] | Replace generic key related attribute with filters by object_id and content_type fields | [
"Replace",
"generic",
"key",
"related",
"attribute",
"with",
"filters",
"by",
"object_id",
"and",
"content_type",
"fields"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/managers.py#L29-L44 |
OpenDataScienceLab/skdata | skdata/cleaning.py | categorize | def categorize(
data, col_name: str = None, new_col_name: str = None,
categories: dict = None, max_categories: float = 0.15
):
"""
:param data:
:param col_name:
:param new_col_name:
:param categories:
:param max_categories: max proportion threshold of categories
:return: new categories
:rtype dict:
"""
_categories = {}
if col_name is None:
if categories is not None:
raise Exception(
'col_name is None when categories was defined.'
)
# create a list of cols with all object columns
cols = [
k for k in data.keys()
if data[k].dtype == 'object' and
(data[k].unique() / data[k].count()) <= max_categories
]
else:
# create a list with col_name
if new_col_name is not None:
data[new_col_name] = data[col_name]
col_name = new_col_name
cols = [col_name]
for c in cols:
if categories is not None:
# assert all keys is a number
assert all(type(k) in (int, float) for k in categories.keys())
# replace values using given categories dict
data[c].replace(categories, inplace=True)
# change column to categorical type
data[c] = data[c].astype('category')
# update categories information
_categories.update({c: categories})
else:
# change column to categorical type
data[c] = data[c].astype('category')
# change column to categorical type
_categories.update({
c: dict(enumerate(
data[c].cat.categories,
))
})
return _categories | python | def categorize(
data, col_name: str = None, new_col_name: str = None,
categories: dict = None, max_categories: float = 0.15
):
"""
:param data:
:param col_name:
:param new_col_name:
:param categories:
:param max_categories: max proportion threshold of categories
:return: new categories
:rtype dict:
"""
_categories = {}
if col_name is None:
if categories is not None:
raise Exception(
'col_name is None when categories was defined.'
)
# create a list of cols with all object columns
cols = [
k for k in data.keys()
if data[k].dtype == 'object' and
(data[k].unique() / data[k].count()) <= max_categories
]
else:
# create a list with col_name
if new_col_name is not None:
data[new_col_name] = data[col_name]
col_name = new_col_name
cols = [col_name]
for c in cols:
if categories is not None:
# assert all keys is a number
assert all(type(k) in (int, float) for k in categories.keys())
# replace values using given categories dict
data[c].replace(categories, inplace=True)
# change column to categorical type
data[c] = data[c].astype('category')
# update categories information
_categories.update({c: categories})
else:
# change column to categorical type
data[c] = data[c].astype('category')
# change column to categorical type
_categories.update({
c: dict(enumerate(
data[c].cat.categories,
))
})
return _categories | [
"def",
"categorize",
"(",
"data",
",",
"col_name",
":",
"str",
"=",
"None",
",",
"new_col_name",
":",
"str",
"=",
"None",
",",
"categories",
":",
"dict",
"=",
"None",
",",
"max_categories",
":",
"float",
"=",
"0.15",
")",
":",
"_categories",
"=",
"{",
"}",
"if",
"col_name",
"is",
"None",
":",
"if",
"categories",
"is",
"not",
"None",
":",
"raise",
"Exception",
"(",
"'col_name is None when categories was defined.'",
")",
"# create a list of cols with all object columns",
"cols",
"=",
"[",
"k",
"for",
"k",
"in",
"data",
".",
"keys",
"(",
")",
"if",
"data",
"[",
"k",
"]",
".",
"dtype",
"==",
"'object'",
"and",
"(",
"data",
"[",
"k",
"]",
".",
"unique",
"(",
")",
"/",
"data",
"[",
"k",
"]",
".",
"count",
"(",
")",
")",
"<=",
"max_categories",
"]",
"else",
":",
"# create a list with col_name",
"if",
"new_col_name",
"is",
"not",
"None",
":",
"data",
"[",
"new_col_name",
"]",
"=",
"data",
"[",
"col_name",
"]",
"col_name",
"=",
"new_col_name",
"cols",
"=",
"[",
"col_name",
"]",
"for",
"c",
"in",
"cols",
":",
"if",
"categories",
"is",
"not",
"None",
":",
"# assert all keys is a number",
"assert",
"all",
"(",
"type",
"(",
"k",
")",
"in",
"(",
"int",
",",
"float",
")",
"for",
"k",
"in",
"categories",
".",
"keys",
"(",
")",
")",
"# replace values using given categories dict",
"data",
"[",
"c",
"]",
".",
"replace",
"(",
"categories",
",",
"inplace",
"=",
"True",
")",
"# change column to categorical type",
"data",
"[",
"c",
"]",
"=",
"data",
"[",
"c",
"]",
".",
"astype",
"(",
"'category'",
")",
"# update categories information",
"_categories",
".",
"update",
"(",
"{",
"c",
":",
"categories",
"}",
")",
"else",
":",
"# change column to categorical type",
"data",
"[",
"c",
"]",
"=",
"data",
"[",
"c",
"]",
".",
"astype",
"(",
"'category'",
")",
"# change column to categorical type",
"_categories",
".",
"update",
"(",
"{",
"c",
":",
"dict",
"(",
"enumerate",
"(",
"data",
"[",
"c",
"]",
".",
"cat",
".",
"categories",
",",
")",
")",
"}",
")",
"return",
"_categories"
] | :param data:
:param col_name:
:param new_col_name:
:param categories:
:param max_categories: max proportion threshold of categories
:return: new categories
:rtype dict: | [
":",
"param",
"data",
":",
":",
"param",
"col_name",
":",
":",
"param",
"new_col_name",
":",
":",
"param",
"categories",
":",
":",
"param",
"max_categories",
":",
"max",
"proportion",
"threshold",
"of",
"categories",
":",
"return",
":",
"new",
"categories",
":",
"rtype",
"dict",
":"
] | train | https://github.com/OpenDataScienceLab/skdata/blob/34f06845a944ff4f048b55c7babdd8420f71a6b9/skdata/cleaning.py#L4-L57 |
OpenDataScienceLab/skdata | skdata/cleaning.py | dropna | def dropna(data: pd.DataFrame, axis: int, **params):
"""
Remove columns with more NA values than threshold level
:param data:
:param axis:
Axes are defined for arrays with more than one dimension.
A 2-dimensional array has two corresponding axes: the first running
vertically downwards across rows (axis 0), and the second running
horizontally across columns (axis 1).
(https://docs.scipy.org/doc/numpy-1.10.0/glossary.html)
:param params:
:return:
"""
if axis == 0:
dropna_rows(data=data, **params)
else:
dropna_columns(data=data, **params) | python | def dropna(data: pd.DataFrame, axis: int, **params):
"""
Remove columns with more NA values than threshold level
:param data:
:param axis:
Axes are defined for arrays with more than one dimension.
A 2-dimensional array has two corresponding axes: the first running
vertically downwards across rows (axis 0), and the second running
horizontally across columns (axis 1).
(https://docs.scipy.org/doc/numpy-1.10.0/glossary.html)
:param params:
:return:
"""
if axis == 0:
dropna_rows(data=data, **params)
else:
dropna_columns(data=data, **params) | [
"def",
"dropna",
"(",
"data",
":",
"pd",
".",
"DataFrame",
",",
"axis",
":",
"int",
",",
"*",
"*",
"params",
")",
":",
"if",
"axis",
"==",
"0",
":",
"dropna_rows",
"(",
"data",
"=",
"data",
",",
"*",
"*",
"params",
")",
"else",
":",
"dropna_columns",
"(",
"data",
"=",
"data",
",",
"*",
"*",
"params",
")"
] | Remove columns with more NA values than threshold level
:param data:
:param axis:
Axes are defined for arrays with more than one dimension.
A 2-dimensional array has two corresponding axes: the first running
vertically downwards across rows (axis 0), and the second running
horizontally across columns (axis 1).
(https://docs.scipy.org/doc/numpy-1.10.0/glossary.html)
:param params:
:return: | [
"Remove",
"columns",
"with",
"more",
"NA",
"values",
"than",
"threshold",
"level"
] | train | https://github.com/OpenDataScienceLab/skdata/blob/34f06845a944ff4f048b55c7babdd8420f71a6b9/skdata/cleaning.py#L60-L78 |
OpenDataScienceLab/skdata | skdata/cleaning.py | dropna_columns | def dropna_columns(data: pd.DataFrame, max_na_values: int=0.15):
"""
Remove columns with more NA values than threshold level
:param data:
:param max_na_values: proportion threshold of max na values
:return:
"""
size = data.shape[0]
df_na = (data.isnull().sum()/size) >= max_na_values
data.drop(df_na[df_na].index, axis=1, inplace=True) | python | def dropna_columns(data: pd.DataFrame, max_na_values: int=0.15):
"""
Remove columns with more NA values than threshold level
:param data:
:param max_na_values: proportion threshold of max na values
:return:
"""
size = data.shape[0]
df_na = (data.isnull().sum()/size) >= max_na_values
data.drop(df_na[df_na].index, axis=1, inplace=True) | [
"def",
"dropna_columns",
"(",
"data",
":",
"pd",
".",
"DataFrame",
",",
"max_na_values",
":",
"int",
"=",
"0.15",
")",
":",
"size",
"=",
"data",
".",
"shape",
"[",
"0",
"]",
"df_na",
"=",
"(",
"data",
".",
"isnull",
"(",
")",
".",
"sum",
"(",
")",
"/",
"size",
")",
">=",
"max_na_values",
"data",
".",
"drop",
"(",
"df_na",
"[",
"df_na",
"]",
".",
"index",
",",
"axis",
"=",
"1",
",",
"inplace",
"=",
"True",
")"
] | Remove columns with more NA values than threshold level
:param data:
:param max_na_values: proportion threshold of max na values
:return: | [
"Remove",
"columns",
"with",
"more",
"NA",
"values",
"than",
"threshold",
"level"
] | train | https://github.com/OpenDataScienceLab/skdata/blob/34f06845a944ff4f048b55c7babdd8420f71a6b9/skdata/cleaning.py#L81-L92 |
OpenDataScienceLab/skdata | skdata/cleaning.py | dropna_rows | def dropna_rows(data: pd.DataFrame, columns_name: str=None):
"""
Remove columns with more NA values than threshold level
:param data:
:param columns_name:
:return:
"""
params = {}
if columns_name is not None:
params.update({'subset': columns_name.split(',')})
data.dropna(inplace=True, **params) | python | def dropna_rows(data: pd.DataFrame, columns_name: str=None):
"""
Remove columns with more NA values than threshold level
:param data:
:param columns_name:
:return:
"""
params = {}
if columns_name is not None:
params.update({'subset': columns_name.split(',')})
data.dropna(inplace=True, **params) | [
"def",
"dropna_rows",
"(",
"data",
":",
"pd",
".",
"DataFrame",
",",
"columns_name",
":",
"str",
"=",
"None",
")",
":",
"params",
"=",
"{",
"}",
"if",
"columns_name",
"is",
"not",
"None",
":",
"params",
".",
"update",
"(",
"{",
"'subset'",
":",
"columns_name",
".",
"split",
"(",
"','",
")",
"}",
")",
"data",
".",
"dropna",
"(",
"inplace",
"=",
"True",
",",
"*",
"*",
"params",
")"
] | Remove columns with more NA values than threshold level
:param data:
:param columns_name:
:return: | [
"Remove",
"columns",
"with",
"more",
"NA",
"values",
"than",
"threshold",
"level"
] | train | https://github.com/OpenDataScienceLab/skdata/blob/34f06845a944ff4f048b55c7babdd8420f71a6b9/skdata/cleaning.py#L95-L107 |
OpenDataScienceLab/skdata | skdata/cleaning.py | drop_columns_with_unique_values | def drop_columns_with_unique_values(
data: pd.DataFrame, max_unique_values: int = 0.25
):
"""
Remove columns when the proportion
of the total of unique values is more than the max_unique_values
threshold, just for columns with type as object or category
:param data:
:param max_unique_values:
:return:
"""
size = data.shape[0]
df_uv = data.apply(
lambda se: (
(se.dropna().unique().shape[0]/size) > max_unique_values and
se.dtype in ['object', 'category']
)
)
data.drop(df_uv[df_uv].index, axis=1, inplace=True) | python | def drop_columns_with_unique_values(
data: pd.DataFrame, max_unique_values: int = 0.25
):
"""
Remove columns when the proportion
of the total of unique values is more than the max_unique_values
threshold, just for columns with type as object or category
:param data:
:param max_unique_values:
:return:
"""
size = data.shape[0]
df_uv = data.apply(
lambda se: (
(se.dropna().unique().shape[0]/size) > max_unique_values and
se.dtype in ['object', 'category']
)
)
data.drop(df_uv[df_uv].index, axis=1, inplace=True) | [
"def",
"drop_columns_with_unique_values",
"(",
"data",
":",
"pd",
".",
"DataFrame",
",",
"max_unique_values",
":",
"int",
"=",
"0.25",
")",
":",
"size",
"=",
"data",
".",
"shape",
"[",
"0",
"]",
"df_uv",
"=",
"data",
".",
"apply",
"(",
"lambda",
"se",
":",
"(",
"(",
"se",
".",
"dropna",
"(",
")",
".",
"unique",
"(",
")",
".",
"shape",
"[",
"0",
"]",
"/",
"size",
")",
">",
"max_unique_values",
"and",
"se",
".",
"dtype",
"in",
"[",
"'object'",
",",
"'category'",
"]",
")",
")",
"data",
".",
"drop",
"(",
"df_uv",
"[",
"df_uv",
"]",
".",
"index",
",",
"axis",
"=",
"1",
",",
"inplace",
"=",
"True",
")"
] | Remove columns when the proportion
of the total of unique values is more than the max_unique_values
threshold, just for columns with type as object or category
:param data:
:param max_unique_values:
:return: | [
"Remove",
"columns",
"when",
"the",
"proportion",
"of",
"the",
"total",
"of",
"unique",
"values",
"is",
"more",
"than",
"the",
"max_unique_values",
"threshold",
"just",
"for",
"columns",
"with",
"type",
"as",
"object",
"or",
"category"
] | train | https://github.com/OpenDataScienceLab/skdata/blob/34f06845a944ff4f048b55c7babdd8420f71a6b9/skdata/cleaning.py#L110-L130 |
opennode/waldur-core | waldur_core/quotas/views.py | QuotaViewSet.list | def list(self, request, *args, **kwargs):
"""
To get an actual value for object quotas limit and usage issue a **GET** request against */api/<objects>/*.
To get all quotas visible to the user issue a **GET** request against */api/quotas/*
"""
return super(QuotaViewSet, self).list(request, *args, **kwargs) | python | def list(self, request, *args, **kwargs):
"""
To get an actual value for object quotas limit and usage issue a **GET** request against */api/<objects>/*.
To get all quotas visible to the user issue a **GET** request against */api/quotas/*
"""
return super(QuotaViewSet, self).list(request, *args, **kwargs) | [
"def",
"list",
"(",
"self",
",",
"request",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"return",
"super",
"(",
"QuotaViewSet",
",",
"self",
")",
".",
"list",
"(",
"request",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")"
] | To get an actual value for object quotas limit and usage issue a **GET** request against */api/<objects>/*.
To get all quotas visible to the user issue a **GET** request against */api/quotas/* | [
"To",
"get",
"an",
"actual",
"value",
"for",
"object",
"quotas",
"limit",
"and",
"usage",
"issue",
"a",
"**",
"GET",
"**",
"request",
"against",
"*",
"/",
"api",
"/",
"<objects",
">",
"/",
"*",
"."
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/quotas/views.py#L27-L33 |
opennode/waldur-core | waldur_core/quotas/views.py | QuotaViewSet.retrieve | def retrieve(self, request, *args, **kwargs):
"""
To set quota limit issue a **PUT** request against */api/quotas/<quota uuid>** with limit values.
Please note that if a quota is a cache of a backend quota (e.g. 'storage' size of an OpenStack tenant),
it will be impossible to modify it through */api/quotas/<quota uuid>** endpoint.
Example of changing quota limit:
.. code-block:: http
POST /api/quotas/6ad5f49d6d6c49648573b2b71f44a42b/ HTTP/1.1
Content-Type: application/json
Accept: application/json
Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4
Host: example.com
{
"limit": 2000.0
}
Example of changing quota threshold:
.. code-block:: http
PUT /api/quotas/6ad5f49d6d6c49648573b2b71f44a42b/ HTTP/1.1
Content-Type: application/json
Accept: application/json
Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4
Host: example.com
{
"threshold": 100.0
}
"""
return super(QuotaViewSet, self).retrieve(request, *args, **kwargs) | python | def retrieve(self, request, *args, **kwargs):
"""
To set quota limit issue a **PUT** request against */api/quotas/<quota uuid>** with limit values.
Please note that if a quota is a cache of a backend quota (e.g. 'storage' size of an OpenStack tenant),
it will be impossible to modify it through */api/quotas/<quota uuid>** endpoint.
Example of changing quota limit:
.. code-block:: http
POST /api/quotas/6ad5f49d6d6c49648573b2b71f44a42b/ HTTP/1.1
Content-Type: application/json
Accept: application/json
Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4
Host: example.com
{
"limit": 2000.0
}
Example of changing quota threshold:
.. code-block:: http
PUT /api/quotas/6ad5f49d6d6c49648573b2b71f44a42b/ HTTP/1.1
Content-Type: application/json
Accept: application/json
Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4
Host: example.com
{
"threshold": 100.0
}
"""
return super(QuotaViewSet, self).retrieve(request, *args, **kwargs) | [
"def",
"retrieve",
"(",
"self",
",",
"request",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"return",
"super",
"(",
"QuotaViewSet",
",",
"self",
")",
".",
"retrieve",
"(",
"request",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")"
] | To set quota limit issue a **PUT** request against */api/quotas/<quota uuid>** with limit values.
Please note that if a quota is a cache of a backend quota (e.g. 'storage' size of an OpenStack tenant),
it will be impossible to modify it through */api/quotas/<quota uuid>** endpoint.
Example of changing quota limit:
.. code-block:: http
POST /api/quotas/6ad5f49d6d6c49648573b2b71f44a42b/ HTTP/1.1
Content-Type: application/json
Accept: application/json
Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4
Host: example.com
{
"limit": 2000.0
}
Example of changing quota threshold:
.. code-block:: http
PUT /api/quotas/6ad5f49d6d6c49648573b2b71f44a42b/ HTTP/1.1
Content-Type: application/json
Accept: application/json
Authorization: Token c84d653b9ec92c6cbac41c706593e66f567a7fa4
Host: example.com
{
"threshold": 100.0
} | [
"To",
"set",
"quota",
"limit",
"issue",
"a",
"**",
"PUT",
"**",
"request",
"against",
"*",
"/",
"api",
"/",
"quotas",
"/",
"<quota",
"uuid",
">",
"**",
"with",
"limit",
"values",
"."
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/quotas/views.py#L35-L71 |
opennode/waldur-core | waldur_core/quotas/views.py | QuotaViewSet.history | def history(self, request, uuid=None):
"""
Historical data endpoints could be available for any objects (currently
implemented for quotas and events count). The data is available at *<object_endpoint>/history/*,
for example: */api/quotas/<uuid>/history/*.
There are two ways to define datetime points for historical data.
1. Send *?point=<timestamp>* parameter that can list. Response will contain historical data for each given point
in the same order.
2. Send *?start=<timestamp>*, *?end=<timestamp>*, *?points_count=<integer>* parameters.
Result will contain <points_count> points from <start> to <end>.
Response format:
.. code-block:: javascript
[
{
"point": <timestamp>,
"object": {<object_representation>}
},
{
"point": <timestamp>
"object": {<object_representation>}
},
...
]
NB! There will not be any "object" for corresponding point in response if there
is no data about object for a given timestamp.
"""
mapped = {
'start': request.query_params.get('start'),
'end': request.query_params.get('end'),
'points_count': request.query_params.get('points_count'),
'point_list': request.query_params.getlist('point'),
}
history_serializer = HistorySerializer(data={k: v for k, v in mapped.items() if v})
history_serializer.is_valid(raise_exception=True)
quota = self.get_object()
serializer = self.get_serializer(quota)
serialized_versions = []
for point_date in history_serializer.get_filter_data():
serialized = {'point': datetime_to_timestamp(point_date)}
version = Version.objects.get_for_object(quota).filter(revision__date_created__lte=point_date)
if version.exists():
# make copy of serialized data and update field that are stored in version
version_object = version.first()._object_version.object
serialized['object'] = serializer.data.copy()
serialized['object'].update({
f: getattr(version_object, f) for f in quota.get_version_fields()
})
serialized_versions.append(serialized)
return response.Response(serialized_versions, status=status.HTTP_200_OK) | python | def history(self, request, uuid=None):
"""
Historical data endpoints could be available for any objects (currently
implemented for quotas and events count). The data is available at *<object_endpoint>/history/*,
for example: */api/quotas/<uuid>/history/*.
There are two ways to define datetime points for historical data.
1. Send *?point=<timestamp>* parameter that can list. Response will contain historical data for each given point
in the same order.
2. Send *?start=<timestamp>*, *?end=<timestamp>*, *?points_count=<integer>* parameters.
Result will contain <points_count> points from <start> to <end>.
Response format:
.. code-block:: javascript
[
{
"point": <timestamp>,
"object": {<object_representation>}
},
{
"point": <timestamp>
"object": {<object_representation>}
},
...
]
NB! There will not be any "object" for corresponding point in response if there
is no data about object for a given timestamp.
"""
mapped = {
'start': request.query_params.get('start'),
'end': request.query_params.get('end'),
'points_count': request.query_params.get('points_count'),
'point_list': request.query_params.getlist('point'),
}
history_serializer = HistorySerializer(data={k: v for k, v in mapped.items() if v})
history_serializer.is_valid(raise_exception=True)
quota = self.get_object()
serializer = self.get_serializer(quota)
serialized_versions = []
for point_date in history_serializer.get_filter_data():
serialized = {'point': datetime_to_timestamp(point_date)}
version = Version.objects.get_for_object(quota).filter(revision__date_created__lte=point_date)
if version.exists():
# make copy of serialized data and update field that are stored in version
version_object = version.first()._object_version.object
serialized['object'] = serializer.data.copy()
serialized['object'].update({
f: getattr(version_object, f) for f in quota.get_version_fields()
})
serialized_versions.append(serialized)
return response.Response(serialized_versions, status=status.HTTP_200_OK) | [
"def",
"history",
"(",
"self",
",",
"request",
",",
"uuid",
"=",
"None",
")",
":",
"mapped",
"=",
"{",
"'start'",
":",
"request",
".",
"query_params",
".",
"get",
"(",
"'start'",
")",
",",
"'end'",
":",
"request",
".",
"query_params",
".",
"get",
"(",
"'end'",
")",
",",
"'points_count'",
":",
"request",
".",
"query_params",
".",
"get",
"(",
"'points_count'",
")",
",",
"'point_list'",
":",
"request",
".",
"query_params",
".",
"getlist",
"(",
"'point'",
")",
",",
"}",
"history_serializer",
"=",
"HistorySerializer",
"(",
"data",
"=",
"{",
"k",
":",
"v",
"for",
"k",
",",
"v",
"in",
"mapped",
".",
"items",
"(",
")",
"if",
"v",
"}",
")",
"history_serializer",
".",
"is_valid",
"(",
"raise_exception",
"=",
"True",
")",
"quota",
"=",
"self",
".",
"get_object",
"(",
")",
"serializer",
"=",
"self",
".",
"get_serializer",
"(",
"quota",
")",
"serialized_versions",
"=",
"[",
"]",
"for",
"point_date",
"in",
"history_serializer",
".",
"get_filter_data",
"(",
")",
":",
"serialized",
"=",
"{",
"'point'",
":",
"datetime_to_timestamp",
"(",
"point_date",
")",
"}",
"version",
"=",
"Version",
".",
"objects",
".",
"get_for_object",
"(",
"quota",
")",
".",
"filter",
"(",
"revision__date_created__lte",
"=",
"point_date",
")",
"if",
"version",
".",
"exists",
"(",
")",
":",
"# make copy of serialized data and update field that are stored in version",
"version_object",
"=",
"version",
".",
"first",
"(",
")",
".",
"_object_version",
".",
"object",
"serialized",
"[",
"'object'",
"]",
"=",
"serializer",
".",
"data",
".",
"copy",
"(",
")",
"serialized",
"[",
"'object'",
"]",
".",
"update",
"(",
"{",
"f",
":",
"getattr",
"(",
"version_object",
",",
"f",
")",
"for",
"f",
"in",
"quota",
".",
"get_version_fields",
"(",
")",
"}",
")",
"serialized_versions",
".",
"append",
"(",
"serialized",
")",
"return",
"response",
".",
"Response",
"(",
"serialized_versions",
",",
"status",
"=",
"status",
".",
"HTTP_200_OK",
")"
] | Historical data endpoints could be available for any objects (currently
implemented for quotas and events count). The data is available at *<object_endpoint>/history/*,
for example: */api/quotas/<uuid>/history/*.
There are two ways to define datetime points for historical data.
1. Send *?point=<timestamp>* parameter that can list. Response will contain historical data for each given point
in the same order.
2. Send *?start=<timestamp>*, *?end=<timestamp>*, *?points_count=<integer>* parameters.
Result will contain <points_count> points from <start> to <end>.
Response format:
.. code-block:: javascript
[
{
"point": <timestamp>,
"object": {<object_representation>}
},
{
"point": <timestamp>
"object": {<object_representation>}
},
...
]
NB! There will not be any "object" for corresponding point in response if there
is no data about object for a given timestamp. | [
"Historical",
"data",
"endpoints",
"could",
"be",
"available",
"for",
"any",
"objects",
"(",
"currently",
"implemented",
"for",
"quotas",
"and",
"events",
"count",
")",
".",
"The",
"data",
"is",
"available",
"at",
"*",
"<object_endpoint",
">",
"/",
"history",
"/",
"*",
"for",
"example",
":",
"*",
"/",
"api",
"/",
"quotas",
"/",
"<uuid",
">",
"/",
"history",
"/",
"*",
"."
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/quotas/views.py#L96-L151 |
opennode/waldur-core | waldur_core/core/serializers.py | GenericRelatedField._get_url | def _get_url(self, obj):
"""
Gets object url
"""
format_kwargs = {
'app_label': obj._meta.app_label,
}
try:
format_kwargs['model_name'] = getattr(obj.__class__, 'get_url_name')()
except AttributeError:
format_kwargs['model_name'] = obj._meta.object_name.lower()
return self._default_view_name % format_kwargs | python | def _get_url(self, obj):
"""
Gets object url
"""
format_kwargs = {
'app_label': obj._meta.app_label,
}
try:
format_kwargs['model_name'] = getattr(obj.__class__, 'get_url_name')()
except AttributeError:
format_kwargs['model_name'] = obj._meta.object_name.lower()
return self._default_view_name % format_kwargs | [
"def",
"_get_url",
"(",
"self",
",",
"obj",
")",
":",
"format_kwargs",
"=",
"{",
"'app_label'",
":",
"obj",
".",
"_meta",
".",
"app_label",
",",
"}",
"try",
":",
"format_kwargs",
"[",
"'model_name'",
"]",
"=",
"getattr",
"(",
"obj",
".",
"__class__",
",",
"'get_url_name'",
")",
"(",
")",
"except",
"AttributeError",
":",
"format_kwargs",
"[",
"'model_name'",
"]",
"=",
"obj",
".",
"_meta",
".",
"object_name",
".",
"lower",
"(",
")",
"return",
"self",
".",
"_default_view_name",
"%",
"format_kwargs"
] | Gets object url | [
"Gets",
"object",
"url"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/serializers.py#L82-L93 |
opennode/waldur-core | waldur_core/core/serializers.py | GenericRelatedField.to_representation | def to_representation(self, obj):
"""
Serializes any object to his url representation
"""
kwargs = None
for field in self.lookup_fields:
if hasattr(obj, field):
kwargs = {field: getattr(obj, field)}
break
if kwargs is None:
raise AttributeError('Related object does not have any of lookup_fields')
request = self._get_request()
return request.build_absolute_uri(reverse(self._get_url(obj), kwargs=kwargs)) | python | def to_representation(self, obj):
"""
Serializes any object to his url representation
"""
kwargs = None
for field in self.lookup_fields:
if hasattr(obj, field):
kwargs = {field: getattr(obj, field)}
break
if kwargs is None:
raise AttributeError('Related object does not have any of lookup_fields')
request = self._get_request()
return request.build_absolute_uri(reverse(self._get_url(obj), kwargs=kwargs)) | [
"def",
"to_representation",
"(",
"self",
",",
"obj",
")",
":",
"kwargs",
"=",
"None",
"for",
"field",
"in",
"self",
".",
"lookup_fields",
":",
"if",
"hasattr",
"(",
"obj",
",",
"field",
")",
":",
"kwargs",
"=",
"{",
"field",
":",
"getattr",
"(",
"obj",
",",
"field",
")",
"}",
"break",
"if",
"kwargs",
"is",
"None",
":",
"raise",
"AttributeError",
"(",
"'Related object does not have any of lookup_fields'",
")",
"request",
"=",
"self",
".",
"_get_request",
"(",
")",
"return",
"request",
".",
"build_absolute_uri",
"(",
"reverse",
"(",
"self",
".",
"_get_url",
"(",
"obj",
")",
",",
"kwargs",
"=",
"kwargs",
")",
")"
] | Serializes any object to his url representation | [
"Serializes",
"any",
"object",
"to",
"his",
"url",
"representation"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/serializers.py#L101-L113 |
opennode/waldur-core | waldur_core/core/serializers.py | GenericRelatedField.to_internal_value | def to_internal_value(self, data):
"""
Restores model instance from its url
"""
if not data:
return None
request = self._get_request()
user = request.user
try:
obj = core_utils.instance_from_url(data, user=user)
model = obj.__class__
except ValueError:
raise serializers.ValidationError(_('URL is invalid: %s.') % data)
except (Resolver404, AttributeError, MultipleObjectsReturned, ObjectDoesNotExist):
raise serializers.ValidationError(_("Can't restore object from url: %s") % data)
if model not in self.related_models:
raise serializers.ValidationError(_('%s object does not support such relationship.') % six.text_type(obj))
return obj | python | def to_internal_value(self, data):
"""
Restores model instance from its url
"""
if not data:
return None
request = self._get_request()
user = request.user
try:
obj = core_utils.instance_from_url(data, user=user)
model = obj.__class__
except ValueError:
raise serializers.ValidationError(_('URL is invalid: %s.') % data)
except (Resolver404, AttributeError, MultipleObjectsReturned, ObjectDoesNotExist):
raise serializers.ValidationError(_("Can't restore object from url: %s") % data)
if model not in self.related_models:
raise serializers.ValidationError(_('%s object does not support such relationship.') % six.text_type(obj))
return obj | [
"def",
"to_internal_value",
"(",
"self",
",",
"data",
")",
":",
"if",
"not",
"data",
":",
"return",
"None",
"request",
"=",
"self",
".",
"_get_request",
"(",
")",
"user",
"=",
"request",
".",
"user",
"try",
":",
"obj",
"=",
"core_utils",
".",
"instance_from_url",
"(",
"data",
",",
"user",
"=",
"user",
")",
"model",
"=",
"obj",
".",
"__class__",
"except",
"ValueError",
":",
"raise",
"serializers",
".",
"ValidationError",
"(",
"_",
"(",
"'URL is invalid: %s.'",
")",
"%",
"data",
")",
"except",
"(",
"Resolver404",
",",
"AttributeError",
",",
"MultipleObjectsReturned",
",",
"ObjectDoesNotExist",
")",
":",
"raise",
"serializers",
".",
"ValidationError",
"(",
"_",
"(",
"\"Can't restore object from url: %s\"",
")",
"%",
"data",
")",
"if",
"model",
"not",
"in",
"self",
".",
"related_models",
":",
"raise",
"serializers",
".",
"ValidationError",
"(",
"_",
"(",
"'%s object does not support such relationship.'",
")",
"%",
"six",
".",
"text_type",
"(",
"obj",
")",
")",
"return",
"obj"
] | Restores model instance from its url | [
"Restores",
"model",
"instance",
"from",
"its",
"url"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/serializers.py#L115-L132 |
opennode/waldur-core | waldur_core/core/serializers.py | TimestampIntervalSerializer.validate | def validate(self, data):
"""
Check that the start is before the end.
"""
if 'start' in data and 'end' in data and data['start'] >= data['end']:
raise serializers.ValidationError(_('End must occur after start.'))
return data | python | def validate(self, data):
"""
Check that the start is before the end.
"""
if 'start' in data and 'end' in data and data['start'] >= data['end']:
raise serializers.ValidationError(_('End must occur after start.'))
return data | [
"def",
"validate",
"(",
"self",
",",
"data",
")",
":",
"if",
"'start'",
"in",
"data",
"and",
"'end'",
"in",
"data",
"and",
"data",
"[",
"'start'",
"]",
">=",
"data",
"[",
"'end'",
"]",
":",
"raise",
"serializers",
".",
"ValidationError",
"(",
"_",
"(",
"'End must occur after start.'",
")",
")",
"return",
"data"
] | Check that the start is before the end. | [
"Check",
"that",
"the",
"start",
"is",
"before",
"the",
"end",
"."
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/serializers.py#L373-L379 |
opennode/waldur-core | waldur_core/core/tasks.py | send_task | def send_task(app_label, task_name):
""" A helper function to deal with waldur_core "high-level" tasks.
Define high-level task with explicit name using a pattern:
waldur_core.<app_label>.<task_name>
.. code-block:: python
@shared_task(name='waldur_core.openstack.provision_instance')
def provision_instance_fn(instance_uuid, backend_flavor_id)
pass
Call it by name:
.. code-block:: python
send_task('openstack', 'provision_instance')(instance_uuid, backend_flavor_id)
Which is identical to:
.. code-block:: python
provision_instance_fn.delay(instance_uuid, backend_flavor_id)
"""
def delay(*args, **kwargs):
full_task_name = 'waldur_core.%s.%s' % (app_label, task_name)
send_celery_task(full_task_name, args, kwargs, countdown=2)
return delay | python | def send_task(app_label, task_name):
""" A helper function to deal with waldur_core "high-level" tasks.
Define high-level task with explicit name using a pattern:
waldur_core.<app_label>.<task_name>
.. code-block:: python
@shared_task(name='waldur_core.openstack.provision_instance')
def provision_instance_fn(instance_uuid, backend_flavor_id)
pass
Call it by name:
.. code-block:: python
send_task('openstack', 'provision_instance')(instance_uuid, backend_flavor_id)
Which is identical to:
.. code-block:: python
provision_instance_fn.delay(instance_uuid, backend_flavor_id)
"""
def delay(*args, **kwargs):
full_task_name = 'waldur_core.%s.%s' % (app_label, task_name)
send_celery_task(full_task_name, args, kwargs, countdown=2)
return delay | [
"def",
"send_task",
"(",
"app_label",
",",
"task_name",
")",
":",
"def",
"delay",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"full_task_name",
"=",
"'waldur_core.%s.%s'",
"%",
"(",
"app_label",
",",
"task_name",
")",
"send_celery_task",
"(",
"full_task_name",
",",
"args",
",",
"kwargs",
",",
"countdown",
"=",
"2",
")",
"return",
"delay"
] | A helper function to deal with waldur_core "high-level" tasks.
Define high-level task with explicit name using a pattern:
waldur_core.<app_label>.<task_name>
.. code-block:: python
@shared_task(name='waldur_core.openstack.provision_instance')
def provision_instance_fn(instance_uuid, backend_flavor_id)
pass
Call it by name:
.. code-block:: python
send_task('openstack', 'provision_instance')(instance_uuid, backend_flavor_id)
Which is identical to:
.. code-block:: python
provision_instance_fn.delay(instance_uuid, backend_flavor_id) | [
"A",
"helper",
"function",
"to",
"deal",
"with",
"waldur_core",
"high",
"-",
"level",
"tasks",
".",
"Define",
"high",
"-",
"level",
"task",
"with",
"explicit",
"name",
"using",
"a",
"pattern",
":",
"waldur_core",
".",
"<app_label",
">",
".",
"<task_name",
">"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/tasks.py#L54-L80 |
opennode/waldur-core | waldur_core/core/tasks.py | log_celery_task | def log_celery_task(request):
""" Add description to celery log output """
task = request.task
description = None
if isinstance(task, Task):
try:
description = task.get_description(*request.args, **request.kwargs)
except NotImplementedError:
pass
except Exception as e:
# Logging should never break workflow.
logger.exception('Cannot get description for task %s. Error: %s' % (task.__class__.__name__, e))
return '{0.name}[{0.id}]{1}{2}{3}'.format(
request,
' {0}'.format(description) if description else '',
' eta:[{0}]'.format(request.eta) if request.eta else '',
' expires:[{0}]'.format(request.expires) if request.expires else '',
) | python | def log_celery_task(request):
""" Add description to celery log output """
task = request.task
description = None
if isinstance(task, Task):
try:
description = task.get_description(*request.args, **request.kwargs)
except NotImplementedError:
pass
except Exception as e:
# Logging should never break workflow.
logger.exception('Cannot get description for task %s. Error: %s' % (task.__class__.__name__, e))
return '{0.name}[{0.id}]{1}{2}{3}'.format(
request,
' {0}'.format(description) if description else '',
' eta:[{0}]'.format(request.eta) if request.eta else '',
' expires:[{0}]'.format(request.expires) if request.expires else '',
) | [
"def",
"log_celery_task",
"(",
"request",
")",
":",
"task",
"=",
"request",
".",
"task",
"description",
"=",
"None",
"if",
"isinstance",
"(",
"task",
",",
"Task",
")",
":",
"try",
":",
"description",
"=",
"task",
".",
"get_description",
"(",
"*",
"request",
".",
"args",
",",
"*",
"*",
"request",
".",
"kwargs",
")",
"except",
"NotImplementedError",
":",
"pass",
"except",
"Exception",
"as",
"e",
":",
"# Logging should never break workflow.",
"logger",
".",
"exception",
"(",
"'Cannot get description for task %s. Error: %s'",
"%",
"(",
"task",
".",
"__class__",
".",
"__name__",
",",
"e",
")",
")",
"return",
"'{0.name}[{0.id}]{1}{2}{3}'",
".",
"format",
"(",
"request",
",",
"' {0}'",
".",
"format",
"(",
"description",
")",
"if",
"description",
"else",
"''",
",",
"' eta:[{0}]'",
".",
"format",
"(",
"request",
".",
"eta",
")",
"if",
"request",
".",
"eta",
"else",
"''",
",",
"' expires:[{0}]'",
".",
"format",
"(",
"request",
".",
"expires",
")",
"if",
"request",
".",
"expires",
"else",
"''",
",",
")"
] | Add description to celery log output | [
"Add",
"description",
"to",
"celery",
"log",
"output"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/tasks.py#L440-L458 |
opennode/waldur-core | waldur_core/core/tasks.py | Task.run | def run(self, serialized_instance, *args, **kwargs):
""" Deserialize input data and start backend operation execution """
try:
instance = utils.deserialize_instance(serialized_instance)
except ObjectDoesNotExist:
message = ('Cannot restore instance from serialized object %s. Probably it was deleted.' %
serialized_instance)
six.reraise(ObjectDoesNotExist, message)
self.args = args
self.kwargs = kwargs
self.pre_execute(instance)
result = self.execute(instance, *self.args, **self.kwargs)
self.post_execute(instance)
if result and isinstance(result, django_models.Model):
result = utils.serialize_instance(result)
return result | python | def run(self, serialized_instance, *args, **kwargs):
""" Deserialize input data and start backend operation execution """
try:
instance = utils.deserialize_instance(serialized_instance)
except ObjectDoesNotExist:
message = ('Cannot restore instance from serialized object %s. Probably it was deleted.' %
serialized_instance)
six.reraise(ObjectDoesNotExist, message)
self.args = args
self.kwargs = kwargs
self.pre_execute(instance)
result = self.execute(instance, *self.args, **self.kwargs)
self.post_execute(instance)
if result and isinstance(result, django_models.Model):
result = utils.serialize_instance(result)
return result | [
"def",
"run",
"(",
"self",
",",
"serialized_instance",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"try",
":",
"instance",
"=",
"utils",
".",
"deserialize_instance",
"(",
"serialized_instance",
")",
"except",
"ObjectDoesNotExist",
":",
"message",
"=",
"(",
"'Cannot restore instance from serialized object %s. Probably it was deleted.'",
"%",
"serialized_instance",
")",
"six",
".",
"reraise",
"(",
"ObjectDoesNotExist",
",",
"message",
")",
"self",
".",
"args",
"=",
"args",
"self",
".",
"kwargs",
"=",
"kwargs",
"self",
".",
"pre_execute",
"(",
"instance",
")",
"result",
"=",
"self",
".",
"execute",
"(",
"instance",
",",
"*",
"self",
".",
"args",
",",
"*",
"*",
"self",
".",
"kwargs",
")",
"self",
".",
"post_execute",
"(",
"instance",
")",
"if",
"result",
"and",
"isinstance",
"(",
"result",
",",
"django_models",
".",
"Model",
")",
":",
"result",
"=",
"utils",
".",
"serialize_instance",
"(",
"result",
")",
"return",
"result"
] | Deserialize input data and start backend operation execution | [
"Deserialize",
"input",
"data",
"and",
"start",
"backend",
"operation",
"execution"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/tasks.py#L97-L114 |
opennode/waldur-core | waldur_core/core/tasks.py | BackgroundTask.is_previous_task_processing | def is_previous_task_processing(self, *args, **kwargs):
""" Return True if exist task that is equal to current and is uncompleted """
app = self._get_app()
inspect = app.control.inspect()
active = inspect.active() or {}
scheduled = inspect.scheduled() or {}
reserved = inspect.reserved() or {}
uncompleted = sum(list(active.values()) + list(scheduled.values()) + reserved.values(), [])
return any(self.is_equal(task, *args, **kwargs) for task in uncompleted) | python | def is_previous_task_processing(self, *args, **kwargs):
""" Return True if exist task that is equal to current and is uncompleted """
app = self._get_app()
inspect = app.control.inspect()
active = inspect.active() or {}
scheduled = inspect.scheduled() or {}
reserved = inspect.reserved() or {}
uncompleted = sum(list(active.values()) + list(scheduled.values()) + reserved.values(), [])
return any(self.is_equal(task, *args, **kwargs) for task in uncompleted) | [
"def",
"is_previous_task_processing",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"app",
"=",
"self",
".",
"_get_app",
"(",
")",
"inspect",
"=",
"app",
".",
"control",
".",
"inspect",
"(",
")",
"active",
"=",
"inspect",
".",
"active",
"(",
")",
"or",
"{",
"}",
"scheduled",
"=",
"inspect",
".",
"scheduled",
"(",
")",
"or",
"{",
"}",
"reserved",
"=",
"inspect",
".",
"reserved",
"(",
")",
"or",
"{",
"}",
"uncompleted",
"=",
"sum",
"(",
"list",
"(",
"active",
".",
"values",
"(",
")",
")",
"+",
"list",
"(",
"scheduled",
".",
"values",
"(",
")",
")",
"+",
"reserved",
".",
"values",
"(",
")",
",",
"[",
"]",
")",
"return",
"any",
"(",
"self",
".",
"is_equal",
"(",
"task",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"for",
"task",
"in",
"uncompleted",
")"
] | Return True if exist task that is equal to current and is uncompleted | [
"Return",
"True",
"if",
"exist",
"task",
"that",
"is",
"equal",
"to",
"current",
"and",
"is",
"uncompleted"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/tasks.py#L353-L361 |
opennode/waldur-core | waldur_core/core/tasks.py | BackgroundTask.apply_async | def apply_async(self, args=None, kwargs=None, **options):
""" Do not run background task if previous task is uncompleted """
if self.is_previous_task_processing(*args, **kwargs):
message = 'Background task %s was not scheduled, because its predecessor is not completed yet.' % self.name
logger.info(message)
# It is expected by Celery that apply_async return AsyncResult, otherwise celerybeat dies
return self.AsyncResult(options.get('task_id') or str(uuid4()))
return super(BackgroundTask, self).apply_async(args=args, kwargs=kwargs, **options) | python | def apply_async(self, args=None, kwargs=None, **options):
""" Do not run background task if previous task is uncompleted """
if self.is_previous_task_processing(*args, **kwargs):
message = 'Background task %s was not scheduled, because its predecessor is not completed yet.' % self.name
logger.info(message)
# It is expected by Celery that apply_async return AsyncResult, otherwise celerybeat dies
return self.AsyncResult(options.get('task_id') or str(uuid4()))
return super(BackgroundTask, self).apply_async(args=args, kwargs=kwargs, **options) | [
"def",
"apply_async",
"(",
"self",
",",
"args",
"=",
"None",
",",
"kwargs",
"=",
"None",
",",
"*",
"*",
"options",
")",
":",
"if",
"self",
".",
"is_previous_task_processing",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"message",
"=",
"'Background task %s was not scheduled, because its predecessor is not completed yet.'",
"%",
"self",
".",
"name",
"logger",
".",
"info",
"(",
"message",
")",
"# It is expected by Celery that apply_async return AsyncResult, otherwise celerybeat dies",
"return",
"self",
".",
"AsyncResult",
"(",
"options",
".",
"get",
"(",
"'task_id'",
")",
"or",
"str",
"(",
"uuid4",
"(",
")",
")",
")",
"return",
"super",
"(",
"BackgroundTask",
",",
"self",
")",
".",
"apply_async",
"(",
"args",
"=",
"args",
",",
"kwargs",
"=",
"kwargs",
",",
"*",
"*",
"options",
")"
] | Do not run background task if previous task is uncompleted | [
"Do",
"not",
"run",
"background",
"task",
"if",
"previous",
"task",
"is",
"uncompleted"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/tasks.py#L363-L370 |
opennode/waldur-core | waldur_core/core/tasks.py | PenalizedBackgroundTask._get_cache_key | def _get_cache_key(self, args, kwargs):
""" Returns key to be used in cache """
hash_input = json.dumps({'name': self.name, 'args': args, 'kwargs': kwargs}, sort_keys=True)
# md5 is used for internal caching, not need to care about security
return hashlib.md5(hash_input).hexdigest() | python | def _get_cache_key(self, args, kwargs):
""" Returns key to be used in cache """
hash_input = json.dumps({'name': self.name, 'args': args, 'kwargs': kwargs}, sort_keys=True)
# md5 is used for internal caching, not need to care about security
return hashlib.md5(hash_input).hexdigest() | [
"def",
"_get_cache_key",
"(",
"self",
",",
"args",
",",
"kwargs",
")",
":",
"hash_input",
"=",
"json",
".",
"dumps",
"(",
"{",
"'name'",
":",
"self",
".",
"name",
",",
"'args'",
":",
"args",
",",
"'kwargs'",
":",
"kwargs",
"}",
",",
"sort_keys",
"=",
"True",
")",
"# md5 is used for internal caching, not need to care about security",
"return",
"hashlib",
".",
"md5",
"(",
"hash_input",
")",
".",
"hexdigest",
"(",
")"
] | Returns key to be used in cache | [
"Returns",
"key",
"to",
"be",
"used",
"in",
"cache"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/tasks.py#L396-L400 |
opennode/waldur-core | waldur_core/core/tasks.py | PenalizedBackgroundTask.apply_async | def apply_async(self, args=None, kwargs=None, **options):
"""
Checks whether task must be skipped and decreases the counter in that case.
"""
key = self._get_cache_key(args, kwargs)
counter, penalty = cache.get(key, (0, 0))
if not counter:
return super(PenalizedBackgroundTask, self).apply_async(args=args, kwargs=kwargs, **options)
cache.set(key, (counter - 1, penalty), self.CACHE_LIFETIME)
logger.info('The task %s will not be executed due to the penalty.' % self.name)
return self.AsyncResult(options.get('task_id') or str(uuid4())) | python | def apply_async(self, args=None, kwargs=None, **options):
"""
Checks whether task must be skipped and decreases the counter in that case.
"""
key = self._get_cache_key(args, kwargs)
counter, penalty = cache.get(key, (0, 0))
if not counter:
return super(PenalizedBackgroundTask, self).apply_async(args=args, kwargs=kwargs, **options)
cache.set(key, (counter - 1, penalty), self.CACHE_LIFETIME)
logger.info('The task %s will not be executed due to the penalty.' % self.name)
return self.AsyncResult(options.get('task_id') or str(uuid4())) | [
"def",
"apply_async",
"(",
"self",
",",
"args",
"=",
"None",
",",
"kwargs",
"=",
"None",
",",
"*",
"*",
"options",
")",
":",
"key",
"=",
"self",
".",
"_get_cache_key",
"(",
"args",
",",
"kwargs",
")",
"counter",
",",
"penalty",
"=",
"cache",
".",
"get",
"(",
"key",
",",
"(",
"0",
",",
"0",
")",
")",
"if",
"not",
"counter",
":",
"return",
"super",
"(",
"PenalizedBackgroundTask",
",",
"self",
")",
".",
"apply_async",
"(",
"args",
"=",
"args",
",",
"kwargs",
"=",
"kwargs",
",",
"*",
"*",
"options",
")",
"cache",
".",
"set",
"(",
"key",
",",
"(",
"counter",
"-",
"1",
",",
"penalty",
")",
",",
"self",
".",
"CACHE_LIFETIME",
")",
"logger",
".",
"info",
"(",
"'The task %s will not be executed due to the penalty.'",
"%",
"self",
".",
"name",
")",
"return",
"self",
".",
"AsyncResult",
"(",
"options",
".",
"get",
"(",
"'task_id'",
")",
"or",
"str",
"(",
"uuid4",
"(",
")",
")",
")"
] | Checks whether task must be skipped and decreases the counter in that case. | [
"Checks",
"whether",
"task",
"must",
"be",
"skipped",
"and",
"decreases",
"the",
"counter",
"in",
"that",
"case",
"."
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/tasks.py#L402-L413 |
opennode/waldur-core | waldur_core/core/tasks.py | PenalizedBackgroundTask.on_failure | def on_failure(self, exc, task_id, args, kwargs, einfo):
"""
Increases penalty for the task and resets the counter.
"""
key = self._get_cache_key(args, kwargs)
_, penalty = cache.get(key, (0, 0))
if penalty < self.MAX_PENALTY:
penalty += 1
logger.debug('The task %s is penalized and will be executed on %d run.' % (self.name, penalty))
cache.set(key, (penalty, penalty), self.CACHE_LIFETIME)
return super(PenalizedBackgroundTask, self).on_failure(exc, task_id, args, kwargs, einfo) | python | def on_failure(self, exc, task_id, args, kwargs, einfo):
"""
Increases penalty for the task and resets the counter.
"""
key = self._get_cache_key(args, kwargs)
_, penalty = cache.get(key, (0, 0))
if penalty < self.MAX_PENALTY:
penalty += 1
logger.debug('The task %s is penalized and will be executed on %d run.' % (self.name, penalty))
cache.set(key, (penalty, penalty), self.CACHE_LIFETIME)
return super(PenalizedBackgroundTask, self).on_failure(exc, task_id, args, kwargs, einfo) | [
"def",
"on_failure",
"(",
"self",
",",
"exc",
",",
"task_id",
",",
"args",
",",
"kwargs",
",",
"einfo",
")",
":",
"key",
"=",
"self",
".",
"_get_cache_key",
"(",
"args",
",",
"kwargs",
")",
"_",
",",
"penalty",
"=",
"cache",
".",
"get",
"(",
"key",
",",
"(",
"0",
",",
"0",
")",
")",
"if",
"penalty",
"<",
"self",
".",
"MAX_PENALTY",
":",
"penalty",
"+=",
"1",
"logger",
".",
"debug",
"(",
"'The task %s is penalized and will be executed on %d run.'",
"%",
"(",
"self",
".",
"name",
",",
"penalty",
")",
")",
"cache",
".",
"set",
"(",
"key",
",",
"(",
"penalty",
",",
"penalty",
")",
",",
"self",
".",
"CACHE_LIFETIME",
")",
"return",
"super",
"(",
"PenalizedBackgroundTask",
",",
"self",
")",
".",
"on_failure",
"(",
"exc",
",",
"task_id",
",",
"args",
",",
"kwargs",
",",
"einfo",
")"
] | Increases penalty for the task and resets the counter. | [
"Increases",
"penalty",
"for",
"the",
"task",
"and",
"resets",
"the",
"counter",
"."
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/tasks.py#L415-L426 |
opennode/waldur-core | waldur_core/core/tasks.py | PenalizedBackgroundTask.on_success | def on_success(self, retval, task_id, args, kwargs):
"""
Clears cache for the task.
"""
key = self._get_cache_key(args, kwargs)
if cache.get(key) is not None:
cache.delete(key)
logger.debug('Penalty for the task %s has been removed.' % self.name)
return super(PenalizedBackgroundTask, self).on_success(retval, task_id, args, kwargs) | python | def on_success(self, retval, task_id, args, kwargs):
"""
Clears cache for the task.
"""
key = self._get_cache_key(args, kwargs)
if cache.get(key) is not None:
cache.delete(key)
logger.debug('Penalty for the task %s has been removed.' % self.name)
return super(PenalizedBackgroundTask, self).on_success(retval, task_id, args, kwargs) | [
"def",
"on_success",
"(",
"self",
",",
"retval",
",",
"task_id",
",",
"args",
",",
"kwargs",
")",
":",
"key",
"=",
"self",
".",
"_get_cache_key",
"(",
"args",
",",
"kwargs",
")",
"if",
"cache",
".",
"get",
"(",
"key",
")",
"is",
"not",
"None",
":",
"cache",
".",
"delete",
"(",
"key",
")",
"logger",
".",
"debug",
"(",
"'Penalty for the task %s has been removed.'",
"%",
"self",
".",
"name",
")",
"return",
"super",
"(",
"PenalizedBackgroundTask",
",",
"self",
")",
".",
"on_success",
"(",
"retval",
",",
"task_id",
",",
"args",
",",
"kwargs",
")"
] | Clears cache for the task. | [
"Clears",
"cache",
"for",
"the",
"task",
"."
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/core/tasks.py#L428-L437 |
opennode/waldur-core | waldur_core/structure/__init__.py | log_backend_action | def log_backend_action(action=None):
""" Logging for backend method.
Expects django model instance as first argument.
"""
def decorator(func):
@functools.wraps(func)
def wrapped(self, instance, *args, **kwargs):
action_name = func.func_name.replace('_', ' ') if action is None else action
logger.debug('About to %s `%s` (PK: %s).', action_name, instance, instance.pk)
result = func(self, instance, *args, **kwargs)
logger.debug('Action `%s` was executed successfully for `%s` (PK: %s).',
action_name, instance, instance.pk)
return result
return wrapped
return decorator | python | def log_backend_action(action=None):
""" Logging for backend method.
Expects django model instance as first argument.
"""
def decorator(func):
@functools.wraps(func)
def wrapped(self, instance, *args, **kwargs):
action_name = func.func_name.replace('_', ' ') if action is None else action
logger.debug('About to %s `%s` (PK: %s).', action_name, instance, instance.pk)
result = func(self, instance, *args, **kwargs)
logger.debug('Action `%s` was executed successfully for `%s` (PK: %s).',
action_name, instance, instance.pk)
return result
return wrapped
return decorator | [
"def",
"log_backend_action",
"(",
"action",
"=",
"None",
")",
":",
"def",
"decorator",
"(",
"func",
")",
":",
"@",
"functools",
".",
"wraps",
"(",
"func",
")",
"def",
"wrapped",
"(",
"self",
",",
"instance",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"action_name",
"=",
"func",
".",
"func_name",
".",
"replace",
"(",
"'_'",
",",
"' '",
")",
"if",
"action",
"is",
"None",
"else",
"action",
"logger",
".",
"debug",
"(",
"'About to %s `%s` (PK: %s).'",
",",
"action_name",
",",
"instance",
",",
"instance",
".",
"pk",
")",
"result",
"=",
"func",
"(",
"self",
",",
"instance",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"logger",
".",
"debug",
"(",
"'Action `%s` was executed successfully for `%s` (PK: %s).'",
",",
"action_name",
",",
"instance",
",",
"instance",
".",
"pk",
")",
"return",
"result",
"return",
"wrapped",
"return",
"decorator"
] | Logging for backend method.
Expects django model instance as first argument. | [
"Logging",
"for",
"backend",
"method",
"."
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/structure/__init__.py#L417-L433 |
opennode/waldur-core | waldur_core/structure/__init__.py | SupportedServices.get_services | def get_services(cls, request=None):
""" Get a list of services endpoints.
{
"Oracle": "/api/oracle/",
"OpenStack": "/api/openstack/",
"GitLab": "/api/gitlab/",
"DigitalOcean": "/api/digitalocean/"
}
"""
return {service['name']: reverse(service['list_view'], request=request)
for service in cls._registry.values()} | python | def get_services(cls, request=None):
""" Get a list of services endpoints.
{
"Oracle": "/api/oracle/",
"OpenStack": "/api/openstack/",
"GitLab": "/api/gitlab/",
"DigitalOcean": "/api/digitalocean/"
}
"""
return {service['name']: reverse(service['list_view'], request=request)
for service in cls._registry.values()} | [
"def",
"get_services",
"(",
"cls",
",",
"request",
"=",
"None",
")",
":",
"return",
"{",
"service",
"[",
"'name'",
"]",
":",
"reverse",
"(",
"service",
"[",
"'list_view'",
"]",
",",
"request",
"=",
"request",
")",
"for",
"service",
"in",
"cls",
".",
"_registry",
".",
"values",
"(",
")",
"}"
] | Get a list of services endpoints.
{
"Oracle": "/api/oracle/",
"OpenStack": "/api/openstack/",
"GitLab": "/api/gitlab/",
"DigitalOcean": "/api/digitalocean/"
} | [
"Get",
"a",
"list",
"of",
"services",
"endpoints",
".",
"{",
"Oracle",
":",
"/",
"api",
"/",
"oracle",
"/",
"OpenStack",
":",
"/",
"api",
"/",
"openstack",
"/",
"GitLab",
":",
"/",
"api",
"/",
"gitlab",
"/",
"DigitalOcean",
":",
"/",
"api",
"/",
"digitalocean",
"/",
"}"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/structure/__init__.py#L161-L171 |
opennode/waldur-core | waldur_core/structure/__init__.py | SupportedServices.get_resources | def get_resources(cls, request=None):
""" Get a list of resources endpoints.
{
"DigitalOcean.Droplet": "/api/digitalocean-droplets/",
"Oracle.Database": "/api/oracle-databases/",
"GitLab.Group": "/api/gitlab-groups/",
"GitLab.Project": "/api/gitlab-projects/"
}
"""
return {'.'.join([service['name'], resource['name']]): reverse(resource['list_view'], request=request)
for service in cls._registry.values()
for resource in service['resources'].values()} | python | def get_resources(cls, request=None):
""" Get a list of resources endpoints.
{
"DigitalOcean.Droplet": "/api/digitalocean-droplets/",
"Oracle.Database": "/api/oracle-databases/",
"GitLab.Group": "/api/gitlab-groups/",
"GitLab.Project": "/api/gitlab-projects/"
}
"""
return {'.'.join([service['name'], resource['name']]): reverse(resource['list_view'], request=request)
for service in cls._registry.values()
for resource in service['resources'].values()} | [
"def",
"get_resources",
"(",
"cls",
",",
"request",
"=",
"None",
")",
":",
"return",
"{",
"'.'",
".",
"join",
"(",
"[",
"service",
"[",
"'name'",
"]",
",",
"resource",
"[",
"'name'",
"]",
"]",
")",
":",
"reverse",
"(",
"resource",
"[",
"'list_view'",
"]",
",",
"request",
"=",
"request",
")",
"for",
"service",
"in",
"cls",
".",
"_registry",
".",
"values",
"(",
")",
"for",
"resource",
"in",
"service",
"[",
"'resources'",
"]",
".",
"values",
"(",
")",
"}"
] | Get a list of resources endpoints.
{
"DigitalOcean.Droplet": "/api/digitalocean-droplets/",
"Oracle.Database": "/api/oracle-databases/",
"GitLab.Group": "/api/gitlab-groups/",
"GitLab.Project": "/api/gitlab-projects/"
} | [
"Get",
"a",
"list",
"of",
"resources",
"endpoints",
".",
"{",
"DigitalOcean",
".",
"Droplet",
":",
"/",
"api",
"/",
"digitalocean",
"-",
"droplets",
"/",
"Oracle",
".",
"Database",
":",
"/",
"api",
"/",
"oracle",
"-",
"databases",
"/",
"GitLab",
".",
"Group",
":",
"/",
"api",
"/",
"gitlab",
"-",
"groups",
"/",
"GitLab",
".",
"Project",
":",
"/",
"api",
"/",
"gitlab",
"-",
"projects",
"/",
"}"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/structure/__init__.py#L184-L195 |
opennode/waldur-core | waldur_core/structure/__init__.py | SupportedServices.get_services_with_resources | def get_services_with_resources(cls, request=None):
""" Get a list of services and resources endpoints.
{
...
"GitLab": {
"url": "/api/gitlab/",
"service_project_link_url": "/api/gitlab-service-project-link/",
"resources": {
"Project": "/api/gitlab-projects/",
"Group": "/api/gitlab-groups/"
}
},
...
}
"""
from django.apps import apps
data = {}
for service in cls._registry.values():
service_model = apps.get_model(service['model_name'])
service_project_link = service_model.projects.through
service_project_link_url = reverse(cls.get_list_view_for_model(service_project_link), request=request)
data[service['name']] = {
'url': reverse(service['list_view'], request=request),
'service_project_link_url': service_project_link_url,
'resources': {resource['name']: reverse(resource['list_view'], request=request)
for resource in service['resources'].values()},
'properties': {resource['name']: reverse(resource['list_view'], request=request)
for resource in service.get('properties', {}).values()},
'is_public_service': cls.is_public_service(service_model)
}
return data | python | def get_services_with_resources(cls, request=None):
""" Get a list of services and resources endpoints.
{
...
"GitLab": {
"url": "/api/gitlab/",
"service_project_link_url": "/api/gitlab-service-project-link/",
"resources": {
"Project": "/api/gitlab-projects/",
"Group": "/api/gitlab-groups/"
}
},
...
}
"""
from django.apps import apps
data = {}
for service in cls._registry.values():
service_model = apps.get_model(service['model_name'])
service_project_link = service_model.projects.through
service_project_link_url = reverse(cls.get_list_view_for_model(service_project_link), request=request)
data[service['name']] = {
'url': reverse(service['list_view'], request=request),
'service_project_link_url': service_project_link_url,
'resources': {resource['name']: reverse(resource['list_view'], request=request)
for resource in service['resources'].values()},
'properties': {resource['name']: reverse(resource['list_view'], request=request)
for resource in service.get('properties', {}).values()},
'is_public_service': cls.is_public_service(service_model)
}
return data | [
"def",
"get_services_with_resources",
"(",
"cls",
",",
"request",
"=",
"None",
")",
":",
"from",
"django",
".",
"apps",
"import",
"apps",
"data",
"=",
"{",
"}",
"for",
"service",
"in",
"cls",
".",
"_registry",
".",
"values",
"(",
")",
":",
"service_model",
"=",
"apps",
".",
"get_model",
"(",
"service",
"[",
"'model_name'",
"]",
")",
"service_project_link",
"=",
"service_model",
".",
"projects",
".",
"through",
"service_project_link_url",
"=",
"reverse",
"(",
"cls",
".",
"get_list_view_for_model",
"(",
"service_project_link",
")",
",",
"request",
"=",
"request",
")",
"data",
"[",
"service",
"[",
"'name'",
"]",
"]",
"=",
"{",
"'url'",
":",
"reverse",
"(",
"service",
"[",
"'list_view'",
"]",
",",
"request",
"=",
"request",
")",
",",
"'service_project_link_url'",
":",
"service_project_link_url",
",",
"'resources'",
":",
"{",
"resource",
"[",
"'name'",
"]",
":",
"reverse",
"(",
"resource",
"[",
"'list_view'",
"]",
",",
"request",
"=",
"request",
")",
"for",
"resource",
"in",
"service",
"[",
"'resources'",
"]",
".",
"values",
"(",
")",
"}",
",",
"'properties'",
":",
"{",
"resource",
"[",
"'name'",
"]",
":",
"reverse",
"(",
"resource",
"[",
"'list_view'",
"]",
",",
"request",
"=",
"request",
")",
"for",
"resource",
"in",
"service",
".",
"get",
"(",
"'properties'",
",",
"{",
"}",
")",
".",
"values",
"(",
")",
"}",
",",
"'is_public_service'",
":",
"cls",
".",
"is_public_service",
"(",
"service_model",
")",
"}",
"return",
"data"
] | Get a list of services and resources endpoints.
{
...
"GitLab": {
"url": "/api/gitlab/",
"service_project_link_url": "/api/gitlab-service-project-link/",
"resources": {
"Project": "/api/gitlab-projects/",
"Group": "/api/gitlab-groups/"
}
},
...
} | [
"Get",
"a",
"list",
"of",
"services",
"and",
"resources",
"endpoints",
".",
"{",
"...",
"GitLab",
":",
"{",
"url",
":",
"/",
"api",
"/",
"gitlab",
"/",
"service_project_link_url",
":",
"/",
"api",
"/",
"gitlab",
"-",
"service",
"-",
"project",
"-",
"link",
"/",
"resources",
":",
"{",
"Project",
":",
"/",
"api",
"/",
"gitlab",
"-",
"projects",
"/",
"Group",
":",
"/",
"api",
"/",
"gitlab",
"-",
"groups",
"/",
"}",
"}",
"...",
"}"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/structure/__init__.py#L216-L248 |
opennode/waldur-core | waldur_core/structure/__init__.py | SupportedServices.get_service_models | def get_service_models(cls):
""" Get a list of service models.
{
...
'gitlab': {
"service": nodeconductor_gitlab.models.GitLabService,
"service_project_link": nodeconductor_gitlab.models.GitLabServiceProjectLink,
"resources": [
nodeconductor_gitlab.models.Group,
nodeconductor_gitlab.models.Project
],
},
...
}
"""
from django.apps import apps
data = {}
for key, service in cls._registry.items():
service_model = apps.get_model(service['model_name'])
service_project_link = service_model.projects.through
data[key] = {
'service': service_model,
'service_project_link': service_project_link,
'resources': [apps.get_model(r) for r in service['resources'].keys()],
'properties': [apps.get_model(r) for r in service['properties'].keys() if '.' in r],
}
return data | python | def get_service_models(cls):
""" Get a list of service models.
{
...
'gitlab': {
"service": nodeconductor_gitlab.models.GitLabService,
"service_project_link": nodeconductor_gitlab.models.GitLabServiceProjectLink,
"resources": [
nodeconductor_gitlab.models.Group,
nodeconductor_gitlab.models.Project
],
},
...
}
"""
from django.apps import apps
data = {}
for key, service in cls._registry.items():
service_model = apps.get_model(service['model_name'])
service_project_link = service_model.projects.through
data[key] = {
'service': service_model,
'service_project_link': service_project_link,
'resources': [apps.get_model(r) for r in service['resources'].keys()],
'properties': [apps.get_model(r) for r in service['properties'].keys() if '.' in r],
}
return data | [
"def",
"get_service_models",
"(",
"cls",
")",
":",
"from",
"django",
".",
"apps",
"import",
"apps",
"data",
"=",
"{",
"}",
"for",
"key",
",",
"service",
"in",
"cls",
".",
"_registry",
".",
"items",
"(",
")",
":",
"service_model",
"=",
"apps",
".",
"get_model",
"(",
"service",
"[",
"'model_name'",
"]",
")",
"service_project_link",
"=",
"service_model",
".",
"projects",
".",
"through",
"data",
"[",
"key",
"]",
"=",
"{",
"'service'",
":",
"service_model",
",",
"'service_project_link'",
":",
"service_project_link",
",",
"'resources'",
":",
"[",
"apps",
".",
"get_model",
"(",
"r",
")",
"for",
"r",
"in",
"service",
"[",
"'resources'",
"]",
".",
"keys",
"(",
")",
"]",
",",
"'properties'",
":",
"[",
"apps",
".",
"get_model",
"(",
"r",
")",
"for",
"r",
"in",
"service",
"[",
"'properties'",
"]",
".",
"keys",
"(",
")",
"if",
"'.'",
"in",
"r",
"]",
",",
"}",
"return",
"data"
] | Get a list of service models.
{
...
'gitlab': {
"service": nodeconductor_gitlab.models.GitLabService,
"service_project_link": nodeconductor_gitlab.models.GitLabServiceProjectLink,
"resources": [
nodeconductor_gitlab.models.Group,
nodeconductor_gitlab.models.Project
],
},
...
} | [
"Get",
"a",
"list",
"of",
"service",
"models",
".",
"{",
"...",
"gitlab",
":",
"{",
"service",
":",
"nodeconductor_gitlab",
".",
"models",
".",
"GitLabService",
"service_project_link",
":",
"nodeconductor_gitlab",
".",
"models",
".",
"GitLabServiceProjectLink",
"resources",
":",
"[",
"nodeconductor_gitlab",
".",
"models",
".",
"Group",
"nodeconductor_gitlab",
".",
"models",
".",
"Project",
"]",
"}",
"...",
"}"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/structure/__init__.py#L252-L281 |
opennode/waldur-core | waldur_core/structure/__init__.py | SupportedServices.get_resource_models | def get_resource_models(cls):
""" Get a list of resource models.
{
'DigitalOcean.Droplet': waldur_digitalocean.models.Droplet,
'JIRA.Project': waldur_jira.models.Project,
'OpenStack.Tenant': waldur_openstack.models.Tenant
}
"""
from django.apps import apps
return {'.'.join([service['name'], attrs['name']]): apps.get_model(resource)
for service in cls._registry.values()
for resource, attrs in service['resources'].items()} | python | def get_resource_models(cls):
""" Get a list of resource models.
{
'DigitalOcean.Droplet': waldur_digitalocean.models.Droplet,
'JIRA.Project': waldur_jira.models.Project,
'OpenStack.Tenant': waldur_openstack.models.Tenant
}
"""
from django.apps import apps
return {'.'.join([service['name'], attrs['name']]): apps.get_model(resource)
for service in cls._registry.values()
for resource, attrs in service['resources'].items()} | [
"def",
"get_resource_models",
"(",
"cls",
")",
":",
"from",
"django",
".",
"apps",
"import",
"apps",
"return",
"{",
"'.'",
".",
"join",
"(",
"[",
"service",
"[",
"'name'",
"]",
",",
"attrs",
"[",
"'name'",
"]",
"]",
")",
":",
"apps",
".",
"get_model",
"(",
"resource",
")",
"for",
"service",
"in",
"cls",
".",
"_registry",
".",
"values",
"(",
")",
"for",
"resource",
",",
"attrs",
"in",
"service",
"[",
"'resources'",
"]",
".",
"items",
"(",
")",
"}"
] | Get a list of resource models.
{
'DigitalOcean.Droplet': waldur_digitalocean.models.Droplet,
'JIRA.Project': waldur_jira.models.Project,
'OpenStack.Tenant': waldur_openstack.models.Tenant
} | [
"Get",
"a",
"list",
"of",
"resource",
"models",
".",
"{",
"DigitalOcean",
".",
"Droplet",
":",
"waldur_digitalocean",
".",
"models",
".",
"Droplet",
"JIRA",
".",
"Project",
":",
"waldur_jira",
".",
"models",
".",
"Project",
"OpenStack",
".",
"Tenant",
":",
"waldur_openstack",
".",
"models",
".",
"Tenant",
"}"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/structure/__init__.py#L285-L298 |
opennode/waldur-core | waldur_core/structure/__init__.py | SupportedServices.get_service_resources | def get_service_resources(cls, model):
""" Get resource models by service model """
key = cls.get_model_key(model)
return cls.get_service_name_resources(key) | python | def get_service_resources(cls, model):
""" Get resource models by service model """
key = cls.get_model_key(model)
return cls.get_service_name_resources(key) | [
"def",
"get_service_resources",
"(",
"cls",
",",
"model",
")",
":",
"key",
"=",
"cls",
".",
"get_model_key",
"(",
"model",
")",
"return",
"cls",
".",
"get_service_name_resources",
"(",
"key",
")"
] | Get resource models by service model | [
"Get",
"resource",
"models",
"by",
"service",
"model"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/structure/__init__.py#L302-L305 |
opennode/waldur-core | waldur_core/structure/__init__.py | SupportedServices.get_service_name_resources | def get_service_name_resources(cls, service_name):
""" Get resource models by service name """
from django.apps import apps
resources = cls._registry[service_name]['resources'].keys()
return [apps.get_model(resource) for resource in resources] | python | def get_service_name_resources(cls, service_name):
""" Get resource models by service name """
from django.apps import apps
resources = cls._registry[service_name]['resources'].keys()
return [apps.get_model(resource) for resource in resources] | [
"def",
"get_service_name_resources",
"(",
"cls",
",",
"service_name",
")",
":",
"from",
"django",
".",
"apps",
"import",
"apps",
"resources",
"=",
"cls",
".",
"_registry",
"[",
"service_name",
"]",
"[",
"'resources'",
"]",
".",
"keys",
"(",
")",
"return",
"[",
"apps",
".",
"get_model",
"(",
"resource",
")",
"for",
"resource",
"in",
"resources",
"]"
] | Get resource models by service name | [
"Get",
"resource",
"models",
"by",
"service",
"name"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/structure/__init__.py#L309-L314 |
opennode/waldur-core | waldur_core/structure/__init__.py | SupportedServices.get_name_for_model | def get_name_for_model(cls, model):
""" Get a name for given class or model:
-- it's a service type for a service
-- it's a <service_type>.<resource_model_name> for a resource
"""
key = cls.get_model_key(model)
model_str = cls._get_model_str(model)
service = cls._registry[key]
if model_str in service['resources']:
return '{}.{}'.format(service['name'], service['resources'][model_str]['name'])
else:
return service['name'] | python | def get_name_for_model(cls, model):
""" Get a name for given class or model:
-- it's a service type for a service
-- it's a <service_type>.<resource_model_name> for a resource
"""
key = cls.get_model_key(model)
model_str = cls._get_model_str(model)
service = cls._registry[key]
if model_str in service['resources']:
return '{}.{}'.format(service['name'], service['resources'][model_str]['name'])
else:
return service['name'] | [
"def",
"get_name_for_model",
"(",
"cls",
",",
"model",
")",
":",
"key",
"=",
"cls",
".",
"get_model_key",
"(",
"model",
")",
"model_str",
"=",
"cls",
".",
"_get_model_str",
"(",
"model",
")",
"service",
"=",
"cls",
".",
"_registry",
"[",
"key",
"]",
"if",
"model_str",
"in",
"service",
"[",
"'resources'",
"]",
":",
"return",
"'{}.{}'",
".",
"format",
"(",
"service",
"[",
"'name'",
"]",
",",
"service",
"[",
"'resources'",
"]",
"[",
"model_str",
"]",
"[",
"'name'",
"]",
")",
"else",
":",
"return",
"service",
"[",
"'name'",
"]"
] | Get a name for given class or model:
-- it's a service type for a service
-- it's a <service_type>.<resource_model_name> for a resource | [
"Get",
"a",
"name",
"for",
"given",
"class",
"or",
"model",
":",
"--",
"it",
"s",
"a",
"service",
"type",
"for",
"a",
"service",
"--",
"it",
"s",
"a",
"<service_type",
">",
".",
"<resource_model_name",
">",
"for",
"a",
"resource"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/structure/__init__.py#L317-L328 |
opennode/waldur-core | waldur_core/structure/__init__.py | SupportedServices.get_related_models | def get_related_models(cls, model):
""" Get a dictionary with related structure models for given class or model:
>> SupportedServices.get_related_models(gitlab_models.Project)
{
'service': nodeconductor_gitlab.models.GitLabService,
'service_project_link': nodeconductor_gitlab.models.GitLabServiceProjectLink,
'resources': [
nodeconductor_gitlab.models.Group,
nodeconductor_gitlab.models.Project,
]
}
"""
from waldur_core.structure.models import ServiceSettings
if isinstance(model, ServiceSettings):
model_str = cls._registry.get(model.type, {}).get('model_name', '')
else:
model_str = cls._get_model_str(model)
for models in cls.get_service_models().values():
if model_str == cls._get_model_str(models['service']) or \
model_str == cls._get_model_str(models['service_project_link']):
return models
for resource_model in models['resources']:
if model_str == cls._get_model_str(resource_model):
return models | python | def get_related_models(cls, model):
""" Get a dictionary with related structure models for given class or model:
>> SupportedServices.get_related_models(gitlab_models.Project)
{
'service': nodeconductor_gitlab.models.GitLabService,
'service_project_link': nodeconductor_gitlab.models.GitLabServiceProjectLink,
'resources': [
nodeconductor_gitlab.models.Group,
nodeconductor_gitlab.models.Project,
]
}
"""
from waldur_core.structure.models import ServiceSettings
if isinstance(model, ServiceSettings):
model_str = cls._registry.get(model.type, {}).get('model_name', '')
else:
model_str = cls._get_model_str(model)
for models in cls.get_service_models().values():
if model_str == cls._get_model_str(models['service']) or \
model_str == cls._get_model_str(models['service_project_link']):
return models
for resource_model in models['resources']:
if model_str == cls._get_model_str(resource_model):
return models | [
"def",
"get_related_models",
"(",
"cls",
",",
"model",
")",
":",
"from",
"waldur_core",
".",
"structure",
".",
"models",
"import",
"ServiceSettings",
"if",
"isinstance",
"(",
"model",
",",
"ServiceSettings",
")",
":",
"model_str",
"=",
"cls",
".",
"_registry",
".",
"get",
"(",
"model",
".",
"type",
",",
"{",
"}",
")",
".",
"get",
"(",
"'model_name'",
",",
"''",
")",
"else",
":",
"model_str",
"=",
"cls",
".",
"_get_model_str",
"(",
"model",
")",
"for",
"models",
"in",
"cls",
".",
"get_service_models",
"(",
")",
".",
"values",
"(",
")",
":",
"if",
"model_str",
"==",
"cls",
".",
"_get_model_str",
"(",
"models",
"[",
"'service'",
"]",
")",
"or",
"model_str",
"==",
"cls",
".",
"_get_model_str",
"(",
"models",
"[",
"'service_project_link'",
"]",
")",
":",
"return",
"models",
"for",
"resource_model",
"in",
"models",
"[",
"'resources'",
"]",
":",
"if",
"model_str",
"==",
"cls",
".",
"_get_model_str",
"(",
"resource_model",
")",
":",
"return",
"models"
] | Get a dictionary with related structure models for given class or model:
>> SupportedServices.get_related_models(gitlab_models.Project)
{
'service': nodeconductor_gitlab.models.GitLabService,
'service_project_link': nodeconductor_gitlab.models.GitLabServiceProjectLink,
'resources': [
nodeconductor_gitlab.models.Group,
nodeconductor_gitlab.models.Project,
]
} | [
"Get",
"a",
"dictionary",
"with",
"related",
"structure",
"models",
"for",
"given",
"class",
"or",
"model",
":"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/structure/__init__.py#L331-L358 |
opennode/waldur-core | waldur_core/structure/__init__.py | SupportedServices._is_active_model | def _is_active_model(cls, model):
""" Check is model app name is in list of INSTALLED_APPS """
# We need to use such tricky way to check because of inconsistent apps names:
# some apps are included in format "<module_name>.<app_name>" like "waldur_core.openstack"
# other apps are included in format "<app_name>" like "nodecondcutor_sugarcrm"
return ('.'.join(model.__module__.split('.')[:2]) in settings.INSTALLED_APPS or
'.'.join(model.__module__.split('.')[:1]) in settings.INSTALLED_APPS) | python | def _is_active_model(cls, model):
""" Check is model app name is in list of INSTALLED_APPS """
# We need to use such tricky way to check because of inconsistent apps names:
# some apps are included in format "<module_name>.<app_name>" like "waldur_core.openstack"
# other apps are included in format "<app_name>" like "nodecondcutor_sugarcrm"
return ('.'.join(model.__module__.split('.')[:2]) in settings.INSTALLED_APPS or
'.'.join(model.__module__.split('.')[:1]) in settings.INSTALLED_APPS) | [
"def",
"_is_active_model",
"(",
"cls",
",",
"model",
")",
":",
"# We need to use such tricky way to check because of inconsistent apps names:",
"# some apps are included in format \"<module_name>.<app_name>\" like \"waldur_core.openstack\"",
"# other apps are included in format \"<app_name>\" like \"nodecondcutor_sugarcrm\"",
"return",
"(",
"'.'",
".",
"join",
"(",
"model",
".",
"__module__",
".",
"split",
"(",
"'.'",
")",
"[",
":",
"2",
"]",
")",
"in",
"settings",
".",
"INSTALLED_APPS",
"or",
"'.'",
".",
"join",
"(",
"model",
".",
"__module__",
".",
"split",
"(",
"'.'",
")",
"[",
":",
"1",
"]",
")",
"in",
"settings",
".",
"INSTALLED_APPS",
")"
] | Check is model app name is in list of INSTALLED_APPS | [
"Check",
"is",
"model",
"app",
"name",
"is",
"in",
"list",
"of",
"INSTALLED_APPS"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/structure/__init__.py#L361-L367 |
opennode/waldur-core | waldur_core/logging/models.py | PushHook.process | def process(self, event):
""" Send events as push notification via Google Cloud Messaging.
Expected settings as follows:
# https://developers.google.com/mobile/add
WALDUR_CORE['GOOGLE_API'] = {
'NOTIFICATION_TITLE': "Waldur notification",
'Android': {
'server_key': 'AIzaSyA2_7UaVIxXfKeFvxTjQNZbrzkXG9OTCkg',
},
'iOS': {
'server_key': 'AIzaSyA34zlG_y5uHOe2FmcJKwfk2vG-3RW05vk',
}
}
"""
conf = settings.WALDUR_CORE.get('GOOGLE_API') or {}
keys = conf.get(dict(self.Type.CHOICES)[self.type])
if not keys or not self.token:
return
endpoint = 'https://gcm-http.googleapis.com/gcm/send'
headers = {
'Content-Type': 'application/json',
'Authorization': 'key=%s' % keys['server_key'],
}
payload = {
'to': self.token,
'notification': {
'body': event.get('message', 'New event'),
'title': conf.get('NOTIFICATION_TITLE', 'Waldur notification'),
'image': 'icon',
},
'data': {
'event': event
},
}
if self.type == self.Type.IOS:
payload['content-available'] = '1'
logger.debug('Submitting GCM push notification with headers %s, payload: %s' % (headers, payload))
requests.post(endpoint, json=payload, headers=headers) | python | def process(self, event):
""" Send events as push notification via Google Cloud Messaging.
Expected settings as follows:
# https://developers.google.com/mobile/add
WALDUR_CORE['GOOGLE_API'] = {
'NOTIFICATION_TITLE': "Waldur notification",
'Android': {
'server_key': 'AIzaSyA2_7UaVIxXfKeFvxTjQNZbrzkXG9OTCkg',
},
'iOS': {
'server_key': 'AIzaSyA34zlG_y5uHOe2FmcJKwfk2vG-3RW05vk',
}
}
"""
conf = settings.WALDUR_CORE.get('GOOGLE_API') or {}
keys = conf.get(dict(self.Type.CHOICES)[self.type])
if not keys or not self.token:
return
endpoint = 'https://gcm-http.googleapis.com/gcm/send'
headers = {
'Content-Type': 'application/json',
'Authorization': 'key=%s' % keys['server_key'],
}
payload = {
'to': self.token,
'notification': {
'body': event.get('message', 'New event'),
'title': conf.get('NOTIFICATION_TITLE', 'Waldur notification'),
'image': 'icon',
},
'data': {
'event': event
},
}
if self.type == self.Type.IOS:
payload['content-available'] = '1'
logger.debug('Submitting GCM push notification with headers %s, payload: %s' % (headers, payload))
requests.post(endpoint, json=payload, headers=headers) | [
"def",
"process",
"(",
"self",
",",
"event",
")",
":",
"conf",
"=",
"settings",
".",
"WALDUR_CORE",
".",
"get",
"(",
"'GOOGLE_API'",
")",
"or",
"{",
"}",
"keys",
"=",
"conf",
".",
"get",
"(",
"dict",
"(",
"self",
".",
"Type",
".",
"CHOICES",
")",
"[",
"self",
".",
"type",
"]",
")",
"if",
"not",
"keys",
"or",
"not",
"self",
".",
"token",
":",
"return",
"endpoint",
"=",
"'https://gcm-http.googleapis.com/gcm/send'",
"headers",
"=",
"{",
"'Content-Type'",
":",
"'application/json'",
",",
"'Authorization'",
":",
"'key=%s'",
"%",
"keys",
"[",
"'server_key'",
"]",
",",
"}",
"payload",
"=",
"{",
"'to'",
":",
"self",
".",
"token",
",",
"'notification'",
":",
"{",
"'body'",
":",
"event",
".",
"get",
"(",
"'message'",
",",
"'New event'",
")",
",",
"'title'",
":",
"conf",
".",
"get",
"(",
"'NOTIFICATION_TITLE'",
",",
"'Waldur notification'",
")",
",",
"'image'",
":",
"'icon'",
",",
"}",
",",
"'data'",
":",
"{",
"'event'",
":",
"event",
"}",
",",
"}",
"if",
"self",
".",
"type",
"==",
"self",
".",
"Type",
".",
"IOS",
":",
"payload",
"[",
"'content-available'",
"]",
"=",
"'1'",
"logger",
".",
"debug",
"(",
"'Submitting GCM push notification with headers %s, payload: %s'",
"%",
"(",
"headers",
",",
"payload",
")",
")",
"requests",
".",
"post",
"(",
"endpoint",
",",
"json",
"=",
"payload",
",",
"headers",
"=",
"headers",
")"
] | Send events as push notification via Google Cloud Messaging.
Expected settings as follows:
# https://developers.google.com/mobile/add
WALDUR_CORE['GOOGLE_API'] = {
'NOTIFICATION_TITLE': "Waldur notification",
'Android': {
'server_key': 'AIzaSyA2_7UaVIxXfKeFvxTjQNZbrzkXG9OTCkg',
},
'iOS': {
'server_key': 'AIzaSyA34zlG_y5uHOe2FmcJKwfk2vG-3RW05vk',
}
} | [
"Send",
"events",
"as",
"push",
"notification",
"via",
"Google",
"Cloud",
"Messaging",
".",
"Expected",
"settings",
"as",
"follows",
":"
] | train | https://github.com/opennode/waldur-core/blob/d6c17a9592bb6c49c33567542eef8d099605a46a/waldur_core/logging/models.py#L200-L241 |
stepank/pyws | src/pyws/protocols/soap/__init__.py | get_context_data_from_headers | def get_context_data_from_headers(request, headers_schema):
"""
Extracts context data from request headers according to specified schema.
>>> from lxml import etree as et
>>> from datetime import date
>>> from pyws.functions.args import TypeFactory
>>> Fake = type('Fake', (object, ), {})
>>> request = Fake()
>>> request.parsed_data = Fake()
>>> request.parsed_data.xml = et.fromstring(
... '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">'
... '<s:Header>'
... '<headers>'
... '<string>hello</string>'
... '<number>100</number>'
... '<date>2011-08-12</date>'
... '</headers>'
... '</s:Header>'
... '</s:Envelope>')
>>> data = get_context_data_from_headers(request, TypeFactory(
... {0: 'Headers', 'string': str, 'number': int, 'date': date}))
>>> data == {'string': 'hello', 'number': 100, 'date': date(2011, 8, 12)}
True
"""
if not headers_schema:
return None
env = request.parsed_data.xml.xpath(
'/soap:Envelope', namespaces=SoapProtocol.namespaces)[0]
header = env.xpath(
'./soap:Header/*', namespaces=SoapProtocol.namespaces)
if len(header) < 1:
return None
return headers_schema.validate(xml2obj(header[0], headers_schema)) | python | def get_context_data_from_headers(request, headers_schema):
"""
Extracts context data from request headers according to specified schema.
>>> from lxml import etree as et
>>> from datetime import date
>>> from pyws.functions.args import TypeFactory
>>> Fake = type('Fake', (object, ), {})
>>> request = Fake()
>>> request.parsed_data = Fake()
>>> request.parsed_data.xml = et.fromstring(
... '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">'
... '<s:Header>'
... '<headers>'
... '<string>hello</string>'
... '<number>100</number>'
... '<date>2011-08-12</date>'
... '</headers>'
... '</s:Header>'
... '</s:Envelope>')
>>> data = get_context_data_from_headers(request, TypeFactory(
... {0: 'Headers', 'string': str, 'number': int, 'date': date}))
>>> data == {'string': 'hello', 'number': 100, 'date': date(2011, 8, 12)}
True
"""
if not headers_schema:
return None
env = request.parsed_data.xml.xpath(
'/soap:Envelope', namespaces=SoapProtocol.namespaces)[0]
header = env.xpath(
'./soap:Header/*', namespaces=SoapProtocol.namespaces)
if len(header) < 1:
return None
return headers_schema.validate(xml2obj(header[0], headers_schema)) | [
"def",
"get_context_data_from_headers",
"(",
"request",
",",
"headers_schema",
")",
":",
"if",
"not",
"headers_schema",
":",
"return",
"None",
"env",
"=",
"request",
".",
"parsed_data",
".",
"xml",
".",
"xpath",
"(",
"'/soap:Envelope'",
",",
"namespaces",
"=",
"SoapProtocol",
".",
"namespaces",
")",
"[",
"0",
"]",
"header",
"=",
"env",
".",
"xpath",
"(",
"'./soap:Header/*'",
",",
"namespaces",
"=",
"SoapProtocol",
".",
"namespaces",
")",
"if",
"len",
"(",
"header",
")",
"<",
"1",
":",
"return",
"None",
"return",
"headers_schema",
".",
"validate",
"(",
"xml2obj",
"(",
"header",
"[",
"0",
"]",
",",
"headers_schema",
")",
")"
] | Extracts context data from request headers according to specified schema.
>>> from lxml import etree as et
>>> from datetime import date
>>> from pyws.functions.args import TypeFactory
>>> Fake = type('Fake', (object, ), {})
>>> request = Fake()
>>> request.parsed_data = Fake()
>>> request.parsed_data.xml = et.fromstring(
... '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">'
... '<s:Header>'
... '<headers>'
... '<string>hello</string>'
... '<number>100</number>'
... '<date>2011-08-12</date>'
... '</headers>'
... '</s:Header>'
... '</s:Envelope>')
>>> data = get_context_data_from_headers(request, TypeFactory(
... {0: 'Headers', 'string': str, 'number': int, 'date': date}))
>>> data == {'string': 'hello', 'number': 100, 'date': date(2011, 8, 12)}
True | [
"Extracts",
"context",
"data",
"from",
"request",
"headers",
"according",
"to",
"specified",
"schema",
"."
] | train | https://github.com/stepank/pyws/blob/ff39133aabeb56bbb08d66286ac0cc8731eda7dd/src/pyws/protocols/soap/__init__.py#L94-L131 |
quora/qcore | qcore/caching.py | lazy_constant | def lazy_constant(fn):
"""Decorator to make a function that takes no arguments use the LazyConstant class."""
class NewLazyConstant(LazyConstant):
@functools.wraps(fn)
def __call__(self):
return self.get_value()
return NewLazyConstant(fn) | python | def lazy_constant(fn):
"""Decorator to make a function that takes no arguments use the LazyConstant class."""
class NewLazyConstant(LazyConstant):
@functools.wraps(fn)
def __call__(self):
return self.get_value()
return NewLazyConstant(fn) | [
"def",
"lazy_constant",
"(",
"fn",
")",
":",
"class",
"NewLazyConstant",
"(",
"LazyConstant",
")",
":",
"@",
"functools",
".",
"wraps",
"(",
"fn",
")",
"def",
"__call__",
"(",
"self",
")",
":",
"return",
"self",
".",
"get_value",
"(",
")",
"return",
"NewLazyConstant",
"(",
"fn",
")"
] | Decorator to make a function that takes no arguments use the LazyConstant class. | [
"Decorator",
"to",
"make",
"a",
"function",
"that",
"takes",
"no",
"arguments",
"use",
"the",
"LazyConstant",
"class",
"."
] | train | https://github.com/quora/qcore/blob/fa5cd438eea554db35fd29cbc8dfbde69f09961c/qcore/caching.py#L84-L92 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.