python-2.x

In Python 2.x, using backticks to get decimal string from int object is Horrible?

In Python 2.x, using backticks to get decimal string from int object is Horrible? Because backticks are repr(), not str()? I have noticed that when I answering this question. In Python source, they have same function in Python source, intobject.c (reprfunc)int_to_decimal_string, /* tp_repr */ .... (reprfunc)int_to_decimal_s...

How to force PyYAML to load strings as unicode objects?

The PyYAML package loads unmarked strings as either unicode or str objects, depending on their content. I would like to use unicode objects throughout my program (and, unfortunately, can't switch to Python 3 just yet). Is there an easy way to force PyYAML to always strings load unicode objects? I do not want to clutter my YAML with !!p...

Can one Python project use both 2.x and 3.x code?

Hi all - I'm going to start on a long (~1-year) programming project in Python. I want to use wxPython for my GUI (supports 2.6), but I also want to use 3.1 for the rest of the project (to start using the 3.x syntax). Is there any way for me to design a project that mixes 2.x and 3.x modules? Or should I just bite the bullet and use eit...

Yield only as many are required from a generator

I wish to yield from a generator only as many items are required. In the following code a, b, c = itertools.count() I receive this exception: ValueError: too many values to unpack I've seen several related questions, however I have zero interest in the remaining items from the generator, I only wish to receive as many as I ask for...

python: trouble printing short utf-encoded strings

(The following is using Python 2.6.1) I have 2 strings: >>> a = u'\u05e8\u05db\u05e1' >>> b = u'\u05e8\u05db\u05e1 \u05d4\u05d9\u05d0 \u05de\u05d0\u05d9\u05e8\u05d4 \u05d1\u05e4\u05e0\u05e1' I encode them: >>> ua = a.encode('utf-8') >>> ub = b.encode('utf-8') >>> ua '\xd7\xa8\xd7\x9b\xd7\xa1' >>> ub '\xd7\xa8\xd7\x9b\xd7\xa1 \xd7\x9...

Using named arguments with variable length un-named arguments in Python

I apologize if this question has already been asked/answered, I would have expected that to be the case but was unable to find any related questions... I'd like to create a python function that takes two mandatory arguments, one named argument, and some unknown number of other, non-named arguments as so: def my_function(arg1, arg2, arg...

How to write Russian characters in file?

In console when I'm trying output Russian characters It gives me ??????????????? Who know why? I tried write to file - in this case the same situation. for example f=open('tets.txt','w') f.write('some russian text') f.close inside file is - ?????????????????????????/ or p="some russian text" print p ????????????? In additional...

How to use python modules that were renamed 3 in a cross compatible way?

There are several modules that were renamed in Python 3 and I'm looking for a solution that will make your code work in both python flavors. In Python 3, __builtin__ was renamed to builtins. Example: import __builtin__ #... __builtin__.something # appearing multiple times ("something" may vary) ...

Python utf-8 handling

Hi, I am using Python 2.6.1 and am having utf-8 related problem with my code. This problem is reproducible with this code: # -*- coding: utf-8 -*- import os, sys import string, time import codecs, re bDATA='"Domenick Lombardozzi","Eddie Marsan","Isaach De Bankolé","John Hawkes"' print (bDATA) fileObj = codecs.open("btvresp1.txt", "r", ...

Basic python arithmetic - division

I have two variables : count, which is a number of my filtered objects, and constant value per_page. I want to divide count by per_page and get integer value but I no matter what I try - I'm getting 0 or 0.0 : >>> count = friends.count() >>> print count 1 >>> per_page = 2 >>> print per_page 2 >>> pages = math.ceil(count/per_pages) >>> p...

Safest way to convert float to integer in python?

Hi, Python's math module contain handy functions like floor & ceil. These functions take a floating point number and return the nearest integer bellow or above it. However these functions return the answer as a floating point number. For example: In [1]: import math In [2]: f=math.floor(2.3) In [3]: f Out[3]: 2.0 What is the safest w...

Python UTF-8 comparison

a = {"a":"çö"} b = "çö" a['a'] >>> '\xc3\xa7\xc3\xb6' b.decode('utf-8') == a['a'] >>> False What is going in there? edit= I'm sorry, it was my mistake. It is still False. I'm using Python 2.6 on Ubuntu 10.04. ...

calculating factorial in Python

When calculating math.factorial(100) I get: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L Why is there an L at the end of the number? ...

Getting ready to convert from Python 2.x to 3.x

As we all know by now (I hope), Python 3 is slowly beginning to replace Python 2.x. Of course it will be many MANY years before most of the existing code is finally ported, but there are things we can do right now in our version 2.x code to make the switch easier. Obviously taking a look at what's new in 3.x will be helpful, but what ar...

Number in python - 010

When using the interactive shell: print 010 I get back an 8. I started playing around using other numbers having zeroes before (0110 = 72, 013 = 11) but I could not figure it out... What is going on here? ...

Is there a description of how cmp works for dict objects in Python 2?

I've been trying to make a dict subclass inheriting from UserDict.DictMixin that supports non-hashable keys. Performance isn't a concern. Unfortunately, Python implements some of the functions in DictMixin by trying to create a dict object from the subclass. I can implement these myself, but I am stuck on cmp I cannot find a succinct...

Uninstall python built from source?

I've installed python 2.6 from source, and somehow later mistakenly installed another python 2.6 from a package manager too. I can't find a way to uninstall a python that was built from source, is this possible/easy? Running ubuntu 10.04 Thanks. ...

Learn Python the Hard Way Exercise 17 Extra Question(S)

I'm doing Zed Shaw's fantastic Learn Python The Hard Way, but an extra question has me stumped: Line 9--10 could be written in one line, how? I've tried some different thoughts, but to no avail. I could move on, but what would the fun in that be? from sys import argv from os.path import exists script, from_file, to_file = argv print "...

Compare result from hexdigest() to a string

I've got a generated MD5-hash, which I would like to compare to another MD5-hash from a string. The statement below is false, even though they look the same when you print them and should be true. hashlib.md5("foo").hexdigest() == "acbd18db4cc2f85cedef654fccc4a4d8" Google told me that I should encode the result from hexdigest(), since...

python round problem

I am facing the problem while dividing my max_sum = 14 total_no=4 so when i do print "x :", (total_sum/total_no) , I get 3 and not 3.5 I tried many ways for printing but failed, can somebody let me know what way I get in 3.5 format? Thank you ...