python

General questions regarding Python language

I'm a newbie to programming and I've decided to start with Python. Just curious though, is it enough/recommended to learn Python from online tutorials or from books? I want to go further than simple "Hello World!" programs. I'm not sure if books will actually teach you how to make more advanced programs. One example is Exif-py. How do y...

remove numbers from a list without changing total sum

I have a list of numbers (example: [-1, 1, -4, 5]) and I have to remove numbers from the list without changing the total sum of the list. I want to remove the numbers with biggest absolute value possible, without changing the total, in the example removing [-1, -4, 5] will leave [1] so the sum doesn't change. I wrote the naive approach,...

Weblogic domain and cluster creation with WLST

I want to create a cluster with 2 managed servers on 2 different physical machines. I have following tasks to be performed (please correct me if I miss something) Domain creation. Set admin server properties and create AdminServer under SSL Create logical machines for the physical ones Create managed servers create cluster with the m...

How do you listen to notifications from iTunes on a Mac (Using the NSDistributedNotificationCenter)

Hello All, Looking for help/tutorials/sample code of using python to listen to distributed notifications from applications on a mac. I know the py-objc lib is the bridge between python and mac/cocoa classes, and the Foundation library can be used to add observers, but looking for examples or tutorials on how to use this to monitor iTune...

make python replace un-encodable chars with a string by default

I want to make python ignore chars it can't encode, by simply replacing them with the string "<could not encode>". E.g, assuming the default encoding is ascii, the command '%s is the word'%'ébác' would yield '<could not encode>b<could not encode>c is the word' Is there any way to make this the default behavior, across all my proje...

Programmable transparent forward proxy

I'm looking for a way to script a transparent forward proxy such as the ones that users point their browsers to in proxy settings. I've discovered a distinct tradeoff in forward proxies between scriptability and robustness. For example, their are countless proxies developed in Ruby and Python that allow you to inspect each request resp...

Python - specify which function in file to use on command line

Hi guys, Assume you have a programme with multiple functions defined. Each function is called in a separate for loop. Is it possible to specify which function should be called via the command line? Example: python prog.py -x <<<filname>>> Where -x tells python to go to a particular for loop and then execute the function called in th...

Why should exec() and eval() be avoided?

I've seen this multiple times in multiple places, but never have found a satisfying explanation as to why this should be the case. So, hopefully, one will be presented here. Why should we (at least, generally) not use exec() and eval()? EDIT: I see that people are assuming that this question pertains to web servers – it doesn't. I can...

How to refactor this python code block to be more efficient

This code block works - it loops through a file that has a repeating number of sets of data and extracts out each of the 5 pieces of information for each set. But I I know that the current factoring is not as efficient as it can be since it is looping through each key for each line found. Wondering if some python gurus can offer bet...

Deepcopy a simple Python object

I have an object which defines a __deepcopy__ method. I would like a function that will deepcopy it not by the method given by it, but in the default way that objects of the class object are copied. How could I do that? I think I could try to code it but there are probably many "gotchas" I won't be thinking of. The reason I'm doing it ...

fonts clipping with PIL

This image was created with PIL. See how the g's and the y's are cut off in this image? How can I prevent this? The code that created this image is pretty straight forward (abbreviated): import Image, ImageDraw, ImageFont im = Image.new("RGBA", (200, 200), 'white') draw = ImageDraw.Draw(im) font = ImageFont.truetype("VeraSe.ttf", 1...

How do you clone a class in Python?

I have a class A and i want a class B with exactly the same capabilities. I cannot or do not want to inherit from B, such as doing class B(A):pass Still i want B to be identical to A, yet have a different i: id(A) != id(B) Watch out, i am not talking about instances but classes to be cloned. ...

help with python urllib2 import error

In my script, I've imported urrlib2 and the script was working fine. After reboot, I get the following error: File "demo.py", line 2, in <module> import urllib2 File "/usr/lib/python2.6/urllib2.py", line 92, in <module> import httplib File "/usr/lib/python2.6/httplib.py", line 78, in <module> import mimetools File "/...

Rotating Proxies for web scraping

I've got a python web crawler and I want to distribute the download requests among many different proxy servers, probably running squid (though I'm open to alternatives). For example, it could work in a round-robin fashion, where request1 goes to proxy1, request2 to proxy2, and eventually looping back around. Any idea how to set this up?...

Setting an alias to a Django "plug-in" app

Hello, is there some pythonic way to load a django app trough an alias name? This I think would be one way to make an app more "pluggable-friendly". there is a pattern used in settings.py: INSTALLED_APPS = ('... ','...', ) where INSTALLED_APPS is a tuple containing names of apps. That's fine, but I don't want to put in certain apps ...

python - urrlib2 request https site - getting 400 error

Hi... using the following snip of code to access a url with a post. i can get it using wget and the following: wget --post-data 'p_calling_proc=bwckschd.p_disp_dyn_sched&p_term=201010' https://spectrumssb2.memphis.edu/pls/PROD/bwckgens.p%5Fproc%5Fterm%5Fdate for some reason, i'm having an issue with my python text, in that i get a er...

Python: incorporating index from for loop as part of a list name

How does one generate a set of lists that incorporate the index from a for loop in the list name: for j in range(10): li"j" = [] How can the index 'j' be part of the name, so the lists are li0, li1, li2, ... Thanks! ...

python eric ide

i was wondering if anyone knows where i could download some highlight styles for this one :) thanks ...

Item assignment on bytes object in Python

GAHH, code not working is bad code indeed! in RemoveRETNs toOutput[currentLoc - 0x00400000] = b'\xCC' TypeError: 'bytes' object does not support item assignment How can I fix this: inputFile = 'original.exe' outputFile = 'output.txt' patchedFile = 'original_patched.exe' def GetFileContents(filename): f = open(filename, ...

How to execute python scripts in windows?

I have a simple script blah.py: import sys print sys.argv[1] If I execute my script by: python c:/..../blah.py argument It prints argument but if I execute script by: blah.py argument error occurs: IndexError... So arguments do not pass to script. python.exe in PATH. Folder with blah.py also in PATH. python.exe is default progra...