non-gobject based dbus library?

2014-10-17 Thread Tycho Andersen
Hi all,

An application I maintain recently moved away from the gobject event
loop to the tulip/trollius/asyncio event loop. However, we were using
python-dbus, which internally uses the gobject event loop, as our main
event loop. This worked nicely when we were gobject based, but now
that we're not, I'm trying to find alternatives to python-dbus so that
we can re-implement the dbus-based functionality.

I tried gbulb: https://bitbucket.org/a_ba/gbulb but it seems mostly
abandoned, and I had to hack on it to even get our application to
boot, and some things didn't work correctly.

Does anyone have any ideas?

(Please keep me in CC, I'm not subscribed.)

Thanks!

Tycho
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: better way for ' '.join(args) + '\n'?

2012-10-26 Thread Tycho Andersen
On Fri, Oct 26, 2012 at 09:49:50AM +0200, Ulrich Eckhardt wrote:
 Hi!
 
 General advise when assembling strings is to not concatenate them
 repeatedly but instead use string's join() function, because it
 avoids repeated reallocations and is at least as expressive as any
 alternative.
 
 What I have now is a case where I'm assembling lines of text for
 driving a program with a commandline interface. In this scenario,
 I'm currently doing this:
 
   args = ['foo', 'bar', 'baz']
   line = ' '.join(args) + '\n'

Assuming it's the length of the list that's the problem, not the
length of the strings in the list...

args = ['foo', 'bar', 'baz']
args[-1] = args[-1] + '\n'
line = ' '.join(args)

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: better way for ' '.join(args) + '\n'?

2012-10-26 Thread Tycho Andersen
On Fri, Oct 26, 2012 at 05:36:50PM -0400, Dave Angel wrote:
 On 10/26/2012 05:26 PM, Tycho Andersen wrote:
  Assuming it's the length of the list that's the problem, not the
  length of the strings in the list...
 
  args = ['foo', 'bar', 'baz']
  args[-1] = args[-1] + '\n'
  line = ' '.join(args)
 
  \t
 
 Main problem with that is the trailing space before the newline.  If
 that's not a problem, then fine.

What trailing space before the newline? The other solutions have it,
the above does not. However, the above does mutate args, which isn't
all that great. Alas, if you want the performance of mutable
structures, you're probably going to have to mutate something. (In any
case, it's easy enough to change it back, though ugly.)

 Not sure why we try so hard to optimize something that's going to take
 negligible time.

The same reason some people enjoy sporting events: it's fun :-)

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Gotcha's?

2012-04-05 Thread Tycho Andersen
On Thu, Apr 05, 2012 at 08:32:10AM -0400, Roy Smith wrote:

 One of the hardest things about writing parsers is generating helpful 
 error messages when things don't parse.  But, it's only of value to do 
 that when you're parsing something you expect to be written by a human, 
 and thus a human has to puzzle out what they did wrong.  Nobody expects 
 that a JSON parser will be parsing human-written input, so there's 
 little value to saying anything more than parse error.

Except for the human that has to debug why something automatically
generated doesn't parse. That guy would probably appreciate a
reasonable error message. (And indeed, as a sibling poster points out,
people do write JSON by hand quite frequently.)

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: No os.copy()? Why not?

2012-04-03 Thread Tycho Andersen
On Tue, Apr 03, 2012 at 03:46:31PM -0400, D'Arcy Cain wrote:
 On 03/28/12 16:12, John Ladasky wrote:
 I'm looking for a Python (2.7) equivalent to the Unix cp command.
 Since the equivalents of rm and mkdir are in the os module, I
 figured I look there.  I haven't found anything in the documentation.
 I am also looking through the Python source code in os.py and its
 child, posixfile.py.
 
 cp is not a system command, it's a shell command.  Why not just use the
 incredibly simple and portable
 
   open(outfile, w).write(open(infile).read())

Note, though, that this reads the whole file into memory. As many
others have said, shutil is the most idiomatic option.

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Generating a pkg config file with distutils

2012-03-27 Thread Tycho Andersen
Hi all,

I'm distributing a package which for various legacy reasons needs to
generate a pkgconfig file from a template (adding version numbers,
prefixes, etc.) and install the file in the right place
($PREFIX/lib/pkgconfig/foo.pc in most cases).

Currently, I have a rather nasty hack to implement all this, but
presumably there's a better way to do it. If I could even get the
installation part (e.g. using the right MANIFEST.in incantations),
that would be wonderful. Reading the MANIFEST.in docs [1], it's not
obvious that you can control the install locations of these files
(i.e., .pc files must be installed to the above location to be
correctly detected by other packages).

Is what I want to do possible, or should I continue using my nasty
hack?

TIA!

Tycho

[1]: http://docs.python.org/distutils/sourcedist.html#commands
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to disconnect from ldap?

2012-03-22 Thread Tycho Andersen
On Wed, Mar 21, 2012 at 04:49:54PM -0500, Tim Chase wrote:
 On 03/21/12 15:54, Chris Kaynor wrote:
 As Chris Rebert pointed out, there is no guarantee as to when the
 __del__ method is called. CPython will generally call it immediately,
 however if there are reference cycles it may never call it
 
 And more maddeningly, modules/objects used/called from within the
 __del__ may have already gone out of scope, producing
 head-scratching errors.  I've been bitten by this enough times that
 I just stopped using __del__ completely.

I've had similar experiences. In fact, in light of all this - why does
__del__ exist at all? Novice python users may (reasonably) assume it
behaves similarly to a C++ destructor (even though the docs warn
otherwise).

Given that you can't trust __del__, is there a legitimate use case for
it?

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to disconnect from ldap?

2012-03-22 Thread Tycho Andersen
On Thu, Mar 22, 2012 at 06:27:45AM -0700, Chris Rebert wrote:
 On Thu, Mar 22, 2012 at 6:14 AM, Tycho Andersen ty...@tycho.ws wrote:
  On Wed, Mar 21, 2012 at 04:49:54PM -0500, Tim Chase wrote:
  On 03/21/12 15:54, Chris Kaynor wrote:
  As Chris Rebert pointed out, there is no guarantee as to when the
  __del__ method is called. CPython will generally call it immediately,
  however if there are reference cycles it may never call it
 
  And more maddeningly, modules/objects used/called from within the
  __del__ may have already gone out of scope, producing
  head-scratching errors.  I've been bitten by this enough times that
  I just stopped using __del__ completely.
 
  I've had similar experiences. In fact, in light of all this - why does
  __del__ exist at all? Novice python users may (reasonably) assume it
  behaves similarly to a C++ destructor (even though the docs warn
  otherwise).
 
  Given that you can't trust __del__, is there a legitimate use case for
  it?
 
 Writing resource classes (like `file`) in C? Their __del__()s
 typically involve little/less Python-level stuff and thus less
 paranoia need be exercised.

Sure, but you still have no guarantee that __del__ will ever be
called, so it's a bad idea to rely on it to clean up anything.

 There is somewhat of a perverse incentive in having such last-ditch
 clean-up mechanisms though. This code seems to work fine without
 `with`, so why bother changing it?

Yeah, I guess I can see doing something like:
  __del__ = __exit__
but anything beyond that seems risky...

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to disconnect from ldap?

2012-03-22 Thread Tycho Andersen
On Thu, Mar 22, 2012 at 05:26:11PM +, Steven D'Aprano wrote:
 On Thu, 22 Mar 2012 08:14:47 -0500, Tycho Andersen wrote:
 
  I've had similar experiences. In fact, in light of all this - why does
  __del__ exist at all? Novice python users may (reasonably) assume it
  behaves similarly to a C++ destructor (even though the docs warn
  otherwise).
 
 What makes you think that novice Python users will be familiar with C++ 
 destructors?

I don't, really. It's just natural to assume that __del__ is the
opposite of __init__, when it's really not (i.e. every object is
__init__ed, but not every object is destructed and thus __del__'d).
Novice programmers may make this assumption (indeed, many experienced
programmers do as well).

 Be careful about assuming that idioms in INSERT FAVOURITE LANGUAGE HERE 
 will be shared by all Python programmers, novice or expert.

Yeah, C++ was the first language which has destructors that came to
mind. It's certainly not my favorite ;-)

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating a .pc file using distutils

2012-02-12 Thread Tycho Andersen
Just re-bumping this - I am fiddling with this code again and it's
gross, so any input would be greatly appreciated :-)

\t

On Mon, Jan 23, 2012 at 05:31:20PM -0600, Tycho Andersen wrote:
 Is there some standard way to generate a .pc file (given a .pc.in or
 similar) using distutils?
 
 If there's not, is there a good way to access whatever the user passes
 in as --prefix (besides parsing sys.argv yourself)?
 
 Thanks,
 
 \t
-- 
http://mail.python.org/mailman/listinfo/python-list


[issue13051] Infinite recursion in curses.textpad.Textbox

2012-01-25 Thread Tycho Andersen

Tycho Andersen ty...@tycho.ws added the comment:

Hi, any movement on this?

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13051
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Generating a .pc file using distutils

2012-01-23 Thread Tycho Andersen
Is there some standard way to generate a .pc file (given a .pc.in or
similar) using distutils?

If there's not, is there a good way to access whatever the user passes
in as --prefix (besides parsing sys.argv yourself)?

Thanks,

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


[issue13051] Infinite recursion in curses.textpad.Textbox

2011-12-20 Thread Tycho Andersen

Tycho Andersen ty...@tycho.ws added the comment:

Attached is a patch which contains a testcase as well. A few notes about this 
testcase:

1. I couldn't figure out how to get it to run correctly after all the other 
tests had run, so I had to run it first. This seems lame. One possible fix is 
to run each testcase in curses.wrapper; I'd be happy to change this patch to do 
that if it's more acceptable.

2. This testcase only tests one of the two bugs this patch fixes. The other 
seems much harder to write a testcase for, since you have to have a terminal 
such that curses.LINES * curses.COLUMS  sys.getrecursionlimit(). If there's a 
good way to guarantee this, I'd be happy to write a testcase for it.

Comments are appreciated!

--
Added file: http://bugs.python.org/file24061/textpad-recursion-fix.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13051
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Getting a patch accepted

2011-12-19 Thread Tycho Andersen
Hi all,

A couple months ago I found a bug in a corner of the curses library
(http://bugs.python.org/issue13051) and filed it. Unfortunately, there
was nobody listed to cc on the noisy list, so it probably got lost in
the shuffle. (There is even previous mention of this bug elsewhere on
the tracker, and on stackoverflow, so it does affect some small number
of people.)

Is there anyone else I can bother to get this patch applied?

Thanks!

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unit-profiling, similar to unit-testing

2011-11-17 Thread Tycho Andersen
On Wed, Nov 16, 2011 at 09:36:40AM -0500, Roy Smith wrote:
 In article 95bcp8-bft@satorlaser.homedns.org,
  Ulrich Eckhardt ulrich.eckha...@dominolaser.com wrote:
 
  Hi!
  
  I'm currently trying to establish a few tests here that evaluate certain 
  performance characteristics of our systems. As part of this, I found 
  that these tests are rather similar to unit-tests, only that they are 
  much more fuzzy and obviously dependent on the systems involved, CPU 
  load, network load, day of the week (Tuesday is virus scan day) etc.
  
  What I'd just like to ask is how you do such things. Are there tools 
  available that help? I was considering using the unit testing framework, 
  but the problem with that is that the results are too hard to interpret 
  programmatically and too easy to misinterpret manually. Any suggestions?
 
 It's really, really, really hard to either control for, or accurately 
 measure, things like CPU or network load.  There's so much stuff you 
 can't even begin to see.  The state of your main memory cache.  Disk 
 fragmentation.  What I/O is happening directly out of kernel buffers vs 
 having to do a physical disk read.  How slow your DNS server is today.

While I agree there's a lot of things you can't control for, you can
get a more accurate picture by using CPU time instead of wall time
(e.g. the clock() system call). If what you care about is mostly CPU
time, you can control for the your disk is fragmented, your DNS
server died, or my cow-orker was banging on the test machine this
way.

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


[issue13051] Infinite recursion in curses.textpad.Textbox

2011-09-27 Thread Tycho Andersen

New submission from Tycho Andersen ty...@tycho.ws:

The attached patch fixes two bugs which manifest as infinite recursion in 
_insert_printable_char() of Textbox. First, the previous implementation of 
_insert_printable_char() used recursion to move characters when inserting a 
character. Thus, any Textpad which had an area greater than the interpreter's 
maximum recursion limit would crash. A minimal test case is the following:

#!/usr/bin/python
import curses
from curses.textpad import Textbox

def main(stdscr):
box = Textbox(stdscr, insert_mode=True)
box.stripspaces = True
while 1:
cmd = box.edit()
if cmd == 'q':
break

curses.wrapper(main)

Run that script in a terminal with area (i.e. $LINES * $COLUMNS)  1000 (the 
default max recursion limit), press any key and be greeted by a stack trace. 
The patch changes the implementation of _insert_printable_char() to be 
iterative, thus avoiding the infinite recursion.

Second, when the underlying curses window was resized to be smaller than it was 
when the Textpad was created, pressing any key would result in infinite 
recursion (or with the new method, an infinite loop). The patch also changes 
Textpad so that instead of keeping the underlying window's size as instance 
attributes of this Textpad, Textpad asks the underlying window its size every 
time Textpad needs to know, allowing the underlying window to be resized at 
will.

I've verified this bug is in 2.7.1 and 3.2. Let me know if you need anything 
else.

--
components: Library (Lib)
files: textpad_resize.patch
keywords: patch
messages: 144559
nosy: tycho
priority: normal
severity: normal
status: open
title: Infinite recursion in curses.textpad.Textbox
type: crash
versions: Python 2.7, Python 3.2
Added file: http://bugs.python.org/file23249/textpad_resize.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13051
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Interpreting Left to right?

2011-06-24 Thread Tycho Andersen
On Fri, Jun 24, 2011 at 12:14:27AM -0700, Ethan Furman wrote:
 
 The example given to me when I had this question:
 
 -- x = x['huh'] = {}
 -- x
 {'huh': {...}}
 
 
 As you can see, the creation of the dictionary is evaluated, and
 bound to the name 'x'; then the key 'huh' is set to the same
 dictionary.

Can you please elaborate? I really don't understand how this works at
all. I would have expected a NameError from this (obviously my mental
model is wrong).

This single line is equivalent to:

x = {}
x['huh'] = x

...but I don't understand how python's evaluation semantics get from
the one liner to the two liner/result at all.

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interpreting Left to right?

2011-06-24 Thread Tycho Andersen
On Fri, Jun 24, 2011 at 01:13:08PM -0700, Ethan Furman wrote:
 Tycho Andersen wrote:
 On Fri, Jun 24, 2011 at 12:14:27AM -0700, Ethan Furman wrote:
 The example given to me when I had this question:
 
 -- x = x['huh'] = {}
 -- x
 {'huh': {...}}
 
 
 As you can see, the creation of the dictionary is evaluated, and
 bound to the name 'x'; then the key 'huh' is set to the same
 dictionary.
 
 Can you please elaborate? I really don't understand how this works at
 all. I would have expected a NameError from this (obviously my mental
 model is wrong).
 
 This single line is equivalent to:
 
 x = {}
 x['huh'] = x
 
 ...but I don't understand how python's evaluation semantics get from
 the one liner to the two liner/result at all.
 
 \t
 
 Think of it this way:
 
 x = x['huh'] = {}
 
 obj = {}   # RHS evaluated first (and only once)
 
 x = obj# then first LHS
 
 x['huh'] = obj # then second LHS, etc

Yes, I understand that, but I guess I don't understand *why* things
are done that way. What is the evaluation order principle at work
here? I would have expected:

tmp = {}
x['huh'] = tmp # NameEror!

That is, the right hand sides of assignments are evaluated before the
left hand sides. That is (somehow?) not the case here.

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interpreting Left to right?

2011-06-24 Thread Tycho Andersen
On Fri, Jun 24, 2011 at 01:24:24PM -0700, Ned Deily wrote:
 In article 20110624200618.gk6...@point.cs.wisc.edu,
  Tycho Andersen ty...@tycho.ws wrote:
  Yes, I understand that, but I guess I don't understand *why* things
  are done that way. What is the evaluation order principle at work
  here? I would have expected:
  
  tmp = {}
  x['huh'] = tmp # NameEror!
  
  That is, the right hand sides of assignments are evaluated before the
  left hand sides. That is (somehow?) not the case here.
 
 http://docs.python.org/py3k/reference/simple_stmts.html#assignment-statements

Perhaps I'm thick, but (the first thing I did was read the docs and) I
still don't get it. From the docs:

An assignment statement evaluates the expression list (remember that
this can be a single expression or a comma-separated list, the latter
yielding a tuple) and assigns the single resulting object to each of
the target lists, from left to right.

For a single target, it evaluates the RHS and assigns the result to
the LHS. Thus

x = x['foo'] = {}

first evaluates

x['foo'] = {}

which should raise a NameError, since x doesn't exist yet. Where am I
going wrong?

Thanks,

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interpreting Left to right?

2011-06-24 Thread Tycho Andersen
On Fri, Jun 24, 2011 at 05:02:00PM -0400, Terry Reedy wrote:
 On 6/24/2011 4:06 PM, Tycho Andersen wrote:
 
 tmp = {}
 x['huh'] = tmp # NameEror!
 
 That is, the right hand sides of assignments are evaluated before the
 left hand sides. That is (somehow?) not the case here.
 
 You are parsing a = b = c as a = (b = c) which works in a
 language in which assignment is an expression, but does not work in
 Python where assignment is a statement. You have to parse it more as
 (a = b) = c but that does not work since then the first '=' is not
 what it seems. It is more like (both a and b) = c. Perhaps best to
 expand a = b = c to a = c; b = c and see the first as an
 abbreviation thereof -- just delete the 'c;'.
 
 If I have ever used this sort of multiple assignment, it has been
 for simple unambiguous things like a = b = 0.

Ah, the point about the grammar is what I was missing. Thanks a bunch!

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: split long string in two code lines

2011-06-13 Thread Tycho Andersen
On Mon, Jun 13, 2011 at 11:31:29PM +0200, Tracubik wrote:
 Hi all,
 
 newbie question here
 
 how can i write code like this:
 
 1 def foo():
 2for index in ...
 3for plsdoit in ...
 4print this is a very long string that i'm going to
 write 5 here, it'll be for sure longer than 80 columns
 
 
 the only way i've found is to use the /, but than i've to write
 something like this:

Perhaps you mean '\'?

 
 1 def foo():
 2for index in ...
 3for plsdoit in ...
 4print this is a very long string that i'm going to/
 5  write here, it'll be for sure longer than 80 columns
 
 what i don't really like is that line 5 is not indented. if i indent
 it, the spaces will be counted as spaces of the string.
 
 Is there a better way to split the string?

There is! Python (as C) concatenates string literals with nothing in
between them.

Python 2.6.2 (r262:71600, Jun  8 2009, 11:11:42) 
[GCC 4.3.2] on linux2
Type help, copyright, credits or license for more information.
 def foo():
... print this is not such a huge line  \
...   but it's still pretty long
... 
 foo()
this is not such a huge line but it's still pretty long

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Proper way to handle errors in a module

2011-05-12 Thread Tycho Andersen
On Thu, May 12, 2011 at 03:12:39PM -0500, Andrew Berg wrote:
 On 2011.05.12 02:25 PM, MRAB wrote:
  You can raise an exception wherever you like! :-)
 If I raise an exception that isn't a built-in exception, I get something
 like NameError: name 'HelloError' is not defined. I don't know how to
 define the exception.

You'll have to define it, as you would anything else (exceptions are
just regular things; in fact you can raise anything that's a class
or instance). I typically don't put a whole lot in my exception
classes, though.

point:~/working$ python
Python 2.6.2 (r262:71600, Jun  8 2009, 11:11:42) 
[GCC 4.3.2] on linux2
Type help, copyright, credits or license for more information.
 class HelloError(Exception): pass
... 
 raise HelloError(hello!)
Traceback (most recent call last):
  File stdin, line 1, in module
  __main__.HelloError: hello!

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


re documentation bug?

2011-03-07 Thread Tycho Andersen
Consider the following session:

Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type help, copyright, credits or license for more information.
 import re
 p = re.compile(foo)
 re.sub(p, bar, foobaz, flags=re.IGNORECASE)
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: sub() got an unexpected keyword argument 'flags'

The flags should really be passed to re.compile() instead. However,
the documentation indicates that they can be passed to re.sub() as
well. Is this a bug, or am I reading things wrong?

http://docs.python.org/library/re.html#re.sub

TIA!

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Importing libs on Windows?

2010-08-12 Thread Tycho Andersen
On Thu, Aug 12, 2010 at 04:09:10PM -0700, Brian Salter wrote:
 I've seen a number of tutorials that describe how to bring in a dll
 in python, but does anybody know of a tutorial for how to bring in a
 lib?  Is it even possible?

I don't know if it's possible, but why do you want to do it? .lib 
files in Windows are static libraries, which traditionally aren't 
loaded dynamically. Is there some reason you can't use a DLL?

\t

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: introducing Lettuce, BDD tool for python with Django integration

2010-06-08 Thread Tycho Andersen
On Tue, Jun 8, 2010 at 9:18 PM, alex23 wuwe...@gmail.com wrote:
 On Jun 9, 3:29 am, Terry Reedy tjre...@udel.edu wrote:
 On 6/8/2010 2:26 AM, Gabriel Falcão wrote:
  There is not much to say,
 except to explain 'BDD'.

 If only there was some kind of way to quickly look up the meaning of
 definitions, preferably one known to people of the programming
 persuasion...

I think his point may have been that there could be more than one
meaning. My first guess would have been binary decision diagram.

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Extract all words that begin with x

2010-05-10 Thread Tycho Andersen
On Mon, May 10, 2010 at 10:23 PM, Terry Reedy tjre...@udel.edu wrote:
 On 5/10/2010 5:35 AM, James Mills wrote:

 On Mon, May 10, 2010 at 6:50 PM, Xavier Hocont...@xavierho.com  wrote:

 Have I missed something, or wouldn't this work just as well:

 list_of_strings = ['2', 'awes', '3465sdg', 'dbsdf', 'asdgas']
 [word for word in list_of_strings if word[0] == 'a']

 ['awes', 'asdgas']

 I would do this for completeness (just in case):

 [word for word in list_of_strings if word and word[0] == 'a']

 Just guards against empty strings which may or may not be in the list.

  ... word[0:1] does the same thing. All Python programmers should learn to
 use slicing to extract a  char from a string that might be empty.
 The method call of .startswith() will be slower, I am sure.

Why? Isn't slicing just sugar for a method call?

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Kindly show me a better way to do it

2010-05-08 Thread Tycho Andersen
On Sat, May 8, 2010 at 3:41 PM, Oltmans rolf.oltm...@gmail.com wrote:
 Hi, I've a list that looks like following

 a = [ [1,2,3,4], [5,6,7,8] ]

 Currently, I'm iterating through it like

 for i in [k for k in a]:
        for a in i:
                print a

 but I was wondering if there is a shorter, more elegant way to do it?

How about itertools? In python 2.6:

 a = [ [1,2,3,4], [5,6,7,8] ]
 from itertools import chain
 for i in chain(*a):
... print i
...
1
2
3
4
5
6
7
8
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Kindly show me a better way to do it

2010-05-08 Thread Tycho Andersen
On Sat, May 8, 2010 at 4:09 PM, Günther Dietrich
gd.use...@spamfence.net wrote:
[snip]
 Too simple?

No, not at all. I really only intended to point the OP to itertools,
because it does lots of useful things exactly like this one.

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list vs tuple for a dict key- why aren't both hashable?

2009-11-08 Thread Tycho Andersen

On Sun, 8 Nov 2009, Wells wrote:


I'm not quite understanding why a tuple is hashable but a list is not.
Any pointers? Thanks!


The keys of a dict have to be immutable. Lists are mutable, tuples are
not.

\t
--
http://mail.python.org/mailman/listinfo/python-list


Re: Less APIs or more encapsulation?

2009-09-09 Thread Tycho Andersen
On Wed, Sep 9, 2009 at 10:08 AM, 一首诗newpt...@gmail.com wrote:
 But when C has many many methods to expose to outer user, 2nd choice
 seems to be more reasonable I In the first design, B.newMethod did
 nothing really useful.

Is there any reason you can't do something like the following?

class B(object):
  def __init__(self, c):
self.__c = c;
  def __getattr__(self, name):
return self.__c.__getattribute__(name)

class C(object):
  def sayHi(self):
print c says hi

So that when you call B and it doesn't have the attribute, it looks at
it's instance of C?

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting on '^' ?

2009-08-14 Thread Tycho Andersen
On Fri, Aug 14, 2009 at 3:23 PM, kjno.em...@please.post wrote:
 [snip]
 import re
 re.split('^', 'spam\nham\neggs\n')
 ['spam\nham\neggs\n']
 re.split('(?m)^', 'spam\nham\neggs\n')
 ['spam\nham\neggs\n']
 bol_re = re.compile('^', re.M)
 bol_re.split('spam\nham\neggs\n')
 ['spam\nham\neggs\n']

 Am I doing something wrong?

Why not just:

 re.split(r'\n', 'spam\nham\neggs')

\t
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to fetch an XML file using an HTTPS query

2009-08-04 Thread Tycho Andersen
Blah, forgot to include the list. When is python-list going to get Reply-To?

\t

On Tue, Aug 4, 2009 at 8:38 AM, Tycho Andersenty...@tycho.ws wrote:
 Hi Ido,

 On Tue, Aug 4, 2009 at 6:25 AM, Ido Levyi...@il.ibm.com wrote:
 [snip]
 I got the following result in both cases:
         ?xml version=1.0?
         Devices
         ErrorInvalid filter passed./Error
         /Devices

 To me, this doesn't look like a python problem: the application was
 successfully connected to and returned a result. The problem you're
 experiencing is that it wasn't the result you expected. Are you sure
 you're setting the right headers, posting the right form values, etc.
 and giving the app exactly what it wants?

 \t

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help understanding the decisions *behind* python?

2009-07-20 Thread Tycho Andersen
On Mon, Jul 20, 2009 at 11:27 AM, Phillip B
Oldhamphillip.old...@gmail.com wrote:
 snip
 We often find we need to do manipulations like the above without
 changing the order of the original list, and languages like JS allow
 this. We can't work out how to do this in python though, other than
 duplicating the list, sorting, reversing, then discarding.


I have no idea about why the design decisions were made. You might
take a look at the sorted() function:
http://docs.python.org/library/functions.html#sorted

It will do what you want.

\t
--
http://tycho.ws
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to receive a data file of unknown length using a python socket?

2009-07-18 Thread Tycho Andersen
On Sat, Jul 18, 2009 at 4:43 PM, Irmen de Jongirmen.nos...@xs4all.nl wrote:
 twgray wrote:

 I am attempting to send a jpeg image file created on an embedded
 device over a wifi socket to a Python client running on a Linux pc
 (Ubuntu).  All works well, except I don't know, on the pc client side,
 what the file size is?

 You don't. Sockets are just endless streams of bytes. You will have to
 design some form of 'wire protocol' that includes the length of the message
 that is to be read.
 For instance a minimalistic protocol could be the following:
 Send 4 bytes that contain the length (an int) then the data itself. The
 client reads 4 bytes, decodes it into the integer that tells it the length,
 and then reads the correct amount of bytes from the socket.

Exactly, sending the length first is the only way to know ahead of
time. Alternatively, if you know what the end of the data looks like,
you can look for that 'flag' as well, and stop trying to recv() after
that.

Some things that may be useful, though, are socket.settimeout() and
socket.setblocking(). More information is availible in the docs:
http://docs.python.org/library/socket.html.

You need to be careful with this, though, since network latency may
cause problems. Using these methods will keep your program from
sitting in recv() forever, though.

\t
--
http://tycho.ws
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import module unbelieveable behaviour

2009-07-15 Thread Tycho Andersen
On Wed, Jul 15, 2009 at 8:12 AM, Peter Fodrekpeter.fod...@stuba.sk wrote:

 Would anyone be helpful for me to get more information about this problem
 because  pydb does not show anything usable for me,please?

What is the directory structure for the HeeksCNC module? Although I'm
no expert, I suspect it looks something like:

nc/
nc/rez.py
nc/foo.py

In order for python to look in the nc/ directory for modules, there
needs to be a file called __init__.py (even if it's empty). I suspect
the nc/ directory is missing this file (although, I'm confused as to
why it would work in the interpreter then).

Tycho
-- 
http://mail.python.org/mailman/listinfo/python-list