python

Python: recursively create dictionary from paths

I have several hundred thousand endpoint URLs that I want to generate stats for. For example I have: /a/b/c /a/b/d /a/c/d /b/c/d /b/d/e /a/b/c /b/c/d I want to create a dictionary that looks like this { {'a': {'b': {'c': 2 }, {'d': 1 } }, {'c': {'d': 1 } } ...

Real-world examples of nested functions

I asked previously how the nested functions work, but unfortunately I still don't quite get it. To understand it better, can someone please show some real-wold, practical usage examples of nested functions? Many thanks ...

Trouble with pycurl.POSTFIELDS

I'm familiar with CURL in PHP but am using it for the first time in Python with pycurl. I keep getting the error: Exception Type: error Exception Value: (2, '') I have no idea what this could mean. Here is my code: data = {'cmd': '_notify-synch', 'tx': str(request.GET.get('tx')), 'at': paypal_pdt_test ...

Why aren't "and" and "or" operators in Python?

I wasn't aware of this, but apparently the and and or keywords aren't operators. They don't appear in the list of python operators. Just out of sheer curiosity, why is this? And if they aren't operators, what exactly are they? ...

Python Thread Pause and Wait

I have an array of threads. Each of them call the run method continuously. At the end of each run method I want them to pause and wait. How can I get the threads to execute one at a time, and then continue the loop when all are finished? I need them to ALWAYS execute in order (ie: 1, 2, 3, 4 - 1, 2, 3, 4...) I'm currently using threadi...

Newbie needs help with Python Tutorial

Hello, I am a newbie going through a byte of python (3.0). This is the first programming language I have ever used. I am stuck at the point where you make a simple program that creates a backup zip file (p.75). I'm running Windows 7 (64 bit) with python 3.1. Prior to this I installed GNUWin32 + sources, and added C:\Program Files(x86)\Gn...

Python: Is it possible to have an actual memory leak in Python because of your code?

I don't have an example of code but I'm curious if you do bad coding practice, is that possible? ...

Get memory usage of computer in Windows with Python

How can I tell what the computer's overall memory usage is from Python, running on Windows XP? ...

a(*{'q':'qqq'}),why only print key.

def a(*x): print x a({'q':'qqq'}) a(*{'q':'qqq'})#why only print key. traceback: ({'q': 'qqq'},) ('q',) thanks ...

Runing bcdedit from python in Windows 2008 SP2

I do not know windows well, so that may explain my dilemma ... I am trying to run bcdedit in Windows 2008R2 from Python 2.6. My Python routine to run a command looks like this: def run_program(cmd_str): """Run the specified command, returning its output as an array of lines""" dprint("run_program(%s): entering" % cmd_str) ...

Why does tarfile.extractall ignore errors by default?

Python's tarfile module ignores errors during extraction by default, unless errorlevel is set to either 1 or 2 (or debug to 1 if only error messages need to be printed). Try doing a mkdir /tmp/foo && sudo chown root /tmp/foo && chmod a-w /tmp/foo and using tarfile to extract a .tar.gz file over /tmp/foo -- you will see that your Python...

why defined '__new__' and '__init__' all in a class .

i think you can defined either '__init__' or '__new__' in a class,but why all defined in django.utils.datastructures.py. my code: class a(object): def __init__(self): print 'aaa' def __new__(self): print 'sss' a()#print 'sss' class b: def __init__(self): print 'aaa' def __new__(self): ...

Alternating between iterators in Python

What is the most efficient way to alternate taking values from different iterators in Python, so that, for example, alternate(xrange(1, 7, 2), xrange(2, 8, 2)) would yield 1, 2, 3, 4, 5, 6. I know one way to implement it would be: def alternate(*iters): while True: for i in iters: try: yield i.nex...

Database Design Inquiry

I'm making a trivia webapp that will feature both standalone questions, and 5+ question quizzes. I'm looking for suggestions for designing this model. Should a quiz and its questions be stored in separate tables/objects, with a key to tie them together, or am I better off creating the quiz as a standalone entity, with lists stored for ...

Should I use urllib or urllib2?

In Python (2.5), should I use urllib or urllib2? What's the difference? They seem to do the same thing. (Bonus points, if I'm using Google App Engine, does this change the answer?) ...

Given a unicode error I don't understand.

Here is my code, I'm sure it looks terrible but it all works as it should, only problem I'm having is with the last line... import pyPdf import os import csv class UnicodeWriter: """ A CSV writer which will write rows to CSV file "f", which is encoded in the given encoding. """ def __init__(self, f, dialect=csv.exc...

Data Carving loop improvements.

I currently have a Python app I am developing which will data carve a block device for jpeg files. Let's just say that it sometimes works and sometimes doesn't. I have created it so that I read the block device till I find a ffd8, then I keep the stream open and search via looping for the ffd9 closure. Though I always need to take int...

How to create a class from function

I am still struggling with understanding classes, I am not certain but I have an idea that this function I have created is probably a good candidate for a class. The function takes a list of dictionaries, identifies the keys and writes out a csv file. First Q, is this function a good candidate for a class (I write out a lot of csv fil...

how to update a record using DBSession in turbogears 2

Hi I'm trying to update a user row upon the user logging in. I simply want to increase the users login count by one. Here is the code in the post_login controller method: @expose() def post_login(self, came_from=url('/')): """ Redirect the user to the initially requested page on successful authentication or redirect ...

reading tar file contents without untarring it, in python script.

Hi All, I have a tar file which has number of files within it. I need to write a python script which will read the contents of the files and gives the count o total characters, including total number of letters, spaces, newline characters, everything, without untarring the tar file. ...