python

Python: Invalid Syntax with test data using Pyparser

Using pyparser, I am trying to create a very simple parser for the S-Expression language. I have written a very small grammar. Here is my code: from pyparsing import * alphaword = Word(alphas) integer = Word(nums) sexp = Forward() LPAREN = Suppress("(") RPAREN = Suppress(")") sexp << ( alphaword | integer | ( LPAREN + ZeroOr...

Printing several binary data fields from Google DataStore?

I'm using Google App Engine and python for a web service. Some of the models (tables) I have in my web service have several binary data fields in them, and I'd like to present this data to a computer requesting it, all fields at the same time. Now, the problem is I don't know how to write it out in a way that the other computer knows whe...

Creating container relationship in declarative SQLAlchemy

My Python / SQLAlchemy application manages a set of nodes, all derived from a base class Node. I'm using SQLAlchemy's polymorphism features to manage the nodes in a SQLite3 table. Here's the definition of the base Node class: class Node(db.Base): __tablename__ = 'nodes' id = Column(Integer, primary_key=True) node_type = Col...

Stacking numpy recarrays without losing their recarrayness

Suppose I make two recarrays with the same dtype and stack them: >>> import numpy as np >>> dt = [('foo', int), ('bar', float)] >>> a = np.empty(2, dtype=dt).view(np.recarray) >>> b = np.empty(3, dtype=dt).view(np.recarray) >>> c = np.hstack((a,b)) Although a and b are recarrays, c is not: >>> c.foo Traceback (most recent call last):...

Losing session data when user logs in

Hello, I have been working on a shop that is built in Python on the back of the django framework, everything was working fine until I noticed that when a user proceeds to the checkout and is requested to log in they do so and their basket empties...obvioulsy this is not a great thing for a basket to do, I was wondering what is causing...

Django is_valid() not working with modelformset_factory

I've created a simple contact form using the modelformset_factory to build the form in the view using the DB model. The issue that I am having is that the is_valid() check before the save() is not working. When I submit the form with empty fields it still passes the is_valid() and attempts to write to the DB. I would like the is_vali...

Python: dynamic class generation: overwrite members

I have a python class hierarchy, that I want to extend at runtime. Furthermore every class in this hierarchy has a static attribute 'dict', that I want to overwrite in every subclass. Simplyfied it looks like this: 'dict' is a protected (public but with leading underscore) member class A(object): _dict = {} @classmethod de...

Looping through files in a folder

Hi, I'm fairly new when it comes to programming, and have started out learning python. What I want to do is to recolour sprites for a game, and I am given the original colours, followed by what they are to be turned into. Each sprite has between 20 and 60 angles, so looping through each one in the folder for each colour is probably the ...

Packet Queue in Python?

Hi, is there any way to queue packets to a socket in Python? I've been looking for something like the libipq library, but can't find anything equivalent. Here's what I'm trying to accomplish: Create tcp socket connection between server and client (both under my control). Try transmitting data (waiting for connection to fault -- e.g. ...

What are the limits of Python?

I spent a few days reading about C++ and Python and I found that Python is so much simpler and easy to learn. So I wonder does it really worth spending time learning it? Or should I invest that time learning C++ instead? What can C++ do and Python can't ? ...

Extract URLs out of email in Python

Dear Sam Adams Thanks for your submission to ourdirectory.com URL: http://myurlok.us Please click below link to confirm your submission. http://www.ourdirectory.com/confirm.aspx?id=1247778154270076 Once we receive your comfirmation, your site will be included for process! regards, http://www.ourdirectory.com Thank you! Should be ob...

What is the fool proof way to convert some string (utf-8 or else) to a simple ASCII string in python

Inside my python scrip, I get some string back from a function which I didn't write. The encoding of it varies. I need to convert it to ascii format. Is there some fool-proof way of doing this? I don't mind replacing the non-ascii chars with blanks or something else... ...

Updating value in binary file with Python

I'm trying to figure out how to update the data in a binary file using Python. I'm already comfortable reading and writing complete files using "array", but I'm having trouble with in place editing. Here's what I've tried: my_file.seek(100) my_array = array.array('B') my_array.append(0) my_array.tofile(my_file) Essentially, I want ...

How to treat a returned/stored string like a raw string in Python?

I am trying to .split() a hex string i.e. '\xff\x00' to get a list i.e. ['ff', '00'] This works if I split on a raw string literal i.e. r'\xff\x00' using .split('\\x') but not if I split on a hex string stored in a variable or returned from a function (which I presume is not a raw string) How do I convert or at least 'cast' a stored/r...

Weird MySQL Python mod_wsgi Can't connect to MySQL server on 'localhost' (49) problem

There have been similar questions on StackOverflow about this, but I haven't found quite the same situation. This is on a OS X Leopard machine using MySQL Some starting information: MySQL Server version 5.1.30 Apache/2.2.13 (Unix) Python 2.5.1 mod_wsgi 3 mysqladmin also has skip-networking listed as OFF I am able to connect to m...

Make Dictionary From 2 List

trying to make dictionary with 2 list one being the key and one being the value but I'm having a problem. This is what I have so far: d={} for num in range(10): for nbr in range(len(key)): d[num]=key[nbr] say my key is a list from 1 to 9 and value list is [2,4,0,9,6,6,8,6,4,5] how do i assign so it that its like {0:2, 1:4, et...

operation in arrays

hello everyone, I have a question, as I perform mathematical operations with an array of lists such as I get the sum of each array list getting a new list with the Valar for the sum of each list in the array. thanks for any response ...

Minimal binary diff for similar 1000 byte blocks with static noise?

I need a minimal diff for similar 1000 byte blocks. These blocks will have at most 20% of the bits different. The flipped bits will be like radio static -- randomly flipped bits with a uniform distribution over the whole block. Here's my pseudo code using XOR and lzo compression: minimal_diff=lzo(XOR(block1,block2)) Since the blocks a...

How to join components of a path when you are constructing a URL in Python

For example, I want to join a prefix path to resource paths like /js/foo.js. I want the resulting path to be relative to the root of the server. In the above example if the prefix was "media" I would want the result to be /media/js/foo.js. os.path.join does this really well, but how it joins paths is OS dependent. In this case I know...

django, datetime and timezones

I am using: datetime.now() to get the current time in an Event app that lets you create an event that has an end date, then all of the events are displayed in a calendar and if an event is passed due it is displayed in red. My issue is that I have some users in different timezones than me saying that the events are ending at the wrong...