python-2.7

Efficiently extract lines containing a string in Python

独自空忆成欢 提交于 2021-01-29 18:08:18
问题 Sometimes I need to get only lines containing a certain string from a text file (e.g., while parsing a logfile). I usually do it this way: with open(TEXTFILENAME,'r') as f: contents = f.readlines() targets = [s for s in contents if FINDSTRING in s] However, I saw there's a possible two-liner: with open(TEXTFILENAME,'r') as f: targets = [s for s in f.readlines() if FINDSTRING in s] I wonder if the second method is more efficient, whether the readlines() function in this case act as an iterator

Import error despite __init__.py exists in the folder that contains the target module

天大地大妈咪最大 提交于 2021-01-29 18:05:28
问题 I have the following directory structure inside a virtualenv : /dir_a/dir_b/__init__.py /dir_a/dir_b/module_1.py /dir_a/dir_b/module_2.py /dir_a/dir_c/__init__.py /dir_a/dir_c/module_3.py /dir_a/__init__.py /dir_a/module_4.py Inside module_4.py , I can successfully import module_1.py , module_2.py and module_3.py . On the other hand, I cannot import module_4.py within module_3.py (e.g. import dir_a.module_4 ). It complains: "No module named dir_a.module_4" What am I missing here? Do I need to

img2pdf.py “no module named Image”

為{幸葍}努か 提交于 2021-01-29 17:39:21
问题 Hello I am running Python 2.7 64 bit on windows 7. I've found a python script online called img2pdf.py which could be very useful to me but I can't run it. I've installed the Pillow library for win64 (from http://www.lfd.uci.edu/~gohlke/pythonlibs/) and Image.py is present in "C:\Python27\Lib\site-packages\PIL" I've downloaded the script to a directory containing my image, and python is on my path but when I type python img2pdf.py myimage.j2k at the command prompt I get an error that states

Running two lines of code in python at the same time?

Deadly 提交于 2021-01-29 15:20:29
问题 How would I run two lines of code at the exact same time in Python 2.7? I think it's called parallel processing or something like that but I can't be too sure. I'm asking here because I'm not even sure what to Google for...So if this is a redundant question, I apologize in advanced. Thanks in advance! 回答1: You can use either multi threading or multiprocessing. You will need to use queues for the task. The sample code below will help you get started with multi threading. import threading

Python Multithreading missing data

喜夏-厌秋 提交于 2021-01-29 13:29:11
问题 useI am working on a python script to check if the url is working. The script will write the url and response code to a log file. To speed up the check, I am using threading and queue. The script works well if the number of url's to check is small but when increasing the number of url's to hundreds, some url's just will miss from the log file. Is there anything I need to fix? My script is #!/usr/bin/env python import Queue import threading import urllib2,urllib,sys,cx_Oracle,os import time

What does ''HmacAuthV1Handler' object has no attribute 'presign'' mean?

北城余情 提交于 2021-01-29 12:52:50
问题 I want to use boto2 (2.49) to create a presigned url that uses Sign v4. Here is my function: c = S3Connection(access_key, secret_key) return c.generate_url_sigv4( expires_in=long(expiry), method='PUT', bucket=bucket, key=path ) When I run it, I got this error: 2019-06-18 12:57:51,002 boto [DEBUG]:Using access key provided by client. 2019-06-18 12:57:51,002 boto [DEBUG]:Using secret key provided by client. Traceback (most recent call last): File "scripts/gen_url.py", line 42, in <module> main

Python class inherited singleton inits instance on every call

这一生的挚爱 提交于 2021-01-29 12:26:33
问题 I'm trying to implement class inherited singleton as described here (Method 2). Going over the question and the extensive chosen answer I tried to implement the following: class Singleton(object): _instance = None def __new__(cls, *args, **kwargs): if not isinstance(cls._instance, cls): cls._instance = object.__new__(cls, *args, **kwargs) cls._instance._initialized = False return cls._instance class A(Singleton): def __init__(self): print "Init is called" class B(Singleton): def __init__(self

keras error:Error when checking target: expected dense_2 to have shape (2,) but got array with shape (1,)

北战南征 提交于 2021-01-29 11:53:46
问题 I have tried to write some example with keras,but some error happenError when checking target: expected dense_2 to have shape (2,) but got array with shape (1,) I have tried to change the input_shape but it doesn't work import keras from keras.models import Sequential from keras.layers import Dense from keras.optimizers import SGD from sklearn.preprocessing import LabelBinarizer from sklearn.model_selection import train_test_split import numpy print "hello" input=[[1],[2],[3],[4],[5],[6],[7],

Python module turtle not importing correctly

六眼飞鱼酱① 提交于 2021-01-29 11:33:32
问题 This is my fist time using the turtle module in python but i can't seem to import it? Here's my code: import turtle turtle.shape("turtle") turtle.speed(1) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.exitonclick() I run this as $ python3 example.py And I get $ python3 example.py Traceback (most recent call last): File "example.py", line 1, in <module> from turtle import * File "/usr/lib

Google API v4 for Python KeyError: '_module'

扶醉桌前 提交于 2021-01-29 10:36:41
问题 I'm not really a developer, but I need to engineer tools often. Having trouble trying to set up Google API v4 for Python: https://developers.google.com/sheets/api/quickstart/python Followed all instructions. When executing the quickstart.py: # python quickstart.py Traceback (most recent call last): File "quickstart.py", line 7, in <module> from oauth2client import file, client, tools ImportError: No module named oauth2client To fix I installed oauth2client: # pip install oauth2client