Authentication

2005-04-19 Thread cameron
I am new to Python so please excuse my ignorance.
Are there a nice set of libraries/modules that abstract authentication 
in a platform independent manner? I am working on an application that 
would like to be able to hook into the native authentication mechanisms 
on Linux and OpenBSD. I would like to have a uniform interface and write 
once but I am not really holding my breath.

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


accumulator generators

2008-05-30 Thread Cameron
I was reading this http://www.paulgraham.com/icad.html";>Paul
Graham article and he builds an accumuator generator function in
the appendix. His looks like this:


def foo(n):
  s = [n]
  def bar(i):
s[0] += i
return s[0]
  return bar


Why does that work, but not this:


def foo(n):
  s = n
  def bar(i):
s += i
return s
  return bar

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


Re: accumulator generators

2008-05-30 Thread Cameron
On May 30, 1:04 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> Cameron schrieb:
>
>
>
> > I was reading this http://www.paulgraham.com/icad.html";>Paul
> > Graham article and he builds an accumuator generator function in
> > the appendix. His looks like this:
>
> > 
> > def foo(n):
> >   s = [n]
> >   def bar(i):
> >     s[0] += i
> >     return s[0]
> >   return bar
> > 
>
> > Why does that work, but not this:
>
> > 
> > def foo(n):
> >   s = n
> >   def bar(i):
> >     s += i
> >     return s
> >   return bar
> > 
>
> Because python's static analysis infers s as being a variable local to
> bar in the second case - so you can't modify it in the outer scope.
>
> In the future, you may declare
>
> def bar(i):
>      nonlocal s
>      ...
>
> Diez

thanks for the response. Just to make sure I understand- Is the reason
it works in the first case because s[0] is undefined at that point (in
bar), and so python looks in the outer scope and finds it there?

Cameron

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


Re: Style question -- plural of class name?

2013-05-08 Thread Cameron Simpson
On 09May2013 00:02, Steven D'Aprano  
wrote:
| On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote:
| > "A list of FooEntry's"
| 
| "Here come's an S! Quick, jam on an apostrophe!"
| 
| This is called the grocer's apostrophe, and is universally held in 
| contempt no matter what variant of English you write in. Don't do this.
| 
| The only acceptable use of an apostrophe to make a plural is if the thing 
| being pluralised is a single letter. E.g. one a, two a's.

Frankly, not even then for me. I spell that "one A, two As".

| > "A list of FooEntry instances"
| 
| This is also acceptable, although a little wordy. Do you write "a list of 
| strings" or "a list of str instances"?

How about "a FooEntry list"?
-- 
Cameron Simpson 

Yes Officer, yes Officer, I will Officer. Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: object.enable() anti-pattern

2013-05-09 Thread Cameron Simpson
On 09May2013 19:54, Greg Ewing  wrote:
| Steven D'Aprano wrote:
| >There is no sensible use-case for creating a file without opening
| >it. What would be the point?
| 
| Early unix systems often used this as a form of locking.

Not just early systems: it's a nice lightweight method of making a
lockfile even today if you expect to work over NFS, where not that
many things are synchronous. You open a file with "0" modes, so
that it is _immediately_ not writable. Other attempts to make the
lock file thus fail because of the lack of write, even over NFS.

Cheers,
-- 
Cameron Simpson 

You can listen to what everybody says, but the fact remains that you've got
to get out there and do the thing yourself. - Joan Sutherland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: object.enable() anti-pattern

2013-05-09 Thread Cameron Simpson
On 09May2013 11:30, Steven D'Aprano  
wrote:
| On Thu, 09 May 2013 18:23:31 +1000, Cameron Simpson wrote:
| 
| > On 09May2013 19:54, Greg Ewing  wrote:
| > | Steven D'Aprano wrote:
| > | > There is no sensible use-case for creating a file WITHOUT OPENING
| > | > it. What would be the point?
| > |
| > | Early unix systems often used this as a form of locking.
| > 
| > Not just early systems: it's a nice lightweight method of making a
| > lockfile even today if you expect to work over NFS, where not that many
| > things are synchronous. You OPEN A FILE with "0" modes
| 
| [emphasis added]
| This is all very well and good, but for the life of me, I cannot see how 
| opening a file is a good example of not opening a file. Perhaps it is a 
| Zen thing, like the sound no spoon makes when you don't tap it against a 
| glass that isn't there.

Because a file usually does not exist in isolation (yes sometimes
we want an isolated file). Files usually exist in the filesystem,
which is a namespace. And this is effectively a namespace operation,
not a data storage operation.

Of course, I can take this the other way: just because I opened it
with a 0 mode field doesn't mean _I_, the opener, cannot read/write
it. I've got an open file handle... A race free way to make a scratch
file in a shared area, for example.

The point is probably that a file isn't merely a feature free byte
storage container; in the real world they usually come with all
sorts of features like names and permissions. Those features will
always imply creative uses.

Anyway, this has little to do with your antipattern (about which
I'm not totally convinced anyway unless it is a rule of thumb or
code smell). It might apply to a Platonicly ideal file, but real
files have more than one use case.

Cheers,
-- 
Cameron Simpson 

I just kept it wide-open thinking it would correct itself.
Then I ran out of talent.   - C. Fittipaldi
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: object.enable() anti-pattern

2013-05-09 Thread Cameron Simpson
On 10May2013 10:56, Greg Ewing  wrote:
| Cameron Simpson wrote:
| >You open a file with "0" modes, so
| >that it is _immediately_ not writable. Other attempts to make the
| >lock file thus fail because of the lack of write,
| 
| I don't think that's quite right. You open it with
| O_CREAT+O_EXCL, which atomically fails if the file
| already exists. The read/write modes don't really
| come into it, as far as I know.

Interesting. That is more direct. My understanding that it was
enough to create the file with 0 modes. Maybe either will do. Must
check...

Cheers,
-- 
Cameron Simpson 

Piracy gets easier every day, but listening to legally purchased music gets 
harder by the day.
Firehed - http://politics.slashdot.org/comments.pl?sid=179175&cid=14846089
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: object.enable() anti-pattern

2013-05-10 Thread Cameron Simpson
On 10May2013 09:22, Roy Smith  wrote:
| In article <518cc239$0$29997$c3e8da3$54964...@news.astraweb.com>,
|  Steven D'Aprano  wrote:
| > > int fd = 37;
| > > 
| > > I've just created a file descriptor.  There is not enough information
| > > given to know if it corresponds to an open file or not.
| > 
| > No, you haven't created a file descriptor. You've made up a number which 
| > C will allow you to use as an index into the file descriptor table, 
| > because C is a high-level assembler with very little in the way of type 
| > safety, and what little there is you can normally bypass.
| 
| No, I've created a file descriptor, which is, by definition, an integer. 
| It has nothing to do with C.  This is all defined by the POSIX 
| interface.  For example, the getdtablesize(2) man page says:
| 
| "The entries in the descriptor table are numbered with small integers 
| starting at 0.  The call getdtablesize() returns the size of this table."
[... snip ...]

I'm with Steven here.

You've made a number that can be used with calls that access the
OS file descriptor table. But it isn't a file descriptor. (Yes, the
in-program number is just a number either way.)

The descriptor table is an in-kernel data structure, filled with
file descriptors. All you have is a label that may or may not access
a file descriptor.

Anyway, we all know _what_ goes on. We're just having terminology issues.

Cheers,
-- 
Cameron Simpson 

My computer always does exactly what I tell it to do but sometimes I have
trouble finding out what it was that I told it to do.
- Dick Wexelblat 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Differences of "!=" operator behavior in python3 and python2 [ bug? ]

2013-05-13 Thread Cameron Simpson
On 13May2013 19:22, Dave Angel  wrote:
| On 05/13/2013 06:53 PM, Mark Lawrence wrote:
| >I much prefer the alternative <> for != but some silly people insisted
| >that this be removed from Python3.  Just how stupid can you get?
| 
| So which special methods should the <> operator call?  By rights it
| ought to call both __gt__ and __lt__ and return True if either of
| them is True.

Surely it should require both of them to be true...

Personally I'm for != given we have ==. Aside from notational
consistency it makes conceptual sense for unordered types, which
<> does not really.

Cheers,
-- 
Cameron Simpson 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Differences of "!=" operator behavior in python3 and python2 [ bug? ]

2013-05-13 Thread Cameron Simpson
On 13May2013 21:41, Dave Angel  wrote:
| On 05/13/2013 07:30 PM, Cameron Simpson wrote:
| >On 13May2013 19:22, Dave Angel  wrote:
| >| On 05/13/2013 06:53 PM, Mark Lawrence wrote:
| >| >I much prefer the alternative <> for != but some silly people insisted
| >| >that this be removed from Python3.  Just how stupid can you get?
| >|
| >| So which special methods should the <> operator call?  By rights it
| >| ought to call both __gt__ and __lt__ and return True if either of
| >| them is True.
| >
| >Surely it should require both of them to be true...
| 
| Then it would never be true.  At least not for numbers.

Well that was the point. The _symbol_ looks like it should want
both. Next time I'll include the smiley.

Cheers,
-- 
Cameron Simpson 

I really don't like :-) symbols as well. I feel that if you can't see the pie
coming, you deserve whipped cream up your nose.
- r...@cherry.cray.com (rob derrick)
-- 
http://mail.python.org/mailman/listinfo/python-list


mutable ints: I think I have painted myself into a corner

2013-05-18 Thread Cameron Simpson
TL;DR: I think I want to modify an int value "in place".

Yesterday I was thinking about various "flag set" objects I have
floating around which are essentially bare "object"s whose attributes
I access, for example:

  flags = object()
  flags.this = True
  flags.that = False

and then elsewhere:

  if flags.that:
do that ...

Nice and readable, but I thought to myself: so bulky!

The use case for flags is essentially boolean/binary, and so a int
accessed as a bitmask should be smaller.

So I pulled out my BitMask int subclass (which mostly transcribes
the int as "A|B|C" for readability purposes, partly to dillute Nick
Coglan's liking for bulky strings over compact ints on readability
grounds:-), and gave the subclass attribute access.

This works just fine for querying the flags object, with code exactly
like the "if" statement above.

But setting up a flags object? What I _want_ to write is code like this:

  Flags = BitMask('this', 'that')

  # set default state
  flags = Flags()
  flags.this = False
  flags.that = True
  ... iterate over some options ...: flags.this = True

and there's my problem. This would modify the int in place. There's
no way to do that. For the base type (int) this makes perfect sense,
as they're immutable.

Before I toss this approach and retreat to my former "object"
technique, does anyone see a way forward to modify an int subclass
instance in place? (That doesn't break math, preferably; I don't
do arithmetic with these things but they are, after all, ints...)

Cheers,
-- 
Cameron Simpson 

Why does "philosophy of consciousness/nature of reality" seem to interest you
so much?  Take away consciousness and reality and there's not much left.
- Greg Egan, interview in Eidolon 15
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mutable ints: I think I have painted myself into a corner

2013-05-18 Thread Cameron Simpson
On 19May2013 11:11, Chris Angelico  wrote:
| On Sun, May 19, 2013 at 10:26 AM, Cameron Simpson  wrote:
| > Before I toss this approach and retreat to my former "object"
| > technique, does anyone see a way forward to modify an int subclass
| > instance in place? (That doesn't break math, preferably; I don't
| > do arithmetic with these things but they are, after all, ints...)
| 
| Why is it an int subclass? Because there are places where you want to
| use it as though it were an int? It might be easier to render those,
| instead, eg by creating a __int__ method. (Or is it "an __int__
| method"? Not sure.)

I don't want to use it as an int, on the outside. I want to use an
int on the inside as the implementation.

It's an int _subclass_ so that it is no bigger than an int. Otherwise
I may as well just make an ordinary object and be back where I
started.  Bulky:-(

The reason it is an _int_ subclass, versus something else, is that
a bitmap is a type of int. So the functional mapping is direct.

I _do_ _not_ want to operate on it from the outside as an int (doing
overt addition, for example, though I can imagine doing bitwise
activities); I want to operate on in _internally_ as a int to decide
what names-by-an-attribute flags are on or off.

So an object with an __int__() method is right out; it's the wrong
interface because it would mean an int() call (possibly implicit)
in exterior code.

Cheers,
-- 
Cameron Simpson 

Hoping to shave precious seconds off the time it would take me to get
through the checkout process and on my way home, I opted for the express
line ("9 Items Or Less [sic]"  Why nine items?  Where do they come up with
these rules, anyway?  It's the same way at most stores -- always some
oddball number like that, instead of a more understandable multiple of
five.  Like "five.")
- Geoff Miller, geo...@purplehaze.corp.sun.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Please help with Threading

2013-05-18 Thread Cameron Simpson
On 19May2013 03:02, Carlos Nepomuceno  wrote:
| Just been told that GIL doesn't make things slower, but as I
| didn't know that such a thing even existed I went out looking for
| more info and found that document:
| http://www.dabeaz.com/python/UnderstandingGIL.pdf
| 
| Is it current? I didn't know Python threads aren't preemptive.
| Seems to be something really old considering the state of the art
| on parallel execution on multi-cores.
| What's the catch on making Python threads preemptive? Are there any ongoing 
projects to make that?

Depends what you mean by preemptive. If you have multiple CPU bound
pure Python threads they will all get CPU time without any of them
explicitly yeilding control. But thread switching happens between
python instructions, mediated by the interpreter.

The standard answers for using multiple cores is to either run
multiple processes (either explicitly spawning other executables,
or spawning child python processes using the multiprocessing module),
or to use (as suggested) libraries that can do the compute intensive
bits themselves, releasing the while doing so so that the Python
interpreter can run other bits of your python code.

Plenty of OS system calls (and calls to other libraries from the
interpreter) release the GIL during the call. Other python threads
can run during that window.

And there are other Python implementations other than CPython.

Cheers,
-- 
Cameron Simpson 

Processes are like potatoes.- NCR device driver manual
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to run a python script twice randomly in a day?

2013-05-19 Thread Cameron Simpson
On 19May2013 20:54, Avnesh Shakya  wrote:
|How to run a python script twice randomly in a day? Actually
| I want to run my script randomly in a day and twice only. Please
| help me.. how is it possible.

Do you mean "run twice a day, each at random times"?

If so, do the obvious: at midnight, pick two random times. Sleep
until the first time, run the script, sleep until the second time,
run the script.

There are various ways to do the sleeping and midnight bits; they're
up to you.

Enjoy,
-- 
Cameron Simpson 

The ZZR-1100 is not the bike for me, but the day they invent "nerf" roads
and ban radars I'll be the first in line..AMCN
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mutable ints: I think I have painted myself into a corner

2013-05-19 Thread Cameron Simpson
On 19May2013 09:01, Peter Otten <__pete...@web.de> wrote:
| Cameron Simpson wrote:
| 
| > TL;DR: I think I want to modify an int value "in place".
| > 
| > Yesterday I was thinking about various "flag set" objects I have
| > floating around which are essentially bare "object"s whose attributes
| > I access, for example:
| > 
| >   flags = object()
| >   flags.this = True
| >   flags.that = False
| > 
| > and then elsewhere:
| > 
| >   if flags.that:
| > do that ...
| > 
| > Nice and readable, but I thought to myself: so bulky!
| 
| Plus, it doesn't work:

Yeah, sorry. My "old" code was a trite subclass of object.
The new broken code is a subclass of int.

| > But setting up a flags object? What I _want_ to write is code like this:
| > 
| >   Flags = BitMask('this', 'that')
| > 
| >   # set default state
| >   flags = Flags()
| >   flags.this = False
| >   flags.that = True
| >   ... iterate over some options ...: flags.this = True
| > 
| > and there's my problem. This would modify the int in place. There's
| > no way to do that. For the base type (int) this makes perfect sense,
| > as they're immutable.
| > 
| > Before I toss this approach and retreat to my former "object"
| > technique, does anyone see a way forward to modify an int subclass
| > instance in place? (That doesn't break math, preferably; I don't
| > do arithmetic with these things but they are, after all, ints...)
| 
| No, but you could make
| 
| flags = Flags(this=False, that=True)

Yes, that is true. Flags would need to be a factory function computing
the corresponding bitmask value and then returning my new int, but
it would work. It still wouldn't let me go the whole way of modifying
the flags after instantiation.

Cheers,
-- 
Cameron Simpson 

Carpe Daemon - Seize the Background Process
- Paul Tomblin 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mutable ints: I think I have painted myself into a corner

2013-05-19 Thread Cameron Simpson
On 20May2013 13:23, Greg Ewing  wrote:
| Cameron Simpson wrote:
| >It's an int _subclass_ so that it is no bigger than an int.
| 
| If you use __slots__ to eliminate the overhead of an
| instance dict, you'll get an object consisting of a
| header plus one reference, which is probably about the
| size of an int. But you'll also need an int to put in
| that slot, so the total size will be about twice that
| of an int.

Yeah, I was thinking I'd need to go that way. Thanks for the
suggestion.

| Another approach would be to subclass array.array and
| give instances of it type integer and size 1. Together
| with empty __slots__, it will probably be a bit bigger
| than an int, but it might still be smaller than a
| custom object plus an int.

Really? Interesting. I thinik it crosses my "too baroque" threshold,
but maybe not:-)

| If all of these are still too big, you might need to
| find some way of packing multiple instances into a
| single array.array.

Space isn't that real an issue at present; I'll keep that kind of
approach in mind if it comes up. This really came up because I was
feeling that the obvious object-with-boolean-attributes was terrbily
wasteful for something that can be inplemented with a single int,
in principle.

Cheers,
-- 
Cameron Simpson 

>>>How do you blip the throttle and wave? Do you blip it real high, then wave
>>>before the revs drop back?
>>Blip = right hand; Wave = left hand.  Do both simultaneously.  QED.
>Doesnt this make the bike lurch forward thru the intersection?
Not if the disk lock is in place...
- Dean Woodward 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to run a python script twice randomly in a day?

2013-05-20 Thread Cameron Simpson
On 20May2013 09:47, Avnesh Shakya  wrote:
| On Mon, May 20, 2013 at 9:42 AM, Cameron Simpson  wrote:
| > On 19May2013 20:54, Avnesh Shakya  wrote:
| > |How to run a python script twice randomly in a day? Actually
| > | I want to run my script randomly in a day and twice only. Please
| > | help me.. how is it possible.
| >
| > Do you mean "run twice a day, each at random times"?
| >
| > If so, do the obvious: at midnight, pick two random times. Sleep
| > until the first time, run the script, sleep until the second time,
| > run the script.
| >
| > There are various ways to do the sleeping and midnight bits; they're
| > up to you.
|
| Thanks, Can you mail documentation or link for it? I am totally new for it.

1: Please reply on-list; you asked the list, the discussion should
   remain there. I have added the list to the CC line.

2: Please don't top-post. Quote only the relevant bits of the
   previous message and reply below. If the message is long, do
   that in pieces. [quote] reply [quote] reply. Like a conversation.

Now, to your questions.

A UNIX user would use "cron" to schedule the midnight job and from
the midnight job then probably use "at" to schedule the other jobs
to run at specific times. See "man 1 crontab", "man 5 crontab" to
submit the cron jobs and "man at" to submit the once off jobs.

UNIX means Linux, Solaris, AIX, any of the BSDs (includes MacOSX),
etc.

You could a small python script for the midnight job, and it would
submit the "at" jobs. See the "random" module to compute a pseudorandom
number (and thus a random time), the "datetime" module to compute
the dates and times from that number, and the "subprocess" module
to submit the "at" job for the chosen run times. The modules are
here:

  http://docs.python.org/3/py-modindex.html

That presumes python 3; if you're using python 2 the docs for that
are available at the same web site.

You don't need to use "at". Your midnight job could just compute
the time in seconds to each job time, then use the "time.sleep"
function to delay until then. See the "time" module at the above
link.

Nobody on this list will write your program for you.

Attempt to write the program, then come to the list with your attempt
and any problems. People will help if you've made an initial effort,
and continue to try. This question has the "feel" of a homework
question; the point of homework is for you to learn by solving a
problem yourself. It is fine to seek help, but you must make the
effort yourself.

Come back with specific questions.

Cheers,
--
Cameron Simpson 

I'm not weird; I'm gifted.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Please help with Threading

2013-05-20 Thread Cameron Simpson
On 20May2013 07:25, Fábio Santos  wrote:
| On 18 May 2013 20:33, "Dennis Lee Bieber"  wrote:
| > Python threads work fine if the threads either rely on intelligent
| > DLLs for number crunching (instead of doing nested Python loops to
| > process a numeric array you pass it to something like NumPy which
| > releases the GIL while crunching a copy of the array) or they do lots of
| > I/O and have to wait for I/O devices (while one thread is waiting for
| > the write/read operation to complete, another thread can do some number
| > crunching).
| 
| Has nobody thought of a context manager to allow a part of your code to
| free up the GIL? I think the GIL is not inherently bad, but if it poses a
| problem at times, there should be a way to get it out of your... Way.

The GIL makes individual python operations thread safe by never
running two at once. This makes the implementation of the operations
simpler, faster and safer. It is probably totally infeasible to
write meaningful python code inside your suggested context
manager that didn't rely on the GIL; if the GIL were not held the
code would be unsafe.

It is easy for a C extension to release the GIL, and then to do
meaningful work until it needs to return to python land. Most C
extensions will do that around non-trivial sections, and anything
that may stall in the OS.

So your use case for the context manager doesn't fit well.
-- 
Cameron Simpson 

Gentle suggestions being those which are written on rocks of less than 5lbs.
- Tracy Nelson in comp.lang.c
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Please help with Threading

2013-05-20 Thread Cameron Simpson
On 20May2013 10:53, Carlos Nepomuceno  wrote:
| I just got my hands dirty trying to synchronize Python prints from many 
threads.
| Sometimes they mess up when printing the newlines. 
| I tried several approaches using threading.Lock and Condition.
| None of them worked perfectly and all of them made the code sluggish.

Show us some code, with specific complaints.

Did you try this?

  _lock = Lock()

  def lprint(*a, **kw):
global _lock
with _lock:
  print(*a, **kw)

and use lprint() everywhere?

For generality the lock should be per file: the above hack uses one
lock for any file, so that's going to stall overlapping prints to
different files; inefficient.

There are other things than the above, but at least individual prints will
never overlap. If you have interleaved prints, show us.

| Is there a 100% sure method to make print thread safe? Can it be fast???

Depends on what you mean by "fast". It will be slower than code
with no lock; how much would require measurement.

Cheers,
-- 
Cameron Simpson 

My own suspicion is that the universe is not only queerer than we suppose,
but queerer than we *can* suppose.
- J.B.S. Haldane "On Being the Right Size"
  in the (1928) book "Possible Worlds"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Please help with Threading

2013-05-20 Thread Cameron Simpson
On 20May2013 19:09, Chris Angelico  wrote:
| On Mon, May 20, 2013 at 6:35 PM, Cameron Simpson  wrote:
| >   _lock = Lock()
| >
| >   def lprint(*a, **kw):
| > global _lock
| > with _lock:
| >   print(*a, **kw)
| >
| > and use lprint() everywhere?
| 
| Fun little hack:
| 
| def print(*args,print=print,lock=Lock(),**kwargs):
|   with lock:
| print(*args,**kwargs)
| 
| Question: Is this a cool use or a horrible abuse of the scoping rules?

I carefully avoided monkey patching print itself:-)

That's... mad! I can see what the end result is meant to be, but
it looks like a debugging nightmare. Certainly my scoping-fu is too
weak to see at a glance how it works.
-- 
Cameron Simpson 

I will not do it as a hack   I will not do it for my friends
I will not do it on a MacI will not write for Uncle Sam
I will not do it on weekends I won't do ADA, Sam-I-Am
- Gregory Bond 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to run a python script twice randomly in a day?

2013-05-20 Thread Cameron Simpson
On 20May2013 15:05, Avnesh Shakya  wrote:
| Thanks a lot.

No worries, but ...

AGAIN:
- please DO NOT top post. Post below, trimming the quoted material.
- please POST TO THE LIST, not just to me. This is a public discussion.

Now...

| I did something.
| I have created test.sh file in which i put-
| 
| #!/bin/bash
| cd /home/avin/cronJob
| python try.py

Ok, good. Some minor remarks:

Personally, I always use:

  #!/bin/sh

instead of requiring bash. All UNIX systems have sh, bash is only
common. And even when present, it may not be in /bin. /bin/sh is
always there, and unless you're doing something quite unusual, it
works just fine.

| then i went on terminal -
| and run crontab -e
| and wrote-
|   */2 * * * * bash /home/avin/cronJob/test.sh
| and saved it.

IIRC, this runs every two minutes. Good for testing, but not your original spec.

Also, if you make the shell script (test.sh) executable you do not
need to specify the interpreter. Treat your script like any other
command! So:

  chmod +rx /home/avin/cronJob/test.sh

and then your cron line can look like this:

  */2 * * * * /home/avin/cronJob/test.sh

Also, treat your script the same way as your shell script, start
it with a #!  like this:

  #!/usr/bin/python

Make it executable:

  chmod +rx /home/avin/cronJob/try.py

and then you don't need to say "python" in your shell script:

  ./try.py

(You need the ./ because the current directory is not in your command
search path ($PATH).)

| It's working fine.
| but when I m using like
| 
| import random
| a = random.randrange(0, 59)
| */a * * * * bash /home/avin/cronJob/test.sh
| then it's showing error becose of varable 'a', so now how can i take
| variable?

I take it that this is your python program intended to schedule the two 
randomly timed runs?

As a start, it must all be python. The first two lines are. The third line is
a crontab line.

So as a start, you need to look more like this:

  #!/usr/bin/python
  import random
  a = random.randrange(0, 59)
  cronline = '*/%d * * * * /home/avin/cronJob/test.sh' % (a,)
  print(cronline)

At least then you can see the cron line you're making. It still
does not add it to a cron job.

Some remarks:

- randrange() is like other python ranges: it does not include the end value.
  So your call picks a number from 0..58, not 0..59.
  Say randrange(0,60). Think "start, length".

- My recollection is that you wanted to run a script twice a day at random 
times.
  Your cron line doesn't do that.

- If you're picking random run times you want to schedule a once-off
  job for each to run at a particular times. Cron schedules repeating
  jobs.  To run at a particular time you want an "at" job.

- You need to do one of two things in the pick-a-time script:
- pick a time, then sleep until that time and then directly
  invoke the try.py script
  or
- pick a time, then use the "at" command to schedule the try.py
  (or test.sh) script.

The first approach would look a bit like this (totally untested):

  #!/usr/bin/python
  import random
  import subporcess
  import time
  # choose range time in the next 24 hours
  when = random.randrange(0, 24 * 3600)
  # sleep that many seconds
  time.sleep(when)
  subprocess.call(['/home/avin/cronJob/test.sh'])

For two runs, pick two times. Swap them into order. Sleep twice,
once until the first time and then once until the second time. Etc.

The second approach (using "at") would not sleep. instead, compute
(using the datetime module) the date and time each job should run,
and invoke "at" using the subprocess module, piping the text
"/home/avin/cronJob/test.sh\n" to it.

Cheers,
-- 
Cameron Simpson 

On a related topic, has anyone looked at doing a clean-room copy of CSS
a la RC2 and RC4 a few years back?  I know one or two people have
looked at this in an informal manner, but we couldn't find anyone who
hadn't already seen the DeCSS code to act as the clean person (it says
a lot for the status of their "trade secret" that we couldn't actually
find anyone who didn't already know  it).
- Peter Gutmann 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to run a python script twice randomly in a day?

2013-05-21 Thread Cameron Simpson
On 21May2013 17:56, Chris Angelico  wrote:
| On Tue, May 21, 2013 at 11:12 AM, Cameron Simpson  wrote:
| > - randrange() is like other python ranges: it does not include the end 
value.
| >   So your call picks a number from 0..58, not 0..59.
| >   Say randrange(0,60). Think "start, length".
| 
| Nitpick: It's not start, length; it's start, stop-before. If the start
| is 10 and the second argument is 20, you'll get numbers from 10 to 19.
| But your conclusion is still accurate :)

But it's still a useful thing to think when you're trying to reason
about ranges unless you're doing something unusual.

Cheers,
-- 
Cameron Simpson 

Life IS pain, highness...  anyone who tries to tell you different is
trying to sell you something.   - Wesley, The_Princess_Bride
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to run a python script twice randomly in a day?

2013-05-21 Thread Cameron Simpson
On 21May2013 09:54, Dave Angel  wrote:
| On 05/21/2013 06:32 AM, Cameron Simpson wrote:
| >On 21May2013 17:56, Chris Angelico  wrote:
| >| On Tue, May 21, 2013 at 11:12 AM, Cameron Simpson  wrote:
| >| > - randrange() is like other python ranges: it does not include the end 
value.
| >| >   So your call picks a number from 0..58, not 0..59.
| >| >   Say randrange(0,60). Think "start, length".
| >|
| >| Nitpick: It's not start, length; it's start, stop-before. If the start
| >| is 10 and the second argument is 20, you'll get numbers from 10 to 19.
| >| But your conclusion is still accurate :)
| >
| >But it's still a useful thing to think when you're trying to reason
| >about ranges unless you're doing something unusual.
| 
| No, it's only happens to look like length when start is zero.  So as
| a mnemonic, it's highly misleading.

Feh! No self respecting computer scientist would ever count from
other than zero!

Actually, yes, you're right there.

Cheers,
-- 
Cameron Simpson 

Q: How does a hacker fix a function which doesn't work for all of the elements 
in its domain?
A: He changes the domain.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: This mail never gets delivered. Any ideas why?

2013-05-26 Thread Cameron Simpson
On 26May2013 13:48, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| I'am receiving this now after some tries:
| 
| A problem occurred in a Python script. Here is the sequence of function calls 
leading up to the error, in the order they occurred.
| 
|  /home/nikos/public_html/cgi-bin/metrites.py in ()
| 139 else:
| 140 sp = subprocess.Popen(['mail', '-f', FROM, '-s', 
'Mail from Guest', 'supp...@superhost.gr'], stdin=subprocess.PIPE)
| =>  141 sp.communicate( MESSAGE )
| 142 status = sp.wait()
| 143 if status:
| sp = , sp.communicate = >, MESSAGE = 
'kdsjfksdjkfjksdjfs\r\n\t'
|  /opt/python3/lib/python3.3/subprocess.py in 
communicate(self=, input='kdsjfksdjkfjksdjfs\r\n\t', 
timeout=None)
| 901 if input:
| 902 try:
| =>  903 self.stdin.write(input)

Well, you should probably be writing to sp.stdin, not sys.stdin.

| 904 except IOError as e:
| 905 if e.errno != errno.EPIPE and e.errno != 
errno.EINVAL:
| self = , self.stdin = <_io.BufferedWriter name=5>, 
self.stdin.write = , input 
= 'kdsjfksdjkfjksdjfs\r\n\t'
| TypeError: 'str' does not support the buffer interface 
|   args = ("'str' does not support the buffer interface",) 
|   with_traceback = 

This is symptomatic of sys.stdin (well, whatever you're writing to)
being open in binary mode instead of text mode. And you're passing
a str. Try passing std.encode().

Cheers,
-- 
Cameron Simpson 

Yes, [congress is] petty and venal and selfish.  That's why they're called
_representatives_.  - Will Durst
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: This mail never gets delivered. Any ideas why?

2013-05-26 Thread Cameron Simpson
On 27May2013 10:22, I wrote:
| | =>  903 self.stdin.write(input)
[...]
| | self = , self.stdin = <_io.BufferedWriter name=5>, 
self.stdin.write = , input 
= 'kdsjfksdjkfjksdjfs\r\n\t'
| | TypeError: 'str' does not support the buffer interface 
| |   args = ("'str' does not support the buffer interface",) 
| |   with_traceback = 
| 
| This is symptomatic of sys.stdin (well, whatever you're writing to)
| being open in binary mode instead of text mode. And you're passing
| a str. Try passing std.encode().

Sorry, that should be "input.encode()".
-- 
Cameron Simpson 

PCs are like a submarine, it will work fine till you open Windows. - zollie101
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2013-05-26 Thread Cameron Simpson
On 26May2013 17:45, Mark Lawrence  wrote:
| On 26/05/2013 17:10, Νίκος Γκρ33κ wrote:
| >Here is the live error log coming form apacher when i request the webpage 
form browser:
| >
| >==> /usr/local/apache/logs/error_log <==
| >[Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] suexec failure: 
could not open log file
| >[Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] fopen: Permission 
denied
| >[Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] fopen: Permission 
denied
| >[Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] Premature end of 
script headers: koukos.py
| >[Sun May 26 19:07:41 2013] [error] [client 46.12.46.11] Premature end of 
script headers: koukos.py
| >
| >what is that suexec?
| 
| What has this got to do with Python?

Little, and I've already explained what suexec is to him some weeks ago, off 
list.

Cheers,
-- 
Cameron Simpson 

The road less traveled is FASTER !!
- Luc Marcouiller, marco...@ireq.hydro.qc.ca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python error codes and messages location

2013-05-26 Thread Cameron Simpson
On 27May2013 00:53, Steven D'Aprano  
wrote:
| On Mon, 27 May 2013 02:13:54 +0300, Carlos Nepomuceno wrote:
| > Where can I find all error codes and messages that Python throws (actual
| > codes and messages from exceptions raised by stdlib)?
| 
| There is no list. It is subject to change from version to version, 
| including point releases.

And better still, it is platform specific too.

[...]
| > I've already found the module 'errno' and got a dictionary
| > (errno.errorcode) and some system error messages
| > (os.strerror(errno.ENAMETOOLONG)) but there's more I couldn't find.
| 
| These are the standard C operating system and file system error codes, 
| not Python exceptions.

And the poster boy example for platform dependence.

Besides, knowing the exact errors that may occur is not the Python Way, it is
the Java Way.

Run it. if it goes bang, handle the errors you expect and understand.
If you get something else, go bang for real because you _don't_
know what should happen, and proceeding is probably insane.

Cheers,
-- 
Cameron Simpson 

Motorcycles are like peanuts... who can stop at just one?
- Zebee Johnstone  aus.motorcycles Poser Permit #1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Short-circuit Logic

2013-05-26 Thread Cameron Simpson
On 27May2013 00:40, Steven D'Aprano  
wrote:
| On Sun, 26 May 2013 16:22:26 -0400, Roy Smith wrote:
| 
| > In article ,
| >  Terry Jan Reedy  wrote:
| > 
| >> On 5/26/2013 7:11 AM, Ahmed Abdulshafy wrote:
| >> 
| >> >   if not allow_zero and abs(x) < sys.float_info.epsilon:
| >> >  print("zero is not allowed")
| >> 
| >> The reason for the order is to do the easy calculation first and the
| >> harder one only if the first passes.
| > 
| > This is a particularly egregious case of premature optimization.  You're
| > worried about how long it takes to execute abs(x)?  That's silly.
| 
| I don't think it's a matter of premature optimization so much as the 
| general principle "run code only if it needs to run". Hence, first you 
| check the flag to decide whether or not you care whether x is near zero, 
| and *only if you care* do you then check whether x is near zero.
| 
| # This is silly:
| if x is near zero:
| if we care:
| handle near zero condition()
| 
| # This is better:
| if we care:
| if x is near zero
| handle near zero condition()
| 
| 
| Not only is this easier to understand because it matches how we do things 
| in the real life, but it has the benefit that if the "near zero" 
| condition ever changes to become much more expensive, you don't have to 
| worry about reordering the tests because they're already in the right 
| order.

I wouldn't even go that far, though nothing you say above is wrong.

Terry's assertion "The reason for the order is to do the easy
calculation first and the harder one only if the first passes" is
only sometimes that case, though well worth considering if the
second test _is_ expensive.

There are other reasons also. The first is of course your response,
that if the first test fails there's no need to even bother with
the second one. Faster, for free!

The second is that sometimes the first test is a guard against even
being able to perform the second test. Example:

  if s is not None and len(s) > 0:
... do something with the non-empty string `s` ...

In this example, None is a sentinel value for "no valid string" and
calling "len(s)" would raise an exception because None doesn't have
a length.

With short circuiting logic you can write this clearly and intuitively in one 
line
without extra control structure like the nested ifs above.

Cheers,
-- 
Cameron Simpson 

Who are all you people and why are you in my computer?  - Kibo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python error codes and messages location

2013-05-26 Thread Cameron Simpson
On 27May2013 04:49, Carlos Nepomuceno  wrote:
| > From: steve+comp.lang.pyt...@pearwood.info
| > On Mon, 27 May 2013 02:13:54 +0300, Carlos Nepomuceno wrote:
| >> Where can I find all error codes and messages that Python throws (actual
| >> codes and messages from exceptions raised by stdlib)?
| >
| > There is no list. It is subject to change from version to version,
| > including point releases.
[...]
| > "Read the source code."
[...]
| 
| That's bad! I'd like to check all the IOError codes that may be
| raised by a function/method but the information isn't there.

No, you really don't.

There are particular, expected, errors you know what to do with
and can proceed in the face of.

And there will often be errors you _don't_ know what to do with.
With the latter, what is your program going to do? You often can't
proceed, so all you can do is log the error and cancel, cleaning
up as you back out. In that circumstance, how will knowing the name
of the error help? You've logged it, and hopefully the exception
message is sufficient.

| Take open() for example[1]. It only says it raises an IOError exception.
| 
| I've had to try "open('','r')" to discover that Errno 22 is the one "IOError: 
[Errno 22] invalid mode ('r') or filename: ''"

It is vague because that is an OS level error, and errno 22 is
EINVAL. For example, MacOSX says (in "man 2 intro"):

 22 EINVAL Invalid argument.  Some invalid argument was supplied. (For
 example, specifying an undefined signal to a signal or kill func‐
 tion).

It happens that above, the filename '' is invalid.

But so what? That call is _never_ going to work because the arguments
are invalid. So you response will be the same, programmatically,
as with many other errors: log and abort.

| Will I only be able to get all error codes reading the source code of open()?

Nope. Well, not directly.

Python open call's the OS' open. If that succeeds, Python does some
more housekeeping and hands you a "file" python object. But if the
OS open fails, Python reports the OS error.

From the Python source you will see that the OS open is called. So
you must then go to the OS docuemntation for further information.

| Is there a way to simulate the errors instead of actually causing
| them? I mean not raising an instance of IOError that I create (like
| "raise IOError(666,'Hell happens')"), but one with the actual
| contents 'args = (errno, strerr)' that open() would raise?
| 
| Something like:
| 
| f=open('filename','r')
| for x in range(25):
|     try:
|     f.raise_errno(x)
|     except IOError, e:
|     if   e.errno == 1:
|     treat_err1()
|     continue
|     elif e.errno == 2:
|     treat_err2()
|     continue

Well, maybe this (untested):

  for x in range(25):
      try:
      exc = OSError(os.strerror(x))
  exc.errno = x
  raise exc
      except IOError, e:
      if   e.errno == 1:
      treat_err1()
      continue
      elif e.errno == 2:
      treat_err2()
      continue

BTW, I recommend importing "errno" and using symbolic names. It makes things 
much more readable, and accomodates the situation where the symbols map to 
different numbers on different platforms. And have a catch-all. For example:

  import errno
  ...
  try:
f = open(filename, .)
  except OSError, e:
if e.errno == errno.EPERM:
  ... Operation not permitted ...
elif e.errno == errno.ENOENT:
  ... No such file or directory ...
else:
  # surprise!
  # log message, let the exception get out
  # callers can catch things in turn
  logging.error("open(%s) fails: %s" % (filename, e))
  raise

| >> I've already found the module 'errno' and got a dictionary
| >> (errno.errorcode) and some system error messages
| >> (os.strerror(errno.ENAMETOOLONG)) but there's more I couldn't find.
| >
| > These are the standard C operating system and file system error codes,
| > not Python exceptions.
| 
| Yes, the docs say it's from "linux/include/errno.h".  
  

On Linux, yes. In general, on any UNIX system the command "man 2
intro" lists the errors that system calls like open may return. And
"man 2 open" should list the particular errors that open can return.
_On that platform_.

The platform variation is a major reason why you should not hope
to enumerate all the errors; the other reason is that you can't
treat every error individually in a fine grained fashion without
making your code hard to read and maintain: the core logic of the
program is hidden behind a forest of tiny little special cases.

Your code will be clearer if you handle the errors that are expected
and that permit the program to proceed sensible. Other errors should
elicit a log message of course, but if you can't proceed with the
task at hand you can almost certainly only abort and try to tidy
up while doing so.

Cheers,
-- 

Processes are like potatoes.  

Re: Short-circuit Logic

2013-05-26 Thread Cameron Simpson
On 27May2013 06:59, Vito De Tullio  wrote:
| Cameron Simpson wrote:
| >   if s is not None and len(s) > 0:
| > ... do something with the non-empty string `s` ...
| > 
| > In this example, None is a sentinel value for "no valid string" and
| > calling "len(s)" would raise an exception because None doesn't have
| > a length.
| 
| obviously in this case an `if s: ...` is more than sufficient :P

:P

My fault for picking too similar a test.

Cheers,
-- 
Cameron Simpson 

Death is life's way of telling you you've been fired.   - R. Geis
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How clean/elegant is Python's syntax?

2013-05-29 Thread Cameron Simpson
On 30May2013 02:13, Ma Xiaojun  wrote:
| For the core language, I have mixed feeling. On one hand, I find that
| Python has some sweet feature that is quite useful. On the other hand,
| I often find Pyhton snippets around hard to understand.

I think you will find that is lack of practice. I find Python far
far easier to read, even in snippet form. BUT, for years I found
it obtuse because I hadn't learnt it. Now that I have, I rarely
want to use other things (if Python is apt; often but not always).

| I admit that I
| never learned Python very formally; I've programmed in many other
| languages already.

Me too. Use it some more.

| Code snippets in BASIC or Pascal seems quite obvious to understand
| (Unless I don't understand the algorithm) .

Code in BASIC is generally lower level than python; it is less
expressive.  Because of this, it will be more obvious in terms of
being more direct. But it will be less expressive because you will
often need more BASIC to do something than you would with Python.
So python is usually more succinct, and therefore more more expressive:
a shorter but very readable way to do the same thing.

Comparison: your English is excellent. Presuming from context that
you're in China, many of your compatriots do not speak fluent English
(and why should they?) For those speaking English as a second
language there are difficulties; English grammar I gather is very
different, and it has a fine suite of irregular forms. (Let me say
up front that I do not speak Chinese at all.)

Anyway, would you rather converse with someone fluent, or not? I
would expect you would far rather deal with a fluent English speaker
or a fluent Chinese speaker than a speaker using English badly.

Python code is a lot like written English. BASIC is like badly
written English.

Cheers,
-- 
Cameron Simpson 

Helicopters are considerably more expensive [than fixed wing aircraft],
which is only right because they don't actually fly, but just beat
the air into submission.- Paul Tomblin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python b'...' notation

2013-05-29 Thread Cameron Simpson
On 29May2013 13:14, Ian Kelly  wrote:
| On Wed, May 29, 2013 at 12:33 PM, alcyon  wrote:
| > This notation displays hex values except when they are 'printable', in 
which case it displays that printable character.  How do I get it to force hex 
for all bytes?  Thanks, Steve
| 
| Is this what you want?
| 
| >>> ''.join('%02x' % x for x in b'hello world')
| '68656c6c6f20776f726c64'

Not to forget binascii.hexlify.
-- 
Cameron Simpson 

Every particle continues in its state of rest or uniform motion in a straight
line except insofar as it doesn't.  - Sir Arther Eddington
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Piping processes works with 'shell = True' but not otherwise.

2013-05-29 Thread Cameron Simpson
On 29May2013 19:39, Thomas Rachel 
 wrote:
| Am 27.05.2013 02:14 schrieb Carlos Nepomuceno:
| >pipes usually consumes disk storage at '/tmp'.
| 
| Good that my pipes don't know about that.
| Why should that happen?

It probably doesn't on anything modern. On V7 UNIX at least there
was a kernel notion of the "pipe fs", where pipe storage existed;
usually /tmp; using small real (but unnamed) files is an easy way
to implement them, especially on systems where RAM is very small
and without a paging VM - for example, V7 UNIX ran on PDP-11s amongst
other things. And files need a filesystem.

But even then pipes are still small fixed length buffers; they don't
grow without bound as you might have inferred from the quoted
statement.

Cheers,
-- 
Cameron Simpson 

ERROR 155 - You can't do that.  - Data General S200 Fortran error code list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sendmail smtplib.SMTP('localhost') Where is the email?

2013-05-30 Thread Cameron Simpson
On 30May2013 15:48, inq1ltd  wrote:
| python help,

Please do not make new discussions by replying to an old discussion.

It is not enough to change the subject line; unless you also remove
any References: and In-Reply-To: header lines your message is still
considered part of the old discussion.

| I've tried this code which I got from:
| http://www.tutorialspoint.com/python/python_sending_email.htm
| 
| I build this file and run it 
[...]
|smtpObj = smtplib.SMTP('localhost')
|smtpObj.sendmail(sender, receivers, message)
|print "Successfully sent email"
[...]
| After running the the file and I get 
| "Successfully sent email"
| 
| My question is why doesn't webmaster get an email?

Well, this suggests that the message has been accepted by the mail
system on localhost. Not that final delivery was made anywhere else.

You now have to read the log files on your mail system to see what happened.

One easy check to do first is to see if it is still in your mail
system but undelivered. On a UNIX system running the command:

  mailq

should tell you that. If the queue is empty, the message has been
sent somewhere and you must dig through the logs to find out where.
If the message is in the queue then the "mailq" command will probably
give a suggestion as to why.

Cheers,
-- 
Cameron Simpson 

As you can see, unraveling even a small part of 'sendmail' can introduce more
complexity than answers.- Brian Costales, _sendmail_
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Surprising difference between StringIO.StringIO and io.StringIO

2013-05-30 Thread Cameron Simpson
On 30May2013 15:46, Skip Montanaro  wrote:
| Consider this quick session (Python 2.7 using the tip of the 2.7
| branch in Mercurial):
| 
| % python2.7
| Python 2.7.5+ (2.7:93eb15779050, May 30 2013, 15:27:39)
| [GCC 4.4.6 [TWW]] on linux2
[...]
| >>> import io
| >>> s2 = io.StringIO()
[...]
|   File "/home/skipm/x86_64-linux3.1/lib/python2.7/traceback.py", line
| 13, in _print
| file.write(str+terminator)
| TypeError: unicode argument expected, got 'str'
| >>> print s2.getvalue()
| 
| What is it about io.StringIO that it doesn't like strings and requires
| Unicode?  This is on an OpenSUSE 12.1 system.  I have tried with LANG
| set to the default ("en_US.UTF-8") and to "C".  I also tried on a
| Solaris system with an older micro revision of Python 2.7.  Same
| result.
| 
| Am I missing something about how io.StringIO works?  I thought it was
| a more-or-less drop-in replacement for StringIO.StringIO.

I would expect io.StringIO to be a match for the io.* stuff in Python
3. So it should care whether it is a binary stream or a text stream.
Whereas StringIO.StringIO is your good old Python 2 StringIO, which expects
strs.

On that basis, io.StringIO is a text stream, expecting Unicode
objects for transcription. 'str' is, in that context, probably
considered as 'bytes' in Python 3.

Cheers,
-- 
Cameron Simpson 

A ship in harbor is safe - but that is not what ships are for.  - John A. Shedd
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: usage of os.posix_fadvise

2013-05-30 Thread Cameron Simpson
On 30May2013 17:54, Wolfgang Maier  
wrote:
| Antoine Pitrou wrote:
| >The Linux version of "man posix_fadvise" probably holds the answer:
[...]
| 
| Hi Antoine,
| you're right and thanks a lot for this great piece of information.
[...]
| P.S.: Maybe these new os module features could use a bit more documentation? 

This suggestion comes up a lot WRT to the os.* functions. Unfortunately,
the required doco for your issue is very platform dependent as
you've just discovered; not merely your distro but the very kernel
revisions.

Maybe it should be made much more obvious that users _must_ go and
consult their platform's documentation for specifics on how their
platform implents the interface. On UNIX, that means "man 2 blah"
or "man 3 blah", depending.

Cheers,
-- 
Cameron Simpson 

I suppose the solution would be to close the composition window and let
my article sit for half an hour or so once I've finished with it, and
then go back and proofread it once more.  But that would be a pain in
the proverbial bifurcated derriere.  Part of the experience of flaming is
to load a searing missive into the conceptual breech of my SPARCcannon
and pull the imaginary lanyard whilst flushed with the adrenaline of mortal
combat.
- Geoff Miller, 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can anyone please help me in understanding the following python code

2013-05-30 Thread Cameron Simpson
On 30May2013 21:54, bhk...@gmail.com  wrote:
| One final question, Is there a way to edit the message once it has been 
posted? 

Essentially, no. If there's some error in a post, reply to it
yourself with a correction. Transparency is a good thing. Revisionist
history pretty much is not.
-- 
Cameron Simpson 

Well, it's one louder, isn't it?  It's not ten.  You see, most blokes are
gonna be playing at ten, you're on ten here, all the way up, all the way up,
all the way up, you're on ten on your guitar, where can you go from there?
Where?  Nowhere, exactly.  What we do is, if we need that extra push over the
cliff, you know what we do?  Eleven.  Exactly.  One louder.
- Nigel Tufnel, _This Is Spinal Tap_
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this code correct?

2013-05-31 Thread Cameron Simpson
On 30May2013 06:45, Nikos as SuperHost Support  wrote:
| Τη Πέμπτη, 30 Μαΐου 2013 4:36:11 μ.μ. UTC+3, ο χρήστης Chris Angelico έγραψε:
| > Lemme guess, he's next going to ask on the PostgreSQL mailing list. I
| > mean, that's unrelated to Python, right?
| 
| Well Chris, i'am not that stupid :)
| 
| I intend to ask questions unrelated to Python to a list unrelated to Python 
but related to my subject, whish is 'suexec', that would mean a linux list.

Actually, you need an apache httpd list for suexec. It is part of
the web server CGI implementation.
-- 
Cameron Simpson 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Too many python installations. Should i remove them all and install the latest?

2013-06-01 Thread Cameron Simpson
On 01Jun2013 00:51, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| Τη Σάββατο, 1 Ιουνίου 2013 9:18:26 π.μ. UTC+3, ο χρήστης Chris Angelico 
έγραψε:
| > That would require that the repo have a 3.3.2 build in it. I don't
| > know the Red Hat / CentOS policies there, but I know Debian stable 
| > wouldn't have anything so new - it takes time to test things.
| 
| Is there a way to change to some repo that contain the latest python 3.3.2 to 
yo yum it?

I asked Google:

  extra yum repositories for centos

and it pointed me at:

  http://wiki.centos.org/AdditionalResources/Repositories

Probably one of these has Python 3. Or build it from source; it's not hard.
-- 
Cameron Simpson 

Luge strategy? Lie flat and try not to die.
- Carman Boyle, Olympic Luge Gold Medalist
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Too many python installations. Should i remove them all and install the latest?

2013-06-01 Thread Cameron Simpson
On 01Jun2013 01:30, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| Τη Σάββατο, 1 Ιουνίου 2013 11:21:14 π.μ. UTC+3, ο χρήστης Cameron Simpson 
έγραψε:
| > On 01Jun2013 00:51, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| > | Τη Σάββατο, 1 Ιουνίου 2013 9:18:26 π.μ. UTC+3, ο χρήστης Chris Angelico 
έγραψε:
| > | > That would require that the repo have a 3.3.2 build in it. I don't
| > | > know the Red Hat / CentOS policies there, but I know Debian stable 
| > | > wouldn't have anything so new - it takes time to test things.
| > | 
| > | Is there a way to change to some repo that contain the latest python 
3.3.2 to yo yum it?
| > 
| > I asked Google:
| >   extra yum repositories for centos
| > and it pointed me at:
| >   http://wiki.centos.org/AdditionalResources/Repositories
| > 
| > Probably one of these has Python 3. Or build it from source; it's not hard.
| 
| Should i chnage form CentoOS 6.4 to ubuntu by your opinion?

No. Just sort it out on CentOS. Try the extra repos. Get Python 3
from one (I'm not sure why you think your current Python 3 install
is a problem anyway, though).

You're already slightly familiar with CentOS. Switching distros
will just cause more pain. Fix your actual problems first.

This is not a recommendation one way or another re CentOS versus
Ubunutu; it is a recommendation not to change without a better reason.
-- 
Cameron Simpson 

It takes seven or eight people to send a FAX.   - Anonymous IRS guy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Errin when executing a cgi script that sets a cookie in the browser

2013-06-05 Thread Cameron Simpson
On 05Jun2013 14:18, ru...@yahoo.com  wrote:
| On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote:
| > On Thu, Jun 6, 2013 at 6:56 AM,   wrote:
| > > On Wednesday, June 5, 2013 1:54:45 PM UTC-6, Νικόλαος Κούρας wrote:
| > >>...
| > >> print( cookie, "Content-type: text/html; charset=utf-8\n", message ) 
| > >>...
| > >   print( cookie, "Content-type: text/html; charset=utf-8\n\n", message )
| > > (ie, note the two \n's after the "utf-8" test.)
| > 
| > But that won't solve it either. The default separator for print is a
| > space, so this will indent his Content-type line by one space.
| 
| Ah, quite right.  Something like 
| 
|   print( cookie, "\nContent-type: text/html; charset=utf-8\n\n", message )
| 
| then.

Unless "cookie" already has a newline. Then you'll end the headers there:-)

A more robust approach might be to build a dict (or possibly better,
list) of headers without newlines and then as a separate act to
print them with newlines and add the spacer newline later, before
writing the message body.

Cheers,
-- 
Cameron Simpson 

Drill for oil? You mean drill into the ground to try and find oil?
You're  crazy.
  --Drillers whom Edwin L. Drake tried to enlist to his project
to drill for  oil in 1859.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-06 Thread Cameron Simpson
On 05Jun2013 11:43, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| Τη Τετάρτη, 5 Ιουνίου 2013 9:32:15 μ.μ. UTC+3, ο χρήστης MRAB έγραψε:
| > Using Python, I think you could get the filenames using os.listdir, 
| > passing the directory name as a bytestring so that it'll return the
| > names as bytestrings.
| 
| > Then, for each name, you could decode from its current encoding and 
| > encode to UTF-8 and rename the file, passing the old and new paths to
| > os.rename as bytestrings.
| 
| Iam not sure i follow:
| 
| Change this:
| 
| # Compute a set of current fullpaths
| fullpaths = set()
| path = "/home/nikos/public_html/data/apps/"
| 
| for root, dirs, files in os.walk(path):
[...]

Have a read of this:

  http://docs.python.org/3/library/os.html#os.listdir

The UNIX API accepts bytes for filenames and paths.

Python 3 strs are sequences of Unicode code points. If you try to
open a file or directory on a UNIX system using a Python str, that
string must be converted to a sequence of bytes before being handed
to the OS.

This is done implicitly using your locale settings if you just use a str.

However, if you pass a bytes to open or listdir, this conversion
does not take place. You put bytes in and in the case of listdir
you get bytes out.

You can work on pathnames in bytes and never concern yourself with
encode/decode at all.

In this way you can write code that does not care about the translation
between Unicode and some arbitrary byte encoding.

Of course, the issue will still arise when accepting user input;
your shell has done exactly this kind of thing when you renamed
your MP3 file. But it is possible to write pure utility code that
doesn't care about filenames as Unicode or str if you work purely
in bytes.

Regarding user filenames, the common policy these days is to use
utf-8 throughout. Of course you need to get everything into that
regime to start with.
-- 
Cameron Simpson 

...but C++ gloggles the cheesewad, thus causing a type conflict.
- David Jevans, jev...@apple.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to store a variable when a script is executing for next time execution?

2013-06-06 Thread Cameron Simpson
On 06Jun2013 03:50, Avnesh Shakya  wrote:
| hi,
|I am running a python script and it will create a file name like 
filename0.0.0 and If I run it again then new file will create one more like 
filename0.0.1.. my code is-
| 
| i = 0
| for i in range(1000):
| try:
| with open('filename%d.%d.%d.json'%(0,0,i,)): pass
| continue
| except IOError:
| dataFile = file('filename%d.%d.%d.json'%(0,0,i,), 'a+')
| break
| But It will take more time after creating many files, So i want to store 
value of last var "i" in a variable so that when i run my script again then I 
can use it. for example-
|  my last created file is filename0.0.27 then it should store 
27 in a variable and when i run again then new file should be created 0.0.28 
according to last value "27", so that i could save time and it can create file 
fast..
| 
| Please give me suggestion for it.. How is it possible?

Write it to a file? Read the file next time the script runs?

BTW, trying to open zillions of files is slow.
But using listdir to read the directory you can see all the names.
Pick the next free one (and then test anyway).
-- 
Cameron Simpson 

The mark must be robust enough to survive MP3 transmission over the Internet,
but remain inaudible when played on the yet to be launched DVD-Audio players.
- the SDMI audio watermarkers literally ask for the impossible, since all
  audio compressors aim to pass _only_ human perceptible data
  http://www.newscientist.com/news/news.jsp?id=ns224836
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-06 Thread Cameron Simpson
On 06Jun2013 05:04, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| We are in test mode so i dont know if when renaming actually take place what 
the encodings will be.
| Shall i switch off test mode and try it for real?

I would make a copy. Since you're renaming stuff, hard links would do:

  cp -rpl original-dir test-dir

Then test stuff in test-dir.
-- 
Cameron Simpson 

Too much of a good thing is never enough.   - Luba
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-06 Thread Cameron Simpson
On 06Jun2013 11:46, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| Τη Πέμπτη, 6 Ιουνίου 2013 3:44:52 μ.μ. UTC+3, ο χρήστης Steven D'Aprano 
έγραψε:
| > py> s = '999-Eυχή-του-Ιησού'
| > py> bytes_as_utf8 = s.encode('utf-8')
| > py> t = bytes_as_utf8.decode('iso-8859-7', errors='replace')
| > py> print(t) 
| > 999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ
| 
| errors='replace' mean dont break in case or error?

Yes. The result will be correct for correct iso-8859-7 and slightly mangled
for something that would not decode smoothly.

| You took the unicode 's' string you utf-8 bytestringed it.
| Then how its possible to ask for the utf8-bytestring to decode
| back to unicode string with the use of a different charset that the
| one used for encoding and thsi actually printed the filename in
| greek-iso?

It is easily possible, as shown above. Does it make sense? Normally
not, but Steven is demonstrating how your "mv" exercises have
behaved: a rename using utf-8, then a _display_ using iso-8859-7.

| > So that demonstrates part of your problem: even though your Linux system  
| > is using UTF-8, your terminal is probably set to ISO-8859-7. The  
| > interaction between these will lead to strange and disturbing Unicode 
| > errors.
| 
| Yes i feel this is the problem too. 
| Its a wonder to me why putty used by default greek-iso instead of utf-8 !!

Putty will get its terminal setting from the system you came from.
I suppose Windows of some kind. If you look at Putty's settings you
may be able to specify UTF-8 explicitly; not sure. If you can, do
that. At least there will be one less layer of confusion to debug.

| Please explain this t me because now that i begin to understand
| this encode/decode things i begin to like them!
| 
| a) WHAT does it mean when a linux system is set to use utf-8?

It means the locale settings _for the current process_ are set for
UTF-8. The "locale" command will show you the current state. There
will also be some system settings with defaults for stuff started
up by the system. On CentOS and RedHat that is probably the file:

  /etc/sysconfig/i18n

_However_, when you ssh in to the system using Putty or another ssh
client, the settings at your local end are passes to the remote ssh
session. In this way different people using different locales can
ssh in and get the locales they expect to use.

Of course, of the locale settings differ and these people are working
on the same files and text, madness will ensue.

| b) WHAT does it mean when a terminal client is set to use utf-8?

It means the _display_ end of the terminal will render characters
using UTF-8. Data comes from the remote system as a sequence of
bytes. The terminal receives these bytes and _decodes_ them using
utf-8 (or whatever) in order to decides what characters to display.

| c) WHAT happens when the two of them try to work together?

If everything matches, it is all good. If the locales do not match,
the mismatch will result in an undesired bytes<->characters
encode/decode step somewhere, and something will display incorrectly
or be entered as input incorrectly.

| > So I believe I understand how your file name has become garbage. To fix 
| > it, make sure that your terminal is set to use UTF-8, and then rename it. 
| > Do the same with every file in the directory until the problem goes away.
| 
| ni...@superhost.gr [~/www/cgi-bin]# echo $LS_OPTIONS
| --color=tty -F -a -b -T 0
| 
| Is this okey? The '-b' option is for to display a filename in binary mode?

Probably. "man ls" will tell you.

Personally, I "unalias ls" on RedHat systems (and any other system
where an alias has been set up). I want ls to do what I say, not
what someone else thought was a good idea.

| Indeed i have changed putty to use 'utf-8' and 'ls -l' now displays
| the file in correct greek letters. Switching putty's encoding back
| to 'greek-iso' then the *displayed* filanames shows in mojabike.

Exactly so.

| WHAT is being displayed and what is actually stored as bytes is two different 
thigns right?

Yes. Display requires the byte stream to be decoded. Wrong decoding
display wrong characters/glyphs.

| Ευχη του Ιησου.mp3
| EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ
| 
| is the way the filaname is displayed in the terminal depending
| on the encoding the terminal uses, correct? But no matter *how* its
| being dislayed those two are the same file?

In principle, yes. Nothing has changed on the filesystem itself.

Cheers,
-- 
Cameron Simpson 

in rec.moto, jsh wrote:
> Dan Nitschke wrote:
> > Ged Martin wrote:
> > > On Sat, 17 May 1997 16:53:33 +, Dan Nitschke scribbled:
> > > >(And you stay *out* of my dreams, you deviant little
> > > >weirdo.)
> > > Yeah, yeah, that's what you're saying in _public_
> > Feh.

Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-07 Thread Cameron Simpson
On 07Jun2013 11:10, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| On 7/6/2013 10:42 πμ, Michael Weylandt wrote:
| >os.rename( filepath_bytes filepath.encode('utf-8')
| >Missing comma, which is, after all, just a matter of syntax so it can't 
matter, right?
|
| I doubted that os.rename arguments must be comma seperated.

Why?

Every other python function separates arguments with commas.

| 'mv source target' didn't require commas so i though it was safe to assume 
that os.rename did not either.

"mv" is shell syntax.
os.rename is Python syntax.

Two totally separate languages.
-- 
Cameron Simpson 

Cynic, n. A blackguard whose faulty vision sees things as they are, not as
they ought to be.
Ambrose Bierce (1842-1914), U.S. author. The Devil's Dictionary (1881-1906).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-07 Thread Cameron Simpson
On 07Jun2013 09:56, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| On 7/6/2013 4:01 πμ, Cameron Simpson wrote:
| >On 06Jun2013 11:46, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| >| Τη Πέμπτη, 6 Ιουνίου 2013 3:44:52 μ.μ. UTC+3, ο χρήστης Steven D'Aprano 
έγραψε:
| >| > py> s = '999-Eυχή-του-Ιησού'
| >| > py> bytes_as_utf8 = s.encode('utf-8')
| >| > py> t = bytes_as_utf8.decode('iso-8859-7', errors='replace')
| >| > py> print(t)
| >| > 999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ
| >|
| >| errors='replace' mean dont break in case or error?
| >
| >Yes. The result will be correct for correct iso-8859-7 and slightly mangled
| >for something that would not decode smoothly.
|
| How can it be correct? We have encoded out string in utf-8 and then
| we tried to decode it as greek-iso? How can this possibly be
| correct?

Ok, not correct. But consistent. Safe to call.

If it is a valid iso-8859-7 sequence (which might cover everything,
since I expect it is an 8-bit 1:1 mapping from bytes values to a
set of codepoints, just like iso-8859-1) then it may decode to the
"wrong" characters, but the reverse process (characters encoded as
bytes) should produce the original bytes.  With a mapping like this,
errors='replace' may mean nothing; there will be no errors because
the only Unicode characters in play are all from iso-8859-7 to start
with. Of course another string may not be safe.

| >| You took the unicode 's' string you utf-8 bytestringed it.
| >| Then how its possible to ask for the utf8-bytestring to decode
| >| back to unicode string with the use of a different charset that the
| >| one used for encoding and thsi actually printed the filename in
| >| greek-iso?
| >
| >It is easily possible, as shown above. Does it make sense? Normally
| >not, but Steven is demonstrating how your "mv" exercises have
| >behaved: a rename using utf-8, then a _display_ using iso-8859-7.
|
| Same as above, i don't understand it at all, since different
| charsets(encodings) used in the encode/decode process.

Visually, the names will be garbage. And if you go:

  mv '999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ.mp3' '999-Eυχή-του-Ιησού.mp3'

while using the iso-8859-7 locale, the wrong thing will occur
(assuming it even works, though I think it should because all these
characters are represented in iso-8859-7, yes?)

Why?

In the iso-8859-7 locale, your (currently named under an utf-8
regime) file looks like '999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ.mp3' (because the
Unicode byte sequence maps to those characters in iso-8859-7). Why
you issue the about "mv" command, the new name will be the _iso-8859-7_
bytes encoding for '999-Eυχή-του-Ιησού.mp3'.  Which, under an utf-8
regime will decode to _other_ characters.

If you want to repair filenames, by which I mean, cause them to be correctly
encoded for utf-8, you are best to work in utf-8 (using "mv" or python).

Of course, the badly named files will then look wrong in your listing.

If you _know_ the filenames were written using iso-8859-7 encoding, and that 
the names are "right" under that encoding, you can write python code to rename 
them to utf-8.

Totally untested example code:

  import sys
  from binascii import hexlify

  for bytename in os.listdir( b'.' ):
unicode_name = bytename.decode('iso-8859-7')
new_bytename = unicode_name.encode('utf-8')
print("%s: %s => %s" % (unicode_name, hexlify(bytename), 
hexlify(new_bytename)), file=sys.stderr)
os.rename(bytename, new_bytename)

That code should not care what locale you are using because it uses
bytes for the file calls and is explicit about the encoding/decoding
steps.

| >| a) WHAT does it mean when a linux system is set to use utf-8?
| >
| >It means the locale settings _for the current process_ are set for
| >UTF-8. The "locale" command will show you the current state.
|
| That means that, when a linux application needs to saved a filename
| to the linux filesystem, the app checks the filesytem's 'locale', so
| to encode the filename using the utf-8 charset ?

At the command line, many will not. They'll just read and write bytes.

Some will decode/encode. Those that do, should by default use the
current locale.

But broadly, it is GUI apps that care about this because they must
translate byte sequences to glyphs: images of characters. So plenty
of command line tools do not need to care; the terminal application
is the one that presents the names to you; _it_ will decode them
for display. And it is the terminal app that translates your
keystrokes into bytes to feed to the command line.

NOTE: it is NOT the filesystem's locale. It is the current process'
locale, which is deduced from environment variables (which 

Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-07 Thread Cameron Simpson
On 07Jun2013 11:52, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| ni...@superhost.gr [~/www/cgi-bin]# [Fri Jun 07 21:49:33 2013] [error] 
[client 79.103.41.173]   File "/home/nikos/public_html/cgi-bin/files.py", line 
81
| [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] if( flag == 
'greek' )
| [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] 
^
| [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] SyntaxError: 
invalid syntax
| [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] Premature end of 
script headers: files.py
| ---
| i dont know why that if statement errors.

Python statements that continue (if, while, try etc) end in a colon, so:

  if flag == 'greek':

Cheers,
-- 
Cameron Simpson 

Hello, my name is Yog-Sothoth, and I'll be your eldritch horror today.
- Heather Keith 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-07 Thread Cameron Simpson
On 07Jun2013 04:53, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| Τη Παρασκευή, 7 Ιουνίου 2013 11:53:04 π.μ. UTC+3, ο χρήστης Cameron Simpson 
έγραψε:
| > | >| errors='replace' mean dont break in case or error?
| > 
| > | >Yes. The result will be correct for correct iso-8859-7 and slightly 
mangled
| > | >for something that would not decode smoothly.
| > 
| > | How can it be correct? We have encoded out string in utf-8 and then
| > | we tried to decode it as greek-iso? How can this possibly be
| > | correct?
| 
| > If it is a valid iso-8859-7 sequence (which might cover everything, 
| > since I expect it is an 8-bit 1:1 mapping from bytes values to a 
| > set of codepoints, just like iso-8859-1) then it may decode to the 
| > "wrong" characters, but the reverse process (characters encoded as
| > bytes) should produce the original bytes.  With a mapping like this, 
| > errors='replace' may mean nothing; there will be no errors because
| > the only Unicode characters in play are all from iso-8859-7 to start
| > with. Of course another string may not be safe. 
| 
| > Visually, the names will be garbage. And if you go:
| >   mv '999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ.mp3' '999-Eυχή-του-Ιησού.mp3'
| > while using the iso-8859-7 locale, the wrong thing will occur
| > (assuming it even works, though I think it should because all these
| > characters are represented in iso-8859-7, yes?)
| 
| All the rest you i understood only the above quotes its still unclear to me.
| I cant see to understand it.
| 
| Do you mean that utf-8, latin-iso, greek-iso and ASCII have the 1st 0-127 
codepoints similar?

Yes. It is certainly true for utf-8 and latin-iso and ASCII.
I expect it to be so for greek-iso, but have not checked.

They're all essentially the ASCII set plus a range of other character
codepoints for the upper values.  The 8-bit sets iso-8859-1 (which
I take you to mean by "latin-iso") and iso-8859-7 (which I take you
to mean by "greek-iso") are single byte mapping with the top half
mapped to characters commonly used in a particular region.

Unicode has a much greater range, but the UTF-8 encoding of Unicode
deliberately has the bottom 0-127 identical to ASCII, and higher
values represented by multibyte sequences commences with at least
the first byte in the 128-255 range. In this way pure ASCII files
are already in UTF-8 (and, in fact, work just fine for the iso-8859-x
encodings as well).

| For example char 'a' has the value of '65' for all of those character sets?
| Is hat what you mean?

Yes.

| s = 'a'  (This is unicode right?  Why when we assign a string to
| a variable that string's type is always unicode and does not
| automatically become utf-8 which includes all available world-wide
| characters? Unicode is something different that a character set? )

In Python 3, yes. Strings are unicode. Note that that means they are
sequences of codepoints whose meaning is as for Unicode.

"utf-8" is a byte encoding for Unicode strings. An external storage
format, if you like. The first 0-127 codepoints are 1:1 with byte
values, and the higher code points require multibyte sequences.

| utf8_byte = s.encode('utf-8')

Unicode string => utf-8 byte encoding.

| Now if we are to decode this back to utf8 we will receive the char 'a'.

Yes.

| I beleive same thing will happen with latin, greek, ascii isos. Correct?
| 
| utf8_a = utf8_byte.decode('iso-8859-7')
| latin_a = utf8_byte.decode('iso-8859-1')
| ascii_a = utf8_byte.decode('ascii')
| utf8_a = utf8_byte.decode('iso-8859-7')
| 
| Is this correct? 

Yes, because of the design decision about the 0-127 codepoints.

| All of those decodes will work even if the encoded bytestring was of utf8 
type?
| 
| The characters that will not decode correctly are those that their codepoints 
are greater that > 127 ?
| for example if s = 'α' (greek character equivalent to english 'a')
| Is this what you mean?

Yes, exactly so.

| 
| 
| Now back to my almost ready files.py script please:
| 
| 
| #
| # Collect filenames of the path dir as bytes
| greek_filenames = os.listdir( b'/home/nikos/public_html/data/apps/' )
| 
| for filename in greek_filenames:
|   # Compute 'path/to/filename' in bytes
|   greek_path = b'/home/nikos/public_html/data/apps/' + b'filename'

You don't mean b'filename', which is the literal word "filename".
You mean: filename.encode('iso-8859-7')

More probably, you mean:

  dirpath = b'/home/nikos/public_html/data/apps/'
  greek_filenames = os.listdir(dirpath)
  for greek_filename in greek_filenames:
try:
  fi

Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-08 Thread Cameron Simpson
On 08Jun2013 14:14, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| Τη Σάββατο, 8 Ιουνίου 2013 10:01:57 μ.μ. UTC+3, ο χρήστης Steven D'Aprano 
έγραψε:
| > ASCII actually needs 7 bits to store a character. Since computers are  
| > optimized to work with bytes, not bits, normally ASCII characters are
| > stored in a single byte, with one bit wasted.
| 
| So ASCII and Unicode are 2 Encoding Systems currently in use.
| How should i imagine them, visualize them?
| Like tables 'A' = 65, 'B' = 66 and so on?

Yes, that works.

| But if i do then that would be the visualization of a 'charset' not of an 
encoding system.
| What the diffrence of an encoding system and of a charset?

And encoding system is the method or transcribing these values to bytes and 
back again.

| ebcdic - ascii - unicode = al of them are encoding systems
| greek-iso - latin-iso - utf8 - utf16 = all of them are charsets.

No.

EBCDIC and ASCII and Unicode and Greek-ISO (iso-8859-7) are all character sets.
(1:1 mappings of characters to numbers/ordinals).

And encoding is a way of writing these values to bytes.
Decoding reads bytes and emits character values.

Because all of EBCDIC, ASCII and the iso-8859-x characters sets fit in the 
range 0-255,
they are usually transcribed (encoded) directly, one byte per ordinal.

Unicode is much larger. It cannot be transcribed (encoded) as one bytes to one 
value.
There are several ways of transcribing Unicode. UTF-8 is a popular and usually 
compact form,
using one byte for values below 128 and and multiple bytes for higher values.

| Why python interprets by default all given strings as unicode and
| not ascii? because the former supports many positions while ascii
| only 127 positions , hence can interpet only 127 different characters?

Yes.

[...]
| > Latin-1 is similar, except there are 256 positions. Greek ISO-8859-7 is 
| > also similar, also 256 positions, but the characters are different. And 
| > so on, with dozens of charsets. 
| 
| Latin has to display english chars(capital, small) + numbers + symbols. that 
would be 127 why 256?

ASCII runs up to 127. Essentially English, numerals, control codes and various 
symbols.

The iso-8859-x sets run to 255, and the upper 128 values map to
characters popular in various regions.

| greek = all of the above plus greek chars, no?

So iso-8859-7 included the Greek characters.

| > And then there is Unicode, which includes *every* character is all of 
| > those dozens of charsets. It has 1114111 positions (most are currently  
| > unfilled).
| 
| Shouldt the positions that Unicode has to use equal to the summary
| of all available characters of all the languages of the worlds plus
| numbers and special chars? why 1.000.000+ why the need for so many
| positions? Narrow Unicode format (2 byted) can cover all ofmthe
| worlds symbols.

2 bytes is not enough. Chinese alone has more glyphs than that.

| > An encoding is simply a program that takes a character and returns a 
| > byte, or visa versa. For instance, the ASCII encoding will take character 
| > 'A'. That is found at position 65, which is 0x41 in hexadecimal, so the 
| > ASCII encoding turns character 'A' into byte 0x41, and visa versa.
| 
| Why you say ASCII turn a character into HEX format and not as in binary 
format?

Steven didn't say that. He said "position 65". People often write
bytes in hex (eg 0x41) because a byte always fits in a 2-character
hex (16 x 16) and because often these values have binary-based
subranges, and hex makes that more obvious.

For example, 'A' is 0x41. 'a' is 0x61. So you can look at the hex
code and almost visually know if you're dealing with upper or lower
case, etc.

| Isnt the latter the way bytes are stored into hdd, like 01010010101 etc?
| Are they stored as hex instead or you just said so to avoid printing 0s and 
1s?

They're stored as bits at the gate level. But writing hex codes
_in_ _text_ is more compact, and more readable for humans.

Cheers,
-- 
Cameron Simpson 

A lot of people don't know the difference between a violin and a viola, so
I'll tell you.  A viola burns longer.   - Victor Borge
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-09 Thread Cameron Simpson
On 09Jun2013 06:25, Steven D'Aprano  
wrote:
| [... heaps of useful explaination ...]
| > When locale to linux system is set to utf-8 that would mean that the 
| > linux applications, should try to encode string into hdd by using 
| > system's default encoding to utf-8 nad read them back from bytes by
| > also using utf-8. Is that correct?
| 
| Yes.

Although I'd point out that only application that care about text
as _text_ need to consider Unicode and the encoding. A command like
"mv" does not care. You type the command and "mv" receives byte
strings as its arguments. So it is doing straight forward "bytes"
file renames. It does not care or even know about encodings.

In this scenario, really it is the Terminal program (eg Putty) which
cares about text (what you type, and what gets displayed). It is
because of mismatches between your Terminal local settings and the
encoding that was chosen for the filenames that you get garbage
listings, one way or another.

Cheers,
-- 
Cameron Simpson 

But then, I'm only 50. Things may well get a bit much for me when I
reach the gasping heights of senile decrepitude of which old Andy
Woodward speaks with such feeling.
- Chris Malcolm, c...@uk.ac.ed.aifh, DoD #205
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-09 Thread Cameron Simpson
On 09Jun2013 02:00, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= 
 wrote:
| Steven wrote:
| >> Since 1 byte can hold up to 256 chars, why not utf-8 use 1-byte for 
| >> values up to 256? 
| 
| >Because then how do you tell when you need one byte, and when you need 
| >two? If you read two bytes, and see 0x4C 0xFA, does that mean two 
| >characters, with ordinal values 0x4C and 0xFA, or one character with 
| >ordinal value 0x4CFA? 
| 
| I mean utf-8 could use 1 byte for storing the 1st 256 characters. I meant up 
to 256, not above 256.

Then it would not be UTF-8. UTF-8 will encode an Unicode codepoint. Your 
suggestion will not.

I'd point out that if you did this, you'd be back in the same
situation you just encountered with ASCII: the first above-255 value
would raise a UnicodeEncodeError (an error which does not even exist
at present:-)

| >> UTF-8 and UTF-16 and UTF-32 
| >> I though the number beside of UTF- was to declare how many bits the 
| >> character set was using to store a character into the hdd, no? 
| 
| >Not exactly, but close. UTF-32 is completely 32-bit (4 byte) values. 
| >UTF-16 mostly uses 16-bit values, but sometimes it combines two 16-bit 
| >values to make a surrogate pair.
| 
| A surrogate pair is like itting for example Ctrl-A, which means is a 
combination character that consists of 2 different characters?
| Is this what a surrogate is? a pari of 2 chars?

Essentially. The combination represents a code point.

| >UTF-8 uses 8-bit values, but sometimes 
| >it combines two, three or four of them to represent a single code-point.
| 
| 'a' to be utf8 encoded needs 1 byte to be stored ? (since ordinal = 65)
| 'α΄' to be utf8 encoded needs 2 bytes to be stored ? (since ordinal is > 127 )
| 'a chinese ideogramm' to be utf8 encoded needs 4 byte to be stored ? (since 
ordinal >  65000 )
| 
| The amount of bytes needed to store a character solely depends on the 
character's ordinal value in the Unicode table?

Essentially. You can read up on the exact process in Wikipedia or the Unicode 
Standard.

Cheers,
-- 
Cameron Simpson 

The most annoying thing about being without my files after our disc crash was
discovering once again how widespread BLINK was on the web.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-09 Thread Cameron Simpson
On 09Jun2013 08:15, Steven D'Aprano  
wrote:
| On Sun, 09 Jun 2013 00:00:53 -0700, nagia.retsina wrote:
| > path = b'/home/nikos/public_html/data/apps/'
| > files = os.listdir( path )
| > 
| > for filename in files:
| > # Compute 'path/to/filename'
| > filepath_bytes = path + filename
| > for encoding in ('utf-8', 'iso-8859-7', 'latin-1'):
| > try:
| > filepath = filepath_bytes.decode( encoding )
| > except UnicodeDecodeError:
| > continue
| > 
| > # Rename to something valid in UTF-8
| > if encoding != 'utf-8':
| > os.rename( filepath_bytes, 
| >  filepath.encode('utf-8') )
| > assert os.path.exists( filepath )
| > break
| > else:
| > # This only runs if we never reached the break 
| >   raise ValueError(
| > 'unable to clean filename %r' % filepath_bytes )
| 
| Editing the traceback to get rid of unnecessary noise from the logging:
| 
| Traceback (most recent call last):
|   File "/home/nikos/public_html/cgi-bin/files.py", line 83, in 
|   assert os.path.exists( filepath )
|   File "/usr/local/lib/python3.3/genericpath.py", line 18, in exists
|   os.stat(path)
| UnicodeEncodeError: 'ascii' codec can't encode characters in position 
| 34-37: ordinal not in range(128)
| 
| > Why am i still receing unicode decore errors? With the help of you guys
| > we have writen a prodecure just to avoid this kind of decoding issues
| > and rename all greek_byted_filenames to utf-8_byted.
| 
| That's a very good question. It works for me when I test it, so I cannot 
| explain why it fails for you.

If he's lucky the UnicodeEncodeError occurred while trying to print
an error message, printing a greek Unicode string in the error with
ASCII as the output encoding (default when not a tty IIRC).

Cheers,
-- 
Cameron Simpson 

I generally avoid temptation unless I can't resist it.  - Mae West
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A certainl part of an if() structure never gets executed.

2013-06-12 Thread Cameron Simpson
On 11Jun2013 18:25, Nikos  wrote:
| > What are the values of 'name', 'month' and 'year' in each of the cases?
| > 
| > Printing out ascii(name), ascii(month) and ascii(year), will be helpful.
| > 
| > Then try stepping through those lines in your head.
| 
| i hav epribted all values of those variables and they are all correct.
| i just dont see why ti fails to enter the specific if case.

Gah!
_Show_ us the values!
And _specify_ which part of the if-statement should run.
-- 
Cameron Simpson 

It's a vague science.   - Rory Tate, circle researcher.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A few questiosn about encoding

2013-06-13 Thread Cameron Simpson
On 13Jun2013 17:19, Nikos as SuperHost Support  wrote:
| A code-point and the code-point's ordinal value are associated into
| a Unicode charset. They have the so called 1:1 mapping.
| 
| So, i was under the impression that by encoding the code-point into
| utf-8 was the same as encoding the code-point's ordinal value into
| utf-8.
| 
| So, now i believe they are two different things.
| The code-point *is what actually* needs to be encoded and *not* its
| ordinal value.

Because there is a 1:1 mapping, these are the same thing: a code
point is directly _represented_ by the ordinal value, and the ordinal
value is encoded for storage as bytes.

| > The leading 0b is just syntax to tell you "this is base 2, not base 8
| > (0o) or base 10 or base 16 (0x)". Also, leading zero bits are dropped.
| 
| But byte objects are represented as '\x' instead of the
| aforementioned '0x'. Why is that?

You're confusing a "string representation of a single number in
some base (eg 2 or 16)" with the "string-ish representation of a
bytes object".

The former is just notation for writing a number in different bases, eg:

  27base 10
  1bbase 16
  33base 8
  11011 base 2

A common convention, and the one used by hex(), oct() and bin() in
Python, is to prefix the non-base-10 representations with "0x" for
base 16, "0o" for base 8 ("o"ctal) and "0b" for base 2 ("b"inary):

  27
  0x1b
  0o33
  0b11011

This allows the human reader or a machine lexer to decide what base
the number is written in, and therefore to figure out what the
underlying numeric value is.

Conversely, consider the bytes object consisting of the values [97,
98, 99, 27, 10]. In ASCII (and UTF-8 and the iso-8859-x encodings)
these may all represent the characters ['a', 'b', 'c', ESC, NL].
So when "printing" a bytes object, which is a sequence of small integers 
representing
values stored in bytes, it is compact to print:

  b'abc\x1b\n'

which is ['a', 'b', 'c', chr(27), newline].

The slosh (\) is the common convention in C-like languages and many
others for representing special characters not directly represents
by themselves. So "\\" for a slosh, "\n" for a newline and "\x1b"
for character 27 (ESC).

The bytes object is still just a sequence on integers, but because
it is very common to have those integers represent text, and very
common to have some text one want represented as bytes in a direct
1:1 mapping, this compact text form is useful and readable. It is
also legal Python syntax for making a small bytes object.

To demonstrate that this is just a _representation_, run this:

  >>> [ i for i in b'abc\x1b\n' ]
  [97, 98, 99, 27, 10]

at an interactive Python 3 prompt. See? Just numbers.

| To encode a number we have to turn it into a string first.
| 
| "16474".encode('utf-8')
| b'16474'
| 
| That 'b' stand for bytes.

Syntactic details. Read this:
  
http://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

| How can i view this byte's object representation as hex() or as bin()?

See above. A bytes is a _sequence_ of values. hex() and bin() print
individual values in hexadecimal or binary respectively. You could
do this:

  for value in b'16474':
print(value, hex(value), bin(value))

Cheers,
-- 
Cameron Simpson 

Uhlmann's Razor: When stupidity is a sufficient explanation, there is no need
 to have recourse to any other.
- Michael M. Uhlmann, assistant attorney general
  for legislation in the Ford Administration
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: My son wants me to teach him Python

2013-06-13 Thread Cameron Simpson
On 12Jun2013 21:47, Rick Johnson  wrote:
| On Wednesday, June 12, 2013 11:08:44 PM UTC-5, Chris Angelico wrote:
| > No. Definitely not. Programming does NOT begin with a GUI. It begins
| > with something *simple*, so you're not stuck fiddling around with the
| > unnecessary. On today's computers, that usually means console I/O
| > (actually console output, with console input coming along much later).
| 
| Chris, you're a dinosaur, only thing is, somebody forgot to tell you.
| 
| *Everything* these days revolves around graphical interfaces.

Oh good.

Please communicate entirely in animations from now on. This text
medium you're using is crude, low bandwidth, and not sparkly enough.

But regrettably, I think Rick may be on the money here. All children
want to write games. I did, my friend's son did, etc. But by starting
with something functional but crude, he will be forced to encounter
the basic tools of coding anyway.

Cheers,
-- 
Cameron Simpson 

This is not my farewell to you. My only wish is to fight as a soldier
in the battle of ideas. I shall continue to write under the heading of
'Reflections by comrade Fidel.' It will be just another weapon you can
count on. Perhaps my voice will be heard. I shall be careful.
- Fidel Castro Ruz, 18feb2008
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A few questiosn about encoding

2013-06-14 Thread Cameron Simpson
On 14Jun2013 11:37, Nikos as SuperHost Support  wrote:
| On 14/6/2013 11:22 πμ, Antoon Pardon wrote:
| 
| >>Python prints numbers:
| >No it doesn't, numbers are abstract concepts that can be represented in
| >various notations, these notations are strings. Those notaional strings
| >end up being printed. As I said before we are so used in using the
| >decimal notation that we often use the notation and the number interchangebly
| >without a problem. But when we are working with multiple notations that
| >can become confusing and we should be careful to seperate numbers from their
| >representaions/notations.
| 
| How do we separate a number then from its represenation-natation?

Shrug. When you "print" a number, Python transcribes a string
representation of it to your terminal.

| What is a notation anywat? is it a way of displayment? but that
| would be a represeantion then

Yep. Same thing. A "notation" is a particulart formal method of
representation.

| No it doesn't, numbers are abstract concepts that can be represented in
| various notations
| 
| >>but when we need a decimal integer
| >
| >There are no decimal integers. There is only a decimal notation of the 
number.
| >Decimal, octal etc are not characteristics of the numbers themselves.
| 
| So everything we see like:
| 
| 16474
| nikos
| abc123
| 
| everything is a string and nothing is a number? not even number 1?

Everything you see like that is textual information. Internally to
Python, various types are used: strings, bytes, integers etc. But
when you print something, text is output.

Cheers,
-- 
Cameron Simpson 

A long-forgotten loved one will appear soon. Buy the negatives at any price.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A few questiosn about encoding

2013-06-14 Thread Cameron Simpson
On 14Jun2013 09:59, Nikos as SuperHost Support  wrote:
| On 14/6/2013 4:00 πμ, Cameron Simpson wrote:
| >On 13Jun2013 17:19, Nikos as SuperHost Support  wrote:
| >| A code-point and the code-point's ordinal value are associated into
| >| a Unicode charset. They have the so called 1:1 mapping.
| >|
| >| So, i was under the impression that by encoding the code-point into
| >| utf-8 was the same as encoding the code-point's ordinal value into
| >| utf-8.
| >|
| >| So, now i believe they are two different things.
| >| The code-point *is what actually* needs to be encoded and *not* its
| >| ordinal value.
| >
| >Because there is a 1:1 mapping, these are the same thing: a code
| >point is directly _represented_ by the ordinal value, and the ordinal
| >value is encoded for storage as bytes.
| 
| So, you are saying that:
| 
| chr(16474).encode('utf-8')   #being the code-point encoded
| 
| ord(chr(16474)).encode('utf-8') #being the code-point's ordinal
| encoded which gives an error.
| 
| that shows us that a character is what is being be encoded to utf-8
| but the character's ordinal cannot.
| 
| So, whay you say "and the ordinal value is encoded for storage
| as bytes." ?

No, I mean conceptually, there is no difference between a codepoint
and its ordinal value. They are the same thing.

Inside Python itself, a character (a string of length 1; there is
no separate character type) is a distinct type. Interally, the
characters in a string are stored numericly. As Unicode codepoints,
as their ordinal values.

It is a meaningful idea to store a Python string encoded into bytes
using some text encoding scheme (utf-8, iso-8859-7, what have you).

It is not a meaningful thing to store a number "encoded" without
some more context. The .encode() method that accepts an encoding
name like "utf-8" is specificly an encoding procedure FOR TEXT.

So strings have such a method, and integers do not.

When you write:

  chr(16474)

you receive a _string_, containing the single character whose ordinal
is 16474. It is meaningful to transcribe this string to bytes using
a text encoding procedure like 'utf-8'.

When you write:

  ord(chr(16474))

you get an integer. Because ord() is the reverse of chr(), you get
the integer 16474.

Integers do not have .encode() methods that accept a _text_ encoding
name like 'utf-8' because integers are not text.

| >| > The leading 0b is just syntax to tell you "this is base 2, not base 8
| >| > (0o) or base 10 or base 16 (0x)". Also, leading zero bits are dropped.
| >|
| >| But byte objects are represented as '\x' instead of the
| >| aforementioned '0x'. Why is that?
| >
| >You're confusing a "string representation of a single number in
| >some base (eg 2 or 16)" with the "string-ish representation of a
| >bytes object".
| 
| >>> bin(16474)
| '0b10001011010'
| that is a binary format string representation of number 16474, yes?

Yes.

| >>> hex(16474)
| '0x405a'
| that is a hexadecimal format string representation of number 16474, yes?

Yes.

| WHILE:
| b'abc\x1b\n' = a string representation of a byte, which in turn is a
| series of integers, so that makes this a string representation of
| integers, is this correct?

A "bytes" Python object. So not "a byte", 5 bytes.
It is a string representation of the series of byte values,
ON THE PREMISE that the bytes may well represent text.
On that basis, b'abc\x1b\n' is a reasonable way to display them.

In other contexts this might not be a sensible way to display these
bytes, and then another format would be chosen, possibly hand
constructed by the programmer, or equally reasonable, the hexlify()
function from the binascii module.

| \x1b = ESC character

Considering the bytes to be representing characters, then yes.

| \ = for seperating bytes

No, \ to introduce a sequence of characters with special meaning.

Normally a character in a b'...' item represents the byte value
matching the character's Unicode ordinal value. But several characters
are hard or confusing to place literally in a b'...' string. For
example a newline character or and escape character.

'a' means 65.
'\n' means 10 (newline, hence the 'n').
'\x1b' means 33 (escape, value 27, value 0x1b in hexadecimal).
And, of course, '\\' means a literal slosh, value 92.

| x = to flag that the following bytes are going to be represented as
| hex values? whats exactly 'x' means here? character perhaps?

A slosh followed by an 'x' means there will be 2 hexadecimal digits
to follow, and those two digits represent the byte value.

So, yes.

| Still its not clear into my head what the differenc

Re: A few questiosn about encoding

2013-06-14 Thread Cameron Simpson
On 14Jun2013 15:59, Nikos as SuperHost Support  wrote:
| So, a numeral = a string representation of a number. Is this correct?

No, a numeral is an individual digit from the string representation of a number.
So: 65 requires two numerals: '6' and '5'.
-- 
Cameron Simpson 

In life, you should always try to know your strong points, but this is
far less important than knowing your weak points.
Martin Fitzpatrick 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A few questiosn about encoding

2013-06-14 Thread Cameron Simpson
On 14Jun2013 16:58, Nikos as SuperHost Support  wrote:
| On 14/6/2013 1:14 μμ, Cameron Simpson wrote:
| >Normally a character in a b'...' item represents the byte value
| >matching the character's Unicode ordinal value.
| 
| The only thing that i didn't understood is this line.
| First please tell me what is a byte value

The numeric value stored in a byte. Bytes are just small integers
in the range 0..255; the values available with 8 bits of storage.

| >\x1b is a sequence you find inside strings (and "byte" strings, the
| >b'...' format).
| 
| \x1b is a character(ESC) represented in hex format

Yes.

| b'\x1b' is a byte object that represents what?

An array of 1 byte, whose value is 0x1b or 27.

| >>> chr(27).encode('utf-8')
| b'\x1b'

Transcribing the ESC Unicode character to byte storage.

| >>> b'\x1b'.decode('utf-8')
| '\x1b'

Reading a single byte array containing a 27 and decoding it assuming 'utf-8'.
This obtains a single character string containing the ESC character.

| After decoding it gives the char ESC in hex format
| Shouldn't it result in value 27 which is the ordinal of ESC ?

When printing strings, the non-printable characters in the string are
_represented_ in hex format, so \x1b was printed.

| > No, I mean conceptually, there is no difference between a code-point
| > and its ordinal value. They are the same thing.
| 
| Why Unicode charset doesn't just contain characters, but instead it
| contains a mapping of (characters <--> ordinals) ?

Look, as far as a computer is concerned a character and an ordinal
are the same thing because you just store character ordinals in
memory when you store a string.

When characters are _displayed_, your Terminal (or web browser or
whatever) takes character ordinals and looks them up in a _font_,
which is a mapping of character ordinals to glyphs (character
images), and renders the character image onto your screen.

| I mean what we do is to encode a character like chr(65).encode('utf-8')
| What's the reason of existence of its corresponding ordinal value
| since it doesn't get involved into the encoding process?

Stop thinking of Unicode code points and ordinal values as separate
things. They are effectively two terms for the same thing. So there
is no "corresponding ordinal value". 65 _is_ the ordinal value.

When you run:

  chr(65).encode('utf-8')

you're going:

  chr(65) ==> 'A'
Producing a string with just one character in it.
Internally, Python stores an array of character ordinals, thus: [65]

  'A'.encode('utf-8')
Walk along all the ordinals in the string and transribe them as bytes.
For 65, the byte encoding in 'utf-8' is a single byte of value 65.
So you get an array of bytes (a "bytes object" in Python), thus: [65]

-- 
Cameron Simpson 

The double cam chain setup on the 1980's DOHC CB750 was another one of
Honda's pointless engineering breakthroughs. You know the cycle (if you'll
pardon the pun :-), Wonderful New Feature is introduced with much fanfare,
WNF is fawned over by the press, WNF is copied by the other three Japanese
makers (this step is sometimes optional), and finally, WNF is quietly dropped
by Honda.
- Blaine Gardner, 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Eval of expr with 'or' and 'and' within

2013-06-14 Thread Cameron Simpson
On 14Jun2013 12:50, Nikos as SuperHost Support  wrote:
| I started another thread because the last one was !@#$'ed up by
| irrelevant replies and was difficult to jeep track.
| 
| >>> name="abcd"
| >>> month="efgh"
| >>> year="ijkl"
| 
| >>> print(name or month or year)
| abcd
| 
| Can understand that, it takes the first string out of the 3 strings
| that has a truthy value.
| 
| >>> print("k" in (name and month and year))
| True
| 
| No clue. since the expression in parenthesis returns 'abcd' how can
| 'k' contained within 'abcd' ?

Did you print the result of "name and month and year"? It is the
_last_ value (if true at all).  You used "or" in the first example
and "and" in the second.

| >>> print(name and month and year)
| ijkl
| 
| Seems here is returning the last string out of 3 strings, but have
| no clue why Python doing this.

To evaluate an "and" it must test all of them to be true, and it
keeps the last value tested.  (Or False, of course, if they are not
all true, in which case Python stops testing at the first False).

But for what you are doing, "and" and "or" are not good operations.

Something like:

  "k" in (name+month+year)

or

  "k" in name or "k" in month or "k" in year

is a more direct and accurate way to write what I imagine you're trying to do.

Cheers,
-- 
Cameron Simpson 

No one is completely worthless...  they can always serve as a bad example.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Eval of expr with 'or' and 'and' within

2013-06-14 Thread Cameron Simpson
On 15Jun2013 01:34, Steven D'Aprano  
wrote:
| On Sat, 15 Jun 2013 00:09:31 +0100, Nobody wrote:
| 
| > On Sat, 15 Jun 2013 03:56:28 +1000, Chris Angelico wrote:
| >> With a few random oddities:
| >>>>> bool(float("nan"))
| >> True
| >> I somehow expected NaN to be false. Maybe that's just my expectations
| >> that are wrong, though.
| > 
| > In general, you should expect the behaviour of NaN to be the opposite of
| > what you expect.
| 
| ... even taking that into account! *wink*
| 
| Everyone is aware that there is more than one NAN, right?

I was not. Interesting.

| If my 
| calculations are correct, there are 9007199254740992 distinct float NANs 
| in Python (although there is no direct way of distinguishing them).

Wouldn't id() do it? At least in terms of telling them apart?
I gather they're not inspectable in Python?

Cheers,
-- 
Cameron Simpson 

2 strokes are quicker than 4.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Don't feed the troll...

2013-06-14 Thread Cameron Simpson
On 15Jun2013 10:42, Ben Finney  wrote:
| "D'Arcy J.M. Cain"  writes:
| Even for those who do participate by email, though, your approach is
| broken:
| > My answer is simple.  Get a proper email system that filters out
| > duplicates.
| 
| The message sent to the individual typically arrives earlier (since it
| is sent straight from you to the individual), and the message on the
| forum arrives later (since it typically requires more processing).
| 
| But since we're participating in the discussion on the forum and not in
| individual email, it is the later one we want, and the earlier one
| should be deleted.

They're the same message! (Delivered twice.) Replying to either is equivalent.
So broadly I don't care which gets deleted; it works regardless.

| So at the point the first message arrives, it isn't a duplicate. The
| mail program will show it anyway, because “remove duplicates” can't
| catch it when there's no duplicate yet.

But it can when the second one arrives. This is true regardless of
the delivery order.

The correct approach it to file the one message wherever it matches.
Message to me+list (I use the list, not the newsgroup) get filed
in my inbox and _also_ in my python folder. I do that based on the
to/cc headers for the most part, so either message's arrival files
the same way.

I delete dups on entry to a mail folder (automatically, of course)
instead of at mail filing time, but the effect is equivalent.

| The proper solution is for you not to send that one at all, and send
| only the message to the forum.

Bah. Plenty of us like both. In the inbox alerts me that someone
replied to _my_ post, and in the python mail gets it nicely threaded.

| You do this by using your mail client's “reply to list” function, which
| uses the RFC 3696 information in every mailing list message.

No need, but a valid option.

| Is there any mail client which doesn't have this function? If so, use
| your vendor's bug reporting system to request this feature as standard,
| and/or switch to a better mail client until they fix that.

Sorry, I could have sworn you said you weren't using a mail client for this...
-- 
Cameron Simpson 

You've read the book. You've seen the movie. Now eat the cast.
- Julian Macassey, describing "Watership Down"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: My son wants me to teach him Python

2013-06-16 Thread Cameron Simpson
On 14Jun2013 20:12, Dennis Lee Bieber  wrote:
| [...] PowerShell has been
| available as a download on WinXP and standard on Win7 [PS 3 is a
| download for Win7, stock on real Win8].
|   While I'm not fluent in it, there are some commands I've gotten
| rather engrained...
| 
| get-childitem -recurse -filter "*.ad*" | select-string -pattern "with"
| 
| finds all the Ada (GNAT convention .ads/.adb) files containing "with"
| statements. And pattern probably is a regex so I could fine tune it to
| just the package withs by using a start of line marker...

Hmm.

  find . -name '*.ad*' | xargs grep with

on almost any UNIX system. There's any number of variations on that
depending on exactly what you want. I'm not going to get sucked
into a UNIX/Windows bickerfest here, but your PowerShell example
isn't winning me over. Sound like they reinvented the wheel. Again,
years later:-(

Cheers,
-- 
Cameron Simpson 

Those who do not understand Unix are condemned to reinvent it, poorly.
- Henry Spencer @ U of Toronto Zoology, he...@zoo.toronto.edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A few questiosn about encoding

2013-06-17 Thread Cameron Simpson
On 17Jun2013 08:49, Antoon Pardon  wrote:
| Op 15-06-13 02:28, Cameron Simpson schreef:
| > On 14Jun2013 15:59, Nikos as SuperHost Support  wrote:
| > | So, a numeral = a string representation of a number. Is this correct?
| >
| > No, a numeral is an individual digit from the string representation of a 
number.
| > So: 65 requires two numerals: '6' and '5'.
|
| Wrong context. A numeral as an individual digit is when you are talking about
| individual characters in a font. In such a context the set of glyphs that
| represent a digit are the numerals.
| 
| However in a context of programming, numerals in general refer to the set of
| strings that represent a number.

No, those are just "numbers" or "numeric strings" (if you're being
overt about them being strings at all). They're "numeric strings"
because they're composed of "numerals". If you think otherwise your
vocabulary needs adjusting. A numeral is a single digit.
-- 
Cameron Simpson 

English is a living language, but simple illiteracy is no basis for
linguistic evolution.   - Dwight MacDonald
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with the "for" loop syntax

2013-06-19 Thread Cameron Simpson
On 20Jun2013 11:09, Chris Angelico  wrote:
| On Thu, Jun 20, 2013 at 11:02 AM, Arturo B  wrote:
| > Fixed, the problem was in
| > HANGMANPICS
| >
| > I didn't open the brackets.
| > Thank you guys :)
| 
| General debugging tip: Syntax errors are sometimes discovered quite
| some way below the actual cause. The easiest way to figure out what's
| the real cause is to start cutting away unnecessary code until all
| that's left is the tiniest part that still has the error. This is also
| very helpful as a prerequisite to posting on a list like this, so even
| if you can't find the problem yourself by this method (you often will,
| though), it's well worth doing.

Also, opening-and-not-closing a set of brackets is almost the only
way in Python to make this kind of error (syntax at one line, actual
mistake far before).

See if your editor has a show-the-matching-bracket mode.

I use vi/vim and it both shows the matching bracket when the cursor
is on one and also have a keystroke to bounce the curser between
this bracket and the matching one.

If you suspect you failed to close a bracket, one approach is to
go _below_ the syntax error (or right on it) and type a closing
bracket. Then see where the editor thinks the opening one is.

If you have a syntax highlighting editor (highlights keywords,
colours quoted text differently, etc) it can also be useful for
finding errors; the text will be the wrong colour at some point.

Just a few suggestions to aid finding this kind of thing without
outside help.

Cheers,
-- 
Cameron Simpson 

Are you experiencing more Windows95 crashes than the norm? How on earth
would you _know_?   - John Brook 
  reporting about a new virus
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with the "for" loop syntax

2013-06-20 Thread Cameron Simpson
On 20Jun2013 15:33, Oscar Benjamin  wrote:
| On 20 June 2013 04:11, Cameron Simpson  wrote:
| > I use vi/vim and it both shows the matching bracket when the cursor
| > is on one and also have a keystroke to bounce the curser between
| > this bracket and the matching one.
| >
| > If you suspect you failed to close a bracket, one approach is to
| > go _below_ the syntax error (or right on it) and type a closing
| > bracket. Then see where the editor thinks the opening one is.
| 
| I use this technique sometimes and it works if the unclosed bracket is
| still in view.

Standing on a bracket, the '%' key jumps to the matching bracket.
No need for it to be in view.

Cheers,
-- 
Cameron Simpson 

"Vy can't ve chust climb?"  - John Salathe
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with the "for" loop syntax

2013-06-20 Thread Cameron Simpson
On 20Jun2013 13:55, Neil Cerutti  wrote:
| On 2013-06-20, Joshua Landau  wrote:
| > On 20 June 2013 04:11, Cameron Simpson  wrote:
| >> Also, opening-and-not-closing a set of brackets is almost the
| >> only way in Python to make this kind of error (syntax at one
| >> line, actual mistake far before).
| >>
| >> See if your editor has a show-the-matching-bracket mode.
| >> If you suspect you failed to close a bracket, one approach is
| >> to go _below_ the syntax error (or right on it) and type a
| >> closing bracket. Then see where the editor thinks the opening
| >> one is.
| >
| > Thanks for that, that's quite an ingenious technique.
| 
| The auto-indent feature of Vim catches this type of syntax error,
| and I imagine other good autoindent support will do the same.

Interesting. I use autoindent but grew up with it for prose.
I hadn't realised vim's support inderstaood python indentation.
I'll have to pay more attention...
-- 
Cameron Simpson 

Do I have it all?  Yes.
Do I want more?  Yeah, sure.- Demi Moore
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python development tools

2013-06-24 Thread Cameron Simpson
On 24Jun2013 14:28, Grant Edwards  wrote:
| On 2013-06-23, cutems93  wrote:
| > I am new to python development and I want to know what kinds of tools
| > people use for python development.
| 
|  1) emacs
|  2) Cpython
|  3) subversion
|  4) http://www.python.org/doc/
|  5) comp.lang.python 

1) vi/vim
2) Cpython
3) mercurial
4) local copy of http://www.python.org/doc/ for python 2 and 3
   (lets me work offline and snappier to browse)
5) python-list@python.org

| 99.9% of the programs I write are command-line tools.

99.9% of the programs I write are command-line tools.

Cheers,
-- 
Cameron Simpson 

The simple truth is that interstellar distances will not fit into the
human imagination. - Douglas Adams
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why is the argparse module so inflexible?

2013-06-27 Thread Cameron Simpson
On 27Jun2013 11:50, Ethan Furman  wrote:
| If the OP is writing an interactive shell, shouldn't `cmd` be used
| instead of `argparse`?  argparse is, after all, intended for
| argument parsing of command line scripts, not for interactive work.

This is specious.

I invoke command line scripts interactively. There's no special case here.

If argparse is raising a useful and inspectable bad argument
exception, why not let it out? Catching that argument and wrapping
it in something opaque seems unhelpful.  After all, if the caller
really wanted to abort on bad arguments the caller would simply not
catch that exception.  There's no need to transmute it into a
whole-program abort.

If Terry's assertion is that the OP has told argparse he never wants
to see bad input, how does one turn that off?

To add to the use case stats, I also subclass cmd and parse interactive
command lines. I'm beginning to be pleased I'm still using Getopt
for that instead of feeling I'm lagging behind the times.

Cheers,
-- 
Cameron Simpson 

If it can't be turned off, it's not a feature. - Karl Heuer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making a pass form cgi => webpy framework

2013-06-27 Thread Cameron Simpson
On 27Jun2013 16:32, Νίκος  wrote:
| a) keep my existing Python cgi way that embed print ''' statements
| within python code to displays mostly tables?

I'd argue against this approach. Like hand constructing SQL, this
is rife with opportunity to make syntax errors, either outright by
mistyping HTML or accidentally by badly escaping content that is
not supposed to be HTML but happens to contain HTML marker characters
like "<".

When I generate HTML I usually make a structure of lists and dicts
that gets converted to HTML. Someone doing a nontrivial amount of
HTML would use a templating system of some kind I imagine; I don't
generate HTML very often.

Pick a simple framework or templating engine and try it. I have no
recommendations to make in this area myself.

Cheers,
-- 
Cameron Simpson 

I'm behind a corporate Exchange Server which seems to have
changed recently to converting everything it sees to HTML. 
How embarrassing. - Dan Espen 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why is the argparse module so inflexible?

2013-06-27 Thread Cameron Simpson
On 27Jun2013 22:49, Steven D'Aprano  
wrote:
| [rant]
| I think it is lousy design for a framework like argparse to raise a 
| custom ArgumentError in one part of the code, only to catch it elsewhere 
| and call sys.exit. At the very least, that ought to be a config option, 
| and off by default.
| 
| Libraries should not call sys.exit, or raise SystemExit. Whether to quit 
| or not is not the library's decision to make, that decision belongs to 
| the application layer. Yes, the application could always catch 
| SystemExit, but it shouldn't have to.

+1
-- 
Cameron Simpson 

There is more stupidity than hydrogen in the universe, and it has a
longer shelf life.  - Frank Zappa
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Stupid ways to spell simple code

2013-06-30 Thread Cameron Simpson
On 30Jun2013 16:06, Chris Angelico  wrote:
| So, here's a challenge: Come up with something really simple, and
| write an insanely complicated - yet perfectly valid - way to achieve
| the same thing. Bonus points for horribly abusing Python's clean
| syntax in the process.

_Must_ you turn this into comp.lang.perl?
Once I was JAPH...
-- 
Cameron Simpson 

Yes, sometimes Perl looks like line-noise to the uninitiated, but to the
seasoned Perl programmer, it looks like checksummed line-noise with a mission
in life.- The Llama Book
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OSError [Errno 26] ?!?!

2013-07-01 Thread Cameron Simpson
On 02Jul2013 06:37, Νίκος  wrote:
| After making a slightly chnage inside my pelatologio.py script
| substituting '*' instead of '-' for no apparent reason i
| receive the following error:
| 
| [Tue Jul 02 06:33:06 2013] [error] [client 46.12.97.148] OSError:
| [Errno 26] \\u0391\\u03c1\\u03c7\\u03b5\\u03af\\u03bf
| \\u03ba\\u03b5\\u03b9\\u03bc\\u03ad\\u03bd\\u03bf\\u03c5
| \\u03c3\\u03b5 \\u03c7\\u03c1

Errno 26 depends on your operating system. See "man 2 intro" for details.
But on my Mac and on a handle Linux system number 26 is:

   26 ETXTBSY Text file busy.  The new process was a pure procedure (shared
 text) file which was open for writing by another process, or
 while the pure procedure file was being executed an open call
 requested write access.

See the os.strerror function for printing the message from an errno number:

  http://docs.python.org/3/library/os.html#os.strerror

I'd say you're trying to write to a file you shouldn't be writing to.
Maybe an executable?

Try to get your script to print out the filename of whatever it was trying to
overwrite before the open() call.

Regarding the characters, I'd say they've been double escaped. Let's undo that:

  [/Users/cameron]fleet*> py3
  Python 3.3.2 (default, May 21 2013, 11:50:47)
  [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on 
darwin
  Type "help", "copyright", "credits" or "license" for more information.
  >>> print('\\u0391\\u03c1\\u03c7\\u03b5\\u03af\\u03bf 
\\u03ba\\u03b5\\u03b9\\u03bc\\u03ad\\u03bd\\u03bf\\u03c5 \\u03c3\\u03b5 
\\u03c7\\u03c1')
  \u0391\u03c1\u03c7\u03b5\u03af\u03bf 
\u03ba\u03b5\u03b9\u03bc\u03ad\u03bd\u03bf\u03c5 \u03c3\u03b5 \u03c7\u03c1
  >>> print('\u0391\u03c1\u03c7\u03b5\u03af\u03bf 
\u03ba\u03b5\u03b9\u03bc\u03ad\u03bd\u03bf\u03c5 \u03c3\u03b5 \u03c7\u03c1')
  Αρχείο κειμένου σε χρ
  >>>

I can't read that, but I'd hope you can.

| What is this OSError and these unicode characters besided it?
| Here is the complete traceback.

A traceback without code isn't terribly useful.

Cheers,
-- 
Cameron Simpson 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OSError [Errno 26] ?!?!

2013-07-02 Thread Cameron Simpson
On 02Jul2013 08:57, Νίκος  wrote:
| Thank you, the error have been caused due to the fact that the
| uploading procedure of my edited 'pelatologio.py' hadn't been
| completed yet, while i was requesting the execution of the script
| via browser.
| 
| I didn't had to do anything it solved by itself, after upload was
| successful and file got  unlocked for access.

If you're uploading using sftp (or, worse, ftp), might I suggest
using rsync instead? Amongst other features, it uploads to a temporary
file and then renames the temporary file to the real file. There is
no window where the "live" file is being written to. The live file
is the old one, and then later it is the new one.

Cheers,
-- 
Cameron Simpson 

No team manager will tell you this; but they all want to see you
come walking back into the pits sometimes, carrying the steering wheel.
- Mario Andretti
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python list code of conduct

2013-07-02 Thread Cameron Simpson
On 02Jul2013 19:46, Roy Smith  wrote:
| In article ,
|  Ned Deily  wrote:
| 
| > If you find a bug in Python, don't send it to comp.lang.python; file 
| > a bug report in the issue tracker.
| 
| I'm not sure I agree with that one, at least not fully.  It's certainly 
| true that you shouldn't expect anybody to do anything about a bug unless 
| you open an issue.
| 
| On the other hand, I often find it useful to discuss things that I 
| believe are bugs on c.l.p first.  Sometimes people will explain to me 
| that I'm just doing it wrong.  Sometimes the discussion will end up 
| with, "Yeah, that's a bug".  In either case, it serves as a good initial 
| filter for whether I should file a bug or not, and the discussion is 
| often educational.

+1

I've certinly been educated that way.
-- 
Cameron Simpson 

Try not to get sucked into the vortex of hell, Billy!
- MST3K, "Megalon vs. Godzilla"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Coping with cyclic imports

2013-07-04 Thread Cameron Simpson
On 04Jul2013 16:03, Oscar Benjamin  wrote:
| On 4 July 2013 13:48,   wrote:
| > On Tuesday, April 8, 2008 10:06:46 PM UTC+2, Torsten Bronger wrote:
| > 
http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python
| 
| Is there some reason you're responding to a post from 5 years ago?

Is there some reason not to, if no newer solutions are available?

Certainly if someone has new input on an old Python thread, which
is legitimately part of the old thread, I am _glad_ if they reply
to the old post. It pulls the whole prior thread content up the top
for my perusal should it be necessary. If they make a new post I
have to go digging through archives if I need to do that.

Cheers,
-- 
Cameron Simpson 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Important features for editors

2013-07-04 Thread Cameron Simpson
[ Digressing to tuning remote access. Sorry. - Cameron ]

On 04Jul2013 21:50, Roy Smith  wrote:
| Does Sublime have some sort of remote mode?  We've got one guy who loves 
| it, but needs to work on a remote machine (i.e. in AWS).  I got X11 
| working for him (he has a Mac desktop), so he can run Sublime on the AWS 
| Linux box and have it display on his Mac desktop, but that's less than 
| ideal.

It's worth pointing out that, depending on the app, X11 can do a
fair bit of traffic. Particularly with more recent things on "rich"
widget libraries with animation or drag'n'drop, it can be quite
painful because the app's developed on someones local desktop where
latency is negligible.

Sometimes it is worth running a desktop local to the remote machine
(eg using vncserver) and operating it remotely via a viewer (eg a
vnc viewer). Although you're now throwign screen updates around as
bitmaps of some flavour, this can decouple you from vile spinning
progress icons and modal drag'n'drop stuff. (This also insulates
you against network drops; just reconnect the viewer, just like
screen or tmux do for terminals.)

Seamonkey, for example, is like molasses done directly with X11 from
a remote host. It used to be snappy, but widget library bling creep
has made it an exercise in pain.

Another alternative, better still if easy, is to mount the remote
system's files on your local machine and run the editor locally.
Snappy response, your native widget-set/look'n'feel, and saving a
file is normally pretty cheap even remotely; that would normally
be your main remote transaction under this model.

Cheers,
-- 
Cameron Simpson 

Ninety percent of everything is crud.   - Theodore Sturgeon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Important features for editors

2013-07-05 Thread Cameron Simpson
On 05Jul2013 05:12, rusi  wrote:
| On Thursday, July 4, 2013 1:37:10 PM UTC+5:30, Göktuğ Kayaalp wrote:
| > Programmability comes to my mind, before anything else.  I'd suggest
| > to find out about designs of Emacs and Vi(m).
| 
| There's one reason I prefer emacs -- and I guess some people
| prefer Idle -- the interpreter and editor are tightly integrated.

That is indeed a strength of emacs over vi.

For myself, I generally don't want to program my editor beyond writing
keyboard macros, and vim's programming interface has yet to attract me.

When I want to manipulate text beyond a simple macro I tend to write
a sed script. Or awk, or python in increasing complexity of task.

[...]
| One expansion for EMACS is Editor for Middle Aged Computer
| Scientists -- so I am guessing if you're asking the question you
| dont qualify :-)

While I started with vi just slightly before encountering emacs
(mid-to-late 1980s, both), my main trouble with choosing emacs was
the heavy use of control keys. Vi's modal nature means that in
"edit" mode, all the keystrokes are available as edit controls.
Emacs' modeless nature means that all the edit controls must be
control-this and meta/escape-that.

For this reason, I often expand EMACS as Escape Meta Alt Control Shift.

I'm a vi user. Once I mastered "hit ESC by reflex when you pause
typing an insert" I was never confused above which mode I was in.

And now my fingers know vi.

Cheers,
-- 
Cameron Simpson 

A novice of the temple once approached the Chief Priest with a question.

  "Master, does Emacs have the Buddha nature?" the novice asked.

  The Chief Priest had been in the temple for many years and could be relied
  upon to know these things.  He thought for several minutes before replying.

  "I don't see why not.  It's got bloody well everything else."

  With that, the Chief Priest went to lunch.  The novice suddenly achieved
enlightenment, several years later.

Commentary:

His Master is kind,
Answering his FAQ quickly,
With thought and sarcasm.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check for threads being finished?

2013-07-05 Thread Cameron Simpson
On 05Jul2013 16:59, Steven D'Aprano  
wrote:
| I have a pool of worker threads, created like this:
| 
| threads = [MyThread(*args) for i in range(numthreads)]
| for t in threads:
| t.start()
| 
| I then block until the threads are all done:
| 
| while any(t.isAlive() for t in threads):
| pass

This spins.

How about:

  for t in threads:
t.join()

Blocks instead of polls.

Cheers,
-- 
Cameron Simpson 

Processes are like potatoes.- NCR device driver manual
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Coping with cyclic imports

2013-07-05 Thread Cameron Simpson
On 05Jul2013 10:36, Oscar Benjamin  wrote:
| On 5 July 2013 02:24, Cameron Simpson  wrote:
| > On 04Jul2013 16:03, Oscar Benjamin  wrote:
| > | Is there some reason you're responding to a post from 5 years ago?
| >
| > Is there some reason not to, if no newer solutions are available?
| 
| No, I was genuinely curious. My way of accessing this
| forum/newsgroup/mailing list doesn't give me a way to respond to very
| old posts but others seem to do it every now and again. I see now that
| if you're looking at an old thread in Google Groups (rather than e.g.
| the python.org archives) it makes the thread seem more like a forum
| than a newsgroup or a mailing list so that it's easy and seems more
| natural to respond to old posts.

Fair enough.

Personally, like others, I stay as far away from GG as possible.

Instead, when I join a mailing list (a far better way to access this IMO),
I pull down the list archives and unpack them into my folder for the list.

That way old posts are right there, ready for access. And if someone
replies to an old thread, I've got the whole thing!

Cheers,
-- 
Cameron Simpson 

It is not a simple life to be a single cell, although I have no right to say
so, having been a single cell so long ago myself that I have no memory at all
of that stage of my life.   - Lewis Thomas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Concurrent writes to the same file

2013-07-10 Thread Cameron Simpson
On 10Jul2013 22:57, Jason Friedman  wrote:
| Other than using a database, what are my options for allowing two processes
| to edit the same file at the same time?  When I say same time, I can accept
| delays.  I considered lock files, but I cannot conceive of how I avoid race
| conditions.

Sure. (Assuming UNIX. Windows probably excludes both processes
having the file open at the one time, but that's not a show stopper.)

You need to use a lock file to ensure only one process accesses the file at a 
time.

While a process holds the lock, access the file.

There are two basic approaches here: 

  take lock
open file for write, modify the file, close it
  release lock

  both processes have the file open for update
take lock
  modify file, flush buffers
release lock

The code I use to take a lockfile is my "lockfile" context manager:

  
https://bitbucket.org/cameron_simpson/css/src/374f650025f156554a986fb3fd472003d2a2519a/lib/python/cs/fileutils.py?at=default#cl-408

An example caller goes:

with lockfile(self.csvpath):
backup = "%s.bak-%s" % (self.csvpath, 
datetime.datetime.now().isoformat())
copyfile(self.csvpath, backup)
write_csv_file(self.csvpath, self.nodedb.nodedata())
if not self.keep_backups:
os.remove(backup)

as shown here:

  
https://bitbucket.org/cameron_simpson/css/src/374f650025f156554a986fb3fd472003d2a2519a/lib/python/cs/nodedb/csvdb.py?at=default#cl-171

Simplify as necessary; you just want:

  with lockfile(mainfile):
modify(mainfile)
if you have no wish for backups in case of a failed modify.

If both processes need to see each other's changes then there's
some more logic needed to monitor the file for changes. I've got
such a scheme for CSV files in beta at present for a personal
project. It won't suit all use cases; mine is well defined.

Cheers,
-- 
Cameron Simpson 

Is it true, Sen. Bedfellow, that your wife rides with bikers?   - Milo Bloom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP8 79 char max

2013-07-30 Thread Cameron Simpson
On 30Jul2013 01:41, Rhodri James  wrote:
| On Tue, 30 Jul 2013 01:11:18 +0100, Joshua Landau  wrote:
| >On 30 July 2013 00:08, Rhodri James  wrote:
| >>I'm working on some shonky C code at the moment that inconsistent
| >>indentation and very long lines.
[...]

Have you tried the indent(1) command?

  DESCRIPTION
 indent is a C program formatter.  It reformats the C program in the
 input_file according to the switches.  The switches which can be speci‐
 fied are described below.  They may appear before or after the file
 names.

Very handy sometimes.

Cheers,
-- 
Cameron Simpson 

The top three answers:  Yes I *am* going to a fire!
Oh! We're using *kilometers* per hour now.
I have to go that fast to get back to my own time.
- Peter Harper 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python script help

2013-07-30 Thread Cameron Simpson
On 30Jul2013 09:12, cool1...@gmail.com  wrote:
| ** urlib, urlib2

Sure. And I'd use BeautifulSoup to do the parse. You'll need to fetch that.
So: urllib[2] to fetch the document and BS to parse it for links,
then urllib[2] to fetch the links you want.

http://www.crummy.com/software/BeautifulSoup/bs4/download/4.0/

Cheers,
-- 
Cameron Simpson 

You can be psychotic and still be competent.
- John L. Young, American Academy of Psychiatry and the Law on Ted
  Kaczynski, and probably most internet users
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP8 79 char max

2013-07-30 Thread Cameron Simpson
On 29Jul2013 16:24, Devyn Collier Johnson  wrote:
| So, I can have a script with large lines and not negatively
| influence performance on systems that do not use punch cards?

Well, running anything will negatively impact the performance of a
system for others...o

Please think about what CPython actually executes, and then try to
figure out for yourself whether the line length or the source code
will affect that execution.

Cheers,
-- 
Cameron Simpson 

"How do you know I'm Mad?" asked Alice.
"You must be," said the Cat, "or you wouldn't have come here."
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 2 + 2 = 5

2012-07-04 Thread Cameron Simpson
On 04Jul2012 19:39, Evan Driscoll  wrote:
| On 7/4/2012 14:37, Paul Rubin wrote:
| > I just came across this (https://gist.github.com/1208215):
| > 
| > import sys
| > import ctypes
| > pyint_p = ctypes.POINTER(ctypes.c_byte*sys.getsizeof(5))
| > five = ctypes.cast(id(5), pyint_p)
| > print(2 + 2 == 5) # False
| > five.contents[five.contents[:].index(5)] = 4
| > print(2 + 2 == 5) # True (must be sufficiently large values of 2 
there...)
| > 
| > Heh.  The author is apparently anonymous, I guess for good reason.
| 
| Probably just nostalgic for old Fortran, which, supposedly, allowed you
| to change the values of literals by passing them to a function by
| reference and then modifying the value.

Yeah, I was thinking that too. Because all parameters were pass-by-reference
in early fortran IIRC. You could, for example, set pi to 3.
-- 
Cameron Simpson 

If I have seen farther than others, it is because I was standing on the
shoulders of giants.- Isaac Newton

If I have not seen as far as others, it is because giants were standing on my
shoulders.  - Hal Abelson

In computer science, we stand on each other's feet. - Brian K. Reed
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-07-09 Thread Cameron Simpson
On 09Jul2012 11:44, Rick Johnson  wrote:
| On Jul 9, 12:40 pm, Tim Chase  wrote:
| > The second[or higher]-order
| > ignorance of not knowing what pdb is (or, if you need more powerful
| > debugging, how to do it) is sign the person hasn't been programming
| > in Python much.
| 
| So guru knowledge of pdb is prerequisite to being accepted as a
| Pythonista? I find that ridiculous since *real* programmers don't use
| debuggers anyway.

You've misread him. He's saying not knowing what PDB is and what it may
be used for is a sign of low experience. Whether one uses it or not
isn't what he's measuring, it's whether one knows what it is for and how
it may be used.

| > [...]  I've seen enough Java-written-in-Python to know what
| > I don't want :-)
| 
| I know you are a member of the group who has an aversion to strict OOP
| paradigm but is this a justified aversion, or just fear of OOP due to
| static evolution? Look, i don't like java's strict approach either,
| however, i do not have an aversion to OOP.

More misreading. "Java-written-in-Python" (and its variants) means non-python
code written in python, in this case from someone with a strong (or
rigid) Java background who is not adept with Python idioms. It has
nothing to do with OOP one way or the other. Surely we've all seen
(and doubtless written) clumsy python code; this is an example.

Cheers,
-- 
Cameron Simpson 

A strong conviction that something must be done is the parent of many
bad measures.   - Daniel Webster
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-07-09 Thread Cameron Simpson
On 09Jul2012 18:53, Devin Jeanpierre  wrote:
| On Mon, Jul 9, 2012 at 5:22 PM, Peter  wrote:
| > One of my favourite questions when interviewing - and it was 100% reliable 
:-) - "what are your hobbies?"
| > If the answer included programming then they were hired, if not, then they 
went to the "B" list.
| 
| Woe is the poor college grad, who wants to appear like a well-rounded
| individual and lists capoeira and gardening, instead.

A new word! A quick google search confused me as to whether this was
martial art or a form of dance. The GF suggested it was both. Having
seen this:

  http://www.youtube.com/watch?v=Z8xxgFpK-NM

I am now convinced :-)
-- 
Cameron Simpson 

Hit the button Chewie!  - Han Solo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I thought I understood how import worked...

2012-08-07 Thread Cameron Simpson
On 07Aug2012 13:52, Steven D'Aprano  
wrote:
| On Tue, 07 Aug 2012 09:18:26 -0400, Roy Smith wrote:
| > I thought modules could not get imported twice.  The first time they get
| > imported, they're cached, and the second import just gets you a
| > reference to the original.  Playing around, however, I see that it's
| > possible to import a module twice if you refer to it by different names.
| 
| Yes. You've found a Python gotcha.
[...]
| > $ cat try.py
| > import broken
| > import foo.broken
| 
| Which are two names for the same module.
[...]

This, I think, is a core issue in this misunderstanding. (I got bitten
by this too, maybe a year ago. My error, and I'm glad to have improved
my understanding.)

All of you are saying "two names for the same module", and variations
thereof. And that is why the doco confuses.

I would expect less confusion if the above example were described as
_two_ modules, with the same source code.

Make it clear that these are _two_ modules (because they have two
names), who merely happen to have been obtained from the same "physical"
filesystem object due to path search effects i.e. change the doco
wording to describe a module as the in-memory result of reading a "file"
found from an import name.

So I think I'm arguing for a small change in terminology in the doco
with no change in Python semantics. Is a module a set of files on the
disc, or an in-memory Python notion with a name? I would argue for the
latter.

With such a change, the "a module can't be imported twice" would then be
true (barring hacking around in sys.modules between imports).

Cheers,
-- 
Cameron Simpson 

As you can see, unraveling even a small part of 'sendmail' can introduce more
complexity than answers.- Brian Costales, _sendmail_
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I thought I understood how import worked...

2012-08-08 Thread Cameron Simpson
On 08Aug2012 14:14, Ben Finney  wrote:
| Cameron Simpson  writes:
| > All of you are saying "two names for the same module", and variations
| > thereof. And that is why the doco confuses.
| >
| > I would expect less confusion if the above example were described as
| > _two_ modules, with the same source code.
| 
| That's not true though, is it? It's the same module object with two
| different references, I thought.

No. They're two in-memory module instantiations with distinct module
names. ISTR that when this got me I had imported the a module in one
place with its full name and elsewhere with a bad relative reference; I
forget the details now. But anyway, that got me the same module code
loaded twice, distinctly named.

| Also, even if what you say were true, “source code” implies the module
| was loaded from source code, when Python allows loading modules with no
| source code available. So that implication just seems to be inviting
| different confusion.

Please let's nail one thing first. We can always quibble about the term "source
code" to prefer "source code or byte code" or something of that ilki,
later.

But the confusion comes from calling these things the "same module".
Which breaks the "modules are only loaded once" mental image.

The confusion would often be avoided if the doco took the line that
it is two modules because they were obtained using two names. Their
original might be the same physical/logical place (such as a module
source code file), but the end result is two modules.

The point is that the "module" term would be better if it referred to the
in-memory Python module, and didn't do double time as meaning the
(typically) source file from which the Python code comes.

Cheers,
-- 
Cameron Simpson 

We had the experience, but missed the meaning.  - T.S. Eliot
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sharing code between different projects?

2012-08-14 Thread Cameron Simpson
On 13Aug2012 17:53, andrea crotti  wrote:
| I am in the situation where I am working on different projects that
| might potentially share a lot of code.
| 
| I started to work on project A, then switched completely to project B
| and in the transiction I copied over a lot of code with the
| corresponding tests, and I started to modify it.
| 
| Now it's time to work again on project A, but I don't want to copy
| things over again.
[...]
| The problem is that there are functions/classes from many domains, so it
| would not make much sense to create a real project, and the only name I
| could give might be "utils or utilities"..
| 
| In plus the moment the code is shared I must take care of versioning and
| how to link different pieces together (we use perforce by the way).
[...]

Having just skimmed this thread, one thing I haven't quite seen suggested is
this:

Really do make a third "utilities" project, and treat "the project" and
"deploy" as separate notions. So to actually run/deploy project A's code
you'd have a short script that copied project A and the utilities project
code into a tree and ran off that. Or even a simple process/script to
update the copy of "utilities" in "project A"'s area.

So you don't "share" code on an even handed basis but import the
"utilities" library into each project as needed.

I do this (one my own very small scale) in one of two ways:

  - as needed, copy the desired revision of utilities into the project's
library space and do perforce's equivalent of Mercurial's addremove
on that library tree (comment "update utilities to revision X").

  - keep a perforce work area for the utilities in your project A area,
where your working project A can hook into it with a symlink or some
deploy/copy procedure as suggested above.
With this latter one you can push back into the utilities library
from your "live" project, because you have a real checkout. So:

  projectAdir
projectA-perforce-checkout
utilities-perforce-checkout
  projectBdir
projectB-perforce-checkout
utilities-perforce-checkout

Personally I become more and more resistent to cut/paste even for small
things as soon as multiple people use it; you will never get to backport
updates to even trivial code to all the copies.

Cheers,
-- 
Cameron Simpson 

The mere existence of a problem is no proof of the existence of a solution.
- Yiddish Proverb
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Flushing buffer on file copy on linux

2012-08-14 Thread Cameron Simpson
On 14Aug2012 22:55, J  wrote:
| Now, the problem I have is that linux tends to buffer data writes to a
| device, and I want to work around that.

To what _specific_ purpose? Benchmarking? Ensuring the device can be
pulled? Ensuring another program can see the data? (The last should be
taken care of regardless of the Linux OS level buffering.)

| When run in normal non-stress
| mode, the program is slow enough that the linux buffers flush and put
| the file on disk before the hash occurs.  However, when run in stress
| mode, what I'm finding is that it appears that the files are possibly
| being hashed while still in the buffer, before being flushed to disk.

You're probably right, but how are you inferring this?

| Generate the parent data file
| hash parent
| instead of copy, open parent and write to a new file object on disk
| with a 0 size buffer
| or flush() before close()
| hash the copy.
| 
| Does that seem reasonable? or is there a cleaner way to copy a file
| from one place to another and ensure the buffers are properly flushed
| (maybe something in os or sys that forces file buffers to be flushed?)

For OS buffers you either want to call sync() (flushes _all_ OS buffers
to disc before returning) or fsync(open-file-handle), which flushes at
least the blocks for that file (in practice, typically everything
outstanding for the same filesystem, alas).

So look at os.fsync in python.

You will need to do this after a python-level data flush but before file
close:

  with open("output-file", "w") as fp:
fp.write(lots of stuff)...
fp.flush()
os.fsync(fp.fileno)

But be clear about your purpose: why do you care that the disc writes
themselve are complete? There are legitimate reasons for this, but
unless it is benchmarking or utterly mad (eg database) data integrity,
they generally aren't:-)

Cheers,
-- 
Cameron Simpson 

English is a living language, but simple illiteracy is no basis for
linguistic evolution.   - Dwight MacDonald
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python 6 compilation failure on RHEL

2012-08-20 Thread Cameron Simpson
On 20Aug2012 12:19, Emile van Sebille  wrote:
| On 8/20/2012 11:37 AM Walter Hurry said...
| > On Mon, 20 Aug 2012 11:02:25 -0700, Emile van Sebille wrote:
| >> On 8/20/2012 10:20 AM Walter Hurry said...
| >>> I concur, but FYI the version of Python with RHEL5 is 2.4. Still, OP
| >>> should stick with that unless there is a pressing reason.
| >>
| >> Hence, the 2.6 install.
| >
| > First, sorry for my omission to trim.
| >
| > Second, the reason for recommending that OP stick to the Red Hat provided
| > version (unless there is a pressing reason) is the question of the
| > already-paid-for Red Hat support.
| 
| Generally, when you compile from source the binaries will install to 
| /usr/local/bin and not be in conflict with RH's install version.

I was going to chime in with this anyway had the thread said nothing; I
strongly prefer to specify --prefix explicitly with configure.

My personal habit to to build with (adjust to match):

  --prefix=/usr/local/python-2.6.4

and put some symlinks in /usr/local/bin afterwards (python2.6, etc).

That way one doesn't tread on the system Python (after all the OS vendor
distro is also a collection of packages with coordinated versions)
and one can easily put in another python beside it.

| > And for that matter, if OP is forced to a later Python 2 version than
| > 2.4, why not 2.7.3?
| 
| Package dependencies.  If the OP intends to install a package that 
| doesn't support other than 2.6, you install 2.6.

Indeed. And this is a strong reason to keep out of the vendor's /usr
filesystem space, also.

Cheers,
-- 
Cameron Simpson 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Make error when installing Python 1.5

2012-08-26 Thread Cameron Simpson
On 27Aug2012 01:54, Steven D'Aprano  
wrote:
| Yes, you read the subject line right -- Python 1.5. Yes, I am nuts ;)
| 
| (I like having old versions of Python around for testing historical 
| behaviour.)
| 
| On Debian squeeze, when I try to build Python 1.5, I get this error:
| 
| fileobject.c:590: error: conflicting types for ‘getline’
| /usr/include/stdio.h:651: note: previous declaration of ‘getline’ was here
| make[1]: *** [fileobject.o] Error 1
| make[1]: Leaving directory `/home/steve/personal/python/Python-1.5.2/
| Objects'
| make: *** [Objects] Error 2
[...]

I would take the compile line for fileobject.c and supposing it to look
like this:

  gcc -c -blah fileobject.c

run it by hand as:

  gcc -E -blah fileobject.c >fileobject.cpp

and examine the preprocessor output (initially with grep, then with
vi/emacs).

I would expect the lines for getline to be different, with some macro
definition occuring between the first and second occurence.

Cheers,
-- 
Cameron Simpson 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6 and Sqlite3 - Slow

2012-08-28 Thread Cameron Simpson
On 27Aug2012 13:41, bruceg113...@gmail.com  wrote:
| When using the database on my C Drive, Sqlite performance is great!   (<1S)
| When using the database on a network, Sqlite performance is terrible! (17S)

Let me first echo everyone saying not to use SQLite on a network file.

| I like your idea of trying Python 2.7

I doubt it will change anything.

| Finally, the way my program is written is:
|   loop for all database records:
|  read a database record
|  process data
|  display data (via wxPython)
| 
| Perhaps, this is a better approach:
|  read all database records
|  loop for all records:
| process data
| display data (via wxPython)

Yes, provided the "read all database records" is a single select
statement. In general, with any kind of remote resource you want to
minimise the number of transactions - the to and fro part, because each
such item tends to have latency while something is sent to and again
receiving from. So if you can say "gimme all the records" you get one
"unit" of latency at the start and end, versus latency around each
record fetch.

Having said all that, because SQLite works directly against the file, if
you say to it "giev me all the records" and the file is remote, SQLite
will probably _still_ fetch each record individually internally, gaining
you little.

This is why people are suggesting a database "server": then you can say
"get me all the records" over the net, and the server does
local-to-the-server file access to obtain the data. So all the "per
record" latency is at its end, and very small. Not to mention any
cacheing it may do.

Of course, if your requirements are very simple you might be better off
with a flat text file, possibly in CSV format, and avoid SQLite
altogether.

Cheers,
-- 
Cameron Simpson 

I do not trust thee, Cage from Hell, / The reason why I cannot tell, /
But this I know, and know full well: / I do not trust thee, Cage from Hell.
- Leigh Ann Hussey, leigh...@sybase.com, DoD#5913
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending USB commands with Python

2012-08-29 Thread Cameron Simpson
On 29Aug2012 17:57, Dennis Lee Bieber  wrote:
| On Wed, 29 Aug 2012 14:21:30 -0700 (PDT), "Adam W."
|  declaimed the following in
| gmane.comp.python.general:
| > You are correct about the 2 being the number of bytes written.  However 
when I issue a read command I get:
| > 
| > >>> ep.write('\x1BA')
| > 4
| 
|   That's interesting -- as if each byte you send is expanding into a
| pair of bytes.

UTF-16? ISTR that Windows often uses big endian UTF-16 for filenames and
text data; could there be some default encoding in ep.write getting in
your way?

Disclaimer: I'm really not a Windows guy.
-- 
Cameron Simpson 

There are too many people now for everyone to be entitled to his own opinion.
- Dr. Handelman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending USB commands with Python

2012-08-29 Thread Cameron Simpson
On 30Aug2012 08:29, I wrote:
| UTF-16? ISTR that Windows often uses big endian UTF-16 [...]

Sorry, little-endian. Anyway...
-- 
Cameron Simpson 

Ed Campbell's  pointers for long trips:
3. Stop and take a break before you really need it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending USB commands with Python

2012-08-30 Thread Cameron Simpson
On 30Aug2012 05:51, Adam W.  wrote:
| On Thursday, August 30, 2012 12:55:14 AM UTC-4, Dennis Lee Bieber wrote:
| > How many bytes did it claim to send?
|
| 11, which is what I expected.  But I changed the byte value to 16
| (because I was having trouble getting single digit hex values working
| in the command) and sent this command:
|
| >>> for x in range(0,500):
|   
ep.write(b'\x16\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF')
| 
| it respond with 500 17's and prints a black bar!  So it looks like whatever 
concern you had with using encode was coming to fruition.

Yeah. Try 'iso8859-1' instead of 'utf-8'. You want to be not translating
the byte values at all. UTF-8 encodes character values over 127 as multibyte
sequences. ISO8859-1 is a 256 code set that does no translation -
character codes in go directly to byte values out.

You're speaking a binary protocol, not text, so you want a one to one
mapping. Better still would be to be using a bytes I/O layer instead of
one with a text->byte translation; I do not know if the USB library
you're using offers such. So try 'iso8859-1'; at least the translation
is a no-op.

Cheers,
-- 
Cameron Simpson 

B1FF is an archetype, and all you're showing us is one of the more amusing of
his many instantiations.- Howard E. Motteler 
Ah, perhaps Arthur Clarke anticipated this in his celebrated short story,
"The Nine Million Names Of B1FF"? - Nosy 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bitshifts and "And" vs Floor-division and Modular

2012-09-06 Thread Cameron Simpson
On 07Sep2012 01:30, Mark Lawrence  wrote:
| On 07/09/2012 01:01, jimbo1qaz wrote:
| > Is it faster to use bitshifts or floor division? And which is better, & or 
%?
| > All divisors and mods are power of 2, so are binary operations faster? And 
are they considered bad style?
| 
| Why don't you use the timeit module and find out for yourself?

Because timeit doesn't output style advice?
Because timeit won't offer even a short single parapgraph description
of how python ints (even just in CPython) are implemented and how that
may affect performance in general?

To the OP: personally, I would suggest using % when I am thinking of
division and a bit shift when I am thinking of a bitshift, and only reach
for timeit when performance becomes an issue. Code for the algoritm,
and only optimise later.

Of course only a well run benchmark will measure the real world, but it
possible to address his other questions in a helpful fashion and address
the benchmark question in a less offputting tone. If you can't be
bothered, please don't. (Especially since these irritating posts from
you are usually in response to a post you feel could have used more
effort from the OP.)

Nobody answers all performance considerations or design choices with an
exhaustive timeit benchmark, and it is silly to suggest so. It is
helpful for people to have a mental model of the python internals
so they can make often-sensible choices from the start.

So try being helpful instead of slapping people down when they haven't
reached your private bar.

Cheers,
-- 
Cameron Simpson 

Microsoft: Where do you want to go today?
UNIX:  Been there, done that!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: AttributeError: 'list' object has no attribute 'lower'

2012-09-08 Thread Cameron Simpson
On 08Sep2012 13:45, Roy Smith  wrote:
| First, I don't understand this code:
| 
| In article ,
|  Token Type  wrote:
| > synset_list = list(wn.all_synsets(pos))
| > lemma_list = [synset.lemma_names for synset in synset_list]
| 
| It looks like you're taking an iterable, converting it to a list, just 
| so you can iterate over it again.  Why not the simpler:
| 
| > lemma_list = [synset.lemma_names for synset in wn.all_synsets(pos)]

Speaking for myself, when I write something like that it is because I
need to iterate over it twice or more. Often I'll make a tuple instead
of a list in that case, too, to avoid certain types of accidents.

| ?  But, I'm also confused about what lemma_list is supposed to end up 
| being.  The name "lemma_names" is plural, making me think it returns a 
| list of something.  And then you build those up into a list of lists?
| 
| In fact, I'm guessing that's your problem.  I think you're ending up 
| with a list of lists of strings, when you think you're getting a list of 
| strings.

In my case, I have most often had this error (.lower or its
equivalent) when I've accidentally converted a string into a list
of characters; easy to do because strings are themselves iterables,
yielding a sequence of single character strings:-)

It is usually an accident from getting my nesting wrong somewhere.

| My suggestion is to print out all the intermediate data structures 
| (synset_list, lemma_list, etc) and see what they look like.  If the 
| structures are simple, just plain print will work, but for more 
| complicated structures, pprint.pprint() is a life saver.
| 
| Another possibility is to assert that things are what you expect them to 
| be.  Something like:
| 
| assert isinstance(synset_list, list)
| assert isinstance(lemma_list, list)
| assert isinstance(lemma_list[0], str)
| 
| and so on.

+1 to all of this, too.

Cheers,
-- 
Cameron Simpson 

Too much of a good thing is never enough.   - Luba
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   8   9   10   >