python

Mimic Haskell with Python

Haskell provides the feature something like f = f1 . f2 How can I mimic that with Python? For example, if I have to do the 'map' operation two times, is there any way to do something like map . map in Python? x = ['1','2','3'] x = map(int,x) x = map(lambda i:i+1, x) ...

Check if the integer in a list is not duplicated, and sequential

testGroupList is a list of integer. I need to check the numbers in testGroupList is sequential and not duplicate numbers. Ignore the negative integer. For example, [1,2,-1,2,3,4] is an error as 2 is duplicated, but [-1,3,2,4,1,5] is OK. I implemented it as follows, and it's pretty ugly. Is there any clever way to do this? buff ...

How to treat the last element in list differently in Python?

I need to do some special operation for the last element in a list. Is there any better way than this? array = [1,2,3,4,5] for i, val in enumerate(array): if (i+1) == len(array): // Process for the last element else: // Process for the other element ...

How to determine if the given object is of given type in Python?

I always thought operator is determined if the given variable is of the given type. But I just determined it was not true: >>> class A(): pass ... >>> a = A() >>> a is A False How do I test if a is of type class A? Please advise. Thanks, Boda Cydo. ...

Difference between Python dynamic field lookup

What's the difference (if any) between model.__dict__['title_en'] and model.__getattribute__('title_en') and what's best practice ? ...

Why is Decimal('0') > 9999.0 True in Python?

This is somehow related to my question Why is ''>0 True in Python? In Python 2.6.4: >> Decimal('0') > 9999.0 True From the answer to my original question I understand that when comparing objects of different types in Python 2.x the types are ordered by their name. But in this case: >> type(Decimal('0')).__name__ > type(9999.0).__nam...

Why do people write #!/usr/bin/env python on the first line of a Python script?

It seems to me like the files run the same without that line. ...

Python for loop question

I was wondering how to achieve the following in python: for( int i = 0; cond...; i++) if cond... i++; //to skip an run-through I tried this with no luck. for i in range(whatever): if cond... : i += 1 ...

Add Keyboard Binding To Existing Emacs Mode

I'm attempting my first modification of Emacs. I recorded a little keyboard macro and had Emacs spit it out as elisp, resulting in: (setq add-docstring "\C-rdef\C-n\C-a\C-m\C-p\C-i\C-u6\"\C-u3\C-b") (global-set-key "\C-c\C-d" 'add-docstring) Searching the Emacs reference, though, revealed that C-c C-d is already bound in diff mode...

python: variable not getting defined after several conditionals

For some reason this program is saying that 'switch' is not defined. What is going on? #PYTHON 3.1.1 class mysrt: def __init__(self): self.DATA = open('ORDER.txt', 'r') self.collect = 0 cache1 = str(self.DATA.readlines()) cache2 = [] for i in range(len(cache1)): ...

What is the purpose of a zip function (as in Python or C# 4.0) ?

Someone asked How to do Python’s zip in C#?... ...which leads me to ask, what good is zip? In what scenarios do I need this? Is it really so foundational that I need this in the base class library? ...

Need help understanding "TypeError: default __new__ takes no parameters" error in python

For some reason I am having trouble getting my head around __init__ and __new__. I have a bunch of code that runs fine from the terminal, but when I load it as a plugin for Google Quick Search Box, I get the error TypeError: default __new__ takes no parameters. I have been reading about the error, and it's kind of making my brain spin. ...

Python help() function and the string.title function

Why doesn't import string;help(string.title) seem to work but help(string.strip) works just fine? I get the error Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'title' ...

Which XML library for what purposes?

A search for "python" and "xml" returns a variety of libraries for combining the two. This list probably faulty: xml.dom xml.etree xml.sax xml.parsers.expat PyXML beautifulsoup? HTMLParser htmllib sgmllib Be nice if someone can offer a quick summary of when to use which, and why. ...

using python 'with' statement with iterators?

Hi, I'm using Python 2.5. I'm trying to use this 'with' statement. from __future__ import with_statement a = [] with open('exampletxt.txt','r') as f: while True: a.append(f.next().strip().split()) print a The contents of 'exampletxt.txt' are simple: a b In this case, I get the error: Traceback (most recent call last): ...

How to ping an ip and get only the ms in the Tk with Python?

I want to make a little tk app that continuous ping an ip and only show the MS, like, "10ms" how could I do? ...

Pycassa | Python

Is anyone having experience working with pycassa I have a doubt with it. How do I get all the keys that are stored in the database? well in this small snippet we need to give the keys in order to get the associated columns (here the keys are 'foo' and 'bar'),that is fine but my requirement is to get all the keys (only keys) at once as P...

Scapy install issues. Nothing seems to actually be installed?

I have an apple computer running Leopard with python 2.6. I downloaded the latest version of scapy and ran "python setup.py install". All went according to plan. Now, when I try to run it in interactive mode by just typing "scapy", it throws a bunch of errors. What gives! Just in case, here is the FULL error message.. INFO: Can't i...

Problem with Tk and Ping in Python

I'm not being able to make this line work with Tk import os while(1): ping = os.popen('ping www.google.com -n 1') result = ping.readlines() msLine = result[-1].strip() print msLine.split(' = ')[-1] I'm trying to create a label and text = msLine.split... but everything freezes ...

can we display glass bar chart in python with google app engine

i am using bar chat and i want to use glass bar chart instead of that tutorials are given for PHP only. ...