repo
stringlengths 7
55
| path
stringlengths 4
127
| func_name
stringlengths 1
88
| original_string
stringlengths 75
19.8k
| language
stringclasses 1
value | code
stringlengths 75
19.8k
| code_tokens
list | docstring
stringlengths 3
17.3k
| docstring_tokens
list | sha
stringlengths 40
40
| url
stringlengths 87
242
| partition
stringclasses 1
value |
---|---|---|---|---|---|---|---|---|---|---|---|
dahlia/sqlalchemy-imageattach
|
sqlalchemy_imageattach/entity.py
|
Image.locate
|
def locate(self, store=current_store):
"""Gets the URL of the image from the ``store``.
:param store: the storage which contains the image.
:data:`~sqlalchemy_imageattach.context.current_store`
by default
:type store: :class:`~sqlalchemy_imageattach.store.Store`
:returns: the url of the image
:rtype: :class:`str`
"""
if not isinstance(store, Store):
raise TypeError('store must be an instance of '
'sqlalchemy_imageattach.store.Store, not ' +
repr(store))
return store.locate(self)
|
python
|
def locate(self, store=current_store):
"""Gets the URL of the image from the ``store``.
:param store: the storage which contains the image.
:data:`~sqlalchemy_imageattach.context.current_store`
by default
:type store: :class:`~sqlalchemy_imageattach.store.Store`
:returns: the url of the image
:rtype: :class:`str`
"""
if not isinstance(store, Store):
raise TypeError('store must be an instance of '
'sqlalchemy_imageattach.store.Store, not ' +
repr(store))
return store.locate(self)
|
[
"def",
"locate",
"(",
"self",
",",
"store",
"=",
"current_store",
")",
":",
"if",
"not",
"isinstance",
"(",
"store",
",",
"Store",
")",
":",
"raise",
"TypeError",
"(",
"'store must be an instance of '",
"'sqlalchemy_imageattach.store.Store, not '",
"+",
"repr",
"(",
"store",
")",
")",
"return",
"store",
".",
"locate",
"(",
"self",
")"
] |
Gets the URL of the image from the ``store``.
:param store: the storage which contains the image.
:data:`~sqlalchemy_imageattach.context.current_store`
by default
:type store: :class:`~sqlalchemy_imageattach.store.Store`
:returns: the url of the image
:rtype: :class:`str`
|
[
"Gets",
"the",
"URL",
"of",
"the",
"image",
"from",
"the",
"store",
"."
] |
b4bafa73f3bb576ecf67ed7b40b702704a0fbdc8
|
https://github.com/dahlia/sqlalchemy-imageattach/blob/b4bafa73f3bb576ecf67ed7b40b702704a0fbdc8/sqlalchemy_imageattach/entity.py#L323-L338
|
train
|
dahlia/sqlalchemy-imageattach
|
sqlalchemy_imageattach/entity.py
|
BaseImageQuery._original_images
|
def _original_images(self, **kwargs):
"""A list of the original images.
:returns: A list of the original images.
:rtype: :class:`typing.Sequence`\ [:class:`Image`]
"""
def test(image):
if not image.original:
return False
for filter, value in kwargs.items():
if getattr(image, filter) != value:
return False
return True
if Session.object_session(self.instance) is None:
images = []
for image, store in self._stored_images:
if test(image):
images.append(image)
state = instance_state(self.instance)
try:
added = state.committed_state[self.attr.key].added_items
except KeyError:
pass
else:
for image in added:
if test(image):
images.append(image)
if self.session:
for image in self.session.new:
if test(image):
images.append(image)
else:
query = self.filter_by(original=True, **kwargs)
images = query.all()
return images
|
python
|
def _original_images(self, **kwargs):
"""A list of the original images.
:returns: A list of the original images.
:rtype: :class:`typing.Sequence`\ [:class:`Image`]
"""
def test(image):
if not image.original:
return False
for filter, value in kwargs.items():
if getattr(image, filter) != value:
return False
return True
if Session.object_session(self.instance) is None:
images = []
for image, store in self._stored_images:
if test(image):
images.append(image)
state = instance_state(self.instance)
try:
added = state.committed_state[self.attr.key].added_items
except KeyError:
pass
else:
for image in added:
if test(image):
images.append(image)
if self.session:
for image in self.session.new:
if test(image):
images.append(image)
else:
query = self.filter_by(original=True, **kwargs)
images = query.all()
return images
|
[
"def",
"_original_images",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"def",
"test",
"(",
"image",
")",
":",
"if",
"not",
"image",
".",
"original",
":",
"return",
"False",
"for",
"filter",
",",
"value",
"in",
"kwargs",
".",
"items",
"(",
")",
":",
"if",
"getattr",
"(",
"image",
",",
"filter",
")",
"!=",
"value",
":",
"return",
"False",
"return",
"True",
"if",
"Session",
".",
"object_session",
"(",
"self",
".",
"instance",
")",
"is",
"None",
":",
"images",
"=",
"[",
"]",
"for",
"image",
",",
"store",
"in",
"self",
".",
"_stored_images",
":",
"if",
"test",
"(",
"image",
")",
":",
"images",
".",
"append",
"(",
"image",
")",
"state",
"=",
"instance_state",
"(",
"self",
".",
"instance",
")",
"try",
":",
"added",
"=",
"state",
".",
"committed_state",
"[",
"self",
".",
"attr",
".",
"key",
"]",
".",
"added_items",
"except",
"KeyError",
":",
"pass",
"else",
":",
"for",
"image",
"in",
"added",
":",
"if",
"test",
"(",
"image",
")",
":",
"images",
".",
"append",
"(",
"image",
")",
"if",
"self",
".",
"session",
":",
"for",
"image",
"in",
"self",
".",
"session",
".",
"new",
":",
"if",
"test",
"(",
"image",
")",
":",
"images",
".",
"append",
"(",
"image",
")",
"else",
":",
"query",
"=",
"self",
".",
"filter_by",
"(",
"original",
"=",
"True",
",",
"*",
"*",
"kwargs",
")",
"images",
"=",
"query",
".",
"all",
"(",
")",
"return",
"images"
] |
A list of the original images.
:returns: A list of the original images.
:rtype: :class:`typing.Sequence`\ [:class:`Image`]
|
[
"A",
"list",
"of",
"the",
"original",
"images",
"."
] |
b4bafa73f3bb576ecf67ed7b40b702704a0fbdc8
|
https://github.com/dahlia/sqlalchemy-imageattach/blob/b4bafa73f3bb576ecf67ed7b40b702704a0fbdc8/sqlalchemy_imageattach/entity.py#L457-L494
|
train
|
dahlia/sqlalchemy-imageattach
|
sqlalchemy_imageattach/entity.py
|
BaseImageSet.from_file
|
def from_file(self, file, store=current_store,
extra_args=None, extra_kwargs=None):
"""Stores the ``file`` for the image into the ``store``.
:param file: the readable file of the image
:type file: file-like object, :class:`file`
:param store: the storage to store the file.
:data:`~sqlalchemy_imageattach.context.current_store`
by default
:type store: :class:`~sqlalchemy_imageattach.store.Store`
:param extra_args: additional arguments to pass to the model's
constructor.
:type extra_args: :class:`collections.abc.Sequence`
:param extra_kwargs: additional keyword arguments to pass to the
model's constructor.
:type extra_kwargs: :class:`typing.Mapping`\ [:class:`str`,
:class:`object`]
:returns: the created image instance
:rtype: :class:`Image`
.. versionadded:: 1.0.0
The ``extra_args`` and ``extra_kwargs`` options.
"""
if isinstance(file, cgi.FieldStorage):
file = file.file
data = io.BytesIO()
shutil.copyfileobj(file, data)
data.seek(0)
return self.from_raw_file(data, store, original=True,
extra_args=extra_args,
extra_kwargs=extra_kwargs)
|
python
|
def from_file(self, file, store=current_store,
extra_args=None, extra_kwargs=None):
"""Stores the ``file`` for the image into the ``store``.
:param file: the readable file of the image
:type file: file-like object, :class:`file`
:param store: the storage to store the file.
:data:`~sqlalchemy_imageattach.context.current_store`
by default
:type store: :class:`~sqlalchemy_imageattach.store.Store`
:param extra_args: additional arguments to pass to the model's
constructor.
:type extra_args: :class:`collections.abc.Sequence`
:param extra_kwargs: additional keyword arguments to pass to the
model's constructor.
:type extra_kwargs: :class:`typing.Mapping`\ [:class:`str`,
:class:`object`]
:returns: the created image instance
:rtype: :class:`Image`
.. versionadded:: 1.0.0
The ``extra_args`` and ``extra_kwargs`` options.
"""
if isinstance(file, cgi.FieldStorage):
file = file.file
data = io.BytesIO()
shutil.copyfileobj(file, data)
data.seek(0)
return self.from_raw_file(data, store, original=True,
extra_args=extra_args,
extra_kwargs=extra_kwargs)
|
[
"def",
"from_file",
"(",
"self",
",",
"file",
",",
"store",
"=",
"current_store",
",",
"extra_args",
"=",
"None",
",",
"extra_kwargs",
"=",
"None",
")",
":",
"if",
"isinstance",
"(",
"file",
",",
"cgi",
".",
"FieldStorage",
")",
":",
"file",
"=",
"file",
".",
"file",
"data",
"=",
"io",
".",
"BytesIO",
"(",
")",
"shutil",
".",
"copyfileobj",
"(",
"file",
",",
"data",
")",
"data",
".",
"seek",
"(",
"0",
")",
"return",
"self",
".",
"from_raw_file",
"(",
"data",
",",
"store",
",",
"original",
"=",
"True",
",",
"extra_args",
"=",
"extra_args",
",",
"extra_kwargs",
"=",
"extra_kwargs",
")"
] |
Stores the ``file`` for the image into the ``store``.
:param file: the readable file of the image
:type file: file-like object, :class:`file`
:param store: the storage to store the file.
:data:`~sqlalchemy_imageattach.context.current_store`
by default
:type store: :class:`~sqlalchemy_imageattach.store.Store`
:param extra_args: additional arguments to pass to the model's
constructor.
:type extra_args: :class:`collections.abc.Sequence`
:param extra_kwargs: additional keyword arguments to pass to the
model's constructor.
:type extra_kwargs: :class:`typing.Mapping`\ [:class:`str`,
:class:`object`]
:returns: the created image instance
:rtype: :class:`Image`
.. versionadded:: 1.0.0
The ``extra_args`` and ``extra_kwargs`` options.
|
[
"Stores",
"the",
"file",
"for",
"the",
"image",
"into",
"the",
"store",
"."
] |
b4bafa73f3bb576ecf67ed7b40b702704a0fbdc8
|
https://github.com/dahlia/sqlalchemy-imageattach/blob/b4bafa73f3bb576ecf67ed7b40b702704a0fbdc8/sqlalchemy_imageattach/entity.py#L655-L688
|
train
|
benmoran56/esper
|
esper.py
|
World.clear_database
|
def clear_database(self) -> None:
"""Remove all Entities and Components from the World."""
self._next_entity_id = 0
self._dead_entities.clear()
self._components.clear()
self._entities.clear()
self.clear_cache()
|
python
|
def clear_database(self) -> None:
"""Remove all Entities and Components from the World."""
self._next_entity_id = 0
self._dead_entities.clear()
self._components.clear()
self._entities.clear()
self.clear_cache()
|
[
"def",
"clear_database",
"(",
"self",
")",
"->",
"None",
":",
"self",
".",
"_next_entity_id",
"=",
"0",
"self",
".",
"_dead_entities",
".",
"clear",
"(",
")",
"self",
".",
"_components",
".",
"clear",
"(",
")",
"self",
".",
"_entities",
".",
"clear",
"(",
")",
"self",
".",
"clear_cache",
"(",
")"
] |
Remove all Entities and Components from the World.
|
[
"Remove",
"all",
"Entities",
"and",
"Components",
"from",
"the",
"World",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L46-L52
|
train
|
benmoran56/esper
|
esper.py
|
World.add_processor
|
def add_processor(self, processor_instance: Processor, priority=0) -> None:
"""Add a Processor instance to the World.
:param processor_instance: An instance of a Processor,
subclassed from the Processor class
:param priority: A higher number is processed first.
"""
assert issubclass(processor_instance.__class__, Processor)
processor_instance.priority = priority
processor_instance.world = self
self._processors.append(processor_instance)
self._processors.sort(key=lambda proc: proc.priority, reverse=True)
|
python
|
def add_processor(self, processor_instance: Processor, priority=0) -> None:
"""Add a Processor instance to the World.
:param processor_instance: An instance of a Processor,
subclassed from the Processor class
:param priority: A higher number is processed first.
"""
assert issubclass(processor_instance.__class__, Processor)
processor_instance.priority = priority
processor_instance.world = self
self._processors.append(processor_instance)
self._processors.sort(key=lambda proc: proc.priority, reverse=True)
|
[
"def",
"add_processor",
"(",
"self",
",",
"processor_instance",
":",
"Processor",
",",
"priority",
"=",
"0",
")",
"->",
"None",
":",
"assert",
"issubclass",
"(",
"processor_instance",
".",
"__class__",
",",
"Processor",
")",
"processor_instance",
".",
"priority",
"=",
"priority",
"processor_instance",
".",
"world",
"=",
"self",
"self",
".",
"_processors",
".",
"append",
"(",
"processor_instance",
")",
"self",
".",
"_processors",
".",
"sort",
"(",
"key",
"=",
"lambda",
"proc",
":",
"proc",
".",
"priority",
",",
"reverse",
"=",
"True",
")"
] |
Add a Processor instance to the World.
:param processor_instance: An instance of a Processor,
subclassed from the Processor class
:param priority: A higher number is processed first.
|
[
"Add",
"a",
"Processor",
"instance",
"to",
"the",
"World",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L54-L65
|
train
|
benmoran56/esper
|
esper.py
|
World.remove_processor
|
def remove_processor(self, processor_type: Processor) -> None:
"""Remove a Processor from the World, by type.
:param processor_type: The class type of the Processor to remove.
"""
for processor in self._processors:
if type(processor) == processor_type:
processor.world = None
self._processors.remove(processor)
|
python
|
def remove_processor(self, processor_type: Processor) -> None:
"""Remove a Processor from the World, by type.
:param processor_type: The class type of the Processor to remove.
"""
for processor in self._processors:
if type(processor) == processor_type:
processor.world = None
self._processors.remove(processor)
|
[
"def",
"remove_processor",
"(",
"self",
",",
"processor_type",
":",
"Processor",
")",
"->",
"None",
":",
"for",
"processor",
"in",
"self",
".",
"_processors",
":",
"if",
"type",
"(",
"processor",
")",
"==",
"processor_type",
":",
"processor",
".",
"world",
"=",
"None",
"self",
".",
"_processors",
".",
"remove",
"(",
"processor",
")"
] |
Remove a Processor from the World, by type.
:param processor_type: The class type of the Processor to remove.
|
[
"Remove",
"a",
"Processor",
"from",
"the",
"World",
"by",
"type",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L67-L75
|
train
|
benmoran56/esper
|
esper.py
|
World.get_processor
|
def get_processor(self, processor_type: Type[P]) -> P:
"""Get a Processor instance, by type.
This method returns a Processor instance by type. This could be
useful in certain situations, such as wanting to call a method on a
Processor, from within another Processor.
:param processor_type: The type of the Processor you wish to retrieve.
:return: A Processor instance that has previously been added to the World.
"""
for processor in self._processors:
if type(processor) == processor_type:
return processor
|
python
|
def get_processor(self, processor_type: Type[P]) -> P:
"""Get a Processor instance, by type.
This method returns a Processor instance by type. This could be
useful in certain situations, such as wanting to call a method on a
Processor, from within another Processor.
:param processor_type: The type of the Processor you wish to retrieve.
:return: A Processor instance that has previously been added to the World.
"""
for processor in self._processors:
if type(processor) == processor_type:
return processor
|
[
"def",
"get_processor",
"(",
"self",
",",
"processor_type",
":",
"Type",
"[",
"P",
"]",
")",
"->",
"P",
":",
"for",
"processor",
"in",
"self",
".",
"_processors",
":",
"if",
"type",
"(",
"processor",
")",
"==",
"processor_type",
":",
"return",
"processor"
] |
Get a Processor instance, by type.
This method returns a Processor instance by type. This could be
useful in certain situations, such as wanting to call a method on a
Processor, from within another Processor.
:param processor_type: The type of the Processor you wish to retrieve.
:return: A Processor instance that has previously been added to the World.
|
[
"Get",
"a",
"Processor",
"instance",
"by",
"type",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L77-L89
|
train
|
benmoran56/esper
|
esper.py
|
World.create_entity
|
def create_entity(self, *components) -> int:
"""Create a new Entity.
This method returns an Entity ID, which is just a plain integer.
You can optionally pass one or more Component instances to be
assigned to the Entity.
:param components: Optional components to be assigned to the
entity on creation.
:return: The next Entity ID in sequence.
"""
self._next_entity_id += 1
# TODO: duplicate add_component code here for performance
for component in components:
self.add_component(self._next_entity_id, component)
# self.clear_cache()
return self._next_entity_id
|
python
|
def create_entity(self, *components) -> int:
"""Create a new Entity.
This method returns an Entity ID, which is just a plain integer.
You can optionally pass one or more Component instances to be
assigned to the Entity.
:param components: Optional components to be assigned to the
entity on creation.
:return: The next Entity ID in sequence.
"""
self._next_entity_id += 1
# TODO: duplicate add_component code here for performance
for component in components:
self.add_component(self._next_entity_id, component)
# self.clear_cache()
return self._next_entity_id
|
[
"def",
"create_entity",
"(",
"self",
",",
"*",
"components",
")",
"->",
"int",
":",
"self",
".",
"_next_entity_id",
"+=",
"1",
"# TODO: duplicate add_component code here for performance",
"for",
"component",
"in",
"components",
":",
"self",
".",
"add_component",
"(",
"self",
".",
"_next_entity_id",
",",
"component",
")",
"# self.clear_cache()",
"return",
"self",
".",
"_next_entity_id"
] |
Create a new Entity.
This method returns an Entity ID, which is just a plain integer.
You can optionally pass one or more Component instances to be
assigned to the Entity.
:param components: Optional components to be assigned to the
entity on creation.
:return: The next Entity ID in sequence.
|
[
"Create",
"a",
"new",
"Entity",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L91-L109
|
train
|
benmoran56/esper
|
esper.py
|
World.delete_entity
|
def delete_entity(self, entity: int, immediate=False) -> None:
"""Delete an Entity from the World.
Delete an Entity and all of it's assigned Component instances from
the world. By default, Entity deletion is delayed until the next call
to *World.process*. You can request immediate deletion, however, by
passing the "immediate=True" parameter. This should generally not be
done during Entity iteration (calls to World.get_component/s).
Raises a KeyError if the given entity does not exist in the database.
:param entity: The Entity ID you wish to delete.
:param immediate: If True, delete the Entity immediately.
"""
if immediate:
for component_type in self._entities[entity]:
self._components[component_type].discard(entity)
if not self._components[component_type]:
del self._components[component_type]
del self._entities[entity]
self.clear_cache()
else:
self._dead_entities.add(entity)
|
python
|
def delete_entity(self, entity: int, immediate=False) -> None:
"""Delete an Entity from the World.
Delete an Entity and all of it's assigned Component instances from
the world. By default, Entity deletion is delayed until the next call
to *World.process*. You can request immediate deletion, however, by
passing the "immediate=True" parameter. This should generally not be
done during Entity iteration (calls to World.get_component/s).
Raises a KeyError if the given entity does not exist in the database.
:param entity: The Entity ID you wish to delete.
:param immediate: If True, delete the Entity immediately.
"""
if immediate:
for component_type in self._entities[entity]:
self._components[component_type].discard(entity)
if not self._components[component_type]:
del self._components[component_type]
del self._entities[entity]
self.clear_cache()
else:
self._dead_entities.add(entity)
|
[
"def",
"delete_entity",
"(",
"self",
",",
"entity",
":",
"int",
",",
"immediate",
"=",
"False",
")",
"->",
"None",
":",
"if",
"immediate",
":",
"for",
"component_type",
"in",
"self",
".",
"_entities",
"[",
"entity",
"]",
":",
"self",
".",
"_components",
"[",
"component_type",
"]",
".",
"discard",
"(",
"entity",
")",
"if",
"not",
"self",
".",
"_components",
"[",
"component_type",
"]",
":",
"del",
"self",
".",
"_components",
"[",
"component_type",
"]",
"del",
"self",
".",
"_entities",
"[",
"entity",
"]",
"self",
".",
"clear_cache",
"(",
")",
"else",
":",
"self",
".",
"_dead_entities",
".",
"add",
"(",
"entity",
")"
] |
Delete an Entity from the World.
Delete an Entity and all of it's assigned Component instances from
the world. By default, Entity deletion is delayed until the next call
to *World.process*. You can request immediate deletion, however, by
passing the "immediate=True" parameter. This should generally not be
done during Entity iteration (calls to World.get_component/s).
Raises a KeyError if the given entity does not exist in the database.
:param entity: The Entity ID you wish to delete.
:param immediate: If True, delete the Entity immediately.
|
[
"Delete",
"an",
"Entity",
"from",
"the",
"World",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L111-L135
|
train
|
benmoran56/esper
|
esper.py
|
World.component_for_entity
|
def component_for_entity(self, entity: int, component_type: Type[C]) -> C:
"""Retrieve a Component instance for a specific Entity.
Retrieve a Component instance for a specific Entity. In some cases,
it may be necessary to access a specific Component instance.
For example: directly modifying a Component to handle user input.
Raises a KeyError if the given Entity and Component do not exist.
:param entity: The Entity ID to retrieve the Component for.
:param component_type: The Component instance you wish to retrieve.
:return: The Component instance requested for the given Entity ID.
"""
return self._entities[entity][component_type]
|
python
|
def component_for_entity(self, entity: int, component_type: Type[C]) -> C:
"""Retrieve a Component instance for a specific Entity.
Retrieve a Component instance for a specific Entity. In some cases,
it may be necessary to access a specific Component instance.
For example: directly modifying a Component to handle user input.
Raises a KeyError if the given Entity and Component do not exist.
:param entity: The Entity ID to retrieve the Component for.
:param component_type: The Component instance you wish to retrieve.
:return: The Component instance requested for the given Entity ID.
"""
return self._entities[entity][component_type]
|
[
"def",
"component_for_entity",
"(",
"self",
",",
"entity",
":",
"int",
",",
"component_type",
":",
"Type",
"[",
"C",
"]",
")",
"->",
"C",
":",
"return",
"self",
".",
"_entities",
"[",
"entity",
"]",
"[",
"component_type",
"]"
] |
Retrieve a Component instance for a specific Entity.
Retrieve a Component instance for a specific Entity. In some cases,
it may be necessary to access a specific Component instance.
For example: directly modifying a Component to handle user input.
Raises a KeyError if the given Entity and Component do not exist.
:param entity: The Entity ID to retrieve the Component for.
:param component_type: The Component instance you wish to retrieve.
:return: The Component instance requested for the given Entity ID.
|
[
"Retrieve",
"a",
"Component",
"instance",
"for",
"a",
"specific",
"Entity",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L137-L149
|
train
|
benmoran56/esper
|
esper.py
|
World.components_for_entity
|
def components_for_entity(self, entity: int) -> Tuple[C, ...]:
"""Retrieve all Components for a specific Entity, as a Tuple.
Retrieve all Components for a specific Entity. The method is probably
not appropriate to use in your Processors, but might be useful for
saving state, or passing specific Components between World instances.
Unlike most other methods, this returns all of the Components as a
Tuple in one batch, instead of returning a Generator for iteration.
Raises a KeyError if the given entity does not exist in the database.
:param entity: The Entity ID to retrieve the Components for.
:return: A tuple of all Component instances that have been
assigned to the passed Entity ID.
"""
return tuple(self._entities[entity].values())
|
python
|
def components_for_entity(self, entity: int) -> Tuple[C, ...]:
"""Retrieve all Components for a specific Entity, as a Tuple.
Retrieve all Components for a specific Entity. The method is probably
not appropriate to use in your Processors, but might be useful for
saving state, or passing specific Components between World instances.
Unlike most other methods, this returns all of the Components as a
Tuple in one batch, instead of returning a Generator for iteration.
Raises a KeyError if the given entity does not exist in the database.
:param entity: The Entity ID to retrieve the Components for.
:return: A tuple of all Component instances that have been
assigned to the passed Entity ID.
"""
return tuple(self._entities[entity].values())
|
[
"def",
"components_for_entity",
"(",
"self",
",",
"entity",
":",
"int",
")",
"->",
"Tuple",
"[",
"C",
",",
"...",
"]",
":",
"return",
"tuple",
"(",
"self",
".",
"_entities",
"[",
"entity",
"]",
".",
"values",
"(",
")",
")"
] |
Retrieve all Components for a specific Entity, as a Tuple.
Retrieve all Components for a specific Entity. The method is probably
not appropriate to use in your Processors, but might be useful for
saving state, or passing specific Components between World instances.
Unlike most other methods, this returns all of the Components as a
Tuple in one batch, instead of returning a Generator for iteration.
Raises a KeyError if the given entity does not exist in the database.
:param entity: The Entity ID to retrieve the Components for.
:return: A tuple of all Component instances that have been
assigned to the passed Entity ID.
|
[
"Retrieve",
"all",
"Components",
"for",
"a",
"specific",
"Entity",
"as",
"a",
"Tuple",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L151-L165
|
train
|
benmoran56/esper
|
esper.py
|
World.has_component
|
def has_component(self, entity: int, component_type: Any) -> bool:
"""Check if a specific Entity has a Component of a certain type.
:param entity: The Entity you are querying.
:param component_type: The type of Component to check for.
:return: True if the Entity has a Component of this type,
otherwise False
"""
return component_type in self._entities[entity]
|
python
|
def has_component(self, entity: int, component_type: Any) -> bool:
"""Check if a specific Entity has a Component of a certain type.
:param entity: The Entity you are querying.
:param component_type: The type of Component to check for.
:return: True if the Entity has a Component of this type,
otherwise False
"""
return component_type in self._entities[entity]
|
[
"def",
"has_component",
"(",
"self",
",",
"entity",
":",
"int",
",",
"component_type",
":",
"Any",
")",
"->",
"bool",
":",
"return",
"component_type",
"in",
"self",
".",
"_entities",
"[",
"entity",
"]"
] |
Check if a specific Entity has a Component of a certain type.
:param entity: The Entity you are querying.
:param component_type: The type of Component to check for.
:return: True if the Entity has a Component of this type,
otherwise False
|
[
"Check",
"if",
"a",
"specific",
"Entity",
"has",
"a",
"Component",
"of",
"a",
"certain",
"type",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L167-L175
|
train
|
benmoran56/esper
|
esper.py
|
World.add_component
|
def add_component(self, entity: int, component_instance: Any) -> None:
"""Add a new Component instance to an Entity.
Add a Component instance to an Entiy. If a Component of the same type
is already assigned to the Entity, it will be replaced.
:param entity: The Entity to associate the Component with.
:param component_instance: A Component instance.
"""
component_type = type(component_instance)
if component_type not in self._components:
self._components[component_type] = set()
self._components[component_type].add(entity)
if entity not in self._entities:
self._entities[entity] = {}
self._entities[entity][component_type] = component_instance
self.clear_cache()
|
python
|
def add_component(self, entity: int, component_instance: Any) -> None:
"""Add a new Component instance to an Entity.
Add a Component instance to an Entiy. If a Component of the same type
is already assigned to the Entity, it will be replaced.
:param entity: The Entity to associate the Component with.
:param component_instance: A Component instance.
"""
component_type = type(component_instance)
if component_type not in self._components:
self._components[component_type] = set()
self._components[component_type].add(entity)
if entity not in self._entities:
self._entities[entity] = {}
self._entities[entity][component_type] = component_instance
self.clear_cache()
|
[
"def",
"add_component",
"(",
"self",
",",
"entity",
":",
"int",
",",
"component_instance",
":",
"Any",
")",
"->",
"None",
":",
"component_type",
"=",
"type",
"(",
"component_instance",
")",
"if",
"component_type",
"not",
"in",
"self",
".",
"_components",
":",
"self",
".",
"_components",
"[",
"component_type",
"]",
"=",
"set",
"(",
")",
"self",
".",
"_components",
"[",
"component_type",
"]",
".",
"add",
"(",
"entity",
")",
"if",
"entity",
"not",
"in",
"self",
".",
"_entities",
":",
"self",
".",
"_entities",
"[",
"entity",
"]",
"=",
"{",
"}",
"self",
".",
"_entities",
"[",
"entity",
"]",
"[",
"component_type",
"]",
"=",
"component_instance",
"self",
".",
"clear_cache",
"(",
")"
] |
Add a new Component instance to an Entity.
Add a Component instance to an Entiy. If a Component of the same type
is already assigned to the Entity, it will be replaced.
:param entity: The Entity to associate the Component with.
:param component_instance: A Component instance.
|
[
"Add",
"a",
"new",
"Component",
"instance",
"to",
"an",
"Entity",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L177-L197
|
train
|
benmoran56/esper
|
esper.py
|
World.remove_component
|
def remove_component(self, entity: int, component_type: Any) -> int:
"""Remove a Component instance from an Entity, by type.
A Component instance can be removed by providing it's type.
For example: world.delete_component(enemy_a, Velocity) will remove
the Velocity instance from the Entity enemy_a.
Raises a KeyError if either the given entity or Component type does
not exist in the database.
:param entity: The Entity to remove the Component from.
:param component_type: The type of the Component to remove.
"""
self._components[component_type].discard(entity)
if not self._components[component_type]:
del self._components[component_type]
del self._entities[entity][component_type]
if not self._entities[entity]:
del self._entities[entity]
self.clear_cache()
return entity
|
python
|
def remove_component(self, entity: int, component_type: Any) -> int:
"""Remove a Component instance from an Entity, by type.
A Component instance can be removed by providing it's type.
For example: world.delete_component(enemy_a, Velocity) will remove
the Velocity instance from the Entity enemy_a.
Raises a KeyError if either the given entity or Component type does
not exist in the database.
:param entity: The Entity to remove the Component from.
:param component_type: The type of the Component to remove.
"""
self._components[component_type].discard(entity)
if not self._components[component_type]:
del self._components[component_type]
del self._entities[entity][component_type]
if not self._entities[entity]:
del self._entities[entity]
self.clear_cache()
return entity
|
[
"def",
"remove_component",
"(",
"self",
",",
"entity",
":",
"int",
",",
"component_type",
":",
"Any",
")",
"->",
"int",
":",
"self",
".",
"_components",
"[",
"component_type",
"]",
".",
"discard",
"(",
"entity",
")",
"if",
"not",
"self",
".",
"_components",
"[",
"component_type",
"]",
":",
"del",
"self",
".",
"_components",
"[",
"component_type",
"]",
"del",
"self",
".",
"_entities",
"[",
"entity",
"]",
"[",
"component_type",
"]",
"if",
"not",
"self",
".",
"_entities",
"[",
"entity",
"]",
":",
"del",
"self",
".",
"_entities",
"[",
"entity",
"]",
"self",
".",
"clear_cache",
"(",
")",
"return",
"entity"
] |
Remove a Component instance from an Entity, by type.
A Component instance can be removed by providing it's type.
For example: world.delete_component(enemy_a, Velocity) will remove
the Velocity instance from the Entity enemy_a.
Raises a KeyError if either the given entity or Component type does
not exist in the database.
:param entity: The Entity to remove the Component from.
:param component_type: The type of the Component to remove.
|
[
"Remove",
"a",
"Component",
"instance",
"from",
"an",
"Entity",
"by",
"type",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L199-L222
|
train
|
benmoran56/esper
|
esper.py
|
World._get_component
|
def _get_component(self, component_type: Type[C]) -> Iterable[Tuple[int, C]]:
"""Get an iterator for Entity, Component pairs.
:param component_type: The Component type to retrieve.
:return: An iterator for (Entity, Component) tuples.
"""
entity_db = self._entities
for entity in self._components.get(component_type, []):
yield entity, entity_db[entity][component_type]
|
python
|
def _get_component(self, component_type: Type[C]) -> Iterable[Tuple[int, C]]:
"""Get an iterator for Entity, Component pairs.
:param component_type: The Component type to retrieve.
:return: An iterator for (Entity, Component) tuples.
"""
entity_db = self._entities
for entity in self._components.get(component_type, []):
yield entity, entity_db[entity][component_type]
|
[
"def",
"_get_component",
"(",
"self",
",",
"component_type",
":",
"Type",
"[",
"C",
"]",
")",
"->",
"Iterable",
"[",
"Tuple",
"[",
"int",
",",
"C",
"]",
"]",
":",
"entity_db",
"=",
"self",
".",
"_entities",
"for",
"entity",
"in",
"self",
".",
"_components",
".",
"get",
"(",
"component_type",
",",
"[",
"]",
")",
":",
"yield",
"entity",
",",
"entity_db",
"[",
"entity",
"]",
"[",
"component_type",
"]"
] |
Get an iterator for Entity, Component pairs.
:param component_type: The Component type to retrieve.
:return: An iterator for (Entity, Component) tuples.
|
[
"Get",
"an",
"iterator",
"for",
"Entity",
"Component",
"pairs",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L224-L233
|
train
|
benmoran56/esper
|
esper.py
|
World._get_components
|
def _get_components(self, *component_types: Type)-> Iterable[Tuple[int, ...]]:
"""Get an iterator for Entity and multiple Component sets.
:param component_types: Two or more Component types.
:return: An iterator for Entity, (Component1, Component2, etc)
tuples.
"""
entity_db = self._entities
comp_db = self._components
try:
for entity in set.intersection(*[comp_db[ct] for ct in component_types]):
yield entity, [entity_db[entity][ct] for ct in component_types]
except KeyError:
pass
|
python
|
def _get_components(self, *component_types: Type)-> Iterable[Tuple[int, ...]]:
"""Get an iterator for Entity and multiple Component sets.
:param component_types: Two or more Component types.
:return: An iterator for Entity, (Component1, Component2, etc)
tuples.
"""
entity_db = self._entities
comp_db = self._components
try:
for entity in set.intersection(*[comp_db[ct] for ct in component_types]):
yield entity, [entity_db[entity][ct] for ct in component_types]
except KeyError:
pass
|
[
"def",
"_get_components",
"(",
"self",
",",
"*",
"component_types",
":",
"Type",
")",
"->",
"Iterable",
"[",
"Tuple",
"[",
"int",
",",
"...",
"]",
"]",
":",
"entity_db",
"=",
"self",
".",
"_entities",
"comp_db",
"=",
"self",
".",
"_components",
"try",
":",
"for",
"entity",
"in",
"set",
".",
"intersection",
"(",
"*",
"[",
"comp_db",
"[",
"ct",
"]",
"for",
"ct",
"in",
"component_types",
"]",
")",
":",
"yield",
"entity",
",",
"[",
"entity_db",
"[",
"entity",
"]",
"[",
"ct",
"]",
"for",
"ct",
"in",
"component_types",
"]",
"except",
"KeyError",
":",
"pass"
] |
Get an iterator for Entity and multiple Component sets.
:param component_types: Two or more Component types.
:return: An iterator for Entity, (Component1, Component2, etc)
tuples.
|
[
"Get",
"an",
"iterator",
"for",
"Entity",
"and",
"multiple",
"Component",
"sets",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L235-L249
|
train
|
benmoran56/esper
|
esper.py
|
World.try_component
|
def try_component(self, entity: int, component_type: Type):
"""Try to get a single component type for an Entity.
This method will return the requested Component if it exists, but
will pass silently if it does not. This allows a way to access optional
Components that may or may not exist.
:param entity: The Entity ID to retrieve the Component for.
:param component_type: The Component instance you wish to retrieve.
:return: A iterator containg the single Component instance requested,
which is empty if the component doesn't exist.
"""
if component_type in self._entities[entity]:
yield self._entities[entity][component_type]
else:
return None
|
python
|
def try_component(self, entity: int, component_type: Type):
"""Try to get a single component type for an Entity.
This method will return the requested Component if it exists, but
will pass silently if it does not. This allows a way to access optional
Components that may or may not exist.
:param entity: The Entity ID to retrieve the Component for.
:param component_type: The Component instance you wish to retrieve.
:return: A iterator containg the single Component instance requested,
which is empty if the component doesn't exist.
"""
if component_type in self._entities[entity]:
yield self._entities[entity][component_type]
else:
return None
|
[
"def",
"try_component",
"(",
"self",
",",
"entity",
":",
"int",
",",
"component_type",
":",
"Type",
")",
":",
"if",
"component_type",
"in",
"self",
".",
"_entities",
"[",
"entity",
"]",
":",
"yield",
"self",
".",
"_entities",
"[",
"entity",
"]",
"[",
"component_type",
"]",
"else",
":",
"return",
"None"
] |
Try to get a single component type for an Entity.
This method will return the requested Component if it exists, but
will pass silently if it does not. This allows a way to access optional
Components that may or may not exist.
:param entity: The Entity ID to retrieve the Component for.
:param component_type: The Component instance you wish to retrieve.
:return: A iterator containg the single Component instance requested,
which is empty if the component doesn't exist.
|
[
"Try",
"to",
"get",
"a",
"single",
"component",
"type",
"for",
"an",
"Entity",
".",
"This",
"method",
"will",
"return",
"the",
"requested",
"Component",
"if",
"it",
"exists",
"but",
"will",
"pass",
"silently",
"if",
"it",
"does",
"not",
".",
"This",
"allows",
"a",
"way",
"to",
"access",
"optional",
"Components",
"that",
"may",
"or",
"may",
"not",
"exist",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L259-L274
|
train
|
benmoran56/esper
|
esper.py
|
World._clear_dead_entities
|
def _clear_dead_entities(self):
"""Finalize deletion of any Entities that are marked dead.
In the interest of performance, this method duplicates code from the
`delete_entity` method. If that method is changed, those changes should
be duplicated here as well.
"""
for entity in self._dead_entities:
for component_type in self._entities[entity]:
self._components[component_type].discard(entity)
if not self._components[component_type]:
del self._components[component_type]
del self._entities[entity]
self._dead_entities.clear()
self.clear_cache()
|
python
|
def _clear_dead_entities(self):
"""Finalize deletion of any Entities that are marked dead.
In the interest of performance, this method duplicates code from the
`delete_entity` method. If that method is changed, those changes should
be duplicated here as well.
"""
for entity in self._dead_entities:
for component_type in self._entities[entity]:
self._components[component_type].discard(entity)
if not self._components[component_type]:
del self._components[component_type]
del self._entities[entity]
self._dead_entities.clear()
self.clear_cache()
|
[
"def",
"_clear_dead_entities",
"(",
"self",
")",
":",
"for",
"entity",
"in",
"self",
".",
"_dead_entities",
":",
"for",
"component_type",
"in",
"self",
".",
"_entities",
"[",
"entity",
"]",
":",
"self",
".",
"_components",
"[",
"component_type",
"]",
".",
"discard",
"(",
"entity",
")",
"if",
"not",
"self",
".",
"_components",
"[",
"component_type",
"]",
":",
"del",
"self",
".",
"_components",
"[",
"component_type",
"]",
"del",
"self",
".",
"_entities",
"[",
"entity",
"]",
"self",
".",
"_dead_entities",
".",
"clear",
"(",
")",
"self",
".",
"clear_cache",
"(",
")"
] |
Finalize deletion of any Entities that are marked dead.
In the interest of performance, this method duplicates code from the
`delete_entity` method. If that method is changed, those changes should
be duplicated here as well.
|
[
"Finalize",
"deletion",
"of",
"any",
"Entities",
"that",
"are",
"marked",
"dead",
".",
"In",
"the",
"interest",
"of",
"performance",
"this",
"method",
"duplicates",
"code",
"from",
"the",
"delete_entity",
"method",
".",
"If",
"that",
"method",
"is",
"changed",
"those",
"changes",
"should",
"be",
"duplicated",
"here",
"as",
"well",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L276-L294
|
train
|
benmoran56/esper
|
esper.py
|
World._timed_process
|
def _timed_process(self, *args, **kwargs):
"""Track Processor execution time for benchmarking."""
for processor in self._processors:
start_time = _time.process_time()
processor.process(*args, **kwargs)
process_time = int(round((_time.process_time() - start_time) * 1000, 2))
self.process_times[processor.__class__.__name__] = process_time
|
python
|
def _timed_process(self, *args, **kwargs):
"""Track Processor execution time for benchmarking."""
for processor in self._processors:
start_time = _time.process_time()
processor.process(*args, **kwargs)
process_time = int(round((_time.process_time() - start_time) * 1000, 2))
self.process_times[processor.__class__.__name__] = process_time
|
[
"def",
"_timed_process",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"for",
"processor",
"in",
"self",
".",
"_processors",
":",
"start_time",
"=",
"_time",
".",
"process_time",
"(",
")",
"processor",
".",
"process",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"process_time",
"=",
"int",
"(",
"round",
"(",
"(",
"_time",
".",
"process_time",
"(",
")",
"-",
"start_time",
")",
"*",
"1000",
",",
"2",
")",
")",
"self",
".",
"process_times",
"[",
"processor",
".",
"__class__",
".",
"__name__",
"]",
"=",
"process_time"
] |
Track Processor execution time for benchmarking.
|
[
"Track",
"Processor",
"execution",
"time",
"for",
"benchmarking",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L300-L306
|
train
|
benmoran56/esper
|
esper.py
|
World.process
|
def process(self, *args, **kwargs):
"""Call the process method on all Processors, in order of their priority.
Call the *process* method on all assigned Processors, respecting their
optional priority setting. In addition, any Entities that were marked
for deletion since the last call to *World.process*, will be deleted
at the start of this method call.
:param args: Optional arguments that will be passed through to the
*process* method of all Processors.
"""
self._clear_dead_entities()
self._process(*args, **kwargs)
|
python
|
def process(self, *args, **kwargs):
"""Call the process method on all Processors, in order of their priority.
Call the *process* method on all assigned Processors, respecting their
optional priority setting. In addition, any Entities that were marked
for deletion since the last call to *World.process*, will be deleted
at the start of this method call.
:param args: Optional arguments that will be passed through to the
*process* method of all Processors.
"""
self._clear_dead_entities()
self._process(*args, **kwargs)
|
[
"def",
"process",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"self",
".",
"_clear_dead_entities",
"(",
")",
"self",
".",
"_process",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")"
] |
Call the process method on all Processors, in order of their priority.
Call the *process* method on all assigned Processors, respecting their
optional priority setting. In addition, any Entities that were marked
for deletion since the last call to *World.process*, will be deleted
at the start of this method call.
:param args: Optional arguments that will be passed through to the
*process* method of all Processors.
|
[
"Call",
"the",
"process",
"method",
"on",
"all",
"Processors",
"in",
"order",
"of",
"their",
"priority",
"."
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/esper.py#L308-L320
|
train
|
benmoran56/esper
|
examples/pysdl2_example.py
|
texture_from_image
|
def texture_from_image(renderer, image_name):
"""Create an SDL2 Texture from an image file"""
soft_surface = ext.load_image(image_name)
texture = SDL_CreateTextureFromSurface(renderer.renderer, soft_surface)
SDL_FreeSurface(soft_surface)
return texture
|
python
|
def texture_from_image(renderer, image_name):
"""Create an SDL2 Texture from an image file"""
soft_surface = ext.load_image(image_name)
texture = SDL_CreateTextureFromSurface(renderer.renderer, soft_surface)
SDL_FreeSurface(soft_surface)
return texture
|
[
"def",
"texture_from_image",
"(",
"renderer",
",",
"image_name",
")",
":",
"soft_surface",
"=",
"ext",
".",
"load_image",
"(",
"image_name",
")",
"texture",
"=",
"SDL_CreateTextureFromSurface",
"(",
"renderer",
".",
"renderer",
",",
"soft_surface",
")",
"SDL_FreeSurface",
"(",
"soft_surface",
")",
"return",
"texture"
] |
Create an SDL2 Texture from an image file
|
[
"Create",
"an",
"SDL2",
"Texture",
"from",
"an",
"image",
"file"
] |
5b6cd0c51718d5dcfa0e5613f824b5251cf092ac
|
https://github.com/benmoran56/esper/blob/5b6cd0c51718d5dcfa0e5613f824b5251cf092ac/examples/pysdl2_example.py#L76-L81
|
train
|
sdss/tree
|
tasks.py
|
setup_tree
|
def setup_tree(ctx, verbose=None, root=None, tree_dir=None, modules_dir=None):
''' Sets up the SDSS tree enviroment '''
print('Setting up the tree')
ctx.run('python bin/setup_tree.py -t {0} -r {1} -m {2}'.format(tree_dir, root, modules_dir))
|
python
|
def setup_tree(ctx, verbose=None, root=None, tree_dir=None, modules_dir=None):
''' Sets up the SDSS tree enviroment '''
print('Setting up the tree')
ctx.run('python bin/setup_tree.py -t {0} -r {1} -m {2}'.format(tree_dir, root, modules_dir))
|
[
"def",
"setup_tree",
"(",
"ctx",
",",
"verbose",
"=",
"None",
",",
"root",
"=",
"None",
",",
"tree_dir",
"=",
"None",
",",
"modules_dir",
"=",
"None",
")",
":",
"print",
"(",
"'Setting up the tree'",
")",
"ctx",
".",
"run",
"(",
"'python bin/setup_tree.py -t {0} -r {1} -m {2}'",
".",
"format",
"(",
"tree_dir",
",",
"root",
",",
"modules_dir",
")",
")"
] |
Sets up the SDSS tree enviroment
|
[
"Sets",
"up",
"the",
"SDSS",
"tree",
"enviroment"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/tasks.py#L61-L64
|
train
|
sdss/tree
|
python/tree/tree.py
|
Tree.set_roots
|
def set_roots(self, uproot_with=None):
''' Set the roots of the tree in the os environment
Parameters:
uproot_with (str):
A new TREE_DIR path used to override an existing TREE_DIR environment variable
'''
# Check for TREE_DIR
self.treedir = os.environ.get('TREE_DIR', None) if not uproot_with else uproot_with
if not self.treedir:
treefilepath = os.path.dirname(os.path.abspath(__file__))
if 'python/' in treefilepath:
self.treedir = treefilepath.rsplit('/', 2)[0]
else:
self.treedir = treefilepath
self.treedir = treefilepath
os.environ['TREE_DIR'] = self.treedir
# Check sas_base_dir
if 'SAS_BASE_DIR' in os.environ:
self.sasbasedir = os.environ["SAS_BASE_DIR"]
else:
self.sasbasedir = os.path.expanduser('~/sas')
# make the directories
if not os.path.isdir(self.sasbasedir):
os.makedirs(self.sasbasedir)
|
python
|
def set_roots(self, uproot_with=None):
''' Set the roots of the tree in the os environment
Parameters:
uproot_with (str):
A new TREE_DIR path used to override an existing TREE_DIR environment variable
'''
# Check for TREE_DIR
self.treedir = os.environ.get('TREE_DIR', None) if not uproot_with else uproot_with
if not self.treedir:
treefilepath = os.path.dirname(os.path.abspath(__file__))
if 'python/' in treefilepath:
self.treedir = treefilepath.rsplit('/', 2)[0]
else:
self.treedir = treefilepath
self.treedir = treefilepath
os.environ['TREE_DIR'] = self.treedir
# Check sas_base_dir
if 'SAS_BASE_DIR' in os.environ:
self.sasbasedir = os.environ["SAS_BASE_DIR"]
else:
self.sasbasedir = os.path.expanduser('~/sas')
# make the directories
if not os.path.isdir(self.sasbasedir):
os.makedirs(self.sasbasedir)
|
[
"def",
"set_roots",
"(",
"self",
",",
"uproot_with",
"=",
"None",
")",
":",
"# Check for TREE_DIR",
"self",
".",
"treedir",
"=",
"os",
".",
"environ",
".",
"get",
"(",
"'TREE_DIR'",
",",
"None",
")",
"if",
"not",
"uproot_with",
"else",
"uproot_with",
"if",
"not",
"self",
".",
"treedir",
":",
"treefilepath",
"=",
"os",
".",
"path",
".",
"dirname",
"(",
"os",
".",
"path",
".",
"abspath",
"(",
"__file__",
")",
")",
"if",
"'python/'",
"in",
"treefilepath",
":",
"self",
".",
"treedir",
"=",
"treefilepath",
".",
"rsplit",
"(",
"'/'",
",",
"2",
")",
"[",
"0",
"]",
"else",
":",
"self",
".",
"treedir",
"=",
"treefilepath",
"self",
".",
"treedir",
"=",
"treefilepath",
"os",
".",
"environ",
"[",
"'TREE_DIR'",
"]",
"=",
"self",
".",
"treedir",
"# Check sas_base_dir",
"if",
"'SAS_BASE_DIR'",
"in",
"os",
".",
"environ",
":",
"self",
".",
"sasbasedir",
"=",
"os",
".",
"environ",
"[",
"\"SAS_BASE_DIR\"",
"]",
"else",
":",
"self",
".",
"sasbasedir",
"=",
"os",
".",
"path",
".",
"expanduser",
"(",
"'~/sas'",
")",
"# make the directories",
"if",
"not",
"os",
".",
"path",
".",
"isdir",
"(",
"self",
".",
"sasbasedir",
")",
":",
"os",
".",
"makedirs",
"(",
"self",
".",
"sasbasedir",
")"
] |
Set the roots of the tree in the os environment
Parameters:
uproot_with (str):
A new TREE_DIR path used to override an existing TREE_DIR environment variable
|
[
"Set",
"the",
"roots",
"of",
"the",
"tree",
"in",
"the",
"os",
"environment"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/tree.py#L75-L103
|
train
|
sdss/tree
|
python/tree/tree.py
|
Tree.load_config
|
def load_config(self, config=None):
''' loads a config file
Parameters:
config (str):
Optional name of manual config file to load
'''
# Read the config file
cfgname = (config or self.config_name)
cfgname = 'sdsswork' if cfgname is None else cfgname
assert isinstance(cfgname, six.string_types), 'config name must be a string'
config_name = cfgname if cfgname.endswith('.cfg') else '{0}.cfg'.format(cfgname)
self.configfile = os.path.join(self.treedir, 'data', config_name)
assert os.path.isfile(self.configfile) is True, 'configfile {0} must exist in the proper directory'.format(self.configfile)
self._cfg = SafeConfigParser()
try:
self._cfg.read(self.configfile.decode('utf-8'))
except AttributeError:
self._cfg.read(self.configfile)
# create the local tree environment
self.environ = OrderedDict()
self.environ['default'] = self._cfg.defaults()
# set the filesystem envvar to sas_base_dir
self._file_replace = '@FILESYSTEM@'
if self.environ['default']['filesystem'] == self._file_replace:
self.environ['default']['filesystem'] = self.sasbasedir
|
python
|
def load_config(self, config=None):
''' loads a config file
Parameters:
config (str):
Optional name of manual config file to load
'''
# Read the config file
cfgname = (config or self.config_name)
cfgname = 'sdsswork' if cfgname is None else cfgname
assert isinstance(cfgname, six.string_types), 'config name must be a string'
config_name = cfgname if cfgname.endswith('.cfg') else '{0}.cfg'.format(cfgname)
self.configfile = os.path.join(self.treedir, 'data', config_name)
assert os.path.isfile(self.configfile) is True, 'configfile {0} must exist in the proper directory'.format(self.configfile)
self._cfg = SafeConfigParser()
try:
self._cfg.read(self.configfile.decode('utf-8'))
except AttributeError:
self._cfg.read(self.configfile)
# create the local tree environment
self.environ = OrderedDict()
self.environ['default'] = self._cfg.defaults()
# set the filesystem envvar to sas_base_dir
self._file_replace = '@FILESYSTEM@'
if self.environ['default']['filesystem'] == self._file_replace:
self.environ['default']['filesystem'] = self.sasbasedir
|
[
"def",
"load_config",
"(",
"self",
",",
"config",
"=",
"None",
")",
":",
"# Read the config file",
"cfgname",
"=",
"(",
"config",
"or",
"self",
".",
"config_name",
")",
"cfgname",
"=",
"'sdsswork'",
"if",
"cfgname",
"is",
"None",
"else",
"cfgname",
"assert",
"isinstance",
"(",
"cfgname",
",",
"six",
".",
"string_types",
")",
",",
"'config name must be a string'",
"config_name",
"=",
"cfgname",
"if",
"cfgname",
".",
"endswith",
"(",
"'.cfg'",
")",
"else",
"'{0}.cfg'",
".",
"format",
"(",
"cfgname",
")",
"self",
".",
"configfile",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"treedir",
",",
"'data'",
",",
"config_name",
")",
"assert",
"os",
".",
"path",
".",
"isfile",
"(",
"self",
".",
"configfile",
")",
"is",
"True",
",",
"'configfile {0} must exist in the proper directory'",
".",
"format",
"(",
"self",
".",
"configfile",
")",
"self",
".",
"_cfg",
"=",
"SafeConfigParser",
"(",
")",
"try",
":",
"self",
".",
"_cfg",
".",
"read",
"(",
"self",
".",
"configfile",
".",
"decode",
"(",
"'utf-8'",
")",
")",
"except",
"AttributeError",
":",
"self",
".",
"_cfg",
".",
"read",
"(",
"self",
".",
"configfile",
")",
"# create the local tree environment",
"self",
".",
"environ",
"=",
"OrderedDict",
"(",
")",
"self",
".",
"environ",
"[",
"'default'",
"]",
"=",
"self",
".",
"_cfg",
".",
"defaults",
"(",
")",
"# set the filesystem envvar to sas_base_dir",
"self",
".",
"_file_replace",
"=",
"'@FILESYSTEM@'",
"if",
"self",
".",
"environ",
"[",
"'default'",
"]",
"[",
"'filesystem'",
"]",
"==",
"self",
".",
"_file_replace",
":",
"self",
".",
"environ",
"[",
"'default'",
"]",
"[",
"'filesystem'",
"]",
"=",
"self",
".",
"sasbasedir"
] |
loads a config file
Parameters:
config (str):
Optional name of manual config file to load
|
[
"loads",
"a",
"config",
"file"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/tree.py#L105-L135
|
train
|
sdss/tree
|
python/tree/tree.py
|
Tree.branch_out
|
def branch_out(self, limb=None):
''' Set the individual section branches
This adds the various sections of the config file into the
tree environment for access later. Optically can specify a specific
branch. This does not yet load them into the os environment.
Parameters:
limb (str/list):
The name of the section of the config to add into the environ
or a list of strings
'''
# Filter on sections
if not limb:
limbs = self._cfg.sections()
else:
# we must have the general always + secton
limb = limb if isinstance(limb, list) else [limb]
limbs = ['general']
limbs.extend(limb)
# add all limbs into the tree environ
for leaf in limbs:
leaf = leaf if leaf in self._cfg.sections() else leaf.upper()
self.environ[leaf] = OrderedDict()
options = self._cfg.options(leaf)
for opt in options:
if opt in self.environ['default']:
continue
val = self._cfg.get(leaf, opt)
if val.find(self._file_replace) == 0:
val = val.replace(self._file_replace, self.sasbasedir)
self.environ[leaf][opt] = val
|
python
|
def branch_out(self, limb=None):
''' Set the individual section branches
This adds the various sections of the config file into the
tree environment for access later. Optically can specify a specific
branch. This does not yet load them into the os environment.
Parameters:
limb (str/list):
The name of the section of the config to add into the environ
or a list of strings
'''
# Filter on sections
if not limb:
limbs = self._cfg.sections()
else:
# we must have the general always + secton
limb = limb if isinstance(limb, list) else [limb]
limbs = ['general']
limbs.extend(limb)
# add all limbs into the tree environ
for leaf in limbs:
leaf = leaf if leaf in self._cfg.sections() else leaf.upper()
self.environ[leaf] = OrderedDict()
options = self._cfg.options(leaf)
for opt in options:
if opt in self.environ['default']:
continue
val = self._cfg.get(leaf, opt)
if val.find(self._file_replace) == 0:
val = val.replace(self._file_replace, self.sasbasedir)
self.environ[leaf][opt] = val
|
[
"def",
"branch_out",
"(",
"self",
",",
"limb",
"=",
"None",
")",
":",
"# Filter on sections",
"if",
"not",
"limb",
":",
"limbs",
"=",
"self",
".",
"_cfg",
".",
"sections",
"(",
")",
"else",
":",
"# we must have the general always + secton",
"limb",
"=",
"limb",
"if",
"isinstance",
"(",
"limb",
",",
"list",
")",
"else",
"[",
"limb",
"]",
"limbs",
"=",
"[",
"'general'",
"]",
"limbs",
".",
"extend",
"(",
"limb",
")",
"# add all limbs into the tree environ",
"for",
"leaf",
"in",
"limbs",
":",
"leaf",
"=",
"leaf",
"if",
"leaf",
"in",
"self",
".",
"_cfg",
".",
"sections",
"(",
")",
"else",
"leaf",
".",
"upper",
"(",
")",
"self",
".",
"environ",
"[",
"leaf",
"]",
"=",
"OrderedDict",
"(",
")",
"options",
"=",
"self",
".",
"_cfg",
".",
"options",
"(",
"leaf",
")",
"for",
"opt",
"in",
"options",
":",
"if",
"opt",
"in",
"self",
".",
"environ",
"[",
"'default'",
"]",
":",
"continue",
"val",
"=",
"self",
".",
"_cfg",
".",
"get",
"(",
"leaf",
",",
"opt",
")",
"if",
"val",
".",
"find",
"(",
"self",
".",
"_file_replace",
")",
"==",
"0",
":",
"val",
"=",
"val",
".",
"replace",
"(",
"self",
".",
"_file_replace",
",",
"self",
".",
"sasbasedir",
")",
"self",
".",
"environ",
"[",
"leaf",
"]",
"[",
"opt",
"]",
"=",
"val"
] |
Set the individual section branches
This adds the various sections of the config file into the
tree environment for access later. Optically can specify a specific
branch. This does not yet load them into the os environment.
Parameters:
limb (str/list):
The name of the section of the config to add into the environ
or a list of strings
|
[
"Set",
"the",
"individual",
"section",
"branches"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/tree.py#L137-L172
|
train
|
sdss/tree
|
python/tree/tree.py
|
Tree.add_limbs
|
def add_limbs(self, key=None):
''' Add a new section from the tree into the existing os environment
Parameters:
key (str):
The section name to grab from the environment
'''
self.branch_out(limb=key)
self.add_paths_to_os(key=key)
|
python
|
def add_limbs(self, key=None):
''' Add a new section from the tree into the existing os environment
Parameters:
key (str):
The section name to grab from the environment
'''
self.branch_out(limb=key)
self.add_paths_to_os(key=key)
|
[
"def",
"add_limbs",
"(",
"self",
",",
"key",
"=",
"None",
")",
":",
"self",
".",
"branch_out",
"(",
"limb",
"=",
"key",
")",
"self",
".",
"add_paths_to_os",
"(",
"key",
"=",
"key",
")"
] |
Add a new section from the tree into the existing os environment
Parameters:
key (str):
The section name to grab from the environment
|
[
"Add",
"a",
"new",
"section",
"from",
"the",
"tree",
"into",
"the",
"existing",
"os",
"environment"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/tree.py#L174-L183
|
train
|
sdss/tree
|
python/tree/tree.py
|
Tree.get_paths
|
def get_paths(self, key):
''' Retrieve a set of environment paths from the config
Parameters:
key (str):
The section name to grab from the environment
Returns:
self.environ[newkey] (OrderedDict):
An ordered dict containing all of the paths from the
specified section, as key:val = name:path
'''
newkey = key if key in self.environ else key.upper() if key.upper() \
in self.environ else None
if newkey:
return self.environ[newkey]
else:
raise KeyError('Key {0} not found in tree environment'.format(key))
|
python
|
def get_paths(self, key):
''' Retrieve a set of environment paths from the config
Parameters:
key (str):
The section name to grab from the environment
Returns:
self.environ[newkey] (OrderedDict):
An ordered dict containing all of the paths from the
specified section, as key:val = name:path
'''
newkey = key if key in self.environ else key.upper() if key.upper() \
in self.environ else None
if newkey:
return self.environ[newkey]
else:
raise KeyError('Key {0} not found in tree environment'.format(key))
|
[
"def",
"get_paths",
"(",
"self",
",",
"key",
")",
":",
"newkey",
"=",
"key",
"if",
"key",
"in",
"self",
".",
"environ",
"else",
"key",
".",
"upper",
"(",
")",
"if",
"key",
".",
"upper",
"(",
")",
"in",
"self",
".",
"environ",
"else",
"None",
"if",
"newkey",
":",
"return",
"self",
".",
"environ",
"[",
"newkey",
"]",
"else",
":",
"raise",
"KeyError",
"(",
"'Key {0} not found in tree environment'",
".",
"format",
"(",
"key",
")",
")"
] |
Retrieve a set of environment paths from the config
Parameters:
key (str):
The section name to grab from the environment
Returns:
self.environ[newkey] (OrderedDict):
An ordered dict containing all of the paths from the
specified section, as key:val = name:path
|
[
"Retrieve",
"a",
"set",
"of",
"environment",
"paths",
"from",
"the",
"config"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/tree.py#L185-L202
|
train
|
sdss/tree
|
python/tree/tree.py
|
Tree.add_paths_to_os
|
def add_paths_to_os(self, key=None, update=None):
''' Add the paths in tree environ into the os environ
This code goes through the tree environ and checks
for existence in the os environ, then adds them
Parameters:
key (str):
The section name to check against / add
update (bool):
If True, overwrites existing tree environment variables in your
local environment. Default is False.
'''
if key is not None:
allpaths = key if isinstance(key, list) else [key]
else:
allpaths = [k for k in self.environ.keys() if 'default' not in k]
for key in allpaths:
paths = self.get_paths(key)
self.check_paths(paths, update=update)
|
python
|
def add_paths_to_os(self, key=None, update=None):
''' Add the paths in tree environ into the os environ
This code goes through the tree environ and checks
for existence in the os environ, then adds them
Parameters:
key (str):
The section name to check against / add
update (bool):
If True, overwrites existing tree environment variables in your
local environment. Default is False.
'''
if key is not None:
allpaths = key if isinstance(key, list) else [key]
else:
allpaths = [k for k in self.environ.keys() if 'default' not in k]
for key in allpaths:
paths = self.get_paths(key)
self.check_paths(paths, update=update)
|
[
"def",
"add_paths_to_os",
"(",
"self",
",",
"key",
"=",
"None",
",",
"update",
"=",
"None",
")",
":",
"if",
"key",
"is",
"not",
"None",
":",
"allpaths",
"=",
"key",
"if",
"isinstance",
"(",
"key",
",",
"list",
")",
"else",
"[",
"key",
"]",
"else",
":",
"allpaths",
"=",
"[",
"k",
"for",
"k",
"in",
"self",
".",
"environ",
".",
"keys",
"(",
")",
"if",
"'default'",
"not",
"in",
"k",
"]",
"for",
"key",
"in",
"allpaths",
":",
"paths",
"=",
"self",
".",
"get_paths",
"(",
"key",
")",
"self",
".",
"check_paths",
"(",
"paths",
",",
"update",
"=",
"update",
")"
] |
Add the paths in tree environ into the os environ
This code goes through the tree environ and checks
for existence in the os environ, then adds them
Parameters:
key (str):
The section name to check against / add
update (bool):
If True, overwrites existing tree environment variables in your
local environment. Default is False.
|
[
"Add",
"the",
"paths",
"in",
"tree",
"environ",
"into",
"the",
"os",
"environ"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/tree.py#L204-L225
|
train
|
sdss/tree
|
python/tree/tree.py
|
Tree.check_paths
|
def check_paths(self, paths, update=None):
''' Check if the path is in the os environ, and if not add it
Paramters:
paths (OrderedDict):
An ordered dict containing all of the paths from the
a given section, as key:val = name:path
update (bool):
If True, overwrites existing tree environment variables in your
local environment. Default is False.
'''
# set up the exclusion list
exclude = [] if not self.exclude else self.exclude \
if isinstance(self.exclude, list) else [self.exclude]
# check the path names
for pathname, path in paths.items():
if update and pathname.upper() not in exclude:
os.environ[pathname.upper()] = os.path.normpath(path)
elif pathname.upper() not in os.environ:
os.environ[pathname.upper()] = os.path.normpath(path)
|
python
|
def check_paths(self, paths, update=None):
''' Check if the path is in the os environ, and if not add it
Paramters:
paths (OrderedDict):
An ordered dict containing all of the paths from the
a given section, as key:val = name:path
update (bool):
If True, overwrites existing tree environment variables in your
local environment. Default is False.
'''
# set up the exclusion list
exclude = [] if not self.exclude else self.exclude \
if isinstance(self.exclude, list) else [self.exclude]
# check the path names
for pathname, path in paths.items():
if update and pathname.upper() not in exclude:
os.environ[pathname.upper()] = os.path.normpath(path)
elif pathname.upper() not in os.environ:
os.environ[pathname.upper()] = os.path.normpath(path)
|
[
"def",
"check_paths",
"(",
"self",
",",
"paths",
",",
"update",
"=",
"None",
")",
":",
"# set up the exclusion list",
"exclude",
"=",
"[",
"]",
"if",
"not",
"self",
".",
"exclude",
"else",
"self",
".",
"exclude",
"if",
"isinstance",
"(",
"self",
".",
"exclude",
",",
"list",
")",
"else",
"[",
"self",
".",
"exclude",
"]",
"# check the path names",
"for",
"pathname",
",",
"path",
"in",
"paths",
".",
"items",
"(",
")",
":",
"if",
"update",
"and",
"pathname",
".",
"upper",
"(",
")",
"not",
"in",
"exclude",
":",
"os",
".",
"environ",
"[",
"pathname",
".",
"upper",
"(",
")",
"]",
"=",
"os",
".",
"path",
".",
"normpath",
"(",
"path",
")",
"elif",
"pathname",
".",
"upper",
"(",
")",
"not",
"in",
"os",
".",
"environ",
":",
"os",
".",
"environ",
"[",
"pathname",
".",
"upper",
"(",
")",
"]",
"=",
"os",
".",
"path",
".",
"normpath",
"(",
"path",
")"
] |
Check if the path is in the os environ, and if not add it
Paramters:
paths (OrderedDict):
An ordered dict containing all of the paths from the
a given section, as key:val = name:path
update (bool):
If True, overwrites existing tree environment variables in your
local environment. Default is False.
|
[
"Check",
"if",
"the",
"path",
"is",
"in",
"the",
"os",
"environ",
"and",
"if",
"not",
"add",
"it"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/tree.py#L227-L248
|
train
|
sdss/tree
|
python/tree/tree.py
|
Tree.replant_tree
|
def replant_tree(self, config=None, exclude=None):
''' Replant the tree with a different config setup
Parameters:
config (str):
The config name to reload
exclude (list):
A list of environment variables to exclude
from forced updates
'''
# reinitialize a new Tree with a new config
self.__init__(key=self.key, config=config, update=True, exclude=exclude)
|
python
|
def replant_tree(self, config=None, exclude=None):
''' Replant the tree with a different config setup
Parameters:
config (str):
The config name to reload
exclude (list):
A list of environment variables to exclude
from forced updates
'''
# reinitialize a new Tree with a new config
self.__init__(key=self.key, config=config, update=True, exclude=exclude)
|
[
"def",
"replant_tree",
"(",
"self",
",",
"config",
"=",
"None",
",",
"exclude",
"=",
"None",
")",
":",
"# reinitialize a new Tree with a new config",
"self",
".",
"__init__",
"(",
"key",
"=",
"self",
".",
"key",
",",
"config",
"=",
"config",
",",
"update",
"=",
"True",
",",
"exclude",
"=",
"exclude",
")"
] |
Replant the tree with a different config setup
Parameters:
config (str):
The config name to reload
exclude (list):
A list of environment variables to exclude
from forced updates
|
[
"Replant",
"the",
"tree",
"with",
"a",
"different",
"config",
"setup"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/tree.py#L250-L262
|
train
|
sdss/tree
|
python/tree/misc/logger.py
|
print_exception_formatted
|
def print_exception_formatted(type, value, tb):
"""A custom hook for printing tracebacks with colours."""
tbtext = ''.join(traceback.format_exception(type, value, tb))
lexer = get_lexer_by_name('pytb', stripall=True)
formatter = TerminalFormatter()
sys.stderr.write(highlight(tbtext, lexer, formatter))
|
python
|
def print_exception_formatted(type, value, tb):
"""A custom hook for printing tracebacks with colours."""
tbtext = ''.join(traceback.format_exception(type, value, tb))
lexer = get_lexer_by_name('pytb', stripall=True)
formatter = TerminalFormatter()
sys.stderr.write(highlight(tbtext, lexer, formatter))
|
[
"def",
"print_exception_formatted",
"(",
"type",
",",
"value",
",",
"tb",
")",
":",
"tbtext",
"=",
"''",
".",
"join",
"(",
"traceback",
".",
"format_exception",
"(",
"type",
",",
"value",
",",
"tb",
")",
")",
"lexer",
"=",
"get_lexer_by_name",
"(",
"'pytb'",
",",
"stripall",
"=",
"True",
")",
"formatter",
"=",
"TerminalFormatter",
"(",
")",
"sys",
".",
"stderr",
".",
"write",
"(",
"highlight",
"(",
"tbtext",
",",
"lexer",
",",
"formatter",
")",
")"
] |
A custom hook for printing tracebacks with colours.
|
[
"A",
"custom",
"hook",
"for",
"printing",
"tracebacks",
"with",
"colours",
"."
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/misc/logger.py#L49-L55
|
train
|
sdss/tree
|
python/tree/misc/logger.py
|
colored_formatter
|
def colored_formatter(record):
"""Prints log messages with colours."""
colours = {'info': ('blue', 'normal'),
'debug': ('magenta', 'normal'),
'warning': ('yellow', 'normal'),
'print': ('green', 'normal'),
'error': ('red', 'bold')}
levelname = record.levelname.lower()
if levelname == 'error':
return
if levelname.lower() in colours:
levelname_color = colours[levelname][0]
header = color_text('[{}]: '.format(levelname.upper()), levelname_color)
message = '{0}'.format(record.msg)
warning_category = re.match(r'^(\w+Warning:).*', message)
if warning_category is not None:
warning_category_colour = color_text(warning_category.groups()[0], 'cyan')
message = message.replace(warning_category.groups()[0], warning_category_colour)
sub_level = re.match(r'(\[.+\]:)(.*)', message)
if sub_level is not None:
sub_level_name = color_text(sub_level.groups()[0], 'red')
message = '{}{}'.format(sub_level_name, ''.join(sub_level.groups()[1:]))
# if len(message) > 79:
# tw = TextWrapper()
# tw.width = 79
# tw.subsequent_indent = ' ' * (len(record.levelname) + 2)
# tw.break_on_hyphens = False
# message = '\n'.join(tw.wrap(message))
sys.__stdout__.write('{}{}\n'.format(header, message))
sys.__stdout__.flush()
return
|
python
|
def colored_formatter(record):
"""Prints log messages with colours."""
colours = {'info': ('blue', 'normal'),
'debug': ('magenta', 'normal'),
'warning': ('yellow', 'normal'),
'print': ('green', 'normal'),
'error': ('red', 'bold')}
levelname = record.levelname.lower()
if levelname == 'error':
return
if levelname.lower() in colours:
levelname_color = colours[levelname][0]
header = color_text('[{}]: '.format(levelname.upper()), levelname_color)
message = '{0}'.format(record.msg)
warning_category = re.match(r'^(\w+Warning:).*', message)
if warning_category is not None:
warning_category_colour = color_text(warning_category.groups()[0], 'cyan')
message = message.replace(warning_category.groups()[0], warning_category_colour)
sub_level = re.match(r'(\[.+\]:)(.*)', message)
if sub_level is not None:
sub_level_name = color_text(sub_level.groups()[0], 'red')
message = '{}{}'.format(sub_level_name, ''.join(sub_level.groups()[1:]))
# if len(message) > 79:
# tw = TextWrapper()
# tw.width = 79
# tw.subsequent_indent = ' ' * (len(record.levelname) + 2)
# tw.break_on_hyphens = False
# message = '\n'.join(tw.wrap(message))
sys.__stdout__.write('{}{}\n'.format(header, message))
sys.__stdout__.flush()
return
|
[
"def",
"colored_formatter",
"(",
"record",
")",
":",
"colours",
"=",
"{",
"'info'",
":",
"(",
"'blue'",
",",
"'normal'",
")",
",",
"'debug'",
":",
"(",
"'magenta'",
",",
"'normal'",
")",
",",
"'warning'",
":",
"(",
"'yellow'",
",",
"'normal'",
")",
",",
"'print'",
":",
"(",
"'green'",
",",
"'normal'",
")",
",",
"'error'",
":",
"(",
"'red'",
",",
"'bold'",
")",
"}",
"levelname",
"=",
"record",
".",
"levelname",
".",
"lower",
"(",
")",
"if",
"levelname",
"==",
"'error'",
":",
"return",
"if",
"levelname",
".",
"lower",
"(",
")",
"in",
"colours",
":",
"levelname_color",
"=",
"colours",
"[",
"levelname",
"]",
"[",
"0",
"]",
"header",
"=",
"color_text",
"(",
"'[{}]: '",
".",
"format",
"(",
"levelname",
".",
"upper",
"(",
")",
")",
",",
"levelname_color",
")",
"message",
"=",
"'{0}'",
".",
"format",
"(",
"record",
".",
"msg",
")",
"warning_category",
"=",
"re",
".",
"match",
"(",
"r'^(\\w+Warning:).*'",
",",
"message",
")",
"if",
"warning_category",
"is",
"not",
"None",
":",
"warning_category_colour",
"=",
"color_text",
"(",
"warning_category",
".",
"groups",
"(",
")",
"[",
"0",
"]",
",",
"'cyan'",
")",
"message",
"=",
"message",
".",
"replace",
"(",
"warning_category",
".",
"groups",
"(",
")",
"[",
"0",
"]",
",",
"warning_category_colour",
")",
"sub_level",
"=",
"re",
".",
"match",
"(",
"r'(\\[.+\\]:)(.*)'",
",",
"message",
")",
"if",
"sub_level",
"is",
"not",
"None",
":",
"sub_level_name",
"=",
"color_text",
"(",
"sub_level",
".",
"groups",
"(",
")",
"[",
"0",
"]",
",",
"'red'",
")",
"message",
"=",
"'{}{}'",
".",
"format",
"(",
"sub_level_name",
",",
"''",
".",
"join",
"(",
"sub_level",
".",
"groups",
"(",
")",
"[",
"1",
":",
"]",
")",
")",
"# if len(message) > 79:",
"# tw = TextWrapper()",
"# tw.width = 79",
"# tw.subsequent_indent = ' ' * (len(record.levelname) + 2)",
"# tw.break_on_hyphens = False",
"# message = '\\n'.join(tw.wrap(message))",
"sys",
".",
"__stdout__",
".",
"write",
"(",
"'{}{}\\n'",
".",
"format",
"(",
"header",
",",
"message",
")",
")",
"sys",
".",
"__stdout__",
".",
"flush",
"(",
")",
"return"
] |
Prints log messages with colours.
|
[
"Prints",
"log",
"messages",
"with",
"colours",
"."
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/misc/logger.py#L58-L98
|
train
|
sdss/tree
|
python/tree/misc/logger.py
|
MyLogger._catch_exceptions
|
def _catch_exceptions(self, exctype, value, tb):
"""Catches all exceptions and logs them."""
# Now we log it.
self.error('Uncaught exception', exc_info=(exctype, value, tb))
# First, we print to stdout with some colouring.
print_exception_formatted(exctype, value, tb)
|
python
|
def _catch_exceptions(self, exctype, value, tb):
"""Catches all exceptions and logs them."""
# Now we log it.
self.error('Uncaught exception', exc_info=(exctype, value, tb))
# First, we print to stdout with some colouring.
print_exception_formatted(exctype, value, tb)
|
[
"def",
"_catch_exceptions",
"(",
"self",
",",
"exctype",
",",
"value",
",",
"tb",
")",
":",
"# Now we log it.",
"self",
".",
"error",
"(",
"'Uncaught exception'",
",",
"exc_info",
"=",
"(",
"exctype",
",",
"value",
",",
"tb",
")",
")",
"# First, we print to stdout with some colouring.",
"print_exception_formatted",
"(",
"exctype",
",",
"value",
",",
"tb",
")"
] |
Catches all exceptions and logs them.
|
[
"Catches",
"all",
"exceptions",
"and",
"logs",
"them",
"."
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/misc/logger.py#L205-L212
|
train
|
sdss/tree
|
python/tree/misc/logger.py
|
MyLogger._set_defaults
|
def _set_defaults(self, log_level=logging.INFO, redirect_stdout=False):
"""Reset logger to its initial state."""
# Remove all previous handlers
for handler in self.handlers[:]:
self.removeHandler(handler)
# Set levels
self.setLevel(logging.DEBUG)
# Set up the stdout handler
self.fh = None
self.sh = logging.StreamHandler()
self.sh.emit = colored_formatter
self.addHandler(self.sh)
self.sh.setLevel(log_level)
# warnings.showwarning = self._show_warning
# Redirects all stdout to the logger
if redirect_stdout:
sys.stdout = LoggerStdout(self._print)
# Catches exceptions
sys.excepthook = self._catch_exceptions
|
python
|
def _set_defaults(self, log_level=logging.INFO, redirect_stdout=False):
"""Reset logger to its initial state."""
# Remove all previous handlers
for handler in self.handlers[:]:
self.removeHandler(handler)
# Set levels
self.setLevel(logging.DEBUG)
# Set up the stdout handler
self.fh = None
self.sh = logging.StreamHandler()
self.sh.emit = colored_formatter
self.addHandler(self.sh)
self.sh.setLevel(log_level)
# warnings.showwarning = self._show_warning
# Redirects all stdout to the logger
if redirect_stdout:
sys.stdout = LoggerStdout(self._print)
# Catches exceptions
sys.excepthook = self._catch_exceptions
|
[
"def",
"_set_defaults",
"(",
"self",
",",
"log_level",
"=",
"logging",
".",
"INFO",
",",
"redirect_stdout",
"=",
"False",
")",
":",
"# Remove all previous handlers",
"for",
"handler",
"in",
"self",
".",
"handlers",
"[",
":",
"]",
":",
"self",
".",
"removeHandler",
"(",
"handler",
")",
"# Set levels",
"self",
".",
"setLevel",
"(",
"logging",
".",
"DEBUG",
")",
"# Set up the stdout handler",
"self",
".",
"fh",
"=",
"None",
"self",
".",
"sh",
"=",
"logging",
".",
"StreamHandler",
"(",
")",
"self",
".",
"sh",
".",
"emit",
"=",
"colored_formatter",
"self",
".",
"addHandler",
"(",
"self",
".",
"sh",
")",
"self",
".",
"sh",
".",
"setLevel",
"(",
"log_level",
")",
"# warnings.showwarning = self._show_warning",
"# Redirects all stdout to the logger",
"if",
"redirect_stdout",
":",
"sys",
".",
"stdout",
"=",
"LoggerStdout",
"(",
"self",
".",
"_print",
")",
"# Catches exceptions",
"sys",
".",
"excepthook",
"=",
"self",
".",
"_catch_exceptions"
] |
Reset logger to its initial state.
|
[
"Reset",
"logger",
"to",
"its",
"initial",
"state",
"."
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/misc/logger.py#L214-L239
|
train
|
sdss/tree
|
python/tree/misc/logger.py
|
MyLogger.start_file_logger
|
def start_file_logger(self, name, log_file_level=logging.DEBUG, log_file_path='./'):
"""Start file logging."""
log_file_path = os.path.expanduser(log_file_path) / '{}.log'.format(name)
logdir = log_file_path.parent
try:
logdir.mkdir(parents=True, exist_ok=True)
# If the log file exists, backs it up before creating a new file handler
if log_file_path.exists():
strtime = datetime.datetime.utcnow().strftime('%Y-%m-%d_%H:%M:%S')
shutil.move(log_file_path, log_file_path + '.' + strtime)
self.fh = TimedRotatingFileHandler(str(log_file_path), when='midnight', utc=True)
self.fh.suffix = '%Y-%m-%d_%H:%M:%S'
except (IOError, OSError) as ee:
warnings.warn('log file {0!r} could not be opened for writing: '
'{1}'.format(log_file_path, ee), RuntimeWarning)
else:
self.fh.setFormatter(fmt)
self.addHandler(self.fh)
self.fh.setLevel(log_file_level)
self.log_filename = log_file_path
|
python
|
def start_file_logger(self, name, log_file_level=logging.DEBUG, log_file_path='./'):
"""Start file logging."""
log_file_path = os.path.expanduser(log_file_path) / '{}.log'.format(name)
logdir = log_file_path.parent
try:
logdir.mkdir(parents=True, exist_ok=True)
# If the log file exists, backs it up before creating a new file handler
if log_file_path.exists():
strtime = datetime.datetime.utcnow().strftime('%Y-%m-%d_%H:%M:%S')
shutil.move(log_file_path, log_file_path + '.' + strtime)
self.fh = TimedRotatingFileHandler(str(log_file_path), when='midnight', utc=True)
self.fh.suffix = '%Y-%m-%d_%H:%M:%S'
except (IOError, OSError) as ee:
warnings.warn('log file {0!r} could not be opened for writing: '
'{1}'.format(log_file_path, ee), RuntimeWarning)
else:
self.fh.setFormatter(fmt)
self.addHandler(self.fh)
self.fh.setLevel(log_file_level)
self.log_filename = log_file_path
|
[
"def",
"start_file_logger",
"(",
"self",
",",
"name",
",",
"log_file_level",
"=",
"logging",
".",
"DEBUG",
",",
"log_file_path",
"=",
"'./'",
")",
":",
"log_file_path",
"=",
"os",
".",
"path",
".",
"expanduser",
"(",
"log_file_path",
")",
"/",
"'{}.log'",
".",
"format",
"(",
"name",
")",
"logdir",
"=",
"log_file_path",
".",
"parent",
"try",
":",
"logdir",
".",
"mkdir",
"(",
"parents",
"=",
"True",
",",
"exist_ok",
"=",
"True",
")",
"# If the log file exists, backs it up before creating a new file handler",
"if",
"log_file_path",
".",
"exists",
"(",
")",
":",
"strtime",
"=",
"datetime",
".",
"datetime",
".",
"utcnow",
"(",
")",
".",
"strftime",
"(",
"'%Y-%m-%d_%H:%M:%S'",
")",
"shutil",
".",
"move",
"(",
"log_file_path",
",",
"log_file_path",
"+",
"'.'",
"+",
"strtime",
")",
"self",
".",
"fh",
"=",
"TimedRotatingFileHandler",
"(",
"str",
"(",
"log_file_path",
")",
",",
"when",
"=",
"'midnight'",
",",
"utc",
"=",
"True",
")",
"self",
".",
"fh",
".",
"suffix",
"=",
"'%Y-%m-%d_%H:%M:%S'",
"except",
"(",
"IOError",
",",
"OSError",
")",
"as",
"ee",
":",
"warnings",
".",
"warn",
"(",
"'log file {0!r} could not be opened for writing: '",
"'{1}'",
".",
"format",
"(",
"log_file_path",
",",
"ee",
")",
",",
"RuntimeWarning",
")",
"else",
":",
"self",
".",
"fh",
".",
"setFormatter",
"(",
"fmt",
")",
"self",
".",
"addHandler",
"(",
"self",
".",
"fh",
")",
"self",
".",
"fh",
".",
"setLevel",
"(",
"log_file_level",
")",
"self",
".",
"log_filename",
"=",
"log_file_path"
] |
Start file logging.
|
[
"Start",
"file",
"logging",
"."
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/misc/logger.py#L241-L265
|
train
|
sdss/tree
|
bin/setup_tree.py
|
create_index_page
|
def create_index_page(environ, defaults, envdir):
''' create the env index html page
Builds the index.html page containing a table of symlinks
to datamodel directories
Parameters:
environ (dict):
A tree environment dictionary
defaults (dict):
The defaults dictionary from environ['default']
envdir (str):
The filepath for the env directory
Returns:
A string defintion of an html page
'''
# header of index file
header = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta name="viewport" content="width=device-width"/><meta http-equiv="content-type" content="text/html; charset=utf-8"/><style type="text/css">body,html {{background:#fff;font-family:"Bitstream Vera Sans","Lucida Grande","Lucida Sans Unicode",Lucidux,Verdana,Lucida,sans-serif;}}tr:nth-child(even) {{background:#f4f4f4;}}th,td {{padding:0.1em 0.5em;}}th {{text-align:left;font-weight:bold;background:#eee;border-bottom:1px solid #aaa;}}#list {{border:1px solid #aaa;width:100%%;}}a {{color:#a33;}}a:hover {{color:#e33;}}</style>
<link rel="stylesheet" href="{url}/css/sas.css" type="text/css"/>
<title>Index of /sas/{name}/env/</title>
</head><body><h1>Index of /sas/{name}/env/</h1>
"""
# footer of index file
footer = """<h3><a href='{url}/sas/'>{location}</a></h3>
<p>This directory contains links to the contents of
environment variables defined by the tree product, version {name}.
To examine the <em>types</em> of files contained in each environment variable
directory, visit <a href="/datamodel/files/">the datamodel.</a></p>
</body></html>
"""
# create index html file
index = header.format(**defaults)
index += create_index_table(environ, envdir)
index += footer.format(**defaults)
return index
|
python
|
def create_index_page(environ, defaults, envdir):
''' create the env index html page
Builds the index.html page containing a table of symlinks
to datamodel directories
Parameters:
environ (dict):
A tree environment dictionary
defaults (dict):
The defaults dictionary from environ['default']
envdir (str):
The filepath for the env directory
Returns:
A string defintion of an html page
'''
# header of index file
header = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta name="viewport" content="width=device-width"/><meta http-equiv="content-type" content="text/html; charset=utf-8"/><style type="text/css">body,html {{background:#fff;font-family:"Bitstream Vera Sans","Lucida Grande","Lucida Sans Unicode",Lucidux,Verdana,Lucida,sans-serif;}}tr:nth-child(even) {{background:#f4f4f4;}}th,td {{padding:0.1em 0.5em;}}th {{text-align:left;font-weight:bold;background:#eee;border-bottom:1px solid #aaa;}}#list {{border:1px solid #aaa;width:100%%;}}a {{color:#a33;}}a:hover {{color:#e33;}}</style>
<link rel="stylesheet" href="{url}/css/sas.css" type="text/css"/>
<title>Index of /sas/{name}/env/</title>
</head><body><h1>Index of /sas/{name}/env/</h1>
"""
# footer of index file
footer = """<h3><a href='{url}/sas/'>{location}</a></h3>
<p>This directory contains links to the contents of
environment variables defined by the tree product, version {name}.
To examine the <em>types</em> of files contained in each environment variable
directory, visit <a href="/datamodel/files/">the datamodel.</a></p>
</body></html>
"""
# create index html file
index = header.format(**defaults)
index += create_index_table(environ, envdir)
index += footer.format(**defaults)
return index
|
[
"def",
"create_index_page",
"(",
"environ",
",",
"defaults",
",",
"envdir",
")",
":",
"# header of index file",
"header",
"=",
"\"\"\"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head><meta name=\"viewport\" content=\"width=device-width\"/><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/><style type=\"text/css\">body,html {{background:#fff;font-family:\"Bitstream Vera Sans\",\"Lucida Grande\",\"Lucida Sans Unicode\",Lucidux,Verdana,Lucida,sans-serif;}}tr:nth-child(even) {{background:#f4f4f4;}}th,td {{padding:0.1em 0.5em;}}th {{text-align:left;font-weight:bold;background:#eee;border-bottom:1px solid #aaa;}}#list {{border:1px solid #aaa;width:100%%;}}a {{color:#a33;}}a:hover {{color:#e33;}}</style>\n<link rel=\"stylesheet\" href=\"{url}/css/sas.css\" type=\"text/css\"/>\n<title>Index of /sas/{name}/env/</title>\n</head><body><h1>Index of /sas/{name}/env/</h1>\n\"\"\"",
"# footer of index file",
"footer",
"=",
"\"\"\"<h3><a href='{url}/sas/'>{location}</a></h3>\n<p>This directory contains links to the contents of\nenvironment variables defined by the tree product, version {name}.\nTo examine the <em>types</em> of files contained in each environment variable\ndirectory, visit <a href=\"/datamodel/files/\">the datamodel.</a></p>\n</body></html>\n\"\"\"",
"# create index html file",
"index",
"=",
"header",
".",
"format",
"(",
"*",
"*",
"defaults",
")",
"index",
"+=",
"create_index_table",
"(",
"environ",
",",
"envdir",
")",
"index",
"+=",
"footer",
".",
"format",
"(",
"*",
"*",
"defaults",
")",
"return",
"index"
] |
create the env index html page
Builds the index.html page containing a table of symlinks
to datamodel directories
Parameters:
environ (dict):
A tree environment dictionary
defaults (dict):
The defaults dictionary from environ['default']
envdir (str):
The filepath for the env directory
Returns:
A string defintion of an html page
|
[
"create",
"the",
"env",
"index",
"html",
"page",
"Builds",
"the",
"index",
".",
"html",
"page",
"containing",
"a",
"table",
"of",
"symlinks",
"to",
"datamodel",
"directories"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/bin/setup_tree.py#L113-L153
|
train
|
sdss/tree
|
bin/setup_tree.py
|
create_env
|
def create_env(environ, mirror=None, verbose=None):
''' create the env symlink directory structure
Creates the env folder filled with symlinks to datamodel directories
for a given tree config file.
Parameters:
environ (dict):
A tree environment dictionary
mirror (bool):
If True, use the SAM url location
verbose (bool):
If True, print more information
'''
defaults = environ['default'].copy()
defaults['url'] = "https://data.mirror.sdss.org" if mirror else "https://data.sdss.org"
defaults['location'] = "SDSS-IV Science Archive Mirror (SAM)" if mirror else "SDSS-IV Science Archive Server (SAS)"
if not os.path.exists(environ['general']['sas_root']):
if verbose:
print("{0} doesn't exist, skipping env link creation.".format(environ['general']['sas_root']))
return
if verbose:
print("Found {0}.".format(environ['general']['sas_root']))
# sets and creates envdir
envdir = os.path.join(environ['general']['sas_root'], 'env')
if not os.path.exists(envdir):
os.makedirs(envdir)
if not os.access(envdir, os.W_OK):
return
# create index html
index = create_index_page(environ, defaults, envdir)
# write the index file
indexfile = os.path.join(envdir, 'index.html')
with open(indexfile, 'w') as f:
f.write(index)
|
python
|
def create_env(environ, mirror=None, verbose=None):
''' create the env symlink directory structure
Creates the env folder filled with symlinks to datamodel directories
for a given tree config file.
Parameters:
environ (dict):
A tree environment dictionary
mirror (bool):
If True, use the SAM url location
verbose (bool):
If True, print more information
'''
defaults = environ['default'].copy()
defaults['url'] = "https://data.mirror.sdss.org" if mirror else "https://data.sdss.org"
defaults['location'] = "SDSS-IV Science Archive Mirror (SAM)" if mirror else "SDSS-IV Science Archive Server (SAS)"
if not os.path.exists(environ['general']['sas_root']):
if verbose:
print("{0} doesn't exist, skipping env link creation.".format(environ['general']['sas_root']))
return
if verbose:
print("Found {0}.".format(environ['general']['sas_root']))
# sets and creates envdir
envdir = os.path.join(environ['general']['sas_root'], 'env')
if not os.path.exists(envdir):
os.makedirs(envdir)
if not os.access(envdir, os.W_OK):
return
# create index html
index = create_index_page(environ, defaults, envdir)
# write the index file
indexfile = os.path.join(envdir, 'index.html')
with open(indexfile, 'w') as f:
f.write(index)
|
[
"def",
"create_env",
"(",
"environ",
",",
"mirror",
"=",
"None",
",",
"verbose",
"=",
"None",
")",
":",
"defaults",
"=",
"environ",
"[",
"'default'",
"]",
".",
"copy",
"(",
")",
"defaults",
"[",
"'url'",
"]",
"=",
"\"https://data.mirror.sdss.org\"",
"if",
"mirror",
"else",
"\"https://data.sdss.org\"",
"defaults",
"[",
"'location'",
"]",
"=",
"\"SDSS-IV Science Archive Mirror (SAM)\"",
"if",
"mirror",
"else",
"\"SDSS-IV Science Archive Server (SAS)\"",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"environ",
"[",
"'general'",
"]",
"[",
"'sas_root'",
"]",
")",
":",
"if",
"verbose",
":",
"print",
"(",
"\"{0} doesn't exist, skipping env link creation.\"",
".",
"format",
"(",
"environ",
"[",
"'general'",
"]",
"[",
"'sas_root'",
"]",
")",
")",
"return",
"if",
"verbose",
":",
"print",
"(",
"\"Found {0}.\"",
".",
"format",
"(",
"environ",
"[",
"'general'",
"]",
"[",
"'sas_root'",
"]",
")",
")",
"# sets and creates envdir",
"envdir",
"=",
"os",
".",
"path",
".",
"join",
"(",
"environ",
"[",
"'general'",
"]",
"[",
"'sas_root'",
"]",
",",
"'env'",
")",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"envdir",
")",
":",
"os",
".",
"makedirs",
"(",
"envdir",
")",
"if",
"not",
"os",
".",
"access",
"(",
"envdir",
",",
"os",
".",
"W_OK",
")",
":",
"return",
"# create index html",
"index",
"=",
"create_index_page",
"(",
"environ",
",",
"defaults",
",",
"envdir",
")",
"# write the index file",
"indexfile",
"=",
"os",
".",
"path",
".",
"join",
"(",
"envdir",
",",
"'index.html'",
")",
"with",
"open",
"(",
"indexfile",
",",
"'w'",
")",
"as",
"f",
":",
"f",
".",
"write",
"(",
"index",
")"
] |
create the env symlink directory structure
Creates the env folder filled with symlinks to datamodel directories
for a given tree config file.
Parameters:
environ (dict):
A tree environment dictionary
mirror (bool):
If True, use the SAM url location
verbose (bool):
If True, print more information
|
[
"create",
"the",
"env",
"symlink",
"directory",
"structure",
"Creates",
"the",
"env",
"folder",
"filled",
"with",
"symlinks",
"to",
"datamodel",
"directories",
"for",
"a",
"given",
"tree",
"config",
"file",
"."
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/bin/setup_tree.py#L156-L196
|
train
|
sdss/tree
|
bin/setup_tree.py
|
check_sas_base_dir
|
def check_sas_base_dir(root=None):
''' Check for the SAS_BASE_DIR environment variable
Will set the SAS_BASE_DIR in your local environment
or prompt you to define one if is undefined
Parameters:
root (str):
Optional override of the SAS_BASE_DIR envvar
'''
sasbasedir = root or os.getenv("SAS_BASE_DIR")
if not sasbasedir:
sasbasedir = input('Enter a path for SAS_BASE_DIR: ')
os.environ['SAS_BASE_DIR'] = sasbasedir
|
python
|
def check_sas_base_dir(root=None):
''' Check for the SAS_BASE_DIR environment variable
Will set the SAS_BASE_DIR in your local environment
or prompt you to define one if is undefined
Parameters:
root (str):
Optional override of the SAS_BASE_DIR envvar
'''
sasbasedir = root or os.getenv("SAS_BASE_DIR")
if not sasbasedir:
sasbasedir = input('Enter a path for SAS_BASE_DIR: ')
os.environ['SAS_BASE_DIR'] = sasbasedir
|
[
"def",
"check_sas_base_dir",
"(",
"root",
"=",
"None",
")",
":",
"sasbasedir",
"=",
"root",
"or",
"os",
".",
"getenv",
"(",
"\"SAS_BASE_DIR\"",
")",
"if",
"not",
"sasbasedir",
":",
"sasbasedir",
"=",
"input",
"(",
"'Enter a path for SAS_BASE_DIR: '",
")",
"os",
".",
"environ",
"[",
"'SAS_BASE_DIR'",
"]",
"=",
"sasbasedir"
] |
Check for the SAS_BASE_DIR environment variable
Will set the SAS_BASE_DIR in your local environment
or prompt you to define one if is undefined
Parameters:
root (str):
Optional override of the SAS_BASE_DIR envvar
|
[
"Check",
"for",
"the",
"SAS_BASE_DIR",
"environment",
"variable"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/bin/setup_tree.py#L199-L213
|
train
|
sdss/tree
|
bin/setup_tree.py
|
write_file
|
def write_file(environ, term='bash', out_dir=None, tree_dir=None):
''' Write a tree environment file
Loops over the tree environ and writes them out to a bash, tsch, or
modules file
Parameters:
environ (dict):
The tree dictionary environment
term (str):
The type of shell header to write, can be "bash", "tsch", or "modules"
tree_dir (str):
The path to this repository
out_dir (str):
The output path to write the files (default is etc/)
'''
# get the proper name, header and file extension
name = environ['default']['name']
header = write_header(term=term, name=name, tree_dir=tree_dir)
exts = {'bash': '.sh', 'tsch': '.csh', 'modules': '.module'}
ext = exts[term]
# shell command
if term == 'bash':
cmd = 'export {0}={1}\n'
else:
cmd = 'setenv {0} {1}\n'
# write the environment config files
filename = os.path.join(out_dir, name + ext)
with open(filename, 'w') as f:
f.write(header + '\n')
for key, values in environ.items():
if key != 'default':
# write separator
f.write('#\n# {0}\n#\n'.format(key))
# write tree names and paths
for tree_name, tree_path in values.items():
f.write(cmd.format(tree_name.upper(), tree_path))
# write default .version file for modules
modules_version = write_version(name)
if term == 'modules' and environ['default']['current']:
version_name = os.path.join(out_dir, '.version')
with open(version_name, 'w') as f:
f.write(modules_version)
|
python
|
def write_file(environ, term='bash', out_dir=None, tree_dir=None):
''' Write a tree environment file
Loops over the tree environ and writes them out to a bash, tsch, or
modules file
Parameters:
environ (dict):
The tree dictionary environment
term (str):
The type of shell header to write, can be "bash", "tsch", or "modules"
tree_dir (str):
The path to this repository
out_dir (str):
The output path to write the files (default is etc/)
'''
# get the proper name, header and file extension
name = environ['default']['name']
header = write_header(term=term, name=name, tree_dir=tree_dir)
exts = {'bash': '.sh', 'tsch': '.csh', 'modules': '.module'}
ext = exts[term]
# shell command
if term == 'bash':
cmd = 'export {0}={1}\n'
else:
cmd = 'setenv {0} {1}\n'
# write the environment config files
filename = os.path.join(out_dir, name + ext)
with open(filename, 'w') as f:
f.write(header + '\n')
for key, values in environ.items():
if key != 'default':
# write separator
f.write('#\n# {0}\n#\n'.format(key))
# write tree names and paths
for tree_name, tree_path in values.items():
f.write(cmd.format(tree_name.upper(), tree_path))
# write default .version file for modules
modules_version = write_version(name)
if term == 'modules' and environ['default']['current']:
version_name = os.path.join(out_dir, '.version')
with open(version_name, 'w') as f:
f.write(modules_version)
|
[
"def",
"write_file",
"(",
"environ",
",",
"term",
"=",
"'bash'",
",",
"out_dir",
"=",
"None",
",",
"tree_dir",
"=",
"None",
")",
":",
"# get the proper name, header and file extension",
"name",
"=",
"environ",
"[",
"'default'",
"]",
"[",
"'name'",
"]",
"header",
"=",
"write_header",
"(",
"term",
"=",
"term",
",",
"name",
"=",
"name",
",",
"tree_dir",
"=",
"tree_dir",
")",
"exts",
"=",
"{",
"'bash'",
":",
"'.sh'",
",",
"'tsch'",
":",
"'.csh'",
",",
"'modules'",
":",
"'.module'",
"}",
"ext",
"=",
"exts",
"[",
"term",
"]",
"# shell command",
"if",
"term",
"==",
"'bash'",
":",
"cmd",
"=",
"'export {0}={1}\\n'",
"else",
":",
"cmd",
"=",
"'setenv {0} {1}\\n'",
"# write the environment config files",
"filename",
"=",
"os",
".",
"path",
".",
"join",
"(",
"out_dir",
",",
"name",
"+",
"ext",
")",
"with",
"open",
"(",
"filename",
",",
"'w'",
")",
"as",
"f",
":",
"f",
".",
"write",
"(",
"header",
"+",
"'\\n'",
")",
"for",
"key",
",",
"values",
"in",
"environ",
".",
"items",
"(",
")",
":",
"if",
"key",
"!=",
"'default'",
":",
"# write separator",
"f",
".",
"write",
"(",
"'#\\n# {0}\\n#\\n'",
".",
"format",
"(",
"key",
")",
")",
"# write tree names and paths",
"for",
"tree_name",
",",
"tree_path",
"in",
"values",
".",
"items",
"(",
")",
":",
"f",
".",
"write",
"(",
"cmd",
".",
"format",
"(",
"tree_name",
".",
"upper",
"(",
")",
",",
"tree_path",
")",
")",
"# write default .version file for modules",
"modules_version",
"=",
"write_version",
"(",
"name",
")",
"if",
"term",
"==",
"'modules'",
"and",
"environ",
"[",
"'default'",
"]",
"[",
"'current'",
"]",
":",
"version_name",
"=",
"os",
".",
"path",
".",
"join",
"(",
"out_dir",
",",
"'.version'",
")",
"with",
"open",
"(",
"version_name",
",",
"'w'",
")",
"as",
"f",
":",
"f",
".",
"write",
"(",
"modules_version",
")"
] |
Write a tree environment file
Loops over the tree environ and writes them out to a bash, tsch, or
modules file
Parameters:
environ (dict):
The tree dictionary environment
term (str):
The type of shell header to write, can be "bash", "tsch", or "modules"
tree_dir (str):
The path to this repository
out_dir (str):
The output path to write the files (default is etc/)
|
[
"Write",
"a",
"tree",
"environment",
"file"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/bin/setup_tree.py#L272-L319
|
train
|
sdss/tree
|
bin/setup_tree.py
|
get_tree
|
def get_tree(config=None):
''' Get the tree for a given config
Parameters:
config (str):
The name of the tree config to load
Returns:
a Python Tree instance
'''
path = os.path.dirname(os.path.abspath(__file__))
pypath = os.path.realpath(os.path.join(path, '..', 'python'))
if pypath not in sys.path:
sys.path.append(pypath)
os.chdir(pypath)
from tree.tree import Tree
tree = Tree(config=config)
return tree
|
python
|
def get_tree(config=None):
''' Get the tree for a given config
Parameters:
config (str):
The name of the tree config to load
Returns:
a Python Tree instance
'''
path = os.path.dirname(os.path.abspath(__file__))
pypath = os.path.realpath(os.path.join(path, '..', 'python'))
if pypath not in sys.path:
sys.path.append(pypath)
os.chdir(pypath)
from tree.tree import Tree
tree = Tree(config=config)
return tree
|
[
"def",
"get_tree",
"(",
"config",
"=",
"None",
")",
":",
"path",
"=",
"os",
".",
"path",
".",
"dirname",
"(",
"os",
".",
"path",
".",
"abspath",
"(",
"__file__",
")",
")",
"pypath",
"=",
"os",
".",
"path",
".",
"realpath",
"(",
"os",
".",
"path",
".",
"join",
"(",
"path",
",",
"'..'",
",",
"'python'",
")",
")",
"if",
"pypath",
"not",
"in",
"sys",
".",
"path",
":",
"sys",
".",
"path",
".",
"append",
"(",
"pypath",
")",
"os",
".",
"chdir",
"(",
"pypath",
")",
"from",
"tree",
".",
"tree",
"import",
"Tree",
"tree",
"=",
"Tree",
"(",
"config",
"=",
"config",
")",
"return",
"tree"
] |
Get the tree for a given config
Parameters:
config (str):
The name of the tree config to load
Returns:
a Python Tree instance
|
[
"Get",
"the",
"tree",
"for",
"a",
"given",
"config"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/bin/setup_tree.py#L322-L339
|
train
|
sdss/tree
|
bin/setup_tree.py
|
copy_modules
|
def copy_modules(filespath=None, modules_path=None, verbose=None):
''' Copy over the tree module files into your path '''
# find or define a modules path
if not modules_path:
modulepath = os.getenv("MODULEPATH")
if not modulepath:
modules_path = input('Enter the root path for your module files:')
else:
split_mods = modulepath.split(':')
if len(split_mods) > 1:
if verbose:
print('Multiple module paths found. Finding all that contain a tree directory.')
for mfile in split_mods:
if os.path.exists(os.path.join(mfile, 'tree')):
copy_modules(filespath=filespath, modules_path=mfile, verbose=verbose)
else:
return
else:
modules_path = split_mods[0]
# check for the tree module directory
tree_mod = os.path.join(modules_path, 'tree')
if not os.path.isdir(tree_mod):
os.makedirs(tree_mod)
# copy the modules into the tree
if verbose:
print('Copying modules from etc/ into {0}'.format(tree_mod))
module_files = glob.glob(os.path.join(filespath, '*.module'))
for mfile in module_files:
base = os.path.splitext(os.path.basename(mfile))[0]
tree_out = os.path.join(tree_mod, base)
shutil.copy2(mfile, tree_out)
# copy the default version into the tree
version = os.path.join(filespath, '.version')
if os.path.isfile(version):
shutil.copy2(version, tree_mod)
|
python
|
def copy_modules(filespath=None, modules_path=None, verbose=None):
''' Copy over the tree module files into your path '''
# find or define a modules path
if not modules_path:
modulepath = os.getenv("MODULEPATH")
if not modulepath:
modules_path = input('Enter the root path for your module files:')
else:
split_mods = modulepath.split(':')
if len(split_mods) > 1:
if verbose:
print('Multiple module paths found. Finding all that contain a tree directory.')
for mfile in split_mods:
if os.path.exists(os.path.join(mfile, 'tree')):
copy_modules(filespath=filespath, modules_path=mfile, verbose=verbose)
else:
return
else:
modules_path = split_mods[0]
# check for the tree module directory
tree_mod = os.path.join(modules_path, 'tree')
if not os.path.isdir(tree_mod):
os.makedirs(tree_mod)
# copy the modules into the tree
if verbose:
print('Copying modules from etc/ into {0}'.format(tree_mod))
module_files = glob.glob(os.path.join(filespath, '*.module'))
for mfile in module_files:
base = os.path.splitext(os.path.basename(mfile))[0]
tree_out = os.path.join(tree_mod, base)
shutil.copy2(mfile, tree_out)
# copy the default version into the tree
version = os.path.join(filespath, '.version')
if os.path.isfile(version):
shutil.copy2(version, tree_mod)
|
[
"def",
"copy_modules",
"(",
"filespath",
"=",
"None",
",",
"modules_path",
"=",
"None",
",",
"verbose",
"=",
"None",
")",
":",
"# find or define a modules path",
"if",
"not",
"modules_path",
":",
"modulepath",
"=",
"os",
".",
"getenv",
"(",
"\"MODULEPATH\"",
")",
"if",
"not",
"modulepath",
":",
"modules_path",
"=",
"input",
"(",
"'Enter the root path for your module files:'",
")",
"else",
":",
"split_mods",
"=",
"modulepath",
".",
"split",
"(",
"':'",
")",
"if",
"len",
"(",
"split_mods",
")",
">",
"1",
":",
"if",
"verbose",
":",
"print",
"(",
"'Multiple module paths found. Finding all that contain a tree directory.'",
")",
"for",
"mfile",
"in",
"split_mods",
":",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"os",
".",
"path",
".",
"join",
"(",
"mfile",
",",
"'tree'",
")",
")",
":",
"copy_modules",
"(",
"filespath",
"=",
"filespath",
",",
"modules_path",
"=",
"mfile",
",",
"verbose",
"=",
"verbose",
")",
"else",
":",
"return",
"else",
":",
"modules_path",
"=",
"split_mods",
"[",
"0",
"]",
"# check for the tree module directory",
"tree_mod",
"=",
"os",
".",
"path",
".",
"join",
"(",
"modules_path",
",",
"'tree'",
")",
"if",
"not",
"os",
".",
"path",
".",
"isdir",
"(",
"tree_mod",
")",
":",
"os",
".",
"makedirs",
"(",
"tree_mod",
")",
"# copy the modules into the tree",
"if",
"verbose",
":",
"print",
"(",
"'Copying modules from etc/ into {0}'",
".",
"format",
"(",
"tree_mod",
")",
")",
"module_files",
"=",
"glob",
".",
"glob",
"(",
"os",
".",
"path",
".",
"join",
"(",
"filespath",
",",
"'*.module'",
")",
")",
"for",
"mfile",
"in",
"module_files",
":",
"base",
"=",
"os",
".",
"path",
".",
"splitext",
"(",
"os",
".",
"path",
".",
"basename",
"(",
"mfile",
")",
")",
"[",
"0",
"]",
"tree_out",
"=",
"os",
".",
"path",
".",
"join",
"(",
"tree_mod",
",",
"base",
")",
"shutil",
".",
"copy2",
"(",
"mfile",
",",
"tree_out",
")",
"# copy the default version into the tree",
"version",
"=",
"os",
".",
"path",
".",
"join",
"(",
"filespath",
",",
"'.version'",
")",
"if",
"os",
".",
"path",
".",
"isfile",
"(",
"version",
")",
":",
"shutil",
".",
"copy2",
"(",
"version",
",",
"tree_mod",
")"
] |
Copy over the tree module files into your path
|
[
"Copy",
"over",
"the",
"tree",
"module",
"files",
"into",
"your",
"path"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/bin/setup_tree.py#L342-L380
|
train
|
sdss/tree
|
python/tree/misc/docutree.py
|
_indent
|
def _indent(text, level=1):
''' Does a proper indenting for Sphinx rst '''
prefix = ' ' * (4 * level)
def prefixed_lines():
for line in text.splitlines(True):
yield (prefix + line if line.strip() else line)
return ''.join(prefixed_lines())
|
python
|
def _indent(text, level=1):
''' Does a proper indenting for Sphinx rst '''
prefix = ' ' * (4 * level)
def prefixed_lines():
for line in text.splitlines(True):
yield (prefix + line if line.strip() else line)
return ''.join(prefixed_lines())
|
[
"def",
"_indent",
"(",
"text",
",",
"level",
"=",
"1",
")",
":",
"prefix",
"=",
"' '",
"*",
"(",
"4",
"*",
"level",
")",
"def",
"prefixed_lines",
"(",
")",
":",
"for",
"line",
"in",
"text",
".",
"splitlines",
"(",
"True",
")",
":",
"yield",
"(",
"prefix",
"+",
"line",
"if",
"line",
".",
"strip",
"(",
")",
"else",
"line",
")",
"return",
"''",
".",
"join",
"(",
"prefixed_lines",
"(",
")",
")"
] |
Does a proper indenting for Sphinx rst
|
[
"Does",
"a",
"proper",
"indenting",
"for",
"Sphinx",
"rst"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/python/tree/misc/docutree.py#L22-L31
|
train
|
sdss/tree
|
setup.py
|
get_requirements
|
def get_requirements(opts):
''' Get the proper requirements file based on the optional argument '''
if opts.dev:
name = 'requirements_dev.txt'
elif opts.doc:
name = 'requirements_doc.txt'
else:
name = 'requirements.txt'
requirements_file = os.path.join(os.path.dirname(__file__), name)
install_requires = [line.strip().replace('==', '>=') for line in open(requirements_file)
if not line.strip().startswith('#') and line.strip() != '']
return install_requires
|
python
|
def get_requirements(opts):
''' Get the proper requirements file based on the optional argument '''
if opts.dev:
name = 'requirements_dev.txt'
elif opts.doc:
name = 'requirements_doc.txt'
else:
name = 'requirements.txt'
requirements_file = os.path.join(os.path.dirname(__file__), name)
install_requires = [line.strip().replace('==', '>=') for line in open(requirements_file)
if not line.strip().startswith('#') and line.strip() != '']
return install_requires
|
[
"def",
"get_requirements",
"(",
"opts",
")",
":",
"if",
"opts",
".",
"dev",
":",
"name",
"=",
"'requirements_dev.txt'",
"elif",
"opts",
".",
"doc",
":",
"name",
"=",
"'requirements_doc.txt'",
"else",
":",
"name",
"=",
"'requirements.txt'",
"requirements_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"os",
".",
"path",
".",
"dirname",
"(",
"__file__",
")",
",",
"name",
")",
"install_requires",
"=",
"[",
"line",
".",
"strip",
"(",
")",
".",
"replace",
"(",
"'=='",
",",
"'>='",
")",
"for",
"line",
"in",
"open",
"(",
"requirements_file",
")",
"if",
"not",
"line",
".",
"strip",
"(",
")",
".",
"startswith",
"(",
"'#'",
")",
"and",
"line",
".",
"strip",
"(",
")",
"!=",
"''",
"]",
"return",
"install_requires"
] |
Get the proper requirements file based on the optional argument
|
[
"Get",
"the",
"proper",
"requirements",
"file",
"based",
"on",
"the",
"optional",
"argument"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/setup.py#L54-L67
|
train
|
sdss/tree
|
setup.py
|
remove_args
|
def remove_args(parser):
''' Remove custom arguments from the parser '''
arguments = []
for action in list(parser._get_optional_actions()):
if '--help' not in action.option_strings:
arguments += action.option_strings
for arg in arguments:
if arg in sys.argv:
sys.argv.remove(arg)
|
python
|
def remove_args(parser):
''' Remove custom arguments from the parser '''
arguments = []
for action in list(parser._get_optional_actions()):
if '--help' not in action.option_strings:
arguments += action.option_strings
for arg in arguments:
if arg in sys.argv:
sys.argv.remove(arg)
|
[
"def",
"remove_args",
"(",
"parser",
")",
":",
"arguments",
"=",
"[",
"]",
"for",
"action",
"in",
"list",
"(",
"parser",
".",
"_get_optional_actions",
"(",
")",
")",
":",
"if",
"'--help'",
"not",
"in",
"action",
".",
"option_strings",
":",
"arguments",
"+=",
"action",
".",
"option_strings",
"for",
"arg",
"in",
"arguments",
":",
"if",
"arg",
"in",
"sys",
".",
"argv",
":",
"sys",
".",
"argv",
".",
"remove",
"(",
"arg",
")"
] |
Remove custom arguments from the parser
|
[
"Remove",
"custom",
"arguments",
"from",
"the",
"parser"
] |
f61fe0876c138ccb61874912d4b8590dadfa835c
|
https://github.com/sdss/tree/blob/f61fe0876c138ccb61874912d4b8590dadfa835c/setup.py#L70-L80
|
train
|
sarugaku/shellingham
|
tasks/__init__.py
|
_render_log
|
def _render_log():
"""Totally tap into Towncrier internals to get an in-memory result.
"""
config = load_config(ROOT)
definitions = config['types']
fragments, fragment_filenames = find_fragments(
pathlib.Path(config['directory']).absolute(),
config['sections'],
None,
definitions,
)
rendered = render_fragments(
pathlib.Path(config['template']).read_text(encoding='utf-8'),
config['issue_format'],
split_fragments(fragments, definitions),
definitions,
config['underlines'][1:],
)
return rendered
|
python
|
def _render_log():
"""Totally tap into Towncrier internals to get an in-memory result.
"""
config = load_config(ROOT)
definitions = config['types']
fragments, fragment_filenames = find_fragments(
pathlib.Path(config['directory']).absolute(),
config['sections'],
None,
definitions,
)
rendered = render_fragments(
pathlib.Path(config['template']).read_text(encoding='utf-8'),
config['issue_format'],
split_fragments(fragments, definitions),
definitions,
config['underlines'][1:],
)
return rendered
|
[
"def",
"_render_log",
"(",
")",
":",
"config",
"=",
"load_config",
"(",
"ROOT",
")",
"definitions",
"=",
"config",
"[",
"'types'",
"]",
"fragments",
",",
"fragment_filenames",
"=",
"find_fragments",
"(",
"pathlib",
".",
"Path",
"(",
"config",
"[",
"'directory'",
"]",
")",
".",
"absolute",
"(",
")",
",",
"config",
"[",
"'sections'",
"]",
",",
"None",
",",
"definitions",
",",
")",
"rendered",
"=",
"render_fragments",
"(",
"pathlib",
".",
"Path",
"(",
"config",
"[",
"'template'",
"]",
")",
".",
"read_text",
"(",
"encoding",
"=",
"'utf-8'",
")",
",",
"config",
"[",
"'issue_format'",
"]",
",",
"split_fragments",
"(",
"fragments",
",",
"definitions",
")",
",",
"definitions",
",",
"config",
"[",
"'underlines'",
"]",
"[",
"1",
":",
"]",
",",
")",
"return",
"rendered"
] |
Totally tap into Towncrier internals to get an in-memory result.
|
[
"Totally",
"tap",
"into",
"Towncrier",
"internals",
"to",
"get",
"an",
"in",
"-",
"memory",
"result",
"."
] |
295fc3094ef05437597ea0baa02f5cd7a3335d28
|
https://github.com/sarugaku/shellingham/blob/295fc3094ef05437597ea0baa02f5cd7a3335d28/tasks/__init__.py#L49-L67
|
train
|
sods/paramz
|
paramz/core/nameable.py
|
adjust_name_for_printing
|
def adjust_name_for_printing(name):
"""
Make sure a name can be printed, alongside used as a variable name.
"""
if name is not None:
name2 = name
name = name.replace(" ", "_").replace(".", "_").replace("-", "_m_")
name = name.replace("+", "_p_").replace("!", "_I_")
name = name.replace("**", "_xx_").replace("*", "_x_")
name = name.replace("/", "_l_").replace("@", '_at_')
name = name.replace("(", "_of_").replace(")", "")
if re.match(r'^[a-zA-Z_][a-zA-Z0-9-_]*$', name) is None:
raise NameError("name {} converted to {} cannot be further converted to valid python variable name!".format(name2, name))
return name
return ''
|
python
|
def adjust_name_for_printing(name):
"""
Make sure a name can be printed, alongside used as a variable name.
"""
if name is not None:
name2 = name
name = name.replace(" ", "_").replace(".", "_").replace("-", "_m_")
name = name.replace("+", "_p_").replace("!", "_I_")
name = name.replace("**", "_xx_").replace("*", "_x_")
name = name.replace("/", "_l_").replace("@", '_at_')
name = name.replace("(", "_of_").replace(")", "")
if re.match(r'^[a-zA-Z_][a-zA-Z0-9-_]*$', name) is None:
raise NameError("name {} converted to {} cannot be further converted to valid python variable name!".format(name2, name))
return name
return ''
|
[
"def",
"adjust_name_for_printing",
"(",
"name",
")",
":",
"if",
"name",
"is",
"not",
"None",
":",
"name2",
"=",
"name",
"name",
"=",
"name",
".",
"replace",
"(",
"\" \"",
",",
"\"_\"",
")",
".",
"replace",
"(",
"\".\"",
",",
"\"_\"",
")",
".",
"replace",
"(",
"\"-\"",
",",
"\"_m_\"",
")",
"name",
"=",
"name",
".",
"replace",
"(",
"\"+\"",
",",
"\"_p_\"",
")",
".",
"replace",
"(",
"\"!\"",
",",
"\"_I_\"",
")",
"name",
"=",
"name",
".",
"replace",
"(",
"\"**\"",
",",
"\"_xx_\"",
")",
".",
"replace",
"(",
"\"*\"",
",",
"\"_x_\"",
")",
"name",
"=",
"name",
".",
"replace",
"(",
"\"/\"",
",",
"\"_l_\"",
")",
".",
"replace",
"(",
"\"@\"",
",",
"'_at_'",
")",
"name",
"=",
"name",
".",
"replace",
"(",
"\"(\"",
",",
"\"_of_\"",
")",
".",
"replace",
"(",
"\")\"",
",",
"\"\"",
")",
"if",
"re",
".",
"match",
"(",
"r'^[a-zA-Z_][a-zA-Z0-9-_]*$'",
",",
"name",
")",
"is",
"None",
":",
"raise",
"NameError",
"(",
"\"name {} converted to {} cannot be further converted to valid python variable name!\"",
".",
"format",
"(",
"name2",
",",
"name",
")",
")",
"return",
"name",
"return",
"''"
] |
Make sure a name can be printed, alongside used as a variable name.
|
[
"Make",
"sure",
"a",
"name",
"can",
"be",
"printed",
"alongside",
"used",
"as",
"a",
"variable",
"name",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/nameable.py#L33-L47
|
train
|
sods/paramz
|
paramz/core/nameable.py
|
Nameable.name
|
def name(self, name):
"""
Set the name of this object.
Tell the parent if the name has changed.
"""
from_name = self.name
assert isinstance(name, str)
self._name = name
if self.has_parent():
self._parent_._name_changed(self, from_name)
|
python
|
def name(self, name):
"""
Set the name of this object.
Tell the parent if the name has changed.
"""
from_name = self.name
assert isinstance(name, str)
self._name = name
if self.has_parent():
self._parent_._name_changed(self, from_name)
|
[
"def",
"name",
"(",
"self",
",",
"name",
")",
":",
"from_name",
"=",
"self",
".",
"name",
"assert",
"isinstance",
"(",
"name",
",",
"str",
")",
"self",
".",
"_name",
"=",
"name",
"if",
"self",
".",
"has_parent",
"(",
")",
":",
"self",
".",
"_parent_",
".",
"_name_changed",
"(",
"self",
",",
"from_name",
")"
] |
Set the name of this object.
Tell the parent if the name has changed.
|
[
"Set",
"the",
"name",
"of",
"this",
"object",
".",
"Tell",
"the",
"parent",
"if",
"the",
"name",
"has",
"changed",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/nameable.py#L65-L74
|
train
|
sods/paramz
|
paramz/core/nameable.py
|
Nameable.hierarchy_name
|
def hierarchy_name(self, adjust_for_printing=True):
"""
return the name for this object with the parents names attached by dots.
:param bool adjust_for_printing: whether to call :func:`~adjust_for_printing()`
on the names, recursively
"""
if adjust_for_printing: adjust = lambda x: adjust_name_for_printing(x)
else: adjust = lambda x: x
if self.has_parent():
return self._parent_.hierarchy_name() + "." + adjust(self.name)
return adjust(self.name)
|
python
|
def hierarchy_name(self, adjust_for_printing=True):
"""
return the name for this object with the parents names attached by dots.
:param bool adjust_for_printing: whether to call :func:`~adjust_for_printing()`
on the names, recursively
"""
if adjust_for_printing: adjust = lambda x: adjust_name_for_printing(x)
else: adjust = lambda x: x
if self.has_parent():
return self._parent_.hierarchy_name() + "." + adjust(self.name)
return adjust(self.name)
|
[
"def",
"hierarchy_name",
"(",
"self",
",",
"adjust_for_printing",
"=",
"True",
")",
":",
"if",
"adjust_for_printing",
":",
"adjust",
"=",
"lambda",
"x",
":",
"adjust_name_for_printing",
"(",
"x",
")",
"else",
":",
"adjust",
"=",
"lambda",
"x",
":",
"x",
"if",
"self",
".",
"has_parent",
"(",
")",
":",
"return",
"self",
".",
"_parent_",
".",
"hierarchy_name",
"(",
")",
"+",
"\".\"",
"+",
"adjust",
"(",
"self",
".",
"name",
")",
"return",
"adjust",
"(",
"self",
".",
"name",
")"
] |
return the name for this object with the parents names attached by dots.
:param bool adjust_for_printing: whether to call :func:`~adjust_for_printing()`
on the names, recursively
|
[
"return",
"the",
"name",
"for",
"this",
"object",
"with",
"the",
"parents",
"names",
"attached",
"by",
"dots",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/nameable.py#L76-L88
|
train
|
sods/paramz
|
paramz/parameterized.py
|
Parameterized.grep_param_names
|
def grep_param_names(self, regexp):
"""
create a list of parameters, matching regular expression regexp
"""
if not isinstance(regexp, _pattern_type): regexp = compile(regexp)
found_params = []
def visit(innerself, regexp):
if (innerself is not self) and regexp.match(innerself.hierarchy_name().partition('.')[2]):
found_params.append(innerself)
self.traverse(visit, regexp)
return found_params
|
python
|
def grep_param_names(self, regexp):
"""
create a list of parameters, matching regular expression regexp
"""
if not isinstance(regexp, _pattern_type): regexp = compile(regexp)
found_params = []
def visit(innerself, regexp):
if (innerself is not self) and regexp.match(innerself.hierarchy_name().partition('.')[2]):
found_params.append(innerself)
self.traverse(visit, regexp)
return found_params
|
[
"def",
"grep_param_names",
"(",
"self",
",",
"regexp",
")",
":",
"if",
"not",
"isinstance",
"(",
"regexp",
",",
"_pattern_type",
")",
":",
"regexp",
"=",
"compile",
"(",
"regexp",
")",
"found_params",
"=",
"[",
"]",
"def",
"visit",
"(",
"innerself",
",",
"regexp",
")",
":",
"if",
"(",
"innerself",
"is",
"not",
"self",
")",
"and",
"regexp",
".",
"match",
"(",
"innerself",
".",
"hierarchy_name",
"(",
")",
".",
"partition",
"(",
"'.'",
")",
"[",
"2",
"]",
")",
":",
"found_params",
".",
"append",
"(",
"innerself",
")",
"self",
".",
"traverse",
"(",
"visit",
",",
"regexp",
")",
"return",
"found_params"
] |
create a list of parameters, matching regular expression regexp
|
[
"create",
"a",
"list",
"of",
"parameters",
"matching",
"regular",
"expression",
"regexp"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/parameterized.py#L282-L292
|
train
|
sods/paramz
|
paramz/param.py
|
Param._setup_observers
|
def _setup_observers(self):
"""
Setup the default observers
1: pass through to parent, if present
"""
if self.has_parent():
self.add_observer(self._parent_, self._parent_._pass_through_notify_observers, -np.inf)
|
python
|
def _setup_observers(self):
"""
Setup the default observers
1: pass through to parent, if present
"""
if self.has_parent():
self.add_observer(self._parent_, self._parent_._pass_through_notify_observers, -np.inf)
|
[
"def",
"_setup_observers",
"(",
"self",
")",
":",
"if",
"self",
".",
"has_parent",
"(",
")",
":",
"self",
".",
"add_observer",
"(",
"self",
".",
"_parent_",
",",
"self",
".",
"_parent_",
".",
"_pass_through_notify_observers",
",",
"-",
"np",
".",
"inf",
")"
] |
Setup the default observers
1: pass through to parent, if present
|
[
"Setup",
"the",
"default",
"observers"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/param.py#L211-L218
|
train
|
sods/paramz
|
paramz/param.py
|
Param._repr_html_
|
def _repr_html_(self, indices=None, iops=None, lx=None, li=None, lls=None):
"""Representation of the parameter in html for notebook display."""
filter_ = self._current_slice_
vals = self.flat
if indices is None: indices = self._indices(filter_)
if iops is None:
ravi = self._raveled_index(filter_)
iops = OrderedDict([name, iop.properties_for(ravi)] for name, iop in self._index_operations.items())
if lls is None: lls = [self._max_len_names(iop, name) for name, iop in iops.items()]
header_format = """
<tr>
<th><b>{i}</b></th>
<th><b>{x}</b></th>
<th><b>{iops}</b></th>
</tr>"""
header = header_format.format(x=self.hierarchy_name(), i=__index_name__, iops="</b></th><th><b>".join(list(iops.keys()))) # nice header for printing
to_print = ["""<style type="text/css">
.tg {padding:2px 3px;word-break:normal;border-collapse:collapse;border-spacing:0;border-color:#DCDCDC;margin:0px auto;width:100%;}
.tg td{font-family:"Courier New", Courier, monospace !important;font-weight:bold;color:#444;background-color:#F7FDFA;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}
.tg th{font-family:"Courier New", Courier, monospace !important;font-weight:normal;color:#fff;background-color:#26ADE4;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}
.tg .tg-left{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:left;}
.tg .tg-right{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:right;}
</style>"""]
to_print.append('<table class="tg">')
to_print.append(header)
format_spec = self._format_spec(indices, iops, lx, li, lls, False)
format_spec[:2] = ["<tr><td class=tg-left>{i}</td>".format(i=format_spec[0]), "<td class=tg-right>{i}</td>".format(i=format_spec[1])]
for i in range(2, len(format_spec)):
format_spec[i] = '<td class=tg-left>{c}</td>'.format(c=format_spec[i])
format_spec = "".join(format_spec) + '</tr>'
for i in range(self.size):
to_print.append(format_spec.format(index=indices[i], value="{1:.{0}f}".format(__precision__, vals[i]), **dict((name, ' '.join(map(str, iops[name][i]))) for name in iops)))
return '\n'.join(to_print)
|
python
|
def _repr_html_(self, indices=None, iops=None, lx=None, li=None, lls=None):
"""Representation of the parameter in html for notebook display."""
filter_ = self._current_slice_
vals = self.flat
if indices is None: indices = self._indices(filter_)
if iops is None:
ravi = self._raveled_index(filter_)
iops = OrderedDict([name, iop.properties_for(ravi)] for name, iop in self._index_operations.items())
if lls is None: lls = [self._max_len_names(iop, name) for name, iop in iops.items()]
header_format = """
<tr>
<th><b>{i}</b></th>
<th><b>{x}</b></th>
<th><b>{iops}</b></th>
</tr>"""
header = header_format.format(x=self.hierarchy_name(), i=__index_name__, iops="</b></th><th><b>".join(list(iops.keys()))) # nice header for printing
to_print = ["""<style type="text/css">
.tg {padding:2px 3px;word-break:normal;border-collapse:collapse;border-spacing:0;border-color:#DCDCDC;margin:0px auto;width:100%;}
.tg td{font-family:"Courier New", Courier, monospace !important;font-weight:bold;color:#444;background-color:#F7FDFA;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}
.tg th{font-family:"Courier New", Courier, monospace !important;font-weight:normal;color:#fff;background-color:#26ADE4;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}
.tg .tg-left{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:left;}
.tg .tg-right{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:right;}
</style>"""]
to_print.append('<table class="tg">')
to_print.append(header)
format_spec = self._format_spec(indices, iops, lx, li, lls, False)
format_spec[:2] = ["<tr><td class=tg-left>{i}</td>".format(i=format_spec[0]), "<td class=tg-right>{i}</td>".format(i=format_spec[1])]
for i in range(2, len(format_spec)):
format_spec[i] = '<td class=tg-left>{c}</td>'.format(c=format_spec[i])
format_spec = "".join(format_spec) + '</tr>'
for i in range(self.size):
to_print.append(format_spec.format(index=indices[i], value="{1:.{0}f}".format(__precision__, vals[i]), **dict((name, ' '.join(map(str, iops[name][i]))) for name in iops)))
return '\n'.join(to_print)
|
[
"def",
"_repr_html_",
"(",
"self",
",",
"indices",
"=",
"None",
",",
"iops",
"=",
"None",
",",
"lx",
"=",
"None",
",",
"li",
"=",
"None",
",",
"lls",
"=",
"None",
")",
":",
"filter_",
"=",
"self",
".",
"_current_slice_",
"vals",
"=",
"self",
".",
"flat",
"if",
"indices",
"is",
"None",
":",
"indices",
"=",
"self",
".",
"_indices",
"(",
"filter_",
")",
"if",
"iops",
"is",
"None",
":",
"ravi",
"=",
"self",
".",
"_raveled_index",
"(",
"filter_",
")",
"iops",
"=",
"OrderedDict",
"(",
"[",
"name",
",",
"iop",
".",
"properties_for",
"(",
"ravi",
")",
"]",
"for",
"name",
",",
"iop",
"in",
"self",
".",
"_index_operations",
".",
"items",
"(",
")",
")",
"if",
"lls",
"is",
"None",
":",
"lls",
"=",
"[",
"self",
".",
"_max_len_names",
"(",
"iop",
",",
"name",
")",
"for",
"name",
",",
"iop",
"in",
"iops",
".",
"items",
"(",
")",
"]",
"header_format",
"=",
"\"\"\"\n<tr>\n <th><b>{i}</b></th>\n <th><b>{x}</b></th>\n <th><b>{iops}</b></th>\n</tr>\"\"\"",
"header",
"=",
"header_format",
".",
"format",
"(",
"x",
"=",
"self",
".",
"hierarchy_name",
"(",
")",
",",
"i",
"=",
"__index_name__",
",",
"iops",
"=",
"\"</b></th><th><b>\"",
".",
"join",
"(",
"list",
"(",
"iops",
".",
"keys",
"(",
")",
")",
")",
")",
"# nice header for printing",
"to_print",
"=",
"[",
"\"\"\"<style type=\"text/css\">\n.tg {padding:2px 3px;word-break:normal;border-collapse:collapse;border-spacing:0;border-color:#DCDCDC;margin:0px auto;width:100%;}\n.tg td{font-family:\"Courier New\", Courier, monospace !important;font-weight:bold;color:#444;background-color:#F7FDFA;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}\n.tg th{font-family:\"Courier New\", Courier, monospace !important;font-weight:normal;color:#fff;background-color:#26ADE4;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}\n.tg .tg-left{font-family:\"Courier New\", Courier, monospace !important;font-weight:normal;text-align:left;}\n.tg .tg-right{font-family:\"Courier New\", Courier, monospace !important;font-weight:normal;text-align:right;}\n</style>\"\"\"",
"]",
"to_print",
".",
"append",
"(",
"'<table class=\"tg\">'",
")",
"to_print",
".",
"append",
"(",
"header",
")",
"format_spec",
"=",
"self",
".",
"_format_spec",
"(",
"indices",
",",
"iops",
",",
"lx",
",",
"li",
",",
"lls",
",",
"False",
")",
"format_spec",
"[",
":",
"2",
"]",
"=",
"[",
"\"<tr><td class=tg-left>{i}</td>\"",
".",
"format",
"(",
"i",
"=",
"format_spec",
"[",
"0",
"]",
")",
",",
"\"<td class=tg-right>{i}</td>\"",
".",
"format",
"(",
"i",
"=",
"format_spec",
"[",
"1",
"]",
")",
"]",
"for",
"i",
"in",
"range",
"(",
"2",
",",
"len",
"(",
"format_spec",
")",
")",
":",
"format_spec",
"[",
"i",
"]",
"=",
"'<td class=tg-left>{c}</td>'",
".",
"format",
"(",
"c",
"=",
"format_spec",
"[",
"i",
"]",
")",
"format_spec",
"=",
"\"\"",
".",
"join",
"(",
"format_spec",
")",
"+",
"'</tr>'",
"for",
"i",
"in",
"range",
"(",
"self",
".",
"size",
")",
":",
"to_print",
".",
"append",
"(",
"format_spec",
".",
"format",
"(",
"index",
"=",
"indices",
"[",
"i",
"]",
",",
"value",
"=",
"\"{1:.{0}f}\"",
".",
"format",
"(",
"__precision__",
",",
"vals",
"[",
"i",
"]",
")",
",",
"*",
"*",
"dict",
"(",
"(",
"name",
",",
"' '",
".",
"join",
"(",
"map",
"(",
"str",
",",
"iops",
"[",
"name",
"]",
"[",
"i",
"]",
")",
")",
")",
"for",
"name",
"in",
"iops",
")",
")",
")",
"return",
"'\\n'",
".",
"join",
"(",
"to_print",
")"
] |
Representation of the parameter in html for notebook display.
|
[
"Representation",
"of",
"the",
"parameter",
"in",
"html",
"for",
"notebook",
"display",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/param.py#L275-L311
|
train
|
sods/paramz
|
paramz/core/observable.py
|
Observable.add_observer
|
def add_observer(self, observer, callble, priority=0):
"""
Add an observer `observer` with the callback `callble`
and priority `priority` to this observers list.
"""
self.observers.add(priority, observer, callble)
|
python
|
def add_observer(self, observer, callble, priority=0):
"""
Add an observer `observer` with the callback `callble`
and priority `priority` to this observers list.
"""
self.observers.add(priority, observer, callble)
|
[
"def",
"add_observer",
"(",
"self",
",",
"observer",
",",
"callble",
",",
"priority",
"=",
"0",
")",
":",
"self",
".",
"observers",
".",
"add",
"(",
"priority",
",",
"observer",
",",
"callble",
")"
] |
Add an observer `observer` with the callback `callble`
and priority `priority` to this observers list.
|
[
"Add",
"an",
"observer",
"observer",
"with",
"the",
"callback",
"callble",
"and",
"priority",
"priority",
"to",
"this",
"observers",
"list",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/observable.py#L49-L54
|
train
|
sods/paramz
|
paramz/core/observable.py
|
Observable.notify_observers
|
def notify_observers(self, which=None, min_priority=None):
"""
Notifies all observers. Which is the element, which kicked off this
notification loop. The first argument will be self, the second `which`.
.. note::
notifies only observers with priority p > min_priority!
:param min_priority: only notify observers with priority > min_priority
if min_priority is None, notify all observers in order
"""
if self._update_on:
if which is None:
which = self
if min_priority is None:
[callble(self, which=which) for _, _, callble in self.observers]
else:
for p, _, callble in self.observers:
if p <= min_priority:
break
callble(self, which=which)
|
python
|
def notify_observers(self, which=None, min_priority=None):
"""
Notifies all observers. Which is the element, which kicked off this
notification loop. The first argument will be self, the second `which`.
.. note::
notifies only observers with priority p > min_priority!
:param min_priority: only notify observers with priority > min_priority
if min_priority is None, notify all observers in order
"""
if self._update_on:
if which is None:
which = self
if min_priority is None:
[callble(self, which=which) for _, _, callble in self.observers]
else:
for p, _, callble in self.observers:
if p <= min_priority:
break
callble(self, which=which)
|
[
"def",
"notify_observers",
"(",
"self",
",",
"which",
"=",
"None",
",",
"min_priority",
"=",
"None",
")",
":",
"if",
"self",
".",
"_update_on",
":",
"if",
"which",
"is",
"None",
":",
"which",
"=",
"self",
"if",
"min_priority",
"is",
"None",
":",
"[",
"callble",
"(",
"self",
",",
"which",
"=",
"which",
")",
"for",
"_",
",",
"_",
",",
"callble",
"in",
"self",
".",
"observers",
"]",
"else",
":",
"for",
"p",
",",
"_",
",",
"callble",
"in",
"self",
".",
"observers",
":",
"if",
"p",
"<=",
"min_priority",
":",
"break",
"callble",
"(",
"self",
",",
"which",
"=",
"which",
")"
] |
Notifies all observers. Which is the element, which kicked off this
notification loop. The first argument will be self, the second `which`.
.. note::
notifies only observers with priority p > min_priority!
:param min_priority: only notify observers with priority > min_priority
if min_priority is None, notify all observers in order
|
[
"Notifies",
"all",
"observers",
".",
"Which",
"is",
"the",
"element",
"which",
"kicked",
"off",
"this",
"notification",
"loop",
".",
"The",
"first",
"argument",
"will",
"be",
"self",
"the",
"second",
"which",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/observable.py#L75-L96
|
train
|
sods/paramz
|
paramz/core/constrainable.py
|
Constrainable.constrain_fixed
|
def constrain_fixed(self, value=None, warning=True, trigger_parent=True):
"""
Constrain this parameter to be fixed to the current value it carries.
This does not override the previous constraints, so unfixing will
restore the constraint set before fixing.
:param warning: print a warning for overwriting constraints.
"""
if value is not None:
self[:] = value
#index = self.unconstrain()
index = self._add_to_index_operations(self.constraints, np.empty(0), __fixed__, warning)
self._highest_parent_._set_fixed(self, index)
self.notify_observers(self, None if trigger_parent else -np.inf)
return index
|
python
|
def constrain_fixed(self, value=None, warning=True, trigger_parent=True):
"""
Constrain this parameter to be fixed to the current value it carries.
This does not override the previous constraints, so unfixing will
restore the constraint set before fixing.
:param warning: print a warning for overwriting constraints.
"""
if value is not None:
self[:] = value
#index = self.unconstrain()
index = self._add_to_index_operations(self.constraints, np.empty(0), __fixed__, warning)
self._highest_parent_._set_fixed(self, index)
self.notify_observers(self, None if trigger_parent else -np.inf)
return index
|
[
"def",
"constrain_fixed",
"(",
"self",
",",
"value",
"=",
"None",
",",
"warning",
"=",
"True",
",",
"trigger_parent",
"=",
"True",
")",
":",
"if",
"value",
"is",
"not",
"None",
":",
"self",
"[",
":",
"]",
"=",
"value",
"#index = self.unconstrain()",
"index",
"=",
"self",
".",
"_add_to_index_operations",
"(",
"self",
".",
"constraints",
",",
"np",
".",
"empty",
"(",
"0",
")",
",",
"__fixed__",
",",
"warning",
")",
"self",
".",
"_highest_parent_",
".",
"_set_fixed",
"(",
"self",
",",
"index",
")",
"self",
".",
"notify_observers",
"(",
"self",
",",
"None",
"if",
"trigger_parent",
"else",
"-",
"np",
".",
"inf",
")",
"return",
"index"
] |
Constrain this parameter to be fixed to the current value it carries.
This does not override the previous constraints, so unfixing will
restore the constraint set before fixing.
:param warning: print a warning for overwriting constraints.
|
[
"Constrain",
"this",
"parameter",
"to",
"be",
"fixed",
"to",
"the",
"current",
"value",
"it",
"carries",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/constrainable.py#L52-L68
|
train
|
sods/paramz
|
paramz/core/constrainable.py
|
Constrainable.unconstrain_fixed
|
def unconstrain_fixed(self):
"""
This parameter will no longer be fixed.
If there was a constraint on this parameter when fixing it,
it will be constraint with that previous constraint.
"""
unconstrained = self.unconstrain(__fixed__)
self._highest_parent_._set_unfixed(self, unconstrained)
#if self._default_constraint_ is not None:
# return self.constrain(self._default_constraint_)
return unconstrained
|
python
|
def unconstrain_fixed(self):
"""
This parameter will no longer be fixed.
If there was a constraint on this parameter when fixing it,
it will be constraint with that previous constraint.
"""
unconstrained = self.unconstrain(__fixed__)
self._highest_parent_._set_unfixed(self, unconstrained)
#if self._default_constraint_ is not None:
# return self.constrain(self._default_constraint_)
return unconstrained
|
[
"def",
"unconstrain_fixed",
"(",
"self",
")",
":",
"unconstrained",
"=",
"self",
".",
"unconstrain",
"(",
"__fixed__",
")",
"self",
".",
"_highest_parent_",
".",
"_set_unfixed",
"(",
"self",
",",
"unconstrained",
")",
"#if self._default_constraint_ is not None:",
"# return self.constrain(self._default_constraint_)",
"return",
"unconstrained"
] |
This parameter will no longer be fixed.
If there was a constraint on this parameter when fixing it,
it will be constraint with that previous constraint.
|
[
"This",
"parameter",
"will",
"no",
"longer",
"be",
"fixed",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/constrainable.py#L71-L82
|
train
|
sods/paramz
|
paramz/core/gradcheckable.py
|
Gradcheckable.checkgrad
|
def checkgrad(self, verbose=0, step=1e-6, tolerance=1e-3, df_tolerance=1e-12):
"""
Check the gradient of this parameter with respect to the highest parent's
objective function.
This is a three point estimate of the gradient, wiggling at the parameters
with a stepsize step.
The check passes if either the ratio or the difference between numerical and
analytical gradient is smaller then tolerance.
:param bool verbose: whether each parameter shall be checked individually.
:param float step: the stepsize for the numerical three point gradient estimate.
:param float tolerance: the tolerance for the gradient ratio or difference.
:param float df_tolerance: the tolerance for df_tolerance
.. note::
The *dF_ratio* indicates the limit of accuracy of numerical gradients.
If it is too small, e.g., smaller than 1e-12, the numerical gradients
are usually not accurate enough for the tests (shown with blue).
"""
# Make sure we always call the gradcheck on the highest parent
# This ensures the assumption of the highest parent to hold the fixes
# In the checkgrad function we take advantage of that, so it needs
# to be set in place here.
if self.has_parent():
return self._highest_parent_._checkgrad(self, verbose=verbose, step=step, tolerance=tolerance, df_tolerance=df_tolerance)
return self._checkgrad(self, verbose=verbose, step=step, tolerance=tolerance, df_tolerance=df_tolerance)
|
python
|
def checkgrad(self, verbose=0, step=1e-6, tolerance=1e-3, df_tolerance=1e-12):
"""
Check the gradient of this parameter with respect to the highest parent's
objective function.
This is a three point estimate of the gradient, wiggling at the parameters
with a stepsize step.
The check passes if either the ratio or the difference between numerical and
analytical gradient is smaller then tolerance.
:param bool verbose: whether each parameter shall be checked individually.
:param float step: the stepsize for the numerical three point gradient estimate.
:param float tolerance: the tolerance for the gradient ratio or difference.
:param float df_tolerance: the tolerance for df_tolerance
.. note::
The *dF_ratio* indicates the limit of accuracy of numerical gradients.
If it is too small, e.g., smaller than 1e-12, the numerical gradients
are usually not accurate enough for the tests (shown with blue).
"""
# Make sure we always call the gradcheck on the highest parent
# This ensures the assumption of the highest parent to hold the fixes
# In the checkgrad function we take advantage of that, so it needs
# to be set in place here.
if self.has_parent():
return self._highest_parent_._checkgrad(self, verbose=verbose, step=step, tolerance=tolerance, df_tolerance=df_tolerance)
return self._checkgrad(self, verbose=verbose, step=step, tolerance=tolerance, df_tolerance=df_tolerance)
|
[
"def",
"checkgrad",
"(",
"self",
",",
"verbose",
"=",
"0",
",",
"step",
"=",
"1e-6",
",",
"tolerance",
"=",
"1e-3",
",",
"df_tolerance",
"=",
"1e-12",
")",
":",
"# Make sure we always call the gradcheck on the highest parent",
"# This ensures the assumption of the highest parent to hold the fixes",
"# In the checkgrad function we take advantage of that, so it needs",
"# to be set in place here.",
"if",
"self",
".",
"has_parent",
"(",
")",
":",
"return",
"self",
".",
"_highest_parent_",
".",
"_checkgrad",
"(",
"self",
",",
"verbose",
"=",
"verbose",
",",
"step",
"=",
"step",
",",
"tolerance",
"=",
"tolerance",
",",
"df_tolerance",
"=",
"df_tolerance",
")",
"return",
"self",
".",
"_checkgrad",
"(",
"self",
",",
"verbose",
"=",
"verbose",
",",
"step",
"=",
"step",
",",
"tolerance",
"=",
"tolerance",
",",
"df_tolerance",
"=",
"df_tolerance",
")"
] |
Check the gradient of this parameter with respect to the highest parent's
objective function.
This is a three point estimate of the gradient, wiggling at the parameters
with a stepsize step.
The check passes if either the ratio or the difference between numerical and
analytical gradient is smaller then tolerance.
:param bool verbose: whether each parameter shall be checked individually.
:param float step: the stepsize for the numerical three point gradient estimate.
:param float tolerance: the tolerance for the gradient ratio or difference.
:param float df_tolerance: the tolerance for df_tolerance
.. note::
The *dF_ratio* indicates the limit of accuracy of numerical gradients.
If it is too small, e.g., smaller than 1e-12, the numerical gradients
are usually not accurate enough for the tests (shown with blue).
|
[
"Check",
"the",
"gradient",
"of",
"this",
"parameter",
"with",
"respect",
"to",
"the",
"highest",
"parent",
"s",
"objective",
"function",
".",
"This",
"is",
"a",
"three",
"point",
"estimate",
"of",
"the",
"gradient",
"wiggling",
"at",
"the",
"parameters",
"with",
"a",
"stepsize",
"step",
".",
"The",
"check",
"passes",
"if",
"either",
"the",
"ratio",
"or",
"the",
"difference",
"between",
"numerical",
"and",
"analytical",
"gradient",
"is",
"smaller",
"then",
"tolerance",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/gradcheckable.py#L44-L69
|
train
|
sods/paramz
|
paramz/optimization/optimization.py
|
opt_tnc.opt
|
def opt(self, x_init, f_fp=None, f=None, fp=None):
"""
Run the TNC optimizer
"""
tnc_rcstrings = ['Local minimum', 'Converged', 'XConverged', 'Maximum number of f evaluations reached',
'Line search failed', 'Function is constant']
assert f_fp != None, "TNC requires f_fp"
opt_dict = {}
if self.xtol is not None:
opt_dict['xtol'] = self.xtol
if self.ftol is not None:
opt_dict['ftol'] = self.ftol
if self.gtol is not None:
opt_dict['pgtol'] = self.gtol
opt_result = optimize.fmin_tnc(f_fp, x_init, messages=self.messages,
maxfun=self.max_f_eval, **opt_dict)
self.x_opt = opt_result[0]
self.f_opt = f_fp(self.x_opt)[0]
self.funct_eval = opt_result[1]
self.status = tnc_rcstrings[opt_result[2]]
|
python
|
def opt(self, x_init, f_fp=None, f=None, fp=None):
"""
Run the TNC optimizer
"""
tnc_rcstrings = ['Local minimum', 'Converged', 'XConverged', 'Maximum number of f evaluations reached',
'Line search failed', 'Function is constant']
assert f_fp != None, "TNC requires f_fp"
opt_dict = {}
if self.xtol is not None:
opt_dict['xtol'] = self.xtol
if self.ftol is not None:
opt_dict['ftol'] = self.ftol
if self.gtol is not None:
opt_dict['pgtol'] = self.gtol
opt_result = optimize.fmin_tnc(f_fp, x_init, messages=self.messages,
maxfun=self.max_f_eval, **opt_dict)
self.x_opt = opt_result[0]
self.f_opt = f_fp(self.x_opt)[0]
self.funct_eval = opt_result[1]
self.status = tnc_rcstrings[opt_result[2]]
|
[
"def",
"opt",
"(",
"self",
",",
"x_init",
",",
"f_fp",
"=",
"None",
",",
"f",
"=",
"None",
",",
"fp",
"=",
"None",
")",
":",
"tnc_rcstrings",
"=",
"[",
"'Local minimum'",
",",
"'Converged'",
",",
"'XConverged'",
",",
"'Maximum number of f evaluations reached'",
",",
"'Line search failed'",
",",
"'Function is constant'",
"]",
"assert",
"f_fp",
"!=",
"None",
",",
"\"TNC requires f_fp\"",
"opt_dict",
"=",
"{",
"}",
"if",
"self",
".",
"xtol",
"is",
"not",
"None",
":",
"opt_dict",
"[",
"'xtol'",
"]",
"=",
"self",
".",
"xtol",
"if",
"self",
".",
"ftol",
"is",
"not",
"None",
":",
"opt_dict",
"[",
"'ftol'",
"]",
"=",
"self",
".",
"ftol",
"if",
"self",
".",
"gtol",
"is",
"not",
"None",
":",
"opt_dict",
"[",
"'pgtol'",
"]",
"=",
"self",
".",
"gtol",
"opt_result",
"=",
"optimize",
".",
"fmin_tnc",
"(",
"f_fp",
",",
"x_init",
",",
"messages",
"=",
"self",
".",
"messages",
",",
"maxfun",
"=",
"self",
".",
"max_f_eval",
",",
"*",
"*",
"opt_dict",
")",
"self",
".",
"x_opt",
"=",
"opt_result",
"[",
"0",
"]",
"self",
".",
"f_opt",
"=",
"f_fp",
"(",
"self",
".",
"x_opt",
")",
"[",
"0",
"]",
"self",
".",
"funct_eval",
"=",
"opt_result",
"[",
"1",
"]",
"self",
".",
"status",
"=",
"tnc_rcstrings",
"[",
"opt_result",
"[",
"2",
"]",
"]"
] |
Run the TNC optimizer
|
[
"Run",
"the",
"TNC",
"optimizer"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/optimization/optimization.py#L75-L98
|
train
|
sods/paramz
|
paramz/optimization/optimization.py
|
opt_simplex.opt
|
def opt(self, x_init, f_fp=None, f=None, fp=None):
"""
The simplex optimizer does not require gradients.
"""
statuses = ['Converged', 'Maximum number of function evaluations made', 'Maximum number of iterations reached']
opt_dict = {}
if self.xtol is not None:
opt_dict['xtol'] = self.xtol
if self.ftol is not None:
opt_dict['ftol'] = self.ftol
if self.gtol is not None:
print("WARNING: simplex doesn't have an gtol arg, so I'm going to ignore it")
opt_result = optimize.fmin(f, x_init, (), disp=self.messages,
maxfun=self.max_f_eval, full_output=True, **opt_dict)
self.x_opt = opt_result[0]
self.f_opt = opt_result[1]
self.funct_eval = opt_result[3]
self.status = statuses[opt_result[4]]
self.trace = None
|
python
|
def opt(self, x_init, f_fp=None, f=None, fp=None):
"""
The simplex optimizer does not require gradients.
"""
statuses = ['Converged', 'Maximum number of function evaluations made', 'Maximum number of iterations reached']
opt_dict = {}
if self.xtol is not None:
opt_dict['xtol'] = self.xtol
if self.ftol is not None:
opt_dict['ftol'] = self.ftol
if self.gtol is not None:
print("WARNING: simplex doesn't have an gtol arg, so I'm going to ignore it")
opt_result = optimize.fmin(f, x_init, (), disp=self.messages,
maxfun=self.max_f_eval, full_output=True, **opt_dict)
self.x_opt = opt_result[0]
self.f_opt = opt_result[1]
self.funct_eval = opt_result[3]
self.status = statuses[opt_result[4]]
self.trace = None
|
[
"def",
"opt",
"(",
"self",
",",
"x_init",
",",
"f_fp",
"=",
"None",
",",
"f",
"=",
"None",
",",
"fp",
"=",
"None",
")",
":",
"statuses",
"=",
"[",
"'Converged'",
",",
"'Maximum number of function evaluations made'",
",",
"'Maximum number of iterations reached'",
"]",
"opt_dict",
"=",
"{",
"}",
"if",
"self",
".",
"xtol",
"is",
"not",
"None",
":",
"opt_dict",
"[",
"'xtol'",
"]",
"=",
"self",
".",
"xtol",
"if",
"self",
".",
"ftol",
"is",
"not",
"None",
":",
"opt_dict",
"[",
"'ftol'",
"]",
"=",
"self",
".",
"ftol",
"if",
"self",
".",
"gtol",
"is",
"not",
"None",
":",
"print",
"(",
"\"WARNING: simplex doesn't have an gtol arg, so I'm going to ignore it\"",
")",
"opt_result",
"=",
"optimize",
".",
"fmin",
"(",
"f",
",",
"x_init",
",",
"(",
")",
",",
"disp",
"=",
"self",
".",
"messages",
",",
"maxfun",
"=",
"self",
".",
"max_f_eval",
",",
"full_output",
"=",
"True",
",",
"*",
"*",
"opt_dict",
")",
"self",
".",
"x_opt",
"=",
"opt_result",
"[",
"0",
"]",
"self",
".",
"f_opt",
"=",
"opt_result",
"[",
"1",
"]",
"self",
".",
"funct_eval",
"=",
"opt_result",
"[",
"3",
"]",
"self",
".",
"status",
"=",
"statuses",
"[",
"opt_result",
"[",
"4",
"]",
"]",
"self",
".",
"trace",
"=",
"None"
] |
The simplex optimizer does not require gradients.
|
[
"The",
"simplex",
"optimizer",
"does",
"not",
"require",
"gradients",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/optimization/optimization.py#L166-L188
|
train
|
sods/paramz
|
paramz/caching.py
|
Cacher.combine_inputs
|
def combine_inputs(self, args, kw, ignore_args):
"Combines the args and kw in a unique way, such that ordering of kwargs does not lead to recompute"
inputs= args + tuple(c[1] for c in sorted(kw.items(), key=lambda x: x[0]))
# REMOVE the ignored arguments from input and PREVENT it from being checked!!!
return [a for i,a in enumerate(inputs) if i not in ignore_args]
|
python
|
def combine_inputs(self, args, kw, ignore_args):
"Combines the args and kw in a unique way, such that ordering of kwargs does not lead to recompute"
inputs= args + tuple(c[1] for c in sorted(kw.items(), key=lambda x: x[0]))
# REMOVE the ignored arguments from input and PREVENT it from being checked!!!
return [a for i,a in enumerate(inputs) if i not in ignore_args]
|
[
"def",
"combine_inputs",
"(",
"self",
",",
"args",
",",
"kw",
",",
"ignore_args",
")",
":",
"inputs",
"=",
"args",
"+",
"tuple",
"(",
"c",
"[",
"1",
"]",
"for",
"c",
"in",
"sorted",
"(",
"kw",
".",
"items",
"(",
")",
",",
"key",
"=",
"lambda",
"x",
":",
"x",
"[",
"0",
"]",
")",
")",
"# REMOVE the ignored arguments from input and PREVENT it from being checked!!!",
"return",
"[",
"a",
"for",
"i",
",",
"a",
"in",
"enumerate",
"(",
"inputs",
")",
"if",
"i",
"not",
"in",
"ignore_args",
"]"
] |
Combines the args and kw in a unique way, such that ordering of kwargs does not lead to recompute
|
[
"Combines",
"the",
"args",
"and",
"kw",
"in",
"a",
"unique",
"way",
"such",
"that",
"ordering",
"of",
"kwargs",
"does",
"not",
"lead",
"to",
"recompute"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/caching.py#L90-L94
|
train
|
sods/paramz
|
paramz/caching.py
|
Cacher.ensure_cache_length
|
def ensure_cache_length(self):
"Ensures the cache is within its limits and has one place free"
if len(self.order) == self.limit:
# we have reached the limit, so lets release one element
cache_id = self.order.popleft()
combined_args_kw = self.cached_inputs[cache_id]
for ind in combined_args_kw:
ind_id = self.id(ind)
tmp = self.cached_input_ids.get(ind_id, None)
if tmp is not None:
ref, cache_ids = tmp
if len(cache_ids) == 1 and ref() is not None:
ref().remove_observer(self, self.on_cache_changed)
del self.cached_input_ids[ind_id]
else:
cache_ids.remove(cache_id)
self.cached_input_ids[ind_id] = [ref, cache_ids]
try:
del self.cached_outputs[cache_id]
except KeyError:
# Was not cached before, possibly a keyboard interrupt
pass
try:
del self.inputs_changed[cache_id]
except KeyError:
# Was not cached before, possibly a keyboard interrupt
pass
try:
del self.cached_inputs[cache_id]
except KeyError:
# Was not cached before, possibly a keyboard interrupt
pass
|
python
|
def ensure_cache_length(self):
"Ensures the cache is within its limits and has one place free"
if len(self.order) == self.limit:
# we have reached the limit, so lets release one element
cache_id = self.order.popleft()
combined_args_kw = self.cached_inputs[cache_id]
for ind in combined_args_kw:
ind_id = self.id(ind)
tmp = self.cached_input_ids.get(ind_id, None)
if tmp is not None:
ref, cache_ids = tmp
if len(cache_ids) == 1 and ref() is not None:
ref().remove_observer(self, self.on_cache_changed)
del self.cached_input_ids[ind_id]
else:
cache_ids.remove(cache_id)
self.cached_input_ids[ind_id] = [ref, cache_ids]
try:
del self.cached_outputs[cache_id]
except KeyError:
# Was not cached before, possibly a keyboard interrupt
pass
try:
del self.inputs_changed[cache_id]
except KeyError:
# Was not cached before, possibly a keyboard interrupt
pass
try:
del self.cached_inputs[cache_id]
except KeyError:
# Was not cached before, possibly a keyboard interrupt
pass
|
[
"def",
"ensure_cache_length",
"(",
"self",
")",
":",
"if",
"len",
"(",
"self",
".",
"order",
")",
"==",
"self",
".",
"limit",
":",
"# we have reached the limit, so lets release one element",
"cache_id",
"=",
"self",
".",
"order",
".",
"popleft",
"(",
")",
"combined_args_kw",
"=",
"self",
".",
"cached_inputs",
"[",
"cache_id",
"]",
"for",
"ind",
"in",
"combined_args_kw",
":",
"ind_id",
"=",
"self",
".",
"id",
"(",
"ind",
")",
"tmp",
"=",
"self",
".",
"cached_input_ids",
".",
"get",
"(",
"ind_id",
",",
"None",
")",
"if",
"tmp",
"is",
"not",
"None",
":",
"ref",
",",
"cache_ids",
"=",
"tmp",
"if",
"len",
"(",
"cache_ids",
")",
"==",
"1",
"and",
"ref",
"(",
")",
"is",
"not",
"None",
":",
"ref",
"(",
")",
".",
"remove_observer",
"(",
"self",
",",
"self",
".",
"on_cache_changed",
")",
"del",
"self",
".",
"cached_input_ids",
"[",
"ind_id",
"]",
"else",
":",
"cache_ids",
".",
"remove",
"(",
"cache_id",
")",
"self",
".",
"cached_input_ids",
"[",
"ind_id",
"]",
"=",
"[",
"ref",
",",
"cache_ids",
"]",
"try",
":",
"del",
"self",
".",
"cached_outputs",
"[",
"cache_id",
"]",
"except",
"KeyError",
":",
"# Was not cached before, possibly a keyboard interrupt",
"pass",
"try",
":",
"del",
"self",
".",
"inputs_changed",
"[",
"cache_id",
"]",
"except",
"KeyError",
":",
"# Was not cached before, possibly a keyboard interrupt",
"pass",
"try",
":",
"del",
"self",
".",
"cached_inputs",
"[",
"cache_id",
"]",
"except",
"KeyError",
":",
"# Was not cached before, possibly a keyboard interrupt",
"pass"
] |
Ensures the cache is within its limits and has one place free
|
[
"Ensures",
"the",
"cache",
"is",
"within",
"its",
"limits",
"and",
"has",
"one",
"place",
"free"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/caching.py#L101-L132
|
train
|
sods/paramz
|
paramz/caching.py
|
Cacher.add_to_cache
|
def add_to_cache(self, cache_id, inputs, output):
"""This adds cache_id to the cache, with inputs and output"""
self.inputs_changed[cache_id] = False
self.cached_outputs[cache_id] = output
self.order.append(cache_id)
self.cached_inputs[cache_id] = inputs
for a in inputs:
if a is not None and not isinstance(a, Number) and not isinstance(a, str):
ind_id = self.id(a)
v = self.cached_input_ids.get(ind_id, [weakref.ref(a), []])
v[1].append(cache_id)
if len(v[1]) == 1:
a.add_observer(self, self.on_cache_changed)
self.cached_input_ids[ind_id] = v
|
python
|
def add_to_cache(self, cache_id, inputs, output):
"""This adds cache_id to the cache, with inputs and output"""
self.inputs_changed[cache_id] = False
self.cached_outputs[cache_id] = output
self.order.append(cache_id)
self.cached_inputs[cache_id] = inputs
for a in inputs:
if a is not None and not isinstance(a, Number) and not isinstance(a, str):
ind_id = self.id(a)
v = self.cached_input_ids.get(ind_id, [weakref.ref(a), []])
v[1].append(cache_id)
if len(v[1]) == 1:
a.add_observer(self, self.on_cache_changed)
self.cached_input_ids[ind_id] = v
|
[
"def",
"add_to_cache",
"(",
"self",
",",
"cache_id",
",",
"inputs",
",",
"output",
")",
":",
"self",
".",
"inputs_changed",
"[",
"cache_id",
"]",
"=",
"False",
"self",
".",
"cached_outputs",
"[",
"cache_id",
"]",
"=",
"output",
"self",
".",
"order",
".",
"append",
"(",
"cache_id",
")",
"self",
".",
"cached_inputs",
"[",
"cache_id",
"]",
"=",
"inputs",
"for",
"a",
"in",
"inputs",
":",
"if",
"a",
"is",
"not",
"None",
"and",
"not",
"isinstance",
"(",
"a",
",",
"Number",
")",
"and",
"not",
"isinstance",
"(",
"a",
",",
"str",
")",
":",
"ind_id",
"=",
"self",
".",
"id",
"(",
"a",
")",
"v",
"=",
"self",
".",
"cached_input_ids",
".",
"get",
"(",
"ind_id",
",",
"[",
"weakref",
".",
"ref",
"(",
"a",
")",
",",
"[",
"]",
"]",
")",
"v",
"[",
"1",
"]",
".",
"append",
"(",
"cache_id",
")",
"if",
"len",
"(",
"v",
"[",
"1",
"]",
")",
"==",
"1",
":",
"a",
".",
"add_observer",
"(",
"self",
",",
"self",
".",
"on_cache_changed",
")",
"self",
".",
"cached_input_ids",
"[",
"ind_id",
"]",
"=",
"v"
] |
This adds cache_id to the cache, with inputs and output
|
[
"This",
"adds",
"cache_id",
"to",
"the",
"cache",
"with",
"inputs",
"and",
"output"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/caching.py#L134-L147
|
train
|
sods/paramz
|
paramz/caching.py
|
Cacher.on_cache_changed
|
def on_cache_changed(self, direct, which=None):
"""
A callback funtion, which sets local flags when the elements of some cached inputs change
this function gets 'hooked up' to the inputs when we cache them, and upon their elements being changed we update here.
"""
for what in [direct, which]:
ind_id = self.id(what)
_, cache_ids = self.cached_input_ids.get(ind_id, [None, []])
for cache_id in cache_ids:
self.inputs_changed[cache_id] = True
|
python
|
def on_cache_changed(self, direct, which=None):
"""
A callback funtion, which sets local flags when the elements of some cached inputs change
this function gets 'hooked up' to the inputs when we cache them, and upon their elements being changed we update here.
"""
for what in [direct, which]:
ind_id = self.id(what)
_, cache_ids = self.cached_input_ids.get(ind_id, [None, []])
for cache_id in cache_ids:
self.inputs_changed[cache_id] = True
|
[
"def",
"on_cache_changed",
"(",
"self",
",",
"direct",
",",
"which",
"=",
"None",
")",
":",
"for",
"what",
"in",
"[",
"direct",
",",
"which",
"]",
":",
"ind_id",
"=",
"self",
".",
"id",
"(",
"what",
")",
"_",
",",
"cache_ids",
"=",
"self",
".",
"cached_input_ids",
".",
"get",
"(",
"ind_id",
",",
"[",
"None",
",",
"[",
"]",
"]",
")",
"for",
"cache_id",
"in",
"cache_ids",
":",
"self",
".",
"inputs_changed",
"[",
"cache_id",
"]",
"=",
"True"
] |
A callback funtion, which sets local flags when the elements of some cached inputs change
this function gets 'hooked up' to the inputs when we cache them, and upon their elements being changed we update here.
|
[
"A",
"callback",
"funtion",
"which",
"sets",
"local",
"flags",
"when",
"the",
"elements",
"of",
"some",
"cached",
"inputs",
"change"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/caching.py#L194-L204
|
train
|
sods/paramz
|
paramz/caching.py
|
Cacher.reset
|
def reset(self):
"""
Totally reset the cache
"""
[a().remove_observer(self, self.on_cache_changed) if (a() is not None) else None for [a, _] in self.cached_input_ids.values()]
self.order = collections.deque()
self.cached_inputs = {} # point from cache_ids to a list of [ind_ids], which where used in cache cache_id
#=======================================================================
# point from each ind_id to [ref(obj), cache_ids]
# 0: a weak reference to the object itself
# 1: the cache_ids in which this ind_id is used (len will be how many times we have seen this ind_id)
self.cached_input_ids = {}
#=======================================================================
self.cached_outputs = {} # point from cache_ids to outputs
self.inputs_changed = {}
|
python
|
def reset(self):
"""
Totally reset the cache
"""
[a().remove_observer(self, self.on_cache_changed) if (a() is not None) else None for [a, _] in self.cached_input_ids.values()]
self.order = collections.deque()
self.cached_inputs = {} # point from cache_ids to a list of [ind_ids], which where used in cache cache_id
#=======================================================================
# point from each ind_id to [ref(obj), cache_ids]
# 0: a weak reference to the object itself
# 1: the cache_ids in which this ind_id is used (len will be how many times we have seen this ind_id)
self.cached_input_ids = {}
#=======================================================================
self.cached_outputs = {} # point from cache_ids to outputs
self.inputs_changed = {}
|
[
"def",
"reset",
"(",
"self",
")",
":",
"[",
"a",
"(",
")",
".",
"remove_observer",
"(",
"self",
",",
"self",
".",
"on_cache_changed",
")",
"if",
"(",
"a",
"(",
")",
"is",
"not",
"None",
")",
"else",
"None",
"for",
"[",
"a",
",",
"_",
"]",
"in",
"self",
".",
"cached_input_ids",
".",
"values",
"(",
")",
"]",
"self",
".",
"order",
"=",
"collections",
".",
"deque",
"(",
")",
"self",
".",
"cached_inputs",
"=",
"{",
"}",
"# point from cache_ids to a list of [ind_ids], which where used in cache cache_id",
"#=======================================================================",
"# point from each ind_id to [ref(obj), cache_ids]",
"# 0: a weak reference to the object itself",
"# 1: the cache_ids in which this ind_id is used (len will be how many times we have seen this ind_id)",
"self",
".",
"cached_input_ids",
"=",
"{",
"}",
"#=======================================================================",
"self",
".",
"cached_outputs",
"=",
"{",
"}",
"# point from cache_ids to outputs",
"self",
".",
"inputs_changed",
"=",
"{",
"}"
] |
Totally reset the cache
|
[
"Totally",
"reset",
"the",
"cache"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/caching.py#L206-L223
|
train
|
sods/paramz
|
paramz/caching.py
|
FunctionCache.disable_caching
|
def disable_caching(self):
"Disable the cache of this object. This also removes previously cached results"
self.caching_enabled = False
for c in self.values():
c.disable_cacher()
|
python
|
def disable_caching(self):
"Disable the cache of this object. This also removes previously cached results"
self.caching_enabled = False
for c in self.values():
c.disable_cacher()
|
[
"def",
"disable_caching",
"(",
"self",
")",
":",
"self",
".",
"caching_enabled",
"=",
"False",
"for",
"c",
"in",
"self",
".",
"values",
"(",
")",
":",
"c",
".",
"disable_cacher",
"(",
")"
] |
Disable the cache of this object. This also removes previously cached results
|
[
"Disable",
"the",
"cache",
"of",
"this",
"object",
".",
"This",
"also",
"removes",
"previously",
"cached",
"results"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/caching.py#L246-L250
|
train
|
sods/paramz
|
paramz/caching.py
|
FunctionCache.enable_caching
|
def enable_caching(self):
"Enable the cache of this object."
self.caching_enabled = True
for c in self.values():
c.enable_cacher()
|
python
|
def enable_caching(self):
"Enable the cache of this object."
self.caching_enabled = True
for c in self.values():
c.enable_cacher()
|
[
"def",
"enable_caching",
"(",
"self",
")",
":",
"self",
".",
"caching_enabled",
"=",
"True",
"for",
"c",
"in",
"self",
".",
"values",
"(",
")",
":",
"c",
".",
"enable_cacher",
"(",
")"
] |
Enable the cache of this object.
|
[
"Enable",
"the",
"cache",
"of",
"this",
"object",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/caching.py#L252-L256
|
train
|
sods/paramz
|
paramz/core/lists_and_dicts.py
|
ObserverList.remove
|
def remove(self, priority, observer, callble):
"""
Remove one observer, which had priority and callble.
"""
self.flush()
for i in range(len(self) - 1, -1, -1):
p,o,c = self[i]
if priority==p and observer==o and callble==c:
del self._poc[i]
|
python
|
def remove(self, priority, observer, callble):
"""
Remove one observer, which had priority and callble.
"""
self.flush()
for i in range(len(self) - 1, -1, -1):
p,o,c = self[i]
if priority==p and observer==o and callble==c:
del self._poc[i]
|
[
"def",
"remove",
"(",
"self",
",",
"priority",
",",
"observer",
",",
"callble",
")",
":",
"self",
".",
"flush",
"(",
")",
"for",
"i",
"in",
"range",
"(",
"len",
"(",
"self",
")",
"-",
"1",
",",
"-",
"1",
",",
"-",
"1",
")",
":",
"p",
",",
"o",
",",
"c",
"=",
"self",
"[",
"i",
"]",
"if",
"priority",
"==",
"p",
"and",
"observer",
"==",
"o",
"and",
"callble",
"==",
"c",
":",
"del",
"self",
".",
"_poc",
"[",
"i",
"]"
] |
Remove one observer, which had priority and callble.
|
[
"Remove",
"one",
"observer",
"which",
"had",
"priority",
"and",
"callble",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/lists_and_dicts.py#L78-L86
|
train
|
sods/paramz
|
paramz/core/lists_and_dicts.py
|
ObserverList.add
|
def add(self, priority, observer, callble):
"""
Add an observer with priority and callble
"""
#if observer is not None:
ins = 0
for pr, _, _ in self:
if priority > pr:
break
ins += 1
self._poc.insert(ins, (priority, weakref.ref(observer), callble))
|
python
|
def add(self, priority, observer, callble):
"""
Add an observer with priority and callble
"""
#if observer is not None:
ins = 0
for pr, _, _ in self:
if priority > pr:
break
ins += 1
self._poc.insert(ins, (priority, weakref.ref(observer), callble))
|
[
"def",
"add",
"(",
"self",
",",
"priority",
",",
"observer",
",",
"callble",
")",
":",
"#if observer is not None:",
"ins",
"=",
"0",
"for",
"pr",
",",
"_",
",",
"_",
"in",
"self",
":",
"if",
"priority",
">",
"pr",
":",
"break",
"ins",
"+=",
"1",
"self",
".",
"_poc",
".",
"insert",
"(",
"ins",
",",
"(",
"priority",
",",
"weakref",
".",
"ref",
"(",
"observer",
")",
",",
"callble",
")",
")"
] |
Add an observer with priority and callble
|
[
"Add",
"an",
"observer",
"with",
"priority",
"and",
"callble"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/lists_and_dicts.py#L91-L101
|
train
|
sods/paramz
|
paramz/core/index_operations.py
|
ParameterIndexOperations.properties_for
|
def properties_for(self, index):
"""
Returns a list of properties, such that each entry in the list corresponds
to the element of the index given.
Example:
let properties: 'one':[1,2,3,4], 'two':[3,5,6]
>>> properties_for([2,3,5])
[['one'], ['one', 'two'], ['two']]
"""
return vectorize(lambda i: [prop for prop in self.properties() if i in self[prop]], otypes=[list])(index)
|
python
|
def properties_for(self, index):
"""
Returns a list of properties, such that each entry in the list corresponds
to the element of the index given.
Example:
let properties: 'one':[1,2,3,4], 'two':[3,5,6]
>>> properties_for([2,3,5])
[['one'], ['one', 'two'], ['two']]
"""
return vectorize(lambda i: [prop for prop in self.properties() if i in self[prop]], otypes=[list])(index)
|
[
"def",
"properties_for",
"(",
"self",
",",
"index",
")",
":",
"return",
"vectorize",
"(",
"lambda",
"i",
":",
"[",
"prop",
"for",
"prop",
"in",
"self",
".",
"properties",
"(",
")",
"if",
"i",
"in",
"self",
"[",
"prop",
"]",
"]",
",",
"otypes",
"=",
"[",
"list",
"]",
")",
"(",
"index",
")"
] |
Returns a list of properties, such that each entry in the list corresponds
to the element of the index given.
Example:
let properties: 'one':[1,2,3,4], 'two':[3,5,6]
>>> properties_for([2,3,5])
[['one'], ['one', 'two'], ['two']]
|
[
"Returns",
"a",
"list",
"of",
"properties",
"such",
"that",
"each",
"entry",
"in",
"the",
"list",
"corresponds",
"to",
"the",
"element",
"of",
"the",
"index",
"given",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/index_operations.py#L132-L143
|
train
|
sods/paramz
|
paramz/core/index_operations.py
|
ParameterIndexOperations.properties_dict_for
|
def properties_dict_for(self, index):
"""
Return a dictionary, containing properties as keys and indices as index
Thus, the indices for each constraint, which is contained will be collected as
one dictionary
Example:
let properties: 'one':[1,2,3,4], 'two':[3,5,6]
>>> properties_dict_for([2,3,5])
{'one':[2,3], 'two':[3,5]}
"""
props = self.properties_for(index)
prop_index = extract_properties_to_index(index, props)
return prop_index
|
python
|
def properties_dict_for(self, index):
"""
Return a dictionary, containing properties as keys and indices as index
Thus, the indices for each constraint, which is contained will be collected as
one dictionary
Example:
let properties: 'one':[1,2,3,4], 'two':[3,5,6]
>>> properties_dict_for([2,3,5])
{'one':[2,3], 'two':[3,5]}
"""
props = self.properties_for(index)
prop_index = extract_properties_to_index(index, props)
return prop_index
|
[
"def",
"properties_dict_for",
"(",
"self",
",",
"index",
")",
":",
"props",
"=",
"self",
".",
"properties_for",
"(",
"index",
")",
"prop_index",
"=",
"extract_properties_to_index",
"(",
"index",
",",
"props",
")",
"return",
"prop_index"
] |
Return a dictionary, containing properties as keys and indices as index
Thus, the indices for each constraint, which is contained will be collected as
one dictionary
Example:
let properties: 'one':[1,2,3,4], 'two':[3,5,6]
>>> properties_dict_for([2,3,5])
{'one':[2,3], 'two':[3,5]}
|
[
"Return",
"a",
"dictionary",
"containing",
"properties",
"as",
"keys",
"and",
"indices",
"as",
"index",
"Thus",
"the",
"indices",
"for",
"each",
"constraint",
"which",
"is",
"contained",
"will",
"be",
"collected",
"as",
"one",
"dictionary"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/index_operations.py#L145-L159
|
train
|
sods/paramz
|
paramz/model.py
|
Model.optimize
|
def optimize(self, optimizer=None, start=None, messages=False, max_iters=1000, ipython_notebook=True, clear_after_finish=False, **kwargs):
"""
Optimize the model using self.log_likelihood and self.log_likelihood_gradient, as well as self.priors.
kwargs are passed to the optimizer. They can be:
:param max_iters: maximum number of function evaluations
:type max_iters: int
:messages: True: Display messages during optimisation, "ipython_notebook":
:type messages: bool"string
:param optimizer: which optimizer to use (defaults to self.preferred optimizer)
:type optimizer: string
Valid optimizers are:
- 'scg': scaled conjugate gradient method, recommended for stability.
See also GPy.inference.optimization.scg
- 'fmin_tnc': truncated Newton method (see scipy.optimize.fmin_tnc)
- 'simplex': the Nelder-Mead simplex method (see scipy.optimize.fmin),
- 'lbfgsb': the l-bfgs-b method (see scipy.optimize.fmin_l_bfgs_b),
- 'lbfgs': the bfgs method (see scipy.optimize.fmin_bfgs),
- 'sgd': stochastic gradient decsent (see scipy.optimize.sgd). For experts only!
"""
if self.is_fixed or self.size == 0:
print('nothing to optimize')
return
if not self.update_model():
print("updates were off, setting updates on again")
self.update_model(True)
if start is None:
start = self.optimizer_array
if optimizer is None:
optimizer = self.preferred_optimizer
if isinstance(optimizer, optimization.Optimizer):
opt = optimizer
opt.model = self
else:
optimizer = optimization.get_optimizer(optimizer)
opt = optimizer(max_iters=max_iters, **kwargs)
with VerboseOptimization(self, opt, maxiters=max_iters, verbose=messages, ipython_notebook=ipython_notebook, clear_after_finish=clear_after_finish) as vo:
opt.run(start, f_fp=self._objective_grads, f=self._objective, fp=self._grads)
self.optimizer_array = opt.x_opt
self.optimization_runs.append(opt)
return opt
|
python
|
def optimize(self, optimizer=None, start=None, messages=False, max_iters=1000, ipython_notebook=True, clear_after_finish=False, **kwargs):
"""
Optimize the model using self.log_likelihood and self.log_likelihood_gradient, as well as self.priors.
kwargs are passed to the optimizer. They can be:
:param max_iters: maximum number of function evaluations
:type max_iters: int
:messages: True: Display messages during optimisation, "ipython_notebook":
:type messages: bool"string
:param optimizer: which optimizer to use (defaults to self.preferred optimizer)
:type optimizer: string
Valid optimizers are:
- 'scg': scaled conjugate gradient method, recommended for stability.
See also GPy.inference.optimization.scg
- 'fmin_tnc': truncated Newton method (see scipy.optimize.fmin_tnc)
- 'simplex': the Nelder-Mead simplex method (see scipy.optimize.fmin),
- 'lbfgsb': the l-bfgs-b method (see scipy.optimize.fmin_l_bfgs_b),
- 'lbfgs': the bfgs method (see scipy.optimize.fmin_bfgs),
- 'sgd': stochastic gradient decsent (see scipy.optimize.sgd). For experts only!
"""
if self.is_fixed or self.size == 0:
print('nothing to optimize')
return
if not self.update_model():
print("updates were off, setting updates on again")
self.update_model(True)
if start is None:
start = self.optimizer_array
if optimizer is None:
optimizer = self.preferred_optimizer
if isinstance(optimizer, optimization.Optimizer):
opt = optimizer
opt.model = self
else:
optimizer = optimization.get_optimizer(optimizer)
opt = optimizer(max_iters=max_iters, **kwargs)
with VerboseOptimization(self, opt, maxiters=max_iters, verbose=messages, ipython_notebook=ipython_notebook, clear_after_finish=clear_after_finish) as vo:
opt.run(start, f_fp=self._objective_grads, f=self._objective, fp=self._grads)
self.optimizer_array = opt.x_opt
self.optimization_runs.append(opt)
return opt
|
[
"def",
"optimize",
"(",
"self",
",",
"optimizer",
"=",
"None",
",",
"start",
"=",
"None",
",",
"messages",
"=",
"False",
",",
"max_iters",
"=",
"1000",
",",
"ipython_notebook",
"=",
"True",
",",
"clear_after_finish",
"=",
"False",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"self",
".",
"is_fixed",
"or",
"self",
".",
"size",
"==",
"0",
":",
"print",
"(",
"'nothing to optimize'",
")",
"return",
"if",
"not",
"self",
".",
"update_model",
"(",
")",
":",
"print",
"(",
"\"updates were off, setting updates on again\"",
")",
"self",
".",
"update_model",
"(",
"True",
")",
"if",
"start",
"is",
"None",
":",
"start",
"=",
"self",
".",
"optimizer_array",
"if",
"optimizer",
"is",
"None",
":",
"optimizer",
"=",
"self",
".",
"preferred_optimizer",
"if",
"isinstance",
"(",
"optimizer",
",",
"optimization",
".",
"Optimizer",
")",
":",
"opt",
"=",
"optimizer",
"opt",
".",
"model",
"=",
"self",
"else",
":",
"optimizer",
"=",
"optimization",
".",
"get_optimizer",
"(",
"optimizer",
")",
"opt",
"=",
"optimizer",
"(",
"max_iters",
"=",
"max_iters",
",",
"*",
"*",
"kwargs",
")",
"with",
"VerboseOptimization",
"(",
"self",
",",
"opt",
",",
"maxiters",
"=",
"max_iters",
",",
"verbose",
"=",
"messages",
",",
"ipython_notebook",
"=",
"ipython_notebook",
",",
"clear_after_finish",
"=",
"clear_after_finish",
")",
"as",
"vo",
":",
"opt",
".",
"run",
"(",
"start",
",",
"f_fp",
"=",
"self",
".",
"_objective_grads",
",",
"f",
"=",
"self",
".",
"_objective",
",",
"fp",
"=",
"self",
".",
"_grads",
")",
"self",
".",
"optimizer_array",
"=",
"opt",
".",
"x_opt",
"self",
".",
"optimization_runs",
".",
"append",
"(",
"opt",
")",
"return",
"opt"
] |
Optimize the model using self.log_likelihood and self.log_likelihood_gradient, as well as self.priors.
kwargs are passed to the optimizer. They can be:
:param max_iters: maximum number of function evaluations
:type max_iters: int
:messages: True: Display messages during optimisation, "ipython_notebook":
:type messages: bool"string
:param optimizer: which optimizer to use (defaults to self.preferred optimizer)
:type optimizer: string
Valid optimizers are:
- 'scg': scaled conjugate gradient method, recommended for stability.
See also GPy.inference.optimization.scg
- 'fmin_tnc': truncated Newton method (see scipy.optimize.fmin_tnc)
- 'simplex': the Nelder-Mead simplex method (see scipy.optimize.fmin),
- 'lbfgsb': the l-bfgs-b method (see scipy.optimize.fmin_l_bfgs_b),
- 'lbfgs': the bfgs method (see scipy.optimize.fmin_bfgs),
- 'sgd': stochastic gradient decsent (see scipy.optimize.sgd). For experts only!
|
[
"Optimize",
"the",
"model",
"using",
"self",
".",
"log_likelihood",
"and",
"self",
".",
"log_likelihood_gradient",
"as",
"well",
"as",
"self",
".",
"priors",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/model.py#L65-L116
|
train
|
sods/paramz
|
paramz/model.py
|
Model.optimize_restarts
|
def optimize_restarts(self, num_restarts=10, robust=False, verbose=True, parallel=False, num_processes=None, **kwargs):
"""
Perform random restarts of the model, and set the model to the best
seen solution.
If the robust flag is set, exceptions raised during optimizations will
be handled silently. If _all_ runs fail, the model is reset to the
existing parameter values.
\*\*kwargs are passed to the optimizer.
:param num_restarts: number of restarts to use (default 10)
:type num_restarts: int
:param robust: whether to handle exceptions silently or not (default False)
:type robust: bool
:param parallel: whether to run each restart as a separate process. It relies on the multiprocessing module.
:type parallel: bool
:param num_processes: number of workers in the multiprocessing pool
:type numprocesses: int
:param max_f_eval: maximum number of function evaluations
:type max_f_eval: int
:param max_iters: maximum number of iterations
:type max_iters: int
:param messages: whether to display during optimisation
:type messages: bool
.. note::
If num_processes is None, the number of workes in the
multiprocessing pool is automatically set to the number of processors
on the current machine.
"""
initial_length = len(self.optimization_runs)
initial_parameters = self.optimizer_array.copy()
if parallel: #pragma: no cover
try:
pool = mp.Pool(processes=num_processes)
obs = [self.copy() for i in range(num_restarts)]
[obs[i].randomize() for i in range(num_restarts-1)]
jobs = pool.map(opt_wrapper, [(o,kwargs) for o in obs])
pool.close()
pool.join()
except KeyboardInterrupt:
print("Ctrl+c received, terminating and joining pool.")
pool.terminate()
pool.join()
for i in range(num_restarts):
try:
if not parallel:
if i > 0:
self.randomize()
self.optimize(**kwargs)
else:#pragma: no cover
self.optimization_runs.append(jobs[i])
if verbose:
print(("Optimization restart {0}/{1}, f = {2}".format(i + 1, num_restarts, self.optimization_runs[-1].f_opt)))
except Exception as e:
if robust:
print(("Warning - optimization restart {0}/{1} failed".format(i + 1, num_restarts)))
else:
raise e
if len(self.optimization_runs) > initial_length:
# This works, since failed jobs don't get added to the optimization_runs.
i = np.argmin([o.f_opt for o in self.optimization_runs[initial_length:]])
self.optimizer_array = self.optimization_runs[initial_length + i].x_opt
else:
self.optimizer_array = initial_parameters
return self.optimization_runs
|
python
|
def optimize_restarts(self, num_restarts=10, robust=False, verbose=True, parallel=False, num_processes=None, **kwargs):
"""
Perform random restarts of the model, and set the model to the best
seen solution.
If the robust flag is set, exceptions raised during optimizations will
be handled silently. If _all_ runs fail, the model is reset to the
existing parameter values.
\*\*kwargs are passed to the optimizer.
:param num_restarts: number of restarts to use (default 10)
:type num_restarts: int
:param robust: whether to handle exceptions silently or not (default False)
:type robust: bool
:param parallel: whether to run each restart as a separate process. It relies on the multiprocessing module.
:type parallel: bool
:param num_processes: number of workers in the multiprocessing pool
:type numprocesses: int
:param max_f_eval: maximum number of function evaluations
:type max_f_eval: int
:param max_iters: maximum number of iterations
:type max_iters: int
:param messages: whether to display during optimisation
:type messages: bool
.. note::
If num_processes is None, the number of workes in the
multiprocessing pool is automatically set to the number of processors
on the current machine.
"""
initial_length = len(self.optimization_runs)
initial_parameters = self.optimizer_array.copy()
if parallel: #pragma: no cover
try:
pool = mp.Pool(processes=num_processes)
obs = [self.copy() for i in range(num_restarts)]
[obs[i].randomize() for i in range(num_restarts-1)]
jobs = pool.map(opt_wrapper, [(o,kwargs) for o in obs])
pool.close()
pool.join()
except KeyboardInterrupt:
print("Ctrl+c received, terminating and joining pool.")
pool.terminate()
pool.join()
for i in range(num_restarts):
try:
if not parallel:
if i > 0:
self.randomize()
self.optimize(**kwargs)
else:#pragma: no cover
self.optimization_runs.append(jobs[i])
if verbose:
print(("Optimization restart {0}/{1}, f = {2}".format(i + 1, num_restarts, self.optimization_runs[-1].f_opt)))
except Exception as e:
if robust:
print(("Warning - optimization restart {0}/{1} failed".format(i + 1, num_restarts)))
else:
raise e
if len(self.optimization_runs) > initial_length:
# This works, since failed jobs don't get added to the optimization_runs.
i = np.argmin([o.f_opt for o in self.optimization_runs[initial_length:]])
self.optimizer_array = self.optimization_runs[initial_length + i].x_opt
else:
self.optimizer_array = initial_parameters
return self.optimization_runs
|
[
"def",
"optimize_restarts",
"(",
"self",
",",
"num_restarts",
"=",
"10",
",",
"robust",
"=",
"False",
",",
"verbose",
"=",
"True",
",",
"parallel",
"=",
"False",
",",
"num_processes",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"initial_length",
"=",
"len",
"(",
"self",
".",
"optimization_runs",
")",
"initial_parameters",
"=",
"self",
".",
"optimizer_array",
".",
"copy",
"(",
")",
"if",
"parallel",
":",
"#pragma: no cover",
"try",
":",
"pool",
"=",
"mp",
".",
"Pool",
"(",
"processes",
"=",
"num_processes",
")",
"obs",
"=",
"[",
"self",
".",
"copy",
"(",
")",
"for",
"i",
"in",
"range",
"(",
"num_restarts",
")",
"]",
"[",
"obs",
"[",
"i",
"]",
".",
"randomize",
"(",
")",
"for",
"i",
"in",
"range",
"(",
"num_restarts",
"-",
"1",
")",
"]",
"jobs",
"=",
"pool",
".",
"map",
"(",
"opt_wrapper",
",",
"[",
"(",
"o",
",",
"kwargs",
")",
"for",
"o",
"in",
"obs",
"]",
")",
"pool",
".",
"close",
"(",
")",
"pool",
".",
"join",
"(",
")",
"except",
"KeyboardInterrupt",
":",
"print",
"(",
"\"Ctrl+c received, terminating and joining pool.\"",
")",
"pool",
".",
"terminate",
"(",
")",
"pool",
".",
"join",
"(",
")",
"for",
"i",
"in",
"range",
"(",
"num_restarts",
")",
":",
"try",
":",
"if",
"not",
"parallel",
":",
"if",
"i",
">",
"0",
":",
"self",
".",
"randomize",
"(",
")",
"self",
".",
"optimize",
"(",
"*",
"*",
"kwargs",
")",
"else",
":",
"#pragma: no cover",
"self",
".",
"optimization_runs",
".",
"append",
"(",
"jobs",
"[",
"i",
"]",
")",
"if",
"verbose",
":",
"print",
"(",
"(",
"\"Optimization restart {0}/{1}, f = {2}\"",
".",
"format",
"(",
"i",
"+",
"1",
",",
"num_restarts",
",",
"self",
".",
"optimization_runs",
"[",
"-",
"1",
"]",
".",
"f_opt",
")",
")",
")",
"except",
"Exception",
"as",
"e",
":",
"if",
"robust",
":",
"print",
"(",
"(",
"\"Warning - optimization restart {0}/{1} failed\"",
".",
"format",
"(",
"i",
"+",
"1",
",",
"num_restarts",
")",
")",
")",
"else",
":",
"raise",
"e",
"if",
"len",
"(",
"self",
".",
"optimization_runs",
")",
">",
"initial_length",
":",
"# This works, since failed jobs don't get added to the optimization_runs.",
"i",
"=",
"np",
".",
"argmin",
"(",
"[",
"o",
".",
"f_opt",
"for",
"o",
"in",
"self",
".",
"optimization_runs",
"[",
"initial_length",
":",
"]",
"]",
")",
"self",
".",
"optimizer_array",
"=",
"self",
".",
"optimization_runs",
"[",
"initial_length",
"+",
"i",
"]",
".",
"x_opt",
"else",
":",
"self",
".",
"optimizer_array",
"=",
"initial_parameters",
"return",
"self",
".",
"optimization_runs"
] |
Perform random restarts of the model, and set the model to the best
seen solution.
If the robust flag is set, exceptions raised during optimizations will
be handled silently. If _all_ runs fail, the model is reset to the
existing parameter values.
\*\*kwargs are passed to the optimizer.
:param num_restarts: number of restarts to use (default 10)
:type num_restarts: int
:param robust: whether to handle exceptions silently or not (default False)
:type robust: bool
:param parallel: whether to run each restart as a separate process. It relies on the multiprocessing module.
:type parallel: bool
:param num_processes: number of workers in the multiprocessing pool
:type numprocesses: int
:param max_f_eval: maximum number of function evaluations
:type max_f_eval: int
:param max_iters: maximum number of iterations
:type max_iters: int
:param messages: whether to display during optimisation
:type messages: bool
.. note::
If num_processes is None, the number of workes in the
multiprocessing pool is automatically set to the number of processors
on the current machine.
|
[
"Perform",
"random",
"restarts",
"of",
"the",
"model",
"and",
"set",
"the",
"model",
"to",
"the",
"best",
"seen",
"solution",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/model.py#L118-L190
|
train
|
sods/paramz
|
paramz/model.py
|
Model._grads
|
def _grads(self, x):
"""
Gets the gradients from the likelihood and the priors.
Failures are handled robustly. The algorithm will try several times to
return the gradients, and will raise the original exception if
the objective cannot be computed.
:param x: the parameters of the model.
:type x: np.array
"""
try:
# self._set_params_transformed(x)
self.optimizer_array = x
self.obj_grads = self._transform_gradients(self.objective_function_gradients())
self._fail_count = 0
except (LinAlgError, ZeroDivisionError, ValueError): #pragma: no cover
if self._fail_count >= self._allowed_failures:
raise
self._fail_count += 1
self.obj_grads = np.clip(self._transform_gradients(self.objective_function_gradients()), -1e100, 1e100)
return self.obj_grads
|
python
|
def _grads(self, x):
"""
Gets the gradients from the likelihood and the priors.
Failures are handled robustly. The algorithm will try several times to
return the gradients, and will raise the original exception if
the objective cannot be computed.
:param x: the parameters of the model.
:type x: np.array
"""
try:
# self._set_params_transformed(x)
self.optimizer_array = x
self.obj_grads = self._transform_gradients(self.objective_function_gradients())
self._fail_count = 0
except (LinAlgError, ZeroDivisionError, ValueError): #pragma: no cover
if self._fail_count >= self._allowed_failures:
raise
self._fail_count += 1
self.obj_grads = np.clip(self._transform_gradients(self.objective_function_gradients()), -1e100, 1e100)
return self.obj_grads
|
[
"def",
"_grads",
"(",
"self",
",",
"x",
")",
":",
"try",
":",
"# self._set_params_transformed(x)",
"self",
".",
"optimizer_array",
"=",
"x",
"self",
".",
"obj_grads",
"=",
"self",
".",
"_transform_gradients",
"(",
"self",
".",
"objective_function_gradients",
"(",
")",
")",
"self",
".",
"_fail_count",
"=",
"0",
"except",
"(",
"LinAlgError",
",",
"ZeroDivisionError",
",",
"ValueError",
")",
":",
"#pragma: no cover",
"if",
"self",
".",
"_fail_count",
">=",
"self",
".",
"_allowed_failures",
":",
"raise",
"self",
".",
"_fail_count",
"+=",
"1",
"self",
".",
"obj_grads",
"=",
"np",
".",
"clip",
"(",
"self",
".",
"_transform_gradients",
"(",
"self",
".",
"objective_function_gradients",
"(",
")",
")",
",",
"-",
"1e100",
",",
"1e100",
")",
"return",
"self",
".",
"obj_grads"
] |
Gets the gradients from the likelihood and the priors.
Failures are handled robustly. The algorithm will try several times to
return the gradients, and will raise the original exception if
the objective cannot be computed.
:param x: the parameters of the model.
:type x: np.array
|
[
"Gets",
"the",
"gradients",
"from",
"the",
"likelihood",
"and",
"the",
"priors",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/model.py#L225-L246
|
train
|
sods/paramz
|
paramz/model.py
|
Model._objective
|
def _objective(self, x):
"""
The objective function passed to the optimizer. It combines
the likelihood and the priors.
Failures are handled robustly. The algorithm will try several times to
return the objective, and will raise the original exception if
the objective cannot be computed.
:param x: the parameters of the model.
:parameter type: np.array
"""
try:
self.optimizer_array = x
obj = self.objective_function()
self._fail_count = 0
except (LinAlgError, ZeroDivisionError, ValueError):#pragma: no cover
if self._fail_count >= self._allowed_failures:
raise
self._fail_count += 1
return np.inf
return obj
|
python
|
def _objective(self, x):
"""
The objective function passed to the optimizer. It combines
the likelihood and the priors.
Failures are handled robustly. The algorithm will try several times to
return the objective, and will raise the original exception if
the objective cannot be computed.
:param x: the parameters of the model.
:parameter type: np.array
"""
try:
self.optimizer_array = x
obj = self.objective_function()
self._fail_count = 0
except (LinAlgError, ZeroDivisionError, ValueError):#pragma: no cover
if self._fail_count >= self._allowed_failures:
raise
self._fail_count += 1
return np.inf
return obj
|
[
"def",
"_objective",
"(",
"self",
",",
"x",
")",
":",
"try",
":",
"self",
".",
"optimizer_array",
"=",
"x",
"obj",
"=",
"self",
".",
"objective_function",
"(",
")",
"self",
".",
"_fail_count",
"=",
"0",
"except",
"(",
"LinAlgError",
",",
"ZeroDivisionError",
",",
"ValueError",
")",
":",
"#pragma: no cover",
"if",
"self",
".",
"_fail_count",
">=",
"self",
".",
"_allowed_failures",
":",
"raise",
"self",
".",
"_fail_count",
"+=",
"1",
"return",
"np",
".",
"inf",
"return",
"obj"
] |
The objective function passed to the optimizer. It combines
the likelihood and the priors.
Failures are handled robustly. The algorithm will try several times to
return the objective, and will raise the original exception if
the objective cannot be computed.
:param x: the parameters of the model.
:parameter type: np.array
|
[
"The",
"objective",
"function",
"passed",
"to",
"the",
"optimizer",
".",
"It",
"combines",
"the",
"likelihood",
"and",
"the",
"priors",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/model.py#L248-L269
|
train
|
sods/paramz
|
paramz/model.py
|
Model._repr_html_
|
def _repr_html_(self):
"""Representation of the model in html for notebook display."""
model_details = [['<b>Model</b>', self.name + '<br>'],
['<b>Objective</b>', '{}<br>'.format(float(self.objective_function()))],
["<b>Number of Parameters</b>", '{}<br>'.format(self.size)],
["<b>Number of Optimization Parameters</b>", '{}<br>'.format(self._size_transformed())],
["<b>Updates</b>", '{}<br>'.format(self._update_on)],
]
from operator import itemgetter
to_print = ["""<style type="text/css">
.pd{
font-family: "Courier New", Courier, monospace !important;
width: 100%;
padding: 3px;
}
</style>\n"""] + ["<p class=pd>"] + ["{}: {}".format(name, detail) for name, detail in model_details] + ["</p>"]
to_print.append(super(Model, self)._repr_html_())
return "\n".join(to_print)
|
python
|
def _repr_html_(self):
"""Representation of the model in html for notebook display."""
model_details = [['<b>Model</b>', self.name + '<br>'],
['<b>Objective</b>', '{}<br>'.format(float(self.objective_function()))],
["<b>Number of Parameters</b>", '{}<br>'.format(self.size)],
["<b>Number of Optimization Parameters</b>", '{}<br>'.format(self._size_transformed())],
["<b>Updates</b>", '{}<br>'.format(self._update_on)],
]
from operator import itemgetter
to_print = ["""<style type="text/css">
.pd{
font-family: "Courier New", Courier, monospace !important;
width: 100%;
padding: 3px;
}
</style>\n"""] + ["<p class=pd>"] + ["{}: {}".format(name, detail) for name, detail in model_details] + ["</p>"]
to_print.append(super(Model, self)._repr_html_())
return "\n".join(to_print)
|
[
"def",
"_repr_html_",
"(",
"self",
")",
":",
"model_details",
"=",
"[",
"[",
"'<b>Model</b>'",
",",
"self",
".",
"name",
"+",
"'<br>'",
"]",
",",
"[",
"'<b>Objective</b>'",
",",
"'{}<br>'",
".",
"format",
"(",
"float",
"(",
"self",
".",
"objective_function",
"(",
")",
")",
")",
"]",
",",
"[",
"\"<b>Number of Parameters</b>\"",
",",
"'{}<br>'",
".",
"format",
"(",
"self",
".",
"size",
")",
"]",
",",
"[",
"\"<b>Number of Optimization Parameters</b>\"",
",",
"'{}<br>'",
".",
"format",
"(",
"self",
".",
"_size_transformed",
"(",
")",
")",
"]",
",",
"[",
"\"<b>Updates</b>\"",
",",
"'{}<br>'",
".",
"format",
"(",
"self",
".",
"_update_on",
")",
"]",
",",
"]",
"from",
"operator",
"import",
"itemgetter",
"to_print",
"=",
"[",
"\"\"\"<style type=\"text/css\">\n.pd{\n font-family: \"Courier New\", Courier, monospace !important;\n width: 100%;\n padding: 3px;\n}\n</style>\\n\"\"\"",
"]",
"+",
"[",
"\"<p class=pd>\"",
"]",
"+",
"[",
"\"{}: {}\"",
".",
"format",
"(",
"name",
",",
"detail",
")",
"for",
"name",
",",
"detail",
"in",
"model_details",
"]",
"+",
"[",
"\"</p>\"",
"]",
"to_print",
".",
"append",
"(",
"super",
"(",
"Model",
",",
"self",
")",
".",
"_repr_html_",
"(",
")",
")",
"return",
"\"\\n\"",
".",
"join",
"(",
"to_print",
")"
] |
Representation of the model in html for notebook display.
|
[
"Representation",
"of",
"the",
"model",
"in",
"html",
"for",
"notebook",
"display",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/model.py#L410-L427
|
train
|
sods/paramz
|
paramz/core/indexable.py
|
Indexable.add_index_operation
|
def add_index_operation(self, name, operations):
"""
Add index operation with name to the operations given.
raises: attribute error if operations exist.
"""
if name not in self._index_operations:
self._add_io(name, operations)
else:
raise AttributeError("An index operation with the name {} was already taken".format(name))
|
python
|
def add_index_operation(self, name, operations):
"""
Add index operation with name to the operations given.
raises: attribute error if operations exist.
"""
if name not in self._index_operations:
self._add_io(name, operations)
else:
raise AttributeError("An index operation with the name {} was already taken".format(name))
|
[
"def",
"add_index_operation",
"(",
"self",
",",
"name",
",",
"operations",
")",
":",
"if",
"name",
"not",
"in",
"self",
".",
"_index_operations",
":",
"self",
".",
"_add_io",
"(",
"name",
",",
"operations",
")",
"else",
":",
"raise",
"AttributeError",
"(",
"\"An index operation with the name {} was already taken\"",
".",
"format",
"(",
"name",
")",
")"
] |
Add index operation with name to the operations given.
raises: attribute error if operations exist.
|
[
"Add",
"index",
"operation",
"with",
"name",
"to",
"the",
"operations",
"given",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/indexable.py#L81-L90
|
train
|
sods/paramz
|
paramz/core/indexable.py
|
Indexable._offset_for
|
def _offset_for(self, param):
"""
Return the offset of the param inside this parameterized object.
This does not need to account for shaped parameters, as it
basically just sums up the parameter sizes which come before param.
"""
if param.has_parent():
p = param._parent_._get_original(param)
if p in self.parameters:
return reduce(lambda a,b: a + b.size, self.parameters[:p._parent_index_], 0)
return self._offset_for(param._parent_) + param._parent_._offset_for(param)
return 0
|
python
|
def _offset_for(self, param):
"""
Return the offset of the param inside this parameterized object.
This does not need to account for shaped parameters, as it
basically just sums up the parameter sizes which come before param.
"""
if param.has_parent():
p = param._parent_._get_original(param)
if p in self.parameters:
return reduce(lambda a,b: a + b.size, self.parameters[:p._parent_index_], 0)
return self._offset_for(param._parent_) + param._parent_._offset_for(param)
return 0
|
[
"def",
"_offset_for",
"(",
"self",
",",
"param",
")",
":",
"if",
"param",
".",
"has_parent",
"(",
")",
":",
"p",
"=",
"param",
".",
"_parent_",
".",
"_get_original",
"(",
"param",
")",
"if",
"p",
"in",
"self",
".",
"parameters",
":",
"return",
"reduce",
"(",
"lambda",
"a",
",",
"b",
":",
"a",
"+",
"b",
".",
"size",
",",
"self",
".",
"parameters",
"[",
":",
"p",
".",
"_parent_index_",
"]",
",",
"0",
")",
"return",
"self",
".",
"_offset_for",
"(",
"param",
".",
"_parent_",
")",
"+",
"param",
".",
"_parent_",
".",
"_offset_for",
"(",
"param",
")",
"return",
"0"
] |
Return the offset of the param inside this parameterized object.
This does not need to account for shaped parameters, as it
basically just sums up the parameter sizes which come before param.
|
[
"Return",
"the",
"offset",
"of",
"the",
"param",
"inside",
"this",
"parameterized",
"object",
".",
"This",
"does",
"not",
"need",
"to",
"account",
"for",
"shaped",
"parameters",
"as",
"it",
"basically",
"just",
"sums",
"up",
"the",
"parameter",
"sizes",
"which",
"come",
"before",
"param",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/indexable.py#L130-L141
|
train
|
sods/paramz
|
paramz/core/indexable.py
|
Indexable._raveled_index_for
|
def _raveled_index_for(self, param):
"""
get the raveled index for a param
that is an int array, containing the indexes for the flattened
param inside this parameterized logic.
!Warning! be sure to call this method on the highest parent of a hierarchy,
as it uses the fixes to do its work
"""
from ..param import ParamConcatenation
if isinstance(param, ParamConcatenation):
return np.hstack((self._raveled_index_for(p) for p in param.params))
return param._raveled_index() + self._offset_for(param)
|
python
|
def _raveled_index_for(self, param):
"""
get the raveled index for a param
that is an int array, containing the indexes for the flattened
param inside this parameterized logic.
!Warning! be sure to call this method on the highest parent of a hierarchy,
as it uses the fixes to do its work
"""
from ..param import ParamConcatenation
if isinstance(param, ParamConcatenation):
return np.hstack((self._raveled_index_for(p) for p in param.params))
return param._raveled_index() + self._offset_for(param)
|
[
"def",
"_raveled_index_for",
"(",
"self",
",",
"param",
")",
":",
"from",
".",
".",
"param",
"import",
"ParamConcatenation",
"if",
"isinstance",
"(",
"param",
",",
"ParamConcatenation",
")",
":",
"return",
"np",
".",
"hstack",
"(",
"(",
"self",
".",
"_raveled_index_for",
"(",
"p",
")",
"for",
"p",
"in",
"param",
".",
"params",
")",
")",
"return",
"param",
".",
"_raveled_index",
"(",
")",
"+",
"self",
".",
"_offset_for",
"(",
"param",
")"
] |
get the raveled index for a param
that is an int array, containing the indexes for the flattened
param inside this parameterized logic.
!Warning! be sure to call this method on the highest parent of a hierarchy,
as it uses the fixes to do its work
|
[
"get",
"the",
"raveled",
"index",
"for",
"a",
"param",
"that",
"is",
"an",
"int",
"array",
"containing",
"the",
"indexes",
"for",
"the",
"flattened",
"param",
"inside",
"this",
"parameterized",
"logic",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/indexable.py#L150-L162
|
train
|
sods/paramz
|
paramz/core/observable_array.py
|
ObsAr.copy
|
def copy(self):
"""
Make a copy. This means, we delete all observers and return a copy of this
array. It will still be an ObsAr!
"""
from .lists_and_dicts import ObserverList
memo = {}
memo[id(self)] = self
memo[id(self.observers)] = ObserverList()
return self.__deepcopy__(memo)
|
python
|
def copy(self):
"""
Make a copy. This means, we delete all observers and return a copy of this
array. It will still be an ObsAr!
"""
from .lists_and_dicts import ObserverList
memo = {}
memo[id(self)] = self
memo[id(self.observers)] = ObserverList()
return self.__deepcopy__(memo)
|
[
"def",
"copy",
"(",
"self",
")",
":",
"from",
".",
"lists_and_dicts",
"import",
"ObserverList",
"memo",
"=",
"{",
"}",
"memo",
"[",
"id",
"(",
"self",
")",
"]",
"=",
"self",
"memo",
"[",
"id",
"(",
"self",
".",
"observers",
")",
"]",
"=",
"ObserverList",
"(",
")",
"return",
"self",
".",
"__deepcopy__",
"(",
"memo",
")"
] |
Make a copy. This means, we delete all observers and return a copy of this
array. It will still be an ObsAr!
|
[
"Make",
"a",
"copy",
".",
"This",
"means",
"we",
"delete",
"all",
"observers",
"and",
"return",
"a",
"copy",
"of",
"this",
"array",
".",
"It",
"will",
"still",
"be",
"an",
"ObsAr!"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/observable_array.py#L91-L100
|
train
|
sods/paramz
|
paramz/core/updateable.py
|
Updateable.update_model
|
def update_model(self, updates=None):
"""
Get or set, whether automatic updates are performed. When updates are
off, the model might be in a non-working state. To make the model work
turn updates on again.
:param bool|None updates:
bool: whether to do updates
None: get the current update state
"""
if updates is None:
return self._update_on
assert isinstance(updates, bool), "updates are either on (True) or off (False)"
p = getattr(self, '_highest_parent_', None)
def turn_updates(s):
s._update_on = updates
p.traverse(turn_updates)
self.trigger_update()
|
python
|
def update_model(self, updates=None):
"""
Get or set, whether automatic updates are performed. When updates are
off, the model might be in a non-working state. To make the model work
turn updates on again.
:param bool|None updates:
bool: whether to do updates
None: get the current update state
"""
if updates is None:
return self._update_on
assert isinstance(updates, bool), "updates are either on (True) or off (False)"
p = getattr(self, '_highest_parent_', None)
def turn_updates(s):
s._update_on = updates
p.traverse(turn_updates)
self.trigger_update()
|
[
"def",
"update_model",
"(",
"self",
",",
"updates",
"=",
"None",
")",
":",
"if",
"updates",
"is",
"None",
":",
"return",
"self",
".",
"_update_on",
"assert",
"isinstance",
"(",
"updates",
",",
"bool",
")",
",",
"\"updates are either on (True) or off (False)\"",
"p",
"=",
"getattr",
"(",
"self",
",",
"'_highest_parent_'",
",",
"None",
")",
"def",
"turn_updates",
"(",
"s",
")",
":",
"s",
".",
"_update_on",
"=",
"updates",
"p",
".",
"traverse",
"(",
"turn_updates",
")",
"self",
".",
"trigger_update",
"(",
")"
] |
Get or set, whether automatic updates are performed. When updates are
off, the model might be in a non-working state. To make the model work
turn updates on again.
:param bool|None updates:
bool: whether to do updates
None: get the current update state
|
[
"Get",
"or",
"set",
"whether",
"automatic",
"updates",
"are",
"performed",
".",
"When",
"updates",
"are",
"off",
"the",
"model",
"might",
"be",
"in",
"a",
"non",
"-",
"working",
"state",
".",
"To",
"make",
"the",
"model",
"work",
"turn",
"updates",
"on",
"again",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/updateable.py#L42-L60
|
train
|
sods/paramz
|
paramz/core/updateable.py
|
Updateable.trigger_update
|
def trigger_update(self, trigger_parent=True):
"""
Update the model from the current state.
Make sure that updates are on, otherwise this
method will do nothing
:param bool trigger_parent: Whether to trigger the parent, after self has updated
"""
if not self.update_model() or (hasattr(self, "_in_init_") and self._in_init_):
#print "Warning: updates are off, updating the model will do nothing"
return
self._trigger_params_changed(trigger_parent)
|
python
|
def trigger_update(self, trigger_parent=True):
"""
Update the model from the current state.
Make sure that updates are on, otherwise this
method will do nothing
:param bool trigger_parent: Whether to trigger the parent, after self has updated
"""
if not self.update_model() or (hasattr(self, "_in_init_") and self._in_init_):
#print "Warning: updates are off, updating the model will do nothing"
return
self._trigger_params_changed(trigger_parent)
|
[
"def",
"trigger_update",
"(",
"self",
",",
"trigger_parent",
"=",
"True",
")",
":",
"if",
"not",
"self",
".",
"update_model",
"(",
")",
"or",
"(",
"hasattr",
"(",
"self",
",",
"\"_in_init_\"",
")",
"and",
"self",
".",
"_in_init_",
")",
":",
"#print \"Warning: updates are off, updating the model will do nothing\"",
"return",
"self",
".",
"_trigger_params_changed",
"(",
"trigger_parent",
")"
] |
Update the model from the current state.
Make sure that updates are on, otherwise this
method will do nothing
:param bool trigger_parent: Whether to trigger the parent, after self has updated
|
[
"Update",
"the",
"model",
"from",
"the",
"current",
"state",
".",
"Make",
"sure",
"that",
"updates",
"are",
"on",
"otherwise",
"this",
"method",
"will",
"do",
"nothing"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/updateable.py#L68-L79
|
train
|
sods/paramz
|
paramz/core/parameter_core.py
|
OptimizationHandlable.optimizer_array
|
def optimizer_array(self):
"""
Array for the optimizer to work on.
This array always lives in the space for the optimizer.
Thus, it is untransformed, going from Transformations.
Setting this array, will make sure the transformed parameters for this model
will be set accordingly. It has to be set with an array, retrieved from
this method, as e.g. fixing will resize the array.
The optimizer should only interfere with this array, such that transformations
are secured.
"""
if self.__dict__.get('_optimizer_copy_', None) is None or self.size != self._optimizer_copy_.size:
self._optimizer_copy_ = np.empty(self.size)
if not self._optimizer_copy_transformed:
self._optimizer_copy_.flat = self.param_array.flat
#py3 fix
#[np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.iteritems() if c != __fixed__]
[np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.items() if c != __fixed__]
self._optimizer_copy_transformed = True
if self._has_fixes():# or self._has_ties()):
self._ensure_fixes()
return self._optimizer_copy_[self._fixes_]
return self._optimizer_copy_
|
python
|
def optimizer_array(self):
"""
Array for the optimizer to work on.
This array always lives in the space for the optimizer.
Thus, it is untransformed, going from Transformations.
Setting this array, will make sure the transformed parameters for this model
will be set accordingly. It has to be set with an array, retrieved from
this method, as e.g. fixing will resize the array.
The optimizer should only interfere with this array, such that transformations
are secured.
"""
if self.__dict__.get('_optimizer_copy_', None) is None or self.size != self._optimizer_copy_.size:
self._optimizer_copy_ = np.empty(self.size)
if not self._optimizer_copy_transformed:
self._optimizer_copy_.flat = self.param_array.flat
#py3 fix
#[np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.iteritems() if c != __fixed__]
[np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.items() if c != __fixed__]
self._optimizer_copy_transformed = True
if self._has_fixes():# or self._has_ties()):
self._ensure_fixes()
return self._optimizer_copy_[self._fixes_]
return self._optimizer_copy_
|
[
"def",
"optimizer_array",
"(",
"self",
")",
":",
"if",
"self",
".",
"__dict__",
".",
"get",
"(",
"'_optimizer_copy_'",
",",
"None",
")",
"is",
"None",
"or",
"self",
".",
"size",
"!=",
"self",
".",
"_optimizer_copy_",
".",
"size",
":",
"self",
".",
"_optimizer_copy_",
"=",
"np",
".",
"empty",
"(",
"self",
".",
"size",
")",
"if",
"not",
"self",
".",
"_optimizer_copy_transformed",
":",
"self",
".",
"_optimizer_copy_",
".",
"flat",
"=",
"self",
".",
"param_array",
".",
"flat",
"#py3 fix",
"#[np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.iteritems() if c != __fixed__]",
"[",
"np",
".",
"put",
"(",
"self",
".",
"_optimizer_copy_",
",",
"ind",
",",
"c",
".",
"finv",
"(",
"self",
".",
"param_array",
"[",
"ind",
"]",
")",
")",
"for",
"c",
",",
"ind",
"in",
"self",
".",
"constraints",
".",
"items",
"(",
")",
"if",
"c",
"!=",
"__fixed__",
"]",
"self",
".",
"_optimizer_copy_transformed",
"=",
"True",
"if",
"self",
".",
"_has_fixes",
"(",
")",
":",
"# or self._has_ties()):",
"self",
".",
"_ensure_fixes",
"(",
")",
"return",
"self",
".",
"_optimizer_copy_",
"[",
"self",
".",
"_fixes_",
"]",
"return",
"self",
".",
"_optimizer_copy_"
] |
Array for the optimizer to work on.
This array always lives in the space for the optimizer.
Thus, it is untransformed, going from Transformations.
Setting this array, will make sure the transformed parameters for this model
will be set accordingly. It has to be set with an array, retrieved from
this method, as e.g. fixing will resize the array.
The optimizer should only interfere with this array, such that transformations
are secured.
|
[
"Array",
"for",
"the",
"optimizer",
"to",
"work",
"on",
".",
"This",
"array",
"always",
"lives",
"in",
"the",
"space",
"for",
"the",
"optimizer",
".",
"Thus",
"it",
"is",
"untransformed",
"going",
"from",
"Transformations",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/parameter_core.py#L67-L93
|
train
|
sods/paramz
|
paramz/core/parameter_core.py
|
OptimizationHandlable._trigger_params_changed
|
def _trigger_params_changed(self, trigger_parent=True):
"""
First tell all children to update,
then update yourself.
If trigger_parent is True, we will tell the parent, otherwise not.
"""
[p._trigger_params_changed(trigger_parent=False) for p in self.parameters if not p.is_fixed]
self.notify_observers(None, None if trigger_parent else -np.inf)
|
python
|
def _trigger_params_changed(self, trigger_parent=True):
"""
First tell all children to update,
then update yourself.
If trigger_parent is True, we will tell the parent, otherwise not.
"""
[p._trigger_params_changed(trigger_parent=False) for p in self.parameters if not p.is_fixed]
self.notify_observers(None, None if trigger_parent else -np.inf)
|
[
"def",
"_trigger_params_changed",
"(",
"self",
",",
"trigger_parent",
"=",
"True",
")",
":",
"[",
"p",
".",
"_trigger_params_changed",
"(",
"trigger_parent",
"=",
"False",
")",
"for",
"p",
"in",
"self",
".",
"parameters",
"if",
"not",
"p",
".",
"is_fixed",
"]",
"self",
".",
"notify_observers",
"(",
"None",
",",
"None",
"if",
"trigger_parent",
"else",
"-",
"np",
".",
"inf",
")"
] |
First tell all children to update,
then update yourself.
If trigger_parent is True, we will tell the parent, otherwise not.
|
[
"First",
"tell",
"all",
"children",
"to",
"update",
"then",
"update",
"yourself",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/parameter_core.py#L126-L134
|
train
|
sods/paramz
|
paramz/core/parameter_core.py
|
OptimizationHandlable._transform_gradients
|
def _transform_gradients(self, g):
"""
Transform the gradients by multiplying the gradient factor for each
constraint to it.
"""
#py3 fix
#[np.put(g, i, c.gradfactor(self.param_array[i], g[i])) for c, i in self.constraints.iteritems() if c != __fixed__]
[np.put(g, i, c.gradfactor(self.param_array[i], g[i])) for c, i in self.constraints.items() if c != __fixed__]
if self._has_fixes(): return g[self._fixes_]
return g
|
python
|
def _transform_gradients(self, g):
"""
Transform the gradients by multiplying the gradient factor for each
constraint to it.
"""
#py3 fix
#[np.put(g, i, c.gradfactor(self.param_array[i], g[i])) for c, i in self.constraints.iteritems() if c != __fixed__]
[np.put(g, i, c.gradfactor(self.param_array[i], g[i])) for c, i in self.constraints.items() if c != __fixed__]
if self._has_fixes(): return g[self._fixes_]
return g
|
[
"def",
"_transform_gradients",
"(",
"self",
",",
"g",
")",
":",
"#py3 fix",
"#[np.put(g, i, c.gradfactor(self.param_array[i], g[i])) for c, i in self.constraints.iteritems() if c != __fixed__]",
"[",
"np",
".",
"put",
"(",
"g",
",",
"i",
",",
"c",
".",
"gradfactor",
"(",
"self",
".",
"param_array",
"[",
"i",
"]",
",",
"g",
"[",
"i",
"]",
")",
")",
"for",
"c",
",",
"i",
"in",
"self",
".",
"constraints",
".",
"items",
"(",
")",
"if",
"c",
"!=",
"__fixed__",
"]",
"if",
"self",
".",
"_has_fixes",
"(",
")",
":",
"return",
"g",
"[",
"self",
".",
"_fixes_",
"]",
"return",
"g"
] |
Transform the gradients by multiplying the gradient factor for each
constraint to it.
|
[
"Transform",
"the",
"gradients",
"by",
"multiplying",
"the",
"gradient",
"factor",
"for",
"each",
"constraint",
"to",
"it",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/parameter_core.py#L143-L152
|
train
|
sods/paramz
|
paramz/core/parameter_core.py
|
OptimizationHandlable.parameter_names
|
def parameter_names(self, add_self=False, adjust_for_printing=False, recursive=True, intermediate=False):
"""
Get the names of all parameters of this model or parameter. It starts
from the parameterized object you are calling this method on.
Note: This does not unravel multidimensional parameters,
use parameter_names_flat to unravel parameters!
:param bool add_self: whether to add the own name in front of names
:param bool adjust_for_printing: whether to call `adjust_name_for_printing` on names
:param bool recursive: whether to traverse through hierarchy and append leaf node names
:param bool intermediate: whether to add intermediate names, that is parameterized objects
"""
if adjust_for_printing: adjust = adjust_name_for_printing
else: adjust = lambda x: x
names = []
if intermediate or (not recursive):
names.extend([adjust(x.name) for x in self.parameters])
if intermediate or recursive: names.extend([
xi for x in self.parameters for xi in
x.parameter_names(add_self=True,
adjust_for_printing=adjust_for_printing,
recursive=True,
intermediate=False)])
if add_self: names = map(lambda x: adjust(self.name) + "." + x, names)
return names
|
python
|
def parameter_names(self, add_self=False, adjust_for_printing=False, recursive=True, intermediate=False):
"""
Get the names of all parameters of this model or parameter. It starts
from the parameterized object you are calling this method on.
Note: This does not unravel multidimensional parameters,
use parameter_names_flat to unravel parameters!
:param bool add_self: whether to add the own name in front of names
:param bool adjust_for_printing: whether to call `adjust_name_for_printing` on names
:param bool recursive: whether to traverse through hierarchy and append leaf node names
:param bool intermediate: whether to add intermediate names, that is parameterized objects
"""
if adjust_for_printing: adjust = adjust_name_for_printing
else: adjust = lambda x: x
names = []
if intermediate or (not recursive):
names.extend([adjust(x.name) for x in self.parameters])
if intermediate or recursive: names.extend([
xi for x in self.parameters for xi in
x.parameter_names(add_self=True,
adjust_for_printing=adjust_for_printing,
recursive=True,
intermediate=False)])
if add_self: names = map(lambda x: adjust(self.name) + "." + x, names)
return names
|
[
"def",
"parameter_names",
"(",
"self",
",",
"add_self",
"=",
"False",
",",
"adjust_for_printing",
"=",
"False",
",",
"recursive",
"=",
"True",
",",
"intermediate",
"=",
"False",
")",
":",
"if",
"adjust_for_printing",
":",
"adjust",
"=",
"adjust_name_for_printing",
"else",
":",
"adjust",
"=",
"lambda",
"x",
":",
"x",
"names",
"=",
"[",
"]",
"if",
"intermediate",
"or",
"(",
"not",
"recursive",
")",
":",
"names",
".",
"extend",
"(",
"[",
"adjust",
"(",
"x",
".",
"name",
")",
"for",
"x",
"in",
"self",
".",
"parameters",
"]",
")",
"if",
"intermediate",
"or",
"recursive",
":",
"names",
".",
"extend",
"(",
"[",
"xi",
"for",
"x",
"in",
"self",
".",
"parameters",
"for",
"xi",
"in",
"x",
".",
"parameter_names",
"(",
"add_self",
"=",
"True",
",",
"adjust_for_printing",
"=",
"adjust_for_printing",
",",
"recursive",
"=",
"True",
",",
"intermediate",
"=",
"False",
")",
"]",
")",
"if",
"add_self",
":",
"names",
"=",
"map",
"(",
"lambda",
"x",
":",
"adjust",
"(",
"self",
".",
"name",
")",
"+",
"\".\"",
"+",
"x",
",",
"names",
")",
"return",
"names"
] |
Get the names of all parameters of this model or parameter. It starts
from the parameterized object you are calling this method on.
Note: This does not unravel multidimensional parameters,
use parameter_names_flat to unravel parameters!
:param bool add_self: whether to add the own name in front of names
:param bool adjust_for_printing: whether to call `adjust_name_for_printing` on names
:param bool recursive: whether to traverse through hierarchy and append leaf node names
:param bool intermediate: whether to add intermediate names, that is parameterized objects
|
[
"Get",
"the",
"names",
"of",
"all",
"parameters",
"of",
"this",
"model",
"or",
"parameter",
".",
"It",
"starts",
"from",
"the",
"parameterized",
"object",
"you",
"are",
"calling",
"this",
"method",
"on",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/parameter_core.py#L174-L199
|
train
|
sods/paramz
|
paramz/core/parameter_core.py
|
OptimizationHandlable.parameter_names_flat
|
def parameter_names_flat(self, include_fixed=False):
"""
Return the flattened parameter names for all subsequent parameters
of this parameter. We do not include the name for self here!
If you want the names for fixed parameters as well in this list,
set include_fixed to True.
if not hasattr(obj, 'cache'):
obj.cache = FunctionCacher()
:param bool include_fixed: whether to include fixed names here.
"""
name_list = []
for p in self.flattened_parameters:
name = p.hierarchy_name()
if p.size > 1:
name_list.extend(["{}[{!s}]".format(name, i) for i in p._indices()])
else:
name_list.append(name)
name_list = np.array(name_list)
if not include_fixed and self._has_fixes():
return name_list[self._fixes_]
return name_list
|
python
|
def parameter_names_flat(self, include_fixed=False):
"""
Return the flattened parameter names for all subsequent parameters
of this parameter. We do not include the name for self here!
If you want the names for fixed parameters as well in this list,
set include_fixed to True.
if not hasattr(obj, 'cache'):
obj.cache = FunctionCacher()
:param bool include_fixed: whether to include fixed names here.
"""
name_list = []
for p in self.flattened_parameters:
name = p.hierarchy_name()
if p.size > 1:
name_list.extend(["{}[{!s}]".format(name, i) for i in p._indices()])
else:
name_list.append(name)
name_list = np.array(name_list)
if not include_fixed and self._has_fixes():
return name_list[self._fixes_]
return name_list
|
[
"def",
"parameter_names_flat",
"(",
"self",
",",
"include_fixed",
"=",
"False",
")",
":",
"name_list",
"=",
"[",
"]",
"for",
"p",
"in",
"self",
".",
"flattened_parameters",
":",
"name",
"=",
"p",
".",
"hierarchy_name",
"(",
")",
"if",
"p",
".",
"size",
">",
"1",
":",
"name_list",
".",
"extend",
"(",
"[",
"\"{}[{!s}]\"",
".",
"format",
"(",
"name",
",",
"i",
")",
"for",
"i",
"in",
"p",
".",
"_indices",
"(",
")",
"]",
")",
"else",
":",
"name_list",
".",
"append",
"(",
"name",
")",
"name_list",
"=",
"np",
".",
"array",
"(",
"name_list",
")",
"if",
"not",
"include_fixed",
"and",
"self",
".",
"_has_fixes",
"(",
")",
":",
"return",
"name_list",
"[",
"self",
".",
"_fixes_",
"]",
"return",
"name_list"
] |
Return the flattened parameter names for all subsequent parameters
of this parameter. We do not include the name for self here!
If you want the names for fixed parameters as well in this list,
set include_fixed to True.
if not hasattr(obj, 'cache'):
obj.cache = FunctionCacher()
:param bool include_fixed: whether to include fixed names here.
|
[
"Return",
"the",
"flattened",
"parameter",
"names",
"for",
"all",
"subsequent",
"parameters",
"of",
"this",
"parameter",
".",
"We",
"do",
"not",
"include",
"the",
"name",
"for",
"self",
"here!"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/parameter_core.py#L201-L223
|
train
|
sods/paramz
|
paramz/core/parameter_core.py
|
OptimizationHandlable._propagate_param_grad
|
def _propagate_param_grad(self, parray, garray):
"""
For propagating the param_array and gradient_array.
This ensures the in memory view of each subsequent array.
1.) connect param_array of children to self.param_array
2.) tell all children to propagate further
"""
#if self.param_array.size != self.size:
# self._param_array_ = np.empty(self.size, dtype=np.float64)
#if self.gradient.size != self.size:
# self._gradient_array_ = np.empty(self.size, dtype=np.float64)
pi_old_size = 0
for pi in self.parameters:
pislice = slice(pi_old_size, pi_old_size + pi.size)
self.param_array[pislice] = pi.param_array.flat # , requirements=['C', 'W']).flat
self.gradient_full[pislice] = pi.gradient_full.flat # , requirements=['C', 'W']).flat
pi.param_array.data = parray[pislice].data
pi.gradient_full.data = garray[pislice].data
pi._propagate_param_grad(parray[pislice], garray[pislice])
pi_old_size += pi.size
self._model_initialized_ = True
|
python
|
def _propagate_param_grad(self, parray, garray):
"""
For propagating the param_array and gradient_array.
This ensures the in memory view of each subsequent array.
1.) connect param_array of children to self.param_array
2.) tell all children to propagate further
"""
#if self.param_array.size != self.size:
# self._param_array_ = np.empty(self.size, dtype=np.float64)
#if self.gradient.size != self.size:
# self._gradient_array_ = np.empty(self.size, dtype=np.float64)
pi_old_size = 0
for pi in self.parameters:
pislice = slice(pi_old_size, pi_old_size + pi.size)
self.param_array[pislice] = pi.param_array.flat # , requirements=['C', 'W']).flat
self.gradient_full[pislice] = pi.gradient_full.flat # , requirements=['C', 'W']).flat
pi.param_array.data = parray[pislice].data
pi.gradient_full.data = garray[pislice].data
pi._propagate_param_grad(parray[pislice], garray[pislice])
pi_old_size += pi.size
self._model_initialized_ = True
|
[
"def",
"_propagate_param_grad",
"(",
"self",
",",
"parray",
",",
"garray",
")",
":",
"#if self.param_array.size != self.size:",
"# self._param_array_ = np.empty(self.size, dtype=np.float64)",
"#if self.gradient.size != self.size:",
"# self._gradient_array_ = np.empty(self.size, dtype=np.float64)",
"pi_old_size",
"=",
"0",
"for",
"pi",
"in",
"self",
".",
"parameters",
":",
"pislice",
"=",
"slice",
"(",
"pi_old_size",
",",
"pi_old_size",
"+",
"pi",
".",
"size",
")",
"self",
".",
"param_array",
"[",
"pislice",
"]",
"=",
"pi",
".",
"param_array",
".",
"flat",
"# , requirements=['C', 'W']).flat",
"self",
".",
"gradient_full",
"[",
"pislice",
"]",
"=",
"pi",
".",
"gradient_full",
".",
"flat",
"# , requirements=['C', 'W']).flat",
"pi",
".",
"param_array",
".",
"data",
"=",
"parray",
"[",
"pislice",
"]",
".",
"data",
"pi",
".",
"gradient_full",
".",
"data",
"=",
"garray",
"[",
"pislice",
"]",
".",
"data",
"pi",
".",
"_propagate_param_grad",
"(",
"parray",
"[",
"pislice",
"]",
",",
"garray",
"[",
"pislice",
"]",
")",
"pi_old_size",
"+=",
"pi",
".",
"size",
"self",
".",
"_model_initialized_",
"=",
"True"
] |
For propagating the param_array and gradient_array.
This ensures the in memory view of each subsequent array.
1.) connect param_array of children to self.param_array
2.) tell all children to propagate further
|
[
"For",
"propagating",
"the",
"param_array",
"and",
"gradient_array",
".",
"This",
"ensures",
"the",
"in",
"memory",
"view",
"of",
"each",
"subsequent",
"array",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/parameter_core.py#L270-L296
|
train
|
sods/paramz
|
paramz/core/parameter_core.py
|
Parameterizable.initialize_parameter
|
def initialize_parameter(self):
"""
Call this function to initialize the model, if you built it without initialization.
This HAS to be called manually before optmizing or it will be causing
unexpected behaviour, if not errors!
"""
#logger.debug("connecting parameters")
self._highest_parent_._notify_parent_change()
self._highest_parent_._connect_parameters() #logger.debug("calling parameters changed")
self._highest_parent_._connect_fixes()
self.trigger_update()
|
python
|
def initialize_parameter(self):
"""
Call this function to initialize the model, if you built it without initialization.
This HAS to be called manually before optmizing or it will be causing
unexpected behaviour, if not errors!
"""
#logger.debug("connecting parameters")
self._highest_parent_._notify_parent_change()
self._highest_parent_._connect_parameters() #logger.debug("calling parameters changed")
self._highest_parent_._connect_fixes()
self.trigger_update()
|
[
"def",
"initialize_parameter",
"(",
"self",
")",
":",
"#logger.debug(\"connecting parameters\")",
"self",
".",
"_highest_parent_",
".",
"_notify_parent_change",
"(",
")",
"self",
".",
"_highest_parent_",
".",
"_connect_parameters",
"(",
")",
"#logger.debug(\"calling parameters changed\")",
"self",
".",
"_highest_parent_",
".",
"_connect_fixes",
"(",
")",
"self",
".",
"trigger_update",
"(",
")"
] |
Call this function to initialize the model, if you built it without initialization.
This HAS to be called manually before optmizing or it will be causing
unexpected behaviour, if not errors!
|
[
"Call",
"this",
"function",
"to",
"initialize",
"the",
"model",
"if",
"you",
"built",
"it",
"without",
"initialization",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/parameter_core.py#L326-L337
|
train
|
sods/paramz
|
paramz/core/parameter_core.py
|
Parameterizable.traverse_parents
|
def traverse_parents(self, visit, *args, **kwargs):
"""
Traverse the hierarchy upwards, visiting all parents and their children except self.
See "visitor pattern" in literature. This is implemented in pre-order fashion.
Example:
parents = []
self.traverse_parents(parents.append)
print parents
"""
if self.has_parent():
self.__visited = True
self._parent_.traverse_parents(visit, *args, **kwargs)
self._parent_.traverse(visit, *args, **kwargs)
self.__visited = False
|
python
|
def traverse_parents(self, visit, *args, **kwargs):
"""
Traverse the hierarchy upwards, visiting all parents and their children except self.
See "visitor pattern" in literature. This is implemented in pre-order fashion.
Example:
parents = []
self.traverse_parents(parents.append)
print parents
"""
if self.has_parent():
self.__visited = True
self._parent_.traverse_parents(visit, *args, **kwargs)
self._parent_.traverse(visit, *args, **kwargs)
self.__visited = False
|
[
"def",
"traverse_parents",
"(",
"self",
",",
"visit",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"self",
".",
"has_parent",
"(",
")",
":",
"self",
".",
"__visited",
"=",
"True",
"self",
".",
"_parent_",
".",
"traverse_parents",
"(",
"visit",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"self",
".",
"_parent_",
".",
"traverse",
"(",
"visit",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"self",
".",
"__visited",
"=",
"False"
] |
Traverse the hierarchy upwards, visiting all parents and their children except self.
See "visitor pattern" in literature. This is implemented in pre-order fashion.
Example:
parents = []
self.traverse_parents(parents.append)
print parents
|
[
"Traverse",
"the",
"hierarchy",
"upwards",
"visiting",
"all",
"parents",
"and",
"their",
"children",
"except",
"self",
".",
"See",
"visitor",
"pattern",
"in",
"literature",
".",
"This",
"is",
"implemented",
"in",
"pre",
"-",
"order",
"fashion",
"."
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/core/parameter_core.py#L395-L410
|
train
|
sods/paramz
|
paramz/examples/ridge_regression.py
|
RidgeRegression.phi
|
def phi(self, Xpred, degrees=None):
"""
Compute the design matrix for this model
using the degrees given by the index array
in degrees
:param array-like Xpred: inputs to compute the design matrix for
:param array-like degrees: array of degrees to use [default=range(self.degree+1)]
:returns array-like phi: The design matrix [degree x #samples x #dimensions]
"""
assert Xpred.shape[1] == self.X.shape[1], "Need to predict with same shape as training data."
if degrees is None:
degrees = range(self.basis.degree+1)
tmp_phi = np.empty((len(degrees), Xpred.shape[0], Xpred.shape[1]))
for i, w in enumerate(degrees):
# Objective function
tmpX = self._phi(Xpred, w)
tmp_phi[i] = tmpX * self.weights[[w], :]
return tmp_phi
|
python
|
def phi(self, Xpred, degrees=None):
"""
Compute the design matrix for this model
using the degrees given by the index array
in degrees
:param array-like Xpred: inputs to compute the design matrix for
:param array-like degrees: array of degrees to use [default=range(self.degree+1)]
:returns array-like phi: The design matrix [degree x #samples x #dimensions]
"""
assert Xpred.shape[1] == self.X.shape[1], "Need to predict with same shape as training data."
if degrees is None:
degrees = range(self.basis.degree+1)
tmp_phi = np.empty((len(degrees), Xpred.shape[0], Xpred.shape[1]))
for i, w in enumerate(degrees):
# Objective function
tmpX = self._phi(Xpred, w)
tmp_phi[i] = tmpX * self.weights[[w], :]
return tmp_phi
|
[
"def",
"phi",
"(",
"self",
",",
"Xpred",
",",
"degrees",
"=",
"None",
")",
":",
"assert",
"Xpred",
".",
"shape",
"[",
"1",
"]",
"==",
"self",
".",
"X",
".",
"shape",
"[",
"1",
"]",
",",
"\"Need to predict with same shape as training data.\"",
"if",
"degrees",
"is",
"None",
":",
"degrees",
"=",
"range",
"(",
"self",
".",
"basis",
".",
"degree",
"+",
"1",
")",
"tmp_phi",
"=",
"np",
".",
"empty",
"(",
"(",
"len",
"(",
"degrees",
")",
",",
"Xpred",
".",
"shape",
"[",
"0",
"]",
",",
"Xpred",
".",
"shape",
"[",
"1",
"]",
")",
")",
"for",
"i",
",",
"w",
"in",
"enumerate",
"(",
"degrees",
")",
":",
"# Objective function",
"tmpX",
"=",
"self",
".",
"_phi",
"(",
"Xpred",
",",
"w",
")",
"tmp_phi",
"[",
"i",
"]",
"=",
"tmpX",
"*",
"self",
".",
"weights",
"[",
"[",
"w",
"]",
",",
":",
"]",
"return",
"tmp_phi"
] |
Compute the design matrix for this model
using the degrees given by the index array
in degrees
:param array-like Xpred: inputs to compute the design matrix for
:param array-like degrees: array of degrees to use [default=range(self.degree+1)]
:returns array-like phi: The design matrix [degree x #samples x #dimensions]
|
[
"Compute",
"the",
"design",
"matrix",
"for",
"this",
"model",
"using",
"the",
"degrees",
"given",
"by",
"the",
"index",
"array",
"in",
"degrees"
] |
ae6fc6274b70fb723d91e48fc5026a9bc5a06508
|
https://github.com/sods/paramz/blob/ae6fc6274b70fb723d91e48fc5026a9bc5a06508/paramz/examples/ridge_regression.py#L57-L75
|
train
|
PyAr/fades
|
fades/main.py
|
consolidate_dependencies
|
def consolidate_dependencies(needs_ipython, child_program,
requirement_files, manual_dependencies):
"""Parse files, get deps and merge them. Deps read later overwrite those read earlier."""
# We get the logger here because it's not defined at module level
logger = logging.getLogger('fades')
if needs_ipython:
logger.debug("Adding ipython dependency because --ipython was detected")
ipython_dep = parsing.parse_manual(['ipython'])
else:
ipython_dep = {}
if child_program:
srcfile_deps = parsing.parse_srcfile(child_program)
logger.debug("Dependencies from source file: %s", srcfile_deps)
docstring_deps = parsing.parse_docstring(child_program)
logger.debug("Dependencies from docstrings: %s", docstring_deps)
else:
srcfile_deps = {}
docstring_deps = {}
all_dependencies = [ipython_dep, srcfile_deps, docstring_deps]
if requirement_files is not None:
for rf_path in requirement_files:
rf_deps = parsing.parse_reqfile(rf_path)
logger.debug('Dependencies from requirements file %r: %s', rf_path, rf_deps)
all_dependencies.append(rf_deps)
manual_deps = parsing.parse_manual(manual_dependencies)
logger.debug("Dependencies from parameters: %s", manual_deps)
all_dependencies.append(manual_deps)
# Merge dependencies
indicated_deps = {}
for dep in all_dependencies:
for repo, info in dep.items():
indicated_deps.setdefault(repo, set()).update(info)
return indicated_deps
|
python
|
def consolidate_dependencies(needs_ipython, child_program,
requirement_files, manual_dependencies):
"""Parse files, get deps and merge them. Deps read later overwrite those read earlier."""
# We get the logger here because it's not defined at module level
logger = logging.getLogger('fades')
if needs_ipython:
logger.debug("Adding ipython dependency because --ipython was detected")
ipython_dep = parsing.parse_manual(['ipython'])
else:
ipython_dep = {}
if child_program:
srcfile_deps = parsing.parse_srcfile(child_program)
logger.debug("Dependencies from source file: %s", srcfile_deps)
docstring_deps = parsing.parse_docstring(child_program)
logger.debug("Dependencies from docstrings: %s", docstring_deps)
else:
srcfile_deps = {}
docstring_deps = {}
all_dependencies = [ipython_dep, srcfile_deps, docstring_deps]
if requirement_files is not None:
for rf_path in requirement_files:
rf_deps = parsing.parse_reqfile(rf_path)
logger.debug('Dependencies from requirements file %r: %s', rf_path, rf_deps)
all_dependencies.append(rf_deps)
manual_deps = parsing.parse_manual(manual_dependencies)
logger.debug("Dependencies from parameters: %s", manual_deps)
all_dependencies.append(manual_deps)
# Merge dependencies
indicated_deps = {}
for dep in all_dependencies:
for repo, info in dep.items():
indicated_deps.setdefault(repo, set()).update(info)
return indicated_deps
|
[
"def",
"consolidate_dependencies",
"(",
"needs_ipython",
",",
"child_program",
",",
"requirement_files",
",",
"manual_dependencies",
")",
":",
"# We get the logger here because it's not defined at module level",
"logger",
"=",
"logging",
".",
"getLogger",
"(",
"'fades'",
")",
"if",
"needs_ipython",
":",
"logger",
".",
"debug",
"(",
"\"Adding ipython dependency because --ipython was detected\"",
")",
"ipython_dep",
"=",
"parsing",
".",
"parse_manual",
"(",
"[",
"'ipython'",
"]",
")",
"else",
":",
"ipython_dep",
"=",
"{",
"}",
"if",
"child_program",
":",
"srcfile_deps",
"=",
"parsing",
".",
"parse_srcfile",
"(",
"child_program",
")",
"logger",
".",
"debug",
"(",
"\"Dependencies from source file: %s\"",
",",
"srcfile_deps",
")",
"docstring_deps",
"=",
"parsing",
".",
"parse_docstring",
"(",
"child_program",
")",
"logger",
".",
"debug",
"(",
"\"Dependencies from docstrings: %s\"",
",",
"docstring_deps",
")",
"else",
":",
"srcfile_deps",
"=",
"{",
"}",
"docstring_deps",
"=",
"{",
"}",
"all_dependencies",
"=",
"[",
"ipython_dep",
",",
"srcfile_deps",
",",
"docstring_deps",
"]",
"if",
"requirement_files",
"is",
"not",
"None",
":",
"for",
"rf_path",
"in",
"requirement_files",
":",
"rf_deps",
"=",
"parsing",
".",
"parse_reqfile",
"(",
"rf_path",
")",
"logger",
".",
"debug",
"(",
"'Dependencies from requirements file %r: %s'",
",",
"rf_path",
",",
"rf_deps",
")",
"all_dependencies",
".",
"append",
"(",
"rf_deps",
")",
"manual_deps",
"=",
"parsing",
".",
"parse_manual",
"(",
"manual_dependencies",
")",
"logger",
".",
"debug",
"(",
"\"Dependencies from parameters: %s\"",
",",
"manual_deps",
")",
"all_dependencies",
".",
"append",
"(",
"manual_deps",
")",
"# Merge dependencies",
"indicated_deps",
"=",
"{",
"}",
"for",
"dep",
"in",
"all_dependencies",
":",
"for",
"repo",
",",
"info",
"in",
"dep",
".",
"items",
"(",
")",
":",
"indicated_deps",
".",
"setdefault",
"(",
"repo",
",",
"set",
"(",
")",
")",
".",
"update",
"(",
"info",
")",
"return",
"indicated_deps"
] |
Parse files, get deps and merge them. Deps read later overwrite those read earlier.
|
[
"Parse",
"files",
"get",
"deps",
"and",
"merge",
"them",
".",
"Deps",
"read",
"later",
"overwrite",
"those",
"read",
"earlier",
"."
] |
e5ea457b09b105f321d4f81772f25e8695159604
|
https://github.com/PyAr/fades/blob/e5ea457b09b105f321d4f81772f25e8695159604/fades/main.py#L56-L95
|
train
|
PyAr/fades
|
fades/main.py
|
detect_inside_virtualenv
|
def detect_inside_virtualenv(prefix, real_prefix, base_prefix):
"""Tell if fades is running inside a virtualenv.
The params 'real_prefix' and 'base_prefix' may be None.
This is copied from pip code (slightly modified), see
https://github.com/pypa/pip/blob/281eb61b09d87765d7c2b92f6982b3fe76ccb0af/
pip/locations.py#L39
"""
if real_prefix is not None:
return True
if base_prefix is None:
return False
# if prefix is different than base_prefix, it's a venv
return prefix != base_prefix
|
python
|
def detect_inside_virtualenv(prefix, real_prefix, base_prefix):
"""Tell if fades is running inside a virtualenv.
The params 'real_prefix' and 'base_prefix' may be None.
This is copied from pip code (slightly modified), see
https://github.com/pypa/pip/blob/281eb61b09d87765d7c2b92f6982b3fe76ccb0af/
pip/locations.py#L39
"""
if real_prefix is not None:
return True
if base_prefix is None:
return False
# if prefix is different than base_prefix, it's a venv
return prefix != base_prefix
|
[
"def",
"detect_inside_virtualenv",
"(",
"prefix",
",",
"real_prefix",
",",
"base_prefix",
")",
":",
"if",
"real_prefix",
"is",
"not",
"None",
":",
"return",
"True",
"if",
"base_prefix",
"is",
"None",
":",
"return",
"False",
"# if prefix is different than base_prefix, it's a venv",
"return",
"prefix",
"!=",
"base_prefix"
] |
Tell if fades is running inside a virtualenv.
The params 'real_prefix' and 'base_prefix' may be None.
This is copied from pip code (slightly modified), see
https://github.com/pypa/pip/blob/281eb61b09d87765d7c2b92f6982b3fe76ccb0af/
pip/locations.py#L39
|
[
"Tell",
"if",
"fades",
"is",
"running",
"inside",
"a",
"virtualenv",
"."
] |
e5ea457b09b105f321d4f81772f25e8695159604
|
https://github.com/PyAr/fades/blob/e5ea457b09b105f321d4f81772f25e8695159604/fades/main.py#L137-L154
|
train
|
PyAr/fades
|
fades/main.py
|
_get_normalized_args
|
def _get_normalized_args(parser):
"""Return the parsed command line arguments.
Support the case when executed from a shebang, where all the
parameters come in sys.argv[1] in a single string separated
by spaces (in this case, the third parameter is what is being
executed)
"""
env = os.environ
if '_' in env and env['_'] != sys.argv[0] and len(sys.argv) >= 1 and " " in sys.argv[1]:
return parser.parse_args(shlex.split(sys.argv[1]) + sys.argv[2:])
else:
return parser.parse_args()
|
python
|
def _get_normalized_args(parser):
"""Return the parsed command line arguments.
Support the case when executed from a shebang, where all the
parameters come in sys.argv[1] in a single string separated
by spaces (in this case, the third parameter is what is being
executed)
"""
env = os.environ
if '_' in env and env['_'] != sys.argv[0] and len(sys.argv) >= 1 and " " in sys.argv[1]:
return parser.parse_args(shlex.split(sys.argv[1]) + sys.argv[2:])
else:
return parser.parse_args()
|
[
"def",
"_get_normalized_args",
"(",
"parser",
")",
":",
"env",
"=",
"os",
".",
"environ",
"if",
"'_'",
"in",
"env",
"and",
"env",
"[",
"'_'",
"]",
"!=",
"sys",
".",
"argv",
"[",
"0",
"]",
"and",
"len",
"(",
"sys",
".",
"argv",
")",
">=",
"1",
"and",
"\" \"",
"in",
"sys",
".",
"argv",
"[",
"1",
"]",
":",
"return",
"parser",
".",
"parse_args",
"(",
"shlex",
".",
"split",
"(",
"sys",
".",
"argv",
"[",
"1",
"]",
")",
"+",
"sys",
".",
"argv",
"[",
"2",
":",
"]",
")",
"else",
":",
"return",
"parser",
".",
"parse_args",
"(",
")"
] |
Return the parsed command line arguments.
Support the case when executed from a shebang, where all the
parameters come in sys.argv[1] in a single string separated
by spaces (in this case, the third parameter is what is being
executed)
|
[
"Return",
"the",
"parsed",
"command",
"line",
"arguments",
"."
] |
e5ea457b09b105f321d4f81772f25e8695159604
|
https://github.com/PyAr/fades/blob/e5ea457b09b105f321d4f81772f25e8695159604/fades/main.py#L157-L169
|
train
|
PyAr/fades
|
fades/parsing.py
|
parse_fade_requirement
|
def parse_fade_requirement(text):
"""Return a requirement and repo from the given text, already parsed and converted."""
text = text.strip()
if "::" in text:
repo_raw, requirement = text.split("::", 1)
try:
repo = {'pypi': REPO_PYPI, 'vcs': REPO_VCS}[repo_raw]
except KeyError:
logger.warning("Not understood fades repository: %r", repo_raw)
return
else:
if ":" in text and "/" in text:
repo = REPO_VCS
else:
repo = REPO_PYPI
requirement = text
if repo == REPO_VCS:
dependency = VCSDependency(requirement)
else:
dependency = list(parse_requirements(requirement))[0]
return repo, dependency
|
python
|
def parse_fade_requirement(text):
"""Return a requirement and repo from the given text, already parsed and converted."""
text = text.strip()
if "::" in text:
repo_raw, requirement = text.split("::", 1)
try:
repo = {'pypi': REPO_PYPI, 'vcs': REPO_VCS}[repo_raw]
except KeyError:
logger.warning("Not understood fades repository: %r", repo_raw)
return
else:
if ":" in text and "/" in text:
repo = REPO_VCS
else:
repo = REPO_PYPI
requirement = text
if repo == REPO_VCS:
dependency = VCSDependency(requirement)
else:
dependency = list(parse_requirements(requirement))[0]
return repo, dependency
|
[
"def",
"parse_fade_requirement",
"(",
"text",
")",
":",
"text",
"=",
"text",
".",
"strip",
"(",
")",
"if",
"\"::\"",
"in",
"text",
":",
"repo_raw",
",",
"requirement",
"=",
"text",
".",
"split",
"(",
"\"::\"",
",",
"1",
")",
"try",
":",
"repo",
"=",
"{",
"'pypi'",
":",
"REPO_PYPI",
",",
"'vcs'",
":",
"REPO_VCS",
"}",
"[",
"repo_raw",
"]",
"except",
"KeyError",
":",
"logger",
".",
"warning",
"(",
"\"Not understood fades repository: %r\"",
",",
"repo_raw",
")",
"return",
"else",
":",
"if",
"\":\"",
"in",
"text",
"and",
"\"/\"",
"in",
"text",
":",
"repo",
"=",
"REPO_VCS",
"else",
":",
"repo",
"=",
"REPO_PYPI",
"requirement",
"=",
"text",
"if",
"repo",
"==",
"REPO_VCS",
":",
"dependency",
"=",
"VCSDependency",
"(",
"requirement",
")",
"else",
":",
"dependency",
"=",
"list",
"(",
"parse_requirements",
"(",
"requirement",
")",
")",
"[",
"0",
"]",
"return",
"repo",
",",
"dependency"
] |
Return a requirement and repo from the given text, already parsed and converted.
|
[
"Return",
"a",
"requirement",
"and",
"repo",
"from",
"the",
"given",
"text",
"already",
"parsed",
"and",
"converted",
"."
] |
e5ea457b09b105f321d4f81772f25e8695159604
|
https://github.com/PyAr/fades/blob/e5ea457b09b105f321d4f81772f25e8695159604/fades/parsing.py#L67-L89
|
train
|
PyAr/fades
|
fades/parsing.py
|
_parse_content
|
def _parse_content(fh):
"""Parse the content of a script to find marked dependencies."""
content = iter(fh)
deps = {}
for line in content:
# quickly discard most of the lines
if 'fades' not in line:
continue
# discard other string with 'fades' that isn't a comment
if '#' not in line:
continue
# assure that it's a well commented line and no other stuff
line = line.strip()
index_of_last_fades = line.rfind('fades')
index_of_first_hash = line.index('#')
# discard when fades does not appear after #
if index_of_first_hash > index_of_last_fades:
continue
import_part, fades_part = line.rsplit("#", 1)
# discard other comments in the same line that aren't for fades
if "fades" not in fades_part:
import_part, fades_part = import_part.rsplit("#", 1)
fades_part = fades_part.strip()
if not fades_part.startswith("fades"):
continue
if not import_part:
# the fades comment was done at the beginning of the line,
# which means that the import info is in the next one
import_part = next(content).strip()
if import_part.startswith('#'):
continue
# get module
import_tokens = import_part.split()
if import_tokens[0] == 'import':
module_path = import_tokens[1]
elif import_tokens[0] == 'from' and import_tokens[2] == 'import':
module_path = import_tokens[1]
else:
logger.debug("Not understood import info: %s", import_tokens)
continue
module = module_path.split(".")[0]
# If fades know the real name of the pkg. Replace it!
if module in PKG_NAMES_DB:
module = PKG_NAMES_DB[module]
# To match the "safe" name that pkg_resources creates:
module = module.replace('_', '-')
# get the fades info after 'fades' mark, if any
if len(fades_part) == 5 or fades_part[5:].strip()[0] in "<>=!":
# just the 'fades' mark, and maybe a version specification, the requirement is what
# was imported (maybe with that version comparison)
requirement = module + fades_part[5:]
elif fades_part[5] != " ":
# starts with fades but it's part of a longer weird word
logger.warning("Not understood fades info: %r", fades_part)
continue
else:
# more complex stuff, to be parsed as a normal requirement
requirement = fades_part[5:]
# parse and convert the requirement
parsed_req = parse_fade_requirement(requirement)
if parsed_req is None:
continue
repo, dependency = parsed_req
deps.setdefault(repo, []).append(dependency)
return deps
|
python
|
def _parse_content(fh):
"""Parse the content of a script to find marked dependencies."""
content = iter(fh)
deps = {}
for line in content:
# quickly discard most of the lines
if 'fades' not in line:
continue
# discard other string with 'fades' that isn't a comment
if '#' not in line:
continue
# assure that it's a well commented line and no other stuff
line = line.strip()
index_of_last_fades = line.rfind('fades')
index_of_first_hash = line.index('#')
# discard when fades does not appear after #
if index_of_first_hash > index_of_last_fades:
continue
import_part, fades_part = line.rsplit("#", 1)
# discard other comments in the same line that aren't for fades
if "fades" not in fades_part:
import_part, fades_part = import_part.rsplit("#", 1)
fades_part = fades_part.strip()
if not fades_part.startswith("fades"):
continue
if not import_part:
# the fades comment was done at the beginning of the line,
# which means that the import info is in the next one
import_part = next(content).strip()
if import_part.startswith('#'):
continue
# get module
import_tokens = import_part.split()
if import_tokens[0] == 'import':
module_path = import_tokens[1]
elif import_tokens[0] == 'from' and import_tokens[2] == 'import':
module_path = import_tokens[1]
else:
logger.debug("Not understood import info: %s", import_tokens)
continue
module = module_path.split(".")[0]
# If fades know the real name of the pkg. Replace it!
if module in PKG_NAMES_DB:
module = PKG_NAMES_DB[module]
# To match the "safe" name that pkg_resources creates:
module = module.replace('_', '-')
# get the fades info after 'fades' mark, if any
if len(fades_part) == 5 or fades_part[5:].strip()[0] in "<>=!":
# just the 'fades' mark, and maybe a version specification, the requirement is what
# was imported (maybe with that version comparison)
requirement = module + fades_part[5:]
elif fades_part[5] != " ":
# starts with fades but it's part of a longer weird word
logger.warning("Not understood fades info: %r", fades_part)
continue
else:
# more complex stuff, to be parsed as a normal requirement
requirement = fades_part[5:]
# parse and convert the requirement
parsed_req = parse_fade_requirement(requirement)
if parsed_req is None:
continue
repo, dependency = parsed_req
deps.setdefault(repo, []).append(dependency)
return deps
|
[
"def",
"_parse_content",
"(",
"fh",
")",
":",
"content",
"=",
"iter",
"(",
"fh",
")",
"deps",
"=",
"{",
"}",
"for",
"line",
"in",
"content",
":",
"# quickly discard most of the lines",
"if",
"'fades'",
"not",
"in",
"line",
":",
"continue",
"# discard other string with 'fades' that isn't a comment",
"if",
"'#'",
"not",
"in",
"line",
":",
"continue",
"# assure that it's a well commented line and no other stuff",
"line",
"=",
"line",
".",
"strip",
"(",
")",
"index_of_last_fades",
"=",
"line",
".",
"rfind",
"(",
"'fades'",
")",
"index_of_first_hash",
"=",
"line",
".",
"index",
"(",
"'#'",
")",
"# discard when fades does not appear after #",
"if",
"index_of_first_hash",
">",
"index_of_last_fades",
":",
"continue",
"import_part",
",",
"fades_part",
"=",
"line",
".",
"rsplit",
"(",
"\"#\"",
",",
"1",
")",
"# discard other comments in the same line that aren't for fades",
"if",
"\"fades\"",
"not",
"in",
"fades_part",
":",
"import_part",
",",
"fades_part",
"=",
"import_part",
".",
"rsplit",
"(",
"\"#\"",
",",
"1",
")",
"fades_part",
"=",
"fades_part",
".",
"strip",
"(",
")",
"if",
"not",
"fades_part",
".",
"startswith",
"(",
"\"fades\"",
")",
":",
"continue",
"if",
"not",
"import_part",
":",
"# the fades comment was done at the beginning of the line,",
"# which means that the import info is in the next one",
"import_part",
"=",
"next",
"(",
"content",
")",
".",
"strip",
"(",
")",
"if",
"import_part",
".",
"startswith",
"(",
"'#'",
")",
":",
"continue",
"# get module",
"import_tokens",
"=",
"import_part",
".",
"split",
"(",
")",
"if",
"import_tokens",
"[",
"0",
"]",
"==",
"'import'",
":",
"module_path",
"=",
"import_tokens",
"[",
"1",
"]",
"elif",
"import_tokens",
"[",
"0",
"]",
"==",
"'from'",
"and",
"import_tokens",
"[",
"2",
"]",
"==",
"'import'",
":",
"module_path",
"=",
"import_tokens",
"[",
"1",
"]",
"else",
":",
"logger",
".",
"debug",
"(",
"\"Not understood import info: %s\"",
",",
"import_tokens",
")",
"continue",
"module",
"=",
"module_path",
".",
"split",
"(",
"\".\"",
")",
"[",
"0",
"]",
"# If fades know the real name of the pkg. Replace it!",
"if",
"module",
"in",
"PKG_NAMES_DB",
":",
"module",
"=",
"PKG_NAMES_DB",
"[",
"module",
"]",
"# To match the \"safe\" name that pkg_resources creates:",
"module",
"=",
"module",
".",
"replace",
"(",
"'_'",
",",
"'-'",
")",
"# get the fades info after 'fades' mark, if any",
"if",
"len",
"(",
"fades_part",
")",
"==",
"5",
"or",
"fades_part",
"[",
"5",
":",
"]",
".",
"strip",
"(",
")",
"[",
"0",
"]",
"in",
"\"<>=!\"",
":",
"# just the 'fades' mark, and maybe a version specification, the requirement is what",
"# was imported (maybe with that version comparison)",
"requirement",
"=",
"module",
"+",
"fades_part",
"[",
"5",
":",
"]",
"elif",
"fades_part",
"[",
"5",
"]",
"!=",
"\" \"",
":",
"# starts with fades but it's part of a longer weird word",
"logger",
".",
"warning",
"(",
"\"Not understood fades info: %r\"",
",",
"fades_part",
")",
"continue",
"else",
":",
"# more complex stuff, to be parsed as a normal requirement",
"requirement",
"=",
"fades_part",
"[",
"5",
":",
"]",
"# parse and convert the requirement",
"parsed_req",
"=",
"parse_fade_requirement",
"(",
"requirement",
")",
"if",
"parsed_req",
"is",
"None",
":",
"continue",
"repo",
",",
"dependency",
"=",
"parsed_req",
"deps",
".",
"setdefault",
"(",
"repo",
",",
"[",
"]",
")",
".",
"append",
"(",
"dependency",
")",
"return",
"deps"
] |
Parse the content of a script to find marked dependencies.
|
[
"Parse",
"the",
"content",
"of",
"a",
"script",
"to",
"find",
"marked",
"dependencies",
"."
] |
e5ea457b09b105f321d4f81772f25e8695159604
|
https://github.com/PyAr/fades/blob/e5ea457b09b105f321d4f81772f25e8695159604/fades/parsing.py#L92-L169
|
train
|
PyAr/fades
|
fades/parsing.py
|
_parse_docstring
|
def _parse_docstring(fh):
"""Parse the docstrings of a script to find marked dependencies."""
find_fades = re.compile(r'\b(fades)\b:').search
for line in fh:
if line.startswith("'"):
quote = "'"
break
if line.startswith('"'):
quote = '"'
break
else:
return {}
if line[1] == quote:
# comment start with triple quotes
endquote = quote * 3
else:
endquote = quote
if endquote in line[len(endquote):]:
docstring_lines = [line[:line.index(endquote)]]
else:
docstring_lines = [line]
for line in fh:
if endquote in line:
docstring_lines.append(line[:line.index(endquote)])
break
docstring_lines.append(line)
docstring_lines = iter(docstring_lines)
for doc_line in docstring_lines:
if find_fades(doc_line):
break
else:
return {}
return _parse_requirement(list(docstring_lines))
|
python
|
def _parse_docstring(fh):
"""Parse the docstrings of a script to find marked dependencies."""
find_fades = re.compile(r'\b(fades)\b:').search
for line in fh:
if line.startswith("'"):
quote = "'"
break
if line.startswith('"'):
quote = '"'
break
else:
return {}
if line[1] == quote:
# comment start with triple quotes
endquote = quote * 3
else:
endquote = quote
if endquote in line[len(endquote):]:
docstring_lines = [line[:line.index(endquote)]]
else:
docstring_lines = [line]
for line in fh:
if endquote in line:
docstring_lines.append(line[:line.index(endquote)])
break
docstring_lines.append(line)
docstring_lines = iter(docstring_lines)
for doc_line in docstring_lines:
if find_fades(doc_line):
break
else:
return {}
return _parse_requirement(list(docstring_lines))
|
[
"def",
"_parse_docstring",
"(",
"fh",
")",
":",
"find_fades",
"=",
"re",
".",
"compile",
"(",
"r'\\b(fades)\\b:'",
")",
".",
"search",
"for",
"line",
"in",
"fh",
":",
"if",
"line",
".",
"startswith",
"(",
"\"'\"",
")",
":",
"quote",
"=",
"\"'\"",
"break",
"if",
"line",
".",
"startswith",
"(",
"'\"'",
")",
":",
"quote",
"=",
"'\"'",
"break",
"else",
":",
"return",
"{",
"}",
"if",
"line",
"[",
"1",
"]",
"==",
"quote",
":",
"# comment start with triple quotes",
"endquote",
"=",
"quote",
"*",
"3",
"else",
":",
"endquote",
"=",
"quote",
"if",
"endquote",
"in",
"line",
"[",
"len",
"(",
"endquote",
")",
":",
"]",
":",
"docstring_lines",
"=",
"[",
"line",
"[",
":",
"line",
".",
"index",
"(",
"endquote",
")",
"]",
"]",
"else",
":",
"docstring_lines",
"=",
"[",
"line",
"]",
"for",
"line",
"in",
"fh",
":",
"if",
"endquote",
"in",
"line",
":",
"docstring_lines",
".",
"append",
"(",
"line",
"[",
":",
"line",
".",
"index",
"(",
"endquote",
")",
"]",
")",
"break",
"docstring_lines",
".",
"append",
"(",
"line",
")",
"docstring_lines",
"=",
"iter",
"(",
"docstring_lines",
")",
"for",
"doc_line",
"in",
"docstring_lines",
":",
"if",
"find_fades",
"(",
"doc_line",
")",
":",
"break",
"else",
":",
"return",
"{",
"}",
"return",
"_parse_requirement",
"(",
"list",
"(",
"docstring_lines",
")",
")"
] |
Parse the docstrings of a script to find marked dependencies.
|
[
"Parse",
"the",
"docstrings",
"of",
"a",
"script",
"to",
"find",
"marked",
"dependencies",
"."
] |
e5ea457b09b105f321d4f81772f25e8695159604
|
https://github.com/PyAr/fades/blob/e5ea457b09b105f321d4f81772f25e8695159604/fades/parsing.py#L172-L209
|
train
|
PyAr/fades
|
fades/parsing.py
|
_parse_requirement
|
def _parse_requirement(iterable):
"""Actually parse the requirements, from file or manually specified."""
deps = {}
for line in iterable:
line = line.strip()
if not line or line[0] == '#':
continue
parsed_req = parse_fade_requirement(line)
if parsed_req is None:
continue
repo, dependency = parsed_req
deps.setdefault(repo, []).append(dependency)
return deps
|
python
|
def _parse_requirement(iterable):
"""Actually parse the requirements, from file or manually specified."""
deps = {}
for line in iterable:
line = line.strip()
if not line or line[0] == '#':
continue
parsed_req = parse_fade_requirement(line)
if parsed_req is None:
continue
repo, dependency = parsed_req
deps.setdefault(repo, []).append(dependency)
return deps
|
[
"def",
"_parse_requirement",
"(",
"iterable",
")",
":",
"deps",
"=",
"{",
"}",
"for",
"line",
"in",
"iterable",
":",
"line",
"=",
"line",
".",
"strip",
"(",
")",
"if",
"not",
"line",
"or",
"line",
"[",
"0",
"]",
"==",
"'#'",
":",
"continue",
"parsed_req",
"=",
"parse_fade_requirement",
"(",
"line",
")",
"if",
"parsed_req",
"is",
"None",
":",
"continue",
"repo",
",",
"dependency",
"=",
"parsed_req",
"deps",
".",
"setdefault",
"(",
"repo",
",",
"[",
"]",
")",
".",
"append",
"(",
"dependency",
")",
"return",
"deps"
] |
Actually parse the requirements, from file or manually specified.
|
[
"Actually",
"parse",
"the",
"requirements",
"from",
"file",
"or",
"manually",
"specified",
"."
] |
e5ea457b09b105f321d4f81772f25e8695159604
|
https://github.com/PyAr/fades/blob/e5ea457b09b105f321d4f81772f25e8695159604/fades/parsing.py#L212-L226
|
train
|
PyAr/fades
|
fades/parsing.py
|
_read_lines
|
def _read_lines(filepath):
"""Read a req file to a list to support nested requirement files."""
with open(filepath, 'rt', encoding='utf8') as fh:
for line in fh:
line = line.strip()
if line.startswith("-r"):
logger.debug("Reading deps from nested requirement file: %s", line)
try:
nested_filename = line.split()[1]
except IndexError:
logger.warning(
"Invalid format to indicate a nested requirements file: '%r'", line)
else:
nested_filepath = os.path.join(
os.path.dirname(filepath), nested_filename)
yield from _read_lines(nested_filepath)
else:
yield line
|
python
|
def _read_lines(filepath):
"""Read a req file to a list to support nested requirement files."""
with open(filepath, 'rt', encoding='utf8') as fh:
for line in fh:
line = line.strip()
if line.startswith("-r"):
logger.debug("Reading deps from nested requirement file: %s", line)
try:
nested_filename = line.split()[1]
except IndexError:
logger.warning(
"Invalid format to indicate a nested requirements file: '%r'", line)
else:
nested_filepath = os.path.join(
os.path.dirname(filepath), nested_filename)
yield from _read_lines(nested_filepath)
else:
yield line
|
[
"def",
"_read_lines",
"(",
"filepath",
")",
":",
"with",
"open",
"(",
"filepath",
",",
"'rt'",
",",
"encoding",
"=",
"'utf8'",
")",
"as",
"fh",
":",
"for",
"line",
"in",
"fh",
":",
"line",
"=",
"line",
".",
"strip",
"(",
")",
"if",
"line",
".",
"startswith",
"(",
"\"-r\"",
")",
":",
"logger",
".",
"debug",
"(",
"\"Reading deps from nested requirement file: %s\"",
",",
"line",
")",
"try",
":",
"nested_filename",
"=",
"line",
".",
"split",
"(",
")",
"[",
"1",
"]",
"except",
"IndexError",
":",
"logger",
".",
"warning",
"(",
"\"Invalid format to indicate a nested requirements file: '%r'\"",
",",
"line",
")",
"else",
":",
"nested_filepath",
"=",
"os",
".",
"path",
".",
"join",
"(",
"os",
".",
"path",
".",
"dirname",
"(",
"filepath",
")",
",",
"nested_filename",
")",
"yield",
"from",
"_read_lines",
"(",
"nested_filepath",
")",
"else",
":",
"yield",
"line"
] |
Read a req file to a list to support nested requirement files.
|
[
"Read",
"a",
"req",
"file",
"to",
"a",
"list",
"to",
"support",
"nested",
"requirement",
"files",
"."
] |
e5ea457b09b105f321d4f81772f25e8695159604
|
https://github.com/PyAr/fades/blob/e5ea457b09b105f321d4f81772f25e8695159604/fades/parsing.py#L236-L253
|
train
|
PyAr/fades
|
fades/envbuilder.py
|
create_venv
|
def create_venv(requested_deps, interpreter, is_current, options, pip_options):
"""Create a new virtualvenv with the requirements of this script."""
# create virtualenv
env = _FadesEnvBuilder()
env_path, env_bin_path, pip_installed = env.create_env(interpreter, is_current, options)
venv_data = {}
venv_data['env_path'] = env_path
venv_data['env_bin_path'] = env_bin_path
venv_data['pip_installed'] = pip_installed
# install deps
installed = {}
for repo in requested_deps.keys():
if repo in (REPO_PYPI, REPO_VCS):
mgr = PipManager(env_bin_path, pip_installed=pip_installed, options=pip_options)
else:
logger.warning("Install from %r not implemented", repo)
continue
installed[repo] = {}
repo_requested = requested_deps[repo]
logger.debug("Installing dependencies for repo %r: requested=%s", repo, repo_requested)
for dependency in repo_requested:
try:
mgr.install(dependency)
except Exception:
logger.debug("Installation Step failed, removing virtualenv")
destroy_venv(env_path)
raise FadesError('Dependency installation failed')
if repo == REPO_VCS:
# no need to request the installed version, as we'll always compare
# to the url itself
project = dependency.url
version = None
else:
# always store the installed dependency, as in the future we'll select the venv
# based on what is installed, not what used requested (remember that user may
# request >, >=, etc!)
project = dependency.project_name
version = mgr.get_version(project)
installed[repo][project] = version
logger.debug("Installed dependencies: %s", installed)
return venv_data, installed
|
python
|
def create_venv(requested_deps, interpreter, is_current, options, pip_options):
"""Create a new virtualvenv with the requirements of this script."""
# create virtualenv
env = _FadesEnvBuilder()
env_path, env_bin_path, pip_installed = env.create_env(interpreter, is_current, options)
venv_data = {}
venv_data['env_path'] = env_path
venv_data['env_bin_path'] = env_bin_path
venv_data['pip_installed'] = pip_installed
# install deps
installed = {}
for repo in requested_deps.keys():
if repo in (REPO_PYPI, REPO_VCS):
mgr = PipManager(env_bin_path, pip_installed=pip_installed, options=pip_options)
else:
logger.warning("Install from %r not implemented", repo)
continue
installed[repo] = {}
repo_requested = requested_deps[repo]
logger.debug("Installing dependencies for repo %r: requested=%s", repo, repo_requested)
for dependency in repo_requested:
try:
mgr.install(dependency)
except Exception:
logger.debug("Installation Step failed, removing virtualenv")
destroy_venv(env_path)
raise FadesError('Dependency installation failed')
if repo == REPO_VCS:
# no need to request the installed version, as we'll always compare
# to the url itself
project = dependency.url
version = None
else:
# always store the installed dependency, as in the future we'll select the venv
# based on what is installed, not what used requested (remember that user may
# request >, >=, etc!)
project = dependency.project_name
version = mgr.get_version(project)
installed[repo][project] = version
logger.debug("Installed dependencies: %s", installed)
return venv_data, installed
|
[
"def",
"create_venv",
"(",
"requested_deps",
",",
"interpreter",
",",
"is_current",
",",
"options",
",",
"pip_options",
")",
":",
"# create virtualenv",
"env",
"=",
"_FadesEnvBuilder",
"(",
")",
"env_path",
",",
"env_bin_path",
",",
"pip_installed",
"=",
"env",
".",
"create_env",
"(",
"interpreter",
",",
"is_current",
",",
"options",
")",
"venv_data",
"=",
"{",
"}",
"venv_data",
"[",
"'env_path'",
"]",
"=",
"env_path",
"venv_data",
"[",
"'env_bin_path'",
"]",
"=",
"env_bin_path",
"venv_data",
"[",
"'pip_installed'",
"]",
"=",
"pip_installed",
"# install deps",
"installed",
"=",
"{",
"}",
"for",
"repo",
"in",
"requested_deps",
".",
"keys",
"(",
")",
":",
"if",
"repo",
"in",
"(",
"REPO_PYPI",
",",
"REPO_VCS",
")",
":",
"mgr",
"=",
"PipManager",
"(",
"env_bin_path",
",",
"pip_installed",
"=",
"pip_installed",
",",
"options",
"=",
"pip_options",
")",
"else",
":",
"logger",
".",
"warning",
"(",
"\"Install from %r not implemented\"",
",",
"repo",
")",
"continue",
"installed",
"[",
"repo",
"]",
"=",
"{",
"}",
"repo_requested",
"=",
"requested_deps",
"[",
"repo",
"]",
"logger",
".",
"debug",
"(",
"\"Installing dependencies for repo %r: requested=%s\"",
",",
"repo",
",",
"repo_requested",
")",
"for",
"dependency",
"in",
"repo_requested",
":",
"try",
":",
"mgr",
".",
"install",
"(",
"dependency",
")",
"except",
"Exception",
":",
"logger",
".",
"debug",
"(",
"\"Installation Step failed, removing virtualenv\"",
")",
"destroy_venv",
"(",
"env_path",
")",
"raise",
"FadesError",
"(",
"'Dependency installation failed'",
")",
"if",
"repo",
"==",
"REPO_VCS",
":",
"# no need to request the installed version, as we'll always compare",
"# to the url itself",
"project",
"=",
"dependency",
".",
"url",
"version",
"=",
"None",
"else",
":",
"# always store the installed dependency, as in the future we'll select the venv",
"# based on what is installed, not what used requested (remember that user may",
"# request >, >=, etc!)",
"project",
"=",
"dependency",
".",
"project_name",
"version",
"=",
"mgr",
".",
"get_version",
"(",
"project",
")",
"installed",
"[",
"repo",
"]",
"[",
"project",
"]",
"=",
"version",
"logger",
".",
"debug",
"(",
"\"Installed dependencies: %s\"",
",",
"installed",
")",
"return",
"venv_data",
",",
"installed"
] |
Create a new virtualvenv with the requirements of this script.
|
[
"Create",
"a",
"new",
"virtualvenv",
"with",
"the",
"requirements",
"of",
"this",
"script",
"."
] |
e5ea457b09b105f321d4f81772f25e8695159604
|
https://github.com/PyAr/fades/blob/e5ea457b09b105f321d4f81772f25e8695159604/fades/envbuilder.py#L124-L168
|
train
|
PyAr/fades
|
fades/envbuilder.py
|
destroy_venv
|
def destroy_venv(env_path, venvscache=None):
"""Destroy a venv."""
# remove the venv itself in disk
logger.debug("Destroying virtualenv at: %s", env_path)
shutil.rmtree(env_path, ignore_errors=True)
# remove venv from cache
if venvscache is not None:
venvscache.remove(env_path)
|
python
|
def destroy_venv(env_path, venvscache=None):
"""Destroy a venv."""
# remove the venv itself in disk
logger.debug("Destroying virtualenv at: %s", env_path)
shutil.rmtree(env_path, ignore_errors=True)
# remove venv from cache
if venvscache is not None:
venvscache.remove(env_path)
|
[
"def",
"destroy_venv",
"(",
"env_path",
",",
"venvscache",
"=",
"None",
")",
":",
"# remove the venv itself in disk",
"logger",
".",
"debug",
"(",
"\"Destroying virtualenv at: %s\"",
",",
"env_path",
")",
"shutil",
".",
"rmtree",
"(",
"env_path",
",",
"ignore_errors",
"=",
"True",
")",
"# remove venv from cache",
"if",
"venvscache",
"is",
"not",
"None",
":",
"venvscache",
".",
"remove",
"(",
"env_path",
")"
] |
Destroy a venv.
|
[
"Destroy",
"a",
"venv",
"."
] |
e5ea457b09b105f321d4f81772f25e8695159604
|
https://github.com/PyAr/fades/blob/e5ea457b09b105f321d4f81772f25e8695159604/fades/envbuilder.py#L171-L179
|
train
|
PyAr/fades
|
fades/envbuilder.py
|
_FadesEnvBuilder.create_with_virtualenv
|
def create_with_virtualenv(self, interpreter, virtualenv_options):
"""Create a virtualenv using the virtualenv lib."""
args = ['virtualenv', '--python', interpreter, self.env_path]
args.extend(virtualenv_options)
if not self.pip_installed:
args.insert(3, '--no-pip')
try:
helpers.logged_exec(args)
self.env_bin_path = os.path.join(self.env_path, 'bin')
except FileNotFoundError as error:
logger.error('Virtualenv is not installed. It is needed to create a virtualenv with '
'a different python version than fades (got {})'.format(error))
raise FadesError('virtualenv not found')
except helpers.ExecutionError as error:
error.dump_to_log(logger)
raise FadesError('virtualenv could not be run')
except Exception as error:
logger.exception("Error creating virtualenv: %s", error)
raise FadesError('General error while running virtualenv')
|
python
|
def create_with_virtualenv(self, interpreter, virtualenv_options):
"""Create a virtualenv using the virtualenv lib."""
args = ['virtualenv', '--python', interpreter, self.env_path]
args.extend(virtualenv_options)
if not self.pip_installed:
args.insert(3, '--no-pip')
try:
helpers.logged_exec(args)
self.env_bin_path = os.path.join(self.env_path, 'bin')
except FileNotFoundError as error:
logger.error('Virtualenv is not installed. It is needed to create a virtualenv with '
'a different python version than fades (got {})'.format(error))
raise FadesError('virtualenv not found')
except helpers.ExecutionError as error:
error.dump_to_log(logger)
raise FadesError('virtualenv could not be run')
except Exception as error:
logger.exception("Error creating virtualenv: %s", error)
raise FadesError('General error while running virtualenv')
|
[
"def",
"create_with_virtualenv",
"(",
"self",
",",
"interpreter",
",",
"virtualenv_options",
")",
":",
"args",
"=",
"[",
"'virtualenv'",
",",
"'--python'",
",",
"interpreter",
",",
"self",
".",
"env_path",
"]",
"args",
".",
"extend",
"(",
"virtualenv_options",
")",
"if",
"not",
"self",
".",
"pip_installed",
":",
"args",
".",
"insert",
"(",
"3",
",",
"'--no-pip'",
")",
"try",
":",
"helpers",
".",
"logged_exec",
"(",
"args",
")",
"self",
".",
"env_bin_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"env_path",
",",
"'bin'",
")",
"except",
"FileNotFoundError",
"as",
"error",
":",
"logger",
".",
"error",
"(",
"'Virtualenv is not installed. It is needed to create a virtualenv with '",
"'a different python version than fades (got {})'",
".",
"format",
"(",
"error",
")",
")",
"raise",
"FadesError",
"(",
"'virtualenv not found'",
")",
"except",
"helpers",
".",
"ExecutionError",
"as",
"error",
":",
"error",
".",
"dump_to_log",
"(",
"logger",
")",
"raise",
"FadesError",
"(",
"'virtualenv could not be run'",
")",
"except",
"Exception",
"as",
"error",
":",
"logger",
".",
"exception",
"(",
"\"Error creating virtualenv: %s\"",
",",
"error",
")",
"raise",
"FadesError",
"(",
"'General error while running virtualenv'",
")"
] |
Create a virtualenv using the virtualenv lib.
|
[
"Create",
"a",
"virtualenv",
"using",
"the",
"virtualenv",
"lib",
"."
] |
e5ea457b09b105f321d4f81772f25e8695159604
|
https://github.com/PyAr/fades/blob/e5ea457b09b105f321d4f81772f25e8695159604/fades/envbuilder.py#L75-L93
|
train
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.