python

Python: what does "...".encode("utf8") fix?

I wanted to url encode a python string and got exceptions with hebrew strings. I couldn't fix it and started doing some guess oriented programming. Finally, doing mystr = mystr.encode("utf8") before sending it to the url encoder saved the day. Can somebody explain what happened? What does .encode("utf8") do? My original string was a un...

Twisted and command line history console application

So i did the following (This app is a client and a server at the same time) class WebCheckerCommandProtocol(recvline.HistoricRecvLine): def connectionMade(self): self.sendLine("checker console. Type 'help' for help.") def lineReceived(self, line): ... def connectionLost(self, reason): # stop the re...

Dynamic Importing in Python (Dotted statments)

I'm having trouble with the following code: def get_module(mod_path): mod_list = mod_path.split('.') mod = __import__(mod_list.pop(0)) while mod_list: mod = getattr(mod, mod_list.pop(0)) return mod When I do get_module('qmbpmn.common.db_parsers') I get the error message: AttributeError: 'module' object has no...

Bulk Upload XML data to Google App Engine Using the YAML config method

Hi, I would like to bulk load the wurfl database (http://wurfl.sourceforge.net/) and I am not quite sure how to structure my data entities as well as how to write the transforms for the bulkloader config file. Here is a sample of a node from the wurfl file: <device id="generic" user_agent="" fall_back="root"> <group id="product...

Pass QuerySet object in template. Django.

How can i pass QuerySet object in to template. And then Iterate through it in tempalte. If ican do it....? Example model=MyModel.object.all() return render_to_response('template.html',{'model':model}) How it'll looks in template? Can I show field of foreigne key object in this template? ...

uploading data using Numpy.genfromtxt with multiple formats

I have a file with a time stamp as a column, and numbers in all the rest. I can either load one or the other correctly, but not both. Frustrating the heck out of me... This is what I am doing: import numpy as np file = np.genfromtxt('myfile.dat', skip_header = 1, usecols = (0,1,2,3), dtype = (str, float), delimiter = '\t') So colu...

JSON module for python 2.4?

I'm accustomed to doing import json in Python 2.6, however I now need to write some code for Python 2.4. Is there a JSON library with a similar interface that is available for Python 2.4? ...

Python: Getting "EOFError" when using paramiko sftp across modules as a global variable in a custom module

I'm using paramiko to transfer files with SFTP, on a apache server with mod_python. It works fine if I open the SFTP connection in the function called by mod_python (ex. index(req) ). But when I have 10-15 files that all uses a SFTP connection, I want to use a module that starts this connection. I have tried to make a function that re...

.NET equivalents of Some Python Functions

I am trying to port some Python code to .NET, and I was wondering if there were equivalents of the following Python functions in .NET, or some code snippets that have the same functionality. os.path.split() os.path.basename() Edit os.path.basename() in Python returns the tail of os.path.split, not the result of System.IO.Path.GetPath...

Building ALPY on OS X, cannot find python library path

I've been trying to build ALPY (http://www.stolk.org/alpy/) on Mac OS X, but I can't get past ./configure. I have python 2.5 installed, and most of the folders it uses are named python2.5, so I've symlinked them with a python folder in the same directory, but this had no effect. There was previously an error that required the symlinkin...

Multiple assignments under 'if' statement

Why can't I make multiple assignments under an if statement in python? Is there some syntax I am missing? I want to do this: files = ["file1", "file2", "file3"] print "\nThe following files are available: \n" i = 0 for file in files: i = i + 1 print i, file choice = int(raw_input("\Enter a file number: ")) if choice ==1: ...

Sending email from my domain vs from the admin google account?

I have a domain xyz.com and right now it is pointing to my app in appspot. I want to send email alerts to users for various events. However, appengine restricts email sender to admin email address which was used to create the google app engine account. Can I send emails on behalf of [email protected] using app engine? If not, is there a simp...

How to replace letters in a string with underscores?

Say I got a random word like WORD, i want to replace each of the letters of the word with _. How can I do it? Also, say if there is a space between the word like WO RD, I don't want it to replace the space with _. ...

Django mod_wsgi PicklingError while saving object

Do you know any solution to this: [Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162] mod_wsgi (pid=3072): Exception occurred processing WSGI script '/home/www/shop/django.wsgi'., referer: http://shop.domain.com/accounts/checkout/? [Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162] Traceback (most recent call last):, refere...

lapack import error with NumPy

Trying to import numpy in Python 2.6 I run into: from numpy.linalg import lapack_lite ImportError: libmkl_lapack.so: cannot open shared object file: No such file or directory There are multiple instances of Intel's Math Kernel Library on the machine providing libmkl_lapack.so and I'm pointing at them with every relevant or semi-releva...

Most Important Python Idioms

This question is inspired by several back-and-forths I've had recently about techniques that are or are not optimal for python programming. I may know other languages, but I am still learning in python. A wonderfully helpful comment was just recently posted by MikeD on a suboptimal answer of mine here that mentioned the operator module a...

Using csv modele to extract specific lines of text from a larger file

So I'm extracting the lines that I want from this larger file using this program: import csv name = ['NAMETHEFIRST,' 'NAMEANOTHERNAME '] data = csv.reader(open('C:\\bigfile.csv')) with open('C:\\smalldataset.xcl','w') as outf: csv.writer(outf).writerows(l for l in data if l[0] in name) The program runs. However I am only getting...

Accessing only part of a dictionary in a for using Python

Hey again all. My example dictionary is this data_dictionary = {1:'blue',2:'green',3:'red',4:'orange',5:'purple',6:'mauve'} The data_dictionary can have more elements depending on the incoming data . The first value is what we call a payload_index. I always get payload_index 1 to 4 . I need to assemble a list from this. Pretty eas...

Problem with nested for loops

I have to read two csv file, combine the row and write the result in a third csv file. first csv file have five row with user name in the first colunm.( 25 colunm in total) second csv file have five row with user name in the first colunm and user id in second colunm.(only 2 colunm) the third csv file will contain username+useridand al...

Are accessors in Python ever justified?

I realize that in most cases, it's preferred in Python to just access attributes directly, since there's no real concept of encapsulation like there is in Java and the like. However, I'm wondering if there aren't any exceptions, particularly with abstract classes that have disparate implementations. Let's say I'm writing a bunch of abst...