python

Archiving (tar and compress) with metadata (user id, and ctime) in Python

I am in the process of backing up a filesystem and I need to make sure that the metadata is conserved (the file owner and creation time). The tarfile module in Python is really helpful, and I use it extensively in my solution. However, I cannot create the tar file with files conserving their metadata (presumably because copy and copy2 ...

How can I run Python code on a windows system?

I am used to using PHP and it is easy to set up, I can just run an exe package like Xampp and have apache and PHP running in 5 minutes on my windows system. Is there something similar to Python? ...

Python / Django: Is it wise to use "Item" as a class name in Python (in Django in this case)?

I'm creating this: # models.py class Item(models.Model): sku = models.CharField(max_length=20) class Attribute(models.Model): item = models.ForeignKey(Item, related_name='items') Is that going to cause naming collisions in Python? Like: # views.py some_object.items.create(sku='123abc') # Is there a place / way that this c...

Can Moblin run (and compile) Python scripts ?

Hello, I want to develop an app for Moblin for a competition. The development environment needed by Moblin is too complicated to setup. It uses Anjuta as its IDE, but I could never make sense of the entire compilation toolchain. However, I would like to know if Moblin could compile and run Python scripts. If I could write the app in P...

Reduce strings in python to a specific point

I have strings in my python application that look this way: test1/test2/foo/ Everytime I get such a string, I want to reduce it, beginning from the tail and reduced until the fist "/" is reached. test1/test2/ More examples: foo/foo/foo/foo/foo/ => foo/foo/foo/foo/ test/test/ => test/ how/to/implement/this => how/to/imp...

Python: Random is barely random at all?

I did this to test the randomness of randint: >>> from random import randint >>> >>> uniques = [] >>> for i in range(4500): # You can see I was optimistic. ... x = randint(500, 5000) ... if x in uniques: ... raise Exception('We duped ' + str(x) + ' at iteration number ' + str(i)) ... uniques.append(x) ... Traceback ...

setup.py: installing just a pth file?

As the final step in building a custom python, I need to add a myproject.pth. Currently I'm doing this in a Makefile: install: rm -f ../../lib/python2.6/site-packages/myproject.pth cp myproject.pth ../../lib/python2.6/site-packages/myproject.pth but I would like to encapsulate this in a setup.py. Unfortunt...

Convert Unicode/UTF-8 string to lower/upper case using pure & pythonic library

I use Google App Engine and cannot use any C/C++ extension, just pure & pythonic library to do conversion of Unicode/UTF-8 strings to lower/upper case. str.lower() and string.lowercase() don't. ...

Escape @ from python line command

I don't know if this is a problem with python or with the shell (zsh on linux), I've an argument like this: "@xyz" that starts with a "@" python the_script.py first_argument @second_argument third_arg I tried to escape @ with \ or \\, or use "" but the program doesn't start. If I leave the @ from @second_arguments everything's ok. ...

Python image processing of picture directly from the web

Hello, I am writing python code to take an image from the web and calculate the standard deviation, ... and do other image processing with it. I have the following code: from scipy import ndimage from urllib2 import urlopen from urllib import urlretrieve import urllib2 import Image import ImageFilter def imagesd(imagelist...

What is the equivalent of 'fread' from Matlab in Python?

I have practically no knowledge of Matlab, and need to translate some parsing routines into Python. They are for large files, that are themselves divided into 'blocks', and I'm having difficulty right from the off with the checksum at the top of the file. What exactly is going on here in Matlab? status = fseek(fid, 0, 'cof'); fposition...

Help parsing a page with python

Hi, I would like to parse a webpage to can get the url of the video download. I use python and firebug but I cant get the url link. Example: The url where I have to get the video link is: hxxp://www.rtve.es/mediateca/videos/20100125/saber-comer---salsa-verde-judiones-25-01-10/676590.shtml" The video is hxxp://www.rtve.es/resources/TE...

__cmp__ method is this not working as expected in Python 2.x?

class x: def __init__(self,name): self.name=name def __str__(self): return self.name def __cmp__(self,other): print("cmp method called with self="+str(self)+",other="+str(other)) return self.name==other.name # return False instance1=x("hello") instance2=x("there") print(instance1==i...

Resizing Plasma.Label

I'm writing plasmoid in python and when i'm trying to resize Plasma.Label nothing happens label3= Plasma.Label(self.applet) label3.setImage("/home/dark/fract.png") self.layout.addItem(label3) label3.resize(20,20) Any ideas? ...

Use of meta class in django

Can someone please explain why is meta class used in the following example. Ex: Class Employee (models.Model): name = models.ForeignKey(name) Gender = models.IntegerField() class Meta: ordering = ["Gender"] Thanks... ...

Leading and Trailing '0' gives error

random.randint(50,9) or random.randint(5,09) give errors, although just random.randint(5,9) ..works! Leading and trailing zero's aren't allowed in python without converting it to string or using x.f formatting? ...

HTTPS connection Python

Hello, I am trying to verify the that target exposes a https web service. I have code to connect via HTTP but I am not sure how to connect via HTTPS. I have read you use SSL but I have also read that it did not support certificate errors. The code I have got is from the python docs: import httplib conn = httplib.HTTPConnection("www.pyt...

Python: Simulate search algorithms in network models

I am using networkx package to draw power law graphs. I want to simulate a search algorithm on this graph and want to visually see the algorithm move from one node to another on the graph. How do I do that? ...

How to do while loops with multiple conditions

Hi I have a while loop in python condition1=False condition1=False val = -1 while condition1==False and condition2==False and val==-1: val,something1,something2 = getstuff() if something1==10: condition1 = True if something2==20: condition2 = True ' ' I want to break out of the loop when all these cond...

Raising an exception vs printing?

Whats the difference between raising an exception and simply printing an error. For example, whats the benefit of using the following: if size < 0: raise ValueError('number must be non-negative') instead of simply: if size < 0: print 'number must be non-negative' I'm a newbie, please take it easy on me. :) ...