Re: how to call os.path.join() on a list ...

2007-02-26 Thread funkyj
On Feb 26, 8:03 pm, "funkyj" <[EMAIL PROTECTED]> wrote:
> I want to call os.path.join() on a list instead of a variable list of
> arguments.  I.e.
>
> [scr-misc] (186:0)$ python
> iPython 2.4 (#2, Feb 18 2005, 16:39:27)
> [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
> Type "help", "copyright", "credits" or "license" for more
> information.
> m>>>
> >>> import os
> >>> import string
> >>> p = os.environ['PWD']
> >>> p
> '/tmp/a/b/c/d'
> >>> os.path.join(string.split(p, os.sep))
> ['', 'tmp', 'a', 'b', 'c', 'd']
> >>>
>
> the value returned by os.path.join() is obviously not the desired
> result ...
>
> Sure, I can hack my own version of os.path.join() by using os.sep but
> that does not seem very pythonic.
>
> In lisp one would do something like
>
> (funcall  #'os.path.join  (string.split p os.sep))
>
> What is the python idiom for callling a function like os.path.join()
> that takes a variable number of arguments when you currently have the
> arguements in a list variable?
>
> I'm curious about the answer to the question above but in the meantime
> I'll hack "my.path.join()' that takes a single list as an argument and
> move on with my little project.
>
> Regards,
>   fj

figured it out ...

I can just do:

os.sep.join(string.split(p, os.sep))

it isn't "funcall" but it gets me where I want to go.

Regards,
  --jfc

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


how to call os.path.join() on a list ...

2007-02-26 Thread funkyj

I want to call os.path.join() on a list instead of a variable list of
arguments.  I.e.

[scr-misc] (186:0)$ python
iPython 2.4 (#2, Feb 18 2005, 16:39:27)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for more
information.
m>>>
>>> import os
>>> import string
>>> p = os.environ['PWD']
>>> p
'/tmp/a/b/c/d'
>>> os.path.join(string.split(p, os.sep))
['', 'tmp', 'a', 'b', 'c', 'd']
>>>

the value returned by os.path.join() is obviously not the desired
result ...

Sure, I can hack my own version of os.path.join() by using os.sep but
that does not seem very pythonic.

In lisp one would do something like

(funcall  #'os.path.join  (string.split p os.sep))

What is the python idiom for callling a function like os.path.join()
that takes a variable number of arguments when you currently have the
arguements in a list variable?

I'm curious about the answer to the question above but in the meantime
I'll hack "my.path.join()' that takes a single list as an argument and
move on with my little project.

Regards,
  fj

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


Re: how to suppress the "source code echo" output by warnings.warn("x")?

2006-05-19 Thread funkyj

Peter Otten wrote:
> funkyj wrote:
>
> > I've been googling around trying to find the answer to this question
> > but all I've managed to turn up is a 2 year old post of someone else
> > asking the same question (no answer though).

> How about monkey-patching?
>
> import warnings
>
> def formatwarning(message, category, filename, lineno):
> return  "%s:%s: %s: %s\n" % (filename, lineno,
> category.__name__, message)
>
> warnings.formatwarning = formatwarning
> 
> warnings.warn("so what")

Thanks, that did the trick!

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


how to suppress the "source code echo" output by warnings.warn("x")?

2006-05-19 Thread funkyj
I've been googling around trying to find the answer to this question
but all I've managed to turn up is a 2 year old post of someone else
asking the same question (no answer though).

http://groups.google.com/group/comp.lang.python/browse_frm/thread/8004c690b8c4db53/81e460a0ee8b03a5?lnk=st&q=python+warnings+echo&rnum=6#81e460a0ee8b03a5

jh> In the following
jh>
jh> import warnings
jh> warnings.warn('change me')
jh>
jh> The warning is issued:
jh>
jh> hunter:~/python/test> python test.py
jh> test.py:3: UserWarning: change me
jh>   warnings.warn('change me')
jh>
jh> I want to supress the line echo.  Eg, I just want
jh>
jh> hunter:~/python/test> python test.py
jh> test.py:3: UserWarning: change me
jh>
jh> How do I configure warnings to do this?

Perhaps this can't be done without rewriting the warning module?

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


Re: Programming challenge: wildcard exclusion in cartesian products

2006-03-28 Thread funkyj
Chris F Clark wrote:
> Yes, there is literature on the generating side of the regular
> expression/FSM model.  In fact, the matching problem and the
> generating problems are exactly equivalent.  A slight variation of the
> definition of how a matcher works, turns it into a generator and vice
> versa.  To directly generate (rather than match) from an FSM, one
> simply does a walk (e.g. depth first search or breath first search)
> over the machine writing out (rather than matching) the symbols used
> as labels.  Thus, all the theorems about matching apply to generating
> and also in reverse.

 If the language is Sigma* (rather than Sigma^n in the original post)
then doing a depth first search over the FSM requires a stack to
maintain context, right?  I find it interesting that you suggest a PDA
(push down automata) is required to enumerate the language accepted by
an FSM since PDAs are strongly associated with CFGs (context free
grammars).  Does it follow then that a Turing machine is required to
enumerate the language defined by a CFG?  (that was a joke, I think).


> It is worth noting the regular languages
> are closed under the difference operator, so that resulting language
> from substracting one regular expression from another is still a
> regular language,

I was pretty sure this was the case but it has been more than a decade
since I studied computational models in school so I wasn't absolutely
certain.  While I do remember homework that called for creating FSMs
that accepted languages defined by a regexp, I don't recall having
solved the "enumeration" problem.

Your clear and concise comments did a nice job of jogging my memory.

  ===

It seems to me that the purpose of the original challenge was to
compare how different languages implement the solution to the problem.
For such a well understood problem as this the goal of the challenge
(to contrast the languages) is best served when participants have a
good understanding of the 'state of the art' abstract solutions (e.g.
using a stack to traverse a FSM in DFS fashion).

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


Re: Programming challenge: wildcard exclusion in cartesian products

2006-03-27 Thread funkyj
Going in a slightly different direction ...

There has been lots of published work on how to create efficient FSMs
from regexps.  Generally these FSMs are used for pattern matching (i.e.
"does string 's' match regexp 'e'?").

Is there any corresponding literature on the topic addressed by the
OP's challenge of generating the languaged defined by a regexp (or the
complement of that regexp)?

  --jfc

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


Re: Programming challenge: wildcard exclusion in cartesian products

2006-03-21 Thread funkyj
Dinko Tenev wrote:
> Doug Quale wrote:

> Hmmm...storage is not an issue in the Prolog version.  It generates a
> candidate solution, then checks membership in the wildcard set, then
> backtracks (backtracking is caused by "fail" in the test goal.)  On
> backtracking, it effectively "forgets" the last solution, so the memory
> is freed up (or at least free to be reclaimed through GC.)

How about the other iterator characteristics?

when there is a huge solution space can I ask the prolog version to
give me the first 1000 solutions?  The next 1000 solutions (i.e. 1000 -
1999)?

If you can do that then it would appear that generators have no
advantage over your prolog solution.

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


Re: Programming challenge: wildcard exclusion in cartesian products

2006-03-17 Thread funkyj
[EMAIL PROTECTED] wrote:
> It would seem that your program is just filtering the full cartesian
> product, right? The solution I'm looking for generates the elements
> one-by-one so that it could be used in a loop.

One advantage of a generator over filtering the full product is that I,
as the user of the generator, am not obligated to iterate over the
entire solution space.

Are there other _practical_ advantages of generators over mapping &
filtering complete sets?

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


Re: Programming challenge: wildcard exclusion in cartesian products

2006-03-16 Thread funkyj
NOTE: I am a lisp newbie.  I'm sure our resident lisp experts can
create much better (both faster, shorter and clearer) solutions than
the one above.

Even I could have created something shorter but I thought it would be
fun to apply the "utility function" approach in decomposing the
problem.

  --jfc

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


Re: Programming challenge: wildcard exclusion in cartesian products

2006-03-16 Thread funkyj
here is my version of the same.

REPL output:

CL-USER> (tests)


set  = (1 2)
n= 3
patterns = ((1 ANY 2))
---
(1 1 1)
(1 2 1)
(2 1 1)
(2 1 2)
(2 2 1)
(2 2 2)


set  = (A B)
n= 3
patterns = ((A ANY B) (B ANY A))
---
(A A A)
(A B A)
(B A B)
(B B B)


set  = (1 2)
n= 3
patterns = ((1 ANY 2) (2 ANY 1))
---
(1 1 1)
(1 2 1)
(2 1 2)
(2 2 2)
NIL
CL-USER>

source:

 cartesian products minus wildcard patterns per:

 >Newsgroups: comp.lang.lisp, etc...
 >Subject: Programming challenge: wildcard exclusion in cartesian
products
 >Date: 16 Mar 2006 03:14:23 -0800



(defun show-me (x) (format t "~A~%" x))

(defun set^n (fn set n &optional acc)
  "call `fn' on each permutation of `set' raised to the `n' power"
  (if (<= n 0)
  (funcall fn (reverse acc))
  (dolist (e set)
(set^n fn set (- n 1) (cons e acc)

;; test set^n by printing and visually inspecting the result
(defun pr-set^n (set n)   (set^n #'show-me set n))

;; curry `set^n' so that `fn' is the only parameter
(defun set^n-gen (set n)
  (lambda (fn) (set^n fn set n)))

(defun mk-matchl-p (pat-list)
  "return a function that tests a value against the patterns in
`pat-list'"
  (labels ((matchp (pat val)
 (cond ((null pat) t)
   ((or (eq (car pat) (car val))
(eq (car pat) :any))
(matchp (cdr pat) (cdr val))
(lambda (val)
  "predicate: return true if val matches any pattern in `pat-list'"
  (dolist (p pat-list)
(if (matchp p val)
(return t))

(defun not-fp (f-pred)
  "return the complement of predicate `f-pred'"
  (lambda (x) (not (funcall f-pred x

;; f-gen is a generator of the form returned by set^n-gen
(defun accumulate-if (f-gen f-pred)
  "accumulate values generated by f-gen that satisfy f-pred"
  (let (acc)
(funcall f-gen (lambda (x) (if (funcall f-pred x) (push x acc
(nreverse acc)))

;; `pr-set^n-withoutWC' is the lisp equivalent (more or less) of
;; python code:
;;   >>> for i in cp.CPWithoutWC(x,y,z): print i
(defun pr-set^n-withoutWC (set n pat-list)
  (format t "~%~%set  = ~A~%n= ~A~%patterns = ~A~%~A~%"
  set n pat-list "---")
  (dolist (e (accumulate-if (set^n-gen set n)
(not-fp (mk-matchl-p pat-list
(format t "~A~%" e)))

(defun tests ()
  "generate test output per the original problem examples"
  (pr-set^n-withoutWC '(1 2) 3 '((1 :any 2)))
  (pr-set^n-withoutWC '(a b) 3 '((a :any b) (b :any a)))
  (pr-set^n-withoutWC '(1 2) 3 '((1 :any 2) (2 :any 1

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


Re: ConfigParser: writes a list but reads a string?

2006-01-20 Thread funkyj
I'm interested in the same sort of config file issues.

Unfortunately the restricted execution has fallen out of favor.  My
preferred solution would be to have a configEval() function that is
just like the regular Python eval() but only accepts a subset of the
python language, e.g. creating the basic built in datatypes and
variable assignment and perhaps a short list of safe library functions:

favoriteColors = [ 'red', 'blue', 'orange']
props = {'fish': 7, 'cow': 'milk'}
# a comment
otherstuff = (props, favoritColors, 7)

While currently I just write my config file in python and eval() it,
this sucks from a security standpoint (e.g. someone could replace the
config file with a python program that deletes my hard drive).  Thanks
for the configObj suggestion, I'll look into that.

making the config file XML and using xml.dom is another option,
although XML is a bit ugly to edit by hand.
  --jfc

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


Re: add pexpect to the standard library, standard "install" mechanism.

2006-01-20 Thread funkyj
Yeah, going back to the sourceforge site now I see the install
instructions you quote above.

Perhaps I'm spoiled by all the FSF packages I've dealt with over the
years.  In addition to being impatient I expect the README file to
either tell me how to install the product or direct me to the INSTALL
file in the package that has the install instructions.

Back to my other question, how can I lobby to have pexpect added to the
standard python library?

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


Re: add pexpect to the standard library, standard "install" mechanism.

2006-01-19 Thread funkyj
Fredrik Lundh wrote:
> any special reason why pexpect cannot ship with a standard distutils
> setup.py file (or even egg support) ?



Oh, apparently there is a standard/builtin python distribution
mechanism called 'distutils'.

I guess on the install issue my only suggestion would be for the
pexpect README to say something like:

To install this package on your system run:

  bash$ python setup.py install

I read the pexpect README file but it didn't say the bit above and I
had never heard of distutils.

Thanks Fredrik!
  --jfc

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


add pexpect to the standard library, standard "install" mechanism.

2006-01-19 Thread funkyj
I love pexpect because it means I may never have to use expect again (I
don't do any heavy expect lifting -- I just need simple tty control)!

As a python advocate I find it embarassing how difficult it is do the
following in python (without pexpect):

- logon to a remote system using ssh
- do an 'ls' and exit the remote shell
- print the output from the remote shell session.

pexpect, of course, makes this childs play (see the pexpect sshls.py
example for one way to do this).

I'm surprised that, as useful as pexpect is, it has not been included
in the standard python library.  How can we get this wonderful package
in the standard library?

TANGENT: it would be nice if python included a standard "tarfile
install" program to make installing 3rd party packages like pexpect
easier.

   bash $ pinstall_tarfile   foo-pkg.tgz

This install program would
+ look at your sys.path for a suitable install directory
+ check for appropriate directory permissions
+ check whether the tarfile extracts to the CWD or a subdir
  (the rest of the script is adjusted accordingly -- assume subdir
  behavior).
+ tell the user where you plan to install the package.  confirm
   that this is OK.
+ extract the tarfile and create/update the foo.pth file.

Perhaps such an install script is already in the standard python
library and I just haven't stumbled across it?

Regards,
  --jfc

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