Skip to content

Instantly share code, notes, and snippets.

View anandtripathi5's full-sized avatar
🎯
Focusing

Anand Tripathi anandtripathi5

🎯
Focusing
View GitHub Profile
from flask import Flask
from extensions import socketio
def create_app():
app = Flask(__name__)
# other initializations and configurations
...
from flask_socketio import SocketIO
socketio = SocketIO(async_mode="gevent", cors_allowed_origins='some url')
@anandtripathi5
anandtripathi5 / one_way_to_do_it.py
Created February 16, 2022 23:09
Zen of python - There should be one - and preferably only one - obvious way to do it. Although that way may not be obvious at first unless you're Dutch.
# Using the % operator
proglang = "Python"
print('The Zen of %s' % proglang)
# Using the .format() method
my_str = 'The Zen of {}'
print(my_str.format(proglang))
# Using f-strings
print(f"The Zen of {proglang}")
@anandtripathi5
anandtripathi5 / unless_explicitly_silenced.py
Created February 16, 2022 22:55
Zen of python - Unless explicitly silenced
# Bad
capitals = dict(india='delhi', netherlands='amsterdam')
try:
captial = capitals['kenya']
except:
print("What is this error")
# Good
try:
@anandtripathi5
anandtripathi5 / error_should_never_pass_silently.py
Created February 16, 2022 21:43
Zen of python - error should never pass silently
# Bad
try:
mylist = get_items()
exception Exception as e:
pass
return mylist
# Good
import logging
@anandtripathi5
anandtripathi5 / sparse_better_than_dense.py
Last active February 16, 2022 10:24
Zen of python - Sparse is better than dense
if i>0: return sqrt(i)
elif i==0: return 0
else: return 1j * sqrt(-i)
# Versus
if i > 0:
return sqrt(i)
elif i == 0:
@anandtripathi5
anandtripathi5 / complex_is_better_than_complicated_solution.py
Created February 16, 2022 10:17
Zen of python - Complex is better than complicated
for i in xrange(5):
print i
@anandtripathi5
anandtripathi5 / complex_is_better_than_complicated.py
Created February 16, 2022 10:16
Zen of python - Complex is better than complicated
counter = 0
while counter < 5:
print counter
counter += 1
@anandtripathi5
anandtripathi5 / zen_of_python_3.py
Created February 13, 2022 23:56
Simple is better than complex
# c,a,b,d are some random variables
c = a and b or d
# or
if a:
c = b
else:
c = d
@anandtripathi5
anandtripathi5 / zen_of_python_2.py
Created February 13, 2022 23:47
Explicit is better than implicit
from .. import some_module
# or
from foo.bar import some_module
# another example
from foo.bar.some_module import *
# or
from foo.bar.some_module import specific_variable