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
When creating a "Vote" system should you store count or is subtracting downvotes from upvotes fine?

I'm looking to create a basic voting system for some objects. My model looks like:

class Model(models.Model):

obj = models.ForeignKey('obj.Obj')
user = models.ForeignKey('users.User')

upvotes = models.PositiveIntegerField(default=0)
downvotes = models.PositiveIntegerField(default=0)
count = models.IntegerField(default=0)

I was just wondering do i need the `count` field or could i just rely on using something like


@cached_property
def count(self):
return self.upvotes - self.downvotes

If i should store a count field, why is that?

/r/django
https://redd.it/6z3wh6
Help with adding javascript to django templates

Today was my first attempt at adding javascript to a django template. I've tried to load the javascript in my base template, then I included a bunch of script inside another template. Part of the base.html is as follows, it is generally working because I know that my css files get loaded:

<!DOCTYPE html>
<html lang="en">
{% block head %}<head>{% load static %}

<meta charset="utf-8">

<!-- CSS
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<link rel="stylesheet" href="{% static "css/normalize.css" %}">
<link rel="stylesheet" href="{% static "css/skeleton.css" %}">
<link rel="stylesheet" href="{% static "css/layout.css" %}">

<!-- JAVASCRIPT
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<script type="text/javascript" language="Javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.11.3.min.js"></script>

</head>
{% endblock %}


And here is the template that is supposed to have the javascript working:

{% extends 'classroom/base.html' %}
{% block body %}
<div class="container">
<div class="row">
<div class="twelve columns">
<h1>Choose the block</h1>
</div>
</div>

<div class="row">
<div class="twelve columns">
{{ classroom.id }}
<form action="{% url 'classroom:random' %}" method="get">
<select name="class_block">
{% for room in classroom %}
<option value={{ room.course_block }}>{{ room.get_course_block_display }}</option>
{% endfor %}
</select>
<input type="submit" value="submit" />
</form>
</div>
</div>
<div class="row">
<div class="one-third columns">
test
{% for s in students %}
<ul>
<li>{{ s.nickname }}</li>
</ul>
{% endfor %}
</div>
</div>
<div class="row">
<div class="twelve columns">
<script>
var nameArray = [
"Jimmy",
"Sonny",
"Sammy",
"Henry",
"Hank",
"Susan",
"Sebby",
"Alyssa",
"Pepper",
"Ken",
"Steve",
"Kandi",
"Wes"
];
var numberStudents = nameArray.length;
var interval;

for(n = 0; n < numberStudents; n++){
var divName = "floatName" + n;
console.log(divName);
var names = nameArray[n];
var divTag = document.createElement('div');
divTag.id = divName;
divTag.innerHTML = names;
divTag.style.position = "absolute";
divTag.style.top = 50 + n*20 + "px";
divTag.style.left = "50px";
divTag.className = "randomFloat";
document.body.appendChild(divTag);
};
var loopAllowed = false;
$( "#go" ).click(function() {
loopAllowed = true;
var max = 12;
var loop = function(){
for(i = 0; i < (numberStudents + 1); i++){
var divName = "floatName" + i;
console.log(divName);
$( "#" + divName ).animate({
left: Math.random()*500 + "px",
top: Math.random()*500 + "px"
}, 500, i == max - 1 && loopAllowed ? loop : undefined);
}
};
loop();
});

$( "#stop" ).click(function() {
loopAllowed = false;
});

</script>
</div>
</div>
</div>
{% endblock %}

The parts inside <script></script> work when its loaded into a browser on its own. Inside this template though, nothing is happening.

/r/django
https://redd.it/6z38c1
Auto setting a thumbnail to an album model

Hey guys,

I'm working on a project where I have photo albums and each album has it's pictures (with a ForeignKey to the album model). How can I pull the first image of the album and set it as the thumbnail of the album?

I'm thinking about setting up a view function that checks if there are any images in the model, and if there is, it pulls one of these images and defines it as a thumb. But I'm not sure how to do it exactly.

/r/django
https://redd.it/6z24dj
We are the Google Brain team. We’d love to answer your questions (again)

We had so much fun at our [2016 AMA](https://www.reddit.com/r/MachineLearning/comments/4w6tsv/ama_we_are_the_google_brain_team_wed_love_to/) that we’re back again!

We are a group of research scientists and engineers that work on the Google Brain team. You can learn more about us and our work at [g.co/brain](http://g.co/brain), including a [list of our publications](https://research.google.com/pubs/BrainTeam.html), our [blog posts](https://research.googleblog.com/search/label/Google%20Brain), our [team's mission and culture](https://research.google.com/teams/brain/about.html), some of our particular areas of research, and can read about the experiences of our first cohort of [Google Brain Residents](http://g.co/brainresidency) who “graduated” in June of 2017.

You can also learn more about the TensorFlow system that our group open-sourced at [tensorflow.org](http://tensorflow.org) in November, 2015. In less than two years since its open-source release, TensorFlow has attracted a vibrant community of developers, machine learning researchers and practitioners from all across the globe.

We’re excited to talk to you about our work, including topics like creating machines that [learn how to learn](https://research.google.com/pubs/pub45826.html), enabling people to [explore deep learning right in their browsers](https://research.googleblog.com/2017/08/harness-power-of-machine-learning-in.html), Google's custom machine learning TPU chips and systems ([TPUv1](https://arxiv.org/abs/1704.04760) and [TPUv2](http://g.co/tpu)), use of machine learning for [robotics](http://g.co/brain/robotics) and [healthcare](http://g.co/brain/healthcare), our papers accepted to [ICLR 2017](https://research.googleblog.com/2017/04/research-at-google-and-iclr-2017.html), [ICML 2017](https://research.googleblog.com/2017/08/google-at-icml-2017.html) and NIPS 2017 (public list to be posted soon), and anything else you all want to discuss.

We're posting this a few days early to collect your questions here, and we’ll be online for much of the day on September 13, 2017, starting at around 9 AM PDT to answer your questions.


/r/MachineLearning
https://redd.it/6z51xb
OLS model in Python; how to find correlation between gender and 20 specific diseases?

Apologies for the bad explanations, I am really confused and clueless.

As the title states, I am trying to find the correlation but I am having some difficulties with the work before running the model.

I have male, female, 20 different diseases "as the columns" and patients on the rows. I have "dummied" it down to 0.0 or 1.0,
ex:

Male Female D1 D2

P1 0.0 , 1.0 , 0.0 , 1.0

P1 is female with disease D2.

I want to use the model to find which disease(s) have a higher correlation to males.

Now I am clueless to the steps to find the correlation....For the first time, I would appreciate answers which is "dummyed" down for me hehehe

/r/pystats
https://redd.it/6z8tyl
How do you distribute your app?

I finally made my way to finalize the first version of my web app! Now the question I'm struggling with is: how do I distribute it?

The app (Django + Django Rest Framowork + client with tons of JS) is of the "final product" type (not a Django reusable app) with, obviously, dependencies. It is supposed to be simply deployed on a host and served by a HTTP server.

When I deploy a PHP web app on a server, I am used to deal with a single archive containing all the necessary files. I thought it'd be the same for a Django app, but I don't know how to handle the dependencies.

So far, the suggestions I've read are the following:

* Create a Docker container. Seems neat, but a bit overkill;
* Create a system package (RPM, DEB). Still overkill;
* Create a Python package with setuptools. Many posts on SO suggest to go this way. Why? For me, it doesn't make sense: as I said, the app is a "final product," there is no point to install it as a regular package (nor point to import it in another project). Moreover, dependencies are not included in the archive. And, if installed, how do I edit local settings?
* Create a all-in-one Python package with PyInstaller. Seems overkill as Python should be available on a server. And I had a hard time to get it working;
* Do nothing, just create an archive of the repo, with no dependencies, and put documentation to set the project up.

I'm very perplex. What should I do?

/r/django
https://redd.it/6zbdi8
DjangoCon 2017 Videos are up and a question!

Firstly, I know a lot of people have been waiting, but the [recordings from DjangoCon 2017 are finally up!](https://www.youtube.com/channel/UC0yY6a79pPY9J0ShIHRf6yw/videos)

Secondly, I want to know from you, the community, what would you like to see covered next year?

/r/djangolearning
https://redd.it/6yio0r
Extract data from invoices?

Hi, I’m trying to automate some tedious parts of my work but I don’t know exactly how to go about doing so.

I get a lot of invoices from several companies who all have different designs and types, some will be an excel spreadsheet converted to a pdf and some will be a scanned document, but I need to extract some information from them.

I don’t need all the data from the invoices and there are certain areas where the data is or certain keywords I could use to find the data.

What would be the best way to go about doing this?

/r/Python
https://redd.it/6zd6ui
New to Django, how do i know which imports i need and where do i found or learn them?

I am new to Django and Python, and this is the first time that i am learning and using a Framework (MVT). My question is, how do i know which kind of imports i need when using Django?
Example: from django.conf.urls import url
How do i know i need to include it. I learned some PHP and i didn't have to import classes/functions manually?
How do i learn what i need and where can i find what i need? Isn't this a bit to complex, why do i need to add and import everything manually?

/r/djangolearning
https://redd.it/6zed09
I made a hacker news clone for practice

I post the Pinterest clone about a week ago, now its the time for me to post a [hacker news clone](https://hnews-clone.herokuapp.com/) and I don't know but I think it's much better than my last one app who doesn't allow you to sign up and the CBV login view don't have any style class

I use bootstrap for this one, and I think, the only issue is my cancel button when I try to delete because doesn't redirect to the detail view again and give an error. And maybe add a pagination after 30 items.

Well, tell me anything or suggest me another app to build

[github](https://github.com/iKenshu/hnews)

/r/django
https://redd.it/6zewgx