python

How to detect the OS default language in python?

Is there any universal method to detect the OS default language? (regardless what is the OS that is running the code) import os os.getenv('LANG') The above code works under Linux, does it work under other OS? ...

sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings

Using SQLite3 in Python, I am trying to store a compressed version of a snippet of UTF-8 HTML code. Code looks like this: ... c = connection.cursor() c.execute('create table blah (cid integer primary key,html blob)') ... c.execute('insert or ignore into blah values (?, ?)',(cid, zlib.compress(html))) At which point at get the error: ...

Finding the last group in a regular expression

Hello everyone, Three underscore separated elements make my strings : - first (letters and digits) - middle (letters, digits and underscore) - last (letters and digits) The last element is optional. Note : I need to access my groups by their names, not their indices. Examples : String : abc_def first : abc middle : def last : None ...

Why does corrcoef return a matrix?

Hi, It seems strange to me that np.corrcoef returns a matrix. correlation1 = corrcoef(Strategy1Returns,Strategy2Returns) [[ 1. -0.99598935] [-0.99598935 1. ]] Does anyone know why this is the case and whether it is possible to return just one value in the classical sense? ...

How do I get the same functionality as C's __FUNCTION__ in Python?

In C, I can put a log printf inside a function like this: void xx_lock (int xx_flag) { printf ("%s: START with %d\n", __FUNCTION__, xx_flag); } so I can copy the same line where I need in any function and it displays the function name in the log. I want something similar in Python. But if I use __name__ the same way, it displays ...

py-appscript is starting a new Finder instance

i have a py2app application, which runs an appscript using py-appscript. the Applescript code is this one line: app('Finder').update(<file alias of a certain file>) What this normally does is update a file's preview in Finder. It works most of the time, except for Leopard. In Leopard, everytime that script is executed, instead of updat...

How to search through a gtk.ListStore in pyGTK and remove elements?

I have the following code (where store is a gtk.ListStore and titer is a gtk.TreeIter. The docs say that if there is no next row, iter_next() will return None, hence the break when that is found. It is supposed to search through the ListStore of (int, str) and remove the one item whose int component matches item_id. while True: ...

Numeric Sort in Python

I know that this sounds trivial but I did not realize that the sort() function of Python was weird. I have a list of "numbers" that are actually in string form, so I first convert them to ints, then attempt a sort. list1=["1","10","3","22","23","4","2","200"] for item in list1: item=int(item) list1.sort() print list1 Gives me: ...

Trying to call readline() on a file object in python but it's pausing

I'm using the readline() function to read data from a file object obtained through the subprocess module: proc = subprocess.Popen(cmd, bufsize=0, stdout=subprocess.PIPE). This allows me to use proc.stdout as a file-like object with proc.stdout.readline(). My issue is that this pauses waiting for input and I'd like it to time out and m...

Deploying python executables- PyInstaller, cx-freeze, etc.

I'm looking to find a way to bundle a python app into stand-alone executables so my windows and mac using friends can use it without installing ugly dependencies. Looking online I've found a few utilities to help do this, including py2exe for windows and py2app for mac, as well as PyInstaller, cx-freeze, and bbfreeze. What have ya'll use...

python regexp help

Good day. Little question about reg exp. I have a string look like http://servercom/smth/Age=&amp;amp;Filter=2&amp;amp; How can i cut &amp; with regexp from url? After regexp url-string must be http://server.com/smth/Age=1&amp;Filter=2&amp; ...

Simple problem with dicT in Python

I have this dicT in my code that contain some positions. position = ['712,352', '712,390', '622,522'] when I'm trying to run this part def MouseMove(x,y): ctypes.windll.user32.SetCursorPos(x,y) with MouseMove(position[0]), the compiler says to me that I need 2 arguments on this command... how can I so...

Calculating Time Difference

Hello... at the start and end of my program, I have from time import strftime print int(strftime("%Y-%m-%d %H:%M:%S") Y1=int(strftime("%Y")) m1=int(strftime("%m")) d1=int(strftime("%d")) H1=int(strftime("%H")) M1=int(strftime("%M")) S1=int(strftime("%S")) Y2=int(strftime("%Y")) m2=int(strftime("%m")) d2=int(strftime("%d")) H2=int(...

How to remove 'None' from an Appended Multidimensional Array using numpy

Hello all, I need to take a csv file and import this data into a multi-dimensional array in python, but I am not sure how to strip the 'None' values out of the array after I have appended my data to the empty array. I first created a structure like this: storecoeffs = numpy.empty((5,11), dtype='object') This returns an 5 row by 11 ...

is there a better way to hold this data then a dictionary of dictionaries of dictionaries? python

I am creating a data structure dynamically that holds car information. The dictionary looks something like this: cars = {'toyota': {'prius': {'transmission':'automatic', 'mpg':30, 'misc':[]}}} The outermost dictionary contains car brand (toyota, bmw, etc.), the second dictionary contains model (prius, m5, etc.) and the inner dictionar...

All possible combinations of a list of a list

Hi I am in desperate need for some algorithm help when combining lists inside lists. Assuming that I have the following data structure: fields = [ ['a1', 'a2', 'a3'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3'], ['d1', 'd2', 'd3'] ] I am trying to write a generator (Python) that will yield each possi...

Is there a way to automate restarting the python process after every change I make to Django models?

I am using Django with Passenger on Dreamhost. Every time I make a change to models, settings or views I need to pkill python from a terminal session. Does anyone know of a way to automate this? Is this something that Passenger can do? Thanks. ...

Compiling a regex inside a function that's called multiple times

If you compile a regex inside a function, and that function gets called multiple times, does Python recompile the regex each time, or does Python cache the compiled regex (assuming the regex doesn't change)? For example: def contains_text_of_interest(line): r = re.compile(r"foo\dbar\d") return r.match(line) def parse_file(fn...

django shell triggering Postgres idle transaction problems

It's not the fault of the django (iPython) shell, actually. The problem is developers who open the django shell ./manage.py shell run through some queries (it often only generates selects), and then either leave the shell running or somehow kill their (ssh) session (actually, I'm not sure if the latter case leaves the transaction open - ...

How does Pythonic garbage collection with numpy array appends and deletes?

Hi all, I am trying to adapt the underlying structure of plotting code (matplotlib) that is updated on a timer to go from using Python lists for the plot data to using numpy arrays. I want to be able to lower the time step for the plot as much as possible, and since the data may get up into the thousands of points, I start to lose valua...