python

How to suppress carriage return after a variable?

Don't flame but I'm still a python newbie. I need to suppress the carriage return after I display a variable. data = """ [virtual_machines: %s] \taddress %s.domain.com """ % (line, line) fg = file('munin.txt', 'a') fg.write(stuff) When printing this out, it creates a new line after the variable gets printed. I tried using %r but ...

Testing for external resource consistency / skipping django tests

I'm writing tests for a Django application that uses an external data source. Obviously, I'm using fake data to test all the inner workings of my class but I'd like to have a couple of tests for the actual fetcher as well. One of these will need to verify that the external source is still sending the data in the format my application exp...

Generate UUID for Cassandra in Python

Heh, I'm using cf.insert(uuid.uuid1().bytes_le, {'column1': 'val1'}) (pycassa) to create a TimeUUID for Cassandra, but getting the error InvalidRequestException: InvalidRequestException(why='UUIDs must be exactly 16 bytes') It doesn't work with uuid.uuid1() uuid.uuid1().bytes str(uuid.uuid1()) either. What's the best way...

Python and HTML: Assign a variable to a submit button

I'm writing this to a text file which is then read in by my browser. How do I assign the variable "i[0]" to the to the "submit" button so it is passed to my edit_python script? k.write('<table>') for i in row: k.write('<tr>') k.write('<td><form action="edit_python" method="post" name="edit_python"><input type="submit...

How to increment the day in datetime? Python.

How to increment the day in datetime? In python. for i in xrange(1,5) date=datetime.datetime(2003,8,i,12,4,5) print date But i need pass through years? Any ideas? Should be easyier way.... ...

Is there a reason that a ReferenceProperty might not generate a back-reference?

In my current project, I have two models, Version and Comment. There is a one-to-many relationship between the two; each Version can have many Comment and the Comment model has a ReferenceProperty to record which Version it belongs to: class Comment(db.Model): version = db.ReferenceProperty(version.Version, collection_name="comments...

Recompiling Python to fix arrow keys in interactive mode issue

I am working in python 2.6 (installed alongside Python2.4.3 required for CentOS) and I am having issues with the arrow keys and backspace etc. I compiled from source and I imagine the solution is to recompile after installing readline-devel as outlined in: http://stackoverflow.com/questions/893053/python-shell-arrow-keys-do-not-work-on...

Weird error when trying to write to an mmap under windows

This simple python code: import mmap with file("o:/temp/mmap.test", "w+b") as fp: m = mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ|mmap.ACCESS_WRITE) m.write("Hello world!") Produces the following error (on the mmap.mmap(...) line): WindowsError: [Error 1006] The volume for a file has been externally altered so...

make data struct iterable in python

Hey everyone, So I've done some looking online and throught the documentation, but I'm having troubling finding out how to do this. I am working on creating an adventure game. I have a level class (which contains a bunch of rooms) and a Room class. I would like to be able to do something like the following. l = Level() for room in l: ...

Visualize data and clustering

Hi, i am currently writing a python script to find the similarity between documents.I have already calculated the similarities score for each document pairs and store them in dictionaries. It looks something like this: {(8328, 8327): 1.0, (8313, 8306): 0.12405229825691289, (8329, 8328): 1.0, (8322, 8321): 0.99999999999999989, (8328, 832...

Getting python exceptions printed the normal way with PyObjC

I'm getting errors like this: 2010-07-13 20:43:15.131 Python[1527:60f] main: Caught OC_PythonException: : LoginMenuSet instance has no attribute 'play_sound' That's with this code: @try { [section loop]; //Loop through section } @catch (NSException *exception) { NSLog(@"Caught %@: %@", [exception name], [exception ...

Python: flatten function works in console but not in file?

I'm doing an exercise to flatten nested lists. The code works in console but it doesn't work when its in a file. I have no idea what's going on. :( def flatten(nested): """ >>> flatten([2, 9, [2, 1, 13, 2], 8, [2, 6]]) [2, 9, 2, 1, 13, 2, 8, 2, 6] >>> flatten([[9, [7, 1, 13, 2], 8], [7, 6]]) ...

Authentication in Google App Engine: app.yaml vs. python code

I am writing a small app that uses the GAE. I have parts of my app that are for administrative use only. I have two options using login: admin option in the app.yaml or google.appengine.api.users.is_current_user_admin() in python code. The basic authentication is sufficient for my case. Which solution is better? The advantage of using ...

How do i extract specific lines of data from a huge Excel sheet using Python?

I need to get speciic lines of data that have certain key words in them (names) and write them to another file....the starting file is huge. I cant just open it up and save it as a different format. How should I handle this using python. Detail is appreciasted. Thanks. ...

Error in IPython Shell

I've just started playing around with IPython, and it's got some pretty cool features. I especially like that it "remembers" input from previous shell sessions. I am having some trouble, however, with the tab completion. Here's an example of my console: In [5]: a.__class__.python(58387,0x7fff70630c20) malloc: *** error for object 0x10...

Programmatically Uncheck a Checkbox in a wx.CheckListBox

Is there a method to uncheck a checkbox in a wx.CheckListBox as I need to implement an "uncheck all" button, can't seem to find anything... although there is number of methods for setting a checkbox/s. ...

Creating fewer files when freezing a Python application

I'm using cxFreeze to freeze my Python application. All seems to be working as expected but peering into the build directory got me thinking... Is there a way I could have fewer files in the build directory? Currently, there's a bunch of PYD files and the necessary DLL files lying around. Then I have some configuration files (custom) a...

obtaining error number of an error

I need to obtain the error number from an error that has occurred in Python. Ex; When trying to transfer a directory via the Paramiko package, an error is caught with this piece of code: try: sftp.put(local_path,target_path) except (IOError,OSError),errno: print "Error:",errno For which I get the outpu...

Change package import name in python.

Hi ! I was wondering if it was possible to import a library in python, and completely change its name. say i need to do : import plop.blah.wii but I want it to be recognized as foo.bar.yeah something like import plop.blah.wii as foo.bar.yeah Any idea how can this be done ? When unpickling an object, Python expects a library th...

ValueError: need more than 1 value to unpack

Here's the line that throws this error (x,neighbor) = random.sample(out_edge_list,1) ...