python

improving Boyer-Moore string search

I've been playing around with the Boyer-Moore sting search algorithm and starting with a base code set from Shriphani Palakodety I created 2 additional versions (v2 and v3) - each making some modifications such as removing len() function from the loop and than refactoring the while/if conditions. From v1 to v2 I see about a 10%-15% impr...

How are these type of python decorators written?

I'd like to write a decorator that would limit the number of times a function can be executed, something along the following syntax : @max_execs(5) def my_method(*a,**k): # do something here pass I think it's possible to write this type of decorator, but I don't know how. I think a function won't be this decorator's first argum...

Python: better way to open lots of sockets

I have the following program to open lot's of sockets, and hold them open to stress test one of our servers. There are several problem's with this. I think it could be a lot more efficient than a recursive call, and it's really still opening sockets in a serial fashion rather than parallel fashion. I realize there are tools like ab th...

How does os.path map to posixpath.pyc and not os/path.py?

What is the underlying mechanism in Python that handles such "aliases"? >>> import os.path >>> os.path.__file__ '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/posixpath.pyc' ...

installing easy_install for Python 2.6.2 (missing?)

I am not a python user, I'm just trying to get couchdb-dump up and running and it's in an "egg" file which I guess needs easy_install. I have Python 2.6.2 running on my computer but it seems to know nothing about easy_install or setuptools... help! What can I do to fix this??? edit: you may note from the setuptools page that there are ...

Do I have any obligations if I upload an egg to the CheeseShop?

Suppose I'd like to upload some eggs on the Cheese Shop. Do I have any obligation? Am I required to provide a license? Am I required to provide tests? Will I have any obligations to the users of this egg ( if any ) ? I haven't really released anything as open source 'till now, and I'd like to know the process. ...

Find functions explicitly defined in a module (python)

Ok I know you can use the dir() method to list everything in a module, but is there any way to see only the functions that are defined in that module? For example, assume my module looks like this: from datetime import date, datetime def test(): return "This is a real method" Even if i use inspect() to filter out the builtins, I...

Is 'if element in aList' possible with Django templates?

Does something like the python if "a" in ["a", "b", "c"]: pass exist in Django templates? If not, is there an easy way to implement it? ...

Python: StopIteration exception and list comprehensions

I'd like to read at most 20 lines from a csv file: rows = [csvreader.next() for i in range(20)] Works fine if the file has 20 or more rows, fails with a StopIteration exception otherwise. Is there an elegant way to deal with an iterator that could throw a StopIteration exception in a list comprehension or should I use a regular for l...

How to model an object with references to arbitrary number of arbitrary field types? (django orm)

I'd like to define a set of model/objects which allow for one to represent the relationship: field_set has many fields where fields are django.db.model field objects (IPAddressField, FilePathField etc). My goals is to have a ORM model which supports the following type of 'api'. From a controller view lets say: # Desired api below def...

Python Lambda Problems

What's going on here? I'm trying to create a list of functions: def f(a,b): return a*b funcs = [] for i in range(0,10): funcs.append(lambda x:f(i,x)) This isn't doing what I expect. I would expect the list to act like this: funcs[3](3) = 9 funcs[0](5) = 0 But all the functions in the list seem to be identical, and be se...

Platform-independent version of /var/lib and ~/.config

We see that programs like apt-get store information in several places: /var/cache/apt <- cache /var/lib/apt <- keyrings, package db, states, locks, mirrors /etc/apt <- configuration file ~/.aptitude/config <- user configuration file So we see four kinds of paths here: Cache path Data path System-wide configur...

looking for a more pythonic way to access the database

I have a bunch of python methods that follow this pattern: def delete_session(guid): conn = get_conn() cur = conn.cursor() cur.execute("delete from sessions where guid=%s", guid) conn.commit() conn.close() Is there a more pythonic way to execute raw sql. The 2 lines at the beginning and end of every method are st...

hex to string formatting conversion in python

I used to generate random string in the following way (now I've switched to this method). key = '%016x' % random.getrandbits(128) The key generated this way is most often a 32 character string, but once I've got 31 chars. This is what I don't get: why it's 32 chars, not 16? Doesn't one hex digit take one character to print? So if I...

Noob components design question

Updated question, see below I'm starting a new project and I would like to experiment with components based architecture (I chose PyProtocols). It's a little program to display and interract with realtime graphics. I started by designing the user input components: IInputDevice - e.g. a mouse, keyboard, etc... An InputDevice may have ...

Manually logging out a user, after a site update in Django

I have a website,which will be frequently updated. Sometimes changes happen to User specific models and are linked to sessions. So after I update my site, I want the user to logout and log back in. So I would logout the user right then. If he logs back in, he will see the latest updates to the site.How do I do it? ...

Assigning a Iron Python list to .NET array

I have a list comprehension operating on elements of an .NET array like obj.arr = [f(x) for x in obj.arr] However the assignment back to obj.arr fails. Is it possible to convert a list to a .NET array in IronPython? ...

python long running daemon job processor

I want to write a long running process (linux daemon) that serves two purposes: responds to REST web requests executes jobs which can be scheduled I originally had it working as a simple program that would run through runs and do the updates which I then cron’d, but now I have the added REST requirement, and would also like to chang...

Looking for testing/QA idea for Python Web Application Project

Hi, all, I have the 'luck' of develop and enhance a legacy python web application for almost 2 years. The major contribution I consider I made is the introduction of the use of unit test, nosestest, pychecker and CI server. Yes, that's right, there are still project out there that has no single unit test (To be fair, it has a few doctes...

Komodo Edit Changes Python sys.path If you "Show in Explorer"

I am using Komodo Edit, a code editor. When I right click on projects and click "Show in Explorer", it will pop up a box just like Windows Explorer at the directory my project is. This is very convenient. However, I noticed an insidious side effect. When you try to run a python file with this window that looks exactly like Windows Expl...