Re: calling a simple PyQt application more than once

2012-05-20 Thread Jonathan
Hello Jabba,

Did you ever find a solution to the problem?  If so, can you please post it?

Thanks,
Jonathan

--
View this message in context: 
http://python.6.n6.nabble.com/calling-a-simple-PyQt-application-more-than-once-tp4335946p4975385.html
Sent from the Python - python-list mailing list archive at Nabble.com.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sudoku dictionary attack

2005-06-20 Thread Jonathan


[EMAIL PROTECTED] wrote:
> Thought I'd offer a method for solving all possible 9x9 sudoku puzzles
> in one go. It'll takes a bit of time to run however (and 9x9 seems to
> be about as big as is reasonably possible before combinatorial
> explosion completely scuppers this type of program)...
>
> Basic idea:-
>
> Start with a grid initialised with:
>
> 123456789
> 234567891
> 345678912
> 456789123
> 567891234
> 678912345
> 789123456
> 891234567
> 912345678
>
> Note that all rows and columns contain all 9 digits (but that the
> sub-tiles aren't correct for a sudoku solution).
>
> Next write a program which permutes all 9 columns, and then for each of
> those permutations permutes the last 8 rows. This will, I believe,
> generate all possible grids with no digit repetitions in any row or
> column. It will generate 14,631,321,600 (9!*8!) possible sudoku
> candidates. Finally, check each candidate to see if any 3x3 subtile has
> a repeated digit in it and discard as soon as a repeat is found (sooner
> the better). Any that come through unscathed get written as a 82 (81 +
> lf) char string to an output file.

I'm having trouble coming up with anything that fits this grid:

..12.
..2x.
.
.
.
.
.
.
.

where x is not 3, by permuting columns, then rows.  You may also have
to permute the numbers.  Although, even then, x=1 is still impossible.

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


Re: New user's initial thoughts / criticisms of Python

2013-11-09 Thread Jonathan
On Saturday, November 9, 2013 8:27:02 AM UTC-5, Joshua Landau wrote:

> `select` is quite an odd statement, in that in most cases it's just a
> weaker variant of `if`. By the time you're at the point where a
> `select` is actually more readable you're also at the point where a
> different control flow is probably a better idea. Things like
> dictionaries or a variables pointing to functions are really useful
> and can be encapsulated in a class quite well. This is a bit more
> advanced but largely more rigorous.
> 
> But most of the time the idea is just that an `if` is more explicit,
> and it's not like a `case` statement can be optimised as it can in
> lower-level languages with simpler datatypes.

The C switch statement is very limited.  The select statement in the dialect of 
BASIC I regularly use is more flexible.   It's more concise on long if chains 
because it elides the "end if"s.  But the use of indentation for blocks and the 
"in" operator certainly reduce the need for it in Python.

In pythonic syntax:

select :
  case ,[],:
  
  
  case else:


where  is one of

a) 

which is equivalent to:   elif  = :

b)  

which is equivalent to:   elif  

Note that:
 "elif" is actually "if" for the first case in the select.
 control exits at next case, no need for breaks.
 expression0 is only evaluated once and stored in an anonymous temporary 
variable.

The switch statement in (the language) go is similar, except that  
defaults to true and it doesn't elide  in the case statements.

Dictionaries can't handle the uses where expression0 is constant and the case 
expressions are not.
Function variables beg the question.  How did you decide what to assign to the 
variable?

I'd use this select if it was in Python, but I don't see much need for it.

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


Getting ASCII encoding where unicode wanted under Py3k

2013-05-13 Thread Jonathan Hayward
quot;', "''")
edit_table += ''
edit_table += ''
if russian != None:
edit_table += '''''' % locals()
edit_table += '\n'
edit_table += ''
edit_table += '''''' % locals()
edit_table += ''
edit_table += '''''' % locals()
edit_table += '\n'
edit_table += ''
print ('''Content-Type: text/html




Edit Phrases





%(edit_table)s



''' % locals())
else:
text = ''
for phrase in state['phrases']:
if phrase[0]:
text += ('' +
  phrase[0] + '')
else:
text += '' + phrase[1] + ''

print ('''Content-type: text/html




___


body
{
font-family: Verdana, Arial, sans;
}
div
{
background-color: #00;
}




%(text)s



jQuery(document).tooltip();


''' % locals())


-- 
[image: Christos Jonathan Hayward] <http://jonathanscorner.com/>
Christos Jonathan Hayward, an Orthodox Christian author.

*Amazon <http://amazon.com/author/cjshayward>* • Author
Bio<http://jonathanscorner.com/author/>
 • *Author Site <http://cjsh.name/>* •
*Email
* • Facebook <http://www.facebook.com/christos.jonathan.hayward> • Fan
Page<http://fan.cjshayward.com/>
 • Google Plus <http://jonathanscorner.com/plus> •
LinkedIn<http://www.linkedin.com/in/jonathanhayward>
 • *Professional <http://jonathanhayward.com/>* •
Twitter<http://twitter.com/JonathansCorner>
 • *Web <http://jonathanscorner.com/>* • What's
New?<http://jonathanscorner.com/>
If you read just *one* of my books, you'll want *The Best of Jonathan's
Corner <http://www.amazon.com/dp/1478219912>*.
-- 
http://mail.python.org/mailman/listinfo/python-list


New line conversion with Popen attached to a pty

2013-06-21 Thread Jonathan Harden
Hi,

We have a class which executes external processes in a controlled environment 
and does "things" specified by the client program with each line of output. To 
do this we have been attaching stdout from the subprocess.Popen to a pseudo 
terminal (pty) made with pty.openempty and opened with os.fdopen. I noticed 
that we kept getting a bunch of extra new line characters. 

This is all using python 2.6.4 in a centos6 environment.

After some investigation I realised we needed to use universal_newline support 
so I enabled it for the Popen and specified the mode in the fdopen to be rU. 
Things still seemed to be coming out wrong so I wrote up a test program boiling 
it down to the simplest cases (which is at the end of this message). The output 
I was testing was this:

Fake\r\nData\r\n
as seen through hexdump -C:

> hexdump -C output.txt 
  46 61 6b 65 0d 0a 44 61  74 61 0d 0a  |Fake..Data..|
000c

Now if I do a simple subprocess.Popen and set the stdout to subprocess.PIPE, 
then do p.stdout.read() I get the correct output of

Fake\nData\n

When do the Popen attached to a pty I end up with

Fake\n\nData\n\n

Does anyone know why the newline conversion would be incorrect, and what I 
could do to fix it? In fact if anyone even has any pointers to where this might 
be going wrong I'd be very helpful, I've done a lot of hours of fiddling with 
this and googling to no avail.

Thanks,

Jonathan

#!/usr/bin/env python2.6.4
import os
import pty
import subprocess
import select
import fcntl

class TestRead(object):

def __init__(self):
super(TestRead, self).__init__()
self.outputPipe()
self.outputPty()

def outputPipe(self):
p1 = subprocess.Popen(
("/bin/cat", "output.txt"),
stdout=subprocess.PIPE,
universal_newlines=True
)
print "1: %r" % p1.stdout.read()

def outputPty(self):
outMaster, outSlave = pty.openpty()
fcntl.fcntl(outMaster, fcntl.F_SETFL, os.O_NONBLOCK)

p2 = subprocess.Popen(
("/bin/cat", "output.txt"),
stdout=outSlave,
universal_newlines=True
)

with os.fdopen(outMaster, 'rU') as pty_stdout:
while True:
try:
rfds, _, _ = select.select([pty_stdout], [], [], 0.1)
break
except select.error:
continue

for fd in rfds:
buf = pty_stdout.read()
print "2: %r" % buf

if __name__ == "__main__":
t = TestRead()
-- 
http://mail.python.org/mailman/listinfo/python-list


Looking for a name for a deployment framework...

2013-06-24 Thread jonathan . slenders
Hi all,

Any suggestions for a good name, for a framework that does automatic server 
deployments?

It's like Fabric, but more powerful.
It has some similarities with Puppet, Chef and Saltstack, but is written in 
Python.

Key points are that it uses Python, but is still very declarative and supports 
introspection. It supports parallel deployments, and interactivity. And it has 
a nice commandline shell with autocompletion for traversing the deployment tree.

The repository:
https://github.com/jonathanslenders/python-deployer/tree/refactoring-a-lot-v2


Suggestions welcome :)
Jonathan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for a name for a deployment framework...

2013-06-24 Thread jonathan . slenders
Thanks everyone, I'll think about it.

The main reason is that I'm working on the documentation, and this a a good 
opportunity to think about the naming. python-deploy-framework or 
python-deployer could be too boring.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for a name for a deployment framework...

2013-06-25 Thread jonathan . slenders
Le mardi 25 juin 2013 06:38:44 UTC+2, Chris Rebert a écrit :
> Er, Salt is likewise written in Python.

You're right. Salt is also Python, excuse me, and it's very powerful as well.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is Django v1.3 documentation the newest version?

2012-07-02 Thread Jonathan Harris
Hello

It isn't
Having been frustrated with out of date books, specifically the Apress
published 'Definitive Guide To Django', I've downloaded the Kindle edition
of Django 1.4 documentation
It's a good tutorial

J

On Mon, Jul 2, 2012 at 11:00 AM, levi nie  wrote:

> Is  Django v1.3 documentation the newest version?
> i use the Django book 2.0.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: psphere: how to make thread safe

2012-08-26 Thread jonathan . kinred
On Wednesday, 22 August 2012 22:03:48 UTC+10, sajuptpm  wrote:
> Hi,
> 
> 
> 
> psphere: Python interface for the VMware vSphere Web Services SDK
> 
> 
> 
> I already developed an app using https://bitbucket.org/jkinred/psphere. But 
> getting lot of errors since psphere is not thread safe (I think). So i wrote 
> couple of scripts to test it (See attached files) and found that caching 
> mechanism used by psphere is not thread safe. Could someone please give me 
> some suggestion to make it thread safe.
> 
> 
> 
> 
> 
> ===Test Code 
> 
> 
> 
> import psphere
> 
> from psphere.client import Client
> 
> from psphere.managedobjects import HostSystem, VirtualMachine, ComputeResource
> 
> client = Client("192.168.0.114", "root", "vmware1") ##vCenter
> 
> print "\nSucessfully connected to vCenter.\n"
> 
> 
> 
> from threading import Thread
> 
> 
> 
> def myfunc(i):
> 
>   host1 = HostSystem.get(client, name="192.168.0.134")
> 
>   host2 = HostSystem.get(client, name="192.168.0.113")
> 
>   print "i--",i
> 
>   while True:
> 
>   #host1.update(properties=["config", "vm"])
> 
>   #host2.update(properties=["config", "vm"])
> 
>   c = type(host1.config.network)
> 
>   v = type(host2.config.network)
> 
>   for vm in host1.vm:
> 
>   k = vm.config.template
> 
>   for vm in host2.vm:
> 
>   p = vm.config.template
> 
> 
> 
> 
> 
> for i in range(10):   
> 
> t = Thread(target=myfunc, args=(i,))
> 
> t.start()
> 
> 
> 
> 
> 
> """
> 
> OUTPUT 
> 
> ===
> 
> Sucessfully connected to vCenter.
> 
> 
> 
> i-- 1
> 
> i-- 3
> 
> i-- 5
> 
> i-- 0
> 
>  i-- 4
> 
> i-- 2i-- 7
> 
> i-- 
> 
> 9
> 
> i-- 6
> 
> i-- 8
> 
> Exception in thread Thread-4:
> 
> Traceback (most recent call last):
> 
>   File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
> 
> self.run()
> 
>   File "/usr/lib/python2.7/threading.py", line 504, in run
> 
> self.__target(*self.__args, **self.__kwargs)
> 
>   File "vcenter_test1.py", line 19, in myfunc
> 
> k = vm.config.template
> 
>   File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/__init__.py", line 79, 
> in __get__
> 
> value = self.fget(inst)
> 
>   File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/managedobjects.py", line 
> 1236, in config
> 
> return self._get_dataobject("config", False)
> 
>   File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/__init__.py", line 116, 
> in _get_dataobject
> 
> return self._cache[name][0]
> 
> KeyError: 'config'
> 
> 
> 
> Exception in thread Thread-6:
> 
> Traceback (most recent call last):
> 
>   File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
> 
> self.run()
> 
>   File "/usr/lib/python2.7/threading.py", line 504, in run
> 
> self.__target(*self.__args, **self.__kwargs)
> 
>   File "vcenter_test1.py", line 17, in myfunc
> 
> v = type(host2.config.network)
> 
> AttributeError: VirtualMachineConfigInfo instance has no attribute 'network'
> 
> 
> 
> 
> 
> 
> 
> """

I'm the author of psphere. I'd be really interested to get some experienced 
insight into this as I really don't know the first thing about thread safety.

psphere does use a cache, but I'm confused as to how the cache would be thread 
unsafe as the cache is just a property of an object.

What I'm suspecting (and this only came to me while writing this post) is that 
I have a "client" object which is passed into each object and used to retrieve 
data from the server. See this gist:
https://gist.github.com/3478641

When used in a threaded scenario could the client be used by multiple objects 
simultaneously and the replies would come in out of order?

A couple of years ago I started psphere to learn Python and I always hoped that 
I would have an experienced developer join me. I did learn Python but I never 
got that experienced developer so when it comes to issues like this I'm 
definitely out of my depth! Please if someone could spend the time with me I 
would really appreciate it!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: psphere: how to make thread safe

2012-08-26 Thread jonathan . kinred
On Sunday, 26 August 2012 22:45:25 UTC+10, [email protected]  wrote:
> On Wednesday, 22 August 2012 22:03:48 UTC+10, sajuptpm  wrote:
> 
> > Hi,
> 
> > 
> 
> > 
> 
> > 
> 
> > psphere: Python interface for the VMware vSphere Web Services SDK
> 
> > 
> 
> > 
> 
> > 
> 
> > I already developed an app using https://bitbucket.org/jkinred/psphere. But 
> > getting lot of errors since psphere is not thread safe (I think). So i 
> > wrote couple of scripts to test it (See attached files) and found that 
> > caching mechanism used by psphere is not thread safe. Could someone please 
> > give me some suggestion to make it thread safe.
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > ===Test Code 
> 
> > 
> 
> > 
> 
> > 
> 
> > import psphere
> 
> > 
> 
> > from psphere.client import Client
> 
> > 
> 
> > from psphere.managedobjects import HostSystem, VirtualMachine, 
> > ComputeResource
> 
> > 
> 
> > client = Client("192.168.0.114", "root", "vmware1") ##vCenter
> 
> > 
> 
> > print "\nSucessfully connected to vCenter.\n"
> 
> > 
> 
> > 
> 
> > 
> 
> > from threading import Thread
> 
> > 
> 
> > 
> 
> > 
> 
> > def myfunc(i):
> 
> > 
> 
> > host1 = HostSystem.get(client, name="192.168.0.134")
> 
> > 
> 
> > host2 = HostSystem.get(client, name="192.168.0.113")
> 
> > 
> 
> > print "i--",i
> 
> > 
> 
> > while True:
> 
> > 
> 
> > #host1.update(properties=["config", "vm"])
> 
> > 
> 
> > #host2.update(properties=["config", "vm"])
> 
> > 
> 
> > c = type(host1.config.network)
> 
> > 
> 
> > v = type(host2.config.network)
> 
> > 
> 
> > for vm in host1.vm:
> 
> > 
> 
> > k = vm.config.template
> 
> > 
> 
> > for vm in host2.vm:
> 
> > 
> 
> > p = vm.config.template
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > for i in range(10): 
> 
> > 
> 
> > t = Thread(target=myfunc, args=(i,))
> 
> > 
> 
> > t.start()
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > """
> 
> > 
> 
> > OUTPUT 
> 
> > 
> 
> > ===
> 
> > 
> 
> > Sucessfully connected to vCenter.
> 
> > 
> 
> > 
> 
> > 
> 
> > i-- 1
> 
> > 
> 
> > i-- 3
> 
> > 
> 
> > i-- 5
> 
> > 
> 
> > i-- 0
> 
> > 
> 
> >  i-- 4
> 
> > 
> 
> > i-- 2i-- 7
> 
> > 
> 
> > i-- 
> 
> > 
> 
> > 9
> 
> > 
> 
> > i-- 6
> 
> > 
> 
> > i-- 8
> 
> > 
> 
> > Exception in thread Thread-4:
> 
> > 
> 
> > Traceback (most recent call last):
> 
> > 
> 
> >   File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
> 
> > 
> 
> > self.run()
> 
> > 
> 
> >   File "/usr/lib/python2.7/threading.py", line 504, in run
> 
> > 
> 
> > self.__target(*self.__args, **self.__kwargs)
> 
> > 
> 
> >   File "vcenter_test1.py", line 19, in myfunc
> 
> > 
> 
> > k = vm.config.template
> 
> > 
> 
> >   File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/__init__.py", line 79, 
> > in __get__
> 
> > 
> 
> > value = self.fget(inst)
> 
> > 
> 
> >   File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/managedobjects.py", 
> > line 1236, in config
> 
> > 
> 
> > return self._get_dataobject("config", False)
> 
> > 
> 
> >   File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/__init__.py", line 
> > 116, in _get_dataobject
> 
> > 
> 
> > return self._cache[name][0]
> 
> > 
> 
> > KeyError: 'config'
> 
> > 
> 
> > 
> 
> > 
> 
> > Exception in thread Thread-6:
> 
> > 
> 
> > Traceback (most recent call last):
> 
> > 
> 
> >   File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
> 
> > 
> 
> > self.run()
> 
> > 
> 
> >   File "/usr/lib/python2.7/threading.py", line 504, in run
> 
> > 
> 
> > self.__target(*self.__args, **self.__kwargs)
> 
> > 
> 
> >   File "vcenter_test1.py", line 17, in myfunc
> 
> > 
> 
> > v = type(host2.config.network)
> 
> > 
> 
> > AttributeError: VirtualMachineConfigInfo instance has no attribute 'network'
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > """
> 
> 
> 
> I'm the author of psphere. I'd be really interested to get some experienced 
> insight into this as I really don't know the first thing about thread safety.
> 
> 
> 
> psphere does use a cache, but I'm confused as to how the cache would be 
> thread unsafe as the cache is just a property of an object.
> 
> 
> 
> What I'm suspecting (and this only came to me while writing this post) is 
> that I have a "client" object which is passed into each object and used to 
> retrieve data from the server. See this gist:
> 
> https://gist.github.com/3478641
> 
> 
> 
> When used in a threaded scenario could the client be used by multiple objects 
> simultaneously and the replies would come in out of order?
> 
> 
> 
> A couple of years ago I started psphere to learn Python and I always hoped 
> that I would have an experienced developer join me. I did learn Python but I 
> never got that experienced developer so when it comes to issues 

Re: Hiring Python Developer @ CA, USA

2012-09-18 Thread Jonathan Holloway
Incidentally and I know this is region specific, but what's the average
salary approximately in the US/UK for a Senior Python programmer?

ITJobsWatch in the UK says - http://www.itjobswatch.co.uk/jobs/uk/python.do

Is that about right?

Jon.

On 18 September 2012 08:40, Paul Rudin  wrote:

> [email protected] writes:
>
> > ...Must be an export in this language...
>
> Are you hiring proof readers as well? :)
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Experimental Python-based shell

2012-10-03 Thread Jonathan Hayward
The chief benefit besides the searching, so far, is that you can use Py3k
mixed with shell commands as the scripting language--so script in Python
instead of bash.

When using Python for scripting, Python lines are indented by an extra tab
(or four spaces) while shell-like commands are not indented. So:

cjsh> for index in range(10):
> echo %(index)d
>
0
1
2
3
4
5
6
7
8
9

Echo could (and maybe should) be a built-in, but it isn't. The output is
os.system()'ed to bash, which echoes based on a command that includes the
value of a Python variable. The implementation is a bit crude, but it is
reasonably powerful.

I have other things on the agenda, like making it able to run scripts and
doing fuzzy matching, but for now those are the main two attractions.

On Wed, Oct 3, 2012 at 8:52 AM, Amirouche Boubekki <
[email protected]> wrote:

> Héllo,
>
> 2012/10/3 Jonathan Hayward 
>
>> I've made an experimental Python-based Unix/Linux shell at:
>>
>> http://JonathansCorner.com/cjsh/
>>
>> An experimental Unix/Linux command line shell, implemented in Python 3,
>> that takes advantage of some more recent concepts in terms of usability and
>> searching above pinpointing files in heirarchies.
>>
>
> This sound like a great idea!  What are the other benefits of it except
> the file lookup thing ? Does it do fuzzy match yet ?
>
> Is there any repository on bitbucket or github ?
>
> Thanks,
>
> Amirouche
>



-- 
[image: Christos Jonathan Hayward] <http://jonathanscorner.com/>
Christos Jonathan Hayward, an Orthodox Christian author.

*Amazon <http://amazon.com/author/cjshayward>* • Author
Bio<http://jonathanscorner.com/author/>
 • *Email * •
Facebook<http://www.facebook.com/christos.jonathan.hayward>
 • Google Plus <http://jonathanscorner.com/plus> •
*Kindle<http://stornge.com/amazon>
* • LinkedIn <http://www.linkedin.com/in/jonathanhayward> •
*Professional<http://jonathanhayward.com/>
* • Twitter <http://twitter.com/JonathansCorner> •
*Web<http://jonathanscorner.com/>
* • What's New? <http://jonathanscorner.com/>
I invite you to visit my "theology, literature, and other creative works"
site. *See one page of my website! <http://random.jonathanscorner.com/>*
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Experimental Python-based shell

2012-10-03 Thread Jonathan Hayward
I am open to suggestions and patches. I don't think the syntax strange,
though: it offers a clear and distinct way to differentiate Python and
shell commands, and shell commands can access Python variables when
specified. And it is a simple rule, without footnotes needed.

On Wed, Oct 3, 2012 at 11:25 AM, Amirouche Boubekki <
[email protected]> wrote:

>
>
> 2012/10/3 Jonathan Hayward 
>
>> The chief benefit besides the searching, so far, is that you can use Py3k
>> mixed with shell commands as the scripting language--so script in Python
>> instead of bash.
>>
>> When using Python for scripting, Python lines are indented by an extra
>> tab (or four spaces) while shell-like commands are not indented. So:
>>
>> cjsh> for index in range(10):
>> > echo %(index)d
>> >
>> 0
>> 1
>> 2
>> 3
>> 4
>> 5
>> 6
>> 7
>> 8
>> 9
>>
>> Echo could (and maybe should) be a built-in, but it isn't. The output is
>> os.system()'ed to bash, which echoes based on a command that includes the
>> value of a Python variable. The implementation is a bit crude, but it is
>> reasonably powerful.
>>
>> I have other things on the agenda, like making it able to run scripts and
>> doing fuzzy matching, but for now those are the main two attractions.
>>
>
> Is it possible to drop completly the bash syntax and use some python
> library (I saw it on github) that wraps bash commands with python functions
> or the other around making it possible to call python functions with a
> bash-like syntax. The syntax you are talking about seems strange.
>
> Regards,
>
> Amirouche
>



-- 
[image: Christos Jonathan Hayward] <http://jonathanscorner.com/>
Christos Jonathan Hayward, an Orthodox Christian author.

*Amazon <http://amazon.com/author/cjshayward>* • Author
Bio<http://jonathanscorner.com/author/>
 • *Email * •
Facebook<http://www.facebook.com/christos.jonathan.hayward>
 • Google Plus <http://jonathanscorner.com/plus> •
*Kindle<http://stornge.com/amazon>
* • LinkedIn <http://www.linkedin.com/in/jonathanhayward> •
*Professional<http://jonathanhayward.com/>
* • Twitter <http://twitter.com/JonathansCorner> •
*Web<http://jonathanscorner.com/>
* • What's New? <http://jonathanscorner.com/>
I invite you to visit my "theology, literature, and other creative works"
site. *See one page of my website! <http://random.jonathanscorner.com/>*
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newby Python help needed with functions

2011-06-03 Thread Jonathan Gardner
On Fri, Jun 3, 2011 at 7:42 AM, Cathy James  wrote:
> I need a jolt here with my python excercise, please somebody!! How can I
> make my functions work correctly? I tried below but I get the following
> error:
>
> if f_dict[capitalize]:
>
> KeyError: 
>

This error is because the function capitalize is not a key in the f_dict dict.

> Code below:
>
>
>
> def capitalize (s):
>     """capitalize accepts a string parameter and applies the capitalize()
> method"""
>     s.capitalize()
> def title(s):
>     """accepts a string parameter and applies the title() method"""
>     s.title()
> def upper(s):
>     """accepts a string parameter and applies the upper() method"""
>     s.upper()
> def lower(s):
>     """accepts a string parameter and applies the lower() method"""
>     s.lower()

As Andrew mentioned, these functions return nothing when they are
called, which is probably not what you want.

>>> lower("ABCD")
(nothing)

You need to return the value you want the function to return explicitly.

def lower(s):
return s.lower()

Note that your functions are just calling string methods. Why define
them at all? Just use the methods of the str. For instance:

>>> str.lower("ABCD"
'abcd'
>>> lower = str.lower
>>> lower("ABCD")
'abcd'

> def exit():
>     """ends the program"""
>     import sys
>     sys.exit()

you can just use sys.exit for exit, no need to define a new function
here either.

> if __name__ == "__main__":
>     f_dict = {'capitalize': 'capitalize(s)',
>   'title': 'title(s)',
>   'upper': 'upper(s)',
>   'lower': 'lower(s)',
>   'exit': 'exit(s)'}

I think this is where things go really bad for you.

You are defining a new dict here, f_dict. It has keys that are the
strings 'capitalize', 'title', etc... These have values that are
strings 'capitalize(s)', etc...

I doubt this is what you want.

Don't you want the values to be the functions corresponding to the
names in the keys? If so, just tell Python so.

f_dict = {'capitalize', str.capitalize, ...}

>     options = f_dict.keys()
>     prompt = 'Enter a function name from the list (%s): ' % ',
> '.join(options)

Note that you can use %r to format the repr() of the value.

prompt = 'Enter a function name from the list %r: ' % (options,)

> while True:
>     inp = input(prompt)
>     option =f_dict.get(inp, None)#either finds the function in question or
> returns a None object
>     s = input ("Enter a string: ").strip()
>     if not (option):
>     print ("Please enter a valid selection")
>     else:
>     if f_dict[capitalize]:
>     capitalize(s)
>     elif f_dict [title]:
>     title(s)
>     elif f_dict[upper]:
>     upper(s)
>     elif f_dict [lower]:
>     lower(s)
>     elif f_dict[exit]:
>     print ("Goodbye!! ")

Here is where things are also probably not what you intended. What you
are asking Python to do is:

1. Test if f_dict has a key that is the function capitalize; if so,
call capitalize on s and throw the result away.
2. If not, then test if f_dict has a key that is the function title;
if so, call title on s and throw the result away.
etc...

First, you can use the dict to retrieve the function you've stored as
the value in the dict.

fn = f_dict.get(inp, None)

That returned value is actually callable! That is, you can then do
something like:

fn("This is the input string")

Of course, as you already know, you should test fn to see if it is
None. If so, they typed in an option you don't recognize.

Secondly, what are you doing with the formatted string? I assume you
want to print it. If so, be explicit.

The above lines can be roughly condensed down to:

f_dict = {'capitalize':str.capitalize, ...}
while True:
inp = input(prompt)
fn = f_dict.get(inp, None)
if fn is not None:
   s = input("Input a string: ").strip()
   print "Output is: %s" % fn(s)
else:
print "Please input a valid selection"


Since this is homework, I've purposefully left important bits out that
you should be able to figure out on your own.


-- 
Jonathan Gardner
[email protected]
http://deal-digger.com
1 (855) 5-DIGGER
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional style programming in python: what will you talk about if you have an hour on this topic?

2011-07-14 Thread Jonathan Hartley
On Jul 13, 1:39 pm, Anthony Kong  wrote:
> (My post did not appear in the mailing list, so this is my second try. 
> Apology if it ends up posted twice)
>
> Hi, all,
>
> If you have read my previous posts to the group, you probably have some idea 
> why I asked this question.
>
> I am giving a few presentations on python to my colleagues who are mainly 
> java developers and starting to pick up python at work.
>
> 
> So I have picked this topic for one of my presentation. It is because 
> functional programming technique is one of my favorite in my bag  of python 
> trick. It also takes me to the rabbit hole of the functional programming 
> world, which is vastly more interesting than the conventional procedural/OO 
> languages.
> 
>
> I think I will go through the following items:
>
> itertools module
> functools module
> concept of currying ('partial')
>
> I would therefore want to ask your input e.g.
>
> Is there any good example to illustrate the concept?
> What is the most important features you think I should cover?
> What will happen if you overdo it?
>
> Cheers

I'd think you'd want to at least mention the relatively new
'lru_cache' decorator, for memoizing the return value expensive
functions, providing they are deterministic / pure, etc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional style programming in python: what will you talk about if you have an hour on this topic?

2011-07-14 Thread Jonathan Hartley
On Jul 14, 4:32 am, Gregory Ewing  wrote:
> Anthony Kong wrote:
> > So I have picked this topic for one of my presentation. It is because
> > functional programming technique is one of my favorite in my bag  of python 
> > trick.
>
> I'm not sure it's a good idea to emphasise functional
> programming too much. Python doesn't really lend itself
> to a heavily functional style. While you *can* write
> Python code that way, it's not idiomatic, and you're
> likely to give beginners a distorted idea of how Python
> is normally written.
>
> --
> Greg

Maybe the talk would work well if not aimed at complete all-round
beginners, but instead aimed at Pythonistas who are interested in
functional programming, or functionistas who are py-curious (I think
the same talk would work well for both groups)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can someone help please

2011-07-22 Thread Jonathan Hartley
Hey! Is Billy a responder, rather than the OP? Sorry then! My previous point is 
entirely nullified.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can someone help please

2011-07-22 Thread Jonathan Hartley
Hey Billy. That may not be the important part of the code, but the many people 
giving up their free time to read it and help you don't know that. It's 
probably most helpful to give them a working example so as not to waste their 
time. Just sayin for future, is all. :-)
Best regards,
  Jonathan
-- 
http://mail.python.org/mailman/listinfo/python-list


Writing a MUD Console

2011-07-22 Thread Jonathan Gardner
I had this idea on an easy way to write a MUD console.

Basically, you start a program written in Python. This will connect to
the MUD of your choice, and otherwise behave like Telnet. In fact, it
will just spawn telnet to do all the hard work for you.

As you type commands, they are piped directly into the telnet process
as MUD input commands. Commands that start with ! or maybe # will be
intercepted, however, and interpreted as special commands

Also, as output comes back, you can put hooks in to react to the data
and execute Python functions, or pipe the data to subprocesses you may
spawn. For instance, you might spawn a subprocess that opens up a
window to show your current map location. When the MUD sends the map,
it is intercepted and directed to this map subprocess. Your status
line could be interpreted and displayed in another window in graphical
format.

I have the feeling that this isn't that hard to do. It's just a matter
of getting the right combination of subprocesses working.

My preliminary experiments with the telnet subprocess have had curious results:

CODE:
import subprocess
import sys
import traceback

host = 'midkemiaonline.com'
port = '23'

conn = subprocess.Popen(
['/usr/bin/telnet', host, port],
stdin=subprocess.PIPE,
stdout=sys.stdout,
stderr=sys.stderr)

def do_command(cmd):
print "\n>>> {}".format(cmd)
try:
result = eval(cmd)
except Exception:
traceback.print_exc()
else:
if result is not None:
print repr(result)

while True:
cmd = raw_input()

if cmd[:1] == '!':
do_command(cmd[1:])
else:
conn.stdin.write(cmd)

conn.terminate()
END CODE

It seems all goes well, except certain input sequences are being
intercepted. For instance, CTRL-C throws a KeyboardInterrupt.

Also, I only get to enter one command:

$ ./telnetsubprocess.py
Trying 209.212.147.74...
Connected to midkemiaonline.com.
Escape character is '^]'.
Rapture Runtime Environment v2.1.6 -- (c) 2010 -- Iron Realms Entertainment
Multi-User License: 100-0004-000

   *

 -- Midkemia Online --

   *

  [Open Beta]

Based on the novels by Raymond E. Feist

   Midkemia Online's IP address is 209.212.147.74
   For general questions e-mail [email protected]
   There are 20 people currently on-line.

1. Enter the game.
2. Create a new character.
3. Quit.

Enter an option or your character's name. 1
Traceback (most recent call last):
  File "./telnetsubprocess.py", line 17, in 
cmd = raw_input()
EOFError
Connection closed by foreign host.

Any ideas on what is going on here?

-- 
Jonathan Gardner
[email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a MUD Console

2011-07-22 Thread Jonathan Gardner
On Fri, Jul 22, 2011 at 2:25 AM, Chris Angelico  wrote:
> Rather than attach it to this post, I've tossed the script onto my
> MUD's web site. (Yes, I run a MUD. Your subject line grabbed my
> attention!) Grab it from http://minstrelhall.com/RosMudAndroid.py and
> give it a whirl!
>

That code is surprisingly simple. Let me write one that uses asyncore
so that the looping can incorporate other async processes as well.

-- 
Jonathan Gardner
[email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP for module naming conventions

2011-03-17 Thread Jonathan Gossage
I have found this approach problematic if you have packages separately
developed and maintained in different directory trees, resulting in
more than one PYTHONPATH entry with the same root metapackage name.
What happens is that only the first entry in the PYTHONPATH containing
the metapackage name is looked in for package/module resolution. Do
you have any suggestions for handling this kind of packaging?


On Thu, Mar 17, 2011 at 12:17 PM, eryksun ()  wrote:
> On Friday, March 11, 2011 4:52:57 PM UTC-5, Tim Johnson wrote:
>> I need to be better informed on naming conventions for modules.  For
>> instance, I need to create a new module and I want to make sure that
>> the module name will not conflict with any future or current python
>> system module names.
>
> Do you mean package names? Within a package you can use relative imports to 
> avoid conflicts. You could put all of your packages in a metapackage 
> namespace with a unique name -- a company/group name or personal/family name, 
> an uncommon English word, or something from another language.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Validating Command Line Options

2011-03-23 Thread Jonathan Gossage
See inline comments

On Wed, Mar 23, 2011 at 2:13 PM, Alex Willmer  wrote:

> On Mar 23, 3:20 pm, T  wrote:
> > Thanks!  argparse is definitely what I need..unfortunately I'm running
> > 2.6 now, so I'll need to upgrade to 2.7 and hope that none of my other
> > scripts break.
>
> Argparse was a third-party module before it became part of the std-
> lib. You may find it easier to use this version:
>
> http://pypi.python.org/pypi/argparse/


I have used this version successfully on 2.6 including using mutually
exclusive groups.

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


Re: Vectors

2011-04-25 Thread Jonathan Hartley
On Apr 20, 2:43 pm, Andreas Tawn  wrote:
> > Algis Kabaila  writes:
>
> > > Are there any modules for vector algebra (three dimensional
> > > vectors, vector addition, subtraction, multiplication [scalar
> > > and vector]. Could you give me a reference to such module?
>
> > NumPy has array (and matrix) types with support for these basic
> > operations you mention. See the tutorial athttp://numpy.scipy.org/
>
> You might also want to considerhttp://code.google.com/p/pyeuclid/
>
> Cheers,
>
> Drea


Stealing this from Casey Duncan's recent post to the Grease users
list:


- (ab)use complex numbers for 2D vectors (only). Very fast arithmetic
and built-in to Python. Downside is lack of abstraction.

- Use pyeuclid (pure python) if ultimate speed isn't an issue, or if
compiled extensions are. It supports 3D and has a nice api

- vectypes is a more recent project from the same author as pyeuclid.
It offers a more consistent 'GLSL' like interface, including
swizzling, and internally seems to have more maintainable code because
it generates various sizes of vector and matrix from a single
template. This is done without performance penalty because the
generation is done at design time, not runtime.

- Use pyeigen if you want fast vectors, and don't mind compiling some
C/C++. I don't know how the Python api looks though

- Use numpy if you want fast batch operations

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


Re: Development tools and practices for Pythonistas

2011-05-06 Thread Jonathan Hartley
On Apr 26, 3:39 pm, snorble  wrote:
> I appreciate any advice or guidance anyone has to offer.

The 'Python Project HOWTO' gives good advice in terms of setting up a
new project, what files and directories to create, what to put in
version control, etc:

http://infinitemonkeycorps.net/docs/pph/

Also, where I work we have tried many IDEs, but happily and
productively use GVim and very little else, so don't feel you *have*
to use an IDE.

Best regards,

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


lightweight way to create new projects from templates

2011-05-11 Thread Jonathan Hartley

Hi.

I'm looking for a quick way to create new Python projects from a template.

I understand that 'Paste' (http://pythonpaste.org/) is one way to do 
this, but I find Paste very intimidating because of all the 
functionality it includes. I'm not even sure whether 'creating new 
projects from templates' is the central goal of Paste, or just an 
incidental benefit that it provides while performing some other task.


As a lightweight alternative, I'm creating a project called 'Genesis'. 
Functionally, it will be little more than a 'cp -r' followed by a few 
search and replace operations.



I realise this idea is modest, but I think it could be helpful for:

a) people who are just getting started with Python, and want to see how 
best to structure their projects, and


b) people who are already up-to-speed, and who want to create their own 
templates and spawn projects from them with a minimum of effort.


c) for the community, to exemplify and publicise best practices for new 
projects.



My goals include:

* Create a new project using the default template:

$ genesis myproj

* or using a particular project template:

$ genesis --template=mytemplate myproj

* Allow people to create their own templates, simply stored as 
directories under '~/.genesis'


* Files in a template contain 'tags' of the form 'G{author}'. When a new 
project is created, these tags are replaced. For example, G{author} will 
be replaced with the value for 'author' supplied either on the command-line:


$ genesis myproj author=Jonathan

Or in your ~/.genesis/config file (a python file read using 'exec'):

author = 'Jonathan'

* The default template should embody good practices like those 
demonstrated in the Python Project howto:

http://infinitemonkeycorps.net/docs/pph/

* The default template should include a setup.py, supporting sdist, 
register and upload commands.


* I'd also like the default template to include things like creating 
Windows binaries, so that it is easier for projects to include this out 
of the box. It would be nice to extend this to binaries for other 
platforms too.


* The default template for command-line applications should include an 
acceptance test, which invokes the script in a new process and makes 
assertions about stdout, stderr and exit value.


* One of my inspirations is to create a good template for events like 
pyweek, where many people set out to write opengl or pyglet applications 
from scratch, and yet every time, many entrants fail to properly create 
binaries, etc.



Genesis is only partially written, so is not yet on PyPI, but is on 
bitbucket:

https://bitbucket.org/tartley/genesis/overview

If you have any feedback, it would be very welcome. In particular:

What do you think about the approach in general?

What do you think about the contents of my default template:
https://bitbucket.org/tartley/genesis/src/tip/genesis/config/default/

Do you think a single default template containing all the above ideas is 
viable? Or is it likely to be so idiosyncratic that I might be better 
shipping with several built-in templates, including a 'minimal' one? Do 
I need different templates for applications than for libraries? For 
console apps vs GUI apps?


What do you think of my choice of 'G{name}' for replaceable tags in the 
template? I wanted something that would not be valid Python, and would 
be unlikely to occur naturally in a project.


Best regards,

Jonathan

--
Jonathan [email protected]://tartley.com
Made of meat.   +44 7737 062 225   twitter/skype: tartley

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


Re: lightweight way to create new projects from templates

2011-05-12 Thread Jonathan Hartley
Hey Chris,

Thanks for the thoughts. I must confess I had already given up on a 'single 
file' approach, because I want to make it easy for people to create their own 
templates, so I have to handle copying a template defined by creating a new 
directory full of diles. If I'm already handling this case, I thought, I might 
as well bundle the built-in default templates as a bunch of files.

I'd hope to provide platform binaries to make download and install easy for 
end-users, regardless of what Python version they already have installed.

Which reminds me, I should make sure the default template works on Python2.x as 
well as 3.x. Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Lies in education [was Re: The "loop and a half"]

2017-10-11 Thread Jonathan Cast
On Wed, 2017-10-11 at 15:14 +0100, bartc wrote:
> On 11/10/2017 14:16, Marko Rauhamaa wrote:
> > Python and C don't try to protect you. In return, you get syntactic
> > convenience that probably enhances the quality of your programs.
> 
> Python, maybe. C syntax isn't as painful as C++ but I still have a lot 
> of trouble with it. (Eg. the variable declaration 'char(*(*x[3])())[5]'. 
> The name of the variable can be found lurking in that lot somewhere, but 
> what's the type?) Not so convenient.

I believe the type of any variable in C is the same as its declaration,
but with the variable name deleted.  So:

char (*(*[3])())[5]

That is, an array of 3 pointers to functions that return pointers to
arrays of 5 characters.

Jonathan

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


Seeking deeper understanding of python equality (==)

2022-05-06 Thread Jonathan Kaczynski
Hi,

I was recently trying to explain how python equality works and ran into a
gap in my knowledge. I haven't found any good pages going beneath a surface
level explanation of python equality comparison.

I'll post my investigations below. What I think I'm looking for is where in
the source code (https://github.com/python/cpython) does the equality
comparison occur. I have an idea but wanted to ask first.


Using the dis module, we see the comparison operator is a single bytecode,
which is expected.

❯ docker run -it --rm ubuntu:jammy
root@919d94c98191:/# apt-get update
root@919d94c98191:/# apt-get --yes install python3
root@919d94c98191:/# cat >play.py <play.py < //play.py(5)()
-> x == y
(Pdb) s
--Call--
> /usr/lib/python3.10/uuid.py(239)__eq__()
-> def __eq__(self, other):


Thank you,
Jonathan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Seeking deeper understanding of python equality (==)

2022-05-13 Thread Jonathan Kaczynski
Thank you for your responses, Sam and Greg.

The do_richcompare function is where my research originally took me, but I
feel like I'm still missing some pieces to the puzzle.

Here is my updated research since you posted your responses (I'll attach a
pdf copy too):
https://docs.google.com/document/d/10zgOMetEQtZCiYFnSS90pDnNZD7I_-MFohSy83pOieA/edit#
The summary section, in the middle, is where I've summarized my reading of
the source code.

Greg, your response here,

> Generally what happens with infix operators is that the interpreter
> first looks for a dunder method on the left operand. If that method
> doesn't exist or returns NotImplemented, it then looks for a dunder
> method on the right operand.

reads like the contents of the do_richcompare function.

What I think I'm missing is how do the dunder methods relate to
the tp_richcompare function?

Thank you,
Jonathan


On Fri, May 6, 2022 at 11:55 PM Greg Ewing 
wrote:

> On 7/05/22 12:22 am, Jonathan Kaczynski wrote:
> > Stepping through the code with gdb, we see it jump from the compare
> > operator to the dunder-eq method on the UUID object. What I want to be
> able
> > to do is explain the in-between steps.
>
> Generally what happens with infix operators is that the interpreter
> first looks for a dunder method on the left operand. If that method
> doesn't exist or returns NotImplemented, it then looks for a dunder
> method on the right operand.
>
> There is an exception if the right operand is a subclass of the
> left operand -- in that case the right operand's dunder method
> takes precedence.
>
> > Also, if you change `x == y` to `y
> > == x`, you still see the same behavior, which I assume has to do with
> > dunder-eq being defined on the UUID class and thus given priority.
>
> No, in that case the conparison method of str will be getting
> called first, but you won't see that in pdb because it doesn't
> involve any Python code. Since strings don't know how to compare
> themselves with uuids, it will return NotImplemented and the
> interpreter will then call uuid's method.
>
> --
> Greg
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Seeking deeper understanding of python equality (==)

2022-05-14 Thread Jonathan Kaczynski
Trying some new searches, I came across slotdefs in ./Objects/typeobject.c,
and those are used in the resolve_slotdups function.

The comment preceding the function says, "Note that multiple names may map
to the same slot (e.g. __eq__, __ne__ etc. all map to tp_richcompare)".

So, I'm still wondering how Py_TYPE(v)->tp_richcompare resolves to __eq__
on a user-defined class. Conversely, my understanding is, for a type
defined in cpython, like str, there is usually an explicitly
defined tp_richcompare function.

Thank you,
Jonathan


On Fri, May 13, 2022 at 8:23 PM Jonathan Kaczynski <
[email protected]> wrote:

> Thank you for your responses, Sam and Greg.
>
> The do_richcompare function is where my research originally took me, but I
> feel like I'm still missing some pieces to the puzzle.
>
> Here is my updated research since you posted your responses (I'll attach a
> pdf copy too):
> https://docs.google.com/document/d/10zgOMetEQtZCiYFnSS90pDnNZD7I_-MFohSy83pOieA/edit#
> The summary section, in the middle, is where I've summarized my reading of
> the source code.
>
> Greg, your response here,
>
>> Generally what happens with infix operators is that the interpreter
>> first looks for a dunder method on the left operand. If that method
>> doesn't exist or returns NotImplemented, it then looks for a dunder
>> method on the right operand.
>
> reads like the contents of the do_richcompare function.
>
> What I think I'm missing is how do the dunder methods relate to
> the tp_richcompare function?
>
> Thank you,
> Jonathan
>
>
> On Fri, May 6, 2022 at 11:55 PM Greg Ewing 
> wrote:
>
>> On 7/05/22 12:22 am, Jonathan Kaczynski wrote:
>> > Stepping through the code with gdb, we see it jump from the compare
>> > operator to the dunder-eq method on the UUID object. What I want to be
>> able
>> > to do is explain the in-between steps.
>>
>> Generally what happens with infix operators is that the interpreter
>> first looks for a dunder method on the left operand. If that method
>> doesn't exist or returns NotImplemented, it then looks for a dunder
>> method on the right operand.
>>
>> There is an exception if the right operand is a subclass of the
>> left operand -- in that case the right operand's dunder method
>> takes precedence.
>>
>> > Also, if you change `x == y` to `y
>> > == x`, you still see the same behavior, which I assume has to do with
>> > dunder-eq being defined on the UUID class and thus given priority.
>>
>> No, in that case the conparison method of str will be getting
>> called first, but you won't see that in pdb because it doesn't
>> involve any Python code. Since strings don't know how to compare
>> themselves with uuids, it will return NotImplemented and the
>> interpreter will then call uuid's method.
>>
>> --
>> Greg
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Seeking deeper understanding of python equality (==)

2022-05-17 Thread Jonathan Kaczynski
Thank you for your response Eryk.

I did try using gdb in my original post, but using breakpoint() left me in
the python layer (pdb), not the cpython layer (gdb) and I couldn't figure
out how to drop down.

I see you're using the kernel32 library, so I assume you're on windows. I
only have a mac and getting gdb functional on a mac seems to be onerous (
https://gist.github.com/mike-myers-tob/9a6013124bad7ff074d3297db2c98247),
so I'm performing my testing on a linux container running ubuntu.

The closest equivalent to putting kernel32.DebugBreak() in front of the x
== y line in my test script is os.kill(os.getpid(), signal.SIGTRAP).
Though, this drops me into the signal handling code in the cpython layer,
and after several dozen steps I did not reach the richcompare-related
functions. I'll make another attempt later today.

Thanks to everyone's responses, I have a great idea of what's going on,
now. I appreciate you all.

My goal now is to be able to work with the debugger, like Erik is, so that
next time I am able to perform this investigation in-full. Should I create
a new thread for this question?

Thank you,
Jonathan



On Sat, May 14, 2022 at 1:51 PM Eryk Sun  wrote:

> On 5/14/22, Jonathan Kaczynski 
> wrote:
> >
> > So, I'm still wondering how Py_TYPE(v)->tp_richcompare resolves to __eq__
> > on a user-defined class. Conversely, my understanding is, for a type
> > defined in cpython, like str, there is usually an explicitly
> > defined tp_richcompare function.
>
> Sometimes it's simplest to directly examine an object using a native
> debugger (e.g. gdb in Linux; cdb/windbg in Windows).
>
> With a debugger attached to the interpreter, create two classes, one
> that doesn't override __eq__() and one that does:
>
> >>> class C:
> ... pass
> ...
> >>> class D:
> ... __eq__ = lambda s, o: False
> ...
>
> In CPython, the id() of an object is its address in memory:
>
> >>> hex(id(C))
> '0x2806a705790'
> >>> hex(id(D))
> '0x2806a6bbfe0'
>
> Break into the attached debugger to examine the class objects:
>
> >>> kernel32.DebugBreak()
>
> (1968.1958): Break instruction exception - code 8003 (first chance)
> KERNELBASE!wil::details::DebugBreak+0x2:
> 7ffd`8818fd12 cc  int 3
>
> Class C uses the default object_richcompare():
>
> 0:000> ?? *((python310!PyTypeObject *)0x2806a705790)->tp_richcompare
>  0x7ffd`55cac288
>  _object*  python310!object_richcompare+0(
> _object*,
> _object*,
> int)
>
> Class D uses slot_tp_richcompare():
>
> 0:000> ?? *((python310!PyTypeObject *)0x2806a6bbfe0)->tp_richcompare
>  0x7ffd`55cdef1c
>  _object*  python310!slot_tp_richcompare+0(
> _object*,
> _object*,
> int)
>
> Source code of slot_tp_richcompare():
>
>
> https://github.com/python/cpython/blob/v3.10.4/Objects/typeobject.c#L7610-L7626
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Persistent Error: Python was not found

2022-08-14 Thread Jonathan Owah
Good day,
Great job on making Python easily accessible.

I'm using a  Windows 10, 64gb HP EliteBook.

I've been trying to configure my laptop to run python scripts.
This is the error I keep getting:
Python was not found; run without arguments to install from the Microsoft
Store, or disable this shortcut from Settings > Manage App Execution
Aliases.

Everything I've tried has failed.
I've uninstalled and reinstalled
I've added to path, both user and system path,manually and from fresh
installation
I've downloaded from Microsoft Store
I've gone to manage app aliases and switched off
I've used Git Bash, Powershell, cmd

I'm able to check my python version: 3.10.6.

I can't do anything else and it's really frustrating.

I've been at it for days, I don't know what else to do.

Thanks in advance for your help.

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


Re: Persistent Error: Python was not found

2022-08-15 Thread Jonathan Owah
Thank you so much for your assistance .

The fault was actually mine: I was running a command
with python3, instead of just python.
python3 works for Mac, but not Windows.

I'm fairly new to Python so I was just following along a
tutorial, and I didn't take note of the fact that the command
didn't work because the tutorial was done on a MacBook,
while I'm using a Windows device.

Thanks for your help,
Regards

On Mon, Aug 15, 2022 at 8:14 AM Eryk Sun  wrote:

> On 8/13/22, Jonathan Owah  wrote:
> >
> > I've been trying to configure my laptop to run python scripts.
> > This is the error I keep getting:
> > Python was not found; run without arguments to install from the Microsoft
> > Store, or disable this shortcut from Settings > Manage App Execution
> > Aliases.
>
> If you keep seeing this message, then the shell is finding and running
> Microsoft's default "python.exe" redirector app execution alias that's
> located in "%LocalAppData%\Microsoft\WindowsApps". By default, this
> directory is set at the beginning of the user "Path" value in the
> registry and thus takes precedence (but not over the system "Path").
> Confirm this by running `where.exe python`.
>
> An app execution alias is a special type of filesystem symbolic link
> to a store app's executable. These aliases are created in a user's
> "%LocalAppData%\Microsoft\WindowsApps" directory. Store apps
> themselves are usually installed in "%ProgramFiles%\WindowsApps",
> which is a system managed directory that even administrators can't
> easily modify (and shouldn't modify). Each user on a system has their
> own set of installed store apps, even though the apps are installed
> only once at the system level.
>
> By default, Windows creates "python.exe" and "python3.exe" aliases for
> the "App Installer" PythonRedirector app. In the alias manager, these
> two will be clearly listed as aliases for "App Installer". If you run
> this redirector app with one or more command-line arguments, it will
> print the above quoted message to the console. If the redirector app
> is run without arguments, it will open the Microsoft Store to install
> the latest version of the Python store app distribution. Currently
> that means Python 3.10.
>
> In my experience, the app execution alias manager component of Windows
> is unreliable. A disabled alias might still exist in
> "%LocalAppData%\Microsoft\WindowsApps", or an old alias might be left
> in place when an app is installed.  Once the real Python store app is
> installed, go back into the alias manager and toggle the "python.exe"
> and "python3.exe" aliases off and back on. If that doesn't resolve the
> problem, manually delete the "python.exe" and "python3.exe" aliases
> from "%LocalAppData%\Microsoft\WindowsApps". Then toggle them off and
> on again in the alias manager. Hopefully they'll be created to
> correctly alias the real Python app instead of the "App Installer"
> redirector.
>
-- 
https://mail.python.org/mailman/listinfo/python-list


OT: Accessibility: Jana Schroeder's Holman Prize Application

2021-05-10 Thread Jonathan Fine
Perhaps Off Topic, but for a good cause.

This year I met Jana Schroeder, a blind person forced to change jobs as part of 
the social cost of Covid. Her outsider experience of computer coding training 
became a wish to make things better. She has applied for a Holman Prize 
($25,000 over a year) to fund this. She's also set up a survey to reach and 
know better those with similar wishes.

One simple way to help open to many is to volunteer to be a sighted helper for 
a code and math variant of BeMyEyes.org. I encourage you to listen to Jana's 
pitch for a Holman prize, and if you want to help complete the survey (whether 
you're blind or sighted, code or math, young or old). I've learnt a lot about 
accessibility from Jana.

Jana Schroeder's Holman pitch (90 seconds):
https://www.youtube.com/watch?v=3ywl5d162vU

Jana Schroeder's survey (15 minutes):
https://tinyurl.com/blindcodersurvey

Finally, The Holman Prize:
https://holman.lighthouse-sf.org/

best regards

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


Using PIP in Python 3.10 on Windows 10

2022-01-14 Thread Jonathan Gossage
I have installed Python 3.10.1 on Windows 10 using the recommended Windows
Installer. When I try to access PIP from the command line, I get the
following result, even though Python itself is accessible.


C:\Users\jgoss>python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()

C:\Users\jgoss>pip install pip --upgrade
Fatal error in launcher: Unable to create process using '"C:\Program
Files\Python310\python.exe"  "C:\Program Files\Python310\Scripts\pip.exe"
install pip --upgrade': The system cannot find the file specified.
During the installation, I chose to install Python in a non-default
location and to set the Environment variables.
 The result of this attempt is shown below:


C:\Users\jgoss>python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()

C:\Users\jgoss>pip install pip --upgrade
Fatal error in launcher: Unable to create process using '"C:\Program
Files\Python310\python.exe"  "C:\Program Files\Python310\Scripts\pip.exe"
install pip --upgrade': The system cannot find the file specified.

It looks as if the launcher is expecting to find Python installed at
c:\Program Files\Python3.10 whereas it has actually been installed at
D:\Users\jgoss\AppData\local\python\python3.10. It seems that the launcher
has not been updated to the latest installation location for python and
that it also needs to handle a non-default install location. The same
problem occurs if I take the install option to install to the default
location.
Is there any workaround as PIP is essential to my environment?

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


Re: Why not allow empty code blocks?

2016-07-24 Thread Jonathan Hayward
I might point out something that captures something about pass: it is made
to do extra duty in web2py, which is meant to shield students from some of
the more preventable complexities of a LUM CLI, and you don't easily indent
code the way that Vim and Emacs, let alone a full IDE besides the IDE
nature of web2py, forces people to deal with. People getting up to speed
with Vim or Emacs, for anything that is usually indented, find and/or seek
out how to maintain indentation without hitting the space bar 4/8/12/...
times at the beginning of the next line.

And it works. It was probably not intended or even anticipated in the
design of Python, but my understanding is that the adapted usage works well.

And if I may put on asbestos longjohns, there is no reason I am aware of
why syntactic sugar, in the style of Python-influenced Coffeescript, could
not be modified to make "from __future__ import braces" represent a live
and active feature. The net effect of significant whitespace in Python is
that it provides one model to "say what you mean and mean what you say",
and everybody who understands the language recognizes the beginning and end
of a block, the end of a statement, a noop, etc. And this is pretty much
the job description of C-family language syntax etc. Now there are
advantages, namely no braces and no brace holy wars either, fewer Perlish
sigils as even a statement is usually ended by line break, and so on. But
Python-style and C-style syntax alike provide an unambiguous tool to
specify what you want, and an unambiguous means of interpreting what was
actually said. The non-C-style syntax was the biggest "steaming pile of
dinosaur dung" hurdle before ESR appreciated the language, but Python
syntax and C-style syntax are both adequate. They may or may not be equal,
but every instance of code which unambiguous Pythonic syntax is underscored
is an effect that should usually be equally easy to implement with C-style
syntax.

On Sun, Jul 24, 2016 at 1:14 PM, BartC  wrote:

> On 24/07/2016 15:51, Chris Angelico wrote:
>
>> On Mon, Jul 25, 2016 at 12:44 AM, BartC  wrote:
>>
>>> Your attention is diverted, you're doing something on your desk, but you
>>> hit
>>> one of the keys by mistake. You might have pressed Delete or you might
>>> not.
>>> You look at the screen which has a 5000-line program open, and you see
>>> this
>>> (borrowing your example and with the cursor at "_"):
>>>
>>>  def f():
>>>  for x in seq:
>>>  do_this()
>>>  do_that()
>>>  _   do_more()
>>>
>>> Did you just unindent do_more(), or is that where it's meant to be? Undo
>>> may
>>> or may not help (or it may undo something is needed).
>>>
>>
>> Undo, redo. See what happened. Easy.
>>
>> Also, if you're regularly committing to source control, you can always
>> check the diff. Before you 'git commit', check what 'gitk' shows, or
>> before 'hg commit', have a glance at 'hg diff'. Make sure what you're
>> seeing is what you intend to change. Remember, code doesn't just
>> accidentally change; everything should have purpose, including
>> (especially) any indent/unindent.
>>
>> Source control protects you from everything other than multiple
>> changes since the last commit. So commit often. It'll save you a lot
>> of time - if not coding time, then debating-on-python-list time. :)
>>
>
> OK. I understand that it is not possible to point out any kind of weakness
> of a language (any language not just Python!) because the counter-argument
> is always going to be about:
>
> Use syntax highlighting, use a smart editor, use a version control system,
> use a linter, use 'tabnanny', use tool X, Y or Z to get around the
> problems, use obscure language options..
>
> The thing is, if everyone does depend more on such tools, then it really
> doesn't matter exactly what the language does - the tools will take care of
> such details. So the language could delimit blocks using any scheme it
> likes, including use 'end', 'else' and so on.
>
> It only becomes important to people like me who use plain editors.
>
> --
> Bartc
>
>
> --
> Bartc
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
[image: Christos Jonathan Seth Hayward] <https://cjshayward.com/>
*Jonathan Hayward*, a User Experience professional.

Email  • Flagship  <https://cjshayward.com/>Website
<https://cjshayward.com/> • Github
<https://github.com/jonathanhayward> • *LinkedIn
<http://www.linkedin.com/in/jonathanhayward>* • Portfolio + More
<http://jonathanhayward.com/> • *Recent Title
<https://www.packtpub.com/application-development/reactive-programming-javascript>*
 • Skills <http://jsh.name/>

Loads of talent and a thorough grounding in all major academic disciplines
supporting User Experience.
-- 
https://mail.python.org/mailman/listinfo/python-list


Boolean comparison & PEP8

2019-07-28 Thread Jonathan Moules

Hi List,
Lets say I want to know if the value of `x` is bool(True).
My preferred way to do it is:

if x is True:
    pass

Because this tests both the value and the type.

But this appears to be explicitly called out as being "Worse" in PEP8:

"""
Don't compare boolean values to True or False using ==.

Yes:   if greeting:
No:    if greeting == True:
Worse: if greeting is True:
"""

Why?

If `x` can also have a value of "1"(str) or 1(int) then in both cases 
this would be a false positive if I were to do the below as they'll both 
equate to True:


if x:
    pass

The PEP for boolean type (285 - 
https://www.python.org/dev/peps/pep-0285/) doesn't mention the "is" 
comparison keyword at all as far as I can tell.


What am I missing?
Thanks



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


Re: Python/SQLite best practices

2019-08-05 Thread Jonathan Moules
Some gotcha tips from using SQLite with Python that I've encountered. 
You may already know some/all of these:


* SQLite doesn't have a "Truncate" function - simply delete the file if 
possible for larger datasets.
* Explicitly committing is good because the default python sqlite3 
library does it randomly and implicitly. I found that doing it only when 
the database is dettaching or closing speeds things up a lot.
* SQLite 3 only considers up to 64bits an INTEGER. So if you want to 
insert a 128bit string you have to use Python string substitution (i.e. 
"Hello %s") rather than the SQLite variable substitution "insert into 
tab values (?)"
* To be reliably INSERTed Byte data should be first converted to 
sqlite3.Binary(my_data) explicitly
* By default Foreign Keys are not enforced. Enable them at connection 
time if you care about referential integrity!
* It's typically opaque as to where the install of SQLite is that the 
library is using and it's very hard and not-documented as to how to 
update the SQLite version that Python is using.


If you want an even thinner wrapper around SQLite there's APSW ( 
https://rogerbinns.github.io/apsw/index.html ) - I've never used it 
myself but it's useful to know about. There's a page with differences - 
https://rogerbinns.github.io/apsw/pysqlite.html#pysqlitediffs



On 2019-08-05 22:43, David Raymond wrote:

"What's the advantage of this over letting the connection object do
that for you? As the context manager exits, it will automatically
either commit or roll back. If you want to guarantee closing _as
well_, then you can do that, but you can at least use what already
exists."

After review I guess I should have phrased it more as a "here's what I've found for 
reference" rather than a "here's what _you_ should do"


Part of it is large use of the Command Line Interface for SQLite, and similar 
command line tools for other db's, which all work in autocommit mode by 
default, so that's how my brain is now wired to think about executing things.

The context manager transaction feature I can see using, and might actually 
start switching to it as it's explicit enough. Though oddly, __enter__ doesn't 
seem to actually begin a transaction, not even a deferred one. It's only 
__exit__ that either commits or rolls back.
(Eh, it'd "probably" be simple enough to subclass Connection so that __enter__ 
and __exit__ work properly no matter the isolation_level. Famous last words)

The implicit stuff I hated because it never seemed straightforward enough. Especially since there used to be 
implicit commits as well as implicit begins ("Changed in version 3.6: sqlite3 used to implicitly commit 
an open transaction before DDL statements. This is no longer the case.") Maybe because I was new to both 
Python and SQLite at the time, but there was a lot of "stop doing hidden stuff I didn't tell you 
do" getting muttered, along with others like "why do I need to commit when I never did a 
begin?" The documentation on it is all of 1 sentence, so there was a lot of trial an error going on.
"The Python sqlite3 module by default issues a BEGIN statement implicitly before a 
Data Modification Language (DML) statement (i.e. INSERT/UPDATE/DELETE/REPLACE)."


"(Also, I'd definitely use conn.commit() rather than
cur.execute("commit"), in case there's extra functionality in the
commit method.)"

True. I know for example that if you try to rollback when not in a transaction that 
cur.execute("rollback;") will raise an exception whereas conn.rollback() will 
quietly suppress it for you. So there might be similarly useful stuff in .commit()
sqlite3 is (almost) all C though, so there'd be noticeably more digging and 
decyphering required to check. (For me anyway)



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


Re: Python/SQLite best practices

2019-08-06 Thread Jonathan Moules



* To be reliably INSERTed Byte data should be first converted to 
sqlite3.Binary(my_data) explicitly


Interesting. Is that Python 2 specific, or also in Python 3. Because 
the latter would surprise me (not saying it isn't the case).
Only tried on Python 3. I'm inserting raw byte versions of web-pages. I 
forget the exact details but I have a comment in my code to the above 
effect so I must have ended up bashing my head against it at some point. 
A zip/lzma compressed byte output doesn't need this wrapper though.


On a UNIX system the command "lsof -p pid-of-running-python-process" 
should show the path of the sqlite library that is linked to the 
Python executable, which should let you learn this.


Thanks, I'll have to add that to the collection. I know when I was 
googling around for it on *nix I came across a bunch of different 
answers none of which seemed to work. Windows is a simple matter of 
replacing the right DLL (though again not really documented). I still 
have no idea how to do it on *nix but that's another thread.


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


Re: [Python-Dev] [RELEASE] Python 3.7.7rc1 is now available for testing

2020-03-04 Thread Jonathan Goble
On Wed, Mar 4, 2020 at 1:02 PM Ned Deily  wrote:

> Details here:
>
>
> https://discuss.python.org/t/python-3-7-7rc1-is-now-available-for-testing/3638


"Assuming no critical problems are found prior to *2020-02-10*..."

I would like to know how you expect people to travel back in time to report
problems. :P
-- 
https://mail.python.org/mailman/listinfo/python-list


Asynchronous generators

2020-06-23 Thread Jonathan Gossage
-- 
I am attempting to learn how to use asyncio and I have been unable to find
any documentation or Internet posts that give information on the principles
underlying asyncio, let alone any examples showing how asynchronous
generators should be used. I have built a toy program to test trying to
read a text file, line by line, and process each line asynchronously. I
wind up with a message complaining that my asynchronous generator is not a
coroutine. I am posting a copy of my program here along with the results
from trying to run it. I am hoping for some suggestions about how to fix
it.  I am using Python 3.8.3 on Ubuntu 20.04

import asyncio
> from pathlib import Path
> import timeit
>
> async def _read_password() -> str:
> with Path('/etc/passwd').open() as f:
> line: str = f.readline()
> yield line
> print('read_password has read everything')
>
> async def main(_rp) -> int:
> line = ''
> async for line in _rp:
> try:
> print(f'Got line {line}')
> except StopAsyncIteration:
> line = None
> _rp.aclose()
> return 0
>
> if __name__ == '__main__':
> _loop = asyncio.get_event_loop()
> _rp = _read_password()
> _m = main(_rp)
> _loop.create_task(_m)
> _loop.create_task(_rp)
> timeit.Timer(_loop.run_until_complete()).timeit()
> _loop.close()


Here is the output:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
  File
"/home/jgossage/EclipseWorkspaces/GlobalVillage/Infrastructure/Play/Async/passwd.py",
line 34, in 
_loop.create_task(_rp)
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 431, in
create_task
task = tasks.Task(coro, loop=self, name=name)
TypeError: a coroutine was expected, got 
-- 
https://mail.python.org/mailman/listinfo/python-list


Function type in typing

2020-07-08 Thread Jonathan Gossage
How could I type a function that returns a generic type? For example, I use
a function that expects another function as an argument. This second
function must return a datatype or a collection. I assume that I could
use *Callable[...,
return type]* and I have no way to specify the return type.

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


Typing modules

2020-07-20 Thread Jonathan Gossage
I have the following code and I would like to type the variable *contents*:

contents: something = importlib._import_module(name)


I have been unable to find out what *something* should be.

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


Re: how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread Jonathan LaCour
> class Spam(object):
> cache = {}
> def __new__(cls, x):
> if cls.cache.has_key(x):
> return cls.cache[x]
> def __init__(self, x):
> self.x = x
> self.cache[x] = self
>
> a = Spam('foo')
> b = Spam('foo')
>
> Well, in this case a and b are identical... to None!  I assume this is
> because the test in __new__ fails so it returns None, I need to then
> create a new Spam.. but how do I do that without calling __new__  
> again?
> I can't call __init__ because there's no self...
>
>

Oops, you forgot to return object.__new__(cls, x) in the case the  
object isn't in the cache.  That should fix it.

Jonathan
http://cleverdevil.org


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


C replacement for Queue module

2005-10-21 Thread Jonathan Ellis
I'm working on an application that makes heavy use of Queue objects in
a multithreaded environment.

By "heavy" I mean "millions of calls to put and get, constituting ~20%
of the app's run time."  The profiler thinks that a significant amount
of time is spent in this code -- not just a consumer waiting for a
producer, but actual _empty, notify calls, etc.

Would it be worth the time to write a CQueue module with pthread_cond
instead of Python Condition objects, etc?  I don't really have a gut
feeling for how much bang-for-the-loc C optimization might give: twice
as fast?  5x as fast?

thanks,

-Jonathan

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


Re: C replacement for Queue module

2005-10-21 Thread Jonathan Ellis
[EMAIL PROTECTED] wrote:
> does collections.deque have a blocking popleft()?  If not, it's not very
> suitable to replace Queue.Queue.

It does not.

-Jonathan

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


Re: How to get started in GUI Programming?

2005-11-26 Thread Jonathan Gardner
I would argue with your assertion that either TKinter of PyGTK are the
best. There are several other good alternatives, including wxPython and
PyQt, which are very comparable, if not better.

I would strongly suggest starting with PyQt. It's my personal favorite.
I wrote a short tutorial on the Python Wiki.

http://wiki.python.org/moin/JonathanGardnerPyQtTutorial

I find that the Qt API is a lot simpler than the alternatives. Plus, it
has great documentation.

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


Re: How can I do this in Python?

2005-11-27 Thread Jonathan Gardner
I don't know what engine you are using. Basically all you have to do is
render the login form at the url of the page that needs the login.
You're going to have to hook in the authentication code before you
render the page normally. For instance, this is a common way I've been
doing it in various engines I use:

(1) Check to see if the user is logging in by looking at the
parameters. If they login successfully, record that in the session and
continue as normal.

(2) Normal operation being... check to see if they are logged in
according to their session. (The code in (1) will set up their session
to be logged in if they just logged in.) If they aren't and they need
to be, show the login page. Otherwise, show the regular content.

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


What could 'f(this:that=other):' mean?

2005-01-05 Thread Jonathan Fine
Giudo has suggested adding optional static typing to Python.
(I hope suggested is the correct word.)
  http://www.artima.com/weblogs/viewpost.jsp?thread=85551
An example of the syntax he proposes is:
> def f(this:that=other):
> print this
This means that f() has a 'this' parameter, of type 'that'.
And 'other' is the default value.
I'm going to suggest a different use for a similar syntax.
In XML the syntax
>  
is used for name spaces.
Name spaces allow independent attributes to be applied to an
element.  For example, 'fo' attributes for fonts and layout.
XSLT is of course a big user of namespaces in XML.
Namespaces seems to be a key idea in allow independent
applications to apply attributes to the same element.
For various reasons, I am interested in wrapping functions,
and supplying additional arguments.  Namespaces would be
useful here.  Decorators, by the way, are ways of wrapping
functions.  Namespaces might make decorators a bit easier
to use.
Here's an example of how it might work.  With f as above:
> f(this:that='value')
{'that': 'value'}
Do you see?  The syntax of def makes 'this' a dictionary.
And the syntax of the call adds an item to 'this'.
This aligns nicely with XML syntax and semantics.
One could extend **kwargs similarly.
> def g(***nsargs):
> print ***nsargs
>
> g(this:that='other', that:this='more')
{'this': {'that': 'other'}; {'that': {'this': 'more'}}
All the namespace args are gathered into a dict of dicts.
Thus, this suggestion is mostly syntactic sugar for
f(this=dict(that='other), that=dict('this'=other))
(Have I got this right? - I'm only up to Python 2.2 at
home.  This is how I remember 2.4.)
Back to optional static typing.  A common idiom is
>  def return_dict(data=None):
>  if data is None:
>   data = {}
# etc
This avoid the newbie gotcha in
>  def return_dict(data={}:
>   # etc
So to write this using the suggested syntax one has:
>  def return_dict(data:dict=None):
>   # oops!
So now some final comments.
1.  I have no opinion yet about optional static typing.
2.  Calls of function f() should outnumber definitions of f().
(I'm not totally convinced of this - for example __eq__ may
be defined in many classes, but called by few functions.
Frameworks often have functions as parameters.)
3.  Granted (2), perhaps function calls are first in the
queue for syntactic sugar.
--
Jonathan
http://www.pytex.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: What could 'f(this:that=other):' mean?

2005-01-05 Thread Jonathan Fine
Jeff Shannon wrote:
Jonathan Fine wrote:
Giudo has suggested adding optional static typing to Python.
(I hope suggested is the correct word.)
  http://www.artima.com/weblogs/viewpost.jsp?thread=85551
An example of the syntax he proposes is:
 > def f(this:that=other):
 > print this

I'm going to suggest a different use for a similar syntax.
In XML the syntax
 >  
is used for name spaces.
Name spaces allow independent attributes to be applied to an
element.  For example, 'fo' attributes for fonts and layout.
XSLT is of course a big user of namespaces in XML.
Namespaces seems to be a key idea in allow independent
applications to apply attributes to the same element.
[...]
Here's an example of how it might work.  With f as above:
 > f(this:that='value')
{'that': 'value'}

I fail to see how this is a significant advantage over simply using 
**kwargs.  It allows you to have multiple dictionaries instead of just 
one, that's all.  And as you point out, it's trivial to construct your 
own nested dicts.
This argument could be applied to **kwargs (and to *args).  In other
words, **kwargs can be avoided using a trivial construction.
>>> def f_1(**kwargs): print kwargs
...
>>> f_1(a=3, b=4)
{'a': 3, 'b': 4}
>>>
>>> def f_2(kwargs): print kwargs
...
>>> f_2({'a':3, 'b':4})
{'a': 3, 'b': 4}
(and in Python 2.3)
>>> f_2(dict(a=3, b=4))
{'a': 3, 'b': 4}
f_1() is internally the same as f_2(), but the argument passing is
different.
Jeff, are you in favour of kwargs as a language feature?  If so, you
may wish to refine your argument.
(One can be in favour of kwargs and against my proposal.  That kwargs
is widely used, and my proposal would not be, is a good argument, IMO.)
I'll post some usage examples later today, I hope.

Besides, Python already uses the concept of namespaces by mapping them 
to object attributes.  Module references are a namespace, exposed via 
the attribute-lookup mechanism.  This (IMO) fails the "there should be 
one (and preferably only one) obvious way to do things" test.  The 
functionality already exists, so having yet-another way to spell it will 
only result in more confusion.  (The fact that we're borrowing the 
spelling from XML does little to mollify that confusion.)

Here, I don't understand.  Could you give an example of two obvious ways
of doing the same thing, should my suggestion be adopted?
--
Jonathan
http://www.pytex.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: HTML purifier using BeautifulSoup?

2005-01-07 Thread Jonathan Clark
Dan Stromberg wrote:
> Has anyone tried to construct an HTML janitor script using
BeautifulSoup?
>
> My situation:
>
> I'm trying to convert a series of web pages from .html to palmdoc
format,
> using plucker, which is written in python.  The plucker project
suggests
> passing html through "tidy", to get well-formed html for plucker to
work
> with.
>
> However, some of the pages I want to convert are so bad that even
tidy
> pukes on them.
>
> I was thinking that BeautifulSoup might be more tolerant of really
bad
> html...  Which led me to the question this article started out with.
:)
>
> Thanks!

I have used BeautifulSoup for screen scraping, pulling html into
structured form (using XML). Is that similar to a janitor script? I
used it because tidy was puking on some html. BS has been excellent.

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


Re: What could 'f(this:that=other):' mean?

2005-01-07 Thread Jonathan Fine
Jonathan Fine wrote:

I'll post some usage examples later today, I hope.

Well, here are some examples.  A day later, I'm afraid.
** Pipelines and other composites
This is arising for me at work.
I produce Postscript by running TeX on a document.
And then running dvips on the output of TeX.
TeX as a program has parameters (command line options).
As does dvips.
For various reasons I wish to wrap TeX and dvips.
  def tex_fn(filename, input_path=None, eps_path=None):
'''Run tex on filename.
Search for input files on input_path.
Search for EPS files on eps_path. '''
pass
  def dvips_fn(filename, page_size=None, color_profile=None):
'''Run dvips on filename.  etc.'''
pass
In reality, these tex and dvips have many options.
More parameters will be added later.
So now I wish to produce a composite function, that runs
both tex and dvips.  And does other glue things.
  def tex_and_dvips_fn(filename,
tex:input_path=xxx,
dvips:color_profile=yyy):
# glueing stuff
tex_fn(filename, **tex)
# more glueing stuff
dvips_fn(filename, **dvips)
To avoid a name clash, we use 'tex' for the parameter
space, and 'tex_fn' for the function that takes 'tex'
as parameter.
The point is that parameters can be added to tex_fn and
dvips_fn without our having to change tex_and_dvips_fn
**  Wrapping functions
This is the problem that originally motivated my
suggestion.
We have coded a new function.
  def adder(i, j):  return i + j
We wish to test 'adder'.
But unittest is too verbose for us.
We wish to define a decorator (?) test_wrap to
work as follows.
  orig_adder = adder
  adder = test_wrap(adder)
  new_adder = adder
orig_adder(i, j) and new_adder(i, j) to be
effectively identical - same return, same side
effects, raise same exceptions.
So far,
  def test_wrap(fn): return fn
does the job.
But now we want
  new_adder(2, 2, returns=4)
  new_adder(2, '', raises=TypeError)
to be same as
  orig_adder(2, 2)
  orig_adder(2, '')
(which can be achieved by ignoring 'returns' and 'raises'.
The idea here is that we can call
  adder = new(adder)
early on, and not break any working code.
And we also want
  new_adder(2, 2, 5)
  new_adder('', '', raises=TypeError)
to raise something like an AssertionError.
OK - so I have an informal specification of test_wrap.
Its clear, I think, that test_wrap must be something like
  def test_wrap(fn):
def wrapped_fn(*args, **kwargs):
test_args = {}
# transfer entries from one dict to another
for key in ('returns', 'raises'):
if kwargs.has_key(key):
test_args[key] = kwargs[key]
del kwargs[key]
result = fn(args, kwargs)
if test_args.has_key(result):
 assert test_args['result'] = result
(I've not coded testing for 'raises'.)
Now, the more parameters added by the test_wrap function,
the more the chance of a name clash.
So why not reduce the chances by using name spaces.
One possible namespace syntax is:
  new_adder(2, 3, test=dict(returns=5))
Another such syntax is:
  new_adder(2, 3, test:returns=5)
Each has its merits.
The first works with Python 2.4.
The second is, in my opinion, easier on the eye.
Anyway, that's my suggestion.
Jonathan
--
http://mail.python.org/mailman/listinfo/python-list


Re: What could 'f(this:that=other):' mean?

2005-01-07 Thread Jonathan Fine
Jeff Shannon wrote:
Jonathan Fine wrote:

The use of *args and **kwargs allows functions to take a variable number 
of arguments.  The addition of ***nsargs does not add significantly.  
I've posted usage examples elsewhere in this thread.
I think they show that ***nsargs do provide a benefit.
At least in these cases.
How significant is another matter.  And how often do they occur.

Now, though, we've lost the ability to specify *only* the argname and 
not the namespace as well -- that is, you *cannot* call f4 with keywords 
but not namespaces.  From the caller's vantage point, this means that 
they need to know the full namespace spec of the function, which makes 
it no different than simply using longer (but unique) keyword names.
This is a major point.
From the caller's point of view:
  fn_1(x_aa=1, x_bb=2, y_aa=3)
  fn_2(x:aa=1, x:bb=2, y:aa=3)
are very similar.  Replace '_' by ':'.
So you are saying, I think, 'what does this buy the caller'.
Well, by using ':' the namespacing is explicit.
  fn_3(my_path, my_file, any_file)
Is this an example of implicit namespacing? (Rhetorical question.)
Explicit is better than implicit, I'm told (smile).

So, we can see that allowing namespaces and ***nsargs doesn't add any 
utility from the caller's perspective.  How much does the callee gain 
from it?

Well, the following functions would be equivalent:
def g1(arg1, arg2, arg3, arg4):
ns1 = {'arg1':arg1, 'arg2':arg2, 'arg3':arg3, 'arg4':arg4}
return ns1
def g2(ns1:arg1, ns1:arg2, ns1:arg3, ns1:arg4):
return ns1
You might say "Wow, look at all that repetetive typing I'm saving!" But 
that *only* applies if you need to stuff all of your arguments into 
dictionaries.  
This is a key point.  But there's more to it.
Namespace arguments are good if you have to divide the arguments into
two or more dictionaries.  Based on the namespace prefix.
I suspect that this is a rather small subset of 
functions.  In most cases, it will be more convenient to use your 
arguments as local variables than as dictionary entries.
This is a matter of usage.  If the usage is very, very small, then
what's the point.  If the usage is very, very large, then the case
is very strong.
def gcd1(a, b):
while a:
a, b = b%a, a
return b
def gcd2(ns1:a, ns1:b):
while ns1['a']:
ns1['a'], ns1['b'] = ns1['b']%ns1['a'], ns1['a']
return ns1['b']
Speaking of repetetive typing :P
Besides, another function equivalent to the above two would be:
def g3(arg1, arg2, arg3, arg4):
ns1 = locals()
return ns1
which is quite a bit less repetetive typing than the 'namespace' 
version.
These are not good examples for the use of namespacing.
And the results are horrible.
So this is an argument _in favour_ of namespacing.
Namely, that it _passes_ "there should be one (and preferably only one) 
obvious way to do things" test (quoted from your earlier message).


So, you're looking at making the function-calling protocol significantly 
more complex, both for caller and callee, for the rather marginal gain 
of being able to get arguments prepackaged in a dictionary or two, when 
there already exist easy ways of packaging function arguments into a 
dict.  Given the deliberate bias against adding lots of new features to 
Python, one needs a *much* better cost/benefit ratio for a feature to be 
considered worthwhile.
I believe that we _agree_, that it is a matter of cost/benefit ratio.
My opinion is that studying usage examples is a good way of evaluating
this ratio.  Which is why I have posted some favourable usage examples.

I'd note also that the usage you're drawing from, in XML/XSLT, isn't 
really comparable to function parameters.  It's a much closer parallel 
to object attributes.  Python *does* have this concept, but it's spelled 
differently, using a '.' instead of a ':'.  In other words, the XML 
fragment you give,


 would be more appropriate to render in Python as
e = Element()
e.this.that = 'other'
It's quite reasonable to suppose that some object of type Element may 
have a set of font attributes and a set of image attributes, and that 
some of these may have the same name.  Python would use font objects and 
image objects instead of 'namespaces' --

e.font.size = '14pt'
e.image.size = (640, 480)
So while these namespaces are probably a great thing for XML, XSLT, 
they're not very useful in Python.  Which, given the rather different 
goals and design philosophies behind the languages, shouldn't really be 
much of a surprise.
It may be better, in some cases, to write
  fp = FontParams()
  fp.size = 14

Re: What could 'f(this:that=other):' mean?

2005-01-11 Thread Jonathan Fine
Nick Coghlan wrote:
If the caller is meant to supply a namespace, get them to supply a 
namespace.

def f(ns1, ns2):
  print ns1['a'], ns1['b'], ns2['a'], ns2['b']
f(ns1 = dict(a=1, b=2), ns2 = dict(a=3, b=4))
Hey, where's Steve? Maybe his generic objects should be called 
namespaces instead of bunches. . .

def f(ns1, ns2):
  print ns1.a, ns1.b, ns2.a, ns2.b
f(ns1 = namespace(a=1, b=2), ns2 = namespace(a=3, b=4))

Basically, there are three main possibilities.
  f_1(ns1=dict(a=1, b=2), ns2=dict(a=3, b=4))
  f_2(ns1_a=1m, ns1_b=2, ns2_a=3, ns2_b=4)
  f_3(ns1:a=1m, ns1:b=2, ns2:a=3, ns2:b=4)
f_3 is the suggested extension to Python.
f_3 is similar to f_2 for the caller of f_3.
f_3 is similar to f_1 for the implementor of f_3.
Nick points out that a key issue is this:  Is the user meant
to supply arguments belonging to a namespace?
I'm not, at this time, wishing to promote my suggestion.
If I were, I would be well advised to find usage cases.
Rather, I simply wish to point out that the
  f(this:that=other)
syntax may have uses, other than optional static typing.
And this I've done.  So for me the thread is closed.
Jonathan

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


Re: how to write a tutorial

2005-01-24 Thread Jonathan Burd
Xah Lee wrote:
adding to my previosu comment...

*plonk*
--
"Women should come with documentation." - Dave
--
http://mail.python.org/mailman/listinfo/python-list


Re: best way to do a series of regexp checks with groups

2005-01-24 Thread Jonathan Fine
Mark Fanty wrote:
In perl, I might do (made up example just to illustrate the point):
if(/add (\d+) (\d+)/) {
  do_add($1, $2);
} elsif (/mult (\d+) (\d+)/) {
  do_mult($1,$2);
} elsif(/help (\w+)/) {
  show_help($1);
}
or even
do_add($1,$2) if /add (\d+) (\d+)/;
do_mult($1,$2) if /mult (\d+) (\d+)/;
show_help($1) if /help (\w+)/;

Here's some Python code (tested).
It is not as concise as the Perl code.
Which might or might not be a disadvantage.
Sometimes, regular expressions are not the right thing.
For example, a simple str.startswith() might be better.
What about "add 9 999"?
Maybe we want to catch the error before we get to the do_add.
Can't easily do that with regular expressions.
And what about a variable number of arguments.
If regular expressions are no longer used, the Perl code seems
to loose some of its elegance.
I've been arguing for writing small, simple functions that do something.
This should make testing much easier.
These functions might happen to use regular expressions.
The code below is clearly more flexible.
It's easy, for example, to add a new command.
Just add an entry to dispatch.
The thing I like best about it is the passing of a dict.
===
#!/usr/bin/python
import re
# here we know about functions and patterns
def do_add(arg1, arg2): print "+ %s %s" % (arg1, arg2)
def do_times(arg1, arg2): print "* %s %s" % (arg1, arg2)
add_re = re.compile(r'add (?P.*) (?P.*)')
times_re = re.compile(r'times (?P.*) (?P.*)')
def find_add(str):
match = add_re.match(str)
if match is None:
return match
return match.groupdict()
def find_times(str):
match = times_re.match(str)
if match is None:
return match
return match.groupdict()
# here we bind everything together
dispatch = [
(find_add, do_add),
(find_times, do_times),
]
def doit(str):
for (find, do) in dispatch:
d = find(str)
if d is not None:
return do(**d)
return None # or error
if __name__ == '__main__':
doit('add this that')
doit('times this that')
===
Jonathan
--
http://mail.python.org/mailman/listinfo/python-list


Re: gnuplot on Canvas widget

2005-01-27 Thread Jonathan Ellis
Blues wrote:
> I have used two great models - Tkinter and Gnuplot.py - for a while.
I
> can display an image on a Canvas widget using Tkinter and I can also
> generate a gnuplot from Python on the fly in a separate window.  Does
> anyone know how to display such a gnuplot on the Canvas widget with
an
> image in it?  Thanks.

>From my experience, the Gnuplot module isn't designed to be used in
"headless" mode -- it can save to the usual formats, but you have to
render everything in an x11 window interactively first.
It might not be hard to modify this, though.

-Jonathan

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


Converting TeX tokens into characters

2005-06-28 Thread Jonathan Fine
I'm sort of wishing to convert TeX tokens into characters.

We can assume the standard (i.e. plain) category codes.
And that the characters are to be written to a file.

This proceess to take place outside of TeX.
Say in a Python program.

Think of a pretty-printer.
* Read the TeX in as tokens.
* Write the TeX out as characters.

My present interest is in the writer part.

And I'd very much prefer to have human-editable output.

So the writer should have methods for
* Writing a string (not containing control sequences).
* Writing a control sequence (or control symbol).

And, like humans, it should also have a line length limit.

Does anyone know of such a writer?  Or something close?

Or any projects that could use such a writer?


-- 
Jonathan

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


Re: pexpect question....

2005-07-04 Thread Jonathan Ellis
[EMAIL PROTECTED] wrote:
> Currently, I am spawning a new thread
> that just does pexpect_spawned_child.close(wait=1). It seems to work in
> some cases but the child process is occassionally getting deadlocked.

I think your only cross-platform option will be to fix the child
process to die nicely instead of trying to find better ways kill it.

-Jonathan

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


Re: threads and sleep?

2005-07-05 Thread Jonathan Ellis
Jeffrey Maitland wrote:
> The problem I have is I had an application
> (wrote/co-wrote) that has a long run time dependant on some variables
> passed to it (mainly accuracy variables, the more accurate the longer
> the run time - makes sense). However in the hopes to speed it up I
> decided to write a threaded version  of the program to try and speed
> it up.  How ever what I am noticing is that the threaded version is
> taking as long possibly longer to run.  The thing is the threaded
> version is running on an 8 ia-64 proccessor system and it seems to
> only be using 2 or 3 porcessors at about 30% (fluxiates).  My guess is
> that 6 threads are running they are using 30% sprox each of a 2 given
> CPUS.

In many ways, Python is an incredibly bad choice for deeply
multithreaded applications.  One big problem is the global interpreter
lock; no matter how many CPUs you have, only one will run python code
at a time.  (Many people who don't run on multiple CPUs anyway try to
wave this off as a non-problem, or at least worth the tradeoff in terms
of a simpler C API, but with multicore processors coming from every
direction I think the "let's pretend we don't have a problem" approach
may not work much longer.)

If the GIL isn't an issue (and in your case it clearly is), you'll
quickly find that there's little support for debugging multithreaded
applications, and even less for profiling.

Sometimes running multiple processes is an acceptable workaround; if
not, good luck with the rewrite in Java or something else with real
thread support.  (IIRC Jython doesn't have a GIL; that might be an
option too.)

Python is a great tool but if you really need good threading support
you will have to look elsewhere.

-Jonathan

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


Re: threads and sleep?

2005-07-05 Thread Jonathan Ellis
Peter Hansen wrote:
> Jeffrey Maitland wrote:
> > I was hoping that python would allow for the cpu threading such in
> > Java etc.. but I guess not. (from the answers,and other findings) I
> > guess I will have to write this part of the code in something such as
> > java or c or something that allows for it then I can either wrap it in
> > python or avoid python for this part of the app.
>
> Or investigate the use of Irmen's Pyro package and how it could let you
> almost transparently move your code to a *multi-process* architecture

Unless you're doing anything that would require distributed locking.
Many if not most such projects do, which is why almost everyone prefers
to use threads on an SMP machine instead of splitting it across
multiple smaller boxes.

-Jonathan

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


Re: threads and sleep?

2005-07-06 Thread Jonathan Ellis
Peter Hansen wrote:
> Jonathan Ellis wrote:
> > Peter Hansen wrote:
> >>Or investigate the use of Irmen's Pyro package and how it could let you
> >>almost transparently move your code to a *multi-process* architecture
> >
> > Unless you're doing anything that would require distributed locking.
> > Many if not most such projects do, which is why almost everyone prefers
> > to use threads on an SMP machine instead of splitting it across
> > multiple smaller boxes.
>
> I can't address the issue of whether or not "most" such projects require
> distributed locking, because I'm not familiar with more than half of
> such projects, as you appear to be. 

Your sarcasm is cute, I suppose, but think about it for a minute.  If
the opposite of what I assert is true, why would even the mainstream
press be running articles along the lines of "multicore CPUs mean
programming will get tougher because locking is hard to get right and
you can't just scale by relying on the cpu to run your one
thread/process really fast anymore."

http://www.gotw.ca/publications/concurrency-ddj.htm for one example.

-Jonathan

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


Re: calling python procedures from tcl using tclpython

2005-07-09 Thread Jonathan Ellis
Jeff Hobbs wrote:
> chand wrote:
> > can anyone help me how to provide the info about the python file
> > procedure in the tcl script which uses tclpython i.e., is there a way
> > to import that .py file procedure in the tcl script
>
> >>>currently I have wriiten this tcl code which is not working
> >>>
> >>>package require tclpython
> >>>set interpreter [python::interp new]
> >>>$interpreter eval {def test_function(): arg1,arg2} ;
> >>>python::interp delete $interpreter
>
> You would call 'import' in the python interpreter, like so:
>   $interpreter eval { import testfile }
> assuming it's on the module search path.  Look in the python
> docs about Modules to get all the info you need.

Actually, both your import and the original "def" problem need to use
exec instead of eval.  Eval works with expressions; for statements you
need exec.

I blogged a brief example of tclpython over here:
http://spyced.blogspot.com/2005/06/tale-of-wiki-diff-implementation.html

-Jonathan

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


Re: Yet Another Python Web Programming Question

2005-07-09 Thread Jonathan Ellis
Daniel Bickett wrote:
> He would read the documentation of Nevow, Zope, and Quixote, and would
> find none of them to his liking because:
>
> * They had a learning curve, and he was not at all interested, being
> eager to fulfill his new idea for the web app. It was his opinion that
> web programming should feel no different from desktop programming.

If you're coming from a PHP background, you'll find Spyce's learning
curve even shallower.

http://spyce.sourceforge.net

-Jonathan

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


Re: Reading variables from a forked child (Python C/API)

2005-07-15 Thread Jonathan Conrad
#ifdef def
Donn Cave wrote:
> Quoth MrEntropy <[EMAIL PROTECTED]>:
> 
> | I'm having a little trouble getting an idea running. I am writing a C 
> | program which is really a frontend to a Python program. Now, my C 
> | program starts up, does some initialisation like initialisation of it's 
> | variables and Py_Initialize() and then it fork()s. After forking the 
> | child launches the python program with Py_Main() and with the parent I 
> | want to be able to read the variables of the Python program. I have 
> | tried many ways but cannot find a solution to do this.
> 
> That's because there is no solution.  After a fork, the only effect
> observable in the parent process is the return value of fork, which
> will have a non-zero value.  Subsequent execution in the child process
> occurs completely independently from the parent, and leaves no trace
> whatever.  So you can't read variables from memory, that were set by
> the child.

Though there is probably some solution involving a third party 
such as POSIX threads, Linux has a clone(2) procedure which 
fork(2) is a macro of.  It is tricky to use, but allows for 
having the child process have the same pid, memory, memory 
mappings, file tables, etc.  BSD mmap(2) is more portable and can 
be used for interprocess sharing.  The solution which Don Cave 
provided is probably the one that Mr. Entropy needs to start 
with.  More specifically, ...

#else

int main (int argc, char **argv)
{
int fd0, fd1, fd2;
{
int fd_in [2], fd_out [2], fd_err [2];
pipe (fd_in);  fd0 = fd_in  [1];
pipe (fd_out); fd1 = fd_out [0];
pipe (fd_err); fd2 = fd_err [0];
if (!fork ()) {
dup2 (fd_in  [0], 0); close (fd0);
dup2 (fd_out [1], 1); close (fd1);
dup2 (fd_err [1], 2); close (fd2);
execl ("python", "python", 0);
perror ("execution failed");
exit (1);
}
}

write (fd0, "print None\n", 11);
write (1, b, read (fd1, b, sizeof (b) /* 5 or more */));

#define write0(q...) write (fd0, "print " #q "\n",  \
sizeof (#q) + 5 + 1 - 1)
write0 (True);  write (1, b, read (fd1, b, sizeof (b)));
write0 (5, str (5.0), ...);
write (1, b, read (fd1, b, sizeof (b)));
return write0 (True, False, None,
True and False,
True and None,
False and None,
True and False and None) && 0;
}

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


Re: What module to use to get a file from a website?

2005-07-16 Thread Jonathan Conrad
from os import system
system ("start http://www.python.org/";)

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


ANN: Spyce 2.0.3

2005-07-22 Thread Jonathan Ellis
Spyce 2.0.3 released

Spyce is a python web application server, combining features of
popular frameworks such as ASP.NET and JSP with Pythonic elegance.
Spyce may be deployed as a standalone server, proxied behind
Apache, under mod_python, FastCGI, or CGI.

Spyce 2.0 includes features unique to Python web frameworks such as
  Active Handlers
- reusable components without the leaky abstractions seen in
  ASP.NET et al.
- http://spyce.sourceforge.net/docs/doc-lang_handlers.html
  Active Tag compiler
- http://spyce.sourceforge.net/docs/doc-tag_new2.html

2.0.3 is a bugfix and documentation-improvement release.
The installation section of the manual has received particular
attention.  There is also the new section on starting your first
project (http://spyce.sourceforge.net/docs/doc-conf_next.html),
which answers the FAQ, "how do I organize my Spyce files."

Demos and downloads are available at http://spyce.sourceforge.net/.

Changelog is at http://svn-hosting.com/svn/spyce/trunk/spyce/CHANGES.

Jonathan Ellis
http://spyced.blogspot.com

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


datagram queue length

2005-08-10 Thread Jonathan Ellis
I seem to be running into a limit of 64 queued datagrams.  This isn't a
data buffer size; varying the size of the datagram makes no difference
in the observed queue size.  If more datagrams are sent before some are
read, they are silently dropped.  (By "silently," I mean, "tcpdump
doesn't record these as dropped packets.")  This is a problem because
while my consumer can handle the overall load easily, the requests
often come in large bursts.

This only happens when the sending and receiving processes are on
different machines, btw.

Can anyone tell me where this magic 64 number comes from, so I can
increase it?

Illustration follows.

-Jonathan

# 
# start this, then immediately start the other
# _on another machine_
import socket, time

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', 3001))

time.sleep(5)

while True:
 data, client_addr = sock.recvfrom(8192)
 print data

# 
import socket

for i in range(200):
 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 sock.sendto('a' * 100, 0, ('***other machine ip***', 3001))
 sock.close()

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


Re: Django Vs Rails

2005-09-15 Thread Jonathan Ellis
James wrote:
> I actually like the framework to reflect on my database. I am more of a
> visual person. I have tools for all my favorite databases that allow me
> to get a glance of ER diagrams and I would rather develop my data
> models in these tools rather than in code. Further more I rather like
> the idea of parsimonious use of code (which is probably why I use
> Python in the first place) and do not really like manually specifying
> data schemas in code as much as possible.
>
> Is some familiar with a Python Framework that builds by reflection.

PyDO (http://skunkweb.sourceforge.net/pydo2.html) is a Python ORM tool
that does this well (*cough* better than sqlobject *cough*).

-Jonathan

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


Re: web scrapping - POST and auto-login

2005-09-19 Thread Jonathan Ellis
"james  hal-pc.org" wrote:
>I'm trying to update the WEP key on a wireless router via script and
> email the results to myself.  this will be run once a week.

Look up Mechanize (http://wwwsearch.sourceforge.net/mechanize/) or the
more low-level ClientForm by the same author.  This will be _much_
easier than doing it by hand.

-Jonathan

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


Unable to run IDLE Under Windows

2005-02-06 Thread Jonathan Polley
I have one account on a WindowsXP machine that refuses to run IDLE (or
any other python script that uses Tk).  Other people can login to that
PC and IDLE runs just fine, so it is not an installation issue.  When
the person who has the problem logs into another PC the problem follows
them.  Any ideas as to what might me wrong?

This is the traceback from IDLE:

C:\Python20\Tools\idle>..\..\python.exe idle.py
Traceback (most recent call last):
  File "idle.py", line 12, in ?
PyShell.main()
  File "C:\Python20\Tools\idle\PyShell.py", line 732, in main
root = Tk(className="Idle")
  File "c:\python20\lib\lib-tk\Tkinter.py", line 1482, in __init__
self.tk = _tkinter.create(screenName, baseName, className)
TclError: unknown color name "white "

C:\Python20\Tools\idle>

Thanks in advance,

Jonathan Polley

jwpolley rockwellcollins com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unable to run IDLE Under Windows

2005-02-07 Thread Jonathan Polley
In article <[EMAIL PROTECTED]>, Marian Aldenhövel
<[EMAIL PROTECTED]> wrote:

> Hi,
> 
>  > Any ideas as to what might me wrong?
> 
> > TclError: unknown color name "white "
> 
> There is a space after "white" in this error-message. If this string comes
> from some per-user configuration that may well explain your problem.
> 
> Ciao, MM

Where do I have to go in order to fix it?  It follows them from
computer to computer...

Thanks,

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


Non-blocking input on windows, like select in Unix

2005-03-01 Thread Jonathan Fine
Hello
I have written a program that interacts with a
command line program.
Roughly speaking, it mimics human interaction.
(With more speed and accuracy, less intelligence.)
It works fine under Linux, using select().
But Windows does not support select for files.
Only for sockets.
Here's a google search on this topic.
<http://groups.google.co.uk/groups?q=select+windows+non-blocking&hl=en&lr=&group=comp.lang.python&scoring=d&start=0&sa=N>
I'd like my program to work under Windows also.
I'm exploring the following approach.
   stdin, stdout = os.popen2('tex')
Now read a byte at a time from stdout.
Whenever a prompt string from TeX appears,
write the response to stdin.
An experiment at the Python command line shows
that this works.  At each step either
a) we can read one byte from stdout w/o blocking
or
b) we can usefully write to stdin.
By the way, this experiment is rather tedious.
Still, I hope to have my computer do this for me.
So, unless I'm mistaken, I can get the program to
work under Windows.  Though reading a byte at a
time might give a performance penalty.  Can't say
yet on  that.
My question is this: Under Windows, is it possible
to read as many bytes as are available from stdout,
without blocking?
This is one way to improve performance.  (If needed.)
Another way, is to use _longer_ prompt strings.
If prompt strings are at least 64 bytes long, then
we can safely read 64 bytes -- unless we are in
the process of reading what might be a prompt
string.
This will of course increase performance in the
limiting case of when there are zero prompt
strings, and expensive system calls.
This problem of non-blocking input on Windows seems
to arise often.  I hope my remarks might be helpful
to others.  Certainly, I've found it helpful to
write them.
--
Jonathan
http://www.pytex.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Non-blocking input on windows, like select in Unix

2005-03-01 Thread Jonathan Fine
Paul Rubin wrote:
Jonathan Fine <[EMAIL PROTECTED]> writes:
My question is this: Under Windows, is it possible
to read as many bytes as are available from stdout,
without blocking?

I think Windows implements non-blocking i/o calls.  However the
traditional (to some) Python or Java approach to this problem is
to use separate threads for the reader and writer, and let them block
as needed.
Thank you for this.
As I recall, some posts to this list say that Windows provides
non-blocking i/o for sockets but not for files.
However, if non-blocking i/o for files were available, that
would be great.  Actually, all I care about is input.
Can anyone provide a definite answer to this question?
And please, if the answer is YES (hope it is), with
working sample code.
The threaded approach does not help me.  If the read blocks,
I do not know what to write.  (I am responding to a command
line prompt - I have to read it first.)
--
Jonathan
--
http://mail.python.org/mailman/listinfo/python-list


Re: Non-blocking input on windows, like select in Unix

2005-03-02 Thread Jonathan Fine
fraca7 wrote:
Jonathan Fine a écrit :
Paul Rubin wrote:

As I recall, some posts to this list say that Windows provides
non-blocking i/o for sockets but not for files.

No, Windows does provide non-blocking I/O for regular files, but it's a 
completely different mechanism than the one used by winsock. You'll have 
to use win32all and enter the Dark Side, that is Windows APIs.

You don't want to do that if you're not already familiar with 
CreateProcess, CreatePipe, overlapped structures, WaitForSingleObject & 
al...

Thank you for this.
My application will, I think, become much more complicated if I cannot
use non-blocking input.  (As already explained, it is not a problem
that can be solved by threads.  Basically, I _need_  either to read
all available data, _or_ to read very carefully a byte at a time.)
Knowing that non-blocking input can be done under Windows, I would
like to use it.  In the longer run, that will be easier than
rewriting my application.  Or so it seems to me.
I did a google search, on the web, for
  CreateProcess, CreatePipe, overlapped structures, WaitForSingleObject
This is the first page is produced
  http://www.codeproject.com/threads/anonpipe1.asp
Seems to be the right sort of thing.  But I don't have time to read it
now.
I'm not really a Windows programmer.  Don't know the system calls.
But I do want my application to run on Windows.
I'll get back to this in a couple of weeks.  (Busy right now.)
--
Jonathan
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to handle repetitive regexp match checks

2005-03-18 Thread Jonathan Giddy
GiddyJP wrote:
# do this once
import Trespass
pattern = Trespass.Pattern()
pattern.addRegExp(r'struct {', 1)
pattern.addRegExp(r'typedef struct {', 2)
pattern.addRegExp(r'something else', 3)
Minor correction... in this module { always needs to be escaped if not 
indicating a bounded repeat:
 pattern.addRegExp(r'struct \{', 1)
 pattern.addRegExp(r'typedef struct \{', 2)
 pattern.addRegExp(r'something else', 3)
--
http://mail.python.org/mailman/listinfo/python-list


Re: cookie lib policy how-tp?

2004-12-07 Thread Jonathan Ellis
Riko Wichmann wrote:
>
> When I use opera to access this page by hand and look at the sources,
I
> see the full sources when letting opera identify itself as MSIE 6.0.
> When using Mozilla 5.0 I get the same in-complete source file as with

> python.

Sounds like your first step should be to identify yourself as IE.

opener = urllib2.build_opener(...)
opener.addheaders = [("User-Agent", "whatever IE calls itself these
days")]

-Jonathan

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


Simple Python + Tk text editor

2005-04-13 Thread Jonathan Fine
Hi
I'm looking for a simple Python + Tk text editor.
I want it as a building block/starting point.
I need basic functions only:
  open a file, save a file, new file etc.
It has to be open source.
Anyone know of a candidate?
--
Jonathan
http://qatex.sourceforge.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Python + Tk text editor

2005-04-13 Thread Jonathan Fine
Paul Rubin wrote:
Jonathan Fine <[EMAIL PROTECTED]> writes:
I'm looking for a simple Python + Tk text editor.
I want it as a building block/starting point.

Something wrong with IDLE?

Thanks for this suggestion.
For some reason, I did not think of IDLE as an editor.
Must have been a blind spot.
Though not simple, IDLE is a good starting point for me.
And for my project (integration of Python and TeX) there
is most unlikely to be a better one.
However, learning IDLE details might be quite a detour
from my immediate goals.
Some code follows, in case anyone else is interested.
IDLE is not yet a package, it seems.  But the idea is there.
===
[EMAIL PROTECTED]:/usr/lib/idle-python2.1$ cat __init__.py
# Dummy file to make this a potential package.
===
/usr/bin/idle hacked to produce an EditorWindow.
===
#! /usr/bin/python
import os
import sys
sys.path[:0] = ['/usr/lib/idle-python2.1']
import IdleConf
idle_dir = os.path.dirname(IdleConf.__file__)
IdleConf.load(idle_dir)
# new code
import Tkinter
import EditorWindow
root = Tkinter.Tk()
EditorWindow.EditorWindow(root=root)
EditorWindow.mainloop()
sys.exit()
# end of new code
# defer importing Pyshell until IdleConf is loaded
import PyShell
PyShell.main()
===
--
Jonathan
http://qatex.souceforge.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Python + Tk text editor

2005-04-14 Thread Jonathan Fine
Eric Brunel wrote:

Do you know the (apparently dead) project named e:doc? You can find it 
here:
http://members.nextra.at/hfbuch/edoc/
It's a kind of word processor that can produce final documents to 
various formats using backends, and one of the backends is for LaTeX.

It's written in Perl, but with Perl::Tk as a tool-kit, so it is quite 
close to Tkinter. There may be some ideas to steal from it.

Thanks for this.  I've not seen it before
There are quite a few GUI semi-wysiwyg front ends to (La)TeX.
Interesting that there are so many, and that besides LyX few
seem to have succeeded.  Guess it's an important problem that
is also difficult.
My approach is a rather different - it is to exploit running
TeX as a daemon
  http://www.pytex.org/texd
This allows for Instant Preview.  My application is simply
a means of show-casing this capability.  And making it
useful in simple contexts.
So what I'm really wanting to do is provide a component for
projects such as e:doc.
Any, this might be a bit off-topic.
And thanks again for the link.
--
Jonathan
http://qatex.sourceforge.net

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


Re: Problem with unpack hex to decimal

2005-04-17 Thread Jonathan Brady

<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hello,
>
> I was looking at this:
> http://docs.python.org/lib/module-struct.html
> and tried the following
>
 import struct
 struct.calcsize('h')
> 2
 struct.calcsize('b')
> 1
 struct.calcsize('bh')
> 4
>
> I would have expected
>
 struct.calcsize('bh')
> 3
>
> what am I missing ?

Not sure, however I also find the following confusing:
>>> struct.calcsize('hb')
3
>>> struct.calcsize('hb') == struct.calcsize('bh')
False

I could understand aligning to multiples of 4, but why is 'hb' different 
from 'bh'? 


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


1993 Van Rossum Python Web Robot

2005-04-19 Thread Jonathan Vance
I am looking for a python robot that Van Rossum released with python
0.9.8.  It may have been the first web robot (see
http://www.webhistory.org/www.lists/www-talk.1993q1/0060.html).

I've had no luck finding the code for the robot or the 0.9.8 tarball.
Can anyone help me out?

Thanks.

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


New user group: Rochester, MN, USA

2016-01-20 Thread Jonathan Hartley

http://www.meetup.com/PyRochesterMN

First meeting planned for Thu 28th January 2016

--
Jonathan Hartley
[email protected]
+1 507-513-1101
--
https://mail.python.org/mailman/listinfo/python-list


Confirm: About python Registration

2016-03-04 Thread Jonathan Qin
Dear Manager,
(Please forward this to your CEO, because this is urgent. Thanks!)
This is Jonathan Qin---the manager of domain name registration and solution 
center in China. On February 29th, 2016, we received an application from Baiyao 
Holdings Ltd requested “python” as their internet keyword and China (CN) domain 
names. But after checking it, we find this name conflict with your company name 
or trademark. In order to deal with this matter better, it‘s necessary to send 
an email to you and confirm whether this company is your distributor or 
business partner in China?
Kind regards
Jonathan Qin
Manager  
Cname (Head Office)
8008, Tianan Building, 
No. 1399 Jinqiao Road, 
Shanghai 200120
Tel: +86-21-6191-8696
Mobile: +86-1377-4400-340 
Fax:+86-21-6191-8697 
www.cname.org.cn
-- 
https://mail.python.org/mailman/listinfo/python-list


Python implementations in JavaScript

2015-02-14 Thread Jonathan Hayward
What is the relative maturity of different Python implementations in
JavaScript? Are any of the implementations ready to rely on?

-- 
[image: Christos Jonathan Seth Hayward] <http://jonathanscorner.com/>
Jonathan S. Hayward, a full stack web developer with Python/Django and
AngularJS/jQuery.

Articles on Technology <http://jonathanscorner.com/technology/> • Book: *Django
JavaScript Integration*
<http://www.packtpub.com/django-javascript-integration-ajax-and-jquery/book>
 • *Email * • Github <http://github.com/JonathanHayward> •
LinkedIn <http://www.linkedin.com/in/jonathanhayward> • Main Website
<http://jonathanscorner.com/> • *Professional Site <http://jsh.name/>*

I invite you to visit my professional site! <http://jsh.name/>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python implementations in JavaScript

2015-02-16 Thread Jonathan Hayward
@all, thanks. I think I have Brython to try out first and others to maybe
fall back on, which is the kind of information I wanted.


Thanks,

On Mon, Feb 16, 2015 at 3:59 PM, Jonathan Hayward <
[email protected]> wrote:

> @all, thanks. I think I have Brython to try out first and others to maybe
> fall back on, which is the kind of information I wanted.
>
>
> Thanks,
>
> On Sat, Feb 14, 2015 at 9:37 PM, Ethan Furman  wrote:
>
>> On 02/14/2015 05:28 PM, Ian Kelly wrote:
>> > On Fri, Feb 13, 2015 at 4:05 PM, Jonathan Hayward  wrote:
>> >>
>> >> What is the relative maturity of different Python implementations in
>> JavaScript? Are any of the implementations ready to rely on?
>> >
>> > Brython is about two years old now.
>>
>> I remember a post from a few weeks ago that said Brython had worked just
>> fine for their needs.
>>
>> --
>> ~Ethan~
>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>>
>
>
> --
> [image: Christos Jonathan Seth Hayward] <http://jonathanscorner.com/>
> Jonathan S. Hayward, a full stack web developer with Python/Django and
> AngularJS/jQuery.
>
> Articles on Technology <http://jonathanscorner.com/technology/> • Book: 
> *Django
> JavaScript Integration*
> <http://www.packtpub.com/django-javascript-integration-ajax-and-jquery/book>
>  • *Email * • Github <http://github.com/JonathanHayward> •
> LinkedIn <http://www.linkedin.com/in/jonathanhayward> • Main Website
> <http://jonathanscorner.com/> • *Professional Site <http://jsh.name/>*
>
> I invite you to visit my professional site! <http://jsh.name/>
>



-- 
[image: Christos Jonathan Seth Hayward] <http://jonathanscorner.com/>
Jonathan S. Hayward, a full stack web developer with Python/Django and
AngularJS/jQuery.

Articles on Technology <http://jonathanscorner.com/technology/> • Book: *Django
JavaScript Integration*
<http://www.packtpub.com/django-javascript-integration-ajax-and-jquery/book>
 • *Email * • Github <http://github.com/JonathanHayward> •
LinkedIn <http://www.linkedin.com/in/jonathanhayward> • Main Website
<http://jonathanscorner.com/> • *Professional Site <http://jsh.name/>*

I invite you to visit my professional site! <http://jsh.name/>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Examples of modern GUI python programms

2014-03-31 Thread Jonathan Harden
On 31 Mar 2014 00:21, "D. Xenakis"  wrote:
>

... Snip ...

> What i need is to develop an android looking program (entirelly in
python) for windows, but dunno if this is possible (most propably is), and
which tool between those would help me most: tkinter - wxpython - pyqt -
pygtk .
>
> Any examples and suggestions are most welcome.

While I've not used it much Kivy could be useful if you want it to look
Android like, with the added benefit that you can build for android as well.
-- 
https://mail.python.org/mailman/listinfo/python-list


Aide pour bien démarrer en Python

2013-09-27 Thread jonathan . corriveau
Je sais qu'il y a plein d'information à lire un peu partout, mais j'ai vraiment 
du mal à voir pourquoi Python est si fantastique...

Je m'explique

Pour moi python c'est un langage de script très cryptique avec des noms de 
méthodes courts, pas claire, dans une identation pas toujours facile à lire et 
qui me rappel (mauvais souvenir) le positionnement obligatoire des instructions 
en COBOL... Je viens d'un monde .Net où les noms de méthodes disent exactement, 
très précisemment ce que fait le code (quite à avoir un nom de 30 caractères)...

Dans les standards PEP-8, il y a beaucoup de standards qui vont de soit, c'est 
facile de comprendre pourquoi c'est comme ça. Mais, il y a plein de cas où, 
personnelement et pour probablement plusieurs développeurs qui viennent du 
monde JAVA ou .Net, c'est très illisible et même contre-intuitif... 

Outre l'aspect visuel, je trouve des codes d'exemples, prit dans un pain et 
dans un seul fichier, très mal découpés et ça devient très difficile de se 
retrouver pour savoir qui fait quoi... 

On voit partout Python est orienté objet, tout le tralala, mais dans ma tête, 
j'ai bien du mal à voir du code Python qui suit certaine règle à la Clean Code 
avec des patterns bien établient (IoC, DI, Repository, UnitOfWork)...

J'ai plus souvent l'impression de voir des SmartUI (anti-pattern) qui dépendent 
de la terre entière avec aucune réél separation of concern...

Ceci dit, j'essaie vraiment d'apprendre le python et j'aimerais bien le faire...
Pourriez-vous m'indiquer des bonnes ressources, documentation qui pourrait 
répondre à mes intérogations ?

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


Why is array.array('u') deprecated?

2015-05-08 Thread jonathan . slenders
Why is array.array('u') deprecated?

Will we get an alternative for a character array or mutable unicode string?

Thanks!
Jonathan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is array.array('u') deprecated?

2015-05-08 Thread jonathan . slenders
Le vendredi 8 mai 2015 12:29:15 UTC+2, Steven D'Aprano a écrit :
> On Fri, 8 May 2015 07:14 pm, jonathan.slenders wrote:
> 
> > Why is array.array('u') deprecated?
> > 
> > Will we get an alternative for a character array or mutable unicode
> > string?
> 
> 
> Good question.
> 
> Of the three main encodings for Unicode, two are variable-width: 
> 
> * UTF-8 uses 1-4 bytes per character 
> * UTF-16 uses 2 or 4 bytes per character
> 
> while UTF-32 is fixed-width (4 bytes per character). So you could try faking
> it with a 32-bit array and filling it with string.encode('utf-32').


I guess that doesn't work. I need to have something that I can pass to the re 
module for searching through it. Creating new strings all the time is no 
option. (Think about gigabyte strings.)


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


Re: Why is array.array('u') deprecated?

2015-05-08 Thread jonathan . slenders
> Can you expand a bit on how array("u") helps here? Are the matches in the 
> gigabyte range?

I have a string of unicode characters, e.g.:

data = array.array('u', u'x' * 10)

Then I need to change some data in the middle of this string, for instance:

data[50] = 'y'

Then I want to use re to search in this text:

re.search('y', data)

This has to be fast. I really don't want to split and concatenate strings. Re 
should be able to process it and the expressions can be much more complex than 
this. (I think it should be anything that implements the buffer protocol).

So, this works perfectly fine and fast. But it scares me that it's deprecated 
and Python 4 will not support it anymore.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is array.array('u') deprecated?

2015-05-08 Thread jonathan . slenders
Le vendredi 8 mai 2015 15:11:56 UTC+2, Peter Otten a écrit :
> > So, this works perfectly fine and fast. But it scares me that it's
> > deprecated and Python 4 will not support it anymore.
> 
> Hm, this doesn't even work with Python 3:

My mistake. I should have tested better.

> >>> data = array.array("u", u"x"*1000)
> >>> data[100] = "y"
> >>> re.search("y", data)
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/lib/python3.4/re.py", line 166, in search
> return _compile(pattern, flags).search(string)
> TypeError: can't use a string pattern on a bytes-like object
> 
> You can search for bytes
> 
> >>> re.search(b"y", data)
> <_sre.SRE_Match object; span=(400, 401), match=b'y'>
> >>> data[101] = "z"
> >>> re.search(b"y", data)
> <_sre.SRE_Match object; span=(400, 401), match=b'y'>
> >>> re.search(b"yz", data)
> >>> re.search(b"y\0\0\0z", data)
> <_sre.SRE_Match object; span=(400, 405), match=b'y\x00\x00\x00z'>
> 
> but if that is good enough you can use a bytearray in the first place.

Maybe I'll try that. Thanks for the suggestions!

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


How do I check if a string is a prefix of any possible other string that matches a given regex.

2014-10-07 Thread jonathan . slenders
Hi everyone,


Probably I'm turning the use of regular expressions upside down with this 
question. I don't want to write a regex that matches prefixes of other strings, 
I know how to do that. I want to generate a regex -- given another regex --, 
that matches all possible strings that are a prefix of a string that matches 
the given regex.


E.g. You have the regex  ^[a-z]*4R$  then the strings "a", "ab", "A4" "ab4" are 
prefixes of this regex (because there is a way of adding characters that causes 
the regex to match), but "4a" or "a44" or not.
How do I programmatically create a regex that matches "a", "ab", "A4", etc.. 
but not "4a", "a44", etc..

Logically, I'd think it should be possible by running the input string against 
the state machine that the given regex describes, and if at some point all the 
input characters are consumed, it's a match. (We don't have to run the regex 
until the end.) But I cannot find any library that does it...

Thanks a lot, if anyone knows the answer to this question!


Cheers,
Jonathan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do I check if a string is a prefix of any possible other string that matches a given regex.

2014-10-07 Thread jonathan . slenders

> > Logically, I'd think it should be possible by running the input string 
> > against the state machine that the given regex describes, and if at some 
> > point all the input characters are consumed, it's a match. (We don't have 
> > to run the regex until the end.) But I cannot find any library that does 
> > it...
> 
> 
> 
> Strictly speaking, a DFA or NFA always consumes the entire input; the
> 
> question of interest is whether it halts in an accepting state or not.
> 
> 
> 
> It would be easy to transform a DFA or NFA in the manner that you
> 
> want, though.  For each non-accepting state, determine whether it has
> 
> any transitions that lead in one or more steps to an accepting state.
> 
> Modify the FSM so that each such state is also an accepting state.


Thanks, I'll make every state of the FSM an accepting state.

My use case is to implement autocompletion for a regular language. So I think 
if the input is accepted by the FSM that I build, it's a valid "prefix", and 
the autocompletion can be generated by looking at which transitions are 
possible at that point.

More pointers are welcome, but I think that I have enough to start the 
implementation.

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


Re: How do I check if a string is a prefix of any possible other string that matches a given regex.

2014-10-08 Thread jonathan . slenders
Le mercredi 8 octobre 2014 01:40:11 UTC+2, MRAB a écrit :
> If you're not interested in generating an actual regex, but only in
> 
> matching the prefix, then it sounds like you want "partial matching".
> 
> 
> 
> The regex module supports that:
> 
> 
> 
> https://pypi.python.org/pypi/regex

Wow, thanks a lot! That really helps me.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to structure packages

2011-09-08 Thread Jonathan Hartley
On Thursday, September 8, 2011 1:29:26 AM UTC+1, Steven D'Aprano wrote:
> Steven D'Aprano wrote:
> 
> Other than that, is there any justification
> for this rule? Any Java fans want to defend this?
>
> If "one class per file", why not "one method per class" too? Why is the
> second rule any more silly than the first?


Hey. I'm not a Java fan but I'll give it a go.

One method per class is not a good idea because a class is a bunch of code that 
forms a coherent conceptual bundle of functionality. Often, to provide such a 
bundle, it requires more than one method. To split these methods across several 
classes would be splitting up a single coherent entity, which makes it harder 
to understand the purpose and identity of the entity, and more fiddly to 
delineate the boundary between one such entity and the next.

On the other hand, IMHO one class per file is often a good idea. Since a class 
is a single coherent bundle, then the natural way to split a program into files 
is often to divide it into these same coherent bundles. Sometimes you have two 
or more classes that are conceptually very tightly coupled, and it makes sense 
to gather them up into a single file. However, for me, this is the exception 
rather than the rule, so in the general case, I usually end up with code that 
has one class per file. It's only a rule-of-thumb though, and should be broken 
whenever it seems appropriate.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cause __init__ to return a different class?

2011-09-15 Thread Jonathan Hartley
Perhaps a more idiomatic way of achieving the same thing is to use a factory 
function, which returns instances of different classes:

  def PersonFactory(foo):
  if foo:
 return Person()
  else:
 return Child()


Apologies if the code is messed up, I'm posting from Google groups web UI.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: executing arbitrary statements

2011-10-02 Thread Jonathan Hartley
On Saturday, October 1, 2011 8:06:43 AM UTC+1, Chris Rebert wrote:
> On Fri, Sep 30, 2011 at 11:31 PM, Jason Swails  wrote:
> > I'm probably missing something pretty obvious, but I was wondering if there
> > was a way of executing an arbitrary line of code somehow (such as a line of
> > code based on user-input).  There's the obvious use of "eval" that will
> > evaluate a function call, but that doesn't allow all things.
> 
> > Because write is a function eval works fine for it.  But since print isn't
> > (2.7), it throws a syntax error.  Likewise, variable assignments aren't
> > allowed either as they are also not functions and lack a return value:
> 
> 
> Use the `exec` statement, which is capable of executing statements (as
> opposed to just expressions):
> http://docs.python.org/reference/simple_stmts.html#the-exec-statement
> 
> > What I'm more or less looking to do is present a (limited) form of an
> > interpreter inside the application I'm writing for the advanced user.
> >
> > I'm also interested to hear if this is a particularly bad idea for any
> > reason,
> 
> It's potentially rather hacky and ad-hoc to effectively directly
> inject arbitrary statements at certain points in your code.
> 
> Assuming you were to trust the user-provided code, callbacks/hooks or
> plugin modules are typically much cleaner ways to integrate custom
> code from the user.
> 
> Depending on your particular use case, the `cmd` module might also be
> a workable alternative:
> http://docs.python.org/library/cmd.html
> 
> > and if there are security issues involved with allowing users to
> > execute their own code inside my program (keeping in mind that some people
> > may "donate" their scripts to others that may run them as black boxes).
> 
> It is *very much* a security issue!
> 
> > Is
> > it enough to disallow import statements, thereby not giving direct access to
> > the sys and os modules?
> 
> Not by a long shot! There are a bunch of known tricks that exploit
> introspection to circumvent such restrictions.
> Secure execution of untrusted Python code is a difficult problem. Some
> have accomplished it to a degree, but typically only by modifying the
> interpreter itself or imposing relatively onerous restrictions on the
> untrusted code.
> 
> > I know more or less what I want to do, but I'd also
> > appreciate any experienced input/advice/suggestions.
> 
> I additionally came across this in researching my reply:
> http://pypi.python.org/pypi/RestrictedPython/
> Apparently the speed of execution leaves something to be desired, but
> the package /supposedly/ works well otherwise.
> 
> Cheers,
> Chris
> --
> http://rebertia.com


I (and many others) entirely avoid using 'eval' in all my code for many years, 
based on the security concerns that Chris rightly highlights. It's worth noting 
though, that RaymondH's talks last year on some valid uses of 'eval' and 'exec' 
have opened my eyes to it somewhat. In summary, while it's dangerous to execute 
user-submitted code, there are no security risks associated with executing code 
generated by your own program. It takes a while to see how this might be 
useful, if (like me) you're not used to thinking about it.

Raymond's premier example was his implementation of namedtuple:
(see http://hg.python.org/cpython/file/default/Lib/collections/__init__.py)
This defines a string, the contents of which is a class definition, with some 
string formatting markers in it. These are replaced with known values on 
invocation of the namedtuple factory function, which then exec's the 
class-definition string and returns the resulting new type.

This results in an implementation that is both simpler and faster than trying 
to simply write a general-purpose class that does at runtime all the things 
that 'namedtuple' does.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: executing arbitrary statements

2011-10-03 Thread Jonathan Hartley
Fair points Steven. Thanks for further refining my initial refinement. :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   >