python-2.7

return first non NaN value in python list

社会主义新天地 提交于 2021-02-08 19:57:54
问题 What would be the best way to return the first non nan value from this list? testList = [nan, nan, 5.5, 5.0, 5.0, 5.5, 6.0, 6.5] edit: nan is a float 回答1: If you're doing it a lot, put it into a function to make it readable and easy: import math t = [float('nan'), float('nan'), 5.5, 5.0, 5.0, 5.5, 6.0, 6.5] def firstNonNan(listfloats): for item in listfloats: if math.isnan(item) == False: return item firstNonNan(t) 5.5 回答2: You can use next, a generator expression, and math.isnan: >>> from

gunicorn log-config access_log_format

老子叫甜甜 提交于 2021-02-08 14:00:07
问题 I want gunicorn to log JSON in my docker container. I want to use --access-logformat in the config file. Trying to add format or access_log_format or access_logformat does not configure the logger. How do I configure the access_log_format to output JSON? http://docs.gunicorn.org/en/stable/settings.html#access-log-format gunicorn_logging.conf [loggers] keys=root, gunicorn.error, gunicorn.access [handlers] keys=console [formatters] keys=json [logger_root] level=INFO handlers=console access_log

Using celery with Flask app context gives “Popped wrong app context.” AssertionError

空扰寡人 提交于 2021-02-08 13:45:41
问题 I'm more or less using the setup to run Celery tasks using your flask app context from here: http://flask.pocoo.org/docs/0.10/patterns/celery/ I'm getting the same error message as Create, manage and kill background tasks in flask app but, I'm getting it in the actual worker where the Celery task is being executed. Here is the trace: worker_1 | Traceback (most recent call last): worker_1 | File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 240, in trace_task worker_1 | R

Why does communicate deadlock when used with multiple Popen subprocesses?

我的未来我决定 提交于 2021-02-08 12:53:08
问题 The following issue does not occur in Python 2.7.3. However, it occurs with both Python 2.7.1 and Python 2.6 on my machine (64-bit Mac OSX 10.7.3). This is code I will eventually distribute, so I would like to know if there is any way to complete this task that does not depend so dramatically on the Python version. I need to open multiple subprocesses in parallel and write STDIN data to each of them. Normally I would do this using the Popen.communicate method. However, communicate is

Locally hosting Django project

走远了吗. 提交于 2021-02-08 12:52:25
问题 So I'm trying to locally host a Django project that I am working on. I'm looking to have the ability to have all computers who are connected to the local network be able to access this django app/webapp. Sort of like a central, internal, local only website/hub. And I was wondering how I could go about setting up my project to be able to do this. Would I need to setup a webserver to accomplish this functionality? If so would you be able to recommend any? Can I do this within Django itself

Using zip_longest on unequal lists but repeat the last entry instead of returning None

我的梦境 提交于 2021-02-08 12:15:31
问题 There is an existing thread about this Zipping unequal lists in python in to a list which does not drop any element from longer list being zipped But it's not quite I'm after. Instead of returning None , I need it to copy the previous entry on the list. Is this possible? a = ["bottle","water","sky"] b = ["red", "blue"] for i in itertools.izip_longest(a,b): print i #result # ('bottle', 'red') # ('water', 'blue') # ('sky', None) # What I want on the third line is # ('sky', 'blue') 回答1:

Curve fitting with broken power law in Python

一笑奈何 提交于 2021-02-08 12:12:13
问题 Im trying to follow and re-use a piece of code (with my own data) suggested by someone named @ThePredator (I couldn't comment on that thread since I don't currently have the required reputation of 50). The full code is as follows: import numpy as np # This is the Numpy module from scipy.optimize import curve_fit # The module that contains the curve_fit routine import matplotlib.pyplot as plt # This is the matplotlib module which we use for plotting the result """ Below is the function that

How to get notify whenever a file is started to upload in FTP server?

蹲街弑〆低调 提交于 2021-02-08 11:59:26
问题 I want to get notify whenever the file is started to upload in FTP server and whenever there is no file upload in the ftp directory more than 10 minute. Is there any method to tell me that file is started to upload in FTP server (in Python)? 回答1: I think the below code will solve your problem you just need to connect it with your server. You can try in your local directory also. import os, time path_to_watch = "test_ftp/" flag = 0 before = dict ([(f, None) for f in os.listdir (path_to_watch)]

How to run python programs in pycharm with passing parameters?

牧云@^-^@ 提交于 2021-02-08 11:58:18
问题 I am currently having problem running python programs in pycharm, I generally used to right click and run the program. I want to run the program like this: python hrllo.py rahulkapoo vit university Here is the error I get: Traceback (most recent call last): File "C:/Users/rahul/PycharmProjects/untitled/hrllo.py", line 107, in <module> aggregate() File "C:/Users/rahul/PycharmProjects/untitled/hrllo.py", line 88, in aggregate br = login() File "C:/Users/rahul/PycharmProjects/untitled/hrllo.py",

Python append words to a list from file

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-08 11:42:19
问题 I'm writing a program to read text from a file into a list, split it into a list of words using the split function. And for each word, I need to check it if its already in the list, if not I need to add it to the list using the append function. The desired output is: ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder'] My output