The '-c' CLI option removes just the command str.

2020-05-05 Thread Simon Forman via Python-list
Is this anything?  When you run a python command from the shell to just print 
the command line args you get this:

$ python -c "import sys; print(sys.argv)"
['-c']

But I would expect one of these:

Either the '-c' option consumes both args:

$ python -c "import sys; print(sys.argv)"
[]

Or it leaves them both in:

$ python -c "import sys; print(sys.argv)"
['-c', 'import sys; print(sys.argv)']


What do you think?

Warm regards,
~Simon

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


Re: A curious bit of code...

2014-02-14 Thread Simon Forman
On Friday, February 14, 2014 1:01:48 PM UTC-8, Mark Lawrence wrote:
[snip]
 
 Pleased to have you on board, as I'm know that Terry Reedy et al can do 
 with a helping hand.
 
 But please note you appear to be using google groups, hence the double 
 line spacing above and trying to reply to paragraphs that run as a 
 single line across the screen.  Therefore would you please read and 
 action this https://wiki.python.org/moin/GoogleGroupsPython, thanks.


Ah!  Thanks for the tip. ;-)

(Just last night I was trying to explain to a friend about Usenet
and how it's not Google Groups.)


I really hope I can be of use with IDLE.  I've been using it for years now. :)


Warm regards,
~Simon



-- 
http://phoenixbureau.org/
http://phoenixbureau.org/blog.html
http://twitter.com/SimonForman



The history of mankind for the last four centuries is rather like that of
an imprisoned sleeper, stirring clumsily and uneasily while the prison that
restrains and shelters him catches fire, not waking but incorporating the
crackling and warmth of the fire with ancient and incongruous dreams, than
like that of a man consciously awake to danger and opportunity.
--H. P. Wells, A Short History of the World

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


A curious bit of code...

2014-02-14 Thread Simon Forman

(Apologies if this results in a double-post.)

On Friday, February 14, 2014 1:01:48 PM UTC-8, Mark Lawrence wrote:
[snip]
 
 Pleased to have you on board, as I'm know that Terry Reedy et al can do 
 with a helping hand.
 
 But please note you appear to be using google groups, hence the double 
 line spacing above and trying to reply to paragraphs that run as a 
 single line across the screen.  Therefore would you please read and 
 action this https://wiki.python.org/moin/GoogleGroupsPython, thanks.



Ah!  Thanks for the tip. ;-)

(Just last night I was trying to explain to a friend about Usenet
and how it's not Google Groups.)


I really hope I can be of use with IDLE.  I've been using it for years now. :)


Warm regards,
~Simon

-- 
http://phoenixbureau.org/
http://phoenixbureau.org/blog.html
http://twitter.com/SimonForman



The history of mankind for the last four centuries is rather like that of
an imprisoned sleeper, stirring clumsily and uneasily while the prison that
restrains and shelters him catches fire, not waking but incorporating the
crackling and warmth of the fire with ancient and incongruous dreams, than
like that of a man consciously awake to danger and opportunity.
--H. P. Wells, A Short History of the World

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


Pigeon Computer 0.1 Initial (BETA) release

2012-12-21 Thread Simon Forman
Pigeon Computer 0.1 Initial (BETA) release

Summary


The Pigeon Computer is a simple but sophisticated system for learning
and exploring the fundamentals of computers and programming.

It is written to support a course or class (as yet pending) to learn
programming from the bit to the compiler.

There is a DRAFT manual and a Pigeon User Interface that includes:

  * An assembler for the ATmega328P micro-controller.
  * A polymorphic meta-compiler.
  * Forth-like firmware in assembly.
  * Simple high-level language for assembly control structures.
  * A virtual computer that illustrates Functional Programming.

Source code is released under the GPL (v3) and is hosted on Github:
  https://github.com/PhoenixBureau/PigeonComputer

The manual is online in HTML form here:
  http://phoenixbureau.github.com/PigeonComputer/

Mailing list:
  https://groups.google.com/d/forum/pigeoncomputer

It has been tested on Linux with Python 2.7, YMMV.

I'm releasing it now because it's basically done even though it needs
polish and I'm just too excited about it.  Happy End of the World Day!


Details


The class starts with the bit and develops the core concepts
underlying computers with a hands-on exploration of bits and bytes as
embodied in physical bits on a table: coins or other binary
tokens.

Using this RAM-abacus we explore different coding schemes (binary
numbers, ASCII codes, Grey codes, etc.) and then play with simple
operations to show how the bits can be used to perform math and logic
as well as simple text processing.  We number the operations, lay in
simple programs in the RAM, and manually act out the actions of a
(hypothetical) CPU.

Once the students have the idea we introduce a concrete example: the
ATmega328P, a simple but powerful 8-bit RISC micro-controller with
Harvard Architecture and a 16-bit program RAM that is used in many
popular devices such as the Arduino Uno.

Using this chip and the Pigeon User Interface students will be able to
develop simple robots and other devices and program them themselves.

 - - -

The Pigeon User Interface (PUI) is a simple and elegant IDE that
presents the user with a very simple but powerful stack-based virtual
computer that includes commands for assembling object code for the
ATmega328P and compiling high-level languages to assembly.

To get the students into assembly language the Pigeon Computer system
includes a tiny Forth-like firmware that implements a command
interpreter on the serial port (on-chip USART peripheral) in less than
a kilobyte of object code.

The firmware is a stub. It is meant to be understood and extended by
the students rather than used as-is.  I have written some commands for
it that give it the ability to use the TWI (I2C) subsystem of the
ATmega328P to communicate with other devices, and used that to talk to
an Inertial Motion Unit and get gyro and accelerometer readings, so
there's that.

Once students are comfortable with assembly, the PUI includes a tiny,
powerful, and extremely flexible meta-compiler: Val Shorre's Meta-II
(implemented in Python.)  This amazing bit of software is polymorphic
in the sense that you can feed it different compiler descriptions and
it becomes different compilers.

The underlying Meta-II virtual machine works just like an assembler to
read and become a compiler description, then it operates as a
syntax-driven compiler for the language described in the compiler
description.

It is simple enough to be understood by someone willing to take the
time, yet powerful enough to be used in earnest to develop little
languages for use in real projects.  The PUI includes a simple
compiler (a description in Meta-II) that generates control-flow
constructs for the firmware commands, illustrating the path from
hand-coded assembly to high-level languages.

(Those interested in further information regarding these issues are
urged to examine the work of the Viewpoints Research Institute.)

 - - -

Last but not least, the virtual computer in the PUI is a stack-based
Forth-like fully Functional Programming not-quite-language.  It mimics
the operation of the firmware but goes far beyond what that code is
capable of as it has the whole of Python and the host machine to work
with.

When you start the PUI it creates a roost directory (I apologize for
the pun) where it writes a Git repository (using a pure-Python Git
implementation called Dulwich) and two files: 'log' and
'system.pickle'.

Then the GUI starts and the user sees a window with two panes. On the
left is a listbox that shows the contents of the stack and on the
right is a text editor.

The 'log' file is kept in sync with the contents of the text
(auto-saved two seconds after the last edit) and the 'system.pickle'
file keeps a copy of the serialized state of the PUI and is updated
after every change to the virtual computer's state.  When either of
these files change a commit is made to the git repository.

This ensures that you never have to save (ever!) 

Re: testing if a list contains a sublist

2011-08-20 Thread Simon Forman
On Mon, Aug 15, 2011 at 4:26 PM, Johannes dajo.m...@web.de wrote:
 hi list,
 what is the best way to check if a given list (lets call it l1) is
 totally contained in a second list (l2)?

 for example:
 l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2
 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained in l2
 l1 = [1,2,3], l2 = [1,3,5,7] - l1 is not contained in l2

 my problem is the second example, which makes it impossible to work with
 sets insteads of lists. But something like set.issubset for lists would
 be nice.

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


Probably not the most efficient way, but I wanted to mention it:


from difflib import SequenceMatcher


def list_in(a, b):
'''Is a completely contained in b?'''
matcher = SequenceMatcher(a=a, b=b)
m = matcher.find_longest_match(0, len(a), 0, len(b))
return m.size == len(a)


Cheers,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


[issue12590] First line and cursor not visible when opening files

2011-07-26 Thread Simon Forman

Simon Forman forman.si...@gmail.com added the comment:

You're very welcome.

--

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



[issue12590] First line and cursor not visible when opening files

2011-07-19 Thread Simon Forman

New submission from Simon Forman forman.si...@gmail.com:

In IDLE if you open a file that is longer than the editor window the first 
line, with the cursor, is scrolled off the top of the window making it appear 
as though the file begins at the second line.

This can be fixed by adding 'text.see(insert)' to the end of the EditorWindow 
__init__() method, but see Bruce Sherwood's comment on 
http://bugs.python.org/issue10079#msg118618

--
components: IDLE
messages: 140716
nosy: sforman
priority: normal
severity: normal
status: open
title: First line and cursor not visible when opening files
type: behavior
versions: Python 2.6

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



Re: parallel class structures for AST-based objects

2009-11-22 Thread Simon Forman
On Sun, Nov 22, 2009 at 4:50 AM, Diez B. Roggisch de...@nospam.web.de wrote:
 Steve Howell schrieb:

 On Nov 21, 4:07 pm, MRAB pyt...@mrabarnett.plus.com wrote:

 I don't see the point of EvalNode and PrettyPrintNode. Why don't you
 just give Integer, Sum and Product 'eval' and 'pprint' methods?

 That's a good question, and it's the crux of my design dilemma.  If
 ALL I ever wanted to to with Integer/Sum/Product was to eval() and
 pprint(), then I would just add those methods to Integer, Sum, and
 Product, and be done with it, as you suggest.  But what happens when
 somebody wants to extend capability?  Should every future software
 developer that wants to use Integer/Sum/Product extend those classes
 to get work done?  What if they want to store additional state for
 nodes?


 What's usually done is to create visitors/matchers. Those traverse the AST,
 and either only visit, or return transformed versions of it (think e.g.
 algebraic optimization)

 A visitor will roughly look like this:


 class ASTVisitor(object):



   def visit(self, node):
       name = node.__class__.__name__.lower()
       if hasattr(self, visit_%s % name):
           getattr(self, visit_%s % name)(node)
       for child in node:
           self.visit(child)



 You can of course chose another type of dispatch, using e.g. a generic
 method.

 Then you create Visitors for specific tasks - pretty-printing, evaluation,
 rewriting. Those don't have the overhead of your current design with all
 those factory-mapping stuff, and yet you aren't forced to put logic into AST
 you don't want there.



FWIW I often use John Aycock's SPARK (Scanning, Parsing, and Rewriting
Kit) for this sort of thing.  It has a GenericASTTraversal which is a
Visitor pattern according to Design Patterns.

http://pages.cpsc.ucalgary.ca/~aycock/spark/

It's apparently distributed with the python source, but it's not in
the standard library, more's the pity IMO.

There's a bit of a learning curve but it's well worth it.

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


Re: make two tables having same orders in both column and row names

2009-11-19 Thread Simon Forman
On Wed, Nov 18, 2009 at 3:57 PM, Ping-Hsun Hsieh hsi...@ohsu.edu wrote:
 Hi,

 I would like to compare values in two table with same column and row names, 
 but with different orders in column and row names.
 For example, table_A in a file looks like the follows:
 AA100   AA109   AA101   AA103   AA102
 BB1     2       9       2.3     1       28
 BB3     12      9       2.3     1       28
 BB9     0.5     2       2.3     1       28
 BB2     2       9       21      1       20

 Table_B in the other file looks like the follows:
 AA101   AA109   AA100   AA103   AA102
 BB1     2       9       2.3     2       28
 BB2     2       9       2.3     1       28
 BB9     2       9       2.3     1       28
 BB3     2       2       2       1       28

 Can anyone give an efficient way to make the two tables having same orders in 
 column and row names so I can easily and correctly compare the values in 
 positions?

 Thanks,
 PingHsun

Depending on the kind of comparisons you want to do and the sizes of
your input files you could build a dict of dicts.
For example:


def generate_dict_values(F):
column_names = f.readline().split()
for line in F:
row = line.split()
yield row[0], dict(zip(column_names, row[1:]))


# Fake a file.
from StringIO import StringIO
f = StringIO('''\
AA100   AA109   AA101   AA103   AA102
BB1 2   9   2.3 1   28
BB3 12  9   2.3 1   28
BB9 0.5 2   2.3 1   28
BB2 2   9   21  1   20
''')


d = dict(generate_dict_values(f))

# Now you can access the values without regards to the order in the
file like so:

d['BB9']['AA109']



# Pretty print the dict of dicts.
from pprint import pprint
pprint(d)

# Prints:
#
#{'BB1': {'AA100': '2',
# 'AA101': '2.3',
# 'AA102': '28',
# 'AA103': '1',
# 'AA109': '9'},
# 'BB2': {'AA100': '2',
# 'AA101': '21',
# 'AA102': '20',
# 'AA103': '1',
# 'AA109': '9'},
# 'BB3': {'AA100': '12',
# 'AA101': '2.3',
# 'AA102': '28',
# 'AA103': '1',
# 'AA109': '9'},
# 'BB9': {'AA100': '0.5',
# 'AA101': '2.3',
# 'AA102': '28',
# 'AA103': '1',
# 'AA109': '2'}}
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Language mavens: Is there a programming with if then else ENDIF syntax?

2009-11-18 Thread Simon Forman
On Wed, Nov 18, 2009 at 4:15 AM, Steve Howell showel...@yahoo.com wrote:
 On the topic of switch statements and even-more-concise-then-we-have-
 already if/elif/else/end constructs, I have to say that Python does
 occasionally force you to write code like the code below.  Maybe
 force is too strong a word, but Python lends itself to if/elif
 blocks like below, which get the job done just fine, but which are not
 syntactically pretty, due to the (el){0,1}if kind == duplication.
 There are often cases where if/elif statements are just a smell that
 you do not know how to do dictionary lookups, but if you converted the
 below code to use dictionary lookups, you would complicate the code
 almost as much as you abstracted the code, if not more, unless I am
 just being very naive.  Anonymous methods would help to a certain
 degree.  I am not saying I want either anonymous methods or switch
 statements, but the lack of either in a language leads to very
 procedural looking code when you use number-of-lines-of-code as a
 (possibly dubious) metric.

 Maybe this excerpt can be golfed down to something simpler, I would
 love to see it!

        if kind == 'dict':
            return dictionary_schema(ast)
        elif kind == 'list':
            method = dictionary_schema(ast)
            return lambda lst: map(method, lst)
        elif kind == 'attr':
            return ((lambda obj: getattr(obj, ast.field)), ast.field)
        elif kind == 'key':
            return (lambda obj: obj.get(ast.field), ast.field)
        elif kind == 'as':
            method, old_name = schema(ast.parent)
            return (method, ast.synonym)
        elif kind == 'call':
            method, old_name = schema(ast.parent)
            def call(obj):
                return method(obj)()
            return (call, old_name)
        elif kind == 'recurse':
            expr = ast.expr
            kind = expr.kind
            method, field_name = schema(ast.parent)
            if kind in ['attr', 'key']:
                new_method, new_field_name = schema(expr)
                field_name = new_field_name
            elif kind in ['dict', 'list']:
                new_method = schema(expr)
            else:
                raise Exception('unknown kind!')
            def recurse(obj):
                obj = method(obj)
                return new_method(obj)
            return (recurse, field_name)
        else:
            raise Exception('unknown kind!')


This code is perhaps not a great example of your point.  Every
(el){0,1}if clause (other than the ones in the 'recurse' branch) end
in return statements, so the el's are pointless.

FWIW I might write it like so:

class ASTthing:

def processAST(self, ast, kind):
try:
method = getattr(self, 'do_' + kind)
except AttributeError:
raise Exception('unknown kind!')

self.ast = ast

return method()

def do_dict(self):
return dictionary_schema(self.ast)

def do_list(self):
method = dictionary_schema(self.ast)
return lambda lst: map(method, lst)

def do_attr(self):
field_name = self.ast.field
return lambda obj: getattr(obj, field_name), field_name

def do_key(self):
field_name = self.ast.field
return lambda obj: obj.get(field_name), field_name

def do_as(self):
method, old_name = schema(self.ast.parent)
return method, self.ast.synonym

def do_call(self):
method, old_name = schema(self.ast.parent)
return lambda obj: method(obj)(), old_name

def do_recurse(self):
expr = self.ast.expr
kind = expr.kind

method, field_name = schema(self.ast.parent)

if kind in 'attrkey':
new_method, field_name = schema(expr)

elif kind in 'dictlist':
new_method = schema(expr)

else:
raise Exception('unknown kind!')

def recurse(obj):
obj = method(obj)
return new_method(obj)

return recurse, field_name


IMO, that's more pythonic and less complicated.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using struct module on a file

2009-11-18 Thread Simon Forman
On Wed, Nov 18, 2009 at 11:42 AM, Ulrich Eckhardt dooms...@knuut.de wrote:
 Hia!

 I need to read a file containing packed binary data. For that, I find the
 struct module pretty convenient. What I always need to do is reading a chunk
 of data from the file (either using calcsize() or a struct.Struct instance)
 and then parsing it with unpack(). For that, I repeatedly write utility
 functions that help me do just that, but I can't imagine that there is no
 better way for that.

 Questions:
 0. Am I approaching this from the wrong direction? I'm not a programming
 noob, but rather new to Python still.
 1. The struct module has pack_into() or unpack_from(), could I somehow
 combine that with a file?
 2. Is there some easier way to read files? I know about array and xdrlib,
 but neither really fit my desires.
 3. Failing all that, would you consider this a useful addition to the struct
 module, i.e. should I write a feature request?

 Thanks!

 Uli

You might look at Construct:

http://construct.wikispaces.com/

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


Re: Anything equivalent to cassert in C++?

2009-11-17 Thread Simon Forman
On Tue, Nov 17, 2009 at 4:19 PM, Peng Yu pengyu...@gmail.com wrote:
 There are some assertion code (testing if a condition is false, if it
 is false, raise an Error object) in my python, which is useful when I
 test my package.  But such case would never occur when in the produce
 code. If I keep them in if statement, it will take some runtime. I'm
 wondering what is the practice that take care of the assertion code in
 python.

Read the manual:

http://docs.python.org/reference/simple_stmts.html#the-assert-statement

In the current implementation, the built-in variable __debug__ is
True under normal circumstances, False when optimization is requested
(command line option -O). The current code generator emits no code for
an assert statement when optimization is requested at compile time.

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Go

2009-11-15 Thread Simon Forman
On Sat, Nov 14, 2009 at 5:10 PM, Terry Reedy tjre...@udel.edu wrote:
 Paul Rubin wrote:

 Mark Chu-Carroll has a new post about Go:


  http://scienceblogs.com/goodmath/2009/11/the_go_i_forgot_concurrency_an.php

 In a couple of minutes, I wrote his toy prime filter example in Python,
 mostly from the text rather than the code, which I can barely stand to read.
 It ran the first time without error.

 def plurals():
  i = 2
  while True:
    yield i
    i += 1

 def primefilter(src, prime):
  for i in src:
    if i % prime:
      yield i

 src = plurals()
 while True:
  i = next(src)
  print(i)
  src = primefilter(src, i)

 As I commented there
 It stopped at 7877 when it hit the default recursion limit of 1000, which
 could easily be increased to get out-of-memory error instead.

 I think Google is making a blunder if it moves to another old-fashioned
 language whose code is bloated with junky boilerplate that doubles the size.
 It would be much better, for instance, to tweak Python, which it has had
 great success with, to better run on multiple cores.

 Terry Jan Reedy

FWIW,

def plurals():
 i = 3
 while True:
   yield i
   i += 2
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calendar Stuff

2009-11-10 Thread Simon Forman
On Tue, Nov 10, 2009 at 12:53 PM, Victor Subervi
victorsube...@gmail.com wrote:
 Hi;
 I have the following code:

 import calendar, datetime

 def cal():
   ...
   myCal = calendar.Calendar(calendar.SUNDAY)
   today = datetime.date.today()
   day = today.day
   mo = today.month
   yr = today.year
 #  month = myCal.monthdayscalendar(int(time.strftime(%Y))
   month = myCal.monthdayscalendar(yr, mo)
   print 'hi'

 html headers are included. No matter which one of the last two lines I
 comment out, I never get to the point of printing 'hi'. (If I comment them
 both out, it does print.) What do?
 TIA,
 Victor

Have you tried walking through the code line-by-line in the
interactive interpreter?

That should give you an idea of why those lines aren't working.  Also,
if your code never prints 'hi', what does it do instead?  Hang? Or
give you a traceback?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Serious Privileges Problem: Please Help

2009-11-09 Thread Simon Forman
On Mon, Nov 9, 2009 at 12:44 PM, Victor Subervi victorsube...@gmail.com wrote:
 Did you give up on me?
 V


Please don't top-post.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NEWB problem with urllib2

2009-11-09 Thread Simon Forman
On Mon, Nov 9, 2009 at 6:29 PM, Penn powderd...@gmail.com wrote:
 I just installed PyDev into Eclipse using the 'update' method and did
 the standard installation.  I allowed it to Auto Configure itself and
 ran a Hello World module to make sure I was in the ballpark.

 I got an starting module up and have run Hello World but now am
 stuck on getting urlopen to import from urllib2 for the following
 example.

 from urllib2 import *    # doesn't give me an error
 ur = urlopen(http://www.daniweb.com/forums/thread161312.html;) #
 gives me Undefined Variable: urlopen

 so I tried just

 import urllib2        # Eclipse gives Unresolved Import

 Is urllib2 not on my path?  Isn't urllib2 in the standard installation
 and shouldn't that automatically be on my path?  If not, how can I get
 it on the path?

 ThankS!!

This sounds more like a PyDev and/or Eclipse problem than an urllib2 problem. :)

One thing you can check: open the raw python interpreter outside of
Eclipse and try importing urllib2 there.  You might also try the
interpreter interface within Eclipse (if it provides one.)

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


Re: Why do you use python?

2009-11-05 Thread Simon Forman
On Sat, Oct 31, 2009 at 2:11 AM, sk catchyouraak...@gmail.com wrote:
 What would be your answer if this question is asked to you in an
 interview?

 a modified version might be:
 Where would you use python over C/C++/Java?

 (because my resume says I know C/C++/Java)?


Mark Miller has some adages posted on his homepage. [1]  One of my
favorites is this:

A Computer's Perspective on Moore's Law:
Humans are getting more expensive at an exponential rate.

Python saves you human-time.

Python allows you to write software that runs, is readable, can be
maintained and modified easily, etc...  far better than any other
language I've ever used.

You can write working code, and read and understand already-written
code /faster/ in Python than any other language I've ever used.


Use Python.  I would use C (to write Python extensions) only where
profiling had shown a definite hotspot, and then only when there were
no better algorithmic choices available.  I would never use C++ or
Java.


(N.B. I've managed to be gainfully, and blissfully, employed
programming in Python for about six years now.  I've passed on
otherwise interesting jobs because the folks involved were using
something other than Python.)

I'm actually working at a startup at the moment that originally hired
me to do python and then decided to use PHP because more of the team
(two out of three) knew it and didn't know python.  I figured what
the heck, it's been awhile, and it will look good on my resume so I
have stuck with them.

PHP isn't as horrible as I remembered, but it's still horrible.
/Everything/ is horrible compared to Python.  WTF is wrong with
people?  (Disclaimer: Smalltalk and LISP are not horrible... *grin*)

Anyway, I wouldn't say all that in an interview, heh, but that's my $0.02.

My strong advice to you or any programmer is, don't bother
interviewing with a company that's not already committed to using
Python as their main language (modulo extraordinary circumstances.)

[1] http://www.caplet.com/adages.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: disutils, project structure developing - n00b question

2009-10-29 Thread Simon Forman
On Thu, Oct 29, 2009 at 2:42 PM, Wells we...@submute.net wrote:
 So I have my project partitioned like so:

 ./setup.py
 ./pymlb/
 ./pymlb/fetcher.py
 ./demos
 ./demos/demo.py

 In demo.py I have:

 from pymlb import fetcher

 However, it fails b/c pymlb is up a folder. It's also NOT installed as
 a module in my module directory because it's a development effort and
 I don't want to run setup.py to install them. See what I mean?

 What's the work around here?


In order for from pymlb import fetcher no work you must make the
'./pymlb' directory into a package by adding a file called
__init__.py (it can be empty.)

Then make sure the top directory (i.e. '.' in your example) is in
the python PATH.  There are a couple of ways to do that:

1.) Hack it in demo.py before importing fetcher
  (i.e. import sys; sys.path.append(string absolute path of '.'))

2.) Use the PYTHONPATH environment variable.

3.) Use a .pth file (See http://docs.python.org/library/site.html)
You'll have to figure out what directory to put it in (on my system
'/usr/lib/python2.5/site-packages' works) Note, although it's not
mentioned in the site module docs you can include an absolute path and
it will be added to sys.path.

There is additional good information about .pth files on Bob
Ippolito's blog:
http://bob.pythonmac.org/archives/2005/02/06/using-pth-files-for-python-development/
Be sure to read the comments too.

4.) Probably some other method(s) that someone else will tell you...  ;]

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: disutils, project structure developing - n00b question

2009-10-29 Thread Simon Forman
On Thu, Oct 29, 2009 at 3:45 PM, Simon Forman sajmik...@gmail.com wrote:
 In order for from pymlb import fetcher no work you must make the

s/no/to/

D'oh!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What IDE has good git and python support?

2009-10-28 Thread Simon Forman
On Tue, Oct 27, 2009 at 11:56 AM, Aweks a...@ewadev.com wrote:
 what do you use?

I use IDLE for python and Bash for GIT.

Regards,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python web service or Apache?

2009-10-26 Thread Simon Forman
On Mon, Oct 26, 2009 at 10:00 AM, Peng Yu pengyu...@gmail.com wrote:
 On Sun, Oct 25, 2009 at 11:09 PM, Simon Forman sajmik...@gmail.com wrote:
 On Sun, Oct 25, 2009 at 4:47 PM, Peng Yu pengyu...@gmail.com wrote:
 Although, python can be used to provide web service. The following
 webpage also mentioned, Apache the best and most widely used web
 server on the Internet today, check it out. If you want to run your
 own web server this is the one to get, you can get binaries for both
 Windows and Unix. You can download the entire sourcecode if you want
 to check how it was made. Therefore, it would be better to use Apache
 rather than python to provide web service, right?

 http://fragments.turtlemeat.com/pythonwebserver.php


 Both best and better (that website and you, respectively) omit
 mention of the criteria used for the evaluation.

 To determine what is best you must first answer for what?


 (Also, it is reasonable to use /both/ apache and python together, with
 mod_python or mod_wsgi, etc...)

 I have never made a web server before. So I don't know what criterion
 I should use? If possible, would you please let me know what pros and
 cons you can think of?

Well, criteria like, how much traffic do you need to support?  How
often will you be changing your code?  Do you have your own server or
are you using some sort of hosting plan with another company?  Those
are pretty general.  I'm not a web server expert.

I recently wrote a small server script. It's only job was to listen
for POST requests from a third-party SVN repository service that
indicated a commit had been made and then trigger a buildbot
build/test cycle.  For this the BaseHTTPServer module was sufficient.

Apache is highly regarded, but I've heard Nginx is, or can be, faster
(I don't know if that's true, but I've heard it...)

Apache has a /lot/ of features and capabilities, which can mean it
will have a steep learning curve (again depending on what you want to
do with it.)  But if you're using an OS with good package support,
like Ubuntu linux, getting a basic Apache installation up and running
takes one command. (And some time understanding the default
configuration...)

There are also options like CherryPy (http://www.cherrypy.org/) or
Twisted Web (http://twistedmatrix.com/trac/) which are HTTP servers
(and more) written in python.

Probably the most important question to answer is, How much do you
want to learn?

 How to use apache and python together? Does each of them offer some
 functions that are not available in the other? Thank you!

You're welcome. :)

Possibly the simplest method is to write a CGI script in python.  I've
already mentioned mod_python and mod_wsgi which are Apache modules
that let you use python with Apache.

There are also web frameworks like Django and TurboGears which can
work with Apache.

These options all offer different functions while providing
essentially the same functionality.  You'll have to do your own
homework to figure out which is the best for you.

Regards,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: handling PAMIE and lxml

2009-10-26 Thread Simon Forman
On Mon, Oct 26, 2009 at 3:05 AM, elca high...@gmail.com wrote:

 Hello,
 i was open anther new thread ,old thread is too long.

Too long for what?

 first of all,i really appreciate other many people's help in this newsgroup.
 im making webscraper now.
 but still problem with my script source.
 http://elca.pastebin.com/m52e7d8e0
 i was upload my script in here.
 so anybody can modify it.
 main problem is , if you see line number 74 ,75 in my source,
 you can see this line thepage = urllib.urlopen(theurl).read().
 i want to change this line to work with Pamie not urllib.

Why?

 if anyone help me,really appreciate.
 thanks in advance.

I just took a look at your code.  I don't want to be mean but your
code is insane.

1.) you import HTMLParser and fromstring but don't use them.

2.) the page_check() function is useless.  All it does is sleep for
len(www.naver.com) seconds.  Why are you iterating through the
characters in that string anyway?

3.) On line 21 you have a pointless pass statement.

4.) The whole if x: statement on line 19 is pointless because both
branches do exactly the same thing.

5.) The variables start_line and end_line you're using strings.  This
is not php. Strings are not automatically converted to integers.

6.) Because you never change end_line anywhere, and because you don't
use break anywhere in the loop body, the while loop on line 39 will
never end.

7.) The while loop on line 39 defines the getit() function (over and
over again) but never calls it.

8.) On line 52 you define a list call results and then never use it anywhere.

9.) In getit() the default value for howmany is 0, but on line 68 you
subtract 1 from it and the next line you return if not howmany.  This
means if you ever forget to call getit() with a value of howmany above
zero that if statement will never return.

8.) In the for loop on line 54, in the while loop on line 56, you
recursively call getit() on line 76.  wtf?  I suspect lines 73-76 are
at the wrong indentation level.

9.) On line 79 you have a bare except, which just calls exit(1) on
the next line.  This replaces the exception you had (which contains
important information about the error encountered) with a SystemExit
exception (which does not.)  Note that an uncaught exception will exit
your script with a non-zero return code, so all you're doing here is
throwing away debugging information.

10.) On line 81 you have 'return()'.  This line will never be reached
because you just called exit() on the line before.  Also, return is
not a function, you do not need '()' after it.

11.) Why do you sleep for half a second on line 83?


I cannot believe that this script does anything useful.  I would
recommend playing with the interactive interpreter for awhile until
you understand python and what you're doing.  Then worry about Pamie
vs. urllib.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python web service or Apache?

2009-10-25 Thread Simon Forman
On Sun, Oct 25, 2009 at 4:47 PM, Peng Yu pengyu...@gmail.com wrote:
 Although, python can be used to provide web service. The following
 webpage also mentioned, Apache the best and most widely used web
 server on the Internet today, check it out. If you want to run your
 own web server this is the one to get, you can get binaries for both
 Windows and Unix. You can download the entire sourcecode if you want
 to check how it was made. Therefore, it would be better to use Apache
 rather than python to provide web service, right?

 http://fragments.turtlemeat.com/pythonwebserver.php


Both best and better (that website and you, respectively) omit
mention of the criteria used for the evaluation.

To determine what is best you must first answer for what?


(Also, it is reasonable to use /both/ apache and python together, with
mod_python or mod_wsgi, etc...)


Regards,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple audio

2009-10-20 Thread Simon Forman
On Tue, Oct 20, 2009 at 4:04 PM, Peter Chant pet...@mpeteozilla.vco.uke wrote:
 What are recommendations for simple audio playback?  I want to play back on
 linux (Slackware), which uses alsa.  There seem to be many ways - but some
 are a couple of years old and won't compile, like pymedia, or seem not
 widely used and need pulseaudio (swmixer) which I have not installed.  I
 want to be able to play sounds from a numpy array and would like it to be
 non-blocking so I can play sounds without locking out a pyQt front end.

 Whats the current best way?


Someone else will probably give you better advice, but have you looked
at pygame?  IIRC they have a pretty simple audio playback api.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Iterators

2009-10-16 Thread Simon Forman
On Fri, Oct 16, 2009 at 8:22 AM, Duncan Booth
duncan.bo...@invalid.invalid wrote:
 Chris Rebert c...@rebertia.com wrote:

 Essentially, file iterators are dumb and don't keep track of where in
 the file the next line starts, instead relying on their associated
 file object to keep track of the current position in the file; the
 iterator's state is little more than a reference to its associated
 file object. When asked for the next line, a file iterator just
 reads forward to the next newline from the file object's current
 position, changing the current position as tracked by the file object
 as a side-effect. Thus, using multiple iterators to the same file
 object can have the results you're seeing when these side-effects
 interact.

 Nothing 'dumb' or 'smart' about it: it is simply that a file object is
 already an iterator. Trying to create an iterator from an existing iterator
 in Python never duplicates the iterator.

 f = open('somefile')
 iter(f) is f
 True


The OP's question has been answered but since no one mentioned the
itertools.tee() function yet I figured I would..

http://docs.python.org/library/itertools.html#itertools.tee


In [1]: open('afile', 'w').write('''line one
   ...: line two
   ...: line three''')

In [2]: from itertools import tee

In [3]: i0, i1 = tee(open('afile'))

In [4]: i0.next()
Out[4]: 'line one\n'

In [5]: i0.next()
Out[5]: 'line two\n'

In [6]: i1.next()
Out[6]: 'line one\n'

In [7]: i0.next()
Out[7]: 'line three'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a=[1,2,3,4].reverse() - why a is None?

2009-10-13 Thread Simon Forman
On Mon, Oct 12, 2009 at 4:44 AM, Nadav Chernin nada...@qualisystems.com wrote:

        Chris Withers wrote:

        ...becauase you were looking for:

        reversed([1,2,3,4])

 OK, but my question is generic. Why when I use object's function that
 changed values of the object, I can't to get value of it on the fly
 without writing additional code?

 a=[1,3,2,4]
 a.sort()
 a.reverse()
 a
 [4, 3, 2, 1]

 Why I can't a==[1,3,2,4].sort().reverse() ?

That's just the way it is with list objects.

Note that string objects do work that way:

In [1]: FOO.lower().replace('o', 'a')
Out[1]: 'faa'

But this is because string objects are immutable in python, so their
methods must return new strings.

Bottom line: read the documentation.  (In the python interpreter you
can use the built-in function help() on any object or function or
method to get information on it.)

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why ELIF?

2009-10-11 Thread Simon Forman
On Sun, Oct 11, 2009 at 2:15 PM, TerryP bigboss1...@gmail.com wrote:
 On Oct 11, 3:42 pm, Esmail ebo...@hotmail.com wrote:
 cool .. I hadn't seen that. Not working quite at the 'pythonic' level yet
 I am not sure I think it's more readable that the if statement. Also, curious
 if the dictionary approach is more efficient.


 Somehow I doubt that Look up X in dictionary D could ever be more
 efficient (in terms of space and time, at least) then Check if X is
 equal to Y. It's not about what you get in runtime but what you get
 in monkey time.


 Most expressions that would make someone reach for a C-like switch()
 statement can be expressed with dictionaries or attributes instead.

 Here is a dorks approach to calling a specific function with arguments
 based on a command:

  args = re.split('\s', line)
  cmd  = args.pop(0)

  if cmd == ham:
      ...(args)
  elif cmd == spam:
      ...(args)
  elif cmd == eggs:
      ...(args)
  else:
      raise SyntaxWarning(Syntax error in above program)

 Here is more of a look up table approach:

 Note: let Commands be a dictionary, such that { ham : ...,
 spam : ..., eggs : ... }.

  args = re.split('\s', line)
  cmd  = args.pop(0)

  if cmd in Commands:
      Commands[cmd](args)
  else:
      raise SyntaxWarning(Syntax error in above program)



I'll often do that this way:

args = re.split('\s', line)
cmd  = args.pop(0)

def no_cmd(*a, **b):
raise SyntaxWarning(Syntax error in above program)

Commands.get(cmd, no_cmd)(args)



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


Re: Persistent Distributed Objects

2009-10-11 Thread Simon Forman
On Sun, Oct 11, 2009 at 12:46 PM, John Haggerty bouncy...@gmail.com wrote:
 Does pyro work inside of stackless?

I have no idea, but you wouldn't need both.  Only one or the other.

~Simon


 On Sat, Oct 10, 2009 at 9:54 AM, Simon Forman sajmik...@gmail.com wrote:

 On Fri, Oct 9, 2009 at 1:11 AM, John Haggerty bouncy...@gmail.com wrote:
  I am interested in seeing how it would be possible in python to have
  persistent objects (basically be able to save objects midway through a
  computation, etc) and do so across multiple computers.
 
  Something that would allow for memory, disk space, processing power, etc
  to
  be distributed across the nodes not just for number crunching.
 
  So for example a warehouse program started up on 3 machines one for
  adding
  orders, one for searching for orders, one for organizing the warehouse
  and
  them running on a different machine with a single interface.
 
  I've seen evidence about this being done wrt what looks like insanely
  complex stuff on this list but I'm wondering if there is something to do
  this with any number of nodes and just farm out random classes/objects
  to
  them?


 Check out Pyro (Python Remote Objects): http://pyro.sourceforge.net/

 or perhaps Stackless Python http://www.stackless.com/ (Tasklets can be
 serialized and passed around.)
 --
 http://mail.python.org/mailman/listinfo/python-list


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


Re: Persistent Distributed Objects

2009-10-10 Thread Simon Forman
On Fri, Oct 9, 2009 at 1:11 AM, John Haggerty bouncy...@gmail.com wrote:
 I am interested in seeing how it would be possible in python to have
 persistent objects (basically be able to save objects midway through a
 computation, etc) and do so across multiple computers.

 Something that would allow for memory, disk space, processing power, etc to
 be distributed across the nodes not just for number crunching.

 So for example a warehouse program started up on 3 machines one for adding
 orders, one for searching for orders, one for organizing the warehouse and
 them running on a different machine with a single interface.

 I've seen evidence about this being done wrt what looks like insanely
 complex stuff on this list but I'm wondering if there is something to do
 this with any number of nodes and just farm out random classes/objects to
 them?


Check out Pyro (Python Remote Objects): http://pyro.sourceforge.net/

or perhaps Stackless Python http://www.stackless.com/ (Tasklets can be
serialized and passed around.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a better way to code variable number of return arguments?

2009-10-08 Thread Simon Forman
On Thu, Oct 8, 2009 at 7:14 PM, Dr. Phillip M. Feldman
pfeld...@verizon.net wrote:

 I'm amazed that this works.  I had not realized that

 x,y= [3,4]

 is equivalent to

 x= 3; y= 4

 Python is rather clever.

 Thanks!


Python is very clever:

 (a, b), c = (1, 2), 3
 a, b, c
(1, 2, 3)

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


Re: Object Relational Mappers are evil (a meditation)

2009-10-06 Thread Simon Forman
On Tue, Oct 6, 2009 at 2:00 AM, Carl Banks pavlovevide...@gmail.com wrote:
 On Oct 5, 7:25 am, Aaron Watters aaron.watt...@gmail.com wrote:
 This is a bit off topic except that many Python
 programmers seem to be allergic to typing SQL.

 RESOLVED:  Using ORMs leads lazy programmers
 to make bad database designs.  It's better to
 carefully design your database with no invisible
 means of support and there is no reason to not
 use SQL directly for this purpose.

 Yeah sure, whatever.  I'm sure a good programmer could use sql
 directly and produce a tighter, faster, better-performing application
 than an ORM-solution, same as you could use C to produce a tighter,
 faster, better-performing application than a pure Python one.

 No thanks, you can write your databases in database assembly language
 if you want, I'll stick to my ORM, tyvm.

 Isn't WordPress written in PHP?  Are ORMs even possible in PHP?  I can
 almost rationalize use of direct sql if the alternative is some
 hellspawn PHP ORM.


I recently had the uncomfortable task of picking an ORM to use in a
php project for a start up.

There's something called RedBean http://www.redbeanphp.com/ that's
actually pretty effin sweet (IMHO).  Sweet enough that I'm considering
porting it to python.

It's more of a RAD tool than something you'd want to use in production.

As far as the OP rant goes, my $0.02:  bad programmers will write bad
code in any language, with any tool or system or environment they're
given.  If you want to avoid bad code there's (apparently) no
substitute for smrt programmers who are familiar with the tools
they're using, not just the syntax but the underlying conceptual
models as well.

That said, I feel ya bro'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Relational Mappers are evil (a meditation)

2009-10-05 Thread Simon Forman
On Mon, Oct 5, 2009 at 11:14 PM, Paul Rubin
http://phr...@nospam.invalid wrote:
 Steve Holden st...@holdenweb.com writes:
 It seems to me that the biggest sin in databases is a failure to use
 rigorous design techniques. If somebody doesn't understand relational
 theory

 Where does one find out about this?  I've somehow managed to avoid
 it for an awfully long time, but it begins to look useful.  Thanks.


Go to the source: The relational model for database management by E. F. Codd
http://portal.acm.org/citation.cfm?id=77708
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: defaults for function arguments bound only once(??)

2009-10-04 Thread Simon Forman
On Sun, Oct 4, 2009 at 2:29 AM, horos11 horo...@gmail.com wrote:
 All,

 Another one, this time a bit shorter.

 It looks like defaults for arguments are only bound once, and every
 subsequent call reuses the first reference created. Hence the
 following will print '[10,2]' instead of the expected '[1,2]'.

 Now my question - exactly why is 'default_me()' only called once, on
 the construction of the first object? And what's the best way to get
 around this if you want to have a default for an argument which so
 happens to be a reference or a new object?

This is a FAQ: 
http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects


  code begins here ---

 import copy
 class A:

    def default_me():
        return [1,2]

    def __init__(self, _arg=default_me()):
        self.arg = _a


 a = A()
 a.arg[0] = 10
 b = A()

 print b.arg  # prints [10,2]

Your code is weird:  you import copy module but don't use it; you
define default_me() as a sort of static method
(http://docs.python.org/library/functions.html#staticmethod) but then
only use it to generate a default argument that has nothing to do with
the class you just defined; and in __init__() you use _a which isn't
defined anywhere in this code snippet.

What are you actually trying to accomplish?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is pythonic version of scanf() or sscanf() planned?

2009-10-04 Thread Simon Forman
On Sun, Oct 4, 2009 at 5:29 AM, Martien Verbruggen
martien.verbrug...@invalid.see.sig wrote:
 On Sun, 4 Oct 2009 01:17:18 + (UTC),
        Grant Edwards inva...@invalid.invalid wrote:
 On 2009-10-03, ryniek90 rynie...@gmail.com wrote:

 So, whether it is or has been planned the core Python
 implementation of *scanf()* ?

 One of the fist things I remember being taught as a C progrmmer
 was to never use scanf.  Programs that use scanf tend to fail
 in rather spectacular ways when presented with simple typos and
 other forms of unexpected input.

 That's right. One shouldn't use scanf() if the input is unpredictable
 osr comes from people, because the pitfalls are many and hard to avoid.
 However, for input that is formatted, scanf() is perfectly fine, and
 nice and fast.

 fgets() with sscanf() is better to control if your input is not as
 guaranteed.

 Given the bad behavior and general fragility of scanf(), I
 doubt there's much demand for something equally broken for
 Python.

 scanf() is not broken. It's just hard to use correctly for unpredictable
 input.

 Having something equivalent in Python would be nice where most or all of
 your input is numerical, i.e. floats or integers. Using the re module,
 or splitting and converting everything with int() or float() slows down
 your program rather spectacularly. If those conversions could be done
 internally, and before storing the input as Python strings, the speed
 improvements could be significant.

 There is too much storing, splitting, regex matching and converting
 going on if you need to read numerical data from columns in a file.
 scanf() and friends make this sort of task rather quick and easy.

 For example, if your data is the output of a numerical analysis program
 or data coming from a set of measuring probes, it often takes the form
 of one or more columns of numbers, and there can be many of them. If you
 want to take one of these output files, and transform the data, Python
 can be terribly slow.

 It doesn't have to be scanf(), but something that would allow the direct
 reading of text input as numerical data would be nice.

 On the other hand, if something really needs to be fast, I generally
 write it in C anyway :)

 Martien

I haven't tried it but couldn't you use scanf from ctypes?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating class objects inside methods

2009-10-04 Thread Simon Forman
On Sun, Oct 4, 2009 at 1:12 AM, horos11 horo...@gmail.com wrote:

  a

 __main__.Myclass instance at 0x95cd3ec b

 __main__.Myclass instance at 0x95cd5ac

 What's the problem?

 Like I said, the code was a sample of what I was trying to do, not the
 entire thing.. I just wanted to see if the metaphor was kosher.

Right, but it doesn't help if you post code that doesn't actually
display the unexpected behaviour you're asking about.

 It sounds to me from your answer that this is unexpected behavior, so
 I'll go ahead and post the whole thing. My guess is that it is a
 python bug..

/What/ is unexpected behaviour?  I ran the code you posted and worked
exactly like I expected.

Posting the whole thing is better than posting code that doesn't
display the issue, but this is too long.  I'd ask you to repost a
minimal version that actually displays the problem you're asking about
but I see from the rest of this thread that you've already figured it
out.

Related to that, two points.

Learn python AS python, don't get caught up in what it does
differently than other languages.  IMHO it's hands-down the most
useful. productive language out there for a tremendous number of
problem domains.  Treat yourself to it. ;]

And second, please don't /teach/ python until you've learned it...

Regards,
~simon

 Run it (a simple puzzle game solved by breadth first search), and the
 first time state() is called inside the method, it calls __init__.
 Second time, and therafter, it calls __call__. I've highlighted where
 the code fails by putting a pdb.set_trace().

 Anyways, I've got a workaround (simply pass in any new objects needed
 from the caller), but it is truly annoying that python is either
 misleading or broken in this way.

 Attached find code, does not work vs. 2.6..


 Ed

 

 from collections import deque
 import copy
 import pdb

 class state:

    def default_board():

        return [
              [ 1, 'x', 'x', 0 ],
              [ 2, 2,  3,  4 ],
              [ 5, 6,  6,  7 ],
              [ 5, 6,  6,  7 ],
              [ 8, 9, 10, 10 ],
              [ 0, 'x', 'x', 0 ]
            ]

    def default_types():

        return {
                1  : [ 0, 0 ],
                2  : [ 0, 0, 0, 1 ],
                3  : [ 0, 0 ],
                4  : [ 0, 0 ],
                5  : [ 0, 0, 1, 0 ],
                6  : [ 0, 0, 1, 0, 0, 1, 1, 1 ],
                7  : [ 0, 0, 1, 0 ],
                8  : [ 0, 0 ],
                9  : [ 0, 0 ],
                10 : [ 0, 0, 0, 1 ]
            }

    def default_moves():

        return []

    def print_move(self, moveno, move):
        print str(moveno) + :  + str(move) + \n


    def __init__(self, _board=default_board(), _moves=default_moves(),
 _types=default_types()):

        self.board = _board
        self.moves = _moves
        self.types = _types

    def possible_moves(self):

        moves_so_far = set()
        moves_so_far.add('x')
        moves_so_far.add(0)
        ret = []
        for y_idx in range(0, len(self.board)):
            for x_idx in range(0, len(self.board[y_idx])):

                piece = self.board[y_idx][x_idx]

                if not piece in moves_so_far:

                    moves = self.legal_moves(y_idx, x_idx)
                    moves_so_far.add(piece)

                    if moves:
                        ret.extend(moves)

        return ret

    def is_answer(self):

        if self.board[5][3] == 1:
            return True
        else:
            return False

    def legal_moves(self, ycoord, xcoord):

        ret = []
        for dir in [ [ 0, 1 ], [ 0, -1 ], [ 1, 0 ], [ -1, 0 ] ]:
            ret.extend(self.addmove(dir[0], dir[1], ycoord, xcoord))

        return ret

    def empty(self, type, ycoord, xcoord, pieceno):

        for itr in range(0, len(type), 2):

            yy = type[itr]
            xx = type[itr+1]

            if not (len(self.board)  (yy+ycoord) = 0)  or not (len
 (self.board[yy+ycoord])  xx+xcoord = 0):
                return False

            if not self.board[yy+ycoord][xx+xcoord] in [ 0, pieceno ]:
                return False

        return True

    def addmove(self, ymult, xmult, ycoord, xcoord):

        ret = []
        pieceno = self.board[ycoord][xcoord]
        type    = self.types[pieceno]

        if xmult != 0:
            for xx in range(xcoord + xmult, -1 if xmult  0 else 4, -1
 if xmult  0 else 1):
 #               if xx == 0:
 #                   continue
                if self.empty(type, ycoord, xx, pieceno):
                    ret.append(self.newmove(ycoord, xcoord, ycoord,
 xx ))
                else:
                    break

        if ymult != 0:
            for yy in range(ycoord + ymult, -1 if ymult  0 else 6, -1
 if ymult  0 else 1):
 #               if yy == 0:
 #                   continue
                if self.empty(type, yy, xcoord, pieceno):
                    ret.append(self.newmove(ycoord, xcoord, yy,
 xcoord))
                else:
                

Re: creating class objects inside methods

2009-10-04 Thread Simon Forman
On Sun, Oct 4, 2009 at 2:44 PM, horos11 horo...@gmail.com wrote:


  Thanks for the info, but a couple of points:

      1. it wasn't meant to be production code, simply a way to teach
  python.

 Speaking as someone who does teach Python, Ew, no!  If you start by
 teaching people bad habits, every educator who comes along afterwards
 will curse your name.  That includes teaching yourself.

 --
 Rhodri James *-* Wildebeest Herder to the Masses

 No offense, but I disagree. By programming without regards to pre-
 existing style or convention I learned far more than I otherwise would
 have if I had simply mimicked someone else.

Don't teach newbies bad idiom.

 And I still think that unbridled assignment - especially assignment
 that can change the operational semantics of surrounding terms, at a
 distance no less - is a horrid thing.

That's opinion.

Python allows you to shoot yourself in the foot.  It's on us as
programmers to be able to grok our code well enough to prevent nasty
errors.  With great power comes great responsibility. and all that.

 It gets even worse because the way python handles assignment. To go
 back to my original program: why isn't the state variable that I
 defined local to that 'if' loop?

There's no such thing as an if loop.

The only answer to your question is that's the way it is...  or,
equivalently, that's the way Guido made it.

Check out Python Scopes and Name Spaces
http://www.python.org/doc/2.2/tut/node11.html#SECTION001120

 while len(dq):

    ...
    if curstate.is_answer():
        ...
    else:
        for state in ...


 The answer? Because you can't explicitly declare it. It therefore
 looks globally, finds the 'class state:' statement, and runs with it.
 I should be able to say:

    for my state in curstate.next_states():

 to show explicitly what I'm doing.

You can, you just have to be careful not to shadow some other name
in your namespace(s).  Again, python makes a trade off between
babysitting the programmer on the one hand vs. raw flexibility on
the other.

 Anyways, maybe I got off to a bad start, but I'm a bit leery of the
 language. In my estimation it's trying to be 'too clever by half', and
 this coming from a veteran bash/perl programmer. I mean, free form is
 one thing, but too much of a good thing can be harmful to your
 programming health. Maybe PyChecker or PyLint will help, I don't know.

I think if you let python seep into your thinking you'll eventually
come to love it.

It /IS/ very (too) clever, but the cleverness is (IMHO) spent on
getting out of your way, rather than trying to insure you're doing
things right.

Eric Raymond has a great article where he talks about learning python:
http://www.linuxjournal.com/article/3882

My second [surprise] came a couple of hours into the project, when I
noticed (allowing for pauses needed to look up new features in
Programming Python) I was generating working code nearly as fast as I
could type. When I realized this, I was quite startled. An important
measure of effort in coding is the frequency with which you write
something that doesn't actually match your mental representation of
the problem, and have to backtrack on realizing that what you just
typed won't actually tell the language to do what you're thinking. An
important measure of good language design is how rapidly the
percentage of missteps of this kind falls as you gain experience with
the language.

I can vouch for this: I can routinely write 200~300 lines of python
code and have it run flawlessly the first time.  This doesn't happen
every time, but it's the norm rather than the exception.


Give it a chance.  It's (IMHO) /beautiful/.


Happy hacking,
~Simon


 Ed

 (
 ps - an aside, but what was the rationale behind only displaying one
 error at a time on trying to run a script? I typically like to run a
 compilation phase inside my editor (vim), get a list of errors, and
 then go to each one and fix them.

I dunno the rationale, but the mechanism is uncaught exceptions
propagate up and halt the interpreter.  Since nothing keeps going
after an uncaught exception, there's nothing there to report the next
error.

 And how do you just check a script's syntax without running it
 anyways?

Just run it. ;]

FWIW, use the interactive interpreter (or IPython variant) to play
with the language until you learn the syntax well enough not to write
bad syntax.  For me anyway, that was the best/fastest way to learn it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating class objects inside methods

2009-10-03 Thread Simon Forman
On Sat, Oct 3, 2009 at 11:32 PM, horos11 horo...@gmail.com wrote:
 All,

 I've got a strange one..

 I'm trying to create a class object inside another class object by
 using the code template below (note.. this isn't the exact code.. I'm
 having difficulty reproducing it without posting the whole thing)

 Anyways, the upshot is that the first time the Myclass() constructor
 is called, the __init__ function gets executed.. The second time, it
 calls the '__call__' method (and dies, because I don't have a call
 method defined).

 To get around this, I've made __call__ a synonym of __init__, which is
 a horrid hack, and doesn't help me much (since I don't have a good
 idea what's going on).

 So - anyone have an idea of what's going on here? It looks like the
 second time, the Myclass() call is interpreted as a class instance,
 not a  class object, but that seems odd to me.. Is this a python bug?
 I'm seeing it in 2.6..If necessary, I can post the whole piece of
 code..

 Ed

 class Myclass:

    def __init__(self, b='default1', c='default2'):

        self.b = b;
        self.c = c;

    def function(self):

         other = Myclass();
         return(other)

 a = Myclass();

 b = a.function()
 --
 http://mail.python.org/mailman/listinfo/python-list



class Myclass:
def __init__(self, b='default1', c='default2'):
self.b = b
self.c = c

def function(self):
other = Myclass()
return other

a = Myclass()
b = a.function()


 a
__main__.Myclass instance at 0x95cd3ec
 b
__main__.Myclass instance at 0x95cd5ac


What's the problem?


(Also, you can leave out the ';' at the end of statements. And
'return' isn't a function, you can leave out the ()'s.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Python struct issue

2009-10-02 Thread Simon Forman
On Fri, Oct 2, 2009 at 12:07 PM, Carlo DiCelico
carlo.dicel...@gmail.com wrote:
 I saw an article on O'Reilly about using NumPy and Dislin to analyze
 and visualize WAV files. It's a really fantastic article but was a
 little out of date. I updated the script to work with the newer
 modules etc but am still having trouble getting it working.

 The line

 temp[i,:] = array(struct.unpack(%dB%(fft_length), tempb),Float) -
 128.0

 always returns the same error: Traceback (most recent call last):
  File pysono.py, line 31, in module
   temp[i,:] = array(struct.unpack(%dB%(fft_length),tempb),float) -
 128.0
 struct.error: unpack requires a string argument of length 256 when I
 do python pysono.py test.wav 256

 I'm sure it's probably something simple but I just can't see what it
 is!

 Here's the original code: 
 http://onlamp.com/python/2001/01/31/graphics/pysono.py

 Thanks!

In:

struct.unpack(%dB % (fft_length), tempb)

tempb is not length 256.

Also, note that (foo) is the same as just foo.  To create a tuple of
length 1 you must say (foo,)

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Opinions, please, on PEP 8 and local, 3rd party imports

2009-10-02 Thread Simon Forman
On Fri, Oct 2, 2009 at 3:50 PM, Philip Semanchuk phi...@semanchuk.com wrote:
 Hi all,
 Our project uses some libraries that were written by 3rd parties (i.e. not
 us). These libraries fit into a single Python file and live in our source
 tree alongside other modules we've written. When our app is distributed,
 they'll be included in the installation. In other words, they're not
 prerequisites that we'll make the user install, and they won't live in
 site-packages.

 For example, we use configobj from
 http://www.voidspace.org.uk/downloads/configobj.py, and I think Beautiful
 Soup can be installed as a single file too, although we're not using that
 particular module.

 PEP 8 http://www.python.org/dev/peps/pep-0008/ says the following:

   Imports should be grouped in the following order:
   1. standard library imports
   2. related third party imports
   3. local application/library specific imports


 I'm not sure in which category to place local, 3rd-party modules like
 configobj.

 Clearly, they belong in category 3 since they're local.

 Clearly, they also belong in category 2 since they're 3rd party modules, and
 explicitly labeling an imported module as this is code we didn't write is
 useful. But I've always reserved category 2  for external libraries (e.g.
 numpy, wxPython, Twisted, PIL, etc.).

 Clearly, the best choice is category 2.5?

 In your Pythonic opinion, should 3rd-party modules that live alongside
 homegrown code be listed in import category 2 or 3?

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


2.5 +1

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


Re: How different are a generator's send and next methods

2009-10-01 Thread Simon Forman
On Wed, Sep 30, 2009 at 4:24 PM, Andrey Fedorov anfedo...@gmail.com wrote:
 As far as I can tell, a generator's .next() is equivalent to .send(None). Is
 this true?

They are equivalent AFAIK.

 If so, [why] aren't they unified in a method with a single argument which 
 defaults
 to None?
 - Andrey

next() predates send().
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: iterate over list while changing it

2009-10-01 Thread Simon Forman
On Wed, Sep 30, 2009 at 11:19 PM, Daniel Stutzbach
dan...@stutzbachenterprises.com wrote:
 On Thu, Sep 24, 2009 at 3:32 PM, Torsten Mohr tm...@s.netic.de wrote:

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

 for i, x in enumerate(a):
    if x == 3:
        a.pop(i)
        continue

    if x == 4:
        a.push(88)

    print i, i, x, x

 I'd like to iterate over a list and change that list while iterating.
 I'd still like to work on all items in that list, which is not happening
 in the example above.

 I assume that by a.push you meant a.append.

 I believe this will do what you want:

 a = [1, 2, 3, 4, 5, 6]
 i = 0
 while i  len(a):
    x = a[i]
    if x == 3:
        a.pop(i)
    i += 1
        continue

    if x == 4:
        a.append(88)

    print i, i, x, x
    i += 1

 --
 Daniel Stutzbach, Ph.D.
 President, Stutzbach Enterprises, LLC



In the case of x == 3 when you remove an item from the list you should
not increment the index.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: hii

2009-10-01 Thread Simon Forman
On Thu, Oct 1, 2009 at 7:55 AM, padmapriya sekaran ppriy...@gmail.com wrote:
 I have a problem in using interp from numpy for which i need 3 array.
 my first array is
 x = scipy.linspace(0.009,0.53,100)

 and the other two array should be read from my file with 100x2 dimension
 (file1_lines) but it is read as string instead of two columns

 for line in file1_lines:
     line = line.split()
         xp = line[0]
         yp = line[1]
     y = scipy.interp(x,xp,yp)
 if i code like this it takes only one value for xp and yp but 100 values for
 x. i dont know what to do please help.

It is difficult to say without a sample of the file data.  If you
would please provide some?


Also, once you are sure the lines are being split correctly, you can just say:
xp, yp = line.split()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: emptying a list

2009-10-01 Thread Simon Forman
On Thu, Oct 1, 2009 at 11:30 AM, lallous lall...@lgwm.org wrote:
 Hello

 What is faster when clearing a list?

 del L[:]

 or

 L = []

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


The first form actually clears the list, the second for just re-binds
the name 'L' to a new, empty list.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Split string but ignore quotes

2009-09-29 Thread Simon Forman
On Tue, Sep 29, 2009 at 11:11 AM, Scooter slbent...@gmail.com wrote:
 I'm attempting to reformat an apache log file that was written with a
 custom output format. I'm attempting to get it to w3c format using a
 python script. The problem I'm having is the field-to-field matching.
 In my python code I'm using split with spaces as my delimiter. But it
 fails when it reaches the user agent because that field itself
 contains spaces. But that user agent is enclosed with double quotes.
 So is there a way to split on a certain delimiter but not to split
 within quoted words.

 i.e. a line might look like

 2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0;
 Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC
 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022) http://somehost.com 200
 1923 1360 31715 -
 --
 http://mail.python.org/mailman/listinfo/python-list


s = '''2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0;
Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0;
.NET CLR 3.0.04506; .NET CLR 3.5.21022) http://somehost.com 200 1923
1360 31715 -'''


initial, user_agent, trailing = s.split('')

# Then depending on what you want to do with them...
foo = initial.split() + [user_agent] + trailing.split()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multidimensional arrays/lists

2009-09-28 Thread Simon Forman
On Mon, Sep 28, 2009 at 3:27 PM, Ethan Furman et...@stoneleaf.us wrote:
 Simon Forman wrote:

 On Sun, Sep 27, 2009 at 12:40 PM, Someone Something
 fordhai...@gmail.com wrote:

 I'm trying to write a little tic-tac-toe program I need a array/list such
 that I can represent the tic tac toe board with an x axis and y axis and
 i
 can access each square to find out whether there is an X or an O. I have
 absolutely no idea how to do this in python and I really, really, don't
 want
 to do this is C.

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




 You can use tuples as keys in a dict

 R = range(3)

 board = {}

 # Initialize the board.
 for x in R:
    for y in R:
        board[x, y] = 'O'


 def board_to_string(b):
    return '\n'.join(
               ' | '.join(b[x, y] for x in R)
               for y in R
               )



 print board_to_string(board)

 O | O | O
 O | O | O
 O | O | O


 board[0, 1] = 'X'
 print board_to_string(board)

 O | O | O
 X | O | O
 O | O | O

 You might not want to start out with 'O' already being everywhere, though.
  ;-)

 ~Ethan~


D'oh!  I really do need to get more sleep.  ;P

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


Re: Repeated output when logging exceptions

2009-09-28 Thread Simon Forman
On Mon, Sep 28, 2009 at 4:38 PM, John Gordon gor...@panix.com wrote:
 In 6bce12c3-f2d9-450c-89ee-afa4f21d5...@h30g2000vbr.googlegroups.com Vinay 
 Sajip vinay_sa...@yahoo.co.uk writes:

 The logging package allows you to add tracebacks to your logs by using
 the exception() method, which logs an ERROR with a traceback and is
 specifically intended for use from within exception handlers. All that
 stuff with temporary files seems completely unnecessary, even with Python
 2.3.

 I didn't know about the exception() method.  Thanks!

 In general, avoid adding handlers more than once - which you are
 almost guaranteed to not avoid if you do this kind of processing in a
 constructor. If you write your applications without using the
 exceptionLogger class (not really needed, since logging exceptions
 with tracebacks is part and parcel of the logging package's
 functionality since its introduction with Python 2.3), what
 functionality do you lose?

 I'm trying to encapsulate all the setup work that must be done before any
 actual logging occurs: setting the debugging level, choosing the output
 filename, declaring a format string, adding the handler, etc.

 If I didn't do all that in a class, where would I do it?

Put it in a module.  :]


 --
 John Gordon                   A is for Amy, who fell down the stairs
 gor...@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, The Gashlycrumb Tinies

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


Re: Business issues regarding adapting Python

2009-09-27 Thread Simon Forman
On Sun, Sep 27, 2009 at 10:48 AM, Nash nasrul...@gmail.com wrote:
 On Sep 27, 4:13 pm, Martin P. Hellwig martin.hell...@dcuktec.org
 wrote:
 Nash wrote:

 cut can't get enough Python Developers
 I think normal market rules will apply to Pakistan too, if your desired
 trade has not the quantity you wish, the price per item should get
 higher. Net result should be that more quantity will be available due to
 increased interest.

 --
 MPHhttp://blog.dcuktec.com
 'If consumed, best digested with added seasoning to own preference.'

 If I rephrase the question: In an absense of steady Python Developers;
 can there be a viable strategy involving training? Or will it be much
 safer going with an already common developer pool.

 Please note that my goal is not to promote python but to make a sound
 business decision. Using Python is not an absolute requirement.

 I appreciate all the feedback thus far, please keep it coming in,
 thanks everyone!
 --
 http://mail.python.org/mailman/listinfo/python-list


Hire good programmers, they can pick up python rapidly.

It's widely acknowledged that hiring good people is a (the?) crucial
factor in the success of programming endeavors.  Good programmers,
almost by definition, will be able to handle learning python without
problems.

Just my $0.02,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Detecting changes to a dict

2009-09-27 Thread Simon Forman
On Sun, Sep 27, 2009 at 5:36 AM, Steven D'Aprano
st...@remove-this-cybersource.com.au wrote:
 I'm pretty sure the answer to this is No, but I thought I'd ask just in
 case...

 Is there a fast way to see that a dict has been modified? I don't care
 what the modifications are, I just want to know if it has been changed,
 where changed means a key has been added, or deleted, or a value has
 been set. (Modifications to mutable values aren't important.) In other
 words, any of these methods count as modifying the dict:

 __setitem__
 __delitem__
 clear
 pop
 popitem
 setdefault
 update

 Of course I can subclass dict to do this, but if there's an existing way,
 that would be better.


 --
 Steven


Depending on what you're doing you could use something like this:

(Note that it doesn't work on empty dicts, and you'd have to reset
it if your dict ever became empty after processing.)

def f(d):
while True:
i = iter(d).next
try:
while True:
try:
i()
except RuntimeError:
yield True
break
else:
yield False
except StopIteration:
if not d:
break # else we'd enter an infinite loop.


In [1]: d = {23: 18}

In [2]: check = f(d).next

In [3]: check()
Out[3]: False

In [4]: d['cats'] = 'lol'

In [5]: check()
Out[5]: True

In [6]: check()
Out[6]: False

In [7]: d.clear()

In [8]: check()
Out[8]: True

In [9]: check()
---
StopIteration Traceback (most recent call last)

/home/sforman/ipython console in module()

StopIteration:



HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a method (similar to str() method in R) that can print the data structure in python?

2009-09-27 Thread Simon Forman
On Sun, Sep 27, 2009 at 12:14 PM, Peng Yu pengyu...@gmail.com wrote:
 On Sat, Sep 26, 2009 at 2:05 PM, Robert Kern robert.k...@gmail.com wrote:
 On 2009-09-26 09:32 AM, Peng Yu wrote:

 Hi,

 I am looking for a method in python that is similar to the function
 str() in R, if you are familiar with R,

 If you have no idea of R, what I want is to print the class
 information of an object and the values of its members. Overloading
 '__expr__' and '__repr__' then using 'print' can sort of do what I
 want. But not quite. For example, if I have a list of many elements, I
 don't want to print all the elements. R's str() function can
 automatically take care of this issue. It also has other advantages, I
 am wondering if there is something similar available in python?

 I use Armin Ronacher's pretty.py as a pluggable pretty-printer. You can plug
 into its logic to implement these kinds of tools.

  http://dev.pocoo.org/hg/sandbox/file/tip/pretty

 But I want an even simpler solution. I don't want the user to define
 __pretty__. Is there a tool that can automatically print the content
 of an object without defining such a member function like __pretty__.

 Regards,
 Peng

Have you examined the pprint module?

http://docs.python.org/library/pprint.html

The repr module might also be of interest:

http://docs.python.org/library/repr.html

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multidimensional arrays/lists

2009-09-27 Thread Simon Forman
On Sun, Sep 27, 2009 at 12:40 PM, Someone Something
fordhai...@gmail.com wrote:
 I'm trying to write a little tic-tac-toe program I need a array/list such
 that I can represent the tic tac toe board with an x axis and y axis and i
 can access each square to find out whether there is an X or an O. I have
 absolutely no idea how to do this in python and I really, really, don't want
 to do this is C.

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



You can use tuples as keys in a dict

R = range(3)

board = {}

# Initialize the board.
for x in R:
for y in R:
board[x, y] = 'O'


def board_to_string(b):
return '\n'.join(
   ' | '.join(b[x, y] for x in R)
   for y in R
   )


 print board_to_string(board)
O | O | O
O | O | O
O | O | O

 board[0, 1] = 'X'
 print board_to_string(board)
O | O | O
X | O | O
O | O | O
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Most active coroutine library project?

2009-09-25 Thread Simon Forman
On Fri, Sep 25, 2009 at 11:42 AM, Grant Edwards inva...@invalid.invalid wrote:
 On 2009-09-25, Piet van Oostrum p...@cs.uu.nl wrote:
 exar...@twistedmatrix.com (e) wrote:

e I specifically left out all yield statements in my version, since that's
e exactly the point here. :)  With real coroutines, they're not necessary 
-
e coroutine calls look just like any other call.  With Python's enhanced
e generators, they are.

 The first time I encountered coroutines was in Simula-67. Coroutine
 switching was certainly explicit there. IIRC, the keyword was resume.

 I'm not sure exactly what coroutine calls refers to, but the
 mis-feature in Python co-routines that's being discussed is
 the fact that you can only yeild/resume from the main coroutine
 function.

 You can't call a function that yields control back to the other
 coroutine(s).  By jumping through some hoops you can get the
 same effect, but it's not very intuitive and it sort of feels
 wrong that the main routine has to know ahead of time when
 calling a function whether that function might need to yield or
 not.

You mean a trampoline function?  I.e. you have to call into your
coroutines in a special main function that expects as part of the
yielded value(s) the next coroutine to pass control to, and your
coroutines all need to yield the next coroutine?

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


Re: Most active coroutine library project?

2009-09-25 Thread Simon Forman
On Fri, Sep 25, 2009 at 2:07 PM, Jason Tackaberry t...@urandom.ca wrote:
 On Fri, 2009-09-25 at 15:42 +, Grant Edwards wrote:
 You can't call a function that yields control back to the other
 coroutine(s).  By jumping through some hoops you can get the
 same effect, but it's not very intuitive and it sort of feels
 wrong that the main routine has to know ahead of time when
 calling a function whether that function might need to yield or
 not.

 Not directly, but you can simulate this, or at least some pseudo form of
 it which is useful in practice.  I suppose you could call this jumping
 through some hoops, but from the point of view of the coroutine, it can
 be done completely transparently, managed by the coroutine scheduler.

 In kaa, which I mentioned earlier, this might look like:

        import kaa

       �...@kaa.coroutine()
        def task(name):
           for i in range(10):
              print name, i
              yield kaa.NotFinished  # kind of like a time slice

       �...@kaa.coroutine()
        def fetch_google():
           s = kaa.Socket()
           try:
              yield s.connect('google.com:80')
           except:
              print 'Connection failed'
              return
           yield s.write('GET / HTTP/1.1\nHost: google.com\n\n')
           yield (yield s.read())

       �...@kaa.coroutine()
        def orchestrate():
            task('apple')
            task('banana')
            page = yield fetch_google()
            print 'Fetched %d bytes' % len(page)

        orchestrate()
        kaa.main.run()


 The two task() coroutines spawned by orchestrate() continue to run in
 the background while any of the yields in fetch_google() are pending
 (waiting on some network resource).

 It's true that the yields in fetch_google() aren't yielding control
 _directly_ to one of the task() coroutines, but it _is_ doing so
 indirectly, via the coroutine scheduler, which runs inside the main
 loop.

 Cheers,
 Jason.




So Kaa is essentially implementing the trampoline function.

If I understand it correctly MyHDL does something similar (to
implement models of hardware components running concurrently.)
http://www.myhdl.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: flow control and nested loops

2009-09-25 Thread Simon Forman
On Fri, Sep 25, 2009 at 3:01 PM, kj no.em...@please.post wrote:


 In Perl, one can label loops for finer flow control.  For example:

 X: for my $x (@X) {
  Y: for my $y (@Y) {
    for my $z (@Z) {
      next X if test1($x, $y, $z);
      next Y if test2($x, $y, $z);
      frobnicate($x, $y, $z);
    }
    glortz($x, $y);
  }
  splat($x);
 }

 What's considered best practice in the Python world for this sort
 of situation?  The only approach I can think of requires setting
 up indicator variables that must be set and tested individually;
 e.g.

 for x in X:
    next_X = False
    for y in Y:
        next_Y = False
        for z in Z:
            if test1(x, y, z):
                next_X = True
                break
            if test2(x, y, z):
                next_Y = True
                break
            frobnicate(x, y, z)
        if next_X:
            break
        if next_Y:
            continue
        glortz(x, y)
    if next_X:
        continue
    splat(x)

 Whereas I find the Perl version reasonably readable, the Python
 one I find nearly incomprehensible.  In fact, I'm not even sure
 that the Python version faithfully replicates what the Perl one is
 doing!

 Is there a better approach?

 TIA!

 kynn


snarkThe best approach would be to reorganize your code so you
didn't have to do that./snark

Seriously though, I find both the perl and python versions
non-obvious.  You have had to use constructs like this in practice?

Generally, I would use flags in tricky nested loops just like you
did, perhaps with some comments to clarify things.   An alternative
might be to use custom exceptions.  Hopefully someone smarter than me
will come along and show an even better approach.


class NextX(Exception): pass
class NextY(Exception): pass


for x in X:
try:
for y in Y:
try:
for z in Z:

if test1(x, y, z):
raise NextX

if test2(x, y, z):
raise NextY

frobnicate(x, y, z)

except NextY:
continue

glortz(x, y)

except NextX:
continue

splat(x)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regex trouble

2009-09-24 Thread Simon Forman
On Thu, Sep 24, 2009 at 10:43 AM, Support Desk m...@ipglobal.net wrote:
 I am trying to loop over a dictionary  of phone numbers and using a python
 regex to determine if they are long distance or local and then adding them
 to their appropriate dictionary, My regex doesn't appear to be working
 though.

doesn't appear to be working is a useless way to characterize the
problem you're having.

Always tell us what you expected and what you got instead.  Preferably
with the actual traceback if any.

Also, post a /runnable/ code fragment. That way we can try it out for ourselves.


FWIW this problem is too simple (IMHO) for regular expressions.
Simply carve off the first three digits and check against sets of the
prefixes you're interested in:


#any number starting with these prefixes is not long distance
local_prefixes = set(['832', '877', '888', '713', '866', '011', '001',
'281', '800'])

long_distance= {}
for key1, value1 in x.iteritems():
if key1 == 'dest':
if value1[:3] not in local_prefixes:
long_distance[key1] = value1

HTH,
~Simon




 My regex's are these



 international__iregex=r'^1?(011|001)'  #Anything starting with these
 prefixes is International

 local__iregex=r'^1?(281|832|713|800)' #anything starting with these are
 local

 #long distance

 ld_regex=r'^1?(281|832|713|800|866|877|011|001|888)'  #any number not
 starting with these prefixes is long distance



 long_distance= {}

 My loop:

                 for key1,value1 in x.items():

                         if key1 == 'dest':

                                 if not re.search(ld_regex,value1):

                                         long_distance[key1] = value1

                                 print long_distance

Out of curiosity, why are you printing the whole dict for every key1
== 'dest' even when you're not modifying the output dict?




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


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


Re: iterate over list while changing it

2009-09-24 Thread Simon Forman
On Thu, Sep 24, 2009 at 4:32 PM, Torsten Mohr tm...@s.netic.de wrote:
 Hello,

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

 for i, x in enumerate(a):
    if x == 3:
        a.pop(i)
        continue

    if x == 4:
        a.push(88)

    print i, i, x, x

 I'd like to iterate over a list and change that list while iterating.

This is generally a bad idea, but you can do it if you're careful.

Let's check that enumerate() works when you append to the underlying list:

In [1]: n = range(3)

In [2]: for i, x in enumerate(n):
   ...: print i, '=', x
   ...: if i  3:
   ...: n.append(23)
   ...:
   ...:
0 = 0
1 = 1
2 = 2
3 = 23
4 = 23
5 = 23

So far so good.  But notice if you delete an item from the list while
iterating over the enumerate object the index returned will still be
incremented on the next iteration and you'll skip the item after the
one you just deleted:

In [1]: n = range(3)

In [2]: for i, x in enumerate(n):
   ...: print i, '=', x, '\t', n
   ...: if i == 1:
   ...: del n[i]
   ...:
   ...:
0 = 0  [0, 1, 2]
1 = 1  [0, 1, 2]

In [3]: n
Out[3]: [0, 2]

In [4]: n = range(6)

In [5]: for i, x in enumerate(n):
   ...: print i, '=', x, '\t', n
   ...: if i == 3:
   ...: del n[i]
   ...:
   ...:
0 = 0  [0, 1, 2, 3, 4, 5]
1 = 1  [0, 1, 2, 3, 4, 5]
2 = 2  [0, 1, 2, 3, 4, 5]
3 = 3  [0, 1, 2, 3, 4, 5]
4 = 5  [0, 1, 2, 4, 5]



 I'd still like to work on all items in that list, which is not happening
 in the example above.
 The conditions in the example are not real but much more complex
 in reality.

 Can anybody tell me how to do this?

You could manage the index yourself, like so:

def f(n):
i = 0
while True:
try:
x = n[i]
except IndexError:
break

print i, '=', x

if x == 3:
del n[i]
continue

if x == 4:
n.append(23)

# Increment the index
# only if you didn't
# delete anything during
# this iteration.
i += 1


N = range(6)
f(N)
print N


This prints:

0 = 0
1 = 1
2 = 2
3 = 3
3 = 4
4 = 5
5 = 23
[0, 1, 2, 4, 5, 23]


HTH,
~Simon


 Thanks for any hints,
 Torsten.


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

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


Re: Regex trouble

2009-09-24 Thread Simon Forman
On Thu, Sep 24, 2009 at 6:31 PM, Rhodri James
rho...@wildebst.demon.co.uk wrote:
 On Thu, 24 Sep 2009 19:45:37 +0100, Simon Forman sajmik...@gmail.com
 wrote:

 FWIW this problem is too simple (IMHO) for regular expressions.
 Simply carve off the first three digits and check against sets of the
 prefixes you're interested in:


 #any number starting with these prefixes is not long distance
 local_prefixes = set(['832', '877', '888', '713', '866', '011', '001',
 '281', '800'])

 long_distance= {}
 for key1, value1 in x.iteritems():
    if key1 == 'dest':
        if value1[:3] not in local_prefixes:
            long_distance[key1] = value1

 You need to allow for the potential leading 1, which can be done with
 a bit of straightforward string slicing but still puts REs more
 sensibly in the running.

 --
 Rhodri James *-* Wildebeest Herder to the Masses
 --
 http://mail.python.org/mailman/listinfo/python-list


Wow, I was looking right at it ('1?') and I missed it.  I need more sleep.


This is slightly less win:

for key1, value1 in x.iteritems():
   if key1 == 'dest':
   n = value1[0] == '1'
   if value1[n:n + 3] not in local_prefixes:
   long_distance[key1] = value1


(MRAB's comment about the foolishness of iterating through the dict
but testing for key == string_literal still applies, of course.)


Depending on what the OP's doing I might recommend just normalizing
the phone numbers before processing them, (i.e. stripping off the '1's
and possible breaking them up into tuples ('nnn', 'nnn', '').

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


Re: Most active coroutine library project?

2009-09-23 Thread Simon Forman
On Sun, Aug 23, 2009 at 11:02 AM, Phillip B Oldham
phillip.old...@gmail.com wrote:
 I've been taking a look at the multitude of coroutine libraries
 available for Python, but from the looks of the projects they all seem
 to be rather quiet. I'd like to pick one up to use on a current
 project but can't deduce which is the most popular/has the largest
 community.

 Libraries I looked at include: cogen, weightless, eventlet and
 circuits (which isn't exactly coroutine-based but it's event-driven
 model was intriguing).

 Firstly, are there any others I've missed? And what would the
 consensus be on the which has the most active community behind it?
 --
 http://mail.python.org/mailman/listinfo/python-list


Coroutines are built into the language.  There's a good talk about
them here: http://www.dabeaz.com/coroutines/

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2009-09-23 Thread Simon Forman
On Wed, Sep 23, 2009 at 1:14 PM, Rudolf yellowblueyel...@gmail.com wrote:
 Can someone tell me how to allocate single and multidimensional arrays
 in python. I looked online and it says to do the following x =
 ['1','2','3','4']

 However, I want a much larger array like a 100 elements, so I cant
 possibly do that. I want to allocate an array and then populate it
 using a for loop. Thanks for your help.
 --
 http://mail.python.org/mailman/listinfo/python-list


In python they're called 'lists'.  There are C-style array objects but
you don't want to use them unless you specifically have to.

You can create an empty list like so:

x = []

and then put items into it with a for loop like so:

for item in some_iterable:
x.append(item)

But in simple cases like this there's also the list comprehension
syntax which will do it all in one step:

x = [item for item in some_iterable]

But again in a case like this, if you're simply populating a list from
an iterable source you would just say:

x = list(some_iterable)

For multidimensional 'arrays' you just put lists in lists.  (Or use
NumPy if you really want arrays and are doing lots of wild processing
on them.)

But if you do this:

two_dimensional_list = [ [] for var in some_iterable]

The list of lists you create thereby will contain multiple references
to the /same/ inner list object.  This is something that catches many
people unawares.  You have to go back to using a for loop:

x = []
for n in range(100):
x.append([(n, m) for m in range(10)])

(That will create a list of one hundred lists, each of which contains
ten tuples.)

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


Re: arrays in python

2009-09-23 Thread Simon Forman
On Wed, Sep 23, 2009 at 1:22 PM, Donn donn.in...@gmail.com wrote:
 On Wednesday 23 September 2009 19:14:20 Rudolf wrote:
 I want to allocate an array and then populate it
 using a for loop.
 You don't need to allocate anything, just use the list or dictionary types.

 l=[] #empty list
 for x in range(1,500):
  l.append(x)


Of course, in this example you could just say,

l = range(1,500)

Or in python 3,

l = list(range(1,500))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Most active coroutine library project?

2009-09-23 Thread Simon Forman
On Wed, Sep 23, 2009 at 2:05 PM,  exar...@twistedmatrix.com wrote:
 On 05:00 pm, sajmik...@gmail.com wrote:

 On Sun, Aug 23, 2009 at 11:02 AM, Phillip B Oldham
 phillip.old...@gmail.com wrote:

 I've been taking a look at the multitude of coroutine libraries
 available for Python, but from the looks of the projects they all seem
 to be rather quiet. I'd like to pick one up to use on a current
 project but can't deduce which is the most popular/has the largest
 community.

 Libraries I looked at include: cogen, weightless, eventlet and
 circuits (which isn't exactly coroutine-based but it's event-driven
 model was intriguing).

 Firstly, are there any others I've missed? And what would the
 consensus be on the which has the most active community behind it?
 --
 http://mail.python.org/mailman/listinfo/python-list

 Coroutines are built into the language.  There's a good talk about
 them here: http://www.dabeaz.com/coroutines/

 But what some Python programmers call coroutines aren't really the same as
 what the programming community at large would call a coroutine.

 Jean-Paul

Really?  I'm curious as to the differences.  (I just skimmed the entry
for coroutines in Wikipedia and PEP 342, but I'm not fully
enlightened.)

Warm regards,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Searching Dictionary

2009-09-23 Thread Simon Forman
On Wed, Sep 23, 2009 at 2:31 PM, Support Desk
support.desk@gmail.com wrote:

 i am trying to search a large Python dictionary for a matching value. The
 results would need to be structured into a new dictionary with the same
 structure. Thanks.

 The structure is like this

 { Key : [{'item':value,'item2':value,'
 item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}],
 Key2 :
 [{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}],
 Key3 :
 [{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}]
 }

There is not really enough information to answer your query directly.
But I'll take a guess anyway.


First, let's put your in a piece of code that will actually run (which
is what you shourd have done in the first place..):



Key = Key2 = Key3 = value = 42

D = {
Key: [
{'item':value,
 'item2':value,
 'item3':value,
 'item4':value,
 'item5':value,
 'item6':value,
 'item7':value,
 'item8':value,
 'item9':value}
],
Key2: [
{'item':value,
 'item2':value,
 'item3':value,
 'item4':value,
 'item5':value,
 'item6':value,
 'item7':value,
 'item8':value,
 'item9':value}
],
Key3: [
{'item':value,
 'item2':value,
 'item3':value,
 'item4':value,
 'item5':value,
 'item6':value,
 'item7':value,
 'item8':value,
 'item9':value}
]
}

Now let's write a function that (maybe) does what you (kind of) specified:

def wtf(d, match_me):
result = {}
for key, value in d.iteritems():
inner_dict = value[0]
for inner_key, inner_value in inner_dict.iteritems():
if inner_value == match_me:
result[key] = [{inner_key: inner_value}]
return result


And let's try it:

print wtf(D, 42)

That prints:

{42: [{'item': 42}]}


Is that what you're asking for?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: arrays in python

2009-09-23 Thread Simon Forman
On Wed, Sep 23, 2009 at 10:03 PM, AggieDan04 danb...@yahoo.com wrote:
 On Sep 23, 3:02 pm, Simon Forman sajmik...@gmail.com wrote:
 On Wed, Sep 23, 2009 at 1:14 PM, Rudolf yellowblueyel...@gmail.com wrote:
  Can someone tell me how to allocate single and multidimensional arrays
  in python. I looked online and it says to do the following x =
  ['1','2','3','4']

  However, I want a much larger array like a 100 elements, so I cant
  possibly do that. I want to allocate an array and then populate it
  using a for loop. Thanks for your help.
  --
 http://mail.python.org/mailman/listinfo/python-list

 In python they're called 'lists'.  There are C-style array objects but
 you don't want to use them unless you specifically have to.
 ...
 But if you do this:

 two_dimensional_list = [ [] for var in some_iterable]

 The list of lists you create thereby will contain multiple references
 to the /same/ inner list object.

 No, that creates a list of distinct empty lists.  If you want multiple
 references to the same inner list, it's

 inner_list = []
 two_dimensional_list = [inner_list for var in some_iterable]

 or

 two_dimensional_list = [[]] * num_copies
 --
 http://mail.python.org/mailman/listinfo/python-list


Oh, you're right.  I was thinking of the [[]] * n form.  My bad.

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


Re: Dynamic Form

2009-09-22 Thread Simon Forman
On Tue, Sep 22, 2009 at 10:46 AM, Victor Subervi
victorsube...@gmail.com wrote:
 Hi;
 I have a dynamic form in which I do the following:
 1) Request two fields (company name, number of entries). That is sent back
 to the form.
 2) If the two fields are not None, the form requests other data. That, too,
 is sent back to the form.
 3) That new data is then entered into a MySQL table.
 The problem is, that when I go back to refresh the form, the data is
 re-entered into the table! How do I prevent that?
 TIA,
 Victor


First, this seems like it's not a python question, rather it's seems
to be about some web stuff.

Second, there's not enough information to tell you anything useful.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Read header and data from a binary file

2009-09-22 Thread Simon Forman
On Tue, Sep 22, 2009 at 4:30 PM, Jose Rafael Pacheco
jose_rafael_pach...@yahoo.es wrote:
 Hello,

 I want to read from a binary file called myaudio.dat
 Then I've tried the next code:

 import struct
 name = myaudio.dat
 f = open(name,'rb')
 f.seek(0)

Don't bother to seek(0) on a file you just opened.

 chain =  4s 4s I 4s I 20s I I i 4s I 67s s 4s I
 s = f.read(4*1+4*1+4*1+4*1+4*1+20*1+4*1+4*1+4*1+4*1+4*1+67*1+1+4*1+4*1)

Instead of calculating the size of the data represented by the format,
instead use the struct.calcsize() function

s = f.read(struct.calcsize(chain))

 a = struct.unpack(chain, s)
 header = {'identifier' : a[0],
           'cid'  : a[1],
           'clength'   : a[2],
   'hident' : a[3],
   'hcid32' : a[4],
   'hdate'  : a[5],
   'sampling' : a[6],
   'length_B'  : a[7],
   'max_cA'   : a[8],
   'max_cA1' : a[9],
   'identNOTE'  : a[10],
   'c2len'  : a[11],}

 It produces:

 {'length_B': 150001, 'sampling': 5, 'max_cA1': 'NOTE', 'hident': 'HEDR',
 'c2len': Normal Sustained Vowel 'A', Voice and Speech Lab., MEEI, Boston,
 MA, 'hdate': 'Jul 13 11:57:41 1994', 'identNOTE': 68, 'max_cA': -44076,
 'cid': 'DS16', 'hcid32': 32, 'identifier': 'FORM', 'clength': 300126}

 So far when I run f.tell()
f.tell()
 136L

 The audio data length is 300126, now I need a clue to build an array with
 the audio data (The Chunk SDA_), would it possible with struct?, any help ?

Read the chunk ID and length and then use the length to read the rest
of the chunk data.



 Thanks

 The file format is:


 Offset  |  Length |  Type |    Contents
 0   4    character     Identifier: FORM
 4      4        character     Chunk identifier: DS16
 8      4        integer         Chunk length
 12      -         -  Chunk data

 Header 2

 Offset       Length       Type       Contents
 0     4     character     Identifier: HEDR or HDR8
 4     4     integer     Chunk length (32)
 8     20     character     Date, e.g. May 26 23:57:43 1995
 28     4     integer     Sampling rate
 32     4     integer     Data length (bytes)
 36     2     unsigned integer     Maximum absolute value for channel A:
 0x if not defined
 38     2     unsigned integer     Maximum absolute value for channel A:
 0x if not defined

 NOTE Chunk

 Offset       Length       Type       Contents
 0     4     character     Identifier: NOTE
 4     4     integer     Chunk length
 8     -     character     Comment string

 SDA_, SD_A or SDAB Chunk
 Offset     Length     Type     Contents
 0     4     character     Identifier: SDA_, SD_B, or SDAB
 4     4     integer     Chunk length
 8     -     -     Data


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


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


Re: Searching a large dictionary

2009-09-22 Thread Simon Forman
On Tue, Sep 22, 2009 at 5:50 PM, Support Desk
support.desk@gmail.com wrote:
 I need help searching a large python dictionary. The dictionary is setup
 like so

 Key[{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}]

 Key2[{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}]

 Key3[{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}]


These look like you're indexing objects Key, Key2, Key3 by
dictionaries, or possibly these are the result of printing dict-like
objects with custom __repr__() methods.  Could you give more
information about what's actually going on here?

 What would be the best way to search for a specific value of item1 and add
 all the results to a new dictionary? Thanks

Searching a dict is accomplished by simply asking for the key:  d['item1']

I'm going to guess you meant something like this:

list_of_dicts = [Key, Key2, Key3]
new_dict = {}

for i, k in enumerate(list_of_dicts):
value = k['item1']
if value == some_specific_value:
new_dict[i] = value


But it's not really clear from your post exactly what you meant to ask.



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


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


Re: Searching a large dictionary

2009-09-22 Thread Simon Forman
On Tue, Sep 22, 2009 at 6:30 PM, Support Desk
support.desk@gmail.com wrote:
 Chris,  Yes that is the correct syntax, thanks


Okay, but correct syntax of what?  Help us help you.


 On Tue, Sep 22, 2009 at 5:22 PM, Chris Rebert c...@rebertia.com wrote:

 On Tue, Sep 22, 2009 at 2:50 PM, Support Desk
 support.desk@gmail.com wrote:
  I need help searching a large python dictionary. The dictionary is setup
  like so
 
 
  Key[{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}]
 
 
  Key2[{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}]
 
 
  Key3[{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}]

 That is not valid syntax. Do you mean you have a dictionary like this?:

 { Key :
 [{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}],
 Key2 :
 [{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}],
 Key3 :
 [{'item':value,'item2':value,'item3':value,'item4':value,'item5':value','item6':value,'item7':value,'item8':value,'item9':value}]
 }

 Cheers,
 Chris
 --
 http://blog.rebertia.com


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


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


Re: How to change string or number passed as argument?

2009-09-20 Thread Simon Forman
On Sep 19, 9:59 pm, Peng Yu pengyu...@gmail.com wrote:
 Hi,

 I know that strings or numbers are immutable when they passed as
 arguments to functions. But there are cases that I may want to change
 them in a function and propagate the effects outside the function. I
 could wrap them in a class, which I feel a little bit tedious. I am
 wondering what is the common practice for this problem.

 Regards,
 Peng

Python strings and numbers are always immutable, not just when passed
as arguments.

propagate the effects outside the function is a little vague.

You can return new data objects (like str.lower() etc.. do) or you can
wrap them in a namespace (a dict or class instance) or you can pass a
list object that contains the string or int or whatever, and your
functions can replace the values in the dict/instance/list.

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What are the naming convention for private member variable, and private and public member function?

2009-09-20 Thread Simon Forman
On Sep 19, 11:33 pm, Peng Yu pengyu...@gmail.com wrote:
 Hi,

 It says inhttp://www.python.org/dev/peps/pep-0008/

     Method Names and Instance Variables

       Use the function naming rules: lowercase with words separated by
       underscores as necessary to improve readability.

       Use one leading underscore only for non-public methods and
 instance
       variables.

 I am wondering what is the different between member function and
 member variable in term of naming convention.

 Regards,
 Peng

The naming convention for both to indicate that attributes of a class
should be considered internal is a leading underscore.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Comparison of parsers in python?

2009-09-20 Thread Simon Forman
On Sep 19, 9:34 pm, Peng Yu pengyu...@gmail.com wrote:
 On Sep 19, 6:05 pm, Robert Kern robert.k...@gmail.com wrote:

  Peng Yu wrote:
   Hi,

   I did a google search and found various parser in python that can be
   used to parse different files in various situation. I don't see a page
   that summarizes and compares all the available parsers in python, from
   simple and easy-to-use ones to complex and powerful ones.

  Second hit for python parser:

 http://nedbatchelder.com/text/python-parsers.html

 This is more a less just a list of parsers. I would like some detailed
 guidelines on which one to choose for various parsing problems.

 Regards,
 Peng

Check out http://navarra.ca/?p=538

(FWIW I like and use SPARK, but I haven't used or tried many others.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get the minimum number that can be represented?

2009-09-20 Thread Simon Forman
On Sep 20, 11:23 am, Peng Yu pengyu...@gmail.com wrote:
 On Sun, Sep 20, 2009 at 9:37 AM, Grant Edwards inva...@invalid.invalid 
 wrote:
  On 2009-09-20, Peng Yu pengyu...@gmail.com wrote:

  Suppose I want to define a function that return the minimum number
  that can be represented.

  def f(x):
    #body

  That it, if I call f(10), f will return the minimum integer that can
  be represented in the machine; if I cal f(10.5), f will return the
  minimum float that can be represented in the machine.

  Could somebody let me know what should be in the function body?

  The stuff you wan is in the sys module.

  For example:

  sys.float_info
  sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024,
  max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021,
  min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.2204460492503131e-16, 
  radix=2, rounds=1)

  sys.maxint
  2147483647

  You might also want to read up on the type() builtin

 I want avoid using any 'if' statement. In C++, I can use template. How
 to do not use 'if' statement in python?

 Regards,
 Peng

Python polymorphism is done differently than C++ templates.

Normally you would write your code to be as type agnostic as you
reasonable could.  In this case you actually desire to do different
things based on the type of the argument.  Either use an if statement
or you can dispatch on the type using a dict:


mins = {
float: 2.2250738585072014e-308,
int: 0,
long: 0,
}

def f(arg):
return mins[type(arg)]

(Hint: google for duck typing.)
HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why use locals()

2009-09-18 Thread Simon Forman
On Mon, Sep 14, 2009 at 1:12 AM, Sean DiZazzo half.ital...@gmail.com wrote:

 Thanks for your explanation Steven.  I see how it can be valuable, but
 it seems to always break the second rule of Zen.  I don't really want
 to get into the code of debuggers, but I guess I can see how they
 might have no other way to know what local variables have been set.

 ~Sean


The zeroth rule of zen is don't take the rules so seriously.
They're really more like guidelines anyway.

Regards,
~simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: detmining name during an assignment

2009-09-18 Thread Simon Forman
On Sep 18, 1:00 pm, Jamie Riotto jamie.rio...@gmail.com wrote:
 I have an app that uses Python scripting. When a user creates a new object:

 objName = newObject()

 I'd like the newObject be able to use objName as its internal name.
 So, if a user says:

 cube1 = Cube()

  A cube1 object should apear in the scene list. I believe this means I need
 to determine that a namespace assignment is going on from inside the
 object's init code
 or by using properties.

 I've searched the web but can't find a simple answer. Is there some
 way to use the inspect
 module get source code functoins? How would I get the code for the
 current interpreter line
 causing the init code to execute?

 Thanks very much in advance,
 Jamie Riotto

Save yourself a lot of trouble and just assign a name manually:

class foo:
def __init__(self, name):
self.name = name


some_foo = foo(some_foo)

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


Re: Urllib2 not working

2009-08-22 Thread Simon Forman
On Sat, Aug 22, 2009 at 2:12 PM, Carlos Fabian
Ramirezcarlosfabianrami...@gmail.com wrote:
 Hello,
 When I try to open a URL using urllib2.urlopen it returns Name or service
 not known. It is not a problem with my Internet I believe, since I have
 Internet access on my computer, and I have verified it is not a syntax, or
 spelling, error on my part. I have also tried accessing the site
 (google.com) by IP like so:
 urllib2.urlopen('http://74.125.67.100/')
 but this does not work either.
 This is a traceback of an attempt to access it through its URL
 (http://www.google.com/)
 http://dpaste.com/84016/
 Any help would be very appreciated.


Try googling the error message Name or service not known and urllib2.

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


Re: TypeError while checking for permissions with os.access() on windows xp

2009-08-21 Thread Simon Forman
On Aug 21, 1:33 pm, ryniek90 rynie...@gmail.com wrote:
 I've got some code that checks priviliges on two paths:
 First - chosen by user
 Second - hardcoded home directory represented by **os.getenv('HOME')** -
 (os.getenv('HOME') works both on Linux and Windows)

 Here's the code:
 
 def __check_set_perm(self, rd_obj_path, backup_dest):

         try:

             if os.path.exists(rd_obj_path):
                 if os.access(rd_obj_path, os.R_OK) != True:
                     print Have no permissions on [%s] for reading
 operation.\nTrying to set them... % os.path.split(rd_obj_path)[1]
                     if not os.path.isdir(rd_obj_path):
                         os.chmod(rd_obj_path, stat.S_IREAD)
                     else:
                         for root, dirs, files in os.walk(rd_obj_path):
                             for f in files:
                                 os.chmod(os.path.join(root, f),
 stat.S_IREAD)
                     print Get permissions for reading on [%s]
 successfully. % os.path.split(rd_obj_path)[1]
                 else:
                     print Have permissions on [%s] for reading. %
 os.path.split(rd_obj_path)[1]

                 if os.access(backup_dest, os.W_OK) != True:
                     print Have no permissions on [%s] for writing
 operation.\nTrying to set them... % os.path.split(backup_dest)[1]
                     os.chmod(backup_dest, stat.S_IWRITE)
                     print Get permissions for reading on [%s]
 successfully. % os.path.split(backup_dest)[1]
                 else:
                     print Have permissions on [%s] for writing. %
 os.path.split(backup_dest)[1]
             else:
                 return Can't find specified path - [%s]. % rd_obj_path
                 sys.exit(0)

         except (Except), ex:
             return ex
 

 This code is part of my backup script. When script checks for reading
 permission on 'user chosen path' it seems ok, but when it checks for
 writing permissions on 'home directory', (this line: **if
 os.access(backup_dest, os.W_OK) != True**), i get error:
 **TypeError: coercing to Unicode: need string or buffer, NoneType found**
 The second os.access check fails, but don't know why. When i turned
 os.W_OK to integer, i get the same error.
 But when changed os.W_OK or that int to string, i get error that integer
 is required. I'm launching this code on WinXP Prof+SP3, on Administrator
 account. Is it bug in os.acces() or my code is written wrong?
 Btw. i kow that os.chmod in windows can set only read-olny flag, so
 later i'll change the line with **os.chmod(path, stat.S_IWRITE)**

 Thanks and cheers.

First, some nitpicking: Include the whole traceback when posting about
errors please.  Don't write if some_boolean_expression != True:
instead prefer if not some_boolean_expression:.  Also, in your code
except (Except), ex: return ex the parentheses are redundant,
there's no Except Exception (unless you created one yourself) and
why are you catching and returning the Exception object?  (Also,
what's that sys.exit(0) doing in there? And why exit with 0 if there
was a problem? Exit code 0 means no problem.)


Second, from what you've said backup_dest must be None:

 import os
 os.access(None, os.W_OK)

Traceback (most recent call last):
  File pyshell#1, line 1, in module
os.access(None, os.W_OK)
TypeError: coercing to Unicode: need string or buffer, NoneType found

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


Re: Annoying octal notation

2009-08-20 Thread Simon Forman
On Aug 20, 3:06 pm, David 71da...@libero.it wrote:
 Hi all,

 Is there some magic to make the 2.x CPython interpreter to ignore the
 annoying octal notation?

No.  You would have to modify and recompile the interpreter. This is
not exactly trivial, see How to Change Python's Grammar
http://www.python.org/dev/peps/pep-0306/

However, see Integer Literal Support and Syntax 
http://www.python.org/dev/peps/pep-3127/

(Basically in 2.6 and onwards you can use 0oNNN notation.)

 I'd really like 012 to be 12 and not 10.

 If I want an octal I'll use oct()!

But that gives you a string, you're asking about literals.

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


Re: incrementing string/hex value from file and write back

2009-08-20 Thread Simon Forman
On Aug 20, 5:08 pm, Matthias Güntert matzeguent...@gmx.de wrote:
 Hello guys

 I would like to read a hex number from an ASCII file, increment it and
 write it back.
 How can this be performed?

 I have tried several approaches:

 my file serial.txt contains: 0C

 --
 f = open('serial.txt', 'r')
 val = f.read()
 val = val.encode('hex')
 print val
 --
 -- 3043

 --
 f = open('serial.txt', 'r')
 val = f.read()  
 print val
 val = val+1
 --
 -- TypeError: cannot concatenate 'str' and 'int' objects

 --
 f = open('serial.txt', 'rb')
 val = f.read()
 val = val + 1
 --
 -- TypeError: cannot concatenate 'str' and 'int' objects

 hm


Check this out:

In [1]: val = '0C'

In [2]: val.encode('hex')
Out[2]: '3043'

That's not what you want. Try this:

In [3]: int(val, 16)
Out[3]: 12

And to convert an int to a hex string.

In [4]: '%x' % 13
Out[4]: 'd'

The interpreter has a help() function that gives you quick access to
information about python objects:

 help(str.encode)
Help on method_descriptor:

encode(...)
S.encode([encoding[,errors]]) - object

Encodes S using the codec registered for encoding. encoding
defaults
to the default encoding. errors may be given to set a different
error
handling scheme. Default is 'strict' meaning that encoding errors
raise
a UnicodeEncodeError. Other possible values are 'ignore',
'replace' and
'xmlcharrefreplace' as well as any other name registered with
codecs.register_error that is able to handle UnicodeEncodeErrors.

 help(int)
Help on class int in module __builtin__:

class int(object)
 |  int(x[, base]) - integer
 |
 |  Convert a string or number to an integer, if possible.  A floating
point
 |  argument will be truncated towards zero (this does not include a
string
 |  representation of a floating point number!)  When converting a
string, use
 |  the optional base.  It is an error to supply a base when
converting a
 |  non-string. If the argument is outside the integer range a long
object
 |  will be returned instead.
 |
 |  Methods defined here:
 |
...


Unfortunately you can't use it on the '%' string formatting
operator...

 help(%)
SyntaxError: invalid syntax

So here's a link to the docs:
http://docs.python.org/library/stdtypes.html#string-formatting-operations


HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: incrementing string/hex value from file and write back

2009-08-20 Thread Simon Forman
On Aug 20, 5:18 pm, Rami Chowdhury rami.chowdh...@gmail.com wrote:
  val = val.encode('hex')

 That's the crucial line -- it's returning a new integer, which you are  
 re-binding to val. If you then did:

No, it returns another string, which still isn't the decimal
representation of the hex string.  hex C = decimal 12

In [1]: '0C'.encode('hex')
Out[1]: '3043'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recording input from USB port and write to text file

2009-08-19 Thread Simon Forman
On Aug 18, 7:33 pm, Allan af2...@gmail.com wrote:
 Hi! I'm fairly new to Python.  I understand the basics basics but I'm
 been trying to write a simple python code that will let me read input
 data (such as mouse movement) from my USB port and write it in a text
 file and I am so lost.  Can anyone help or direct me to some
 resources?  Thank you!

This isn't exactly what you were looking for, but maybe it will give
you some ideas:


from Tkinter import *

def printMotionEvents(event):
print mouse coords: %i x %i % (event.x, event.y)

t = Tk()
t.bind('Motion', printMotionEvents)
t.mainloop()


This script creates a little Tkinter window and then makes it so that
when you move the mouse over the window the mouse coordinates are
printed to stdout.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need cleanup advice for multiline string

2009-08-19 Thread Simon Forman
On Aug 19, 12:05 am, Ben Finney ben+pyt...@benfinney.id.au wrote:
 Simon Forman sajmik...@gmail.com writes:
  On Tue, Aug 18, 2009 at 8:42 PM, Ben Finneyben+pyt...@benfinney.id.au 
  wrote:
   We're all unified by our humanity. Bringing any god into the picture
   is surely counter to any goals of unity.

  Unity in humanity is, to my way of thinking, the same as Unity in
  God.

 Then you're playing Humpty-Dumpty games with words. You know very well
 that “God” has established meanings entirely different from “humanity”,
 and those meanings played a part in your choice of that word.

 I maintain that you can't consistently make a declaration in favour of
 human unity and unfounded religious assertions.

  I think Unity, like None, is a built-in singleton, so to speak.

 This is white noise.

   We are one family.

   Agreed.

  3

 I think we can peaceably leave it at that.

Hear hear!

(FWIW, I've always admired Humpty Dumpty's attitude to words.  Have
you ever read R.A. Wilson's Quantum Psychology?)

Regards,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: #elements of seq A in seq B

2009-08-19 Thread Simon Forman
On Aug 19, 8:17 pm, Jan Kaliszewski z...@chopin.edu.pl wrote:
 20-08-2009 o 01:19:24 Neal Becker ndbeck...@gmail.com wrote:

  What would be a time efficient way to count the number of occurrences of
  elements of sequence A in sequence B?  (in this particular case, these
  sequences are strings, if that matters).

 If you mean: to count occurences of each element of A (separately) in B...

 Hm, maybe something like this:

    # result as a dict {element of A: how many occurences in B, ...}
    dict((element, B.count(element)) for element in A)

 If you mean: to count non overlaping occurences of string A in B
 -- simply:

    B.count(A)

 Regards,
 *j

 --
 Jan Kaliszewski (zuo) z...@chopin.edu.pl

You don't want to use count() in a case like this because it iterates
through B len(A) times, i.e. this algorithm is O(A x B).

If all you want is the total you can do it like this:

def f(a, b):
a = set(a)
return sum(item in a for item in b)


A = 'some string'
B = 'another string'

print f(A, B) # prints 12


If you want to know the count for each element you can use this:

from collections import defaultdict

def g(a, b):
a = set(a)
d = defaultdict(int)
for item in b:
if item in a:
d[item] += 1
return d

print g(A, B)

# prints defaultdict(type 'int', {' ': 1, 'e': 1, 'g': 1, 'i': 1,
'o': 1, 'n': 2, 's': 1, 'r': 2, 't': 2})

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: #elements of seq A in seq B

2009-08-19 Thread Simon Forman
On Aug 19, 11:34 pm, John Machin sjmac...@lexicon.net wrote:
 On Aug 20, 12:12 pm, Simon Forman sajmik...@gmail.com wrote:

  On Aug 19, 8:17 pm, Jan Kaliszewski z...@chopin.edu.pl wrote:
   If you mean: to count non overlaping occurences of string A in B
   -- simply:

      B.count(A)

  You don't want to use count() in a case like this because it iterates
  through B len(A) times, i.e. this algorithm is O(A x B).

 There's seems to be multiple confusion here. Jan was talking about
 counting non-overlapping occurrences of string A in [string] B. In
 that case B.count(A) is nowhere near O(A*B)... in fact in reasonable
 cases it is sub-linear i.e. O(B/A)

 Seehttp://svn.python.org/view/python/trunk/Objects/stringlib/fastsearch

 And even a naive implementation of str.count() would not iterate
 through B len(A) times.

 and list.count() can't be used on its own to solve Neal's problem; its
 arg is a single element so it would have to be wrapped in a loop: sum
 (B.count(a) for a in A) which would of course be O(A*B)

First, thanks for pointing out str.count().  I didn't know of it and I
didn't notice that that was what was being used in Jan's code. (It's
blindingly obvious-- now that you've pointed it out. :] )


Second, I was referring to this:

   dict((element, B.count(element)) for element in A)

not B.count(A). Sorry for not interspersing my comment better.

Regards,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: comparing XML files to eachother

2009-08-18 Thread Simon Forman
On Mon, Aug 17, 2009 at 3:51 PM, David Brochubrochu...@gmail.com wrote:
 I need to compare one xml document to another to see if the content matches.
 Unfortunately, the formatting (spacing) and order of elements may change
 between files from run to run. I have looked into xml dom minidom but can't
 seem to find how to accomplish this. Does anyone know how I can do a compare
 between two XML documents using the STL?

It seems to me that you're going to have to normalize the documents
to be able to compare them.  (I.e. strip or ignore spacing
differences, sort the elements to be in the same order, etc..)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: define class over 2 files

2009-08-18 Thread Simon Forman
On Tue, Aug 18, 2009 at 1:57 AM, Steven
D'Apranoste...@remove.this.cybersource.com.au wrote:
 On Mon, 17 Aug 2009 21:45:57 -0700, naveen wrote:

 Is it possible to split up a class definition over multiple files?

 Not exactly, but you can do variations of this:


 In file A.py, create:

 class Parent:
    def method(self):
        return Method


 In file B.py, do this:

 import A
 class Child(B.parent):

class Child(A.Parent):
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need cleanup advice for multiline string

2009-08-18 Thread Simon Forman
On Tue, Aug 18, 2009 at 1:05 PM, Jean-Michel
Pichavantjeanmic...@sequans.com wrote:
 Steve Holden wrote:

 Robert Dailey:


 [...]


 It's a figure of speech. And besides, why would I want programming
 advice from a woman? lol. Thanks for the help.


 Sorry, Robert, simply not acceptable. Whether designed to be funny or
 not it's the kind of inane remark I would be really happy never to see
 again.

 The problem is that we can't just let these things go by all the
 time (even though we aren't discussing a major crime here). If we do
 that it encourages (at best) an atmosphere of complacency and a
 feeling that it's OK to demean people in Python forums. I'd really
 like to see those *not* get a hold.

 regards
  Steve


 Did you read the original post (this is an old one) ? Because quoting a joke
 out of its context is totally unfair.

Unfair to whom?

In any event, the context here seems to be a mixture of Guy Fawkes
trivia and general disapproval of sexist remarks on c.l.p, so I don't
see this comment as out of context.

(For the record, I use guys to refer to groups of people regardless
of their gender.  I also use dude to refer to people regardless of
their gender.  The only person who has taken offense to this is my
mom.  I am not a dude.  But she means she's not a cowboy, not not a
male.) (Uh, she is, in fact, female.)

 Anyway the hysteria that is surrounding this thread is just amazing. I'm
 waiting for more.

I don't feel hysterical, only appalled.

The OP used a common slang term, someone mentioned that the term
implies a group of males when in fact the group being addressed is
mixed gender, and the OP replied that it was just a figure of
speech.

So far, so good. But then the OP made a sexist joke.  He was called
on it and issued a rather gracious apology, but then he dropped
another sexist turd right in the middle of his otherwise model
apology.

I won't speak for anyone else, but I was appalled.


Sexism, racism, homophobia, religious intolerance, etc., all stem from
a fundamental forgetfulness of our Unity in God (as I would put it)
and this is perhaps the single greatest cause of human misery.

We are one family.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inheriting dictionary

2009-08-18 Thread Simon Forman
On Tue, Aug 18, 2009 at 6:08 PM, Simon Formansajmik...@gmail.com wrote:
 On Aug 18, 3:44 pm, Pavel Panchekha pavpanche...@gmail.com wrote:
 I want a dictionary that will transparently inherit from a parent
 dictionary. So, for example:

 
 a = InheritDict({1: one, 2: two, 4: four})
 b = InheritDict({3: three, 4: foobar}, inherit_from=a)

 a[1] # one
 a[4] # four
 b[1] # one
 b[3] # three
 b[4] # foobar
 

 I've written something like this in Python already, but I'm wondering
 if something like this already exists, preferably written in C, for
 speed.


 I would consider something like the following:


 a = {1: one, 2: two, 4: four}
 b = {3: three, 4: foobar}


 from functools import partial


 def getter(first, second, key):
    try:
        return first(key)
    except KeyError:
        return second(key)


 f = partial(getter, b.__getitem__, a.__getitem__)


 # Or, if you know you'll be getting a lot of KeyErrors, you
 # can use get() instead of __getitem__().

 def getter1(first, second, key, sentinel=None):
    item = first(key, sentinel)
    if item is sentinel:
        item = second(key)
    return item

 g = partial(getter1, b.get, a.__getitem__)


 HTH,
 ~Simon
 --
 http://mail.python.org/mailman/listinfo/python-list


Since you're going for speed the overhead of using partial (which
creates a wrapper object around the getter function) can be replaced
by something like this:

def makeGetter(first, second):
def getter(key):
try:
return first(key)
except KeyError:
return second(key)
return getter


f = makeGetter(b.__getitem__, a.__getitem__)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need cleanup advice for multiline string

2009-08-18 Thread Simon Forman
On Tue, Aug 18, 2009 at 8:03 PM, Steven
D'Apranost...@remove-this-cybersource.com.au wrote:
 On Tue, 18 Aug 2009 17:13:02 -0400, Simon Forman wrote:

 Sexism, racism, homophobia, religious intolerance, etc., all stem from a
 fundamental forgetfulness of our Unity in God (as I would put it) and

 Of the tens of thousands of Gods that people have invented, which is the
 one we're supposed to believe in? I always forget which ones we're
 supposed to dismiss as nonsense, and which one we're not.

Why the heck are you asking me?  (I'd say /you/ are the God you
should believe in.)

 this is perhaps the single greatest cause of human misery.

 Okay, we're Unified in God. Great. What does that actually mean, really?
 In the struggle to survive in a world of shortages, disease, natural
 disasters and disputes between well-meaning but incompatible viewpoints
 (to say nothing of the selfish and greedy), what practical difference
 does it make?

It means (to me, at least) that underlying all the BS and misery
there's a reason to hope, a reality the transcends this squalid
mud-ball Earth, and can overcome it completely if tapped.

It's more than a symbol with meaning, Unity is a phenomenon that can
be lived, experienced. It can be practiced and deepened.  Done well,
the practical difference it makes defies measurement.

(That's why calling folks on sexist behavior is important enough to
butt in on a thread on usenet with an OT reprimand. IMHO)

Regards,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need cleanup advice for multiline string

2009-08-18 Thread Simon Forman
On Tue, Aug 18, 2009 at 8:42 PM, Ben Finneyben+pyt...@benfinney.id.au wrote:
 Simon Forman sajmik...@gmail.com writes:

 Sexism, racism, homophobia, religious intolerance, etc., all stem from
 a fundamental forgetfulness of our Unity in God (as I would put it)

 It seems odd, for someone who cites religious intolerance as a problem,
 to then assert an extremely divisive religious idea.

Well, I did say, (as I would put it) to try to cushion the blow.

 We're all unified by our humanity. Bringing any god into the picture is
 surely counter to any goals of unity.

Unity in humanity is, to my way of thinking, the same as Unity in
God.  I think Unity, like None, is a built-in singleton, so to speak.

 We are one family.

 Agreed.

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


Re: problem with interface of operator.itemgetter

2009-08-16 Thread Simon Forman
On Thu, Aug 13, 2009 at 11:16 PM, dou dounirvana...@gmail.com wrote:
 I have a function to do some thing like LEFT JOIN in SQL, the function use
 the itemgetter to get the ON and SELECT parameters of the two table(list
 of list), the problem is that itemgetter may return a value or a tuple of
 values, because of the inconsistent return type of itemgetter, I have to do
 2^3 IF check,
 the function like this:

 def left_join(table1, on_index1, table2, on_index2, getter1, getter2):
     do thing like:
     SELECT on1(table1), g1(table1), g2(table2) FROM table1
     LEFT JOIN table2
     ON on1(table1) == on2(table2)
     
     on1 = itemgetter(*on_index1)
     on2 = itemgetter(*on_index2)
     g1 = itemgetter(*getter1)
     g2 = itemgetter(*getter2)

     d2 = {}
     d2 = dict((on2(r2), r2) for r2 in table2)

     #if itemgetter always return tuple, below could simple as one line
     #return [ list(on1(r1) + g1(r1) + g2(d2.get(on1(r1 for r1 in table1
 ]

     len_on = len(on_index1)
     len_g1 = len(getter1)
     len_g2 = len(getter2)

     if len_on == 1:
     if len_g1 == 1 and len_g2 == 1:
     return [ [on1(r1), g1(r1), g2(d2.get(on1(r1)))] for r1 in table1
 ]
     elif len_g1 == 1 and len_g2  1:
     return [ list((on1(r1),g1(r1))+g2(d2.get(on1(r1 for r1 in
 table1 ]
     elif len_g1  1 and len_g2 == 1:
     return [ list((on1(r1),)+g1(r1)+(g2(d2.get(on1(r1))),))  for r1
 in table1 ]
     else: #len_g1  1 and len_g2  1:
     return [ list((on1(r1),)+g1(r1)+g2(d2.get(on1(r1 for r1 in
 table1 ]
     else: # len_on  1
     if len_g1 == 1 and len_g2 == 1:
     return [ list(on1(r1))+[g1(r1),g2(d2.get(on1(r1)))] for r1 in
 table1 ]
     elif len_g1 == 1 and len_g2  1:
     return [ list(on1(r1)+(g1(r1),)+g2(d2.get(on1(r1 for r1 in
 table1 ]
     elif len_g1  1 and len_g2 == 1:
     return [ list(on1(r1)+g1(r1)+(g2(d2.get(on1(r1))),))  for r1 in
 table1 ]
     else: #len_g1  1 and len_g2  1:
     return [ list(on1(r1)+g1(r1)+g2(d2.get(on1(r1 for r1 in
 table1 ]

 so is there a way to force itemgetter to return tuple even when
 itemgetter(only_have_one_argument)? or some other function to do this?

 Thanks.



You can use a little helper function to create your itemgetter like this:

def makeItemGetter(indexes):
   I = itemgetter(*indexes)
   if len(indexes)  1:
   return I
   return lambda thing: (I(thing),)

If indexes contains only one index the itemgetter is wrapped in a
lambda that turns its output into a tuple.

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting a string into substrings of equal size

2009-08-16 Thread Simon Forman
On Aug 14, 8:22 pm, candide cand...@free.invalid wrote:
 Suppose you need to split a string into substrings of a given size (except
 possibly the last substring). I make the hypothesis the first slice is at the
 end of the string.
 A typical example is provided by formatting a decimal string with thousands
 separator.

 What is the pythonic way to do this ?

 For my part, i reach to this rather complicated code:

 # --

 def comaSep(z,k=3, sep=','):
     z=z[::-1]
     x=[z[k*i:k*(i+1)][::-1] for i in range(1+(len(z)-1)/k)][::-1]
     return sep.join(x)

 # Test
 for z in [75096042068045, 509, 12024, 7, 2009]:
     print z+ -- , comaSep(z)

 # --

 outputting :

 75096042068045 --  75,096,042,068,045
 509 --  509
 12024 --  12,024
 7 --  7
 2009 --  2,009

 Thanks

FWIW:

def chunks(s, length=3):
stop = len(s)
start = stop - length
while start  0:
yield s[start:stop]
stop, start = start, start - length
yield s[:stop]


s = '1234567890'
print ','.join(reversed(list(chunks(s
# prints '1,234,567,890'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need cleanup advice for multiline string

2009-08-12 Thread Simon Forman
On Aug 12, 10:41 am, Robert Dailey rcdai...@gmail.com wrote:
 On Aug 12, 9:09 am, exar...@twistedmatrix.com wrote:



  On 01:27 pm, jeanmic...@sequans.com wrote:

  Simon Brunning wrote:
  2009/8/11 Robert Dailey rcdai...@gmail.com:
  On Aug 11, 3:40 pm, Bearophile bearophileh...@lycos.com wrote:
  There are gals too here.
  It's a figure of speech. And besides, why would I want programming
  advice from a woman? lol. Thanks for the help.

  Give the attitudes still prevalent in our industry (cf
  http://tinyurl.com/c5nqju and many more), I'm sorry to say that I
  don't think this is funny.
  Having someone present technical informations with porn content cannot
  be qualified as prevalent in our industry. I would even dare to say
  this is the opposite, it is almost unique.
  I would also add that Robert was very far from this attitude, I
  consider his joke as a friendly tickle, not a male chauvinist
  aggression. I'm no women, but I'm sure they are as capable as me, not
  to say more, of making the distinction.

  It has been said this list is not very friendly to newbies, let's not
  make it hostile to gentle jokes (even those not funny) when thanking
  helpers.

  It's lots of little things like this which combine to create an
  environment which is less friendly towards women than it is towards
  others.  You might read it as a joke, others might not.  Even if it is a
  joke, it's in poor taste and doesn't really belong on python-list.

  There's a difference between pointing out inappropriate behavior and
  being unfriendly.  Hopefully Robert got help with his problem.  That's
  what the list is here for.  Having accomplished that, it is not
  unfriendly to ask him not to make disparaging comments, jokes or
  otherwise, about groups of people.

  Jean-Paul

 Hey everyone,

 I was actually joking about my remark, I was making fun of the fact
 that Bearophile took my figure of speech literally. I have worked with
 a lot of women in the past and they even use guys to refer to
 everyone in a room (When there were obviously other females in that
 room as well).

 On a more serious note, I do apologize to those offended by my remark.
 I realize that these things can be a touchy subject for some people. I
 expected more of a laid-back attitude from everyone. No need to be so
 serious all the time. I cannot completely doubt that there are logical
 women out there. I just haven't seen one yet. But that doesn't mean
 I'm a sexist.

Oh my.  And you were doing so well.  You haven't seen a logical
woman?  Perhaps you're blind because your eyes were torn out by a
raging marmoset?

Guess what?  Thinking (or just saying) that /does/ mean you're a
sexist.  (Even if it was just another friendly, harmless joke.)




 With my apology presented, I would like to propose that we end the
 discussion here. As I said, this is a very sensitive subject and this
 thread could spin way out of control if we don't just ignore the
 issue. For those that took it as a friendly, harmless joke, hopefully
 you had a laugh. For those that took it seriously or as an offense,
 please take my apology to heart. Thanks once again to everyone for
 your help. I've long been a member of this community and I really
 appreciate the continuous support I've been receiving!

 Take care everyone!

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


Re: Search and write to .txt file

2009-08-11 Thread Simon Forman
On Aug 11, 7:22 am, Helvin helvin...@gmail.com wrote:
 Hi everyone,

 I am writing some python script that should find a line which contains
 '1' in the data.txt file, then be able to move a certain number of
 lines down, before replacing a line. At the moment, I am able to find
 the line '1', but when I use f.seek to move, and then rewrite, what I
 write goes to the end of the .txt file, instead of being adjusted by
 my f.seek.

 Do you know what way I should take?

 Data.txt is a file of 3 lines:
    line1
    line2
    line3

 Code:

    with open('data.txt', 'r+') as f:
        firstread = f.readlines()   # Take a snapshot of initial file

        f.seek(0,0)    # Go back to beginning and search
        for line in f:
            print line
            if line.find('1'):
                print 'line matched'
                f.seek(1,1)       # Move one space along
                f.write('house\n')     # f.write overwrites the exact
 number of bytes.
                break                    # leave loop once '1' is found

        f.seek(0,0)              # Go back to beginning, and read
 data.txt again
        lastread = f.readlines()

        print 'firstread is', firstread
        print 'lastread is', lastread

 This shouldn't be too difficult, but I don't know how.  
 Help appreciated!

There's a bug in this line:

if line.find('1'):

the string find() method returns an integer, which will be -1 if the
substring is not found. In python -1 is treated as True (only 0 is
False) so your if statement will always succeed unless '1' is the
first character on the line.

HTH,
~Simon


P.S. you can use the help() command in the python interpreter to get
docs on most things:

 help(str.find)

Help on method_descriptor:

find(...)
S.find(sub [,start [,end]]) - int

Return the lowest index in S where substring sub is found,
such that sub is contained within s[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.

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


Re: dictionary help

2009-08-11 Thread Simon Forman
On Aug 11, 11:51 am, MRAB pyt...@mrabarnett.plus.com wrote:
 Krishna Pacifici wrote:
  Thanks for the help.

  Actually this is part of a much larger project, but I have unfortunately
  pigeon-holed myself into needing to do these things without a whole lot
  of flexibility.

  To give a specific example I have the following dictionary where I need
  to remove values that are duplicated with other values and remove values
  that are duplicates of the keys, but still retain it as a dictionary.  
  Each value is itself a class with many attributes that I need to call
  later on in the program, but I cannot have duplicates because it would
  mess up some estimation part of my model.

  d =
  {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22:
  [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}

  So I want a new dictionary that would get rid of the duplicate values of
  21, 22, 36 and 20 and give me back a dictionary that looked like this:

  new_d=
  {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23,
  12, 32], 26: [25, 27, 16], 30: [40]}

  I understand that a dictionary may not be the best approach, but like I
  said I have sort of pigeon-holed myself by the way that I am simulating
  my data and the estimation model that I am using.  Any suggestions or
  comments about the above problem would be greatly appreciated.

   d = {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11,
 31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}
   new_d = {}
   seen = set(d.keys())
   for k, v in d.items():
 ...     new_d[k] = [x for x in v if x not in seen]
 ...     seen |= set(new_d[k])
 ...
   new_d
 {36: [35, 37, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12,
 32], 26: [25, 27, 16], 30: [40]}

Ha ha, MRAB beat me to it:

d = {
36: [35, 37, 26, 46],
75: [74, 76, 65, 85],
21: [20, 22, 11, 31],
22: [21, 23, 12, 32],
26: [25, 27, 16, 36],
30: [20, 31, 40],
}


new_d = { # Given, and apparently incorrect.
36: [35, 37, 26, 46], # 26 is a key and should be gone.
75: [74, 76, 65, 85],
21: [20, 11, 31],
22: [23, 12, 32],
26: [25, 27, 16],
30: [40],
}


expected = {
36: [35, 37, 46],
75: [74, 76, 65, 85],
21: [20, 11, 31],
22: [23, 12, 32],
26: [25, 27, 16],
30: [40],
}


def removeDuplicates(D):
'''
Remove values that are duplicated with other values
and remove values that are duplicates of the keys.

Assumes that values in the lists are already unique within
each list.  I.e. duplicates are only in the keys or in other
lists.

This function works in place on D, so it doesn't return
anything.  Caller must keep a reference to D.
'''

seen = set(D) # Get a set of the keys.

for key, values_list in D.iteritems():

# Filter out values that have already been seen.
filtered_values = [
value
for value in values_list
if not value in seen
]

# Remember newly seen values.
seen.update(filtered_values)

D[key] = filtered_values


## Example:
##
## d == expected
##False
## removeDuplicates(d)
## d == expected
##True
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unique-ifying a list

2009-08-09 Thread Simon Forman
On Aug 7, 4:53 pm, kj no.em...@please.post wrote:
 Suppose that x is some list.  To produce a version of the list with
 duplicate elements removed one could, I suppose, do this:

     x = list(set(x))

 but I expect that this will not preserve the original order of
 elements.

 I suppose that I could write something like

 def uniquify(items):
     seen = set()
     ret = []
     for i in items:
         if not i in seen:
             ret.append(i)
             seen.add(i)
     return ret

 But this seems to me like such a commonly needed operation that I
 find it hard to believe one would need to resort to such self-rolled
 solutions.  Isn't there some more standard (and hopefully more
 efficient, as in C-coded/built-in) approach?

 TIA!

 kynn


Unique items in a list, in the same order as in the list:

x = sorted(set(x), key=x.index)

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


Re: Very Strange Problem

2009-07-30 Thread Simon Forman
On Wed, Jul 29, 2009 at 3:10 PM, Omer Khalidomer.kha...@cern.ch wrote:
 Hi Dave,

 Thanks for your reply. I actually didn't cut and paste my code as it was
 dispersed in different places, i typed the logic behind my code in the email
 (and obiviously made some typos, indentations is some thing else) but my

Please, do not do  that.  It's very difficult to debug code that
hasn't been seen.  The code you posted has numerous problems (that
likely have nothing to do with your actual problem.)

If you're going to post code, try to recreate the issue with a small
runnable script.  If you can't do that, as it sounds like it would be
difficult in this case as your code is dispersed in different places,
at least post the relevant portions of the actual code.  Don't
re-type.

 real code does not have these problems as my application runs fine with out
 errors...

 Except that the line where i want to update the value doesn't get updated
 and no exception is thrown. What's surprising for me is that i am doing the
 same thing in hundreds of places in my 3k+ line code but by some reason this
 part doesn't work...

 As far as the global variables are concerned, i am using them in other
 places too and didn't see any problems.

 I think some thing else is going on here as the statement above and below my
 modified lines get executed.

If the statements above and below that line(s) are executed, then that
line is certainly being executed as well.

Try introducing some additional print statements to verify your mental
model of what's happening:

# set it to 1
print jobs
print jobs[index]
print jobs[index]['v']
jobs[index]['v'] = 1
print jobs
print jobs[index]
print jobs[index]['v']
print Set to 1

 Is there a way in Python to debug memory address or to see where in memory
 this object is stored, and is there a lock on it or else?

You are barking up the wrong tree.  Somewhere in your code you're
doing something silly that's causing your issue.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: invoke method on many instances

2009-07-21 Thread Simon Forman
On Jul 20, 3:29 am, Gabriel Genellina gagsl-...@yahoo.com.ar
wrote:
 En Sat, 18 Jul 2009 12:31:46 -0300, Alan G Isaac alan.is...@gmail.com  
 escribió:

  On Fri, 17 Jul 2009 05:19:50 +, Alan G Isaac wrote:
  def apply2(itr, methodname, *args, **kwargs):
      f = operator.methodcaller(methodname, *args, **kwargs)
      for item in itr:
          f(item)

  On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote:
  for obj in objects:
      getattr(obj, methodname)(*args, **kwargs)

  Are there any obvious considerations in choosing
  between those two?

 The operator.methodcaller version is faster in my tests for large  
 collections, but slightly slower when you have very few elements.

 code
 import operator
 import timeit

 class X:
    def method(self, x, y, **kw): pass

 def apply1(itr, methodname, *args, **kwargs):
      for item in itr:
          getattr(item, methodname)(*args, **kwargs)

 def apply2(itr, methodname, *args, **kwargs):
      f = operator.methodcaller(methodname, *args, **kwargs)
      for item in itr:
          f(item)

 L=[X() for _ in range(3000)]
 apply1(L,'method', 1, 2, foo=3)
 apply2(L,'method', 1, 2, foo=3)

 timeit.timeit(setup=from __main__ import apply1,apply2,L,  
 stmt=apply1(L,'method', 1, 2, foo=3), number=1000)
 timeit.timeit(setup=from __main__ import apply1,apply2,L,  
 stmt=apply2(L,'method', 1, 2, foo=3), number=1000)
 /code

 --
 Gabriel Genellina

If your instances are all of the same class you can pass in the method
directly like so:

def apply3(iterator, method, *args, **kwargs):
for item in iterator:
method(item, *args, **kwargs)

class X:
   def method(self, x, y, **kw): pass

items = [X() for _ in range(10)]

apply3(items, X.method, 1, 2, foo=3)

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tough-to-explain Python

2009-07-21 Thread Simon Forman
On Mon, Jul 20, 2009 at 9:54 PM, Paul Rubinhttp://phr...@nospam.invalid wrote:
 Simon Forman sajmik...@gmail.com writes:
 But I'm glad it's there to study, these are wheels I don't have to
 invent for myself.

 http://dwheeler.com/essays/high-assurance-floss.html

 might be an interesting place to start.

OO la la!  Thank you!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help understanding the decisions *behind* python?

2009-07-21 Thread Simon Forman
On Jul 20, 12:27 pm, Phillip B Oldham phillip.old...@gmail.com
wrote:
 My colleagues and I have been working with python for around 6 months
 now, and while we love a lot of what python has done for us and what
 it enables us to do some of the decisions behind such certain
 data-types and their related methods baffle us slightly (when compared
 to the decisions made in other, similarly powerful languages).

 Specifically the differences between lists and tuples have us
 confused and have caused many discussions in the office. We
 understand that lists are mutable and tuples are not, but we're a
 little lost as to why the two were kept separate from the start. They
 both perform a very similar job as far as we can tell.

 Consider the following:

  x = [2,1,3]
  x.sort()
  print x

 [1, 2, 3]

 Now, if the sort operations were unable to affect the original
 structure of the list (as in JavaScript) you'd effectively have a
 tuple which you could add/remove from, and the example above would
 look more like:

  x = [2,1,3]
  print x.sort()
 [1, 2, 3]
  print x

 [2,1,3]

 This make a lot more sense to us, and follows the convention from
 other languages. It would also mean chaining methods to manipulate
 lists would be easier:

  x = [2,1,3]
  print x.sort()[0]
 3

(As others have already pointed out this would print 1, not 3.)

  print x

 [2,1,3]

 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.

In this case you can use:

 x = [2,1,3]
 print min(x)
1

(There's also a max() function.)

 We're not looking to start any arguments or religious wars and we're
 not asking that python be changed into something its not. We'd simply
 like to understand the decision behind the lists and tuple structures.
 We feel that in not getting the difference between the two types we
 may be missing out on using these data structures to their full
 potential.


One way to think about tuples (as distinct from lists) is as a short-
hand version of Pascal's 'record' type, or C's 'struct' (with the
caveats that the fields are not named, only indexed, and the types of
the fields are implied by use, not explicitly declared.)

(FWIW: http://en.wikipedia.org/wiki/Record_(computer_science) )

Broadly speaking, lists are useful for things like stacks and queues,
and sorting, while tuples are useful for aggregating heterogeneous
data into coherent units, and you can hash them (provided their
contents are also hashable.)

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order

2009-07-21 Thread Simon Forman
On Jul 21, 5:00 pm, davidj411 davidj...@gmail.com wrote:
 I am using a recursive function to print the time and a few other
 things on each pass. ( the function calculates size of file that is
 being transferred and if not 100 % copied, it waits 20 secs and checks
 again).

 i would expect the time to be correct anytime it is used:

 --code below --
 print time.strftime('%m-%d-%Y %H:%m:%S')
 --code above --

 here is an example of what i am seeing:

 16:07:16
 16:07:36
 16:07:56
 16:07:16
 16:07:36
 16:07:56
 16:07:16
 16:07:36
 16:07:56

Your output doesn't match your format string:

In [1]: import time

In [2]: print time.strftime('%m-%d-%Y %H:%m:%S')
07-21-2009 17:07:16

There's no way to tell why your output times seem to repeat without
seeing the code that surrounds your print time.strftime('%m-%d-%Y %H:
%m:%S') line.

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


Re: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order

2009-07-21 Thread Simon Forman
On Jul 21, 5:53 pm, davidj411 davidj...@gmail.com wrote:
 On Jul 21, 5:29 pm, Simon Forman sajmik...@gmail.com wrote:



  On Jul 21, 5:00 pm, davidj411 davidj...@gmail.com wrote:

   I am using a recursive function to print the time and a few other
   things on each pass. ( the function calculates size of file that is
   being transferred and if not 100 % copied, it waits 20 secs and checks
   again).

   i would expect the time to be correct anytime it is used:

   --code below --
   print time.strftime('%m-%d-%Y %H:%m:%S')
   --code above --

   here is an example of what i am seeing:

   16:07:16
   16:07:36
   16:07:56
   16:07:16
   16:07:36
   16:07:56
   16:07:16
   16:07:36
   16:07:56

  Your output doesn't match your format string:

  In [1]: import time

  In [2]: print time.strftime('%m-%d-%Y %H:%m:%S')
  07-21-2009 17:07:16

  There's no way to tell why your output times seem to repeat without
  seeing the code that surrounds your print time.strftime('%m-%d-%Y %H:
  %m:%S') line.

 sorry, yes, i did manually filter the output.

 here is the function:

 def log_out(msg,servername='std.out'):
         print msg
         open(log_dir + '\\' + servername + .log,'a').write(servername + -
 + time.strftime('%m-%d-%Y %H:%M:%S') +   + msg+'\n')

 on each pass, it should output the newer time (whether recursive or
 not, right) ?

Well, as Piet van Oostrum pointed out, your problem in the first code
you posted was that you used '%m' rather than '%M' for the minutes.
(Good eye Van Oostrum!)  But now in this function you seem to have the
correct '%M' field.  Are you still having the same output after
changing that?


In any event, here's a rewritten version of that function that's a
little cleaner, FWIW.


from os.path import join
from time import strftime

format = '%m-%d-%Y %H:%M:%S'

def log_out(msg, servername='std.out'):
print msg
msg = %s - %s %s\n % (servername, strftime(format), msg)
log_file = open(join(log_dir, servername + .log), 'a')
try:
log_file.write(msg)
finally:
log_file.close()


But why not just use the logging module?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tough-to-explain Python

2009-07-20 Thread Simon Forman
On Jul 19, 2:51 pm, Paul Rubin http://phr...@nospam.invalid wrote:
 Calroc forman.si...@gmail.com writes:
  I'm engaged presently in starting a school to teach programming from
  the ground up, based roughly on the curriculum outlined in the article
  I mentioned. ...
  I'm excited about formal methods because one, I just heard about them,

 Formal methods are a big and complicated subject.  Right after you
 heard about them is probably not the best time to start teaching them.
 Better get to understand them yourself first.

Very good point.

But I'm glad it's there to study, these are wheels I don't have to
invent for myself.

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


  1   2   3   4   5   >