def test_slugify(): assert slugify("a") == "a" assert slugify("1a") == "1a" assert slugify("1") == "1" assert slugify("a a") == "a_a" assert slugify("a", valid_variable_name=True) == "a" assert slugify("1a", valid_variable_name=True) == "Number_1a" assert slugify("1", valid_variable_name=True) == "Number_1" assert slugify("a", valid_variable_name=False) == "a" assert slugify("1a", valid_variable_name=False) == "1a" assert slugify("1", valid_variable_name=False) == "1"
def test_slugify(): assert slugify('a') == 'a' assert slugify('1a') == '1a' assert slugify('1') == '1' assert slugify('a a') == 'a_a' assert slugify('a', valid_variable_name=True) == 'a' assert slugify('1a', valid_variable_name=True) == 'Number_1a' assert slugify('1', valid_variable_name=True) == 'Number_1' assert slugify('a', valid_variable_name=False) == 'a' assert slugify('1a', valid_variable_name=False) == '1a' assert slugify('1', valid_variable_name=False) == '1'
def __delattr__(self, name): if name in self: del self[name] else: for k in self: if name == slugify(k, True): del self[k]
def __getattr__(self, name): if name in self: return self[name] else: for k in self: if name == slugify(k, True): return self[k]
def export(self, folder=None, name=None, format="hspy", save_std=False): """Save the data to a file. All the arguments are optional. Parameters ---------- folder : str or None The path to the folder where the file will be saved. If `None` the current folder is used by default. name : str or None The name of the file. If `None` the Components name followed by the Parameter `name` attributes will be used by default. If a file with the same name exists the name will be modified by appending a number to the file path. save_std : bool If True, also the standard deviation will be saved format: str The extension of any file format supported by HyperSpy, default hspy """ if format is None: format = "hspy" if name is None: name = self.component.name + '_' + self.name filename = incremental_filename(slugify(name) + '.' + format) if folder is not None: filename = os.path.join(folder, filename) self.as_signal().save(filename) if save_std is True: self.as_signal(field='std').save(append2pathname(filename, '_std'))
def export(self, folder=None, name=None, format=None, save_std=False): """Save the data to a file. All the arguments are optional. Parameters ---------- folder : str or None The path to the folder where the file will be saved. If `None` the current folder is used by default. name : str or None The name of the file. If `None` the Components name followed by the Parameter `name` attributes will be used by default. If a file with the same name exists the name will be modified by appending a number to the file path. save_std : bool If True, also the standard deviation will be saved """ if format is None: format = preferences.General.default_export_format if name is None: name = self.component.name + "_" + self.name filename = incremental_filename(slugify(name) + "." + format) if folder is not None: filename = os.path.join(folder, filename) self.as_signal().save(filename) if save_std is True: self.as_signal(field="std").save(append2pathname(filename, "_std"))
def test_components_class_change_invalid_name(self): m = self.model g1 = hs.model.components1D.Gaussian() m.append(g1) g1.name = "1, Test This!" assert (getattr(m.components, slugify(g1.name, valid_variable_name=True)) is g1)
def export(self, folder=None, name=None, format="hspy", save_std=False): """Save the data to a file. All the arguments are optional. Parameters ---------- folder : str or None The path to the folder where the file will be saved. If `None` the current folder is used by default. name : str or None The name of the file. If `None` the Components name followed by the Parameter `name` attributes will be used by default. If a file with the same name exists the name will be modified by appending a number to the file path. save_std : bool If True, also the standard deviation will be saved format: str The extension of any file format supported by HyperSpy, default hspy """ if format is None: format = "hspy" if name is None: name = self.component.name + '_' + self.name filename = incremental_filename(slugify(name) + '.' + format) if folder is not None: filename = os.path.join(folder, filename) self.as_signal().save(filename) if save_std is True: self.as_signal(field='std').save(append2pathname( filename, '_std'))
def _set_name(self, value): old_value = self._name if self.model: for component in self.model: if value == component.name: if not (component is self): raise ValueError("Another component already has " "the name " + str(value)) self._name = value setattr(self.model.components, slugify(value, valid_variable_name=True), self) self.model.components.__delattr__( slugify(old_value, valid_variable_name=True)) else: self._name = value self.trait_property_changed('name', old_value, self._name)
def _set_name(self, value): old_value = self._name if self.model: for component in self.model: if value == component.name: if not (component is self): raise ValueError( "Another component already has " "the name " + str(value)) self._name = value setattr(self.model.components, slugify( value, valid_variable_name=True), self) self.model.components.__delattr__( slugify(old_value, valid_variable_name=True)) else: self._name = value self.trait_property_changed('name', old_value, self._name)
def test_components_class_change_name_del_default(self): m = self.model g1 = hs.model.components.Gaussian() m.append(g1) invalid_name = "1, Test This!" g1.name = invalid_name g1.name = "test" getattr(m.components, slugify(invalid_name))
def test_components_class_change_name_del_default(self): m = self.model g1 = hs.model.components1D.Gaussian() m.append(g1) invalid_name = "1, Test This!" g1.name = invalid_name g1.name = "test" getattr(m.components, slugify(invalid_name))
def test_components_class_change_invalid_name(self): m = self.model g1 = hs.model.components.Gaussian() m.append(g1) g1.name = "1, Test This!" nt.assert_is( getattr(m.components, slugify(g1.name, valid_variable_name=True)), g1)
def test_components_class_change_name_del_default(self): m = self.model g1 = hs.model.components1D.Gaussian() m.append(g1) invalid_name = "1, Test This!" g1.name = invalid_name g1.name = "test" with pytest.raises(AttributeError): getattr(m.components, slugify(invalid_name))
def __setattr__(self, name, value): if name in self: self[name] = value else: for k in self: if name == slugify(k, True): self[k] = value else: self[name] = value
def test_components_class_change_name_del_default2(self): m = self.model g1 = hs.model.components1D.Gaussian() m.append(g1) invalid_name = "1, Test This!" g1.name = invalid_name g1.name = "test" with pytest.raises(AttributeError): getattr(m.components, slugify(invalid_name))
def backup(self, filename=None, on_count=True): """Backs-up the samfire results in a file Parameters ---------- filename: {str, None} the filename. If None, a default value of "backup\_"+signal_title is used. on_count: bool if True (default), only saves on the required count of steps """ if filename is None: title = self.model.signal.metadata.General.title filename = slugify('backup_' + title) # maybe add saving marker + strategies as well? if self.count % self.save_every == 0 or not on_count: self.model.save(filename, name='samfire_backup', overwrite=True) self.model.signal.models.remove('samfire_backup')
def backup(self, filename=None, on_count=True): """Backs-up the samfire results in a file Parameters ---------- filename: {str, None} the filename. If None, a default value of "backup_"+signal_title is used on_count: bool if True (default), only saves on the required count of steps """ if filename is None: title = self.model.signal.metadata.General.title filename = slugify('backup_' + title) # maybe add saving marker + strategies as well? if self.count % self.save_every == 0 or not on_count: self.model.save(filename, name='samfire_backup', overwrite=True) self.model.signal.models.remove('samfire_backup')
def test_slugify(): nose.tools.assert_equal(slugify('ab !@#_ ja &(]'), u'ab___ja')
def test_slugify(): assert slugify("a") == "a" assert slugify("1a") == "1a" assert slugify("1") == "1" assert slugify("a a") == "a_a" assert slugify(42) == "42" assert slugify(3.14159) == "314159" assert slugify("├── Node1") == "Node1" assert slugify("a", valid_variable_name=True) == "a" assert slugify("1a", valid_variable_name=True) == "Number_1a" assert slugify("1", valid_variable_name=True) == "Number_1" assert slugify("a", valid_variable_name=False) == "a" assert slugify("1a", valid_variable_name=False) == "1a" assert slugify("1", valid_variable_name=False) == "1"
def __dir__(self): return [slugify(k, True) for k in self]
def test_slugify(): nose.tools.assert_equal(slugify("ab !@#_ ja &(]"), "ab___ja")
def test_slugify(): assert slugify('ab !@#_ ja &(]') == 'ab___ja'