python

Format date based on locale in python

I have a date output like >>> import time >>> print time.strftime("%d %B") 19 July Is there a way to format the date based on the locale, but still have control of what is shown (in some cases I don't want the year). For example, on a en_US machine, I want it to output: July 19'th ...

Equivalent of __DATE__ and __TIME__ in Python?

Is there an equivalent of __DATE__ and __TIME__ in Python? ...

terminate script of another user

On a linux box I've got a python script that's always started from predefined user. It may take a while for it to finish so I want to allow other users to stop it from the web. Using kill fails with Operation not permitted. Can I somehow modify my long running python script so that it'll recive a signal from another user? Obviously, t...

Setting the system date in Python (on Windows)

There appears to be many packages for getting/formatting the current date, or finding out the date n time intervals from now. But I must be overlooking the existence of a simple method to set the date (like Windows' date.exe) in Python. Surely such a function exists? I've been unable to find anything on Google, the python docs (datetime...

Automatically call all functions matching a certain pattern in python

In python I have many functions likes the ones below. I would like to run all the functions whose name matches setup_* without having to explicitly call them from main. The order in which the functions are run is not important. How can I do this in python? def setup_1(): .... def setup_2(): .... def setup_3(): ... ... if...

Best way to install python packages locally for development

Being new to the python games I seem to have missed out on some knowledge on how you can develop on a program but also keep it in your live environment. Programs like gpodder can be run directly from the source checkout which is really handy however others want to be "installed" to run. A lot of programs are distributed with a setup.p...

Really awkward (seemingly simple) bug with python integer comparisons.

I have the following piece of code which is not working the way I expect it to at all... current_frame = 15 # just for showcasing purposes g_ch = 7 if (current_frame != int(row[0])) and (int(row[1]) != g_ch): current_frame = int(row[0]) print "curious=================================" pri...

Checking duplicate while inserting in SQLite

Hello All, I am trying to insert a data into SQLite database using Python. INSERT INTO DATA_TABLE(UID,LABEL) VALUES (NULL, "UK") WHERE "UK" NOT EXISTS IN (SELECT LABEL FROM DATA_TABLE); This query is dynamically generated from Python and I am checking whether the date is already exist in the table before inserting and its not w...

python - list operations

Given a list of unsorted numbers, I want to find the smallest number larger than N (if any). In C#, I'd do something like this (checks omitted) : var x = list.Where(i => i > N).Min(); What's a short, READABLE way to do this in Python? ...

Compiling a PyObjC application for 10.5 (Leopard) into xcode 10.6 (Snow Leopard)

I'm trying to deploy on 10.5 a PyObjC (or Cocoa-Python) application developed on Xcode 3.2.X (Snow Leopard) which runs perfectly fine on 10.6 systems. The application doesn't launch on 10.5;it crashes at launch giving this error message (found on Crash Report): Dyld Error Message: Library not loaded: /System/Library/Frameworks/...

Python, suds, Error.

When I'm trying get method from remote webservice it gives me error. My code is: portion=10 start=0 print self.stamp.datetime client=self.client while 1: print 'getting ids...........' fresh_ids=client.service.GetTopicsIDsUpdatedAfterDateTime(self.stamp.datetime,start,port...

What python virtual environment and deployment solution should I use?

I'm looking for a virtual environment solution for Python applications and I would like something that respects these requirements: Windows and Linux works with x86/x64 Python versions easy to use/maintain Python 2.6-2.7 compatible and preferably even 3.x source control friendly - I want to keep the packages in SCM. So far I identif...

Python Threading with Timer

I would like 3 Threads in Python to run for n seconds. I want to start them all at the same time and have them finish at the same time (within milliseconds). How do I do this? threading.Timer only starts after the previous one has been completed. ...

Critique my prime_factors() function

I just wrote this function to break a number into its prime factors. Is the algorithm I'm using a good one? How might I improve this function? So far it seems to be working quite well. from copy import copy # Decorator to memoize using the first argument only @memoize_first def prime_factors(n, prime_list=[None], prime_limit=[None]): ...

How to convert a Python string representing bytes into actual bytes?

I have a string like: "01030009" and I want to get another string (because in Python 2.x we use strings for bytes) newString which will produce this result: for a in newString: print ord(a) 0 1 0 3 0 0 0 9 Thanks ...

Getting parameters from a file via shell script into python script in the right format

I have the following shell script: #! /bin/sh while read page_section page=${page_section%%\ *} section=${page_section#* } #NOTE: `%* }` is NOT a comment wget --quiet --no-proxy www.cs.sun.ac.za/hons/$page -O html.tmp & wait # echo ${page_section%%\ *} # verify correct string chopping # echo ${page_section#* } # verify ...

Question about nested loops

I am new to programming and having some problem figuring out nested loops. I have a list of data that I want to extract from a larger file. I am able to extract one item of data from the larger file successfully but I need to extract 100 different trials from this larger file of thousands of trials. Each trial is one line of data of the ...

Python: get key with the least value from a dictionary

If I have a Python dictionary, how do I get the key to the entry which contains the minimum value? I was thinking about something to do with the min() function... Given the input: {320:1, 321:0, 322:3} It would return 321. Any ideas? Thanks! ...

Looking for a PHP's str_split() replacement

Let's say I have this little piece of code: <?php $tmp = str_split('hello world!',2); // $tmp will now be: array('he','ll','o ','wo','rl','d!'); foreach($tmp AS &$a) { // some processing } unset($tmp); ?> How can I do this in Python v2.7? I thought this would do it: the_string = 'hello world!' tmp = the_string.split('',2) for a...

Python treemap infograph

What is the easiest way to draw such graph in python ? Is there any library that suports it and I need only to provide data ? link: http://upload.wikimedia.org/wikipedia/commons/9/95/Heatmap_incito.png ...