Re: Unix Device File Emulation

2008-04-23 Thread Martin Blume
"blaine" schrieb
> >
> > while 1:
> >  r = self.fifodev.readline()
> >  if r: print r
> >
> > According to my docs, readline() returns an empty 
> > string at the end of the file.
> > Also, you might want to sleep() between reads a 
> > little bit.
> >
> 
> Oh ok, that makes sense.  Hmm.  So do I not want to use 
> readline()? Or is there a way to do something like 
> 'block until the file is not empty'?
>

No, 
  while 1:
  r = self.fifodev.readline()
  if r: print r
  else: time.sleep(0.1)
is ok (note the "if r:" clause).

Martin



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


Re: Unix Device File Emulation

2008-04-23 Thread Martin Blume
"blaine" schrieb
> 
> # Fake Nokia Screen Emulator
> import sys, os
> 
> class nokia_fkscrn:
>   def __init__(self, file):
> if not os.path.exists(file):
>   os.mkfifo(file)
> self.fifodev = open(file, 'r')
>   def read(self):
> while 1:
>   r = self.fifodev.readline()
>   print r
> 
> nokia = nokia_fkscrn('dev.file')
> nokia.read()
> 
> This works at first, but when I write to the 'dev.file' 
> for the first time, the text is displayed as intended, 
> but then the program just keeps spitting out blank lines.  
> I can continue to write to the file 
> (using echo 'test\n' > dev.file) 
> and this shows up in my output, but amist a giant mass 
> of scrolling blank lines.  This also causes my CPU
> usage to shoot up to 100%.
> 
> Any ideas? This is OS X 10.4
>

while 1:
 r = self.fifodev.readline()
 if r: print r

According to my docs, readline() returns an empty string
at the end of the file. 
Also, you might want to sleep() between reads a little bit.


IMHO. HTH.
Martin



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


Re: Is subprocess.Popen completely broken?

2008-03-27 Thread Martin Blume
"Istvan Albert" schrieb 
> 
> > Is subprocess.Popen completely broken?
> 
> Your lack of faith in Python is somewhat 
> disturbing ...
> 
I have consistently made the experience that when 
I was about to ask "is X completely broken", the
error was on my side.

Martin


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


Re: Strange problem with structs Linux vs. Mac

2008-03-18 Thread Martin Blume
"Marc 'BlackJack' Rintsch" schrieb 
> 
> > I don't think this qualifies as a bug, but I am astonished
> > that the struct module does not tell you whether you are
> > big endian, you have to find out yourself with
> >struct.unpack('@I', s)[0]==struct.unpack(">I", s)[0]
> 
> Maybe a little more compact and readable:
> 
> In [92]: sys.byteorder
> Out[92]: 'little'
> 
Yes, indeed it is more compact and readable.
Thanks.
Martin


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


Re: Strange problem with structs Linux vs. Mac

2008-03-16 Thread Martin Blume
"sturlamolden" schrieb
> 
> > This seems to imply that the Mac, although running now 
> > on Intel processors, is still big-endian.
> 
> Or maybe the struct module thinks big-endian is native 
> to all Macs? It could be a bug.
> 
Dunno, I'm on thin ice here. Never used a Mac. 
Maybe the underlying C library thinks that all Macs are 
big-endian?
I don't think this qualifies as a bug, but I am astonished
that the struct module does not tell you whether you are
big endian, you have to find out yourself with
   struct.unpack('@I', s)[0]==struct.unpack(">I", s)[0]

Anyway, when handling binary data across machines, I think
it is proper to explicitly specify the endian-ness and to 
do sanity-checking of the results.

Regards
Martin

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


Re: Strange problem with structs Linux vs. Mac

2008-03-16 Thread Martin Blume
"jasonwiener" schrieb
> 
> I am having a VERY odd problem with unpacking right now. 
> I'm reading data from a binary file and then using a very 
> simple struct.unpack to get a long.  Works fine on my MacBook,
> but when I push it to a Linux box,it acts differently and 
> ends up pewking.
> [...]
> 
> the data looks to be the same, but the unpacking seems to 
> treat it differently.
> 
Probably little-endian vs. big-endian issue:

>>> s
'\x1e\xc6\xf3\xb4'
>>> struct.unpack('>> struct.unpack('>I', s)
(516354996L,)

See help(struct) for further information.

This seems to imply that the Mac, although running now on Intel
processors, is still big-endian.

HTH
Martin

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


Re: Problem with the strip string method

2008-03-02 Thread Martin Blume
"Colin J. Williams" schrieb 
> The Library Reference has
> strip( [chars])
> 
> Return a copy of the string with the 
> leading and trailing characters removed. 
  

It's "leading and trailing", not 
"leading, trailing or embedded".

>>> "xxxaaaxxx".strip("x")
'aaa'
>>> "xxxaaaxxxaaaxxx".strip("x")
'aaaxxxaaa'
>>>

HTH 
Martin



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


Re: Any experience with Python on a PDA ?

2008-02-22 Thread Martin Blume
"Stef Mientki" schrieb
> hello,
> 
> I wonder if anyone has (good ;-) experiences 
> with Python on a PDA ?
> And if so,
> - what OS
> - what GUI
> 
Python runs here on:
- Sharp Zaurus SL-C3100 (with the exception of Tkinter)
(Linux 2.4)
- Mio A701
(Windows Mobile)

But I only do small stuff on these machines, like my
own hp-like (rpn) calculator or other computations
directly in the python shell.

Experience is very good (with the exception of Tkinter
not running on the Zaurus), scripts can be moved to and
from PC and PDA and run unchanged (except for speed and
screen size issues).

HTH
Martin


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


Re: how to finish a while loop...

2008-02-20 Thread Martin Blume
"richie" schrieb
> > > That code works. Maybe you fixed it while 
> > > you were mailing it =)
> >
> > This is weird mate.
> > I'm using eclipse 3.2 with the pydev plugin.  
> > There it loops forever - from the eclipse console.
> > Two hours of trying, changing the code...finally gave up.
> >
> > Then I got your reply.  Opened up a regular console 
> > and executed it from there.
> > And voilait works! Well, after this I'm going 
> > back to the old trusty shell.
> >
> > Thanks again mate.
> 
> I try it too in my eclipse3.2. I got the same result.
> It seems very strange.
>
Print out "answer" and see if there is a difference ...

my $0.02
Martin


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


Re: Bug in __init__?

2008-01-18 Thread Martin Blume
"Zbigniew Braniecki"  schrieb
> I found a bug in my code today, and spent an hour trying to locate
it
> and then minimize the testcase.
>   [...]
>def __init__ (self, val=[]):
>  [...]
> Any clue on what's going on here, and/if where I should report it?
>

I think this has to do with
http://docs.python.org/tut/node6.html#SECTION00671
especially the "Important Warning"

Regards
Martin



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


Re: Filtering two files with uncommon column

2008-01-18 Thread Martin Blume
"Madhur" schrieb
> I would like to know the best way of generating filter 
> of two files based upon the following condition
> [...]
>
Sounds like homework. Here some suggestions:

- for each file, create a dictionary (see help(dict)
in the python shell for details) and populate it with 
the values, so that e.g.
d1['def'] = 'abc def hij'
(help("".split), perhaps help("".strip))

- for each key in the first dictionary, look whether
it exists in the second, if not, write the value (the 
line extracted in the first step) out.
(help(dict.iteritems), help(dict.has_key))
(Note that for 
if a_dict.has_key("def"): pass
one can also write 
if "def" in a_dict: pass
but you won't find this in the simple on-line help,
at least in my version)

HTH
Martin

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


Re: Compiler or stg. to get exe!

2007-12-28 Thread Martin Blume
"SMALLp" schrieb 
> I have question. After short goggling, I haven't found 
> anything good.  So my question is:
> I wrote a program in python and i Get .py files and some 
> .pyc in working folder. Now i want program tu run under 
> windows, so i need to get exe files or something.
>
If python is installed on the target machine (Windows or 
Linux or ...) you do not need an exe, just copy the py file.

> And what do i need to do to make program for linux. 
> (stg. like .deb package)
a .deb package is more than a "program".
You'll have to look at the .deb developer documentation.

Again, for simple programs, just copying the .py file
is sufficient, provided that Python (and all the modules 
your .py needs) is installed.

HTH
Martin


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


Re: making all letters Caps/Small Letters

2007-12-14 Thread Martin Blume
"Merrigan" schrieb im> 
> I'm sure I have done this before, but cannot remember how, 
> or find out how to do it quickly - but is there a 
> way/function/something in python to make all the letters 
> of a raw_input() string small/capital letters?
> 
"upper might help".upper()
"OR LOWER".lower()

HTH
Martin


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


Re: pprinting objects

2007-12-08 Thread Martin Blume
"Donn Ingle" schrieb
>  Is there a way to get a dump of the insides of an object?
> I thought pprint would do it.
>
print would actually like to do it if you told it how to do it.
print actually does it, but takes a default implementation if
you do not override __repr__ or __str__.

> If I had a class like this:
>
> class t:
>  def __init__(self):
>   self.x=1
>   self.y=2
>   self.obj = SomeOtherObj()
>
> Then it could display it as:
>
>  t,
>  x,1,
>  y,2,
>  obj,
>
> Or something like that -- a complete output of the object really,
> with id()'s and so forth.
>
Define a __repr__ or __str__ method for the class:

class t:
 def __init__(self):
  self.x=1
  self.y=2
  self.obj = SomeOtherObj()

 def __repr__(self):
  s = " %s\n  x,%d\n  y,%d\n  obj," \
   % (self.__class__, self.x, self.y, self.obj.__class__)
  return s

a_t = t()
print "a t obj: %s" % (a_t)


a t obj:  __main__.t
  x,1
  y,2
  obj,


HTH
Martin


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


Re: reading raw variables from file

2007-12-02 Thread Martin Blume
"MonkeeSage" schrieb 
> >
> > If I have understood python naming scoping correctly,
> > doing
> >my_var="hello"
> >import stuff
> >print my_var
> > is not the same as
> >my_var="hello"
> >exec open("stuff.py").read()
> >print my_var
> > with stuff.py containing
> >my_var="bye"
> 
> It's not the same...
> 
> from stuff import *
> 
> ...is.
> 
And indeed it is. Thanks.
Martin

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


Re: reading raw variables from file

2007-11-30 Thread Martin Blume
"Bruno Desthuilliers"  schrieb 
> >> I have a file that might contain literal python 
> >> variable statements at every line. For example 
> >> the file info.dat looks like this:
> >> users = ["Bob", "Jane"]
> >> status = {1:"ok",2:users[0]}
> >> the problem is I want to read this file and load 
> >> whatever variables written in it as normal python 
> >> variable statements so that when i read the file, 
> >> my users var will be ["Bob","Jane"] and my status 
> >> var will be {1:"ok",2:users[0]} . 
> >> Is there an easy way of doing this instead of 
> >> parsing the files and checking said types?
> >>
> > 
> > You might want to look at the eval, exec and execfile;
> 
> Or just import...
> 
> > but bear in in mind Paddy's warning about security.
> 
> +10
> 

If I have understood python naming scoping correctly, 
doing
   my_var="hello"
   import stuff
   print my_var
is not the same as
   my_var="hello"
   exec open("stuff.py").read()
   print my_var
with stuff.py containing
   my_var="bye"

I use this exec open("stuff.py").read() mechanism to set
values in my scripts: the script sets a useful default, 
a command-line argument in the form a valid python program
may override it. Why bother with inventing or using another
mechanism when this is perfectly simple, easy and self-explaining?

The one and only thing against it is that a malicious user 
can sneak in an os.system("cd / && rm -rf *"). 

IMHO. YMMV.
Martin








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


Re: reading raw variables from file

2007-11-30 Thread Martin Blume
"Astan Chee" schrieb
> I have a file that might contain literal python 
> variable statements at every line. For example 
> the file info.dat looks like this:
> users = ["Bob", "Jane"]
> status = {1:"ok",2:users[0]}
> the problem is I want to read this file and load 
> whatever variables written in it as normal python 
> variable statements so that when i read the file, 
> my users var will be ["Bob","Jane"] and my status 
> var will be {1:"ok",2:users[0]} . 
> Is there an easy way of doing this instead of 
> parsing the files and checking said types?
>

You might want to look at the eval, exec and execfile;
but bear in in mind Paddy's warning about security.

Regards
Martin


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


Re: where is help file?

2007-09-14 Thread Martin Blume
"Carsten Haese" schrieb
> > new to Fedora7, typed python in interactive interpreter, then
help().
> > Then modules to get a list of modules.  Then module name to get
info
> > on a module but no help file.  What is the help file name?
> > Is there an environmental variable I have to set?
>
> There is no help file, and no environment variable. The help text
is
> stored directly inside each module and function that supplies a
help
> text. Observe:
>
> >>> def f(x):
> ...   "Calculate the square of x."
> ...   return x**x
> ...
> >>> help(f)
> Help on function f in module __main__:
>
> f(x)
> Calculate the square of x.
> (END)
>
> Providing such a help text is optional, but all standard library
modules
> do provide help as far as I know. Consequently, the fact that you
have a
> module that doesn't provide a help text seems to indicate that
it's not
> a standard library module. What's the name of the module you're
trying
> to get help on?
>
> Hope this helps (no pun intended),
>
AFAIK you have to import the module first, before you can get help
on that module.

HTH
Martin

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


Re: strftime in python 2.2

2007-08-13 Thread Martin Blume
"Flyzone"schrieb
> I'm trying to make work this code in python 2.2.3:
> 
> check=datetime.datetime.today().strftime("%H%M")
> 
> but datetime is not supported in that version but 
> just in the later. I can't upgrade python, too many 
> dependencies in a critical system.
> How can i convert that string to have the same result?
> 

import time
time.strftime("%H%M)

HTH
Martin


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


Re: Need a Little Help on Tkinter and Python

2007-06-06 Thread Martin Blume
"W. Watson" schrieb
> I have about a 1600 line Pythron program I'd like to 
> make some simple mods to, but have really just a nodding 
> acquaintance with Python and Tkinter. 
> [...]
> Let's change that. 
>

The book "Learning Python" from O'Reilly is excellent. 

If you are into scientific programming, the book
"Python Scripting for Computational Science"
by Hans Petter Langtangen from Springer is useful.

HTH
Martin



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


Re: Execute commands from file

2007-05-19 Thread Martin Blume
"Steve Holden" schrieb 
> 
> I simply meant that the whole source has to be presented 
> to the exec statement and not chunked into lines.
>
That's what I meant: With exec open(f).read() it is not 
broken into several exec invocations.

> 
> I was probably just a little over-zealous in pursuing 
> correct English usage, in which case please accept 
> my apology.
> 
The apology is on my part, I didn't explain my thinking
clearly enough.


Thanks for your explanations. Makes my newbie understanding 
of Python much more robust.

Regards
Martin


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


Re: Execute commands from file

2007-05-19 Thread Martin Blume
"Steve Holden" schrieb
> >
> > [ difference between exec open(fname).read() 
> >and for line in open(fname): exec line ] 
> > 
> > So it seems to depend on the way the file is read.
> > 
> It depends on the way the lines of the file are executed, 
> not how they are read. 
>
Could you elaborate a little bit more on the difference?
I assumed that because read() reads the whole file, the 
body of my function sowhat() is present, so that it can
be parsed while the invocation of exec is still running.
If it is read and exec'd line by line, the definition of
the function is still left open at the moment exec() ends,
causing the "EOF" error. Hence my statement, "it depends
on the way the file is read".


> And you may remember the original poster was 
> proposing this:
> 
> inp = open(cmd_file)
> for line in inp:
>   exec line
> 
> As for your first example, why not just use execfile() ?
> 
I assume that 
   execfile(fname)
is equivalent to
   exec open(fname).read() ?


Regards
Martin
   

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


Re: Execute commands from file

2007-05-17 Thread Martin Blume
"Steve Holden" schrieb 
> >>
> >> Try it on a file that reads something like
> >>
> >> xxx = 42
> >> print xxx
> >>
> >> and you will see NameError raised because the assignment 
> >> hasn't affected the environment for the print statement.
> >>
> > [...]
> > 
> No, because there isn't one. Now try adding a function 
> definition and see how well it works.
> 
C:\temp>more question.py
xxx=42
print xxx
def sowhat():
print xxx

print xxx


C:\temp>c:\programme\python\python
Python 2.4 (#60, Nov 30 2004, 11:49:19) 
  [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" 
  for more information.
>>> exec open("question.py").read()
42
42
>>> sowhat()
42
>>> xxx
42


Seems to work great to me.

OTOH, this doesn't:
>>> inp=open("question.py")
>>> for l in inp:
...  exec l
...
42
Traceback (most recent call last):
  File "", line 2, in ?
  File "", line 1
def sowhat():
^
SyntaxError: unexpected EOF while parsing


So it seems to depend on the way the file is read.


Regards
Martin



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


Re: Execute commands from file

2007-05-16 Thread Martin Blume
"tmp123" schrieb > 
> We have very big files with python commands 
> (more or less, 50 commands each file).
> 
> It is possible to execute them command by command, 

inp = open(cmd_file)
for line in inp:
exec line

might help. You don't get quite the same feeling as
"like if the commands was typed one after the other 
in a interactive session", but perhaps this helps.

Warning: the code above is without any error checks.
You might also run into security problems, the example
above assumes you trust your input.

HTH. YMMV.
Martin




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


Re: Finding the insertion point in a list

2007-03-17 Thread Martin Blume
"7stud" schrieb 
> How about:
> 
> ---
> x = [0, 100, 200, 1000]
> y = -1
> inserted = False
> 
> for i in range(len(x)):
> if(y <= x[i]):
> x.insert(i, y)
> inserted = True
> break
> if(not inserted): x.append(y)
> 
> print x
> 
>

You can get rid of the sentinel "inserted" using the 
else clause of the for loop:

for i in range(len(x)):
if (y <= x[i]):
x.insert(i, y)
break
else: x.append(y)

Python is cool :-)

IMHO. HTH.
Martin


 

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


Re: How to call a function defined in another py file

2007-02-19 Thread Martin Blume
<[EMAIL PROTECTED]> schrieb
>>
>>> I have a function called 'test' defined in A.py.
>>> How can I call that function test in my another file B.py?
>>
>> In B.py:
>>   import A
>>   A.test()
>>
> 
> But Do I need to put A.py and B.py in the same directory?
No, but then you have to take certain precautions. (*)

> if not, where does python look for A.py ?
In the path defined by the (IIRC) PYTHONPATH (*)

> And do I need to compile A.py before I can import it to B.py?
No.

(*) you might want to read the fine documentation at
http://docs.python.org/tut/node8.html
which tells it much better than I do, and might give you 
some more ideas for googling.
I haven't had yet the necessity for cross-directory imports.

HTH
Martin



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


Re: How to call a function defined in another py file

2007-02-19 Thread Martin Blume
<[EMAIL PROTECTED]> schrieb 
> 
> I have a function called 'test' defined in A.py.
> How can I call that function test in my another file B.py?
> 
In B.py:

import A

A.test()


HTH
Martin


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


Re: Why does this code crash python?

2006-11-12 Thread Martin Blume
<[EMAIL PROTECTED]> schrieb 
> I am trying to make a program that will basically simulate 
> a chess clock in python. ...
> ... it crashes shortly after. 

Can't help you on why it crashes, but

> 
> class eventMonitor (Thread):
> def run ( self ):
> [snipped]
> if event.keycode == 32:
> if turn == 1: turn = 0
> if turn == 0: turn = 1
>

looks wrong to me. This is supposed to switch between the 
players (0 and 1), but the first if changes turn to 0, the
second changes it immediately back to 1.

I'd do:
  if turn == 1:  turn = 0
  else:  turn = 1

HTH
Martin


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


Re: Decorator

2006-05-12 Thread Martin Blume
"bruno at modulix" schrieb
>
> [lucid introduction into decorators]
>
Thanks for the help in understanding decorators.

Martin


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


Re: Decorator

2006-05-12 Thread Martin Blume
"bruno at modulix"  schrieb 
> > 
> > Well, if you're changing the original module,
> Who's talking about "changing the original module" ?
> 
Well, you have to apply @deco in the module where
func_to_decorated is placed.

>
> > Isn't the point of a decorator to change the 
> > behavior externally, at runtime, possibly changing
> > it in different ways at different places at 
> > different times?
> 
> You're confusing the python specific @decorator 
> syntax with the OO design pattern by the same name. 
> This syntax is purely syntactic sugar
> for a specific use case of higher order functions.
>
Yes, that explains my confusion.

> 
> > So why this @deco notation? 
> 
> To improve readability.
> 
> @decorator
> def my_one_hundred_locs_func():
>   ...
> 
> is much more readable than:
> def my_one_hundred_locs_func():
>   ...
>   # 100 LOCS later
> my_one_hundred_locs_func = decorator (my_one_hundred_locs_func)
>
That makes sense.

> 
> Note that all this should be clear for anyone having 
> read the doc...
> 

Errm, yes, you're so right.

Thanks for reading the documentation to me 
and clearing this up :-)

Martin


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


Re: Decorator

2006-05-12 Thread Martin Blume
"bruno at modulix" schrieb 
> 
> [snip]
>
> The use case for @decorator is for wrapping functions
> or method *in the module/class itself*. 
That was the question. What's the use of doing it 
like that in the module *itself* (I mean, you change
directly the original function)?

> It's not for module client code (but this of
> course doesn't prevent client code to dynamically 
> add other wrappers...)
> 
How do the clients it? The "oldfashioned"
deco(doSillyWalk)
way?

Martin




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


Re: Decorator

2006-05-12 Thread Martin Blume
"Sybren Stuvel" schrieb
> Martin Blume enlightened us with:
Don't know if I enlightened anybody ... :-)

> > Another question: Isn't decorating / wrapping
> >  usually done at runtime, so that the @deco 
> > notation is pretty useless (because you'd
> > have to change the original code)?
> 
> Please explain why that would make the @deco 
> notation pretty useless.
> 
Well, if you're changing the original module, you
might as well insert the needed functionality in
the original function, no?
Or rename the original function, write a function
having this original name and calling from it the
original functionality?

Isn't the point of a decorator to change the 
behavior externally, at runtime, possibly changing
it in different ways at different places at different
times?

So why this @deco notation? Can you apply it externally?
Meaning to 
import module
first, then
@deco(module.func)
somewhere later?


Martin



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


Re: Decorator

2006-05-12 Thread Martin Blume
"bruno at modulix" schrieb 
> 
> What Python 2.4 adds is only syntactic sugar for decorators. 
> You can do the same - somewhat more explicitely - in 2.3.
> 
> > What is the decorator useful for?
> 
> 
> The whole things looks like this:
> 
> def deco(func):
>   print "decorating %s" % func.__name__
>   def _wrapper(*args, **kw):
> print "%s called " % func.__name__
> res = func(*args, **kw)
> print "%s returned %s" % (func.__name__, str(res))
  return res
  ^^
Shouldn't here be a return res, so that wrapper
behaves like the original function?

>   return _wrapper
> 
> # python < 2.4
> def somefunc():
>   print "in somefunc"
>   return 42
> 
> somefunc = deco(somefunc)
> 

Thanks for the explanation.


Another question: Isn't decorating / wrapping usually
done at runtime, so that the @deco notation is pretty
useless (because you'd have to change the original 
code)?
What do I miss here?

Martin


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


Re: Is this a legal / acceptable statement ?

2006-05-05 Thread Martin Blume
"Philippe Martin" schrieb 
> Hi,
> 
> This code works, but is it "appropriate" ?
> 
> l_init = False
> 
> if True == l_init and 1234 = l_value:
>  print 'l_value is initialized'
> 
> I know I can do this with a try but ...
> 
I am a Python newbie, but I think working with
l_value = None 
would be the most pythonic way.

C:\>c:\programme\python\python
Python 2.4 (#60, Nov 30 2004, 11:49:19) 
   [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" 
for more information.
>>> v = None
>>> x = 2
>>> x
2
>>> v
>>> v_default=3
>>> y = x + (v or v_default)
>>> y
5
>>> v = 6
>>> y = x + (v or v_default)
>>> y
8
>>> v = None
>>> y = x + (v or v_default)
>>> y
5
>>>

Of course in a function you can use:
def travel_time(from, to, speed=60):
pass

and if travel_time is called 
travel_time(a,b,1000) the speed will be 1000
and if travel_time is called
travel_time(a,b) the speed will be 60

IMHO.
Martin





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


Re: Invoking Unix commands from a Python app

2005-12-16 Thread Martin Blume
"Rob Cowie" schrieb
> Excellent... just the thing I was looking for. Thanks.
> 
> Does anyone know of a unix app that could be used to
> monitor the duration of processes etc.? 
> 
man -k account showed me (among others):
acct (2) - switch process accounting on or off
acct (5) - execution accounting file

a short program to start accounting:
(warning: just hacked together)

smail:/home/mblume/wrk/tmp # cat acct.c
#include 
#include 
#include 
#include 


int main(int argc, char *argv[])
{
char*pf;
struct stat st_buf;

if (strcmp(argv[1], "NULL") == 0)
{
pf = NULL;
printf("turning accounting off\n");
} // turn accounting off
else if (stat(argv[1], &st_buf) == -1)
{
printf("stat %s failed, error %d=%s\n",
argv[1], errno, strerror(errno));
return 1;
} // stat failed
else
{
// TBD check for a regular file
pf = argv[1];
printf("acct for %s\n", pf);
} // file seems ok

if (acct(pf) == -1)
{
printf("acct failed %d=%s\n", 
errno, strerror(errno));
} // oops, acct failed

return 0;

} // main


HTH
Martin


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


Re: finding a number...

2005-10-21 Thread Martin Blume
"Enrique Palomo Jiménez"
>
> After ftp a file from mvs to windows, i find:
> is an offset, so up to 2GB, a commercial application
> drives crazy
> [...]
>
??? I didn't understand your question, but 2 GB is popular
limit for the maximal size of a file for some filesystems
(e.g. ext2, FAT [???]).

Maybe this helps, otherwise you'd have to reformulate
the question.

HTH. YMMV.
Martin



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


Re: eval() in python

2005-06-21 Thread Martin Blume
"Xah Lee" schrieb
>
> perhaps i'm tired, but why can't i run:
>
> t='m=3'
> print eval(t)
>
Perhaps you didn't read the documentation? :-)
Perhaps you didn't try hard enough?


C:\WINNT>c:\programme\python\python
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more
information.
>>> t='m=3'
>>> m
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'm' is not defined
>>> exec(t)
>>> m
3
>>>

HTH
Martin


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


Re: oddness in super()

2005-06-19 Thread Martin Blume
"Björn Lindström"  schrieb
>
> > A great analysis, but what's a "pogo stick" and where can I get
one?
>
> http://search.ebay.com/pogo-stick
>
Yes, that explains the "bouncing with the pogo stick"; I would have
poked around
with a stick.

ROTFL, thank you.

Martin


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

Re: oddness in super()

2005-06-19 Thread Martin Blume
"John Machin" schrieb
>
> [analysis of super() "oddness"]
>
A great analysis, but what's a "pogo stick" and where
can I get one?

Thanks
Martin


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


Re: continuous plotting with Tkinter

2005-02-04 Thread Martin Blume
"Russell E. Owen" schrieb
>
> >I have a number-crunching application that spits out
> >a lot of numbers. Now I'd like to pipe this into a
> >python app and plot them using Tkinter, such as:
> >$ number_cruncher | myplot.py
> >But with Tkinter once  I call Tkinter's mainloop() I
> >give up my control of the app and I can't continue to
> >read in data from stdin.  Or can I? If so, how?
>
> One way is to use a thread to read the numbers, then
> make them available to the main thread via a Queue
> object, which you poll for new values.
>
I already tinkered with threads, yet it didn't work.
I'll try again.

>
> Another option is to use a Tk-compatible file or socket
> of some kind which triggers a callback when data comes
> in. See
>
>htttp://www.astro.washington.edu/rowen/TkinterSummary.html#FileHand
lers>

> some ideas on this.
>
Thanks, I'll look.

Martin


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


continuous plotting with Tkinter

2005-02-02 Thread Martin Blume
I have a number-crunching application that spits out 
a lot of numbers. Now I'd like to pipe this into a python
app and plot them using Tkinter, such as:
$ number_cruncher | myplot.py
But with Tkinter once  I call Tkinter's mainloop() I
give up my control of the app and I can't continue to
read in data from stdin.  Or can I? If so, how?

Regards
Martin



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