terminate a program gracefully from a thread

2014-03-22 Thread Jabba Laci
Hi,

I have a script (see below) that I want to terminate after X seconds.
The main loop of the program is waiting for user input.
The program enters the main loop and I try to shut down the program
after X seconds from a thread but I can't figure out how to do it. The
program should also do some cleanup before termination, so the shut
down must be graceful.

The code below is a simplified version. The whole idea is the
following: I have a script that has grown quite big over time. It
needs to read several data files, so when I start it for the first
time, it takes about 3-4 seconds to launch. The next start is much
faster since, I guess, the OS has done some caching. I use this script
a lot and the first slow launch bothers me. My idea: after booting, I
want to start the script in the background in suicide mode. OS does
the caching, so when I need it, it starts quickly.

See the code below with comments.

Thanks,

Laszlo

===

import atexit
import sys
import time
from threading import Thread
import os

def suicide(wait):
time.sleep(wait)
print(should exit now...)
sys.exit()  # exits this thread but not the main thread
#os._exit(0)# no cleanup with this :(

def cleanup():
# I want it to run before termination.
print(cleaning up...)

def main():
Thread(target=suicide, kwargs={'wait': 3}).start()
#
while True:
try:
inp = raw_input(in )
print(inp)
except (KeyboardInterrupt, EOFError):
print()
sys.exit()

#

if __name__ == __main__:
atexit.register(cleanup)
main()
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: terminate a program gracefully from a thread

2014-03-22 Thread Jabba Laci
 You need a flag to indicate that a particular invocation is the
  dummy one (background). So use that same flag either to suppress
  starting the thread,  or to avoid the unwanted raw_input.

 Alternatively,  rethink the need to preload at boot time.  Any
  caching the OS does is likely to only last a few minutes,
  depending on load. So maybe you can make the real load seem to be
  quicker by displaying the gui right away,  but doing the
  time-consuming part in a thread.

Hi,

Thanks for the idea. Right, if it's started in suicide mode, then
there is no need to enter the raw_input.

Thanks,

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


extract stream title from the output of mplayer

2014-03-18 Thread Jabba Laci
Hi,

I have a simple command-line radio player and I want to extract song
titles from the output of mplayer.

Example:

$ mplayer http://relay2.slayradio.org:8000/

It produces a streamed output of this form:

MPlayer2 UNKNOWN (C) 2000-2012 MPlayer Team
mplayer: could not connect to socket
mplayer: No such file or directory
...
ICY Info: StreamTitle='Alexander 'Taxim' Nev - Unsound minds feat.
SAM';StreamUrl='http://www.SLAYRadio.org/';
...

At the end it shows a progress indicator, thus the output is streamed.
The problem is I can't get this output in a string. My idea is to
launch mplayer with a timeout of 2 seconds for instance, get the
produced output and find the line that starts with ICY Info. But
when I kill the process after the timeout, I don't know how to fetch
the output produced so far.

Thanks,

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


Re: extract stream title from the output of mplayer

2014-03-18 Thread Jabba Laci
 Python. (Or s/guess/hop/ if you prefer!) There are many ways this
 could be done; what have you tried, what partly worked, what did
 something unexpected?

Hi,

I managed to solve the problem. In the man of mplayer I found how to
quit after X seconds: -endpos X. See my solution below.

Best,

Laszlo



import re
import shlex
from subprocess import PIPE, Popen

URL = 'http://relay2.slayradio.org:8000/'


def get_exitcode_stdout_stderr(cmd):

Execute the external command and get its exitcode, stdout and stderr.

args = shlex.split(cmd)

proc = Popen(args, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
exitcode = proc.returncode
#
return exitcode, out, err


def get_title():
cmd = mplayer -endpos 1 -ao null {url}.format(url=URL)
out = get_exitcode_stdout_stderr(cmd)[1]

for line in out.split(\n):
#print(line)
if line.startswith('ICY Info:'):
match = re.search(rStreamTitle='(.*)';StreamUrl=, line)
title = match.group(1)
return title

def main():
print(get_title())
-- 
https://mail.python.org/mailman/listinfo/python-list


passing an option to the python interpreter

2014-02-15 Thread Jabba Laci
Hi,

The first line in my scripts looks like this:

#!/usr/bin/env python

I would like to use unbuffered output in a script, which can be done
with the -u option, thus I tried this:

#!/usr/bin/env python -u

But if I want to run it from the command line ($ ./unbuffered.py), I
get this error:

/usr/bin/env: python -u: No such file or directory

env is looking for python -u but such a command doesn't exist.

How to overcome this problem? I could write

#!/usr/bin/python -u

but I prefer to call python through env. For instance if I put this
last one in a virtualenv, that would call the system-wide interpreter
not the one in the virtualenv.

Thanks,

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


nginx config: different projects in different directories

2013-10-06 Thread Jabba Laci
Hi,

I'm playing with Flask and I would like to try it in production
environment too. I managed to bring Flask together with uwsgi and
nginx. My Flask application is available at the address localhost:81 .

I would like to add several applications and I want them to be
available under different URLs. For instance, if I have two projects
called hello and world, I want to access them as
localhost:81/hello/ and localhost:81/world/ . The problem is I can't
figure out how to configure nginx for this.

Here is my current setup:

* The project hello is in this directory: /home/jabba/public_pyapps/hello/
* Its nginx entry:

server {
listen  81;
server_name localhost;
charset utf-8;
client_max_body_size 75M;

location / { try_files $uri @yourapplication; }
location @yourapplication {
include uwsgi_params;
uwsgi_pass unix:/home/jabba/public_pyapps/hello/hello_uwsgi.sock;
}
}

It's available at localhost:81 .

Questions:

(1) How to make it available under localhost:81/hello/ instead?

(2) If I add a new application (e.g. world), how to add it to nginx?

Thanks,

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


Re: building an online judge to evaluate Python programs

2013-09-21 Thread Jabba Laci
Hi Ned,

Could you please post here your AppArmor profile for restricted Python scripts?

Thanks,

Laszlo

On Sat, Sep 21, 2013 at 12:46 AM, Ned Batchelder n...@nedbatchelder.com wrote:
 On 9/20/13 6:26 PM, Jabba Laci wrote:

 I just found Docker ( http://docs.docker.io/en/latest/faq/ ). It seems
 sandboxing could be done with this easily.


 At edX, I wrote CodeJail (https://github.com/edx/codejail) to use AppArmor
 to run Python securely.

 For grading Python programs, we use a unit-test like series of challenges.
 The student writes problems as functions (or classes), and we execute them
 with unit tests (not literally unittest, but a similar idea).  We also
 tokenize the code to check for simple things like, did you use a while loop
 when the requirement was to write a recursive function.  The grading code is
 not open-source, unfortunately, because it is part of the MIT courseware.

 --Ned.

 Laszlo

 On Fri, Sep 20, 2013 at 10:08 PM, John Gordon gor...@panix.com wrote:

 In mailman.195.1379698177.18130.python-l...@python.org Jabba Laci
 jabba.l...@gmail.com writes:

 There are several questions:
 * What is someone sends an infinite loop? There should be a time limit.

 You could run the judge as a background process, and kill it after ten
 seconds if it hasn't finished.

 * What is someone sends a malicious code? The script should be run in a
 sandbox.

 You could run the judge from its own account that doesn't have access to
 anything else.  For extra security, make the judge program itself owned
 by
 a separate account (but readable/executable by the judge account.)

 I suppose you'd have to disable mail access from the judge account too.
 Not sure how to easily do that.

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

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


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


building an online judge to evaluate Python programs

2013-09-20 Thread Jabba Laci
Hi,

In our school I have an introductory Python course. I have collected a
large list of exercises for the students and I would like them to be
able to test their solutions with an online judge (
http://en.wikipedia.org/wiki/Online_judge ). At the moment I have a
very simple web application that is similar to Project Euler: you
provide the ID of the exercise and the output of the program, and it
tells you if it's correct or not. However, it can only be used with
programs that produce an output (usually a short string or a number).

In the next step I would like to do the following. The user can upload
his/her script, and the system tests it with various inputs and tells
you if it's OK or not (like checkio.org for instance). How to get
started with this?

There are several questions:
* What is someone sends an infinite loop? There should be a time limit.
* What is someone sends a malicious code? The script should be run in a sandbox.

All tips are appreciated.

Thanks,

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


Re: building an online judge to evaluate Python programs

2013-09-20 Thread Jabba Laci
Let's take this simple exercise:

Write a function that receives a list and decides whether the list is
sorted or not.

Here the output of the function is either True or False, so I cannot
test it with my current method.

Laszlo

On Fri, Sep 20, 2013 at 7:57 PM, Aseem Bansal asmbans...@gmail.com wrote:
However, it can only be used with programs that produce an output

 Just interested, what else are you thinking of checking?
 --
 https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: building an online judge to evaluate Python programs

2013-09-20 Thread Jabba Laci
 That last seems to me to be the biggie.  Several times in the past few
 years, people in this mailing list have tried to build a safe sandbox.
 And each one was a big failure, for a hacker of sufficient interest.
 Some of them were spectacular failures.

 If you have to be safe from your user, Python may be the wrong language
 to give them.

Well, the course is about Python and I want to test Python scripts...

I've heard about chroot jail but I never used it. Wikipedia says:

A chroot on Unix operating systems is an operation that changes the
apparent root directory for the current running process and its
children. A program that is run in such a modified environment cannot
name (and therefore normally not access) files outside the designated
directory tree. The term chroot may refer to the chroot(2) system
call or the chroot(8) wrapper program. The modified environment is
called a chroot jail.

I guess it could be used for sandboxing.

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


Re: building an online judge to evaluate Python programs

2013-09-20 Thread Jabba Laci
I just found Docker ( http://docs.docker.io/en/latest/faq/ ). It seems
sandboxing could be done with this easily.

Laszlo

On Fri, Sep 20, 2013 at 10:08 PM, John Gordon gor...@panix.com wrote:
 In mailman.195.1379698177.18130.python-l...@python.org Jabba Laci 
 jabba.l...@gmail.com writes:

 There are several questions:
 * What is someone sends an infinite loop? There should be a time limit.

 You could run the judge as a background process, and kill it after ten
 seconds if it hasn't finished.

 * What is someone sends a malicious code? The script should be run in a
 sandbox.

 You could run the judge from its own account that doesn't have access to
 anything else.  For extra security, make the judge program itself owned by
 a separate account (but readable/executable by the judge account.)

 I suppose you'd have to disable mail access from the judge account too.
 Not sure how to easily do that.

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

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


detect key conflict in a JSON file

2013-05-29 Thread Jabba Laci
Hi,

How can you detect if a key is duplicated in a JSON file? Example:

{
something: [...],
...
something: [...]
}

I have a growing JSON file that I edit manually and it might happen
that I repeat a key. If this happens, I would like to get notified.
Currently the value of the second key silently overwrites the value of
the first.

Do you know about a command line JSON validator?

Thanks,

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


Re: detect key conflict in a JSON file

2013-05-29 Thread Jabba Laci
 The real answer here is that JSON is probably not the best choice for
 large files that get hand-edited.  For data that you intend to hand-edit
 a lot, YAML might be a better choice.

 Currently the value of the second key silently overwrites the value of
 the first.

Thanks but how would it be different with YAML? Can YAML check duplicate keys?

How to process (read) YAML files in Python? Can you give me some hints
how to get started? All I need is read it in and create a dictionary
of it.

Thanks,

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


extract HTML table in a structured format

2013-04-10 Thread Jabba Laci
Hi,

I wonder if there is a nice way to extract a whole HTML table and have the
result in a nice structured format. What I want is to have the lifetime
table at the bottom of this page:
http://en.wikipedia.org/wiki/List_of_Ubuntu_releases (then figure out with
a script until when my Ubuntu release is supported).

I could do it with BeautifulSoup or lxml but is there a better way? There
should be :)

Thanks,

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


Re: extract HTML table in a structured format

2013-04-10 Thread Jabba Laci
Hi,

Thanks, it's a great idea to parse the wiki source. Since then I have found
a command for this particular task called ubuntu-support-status that
shows what I need.

But the idea to extract an HTML table from a webpage in a structured format
could be an interesting (and useful) project idea.

Best,

Laszlo


On Wed, Apr 10, 2013 at 8:11 PM, Arnaud Delobelle arno...@gmail.com wrote:

 On 10 April 2013 09:44, Jabba Laci jabba.l...@gmail.com wrote:
  Hi,
 
  I wonder if there is a nice way to extract a whole HTML table and have
 the
  result in a nice structured format. What I want is to have the lifetime
  table at the bottom of this page:
  http://en.wikipedia.org/wiki/List_of_Ubuntu_releases (then figure out
 with a
  script until when my Ubuntu release is supported).
 
  I could do it with BeautifulSoup or lxml but is there a better way? There
  should be :)

 Instead of parsing HTML, you could just parse the source of the page
 (available via action=raw):

 --
 import urllib2

 url = (
 'http://en.wikipedia.org/w/index.php'
 '?title=List_of_Ubuntu_releasesaction=raw'
 )

 source = urllib2.urlopen(url).read()

 # Table rows are separated with the line |-
 # Then there is a line starting with |
 potential_rows = source.split(\n|-\n|)

 rows = []

 for row in potential_rows:
 # Rows in the table start with a link (' [[ ... ]]')
 if row.startswith( [[):
 row = [item.strip() for item in row.split(\n|)]
 rows.append(row)
 --

  import pprint
  pprint.pprint(rows)
 [['[[Warty Warthog|4.10]]',
   'Warty Warthog',
   '20 October 2004',
   'colspan=2 {{Version |o |30 April 2006}}',
   '2.6.8'],
  ['[[Hoary Hedgehog|5.04]]',
   'Hoary Hedgehog',
   '8 April 2005',
   'colspan=2 {{Version |o |31 October 2006}}',
   '2.6.10'],
  ['[[Breezy Badger|5.10]]',
   'Breezy Badger',
   '13 October 2005',
   'colspan=2 {{Version |o |13 April 2007}}',
   '2.6.12'],
  ['[[Ubuntu 6.06|6.06 LTS]]',
   'Dapper Drake',
   '1 June 2006',
   '{{Version |o | 14 July 2009}}',
   '{{Version |o | 1 June 2011}}',
   '2.6.15'],
  ['[[Ubuntu 6.10|6.10]]',
   'Edgy Eft',
   '26 October 2006',
   'colspan=2 {{Version |o | 25 April 2008}}',
   '2.6.17'],
   [...]
 ]
 

 That should give you the info you need (until the wiki page changes too
 much!)

 --
 Arnaud

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


Python source to C++ and/or Java

2013-03-07 Thread Jabba Laci
Hi,

As a university project, I would like to work on an automated
converter that transforms a Python source code to C++ or Java (not yet
decided but I would vote on Java since it seems less complicated).
Do you know if it's already done? What other similar projects are you aware of?

And most importantly: where could I start this project? Could you give
some ideas/tips?

The converter doesn't have to recognize everything. First we want to
build a simple prototype.

Thanks,

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


Re: autoflush on/off

2013-02-04 Thread Jabba Laci
Hi,

Thanks for the answers. I like the context manager idea but setting
the sys.stdout back to the original value doesn't work.

Example:

class Unbuff(object):
def __init__(self):
self.stdout_bak = sys.stdout

def __enter__(self):
sys.stdout.flush()
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout = self.stdout_bak



with Unbuff():
for i in range(5):
sys.stdout.write('.')
sleep(.5)
#
sys.stdout.write('EXIT')# provokes an error


The problem is in __exit__ when sys.stdout is pointed to the old
value. sys.stdout.write doesn't work from then on. Output:

.close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

Laszlo


 Write a context manager class. See Library manual, 4.11. Context Manager
 Types. The __enter__ method would be much like the above except that is
 should save the old stdout object 'oldstdout = sys.stdout' instead of
 fiddling with 'autoflush_on'. Then __exit__ would simply be 'sys.stdout =
 oldstdout'. Drop autoflush_on.  Your context manager should not care about
 the existing buffering other than to restore it on exit. Saving and
 restoring the existing stdout object does that.

 --
 Terry Jan Reedy

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


Re: environment fingerprint

2013-01-29 Thread Jabba Laci
Hi,

Thanks for the tip. I came up with the solution below. For my purposes
the short fingerprint is enough.

Laszlo

==

import platform as p
import uuid
import hashlib

def get_fingerprint(md5=False):

Fingerprint of the current operating system/platform.

If md5 is True, a digital fingerprint is returned.

sb = []
sb.append(p.node())
sb.append(p.architecture()[0])
sb.append(p.architecture()[1])
sb.append(p.machine())
sb.append(p.processor())
sb.append(p.system())
sb.append(str(uuid.getnode())) # MAC address
text = '#'.join(sb)
if md5:
md5 = hashlib.md5()
md5.update(text)
return md5.hexdigest()
else:
return text

def get_short_fingerprint(length=6):

A short digital fingerprint of the current operating system/platform.

Length should be at least 6 characters.

assert 6 = length = 32
#
return get_fingerprint(md5=True)[-length:]

On Tue, Jan 29, 2013 at 2:43 PM, Andrew Berg bahamutzero8...@gmail.com wrote:
 On 2013.01.29 07:18, Jabba Laci wrote:
 Hi,

 I have a script that I want to run in different environments: on
 Linux, on Windows, on my home machine, at my workplace, in virtualbox,
 etc. In each environment I want to use different configurations. For
 instance the temp. directory on Linux would be /tmp, on Windows
 c:\temp, etc. When the script starts, I want to test the environment
 and load the corresponding config. settings. How to get an
 OS-independent fingerprint of the environment?
 http://docs.python.org/3.3/library/platform.html
 http://docs.python.org/3.3/library/os.html#os.environ

 --
 CPython 3.3.0 | Windows NT 6.2.9200.16461 / FreeBSD 9.1-RELEASE
 --
 http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: open URL in the current tab

2012-12-11 Thread Jabba Laci
Dear All,

If someone is interested, I've made a module out of it. Available here:

https://github.com/jabbalaci/jabbapylib/blob/master/jabbapylib/browser/firefox.py

It seems you can program your Firefox instance from your script as you
want through this add-on. I will explore the possibilities in the
future.

Best,

Laszlo

On Tue, Dec 11, 2012 at 1:16 AM, Chris Angelico ros...@gmail.com wrote:
 On Tue, Dec 11, 2012 at 11:05 AM, Jabba Laci jabba.l...@gmail.com wrote:
 Hi,

 If this is for use on somebody else's system, *please don't*. My

 This is for me. I have a simple GUI that produces some URL that I want
 to open in the current tab. Since I want to verify several URLs, I
 don't want to open dozens of new tabs.

 Here is my working solution. It requires the MozRepl Firefox add-on
 that I mentioned in the previous message.

 Looks good! Since it's your own single system, the add-on requirement
 isn't too onerous (but even if it's an all-mine system, I'd hesitate
 to deploy an add-on to more than a handful of computers).

 Specific problem, specific solution. I like it.

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


open URL in the current tab

2012-12-10 Thread Jabba Laci
Hi,

With the webbrowser module you can open a URL in a new tab. But how
could I tell Firefox from Python to open a URL in the _current_ tab?

Thanks,

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


Re: open URL in the current tab

2012-12-10 Thread Jabba Laci
Thanks. I've found something interesting since then:

https://addons.mozilla.org/en-US/firefox/addon/mozrepl/
https://github.com/bard/mozrepl/wiki

It allows you to connect to your Firefox via telnet. Then changing the URL:

content.location.href = new_url

However, for this you need to install this add-on.

Laszlo

On Mon, Dec 10, 2012 at 11:43 PM, Boris FELD lothiral...@gmail.com wrote:
 Don't think that it's possible with webbrowser, you should try with Selenium.

 For example with sst (Simple Selenium Test), it open url in current
 tab or create a new one if no one exists:

 from sst.actions import *
 go_to('http://www.ubuntu.com/')

 2012/12/10 Jabba Laci jabba.l...@gmail.com:
 Hi,

 With the webbrowser module you can open a URL in a new tab. But how
 could I tell Firefox from Python to open a URL in the _current_ tab?

 Thanks,

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


Re: open URL in the current tab

2012-12-10 Thread Jabba Laci
Hi,

 If this is for use on somebody else's system, *please don't*. My

This is for me. I have a simple GUI that produces some URL that I want
to open in the current tab. Since I want to verify several URLs, I
don't want to open dozens of new tabs.

Here is my working solution. It requires the MozRepl Firefox add-on
that I mentioned in the previous message.

Laszlo

===

import telnetlib

HOST = 'localhost'
PORT = 4242# MozRepl default

def open_curr_tab(url):
tn = telnetlib.Telnet(HOST, PORT)
cmd = content.location.href = '{url}'.format(url=url)
tn.read_until(repl )
tn.write(cmd + \n)
tn.write(repl.quit()\n)

#

if __name__ == __main__:
open_curr_tab('http://www.python.org')
-- 
http://mail.python.org/mailman/listinfo/python-list


PyCharm messed up my PyDev

2012-12-06 Thread Jabba Laci
Hi,

I have a strange problem. I've used PyDev for a long time and a few
days ago I wanted to try PyCharm. When I wanted to create a new
project in PyCharm, it asked me to select the interpreter. There was a
possibility to upgrade packages so I selected all and pressed the
upgrade button. For the upgrade, it asked my admin password (I'm under
Linux).
Then today I wanted to go back to PyDev but it says Python not
configured for _all_ my projects :( Under Preferences I can select
the interpreter (Auto Config) but when I click on OK, the selection is
not held. When I go back to the interpreter selection window, it's
empty again. Clicking on OK it says Restoring PYTHONPATH.

I think it's because of PyCharm. I did the same thing on my laptop and
I have the same problem. Has anyone met this issue? How to get back a
working PyDev?

I also tried Aptana Studio 3 after Eclipse, but the same thing
happens. Did PyCharm change some permissions that PyDev doesn't like?

Thanks,

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


Re: PyCharm messed up my PyDev

2012-12-06 Thread Jabba Laci
Dear All,

I managed to solve the problem. Something got messed up in the
.metadata directory. After renaming it, Aptana Studio re-created it
and I could successfully specify the Python interpreter.

In PyCharm I specified the same workspace to use, maybe that was the problem.

Laszlo

On Thu, Dec 6, 2012 at 7:30 PM, Jabba Laci jabba.l...@gmail.com wrote:
 Hi,

 I have a strange problem. I've used PyDev for a long time and a few
 days ago I wanted to try PyCharm. When I wanted to create a new
 project in PyCharm, it asked me to select the interpreter. There was a
 possibility to upgrade packages so I selected all and pressed the
 upgrade button. For the upgrade, it asked my admin password (I'm under
 Linux).
 Then today I wanted to go back to PyDev but it says Python not
 configured for _all_ my projects :( Under Preferences I can select
 the interpreter (Auto Config) but when I click on OK, the selection is
 not held. When I go back to the interpreter selection window, it's
 empty again. Clicking on OK it says Restoring PYTHONPATH.

 I think it's because of PyCharm. I did the same thing on my laptop and
 I have the same problem. Has anyone met this issue? How to get back a
 working PyDev?

 I also tried Aptana Studio 3 after Eclipse, but the same thing
 happens. Did PyCharm change some permissions that PyDev doesn't like?

 Thanks,

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


fabric question

2012-11-09 Thread Jabba Laci
Hi,

I'm trying to use fabric to run a command on another Linux machine.
When I call fab remote_info (using the example from its
documentation), this is what I get:

local$ fab remote_info
[remote] Executing task 'remote_info'
[remote] run: uname -a
[remote] out: remote@path$

That is, it logs in successfully, but I get no output and I get a
prompt on the remote machine, i.e. it doesn't log out automatically
after calling the remote command. I also tried to specify the absolute
path of uname, same result. Any idea?

Thanks,

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


avoid the redefinition of a function

2012-09-12 Thread Jabba Laci
Hi,

I have an installer script that contains lots of little functions. It
has an interactive menu and the corresponding function is called. Over
time it grew long and when I want to add a new function, I should give
a unique name to that function. However, Python allows the
redefinition of functions:

#!/usr/bin/env python

def step_1():
print 1

def step_1():
print 2

step_1()

This will call the 2nd function. Now my functions are called step_ID
(like step_27(), step_28(), etc.). How to avoid the danger of
redefinition? Now, when I write a new function, I search for its name
to see if it's unique but there must be a better way.

Thanks,

Laszlo
P.S.: the script is here ( https://github.com/jabbalaci/jabbatron ) if
you are interested. It's made for Ubuntu.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread Jabba Laci
 For example:

 def install_java():
pass

 def install_tomcat():
pass

Thanks for the answers. I decided to use numbers in the name of the
functions to facilitate function calls. Now if you have this menu
option for instance:

(5) install mc

You can type just 5 as user input and step_5() is called
automatically. If I use descriptive names like install_java() then
selecting a menu point would be more difficult. And I don't want users
to type java, I want to stick to simple numbers.

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


Re: parallel programming in Python

2012-05-29 Thread Jabba Laci
Hehe, I just asked this question a few days ago but I didn't become
much cleverer:

http://www.gossamer-threads.com/lists/python/python/985701

Best,

Laszlo

On Thu, May 10, 2012 at 2:14 PM, Jabba Laci jabba.l...@gmail.com wrote:
 Hi,

 I would like to do some parallel programming with Python but I don't
 know how to start. There are several ways to go but I don't know what
 the differences are between them: threads, multiprocessing, gevent,
 etc.

 I want to use a single machine with several cores. I want to solve
 problems like this: iterate over a loop (with millions of steps) and
 do some work at each step. The steps are independent, so here I would
 like to process several steps in parallel. I want to store the results
 in a global list (which should be synchronised). Typical use case:
 crawl webpages, extract images and collect the images in a list.

 What's the best way?

 Thanks,

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


parallel programming in Python

2012-05-10 Thread Jabba Laci
Hi,

I would like to do some parallel programming with Python but I don't
know how to start. There are several ways to go but I don't know what
the differences are between them: threads, multiprocessing, gevent,
etc.

I want to use a single machine with several cores. I want to solve
problems like this: iterate over a loop (with millions of steps) and
do some work at each step. The steps are independent, so here I would
like to process several steps in parallel. I want to store the results
in a global list (which should be synchronised). Typical use case:
crawl webpages, extract images and collect the images in a list.

What's the best way?

Thanks,

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


Re: parallel programming in Python

2012-05-10 Thread Jabba Laci
Hi,

Thanks for the answer. I use Linux with CPython 2.7. I plan to work
with CPU bound and I/O bound problems too. Which packages to use in
these cases? Could you redirect me to some guides? When to use
multiprocessing / gevent?

Thanks,

Laszlo


On Thu, May 10, 2012 at 2:34 PM, Dave Angel d...@davea.name wrote:
 On 05/10/2012 08:14 AM, Jabba Laci wrote:
 Hi,

 I would like to do some parallel programming with Python but I don't
 know how to start. There are several ways to go but I don't know what
 the differences are between them: threads, multiprocessing, gevent,
 etc.

 I want to use a single machine with several cores. I want to solve
 problems like this: iterate over a loop (with millions of steps) and
 do some work at each step. The steps are independent, so here I would
 like to process several steps in parallel. I want to store the results
 in a global list (which should be synchronised). Typical use case:
 crawl webpages, extract images and collect the images in a list.

 What's the best way?

 Thanks,

 Laszlo

 There's no single best-way.  First question is your programming
 environment.  That includes the OS you're running, and the version # and
 implementation of Python.

 I'll assume you're using CPython 2.7 on Linux, which is what I have the
 most experience on.  But after you answer, others will probably make
 suggestions appropriate to whatever you're actually using

 Next question is whether the problem you're solving at any given moment
 is cpu-bound or i/o bound.  I'll try to answer for both cases, here.

 CPU-bound:
 In CPython 2.7, there's a GIL, which is a global lock preventing more
 than one CPU-bound thread from running at the same time.  it's more
 complex than that, but bottom line is that multiple threads won't help
 (and might hurt) a CPU-bound program, even in a multi-core situation.
 So use multiple processes, and cooperate between them with queues or
 shared memory, or even files. In fact, you can use multiple computers,
 and communicate using sockets, in many cases.

 IO-bound:
 This is what CPython is good at solving with threads.  Once you make a
 blocking I/O call, usually the C code involves releases the GIL, and
 other threads can run.  For this situation, the fact that you can share
 data structures makes threads a performance win.

 Web crawling is likely to be IO-bound, but i wanted to be as complete as
 I could.

 --

 DaveA

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


geoinfo with python

2012-05-01 Thread Jabba Laci
Hi,

I want to figure out where a host is located, in which country. There
are sites that look up this information (e.g.
http://geoip.flagfox.net/). Before writing a scraper, I would like to
ask if you know a python API for this task.

Thanks,

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


Re: geoinfo with python

2012-05-01 Thread Jabba Laci
I've found a web service for the task:
http://www.geoplugin.com/webservices . It can produce JSON output too.

Laszlo

On Tue, May 1, 2012 at 09:35, Jabba Laci jabba.l...@gmail.com wrote:
 Hi,

 I want to figure out where a host is located, in which country. There
 are sites that look up this information (e.g.
 http://geoip.flagfox.net/). Before writing a scraper, I would like to
 ask if you know a python API for this task.

 Thanks,

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


Re: For loop

2012-05-01 Thread Jabba Laci
Hi,

Try this:

import sys

for i in range (1, 5+1):
  for j in range (i):
sys.stdout.write(str(i))
  print

print adds a newline character
print hi,   notice the comma, it won't add newline, however
it adds a space
With sys.stdout.write you can print the way you want, it won't add any
extra stuff (newline or space).

Best,

Laszlo


On Mon, Apr 30, 2012 at 13:52, Daniel dan...@kingdread.de wrote:
 You could also try http://docs.python.org/library/stdtypes.html#str.join
 like this:
 for i in range(5):
    print .join(str(i) for j in range(i))
 --
 http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


generate random numbers in a deterministic way

2012-04-24 Thread Jabba Laci
Hi,

I'm working with some sorting algorithms and I want to compare their
efficiency. One test fills a list with one million random integers,
which serves as input for the algorithms. However, if this list is
different each time I run the tests, the tests wouldn't be fair. At
the moment the selected sorting alg. can be specified with a switch
and only one alg. is tested each time.

So what I want is the following: fill the list with random numbers,
but when I re-execute the script, I want the same random numbers in
the same order. This way each sorting alg. would get the same input.

As a workaround, I made a generator that outputs a random list in a
file, and this list is read each time by the testing script. I just
wonder if there is a more elegant solution.

Thanks,

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


first X digits of pi

2012-04-13 Thread Jabba Laci
Hi,

I'd like to work with the digits of pi. I would need high precision,
like 100,000 digits or even more. At the moment I download the
necessary data from the web
(http://newton.ex.ac.uk/research/qsystems/collabs/pi/) and parse it.
I just wonder: is there a more elegant way? I found a Perl solution:
perl -Mbignum=bpi -wle 'print bpi(1000)' . Is there something similar
in Python?

Thanks,

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


Re: first X digits of pi

2012-04-13 Thread Jabba Laci
Hi,

Thanks for the answers. Gibbons' algorithm (from 2006) is a nice way
to generate the digits one after the other. However, it can get slow.
The mpmath approach is very fast, I think I will use that one. In a
script you can get the value of pi as a string with

str(mp.pi)

Best,

Laszlo

On Fri, Apr 13, 2012 at 15:56, Emanuel Woiski woi...@gmail.com wrote:
 Easy. Use mpmath (alone or under sympy):

 from sympy.mpmath import mp
 mp.dps = 20
 +mp.pi

 ... and there you are: 20 digits of pi :)

 regards
 woiski


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


red-black tree data structure

2012-04-11 Thread Jabba Laci
Hi,

It's not really a Python-related question, sorry for that. Does anyone
know why red-black trees got these colors in their names? Why not
blue-orange for instance? I'm just curious.

Thanks,

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


Re: red-black tree data structure

2012-04-11 Thread Jabba Laci
Hi,

Thanks for the answer. I copy the solution here:

According to wikipedia: The original structure was invented in 1972
by Rudolf Bayer and named symmetric binary B-tree, but acquired its
modern name in a paper in 1978 by Leonidas J. Guibas and Robert
Sedgewick.

Answer from Professor Guidas:

from Leonidas Guibas gui...@cs.stanford.edu to of the Red-Black
term mailed-by cs.stanford.edu hide details 16:16 (0 minutes ago)

we had red and black pens for drawing the trees.

Laszlo

On Wed, Apr 11, 2012 at 19:39, Ian Kelly ian.g.ke...@gmail.com wrote:
 On Wed, Apr 11, 2012 at 11:14 AM, Jabba Laci jabba.l...@gmail.com wrote:
 Hi,

 It's not really a Python-related question, sorry for that. Does anyone
 know why red-black trees got these colors in their names? Why not
 blue-orange for instance? I'm just curious.

 http://programmers.stackexchange.com/questions/116614/where-does-the-term-red-black-tree-come-from
-- 
http://mail.python.org/mailman/listinfo/python-list


produce the same output as Unix's date command

2012-04-05 Thread Jabba Laci
Hi,

Unix's date command produces this output (example):

Thu Apr  5 22:49:42 CEST 2012

I would like to produce the same output with Python, without calling
date externally. Before doing it I'd like to ask the list if anyone
has an existing solution for this.

Thanks,

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


question about file handling with with

2012-03-28 Thread Jabba Laci
Hi,

Is the following function correct? Is the input file closed in order?

def read_data_file(self):
with open(self.data_file) as f:
return json.loads(f.read())

Thanks,

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


use CTRL+L for clearing the screen

2012-02-29 Thread Jabba Laci
Hi,

I'm working on an interactive script. With raw_input user input is
read and the script produces some output and offers the prompt again.
I would like to add a clear screen feature, which would be activated
with CTRL+L. How to do that?
Another thing: raw_input waits until Enter but I'd like to clear the
screen at the moment when CTRL+L is pressed.

The script should be self-contained, thus I'd like to solve it by
using the standard library only.

Thanks,

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


Re: Python as a default shell, replacement of bash, sh, cmd ?

2012-02-18 Thread Jabba Laci
Have a look at IPython (http://ipython.org/). It can interact with the
normal shell very well.

Laszlo

On Sat, Feb 18, 2012 at 19:58, SherjilOzair sherjiloz...@gmail.com wrote:
 Has it been considered to add shell features to python, such that it can be 
 used as a default shell, as a replacement for bash, etc.

 I'm sure everyone would agree that doing this would make the terminal very 
 powerful.

 What are your views on this?
 --
 http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


name of a sorting algorithm

2012-02-14 Thread Jabba Laci
Hi,

Could someone please tell me what the following sorting algorithm is called?

Let an array contain the elements a_1, a_2, ..., a_N. Then:

for i = 1 to N-1:
for j = i+1 to N:
if a_j  a_i then swap(a_j, a_i)

It's so simple that it's not mentioned anywhere. I guess it's called
selection sort but I'm not sure. The minimum selection sort is an
improvement of this one.

Thanks,

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


Re: name of a sorting algorithm

2012-02-14 Thread Jabba Laci
Hi,

 Either you're misremembering, or the algorithm you programmed 43 years
 ago was not actually bubble sort.  Quoting from Wikipedia:

 
 Bubble sort, also known as sinking sort, is a simple sorting algorithm
 that works by repeatedly stepping through the list to be sorted,
 comparing each pair of adjacent items and swapping them if they are in
 the wrong order. The pass through the list is repeated until no swaps
 are needed, which indicates that the list is sorted. The algorithm
 gets its name from the way smaller elements bubble to the top of the
 list.
 

I don't agree with the last sentence. During bubble sort, in the 1st
pass the largest element is moved to the top (for me top means the
right side (end) of an array). Thus the end of the array is sorted. In
the 2nd pass, the largest element of the unsorted left part is moved
to the end, etc. That is, it's the _larger_ elements that bubble to
the top. At http://en.wikipedia.org/wiki/Bubble_sort you can find an
animated gif that shows how the algorithm works.

 In the present algorithm, you'll note that elements in the unsorted
 part of the list do not bubble up as they would in bubble sort.
 Rather, they jump around somewhat randomly until they are finally
 selected for the current sort index.  I agree with Arnaud -- this is a
 selection sort variant that saves a local variable (the index of the
 minimum element) by placing it at the current sort index instead -- at
 the cost of doing additional swaps.  Probably not a good trade-off in
 Python (but then again, no pure Python sort algorithm is likely to
 perform better than the built-in).

The minimum selection sort is an improvement of this noname
algorithm. I give it in pseudo-code. Let A be an array with N
elements. Indexing starts with 1.

for i := 1 to N-1:
minindex := i
for j := i+1 to N:
if A[j]  A[minindex] then minindex := j
end for
if i != minindex then swap(A[i], A[minindex])
end for

The two loops are the same as in the naive version. It will also sort
the array from the left side. It does much less swaps than the naive
version.

If the noname algorithm is called selection sort, then its name
can be misleading. One may ask OK, but which one? Minimum or maximum
selection sort?. Well, neither...

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


Re: calling a simple PyQt application more than once

2012-01-28 Thread Jabba Laci
 You can click the Press me button as many times as you wish; it
 retrieves and displays/prints the same HTML file on each click.

Hi,

Thanks for your reply. I forgot to mention that my first solution
created a headless browser, i.e. it didn't create any GUI. I would
like to keep it that way, thus I could scrape (AJAX-powered) webpages
in batch mode without any user interaction.

One workaround is to put the scraper in a separate file and call it as
an external command. But I'd like a more elegant solution that that.

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


calling a simple PyQt application more than once

2012-01-24 Thread Jabba Laci
Hi,

I have a simple PyQt application that creates a webkit instance to
scrape AJAX web pages. It works well but I can't call it twice. I
think the application is not closed correctly, that's why the 2nd call
fails. Here is the code below. I also put it on pastebin:
http://pastebin.com/gkgSSJHY .

The question is: how to call this code several times within a script.

Thanks,

Laszlo

=

import sys

from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebPage
from PyQt4.QtCore import QUrl

class SimpleWebkit(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self.save)
self.mainFrame().load(QUrl(url))
self.app.exec_()

def save(self):
self.html = self.mainFrame().toHtml()
self.app.quit()

def get_html(url):
s = SimpleWebkit(url)
return str(s.html)  # QString to string !

#

if __name__ == __main__:
url = 'http://simile.mit.edu/crowbar/test.html'
print get_html(url) # OK
print '=='
print get_html(url) # problem here, never called :(
-- 
http://mail.python.org/mailman/listinfo/python-list


verify the return value of a function

2012-01-19 Thread Jabba Laci
Hi,

In a unit test, I want to verify that a function returns a
cookielib.LWPCookieJar object. What is the correct way of doing that?

1) First I tried to figure out its type with type(return_value) but it
is type 'instance'

2) return_value.__class__ .__name__ gives 'LWPCookieJar', which is bettter

3) isinstance(return_value, cookielib.LWPCookieJar) seems to be the
best way, however somewhere I read that using isinstance is
discouraged

Thanks,

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


sqlalchemy beginner

2011-11-21 Thread Jabba Laci
Hi,

I'm reading the Essential SQLAlchemy book from O'Reilly. It explains
SqlAlch 0.4 but my current version is 0.7 and there are some
differences.

Here is an example from the book:

user_table = Table('tf_user', metadata,
Column('id', Integer, primary_key=True),
Column('user_name', Unicode(16), unique=True, nullable=False),
Column('password', Unicode(40), nullable=False),
Column('display_name', Unicode(255), default=''),
Column('created', DateTime, default=datetime.now)
)

Here I get the following warning:

SAWarning: Unicode column received non-unicode default value.
  Column('display_name', Unicode(255), default=''),

Changing Unicode(255) to String(255) makes the warning disappear but
I'm not sure if it's the correct solution.

For table names, the book uses the prefix convention 'tf_' but what
does it mean? 't' is table, but what is 'f'?

Thanks,

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


scraping a tumblr.com archive page

2011-11-20 Thread Jabba Laci
Hi,

I want to extract the URLs of all the posts on a tumblr blog. Let's
take for instance this blog: http://loveyourchaos.tumblr.com/archive .
If I download this page with a script, there are only 50 posts in the
HTML. If you scroll down in your browser to the end of the archive,
the browser will dynamically load newer and newer posts.

How to scrape such a dynamic page?

Thanks,

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


Re: scraping a tumblr.com archive page

2011-11-20 Thread Jabba Laci
Hi,

Thanks for the answer. Finally I found an API for this task:
http://www.tumblr.com/docs/en/api/v2#posts . It returns the required
data in JSON format.

Laszlo

 The page isn't really that dynamic- HTTP doesn't allow for that.
 Scrolling down the page triggers some Javascript. That Javascript
 sends some HTTP requests to the server, which returns more HTML, which
 gets stuck into the middle of the page. If you take the time to
 monitor your network traffic using a tool like Firebug, you should be
 able to figure out the pattern in the requests for more content. Just
 send those same requests yourself and parse the results.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: redis beginner question

2011-11-16 Thread Jabba Laci
 Why do you want to stop redis after your program terminates?  Generally,
 you just start redis up when the system boots and leave it running.

Hi,

OK, so it's more like MySQL or PostgeSQL, i.e. leave the server
running in the background. I wanted to use it like SQLite, i.e. let it
run only when I need it.

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


redis beginner question

2011-11-15 Thread Jabba Laci
Hi,

I'm reading the redis documentation and there is one thing that
bothers me. For redis, you need to start a server on localhost. Is
there an easy way that my Python script starts this server
automatically? Before using my script, I don't want to start
redis-server each time. When my program terminates, the server could
be shut down automatically.

Thanks,

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


install packages with pip to older Python versions

2011-09-26 Thread Jabba Laci
Hi,

I have Python 2.7 on my system. Today I wanted to try Google App
Engine but it runs on Python 2.5 at Google so I installed this version
on my machine next to v2.7 to avoid compatibility problems. However,
when I start the Python shell v2.5 and try to import something from
the GAE SDK (for instance from google.appengine.ext import webapp),
I get an error: ImportError: No module named webob. (Note that with
v2.7 I don't have this problem.)

So, how can I install packages for a specific version of Python (here,
v2.5)? With 2.7 I use sudo pip install package_name.

Thanks,

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


update all python packages on Debian/Ubuntu

2011-09-12 Thread Jabba Laci
Hi,

I use Ubuntu and the Python packages on my system were either
installed with (1) apt-get, or with (2) pip. Since the number of
python packages in the Ubuntu repositories is limited, I had to
install several packages with pip.

Now I want to upgrade the packages that were installed with pip (and
only those packages).

I can collect the list of python packages:
(pip freeze | cut -d = -f 1 | grep -v FIXME | xargs echo | tr ' ' '\n'
list.txt) 2/dev/null

However, if I update every package in list.txt with pip install -U,
it will also update the ones that were installed with apt-get. Since
apt-get builds a database with the installed files, I'm sure it won't
like that pip replaces those files. I didn't try it yet but I'm afraid
it would mess up the system.

So, how to upgrade the python packages correctly?

Thanks,

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


import os or import os.path

2011-09-06 Thread Jabba Laci
Hi,

If I want to use the 'os.path' module, it's enought to import 'os':

import os
if os.path.isfile('/usr/bin/bash'):
print 'got it'

In other source codes I noticed that people write 'import os.path' in
this case. Which is better practice?

Thanks,

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


Re: monitor mouse coordinates in real-time

2011-08-17 Thread Jabba Laci
Hi,

Thanks, the problem got solved. The updated version can be found at
https://gist.github.com/1144708 in a comment below the original post.
Solution:

self.connect(destroy, self.quit)

def quit(self, widget):
self.mouseThread.kill()
gtk.main_quit()

It was not evident that quit() must be passed the argument widget too.

Thanks,

Laszlo


On Wed, Aug 17, 2011 at 16:48, MrJean1 mrje...@gmail.com wrote:
 Check that the app.quit method registered with atexit is called.  E.g.
 on Windows, that may not be the case.

 /JeAN


 On Aug 14, 9:39 am, Jabba Laci jabba.l...@gmail.com wrote:
 I'm trying something similar. In the thread there is a variable which
 is modified by the parent. However, the thread doesn't quit the
 infinite loop. If someone could provide a patch, that'd be really
 useful.

 Thanks,

 Laszlo


 On Sun, Aug 14, 2011 at 13:00, TheSaint n...@nowhere.net.no wrote:
  Jabba Laci wrote:

  Could you please help me out how to close the application correctly?

  I think you should put a flag into the code, which the parent might modify
  it, so it will tell the child process to quit.
  Then the flag should need to be read periodically to know whether is time 
  to
  quit.

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


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


monitor mouse coordinates in real-time

2011-08-14 Thread Jabba Laci
Hi,

I wrote a simple GUI with pygtk to monitor mouse coordinates in
real-time. It works but when I close the application, the thread
doesn't stop and I can't figure out how to terminate it. Here is the
current source: https://gist.github.com/1144708 (83 lines).

Could you please help me out how to close the application correctly?

Thanks,

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


Re: monitor mouse coordinates in real-time

2011-08-14 Thread Jabba Laci
I'm trying something similar. In the thread there is a variable which
is modified by the parent. However, the thread doesn't quit the
infinite loop. If someone could provide a patch, that'd be really
useful.

Thanks,

Laszlo

On Sun, Aug 14, 2011 at 13:00, TheSaint n...@nowhere.net.no wrote:
 Jabba Laci wrote:

 Could you please help me out how to close the application correctly?

 I think you should put a flag into the code, which the parent might modify
 it, so it will tell the child process to quit.
 Then the flag should need to be read periodically to know whether is time to
 quit.

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

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


new string formatting with local variables

2011-06-06 Thread Jabba Laci
Hi,

I'd like to simplify the following string formatting:

solo = 'Han Solo'
jabba = 'Jabba the Hutt'
print {solo} was captured by {jabba}.format(solo=solo, jabba=jabba)
# Han Solo was captured by Jabba the Hutt

What I don't like here is this: solo=solo, jabba=jabba, i.e. the
same thing is repeated. In solo=solo, the left part is a key and the
right part is the value of a local variable, but it looks strange.

I'd like something like this:
print {solo} was captured by {jabba}.format(locals())# WRONG!

But it doesn't work.

Do you have any idea?

Thanks,

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


checking if a list is empty

2011-05-06 Thread Jabba Laci
Hi,

If I want to check if a list is empty, which is the more pythonic way?

li = []

(1) if len(li) == 0:
...
or
(2) if not li:
...

Thanks,

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


string formatting

2011-05-06 Thread Jabba Laci
Hi,

Which is the preferred way of string formatting?

(1) the %s is %s % ('sky', 'blue')

(2) the {0} is {1}.format('sky', 'blue')

(3) the {} is {}.format('sky', 'blue')

As I know (1) is old style. (2) and (3) are new but (3) is only
supported from Python 2.7+.

Which one should be used?

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


vertical ordering of functions

2011-05-03 Thread Jabba Laci
Hi,

I'm just reading Robert M. Martin's book entitled Clean Code. In Ch.
5 he says that a function that is called should be below a function
that does the calling. This creates a nice flow down from top to
bottom.
However, when I write a Python script I do just the opposite. I start
with the lines

if __name__ == __main__:
main()

Then I add main() above, which is a control function that contains
some function calls that decompose the problem into subproblems. Then
I add these functions above, etc.

Is there a convention for this? Should main() be at the top and called
function below?

Thanks,

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


download web pages that are updated by ajax

2011-04-12 Thread Jabba Laci
Hi,

I want to download a web page that is updated by AJAX. The page
requires no human interaction, it is updated automatically:
http://www.ncbi.nlm.nih.gov/nuccore/CP002059.1

If I download it with wget, I get a file of size 97 KB. The source is
full of AJAX calls, i.e. the content of the page is not expanded.
If I open it in a browser and save it manually, the result is a file
of almost 5 MB whose content is expanded.

(1) How to download such a page with Python? I need the post-AJAX
version of the page.
(2) Can it be done with wget or some other command-line utility? I
would be interested in that too.

Thanks,

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


Re: download web pages that are updated by ajax

2011-04-12 Thread Jabba Laci
 I've heard you can drive a web browser using Selenium
 (http://code.google.com/p/selenium/ ), have it visit the webpage and
 run the JavaScript on it, and then grab the final result.

Hi,

Thanks for the info. I tried selenium, you can get the source with the
get_html_source() function but it returns the original HTML, not the
DOM.

For the problem, I found a _general solution_ at
http://simile.mit.edu/wiki/Crowbar . Crowbar is a web scraping
environment based on the use of a server-side headless mozilla-based
browser.
Its purpose is to allow running javascript scrapers against a DOM...

A _specific solution_ for my case is to use BioPython:

---
from Bio import Entrez

id='CP002059.1'

Entrez.email = 'wha...@mail.com'
handle=Entrez.efetch(db='nucleotide',id=id,rettype='gb')
local_file=open(id,'w')
local_file.write(handle.read())
handle.close()
local_file.close()
---

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


Is the function filter deprecated?

2011-04-06 Thread Jabba Laci
Hi,

I tried Pylint today and it gave me a warning for the function
filter. Is it deprecated? Is the usage of list comprehensions
encouraged? The transformation is not complicated, by the way:
replace filter( func, seq ) with [ x for x in seq if func(x) ] .

Thanks,

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


XML header with lxml

2011-04-04 Thread Jabba Laci
Hi,

I want to construct an XML file with lxml but I don't find how to add
the '?xml version=1.0?' header.

from lxml import etree as ET

html = ET.Element(html)
print ET.tostring(html)

simply prints
html/

Thanks,

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


Re: value of pi and 22/7

2011-03-17 Thread Jabba Laci
 My favorite approximation is: 355/113  (visualize 113355 split into two 113 
 355 and then do the division). The first 6 decimal places are the same.

 3.141592920353982 = 355/113
 vs
 3.1415926535897931

Another, rather funny, approximation of the first 15 digits of pi is
to take the length of the words in the following verse:

s = 
How I want a drink
alcoholic of course
After the heavy lectures
involving complex functions


print [len(w) for w in s.split()]

will produce:

[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]

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


pip install, permission problem

2011-03-16 Thread Jabba Laci
Hi,

I'm trying to install a pypi package with pip. The installation is
done, but the permissions are not set correctly, thus I cannot import
it as a normal user.
Example:
sudo pip install termcolor
/usr/local/lib/python2.6/dist-packages/termcolor.py is created with
permissions 600

I can post-correct it manually, but there is something wrong going on
here... I have a hint but I'm not sure: as a normal user, I have
umask 077 in my .bashrc. When I do sudo, maybe this setting is
inherited? But even if it's the case, pip install should change the
permissions correctly IMO.

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


self-closing window with wxPython

2010-09-17 Thread Jabba Laci
Hi,

I'd like to create a simple alarm application that shows an alarm
window. The application should shut down automatically after 5
seconds. The problem is the following:
* If I keep the mouse outside of the window, the application keeps
running. Somehow self.Destroy() is not taken into account.
* If the mouse is over the window and I keep moving it, the window closes.

I'm using Ubuntu Linux with wxPython 2.8. Below you can find what I have so far.

Thanks,

Laszlo

==

class MyThread(threading.Thread):
def __init__(self, parent):
self.parent = parent
threading.Thread.__init__(self)

def run(self):
print time.time()# appears on stdout
time.sleep(5)
print time.time()# appears on stdout

self.parent.Destroy()# ??? doesn't work if the mouse is
outside of the application window

class Alarm(wx.Frame):
def __init__(self, title, *args):
wx.Frame.__init__(self, None, -1, title, pos=(0, 0),
size=(800, 600), *args)

self.sleepThread = MyThread(self)
self.sleepThread.start()

self.Bind(wx.EVT_CLOSE, self.on_close)

def on_close(self, event):
self.Destroy()

==

To call it:

class Main(wx.PySimpleApp):
def OnInit(self):
self.frame = alarm.Alarm(Alarm 0.1)
self.SetTopWindow(self.frame)
self.SetExitOnFrameDelete(True)
self.frame.Show()
return True

a = Main()
a.MainLoop()

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


Re: self-closing window with wxPython

2010-09-17 Thread Jabba Laci
Hi,

 2) I saw this in the documentation for Destroy() -- Frames and dialogs are 
 not destroyed immediately when this function is called -- they are added to a 
 list of windows to be deleted on idle time, when all the window's events have 
 been processed. That might be consistent with what you're seeing. The window 
 you're trying to destroy has no events in its queue. WHen you move the mouse 
 over it, the window processes those mouse events and then wx realizes, Hey, 
 this window has processed all of its events, and it's on the list of windows 
 to be destroyed. I'd better get rid of it.

 If you're interested in experimenting, find a non-mouse way to force that 
 window to process an event and I'll bet that would have the same effect as 
 moving the mouse over it.

Thanks for the hint, I could solve the problem. After Destroy() I
added an extra event:

self.parent.Destroy()
self.parent.dc.SetFocus()

As you suggested, the extra event triggers the queue processing and
when it becomes empty the window gets destroyed.

Thanks,

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


__str__ difficulty with lists

2009-11-01 Thread Jabba Laci
Hi,

I have a list that contains custom objects. When printing the list,
I'd like to have a readable result, i.e. I'd like to see the output of
the __str__ functions. See an example below. When I call print li, I
would like to get [3, 5]. How to do that?

Thanks,

Laszlo

==

class MyNumber:
def __init__(self, n):
self.n = n

def __str__(self):
return str(self.n)

if __name__ == __main__:
li = []
a = MyNumber(3)
li.append(a)
li.append(MyNumber(5))
print li   # [__main__.MyNumber instance at 0xb77a456c,
__main__.MyNumber instance at 0xb77a46ec]
print a   # 3
-- 
http://mail.python.org/mailman/listinfo/python-list


playing mp3

2009-10-26 Thread Jabba Laci
Hi,

What do you suggest for playing mp3 files with Python? I found a
simple module (http://code.google.com/p/mp3play/) but it only works
with Windows. I'd need Linux support too.

Thanks,

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


Re: Reverse Iteration Through Integers

2009-10-18 Thread Jabba Laci
Hi,

Would someone explain how str[::-1] work? I'm new to Python and I only
saw so far the str[begin:end] notation. What is the second colon?

Thanks,

Laszlo

 Here is a simplistic version that doesn't use fancy math:

 str(24)
 '24'
 str(24)[::-1]
 '42'
 int(str(24)[::-1])
 42
-- 
http://mail.python.org/mailman/listinfo/python-list


list to tuple and vice versa

2009-10-17 Thread Jabba Laci
Hi,

I have some difficulties with list - tuple conversion:

t = ('a', 'b')
li = list(t)   # tuple - list, works
print li   # ['a', 'b']

tu = tuple(li)   # list - tuple, error
print tu   # what I'd expect: ('a', 'b')

The error message is: TypeError: 'tuple' object is not callable.

Thanks,

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


Re: list to tuple and vice versa

2009-10-17 Thread Jabba Laci
 The error message is: TypeError: 'tuple' object is not callable.

 You created a variable named tuple somewhere, which is shadowing the
 built-in type. Rename that variable to something else.

Right, it was my bad. After removal the tuple() function works perfectly.

Thanks,

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