Questions tagged [python-2.6]

For issues that are specific to Python 2.6. If your question applies to Python in general, use the tag [python].

Use this tag if your question is specifically about Python 2.6. If your question applies to Python in general, use the tag . If your question applies to Python 2.x but not to Python 3, use the tag . If you aren't sure, tag your question and mention which version you're using in the body of your question.

1318 questions
62
votes
3 answers

Simple example of how to use ast.NodeVisitor?

Does anyone have a simple example using ast.NodeVisitor to walk the abstract syntax tree in Python 2.6? The difference between visit and generic_visit is unclear to me, and I cannot find any example using google codesearch or plain google.
lacker
  • 5,470
  • 6
  • 36
  • 38
56
votes
2 answers

pyodbc insert into sql

I use a MS SQL express db. I can connect and fetch data. But inserting data does not work: cursor.execute("insert into [mydb].[dbo].[ConvertToolLog] ([Message]) values('test')") I get no error but nothing is inserted into the table. Directly after…
daniel
  • 34,281
  • 39
  • 104
  • 158
54
votes
1 answer

global variable warning in python

I have a python 2.6 script (yes I know I should upgrade to at least 2.7) that looks like this: ret_code = 0 def some_func() global ret_code ... if __name__ == '__main__': global ret_code ... Now I get a warning if I run the…
whomaniac
  • 1,258
  • 4
  • 15
  • 22
53
votes
3 answers

"outsourcing" exception-handling to a decorator

Many try/except/finally-clauses not only "uglify" my code, but i find myself often using identical exception-handling for similar tasks. So i was considering reducing redundancy by "outsourcing" them to a ... decorator. Because i was sure not to be…
Don Question
  • 11,227
  • 5
  • 36
  • 54
50
votes
9 answers

python histogram one-liner

There are many ways to write a Python program that computes a histogram. By histogram, I mean a function that counts the occurrence of objects in an iterable and outputs the counts in a dictionary. For example: >>> L = 'abracadabra' >>>…
mykhal
  • 19,175
  • 11
  • 72
  • 80
49
votes
2 answers

Python readlines() usage and efficient practice for reading

I have a problem to parse 1000's of text files(around 3000 lines in each file of ~400KB size ) in a folder. I did read them using readlines, for filename in os.listdir (input_dir) : if filename.endswith(".gz"): f =…
Learner
  • 1,685
  • 6
  • 30
  • 42
49
votes
14 answers

Recursively convert python object graph to dictionary

I'm trying to convert the data from a simple object graph into a dictionary. I don't need type information or methods and I don't need to be able to convert it back to an object again. I found this question about creating a dictionary from an…
Shabbyrobe
  • 12,298
  • 15
  • 60
  • 87
48
votes
4 answers

List Comprehension: why is this a syntax error?

Why is print(x) here not valid (SyntaxError) in the following list-comprehension? my_list=[1,2,3] [print(my_item) for my_item in my_list] To contrast - the following doesn't give a syntax error: def my_func(x): print(x) [my_func(my_item) for…
monojohnny
  • 5,894
  • 16
  • 59
  • 83
47
votes
13 answers

How do you switch between python 2 and 3, and vice versa?

I am reading How To Learn Python The Hard Way, which uses 2. Recently discovered Invent With Python, which uses 3. Can I download python 3, and use it when I read Invent With Python, then switch back to python 2 when I want to read How To Learn…
rayne117
  • 671
  • 2
  • 6
  • 7
45
votes
7 answers

Python 2.6 JSON decoding performance

I'm using the json module in Python 2.6 to load and decode JSON files. However I'm currently getting slower than expected performance. I'm using a test case which is 6MB in size and json.loads() is taking 20 seconds. I thought the json module had…
James Austin
  • 955
  • 2
  • 8
  • 8
45
votes
4 answers

How do you get the current text contents of a QComboBox?

Using pyqt4 and python 2.6, I am using a qcombobox to provide a list of options. I am having problems with using the selected option. I have been able to use a signal to trigger a method when the option is selected, but the problem is that when the…
Ben
  • 5,525
  • 8
  • 42
  • 66
44
votes
1 answer

How to fix symbol lookup error: undefined symbol errors in a cluster environment

I'm working on some python code that extracts some image data from an ECW file using GDAL (http://www.gdal.org/) and its python bindings. GDAL was built from source to have ECW support. The program is run on a cluster server that I ssh into. I have…
agnussmcferguss
  • 485
  • 1
  • 4
  • 10
43
votes
8 answers

How to get the current running module path/name

I've searched and this seems to be a simple question without a simple answer. I have the file a/b/c.py which would be called with python -m a.b.c. I would like to obtain the value a.b.c in the module level. USAGE = u'''\ Usage: python -m %s…
Danosaure
  • 3,578
  • 4
  • 26
  • 41
41
votes
11 answers

How to check whether a JPEG image is color or gray scale using only Python stdlib

I have to write a test case in Python to check whether a jpg image is in color or grayscale. Can anyone please let me know if there is any way to do it with out installing extra libraries like OpenCV?
kadina
  • 5,042
  • 4
  • 42
  • 83
41
votes
2 answers

Why does Python have a format function as well as a format method

The format function in builtins seems to be like a subset of the str.format method used specifically for the case of a formatting a single object. eg. >>> format(13, 'x') 'd' is apparently preferred over >>> '{0:x}'.format(13) 'd' and IMO it does…
jamylak
  • 128,818
  • 30
  • 231
  • 230
1
2
3
87 88