Re: Dynamically determine base classes on instantiation

2012-08-17 Thread Richard Thomas
On Thursday, 16 August 2012 19:49:43 UTC+2, Steven D'Aprano wrote: > On Thu, 16 Aug 2012 10:03:51 -0700, Richard Thomas wrote: > > > > > class Foo(object): > > > def __new__(cls, arg): > > > if isinstance(arg, list): > > >

Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Richard Thomas
> a is a Foo > b is a Foo > therefore a and b are the same type What you mean here is "a and b share a common base class". -- http://mail.python.org/mailman/listinfo/python-list

Re: Dynamically determine base classes on instantiation

2012-08-16 Thread Richard Thomas
class Foo(object): def __new__(cls, arg): if isinstance(arg, list): cls = FooList elif isinstance(arg, dict): cls = FooDict return object.__new__(cls, arg) class FooList(Foo, list): pass class FooDict(Foo, dict): pass You could even hav

Re: setup.py for an extension

2012-03-21 Thread Richard Thomas
Assuming you have: lib/__init__.py lib/foo.py lib/foo.c Then: from distutils.core import setup, Extension setup(name="lib", packages=["lib"], ext_modules=[Extension("lib._foo", ["lib/foo.c"])]) -- http://mail.python.org/mailman/listinfo/python-list

Re: How to get a reference of the 'owner' class to which a method belongs in Python 3.X?

2012-03-17 Thread Richard Thomas
On Saturday, 17 March 2012 05:30:34 UTC, Cosmia Luna wrote: > I'm porting my existing work to Python 3.X, but... > > class Foo: > def bar(self): > pass > > mthd = Foo.bar > > assert mthd.im_class is Foo # this does not work in py3k mthd.im_class is the class of mthd.im_self not the

Re: building a dictionary dynamically

2012-02-06 Thread Richard Thomas
On Feb 4, 6:13 pm, noydb wrote: > How do you build a dictionary dynamically?  Doesn't seem to be an > insert object or anything.  So I need an empty dictionary that I then > want to populate with values I get from looping through a list and > grabbing some properties.  So simply, I have (fyi, arcp

Re: Function docstring as a local variable

2011-07-10 Thread Richard Thomas
> >>> def findself(): > >         """Find myself. Ooh look, there I am!""" >         import sys >         try: >                 1/0 >         except: >                 traceback=sys.exc_info()[2] >         # Now I'm not sure what to do with traceback. >         # traceback.tb_frame.f_code.co_name

Re: Determine attributes of calling method

2011-06-04 Thread Richard Thomas
On Jun 3, 9:35 pm, Joe wrote: > Hello, > > I'm trying to implement a way to restrict method usage based on the > caller's attributes.  In the following example I'd like to execute the > server method "bar" only if the caller's method has a "blue" value for > it's color attribute. > > The current o

Re: Finding the bitness of an arbitrary executable with Python

2011-05-09 Thread Richard Thomas
On May 9, 9:52 pm, Andrew Berg wrote: > I need to find whether a given file is 32-bit or 64-bit (and raise an > exception if the file doesn't exist or isn't an executable file). I > thought platform.architecture() would do this, but it returns ('64bit', > '') no matter what value I assign to the e

Re: checking if a list is empty

2011-05-06 Thread Richard Thomas
On May 6, 7:36 am, Jabba Laci wrote: > Hi, > > If I want to check if a list is empty, which is the more pythonic way? > > li = [] > > (1) if len(li) == 0: > ... > or > (2) if not li: > ... > > Thanks, > > Laszlo I prefer (1), it feels more explicit about what I'm testing. The fact that empty sequ

Re: Namespaces in functions vs classes

2011-04-17 Thread Richard Thomas
On Apr 17, 8:56 pm, Chris Rebert wrote: > On Sun, Apr 17, 2011 at 12:30 PM, Gerald Britton > > > > > > > > > > wrote: > > I apologize if this has been answered before or if it is easy to find > > in the docs. (I couldn't find it but might have missed it) > > > I'm trying to understand the differe

Re: return an object of a different class

2011-02-15 Thread Richard Thomas
On Feb 16, 2:23 am, s...@uce.gov wrote: > How can I do something like this in python: > > #!/usr/bin/python3.1 > > class MyNumbers: >    def __init__(self, n): >      self.original_value = n >      if n <= 100: >        self = SmallNumers(self) >      else: >        self = BigNumbers(self) > > clas

Re: How to populate all possible hierarchical clusterings from a set of elements?

2011-01-13 Thread Richard Thomas
On Jan 13, 3:59 pm, Alain Ketterlin wrote: > Richard Thomas writes: > > On Jan 13, 10:02 am, Alain Ketterlin > >> def clusterings(l): > >>     if len(l) == 1: > >>         print repr(l) > >>     else: > >>         n = len(l) > >>  

Re: How to populate all possible hierarchical clusterings from a set of elements?

2011-01-13 Thread Richard Thomas
On Jan 13, 10:02 am, Alain Ketterlin wrote: > justin writes: > > Suppose I have [1,2,3,4,5], then there are many ways of making > > clustering. > > Among them, I want to pair up terminals until there is only one left > > at the end. > > Are you trying "ascending hierarchical clustering" by any ch

Re: unittests with different parameters

2010-11-22 Thread Richard Thomas
On Nov 22, 11:38 am, Ulrich Eckhardt wrote: > Hi! > > I'm writing tests and I'm wondering how to achieve a few things most > elegantly with Python's unittest module. > > Let's say I have two flags invert X and invert Y. Now, for testing these, I > would write one test for each combination. What I

Re: Is there a way to pring a list object in Python?

2010-10-31 Thread Richard Thomas
On Oct 31, 7:04 pm, Zeynel wrote: > On Oct 31, 5:52 am, Dave Angel wrote: > > > > > > > > > > > On 2:59 PM, Zeynel wrote:> class Rep(db.Model): > > >      author = db.UserProperty() > > >      replist = db.ListProperty(str) > > >      unique = db.ListProperty(str) > > >      date = db.DateTimePro

Re: Is there a way to pring a list object in Python?

2010-10-31 Thread Richard Thomas
On Oct 31, 5:42 am, Zeynel wrote: > class Rep(db.Model): >     author = db.UserProperty() >     replist = db.ListProperty(str) >     unique = db.ListProperty(str) >     date = db.DateTimeProperty(auto_now_add=True) > > > > Rep().replist = L > Rep().put() > mylist = Rep().all().fetch(10) > > I

Re: question about a program

2010-10-08 Thread Richard Thomas
On Oct 8, 1:39 am, Logan Butler wrote: > question about an assignment: > > >>> places("home sweet home is here",' ') > > [4, 10, 15, 18] > > this is my code: > > def places(x, y): >     return [x.index(y) for v in x if (v == y)] > > so far I'm only getting > [4, 4, 4, 4] > > so the first value is

Re: why L[3:1]=['?'] become L.insert(3,'?')

2010-10-04 Thread Richard Thomas
On Oct 4, 3:26 pm, sd44 wrote: > In > part III > how does Python handle it if you try to extract a sequence in reverse, > with the lower bound greater than the higher bound (e.g., L[3:1])? Hint: > try > assigning to this slice (L[3:1]=['?']), and see where the value is put. > > >>> L=[1,2,3,4] >

Re: Upload files with wsgi

2010-09-29 Thread Richard Thomas
On Sep 28, 11:31 pm, Hidura wrote: > Hello, i have a project on Python3k, and i have a very big problem i > don' t find how take an upload file i am using the wsgiref lib, and or > theres any way to connect to the client in order to get the file by > myself? > > Thank you > Diego Hidalgo. > > -- >

Re: using modules

2010-09-06 Thread Richard Thomas
On Sep 6, 5:55 pm, Sal Lopez wrote: > The following code runs OK under 3.1: > > @filename=cats_and_dogs.py > > #!/usr/bin/python > > def make_sound(animal): >     print(animal + ' says ' + sounds[animal]) > > sounds = { "cat": "meow", "dog": "woof" } > > for i in sounds.keys(): >     make_sound(i)

Re: Subsets of Python implemented in Python

2010-07-16 Thread Richard Thomas
On Jul 17, 12:34 am, candide wrote: > I don't understand why some parts of the Python language (or the Python > standard library too) are implemented in C while some other parts are > implemented in the Python language itself. For instance, lists and > dictionnaries are implemented in C but sets a

Re: Argh! Name collision!

2010-07-06 Thread Richard Thomas
On Jul 7, 3:11 am, "Alf P. Steinbach /Usenet" wrote: > Donald Knuth once remarked (I think it was him) that what matters for a > program > is the name, and that he'd come up with a really good name, now all he'd had > to > do was figure out what it should be all about. > > And so considering Stu

Re: Python dynamic attribute creation

2010-06-25 Thread Richard Thomas
On Jun 25, 2:15 pm, WANG Cong wrote: > Hi, list! > > I have a doubt about the design of dynamic attribute creation by > assignments in Python. > > As we know, in Python, we are able to create a new attribute of > a class dynamically by an assignment: > > > > >>> class test: pass > ... > >>> test.a

Re: UnboundLocalError: local variable referenced before assignment

2010-06-08 Thread Richard Thomas
On Jun 8, 9:03 am, ch1zra wrote: > I have following code : > > import os, time, re, pyodbc, Image, sys > from datetime import datetime, date, time > from reportlab.lib.pagesizes import A4 > from reportlab.lib.units import cm > from reportlab.pdfgen import canvas > from reportlab.pdfbase import pdf

Re: Reading file bit by bit

2010-06-07 Thread Richard Thomas
On Jun 7, 10:17 am, Peter Otten <__pete...@web.de> wrote: > Alfred Bovin wrote: > > I'm working on something where I need to read a (binary) file bit by bit > > and do something depending on whether the bit is 0 or 1. > > > Any help on doing the actual file reading is appreciated. > > The logical u

Re: map is useless!

2010-06-06 Thread Richard Thomas
Python's map has the useful feature that nobody is in any doubt about what it does. I don't know much about Ruby I have to say but looking at that piece of syntax you gave I had no idea how to interpret it. Anyway, I looked it up. Calling an method on each of a collection of objects is best accomp

Re: function that counts...

2010-05-19 Thread Richard Thomas
For this kind of problem you should avoid all that stringification. I find it best to deal with sequences of digits of a fixed length and go from there. For example: def count1(m, n, cache={}): """Number of digit sequences of length `n` summing to `m`.""" if n < 0 or m < 0: return

Re: Generating a rainbow?

2010-04-08 Thread Richard Thomas
On Apr 8, 5:46 pm, Tobiah wrote: > I'm having a difficult time with this.  I want > to display a continuous range of hues using HTML > hex representation (#RRGGBB).  How would I go > about scanning through the hues in order to > make a rainbow? > > Thanks, > > Toby Look at the colorsys module. h

Re: Interfaces

2010-04-05 Thread Richard Thomas
On Apr 5, 4:40 pm, Roald de Vries wrote: > Dear all, > > PEP 245 and 246 about interfaces for python are both rejected for   > 'something much better' (GvR in 246's rejection notice). Does anybody   > know what this is? I am *very* curious! > > Kind regards, Roald Given that was in 2001, probably

Re: why (1, 2, 3) > [1, 2, 3] is true?

2010-02-25 Thread Richard Thomas
On Feb 25, 2:03 pm, fat bold cyclop wrote: > > Both are not equal, so the comparison returns an arbitrary result in Py2. > > Thanks, Stefan. If I understand you correctly the comparison is not > valid. > But I wonder if there is any logic behind this (in 2.x). > Is it possible to predict result of

Re: Can't get sys.stdin.readlines() to work

2010-01-31 Thread Richard Thomas
On Jan 31, 6:15 pm, tinn...@isbd.co.uk wrote: > I'm trying to read some data from standard input, what I'm actually > trying to do is process some date pasted in using the mouse cut and > paste on a Linux box (xubuntu 9.10) in a terminal window. > > First attempts failed so I'm now trying the trivi

Re: Object Integer mapping

2010-01-20 Thread Richard Thomas
On Jan 20, 4:43 pm, NighterNet wrote: > Need help on python version 3.1.x. I can't seem to know what going on > a bit. The code that I check that the hash is different every time. Is > there a way to make the hash the same? I using as to check the class > is the same variables but if it different

Re: Line indexing in Python

2009-12-18 Thread Richard Thomas
On Dec 18, 3:42 pm, seafoid wrote: > Hi Guys, > > When python reads in a file, can lines be referred to via an index? > > Example: > > for line in file: >      if line[0] == '0': >          a.write(line) > > This works, however, I am unsure if line[0] refers only to the first line or > the first c

Re: Variable class instantiation

2009-12-11 Thread Richard Thomas
On Dec 11, 9:26 am, Jan Mach wrote: > Hi everybody, > I am currently solving the following problem and I am stuck. I am trying > to create instance of the class of variable name. I know, that the > following works: > > if (something): >     classToUse = C1 > else: >     classToUse = C2 > > o = cla

Re: UnboundLocalError: local variable '_[1]' referenced before assignment

2009-12-09 Thread Richard Thomas
On Dec 9, 10:17 am, Gabriel Rossetti wrote: > Hello everyone, > > I get this error on python 2.6.1 on mac os x 10.6 : > > UnboundLocalError: local variable '_[1]' referenced before assignment > > here's the code that raises this: > > params = [ self.__formatData(paramProcFunc, query, p) for p in p

Re: test if an input string starts with a python expression

2009-12-07 Thread Richard Thomas
On Dec 8, 1:22 am, r0g wrote: > Torsten Mohr wrote: > > Hi, > > > i'd like to test if an input string starts with a python expression > > and also where that expression ends.  An example: > > > a_func(3*7, '''abc''') +5 pls some more > > > The first part until (inclusive) the 5 should be found as

Re: Any elegant way to construct the complete $k$-partite graph in Python?

2009-11-23 Thread Richard Thomas
On Nov 24, 2:45 am, geremy condra wrote: > On Mon, Nov 23, 2009 at 9:10 PM, geremy condra wrote: > > On Mon, Nov 23, 2009 at 9:03 PM, geremy condra wrote: > >> On Mon, Nov 23, 2009 at 7:05 PM, Paul Miller > >> wrote: > >>> I was wondering if there were any neat tools (like for instance, > >>> s

Re: parallel class structures for AST-based objects

2009-11-21 Thread Richard Thomas
On 22 Nov, 00:07, MRAB wrote: > Steve Howell wrote: > > I have been writing some code that parses a mini-language, and I am > > running into what I know is a pretty common design pattern problem, > > but I am wondering the most Pythonic way to solve it. > > > Basically, I have a bunch of really si

Re: unittest

2009-08-14 Thread Richard Thomas
On Aug 15, 4:28 am, Mag Gam wrote: > I am writing an application which has many command line arguments. > For example: foo.py -args "bar bee" > > I would like to create a test suit using unittest so when I add > features to "foo.py" I don't want to break other things. I just heard > about unittest

Bizarre import duplication.

2009-03-06 Thread Richard Thomas
Say I have a project like this: ./run.py ./package/__init__.py ./package/mod1.py ./package/subpackage/__init__.py ./package/subpackage/mod2.py ./package/subpackage/mod3.py And suppose that "." and "package" (or their absolute paths) are in sys.path. Now mod1.py and mod2.p

Re: True of False

2007-09-27 Thread Richard Thomas
On 27/09/2007, Casey <[EMAIL PROTECTED]> wrote: > On Sep 27, 12:48 pm, "Simon Brunning" <[EMAIL PROTECTED]> > wrote: > > On 9/27/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > > > I tried writing a true and false If statement and didn't get > > > anything? I read some previous posts, but I