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
How to use chromeless with Python?

Hello,
I wonder if there is any way to use this chromeless with Python?
https://github.com/graphcool/chromeless
It looks like a great tool but unfortunately i do not know if there is any way to use it with python.
Thank you

/r/Python
https://redd.it/6r2lsd
Which platform to quickly build a (paid) python-based API

Hi!

I have a python algorithm and I want to make a scalable web interface / API for that algorithm.

**First I would like to enable people to make call to my algorithm, either on the web interface or through a web api.**

- Which platform would be the best for me ? AWS lambda ? google app engine ? pythonanywhere ? something else ?

**Then if it works well I would like to create billing plans such as "ipinfo.io" (like 20 euros per month for X calls / X CPU time used...)**

- I know how to code in python / php but I don't see myself building the whole used authentification / billing plans.. payment tracking. Is there an easy platform to set up paid apis ?

Finally.. i'm poor so I can't afford to spend too much. I've seen 3scale for example but 750 euros is too much, especially considering that my algorithm is quite simple (like just a function with some arguments processed by a single python function, which gives back one or multiple numbers..).

Thanks!

/r/Python
https://redd.it/6r2r88
Django app that returns value based on what users choose in form (x-post r/django)

I did the tutorial on the Django website and made the polls app. I've since added static pages and gained a better understanding of how URLs and views work.
I've written a Python function that is essentially a logistic regression predictor. It takes in an array and based on that array returns a value.
I want to build off the polls app in Django, and make it so that instead of typing in an array, users can select from the poll form, and then the web app returns the value.
How would I go about starting this?

/r/djangolearning
https://redd.it/6r0q9e
Is this no longer the place to post notifications about Python library updates?

Lately, when I post notices about a new release of one of my Python libraries here, the post is pretty reliably downvoted. If people really don't care about the libraries I'm putting out, that's fine, but I just wanted to make sure I'm not violating a rule of some kind.

/r/Python
https://redd.it/6r3sbk
[HELP] Left Join between multiple databases

I want to do a left join with another db, What would be the django equivalent of this query ?

USE PARTNER_DB;
SELECT CLIENT_ID, PARTNER.FIRST_NAME, PARTNER.LAST_NAME,
RPM, DRAWDOWN_PROTECTION, CONCAT(EMPLOYEE_DB.EMPLOYEE.FIRST_NAME, " ", EMPLOYEE_DB.EMPLOYEE.LAST_NAME),
EMPLOYEE_DB.EMPLOYEE.MAIL
FROM PARTNER
LEFT JOIN EMPLOYEE_DB.EMPLOYEE
ON RM_ID = EMPLOYEE_DB.EMPLOYEE.EMP_ID
WHERE CLIENT_ID = 'X1234'


I am using multiple databases in my project and both of these databases are not managed by django.

I tried to dig into django documentation but was unable to find a way to achieve this, instead i bumped into this
[https://docs.djangoproject.com/en/1.11/topics/db/multi-db/#no-cross-database-relations][1].

I am sure that there will be a hack / workaround this. Looking forward to your solutions.



**These are two different db's not managed by django.**

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'PARTNER_DB',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
},
'employeedb': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'EMPLOYEE_DB',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}

**My db models are**

class Partner(models.Model):
client_id = models.CharField(db_column='CLIENT_ID', primary_key=True, max_length=25)
first_name = models.CharField(db_column='FIRST_NAME', max_length=50)
last_name = models.CharField(db_column='LAST_NAME', max_length=50, blank=True, null=True)
mobile = models.CharField(db_column='MOBILE', max_length=10, blank=True, null=True)
active_from = models.DateField(db_column='ACTIVE_FROM')
rm_id = models.CharField(db_column='RM_ID', max_length=10, blank=True, null=True)
preferred_name = models.CharField(db_column='PREFERRED_NAME', max_length=50, blank=True, null=True)
rpm = models.IntegerField(db_column='RPM', blank=True, null=True)
drawdown_protection = models.IntegerField(db_column='DRAWDOWN_PROTECTION', blank=True, null=True)
depository_id = models.CharField(db_column='DEPOSITORY_ID', max_length=45, blank=True, null=True)
dob = models.DateField(db_column='DOB', blank=True, null=True)
account_type = models.CharField(db_column='ACCOUNT_TYPE', max_length=25, blank=True, null=True)
account_status = models.CharField(db_column='ACCOUNT_STATUS', max_length=25, blank=True, null=True)
first_holder_pan_number = models.CharField(db_column='FIRST_HOLDER_PAN_NUMBER', max_length=45, blank=True, null=True)
correspondence_address = models.CharField(db_column='CORRESPONDENCE_ADDRESS', max_length=1000, blank=True, null=True)
permanent_address = models.CharField(db_column='PERMANENT_ADDRESS', max_length=1000, blank=True, null=True)
nominee_name = models.CharField(db_column='NOMINEE_NAME', max_length=100, blank=True, null=True)
second_holder_name = models.CharField(db_column='SECOND_HOLDER_NAME', max_length=100, blank=True, null=True)
second_pan_number = models.CharField(db_column='SECOND_PAN_NUMBER', max_length=45, blank=True, null=True)
mail_id = models.CharField(db_column='MAIL_ID', max_length=200, blank=True, null=True)
bank_name = models.CharField(db_column='BANK_NAME', max_length=1000, blank=True, null=True)
bank_account_number = models.CharField(db_column='BANK_ACCOUNT_NUMBER', max_length=50, blank=True, null=True)
bank_micr_code = models.CharField(db_column='BANK_MICR_CODE', max_length=50, blank=True, null=True)
bank_ifsc_code = models.CharField(db_column='BANK_IFSC_CODE', max_length=50, blank=True, null=True)

class Meta:
managed = False
db_table = 'PARTNER'

class Employee
(models.Model):
emp_id = models.IntegerField(db_column='EMP_ID', primary_key=True)
first_name = models.CharField(db_column='FIRST_NAME', max_length=100)
last_name = models.CharField(db_column='LAST_NAME', max_length=100)
mail = models.CharField(db_column='MAIL', max_length=100)

class Meta:
managed = False
db_table = 'EMPLOYEE'


[1]: https://docs.djangoproject.com/en/1.11/topics/db/multi-db/#no-cross-database-relations


/r/django
https://redd.it/6r2kwt
Just wanna say thanks to flask,sqlalchemy,postgres and redis. I managed to create a server app that handles 7 Million requests per day. (probably more)

Proof:
http://imgur.com/a/n3mfG

I think it's one of my greatest achievement ever since we were only a 2-man team in the server dev.



/r/flask
https://redd.it/6r2gir
A Julia set fractal with python

/r/Python
https://redd.it/6r4rb4
Clustering multiple GPUs from different machines and remotely run Jupyter Notebook

Hello everyone, I am a newcomer here in this community.
I have only 2 GB GPU but my friend has 4GB so I generally train my model on his machine. I normally use Jupyter Notebook and I code in Python. Recently I came to know about "Running a notebook server" and I set up that. Now I can remotely run a jupyter notebook on my machine (client) while the resources are used from my friend's machine (server).

4 GB of GPU is also not sufficient for me. I am curious if I could remotely use GPUs from many of my friends' machine and cluster them and then remotely run the jupyter notebook. Its similar to the server-client model that we previously created but I wish to extend it to multiple "shared-servers" so that I can use all of their GPU's in collaborative and distributive fashion. It is a kind of 'many-to-one' server (many) and client (one) model.

Can anybody help me how can I achieve that in Jupyter Notebook server ?
Or is there any option to remotely use GPU from different machines and run my python code remotely ?

Thanks

LINK - jupyter-notebook.readthedocs.io/en/latest/public_server.html

/r/JupyterNotebooks
https://redd.it/6r76md
My first program for today, feeling creative.

>>> true=False
>>> true!=False
False
>>> True!=False
True

/r/Python
https://redd.it/6raocg
I have set up my React frontend. Need a backend to manage user accounts and store data - is Django a good progression?

As mentioned in the title, I have set up the base foundation for my webapp using React/Redux for my front end.

I would like to take it a step further by adding a backend so that users can create accounts, login, and save data.

I have been looking into Firebase as it's recommended but it feels like I won't learn as much about backend / databases since Firebase does a lot of it behinds the scene.

I am considering Django and was wondering if this would be a good progression for me? Where does Django or Firebase fit into the picture?

I have zero experience with anything backend related but want to learn more.

/r/django
https://redd.it/6r9t4a
Are Django session variables secure?

I am creating an app for use in our organization that will login users based on their Office 365 credentials using OAuth2.0. I am looking at fetching an access token that I will store in a session variable. Right now, though, I am just saving 'oauth_state' into a session variable and setting that variable to 'authorized'. Upon loading of each (non-login view), I am checking whether 'oauth_state' is set to 'authorized'. I'm probably going to change this and store a token the variable and use that to query the MSGraph API for a 200 response upon each page load. Anyways, here is an example of what I am doing:

@never_cache
def authorization(request):
microsoft = OAuth2Session(client_id,scope=scope,redirect_uri=redirect_uri)
token = ""
try:
users = 'https://graph.microsoft.com/v1.0/me' ##msgraph query url-
##This query is purelyjust used to
##authenticate user!
token = microsoft.fetch_token(token_url,client_secret=client_secret,code=request.GET.get('code', '')) ##Code is the authorization code present
##in request URL
header = {'Authorization': 'Bearer ' + token['access_token']}
response = requests.get(url = users, headers = header)

if int(response.status_code) != 200: ##if status code is not 200, then authentication failed. Redirect to login.
print ('Not validated. Return to login.')
request.session.flush()
return redirect('http://localhost:8000/login')
except Exception as e:
print ('User not does not have authentication rights')
request.session.flush()
return redirect('http://localhost:8000/login')

request.session['oauth_state'] = 'authorized'
response = HttpResponseRedirect('http://localhost:8000/search')
return response

I am then using this to check if 'oauth_state' is set to 'authorized'. However, I may change this so that the token is used to query the MS Graph API in each function in order to check if the user has proper permissions or not. Here's an example of what I am doing:

def search(request):
try:
if (str(request.session['oauth_state']) != 'authorized'):
print ('Not authorized')
request.session.flush()
return redirect('http://localhost:8000/login')
except Exception as e:
print ('Not authorized')
request.session.flush()
return redirect('http://localhost:8000/login')
<rest of code>

How insecure is this? Should I possibly be passing in the token to the response header? Or should I get rid of this method, and use django's standard auth and login system? I really appreciated the benefits of OAuth2.0, but if this method compromises our security, I might scrap it.

/r/django
https://redd.it/6r8hg8
Cannot put templatetags at the root of the project

I'm trying to put my *templatetags* folder, which contains a *menu* common to all the applications, at the root of the project.

But I'm still getting a: **TemplateSyntaxError**: 'menu' is not a registered tag library

Here is the relevant part of my *setting.py*:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# Look for base template at root of project
'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
'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',
],
# Look for base templatetags at root of project
'libraries': {
'project_tags': 'templatetags.menu',
}
},
},
]

Is something wrong with my configuration?


/r/django
https://redd.it/6rf6nf
PSA: Don't use the unofficial Windows binaries in an application handling sensitive data.

I've noticed, more and more often, that people are using the [~gohlke binaries](http://www.lfd.uci.edu/~gohlke/pythonlibs/) in serious production applications. I've seen it be mentioned or even recommended by a lot of people who ought to know better.

That's a terrible idea for a lot of reasons -- they're built by a third party and are explicitly marked for testing purposes only -- but there's a huge problem a lot of people ignore, and I can't seem to find any conversations about: they're served over HTTP, and only HTTP. HTTPS connections get redirected to HTTP.

This means that a man-in-the-middle attack could trivially redirect them to a booby-trapped version of the binary. Whenever you import the binary, it might open a TTY on your machine and let an attacker connect to it at will. Or it could traverse your hard drive and `open(file, 'rb')` every file to wipe them. Those are just two really uncreative examples -- you can imagine what arbitrary code execution on your production systems could do.

If your application handles anyone's personal/sensitive data, or if it's in production anywhere, please take the extra time to compile your binaries from scratch, even though it can be a huge pain. MITM attacks are very easy for unskilled attackers to pull off, and the results could be disastrous.

/r/Python
https://redd.it/6rdd21
Python 101 on the Web

Last year I gave my book, **Python 101**, away for free on Reddit and then made it free on [Leanpub](https://leanpub.com/python_101/) permanently. I've been meaning to make Python 101 available as a website too but only recently have I gotten that accomplished. You can check it out here: http://python101.pythonlibrary.org/

For those of you who like the nitty gritty details, I ended up using Sphinx because I write all my books in ReStructuredText. Sphinx uses the Alabaster theme by default, which I didn't care for. I wanted a theme that has previous/next buttons on by default and that was mobile friendly. The only theme I found for that was the [Read the Docs](http://docs.readthedocs.io/en/latest/theme.html) theme. I will probably end up tweaking it a bit to make it look a little more unique. If you have any tips for that, feel free to let me know.

/r/Python
https://redd.it/6rcbxw