python

Executing shell commands as given UID in Python

I need a way to execute the os.system() module as different UID's. It would need to behave similar to the following BASH code (note these are not the exact commands I am executing): su user1 ls ~ mv file1 su user2 ls ~ mv file1 The target platform is GNU Linux Generic. Of course I could just pass these to the os.system module, but h...

Python: printing floats / modifying the output

I have a problem which prints out a float variable. What I should get is for example: 104.0625, 119.0, 72.0. I know how to control the number of decimals but how do I control the number of zeros specifically, i.e. when the float is 104.06250000 the program should print 104.0625 but when the float is 119.00000 or 72.000000 etc., I should ...

Can Windows drivers be written in Python?

All in the title :) ...

Stomp Broadcast with Rabbitmq and Python

Im trying to move a system from using morbid to rabbitmq, but I cannot seem to get the same broadcast behaviour morbid supplied by default. By broadcast I mean that when a message is added to the queue, every consumer recieves it. With rabbit, when a message is added they are distributed round robin style to every listener. Can anyone t...

Send output from a python server back to client

Hello, I now have a small java script server working correctly, called by: <?php $handle = fsockopen("udp://78.129.148.16",12345); fwrite($handle,"vzctlrestart110"); fclose($handle); ?> On a remote server the following python server is running and executing the comand's #!/usr/bin/python import os import socket print " Loadin...

Get original path from django filefield

My django app accepts two files (in this case a jad and jar combo). Is there a way I can preserve the folders they came from? I need this so I can check later that they came from the same path. (And later on accept a whole load of files and be able to work out which came from the same folder). ...

Using a Django custom model method property in order_by()

I'm currently learning Django and some of my models have custom methods to get values formatted in a specific way. Is it possible to use the value of one of these custom methods that I've defined as a property in a model with order_by()? Here is an example that demonstrates how the property is implemented. class Author(models.Model): ...

Run a Python project in Eclipse as root

Hi, I use Eclipse as my IDE, and when I run my application I wish the application itself to run as root. My program currently checks if it is root, and if not it restarts itself with gksudo. The output, however, isn't written to the console. I can't use sudo, since it doesn't give me a graphical prompt. (While my program is CLI, Eclipse...

windows command line and Python

I have a python script that i want to run from the command line but unsure how to run it. Thanks :) ...

python automatic (or dynamic) import classes in packages

Hello people, I'm writing some kind of automated test suite and I want to make sure the classes contained in the package 'tests' get imported automatically upon runtime in the main namespace, without adding them to the __init__ file of the package. So here is the script I have so far: import os for dirpath, dirnames, filenames in os....

python recursive class inclusion & instantiation

I would like to import classes located into a folder named some-dir/ and named class*.py and finally instantiate them. Wich is the best way to look inside the folder, import classes and instantiate them? ...

Help requested: what's a fast way to bounce between lists and tuples in python?

I'm working on a script that takes the elements from companies and pairs them up with the elements of people. The goal is to optimize the pairings such that the sum of all pair values is maximized (the value of each individual pairing is precomputed and stored in the dictionary ctrPairs). They're all paired in a 1:1, each company has...

Trac Using Database Authentication

Is it possible to use a database for authentication with Trac? .htpasswd auth is not desired in this install. Using Trac .11 and MySQL as the database. Trac is currently using the database, but provides no authentication. ...

Obfuscate strings in Python

I have a password string that must be passed to a method. Everything works fine but I don't feel comfortable storing the password in clear text. Is there a way to obfuscate the string or to truly encrypt it? I'm aware that obfuscation can be reverse engineered, but I think I should at least try to cover up the password a bit. At the very...

Finding PI digits using Monte Carlo

I have tried many algorithms for finding π using Monte Carlo. One of the solutions (in Python) is this: def calc_PI(): n_points = 1000000 hits = 0 for i in range(1, n_points): x, y = uniform(0.0, 1.0), uniform(0.0, 1.0) if (x**2 + y**2) <= 1.0: hits += 1 print "Calc2: PI result", 4.0 * floa...

How can I search and replace in XML with Python?

I am in the middle of making a script for doing translation of xml documents. It's actually pretty cool, the idea is (and it is working) to take an xml file (or a folder of xml files) and open it, parse the xml, get whatever is in between some tags and using the google translate api's translate it and replace the content of the xml files...

Mark data as sensitive in python

Hi, I need to store a user's password for a short period of time in memory. How can I do so yet not have such information accidentally disclosed in coredumps or tracebacks? Is there a way to mark a value as "sensitive", so it's not saved anywhere by a debugger? ...

Weird program behaviour in Python

When running the following code, which is an easy problem, the Python interpreter works weirdly: n = input() for i in range(n): testcase = raw_input() #print i print testcase[2:(int(testcase[0])+1)]+testcase[(int(testcase[0])+2):] The problem consists in taking n strings and deleting a single character from them. For examp...

How to do a non-blocking URL fetch in Python

I am writing a GUI app in Pyglet that has to display tens to hundreds of thumbnails from the Internet. Right now, I am using urllib.urlretrieve to grab them, but this blocks each time until they are finished, and only grabs one at a time. I would prefer to download them in parallel and have each one display as soon as it's finished, wit...

python and sys.argv

if len(sys.argv) < 2: sys.stderr.write('Usage: sys.argv[0] ') sys.exit(1) if not os.path.exists(sys.argv[1]): sys.stderr.write('ERROR: Database sys.argv[1] was not found!') sys.exit(1) This is a portion of code I'm working on. The first part I'm trying to say if the user doesn't type.... python programname something...