Skip to content

8th July 2022

Coding

Python

Python Snippets

  • New: Parse an RFC2822 date.

    Interesting to test the accepted format of RSS dates.

    >>> from email.utils import parsedate_to_datetime
    >>> datestr = 'Sun, 09 Mar 1997 13:45:00 -0500'
    >>> parsedate_to_datetime(datestr)
    datetime.datetime(1997, 3, 9, 13, 45, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))
    
  • New: Convert a datetime to RFC2822.

    Interesting as it's the accepted format of RSS dates.

    >>> import datetime
    >>> from email import utils
    >>> nowdt = datetime.datetime.now()
    >>> utils.format_datetime(nowdt)
    'Tue, 10 Feb 2020 10:06:53 -0000'
    
  • New: Encode url.

    import urllib.parse
    from pydantic import AnyHttpUrl
    
    def _normalize_url(url: str) -> AnyHttpUrl:
        """Encode url to make it compatible with AnyHttpUrl."""
        return typing.cast(
            AnyHttpUrl,
            urllib.parse.quote(url, ":/"),
        )
    

    The :/ is needed when you try to parse urls that have the protocol, otherwise https://www. gets transformed into https%3A//www..

Javascript

Javascript snippets

Science

Data Analysis

Recommender Systems