Re: [OT] Weaknesses of distro package managers - was Re: Python 2 to 3 conversion - embrace the pain

2015-03-16 Thread Alexander
Hi all,

Surely you did not look at the package manager and package assortment of
OpenBSD.

It is actually a really good example of how package repository can be both
reliable, easy to use and up to date.

Also, what sort of quality can be expected from a piece of software, whose
author is unable to package it properly (or to design it, so it can be
packaged)?



On Tue, Mar 17, 2015 at 3:51 PM, Michael Torrie  wrote:

> On 03/16/2015 08:40 PM, Ben Finney wrote:
> > Thanks again. This is an important and difficult problem, with competing
> > forces at play, and I am not at all satisfied with the current state of
> > packaging.
>
> I agree.  Though I like the concept of package managers and curated
> repos, compared to the dismal world of Windows, I also am not very
> satisfied with the current state of things.
>
> That's why I think the ideas coming out of this Red Hat think tank are
> interesting and worth discussion.  They put forth a possible solution
> that keeps the existing things we love about package managers and our
> distros of choice, while addressing these important issues in a
> maintainable, flexible, and still secure way.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] Localhost client-server simple ssl socket test program problems

2011-12-15 Thread Alexander
2011/12/15 Yang Chun-Kai 

>  Hello,everyone!!
>
> I am writing a simple ssl client-server test program on my personal laptop.
>
> And I encounter some problems with my simple programs.
>
> Please give me some helps.
>
> 
>
> My server code:
>
> import socket
> import ssl
> bindsocket = socket.socket()
> bindsocket.bind(('127.0.0.1', 1234))
> bindsocket.listen(5)
> print 'server is waiting for connection...'
> newsocket, fromaddr = bindsocket.accept()
> print 'start ssl socket...'
> connstream = ssl.wrap_socket(newsocket, server_side=True,
> certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt",
> keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key",
> ssl_version=ssl.PROTOCOL_SSLv23)
> data = connstream.read()
> print 'connected from address', fromaddr
> print 'received data as', repr(data)
> connstream.close()
>
> My client code:
>
> import socket
> import ssl
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> ssl_sock = ssl.wrap_socket(s,
> ca_certs="/home/ckyang/PHA/testsslsocket/myCA.crt",
> cert_reqs=ssl.CERT_REQUIRED)
> ssl_sock.connect(("127.0.0.1", 1234))
> ssl_sock.write("hello")
> ssl_sock.close()
>
>
> ---
> Server side error:
>
> File "views.py", line 17, in & lt;module>
> connstream = ssl.wrap_socket(newsocket, server_side=True,
> certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt",
> keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key",
> ssl_version=ssl.PROTOCOL_SSLv23)
>   File "/usr/lib/python2.7/ssl.py", line 344, in wrap_socket
> ciphers=ciphers)
>   File "/usr/lib/python2.7/ssl.py", line 119, in __init__
> ciphers)
> ssl.SSLError: [Errno 336265218] _ssl.c:347: error:140B0002:SSL
> routines:SSL_CTX_use_PrivateKey_file:system lib
>
> Client side error:
>
> File "client.py", line 10, in 
> ssl_sock.connect(("127.0.0.1", 1234))
>   File "/usr/lib/python2.7/ssl.py", line 299, in connect**
> self.do_handshake()
>   File "/usr/lib/python2.7/ssl.py", line 283, in do_handshake
> self._sslobj.do_handshake()
> socket.error: [Errno 104] Connection reset by peer
>
>
> 
> So what is wrong with my code?
>
> The codes are so simple and so much like python official site sample
> demonstration, but I still cant get it work, so frustrating.
>
> Seems the problem happened on server side then cause client side cant
> connect well, is that right?
>
> **
> My platform is ubuntu, with openssl 0.9.8 and python 2.7.
>
> All certificates and keys self-signed by openssl for test convenience.
>
> This is the site for referrence :
> http://andyjeffries.co.uk/articles/x509-encrypted-authenticated-socket-ruby-client
>
> Or should I need a real certificate issued by a real CA to let things work?
>
> Any tips or suggestions welcomed, thank you very much~
>
> Good day.
>
> Kay
>
> **
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
You're trying to connect to the same port on localhost as a client and a
server? I don't know for certain but I don't think that should work.
Two computers?


-- 
Alexander
7D9C597B
-- 
http://mail.python.org/mailman/listinfo/python-list


class instance customization

2010-04-17 Thread Alexander
Hi, list.

I've some nontrivial class implementation MyClass and its instance my:

my = MyClass(args)

MyClass uses in internals some variable which is not defined in MyClass
itself. I want to extend instance of MyClass at runtime defining this
variable and making new instance. It is like a class inheritance in a
static way

class MyNewClass(MyClass):
def __init__(s, a):
s._variable = a

but this doesn't give me ability to make inheritance at runtime of the
single parent intance. Finaly this should look like this

my = MyClass(args)

a1 = my.new(1)
a2 = my.new(2)

and e.t.c. Is it possible to release this interface in python?


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


Re: class instance customization

2010-04-17 Thread Alexander
On 17.04.2010 18:32, Steven D'Aprano wrote:
> On Sat, 17 Apr 2010 13:09:43 +0400, Alexander wrote:
>
>   
>> Hi, list.
>>
>> I've some nontrivial class implementation MyClass and its instance my:
>>
>> my = MyClass(args)
>>
>> MyClass uses in internals some variable which is not defined in MyClass
>> itself. I want to extend instance of MyClass at runtime defining this
>> variable and making new instance. It is like a class inheritance in a
>> static way
>> 
> I'm afraid I don't understand what you are asking. MyClass uses a 
> variable which is not defined in MyClass. Where is it defined? Is it a 
> global variable?
>
> What do you mean, "like a class inheritance in a static way"?
>
> Perhaps you should give an example of what you want to happen.
>   

Ok, I'll try to explain on the following example. Let's consider class
MyClass that holds one string and concatenate it with other not defined
in this class:

class MyClass(object):
def __init__(s): pass
def set(s, key):
s.__key = key
def __str__(s):
return s.__key + ' ' + s.__other
def new(s, value):
return SubClass(s, value)

The problem is how to implement class SubClass which inherits MyClass,
define new variable __other accessible from MyClass intance and with
working application:

a = MyClass()
a.set('key1')

b1 = a.new('value1')
b2 = a.new('value2')

print b1, "," ,b2 # give 'key1 value1 , key1 value2'

a.set('key2')

print b1, ",", b2 # give 'key2 value1 , key2 value2'


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


Re: class instance customization

2010-04-19 Thread Alexander
On 18.04.2010 13:23, Steven D'Aprano wrote:
> On Sat, 17 Apr 2010 19:55:44 +0400, Alexander wrote:
>  
>   
>> Ok, I'll try to explain on the following example. Let's consider class
>> MyClass that holds one string and concatenate it with other not defined
>> in this class:
>> 
> [...]
>   
>> and with working application:
>>
>> a = MyClass()
>> a.set('key1')
>>
>> b1 = a.new('value1')
>> b2 = a.new('value2')
>>
>> print b1, "," ,b2 # give 'key1 value1 , key1 value2'
>>
>> a.set('key2')
>>
>> print b1, ",", b2 # give 'key2 value1 , key2 value2'
>> 
> Looking at your design, I can't see any reason for SubClass to be a 
> subclass of MyClass. It doesn't inherit any behaviour, and the 
> constructor takes completely different arguments. Why make it a Subclass?
>   
It was a very simple example that express the crux of my question. There
isn't any sense discuss design on it. In real implementation SubClass
has almost the same functionality and methods as MyClass, the difference
only in a state variable (one instance has one state and second the
other, see example below).
> MyClass is dangerous: creating an instance doesn't fully initialise the 
> instance, it leaves it in a half-initialised state that can cause 
> exceptions from simple operations like:
>
> instance = MyClass()
> print instance
>
> This is very bad design.
>   
This actualy depends on practical implementation of class. There could
be a checkup in __str__() member for presence of variable in instance
and result of each evaluation of instance.__str__() could be a valid:

class MyClass(object):
def __init__(s):
s.__default = 'value3'
def set(s, key):
s.__key = key
def __str__(s):
if '__other' in s.__dict__:
return s.__key + ' ' + s.__other
else:
return s.__key + ' ' + s.__default
def new(s, value):
return SubClass(s, value)


Assume MyClass implementation is already given and SubClass have to be
implemented in some way.


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


convert time to UTC seconds since epoch

2010-07-20 Thread Alexander
 Hi, list

How with python standard library to convert string like '-MM-DD
mm:HH:SS ZONE' to seconds since epoch in UTC? ZONE may be literal time
zone or given in explicit way like +0100.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: convert time to UTC seconds since epoch

2010-07-20 Thread Alexander
 On 21.07.2010 00:46, Rami Chowdhury wrote:
> On Jul 20, 2010, at 12:26 , Alexander wrote:
>
>> Hi, list
>>
>> How with python standard library to convert string like '-MM-DD
>> mm:HH:SS ZONE' to seconds since epoch in UTC? ZONE may be literal time
>> zone or given in explicit way like +0100.
> If you have a sufficiently recent version of Python, have you considered 
> time.strptime: http://docs.python.org/library/time.html#time.strptime ?
>
Yes. May be I don't undertand something. but it seems strptime doesn't
work with timezones at all. Only understands localzone and dates w/o zones.
-- 
http://mail.python.org/mailman/listinfo/python-list


Curses Programming

2010-11-10 Thread alexander
Hi, all

 Here is the test. Plz help.

   /
***/
 If you use the new image search of Google, you will find that the
result images are layouted in a way that each row has the same height,
but contains different number of images dependents the width-height
ratio of each image. The goal is to make each row of images having
approximately same total width. Just try search any image
inimages.google.com, you will get the idea.
If you're the programmer designing this page, what algorithm would you
use to select images for each row?
 Input:
The input of the program is a plain text file, each line in the file
containg 2 numbers, describing the width and height of an image
 e.g.
800 600
640 400
720 1280
...
 Goal:
Design an algorithm to put those images into a number of rows, each
row is 150 pixel height and 1200 pixel wide. You could scale images
proportionally to the same height. You need make sure the images are
aligned as closely as possbile to the row boundary.
Output:
The indexes of the images you would put on each row followed by blank
line.
e.g.
0
1
2
 According to the input file example, this output means image-0 (800 x
600) and image-1 (640 x 400) is put on the first row and image-2 (720
x 1280) is put on the second row.
Constraint:
Assuming, the input file contains less than 100 lines. Your algorithm
should finish within 5 secs on a standard laptop.

/
**/

Could any give a hand?
Thanks a lot
Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Feed subscription IM bot

2010-11-10 Thread alexander
Hi,

   Can anyone help on this?

   /
/


 The project should either be hosted online and usable from there, or
can be run locally. Complete ource code should be delivered along with
a brief readme for features. It should take about about 1-2 hours to
complete.
 Basic requirements: Build a Python driven IM bot with the following
features
 * A user can add the bot to his/her Google talk/jabber friends list.
 * The user types the words "sub [feed_url]" to the bot to begin
subscribing to the feed at the specified feed_url ([feed_url] is the
placeholder for the actual feed url. e.g. http://myweb.com/feed)
* The user types the words "unsub [feed_url]" to stop subscribing to
the feed
* The bot scans the feed at a specified time interval, and sends new
contents to the subscribing user
Additional credits:
* Additional bot commands: "ls", "pause [feed_url]", "
resume[feed_url]", "grep [feed_url] [keywords]", whatever you think is
useful and fun, even not feed subscription related
* A web interface for reading/managing subscriptions
* Beautiful, extensible and scalable architecture
 Requirements:
 * Python 2.5
 * Robust and graceful error handling
 * Robust unicode handling

/
/

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


Re: Python equivalent to the "A" or "a" output conversions in C

2012-06-19 Thread Alexander Blinne
On 19.06.2012 18:23, Edward C. Jones wrote:
> Consider the following line in C:
>printf('%a\n', x);
> where x is a float or double.  This outputs a hexadecimal representation
> of x.  Can I do this in Python?

Don't know why there is no format character %a or %A in python, but the
conversion is done by float method hex():

a = 3.1415
print a.hex()

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


Re: 2 + 2 = 5

2012-07-05 Thread Alexander Blinne
On 05.07.2012 16:34, Laszlo Nagy wrote:
 five.contents[five.contents[:].index(5)] = 4
 5
> 4
 5 is 4
> True

That's surprising, because even after changing 5 to 4 both objects still
have different id()s (tested on Py2.7), so 5 is 4 /should/ still be
False (But isn't on my 2.7). But that's some implementation detail we
are not supposed to play with ;)

> But this I don't understand:
> 
 5+0
> 4
 5+1
> 4
 5+2
> 6

That's easy:

5+0 is actually 4+0, because 5 == 4, so 5+0 gives 4.
5+1 is actually 4+1, which is 5, but 5 is again 4.
5+2 is 4+2 which is 6.

Greetings

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


Gender, Representativeness and Reputation in StackOverflow

2012-07-23 Thread Alexander Serebrenik
Do you participate in StackOverflow discussions? 

As a part of a joint on-going research effort of the Brunel University (UK) and 
Eindhoven University of Technology (The Netherlands)  on the impact of 
collaboration sites on the developers community, we would like to understand 
the demographics of StackOverflow participants and their activity. Specifically 
we are focusing on how genders, minorities and cultural background are 
represented in the population of users and participants of StackOverflow. 

Therefore, we have prepared a small questionnaire: 
https://docs.google.com/spreadsheet/viewform?formkey=dEhtUVNQTEJmRTlwMVJSQ1hkeUZTR3c6MQ#gid=0
 

In our previous research we have proposed an h-index for open source developers 
(http://scholar.google.com/scholar?q=%22Developing+an+h-index+for+OSS+developers.%22),
 and we have a paper under review about how web activity (also Stack 
Overflow's) could be used by candidates as their resumes. 

Filling this questionnaire should not take more than a couple of minutes. 
Personal data will not be made available to third parties and no identifiable 
details about individual participants will be published. 


Best regards, 
Andrea Capiluppi (andrea.capiluppi @ brunel.ac.uk) [Lecturer, Brunel 
University, UK; NL; SO userid: 1528556] 
Alexander Serebrenik (a.serebrenik @ tue.nl)  [Assistant Professor, Eindhoven 
University of Technology, NL; SO userid: 1277111] 
Bogdan Vasilescu (b.n.vasilescu @ tue.nl) [PhD student, Eindhoven University of 
Technology, NL; SO userid: 1285620] 

Discussion of this survey on Meta Stack Overflow: 
http://meta.stackoverflow.com/questions/139901/gender-representativeness-and-reputation-in-stackoverflow
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Gender, Representativeness and Reputation in StackOverflow

2012-07-23 Thread Alexander Serebrenik
1) The paper referenced contains 4 pages, so it should be available via 
IEEXplore. Moreover, you can find a copy on 
http://www.win.tue.nl/~aserebre/MSR2012.pdf

2) Since the survey is only one of the techniques we intend to use, and it will 
be augmented by analysing the data publicly available in the SE dump, I believe 
the selection bias is appropriately addressed.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Gender, Representativeness and Reputation in StackOverflow

2012-07-24 Thread Alexander Serebrenik
As a scientist I would be more than happy to publish in Open-Access Journals 
rather than in IEEE/ACM/Springer-published ones. However, I also have to be 
sure that my publications reach the scientific community and make an impact on 
it. Unfortunately, in many fields AFAIK the better journals (= higher impact, 
etc) are access-restricted, while the open-access ones are overloaded by 
low-quality submissions and are no much better than a trash bin.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [CGI] Why is HTML not rendered?

2012-08-17 Thread Alexander Blinne
On 17.08.2012 15:27, Gilles wrote:
> For some reason, this CGI script that I found on Google displays the
> contents of the variable but the HTML surrounding it is displayed
> as-is by the browser instead of being rendered:

> print "Content-Type: text/plain;charset=utf-8"

With this line you tell the browser to expect a simple plain text file
and no html. Change the line to

print "Content-Type: text/html;charset=utf-8"

and it should work.

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


Re: Objects in Python

2012-08-24 Thread Alexander Blinne
On 23.08.2012 20:30, Dennis Lee Bieber wrote:
> On Thu, 23 Aug 2012 15:33:33 +1000, Chris Angelico 
> declaimed the following in gmane.comp.python.general:
>> x = 1;
>>
>> In C, this means: Assign the integer 1 to the variable x (possibly
>> with implicit type casting, eg to floating point).
>>
>   Or, at an even lower level...
> 
>   Convert the decimal literal "1" to binary (including type casting)
> to the predeclared type given to the variable "x", and store that binary
> value into the predetermined memory associated with "x".

Not really the way i would view it. The conversion to binary of the
string "1" is part of the parsers and compilers work in order to do what
the language reference says about the meaning of x=1;. The resulting
code would simply store the binary value of an integer 1 (which is
contained in the code as is, nothing has to be converted or typecasted)
into the location corresponding to the variable x. So in C x=1; really
means store integer 1 to the variable x.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: issue with struct.unpack

2012-08-26 Thread Alexander Blinne
On 26.08.2012 01:31, Dennis Lee Bieber wrote:
> The struct module relies upon the user knowing the format of the data.
> If your problem is that you have some null-terminated string data in a
> variable width field, you will have to locate the position of the null
> FIRST, and specify the appropriate "count" for the s format.

This gave me the idea of an Enhancement of the Struct class with an
additional format character (perhaps 'n') which corresponds to a
null-terminated string:

-
# -*- coding: utf-8 -*-

import struct

class Nstr(object):
def __init__(self, ncount):
self.ncount = ncount

def unpack(self, s):
s = s.split('\0')
return s[:self.ncount], '\0'.join(s[self.ncount:])

def pack(self, *s):
if len(s)!=self.ncount:
raise ValueError
for st in s:
if '\0' in st:
raise ValueError
return '\0'.join(s)+'\0'

def __repr__(self):
return 'Nstr('+repr(self.ncount)+')'

class NStruct(object):
def __init__(self, format):
self.format = format
if format[0] in '!=<>@':
self.endianness = format[0]
format = format[1:]
else:
self.endianness = ''
self.chunks = []
while len(format)>0:
j = format.find('n')
if j > -1:
k = j-1
while format[k].isdigit():
k-=1
chunkformat, ncount, format = format[:k+1],\
format[k+1:j], format[j+1:]
ncount = 1 if len(ncount)==0 else int(ncount)
else:
chunkformat, ncount, format = format, '', ''
ncount = 0
stru = struct.Struct(self.endianness+chunkformat)
l = len(stru.unpack("0"*stru.size))
self.chunks.append((stru, l))
if ncount > 0:
self.chunks.append((Nstr(ncount), ncount))

def unpack(self, data):
res = []
for sth, n in self.chunks:
if isinstance(sth, struct.Struct):
chunk, data = data[:sth.size], data[sth.size:]
res.extend(sth.unpack(chunk))
elif isinstance(sth, Nstr):
chunk, data = sth.unpack(data)
res.extend(chunk)
return res

def pack(self, *data):
res = []
for sth, n in self.chunks:
chunk, data = data[:n], data[n:]
res.append(sth.pack(*chunk))
return ''.join(res)

def __repr__(self):
return 'NStruct('+repr(self.format)+')'

if __name__=="__main__":
a = NStruct('h b 2n 2h')
print repr(a)
d = 'asdblah blah\0haha\0asdf'
r = a.unpack(d)
assert r == [29537, 100, 'blah blah', 'haha', 29537, 26212]
print repr(d), repr(r)
dd = a.pack(*r)
print r, repr(dd)
assert dd == d

-

beware of bugs in the above code, i haven't testet it much yet.

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


Re: Python presentations

2012-09-13 Thread Alexander Blinne
On 13.09.2012 21:01, 8 Dihedral wrote:
> def powerlist(x, n):
> # n is a natural number
>  result=[]
>  y=1
>  for i in xrange(n):
> result.append(y) 
> y*=x
>  return result # any object in the local function can be returned

def powerlist(x, n):
result=[1]
for i in xrange(n-1):
result.append(result[-1]*x)
return result

def powerlist(x,n):
if n==1:
return [1]
p = powerlist(x,n-1)
return p + [p[-1]*x]

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


Re: Python presentations

2012-09-14 Thread Alexander Blinne
On 14.09.2012 00:38, Chris Angelico wrote:
> On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne  wrote:
>> def powerlist(x,n):
>> if n==1:
>> return [1]
>> p = powerlist(x,n-1)
>> return p + [p[-1]*x]
> 
> Eh, much simpler.
> 
> def powerlist(x,n):
>   return [x*i for i in xrange(n-1)]

I suppose you meant:

def powerlist(x,n):
  return [x**i for i in xrange(n-1)]

But this is less efficient, because it needs more multiplications (see
Horner's method)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python presentations

2012-09-16 Thread Alexander Blinne
On 14.09.2012 14:19, Chris Angelico wrote:
> Err, yes, I did mean ** there. The extra multiplications may be
> slower, but which is worse? Lots of list additions, or lots of integer
> powers? In the absence of clear and compelling evidence, I'd be
> inclined to go with the list comp - and what's more, to skip this
> function altogether and use the list comp directly where it's needed.

I did some timing with the following versions of the function:

def powerlist1(x, n):
result=[1]
for i in xrange(n-1):
result.append(result[-1]*x)
return result

def powerlist2(x,n):
if n==1:
return [1]
p = powerlist3(x,n-1)
p.append(p[-1]*x)
return p

def powerlist3(x,n):
  return [x**i for i in xrange(n)]

with Python 2.7 you are quite right, i used x=4. Below n=26 powerlist3
is the fastest version, for n>26 powerlist1 is faster, powerlist2 is
always slower than both.

With Pypy there is a completely different picture, with n<30 powerlist2
is way faster then the other two, but then powerlist1 is again faster
for greater n, somehow because now C long int cannot be used any longer.

for really big n powerlist3 always takes very much time :)

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


Re: Python presentations

2012-09-17 Thread Alexander Blinne
On 16.09.2012 19:35, Steven D'Aprano wrote:
> On Sun, 16 Sep 2012 18:13:36 +0200, Alexander Blinne wrote:
>> def powerlist2(x,n):
>> if n==1:
>> return [1]
>> p = powerlist3(x,n-1)
>> p.append(p[-1]*x)
>> return p
> 
> Is that a typo? I think you mean to make a recursive call to powerlist2, 
> not a non-recursive call to powerlist3.

Yes, it is a typo. I originally tested 2 more versions, but tried to
change the numbering before posting. Bad idea :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Alexander Blinne
On 21.09.2012 00:58, thorso...@lavabit.com wrote:
> Hi,
> 
> list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
> 
> I want to check for a value (e.g. '4'), and get the key of the dictionary
> that contains that value.
> (Yep, this is bizarre.)
> 
> some_magic(list, '4')
> => '3'
> 
> What's the functional way to do it?
> Is it possible to do it with a one-liner?

simple, but possibly slow solution:

import itertools

def some_magic(list, search):
return (key for key, val in itertools.chain(*(d.iteritems() for d in
list)) if search in val).next()

one-liner, yeah...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write binary with struct.pack_into

2012-10-06 Thread Alexander Blinne
First, you should consider reading the documentation of
struct.unpack_from and struct.pack_into at
http://docs.python.org/library/struct.html quite carefully. It says,
that these commands take a parameter called offset, which names the
location of the data in a buffer (e.g. an opened file).

example:

bloco='>%df' % (252)# Format string (252 floats)
fa = open('testIN.bin', 'rb')   # open for reading in binary mode
off = 0   # suppose i want to read block at beginning of file
my_array=struct.unpack_from(bloco, fa, off)  #read data


now write them to another file:

fb = open('testOUT.bin', 'r+b')   # open for updating in binary mode
off = 0   # suppose i want to write block at beginning of file
struct.pack_into(bloco, fb, off, *my_array)  #write data to testOUT


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


Re: creating size-limited tar files

2012-11-07 Thread Alexander Blinne
I don't know the best way to find the current size, I only have a
general remark.
This solution is not so good if you have to impose a hard limit on the
resulting file size. You could end up having a tar file of size "limit +
size of biggest file - 1 + overhead" in the worst case if the tar is at
limit - 1 and the next file is the biggest file. Of course that may be
acceptable in many cases or it may be acceptable to do something about
it by adjusting the limit.

My Idea:
Assuming tar_file works on some object with a file-like interface one
could implement a "transparent splitting file" class which would have to
use some kind of buffering mechanism. It would represent a virtual big
file that is stored in many pieces of fixed size (except the last) and
would allow you to just add all files to one tar_file and have it split
up transparently by the underlying file-object, something like

tar_file = TarFile(SplittingFile(names='archiv.tar-%03d', chunksize=
chunksize, mode='wb'))
while remaining_files:
tar_file.addfile(remaining_files.pop())

and the splitting_file would automatically create chunks with size
chunksize and filenames archiv.tar-001, archiv.tar-002, ...

The same class could be used to put it back together, it may even
implement transparent seeking over a set of pieces of a big file. I
would like to have such a class around for general usage.

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


Re: Python Noob Question.

2012-12-03 Thread Alexander Blinne
Hello,

by having a quick look at their website i found a plugin for CoreTemp
which acts as a server and can be asked for status information of the
cpu. Now your task is really simple: write a little function or class
that opens a network socket, connects to that plugin und asks it for the
information you require. You just need to find out what network protocol
this plugin uses to communicate. If it is no standard protocol for which
a higher level module is present (xmlrpc or something), see
http://docs.python.org/3/library/socket.html for low level sockets.

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


Re: Conversion of List of Tuples

2012-12-04 Thread Alexander Blinne
Am 03.12.2012 20:58, schrieb subhabangal...@gmail.com:
> Dear Group,
> 
> I have a tuple of list as,
> 
> tup_list=[(1,2), (3,4)]
> Now if I want to covert as a simple list,
> 
> list=[1,2,3,4]
> 
> how may I do that?

Another approach that has not yet been mentioned here:

>>> a=[(1,2), (3,4)]
>>> b=[]
>>> map(b.extend, a)
[None, None]
>>> b
[1, 2, 3, 4]

map returns [None, None] because extend returns nothing, but now
b==[1,2,3,4].

There are more ways:

>>> from operator import add
>>> reduce(add, a)
(1, 2, 3, 4)

or

>>> reduce(operator.add, (list(t) for t in a))
[1, 2, 3, 4]

I didn't do any performance testing, i guess the first one should be
about as fast es the for-loop approach with .extend() and the other two
might be quite slow. Although this only really matters if you have large
lists.

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-04 Thread Alexander Blinne
Another neat solution with a little help from

http://stackoverflow.com/questions/1701211/python-return-the-index-of-the-first-element-of-a-list-which-makes-a-passed-fun

>>> def split_product(p):
... w = p.split(" ")
... j = (i for i,v in enumerate(w) if v.upper() != v).next()
... return " ".join(w[:j]), " ".join(w[j:])

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-04 Thread Alexander Blinne
Am 04.12.2012 19:28, schrieb DJC:
 (i for i,v in enumerate(w) if v.upper() != v).next()
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: 'generator' object has no attribute 'next'

Yeah, i saw this problem right after i sent the posting. It now is
supposed to read like this

>>> def split_product(p):
... w = p.split(" ")
... j = next(i for i,v in enumerate(w) if v.upper() != v)
... return " ".join(w[:j]), " ".join(w[j:])

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-04 Thread Alexander Blinne
Am 04.12.2012 20:37, schrieb Ian Kelly:
> >>> def split_product(p):
> ... w = p.split(" ")
> ... j = next(i for i,v in enumerate(w) if v.upper() != v)
> ... return " ".join(w[:j]), " ".join(w[j:])
> 
> 
> It still fails if the product description is empty.

That's true... let's see, next() takes a default value in case the
iterator is empty and then we could use some special value and test for
it. But i think it would be more elegant to just handle the excepten
ourselves, so:

>>> def split_product(p):
... w = p.split(" ")
... try:
... j = next(i for i,v in enumerate(w) if v.upper() != v)
... except StopIteration:
... return p, ''
... return " ".join(w[:j]), " ".join(w[j:])

> I'm not meaning to pick on you; some of the other solutions in this
> thread also fail in that case.

It's ok, opening the eye for edge cases is always a good idea :)

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-06 Thread Alexander Blinne
Am 05.12.2012 18:04, schrieb Nick Mellor:
> Sample data

Well let's see what

def split_product(p):
p = p.strip()
w = p.split(" ")
try:
j = next(i for i,v in enumerate(w) if v.upper() != v)
except StopIteration:
return p, ''
return " ".join(w[:j]), " ".join(w[j:])

(which i still find a very elegant solution) has to say about those
sample data:

>>> for line in open('test.dat', 'r'):
... print(split_product(line))
('BEANS', 'hand picked')
('BEETROOT', 'certified organic')
('BOK CHOY', '(bunch)')
('BROCCOLI', 'Mornington Peninsula')
('BRUSSEL  SPROUTS', '')
('CABBAGE', 'green')
('CABBAGE', 'Red')
('CAPSICUM RED', '')
('CARROTS', '')
('CARROTS', 'loose')
('CARROTS', 'juicing, certified organic')
('CARROTS', 'Trentham, large seconds, certified organic')
('CARROTS', 'Trentham, firsts, certified organic')
('CAULIFLOWER', '')
('CELERY', 'Mornington Peninsula IPM grower')
('CELERY', 'Mornington Peninsula IPM grower')
('CUCUMBER', '')
('EGGPLANT', '')
('FENNEL', '')
('GARLIC', '(from Argentina)')
('GINGER', 'fresh uncured')
('KALE', '(bunch)')
('KOHL RABI', 'certified organic')
('LEEKS', '')
('LETTUCE', 'iceberg')
('MUSHROOM', 'cup or flat')
('MUSHROOM', 'Swiss brown')
('ONION', 'brown')
('ONION', 'red')
('ONION', 'spring (bunch)')
('PARSNIP,', 'certified organic')
('POTATOES', 'certified organic')
('POTATOES', 'Sebago')
('POTATOES', 'Desiree')
('POTATOES', 'Bullarto chemical free')
('POTATOES', 'Dutch Cream')
('POTATOES', 'Nicola')
('POTATOES', 'Pontiac')
('POTATOES', 'Otway Red')
('POTATOES', 'teardrop')
('PUMPKIN', 'certified organic')
('SCHALLOTS', 'brown')
('SNOW PEAS', '')
('SPINACH', "I'll try to get certified organic (bunch)")
('SWEET POTATO', 'gold certified organic')
('SWEET POTATO', 'red small')
('SWEDE', 'certified organic')
('TOMATOES ', 'Qld')
('TURMERIC', 'fresh certified organic')
('ZUCCHINI', '')
('APPLES', 'Harcourt  Pink Lady, Fuji, Granny Smith')
('APPLES', 'Harcourt 2 kg bags, Pink Lady or Fuji (bag)')
('AVOCADOS', '')
('AVOCADOS', 'certified organic, seconds')
('BANANAS', 'Qld, organic')
('GRAPEFRUIT', '')
('GRAPES', 'crimson seedless')
('KIWI FRUIT', 'Qld certified organic')
('LEMONS', '')
('LIMES', '')
('MANDARINS', '')
('ORANGES', 'Navel')
('PEARS', 'Beurre Bosc Harcourt new season')
('PEARS', 'Packham, Harcourt new season')
('SULTANAS', '350g pre-packed bags')
('EGGS', "Melita free range, Barker's Creek")
('BASIL', '(bunch)')
('CORIANDER', '(bunch)')
('DILL', '(bunch)')
('MINT', '(bunch)')
('PARSLEY', '(bunch)')
('', 'Spring ONION from QLD')

I think the only thing one is left to think about is the
('PARSNIP,', 'certified organic')
case. What about that extra comma? Perhaps it could even be considered
an "error" in the original data? I don't see a good general way to deal
with those which does not have to handle trailing punctuation on the
product name explicitly as a special case.

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


Re: Python Noob Question.

2012-12-10 Thread Alexander Blinne
Am 05.12.2012 21:24, schrieb Owatch:
> Thanks a TON for your answer thought, this is exactly what I really hoped for.
> The problem for me is that I don't actually know anything about writing a 
> function that opens a network socket, and "connects to that plugin und asks 
> it for the 
> information you require."

That plugin should have some documentation which should tell you
something about how to connect to it and how to request information.
When you know that you can turn to the python documentation and find out
how to do this in python.

> That's all really beyond me, all I can do is what I did so far, which is make 
> it ask for your temperature value, and then test it to see if its an integer
> 
> Then (I added this for testing) It asks for any temperature value. And if it 
> exceeds the given limit, it rings an alarm. Until it freezes and becomes 
> unresponsive :D

If you have specific problems with code you have written, try to build
up a minimal "working" example that shows the problem plus any error
messages/exceptions/stack traces you get back. We might be able to help
you with your code.

> I don't know how to make it 'query' or grab values constantly, if you don't 
> mind my potentially incorrect terminology. 

This is typically done with some kind of loop, e.g.

run = True
while run:
    #do something repeatedly and do "run = False" if you want to stop
pass

Greetings
Alexander

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


Re: Preventing tread collisions

2012-12-13 Thread Alexander Blinne
Am 12.12.2012 21:29, schrieb Dave Angel:
> On 12/12/2012 03:11 PM, Wanderer wrote:
>> I have a program that has a main GUI and a camera. In the main GUI, you can 
>> manipulate the images taken by the camera. You can also use the menu to 
>> check the camera's settings. Images are taken by the camera in a separate 
>> thread, so the long exposures don't block the GUI. I block conflicts between 
>> the camera snapshot thread and the main thread by setting a flag called 
>> self.cameraActive. I check to see if the cameraActive flag is false and set 
>> the cameraActive to True just before starting the thread. I generate an 
>> event on exiting the thread which sets the cameraActive flag to False. I 
>> also check and set and reset the flag in all the menu commands that access 
>> the camera. Like this.
>>
>> def onProperties(self, event):
>> """ Display a message window with the camera properties
>> event -- The camera properties menu event
>> """
>> # Update the temperature
>> if not self.cameraActive:
>> self.cameraActive = True
>> self.camera.getTemperature()
>> camDict = self.camera.getPropertyDict()
>> self.cameraActive = False
>> else:
>> camDict = {'Error': 'Camera Busy'}
>> dictMessage(camDict, 'Camera Properties')
>>
>> This works 
> 
> I don't think so.  in between the if and the assignment, another thread
> could get in there and also set the flag.  Then when either one of them
> finishes, it'll clear the flag and the other code is unprotected.

I have a general question about this kinds of things. I see that the
above is a common use case for some kind of lock which does this
testing/locking atomically. But the question is: if I know for sure that
there is no other thread that might get in the way this solution would
be fine, right?

In one of my applications i have a somewhat different case: i have a
list of objects and call the same method of each object, each in its own
thread (which is created and later joined just for this purpose). The
objects are thus only used by that one thread, the main thread waits for
all threads to be finished before accessing those objects again. Do i
really need some kind of locking for those objects?

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


Re: [newbie] problem making equally spaced value array with linspace

2012-12-18 Thread Alexander Blinne
Am 18.12.2012 13:37, schrieb Jean Dubois:
> I have trouble with the code beneath to make an array with equally
> spaced values
> When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I
> get the following result:
> [ 0.0001   0.00022  0.00034  0.00046  0.00058  0.0007 ]
> But I was hoping for:
> [ 0.0001   0.0002  0.0003  0.0004  0.0005  0.0006 0.0007]
> It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1
> then again for 0.01,0.07,0.01
> 
> What I find strange is that for the 1st example "1+abs(float(endvalue)-
> float(startvalue))/float(incr)" gives 7.0 but int() of this value
> gives 6
> can someone provide help with this issue?
> thanks
> jean
> 
> #!/usr/bin/python
> import math
> import numpy as np
> print "Enter start value as a float (e.g. 0.001) or in scientific
> notation (e.g. 1e-3): ",
> startvalue = raw_input()
> print "Enter end value: ",
> endvalue = raw_input()
> print "Enter step: ",
> incr = raw_input()
> #nom = number of measurements
> nom=int(1+abs(float(endvalue)-float(startvalue))/float(incr))
> array=np.linspace(float(startvalue), float(endvalue), float(nom))
> print "Array with current values: ",array

The Problem is the accuracy/precision of floating point operations

Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 100e-6 #start
>>> b = 700e-6 #end
>>> c = 100e-6 #incr
>>> 1+(b-a)/c
6.999

and the fact that int() only takes the integer part of a floating point
number.

>>> int(1+(b-a)/c)
6

So you have to make a more detailed decision about the number of points
in the case that (end-start)/incr is not exactly an integer which it
will almost never be.

The np.arange(a,b,c) function chooses a simple rule: give a list of
numbers a + k * c with k running from 0 to the highest integer with a +
k * c < b.

>>> np.arange(a,b,c)
array([ 0.0001,  0.0002,  0.0003,  0.0004,  0.0005,  0.0006])

You can get your desired list by adding some epsilon to the value of b.
Just make sure your epsilon is quite small compared to c.

>>> np.arange(a,b+1e-15,c)
array([ 0.0001,  0.0002,  0.0003,  0.0004,  0.0005,  0.0006,  0.0007])

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


Re: Pattern-match & Replace - help required

2012-12-19 Thread Alexander Blinne
Am 19.12.2012 14:41, schrieb AT:
> Thanks a million
> Can you recommend a good online book/tutorial on regular expr. in python?

http://docs.python.org/3/howto/regex.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

2012-12-22 Thread Alexander Blinne
Am 22.12.2012 13:45, schrieb prilisa...@googlemail.com:
> Ps.: The Socket, the DB has to be kept allways open, because of it's Server 
> functionality, A lot of Sensors, Timers, User interaction, must recived , 
> Calculated, etc so a reaction must be send in about 16~100 ms, different 
> modules opens and closes Sockets or files, could result in a dead situation.
> 
> 
> Or do i didn't see any Tree's in the Wood?

I would strongly recommend an object oriented view:

Suppose Datastore.py contains a Class Datastore. You can instantiate
that class once in the beginning of your main file and the resulting
object has methods to store and retrieve data in/from the store.

ModbusClient.py contains a Class Modbus. This also can be instantiated
just once which opens a TCP connection to be used many times and you can
hand over a reference to the Instance of Datastore you created earlier,
so it can speak with the Datastore. The object has methods to do the
things you want it to do.

The Same for DaliBusClient.

Now your main.py could look something linke

from Datastore import Datastore
from ModbusClient import Modbus
from DaliBusClient import DaliBus

def main():
datastore = Datastore(...)
modbus = Modbus(..., datastore)
dalibus = DaliBus(..., datastore)

modbus.read_data_and_save_to_store()
dalibus.read_data_and_save_to_store()

if __name__=="__main__":
main()

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


Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

2012-12-22 Thread Alexander Blinne
Am 22.12.2012 19:10, schrieb prilisa...@googlemail.com:
> It's for me a view of top side down, but how could the midlevel comunicate to 
> each oter... "not hirachical"

You could use something like the singleton pattern in order to get a
reference to the same datastore-object every time Datastore.Datastore()
is called. But you still need to close the connection properly at some
point, propably using a classmethod Datastore.close().

e.g.:

main.py:

from Datastore import Datastore
from ModbusClient import Modbus
from DaliBusClient import DaliBus

def main():
modbus = Modbus(...)
dalibus = DaliBus(...)

modbus.read_data_and_save_to_store()
dalibus.read_data_and_save_to_store()
Datastore.close()

if __name__=="__main__":
main()


ModbusClient.py:

import Datastore

class Modbus(object):
def read_data_and_save_to_store(self):
datastore = Datastore.Datastore()
#do something with datastore
-- 
http://mail.python.org/mailman/listinfo/python-list


Python, email temperature

2012-12-22 Thread Alexander Ranstam
Hi!

Im totally new to Python, and im using it on my Raspberry pi. I found a program 
that sends an email, and one that checks the temperature of my CPU, but i cant 
seem to combine the to into the funktion that i want, sending me the CPU temp 
via Email.

The two programs work very well on their own, but this doesnt work.

this works: server.sendmail(fromaddr, toaddrs, msg)  
but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)

despite the command "print cputemp" working in the same program. 

When i run the program i get the error:  

Traceback (most recent call last):
  File "sendcpu.py", line 36, in 
msg = cpu_temperature
NameError: name 'cpu_temperature' is not defined

Does anyone know why the program claims that cpu_temperature isnt defined, when 
it is?

Thanx!

//Alexander

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


Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

2012-12-22 Thread Alexander Blinne
Am 22.12.2012 21:43, schrieb prilisa...@googlemail.com:
> I Think I describe my Situation wrong, the written Project is a
> Server, that should store sensor data, perfoms makros on lamps according
> a sequence stored in the DB and Rule systems schould regulate home devices 
> and plan scheduler jobs so on.

I really don't understand your problem and I don't think anyone else
does. A python programm written like I have explained could do all those
things with no problem whatsoever, if only the classes contain all the
relevant methods.

> The System Runs in a threated environment. It looks for me, like the limits 
> are at the end of file. my core problem also the only one I have is: I don't 
> know how to get over that limits and enable dataexchange like a backbone...

There is no limit at the end of a file. A module is only a namespace and
you can access the names of another module simply by importing it first.
You also can freely pass around objects as parameters in function/method
calls or even just store them to module-level global names. Threads
don't change anything about that.

Greetings.



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


Re: [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

2012-12-24 Thread Alexander Blinne
At this point I think i could just refer to my other 2 postings and urge
you to read them again. They offer the idea of encapsulating the
function QuerySqlite into a method of an object that can be passed over
to some object (possibly throu the __init__-method) and store it in an
attribute of that other object. Those other objects can then simply call
the method belonging to the object.
If you really don't understand what I mean by this maybe you should
learn a bit about the basics of object-oriented programming.
Some pseudo-code illustrating this idea (which differs a bit from the
first singleton-like suggestion):

datastore.py:

class Datastore(object):
def __init__(self, some_args):
#do all things needed to open datastore and store everything to
#self.something and self.someotherthing

def query(self, query, *values):
#execute query with values inserted
#using self.something and self.someotherting
#return result

modbus.py:

class Modbus(self):
def __init__(self, datastore):
#store the argument datastore to an attribute of the newly
#created object
self.datastore = datastore

def read_bus(self, sensor):
#read from bus the value of sensor and return value

def read_temp_and_store(self, sensor):
#read and store
value = self.read_bus(sensor)
self.datastore.query("some query string", value)

scheduler.py:

class Scheduler(object):
def __init__(self, datastore, modbus):
#store the arguments datastore and modbus to attributes
#of the newly created object
self.datastore = datastore
self.modbus = modbus
#maybe read some config data from datastore
self.config = self.datastore.query("some initialising query if
necessary")

def do_things(self):
#do things you wanna do, perhaps in some loop or in a thread or
#something, does not really matter.
#Threading may require locking of some kind, but this also is
#not really related to your problem as I understand ist.
self.modbus.read_temp_and_store("sensor1")

main.py:

from scheduler import Scheduler
from datastore import Datastore
from modbus import Modbus

def main():
datastore = Datastore(some_args)
modbus = Modbus(datastore)
scheduler = Scheduler(datastore, modbus)

scheduler.do_things()

if __name__=="__main__":
main()

Please feel free to ask specific questions about this approach.

merry christmas everyone
Alexander Blinne
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python recursive tree, linked list thingy

2012-03-07 Thread Alexander Blinne

Am 07.03.2012 20:49, schrieb Wanderer:

I have a list of defective CCD pixels and I need to find clusters
where a cluster is a group of adjacent defective pixels. This seems to
me to be a classic linked list tree search.I take a pixel from the
defective list and check if an adjacent pixel is in the list. If it is
I add the pixel to the cluster and remove it from the defective list.
I then check an adjacent pixel of the new pixel and so on down the
branch until I don't find a defective pixel. The I move up to the
previous pixel and check the next adjacent pixel  and so on until I'm
back at the head I can't find any more defective adjacent pixels. How
do you handle this sort of thing in Python?


I'd do something like (code not tested):

defective_list = [(x1, y1), (x2, y2), ...]   #list of coordinates of
 #defective pixel
#build one cluster:
cluster_start = defective_list.pop() #starting point
buf = [] #buffer for added pixels
buf.push(cluster_start)
cluster = []
cluster.push(cluster_start)
while len(buf)>0:
i = buf.pop()
for b, d in itertools.product(xrange(2), [-1,1]):  #4 neighbours
j = list(i)
j[b] += d
j = tuple(j)
if outside_lcd(j) or j in cluster or j not in defective_list:
continue
defective_list.remove(j)
cluster.push(j)
buf.push(j)
return cluster

and repeat it until defective_list is empty.
--
http://mail.python.org/mailman/listinfo/python-list


Documentation, assignment in expression.

2012-03-23 Thread Alexander Blinne
Hi,

I think this section of the docs needs some kind of rewrite:

<http://docs.python.org/faq/design.html#why-can-t-i-use-an-assignment-in-an-expression>

While it is great to discuss the reasons for not allowing an assignment
in an expression, I feel that the given example is some kind of
outdated. The last sentence "For example, in the current version of
Python file objects support the iterator protocol, so you can now write
simply (for line in file:)" makes me think that this section was written
while that syntax was still new. No one I know would ever write
something like this:

> while True:
> line = f.readline()
> if not line:
> break
> ... # do something with line

I think at least we need a new example. Any ideas?

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


Re: Documentation, assignment in expression.

2012-03-25 Thread Alexander Blinne
I am not sure I understand your argument. The doc section states that

" [...] in Python you’re forced to write this:

while True:
line = f.readline()
if not line:
break
... # do something with line".

That simply isn't true as one can simply write:

for line in f:
#do stuff

which is the exact counter of the C idiom that C or perl people try to
use and complained about when they were really forced to write the above
lengthy while True loop. So there is no reason to want to do "assignment
in expression", so no real reason to use this example to explain why
they aren't there in python. I totally agree with the
error-handling-reason not allowing them.

I agree with Roy Smith saying that documentation shouldn't refer to the
"current version".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zipping a dictionary whose values are lists

2012-04-13 Thread Alexander Blinne
Am 12.04.2012 18:38, schrieb Kiuhnm:
> Almost. Since d.values() = [[1,2], [1,2,3], [1,2,3,4]], you need to use
> list(zip(*d.values()))
> which is equivalent to
> list(zip([1,2], [1,2,3], [1,2,3,4]))
> 
> Kiuhnm

While this accidently works in this case, let me remind you that
d.values() does not return the elements of the d in any specific order.
(It is a non-random but implementation-specific order, see
.) Thus if you
need the correct order (as suggested by the dict keys) an explicit
sorting step is required, for example

zip(*[x[1] for x in sorted(d.items(), key=lambda y: y[0])])

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


Re: why () is () and [] is [] work in other way?

2012-04-21 Thread Alexander Blinne
Am 21.04.2012 05:25, schrieb Rotwang:
> On 21/04/2012 01:01, Roy Smith wrote:
>> In article<877gxajit0@dpt-info.u-strasbg.fr>,
>>   Alain Ketterlin  wrote:
>>
>>> Tuples are immutable, while lists are not.
>>
>> If you really want to have fun, consider this classic paradox:
>>
> [] is []
>> False
> id([]) == id([])
>> True
> 
> Huh. This is not what I would have expected. What gives?

This happens only because the first [] gets destroyed after evaluation
of id([]). The second [] then by accident gets the same id as the first
one had.

>>> a = []
>>> b = []
>>> id(a) == id(b)
False

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


Re: why () is () and [] is [] work in other way?

2012-04-23 Thread Alexander Blinne
Am 21.04.2012 14:51, schrieb gst:
> Hi,
> 
> I played (python3.2) a bit on that and :
> 
> case 1) Ok to me (right hand side is a tuple, whose elements are evaluated 
> one per one and have same effect as your explanation (first [] is destroyed 
> right before the second one is created) :
> 
 x, y = id([]), id([])
 x == y
> True

> 
> 
> case 2) also ok to me:
> 
 x = id([]) ; y = id([])
 x == y
> True

> 
> 
> case 3) NOT ok to me :
> 
 x = id([])
 y = id([])
 x == y
> False

> 
> 
> case 4) ok to me :
> 
 def f():
>   x = id([])
>   y = id([])
>   return x == y
> 
 f()
> True

> 
> 
> I'd have thought that cases 2&3 are totally, while 3&4 nearly, syntactically 
> equal and that case 3 is the incorrect result..
> 
> how would you explain case 3 vs cases 2 and 4 ??

It is simply not defined if, after creation and destruction of an empty
list with some id, a newly created empty list should carry the same id
or not. In cases 1,2 and 4 it happens, but in case 3 it doesn't. This is
simply an implementation detail and neither behaviour is right or wrong.

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


Re: which one do you prefer? python with C# or java?

2012-06-11 Thread Alexander Blinne
On 10.06.2012 23:27, Paul Rubin wrote:
> Here is an exercise from the book that you might like to try in Python:
> 
>   http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#%_idx_3894
> 
> It's not easy ;-)

I liked this exercize. At first I wrote my own merger.

> def merge(*iterables):
> iterables = list(iterables)
> current = [i.next() for i in iterables]
> last = None
> while True:
> m = min(current)
> while last == m:
> p = current.index(m)
> try:
> current[p] = iterables[p].next()
> except StopIteration:
> del current[p]
> del iterables[p]
> if len(current) == 0:
> raise StopIteration
> m = min(current)
> yield m
> last = m

But then I realised the vast library of python already contained (a
faster) one (propably based upon
), which
just needed to be enhanced a little bit to allow duplicate items to be
removed:

> import heapq
> 
> def skipdups(m):
> l = k = m.next()
> yield k
> while True:
> while l == k:
> k = m.next()
> yield k
> l = k
> 
> def gen_s():
>   s = [1]
>   m = skipdups(heapq.merge(*[(lambda j: (k*j for k in s))(n) for n in 
> [2,3,5]]))
>   yield s[0]
>   while True:
>   k = m.next()
>   s.append(k)
>   yield k

Now gen_s() generates the wanted sequence.

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


Re: which one do you prefer? python with C# or java?

2012-06-15 Thread Alexander Blinne
On 15.06.2012 09:00, Paul Rubin wrote:
> Alexander Blinne  writes:
>>> def gen_s():
>>>   s = [1]
>>>   m = skipdups(heapq.merge(*[(lambda j: (k*j for k in s))(n) for n in 
>>> [2,3,5]]))
>>>   yield s[0]
>>>   while True:
>>>   k = m.next()
>>>   s.append(k)
>>>   yield k
> 
> Nice.  I wouldn't have been sure that "for k in s" worked properly when
> s was changing like that.

I just tried it and it worked. Not sure if it is guaranteed.

> There is a space complexity problem compared to the Scheme or Haskell
> version: all the s[i]'s are saved in the s array, instead of being
> discarded once they are yielded.  That means generating n elements needs
> O(n) space instead of O(n**0.7) or something like that.  I guess you can
> get around it with collections.deque instead of a list.

An Element of s could be discarded, after every one of the three (k*j
for k in s)-generators went over it. I don't think that this is possible
with one deque (at least with the built-in merger of heapq, a
self-written one could be adapted). Storing everything three times (one
deque for every generator) would be a mess as well.

"Manual" garbage collection could be done by discarding all elements
smaller one fifth of the current element of s, because the three
generators already went by them. This could be done with a deque.

How do Haskell or Scheme determine when elements are not longer needed?

Greetings


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


Re: why the following python program does not face any concurrency problems without synchronize mechanism?

2011-07-09 Thread Alexander Kapps

On 09.07.2011 22:45, smith jack wrote:

from threading import Thread

def calc(start, end):
 total = 0;
 for i in range(start, end + 1):
 total += i;
 print '--result:', total
 return total

t = Thread(target=calc, args=(1,100))
t.start()

I have run this program for many times,and the result is always 5050,
if there is any concurrency problem, the result should not be 5050,
which is never met, anyhow
I mean this program should get the wrong answer at some times, but
this never happens, why?
can concurrency without synchronize mechanism always get the right answer?
any special case in python programming?


Why do you think, that there's a concurrency problem?

All variables are local to the calc() function and all calc() 
invocations run in an own thread. No thread tries to access any 
shared data, so why should there be a concurrency problem?


Concurrency is an issue, when two or more threads/processes try to 
access the same data, but in your program everything is local to the 
calc() function.

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


Re: How can I make a program automatically run once per day?

2011-07-09 Thread Alexander Kapps

On 10.07.2011 02:26, John Salerno wrote:

I have a script that does some stuff that I want to run every day for
maybe a week, or a month. So far I've been good about running it every
night, but is there some way (using Python, of course) that I can make
it automatically run at a set time each night?


Use your operating system's facilities to run timed jobs.

Unix/Linux: Cron jobs
Windows: Scheduled Tasks
Mac: don't know, but probably Cron too
--
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension to do os.path.split_all ?

2011-07-28 Thread Alexander Kapps

On 28.07.2011 22:44, Ian Kelly wrote:

On Thu, Jul 28, 2011 at 2:18 PM, gry  wrote:

[python 2.7] I have a (linux) pathname that I'd like to split
completely into a list of components, e.g.:
   '/home/gyoung/hacks/pathhack/foo.py'  -->['home', 'gyoung',
'hacks', 'pathhack', 'foo.py']

os.path.split gives me a tuple of dirname,basename, but there's no
os.path.split_all function.

I expect I can do this with some simple loop, but I have such faith in
the wonderfulness of list comprehensions, that it seems like there
should be a way to use them for an elegant solution of my problem.
I can't quite work it out.  Any brilliant ideas?   (or other elegant
solutions to the problem?)


path = '/home/gyoung/hacks/pathhack/foo.py'
parts = [part for path, part in iter(lambda: os.path.split(path), ('/', ''))]
parts.reverse()
print parts

But that's horrendously ugly.  Just write a generator with a while
loop or something.



pathname = '/home/gyoung/hacks/pathhack/foo.py'
parts = [part for part in pathname.split(os.path.sep) if part]
print parts

['home', 'gyoung', 'hacks', 'pathhack', 'foo.py']
--
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension to do os.path.split_all ?

2011-07-29 Thread Alexander Kapps

On 29.07.2011 21:30, Carl Banks wrote:


It's not even fullproof on Unix.

'/home//h1122/bin///ghi/'.split('/')

['','home','','bin','','','ghi','']

The whole point of the os.path functions are to take care of whatever oddities 
there are in the path system.  When you use string manipulation to manipulate 
paths, you bypass all of that and leave yourself open to those oddities, and 
then you find your applications break when a user enters a doubled slash.

So stick to os.path.


Carl Banks


This would also be fixed with normpath() as Dennis Lee Bieber 
suggested. And my solution with list comprehensions handles this too.


Still, there might be other path oddities which would break here. I 
think, that something like a split_all() function should be 
available in the stdlib, no?


Actually, it isn't the first time, where I wonder why 
os.path.split() doesn't do this already. I mean, str.split() doesn't 
only split on the first part, right?

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


Re: Hardlink sub-directories and files

2011-08-08 Thread Alexander Gattin
Hello,

On Tue, Aug 02, 2011 at 02:32:54AM -0700, loial
wrote:
> This works fine for files, but the directory
> also contains sub- directories (which themselves
> contain files and sub-directories).  However I
> do not think it is possible to hard link
> directories ?

On some Unices it is, as I heard. But in general,
it's not always possible to even hardlink to an
ordinary file:
1) across filesystem boundary
2) on GRSEC system, if you don't own the original
   file

-- 
WBR,
xrgtn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Linux : create a user if not exists

2011-08-16 Thread Alexander Kapps

On 16.08.2011 16:57, smain kahlouch wrote:

Ok than you. You're right but it doesn't help me :
I replaced it :

 >>> def finduser(user):
... if pwd.getpwnam(user):
... print user, "user exists"
... return True
... return False
...
 >>> finduser('realuser')
realuser user exists
True
 >>> finduser('blabla')
Traceback (most recent call last):
   File "", line 1, in ?
   File "", line 2, in finduser
KeyError: 'getpwnam(): name not found: blabla'



Untested:

def finduser(name):
try:
return pwd.getpwnam(name)
except KeyError:
return None

if not finduser("myuser"):
print "creating user..."
else:
print "user already exists"


Has the advantage that finduser() returns the user details if needed.


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


Re: I think I found 2 undocumented string format codes.

2011-08-24 Thread Alexander Kapps

On 24.08.2011 22:45, Bill wrote:

My google-fu has failed me in finding info on %h and %l string
formatting codes.


'%h' %'hello'

exceptions.ValueError: incomplete format


'%l' %'hello'

exceptions.ValueError: incomplete format

Does anyone know what doing a "complete format" means?


See

http://docs.python.org/library/stdtypes.html#string-formatting-operations

The formatting codes have been borrowed from the C function 
sprintf(), where l and h are length modifier for integers (%hi, %li, 
...)


The Python docs (link above) say:

"A length modifier (h, l, or L) may be present, but is ignored as it 
is not necessary for Python – so e.g. %ld is identical to %d."


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


Re: Tk MouseWheel Support

2011-03-10 Thread Alexander Kapps

On 10.03.2011 21:28, Richard Holmes wrote:

I am trying to use the mouse wheel to scroll a list box, but I'm not
getting the event. When I bind "" to the listbox I get the
event and I'm able to scroll using yview_scroll. I've tried binding
"MouseWheel" and"", and I've also tried"" and
"" even though I'm using Windows (XP, if that makes a
difference). None of these works (I'm using print to see if I got to
an event handler, and there's no printout).

TIA for any help!

Dick


Can you post your code please (if it's too long, strip it down to 
the smallest program which still shows the problem.)


On Ubuntu 10.04, Python 2.6.5, the Listbox already recognizes the 
mouse wheel. Listbox aside, the following works here:



import Tkinter as tk

def wheel_up(event):
print "wheel_up", event

def wheel_down(event):
print "wheel_down", event

root = tk.Tk()
root.bind("", wheel_up)
root.bind("", wheel_down)
root.mainloop()
--
http://mail.python.org/mailman/listinfo/python-list


Re: attach to process by pid?

2011-03-10 Thread Alexander Kapps

On 10.03.2011 23:25, Nobody wrote:

On Thu, 10 Mar 2011 20:22:11 +0100, Giampaolo Rodolà wrote:


I think he wants to attach to another process's stdin/stdout and
read/write from/to them.
I don't know if this is possible but it would be a great addition for psutil.


It's not even a meaningful concept, let alone possible.


Unless I misunderstand something, it is possible (at least on Linux):

Two terminal windows:

1:
alex@frittenbude:~$ grep foobar

2:
alex@frittenbude:~$ ps ax|grep 'grep foobar'
13075 pts/4S+ 0:00 grep --color=auto grep foobar
alex@frittenbude:~$ echo foobar > /proc/13075/fd/0

That this is *highly* system dependent, problematic in many regards 
and just terrible hackery is another issue.


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


OT: processes, terminals and file descriptors on *nix (was: Re: attach to process by pid?)

2011-03-11 Thread Alexander Kapps

On 11.03.2011 03:18, Nobody wrote:

On Thu, 10 Mar 2011 23:55:51 +0100, Alexander Kapps wrote:


I think he wants to attach to another process's stdin/stdout and
read/write from/to them.
I don't know if this is possible but it would be a great addition for
psutil.


It's not even a meaningful concept, let alone possible.


Unless I misunderstand something,


You do ...


Many thanks for the correction and lesson (to Grand Edwards too)!

I still try to digest your explanations. I thought, that processes 
just do something like dup()'ing the file descriptors of their 
terminal but after some strace experiments, I think that is totally 
wrong.


I'd like to learn more about this (how processes, their controlling 
terminals and the std file descriptors relate)


Do you know any links to deeper material (tried Google but what I've 
found is to shallow)

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


Get Path of current Script

2011-03-14 Thread Alexander Schatten
Hi,

could someone help me with a small problem? I wrote a Python script
that does some RegEx... transformations. Now, this script loads some
configuration data from a file located in the same directory:

open ('config.txt', 'r').

However, this only works when I execute the script being in the
directory where the script is locates, because otherwise, of course,
this config file is not found, as the path is relative. Now my
question: is there an easy way (API) to get the directory of the
currently running script? Something along the line of:

open (API.getCurrentPath + 'config.txt', 'r').


thanks a lot,

cheers

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


Re: Get Path of current Script

2011-03-14 Thread Alexander Schatten
Thanks for the comments so far. This sounds to be more complicated in
detail than I expected. I wonder how all the other Python programs and
scripts are doing that...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get Path of current Script

2011-03-14 Thread Alexander Schatten
They don't. Hm, ok, I am always for best practices. If there is a
better way to do it I am open for suggestions ;-) How would the best
practice be to load configuration data from a file.

I mean, this is something very common: you write a program or a script
and want to load some configuration data.


thanks


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


Re: os.path.walk() to get full path of all files

2011-03-16 Thread Alexander Kapps

On 16.03.2011 20:41, dude wrote:

My goal is create a list of absolute paths for all files in a given
directory (any number of levels deep).

root
dir1
file1
file2
dir2
file3
dir3
-dir4
--file4
file5

So the above would return:
[root/dir1/file1, root/dir1/file2, root/dir1/dir2/file3, etc...]

I've been trying different ways of using os.path.walk() for that, but
I can't find an elegant way.  Anyone know of something simple to
accomplish that?


Try this:

file_list = []
for root, _, filenames in os.walk(root_path):
for filename in filenames:
file_list.append(os.path.join(root, filename))

for x in file_list:
print x
--
http://mail.python.org/mailman/listinfo/python-list


Re: OT: processes, terminals and file descriptors on *nix

2011-03-16 Thread Alexander Kapps

On 13.03.2011 01:50, Nobody wrote:


I don't have any links. If you want to understand the core Unix API, the
best reference I know of is Stevens ("Advanced Programming in the Unix
Environment", by W. Richard Stevens). Unfortunately, it's not cheap.

In spite of the title, it doesn't assume any prior Unix knowledge; the
"Advanced" mainly refers to the fact that it's the opposite of the "Learn
X in Five Minutes" books, i.e. it's thorough. That's how it comes to>700
pages while only covering the core API (files, processes, and terminals;
no sockets, no X, no I18N, ...).



Thanks, Nobody and Dan. I'll have a look at the book.
--
http://mail.python.org/mailman/listinfo/python-list


Re: os.path.walk() to get full path of all files

2011-03-16 Thread Alexander Kapps

On 16.03.2011 22:00, dude wrote:

awesome, that worked.  I'm not sure how the magic is working with your 
underscores there, but it's doing what I need.  thanks.


The underscore is no magic here. It's just a conventional variable 
name, saying "I m unused". One could also write:


for root, UNUSED, filenames in os.walk(root_path):
   ...


BTW. Please quote enough of the posts you reply to. Most people 
access this list/newsgroup per mail client or usenet reader, not per 
a webpage, so without quoting, they might not see the context of our 
post. Thank you.

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


Re: class error

2011-03-18 Thread Alexander Kapps

On 18.03.2011 21:13, monkeys paw wrote:

I have the following file:

FileInfo.py:

import UserDict


After this import statement, the name "UserDict" refers to the module.


class FileInfo(UserDict):


Here you are trying to subclass the module. What you need instead is:

class FileInfo(UserDict.UserDict):

Alternatively, import the UserDict class from the UserDict module 
like so:


from UserDict import UserDict

Note, that the UserDict class is obsolete, you can subclass the dict 
type directly:


class FileInfo(dict):
"store file metadata"
def __init__(self, filename=None):
dict.__init__(self)
self["name"] = filename
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading/Writing files

2011-03-18 Thread Alexander Kapps

On 18.03.2011 22:33, Jon Herman wrote:

Hello all,

I am pretty new to Python and am trying to write data to a file. However, I
seem to be misunderstanding how to do so. For starters, I'm not even sure
where Python is looking for these files or storing them. The directories I
have added to my PYTHONPATH variable (where I import modules from
succesfully) does not appear to be it.

So my question is: How do I tell Python where to look for opening files, and
where to store new files?

Thanks,

Jon





There is no special place where Python looks for normal files 
(PYTHONPATH is just tells where to look for modules.)


If you open a file (for reading and/or writing) and don't give an 
absolute path, then the filename is relative to the CWD (Current 
Working Directory). One can change this directory with os.chdir() 
*but* you shouldn't need to do that. Either start Python in the 
desired directory (your operating system will have a way to do this 
for icons and menu entries) or give an absolute path like:


f = open("/home/alex/test.txt")
f = open("c:/test.txt")

Also check out os.environ, a dict containing the environment 
variables. There you will find variables like $HOME or %USERPROFILE% 
(if that's correct) which tells where your personal files are:


import os

# my home directory is /home/alex
homedir = os.environ["HOME"]

# creates the file /home/alex/test.txt
f = open(os.path.join(homedir, "test.txt"))
--
http://mail.python.org/mailman/listinfo/python-list


Re: install excel xlwt in ubuntu 9

2011-03-19 Thread Alexander Kapps

On 19.03.2011 07:29, ratna PB wrote:

Hey friends i tried a lot to install excel xlwt in ubuntu 9 but
failed
please help me before i get full fraustrated...


What have you tried and how did it failed?

On 9.10, simply do (you might need to enable the universe repository 
in Synaptic first):


$ sudo apt-get install python-xlwt

Alternatively, install python-setuptools and then use easy_install:

$ sudo apt-get install python-setuptools
$ sudo easy_install xlwt
--
http://mail.python.org/mailman/listinfo/python-list


Re: send function keys to a legacy DOS program

2011-03-19 Thread Alexander Gattin
Hello,

On Thu, Mar 10, 2011 at 04:58:53PM -0800, Justin
Ezequiel wrote:
> We have an old barcode program (MSDOS and source code unavailable.)
> I've figured out how to populate the fields (by hacking into one of
> the program's resource files.)
> However, we still need to hit the following function keys in sequence.
> F5, F2, F7
> Is there a way to pipe said keys into the program?

It's a very old and good known trick IMO. You just
need to write keycodes to kbd ring buffer in BIOS
data area (I don't remember exact address,
soemewhere in 1st 4kbytes of PC memory), then wait
till the scancode gets consumed. I'd put "scancode
feeding" code into timer interrupt handler.

-- 
With best regards,
xrgtn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: send function keys to a legacy DOS program

2011-03-19 Thread Alexander Gattin
On Sun, Mar 20, 2011 at 12:52:28AM +0200,
Alexander Gattin wrote:
> On Thu, Mar 10, 2011 at 04:58:53PM -0800, Justin
> Ezequiel wrote:
> > We have an old barcode program (MSDOS and source code unavailable.)
> > I've figured out how to populate the fields (by hacking into one of
> > the program's resource files.)
> > However, we still need to hit the following function keys in sequence.
> > F5, F2, F7
> > Is there a way to pipe said keys into the program?
> 
> It's a very old and good known trick IMO. You just
> need to write keycodes to kbd ring buffer in BIOS
> data area (I don't remember exact address,
> soemewhere in 1st 4kbytes of PC memory), then wait
> till the scancode gets consumed. I'd put "scancode
> feeding" code into timer interrupt handler.

Taken from RBIL/MEMORY.LST
(http://www.cs.cmu.edu/~ralf/interrupt-list/inter61c.zip):

K-M0040001A--
MEM 0040h:001Ah - KEYBOARD - POINTER TO NEXT CHARACTER IN KEYBOARD BUFFER
Size:   WORD
SeeAlso: MEM 0040h:001Ch,MEM 0040h:0080h,MEM 0040h:0082h,INT 16/AH=00h
K-M0040001C--
MEM 0040h:001Ch - KEYBOARD - POINTER TO FIRST FREE SLOT IN KEYBOARD BUFFER
Size:   WORD
SeeAlso: MEM 0040h:001Ah,MEM 0040h:001Eh,MEM 0040h:0080h,MEM 0040h:0082h
SeeAlso: INT 16/AH=00h
K-M0040001E--
MEM 0040h:001Eh - KEYBOARD - DEFAULT KEYBOARD CIRCULAR BUFFER
Size:   16 WORDs
SeeAlso: MEM 0040h:001Ah,MEM 0040h:001Ch,MEM 0040h:0080h,MEM 0040h:0082h
SeeAlso: INT 16/AH=00h,INT 16/AH=05h

Buffer is considered empty if
word[0x41A]==word[0x41C] IIRC.

You need to place 2 bytes into the circular buffer
to simulate key press. Lower byte is ASCII code,
higher byte is scan code (they are the same for
functional keys, whe using default keycode set#1):

F5: 0x3F 0x3F
F2: 0x3C 0x3C
F7: 0x41 0x41

-- 
With best regards,
xrgtn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Incompatible _sqlite3.so

2011-03-27 Thread Alexander Kapps

On 27.03.2011 23:24, Tim Johnson wrote:

I have python 2.6.5 on my main workstation with ubuntu 10.04. I am
attempting to set up a temporary test platform on an asus netbook
with slax running from an SD card. I have installed a python 2.7
module on the slax OS. (I can't find a python 2.6.5 module for
slax). For those who don't know, slax is a "pocket" OS and is very
limited. In the original slax install with python 2.7, there is not
_sqlite3.so available. I copied _sqlite3.so from my workstation and
I am getting the following error message:
"... undefined symbol: PyUnicodeUCS4_DecodeUTF8."

I suspect that this is a version problem. I'd like to try
_sqlite3.so for python 2.7 (32-bit). If anyone has one or can tell
me where get one, I would appreciate it. I am reluctant to install
2.7 on my workstation right now.

thanks


Slax is a very modular and flexible "pocket OS"/Live distro. It's 
everything but limited .There are many additional modules available.


Python 2.6:
http://www.slax.org/modules.php?action=detail&id=3118

Several sqlite related packages, you probably need one of the 
pysqlite modules:

http://www.slax.org/modules.php?search=sqlite&category=

Finally, it's quite easy to build your own Slax modules:
http://www.slax.org/documentation_create_modules.php


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


Re: Incompatible _sqlite3.so

2011-03-27 Thread Alexander Kapps

On 28.03.2011 00:21, Tim Johnson wrote:


Python 2.6:
http://www.slax.org/modules.php?action=detail&id=3118


  That module is *not* trusted. See the warning?


It's just not verified by the Slax developers. That doesn't mean 
it's not trusted. It's the same as with Ubuntu packages from the 
Universe repo, or Firefox plugins which haven't been verified by the 
Mozilla team. None of them should be used where security matters, 
but on a testing system, the danger should be rather low.


If you limit yourself to strictly distro developer approved packages 
than I fear it's going to be hard to find one.


If all else fails, you could probably use the _sqlite3.so from 
another distro which has Python 2.7 officially.



  quickest would be the so file itself, maybe from the slack site or
  if someone has one they can send me.


You don't trust an unverified package from the Slax site, but you 
would trust some other stranger on the python-list, to not give you 
a manipulated .so? You're either too paranoid or not paranoid enough ;-)

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


Re: send function keys to a legacy DOS program

2011-03-29 Thread Alexander Gattin
Hello,

On Sun, Mar 20, 2011 at 06:59:46PM -0700, Justin
Ezequiel wrote:
> On Mar 20, 7:30 am, Alexander Gattin
>  wrote:
> > You need to place 2 bytes into the circular buffer
> > to simulate key press. Lower byte is ASCII code,
> > higher byte is scan code (they are the same for
> > functional keys, whe using default keycode set#1):
> >
> > F5: 0x3F 0x3F
> > F2: 0x3C 0x3C
> > F7: 0x41 0x41

I'm not sure regarding the ASCII part. I think it
might need to be set to 0x00 for all functional
keys instead of 0x3F/0x3C/0x41, but probably no
application actually cares.

Another thing is that you may need to send key
release after key press in order for the
application to trigger the F5/F2/F7 event. I'm not
sure what the scan codes for F5/F2/F7 releases
are, but think that they may be:

F5: 0xBF
F2: 0xBC
F7: 0xC1

-- 
With best regards,
xrgtn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ipython: Multiple commands on the same line and newlines

2011-04-17 Thread Alexander Kapps

On 17.04.2011 20:40, Phil Winder wrote:

Ok, thanks all. It's a little disappointing, but I guess that you
always have to work in a different way when you move to a new
language. Andrea's %edit method is probably the best compromise, but
this now means that I will have to learn all the (obscure) shortcuts
for vi!


As you can read in "Python IDE/text-editor" thread. Learning either 
Vim or Emacs will pay off in the long run,


Anyway, IPython honors the $EDITOR environment variable. Just set it 
to whatever editor you prefer.


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


Pickling over a socket

2011-04-19 Thread Roger Alexander
Hi,

I'm trying to understand how to pickle Python objects over a TCP
socket.

In the example below (based on code from Foundations of Python Network
Programming), a client creates a dictionary (lines 34-38) and uses
pickle.dump at line 42 to write the pickled object using file handle
make from a socket. The server de-pickles with pickle.load  (line 24),
again using a file handle made from a socket.

When I run the program, the following output is produced:

Listening at ('127.0.0.1', 1060)
Accepted connection from ('127.0.0.1', 49938)
Traceback (most recent call last):
File "pickles.py", line 24, in 
d = pickle.load( s_fh )
File "/usr/local/lib/python2.7/pickle.py", line 1378, in load
return Unpickler(file).load()
File "/usr/local/lib/python2.7/pickle.py", line 857, in load
key = read(1)
File "/usr/local/lib/python2.7/socket.py", line 380, in read
data = self._sock.recv(left)
socket.error: [Errno 107] Transport endpoint is not connected

I'm at a loss, can anyone provide any guidance?

Thanks,

Roger Alexander

 1  import pickle
 2  import socket, sys
 3
 4  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 5
 6  HOST = sys.argv.pop() if len(sys.argv) == 3 else '127.0.0.1'
 7  PORT = 1060
 8
 9  if sys.argv[1:] == ['server']:
10
11  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
12  s.bind((HOST, PORT))
13  s.listen(1)
14
15  print 'Listening at', s.getsockname()
16
17  sc, sockname = s.accept()
18
19  print 'Accepted connection from', sockname
20
21  sc.shutdown(socket.SHUT_WR)
22  sf = s.makefile( "rb" )
23
24  d = pickle.load(sf)
25
26  sc.close()
27  s.close()
28
29  elif sys.argv[1:] == ['client']:
30
31  s.connect((HOST, PORT))
32  s.shutdown(socket.SHUT_RD)
33
34  d = dict()
35
36  d[ 'Name' ] = 'Jake Thompson.'
37  d[ 'Age' ]  = 25
38  d[ 'Location' ] = 'Washington, D.C.'
39
40  sf = s.makefile( "wb" )
41
42  pickle.dump( d, sf, pickle.HIGHEST_PROTOCOL )
43
44  s.close()
45
46  else:
47  print >>sys.stderr, 'usage: streamer.py server|client [host]'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickling over a socket

2011-04-19 Thread Roger Alexander
Thanks everybody, got it working.

 I appreciate the help!

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


Re: [OT] Disable creation of pyc files in DrPython

2011-04-20 Thread Alexander Kapps

On 20.04.2011 15:21, craf wrote:

Hi.

I wonder if anyone uses Python DrPython as editor.
I need to know if you can disable the creation of
Pyc files created by the program. In the Geany editor you can
add the parameter -B, but not if it can in this editor.


I don't know DrPython, but Python itself checks for the 
$PYTHONDONTWRITEBYTECODE environment variable. Perhaps you can run 
DrPython with a command like:


PYTHONDONTWRITEBYTECODE=1 drpython


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


Python competitions and learnings

2011-04-30 Thread Alexander Lyabah
Hi. My name is Alexander.
I spend a lot of time in writing a new service checkio.org

It's all about python, learn python, find the best solution in
python.

And Im looking for feedback from peoples who best in python. Here I
make some video tutorial about this service http://checkio.blip.tv/

What do you think about it?

I'm also have a not a very good English, so I need help with it too,
because some parts of checkio.org  not in very well English

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


Re: Python competitions and learnings

2011-05-01 Thread Alexander Lyabah
On May 1, 12:29 am, Terry Reedy  wrote:
> On 4/30/2011 3:22 PM, Alexander Lyabah wrote:
>
> > I spend a lot of time in writing a new service checkio.org
>
> > It's all about python, learn python, find the best solution in
> > python.
>
> > And Im looking for feedback from peoples who best in python. Here I
> > make some video tutorial about this servicehttp://checkio.blip.tv/
>
> > What do you think about it?
>
> Pretty impressive. My main disappointment is that you are using 2.7
> instead of 3.2, as I feel that beginners should learn Py 3 now. Also,
> that is what I routinely use ;-).
>
> In any case, the home page should say Python 2.7, not just Python, I had
> to think to click Console Learn in order to find out what would be legal
> when I tried out one of the tasks.
>
> You might consider offering 3.2 as an alternative. Solutions will be
> similar, but details differ. For instance, I noticed that one solution
> to 'string split' *depends* on the 2.x leaking of listcomp loop variables.
>
> > I'm also have a not a very good English, so I need help with it too,
> > because some parts of checkio.org  not in very well English
>
> Home page: "In the descriptions of this tasks there is always
> information from manuals and tutorials," 'these tasks'
>
> "game.Choice a game " => "game. Choose a game "
> "users. Choice a game" => again, 'Choose'
>
> "and fall into the top " I think you mean "and climb into the top "
>
> "programs on arena. " => "program in the arena. "
>
> "By the result of competition formed top of the game. "
> This is unclear. perhaps "The best will be chosen by the result of the
> competition."
>
> I see a mailto: link at the bottom of the page, so I will report
> anything else I see later.
>
> --
> Terry Jan Reedy

thanks for your translation. It's already published on site. And I
will wait for your mail
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python competitions and learnings

2011-05-01 Thread Alexander Lyabah
On May 1, 3:26 am, harrismh777  wrote:
> Alexander Lyabah wrote:
> > What do you think about it?
>
> > I'm also have a not a very good English, so I need help with it too,
>
> Alexander, your site is very interesting. I spent some time this
> afternoon appreciating your work. Nice job.
>
> Be encouraged, your English is much better than my Russian! I also
> looked over the orphanage site and I appreciate how much you are doing
> there in Ukraine for the children. Keep up the good work.
>
> I too am a little disappointed that Python3 is not being used. I want to
> encourage you to work in that direction as well. I may have some time to
> volunteer to checkio.org; for help with the English, and maybe with some
> help as a tester. We'll see... I have a lot of my own fish to fry here,
> as we say in America.
>
> Blessings on your work, friend.
>
> m harris

I spend a lot of time to make a almost full support of python 2.7 from
sandbox.

If somebody help me this sandboxed python 3.* i will add support of it
on checkio.org with big pleasure.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python competitions and learnings

2011-05-01 Thread Alexander Lyabah
And what do you think about Score Games and competitions?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python competitions and learnings

2011-05-02 Thread Alexander Lyabah
On May 1, 7:22 pm, Alexander Lyabah  wrote:
> On May 1, 3:26 am, harrismh777  wrote:
>
>
>
>
>
>
>
>
>
> > Alexander Lyabah wrote:
> > > What do you think about it?
>
> > > I'm also have a not a very good English, so I need help with it too,
>
> > Alexander, your site is very interesting. I spent some time this
> > afternoon appreciating your work. Nice job.
>
> > Be encouraged, your English is much better than my Russian! I also
> > looked over the orphanage site and I appreciate how much you are doing
> > there in Ukraine for the children. Keep up the good work.
>
> > I too am a little disappointed that Python3 is not being used. I want to
> > encourage you to work in that direction as well. I may have some time to
> > volunteer to checkio.org; for help with the English, and maybe with some
> > help as a tester. We'll see... I have a lot of my own fish to fry here,
> > as we say in America.
>
> > Blessings on your work, friend.
>
> > m harris
>
> I spend a lot of time to make a almost full support of python 2.7 from
> sandbox.
>
> If somebody help me this sandboxed python 3.* i will add support of it
> on checkio.org with big pleasure.

Notice about the python 2.7 on main page was added.
thanks for advice
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python competitions and learnings

2011-05-02 Thread Alexander Lyabah
On May 2, 1:33 am, Terry Reedy  wrote:
> On 5/1/2011 12:49 PM, Alexander Lyabah wrote:
>
> > And what do you think about Score Games and competitions?
>
> The rules of the first score game were not clear to me. I could not
> figure out how to play it interactively myself so I could see how it
> actually played.
>
> --
> Terry Jan Reedy

I will add a more detail information how competitions and score games
goes.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python competitions and learnings

2011-05-05 Thread Alexander Lyabah
On May 1, 12:29 am, Terry Reedy  wrote:
> On 4/30/2011 3:22 PM, Alexander Lyabah wrote:
>
> > I spend a lot of time in writing a new service checkio.org
>
> > It's all about python, learn python, find the best solution in
> > python.
>
> > And Im looking for feedback from peoples who best in python. Here I
> > make some video tutorial about this servicehttp://checkio.blip.tv/
>
> > What do you think about it?
>
> Pretty impressive. My main disappointment is that you are using 2.7
> instead of 3.2, as I feel that beginners should learn Py 3 now. Also,
> that is what I routinely use ;-).
>
> In any case, the home page should say Python 2.7, not just Python, I had
> to think to click Console Learn in order to find out what would be legal
> when I tried out one of the tasks.
>
> You might consider offering 3.2 as an alternative. Solutions will be
> similar, but details differ. For instance, I noticed that one solution
> to 'string split' *depends* on the 2.x leaking of listcomp loop variables.
>
> > I'm also have a not a very good English, so I need help with it too,
> > because some parts of checkio.org  not in very well English
>
> Home page: "In the descriptions of this tasks there is always
> information from manuals and tutorials," 'these tasks'
>
> "game.Choice a game " => "game. Choose a game "
> "users. Choice a game" => again, 'Choose'
>
> "and fall into the top " I think you mean "and climb into the top "
>
> "programs on arena. " => "program in the arena. "
>
> "By the result of competition formed top of the game. "
> This is unclear. perhaps "The best will be chosen by the result of the
> competition."
>
> I see a mailto: link at the bottom of the page, so I will report
> anything else I see later.
>
> --
> Terry Jan Reedy

Terry, Hi, sent a mail to you, couple days ago. From
alexan...@lyabah.com

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


Re: PyGTK notebook: get current page

2011-05-07 Thread Alexander Kapps

On 07.05.2011 17:04, Tracubik wrote:

Hi all!
I've made a simple PyGTK program.
It's a window with a notebook, the notebook have 2 pages
When changing page, i'ld like to get the id of current page.

I've coded it, but i can get only the previously open page, not the
current one. This is not a big deal if i have only 2 pages, but it could
be with 3 or more pages.

Here's the code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# template di finestra in pyGTK

import pygtk
pygtk.require('2.0')
import gtk

class FinestraGTK:

 def pippo(self, widget, event, data=None):
 print "current page: " + str(self.nb.get_current_page() )


According to PyGTK docs, the event handler for the switch_page 
signal should look like this:


def pippo(self, notebook, page, page_num, user_data=None):
...

With this method you get the new page number in page_num (or in your 
original method with the misleadingly named "data" argument)



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


Re: turn monitor off and on

2011-05-14 Thread Alexander Kapps

On 14.05.2011 09:29, harrismh777 wrote:

harrismh777 wrote:


def turnOnMonitor():
SC_MONITORPOWER = 0xF170
win32gui.SendMessage(win32con.HWND_BROADCAST,
win32con.WM_SYSCOMMAND, SC_MONITORPOWER, -1)



Wonder what the equivalent of this is in Linux... ?


Probably xset dpms force {on,off,...}
--
http://mail.python.org/mailman/listinfo/python-list


Re: regular expression i'm going crazy

2011-05-16 Thread Alexander Kapps

On 16.05.2011 18:25, Tracubik wrote:

pls help me fixing this:

import re
s = "linka la baba"
re_s = re.compile(r'(link|l)a' , re.IGNORECASE)

print re_s.findall(s)

output:
['link', 'l']

why?


As the docs say:

"If one or more groups are present in the pattern, return a list of 
groups;"


http://docs.python.org/library/re.html?highlight=findall#re.findall


i want my re_s to find linka and la, he just find link and l and forget
about the ending a.


Try with non-grouping parentheses:

re_s = re.compile(r'(?:link|l)a' , re.IGNORECASE)
--
http://mail.python.org/mailman/listinfo/python-list


Debugging reason for python running unreasonably slow when adding numbers

2023-03-14 Thread Alexander Nestorov
I'm working on an NLP and I got bitten by an unreasonably slow behaviour in 
Python while operating with small amounts of numbers.

I have the following code:

```python
import random, time
from functools import reduce

def trainPerceptron(perceptron, data):
  learningRate = 0.002
  weights = perceptron['weights']
  error = 0
  for chunk in data:
      input = chunk['input']
      output = chunk['output']

      # 12x slower than equivalent JS
      sum_ = 0
      for key in input:
          v = weights[key]
          sum_ += v

      # 20x slower than equivalent JS
      #sum_ = reduce(lambda acc, key: acc + weights[key], input)

      actualOutput = sum_ if sum_ > 0 else 0

      expectedOutput = 1 if output == perceptron['id'] else 0
      currentError = expectedOutput - actualOutput
      if currentError:
          error += currentError ** 2
          change = currentError * learningRate
          for key in input:
              weights[key] += change

  return error

# Build mock data structure
data = [{
   'input': random.sample(range(0, 5146), 10),
   'output': 0
} for _ in range(11514)]
perceptrons = [{
   'id': i,
   'weights': [0.0] * 5147,
} for i in range(60)] # simulate 60 perceptrons

# Simulate NLU
for i in range(151): # 150 iterations
  hrstart = time.perf_counter()
  for perceptron in perceptrons:
    trainPerceptron(perceptron, data)
  hrend = time.perf_counter()
  print(f'Epoch {i} - Time for training: {int((hrend - hrstart) * 1000)}ms')
```

Running it on my M1 MBP I get the following numbers.

```
Epoch 0 - Time for training: 199ms
Epoch 1 - Time for training: 198ms
Epoch 2 - Time for training: 199ms
Epoch 3 - Time for training: 197ms
Epoch 4 - Time for training: 199ms
...
Epoch 146 - Time for training: 198ms
Epoch 147 - Time for training: 200ms
Epoch 148 - Time for training: 198ms
Epoch 149 - Time for training: 198ms
Epoch 150 - Time for training: 198ms
```

Each epoch is taking around 200ms, which is unreasonably slow given the small 
amount of numbers that are being processed. I profiled the code with `cProfile` 
in order to find out what is going on:

```
         655306 function calls (655274 primitive calls) in 59.972 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      3/1    0.000    0.000   59.972   59.972 {built-in method builtins.exec}
        1    0.005    0.005   59.972   59.972 poc-small.py:1()
     9060   59.850    0.007   59.850    0.007 poc-small.py:4(trainPerceptron)
        1    0.006    0.006    0.112    0.112 poc-small.py:34()
    11514    0.039    0.000    0.106    0.000 random.py:382(sample)
   115232    0.034    0.000    0.047    0.000 
random.py:235(_randbelow_with_getrandbits)
    11548    0.002    0.000    0.012    0.000 {built-in method 
builtins.isinstance}
    11514    0.002    0.000    0.010    0.000 :117(__instancecheck__)
   183616    0.010    0.000    0.010    0.000 {method 'getrandbits' of 
'_random.Random' objects}
    11514    0.002    0.000    0.008    0.000 {built-in method 
_abc._abc_instancecheck}
    11514    0.002    0.000    0.006    0.000 :121(__subclasscheck__)
   115140    0.005    0.000    0.005    0.000 {method 'add' of 'set' objects}
    11514    0.003    0.000    0.004    0.000 {built-in method 
_abc._abc_subclasscheck}
   115232    0.004    0.000    0.004    0.000 {method 'bit_length' of 'int' 
objects}
      151    0.003    0.000    0.003    0.000 {built-in method builtins.print}
```

This wasn't too helpful, so I tried with [line_profiler][1]:

```
Timer unit: 1e-06 s

Total time: 55.2079 s
File: poc-small.py
Function: trainPerceptron at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==
     4                                           @profile
     5                                           def 
trainPerceptron(perceptron, data):
     6      1214        301.0      0.2      0.0    learningRate = 0.002
     7      1214        255.0      0.2      0.0    weights = 
perceptron['weights']
     8      1214        114.0      0.1      0.0    error = 0
     9  13973840    1742427.0      0.1      3.2    for chunk in data:
    10  13973840    1655043.0      0.1      3.0        input = chunk['input']
    11  13973840    1487543.0      0.1      2.7        output = chunk['output']
    12
    13                                                 # 12x slower than 
equivalent JS
    14  13973840    1210755.0      0.1      2.2        sum_ = 0
    15 139738397   13821056.0      0.1     25.0        for key in input:
    16 139738397   13794656.0      0.1     25.0            v = weights[key]
    17 139738396   14942692.0      0.1     27.1            sum_ += v
    18
    19                                                 # 20x slower than 
equivalent JS
    20                                                 #sum_ = reduce(lambda 
acc, key: acc + weights[key], input)
    21
    22  13973839    1618273.0      0

Re: How Do I Get A Bug In Multiprocessing Fixed?

2021-06-17 Thread Alexander Neilson
Hi Michael

It may be helpful to populate the ticket with further details:
* actual output from when you execute the server and client (possibly with
extra verbosity enabled)
* system you are running this on (windows, macos, linux) flavour / version
details
* minor version of Python interpreter used
* whether you are using a packaged version from your os distributor or from
python.org (or even built your own)
* If you believe it's a regression the version it is working on

Some more guidance can be found here covering some of the above
https://docs.python.org/3/bugs.html

I am very interested to note that a report filed in February hasn't had at
least one person take a brief look at it and post a note or set a status
like "needs more info" etc.

Also I am by no means an expert in multi processing at all as so far my
webapps work worker processes, I am wondering if the wrong approach to this
may be happening and so the client is trying to reuse the same pipe and it
may need a different tear down / process to release that and create a new
pipe / socket / underlying connection cleanly rather than the re connect in
the same object.

For example I am running python 3.7.7 on windows:

Manager Output:
>python manager.py
In test_method

>python manager.py


Client Output:
result: ', TEST'
Kill and restart the server and press return

Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting
Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting
Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting
Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting
Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting
Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting
Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting
Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting
Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting
Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting
Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting
Got exception , ConnectionResetError(10054,
'An existing connection was forcibly closed by the remote host', None,
10054, None)
Reconnecting  # At this point I terminated the manager
Got exception ,
ConnectionRefusedError(10061, 'No connection could be made because the
target machine actively refused it', None, 10061, None)
Reconnecting
Traceback (most recent call last):
  File
"C:\Users\Alexander\AppData\Local\Programs\Python\Python37\lib\multiprocessing\connection.py",
line 619, in SocketClient
s.connect(address)
ConnectionRefusedError: [WinError 10061] No connection could be made
because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "client.py", line 27, in 
manager.connect()
  File
"C:\Users\Alexander\AppData\Local\Programs\Python\Python37\lib\multiprocessing\managers.py",
line 532, in connect
conn = Client(self._address, authkey=self._authkey)
  File
"C:\Users\Alexander\AppData\Local\Programs\Python\Python37\lib\multiprocessing\connection.py",
line 492, in Client
c = SocketClient(address)
  File
"C:\Users\Alexander\AppData\Local\Programs\Python\Python37\lib\multiprocessing\connection.py",
line 619, in SocketClient
s.connect(address)
KeyboardInterrupt




Regards
Alexander

Alexander Neilson
Neilson Productions Limited

alexan...@neilson.net.nz
021 329 681
022 456 2326


On Fri, 18 Jun 2021 at 15:27, Michael Boom  wrote:

> The below issue is pretty serious and it is preventing me from using a
> system I wrote on a larger scale.  How do I get this bug fixed?  Thanks.
> https://bugs.python.org/issue43329
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


(Python 3.5) Asyncio and an attempt to run loop.run_until_complete() from within a running loop

2016-04-08 Thread Alexander Myodov
Hello.

TLDR: how can I use something like loop.run_until_complete(coro), to execute a 
coroutine synchronously, while the loop is already running?

More on this:

I was trying to create an aio_map(coro, iterable) function (which would 
asynchronously launch a coroutine for each iteration over iterable, and collect 
the data; similary to gevent.pool.Group.imap() from another async world), but 
stuck while attempting to make it well both from outside the async event loop 
and from inside one - any help?

My code is at http://paste.pound-python.org/show/EQvN2cSDp0xqXK56dUPy/ - and I 
stuck around lines 22-28, with the problem that loop.run_until_complete() 
cannot be executed when the loop is running already (raising "RuntimeError: 
Event loop is running."). Is it normal? and why is it so restrictive? And what 
can I do to wait for `coros` Future to be finished?

I tried various mixes of loop.run_forever() and even loop._run_once() there, 
but was not able to create a stable working code. Am I doing something 
completely wrong here? Am I expected to create a totally new event loop for 
synchronously waiting for the Future, if the current event loop is running - 
and if so, won't the previous event loop miss any its events?


Thank you in advance.
Alexander
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: (yet another) PEP idea to tackle binary wheels problem efficiently

2019-02-18 Thread Alexander Revin
Two minor typos: platform tag should be separated by "-", not "_".
Also it makes sense to use "amd64" instead of "x86_64", so platform
can be just split by "_"

On Sat, Feb 16, 2019 at 9:29 PM Alexander Revin  wrote:
>
> Hi all,
>
> I've been thoroughly reading various discussions, such as [1], [2] and
> related ones regarding PEP 425, PEP 491 and PEP 513. I also happen to
> use musl sometimes, so as discussed here [3] I thought it would be a
> good idea to submit a new PEP regarding musl compatibility.
>
> It's not a secret that everyday wheel usage on Linux is far from
> perfect. Some packages are trying to compile when there's no compiler
> on the system available, some rely on 3rd-party deps and explode when
> they cannot find required headers installed and so on. Add to this
> cross-compilation support (quite complicated) and distros like Alpine
> or just something not using x86 (how many piwheels.org-like should
> emerge for each non-x86 platform?). For example, I would like to
> seamlessly install numpy, pandas and scikit onto ppc machine running
> Gentoo musl and not waste 1 hour for compilation, or "just" use them
> in x86 standard alpine-based docker image (basically what [3] is all
> about).
>
> Long story short, current wheel filename format:
>
> {distribution}-{version}(-{build tag})?-{python tag}-{abi
> tag}-{platform tag}.whl.
>
> Doesn't not properly express package expectation. Let's take a look at
> pandas wheels ([4]):
>
> pandas-0.24.1-cp36-cp36m-manylinux1_x86_64.whl
> pandas-0.24.1-cp36-cp36m-win_amd64.whl
> pandas-0.24.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
>
> I see issues with each of them:
> 1. First one won't work on Alpine or any musl-based distro;
> 2. Second – how amd64 is different from x86_64?
> 3. Third's platform tag is just a nightmare.
>
> More of that, if you open the last one and inspect one of the
> libraries, you'll find that:
> $ file _libs/algos.cpython-36m-darwin.so
> _libs/algos.cpython-36m-darwin.so: Mach-O universal binary with 2
> architectures: [i386:Mach-O bundle i386] [x86_64]
> _libs/algos.cpython-36m-darwin.so (for architecture i386): Mach-O bundle i386
> _libs/algos.cpython-36m-darwin.so (for architecture x86_64): Mach-O
> 64-bit bundle x86_64
>
> It's universal library! So not x86_64 only, as mentioned in the quite
> long macosx_10_various platform tag.
>
> TL;DR What my solution? To use something widely called "Target
> Triplet" [5], omitting usual "vendor" field, so
> {platform tag} from PEP 435 will have the format of _[_]:
>
> pandas-0.24.1-cp36-cp36m-x86_64_linux_gnu.whl
> pandas-0.24.1-cp36-cp36m-x86_64_linux_musl.whl
> pandas-0.24.1-cp36-cp36m-x86_windows.whl
> pandas-0.24.1-cp36-cp36m-x86_64_windows_msvs2010.whl
> pandas-0.24.1-cp36-cp36m-x86_macosx_10_6.whl <-- won't be used for
> anything Mojave+, see [6]
> pandas-0.24.1-cp36-cp36m_aarch64_netbsd.whl
>
> Primary concerns here:
> - Arch and os are specified more consistently;
> - Environment is specified only when needed;
> - Lots of possible combinations of os and env are possible :)
>
> Since most runtimes are hardcoded during build time anyway and changed
> for each Python release, explicit versioning shouldn't be a problem.
>
> JavaScript and Rustlang [7] use similar naming scheme. Though I don't
> like both of them, at least portability of extensions looks less
> broken than of Python (I've worked on native Nodejs extension in the
> past).
>
>
> What do you think?
>
> Thanks,
> Alex
>
> [1] 
> https://mail.python.org/archives/list/distutils-...@python.org/thread/KCLRIN4PTUGZLLL7GOUM23S46ZZ2D4FU/
> [2] https://github.com/pypa/packaging-problems/issues/69
> [3] https://github.com/pypa/manylinux/issues/37
> [4] https://pypi.org/project/pandas/#files
> [5] https://wiki.osdev.org/Target_Triplet
> [6] https://support.apple.com/en-us/HT208436
> [7] https://doc.rust-lang.org/reference/conditional-compilation.html
-- 
https://mail.python.org/mailman/listinfo/python-list


(yet another) PEP idea to tackle binary wheels problem efficiently

2019-02-18 Thread Alexander Revin
Hi all,

I've been thoroughly reading various discussions, such as [1], [2] and
related ones regarding PEP 425, PEP 491 and PEP 513. I also happen to
use musl sometimes, so as discussed here [3] I thought it would be a
good idea to submit a new PEP regarding musl compatibility.

It's not a secret that everyday wheel usage on Linux is far from
perfect. Some packages are trying to compile when there's no compiler
on the system available, some rely on 3rd-party deps and explode when
they cannot find required headers installed and so on. Add to this
cross-compilation support (quite complicated) and distros like Alpine
or just something not using x86 (how many piwheels.org-like should
emerge for each non-x86 platform?). For example, I would like to
seamlessly install numpy, pandas and scikit onto ppc machine running
Gentoo musl and not waste 1 hour for compilation, or "just" use them
in x86 standard alpine-based docker image (basically what [3] is all
about).

Long story short, current wheel filename format:

{distribution}-{version}(-{build tag})?-{python tag}-{abi
tag}-{platform tag}.whl.

Doesn't not properly express package expectation. Let's take a look at
pandas wheels ([4]):

pandas-0.24.1-cp36-cp36m-manylinux1_x86_64.whl
pandas-0.24.1-cp36-cp36m-win_amd64.whl
pandas-0.24.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl

I see issues with each of them:
1. First one won't work on Alpine or any musl-based distro;
2. Second – how amd64 is different from x86_64?
3. Third's platform tag is just a nightmare.

More of that, if you open the last one and inspect one of the
libraries, you'll find that:
$ file _libs/algos.cpython-36m-darwin.so
_libs/algos.cpython-36m-darwin.so: Mach-O universal binary with 2
architectures: [i386:Mach-O bundle i386] [x86_64]
_libs/algos.cpython-36m-darwin.so (for architecture i386): Mach-O bundle i386
_libs/algos.cpython-36m-darwin.so (for architecture x86_64): Mach-O
64-bit bundle x86_64

It's universal library! So not x86_64 only, as mentioned in the quite
long macosx_10_various platform tag.

TL;DR What my solution? To use something widely called "Target
Triplet" [5], omitting usual "vendor" field, so
{platform tag} from PEP 435 will have the format of _[_]:

pandas-0.24.1-cp36-cp36m-x86_64_linux_gnu.whl
pandas-0.24.1-cp36-cp36m-x86_64_linux_musl.whl
pandas-0.24.1-cp36-cp36m-x86_windows.whl
pandas-0.24.1-cp36-cp36m-x86_64_windows_msvs2010.whl
pandas-0.24.1-cp36-cp36m-x86_macosx_10_6.whl <-- won't be used for
anything Mojave+, see [6]
pandas-0.24.1-cp36-cp36m_aarch64_netbsd.whl

Primary concerns here:
- Arch and os are specified more consistently;
- Environment is specified only when needed;
- Lots of possible combinations of os and env are possible :)

Since most runtimes are hardcoded during build time anyway and changed
for each Python release, explicit versioning shouldn't be a problem.

JavaScript and Rustlang [7] use similar naming scheme. Though I don't
like both of them, at least portability of extensions looks less
broken than of Python (I've worked on native Nodejs extension in the
past).


What do you think?

Thanks,
Alex

[1] 
https://mail.python.org/archives/list/distutils-...@python.org/thread/KCLRIN4PTUGZLLL7GOUM23S46ZZ2D4FU/
[2] https://github.com/pypa/packaging-problems/issues/69
[3] https://github.com/pypa/manylinux/issues/37
[4] https://pypi.org/project/pandas/#files
[5] https://wiki.osdev.org/Target_Triplet
[6] https://support.apple.com/en-us/HT208436
[7] https://doc.rust-lang.org/reference/conditional-compilation.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Mouse control

2019-10-06 Thread Alexander Vergun

Hello all,

I am coding a voice assistant under Python 3.7, Windows 7. I am using 
PYcharm and libraries such as PYSimpleGUI, mouse, keyboard etc.  
Everything works except for the mouse control and probably keyboard, the 
problem is following, when I run the script under PYcharm, the script 
can control mouse only within PYcharm editor, but when the other window 
is on the top, the script cannot control the mouse, and the same happens 
when the script runs under the Python console. There is only one way to 
make script control the mouse - I have to manually click on the window 
which was opened by the script only then the script controls the mouse. 
I tried to call win32gui.SetCapture without any effect.


Does anyone have an idea how to handle this? Thank you very much,

Alexander

--
Ing. Alexander Vergun
tel.: +421905167381

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


Re: Pycharm Won't Do Long Underscore

2020-06-23 Thread Alexander Neilson
Hi Tony

The “long underscore” (often called Dunder as “double underscore”) is actually 
two underscores as you are seeing shown in PyCharm. 

However the display of it as one long underscore is a ligature (special font 
display to communicate clearer) and to enable these in PyCharm go to the 
settings dialog (depending on windows or Mac this could be in different 
locations) and select Editor > Font

In that screen select “enable font ligatures” and if your font supports it 
(like the default JetBrains Mono does) that will start to display the double 
underscores as a single long underscore. 

Regards
Alexander

Alexander Neilson
Neilson Productions Limited
021 329 681
alexan...@neilson.net.nz

> On 24/06/2020, at 07:57, Tony Kaloki  wrote:
> 
> 
> 
> Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
> 
> From: Tony Kaloki<mailto:tkal...@live.co.uk>
> Sent: 23 June 2020 19:45
> To: python-list@python.org<mailto:python-list@python.org>
> Subject: Pycharm Won't Do Long Underscore
> 
> 
> Hi Guys,
>   I’ve just begun to learn basic computer programming by 
> downloading Python and Pycharm and following Youtube tutorials. But I’ve come 
> across a problem that’s stopped me in my tracks.
> When I try to do a long underscore __  for classes in Pycharm, it only 
> gives me two separate single underscores _ _. This is only in Pycharm, no 
> problems anywhere else. Could you tell me how to fix this, because I can’t 
> find any answers on the web and I’m not sure if I can go any further in my 
> learning without being able to get long underscores.
>Sorry if I’m just being really dense, but like I said I’m an absolute 
> beginner. Thanks for your time,
> Tony
> Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pycharm Won't Do Long Underscore

2020-06-23 Thread Alexander Neilson
Hi Tony

Absolutely. The “long underscore” is just a display thing. Underneath it is 
always two underscores. The only difference is what your editor is showing (or 
how the underscore is drawn in the display font - as some fonts make the 
underscore the full width or slightly over the edge of where the character will 
be displayed so that many underscores look all joined up like a line)

These links may provide you with some useful reading around this and what 
underscores signal in Python. 

https://dbader.org/blog/meaning-of-underscores-in-python

https://dbader.org/blog/python-dunder-methods

Happy to have helped and good luck with your Python journey. 

Regards
Alexander

Alexander Neilson
Neilson Productions Limited
021 329 681
alexan...@neilson.net.nz

> On 24/06/2020, at 08:49, Tony Kaloki  wrote:
> 
> 
> Alexander,
>Thank you so much! It worked! Thank you. One question: in 
> your reply, are you saying that Python would have treated the two separate 
> underscores the same way as a long  underscore i.e. it's a stylistic choice 
> rather than a functional necessity?
>In any case, thanks again for your quick and easy to follow - even for me 
> - reply.
> Tony
> 
> Get Outlook for Android
> 
> From: Alexander Neilson 
> Sent: Tuesday, June 23, 2020 9:28:37 PM
> To: Tony Kaloki 
> Cc: python-list@python.org 
> Subject: Re: Pycharm Won't Do Long Underscore
>  
> Hi Tony
> 
> The “long underscore” (often called Dunder as “double underscore”) is 
> actually two underscores as you are seeing shown in PyCharm. 
> 
> However the display of it as one long underscore is a ligature (special font 
> display to communicate clearer) and to enable these in PyCharm go to the 
> settings dialog (depending on windows or Mac this could be in different 
> locations) and select Editor > Font
> 
> In that screen select “enable font ligatures” and if your font supports it 
> (like the default JetBrains Mono does) that will start to display the double 
> underscores as a single long underscore. 
> 
> Regards
> Alexander
> 
> Alexander Neilson
> Neilson Productions Limited
> 021 329 681
> alexan...@neilson.net.nz
> 
> > On 24/06/2020, at 07:57, Tony Kaloki  wrote:
> > 
> > 
> > 
> > Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 
> > 10
> > 
> > From: Tony Kaloki<mailto:tkal...@live.co.uk>
> > Sent: 23 June 2020 19:45
> > To: python-list@python.org<mailto:python-list@python.org>
> > Subject: Pycharm Won't Do Long Underscore
> > 
> > 
> > Hi Guys,
> >   I’ve just begun to learn basic computer programming by 
> > downloading Python and Pycharm and following Youtube tutorials. But I’ve 
> > come across a problem that’s stopped me in my tracks.
> > When I try to do a long underscore __  for classes in Pycharm, it only 
> > gives me two separate single underscores _ _. This is only in Pycharm, no 
> > problems anywhere else. Could you tell me how to fix this, because I can’t 
> > find any answers on the web and I’m not sure if I can go any further in my 
> > learning without being able to get long underscores.
> >Sorry if I’m just being really dense, but like I said I’m an absolute 
> > beginner. Thanks for your time,
> > Tony
> > Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 
> > 10
> > 
> > 
> > -- 
> > https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: strip() method makes me confused

2020-11-07 Thread Alexander Neilson
Because the strip methods argument is the set of characters to remove from 
either end. So it checks from the ends character by character until it finds a 
character that isn’t in the set. Then it removes everything prior to that (or 
after that at end of the string) and then returns the result. 

So your first example leaves “banana” as the string because all characters 
after and before that string in the original string were found in the set. 

However in your second example the set only contains the comma and the string 
neither begins nor ends with commas so if (first character) in (set containing 
comma) returns false so it stops searching from that end and does the same at 
the other end. 

https://www.programiz.com/python-programming/methods/string/strip

Hope that helps. 

Regards
Alexander

Alexander Neilson
Neilson Productions Limited
021 329 681
alexan...@neilson.net.nz

> On 8/11/2020, at 00:06, Bischoop  wrote:
> 
> 
> According to documentation strip method removes heading and trailing
> characters.
> 
> Why then:
> 
> txt = ",rrttggs...,..s,bananas...s.rrr"
> 
> x = txt.strip(",s.grt")
> 
> print(x)
> 
> output: banana
> 
> another example:
> 
> text = "this is text, there should be not commas, but as you see there
> are still"
> y = txt.strip(",")
> print(text)
> 
> output: 
> this is text, there should be not commas, but as you see there are still
> 
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


ANN: Dao Language v.0.9.6-beta is release!

2005-12-02 Thread Alexander Zatvornitskiy
Hello, [EMAIL PROTECTED]

28 nov 2005 at 02:48, [EMAIL PROTECTED] wrote:

 p> This is just to let you know that the lastest version Dao language is
 p> released.

Please wrote "hello world" example for us, and some more simple examples which
highlight major features of this language. I think, it is the best way to
evaluate new language.


Alexander, [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT: MoinMoin and Mediawiki?

2005-01-11 Thread Alexander Schremmer
On 10 Jan 2005 18:45:16 -0800, Paul Rubin wrote:

> I need to set up a wiki for a small group.  I've played with MoinMoin
> a little bit and it's reasonably straightforward to set up, but
> limited in capabilities and uses BogusMarkupConventions.

At which point do you see limitations?
And what of the markup don't you like?

> In the larger world, though, there's currently One True wiki package,
> namely Mediawiki (used by Wikipedia).

It is just very famous because of Wikipedia IMHO.

>  Mediawiki is written in PHP and
> is far more complex than MoinMoin, plus it's database backed, meaning
> you have to run an SQL server as well as the wiki software itself
> (MoinMoin just uses the file system).

Having a DBMS backend is good in your opinion? It has some severe
disadvantages like not easy to scale (you would need to setup DBMS
replication), two potential points of failure, more complex setup, bigger
memory requirements, etc.

>  Plus, I'll guess that it really
> needs mod_php, while MoinMoin runs tolerably as a set of cgi's, at
> least when traffic is low.

Both should run fine in CGI mode I guess.

> What I'm getting at is I might like to install MoinMoin now and
> migrate to Mediawiki sometime later.  Anyone have any thoughts about
> whether that's a crazy plan?

If you really want to use the wiki for content, you have to agree on a
markup style. You could use an independent one (like RestructuredText) and
hope that MediaWiki supports it (MoinMoin does). Or you end up writing
complex migration scripts just for the markup.

Finding some script which just imports the data files into the DB should be
rather easy.

>  Should I just bite the bullet and run Mediawiki from the beginning?

IMHO you should not bite it but choose MoinMoin. :-)

> Is anyone here actually running Mediawiki who can say just how
> big a hassle it is?

A few months I tried to install it. I got it running. But I did not like
the necessary complex administration tasks.

> The other one will be public and is planned grow to medium size (a few
> thousand active users), but which I don't need immediately.  I
> definitely want the second one to eventually run Mediawiki.  I can
> probably keep the first one on MoinMoin indefinitely, but that would
> mean I'm eventually running two separate wiki packages, which gets
> confusing.

There are even MoinMoin sites that are as big as that. Maybe you should
rethink your kind of prejudice and re-evaluate MoinMoin.

Kind regards,
Alexander
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT: MoinMoin and Mediawiki?

2005-01-11 Thread Alexander Schremmer
On 11 Jan 2005 08:49:52 -0800, Paul Rubin wrote:

> Alexander Schremmer <[EMAIL PROTECTED]> writes:
>>> I need to set up a wiki for a small group.  I've played with MoinMoin
>>> a little bit and it's reasonably straightforward to set up, but
>>> limited in capabilities and uses BogusMarkupConventions.
>> 
>> At which point do you see limitations?
> 
> It doesn't have features that MW has, like user pages,

It does.
> lists of incoming links to wiki pages,

It does.

> automatic discussion links for every wiki page, 

Can be easily obtained manually. Just include another page where discussion
is going on. You can even protect it with different ACL config etc.

> automatic update notification for specific pages of your
> choice, 

It does.

> support for managing image uploads and embedding them
> into wiki pages, 

It does.

> 
>> And what of the markup don't you like?
> 
> The BogusMixedCaseLinkNames.  I'd rather have ordinary words with
> spaces between them, like we use in ordinary writing.

Of course it does. It does not even require [[ but [" at the beginning of
the link which might be more intuitive to the writer.

>>> In the larger world, though, there's currently One True wiki package,
>>> namely Mediawiki (used by Wikipedia).
>> 
>> It is just very famous because of Wikipedia IMHO.
> 
> Well, it's gotten a lot more development attention because of that
> same Wikipedia.

Yeah, but you know: Too many cooks spoil the broth.

>> Having a DBMS backend is good in your opinion?

> I didn't say that it was good, in fact I was listing it as a
> disadvantage there.  I think for a small wiki like I was discussing,
> it's just an extra administrative hassle.  For a large wiki though,
> MoinMoin's approach is completely unworkable and MoinMoin's
> documentation actually says so.  First of all MoinMoin uses a separate
> subdirectory for every page, and all those subdirs are in a flat top
> level directory, so if you have 100,000 wiki pages, the top level
> directory has that many subdirs.  Most file systems are not built to
> handle such large directories with any reasonable speed.

But many other are.

>  (Also, every revision has its own file in the subdir.  Lots of Wikipedia 
> pages have
> thousands of revisions).  

Yeah, same point. But if you have a working btree+ etc. implementation in
your FS, then it should not a problem. Personally, I just see another
problem at this point: paths might get very long if your page names are
long. This might result in problems on Windows. But you have to use very
long page names to hit the 256 char limit.

> The DBMS can also handle stuff like replication automatically.

But this is non-trivial.

>>> The other one will be public and is planned grow to medium size (a few
>>> thousand active users)
>>
>> There are even MoinMoin sites that are as big as that. Maybe you should
>> rethink your kind of prejudice and re-evaluate MoinMoin.
> 
> I don't doubt there are MoinMoin sites that size, but with that large
> a user base, I want something nicer looking than those
> StupidMixedCasePageNames.

See above. Just because most people still work with CamelCase, they are not
the only solution. ["Stupid mixed case page names"] is a completly valid
link in MoinMoin and has been one for years.

Kind regards,
Alexander
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT: MoinMoin and Mediawiki?

2005-01-12 Thread Alexander Schremmer
On 11 Jan 2005 21:24:51 -0800, Paul Rubin wrote:

[backlinks]
>> Searching instead of indexing makes it very resilient :-)
> 
> How does it do that?  It has to scan every page in the entire wiki?!
> That's totally impractical for a large wiki.

So you want to say that c2 is not a large wiki? :-)

Kind regards,
Alexander
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: limited python virtual machine (WAS: Another scripting language implemented into Python itself?)

2005-01-25 Thread Alexander Schremmer
On Tue, 25 Jan 2005 12:22:13 -0700, Steven Bethard wrote:

>  >>This is a serious issue.
>  >>
>  >>It's also one that brings Tcl, mentioned several
>  >>times in this thread, back into focus.  Tcl presents
>  >>the notion of "safe interpreter", that is, a sub-
>  >>ordinate virtual machine which can interpret only
>  >>specific commands.  It's a thrillingly powerful and
>  >>correct solution to the main problem Jeff and others
>  >>have described.
>  >
>  > A better (and of course *vastly* more powerful but unfortunately only
>  > a dream ;-) is a similarly limited python virutal machine.
> 
> Yeah, I think there are a lot of people out there who would like 
> something like this, but it's not quite clear how to go about it.  If 
> you search Google Groups, there are a lot of examples of how you can use 
> Python's object introspection to retrieve "unsafe" functions.

IMHO a safe Python would consist of a special mode that disallows all
systemcalls that could spy/harm data (IO etc.) and imports of
non-whitelisted modules. Additionally, a loop counter in the interpreter
loop would ensure that the code does not stall the process/machine.

>>> sys.safecall(func, maxcycles=1000)
could enter the safe mode and call the func.

I am not sure how big the patch would be, it is mainly a C macro at the
begginning of every relevant function that checks the current "mode" and
raises an exception if it is not correct. The import handler would need to
check if the module is whitelisted (based on the path etc.).

Python is too dynamic to get this working while just using tricks that
manipulate some builtins/globals etc.

Kind regards,
Alexander
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: limited python virtual machine (WAS: Another scripting language implemented into Python itself?)

2005-01-26 Thread Alexander Schremmer
On Tue, 25 Jan 2005 22:08:01 +0100, I wrote:

>>>> sys.safecall(func, maxcycles=1000)
> could enter the safe mode and call the func.

This might be even enhanced like this:

>>> import sys
>>> sys.safecall(func, maxcycles=1000,
 allowed_domains=['file-IO', 'net-IO', 'devices', 'gui'],
 allowed_modules=['_sre'])

Every access to objects that are not in the specified domains are
restricted by the interpreter. Additionally, external modules (which are
expected to be not "decorated" by those security checks) have to be in the
modules whitelist to work flawlessy (i.e. not generate exceptions).

Any comments about this from someone who already hacked CPython?

Kind regards,
Alexander
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   >