python

Python equivalent for Ruby's pack function

Hi! I'm translating some of my old Ruby scripts into Python and I have trouble finding a function in Python that belongs to Ruby: http://www.ruby-doc.org/core/classes/Array.src/M002222.html - which keeps the array in little endian byte order. Is there any Python module that can help me? ...

What program to write pdf including other pdf on Linux from Python?

On an Ubuntu server, I want to create pdfs which include other static pdfs. I have tried using ReportLab with pyPdf. Ideally I would use ReportLab to do the whole thing, but in order to import the pdfs requires their PageCatcher which has a large recurring fee. So I use pyPdf to merge a page created with ReportLab and my other pdfs. Th...

Python thread for pre-importing modules

I am writing a Python application in the field of scientific computing. Currently, when the user works with the GUI and starts a new physics simulation, the interpreter immediately imports several necessary modules for this simulation, such as Traits and Mayavi. These modules are heavy and take too long to import, and the user has to wai...

why i get this traceback?

This is part of my code: if ind_1<>0: rbrcol=[] brdod1=[] for i in range(27): if Add_Cyc_1[1,i]!=0: rbrcol.append(Add_Cyc_1[0,i]) brdod1.append(Add_Cyc_1[1,i]) Probrani_1=vstack((rbrcol,brdod1)) pok=0 for i in (rbrcol): pok+=1 broj1=0 for j in range(21): if SYS_STATE_1[i,j]=...

Python conversion from binary string to hexadecimal

how can I perform a conversion of a binary string to the corresponding hex value in python I have 0000 0100 1000 1101 and I want to get 048D I'm using python2.6 Thanks for the help ...

How to get list index and element simultaneously in Python?

I find myself frequently writing code like this: k = 0 for i in mylist: # y[k] = some function of i k += 1 Instead, I could do for k in range(K): # y[k] = some function of mylist[k] but that doesn't seem "pythonic". (You know... indexing. Ick!) Is there some syntax that allows me to extract both the index (k) and the el...

Python class methods

Hi I have a class as follows: class X: def __init__(self): self.sum_x =0.0 self.sum_x_squared=0.0 self.var_x =0.0 self.sum_y =0.0 self.sum_y_squared=0.0 self.var_y =0.0 def update(self,data): [x,y,vx,vy]=data self.update_sums(self.sum_x,self.sum_x_sq...

Need help with python list manipulation

i have two seperate lists list1 = ["Infantry","Tanks","Jets"] list2 = [ 10, 20, 30] so in reality, I have 10 Infantry, 20 Tanks and 30 Jets I want to create a class so that in the end, I can call this: for unit in units: print unit.amount print unit.name #and it will produce: # 10 Infantry # 20 Tanks # 30 Jets so t...

Django Views Join Models Using Foreign Key

I have the following models defined: class Player(models.Model): Team = models.ForeignKey(Team) Name = models.CharField(max_length=200) Position = models.CharField(max_length=3) ... snip ... What I would like to output in a view is a list of players who are in the team with id = 1. I have tried things such as: {% for...

Access to widget in GTK+

Building a GTK+ widget dynamically from code allows for easy access to the child widgets directly. Now, how do I access to the child widgets when building a GTK+ Dialog (as example) from a .glade file? class ConfigDialog(object): def __init__(self, glade_file, testing=False): self.testing=testing builder = gtk.Build...

Random weighted choice

Hello, I have data like that: d = ( (701, 1, 0.2), (701, 2, 0.3), (701, 3, 0.5), (702, 1, 0.2), (702, 2, 0.3), (703, 3, 0.5) ) Where (701, 1, 0.2) = (id1, id2, priority) Is where pretty way to choice id2 if I know id1, using priority? func(701) should return:   1 - in 20% cases   2 - 30%   3 - 50% Percent will be rough o...

Search and Replace in HTML with BeautifulSoup

I want to use BeautfulSoup to search and replace <\a> with <\a><br>. I know how to open with urllib2 and then parse to extract all the <a> tags. What I want to do is search and replace the closing tag with the closing tag plus the break. Any help, much appreciated. EDIT I would assume it would be something similar to: soup.findAll('a...

How get get more search results than the server's sizelimit with Python LDAP?

I am using the python LDAP module to (amongst other things) search for groups, and am running into the server's size limit and getting a SIZELIMIT_EXCEEDED exception. I have tried both synchronous and asynchronous searches and hit the problem both ways. You are supposed to be able to work round this by setting a paging control on the s...

Connect exclusive checkboxes with line edit?

I want that, when I choose a checkbox the lineedit change the text, a different text per checkbox I have something like that: self.connect(self.form.cb_banda, QtCore.SIGNAL('stateChange(int)'), self.type) self.connect(self.form.cb_tira, QtCore.SIGNAL('stateChange(int)'), self.type) def type(self): if self.form.cb...

multiprocessing problem [pyqt, py2exe]

I am writing a GUI program using PyQt4. There is a button in my main window and by clicking this button. I hope to launch a background process which is an instance of a class derived from processing.Process. class BackgroundTask(processing.Process): def __init__(self, input): processing.Process.__init__(self) ... ...

django logging local javascript events

All.. Say there exists a template x.html in Django templates section. The contents of this page are <html> <a href="#" onclick="noserverrequest"> <input type="button onclick="noserverrequest"/> .............. </html> I have n number of buttons and hyperlinks as said above in a page. My question is how to record all the clicks that...

Is there a Python-Challenge for beginner?

The known Python-Challenge is quite difficult. Is there a much easier alternative for training my programming skills? ...

Use function from Python script in OS path

I have a third-party Python script (foo.py) in a folder that is in my system path (but not the Python sys.path). foo.py is not part of any Python module. I am writing another script (bar.py) in which I'd like to call a function located in foo.py. Is this possible? Can it be done without explicitly naming the folder of foo.py? Thanks. ...

How to speedup python unittest on muticore machines?

I'm using python unittest in order to test some other external application but it takes too much time to run the test one by one. I would like to know how can I speedup this process by using the power of multi-cores. Can I tweak unittest to execute tests in parallel? How? This question is not able python GIL limitation because in fact...

max size in BlobProperty (appengine)

What is the maximum size of one BlobProperty in appengine? I'm not talking about of the Blobstore API, i'm referring to the property class BlobProperty Please add a link who support your answers ...