time.perf_counter in Python 2?

2014-10-20 Thread Florian Lindner
Hello,

I wrote a script that does some time measurements. It uses 
time.perf_counter() from Python 3 which works very well. Now I need to 
backport it to python 2.

Docs say that time.clock() is way to go:

time.clock()
On Unix, return the current processor time as a floating point number 
expressed in seconds. The precision, and in fact the very definition of the 
meaning of “processor time”, depends on that of the C function of the same 
name, but in any case, this is the function to use for benchmarking Python 
or timing algorithms.

On Windows, this function returns wall-clock seconds elapsed since the first 
call to this function, as a floating point number, based on the Win32 
function QueryPerformanceCounter(). The resolution is typically better than 
one microsecond.

But for me it always returns the almost same number, nothing time like:

Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.clock()
0.03
>>> time.clock()
0.03
>>> time.clock()
0.04
>>> time.clock()
0.04


What's wrong there?

Thanks,
Florian

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


Why is regexp not working?

2014-07-04 Thread Florian Lindner
Hello,

I have that piece of code:

def _split_block(self, block):
cre = [re.compile(r, flags = re.MULTILINE) for r in self.regexps]
block = "".join(block)
print(block)
print("---")
for regexp in cre:
match = regexp.match(block)
for grp in regexp.groupindex:
data = match.group(grp) if match else None
self.data[grp].append(data)


block is a list of strings, terminated by \n. self.regexps:


self.regexps = [r"it (?P\d+) .* dt complete yes | 
write-iteration-checkpoint |",
r"it (?P\d+) read ahead"


If I run my program it looks like that:


it 1 ahadf dt complete yes | write-iteration-checkpoint |
Timestep completed

---
it 1 read ahead
it 2 ahgsaf dt complete yes | write-iteration-checkpoint |
Timestep completed

---
it 4 read ahead
it 3 dfdsag dt complete yes | write-iteration-checkpoint |
Timestep completed

---
it 9 read ahead
it 4 dsfdd dt complete yes | write-iteration-checkpoint |
Timestep completed

---
it 16 read ahead
---
{'it_read_ahead': [None, '1', '4', '9', '16'], 'coupling_iterations': ['1', 
None, None, None, None]}

it_read_ahead is always matched when it should (all blocks but the first). 
But why is the regexp containing coupling_iterations only matched in the 
first block?

I tried different combinations using re.match vs. re.search and with or 
without re.MULTILINE.

Thanks!
Florian

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


Get named groups from a regular expression

2014-07-01 Thread Florian Lindner
Hello,

Is there a way I can extract the named groups from a regular expression? 
e.g. given "(?P\d)" I want to get something like ["testgrp"].

OR

Can I make the match object to return default values for named groups, even 
if no match was produced?

Thanks,
Florian

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


Format String: Only when value supplied

2014-06-24 Thread Florian Lindner
Hello,

I have a format string like:

 print "{:10} {:25} = {:6}   ({})".format(mod, name, value, description)

description can be None. In this case I want to print an empty string (which 
can be achieved by replacing it with 'description or ""') and I want to omit 
the brackets. Is there a way to tell the format string to omit a part if an 
input variable is None?

Another question: value can be bool. When I format value with just {} if 
prints True or False, when I use {:6} it prints 1 or 0. Is there a way to 
get pack to True / False?

Thanks!
Florian

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


Re: python-list@python.org

2014-01-14 Thread Florian Lindner
Am Dienstag, 14. Januar 2014, 17:00:48 schrieb MRAB:
> On 2014-01-14 16:37, Florian Lindner wrote:
> > Hello!
> >
> > I'm using python 3.2.3 on debian wheezy. My script is called from my mail 
> > delivery agent (MDA) maildrop (like procmail) through it's xfilter 
> > directive.
> >
> > Script works fine when used interactively, e.g. ./script.py < testmail but 
> > when called from maildrop it's producing an infamous UnicodeDecodeError:
> >
> > File "/home/flindner/flofify.py", line 171, in main
> >   mail = sys.stdin.read()
> > File "/usr/lib/python3.2/encodings/ascii.py", line 26, in decode
> >   return codecs.ascii_decode(input, self.errors)[0]
> >
> > Exception for example is always like
> >
> > UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 869: 
> > ordinal not in range(128)
> > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1176: 
> > ordinal not in range(128)
> > UnicodeDecodeError: 'ascii' codec can't decode byte 0x8c in position 846: 
> > ordinal not in range(128)
> >
> > I read mail from stdin "mail = sys.stdin.read()"
> >
> > Environment when called is:
> >
> > locale.getpreferredencoding(): ANSI_X3.4-1968
> > environ["LANG"]: C
> >
> > System environment when using shell is:
> >
> > ~ % echo $LANG
> > en_US.UTF-8
> >
> > As far as I know when reading from stdin I don't need an decode(...) call, 
> > since stdin has a decoding. I also tried some decoding/encoding stuff but 
> > changed nothing.
> >
> > Any ideas to help me?
> >
> When run from maildrop it thinks that the encoding of stdin is ASCII.

Well, true. But what encoding does maildrop actually gives me? It obviously 
does not inherit LANG or is called from the MTA that way. I also tried:

inData = codecs.getreader('utf-8')(sys.stdin)   


 
mail = inData.read()


 

Failed also. But I'm not exactly an encoding expert.

Regards,
Florian

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


Encoding trouble when script called from application

2014-01-14 Thread Florian Lindner
Hello!

I'm using python 3.2.3 on debian wheezy. My script is called from my mail 
delivery agent (MDA) maildrop (like procmail) through it's xfilter directive.

Script works fine when used interactively, e.g. ./script.py < testmail but when 
called from maildrop it's producing an infamous UnicodeDecodeError:

File "/home/flindner/flofify.py", line 171, in main
 mail = sys.stdin.read()
File "/usr/lib/python3.2/encodings/ascii.py", line 26, in decode
 return codecs.ascii_decode(input, self.errors)[0]

Exception for example is always like

UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 869: 
ordinal not in range(128) 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1176: 
ordinal not in range(128)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x8c in position 846: 
ordinal not in range(128)

I read mail from stdin "mail = sys.stdin.read()"

Environment when called is:

locale.getpreferredencoding(): ANSI_X3.4-1968
environ["LANG"]: C

System environment when using shell is:

~ % echo $LANG
en_US.UTF-8

As far as I know when reading from stdin I don't need an decode(...) call, 
since stdin has a decoding. I also tried some decoding/encoding stuff but 
changed nothing.

Any ideas to help me?

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


argparse action on default values

2014-01-08 Thread Florian Lindner
Hello,

I use argparse from Python 3.3.3 with a custom action that normalizes path 
arguments:

http://docs.python.org/3/library/argparse.html#action

def norm_path(*parts):
""" Returns the normalized, absolute, expanded and joined path, assembled 
of all parts. """
parts = [ str(p) for p in parts ] 
return os.path.abspath(os.path.expanduser(os.path.join(*parts)))

# Taken from the docs
class NormPath(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print('%r %r %r' % (namespace, values, option_string))
setattr(namespace, self.dest, norm_path(values))


def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--config", help="Path to config file.",
default = "~/.foobar/config", action=NormPath)

return parser.parse_args()


This works fine when there is actually a --config=path supplied. But it's not 
being applied on default arguments. Of course, I could use "default = 
norm_path('~/.foobar/config')" but I expect that custom actions are applied to 
default values as well. The store action works alike for default and supplied 
values.

What do you think?

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


Re: Trouble with UnicodeEncodeError and email

2014-01-08 Thread Florian Lindner
Am Donnerstag, 9. Januar 2014, 00:26:15 schrieb Chris Angelico:
> On Thu, Jan 9, 2014 at 12:14 AM, Florian Lindner  wrote:
> > I've written some tiny script using Python 3 and it used to work perfectly. 
> > Then I realized it needs to run on my Debian Stable server too, which 
> > offers only Python 2. Ok, most backporting was a matter of minutes, but I'm 
> > becoming desperate on some Unicode error...
> 
> Are you sure it does? The current Debian stable is Wheezy, which comes
> with a package 'python3' in the repository, which will install 3.2.3.
> (The previous Debian stable, Squeeze, has 3.1.3 under the same name.)
> You may need to change your shebang, but that's all you'd need to do.
> Or are you unable to install new packages? If so, I strongly recommend
> getting Python 3 added, as it's going to spare you a lot of Unicode
> headaches.
> 
> Mind you, I compile my own Py3 for Wheezy, since I like to be on the
> bleeding edge. But that's not for everyone. :)

Well, I thought I had scanned to repos but obviously... I had to install 
BeautifulSoup and scikit-learn manually. Now some other Unicode issues have 
arised, but I need to sort them out first how they are connected to my mail 
delivery agent.

Thx a lot,

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


Trouble with UnicodeEncodeError and email

2014-01-08 Thread Florian Lindner
Hello!

I've written some tiny script using Python 3 and it used to work perfectly. 
Then I realized it needs to run on my Debian Stable server too, which offers 
only Python 2. Ok, most backporting was a matter of minutes, but I'm becoming 
desperate on some Unicode error...

i use scikit-learn to train a filter on a set of email messages:

vectorizer = CountVectorizer(input='filename', decode_error='replace', 
strip_accents='unicode',
 preprocessor=self.mail_preprocessor, 
stop_words='english')

http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html

The vectorizer gets a list of filenames, reads them and passes them to the 
preprocessor:


def mail_preprocessor(self, message):
# Filter POPFile cruft by matching date string at the beginning.
print("Type:", type(message)) # imported from __future__
pop_reg = re.compile(r"^[0-9]{4}/[0-1][1-9]/[0-3]?[0-9]")
message = [line for line in message.splitlines(True) if not 
pop_reg.match(line)]
xxx = "".join(message)
msg = email.message_from_string(xxx)  # <-- CRASH here

msg_body = ""

for part in msg.walk():
if part.get_content_type() in ["text/plain", "text/html"]:
body = part.get_payload(decode=True)
soup = BeautifulSoup(body)
msg_body += soup.get_text(" ", strip=True)


if "-BEGIN PGP MESSAGE-" in msg_body:
msg_body = ""

msg_body += " ".join(email.utils.parseaddr(msg["From"]))
try:
msg_body += " " + msg["Subject"]
except TypeError: # Can't convert 'NoneType' object to str implicitly
pass
msg_body = msg_body.lower()
return msg_body


Type: 

Traceback (most recent call last):
  File "flofify.py", line 182, in 
main()
  File "flofify.py", line 161, in main
model.train()
  File "flofify.py", line 73, in train
vectors = vectorizer.fit_transform(data[:,1])
  File "/usr/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", 
line 780, in fit_transform
vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary)
  File "/usr/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", 
line 715, in _count_vocab
for feature in analyze(doc):
  File "/usr/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", 
line 229, in 
tokenize(preprocess(self.decode(doc))), stop_words)
  File "flofify.py", line 119, in mail_preprocessor
msg = email.message_from_string(xxx)
  File "/usr/lib/python2.7/email/__init__.py", line 57, in message_from_string
return Parser(*args, **kws).parsestr(s)
  File "/usr/lib/python2.7/email/parser.py", line 82, in parsestr
return self.parse(StringIO(text), headersonly=headersonly)
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 
1624: ordinal not in range(128)

I've tried various modifications like encoding/decoding the message argument to 
utf-8.

Any help?

Thanks!

Florian

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


Flattening an email message

2013-11-26 Thread Florian Lindner
Hello,

I want to use some machine learning stuff on mail messages. First step is get 
some flattened text from a mail message, python's email package does not work 
as automatically as I wish. Right now I have:

> def mail_preprocessor(str):
> msg = email.message_from_string(str)
> msg_body = ""
> 
> for part in msg.walk():
> if part.get_content_type() == "text/plain":
> msg_body += part.get_payload(decode=True)
> 
> msg_body = msg_body.lower()
> msg_body = msg_body.replace("\n", " ")
> msg_body = msg_body.replace("\t", " ")
> return msg_body

For getting a text from html I could use BeautifulSoup. Right now I'm still a 
layer down (encapsulation etc.) at RFC 2822 stuff. 

Does anybody knows about some package or code I can throw an email message at 
and get some kind of text from it? Attachments being discarded, HTML I can 
take care of...

Thanks!

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


Re: Problem calling script with arguments

2013-10-15 Thread Florian Lindner
Am Dienstag, 15. Oktober 2013, 13:18:17 schrieb Michael Speer:
> >  "/usr/sbin/ftpasswd" "--hash"
> 
> You're missing a comma, and python automatically concatenates adjacent
> strings.

Damn!

Thanks!

> 
> On Tue, Oct 15, 2013 at 1:13 PM, Florian Lindner wrote:
> > Hello,
> > 
> > I have a 3rd party perl script:
> >  head -n 1 /usr/sbin/ftpasswd
> > 
> > #!/usr/bin/perl
> > 
> > I want to write data to stdin and read from stdout:
> > 
> > proc = Popen( ["/usr/bin/perl", "/usr/sbin/ftpasswd" "--hash", "--stdin"],
> > stdout=PIPE, stdin=PIPE)
> > 
> > output, input = proc.communicate(pwd)
> > return output.strip()
> > 
> > Since pwd comes from a non-trusted source I don't want to use shell=True.
> > 
> > The arguments to the perl interpreter do not seem to right:
> > 
> > Can't open perl script "/usr/sbin/ftpasswd--hash": No such file or
> > directory
> > 
> > Adding a leading " " to "--hash" does not help.
> > 
> > How can I use that script and achieve something like
> > 
> > # echo "123" | ftpasswd --hash --stdin
> > ftpasswd: $1$8BuLAqCl$y/URBN/OCSLsKtnu8nFHH0
> > 
> > Thanks!
> > 
> > Florian
> > --
> > https://mail.python.org/mailman/listinfo/python-list

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


Problem calling script with arguments

2013-10-15 Thread Florian Lindner
Hello,

I have a 3rd party perl script:

 head -n 1 /usr/sbin/ftpasswd
#!/usr/bin/perl

I want to write data to stdin and read from stdout:

proc = Popen( ["/usr/bin/perl", "/usr/sbin/ftpasswd" "--hash", "--stdin"], 
stdout=PIPE, stdin=PIPE)

output, input = proc.communicate(pwd)
return output.strip()

Since pwd comes from a non-trusted source I don't want to use shell=True.

The arguments to the perl interpreter do not seem to right:

Can't open perl script "/usr/sbin/ftpasswd--hash": No such file or directory

Adding a leading " " to "--hash" does not help.

How can I use that script and achieve something like

# echo "123" | ftpasswd --hash --stdin
ftpasswd: $1$8BuLAqCl$y/URBN/OCSLsKtnu8nFHH0

Thanks!

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


Using inner dict as class interface

2013-01-16 Thread Florian Lindner
Hello,

I have a:

class C:
   def __init__(self):
  d = dict_like_object_created_somewhere_else()

  def some_other_methods(self):
pass


class C should behave like a it was the dict d. So I could do:

c = C()
print c["key"]
print len(c)

but also

c.some_other_method()

How can I achieve that? Do I need to define all methods like
__getitem__, __len__, ... (what else?) to access the inner dict or is
there something more slick?

Thanks,

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


Logging handler: No output

2012-09-02 Thread Florian Lindner
Hello,

I have a class method that executes a subprocess. There are two loggers in the 
class, self.logger for general logging and proclog for process output (stdout 
& stderr) logging which should go to stdout and a file:


def start_process(self, command, no_shlex=False, raise_excpt=True,
  print_output = True, **kwargs):

cmd = command if no_shlex else shlex.split(command)

# Use an additional logger without formatting for process output. 
proclog = logging.getLogger(self.config.tag)
proclog.propagate = False # Process output should not propage to the main
logger
logfile = self._logfilename()

if logfile:
proclog.addHandler(logging.FileHandler(logfile))

if print_output:
proclog.addHandler(logging.StreamHandler(sys.stdout))

self.popen = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, bufsize=0, **kwargs)
while True:
output = self.popen.stdout.readline().decode()
if output == "" and self.popen.poll() != None:
break
proclog.info(output.rstrip("\n"))

ret_code = self.popen.returncode

self.logger.debug("%s returned with %i", command, ret_code)


But neither the FileHandler nor the StreamHandler produce any actual output. 
The file is being created but stays empty. If I use a print output in the 
while loop it works, so output is catched and the applications stdout in 
working. But why the logger proclog catching nothing?

Thanks,

Florian


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


XML parser: Element ordering?

2012-08-31 Thread Florian Lindner
Hello,

I plan to use the etree.ElementTree XML parser to parse a config file
in which the order of the elements matter, e.g.:





is not equal to:





I have found different answers to the question if order matters in XML
documents. So my question here: Does it matters (and is more or less
guarenteed to matter in the future) for the ElementTree parser of
python?

Thanks,

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


Cut out XML subtree

2012-08-29 Thread Florian Lindner
Hello,

I have a (rather small, memory consumption is not an issue) XML document. The 
application is still at the planning stage, so none of the XML parsers from 
the stdlib is choosen yet.

I want to cut out an XML subtree like that:


  

 


Now I want to get the subB note including parent node, but none of 
sibliblings:


 


Is there a way I can do that using etree or DOM? The first is prefered...

Thanks,

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


Remove root handler from logger

2012-05-14 Thread Florian Lindner
Hello,

I configure my logging on application startup like that:

logging.basicConfig(level = logging.DEBUG, format=FORMAT, filename = logfile)
ch  = logging.StreamHandler()
ch.setFormatter(logging.Formatter(FORMAT))
logging.getLogger().addHandler(ch)

In one module of my application I want a logger that does not log to logfile 
but to another file. How can I get a logger that is either plain (no handlers 
attached) or remove a handler?

The handlers that are derived from the root logger are not shown in handlers:

(Pdb) logger1.handlers
[]
(Pdb) logging.getLogger().handlers
[, ]

How can I remove the FileHandler and StreamHandler from logger1 without 
affecting the root logger?

logger1.removeHandler() does not work since there are no handlers on logger1.

Thanks,

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


tee-like behavior in Python

2012-05-09 Thread Florian Lindner
Hello,

how can I achieve a behavior like tee in Python?

* execute an application
* leave the output to stdout and stderr untouched
* but capture both and save it to a file (resp. file-like object)

I have this code

proc = subprocess.Popen(shlex.split(cmd), stdout = subprocess.PIPE,
stderr=subprocess.STDOUT)
while True:
out = proc.stdout.readline()
if out == '' and proc.poll() != None:
   break
sys.stdout.write(out)
logfile.write(out)

This works so far but always buffers a couple of lines and outputs
them en bloc. The final output is like it is desired but with a
significant delay. Is there a way around that?

Thanks,

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


Re: Avoid newline at the end

2007-11-11 Thread Florian Lindner
Steven D'Aprano wrote:

> On Sun, 11 Nov 2007 11:22:19 +0100, Florian Lindner wrote:
> 
>> Hello,
>> I have a piece of code like that:
>> 
>> for row in resultSet:
>> logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
>> logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--
>> 
>> Now I want to avoid the newline at the last iteration and only at the
>> second line.
> 
> That means your log file doesn't end with a newline. That's often not
> good, because it can confuse some tools.
> 
> Also, appending lots of strings together like that is very inefficient.
> 
>> How to do that most elegantly with Python?
> 
> If you have a small number of rows (say, less than a few tens of
> thousands), you can do this:
> 
> rows = []
> for row in resultSet:
> rows.append("/home/%s/%s/log/access.log" % (row[1], row[0]))
> rows.append("/home/%s/%s/log/error.log" % (row[1], row[0]))
> # note that there are no newlines
> logs = '\n'.join(rows) # do it once at the end
> 
> But again, when you write text to a file, you should end it with a
> newline. It isn't compulsory, but it is best practice.
> 
> Alternatively, check out the logging module.

That is not log file it's a config file for logrotate. And the log string
goes into a template therefore the config file ends with a newline. The
problem is that logrotate gets confused by empty lines between logfile path
and config.
The number of lines will always be < 100 and so config will only be
regenerated not often so efficiency is not issue.


Regards,

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


Avoid newline at the end

2007-11-11 Thread Florian Lindner
Hello,
I have a piece of code like that:

for row in resultSet:
logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--

Now I want to avoid the newline at the last iteration and only at the second
line.
How to do that most elegantly with Python?

Thanks,

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


Re: Coding Help

2007-11-10 Thread Florian Lindner
Marc 'BlackJack' Rintsch wrote:

> On Sat, 10 Nov 2007 09:45:47 -0800, rishiyoor wrote:
> 
>> I need help coding my flowchart. The C's are conditions, the S's are
>> statements. The statements do not affect the conditions except for S5
>> which is an increment for C0. The left is True, and the right is
>> False.
>> 
>> I would probably use a while loop (or for loop without S5) for the
>> first condition C0, and I am thinking of evaluating the rest of the
>> conditions using if statements. But if I use if-statements, I am not
>> able to accomodate the breaks.
>> 
>> Program
>>├───┐
>>┌─< C0 >─┐  │
>>│|  │
>>│┌< C1 >──┐ │
>>│  ┌───< C2 >┐   ┌──< C3 >─┐│
>>│  │┌─< C4 >─┐  [S1]   ││
>>│  │   [S2] [S3] └┬┘│
>>│  │││┌─< C4 >─┐│
>>│  │││   [S4]  ││
>>│  │││└───┬┘│
>>│  └───┬┴┴┘ │
>>│ [S5]  │
>>│  └┘
>> Program
> 
> Sounds pretty much like homework to me.

It's nothing bad ask about help for a homework as long as noone expects to
get the entire solution.

To the OP: What is your problem?

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

Re: Problem with format string / MySQL cursor

2007-10-18 Thread Florian Lindner
On 18 Okt., 22:08, Paul McNett <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > On Oct 19, 7:32 am, Florian Lindner <[EMAIL PROTECTED]> wrote:
> >> Hello,
> >> I have a string:
>
> >> INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
> >> `gid`, `password`) VALUES (%s, %s, %s, %s, %i, %i, %s)
>
> >> that is passed to a MySQL cursor from MySQLdb:
>
> >> ret = cursor.execute(sql, paras)
>
> >> paras is:
>
> >> ('flindner', '[EMAIL PROTECTED]', '/home/flindner/', '/home/
> >> flindner/Mail/test', 1001, 1001, '123')
>
> >> But that gives me an error:
>
> >> Traceback (most recent call last):
> >>   File "account.py", line 188, in ?
> >> main()
> >>   File "account.py", line 33, in main
> >> execute(action, account_type, options)
> >>   File "account.py", line 129, in execute
> >> executeSQL(sql, options.username, options.login, options.home,
> >> options.directory, options.uid, options.gid, options.password)
> >>   File "/home/flindner/common.py", line 29, in executeSQL
> >> ret = cursor.execute(sql, paras)
> >>   File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line
> >> 148, in execute
> >> query = query % db.literal(args)
> >> TypeError: int argument required
>
> >> I don't see errors in the format string or some other problem
>
> >> What's wrong?
> > You should be using '?' for parameter bindings in your sql string not
> > python format specifiers (you are after all writing sql here not
> > python).
>
> > INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
> > `gid`, `password`) VALUES (?, ?, ?, ?, ?, ?, ?)
>
> Sorry Tim, but that isn't correct:
>
> >>> import MySQLdb
> >>> MySQLdb.paramstyle
>
> 'format'
>
> Florian, what happens when you replace your %i with %s?

That works! Thanks! But a weird error message for this solution...

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


Problem with format string / MySQL cursor

2007-10-18 Thread Florian Lindner
Hello,
I have a string:

INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
`gid`, `password`) VALUES (%s, %s, %s, %s, %i, %i, %s)

that is passed to a MySQL cursor from MySQLdb:

ret = cursor.execute(sql, paras)

paras is:

('flindner', '[EMAIL PROTECTED]', '/home/flindner/', '/home/
flindner/Mail/test', 1001, 1001, '123')

But that gives me an error:

Traceback (most recent call last):
  File "account.py", line 188, in ?
main()
  File "account.py", line 33, in main
execute(action, account_type, options)
  File "account.py", line 129, in execute
executeSQL(sql, options.username, options.login, options.home,
options.directory, options.uid, options.gid, options.password)
  File "/home/flindner/common.py", line 29, in executeSQL
ret = cursor.execute(sql, paras)
  File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line
148, in execute
query = query % db.literal(args)
TypeError: int argument required


I don't see errors in the format string or some other problem

What's wrong?

Thanks,

Florian

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


Re: test if email

2007-10-12 Thread Florian Lindner
 [EMAIL PROTECTED] wrote:

> On Oct 12, 2:55 pm, Florian Lindner <[EMAIL PROTECTED]> wrote:
>> Hello,
>> is there a function in the Python stdlib to test if a string is a valid
>> email address?
>>
>> Thanks,
>>
>> florian
> 
> What do you mean? If you're just testing the construction of the email
> address string, then it's pretty easy. If you want to know if the
> email address is live and works, the only way to reliably find that
> out is to send a test email there to see if it goes through or
> bounces.
> 
> Here's an interesting article on the topic, which is completely non-
> Python related:
> 
>
http://www.oreillynet.com/onlamp/blog/2002/12/how_to_validate_an_email_addre.html

Answer to everybody:

I was just asking for the correct syntax of the mail address. I know about
the various problems actually impossibility to test for a live and valid
address.

Regards,

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


Re: Problem with global

2007-10-12 Thread Florian Lindner
Larry Bates wrote:

> Florian Lindner wrote:
>> Hello,
>> I have a little problem with the global statement.
>> 
>> def executeSQL(sql, *args):
>> try:
>> import pdb; pdb.set_trace()
>> cursor = db.cursor()  # db is .
>> [...]
>> except:
>> print "Problem contacting MySQL database. Please contact root."
>> sys.exit(-1)
>> 
>> 
>> db  = None # Global Variable for DB connection
>> 
>> def main():
>> [...]
>> global db
>> db = MySQLdb.connect(...)
>> [...]
>> executeSQL(sql, args)
>> 
>> 
>> Why isn't the global variable db not written in main() to be a mysql
>> connection and still none type in executeSQL?
>> 
>> Thanks,
>> 
>> Florian
> 
> Because you have it to let executeSQL know that it is global or it creates
> a local copy in local namespace.

That's not right in the context because db is read before it written.
Therefore the global copy springs into the local namespace.

> def executeSQL(sql, *args):
>  global db
>  try:
>  import pdb; pdb.set_trace()
>  cursor = db.cursor()  # db is .
>  [...]
>  except:
>  print "Problem contacting MySQL database. Please contact root."
>  sys.exit(-1)

I've solved it. It was a problem you could not have possibly seen. Actually
in my script executeSQL is called before db = MySQLdb.connect(..) is
called. When I have simplified the code for the posting I've changed it
made it right without knowing.

Regards,

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


test if email

2007-10-12 Thread Florian Lindner
Hello,
is there a function in the Python stdlib to test if a string is a valid
email address?

Thanks,

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


Problem with global

2007-10-12 Thread Florian Lindner
Hello,
I have a little problem with the global statement.

def executeSQL(sql, *args):
try:
import pdb; pdb.set_trace()
cursor = db.cursor()  # db is .
[...]
except:
print "Problem contacting MySQL database. Please contact root."
sys.exit(-1) 


db  = None # Global Variable for DB connection

def main():
[...]
global db
db = MySQLdb.connect(...)
[...]
executeSQL(sql, args)


Why isn't the global variable db not written in main() to be a mysql
connection and still none type in executeSQL?

Thanks,

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


Re: Problem with MySQL cursor

2007-10-12 Thread Florian Lindner
Carsten Haese wrote:

> On Fri, 2007-10-12 at 13:12 +0200, Florian Lindner wrote:
>> Carsten Haese wrote:
>> > sql = "INSERT INTO "+DOMAIN_TABLE+"("+DOMAIN_FIELD+") VALUES (%s)"
>> > executeSQL(sql, domainname)
>> 
>> Ok, I understand it and now it works, but why is limitation? Why can't I
>> just the string interpolation in any playes and the cursor function
>> escapes any strings so that they can't do harm to my query?
> 
[...]

Thanks for your good explanation!

Florian

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


Re: Problem with MySQL cursor

2007-10-12 Thread Florian Lindner
Carsten Haese wrote:

> On Thu, 2007-10-11 at 15:14 +0200, Florian Lindner wrote:
>> Hello,
>> I have a function that executes a SQL statement with MySQLdb:
>> 
>> def executeSQL(sql,  *args):
>> print sql % args
>> cursor = conn.cursor()
>> cursor.execute(sql, args)
>> cursor.close()
>> 
>> it's called like that:
>> 
>> sql = "INSERT INTO %s (%s) VALUES (%s)"
>> executeSQL(sql,  DOMAIN_TABLE, DOMAIN_FIELD, domainname)
> 
> You can't use parameter binding to substitute table names and column
> names, or any other syntax element, into a query. You can only bind
> parameters in places where a literal value would be allowed (more or
> less, the real rules are more complicated, but this rule of thumb gets
> you close enough). You have to construct the query string like this, for
> example:
> 
> sql = "INSERT INTO "+DOMAIN_TABLE+"("+DOMAIN_FIELD+") VALUES (%s)"
> executeSQL(sql, domainname)

Ok, I understand it and now it works, but why is limitation? Why can't I
just the string interpolation in any playes and the cursor function escapes
any strings so that they can't do harm to my query?

Regards,

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


Last iteration?

2007-10-12 Thread Florian Lindner
Hello,
can I determine somehow if the iteration on a list of values is the last
iteration?

Example:

for i in [1, 2, 3]:
   if last_iteration:
  print i*i
   else:
  print i

that would print

1
2
9


Can this be acomplished somehow?

Thanks,

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


Problem with MySQL cursor

2007-10-11 Thread Florian Lindner
Hello,
I have a function that executes a SQL statement with MySQLdb:

def executeSQL(sql,  *args):
print sql % args
cursor = conn.cursor()
cursor.execute(sql, args)
cursor.close()

it's called like that:

sql = "INSERT INTO %s (%s) VALUES (%s)"
executeSQL(sql,  DOMAIN_TABLE, DOMAIN_FIELD, domainname)

The statement that is printed looks ok (missing quotes, but AFAIK
cursor.execute does that):

INSERT INTO domains (domain) VALUES (xgm.de)

but MySQL prints an error:

Traceback (most recent call last):
  File "manage.py", line 90, in ?
addDomain(domainName)
  File "manage.py", line 27, in addDomain
executeSQL(sql,  DOMAIN_TABLE, DOMAIN_FIELD, domainname)
  File "manage.py", line 22, in executeSQL
cursor.execute(sql, args)
  File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 163, in
execute
self.errorhandler(self, exc, value)
  File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 35,
in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version for
the right syntax to use near ''domains' ('domain') VALUES ('xgm.de')' at
line 1")

I see the error: 2 opening quotes but only 1 closing around domains. But
where do they come from?

Note that there are no quotes at print sql % args.

Thanks,

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


Formal interfaces with Python

2007-05-28 Thread Florian Lindner
Hello,
some time ago I've heard about proposals to introduce the concecpt of
interfaces into Python. I found this old and rejected PEP about that:
http://www.python.org/dev/peps/pep-0245/

What is the current status of that?

Thanks,

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


Convert from/to struct_time

2007-04-22 Thread Florian Lindner
Hello,
I have a struct_time and a datetime object and need compare them. Is there
any function that converts either of these two to another?

Thanks,

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


Re: RSS feed parser

2007-04-04 Thread Florian Lindner
[EMAIL PROTECTED] wrote:

> On Apr 2, 10:20 pm, Florian Lindner <[EMAIL PROTECTED]> wrote:
>> Some of the question I have but found answered nowhere:
>>
>> I have a feedparser object that was created from a string. How can I
>> trigger a update (from a new string) but the feedparser should treat the
>> new string like the same feed (thus setting feed.updated etc.).
> 
> Hmm. Do you mean that the feed object should stay the same? Like the
> difference between "a = [1,2,3]; a = [1,2,3]+[4]" and "a = [1,2,3];
> a.append(4)"? I glanced at the parse function in the source code and
> it looks like it's not directly possible. You could modify it so that
> the "result" dictionary is optionally given as an argument, so when
> updating you'd do: feedparser.parse(string, oldFeed). You'd also have
> to clear the oldFeed object before update.
> 
> But you might also be able to solve the problem by using an additional
> layer of indirection. Instead of passing around the "feed" object,
> you'd pass around a proxy object like this:
> 
> class Empty: pass
> proxy = Empty()
> proxy.feed = feedparser.parse(string)
> storeProxyForLaterUse(proxy)
> proxy.feed = feedparser.parse(string2)
> useStoredProxy() #this would use the updated feed through the proxy
> 
> Then just use proxy.feed.updated everywhere instead of directly
> feed.updated. A smarter proxy would automatically translate
> proxy.updated into proxy.feed.updated so usage would stay as simple as
> without the proxy. Doing this is quite easy in Python (search for
> __getattr__ examples).

I already use something like that (with __getattr__). The problem is that
with this way there is still a new feed object created everytime a new
string needs to be passed to.
But since I want to use use the updated_parsed etc. function it's not
possible that each time the feed is parsed a new object is created (the
update times will always be the time of the last parsing).

Any idea?

Regards,

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


Re: RSS feed parser

2007-04-02 Thread Florian Lindner
[EMAIL PROTECTED] wrote:

> On Apr 2, 7:22 pm, Florian Lindner <[EMAIL PROTECTED]> wrote:
>> Hello,
>> I'm looking for python RSS feed parser library.
>> Feedparserhttp://feedparser.org/does not seem to maintained anymore.
>>
>> What alternatives are recommendable?
>>
>> Thanks,
>>
>> Florian
> 
> Well, even if it's not maintained anymore (where does it say that?),
> it works fine and the API is great. Although of course I do realize
> that when a new version of RSS appears, feedparser won't be able to
> support it unless someone updates it. But RSS 2.0 appeared already in
> 2002 and no new versions have come since. So I wouldn't worry too much
> about new RSSs popping up every month. Maybe the feedparser code
> hasn't been updated in a while because it's already perfect and
> there's nothing to add to it?-)

No postings neither on the mailinglists nor in the forums are being
answered.
Somewhere he stated that he had turned to another hobby.

Some of the question I have but found answered nowhere:

I have a feedparser object that was created from a string. How can I trigger
a update (from a new string) but the feedparser should treat the new string
like the same feed (thus setting feed.updated etc.). 
 
- How can I trigger a update from a new file? 
- Does feedparser has the desired behavior? 

Regards,

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


RSS feed parser

2007-04-02 Thread Florian Lindner
Hello,
I'm looking for python RSS feed parser library. Feedparser
http://feedparser.org/ does not seem to maintained anymore.

What alternatives are recommendable?

Thanks,

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


Using string as file

2007-03-05 Thread Florian Lindner
Hello,
I have a function from a library thast expects a file object as argument.
How can I manage to give the function a string resp. have the text it would
have written to file object as a string?

Thanks,

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


RSS feed creator

2007-03-04 Thread Florian Lindner
Hello,
I'm looking for a python library that creates a RSS and/or Atom feed. E.g. I
give a list like that:
[
  [title1, short desc1, author1],
  [title2, short desc2, author2],
]

and the library creates a valid feed XML file. (return as a string)

Thanks,

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


Static variables

2007-01-24 Thread Florian Lindner
Hello,
does python have static variables? I mean function-local variables that keep
their state between invocations of the function.

Thanks,

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


Re: ConfigParser and multiple option names

2006-05-10 Thread Florian Lindner
[EMAIL PROTECTED] wrote:

> that will break horribly in windows, remenber it install all it's crap
> in c:\Program Files

Why should this break? If you split at the \n character?

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


Calling superclass

2006-05-04 Thread Florian Lindner
Hello,
I try to call the superclass of the ConfigParser object:

class CustomizedConfParser(ConfigParser.SafeConfigParser):
def get(self, section, attribute):
try:
return super(CustomizedConfParser, self).get(section, attribute)
# [...]


but that gives only

return super(CustomizedConfParser, self).get(section, attribute)
TypeError: super() argument 1 must be type, not classobj

I don't really understand the error message.

Thanks,

Florian

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


Gettings subdirectories

2006-05-03 Thread Florian Lindner
Hello,
how can I get all subdirectories of a given directories? os.listdir() gives
me all entries and I've found no way to tell if an object is a file or a
directory.

Thanks,

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


RFC 822 continuations

2006-05-02 Thread Florian Lindner
Hello,
http://docs.python.org/lib/module-ConfigParser.html writes:

"with continuations in the style of RFC 822; "

what is meant with these continuations?

Thanks,

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


Re: ConfigParser and multiple option names

2006-05-02 Thread Florian Lindner
Alexis Roda wrote:

> Florian Lindner escribió:
>> I think the best solution would be to use a seperation character:
>> 
>> dir="/home/florian, /home/john, home/whoever"
> 
> RCS uses , in filenames

A kommata (,) is a valid character in path names. Ok, you can use quotes.

>> What do you think? Any better ideas?
> 
> A bit ugly, but probably safer and simpler than adding arbitrary
> separators:
> 
> [section]
> dir_1=/home/florian
> dir_2=/home/john
> dir_3=/home/whoever

I tend to use seperators, because I think it's more common to users. (the
PATH environment variable e.g.)
 
> a s(a|i)mple implementation to give you the idea, it has some bugs:
> 
[...]

Thanks for your input!

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

ConfigParser and multiple option names

2006-05-02 Thread Florian Lindner
Hello,
since ConfigParser does not seem to support multiple times the same option
name, like:

dir="/home/florian"
dir="/home/john"
dir="/home/whoever"

(only the last one is read in)

I wonder what the best way to work around this.

I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"

What character would be best to work on various operating systems? (of what
names may a path consist is the question)

What do you think? Any better ideas?

Thanks,

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


Get path of a class

2006-01-09 Thread Florian Lindner
Hello,
how can I get the path of a class. I managed to do it with

c.__module__ + "." + c.__name__

but I'm sure there is a better way.

Thanks,

Florian

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


Re: Graphical debugger/code explorer

2005-10-04 Thread Florian Lindner
benz wrote:

PYTHON_IDE={
> 'spe' : 'http://spe.pycs.net/',
> 'eric3' : 'http://www.die-offenbachs.de/detlev/eric3.html',
> 'drpython' : 'http://drpython.sourceforge.net/'}

I've tried out eric3 and it looks promising. However, I have one problem. I
open a file which is part of Zope and set a breakpoint. Now I open the
runzope start script and execute it from within eric. This runzope script
calles (after running through a larger call-tree) the function where I set
the breakpoint. But execution is not stopped there Why?

Thanks,

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


Graphical debugger/code explorer

2005-10-03 Thread Florian Lindner
Hello,
in order to understand python code from a larger project (Zope 3) I'm
looking for a tool that helps me with that. It should also help 
What (graphical) application running on Linux can you recommend?

Thanks,

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


Monitoring a directory for changes

2005-09-20 Thread Florian Lindner
Hello,
is there a python lib (preferably in the std lib) to monitor a directory for
changes (adding / deleting files) for Linux 2.6?

Thanks,

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


Re: Python for Webscripting (like PHP)

2005-08-19 Thread Florian Lindner
Florian Lindner wrote:

> Hello,
> I've been using Python a lot for scripting (mainly scripts for server
> administration / DB access). All these scripts were shell based.
> 
> Now I'm considering using Python (with mod_python on Apache 2) for a web
> project, just how I've used PHP in some smaller Projects before ( print "foo" ?>)..
> 
> How suitable is Python for these kind of projects? What do think? Does the
> stdlib offers all basic functions for this kind of requirements?

An email I got from Dan Richter. Since he has problems with his news/mail
gateway I forward it with his permission for the benefit of others.

Florian

- - -

Python is great for "heavy lifting": when most of the work is done
behind the scenes and outputting the HTML is relatively trivial. An
example would be a program that searches archives or computes
derivatives.

But PHP is designed for web pages and is quite powerful. If you can
reasonably do a job in PHP, you probably should. Web sites written in
Python usually involve lots of statements like these:
  uri = os.environ['HTTP_URI']
  print '' + theTitle + ''
  print '''
  The answer to your question
  After lots of computing, here's what 
 we discovered.'''
And so on. As you can see, PHP allows you to embed HTML much more
gracefully, as well do other web-like things such as retrieve URL query
string parameters more easily. So PHP is preferable for most web sites.

Depending on what you want to do, you might also consider Perl and Java
Servlets.

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


Python for Webscripting (like PHP)

2005-08-18 Thread Florian Lindner
Hello,
I've been using Python a lot for scripting (mainly scripts for server
administration / DB access). All these scripts were shell based.

Now I'm considering using Python (with mod_python on Apache 2) for a web
project, just how I've used PHP in some smaller Projects before ()..

How suitable is Python for these kind of projects? What do think? Does the
stdlib offers all basic functions for this kind of requirements?

Thanks,

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


Problem with sha.new

2005-07-09 Thread Florian Lindner
Hello,
I try to compute SHA hashes for different files:


for root, dirs, files in os.walk(sys.argv[1]):
for file in files:
path =  os.path.join(root, file)
print path
f = open(path)
sha = sha.new(f.read())
sha.update(f.read())
print sha.hexdigest()


this generates a traceback when sha.new() is called for the second time:

/home/florian/testdir/testfile
c95ad0ce54f903e1568facb2b120ca9210f6778f
/home/florian/testdir/testfile2
Traceback (most recent call last):
  File "duplicatefinder.py", line 11, in ?
sha = sha.new(f.read())
AttributeError: new

What is wrong there?

Thanks,

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


directory traverser

2005-07-09 Thread Florian Lindner
Hello,
IIRC there is a directory traverser for walking recursively through
subdirectories in the standard library. But I can't remember the name and
was unable to find in the docs.
Anyone can point me to it?

Thanks,

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


Re: What are the other options against Zope?

2005-07-03 Thread Florian Lindner
Peter Hansen wrote:

> Florian Lindner wrote:
>> Peter Hansen wrote:
>>>[Zope] doesn't include
>>>database interfaces other than to its own ZODB.
>> 
>> That's not correct. Zope2 includes DB interfaces to MySQL, PostGre, ODBC
>> and many others.
> 
> It actually *includes* them?  I thought those were all add-in modules,
> not ones that Zope actually installs.  (I admit it's been a couple of
> years since I was current with the state of Zope... and my memory sucks.
> :-( )

Ok, you're right. But I don't really think it makes a difference to install
them afterwards.

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


Re: What are the other options against Zope?

2005-07-02 Thread Florian Lindner
Peter Hansen wrote:

> godwin wrote:
>>   I wanna thank Martin for helping out with my ignorance concerning
>> execution of stored procedure with python. Now i have decided to write
>> a web app that googles into my companies proprietary database.
> 
> Just checking... do you really mean "googles", or is that in your mind a
> synonym for "search"?
> 
>> I need
>> to know whether zope is good for that job.
> 
> For which part of it?  The web part, or the searching part?  It's not
> likely sufficient for the searching part, since it doesn't include
> database interfaces other than to its own ZODB.  What proprietary
> database is involved?

That's not correct. Zope2 includes DB interfaces to MySQL, PostGre, ODBC and
many others.
For Zope3 IIRC there are not yet so many interfaces.

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


key - key pairs

2005-06-23 Thread Florian Lindner
Hello,
is there in python a kind of dictionary that supports key - key pairs?
I need a dictionary in which I can access a certain element using two
different keys, both unique.

For example:

I've a dictionary with strings and times. Sometimes I have the string and I
want to have the time, other time I've the time and I want the string. It
is important that one of the keys supports the min/max builtin function.

Thanks,

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


Optimize a cache

2005-06-22 Thread Florian Lindner
Hello,
I am building a object cache in python, The cache has a maximum size and the
items have expiration dates.
At the moment I'm doing like that:

cache = {} # create dictionary

cache[ID] = (object, timestamp) # Save a tuple with the object itself and a
timestamp (from datetime.datetime.now()) under a unique object ID. This
make looking up a certain item a cheap operation (AFAIK). After looking up
my program checks if the expiration date and returns cache[ID][1] or
retrieve a new object and overwrite the one saved in the cache.

My problem now:
When len(cache) > cacheSize I need to remove the oldest objects (the ones
with the smalest timestamp). For that I need to iterate through all items
and get the oldest one: (pseudo code)

oldest = datetime.datetime.now()

for i in cache:
if i[1] < a:
id = i
n = i[0]

del cache[id]


What possible you see to optimize this lookup? Or anything else you see to
make it better?

Thanks,

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


timedelta comparision with gmtime()

2005-06-22 Thread Florian Lindner
Hello,
I want to know if a certain duration is over.
I try it like this with timedelta objects:

d = datetime.timedelta(minutes = 2)
t = time.gmtime()
print (t + d < time.gmtime())

gives:

TypeError: unsupported operand type(s) for +: 'datetime.timedelta' and
'time.struct_time'

How to do that right?

Thanks,

Florian


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


Escape spaces in strings

2005-05-12 Thread Florian Lindner
Hello,
is there a function to escape spaces and other characters in string for
using them as a argument to unix command? In this case rsync
(http://samba.anu.edu.au/rsync/FAQ.html#10)

Thx,

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


Re: Problems with csv module

2005-05-11 Thread Florian Lindner
Richie Hindle wrote:

> 
> [Florian]
>> You mean that csv.reader can't work with unicode as the delimiter
>> parameter?
> 
> Exactly.  http://www.python.org/doc/2.3.5/lib/module-csv.html says:
> 
> "Note: This version of the csv module doesn't support Unicode input. Also,
> there are currently some issues regarding ASCII NUL characters.
> Accordingly, all input should generally be printable ASCII to be safe.
> These restrictions will be removed in the future. "
> 
> That note is still there in the current development docs, so it looks like
> it hasn't yet been fixed.

Uhh.. thanks!

How can I convert Unicode to ASCII?

Thx,

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


Re: Problems with csv module

2005-05-11 Thread Florian Lindner
Richie Hindle wrote:

> 
> [Florian]
>> I've one problem using the csv module.
>> The code:
>> 
>> self.reader = csv.reader(f, delimiter = ",")
>> 
>> works perfectly. But when I use a variable for delimiter:
>> 
>> self.reader = csv.reader(f, delimiter = Adelimiter)
>> 
>> I get the traceback:
>> 
>> 
>> File "/home/florian/visualizer/ConfigReader.py", line 13, in __init__
>> self.reader = csv.reader(f, delimiter = Adelimiter)
>> TypeError: bad argument type for built-in operation
> 
> Is this your problem?:
> 
 Adelimiter = u','
 reader = csv.reader(f, delimiter=Adelimiter)
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: bad argument type for built-in operation
 print type(Adelimiter)
> 

Yes, thats my problem.

You mean that csv.reader can't work with unicode as the delimiter parameter?
Sorry, I don't really get your point what you're saying...

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


Problems with csv module

2005-05-11 Thread Florian Lindner
Hello,
I've one problem using the csv module.
The code: 

self.reader = csv.reader(f, delimiter = ",")

works perfectly. But when I use a variable for delimiter:

self.reader = csv.reader(f, delimiter = Adelimiter)

I get the traceback:


File "/home/florian/visualizer/ConfigReader.py", line 13, in __init__
self.reader = csv.reader(f, delimiter = Adelimiter)
TypeError: bad argument type for built-in operation


The command

print "Adelimiter: ", Adelimiter, len(Adelimiter)

prints

Adelimiter:  , 1

So I think Adelimiter is ok?!

What is wrong there?

It is Python 2.3.5.

Thx,

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


bad argument type for built-in operation

2005-05-10 Thread Florian Lindner
Hello,
I've the traceback:

Traceback (most recent call last):
  File "visualizer.py", line 8, in ?
main()
  File "visualizer.py", line 5, in main
g = GraphCreator(f)
  File "/home/florian/visualizer/GraphCreator.py", line 13, in __init__
self.conf = ConfigReader(config)
  File "/home/florian/visualizer/ConfigReader.py", line 53, in __init__
graph.sourceReader = CSVReader(filename, firstline, delimiter)
  File "/home/florian/visualizer/ConfigReader.py", line 13, in __init__
self.reader = csv.reader(f, delimiter=Adelimiter)
TypeError: bad argument type for built-in operation

f ist file object, Adelimiter is ",".

What is wrong there?

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


Getting number of iteration

2005-05-06 Thread Florian Lindner
Hello,
when I'm iterating through a list with:

for x in list:

how can I get the number of the current iteration?

Thx,

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


Variable option count

2005-05-04 Thread Florian Lindner
Hello,
how can I give an arbitrary number of options in a automated way to a
function?

Example.

I've the list A =

[
1
2
3
...
]

Now I want to give this list to a function so that it is the same for
function like:

f(1, 2, 3, ...)

How can I do that?

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


Problem with pyXML DOM

2005-05-04 Thread Florian Lindner
Hello,
I'm using the PyXML Package.

My XML document looks like that:




/home/florian/visualizer/testdata.csv





print sourceNode.getElementsByTagName("filename")[0]
print sourceNode.getElementsByTagName("filename")[0].nodeValue()

gives:



Traceback (most recent call last):
  File "ConfigReader.py", line 40, in ?
c = ConfigReader(f)
  File "ConfigReader.py", line 32, in __init__
print sourceNode.getElementsByTagName("filename")[0].nodeValue()
TypeError: 'NoneType' object is not callable

Given only the first call everything seems fine, I have a Node object for
the filename.
But why does the second call fails?

The documentationhttp://pyxml.sourceforge.net/topics/howto/node20.html says:


nodeValue
Value of this node. For some types of node, such as Text nodes, the value is
a string containing a chunk of textual data; for others, such as Text, the
value is just None.

Which I don't really understand? What is differencee between the two text
nodes??

Thanks,

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



Re: Problem with pyXML DOM

2005-05-04 Thread Florian Lindner
Maniac wrote:

> Florian Lindner wrote:
> 
>>Traceback (most recent call last):
>>  File "ConfigReader.py", line 40, in ?
>>c = ConfigReader(f)
>>  File "ConfigReader.py", line 32, in __init__
>>print sourceNode.getElementsByTagName("filename")[0].nodeValue()
>>TypeError: 'NoneType' object is not callable
>>  
>>
> This is because nodeValue here is 'None' and 'None()' doesn't make
> sense. Looks like you want the thing between  and .
> This is not a nodeValue, this is nodeValue of firstChild of element
> 'filename'. So this should work:
> 
> print sourceNode.getElementsByTagName("filename")[0].firstChild.nodeValue

Ok, works perfect. Thanks!

But I don't really understand the logic:

given I have the node A

path

A.firstChild.nodeValue == path

How would the second child of A look like? (ok, None in this case) How would
a XML fragment look like that has a second child?

What would be the nodeValue of A?

for me a child of a node is something like


  
  



Thanks,

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


Libraries for creating graphs

2005-05-02 Thread Florian Lindner
Hello,
I'm looking for libraries to create graphs. I'm not talking about plotting a
function like f(x)=x^2 just plotting a couple of values into a short, maybe
interpolating them. Output should be something like JPEG.

Thx,

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


Comparision of GUI framworks

2005-05-02 Thread Florian Lindner
Hello,
I've read the chapter in the Python documentation, but I'm interested in a a
more in-depth comparision. Especially regarding how pythonic it is and how
well it performs and looks under Windows.
I've some C++ experiences with Qt, so I'm very interested to have PyQt
compared to wxWindows and Tk. How fast does PyQt catches up with the
versiones released from Trolltech? etc..

Thx,
Florian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Secure scripts variables

2005-03-30 Thread Florian Lindner
Paul Rubin wrote:

> Florian Lindner <[EMAIL PROTECTED]> writes:
>> I have a script which is readable and executable by a user, but not
>> writable.
>> The users executes the scripts, it reads in a value and based on this
>> value it computes a result and stores it in a variable.
>> Can the user read out the value of this variable? If yes, can he be
>> prevented to do so?
> 
> I don't really understand the question.  The user could, for example,
> run the Python interpreter under a debugger, and examine its internal
> state step by step during execution.
> 
> What you really want is a setuid script.  That can do what you want,
> but you have to write them very carefully.

AFAIK scripts can't be setuid? Can you tell me what you mean and how to do
it?

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


Secure scripts variables

2005-03-29 Thread Florian Lindner
Hello,
given the following situation:

I have a script which is readable and executable by a user, but not
writable.
The users executes the scripts, it reads in a value and based on this value
it computes a result and stores it in a variable.
Can the user read out the value of this variable? If yes, can he be
prevented to do so?

(It's a ordinary user on a Linux system with access to the python
interpreter.)

(Of course: He could just copy the script to a file he has write access and
modify it to print the result. It's a theoretical situation.)

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


Re: Save passwords in scripts

2005-03-28 Thread Florian Lindner
Serge Orlov wrote:

> Florian Lindner wrote:
>> Paul Rubin wrote:
>>
>>> - sort of similar: have a separate process running that knows the
>>> password (administrator enters it at startup time).  That process
>>> listens on a unix socket and checks the ID of the client.  It reveals
>>> the password to authorized clients, i.e. your readable script running
>>> under sudo.  This keeps the password from ever being stored on disk.
>>>
>>> - Modify the script itself to run as a long-running service instead
>>> of as something that gets started and restarted all the time.  Have
>>> an admin start it and type the password into it at startup time.
>>> Users then connect to it (maybe with a web browser) and send it
>>> commands.
>>>
>>> - Move the user operations from the script to server side database
>>> procedures that do their own validity checking.  Then you don't need
>>> a password.
>>
>> I'll evaluate the 3 ideas above further.
> 
> I'm surprised there are no building blocks for a sudo replacement
> in the UNIX world, at least I googled and couldn't find them.
> Basically you need to split you script into two parts: priveledged
> server and user client. They can talk xml-rpc over unix socket.

Can I find out the identity of the client (PID/UID) when using unix socket? 

> If you need performance you can also open another socket
> for sending huge binary objects.
> 
> With regards to clear text password and admin, you can only
> obfuscate or make it hard to obtain the password. It's just to
> keep honest admins honest. Same story on windows, btw.
> 
>   Serge.

Florian

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


Re: Save passwords in scripts

2005-03-21 Thread Florian Lindner
Paul Rubin wrote:

> Florian Lindner <[EMAIL PROTECTED]> writes:
>> I've a scripts that allows limited manipulation of a database to users.
>> This script of course needs to save a password for the database
>> connection. The users, on the other hand need read permission on the
>> script in order to execute it but should not be able to read out the
>> password. What is the common way to solve this problem?
>> 
>> My current way is to allow the users to execute the script with sudo
>> while not having read permission when acting as a ordinary user. But I
>> don't like this solutions and consider it very ugly.
> 
> There's not a one-size-fits-all answer.  A bunch of possibilities:
> 
> - Just have execute permission on the script, not read permission

This does not work. In ordner to execute the interpreter have to read the
script.

[EMAIL PROTECTED] ~/python $ ./account.py
/usr/bin/python: can't open file './account.py'

Or you know a way it works?
 
> - If the database server and client are running on the same machine,
> use a unix-domain socket instead of a tcp socket, and modify the
> server to check that only a specific uid is running the client (you
> can do this check with an ancillary message on the socket).  Then use
> sudo to get the client to run as that user.  You can then leave read
> permission enabled on the script.

This a bit overkill for my needs.
 
> - sort of similar: have a separate process running that knows the
> password (administrator enters it at startup time).  That process
> listens on a unix socket and checks the ID of the client.  It reveals
> the password to authorized clients, i.e. your readable script running
> under sudo.  This keeps the password from ever being stored on disk.
> 
> - Modify the script itself to run as a long-running service instead of
> as something that gets started and restarted all the time.  Have an
> admin start it and type the password into it at startup time.  Users
> then connect to it (maybe with a web browser) and send it commands.
> 
> - Move the user operations from the script to server side database
> procedures that do their own validity checking.  Then you don't need a
> password.

I'll evaluate the 3 ideas above further.

> - Run the script on a machine where users can't run arbitrary programs
> other than the script.  Set up the db server to not accept any
> connections other than from that machine.

Not possible here.

Thanks for your suggestions,

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


Re: Save passwords in scripts

2005-03-21 Thread Florian Lindner
Esben Pedersen wrote:

> Florian Lindner wrote:
>> Hello,
>> I've a scripts that allows limited manipulation of a database to users.
>> This script of course needs to save a password for the database
>> connection. The users, on the other hand need read permission on the
>> script in order to execute it but should not be able to read out the
>> password. What is the common way to solve this problem?
>> 
>> My current way is to allow the users to execute the script with sudo
>> while not having read permission when acting as a ordinary user. But I
>> don't like this solutions and consider it very ugly.
>> 
>> Thanks,
>> Florian
> 
> Which DB? afaik postgre has user-level authentication which means you
> don't even need a password.

But all users are manipulating rows in one table and I need to check to make
sanity checks on the input.

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


Re: Save passwords in scripts

2005-03-21 Thread Florian Lindner
Peter Hansen wrote:

> Florian Lindner wrote:
>> I've a scripts that allows limited manipulation of a database to users.
>> This script of course needs to save a password for the database
>> connection. The users, on the other hand need read permission on the
>> script in order to execute it but should not be able to read out the
>> password. What is the common way to solve this problem?
> 
> The common way is to do something ill-conceived and insecure.
> 
> The correct approach is to use a secure technique that
> does not involve storing the passwords themselves, but
> instead storing a hash version of them (e.g. MD5 or SHA),
> or by requiring the users to enter their passwords at
> the time the information is required.

Hashes could not work, since I need to give the password to a DB server. My
script is the client, not the server. It does not check passwords supplied
by the users, just use the hard-coded password to connect to the DB server.
 
>> My current way is to allow the users to execute the script with sudo
>> while not having read permission when acting as a ordinary user. But I
>> don't like this solutions and consider it very ugly.
> 
> Storing passwords in the clear is always ugly and
> insecure.  Think about the situation where a user
> (unwisely) picks a password that he also uses for,
> say, his online banking.  If the password is stored
> in the clear, then anyone with root access can see
> it and even if you trust all your administrators,
> or are the only admin yourself, it's still not a
> good idea to let an admin see a user's password.

It's not a users password. It's a password of a db user which owns several
system tables and the users should be able to manipulate them in a
constrained manner.

I fully agree with you. That's why I'm looking for a better, more secure
solution.

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


Save passwords in scripts

2005-03-21 Thread Florian Lindner
Hello,
I've a scripts that allows limited manipulation of a database to users. This
script of course needs to save a password for the database connection. The
users, on the other hand need read permission on the script in order to
execute it but should not be able to read out the password.
What is the common way to solve this problem?

My current way is to allow the users to execute the script with sudo while
not having read permission when acting as a ordinary user. But I don't like
this solutions and consider it very ugly.

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


Mark attribute as read-only

2005-03-16 Thread Florian Lindner
Hello,
how can I mark a attribute of a class as read-only (for non classmembers)?
Yes, stupid question, but the docu gave me no help.

Thanks,

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


Lib for RSS/Atom parsing

2005-02-27 Thread Florian Lindner
Hello,
what is a good python library for parsing of RSS and/or Atom feeds. It
should be able to handle most of the common protocols.
Thanks,

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


How to access database?

2005-01-03 Thread Florian Lindner
Hello,
AFAIK python has a generic API for database access which adapters are
supposed to implement. How can I found API documentation on the API?

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


Re: Help with super()

2004-12-07 Thread Florian Lindner
Steven Bethard schrieb:
Christopher J. Bottaro wrote:
Why don't this code work?
import PRI
class Poscdnld_PYIO(PRI.BasicBatch):
  def __init__(self, *argv):
  super(Poscdnld_PYIO, self).__init__(*argv)
x = Poscdnld_PYIO()
I get this exception:
File "poscdnld_pyio.py", line 52, in __init__
super(Poscdnld_PYIO, self).__init__(*argv)
TypeError: super() argument 1 must be type, not classobj
What am I doing wrong?  Thanks.
I don't know what PRI is, but I suspect that PRI.BasicBatch is a classic 
class, not a new-style class.  The super function only works for 
new-style classes:
Never heard of new-stype and classiv-class... What are the differences?
Thx,
Florian
--
http://mail.python.org/mailman/listinfo/python-list


Forums based on python

2004-12-06 Thread Florian Lindner
Hello,
which free forums, based on mod_python and MySQL are around there? 
Something like phpBB...

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


Importing class from file in package

2004-12-04 Thread Florian Lindner
Hello,
I've two files in my package.
In the first file I want to inport a class which is declared in the 
second file. How can do that without stating the absolute path to the 
file, just the relative one?

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