python

add/remove items in a list

I'm trying to create a player who can add and remove items from their inventory. I have everything working, I just have 1 small problem. Every time it prints the inventory, 'None' also appears. I've been messing with it to try and remove that, but no matter what I do, 'None' always appears in the program! I know I'm just missing somethin...

Can I create class properties during __new__ or __init__?

I want to do something like this, but I haven't had much success so far. I would like to make each attr a property that computes _lazy_eval only when accessed: class Base(object): def __init__(self): for attr in self._myattrs: setattr(self, attr, property(lambda self: self._lazy_eval(attr))) def _lazy_eval(...

python duration of a file object in an argument list

In the pickle module documentation there is a snippet of example code: reader = pickle.load(open('save.p', 'rb')) which upon first read looked like it would allocate a system file descriptor, read its contents and then "leak" the open descriptor for there isn't any handle accessible to call close() upon. This got me wondering if there...

How to disable Excel's auto recognition of numbers and text

I used Python to generate a CSV file. But when I open it in Excel, Excel will auto recognize a string into a number if it could be converted. e.g.33E105 becomes 33*10^105, which is actually an ID, not a number. How to disable this in Excel while opening a CSV file? Or I need to resort to a excel-python library to output a excel file ...

read a binary file (python)

Hi, I cant read a file, and I dont understand why: f = open("test/test.pdf", "r") data = list(f.read()) print data Returns : [] I would like to open a PDF, and extract every bytes, and put it in a List. What's wrong with my code ? :( Thanks, ...

Python: Dynamic attribute name generation without exec() or eval()

Hi, I'm trying to dynamically create buttons at runtime with PyQT4.7 However, this being my first python program I'm not sure how to get the functionality I want. I would like to be able to substitute a text string for an attribute name: i.e. for each in xrange(4): myname = "tab1_button%s" % each #tab1_button0, tab1_button1, ...

Is there any online python exercise?

I am teaching a colleague Python and I think he should do some exercises. Is there any online available other than python challenge? I feel that python challenge is puzzles, not exercises. ...

how to get string type X values in matplotlib

I am using matplotlib for a graphing application. I am trying to create a graph which has strings as X values. I cannot do this ( using plot function ) because it's expecting a numaric value for X. How can I get string X values ( for example, build-lables as X values ) ? ...

I am currently serving my static files in Django. How do I use Apache2 to do this?

(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}), As you can see, I have a directory called "media" under my Django project. I would like to delete this line in my urls.py and instead us Apache to serve my static files. What do I do to my Apache configs (which files do I change) in order to...

I'm making a simulated tv

I need to make a tv that shows the user the channel and the volume, and shows whether or not the television is on. I have the majority of the code made, but for some reason the channels won't switch. I'm fairly unfamiliar with how properties work, and I think that's what my problem here is. Help please. class Television(object): de...

How can I tell [G]vim where to look for python26.dll?

I have a version of Vim compiled with python 2.6 support enabled (from here). however vim cannot find the python26.dll. :version confirms +python/dyn :version and gvim.exe confirms DYNAMIC_PYTHON_DLL="python26.dll" echo PATH confirms python26.dll is in the search path. (both c:\windows\system32, and C:\python26) What could I be mi...

Updating section in ConfigParser (or an alternative)

I am making a plugin for another program and so I am trying to make thing as lightweight as possible. What i need to do is be able to update the name of a section in the ConfigParser's config file. [project name] author:john doe email: [email protected] year: 2010 I then have text fields where user can edit project's name, author, em...

Python overriding class (not instance) special methods

How do I override a class special method? I want to be able to call the __str__() method of the class without creating an instance. Example: class Foo: def __str__(self): return 'Bar' class StaticFoo: @staticmethod def __str__(): return 'StaticBar' class ClassFoo: @classmethod def __str__(cls): ...

creating matrix with probabilities

Hi all, I want to generate a matrix of NxN to test some code that I have where each row contains floats as the elements and has to add up to 1 (i.e. a row with a set of probabilities). Where it gets tricky is that I want to make sure that randomly some of the elements should be 0 (in fact most of the elements should be 0 except for so...

Best way to encrypt a directory of files?

I need to programatically encrypt a directory of files, like in a .zip or whatever. Preferably password protected obviously. How can I accomplish this, and WHAT IS the BEST encryption way to do it, if applicable? Programming language doesn't matter. I am dictioned in all syntax. ...

GAE Datastore Put()

def post(self): update = self.request.get('update') if users.get_current_user(): if update: personal = db.GqlQuery("SELECT * FROM Personal WHERE __key__ = :1", db.Key(update)) personal.name = self.request.get('name') personal.gender = self.request.get('gender') personal.mo...

Flash in Python

I was exploring possibilities of Rich Internet applications using Python. The most awesome possibility I found was of programming in IronPython and running it as a Silverlight. Is there something similar available for Adobe AIR? I.e. programing in Python and run in Adobe AIR (Flash, that is). ...

How to Pythonically yield all values from a list?

Suppose I have a list that I wish not to return but to yield values from. What is the most Pythonic way to do that? Here is what I mean. Thanks to some non-lazy computation I have computed the list ['a', 'b', 'c', 'd'], but my code through the project uses lazy computation, so I'd like to yield values from my function instead of returni...

Django: request object to template context transparancy

Hi! I want to include an initialized data structure in my request object, making it accessible in the context object from my templates. What I'm doing right now is passing it manually and tiresome within all my views: render_to_response(...., ( {'menu': RequestContext(request)})) The request object contains the key,value pair which i...

Python: Pretty printing a xml file directly from a tar.gz package

This is the first Python script I've tried to create. I'm reading a xml file from a tar.gz package and then I want to pretty print it. However I can't seem to turn it from a file-like object to a string. I've tried to do it a few different ways including str(), tostring(), etc but nothing is working for me. For testing I just tried to...