python

Create file path from variables

Hello, I am looking for some advice as to the best way to generate a file path using variables, currently my code looks similar to the following: path = /my/root/directory for x in list_of_vars: if os.path.isdir(path + '/' + x): # line A print(x + ' exists.') else: os.mkdir(path + '/' + x) ...

Can Python be made to generate tracing similar to bash's set -x?

Is there a similar mechanism in Python, to the effect set -x has on bash? Here's some example output from bash in this mode: + for src in cpfs.c log.c popcnt.c ssse3_popcount.c blkcache.c context.c types.c device.c ++ my_mktemp blkcache.c.o +++ mktemp -t blkcache.c.o.2160.XXX ++ p=/tmp/blkcache.c.o.2160.IKA ++ test 0 -eq 0 ++ echo /tm...

Python string formatting + UTF-8 strange behaviour

When printing a formatted string with a fixed length (e.g, %20s), the width differs from UTF-8 string to a normal string: >>> str1="Adam Matan" >>> str2="אדם מתן" >>> print "X %20s X" % str1 X Adam Matan X >>> print "X %20s X" % str2 X אדם מתן X Note the difference: X Adam Matan X X אדם מתן X Any i...

Python: catching particular exception

I have such code (Python 2.5, GAE dev server): try: yt_service.UpgradeToSessionToken() // this line produces TokenUpgradeFailed except gdata.service.TokenUpgradeFailed: return HttpResponseRedirect(auth_sub_url()) # this line will never be executed (why?) except Exception, exc: return HttpResponseRedirect(auth_sub_url()) # in...

Is there a framework or pattern for applying filters to data?

The problem: I have some hierarchical data in a Django application that will be passed on through to javascript. Some of this data will need to be filtered out from javascript based on the state of several data classes in the javascript. I need a way of defining the filters in the backend (Django) that will then be applied in javascript...

[about PAMIE] how to get the value of the 'class' attribute in a link?

I wrote the sentence as follows: allLinkValues = ie.getLinksValue('class') but the return values are all None, don't know why... ...

Socket 'No route to host' error.

I have a connection which is behind a restrictive firewall which only allows HTTP(S) access through a proxy (10.10.1.100:9401). The IP address I get is dynamic and the subnet mask is 255.255.255.255 (I know, weird!). I tried to write a simple Python socket program to connect to the proxy in order to send some HTTP requests: import sock...

Join string and None/string using optional delimiter

I am basically looking for the Python equivalent to this VB/VBA string operation: FullName = LastName & ", " + FirstName In VB/VBA + and & are both concatenation operators, but they differ in how they handle a Null value: "Some string" + Null ==> Null "Some string" & Null ==> "Some string" This hidden feature allows for the first l...

How do I get a list of all parent tags in BeautifulSoup?

Let's say I have a structure like this: <folder name="folder1"> <folder name="folder2"> <bookmark href="link.html"> </folder> </folder> If I point to bookmark, what would be the command to just extract all of the folder lines? For example, bookmarks = soup.findAll('bookmark') then beautifulsoupcommand(bookmarks[...

Python: PIL replace a single RGBA color

I have already taken a look at this question: SO question and seem to have implemented a very similar technique for replacing a single color including the alpha values: c = Image.open(f) c = c.convert("RGBA") w, h = c.size cnt = 0 for px in c.getdata(): c.putpixel((int(cnt % w), int(cnt / w)), (255, 0, 0, px[3])) ...

Is django-piston mature enough?

Hello. I'm developing an advertising site. Want to use Web-Services for the requests. I mean, a publisher site will put a JS snippet and it will pull a banner through a REST GET. Is this framework mature enough to implement this functionality? Thanks ...

Python: Adding element to list while iterating

I know that it is not allowed to remove elements while iterating a list, but is it allowed to add elements to a python list while iterating. Here is an example: for a in myarr: if somecond(a): myarr.append(newObj()) I have tried this in my code and it seems to works fine, however i dont know if its because i am jus...

Why is my data not being represented properly in my SQLAchemy model?

I have a peculiar SQLAlchemy ORM problem. This is occurring in a Pylons application, against a Postgresql 8.2 database using psycopg2 as my database driver under SQLAlchemy 0.6.0 (and tried with 0.6.4 as well) I have defined a User model object that has (at minimum) the following properties: class User(Base): __tablename__ = 'users...

What can you execute on visually selected text in vim?

Hello, What are the possible operations (with keyboard shortcuts) that you could execute on a visually selected text? For the simplicity consider this simple piece: a b cd 1 p a b cd 2 y a b cd 3 t a b cd 4 h a b cd 5 o a b cd 6 n One specific question: is it possible to execute Python/shell ...

Slow mergesort implementation, what's wrong?

Hello, I am getting unexpected(?) results from this mergesort implementation. It's extremely slow compared to my three-way quicksort(also written in python). My quicksort finishes with 10000 elements after about 0.005s while mergesort needs 1.6s! Including the source code for both implementations. Mergesort: #Merges two sorted lists...

Using Relative Paths To Log Files In Pylons' development.ini

I am working on a Pylons app that runs on top of Apache with mod_wsgi. I would like to send logging messages that my app generates to files in my app's directory, instead of to Apache's logs. Further, I would like to specify the location of logfiles via a relative path so that it'll be easier to deploy my app on other people's servers....

Using Nltk and Wordnet how do i convert simple tense verb into its present, past or past participle form?

Hi Using Nltk and Wordnet how do i convert simple tense verb into its present, past or past participle form? For example: I want to write a function which would give me verb in expected form as follows. v = 'go' present = present_tense(v) print present # prints "going" past = past_tense(v) print past # prints "went" Any suggestion...

Good tutorial on XML/CSV download from websites

Using pythonm there a good tutorial on downloading from websites both XML and CSV formats. Trying to get info from financial websites with authorizations. I have id/Pw. Any thoughts, TIA I found below, it doesnot work, any help in fixing? Im looking to get stock/option prices. _version__ = "0.3" __date__ = "2008-05-09" __author...

How to specify argument type in a dynamically typed language, i.e. Python?

Is there any such equivalent of Java String myMethod (MyClass argument) {...} in Python? Thank you, Tomas ...

packing and unpacking variable length array/string using the struct module in python

Hi, I am trying to get a grip around the packing and unpacking of binary data in Python 3. Its actually not that hard to understand, except one problem: what if I have a variable length textstring and want to pack and unpack this in the most elegant manner? As far as I can tell from the manual I can only unpack fixed size strings dire...