Re: organizing many python scripts, in a large corporate environment.

2011-03-11 Thread eryksun ()
I'm not an expert at Python packaging, but assuming a structure such as

folder1
   \
__init__.py
module1.py
folder2
   \
__init__.py
module2.py

Then from the root folder I can run 

python -m folder1.module1

and within module1, I can import from module2, e.g.:

from folder2.module2 import foo

The __init__.py files are empty. They make Python treat the folder as a package 
namespace from which you can import, or execute a module directly with the -m 
option.

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


Re: organizing many python scripts, in a large corporate environment.

2011-03-12 Thread eryksun ()
bukzor wrote:
> This only works if you can edit the PYTHONPATH. With thousands of
> users and dozens of groups each with their own custom environments,
> this is a herculean effort.

It works for me without setting PYTHONPATH. Again, I run the module from the 
root folder with the -m option as a package module (no .py extension), which is 
easy enough to do with a Windows shortcut. 

No doubt there's a way to hack things to make direct execution work, but I 
don't think it's recommended to directly run a script that's part of a package 
namespace. Experts on the subject can feel free to chime in. 

If you need a site-wide solution that works with Unix shebang, you could 
symlink the names of all the scripts to a dispatcher. Extract the name from 
argv[0] based on the symlink filename, and then find the script in the package 
(or periodically update a stored hash of all scripts). Have the dispatcher run 
it as a package module with `python -m path.to.module [args]`. 

I hope someone else has a better idea...

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


Re: organizing many python scripts, in a large corporate environment.

2011-03-13 Thread eryksun ()
On Sunday, March 13, 2011 7:27:47 PM UTC-4, bukzor wrote:

>  e) create custom boilerplate in each script that addresses the
> issues in a-d. This seems to be the best practice at the moment...

The boilerplate should be pretty simple. For example, if the base path is the 
parent directory, then the following works for me:

import os.path
import sys
base = os.path.dirname(os.path.dirname(__file__))
sys.path.insert(0, base)

Of course, you need to have the __init__.py in each subdirectory.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get Path of current Script

2011-03-14 Thread eryksun ()
On Monday, March 14, 2011 5:53:25 AM UTC-4, Alain Ketterlin wrote:

> sys.path[0] is the path to the directory containing the script that the
> interpreter started with.

How about os.path.dirname(os.path.realpath(sys.argv[0]))? I think realpath is 
required in case someone runs it from a symlink. I don't know what can be done 
about Windows symlinks created with mklink. Thankfully that's uncommon.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing many python scripts, in a large corporate environment.

2011-03-14 Thread eryksun ()
On Monday, March 14, 2011 2:38:50 AM UTC-4, bukzor wrote:

> I've written this many times. It has issues. In fact, I've created a
> library for this purpose, for the following reasons.

If you're linking to a common file, couldn't you just add in the base folder 
there? I don't think it's a bad practice to hard-code an absolute path in a 
single file. If the path changes you only have to update one line. For example:

# script.py
import _path   # _path.py is a symbolic link

# _path.py:
base = '/absolute/path/to/base'
import site
site.addsitedir(base) 

This also adds paths from any .pth files located in the base folder, but that 
shouldn't be necessary if all of the scripts are located within one package and 
sub-packages. 

On a Posix-compliant system, you should be able to use the following to avoid 
hard-coding the path (untested, though):

# _path.py:
import os.path
import site
base = os.path.dirname(os.path.realpath(__file__))
site.addsitedir(base) 

Possibly it has to be abspath(realpath(__file__)), but the docs say realpath 
returns a 'canonical format', which should be the absolute path. I can't check 
right now, and this doesn't work for me at all on Windows. Python can't resolve 
the symbolic links created by mklink to the real path, which is odd since 
mklink has been standard in Windows for many years now. Maybe Python 3.x 
handles it better.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing many python scripts, in a large corporate environment.

2011-03-14 Thread eryksun ()
On Monday, March 14, 2011 9:45:51 AM UTC-4, eryksun () wrote:
> 
> If you're linking to a common file, couldn't you just add in 
> the base folder there? 
>
> ...
> 
> # script.py
> import _path   # _path.py is a symbolic link
> 
> # _path.py:
> base = '/absolute/path/to/base'
> import site
> site.addsitedir(base) 

To be clear on the file structure, I'm picturing that 'base' is a path on each 
user's shell path where all the accessible scripts are linked, and that this is 
also the package directory. So when a linked script runs "import _path" it will 
import the _path.py that's located in base, which adds the base path to 
Python's sys.path. On the other hand, any subsequently imported modules will be 
found by searching sys.path. Thus each subdirectory needs the additional 
symbolic link back to the base _path.py. It's a bit convoluted, but so are the 
constraints of this problem -- at least to me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing many python scripts, in a large corporate environment.

2011-03-14 Thread eryksun ()
On Monday, March 14, 2011 3:56:15 PM UTC-4, eryksun () wrote:
> To be clear on the file structure, I'm picturing that 'base' is a 
> path on each user's shell path where all the accessible scripts 
> are linked, and that this is also the package directory. 

Wait, this won't work when the script is linked to from somewhere else, which 
means the code still has to be based on __file__ or sys.argv[0] or sys.path[0], 
and have to get the absolute/real path in case it's a link. 

Along those lines, you (bukzor) wrote that

> What I do right now is to symlink this library to all script 
> directories to allow them to bootstrap and gain access to 
> libraries not in the local directory.

Won't this also fail if it's running from a link? The link to the library won't 
necessarily be in the current directory.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get Path of current Script

2011-03-14 Thread eryksun ()
On Monday, March 14, 2011 5:17:49 PM UTC-4, Grant Edwards wrote:
>
> Indeed that is very common, and there's been a "standard" way to do
> that since before dirt.
> 
> The standard on Unix is to look in the following places in (this
> order), and use the first one you find:

On Windows it's typical to use the system registry HKLM\Sofwtare and/or 
HKCU\Software, or a system-wide config files in %ALLUSERSPROFILE% (probably 
read-only for unprivileged users) or per-user config in %APPDATA% or 
%LOCALAPPDATA%. Typically all settings and data will stored in a hierarchy 
relative to the latter, such as Publisher\App\{config}.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing many python scripts, in a large corporate environment.

2011-03-15 Thread eryksun ()
On Tuesday, March 15, 2011 12:44:48 AM UTC-4, bukzor wrote:
> When looking at google code search, this kind of code is 
> rampant (below). Is everyone really happy with this?
> 
> sys.path.insert(0,
> os.path.dirname(os.path.dirname(
> os.path.dirname(os.path.abspath(__file__)

Applications and libraries should be properly packaged (e.g. distutils, egg, 
rpm, deb, msi, etc). If that can't be done for some reason, then we're stuck 
with kludges.

If you could just get a .pth file symlink'd to site-packages.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get Path of current Script

2011-03-15 Thread eryksun ()
On Tuesday, March 15, 2011 9:47:17 AM UTC-4, Mario Rol wrote:
> 
> os.path.join(sys.path[0], 'config.txt')
> 
> If the script and config.txt are in /tmp this will return '/tmp/
> config.txt' no matter from which directory you started the script.

You have to be careful about symlinks. Even Windows has support for symlinks, 
since version 6, via mklink (jaraco.windows can monkeypatch os.readlink to 
support NTFS symlinks). 

Maybe it would be better to use setuptools, which will also work for eggs and 
zip files that don't use __file__: 

http://packages.python.org/distribute/setuptools.html

Included data files are accessible via 'pkg_resources', and the installer can 
deploy 'console_scripts' and 'gui_scripts' as 'entry_points' in locations 
specific to each supported platform.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing many python scripts, in a large corporate environment.

2011-03-16 Thread eryksun ()
On Tuesday, March 15, 2011 12:44:48 AM UTC-4, bukzor wrote:
>
> Currently it requires either: 1) no symlinks to scripts or 2)
> installation of the pathtools to site-packages.

An executable with a unique name on the system PATH could be executed it as a 
subprocess that pipes the configured base directory back via stdout. Then you 
can just do site.addsitedir(base).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Memory Usage of Strings

2011-03-16 Thread eryksun ()
On Wednesday, March 16, 2011 2:20:34 PM UTC-4, Amit Dev wrote:
>
> sum(map(len, l)) =>  8200 for 1st case and 9100 for 2nd case.
> Roughly 100MB as I mentioned.

The two lists used approximately the same memory in my test with Python 2.6.6 
on Windows. An implementation detail such as this is likely to vary between 
interpreters across versions and platforms, including Jython and IronPython. A 
classic implementation detail is caching of small objects that occur 
frequently, such as small strings, integers less than 512, etc. For example:

In [1]: a = 20 * '5'
In [2]: b = 20 * '5'
In [3]: a is b
Out[3]: True

In [4]: a = 21 * '5'
In [5]: b = 21 * '5'
In [6]: a is b
Out[6]: False

It's best not to depend on this behavior.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing many python scripts, in a large corporate environment.

2011-03-17 Thread eryksun ()
On Wednesday, March 16, 2011 9:03:19 PM UTC-4, bukzor wrote:
>
> I finally understand. You mean something along the lines of `kde-
> config`: an executable to help figure out the configuration at
> runtime. This requires either installation or control of the $PATH
> environment variable to work well, which makes not so useful to me.

There's always the virtualenv option in a POSIX system. Just change the shebang 
to point to your package's virtual Python installation. That obviously won't 
work for Windows, which uses system-wide file-type associations. I think the 
closest to shebang-style control on Windows would require changing the file 
extension to something like 'py_' and having an admin add the appropriate 
ftype/assoc to the system, the same as is done for the pyw extension. That 
would be obscene.

If the problem with installation is merely continuing to have central access to 
the scripts for development, you could deploy with setuptools in development 
mode:

http://packages.python.org/distribute/setuptools.html#development-mode
http://packages.python.org/distribute/setuptools.html#develop

However, I think over time you may come to regret this as other group's 
projects become dependent on your code's behavior (including bugs) and a rigid 
interface.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP for module naming conventions

2011-03-17 Thread eryksun ()
On Friday, March 11, 2011 4:52:57 PM UTC-5, Tim Johnson wrote:
> I need to be better informed on naming conventions for modules.  For
> instance, I need to create a new module and I want to make sure that
> the module name will not conflict with any future or current python
> system module names.

Do you mean package names? Within a package you can use relative imports to 
avoid conflicts. You could put all of your packages in a metapackage namespace 
with a unique name -- a company/group name or personal/family name, an uncommon 
English word, or something from another language.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fitting polynomial curve

2011-03-18 Thread eryksun ()
On 3/17/2011 1:42 AM, Astan Chee wrote:
>
> I have 2 points in 3D space and a bunch of points in-between them. I'm
> trying to fit a polynomial curve on it. Currently I'm looking through
> numpy but I don't think the function exists to fit a function like this:
> y = ax**4 + bx**3 + cx**2 + dx + e

You can use np.polyfit, which uses np.linalg.lstsq. 

For a degree M-1 polynomial and K samples such that K > M, this is an 
over-determined least squares problem:

t = [t1, t2, ..., tk]   # K sample times
Y.shape == (K, N)   # data samples Y in R_N

# design matrix X
# you don't have to calculate this since polyfit does it
# for you internally with vander(t, M)
X = [[x**m for m in range(M-1,-1,-1)] for x in t]
X.shape == (K, M)

# unknown coefficient matrix B
B.shape == (M, N)  

Y =~ dot(X, B) 

Use polyfit to form the least squares solution. For example, a 4th order fit:

B = np.polyfit(t, Y, 4)

# or for more information
B, r, rankX, sX, rcond = np.polyfit(t, Y, 4, full=True)

where r is the vector of residuals; rankX and sX are the rank and singular 
values of the Van der Monde matrix; and rcond was used to threshold the 
singular values.

Interpolation: 

t2 = np.linspace(t[0], t[-1], len(t)*1000)

Y2 = np.dot(np.vander(t2, M), B)
or 
Y2 = np.polyval(B, t2[:, newaxis])

polyval uses Horner's method, which calculates the following:

t = t2[:, newaxis]
y = zeros_like(t)

for i in range(len(B)):
y = y * t + B[i]
return y

y is initially a length len(t) column vector of zeros and B[0] is a length N 
row vector, so the first iteration just tiles B[0] in a len(t) by N matrix. 
Otherwise it's a normal Horner evaluation.

Other than minimizing the residuals, you can examine the fit visually with a 
simple matplotlib plot:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(*Y2.T)
ax.plot(*Y.T, marker='.', markerfacecolor='r')

Or you could create three 2D plots of (t2, Y2[:,i]) and (t, Y[:,i]).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Abend with cls.__repr__ = cls.__str__ on Windows.

2011-03-18 Thread eryksun ()
On Thursday, March 17, 2011 8:24:36 PM UTC-4, J Peyret wrote:
>
> I suspect that object.__str__ is really object.__repr__ by default, as
> they both print out the same string, so that this doesn't make any
> sense.

They're not the same object, and they don't have all of the same methods. 

In [1]: object.__repr__ is object.__str__
Out[1]: False

In [2]: object.__repr__.__name__
Out[2]: '__repr__'

In [3]: object.__str__.__name__
Out[3]: '__str__'

In [4]: object.__repr__.__hash__()
Out[4]: 28910896

In [5]: object.__str__.__hash__()
Out[5]: 28910976

In [6]: object.__repr__.__call__(100)
Out[6]: ''

In [7]: object.__str__.__call__(100)
Out[7]: '100'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import newer

2011-03-20 Thread eryksun ()
On Sunday, March 20, 2011 12:27:38 AM UTC-4, xinyou yan wrote:
> I begin to study with  <>
> 
> I met a problem with import.
> 
> first
> I creat a  file  hello.py
> 
> then in fedora /14
> I type python to  the interpreter
> 
> >>> import hello
> Traceback (most recent call last):
>   File "", line 1, in 
> ImportError: No module named hello
> 
> What should i do now.
> The  current path is not  added  defaultly ?

>>> import os
>>> os.listdir(os.getcwd())

Is hello.py in the returned list?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python time

2011-03-20 Thread eryksun ()
On Monday, March 21, 2011 12:07:13 AM UTC-4, ecu_jon wrote:
> so then why does this not work ?
>
> import time
> ...
> time = config.get("myvars", "time")
> ...
> while a>0:
> #import time
> time.sleep(2)
> 
> if i uncomment the import time line in the while loop it works.
> boggle...

An imported module is an object:

>>> import time
>>> repr(time)
""

>>> dir(time)
['__doc__', '__name__', '__package__', 'accept2dyear', 
'altzone', 'asctime', 'clock', 'ctime', 'daylight', 
'gmtime', 'localtime', 'mktime', 'sleep', 'strftime', 
'strptime', 'struct_time', 'time', 'timezone', 'tzname']

In Python variables are references to objects. for example, you could do the 
following:

>>> import time as foo
>>> repr(foo)
""

>>> dir(foo)
['__doc__', '__name__', '__package__', 'accept2dyear', 
'altzone', 'asctime', 'clock', 'ctime', 'daylight', 
'gmtime', 'localtime', 'mktime', 'sleep', 'strftime', 
'strptime', 'struct_time', 'time', 'timezone', 'tzname']

When you execute 'time = config.get("myvars", "time")', the variable "time" now 
references a string object instead of the module.  Strings don't 'sleep'.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why memoizing is faster

2011-03-25 Thread eryksun ()
On Thursday, March 24, 2011 8:12:22 PM UTC-4, Terry Reedy wrote:
>
> If Python did what some have asked, which is to make 'recursive' 
> functions actually recursive by replacing a local variable that matches 
> the function name (in this 'fib') with a direct reference to the 
> function itself, as a constant (and this could be done), then the 
> wrapper would not get called from within the function.

Regarding this I have question about function attributes. Is there an 
equivalent of 'self' for functions? I've seen people use function attributes as 
local static variables, but this seems problematic when all you have is a 
global reference to the function. The original reference might get deleted. 
Then when your function tries to reference its 'static' variable you'll get a 
NameError exception. For example:

In [1]: def test1(n):
   ...: test1.a = n

In [2]: test1(10); test1.a
Out[2]: 10

In [3]: test2 = test1; test2.a
Out[3]: 10

In [4]: del test1

In [5]: test2(20); test2.a
---
NameError


Should such a function always take a reference to itself as the first 
parameter?  

In [6]: def test1(f, n):
   ...: f.a = n

In [7]: test1(test1, 10); test1.a
Out[7]: 10

In [8]: test2 = test1; test2.a
Out[8]: 10

In [9]: del test1

In [10]: test2(test2, 20); test2.a
Out[10]: 20

Granted, this would be better handled in a class.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why memoizing is faster

2011-03-25 Thread eryksun ()
On Friday, March 25, 2011 6:53:48 AM UTC-4, Andrea Crotti wrote:
>
> I had never seen such a thing, but why would this make sense at all?
> When does it make sense logically for a function to have an attribute?
> 
> A function should have some input, some local variables to compute
> temporary results and a return value, I think nothing more...

See Pep 232: http://www.python.org/dev/peps/pep-0232

Also, see the discussion thread on Python-Dev:

http://mail.python.org/pipermail/python-dev/2000-April/thread.html#3282

I don't think self-referenced static variables (as in C's "static" declaration) 
are discussed as a use case. Nonetheless, I've seen it done. For example:

http://stackoverflow.com/questions/338101/python-function-attributes-uses-and-abuses/338420#338420
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python 2.7.1 "serial" vs "pyserial"

2011-03-25 Thread eryksun ()
On 3/25/2011 7:27 AM, bruce bushby wrote:
>
> Is there any difference between the "serial" module in Python 2.7.1 and
> "pyserial 2.5" ?

I've never used it, but "pyserial" is actually "serial": 

http://pyserial.sourceforge.net

I have it installed on my system via Christoph Gohlke's "Base" distribution. 
AFAIK, it's not in the standard install of Python 2.7. 

> However lots of scripts
> "import serial"
> and then
> "ser.readline(size=None, eol=chr(13))"

That's odd that it says keywords aren't supported. The method signature is 

def readline(self, size=None, eol=LF):

This is from the FileLike class in serialutil.py, line 141 here:

http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/serial/serialutil.py?revision=398&view=markup

Are you sure you're not using an actual 'file' object? The latter's readline 
method takes a size argument, but as a regular positional argument.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing to a file

2011-03-25 Thread eryksun ()
On Friday, March 25, 2011 11:07:19 AM UTC-4, jyou...@kc.rr.com wrote:
>
> >>> f = open('~/Desktop/test.txt', 'w')
> >>> f.write('testing 1... 2... 3...')
> >>> f.close()

Consider using "with" to automatically close the file and os.path for 
cross-platform compatibility:

import os.path
user_home = os.path.expanduser('~')
test_absname = os.path.join(user_home, 'Desktop', 'test.txt')

with open(test_absname, 'w') as test:
test.write('testing 1... 2... 3...')

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


Re: Writing to a file

2011-03-25 Thread eryksun ()
On Friday, March 25, 2011 9:39:11 PM UTC-4, Littlefield, Tyler wrote:
> >with open(test_absname, 'w') as test:
> what's the difference in that and test = ...? I can see why you 
> mentioned the os.path for cross-platform, but I don't understand why 
> someone would use with over =.

It avoids having to write a try...finally block to close the file automatically 
and immediately in case of an error. Also, you can easily chain additional 
context managers into this syntax, such as for redirecting standard 
input/output to the file. More info here:

http://diveintopython3.org/files.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why memoizing is faster

2011-03-26 Thread eryksun ()
On Saturday, March 26, 2011 7:50:36 AM UTC-4, Steven D'Aprano wrote:
> 
> That's of the order of 200 MB of memory -- not that much for today's 
> systems. I've had people email me .doc files that big *wink*

Yikes! I know this thread is about caching the output of a function, but in the 
example of Fibonacci numbers, we're blessed with an easily derived closed form 
expression (via Z transform, etc):

def fib(n):
phi = (5 ** 0.5 + 1) / 2
f = (phi ** n - (1 - phi) ** n) / 5 ** 0.5
return int(f)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why memoizing is faster

2011-03-26 Thread eryksun ()
On Saturday, March 26, 2011 11:49:02 AM UTC-4, Steven D'Aprano wrote:
> >
> > def fib(n):
> > phi = (5 ** 0.5 + 1) / 2
> > f = (phi ** n - (1 - phi) ** n) / 5 ** 0.5 
> > return int(f)
> 
> Have you tried it?
> 
> Unfortunately, with floating point rounding error, that rapidly becomes 
> inaccurate. It gives:
> 
> >>> fib(72)
> 498454011879265
> 
> compared to the correct value of 498454011879264 (an error of 1) and 
> rapidly gets worse. It's true that the above formula is correct for real 
> numbers, but floats are not reals. Unless you have an infinite number of 
> decimal places, it will be inaccurate.

That's true. If you need more than double precision, obviously it's time to 
defer to the experts. Here's an O(n) algorithm:

http://www.gnu.org/software/gmp/manual/html_node/Fibonacci-Numbers-Algorithm.html

This library is wrapped by the gmpy extension module:

http://code.google.com/p/gmpy

I was just shocked to see 200 MB of memory used up like chump change, and I 
immediately looked for an alternative. In practice, I prefer to rely on 
libraries created by specialists who have carefully worked out the best 
trade-offs for typical use, and puzzled through the algorithms needed to 
compute that which is so easy to derive on paper (such as my simple little 
Z-transform derived formula that fails so wonderfully in practice). I'm not a 
computer scientist; I don't even play one on TV.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding interactive python interpreter

2011-03-27 Thread eryksun ()
On Friday, March 25, 2011 12:02:16 PM UTC-4, Eric Frederich wrote:
>
> Is there something else I should call besides "exit()" from within the
> interpreter?
> Is there something other than Py_Main that I should be calling?

Does PyRun_InteractiveLoop also have this problem?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Standard way to distribute utilities with packages

2011-03-27 Thread eryksun ()
On Sunday, March 27, 2011 4:05:30 PM UTC-4, Laszlo Nagy wrote:
> I'd like to distribute a pure Python package named "foo". By default it 
> will be placed in lib/site-packages/foo. What if I want to add 
> utilities? Command line or GUI programs that are not full featured 
> applications, but they can be handy for some tasks that are related to 
> the package. Here is what I see:

setuptools can create console and GUI scripts:

http://packages.python.org/distribute/setuptools.html#automatic-script-creation

These are installed in default locations such as C:\Python27\Scripts. If 
--install-dir is specified, the scripts will also be installed there unless 
--script-dir is specified.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Data files for tests

2011-03-27 Thread eryksun ()
On Sunday, March 27, 2011 7:07:33 PM UTC-4, Steven D'Aprano wrote:
> I have a package with some tests. The tests are not part of the package 
> itself, so I have a laid out my files like this:
> 
> 
> src/
> spam/
> __init__.py
> other-files.py
> test_spam.py
> 
> 
> Some of the tests depend on external data files. Where should I put them? 
> In the same directory as test_spam?

Including data files and accessing them via the resource management API:

http://packages.python.org/distribute/setuptools.html#including-data-files
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Data files for tests

2011-03-27 Thread eryksun ()
On Sunday, March 27, 2011 7:33:22 PM UTC-4, eryksun () wrote:
> On Sunday, March 27, 2011 7:07:33 PM UTC-4, Steven D'Aprano wrote:
> > I have a package with some tests. The tests are not part of the package 
> > itself, so I have a laid out my files like this:
> > 
> > 
> > src/
> > spam/
> > __init__.py
> > other-files.py
> > test_spam.py
> > 
> > 
> > Some of the tests depend on external data files. Where should I put them? 
> > In the same directory as test_spam?
> 
> Including data files and accessing them via the resource management API:
> 
> http://packages.python.org/distribute/setuptools.html#including-data-files

For example, if test_spam is in a separate package:

setup.py
src/
spam/
__init__.py
other-files.py
test_spam/
__init__.py
test_spam.py
data/
test_data.dat

setup.py:

from setuptools import setup, find_packages
setup(
# ...

packages = find_packages('src'),  # include all packages under src
package_dir = {'':'src'},   # tell distutils packages are under src

package_data = {
# include any *.dat files found in the 'data' subdirectory
# of the 'test_spam' package:
'test_spam': ['data/*.dat'],
}
)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: embedding interactive python interpreter

2011-03-27 Thread eryksun ()
On Sunday, March 27, 2011 11:06:47 PM UTC-4, Eric Frederich wrote:
> I'm not sure that I know how to run this function in such a way that
> it gives me an interactive session.
> I passed in stdin as the first parameter and NULL as the second and
> I'd get seg faults when running exit() or even imnport sys.

Passing NULL as the 2nd parameter causes the segfault. Do this instead:

FILE* fp = stdin;
char *filename = "Embedded";
PyRun_InteractiveLoop(fp, filename);

See here regarding the segfault:

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


Re: how to create a virtual printer

2011-03-29 Thread eryksun ()
On Monday, March 28, 2011 7:12:23 AM UTC-4, kalo...@gmail.com wrote:
> 
> Does anybody know how to create a virtual
> printer with python (on both windows and linux)?

Here's some code I just put together showing how to use Python to create a 
PostScript file printer on Windows, output raw text to a file, and also output 
text with fonts and simple line graphics. You'll need the pywin32 extensions. 

This code is only minimally tested, and it does little if any error checking. 
However, this is just to demonstrate the gist of using pywin32 for printing 
PostScript to a file. If you get an error about finding the driver for "MS 
Publisher Imagesetter', you may need to add the printer manually to make the 
driver available (it's under the 'generic' manufacturer). No doubt there's a 
way to install this driver automatically using the Win32 API, but I haven't 
looked into it.

The generated PostScript file can be viewed on Windows using the evince viewer:

http://live.gnome.org/Evince/Downloads

#

import win32print, win32ui, win32gui
import win32con, pywintypes

def create_printer(printer_name='MyPSPrinter'):
printer_info = {
'pPrinterName': printer_name,
'pDevMode': pywintypes.DEVMODEType(),
'pDriverName': 'MS Publisher Imagesetter',
'pPortName': 'FILE:',
'pPrintProcessor': 'WinPrint',
'Attributes': 0,
'AveragePPM': 0,
'cJobs': 0,
'DefaultPriority': 0,
'Priority': 0,
'StartTime': 0,
'Status': 0,
'UntilTime': 0,
'pComment': '',
'pLocation': '',
'pDatatype': None,
'pParameters': None,
'pSecurityDescriptor': None,
'pSepFile': None,
'pServerName': None,
'pShareName': None}

h_printer = win32print.AddPrinter(None, 2, printer_info)
return h_printer


def test_raw(printer='MyPSPrinter',
 filename=r'D:\test.txt',
 text=None):
'''send plain text to a file'''
if text is None:
text_data = "This is a test. This is only a test."
else:
text_data = text
job_info = ("Raw File Print", filename, 'RAW')
h_printer = win32print.OpenPrinter(printer)
try:
h_job = win32print.StartDocPrinter(h_printer, 1, job_info)
try:
win32print.StartPagePrinter(h_printer)
win32print.WritePrinter(h_printer, text_data)
win32print.EndPagePrinter(h_printer)
finally:
win32print.EndDocPrinter(h_printer)
finally:
win32print.ClosePrinter(h_printer)


def test_ps(printer='MyPSPrinter',
filename=r'D:\test.ps',
margin=(0.25,1.5,0.25,1.0),
font_size=24,
text=None):
'''render postscript text and graphics to a file'''
if text is None:
text_data = "This is a test.\nThis is only a test."
else:
text_data = str(text)

# Get the printer's DEVMODE structure
h_printer = win32print.OpenPrinter(printer)
devmode = win32print.GetPrinter(h_printer, 2)['pDevMode']
win32print.ClosePrinter(h_printer)

# set up the device context
# see MSDN: ff552837, aa452943, dd319099, dd145045
devmode.FormName = 'Letter'  # or 'A4'
devmode.PaperSize = win32con.DMPAPER_LETTER  # or DMPAPER_A4
devmode.Orientation = win32con.DMORIENT_PORTRAIT 
devmode.PrintQuality = win32con.DMRES_HIGH
devmode.Color = win32con.DMCOLOR_MONOCHROME
devmode.TTOption = win32con.DMTT_SUBDEV
devmode.Scale = 100
devmode.Fields |= (win32con.DM_FORMNAME | 
   win32con.DM_PAPERSIZE | 
   win32con.DM_ORIENTATION | 
   win32con.DM_PRINTQUALITY | 
   win32con.DM_COLOR | 
   win32con.DM_TTOPTION | 
   win32con.DM_SCALE)
h_dc = win32gui.CreateDC('WINSPOOL', printer, devmode)
dc = win32ui.CreateDCFromHandle(h_dc)
dc.SetMapMode(win32con.MM_TWIPS) # or MM_HIMETRIC (0.01 mm)

# begin writing the document
dc.StartDoc('Postscript File Print', filename)
dc.StartPage()

# we need a pen and a font
scale = 20  # 72 pt/inch * 20 twip/pt = 1440
inch = 72*scale
pen = win32ui.CreatePen(win32con.PS_SOLID,
scale, # 1 pt
0) # black
dc.SelectObject(pen)
font = win32ui.CreateFont({
'name': 'Times New Roman',
'height': font_size * scale,
'weight': win32con.FW_NORMAL})
dc.SelectObject(font)

# output the text
x = int(margin[0] * inch)
y = -int(margin[1] * inch)
width = int((8.5 - margin[0] - margin[2]) * inch)
height = int((11.0 - margin[1] - margin[3]) * inch)
rect = (x, y, x + width, y - height)
dc.DrawText(text_data, rect, win32con.DT_LEFT)

if text is None:
# draw 8 steps starting at x = 0.25", y = 3"
width = inch
height = inch
for n in range(8):
x 

Re: [pyplot] using f1=figure(1)

2011-03-30 Thread eryksun ()
On Monday, March 28, 2011 12:04:02 PM UTC-4, Giacomo Boffi wrote:
>
> >>> f1=figure(1)
> >>> f2=figure(2)
> >>> f1
> 
> >>> f2
> 
> >>> plot(sin(linspace(0,10)),figure=f1)
> []
> >>> plot(cos(linspace(0,10)),figure=f2)
> []
> >>> show()

You can set the current figure to fig1 with the following:

figure(fig1.number)
plot(...)

Alternatively, you can use the plot methods of a particular axes:

fig1 = figure()
ax1 = axes()
fig2 = figure()
ax2 = axes()

ax1.plot(...)
ax2.plot(...)

It works the same for subplots:

fig1 = figure()
ax11 = subplot(211)
ax12 = subplot(212)
fig2 = figure()
ax21 = subplot(211)
ax22 = subplot(212)
 
ax12.plot(...)
#etc
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Directly Executable Files in Python

2011-03-30 Thread eryksun ()
On Tuesday, March 29, 2011 3:51:30 AM UTC-4, Paul Rudin wrote:
> Benjamin Kaplan  writes:
> 
> > If you can figure out a good way to compile a language like Python,
> > you'll be very rich. Yes, it is running the interpreter and then
> > running the bytecode on the interpreter. It's the same way Java and
> > .NET work.
> 
> Not exactly AIUI. .NET bytecodes do actually get compiled to executable code
> before being executed (unless things have changed recently - I haven't
> really done anything significant with .NET in the last couple of years).

Java and languages in Microsoft's CLI (common language infrastructure) are 
statically typed, so it's not exactly a straight-forward comparison. 

IIRC, IronPython programs compile to a DLR (dynamic language runtime) AST 
(abstract source tree). This represents the program as runtime method calls and 
invoked DynamicSite objects. A site creates a caching delegate that checks the 
given argument types. As it encounters new combinations of argument types, it 
compiles the operation and updates the delegate.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing Pool.imap broken?

2011-03-30 Thread eryksun ()
On Tuesday, March 29, 2011 9:44:21 PM UTC-4, Yang Zhang wrote:
> I've tried both the multiprocessing included in the python2.6 Ubuntu
> package (__version__ says 0.70a1) and the latest from PyPI (2.6.2.1).
> In both cases I don't know how to use imap correctly - it causes the
> entire interpreter to stop responding to ctrl-C's.  Any hints?  Thanks
> in advance.
> 
> $ python
> Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
> [GCC 4.4.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import multiprocessing as mp
> >>> mp.Pool(1).map(abs, range(3))
> [0, 1, 2]
> >>> list(mp.Pool(1).imap(abs, range(3)))
> ^C^C^C^C^\Quit

It works fine for me on Win32 Python 2.7.1 with multiprocessing 0.70a1. So it's 
probably an issue with the implementation on Linux.

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) 
[MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import multiprocessing as mp
>>> list(mp.Pool(1).imap(abs, range(3)))
[0, 1, 2]
>>> mp.__version__
'0.70a1'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: learn python the hard way exercise 42 help

2011-03-30 Thread eryksun ()
On Wednesday, March 30, 2011 9:48:29 AM UTC-4, neil harper wrote:
> http://pastie.org/1735028
> hey guys play is confusing me, i get how next gets the first room, which
> is passed when the instance of Game() is created, but how does it get
> the next room?
> 
> thanks

Each room is a method of Game. The returned value of each room is a reference 
to another room, depending on some condition. play() just loops the following: 
call room(); store the return reference as next; assign next to room. This 
continues until death(), which randomly insults you and quits.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sudden error: SyntaxError: Non-ASCII character '\xc2' in file

2011-03-30 Thread eryksun ()
On Wednesday, March 30, 2011 10:34:46 AM UTC-4, Gnarlodious wrote:
> 
> The error originates at '·' which string contains a ·
> character.
> 
> Complete error message is:
> 
> SyntaxError: Non-ASCII character '\xc2' in file /Library/WebServer/
> Sites/Sectrum/Site/Feed.py on line 17, but no encoding declared; see
> http://www.python.org/peps/pep-0263.html for details

A middle dot is Unicode \x00\xb7, which maps to UTF-8 \xc2\xb7. According to 
PEP 3120 the default source encoding for Python 3.x is UTF-8. (I'll take their 
word for it, since I'm still using 2.7). Are you declaring an ASCII encoding 
(e.g. # coding: ascii)? If not, are you sure that you're running in 3.x?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: popular programs made in python?

2011-03-30 Thread eryksun ()
On Tuesday, March 29, 2011 10:32:26 AM UTC-4, Neil Alt wrote:
> i mean made with python only, not just a small part of python.

I think it's uncommon for an application to be programmed entirely in Python. 
It's common to use C/C++ to accelerate performance critical parts of the code. 
I don't see that as a weakness of Python. The developer uses whichever tools 
work best for the task at hand.

Calibre is a popular e-book manager/converter created by Kovid Goyal. It's 
mostly written in Python, with some C extensions for speedups (e.g. SQLite 
database access) and interfacing to OS APIs for fonts, USB devices, etc:

http://bazaar.launchpad.net/~kovid/calibre/trunk/files


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


Re: Learn Python the Hardway exercise 11 question 4

2011-03-31 Thread eryksun ()
On Wednesday, March 30, 2011 11:03:09 PM UTC-4, JosephS wrote:
> print "How old are you?", age = raw_input()
> print "How tall are you?", height = raw_input()
> print "How much do you weigh?", weight = raw_input()
> print "So, you're %r old, %r tall and %r heavy." % ( age, height,
> weight)
> Note:
> Notice that we put a , (comma) at the end of each print line. This is
> so that print doesn’t end the line with a newline and go to the next
> line.
> What You Should See
> Extra Credit
> 1. Go online and find out what Python’s raw_input does.
> $ python ex11.py How old are you?
> 35 How tall are you?
> 6'2" How much do you weigh? 180lbs
> So, you're '35' old, '6\'2"' tall and '180lbs' heavy.
> 
> Related to escape sequences, try to find out why the last line has
> ’6\’2"’ with that \’ sequence. See how the single-quote needs to be
> escaped because otherwise it would end the string?

There appears to be a formatting error here. The following works:

print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()
print "So, you're %r old, %r tall and %r heavy." % ( age, height, weight)

Regarding the escape sequence, your're using %r which shows the representation 
(repr) of the string. For example:

In [1]: [x for x in height]
Out[1]: ['6', "'", '2', '"']

In [2]: print height
6'2"

In [3]: [x for x in repr(height)]
Out[3]: ["'", '6', '\\', "'", '2', '"', "'"]

In [4]: print repr(height)
'6\'2"'

The characters stored in height are just 6'2", but you couldn't input the 
string like that as a variable since Python uses quotes to demarcate a string 
literal. The backslash escapes the quote so that the interpreter will treat it 
as a regular character. It's unnecessary to escape the double quote, on the 
other hand, since this string literal is marked with single quotes. If the 
string were instead inputted with double quotes, then the final double quote 
would have to be escaped, such as the following: height = "6'2\"". Finally, 
notice that in the output representation of the characters of repr(height) 
(Out[3]) that the backslash itself has to be escaped. Otherwise it would be 
interpreted as escaping the single quote that follows.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing Pool.imap broken?

2011-03-31 Thread eryksun ()
For comparison, here's the debug info on Win32 Python 2.71. Multiprocessing on 
Windows seems like a very different beast (e.g. there's no way to fork).

Case 1:

In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop.
:import multiprocessing as mp
:import multiprocessing.util as util
:util.log_to_stderr(util.SUBDEBUG)
:
:print(list(mp.Pool(1).imap(abs, range(3
:--
[DEBUG/MainProcess] created semlock with handle 604
[DEBUG/MainProcess] created semlock with handle 644
[DEBUG/MainProcess] added worker
[DEBUG/MainProcess] doing set_length()
[DEBUG/PoolWorker-1] recreated blocker with handle 36
[DEBUG/PoolWorker-1] recreated blocker with handle 48
[INFO/PoolWorker-1] child process calling self.run()
[0, 1, 2]

Case 2:

In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop.
:import multiprocessing as mp
:import multiprocessing.util as util
:util.log_to_stderr(util.SUBDEBUG)
:pool=mp.Pool(1)
:print list(pool.imap(abs, range(3)))
:--
[DEBUG/MainProcess] created semlock with handle 604
[DEBUG/MainProcess] created semlock with handle 644
[DEBUG/MainProcess] added worker
[DEBUG/MainProcess] doing set_length()
[DEBUG/PoolWorker-1] recreated blocker with handle 24
[DEBUG/PoolWorker-1] recreated blocker with handle 32
[INFO/PoolWorker-1] child process calling self.run()
[0, 1, 2]


On Thursday, March 31, 2011 2:46:25 PM UTC-4, Yang Zhang wrote:
> The problem was that Pool shuts down from its finalizer:
> 
> http://stackoverflow.com/questions/5481104/multiprocessing-pool-imap-broken/5481610#5481610
> 
> On Wed, Mar 30, 2011 at 5:59 AM, eryksun ()  wrote:
> > On Tuesday, March 29, 2011 9:44:21 PM UTC-4, Yang Zhang wrote:
> >> I've tried both the multiprocessing included in the python2.6 Ubuntu
> >> package (__version__ says 0.70a1) and the latest from PyPI (2.6.2.1).
> >> In both cases I don't know how to use imap correctly - it causes the
> >> entire interpreter to stop responding to ctrl-C's.  Any hints?  Thanks
> >> in advance.
> >>
> >> $ python
> >> Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
> >> [GCC 4.4.3] on linux2
> >> Type "help", "copyright", "credits" or "license" for more information.
> >> >>> import multiprocessing as mp
> >> >>> mp.Pool(1).map(abs, range(3))
> >> [0, 1, 2]
> >> >>> list(mp.Pool(1).imap(abs, range(3)))
> >> ^C^C^C^C^\Quit
> >
> > It works fine for me on Win32 Python 2.7.1 with multiprocessing 0.70a1. So 
> > it's probably an issue with the implementation on Linux.
> >
> > Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46)
> > [MSC v.1500 32 bit (Intel)] on win32
> > Type "help", "copyright", "credits" or "license" for more information.
> >>>> import multiprocessing as mp
> >>>> list(mp.Pool(1).imap(abs, range(3)))
> > [0, 1, 2]
> >>>> mp.__version__
> > '0.70a1'
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> 
> 
> 
> -- 
> Yang Zhang
> http://yz.mit.edu/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Learn Python the Hardway exercise 11 question 4

2011-03-31 Thread eryksun ()
On Thursday, March 31, 2011 4:35:42 PM UTC-4, Chris Angelico wrote:
>
> I was trolling, I know the reasons behind it. Anyway, most people
> don't share code by email! (Actually, since you seem to be the author
> of that page - could you address that particular point? I think it's
> probably as big an issue as any of the others, to today's coders -
> "code semantics get destroyed by forums/email/etc/etc/etc".)
> 
> Solution: All emailed code should begin with
> from __future__ import braces
> And there you are, out of your difficulty at once!

You could paste it as a base64 stream, such as:


> ZGVmIHNwYW0oKToNCiAgICBwcmludCAiU3BhbSEg
> TG92ZWx5IHNwYW0hIExvdmVseSBzcGFtISI=


Then decode and exec:

In [1]: import base64

In [2]: %cpaste
Pasting code; enter '--' alone on the line to stop.
:code="""> ZGVmIHNwYW0oKToNCiAgICBwcmludCAiU3BhbSEg
:> TG92ZWx5IHNwYW0hIExvdmVseSBzcGFtISI="""
:--

In [3]: print base64.b64decode(code)
def spam():
print "Spam! Lovely spam! Lovely spam!"

In [4]: exec(base64.b64decode(code))

In [5]: spam()
Spam! Lovely spam! Lovely spam!

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


Re: A problem about ipython

2011-04-01 Thread eryksun ()
On Thursday, March 31, 2011 9:48:27 PM UTC-4, Vincent Ren wrote:
>
> when I run this(from Programming Python 3rd) in ipython, I'll
> get a NameError:
>  
>
> /usr/lib/python2.6/profile.pyc in runctx(self, cmd, globals, locals)
> 460 sys.setprofile(self.dispatcher)
> 461 try:
> --> 462 exec cmd in globals, locals
> 463 finally:
> 464 sys.setprofile(None)
> 
> /usr/lib/pymodules/python2.6/IPython/FakeModule.pyc in ()
> 
> NameError: name 'timer' is not defined
> 
> What's going wrong here?

Try this instead:

profile.runctx('timer.test(100, settime.setops, set.Set)', globals=globals(), 
locals=locals())
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyVISA

2011-04-01 Thread eryksun ()
On Friday, April 1, 2011 11:29:10 AM UTC-4, Manatee wrote:
> I have unpacked the PyVISA files into the Python/lib/site-packages dir
> and from the IDLE GUI I get and error
> 
> import visa
> 
> Traceback (most recent call last):
>   File "
> ", line 1, in 
> import visa
> ImportError: No module named visa
>  
> 
> There must be more to just putting the files in the correct directory.
> Need help configuring PyVISA to work.
> My ultimate goal is to control electronic instruments with Python
> through visa.

If you're on Windows, they have a win32 executable installer available on 
SourceForge. Otherwise you can just easy_install the egg.

http://sourceforge.net/projects/pyvisa/files/PyVISA/1.3/

You'll also need a VISA library (.dll or .so file) for your GPIB adapter.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyVISA

2011-04-01 Thread eryksun ()
On Friday, April 1, 2011 3:40:23 PM UTC-4, Manatee wrote:
> 
> Well, ok, I'll try some of that. But I am running window 7, not Linux.
> The "sudo" command sounds like Linux.

Again, there's a win32 exe installer available here:

http://sourceforge.net/projects/pyvisa/files/PyVISA/1.3/PyVISA-1.3.win32.exe/download

If your account is a standard user and the installer doesn't load a UAC prompt, 
you'll probably have to right-click it and choose to "Run as administrator".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie question

2011-04-01 Thread eryksun ()
Regarding the format of your post, please use plain text only. 

On Friday, April 1, 2011 3:52:24 PM UTC-4, Karl wrote:
>
> aList = [0, 1, 2, 3, 4] 
> bList = [2*i for i in aList]
> sum = 0
> for j in bList:
> sum = sum + bList[j]
> print j
> 
> 0
> 2
> 4
> 
> IndexError: list index out of range
> 
> Why is j in the second run 2 and not 1 in the for-loop?? I think 
> j is a control variable with 0, 1, 2, 3, ...

The for loop yields the elements of bList, not their index (it's like a foreach 
statement in other languages). Here's the full printout:

In [1]: aList = [0, 1, 2, 3, 4]
In [2]: bList = [2*i for i in aList]
In [3]: for j in bList:
   ...: print j

0
2
4
6
8

Since bList is 5 elements long, bList[6] is indeed "out of range". Here's a 
more direct way to accomplish this task:

In [4]: b_sum = sum(bList)
In [5]: b_sum
Out[5]: 20

Note that I'm not overwriting the name of the built-in function 'sum'. Try to 
avoid doing that. It's pretty simple to type the name in the interpreter and 
see if it's already bound to something you might want to keep around. 

Here's one way to get the index in a loop:

In [6]: b_sum = 0
In [7]: for j, b in enumerate(bList):
   ...: b_sum += b
   ...: print j

0
1
2
3
4

In [9]: b_sum
Out[9]: 20

The enumerate function returns an iterable that's a tuple of the index and 
item. A for loop iterates over an iterable object such as a sequence.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Alphabetics respect to a given locale

2011-04-01 Thread eryksun ()
On Friday, April 1, 2011 4:55:42 PM UTC-4, candide wrote:
>
> How to retrieve the list of all characters defined as alphabetic for the 
> current locale  ?

Give this a shot:

In [1]: import string

In [2]: print string.letters
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

In [3]: import locale

In [4]: locale.getlocale()
Out[4]: (None, None)

In [5]: locale.setlocale(locale.LC_ALL, 'English_Great Britain')
Out[5]: 'English_United Kingdom.1252'

In [6]: print string.letters
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
ƒSOZsozYªµºÄÅÆÇEÉEEDÑÖOUUUÜY_
ßàáâaäåæçèéêëìíîïdñòóôoöoùúûüy_ÿ

The strings for locales are different for POSIX vs Windows systems. 

http://docs.python.org/library/locale.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyVISA

2011-04-01 Thread eryksun ()
On Friday, April 1, 2011 10:24:58 PM UTC-4, Manatee wrote:
>
> VisaIOError: VI_ERROR_INTF_NUM_NCONFIG: The interface type is valid
> but the specified interface number is not configured.
> 
> My instrument is on GPIB 5 and I can do a *IDN? with another program
> and get a response. So I must still have something not configured
> correct. Getting closer though :)

If you have more than one board, you might need a more complete resource name, 
such as 'GPIB1::5' for board 1. You can query the available instruments using 
get_instruments_list(), which calls the VISA library's viFindRsrc function.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending keystrokes to Windows exe programs

2011-04-02 Thread eryksun ()
On Saturday, April 2, 2011 12:48:44 PM UTC-4, Alex van der Spek wrote:
>
> I can start a windows program on Vista with:
> 
> >>> import subprocess
> >>> dva=subprocess.Popen(DVAname,stdin=subprocess.PIPE)
> 
> Unfortunately sending keystrokes with communicate() does not appear to work:
> 
> >>> dva.communicate('F2')

Is this a GUI process? If so, it probably doesn't have stdin unless it also has 
a console (possibly hidden). 

To send keys to a GUI Window, you can use win32com (from the pywin32 
extensions) to execute WScript commands:

import time
import win32com
import win32com.client

shell = win32com.client.Dispatch('WScript.Shell')
shell.Run('notepad')
time.sleep(0.1)
shell.AppActivate('notepad')
shell.SendKeys("Hello World", 0)
shell.SendKeys("{Enter}", 0)
shell.SendKeys("{F5}", 0)   # F5 prints the time/date

Documentation for SendKeys:
http://msdn.microsoft.com/en-us/library/8c6yea83

SendKeys only sends to the active Window. If you need to send keys to a 
background Window, you can use win32 messaging. 

If you load the process with CreateProcess, you'll get the the main thread ID 
(tid) as well as the process ID (pid). Then you can enumerate handles for the 
thread's windows into a dictionary keyed by the window classes (or window class 
+ window title). 

Here's the example with notepad, but this time using win32 messaging:

import time
import win32con
import win32api
import win32gui
import win32process

_, cmd = win32api.FindExecutable('notepad')

_, _, pid, tid = win32process.CreateProcess(
None,# name
cmd, # command line
None,# process attributes
None,# thread attributes
0,   # inheritance flag
0,   # creation flag
None,# new environment
None,# current directory
win32process.STARTUPINFO ())

# wcallb is callback for EnumThreadWindows and 
# EnumChildWindows. It populates the dict 'handle' with 
# references by class name for each window and child 
# window of the given thread ID.

def wcallb(hwnd, handle):
handle[win32gui.GetClassName(hwnd)] = hwnd
win32gui.EnumChildWindows(hwnd, wcallb, handle)
return True

handle = {}
while not handle:   # loop until the window is loaded
time.sleep(0.1)
win32gui.EnumThreadWindows(tid, wcallb, handle)

# Sending normal characters is a WM_CHAR message. 
# Function keys are sent as WM_KEYDOWN and WM_KEYUP 
# messages using the virtual keys defined in win32con, 
# such as VK_F5 for the f5 key.
#
# Notepad has a separate 'Edit' window to which you can 
# write, but function keys are processed by the main 
# window. 

for c in "Hello World\n":
win32api.PostMessage(
handle['Edit'], 
win32con.WM_CHAR, 
ord(c), 
0)

win32api.PostMessage(
handle['Notepad'], 
win32con.WM_KEYDOWN, 
win32con.VK_F5, 
0)
win32api.PostMessage(
handle['Notepad'], 
win32con.WM_KEYUP, 
win32con.VK_F5, 
0)

# SendMessage waits for a response, but PostMessage 
# queues the message and returns immediately

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


Re: TypeError: iterable argument required

2011-04-03 Thread eryksun ()
On Saturday, April 2, 2011 12:26:18 PM UTC-4, Νικόλαος Κούρας wrote:
> Hello, after inserting this line if "@" in mail and comment not in
> ("Σχολιάστε ή ρωτήστε με σχετικά", ""):
> 
> iam getting the following error which i dont understand
> 
> **
> 163 # insert guest comments into database if form was
> submitted
>   164 if "@" in mail and comment not in ("Σχολιάστε ή ρωτήστε
> με σχετικά", ""):
>   165 try:
>   166 cursor.execute( '''INSERT INTO
> users(mail, comment) VALUES(%s, %s)''', (mail, comment) )
> mail = None, comment = None
> 
> TypeError: iterable argument required
>   args = ('iterable argument required',)

Here's how I parse what you've written so far:

INVALID_COMMENTS = ("Σχολιάστε ή ρωτήστεμε σχετικά", "")
SQL_COMMENT_FORM = "INSERT INTO users(mail, comment) VALUES(%s, %s)"

# insert guest comments into database if form was submitted
mail = form.getvalue('mail')
comment = form.getvalue('comment')
if "@" in mail and comment not in INVALID_COMMENTS:
try:
cursor.execute(SQL_COMMENT_FORM % (mail, comment))
except MySQLdb.Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
else:
mail = comment = None
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError: iterable argument required

2011-04-04 Thread eryksun ()
On Monday, April 4, 2011 9:40:33 AM UTC-4, Νικόλαος Κούρας wrote:

In one of your messages you wrote the following:

> cursor.execute( '''INSERT INTO users(mail, comment) VALUES(%s,
> %s)''', (mail, comment) )
> except MySQLdb.Error:
> print ( "Error %d: %s" % (e.args[0], e.args[1]) )

Is this a typo in your message or the actual code? If 'e' is unassigned you 
should be getting a NameError. The standard Python2 syntax (before version 2.6) 
is the following:

except MySQLdb.Error, e:
print("Error %d: %s" % (e.args[0], e.args[1]))

You also wrote:

> mail = None, comment = None

This should cause a SyntaxError because it's trying to assign None = None. 
That's assuming it's on line 167, after the cursor.execute(...) on line 166.

> Whats was the problem as i have written it?
> Your solution is the same as mine except the fact that you assign
> values and statements into variables.

I was just rewriting it to make sure I was parsing it right.

> I tried it but iam getting an Internal Server Error.

Yes, I made a mistake. It should have been `cursor.execute(SQL_COMMENT_FORM, 
(mail, comment))`, using a comma instead of a '%' to have it generate SQL 
string literals from the tuple.

> Also i noticed that if i append a query string in the end of a url
> with the varibble mail attached like
> 
> http://superhost.gr/hosting.html?mail=test
> 
> then page hosting.html does load without any problem.
> 
> If i remove the query string from the ned of the URL then i'am getting
> the error message i posted.
> 
> So its not that the if condition is wrong but something happens with
> the form variable 'mail' .

Insert a test to print out the type and value of 'mail' for various inputs. 

Regarding the message itself, on Python 2.7.1, I get the following TypeError 
message if I try iterate None:

TypeError: argument of type 'NoneType' is not iterable

Python 2.5.2 on "http://shell.appspot.com"; yields the same error. Version 2.5 
improved the error messages to include the type of the object (see issue 
1507676). The message "iterable argument required" looks like an older version 
of CPython.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 3D subfigures in matplotlib?

2011-04-05 Thread eryksun ()
On Tuesday, April 5, 2011 6:17:34 AM UTC-4, MATLABdude wrote:
> How can I make maybe 3D subfigures with Python and matplotlib?
> 
> I found the following example (http://matplotlib.sourceforge.net/
> examples/mplot3d/subplot3d_demo.html), but it doesn't work.
> 
> [snip]
>
> I am using Python 2.6.6., and matplotlib 0.99.3.
> 
> How can I make several subfigures in 3D?

The demo works fine for me using Matplotlib 1.0.1.  

Here's a version that should work on your older version of Matplotlib. Instead 
of adding a subplot with projection='3d', it adds a normal subplot and gets the 
bounding rectangle. The rectangle is used to configure the Axes3D instance:

from mpl_toolkits.mplot3d.axes3d import Axes3D
import matplotlib.pyplot as plt

# imports specific to the plots in this example
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d.axes3d import get_test_data

fig = plt.figure(figsize=(9.5,5.0))

# First subplot
rect = fig.add_subplot(1, 2, 1).get_position()
ax = Axes3D(fig, rect)
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, 
cmap=cm.jet, linewidth=0, antialiased=False)
ax.set_zlim3d(-1.01, 1.01)

fig.colorbar(surf, shrink=0.5, aspect=10)

# Second subplot
rect = fig.add_subplot(1, 2, 2).get_position()
ax = Axes3D(fig, rect)
X, Y, Z = get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

plt.show()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError: iterable argument required

2011-04-06 Thread eryksun ()
On Wednesday, April 6, 2011 6:06:06 AM UTC-4, Νικόλαος Κούρας wrote:
>
> The trouble was in `if "@" in mail` .
> You can only test somthing `in` something else if the second thing is
> iterable and None isnt.
> 
> So i made the code look like this:
> 
> [code]
> if ( mail is not None and '@' in mail ) and comment not in ("Ρωτήστε
> με σχετικά...", "", None):
> [/code]
> 
> Now it works like i wanted but i want to ask you if i wrote it
> correctly, especially when i check against `""` and None

You can also use an empty string as the default value when getting the field 
value, which would simplify your test by eliminating None as a possibility. The 
particulars will depend on your framework. For example, cgi.FieldStorage has a 
getfirst method that takes a default value as the 2nd parameter. 

Also, a simple OR statement can eliminate the None. For example: mail = mail or 
''. Since None is False, the statement returns the right-hand operand, which is 
an empty string ''.

> And please explain to me the difference betweeen an empty string `""`
> and None. An empty string is still a string with zero characters 
> within?

Yes, an empty string is still of type 'str'. None is Python's null value of 
type 'NoneType'. Its boolean value is False, and it does nothing special and 
cannot be subclassed. When a function doesn't explicitly return a value, it 
implicitly returns None.

> > Yes, I made a mistake. It should have been 
> > `cursor.execute(SQL_COMMENT_FORM, (mail, comment))`, using a comma instead 
> > of a '%' to have it generate SQL string literals from the tuple.
> 
> What do you mean by  "to have it generate SQL string literals from the
> tuple." Please explain

Here's an amusing warning from the Psycopg (PostgreSQL) docs:

"Warning: Never, never, NEVER use Python string concatenation (+) or string 
parameters interpolation (%) to pass variables to a SQL query string. Not even 
at gunpoint."

The line I wrote not only didn't properly quote or escape the data values, but 
it probably also broke the protection from a SQL injection attack. Always list 
the data in the 2nd parameter as a tuple.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError: iterable argument required

2011-04-06 Thread eryksun ()
On Wednesday, April 6, 2011 11:41:24 AM UTC-4, Νικόλαος Κούρας wrote:
> On 6 Απρ, 16:54, "eryksun ()"  wrote:
> 
> > You can also use an empty string as the default value when getting the 
> > field value
> 
> Please provide me an example.

import cgi
form = cgi.FieldStorage()
user = form.getfirst("user", "")

Here's the relevant section of the cgi docs:

http://docs.python.org/library/cgi.html#higher-level-interface

But it depends on your set up. Are you using mod_wsgi, mod_python, etc? Will 
you be using a framework such as Django or web2py?

> Can you please also provide an example of what happens if i use the
> special formatting identidier `%` instead of a comma?

All of the formatting for adding extra quotes and escaping special characters 
can be done with normal string formatting (which I neglected to do). But that's 
not counting the most important reason to let the database handle the 
operation: the potential for an attacker to inject SQL commands into form 
values (e.g., to drop all of your tables). So let the database handle 
formatting the strings and escaping any SQL statements contained therein.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError: iterable argument required

2011-04-06 Thread eryksun ()
On Wednesday, April 6, 2011 11:57:32 AM UTC-4, Νικόλαος Κούρας wrote:
> >>> mail = None
> >>> mail = mail or 7
> >>> mail
> 7

Quote:
The expression ``x or y`` first evaluates *x*; if *x* is 
true, its value is returned; otherwise, *y* is evaluated 
and the resulting value is returned.

Since 'mail is None' and None evaluates to False, the operation returns the 
right-hand operand, 7.

> >>> mail = None
> >>> mail = 7 or mail
> >>> mail
> 7
> 
> Here no matter the order iam writing the comparison it always return
> the number.

In this case the number 7 evaluates to True.

> why not the same here?
> 
> >>> mail = None
> >>> mail = mail or ''
> >>> mail
> ''
> >>> mail = None
> >>> mail = '' or mail
> >>> mail
> >>>
> 
> Why the or operator behaves differently with numbers than from
> strings?

It's behaving the same. You're overlooking the fact that the empty string is 
False:

In [1]: bool('')
Out[1]: False

Length zero sequences are normally False, but you can override this in a 
subclass by implementing the __nonzero__ method:

In [2]: class mystr(str):
   ...: def __nonzero__(self):
   ...: return True

In [3]: bool(mystr(''))
Out[3]: True

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


Re: TypeError: iterable argument required

2011-04-06 Thread eryksun ()
On Wednesday, April 6, 2011 3:21:42 PM UTC-4, Νικόλαος Κούρας wrote:
> On 6 Απρ, 19:58, "eryksun ()"  wrote:
> 
> > The expression ``x or y`` first evaluates *x*; if *x* is
> > true, its value is returned; otherwise, *y* is evaluated
> > and the resulting value is returned.
> 
> I doesnt matter if *y* is True or False before its value is returned?
> *y*'s value returned no matter if its true or false?

In general *y* is an expression that gets evaluated down to some object that 
gets returned:

In [1]: type(False or (3-3))
Out[1]: 

In this case the int equals 0, which is False in a boolean context:

In [2]: bool(False or (3-3))
Out[2]: False

> >Since 'mail is None' and None evaluates to False
> 
> What does the expression "None evaluates to false"  mean in simpler
> words? Every expression is evaluated at the end as True or False?

It means bool(None) is False. In (x or y), the expression y will only be 
evaluated if bool(x) is False. If bool(x) evaluates to True, then the operation 
short circuits to return x without evaluating y.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Spam on the mailing list

2011-04-07 Thread eryksun ()
On Wednesday, April 6, 2011 11:03:31 AM UTC-4, Chris Angelico wrote:
> I don't know whether it's ironic or in some way Pythonesque, but this
> is the only mailing list that I've seen significant amounts of spam
> on...

My impression is that there is much more spam on comp.lang.python than on the 
archives of Python-list, so it seems the gateway is catching a lot of it:

http://mail.python.org/pipermail/python-list/2011-April/date.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python benefits over Cobra

2011-04-07 Thread eryksun ()
On Thursday, April 7, 2011 2:43:09 AM UTC-4, Steven D'Aprano wrote:
> On Thu, 07 Apr 2011 00:25:46 -0500, harrismh777 wrote:
> 
> > The gnu suite of tools and the linux kernel were the keys to unlocking
> > Microsoft lock-in... brilliant technologies for innovation and freedom.
> 
> I used to believe this too, but then I found I was relying on Linux and 
> GNU software so much that I couldn't afford to move away from Linux and 
> GNU. Damn lock-in.

Once you commit yourself to a free and open community, there's nothing locking 
you in other than your personal level of commitment and loyalty, and how much 
you stand to lose by simply walking away. Free software doesn't eliminate the 
real constraints of the real world.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python and DDE

2011-04-07 Thread eryksun ()
On Wednesday, April 6, 2011 3:37:34 PM UTC-4, Robert Upton wrote:
> Dear Pythoners,
> 
> I am attempting to get the DDE module to import into Python and am
> having some trouble.  I have downloaded and installed the pywin32
> extensions for Python 2.6, which is the version of python I am running
> on Windows.  When I attempt to import the DDE module, as follows,
> 
> import win32ui
> import dde
> 
> I get:
> 
> " Traceback (most recent call last):
>  File "", in line 1, in 
> ImportError: This must be an MFC application - try loading with
> win32ui first"

For what it's worth, importing dde works from within the MFC demo application 
Pythonwin.exe, located in site-packages\pythonwin, which is where you'll also 
find the MFC DLLs and manifest, along with win32ui.pyd and dde.pyd.

You'll probably find more help on the python-win32 mailing list:

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

Or file a bug report on the SourceForge bug tracker for pywin32:

http://sourceforge.net/tracker/?group_id=78018&atid=551954
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fighting game made with python

2011-04-07 Thread eryksun ()
On Thursday, April 7, 2011 9:51:05 AM UTC-4, neil harper wrote:
> is there any fighting games(street fighter, mortal kombat, etc) made in
> python?

I found a 2D pygame (SDL) fighter called "fighter framework", which is 
basically karate sparring. 

http://www.pygame.org/project-fighter+framework-1550-3173.html

run.py loads the game. I had to copy a font from ./karate/Karate.ttf to 
./lastninja.ttf in order to get past an error.

The game only has basic graphics and sound, like something from an Atari 2600. 
The available moves for the fighter are low, medium, and high kicks, punch, 
block, and jump, plus combos for a roundhouse kick, jump kick, front flip, and 
back flip. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need Help w. PIP!

2015-09-04 Thread eryksun
On Fri, Sep 4, 2015 at 11:35 AM, Steve Burrus  wrote:
>
> "C:\Users\SteveB>py -m pip
> Job information querying failed

You're using build 10074, an old build of Windows 10 that had a buggy
Job object API. This was fixed in build 10159:

http://bugs.python.org/issue24127

FYI, the py launcher runs Python in a job that's configured to kill
Python if the the launcher gets killed. This is necessary if py.exe is
started by a console shell such as cmd.exe or powershell.exe. The
shell waits on py.exe, not python.exe. If the job didn't kill
python.exe, then Python would compete with the shell for console
input.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need Help w. PIP!

2015-09-04 Thread eryksun
On Fri, Sep 4, 2015 at 5:10 PM, Steve Burrus  wrote:
> so what is my hopefully sinple solution anyway? i do n't see myself anytime
> soon getting off of Build 10074 of Win 10.

Script wrappers such as pip.exe are simple versions of the py launcher
that execute an embedded script. For example, here's the script
embedded in pip.exe on my system:

#!"C:\Program Files\Python34\python.exe"
# -*- coding: utf-8 -*-
import re
import sys
from pip import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

So, as with py.exe, the child python.exe process has to be assigned to
a job object. That won't work as long as you're using build 10074 of
Windows 10. But you can still run pip using `python -m pip`.

Also, since the py launcher doesn't work on your system, you'll have
to ensure that scripts are associated with python.exe / pythonw.exe
instead of py.exe / pyw.exe. If you installed for all users you can do
that in an elevated command prompt (cmd.exe) using the ftype command:

ftype Python.File="C:\Python34\python.exe" "%1" %*
ftype Python.NoConFile="C:\Python34\pythonw.exe" "%1" %*
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need Help w. PIP!

2015-09-04 Thread eryksun
On Fri, Sep 4, 2015 at 10:07 PM, Steve Burrus  wrote:
> well everyone there must be sometjhing about upgrading Python up to version 
> 3.5 rc
> because everything works just fine now, beyond my wildest dreams! I am even 
> able
> now to type in "pip" and get the usual info. on it and I have connected to 
> the Django
> server and configured the admin so thanx for eveyone who tried to help me.

I just tested running pip.exe on a VM with Windows 10, build 10074. It
surprised me that it worked, so I checked the source code of Vinay's
[simple launcher][1]. Apparently the simpler launcher merely asserts
that creating the job object succeeds, so it will only fail in a debug
build. (See run_child in launcher.c). Depending on the nature of the
script it could be confusing if there's no job object and the launcher
gets killed. This leaves python.exe and the shell competing for
console input. That said, it shouldn't really matter for a simple
program such as pip.

But the full py.exe launcher used by Python always fails if creating
the job object doesn't succeed. (Oddly, it doesn't fail if *assigning*
the child to the job fails; I guess because Windows 7 lacks support
for job hierarchies, but still, it makes no sense to care about
creating something that you don't care about using.) So for build
10074 of Windows 10, ensure the filetypes for .py and .pyw scripts
(installed as Python.File and Python.NoConFile) run python.exe and
pythonw.exe instead of py.exe and pyw.exe. Otherwise you won't be able
to run scripts directly at the command prompt or by double clicking
them in Explorer.

[1]: https://bitbucket.org/vinay.sajip/simple_launcher/src
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: .bat file trouble.

2015-09-20 Thread eryksun
On 9/18/15, Christian Gollwitzer  wrote:
> Am 18.09.15 um 11:06 schrieb bobert...@googlemail.com:
>
>> We originally thought that it was because it was missing the files:
>> process_init.py and process_global_variables.py however they are
>> right there in the same directory.
>
> Concerning that, windows usually runs a .bat file in the directory where
> it is situated, so putting the python fies there /should/ work, but you
> can also set this using the right-click menu (execute in...), if you
> make a link to the desktop.

It's fragile to depend on being started by Explorer or a shell link.
For example, if the batch is run from an existing cmd shell, then it
runs with cmd's current working directory. Or another process could
run the batch file with a different working directory. If you actually
want the batch to find the scripts relative to its own directory, then
it should use the %0 environment variable, which references the
fully-qualified path of the batch file. Getting just the directory,
i.e. its [d]rive and [p]ath, is expressed as %~dp0. Note that this
path includes a trailing backslash.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Failed to upgrade pip in fresh 2.7 install

2015-09-26 Thread eryksun
On 9/26/15, Zachary Ware  wrote:
> On Sat, Sep 26, 2015 at 1:13 PM,   wrote:
>> After a fresh install of Python 2.7 32-bit and 64-bit, upgrading pip
>> using pip fails. Am I doing this incorrectly? Any suggestions?
>
> This is a limitation of Windows: you can't replace the executable that
> you're currently running.

A memory-mapped file can be renamed, so replacing the executable is
doable. The new name has to be on the same volume. That's doable. The
problem is that Windows doesn't allow deleting the file. Maybe pip
could spawn a clean-up script to which it pipes the list of renamed
files. The script waits for pip to exit and then tries to remove the
files.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Beginning question #compilersetup, #windows7, #vs2010

2015-09-27 Thread eryksun
On Sat, Sep 26, 2015 at 4:40 PM, Jeff VanderDoes
 wrote:
>
> I'm fairly new to Python and was excited to start playing with it until I
> ran into need to compile some extensions under windows 7 64 bit.  I've done
> some searching but after more hours than I care to count being unsuccessful
> setting up MS visual studio (2015, 2012, and 2010) with service packs and
> SDKs I can tell I'm spinning my wheels and going nowhere fast.

For 3.5 you should be able to just install Visual Studio 2015
Community Edition. This is the current release of Visual Studio, so if
you encounter problems, at least finding help won't be one of them.

2.7 is built with VS 2008, which is no longer supported. But, thanks
to Steve Dower, Microsoft distributes an unsupported version for
building Python 2.7 extension modules [1].

3.4 is built with the fairly old VS 2010, for which the free Express
edition is no longer available. But you should be able to configure a
command-line build environment. Install the Windows SDK 7.1 [2] and
the VC++ 2010 SP1 Compiler Update [3]. Then run the Windows SDK 7.1
Command Prompt [4], and enter

SetEnv /Release /x64

If you plan to do native debugging outside of Visual Studio, install
the Debugging Tools for Windows [5] when installing the SDK. You can
download public debug symbols for operating system components using
Microsoft's symbol server. To do this, create a "C:\Symbols"
directory, and set the following environment variable:


_NT_SYMBOL_PATH=symsrv*symsrv.dll*C:\Symbols*http://msdl.microsoft.com/download/symbols

Additionally you'll need Python's debug symbols, such as for 64-bit
3.4.3 [6]. I typically copy a PDB next to its related DLL/EXE. This is
what 3.5's installer does when you select the option to install debug
symbols (nice job, Steve!). You can also unzip the PDBs to a directory
that's in _NT_SYMBOL_PATH, or update the symbol path dynamically using
.sympath+ and .reload [7].

[1]: https://www.microsoft.com/en-us/download/details.aspx?id=44266
[2]: https://www.microsoft.com/en-us/download/details.aspx?id=8279
[3]: https://www.microsoft.com/en-us/download/details.aspx?id=4422
[4]: https://msdn.microsoft.com/en-us/library/ff660764
[5]: https://msdn.microsoft.com/en-us/library/ff551063
[6]: https://www.python.org/ftp/python/3.4.3/python-3.4.3.amd64-pdb.zip
[7]: https://msdn.microsoft.com/en-us/library/ff565407
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error code 0x80070570

2015-09-30 Thread eryksun
On 9/30/15, Rusiri Jayalath  wrote:
> Error code 0x80070570 appears when installing python 3.5.0 (32-bit) setup
> for my windows 8.1 system. Please help me to solve this problem.

0x80070570 is ERROR_FILE_CORRUPT: the file or directory is corrupted
and unreadable. It could be an intermittent problem with your internet
connection when downloading the required packages. Try using the
non-web installer that already includes most of them [1].

I haven't seen this particular error reported on the issue tracker, so
you might want to submit a new issue. Include a zip of the
installation logs for the most recent failed attempt. They're in your
%TEMP% directory with names like "Python 3.5.0 (32-bit)_*.log".

FYI, the severity/facility code 0x8007 determines that 0x0570 is a
Win32 error code. See HRESULT [2] for an overview of how to decode
HRESULT and NTSTATUS [3] codes. See Win32 Error Codes [4] or System
Error Codes [5] for a list of Win32 errors.

[1]: https://www.python.org/ftp/python/3.5.0/python-3.5.0.exe
[2]: https://msdn.microsoft.com/en-us/library/cc231198
[3]: https://msdn.microsoft.com/en-us/library/cc231200
[4]: https://msdn.microsoft.com/en-us/library/cc231199
[5]: https://msdn.microsoft.com/en-us/library/ms681381
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installation problem

2015-10-08 Thread eryksun
On 10/8/15, Tim Golden  wrote:
>
> What happens if you do [Windows] + R (ie Start > Run), enter "python"
> and click OK?

The new installer for 3.5 doesn't create an "App Paths" key for
"python.exe" like the old installer used to do (see the old
Tools/msi/msi.py). Without that, unless python.exe is in the search
PATH, "Win+R -> python" and running "start python" in the command
prompt won't work. You can of course add the key manually as the
default value for
"[HKLM|HKCU]\Software\Microsoft\Windows\CurrentVersion\App
Paths\python.exe". IMO, this makes it the 'system' Python.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installation problem

2015-10-08 Thread eryksun
On 10/8/15, Oscar Benjamin  wrote:
> On Thu, 8 Oct 2015 22:30 eryksun  wrote:
>
> The new installer for 3.5 doesn't create an "App Paths" key for
> "python.exe" like the old installer used to do (see the old
> Tools/msi/msi.py). Without that, unless python.exe is in the search
> PATH, "Win+R -> python" and running "start python" in the command
> prompt won't work. You can of course add the key manually as the
> default value for
> "[HKLM|HKCU]\Software\Microsoft\Windows\CurrentVersion\App
> Paths\python.exe". IMO, this makes it the 'system' Python.
>
> That's interesting. Any idea why it changed?

Probably it's just an oversight. I doubt Steve Dower would have
purposefully decided to not set the "App Paths" key.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installed 3.5.0 successfully on Windows 10, but where is DDLs, Doc, Lib, etc?

2015-10-14 Thread eryksun
On 10/14/15, Zachary Ware  wrote:
>
> You can find where Python is installed using Python itself: try `py
> -3.5 -c "import sys, os;os.system('explorer ' + sys.prefix)"` at the
> Command Prompt,

Here's a slightly simpler way to open the folder:

py -3.5 -c "import os, sys; os.startfile(sys.prefix)"

> By the way, C:\Users\(your name)\AppData does exist, but is hidden by
> default.  It will tab-complete, though; at Command Prompt do `dir
> C:\Users\(your name)\App`.

You can list all files and directories using the /a switch of cmd's
dir command, and just directories (including hidden ones) using /ad,
e.g.

dir /ad %userprofile%
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: help(string) commands not working on pyton 3.5

2015-10-15 Thread eryksun
On 10/14/15, Prasad Joshi  wrote:
> Hi,
>
> I have installed the "Windows x86-64 executable
> installer"
> on my desktop but I cannot get help ( ) or help (string) command working.
> What could be an issue?
>

It may help to know which version of Windows you're using -- XP,
Vista, 7, 8, or 10?

help() may work after switching to a plain pager:

import pydoc
pydoc.pager = pydoc.plainpager

in which case, check whether the following prints "test":

pydoc.tempfilepager('test', 'more <')

If not, please reply with the traceback or error message, if any.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installed 3.5.0 successfully on Windows 10, but where is DDLs, Doc, Lib, etc?

2015-10-15 Thread eryksun
On 10/15/15, Gisle Vanem  wrote:
>
> This is non-sense. I do have Python2 + 3 both on PATH (but both 32-bits).
> Not sure if PyLauncher looks for 64-bit registry entries only.

Running "py -3" doesn't use PATH. The launcher only uses PATH when
evaluating "/usr/bin/env" in a virtual shebang, e.g. "#!/usr/bin/env
python".

The version of py.exe distributed with Python 3 is a 32-bit
application, so when the debug output says it's looking in the
"native" registry, it's referring to the WOW64 redirected registry
path, i.e. "HKLM\Software\Wow6432Node\Python". If py.exe can't find
your installation of Python 3 in the registry, then the installation
is broken or non-standard. Try to repair or reinstall.

If python.exe can be found on PATH, then you can use "where python",
or open all found folders using a cmd shell for loop:

for /f %f in ('where python') do @start "" "%~dpf"

> But is there a way for py.exe to use '%USERPROFILE%\Local\py.ini' only?
> I failed to find any good documentation on it's format.

The launcher looks for py.ini in "%LOCALAPPDATA%", which is
"%USERPROFILE%\AppData\Local".

The ini file allows setting defaults such as setting "python3=3.5-32"
in the [defaults] section. This is documented here:

https://docs.python.org/3/using/windows.html#customization-via-ini-files

The [commands] section is undocumented, but this section only creates
convenience commands for use with shebangs. You can always use the
absolute path to an executable in the shebang if you need to associate
a script with some other Python implementation, such as PyPy.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installed 3.5.0 successfully on Windows 10, but where is DDLs, Doc, Lib, etc?

2015-10-15 Thread eryksun
On 10/15/15, Gisle Vanem  wrote:
>
> Thanks for the detailed info. I fixed some paths under:
>HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.5-32
>
> Now my Python3.5 almost works. But something is wrong with the
> Python3 sys.prefix:
>c:> py -2 -c "import os, sys; print(sys.prefix)"
>F:\ProgramFiler\Python27
>
>c:> py -3 -c "import os, sys; print(sys.prefix)"
>F:\ProgramFiler
>
> Where should I look for the reason for this?

The directory for 32-bit program files is localized to "Programfiler
(x86)" in Norwegian. Is Python 3.5 installed in  "F:\Programfiler
(x86)\Python 3.5"? What's the value of sys.executable?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: help(string) commands not working on pyton 3.5

2015-10-16 Thread eryksun
On 10/16/15, Prasad Joshi  wrote:
>
> I am using windows 10.
>
 import math


 help (math)
> 'more' is not recognized as an internal or external command,
> operable program or batch file.

What do you get for the following?

import os
windir = os.environ['SystemRoot']
more_path = os.path.join(windir, 'System32', 'more.com')
print(os.path.exists(more_path))

If more.com doesn't exist, your Windows installation needs to be
repaired. If it does exist, then probably PATHEXT is missing .COM.
Ensure it's listed in the output of the following:

import os
print(os.environ['PATHEXT'])
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A high-level cross-platform API for terminal/console access

2015-10-21 Thread eryksun
On 10/21/15, Peter Brittain  wrote:
>>
>> Did you try https://pypi.python.org/pypi/UniCurses ?
>>
>
> Yes - it failed to install with pip and also looked like a dead project when
> I followed the project home page URL.
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Also check out the curses module that's available on Christoph Gohlke's site:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What meaning is of '#!python'?

2015-11-14 Thread eryksun
On Sat, Nov 14, 2015 at 8:26 PM, Zachary Ware
 wrote:
>
> "#!python" is a valid shebang for the Python Launcher for Windows.
> It's also a not-too-terrible placeholder for a Unix shebang meaning
> "whichever Python you want it to be".  The better choice for use with
> both platforms would be "#!/usr/bin/env python", though.

The "/usr/bin/env python" virtual command searches the directories in
PATH, trying each file extension from PATHEXT such as "python.COM",
"python.EXE", and so on. You can also search for other programs such
as "pypy". Note that qualifying "python" (but not other names) as
"python2" or "python3.5" makes the launcher use the registry instead
of searching PATH.

"#!/usr/bin/python" may be better in some cases. This defaults to the
latest installed version of 2.x (or 3.x if no 2.x is installed) that's
configured in the Windows registry. Or specify "python2" or "python3"
to use the latest 2.x or 3.x. These commands can be configured to use
a particular major[.minor[-32]] version via the environment variables
PY_PYTHON, PY_PYTHON2, and PY_PYTHON3. Or you can configure them
instead by setting the "python", "python2" and "python3" keys in the
[defaults] section of the configuration file "%LOCALAPPDATA%\py.ini".
Note that the environment variable overrides the corresponding py.ini
key.

When portability isn't a concern you can use a Windows path in the
shebang such as "#!C:\pypy40\pypy.exe".

https://docs.python.org/3/using/windows.html#shebang-lines
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Launcher, and ftype Python.File

2015-11-18 Thread eryksun
On Wed, Nov 18, 2015 at 8:45 AM, Christian Ullrich  wrote:
> Apparently %L is always the long file name, and %1 is the short name unless
> the shell can prove that the executable can handle long names.

Specifically old Win16 programs (identified by the file header) aren't
aware of long filenames. In the case of these old programs, the shell
calls GetShortPathName when replacing %0 or %1, but not when replacing
%L.

For Win32 programs %0, %1, and %L are all the same:

C:\Temp>ftype Python.File
Python.File="C:\Windows\py.exe" "%0" "%1" "%L" %*

C:\Temp>longfilename.py
sys.argv:
C:\Temp\longfilename.py
C:\Temp\longfilename.py
C:\Temp\longfilename.py

Short Pathname:
C:\Temp\LONGFI~1.PY
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: non-blocking getkey?

2015-11-18 Thread eryksun
On Wed, Nov 18, 2015 at 3:14 AM, Christian Gollwitzer  wrote:
> The standard terminal on Windows is very ugly, can't resize the width, and
> pasting works only if you right-click -> paste.

The console UI is improved in Windows 10:
http://blogs.windows.com/buildingapps/2014/10/07/console-improvements-in-the-windows-10-technical-preview
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: non-blocking getkey?

2015-11-20 Thread eryksun
On Thu, Nov 19, 2015 at 10:31 AM, Michael Torrie  wrote:
> One windows it might be possible to use the win32 api to enumerate the
> windows, find your console window and switch to it.

You can call GetConsoleWindow [1] and then SetForegroundWindow [2].

import os
import sys

try:
import tkinter
from tkinter import filedialog
except ImportError:
import Tkinter as tkinter
import tkFileDialog as filedialog
input = raw_input

if sys.platform == 'win32':
import ctypes

kernel32 = ctypes.WinDLL('kernel32')
user32 = ctypes.WinDLL('user32')

def setfgwin():
hcon = kernel32.GetConsoleWindow()
if hcon and user32.SetForegroundWindow(hcon):
return True
return False

def get_filename():
filename = filedialog.askopenfilename()
setfgwin()
return os.path.normpath(filename)

if __name__ == '__main__':
root = tkinter.Tk()
root.withdraw()
filename = get_filename()
print('filename: %s' % filename)
input('press enter...')

[1]: https://msdn.microsoft.com/en-us/library/ms683175
[2]: https://msdn.microsoft.com/en-us/library/ms633539
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Persistent api-ms-win-crt-runtime-i1-1-0.dll start up error

2015-11-24 Thread eryksun
On Tue, Nov 24, 2015 at 3:54 AM, Laura Creighton  wrote:
> In a message of Tue, 24 Nov 2015 10:56:32 +0200, 
> tolga.karabiyiko...@ankara.edu
>
>>The question is i am receiving 'api-ms-win-crt-runtime-i1-1-0.dll is
>>missing' error on the start up.
>
> You appear to have this problem.
>
> https://bugs.python.org/issue25223
>
> Installing http://www.microsoft.com/en-us/download/details.aspx?id=48234
> directly seems to fix it.

For Vista the Universal CRT update requires service pack 2:

http://windows.microsoft.com/en-US/windows-vista/Learn-how-to-install-Windows-Vista-Service-Pack-2-SP2
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] Convert Qstring to string in windows

2014-10-17 Thread eryksun
On Thu, Oct 16, 2014 at 10:21 AM, C@rlos  wrote:
> in linux i do for this way:
> pythonstringtext=qstringtext.text().toUtf8.data()
> and it return a python string correctly.

pythonstringtext is a byte string that has to be decoded as UTF-8.
Here's the 'mojibake' result when it gets decoded as UTF-16LE or
Windows codepages 850 and 1252:

>>> s = u'áéíóú'
>>> b = s.encode('utf-8')

>>> print b.decode('utf-16le')
ꇃ꧃귃돃뫃

>>> print b.decode('850')
├í├®├¡├│├║

>>> print b.decode('1252')
áéíóú

The native character size in the Windows kernel is 2 bytes, so UTF-16
is the path of least resistance for in-memory strings used with GUI
controls and file/registry paths. UTF-8 is preferred for text data in
files and network communication.
-- 
https://mail.python.org/mailman/listinfo/python-list