python

Python: File IO - Disable incremental flush

Kind of the opposite of this question. Is there a way to tell Python "Do not write to disk until I tell you to." (by closing or flushing the file)? I'm writing to a file on the network, and would rather write the entire file at once. In the meantime, I'm writing to a StringIO buffer, and then writing that to the disk at the end. ...

Algorithm: How to Delete every other file

Hi, I have a folder with thousands of images. I want to delete every other image. What is the most effective way to do this? Going through each one with i%2==0 is still O(n). Is there a fast way to do this (preferably in Python)? Thx ...

Using BeautifulSoup to find a HTML tag that contains certain text

I'm trying to get the elements in an HTML doc that contain the following pattern of text: #\S{11} <h2> this is cool #12345678901 </h2> So, the previous would match by using: soup('h2',text=re.compile(r' #\S{11}')) And the results would be something like: [u'blahblah #223409823523', u'thisisinteresting #293845023984'] I'm able to...

python dealing with Nonetype before cast\addition

I'm pulling a row from a db and adding up the fields (approx 15) to get a total. But some field values will be Null, which causes an error in the addition of the fields (TypeError: unsupported operand type(s) for +: 'NoneType' and 'int') Right now, with each field, I get the field value and set it to 'x#', then check if it is None and ...

keyerror inside django model class __init__

Here's a Django model class I wrote. This class gets a keyerror when I call get_object_or_404 from Django (I conceive that keyerror is raised due to no kwargs being passed to __init__ by the get function, arguments are all positional). Interestingly, it does not get an error when I call get_object_or_404 from console. I wonder why, and ...

In Django, how can you change the User class to work with a different db table?

We're running django alongside - and sharing a database with - an existing application. And we want to use an existing "user" table (not Django's own) to store user information. It looks like it's possible to change the name of the table that Django uses, in the Meta class of the User definition. But we'd prefer not to change the Dja...

Creating a new terminal/shell window to simply display text

I want to pipe [edit: real-time text] the output of several subprocesses (sometimes chained, sometimes parallel) to a single terminal/tty window that is not the active python shell (be it an IDE, command-line, or a running script using tkinter). IPython is not an option. I need something that comes with the standard install. Prefer OS-a...

Can I segment a document in BeautifulSoup before converting it to text based on my analysis of the document?

I have some html files that I want to convert to text. I have played around with BeautifulSoup and made some progress on understanding how to use the instructions and can submit html and get back text. However, my files have a lot of text that is formatted using table structures. For example I might have a paragraph of text that res...

Should I leave php for Python or Ruby?

First i started with wordpress, than moved to Drupal and realized that any CMS would not fit my needs since i want a really big flexbility in building my aplications (plus i hate to see worthless code, that i will not use but stays there). I give up on already made CMS and started learning php from the begining. But in nearly no time I...

python instance variables as optional arguments

In python, is there a way I can use instance variables as optional arguments in a class method? ie: def function(self, arg1=val1, arg2=val2, arg3=self.instance_var): # do stuff.... Any help would be appreciated. ...

db connection in python

I am writing a code in python in which I established a connection with database.I have queries in a loop.While queries being executed in the loop ,If i unplug the network cable it should stop with an exception.But this not happens ,When i again plug yhe network cabe after 2 minutes it starts again from where it ended.I am using linux and...

Python Class Members Initialization

Hello, I have just recently battled a bug in Python. It was one of those silly newbie bugs, but it got me thinking about the mechanisms of Python (I'm a long time C++ programmer, new to Python). I will lay out the buggy code and explain what I did to fix it, and then I have a couple of questions... The scenario: I have a class called A...

Writing Digg like system in django/python

I am tying to write a digg , hackernews , http://collectivesys.com/ like application where users submit something and other users can vote up or down , mark items as favorite ect . I was just wondering if there are some open source implementations django/python that i could use as starting point , instead of reinventing the wheel by s...

How do languages such as Python overcome C's Integral data limits?

While doing some random experimentation with a factorial program in C, Python and Scheme. I came across this fact: In C, using 'unsigned long long' data type, the largest factorial I can print is of 65. which is '9223372036854775808' that is 19 digits as specified here. In Python, I can find the factorial of a number as large as 999 w...

Numbers Comparison - Python Bug?

Hello, Deep inside my code, in a nested if inside a nested for inside a class method, I'm comparing a certain index value to the length of a certain list, to validate I can access that index. The code looks something like that: if t.index_value < len(work_list): ... do stuff ... else: ... print some error ... For clarificati...

How to catch str exception?

import sys try: raise "xxx" except str,e: print "1",e except: print "2",sys.exc_type,sys.exc_value In the above code a string exception is raised which though deprecated but still a 3rd party library I use uses it. So how can I catch such exception without relying on catch all, which could be bad. except str,e: doesn't cat...

Creating a wrapper for a C library in Python

I'm trying to create a wrapper of my own for FLAC, so that I can use FLAC in my own Python code. I tried using ctypes first, but it showed a really weird interface to the library, e.g. all the init functions for FLAC streams and files became one function with no real information on how to initialize it. Especially since it wants a refer...

Convert unicode codepoint to UTF8 hex in python

I want to convert a number of unicode codepoints read from a file to their UTF8 encoding. e.g I want to convert the string 'FD9B' to the string 'EFB69B'. I can do this manually using string literals like this: u'\uFD9B'.encode('utf-8') but I cannot work out how to do it programatically. ...

Python: Elegant way of dual/multiple iteration over the same list

I've written a bit of code like the following to compare items with other items further on in a list. Is there a more elegant pattern for this sort of dual iteration? jump_item_iter = (j for j in items if some_cond) try: jump_item = jump_item_iter.next() except StopIteration: return for item in items: if jump_item is item: ...

Loading files into variables in python

I am trying to write a small function that gets a variable name, check if it exists, and if not loads it from a file (using pickle) to the global namespace. I tried using this in a file: import cPickle # # Load if neccesary # def loadfile(variable, filename): if variable not in globals(): cmd = "%s = cPickle.load(file('%s'...