Re: Idiosyncratic python

2015-09-23 Thread Terry Reedy

On 9/24/2015 2:35 AM, Steven D'Aprano wrote:

On Thursday 24 September 2015 16:16, Paul Rubin wrote:


Steven D'Aprano  writes:

for k, v in mydict.items():
 del(k)


That looks wrong: it's deleting k from what?


The local namespace.

py> k = 23
py> print k
23
py> del k
py> print k
Traceback (most recent call last):
   File "", line 1, in 
NameError: name 'k' is not defined



instead of the more obvious
for v in mydict.values():
 ...


Maybe you mean

while mydict:
   k, v = mydict.popitem()
   ...



Hah, no, you're the second person to make that mistake! One of the guys I
work with suggested `mydict = {}` to empty the dict.

`del k` (aside: no need for parens, del is a statement, not a function)
doesn't delete the key from the dictionary. It just deletes the name k. The
obvious intent is to iterate over the *values* of the dictionary, but the
coder didn't know about values, so he iterated over (key,value) pairs, then
deleted the key local variable (not the key in the dict!) to keep the
namespace clean.


but, but, each iteration rebinds k, so del k is only needed after the 
loop .. unless one is super fanatic about cleanliness within the loop ;-)



--
Terry Jan Reedy

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


Re: Idiosyncratic python

2015-09-23 Thread Ben Finney
Steven D'Aprano  writes:

> On Thursday 24 September 2015 16:16, Paul Rubin wrote:
>
> > Steven D'Aprano  writes:
> >> for k, v in mydict.items():
> >> del(k)
>
> […] The obvious intent is to iterate over the *values* of the
> dictionary, but the coder didn't know about values, so he iterated
> over (key,value) pairs, then deleted the key local variable (not the
> key in the dict!) to keep the namespace clean.

That's not obvious to me. It's plausible, now that you say it. I find it
also plausible, though, that the author is under the mistaken impression
that the key and value must both be deleted, and has found a way that
appears to do that.

Perhaps what we're illustrating here is exactly why such idiosyncratic
code *is* bad: it obscures the intent of the code for programmers who
know idiomatic Python.

And those readers are hopefully the ones the author should be trying to
communicate to, on the theory that we've all got the goal to become a
programmer who knows idiomatic Python.

-- 
 \“The fact of your own existence is the most astonishing fact |
  `\you'll ever have to confront. Don't dare ever see your life as |
_o__)boring, monotonous, or joyless.” —Richard Dawkins, 2010-03-10 |
Ben Finney

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


Re: Idiosyncratic python

2015-09-23 Thread Steven D'Aprano
On Thursday 24 September 2015 16:16, Paul Rubin wrote:

> Steven D'Aprano  writes:
>> for k, v in mydict.items():
>> del(k)
> 
> That looks wrong: it's deleting k from what?

The local namespace.

py> k = 23
py> print k
23
py> del k
py> print k
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'k' is not defined

 
>> instead of the more obvious
>> for v in mydict.values():
>> ...
> 
> Maybe you mean
> 
>while mydict:
>   k, v = mydict.popitem()
>   ...


Hah, no, you're the second person to make that mistake! One of the guys I 
work with suggested `mydict = {}` to empty the dict.

`del k` (aside: no need for parens, del is a statement, not a function) 
doesn't delete the key from the dictionary. It just deletes the name k. The 
obvious intent is to iterate over the *values* of the dictionary, but the 
coder didn't know about values, so he iterated over (key,value) pairs, then 
deleted the key local variable (not the key in the dict!) to keep the 
namespace clean.



-- 
Steve

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


Re: Idiosyncratic python

2015-09-23 Thread Paul Rubin
Steven D'Aprano  writes:
> for k, v in mydict.items(): 
> del(k)

That looks wrong: it's deleting k from what?

> instead of the more obvious
> for v in mydict.values(): 
> ...

Maybe you mean

   while mydict:
  k, v = mydict.popitem()
  ...
-- 
https://mail.python.org/mailman/listinfo/python-list


Idiosyncratic python

2015-09-23 Thread Steven D'Aprano
I was looking at an in-house code base today, and the author seems to have a 
rather idiosyncratic approach to Python. For example:


for k, v in mydict.items(): 
del(k)
...


instead of the more obvious

for v in mydict.values(): 
...



What are your favorite not-wrong-just-weird Python moments?



-- 
Steve

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


Re: Cyber Grand Challenge, prizes up to 2 million dollars ! (DARPA)

2015-09-23 Thread Robert Baer

Skybuck Flying wrote:

(Click on little icon on website top left for menu):

Information about challenge:

http://www.cybergrandchallenge.com/site/index.html#about

https://cgc.darpa.mil/CGC_Rules_16_May_14_Version_2.pdf

Perhaps this will be a yearly contest.

There is a catch though, to collect the prizes: "The prize recipient
shall be a citizen, a permanent resident of the United States, or a US
Entity. "

Bye,
Skybuck.

  With 1024x768 screen:

"Please use a wider window to browse cybergrandchallenge.com"

  All else black, NO "contest" info.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Readlines returns non ASCII character

2015-09-23 Thread paul . hermeneutic
After looking at this briefly, I am not sure that this is a plain-text
file. Interpreting it as UTF-16 LE shows that the characters are as
they appear.

Immediately after the BOM is:

SINGLE LOW-9 QUOTATION MARK' (U+201A)
START OF HEADING (U+0001)
SPACE (U+0020)
SPACE (U+0020)
LATIN SMALL LETTER Y WITH DIAERESIS (U+00FF)
LATIN SMALL LETTER THORN (U+00FE)
LATIN SMALL LETTER Y WITH DIAERESIS (U+00FF)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Readlines returns non ASCII character

2015-09-23 Thread paul . hermeneutic
After looking at this briefly, I am not sure that this is a plain-text
file. Interpreting it as UTF-16 LE shows that the characters are as
they appear.

Immediately after the BOM is:

SINGLE LOW-9 QUOTATION MARK' (U+201A)
START OF HEADING (U+0001)
SPACE (U+0020)
SPACE (U+0020)
LATIN SMALL LETTER Y WITH DIAERESIS (U+00FF)
LATIN SMALL LETTER THORN (U+00FE)
LATIN SMALL LETTER Y WITH DIAERESIS (U+00FF)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ConnectionError handling problem

2015-09-23 Thread shiva upreti
On Sunday, September 20, 2015 at 8:11:18 PM UTC+5:30, Laura Creighton wrote:
> The discussion about why or why not to use a bare except has gotten us
> away from the problem reported, which is "why is my script hanging?"
> 
> In a message of Sat, 19 Sep 2015 17:18:12 +0100, Mark Lawrence writes:
> >> I am learning python. I wrote a script using requests module.
> >> The scripts runs fine for sometime, but after a while it hangs. When I 
> >> press CTRL+C it shows ConnectionError even though I have included 
> >> exception handling.
> >> I am not sure as to why it cant handle ConnectionError when the script 
> >> runs for a long time.
> >>
> >> This is a part(causing issues) of the script I am running:
> >>
> >> while(k<46656):
> >>j=res[k]
> >>url="http://172.16.68.6:8090/login.xml"; 
> >>query_args = {'mode':'191', 'username':str(i), 
> >> 'password':str(j), 'a':'1442397582010', 'producttype':'0'}
> >>
> >>try:
> >>r=requests.post(url, data=query_args)
> >>except:
> >>print "Connection error"
> >>time.sleep(30)
> >>continue
> >>
> >>html=r.text
> >>if(len(html) < 10):
> >>continue
> >>
> >>if("The system could not log you on" not in html):
> >>print "hello"
> >>filehandle=open("ids", "a")
> >>filehandle.write(str(i)+'\n')
> >>filehandle.write(str(j)+'\n')
> >>filehandle.close()
> >>break
> >>
> >>k=k+1
> >>
> >> Any help will be highly appreciated.
> 
> So, when it hangs there are two main problems you can have.  One sort
> is that the other side, http://172.16.68.6:8090/ it in itself
> configured to only let people use a connection for a certain amount of
> time.  If you are using it for longer, it will disconnect you.  Or the
> other sort is that the other side disconnects people who have been
> silent for a certain amount of time.  If you haven't send anything for
> a certain amount of time it will log you off.  There are lots of other
> things that work this way -- the system may see many attempts to login
> and think you are trying to break into the system, the machine may
> have crashed ... but the bottom line is that the reason your script
> hangs is that there is nobody there on the other end.
> 
> The other sort of problem you can have is that the other end is
> alive and well and talking to you, but you don't understand what
> you are getting, and you are ignoring things you don't understand.
> This can look exactly the same.
> 
> To find out what is going on you need to log what it is that you
> are getting, to see if the answer is 'nothing' or 'garbage'.
> 
> Laura

Hi
If my script hangs because of the reasons you mentioned above, why doesnt it 
catch ConnectionError?
My script stops for a while and when I press CTRL+C, it shows ConnectionError 
without terminating the process, and the script resumes from where it left off.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Readlines returns non ASCII character

2015-09-23 Thread MRAB

On 2015-09-24 02:37, Ian Kelly wrote:

On Wed, Sep 23, 2015 at 6:09 PM, MRAB  wrote:

On 2015-09-24 00:51, paul.hermeneu...@gmail.com wrote:


  If this starts at the beginning of the file, then it indicates that
the file is UTF-16 (LE).

UTF-8[t 1] EF BB BF   239 187 191
UTF-16 (BE)FE FF  254 255
UTF-16 (LE)FF FE  255 254
UTF-32 (BE)00 00 FE FF0 0 254 255
UTF-32 (LE)FF FE 00 00255 254 0 0


The "signature" EF BB BF indicates the encoding called "utf-8-sig" by
Python. It occurs on Windows.

If the file doesn't start with any of these, then it could be using any
encoding (except UTF-16 or UTF-32).


Yes, but what does it mean when the signature is 00 FF 00 FE 00 FF and
occurs not at the beginning but repeatedly throughout the file, as
appears in the OP's case?

At least, I'm assuming that the high-order bytes are 00 based on what
the OP posted. I wouldn't be surprised though if they're just being
mangled by the terminal, if it happens to be a certain one that will
not be named but uses CP 1252.


Yes, a byte-string literal or a hex dump of, say, the first 256 bytes
would've been better.

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


Re: Readlines returns non ASCII character

2015-09-23 Thread Ian Kelly
On Wed, Sep 23, 2015 at 6:09 PM, MRAB  wrote:
> On 2015-09-24 00:51, paul.hermeneu...@gmail.com wrote:
>>
>>   If this starts at the beginning of the file, then it indicates that
>> the file is UTF-16 (LE).
>>
>> UTF-8[t 1] EF BB BF   239 187 191
>> UTF-16 (BE)FE FF  254 255
>> UTF-16 (LE)FF FE  255 254
>> UTF-32 (BE)00 00 FE FF0 0 254 255
>> UTF-32 (LE)FF FE 00 00255 254 0 0
>>
> The "signature" EF BB BF indicates the encoding called "utf-8-sig" by
> Python. It occurs on Windows.
>
> If the file doesn't start with any of these, then it could be using any
> encoding (except UTF-16 or UTF-32).

Yes, but what does it mean when the signature is 00 FF 00 FE 00 FF and
occurs not at the beginning but repeatedly throughout the file, as
appears in the OP's case?

At least, I'm assuming that the high-order bytes are 00 based on what
the OP posted. I wouldn't be surprised though if they're just being
mangled by the terminal, if it happens to be a certain one that will
not be named but uses CP 1252.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Modify environment variable for subprocess

2015-09-23 Thread Akira Li
loial  writes:

> I need to modify the LIBPATH environment variable when running a
> process via subprocess, but otherwise retain the existing environment.
>
> Whats the best way to do that?

Pass env=dict(os.environ, LIBPATH=value) parameter:

  import os
  import subprocess 
  
  subprocess.check_call('echo $LIBPATH', shell=True,
env=dict(os.environ, LIBPATH='/some/path'))


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


Re: Readlines returns non ASCII character

2015-09-23 Thread MRAB

On 2015-09-24 00:51, paul.hermeneu...@gmail.com wrote:

  If this starts at the beginning of the file, then it indicates that
the file is UTF-16 (LE).

UTF-8[t 1] EF BB BF   239 187 191
UTF-16 (BE)FE FF  254 255
UTF-16 (LE)FF FE  255 254
UTF-32 (BE)00 00 FE FF0 0 254 255
UTF-32 (LE)FF FE 00 00255 254 0 0


The "signature" EF BB BF indicates the encoding called "utf-8-sig" by
Python. It occurs on Windows.

If the file doesn't start with any of these, then it could be using any
encoding (except UTF-16 or UTF-32).

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


Re: Postscript to pdf

2015-09-23 Thread mithra
On Tuesday, September 22, 2015 at 2:20:56 AM UTC-5, Bala Ji wrote:
> Hello,
> This is my programe : on mac i was able to output ps file but i didn't
> got the pdf file :/
> 
> # -*- coding: utf-8 -*-
> # script lecture_gif.py
> from Tkinter import *
> import tkMessageBox
> import Tkconstants
> import tkFileDialog
> from PIL import ImageTk
> import PIL.Image
> import os, sys
> import subprocess
> 
> 
> def Ouvrir():
> Canevas.delete(ALL) # on efface la zone graphique
> 
> filename = tkFileDialog.askopenfilename(title="Ouvrir une
> image",filetypes=[('gif files','.gif'),('all files','.*')])
> print(filename)
> photo = PhotoImage(file=filename)
> gifdict[filename] = photo  # référence
> print(gifdict)
> 
> Canevas.create_image(0,0,anchor=NW,image=photo)
> Canevas.config(height=photo.height(),width=photo.width())
> 
> Mafenetre.title("Image "+str(photo.width())+" x "+str(photo.height()))
> 
> def insertimage():
> n=tkFileDialog.askopenfilename(filetypes = [("Image Files", ("*.jpg",
> "*.gif")),("JPEG",'*.jpg'),("GIF",'*.gif'),('All','*')])
> img = PIL.Image.open(n)
> img = img.resize((229, 253))
> photoimg = ImageTk.PhotoImage(img)
> label = Label(image=photoimg)
> label.image = photoimg # keep a reference!
> Canevas.create_image(65,320,anchor=W,image = photoimg)
> def insertsign():
> n=tkFileDialog.askopenfilename(filetypes = [("Image Files", ("*.jpg",
> "*.gif")),("JPEG",'*.jpg'),("GIF",'*.gif'),('All','*')])
> img = PIL.Image.open(n)
> img = img.resize((300, 100))
> photoimg = ImageTk.PhotoImage(img)
> Canevas.create_image(600,500,anchor=W,image = photoimg)
> Canvas.pack()
> 
> def Fermer():
> Canevas.delete(ALL)
> Mafenetre.title("Image")
> 
> def save():
> Canevas.update()
> Canevas.postscript(file=tkFileDialog.asksaveasfilename(), colormode='color')
> subprocess.call(["ps2pdf", "-dEPSCrop", "test.ps", "test.pdf"])
> 
> # def convert():
> # ps2pdf -dEPSCrop image.ps
> # convert -density 300 PASSPORTQUALITE.ps output.png
> 
> # class TkFileDialogExample(Tkinter.Frame):
> #
> # def __init__(self, root):
> #
> # Tkinter.Frame.__init__(self, root)
> # button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
> # Tkinter.Button(self, text='Save',
> command=self.asksaveasfilename).pack(**button_opt)
> #
> # self.file_opt = options = {}
> # options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
> # options['initialfile'] = 'myfile.txt'
> # options['parent'] = root
> #
> # def asksaveasfilename(self):
> # filename = tkFileDialog.asksaveasfilename(**self.file_opt)
> #
> # if filename:
> # return open(filename, 'w')
> #
> # if __name__=='__main__':
> # root = Tkinter.Tk()
> # TkFileDialogExample(root).pack()
> # root.mainloop()
> def Apropos():
> tkMessageBox.showinfo("A propos","Tutorial")
> 
> def Write():
> def delete():
> e1.delete(0,END)
> e2.delete(0,END)
> e3.delete(0,END)
> e4.delete(0,END)
> e5.delete(0,END)
> e6.delete(0,END)
> Canevas.delete("e1")
> def valider():
> Canevas.create_text(315,200,anchor=W,text="Surname/Nom",fill='Black',font='Arial
> 14')
> Canevas.create_text(315,220,anchor=W,text=e1.get(),fill='Black',font='Arial
> 30',tags ="e1")
> Canevas.create_text(315,250,anchor=W,text="Given
> name/Prénom",fill='Black',font='Arial 14')
> Canevas.create_text(315,270,anchor=W,text=e2.get(),fill='Black',font='Arial
> 30',tags ="e1")
> Canevas.create_text(315,300,anchor=W,text="Fonction/Function",fill='Black',font='Arial
> 14')
> Canevas.create_text(315,320,anchor=W,text=e3.get(),fill='Black',font='Arial
> 30',tags ="e1")
> Canevas.create_text(470,395,anchor=W,text=e4.get(),fill='Black',font='Arial
> 30',tags ="e1")
> Canevas.create_text(500,438,anchor=W,text=e5.get(),fill='Black',font='Arial
> 30',tags ="e1")
> Canevas.create_text(228,503,anchor=W,text=e6.get(),fill='Black',font='Arial
> 30',tags ="e1")
> master = Tk()
> Label(master, text="Surname/Nom").grid(row=0)
> Label(master, text="Given name/Prénom").grid(row=1)
> Label(master, text="Fonction/Function").grid(row=2)
> Label(master, text="Validity Date").grid(row=3)
> Label(master, text="Chef").grid(row=4)
> Label(master, text="Student number").grid(row=5)
> e1 = Entry(master)
> e2 = Entry(master)
> e3 = Entry(master)
> e4 = Entry(master)
> e5 = Entry(master)
> e6 = Entry(master)
> e1.grid(row=0, column=1)
> e2.grid(row=1, column=1)
> e3.grid(row=2, column=1)
> e4.grid(row=3, column=1)
> e5.grid(row=4, column=1)
> e6.grid(row=5, column=1)
> Button(master, text='Ok', command=valider).grid(row=2, column=2,
> sticky=W, pady=4)
> Button(master, text='Delete', command=delete).grid(row=3, column=2,
> sticky=W, pady=4)
> mainloop( )
> 
> # Main window
> Mafenetre = Tk()
> Mafenetre.title("Image")
> 
> # Création d'un widget Menu
> menubar = Menu(Mafenetre)
> 
> menufichier = Menu(menubar,tearoff=0)
> menufichier.add_command(label="Open mask",command=Ouvrir)
> menufichier.add_command(labe

Re: Readlines returns non ASCII character

2015-09-23 Thread paul . hermeneutic
 If this starts at the beginning of the file, then it indicates that
the file is UTF-16 (LE).

UTF-8[t 1] EF BB BF   239 187 191
UTF-16 (BE)FE FF  254 255
UTF-16 (LE)FF FE  255 254
UTF-32 (BE)00 00 FE FF0 0 254 255
UTF-32 (LE)FF FE 00 00255 254 0 0
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Readlines returns non ASCII character

2015-09-23 Thread Ian Kelly
On Wed, Sep 23, 2015 at 3:02 PM, SANKAR .  wrote:
> Thanks Ian,
> this isn't a text file, but when I read with  readline I get the data I need
> along with mojibake. UTF 32 returns following error:
>
> Traceback (most recent call last):
>   File "D:\RV\RV1.py", line 17, in 
> linenumx1 = file.readlines()
>   File "C:\Python27\lib\codecs.py", line 682, in readlines
> return self.reader.readlines(sizehint)
>   File "C:\Python27\lib\codecs.py", line 591, in readlines
> data = self.read()
>   File "C:\Python27\lib\codecs.py", line 480, in read
> newchars, decodedbytes = self.decode(data, self.errors)
>   File "C:\Python27\lib\encodings\utf_32.py", line 130, in decode
> codecs.utf_32_ex_decode(input, errors, 0, False)
> UnicodeDecodeError: 'utf32' codec can't decode bytes in position 0-3: code
> point not in range(0x11)

1) Open the file in binary mode using the open function, not using codecs.open.
2) Find out or figure out the file format.
3) Read the file and extract the particular fields that you're
interested in from the file as bytes objects.
4) Decode those bytes objects and only those using UTF-32.
-- 
https://mail.python.org/mailman/listinfo/python-list


Sending a python argument to a Boost.Python function

2015-09-23 Thread kenseehart

I have a python class that wraps a boost.python object. I am using a 
boost.python library function that requires the raw boost.python object. I want 
to be able to pass the python object to the boost api function.

Is there a hook available on the python side that instructs boost to perform a 
conversion?

Hypothetically, it would be something like this:

class EggWrapper(object):
def __init__(self, egg):
object.__setattr__(self, '_egg', egg)

def __getattribute__(self, name):
egg = object.__getattribute__(self, '_egg')

if name=='_egg':
return egg

if name in EggWrapper.__dict__:
return object.__getattribute__(self, name)

return getattr(egg, name)

def __setattr__(self, name, value):
egg = object.__getattribute__(self, '_egg')
setattr(egg, name, value)

def magic_boost_conversion_unicorn_hook(self):
'this is automatically called by boost to convert arguments'
egg = object.__getattribute__(self, '_egg')
return egg


import myboostlib

egg = EggWrapper(myboostlib.getEgg())

myboostlib.spam(egg)


Where the signature for spam is: spam(class boost::python::api::object)
And myboostlib.getEgg() returns a boost::python::api::object

- I do not have the ability to modify the library or any C/C++ code, the 
solution must be entirely on the python side.

- I can't change the calling convention. The wrapped object must be used (i.e. 
I can't just say myboostlib.spam(egg._egg), though that is the desired result). 
So the solution should be incorporated into the EggWrapper class.

- I'm only interested in solutions that reflect expert knowledge of 
Boost.Python, since I believe I have already considered all the trivial 
solutions.

So my question is, does magic_boost_conversion_unicorn_hook() exist, and if so, 
how is it spelled?


Thanks,
~ Ken





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


Re: List comprehensions and evaluation of elements therein

2015-09-23 Thread Ian Kelly
On Wed, Sep 23, 2015 at 12:12 PM, James Harris  wrote:
> A list comprehension has various components. Anyone know when each of the
> elements is evaluated? In the form
>
>  [v0 for v0 in expr0 if expr1]
>
> If v0 appears in expr0 or expr1 the evaluation order matters.
>
> I think of the above as being a rewrite of
>
>  results = []
>  for v0 in expr0:
>if expr1:
>  results.append(v0)
>  return results
>
> Further,
>
>  [v0, v2 for v0 in expr0 if expr1 for v2 in expr2 if expr3]
>
> leads to
>
>  results = []
>  for v0 in expr0:
> if expr1:
>for v2 in expr2:
>   if expr3:
>  results.append((v0, v2))
>  return results
>
> First of all, is that a correct analog of the list comprehension?

Looks right, see:
https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries

Note that the last sentence there about names not "leaking" is only
true for Python 3.

You may also be interested in PEPs 202 and 289.
-- 
https://mail.python.org/mailman/listinfo/python-list


List comprehensions and evaluation of elements therein

2015-09-23 Thread James Harris
A list comprehension has various components. Anyone know when each of 
the elements is evaluated? In the form


 [v0 for v0 in expr0 if expr1]

If v0 appears in expr0 or expr1 the evaluation order matters.

I think of the above as being a rewrite of

 results = []
 for v0 in expr0:
   if expr1:
 results.append(v0)
 return results

Further,

 [v0, v2 for v0 in expr0 if expr1 for v2 in expr2 if expr3]

leads to

 results = []
 for v0 in expr0:
if expr1:
   for v2 in expr2:
  if expr3:
 results.append((v0, v2))
 return results

First of all, is that a correct analog of the list comprehension?

With this latter expansion the values of v0 and v2 could appear in expr4 
or expr5. Again, the evaluation order would matter.


James

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


Re: PyInstaller+ Python3.5 (h5py import error)

2015-09-23 Thread Christian Gollwitzer

Am 23.09.15 um 18:20 schrieb Heli Nix:

Dear all,

Thanks a lot for your replies. Very helpful. I have already done some trials 
with Virtualenv, but PyInstaller is much closer to the idea of an installer you 
can pass to someone.

  I have been using development version of PyInstaller in order to be able to 
use it with my script written with Python versin 3.3.5.

I started with a very simple script just to test. I use the following command 
to create the distribution folder.

pyinstaller test.py

my script contains the following few lines and it runs ok on my own machine.

import numpy as np
import h5py

a=np.arange(10)
print(a)
inputFiles="test.h5"
with h5py.File(inputFiles, 'w') as inputFileOpen:
   pass

I am getting the following error related to importing h5py.

[...]
ImportError: No module named 'h5py.defs'


pyinstaller guesses from the code which modules are imported. It looks 
like if h5py imports a module h5py.defs, which is missing. For some 
programs, you need to support pyinstaller with additional information, 
especially if modules are loaded at runtime. Try:



pyinstaller --hidden-import=h5py.defs test.py


If I modify my script to

import numpy as np
import h5py
a=np.arange(10)
print(a)


This is another hint: obviously h5py defers module loading until you 
first really open a HDF5 file. There pyinstaller has no means to find 
this out.


Christian

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


Re: A little test for you Guys😜

2015-09-23 Thread alister
On Wed, 23 Sep 2015 00:56:19 +0100, MRAB wrote:

> On 2015-09-23 00:32, Mark Lawrence wrote:
>> On 22/09/2015 19:43, Python_Teacher via Python-list wrote:
>>> you have 10 minutes😂 Good luck!!
>>>
>>>
>>> 1. What is PEP8 ?
>>
>> It's the one between PEP7 and PEP9.
>>
>>
>>> 2. What are the different ways to distribute some python source code ?
>>
>> Write on sheet of paper, fold into paper dart, throw from window.
>>
>>
>>> 2 Lists
>>
>> Tut, tut, tut.
>>
>>
>>> Let's define the function plural :
>>>
>>> def plural(words):
>>>  plurals = []
>>>  for word in words:
>>> plurals.append(word + 's')
>>>  return plurals
>>>
>>> for word in plural(['cabagge','owl','toy']):
>>>  print word
>>>
>>> Question : How could the code of the function plural be optimised?
>>
>> It is all ready optimised for programmer time so don't bother with it
>> unless there are unforeseen bugs.
>>
>>
>>> 3 Dictionaries
>>>
>>> Here are two dictionnaries :
>>>
>>> input = {
>>>  'foo1': 'bar1', 'chose': 'truc', 'foo2': 'bar2',
>>> }
>>> output = {
>>>  'bar1': 'foo1', 'truc': 'chose', 'bar2': 'foo2'
>>> }
>>>
>>> Question : Propose a function that returns output when you provide
>>> input ?
>>
>> def function():
>>   return input("Who cares?")
>>
> You have a couple of problems:
> 
> 1. 'input' is already bound to a dict.
> 
> 2. From question 2, it's clear that Python 2 is being used, so you
> should be using 'raw_input' instead.
> 
> [snip]

the question also shadows a builtin :-)



-- 
This is for all ill-treated fellows
Unborn and unbegot,
For them to read when they're in trouble
And I am not.
-- A. E. Housman
-- 
https://mail.python.org/mailman/listinfo/python-list


Video Tutorial Online On How To Create a Basic Dynamic Website In Python?

2015-09-23 Thread Cai Gengyang
Hello!

So I have Python 2.7.10, 3.3.2, 3.3.4 downloaded on my Mac OS X Yosemite 
10.10.2 and also downloaded pip and django. Is there an online tutorial on how 
to create a basic dynamic website in Python where I can put my 
image/photo/video sharing app ? I find it much easier to learn from something 
visual which I can follow step-by-step ...

Thanks a lot.

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


PyInstaller+ Python3.5 (h5py import error)

2015-09-23 Thread Heli Nix
Dear all, 

Thanks a lot for your replies. Very helpful. I have already done some trials 
with Virtualenv, but PyInstaller is much closer to the idea of an installer you 
can pass to someone. 

 I have been using development version of PyInstaller in order to be able to 
use it with my script written with Python versin 3.3.5. 

I started with a very simple script just to test. I use the following command 
to create the distribution folder. 

pyinstaller test.py

my script contains the following few lines and it runs ok on my own machine.

import numpy as np
import h5py

a=np.arange(10)
print(a)
inputFiles="test.h5"
with h5py.File(inputFiles, 'w') as inputFileOpen:
  pass

I am getting the following error related to importing h5py. 

test returned -1
Traceback (most recent call last):
  File "", line 2, in 
  File 
"/usr/lib/python3.3/site-packages/PyInstaller-3.0.dev2-py3.3.egg/PyInstaller/loader/pyimod03_importers.py",
 line 311, in load_module
  File "/usr/lib64/python3.3/site-packages/h5py/__init__.py", line 23, in 

  File 
"/usr/lib/python3.3/site-packages/PyInstaller-3.0.dev2-py3.3.egg/PyInstaller/loader/pyimod03_importers.py",
 line 493, in load_module
  File "h5r.pxd", line 21, in init h5py._conv 
(/tmp/pip_build_root/h5py/h5py/_conv.c:6563)
  File 
"/usr/lib/python3.3/site-packages/PyInstaller-3.0.dev2-py3.3.egg/PyInstaller/loader/pyimod03_importers.py",
 line 493, in load_module
  File "_objects.pxd", line 12, in init h5py.h5r 
(/tmp/pip_build_root/h5py/h5py/h5r.c:2708)
  File 
"/usr/lib/python3.3/site-packages/PyInstaller-3.0.dev2-py3.3.egg/PyInstaller/loader/pyimod03_importers.py",
 line 493, in load_module
  File "_objects.pyx", line 1, in init h5py._objects 
(/tmp/pip_build_root/h5py/h5py/_objects.c:6407)
ImportError: No module named 'h5py.defs'

If I modify my script to 

import numpy as np
import h5py
a=np.arange(10)
print(a)

 then, the created exectuable will run successfully on other linux machines.  
Does anybody have any idea why I am getting the following h5py import error?

My spec file also looks like this:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['test.py'],
 pathex=['/home/albert/test'],
 binaries=None,
 datas=None,
 hiddenimports=[],
 hookspath=None,
 runtime_hooks=None,
 excludes=None,
 win_no_prefer_redirects=None,
 win_private_assemblies=None,
 cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
 cipher=block_cipher)
exe = EXE(pyz,
  a.scripts,
  exclude_binaries=True,
  name='test',
  debug=False,
  strip=None,
  upx=True,
  console=True )
coll = COLLECT(exe,
   a.binaries,
   a.zipfiles,
   a.datas,
   strip=None,
   upx=True,
   name='test')


Thank you very much in Advance for your help, 


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


Re: Python, convert an integer into an index?

2015-09-23 Thread Chris Roberts
Fantastic!
I started a course, but I have been having issues with
string/index/list/integer conversions and manipulations.
This variation wasn't in any of my texts or class exercises either.
Your way was both simple enough to understand and very informative!
Thanks.


On Tue, Sep 22, 2015 at 6:21 PM, Laura Creighton  wrote:

> In a message of Tue, 22 Sep 2015 14:43:55 -0700, Chris Roberts writes:
> >
> >
> >(How do I make it into an index? )
> >Preferably something fairly easy to understand as I am new at this.
> >
> >results = 134523  #(Integer)
> >
> >Desired:
> >results = [1, 2, 3, 4, 5, 2, 3]   #(INDEX)
> >
> >Somehow I see ways to convert index to list to int, but not back again.
> >
> >Thanks,
> >crzzy1
>
> You need to convert your results into a string first.
>
> result_int=1234523
> result_list=[]
>
> for digit in str(result_int):
> result_list.append(int(digit))
>
> digit will be assigned to successive 1 character long strings.  Since
> you wanted a list of integers, you have to convert it back.
>
> If you are learning python you may be interested in the tutor mailing
> list. https://mail.python.org/mailman/listinfo/tutor
>
> Laura
>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Readlines returns non ASCII character

2015-09-23 Thread Ian Kelly
On Wed, Sep 23, 2015 at 6:47 AM, SANKAR .  wrote:
> Hi all,
>
> I am not a expert programmer but I have to extract information from a large
> file.
>  I used  codecs.open(..) with UTF16 encoding to read this file. It could
> read all the lines in the file but returns with the non Ascii characters.
> Below are 5 sample lines. How do I avoid having this non Ascii items. Is
> there a better way to read this?

I suspect that what you want is not "non-ASCII" but just to read the
file without all the mojibake, which is likely an indication that
you're using the wrong encoding.

Do you know that UTF-16 is actually the encoding of the file?

Based on the spaces that appear between adjacent characters, I would
guess that this is probably in a 32-bit encoding, perhaps UTF-32. On
the other hand, the repeated 0x00ff 0x00fe 0x00ff are very curious; I
don't see how that could be valid UTF-32. Are you sure that this is a
text file and not some propietary binary data format?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-23 Thread Tim Daneliuk
On 09/22/2015 04:43 PM, Chris Roberts wrote:
> 
> (How do I make it into an index? )
> Preferably something fairly easy to understand as I am new at this.
> 
> results = 134523  #(Integer) 
> 
> Desired: 
> results = [1, 2, 3, 4, 5, 2, 3]   #(INDEX)
> 
> Somehow I see ways to convert index to list to int, but not back again.
> 
> Thanks, 
> crzzy1
> 



results = [x for x in str(results)]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-23 Thread Denis McMahon
On Tue, 22 Sep 2015 14:43:55 -0700, Chris Roberts wrote:

> results = 134523  #(Integer)

This appears to be an integer expressed (presumably) in base 10 with 6 
digits

> Desired:
> results = [1, 2, 3, 4, 5, 2, 3]   #(INDEX)

This appears to be a python list of 7 elements, with the first and the 
the third through seventh elements corresponding to the first and the 
second through sixth most significant digits respectively of the 
previously discussed integer.

I can't actually see any direct method of creating the list given from 
the number given.

However, if I understand the intent of the question you meant to ask, you 
might find that the following code does something interesting:

x = 9876543210
y = []

while x > 0:
y.append(x % 10)
x = int(x / 10)

y = list(reversed(y))
print y

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Readlines returns non ASCII character

2015-09-23 Thread SANKAR .
Hi all,

I am not a expert programmer but I have to extract information from a large
file.
 I used  codecs.open(..) with UTF16 encoding to read this file. It could
read all the lines in the file but returns with the non Ascii characters.
Below are 5 sample lines. How do I avoid having this non Ascii items. Is
there a better way to read this?



Thanks
Sankar


‚  ÿþÿ

2 0 1 5 - 0 8 - 2 8 ÿþÿ1 7 : 2 2 : 0 3 Á   í    0ÿþÿE L E K T A _
E O S _ R T D _ V X 2 ÿþÿ ÿþÿ 0ÿþÿ ÿþÿ ÿþÿ 0ÿþÿ ÿþÿ ÿþÿ 0ÿþÿ ÿþÿ
ÿþÿ  ¸

0ÿþÿ ÿþÿM L C X ÿþÿM L C X  Pÿþÿ0 . 0 ÿþÿ0 . 0 ÿþÿ0 . 0 
Pÿþÿ  ÿþÿ  ÿþÿN o n e  Pÿþÿ
ÿþÿ  ÿþÿ0  Pÿþÿ  ÿþÿ  ÿþÿO U T 
Pÿþÿ  ÿþÿ  ÿþÿ0  Pÿþÿ
ÿþÿ  ÿþÿ- 5 . 0  Pÿþÿ  ÿþÿ  ÿþÿ6
0 . 0   Pÿþÿ  ÿþÿ  ÿþÿ0 . 5 2

 Pÿþÿ  ÿþÿ  ÿþÿ6 . 4 9

 Pÿþÿ  ÿþÿ  ÿþÿ2 0 . 0 0
-- 
https://mail.python.org/mailman/listinfo/python-list


Python 3 windows installer problem [WAS: Re: an installing problem]

2015-09-23 Thread Lorenzo Sutton

Hi,

Not too familiar with the 'new' Python 3 installer on windows.. but

On 23/09/2015 13:37, Narges Asadi wrote:

Hello
I’ve encountered a problem when I wanted to install Python 3.5.
I  sent you the log file. Please help me to fix the problem.


From the log:

[0F4C:1110][2015-09-23T14:54:17]e000: Error 0x80072ee7: Failed to send 
request to URL: 
https://www.python.org/ftp/python/3.5.0/win32/core_pdb.msi, trying to 
process HTTP status code anyway.


which seems to be reachable now... so maybe a network problem when you 
were installing??


Look here about installing without downloading, it might be helpful.

https://docs.python.org/3/using/windows.html#installing-without-downloading

This bug report might also be relevant:
https://bugs.python.org/issue25126

Hope this helps.
Lorenzo.
--
https://mail.python.org/mailman/listinfo/python-list


Re: sort help

2015-09-23 Thread Larry Martell
On Tue, Sep 22, 2015 at 6:55 PM, Chris Angelico  wrote:
> On Wed, Sep 23, 2015 at 8:42 AM, Larry Martell  
> wrote:
>> I currently have 3 lists of lists and I sort them based on a common
>> field into a single list like this:
>>
>> def GetObjKey(a):
>> return a[2]
>>
>> sorted(a + b + c, key=GetObjKey)
>>
>> Which works just fine.
>>
>> But now, I need to have just the first list (a) also sub sorted by
>> another field and I can't quite figure out how to do this.
>
> Have you tried simply sorting a by the other field prior to doing your
> merge-and-sort? The Python list.sort() method is guaranteed to be
> stable. I can't find a comparable guarantee for sorted(), but worst
> case, you should be able to do your list merge, and then explicitly
> name it and sort it.

Thanks to everyone for the replied. I ended up just presorting he
first list, then merging and sorting all 3. Very simple. Not sure why
I didn't see that. Probably comes from working 75 hours/week.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cyber Grand Challenge, prizes up to 2 million dollars ! (DARPA)

2015-09-23 Thread Skybuck Flying

Also very interesting read:

http://blog.trailofbits.com/2015/07/15/how-we-fared-in-the-cyber-grand-challenge/

"
How We Fared in the Cyber Grand Challenge
July 15, 2015 by Artem Dinaburg 6 Comments

The Cyber Grand Challenge qualifying event was held on June 3rd, at exactly 
noon Eastern time. At that instant, our Cyber Reasoning System (CRS) was 
given 131 purposely built insecure programs. During the following 24 hour 
period, our CRS was able to identify vulnerabilities in 65 of those programs 
and rewrite 94 of them to eliminate bugs built in their code. This proves, 
without a doubt, that it is not only possible but achievable to automate the 
actions of a talented software auditor.


Despite the success of our CRS at finding and patching vulnerabilities, we 
did not qualify for the final event, to be held next year. There was a fatal 
flaw that lowered our overall score to 9th, below the 7th place threshold 
for qualification. In this blog post we’ll discuss how our CRS works, how it 
performed against competitor systems, what doomed its score, and what we are 
going to do next.

Cyber Grand Challenge Background

The goal of the Cyber Grand Challenge (CGC) is to combine the speed and 
scale of automation with the reasoning capabilities of human experts. 
Multiple teams create Cyber Reasoning Systems (CRSs) that autonomously 
reason about arbitrary networked programs, prove the existence of flaws in 
those programs, and automatically formulate effective defenses against those 
flaws. How well these systems work is evaluated through head-to-head 
tournament-style competition.


The competition has two main events: the qualifying event and the final 
event. The qualifying event was held on June 3, 2015. The final event is set 
to take place during August 2016. Only the top 7 competitors from the 
qualifying event proceed to the final event.


During the qualifying event, each competitor was given the same 131 
challenges, or purposely built vulnerable programs, each of which contained 
at least one intentional vulnerability. For 24 hours, the competing CRSes 
faced off against each other and were scored according to four criteria. The 
full details are in the CGC Rules, but here’s a quick summary:


   The CRS had to work without human intervention. Any teams found to use 
human assistance were disqualified.
   The CRS had to patch bugs in challenges. Points were gained for every 
bug successfully patched. Challenges with no patched bugs received zero 
points.
   The CRS could prove bugs exist in challenges. The points from patched 
challenges were doubled if the CRS could generate an input that crashed the 
challenge.
   The patched challenges had to function and perform almost as well as the 
originals. Points were lost based on performance and functionality loss in 
the patched challenges.


A spreadsheet with all the qualifying event scores and other data used to 
make the graphs in this post is available from DARPA (Trail of Bits is the 
ninth place team). With the scoring in mind, let’s review the Trail of Bits 
CRS architecture and the design decisions we made.

Preparation

We’re a small company with a distributed workforce, so we couldn’t 
physically host a lot of servers. Naturally, we went with cloud computing to 
do processing; specifically, Amazon EC2. Those who saw our tweets know we 
used a lot of EC2 time. Most of that usage was purely out of caution.


We didn’t know how many challenges would be in the qualifying event — just 
that it would be “more than 100.” We prepared for a thousand, with each 
accompanied by multi-gigabyte network traffic captures. We were also 
terrified of an EC2 region-wide failure, so we provisioned three different 
CRS instances, one in each US-based EC2 region, affectionately named Biggie 
(us-east-1), Tupac (us-west-2), and Dre (us-west-1).


It turns out that there were only 131 challenges and no gigantic network 
captures in the qualifying event. During the qualifying event, all EC2 
regions worked normally. We could have comfortably done the qualifying event 
with 17 c4.8xlarge EC2 instances, but instead we used 297. Out of our 
abundance of caution, we over-provisioned by a factor of ~17x.

Bug Finding

The Trail of Bits CRS was ranked second by the number of verified bugs found 
(Figure 1). This result is impressive considering that we started with 
nothing while several other teams already had existing bug finding systems 
prior to CGC.


Figure 1: Teams in the qualifying event ranked by number of bugs found. 
Orange bars signify finalists.


Our CRS used a multi-pronged strategy to find bugs (Figure 2). First, there 
was fuzzing. Our fuzzer is implemented with a custom dynamic binary 
translator (DBT) capable of running several 32-bit challenges in a single 
64-bit address space. This is ideal for challenges that feature multiple 
binaries communicating with one another. The fuzzer’s instrumentation and 
mutation are separated, allowing for pluggabl

Re: Cyber Grand Challenge, prizes up to 2 million dollars ! (DARPA)

2015-09-23 Thread Skybuck Flying

Also very interesting read:

https://cgc.darpa.mil/CGC_FAQ.pdf

Just the list of common programming mistakes is already pretty interesting ! 
;) =D


Bye,
 Skybuck.

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


Cyber Grand Challenge, prizes up to 2 million dollars ! (DARPA)

2015-09-23 Thread Skybuck Flying

(Click on little icon on website top left for menu):

Information about challenge:

http://www.cybergrandchallenge.com/site/index.html#about

https://cgc.darpa.mil/CGC_Rules_16_May_14_Version_2.pdf

Perhaps this will be a yearly contest.

There is a catch though, to collect the prizes: "The prize recipient shall 
be a citizen, a permanent resident of the United States, or a US Entity. "


Bye,
 Skybuck. 


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


Re: True == 1 weirdness

2015-09-23 Thread Michael Schwarz
On 2015-09-19, at 09:19, Gregory Ewing  wrote:

> Random832 wrote:
>> I'm disputing that chained comparisons are used for the particular
>> combinations that I am actually arguing should not be used in python.
>> Such as a < b > c or a != b != c  [whereas a may or may not be equal to
>> c]
> 
> I can't remember offhand seeing a != b != c written by a
> mathematician, but if I did, I would suspect that he
> *intended* it to imply a != c, even if that's not a strict
> logical consequence of the notation.

Mathematica interprets a != b != c as "none of a, b or c are equal". See [0]. 
It does this by parsing it to Unequal[a, b, c] (square brackets are function 
calls), where Unequal then implements that operation.

Normally I'm used to Mathematica being a very consistent language. But being 
prepared by this thread, I of course wondered where the inconsistencies start 
here and whether inequalities mix well with comparisons. They don't:

While b != c != d gets parsed as this:

Unequal[b, c, d]

But a < b != c != d < e gets parsed as this:

And[Less[a, b], Unequal[b, c], Unequal[c, d], Less[d, e]]

Which means that a != b != c is interpreted differently depending on context. I 
don't think every mathematician would agree which of these interpretations make 
sense. :)

[0]: https://reference.wolfram.com/language/ref/Unequal.html


signature.asc
Description: Message signed with OpenPGP using GPGMail
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Modify environment variable for subprocess

2015-09-23 Thread Laura Creighton
In a message of Wed, 23 Sep 2015 02:51:53 -0700, loial writes:
>I need to modify the LIBPATH environment variable when running a process via 
>subprocess, but otherwise retain the existing environment.
>
>Whats the best way to do that?

import subprocess, os
my_env = os.environ  # if your program should be able to modify the current env
# otherwise
my_env = os.environ.copy() # if it shouldn't

# if you just want to add something to the existing LIBPATH
my_env["LIBPATH"] = "/where/I/want/to/look/first:" + my_env["LIBPATH"]
# otherwise
my_env["LIBPATH"] = "/what/I/want"

subprocess.Popen(my_program, env=my_env)

Setting os.environ leaks memory under Mac OS and FreeBSD.
I am not sure if this means that if  you do this a gazillion times
on a Mac you will have a problem.

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


Re: Modify environment variable for subprocess

2015-09-23 Thread Cameron Simpson

On 23Sep2015 02:51, loial  wrote:

I need to modify the LIBPATH environment variable when running a process via 
subprocess, but otherwise retain the existing environment.

Whats the best way to do that?


Make a copy of os.environ, modify the copy, pass it via the env=parameter of 
subprocess.Popen. That is the most direct and controllable method.


Cheers,
Cameron Simpson 

Tachyon: A gluon that's not completely dry.
--
https://mail.python.org/mailman/listinfo/python-list


Re: problem building python 3.5 extensions for windows

2015-09-23 Thread Robin Becker

On 22/09/2015 22:37, cjgoh...@gmail.com wrote:

On Tuesday, September 22, 2015 at 1:49:16 PM UTC-7, Terry Reedy wrote:

On 9/22/2015 9:35 AM, Robin Becker wrote:

On 22/09/2015 11:14, Robin Becker wrote:

On 22/09/2015 01:36, CG wrote:

.t

.


Thanks for the pointer Christoph.

I certainly didn't let it run for 30 minutes. When I build with 2.7,
3.3 or 3.4
the whole build including reportlab stuff is over in a couple of
minutes. I will
try again, but a linker that takes 30 minutes to create an extension
that ends
up 204Kb long has to be seriously broken. Is it trying to hard? Most
of the code
size is in arrays for code points etc etc.


I timed my builds of pyRXPU for x86 + amd64; these are on a core i5-3470
@ 3.20Ghz with 4Gb memory.

python 3.4   1 minute  14 seconds
python 3.5  52 minutes 50 seconds

so with VS2015 it will now take me an hour to make and test any changes
to this extension. I don't see how the issue can be considered closed.
VS2015 is clearly not the way forward for any reasonable development
process.


I think you should add the above to the issue.

--
Terry Jan Reedy


It's a compiler bug. To work around, disable compiler optimizations, i.e. set 
`extra_compile_args=['/Od']` in setup.py.

--
Christoph

Thanks Christoph, I guessed it must be something like that. I suppose it's 
trying desperately to place small chunks of encoding tables or something.


--
Robin Becker

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


Modify environment variable for subprocess

2015-09-23 Thread loial
I need to modify the LIBPATH environment variable when running a process via 
subprocess, but otherwise retain the existing environment.

Whats the best way to do that?

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


Re: Successfully send sms with python

2015-09-23 Thread Cameron Simpson

On 23Sep2015 01:46, Pavel S  wrote:

I don't understand why all of you are telling him about '\r\n\, write(),..' 
instead of recommending to use take library which already has all problems 
resolved (python-gammu / wammu).


Sometimes it is useful to know how things are done instead of which big dumb 
button to push. Besides, he came here saying: I'm doing this, what am I doing 
wrong? We're trying to help him with this.


It is not always about the final destination you know.

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


Re: Dummy Decoder Example (was Re: Parallel decoding lesson for you.)

2015-09-23 Thread Skybuck Flying

Also here is test set 2 to test input values:

// input test 2, c version
a1 = 1; a2 = 0; a3 = 0; a4 = 1;
b1 = 1; b2 = 1; b3 = 0;
c1 = 1;
d1 = 1; d2 = 1; d3 = 0; d4 = 0; d5 = 1; d6 = 0;

Bye,
 Skybuck.

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


Re: Dummy Decoder Example (was Re: Parallel decoding lesson for you.)

2015-09-23 Thread Skybuck Flying
Here is the C version of the example in case your Delphi-to-C skills are not 
so great or you lazy lol =D:


// ParallelDecodingCVersion.cpp : Defines the entry point for the console 
application.

//

#include "stdafx.h"

// Begin of Dummy Decoder Example

const int MaxProcessorCount = 4;

int _tmain(int argc, _TCHAR* argv[])
{
// information stream, input
int Stream[21+(MaxProcessorCount-1)]; // add max processor count to create a 
safe "padding" for reading so no out of bounds/range check errors with 
arrays.


// bits representing fields of data
int a1,a2,a3,a4;
int b1,b2,b3;
int c1;
int d1,d2,d3,d4,d5,d6;

// output
int RowIndex;

int RowCount;
int RowLength[6];
int RowOffset[6];

int DataOffset;

int FieldCount;
int FieldLength;

int Processor[4];

// debug fields
int FieldA;
int FieldB;
int FieldC;
int FieldD;

a1 = 1; a2 = 1; a3 = 1; a4 = 1;
b1 = 1; b2 = 1; b3 = 1;
c1 = 1;
d1 = 1; d2 = 1; d3 = 1; d4 = 1; d5 = 1; d6 = 1;

// compute input fields to compare it later with output fields
FieldA = (a1) | (a2 << 1) | (a3 << 2) | (a4 << 3);
FieldB = (b1) | (b2 << 1) | (b3 << 2);
FieldC = (c1);
FieldD = (d1) | (d2 << 1) | (d3 << 2) | (d4 << 3) | (d5 << 4) | (d6 << 5);

// print field values
printf( "FieldD: %d \n", FieldD );
printf( "FieldA: %d \n", FieldA );
printf( "FieldB: %d \n", FieldB );
printf( "FieldC: %d \n\n", FieldC );

// number of rows
Stream[0] = 6;

// row lengths
Stream[1] = 4;
Stream[2] = 3;
Stream[3] = 3;
Stream[4] = 2;
Stream[5] = 1;
Stream[6] = 1;

// sorted information stream:
// d1a1b1c1d2a2b2d3a3b3d4a4d5d6

// data bits
Stream[7] = d1;
Stream[8] = a1;
Stream[9] = b1;
Stream[10] = c1;
Stream[11] = d2;
Stream[12] = a2;
Stream[13] = b2;
Stream[14] = d3;
Stream[15] = a3;
Stream[16] = b3;
Stream[17] = d4;
Stream[18] = a4;
Stream[19] = d5;
Stream[20] = d6;

// now the decoding algorithm:

// determine number of rows
RowCount = Stream[0];

// extract row lengths
RowLength[0] = Stream[1];
RowLength[1] = Stream[2];
RowLength[2] = Stream[3];
RowLength[3] = Stream[4];
RowLength[4] = Stream[5];
RowLength[5] = Stream[6];

// determine field count
FieldCount = RowLength[0]; // row[0] indicates number of fields.

// I will help out a bit... by leaving this code in ! ;) seems somewhat 
obvious ;)

// first determine data offset properly ! ;) :) 1 for the row count +
// RowCount to skip over row lengths.
DataOffset = 1 + RowCount;

RowOffset[0] = DataOffset;
RowOffset[1] = RowOffset[0] + RowLength[0];
RowOffset[2] = RowOffset[1] + RowLength[1];
RowOffset[3] = RowOffset[2] + RowLength[2];
RowOffset[4] = RowOffset[3] + RowLength[3];
RowOffset[5] = RowOffset[4] + RowLength[4];


// some how the data bits from the stream needs to end up in these 4
// processors so that it produces the same values
// as below:
// fields may be processed in a different order though.

// *** You will need to replace this code with your own code... and
// preferably it should be parallel, fast and somewhat general/scalable. ***
Processor[0] = FieldD;
Processor[1] = FieldA;
Processor[2] = FieldB;
Processor[3] = FieldC;

// print processor values.
printf( "Processor[0]: %d \n", Processor[0] );
printf( "Processor[1]: %d \n", Processor[1] );
printf( "Processor[2]: %d \n", Processor[2] );
printf( "Processor[3]: %d \n\n", Processor[3] );

return 0;
}

// End of Dummy Decoder Example

Bye,
 Skybuck :)


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


Re: Successfully send sms with python

2015-09-23 Thread Laura Creighton
In a message of Tue, 22 Sep 2015 22:25:31 -0600, Michael Torrie writes:

>Consider something like this with no error checking when using the
>serial port, no context managers for the serial device:
>file sms.py:
>--
>import serial
>import time
>
>serial_port = 'COM13'
>timeout = 5
>baud = 460800
>
>def send_message(recipient, message):
>ser = serial.Serial(serial_port, baud, timeout=timeout)
>time.sleep(1)
>self.ser.write('ATZ\r')
>time.sleep(1)
>self.ser.write('AT+CMGF=1\r')
>time.sleep(1)
>self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r\n''')
>time.sleep(1)
>self.ser.write(self.content + "\r\n")
>time.sleep(1)
>self.ser.write(chr(26))
>time.sleep(1)
>print "message sent!"
>
>ser.close()

2 questions.
Why all the sleep(1)s?
and don't you need to flush the thing after each write?

Laura

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


Re: Dummy Decoder Example (was Re: Parallel decoding lesson for you.)

2015-09-23 Thread Skybuck Flying

The example may be modified as much as needed.

For now my solution needs a little reading pad to avoid costly mods or 
branches or whatever.


I think this is a nice speedy solution, so code may be modified as follows:

const
MaxProcessorCount = 4;
var
// information stream, input
Stream : array[0..20+(MaxProcessorCount-1)] of integer; // add max processor 
count to create a safe "padding" for reading so no out of bounds/range check 
errors with arrays.


Bye,
 Sybuck.


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


Re: 64bit Python builds on HP-UX ia64 and PA-RISC (Using GCC)

2015-09-23 Thread Laura Creighton
In a message of Wed, 23 Sep 2015 09:03:21 +0100, James Matthews writes:
>Hi,
>
>I'm having some issues getting 64bit Python builds on HP-UX. I'm using the
>GCC version available from the HP website. I've also tried using HP's
>compiler but don't have much success either, even following the readme.
>These are the results I get:
>
>PA-RISC:
>
>./configure CC=/opt/hp-gcc64-4.7.1/bin/gcc
>make
>
>Traceback (most recent call last):
>  File "./setup.py", line 2240, in 
>main()
>  File "./setup.py", line 2235, in main
>'Lib/smtpd.py']
>  File "/tmp/jxm/Python-2.7.10/Lib/distutils/core.py", line 151, in setup
>dist.run_commands()
>  File "/tmp/jxm/Python-2.7.10/Lib/distutils/dist.py", line 953, in
>run_commands
>self.run_command(cmd)
>  File "/tmp/jxm/Python-2.7.10/Lib/distutils/dist.py", line 972, in
>run_command
>cmd_obj.run()
>  File "/tmp/jxm/Python-2.7.10/Lib/distutils/command/build.py", line 127,
>in run
>self.run_command(cmd_name)
>  File "/tmp/jxm/Python-2.7.10/Lib/distutils/cmd.py", line 326, in
>run_command
>self.distribution.run_command(command)
>  File "/tmp/jxm/Python-2.7.10/Lib/distutils/dist.py", line 972, in
>run_command
>cmd_obj.run()
>  File "/tmp/jxm/Python-2.7.10/Lib/distutils/command/build_ext.py", line
>337, in run
>self.build_extensions()
>  File "./setup.py", line 251, in build_extensions
>build_ext.build_extensions(self)
>  File "/tmp/jxm/Python-2.7.10/Lib/distutils/command/build_ext.py", line
>446, in build_extensions
>self.build_extension(ext)
>  File "./setup.py", line 287, in build_extension
>if not self.configure_ctypes(ext):
>  File "./setup.py", line 2041, in configure_ctypes
>exec f in fficonfig
>  File "build/temp.hp-ux-B.11.31-9000-800-2.7/libffi/fficonfig.py", line
>33, in 
>ffi_sources += ffi_platforms['PA64_HPUX']
>KeyError: 'PA64_HPUX'
>*** Error exit code 1
>
>During make I also get lots of errors like this: ./pyconfig.h:1188:0:
>warning: "_POSIX_C_SOURCE" redefined [enabled by default]
>
>
>ia64:
>
>./configure CC=/opt/hp-gcc-4.7.1/bin/gcc CFLAGS="-mlp64"
>Make
>
>./pyconfig.h:1188:0: warning: "_POSIX_C_SOURCE" redefined [enabled by
>default]
>:0:0: note: this is the location of the previous definition
>/opt/hp-gcc-4.7.1/bin/gcc -pthread -DNDEBUG -g -fwrapv -O3 -Wall
>-Wstrict-prototypes  Parser/acceler.o  Parser/grammar1.o
>Parser/listnode.o  Parser/node.o  Parser/parser.o  Parser/parsetok.o
>Parser/bitset.o  Parser/metagrammar.o  Parser/firstsets.o
>Parser/grammar.o  Parser/pgen.o Objects/obmalloc.o  Python/mysnprintf.o
>Python/pyctype.o  Parser/tokenizer_pgen.o  Parser/printgrammar.o
>Parser/pgenmain.o -lnsl -lrt -ldld -ldl  -o Parser/pgen
>ld: Mismatched Data ABI. Expected None but found EF_IA_64_ABI64 in file
>Parser/acceler.o
>Fatal error.
>collect2: error: ld returned 1 exit status
>*** Error exit code 1
>Stop.
>*** Error exit code 1
>
>Seems to be a library mismatch?
>
>Any help would be appreciated.
>
>Regards,
>James
>-- 
>https://mail.python.org/mailman/listinfo/python-list

I think you have this problem.
http://h30499.www3.hp.com/t5/Languages-and-Scripting/Conpilation-errors-on-HP-UX-11-31-IA64/td-p/4777801#.VgJtfUlZLZs

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


Re: Successfully send sms with python

2015-09-23 Thread Pavel S
I don't understand why all of you are telling him about '\r\n\, write(),..' 
instead of recommending to use take library which already has all problems 
resolved (python-gammu / wammu).

When one will write custom templating stuff, you would also recommend him to 
take jinja.
-- 
https://mail.python.org/mailman/listinfo/python-list


64bit Python builds on HP-UX ia64 and PA-RISC (Using GCC)

2015-09-23 Thread James Matthews
Hi,

I'm having some issues getting 64bit Python builds on HP-UX. I'm using the
GCC version available from the HP website. I've also tried using HP's
compiler but don't have much success either, even following the readme.
These are the results I get:

PA-RISC:

./configure CC=/opt/hp-gcc64-4.7.1/bin/gcc
make

Traceback (most recent call last):
  File "./setup.py", line 2240, in 
main()
  File "./setup.py", line 2235, in main
'Lib/smtpd.py']
  File "/tmp/jxm/Python-2.7.10/Lib/distutils/core.py", line 151, in setup
dist.run_commands()
  File "/tmp/jxm/Python-2.7.10/Lib/distutils/dist.py", line 953, in
run_commands
self.run_command(cmd)
  File "/tmp/jxm/Python-2.7.10/Lib/distutils/dist.py", line 972, in
run_command
cmd_obj.run()
  File "/tmp/jxm/Python-2.7.10/Lib/distutils/command/build.py", line 127,
in run
self.run_command(cmd_name)
  File "/tmp/jxm/Python-2.7.10/Lib/distutils/cmd.py", line 326, in
run_command
self.distribution.run_command(command)
  File "/tmp/jxm/Python-2.7.10/Lib/distutils/dist.py", line 972, in
run_command
cmd_obj.run()
  File "/tmp/jxm/Python-2.7.10/Lib/distutils/command/build_ext.py", line
337, in run
self.build_extensions()
  File "./setup.py", line 251, in build_extensions
build_ext.build_extensions(self)
  File "/tmp/jxm/Python-2.7.10/Lib/distutils/command/build_ext.py", line
446, in build_extensions
self.build_extension(ext)
  File "./setup.py", line 287, in build_extension
if not self.configure_ctypes(ext):
  File "./setup.py", line 2041, in configure_ctypes
exec f in fficonfig
  File "build/temp.hp-ux-B.11.31-9000-800-2.7/libffi/fficonfig.py", line
33, in 
ffi_sources += ffi_platforms['PA64_HPUX']
KeyError: 'PA64_HPUX'
*** Error exit code 1

During make I also get lots of errors like this: ./pyconfig.h:1188:0:
warning: "_POSIX_C_SOURCE" redefined [enabled by default]


ia64:

./configure CC=/opt/hp-gcc-4.7.1/bin/gcc CFLAGS="-mlp64"
Make

./pyconfig.h:1188:0: warning: "_POSIX_C_SOURCE" redefined [enabled by
default]
:0:0: note: this is the location of the previous definition
/opt/hp-gcc-4.7.1/bin/gcc -pthread -DNDEBUG -g -fwrapv -O3 -Wall
-Wstrict-prototypes  Parser/acceler.o  Parser/grammar1.o
Parser/listnode.o  Parser/node.o  Parser/parser.o  Parser/parsetok.o
Parser/bitset.o  Parser/metagrammar.o  Parser/firstsets.o
Parser/grammar.o  Parser/pgen.o Objects/obmalloc.o  Python/mysnprintf.o
Python/pyctype.o  Parser/tokenizer_pgen.o  Parser/printgrammar.o
Parser/pgenmain.o -lnsl -lrt -ldld -ldl  -o Parser/pgen
ld: Mismatched Data ABI. Expected None but found EF_IA_64_ABI64 in file
Parser/acceler.o
Fatal error.
collect2: error: ld returned 1 exit status
*** Error exit code 1
Stop.
*** Error exit code 1

Seems to be a library mismatch?

Any help would be appreciated.

Regards,
James
-- 
https://mail.python.org/mailman/listinfo/python-list


ANN: eGenix mxODBC Connect 2.1.4 - Remote Python Database Interface

2015-09-23 Thread eGenix Team: M.-A. Lemburg


ANNOUNCING

  eGenix.com mxODBC Connect

  Remote Python Database Interface

Version 2.1.4


 mxODBC Connect is our commercially supported client-server product for
   connecting Python applications to relational databases
 in a truly platform independent way.


This announcement is also available on our website for online reading:
http://www.egenix.com/company/news/eGenix-mxODBC-Connect-2.1.4-GA.html



INTRODUCTION

The mxODBC Connect Database Interface for Python allows users to
easily connect Python applications to all major databases on the
market today in a highly portable, convenient and secure way.

Python Database Connectivity the Easy Way
-

Building on our mxODBC database interface for Python, mxODBC Connect
is designed as client-server application, so you no longer need to
find production quality database drivers for all platforms you target
with your Python application.

Instead, you use an easy to install royalty-free Python client library
which connects directly to the mxODBC Connect database server over the
network.

This makes mxODBC Connect a great basis for writing cross-platform
multi-tier database applications and utilities in Python, especially
if you run applications that need to communicate with databases such
as MS SQL Server and MS Access, Oracle Database, IBM DB2 and Informix,
Sybase ASE and Sybase Anywhere, MySQL, PostgreSQL, SAP MaxDB and many
more, that run on Windows or Linux machines.

Ideal for Database Driven Client Applications
-

By removing the need to install and configure ODBC drivers on the
client side and dealing with complicated network setups for each set
of drivers, mxODBC Connect greatly simplifies deployment of database
driven client applications, while at the same time making the network
communication between client and database server more efficient and
more secure.

For more information, please have a look at the mxODBC Connect product
page, in particular, the full list of available features.

For more information, please see the product page:

http://www.egenix.com/products/python/mxODBCConnect/



NEWS

mxODBC Connect 2.1.4 is a patch level release of our successful mxODBC
Connect database product. It includes these enhancements and fixes:

Security Enhancements
-

 * Updated included OpenSSL libraries to 1.0.1p. Please see the
   egenix-pyopenssl change log for a complete list of changes. Among
   other security fixes, this addresses the Logjam attack.

   http://www.egenix.com/products/python/pyOpenSSL/changelog.html

mxODBC Connect Enhancements
---

 * Added support for the BinaryNull work-around added to mxODBC 3.3.5
   in order to better support VARBINARY columns in MS SQL Server.

   Both mxODBC Connect Client and Server will need to upgraded to
   version 2.1.4 in order to be able to use the new singleton.

 * The mxODBC Connect Client can now be compiled to a wheel file to
   simplify deployment. Simply point the pip at the prebuilt archive.

mxODBC API Enhancements
---

 * Upgraded the mxODBC Connect Server to mxODBC 3.3.5:

   http://www.egenix.com/company/news/eGenix-mxODBC-3.3.5-GA.html

MS SQL Server
-

 * Documented and recommended use of SET NOCOUNT ON for running
   multiple statements or stored procedures. This can not only resolve
   issues with error reporting, it also results in better performance.

 * Added a work-around for MS SQL Server Native Client to be able to
   support VARCHAR/VARBINARY(MAX) columns when using the Native Client
   with direct execution mode or Python type binding mode. Thanks to
   ZeOmega for reporting this.

 * Added new helper singleton BinaryNull to allow binding a NULL to a
   VARBINARY column with SQL Server in direct execution mode or Python
   type binding mode (as used for FreeTDS). Using the usual None
   doesn't work in those cases, since SQL Server does not accept a
   VARCHAR data type as input for VARBINARY, except by using an
   explicit "CAST(? AS VARBINARY)". mxODBC binds None as VARCHAR for
   best compatibility, when not getting any type hints from the ODBC
   driver.

 * Added a fix for the MS SQL Server Native Client error
   "[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]The data
   types varchar and text are incompatible in the equal to operator."
   when trying to bind a string of more than 256 bytes to a VARCHAR
   column while using cursor.executedirect(). cursor.execute() was
   unaffected by this. Thanks to Paul Perez for reporting this.

 * Added a note to avoid using "execute " when calling stored
   procedures with 

Re: sort help

2015-09-23 Thread Peter Otten
Larry Martell wrote:

> I currently have 3 lists of lists and I sort them based on a common
> field into a single list like this:
> 
> def GetObjKey(a):
> return a[2]
> 
> sorted(a + b + c, key=GetObjKey)
> 
> Which works just fine.
> 
> But now, I need to have just the first list (a) also sub sorted by
> another field and I can't quite figure out how to do this.
> 
> So for example, if my initial data was this (I'll only show the fields
> involved with the sort - the first number is a[2] above and the second
> is the new additional sorting field, only present in a)
> 
> a[1, 4]
> a[1, 2]
> a[2, 3]
> a[2, 1]
> a[5, 6]
> a[5, 2]
> b[2]
> b[5]
> c[1]
> c[6]
> 
> Then I'd want my sorted list to be this:
> 
> a[1,2]
> a[1,4]
> c[1]
> a[2,1]
> a[2,3]
> b[2]
> a[5,2]
> a[5,6]
> b[5]
> c[6]
> 
> I hope that's clear.
> 
> So is there some pythonic way to sort this without resorting to a
> brute force old fashioned plow through the data?

Performing two sorts as suggested by Chris and Paul is cleaner, but it is 
possible to write the key function you were probably looking for.

def get_obj_key(a):
if has_extra_field(a):
return a[2], 0, a[-1] # assuming a[-1] is the extra field
else:
return a[2], 1

You have to write the has_extra_field() test yourself as I don't know what 
distinguishes the items in a from those in the other sequences. For example:

>>> a = [[1, 4], [1, 2], [2, 3], [2, 1], [5, 6], [5, 2]]
>>> b = [[2], [5]]
>>> c = [[1], [6]]
>>> sorted(a + b + c,
... key=lambda a: (a[0], 0, a[-1]) if len(a) > 1 else (a[0], 1))
[[1, 2], [1, 4], [1], [2, 1], [2, 3], [2], [5, 2], [5, 6], [5], [6]]


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


Re: Python, convert an integer into an index?

2015-09-23 Thread marco . nawijn
On Wednesday, September 23, 2015 at 1:27:51 AM UTC+2, MRAB wrote:
> On 2015-09-22 23:21, Laura Creighton wrote:
> > In a message of Tue, 22 Sep 2015 14:43:55 -0700, Chris Roberts writes:
> >>
> >>
> >>(How do I make it into an index? )
> >>Preferably something fairly easy to understand as I am new at this.
> >>
> >>results = 134523  #(Integer)
> >>
> >>Desired:
> >>results = [1, 2, 3, 4, 5, 2, 3]   #(INDEX)
> >>
> >>Somehow I see ways to convert index to list to int, but not back again.
> >>
> >>Thanks,
> >>crzzy1
> >
> > You need to convert your results into a string first.
> >
> > result_int=1234523
> > result_list=[]
> >
> > for digit in str(result_int):
> >  result_list.append(int(digit))
> >
> > digit will be assigned to successive 1 character long strings.  Since
> > you wanted a list of integers, you have to convert it back.
> >
> > If you are learning python you may be interested in the tutor mailing
> > list. https://mail.python.org/mailman/listinfo/tutor
> >
> A shorter way using strings:
> 
> >>> results = 134523
> >>> list(map(int, str(results)))
> [1, 3, 4, 5, 2, 3]
Or you can use a list comprehension:

>>> result = [int(c) for c in str(134523)]
>>> print result
[1, 3, 4, 5, 2, 3]
-- 
https://mail.python.org/mailman/listinfo/python-list