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
What is wrong?

on terminal, it works good, but on ipython notebook it doesn't work.


```sumoProcess = subprocess.Popen([sumoBinary, "-c", "data/cross.sumocfg", "--tripinfo-output","tripinfo.xml", "--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr)```


UnsupportedOperationTraceback (most recent call last)
<ipython-input-15-80cdeae4d531> in <module>()
----> 1 sumoProcess = subprocess.Popen([sumoBinary, "-c", "data/cross.sumocfg", "--tripinfo-output","tripinfo.xml", "--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr)

/usr/lib/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
700 (p2cread, p2cwrite,
701 c2pread, c2pwrite,
--> 702 errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr)
703
704 try:

/usr/lib/python2.7/subprocess.pyc in _get_handles(self, stdin, stdout, stderr)
1126 else:
1127 # Assuming file-like object
-> 1128 c2pwrite = stdout.fileno()
1129
1130 if stderr is None:

/usr/local/lib/python2.7/dist-packages/ipykernel/iostream.pyc in fileno(self)
304
305 def fileno(self):
--> 306 raise UnsupportedOperation("IOStream has no fileno.")
307
308 def write(self, string):

UnsupportedOperation: IOStream has no fileno.


Python 2.7.6


/r/IPython
https://redd.it/4q13m9
[AF]cannot get environment variables set in Flask application

Hi,
I tried to set up sensitive information as environment variables in CentOS, and pass them to Flask config file used in main file, i.e. __init__.py . But it did not work. The Flask application is running under Apache.

I first edit /etc/environment as root user

MAIL_USERNAME="abcde@abc.com"

then logout, login again
Then verify MAIL_USERNAME is set by running

echo $MAIL_USERNAME

This works fine

And in configuration.py, this is how I set MAIL_USERNAME.

MAIL_USERNAME = os.environ.get('MAIL_USERNAME')

for testing purpose,
I print out MAIL_USERNAME

in __init__.py
print(MAIL_USERNAME)

Then from terminal, if I run

python3.4 __init__.py

it print out correct values of MAIL_USERNAME

However, if I tested on web browser, MAIL_USERNAME is just not set. it shows NONE. I verify this by looking Apache log.

Any idea of how this works would be really appreciated.

Thanks



/r/flask
https://redd.it/5dc4dn
Django and Celery on seperate servers without code on both

So far what I've read up the code needs to live on both the servers for Celery to run the task.

My question is, is it possible for me to run a Celery instance on a separate server (more resources for running high resource tasks), without having access to the code?

/r/django
https://redd.it/5jqqkq
Seemingly random Jupyter freezes

Very often once cell will get stuck with [*] and output nothing. I can't fix that unless I relaunch Jupyter each time, wich is atrocious.

The only thing that looks weird (I'm a noob with this, though) in the console is:

[W 05:01:23.008 NotebookApp] Saving untrusted notebook ipywidgets-master/docs/source/examples/Widget List.ipynb

Sometimes it's with "big" cells, and sometimes it's with a couple of lines, usually when making quick changes.

But when it's stuck, it doesn't matter if I erase the cell, create new ones, use others... the whole thing is stuck. Only resetting kernel or relaunching the app will unstuck it.

How can I fix this little nightmare? Jupyter, you looked so cool...

Edit with more info
===
Python 2.7.11

Jupyter 4.1.0

I was using particularly small snippets from the ipywidgets library, from their own examples. Right now I can't remember the exact code and I can't seem to replicate the freeze, but it froze also using regular print statements. If it happens again I'll update this thread with the exact code.

/r/IPython
https://redd.it/4penv7
Wondering how to create a variable list of strings that saves as json in the db

I'm new to django, and for the project I'm working on I need a list of strings that gets stored to the model.

Since those string are unique to each "post" it doesn't make much sense to store them in their own table, adding to the db lookup time.

So I was wondering how I can create a list of strings that when saved, get parsed to json, and stored as a single charfield to the db?

/r/django
https://redd.it/5jr6mt
Thinking of writing a tutorial. Would like some feedback.

As the title states, I'm thinking of writing a tutorial for Django. I would like to get some feedback and suggestions from the community regarding the content or if the tutorial itself is even a good idea.

The tutorial will be at an intermediate to advanced level. Though, I could write it in such a way that beginners will be able to understand it.

The idea is as follows:

## How to build a REST API in Django without a framework

The tutorial will guide a user through setting up a simple application with 3 - 5 models and make these models available through a RESTful API. Further tutorials may include how to utilize modern front end technologies to build a SPA if there is enough interest.

You may ask; "Why not use a REST framework like DRF or Tastypie?". Well, I do have an answer. They are not always the best choice for an application. In many cases, they are simply unnecessary. Here are the reasons why using a REST framework may not be right for an application:

### Bloat:

Most REST frameworks come with a lot of functionality that a given developer will not use. Some come with entire work flows that a developer will not use. Do you need all of that code that checks whether or not you are using session or JWT authentication if you're using Token authentication? Sending and receiving JSON data is a remarkably simple task. Django has a built in `JsonResponse` class that can do this for you. All you have to do is provide it a dict. The hard parts are (de)serialization, filtering, pagination, etc. Django has these facilities built in as well, but few people really know how to use them in conjunction with one another. My tutorial will aim to remedy this.

### Cognitive Load

Each REST framework has it's own idioms and best practices. It forces the developer to have to learn an entire new library. This forces the developer to pick a single framework and stick with it to become acquainted with it enough to use it effectively. A great example of this is how differently Tastypie and DRF handle permissions.

### Lack of simple flexibility

Most REST frameworks are completely customizable through overriding base classes. In DRF for instance, you can override the default serializer class to enforce your own custom behavior. The only problem is that knowing which classes to override and how requires an intimate knowledge of the framework or a healthy amount of research.

### Nested resources are a pain in the ass

DRF and Tastypie come with built in functionality to allow for the creation of nested resources. However, these never seem to work in exactly the way you want them to unless you override a class or method to make them do so. I have run into some nasty bugs with both of these frameworks when trying to create a nested resource. Django's ORM does a really great job of taking rows in your database and transforming them into simple objects. It really doesn't need to be difficult to nest them, especially when they have relations.

### Implicit non-declaritive classes

This is fine if you are tacking on an API endpoint as an afterthought. However, if the API is the core of the application, it's generally not such a great idea in my opinion. Both Tastypie and DRF allow the developer to simply provide the serializer/resource class with a model or queryset and then all the work happens behind the scenes. You can declare any fields that you want excluded and even have some options for enforcing simple validation and permissions. What if you need to enforce validation on a field to field basis? What if you need two similar fields to have very different validation methods? What if you need to validate fields based on other fields? What if you need to also perform permissions checks based on a certain field in conjunction with the requesting user? DRF and Tastypie both have answers to all of these questions, though they may not be very simple answers.

## Caveats

I am in no way trying to bash on Tastypie or DRF. They are both very useful and valuable tools if you need an all e
ncompassing solution that uses a majority of their features. I just believe that many applications would be less complex if they chose not to use these tools. Both DRF and Tastypie do a great job and you should still use them if you like to use them. I simply want to offer an alternative tool for your tool belt.

## The Tutorial

Ok, here's the meat and potatoes:

I want to write a tutorial that walks a reader through creating a REST API using Marhsmallow schemas and two custom view classes that inherit from `django.views.generic.View`. These view classes will handle pagination, filtering, object creation, object deletion, listing objects, getting a single object, and permissions. The schema will handle data validation and (de)serialization.

The tutorial will also cover creating nested resources in the form of the following: `/api/v1/users/<id>/posts/` which returns a paginated and filterable list of a user's posts.

I know many of you in this sub already know how to do this, but the tutorial will be written for those who do not.

So, if you could help me by answering any of the following questions, that would be fantastic:

* What would you like to see in a Django REST API tutorial in general?
* Have you tried to build a REST API in Django without a framework before?
* What are some API features you have found difficult to implement in the past?
* Have you used Marshmallow before?
* What are your thoughts on the method I am presenting here?
* Do you think this tutorial would be helpful for an intermediate Django developer?
* What kind of application do you think would be best to demonstrate this concept?
* Do you think this tutorial is a good idea? Why or why not?

I'm thinking I'll probably put the tutorial in a Github repo so others can help if they so wish. I am also building another website where the tutorial will be posted. I may also put it up on medium or something like that.

If you have any questions of your own, I'll be happy to answer them.

/r/django
https://redd.it/5js4yf
Pythonanywhere. All static files are working except in django admin?

What am I doing wrong here? I am trying to use the django-material library. It works locally but not on python anywhere. I have run `python manage.py collectstatic` I can't figure out how to get the server to point to the admin static files! Below is my settings.py

INSTALLED_APPS = (
'material',
'material.admin',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
......)

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media',
],
},
},

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

/r/djangolearning
https://redd.it/5ieh7z
Django-page-cms gets official inline page editing support
http://django-page-cms.readthedocs.io/en/latest/edit-content.html

/r/django
https://redd.it/5jl3ej
[AF] Flask Admin not displaying Foreign Key column

I'm building a blog with Flask. I use Flask-Admin for setting up an administrative interface. Everything is fine, but I cannot see the "tags" field in the Post form. This is defined as a relationship/FK in my Post model.

Please see below my model file and admin configuration. Appreciate any idea.

Thanks!

##### MODELS.PY
tags = db.Table('post_tag',
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
db.Column('post_id', db.Integer, db.ForeignKey('post.id'))
)


class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime, default=datetime.utcnow)
title = db.Column(db.String(140))
slug = db.Column(db.String(200))
content = db.Column(db.Text)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
_tags = db.relationship('Tag', secondary=tags,
backref=db.backref('posts', lazy='dynamic'))

@observes('title')
def compute_slug(self, title):
self.slug = re.sub('[^\w]+', '-', title.lower())

@staticmethod
def newest(num):
return Post.query.order_by(desc(Post.date)).limit(num)

@property
def tags(self):
return ",".join([t.name for t in self._tags])

@tags.setter
def tags(self, string):
if string:
self._tags = [Tag.get_or_create(name) for name in string.split(',')]

def __repr__(self):
return "<Post '{}': '{}'>".format(self.title,self.date)


class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(25), nullable=False, unique=True, index=True)

@staticmethod
def get_or_create(name):
try:
return Tag.query.filter_by(name=name).one()
except:
return Tag(name=name)

@staticmethod
def all():
return Tag.query.all()
def __str__(self):
return self.name

def __repr__(self):
return self.name



##ADMIN.PY
admin = Admin(app, name='Admin', template_mode='bootstrap3')

class CKTextAreaWidget(TextArea):
def __call__(self, field, **kwargs):
if kwargs.get('class'):
kwargs['class'] += ' ckeditor'
else:
kwargs.setdefault('class', 'ckeditor')
return super(CKTextAreaWidget, self).__call__(field, **kwargs)

class CKTextAreaField(TextAreaField):
widget = CKTextAreaWidget()

class PostAdmin(sqla.ModelView):
form_overrides = dict(content=CKTextAreaField)
create_template = 'blog/ckeditor.html'
edit_template = 'blog/ckeditor.html'
form_excluded_columns = ('slug')
def is_accessible(self):
return current_user.is_authenticated
admin.add_view(PostAdmin(Post, db.session))

class TagAdmin(sqla.ModelView):
def is_accessible(self):
return current_user.is_authenticated
admin.add_view(TagAdmin(Tag, db.session))

class UserAdmin(sqla.ModelView):
def is_accessible(self):
return current_user.is_authenticated
admin.add_view(UserAdmin(User, db.session))

/r/flask
https://redd.it/5dbvbi
How to authenticate IMAP using oauth tokens?

I'm building a flask app that authenticates a user using Google's OAuth and then connects to gmail using IMAP for further email processing, however, i'm having trouble with the whole process.

My current code is as follows. I'm able to sign the user into google and access their user information, such as their email, and obtain an access token, however, i'm not sure where to go from there. Any/all help is greatly appreciated.

from flask import Flask, request, url_for, session, redirect, jsonify
from flask_oauth import OAuth
import json
import imaplib
import email
from bs4 import BeautifulSoup



GOOGLE_CLIENT_ID = 'MY ID'
GOOGLE_CLIENT_SECRET = 'MY SECRET'
REDIRECT_URI = '/authorized' # one of the Redirect URIs from Google APIs console

SECRET_KEY = 'Uber'
DEBUG = True

app = Flask(__name__)
app.secret_key = 'Uber'
oauth = OAuth()

google = oauth.remote_app('google',
base_url='https://www.google.com/accounts/',
authorize_url='https://accounts.google.com/o/oauth2/auth',
request_token_url=None,
request_token_params={'scope': 'https://www.googleapis.com/auth/userinfo.email',
'response_type': 'code'},
access_token_url='https://accounts.google.com/o/oauth2/token',
access_token_method='POST',
access_token_params={'grant_type': 'authorization_code'},
consumer_key=GOOGLE_CLIENT_ID,
consumer_secret=GOOGLE_CLIENT_SECRET)


@app.route('/')
def index():
access_token = session.get('access_token')
if access_token is None:
return redirect(url_for('login'))

access_token = access_token[0]
from urllib2 import Request, urlopen, URLError

headers = {'Authorization': 'OAuth '+access_token}
req = Request('https://www.googleapis.com/oauth2/v1/userinfo',
None, headers)
try:
res = urlopen(req)
except URLError, e:
if e.code == 401:
# Unauthorized - bad token
session.pop('access_token', None)
return redirect(url_for('login'))
return res.read()
j = json.loads(res.read())
email_address = j['email']
print email_address, access_token
return "Hello World"


@app.route('/login')
def login():
callback=url_for('authorized', _external=True)
return google.authorize(callback=callback)



@app.route(REDIRECT_URI)
@google.authorized_handler
def authorized(resp):
access_token = resp['access_token']
session['access_token'] = access_token, ''
return redirect(url_for('index'))


@google.tokengetter
def get_access_token():
return session.get('access_token')

def Uber_Cost(email_address, access_token):

# this function is completed, i'm just not sure how to authenticate IMAP using the oauth from above

/r/flask
https://redd.it/5dc81w