python

Django post save signal add an object into a manytomanyfield on the sender instance

I am using a post_save signal on an instance. In the callback I would like to add elements to the ManyToManyField of this very instance (named 'locales'). Here is my code: def after_insertion(sender, instance, **kwargs): locale = Locale(locale='en') locale.save() instance.locales.add(locale) Locale instance is created but...

Integrating a script language into a C++ application

Hi, I'm really new to C++ and I've come across a problem I've not been able to solve by reading documentations. I want to embed a script language into my c++ application. That language could be javascript, lua or preferably python. I'm not looking for something like Boost.Python / swig, something that is able to wrap my c++ functions ...

Python: Sum string lengths

Is there a more idiomatic way to sum strings in Python than by using a loop? length = 0 for string in strings: length += len(string) I tried sum(), but it only works for integers: >>> sum('abc', 'de') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: sum() can't sum strings [use ''.join(seq) inst...

Passing dictionaries to a Python script through the command line

How can I pass a dictionary to a python script from another python script over the command line? I use subprocess to call the second script. The options I've come to are: I) Build a module to parse a dictionary from a string (more in-depth than I had hoped to go). II) Use a temporary file to write a pickle, and pass the file's name as...

Regex in python to get javadoc-style comments in CSS

Hi there, I'm writing a python script to loop through a directory of CSS files and save the contents of any which contain a specifically-formatted javadoc style comment. The comment/CSS looks like this: /**thirdpartycss * @description Used for fixing stuff */ .class_one { margin: 10px; } #id_two { padding: 2px; } The regex...

Add a custom button to a Django application's admin page

Hi, I have an application in Django with a routine which would be available only to the admin. I'm quite new to the python/django world, so maybe my question is trivial. What I want to do is add a button to perform the routine in this application's section of the admin app. I'm quite confused from there, am I suppose to make a template ...

Character encoding is violated

I am trying to parse a file encoded in utf-8. No operation has problem apart from write to file (or at least I think so). A minimum working example follows: from lxml import etree parser = etree.HTMLParser() tree = etree.parse('example.txt', parser) tree.write('aaaaaaaaaaaaaaaaa.html') example.txt: <html> <body> <invalid ...

shell script remote execution using python

Is there a way that I can use Python on Windows to execute shell scripts which are located on a remote Unix machine? P.S: Sorry about the late edit. I do know of Paramiko, but I wanted to know if there is way of doing it without it. For starters, could it be done with subprocess()? ...

How to simulate ZipFile.open in Python 2.5?

I want to extract a file from a zip to a specific path, ignoring the file path in the archive. This is very easy in Python 2.6 (my docstring is longer than the code) import shutil import zipfile def extract_from_zip(name, dest_path, zip_file): """Similar to zipfile.ZipFile.extract but extracts the file given by name from the zi...

How can I replace the class by monkey patching?

How can I replace the ORM class - so it should not cause recursion !!! Problem: original class has the super call, when its got replaced - it causes self inheritance and causes maximum recursion depth exceed exception. i.e. class orm is calling super(orm, self).... and orm has been replaced by another class which inherits original orm.....

Are my permissions set correctly? (python)

Hello, In python I'm doing a os.system('chmod o+w filename.png') command so I can overwrite the file with pngcrush. These are the permissions after I set them in python: -rw-rw-rw- 1 me users 925 Sep 20 11:25 filename.png Then I attempt: os.system('pngcrush filename.png filename.png') which is supposed to overwrite the file, but...

Sort distributed couples from two lists

Having two lists, I want to get all the possible couples. (a couple can be only one element from list 1 and another from list 2) If I do a double "foreach" statement, I get it immediately (I am using python): couples = [] for e1 in list_1: for e2 in list_2: couples.append([l1, l2]) How can I sort couples list in a way tha...

why python doesn't need type declaration for python, other way what are the adv. of not declaring type?

If we know the type of variable or parameter very well, why not to declare them? I'd like to know why it's bad or not necessary. Sorry, I'm new on Python (from about 1 year) and before I was on C, VB, VB.NET and C# programming languages. With Python, I hope to have bad parameter types to be catched at compilation time. And I hope to...

Why do Python modules sometimes not import their sub-modules?

Hi all. I noticed something weird today I would like explained. I wasn't 100% sure how to even phrase this as a question, so google is out of the question. The logging module does not have access to the module logging.handlers for some odd reason. Try it yourself if you don't believe me: >>> import logging >>> logging.handlers Traceback...

Python Sort Two Dimensional Dictionary By First Key

I have a 2D dictionary in python indexed by two IPs. I want to group the dictionary by the first key. For example, the before would look like this: myDict["182.12.17.50"]["175.12.13.14"] = 14 myDict["182.15.12.30"]["175.12.13.15"] = 10 myDict["182.12.17.50"]["185.23.15.69"] = 30 myDict["182.15.12.30"]["145.33.34.56"] = 230 so for ke...

Tkinter text highlighting in python

Hello guys. I wanna know how to change the style of certain words, expressions .. etc based on certain pattens. I am using the Tkinter.Text widget and I am not sure how can we do such thing (The same idea of syntax highlighting in text editors). I am not sure even if this is the right widget to use for that purpose. ...

Selecting a Python Web Framework

Hello everybody. This may seem like a subjective question. But it is not (that's not the idea, at least). I'm developing an Advertising software (like AdWords, AdBrite, etc) and i've decide to use Python. And would like to use one of those well known web frameworks (Django, Cherrypy, pylons, etc). The question is: Given that it will ...

Properties in Python

whats the reason to use the variable the self._age? A similar name that doesn't link to the already used self.age? class newprops(object): def getage(self): return 40 def setage(self, value): self._age = value age = property(getage, setage, None, None) ...

Run a python script from another python script, passing in args.

I want to run a python script from another python script. Where I pass in variables as I would at command line. So for example I would run my first script that would iterate thru a list of values 0,1,2,3 and past those to the 2nd script "script2.py 0" then "script2.py 1", etc. I found SO 1186789 which is a similar question but ars's ...

pywin32 captive installation (avoid py*.dll getting installed in system32 directory)

I have python as an embedded scripting environment in my application. I supply the python bits (python26.dll, DLLs & Lib folders) with my application. All this to avoid asking users to install python (you know how it goes in big corporations). All works nice except pywin32. It installs pythoncom26.dll and pywintypes26.dll to the system3...