python

Python - to check if a char is in dictionary and if not to deal with it

I am going about transliteration from one source language(input file) to a target language(target file) so I am checking for equivalent mappings in a dictionary in my source code, certain characters in the source code don't have an equivalent mapping like comma(,) and all other such special symbols. How do I check if the character belong...

Modify entity data either transactionally or not (depending on the need)

What is the best way to keep code modular and decoupled but avoid entering a transaction twice? Entities often have class methods to load, modify, and store data. Often, this must be transactional to be consistent with child/sibling/cousin entities. Here is the pattern: class MyEntity(db.Model): # ... some properties @classmethod ...

Why do I get "expected an indented block" when I try to run my Python script ?

I have an error which says "expected an indented block" Could you please guide me on how to deal with this error. Thank you:) Code example: for ch in f: ( translatedToken = english_hindi_dict[ch] ) if (ch in english_hindi_dict) else (translatedToken = ch) ...

SQLAlchemy getting column data types of query results

from sqlalchemy import create_engine, MetaData, ForeignKey engine = create_engine("mysql://user:passwd@localhost/shema", echo=False) meta = MetaData(engine, True) conn = engine.connect() tb_list = meta.tables["tb_list"] tb_data = meta.tables["tb_data"] tb_list.c.i_data.append_foreign_key( ForeignKey(tb_data.c.i_id) ) q = tb_list.oute...

How do I implement a simple cross platform Python daemon?

I would like to have my Python program run in the background as a daemon, on either Windows or Unix. I see that the python-daemon package is for Unix only; is there an alternative for cross platform? If possible, I would like to keep the code as simple as I can. ...

How to execute a VS2008 command from Python and grab its output?

I wish to run tf changeset 12345 Using the Visual Studio 2008 Command tool. It is located in: "c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\" and the command that gets launched is: %comspec% /k ""c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"" x86 I would like to append the "tf changeset 12345" to it s...

Python Text widget in Tkinter

from Tkinter import * root = Tk() root.title("Whois Tool") text = Text() text1 = Text() text1.config(width=15, height=1) text1.pack() def button1(): text.insert(END, text1) b = Button(root, text="Enter", width=10, height=2, command=button1) b.pack() scrollbar = Scrollbar(root) scrollbar.pack(side=RIGHT, fill=Y) text.config(...

How to force an ImportError on development machine? (pwd module)

I'm trying to use a third-party lib (docutils) on Google App Engine and have a problem with this code (in docutils): try: import pwd do stuff except ImportError: do other stuff I want the import to fail, as it will on the actual GAE server, but the problem is that it doesn't fail on my development box (ubuntu). How to make...

Avoid race condition when asserting file permissions in Python

An application wants to parse and "execute" a file, and wants to assert the file is executable for security reasons. A moments thought and you realize this initial code has a race condition that makes the security scheme ineffective: import os class ExecutionError (Exception): pass def execute_file(filepath): """Execute seria...

How do I handle Python XML-RPC output and exceptions?

I have created a simple Python XML-RPC implementation, largely based on the examples. However, it sends output like this: foo.bar.com - - [13/Feb/2010 17:55:47] "POST /RPC2 HTTP/1.0" 200 - ... to the terminal, even if I redirect standard out and standard error to a file using >> or >. I'm doing this with the following line: python f...

Using TinyMCE for sites with a dark background

I'm using django-tinymce in my Django website. Through the admin interface one can edit a SimplePage object which has a tinymce.models.HTMLField. The website visitor will then see the html rendered in the content area of the page. Problem is, the website itself has a dark background, and the TinyMCE textarea has a white one. By default ...

More than 1 docstrings for a single module/function etc.?

I'm using python 3.1. Is it possible to create more than 1 docstring for a single module or function? I'm creating a program, and I'm intending to have multiple docstrings with a category for each. I intend to give other people the program so they can use it, and to make things easy for programmers and non-programmers alike, I'm putting...

Prevent a console app from closing when not invoked from an existing terminal?

There are many variants on this kind of question. However I am specifically after a way to prevent a console application in Python from closing when it is not invoked from a terminal (or other console, as it may be called on Windows). An example where this could occur is double clicking a .py file from the Windows explorer. Typically I ...

Google Wave gadget configure / set properties

How can you configure or mutate a Google Wave gadget after creating one in Python? The following code will load the gadget via XML: from waveapi import document gadget = document.Gadget('http://domain.com/gadget.xml') The API reference says you can pass a dictionary of initial properties, but I can't find any information on where the ...

How to add an HTML class to a Django form's help_text?

I have a simple form class MyForm(forms.Form): ... fieldname = forms.CharField(help_text="Some help text") I can then display this form with django's handy {{ form.as_ul }} if I like, now I need to stylise the help_text and I have no idea how. Django doesn't appear to wrap that string in anything that will let my CSS get to i...

python: inserting a variable value into a variable name

I would like to insert the value of a variable into the name of another variable in python. In a shell script this would be something like: for n in `more list` do var_$n = some_calculation done but I can't see how to do a similar thing in python. Is there a way or should I be using an alternative approach? thanks, Andy ...

boolean string formatting in Python?

I see I can't do: "%b %b" % (True, False) in Python. I guessed %b for b(oolean). Is there something like this? ...

AttributeError: 'module' object has no attribute 'printable'

I have this problem. Any idea? from OpenGL.raw.GLUT import * File "/usr/lib/python2.6/site-packages/OpenGL/raw/GLUT/__init__.py", line 6, in <module> from OpenGL.raw.GLUT.constants import * File "/usr/lib/python2.6/site-packages/OpenGL/raw/GLUT/constants.py", line 7, in <module> from OpenGL import platform, ...

Pickle or json?

I need to save to disk a little dict object which keys are strings and values are ints and then recover it. Something like this: {'juanjo': 2, 'pedro':99, 'other': 333} Which and why is the best option? Serialize it with pickle or with simplejson? I'm using Python 2.6 ...

How to execute some code when a file is modified using python?

Hello, I want to execute one funtion each time a file gets written with new data (gets modified) and I'm using Python. How can I do it? Thanks in advance! :) ...