treyhunner,
@treyhunner@mastodon.social avatar

What are your favorite #Python one liners?

bmispelon,
@bmispelon@mastodon.social avatar

@treyhunner One I came up with today:

print("".join(chr(127397 + ord(c.upper())) for c in country_code)) # where country_code is a 2-letter country code string, like FR, NO or US

jonny,
@jonny@neuromatch.social avatar

@treyhunner

p=print;print=lambda *a, **k:p(*[str(b).upper()+'!!!' for b in a], **k)
romanroe,
@romanroe@fosstodon.org avatar

@jonny @treyhunner

group by id:

{o.id: o for o in objs}  
jonny,
@jonny@neuromatch.social avatar

@romanroe @treyhunner what kinda objects do ya got with an id attribute?

ildave,
@ildave@mastodon.social avatar

@treyhunner i love any list comprehension.

Sevoris,

@treyhunner "TypeError: cannot (operation) type 'Timestamp' with type 'datetime64[ns]'"

iris,
@iris@neuromatch.social avatar

@treyhunner print("\n".join([s for s in dir(something) if substring in s])) in the interactive shell, for installations without ipython

iris,
@iris@neuromatch.social avatar

@treyhunner relatedly: locals()

treyhunner,
@treyhunner@mastodon.social avatar

@iris Great ones!

Here's an even shorter equivalent to locals():

vars()

Relatedly: dir() (for just the names)

Also vars(an_object) or dir(an_object).

Since vars(an_object) only looks at dict they're a bit different. Sometimes I'll do vars(an_object).keys() to see just the attributes that live directly on an object.

siddhantgoel,
@siddhantgoel@mastodon.social avatar

@treyhunner anything that uses functools.partial.

mariatta,
@mariatta@fosstodon.org avatar

@treyhunner breakpoint()

rochacbruno,
@rochacbruno@social.rochacbruno.com avatar

@treyhunner

import("ipdb").set_trace()

treyhunner,
@treyhunner@mastodon.social avatar

@rochacbruno nice one.

In case you're unaware of it:

export PYTHONBREAKPOINT="ipdb.set_trace"

https://kortina.nyc/notes/use-ipdb-as-the-default-python-debugger-with-breakpoint-and-pythonbreakpoint/

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • magazineikmin
  • rosin
  • Youngstown
  • mdbf
  • ngwrru68w68
  • slotface
  • khanakhh
  • ethstaker
  • everett
  • kavyap
  • thenastyranch
  • DreamBathrooms
  • tacticalgear
  • osvaldo12
  • JUstTest
  • InstantRegret
  • Durango
  • cisconetworking
  • modclub
  • cubers
  • GTA5RPClips
  • tester
  • normalnudes
  • Leos
  • provamag3
  • megavids
  • anitta
  • lostlight
  • All magazines