python

Python: coerce new-style class

I want this code to "just work": def main(): c = Castable() print c/3 print 2-c print c%7 print c**2 print "%s" % c print "%i" % c print "%f" % c Of course, the easy way out is to write int(c)/3, but I'd like to enable a simpler perl-ish syntax for a configuration mini-language. It's notable that if I ...

Python: urlopen not downloading the entire site.

Greetings, I have done: import urllib site = urllib.urlopen('http://www.weather.com/weather/today/Temple+TX+76504') site_data = site.read() site.close() but it doesn't compare to viewing the source when loaded in firefox. I suspected the user agent and did this: class AppURLopener(urllib.FancyURLopener): version = "Mozilla/5.0...

Python compile all non-words except dot[.]

I am trying to break a line on all non-word patterns except .(dot) Usually I guess it can be done as [\W ^[.]] in java, but how to I do in python? ...

Django:How can I ensure that a view can only be directed from another view

I have two views def view1(request): do something return HttpResponseRedirect(reverse(view2), args1) Now I need view2 to only work if it's referred by view1. How do I do that? I did read it somewhere, not able to recollect @somefilter def view2(request): do something #view2 will only be referred from view1, else Http...

django combobox

ok so i`m makeing an app that has a file name field upload file field and a combobox, lets say I have smth like this for the combobox <select name="menu"> <option value="0" selected> select imp </option> <option value="1"> imp 1 </option> <option value="2"> imp 2 </option> <option value="3"> imp 3 </option> <option value=...

'in-place' string modifications in Python

In Python, strings are immutable. What is the standard idiom to walk through a string character-by-character and modify it? The only methods I can think of are some genuinely stanky hacks related to joining against a result string. -- In C: for(int i = 0; i < strlen(s); i++) { s[i] = F(s[i]); } This is super expressive and says...

uwsgi + django import error

Hello, I have a problem when I use django with uwsgi with the pythonpath. I have a django project named 'project' which is the /sites/django/ directory So to start uwsgi i use this command : /opt/uwsgi/uwsgi -s 127.0.0.1:9001 -C -M 4 -t 30 -A 4 -p 4 -d /var/log/uwsgi.log --pythonpath '/sites/django/project/' --module wsgi If I am i...

How to round the minute of a datetime object python

I have a datetime object produced using strptime(). >>> tm datetime.datetime(2010, 6, 10, 3, 56, 23) What I need to do is round the minute to the closest 10th minute. What I have been doing up to this point was taking the minute value and using round() on it. min = round(tm.minute, -1) However, as with the above example, it gives ...

Cast base class to derived class python (or more pythonic way of extending classes)

I need to extend the Networkx python package and add a few methods to the Graph class for my particular need The way I thought about doing this is simplying deriving a new class say NewGraph, and adding the required methods. However there are several other functions in networkx which create and return Graph objects (e.g. generate a ran...

What is a .pyc_dis file?

I have a folder that has a .pyc_dis file from a module. What can I do with it? How do I run it? What does this file even come from? I couldn't find any information with a Google search. Thanks. ...

How to simply read in input from stdin delimited by space or spaces

Hello I'm a trying to learn python, In C++ to read in string from stdin I simply do string str; while (cin>>str) do_something(str) but in python, I have to use line = raw_input() then x = line.split() then I have to loop through the list x to access each str to do_something(str) this seems like a lot of code just to get eac...

Is it easy to fully decompile python compiled(*.pyc) files?

I wanted to know that how much easy is to decompile python byte code. I have made an application in python whose source I want to be secure. I am using py2exe which basically relies on python compiled files. Does that secure the code? ...

Is it possible to wrap the text of xticks in matplotlib in python?

Anyone know if it is possible to wrap the xtick labels in matplotlib? Right now I've got the following code (kind of messy -- been hacking at it for a while): def plotResults(request, question_id): responses = ResponseOption.objects.filter(question__id=question_id).order_by('order').annotate(response_num=Count('response')) counts = ...

Throwing exception in Python and reading the message in jQuery

How can I throw an exception on my server and have the exception's message be read in JavaScript (I'm using AJAX with jQuery). My server environment is Google App Engine (Python). Here's my server code: def post(self): answer_text = util.escapeText(self.request.get("answer")) # Validation if ( len(str(answer_text)) < 3): ...

sqlalchemy: one-to-one relationship with declarative

What is the best way to create a one-to-one relationship in SQLAlchemy using declarative? I have two tables, foo and bar, and I want foo.bar_id to link to bar. The catch is that this is a one-way one-to-one relationship. bar must not know anything about foo. For every foo, there will be one and only one bar. Ideally, after selecting a...

How to iterate a dict of dynamic "depths" in python?

I have a dict data structure with various "depths". By "depths" I mean for example: When depth is 1, dict will be like: {'str_key1':int_value1, 'str_key2:int_value2} When depth is 2, dict will be like: {'str_key1': {'str_key1_1':int_value1_1, 'str_key1_2':int_value1_2}, 'str_key2': {'str_key2_1':int_value2_1, '...

What is the subprocess equivalent of passing "b" to os.popen2?

In Python 2.x, os.popen(command, "b") gives me a binary stream of the given command's output. This is primarily important on Windows, where binary and text streams actually give you different bytes. The subprocess module is supposed to replace os.popen and the other child-process spawning APIs. However, the conversion docs don't talk ...

How should I represent a bit flags int field in django admin?

I have a data model with a bitfield defined something like this: alter table MemberFlags add column title varchar(50) not null default ''; alter table MemberFlags add column value integer( 3) not null default 0; insert into MemberFlags (title, value) values ("Blacklisted", 1), ("Special Guest", 2), ("A...

How can I design a dict like sqlite class in python which can using different field as "key"?

Hi All, I have a such a data structure, "ID NAME BIRTH AGE SEX" ================================= 1 Joe 01011980 30 M 2 Rose 12111986 24 F 3 Tom 31121965 35 M 4 Joe 15091990 20 M I want to use python + sqlite to store and query data in a easy way. I am in trying to design a dict like...

Error in Python Mechanize - "mechanize._mechanize.BrowserStateError: not viewing HTML"

for link in br.links(url_regex="inquiry-results.jsp"): cb[link.url] = link for page_link in cb.values(): for link in br.links(url_regex="inquiryDetail.jis"): .................... url = link.absolute_url br.follow_link(link) ...................... br.follow_link(page_link) T...