2018-10-22         Joanne

Python iterate through object to create tables with Flask-SQLAlchemy

EDITEDI've been trying to create an class that will allow me to iterate through a list, or dictionary, and generate tables and rows.The code starts as follows:from flask_sqlalchemy import SQLAlchemy as safrom flask import Flask as flimport pymysqlpymysql.install_as_MySQLdb()app = fl(__name__)app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:the_other_stuff'app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] =FalseDEBUG =Truedb = sa(app)a=['some_stuff','gaaahhhh','lalala','rawr','pizza']class test(db.Model): __tablename__ = 'stuff' id = db.Column('id', db.Integer, primary...

 python                     2 answers                     17 view
 2018-10-22         Barbara

SyntaxError: multiple statements found while compiling a single statement

I'm in Python 3.3 and I'm only entering these 3 lines:import sklearn as skimport numpy as npimport matplotlib.pyplot as pltI'm getting this error:SyntaxError: multiple statements found while compiling a single statementWhat could I be doing wrong?Edit: If anyone comes across this question, the solution I found was to download Idlex and use its IDLE version, which allows multiple lines. Screenshot: http://imgur.com/AJSrhhD In the shell, you can't execute more than one statement at a time:>>> x = 5y = 6SyntaxError: multiple statements found while compiling a singl...

 python                     1 answers                     34 view
 2018-10-22         Lester

How to display definition of a class in Python

I was going through this question : How do I return the definition of a class in python?But I am unable to display the class definition. I am getting the below error:>>> class A:... pass...>>> import inspect>>> source_text = inspect.getsource(A)Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\**\Python\Python36\lib\inspect.py", line 968, in getsource lines, lnum = getsourcelines(object) File "C:\Users\**\Python\Python36\lib\inspect.py", line 955, in getsourcelines lines, lnum = findsour...

 python                     1 answers                     36 view
 2018-10-22         Leo

Rotate transformation on matplotlib axis in subplot

I am trying to create a four-panel figure where the bottom-left panel contains a scatter plot, and the other three panels contain histograms. The top-left will be a standard histogram across the x-dimension of the scatter, the bottom-right will be a 90° rotated histogram for the y-dimension. Both of these are easy to do in matplotlib.I am running into problems with the third histogram, which is to be a 45° rotated plot in the top-right of the figure giving the distribution of the differences between the x and y points. I have made such figures before by manually rotating an...

 python                     1 answers                     56 view
 2018-10-22         Simon

Negative Binomial Loss in Neural Network using Tensorflow / Keras

I'm working with a highly skewed count dataset that's typically fit with a negative binomial. I want to use the negative binomial as a loss functions in Keras or Tensorflow on a feed forward neural network. To my knowledge, after looking through available loss functions, such a function doesn't exist for keras or tensorflow (although I'm hoping I'm wrong and I just missed something). I've looked around and I've seen posts suggest just swapping out the loss to include the negative binomial, but it appears there's more to creating a custom loss in tensorflow than just swappin...

 python                     1 answers                     67 view
 2018-10-22         Godfery

Downloading Google Documents using api

this is shareable link link to fileid = 1wzCjl51u131v1KBgpbiKLJs8DPPakhXCFosfYjp7BY0so manage downloads documentation.file_id = '11wzCjl51u131v1KBgpbiKLJs8DPPakhXCFosfYjp7BY0'request = drive_service.files().export_media(fileId=file_id, mimeType='application/pdf')fh = io.BytesIO()downloader = MediaIoBaseDownload(fh, request)done = Falsewhile done is False: status, done = downloader.next_chunk() print ("Download %d%%." % int(status.progress() * 100))i saved the file as dd.py, run using f5 and got this error line 2, in r...

 python                     1 answers                     72 view
 2018-10-22         Herman

AWS / Python Lambda function checking if a query string is present

Within my lambda function, which takes in event api query strings, I want to check if one is present. The below works if it is:if event['queryStringParameters']['order'] == 'desc': file_names.append('hello')I have tried event['queryStringParameters']['order'] != null but if there is no order query string used the lambda function the function breaks causing a 502 response. How do I check if a query string is not used without it breaking? Always check if the dict contains an key before referencing it.if 'queryStringParameters' in event and 'order' in event['querySt...

 python                     1 answers                     74 view
 2018-10-22         Madge

SyntaxError: multiple statements found while compiling a single statement [Real Python Rookie]

This question already has an answer here: SyntaxError: multiple statements found while compiling a single statement 1 answer Behaviour of increment and decrement operators in Python 6 answers i-- doesn't work in python. -- is no existing operator. You could use i -= 1 for example. [XXX]

 python                     1 answers                     75 view
 2018-10-22         Giles

Pandas - expand dataframe using daterange

I have the following dataframe: name from amount days A 7/31/18 200 1 B 7/31/18 300 1 C 7/30/18 200 1 D 7/27/18 100 3 ...... G 7/17/18 50 1 H 7/13/18 150 4I'd like to expand it to this, where days does not equal 1: name from amount days A 7/31/18 200 1 B 7/31/18 300 1 C 7/30/18 200 1 D 7/29/18 100 3 D 7/28/18 100 3 D 7/27/18 100 3 ...... G 7/17/18 50 1 H 7/16/18 ...

 python                     2 answers                     77 view
 2018-10-22         Edith

Tweepy Cursor Search - User input and Mention

I'm trying to have a user input search and a set search parameter in the api.search function of tweepy. Essentially I want it like this: print("input search term to search within Tesla tweets")keyword = input()print("Thanks!")print("Tweets are cooking in the background now...")# create list to append tweets totweets = []# append all tweet data to listfor tweet in tweepy.Cursor(api.search, q="@tesla" + keyword, tweet_mode="extended", count=10000, since="2018-08-01").items(): tweets.append(tweet) word_string = repr(tweet)But that won't work it jus...

 python                     1 answers                     78 view
 2018-10-22         Denise

jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'posted'

I'm new to programming and recently a friend of mine gave me a project to work on in order to get myself familiar with the programming environment and language (python in this particular example).https://www.youtube.com/watch?v=QnDWIZuWYW0 I used this video as a beginners tutorial to help me understand how i'm supposed to proceed with my personal project. the code i wrote is exactly how it is written in the video, and i get an error.from flask import Flask, render_templateapp = Flask(__name__)posts = [ { 'author': 'Alon Salzmann', 'title': 'First Post', ...

 python                     2 answers                     79 view
 2018-10-22         Barnett

Moving label of seaborn colour bar

I've got a seaborn heat map with a labelled colour bar using:seaborn.heatmap(df, cbar_kws = {'label': 'Label for colour bar axis'})but the colourbar's label overlaps with its tick labels. Is there a way to reposition this label further from the ticks (in my case, further to the right) like the 'padding' options for a normal matplotlib plot? Unfortunately cbar_kws doesn't accept the labelpad argument. Therefore one way to add some padding to the labels will be to access the colorbar after it has been drawn.You can use ax.collections[0].colorbar to get access to the matplo...

 python                     1 answers                     80 view
 2018-10-22         Chad

How to retrieve specific information from a list in python?

I have a file from which I would like to retrieve specific information.First, I extracted the lines of interest and I put them in a list:array = []file_in = open("Traj.pdb", "r") for line in file_in: if line.startswith('TITLE'): array.append(line)I end up with a list like this: ['TITLE Protein in INSANE! Membrane UpperLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500000.00000\n', 'TITLE Protein in INSANE! Membrane UpperLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 LowerLeaflet>POPC:POPE:CHOL=31.0:41.0:28.0 t= 1500020.00...

 python                     2 answers                     86 view
 2018-10-22         Rock

Please clarify my mistake while doing the dry run

The following code is all correct but I have a doubt. While I am doing the dry run I find in my mind that it should show 2 as NOT a prime number. Someone please clarify how the code is running all good. num = 4if num > 1: for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") print(i,"times",num//i,"is",num) break else: print(num,"is a prime number")else: print(num,"is not a prime number") 2 is a prime number, but your code will shows 2 as not a prime numberHere is the fix:num = 4if num > 2: fo...

 python                     1 answers                     41 view
 2018-10-22         Merry

Calculate the area under each peak in a graph in python

I am trying to calculate the area under each peak in a graph that I plotted with a set of x and y co-ordinates, I don't have a function for (x,y), and so I haven't been able to find an appropriate method to do the same.the co-ordinates are { [10 10] [11 1] [12 7] [14 4] [16 8] [17 5]]}And y=0 for all the unmarked x values The area below two points consists of a 'square' part and a triangular part. The trick is to calculate them both separately and add them. Do this for all points, and you get a list of areas under peaks:points = [[10, 10], [11, 1], ...

 python                     3 answers                     42 view
 2018-10-22         Jeff

Time Complexity of while loop

What is the Best case, Worst Case and Average Case scenario of the followingcode segment ? def sumInts(n): Count = 0 while i < n: Count = count + n Return count It doesn't run because there's neither an i nor a count defined anywhere.In other words: Best case: crash, Worst case: crash, Average case: crash. [XXX]

 python                     1 answers                     43 view

Page 1 of 10095  |  Show More Pages:  Top Prev Next Last