python

file output in python giving me garbage

When I write the following code I get garbage for an output. It is just a simple program to find prime numbers. It works when the first for loops range only goes up to 1000 but once the range becomes large the program fail's to output meaningful data output = open("output.dat", 'w') for i in range(2, 10000): prime = 1 for j in r...

is it possible to control the windows of an external app

I want to take the control of the window for exmple i want to force to external app to start minimized I mean when i enter this command myapp Firefox Firefox starts but minimized firefox is not important I mean i want to do it with any gui application If yes can any body show me the way? ...

What is __path__ useful for?

I had never noticed the __path__ attribute that gets defined on some of my packages before today. According to the documentation: Packages support one more special attribute, __path__. This is initialized to be a list containing the name of the directory holding the package’s __init__.py before the code in that file is exe...

How to setup RAM disk drive using python or WMI?

Hi, The background of my question is associated with Tesseract, the free OCR engine (1985-1995 by HP, now hosting in Google). It specifically requires an input file and an output file; the argument only takes filename (not stream / binary string), so in order to use the wrapper API such as pytesser and / or python-tesser.py, the OCR tem...

Python: x-y-plot with matplotlib

I want to plot some data. The first column contains the x-data. But matplotlib doesnt plot this. Where is my mistake? #fresnel formula import numpy as np from numpy import cos from scipy import * from pylab import plot, show, ylim, yticks from matplotlib import * from pprint import pprint n1 = 1.0 n2 = 1.5 #alpha, beta, intensity dat...

Regex: Matching a space-joined list of words, excluding last whitespace

How would I match a space separated list of words followed by whitespace and some optional numbers? I have this: >>> import re >>> m = re.match('(?P<words>(\S+\s+)+)(?P<num>\d+)?\r\n', 'Foo Bar 12345\r\n') >>> m.groupdict() {'num': '12345', 'words': 'Foo Bar '} I'd like the words group to not include the last whitespace(s) but I can...

Regular Expression Question

I'm trying to use regular expression to extract the comments in the heading of a file. For example, the source code may look like: //This is an example file. //Please help me. #include "test.h" int main() //main function { ... } What I want to extract from the code are the first two lines, i.e. //This is an example file. //Please...

Storing hierarchical (parent/child) data in Python/Django: MPTT alternative?

I'm looking for a good way to store and use hierarchical (parent/child) data in Django. I've been using django-mptt, but it seems entirely incompatible with my brain - I end up with non-obvious bugs in non-obvious places, mostly when moving things around in the tree: I end up with inconsistent state, where a node and its parent will disa...

Dropping Root Permissions In Python

I'd like to have a Python program start listening on port 80, but after that execute without root permissions. Is there a way to drop root or to get port 80 without it? ...

Importing Classes Within a Module

Currently, I have a parser with multiple classes that work together. For Instance: TreeParser creates multiple Product and Reactant modules which in turn create multiple Element classes. The TreeParser is called by a render method within the same module, which is called from the importer. Finally, if the package has dependencies (such ...

How to suppress error messages in rpy2

Hello! The following code does not work. It seems that the R warning message raises a python error. # enable use of python objects in rpy2 import rpy2.robjects.numpy2ri import numpy as np from rpy2.robjects import r # create an example array a = np.array([[5,2,5],[3,7,8]]) # this line leads to a warning message, which in turn raises ...

Reading a Delphi binary file in Python

I have a file that was written with the following Delphi declaration ... Type Tfulldata = Record dpoints, dloops : integer; dtime, bT, sT, hI, LI : real; tm : real; data : array[1..armax] Of Real; End; ... Var: fh: File Of Tfulldata; I want to analyse the data in the files (many MB in size) using Python if poss...

Is it possible to retrieve an uri chunk on AppEngine?

Let's say i go to myblog.com/post/12. The /post handler is already defined, but how can i get the parameter being passed? 12 in this case is the post_id. I'm using the Python SDK. ...

Python: need to get energies of charge pairs.

I am new to python. I have to make a program for a project that takes a PDB format file as input and returns a list of all the intra-chain and inter-chain charge pairs and their energies (using coulomb’s law assuming a dielectric constant of () of 40.0). For simplicity, the charged residues for this program are just Arg (CZ), Lys (NZ)...

Python if statement not working as expected

I'm searching for a string in a website and checking to see if the location of this string is in the expected location. I know the string starts at the 182nd character, and if I print temp it will even tell me that it is 182, however, the if statement says 182 is not 182. Some code f = urllib.urlopen(link) #store page contents in 's...

Bipartite matching in Python

Does anybody know any module in Python that computes the best bipartite matching? I have tried the following two: munkres hungarian However, in my case, I have to deal with non-complete graph (i.e., there might not be an edge between two nodes), and therefore, there might not be a match if the node has no edge. The above two packag...

How to replace unicode characters by ascii characters in Python (perl script given)?

I am trying to learn python and couldn't figure out how to translate the following perl script to python: #!/usr/bin/perl -w use open qw(:std :utf8); while(<>) { s/\x{00E4}/ae/; s/\x{00F6}/oe/; s/\x{00FC}/ue/; print; } The script just changes unicode umlauts to alternative ascii output. (So the complete ...

Multiply with find and replace

Can regular expressions be used to perform arithmetic? Such as find all numbers in a file and multiply them by a scalar value. ...

Api to analyse complex graph

I am looking for an API (preferably in python) that could be used to analyze complex networks. Basically I want to find things like: Average shortest path, Degree distribution Giant Component local clustering coefficient, global clustering coefficient etc.. Thanks ...

Most efficient way for a lookup/search in a huge list (python)

Hey guys, -- I just parsed a big file and I created a list containing 42.000 strings/words. I want to query [against this list] to check if a given word/string belongs to it. So my question is: What is the most efficient way for such a lookup? A first approach is to sort the list [list.sort()] and then just use the >> if word in list: ...