python

For Loop, os.listdir() not working correctly

Hello, I am creating a script to create new folder hierarchies for a friend of mine. There are around a thousand clients, so a script would save a ton of time. I have everything almost working, the part I don't have is this. yearList = os.listdir(driveLetter + clientName) for year in yearList: os.chdir(year) os.mkdir('Folder O...

String representation of arrays in python

Is there anything that performs the following, in python? Or will I have to implement it myself? array = [0, 1, 2] myString = SOME_FUNCTION_THAT_TAKES_AN_ARRAY_AS_INPUT(array) print myString which prints (0, 1, 2) Thanks ...

Pythonic way to write a for loop that doesn't use the loop index

This is to do with the following code, which uses a for loop to generate a series of random offsets for use elsewhere in the program. The index of this for loop is unused, and this is resulting in the 'offending' code being highlighted as a warning by Eclipse / PyDev def RandomSample(count): pattern = [] for i in range(coun...

Django-Tagging - count and ordering top "tags" (Is there a cleaner solution to mine?)

I'm using Django-Tagging, and I don't exactly need a cloud, I just want a limited list of the most popular tags used in my blog entries. Using the following: [(tag.name, int(tag.count)) for tag in Tag.objects.usage_for_model(Post, counts=True)] It returns an array (note I'm using Lorem Ipsum while I develop): [(u'deposit', 5), (u'e...

Latest books to learn PHP, Python?

Decided to learn PHP, Python. Lots of topic/books are there, but don't know which are latest/newbie friendly. Need your help.. ...

Python app engine import issues after app is cached

I'm using a modified version on juno (http://github.com/breily/juno/) in appengine. The problem I'm having is I have code like this: import juno import pprint @get('/') def home(web): pprint.pprint("test") def main(): run() if __name__ == '__main__': main() The first time I start the app up in the dev environment it works fin...

Imports with Pydev/Eclipse

I'm working with the interactive console in eclipse, and reload does not show updated functions in my code. My code was : def func1(): return 1 def func2(): return 2 but when I changed it to def afunc1(): return 1 def func2(): return 2 def func1(): return 3 and ran imp.reload(TestMod), I go...

Why does Django say I haven't set DATABASE_ENGINE yet?

I have a Django project, and I'm somewhat of a newbie in it. I have the following PyUnit test trying to save an object into a PostgreSQL database: import unittest from foo.models import ObjectType class DbTest(unittest.TestCase): def testDBConnection(self): object_type = ObjectType() object_type.name = 'Test Obje...

Will this urllib2 python code download the page of the file?

urllib2.urlopen(theurl).read() ...this downloads the file. urllib2.urlopen(theurl).geturl()...does this download the file? (how long does it take) ...

Using cProfile results with KCacheGrind

I'm using cProfile to profile my Python program. Based upon this talk I was under the impression that KCacheGrind could parse and display the output from cProfile. However, when I go to import the file, KCacheGrind just displays an 'Unknown File Format' error in the status bar and sits there displaying nothing. Is there something speci...

How do I use this information in Python? I don't know how to use this data-type.

According to the NLTK book, I first apply the grammar, and parse it. grammar = r""" NP: {<DT|PP\$>?<JJ>*<NN>} {<NNP>+} """ cp = nltk.RegexpParser(grammar) chunked_sent = cp.parse(sentence) When I print chunked_sent, I get this: (S i/PRP use/VBP to/TO work/VB with/IN you/PRP a...

How to determine the datatype in Python?

astring ('a','tuple') How do I determine if "x" is a tuple or string? ...

Special characters in OSX filename ? (Python os.rename)

I am trying to rename some files automatically on OSX with a python script. But I fail to work with special characters like forward slash etc.: oldname = "/test" newname = "/test(1\/10)" os.rename(oldname, newname) I think I do have an encoding problem. But different tries with re.escape or using UTF-8 unicode encodings havent been su...

Books to learn PHP, Python?

Decided to learn PHP, Python. Lots of books are there in market, but donno which are latest/newbie friendly. Need your help.. ...

lxml[.objectify] documentElement tagName

I'm receiving data packets in XML format, each with a specific documentRoot tag, and I'd like to delegate specialized methods to take care of those packets, based on the root tag name. This worked with xml.dom.minidom, something like this: dom = minidom.parseString(the_data) root = dom.documentElement deleg = getattr(self,'elem_' + str(...

Python: How to read huge text file into memory

I'm using Python 2.6 on a Mac Mini with 1GB RAM. I want to read in a huge text file $ ls -l links.csv; file links.csv; tail links.csv -rw-r--r-- 1 user user 469904280 30 Nov 22:42 links.csv links.csv: ASCII text, with CRLF line terminators 4757187,59883 4757187,99822 4757187,66546 4757187,638452 4757187,4627959 4757187,312826 475718...

How to return a float point number with a defined number of decimal places?

So I know how to print a floating point number with a certain decimal places. My question is how to return it with a specified number of decimal places? Thanks. ...

Running unittest with typical test directory structure.

The very common directory structure for even a simple Python module seems to be to separate the unit tests into their own test directory: new_project/ antigravity/ antigravity.py test/ test_antigravity.py setup.py etc. for example see this Python project howto. My question is simply What's the usual wa...

Creating Regular Expressions in Python

Hi All, I'm trying to create regular expression that filters from the following partial text: amd64 build of software 1:0.98.10-0.2svn20090909 in archive what I want to extract is: software 1:0.98.10-0.2svn20090909 How can I do this?? I've been trying and this is what I have so far: p = re.compile('([a-zA-Z0-9\-\+\.]+)\ ([0-9\:\...

Problem opening registry key on Windows 7

This code used to work on Vista (and Windows XP) but after an upgrade to Windows 7 it now fails with the error shown: Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 >>> import _winreg >>> h1 = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE) >>> key = r'SOFTWARE\Python\PythonCore\2...