Re: newbie ``print`` question

2012-09-02 Thread mblume
Am Sun, 02 Sep 2012 10:23:53 -0700 schrieb gwhite:

> I can't figure out how to stop the "add a space at the beginning"
> behavior of the print function.
> 
 print 1,;print 2,
> 1 2
> 
> See the space in between the 1 and the 2 at the output print to the
> command console?
> 
> The help for print is:
> 
> "A space is written before each object is (converted and) written,
> unless the output system believes it is positioned at the beginning of a
> line."
> 
> So it is apparently doing what it is supposed to do.
> 
> Is there a way to stop this?  Or is there a different function that will
> only print what you have in the formatted string?
> 
> For example, in MATLAB I only had to do:
> 
>>> fprintf('1');fprintf('2')
> 12
> 
>  fprintf works the same way if printing to a file.  It only puts in
> what you explicitly tell it to put in.

import sys

sys.stdout.write("1"); sys.stdout.write("2")


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


Re: Python presentations

2012-09-13 Thread mblume
Am Thu, 13 Sep 2012 17:00:19 +0100 schrieb andrea crotti:
>
> I have to give a couple of Python presentations in the next weeks, and
> I'm still thinking what is the best approach.
> 
My idea for an introductory presentation of python was to prepare some 
code snippets (all valid python), show them in the editor, explain them,
then run in a console. 

Beginners could then use these code snippets for their own experiments.

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


Re: docstrings for data fields

2012-05-03 Thread mblume
Am Thu, 03 May 2012 14:51:54 +0200 schrieb Ulrich Eckhardt:

> Hi!
> 
> My class Foo exports a constant, accessible as Foo.MAX_VALUE. Now, with
> functions I would simply add a docstring explaining the meaning of this,
> but how do I do that for a non-function member? Note also that ideally,
> this constant wouldn't show up inside instances of the class but only
> inside the class itself.
> 
> There are decorators for static functions or class functions, similarly
> there is one for instance properties but there isn't one for class
> properties. Would that be a useful addition?
> 
> Uli

Docstring for Foo?


>>> 
>>> class Foo:
... """ exports a FOO_MAX value """
... FOO_MAX = 42
... 
>>> 
>>> 
>>> 
>>> 
>>> help(Foo)
Help on class Foo in module __main__:

class Foo
 |  exports a FOO_MAX value
 |  
 |  Data and other attributes defined here:
 |  
 |  FOO_MAX = 42

>>> Foo.FOO_MAX
42
>>> 
>>> 
>>> 


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


Re: OT: Code Examples

2011-03-01 Thread mblume
Am Mon, 28 Feb 2011 08:03:01 -0800 schrieb Fred Marshall:

> I'm interested in developing Python-based programs, including an
> engineering app. ... re-writing from Fortran and C versions.  One of the
> objectives would to be make reasonable use of the available structure
> (objects, etc.).  So, I'd like to read a couple of good, simple
> scientific-oriented programs that do that kind of thing.
> 
> Looking for links, etc.
> 
> Fred

An execellent book in this domain is
"Python Scripting for Computational Science"
by Hans Petter Langtangen

Springer, ISBN 3-450-43508-5

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


Re: Generating sin/square waves sound

2011-12-30 Thread mblume
Am Fri, 30 Dec 2011 07:17:13 + schrieb Paulo da Silva:

> Hi,
> Sorry if this is a FAQ, but I have googled and didn't find any
> satisfatory answer.
> 
> Is there a simple way, preferably multiplataform (or linux), of
> generating sinusoidal/square waves sound in python?
> 
> Thanks for any answers/suggestions.

Have a look at the wave module, available under Windows and Linux,
which operates on .WAV files. The following snippet might get you going:

#!/usr/bin/python
import math, wave, struct

def signal(t, freq):
return math.sin(2.0*math.pi*freq*t)


wout  = wave.open("sample.wav", "wb")
nchan = 1
sampwidth = 2
framerate = 8000
nframes   = 7 * framerate
comptype  = "NONE"
compname  = "no compression"


wout.setparams((nchan, sampwidth, framerate, nframes, comptype, compname))

ts = 1.0 / framerate
t  = 0.0
n  = 0
data = []
vals = []
while n < nframes:
vals.append(signal(t, 517.0))
n = n + 1
t = t + ts

mx   = max((abs(x) for x in vals))
vals = [ x/mx for x in vals ]   
data = ""
for v in vals:
data = data + struct.pack("http://mail.python.org/mailman/listinfo/python-list


Re: a couple of questions: pickling objects and strict types

2013-04-06 Thread mblume
Am Sat, 06 Apr 2013 02:37:31 + schrieb Steven D'Aprano:
>>> [...]
>>  def __init__(self):
>>  self.events = {}
>>  self.components = []
>>  self.contents = []
>>  self.uid = uuid4().int
>>  self.events['OnLook'] = teventlet()
>> 
>> 
>> Basically events don't get initialized like I'd like after I depickle
>> objects.
> 
> 
> Correct, by default pickle does not call the __init__ method, it just
> reconstructs the instance. Basically, it takes a snapshot of the
> instance's internal state (the __dict__) and reconstructs from the
> snapshot.
> 
> [...]
>
To the OP: Did you really mean
 self.events['OnLook'] = teventlet()
as opposed to:
 self.events['OnLook'] = teventlet

The first one executes teventlet and then assigns the result of the function to 
self.events['OnLook']. The second one assigns the function teventlet to the dict
entry (presumably so that it will be called when the objct detects the 'OnLook'
event). Unless the teventlet() function returns itself a function, an 'OnLook' 
event won't do anything useful during the remaining life time of the object, I 
think.

Regards
Martin




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


Re: Equivalent of 'wget' for python?

2008-12-08 Thread mblume
Am Mon, 08 Dec 2008 08:22:55 -0800 schrieb Robert Dailey:
> Hi,
> 
> I'm looking for a portable way to download ZIP files on the internet
> through Python. I don't want to do os.system() to invoke 'wget', since
> this isn't portable on Windows. I'm hoping the core python library has a
> library for this. Note that I'll be using Python 3.0.
> 
> Thanks.
>
IIRC, wget also exists for Windows. 

Regards
Martin

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


Re: dictionary

2008-10-24 Thread mblume
Am Fri, 24 Oct 2008 05:06:23 -0500 schrieb Tim Chase:

> ["%s="%s" % (k,v) for k,v in d.items()]
>>   File "", line 1
>> ["%s="%s" % (k,v) for k,v in d.items()]
>>   ^
>> SyntaxError: EOL while scanning single-quoted string
> 
> You have three quotation marks...  you want
> 
>"%s=%s"
> 
> not
> 
>"%s="%s"
> 
> -tkc
>
Or he may have wanted
 "%s = '%s'"
Another neat python feature.
HTH
Martin
--
http://mail.python.org/mailman/listinfo/python-list


Re: portable python

2008-10-24 Thread mblume
Am Fri, 24 Oct 2008 11:33:33 -0700 schrieb asit:

> On Oct 24, 11:18 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
>> On Fri, Oct 24, 2008 at 1:42 PM, asit <[EMAIL PROTECTED]> wrote:
>> > I code in both windows and Linux. As python is portable, the o/p
>> > should be same in both cases. But why the following code is perfect
>> > in windows but error one   in Linux ???
>>
>> What error message do you get in linux?  How are you running your code
>> in linux?  Your code seems to generally work on my Ubuntu linux box, so
>> you need to give us more information.
>>
>> --
>> Jerry
> 
> this the o/p
> [EMAIL PROTECTED]:~/hack$ python portscan.py 59.93.128.10 10 20
> Traceback (most recent call last):
>   File "portscan.py", line 33, in 
> print str(port) + " : " + scan(ip,port,timeout)
>   File "portscan.py", line 22, in scan
> return status[result]
> KeyError: 11
> [EMAIL PROTECTED]:~/hack$

Python is certainly portable, but ino order to do its job, it relies
on the underlying OS and libraries to work, which may be different from OS
to OS. The status message you define in your code 
(e.g. 10056:"already connected") seem to be specific to Windows, IIRC. 

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


Re: datetime.stdptime help

2008-05-28 Thread mblume
Am Tue, 27 May 2008 12:37:34 -0700 schrieb Dennis Lee Bieber:
> 
>   From the library reference:
> """
> Support for the %Z directive is based on the values contained in tzname
> and whether daylight is true. Because of this, it is platform-specific
> except for recognizing UTC and GMT which are always known (and are
> considered to be non-daylight savings timezones). """
> 
>   The only value that passed for me was UTC (I didn't try GMT) but...
> 
For me, only UTC, GMT, CET and CEST (Central European [Summer] Time) work.
My time.tzname is ('CET', 'CEST').
I think the documentation must be read that ***only*** 
UTC,GMT,time.tzname work.

Also, time zone names are not unique: EST can be Eastern Summer Time (US) 
as well as Eastern Summer Time (Australia).

For working with time zones, I think that a module like pytz
http://pytz.sourceforge.net/
may be better suited.

My 0.02c.
Martin
--
http://mail.python.org/mailman/listinfo/python-list


Re: like a "for loop" for a string

2008-08-17 Thread mblume
Am Sun, 17 Aug 2008 13:12:36 -0700 schrieb Alexnb:
> Uhm, "string" and "non-string" are just that, words within the string.
> Here shall I dumb it down for you?
> 
Please, bear with us. You are deep into the problem, we are not.
It doesn't help to be rude. If you can explain your problem well, you are
halfway through to the solution.

> 
> string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes
> text6  no text7 yes text8"
> 
> It doesn't matter what is in the string, I want to be able to know
> exactly how many "yes"'s there are.
> I also want to know what is after each, regardless of length. So, I want
> to be able to get "text1", but not "text4" because it is after "no" and
> I want all of "text5+more Text" because it is after "yes". It is like
> the yeses are bullet points and I want all the info after them. However,
> all in one string.
> 

How about this:
>>> s="yes t1 yes t2 no t3 yes t4 no t5"
>>> l=s.split()
>>> l
['yes', 't1', 'yes', 't2', 'no', 't3', 'yes', 't4', 'no', 't5']
>>> for i in range(1,len(l),2):
... if l[i-1] == 'yes': print l[i]
t1
t2
t4
>>> 

Now your problem is reduced to splitting the input into (yes/no) and 
(text) pairs. For a simple string like the one above split() is ok, but
> string = "yes text4 yes text5+more Text yes text6  no text7"
will be split into [ ... 'yes', 'text5+more', 'Text', 'yes', ...], 
breaking my simple algorithm. 

You could look for the index() of 'yes' / 'no', but then you'd have to 
make sure that 'text' does not contain 'yes' or 'no'.

HTH.
Martin

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


Re: recursively change values in list of lists

2008-08-24 Thread mblume
Am Sun, 24 Aug 2008 15:17:46 +0100 schrieb Carson Farmer:
> Dear list,
> 
> I'm sure this is a relatively trivial problem, but I have been unable to
> find any good examples/explanations on how to do this, so here goes:
> 
> I have multi-polygon object, which is simply a list of polygons, where
> each polygon is a list of lines, where each line is a list of points.
> What I would like to do, is change the x and y values of each point, all
> the while keeping the structure of the lists intact.
> 
> So in the end, the only thing that should be changed is the values, not
> the lists themselves... clear as mud?
> 
> Any hints, and/or suggestions are greatly appreciated,
> 
I always do something along the lines of:

for (i,obj) in enumerate(list_of_objects):
obj = do_something_to_obj(obj)
list_of_objects[i] = obj


HTH. YMMV.
Martin
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to update value in dictionary?

2008-08-27 Thread mblume
Am Wed, 27 Aug 2008 15:45:13 +0200 schrieb Diez B. Roggisch:
> 
>> dict.update({"a":1}) SETS the dict item "a" 's value to 1.
>> 
>> i want to increase it by 1. isnt that possible in an easy way? 
>> I should use a tuple for this?
> 
> 
> 1) Don't use dict as name for a dictionary, it shadows the type dict
> 
> 2) setdefault is your friend
> 
> d = {}
> d['a'] = d.setdefault('a', 1) + 1
> 
d['a'] = d.get('a', 1) + 1

seems to me a little better, as d['a'] doesn't get set twice, right?

Curious
Martin

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


Re: List of modules available for import inside Python?

2008-08-28 Thread mblume
Am Thu, 28 Aug 2008 11:23:01 -0700 schrieb Jason Scheirer:
> 
> I like to direct new users to pydoc's built-in HTTP server:
> 
> import pydoc
> pydoc.gui()
> (then click the 'open browser' button)
>

Now, this is cool !

Thanks a lot!

Martin

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


Re: Read Binary data

2008-09-04 Thread mblume
Am Thu, 04 Sep 2008 18:03:54 +0200 schrieb Fredrik Lundh:
>
>>   I am trying to read a binary file [...]
> 
> 
> f = open("a.bin", "rb") # read binary data 
> s = f.read() # read all bytes into a string
> 
> import array, sys
> 
> a = array.array("f", s) # "f" for float 
> if sys.byteorder != "big":
> a.byteswap()
> 
For more complicated structures, the struct module may help.

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


Re: Portable way to tell if a process is still alive

2009-12-29 Thread mblume
Am Tue, 29 Dec 2009 12:35:11 +0430 schrieb Laszlo Nagy:
> Suppose we have a program that writes its process id into a pid file.
> Usually the program deletes the pid file when it exists... But in some
> cases (for example, killed with kill -9 or TerminateProcess) pid file is
> left there. I would like to know if a process (given with its process
> id) is still running or not. I know that this can be done with OS
> specific calls. But that is not portable. It can also be done by
> executing "ps -p 23423" with subprocess module, but that is not portable
> either. Is there a portable way to do this?
> 
> If not, would it be a good idea to implement this (I think very
> primitive) function in the os module?
> 
In UNIXish systems, successfully sending the signal 0 to a process means
that a process with this pid is still running. Don't know whether this 
works on Windows. What you still don't know is whether this process is 
actually your process or that the OS has reassigned this pid to something
else.

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


Re: ImportError: No module named glib

2010-03-05 Thread mblume
Am Fri, 05 Mar 2010 10:30:49 +0200 schrieb Michael Joachimiak:

> I am too new to python.
> If anybody has an idea what to do please help. when I use
> 
> import glib
> 
> in my code I get this:
> 
 import glib
> Traceback (most recent call last):
>   File "", line 1, in 
> ImportError: No module named glib
> 

Shot in the dark: maybe "import pyglib"?
Hint: you can look around in /usr/lib/python to see what files can be imported.

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


Re: TypeError: int argument required

2009-06-12 Thread mblume
Am Thu, 11 Jun 2009 20:56:24 -0700 schrieb lucius:

> I am trying to
> print some values to a file (using c's printf like method). TypeError:
> int argument required
> # this works, i see value on screen
>  print  w, h, absX, absY
> 
Are you sure that w or h are not returned as strings?
Hint: try this in an interpreter:
w="100"
print "%d" % (w)

> 
> # this fails, I get "TypeError: int argument required"
>  print >> fo, " style=\"fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-
> opacity:0.9\"/> " % (absX, absY, w, h)
> 

You could simplify such as an expression by writing:
print 'x="%f"' % (x)

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


Re: Is this pylint error message valid or silly?

2009-06-19 Thread mblume
Am Fri, 19 Jun 2009 00:56:18 + schrieb Matthew Wilson:

> Here's the code that I'm feeding to pylint:
> 
> $ cat f.py
> from datetime import datetime
> 
> def f(c="today"):
> 
> if c == "today":
> c = datetime.today()
> 
> return c.date()
> 
> 
> And here's what pylint says:
> 
> $ pylint -e f.py
> No config file found, using default configuration *
> Module f
> E: 10:f: Instance of 'str' has no 'date' member (but some types
> could not be inferred)
> 
> Is this a valid error message?  Is the code above bad?  If so, what is
> the right way?
> 
> I changed from using a string as the default to None, and then pylint
> didn't mind:
> 
> 
> $ cat f.py
> from datetime import datetime
> 
> def f(c=None):
> 
> if c is None:
> c = datetime.today()
> 
> return c.date()
> 
> $ pylint -e f.py
> No config file found, using default configuration
> 
> I don't see any difference between using a string vs None.  Both are
> immutable.  I find the string much more informative, since I can write
> out what I want.
> 
> Looking for comments.
> 
> Matt

Think of what happens if somebody calls
some_result = f("yesterday")

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


Re: Network and socket programming in python

2010-09-05 Thread mblume
Am Sat, 04 Sep 2010 21:29:49 -0700 schrieb shivram:
>
> i want to learn network and socket programming but i would like to do
> this in python.Reason behind this is that python is very simple and the
> only language i know .
> anybody can suggest me which book should i pick. the book should have
> following specification-- 1)not tedious to follow
> 2)lots of example
> 3)starts with some networking stuff and then get into codes
> 
> thanks in advance with regards.
>
Ever heard of Google?
"python network tutorial" / "python socket tutorial" should get you started.

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