python

IPython in unbuffered mode

Is there a way to run IPython in unbuffered mode? The same way as python -u gives unbuffered IO for the standard python shell ...

Python: Extracting data from buffer with ctypes

I am able to successfully call a function with ctypes in Python. I now have a buffer that is filled with Structures of data I want to extract. What is the best strategy for this? Anything else I should post? Function: class list(): def __init__(self): #[...] def getdirentries(self, path): self.load_c() ...

Python Access to BaseRequestHandler

My code basically needs to start up a simple chat server with a client. Where the server and the client can talk back and forth to each other. I've gotten everything to be implemented correctly, but I can't figure out how to shut down the server whenever I'm done. (I know it's ss.shutdown()). I'm wanting to end right now based on a...

PYTHON: Replace SRC of all IMG elements using Parser

Python Newbie. I am looking for a way to replace the SRC attribute in all IMG tags not using Regular expressions. (would like to use any out-of-the box HTML parser included with default Python install) I need to reduce the source from what ever it may be to: <img src="cid:imagename"> I am trying to replace all src tags to point to th...

numpy.extract and numpy.any functions, is it possible to make it simpler way?

If there is any possibility to make this code simpler, I'd really appreciate it! I am trying to get rid of rows with zeros. The first column is date. If all other columns are zero, they have to be deleted. Number of columns varies. import numpy as np condition = [ np.any( list(x)[1:] ) for x in r] r = np.extract( condition, r ) nump...

Running Tests From a Module

I am attempting to run some unit tests in python from what I believe is a module. I have a directory structure like TestSuite.py UnitTests |__init__.py |TestConvertStringToNumber.py In testsuite.py I have import unittest import UnitTests class TestSuite: def __init__(self): pass print "Starting testting" suite = unit...

Is there a random function in python that accepts variables?

I'm attempting to create a simple dice roller, and I want it to create a random number between 1 and the number of sides the dice has. However, randint will not accept a variable. Is there a way to do what I'm trying to do? code below: import random a=0 final=0 working=0 sides = input("How many dice do you want to roll?") while a<=si...

How do I know what data type to use in Python?

I'm working through some tutorials on Python and am at a position where I am trying to decide what data type/structure to use in a certain situation. I'm not clear on the differences between arrays, lists, dictionaries and tuples. How do you decide which one is appropriate - my current understanding doesn't let me distinguish between t...

Fastest nested loops over a single list (with elements remove or not)

Hello, I am looking for advice about how to parse a single list, using two nested loops, in the fastest way, avoiding doing len(list)^2 comparisons, and avoiding duplicate files in groups. More precisely: I have a list of 'file' objects, that each has a timestamp. I want to group the files by their timestamp and a time offset. Ex. star...

Extremely large Boolean list in Python

I want to create an object in python that is a collection of around 200,000,000 true/false values. So that I can most effectively change or recall any given true/false value, so that I can quickly determine if any given number, like 123,456,000 is true or false or change its value. Is the best way to do this a list? or an array? or a cl...

Python | How to make local variable global, after script execution

Here is the code. What I need to do is find a way to make 'i' global so that upon repeated executions the value of 'i' will increment by 1 instead of being reset to 0 everytime. The code in 'main' is from another script that I embed in 'main' in order to have the trace function work. This is all being done from Java. from __future__ imp...

Find time until a date in Python

What's the best way to find the time until a date. I would like to know the years, months, days and hours. I was hoping somebody had a nice function. I want to do something like: This comment was posted 2month and three days ago or this comment was posted 1year 5months ago. ...

Mixing Python web platforms PHP, e.g. - Mediawiki, Wordpress, etc.

Is anyone developing application integrated with Mediawiki - using Django or other Python web development platforms using mod_wsgi? Would be very interested to find out what has been done in this direction and maybe there is some code available for re-use. (I've started creating wiki extensions working with MW database in python whose ...

What's the best way to search for a Python dictionary value in a list of dictionaries?

I have the following data structure: data = [ {'site': 'Stackoverflow', 'id': 1}, {'site': 'Superuser', 'id': 2}, {'site': 'Serverfault', 'id': 3} ] I want to search the above list to see if it has any site with a specific value. For instance, search the above to see if the list contain a dictionary with site = ...

Facebook Connect help.

According to the Facebook API documentation, most of the work is handled through javascript. That means that all the processing is done, and then the front end checks if the user is connected to Facebook/authorized. right? My question is: Suppose a user goes to my site for the first time ever. He clicks on "facebook connect". The ja...

Key compare using dictionary

Hi all, I have a file with the following structure: system.action.webMessage=An error has happened during web access. system.action.okMessage=Everything is ok. core.alert.inform=Error number 5512. I need a script to compare the keys in 2 files with this structure. I was working in a script to convert the file into a dictionary and use...

How to concatenate multple python source files into single file

(Assume that: application start-up time is absolutely critical; my application is started a lot; my application runs in an environment in which importing is slower than usual; many file need to be imported; and compilation to .pyc files is not available.) I would like to concatenate all the python source files that define a collection o...

[SOLVED] Apache2 + RewriteMap + Python -- when returning 'NULL', apache hangs

[SOLVED: See solution below.] I'm having a problem writing a RewriteMap program (using Python). I have a RewriteMap directive pointing to a Python script which determines if the requested URL needs to be redirected elsewhere. When the script outputs a string terminated by a linebreak, Apache redirects accordingly. However, when the s...

How to avoid excessive parameter passing?

Dear Stackoverflow'ers, I am developing a medium size program in python spread across 5 modules. The program accepts command line arguments using OptionParser in the main module e.g. main.py. These options are later used to determine how methods in other modules behave (e.g. a.py, b.py). As I extend the ability for the user to customise...

Python logging for non-trivial uses?

I'm attempting to use the python logging module to do complex things. I'll leave the motivation for this design out because it would greatly lengthen the post, but I need to have a root logger that spams a regular log file for our code and libraries that use logging -- and a collection of other loggers that go to different log files. ...