python

Python: Numpy array help. Is there a function to return the index of something in an array?

I know there is a method for python list to return the first index of something l = list(1,2,3) l.index(2) >>> 1 Is there something like that for numpy arrays? Thanks for your help :) ...

when to delete user's session

I'm writing a webapp that will only be used by authenticated users. Some temporary databases and log files will be created during each user session. I'd like to erase all these temp files when the session is finished. Obviously, a logout or window close event would be sufficient to close the session, but in some cases the user may keep...

Short Python Code to say "Pick the lower value"?

What I mean is, I'm looking for really short code that returns the lower value. for example: a=[1,2,3,4,5,6,7,8,9,10] b=[1,2,3,4,5,6,7,8] len(a) = 10 len(b) = 8 if (fill-this-in): print(lesser-value) And I forgot to add that if b is lower than a, I want b returned - not len(b) - the variable b. ...

SFTP in Python? (platform independent)

I'm working on a simple tool that transfers files to a hard-coded location with the password also hard-coded. I'm a python novice, but thanks to ftplib, it was easy: import ftplib info= ('someuser', 'password') #hard-coded def putfile(file, site, dir, user=(), verbose=True): """ upload a file by ftp to a site/directory ...

Materials for SICP with python?

I want to try out SICP with Python. Can any one point to materials (video.article...) that teaches Structure and Interpretation of Computer Programs in python. Currently learning from SICP videos of Abelson, Sussman, and Sussman. ...

How can I assign a new class attribute via __dict__ in python?

I want to assign a class attribute via a string object - but how? Example: class test(object): pass a = test() test.value = 5 a.value # -> 5 test.__dict__['value'] # -> 5 # BUT: attr_name = 'next_value' test.__dict__[attr_name] = 10 # -> 'dictproxy' object does not support item assignment ...

How do you get the logical xor of two variables in Python?

How do you get the logical xor of two variables in Python? For example, I have two variables that I expect to be strings. I want to test that only one of them contains a True value (is not None or the empty string): str1 = raw_input("Enter string one:") str2 = raw_input("Enter string two:") if logical_xor(str1, str2): print "ok" el...

Can I access constants in settings.py from templates in Django?

I have some stuff in settings.py that I'd like to be able to access from a template, but I can't figure out how to do it. I already tried {{CONSTANT_NAME}} but that doesn't seem to work. Is this possible? ...

How do I add a custom inline admin widget in Django?

This is easy for non-inlines. Just override the following in the your admin.py AdminOptions: def formfield_for_dbfield(self, db_field, **kwargs): if db_field.name == 'photo': kwargs['widget'] = AdminImageWidget() return db_field.formfield(**kwargs) return super(NewsOptions,self).formfield_for_dbfield(db_field,**k...

Column comparison in Django queries

I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage = models.IntegerField() I want to select all cars where mileage is less than mileage_limit, so in SQL it would be something like: select * from car where mileage < mileage_limit; Using ...

Python Library to Generate VCF Files?

Know of any good libraries for this? I did some searches and didn't come across anything. Someone somewhere must have done this before, I hate to reinvent the wheel. ...

Python-Regex, what's going on here?

I've got a book on python recently and it's got a chapter on Regex, there's a section of code which I can't really understand. Can someone explain exactly what's going on here (this section is on Regex groups)? >>> my_regex = r'(?P<zip>Zip:\s*\d\d\d\d\d)\s*(State:\s*\w\w)' >>> addrs = "Zip: 10010 State: NY" >>> y = re.search(my_regex, a...

Filtering a complete date in django?

There are several filter methods for dates (year,month,day). If I want to match a full date, say 2008/10/18, is there a better way than this: Entry.objects.filter(pub_date__year=2008).filter(pub_date__month=10).filter(pub_date__day=18) ...

Is late binding consistent with the philosophy of "readibility counts"?

I am sorry all - I am not here to blame Python. This is just a reflection on whether what I believe is right. Being a Python devotee for two years, I have been writing only small apps and singing Python's praises wherever I go. I recently had the chance to read Django's code, and have started wondering if Python really follows its "reada...

Analyse python list with algorithm for counting occurences over date ranges

The following shows the structure of some data I have (format: a list of lists) data = [ [1,2008-12-01], [1,2008-12-01], [2,2008-12-01] ... (the lists continue) ] The dates range from 2008-12-01 to 2008-12-25. The first field identifies a user by id, the second field (a date field) shows when this user visited a page on my ...

Shorthand adding/appending in Python

I like that in PHP I can do the following $myInteger++; $myString += 'more text'; With Python I must do the following myInteger = myInteger + 1 myString = myString + "more text" Is there a better way to add or append to a variable in Python? ...

Python: JFileChooser for Python?

I was wondering if there is something similar to Java's JFileChooser for Python? JFileChooser is a graphical front end to choose a file. Preferably something that is already with Python..maybe with Tkinter. Thanks for your help :) ...

What is the most "pythonic" way to iterate over a list in chunks?

I have a Python script which takes as input a list of integers, which I need to work with four integers at a time. Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples. Currently, I'm iterating over it this way: for i in xrange(0, len(ints), 4): # dummy op for example code ...

Comparing dictionaries in Python

Given two dictionaries, d1 and d2, and an integer l, I want to find all keys k in d1 such that either d2[k]<l or k not in l. I want to output the keys and the corresponding values in d2, except if d2 does not contain the key, I want to print 0. For instance, if d1 is a: 1 b: 1 c: 1 d: 1 and d2 is a: 90 b: 89 x: 45 d: 90 and l is 9...

Installing certain packages using virtualenv

So, I want to start using virtualenv this year. I like the no-site-packages option, that is nice. However I was wondering how to install certain packages into each virtualenv. For example, lets say I want to install django into each virtualenv... is this possible, and if so, how? Does buildout address this? ...