Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
How do I synchronously wait for an asyncio future?

I can't just use `loop.run_until_complete` because my code is being called from an asyncio loop. I can't just make my function async because the thing calling my code (neovim remote plugin host) doesn't work that way (because it supports pyuv so it can work on python 2).

I can't do `asyncio.new_event_loop().run_until_complete` because apparently you can't run a loop while another loop is running. (Why not???? I don't understand.)

I've thought of starting a new thread so I can run a new loop in that thread, but **there *must* be a better way**.

/r/Python
https://redd.it/88mxto
Is there something weird I don't understand with callbacks?

Hello,

I am learning python and did this quick exercise to familiarize myself with tkinter, lambdas, and callbacks. But it's behaving very weirdly. Here's the sample code:


def callback(col, val):

print("Hello: i=" + str(col) + ",j=" + str(val))

for i in range(10):

col = tkinter.Frame(frame)

for j in range(i):

button = tkinter.Button(col, height = 3, width = 5, text = str(j+1), command = lambda: callback(i, j))

button.pack(side = tkinter.TOP)

col.pack(side = tkinter.LEFT, anchor = tkinter.N, expand = False)

frame.pack()

top.mainloop()



The buttons get laid out properly and display the right thing. But when you click each one, rather than printing out the specific (i, j), they all print the same i and j (9 and 8) - as if they were all getting the same callback - the last callback defined. This is really surprising behavior, am I missing something?

Thank you!

/r/Python
https://redd.it/88ox3y
Play Tetris inside a Jupyter notebook today!
http://www.juliatetris.com

/r/IPython
https://redd.it/88p097
Im new to Python, and I have Komodo IDE

Ive been looking to get into Learning Languages, and was recommended Python as a starting point. The following will sound stupid but I cant move past it.

I have been using Komodo IDE to start on with Python and am Following along Classes and Tutorials etc, one of the first things is Arithmetics.

On inputting 4 + 4 I do not get any output, am I doing something wrong? using cmd py -3 then 4 + 4 I get 8 as I should.

My question is what am I doing wrong to get no Output in Komodo? I get outputs for print(' ') etc.

Sorry for the dull question, thanks in advance for any help.

EDIT: Thanks for all the help, I have figured it out thanks to help. Appreciate it all :)

/r/Python
https://redd.it/88qjmn
[AF] New to flask, every route is hitting the same function.

@app.route('/')
@app.route('/index')
@app.route('/login')
@app.route('/test')



def index():
print('hit index')
user = {'username': 'test'}
work.buildislandlist()
return render_template('index.html', user=user, island_list=work.island_list)

def login():
print('hit login')
form = LoginForm()
return render_template('login.html', title='Sign In?', form=form)

def test():
print("hit test")
return render_template('test.html')


So you can see I have those print statements in each function, and every time I hit /index, /login, /test on my browser, it only ever hits the index() function. Any ideas on why that may be?

/r/flask
https://redd.it/88rb2o
What's the coolest thing you did with Python?

Hello everyone.

I'm a beginner in Python and still doing a course on it. I just started Python just 2 months. I really want to motivate myself to continue learning Python, but I only learn the course on and off . I felt that only way to keep me going is be shown on how great and useful Python is. Yes, I do have a project I plan to do with the knowledge of Python, but just feel lazy sometimes (I really want to be very committed).I will really get inspired and motivated when I see the amazing things that Python can do and create. Please tell me your stories.
Also, you can mention how it was life changing.

Did it help make your job non related to programming easier?

Were your co-workers impressed by your skill and felt like they should learn too?

Did it help get you a job?
Thank you

/r/Python
https://redd.it/88w1ws
how to generate oauth2 credentials after successful google login

i have successfully configured everything to work with google oauth2.

i have used

>django-rest-framework-social-oauth2


the user gets registered if new in DB, the user get logged in if old

everything working fine

i want to generate a oauth2 credentials if the user successful logged in using google oauth

like this

>{


>"access_token": "jCvkP4l7jt88D2O1H1WnNC5wx56knj",


>"expires_in": 36000,


>"token_type": "Bearer",


>"scope": "read write",


>"refresh_token": "u51zWkv056BNMsug8Qw6KCgk5Qtyp"


>}

/r/djangolearning
https://redd.it/88yf73
How do I run a flask app without a virtual environment?

I'm following a flask lesson, and they have me using a virtual environment.

I run the flask app by doing the following:

>(venv) set FLASK_APP=main.py

>(venv) flask run

but trying the same while outside of the venv I get "flask is not recognized as an internal or external command.

What am I missing?

/r/flask
https://redd.it/88v4jm
how to get access_token of google oauth user?

i have used django-rest-framework-social-oauth2

 

**added this to templates**

>'social_django.context_processors.backends',

>'social_django.context_processors.login_redirect',

 

**added backends**

>'social_core.backends.open_id.OpenIdAuth',

>'social_core.backends.google.GoogleOpenId',

>'social_core.backends.google.GoogleOAuth2',

 

**google keys**

>SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = "***"

>SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = "***"

 

setup everything on the https://console.developers.google.com

 

when i access this link http://localhost:8000/auth/login/google-oauth2/ from browser

 

everything working fine my logged user details are register in DB if new, else login if old. then the social auth auto redirects to this link http://localhost:8000/accounts/profile/# which is empty.

 

i need the access token of the google user so that i can convert the token to oauth token in this link http://localhost:8000/auth/convert-token with my site client id & secret i created.

 

after that i can use the converted token to grant access to my API request

/r/django
https://redd.it/88xvk6
Django Deployment: Where did the Training Wheels Go?

I have been learning django, and I wanted to "deploy" the resume website I have been building to learn django.

Django documentation is clear and concise, and it never takes more than a little google-fu to figure out how to build my project, but there seems to be FAR LESS information about deploying a project.

How do I get this thing online? Do you have a tutorial that helped you?

/r/django
https://redd.it/88zrjv
Procedural dungeon generation in Python

If you are interested in procedural dungeon generation, [here](http://arcade.academy/examples/index.html#procedural-generation) are few examples written in Python.

* [Recursive division maze-generation](http://arcade.academy/examples/maze_recursive.html)
* [Depth first maze-generation](http://arcade.academy/examples/maze_depth_first.html)
* [Procedural Caves - Cellular Automata](http://arcade.academy/examples/procedural_caves_cellular.html)
* [Procedural Caves - Binary Space Partitioning](http://arcade.academy/examples/procedural_caves_bsp.html)

Visualized using the Arcade library, but the maps are generated with a 2D array first, so it should be easy to visualize them using any system.

/r/Python
https://redd.it/88zans