python-2.6

How to format an output in Python?

丶灬走出姿态 提交于 2019-12-12 10:12:33
问题 I am having difficulty in formatting some code in Python: My code is here: keys = ['(Lag)=(\d+\.?\d*)','\t','(Autocorrelation Index): (\d+\.?\d*)', '(Autocorrelation Index): (\d+\.?\d*)', '(Semivariance): (\d+\.?\d*)'] import re string1 = ''.join(open("dummy.txt").readlines()) found = [] for key in keys: found.extend(re.findall(key, string1)) for result in found: print '%s = %s' % (result[0],result[1]) raw_input() So far, I am getting this output: Lag = 1 Lag = 2 Lag = 3 Autocorrelation Index

Disabling nagle in python: how to do it the right way?

怎甘沉沦 提交于 2019-12-12 09:59:16
问题 I need to disable nagle algorithm in python2.6. I found out that patching HTTPConnection in httplib.py that way def connect(self): """Connect to the host and port specified in __init__.""" self.sock = socket.create_connection((self.host,self.port), self.timeout) self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True) # added line does the trick. Obviously, I would like to avoid patching system lib if possible. So, the question is: what is right way to do such thing? (I'm pretty new

How to “overload” python's print function “globally”?

空扰寡人 提交于 2019-12-12 08:37:30
问题 I am using python 2.6.6 and I need to overload the default python print function. I need to do it because this code may be used on a system where a built-in function has to be used to generate output, otherwise no output is displayed. So, just for example, if you have a python script like this: from __future__ import print_function def NewPrint(Str): with open("somefile.txt","a") as AFile: AFile.write(Str) def OverloadPrint(): global print print = NewPrint OverloadPrint() print("ha") It works

How to set up a resource shared by several unit tests?

依然范特西╮ 提交于 2019-12-12 08:30:02
问题 In Python, how can I have one setup (which may contain expensive function calls) for a whole set of unit tests? Example: import unittest class Test1(unittest.TestCase): def setUp(self): print "expensive call" def test1(self): self.assertEqual(1, 1) def test2(self): self.assertEqual(1, 1) if __name__ == "__main__": unittest.main() Will run the expensive call twice: $ python unittest.py expensive call .expensive call . ---------------------------------------------------------------------- Ran 2

Python multiprocessing: synchronizing file-like object

浪尽此生 提交于 2019-12-12 07:58:40
问题 I'm trying to make a file like object which is meant to be assigned to sys.stdout/sys.stderr during testing to provide deterministic output. It's not meant to be fast, just reliable. What I have so far almost works, but I need some help getting rid of the last few edge-case errors. Here is my current implementation. try: from cStringIO import StringIO except ImportError: from StringIO import StringIO from os import getpid class MultiProcessFile(object): """ helper for testing multiprocessing

python 2.6 subprocess enter psql password C shell

家住魔仙堡 提交于 2019-12-12 05:49:11
问题 I am not able to install any additional modules on an AIX server I am scripting on. It has Python 2.6.2 installed and basically I am trying to run the below command psql -c "select * from cms_agprm;" -o u350932.txt -d tctmsv80 -U tctmsv80 Now I have done some goggling and found a few different recommendation on how to do this. None with much success. I'm also a little paranoid because I am dealing with processes and want to make sure that i get things right in this script otherwise things

Python 2.6 'import site' failed error with 2.7 installed

允我心安 提交于 2019-12-12 05:19:11
问题 I have Python 2.7 operating correctly on Windows. I installed 2.6 in another folder. However, when I run 2.6 in IDLE or PowerShell , it does not recognize basic commands and cannot import installed libraries. Whenever I launch 2.6, the first thing that comes up is: 'import site' failed; use -v for traceback I can't find anything on this error aside from some mentions with specific libraries. Trying again using -v suggests 2.6 is pulling libraries from 2.7 which is causing some of the errors.

Parse javascript object declaration which doesn't use strings for property names (using python and BeautifulSoup)

蹲街弑〆低调 提交于 2019-12-11 20:25:26
问题 I'm doing something very similar to what this user was doing: trying to load a javascript object declaration into a python dictionary. However, unlike that user, the property names aren't enclosed in quotes. >>> simplejson.loads('{num1: 1383241561141, num2: 1000}') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/lalalal/site-packages/simplejson/__init__.py", line 385, in loads return _default_decoder.decode(s) File "/Users/lalalal/site-packages/simplejson

Textfield query for prefix replacing

我只是一个虾纸丫 提交于 2019-12-11 19:49:01
问题 I'm busy creating a function in my rigging tool which allows mirroring of joints and replacing the prefix. The mirroring of joints (both behaviours and orientation) is working but I get an error when I search and replace what is in my two text fields. The prefix of joints in the scene is either R_ or L_ and I would like to replace them with this. The error is as follows: NameError: name searchFor is not defined. The odd part here is that I actually create a variable called searchFor and one

Importing modules from different directories

孤人 提交于 2019-12-11 19:37:40
问题 I have a problem importing a module: It is under this directory ./dao and the code that calls it is here ./core . Schematically represented as: rnaspace/ __init__.py core/ __init__.py logger.py dao/ __init__.py storage_configuration_reader.py This is the error message: Traceback (most recent call last): File "logger.py", line 21, in <module> from rnaspace.dao.storage_configuration_reader import storage_configuration_reader ImportError: No module named rnaspace.dao.storage_configuration_reader