python

Any hints on programming Dia with Python extensions?

I'm searching for documentation on how to do it properly. Any hints? ...

How to execute os.* methods as root?

Is it possible to ask for a root pw without storing in in my script memory and to run some of os.* commands as root? My script scans some folders and files to check if it can do the job makes some changes in /etc/... creates a folder and files that should be owned by the user who ran the script (1) can be done as a normal user. I ca...

I am downloading a file using Python urllib2. How do I check how large the file size is?

And if it is large...then stop the download? I don't want to download files that are larger than 12MB. request = urllib2.Request(ep_url) request.add_header('User-Agent',random.choice(agents)) thefile = urllib2.urlopen(request).read() ...

How am I able to assign a value to a literal? ('a' = 10)

def foo(**args): for k, v in args.items(): print type(k), type(v) for k, v in args.items(): k = v print k print type(k) foo(a = 10) foo(**{'a':10}) Gives me <type 'str'> <type 'int'> 10 <type 'int'> <type 'str'> <type 'int'> 10 <type 'int'> So I am confused how am I able to do this as k is a string, ...

Django formsets required

How to make all forms in django formset required? I tried to validate presence of all fields in cleaned_data overriding formset's clean() method but it just fails silently without any error displayed. Thanks! Source code: class BaseScheduleForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(BaseScheduleForm,...

pythoncomplete in vim - hardcode factory function returns?

I'm using pythoncomplete omnicompletion in vim. It works great when I instantiate classes directly, eg import numpy as np x = np.ndarray(l) then x attributes complete correctly. But I work with numpy and matplotlib so usually use factory functions ie x = np.zeros((2,2)) f = plt.figure() ax = f.add_subplot(111) Is there any wa...

Unable to query from entities loaded onto the app engine datastore

Hi, I am a newbie to python. I am not able to query from the entities- UserDetails and PhoneBook I loaded to the app engine datastore. I have written this UI below based on the youtube video by Brett on "Developing and Deploying applications on GAE" -- shoutout application. Well I just tried to do some reverse engineering to query from t...

Method to peek at a Python program running right now

Is it possible to find any information about what a Python program running right now is doing without interrupting it? Also, if it isn't possible, is there anyway to crash a running Python program so that I can at least get a stacktrace (using PyDev on Ubuntu)? I know I should have used logs or run it in debug mode or inserted a state...

is there a way to generate pdf containing non-ascii symbols with pisa from django template?

Hi. i'm trying to generate a pdf from template using this snippet: def write_pdf(template_src, context_dict): template = get_template(template_src) context = Context(context_dict) html = template.render(context) result = StringIO.StringIO() pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result) ...

Remove html formating "&gt;" from text file using Python csv.reader

I have a text file with ; used as the delimiter. The problem is that it has some html text formating in it such as &gt; Obviously the ; in this causes problems. The text file is large and I don't have a list of these html strings, that is there are many different examples such as $amp;. How can I remove all of them using python. The fil...

IDLE and unicode chars (2.5.4)

Why does IDLE handle one symbol correctly but not another? >>> e = '€' >>> print unichr(ord(e)) € # looks like a very thin rectangle on my system. >>> p = '£' >>> print unichr(ord(p)) £ >>> ord(e) 128 >>> ord(p) 163 I tried adding various # coding lines, but that didn't help. EDIT: browser should be UTF-8, else this will look rat...

Modifying list while iterating

l = range(100) for i in l: print i, print l.pop(0), print l.pop(0) The above python code gives the output quite different from expected. I want to loop over items so that I can skip an item while looping. Please explain. ...

Python/Django: Which authorize.net library should I use?

I need authorize.net integration for subscription payments, likely using CIM. The requirements are simple - recurring monthly payments, with a few different price points. Customer credit card info will be stored a authorize.net . There are quite a few libraries and code snippets around, I'm looking for recommendations as to which work b...

How to check which XP theme is enabled

I have a wxPython which works perfectly on window xp theme but on switching to 'classic theme' rich text cntrl comes up without border. I can enable border for classic theme but for that Q1. I need to know if classic theme is enabled. Q2.I am also not sure how many different theme could be there which may break my app appearance. so w...

Deciphering Python word unscrambler

I'm currently trying to dig deep into python and I have found a challenge on (hackthissite.org) that I'm trying to crack. I have to unscramble 10 words that are found in the provided wordlist. def permutation(s): if s == "": return [s] else: ans = [] for an in permutation(s[1:]): for pos in r...

Python Class with integer emulation

Given is the following example: class Foo(object): def __init__(self, value=0): self.value=value def __int__(self): return self.value I want to have a class Foo, which acts as an integer (or float). So I want to do the followng things: f=Foo(3) print int(f)+5 # is working print f+5 # TypeError: unsupported op...

Problem Using Python's subprocess.communicate() on Windows

I have an application that I am trying to control via Python and the subprocess module. Essentially what I do is start the application using Popen (which opens a command prompt within which the program executes) and then at some point in time later on in the execution I need to send a string (a command) to the STDIN of that program. Th...

Google App Engine and google authentication with redirect and HTTP POST

I have a form and I need to send the content to the server. I use google authentication because only authorized people can send to the server. The form is somthing like this: <form action="/blog/submit" method="post"> ... </form> The authentication is needed only during the submit, not entering the form page. So in the submit contro...

Python bitwise operations confusion

I came up with this "magic string" to meet the ID3 tagging specification: The ID3v2 tag size is encoded with four bytes where the most significant bit (bit 7) is set to zero in every byte, making a total of 28 bits. The zeroed bits are ignored, so a 257 bytes long tag is represented as $00 00 02 01. >>> hex_val = 0xFFFFFFFF >>> str...

is there a way to check if a param contains a class or a class instance?

I want the wrapper my_function to be able to receive either a class or class instance, instead of writing two different functions: >>> from module import MyClass >>> my_function(MyClass) True >>> cls_inst = MyClass() >>> my_function(cls_inst) True the problem is that I don't know in advance which type of classes or class instance...