python

How to refine an initial query in Django?

Following the reply to this question (Thanks again Ellie P!) I created a search page and a results page. For instance if you search for the lawyer "delelle" the result page shows her firm, school and year graduated. But instead of displaying her info, I want to display other lawyers who graduated from the same school the same year. T...

Python’s ftplib STOR reliable?

I'm using this code to upload myfile.txt from my windows machine to a ftp server. after the upoad the script deletes the file on my local machine (I'm not deleting it on the ftp). try: ftp = FTP(ftp.host.com) ftp.login(your_username, your_password) file = open(myfile.txt, "rb") ftp.storbinary('STOR myfile.txt', file) ...

How can I get the version defined in setup.py (setuptools) in my package?

How could I get the version defined in setup.py from my package? (for --version, or other purposes) thanks! ...

Python-style classmethod for C#?

Is there a way to do what classmethod does in Python in C#? That is, a static function that would get a Type object as an (implicit) parameter according to whichever subclass it's used from. An example of what I want, approximately, is class Base: @classmethod def get(cls, id): print "Would instantiate a new %r with ID...

Python How to I check if last element has been reached in iterator tool chain?

for elt in itertools.chain.from_iterable(node): if elt is the last element: do statement How do I achieve this ...

How can I break up this long line in Python?

How would you go about formatting a long line such as this? I'd like to get it to no more than 80 characters wide: logger.info("Skipping {0} because its thumbnail was already in our system as {1}.".format(line[indexes['url']], video.title)) Is this my best option? url = "Skipping {0} because its thumbnail was already in our system as...

Python Identity Problem: Multiple Personality Disorder. Need Code Shrink

I stumbled upon the following python weirdity: >>> two = 2 >>> ii = 2 >>> id(two) == id(ii) True >>> [id(i) for i in [42,42,42,42]] [10084276, 10084276, 10084276, 10084276] >>> help(id) Help on built-in function id in module __builtin__: id(...) id(object) -> integer Return the identity of an object. This is guaranteed to b...

Python count sub lists in nested list

I have created a list -> a = [[1,2,3],[4,5,6],[7,8,9]] 1) How do I get the count of number of sub-lists in a? Like in this case it is 3 2) I am using iterator tool chain for traversing this list for elt in itertools.chain.from_iterable(node): Is there any way to know if I have traversed a sub list ? ...

Utilizing objects in another class in Python

In code(pseudo) like this def path(): dirList = ['c:\\', 'y:\\', 'z:\\'] home_folder = 'peter.txt' complete = [s + home_folder for s in dirList] print complete def fileWrite(): filename = 'c:\peter.txt' text = 'Hello World' file = open(filename, 'w') file.write(text) file.close() I can make both wo...

Python If Statement with Multiple Conditions

Hi. Here is some code: if (message.value[0] == "/" or message.value[0] == "\"): do stuff. I'm sure it's a simple syntax error, but something is wrong with my if statement. Any ideas to help out a python newbie? Cheers ...

After doing a SQLAlchemy update(), is there a way to get the changed values?

After calling update(), I know that the return value has a .rowcount attribute that will reveal how many rows were changed. Is there a way to get the actual new values that they were changed to? For example, if I do the SQLAlchemy equivalent of: UPDATE t SET x=x+1 WHERE y=z ...is there a way to get the new value for x? Or do I have t...

BeautifulSoup HTML table parsing

I am trying to parse information (html tables) from this site: http://www.511virginia.org/RoadConditions.aspx?j=All&r=1 Currently I am using BeautifulSoup and the code I have looks like this from mechanize import Browser from BeautifulSoup import BeautifulSoup mech = Browser() url = "http://www.511virginia.org/RoadConditions.aspx...

Practices while releasing the python/ruby/script based web applications on production

I am purely a windows programmer and spend all my time hacking VC++. Recently I have been heading several web based applications and myself built applications with python (/pylons framework) and doing projects on rails. All the web projects are hosted on ubuntu linux. The RELEASE procedures and check list we followed for building and...

Return Django form contents on error

All, I have a template page say x.html i have 3 text fields name(varchar2) ,age(int),school(varchar2) in it. If the users enters values in the form in x.html(say values name="a" ,age="2" ,school="a") and submit it.I need to return the same values back to x.html indicating an error. My question is how to return the same values to x.ht...

Lazy SAX XML parser with stop/resume

I am pretty sure the answer is no but of course there are cleverer guys than me! Is there a way to construct a lazy SAX based XML parser that can be stopped (e.g. raising an exception is a possible way of doing this) but also resumable ? I am looking for a possible solution for Python >= 2.6 with standard XML libraries. The "lazy" part...

Python - temporarily modify the current process's environment

I use the following code to temporarily modify environment variables. @contextmanager def _setenv(**mapping): """``with`` context to temporarily modify the environment variables""" backup_values = {} backup_remove = set() for key, value in mapping.items(): if key in os.environ: backup_values[key] = o...

Python: sort the list

I want to sort the array c. But I don't get the answer a,b,c,d. Instead I get a,b,d,c. What could I do, for sorting the whole array and not only one row? EDIT: I want to sort the numbers. And the connected letters, should have the same order like the sorted numbers. sorry my question wasn't clear. Maybe I should join number and letters ...

Catching warnings pre-python 2.6

In Python 2.6 it is possible to suppress warnings from the warnings module by using with warnings.catch_warnings(): warnings.simplefilter("ignore") fxn() Versions of Python before 2.6 don't support with however, so I'm wondering if there alternatives to the above that would work with pre-2.6 versions? ...

Python C API and data persistent in memory?

I'm considering integrating some C code into a Python system (Django), and I was considering using the Python / C API. The alternative is two separate processes with IPC, but I'm looking into direct interaction first. I'm new to Python so I'm trying to get a feel for the right direction to take. Is it possible for a call to a C initiali...

Opening multiple windows from a list in WxPython

I have a little program that goes to news aggregater's, gets the hrefs, and returns in a window. I want to have multiple windows open if multiple sites are choosen, right now, it will only go to the first one in a list, and completes perfectly. I assume I am not passing the the contents of the list properly to the next step in my program...