Python Crypto Singature do not support Android???

2014-09-05 Thread Frank Liou
I use Privatekey sign a signature by android 

and then send to python 

python can't use public key to verify

and 

python signature result all is number

but 

android is number and english words

is that no support??
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Crypto Singature do not support Android???

2014-09-05 Thread Chris Angelico
On Fri, Sep 5, 2014 at 5:02 PM, Frank Liou  wrote:
> I use Privatekey sign a signature by android
>
> and then send to python
>
> python can't use public key to verify
>
> and
>
> python signature result all is number
>
> but
>
> android is number and english words
>
> is that no support??

It would help if you showed us (a) the code, (b) the two different
results, and (c) what you're expecting to see.

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


Re: Python Crypto Singature do not support Android???

2014-09-05 Thread Frank Liou
Sorry!!

here is my code

python :

msg = '123'
msg = msg.encode('utf-8')
digest = SHA.new(msg).digest()
signature = APP_key_Private.sign(digest, '')

signature like: 
(331360957518977045630977695238805536647918381415993574748735382369846464411185614465200440681176208702632880466448650921484169461348176316490985530702996236304709994452069542950422484358331316454332743470132105629096577439312314814238554088472504815623363978006284897433036223490623530076474911740732410,)


and android :

InputStream inPrivate = getResources().getAssets().open("PR1.pem");
PrivateKey privateKey = RSAUtils.loadPrivateKey(inPrivate);
Log.d("privateKey--",privateKey.toString());

 byte[] a = Sign.digest("123".getBytes());
  String test_a_to64  = Base64.encodeToString(a,Base64.DEFAULT);
  Log.d("X--",test_a_to64);
  Log.d("sdfgserthwrthhsrethwegerg--",Arrays.toString(a));

String sdf = Sign.sign(a,privateKey);
 Log.d("0--",Arrays.toString(sdf.getBytes()));

Android like:


[34, 68, 28, 72, -43, 7, -64, -127, -119, -44, 61, -53, -22, 112, -74, -122, 
-19, -127, -120, -43, 1, -55, 66, -47, -63, -128, 104, -97, 104, 65, -48, -120, 
-50, 117, 85, 86, -33, 37, -77, -26, -46, -59, -43, -42, -79, -9, -78, -26, 
-25, 5, 4, -104, 95, -120, -73, 123, 70, -11, 56, -10, -99, -126, -30, -3, -63, 
42, 45, 33, 36, 23, -52, 10, -4, -17, 8, 82, -61, 74, 43, -5, 44, 29, 127, 78, 
100, -88, -24, -61, -24, 3, -105, 4, 117, 71, 41, 74, 99, 23, 68, 112, -8, 
-105, 67, -68, -115, -27, -24, 114, -108, 28, 15, -85, -77, 49, -110, -10, -83, 
-58, -27, 34, 90, -52, 71, -72, 73, -5, -2, -56]



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


Run an http server on Apache without wsgi?

2014-09-05 Thread Frank Millman
Hi all

My AccInABox project runs an http server. Previously I used the cherry.py 
wsgiserver, but I have now switched to asyncio.

I tried to use the wsgi interface, but I have a particular requirement and 
found that wsgi got in the way, so I dropped it and now handle all requests 
directly.

Now the question arises, what if someone wants to run my program under 
Apache or nginx? I have had some feedback from a sysadmin, whose main 
concern is how to deploy my program, which is something I have not given 
serious thought to.

If the answer is that I really should be using wsgi, I will explain my 
specific requirement in more detail, and see if anyone can suggest a 
workaround.

But at this stage, I would like to discuss whether it is possible to avoid 
using wsgi. Should I be looking at FastCGI, or is that now considered 
outdated?

I would appreciate any pointers or links to get me started.

Thanks

Frank Millman



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


Re: Python Crypto Singature do not support Android???

2014-09-05 Thread Chris Angelico
On Fri, Sep 5, 2014 at 5:23 PM, Frank Liou  wrote:
> here is my code
>
> python :
>
> msg = '123'
> msg = msg.encode('utf-8')
> digest = SHA.new(msg).digest()
> signature = APP_key_Private.sign(digest, '')

That code is incomplete, I can't run it. Ditto your Java code. But
what seems to be happening is that you're getting data back in two
very different formats, plus you're generating one signature based on
a UTF-8 stream and one based on a Base-64 stream. Start by writing
*simple* code that does what you want, and then porting that to the
other language exactly.

Also, please don't use Google Groups, or if you must, please remember
to include context.

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


Re: How to turn a string into a list of integers?

2014-09-05 Thread cl
Joshua Landau  wrote:
> On 3 September 2014 15:48,   wrote:
> > Peter Otten <__pete...@web.de> wrote:
> >> >>> [ord(c) for c in "This is a string"]
> >> [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103]
> >>
> >> There are other ways, but you have to describe the use case and your Python
> >> version for us to recommend the most appropriate.
> >>
> > That looks OK to me.  It's just for outputting a string to the block
> > write command in python-smbus which expects an integer array.
> 
> Just be careful about Unicode characters.

I have to avoid them completely because I'm sending the string to a
character LCD with a limited 8-bit only character set.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


reading file in for loop

2014-09-05 Thread loial
If I read a file using a for loop, as follows, is the file left open if I 
execute a break in the for loop?


for line in open(myFile).readlines():

if something:
break


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


Re: reading file in for loop

2014-09-05 Thread Chris Angelico
On Fri, Sep 5, 2014 at 8:20 PM, loial  wrote:
> If I read a file using a for loop, as follows, is the file left open if I 
> execute a break in the for loop?
>
>
> for line in open(myFile).readlines():
>
> if something:
> break

It'll be closed when the object expires. With CPython, that's probably
going to happen fairly soon, but it's not guaranteed in any way. If
you want to be sure it's closed, use a 'with' statement - look it up
in the docs.

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


Re: reading file in for loop

2014-09-05 Thread Peter Otten
loial wrote:

> If I read a file using a for loop, as follows, is the file left open if I
> execute a break in the for loop?
> 
> 
> for line in open(myFile).readlines():
> 
> if something:
> break

Whether the file is left open has nothing to do with how you leave the loop. 
The way you wrote it (readlines() reads all lines into a list and there is 
no reference to the file object left) CPython will close the file before you 
enter the loop:

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for line in open("tmp.txt").readlines():
... raw_input("you are here")
... break
... 
you are here
[1]+  Angehalten  python
$ lsof *

Other implementations take a bit more time:

$ jython
"my" variable $jythonHome masks earlier declaration in same scope at 
/usr/bin/jython line 15.
Jython 2.5.3 (, Nov 21 2013, 23:15:42) 
[OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_65
Type "help", "copyright", "credits" or "license" for more information.
>>> for line in open("tmp.txt"): pass

... 
>>> 
[2]+  Angehalten  jython
$ lsof *
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFFNODE NAME
java9396 x   34r   REG   8,23   17 5906455 tmp.txt
$ 


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


“Eric Python 3 (& 2) IDE” Tech.Doc: How useful?

2014-09-05 Thread Pietro Moras
So to schedule my next year's “free” (in all senses) activity, I'd appreciate 
your remarks about your perceived practical usefulness of the Tech. 
Documentation of Eric advanced Python IDE, as available at URL:  
http://eric-ide.python-projects.org/eric-documentation.html 
For private answers:  [Studio-PM  hotmail  com]. Critical remarks 
welcome too. Thanks. - P.M.-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Raspberry pi, python and robotics

2014-09-05 Thread BartC



"Gregory Ewing"  wrote in message
news:c6nv2hf8q6...@mid.individual.net...

Rob Gaddi wrote:

otherwise getting up and
running will cost you a solid $1200 just in gear.


While having fancy gear certainly helps, it's not
*strictly* necessary. When I first started dabbling
in electronics, the most sophisticated piece of
equipment I had was an analog multimeter.

It got me through a lot of projects, including a
couple of homebrew computers. It's surprising how
much you can deduce about what's happening in a
digital circuit by watching a needle bouncing
around!


I also used LEDs (even showing complex signals, as they will dim in
different ways, or you could sometimes view them through a flipped mirror to
show the patterns).

Sometimes I fed a digital output to the video input of my TV (it was 5V to
1V, some care was needed), and the waveform became visible, although
unsynchronised to anything. With one line representing about 50us, you could
get a lot of clues about what was going on. (What I never tried with a TV,
because I later had a scope, was to arrange for the line-sync to trigger
some repeatable event I was trying to monitor. By using it as the /reset
signal of a microprocessor for example.)

Anyway, there were all sorts of tricks. I didn't have a proper knowledge of
(analogue) electronics either, just enough to get by, or picked up what I
needed as I went. (You tended to copy or adapt someone else's circuits.)

Besides, a normal non-storage oscilloscope wasn't directly useful for the
complex non-repeating signals you get with computer boards.

--
Bartc 


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


Re: I have tried and errored a reasonable amount of times

2014-09-05 Thread Grant Edwards
On 2014-09-05, Denis McMahon  wrote:
> On Thu, 04 Sep 2014 21:42:56 +1000, Chris Angelico wrote:
>> On Thu, Sep 4, 2014 at 9:17 PM, Denis McMahon  
>> wrote:
>>> On Wed, 03 Sep 2014 07:16:34 +, Steven D'Aprano wrote:
>>>
 Who uses + for disjunction (∨ OR) and concatenation for conjunction
 (∧ AND)? That's crazy notation.
>>>
>>> The way I was taught it in the mid 1980s, a.b === a and b, a+b === a
>>> or b.
>> 
>> The middle dot is another notation for multiplication, as is abuttal
>> (not actually concatenation, in this context). So they're all saying
>> the same thing: boolean 'and' is multiplication, boolean 'or' is
>> addition.
>
> Yes Chris, I know that. I was responding to Stephen's statement that 
> using + for or was crazy notation, and pointing out that 30 years
> ago, that's what UK colleges were teaching!

AFAIK, that's the standard notation in both CS and EE university
classes in the US also: + for 'or' and dot or abuttal for 'and'.

Well, it was 30 years ago...

-- 
Grant Edwards   grant.b.edwardsYow! But was he mature
  at   enough last night at the
  gmail.comlesbian masquerade?
-- 
https://mail.python.org/mailman/listinfo/python-list


My backwards logic

2014-09-05 Thread Seymore4Head
I'm still doing practice problems.  I haven't heard from the library
on any of the books I have requested.

http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html

This is not a hard problem, but it got me to thinking a little.  A
prime number will divide by one and itself.  When setting up this
loop, if I start at 2 instead of 1, that automatically excludes one of
the factors.  Then, by default, Python goes "to" the chosen count and
not "through" the count, so just the syntax causes Python to rule out
the other factor (the number itself).

So this works:
while True:
a=random.randrange(1,8)
print (a)
for x in range(2,a):
if a%x==0:
print ("Number is not prime")
break
wait = input (" "*40  + "Wait")

But, what this instructions want printed is "This is a prime number"
So how to I use this code logic NOT print (not prime) and have the
logic print "This number is prime"

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


Re: I have tried and errored a reasonable amount of times

2014-09-05 Thread Marko Rauhamaa
Grant Edwards :

> Who uses + for disjunction (∨ OR) and concatenation for
> conjunction (∧ AND)? That's crazy notation.
>
> AFAIK, that's the standard notation in both CS and EE university
> classes in the US also: + for 'or' and dot or abuttal for 'and'.

Besides, it's no crazier for Boolean algebra than real algebra. For some
reason, mathematicians use + for addition and concatenation for
multiplication...


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


Re: My backwards logic

2014-09-05 Thread MRAB

On 2014-09-05 17:48, Seymore4Head wrote:

I'm still doing practice problems.  I haven't heard from the library
on any of the books I have requested.

http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html

This is not a hard problem, but it got me to thinking a little.  A
prime number will divide by one and itself.  When setting up this
loop, if I start at 2 instead of 1, that automatically excludes one of
the factors.  Then, by default, Python goes "to" the chosen count and
not "through" the count, so just the syntax causes Python to rule out
the other factor (the number itself).

So this works:
while True:
 a=random.randrange(1,8)
 print (a)
 for x in range(2,a):
 if a%x==0:
 print ("Number is not prime")
 break
 wait = input (" "*40  + "Wait")

But, what this instructions want printed is "This is a prime number"
So how to I use this code logic NOT print (not prime) and have the
logic print "This number is prime"


Look for a factor. If you don't find one, it's a prime number.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Storing instances using jsonpickle

2014-09-05 Thread MRAB

On 2014-09-04 06:17, Chris Angelico wrote:

On Thu, Sep 4, 2014 at 9:39 AM, MRAB  wrote:

I occasionally think about a superset of JSON, called, say, "pyson" ...
ah, name already taken! :-(


While I'm somewhat sympathetic to the concept, there are some parts of
your description that I disagree with. Am I misreading something? Are
there typos in the description and I'm making something out of
nothing?


It would add tuples, delimited by (...), which are not used otherwise (no
expressions):

() => ()
(0, ) => (0)


This seems odd. Part of JSON's convenience is that it's a subset of
JavaScript syntax, so you can just plop a block of JSON into a REPL
and it'll decode correctly. With PyON (or whatever you call it), it'd
be nice to have the same correspondence; for a start, I would strongly
encourage the "trailing comma is permitted" rule (so [1,2,3,] is
equivalent to [1,2,3]), and then I'd have the default encoding for a
single-element tuple include that trailing comma. If (0) is a
one-element tuple, you end up with a subtle difference between a PyON
decode and the Python interpreter, which is likely to cause problems.
It might even be worth actually mandating (not just encouraging) that
one-element tuples have the trailing comma, just to prevent that.


[snip]

JSON has 'true' and 'false'.

Python has 'True' and 'False'.

Therefore, if you want it to be able to drop it into Python's REPL, it
won't be compatible with JSON anyway! (Well, not unless you define
'true' and 'false' first.)

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


Re: My backwards logic

2014-09-05 Thread Bob Gailer
Bob gailer
On Sep 5, 2014 12:51 PM, "Seymore4Head" 
wrote:
>
> I'm still doing practice problems.  I haven't heard from the library
> on any of the books I have requested.
>
>
http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html
>
> This is not a hard problem, but it got me to thinking a little.  A
> prime number will divide by one and itself.  When setting up this
> loop, if I start at 2 instead of 1, that automatically excludes one of
> the factors.  Then, by default, Python goes "to" the chosen count and
> not "through" the count, so just the syntax causes Python to rule out
> the other factor (the number itself).
>
> So this works:
> while True:
> a=random.randrange(1,8)
> print (a)
> for x in range(2,a):
> if a%x==0:
> print ("Number is not prime")
> break
   else:
   print ...
> wait = input (" "*40  + "Wait")
>
> But, what this instructions want printed is "This is a prime number"
> So how to I use this code logic NOT print (not prime) and have the
> logic print "This number is prime"
>
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread Ethan Furman

On 09/05/2014 09:48 AM, Seymore4Head wrote:

I'm still doing practice problems.  I haven't heard from the library
on any of the books I have requested.

http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html

This is not a hard problem, but it got me to thinking a little.  A
prime number will divide by one and itself.  When setting up this
loop, if I start at 2 instead of 1, that automatically excludes one of
the factors.  Then, by default, Python goes "to" the chosen count and
not "through" the count, so just the syntax causes Python to rule out
the other factor (the number itself).

So this works:
while True:
 a=random.randrange(1,8)
 print (a)
 for x in range(2,a):
 if a%x==0:
 print ("Number is not prime")
 break
 wait = input (" "*40  + "Wait")

But, what this instructions want printed is "This is a prime number"
So how to I use this code logic NOT print (not prime) and have the
logic print "This number is prime"


Python's 'for' loop has a handy 'else' extension which is perfect for the 
search-type of 'for' loop:

   while True:
a=random.randrange(1,8)
print (a)
for x in range(2,a):
if a%x==0:
print ("Number is not prime")
break
else:
print ("Number is prime")
wait = input (" "*40  + "Wait")

Note the two lines I added after the 'break' and before the 'wait'.

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


Re: My backwards logic

2014-09-05 Thread Chris Kaynor
On Fri, Sep 5, 2014 at 9:48 AM, Seymore4Head 
wrote:

> I'm still doing practice problems.  I haven't heard from the library
> on any of the books I have requested.
>
>
> http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html
>
> This is not a hard problem, but it got me to thinking a little.  A
> prime number will divide by one and itself.  When setting up this
> loop, if I start at 2 instead of 1, that automatically excludes one of
> the factors.  Then, by default, Python goes "to" the chosen count and
> not "through" the count, so just the syntax causes Python to rule out
> the other factor (the number itself).
>
> So this works:
> while True:
> a=random.randrange(1,8)
> print (a)
> for x in range(2,a):
> if a%x==0:
> print ("Number is not prime")
> break
> wait = input (" "*40  + "Wait")
>
> But, what this instructions want printed is "This is a prime number"
> So how to I use this code logic NOT print (not prime) and have the
> logic print "This number is prime"
>
>
One neat feature of Python is the for...else function. The code inside the
else block will run only if the loop ran to completion (untested code
follows):

while True:
a=random.randrange(1,8)
print (a)
for x in range(2,a):
if a%x==0:
print ("Number is not prime")
break
else:
print("Number is prime")
wait = input (" "*40  + "Wait")


Depending on how advanced you want to get (I know you are relatively new to
Python), another decent way would be to extract the prime check to a
function, and return the result, and print based on that result (again,
untested code):

def isPrime(number):
for x in range(2,a):
if a%x==0:
return False
return True

while True:
a=random.randrange(1,8)
print (a)
if isPrime(a):
print("Number is prime")
else:
print("Number is not prime")
wait = input (" "*40  + "Wait")
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread John Gordon
In <1enj0att6bkrnvb81rhma5dbuk3h28a...@4ax.com> Seymore4Head 
 writes:

> I'm still doing practice problems.  I haven't heard from the library
> on any of the books I have requested.

> http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html

> This is not a hard problem, but it got me to thinking a little.  A
> prime number will divide by one and itself.  When setting up this
> loop, if I start at 2 instead of 1, that automatically excludes one of
> the factors.  Then, by default, Python goes "to" the chosen count and
> not "through" the count, so just the syntax causes Python to rule out
> the other factor (the number itself).

> So this works:
> while True:
> a=random.randrange(1,8)
> print (a)
> for x in range(2,a):
> if a%x==0:
> print ("Number is not prime")
> break
> wait = input (" "*40  + "Wait")

> But, what this instructions want printed is "This is a prime number"
> So how to I use this code logic NOT print (not prime) and have the
> logic print "This number is prime"


There are two basic tactics you can use:

1. Initialize an "isprime" flag to True at the top of the while loop.  
   In the for loop, replace the print statement with a statement that
   sets isprime to False.  After the for loop, insert a check on isprime,
   and print "This number is prime" if isprime is still True.

2. Create a separate function just for testing if a number is prime, which
   returns True or False.  Then call that function within your while loop.

-- 
John Gordon Imagine what it must be like for a real medical doctor to
gor...@panix.comwatch 'House', or a real serial killer to watch 'Dexter'.

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


Re:My backwards logic

2014-09-05 Thread Dave Angel
Seymore4Head  Wrote in message:
> I'm still doing practice problems.  I haven't heard from the library
> on any of the books I have requested.
> 
> http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html
> 
> This is not a hard problem, but it got me to thinking a little.  A
> prime number will divide by one and itself.  When setting up this
> loop, if I start at 2 instead of 1, that automatically excludes one of
> the factors.  Then, by default, Python goes "to" the chosen count and
> not "through" the count, so just the syntax causes Python to rule out
> the other factor (the number itself).
> 
> So this works:
> while True:
> a=random.randrange(1,8)
> print (a)
> for x in range(2,a):
> if a%x==0:
> print ("Number is not prime")
> break
> wait = input (" "*40  + "Wait")
> 
> But, what this instructions want printed is "This is a prime number"
> So how to I use this code logic NOT print (not prime) and have the
> logic print "This number is prime"
> 
> 

The traditional way of telling whether something happened in a
 loop is to set a flag to False outside the loop,  and
 conditionally set it to True in the if test. Then after the loop,
 check your flag.

Python however has a better way. You can put an else clause on the
 for loop:


 for x in range(2,a):
if a%x==0:
   print ("Number is not prime")
   break
  else:
print ("Number is prime")

The else clause fires if no break executed. 

There are also ways to do it using not, any, and a list
 comprehension, no explicit loop at all.

-- 
DaveA

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


Re: My backwards logic

2014-09-05 Thread Ian Kelly
On Fri, Sep 5, 2014 at 11:08 AM, Ethan Furman  wrote:
> Python's 'for' loop has a handy 'else' extension which is perfect for the
> search-type of 'for' loop:
>
>while True:
> a=random.randrange(1,8)
> print (a)
> for x in range(2,a):
> if a%x==0:
> print ("Number is not prime")
> break
> else:
> print ("Number is prime")
> wait = input (" "*40  + "Wait")
>
> Note the two lines I added after the 'break' and before the 'wait'.

Also note that this construct tends to be particularly sensitive to
indentation. If you accidentally indent the else block one level too
many (which your editor may well do for you to "helpfully" match it up
with the if), then you'll get a completely different result.

I would not worry about the else clause as a beginner, as it's
relatively unique to Python and tends to be somewhat confusing. Use a
flag or refactor the function instead.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Storing instances using jsonpickle

2014-09-05 Thread Marko Rauhamaa
MRAB :

> Therefore, if you want it to be able to drop it into Python's REPL, it
> won't be compatible with JSON anyway! (Well, not unless you define
> 'true' and 'false' first.)

Sigh. I was so hopeful JSON would be great. Unfortunately, it flopped by
requiring the parser to heuristically support 5 encoding systems.

Thus, ast.literal_eval() is superior to anything JSON has to offer.


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


Re: Storing instances using jsonpickle

2014-09-05 Thread Ned Batchelder

On 9/5/14 1:16 PM, Marko Rauhamaa wrote:

MRAB :


Therefore, if you want it to be able to drop it into Python's REPL, it
won't be compatible with JSON anyway! (Well, not unless you define
'true' and 'false' first.)


Sigh. I was so hopeful JSON would be great. Unfortunately, it flopped by
requiring the parser to heuristically support 5 encoding systems.


I don't understand how JSON has flopped?  The parser may be a bit more 
complex (but not much, it isn't hard to examine the first few bytes), 
but you're using off-the-shelf parsers anyway, so why are you concerned 
by this?




Thus, ast.literal_eval() is superior to anything JSON has to offer.


Marko




--
Ned Batchelder, http://nedbatchelder.com

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


Re: My backwards logic

2014-09-05 Thread Juan Christian
I made this code just for fun and learning, it's working, but would this be
a good approach? Thanks.

import sys


def prime_checker(start = 1, stop = 1):
for number in range(start, stop + 1):
divisors = [(number % x) for x in range(1, number + 1)]
print("{n} prime? {r}".format(n = number, r = (divisors.count(0) == 2)))


if __name__ == '__main__':
prime_checker(int(sys.argv[1]), int(sys.argv[2]))


On Fri, Sep 5, 2014 at 2:17 PM, Ian Kelly  wrote:

> On Fri, Sep 5, 2014 at 11:08 AM, Ethan Furman  wrote:
> > Python's 'for' loop has a handy 'else' extension which is perfect for the
> > search-type of 'for' loop:
> >
> >while True:
> > a=random.randrange(1,8)
> > print (a)
> > for x in range(2,a):
> > if a%x==0:
> > print ("Number is not prime")
> > break
> > else:
> > print ("Number is prime")
> > wait = input (" "*40  + "Wait")
> >
> > Note the two lines I added after the 'break' and before the 'wait'.
>
> Also note that this construct tends to be particularly sensitive to
> indentation. If you accidentally indent the else block one level too
> many (which your editor may well do for you to "helpfully" match it up
> with the if), then you'll get a completely different result.
>
> I would not worry about the else clause as a beginner, as it's
> relatively unique to Python and tends to be somewhat confusing. Use a
> flag or refactor the function instead.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread Seymore4Head
On Fri, 05 Sep 2014 10:08:18 -0700, Ethan Furman 
wrote:

>On 09/05/2014 09:48 AM, Seymore4Head wrote:
>> I'm still doing practice problems.  I haven't heard from the library
>> on any of the books I have requested.
>>
>> http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html
>>
>> This is not a hard problem, but it got me to thinking a little.  A
>> prime number will divide by one and itself.  When setting up this
>> loop, if I start at 2 instead of 1, that automatically excludes one of
>> the factors.  Then, by default, Python goes "to" the chosen count and
>> not "through" the count, so just the syntax causes Python to rule out
>> the other factor (the number itself).
>>
>> So this works:
>> while True:
>>  a=random.randrange(1,8)
>>  print (a)
>>  for x in range(2,a):
>>  if a%x==0:
>>  print ("Number is not prime")
>>  break
>>  wait = input (" "*40  + "Wait")
>>
>> But, what this instructions want printed is "This is a prime number"
>> So how to I use this code logic NOT print (not prime) and have the
>> logic print "This number is prime"
>
>Python's 'for' loop has a handy 'else' extension which is perfect for the 
>search-type of 'for' loop:
>
>while True:
> a=random.randrange(1,8)
> print (a)
> for x in range(2,a):
> if a%x==0:
> print ("Number is not prime")
> break
> else:
> print ("Number is prime")
> wait = input (" "*40  + "Wait")
>
>Note the two lines I added after the 'break' and before the 'wait'.

I had already tried this one.
The solution I want should only print:
"This number is prime"

Adding else causes the program to also print "This number is not
prime"

I also tried the flag=True suggestion, but never got one that worked.
I am unsure when to use flag=True and flag==True 
Then there is flag="True" and flag=="True"

What I really wanted was something like:
if !(a%x==0)

BTW since I am getting no grade, I much prefer the answer than a hint.
The best hint IMO is to tell me how you would do it.

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


Re: How to turn a string into a list of integers?

2014-09-05 Thread Kurt Mueller
Am 05.09.2014 um 10:42 schrieb c...@isbd.net:

> Joshua Landau  wrote:
>> On 3 September 2014 15:48,   wrote:
>>> Peter Otten <__pete...@web.de> wrote:
>>> [ord(c) for c in "This is a string"]
 [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103]
 
 There are other ways, but you have to describe the use case and your Python
 version for us to recommend the most appropriate.
 
>>> That looks OK to me.  It's just for outputting a string to the block
>>> write command in python-smbus which expects an integer array.
>> 
>> Just be careful about Unicode characters.
> 
> I have to avoid them completely because I'm sending the string to a
> character LCD with a limited 8-bit only character set.


Could someone please explain the following behavior to me:
Python 2.7.7, MacOS 10.9 Mavericks

>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> [ord(c) for c in 'AÄ']
[65, 195, 132]
>>> [ord(c) for c in u'AÄ']
[65, 196]

My obviously wrong understanding:
‚AÄ‘ in ‚ascii‘ are two characters
 one with ord A=65 and
 one with ord Ä=196 ISO8859-1 
 —-> why [65, 195, 132]
u’AÄ’ is an Unicode string
 —-> why [65, 196]

It is just the other way round as I would expect.



Thank you
-- 
Kurt Mueller, kurt.alfred.muel...@gmail.com

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


Re: Storing instances using jsonpickle

2014-09-05 Thread Marko Rauhamaa
Ned Batchelder :

> I don't understand how JSON has flopped? The parser may be a bit more
> complex (but not much, it isn't hard to examine the first few bytes),
> but you're using off-the-shelf parsers anyway, so why are you
> concerned by this?

There are occasions where you need to take shortcuts. Say you need to
glean information from JSON with bash, grep, awk or straight C. If JSON
was fixed to UTF-8, that would be quite feasible. Being as it is, you
are bound to 3rd-party libraries.

That alone invites ad-hoc encodings. For example, I have run into
"asterisked" JSON, libraries that limit themselves to UTF-8.

Compare that with HTTP, SMTP, or even XML(!). They fix the encoding to
the bit. No need for completely unnecessary options.


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


Re: My backwards logic

2014-09-05 Thread Chris Kaynor
On Fri, Sep 5, 2014 at 10:44 AM, Seymore4Head 
wrote:

> On Fri, 05 Sep 2014 10:08:18 -0700, Ethan Furman 
> wrote:
>
> >On 09/05/2014 09:48 AM, Seymore4Head wrote:
> >> I'm still doing practice problems.  I haven't heard from the library
> >> on any of the books I have requested.
> >>
> >>
> http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html
> >>
> >> This is not a hard problem, but it got me to thinking a little.  A
> >> prime number will divide by one and itself.  When setting up this
> >> loop, if I start at 2 instead of 1, that automatically excludes one of
> >> the factors.  Then, by default, Python goes "to" the chosen count and
> >> not "through" the count, so just the syntax causes Python to rule out
> >> the other factor (the number itself).
> >>
> >> So this works:
> >> while True:
> >>  a=random.randrange(1,8)
> >>  print (a)
> >>  for x in range(2,a):
> >>  if a%x==0:
> >>  print ("Number is not prime")
> >>  break
> >>  wait = input (" "*40  + "Wait")
> >>
> >> But, what this instructions want printed is "This is a prime number"
> >> So how to I use this code logic NOT print (not prime) and have the
> >> logic print "This number is prime"
> >
> >Python's 'for' loop has a handy 'else' extension which is perfect for the
> search-type of 'for' loop:
> >
> >while True:
> > a=random.randrange(1,8)
> > print (a)
> > for x in range(2,a):
> > if a%x==0:
> > print ("Number is not prime")
> > break
> > else:
> > print ("Number is prime")
> > wait = input (" "*40  + "Wait")
> >
> >Note the two lines I added after the 'break' and before the 'wait'.
>
> I had already tried this one.
> The solution I want should only print:
> "This number is prime"
>
> Adding else causes the program to also print "This number is not
> prime"
>

If you do not want it to print the "Number is not prime", just remove the
print("Number is not prime") line.


> I also tried the flag=True suggestion, but never got one that worked.
> I am unsure when to use flag=True and flag==True
> Then there is flag="True" and flag=="True"
>

Generally, you would want to use:

flag = False

 before the loop, and

flag = True

inside the loop (once you know the number is not prime). After the loop,
you would typically just use:

if flag: # This could be "if flag == True:", however the plain "if flag:"
is more Pythonic.

print("Number is prime")


The "=" operator is assignment - storing a new value.
The "==" operator is for equality checking. The inverse is the "!="
operator which checks for inequality.

What I really wanted was something like:
> if !(a%x==0)


In this case, that will not work, as you only know that the number is prime
after checking all the values, which means after the loop has completed.

Ignoring the fact that it would not function for this use case, the proper
Python syntax for that would be one of:

   - if a%x != 0: # Probably the clearest for this case.
   - if not (a%x==0):
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to turn a string into a list of integers?

2014-09-05 Thread Chris “Kwpolska” Warrick
On Sep 5, 2014 7:57 PM, "Kurt Mueller" 
wrote:
> Could someone please explain the following behavior to me:
> Python 2.7.7, MacOS 10.9 Mavericks
>
> >>> import sys
> >>> sys.getdefaultencoding()
> 'ascii'
> >>> [ord(c) for c in 'AÄ']
> [65, 195, 132]
> >>> [ord(c) for c in u'AÄ']
> [65, 196]
>
> My obviously wrong understanding:
> ‚AÄ‘ in ‚ascii‘ are two characters
>  one with ord A=65 and
>  one with ord Ä=196 ISO8859-1 
>  —-> why [65, 195, 132]
> u’AÄ’ is an Unicode string
>  —-> why [65, 196]
>
> It is just the other way round as I would expect.

Basically, the first string is just a bunch of bytes, as provided by your
terminal — which sounds like UTF-8 (perfectly logical in 2014).  The second
one is converted into a real Unicode representation. The codepoint for Ä is
U+00C4 (196 decimal). It's just a coincidence that it also matches latin1
aka ISO 8859-1 as Unicode starts with all 256 latin1 codepoints. Please
kindly forget encodings other than UTF-8.

BTW: ASCII covers only the first 128 bytes.

--
Chris “Kwpolska” Warrick 
Sent from my Galaxy S3.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread Ethan Furman

On 09/05/2014 10:17 AM, Ian Kelly wrote:


I would not worry about the else clause as a beginner, as it's
relatively unique to Python and tends to be somewhat confusing. Use a
flag or refactor the function instead.


I don't disagree with this, but early exposure to "for..else is for search loops" is a good thing.  I still get tripped 
up by this as I didn't learn it that way.


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


Re: My backwards logic

2014-09-05 Thread MRAB

On 2014-09-05 18:35, Juan Christian wrote:

I made this code just for fun and learning, it's working, but would this
be a good approach? Thanks.

import sys


def prime_checker(start = 1, stop = 1):


In Python, the standard is to use a half-open range.


for number in range(start, stop + 1):
divisors = [(number % x) for x in range(1, number + 1)]
print("{n} prime? {r}".format(n = number, r = (divisors.count(0) == 
2)))


You want to know only whether it's prime, so why continue looking after
finding a factor?


if __name__ == '__main__':
prime_checker(int(sys.argv[1]), int(sys.argv[2]))


[snip]

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


Re: Anyway to reduce size of pdf using python script.

2014-09-05 Thread kekeje
On Friday, February 1, 2013 8:03:41 PM UTC-5, access...@gmail.com wrote:
> I have a batch file that exports ArcGIS pdf maps to a directory. I would like 
> to include a step in the script where the pdf file is reduced in size instead 
> of manually opening each file in Acrobat X Pro after the script has run and 
> doing it there.
> 
> 
> 
> Can this be done using python scripting or does the automation stop at 
> exporting the map?
> 
> 
> 
> Thanks



Have you found the solution? I am having exactly the same problem and happen to 
see your post here. Please let me know how you did it, thanks! 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to turn a string into a list of integers?

2014-09-05 Thread Kurt Mueller

Am 05.09.2014 um 20:25 schrieb Chris “Kwpolska” Warrick :
> On Sep 5, 2014 7:57 PM, "Kurt Mueller"  wrote:
> > Could someone please explain the following behavior to me:
> > Python 2.7.7, MacOS 10.9 Mavericks
> >
> > >>> import sys
> > >>> sys.getdefaultencoding()
> > 'ascii'
> > >>> [ord(c) for c in 'AÄ']
> > [65, 195, 132]
> > >>> [ord(c) for c in u'AÄ']
> > [65, 196]
> >
> > My obviously wrong understanding:
> > ‚AÄ‘ in ‚ascii‘ are two characters
> >  one with ord A=65 and
> >  one with ord Ä=196 ISO8859-1 
> >  —-> why [65, 195, 132]
> > u’AÄ’ is an Unicode string
> >  —-> why [65, 196]
> >
> > It is just the other way round as I would expect.
> 
> Basically, the first string is just a bunch of bytes, as provided by your 
> terminal — which sounds like UTF-8 (perfectly logical in 2014).  The second 
> one is converted into a real Unicode representation. The codepoint for Ä is 
> U+00C4 (196 decimal). It's just a coincidence that it also matches latin1 aka 
> ISO 8859-1 as Unicode starts with all 256 latin1 codepoints. Please kindly 
> forget encodings other than UTF-8.

So:
‘AÄ’ is an UTF-8 string represented by 3 bytes:
A -> 41   -> 65  first byte decimal
Ä -> c384 -> 195 and 132 second and third byte decimal

u’AÄ’ is an Unicode string represented by 2 bytes?:
A -> U+0041 -> 65 first byte decimal, 00 is omitted or not yielded by ord()?
Ä -> U+00C4 -> 196 second byte decimal, 00 is ommited or not yielded by ord()?


> BTW: ASCII covers only the first 128 bytes.

ACK
-- 
Kurt Mueller, kurt.alfred.muel...@gmail.com

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


Re: Anyway to reduce size of pdf using python script.

2014-09-05 Thread Tim
On Friday, February 1, 2013 8:03:41 PM UTC-5, access...@gmail.com wrote:
> I have a batch file that exports ArcGIS pdf maps to a directory. I would like 
> to include a step in the script where the pdf file is reduced in size instead 
> of manually opening each file in Acrobat X Pro after the script has run and 
> doing it there.
> 
> Can this be done using python scripting or does the automation stop at 
> exporting the map?
> 
> Thanks

I don't have a definitive answer but I use qpdf to optimize my pdf files. 
Whether that reduces the size I'm not sure. http://qpdf.sourceforge.net/

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


Re: My backwards logic

2014-09-05 Thread Juan Christian
What's [snip] ??


On Fri, Sep 5, 2014 at 3:48 PM, MRAB  wrote:

> On 2014-09-05 18:35, Juan Christian wrote:
>
>> I made this code just for fun and learning, it's working, but would this
>> be a good approach? Thanks.
>>
>> import sys
>>
>>
>> def prime_checker(start = 1, stop = 1):
>>
>
> In Python, the standard is to use a half-open range.
>
>  for number in range(start, stop + 1):
>> divisors = [(number % x) for x in range(1, number + 1)]
>> print("{n} prime? {r}".format(n = number, r =
>> (divisors.count(0) == 2)))
>>
>>  You want to know only whether it's prime, so why continue looking after
> finding a factor?
>
>>
>> if __name__ == '__main__':
>> prime_checker(int(sys.argv[1]), int(sys.argv[2]))
>>
>>  [snip]
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread Mark Lawrence

On 05/09/2014 20:34, Juan Christian wrote:

What's [snip] ??



As in cut out or chopped out such that some of the original text has 
been removed.  And please don't top post here, thanks.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: My backwards logic

2014-09-05 Thread Paul Rubin
Juan Christian  writes:
>  I made this code just for fun and learning, it's working, but would
>  this be a good approach? Thanks. ...
>  ** ** for number in range(start, stop + 1):
>  ** ** ** ** divisors = [(number % x) for x in range(1, number + 1)]
>  ** ** ** ** ** ** print("{n} prime? {r}".format(n = number, r =
>  (divisors.count(0) == 2)))

1. Mathematically it's better to stop checking once you've gone past
the square root of the number, since if n = p*q and is composite,
then at least one of p,q will be <= sqrt(n).

2. As a program design matter, it's generally preferable for functions
like this to not print the answer.  They should just return a value, in
this case a bool indicating whether the number is prime.  That makes it
easier to re-use the function, to wrap it in a unit test framework, etc.
If you want to print the result, then call the function and have the
caller print the returned value.

3. As a simple optimization, once you have checked that the number is
not divisible by 2, you don't have to check for other even divisors.
So just check for 2, then the odd numbers 3,5,7...

This is a little bit fancy but I like to use itertools for these
sorts of problems:

  from itertools import chain, takewhile, count

  def is_prime(n):
  if n < 2: return False
  candidates = chain([2], count(3,2))
  return all(n%d!=0 for d in takewhile(lambda d: d*d<=n, candidates))

If you're not familiar with those functions, "chain" concatenates
two sequences, and "count(n,i)" gives an increasing sequence starting
with n and incrementing by i.  So chain([2], count(3,2)) gives
the unending sequence [2, 3, 5, 7, 9, ...] which is the numbers
you want to check against.  Then the takewhile expression chops
that sequence once it gets past sqrt(n), and "all" returns true iff
all of the candidate divisors fail to divide the number.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to turn a string into a list of integers?

2014-09-05 Thread Kurt Mueller
Am 05.09.2014 um 21:16 schrieb Kurt Mueller :
> Am 05.09.2014 um 20:25 schrieb Chris “Kwpolska” Warrick :
>> On Sep 5, 2014 7:57 PM, "Kurt Mueller"  wrote:
>>> Could someone please explain the following behavior to me:
>>> Python 2.7.7, MacOS 10.9 Mavericks
>>> 
>> import sys
>> sys.getdefaultencoding()
>>> 'ascii'
>> [ord(c) for c in 'AÄ']
>>> [65, 195, 132]
>> [ord(c) for c in u'AÄ']
>>> [65, 196]
>>> 
>>> My obviously wrong understanding:
>>> ‚AÄ‘ in ‚ascii‘ are two characters
>>> one with ord A=65 and
>>> one with ord Ä=196 ISO8859-1 
>>> —-> why [65, 195, 132]
>>> u’AÄ’ is an Unicode string
>>> —-> why [65, 196]
>>> 
>>> It is just the other way round as I would expect.
>> 
>> Basically, the first string is just a bunch of bytes, as provided by your 
>> terminal — which sounds like UTF-8 (perfectly logical in 2014).  The second 
>> one is converted into a real Unicode representation. The codepoint for Ä is 
>> U+00C4 (196 decimal). It's just a coincidence that it also matches latin1 
>> aka ISO 8859-1 as Unicode starts with all 256 latin1 codepoints. Please 
>> kindly forget encodings other than UTF-8.
> 
> So:
> ‘AÄ’ is an UTF-8 string represented by 3 bytes:
> A -> 41   -> 65  first byte decimal
> Ä -> c384 -> 195 and 132 second and third byte decimal
> 
> u’AÄ’ is an Unicode string represented by 2 bytes?:
> A -> U+0041 -> 65 first byte decimal, 00 is omitted or not yielded by ord()?
> Ä -> U+00C4 -> 196 second byte decimal, 00 is ommited or not yielded by ord()?

After reading the ord() manual:
The second case should read:
u’AÄ’ is an Unicode string represented by 2 unicode characters:
If Python was built with UCS2 Unicode, then the character’s code point must
be in the range [0..65535, 16 bits, U-..U-]
A -> U+0041 ->  65 first  character decimal (code point)
Ä -> U+00C4 -> 196 second character decimal (code point)


Am I right now?

-- 
Kurt Mueller, kurt.alfred.muel...@gmail.com

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


Re: Storing instances using jsonpickle

2014-09-05 Thread Ned Batchelder

On 9/5/14 2:04 PM, Marko Rauhamaa wrote:

Ned Batchelder :


I don't understand how JSON has flopped? The parser may be a bit more
complex (but not much, it isn't hard to examine the first few bytes),
but you're using off-the-shelf parsers anyway, so why are you
concerned by this?


There are occasions where you need to take shortcuts. Say you need to
glean information from JSON with bash, grep, awk or straight C. If JSON
was fixed to UTF-8, that would be quite feasible. Being as it is, you
are bound to 3rd-party libraries.

That alone invites ad-hoc encodings. For example, I have run into
"asterisked" JSON, libraries that limit themselves to UTF-8.

Compare that with HTTP, SMTP, or even XML(!). They fix the encoding to
the bit. No need for completely unnecessary options.


I see what you mean about JSON, but you are mistaken about HTTP and XML. 
Neither of them dictates the encoding of the data, and both of them 
offer ways to declare the encoding.  This means XML parsers must be 
prepared for many different encodings.





Marko




--
Ned Batchelder, http://nedbatchelder.com

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


Re: Storing instances using jsonpickle

2014-09-05 Thread Marko Rauhamaa
Ned Batchelder :

> I see what you mean about JSON, but you are mistaken about HTTP and
> XML. Neither of them dictates the encoding of the data, and both of
> them offer ways to declare the encoding. This means XML parsers must
> be prepared for many different encodings.

You can rest assured that the line

   GET / HTTP/1.1

is strictly 8-bit ASCII.

I was intentionally exaggerating about XML, but I have yet to encounter
non-UTF-8 XML in the wild.


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


Re: My backwards logic

2014-09-05 Thread Ian Kelly
On Fri, Sep 5, 2014 at 11:44 AM, Seymore4Head
 wrote:
> BTW since I am getting no grade, I much prefer the answer than a hint.
> The best hint IMO is to tell me how you would do it.

from math import ceil, sqrt

def is_prime(n):
if n < 2:
return False
if n % 2 == 0:
return n == 2
return all(n % x != 0 for x in range(3, int(sqrt(n)) + 1, 2))

while True:
n = int(input("Enter a number: "))
if is_prime(n):
print("{} is prime.".format(n))
else:
print("{} is not prime.".format(n))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread Seymore4Head
On Fri, 05 Sep 2014 12:48:56 -0400, Seymore4Head
 wrote:

>I'm still doing practice problems.  I haven't heard from the library
>on any of the books I have requested.
>
>http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html
>
>This is not a hard problem, but it got me to thinking a little.  A
>prime number will divide by one and itself.  When setting up this
>loop, if I start at 2 instead of 1, that automatically excludes one of
>the factors.  Then, by default, Python goes "to" the chosen count and
>not "through" the count, so just the syntax causes Python to rule out
>the other factor (the number itself).
>
>So this works:
>while True:
>a=random.randrange(1,8)
>print (a)
>for x in range(2,a):
>if a%x==0:
>print ("Number is not prime")
>break
>wait = input (" "*40  + "Wait")
>
>But, what this instructions want printed is "This is a prime number"
>So how to I use this code logic NOT print (not prime) and have the
>logic print "This number is prime"

I am sure this has already been done, but after it was pointed out
that you don't need to test for any number that multiplies by 2 it
made me think again.

If you start with the list [3,5,7] and step through the list of all
remaining odd numbers (step 2), and start appending numbers that won't
divide by numbers already appended in the list, that would seem like a
pretty efficient way to find all prime numbers.

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


Re: O'Reilly Python Certification

2014-09-05 Thread julian
On Wednesday, September 3, 2014 5:53:12 PM UTC-4, jaron...@gmail.com wrote:
> Ethan, Steve, Tim, and others:
> 
> 
> 
> I'm thinking of taking the program. How long, in hours, does it take to 
> complete all four Python courses?

I'm currently taking the first out of four modules. I have extensive PHP 
knowledge and I also have a certification by Zend so programming is not new to 
me. I've never done anything at all with Python.

I've already completed 12 out of the 16 lessons and so far the reading has 
taken me from 15 to 30 minutes and the code assignments no more than 10. There 
was one exception that took me 30 minutes as I wasn't sure what the hell they 
were asking (it was a very obscure and useless algorithm which hasn't been the 
case on any of the other assignments). On top of that you have to answer 2 
quizzes and each quiz usually has 2 questions (no more than 5). Those usually 
take 1 to 3 minutes max as they are mostly syntax or concepts but not coding. 

As I said, this is only true for the first module as I haven't taken the others 
yet. But if you already know programming and have a few years of coding 
experience you will breeze through the first module in probably 3 days 
(assuming you only do that). The syllabus for the other modules suggests (to 
me) that going so fast would not be that easy.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread Chris Kaynor
On Fri, Sep 5, 2014 at 2:49 PM, Seymore4Head 
wrote:

> On Fri, 05 Sep 2014 12:48:56 -0400, Seymore4Head
>  wrote:
>
> >I'm still doing practice problems.  I haven't heard from the library
> >on any of the books I have requested.
> >
> >
> http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html
> >
> >This is not a hard problem, but it got me to thinking a little.  A
> >prime number will divide by one and itself.  When setting up this
> >loop, if I start at 2 instead of 1, that automatically excludes one of
> >the factors.  Then, by default, Python goes "to" the chosen count and
> >not "through" the count, so just the syntax causes Python to rule out
> >the other factor (the number itself).
> >
> >So this works:
> >while True:
> >a=random.randrange(1,8)
> >print (a)
> >for x in range(2,a):
> >if a%x==0:
> >print ("Number is not prime")
> >break
> >wait = input (" "*40  + "Wait")
> >
> >But, what this instructions want printed is "This is a prime number"
> >So how to I use this code logic NOT print (not prime) and have the
> >logic print "This number is prime"
>
> I am sure this has already been done, but after it was pointed out
> that you don't need to test for any number that multiplies by 2 it
> made me think again.
>
> If you start with the list [3,5,7] and step through the list of all
> remaining odd numbers (step 2), and start appending numbers that won't
> divide by numbers already appended in the list, that would seem like a
> pretty efficient way to find all prime numbers.
>

To be correct, you only need to check for n being divisible by primes less
than sqrt(n). For example, the following code will produce a list of primes
from 2 to 1000 (the result will be in the "primes" list):

import math
primes = [2]
for i in range(3, 1000):
end = math.sqrt(i)
for x in primes:
if x > end: # Once x is larger than the sqrt(i), we know it must be
prime, so we can early exit.
#print(i, "is a prime number")
primes.append(i)
break
if (i % x) == 0:
#print(i, "is a composite number")
break
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Storing instances using jsonpickle

2014-09-05 Thread Irmen de Jong
On 5-9-2014 19:16, Marko Rauhamaa wrote:

> Thus, ast.literal_eval() is superior to anything JSON has to offer.

Incidentally,

I've made a serialization library based on Python's literal expressions.
It uses ast.literal_eval() to deserialize, and a bit of custom code to 
serialize Python
objects:   https://pypi.python.org/pypi/serpent


Irmen

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


Re: My backwards logic

2014-09-05 Thread Ian Kelly
On Fri, Sep 5, 2014 at 3:49 PM, Seymore4Head
 wrote:
> I am sure this has already been done, but after it was pointed out
> that you don't need to test for any number that multiplies by 2 it
> made me think again.
>
> If you start with the list [3,5,7] and step through the list of all
> remaining odd numbers (step 2), and start appending numbers that won't
> divide by numbers already appended in the list, that would seem like a
> pretty efficient way to find all prime numbers.

Indeed, this type of algorithm is known as a sieve. For a (literally)
classical example, see
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

It's a nice way to find all the prime numbers up to a given limit, or
it can be modified to run indefinitely high, but it's not very
efficient for determining the primality of a single number.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread Seymore4Head
On Fri, 5 Sep 2014 15:14:41 -0700, Chris Kaynor
 wrote:

>On Fri, Sep 5, 2014 at 2:49 PM, Seymore4Head 
>wrote:
>
>> On Fri, 05 Sep 2014 12:48:56 -0400, Seymore4Head
>>  wrote:
>>
>> >I'm still doing practice problems.  I haven't heard from the library
>> >on any of the books I have requested.
>> >
>> >
>> http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html
>> >
>> >This is not a hard problem, but it got me to thinking a little.  A
>> >prime number will divide by one and itself.  When setting up this
>> >loop, if I start at 2 instead of 1, that automatically excludes one of
>> >the factors.  Then, by default, Python goes "to" the chosen count and
>> >not "through" the count, so just the syntax causes Python to rule out
>> >the other factor (the number itself).
>> >
>> >So this works:
>> >while True:
>> >a=random.randrange(1,8)
>> >print (a)
>> >for x in range(2,a):
>> >if a%x==0:
>> >print ("Number is not prime")
>> >break
>> >wait = input (" "*40  + "Wait")
>> >
>> >But, what this instructions want printed is "This is a prime number"
>> >So how to I use this code logic NOT print (not prime) and have the
>> >logic print "This number is prime"
>>
>> I am sure this has already been done, but after it was pointed out
>> that you don't need to test for any number that multiplies by 2 it
>> made me think again.
>>
>> If you start with the list [3,5,7] and step through the list of all
>> remaining odd numbers (step 2), and start appending numbers that won't
>> divide by numbers already appended in the list, that would seem like a
>> pretty efficient way to find all prime numbers.
>>
>
>To be correct, you only need to check for n being divisible by primes less
>than sqrt(n). For example, the following code will produce a list of primes
>from 2 to 1000 (the result will be in the "primes" list):
>
>import math
>primes = [2]
>for i in range(3, 1000):
>end = math.sqrt(i)
>for x in primes:
>if x > end: # Once x is larger than the sqrt(i), we know it must be
>prime, so we can early exit.
>#print(i, "is a prime number")
>primes.append(i)
>break
>if (i % x) == 0:
>#print(i, "is a composite number")
>break

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


Re: My backwards logic

2014-09-05 Thread Seymore4Head
On Fri, 5 Sep 2014 16:35:18 -0600, Ian Kelly 
wrote:

>On Fri, Sep 5, 2014 at 3:49 PM, Seymore4Head
> wrote:
>> I am sure this has already been done, but after it was pointed out
>> that you don't need to test for any number that multiplies by 2 it
>> made me think again.
>>
>> If you start with the list [3,5,7] and step through the list of all
>> remaining odd numbers (step 2), and start appending numbers that won't
>> divide by numbers already appended in the list, that would seem like a
>> pretty efficient way to find all prime numbers.
>
>Indeed, this type of algorithm is known as a sieve. For a (literally)
>classical example, see
>http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
>
>It's a nice way to find all the prime numbers up to a given limit, or
>it can be modified to run indefinitely high, but it's not very
>efficient for determining the primality of a single number.

That is a pretty nice example.
Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread Chris Angelico
On Sat, Sep 6, 2014 at 3:44 AM, Seymore4Head
 wrote:
> BTW since I am getting no grade, I much prefer the answer than a hint.
> The best hint IMO is to tell me how you would do it.

But for your own learning, it's still better for you to do things
yourself. Giving you the answer doesn't teach you nearly as
effectively as giving you a hint and having you work out the answer
yourself.

But telling you how we'd do it can be useful too, especially when we
get down to the detaily bits where there are lots of ways to do
things. For instance, there's been a suggestion made a few times to
break the primality test out into a function... but compare these two:

def isprime(n):
   """Return True iff n is prime"""

def find_factor(n):
"""Return a factor of n, or None if n is prime"""

Aside from inverting the truth value, these are both tests of
primality - you can write "if isprime(n):" or "if find_factor(n):" and
they'll both work. (The latter is actually "iscomposite".) But the
second function is giving you a bit more information - it tells you
what factor it found.

As a general rule, try to avoid throwing information away. To prove
that n is composite, your function found a factor. Returning that,
rather than a boolean value, might make your function more useful; and
it's zero cost, because you know that factors of a number will never
be zero.

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


Re: Storing instances using jsonpickle

2014-09-05 Thread Chris Angelico
On Sat, Sep 6, 2014 at 3:04 AM, MRAB  wrote:
> JSON has 'true' and 'false'.
>
> Python has 'True' and 'False'.
>
> Therefore, if you want it to be able to drop it into Python's REPL, it
> won't be compatible with JSON anyway! (Well, not unless you define
> 'true' and 'false' first.)

This is a new spec, so I guess the question is whether it's primarily
"JSON with some more features" or "subset of Python syntax in the same
way that JSON is a subset of JS". If it's the former, then yes, it'd
use "true" and "false", and you'd have to define them; but if the
latter, the spec would simply use "True" and "False". But being able
to guarantee that JSON decodes correctly with this parser (ie make it
a guaranteed superset of JSON) would be of value.

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


Re: My backwards logic

2014-09-05 Thread Rustom Mody
On Saturday, September 6, 2014 1:37:57 AM UTC+5:30, Paul Rubin wrote:
> Juan Christian  writes:
> >  I made this code just for fun and learning, it's working, but would
> >  this be a good approach? Thanks. ...
> >  ** ** for number in range(start, stop + 1):
> >  ** ** ** ** divisors = [(number % x) for x in range(1, number + 1)]
> >  ** ** ** ** ** ** print("{n} prime? {r}".format(n = number, r =
> >  (divisors.count(0) == 2)))

> 1. Mathematically it's better to stop checking once you've gone past
> the square root of the number, since if n = p*q and is composite,
> then at least one of p,q will be <= sqrt(n).

> 2. As a program design matter, it's generally preferable for functions
> like this to not print the answer.  They should just return a value, in
> this case a bool indicating whether the number is prime.  That makes it
> easier to re-use the function, to wrap it in a unit test framework, etc.
> If you want to print the result, then call the function and have the
> caller print the returned value.

Hoo boy! And we come full circle

> 3. As a simple optimization, once you have checked that the number is
> not divisible by 2, you don't have to check for other even divisors.
> So just check for 2, then the odd numbers 3,5,7...

> This is a little bit fancy but I like to use itertools for these
> sorts of problems:

>   from itertools import chain, takewhile, count

>   def is_prime(n):
>   if n < 2: return False
>   candidates = chain([2], count(3,2))
>   return all(n%d!=0 for d in takewhile(lambda d: d*d<=n, candidates))

Paul's suggestions without the fancy -- no sqrt stop, no generators, no 
itertools
etc -- (all to be learnt at their time of course!)


First of all I make for myself a closed range

>>> def crange(a,b) : return range(a,b+1)

Check it

>>> crange(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Now for divisors of a number

>>> def divisors(n): return [fact for fact in crange(1,n) if n%fact == 0]

Check it

>>> divisors(4)
[1,2,4]

>>> divisors(7)
[1, 7]

So now a prime is a number n whose divisors are only 1 and n

>>> def is_prime(n): return divisors(n) == [1,n]

>>> is_prime(2)
True
>>> is_prime(3)
True
>>> is_prime(4)
False
>>> is_prime(5)
True
>>> is_prime(6)
False
>>> is_prime(7)
True

So collecting the code together

>>> def crange(a,b): return range(a,b+1)
>>> def divisors(n): return [fact for fact in crange(1,n) if n%fact == 0]
>>> def is_prime(n): return divisors(n) == [1,n]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread Juan Christian
@Mark Lawrence: Sorry to ask, but what do you mean by "don't top post here,
thanks.", I'm not familiar with mailing lists, so I may be doing something
wrong and I don't know.


On Fri, Sep 5, 2014 at 4:54 PM, Mark Lawrence 
wrote:

> On 05/09/2014 20:34, Juan Christian wrote:
>
>> What's [snip] ??
>>
>>
> As in cut out or chopped out such that some of the original text has been
> removed.  And please don't top post here, thanks.
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread Rustom Mody
On Saturday, September 6, 2014 7:25:10 AM UTC+5:30, Juan Christian wrote:
> @Mark Lawrence: Sorry to ask, but what do you mean by "don't top post here, 
> thanks.", I'm not familiar with mailing lists, so I may be doing something 
> wrong and I don't know.


Maybe better to say use this
http://en.wikipedia.org/wiki/Posting_style#Interleaved_style

rather than to say dont use

http://en.wikipedia.org/wiki/Posting_style#Top-posting

Also read
https://wiki.python.org/moin/GoogleGroupsPython
-- 
https://mail.python.org/mailman/listinfo/python-list


Posting style: interleaved responses (was: My backwards logic)

2014-09-05 Thread Ben Finney
Juan Christian  writes:

> @Mark Lawrence: Sorry to ask, but what do you mean by "don't top post
> here, thanks.", I'm not familiar with mailing lists, so I may be doing
> something wrong and I don't know.

Please post your responses interleaved with the quoted material to
which you're responding. See the article on “interleaved style”
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style>. And
trim any quoted material to which you're not responding, so your message
only contains relevant material.

-- 
 \“Human reason is snatching everything to itself, leaving |
  `\   nothing for faith.” —Bernard of Clairvaux, 1090–1153 CE |
_o__)  |
Ben Finney

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


Re: Posting style: interleaved responses (was: My backwards logic)

2014-09-05 Thread Juan Christian
On Fri, Sep 5, 2014 at 11:37 PM, Ben Finney 
wrote:

> Juan Christian  writes:
>
> > @Mark Lawrence: Sorry to ask, but what do you mean by "don't top post
> > here, thanks.", I'm not familiar with mailing lists, so I may be doing
> > something wrong and I don't know.
>
> Please post your responses interleaved with the quoted material to
> which you're responding. See the article on “interleaved style”
> https://en.wikipedia.org/wiki/Posting_style#Interleaved_style>. And
> trim any quoted material to which you're not responding, so your message
> only contains relevant material.
>
> --
>  \“Human reason is snatching everything to itself, leaving |
>   `\   nothing for faith.” —Bernard of Clairvaux, 1090–1153 CE |
> _o__)  |
> Ben Finney
>
> --
> https://mail.python.org/mailman/listinfo/python-list


Like that? I'm using gmail so it automatically put the text in the [...]
icon.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My backwards logic

2014-09-05 Thread Dave Angel
Seymore4Head  Wrote in message:
> On Fri, 05 Sep 2014 12:48:56 -0400, Seymore4Head
>  wrote:
> 

> 
> If you start with the list [3,5,7] and step through the list of all
> remaining odd numbers (step 2), and start appending numbers that won't
> divide by numbers already appended in the list, that would seem like a
> pretty efficient way to find all prime numbers.
> 
> 

Yes, that's a well known optimization.  In addition,  you can stop
 once you reach the square root of the target.  No point in
 dividing by the higher numbers in the list,  since if the result
 comes out even, you'd have already exited the loop.


-- 
DaveA

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


Re: Posting style: interleaved responses (was: My backwards logic)

2014-09-05 Thread Zachary Ware
On Fri, Sep 5, 2014 at 10:06 PM, Juan Christian
 wrote:
> On Fri, Sep 5, 2014 at 11:37 PM, Ben Finney 
> wrote:
>>
>> Juan Christian  writes:
>>
>> > @Mark Lawrence: Sorry to ask, but what do you mean by "don't top post
>> > here, thanks.", I'm not familiar with mailing lists, so I may be doing
>> > something wrong and I don't know.
>>
>> Please post your responses interleaved with the quoted material to
>> which you're responding. See the article on “interleaved style”
>> https://en.wikipedia.org/wiki/Posting_style#Interleaved_style>. And
>> trim any quoted material to which you're not responding, so your message
>> only contains relevant material.
>>
>> --
>>  \“Human reason is snatching everything to itself, leaving |
>>   `\   nothing for faith.” —Bernard of Clairvaux, 1090–1153 CE |
>> _o__)  |
>> Ben Finney
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>
> Like that?

Almost.  I left your entire message intact up to this point so you can
see what you sent, though you may need to click the [...] to expand it
[1].  (Normally, I would edit out everything but the actual message
I'm replying to ("Like that?") and some context from Ben's message.)

> I'm using gmail so it automatically put the text in the [...]
> icon.

Gmail (which I also use) requires a little bit of extra effort to
abide by proper mailing list etiquette; in particular, you should
always expand the [...]-hidden text and edit out what you don't need
to send such as signatures and footers from the message you're
replying to.  Then add your replies immediately after the text you're
actually responding to, like I've done with this paragraph and my
previous one.  It's also best to choose "plain text mode" from the
little menu at lower-right (choose that before you actually edit the
message at all, though, or set it as the default.  Changing it
mid-message causes havoc with the formatting).

It's at least a step up from a certain other Google interface to this
list, and I think I speak for everyone in saying "thank you for being
willing to learn proper etiquette" :)

-- 
Zach

[1] Or look here:
https://mail.python.org/pipermail/python-list/2014-September/678058.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to turn a string into a list of integers?

2014-09-05 Thread Steven D'Aprano
Chris Angelico wrote:

> On Fri, Sep 5, 2014 at 12:09 PM, Ian Kelly  wrote:
>> On Thu, Sep 4, 2014 at 6:12 PM, Chris Angelico  wrote:
>>> If it's a Unicode string (which is the default in Python 3), all
>>> Unicode characters will work correctly.
>>
>> Assuming the library that needs this is expecting codepoints and will
>> accept integers greater than 255.
> 
> They're still valid integers. It's just that someone might not know
> how to work with them. Everyone has limits - I don't think repr()
> would like to be fed Graham's Number, for instance, but we still say
> that it accepts integers :)

If you can fit Graham's Number into memory, repr() will happily deal with
it. Although, it might take a while to print out...

[...]
> I just don't like people talking about "Unicode characters" being
> somehow different from "normal text" or something, and being something
> that you need to be careful of. It's not that there are some
> characters that behave nicely, and then other ones ("Unicode" ones)
> that don't.

"Behave nicely" depends on what behaviour you're expecting.

There is a sense in which Unicode is different from ASCII text. ASCII is a 7
bit character set. In principle, you could have different implementations
of ASCII but in practice it's been so long since any machine you're likely
to come across uses anything but exactly a single 8-bit byte for each ASCII
character that we might as well say that ASCII has a single implementation:

* 1 byte code units, fixed width characters 

That is, every character takes exactly one 8-bit byte.

(Reminder: "byte" does not necessarily mean 8 bits.)

Unicode, on the other hand, has *at least* nine different implementations
which you are *likely* to come across:

* UTF-8 has 1-byte code units, variable width characters: every character
takes between 1 and 4 bytes;

* UTF-8 with a so-called "Byte Order Mark" at the beginning of the file;

* UTF-16-BE has 2-byte code units, variable width characters: every
character takes either 2 or 4 bytes;

* UTF-16-LE is the same, but the bytes are in opposite order;

* UTF-16 with a Byte Order Mark at the beginning of the file;

* UTF-32-BE has 4-byte code units, fixed width characters; every character
takes exactly 4 bytes;

* UTF-32-LE is the same, but the bytes are in opposite order;

* UTF-32 with a Byte Order Mark at the beginning of the file;

* UCS-2 is a subset of Unicode with 2-byte code units, fixed width
characters; every character takes exactly 2 bytes (UCS-2 is effectively
UTF-16-BE for characters in the Basic Multilingual Plane).

Plus various more obscure or exotic encodings.

So, while it is not *strictly* correct to say that ASCII character 'A' is
always the eight bits 0101, the exceptions are so rare that there might
as well not be any. But the Unicode character 'A' could be:

0101
0101 
 0101
0101   
   0101


and possibly more.


-- 
Steven

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


Re: How to turn a string into a list of integers?

2014-09-05 Thread Steven D'Aprano
Kurt Mueller wrote:

> Could someone please explain the following behavior to me:
> Python 2.7.7, MacOS 10.9 Mavericks
> 
 import sys
 sys.getdefaultencoding()
> 'ascii'

That's technically known as a "lie", since if it were *really* ASCII it
would refuse to deal with characters with the high-bit set. But it doesn't,
it treats them in an unpredictable and implementation-dependent manner.

 [ord(c) for c in 'AÄ']
> [65, 195, 132]

In this case, it looks like your terminal is using UTF-8, so the character Ä
is represented in memory by bytes 195, 132:

py> u'Ä'.encode('utf-8')
'\xc3\x84'
py> for c in u'Ä'.encode('utf-8'):
... print ord(c)
...
195
132

If your terminal was set to use a different encoding, you probably would
have got different results. When you type whatever key combination you used
to get Ä, your terminal receives the bytes 195, 132, and displays Ä. But
when Python processes those bytes, it's not expecting arbitrary Unicode
characters, it's expecting ASCII-ish bytes, and so treats it as two bytes
rather than a single character:

py> 'AÄ'
'A\xc3\x84'

That's not *really* ASCII, because ASCII doesn't include anything above 127,
but we can pretend that "ASCII plus arbitrary bytes between 128 and 256" is
just called ASCII. The important thing here is that although your terminal
is interpreting those two bytes \xc3\x84 (decimal 195, 132) as the
character Ä, it isn't anything of the sort. It's just two arbitrary bytes.

 [ord(c) for c in u'AÄ']
> [65, 196]

Here, you have a proper Unicode string, so Python is expecting to receive
arbitrary Unicode characters and can treat the two bytes 195, 132 as Ä, and
that character has ordinal value 196:

py> ord(u"Ä")
196



> My obviously wrong understanding:
> ‚AÄ‘ in ‚ascii‘ are two characters
>  one with ord A=65 and
>  one with ord Ä=196 ISO8859-1 

As soon as you start talking about code tables, *it isn't ASCII anymore*.
(Technically, ASCII *is* a code table, but it's one that only covers 127
different characters.)

When you type AÄ on your keyboard, or paste them, or however they were
entered, the *actual bytes* the terminal receives will vary, but regardless
of how they vary, the terminal *almost certainly* will interpret the first
byte (or possibly more than one byte, who knows?) as the ASCII character A.

(Most, but not all, code pages agree that byte 65 is A, 66 is B, and so on.)

The second (third? fifth?) byte, and possibly subsequent bytes, will
*probably* be displayed by the terminal as Ä, but Python only sees the raw
bytes. The important thing here is that unless you have some bizarre and
broken configuration, Python can correctly interpret the A as A, but what
you get for the Ä depends on the interaction of keyboard, OS, terminal and
the phase of the moon.

>  —-> why [65, 195, 132]

Since Python is expecting to interpret those bytes as an ASCII-ish byte
string, it grabs the raw bytes and ends up (in your case) with 65, 195,
132, or 'A\xc3\x84', even though your terminal displays it as AÄ.

This does not happen with Unicode strings.

> u’AÄ’ is an Unicode string
>  —-> why [65, 196]

In this case, Python knows that you are dealing with a Unicode string, and Ä
is a valid character in Unicode. Python deals with the internal details of
converting from whatever-damn-bytes your terminal sends it, and ends up
with a string of characters A followed by Ä.

If you could peer under the hood, and see what implementation Python uses to
store that string, you would see something version dependent. In Python
2.7, you would see an object more or less something vaguely like this:

[object header containing various fields]
[length = 2]
[array of bytes = 0x0041 0x00C4]


That's for a so-called "narrow build" of Python. If you have a "wide build",
it will something like this:

[object header containing various fields]
[length = 2]
[array of bytes = 0x0041 0x00C4]

In Python 3.3, "narrow builds" and "wide builds" are gone, and you'll have
something conceptually like this:

[object header containing various fields]
[length = 2]
[tag = one byte per character]
[array of bytes = 0x41 0xC4]

Some other implementations of Python could use UTF-8 internally:

[object header containing various fields]
[length = 2]
[array of bytes = 0x41 0xC3 0x84]


or even something more complex. But the important thing is, regardless of
the internal implementation, Python guarantees that a Unicode string is
treated as a fixed array of code points. Each code point has a value
between 0 and, not 127, not 255, not 65535, but 1114111.


-- 
Steven

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


Re: Run an http server on Apache without wsgi?

2014-09-05 Thread dieter
"Frank Millman"  writes:

> My AccInABox project runs an http server. Previously I used the cherry.py 
> wsgiserver, but I have now switched to asyncio.
>
> I tried to use the wsgi interface, but I have a particular requirement and 
> found that wsgi got in the way, so I dropped it and now handle all requests 
> directly.
>
> Now the question arises, what if someone wants to run my program under 
> Apache or nginx?

There are various ways to interface Apache and Python: among
others "cgi", "fcgi", "wsgi". There also used to be "mod_python"
which allowed to run Python directly inside an Apache process - however,
this module is no longer supported.

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


Re: Anyway to reduce size of pdf using python script.

2014-09-05 Thread dieter
kek...@gmail.com writes:

> On Friday, February 1, 2013 8:03:41 PM UTC-5, access...@gmail.com wrote:
>> I have a batch file that exports ArcGIS pdf maps to a directory. I would 
>> like to include a step in the script where the pdf file is reduced in size 
>> instead of manually opening each file in Acrobat X Pro after the script has 
>> run and doing it there.
>> 
>> Can this be done using python scripting or does the automation stop at 
>> exporting the map?
>
> Have you found the solution? I am having exactly the same problem and happen 
> to see your post here. Please let me know how you did it, thanks! 

Python is a (mostly) general purpose programming language.
To optimize "PDF" specialized PDF knowledge is requires - which
Python, by itself, lacks.

You may check whether "Acrobat X Pro" is scriptable (can be controlled
externally). Windows programs sometimes expose a COM interface
for such external control (also called "automation"). If "Acrobat"
supports automation via COM interfaces, the you can use Python
to "drive the automation" (some Python versions for Windows come
with COM support).

The required PDF knowledge may also come from special (Python extension)
packages. "reportlab" provides Python extensions to handle PDF.
I do not know whether this includes optimizing "PDF" (I use
a "reportlab" package to generate PDF from Python scripts).

I expect that the optimization you observe results from compressing
the images included in the PDF. If this is the case,
you could in principle write your own package to do this in Python.
You would need to learn PDF internals for this.

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