id
int32
0
252k
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
51
19.8k
code_tokens
list
docstring
stringlengths
3
17.3k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
87
242
15,900
seequent/properties
properties/handlers.py
observer
def observer(names_or_instance, names=None, func=None, change_only=False): """Specify a callback function that will fire on Property value change Observer functions on a HasProperties class fire after the observed Property or Properties have been changed (unlike validator functions that fire on set before the value is changed). You can use this method as a decorator inside a HasProperties class .. code:: @properties.observer('variable_name') def callback_function(self, change): print(change) or you can use it to register a function to a single HasProperties instance .. code:: properties.observer(my_has_props, 'variable_name', callback_function) The variable name must refer to a Property name on the HasProperties class. A list of Property names may also be used; the same callback function will fire when any of these Properties change. Also, :class:`properties.everything <properties.utils.Sentinel>` may be specified instead of the variable name. In that case, the callback function will fire when any Property changes. The callback function must take two arguments. The first is the HasProperties instance; the second is the change notification dictionary. This dictionary contains: * 'name' - the name of the changed Property * 'previous' - the value of the Property prior to change (this will be :code:`properties.undefined` if the value was not previously set) * 'value' - the new value of the Property (this will be :code:`properties.undefined` if the value is deleted) * 'mode' - the mode of the change; for observers, this is either 'observe_set' or 'observe_change' Finally, the keyword argument **change_only** may be specified as a boolean. If False (the default), the callback function will fire any time the Property is set. If True, the callback function will only fire if the new value is different than the previous value, determined by the :code:`Property.equal` method. """ mode = 'observe_change' if change_only else 'observe_set' if names is None and func is None: return Observer(names_or_instance, mode) obs = Observer(names, mode)(func) _set_listener(names_or_instance, obs) return obs
python
def observer(names_or_instance, names=None, func=None, change_only=False): mode = 'observe_change' if change_only else 'observe_set' if names is None and func is None: return Observer(names_or_instance, mode) obs = Observer(names, mode)(func) _set_listener(names_or_instance, obs) return obs
[ "def", "observer", "(", "names_or_instance", ",", "names", "=", "None", ",", "func", "=", "None", ",", "change_only", "=", "False", ")", ":", "mode", "=", "'observe_change'", "if", "change_only", "else", "'observe_set'", "if", "names", "is", "None", "and", ...
Specify a callback function that will fire on Property value change Observer functions on a HasProperties class fire after the observed Property or Properties have been changed (unlike validator functions that fire on set before the value is changed). You can use this method as a decorator inside a HasProperties class .. code:: @properties.observer('variable_name') def callback_function(self, change): print(change) or you can use it to register a function to a single HasProperties instance .. code:: properties.observer(my_has_props, 'variable_name', callback_function) The variable name must refer to a Property name on the HasProperties class. A list of Property names may also be used; the same callback function will fire when any of these Properties change. Also, :class:`properties.everything <properties.utils.Sentinel>` may be specified instead of the variable name. In that case, the callback function will fire when any Property changes. The callback function must take two arguments. The first is the HasProperties instance; the second is the change notification dictionary. This dictionary contains: * 'name' - the name of the changed Property * 'previous' - the value of the Property prior to change (this will be :code:`properties.undefined` if the value was not previously set) * 'value' - the new value of the Property (this will be :code:`properties.undefined` if the value is deleted) * 'mode' - the mode of the change; for observers, this is either 'observe_set' or 'observe_change' Finally, the keyword argument **change_only** may be specified as a boolean. If False (the default), the callback function will fire any time the Property is set. If True, the callback function will only fire if the new value is different than the previous value, determined by the :code:`Property.equal` method.
[ "Specify", "a", "callback", "function", "that", "will", "fire", "on", "Property", "value", "change" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/handlers.py#L176-L230
15,901
seequent/properties
properties/handlers.py
validator
def validator(names_or_instance, names=None, func=None): """Specify a callback function to fire on class validation OR property set This function has two modes of operation: 1. Registering callback functions that validate Property values when they are set, before the change is saved to the HasProperties instance. This mode is very similar to the :code:`observer` function. 2. Registering callback functions that fire only when the HasProperties :code:`validate` method is called. This allows for cross-validation of Properties that should only fire when all required Properties are set. **Mode 1:** Validator functions on a HasProperties class fire on set but before the observed Property or Properties have been changed (unlike observer functions that fire after the value has been changed). You can use this method as a decorator inside a HasProperties class .. code:: @properties.validator('variable_name') def callback_function(self, change): print(change) or you can use it to register a function to a single HasProperties instance .. code:: properties.validator(my_has_props, 'variable_name', callback_function) The variable name must refer to a Property name on the HasProperties class. A list of Property names may also be used; the same callback function will fire when any of these Properties change. Also, :class:`properties.everything <properties.utils.Sentinel>` may be specified instead of the variable name. In that case, the callback function will fire when any Property changes. The callback function must take two arguments. The first is the HasProperties instance; the second is the change notification dictionary. This dictionary contains: * 'name' - the name of the changed Property * 'previous' - the value of the Property prior to change (this will be :code:`properties.undefined` if the value was not previously set) * 'value' - the new value of the Property (this will be :code:`properties.undefined` if the value is deleted) * 'mode' - the mode of the change; for validators, this is 'validate' **Mode 2:** When used as a decorator without arguments (i.e. called directly on a HasProperties method), the decorated method is registered as a class validator. These methods execute only when :code:`validate()` is called on the HasProperties instance. .. code:: @properties.validator def validation_method(self): print('validating instance of {}'.format(self.__class__)) The decorated function must only take one argument, the HasProperties instance. """ if names is None and func is None: if callable(names_or_instance): return ClassValidator(names_or_instance) return Observer(names_or_instance, 'validate') val = Observer(names, 'validate')(func) _set_listener(names_or_instance, val) return val
python
def validator(names_or_instance, names=None, func=None): if names is None and func is None: if callable(names_or_instance): return ClassValidator(names_or_instance) return Observer(names_or_instance, 'validate') val = Observer(names, 'validate')(func) _set_listener(names_or_instance, val) return val
[ "def", "validator", "(", "names_or_instance", ",", "names", "=", "None", ",", "func", "=", "None", ")", ":", "if", "names", "is", "None", "and", "func", "is", "None", ":", "if", "callable", "(", "names_or_instance", ")", ":", "return", "ClassValidator", ...
Specify a callback function to fire on class validation OR property set This function has two modes of operation: 1. Registering callback functions that validate Property values when they are set, before the change is saved to the HasProperties instance. This mode is very similar to the :code:`observer` function. 2. Registering callback functions that fire only when the HasProperties :code:`validate` method is called. This allows for cross-validation of Properties that should only fire when all required Properties are set. **Mode 1:** Validator functions on a HasProperties class fire on set but before the observed Property or Properties have been changed (unlike observer functions that fire after the value has been changed). You can use this method as a decorator inside a HasProperties class .. code:: @properties.validator('variable_name') def callback_function(self, change): print(change) or you can use it to register a function to a single HasProperties instance .. code:: properties.validator(my_has_props, 'variable_name', callback_function) The variable name must refer to a Property name on the HasProperties class. A list of Property names may also be used; the same callback function will fire when any of these Properties change. Also, :class:`properties.everything <properties.utils.Sentinel>` may be specified instead of the variable name. In that case, the callback function will fire when any Property changes. The callback function must take two arguments. The first is the HasProperties instance; the second is the change notification dictionary. This dictionary contains: * 'name' - the name of the changed Property * 'previous' - the value of the Property prior to change (this will be :code:`properties.undefined` if the value was not previously set) * 'value' - the new value of the Property (this will be :code:`properties.undefined` if the value is deleted) * 'mode' - the mode of the change; for validators, this is 'validate' **Mode 2:** When used as a decorator without arguments (i.e. called directly on a HasProperties method), the decorated method is registered as a class validator. These methods execute only when :code:`validate()` is called on the HasProperties instance. .. code:: @properties.validator def validation_method(self): print('validating instance of {}'.format(self.__class__)) The decorated function must only take one argument, the HasProperties instance.
[ "Specify", "a", "callback", "function", "to", "fire", "on", "class", "validation", "OR", "property", "set" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/handlers.py#L233-L308
15,902
seequent/properties
properties/base/base.py
build_from_bases
def build_from_bases(bases, classdict, attr, attr_dict): """Helper function to build private HasProperties attributes""" output = OrderedDict() output_keys = set() all_bases = [] # Go through the bases from furthest to nearest ancestor for base in reversed(bases): # Only keep the items that are still defined on the bases if base is not object and isinstance(base, PropertyMetaclass): output_keys = output_keys.union(getattr(base, attr)) # Collect all bases so we ensure overridden items are assigned # in the correct order for item in reversed(base.__mro__): if item is object or not isinstance(item, PropertyMetaclass): continue if item not in all_bases: all_bases.append(item) # Update the items in reverse MRO order; only keep those that are # defined on the bases for base in all_bases: for key, val in iteritems(getattr(base, attr)): if key in base.__dict__ and key in output_keys: output.update({key: val}) # Remove all items that were overridden by this class; this is # potentially a superset of the items added back in the next step. for key in classdict: if key in output: output.pop(key) # Update the items with those defined on this class output.update(attr_dict) return output
python
def build_from_bases(bases, classdict, attr, attr_dict): output = OrderedDict() output_keys = set() all_bases = [] # Go through the bases from furthest to nearest ancestor for base in reversed(bases): # Only keep the items that are still defined on the bases if base is not object and isinstance(base, PropertyMetaclass): output_keys = output_keys.union(getattr(base, attr)) # Collect all bases so we ensure overridden items are assigned # in the correct order for item in reversed(base.__mro__): if item is object or not isinstance(item, PropertyMetaclass): continue if item not in all_bases: all_bases.append(item) # Update the items in reverse MRO order; only keep those that are # defined on the bases for base in all_bases: for key, val in iteritems(getattr(base, attr)): if key in base.__dict__ and key in output_keys: output.update({key: val}) # Remove all items that were overridden by this class; this is # potentially a superset of the items added back in the next step. for key in classdict: if key in output: output.pop(key) # Update the items with those defined on this class output.update(attr_dict) return output
[ "def", "build_from_bases", "(", "bases", ",", "classdict", ",", "attr", ",", "attr_dict", ")", ":", "output", "=", "OrderedDict", "(", ")", "output_keys", "=", "set", "(", ")", "all_bases", "=", "[", "]", "# Go through the bases from furthest to nearest ancestor",...
Helper function to build private HasProperties attributes
[ "Helper", "function", "to", "build", "private", "HasProperties", "attributes" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/base/base.py#L25-L55
15,903
seequent/properties
properties/base/base.py
HasProperties._reset
def _reset(self, name=None): """Revert specified property to default value If no property is specified, all properties are returned to default. """ if name is None: for key in self._props: if isinstance(self._props[key], basic.Property): self._reset(key) return if name not in self._props: raise AttributeError("Input name '{}' is not a known " "property or attribute".format(name)) if not isinstance(self._props[name], basic.Property): raise AttributeError("Cannot reset GettableProperty " "'{}'".format(name)) if name in self._defaults: val = self._defaults[name] else: val = self._props[name].default if callable(val): val = val() setattr(self, name, val)
python
def _reset(self, name=None): if name is None: for key in self._props: if isinstance(self._props[key], basic.Property): self._reset(key) return if name not in self._props: raise AttributeError("Input name '{}' is not a known " "property or attribute".format(name)) if not isinstance(self._props[name], basic.Property): raise AttributeError("Cannot reset GettableProperty " "'{}'".format(name)) if name in self._defaults: val = self._defaults[name] else: val = self._props[name].default if callable(val): val = val() setattr(self, name, val)
[ "def", "_reset", "(", "self", ",", "name", "=", "None", ")", ":", "if", "name", "is", "None", ":", "for", "key", "in", "self", ".", "_props", ":", "if", "isinstance", "(", "self", ".", "_props", "[", "key", "]", ",", "basic", ".", "Property", ")"...
Revert specified property to default value If no property is specified, all properties are returned to default.
[ "Revert", "specified", "property", "to", "default", "value" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/base/base.py#L377-L399
15,904
seequent/properties
properties/base/base.py
HasProperties.validate
def validate(self): """Call all registered class validator methods These are all methods decorated with :code:`@properties.validator`. Validator methods are expected to raise a ValidationError if they fail. """ if getattr(self, '_getting_validated', False): return True self._getting_validated = True self._validation_error_tuples = [] self._non_validation_error = None try: for val in itervalues(self._class_validators): try: if isinstance(val.func, string_types): valid = getattr(self, val.func)() else: valid = val.func(self) if valid is False: raise utils.ValidationError( 'Validation failed', None, None, self ) except utils.ValidationError as val_err: self._validation_error_tuples += val_err.error_tuples except GENERIC_ERRORS as err: if not self._non_validation_error: self._non_validation_error = err if self._validation_error_tuples: self._error_hook(self._validation_error_tuples) msgs = ['Validation failed:'] msgs += [val.message for val in self._validation_error_tuples] raise utils.ValidationError( message='\n- '.join(msgs), _error_tuples=self._validation_error_tuples, ) if self._non_validation_error: raise self._non_validation_error #pylint: disable=raising-bad-type return True finally: self._getting_validated = False self._validation_error_tuples = None self._non_validation_error = None
python
def validate(self): if getattr(self, '_getting_validated', False): return True self._getting_validated = True self._validation_error_tuples = [] self._non_validation_error = None try: for val in itervalues(self._class_validators): try: if isinstance(val.func, string_types): valid = getattr(self, val.func)() else: valid = val.func(self) if valid is False: raise utils.ValidationError( 'Validation failed', None, None, self ) except utils.ValidationError as val_err: self._validation_error_tuples += val_err.error_tuples except GENERIC_ERRORS as err: if not self._non_validation_error: self._non_validation_error = err if self._validation_error_tuples: self._error_hook(self._validation_error_tuples) msgs = ['Validation failed:'] msgs += [val.message for val in self._validation_error_tuples] raise utils.ValidationError( message='\n- '.join(msgs), _error_tuples=self._validation_error_tuples, ) if self._non_validation_error: raise self._non_validation_error #pylint: disable=raising-bad-type return True finally: self._getting_validated = False self._validation_error_tuples = None self._non_validation_error = None
[ "def", "validate", "(", "self", ")", ":", "if", "getattr", "(", "self", ",", "'_getting_validated'", ",", "False", ")", ":", "return", "True", "self", ".", "_getting_validated", "=", "True", "self", ".", "_validation_error_tuples", "=", "[", "]", "self", "...
Call all registered class validator methods These are all methods decorated with :code:`@properties.validator`. Validator methods are expected to raise a ValidationError if they fail.
[ "Call", "all", "registered", "class", "validator", "methods" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/base/base.py#L401-L443
15,905
seequent/properties
properties/base/base.py
HasProperties._deserialize_class
def _deserialize_class(cls, input_cls_name, trusted, strict): """Returns the HasProperties class to use for deserialization""" if not input_cls_name or input_cls_name == cls.__name__: return cls if trusted and input_cls_name in cls._REGISTRY: return cls._REGISTRY[input_cls_name] if strict: raise ValueError( 'Class name {} from deserialization input dictionary does ' 'not match input class {}'.format(input_cls_name, cls.__name__) ) return cls
python
def _deserialize_class(cls, input_cls_name, trusted, strict): if not input_cls_name or input_cls_name == cls.__name__: return cls if trusted and input_cls_name in cls._REGISTRY: return cls._REGISTRY[input_cls_name] if strict: raise ValueError( 'Class name {} from deserialization input dictionary does ' 'not match input class {}'.format(input_cls_name, cls.__name__) ) return cls
[ "def", "_deserialize_class", "(", "cls", ",", "input_cls_name", ",", "trusted", ",", "strict", ")", ":", "if", "not", "input_cls_name", "or", "input_cls_name", "==", "cls", ".", "__name__", ":", "return", "cls", "if", "trusted", "and", "input_cls_name", "in", ...
Returns the HasProperties class to use for deserialization
[ "Returns", "the", "HasProperties", "class", "to", "use", "for", "deserialization" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/base/base.py#L592-L603
15,906
seequent/properties
properties/extras/task.py
BaseTask.report_status
def report_status(self, status): """Hook for reporting the task status towards completion""" status = Instance('', TaskStatus).validate(None, status) print(r'{taskname} | {percent:>3}% | {message}'.format( taskname=self.__class__.__name__, percent=int(round(100*status.progress)), message=status.message if status.message else '', ))
python
def report_status(self, status): status = Instance('', TaskStatus).validate(None, status) print(r'{taskname} | {percent:>3}% | {message}'.format( taskname=self.__class__.__name__, percent=int(round(100*status.progress)), message=status.message if status.message else '', ))
[ "def", "report_status", "(", "self", ",", "status", ")", ":", "status", "=", "Instance", "(", "''", ",", "TaskStatus", ")", ".", "validate", "(", "None", ",", "status", ")", "print", "(", "r'{taskname} | {percent:>3}% | {message}'", ".", "format", "(", "task...
Hook for reporting the task status towards completion
[ "Hook", "for", "reporting", "the", "task", "status", "towards", "completion" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/extras/task.py#L81-L88
15,907
seequent/properties
properties/extras/uid.py
HasUID.serialize
def serialize(self, include_class=True, save_dynamic=False, **kwargs): """Serialize nested HasUID instances to a flat dictionary **Parameters**: * **include_class** - If True (the default), the name of the class will also be saved to the serialized dictionary under key :code:`'__class__'` * **save_dynamic** - If True, dynamic properties are written to the serialized dict (default: False). * You may also specify a **registry** - This is the flat dictionary where UID/HasUID pairs are stored. By default, no registry need be provided; a new dictionary will be created. * Any other keyword arguments will be passed through to the Property serializers. """ registry = kwargs.pop('registry', None) if registry is None: registry = dict() if not registry: root = True registry.update({'__root__': self.uid}) else: root = False key = self.uid if key not in registry: registry.update({key: None}) registry.update({key: super(HasUID, self).serialize( registry=registry, include_class=include_class, save_dynamic=save_dynamic, **kwargs )}) if root: return registry return key
python
def serialize(self, include_class=True, save_dynamic=False, **kwargs): registry = kwargs.pop('registry', None) if registry is None: registry = dict() if not registry: root = True registry.update({'__root__': self.uid}) else: root = False key = self.uid if key not in registry: registry.update({key: None}) registry.update({key: super(HasUID, self).serialize( registry=registry, include_class=include_class, save_dynamic=save_dynamic, **kwargs )}) if root: return registry return key
[ "def", "serialize", "(", "self", ",", "include_class", "=", "True", ",", "save_dynamic", "=", "False", ",", "*", "*", "kwargs", ")", ":", "registry", "=", "kwargs", ".", "pop", "(", "'registry'", ",", "None", ")", "if", "registry", "is", "None", ":", ...
Serialize nested HasUID instances to a flat dictionary **Parameters**: * **include_class** - If True (the default), the name of the class will also be saved to the serialized dictionary under key :code:`'__class__'` * **save_dynamic** - If True, dynamic properties are written to the serialized dict (default: False). * You may also specify a **registry** - This is the flat dictionary where UID/HasUID pairs are stored. By default, no registry need be provided; a new dictionary will be created. * Any other keyword arguments will be passed through to the Property serializers.
[ "Serialize", "nested", "HasUID", "instances", "to", "a", "flat", "dictionary" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/extras/uid.py#L69-L104
15,908
seequent/properties
properties/extras/uid.py
HasUID.deserialize
def deserialize(cls, value, trusted=False, strict=False, assert_valid=False, **kwargs): """Deserialize nested HasUID instance from flat pointer dictionary **Parameters** * **value** - Flat pointer dictionary produced by :code:`serialize` with UID/HasUID key/value pairs. It also includes a :code:`__root__` key to specify the root HasUID instance. * **trusted** - If True (and if the input dictionaries have :code:`'__class__'` keyword and this class is in the registry), the new **HasProperties** class will come from the dictionary. If False (the default), only the **HasProperties** class this method is called on will be constructed. * **strict** - Requires :code:`'__class__'`, if present on the input dictionary, to match the deserialized instance's class. Also disallows unused properties in the input dictionary. Default is False. * **assert_valid** - Require deserialized instance to be valid. Default is False. * You may also specify an alternative **root** - This allows a different HasUID root instance to be specified. It overrides :code:`__root__` in the input dictionary. * Any other keyword arguments will be passed through to the Property deserializers. .. note:: HasUID instances are constructed with no input arguments (ie :code:`cls()` is called). This means deserialization will fail if the init method has been overridden to require input parameters. """ registry = kwargs.pop('registry', None) if registry is None: if not isinstance(value, dict): raise ValueError('HasUID must deserialize from dictionary') registry = value.copy() uid = kwargs.get('root', registry.get('__root__')) else: uid = value if uid in cls._INSTANCES and uid not in registry: return cls._INSTANCES[uid] if uid in cls._INSTANCES: raise ValueError('UID already used: {}'.format(uid)) if uid not in registry: raise ValueError('Invalid UID: {}'.format(uid)) value = registry[uid] if not isinstance(value, HasUID): try: input_class = value.get('__class__') except AttributeError: input_class = None new_cls = cls._deserialize_class(input_class, trusted, strict) new_inst = new_cls() registry.update({uid: new_inst}) super(HasUID, cls).deserialize( value=value, trusted=trusted, strict=strict, registry=registry, _instance=new_inst, **kwargs ) cls._INSTANCES[uid] = registry[uid] return registry[uid]
python
def deserialize(cls, value, trusted=False, strict=False, assert_valid=False, **kwargs): registry = kwargs.pop('registry', None) if registry is None: if not isinstance(value, dict): raise ValueError('HasUID must deserialize from dictionary') registry = value.copy() uid = kwargs.get('root', registry.get('__root__')) else: uid = value if uid in cls._INSTANCES and uid not in registry: return cls._INSTANCES[uid] if uid in cls._INSTANCES: raise ValueError('UID already used: {}'.format(uid)) if uid not in registry: raise ValueError('Invalid UID: {}'.format(uid)) value = registry[uid] if not isinstance(value, HasUID): try: input_class = value.get('__class__') except AttributeError: input_class = None new_cls = cls._deserialize_class(input_class, trusted, strict) new_inst = new_cls() registry.update({uid: new_inst}) super(HasUID, cls).deserialize( value=value, trusted=trusted, strict=strict, registry=registry, _instance=new_inst, **kwargs ) cls._INSTANCES[uid] = registry[uid] return registry[uid]
[ "def", "deserialize", "(", "cls", ",", "value", ",", "trusted", "=", "False", ",", "strict", "=", "False", ",", "assert_valid", "=", "False", ",", "*", "*", "kwargs", ")", ":", "registry", "=", "kwargs", ".", "pop", "(", "'registry'", ",", "None", ")...
Deserialize nested HasUID instance from flat pointer dictionary **Parameters** * **value** - Flat pointer dictionary produced by :code:`serialize` with UID/HasUID key/value pairs. It also includes a :code:`__root__` key to specify the root HasUID instance. * **trusted** - If True (and if the input dictionaries have :code:`'__class__'` keyword and this class is in the registry), the new **HasProperties** class will come from the dictionary. If False (the default), only the **HasProperties** class this method is called on will be constructed. * **strict** - Requires :code:`'__class__'`, if present on the input dictionary, to match the deserialized instance's class. Also disallows unused properties in the input dictionary. Default is False. * **assert_valid** - Require deserialized instance to be valid. Default is False. * You may also specify an alternative **root** - This allows a different HasUID root instance to be specified. It overrides :code:`__root__` in the input dictionary. * Any other keyword arguments will be passed through to the Property deserializers. .. note:: HasUID instances are constructed with no input arguments (ie :code:`cls()` is called). This means deserialization will fail if the init method has been overridden to require input parameters.
[ "Deserialize", "nested", "HasUID", "instance", "from", "flat", "pointer", "dictionary" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/extras/uid.py#L107-L172
15,909
seequent/properties
properties/extras/uid.py
Pointer.deserialize
def deserialize(self, value, **kwargs): """Deserialize instance from JSON value If a deserializer is registered, that is used. Otherwise, if the instance_class is a HasProperties subclass, an instance can be deserialized from a dictionary. """ kwargs.update({'trusted': kwargs.get('trusted', False)}) if self.deserializer is not None: return self.deserializer(value, **kwargs) if value is None: return None if isinstance(value, string_types): return value if issubclass(self.instance_class, base.HasProperties): return self.instance_class.deserialize(value, **kwargs) return self.from_json(value, **kwargs)
python
def deserialize(self, value, **kwargs): kwargs.update({'trusted': kwargs.get('trusted', False)}) if self.deserializer is not None: return self.deserializer(value, **kwargs) if value is None: return None if isinstance(value, string_types): return value if issubclass(self.instance_class, base.HasProperties): return self.instance_class.deserialize(value, **kwargs) return self.from_json(value, **kwargs)
[ "def", "deserialize", "(", "self", ",", "value", ",", "*", "*", "kwargs", ")", ":", "kwargs", ".", "update", "(", "{", "'trusted'", ":", "kwargs", ".", "get", "(", "'trusted'", ",", "False", ")", "}", ")", "if", "self", ".", "deserializer", "is", "...
Deserialize instance from JSON value If a deserializer is registered, that is used. Otherwise, if the instance_class is a HasProperties subclass, an instance can be deserialized from a dictionary.
[ "Deserialize", "instance", "from", "JSON", "value" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/extras/uid.py#L255-L271
15,910
seequent/properties
properties/images.py
ImagePNG.validate
def validate(self, instance, value): """Checks if value is an open PNG file, valid filename, or png.Image Returns an open bytestream of the image """ # Pass if already validated if getattr(value, '__valid__', False): return value # Validate that value is PNG if isinstance(value, png.Image): pass else: value = super(ImagePNG, self).validate(instance, value) try: png.Reader(value).validate_signature() except png.FormatError: self.error(instance, value, extra='Open file is not PNG.') value.seek(0) # Write input to new bytestream output = BytesIO() output.name = self.filename output.__valid__ = True if isinstance(value, png.Image): value.save(output) else: fid = value fid.seek(0) output.write(fid.read()) fid.close() output.seek(0) return output
python
def validate(self, instance, value): # Pass if already validated if getattr(value, '__valid__', False): return value # Validate that value is PNG if isinstance(value, png.Image): pass else: value = super(ImagePNG, self).validate(instance, value) try: png.Reader(value).validate_signature() except png.FormatError: self.error(instance, value, extra='Open file is not PNG.') value.seek(0) # Write input to new bytestream output = BytesIO() output.name = self.filename output.__valid__ = True if isinstance(value, png.Image): value.save(output) else: fid = value fid.seek(0) output.write(fid.read()) fid.close() output.seek(0) return output
[ "def", "validate", "(", "self", ",", "instance", ",", "value", ")", ":", "# Pass if already validated", "if", "getattr", "(", "value", ",", "'__valid__'", ",", "False", ")", ":", "return", "value", "# Validate that value is PNG", "if", "isinstance", "(", "value"...
Checks if value is an open PNG file, valid filename, or png.Image Returns an open bytestream of the image
[ "Checks", "if", "value", "is", "an", "open", "PNG", "file", "valid", "filename", "or", "png", ".", "Image" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/images.py#L53-L83
15,911
seequent/properties
properties/images.py
ImagePNG.to_json
def to_json(value, **kwargs): """Convert a PNG Image to base64-encoded JSON to_json assumes that value has passed validation. """ b64rep = base64.b64encode(value.read()) value.seek(0) jsonrep = '{preamble}{b64}'.format( preamble=PNG_PREAMBLE, b64=b64rep.decode(), ) return jsonrep
python
def to_json(value, **kwargs): b64rep = base64.b64encode(value.read()) value.seek(0) jsonrep = '{preamble}{b64}'.format( preamble=PNG_PREAMBLE, b64=b64rep.decode(), ) return jsonrep
[ "def", "to_json", "(", "value", ",", "*", "*", "kwargs", ")", ":", "b64rep", "=", "base64", ".", "b64encode", "(", "value", ".", "read", "(", ")", ")", "value", ".", "seek", "(", "0", ")", "jsonrep", "=", "'{preamble}{b64}'", ".", "format", "(", "p...
Convert a PNG Image to base64-encoded JSON to_json assumes that value has passed validation.
[ "Convert", "a", "PNG", "Image", "to", "base64", "-", "encoded", "JSON" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/images.py#L86-L97
15,912
seequent/properties
properties/images.py
ImagePNG.from_json
def from_json(value, **kwargs): """Convert a PNG Image from base64-encoded JSON""" if not value.startswith(PNG_PREAMBLE): raise ValueError('Not a valid base64-encoded PNG image') infile = BytesIO() rep = base64.b64decode(value[len(PNG_PREAMBLE):].encode('utf-8')) infile.write(rep) infile.seek(0) return infile
python
def from_json(value, **kwargs): if not value.startswith(PNG_PREAMBLE): raise ValueError('Not a valid base64-encoded PNG image') infile = BytesIO() rep = base64.b64decode(value[len(PNG_PREAMBLE):].encode('utf-8')) infile.write(rep) infile.seek(0) return infile
[ "def", "from_json", "(", "value", ",", "*", "*", "kwargs", ")", ":", "if", "not", "value", ".", "startswith", "(", "PNG_PREAMBLE", ")", ":", "raise", "ValueError", "(", "'Not a valid base64-encoded PNG image'", ")", "infile", "=", "BytesIO", "(", ")", "rep",...
Convert a PNG Image from base64-encoded JSON
[ "Convert", "a", "PNG", "Image", "from", "base64", "-", "encoded", "JSON" ]
096b07012fff86b0a880c8c018320c3b512751b9
https://github.com/seequent/properties/blob/096b07012fff86b0a880c8c018320c3b512751b9/properties/images.py#L100-L108
15,913
eventbrite/conformity
conformity/validator.py
validate
def validate(schema, value, noun='value'): """ Checks the value against the schema, and raises ValidationError if validation fails. """ errors = schema.errors(value) if errors: error_details = '' for error in errors: if error.pointer: error_details += ' - %s: %s\n' % (error.pointer, error.message) else: error_details += ' - %s\n' % error.message raise ValidationError('Invalid %s:\n%s' % (noun, error_details))
python
def validate(schema, value, noun='value'): errors = schema.errors(value) if errors: error_details = '' for error in errors: if error.pointer: error_details += ' - %s: %s\n' % (error.pointer, error.message) else: error_details += ' - %s\n' % error.message raise ValidationError('Invalid %s:\n%s' % (noun, error_details))
[ "def", "validate", "(", "schema", ",", "value", ",", "noun", "=", "'value'", ")", ":", "errors", "=", "schema", ".", "errors", "(", "value", ")", "if", "errors", ":", "error_details", "=", "''", "for", "error", "in", "errors", ":", "if", "error", "."...
Checks the value against the schema, and raises ValidationError if validation fails.
[ "Checks", "the", "value", "against", "the", "schema", "and", "raises", "ValidationError", "if", "validation", "fails", "." ]
12014fe4e14f66869ffda9f9ca09cd20a985c769
https://github.com/eventbrite/conformity/blob/12014fe4e14f66869ffda9f9ca09cd20a985c769/conformity/validator.py#L36-L49
15,914
eventbrite/conformity
conformity/validator.py
validate_call
def validate_call(kwargs, returns, is_method=False): """ Decorator which runs validation on a callable's arguments and its return value. Pass a schema for the kwargs and for the return value. Positional arguments are not supported. """ def decorator(func): @wraps(func) def inner(*passed_args, **passed_kwargs): # Enforce no positional args # first argument of instance method and class method is always positonal so we need # to make expception for them. Static methods are still validated according to standard rules # this check happens before methods are bound, so instance method is still a regular function max_allowed_passed_args_len = 0 if is_method and type(func) in (types.FunctionType, classmethod): max_allowed_passed_args_len = 1 if len(passed_args) > max_allowed_passed_args_len: raise PositionalError('You cannot call this with positional arguments.') # Validate keyword arguments validate(kwargs, passed_kwargs, 'keyword arguments') # Call callable return_value = func(*passed_args, **passed_kwargs) # Validate return value validate(returns, return_value, 'return value') return return_value inner.__wrapped__ = func # caveat: checking for f.__validated__ will only work if @validate_call # is not masked by other decorators except for @classmethod or @staticmethod inner.__validated__ = True return inner return decorator
python
def validate_call(kwargs, returns, is_method=False): def decorator(func): @wraps(func) def inner(*passed_args, **passed_kwargs): # Enforce no positional args # first argument of instance method and class method is always positonal so we need # to make expception for them. Static methods are still validated according to standard rules # this check happens before methods are bound, so instance method is still a regular function max_allowed_passed_args_len = 0 if is_method and type(func) in (types.FunctionType, classmethod): max_allowed_passed_args_len = 1 if len(passed_args) > max_allowed_passed_args_len: raise PositionalError('You cannot call this with positional arguments.') # Validate keyword arguments validate(kwargs, passed_kwargs, 'keyword arguments') # Call callable return_value = func(*passed_args, **passed_kwargs) # Validate return value validate(returns, return_value, 'return value') return return_value inner.__wrapped__ = func # caveat: checking for f.__validated__ will only work if @validate_call # is not masked by other decorators except for @classmethod or @staticmethod inner.__validated__ = True return inner return decorator
[ "def", "validate_call", "(", "kwargs", ",", "returns", ",", "is_method", "=", "False", ")", ":", "def", "decorator", "(", "func", ")", ":", "@", "wraps", "(", "func", ")", "def", "inner", "(", "*", "passed_args", ",", "*", "*", "passed_kwargs", ")", ...
Decorator which runs validation on a callable's arguments and its return value. Pass a schema for the kwargs and for the return value. Positional arguments are not supported.
[ "Decorator", "which", "runs", "validation", "on", "a", "callable", "s", "arguments", "and", "its", "return", "value", ".", "Pass", "a", "schema", "for", "the", "kwargs", "and", "for", "the", "return", "value", ".", "Positional", "arguments", "are", "not", ...
12014fe4e14f66869ffda9f9ca09cd20a985c769
https://github.com/eventbrite/conformity/blob/12014fe4e14f66869ffda9f9ca09cd20a985c769/conformity/validator.py#L52-L83
15,915
BBVA/patton-cli
patton_client/model.py
PattonResults.dump
def dump(self): """Dump to file""" # NO Dump file selected -> DO NOTHING if self.running_config.output_file: # Determinate file format _, extension = op.splitext(self.running_config.output_file) extension = extension.replace(".", "") if extension not in self.ALLOWED_DUMP_FORMATS: raise PCException( f"Extension of dump file is not available. " f"Allowed extensions are: " f"{', '.join(self.ALLOWED_DUMP_FORMATS)}") with open(self.running_config.output_file, "w") as f: if extension == "csv": csv_writer = csv.writer(f) csv_writer.writerow(("# Name", "CPE", "CVE", "Score", "Summary")) csv_writer.writerows(self._to_csv()) elif extension == "json": json.dump(self.results, f, indent=4, sort_keys=True) elif extension == "raw": f.write(self._to_table())
python
def dump(self): # NO Dump file selected -> DO NOTHING if self.running_config.output_file: # Determinate file format _, extension = op.splitext(self.running_config.output_file) extension = extension.replace(".", "") if extension not in self.ALLOWED_DUMP_FORMATS: raise PCException( f"Extension of dump file is not available. " f"Allowed extensions are: " f"{', '.join(self.ALLOWED_DUMP_FORMATS)}") with open(self.running_config.output_file, "w") as f: if extension == "csv": csv_writer = csv.writer(f) csv_writer.writerow(("# Name", "CPE", "CVE", "Score", "Summary")) csv_writer.writerows(self._to_csv()) elif extension == "json": json.dump(self.results, f, indent=4, sort_keys=True) elif extension == "raw": f.write(self._to_table())
[ "def", "dump", "(", "self", ")", ":", "# NO Dump file selected -> DO NOTHING", "if", "self", ".", "running_config", ".", "output_file", ":", "# Determinate file format", "_", ",", "extension", "=", "op", ".", "splitext", "(", "self", ".", "running_config", ".", ...
Dump to file
[ "Dump", "to", "file" ]
b31d9a134158d4c15eeeefde305f193d3f01a2de
https://github.com/BBVA/patton-cli/blob/b31d9a134158d4c15eeeefde305f193d3f01a2de/patton_client/model.py#L173-L206
15,916
edx/django-user-tasks
docs/conf.py
on_init
def on_init(app): # pylint: disable=unused-argument """ Run sphinx-apidoc and swg2rst after Sphinx initialization. Read the Docs won't run tox or custom shell commands, so we need this to avoid checking in the generated reStructuredText files. """ docs_path = os.path.abspath(os.path.dirname(__file__)) root_path = os.path.abspath(os.path.join(docs_path, '..')) apidoc_path = 'sphinx-apidoc' swg2rst_path = 'swg2rst' if hasattr(sys, 'real_prefix'): # Check to see if we are in a virtualenv # If we are, assemble the path manually bin_path = os.path.abspath(os.path.join(sys.prefix, 'bin')) apidoc_path = os.path.join(bin_path, apidoc_path) swg2rst_path = os.path.join(bin_path, swg2rst_path) check_call([apidoc_path, '-o', docs_path, os.path.join(root_path, 'user_tasks'), os.path.join(root_path, 'user_tasks/migrations')]) json_path = os.path.join(docs_path, 'swagger.json') rst_path = os.path.join(docs_path, 'rest_api.rst') check_call([swg2rst_path, json_path, '-f', 'rst', '-o', rst_path])
python
def on_init(app): # pylint: disable=unused-argument docs_path = os.path.abspath(os.path.dirname(__file__)) root_path = os.path.abspath(os.path.join(docs_path, '..')) apidoc_path = 'sphinx-apidoc' swg2rst_path = 'swg2rst' if hasattr(sys, 'real_prefix'): # Check to see if we are in a virtualenv # If we are, assemble the path manually bin_path = os.path.abspath(os.path.join(sys.prefix, 'bin')) apidoc_path = os.path.join(bin_path, apidoc_path) swg2rst_path = os.path.join(bin_path, swg2rst_path) check_call([apidoc_path, '-o', docs_path, os.path.join(root_path, 'user_tasks'), os.path.join(root_path, 'user_tasks/migrations')]) json_path = os.path.join(docs_path, 'swagger.json') rst_path = os.path.join(docs_path, 'rest_api.rst') check_call([swg2rst_path, json_path, '-f', 'rst', '-o', rst_path])
[ "def", "on_init", "(", "app", ")", ":", "# pylint: disable=unused-argument", "docs_path", "=", "os", ".", "path", ".", "abspath", "(", "os", ".", "path", ".", "dirname", "(", "__file__", ")", ")", "root_path", "=", "os", ".", "path", ".", "abspath", "(",...
Run sphinx-apidoc and swg2rst after Sphinx initialization. Read the Docs won't run tox or custom shell commands, so we need this to avoid checking in the generated reStructuredText files.
[ "Run", "sphinx", "-", "apidoc", "and", "swg2rst", "after", "Sphinx", "initialization", "." ]
6a9cf3821f4d8e202e6b48703e6a62e2a889adfb
https://github.com/edx/django-user-tasks/blob/6a9cf3821f4d8e202e6b48703e6a62e2a889adfb/docs/conf.py#L464-L484
15,917
edx/django-user-tasks
user_tasks/signals.py
_get_or_create_group_parent
def _get_or_create_group_parent(message_body, user_id): """ Determine if the given task belongs to a group or not, and if so, get or create a status record for the group. Arguments: message_body (dict): The body of the before_task_publish signal for the task in question user_id (int): The primary key of the user model record for the user who triggered the task. (If using a custom user model, this may not be an integer.) Returns ------- UserTaskStatus: The status record for the containing group, or `None` if there isn't one """ parent_id = message_body.get('taskset', None) if not parent_id: # Not part of a group return None parent_class = 'celery.group' parent_name = message_body['kwargs'].get('user_task_name', '') parent, _ = UserTaskStatus.objects.get_or_create( task_id=parent_id, defaults={'is_container': True, 'name': parent_name, 'task_class': parent_class, 'total_steps': 0, 'user_id': user_id}) if parent_name and not parent.name: parent.name = parent_name parent.save(update_fields={'name', 'modified'}) return parent
python
def _get_or_create_group_parent(message_body, user_id): parent_id = message_body.get('taskset', None) if not parent_id: # Not part of a group return None parent_class = 'celery.group' parent_name = message_body['kwargs'].get('user_task_name', '') parent, _ = UserTaskStatus.objects.get_or_create( task_id=parent_id, defaults={'is_container': True, 'name': parent_name, 'task_class': parent_class, 'total_steps': 0, 'user_id': user_id}) if parent_name and not parent.name: parent.name = parent_name parent.save(update_fields={'name', 'modified'}) return parent
[ "def", "_get_or_create_group_parent", "(", "message_body", ",", "user_id", ")", ":", "parent_id", "=", "message_body", ".", "get", "(", "'taskset'", ",", "None", ")", "if", "not", "parent_id", ":", "# Not part of a group", "return", "None", "parent_class", "=", ...
Determine if the given task belongs to a group or not, and if so, get or create a status record for the group. Arguments: message_body (dict): The body of the before_task_publish signal for the task in question user_id (int): The primary key of the user model record for the user who triggered the task. (If using a custom user model, this may not be an integer.) Returns ------- UserTaskStatus: The status record for the containing group, or `None` if there isn't one
[ "Determine", "if", "the", "given", "task", "belongs", "to", "a", "group", "or", "not", "and", "if", "so", "get", "or", "create", "a", "status", "record", "for", "the", "group", "." ]
6a9cf3821f4d8e202e6b48703e6a62e2a889adfb
https://github.com/edx/django-user-tasks/blob/6a9cf3821f4d8e202e6b48703e6a62e2a889adfb/user_tasks/signals.py#L134-L160
15,918
edx/django-user-tasks
user_tasks/signals.py
_get_user_id
def _get_user_id(arguments_dict): """ Get and validate the `user_id` argument to a task derived from `UserTaskMixin`. Arguments: arguments_dict (dict): The parsed positional and keyword arguments to the task Returns ------- int: The primary key of a user record (may not be an int if using a custom user model) """ if 'user_id' not in arguments_dict: raise TypeError('Each invocation of a UserTaskMixin subclass must include the user_id') user_id = arguments_dict['user_id'] try: get_user_model().objects.get(pk=user_id) except (ValueError, get_user_model().DoesNotExist): raise TypeError('Invalid user_id: {}'.format(user_id)) return user_id
python
def _get_user_id(arguments_dict): if 'user_id' not in arguments_dict: raise TypeError('Each invocation of a UserTaskMixin subclass must include the user_id') user_id = arguments_dict['user_id'] try: get_user_model().objects.get(pk=user_id) except (ValueError, get_user_model().DoesNotExist): raise TypeError('Invalid user_id: {}'.format(user_id)) return user_id
[ "def", "_get_user_id", "(", "arguments_dict", ")", ":", "if", "'user_id'", "not", "in", "arguments_dict", ":", "raise", "TypeError", "(", "'Each invocation of a UserTaskMixin subclass must include the user_id'", ")", "user_id", "=", "arguments_dict", "[", "'user_id'", "]"...
Get and validate the `user_id` argument to a task derived from `UserTaskMixin`. Arguments: arguments_dict (dict): The parsed positional and keyword arguments to the task Returns ------- int: The primary key of a user record (may not be an int if using a custom user model)
[ "Get", "and", "validate", "the", "user_id", "argument", "to", "a", "task", "derived", "from", "UserTaskMixin", "." ]
6a9cf3821f4d8e202e6b48703e6a62e2a889adfb
https://github.com/edx/django-user-tasks/blob/6a9cf3821f4d8e202e6b48703e6a62e2a889adfb/user_tasks/signals.py#L163-L182
15,919
pytroll/trollimage
trollimage/colormap.py
colorbar
def colorbar(height, length, colormap): """Return the channels of a colorbar. """ cbar = np.tile(np.arange(length) * 1.0 / (length - 1), (height, 1)) cbar = (cbar * (colormap.values.max() - colormap.values.min()) + colormap.values.min()) return colormap.colorize(cbar)
python
def colorbar(height, length, colormap): cbar = np.tile(np.arange(length) * 1.0 / (length - 1), (height, 1)) cbar = (cbar * (colormap.values.max() - colormap.values.min()) + colormap.values.min()) return colormap.colorize(cbar)
[ "def", "colorbar", "(", "height", ",", "length", ",", "colormap", ")", ":", "cbar", "=", "np", ".", "tile", "(", "np", ".", "arange", "(", "length", ")", "*", "1.0", "/", "(", "length", "-", "1", ")", ",", "(", "height", ",", "1", ")", ")", "...
Return the channels of a colorbar.
[ "Return", "the", "channels", "of", "a", "colorbar", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/colormap.py#L445-L452
15,920
pytroll/trollimage
trollimage/colormap.py
palettebar
def palettebar(height, length, colormap): """Return the channels of a palettebar. """ cbar = np.tile(np.arange(length) * 1.0 / (length - 1), (height, 1)) cbar = (cbar * (colormap.values.max() + 1 - colormap.values.min()) + colormap.values.min()) return colormap.palettize(cbar)
python
def palettebar(height, length, colormap): cbar = np.tile(np.arange(length) * 1.0 / (length - 1), (height, 1)) cbar = (cbar * (colormap.values.max() + 1 - colormap.values.min()) + colormap.values.min()) return colormap.palettize(cbar)
[ "def", "palettebar", "(", "height", ",", "length", ",", "colormap", ")", ":", "cbar", "=", "np", ".", "tile", "(", "np", ".", "arange", "(", "length", ")", "*", "1.0", "/", "(", "length", "-", "1", ")", ",", "(", "height", ",", "1", ")", ")", ...
Return the channels of a palettebar.
[ "Return", "the", "channels", "of", "a", "palettebar", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/colormap.py#L455-L462
15,921
pytroll/trollimage
trollimage/colormap.py
Colormap.to_rio
def to_rio(self): """Converts the colormap to a rasterio colormap. """ self.colors = (((self.colors * 1.0 - self.colors.min()) / (self.colors.max() - self.colors.min())) * 255) return dict(zip(self.values, tuple(map(tuple, self.colors))))
python
def to_rio(self): self.colors = (((self.colors * 1.0 - self.colors.min()) / (self.colors.max() - self.colors.min())) * 255) return dict(zip(self.values, tuple(map(tuple, self.colors))))
[ "def", "to_rio", "(", "self", ")", ":", "self", ".", "colors", "=", "(", "(", "(", "self", ".", "colors", "*", "1.0", "-", "self", ".", "colors", ".", "min", "(", ")", ")", "/", "(", "self", ".", "colors", ".", "max", "(", ")", "-", "self", ...
Converts the colormap to a rasterio colormap.
[ "Converts", "the", "colormap", "to", "a", "rasterio", "colormap", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/colormap.py#L129-L134
15,922
edx/django-user-tasks
user_tasks/views.py
StatusViewSet.cancel
def cancel(self, request, *args, **kwargs): # pylint: disable=unused-argument """ Cancel the task associated with the specified status record. Arguments: request (Request): A POST including a task status record ID Returns ------- Response: A JSON response indicating whether the cancellation succeeded or not """ status = self.get_object() status.cancel() serializer = StatusSerializer(status, context={'request': request}) return Response(serializer.data)
python
def cancel(self, request, *args, **kwargs): # pylint: disable=unused-argument status = self.get_object() status.cancel() serializer = StatusSerializer(status, context={'request': request}) return Response(serializer.data)
[ "def", "cancel", "(", "self", ",", "request", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "# pylint: disable=unused-argument", "status", "=", "self", ".", "get_object", "(", ")", "status", ".", "cancel", "(", ")", "serializer", "=", "StatusSeriali...
Cancel the task associated with the specified status record. Arguments: request (Request): A POST including a task status record ID Returns ------- Response: A JSON response indicating whether the cancellation succeeded or not
[ "Cancel", "the", "task", "associated", "with", "the", "specified", "status", "record", "." ]
6a9cf3821f4d8e202e6b48703e6a62e2a889adfb
https://github.com/edx/django-user-tasks/blob/6a9cf3821f4d8e202e6b48703e6a62e2a889adfb/user_tasks/views.py#L61-L76
15,923
edx/django-user-tasks
schema/views.py
swagger
def swagger(request): # pylint: disable=unused-argument """ Render Swagger UI and the underlying Open API schema JSON file. """ generator = schemas.SchemaGenerator(title='django-user-tasks REST API') return response.Response(generator.get_schema())
python
def swagger(request): # pylint: disable=unused-argument generator = schemas.SchemaGenerator(title='django-user-tasks REST API') return response.Response(generator.get_schema())
[ "def", "swagger", "(", "request", ")", ":", "# pylint: disable=unused-argument", "generator", "=", "schemas", ".", "SchemaGenerator", "(", "title", "=", "'django-user-tasks REST API'", ")", "return", "response", ".", "Response", "(", "generator", ".", "get_schema", ...
Render Swagger UI and the underlying Open API schema JSON file.
[ "Render", "Swagger", "UI", "and", "the", "underlying", "Open", "API", "schema", "JSON", "file", "." ]
6a9cf3821f4d8e202e6b48703e6a62e2a889adfb
https://github.com/edx/django-user-tasks/blob/6a9cf3821f4d8e202e6b48703e6a62e2a889adfb/schema/views.py#L39-L44
15,924
edx/django-user-tasks
schema/views.py
ConditionalOpenAPIRenderer.render
def render(self, data, accepted_media_type=None, renderer_context=None): """ Render the appropriate Open API JSON file. """ if 'SWAGGER_JSON_PATH' in os.environ: with io.open(os.environ['SWAGGER_JSON_PATH'], 'rb') as f: return f.read() else: return super(ConditionalOpenAPIRenderer, self).render(data, accepted_media_type, renderer_context)
python
def render(self, data, accepted_media_type=None, renderer_context=None): if 'SWAGGER_JSON_PATH' in os.environ: with io.open(os.environ['SWAGGER_JSON_PATH'], 'rb') as f: return f.read() else: return super(ConditionalOpenAPIRenderer, self).render(data, accepted_media_type, renderer_context)
[ "def", "render", "(", "self", ",", "data", ",", "accepted_media_type", "=", "None", ",", "renderer_context", "=", "None", ")", ":", "if", "'SWAGGER_JSON_PATH'", "in", "os", ".", "environ", ":", "with", "io", ".", "open", "(", "os", ".", "environ", "[", ...
Render the appropriate Open API JSON file.
[ "Render", "the", "appropriate", "Open", "API", "JSON", "file", "." ]
6a9cf3821f4d8e202e6b48703e6a62e2a889adfb
https://github.com/edx/django-user-tasks/blob/6a9cf3821f4d8e202e6b48703e6a62e2a889adfb/schema/views.py#L26-L34
15,925
edx/django-user-tasks
user_tasks/rules.py
add_rules
def add_rules(): """ Use the rules provided in this module to implement authorization checks for the ``django-user-tasks`` models. These rules allow only superusers and the user who triggered a task to view its status or artifacts, cancel the task, or delete the status information and all its related artifacts. Only superusers are allowed to directly modify or delete an artifact (or to modify a task status record). """ rules.add_perm('user_tasks.view_usertaskstatus', STATUS_PERMISSION) rules.add_perm('user_tasks.cancel_usertaskstatus', STATUS_PERMISSION) rules.add_perm('user_tasks.change_usertaskstatus', rules.predicates.is_superuser) rules.add_perm('user_tasks.delete_usertaskstatus', STATUS_PERMISSION) rules.add_perm('user_tasks.view_usertaskartifact', ARTIFACT_PERMISSION) rules.add_perm('user_tasks.change_usertaskartifact', rules.predicates.is_superuser) rules.add_perm('user_tasks.delete_usertaskartifact', rules.predicates.is_superuser)
python
def add_rules(): rules.add_perm('user_tasks.view_usertaskstatus', STATUS_PERMISSION) rules.add_perm('user_tasks.cancel_usertaskstatus', STATUS_PERMISSION) rules.add_perm('user_tasks.change_usertaskstatus', rules.predicates.is_superuser) rules.add_perm('user_tasks.delete_usertaskstatus', STATUS_PERMISSION) rules.add_perm('user_tasks.view_usertaskartifact', ARTIFACT_PERMISSION) rules.add_perm('user_tasks.change_usertaskartifact', rules.predicates.is_superuser) rules.add_perm('user_tasks.delete_usertaskartifact', rules.predicates.is_superuser)
[ "def", "add_rules", "(", ")", ":", "rules", ".", "add_perm", "(", "'user_tasks.view_usertaskstatus'", ",", "STATUS_PERMISSION", ")", "rules", ".", "add_perm", "(", "'user_tasks.cancel_usertaskstatus'", ",", "STATUS_PERMISSION", ")", "rules", ".", "add_perm", "(", "'...
Use the rules provided in this module to implement authorization checks for the ``django-user-tasks`` models. These rules allow only superusers and the user who triggered a task to view its status or artifacts, cancel the task, or delete the status information and all its related artifacts. Only superusers are allowed to directly modify or delete an artifact (or to modify a task status record).
[ "Use", "the", "rules", "provided", "in", "this", "module", "to", "implement", "authorization", "checks", "for", "the", "django", "-", "user", "-", "tasks", "models", "." ]
6a9cf3821f4d8e202e6b48703e6a62e2a889adfb
https://github.com/edx/django-user-tasks/blob/6a9cf3821f4d8e202e6b48703e6a62e2a889adfb/user_tasks/rules.py#L64-L78
15,926
edx/django-user-tasks
user_tasks/filters.py
ArtifactFilterBackend.filter_queryset
def filter_queryset(self, request, queryset, view): """ Filter out any artifacts which the requesting user does not have permission to view. """ if request.user.is_superuser: return queryset return queryset.filter(status__user=request.user)
python
def filter_queryset(self, request, queryset, view): if request.user.is_superuser: return queryset return queryset.filter(status__user=request.user)
[ "def", "filter_queryset", "(", "self", ",", "request", ",", "queryset", ",", "view", ")", ":", "if", "request", ".", "user", ".", "is_superuser", ":", "return", "queryset", "return", "queryset", ".", "filter", "(", "status__user", "=", "request", ".", "use...
Filter out any artifacts which the requesting user does not have permission to view.
[ "Filter", "out", "any", "artifacts", "which", "the", "requesting", "user", "does", "not", "have", "permission", "to", "view", "." ]
6a9cf3821f4d8e202e6b48703e6a62e2a889adfb
https://github.com/edx/django-user-tasks/blob/6a9cf3821f4d8e202e6b48703e6a62e2a889adfb/user_tasks/filters.py#L20-L26
15,927
pytroll/trollimage
trollimage/utilities.py
_image2array
def _image2array(filepath): ''' Utility function that converts an image file in 3 np arrays that can be fed into geo_image.GeoImage in order to generate a PyTROLL GeoImage object. ''' im = Pimage.open(filepath).convert('RGB') (width, height) = im.size _r = np.array(list(im.getdata(0)))/255.0 _g = np.array(list(im.getdata(1)))/255.0 _b = np.array(list(im.getdata(2)))/255.0 _r = _r.reshape((height, width)) _g = _g.reshape((height, width)) _b = _b.reshape((height, width)) return _r, _g, _b
python
def _image2array(filepath): ''' Utility function that converts an image file in 3 np arrays that can be fed into geo_image.GeoImage in order to generate a PyTROLL GeoImage object. ''' im = Pimage.open(filepath).convert('RGB') (width, height) = im.size _r = np.array(list(im.getdata(0)))/255.0 _g = np.array(list(im.getdata(1)))/255.0 _b = np.array(list(im.getdata(2)))/255.0 _r = _r.reshape((height, width)) _g = _g.reshape((height, width)) _b = _b.reshape((height, width)) return _r, _g, _b
[ "def", "_image2array", "(", "filepath", ")", ":", "im", "=", "Pimage", ".", "open", "(", "filepath", ")", ".", "convert", "(", "'RGB'", ")", "(", "width", ",", "height", ")", "=", "im", ".", "size", "_r", "=", "np", ".", "array", "(", "list", "("...
Utility function that converts an image file in 3 np arrays that can be fed into geo_image.GeoImage in order to generate a PyTROLL GeoImage object.
[ "Utility", "function", "that", "converts", "an", "image", "file", "in", "3", "np", "arrays", "that", "can", "be", "fed", "into", "geo_image", ".", "GeoImage", "in", "order", "to", "generate", "a", "PyTROLL", "GeoImage", "object", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/utilities.py#L100-L114
15,928
pytroll/trollimage
trollimage/image.py
ycbcr2rgb
def ycbcr2rgb(y__, cb_, cr_): """Convert the three YCbCr channels to RGB channels. """ kb_ = 0.114 kr_ = 0.299 r__ = 2 * cr_ / (1 - kr_) + y__ b__ = 2 * cb_ / (1 - kb_) + y__ g__ = (y__ - kr_ * r__ - kb_ * b__) / (1 - kr_ - kb_) return r__, g__, b__
python
def ycbcr2rgb(y__, cb_, cr_): kb_ = 0.114 kr_ = 0.299 r__ = 2 * cr_ / (1 - kr_) + y__ b__ = 2 * cb_ / (1 - kb_) + y__ g__ = (y__ - kr_ * r__ - kb_ * b__) / (1 - kr_ - kb_) return r__, g__, b__
[ "def", "ycbcr2rgb", "(", "y__", ",", "cb_", ",", "cr_", ")", ":", "kb_", "=", "0.114", "kr_", "=", "0.299", "r__", "=", "2", "*", "cr_", "/", "(", "1", "-", "kr_", ")", "+", "y__", "b__", "=", "2", "*", "cb_", "/", "(", "1", "-", "kb_", "...
Convert the three YCbCr channels to RGB channels.
[ "Convert", "the", "three", "YCbCr", "channels", "to", "RGB", "channels", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/image.py#L1136-L1147
15,929
pytroll/trollimage
trollimage/image.py
Image._add_channel
def _add_channel(self, chn, color_min, color_max): """Adds a channel to the image object """ if isinstance(chn, np.ma.core.MaskedArray): chn_data = chn.data chn_mask = chn.mask else: chn_data = np.array(chn) chn_mask = False scaled = ((chn_data - color_min) * 1.0 / (color_max - color_min)) self.channels.append(np.ma.array(scaled, mask=chn_mask))
python
def _add_channel(self, chn, color_min, color_max): if isinstance(chn, np.ma.core.MaskedArray): chn_data = chn.data chn_mask = chn.mask else: chn_data = np.array(chn) chn_mask = False scaled = ((chn_data - color_min) * 1.0 / (color_max - color_min)) self.channels.append(np.ma.array(scaled, mask=chn_mask))
[ "def", "_add_channel", "(", "self", ",", "chn", ",", "color_min", ",", "color_max", ")", ":", "if", "isinstance", "(", "chn", ",", "np", ".", "ma", ".", "core", ".", "MaskedArray", ")", ":", "chn_data", "=", "chn", ".", "data", "chn_mask", "=", "chn"...
Adds a channel to the image object
[ "Adds", "a", "channel", "to", "the", "image", "object" ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/image.py#L221-L232
15,930
pytroll/trollimage
trollimage/image.py
Image.is_empty
def is_empty(self): """Checks for an empty image. """ if(((self.channels == []) and (not self.shape == (0, 0))) or ((not self.channels == []) and (self.shape == (0, 0)))): raise RuntimeError("Channels-shape mismatch.") return self.channels == [] and self.shape == (0, 0)
python
def is_empty(self): if(((self.channels == []) and (not self.shape == (0, 0))) or ((not self.channels == []) and (self.shape == (0, 0)))): raise RuntimeError("Channels-shape mismatch.") return self.channels == [] and self.shape == (0, 0)
[ "def", "is_empty", "(", "self", ")", ":", "if", "(", "(", "(", "self", ".", "channels", "==", "[", "]", ")", "and", "(", "not", "self", ".", "shape", "==", "(", "0", ",", "0", ")", ")", ")", "or", "(", "(", "not", "self", ".", "channels", "...
Checks for an empty image.
[ "Checks", "for", "an", "empty", "image", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/image.py#L263-L269
15,931
pytroll/trollimage
trollimage/image.py
Image._pngmeta
def _pngmeta(self): """It will return GeoImage.tags as a PNG metadata object. Inspired by: public domain, Nick Galbreath http://blog.modp.com/2007/08/python-pil-and-png-metadata-take-2.html """ reserved = ('interlace', 'gamma', 'dpi', 'transparency', 'aspect') try: tags = self.tags except AttributeError: tags = {} # Undocumented class from PIL import PngImagePlugin meta = PngImagePlugin.PngInfo() # Copy from tags to new dict for k__, v__ in tags.items(): if k__ not in reserved: meta.add_text(k__, v__, 0) return meta
python
def _pngmeta(self): reserved = ('interlace', 'gamma', 'dpi', 'transparency', 'aspect') try: tags = self.tags except AttributeError: tags = {} # Undocumented class from PIL import PngImagePlugin meta = PngImagePlugin.PngInfo() # Copy from tags to new dict for k__, v__ in tags.items(): if k__ not in reserved: meta.add_text(k__, v__, 0) return meta
[ "def", "_pngmeta", "(", "self", ")", ":", "reserved", "=", "(", "'interlace'", ",", "'gamma'", ",", "'dpi'", ",", "'transparency'", ",", "'aspect'", ")", "try", ":", "tags", "=", "self", ".", "tags", "except", "AttributeError", ":", "tags", "=", "{", "...
It will return GeoImage.tags as a PNG metadata object. Inspired by: public domain, Nick Galbreath http://blog.modp.com/2007/08/python-pil-and-png-metadata-take-2.html
[ "It", "will", "return", "GeoImage", ".", "tags", "as", "a", "PNG", "metadata", "object", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/image.py#L404-L427
15,932
pytroll/trollimage
trollimage/image.py
Image._rgb2ycbcr
def _rgb2ycbcr(self, mode): """Convert the image from RGB mode to YCbCr.""" self._check_modes(("RGB", "RGBA")) (self.channels[0], self.channels[1], self.channels[2]) = \ rgb2ycbcr(self.channels[0], self.channels[1], self.channels[2]) if self.fill_value is not None: self.fill_value[0:3] = rgb2ycbcr(self.fill_value[0], self.fill_value[1], self.fill_value[2]) self.mode = mode
python
def _rgb2ycbcr(self, mode): self._check_modes(("RGB", "RGBA")) (self.channels[0], self.channels[1], self.channels[2]) = \ rgb2ycbcr(self.channels[0], self.channels[1], self.channels[2]) if self.fill_value is not None: self.fill_value[0:3] = rgb2ycbcr(self.fill_value[0], self.fill_value[1], self.fill_value[2]) self.mode = mode
[ "def", "_rgb2ycbcr", "(", "self", ",", "mode", ")", ":", "self", ".", "_check_modes", "(", "(", "\"RGB\"", ",", "\"RGBA\"", ")", ")", "(", "self", ".", "channels", "[", "0", "]", ",", "self", ".", "channels", "[", "1", "]", ",", "self", ".", "cha...
Convert the image from RGB mode to YCbCr.
[ "Convert", "the", "image", "from", "RGB", "mode", "to", "YCbCr", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/image.py#L444-L459
15,933
pytroll/trollimage
trollimage/image.py
Image._ycbcr2rgb
def _ycbcr2rgb(self, mode): """Convert the image from YCbCr mode to RGB. """ self._check_modes(("YCbCr", "YCbCrA")) (self.channels[0], self.channels[1], self.channels[2]) = \ ycbcr2rgb(self.channels[0], self.channels[1], self.channels[2]) if self.fill_value is not None: self.fill_value[0:3] = ycbcr2rgb(self.fill_value[0], self.fill_value[1], self.fill_value[2]) self.mode = mode
python
def _ycbcr2rgb(self, mode): self._check_modes(("YCbCr", "YCbCrA")) (self.channels[0], self.channels[1], self.channels[2]) = \ ycbcr2rgb(self.channels[0], self.channels[1], self.channels[2]) if self.fill_value is not None: self.fill_value[0:3] = ycbcr2rgb(self.fill_value[0], self.fill_value[1], self.fill_value[2]) self.mode = mode
[ "def", "_ycbcr2rgb", "(", "self", ",", "mode", ")", ":", "self", ".", "_check_modes", "(", "(", "\"YCbCr\"", ",", "\"YCbCrA\"", ")", ")", "(", "self", ".", "channels", "[", "0", "]", ",", "self", ".", "channels", "[", "1", "]", ",", "self", ".", ...
Convert the image from YCbCr mode to RGB.
[ "Convert", "the", "image", "from", "YCbCr", "mode", "to", "RGB", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/image.py#L461-L477
15,934
pytroll/trollimage
trollimage/image.py
Image._to_p
def _to_p(self, mode): """Convert the image to P or PA mode. """ if self.mode.endswith("A"): chans = self.channels[:-1] alpha = self.channels[-1] self._secondary_mode = self.mode[:-1] else: chans = self.channels alpha = None self._secondary_mode = self.mode palette = [] selfmask = chans[0].mask for chn in chans[1:]: selfmask = np.ma.mask_or(selfmask, chn.mask) new_chn = np.ma.zeros(self.shape, dtype=int) color_nb = 0 for i in range(self.height): for j in range(self.width): current_col = tuple([chn[i, j] for chn in chans]) try: next(idx for idx in range(len(palette)) if palette[idx] == current_col) except StopIteration: idx = color_nb palette.append(current_col) color_nb = color_nb + 1 new_chn[i, j] = idx if self.fill_value is not None: if self.mode.endswith("A"): current_col = tuple(self.fill_value[:-1]) fill_alpha = [self.fill_value[-1]] else: current_col = tuple(self.fill_value) fill_alpha = [] try: next(idx for idx in range(len(palette)) if palette[idx] == current_col) except StopIteration: idx = color_nb palette.append(current_col) color_nb = color_nb + 1 self.fill_value = [idx] + fill_alpha new_chn.mask = selfmask self.palette = palette if alpha is None: self.channels = [new_chn] else: self.channels = [new_chn, alpha] self.mode = mode
python
def _to_p(self, mode): if self.mode.endswith("A"): chans = self.channels[:-1] alpha = self.channels[-1] self._secondary_mode = self.mode[:-1] else: chans = self.channels alpha = None self._secondary_mode = self.mode palette = [] selfmask = chans[0].mask for chn in chans[1:]: selfmask = np.ma.mask_or(selfmask, chn.mask) new_chn = np.ma.zeros(self.shape, dtype=int) color_nb = 0 for i in range(self.height): for j in range(self.width): current_col = tuple([chn[i, j] for chn in chans]) try: next(idx for idx in range(len(palette)) if palette[idx] == current_col) except StopIteration: idx = color_nb palette.append(current_col) color_nb = color_nb + 1 new_chn[i, j] = idx if self.fill_value is not None: if self.mode.endswith("A"): current_col = tuple(self.fill_value[:-1]) fill_alpha = [self.fill_value[-1]] else: current_col = tuple(self.fill_value) fill_alpha = [] try: next(idx for idx in range(len(palette)) if palette[idx] == current_col) except StopIteration: idx = color_nb palette.append(current_col) color_nb = color_nb + 1 self.fill_value = [idx] + fill_alpha new_chn.mask = selfmask self.palette = palette if alpha is None: self.channels = [new_chn] else: self.channels = [new_chn, alpha] self.mode = mode
[ "def", "_to_p", "(", "self", ",", "mode", ")", ":", "if", "self", ".", "mode", ".", "endswith", "(", "\"A\"", ")", ":", "chans", "=", "self", ".", "channels", "[", ":", "-", "1", "]", "alpha", "=", "self", ".", "channels", "[", "-", "1", "]", ...
Convert the image to P or PA mode.
[ "Convert", "the", "image", "to", "P", "or", "PA", "mode", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/image.py#L479-L537
15,935
pytroll/trollimage
trollimage/image.py
Image._from_p
def _from_p(self, mode): """Convert the image from P or PA mode. """ self._check_modes(("P", "PA")) if self.mode.endswith("A"): alpha = self.channels[-1] else: alpha = None chans = [] cdfs = [] color_chan = self.channels[0] for i in range(len(self.palette[0])): cdfs.append(np.zeros(len(self.palette))) for j in range(len(self.palette)): cdfs[i][j] = self.palette[j][i] new_chn = np.ma.array(np.interp(color_chan, np.arange(len(self.palette)), cdfs[i]), mask=color_chan.mask) chans.append(new_chn) if self.fill_value is not None: if alpha is not None: fill_alpha = self.fill_value[-1] self.fill_value = list(self.palette[int(self.fill_value[0])]) self.fill_value += [fill_alpha] else: self.fill_value = list(self.palette[int(self.fill_value[0])]) self.mode = self._secondary_mode self.channels = chans if alpha is not None: self.channels.append(alpha) self.mode = self.mode + "A" self.convert(mode)
python
def _from_p(self, mode): self._check_modes(("P", "PA")) if self.mode.endswith("A"): alpha = self.channels[-1] else: alpha = None chans = [] cdfs = [] color_chan = self.channels[0] for i in range(len(self.palette[0])): cdfs.append(np.zeros(len(self.palette))) for j in range(len(self.palette)): cdfs[i][j] = self.palette[j][i] new_chn = np.ma.array(np.interp(color_chan, np.arange(len(self.palette)), cdfs[i]), mask=color_chan.mask) chans.append(new_chn) if self.fill_value is not None: if alpha is not None: fill_alpha = self.fill_value[-1] self.fill_value = list(self.palette[int(self.fill_value[0])]) self.fill_value += [fill_alpha] else: self.fill_value = list(self.palette[int(self.fill_value[0])]) self.mode = self._secondary_mode self.channels = chans if alpha is not None: self.channels.append(alpha) self.mode = self.mode + "A" self.convert(mode)
[ "def", "_from_p", "(", "self", ",", "mode", ")", ":", "self", ".", "_check_modes", "(", "(", "\"P\"", ",", "\"PA\"", ")", ")", "if", "self", ".", "mode", ".", "endswith", "(", "\"A\"", ")", ":", "alpha", "=", "self", ".", "channels", "[", "-", "1...
Convert the image from P or PA mode.
[ "Convert", "the", "image", "from", "P", "or", "PA", "mode", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/image.py#L539-L579
15,936
pytroll/trollimage
trollimage/image.py
Image._rgb2l
def _rgb2l(self, mode): """Convert from RGB to monochrome L. """ self._check_modes(("RGB", "RGBA")) kb_ = 0.114 kr_ = 0.299 r__ = self.channels[0] g__ = self.channels[1] b__ = self.channels[2] y__ = kr_ * r__ + (1 - kr_ - kb_) * g__ + kb_ * b__ if self.fill_value is not None: self.fill_value = ([rgb2ycbcr(self.fill_value[0], self.fill_value[1], self.fill_value[2])[0]] + self.fill_value[3:]) self.channels = [y__] + self.channels[3:] self.mode = mode
python
def _rgb2l(self, mode): self._check_modes(("RGB", "RGBA")) kb_ = 0.114 kr_ = 0.299 r__ = self.channels[0] g__ = self.channels[1] b__ = self.channels[2] y__ = kr_ * r__ + (1 - kr_ - kb_) * g__ + kb_ * b__ if self.fill_value is not None: self.fill_value = ([rgb2ycbcr(self.fill_value[0], self.fill_value[1], self.fill_value[2])[0]] + self.fill_value[3:]) self.channels = [y__] + self.channels[3:] self.mode = mode
[ "def", "_rgb2l", "(", "self", ",", "mode", ")", ":", "self", ".", "_check_modes", "(", "(", "\"RGB\"", ",", "\"RGBA\"", ")", ")", "kb_", "=", "0.114", "kr_", "=", "0.299", "r__", "=", "self", ".", "channels", "[", "0", "]", "g__", "=", "self", "....
Convert from RGB to monochrome L.
[ "Convert", "from", "RGB", "to", "monochrome", "L", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/image.py#L603-L625
15,937
pytroll/trollimage
trollimage/image.py
Image._ycbcr2l
def _ycbcr2l(self, mode): """Convert from YCbCr to L. """ self._check_modes(("YCbCr", "YCbCrA")) self.channels = [self.channels[0]] + self.channels[3:] if self.fill_value is not None: self.fill_value = [self.fill_value[0]] + self.fill_value[3:] self.mode = mode
python
def _ycbcr2l(self, mode): self._check_modes(("YCbCr", "YCbCrA")) self.channels = [self.channels[0]] + self.channels[3:] if self.fill_value is not None: self.fill_value = [self.fill_value[0]] + self.fill_value[3:] self.mode = mode
[ "def", "_ycbcr2l", "(", "self", ",", "mode", ")", ":", "self", ".", "_check_modes", "(", "(", "\"YCbCr\"", ",", "\"YCbCrA\"", ")", ")", "self", ".", "channels", "=", "[", "self", ".", "channels", "[", "0", "]", "]", "+", "self", ".", "channels", "[...
Convert from YCbCr to L.
[ "Convert", "from", "YCbCr", "to", "L", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/image.py#L627-L635
15,938
pytroll/trollimage
trollimage/image.py
Image._l2ycbcr
def _l2ycbcr(self, mode): """Convert from L to YCbCr. """ self._check_modes(("L", "LA")) luma = self.channels[0] zeros = np.ma.zeros(luma.shape) zeros.mask = luma.mask self.channels = [luma, zeros, zeros] + self.channels[1:] if self.fill_value is not None: self.fill_value = [self.fill_value[0], 0, 0] + self.fill_value[1:] self.mode = mode
python
def _l2ycbcr(self, mode): self._check_modes(("L", "LA")) luma = self.channels[0] zeros = np.ma.zeros(luma.shape) zeros.mask = luma.mask self.channels = [luma, zeros, zeros] + self.channels[1:] if self.fill_value is not None: self.fill_value = [self.fill_value[0], 0, 0] + self.fill_value[1:] self.mode = mode
[ "def", "_l2ycbcr", "(", "self", ",", "mode", ")", ":", "self", ".", "_check_modes", "(", "(", "\"L\"", ",", "\"LA\"", ")", ")", "luma", "=", "self", ".", "channels", "[", "0", "]", "zeros", "=", "np", ".", "ma", ".", "zeros", "(", "luma", ".", ...
Convert from L to YCbCr.
[ "Convert", "from", "L", "to", "YCbCr", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/image.py#L637-L651
15,939
pytroll/trollimage
trollimage/colorspaces.py
rgb2xyz
def rgb2xyz(r__, g__, b__): """RGB to XYZ """ r2_ = r__ / 255.0 g2_ = g__ / 255.0 b2_ = b__ / 255.0 def f__(arr): """Forward """ return np.where(arr > 0.04045, ((arr + 0.055) / 1.055) ** 2.4, arr / 12.92) r2_ = f__(r2_) * 100 g2_ = f__(g2_) * 100 b2_ = f__(b2_) * 100 x__ = r2_ * 0.4124 + g2_ * 0.3576 + b2_ * 0.1805 y__ = r2_ * 0.2126 + g2_ * 0.7152 + b2_ * 0.0722 z__ = r2_ * 0.0193 + g2_ * 0.1192 + b2_ * 0.9505 return x__, y__, z__
python
def rgb2xyz(r__, g__, b__): r2_ = r__ / 255.0 g2_ = g__ / 255.0 b2_ = b__ / 255.0 def f__(arr): """Forward """ return np.where(arr > 0.04045, ((arr + 0.055) / 1.055) ** 2.4, arr / 12.92) r2_ = f__(r2_) * 100 g2_ = f__(g2_) * 100 b2_ = f__(b2_) * 100 x__ = r2_ * 0.4124 + g2_ * 0.3576 + b2_ * 0.1805 y__ = r2_ * 0.2126 + g2_ * 0.7152 + b2_ * 0.0722 z__ = r2_ * 0.0193 + g2_ * 0.1192 + b2_ * 0.9505 return x__, y__, z__
[ "def", "rgb2xyz", "(", "r__", ",", "g__", ",", "b__", ")", ":", "r2_", "=", "r__", "/", "255.0", "g2_", "=", "g__", "/", "255.0", "b2_", "=", "b__", "/", "255.0", "def", "f__", "(", "arr", ")", ":", "\"\"\"Forward\n \"\"\"", "return", "np", ...
RGB to XYZ
[ "RGB", "to", "XYZ" ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/colorspaces.py#L87-L111
15,940
pytroll/trollimage
trollimage/colorspaces.py
xyz2rgb
def xyz2rgb(x__, y__, z__): """XYZ colorspace to RGB """ x2_ = x__ / 100.0 y2_ = y__ / 100.0 z2_ = z__ / 100.0 r__ = x2_ * 3.2406 + y2_ * -1.5372 + z2_ * -0.4986 g__ = x2_ * -0.9689 + y2_ * 1.8758 + z2_ * 0.0415 b__ = x2_ * 0.0557 + y2_ * -0.2040 + z2_ * 1.0570 def finv(arr): """Inverse """ return np.where(arr > 0.0031308, 1.055 * (arr ** (1.0 / 2.4)) - 0.055, 12.92 * arr) return finv(r__) * 255, finv(g__) * 255, finv(b__) * 255
python
def xyz2rgb(x__, y__, z__): x2_ = x__ / 100.0 y2_ = y__ / 100.0 z2_ = z__ / 100.0 r__ = x2_ * 3.2406 + y2_ * -1.5372 + z2_ * -0.4986 g__ = x2_ * -0.9689 + y2_ * 1.8758 + z2_ * 0.0415 b__ = x2_ * 0.0557 + y2_ * -0.2040 + z2_ * 1.0570 def finv(arr): """Inverse """ return np.where(arr > 0.0031308, 1.055 * (arr ** (1.0 / 2.4)) - 0.055, 12.92 * arr) return finv(r__) * 255, finv(g__) * 255, finv(b__) * 255
[ "def", "xyz2rgb", "(", "x__", ",", "y__", ",", "z__", ")", ":", "x2_", "=", "x__", "/", "100.0", "y2_", "=", "y__", "/", "100.0", "z2_", "=", "z__", "/", "100.0", "r__", "=", "x2_", "*", "3.2406", "+", "y2_", "*", "-", "1.5372", "+", "z2_", "...
XYZ colorspace to RGB
[ "XYZ", "colorspace", "to", "RGB" ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/colorspaces.py#L113-L131
15,941
edx/django-user-tasks
user_tasks/tasks.py
purge_old_user_tasks
def purge_old_user_tasks(): """ Delete any UserTaskStatus and UserTaskArtifact records older than ``settings.USER_TASKS_MAX_AGE``. Intended to be run as a scheduled task. """ limit = now() - settings.USER_TASKS_MAX_AGE # UserTaskArtifacts will also be removed via deletion cascading UserTaskStatus.objects.filter(created__lt=limit).delete()
python
def purge_old_user_tasks(): limit = now() - settings.USER_TASKS_MAX_AGE # UserTaskArtifacts will also be removed via deletion cascading UserTaskStatus.objects.filter(created__lt=limit).delete()
[ "def", "purge_old_user_tasks", "(", ")", ":", "limit", "=", "now", "(", ")", "-", "settings", ".", "USER_TASKS_MAX_AGE", "# UserTaskArtifacts will also be removed via deletion cascading", "UserTaskStatus", ".", "objects", ".", "filter", "(", "created__lt", "=", "limit",...
Delete any UserTaskStatus and UserTaskArtifact records older than ``settings.USER_TASKS_MAX_AGE``. Intended to be run as a scheduled task.
[ "Delete", "any", "UserTaskStatus", "and", "UserTaskArtifact", "records", "older", "than", "settings", ".", "USER_TASKS_MAX_AGE", "." ]
6a9cf3821f4d8e202e6b48703e6a62e2a889adfb
https://github.com/edx/django-user-tasks/blob/6a9cf3821f4d8e202e6b48703e6a62e2a889adfb/user_tasks/tasks.py#L110-L118
15,942
pytroll/trollimage
trollimage/xrimage.py
color_interp
def color_interp(data): """Get the color interpretation for this image.""" from rasterio.enums import ColorInterp as ci modes = {'L': [ci.gray], 'LA': [ci.gray, ci.alpha], 'YCbCr': [ci.Y, ci.Cb, ci.Cr], 'YCbCrA': [ci.Y, ci.Cb, ci.Cr, ci.alpha]} try: mode = ''.join(data['bands'].values) return modes[mode] except KeyError: colors = {'R': ci.red, 'G': ci.green, 'B': ci.blue, 'A': ci.alpha, 'C': ci.cyan, 'M': ci.magenta, 'Y': ci.yellow, 'H': ci.hue, 'S': ci.saturation, 'L': ci.lightness, 'K': ci.black, } return [colors[band] for band in data['bands'].values]
python
def color_interp(data): from rasterio.enums import ColorInterp as ci modes = {'L': [ci.gray], 'LA': [ci.gray, ci.alpha], 'YCbCr': [ci.Y, ci.Cb, ci.Cr], 'YCbCrA': [ci.Y, ci.Cb, ci.Cr, ci.alpha]} try: mode = ''.join(data['bands'].values) return modes[mode] except KeyError: colors = {'R': ci.red, 'G': ci.green, 'B': ci.blue, 'A': ci.alpha, 'C': ci.cyan, 'M': ci.magenta, 'Y': ci.yellow, 'H': ci.hue, 'S': ci.saturation, 'L': ci.lightness, 'K': ci.black, } return [colors[band] for band in data['bands'].values]
[ "def", "color_interp", "(", "data", ")", ":", "from", "rasterio", ".", "enums", "import", "ColorInterp", "as", "ci", "modes", "=", "{", "'L'", ":", "[", "ci", ".", "gray", "]", ",", "'LA'", ":", "[", "ci", ".", "gray", ",", "ci", ".", "alpha", "]...
Get the color interpretation for this image.
[ "Get", "the", "color", "interpretation", "for", "this", "image", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L141-L165
15,943
pytroll/trollimage
trollimage/xrimage.py
XRImage._correct_dims
def _correct_dims(data): """Standardize dimensions to bands, y, and x.""" if not hasattr(data, 'dims'): raise TypeError("Data must have a 'dims' attribute.") # doesn't actually copy the data underneath # we don't want our operations to change the user's data data = data.copy() if 'y' not in data.dims or 'x' not in data.dims: if data.ndim != 2: raise ValueError("Data must have a 'y' and 'x' dimension") # rename dimensions so we can use them # don't rename 'x' or 'y' if they already exist if 'y' not in data.dims: # find a dimension that isn't 'x' old_dim = [d for d in data.dims if d != 'x'][0] data = data.rename({old_dim: 'y'}) if 'x' not in data.dims: # find a dimension that isn't 'y' old_dim = [d for d in data.dims if d != 'y'][0] data = data.rename({old_dim: 'x'}) if "bands" not in data.dims: if data.ndim <= 2: data = data.expand_dims('bands') data['bands'] = ['L'] else: raise ValueError("No 'bands' dimension provided.") return data
python
def _correct_dims(data): if not hasattr(data, 'dims'): raise TypeError("Data must have a 'dims' attribute.") # doesn't actually copy the data underneath # we don't want our operations to change the user's data data = data.copy() if 'y' not in data.dims or 'x' not in data.dims: if data.ndim != 2: raise ValueError("Data must have a 'y' and 'x' dimension") # rename dimensions so we can use them # don't rename 'x' or 'y' if they already exist if 'y' not in data.dims: # find a dimension that isn't 'x' old_dim = [d for d in data.dims if d != 'x'][0] data = data.rename({old_dim: 'y'}) if 'x' not in data.dims: # find a dimension that isn't 'y' old_dim = [d for d in data.dims if d != 'y'][0] data = data.rename({old_dim: 'x'}) if "bands" not in data.dims: if data.ndim <= 2: data = data.expand_dims('bands') data['bands'] = ['L'] else: raise ValueError("No 'bands' dimension provided.") return data
[ "def", "_correct_dims", "(", "data", ")", ":", "if", "not", "hasattr", "(", "data", ",", "'dims'", ")", ":", "raise", "TypeError", "(", "\"Data must have a 'dims' attribute.\"", ")", "# doesn't actually copy the data underneath", "# we don't want our operations to change th...
Standardize dimensions to bands, y, and x.
[ "Standardize", "dimensions", "to", "bands", "y", "and", "x", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L189-L220
15,944
pytroll/trollimage
trollimage/xrimage.py
XRImage._create_alpha
def _create_alpha(self, data, fill_value=None): """Create an alpha band DataArray object. If `fill_value` is provided and input data is an integer type then it is used to determine invalid "null" pixels instead of xarray's `isnull` and `notnull` methods. The returned array is 1 where data is valid, 0 where invalid. """ not_alpha = [b for b in data.coords['bands'].values if b != 'A'] null_mask = data.sel(bands=not_alpha) if np.issubdtype(data.dtype, np.integer) and fill_value is not None: null_mask = null_mask != fill_value else: null_mask = null_mask.notnull() # if any of the bands are valid, we don't want transparency null_mask = null_mask.any(dim='bands') null_mask = null_mask.expand_dims('bands') null_mask['bands'] = ['A'] # match data dtype return null_mask
python
def _create_alpha(self, data, fill_value=None): not_alpha = [b for b in data.coords['bands'].values if b != 'A'] null_mask = data.sel(bands=not_alpha) if np.issubdtype(data.dtype, np.integer) and fill_value is not None: null_mask = null_mask != fill_value else: null_mask = null_mask.notnull() # if any of the bands are valid, we don't want transparency null_mask = null_mask.any(dim='bands') null_mask = null_mask.expand_dims('bands') null_mask['bands'] = ['A'] # match data dtype return null_mask
[ "def", "_create_alpha", "(", "self", ",", "data", ",", "fill_value", "=", "None", ")", ":", "not_alpha", "=", "[", "b", "for", "b", "in", "data", ".", "coords", "[", "'bands'", "]", ".", "values", "if", "b", "!=", "'A'", "]", "null_mask", "=", "dat...
Create an alpha band DataArray object. If `fill_value` is provided and input data is an integer type then it is used to determine invalid "null" pixels instead of xarray's `isnull` and `notnull` methods. The returned array is 1 where data is valid, 0 where invalid.
[ "Create", "an", "alpha", "band", "DataArray", "object", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L424-L445
15,945
pytroll/trollimage
trollimage/xrimage.py
XRImage._add_alpha
def _add_alpha(self, data, alpha=None): """Create an alpha channel and concatenate it to the provided data. If ``data`` is an integer type then the alpha band will be scaled to use the smallest (min) value as fully transparent and the largest (max) value as fully opaque. For float types the alpha band spans 0 to 1. """ null_mask = alpha if alpha is not None else self._create_alpha(data) # if we are using integer data, then alpha needs to be min-int to max-int # otherwise for floats we want 0 to 1 if np.issubdtype(data.dtype, np.integer): # xarray sometimes upcasts this calculation, so cast again null_mask = self._scale_to_dtype(null_mask, data.dtype).astype(data.dtype) data = xr.concat([data, null_mask], dim="bands") return data
python
def _add_alpha(self, data, alpha=None): null_mask = alpha if alpha is not None else self._create_alpha(data) # if we are using integer data, then alpha needs to be min-int to max-int # otherwise for floats we want 0 to 1 if np.issubdtype(data.dtype, np.integer): # xarray sometimes upcasts this calculation, so cast again null_mask = self._scale_to_dtype(null_mask, data.dtype).astype(data.dtype) data = xr.concat([data, null_mask], dim="bands") return data
[ "def", "_add_alpha", "(", "self", ",", "data", ",", "alpha", "=", "None", ")", ":", "null_mask", "=", "alpha", "if", "alpha", "is", "not", "None", "else", "self", ".", "_create_alpha", "(", "data", ")", "# if we are using integer data, then alpha needs to be min...
Create an alpha channel and concatenate it to the provided data. If ``data`` is an integer type then the alpha band will be scaled to use the smallest (min) value as fully transparent and the largest (max) value as fully opaque. For float types the alpha band spans 0 to 1.
[ "Create", "an", "alpha", "channel", "and", "concatenate", "it", "to", "the", "provided", "data", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L447-L463
15,946
pytroll/trollimage
trollimage/xrimage.py
XRImage._scale_to_dtype
def _scale_to_dtype(self, data, dtype): """Scale provided data to dtype range assuming a 0-1 range. Float input data is assumed to be normalized to a 0 to 1 range. Integer input data is not scaled, only clipped. A float output type is not scaled since both outputs and inputs are assumed to be in the 0-1 range already. """ if np.issubdtype(dtype, np.integer): if np.issubdtype(data, np.integer): # preserve integer data type data = data.clip(np.iinfo(dtype).min, np.iinfo(dtype).max) else: # scale float data (assumed to be 0 to 1) to full integer space dinfo = np.iinfo(dtype) data = data.clip(0, 1) * (dinfo.max - dinfo.min) + dinfo.min data = data.round() return data
python
def _scale_to_dtype(self, data, dtype): if np.issubdtype(dtype, np.integer): if np.issubdtype(data, np.integer): # preserve integer data type data = data.clip(np.iinfo(dtype).min, np.iinfo(dtype).max) else: # scale float data (assumed to be 0 to 1) to full integer space dinfo = np.iinfo(dtype) data = data.clip(0, 1) * (dinfo.max - dinfo.min) + dinfo.min data = data.round() return data
[ "def", "_scale_to_dtype", "(", "self", ",", "data", ",", "dtype", ")", ":", "if", "np", ".", "issubdtype", "(", "dtype", ",", "np", ".", "integer", ")", ":", "if", "np", ".", "issubdtype", "(", "data", ",", "np", ".", "integer", ")", ":", "# preser...
Scale provided data to dtype range assuming a 0-1 range. Float input data is assumed to be normalized to a 0 to 1 range. Integer input data is not scaled, only clipped. A float output type is not scaled since both outputs and inputs are assumed to be in the 0-1 range already.
[ "Scale", "provided", "data", "to", "dtype", "range", "assuming", "a", "0", "-", "1", "range", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L465-L483
15,947
pytroll/trollimage
trollimage/xrimage.py
XRImage._from_p
def _from_p(self, mode): """Convert the image from P or PA to RGB or RGBA.""" self._check_modes(("P", "PA")) if not self.palette: raise RuntimeError("Can't convert palettized image, missing palette.") pal = np.array(self.palette) pal = da.from_array(pal, chunks=pal.shape) if pal.shape[1] == 4: # colormap's alpha overrides data alpha mode = "RGBA" alpha = None elif self.mode.endswith("A"): # add a new/fake 'bands' dimension to the end alpha = self.data.sel(bands="A").data[..., None] mode = mode + "A" if not mode.endswith("A") else mode else: alpha = None flat_indexes = self.data.sel(bands='P').data.ravel().astype('int64') dim_sizes = ((key, val) for key, val in self.data.sizes.items() if key != 'bands') dims, new_shape = zip(*dim_sizes) dims = dims + ('bands',) new_shape = new_shape + (pal.shape[1],) new_data = pal[flat_indexes].reshape(new_shape) coords = dict(self.data.coords) coords["bands"] = list(mode) if alpha is not None: new_arr = da.concatenate((new_data, alpha), axis=-1) data = xr.DataArray(new_arr, coords=coords, attrs=self.data.attrs, dims=dims) else: data = xr.DataArray(new_data, coords=coords, attrs=self.data.attrs, dims=dims) return data
python
def _from_p(self, mode): self._check_modes(("P", "PA")) if not self.palette: raise RuntimeError("Can't convert palettized image, missing palette.") pal = np.array(self.palette) pal = da.from_array(pal, chunks=pal.shape) if pal.shape[1] == 4: # colormap's alpha overrides data alpha mode = "RGBA" alpha = None elif self.mode.endswith("A"): # add a new/fake 'bands' dimension to the end alpha = self.data.sel(bands="A").data[..., None] mode = mode + "A" if not mode.endswith("A") else mode else: alpha = None flat_indexes = self.data.sel(bands='P').data.ravel().astype('int64') dim_sizes = ((key, val) for key, val in self.data.sizes.items() if key != 'bands') dims, new_shape = zip(*dim_sizes) dims = dims + ('bands',) new_shape = new_shape + (pal.shape[1],) new_data = pal[flat_indexes].reshape(new_shape) coords = dict(self.data.coords) coords["bands"] = list(mode) if alpha is not None: new_arr = da.concatenate((new_data, alpha), axis=-1) data = xr.DataArray(new_arr, coords=coords, attrs=self.data.attrs, dims=dims) else: data = xr.DataArray(new_data, coords=coords, attrs=self.data.attrs, dims=dims) return data
[ "def", "_from_p", "(", "self", ",", "mode", ")", ":", "self", ".", "_check_modes", "(", "(", "\"P\"", ",", "\"PA\"", ")", ")", "if", "not", "self", ".", "palette", ":", "raise", "RuntimeError", "(", "\"Can't convert palettized image, missing palette.\"", ")", ...
Convert the image from P or PA to RGB or RGBA.
[ "Convert", "the", "image", "from", "P", "or", "PA", "to", "RGB", "or", "RGBA", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L492-L527
15,948
pytroll/trollimage
trollimage/xrimage.py
XRImage._finalize
def _finalize(self, fill_value=None, dtype=np.uint8, keep_palette=False, cmap=None): """Wrapper around 'finalize' method for backwards compatibility.""" import warnings warnings.warn("'_finalize' is deprecated, use 'finalize' instead.", DeprecationWarning) return self.finalize(fill_value, dtype, keep_palette, cmap)
python
def _finalize(self, fill_value=None, dtype=np.uint8, keep_palette=False, cmap=None): import warnings warnings.warn("'_finalize' is deprecated, use 'finalize' instead.", DeprecationWarning) return self.finalize(fill_value, dtype, keep_palette, cmap)
[ "def", "_finalize", "(", "self", ",", "fill_value", "=", "None", ",", "dtype", "=", "np", ".", "uint8", ",", "keep_palette", "=", "False", ",", "cmap", "=", "None", ")", ":", "import", "warnings", "warnings", ".", "warn", "(", "\"'_finalize' is deprecated,...
Wrapper around 'finalize' method for backwards compatibility.
[ "Wrapper", "around", "finalize", "method", "for", "backwards", "compatibility", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L586-L591
15,949
pytroll/trollimage
trollimage/xrimage.py
XRImage.finalize
def finalize(self, fill_value=None, dtype=np.uint8, keep_palette=False, cmap=None): """Finalize the image to be written to an output file. This adds an alpha band or fills data with a fill_value (if specified). It also scales float data to the output range of the data type (0-255 for uint8, default). For integer input data this method assumes the data is already scaled to the proper desired range. It will still fill in invalid values and add an alpha band if needed. Integer input data's fill value is determined by a special ``_FillValue`` attribute in the ``DataArray`` ``.attrs`` dictionary. """ if keep_palette and not self.mode.startswith('P'): keep_palette = False if not keep_palette: if self.mode == "P": return self.convert("RGB").finalize(fill_value=fill_value, dtype=dtype, keep_palette=keep_palette, cmap=cmap) if self.mode == "PA": return self.convert("RGBA").finalize(fill_value=fill_value, dtype=dtype, keep_palette=keep_palette, cmap=cmap) if np.issubdtype(dtype, np.floating) and fill_value is None: logger.warning("Image with floats cannot be transparent, so " "setting fill_value to 0") fill_value = 0 final_data = self.data # if the data are integers then this fill value will be used to check for invalid values ifill = final_data.attrs.get('_FillValue') if np.issubdtype(final_data, np.integer) else None if not keep_palette: if fill_value is None and not self.mode.endswith('A'): # We don't have a fill value or an alpha, let's add an alpha alpha = self._create_alpha(final_data, fill_value=ifill) final_data = self._scale_to_dtype(final_data, dtype).astype(dtype) final_data = self._add_alpha(final_data, alpha=alpha) else: # scale float data to the proper dtype # this method doesn't cast yet so that we can keep track of NULL values final_data = self._scale_to_dtype(final_data, dtype) # Add fill_value after all other calculations have been done to # make sure it is not scaled for the data type if ifill is not None and fill_value is not None: # cast fill value to output type so we don't change data type fill_value = dtype(fill_value) # integer fields have special fill values final_data = final_data.where(final_data != ifill, dtype(fill_value)) elif fill_value is not None: final_data = final_data.fillna(dtype(fill_value)) final_data = final_data.astype(dtype) final_data.attrs = self.data.attrs return final_data, ''.join(final_data['bands'].values)
python
def finalize(self, fill_value=None, dtype=np.uint8, keep_palette=False, cmap=None): if keep_palette and not self.mode.startswith('P'): keep_palette = False if not keep_palette: if self.mode == "P": return self.convert("RGB").finalize(fill_value=fill_value, dtype=dtype, keep_palette=keep_palette, cmap=cmap) if self.mode == "PA": return self.convert("RGBA").finalize(fill_value=fill_value, dtype=dtype, keep_palette=keep_palette, cmap=cmap) if np.issubdtype(dtype, np.floating) and fill_value is None: logger.warning("Image with floats cannot be transparent, so " "setting fill_value to 0") fill_value = 0 final_data = self.data # if the data are integers then this fill value will be used to check for invalid values ifill = final_data.attrs.get('_FillValue') if np.issubdtype(final_data, np.integer) else None if not keep_palette: if fill_value is None and not self.mode.endswith('A'): # We don't have a fill value or an alpha, let's add an alpha alpha = self._create_alpha(final_data, fill_value=ifill) final_data = self._scale_to_dtype(final_data, dtype).astype(dtype) final_data = self._add_alpha(final_data, alpha=alpha) else: # scale float data to the proper dtype # this method doesn't cast yet so that we can keep track of NULL values final_data = self._scale_to_dtype(final_data, dtype) # Add fill_value after all other calculations have been done to # make sure it is not scaled for the data type if ifill is not None and fill_value is not None: # cast fill value to output type so we don't change data type fill_value = dtype(fill_value) # integer fields have special fill values final_data = final_data.where(final_data != ifill, dtype(fill_value)) elif fill_value is not None: final_data = final_data.fillna(dtype(fill_value)) final_data = final_data.astype(dtype) final_data.attrs = self.data.attrs return final_data, ''.join(final_data['bands'].values)
[ "def", "finalize", "(", "self", ",", "fill_value", "=", "None", ",", "dtype", "=", "np", ".", "uint8", ",", "keep_palette", "=", "False", ",", "cmap", "=", "None", ")", ":", "if", "keep_palette", "and", "not", "self", ".", "mode", ".", "startswith", ...
Finalize the image to be written to an output file. This adds an alpha band or fills data with a fill_value (if specified). It also scales float data to the output range of the data type (0-255 for uint8, default). For integer input data this method assumes the data is already scaled to the proper desired range. It will still fill in invalid values and add an alpha band if needed. Integer input data's fill value is determined by a special ``_FillValue`` attribute in the ``DataArray`` ``.attrs`` dictionary.
[ "Finalize", "the", "image", "to", "be", "written", "to", "an", "output", "file", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L593-L646
15,950
pytroll/trollimage
trollimage/xrimage.py
XRImage.xrify_tuples
def xrify_tuples(self, tup): """Make xarray.DataArray from tuple.""" return xr.DataArray(tup, dims=['bands'], coords={'bands': self.data['bands']})
python
def xrify_tuples(self, tup): return xr.DataArray(tup, dims=['bands'], coords={'bands': self.data['bands']})
[ "def", "xrify_tuples", "(", "self", ",", "tup", ")", ":", "return", "xr", ".", "DataArray", "(", "tup", ",", "dims", "=", "[", "'bands'", "]", ",", "coords", "=", "{", "'bands'", ":", "self", ".", "data", "[", "'bands'", "]", "}", ")" ]
Make xarray.DataArray from tuple.
[ "Make", "xarray", ".", "DataArray", "from", "tuple", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L667-L671
15,951
pytroll/trollimage
trollimage/xrimage.py
XRImage.gamma
def gamma(self, gamma=1.0): """Apply gamma correction to the channels of the image. If *gamma* is a tuple, then it should have as many elements as the channels of the image, and the gamma correction is applied elementwise. If *gamma* is a number, the same gamma correction is applied on every channel, if there are several channels in the image. The behaviour of :func:`gamma` is undefined outside the normal [0,1] range of the channels. """ if isinstance(gamma, (list, tuple)): gamma = self.xrify_tuples(gamma) elif gamma == 1.0: return logger.debug("Applying gamma %s", str(gamma)) attrs = self.data.attrs self.data = self.data.clip(min=0) self.data **= 1.0 / gamma self.data.attrs = attrs
python
def gamma(self, gamma=1.0): if isinstance(gamma, (list, tuple)): gamma = self.xrify_tuples(gamma) elif gamma == 1.0: return logger.debug("Applying gamma %s", str(gamma)) attrs = self.data.attrs self.data = self.data.clip(min=0) self.data **= 1.0 / gamma self.data.attrs = attrs
[ "def", "gamma", "(", "self", ",", "gamma", "=", "1.0", ")", ":", "if", "isinstance", "(", "gamma", ",", "(", "list", ",", "tuple", ")", ")", ":", "gamma", "=", "self", ".", "xrify_tuples", "(", "gamma", ")", "elif", "gamma", "==", "1.0", ":", "re...
Apply gamma correction to the channels of the image. If *gamma* is a tuple, then it should have as many elements as the channels of the image, and the gamma correction is applied elementwise. If *gamma* is a number, the same gamma correction is applied on every channel, if there are several channels in the image. The behaviour of :func:`gamma` is undefined outside the normal [0,1] range of the channels.
[ "Apply", "gamma", "correction", "to", "the", "channels", "of", "the", "image", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L673-L692
15,952
pytroll/trollimage
trollimage/xrimage.py
XRImage.stretch
def stretch(self, stretch="crude", **kwargs): """Apply stretching to the current image. The value of *stretch* sets the type of stretching applied. The values "histogram", "linear", "crude" (or "crude-stretch") perform respectively histogram equalization, contrast stretching (with 5% cutoff on both sides), and contrast stretching without cutoff. The value "logarithmic" or "log" will do a logarithmic enhancement towards white. If a tuple or a list of two values is given as input, then a contrast stretching is performed with the values as cutoff. These values should be normalized in the range [0.0,1.0]. """ logger.debug("Applying stretch %s with parameters %s", stretch, str(kwargs)) # FIXME: do not apply stretch to alpha channel if isinstance(stretch, (tuple, list)): if len(stretch) == 2: self.stretch_linear(cutoffs=stretch) else: raise ValueError( "Stretch tuple must have exactly two elements") elif stretch == "linear": self.stretch_linear(**kwargs) elif stretch == "histogram": self.stretch_hist_equalize(**kwargs) elif stretch in ["crude", "crude-stretch"]: self.crude_stretch(**kwargs) elif stretch in ["log", "logarithmic"]: self.stretch_logarithmic(**kwargs) elif stretch == "no": return elif isinstance(stretch, str): raise ValueError("Stretching method %s not recognized." % stretch) else: raise TypeError("Stretch parameter must be a string or a tuple.")
python
def stretch(self, stretch="crude", **kwargs): logger.debug("Applying stretch %s with parameters %s", stretch, str(kwargs)) # FIXME: do not apply stretch to alpha channel if isinstance(stretch, (tuple, list)): if len(stretch) == 2: self.stretch_linear(cutoffs=stretch) else: raise ValueError( "Stretch tuple must have exactly two elements") elif stretch == "linear": self.stretch_linear(**kwargs) elif stretch == "histogram": self.stretch_hist_equalize(**kwargs) elif stretch in ["crude", "crude-stretch"]: self.crude_stretch(**kwargs) elif stretch in ["log", "logarithmic"]: self.stretch_logarithmic(**kwargs) elif stretch == "no": return elif isinstance(stretch, str): raise ValueError("Stretching method %s not recognized." % stretch) else: raise TypeError("Stretch parameter must be a string or a tuple.")
[ "def", "stretch", "(", "self", ",", "stretch", "=", "\"crude\"", ",", "*", "*", "kwargs", ")", ":", "logger", ".", "debug", "(", "\"Applying stretch %s with parameters %s\"", ",", "stretch", ",", "str", "(", "kwargs", ")", ")", "# FIXME: do not apply stretch to ...
Apply stretching to the current image. The value of *stretch* sets the type of stretching applied. The values "histogram", "linear", "crude" (or "crude-stretch") perform respectively histogram equalization, contrast stretching (with 5% cutoff on both sides), and contrast stretching without cutoff. The value "logarithmic" or "log" will do a logarithmic enhancement towards white. If a tuple or a list of two values is given as input, then a contrast stretching is performed with the values as cutoff. These values should be normalized in the range [0.0,1.0].
[ "Apply", "stretching", "to", "the", "current", "image", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L694-L730
15,953
pytroll/trollimage
trollimage/xrimage.py
XRImage._compute_quantile
def _compute_quantile(data, dims, cutoffs): """Helper method for stretch_linear. Dask delayed functions need to be non-internal functions (created inside a function) to be serializable on a multi-process scheduler. Quantile requires the data to be loaded since it not supported on dask arrays yet. """ # numpy doesn't get a 'quantile' function until 1.15 # for better backwards compatibility we use xarray's version data_arr = xr.DataArray(data, dims=dims) # delayed will provide us the fully computed xarray with ndarray left, right = data_arr.quantile([cutoffs[0], 1. - cutoffs[1]], dim=['x', 'y']) logger.debug("Interval: left=%s, right=%s", str(left), str(right)) return left.data, right.data
python
def _compute_quantile(data, dims, cutoffs): # numpy doesn't get a 'quantile' function until 1.15 # for better backwards compatibility we use xarray's version data_arr = xr.DataArray(data, dims=dims) # delayed will provide us the fully computed xarray with ndarray left, right = data_arr.quantile([cutoffs[0], 1. - cutoffs[1]], dim=['x', 'y']) logger.debug("Interval: left=%s, right=%s", str(left), str(right)) return left.data, right.data
[ "def", "_compute_quantile", "(", "data", ",", "dims", ",", "cutoffs", ")", ":", "# numpy doesn't get a 'quantile' function until 1.15", "# for better backwards compatibility we use xarray's version", "data_arr", "=", "xr", ".", "DataArray", "(", "data", ",", "dims", "=", ...
Helper method for stretch_linear. Dask delayed functions need to be non-internal functions (created inside a function) to be serializable on a multi-process scheduler. Quantile requires the data to be loaded since it not supported on dask arrays yet.
[ "Helper", "method", "for", "stretch_linear", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L733-L749
15,954
pytroll/trollimage
trollimage/xrimage.py
XRImage.stretch_linear
def stretch_linear(self, cutoffs=(0.005, 0.005)): """Stretch linearly the contrast of the current image. Use *cutoffs* for left and right trimming. """ logger.debug("Perform a linear contrast stretch.") logger.debug("Calculate the histogram quantiles: ") logger.debug("Left and right quantiles: " + str(cutoffs[0]) + " " + str(cutoffs[1])) cutoff_type = np.float64 # numpy percentile (which quantile calls) returns 64-bit floats # unless the value is a higher order float if np.issubdtype(self.data.dtype, np.floating) and \ np.dtype(self.data.dtype).itemsize > 8: cutoff_type = self.data.dtype left, right = dask.delayed(self._compute_quantile, nout=2)(self.data.data, self.data.dims, cutoffs) left_data = da.from_delayed(left, shape=(self.data.sizes['bands'],), dtype=cutoff_type) left = xr.DataArray(left_data, dims=('bands',), coords={'bands': self.data['bands']}) right_data = da.from_delayed(right, shape=(self.data.sizes['bands'],), dtype=cutoff_type) right = xr.DataArray(right_data, dims=('bands',), coords={'bands': self.data['bands']}) self.crude_stretch(left, right)
python
def stretch_linear(self, cutoffs=(0.005, 0.005)): logger.debug("Perform a linear contrast stretch.") logger.debug("Calculate the histogram quantiles: ") logger.debug("Left and right quantiles: " + str(cutoffs[0]) + " " + str(cutoffs[1])) cutoff_type = np.float64 # numpy percentile (which quantile calls) returns 64-bit floats # unless the value is a higher order float if np.issubdtype(self.data.dtype, np.floating) and \ np.dtype(self.data.dtype).itemsize > 8: cutoff_type = self.data.dtype left, right = dask.delayed(self._compute_quantile, nout=2)(self.data.data, self.data.dims, cutoffs) left_data = da.from_delayed(left, shape=(self.data.sizes['bands'],), dtype=cutoff_type) left = xr.DataArray(left_data, dims=('bands',), coords={'bands': self.data['bands']}) right_data = da.from_delayed(right, shape=(self.data.sizes['bands'],), dtype=cutoff_type) right = xr.DataArray(right_data, dims=('bands',), coords={'bands': self.data['bands']}) self.crude_stretch(left, right)
[ "def", "stretch_linear", "(", "self", ",", "cutoffs", "=", "(", "0.005", ",", "0.005", ")", ")", ":", "logger", ".", "debug", "(", "\"Perform a linear contrast stretch.\"", ")", "logger", ".", "debug", "(", "\"Calculate the histogram quantiles: \"", ")", "logger",...
Stretch linearly the contrast of the current image. Use *cutoffs* for left and right trimming.
[ "Stretch", "linearly", "the", "contrast", "of", "the", "current", "image", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L751-L780
15,955
pytroll/trollimage
trollimage/xrimage.py
XRImage.crude_stretch
def crude_stretch(self, min_stretch=None, max_stretch=None): """Perform simple linear stretching. This is done without any cutoff on the current image and normalizes to the [0,1] range. """ if min_stretch is None: non_band_dims = tuple(x for x in self.data.dims if x != 'bands') min_stretch = self.data.min(dim=non_band_dims) if max_stretch is None: non_band_dims = tuple(x for x in self.data.dims if x != 'bands') max_stretch = self.data.max(dim=non_band_dims) if isinstance(min_stretch, (list, tuple)): min_stretch = self.xrify_tuples(min_stretch) if isinstance(max_stretch, (list, tuple)): max_stretch = self.xrify_tuples(max_stretch) delta = (max_stretch - min_stretch) if isinstance(delta, xr.DataArray): # fillna if delta is NaN scale_factor = (1.0 / delta).fillna(0) else: scale_factor = 1.0 / delta attrs = self.data.attrs self.data -= min_stretch self.data *= scale_factor self.data.attrs = attrs
python
def crude_stretch(self, min_stretch=None, max_stretch=None): if min_stretch is None: non_band_dims = tuple(x for x in self.data.dims if x != 'bands') min_stretch = self.data.min(dim=non_band_dims) if max_stretch is None: non_band_dims = tuple(x for x in self.data.dims if x != 'bands') max_stretch = self.data.max(dim=non_band_dims) if isinstance(min_stretch, (list, tuple)): min_stretch = self.xrify_tuples(min_stretch) if isinstance(max_stretch, (list, tuple)): max_stretch = self.xrify_tuples(max_stretch) delta = (max_stretch - min_stretch) if isinstance(delta, xr.DataArray): # fillna if delta is NaN scale_factor = (1.0 / delta).fillna(0) else: scale_factor = 1.0 / delta attrs = self.data.attrs self.data -= min_stretch self.data *= scale_factor self.data.attrs = attrs
[ "def", "crude_stretch", "(", "self", ",", "min_stretch", "=", "None", ",", "max_stretch", "=", "None", ")", ":", "if", "min_stretch", "is", "None", ":", "non_band_dims", "=", "tuple", "(", "x", "for", "x", "in", "self", ".", "data", ".", "dims", "if", ...
Perform simple linear stretching. This is done without any cutoff on the current image and normalizes to the [0,1] range.
[ "Perform", "simple", "linear", "stretching", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L782-L809
15,956
pytroll/trollimage
trollimage/xrimage.py
XRImage.stretch_hist_equalize
def stretch_hist_equalize(self, approximate=False): """Stretch the current image's colors through histogram equalization. Args: approximate (bool): Use a faster less-accurate percentile calculation. At the time of writing the dask version of `percentile` is not as accurate as the numpy version. This will likely change in the future. Current dask version 0.17. """ logger.info("Perform a histogram equalized contrast stretch.") nwidth = 2048. logger.debug("Make histogram bins having equal amount of data, " + "using numpy percentile function:") def _band_hist(band_data): cdf = da.arange(0., 1., 1. / nwidth, chunks=nwidth) if approximate: # need a 1D array flat_data = band_data.ravel() # replace with nanpercentile in the future, if available # dask < 0.17 returns all NaNs for this bins = da.percentile(flat_data[da.notnull(flat_data)], cdf * 100.) else: bins = dask.delayed(np.nanpercentile)(band_data, cdf * 100.) bins = da.from_delayed(bins, shape=(nwidth,), dtype=cdf.dtype) res = dask.delayed(np.interp)(band_data, bins, cdf) res = da.from_delayed(res, shape=band_data.shape, dtype=band_data.dtype) return res band_results = [] for band in self.data['bands'].values: if band == 'A': continue band_data = self.data.sel(bands=band) res = _band_hist(band_data.data) band_results.append(res) if 'A' in self.data.coords['bands'].values: band_results.append(self.data.sel(bands='A')) self.data.data = da.stack(band_results, axis=self.data.dims.index('bands'))
python
def stretch_hist_equalize(self, approximate=False): logger.info("Perform a histogram equalized contrast stretch.") nwidth = 2048. logger.debug("Make histogram bins having equal amount of data, " + "using numpy percentile function:") def _band_hist(band_data): cdf = da.arange(0., 1., 1. / nwidth, chunks=nwidth) if approximate: # need a 1D array flat_data = band_data.ravel() # replace with nanpercentile in the future, if available # dask < 0.17 returns all NaNs for this bins = da.percentile(flat_data[da.notnull(flat_data)], cdf * 100.) else: bins = dask.delayed(np.nanpercentile)(band_data, cdf * 100.) bins = da.from_delayed(bins, shape=(nwidth,), dtype=cdf.dtype) res = dask.delayed(np.interp)(band_data, bins, cdf) res = da.from_delayed(res, shape=band_data.shape, dtype=band_data.dtype) return res band_results = [] for band in self.data['bands'].values: if band == 'A': continue band_data = self.data.sel(bands=band) res = _band_hist(band_data.data) band_results.append(res) if 'A' in self.data.coords['bands'].values: band_results.append(self.data.sel(bands='A')) self.data.data = da.stack(band_results, axis=self.data.dims.index('bands'))
[ "def", "stretch_hist_equalize", "(", "self", ",", "approximate", "=", "False", ")", ":", "logger", ".", "info", "(", "\"Perform a histogram equalized contrast stretch.\"", ")", "nwidth", "=", "2048.", "logger", ".", "debug", "(", "\"Make histogram bins having equal amou...
Stretch the current image's colors through histogram equalization. Args: approximate (bool): Use a faster less-accurate percentile calculation. At the time of writing the dask version of `percentile` is not as accurate as the numpy version. This will likely change in the future. Current dask version 0.17.
[ "Stretch", "the", "current", "image", "s", "colors", "through", "histogram", "equalization", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L811-L856
15,957
pytroll/trollimage
trollimage/xrimage.py
XRImage.stretch_weber_fechner
def stretch_weber_fechner(self, k, s0): """Stretch according to the Weber-Fechner law. p = k.ln(S/S0) p is perception, S is the stimulus, S0 is the stimulus threshold (the highest unpercieved stimulus), and k is the factor. """ attrs = self.data.attrs self.data = k * xu.log(self.data / s0) self.data.attrs = attrs
python
def stretch_weber_fechner(self, k, s0): attrs = self.data.attrs self.data = k * xu.log(self.data / s0) self.data.attrs = attrs
[ "def", "stretch_weber_fechner", "(", "self", ",", "k", ",", "s0", ")", ":", "attrs", "=", "self", ".", "data", ".", "attrs", "self", ".", "data", "=", "k", "*", "xu", ".", "log", "(", "self", ".", "data", "/", "s0", ")", "self", ".", "data", "....
Stretch according to the Weber-Fechner law. p = k.ln(S/S0) p is perception, S is the stimulus, S0 is the stimulus threshold (the highest unpercieved stimulus), and k is the factor.
[ "Stretch", "according", "to", "the", "Weber", "-", "Fechner", "law", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L885-L894
15,958
pytroll/trollimage
trollimage/xrimage.py
XRImage.colorize
def colorize(self, colormap): """Colorize the current image using `colormap`. .. note:: Works only on "L" or "LA" images. """ if self.mode not in ("L", "LA"): raise ValueError("Image should be grayscale to colorize") if self.mode == "LA": alpha = self.data.sel(bands=['A']) else: alpha = None l_data = self.data.sel(bands=['L']) def _colorize(l_data, colormap): # 'l_data' is (1, rows, cols) # 'channels' will be a list of 3 (RGB) or 4 (RGBA) arrays channels = colormap.colorize(l_data) return np.concatenate(channels, axis=0) new_data = l_data.data.map_blocks(_colorize, colormap, chunks=(colormap.colors.shape[1],) + l_data.data.chunks[1:], dtype=np.float64) if colormap.colors.shape[1] == 4: mode = "RGBA" elif alpha is not None: new_data = da.concatenate([new_data, alpha.data], axis=0) mode = "RGBA" else: mode = "RGB" # copy the coordinates so we don't affect the original coords = dict(self.data.coords) coords['bands'] = list(mode) attrs = self.data.attrs dims = self.data.dims self.data = xr.DataArray(new_data, coords=coords, attrs=attrs, dims=dims)
python
def colorize(self, colormap): if self.mode not in ("L", "LA"): raise ValueError("Image should be grayscale to colorize") if self.mode == "LA": alpha = self.data.sel(bands=['A']) else: alpha = None l_data = self.data.sel(bands=['L']) def _colorize(l_data, colormap): # 'l_data' is (1, rows, cols) # 'channels' will be a list of 3 (RGB) or 4 (RGBA) arrays channels = colormap.colorize(l_data) return np.concatenate(channels, axis=0) new_data = l_data.data.map_blocks(_colorize, colormap, chunks=(colormap.colors.shape[1],) + l_data.data.chunks[1:], dtype=np.float64) if colormap.colors.shape[1] == 4: mode = "RGBA" elif alpha is not None: new_data = da.concatenate([new_data, alpha.data], axis=0) mode = "RGBA" else: mode = "RGB" # copy the coordinates so we don't affect the original coords = dict(self.data.coords) coords['bands'] = list(mode) attrs = self.data.attrs dims = self.data.dims self.data = xr.DataArray(new_data, coords=coords, attrs=attrs, dims=dims)
[ "def", "colorize", "(", "self", ",", "colormap", ")", ":", "if", "self", ".", "mode", "not", "in", "(", "\"L\"", ",", "\"LA\"", ")", ":", "raise", "ValueError", "(", "\"Image should be grayscale to colorize\"", ")", "if", "self", ".", "mode", "==", "\"LA\"...
Colorize the current image using `colormap`. .. note:: Works only on "L" or "LA" images.
[ "Colorize", "the", "current", "image", "using", "colormap", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L940-L982
15,959
pytroll/trollimage
trollimage/xrimage.py
XRImage.palettize
def palettize(self, colormap): """Palettize the current image using `colormap`. .. note:: Works only on "L" or "LA" images. """ if self.mode not in ("L", "LA"): raise ValueError("Image should be grayscale to colorize") l_data = self.data.sel(bands=['L']) def _palettize(data): # returns data and palette, only need data return colormap.palettize(data)[0] new_data = l_data.data.map_blocks(_palettize, dtype=l_data.dtype) self.palette = tuple(colormap.colors) if self.mode == "L": mode = "P" else: mode = "PA" new_data = da.concatenate([new_data, self.data.sel(bands=['A'])], axis=0) self.data.data = new_data self.data.coords['bands'] = list(mode)
python
def palettize(self, colormap): if self.mode not in ("L", "LA"): raise ValueError("Image should be grayscale to colorize") l_data = self.data.sel(bands=['L']) def _palettize(data): # returns data and palette, only need data return colormap.palettize(data)[0] new_data = l_data.data.map_blocks(_palettize, dtype=l_data.dtype) self.palette = tuple(colormap.colors) if self.mode == "L": mode = "P" else: mode = "PA" new_data = da.concatenate([new_data, self.data.sel(bands=['A'])], axis=0) self.data.data = new_data self.data.coords['bands'] = list(mode)
[ "def", "palettize", "(", "self", ",", "colormap", ")", ":", "if", "self", ".", "mode", "not", "in", "(", "\"L\"", ",", "\"LA\"", ")", ":", "raise", "ValueError", "(", "\"Image should be grayscale to colorize\"", ")", "l_data", "=", "self", ".", "data", "."...
Palettize the current image using `colormap`. .. note:: Works only on "L" or "LA" images.
[ "Palettize", "the", "current", "image", "using", "colormap", "." ]
d35a7665ad475ff230e457085523e21f2cd3f454
https://github.com/pytroll/trollimage/blob/d35a7665ad475ff230e457085523e21f2cd3f454/trollimage/xrimage.py#L984-L1012
15,960
srstevenson/xdg
src/xdg.py
_path_from_env
def _path_from_env(variable: str, default: Path) -> Path: """Read an environment variable as a path. The environment variable with the specified name is read, and its value returned as a path. If the environment variable is not set, or set to the empty string, the default value is returned. Parameters ---------- variable : str Name of the environment variable. default : Path Default value. Returns ------- Path Value from environment or default. """ # TODO(srstevenson): Use assignment expression in Python 3.8. value = os.environ.get(variable) if value: return Path(value) return default
python
def _path_from_env(variable: str, default: Path) -> Path: # TODO(srstevenson): Use assignment expression in Python 3.8. value = os.environ.get(variable) if value: return Path(value) return default
[ "def", "_path_from_env", "(", "variable", ":", "str", ",", "default", ":", "Path", ")", "->", "Path", ":", "# TODO(srstevenson): Use assignment expression in Python 3.8.", "value", "=", "os", ".", "environ", ".", "get", "(", "variable", ")", "if", "value", ":", ...
Read an environment variable as a path. The environment variable with the specified name is read, and its value returned as a path. If the environment variable is not set, or set to the empty string, the default value is returned. Parameters ---------- variable : str Name of the environment variable. default : Path Default value. Returns ------- Path Value from environment or default.
[ "Read", "an", "environment", "variable", "as", "a", "path", "." ]
ec570eee85d2750ac83e60eeb7e1ac592c8166a8
https://github.com/srstevenson/xdg/blob/ec570eee85d2750ac83e60eeb7e1ac592c8166a8/src/xdg.py#L51-L75
15,961
srstevenson/xdg
src/xdg.py
_paths_from_env
def _paths_from_env(variable: str, default: List[Path]) -> List[Path]: """Read an environment variable as a list of paths. The environment variable with the specified name is read, and its value split on colons and returned as a list of paths. If the environment variable is not set, or set to the empty string, the default value is returned. Parameters ---------- variable : str Name of the environment variable. default : List[Path] Default value. Returns ------- List[Path] Value from environment or default. """ # TODO(srstevenson): Use assignment expression in Python 3.8. value = os.environ.get(variable) if value: return [Path(path) for path in value.split(":")] return default
python
def _paths_from_env(variable: str, default: List[Path]) -> List[Path]: # TODO(srstevenson): Use assignment expression in Python 3.8. value = os.environ.get(variable) if value: return [Path(path) for path in value.split(":")] return default
[ "def", "_paths_from_env", "(", "variable", ":", "str", ",", "default", ":", "List", "[", "Path", "]", ")", "->", "List", "[", "Path", "]", ":", "# TODO(srstevenson): Use assignment expression in Python 3.8.", "value", "=", "os", ".", "environ", ".", "get", "("...
Read an environment variable as a list of paths. The environment variable with the specified name is read, and its value split on colons and returned as a list of paths. If the environment variable is not set, or set to the empty string, the default value is returned. Parameters ---------- variable : str Name of the environment variable. default : List[Path] Default value. Returns ------- List[Path] Value from environment or default.
[ "Read", "an", "environment", "variable", "as", "a", "list", "of", "paths", "." ]
ec570eee85d2750ac83e60eeb7e1ac592c8166a8
https://github.com/srstevenson/xdg/blob/ec570eee85d2750ac83e60eeb7e1ac592c8166a8/src/xdg.py#L78-L103
15,962
thesharp/htpasswd
htpasswd/basic.py
Basic.add
def add(self, user, password): """ Adds a user with password """ if self.__contains__(user): raise UserExists self.new_users[user] = self._encrypt_password(password) + "\n"
python
def add(self, user, password): if self.__contains__(user): raise UserExists self.new_users[user] = self._encrypt_password(password) + "\n"
[ "def", "add", "(", "self", ",", "user", ",", "password", ")", ":", "if", "self", ".", "__contains__", "(", "user", ")", ":", "raise", "UserExists", "self", ".", "new_users", "[", "user", "]", "=", "self", ".", "_encrypt_password", "(", "password", ")",...
Adds a user with password
[ "Adds", "a", "user", "with", "password" ]
8bf5cee0bd5362af586729f4c9cea8131eedd74f
https://github.com/thesharp/htpasswd/blob/8bf5cee0bd5362af586729f4c9cea8131eedd74f/htpasswd/basic.py#L70-L74
15,963
thesharp/htpasswd
htpasswd/basic.py
Basic.pop
def pop(self, user): """ Deletes a user """ if not self.__contains__(user): raise UserNotExists self.new_users.pop(user)
python
def pop(self, user): if not self.__contains__(user): raise UserNotExists self.new_users.pop(user)
[ "def", "pop", "(", "self", ",", "user", ")", ":", "if", "not", "self", ".", "__contains__", "(", "user", ")", ":", "raise", "UserNotExists", "self", ".", "new_users", ".", "pop", "(", "user", ")" ]
Deletes a user
[ "Deletes", "a", "user" ]
8bf5cee0bd5362af586729f4c9cea8131eedd74f
https://github.com/thesharp/htpasswd/blob/8bf5cee0bd5362af586729f4c9cea8131eedd74f/htpasswd/basic.py#L76-L80
15,964
thesharp/htpasswd
htpasswd/basic.py
Basic.change_password
def change_password(self, user, password): """ Changes user password """ if not self.__contains__(user): raise UserNotExists self.new_users[user] = self._encrypt_password(password) + "\n"
python
def change_password(self, user, password): if not self.__contains__(user): raise UserNotExists self.new_users[user] = self._encrypt_password(password) + "\n"
[ "def", "change_password", "(", "self", ",", "user", ",", "password", ")", ":", "if", "not", "self", ".", "__contains__", "(", "user", ")", ":", "raise", "UserNotExists", "self", ".", "new_users", "[", "user", "]", "=", "self", ".", "_encrypt_password", "...
Changes user password
[ "Changes", "user", "password" ]
8bf5cee0bd5362af586729f4c9cea8131eedd74f
https://github.com/thesharp/htpasswd/blob/8bf5cee0bd5362af586729f4c9cea8131eedd74f/htpasswd/basic.py#L82-L86
15,965
thesharp/htpasswd
htpasswd/basic.py
Basic._encrypt_password
def _encrypt_password(self, password): """encrypt the password for given mode """ if self.encryption_mode.lower() == 'crypt': return self._crypt_password(password) elif self.encryption_mode.lower() == 'md5': return self._md5_password(password) elif self.encryption_mode.lower() == 'md5-base': return self._md5_base_password(password) else: raise UnknownEncryptionMode(self.encryption_mode)
python
def _encrypt_password(self, password): if self.encryption_mode.lower() == 'crypt': return self._crypt_password(password) elif self.encryption_mode.lower() == 'md5': return self._md5_password(password) elif self.encryption_mode.lower() == 'md5-base': return self._md5_base_password(password) else: raise UnknownEncryptionMode(self.encryption_mode)
[ "def", "_encrypt_password", "(", "self", ",", "password", ")", ":", "if", "self", ".", "encryption_mode", ".", "lower", "(", ")", "==", "'crypt'", ":", "return", "self", ".", "_crypt_password", "(", "password", ")", "elif", "self", ".", "encryption_mode", ...
encrypt the password for given mode
[ "encrypt", "the", "password", "for", "given", "mode" ]
8bf5cee0bd5362af586729f4c9cea8131eedd74f
https://github.com/thesharp/htpasswd/blob/8bf5cee0bd5362af586729f4c9cea8131eedd74f/htpasswd/basic.py#L88-L97
15,966
thesharp/htpasswd
htpasswd/group.py
Group.add_user
def add_user(self, user, group): """ Adds user to a group """ if self.is_user_in(user, group): raise UserAlreadyInAGroup self.new_groups.add(group, user)
python
def add_user(self, user, group): if self.is_user_in(user, group): raise UserAlreadyInAGroup self.new_groups.add(group, user)
[ "def", "add_user", "(", "self", ",", "user", ",", "group", ")", ":", "if", "self", ".", "is_user_in", "(", "user", ",", "group", ")", ":", "raise", "UserAlreadyInAGroup", "self", ".", "new_groups", ".", "add", "(", "group", ",", "user", ")" ]
Adds user to a group
[ "Adds", "user", "to", "a", "group" ]
8bf5cee0bd5362af586729f4c9cea8131eedd74f
https://github.com/thesharp/htpasswd/blob/8bf5cee0bd5362af586729f4c9cea8131eedd74f/htpasswd/group.py#L63-L67
15,967
thesharp/htpasswd
htpasswd/group.py
Group.delete_user
def delete_user(self, user, group): """ Deletes user from group """ if not self.__contains__(group): raise GroupNotExists if not self.is_user_in(user, group): raise UserNotInAGroup self.new_groups.popvalue(group, user)
python
def delete_user(self, user, group): if not self.__contains__(group): raise GroupNotExists if not self.is_user_in(user, group): raise UserNotInAGroup self.new_groups.popvalue(group, user)
[ "def", "delete_user", "(", "self", ",", "user", ",", "group", ")", ":", "if", "not", "self", ".", "__contains__", "(", "group", ")", ":", "raise", "GroupNotExists", "if", "not", "self", ".", "is_user_in", "(", "user", ",", "group", ")", ":", "raise", ...
Deletes user from group
[ "Deletes", "user", "from", "group" ]
8bf5cee0bd5362af586729f4c9cea8131eedd74f
https://github.com/thesharp/htpasswd/blob/8bf5cee0bd5362af586729f4c9cea8131eedd74f/htpasswd/group.py#L69-L75
15,968
openstack/swauth
swauth/middleware.py
Swauth._get_concealed_token
def _get_concealed_token(self, token): """Returns hashed token to be used as object name in Swift. Tokens are stored in auth account but object names are visible in Swift logs. Object names are hashed from token. """ enc_key = "%s:%s:%s" % (HASH_PATH_PREFIX, token, HASH_PATH_SUFFIX) return sha512(enc_key).hexdigest()
python
def _get_concealed_token(self, token): enc_key = "%s:%s:%s" % (HASH_PATH_PREFIX, token, HASH_PATH_SUFFIX) return sha512(enc_key).hexdigest()
[ "def", "_get_concealed_token", "(", "self", ",", "token", ")", ":", "enc_key", "=", "\"%s:%s:%s\"", "%", "(", "HASH_PATH_PREFIX", ",", "token", ",", "HASH_PATH_SUFFIX", ")", "return", "sha512", "(", "enc_key", ")", ".", "hexdigest", "(", ")" ]
Returns hashed token to be used as object name in Swift. Tokens are stored in auth account but object names are visible in Swift logs. Object names are hashed from token.
[ "Returns", "hashed", "token", "to", "be", "used", "as", "object", "name", "in", "Swift", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/middleware.py#L296-L303
15,969
openstack/swauth
swauth/middleware.py
Swauth.authorize
def authorize(self, req): """Returns None if the request is authorized to continue or a standard WSGI response callable if not. """ try: version, account, container, obj = split_path(req.path, 1, 4, True) except ValueError: return HTTPNotFound(request=req) if not account or not account.startswith(self.reseller_prefix): return self.denied_response(req) user_groups = (req.remote_user or '').split(',') if '.reseller_admin' in user_groups and \ account != self.reseller_prefix and \ account[len(self.reseller_prefix)] != '.': req.environ['swift_owner'] = True return None if account in user_groups and \ (req.method not in ('DELETE', 'PUT') or container): # If the user is admin for the account and is not trying to do an # account DELETE or PUT... req.environ['swift_owner'] = True return None if (req.environ.get('swift_sync_key') and req.environ['swift_sync_key'] == req.headers.get('x-container-sync-key', None) and 'x-timestamp' in req.headers and (req.remote_addr in self.allowed_sync_hosts or get_remote_client(req) in self.allowed_sync_hosts)): return None referrers, groups = parse_acl(getattr(req, 'acl', None)) if referrer_allowed(req.referer, referrers): if obj or '.rlistings' in groups: return None return self.denied_response(req) if not req.remote_user: return self.denied_response(req) for user_group in user_groups: if user_group in groups: return None return self.denied_response(req)
python
def authorize(self, req): try: version, account, container, obj = split_path(req.path, 1, 4, True) except ValueError: return HTTPNotFound(request=req) if not account or not account.startswith(self.reseller_prefix): return self.denied_response(req) user_groups = (req.remote_user or '').split(',') if '.reseller_admin' in user_groups and \ account != self.reseller_prefix and \ account[len(self.reseller_prefix)] != '.': req.environ['swift_owner'] = True return None if account in user_groups and \ (req.method not in ('DELETE', 'PUT') or container): # If the user is admin for the account and is not trying to do an # account DELETE or PUT... req.environ['swift_owner'] = True return None if (req.environ.get('swift_sync_key') and req.environ['swift_sync_key'] == req.headers.get('x-container-sync-key', None) and 'x-timestamp' in req.headers and (req.remote_addr in self.allowed_sync_hosts or get_remote_client(req) in self.allowed_sync_hosts)): return None referrers, groups = parse_acl(getattr(req, 'acl', None)) if referrer_allowed(req.referer, referrers): if obj or '.rlistings' in groups: return None return self.denied_response(req) if not req.remote_user: return self.denied_response(req) for user_group in user_groups: if user_group in groups: return None return self.denied_response(req)
[ "def", "authorize", "(", "self", ",", "req", ")", ":", "try", ":", "version", ",", "account", ",", "container", ",", "obj", "=", "split_path", "(", "req", ".", "path", ",", "1", ",", "4", ",", "True", ")", "except", "ValueError", ":", "return", "HT...
Returns None if the request is authorized to continue or a standard WSGI response callable if not.
[ "Returns", "None", "if", "the", "request", "is", "authorized", "to", "continue", "or", "a", "standard", "WSGI", "response", "callable", "if", "not", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/middleware.py#L438-L477
15,970
openstack/swauth
swauth/middleware.py
Swauth.denied_response
def denied_response(self, req): """Returns a standard WSGI response callable with the status of 403 or 401 depending on whether the REMOTE_USER is set or not. """ if not hasattr(req, 'credentials_valid'): req.credentials_valid = None if req.remote_user or req.credentials_valid: return HTTPForbidden(request=req) else: return HTTPUnauthorized(request=req)
python
def denied_response(self, req): if not hasattr(req, 'credentials_valid'): req.credentials_valid = None if req.remote_user or req.credentials_valid: return HTTPForbidden(request=req) else: return HTTPUnauthorized(request=req)
[ "def", "denied_response", "(", "self", ",", "req", ")", ":", "if", "not", "hasattr", "(", "req", ",", "'credentials_valid'", ")", ":", "req", ".", "credentials_valid", "=", "None", "if", "req", ".", "remote_user", "or", "req", ".", "credentials_valid", ":"...
Returns a standard WSGI response callable with the status of 403 or 401 depending on whether the REMOTE_USER is set or not.
[ "Returns", "a", "standard", "WSGI", "response", "callable", "with", "the", "status", "of", "403", "or", "401", "depending", "on", "whether", "the", "REMOTE_USER", "is", "set", "or", "not", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/middleware.py#L479-L488
15,971
openstack/swauth
swauth/middleware.py
Swauth.is_user_reseller_admin
def is_user_reseller_admin(self, req, account, user): """Returns True if the user is a .reseller_admin. :param account: account user is part of :param user: the user :returns: True if user .reseller_admin, False if user is not a reseller_admin and None if the user doesn't exist. """ req.credentials_valid = True user_json = self.get_user_detail(req, account, user) if user_json is None: req.credentials_valid = False return False user_detail = json.loads(user_json) return '.reseller_admin' in (g['name'] for g in user_detail['groups'])
python
def is_user_reseller_admin(self, req, account, user): req.credentials_valid = True user_json = self.get_user_detail(req, account, user) if user_json is None: req.credentials_valid = False return False user_detail = json.loads(user_json) return '.reseller_admin' in (g['name'] for g in user_detail['groups'])
[ "def", "is_user_reseller_admin", "(", "self", ",", "req", ",", "account", ",", "user", ")", ":", "req", ".", "credentials_valid", "=", "True", "user_json", "=", "self", ".", "get_user_detail", "(", "req", ",", "account", ",", "user", ")", "if", "user_json"...
Returns True if the user is a .reseller_admin. :param account: account user is part of :param user: the user :returns: True if user .reseller_admin, False if user is not a reseller_admin and None if the user doesn't exist.
[ "Returns", "True", "if", "the", "user", "is", "a", ".", "reseller_admin", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/middleware.py#L1203-L1220
15,972
openstack/swauth
swauth/middleware.py
Swauth.get_itoken
def get_itoken(self, env): """Returns the current internal token to use for the auth system's own actions with other services. Each process will create its own itoken and the token will be deleted and recreated based on the token_life configuration value. The itoken information is stored in memcache because the auth process that is asked by Swift to validate the token may not be the same as the auth process that created the token. """ if not self.itoken or self.itoken_expires < time() or \ env.get('HTTP_X_AUTH_NEW_TOKEN', 'false').lower() in \ TRUE_VALUES: self.itoken = '%sitk%s' % (self.reseller_prefix, uuid4().hex) memcache_key = '%s/auth/%s' % (self.reseller_prefix, self.itoken) self.itoken_expires = time() + self.token_life memcache_client = cache_from_env(env) if not memcache_client: raise Exception( 'No memcache set up; required for Swauth middleware') memcache_client.set( memcache_key, (self.itoken_expires, '.auth,.reseller_admin,%s.auth' % self.reseller_prefix), time=self.token_life) return self.itoken
python
def get_itoken(self, env): if not self.itoken or self.itoken_expires < time() or \ env.get('HTTP_X_AUTH_NEW_TOKEN', 'false').lower() in \ TRUE_VALUES: self.itoken = '%sitk%s' % (self.reseller_prefix, uuid4().hex) memcache_key = '%s/auth/%s' % (self.reseller_prefix, self.itoken) self.itoken_expires = time() + self.token_life memcache_client = cache_from_env(env) if not memcache_client: raise Exception( 'No memcache set up; required for Swauth middleware') memcache_client.set( memcache_key, (self.itoken_expires, '.auth,.reseller_admin,%s.auth' % self.reseller_prefix), time=self.token_life) return self.itoken
[ "def", "get_itoken", "(", "self", ",", "env", ")", ":", "if", "not", "self", ".", "itoken", "or", "self", ".", "itoken_expires", "<", "time", "(", ")", "or", "env", ".", "get", "(", "'HTTP_X_AUTH_NEW_TOKEN'", ",", "'false'", ")", ".", "lower", "(", "...
Returns the current internal token to use for the auth system's own actions with other services. Each process will create its own itoken and the token will be deleted and recreated based on the token_life configuration value. The itoken information is stored in memcache because the auth process that is asked by Swift to validate the token may not be the same as the auth process that created the token.
[ "Returns", "the", "current", "internal", "token", "to", "use", "for", "the", "auth", "system", "s", "own", "actions", "with", "other", "services", ".", "Each", "process", "will", "create", "its", "own", "itoken", "and", "the", "token", "will", "be", "delet...
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/middleware.py#L1495-L1519
15,973
openstack/swauth
swauth/middleware.py
Swauth.get_admin_detail
def get_admin_detail(self, req): """Returns the dict for the user specified as the admin in the request with the addition of an `account` key set to the admin user's account. :param req: The swob request to retrieve X-Auth-Admin-User and X-Auth-Admin-Key from. :returns: The dict for the admin user with the addition of the `account` key. """ if ':' not in req.headers.get('x-auth-admin-user', ''): return None admin_account, admin_user = \ req.headers.get('x-auth-admin-user').split(':', 1) user_json = self.get_user_detail(req, admin_account, admin_user) if user_json is None: return None admin_detail = json.loads(user_json) admin_detail['account'] = admin_account return admin_detail
python
def get_admin_detail(self, req): if ':' not in req.headers.get('x-auth-admin-user', ''): return None admin_account, admin_user = \ req.headers.get('x-auth-admin-user').split(':', 1) user_json = self.get_user_detail(req, admin_account, admin_user) if user_json is None: return None admin_detail = json.loads(user_json) admin_detail['account'] = admin_account return admin_detail
[ "def", "get_admin_detail", "(", "self", ",", "req", ")", ":", "if", "':'", "not", "in", "req", ".", "headers", ".", "get", "(", "'x-auth-admin-user'", ",", "''", ")", ":", "return", "None", "admin_account", ",", "admin_user", "=", "req", ".", "headers", ...
Returns the dict for the user specified as the admin in the request with the addition of an `account` key set to the admin user's account. :param req: The swob request to retrieve X-Auth-Admin-User and X-Auth-Admin-Key from. :returns: The dict for the admin user with the addition of the `account` key.
[ "Returns", "the", "dict", "for", "the", "user", "specified", "as", "the", "admin", "in", "the", "request", "with", "the", "addition", "of", "an", "account", "key", "set", "to", "the", "admin", "user", "s", "account", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/middleware.py#L1521-L1539
15,974
openstack/swauth
swauth/middleware.py
Swauth.get_user_detail
def get_user_detail(self, req, account, user): """Returns the response body of a GET request for the specified user The body is in JSON format and contains all user information. :param req: The swob request :param account: the account the user is a member of :param user: the user :returns: A JSON response with the user detail information, None if the user doesn't exist """ path = quote('/v1/%s/%s/%s' % (self.auth_account, account, user)) resp = self.make_pre_authed_request( req.environ, 'GET', path).get_response(self.app) if resp.status_int == 404: return None if resp.status_int // 100 != 2: raise Exception('Could not get user object: %s %s' % (path, resp.status)) return resp.body
python
def get_user_detail(self, req, account, user): path = quote('/v1/%s/%s/%s' % (self.auth_account, account, user)) resp = self.make_pre_authed_request( req.environ, 'GET', path).get_response(self.app) if resp.status_int == 404: return None if resp.status_int // 100 != 2: raise Exception('Could not get user object: %s %s' % (path, resp.status)) return resp.body
[ "def", "get_user_detail", "(", "self", ",", "req", ",", "account", ",", "user", ")", ":", "path", "=", "quote", "(", "'/v1/%s/%s/%s'", "%", "(", "self", ".", "auth_account", ",", "account", ",", "user", ")", ")", "resp", "=", "self", ".", "make_pre_aut...
Returns the response body of a GET request for the specified user The body is in JSON format and contains all user information. :param req: The swob request :param account: the account the user is a member of :param user: the user :returns: A JSON response with the user detail information, None if the user doesn't exist
[ "Returns", "the", "response", "body", "of", "a", "GET", "request", "for", "the", "specified", "user", "The", "body", "is", "in", "JSON", "format", "and", "contains", "all", "user", "information", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/middleware.py#L1541-L1560
15,975
openstack/swauth
swauth/middleware.py
Swauth.credentials_match
def credentials_match(self, user_detail, key): """Returns True if the key is valid for the user_detail. It will use auth_encoder type the password was encoded with, to check for a key match. :param user_detail: The dict for the user. :param key: The key to validate for the user. :returns: True if the key is valid for the user, False if not. """ if user_detail: creds = user_detail.get('auth') try: auth_encoder, creds_dict = \ swauth.authtypes.validate_creds(creds) except ValueError as e: self.logger.error('%s' % e.args[0]) return False return user_detail and auth_encoder.match(key, creds, **creds_dict)
python
def credentials_match(self, user_detail, key): if user_detail: creds = user_detail.get('auth') try: auth_encoder, creds_dict = \ swauth.authtypes.validate_creds(creds) except ValueError as e: self.logger.error('%s' % e.args[0]) return False return user_detail and auth_encoder.match(key, creds, **creds_dict)
[ "def", "credentials_match", "(", "self", ",", "user_detail", ",", "key", ")", ":", "if", "user_detail", ":", "creds", "=", "user_detail", ".", "get", "(", "'auth'", ")", "try", ":", "auth_encoder", ",", "creds_dict", "=", "swauth", ".", "authtypes", ".", ...
Returns True if the key is valid for the user_detail. It will use auth_encoder type the password was encoded with, to check for a key match. :param user_detail: The dict for the user. :param key: The key to validate for the user. :returns: True if the key is valid for the user, False if not.
[ "Returns", "True", "if", "the", "key", "is", "valid", "for", "the", "user_detail", ".", "It", "will", "use", "auth_encoder", "type", "the", "password", "was", "encoded", "with", "to", "check", "for", "a", "key", "match", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/middleware.py#L1562-L1579
15,976
openstack/swauth
swauth/middleware.py
Swauth.is_user_changing_own_key
def is_user_changing_own_key(self, req, user): """Check if the user is changing his own key. :param req: The swob.Request to check. This contains x-auth-admin-user and x-auth-admin-key headers which are credentials of the user sending the request. :param user: User whose password is to be changed. :returns: True if user is changing his own key, False if not. """ admin_detail = self.get_admin_detail(req) if not admin_detail: # The user does not exist return False # If user is not admin/reseller_admin and x-auth-user-admin or # x-auth-user-reseller-admin headers are present in request, he may be # attempting to escalate himself as admin/reseller_admin! if '.admin' not in (g['name'] for g in admin_detail['groups']): if req.headers.get('x-auth-user-admin') == 'true' or \ req.headers.get('x-auth-user-reseller-admin') == 'true': return False if '.reseller_admin' not in \ (g['name'] for g in admin_detail['groups']) and \ req.headers.get('x-auth-user-reseller-admin') == 'true': return False return req.headers.get('x-auth-admin-user') == user and \ self.credentials_match(admin_detail, req.headers.get('x-auth-admin-key'))
python
def is_user_changing_own_key(self, req, user): admin_detail = self.get_admin_detail(req) if not admin_detail: # The user does not exist return False # If user is not admin/reseller_admin and x-auth-user-admin or # x-auth-user-reseller-admin headers are present in request, he may be # attempting to escalate himself as admin/reseller_admin! if '.admin' not in (g['name'] for g in admin_detail['groups']): if req.headers.get('x-auth-user-admin') == 'true' or \ req.headers.get('x-auth-user-reseller-admin') == 'true': return False if '.reseller_admin' not in \ (g['name'] for g in admin_detail['groups']) and \ req.headers.get('x-auth-user-reseller-admin') == 'true': return False return req.headers.get('x-auth-admin-user') == user and \ self.credentials_match(admin_detail, req.headers.get('x-auth-admin-key'))
[ "def", "is_user_changing_own_key", "(", "self", ",", "req", ",", "user", ")", ":", "admin_detail", "=", "self", ".", "get_admin_detail", "(", "req", ")", "if", "not", "admin_detail", ":", "# The user does not exist", "return", "False", "# If user is not admin/resell...
Check if the user is changing his own key. :param req: The swob.Request to check. This contains x-auth-admin-user and x-auth-admin-key headers which are credentials of the user sending the request. :param user: User whose password is to be changed. :returns: True if user is changing his own key, False if not.
[ "Check", "if", "the", "user", "is", "changing", "his", "own", "key", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/middleware.py#L1581-L1609
15,977
openstack/swauth
swauth/middleware.py
Swauth.is_super_admin
def is_super_admin(self, req): """Returns True if the admin specified in the request represents the .super_admin. :param req: The swob.Request to check. :param returns: True if .super_admin. """ return req.headers.get('x-auth-admin-user') == '.super_admin' and \ self.super_admin_key and \ req.headers.get('x-auth-admin-key') == self.super_admin_key
python
def is_super_admin(self, req): return req.headers.get('x-auth-admin-user') == '.super_admin' and \ self.super_admin_key and \ req.headers.get('x-auth-admin-key') == self.super_admin_key
[ "def", "is_super_admin", "(", "self", ",", "req", ")", ":", "return", "req", ".", "headers", ".", "get", "(", "'x-auth-admin-user'", ")", "==", "'.super_admin'", "and", "self", ".", "super_admin_key", "and", "req", ".", "headers", ".", "get", "(", "'x-auth...
Returns True if the admin specified in the request represents the .super_admin. :param req: The swob.Request to check. :param returns: True if .super_admin.
[ "Returns", "True", "if", "the", "admin", "specified", "in", "the", "request", "represents", "the", ".", "super_admin", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/middleware.py#L1611-L1620
15,978
openstack/swauth
swauth/middleware.py
Swauth.is_reseller_admin
def is_reseller_admin(self, req, admin_detail=None): """Returns True if the admin specified in the request represents a .reseller_admin. :param req: The swob.Request to check. :param admin_detail: The previously retrieved dict from :func:`get_admin_detail` or None for this function to retrieve the admin_detail itself. :param returns: True if .reseller_admin. """ req.credentials_valid = False if self.is_super_admin(req): return True if not admin_detail: admin_detail = self.get_admin_detail(req) if not self.credentials_match(admin_detail, req.headers.get('x-auth-admin-key')): return False req.credentials_valid = True return '.reseller_admin' in (g['name'] for g in admin_detail['groups'])
python
def is_reseller_admin(self, req, admin_detail=None): req.credentials_valid = False if self.is_super_admin(req): return True if not admin_detail: admin_detail = self.get_admin_detail(req) if not self.credentials_match(admin_detail, req.headers.get('x-auth-admin-key')): return False req.credentials_valid = True return '.reseller_admin' in (g['name'] for g in admin_detail['groups'])
[ "def", "is_reseller_admin", "(", "self", ",", "req", ",", "admin_detail", "=", "None", ")", ":", "req", ".", "credentials_valid", "=", "False", "if", "self", ".", "is_super_admin", "(", "req", ")", ":", "return", "True", "if", "not", "admin_detail", ":", ...
Returns True if the admin specified in the request represents a .reseller_admin. :param req: The swob.Request to check. :param admin_detail: The previously retrieved dict from :func:`get_admin_detail` or None for this function to retrieve the admin_detail itself. :param returns: True if .reseller_admin.
[ "Returns", "True", "if", "the", "admin", "specified", "in", "the", "request", "represents", "a", ".", "reseller_admin", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/middleware.py#L1622-L1641
15,979
openstack/swauth
swauth/authtypes.py
validate_creds
def validate_creds(creds): """Parse and validate user credentials whether format is right :param creds: User credentials :returns: Auth_type class instance and parsed user credentials in dict :raises ValueError: If credential format is wrong (eg: bad auth_type) """ try: auth_type, auth_rest = creds.split(':', 1) except ValueError: raise ValueError("Missing ':' in %s" % creds) authtypes = sys.modules[__name__] auth_encoder = getattr(authtypes, auth_type.title(), None) if auth_encoder is None: raise ValueError('Invalid auth_type: %s' % auth_type) auth_encoder = auth_encoder() parsed_creds = dict(type=auth_type, salt=None, hash=None) parsed_creds.update(auth_encoder.validate(auth_rest)) return auth_encoder, parsed_creds
python
def validate_creds(creds): try: auth_type, auth_rest = creds.split(':', 1) except ValueError: raise ValueError("Missing ':' in %s" % creds) authtypes = sys.modules[__name__] auth_encoder = getattr(authtypes, auth_type.title(), None) if auth_encoder is None: raise ValueError('Invalid auth_type: %s' % auth_type) auth_encoder = auth_encoder() parsed_creds = dict(type=auth_type, salt=None, hash=None) parsed_creds.update(auth_encoder.validate(auth_rest)) return auth_encoder, parsed_creds
[ "def", "validate_creds", "(", "creds", ")", ":", "try", ":", "auth_type", ",", "auth_rest", "=", "creds", ".", "split", "(", "':'", ",", "1", ")", "except", "ValueError", ":", "raise", "ValueError", "(", "\"Missing ':' in %s\"", "%", "creds", ")", "authtyp...
Parse and validate user credentials whether format is right :param creds: User credentials :returns: Auth_type class instance and parsed user credentials in dict :raises ValueError: If credential format is wrong (eg: bad auth_type)
[ "Parse", "and", "validate", "user", "credentials", "whether", "format", "is", "right" ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/authtypes.py#L42-L60
15,980
openstack/swauth
swauth/authtypes.py
Sha1.encode_w_salt
def encode_w_salt(self, salt, key): """Encodes a user key with salt into a particular format. The result of this method will be used internally. :param salt: Salt for hashing :param key: User's secret key :returns: A string representing user credentials """ enc_key = '%s%s' % (salt, key) enc_val = hashlib.sha1(enc_key).hexdigest() return "sha1:%s$%s" % (salt, enc_val)
python
def encode_w_salt(self, salt, key): enc_key = '%s%s' % (salt, key) enc_val = hashlib.sha1(enc_key).hexdigest() return "sha1:%s$%s" % (salt, enc_val)
[ "def", "encode_w_salt", "(", "self", ",", "salt", ",", "key", ")", ":", "enc_key", "=", "'%s%s'", "%", "(", "salt", ",", "key", ")", "enc_val", "=", "hashlib", ".", "sha1", "(", "enc_key", ")", ".", "hexdigest", "(", ")", "return", "\"sha1:%s$%s\"", ...
Encodes a user key with salt into a particular format. The result of this method will be used internally. :param salt: Salt for hashing :param key: User's secret key :returns: A string representing user credentials
[ "Encodes", "a", "user", "key", "with", "salt", "into", "a", "particular", "format", ".", "The", "result", "of", "this", "method", "will", "be", "used", "internally", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/authtypes.py#L112-L122
15,981
openstack/swauth
swauth/authtypes.py
Sha1.encode
def encode(self, key): """Encodes a user key into a particular format. The result of this method will be used by swauth for storing user credentials. If salt is not manually set in conf file, a random salt will be generated and used. :param key: User's secret key :returns: A string representing user credentials """ salt = self.salt or os.urandom(32).encode('base64').rstrip() return self.encode_w_salt(salt, key)
python
def encode(self, key): salt = self.salt or os.urandom(32).encode('base64').rstrip() return self.encode_w_salt(salt, key)
[ "def", "encode", "(", "self", ",", "key", ")", ":", "salt", "=", "self", ".", "salt", "or", "os", ".", "urandom", "(", "32", ")", ".", "encode", "(", "'base64'", ")", ".", "rstrip", "(", ")", "return", "self", ".", "encode_w_salt", "(", "salt", "...
Encodes a user key into a particular format. The result of this method will be used by swauth for storing user credentials. If salt is not manually set in conf file, a random salt will be generated and used. :param key: User's secret key :returns: A string representing user credentials
[ "Encodes", "a", "user", "key", "into", "a", "particular", "format", ".", "The", "result", "of", "this", "method", "will", "be", "used", "by", "swauth", "for", "storing", "user", "credentials", "." ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/authtypes.py#L124-L135
15,982
openstack/swauth
swauth/authtypes.py
Sha1.match
def match(self, key, creds, salt, **kwargs): """Checks whether the user-provided key matches the user's credentials :param key: User-supplied key :param creds: User's stored credentials :param salt: Salt for hashing :param kwargs: Extra keyword args for compatibility reason with other auth_type classes :returns: True if the supplied key is valid, False otherwise """ return self.encode_w_salt(salt, key) == creds
python
def match(self, key, creds, salt, **kwargs): return self.encode_w_salt(salt, key) == creds
[ "def", "match", "(", "self", ",", "key", ",", "creds", ",", "salt", ",", "*", "*", "kwargs", ")", ":", "return", "self", ".", "encode_w_salt", "(", "salt", ",", "key", ")", "==", "creds" ]
Checks whether the user-provided key matches the user's credentials :param key: User-supplied key :param creds: User's stored credentials :param salt: Salt for hashing :param kwargs: Extra keyword args for compatibility reason with other auth_type classes :returns: True if the supplied key is valid, False otherwise
[ "Checks", "whether", "the", "user", "-", "provided", "key", "matches", "the", "user", "s", "credentials" ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/authtypes.py#L137-L147
15,983
openstack/swauth
swauth/authtypes.py
Sha1.validate
def validate(self, auth_rest): """Validate user credentials whether format is right for Sha1 :param auth_rest: User credentials' part without auth_type :return: Dict with a hash and a salt part of user credentials :raises ValueError: If credentials' part doesn't contain delimiter between a salt and a hash. """ try: auth_salt, auth_hash = auth_rest.split('$') except ValueError: raise ValueError("Missing '$' in %s" % auth_rest) if len(auth_salt) == 0: raise ValueError("Salt must have non-zero length!") if len(auth_hash) != 40: raise ValueError("Hash must have 40 chars!") if not all(c in string.hexdigits for c in auth_hash): raise ValueError("Hash must be hexadecimal!") return dict(salt=auth_salt, hash=auth_hash)
python
def validate(self, auth_rest): try: auth_salt, auth_hash = auth_rest.split('$') except ValueError: raise ValueError("Missing '$' in %s" % auth_rest) if len(auth_salt) == 0: raise ValueError("Salt must have non-zero length!") if len(auth_hash) != 40: raise ValueError("Hash must have 40 chars!") if not all(c in string.hexdigits for c in auth_hash): raise ValueError("Hash must be hexadecimal!") return dict(salt=auth_salt, hash=auth_hash)
[ "def", "validate", "(", "self", ",", "auth_rest", ")", ":", "try", ":", "auth_salt", ",", "auth_hash", "=", "auth_rest", ".", "split", "(", "'$'", ")", "except", "ValueError", ":", "raise", "ValueError", "(", "\"Missing '$' in %s\"", "%", "auth_rest", ")", ...
Validate user credentials whether format is right for Sha1 :param auth_rest: User credentials' part without auth_type :return: Dict with a hash and a salt part of user credentials :raises ValueError: If credentials' part doesn't contain delimiter between a salt and a hash.
[ "Validate", "user", "credentials", "whether", "format", "is", "right", "for", "Sha1" ]
0c8eaf50a9e2b3317f3eba62f205546904bc6d74
https://github.com/openstack/swauth/blob/0c8eaf50a9e2b3317f3eba62f205546904bc6d74/swauth/authtypes.py#L149-L169
15,984
oemof/demandlib
demandlib/bdew.py
ElecSlp.get_profile
def get_profile(self, ann_el_demand_per_sector): """ Get the profiles for the given annual demand Parameters ---------- ann_el_demand_per_sector : dictionary Key: sector, value: annual value Returns ------- pandas.DataFrame : Table with all profiles """ return self.slp_frame.multiply(pd.Series( ann_el_demand_per_sector), axis=1).dropna(how='all', axis=1) * 4
python
def get_profile(self, ann_el_demand_per_sector): return self.slp_frame.multiply(pd.Series( ann_el_demand_per_sector), axis=1).dropna(how='all', axis=1) * 4
[ "def", "get_profile", "(", "self", ",", "ann_el_demand_per_sector", ")", ":", "return", "self", ".", "slp_frame", ".", "multiply", "(", "pd", ".", "Series", "(", "ann_el_demand_per_sector", ")", ",", "axis", "=", "1", ")", ".", "dropna", "(", "how", "=", ...
Get the profiles for the given annual demand Parameters ---------- ann_el_demand_per_sector : dictionary Key: sector, value: annual value Returns ------- pandas.DataFrame : Table with all profiles
[ "Get", "the", "profiles", "for", "the", "given", "annual", "demand" ]
4b62d60e05cb06eb2590f9c5655c2cdebf494080
https://github.com/oemof/demandlib/blob/4b62d60e05cb06eb2590f9c5655c2cdebf494080/demandlib/bdew.py#L118-L132
15,985
oemof/demandlib
demandlib/bdew.py
HeatBuilding.weighted_temperature
def weighted_temperature(self, how='geometric_series'): r""" A new temperature vector is generated containing a multi-day average temperature as needed in the load profile function. Parameters ---------- how : string string which type to return ("geometric_series" or "mean") Notes ----- Equation for the mathematical series of the average tempaerature [1]_: .. math:: T=\frac{T_{D}+0.5\cdot T_{D-1}+0.25\cdot T_{D-2}+ 0.125\cdot T_{D-3}}{1+0.5+0.25+0.125} with :math:`T_D` = Average temperature on the present day :math:`T_{D-i}` = Average temperature on the day - i References ---------- .. [1] `BDEW <https://www.avacon.de/cps/rde/xbcr/avacon/15-06-30_Leitfaden_Abwicklung_SLP_Gas.pdf>`_, BDEW Documentation for heat profiles. """ # calculate daily mean temperature temperature = self.df['temperature'].resample('D').mean().reindex( self.df.index).fillna(method='ffill').fillna(method='bfill') if how == 'geometric_series': temperature_mean = (temperature + 0.5 * np.roll(temperature, 24) + 0.25 * np.roll(temperature, 48) + 0.125 * np.roll(temperature, 72)) / 1.875 elif how == 'mean': temperature_mean = temperature else: temperature_mean = None return temperature_mean
python
def weighted_temperature(self, how='geometric_series'): r""" A new temperature vector is generated containing a multi-day average temperature as needed in the load profile function. Parameters ---------- how : string string which type to return ("geometric_series" or "mean") Notes ----- Equation for the mathematical series of the average tempaerature [1]_: .. math:: T=\frac{T_{D}+0.5\cdot T_{D-1}+0.25\cdot T_{D-2}+ 0.125\cdot T_{D-3}}{1+0.5+0.25+0.125} with :math:`T_D` = Average temperature on the present day :math:`T_{D-i}` = Average temperature on the day - i References ---------- .. [1] `BDEW <https://www.avacon.de/cps/rde/xbcr/avacon/15-06-30_Leitfaden_Abwicklung_SLP_Gas.pdf>`_, BDEW Documentation for heat profiles. """ # calculate daily mean temperature temperature = self.df['temperature'].resample('D').mean().reindex( self.df.index).fillna(method='ffill').fillna(method='bfill') if how == 'geometric_series': temperature_mean = (temperature + 0.5 * np.roll(temperature, 24) + 0.25 * np.roll(temperature, 48) + 0.125 * np.roll(temperature, 72)) / 1.875 elif how == 'mean': temperature_mean = temperature else: temperature_mean = None return temperature_mean
[ "def", "weighted_temperature", "(", "self", ",", "how", "=", "'geometric_series'", ")", ":", "# calculate daily mean temperature", "temperature", "=", "self", ".", "df", "[", "'temperature'", "]", ".", "resample", "(", "'D'", ")", ".", "mean", "(", ")", ".", ...
r""" A new temperature vector is generated containing a multi-day average temperature as needed in the load profile function. Parameters ---------- how : string string which type to return ("geometric_series" or "mean") Notes ----- Equation for the mathematical series of the average tempaerature [1]_: .. math:: T=\frac{T_{D}+0.5\cdot T_{D-1}+0.25\cdot T_{D-2}+ 0.125\cdot T_{D-3}}{1+0.5+0.25+0.125} with :math:`T_D` = Average temperature on the present day :math:`T_{D-i}` = Average temperature on the day - i References ---------- .. [1] `BDEW <https://www.avacon.de/cps/rde/xbcr/avacon/15-06-30_Leitfaden_Abwicklung_SLP_Gas.pdf>`_, BDEW Documentation for heat profiles.
[ "r", "A", "new", "temperature", "vector", "is", "generated", "containing", "a", "multi", "-", "day", "average", "temperature", "as", "needed", "in", "the", "load", "profile", "function", "." ]
4b62d60e05cb06eb2590f9c5655c2cdebf494080
https://github.com/oemof/demandlib/blob/4b62d60e05cb06eb2590f9c5655c2cdebf494080/demandlib/bdew.py#L178-L219
15,986
oemof/demandlib
demandlib/bdew.py
HeatBuilding.get_temperature_interval
def get_temperature_interval(self): """Appoints the corresponding temperature interval to each temperature in the temperature vector. """ intervals = ({ -20: 1, -19: 1, -18: 1, -17: 1, -16: 1, -15: 1, -14: 2, -13: 2, -12: 2, -11: 2, -10: 2, -9: 3, -8: 3, -7: 3, -6: 3, -5: 3, -4: 4, -3: 4, -2: 4, -1: 4, 0: 4, 1: 5, 2: 5, 3: 5, 4: 5, 5: 5, 6: 6, 7: 6, 8: 6, 9: 6, 10: 6, 11: 7, 12: 7, 13: 7, 14: 7, 15: 7, 16: 8, 17: 8, 18: 8, 19: 8, 20: 8, 21: 9, 22: 9, 23: 9, 24: 9, 25: 9, 26: 10, 27: 10, 28: 10, 29: 10, 30: 10, 31: 10, 32: 10, 33: 10, 34: 10, 35: 10, 36: 10, 37: 10, 38: 10, 39: 10, 40: 10}) temperature_rounded = [ceil(i) for i in self.df['temperature_geo']] temperature_interval = [intervals[i] for i in temperature_rounded] return np.transpose(np.array(temperature_interval))
python
def get_temperature_interval(self): intervals = ({ -20: 1, -19: 1, -18: 1, -17: 1, -16: 1, -15: 1, -14: 2, -13: 2, -12: 2, -11: 2, -10: 2, -9: 3, -8: 3, -7: 3, -6: 3, -5: 3, -4: 4, -3: 4, -2: 4, -1: 4, 0: 4, 1: 5, 2: 5, 3: 5, 4: 5, 5: 5, 6: 6, 7: 6, 8: 6, 9: 6, 10: 6, 11: 7, 12: 7, 13: 7, 14: 7, 15: 7, 16: 8, 17: 8, 18: 8, 19: 8, 20: 8, 21: 9, 22: 9, 23: 9, 24: 9, 25: 9, 26: 10, 27: 10, 28: 10, 29: 10, 30: 10, 31: 10, 32: 10, 33: 10, 34: 10, 35: 10, 36: 10, 37: 10, 38: 10, 39: 10, 40: 10}) temperature_rounded = [ceil(i) for i in self.df['temperature_geo']] temperature_interval = [intervals[i] for i in temperature_rounded] return np.transpose(np.array(temperature_interval))
[ "def", "get_temperature_interval", "(", "self", ")", ":", "intervals", "=", "(", "{", "-", "20", ":", "1", ",", "-", "19", ":", "1", ",", "-", "18", ":", "1", ",", "-", "17", ":", "1", ",", "-", "16", ":", "1", ",", "-", "15", ":", "1", "...
Appoints the corresponding temperature interval to each temperature in the temperature vector.
[ "Appoints", "the", "corresponding", "temperature", "interval", "to", "each", "temperature", "in", "the", "temperature", "vector", "." ]
4b62d60e05cb06eb2590f9c5655c2cdebf494080
https://github.com/oemof/demandlib/blob/4b62d60e05cb06eb2590f9c5655c2cdebf494080/demandlib/bdew.py#L221-L238
15,987
oemof/demandlib
demandlib/bdew.py
HeatBuilding.get_sf_values
def get_sf_values(self, filename='shlp_hour_factors.csv'): """ Determine the h-values Parameters ---------- filename : string name of file where sigmoid factors are stored """ file = os.path.join(self.datapath, filename) hour_factors = pd.read_csv(file, index_col=0) hour_factors = hour_factors.query( 'building_class=={0} and shlp_type=="{1}"'.format( self.building_class, self.shlp_type)) # Join the two DataFrames on the columns 'hour' and 'hour_of_the_day' # or ['hour' 'weekday'] and ['hour_of_the_day', 'weekday'] if it is # not a residential slp. residential = self.building_class > 0 left_cols = ['hour_of_day'] + (['weekday'] if not residential else []) right_cols = ['hour'] + (['weekday'] if not residential else []) sf_mat = pd.DataFrame.merge( hour_factors, self.df, left_on=left_cols, right_on=right_cols, how='outer', left_index=True).sort_index() # drop unnecessary columns drop_cols = ( ['hour_of_day', 'hour', 'building_class', 'shlp_type', 'date', 'temperature'] + (['weekday_x'] if residential else []) + (['weekday_y'] if residential else []) + (['weekday'] if not residential else [])) sf_mat = sf_mat.drop(drop_cols, 1) # Determine the h values length = len(self.temperature) sf = (np.array(sf_mat)[np.array(list(range(0, length)))[:], (self.get_temperature_interval() - 1)[:]]) return np.array(list(map(float, sf[:])))
python
def get_sf_values(self, filename='shlp_hour_factors.csv'): file = os.path.join(self.datapath, filename) hour_factors = pd.read_csv(file, index_col=0) hour_factors = hour_factors.query( 'building_class=={0} and shlp_type=="{1}"'.format( self.building_class, self.shlp_type)) # Join the two DataFrames on the columns 'hour' and 'hour_of_the_day' # or ['hour' 'weekday'] and ['hour_of_the_day', 'weekday'] if it is # not a residential slp. residential = self.building_class > 0 left_cols = ['hour_of_day'] + (['weekday'] if not residential else []) right_cols = ['hour'] + (['weekday'] if not residential else []) sf_mat = pd.DataFrame.merge( hour_factors, self.df, left_on=left_cols, right_on=right_cols, how='outer', left_index=True).sort_index() # drop unnecessary columns drop_cols = ( ['hour_of_day', 'hour', 'building_class', 'shlp_type', 'date', 'temperature'] + (['weekday_x'] if residential else []) + (['weekday_y'] if residential else []) + (['weekday'] if not residential else [])) sf_mat = sf_mat.drop(drop_cols, 1) # Determine the h values length = len(self.temperature) sf = (np.array(sf_mat)[np.array(list(range(0, length)))[:], (self.get_temperature_interval() - 1)[:]]) return np.array(list(map(float, sf[:])))
[ "def", "get_sf_values", "(", "self", ",", "filename", "=", "'shlp_hour_factors.csv'", ")", ":", "file", "=", "os", ".", "path", ".", "join", "(", "self", ".", "datapath", ",", "filename", ")", "hour_factors", "=", "pd", ".", "read_csv", "(", "file", ",",...
Determine the h-values Parameters ---------- filename : string name of file where sigmoid factors are stored
[ "Determine", "the", "h", "-", "values" ]
4b62d60e05cb06eb2590f9c5655c2cdebf494080
https://github.com/oemof/demandlib/blob/4b62d60e05cb06eb2590f9c5655c2cdebf494080/demandlib/bdew.py#L240-L276
15,988
oemof/demandlib
demandlib/bdew.py
HeatBuilding.get_sigmoid_parameters
def get_sigmoid_parameters(self, filename='shlp_sigmoid_factors.csv'): """ Retrieve the sigmoid parameters from csv-files Parameters ---------- filename : string name of file where sigmoid factors are stored """ file = os.path.join(self.datapath, filename) sigmoid = pd.read_csv(file, index_col=0) sigmoid = sigmoid.query( 'building_class=={0} and '.format(self.building_class) + 'shlp_type=="{0}" and '.format(self.shlp_type) + 'wind_impact=={0}'.format(self.wind_class)) a = float(sigmoid['parameter_a']) b = float(sigmoid['parameter_b']) c = float(sigmoid['parameter_c']) if self.ww_incl: d = float(sigmoid['parameter_d']) else: d = 0 return a, b, c, d
python
def get_sigmoid_parameters(self, filename='shlp_sigmoid_factors.csv'): file = os.path.join(self.datapath, filename) sigmoid = pd.read_csv(file, index_col=0) sigmoid = sigmoid.query( 'building_class=={0} and '.format(self.building_class) + 'shlp_type=="{0}" and '.format(self.shlp_type) + 'wind_impact=={0}'.format(self.wind_class)) a = float(sigmoid['parameter_a']) b = float(sigmoid['parameter_b']) c = float(sigmoid['parameter_c']) if self.ww_incl: d = float(sigmoid['parameter_d']) else: d = 0 return a, b, c, d
[ "def", "get_sigmoid_parameters", "(", "self", ",", "filename", "=", "'shlp_sigmoid_factors.csv'", ")", ":", "file", "=", "os", ".", "path", ".", "join", "(", "self", ".", "datapath", ",", "filename", ")", "sigmoid", "=", "pd", ".", "read_csv", "(", "file",...
Retrieve the sigmoid parameters from csv-files Parameters ---------- filename : string name of file where sigmoid factors are stored
[ "Retrieve", "the", "sigmoid", "parameters", "from", "csv", "-", "files" ]
4b62d60e05cb06eb2590f9c5655c2cdebf494080
https://github.com/oemof/demandlib/blob/4b62d60e05cb06eb2590f9c5655c2cdebf494080/demandlib/bdew.py#L278-L301
15,989
oemof/demandlib
demandlib/bdew.py
HeatBuilding.get_weekday_parameters
def get_weekday_parameters(self, filename='shlp_weekday_factors.csv'): """ Retrieve the weekday parameter from csv-file Parameters ---------- filename : string name of file where sigmoid factors are stored """ file = os.path.join(self.datapath, filename) f_df = pd.read_csv(file, index_col=0) tmp_df = f_df.query('shlp_type=="{0}"'.format(self.shlp_type)).drop( 'shlp_type', axis=1) tmp_df['weekdays'] = np.array(list(range(7))) + 1 return np.array(list(map(float, pd.DataFrame.merge( tmp_df, self.df, left_on='weekdays', right_on='weekday', how='inner', left_index=True).sort_index()['wochentagsfaktor'])))
python
def get_weekday_parameters(self, filename='shlp_weekday_factors.csv'): file = os.path.join(self.datapath, filename) f_df = pd.read_csv(file, index_col=0) tmp_df = f_df.query('shlp_type=="{0}"'.format(self.shlp_type)).drop( 'shlp_type', axis=1) tmp_df['weekdays'] = np.array(list(range(7))) + 1 return np.array(list(map(float, pd.DataFrame.merge( tmp_df, self.df, left_on='weekdays', right_on='weekday', how='inner', left_index=True).sort_index()['wochentagsfaktor'])))
[ "def", "get_weekday_parameters", "(", "self", ",", "filename", "=", "'shlp_weekday_factors.csv'", ")", ":", "file", "=", "os", ".", "path", ".", "join", "(", "self", ".", "datapath", ",", "filename", ")", "f_df", "=", "pd", ".", "read_csv", "(", "file", ...
Retrieve the weekday parameter from csv-file Parameters ---------- filename : string name of file where sigmoid factors are stored
[ "Retrieve", "the", "weekday", "parameter", "from", "csv", "-", "file" ]
4b62d60e05cb06eb2590f9c5655c2cdebf494080
https://github.com/oemof/demandlib/blob/4b62d60e05cb06eb2590f9c5655c2cdebf494080/demandlib/bdew.py#L303-L321
15,990
oemof/demandlib
demandlib/bdew.py
HeatBuilding.get_normalized_bdew_profile
def get_normalized_bdew_profile(self): """ Calculation of the normalized hourly heat demand """ self.df['temperature'] = self.temperature.values self.df['temperature_geo'] = self.weighted_temperature( how='geometric_series') sf = self.get_sf_values() [a, b, c, d] = self.get_sigmoid_parameters() f = self.get_weekday_parameters() h = (a / (1 + (b / (self.df['temperature_geo'] - 40)) ** c) + d) kw = 1.0 / (sum(h * f) / 24) heat_profile_normalized = (kw * h * f * sf) return heat_profile_normalized
python
def get_normalized_bdew_profile(self): self.df['temperature'] = self.temperature.values self.df['temperature_geo'] = self.weighted_temperature( how='geometric_series') sf = self.get_sf_values() [a, b, c, d] = self.get_sigmoid_parameters() f = self.get_weekday_parameters() h = (a / (1 + (b / (self.df['temperature_geo'] - 40)) ** c) + d) kw = 1.0 / (sum(h * f) / 24) heat_profile_normalized = (kw * h * f * sf) return heat_profile_normalized
[ "def", "get_normalized_bdew_profile", "(", "self", ")", ":", "self", ".", "df", "[", "'temperature'", "]", "=", "self", ".", "temperature", ".", "values", "self", ".", "df", "[", "'temperature_geo'", "]", "=", "self", ".", "weighted_temperature", "(", "how",...
Calculation of the normalized hourly heat demand
[ "Calculation", "of", "the", "normalized", "hourly", "heat", "demand" ]
4b62d60e05cb06eb2590f9c5655c2cdebf494080
https://github.com/oemof/demandlib/blob/4b62d60e05cb06eb2590f9c5655c2cdebf494080/demandlib/bdew.py#L328-L344
15,991
chaosmail/python-fs
fs/fs.py
rename
def rename(oldPath, newPath, **kwargs): """rename the file oldPath to newPath""" import os return os.rename(oldPath, newPath, **kwargs)
python
def rename(oldPath, newPath, **kwargs): import os return os.rename(oldPath, newPath, **kwargs)
[ "def", "rename", "(", "oldPath", ",", "newPath", ",", "*", "*", "kwargs", ")", ":", "import", "os", "return", "os", ".", "rename", "(", "oldPath", ",", "newPath", ",", "*", "*", "kwargs", ")" ]
rename the file oldPath to newPath
[ "rename", "the", "file", "oldPath", "to", "newPath" ]
2567922ced9387e327e65f3244caff3b7af35684
https://github.com/chaosmail/python-fs/blob/2567922ced9387e327e65f3244caff3b7af35684/fs/fs.py#L38-L41
15,992
chaosmail/python-fs
fs/fs.py
chown
def chown(path, user=None, group=None): """change ownership of path""" import os import pwd import grp uid = pwd.getpwnam(user).pw_uid if user else -1 gid = grp.getgrnam(group).gr_gid if group else -1 return os.chown(path, uid, gid)
python
def chown(path, user=None, group=None): import os import pwd import grp uid = pwd.getpwnam(user).pw_uid if user else -1 gid = grp.getgrnam(group).gr_gid if group else -1 return os.chown(path, uid, gid)
[ "def", "chown", "(", "path", ",", "user", "=", "None", ",", "group", "=", "None", ")", ":", "import", "os", "import", "pwd", "import", "grp", "uid", "=", "pwd", ".", "getpwnam", "(", "user", ")", ".", "pw_uid", "if", "user", "else", "-", "1", "gi...
change ownership of path
[ "change", "ownership", "of", "path" ]
2567922ced9387e327e65f3244caff3b7af35684
https://github.com/chaosmail/python-fs/blob/2567922ced9387e327e65f3244caff3b7af35684/fs/fs.py#L59-L66
15,993
chaosmail/python-fs
fs/fs.py
chmod
def chmod(path, mode): """change pernmissions of path""" import os, stat st = os.stat(path) return os.chmod(path, mode)
python
def chmod(path, mode): import os, stat st = os.stat(path) return os.chmod(path, mode)
[ "def", "chmod", "(", "path", ",", "mode", ")", ":", "import", "os", ",", "stat", "st", "=", "os", ".", "stat", "(", "path", ")", "return", "os", ".", "chmod", "(", "path", ",", "mode", ")" ]
change pernmissions of path
[ "change", "pernmissions", "of", "path" ]
2567922ced9387e327e65f3244caff3b7af35684
https://github.com/chaosmail/python-fs/blob/2567922ced9387e327e65f3244caff3b7af35684/fs/fs.py#L68-L72
15,994
chaosmail/python-fs
fs/fs.py
exists
def exists(path, **kwargs): """Check if file or directory exists""" import os.path return os.path.exists(path, **kwargs)
python
def exists(path, **kwargs): import os.path return os.path.exists(path, **kwargs)
[ "def", "exists", "(", "path", ",", "*", "*", "kwargs", ")", ":", "import", "os", ".", "path", "return", "os", ".", "path", ".", "exists", "(", "path", ",", "*", "*", "kwargs", ")" ]
Check if file or directory exists
[ "Check", "if", "file", "or", "directory", "exists" ]
2567922ced9387e327e65f3244caff3b7af35684
https://github.com/chaosmail/python-fs/blob/2567922ced9387e327e65f3244caff3b7af35684/fs/fs.py#L160-L163
15,995
chaosmail/python-fs
fs/fs.py
get
def get(path): """Read an object from file""" try: import cPickle as pickle except: import pickle with open(path, 'rb') as file: return pickle.load(file)
python
def get(path): try: import cPickle as pickle except: import pickle with open(path, 'rb') as file: return pickle.load(file)
[ "def", "get", "(", "path", ")", ":", "try", ":", "import", "cPickle", "as", "pickle", "except", ":", "import", "pickle", "with", "open", "(", "path", ",", "'rb'", ")", "as", "file", ":", "return", "pickle", ".", "load", "(", "file", ")" ]
Read an object from file
[ "Read", "an", "object", "from", "file" ]
2567922ced9387e327e65f3244caff3b7af35684
https://github.com/chaosmail/python-fs/blob/2567922ced9387e327e65f3244caff3b7af35684/fs/fs.py#L251-L259
15,996
chaosmail/python-fs
fs/fs.py
put
def put(path, obj): """Write an object to file""" try: import cPickle as pickle except: import pickle with open(path, 'wb') as file: return pickle.dump(obj, file)
python
def put(path, obj): try: import cPickle as pickle except: import pickle with open(path, 'wb') as file: return pickle.dump(obj, file)
[ "def", "put", "(", "path", ",", "obj", ")", ":", "try", ":", "import", "cPickle", "as", "pickle", "except", ":", "import", "pickle", "with", "open", "(", "path", ",", "'wb'", ")", "as", "file", ":", "return", "pickle", ".", "dump", "(", "obj", ",",...
Write an object to file
[ "Write", "an", "object", "to", "file" ]
2567922ced9387e327e65f3244caff3b7af35684
https://github.com/chaosmail/python-fs/blob/2567922ced9387e327e65f3244caff3b7af35684/fs/fs.py#L261-L269
15,997
chaosmail/python-fs
fs/fs.py
join
def join(*args, **kwargs): """Join parts of a path together""" import os.path if _is_list(args[0]): return os.path.join(*args[0]) return os.path.join(*args, **kwargs)
python
def join(*args, **kwargs): import os.path if _is_list(args[0]): return os.path.join(*args[0]) return os.path.join(*args, **kwargs)
[ "def", "join", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "import", "os", ".", "path", "if", "_is_list", "(", "args", "[", "0", "]", ")", ":", "return", "os", ".", "path", ".", "join", "(", "*", "args", "[", "0", "]", ")", "return",...
Join parts of a path together
[ "Join", "parts", "of", "a", "path", "together" ]
2567922ced9387e327e65f3244caff3b7af35684
https://github.com/chaosmail/python-fs/blob/2567922ced9387e327e65f3244caff3b7af35684/fs/fs.py#L271-L276
15,998
oemof/demandlib
demandlib/tools.py
add_weekdays2df
def add_weekdays2df(time_df, holidays=None, holiday_is_sunday=False): r"""Giving back a DataFrame containing weekdays and optionally holidays for the given year. Parameters ---------- time_df : pandas DataFrame DataFrame to which the weekdays should be added Optional Parameters ------------------- holidays : array with information for every hour of the year, if holiday or not (0: holiday, 1: no holiday) holiday_is_sunday : boolean If set to True, all holidays (0) will be set to sundays (7). Returns ------- pandas.DataFrame : DataFrame with weekdays Notes ----- Using Pandas > 0.16 """ time_df['weekday'] = time_df.index.weekday + 1 time_df['date'] = time_df.index.date # Set weekday to Holiday (0) for all holidays if holidays is not None: if isinstance(holidays, dict): holidays = list(holidays.keys()) time_df['weekday'].mask(pd.to_datetime(time_df['date']).isin( pd.to_datetime(holidays)), 0, True) if holiday_is_sunday: time_df.weekday.mask(time_df.weekday == 0, 7, True) return time_df
python
def add_weekdays2df(time_df, holidays=None, holiday_is_sunday=False): r"""Giving back a DataFrame containing weekdays and optionally holidays for the given year. Parameters ---------- time_df : pandas DataFrame DataFrame to which the weekdays should be added Optional Parameters ------------------- holidays : array with information for every hour of the year, if holiday or not (0: holiday, 1: no holiday) holiday_is_sunday : boolean If set to True, all holidays (0) will be set to sundays (7). Returns ------- pandas.DataFrame : DataFrame with weekdays Notes ----- Using Pandas > 0.16 """ time_df['weekday'] = time_df.index.weekday + 1 time_df['date'] = time_df.index.date # Set weekday to Holiday (0) for all holidays if holidays is not None: if isinstance(holidays, dict): holidays = list(holidays.keys()) time_df['weekday'].mask(pd.to_datetime(time_df['date']).isin( pd.to_datetime(holidays)), 0, True) if holiday_is_sunday: time_df.weekday.mask(time_df.weekday == 0, 7, True) return time_df
[ "def", "add_weekdays2df", "(", "time_df", ",", "holidays", "=", "None", ",", "holiday_is_sunday", "=", "False", ")", ":", "time_df", "[", "'weekday'", "]", "=", "time_df", ".", "index", ".", "weekday", "+", "1", "time_df", "[", "'date'", "]", "=", "time_...
r"""Giving back a DataFrame containing weekdays and optionally holidays for the given year. Parameters ---------- time_df : pandas DataFrame DataFrame to which the weekdays should be added Optional Parameters ------------------- holidays : array with information for every hour of the year, if holiday or not (0: holiday, 1: no holiday) holiday_is_sunday : boolean If set to True, all holidays (0) will be set to sundays (7). Returns ------- pandas.DataFrame : DataFrame with weekdays Notes ----- Using Pandas > 0.16
[ "r", "Giving", "back", "a", "DataFrame", "containing", "weekdays", "and", "optionally", "holidays", "for", "the", "given", "year", "." ]
4b62d60e05cb06eb2590f9c5655c2cdebf494080
https://github.com/oemof/demandlib/blob/4b62d60e05cb06eb2590f9c5655c2cdebf494080/demandlib/tools.py#L6-L44
15,999
oemof/demandlib
demandlib/particular_profiles.py
IndustrialLoadProfile.simple_profile
def simple_profile(self, annual_demand, **kwargs): """ Create industrial load profile Parameters ---------- annual_demand : float Total demand. Other Parameters ---------------- am : datetime.time beginning of workday pm : datetime.time end of workday week : list list of weekdays weekend : list list of weekend days profile_factors : dictionary dictionary with scaling factors for night and day of weekdays and weekend days """ # Day(am to pm), night (pm to am), week day (week), # weekend day (weekend) am = kwargs.get('am', settime(7, 00, 0)) pm = kwargs.get('pm', settime(23, 30, 0)) week = kwargs.get('week', [1, 2, 3, 4, 5]) weekend = kwargs.get('weekend', [0, 6, 7]) default_factors = {'week': {'day': 0.8, 'night': 0.6}, 'weekend': {'day': 0.9, 'night': 0.7}} profile_factors = kwargs.get('profile_factors', default_factors) self.dataframe['ind'] = 0 self.dataframe['ind'].mask( self.dataframe['weekday'].between_time(am, pm).isin(week), profile_factors['week']['day'], True) self.dataframe['ind'].mask( self.dataframe['weekday'].between_time(pm, am).isin(week), profile_factors['week']['night'], True) self.dataframe['ind'].mask( self.dataframe['weekday'].between_time(am, pm).isin(weekend), profile_factors['weekend']['day'], True) self.dataframe['ind'].mask( self.dataframe['weekday'].between_time(pm, am).isin(weekend), profile_factors['weekend']['night'], True) if self.dataframe['ind'].isnull().any(axis=0): logging.error('NAN value found in industrial load profile') time_interval = self.dataframe.index.freq.nanos / 3.6e12 return (self.dataframe['ind'] / self.dataframe['ind'].sum() * annual_demand / time_interval)
python
def simple_profile(self, annual_demand, **kwargs): # Day(am to pm), night (pm to am), week day (week), # weekend day (weekend) am = kwargs.get('am', settime(7, 00, 0)) pm = kwargs.get('pm', settime(23, 30, 0)) week = kwargs.get('week', [1, 2, 3, 4, 5]) weekend = kwargs.get('weekend', [0, 6, 7]) default_factors = {'week': {'day': 0.8, 'night': 0.6}, 'weekend': {'day': 0.9, 'night': 0.7}} profile_factors = kwargs.get('profile_factors', default_factors) self.dataframe['ind'] = 0 self.dataframe['ind'].mask( self.dataframe['weekday'].between_time(am, pm).isin(week), profile_factors['week']['day'], True) self.dataframe['ind'].mask( self.dataframe['weekday'].between_time(pm, am).isin(week), profile_factors['week']['night'], True) self.dataframe['ind'].mask( self.dataframe['weekday'].between_time(am, pm).isin(weekend), profile_factors['weekend']['day'], True) self.dataframe['ind'].mask( self.dataframe['weekday'].between_time(pm, am).isin(weekend), profile_factors['weekend']['night'], True) if self.dataframe['ind'].isnull().any(axis=0): logging.error('NAN value found in industrial load profile') time_interval = self.dataframe.index.freq.nanos / 3.6e12 return (self.dataframe['ind'] / self.dataframe['ind'].sum() * annual_demand / time_interval)
[ "def", "simple_profile", "(", "self", ",", "annual_demand", ",", "*", "*", "kwargs", ")", ":", "# Day(am to pm), night (pm to am), week day (week),", "# weekend day (weekend)", "am", "=", "kwargs", ".", "get", "(", "'am'", ",", "settime", "(", "7", ",", "00", ",...
Create industrial load profile Parameters ---------- annual_demand : float Total demand. Other Parameters ---------------- am : datetime.time beginning of workday pm : datetime.time end of workday week : list list of weekdays weekend : list list of weekend days profile_factors : dictionary dictionary with scaling factors for night and day of weekdays and weekend days
[ "Create", "industrial", "load", "profile" ]
4b62d60e05cb06eb2590f9c5655c2cdebf494080
https://github.com/oemof/demandlib/blob/4b62d60e05cb06eb2590f9c5655c2cdebf494080/demandlib/particular_profiles.py#L20-L78