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
nbviewer does not see notebook in Github repository?

Hi Guys. Does anyone know why nbviewer would not be able to see a notebook I uploaded to one of my Github repositories? If I go to the repository, I can see the notebook and view its contents, but nbviewer will not recognize that it's there. Has anyone else experienced this?

/r/IPython
https://redd.it/6asfa6
Invalid syntax when invoking package name?

I'm learning to use ipython qtconsole with scipy tools to practice graphing differential equations. I wrote a short script to set up the environment to work in. [Here is the script](https://pastebin.com/uhGRYycE).

When I use it in qtconsole, it gives me this error:

File "/Users/me/DiffEq/deenv.py", line 9
%matplotlib inline
^
SyntaxError: invalid syntax

However, when I just type "%matplotlib inline" into the console, it gives me no such error.

Why is this?

/r/IPython
https://redd.it/6b0vlw
How do I remove the In [ ] Out [ ] in a Jupyter notebook?

When coding in a Jupyter notebook, is it possible to disable showing the In [ ], Out [ ] next to each code cell? If not, is it possible to do when rendering the notebook using NBViewer? I'd like my notebooks to look a little nicer without the In [ ], Out [ ] sticking out next to every cell, so I'd like to get rid of these things. Thanks!

/r/IPython
https://redd.it/6ascqz
Checkbox in the admin that changes search_fields in real time

I would like to have a checkbox/toggle/etc under the search_fields box. When the checkbox is/isn't checked, it will change the get_search_results() function. I'd prefer for this to be done using JavaScript (no JQuery if possible!) so that the search results still change after the "Search" button is pressed, but without the page needing to reload.

Please let me know of any insights you might have. Thank you!

/r/django
https://redd.it/6b0mvh
sanitizing code for an exec command

I have a script that runs code provided by the user via an exec command. I know this can be considered a security hazard. As a precaution, before compiling I look for the term "import" anywhere in the code and if I find it the code is rejected. When code is executed, it's only given access to the builtins and a specific locals dictionary of functions and classes that it is meant to be able to interact with. The program is running on a remote server so the user cannot see any output to stdout.

Can anyone tell me if/how this is still unsafe? Is there something I'm not considering that would allow a user to run malicious code?

Thanks for any input.

/r/Python
https://redd.it/6b3zzy
Help with

class Place(models.Model):
address = models.CharField()

class Restaurant(Place):
unique_to_restaurant = models.CharField()
competitors = models.ManyToManyField(Place)

class Stadium(Place):
unique_to_stadium = models.TextField()

class Favorites(models.Model):
description = models.TextField()
favorites = models.ManyToManyField(Place)

I'm not great with databases and this is really giving me headaches. What is the best way to do this database? This is the gist of what I have currently, which functionally does exactly what I need. However the amount of queries are out of control. The children of Place share a lot of the same fields as Place but have some unique ones. I need to query place and favorites often. Favorites must be able to access the children of Place and the children of Place sometimes access the children of Place as well. Generic Foreign keys don't work (I don't think?) because the instance will be used more than once, so I would need to clone an instance. If anybody could help me out that would be absolutely amazing.
Edit: Sorry about the title! I was worried so much about the formatting of the code that I forgot to complete it and now I'm stuck with it :(

/r/django
https://redd.it/6b4cbe
Need help with making a Python Numpy function for calculating distance of line in grid cells

So the situation is basically that there is a unit square (with corners (0,0),(1,0),(0,1) and (1,1)) which is made up by n*n (so n-squared) square 'grid cells'.

I need to define a function F(n,a,b) that takes as input 2 points a and b in the square, for example a=[0.3,0.4] and b=[0.6,0.8], and n (the amount of grid cells per side), and that function should give me (in very good approximation) the total distance that a straight line between those points covers in each grid cell.

So it should return an ordered list of n*n length, each element of the list representing the distance that the straight line covers in that grid cell. The order should be so that the top left grid cell is the 0th element (first element) and then the grid cell to the right of it should be the second element, so row by row.

EXAMPLE:

Suppose we take the very simple and trivial situation n=2, so 4 grid cells, and the coordinates [0,0] and [1,1].

Then the function F(2,[0,0],[1,1]) should return [0, sqrt(2)/2, sqrt(2)/2, 0] (as a numpy array preferably, and of course sqrt(2)/2 as a numerical approximation).

The function should be reasonably well optimised for like 100*100 grid cells and should be very fast (<0.5 sec)

I really hope anyone can help me with this because I've been struggling with the problem for like 10 hours or more already.

/r/Python
https://redd.it/6b4cd2
Can anybody think of a way to do this with less variables?

def approxPi( error ):
num = 4
denom = 1
curr = num/denom
prev = 0
while abs(curr-prev)>error:
num *= -1
denom += 2
prev = curr
curr += num/denom
return curr

/r/Python
https://redd.it/6b56tn
[D] /r/MachineLearning should do an unconference

Reject, Accept or In Submission? Why not post your paper here?

This field is moving too fast to wait 6 months to see the great research that's happening (and that's assuming the review process isn't noisy).

/r/MachineLearning has already been an unofficial mini-conference for the papers submitted here. I find the best discussion happens when the authors show up to answer questions and address comments. It seems like this is happening mostly by coincidence. Someone will post a paper they like and the authors will happen to notice and engage in the discussion.

It stands to reason that if this happens so often by chance, there's enough critical mass of users here to create something more official. Let's get authors posting their work and discussing it on /r/MachineLearning, sort of like a virtual poster session.

/r/MachineLearning
https://redd.it/6ay2ac
Ordering of Fields using WTForms-Alchemy and Flask-Bootstrap quickform

I'm trying to use `WTForms-Alchemy` and the `Flask-Bootstrap` quickform. I am adding some fields and a submit button as described in the [WTForms-Alchemy Docs](https://wtforms-alchemy.readthedocs.io/en/latest/column_conversion.html#adding-overriding-fields) to the form created with `WTForms-Alchemy`.

Unfortunately the quickform function then renders the added fields before the automatically added by `WTForms-Alchemy` resulting the submit button to be in the middle of the form instead of the end.

Is there some fix to influence this ordering?

/r/flask
https://redd.it/6b5clt
I made a style-checking script in python for an introductory programming class and would love feedback!
https://github.com/IllyaStarikov

/r/Python
https://redd.it/6b6bmo
dealing with blanket try/excepts

I'm jumping on a new codebase that where the previous developer was aggressively solving problems by try/excepting throughout every code path. I'm slowly removing these but there're hundreds if not thousands of try/except/pass.

One thing I'd like to do to help me out as I go is to actually get these excepts to at least log. ideally I'd love it if I could define a python logging object that is logged to with a traceback every time an exception is raised *anywhere*. this needs to be automatic, as I dont want to manually go through it all right now.

any ideas?

/r/Python
https://redd.it/6b77cl
Need help dynamically creating buttons with different images and commands for each button.

Okay, I'll jump right into it. I need to dynamically create buttons that have different images and commands. I'm making a loot logging program for an MMO. You select an NPC from a list and the buttons populate with images associated with the loot the NPC can drop. When clicked, they add the text name (name of item dropped) to a list which is displayed in a label.

I have these buttons working properly (image changes when NPC changes, text added to list/label changes as well). But, I don't want to create these buttons statically for two reasons.

1. Different NPC's have different amounts of items they can drop so I want the number of buttons to depend on the length of the loot table.

2. I don't want my code to include dozens of manually written buttons, it seems sloppy and inefficient.

I have tried this code below

def create_loot_buttons():
global lootImages
buttons = len(currentDropTable)
for i in range(0, buttons):
itemPhoto.config(file="ItemSprites/" + itemIDs[currentDropTable[i]] + ".png")
lootImages.append(itemPhoto)
button = Button(dropTableButtons, image = lootImages[i])
button.pack()


Basically all of the buttons have the same image. I thought if I stored images in a list (see lootImages[]), it would save them to an index and I could create the buttons referencing the index number in this list, but it doesn't work).

If you need more info about my program, let me know. This is my first Python project, so the solution may be easy or staring me in the face but I've worked on it for awhile and searched to no avail.

Thank you.

/r/Python
https://redd.it/6b7aak
Can you help me choose a multiplatform frontend (mobile app/web) for a django backend project?

Hello

I'm working on a new project which needs to have both an iOS/Android app, and a web frontend.

I'm doing the backend with Django, but I don't know what to use for the mobile apps. I know there are framewroks (Cordova/Ionic) that can generate native apps with html-like code.

So, do you have any suggestion about which one to use? The app is quite simple, it's a product database wit CRUD, search, filter and a couple of graphs/reports, nothing fancy.

Should I go for a REST API and the frontends consume it, or directly the django app render the views for the different frontends?

Any help is well received.

Thanks pals.

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