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 to get objects with certain foreignkey relationships in ORM?

Assume we have a product model:

`class Product(models.Model):`

`name = models.Charfield()`

And a property model where possible properties are defined (like "price", "color", "weight"...):

`class Property(models.Model):`

`name = models.CharField()`

And we keep product properties in a separate model:

`class ProductProperty(models.Model):`

`propery = models.ForeignKey(Property)`

`product = models.ForeignKey(Product)`

`value = models.Charfield()`

I want to get product objects which have certain properties. For instance, **I want to get objects only if "price" and "color" is defined for them in ProductPropery table.**

I obtain the required properties as objects, but I could not solve how can I get products that have all of the given properties.

To rephrase, I'm looking for something like this:

`properties = Property.objects.filter(Q(name__contains="Price") | Q(name__contains="Color")) #this could return one or multiple property objects`

​

`products = properties.productproperty_set.product_set # imaginary line I made up to show what I want to get`

I could only think of: looping through properties and sub-loop for related ProductProperties to get their products and create multiple lists, and then create a list that's made of common elements (products that are included in each list)

It would be great if you could help,

Best wishes

/r/django
https://redd.it/ihisa1
DS Slideshow, My first Flask/ Python Project.

Hi, I'm an 18 year old college student going into university this year! I've been working on a small Flask based app for a while now and I think its about ready to share! I created a simple lightweight digital signage application for use on a Raspberry Pi or similar. It works by hosting a website on the host machine which you can then access from any number of other screens to display your slideshow.

This is a passion project for me, I intern at a small company that was looking for a digital signage solution for their office. They were not satisfied with any of the applications on the market so I volunteered to see what I could come up with. After a couple of days, I had a simple HTML and JS webpage that you could drop images into a file and they would rotate on a slideshow. Although this was all that was required by the company, I continued to develop this software on my own time and I'm really happy with how it's turning out!

Features:

* Currently there is support for Images and Webpages with video support coming soon!
* Login page to ensure nobody alters the slides without

/r/flask
https://redd.it/ihjqnp
[P] GenRL: PyTorch-First Reinforcement Learning library centered around reproducible and generalizable algorithm implementations

Github: [https://github.com/SforAiDl/genrl](https://github.com/SforAiDl/genrl)

Check out the [documentation](https://genrl.readthedocs.io/) and [tutorials](https://genrl.readthedocs.io/en/latest/usage/tutorials/index.html).

Reinforcement learning research is moving faster than ever before. In order to keep up with the growing trend and ensure that RL research remains reproducible, GenRL aims to aid faster paper reproduction and benchmarking by providing the following main features:

* **PyTorch-first**: Modular, Extensible and Idiomatic Python
* **Unified Trainer and Logging class**: code reusability and high-level UI
* **Ready-made algorithm implementations**: ready-made implementations of popular RL algorithms.
* **Faster Benchmarking**: automated hyperparameter tuning, environment implementations etc.

By integrating these features into GenRL, we aim to eventually support **any new algorithm implementation in less than 100 lines**.

Currently, the library has implementations of popular classical and Deep RL agents that ready to be deployed. Apart from these, various Bandit algorithms are a part of GenRL. It has various abstraction layers that make the addition of new algorithms easy for the user.

The library aims to add other key research areas like Multi-agent RL, Evolutionary RL and hyperparameter optimization and provide extensive support for distributed training of agents.

**We are looking for Open Source Contributors.**

/r/MachineLearning
https://redd.it/ihhtiw
Bare essential javascript to learn to help with flask

I feel that recently lack of ability to interact with dom is getting in the way of me communicating between back and front-end. (ofc, I could be entirely wrong too)

i really want to focus on python and flask at this period but am ope to invest time to JS to help with that journey. I am building things for myself so do not need to achieve anything fancy.

In order for me to make the most of that time what subjects should I focus on? (I hear eloquent is a good book)

Thank you for helping/reading

/r/flask
https://redd.it/ihgum3
Thursday megathread: Python careers!

Discussion of using Python in a professional environment, getting jobs in Python and more!

**This thread is not for recruitment, please see** r/PythonJobs **or the thread in the sidebar for that.**

/r/Python
https://redd.it/ihaiio
Django admin: adding objects for foreignkey from other side

so I have these two models:

class Recipe(models.Model):
short_description = HTMLField(max_length=400)
likes = models.ManyToManyField(User, blank=True,
related_name='recipe_likes')
slug = models.SlugField(blank=True, unique=True)
published_date = models.DateTimeField(blank=True, default=datetime.now)
ratings = GenericRelation(Rating, related_query_name='recipes')

class Ingredient(models.Model):
name = models.CharField(max_length=20)
amount = models.FloatField()
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE,


/r/django
https://redd.it/ihij82
Are my tests okay?

I read on Two Scoops of Django that you should test the following aspects of your models:

>Two Scoops of Django 3.x
>
>Models: Creating/updating/deletingofmodels,modelmethods,modelmanagermethods.

however, there is not an example of how to do it with pytest, is my approach correct for these tests?

​

models.py

from model_utils.models import TimeStampedModel
import uuid

# from mysocialbeer.accounts.models import Account
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.urls import reverse


class Post(TimeStampedModel):
"""
This is the model that represent posts in MySocialBeer
"""

uid = models.UUIDField(default=uuid.uuid4())
title = models.CharField(verbose_name=_("Title"), max_length=50)
body = models.TextField(verbose_name=_("Body"))
latitude = models.FloatField(verbose_name=_("Latitude"))


/r/djangolearning
https://redd.it/ihskvi
Friday megathread: Free chat Friday!

Use this thread to talk about anything Python related! Questions, news, projects and any relevant discussion around Python is permitted!

/r/Python
https://redd.it/ihwl4s
Is there a better way to handle unexpected view errors.

What I do is, I add a middleware so that in case the view function raises an unhandled exception the default Django response is overridden by my specified response, see the following code snippet.


class ErrorHandlingMiddleware:
"""
The purpose of this middleware is to handle unexpected errors in the requests and
provide a user friendly error response.
"""

def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
return response

@staticmethod
def process_exception(request, exception):
if settings.DEBUG:


/r/django
https://redd.it/ihznwr
I learned how to make an online Multiplayer game using Python!!

I am interested in game development but am very new to it. I've messed around with unity and C# for a bit but decided I wanted to learn how to create an online multiplayer game like Agario or Draw my things. I decided to use Python since it is my favorite language!

The whole process was pretty difficult. I had to learn to make web apps (flask), web sockets (socketio), manage databases, and a bunch of other stuff.

[If you are interested in the process or want to see how it turned out, check out my video here!](https://www.youtube.com/watch?v=DrYXpVLQY_E)

Thanks

/r/Python
https://redd.it/ii1tnj
[R] Extended blog post on "Hopfield Networks is All You Need"

My colleague Johannes Brandstetter wrote an awesome blog post on our new paper "Hopfield Networks is All You Need": [https://ml-jku.github.io/hopfield-layers/](https://ml-jku.github.io/hopfield-layers/)

It illustratively introduces traditional, dense, and our modern Hopfield neworks, and provides explained code examples of the Hopfield layer.

Highly recommended!

/r/MachineLearning
https://redd.it/ii319z
PyWeek 30 starts on the 20th September!

***PyWeek 30 will begin on the 20th September (00:00 UTC) and end on the 27th September (00:00 UTC).***

# What is PyWeek?

PyWeek is one of the longest running events in the Python community – it's been run twice a year for the past 15 years! During the jam, competitors are given a theme which is decided by the community through a vote. You must build a game from scratch in a week using Python that relates to this theme. After the jam, your fellow competitors vote on submissions to decide a winner. You can see hundreds of previous submissions to PyWeek at [https://pyweek.org/all\_games/](https://pyweek.org/all_games/)

# How can I join?

You can enter PyWeek as an individual or as a team. If you are looking for teammates, you can find fellow PyWeekers in #pyweek on [Python Discord](https://discord.gg/python)! If you want to compete in PyWeek, check out the information on the PyWeek website at [https://pyweek.org/](https://pyweek.org/), you need to register with this site before September 27th (00:00 UTC) in order to participate!

# Where can I find out more?

You can find all the necessary rules and documentation at [https://pyweek.readthedocs.io/](https://pyweek.readthedocs.io/). There are no restrictions on what game library you use as long as the game logic is written in

/r/Python
https://redd.it/ii5r6d
Build Your Own Flight Tracking Application with Python and Open Air Traffic Data

I wrote a tutorial how to build a flight tracking application with Python and Open Air Traffic Data from OpenSky Network. Check out the tutorial and the code.

[https://www.geodose.com/2020/08/create-flight-tracking-apps-using-python-open-data.html](https://www.geodose.com/2020/08/create-flight-tracking-apps-using-python-open-data.html)

[filght tracking application](https://i.redd.it/vg1z387kzpj51.gif)

/r/Python
https://redd.it/ii4oga
APIs.

I recently joined as a Intern in a company mainly assisting in Back End. Now, last day, my frontends developers came to me and asked me to provide the Api . So, does that mean I should be providing all the codes that I have written for APis.?? I developed simple apis using serialization of some models. Now, I should provide the source code and endpoints to that frontend developer? I mean cant he looked into my Github and download the code? I am totally lost.

/r/django
https://redd.it/ii3isd
[News] Apple's AI/ML Residency Program

Apple just announced it's new AI/ML residency program! More details about the program can be found at [https://machinelearning.apple.com/updates/introducing-aiml-residency-program](https://machinelearning.apple.com/updates/introducing-aiml-residency-program). The program is available in multiple locations -- details [here](https://jobs.apple.com/en-us/search?search=%23aimlresidency&sort=relevance).

I'm an ML engineer at Apple Special Projects Group (SPG) in the Applied ML team led by Ian Goodfellow, and I'll be a resident host for this program. To apply to work on my team, please check out [https://jobs.apple.com/en-us/details/200175569/ai-ml-residency-program?team=MLAI](https://jobs.apple.com/en-us/details/200175569/ai-ml-residency-program?team=MLAI).

/r/MachineLearning
https://redd.it/ii18ae