Re: How to run self-contained Python scripts who don't need Python installation?

2017-06-09 Thread Akira Li
"Juan C."  writes:

> I need to run some Python 3.6.0 scripts on the users' machines (W7 and
> W10) in an enterprise environment, but I can't install Python on those
> machines. I tried looking for those "py to exe", but sadly they don't
> support Python 3.6.0.

I've tried PyInstaller (development version) and it works with Python 3.6:

  $ py -3.6 -m pip install 
https://github.com/pyinstaller/pyinstaller/archive/develop.zip

From
https://www.reddit.com/r/Python/comments/6fpr70/how_do_i_distribute_a_py_file_and_its/


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


Re: Is An Element of a Sequence an Object?

2017-06-04 Thread Akira Li
Jon Forrest  writes:

> I'm learning about Python. A book I'm reading about it
> says "...
> a string in Python is a sequence.

correct.

> A sequence is an ordered collection of objects".

correct. https://docs.python.org/3/glossary.html#term-sequence

> This implies that each character in a string
> is itself an object.

Everything in Python is an object. If *s* is a name that refers to a str
object then *s[i]* returns i-th Unicode code point from the string as a
str object of length 1:

  >>> s = "ab"
  >>> s[0]
  'a'

It does not matter how *s* is represented internally on a chosen Python
implementation: *s[0]* may return an existing object or it may create a
new one on-the-fly.

Here's a perfectly valid sequence of ten squares:

  >>> class Squares:
  ... def __getitem__(self, i):
  ... if not (-len(self) <= i < len(self)):
  ... raise IndexError(i)
  ... if i < 0:
  ... i += len(self)
  ... return i * i
  ...
  ... def __len__(self):
  ... return 10
  >>> s = Squares()
  >>> s[9]
  81

All 10 squares are generated on-the-fly (though all int objects already
exist due to the small int caching on CPython).

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


Re: Transitioning from Linux to Windows

2017-06-04 Thread Akira Li
chitt...@uah.edu writes:

> ...
> Ideally, I would like to set up the user on their Windows 7/10 system
> so that they can "login" to the ubuntu system (say putty) - change
> working directory (to where desired) - run the script (on the ubuntu
> system) - and scp the file back to the windows desktop.
>
> ("porting" the ubuntu script to anaconda(3) on the windows desktop IS
> possible (but it has not been as easy as I had hoped!) (django and
> programs like that do seem to provide a "GUI" to have python scripts
> run on ubuntu/systems - but the setup looks mysterious/complicated (to
> me anyway))
>
> I stumbled onto "paramiko" - is that a possible answer?  
> ...


You could use "fabric" http://www.fabfile.org/ to automate running shell
commands (such as python scripts) via ssh.

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


Re: Top Python Interview Questions

2017-05-26 Thread Akira Li
Terry Reedy  writes:

> On 5/26/2017 1:03 AM, Aarusha wrote:
>> PYTHON INTERVIEW QUESTIONS
>>
>> Mindmajix has compiled Python Interview questions which would
>> benefit the learners to attend the Python interviews.
>>
>> Q. How is Python executed?
>
> It depends on the implementation (interpreter or compiler).
>
>> Python files are compiled to bytecode.
>
...
> Cython and others compiles to C (or C++) which is compiled to machine code.

Cython generates C/C++ code that depends on CPython API such as
PyNumber_TrueDivide(). For comparison, RPython (a restricted subset of
Python used by Pypy) can be statically compiled.

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


[issue29352] provide the authorative source for s[i:j] negative slice indices (<-len(s)) behavior for standard sequences

2017-03-17 Thread Akira Li

Changes by Akira Li <4kir4...@gmail.com>:


--
pull_requests: +576

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue29352>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29352] provide the authorative source for s[i:j] negative slice indices (<-len(s)) behavior for standard sequences

2017-03-17 Thread Akira Li

Akira Li added the comment:

I prefer the wording in the current patch. Though I don't have strong feelings 
one way or the other as long as the behavior is specified explicitly.

--

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue29352>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28876] bool of large range raises OverflowError

2017-03-17 Thread Akira Li

Akira Li added the comment:

> Akira, could you open a pull request on GitHub?

Done. PR 699

--

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28876>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28876] bool of large range raises OverflowError

2017-03-17 Thread Akira Li

Changes by Akira Li <4kir4...@gmail.com>:


--
pull_requests: +572

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28876>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29352] provide the authorative source for s[i:j] negative slice indices (<-len(s)) behavior for standard sequences

2017-01-23 Thread Akira Li

New submission from Akira Li:

I've failed to find where the behavior for negative indices in s[i:j]
expression (i, j < -len(s)) for standard sequences (str, list, etc) is
formally defined.

The observed behavior implemented in PySlice_GetIndicesEx(): If "len(s)
+ i" or "len(s) + j" is negative, use 0. [1] I don't see it in the docs.

if (*start < 0) *start += length;
if (*start < 0) *start = (*step < 0) ? -1 : 0;
...
if (*stop < 0) *stop += length;
if (*stop < 0) *stop = (*step < 0) ? -1 : 0;

The tutorial mentions [2]:

> out of range slice indexes are handled gracefully when used for
> slicing"

slice.indices() documentation says [3]:

> Missing or out-of-bounds indices are handled in a manner consistent
> with regular slices.

Neither define it explicitly.

The behavior for the upper boundary is defined explicitly [4]:

> If *i* or *j* is greater than ``len(s)``, use ``len(s)``

I've added the documentation patch that defines the behavior for the
lower boundary too.

[1] Objects/sliceobject.c
[2] Doc/tutorial/introduction.rst
[3] Doc/reference/datamodel.rst
[4] Doc/library/stdtypes.rst

--
assignee: docs@python
components: Documentation
files: docs-negative-slice-indices.patch
keywords: patch
messages: 286098
nosy: akira, docs@python
priority: normal
severity: normal
status: open
title: provide the authorative source for s[i:j] negative slice indices 
(<-len(s)) behavior for standard sequences
versions: Python 3.5, Python 3.6, Python 3.7
Added file: http://bugs.python.org/file46393/docs-negative-slice-indices.patch

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue29352>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28876] bool of large range raises OverflowError

2017-01-23 Thread Akira Li

Akira Li added the comment:

I've updated the patch to use 4-space indent (pep-7).

I've added space around "=" (pep-7); unlike the usual
"dict(designator=value)" -- no space around "=" for keyword argument
(pep-8).

--
Added file: 
http://bugs.python.org/file46391/range_bool-c99-designated-initializers-indent.patch

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28876>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28876] bool of large range raises OverflowError

2017-01-22 Thread Akira Li

Akira Li added the comment:

Following the python-dev discussion [1] I've added a variant of the patch that 
uses c99 designated initializers [2]

[1] https://mail.python.org/pipermail/python-dev/2017-January/147175.html
[2] https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html

--
Added file: 
http://bugs.python.org/file46378/range_bool-c99-designated-initializers.patch

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28876>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28180] sys.getfilesystemencoding() should default to utf-8

2016-12-21 Thread Akira Li

Changes by Akira Li <4kir4...@gmail.com>:


--
nosy: +akira

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28180>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28876] bool of large range raises OverflowError

2016-12-05 Thread Akira Li

Akira Li added the comment:

I've removed the documentation changes from the patch.

--
Added file: http://bugs.python.org/file45773/range_bool-no_docs.patch

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28876>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28876] bool of large range raises OverflowError

2016-12-05 Thread Akira Li

Akira Li added the comment:

Here's a patch with range_bool() implementation, tests and the docs update.

I'm not sure how it should be documented. I've specified it as 
versionchanged:: 3.6

--
keywords: +patch
nosy: +akira
Added file: http://bugs.python.org/file45765/range_bool.patch

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28876>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: User Interface Suggestions? (newbie)

2016-10-05 Thread Akira Li
Beverly Howard  writes:

>...snip...
> A primary question would be, "What are options for building a display
> that would update displayed values without scrolling?"

To rewrite only the last character, you could use '\b':

  import os
  import itertools
  import time
  for c in map(str.encode, itertools.cycle('\-/|')):
  os.write(1, b'\b'+c)
  time.sleep(.3)

To rewrite only the last line, you could use '\r':

  for i in range(1, 101):
  time.sleep(.1)
  print('\r{:4.0%}'.format(i/100), flush=True, end='')

To control the full screen in the terminal e.g., to change the position,
you could use *blessings* module [1]:

  from blessings import Terminal  # $ pip install blessings

  line = 'Line in the middle of the terminal.'
  term = Terminal()
  with term.hidden_cursor(), term.fullscreen():
  x = (term.width - len(line)) // 2
  y = (term.height - 1) // 2
  with term.location(x, y):
  print(term.bold_white_on_black(line))
  
  with term.location(0, term.height - 1):
  input('press  to exit..')
  
For interactive command-line applications from a simple prompt to full
screen terminal applications, you could use *prompt_toolkit* module [2].

> My first goal is to build a program that would interface with a
> Raspberry Pi to control a heating process.
>

For flexibility, you could split your program into a server and a client
that controls it (text, GUI, web).

To control the Raspberry Pi from your mobile device, you could try Kivy
[3] or just create something quick in Pythonista 3 on iOS [4].  To create
a GUI on desktop, you could try Tkinter, Gtk, Qt frameworks [5,6].

For the web part on the server, you could try *flask*, *bottle* modules
[7], to return json data to a client using http protocol:

  from bottle import route # $ pip install bottle

  @route('/data')
  def data():
  return dict(data=[1,2,3])

On the (text/GUI) client in Python:

  import requests # $ pip install requests

  data = requests.get('https://your.server.example.com/data').json()

Or in the browser using jQuery [8]:

  $.getJSON('https://your.server.example.com/data', function(data) {
  //use data here
  });

To make the interaction more responsive, you could use WebSocket protocol e.g., 
via
Flask-SocketIO on the server [9].

[1]: https://pypi.python.org/pypi/blessings/
[2]: https://python-prompt-toolkit.readthedocs.io/
[3]: https://kivy.org
[4]: http://omz-software.com/pythonista/
[5]: http://www.tkdocs.com/tutorial/onepage.html
[6]: http://doc.qt.io/qt-4.8/gallery-gtk.html
[7]: http://bottlepy.org/docs/stable/
[8]: http://api.jquery.com/jquery.getjson/
[9]: https://flask-socketio.readthedocs.io

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


[issue27079] Bugs in curses.ascii predicates

2016-07-01 Thread Akira Li

Akira Li added the comment:

I'm not sure anything should be done (e.g., it is "undefined behavior" to pass 
a negative value such as CHAR_MIN (if *char* type is signed) to a character 
classification function in C. Though EOF value (-1 traditionally) should be 
handled).

If you want to explore it further; I've enumerated open questions in 2014 
(inconsistent TypeError, ord(c) > 0x100, negative ints handling, etc) 
http://bugs.python.org/issue9770#msg221008

--

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27079>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27079] Bugs in curses.ascii predicates

2016-06-30 Thread Akira Li

Akira Li added the comment:

There is an overlapping issue from 2010: "curses.ascii.isblank() function is 
broken. It confuses backspace (BS 0x08) with tab (0x09)" 
http://bugs.python.org/issue9770 

Your patch fixes it too (it should be closed). Note: the patch does not pass 
tests from Lib/test/test_curses_ascii.py attached to issue9770 (even if the 
code: `if char_class_name in ('cntrl', 'punct') test = 
unittest.expectedFailure(test)` is removed) e.g., iscntrl(-1) should be False 
but it returns True:

  $ ./python
  >>> import curses.ascii
  >>> curses.ascii.iscntrl(-1) #XXX expected False
  True

If we ignore negative ints then isblank, ispunct, iscntrl provided in the 
curses_ascii.patch are ok.

--
nosy: +akira

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27079>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27273] subprocess.run(cmd, input='text') should pass universal_newlines=True to Popen

2016-06-08 Thread Akira Li

New submission from Akira Li:

At the moment, subprocess.run(cmd, input='text') raises TypeError.
It would be nice if universal_newlines=isinstance(input, str) if *input* is set.

I've attached a corresponding patch with the necessary changes to the docs, 
tests and the subprocess.run() code.

--
components: Library (Lib)
files: text_input.diff
keywords: patch
messages: 267936
nosy: akira
priority: normal
severity: normal
status: open
title: subprocess.run(cmd, input='text') should pass universal_newlines=True to 
Popen
type: enhancement
versions: Python 3.6
Added file: http://bugs.python.org/file43314/text_input.diff

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27273>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27050] Demote run() below the high level APIs in subprocess docs

2016-06-08 Thread Akira Li

Akira Li added the comment:

> setting "universal_newlines=True" switches to UTF-8 encoded text pipes

It uses locale.getpreferredencoding(False) encoding -- something like 
cp1252,cp1251,etc on Windows, and UTF-8 on *nix with proper locale settings.

It is ASCII (C/POSIX locale default) if the locale is not set in cron, ssh, 
init.d scripts, etc.

If you need a different character encoding, you could use (instead of 
universal_newlines=True):

  pipe = io.TextIOWrapper(process.stdout, encoding=character_encoding)

A better spelling for universal_newlines=True would be text_mode=True.

A summary table (like in itertools' module) would be nice.

check_output() name is unfortunate but it does the right thing and it is not 
hard to use for a beginner --
once somebody discovers it e.g., via "Running shell command from Python and 
capturing the output" Stack Overflow question
http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output

Compare:

  output = check_output([sys.executable, '-c', 'print("abc")'])

and

  output = run([sys.executable, '-c', 'print("abc)'], stdout=PIPE).stdout

The latter command doesn't raise exception if the child process fails.
A beginner has to know about check=True to do the right thing:

  output = run([sys.executable, '-c', 'print("abc")'], stdout=PIPE, 
check=True).stdout

It is easier to refer to check_output() if someone asks "How do I get command's 
output in Python?"


I wish call() did what check_call() does and the current call() behavior would 
be achieved by
the opposite parameter e.g. no_raise_on_status=False being the default:

   rc = call(command, no_raise_on_status=True)

If we can't change the interface then check_call() is the answer to "Calling an 
external command in Python" question
http://stackoverflow.com/questions/89228/calling-an-external-command-in-python

  - check_call(command) -- run command, raise if it fails
  - output = check_output(command) -- get command's output, raise if it fails.
  To pass *data* to the command via its standard input, pass input=data.
  To get/pass text (Unicode) instead of bytes, pass universal_newlines=True
  - check_call("a -- *.jpg | b 2>&1 >output | c", shell=True) -- run a shell 
command as is
  It is a pity that a list argument such as ["ls", "-l"] is allowed with 
shell=True

These cover the most common operations with a subprocess.

Henceforth, run() is more convenient if we don't need to interact with the 
child process while it is running.

For example, if  we introduce the word PIPE (a magic object in the kernel that 
connects processes) then
to capture both standard and error streams of the command:

  cp = run(command, stdout=PIPE, stderr=PIPE)
  output, errors = cp.stdout, cp.stderr

run() allows to get the output and to get the exit status easily: cp.returncode.
Explicit cp.stdout_text, cp.stdout_bytes regardless the text mode would be nice.

To interact with a child process while it is running, Popen() have to be used 
directly.
There could be buffering and other issues (tty vs. pipe), see "Q: Why not just 
use a pipe (popen())?"
http://pexpect.readthedocs.io/en/latest/FAQ.html#whynotpipe

Working with both stdout/stderr or a non-blocking read require threads or 
asyncio, fcntl, etc.

A couple of words should be said about killing a command started with 
shell=True.
(to kill a process tree: set start_new_session=True parameter and call 
os.killpg()).
timeout option doesn't work in this case (it uses Popen.kill()).
check_output() unlike check_call() may wait for grandchildren if they inherit 
the pipe.
Mention Job object on Windows e.g.,
http://stackoverflow.com/questions/23434842/python-how-to-kill-child-processes-when-parent-dies

--
nosy: +akira

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27050>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22274] subprocess.Popen(stderr=STDOUT) fails to redirect subprocess stderr to stdout

2016-05-08 Thread Akira Li

Akira Li added the comment:

Updated the patch to address vadmium's review comments.

--
versions:  -Python 3.4
Added file: 
http://bugs.python.org/file42777/subprocess-stderr_redirect_with_no_stdout_redirect-2.diff

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22274>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Interacting with Subprocesses

2016-05-04 Thread Akira Li
Terry Reedy  writes:

> On 5/4/2016 2:41 PM, Dick Holmes wrote:
>> I am attempting to write a Python program that will interact with
>> a (non-Python) process. The programs will run under MinGW. The
>> process can use stdin/stdout commands and responses and can work
>> with pipes. The problem I'm having is that I can't find any
>> way in Python to have a continuing dialog with the process. I
>> have tried Popen communicate, but that protocol seems to be
>> limited to a single message/response pair, and the response
>> is not returned to the message originator until the process
>> terminates. Unfortunately I don't have access to the process'
>> source code so I can't change the communication medium.
>>
>> Is there some feature that will allow me to initiate the process
>> and execute multiple message/response pairs between the Python
>> program and the process during a single execution of the process?
>
> I have been told that multiprocessing works better for this.  Not sure
> is true.

OP wants to interact with a *non-Python* process—*multiprocessing*
module won't help here.

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


Re: Interacting with Subprocesses

2016-05-04 Thread Akira Li
Dick Holmes  writes:

> I am attempting to write a Python program that will interact with
> a (non-Python) process. The programs will run under MinGW. The
> process can use stdin/stdout commands and responses and can work 
> with pipes. The problem I'm having is that I can't find any
> way in Python to have a continuing dialog with the process. I
> have tried Popen communicate, but that protocol seems to be
> limited to a single message/response pair, and the response
> is not returned to the message originator until the process
> terminates. Unfortunately I don't have access to the process'
> source code so I can't change the communication medium.
>
> Is there some feature that will allow me to initiate the process
> and execute multiple message/response pairs between the Python
> program and the process during a single execution of the process?
>

Pass stdin=PIPE, stdout=PIPE and use p.stdin, p.stdout file objects to
write input, read output from the child process.

Beware, there could be buffering issues or the child process may change
its behavior some other way when the standard input/output streams are
redirected. See
http://pexpect.readthedocs.io/en/stable/FAQ.html#whynotpipe

btw, If pexpect module works in MingGW environment (if pty is
available); you could try it to communicate with the process
interactively.

You might also find the list of Stackoverflow question related to the
subprocess module useful http://stackoverflow.com/tags/subprocess/info


Akira

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


[issue23220] Documents input/output effects of how IDLE runs user code

2016-03-14 Thread Akira Li

Akira Li added the comment:

IDLE can implement functionality similar to what colorama [1] module does on 
Windows: translate ANSI escape character sequences into corresponding GUI 
method calls.

For example, \b might be implemented using a .delete() call, \r using 
.mark_set(), etc.

[1] https://pypi.python.org/pypi/colorama

--
nosy: +akira

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue23220>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23570] Change "with subprocess.Popen():" (context manager) to ignore broken pipe error

2016-02-17 Thread Akira Li

Akira Li added the comment:

Should this issue be reopened in light of
http://bugs.python.org/issue26372 (Popen.communicate not ignoring
BrokenPipeError)?

If .close() shouldn't raise BrokenPipeError in .communicate() (and it
shouldn't) then it seems logical that .close() shouldn't raise
BrokenPipeError in .__exit__() too (and in other subprocess.Popen
methods that do not return until the child process is dead such as
subprocess.run())

--
nosy: +akira

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue23570>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25536] use sys.platform instead of os.name in asyncio docs consistently

2015-11-02 Thread Akira Li

New submission from Akira Li:

asyncio code uses "sys.platform == 'win32'" to detect OS.
asyncio docs use both os.name and sys.platform.

As far as I can tell there is no *practical* difference
between "os.name == 'nt" and "sys.platform == 'win32'"
for choosing asyncio.ProactorEventLoop()

I've attached a patch that replaces all os.name occurrences
in asyncio docs with sys.platform.

--
components: asyncio
files: doc-asyncio-os.name->sys.platform.patch
keywords: patch
messages: 253931
nosy: akira, gvanrossum, haypo, yselivanov
priority: normal
severity: normal
status: open
title: use sys.platform instead of os.name in asyncio docs consistently
versions: Python 3.4, Python 3.5, Python 3.6
Added file: 
http://bugs.python.org/file40930/doc-asyncio-os.name->sys.platform.patch

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25536>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: threading bug in strptime

2015-10-07 Thread Akira Li
Larry Martell  writes:

> We have been trying to figure out an intermittent problem where a
> thread would fail with this:
>
> AttributeError: 'module' object has no attribute '_strptime'
>
> Even though we were importing datetime. After much banging our heads
> against the wall, we found this:
>
> http://code-trick.com/python-bug-attribute-error-_strptime/
>
> The workaround suggested there, to call strptime before starting your
> threads, seems to have fixed the issue.
>
> I thought I'd mention it here in case anyone else is facing this.

I can reproduce it in Python 2 (but not in Python 3) even with
*threading* module:

  #!/usr/bin/env python
  import threading
  import time

  for _ in range(10):
  threading.Thread(target=time.strptime,
   args=("2013-06-02", "%Y-%m-%d")).start()

Don't use *thread* directly (it is even renamed to *_thread* in Python
3, to discourage an unintended usage), use *threading* module instead.

In Python 3.3+, PyImport_ImportModuleNoBlock()  is deprecated
  https://bugs.python.org/issue9260

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


[issue25288] readline.py file in current directory caused unexpected code execution.

2015-10-02 Thread Akira Li

Akira Li added the comment:

python3 -I

could be used as a workaround.

--
nosy: +akira

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25288>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25286] views are not sequences

2015-10-01 Thread Akira Li

Akira Li added the comment:

Thank you for `view`,  hint. I did look for :term:`view` that was
obviously not enough.

The new patch contains the renamed entry in the correct place. All `view`, 
`
occurrences dictionary view are updated now.

--
Added file: http://bugs.python.org/file40654/dict-views-glossary-2.patch

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25286>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25286] views are not sequences

2015-09-30 Thread Akira Li

New submission from Akira Li:

The entry for *dict view* in the glossary may be clarified, to avoid
confusion with collection.abc.Sequence i.e., from:

  They are lazy sequences that will see changes in the underlying
  dictionary.

to something like:

  They provide a dynamic view on the dictionary’s entries, which means
  that when the dictionary changes, the view reflects these changes.

See https://mail.python.org/pipermail/python-ideas/2015-October/036682.html

I've attached the corresponding doc patch.

--
assignee: docs@python
components: Documentation
files: dict-views-glossary.patch
keywords: patch
messages: 251995
nosy: akira, docs@python
priority: normal
severity: normal
status: open
title: views are not sequences
type: behavior
versions: Python 3.4, Python 3.5, Python 3.6
Added file: http://bugs.python.org/file40638/dict-views-glossary.patch

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25286>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22798] time.mktime doesn't update time.tzname

2015-09-29 Thread Akira Li

Akira Li added the comment:

> Would issue22798.diff patch address your issue?

No. The issue is that C mktime() may update C tzname on some platforms
but time.mktime() does not update time.tzname on these platforms while 
the time module docs suggest that it might be expected e.g.:

  Most of the functions defined in this module call platform C library
  functions with the same name. It may sometimes be helpful to consult
  the platform documentation, because the semantics of these functions 
  varies among platforms.

---

Unrelated: time.strftime('%Z') and
time.strftime('%Z', time.localtime(time.time())) can differ on some 
platforms and python versions
  
http://stackoverflow.com/questions/32353015/python-time-strftime-z-is-always-zero-instead-of-timezone-offset

--

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22798>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24773] Implement PEP 495 (Local Time Disambiguation)

2015-09-29 Thread Akira Li

Changes by Akira Li <4kir4...@gmail.com>:


--
nosy: +akira

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24773>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22798] time.mktime doesn't update time.tzname

2015-09-29 Thread Akira Li

Akira Li added the comment:

Marc-Andre Lemburg <rep...@bugs.python.org> writes:
...
> tzname is set when the module is being loaded and not updated
> afterwards (unless you call tzset()). I can't really see why you
> would expect a module global in Python to follow the semantics
> of a C global, unless this is explicitly documented.

Python docs recommend reading platform docs.
Platform docs say that mktime() may change tzname.
Python docs do not contradict.

Why wouldn't I assume that mktime() may change tzname?

> Note: The fact that tzset() does update the module globals is
> not documented.

It is documented e.g., I see: "These will be propagated into
time.tzname" in tzset() docs.

Though tzset() has nothing to do with the issue (TZ envvar hasn't been
changed).

>> Unrelated: time.strftime('%Z') and
>> time.strftime('%Z', time.localtime(time.time())) can differ on some 
>> platforms and python versions
>>   
>> http://stackoverflow.com/questions/32353015/python-time-strftime-z-is-always-zero-instead-of-timezone-offset
>
> The StackOverflow discussion targets %z (lower case z),
> not %Z (with capital Z).

Look at the answers. The examples clearly shows both %Z and %z.

> I think this is just a documentation bug.
>
> Overall, I think that relying on those module globals is
> not a good idea. They are not threadsafe and their values
> can only be trusted right after module import. It's much
> safer to access the resp. values by using struct_time values
> or strftime().
>

Agree. Moreover, you can't trust them even "right after module import"
sometimes.

Though, some predictability in the behavior such as "time.tzname is
whatever C tzname is -- consult the platform docs" would be nice.

--

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22798>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Idiosyncratic python

2015-09-24 Thread Akira Li
Mark Lawrence  writes:

> On 24/09/2015 07:02, Steven D'Aprano wrote:
>> 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?
>>
>
> My favourite was from a guy I worked with years ago.  In C but I'm
> sure you'll enjoy it.  In all functions, something like:-
>
> int flag = 0;
> if flag {
> printf("\nthe string");
> }
> else{
> printf("the string");
> flag = 1;
> }
>
> At least I think I've got it correct, too lazy to check, sorry :)

It looks like a sys.stdout.softspace hack in Python 2:

  print line, # comma!

-- 
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: A little test for you Guys

2015-09-22 Thread Akira Li
Python_Teacher via Python-list  writes:

...
> 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

plural() should accept a single word. To handle list of words, call
map(plural, words)

...
> def str2print(f):
> def str2print_wrap(*args, **kwargs):
> """wrapper"""
> s = f(*args, **kwargs)
> print s
>return str2print_wrap
>
> def hello(s):
> """ Return "Hello $s" """
> return "%s %s" % ("Hello", s)

Use functools.wraps() to preserve the function info for introspection:

  import functools
  
  def prints_result(function):
  @functools.wraps(function)
  def wrapper(*args, **kwargs):
  result = function(*args, **kwargs)
  print(result)
  return result #XXX return
  return wrapper
  
  @prints_result
  def hello(...):
  pass

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


Re: Lightwight socket IO wrapper

2015-09-20 Thread Akira Li
"James Harris"  writes:

> I guess there have been many attempts to make socket IO easier to
> handle and a good number of those have been in Python.
>
> The trouble with trying to improve something which is already well
> designed (and conciously left as is) is that the so-called improvement
> can become much more complex and overly elaborate. That can apply to
> the initial idea, for sure, but when writing helper or convenience
> functions perhaps it applies more to the temptation to keep adding
> just a little bit extra. The end result can be overly elaborate such
> as a framework which is fine where such is needed but is overkill for
> simpler requirements.
>
> Do you guys have any recommendations of some *lightweight* additions
> to Python socket IO before I write any more of my own? Something built
> in to Python would be much preferred over any modules which have to be
> added. I had in the back of my mind that there was a high-level
> socket-IO library - much as threading was added as a wrapper to the
> basic thread module - but I cannot find anything above socket. Is
> there any?

Does ØMQ qualify as lightweight?

> A current specific to illustrate where basic socket IO is limited: it
> normally provides no guarantees over how many bytes are transferred at
> a time (AFAICS that's true for both streams and datagrams) so the
> delimiting of messages/records needs to be handled by the sender and
> receiver. I do already handle some of this myself but I wondered if
> there was a prebuilt solution that I should be using instead - to save
> me adding just a little bit extra. ;-)

There are already convenience functions in stdlib such as
sock.sendall(), sock.sendfile(), socket.create_connection() in addition
to BSD Sockets API.

If you want to extend this list and have specific suggestions; see
  https://docs.python.org/devguide/stdlibchanges.html

Or just describe your current specific issue in more detail here.

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


Re: Lightwight socket IO wrapper

2015-09-20 Thread Akira Li
"James Harris"  writes:
...
> There are a few things and more crop up as time goes on. For example,
> over TCP it would be helpful to have a function to receive a specific
> number of bytes or one to read bytes until reaching a certain
> delimiter such as newline or zero or space etc. 

The answer is sock.makefile('rb') then `file.read(nbytes)` returns a
specific number of bytes.

`file.readline()` reads until newline (b'\n') There is Python Issue:
"Add support for reading records with arbitrary separators to the
standard IO stack"
  http://bugs.python.org/issue1152248
See also
  http://bugs.python.org/issue17083

Perhaps, it is easier to implement read_until(sep) that is best suited
for a particular case.

> Even better would be to be able to use the iteration protocol so you
> could just code next() and get the next such chunk of read in a for
> loop.

file is an iterator over lines i.e., next(file) works.

> When sending it would be good to just say to send a bunch of bytes but
> know that you will get told how many were sent (or didn't get sent) if
> it fails. Sock.sendall() doesn't do that.

sock.send() returns the number of bytes sent that may be less than given.
You could reimplement sock.sendall() to include the number of bytes
successfully sent in case of an error.

> I thought UDP would deliver (or drop) a whole datagram but cannot find
> anything in the Python documentaiton to guarantee that. In fact
> documentation for the send() call says that apps are responsible for
> checking that all data has been sent. They may mean that to apply to
> stream protocols only but it doesn't state that. (Of course, UDP
> datagrams are limited in size so the call may validly indicate
> incomplete transmission even when the first part of a big message is
> sent successfully.)
>
> Receiving no bytes is taken as indicating the end of the
> communication. That's OK for TCP but not for UDP so there should be a
> way to distinguish between the end of data and receiving an empty
> datagram.

There is no end of communication in UDP and therefore there is no end of
data. If you've got a zero bytes in return then it means that you've
received a zero length datagram.

sock.recvfrom() is a thin wrapper around the corresponding C
function. You could read any docs you like about UDP sockets.
  
http://stackoverflow.com/questions/5307031/how-to-detect-receipt-of-a-0-length-udp-datagram

> The recv calls require a buffer size to be supplied which is a
> technical detail. A Python wrapper could save the programmer dealing
> with that.

It is not just a buffer size. It is the maximum amount of data to be
received at once i.e., sock.recv() may return less but never more.
You could use makefile() and read() if recv() is too low-level.

> Reminder to self: encoding issues.
>
> None of the above is difficult to write and I have written the bits I
> need myself but, basically, there are things that would make socket IO
> easier and yet still compatible with more long-winded code. So I
> wondered if there were already some Python modules which were more
> convenient than what I found in the documentation.
>
> James

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


Re: Shutting down a cross-platform multithreaded app

2015-09-18 Thread Akira Li
"James Harris"  writes:

...
> Needless to say, on a test Windows machine AF_UNIX is not present. The
> only cross-platform option, therefore, seems to be to use each
> subthread's select()s to monitor two AF_INET sockets: the one to the
> client and a control one from the master thread. I would seem to need
> IP socket pairs between the master thread and the subthreads. If the
> master thead receives a shutdown signal it will send a shutdown
> command to each subthread.

There is socket.socketpair() on Windows too (since Python 3.5)

  https://docs.python.org/3/library/socket.html#socket.socketpair
  http://bugs.python.org/issue18643

Note: you could use select() to handle signals in the main thread too
(even on Windows since Python 3.5) if you use signal.set_wakeup_fd()

  https://docs.python.org/3/library/signal.html#signal.set_wakeup_fd

It is known as a self-pipe trick

  http://www.sitepoint.com/the-self-pipe-trick-explained/

Look at *asyncio* source code, to see how to get a portable
implementation for various issues with signals. Some issues might still
be opened e.g., Ctrl+C behavior

  http://bugs.python.org/issue24080

Here's how to combine SIGCHLD signal handling with tkinter's event
loop

  http://stackoverflow.com/questions/30087506/event-driven-system-call-in-python

SIGCHLD, createfilehandler() are not portable but the code demonstrates
possible set_wakeup_fd() issues and their solutions (O_NONBLOCK, dummy
signal handler, SA_RESTART, signal coalescing).

On threads and signals in CPython

  http://bugs.python.org/issue5315#msg102829

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


Re: Terminology: "reference" versus "pointer"

2015-09-14 Thread Akira Li
Random832  writes:
...
> Why can't it describe range(1)? A range object in my model would include
> the start, stop, and step; _not_ the contents of what you would get by
> iterating over it; since that's not part of the physical structure of
> the object, but the consequences of calling methods on it.

start, stop, step attributes (corresponding Python ints) may not exist
("the objects we've talking about have never been created") until you
request them explicitly.

I've mentioned it in another message but to be clear, I consider "parcel
tags" [1] and "box and arrows" [2] (boxes are always empty, they only
point to objects) models to be the same and different from "labelled
box" [3] model (boxes contain objects).

[1]
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#python-has-names
[2]
http://www.pythontutor.com/visualize.html#code=a0+%3D+a1+%3D+%5B1,+2%5D%0Ab0+%3D+%5B1,+2%5D%0Ab1+%3D+%5B1,+2%5D%0Aa+%3D+%5Ba0,+a1%5D%0Ab+%3D+%5Bb0,+b1%5D%0Adel+a0,+a1,+b0,+b1%0A=display=opt-frontend.js=false=false=false=3=%5B%5D=6
[3]
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables
 

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


Re: Terminology: "reference" versus "pointer"

2015-09-14 Thread Akira Li
Steven D'Aprano <st...@pearwood.info> writes:

> On Mon, 14 Sep 2015 01:23 pm, Akira Li wrote:
>
>> Steven D'Aprano <st...@pearwood.info> writes:
>> 
>>> On Mon, 14 Sep 2015 11:22 am, Akira Li wrote:
>>>> Look at the last example:
>>>> http://thread.gmane.org/gmane.comp.python.general/782626/focus=782704
>>>
>>>
>>> I'm afraid that page is broken in my browser. Can you not summarise, or
>>> link to the specific message? I may be able to use another browser in a
>>> day or two, but hopefully the discussion will have moved on by then.
>> 
>> https://mail.python.org/pipermail/python-list/2015-September/696631.html
>
> Thanks. You mean this example?
>
>   lst = [range(1, 3) for _ in range(3)]
>   a = [lst[0], lst[0]]
>   b = [lst[1], lst[2]]
>
>
> I don't see what's difficult about this example. Here's a simple ASCII
> drawing:
>
>
> lst > [ range-object-1 , range-object-2 , range-object-3 ]
>
> a --> [ range-object-1 , range-object-1 ]
>
> b --> [ range-object-2 , range-object-3 ]
>

I should have mentioned that lst plays the role of range-object-X in
your diagram i.e., only *a*, *b* names actualy exits (I should add `del
lst` to the example) -- as the original example requires.


> Trying to draw an arrow diagram using text is not my idea of a good time,
> but I'll give it a go. Requires a monospaced font and an email client that
> won't reflow the text:
>
>   +-+--+
>   | |  | <--- a
>   +--|--+---|--+
>  |  |
>  |  |
>  V  |
>   +-+ <-+ ++
>   |range| <---|-   |< lst 
>   +-+ ++
>   +---|-   |
>   +-+ |   ++
>   +-> |range| <---++--|-   |
>   |   +-+  |  ++
>   ||
>   |   +-+  |
>   |   |range| <+
>   |   +-+
>   |  ^
> +-|-+|
> |   ||
> +---+|
> |  -|+
> +---+
>   ^
>   |
>   +--- b
>
>
> Out of the two, I know which one I prefer.

I don't know what it means. If you mean "box and arrows" is superior
somehow then I don't see any difference from the "parcel tags" model
here.

My point is that neither models are detailed enough to describe
meaningfully the behavior of Python code in the general case.

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


Re: Terminology: "reference" versus "pointer"

2015-09-14 Thread Akira Li
Ned Batchelder <n...@nedbatchelder.com> writes:

> On Monday, September 14, 2015 at 3:32:46 PM UTC-4, Akira Li wrote:
>> Ned Batchelder <n...@nedbatchelder.com> writes:
>> ...
>> > What do you feel is missing from Steven's diagram?
>> 
>> I don't feel anything missing because I don't expect the model to be
>> more detailed.
>
> Akira, you said, "neither models are detailed enough to describe
> meaningfully the behavior of Python code in the general case."   
>
> I'm wondering what aspect of the code's behavior isn't captured
> in this model?  I know you didn't expect it to be complete, but I'm
> not sure what you think is missing.
>

I don't understand what you are talking about.

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


Re: Terminology: "reference" versus "pointer"

2015-09-14 Thread Akira Li
Random832 <random...@fastmail.com> writes:

> On Mon, Sep 14, 2015, at 10:48, Akira Li wrote:
>> start, stop, step attributes (corresponding Python ints) may not exist
>> ("the objects we've talking about have never been created") until you
>> request them explicitly.
>
> That's not true in CPython. In fact, the range object in python contains
> *four* reference boxes - one more for length.

Even if it true in CPython. Specific implementations are irrelevant for
the discussions. Python -- the language -- does not mandate any
reference boxes (and given how you interpret the term "box" -- boxes are
not used anywhere).

>> I've mentioned it in another message but to be clear, I consider "parcel
>> tags" [1] and "box and arrows" [2] (boxes are always empty, they only
>> point to objects) models to be the same and different from "labelled
>> box" [3] model (boxes contain objects).
>
> See, I consider the box and arrow to be the same as the labeled box
> model - only the object the boxes contain is an arrow. Except for
> special kinds of boxes implemented in some other language, such as the
> elements of an array from the array module

   [box + arrow pointing to] + object == parcel tag + object

I could draw a picture but it won't be pretty.

"labelled box" model that assumes that objects are inside boxes is
clearly wrong -- it fails if you have two names that refer to the same
object in Python (you can't put the same object into different
boxes).

If a box contains nothing then there is no need for the box. If you
think the box contains "something" then find me the corresponding term
in Python language reference. I don't understand what is "arrow" that
the box might contain exactly. For example I can find what "name",
"object" mean in Python.

> The problem with "parcel tags" is that it represents namespaces - or one
> particular namespace, I've never seen any version of it that even
> clearly talks about locals, let alone different modules - differently
> from other kinds of objects.

I don't understand what are you trying to say here. If you have a
specific example when the "parcel tags" [1] model predicts a _wrong_
behavior; please do provide it. If your point is that "parcel tag" does
not describe in full detail all aspect of the behavior of an arbitrary
Python code then I agree with you (though none of the discussed models
can or should do it).

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


Re: Terminology: "reference" versus "pointer"

2015-09-14 Thread Akira Li
Random832 <random...@fastmail.com> writes:

> On Mon, Sep 14, 2015, at 13:45, Akira Li wrote:
>>[box + arrow pointing to] + object == parcel tag + object
>
> The problem is that if there are multiple namespaces, or if you've also
> got to include references from within other objects such as list, then
> you've got to write all that somehow on the parcel tag, instead of just
> having different places the arrows can start from.
>
> The box and arrow model easily extends to this, and the fact that no-one
> can make up their mind on what to *call* the thing the arrows represent
> doesn't mean they don't exist.

box has arrow, parcel tag has string/thread.
We could attach an arrow instead of a string to the parcel tag.

The box does not contain anything inside (we can attach the arrow
outside the box -- nothing changes).

If the box does not contain anything then we could use a parcel tag
instead.


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


Re: Terminology: "reference" versus "pointer"

2015-09-14 Thread Akira Li
Ned Batchelder  writes:
...
> What do you feel is missing from Steven's diagram?

I don't feel anything missing because I don't expect the model to be
more detailed.

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


Re: Terminology: "reference" versus "pointer"

2015-09-13 Thread Akira Li
Random832 <random...@fastmail.com> writes:

> Akira Li <4kir4...@gmail.com> writes:
>>Rustom Mody <rustompm...@gmail.com> writes:
>>> viz. I have two variables (or names!) say a and b which look the same
>>>>>> a
>>> [[1,2],[1,2]]
>>>>>> b
>>> [[1,2],[1,2]]
>>> And yet doing
>>>>>> a[0][0] = "Oops!"
>>> gives a data structure one "Oops!"
>>> whereas doing it to b mysteriously gives 2
>>
>> Sorry, I haven't followed the whole thread. Could your provide a
>> complete code example? Mention what you expect to happen and what
>> happens instead in your case.
>
> a0 = a1 = [1, 2]
> b0 = [1, 2]
> b1 = [1, 2]
> a = [a0, a1]
> b = [b0, b1]
> del a0, a1, b0, b1
>
> There's nothing about *him* expecting anything wrong to happen. The
> question is how to draw a diagram that unambiguously shows the resulting
> structure using the "parcel tags" model shown in the diagrams (and
> without having a0/a1/etc as actual names)

I'm not sure what "parcel tags" model is but if you mean these
pictures[1] than it works in this case as well as any other (take *a*,
*b* nametags, put them on the corresponding balloons that represents
list objects).

The only names left are *a* and *b* that refer to the corresponding
lists. There is no ambiguity there to put *a*, *b* nametags.

Lists as any other containers contain references to other objects and
therefore "box and arrows" model provides _more details_ here[2,3]

> If the "parcel tags" model can't show it, then the "parcel tag" model
> clearly is not a correct and complete depiction of how Python actually
> works.
> (If I were drawing a picture rather than ASCII I'd add something to make
> it clear that the pairs shown are list objects Like, it's a circle with
> the word "list" and two pointer-boxes inside it.)

"correct and complete" is impossible in the general case for any model.

Imagine what happens if numpy arrays are used instead of Python lists:

  lst = [numpy.array([1, 2]) for _ in range(3)]
  a = [lst[0], lst[0]]
  b = [lst[1], lst[2]]

Note: there could be no a[0][0], a[0][1], etc corresponding Python
objects until you explicitly reference them in the code. If there are no
Python objects then you can't draw any arrows and therefore "box and
arrows" model is incomplete.

Or consider this collections of all Unicode characters:

  class U:
 def __len__(self):
 return sys.maxunicode + 1
 def __getitem__(self, index):
 if index < 0:
 index += len(self)
 if 0 <= index < len(self):
 return chr(index)
 raise IndexError(index)

  u = U()
  print(u[0x61]) # -> a

In this example, it is even more clear that the corresponding items
might not exist until they are explicitly referenced in a program. *u*
is a sequence as any other in Python (it is easy to make it
*collections.abc.Sequence* compatible).

Or this:

  lst = [range(1, 3) for _ in range(3)]
  a = [lst[0], lst[0]]
  b = [lst[1], lst[2]]

In Python 2, it is the exact replica of the original example. In Python
3, it is the same if you don't need to mutate the ranges. Again, the
leaf objects might not exist until you reference them in the code in
Python 3.

Finally, consider a Python sequence that uses os.urandom()
internally. No model will predict the result (and therefore no model is
complete) in this case.


[1]
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#python-has-names
[2]
http://www.pythontutor.com/visualize.html#code=a0+%3D+a1+%3D+%5B1,+2%5D%0Ab0+%3D+%5B1,+2%5D%0Ab1+%3D+%5B1,+2%5D%0Aa+%3D+%5Ba0,+a1%5D%0Ab+%3D+%5Bb0,+b1%5D%0Adel+a0,+a1,+b0,+b1%0A=display=opt-frontend.js=false=false=false=3=%5B%5D=6
[3]
http://www.pythontutor.com/visualize.html#code=a0+%3D+a1+%3D+%5B1,+2%5D%0Ab0+%3D+%5B1,+2%5D%0Ab1+%3D+%5B1,+2%5D%0Aa+%3D+%5Ba0,+a1%5D%0Ab+%3D+%5Bb0,+b1%5D%0Adel+a0,+a1,+b0,+b1%0Aa%5B0%5D%5B0%5D+%3D+%22Oops!%22=display=opt-frontend.js=false=false=false=3=%5B%5D=7


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


Re: Terminology: "reference" versus "pointer"

2015-09-13 Thread Akira Li
Chris Angelico <ros...@gmail.com> writes:

> On Mon, Sep 14, 2015 at 9:17 AM, Akira Li <4kir4...@gmail.com> wrote:
>> If you mean this quote from [1]:
>>
>>   Although we commonly refer to "variables" even in Python (because it's
>>   common terminology), we really mean "names" or "identifiers". In
>>   Python, "variables" are nametags for values, not labelled boxes.
>>
>> then *name* is the term that is defined in the Python language
>> reference. The word "variable" we can use in day-to-day programming
>> _unless_ we are discussing issues that are specific to _naming and
>> binding_.  In that case, we should use the _exact_
>> terminology. Otherwise there is nothing wrong with using "variables"
>> casually in Python.
>
> Since you're talking about precise terminology, I think it would be
> better to say "name binding", rather than "naming and binding". When
> you talk of naming something (or someone!), you generally mean that
> it's possible to go from the thing to the (canonical) name. With
> people, for instance, you can walk up to someone and say "Hi! What's
> your name?", but with Python objects, you fundamentally can't.

"Naming and binding" is the title from the Python language reference
https://docs.python.org/3/reference/executionmodel.html#naming-and-binding

Otherwise, I agree with you ("name" is better here).

>>> Everything you wrote here has the same issue: The "objects" you are
>>> talking about do not physically exist, but are simply the result of
>>> calling a method on the object. Therefore they do not *belong* on the
>>> diagram, and the diagram not showing them does not mean the diagram is
>>> not complete.
>>
>> "do not physically exist" does not make sense. Objects are *never*
>> destroyed explicitly in Python (you can only make them
>> *unreachable*). You can disable garbage collection completely and it is
>> still will be Python. Immutable objects can be considered immortal e.g.:
>>
>>  (1+1) -- question: does the object that represents int(2) exist before
>> the expression is evaluated?
>>
>> The correct answer: it does not matter: int(2) can be created on the
>> fly, a cached int(2) can be reused by a specific implementation --
>> Python doesn't care.
>>
>> I don't see why the model that can't describe range(1) in Python 3
>> pretends to be complete.
>
> "Physically" isn't really the right word for it, given that objects in
> Python code aren't physical and therefore don't *ever* "physically
> exist". My bad there.
>
> But the objects completely do not exist. Yes, with integers you can't
> tell... but what about here?
>
> dependencies = collections.defaultdict(list)
> for fn in files:
> for dep in gather_deps(fn):
> dependencies[dep].append(fn)
>
> Until the moment when dependencies[dep] is requested for some new
> value of dep, the list *does not exist*. It is constructed anew.
> Obviously the end result of this is a dict of lists, and since those
> lists aren't accessible from anywhere else, it makes fine sense to
> talk about those lists as being "contained within" the (default)dict;
> but they clearly didn't exist until they were poked at. They're
> Heisenburg's Lists, I guess...

lists are _mutable_ in Python.

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


Re: Terminology: "reference" versus "pointer"

2015-09-13 Thread Akira Li
Random832 <random...@fastmail.com> writes:

> Akira Li <4kir4...@gmail.com> writes:
>> I'm not sure what "parcel tags" model is but if you mean these
>> pictures[1] than it works in this case as well as any other (take *a*,
>> *b* nametags, put them on the corresponding balloons that represents
>> list objects).
>>
>> The only names left are *a* and *b* that refer to the corresponding
>> lists. There is no ambiguity there to put *a*, *b* nametags.
>
> But how do you make an a[0][0]/a[1][0] nametag to put on the "1" object?

a[0][0]/a[1][0] are not names. Though if we want to talk about the
corresponding objects then a[0][0]/a[1][0] could be used instead of
names (as a way to identify them). 

>> Lists as any other containers contain references to other objects and
>> therefore "box and arrows" model provides _more details_ here[2,3]
>
> Right, but why not use the *same* model to represent *namespaces*?

If it works for your case; use it. The links [2,3] show that It does
work in this case (if it is correct to call what they show "box and
arrows" model).

> It seems like the "tags" model only exists to make the incorrect claim
> that python doesn't have variables (the boxes are variables).

If you mean this quote from [1]:

  Although we commonly refer to "variables" even in Python (because it's
  common terminology), we really mean "names" or "identifiers". In
  Python, "variables" are nametags for values, not labelled boxes.

then *name* is the term that is defined in the Python language
reference. The word "variable" we can use in day-to-day programming
_unless_ we are discussing issues that are specific to _naming and
binding_.  In that case, we should use the _exact_
terminology. Otherwise there is nothing wrong with using "variables"
casually in Python.

Notice: that [2,3] model is different from "labelled boxes" model. There
are arrows from *a*/*b* _to_ list objects i.e., *a*/*b* are not boxes
that _contain_ list objects within --
_otherwise you have to put the *same* list into two *different* boxes_ --
let's not investigate quantum paradoxes here. The only difference from
"parcel tags" model is that list items do not have explicit names.

>>> If the "parcel tags" model can't show it, then the "parcel tag" model
>>> clearly is not a correct and complete depiction of how Python actually
>>> works.
>>> (If I were drawing a picture rather than ASCII I'd add something to make
>>> it clear that the pairs shown are list objects Like, it's a circle with
>>> the word "list" and two pointer-boxes inside it.)
>>
>> "correct and complete" is impossible in the general case for any
>> model.
>
> Everything you wrote here has the same issue: The "objects" you are
> talking about do not physically exist, but are simply the result of
> calling a method on the object. Therefore they do not *belong* on the
> diagram, and the diagram not showing them does not mean the diagram is
> not complete.

"do not physically exist" does not make sense. Objects are *never*
destroyed explicitly in Python (you can only make them
*unreachable*). You can disable garbage collection completely and it is
still will be Python. Immutable objects can be considered immortal e.g.:

 (1+1) -- question: does the object that represents int(2) exist before
the expression is evaluated?

The correct answer: it does not matter: int(2) can be created on the
fly, a cached int(2) can be reused by a specific implementation --
Python doesn't care.

I don't see why the model that can't describe range(1) in Python 3
pretends to be complete.

[1]
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#python-has-names
[2]
http://www.pythontutor.com/visualize.html#code=a0+%3D+a1+%3D+%5B1,+2%5D%0Ab0+%3D+%5B1,+2%5D%0Ab1+%3D+%5B1,+2%5D%0Aa+%3D+%5Ba0,+a1%5D%0Ab+%3D+%5Bb0,+b1%5D%0Adel+a0,+a1,+b0,+b1%0A=display=opt-frontend.js=false=false=false=3=%5B%5D=6
[3]
http://www.pythontutor.com/visualize.html#code=a0+%3D+a1+%3D+%5B1,+2%5D%0Ab0+%3D+%5B1,+2%5D%0Ab1+%3D+%5B1,+2%5D%0Aa+%3D+%5Ba0,+a1%5D%0Ab+%3D+%5Bb0,+b1%5D%0Adel+a0,+a1,+b0,+b1%0Aa%5B0%5D%5B0%5D+%3D+%22Oops!%22=display=opt-frontend.js=false=false=false=3=%5B%5D=7



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


Re: Terminology: "reference" versus "pointer"

2015-09-13 Thread Akira Li
Chris Angelico <ros...@gmail.com> writes:

> On Mon, Sep 14, 2015 at 11:22 AM, Akira Li <4kir4...@gmail.com> wrote:
>> Steven D'Aprano <st...@pearwood.info> writes:
>>
>>> On Mon, 14 Sep 2015 09:17 am, Akira Li wrote:
>>>
>>>> I don't see why the model that can't describe range(1) in Python 3
>>>> pretends to be complete.
>>>
>>>
>>> Please explain.
>>>
>>> range(1) returns a range instance. What is hard about that?
>>
>> Look at the last example:
>> http://thread.gmane.org/gmane.comp.python.general/782626/focus=782704
>
> Still not sure what the problem is. As per Python's object model, the
> lists contain references to range objects. a contains two references
> to the same range object, b contains references to each of two
> distinct range objects. What of it?

For me, there is no problem. "parcel tags" [1], "box and arrows"[2] are
all the same (note: "labelled box"[3] that may contain an object is a
different model).

The talk about being "complete" is caused by the following quote from
Random832 message [4]:

  If the "parcel tags" model can't show it, then the "parcel tag" model
  clearly is not a correct and complete depiction of how Python actually
  works.

The purpose of the examples in [5] is to demonstate that neither models
are "complete" i.e., there is (obviously) behavior of a Python program
that they can't describe.

[1]
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#python-has-names
[2]
http://www.pythontutor.com/visualize.html#code=a0+%3D+a1+%3D+%5B1,+2%5D%0Ab0+%3D+%5B1,+2%5D%0Ab1+%3D+%5B1,+2%5D%0Aa+%3D+%5Ba0,+a1%5D%0Ab+%3D+%5Bb0,+b1%5D%0Adel+a0,+a1,+b0,+b1%0A=display=opt-frontend.js=false=false=false=3=%5B%5D=6
[3]
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables
[4]
http://thread.gmane.org/gmane.comp.python.general/782626/focus=782645
[5]
http://thread.gmane.org/gmane.comp.python.general/782626/focus=782704

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


Re: Terminology: "reference" versus "pointer"

2015-09-13 Thread Akira Li
Steven D'Aprano <st...@pearwood.info> writes:

> On Mon, 14 Sep 2015 09:17 am, Akira Li wrote:
>
>> I don't see why the model that can't describe range(1) in Python 3
>> pretends to be complete.
>
>
> Please explain.
>
> range(1) returns a range instance. What is hard about that?

Look at the last example:
http://thread.gmane.org/gmane.comp.python.general/782626/focus=782704

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


Re: Terminology: "reference" versus "pointer"

2015-09-13 Thread Akira Li
Steven D'Aprano <st...@pearwood.info> writes:

> On Mon, 14 Sep 2015 11:22 am, Akira Li wrote:
>> Look at the last example:
>> http://thread.gmane.org/gmane.comp.python.general/782626/focus=782704
>
>
> I'm afraid that page is broken in my browser. Can you not summarise, or link
> to the specific message? I may be able to use another browser in a day or
> two, but hopefully the discussion will have moved on by then.

https://mail.python.org/pipermail/python-list/2015-September/696631.html

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


Re: Are there any "correct" implementations of tzinfo?

2015-09-12 Thread Akira Li
Random832  writes:

> I was trying to find out how arithmetic on aware datetimes is "supposed
> to" work, and tested with pytz. When I posted asking why it behaves this
> way I was told that pytz doesn't behave correctly according to the way
> the API was designed. The tzlocal module, on the other hand, appears to
> simply defer to pytz on Unix systems.
>
> My question is, _are_ there any correct reference implementations that
> demonstrate the proper behavior in the presence of a timezone that has
> daylight saving time transitions?

The only way to get correct[*] results now is to use *pytz*. PEP 495
might allow to fix some of non-avoidable (at the moment) bugs[1] in
*dateutil* (another library that provides access to the tz database).

Some core Python developers feel that pytz model does not implement the
initial datetime design intent: both naive and timezone-aware datetime
objects use the same model for arihtmetic (though the actual
implementation contains a mix: aware datetime objects are treated as
naive datetime objects in some cases but in others they behave as though
they are utc-based).

pytz model: aware datetime objects behave *as if* they are converted to
UTC during arithmetic operations, comparisons, etc:

  # d = datetime.now(tz)
  (d2 - d1) == (d2.astimezone(utc) - d1.astimezone(utc))
  tz.normalize(d + delta) == (d.astimezone(utc) + delta).astimezone(tz)

tz.normalize() is necessary to get the correct local time (utc time is
correct even without tz.normalize()) in presence of DST transitions (or
other changes in UTC offset for any reason).

Here's how the stdlib implementation behaves at the moment for a
timezone-aware datetime object that represents local time (the only
timezone with a non-fixed utc offset that is available in stdlib):

  # d = datetime.now(utc).astimezone()
  (d2 - d1) == (d2.astimezone(utc) - d1.astimezone(utc))
  (d + delta).astimezone() == (d.astimezone(utc) + delta).astimezone()

If utc offset is fixed then both naive and aware models are the same.

[*] according to the tz database
[1] https://github.com/dateutil/dateutil/issues/112

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


Re: Terminology: "reference" versus "pointer"

2015-09-12 Thread Akira Li
Rustom Mody  writes:

> On Saturday, September 12, 2015 at 8:11:49 PM UTC+5:30, Laura Creighton wrote:
>> In a message of Sat, 12 Sep 2015 05:46:35 -0700, Rustom Mody writes:
>> >How about lay-English ontology in which "point to" and "refer to" are fairly
>> >synonymous?
>> 
>> This I have found is important in teaching, which is why I favour 'bind'
>> and 'binding' -- rather than pointer, pointer, refer to, referring.
>
> Well we can play humpty dumpty and make any word mean whatever we like.
> However if you are a teacher you will recognize a need for pictures.
> And (as far as I can tell) "Random832" finds a need for the box-n-arrow
> diagrams of classic data-structure books

Speaking of pictures and names in Python
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables


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


Re: Terminology: "reference" versus "pointer"

2015-09-12 Thread Akira Li
Rustom Mody <rustompm...@gmail.com> writes:

> On Saturday, September 12, 2015 at 11:26:18 PM UTC+5:30, Akira Li wrote:
>> Rustom Mody  writes:
>> 
>> > On Saturday, September 12, 2015 at 8:11:49 PM UTC+5:30, Laura Creighton 
>> > wrote:
>> >> In a message of Sat, 12 Sep 2015 05:46:35 -0700, Rustom Mody writes:
>> >> >How about lay-English ontology in which "point to" and "refer to" are 
>> >> >fairly
>> >> >synonymous?
>> >> 
>> >> This I have found is important in teaching, which is why I favour 'bind'
>> >> and 'binding' -- rather than pointer, pointer, refer to, referring.
>> >
>> > Well we can play humpty dumpty and make any word mean whatever we like.
>> > However if you are a teacher you will recognize a need for pictures.
>> > And (as far as I can tell) "Random832" finds a need for the box-n-arrow
>> > diagrams of classic data-structure books
>> 
>> Speaking of pictures and names in Python
>> http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables
>
> Yeah cute
> [I think I will even use these in my classes]
> However they dont address the issue that I think random832 is
> referring to.

The pictures despite their simplicity reflect the actual model that
Python language uses i.e., any deviations are an implementation artifact
and may be ignored.

> viz. I have two variables (or names!) say a and b which look the same
>>>> a
> [[1,2],[1,2]]
>>>> b
> [[1,2],[1,2]]
> And yet doing
>>>> a[0][0] = "Oops!"
> gives a data structure one "Oops!"
> whereas doing it to b mysteriously gives 2

Sorry, I haven't followed the whole thread. Could your provide a
complete code example? Mention what you expect to happen and what
happens instead in your case.

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


Re: Context-aware return

2015-09-10 Thread Akira Li
Grant Edwards  writes:

> On 2015-09-10, Steven D'Aprano  wrote:
>
>> I have a function which is intended for use at the interactive interpreter,
>> but may sometimes be used non-interactively. I wish to change it's output
>> depending on the context of how it is being called.
>
> [...]
>
> Sounds like an excellent way to waste somebody's afternoon when they
> start to troubleshoot code that's using your function.  Over and over
> and over we tell newbies who have questions about what something
> returns or how it works
>
> "Start up an interactive session, and try it!".
>
> If word gets out about functions like yours, we sort of end up looking
> like twits.  
>
>> If I did this thing, would people follow me down the street booing
>> and jeering and throwing things at me?
>
> Only the people who use your function. :)

There are cases when it might be justified to alter the behavior e.g.,
*colorama* allows to strip ANSI codes (e.g., disable colored output) if
stdout is not a tty or *win-unicode-console* make sys.stdout to use
WriteConsoleW() to write Unicode to Windows console (interactive case).

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


Re: passing double quotes in subprocess

2015-09-08 Thread Akira Li
loial  writes:

> I need to execute an external shell script via subprocess on Linux.
>
> One of the parameters needs to be passed inside double quotes 
>
> But the double quotes do not appear to be passed to the script
>
> I am using :
>
> myscript = '/home/john/myscript'
> commandline = myscript + ' ' + '\"Hello\"'
>
> process = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, 
> stderr=subprocess.PIPE)
> output,err = process.communicate()
>
>
> if I make the call from another shell script and escape the double
> quotes it works fine, but not when I use python and subprocess.
>
> I have googled this but cannot find a solution...is there one?

You don't need shell=True here:

  #!/usr/bin/env python3
  from subprocess import Popen, PIPE

  cmd = ['/home/john/myscript', 'Hello'] # if myscript don't need quotes
  # cmd = ['/home/john/myscript', '"Hello"'] # if myscript does need quotes
  with Popen(cmd, stdout=PIPE, stderr=PIPE) as process:
 output, errors = process.communicate()

In general, to preserve backslashes, use raw-string literals:

  >>> print('\"')
  "
  >>> print(r'\"')
  \"
  >>> print('\\"')
  \"


  >>> '\"' == '"'
  True 
  >>> r'\"' == '\\"'
  True

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


Re: Python handles globals badly.

2015-09-08 Thread Akira Li
Vladimir Ignatov  writes:

>>> I had some experience programming in Lua and I'd say - that language
>>> is bad example to follow.
>>> Indexes start with 1  (I am not kidding)
>>
>> What is so bad about that?
>
> It's different from the rest 99.9% of languages for no particular reason.
>
> ( => perfect example of "design smell" => not a good example to follow)
>

It is not just a matter of tradition. Even if you were to invent the
very first programming language; there are reasons to prefer the
zero-based indexing. See "Why numbering should start at zero"
https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html




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


[issue22798] time.mktime doesn't update time.tzname

2015-08-31 Thread Akira Li

Akira Li added the comment:

The C code produces correct values according to the tz database.

If TZ=Europe/Moscow then
tzname={"MSK", "MSD"} at 2010-07-01 and
tzname={"MSK", "MSK"} at 2015-07-01. Notice the difference!

The code calls C mktime() with corresponding time tuples
and checks that *tzname* is equal to the expected values. That's all.

If C code is incomprehensible; here's its Python analog:

  >>> import os
  >>> os.environ['TZ'] = 'Europe/Moscow'
  >>> import time
  >>> time.tzset()
  >>> time.mktime((2010,7,1,0,0,0,-1,-1,-1))
  1277928000.0
  >>> time.tzname #XXX expected ('MSK', 'MSD')
  ('MSK', 'MSK')
  >>> time.mktime((2015,7,1,0,0,0,-1,-1,-1))
  1435698000.0
  >>> time.tzname
  ('MSK', 'MSK')


C tzname changes on my machine after the corresponding C mktime() calls 
but Python time.tzname does not change after the time.mktime() calls.

--

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22798>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22798] time.mktime doesn't update time.tzname

2015-08-31 Thread Akira Li

Akira Li added the comment:

> C mktime itself should not change timezone globals, but it may indirectly if 
> it calls tzset() and TZ changed between calls.

You should have run the attached test_mktime_changes_tzname.c which 
demonstrates that (at least on some systems) C mktime *does* change tzname 
global even if TZ envvar is constant in the test.

Nowhere in my bug report I had mentioned different TZ values. I did mentioned 
*past* *future* dates -- the same timezone may have different utc offset, 
timezone abbreviations at different times. These timezone globals can change 
even if TZ is constant.

--
status: pending -> open

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22798>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Sandboxing Python

2015-08-23 Thread Akira Li
Mark Lawrence breamore...@yahoo.co.uk writes:

 I was always led to believe that the subject was a difficult thing to
 do, but here
 https://www.reddit.com/r/learnpython/comments/3huz4x/how_to_do_math_inside_raw_input/
 is a safe solution in only 23 characters, or are there any discernable
 flaws in it?

Related: 
http://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string

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


Re: asyncio, coroutines, etc. and simultaneous execution

2015-08-23 Thread Akira Li
Charles Hixson charleshi...@earthlink.net writes:

 If I understand correctly asyncio, coroutines, etc. (and, of course,
 Threads) are not simultaneously executed, and that if one wants that
 one must still use multiprocessing.  But I'm not sure.  The note is
 still there at the start of threading, so I'm pretty sure about that
 one.

Due to GIL (used by CPython, Pypy, not used by Jython, IronPython,
pypy-stm) only one thread executes Python bytecode at a time. GIL can be
released on I/O or in a C extension such as numpy, lxml, regex. It is
true for any Python module or concept you use.

Unrelated: use concurrent and parallel execution instead of
simultaneously executed.

Parallelism might make a program faster (it implies that hardware
supports it).

Concurrency is a way to structure the code. The same concurrent program
can run in parallel and without parallelism.

Recommended: Concurrency is not Parallelism
(it's better!) talk by Rob Pike's talk
http://stackoverflow.com/questions/1050222/concurrency-vs-parallelism-what-is-the-difference


 The requirement that coroutines always be awaited seems to confirm
 this, but doesn't really say so explicitly. And the concurrent.futures
 can clearly be either, depending on your choices, but the chart in
 18.5.3.1.3 Example: Chain coroutines is of a kind that I am more
 familiar with in the context of multiprocessing. (E.g., the only gap
 in the chart, which extends across all headings is when a result is
 being waited for during a sleep.)  For threaded execution I would
 expect there to be a gap whenever processing is shifted from one
 column to another.

 If someone has authority to edit the documentation a comment like:

Anybody can suggest a patch
https://docs.python.org/devguide/docquality.html

 If you want your application to make better use of the computational
 resources of multi-core machines, you are advised to use
 multiprocessing
 https://docs.python.org/3.5/library/multiprocessing.html#module-multiprocessing
 or concurrent.futures.ProcessPoolExecutor
 https://docs.python.org/3.5/library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor.
  However,
 threading is still an appropriate model if you want to run multiple
 I/O-bound tasks simultaneously.

 (to quote from the threading documentation) would be helpful at or
 very near the top of each of the appropriate modules.  It would also
 be useful if the modules that were definitely intended to result in
 simultaneous execution, when feasible, were so marked quite near the
 top.

It is not necessary that multiprocessing will make your code faster on a
multi-core machine. It may make it slower depending on the task.

Performance optimization is a vast topic. Short remarks such as you've
suggested are likely misleading.

 OTOH, I may be mistaken about coroutines.  I haven't been able to tell.

Many cooperative multitasking implementations use a *single*
thread. There is a way to offload blocking code e.g.,
loop.run_in_executor() in asyncio.


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


[issue24881] _pyio checks that `os.name == 'win32'` instead of 'nt'

2015-08-21 Thread Akira Li

Akira Li added the comment:

To make _pyio correspond to the C version I've added 

  sys.platform in {'win32', 'cygwin'} 

condition. See the attached pyio_setmode.diff 

It is not clear why the absence of _setmode(fd, os.O_BINARY) is not detected by 
tests.

(a) a corresponding test should be added
(b) OR _setmode() call should be removed from both Python and C io versions

--
keywords: +patch
nosy: +akira
Added file: http://bugs.python.org/file40222/pyio_setmode.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24881
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24842] Mention SimpleNamespace in namedtuple docs

2015-08-16 Thread Akira Li

Akira Li added the comment:

People do have problems that SimpleNamespace can solve:

- Why Python does not support record type i.e. mutable namedtuple [1]
- Does Python have anonymous classes? [2]
- How to create inline objects with properties in Python? [3]
- python create object and add attributes to it [4]

[1] 
http://stackoverflow.com/questions/5227839/why-python-does-not-support-record-type-i-e-mutable-namedtuple
[2] 
http://stackoverflow.com/questions/1123000/does-python-have-anonymous-classes
[3] 
http://stackoverflow.com/questions/1528932/how-to-create-inline-objects-with-properties-in-python
[4] 
http://stackoverflow.com/questions/2827623/python-create-object-and-add-attributes-to-it

--
nosy: +akira

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24842
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Who uses IDLE -- please answer if you ever do, know, or teach

2015-08-07 Thread Akira Li
Terry Reedy tjre...@udel.edu writes:

 There have been discussions, such as today on Idle-sig , about who
 uses Idle and who we should design it for.  If you use Idle in any
 way, or know of or teach classes using Idle, please answer as many of
 the questions below as you are willing, and as are appropriate


Running Python scripts in Windows console that may produce Unicode
output is a worth-mentioning use-case [1] (as you might know) i.e., if
you run:

  T:\ py print_unicode.py

and get the error:

  UnicodeEncodeError: 'charmap' codec can't encode character '...'

then a workaround that works out of the box is to run:

  T:\ py -m idlelib -r print_unicode.py

that can display Unicode (BMP) output in IDLE.

[1] 
http://stackoverflow.com/questions/28521944/python3-print-unicode-to-windows-xp-console-encode-cp437

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


Re: Convert between timezones

2015-07-31 Thread Akira Li
Thomas 'PointedEars' Lahn pointede...@web.de writes:

 [X-Post  F'up2 comp.unix.shell]

 Chris Angelico wrote:

 On Fri, Jul 31, 2015 at 6:15 PM, Cameron Simpson c...@zip.com.au wrote:
 Actually, bash has no timezone support but the date command _does_, and
 probably neither better nor worse than Python. All one has to do is set
 the TZ environment variable, eg (untested):

  _year_gmt=$( TZ=GMT date +%Y )
 
 That's assuming that it's converting against the current system
 timezone. I don't know how you'd use `date` to convert between two
 arbitrary timezones. […]

 With POSIX date(1), ISTM all you could do is set the system time and for an 
 additional invocation the TZ variable accordingly for output.

 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html

 With GNU date(1):

 $ (tz_source=Asia/Dubai; time_source=$(LC_TIME=C TZ=$tz_source date -d 
 today 00:00 UTC+4 -Im); tz_target=America/Chicago; echo When it was 
 $time_source in $tz_source, it was $(LC_TIME=C TZ=$tz_target date -d 
 $time_source) in $tz_target.)
 When it was 2015-07-31T00:00+0400 in Asia/Dubai, it was Thu Jul 30 15:00:00 
 CDT 2015 in America/Chicago.

 $ date --version
 date (GNU coreutils) 8.23
 […]


Here's a corresponding Python code. I haven't seen the beginning of the
discussion. I apologize if it has been already posted:

  #!/usr/bin/env python
  from datetime import datetime
  import pytz # $ pip install pytz
  
  source_tz, target_tz = map(pytz.timezone, ['Asia/Dubai', 'America/Chicago'])
  d = datetime.now(source_tz) # the current time in source_tz timezone
  midnight = source_tz.localize(datetime(d.year, d.month, d.day), is_dst=None)  
  
  fmt = %Y-%m-%dT%H:%M:%S%z
  print(When it was {:{fmt}} in {}, it was {:{fmt}} in {}.format(
  midnight, source_tz.zone, target_tz.normalize(midnight),
  target_tz.zone, fmt=fmt))

Output:

  When it was 2015-08-01T00:00:00+0400 in Asia/Dubai, it was
  2015-07-31T15:00:00-0500 in America/Chicago





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


[issue19007] precise time.time() under Windows 8: use GetSystemTimePreciseAsFileTime

2015-07-20 Thread Akira Li

Changes by Akira Li 4kir4...@gmail.com:


--
nosy: +akira

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19007
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Matplotlib X-axis timezone trouble

2015-07-03 Thread Akira Li
Peter Pearson pkpearson@nowhere.invalid writes:

 The following code produces a plot with a line running from (9:30, 0) to
 (10:30, 1), not from (8:30, 0) to (9:30, 1) as I desire.

 If I use timezone None instead of pacific, the plot is as desired, but
 of course that doesn't solve the general problem of which this is a
 much-reduced example.

 If I use timezone US/Central, I get the same (bad) plot.

 import matplotlib.pyplot as plt
 import datetime
 import pytz
 pacific = pytz.timezone(US/Pacific)
 fig = plt.figure()
 plt.plot([datetime.datetime(2014, 10, 7, 8, 30, tzinfo=pacific),
   datetime.datetime(2014, 10, 7, 9, 30, tzinfo=pacific)],
  [0,1], marker=o, color=green)
 fig.autofmt_xdate()
 plt.show()

 Does anybody know why this shift is occurring?  Is Matplotlib
 confused about what timezone to use in labeling the axis?  How
 would I tell it what timezone to use (preferably explicitly in
 the code, not in matplotlibrc)?


Your pytz usage is incorrect.

Don't pass a pytz tzinfo object to the datetime construtor directly, use
`.localize()` method instead. Read the note at the very beginning of
pytz docs http://pytz.sourceforge.net/


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


[issue1191964] add non-blocking read and write methods to subprocess.Popen

2015-03-25 Thread Akira Li

Akira Li added the comment:

 I'm going to be honest; seeing None being returned from a pipe read feels 
 *really* broken to me. When I get None returned from an IO read operation, my 
 first instinct is there can't be anything else coming, why else would it 
 return None?

It is how it is done in similar cases (returning `None` to indicate 
would block). Do you know a better method that would allow to 
distinguish between EOF (no future data, ever) and would block
(future data possible) without calling process.poll()?

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1191964
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10482] subprocess and deadlock avoidance

2015-03-22 Thread Akira Li

Changes by Akira Li 4kir4...@gmail.com:


--
nosy: +akira

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10482
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23574] datetime: support leap seconds

2015-03-12 Thread Akira Li

Akira Li added the comment:

POSIX timestamp doesn't count (literally) past/future leap seconds.
It allows to find out that the timestamp 2**31-1 corresponds to
2038-01-19T03:14:07Z (UTC) regardless of how many leap seconds will
occur before 2038:

   from datetime import datetime, timedelta
   str(datetime(1970,1,1) + timedelta(seconds=2**31-1))
  '2038-01-19 03:14:07'

If you use right timezone then mktime() may count leap seconds:

  $ TZ=right/UTC ./python
   import time
   time.mktime((2012, 6, 30, 23, 59, 59, -1, -1, -1))
  1341100823.0
   time.mktime((2012, 6, 30, 23, 59, 60, -1, -1, -1))
  1341100824.0
   time.mktime((2012, 7, 1, 0, 0, 0, -1, -1, -1))
  1341100825.0

It is a different time scale. There are no leap seconds in TAI:

   str(datetime(1970,1,1, 0,0, 10) + timedelta(seconds=1341100825))
  '2012-07-01 00:00:35'

i.e., 2012-07-01 00:00:35 TAI that corresponds to 2012-07-01 00:00:00
UTC. Each positive leap second increases the difference TAI-UTC (on
2015-07-01UTC it will be 36 [1]).

TAI-UTC in the future (more than 6 months) is unknown but it is less
than ~200 seconds until 2100 [2].

It might be convenient to think about datetime as a broken-down
timestamp and therefore

  (datetime(2012,6,30,23,59,60) - epoch) == 
  (datetime(2012,7, 1, 0, 0, 0) - epoch)

The code [3] that silently truncates 60 to 59 when datetime
constructor is called implicitly should retire.

Use case: parse timestamps that might include a leap second [4]

[1] https://hpiers.obspm.fr/iers/bul/bulc/bulletinc.dat
[2] http://www.ucolick.org/~sla/leapsecs/year2100.html
[3] https://bugs.python.org/msg155689
[4] http://stackoverflow.com/q/21027639

--
nosy: +akira

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23574
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21619] Cleaning up a subprocess with a broken pipe

2015-03-02 Thread Akira Li

Akira Li added the comment:

On Windows behavior
http://stackoverflow.com/questions/23688492/oserror-errno-22-invalid-argument-in-subprocess

--
nosy: +akira

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21619
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23494] adding timedelta to datetime object is not timezone aware

2015-03-02 Thread Akira Li

Akira Li added the comment:

pytz explicitly documents this case (crossing DST boundary). There is 
tz.normalize() method.

 the tzinfo object is responsible for handling daylight savings time.  This 
 looks like a bug in pytz.

Are any of tzinfo methods even called during `before + timedelta(days=2)`?

Also a DST transition is not the only reason the utc offset or tzname may 
change.

--
nosy: +akira

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23494
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22524] PEP 471 implementation: os.scandir() directory scanning function

2015-02-13 Thread Akira Li

Akira Li added the comment:

As I've mentioned in http://bugs.python.org/issue22524#msg231703

  os.walk size 7925376343, scandir.walk size 5534939617 -- NOT EQUAL!

os.walk and scandir.walk do a different work here.

I don't see that it is acknowledged so I assume the benchmark is not fixed.

If the fixed (same work) benchmark is used then there is no performance 
difference on Linux. See the attached file 
http://bugs.python.org/file37292/get_tree_size_listdir.diff 

Note: the performance improvements on Windows along should be enough to accept 
os.scandir() even if Linux version won't be improved (if the behavior is 
documented).

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22524
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22799] wrong time.timezone

2015-01-28 Thread Akira Li

Akira Li added the comment:

 Isn't this a duplicate of #13466?

In what way is it a duplicate?

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22799
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22799] wrong time.timezone

2015-01-28 Thread Akira Li

Akira Li added the comment:

I agree that time.timezone, time.altzone is not enough in the general
case. Because UTC offset may be different at different dates for 
reasons unrelated to DST transitions therefore any solution that 
doesn't take into account a given date/time into account will fail.

*Nothing* works in the general case. Nothing.

But it doesn't mean that the current behaviour of time.timezone
can't be improved for this particular use-case:

  lt = time.localtime() # in a short-lived script
  assertEqual(lt.tm_gmtoff, -[time.timezone, time.altzone][lt.tm_isdst])

The test checks values for the current time (time.localtime()).
It should work in *most* cases if time.timezone/altzone have correct 
values at import time.

Perhaps synchronizing time.timezone with C timezone variable as I've
mentioned before http://bugs.python.org/issue22798 may fix this issue
too.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22799
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23251] mention in time.sleep() docs that it does not block other Python threads

2015-01-25 Thread Akira Li

Akira Li added the comment:

I've removed mentioning of GIL and uploaded a new patch.

--
Added file: 
http://bugs.python.org/file37850/docs-time.sleep-other-threads-are-not-blocked-2.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23251
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23315] tempfile.mkdtemp fails with non-ascii paths on Python 2

2015-01-25 Thread Akira Li

New submission from Akira Li:

Python 2.7.9 (default, Jan 25 2015, 13:41:30) 
  [GCC 4.9.2] on linux2
  Type help, copyright, credits or license for more information.
   import os, sys, tempfile
   d = u'\u20ac'.encode(sys.getfilesystemencoding()) # non-ascii
   if not os.path.isdir(d): os.makedirs(d)
  ... 
   os.environ['TEMP'] = d
   tempfile.mkdtemp(prefix=u'')
  Traceback (most recent call last):
File stdin, line 1, in module
File .../python2.7/tempfile.py, line 331, in mkdtemp
  file = _os.path.join(dir, prefix + name + suffix)
File .../python2.7/posixpath.py, line 80, in join
  path += '/' + b
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: 
ordinal not in range(128)

Related: https://bugs.python.org/issue1681974

--
components: Unicode
messages: 234662
nosy: akira, ezio.melotti, haypo
priority: normal
severity: normal
status: open
title: tempfile.mkdtemp fails with non-ascii paths on Python 2
type: behavior
versions: Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23315
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23320] devguide should mention rules about paragraph reflow in the documentation

2015-01-25 Thread Akira Li

New submission from Akira Li:

It is suggested in https://bugs.python.org/issue23251
that only a core Python developer may reflow paragraphs
while submitting patches for the Python documentation.

It should be codified in devguide: I haven't found the
word *reflow* in it.

--
components: Devguide
messages: 234692
nosy: akira, ezio.melotti
priority: normal
severity: normal
status: open
title: devguide should mention rules about paragraph reflow in the 
documentation
type: behavior

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23320
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23251] mention in time.sleep() docs that it does not block other Python threads

2015-01-18 Thread Akira Li

Akira Li added the comment:

 Only if the behaviour was unintuitive (i.e. if it *didn't* release the
 GIL) would it make sense to document it.

There is no intuitive interface, not even the nipple. It's all learned. [1]

 Yes, on consideration I agree with Antoine.  That last sentence should
 be deleted.  Otherwise we'd need to mention that the gil was released
 every place that the gil was released, which would be very redundant.

Whether or not other places mention it is unrelated to the current
issue.

Though the documentation should mention it more. Many programmers are
convinced that Python threads can't be executed in parallel.

 The general rule is that anything that blocks in python releases the
 GIL, therefore as Antoine says the only time we need to document GIL
 behavior is when that *doesn't* happen.

The reader of Python documentation is intelegent but not all-knowing.

Blocking is the first and only job for time.sleep() function.
GIL blocks Python code.
You can't understand what time.sleep does without knowing what happens
to GIL.

Ask yourself who and why reads the time.sleep() documentation (novice
and/or exprerienced Python user). Put yourself into the mind of the
reader.


[1] http://www.greenend.org.uk/rjk/misc/nipple.html

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23251
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23251] mention in time.sleep() docs that it does not block other Python threads

2015-01-17 Thread Akira Li

Akira Li added the comment:

 I think it's superfluous to mention the GIL here, since it has no impact on 
 the function.

If GIL is not released then all Python code in other threads is 
effectively blocked.
It is worth mentioning explicitly that it is guaranteed to be released
during the sleep.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23251
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23251] mention in time.sleep() docs that it does not block other Python threads

2015-01-16 Thread Akira Li

New submission from Akira Li:

There is the corresponding StackOverflow question with 60K view 
time.sleep — sleeps thread or process? [1]

The documentation patch is attached.

[1] http://stackoverflow.com/questions/92928/time-sleep-sleeps-thread-or-process

--
assignee: docs@python
components: Documentation
files: docs-time.sleep-other-threads-are-not-blocked.diff
keywords: patch
messages: 234135
nosy: akira, docs@python
priority: normal
severity: normal
status: open
title: mention in time.sleep() docs that it does not block other Python threads
type: enhancement
versions: Python 3.5
Added file: 
http://bugs.python.org/file37730/docs-time.sleep-other-threads-are-not-blocked.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23251
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23251] mention in time.sleep() docs that it does not block other Python threads

2015-01-16 Thread Akira Li

Akira Li added the comment:

I do not understand. Have you tried to look at the patch in Rietveld?

The new content is highlighted in a darker green. It is clearly
visible. I've tested on Chromium, Firefox, Safari.

If I won't reflow then the first line will be longer than the
recommended 80 in devguide:

 The maximum line length is 80 characters for normal text, but
 tables, deeply indented code samples and long links may extend
 beyond that.

I've set *fill-column* to 80 in emacs. Do you suggest other settings?

Anyway, it doesn't affect how the final text is shown in a web
browser.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23251
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22932] email.utils.formatdate uses unreliable time.timezone constant

2015-01-11 Thread Akira Li

Akira Li added the comment:

@mitya57: Please, combine the code changes, tests, docs into a single 
rietveld-compatible patch (hg diff); read devguide and 
http://bugs.python.org/issue13963 

Make sure review link appears on the right near the patch. Example: 
http://bugs.python.org/issue22798

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22932
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22932] email.utils.formatdate uses unreliable time.timezone constant

2014-12-24 Thread Akira Li

Changes by Akira Li 4kir4...@gmail.com:


--
nosy: +akira

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22932
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22673] document the special features (eg: fdclose=False) of the standard streams

2014-12-24 Thread Akira Li

Akira Li added the comment:

Two minor details:

1. It is possible that `fileno(stdout) != 1` even in C [1]. 
   I don't know what happens if the code from the answer is
   run on Windows.

   In principle, it may break eryksun's workaround. I don't 
   know how likely it is in practice.

2. you can redirect at the file descriptor level in Python [2] 
   using os.dup2(). I don't know whether the code in the 
   answer works on Windows.

[1] 
http://stackoverflow.com/questions/25516375/is-it-possible-that-filenostdout-1-on-a-posix-system
[2] 
http://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python/22434262#22434262

--
nosy: +akira

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22673
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Simple background sound effect playback

2014-12-21 Thread Akira Li
Jacob Kruger ja...@blindza.co.za writes:

 Would prefer to use something free, that could work somewhat
 cross-platform, but, my primary target is for windows OS, and would
 primarily just want to be able to easily trigger playback of either
 .wav or .mp3 background sound effects, but, yes, would also be nice to
 be able to control them a little bit in terms of volume, possibly
 stereo panning as well as playback rate/frequency/pitch?

 I have used something called sound_lib, as well as another one
 relating to a sort of windows directSound effect, but, both of them
 had issues when working with either py2exe or cx_freeze when it came
 to compiling into executable, and main thing is would like to keep it
 simple...smile

 Suggestions?


You could try GStreamer: it is free, cross-platform, it allows you to
play media files in the background, to control volume, stereo panning
(e.g., GstAudioPanorama), to change the playback rate while preserving
pitch, it can use DirectShow on Windows, etc -- see
http://gstreamer.freedesktop.org/features/

The downside is that it may be complex to install and use e.g., it
probably works with py2exe but it won't be simple to configure.

If you know other library that provides similar feature list while being
less complex; do tell.


--
Akira.

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


Re: Creating interactive command-line Python app?

2014-12-13 Thread Akira Li
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info writes:

 rfreundlic...@colonial.net wrote:

 um, what if I want to USE a command line for python WITHOUT downloading or
 installing it

 Who are you talking to? What is the context?

 Like all software, you can't use Python apps without all their dependencies
 being installed. If you use the Linux operating system, it will have Python
 already installed. Otherwise, you will have to install it.

 If you can't install it, or don't want to, you can't use Python.

cx_Freeze, PyInstaller, py2exe, etc allow to create a standalone
distribution i.e., you could ship your executable with a bundled Python
interpreter.


--
Akira

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


[issue23017] string.printable.isprintable() returns False

2014-12-13 Thread Akira Li

Akira Li added the comment:

C standard defines locale-specific *printing characters* that are [ -~]
in C locale for implementations that use 7-bit US ASCII character set
i.e., SP (space, 0x20) is a printing character in C (isprint() returns
nonzero).

There is isgraph() function that returns zero for the space but
otherwise is equivalent to isprint().

POSIX definition is aligned with the ISO C standard.

I don't know what RFC 5822 has to do with this issue but the rfc
contradicts itself e.g., in one place it has: printable US-ASCII
characters except SP that imlies that SP *is* printable but in other
places it considers isprint==isgraph. The authors probably meant
characters for which isgraph() is nonzero when they use printable
US-ASCII (that is incorrect according to C standard).

Tests from issue9770 show the relation between C character classes and
string constants [1]:

  set(string.printable) == set(C['graph']) + set(C['space'])

where C['space'] is '\t\n\v\f\r ' (the standard C whitespace).

It is a documented behavior [2]:

  This is a combination of digits, ascii_letters, punctuation,
  and whitespace

where *whitespace* is C['space'].

In Python 2, *printable* is locale-dependent and it coincides with the
corresponding Python 3 definition in C locale with ASCII charset.

Unlike other string constants, *printable* differs from C['print'] on
both Python 2 and 3 because it includes whitespace characters other than
space.

str.isprintable [3] obeys C['print'] (in ASCII range) and considers SP
to be printable.

---

It might be too late to change string.printable to correspond to C
isprint() (for ASCII characters).

I've uploaded a documentation patch that mentions that string.printable
and str.isprintable differ.

[1] http://bugs.python.org/review/9770/diff/12212/Lib/test/test_curses_ascii.py
[2] https://hg.python.org/cpython/file/3.4/Doc/library/string.rst#l62
[3] https://docs.python.org/3.4/library/stdtypes.html#str.isprintable

--
nosy: +akira
Added file: http://bugs.python.org/file37441/docs-string.printable.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23017
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: How to make subprocess run for 60 sec?

2014-12-12 Thread Akira Li
Dan Stromberg drsali...@gmail.com writes:

 On Fri, Dec 12, 2014 at 12:48 AM, Robert Clove cloverob...@gmail.com wrote:
 Hi All,

 I have the following python script that runs.
 I want is to run the subprocess to run for 60 sec and then send the SIGINT
 signal to subprocess and write the output in file.

 #!/usr/bin/python
 import os
 import subprocess
 PIPE = subprocess.PIPE
 import signal
 import time

 def handler(signum, frame):
 pass

 signal.signal(signal.SIGALRM, handler)
 signal.alarm(60)
 command = strace -c ./server
 os.chdir(/root/Desktop/)
 p = subprocess.Popen(command, stdout=PIPE, stderr=PIPE)
 time.sleep(60)
 p.send_signal(signal.SIGINT)
 signal.alarm(0)
 print p.communicate()[1]

 Perhaps try something like:

 #!/usr/bin/python

 #import os
 import subprocess
 PIPE = subprocess.PIPE
 import signal
 import time

 #def handler(signum, frame):
 #pass

 with open('output.txt', 'w') as out_file:
 #signal.signal(signal.SIGALRM, handler)
 #signal.alarm(60)
 #command = strace -c ./server
 command = ./test-script
 #os.chdir(/root/Desktop/)
 p = subprocess.Popen(command, stdout=out_file, stderr=out_file, 
 shell=True)
 time.sleep(5)
 p.send_signal(signal.SIGINT)
 #signal.alarm(0)
 #print p.communicate()[1]

 with open('output.txt', 'r') as in_file:
 output = in_file.read()

 print(output)

You should probably call p.wait() after p.send_signal(), to wait until
the child process exits (possibly properly flushing its stdout buffer). 
It might make the output.txt content less garbled.


 BTW, killing an active strace may leave strace's subprocess stuck in
 an unkillable state.

 Also note that this will sleep for n seconds whether the subprocess
 takes that long or not.

The answer on StackOverflow [1] shows how to avoid waiting n seconds if the
subprocess finishes sooner.

[1] 
http://stackoverflow.com/questions/27443480/how-to-make-subprocess-run-for-60-sec

 If you just want a program that does this, and it doesn't have to be
 in Python, you might try
 http://stromberg.dnsalias.org/~strombrg/maxtime.html
 It's in C, and is the product of considerable real-world use.  It
 exits almost immediately after its subprocess exits, FWIW.


--
Akira

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


Re: time.monotonic() roll over

2014-12-05 Thread Akira Li
Dave Angel da...@davea.name writes:

...many words about sleep()...
 Since the OS has no way of knowing whether the thing being waited for
 is a thread, another process, a human being, a network operation, or
 the end of the world, the interpretation of sleep needs to be the most
 conservative one.  There are many ways of suspending a process, and
 some of them will also suspend the external event.  Since the OS
 cannot know which case is significant, it has to return control to the
 caller at the soonest of the many possible interpretations.

That is why there is API (e.g., clock_nanosleep()) that allows us to
choose whether we need a relative delay (e.g., kill a subprocess if it
hasn't finished in 10 seconds) or an absolute deadline (e.g., these
lights should be on 9pm-6am local time).

*Both* use cases are valid.


--
Akira

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


Re: Do you like the current design of python.org?

2014-12-04 Thread Akira Li
Peter Otten __pete...@web.de writes:

 Did you ever hit the Socialize button? Are you eager to see the latest 
 tweets when you are reading a PEP? Do you run away screaming from a page 
 where nothing moves without you hitting a button? Do you appreciate the 
 choice between ten or so links to the documentation?

 You can probably guess my opinion -- konqueror just crashed on the PEP index 
 and for some reason I'm more annoyed about the page than about the browser.


 PS: Is there a twitter.com something that I can block to trade black friday 
 and cyber monkey sales for a box with a good old error message?

I see that pythondotorg accepts pull requests and allows to report issues.
https://github.com/python/pythondotorg


--
Akira

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


Re: time.monotonic() roll over

2014-12-04 Thread Akira Li
Ian Kelly ian.g.ke...@gmail.com writes:

 On Thu, Dec 4, 2014 at 11:09 AM, Marko Rauhamaa ma...@pacujo.net wrote:

 Chris Angelico ros...@gmail.com:

  It's not a Python issue. Python can't do anything more than ask the
  system, and if the system's value rolls over several times a year,
  Python can't magically cure that. The information has already been
  lost.

 Sure it could by having an invisible background thread occasionally call
 time.monotonic(). It could even be done on the side without a thread.

 Anyway, the idea of a clock is complicated:

  * the program could be stopped by a STOP signal

  * the program could be suspended from power management

  * the program could be resurrected from a virtual machine snapshot

  * the program could be migrated from a different physical machine

 This seems like a lot of effort to unreliably design around a problem that
 will matter to only a tiny fraction of users.

- people's computers are mostly on batteries (laptops, tablets,
  smartphones) -- suspended from power management use case
- corporations's computations are mostly virtualized -- possible
  ressurected, migrated use case

i.e., the opposite might be true -- non-virtualized PCs connected to AC
are (becoming) minority.


--
Akira

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


[issue22356] mention explicitly that stdlib assumes gmtime(0) epoch is 1970

2014-12-02 Thread Akira Li

Akira Li added the comment:

 Alexander Belopolsky added the comment:

 I've provide the direct quote from *C* standard ...

 I understand that C standard uses the word encoding, but it does so
 for a reason that is completely unrelated to the choice of epoch.
 Encoding is how the bytes in memory should be interpreted as number
 of seconds or some other notion of time.  For, example two's
 complement little-endian 32-bit signed int is an example of valid
 time_t encoding, another example would be IEEE 754 big-endian 64-bit
 double.  Note that these choices are valid for both C and POSIX
 standards.

I agree one *part* of encoding is how time_t is *represented* in
memory but it is not the only part e.g.:

  The mktime function converts the broken-down time, expressed as local
  time, in the structure pointed to by timeptr into a calendar time
  value with the same encoding as that of the values returned by the
  time function.

notice: the same encoding as ... returned by the time function.

time() function can return values with different epoch (implementation
defined). mktime() is specified to use the *same* encoding i.e., the
same epoch, etc.

i.e., [in simple words] we have calendar time (Gregorian date, time) and
we can convert it to a number (e.g., Python integer), we can call that
number seconds and we can represent that number as some (unspecified)
bit-pattern in C.

I consider the whole process of converting time to a bit-pattern in
memory as encoding i.e., 32/64, un/signed int/754 double is just
*part* of it e.g.,

1. specify that 1970-01-01T00:00:00Z is zero (0)
2. specify 0 has time_t type
3. specify how time_t type is represented in memory.

I may be wrong that C standard includes the first item in time
encoding.

 If you google for your phrase time in POSIX encoding, this issue is
 the only hit.  This strongly suggests that your choice of words is not
 the most natural.

I've googled the phrase (no surrounding quotes) and the links talk about
time encoded as POSIX time [1] and some *literally* contain the phrase
*POSIX encoding* [2] because *Python* documentation for calendar.timegm
contains it [3]:

  [timegm] returns the corresponding Unix timestamp value, assuming an
  epoch of 1970, and the POSIX encoding. In fact, time.gmtime() and
  timegm() are each others’ inverse.

In an effort to avoid personal influence, I've repeated the expreriment
using Tor browser and other search engines -- the result is the same.

timegm() documentation might be the reason why I've used the phrase.

I agree POSIX encoding might be unclear. The patch could be replaced
by any phrase that expresses that some functions in stdlib assume that
time.time() returns (+/- fractional part) seconds since the Epoch as
defined by POSIX [4].

[1] http://en.wikipedia.org/wiki/Unix_time#Encoding_time_as_a_number
[2] 
http://ruslanspivak.com/2011/07/20/how-to-convert-python-utc-datetime-object-to-unix-timestamp/
[3] https://docs.python.org/3/library/calendar.html#calendar.timegm
[4]
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_15

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22356
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22356] mention explicitly that stdlib assumes gmtime(0) epoch is 1970

2014-12-01 Thread Akira Li

Akira Li added the comment:

 Alexander Belopolsky added the comment:

 1. It is not the job of the time module documentation to warn about
 many functions in the stdlib.  What are these functions, BTW?

The e-mail linked in the first message of this issue msg226539
enumerates some of the functions:

  https://mail.python.org/pipermail/python-ideas/2014-September/029228.html

 2. What is calendar time in POSIX encoding? This sounds like what 
 time.asctime() returns.

It is the language used by C standard for time() function:

  The time function determines the current calendar time. The encoding
  of the value is unspecified.

 I think an improvement would be to spell Epoch with a capital E and
 define it as The time zero hours, zero minutes, zero seconds, on
 January 1, 1970 Coordinated Universal Time (UTC).  See
 http://pubs.opengroup.org/onlinepubs/9699919799.


The word *epoch* (lowercase) is used by C standard.

It is not enough to say that time module uses POSIX epoch (Epoch) e.g.,
a machine may use right zoneinfo (the same epoch year 1970) but the
timestamp for the same UTC time are different by number of leap seconds
(10+25 since 2012).

POSIX encoding implies that the formula works:

  utc_time = datetime(1970, 1,  1) + timedelta(seconds=posix_timestamp)

if time.time() doesn't return posix_timestamp than many functions in
the stdlib will break.

It is possible to inspect all stdlib functions that use time module and
determine for some of them whether they will break if gmtime(0) is not
1970 or right zoneinfo is used or any non-POSIX time encoding is
used. But it is hard to maintain such a list because any future code
change may affect the behavior. I prefer a vague statement (many
functions) over a possible lie (the documentation shouldn't make
promises that the implementation can't keep).

POSIX language is (intentionally) vague and avoids SI seconds vs. UT1
(mean solar) seconds distinction. I don't consider systems where
seconds doesn't mean SI seconds used by UTC time scale.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22356
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22356] mention explicitly that stdlib assumes gmtime(0) epoch is 1970

2014-12-01 Thread Akira Li

Akira Li added the comment:

 Alexander Belopolsky added the comment:

 In the context of Python library documentation, the word encoding
 strongly suggests that you are dealing with string/bytes.  The
 situation may be different in C. If you want to refer to something
 that is defined by the POSIX standard you should use the words that
 can actually be found in that standard.

 When I search for encoding at 
 http://pubs.opengroup.org/onlinepubs/9699919799/, I get

 crypt - string encoding function (CRYPT) 
 encrypt - encoding function (CRYPT) 
 setkey - set encoding key (CRYPT)

 and nothing related to time.


I've provide the direct quote from *C* standard in my previous message 
msg231957:

   2. What is calendar time in POSIX encoding? This sounds like what 
time.asctime() returns.

  It is the language used by C standard for time() function:

The time function determines the current calendar time. The encoding
of the value is unspecified.
^ - from the C standard

notice the word *encoding* in the quote.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue22356
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Asyncio problem, looking for advice.

2014-11-28 Thread Akira Li
Benjamin Risher brisher...@gmail.com writes:

 Hello all,

 I'm working on a project to learn asyncio and network programming.  What I'm 
 trying to do is forward a connection from myself to another machine.  Kind of 
 like an asynchronous python implementation of fpipe.

 In a nutshell:

 1 -- start a server listening on localhost
 2 -- connect to server
 3 -- server connects to a listening service (ssh for instance)
 4 -- server handles both connections to pass traffic back and forth through 
 each

 What I have now *kind of* works.  It sends data back and forth, but when I 
 ssh to localhost -p 12345, i never get the password prompt.  It looks like 
 one packet hangs out and doesn't get sent from what I saw in tcpdump.

 Any help would be greatly appreciated.

Do you want to emulate `ssh -L 12345:localhost:22 host`?

 Here's a link to the same code as below, just with syntax highlighting etc...
 http://pastebin.com/iLE4GZH3

There are several issue e.g., unnecessary async(), deprecated Task()
calls but the main issue is that _handle_client() doesn't read
concurrently from the server while client writes to it.

You could use asyncio.wait() to run several tasks in parallel
[1]. Here's a forward-port.py example [2]:

  #!/usr/bin/env python3
  Forward a local tcp port to host:port.
  
  Usage: %(prog)s local_port:host:port
  
  Example:
  
$ python3 forward-port.py 26992:icanhazip.com:80 # start server
  
  and in another window:
  
$ curl localhost:26992 # connect to it
  
  import asyncio
  import logging
  import sys
  
  info = logging.getLogger('forward-port').info
  
  @asyncio.coroutine
  def copy_stream(reader, writer, bufsize=116):
  while True:
  data = yield from reader.read(bufsize)
  if not data:
  break
  writer.write(data)
  yield from writer.drain()
  writer.close()
  
  def port_forwarder(host, port, *, loop):
  @asyncio.coroutine
  def forward(local_reader, local_writer):
  client = local_writer.get_extra_info('peername')
  info('connected client %s %s', *client)
  remote_reader, remote_writer = yield from 
asyncio.open_connection(host, port, loop=loop)
  yield from asyncio.wait([copy_stream(local_reader, remote_writer),
   copy_stream(remote_reader, local_writer)],
  loop=loop)
  info('disconnected client %s %s', *client)
  
  return forward
  
  # main
  logging.basicConfig(level=logging.INFO,
  format=%(asctime)-15s %(message)s, datefmt=%F %T)
  if len(sys.argv) != 2:
  sys.exit(__doc__)
  local_port, host, port = sys.argv[1].split(':') # e.g., 12345:localhost:22
  
  loop = asyncio.get_event_loop()
  server = loop.run_until_complete(asyncio.start_server(port_forwarder(host, 
int(port), loop=loop),
'localhost', 
int(local_port), loop=loop))
  info('listening on: %s %s', *server.sockets[0].getsockname())
  for closing in range(2):
  try:
  loop.run_until_complete(server.wait_closed())
  except KeyboardInterrupt:
  if not closing:
  server.close()
  info('closing server')
  else:
  break
  info('done')
  loop.close()

[1]
https://docs.python.org/3/library/asyncio-task.html#example-parallel-execution-of-tasks

[2] http://pastebin.com/g08YaJyz


--
Akira

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


Re: localization virt-manager

2014-11-28 Thread Akira Li
Беляев Игорь belyaevigo...@yandex.ru writes:

 I can't install localization for Virt-manager (virt-manager launched on 
 python2.7 (Windows XP)).

virt-manager is a GUI for KVM, Xen, LXC virtual machines. It is a Linux
application.

 How do I correctly install location? 

Do you mean *locale*?

 How can I change the value of the environment variable LANG?

On Windows, you could use *setx* command to set an environment variable.

LANG envvar defines a default value for LC_* envvars on POSIX systems [1]
that define application's locale (after setlocale() call) e.g., in bash:

  $ LANG=en_US.UTF-8 some-program

I don't know whether LANG has any meaning on Windows.

[1] http://pubs.opengroup.org/onlinepubs/007908799/xbd/envvar.html


--
Akira


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


Re: Asyncio problem, looking for advice.

2014-11-28 Thread Akira Li
Benjamin Risher brisher...@gmail.com writes:

 On Friday, November 28, 2014 6:12:20 AM UTC-6, Akira Li wrote:
 Benjamin Risher writes:
 
  Hello all,
 
  I'm working on a project to learn asyncio and network programming.
  What I'm trying to do is forward a connection from myself to
  another machine.  Kind of like an asynchronous python
  implementation of fpipe.
 
  In a nutshell:
 
  1 -- start a server listening on localhost
  2 -- connect to server
  3 -- server connects to a listening service (ssh for instance)
  4 -- server handles both connections to pass traffic back and forth 
  through each
 
  What I have now *kind of* works.  It sends data back and forth,
  but when I ssh to localhost -p 12345, i never get the password
  prompt.  It looks like one packet hangs out and doesn't get sent
  from what I saw in tcpdump.
 
  Any help would be greatly appreciated.
 
 Do you want to emulate `ssh -L 12345:localhost:22 host`?
 
  Here's a link to the same code as below, just with syntax highlighting 
  etc...
  http://pastebin.com/iLE4GZH3
 
 There are several issue e.g., unnecessary async(), deprecated Task()
 calls but the main issue is that _handle_client() doesn't read
 concurrently from the server while client writes to it.
 
 You could use asyncio.wait() to run several tasks in parallel

 [1]
 https://docs.python.org/3/library/asyncio-task.html#example-parallel-execution-of-tasks
 
 [2] http://pastebin.com/g08YaJyz
 


 Akira, 

 First, thank you very much for your response.  It helps tremendously.
 I have a question or two though, if you don't mind.

 You said Task() is deprecated, but it's not listed as such in the
 docs.  Is it just that it's preferred to use other methods instead of
 using Task() directly, or am I missing something?

asyncio is a provisional API [3], it may change without notice (though
it shouldn't without a reason). From asyncio docs [4]:

  Don’t directly create Task instances: use the async() function or the
  BaseEventLoop.create_task() method.

The reason is probably to support Trollius (asyncio for Python 2) [5].

 Also, just so I can wrap my head around things, I was trying to mix
 concurrent and procedural programming in the code I provided, correct?
 Do you have any advice on how to avoid mixing types again in the
 future?

In short, the main issue was that your code executed some parts
sequentially e.g., A then B:

  yield from A
  yield from B # this is not reached until A finishes

that needed to be run concurrently:

  yield from asyncio.wait([A, B])

or in general, if you don't need to wait the results:

  asyncio.async(A)
  asyncio.async(B) 

  # ... yield later, to pass the control to the event loop
(Task._step/_fut_waiter dance ☯)

Ask, if you have any specific questions about the code
http://pastebin.com/g08YaJyz 

There is a bug at the end. info('done') should be outside the loop
(improper indent).

[3] https://www.python.org/dev/peps/pep-0411
[4] https://docs.python.org/3/library/asyncio-task.html#asyncio.Task
[5] https://code.google.com/p/tulip/issues/detail?id=185


--
Akira

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


Re: Iterate over text file, discarding some lines via context manager

2014-11-28 Thread Akira Li
Ned Batchelder n...@nedbatchelder.com writes:

 On 11/28/14 10:22 AM, Dave Angel wrote:
 On 11/28/2014 10:04 AM, fetchinson . wrote:
 Hi all,

 I have a feeling that I should solve this by a context manager but
 since I've never used them I'm not sure what the optimal (in the
 python sense) solution is. So basically what I do all the time is
 this:

 for line in open( 'myfile' ):
  if not line:
  # discard empty lines
  continue
  if line.startswith( '#' ):
  # discard lines starting with #
  continue
  items = line.split( )
  if not items:
  # discard lines with only spaces, tabs, etc
  continue

  process( items )

 You see I'd like to ignore lines which are empty, start with a #, or
 are only white space. How would I write a context manager so that the
 above simply becomes

 with some_tricky_stuff( 'myfile' ) as items:
  process( items )


 I see what you're getting at, but a context manager is the wrong
 paradigm.  What you want is a generator.   (untested)

 def mygenerator(filename):
  with open(filename) as f:
  for line in f:
  if not line: continue
  if line.startswith('#'): continue
  items = line.split()
  if not items: continue
  yield items

 Now your caller simply does:

 for items in mygenerator(filename):
process(items)



 I think it's slightly better to leave the open outside the generator:

 def interesting_lines(f):
 for line in f:
 line = line.strip()
 if line.startswith('#'):
 continue
 if not line:
 continue
 yield line

 with open(my_config.ini) as f:
 for line in interesting_lines(f):
 do_something(line)

 This makes interesting_lines a pure filter, and doesn't care what sort
 of sequence of strings it's operating on.  This makes it easier to
 test, and more flexible.  The caller's code is also clearer in my
 opinion.

 BTW: this example is taken verbatim from my PyCon presentation on
 iteration, it you are interested:
 http://nedbatchelder.com/text/iter.html

The conditions could be combined in this case:

  def iter_rows(lines):
  for line in lines:
  items = line.split()
  if items and not items[0].startswith('#'):
 yield items # space-separated non-emtpy non-comment items
  
  with open(filename):
  for items in iter_rows(file):
  process(items)


--
Akira

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


Re: html page mail link to webmail program

2014-11-27 Thread Akira Li
Ethan Furman et...@stoneleaf.us writes:

 On 11/11/2014 05:08 PM, Ben Finney wrote:
 Ethan Furman et...@stoneleaf.us writes:

 My wife (using a Win7 machine) will be on a web page that has a link
 to mail somebody.  She clicks on it, and it opens the currently
 installed but unused Thunderbird.

 Ideally, what would happen is a new window/tab would open to gmail
 with a new compose window with the email address in place and the
 cursor in the subject line.

 What is the Python question? I can't see anywhere that you would be
 using Python code to address this.

 Really?  Huh.

 Okay, the explicit Python question:  Clicking on a mail link in a web
 browser can start an external program.  I would like that external
 program to be a Python script that: opens a new tab in the currently
 running browser (or a new default browser window), loads up the
 default web mail client (or one specified if there is no way to
 know/have a default), navigates to the compose pane (or starts there
 if possible), enters in the email address from the link that was
 passed to it, and, if not too much more, move the cursor to the
 subject field.

 Surely this can be done in Python.

Related question:
http://stackoverflow.com/questions/14288177/interact-with-other-programs-using-python


--
Akira

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


Re: Curious function argument

2014-11-27 Thread Akira Li
ast nom...@invalid.com writes:

 Hello

 I saw in a code from a previous message in this forum
 a curious function argument.

 def test(x=[0]):
   print(x[0])   ## Poor man's object
   x[0] += 1

 test()
 0
 test()
 1
 test()
 2


 I understand that the author wants to implement a global
 variable x . It would be better to write 'global x' inside the
 function.

 At first test() function call, it prints 0, that's OK.
 But at the second call, since we dont pass any argument to test(),
 x should be equal to its default value [0] (a single item list). But
 it seems that Python keeps the original object whose content has
 been changed to 1.

 Is it a usual way to implement a global variable ?

It is not a global, a global would be easily available (visible) to
other functions. You can get it if you try but Python also allows you to
get private attributes, replace builtins and other things that you
should not normally do.

*# Poor man's object* in this context suggests that I'm the
author. The code is meant as a part of a short Python script that is
executed directly. Imagine the *test()* definition is put inside main()
function that is called when the script is run.

`def test(*, _x=[0]):..` attaches some state (_x list) to the function
*test*. It could be used as a way to emulate *nonlocal* keyword:

  def counter():
  count = 0
  def increment():
  nonlocal count
  count += 1
  return count
  return increment
  
  c = counter()
  print(c(), c()) # 0, 1
  c = counter()
  print(c(), c()) # 0, 1

an alternative is to create an object using class:

  class Counter:
  def __init__(self):
  self.count = 0
  def __call__(self):
  self.count += 1
  return self.count

  c = Counter()
  print(c(), c())
  
For this specific case, you could use `itertools.count`:

  c = itertools.count()
  print(next(c), next(c))

which could be implemented here as a generator:

  def count():
  count = 0
  while True:
 count += 1  
 yield count

  c = count()
  print(next(c), next(c))

Objects are data with methods attached, closures are functions with data
attached. [1]

Sometimes you need to define a class to create objects, in other cases a
function is enough (and of course functions are objects too in Python).

[1]
http://stackoverflow.com/questions/13857/can-you-explain-closures-as-they-relate-to-python


--
Akira


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


  1   2   3   >