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
Registration form shows too many fields after I make custom register form model

I'm trying to make a custom registration form, but am running into trouble.

My `forms.py` has this:

class RegisterForm(UserCreationForm):
'''
Form that makes user using just email as username
'''
error_messages= {
"password_mismatch": _("Passwords do not match."),
"duplicate_email": _("Email already exists."),
"unique": _("Email already exists"),
}
register_username= forms.EmailField(label=_("Email"), widget=forms.TextInput(attrs={"placeholder":"Email"}))
register_password1= forms.CharField(label=_("Password"), widget=forms.PasswordInput(attrs={"placeholder":"Password"}))
register_password2= forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput(attrs={"placeholder":"Confirm password"}))

def clean_username(self):
username = self.cleaned_data["username"]
try:
User._default_manager.get(username=username)

#if the user exists, then let's raise an error message
raise forms.ValidationError(self.error_messages['duplicate_email'], #user my customized error message
code='duplicate_email', #set the error message key
)
except User.DoesNotExist:
return username # great, this user does not exist so we can continue the registration process

class Meta:
model= User
fields= ("username",)

My `views.py` looks like this:

def login_register(request, template="pages/login_register.html"):
registration_form= RegisterForm()
return render(request, template, {"registration_form": registration_form})


Which leads to my registration form in `login_register.html` rendering like this:

<form method="post">
<input type="hidden" name="csrfmiddlewaretoken" value="CSRF-TOKEN-HERE">
<p><input autofocus="" id="id_username" maxlength="150" name="username" type="text" required=""></p>
<p><input id="id_password1" name="password1" type="password" required=""></p>
<p><input id="id_password2" name="password2" type="password" required=""></p>
<p><input id="id_register_username" name="register_username" placeholder="Email" type="text" required=""></p>
<p><input id="id_register_password1" name="register_password1" placeholder="Password" type="password" required=""></p>
<p><input id="id_register_password2" name="register_password2" placeholder="Confirm password" type="password" required=""></p>
<button class="btn btn-primary" type="submit">Register</button>
</form>


I only want the last three `input` tags to be used. My goal is for my registration form to have three fields: Email, password, and password confirmation. Why is Django loading the three extra top `input` fields and how do I prevent that?

/r/django
https://redd.it/7xn7pe
Screen/monitor controlled by PIR sensor

I have three files, all in the same directory (/home/pi/pir)

1. [pir.py](https://pir.py)\--> Main code to detect PIR sensor from GPIO

2. [scON.sh](https://scON.sh) \--> Shell to turn ON screen via dpms

3. [scOFF.sh](https://scOFF.sh) \--> Shell to turn OFF screen via <xset dpms force off> (works from terminal)

&#x200B;

My objective is to toggle the monitor on and off depending on the state of the PIR sensor. The time.sleep times are just in there for testing purposes - I still have to figure that part out.

High = turn on

Low = turn off

&#x200B;

Here is my main code in the [pir.py](https://pir.py) file. I inserted the error I get (when I trip the PIR) as comment lines within the code, but I am new to python, Linux, etc and do not understand the errors.

Here is the main code to sense PIR and toggle screen on (when HIGH) and off (when LOW). could someone kindly point me in the right direction to get this working?

&#x200B;

import RPi.GPIO as GPIO #Import GPIO library
import time #Import time library
import subprocess

GPIO.setmode(GPIO.BOARD) #Set GPIO pin numbering


/r/Python
https://redd.it/cyphtc
New Flask Library that makes uploading & saving files super easy!

[Flask File Upload](https://preview.redd.it/nk0uet18qji41.jpg?width=5945&format=pjpg&auto=webp&s=d02a804c082d423bc0d053a8c8d2b411fa9df72a)

Hi guys

I have just released v0.1.0 of a new Flask library to make uploading files and storing them on your server & database as easy as possible... just let Flask-File-Upload handle all the repetitive work!

Works with Flask & Flask-SQLAlchemy

[https://github.com/joegasewicz/Flask-File-Upload](https://github.com/joegasewicz/Flask-File-Upload)

The v0.1.0 release includes a new method:

`add_file_urls_to_models`

This method appends your file url strings directly onto your models ... very convenient! Read more here: [https://github.com/joegasewicz/flask-file-upload#set-file-paths-to-multiple-objects---available-in-010-rc6--v010](https://github.com/joegasewicz/flask-file-upload#set-file-paths-to-multiple-objects---available-in-010-rc6--v010)

I would be very grateful if anyone wishes to try out this library & or give some feed back & star! And of course contributions are more than welcome.

Thanks so much

&#x200B;

* Update: For anyone using `v0.1.0` there was a small bug when updating files that didn't yet exist. Your files will save but an error msg is logged out, which is not great! This is now fixed as of `v0.1.1`

Joe

/r/flask
https://redd.it/f7zgdd
New Flask Library that makes uploading & saving files super easy!

&#x200B;

https://preview.redd.it/xpo2x8zys7m41.jpg?width=2200&format=pjpg&auto=webp&s=0c9b5a223e152d0c4f351ef58f7a2f378d7d1ff6

Hi guys

I have just released v0.1.0 of a new Flask library to make uploading files and storing them on your server & database as easy as possible... just let Flask-File-Upload handle all the repetitive work!

Works with Flask & Flask-SQLAlchemy

[https://github.com/joegasewicz/Flask-File-Upload](https://github.com/joegasewicz/Flask-File-Upload)

The v0.1.0 release includes a new method:

`add_file_urls_to_models`

This method appends your file url strings directly onto your models ... very convenient! Read more here: [https://github.com/joegasewicz/flask-file-upload#set-file-paths-to-multiple-objects---available-in-010-rc6--v010](https://github.com/joegasewicz/flask-file-upload#set-file-paths-to-multiple-objects---available-in-010-rc6--v010)

I would be very grateful if anyone wishes to try out this library & or give some feed back & star! And of course contributions are more than welcome.

Thanks so much

&#x200B;

* Update: For anyone using `v0.1.0` there was a small bug when updating files that didn't yet exist. Your files will save but an error msg is logged out, which is not great! This is now fixed as of `v0.1.1`

Joe

/r/flask
https://redd.it/fhdy77
Matplotlib Scatterplots, but with Emojis as Markers

I wrote a function to create scatterplots using emojis as markers to support some analysis & visualization I'm doing for a (very silly) side project. After a good bit of research (I was pretty shocked this didn't exist already), I built this based on [this article](https://towardsdatascience.com/how-i-got-matplotlib-to-plot-apple-color-emojis-c983767b39e0), but adapted to produce a scatterplot instead of a bar chart.

#function to create a scatterplot with emojis as markers
#based on https://towardsdatascience.com/how-i-got-matplotlib-to-plot-apple-color-emojis-c983767b39e0
#follow instructions above to install & build mplcairo

#Set the backend to use mplcairo
import matplotlib, mplcairo
print('Default backend: ' + matplotlib.get_backend())
matplotlib.use("module://mplcairo.macosx")
print('Backend is now ' + matplotlib.get_backend())

# IMPORTANT: Import these libraries only AFTER setting the backend
import matplotlib.pyplot as plt, numpy as np
from matplotlib.font_manager import FontProperties

# Load Apple Color Emoji font
prop = FontProperties(fname='/System/Library/Fonts/Apple Color Emoji.ttc')

# Load Apple Color Emoji font
prop =

/r/Python
https://redd.it/iazdqd
PEP 802 – Display Syntax for the Empty Set

PEP 802 – Display Syntax for the Empty Set
https://peps.python.org/pep-0802/

# Abstract

We propose a new notation, {/}, to construct and represent the empty set. This is modelled after the corresponding mathematical symbol ‘∅’.

This complements the existing notation for empty tuples, lists, and dictionaries, which use ()[], and {} respectively.

>>> type({/})
<class 'set'>
>>> {/} == set()
True

# Motivation

Sets are currently the only built-in collection type that have a display syntax, but no notation to express an empty collection. The Python Language Reference notes this, stating:

>An empty set cannot be constructed with {}; this literal constructs an empty dictionary.

This can be confusing for beginners, especially those coming to the language from a scientific or mathematical background, where sets may be in more common use than dictionaries or maps.

A syntax notation for the empty set has the important benefit of not requiring a name lookup (unlike set()). {/} will always have a consistent meaning, improving teachability of core concepts to beginners. For example, users must be careful not to use set as a local variable name, as doing so prevents constructing new sets. This can be frustrating as beginners may not know how to recover the `set` type if they have overriden the name.

/r/Python
https://redd.it/1mnuan1