Get week start date (Monday) from a date column in Python (pandas)?

Another alternative: df[‘week_start’] = df[‘myday’].dt.to_period(‘W’).apply(lambda r: r.start_time) This will set ‘week_start’ to be the first Monday before the time in ‘myday’. You can choose different week starts via anchored offsets e.g. ’W-THU’ to start the week on Thursday instead. (Thanks @Henry Ecker for that suggestion)

Memory efficiency: One large dictionary or a dictionary of smaller dictionaries?

Three suggestions: Use one dictionary. It’s easier, it’s more straightforward, and someone else has already optimized this problem for you. Until you’ve actually measured your code and traced a performance problem to this part of it, you have no reason not to do the simple, straightforward thing. Optimize later. If you are really worried about … Read more

Change Django ModelChoiceField to show users’ full names rather than usernames

You can setup a custom ModelChoiceField that will return whatever label you’d like. Place something like this within a fields.py or wherever applicable. class UserModelChoiceField(ModelChoiceField): def label_from_instance(self, obj): return obj.get_full_name() Then when creating your form, simply use that field UserModelChoiceField(queryset=User.objects.filter(is_staff=False), required = False) More info can be found here

How to automatically reflect database to sqlalchemy declarative?

In theory reflection in sqlalchemy should work for you. In this case I’m using an mssql database with two tables which have a simple Many-to-one relation: “Tests” with fields: id testname author_id (foreign key to the Users table, Users.id field) “Users” with fields: id fullname So the following should reflect the database: from sqlalchemy import … Read more

OpenCV videowrite doesn’t write video

I had the same problem and I solved it by specifying the video output resolution to exactly the same as input: cap = cv2.VideoCapture(‘vtest.avi’) … out = cv2.VideoWriter(‘output.avi’,fourcc, 20.0,(int(cap.get(3)),int(cap.get(4)))) Of course make sure you got ffmpeg installed and working.

Sanitizing HTML in submitted form data

strip_tags actually removes the tags from the input, which may not be what you want. To convert a string to a “safe string” with angle brackets, ampersands and quotes converted to the corresponding HTML entities, you can use the escape filter: from django.utils.html import escape message = escape(form.cleaned_data[‘message’])