Why does “%-d”, or “%-e” remove the leading space or zero?

Python datetime.strftime() delegates to C strftime() function that is platform-dependent: The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime() function, and platform variations are common. To see the full set of format codes supported on your platform, consult the strftime(3) documentation. Glibc notes for strftime(3): – … Read more

How can I type hint an attribute in Python 3.5?

In Python 3.5, you have to write self.some_attribute = None # type: AnotherClass Since Python 3.6, new type hinting syntax was added for variables (PEP 526): self.some_attribute: AnotherClass = None This will probably make every type-checking system complain, because None is in fact not an instance of AnotherClass. Instead, you can use typing.Union[None, AnotherClass], or … Read more

How to validate uniqueness constraint across foreign key (django)

Methods are not called on their own when saving the model. One way to do this is to have a custom save method that calls the validate_unique method when a model is saved: class Room(models.Model): zone = models.ForeignKey(Zone) name = models.CharField(max_length=255) def validate_unique(self, exclude=None): qs = Room.objects.filter(name=self.name) if qs.filter(zone__site=self.zone__site).exists(): raise ValidationError(‘Name must be unique per … Read more

Python patch object with a side_effect

Use patch.object as a decorator or context manager, as in the following code: >>> class EmailChecker(): … def is_email_correct(self, email): … pass … >>> def my_side_effect(*args): … if args[0] == ‘1’: … return True … else: … return False … >>> with mock.patch.object(EmailChecker, ‘is_email_correct’, side_effect=my_side_effect): … checker = EmailChecker() … print(checker.is_email_correct(‘1’)) … print(checker.is_email_correct(‘2’)) … True … Read more

change scatter plot marker thickness

you are looking for the kwarg linewidths. e.g.: import matplotlib.pyplot as plt import numpy as np x = y = np.arange(5) fig,ax = plt.subplots(1) ax.scatter(x,y, s=100,marker=”x”,color=”b”,linewidths=1) ax.scatter(x,y+1,s=100,marker=”x”,color=”r”,linewidths=2) ax.scatter(x,y+2,s=100,marker=”x”,color=”g”,linewidths=3) plt.show() Note: On some versions of matplotlib, it appears the kwarg is linewidth, not linewidths, despite what the manual currently says (April 2020). This is a known … Read more

List comprehensions in Jinja

Since v2.7 there is selectattr: Filters a sequence of objects by applying a test to the specified attribute of each object, and only selecting the objects with the test succeeding. If no test is specified, the attribute’s value will be evaluated as a boolean. Example usage: {{ users|selectattr(“is_active”) }} {{ users|selectattr(“email”, “none”) }} Similar to … Read more

error code: 521