python

Nested loop comparison in Python,Java and C

The following code in python takes very long to run. (I couldn't wait until the program ended, though my friend told me for him it took 20 minutes.) But the equivalent code in Java runs in approximately 8 seconds and in C it takes 45 seconds. I expected Python to be slow but not this much, and in case of C which I expected to be faste...

How can I extend Python's datetime.datetime with my own methods?

I'm trying to extend Python's datetime.datetime class with a couple of extra methods. So, for example I'm doing: import datetime class DateTime(datetime.datetime): def millisecond(self): return self.microsecond/1000 but then if I do >>> d = DateTime(2010, 07, 11, microsecond=3000) >>> print d.millisecond() 3 >>> delta = ...

Efficient bidirectional hash table in Python?

Python dict is a very useful datastructure: d = {'a': 1, 'b': 2} d['a'] # get 1 Sometimes you'd also like to index by values. d[1] # get 'a' Which is the most efficient way to implement this datastructure? Any official recommend way to do it? Thanks! ...

Can't get "Syntastic" vim plugin to work.

I've installed Syntastic plugin in vim. I can't get it to work. I've tried :SyntasticEnable but no luck. SyntasticEnable python in my vimrc doesn't work either (in fact, it doesn't even parse the command, an error is shown when I try to add it to my .vimrc : Not an editor command: SyntasticEnable python). How can I know what's going on?...

PHP devs that moved to Python, is the experience better?

Hi! I'm planning on moving to Python and I have a couple of additional questions along with the title: did you have more fun with python? are you as productive as when you're using PHP? what made you change to python? Would you do a project again in PHP? If so, why? Your answers would really be useful for us PHP devs wanting somethin...

Python: Thread safe dictionary with short lived keys, is this correct?

import threading import weakref _mainlock = threading.RLock() _job_locks = weakref.WeakValueDictionary() def do_thing(job_id): _mainlock.acquire() #Dictionary modification lock acquire _job_locks.setdefault(job_id, threading.RLock()) #Possibly modifies the dictionary _mainlock.release() _job_locks[job_id].acquire() tr...

How to extend pretty print module to tables?

I have the pretty print module, which I prepared because I was not happy the pprint module produced zillion lines for list of numbers which had one list of list. Here is example use of my module. >>> a=range(10) >>> a.insert(5,[range(i) for i in range(10)]) >>> a [0, 1, 2, 3, 4, [[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3],...

mod_wsgi, mod_python, or just cgi?

Hi, I've been playing around with my own webserver (Apache+Ubuntu) and python. From what I've seen there are 3(?) main ways of doing this: Apache configured to handle .py as cgi Apache configured to use mod_python that is now outdated(?) Apache configured to use mod_wsgi I recall reading that Django prefers mod_wsgi, and I'm kinda i...

join tables with django

queryObj = Rating.objects.select_related( 'Candidate','State','RatingCandidate','Sig','Office','OfficeCandidate').get( rating_id = ratingId, ratingcandidate__rating = ratingId, ratingcandidate__rating_candidate_id = \ officecandidate__office_candidate_id) This line gives me an error. I'm tryin...

can i store result directly as .txt file without getting displayed on GUI in python?

my programme gives me very large results containg huge number of symbols,numbers.so that GUI often becomes 'non responding'.also takes so much time to display result.is there any way to store result as .txt file without getting displayed on GUI? ...

What is the best way to connect to a sybase database from python?

Hello everyone, I am trying to retrieve data in a sybase data base from python and I was wondering which would be the best way to do it. I found this module but may be you have some other suggestions: http://python-sybase.sourceforge.net/ Thanks ...

Problem with Python logging RotatingFileHandler in Django website

I've a django powered website, and I use standard logging module to track web activity. The log is done via RotatingFileHandler which is configured with 10 log files, 1000000 byte each. The log system works, but this are the log files I get: -rw-r--r-- 1 apache apache 83 Jul 23 13:30 hr.log -rw-r--r-- 1 apache apache...

How complicate can a Django application go?

Hi, I'm tasked to create a simple CRUD MVC application, and I thought it's a good opportunity to learn python. Because of its great documentation, I'm thinking now that I'll go with Django. Now, this simple CRUD MVC application could become quite complicated in the future. I might have receive and issue JMS messages, display charts th...

re.sub issue when using group with \number

Hi, i'm trying to use a regexp to arrange some text, with re.sub. Let's say it's an almost csv file that I have to clean to make it totally csv. I replaced all \t by \n doing : t = t.replace("\n", "\t") ... and it works just fine. After that, I need to get some \t back to \n, for each of my CSV lines. I use for that this expression...

Python/Sqlite3 - is there a way to programmatically set .nullvalue?

Currently, Python is storing the string "None" for nulls returned by sqlite3. I read that you can change the default value for nulls in sqlite3 at the command line, but can you set this within the Python code? I'd like all of my blank results to look the same without having to loop through and check each value. So I want any nulls ret...

What are some good free parsing programs?

Are there any good free parsing programs out there in python or java? I have been using a lot of textfiles recently and they are all different. I have been spending a lot of time writing code to parse these textfiles. I was wondering if there is some program that could get all the names of a person out of a textfile or parse the file bas...

How to correctly extract data with Regular Expressions

Hi, i'm facing regulars expressions for the first time and i need to extract some data from this report (a txt file with formatting info): \n10: Vikelis M, Rapoport AM. Role of antiepileptic drugs as preventive agents for \nmigraine. CNS Drugs. 2010 Jan 1;24(1):21-33. doi:\n10.2165/11310970-000000000-00000. Review. PubMed P...

Fill OUTSIDE of polygon | Mask array where indicies are beyond a circular boundary?

I use plot(x,y,'r') to plot a red circle. x and y are arrays such that when paired as (x,y) and plotted, all points form a circle-line. fill(x,y,'r') plots a red circle that is filled in (or colored in) red. How can I keep the circle white on the inside, but fill outside the circle out to the axis boundaries? I looked into using fill_...

Dealing with dates and timezones in a python project

In a side project I have to manage, compare and display dates from different formats. What's the best design strategy to follow? I planned: All dates are parsed according their format and stored in the db in 9-tuple python format using UTC When I have to do calculations and compares I transform 9-tuple in datetime object (usin...

How to check if a path is absolute path or relative path in cross platform way with Python?

UNIX absolute path starts with '/', whereas Windows starts with alphabet 'C:' or '\'. Does python has a standard function to check if a path is absolute or relative? Or do I have to come up with a function using regular expression? ...