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
My first many to many join, why isn't membership being populated?

from flask import Flask
from flasksqlalchemy import SQLAlchemy

app = Flask(name)
app.config['SQLALCHEMY
DATABASEURI'] = 'sqlite:///project.db'
db = SQLAlchemy(app)


membership = db.Table('membership',
db.Column('person
id', db.Integer, db.ForeignKey('Person.id')),
db.Column('organisationid', db.Integer, db.ForeignKey('Organisation.id'))
)

class Person(db.Model):
tablename = 'Person'
id = db.Column(db.Integer, primary
key=True)
name = db.Column(db.String(255))
password = db.Column(db.String(255))
memberof = db.relationship('Organisation', secondary=membership, backref='members', viewonly=True)

class Organisation(db.Model):
tablename = 'Organisation'
id = db.Column(db.Integer, primary
key=True)
name = db.Column(db.String(255))
people = db.relationship('Person', secondary=membership, backref='organisation', viewonly=True)




/r/flask
https://redd.it/10bzpcl
Monday Daily Thread: Project ideas!

Comment any project ideas beginner or advanced in this thread for others to give a try! If you complete one make sure to reply to the comment with how you found it and attach some source code! If you're looking for project ideas, you might be interested in checking out Al Sweigart's, "The Big Book of Small Python Projects" which provides a list of projects and the code to make them work.

/r/Python
https://redd.it/10cz8rg
New python graphics library !

Hi everyone.
I would like to share with you my own python graphic library.

The library is called Raylib-Python-Ctypes (also known as raypyc). Raypyc is a binding for python of the great c graphics library Raylib.

What can Raypyc do?
Basically, every thing that Raylib can do, like: open window, draw simple shapes, drawing textures, drawing models, drawing text (with an optional way of loading and using fonts), using shaders and much more!!!

Why should you use Raypyc?
Well, Raypyc was design to be programmer friendly and beginner, friendly. also Raypyc is really simple, no fancy game engines stuff only basic needs for the programmer.
(And it’s 100% open source)

Why did I make Raypyc?
In general, Raypyc was a school project of mine, but as I saw, it’s potential I started to make it more like a professional graphic library.

Library source code and examples: https://github.com/sDos280/raylib-python-ctypes

/r/Python
https://redd.it/10cos5v
Django-react google ads api integration

Hello everyone,

I'have been trying to connect my drf-react application with google ads api. I tried to use django allauth and dj-rest-auth for this but was unable to do so. It would be a great help if anyone can provide me with some resources.

/r/django
https://redd.it/10d9zsn
Why response time with postgres is 15x higher than with sqlite?

Simple get request (django + django-rest-framework) with sqlite3 database takes 2ms. Using postgres, response time extends to over 30ms. Why is that, how to make postgres faster?

​

Settings with sqlite:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASEDIR / 'db.sqlite3',
}
}

https://preview.redd.it/yhrvlevoebca1.png?width=1228&format=png&auto=webp&v=enabled&s=a572f9c9b2f26e3e39de276447a3179026c43150

Settings with postgres:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql
psycopg2',
'NAME': 'drf-test',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'localhost',
'PORT': '5432',
}
}

​

https://preview.redd.it/mdg0q37tebca1.png?width=1442&format=png&auto=webp&v=enabled&s=950e1bd53e70a7f3f064ee266fa215c6aa700adc

class Product(models.Model):


/r/django
https://redd.it/10d2d5z
Are Django 3 tutorials viable today?

I have found 2 YouTube tutorials which are simple to follow, one is for Django 3.0, and one is 4 years old, so I want to know if they are viable today before I figure it out myself when it is too late.

/r/django
https://redd.it/10d7m4y
NiceGUI: Let any browser be the frontend for your Python code

We are thrilled to share that NiceGUI, our open-source library for building web-based user interfaces, continues to thrive. With NiceGUI, you can focus on writing Python code while the web development details are handled behind the scenes. This makes it ideal for a wide range of projects including short scripts, dashboards, robotics projects, IoT solutions, smart home automation, and machine learning.

Of course there are valid use cases for splitting frontend and backend technologies. NiceGUI is for those who don’t want to leave the Python ecosystem and like to reap the benefits of having all code in one place. There are other options like Streamlit, Dash, Anvil, JustPy, and Pynecone. But we initially created NiceGUI to easily handle the state of external hardware like LEDs, motors, and cameras. Additionally, we wanted to offer a gentle learning curve while still providing the ability to go all the way down to HTML, CSS, and JavaScript if needed.

NiceGUI runs on top of FastAPI which allows you to add authentication, routing, OpenAPI specs, and other backend functionality to your projects. The frontend is implemented using Vue, Quasar, and Tailwind, but you don’t need to be familiar with these technologies to create beautiful user interfaces. NiceGUI

/r/Python
https://redd.it/10d6ugv
How Python 3.11 became so fast!!!

With Python 3.11, it’s making quite some noise in the Python circles. It has become almost 2x times faster than its predecessor. But what's new in this version of Python?

New Data structure: Because of the removal of the exception stack huge memory is being saved which is again used by the cache to allocate to the newly created python object frame.

Specialized adaptive Interpreter:

Each instruction is one of the two states.

General, with a warm-up counter: When the counter reaches zero, the instruction is specialized. (to do general lookup)
Specialized, with a miss counter: When the counter reaches zero, the instruction is de-optimized. (to lookup particular values or types of values)

Specialized bytecode: Specialization is just how the memory is read (the reading order) when a particular instruction runs. The same stuff can be accessed in multiple ways, specialization is just optimizing the memory read for that particular instruction.

Read the full article here: https://medium.com/aiguys/how-python-3-11-is-becoming-faster-b2455c1bc555

​

https://preview.redd.it/lzode0q58eca1.png?width=828&format=png&auto=webp&v=enabled&s=46f01d18edf19d961b9fa958e832f3302716533e

/r/Python
https://redd.it/10dcjxx
Launched our first tiny MacOS App Store app written entirely in Python :D Clingwrap: niche app to share custom screen regions in Teams, Zoom, or any video calling app (useful for wide screen monitors)

Sharing my entire 49" screen over Microsoft Teams has always been difficult (people on normal monitors can't see shared widescreen monitors well because of the aspect ratio). Sharing windows also gets annoying because you have to constantly switch between them, which is especially frustrating during presentations or pair programming.

Unfortunately, Teams and Slack don't offer a good way to share only a part of your screen during screen sharing (Although I think Zoom does at this point)

My friend and I always wanted to see what it would be like to launch a small app on MacOS, so we worked on solving this problem. Clingwrap is a simple app with a window that you can share in any video calling program. This window allows you to display custom regions of your screen.

This is the first app I've ever launched and I'm an amateur when it comes to this stuff. It was tough and the app itself certainly is not perfect, but the learning process was rewarding. There are a number of improvements we'd like to make, including having the share window UI be less visible during sharing. It works well enough on our Macs (M1) that it's mostly solved

/r/Python
https://redd.it/10da7pc
Lyrid: New Actor Model Framework for complex parallel programming

I'm really exciting to share my new Python framework for parallel programming called Lyrid. It's still a prototype, but may be if you guys can give me some feedback, I would really appreciate.

Lyrid is an actor model framework, similar to Akka and Erlang, that provides a nice way to design and build a parallel system in Python, which, you know, has some issues with GIL. Not only simple patterns like "computing tasks in parallel and get the results back", but also complex architecture that looks more like a cyclic graph are possible in Lyrid. It enables flexibility to design complex system with the benefit of parallel computing.

Actor model is another paradigm, not OOP, not Functional Programming, saying a system is composed of actors. An actor can have its own state, data, and behavior, similar to OOP. But, an actor works in message-driven manner. Then, actors can interact with each other via message passing only, like in microservices (if done properly).

In this way, it allows the system to be designed for scaling, not only for multi-core, but also on multiple machines. Currently, Lyrid operates on single machine. Might go for large-scale distributed system in the future.

For More detail about Lyrid and

/r/Python
https://redd.it/10dcmpr
New async networking lib with P2P support

Hello Pythonistas,

I spent the last year designing a new async networking library for Python based on what I perceive as short-comings in Python's current networking ecosystem. Some things I don't like about how Python does networking at the moment include:

1. No async support for UDP.
2. Can't use async functions with Protocol classes.
3. Different APIs for UDP and TCP.
4. No way to manage network interface cards.
5. IPv6 is still painful to use.

So I designed a new async API for general networking. Usage looks like this:

from p2pd import *

# Load very general interface information.
netifaces = await init_p2pd()

# Load the default interface.
i = await Interface(netifaces=netifaces)

# Select a route to use.
# The address family used is i.supported()[0] when one is not specified.
# When a route is not specified the first route is used.
# Routes control what external addresses are used for networking.
route = await i.route().bind()

# Open a UDP pipe to an echo server.
dest = await Address("p2pd.net", 7, route)
pipe = await pipe_open(UDP, dest, route)
pipe.subscribe()

# Echo client example.
# Recv may time out because it's UDP.
await pipe.send(b"test message", dest.tup)
out = await pipe.recv()
await pipe.close()


The library also supports P2P networking. You can read more about it on https://p2pd.readthedocs.io/

And the project is on Pypi https://pypi.org/project/p2pd/ and Github https://github.com/robertsdotpm/p2pd

/r/Python
https://redd.it/10ddpe4
Compclasses: prefer composition over inheritance

I am thrilled to share that I just made Compclasses public, a (small) library built to simplify the usage of composition (over inheritance?!) in #python.

Any feedback is very welcomed!

Documentation: https://fbruzzesi.github.io/compclasses
Github: https://github.com/fbruzzesi/compclasses

/r/Python
https://redd.it/10deaff
Python contest worth 50 Euros

Hey guys
Fellow python developer and entrepreneur here.
Me and my team is thinking to host a small coding contest, with 50 euros as the prize money.

We still want to know, if people would be interested in something like this, we are ready with everything else, there's gonna be NO REGISTRATION FEE. So this is not really a promotion.

Just wanna know your views.
If everything works well, then I will post then link in this subreddit too!
Thanks!

/r/Python
https://redd.it/10droer
I made a web extension that lets you preview content of a links on hover for google. Currently works in chromium browsers. Using flask backend to bypass Cors

You can download it from https://github.com/ipriyam26/preview

The backend is also open sourced and can be found on my github

​

https://reddit.com/link/10daxxd/video/dq5z1nw4rdca1/player

Hosted on a cheap render server so please tolerate the delay

/r/Python
https://redd.it/10daxxd
Good practices for 'merging' information from two different models in order to manipulate them in a template

Hi guys. I'm new to Django, and i'am a little bit lost.

In my application, users can follow communities, and earn points by performing certain actions.

Quests are point goals to earn by performing some actions, users get a reward when they complete a quest. Quests are linked to communities, Epics are a set of Quests to complete. Quests are not necessary related to Epics.

I created these models:

class Quest(models.Model):
QUESTTYPESCHOICES = ('...')
community = models.ForeignKey(Community, ondelete=models.CASCADE)
type = models.CharField(max
length=20, choices=QUESTTYPESCHOICES)
name = models.CharField(maxlength=100, blank=False)
description = models.TextField(blank=False)
type = models.CharField(max
length=20, choices=QUESTTYPESCHOICES)
target = models.CharField(maxlength=255, null=True, blank=True)
point
goal = models.IntegerField()
startdate = models.DateField(null=True, blank=True)
end
date = models.DateField(null=True, blank=True)


class

/r/django
https://redd.it/10dtcfo