Skip to content

Lovely snippets introduced to me by Brian Shaginaw

Notifications You must be signed in to change notification settings

beck/shaginaw-wats

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 

Repository files navigation

Shaginaw Wats

Lovely snippets introduced to me by Brian.

Python kebab-case (2020-04-28)

That == magic is 😈.

This one is Adam’s fault.

import collections


class keyed_defaultdict(collections.defaultdict):
    def __missing__(self, key):
        return self.default_factory(self, key)


class Kebabber:
    def __init__(self, owner, key):
        self._owner = owner
        self._key = key

    def __sub__(self, meat):
        return Kebabber(self._owner, f"{self._key}-{meat._key}")

    def __eq__(self, other):
        self._owner[self._key] = other


class KebabMeta(type):
    @classmethod
    def __prepare__(meta, class_name, supers):
        return keyed_defaultdict(Kebabber)


# ---

class Foo(metaclass=KebabMeta):
    kebab-case == 5


print(getattr(Foo, 'kebab-case'))

SQL (2020-02-28)

thonkings

psql=> select;
--
(1 row)
​
psql=> select where 1=0;
--
(0 rows)

smart

psql=> select from generate_series(1,1000);
--
(1000 rows)

Python (2020-02-27)

>>> class Whoa:
...   def __hash__(self):
...     return -1
...
>>> hash(Whoa())
-2
TIL
>>> hash(-1) == hash(-2)
True

Some lovely branching logic:
https://github.com/python/cpython/blob/58ac700fb09497df14d4492b6f820109490b2b88/Objects/typeobject.c#L6553-L6555

Stack Overflow

Python (2020-02-25)

fuuuuuuuuuuuuuuuuu

>>> -8 ** (1/3)
-2.0
>>> x = -8
>>> x ** (1/3)
(1.0000000000000002+1.7320508075688772j)

uuuuuuuck this

Python (2020-02-25)

ᖍ(∙⟞∙)ᖌ

>>> f"{True}"
'True'
>>> f"{'True':>5}"
' True'
>>> f"{True:>5}"
'    1'
TIL

!s and !r exist.

!s and !r are particularly tricky, because classes can hook into those and set their own behavior... which can be Bad

>>> class Foo(int):
...   def __repr__(self):
...     return 'hey'
...   def __str__(self):
...     return 'yo'
...
>>> f"{Foo():>5}"
'    0'
>>> f"{Foo()!s:>5}"
'   yo'
>>> f"{Foo()!r:>5}"
'  hey'
>>> f"{datetime.datetime.now():>5}"
'>5'
>>> f"{datetime.datetime.now():Today is %B %d, %Y}"
'Today is February 25, 2020'
>>> f"{datetime.datetime.now()!s:>30}"
'    2020-02-25 13:31:05.721174'
nothing to do here

Ruby (2020-07-14)

irb(main):005:0> def oh no
irb(main):006:1>   5
irb(main):007:1> end
=> :oh no
irb(main):008:0> oh no
=> 5

irb(main):009:0> def square root of num
irb(main):010:1>   num ** 0.5
irb(main):011:1> end
=> :square root of
irb(main):012:0> square root of 5
=> 2.23606797749979
irb(main):013:0>  6 = 5
=> 5
irb(main):014:0>  6 * 6
=> 25
irb(main):061:0* def def def def def def, def def def def
irb(main):062:1>   return def def def if def def def def == 0
irb(main):063:1>   return def def def def def-def def def def, def def def def if def def def > def def def def
irb(main):064:1>   return def def def def def, def def def def-def def def
irb(main):065:1> end
=> :def def
irb(main):066:0> def def 1280, 400
=> 80

About

Lovely snippets introduced to me by Brian Shaginaw

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published