python-2.6

Difference between simple Python function call and wrapping it in cProfile.run()

I have a rather simple Python script that contains a function call like f(var, other_var) i.e. a function that gets several parameters. All those parameters can be accessed within f and have values. When I instead call cProfile.run('f(var, other_var)') it fails with the error message: NameError: "name 'var' is not defined" Python ...

Python, With ... as ... AST/Symbol access

Disclaimer: Sensible semantics do dictate that the LHS of as behaving differently depending on the RHS lexeme is ludicrous. But I am curious nontheless. Hi guys, Simple question, but one that somone may be able to answer better than my hack. I'm currently messing with metaclasses etc and working out a comfortable syntax for some th...

Python's list comprehensions and other better practices

This relates to a project to convert a 2-way ANOVA program in SAS to Python. I pretty much started trying to learn the language Thursday, so I know I have a lot of room for improvement. If I'm missing something blatantly obvious, by all means, let me know. I haven't got Sage up and running yet, nor numpy, so right now, this is all quit...

Where's the Python 2.6.6 Mac OS X Installer Disk Image?

Python 2.6.6 was released on August 24, 2010. However, there isn't a Mac OS X Installer Disk Image. Is there a Mac OS X Installer Disk Image available for Python 2.6.6? ...

Python raw_input("") error

Hello, I am writing a simple commandline script that uses raw_input, but it doesn't seem to work. This code: print "Hello!" raw_input("") Produces this error: Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> raw_input("") TypeError: 'str' object is not callable I have never encountered this error before,...

Python's getattr gets called twice?

I am using this simple example to understand Python's getattr function: In [25]: class Foo: ....: def __getattr__(self, name): ....: print name ....: ....: In [26]: f = Foo() In [27]: f.bar bar bar Why is bar printed twice? Using Python 2.6.5. ...

Unknown cause for TypeError in Python 2.6

I am writing a program which reads input from a file called 'votes.txt'. It reads in some line of information, each of which is a 'vote' with parties ordered by preference. The program needs to take this data, and output the winner of the election. My Code is as follows: # Define the count_votes() function def count_votes(to_count): ...

Floats incorrect? - Python 2.6

Hi, I have a programming question, as follows, for which my solution does not produce the desired output This particle simulator operates in a universe with different laws of physics to ours. Each particle has a position (x, y), velocity (vx, vy) and an acceleration (ax, ay). Every particle exerts an attractive force on every other ...

Sql Alchemy > TypeError: 'instancemethod' object does not support item assignment

Here's what I've got: from sqlalchemy import * from sqlalchemy.orm import * from web.models.card import * connectionString = "postgresql://www:www@localhost/prod" databaseEngine = create_engine(connectionString) sessionFactory = sessionmaker(autoflush = True, autocommit = False, bind = databaseEngine) session = sessionFactory() CardsCo...

Working around Python bug in different versions.

I've come across a bug in Python (at least in 2.6.1) for the bytearray.fromhex function. This is what happens if you try the example from the docstring: >>> bytearray.fromhex('B9 01EF') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: fromhex() argument 1 must be unicode, not str This example works f...

How do I access outer functions variables inside a closure(python 2.6)?

From wikipedia I need to access outer functions variables in a similar manner as using the 'nonlocal' keyword from python 3.x. Is there some way to do that in python 2.6? (Not necessarily using the nonlocal keyword) ...

How can I make this Python2.6 function work with Unicode?

I've got this function, which I modified from material in chapter 1 of the online NLTK book. It's been very useful to me but, despite reading the chapter on Unicode, I feel just as lost as before. def openbookreturnvocab(book): fileopen = open(book) rawness = fileopen.read() tokens = nltk.wordpunct_tokenize(rawness) nltk...

How do I get the html after its manipulated by BeautifulSoup?

I have a simple script where I am fetching an HTML page, passing it to BeautifulSoup to remove all script and style tags, then I want to pass the HTML result to another method. Is there an easy way to do this? Skimming the BeautifulSoup.py, I haven't seen it yet. soup = BeautifulSoup(html) for script in soup("script"): soup.script.e...

Python 2.6 to 2.5 cheat sheet

I've written my code to target Python 2.6.5, but I now need to run it on a cluster that only has 2.5.4, something that wasn't on the horizon when I wrote the code. Backporting the code to 2.5 shouldn't be too hard, but I was wondering if there was either a cheat-sheet or an automated tool that would help me with this. For some things, li...

example function in Python: counting words

I'm a bit rusty in Python and am just looking for help implementing an example function to count words (this is just a sample target for a scons script that doesn't do anything "real"): def countWords(target, source, env): if (len(target) == 1 and len(source) == 1): fin = open(str(source[0]), 'r') # do something with "f.read()...

Ubuntu 10.04 - Python multiprocessing - 'module' object has no attribute 'local' error

The following code is from the python 2.6 manual. from multiprocessing import Process import os def info(title): print(title) print('module name:', 'me') print('parent process:', os.getppid()) print('process id:', os.getpid()) def f(name): info('function f') print('hello', name) if __name__ == '__main__': ...

How to optimize operations on large (75,000 items) sets of booleans in Python?

There's this script called svnmerge.py that I'm trying to tweak and optimize a bit. I'm completely new to Python though, so it's not easy. The current problem seems to be related to a class called RevisionSet in the script. In essence what it does is create a large hashtable(?) of integer-keyed boolean values. In the worst case - one fo...

Check maxlen of deque in python 2.6

Hi, I have had to change from python 2.7 to 2.6. I've been using a deque with the maxlen property and have been checking what the maxlen is. Apparently you can use maxlen in python 2.6, but in 2.6 deques do not have a maxlen attribute. What is the cleanest way to check what the maxlen of a deque is in python 2.6? In 2.7: from collectio...

Getting a Python function to cleanly return a scalar or list, depending on number of arguments

Disclaimer: I'm looking for a Python 2.6 solution, if there is one. I'm looking for a function that returns a single value when passed a single value, or that returns a sequence when passed multiple values: >>> a = foo(1) 2 >>> b, c = foo(2, 5) >>> b 3 >>> c 6 To be clear, this is in an effort to make some function calls simply look ...