python

Python: Passing unicode string to C++ module

I'm working with an existing module at the moment that provides a C++ interface and does a few operations with strings. I needed to use Unicode strings and the module unfortunately didn't have any support for a Unicode interface, so I wrote an extra function to add to the interface: void SomeUnicodeFunction(const wchar_t* string) How...

pythonic way to rewrite an assignment in an if statement

Is there a pythonic preferred way to do this that I would do in C++: for s in str: if r = regex.match(s): print r.groups() I really like that syntax, imo it's a lot cleaner than having temporary variables everywhere. The only other way that's not overly complex is for s in str: r = regex.match(s) if r: p...

Python: How to write to http input stream

I could see a couple of examples to read from the http stream. But how to write to a http input stream using python? ...

Is this how you paginate, or is there a better algorithm?

I want to be able to take a sequence like: my_sequence = ['foo', 'bar', 'baz', 'spam', 'eggs', 'cheese', 'yogurt'] Use a function like: my_paginated_sequence = get_rows(my_sequence, 3) To get: [['foo', 'bar', 'baz'], ['spam', 'eggs', 'cheese'], ['yogurt']] This is what I came up with by just thinking through it: def get_rows(se...

python's mechanize wont properly parse a form

I'm trying to submit a form using python's mechanize but it wont properly parse the form in question. There are 4 other forms, which are parsed correctly except for this one form. The form is properly parsed in perl's www::mechanize though but i'd like to stick with python. Is there anyway of retrieving the html of the page and editing...

Why do you have to call .iteritems() when iterating over a dictionary in python?

Why do you have to call iteritems() to iterate over key, value pairs in a dictionary? ie dic = {'one':'1', 'two':'2'} for k, v in dic.iteritems(): print k, v Why isn't that the default behavior of iterating over a dictionary for k, v in dic: print k, v ...

Python code to open image in browser?

I want to open an image in the default browser with Python. I thought it might be as simple as webbrowser.open(path_to_file), but on XP at least that opens the Windows Picture and Fax Viewer instead. ...

python script in crontab gets input arguments of another process running at the same time

I run 2 python scripts from crontab at the same time each 30 min, e.g. 00,30 6-19 * * 0-5 /.../x.py site1 */3 6-19 * * 0-5 /.../y.py site2 At the beginning the both scripts make import of a module that prints some data to a log, e.g. name = os.path.basename(sys.argv[0]) site = sys.argv[1] pid = os.getpid() Occasionally (!) the sec...

Accessing the name that an object being created is assigned to using/starting from ast

I'm writing some code to determine the name that an object is assigned to. This is for general debugging work and to further familiarize myself with python internals. I have it structured as a class decorator so that all instances of that class will have their names recorded if it is possible to do. The code is fairly long so I won't po...

Twitter, Error: urllib.error.HTTPError: HTTP Error 401: Unauthorized

def send_to_twitter(): msg = "I am a message that will be sent to Twitter" password_manager = urllib.request.HTTPPasswordMgr() password_manager.add_password("Twitter API", "http://twitter.com/statuses", "username", "password") http_handler = urllib.request.HTTPBasicAuthHandler(password_manager) page_opener = urll...

How to import module in parent folder not working in egg

My project has the following structure: setup.py project/ __init__.py main.py errors.py lib/ li1b.py lib2.py I'm importing the errors module with from . errors import Error1, Error2, Error3 which works fine when running the program with python main.py. However when I build and install it as an egg with ...

How to parse a string in Java? Is in Java anything similar to re.finditer() in Python?

Hi, I have an input string with a very simple pattern - capital letter, integer, capital letter, integer, ... and I would like to separate each capital letter and each integer. I have been dealing with it for quite a while and can't figure out what is the best way to do that in Java, I have already tried regexp using Pattern and Match...

AI and Design of an El-fish like simulator environment?

Hi there, first post here on stack overflow, hoping to get some advice on how to construct a simulation program akin to the 1993 maxis simulator known as El-Fish wiki here , Also, game info here . Are there known "Simulation system" algorithm groups that can function and create real life interaction etc... e.g. the visualization known a...

Running a command line from python and piping arguments from memory

Hi, I was wondering if there was a way to run a command line executable in python, but pass it the argument values from memory, without having to write the memory data into a temporary file on disk. From what I have seen, it seems to that the subprocess.Popen(args) is the preferred way to run programs from inside python scripts. For ex...

__import__() calls __init__.py twice?

I was just wondering why __import__() calls a __init__ module twice when loading a package. test.py testpkg/ __init__.py test.py: pkg = __import__("testpkg", fromlist=['']) __init__.py: print "Called." After calling python test.py, Called. will be printed out twice. Why does python execute the __init__ "module" twice? ...

Django: Problem reading multi valued POST variable

I'm missing something obvious here. I am trying to process a POST request that contains a mixture of single value and multi value variables. I can get the single valued variables using request.POST.get('variable_name'), for example: logging.debug('sale_date: ' + request.POST.get('SALEDATE')) However, I can't get the multi value variab...

Creating a Relation in __init__ of a model (in Django)

Lets assume I had the following Model class A(Models.model): def __init__(self,data): B(a=self,data=data).save() class B(Models.model): data = somefieldtype a = Models.models.ForeignKey('A') now as you might suspect, there is an error in this Model definintion, as one cannot create a relation to the A instance before a...

Python persistent socket connection

Hello, I'm new to python :) I would like to create persistent socket. I tried to do this using file descriptors. What I tried is: Open a socket socket connection s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) Get it's file descriptor number fd = s.fileno() Open the file descriptor as I/O os.open(fd) But I get OSError: [Errno ...

order("-modified") with geomodel

Edit: Solved using key=lambda and learning what I'm actually doing. With gemodel like class A(GeoModel,search.SearchableModel): I'm trying to order by date using db.GeoPt to store google maps coordinates with GAE and geomodel I can map and match. But order("- modified") is not working. There is no trace. All ideas are welcome. The c...

Python / Django Web service Confusion

I am trying to explore more about web service in Python/Django and to be honest i am quite confused. There are so many things like SOAPpy, XML-RPC, JSON-RPC RESTful, web service. Basically all i want to know is what is the standard way of implementing web service in Python/Django and has anyone implemented in live production environmen...