ANN: eGenix pyOpenSSL Distribution 0.13.0-1.0.0j

2012-05-16 Thread eGenix Team: M.-A. Lemburg

ANNOUNCING

   eGenix.com pyOpenSSL Distribution

 Version 0.13.0-1.0.0j


 An easy-to-install and easy-to-use distribution
 of the pyOpenSSL Python interface for OpenSSL -
available for Windows, Mac OS X and Unix platforms


This announcement is also available on our web-site for online reading:
http://www.egenix.com/company/news/eGenix-pyOpenSSL-Distribution-0.13.0-1.0.0j-1.html


INTRODUCTION

The eGenix.com pyOpenSSL Distribution includes everything you need to
get started with SSL in Python.

It comes with an easy-to-use installer that includes the most recent
OpenSSL library versions in pre-compiled form, making your application
independent of OS provided OpenSSL libraries:

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

pyOpenSSL is an open-source Python add-on that allows writing SSL/TLS-
aware network applications as well as certificate management tools:

https://launchpad.net/pyopenssl/

OpenSSL is an open-source implementation of the SSL/TLS protocol:

http://www.openssl.org/


NEWS

This new release of the eGenix.com pyOpenSSL Distribution updates the
included included OpenSSL version to 1.0.0g.

New features in OpenSSL 1.0.0j since 1.0.0g
---

OpenSSL 1.0.0j fixes several vulnerabilities relative to 1.0.0g:

http://openssl.org/news/vulnerabilities.html

and includes a number of stability enhancements as well as extra
protection against attacks:

http://openssl.org/news/changelog.html

New features in the eGenix pyOpenSSL Distribution
-

 * Fixed a compatibility problem with Python 2.7's distutils that
   was introduced in Python 2.7.3

As always, we provide binaries that include both pyOpenSSL and the
necessary OpenSSL libraries for all supported platforms:
Windows x86 and x64, Linux x86 and x64, Mac OS X PPC, x86 and x64.

We've also added egg-file distribution versions of our eGenix.com
pyOpenSSL Distribution for Windows, Linux and Mac OS X to the
available download options. These make setups using e.g. zc.buildout
and other egg-file based installers a lot easier.


DOWNLOADS

The download archives and instructions for installing the package can
be found at:

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


UPGRADING

Before installing this version of pyOpenSSL, please make sure that
you uninstall any previously installed pyOpenSSL version. Otherwise,
you could end up not using the included OpenSSL libs.

___
SUPPORT

Commercial support for these packages is available from eGenix.com.
Please see

http://www.egenix.com/services/support/

for details about our support offerings.


MORE INFORMATION

For more information about the eGenix pyOpenSSL Distributon, licensing
and download instructions, please visit our web-site or write to
sa...@egenix.com.

Enjoy,

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, May 16 2012)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/

2012-07-02: EuroPython 2012, Florence, Italy   47 days to go
2012-04-26: Released mxODBC 3.1.2 http://egenix.com/go28
2012-04-25: Released eGenix mx Base 3.2.4 http://egenix.com/go27

::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ucs2 and ucs4 python

2012-05-16 Thread zayatzz
On May 15, 7:42 pm, Miki Tebeka  wrote:
> > Can someone point me towards a resource or two which will tell me how
> > to do this - im not very good with whole linux/servers stuff. Im using
> > ubuntu linux - if that makes any difference.
>
> Did not test, but this is the direction I would take:
> * Download Python sources
> * Open Terminal
> * Run the following commands in the Terminal window
>   - sudo apt-get build-dep python
>   - tar -xjf Python-2.7.3.tar.bz2
>   - cd Python-2.7.3
>   - ./configure --prefix=/opt --enable-unicode=ucs2 && make
>   - sudo make install
> * Now you should have /opt/bin/python with ucs2
>
> HTH
> --
> Miki Tebeka http://pythonwise.blogspot.com

Thanks for reply :)

And it seems to work... When i type in
/opt/bin/python
import sys
sys.maxunicode

then i get the desired answer - 65535

But it seems the work only begins now, because i cant use other python
libraries now with this version of python and i probably have to
install them all again somehow...

But thanks for your help :)

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


Re: ucs2 and ucs4 python

2012-05-16 Thread zayatzz
There is one problem though...

when i start script with shebang like
#!/opt/bin/python

and then try to run the script i get:

/opt/bin/python^M: bad interpreter: No such file or directory

/opt/bin/python
/opt/bin/python2
/opt/bin/python2.7 all start this new version of python, but none of
those work in shebang

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


Re: ucs2 and ucs4 python

2012-05-16 Thread Stefan Behnel
zayatzz, 16.05.2012 10:22:
> On May 15, 7:42 pm, Miki Tebeka wrote:
>>> Can someone point me towards a resource or two which will tell me how
>>> to do this - im not very good with whole linux/servers stuff. Im using
>>> ubuntu linux - if that makes any difference.
>>
>> Did not test, but this is the direction I would take:
>> * Download Python sources
>> * Open Terminal
>> * Run the following commands in the Terminal window
>>   - sudo apt-get build-dep python
>>   - tar -xjf Python-2.7.3.tar.bz2
>>   - cd Python-2.7.3
>>   - ./configure --prefix=/opt --enable-unicode=ucs2 && make
>>   - sudo make install
>> * Now you should have /opt/bin/python with ucs2
>>
>> HTH
>> --
>> Miki Tebeka http://pythonwise.blogspot.com
> 
> Thanks for reply :)
> 
> And it seems to work... When i type in
> /opt/bin/python
> import sys
> sys.maxunicode
> 
> then i get the desired answer - 65535
> 
> But it seems the work only begins now, because i cant use other python
> libraries now with this version of python and i probably have to
> install them all again somehow...

You should install "distribute" and "pip" into it, then you can use pip to
install packages directly and automatically from the Python Package Index
(PyPI), including any dependencies.

Even better, use "virtualenv" to create a local copy of your installation
and then install packages into that. This allows you to use completely
separate environments of the same Python installation, which can be very
handy for testing.

Stefan

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


Re: ucs2 and ucs4 python

2012-05-16 Thread Matej Cepl

On 16.5.2012 10:36, zayatzz wrote:

/opt/bin/python^M: bad interpreter: No such file or directory


Your script has CRLF end-of-lines. Change it to plain Unix LF.

Matěj
--
http://mail.python.org/mailman/listinfo/python-list


Re: ucs2 and ucs4 python

2012-05-16 Thread Chris Angelico
On Wed, May 16, 2012 at 6:36 PM, zayatzz  wrote:
> There is one problem though...
>
> when i start script with shebang like
> #!/opt/bin/python
>
> and then try to run the script i get:
>
> /opt/bin/python^M: bad interpreter: No such file or directory

You have a Windows end-of-line \r\n instead of a Unix end-of-line \n -
how are you editing the files? If nothing else, run the script through
dos2unix or equivalent before executing.

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


Re: ucs2 and ucs4 python

2012-05-16 Thread zayatzz
On May 16, 11:50 am, Matej Cepl  wrote:
> On 16.5.2012 10:36, zayatzz wrote:
>
> > /opt/bin/python^M: bad interpreter: No such file or directory
>
> Your script has CRLF end-of-lines. Change it to plain Unix LF.
>
> Matěj

Thanks :) but i have no idea what that means or how to achieve that.

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


Re: ucs2 and ucs4 python

2012-05-16 Thread Dave Angel
On 05/16/2012 05:20 AM, zayatzz wrote:
> On May 16, 11:50 am, Matej Cepl  wrote:
>> On 16.5.2012 10:36, zayatzz wrote:
>>
>>> /opt/bin/python^M: bad interpreter: No such file or directory
>> Your script has CRLF end-of-lines. Change it to plain Unix LF.
>>
>> Matěj
> Thanks :) but i have no idea what that means or how to achieve that.
>
> Alan

See in the echo of the shebang line the "^M" at the end ?  That's a
carriage return, hex(0d).  Unix/Linux use a single linefeed (hex(0a)) at
the end of the line.  At some point, you probably edited this file with
a Windows (aka DOS) editor, and it used the CRLF form (hex(0d0a)),
carriage return/line feed at the end of each line.

Your Linux text editor probably has a menu option to convert them back
to simple linefeeds, but if not, your Linux/Unix probably has a utility

dos2unix

which can do the job.



-- 

DaveA

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


Re: Good data structure for finding date intervals including a given date

2012-05-16 Thread Jean-Daniel
On Sun, May 13, 2012 at 2:29 PM, Alec Taylor  wrote:
> There is an ordered dict type since Python 3.1[1] and Python 2.7.3[2].

Ordered dict are useful, but they only remember the ordered in which
they were added, you can not order them a on key.

Thanks for the links.

>
> If you are looking for the best possible self-sorting structure for
> searching, then perhaps you are looking for what's outlined in the
> 2002 article by Han & Thorup: Integer Sorting in O(n sqrt(log log n))
> Expected Time and Linear Space[3].
>
> [1] http://www.python.org/getit/releases/3.1/
> [2] http://www.python.org/getit/releases/2.7.3/
> [3] http://dl.acm.org/citation.cfm?id=645413.652131
>
> On Sat, May 12, 2012 at 10:17 PM, Jean-Daniel
>  wrote:
>>
>> Hello,
>>
>> I have a long list of n date intervals that gets added or suppressed
>> intervals regularly. I am looking for a fast way to find the intervals
>> containing a given date, without having to check all intervals (less
>> than O(n)).
>>
>> Do you know the best way to do this in Python with the stdlib?
>>
>> A variant of the red black trees can do the job quickly [1], is this a
>> good enough use case to discuss the inclusion of a red black tree
>> implementation in the stdlib?
>>
>> This has been discussed here: http://bugs.python.org/issue1324770 ,
>> and lack of good use case was the reason the bug was closed. A dict
>> implemented with red black trees is slower (but not too slow) at
>> inserting, searching and deleting but the dict is always kept ordered.
>> Bigger projects have their own implementation of ordered dict so such
>> datastructures in the standard library would help the porting of the
>> project to other platforms. Take the example of the zodb and the
>> C-only implementation of the btree: btree in the stdlib in Python
>> would help the porting to GAE or pypy [2].
>>
>> Cheers,
>>
>> [1] in the "Cormen" book:
>> http://en.wikipedia.org/wiki/Introduction_to_Algorithms
>> [2] https://blueprints.launchpad.net/zodb/+spec/remove-btree-dependency
>> --
>> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


what does newP = func(code,p) do?

2012-05-16 Thread e-mail mgbg25171
def execute (code) :
p = 0
while p < len(code) :
func = code[p]
p += 1
newP = func(code,p)
if newP != None :
p = newP

I'm trying to work out what this does

code is a list of function addresses and numbers
What on earth is funct(code,p) doing???

My understanding so far is...
set p to 0
while p is less than the number of elements in list code keep doing what
follows...
set func to the first element in the list
increment the list index p
?
if newP is not null then set the list index p to ?
Can someone please explain in english what ? is
Thank you in anticipation

BTW the link for this stuff is

http://openbookproject.net/py4fun/forth/forth.html
to see the thing in action ie the download link is on the above page

Here's a listing of the output from this proc for the following forth input
i.e. I put some print statements in
but have no idea how
newP set to func( [, 4] , 1 ) i.e.  2
results in 2
and then
newP set to func( [] , 1 ) i.e.  None
results in None
???


Forth> 5 4 + .

1st line of execute__
code =  [, 5]
p =  0
1st line of while__
func = code[ 0 ] i.e.  
incrementing p to  1
newP set to func( [, 5] , 1 ) i.e.  2
p = newP i.e.  2

1st line of execute__
code =  [, 4]
p =  0
1st line of while__
func = code[ 0 ] i.e.  
incrementing p to  1
newP set to func( [, 4] , 1 ) i.e.  2
p = newP i.e.  2

1st line of execute__
code =  []
p =  0
1st line of while__
func = code[ 0 ] i.e.  
incrementing p to  1
newP set to func( [] , 1 ) i.e.  None

1st line of execute__
code =  []
p =  0
1st line of while__
func = code[ 0 ] i.e.  
incrementing p to  1
13
newP set to func( [] , 1 ) i.e.  5
None
Forth>
-- 
http://mail.python.org/mailman/listinfo/python-list


python and xml

2012-05-16 Thread Nibin V M
Hi,

I am trying to use cPanel XML-API and every API call return data in XML
format. I would like to know how to manipulate the data here.

For eg: How can I read the CPU load data from the below output


 0.000.000.00

Thank you,
-- 
Regards

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


Re: python and xml

2012-05-16 Thread Stefan Behnel
Nibin V M, 16.05.2012 16:16:
> I am trying to use cPanel XML-API and every API call return data in XML
> format. I would like to know how to manipulate the data here.
> 
> For eg: How can I read the CPU load data from the below output
> 
> 
>  
> 0.000.000.00

Here's some untested code to print the text values:

  import xml.etree.ElementTree as ET

  loadavg = ET.fromstring('0.000.00'
  '0.00')

  for interval_tag in ('one', 'five', 'fifteen'):
  print(loadavg.findtext(interval_tag))


Stefan

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


Wing IDE 4.1.6 released

2012-05-16 Thread Wingware

Hi,

Wingware has released version 4.1.6 of Wing IDE, an integrated development
environment designed specifically for the Python programming language.

Wing IDE is a cross-platform Python IDE that provides a professional code
editor with vi, emacs, and other key bindings, auto-completion, call tips,
refactoring, context-aware auto-editing, a powerful graphical debugger,
version control, unit testing, search, and many other features.

**Changes**

This release includes:

* Support for Django 1.4
* Syntax highlighting Qt Style Sheet (.qss) files
* Command to show selected file in OS-provided file manager
* Per-project configuration of Debug Network Port for remote debugging
* Several auto-editing fixes
* Several turbo completion mode fixes
* Replace All preserves fold state when possible
* Git blame support
* Fixed debugging QThreads in older PyQt versions
* Shorter delay in restarting Python Shell or debug process
* About 15 other bug fixes and minor improvements

Complete change log: http://wingware.com/pub/wingide/4.1.6/CHANGELOG.txt

**New Features in Version 4**

Version 4 adds the following new major features:

* Refactoring -- Rename/move symbols, extract to function/method, and 
introduce variable

* Find Uses -- Find all points of use of a symbol
* Auto-Editing -- Reduce typing by auto-entering expected code
* Diff/Merge -- Graphical file and repository comparison and merge
* Django Support -- Debug Django templates, run Django unit tests, and more
* matplotlib Support -- Maintains live-updating plots in shell and debugger
* Simplified Licensing -- Includes all OSes and adds Support+Upgrades 
subscriptions


Details on licensing changes: http://wingware.com/news/2011-02-16

**About Wing IDE**

Wing IDE is an integrated development environment designed specifically for
the Python programming language.  It provides powerful editing, testing, and
debugging features that help reduce development and debugging time, cut down
on coding errors, and make it easier to understand and navigate Python code.
Wing IDE can be used to develop Python code for web, GUI, and embedded
scripting applications.

Wing IDE is available in three product levels:  Wing IDE Professional is
the full-featured Python IDE, Wing IDE Personal offers a reduced feature
set at a low price, and Wing IDE 101 is a free simplified version designed
for teaching beginning programming courses with Python.

Version 4 of Wing IDE Professional includes the following major features:

* Professional quality code editor with vi, emacs, and other keyboard
  personalities
* Code intelligence for Python:  Auto-completion, call tips, find uses,
  goto-definition, error indicators, refactoring, context-aware 
auto-editing,

  smart indent and rewrapping, and source navigation
* Advanced multi-threaded debugger with graphical UI, command line 
interaction,

  conditional breakpoints, data value tooltips over code, watch tool, and
  externally launched and remote debugging
* Powerful search and replace options including keyboard driven and 
graphical

  UIs, multi-file, wild card, and regular expression search and replace
* Version control integration for Subversion, CVS, Bazaar, git, 
Mercurial, and

  Perforce
* Integrated unit testing with unittest, nose, and doctest frameworks
* Django support:  Debugs Django templates, provides project setup tools,
  and runs Django unit tests
* Many other features including project manager, bookmarks, code snippets,
  diff/merge tool, OS command integration, indentation manager, PyLint
  integration, and perspectives
* Extremely configurable and may be extended with Python scripts
* Extensive product documentation and How-Tos for Django, matplotlib,
  Plone, wxPython, PyQt, mod_wsgi, Autodesk Maya, and many other frameworks

Please refer to http://wingware.com/wingide/featuresfor a detailed listing
of features by product level.

System requirements are Windows 2000 or later, OS X 10.3.9or later (requires
X11 Server), or a recent Linux system (either 32 or 64 bit).  Wing IDE 
supports

Python versions 2.0.x through 3.2.x and Stackless Python.

For more information, see the http://wingware.com/

**Downloads**

Wing IDE Professional and Wing IDE Personal are commercial software and
require a license to run. A free trial can be obtained directly from the
product when launched.

Wing IDE Pro -- Full-featured product:
http://wingware.com/downloads/wingide/4.1

Wing IDE Personal -- A simplified IDE:
http://wingware.com/downloads/wingide-personal/4.1

Wing IDE 101 -- For teaching with Python:
http://wingware.com/downloads/wingide-101/4.1

**Purchasing and Upgrading**

Wing 4.x requires an upgrade for Wing IDE 2.x and 3.x users at a cost of
1/2 the full product pricing.

Upgrade a license: https://wingware.com/store/upgrade
Purchase a new license: https://wingware.com/store/purchase

Optional Support+Upgrades subscriptions are available for expanded
support coverage and free upgrades to new major releases:

http://wingware.co

Re: python and xml

2012-05-16 Thread Nibin V M
thank you Stefan. but the XML output is assigned to a variable; how to
process the variable with XML contents?

On Wed, May 16, 2012 at 7:53 PM, Stefan Behnel  wrote:

> Nibin V M, 16.05.2012 16:16:
> > I am trying to use cPanel XML-API and every API call return data in XML
> > format. I would like to know how to manipulate the data here.
> >
> > For eg: How can I read the CPU load data from the below output
> >
> >
> >
>  
> 0.000.000.00
>
> Here's some untested code to print the text values:
>
>  import xml.etree.ElementTree as ET
>
>  loadavg = ET.fromstring('0.000.00'
>  '0.00')
>
>  for interval_tag in ('one', 'five', 'fifteen'):
>  print(loadavg.findtext(interval_tag))
>
>
> Stefan
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Regards

Nibin.

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


Re: python and xml

2012-05-16 Thread Stefan Behnel
Hi,

please don't top-post (I fixed the citation order below).


Nibin V M, 16.05.2012 16:30:
> On Wed, May 16, 2012 at 7:53 PM, Stefan Behnel wrote:
>> Nibin V M, 16.05.2012 16:16:
>>> I am trying to use cPanel XML-API and every API call return data in XML
>>> format. I would like to know how to manipulate the data here.
>>>
>>> For eg: How can I read the CPU load data from the below output
>>>
>>>
>>>
>>  0.000.000.00
>>  
>>
>> Here's some untested code to print the text values:
>>
>>  import xml.etree.ElementTree as ET
>>
>>  loadavg = ET.fromstring('0.000.00'
>>  '0.00')
>>
>>  for interval_tag in ('one', 'five', 'fifteen'):
>>  print(loadavg.findtext(interval_tag))
>
> thank you Stefan. but the XML output is assigned to a variable; how to
> process the variable with XML contents?

Not sure what you mean. Maybe something like this?

xml = '0.000.00...'
loadavg = ET.fromstring(xml)

Stefan

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


How to embed python2 into python3?

2012-05-16 Thread ytj
Hello, all:

I have two programs, one is written in py3k, the other is written in
python 2. I am wondering how to make them work together except port
the python 2 code to py3k? Is that possible to expose python2's
function to py3k? In other words, I want to embed the Python 2
interpreter into my py3k program. So I can call python2's function in
py3k's code.

Regards,
Tianjiao
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what does newP = func(code,p) do?

2012-05-16 Thread Ian Kelly
On Wed, May 16, 2012 at 8:08 AM, e-mail mgbg25171
 wrote:
> def execute (code) :
>     p = 0
>     while p < len(code) :
>     func = code[p]
>     p += 1
>     newP = func(code,p)
>     if newP != None :
>     p = newP
>
> I'm trying to work out what this does
>
> code is a list of function addresses and numbers
> What on earth is funct(code,p) doing???

code represents the bytecode of the Forth program being executed.  It
contains functions that represent operations and the data arguments used by
those operations.  p is the program counter, keeping track of which
opcode should be executed next.  func is the operation function that
was pulled from code at the current program counter.  func(code, p)
calls that function, passing in both the entire bytecode, and the
index into the bytecode at which the operation can find its data (if
any).  If the function returns a value, it means that the program
counter should be updated -- either to skip past a data element used
for the operation just executed, or to perform a jump to somewhere
else in the bytecode.  If the program counter ever falls off the end
of the bytecode, then the program terminates.
-- 
http://mail.python.org/mailman/listinfo/python-list


Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread Marco

Hi all, because

"There should be one-- and preferably only one --obvious way to do it",

there should be a difference between the two methods in the subject, but 
I can't find it:


>>> '123'.isdecimal(), '123'.isdigit()
(True, True)
>>> print('\u0660123')
٠123
>>> '\u0660123'.isdigit(), '\u0660123'.isdecimal()
(True, True)
>>> print('\u216B')
Ⅻ
>>> '\u216B'.isdecimal(), '\u216B'.isdigit()
(False, False)

Can anyone give me some help?
Regards, Marco
--
http://mail.python.org/mailman/listinfo/python-list


what does newP = func(code,p) do?

2012-05-16 Thread e-mail mgbg25171
It's been a long time since I did any Python and I've never done that
In C I'm used to storing function ptrs and then having to use some other
constructs to call them.
To be able to store func and then use func to call itself like that threw
me...it's very elegant.
Thank you very much for your very lucid explanation and for taking the time
to provide it.
Best Regards
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread Ian Kelly
On Wed, May 16, 2012 at 9:48 AM, Marco  wrote:
> Hi all, because
>
> "There should be one-- and preferably only one --obvious way to do it",
>
> there should be a difference between the two methods in the subject, but I
> can't find it:
>
 '123'.isdecimal(), '123'.isdigit()
> (True, True)
 print('\u0660123')
> ٠123
 '\u0660123'.isdigit(), '\u0660123'.isdecimal()
> (True, True)
 print('\u216B')
> Ⅻ
 '\u216B'.isdecimal(), '\u216B'.isdigit()
> (False, False)
>
> Can anyone give me some help?

Here's one example:

>>> '\u00B2'.isdecimal()
False
>>> '\u00B2'.isdigit()
True
>>> '\u00B2'
'²'

The distinction is explained in the docs at:
http://docs.python.org/py3k/library/stdtypes.html#str.isdigit

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


Re: Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread MRAB

On 16/05/2012 16:48, Marco wrote:

Hi all, because

"There should be one-- and preferably only one --obvious way to do it",

there should be a difference between the two methods in the subject, but
I can't find it:

  >>>  '123'.isdecimal(), '123'.isdigit()
(True, True)
  >>>  print('\u0660123')
٠123
  >>>  '\u0660123'.isdigit(), '\u0660123'.isdecimal()
(True, True)
  >>>  print('\u216B')
Ⅻ
  >>>  '\u216B'.isdecimal(), '\u216B'.isdigit()
(False, False)

Can anyone give me some help?
Regards, Marco


Try this:

>>> different = [chr(c) for c in range(0x1) if chr(c).isdigit() != 
chr(c).isdecimal()]

>>> print(different)
['²', '³', '¹', '፩', '፪', '፫', '፬', '፭', '፮', '፯', '፰', '፱', '⁰', '⁴', 
'⁵', '⁶', '⁷', '⁸', '⁹', '₀', '₁', '₂', '₃', '₄', '₅', '₆', '₇', '₈', 
'₉', '①', '②', '③', '④', '⑤', '⑥', '⑦', '⑧', '⑨', '⑴', '⑵', '⑶', '⑷', 
'⑸', '⑹', '⑺', '⑻', '⑼', '⒈', '⒉', '⒊', '⒋', '⒌', '⒍', '⒎', '⒏', '⒐', 
'⓪', '⓵', '⓶', '⓷', '⓸', '⓹', '⓺', '⓻', '⓼', '⓽', '⓿', '❶', '❷', '❸', 
'❹', '❺', '❻', '❼', '❽', '❾', '➀', '➁', '➂', '➃', '➄', '➅', '➆', '➇', 
'➈', '➊', '➋', '➌', '➍', '➎', '➏', '➐', '➑', '➒']

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


Re: Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread Ulrich Eckhardt
Marco wrote:
>  >>> '123'.isdecimal(), '123'.isdigit()
> (True, True)
>  >>> print('\u0660123')
> ٠123
>  >>> '\u0660123'.isdigit(), '\u0660123'.isdecimal()
> (True, True)
>  >>> print('\u216B')
> Ⅻ
>  >>> '\u216B'.isdecimal(), '\u216B'.isdigit()
> (False, False)

[chr(a) for a in range(0x2) if chr(a).isdigit()]

Congratulations, you found a bug! Or maybe not, it all depends on whether 
Roman numbers are considered digits or not. I could imagine there being a 
difference.

:)

Uli

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


Re: Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread Ian Kelly
On Wed, May 16, 2012 at 10:24 AM, Ulrich Eckhardt  wrote:
> Marco wrote:
>>  >>> '123'.isdecimal(), '123'.isdigit()
>> (True, True)
>>  >>> print('\u0660123')
>> ٠123
>>  >>> '\u0660123'.isdigit(), '\u0660123'.isdecimal()
>> (True, True)
>>  >>> print('\u216B')
>> Ⅻ
>>  >>> '\u216B'.isdecimal(), '\u216B'.isdigit()
>> (False, False)
>
> [chr(a) for a in range(0x2) if chr(a).isdigit()]
>
> Congratulations, you found a bug! Or maybe not, it all depends on whether
> Roman numbers are considered digits or not. I could imagine there being a
> difference.

They're not.  The word "digit" specifically refers to the symbols used
by a positional numeral system, e.g. Arabic numerals.  Roman numerals
are not a positional system.  The word "decimal" in this case more
specifically means a digit character that is actually suitable for
using to compose a decimal number.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to embed python2 into python3?

2012-05-16 Thread Chris Rebert
On Wed, May 16, 2012 at 7:59 AM, ytj  wrote:
> Hello, all:
>
> I have two programs, one is written in py3k, the other is written in
> python 2. I am wondering how to make them work together except port
> the python 2 code to py3k?

Porting the Python 3 code to Python 2 is also an option:
http://pypi.python.org/pypi/3to2

But unless you're dependent on a library not available for Python 3
yet, the normal 2to3 route is probably preferable.

In either direction, this library may help ease the transition:
http://pypi.python.org/pypi/six/

> Is that possible to expose python2's
> function to py3k? In other words, I want to embed the Python 2
> interpreter into my py3k program. So I can call python2's function in
> py3k's code.

That's very likely possible using the C API; I don't know of any
prepackaged solution though.
Another obvious approach to connect the two interpreters would be to
use some form of IPC or RPC (e.g. XML-RPC, Unix domain sockets, etc.).
There will be some degree of extra overhead involved no matter which
route you go.

Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to embed python2 into python3?

2012-05-16 Thread Ian Kelly
On Wed, May 16, 2012 at 8:59 AM, ytj  wrote:
> Hello, all:
>
> I have two programs, one is written in py3k, the other is written in
> python 2. I am wondering how to make them work together except port
> the python 2 code to py3k? Is that possible to expose python2's
> function to py3k? In other words, I want to embed the Python 2
> interpreter into my py3k program. So I can call python2's function in
> py3k's code.

I think you're likely to run into conflicts doing that.  Here's the
result of a quick test using ctypes:

Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> python27 = cdll.python27
>>> python27.Py_Initialize()
  File "c:\python32\lib\site.py", line 159
file=sys.stderr)
^
SyntaxError: invalid syntax

For some reason, even though I loaded the Python 2.7 DLL, it's then
trying to import the Python 3.2 libraries.  My suggestion: instead of
embedding, use the subprocess module, and keep both Python
interpreters firmly entrenched in separate processes.

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


Re: Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread Marco

On 05/16/2012 06:24 PM, Ulrich Eckhardt wrote:


Marco wrote:

>>>>  '123'.isdecimal(), '123'.isdigit()
>  (True, True)
>>>>  print('\u0660123')
>  ٠123
>>>>  '\u0660123'.isdigit(), '\u0660123'.isdecimal()
>  (True, True)
>>>>  print('\u216B')
>  Ⅻ
>>>>  '\u216B'.isdecimal(), '\u216B'.isdigit()
>  (False, False)



[chr(a) for a in range(0x2) if chr(a).isdigit()]


Thanks to your list comprehension I found they are not equal:

>>> set([chr(a) for a in range(0x10) if chr(a).isdigit()]) - \
... set([chr(a) for a in range(0x10) if chr(a).isdecimal()])

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


Re: Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread jmfauth
On 16 mai, 17:48, Marco  wrote:
> Hi all, because
>
> "There should be one-- and preferably only one --obvious way to do it",
>
> there should be a difference between the two methods in the subject, but
> I can't find it:
>
>  >>> '123'.isdecimal(), '123'.isdigit()
> (True, True)
>  >>> print('\u0660123')
> ٠123
>  >>> '\u0660123'.isdigit(), '\u0660123'.isdecimal()
> (True, True)
>  >>> print('\u216B')
> Ⅻ
>  >>> '\u216B'.isdecimal(), '\u216B'.isdigit()
> (False, False)
>
> Can anyone give me some help?
> Regards, Marco

It seems to me that it is correct, and the reason lies in this:

>>> import unicodedata as ud
>>> ud.category('\u216b')
'Nl'
>>> ud.category('1')
'Nd'
>>>
>>> # Note
>>> ud.numeric('\u216b')
12.0

jmf

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


Re: which book?

2012-05-16 Thread Ashraf Fouda
I recommend this book :- "beginning python from novice to professional"

On Wed, May 9, 2012 at 4:13 PM, Miki Tebeka  wrote:

> > I am going to learn python for some plot issues. which book or sources,
> do you recommend please?
> The tutorial is pretty good if you already know how to program.
> I also heard a lot of good things on "Python Essential Reference".
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
yours
Ashraf Fouda
Software Engineer
*
*
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread Thomas 'PointedEars' Lahn
Marco wrote:

> Hi all, because
> 
> "There should be one-- and preferably only one --obvious way to do it",
> 
> there should be a difference between the two methods in the subject, but
> I can't find it:
> 
>  >>> '123'.isdecimal(), '123'.isdigit()
> (True, True)
>  >>> print('\u0660123')
> ٠123
>  >>> '\u0660123'.isdigit(), '\u0660123'.isdecimal()
> (True, True)
>  >>> print('\u216B')
> Ⅻ
>  >>> '\u216B'.isdecimal(), '\u216B'.isdigit()
> (False, False)
> 
> Can anyone give me some help?

RTFM.

$ python3 -c 'print("42".isdecimal.__doc__ + "\n"); 
print("42".isdigit.__doc__)'
S.isdecimal() -> bool

Return True if there are only decimal characters in S,
False otherwise.

S.isdigit() -> bool

Return True if all characters in S are digits
and there is at least one character in S, False otherwise.

-- 
PointedEars

Please do not Cc: me. / Bitte keine Kopien per E-Mail.
-- 
http://mail.python.org/mailman/listinfo/python-list


cPython, IronPython, Jython, and PyPy (Oh my!)

2012-05-16 Thread Ethan Furman

Just hit a snag:

In cPython the deterministic garbage collection allows me a particular 
optimization when retrieving records from a dbf file -- namely, by using 
weakrefs I can tell if the record is still in memory and active, and if 
so not hit the disk to get the data;  with PyPy (and probably the 
others) this doesn't work because the record may still be around even 
when it is no longer active because it hasn't been garbage collected yet.


For PyPy I can use `'PyPy' in sys.version` to set a constant 
(REFRESH_FROM_DISK in this case) to disable the cPython optimization; 
does anyone know what strings to look for for the other implementations?


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


Re: Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread Ian Kelly
On Wed, May 16, 2012 at 3:07 PM, Thomas 'PointedEars' Lahn
 wrote:
> RTFM.
>
> $ python3 -c 'print("42".isdecimal.__doc__ + "\n");
> print("42".isdigit.__doc__)'
> S.isdecimal() -> bool
>
> Return True if there are only decimal characters in S,
> False otherwise.
>
> S.isdigit() -> bool
>
> Return True if all characters in S are digits
> and there is at least one character in S, False otherwise.

Those doc strings are not very helpful at all.  I read them myself and
then had to go to the library docs to figure out what the actual
difference was.
-- 
http://mail.python.org/mailman/listinfo/python-list


non-pickle persistance for dicts?

2012-05-16 Thread Charles Hixson
I want to persist simple dicts, but due to the security problems with 
(un)pickle, I'd prefer to not use shelve, and the only way I could see 
to persist them onto sqlite also invoked pickle.


As (un)pickle allows arbitrary system commands to be issued, I'd really 
rather just use a simple convert to and from either bytes or strings.  
repr works well for the conversion into string (I said they were 
simple), but I'd really rather be able to turn "{'a': 'A', 1: 23, 2: 
['b', 2]}" back into a dict without allowing the execution of arbitrary 
commands.


Any suggestions?

--
Charles Hixson

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


Re: cPython, IronPython, Jython, and PyPy (Oh my!)

2012-05-16 Thread Ian Kelly
On Wed, May 16, 2012 at 3:33 PM, Ethan Furman  wrote:
> Just hit a snag:
>
> In cPython the deterministic garbage collection allows me a particular
> optimization when retrieving records from a dbf file -- namely, by using
> weakrefs I can tell if the record is still in memory and active, and if so
> not hit the disk to get the data;  with PyPy (and probably the others) this
> doesn't work because the record may still be around even when it is no
> longer active because it hasn't been garbage collected yet.
>
> For PyPy I can use `'PyPy' in sys.version` to set a constant
> (REFRESH_FROM_DISK in this case) to disable the cPython optimization; does
> anyone know what strings to look for for the other implementations?

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 sys
>>> sys.subversion
('CPython', 'tags/r271', '86832')

Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_31
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.subversion
('Jython', 'tags/Release_2_5_2', '7206')

I don't know what IronPython or PyPy return, but it should be
something other than 'CPython'.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: non-pickle persistance for dicts?

2012-05-16 Thread Ian Kelly
On Wed, May 16, 2012 at 3:52 PM, Charles Hixson
 wrote:
> I want to persist simple dicts, but due to the security problems with
> (un)pickle, I'd prefer to not use shelve, and the only way I could see to
> persist them onto sqlite also invoked pickle.
>
> As (un)pickle allows arbitrary system commands to be issued, I'd really
> rather just use a simple convert to and from either bytes or strings.  repr
> works well for the conversion into string (I said they were simple), but I'd
> really rather be able to turn "{'a': 'A', 1: 23, 2: ['b', 2]}" back into a
> dict without allowing the execution of arbitrary commands.
>
> Any suggestions?

Either json, or repr with ast.literal_eval will be safe.

>>> import json
>>> d = {'a': 'A', 1: 23, 2: ['b', 2]}
>>> json.dumps(d)
'{"a": "A", "1": 23, "2": ["b", 2]}'
>>> json.loads(json.dumps(d))
{'a': 'A', '1': 23, '2': ['b', 2]}
>>> import ast
>>> ast.literal_eval(repr(d))
{'a': 'A', 1: 23, 2: ['b', 2]}

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


Re: cPython, IronPython, Jython, and PyPy (Oh my!)

2012-05-16 Thread Tim Delaney
On 17 May 2012 07:33, Ethan Furman  wrote:

> Just hit a snag:
>
> In cPython the deterministic garbage collection allows me a particular
> optimization when retrieving records from a dbf file -- namely, by using
> weakrefs I can tell if the record is still in memory and active, and if so
> not hit the disk to get the data;  with PyPy (and probably the others) this
> doesn't work because the record may still be around even when it is no
> longer active because it hasn't been garbage collected yet.
>

What is the distinguishing feature of an "active" record? What is the
problem if you get back a reference to an inactive record? And if there is
indeed a problem, don't you already have a race condition on CPython?

1. Record is active;
2. Get reference to record through weak ref;
3. Record becomes inactive;
4. Start trying to use the (now inactive) record.

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


Re: How to call and execute C code in Python?

2012-05-16 Thread Barry Scott
There are many choices rather then raw python C API calls. Boost, PyCXX and 
ctypes are worth investigating. PyCXX requires you code in C++ but hides lots 
of the issues of using the Python API from you. It also supports python 2 and 
3. 

Barry - PyCXX maintainer

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


Re: cPython, IronPython, Jython, and PyPy (Oh my!)

2012-05-16 Thread Ethan Furman

Ian Kelly wrote:

On Wed, May 16, 2012 at 3:33 PM, Ethan Furman  wrote:

Just hit a snag:

In cPython the deterministic garbage collection allows me a particular
optimization when retrieving records from a dbf file -- namely, by using
weakrefs I can tell if the record is still in memory and active, and if so
not hit the disk to get the data;  with PyPy (and probably the others) this
doesn't work because the record may still be around even when it is no
longer active because it hasn't been garbage collected yet.

For PyPy I can use `'PyPy' in sys.version` to set a constant
(REFRESH_FROM_DISK in this case) to disable the cPython optimization; does
anyone know what strings to look for for the other implementations?


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

('CPython', 'tags/r271', '86832')

Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_31
Type "help", "copyright", "credits" or "license" for more information.

import sys
sys.subversion

('Jython', 'tags/Release_2_5_2', '7206')

I don't know what IronPython or PyPy return, but it should be
something other than 'CPython'.


Thanks!  That will do the trick.  On CPython 2.4 .subversion does not 
exist, so I'll use:


subversion = getattr(sys, 'subversion', None)
if subversion is not None and subversion[0] != 'CPython':
...

Hopefully all the others do have it defined (PyPy does, at least as of 1.8).

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


Re: non-pickle persistance for dicts?

2012-05-16 Thread Charles Hixson

On 05/16/2012 03:11 PM, Ian Kelly wrote:

On Wed, May 16, 2012 at 3:52 PM, Charles Hixson
  wrote:
   

I want to persist simple dicts, but due to the security problems with
(un)pickle, I'd prefer to not use shelve, and the only way I could see to
persist them onto sqlite also invoked pickle.

As (un)pickle allows arbitrary system commands to be issued, I'd really
rather just use a simple convert to and from either bytes or strings.  repr
works well for the conversion into string (I said they were simple), but I'd
really rather be able to turn "{'a': 'A', 1: 23, 2: ['b', 2]}" back into a
dict without allowing the execution of arbitrary commands.

Any suggestions?
 

Either json, or repr with ast.literal_eval will be safe.

   

import json
d = {'a': 'A', 1: 23, 2: ['b', 2]}
json.dumps(d)
 

'{"a": "A", "1": 23, "2": ["b", 2]}'
   

json.loads(json.dumps(d))
 

{'a': 'A', '1': 23, '2': ['b', 2]}
   

import ast
ast.literal_eval(repr(d))
 

{'a': 'A', 1: 23, 2: ['b', 2]}

Cheers,
Ian

   
Thanks.  It looks like either would do what I need.  Any suggestion as 
to how to choose between them?  E.g., is AST better supported?  faster?  
(I'm tending towards AST purely because it seems more tied to Python, 
but of course that *could* be a disadvantage, if there were more 
external tools for working with json.)


--
Charles Hixson

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


Re: non-pickle persistance for dicts?

2012-05-16 Thread Ian Kelly
On Wed, May 16, 2012 at 4:53 PM, Charles Hixson
 wrote:
> On 05/16/2012 03:11 PM, Ian Kelly wrote:
>>
>> On Wed, May 16, 2012 at 3:52 PM, Charles Hixson
>>   wrote:
>>
>>>
>>> I want to persist simple dicts, but due to the security problems with
>>> (un)pickle, I'd prefer to not use shelve, and the only way I could see to
>>> persist them onto sqlite also invoked pickle.
>>>
>>> As (un)pickle allows arbitrary system commands to be issued, I'd really
>>> rather just use a simple convert to and from either bytes or strings.
>>>  repr
>>> works well for the conversion into string (I said they were simple), but
>>> I'd
>>> really rather be able to turn "{'a': 'A', 1: 23, 2: ['b', 2]}" back into
>>> a
>>> dict without allowing the execution of arbitrary commands.
>>>
>>> Any suggestions?
>>>
>>
>> Either json, or repr with ast.literal_eval will be safe.
>>
>>
>
> import json
> d = {'a': 'A', 1: 23, 2: ['b', 2]}
> json.dumps(d)
>
>>
>> '{"a": "A", "1": 23, "2": ["b", 2]}'
>>
>
> json.loads(json.dumps(d))
>
>>
>> {'a': 'A', '1': 23, '2': ['b', 2]}
>>
>
> import ast
> ast.literal_eval(repr(d))
>
>>
>> {'a': 'A', 1: 23, 2: ['b', 2]}
>>
>> Cheers,
>> Ian
>>
>>
>
> Thanks.  It looks like either would do what I need.  Any suggestion as to
> how to choose between them?  E.g., is AST better supported?  faster?  (I'm
> tending towards AST purely because it seems more tied to Python, but of
> course that *could* be a disadvantage, if there were more external tools for
> working with json.)

You pretty much just summed it up.  JSON is more portable because it's
a well-known standard with implementations in a lot of different
languages.  On the downside, since it essentially involves translating
your data into *JavaScript* literals and back, it's not going to be
quite as faithful.  Notice in the example that the numeric dictionary
keys got turned into strings.  AST will do a cleaner job since you're
just converting Python objects into Python literals and back, but the
serialized data will only be easily readable using Python.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cPython, IronPython, Jython, and PyPy (Oh my!)

2012-05-16 Thread Ethan Furman

Tim Delaney wrote:

On 17 May 2012 07:33, Ethan Furman wrote:

Just hit a snag:

In cPython the deterministic garbage collection allows me a
particular optimization when retrieving records from a dbf file --
namely, by using weakrefs I can tell if the record is still in
memory and active, and if so not hit the disk to get the data;  with
PyPy (and probably the others) this doesn't work because the record
may still be around even when it is no longer active because it
hasn't been garbage collected yet.



What is the distinguishing feature of an "active" record? What is the 
problem if you get back a reference to an inactive record? And if there 
is indeed a problem, don't you already have a race condition on CPython?


1. Record is active;
2. Get reference to record through weak ref;
3. Record becomes inactive;
4. Start trying to use the (now inactive) record.


A record is an interesting critter -- it is given life either from the 
user or from the disk-bound data;  its fields can then change, but those 
changes are not reflected on disk until .write_record() is called;  I do 
this because I am frequently moving data from one table to another, 
making changes to the old record contents before creating the new record 
with the changes -- since I do not call .write_record() on the old 
record those changes do not get backed up to disk.


With CPython as soon as a record goes out of scope it dies, and the next 
time I try to access that record I will get the disk version, without 
the temporary changes I had made earlier (this is good).  However, with 
PyPy (and others) not all records are destroyed before I try to access 
them again, and I end up seeing the temp data instead of the disk data.


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


Re: Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread Steven D'Aprano
On Wed, 16 May 2012 17:48:19 +0200, Marco wrote:

> Hi all, because
> 
> "There should be one-- and preferably only one --obvious way to do it",
> 
> there should be a difference between the two methods in the subject, but
> I can't find it:

The Fine Manual has more detail, although I admit it isn't *entirely* 
clear what it is talking about if you're not a Unicode expert:


http://docs.python.org/py3k/library/stdtypes.html#str.isdecimal

str.isdecimal()
Return true if all characters in the string are decimal characters 
and there is at least one character, false otherwise. Decimal characters 
are those from general category “Nd”. This category includes digit 
characters, and all characters that can be used to form decimal-radix 
numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.

str.isdigit()
Return true if all characters in the string are digits and there is 
at least one character, false otherwise. Digits include decimal 
characters and digits that need special handling, such as the 
compatibility superscript digits. Formally, a digit is a character that 
has the property value Numeric_Type=Digit or Numeric_Type=Decimal.


And also:

str.isnumeric()
Return true if all characters in the string are numeric characters, 
and there is at least one character, false otherwise. Numeric characters 
include digit characters, and all characters that have the Unicode 
numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, 
numeric characters are those with the property value Numeric_Type=Digit, 
Numeric_Type=Decimal or Numeric_Type=Numeric.


Examples:

py> c = '\u2155'
py> print(c)
⅕
py> c.isdecimal(), c.isdigit(), c.isnumeric()
(False, False, True)
py> import unicodedata
py> unicodedata.numeric(c)
0.2

py> c = '\u00B2'
py> print(c)
²
py> c.isdecimal(), c.isdigit(), c.isnumeric()
(False, True, True)
py> unicodedata.numeric(c)
2.0


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


Re: non-pickle persistance for dicts?

2012-05-16 Thread Devin Jeanpierre
On Wed, May 16, 2012 at 6:53 PM, Charles Hixson
 wrote:
> Thanks.  It looks like either would do what I need.  Any suggestion as to
> how to choose between them?  E.g., is AST better supported?  faster?  (I'm
> tending towards AST purely because it seems more tied to Python, but of
> course that *could* be a disadvantage, if there were more external tools for
> working with json.)

I'd pick json, because it will tell you if you try to serialize
something that can't be deserialized. repr() will just dump it, and
then you'll find out later (on load) that it wasn't serialized
properly.

repr() is not meant for storing data, it's meant for presenting it to
a programmer.

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


Re: cPython, IronPython, Jython, and PyPy (Oh my!)

2012-05-16 Thread Chris Angelico
On Thu, May 17, 2012 at 9:01 AM, Ethan Furman  wrote:
> A record is an interesting critter -- it is given life either from the user
> or from the disk-bound data;  its fields can then change, but those changes
> are not reflected on disk until .write_record() is called;  I do this
> because I am frequently moving data from one table to another, making
> changes to the old record contents before creating the new record with the
> changes -- since I do not call .write_record() on the old record those
> changes do not get backed up to disk.

I strongly recommend being more explicit about usage and when it gets
written and re-read, rather than relying on garbage collection.
Databasing should not be tied to a language's garbage collection.
Imagine you were to reimplement the equivalent logic in some other
language - could you describe it clearly? If so, then that's your
algorithm. If not, you have a problem.

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


Re: cPython, IronPython, Jython, and PyPy (Oh my!)

2012-05-16 Thread Tim Delaney
On 17 May 2012 11:13, Chris Angelico  wrote:

> On Thu, May 17, 2012 at 9:01 AM, Ethan Furman  wrote:
> > A record is an interesting critter -- it is given life either from the
> user
> > or from the disk-bound data;  its fields can then change, but those
> changes
> > are not reflected on disk until .write_record() is called;  I do this
> > because I am frequently moving data from one table to another, making
> > changes to the old record contents before creating the new record with
> the
> > changes -- since I do not call .write_record() on the old record those
> > changes do not get backed up to disk.
>
> I strongly recommend being more explicit about usage and when it gets
> written and re-read, rather than relying on garbage collection.
> Databasing should not be tied to a language's garbage collection.
> Imagine you were to reimplement the equivalent logic in some other
> language - could you describe it clearly? If so, then that's your
> algorithm. If not, you have a problem.
>

Agreed. To me, this sounds like a perfect case for with: blocks and
explicit reference counting.  Something like (pseudo-python - not runnable):

class Record:
def __init__(self):
self.refs = 0
self.lock = threading.Lock()

def __enter__(self):
with self.lock:
self.refs += 1

def __exit__(self):
with self.lock:
self.refs -=1

if self.refs == 0:
self.write_record()



rec = record_weakrefs.get('record_name')

if rec is None:
rec = load_record()
record_weakrefs.put('record_name', rec)

with rec:
do_stuff

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


Newbie questions on import & cmd line run

2012-05-16 Thread gwhite
Hi,

I am a newbie running the latest pythonxy (2.7.2.1) & spyder and
python 2.7.2.   I suspect my questions are mostly basic to python, and
not specific to Spyder or iPython.

Note: Up until now, I mainly used MATLAB, and thus need to de-program
myself appropriately.

I use Win7-64.

I wrote the following .py file:

-
#! 
# Filename: newbie00.py

if __name__ == '__main__':
print 'This program was called from the \
system command line.'
print __name__ + '.py'
else:
print 'This program was imported on the \
Python command line.'
print __name__ + '.py'

-

If I run from the system (win cmd) command, I get:

C:\engineer\engruser\python>python  newbie00.py

This program was called from the system command line.
__main__.py

-
If I hit the run button in Sypder, I get (in the iPython command
console):

In [70]: runfile(r'C:\engineer\engruser\python\newbie00.py', wdir=r'C:
\engineer\engruser\python')
This program was called from the system command line.
__main__.py


-
If I import on the iPython command, I get:

In [71]: import newbie00
This program was imported on the Python command line.
newbie00.py

-
If I import *again* on the iPython command, I get:

In [72]: import newbie00

In [73]:



-
If I hit the run button (again) in Sypder, I get (in the iPython
command console):

In [73]: runfile(r'C:\engineer\engruser\python\newbie00.py', wdir=r'C:
\engineer\engruser\python')
UMD has deleted: newbie00
This program was called from the system command line.
__main__.py

---

Some questions:

1.  If running from the system command line, or the Sypder "run"
button, "__name__" is "__main__" rather than "newbie00", as seen
above.

So, how would I get the file name newbie00.py in these two noted
cases?  I mean, what other function do I use to get it?  (This
functionality is something that occasionally came in handy back in my
m-file writing days.  Perhaps I am wrong in anticipating such an
occasional need, but I would not know that yet.)

2.  In python, there seems to be a distinction between running
something as if it is a system command of "C:\...>python myPyFile.py"
compared to simply entering that same ".py" file name directly on the
python console command line.  In fact, the latter does not work unless
the somewhat lengthy ">>> runfile(r'C:\... wdir=r'C:\...) stuff is
entered (in iPython).  (I mean, my old MATLAB habit of simply entering
">> mfilename" on the command line seems to be quite wrong in python.)

Is there a shortened syntax of running a .py from the python command
prompt, if not using a Spyder "run" button?  Or should I always run as
if from the system prompt?  That is, dispense with the MATLAB-like
"run from MATLAB/python command line" bias I may be holding.

3.  In injecting my old MATLAB bias of running via the command line
">> mfilename", I tried a tweak of  ">>>import newbie00".  That "sort
of" worked, but only the first time.

Why did the subsequent run of ">>>import newbie00" print nothing?  I'm
just trying to understand how python works.

4.  The final case shown of hitting the Spyder run button included
this:

UMD has deleted: newbie00

What does that mean?  I noted that after this "automatic" deletion, I
could do the ">>>import newbie00" once again and get the print.  (I
did not show that above.)

5.  I think #4 implies an import can be removed.  (Yes/No?)  I am not
sure why that would be desired, but I will ask how to remove an
import, or to "refresh" the run, of that is the appropriate question.

I think I saw someplace where a .pyc file is created on an initial run
and subsequently run instead of the .py.  I'm not sure if that applies
here, but if related, I guess an auxiliary question is how to easily
force the .py to run rather than the .pyc?

6.  Perhaps peripherally related to getting a running script/function/
module name, is getting a "call listing" of all the functions (and
modules) called by a .py program.  How would I get that?  I only ask
as it comes in handy if one distributes a program.  I mean, you only
give people what they actually need.

---
Advance thanks to any one who answers any of my questions.  I am sure
they appear from misguided to rudimentary in character.  I'm don't
claim to be a programmer; I did okay with MATLAB.  I'm trying to
bootstrap here, and appreciate any help.

Thanks!
Greg




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


bash/shell to python

2012-05-16 Thread Rita
Hello,

I currently build a lot of interfaces/wrappers to other applications using
bash/shell. One short coming for it is it lacks a good method to handle
arguments so I switched to python a while ago to use 'argparse' module. Its
a great complement to subprocess module. I was wondering if there is a
generic framework people follow to build python scripts which are replacing
shell scripts? Is there a guide or a template to follow?





-- 
--- Get your facts first, then you can distort them as you please.--
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cPython, IronPython, Jython, and PyPy (Oh my!)

2012-05-16 Thread Ethan Furman

Chris Angelico wrote:

On Thu, May 17, 2012 at 9:01 AM, Ethan Furman  wrote:

A record is an interesting critter -- it is given life either from the user
or from the disk-bound data;  its fields can then change, but those changes
are not reflected on disk until .write_record() is called;  I do this
because I am frequently moving data from one table to another, making
changes to the old record contents before creating the new record with the
changes -- since I do not call .write_record() on the old record those
changes do not get backed up to disk.


I strongly recommend being more explicit about usage and when it gets
written and re-read, rather than relying on garbage collection.
Databasing should not be tied to a language's garbage collection.
Imagine you were to reimplement the equivalent logic in some other
language - could you describe it clearly? If so, then that's your
algorithm. If not, you have a problem.


Yeah, I've been thinking about this for a couple hours now;  initially 
(way back when) I didn't want to keep hitting the disk unnecessarily 
-- but all my other supporting data structures go to great lengths to 
not keep records in memory unless the user has them explicitly named or 
contained... I think I've been fighting against myself!  Good news is 
I'm winning.  ;)


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


Where is the most recent Tkinter information

2012-05-16 Thread Mark R Rivet
It seems like all the info on tkinter is around the 2000 time frame.
Is tkinter still being developed/supported?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where is the most recent Tkinter information

2012-05-16 Thread Terry Reedy

On 5/16/2012 11:02 PM, Mark R Rivet wrote:

It seems like all the info on tkinter is around the 2000 time frame.
Is tkinter still being developed/supported?


tkinter is CPython's tk interface. tcl/tk is still being developed at 
Active State. The Windows release for Py 3.3 will come with 8.5.11, 
released a couple of months ago. tkinter does not change much because tk 
does not change much. But bugs are fixed as reported. A fix for an 
obscure bug affecting certain *nix systems was committed just today. The 
docs do need improving.


--
Terry Jan Reedy

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


Re: Are there any instrumentation widgets for wxpython or tkinter?

2012-05-16 Thread Mark R Rivet
On Sat, 12 May 2012 12:40:28 -0700 (PDT), Sverre
 wrote:

>I searched for widgets used for PLC automation or lab instrumentation
>like gauges, led's etc. in the net, but didn't found anything because
>of those massive link spam sites. In the case there isn't any
>solution, with which toolkit would it be easiest to build gauges?

I just skimmed through the book "Python and Tkinter" by John E.
Grayson. This book goes through everything I think you will need for
instrumentation type widgets. It even shows how to create your own new
widgets. A lot of information in this book. Its dated, but there isn't
any other book available. But try some of the code, see how it works.
I know I'm going to.
-- 
http://mail.python.org/mailman/listinfo/python-list


Python and Tkinter by John E Grayson

2012-05-16 Thread Mark R Rivet
 I have a copy of this book and was wondering how relevant the content
is considering the publish date is 2000. Are people still using this
information? Anyone have any experience with this book? I guess what I
mean, is, any of the code in this book deprecated? or does it still
contain information used today the same as then. I mean, I guess some
things don't change right? For instance,  the derivative of x^2 is 2x
now and in the 1800's.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where is the most recent Tkinter information

2012-05-16 Thread Simon Cropper

On 17/05/12 13:02, Mark R Rivet wrote:

It seems like all the info on tkinter is around the 2000 time frame.
Is tkinter still being developed/supported?


The main page of the python 3.2.3 documentation for tkinter can be found 
here... 
http://docs.python.org/py3k/library/tkinter.html?highlight=tkinter#tkinter 
it is dated 2012.


This seems to be duplicated from the ActiveState site here...
http://docs.activestate.com/activepython/3.1/python/library/tkinter.html 
also dated 2012.


Here are some other resources I have stumbled on...

http://www.tutorialspoint.com/python/python_gui_programming.htm

http://infohost.nmt.edu/tcc/help/pubs/tkinter/

http://www.tkdocs.com/tutorial/index.html

http://www.ibm.com/developerworks/linux/library/l-tkprg/

--
Cheers Simon

   Simon Cropper - Open Content Creator / Website Administrator

   Free and Open Source Software Workflow Guides
   
   Introduction   http://www.fossworkflowguides.com
   GIS Packages   http://www.fossworkflowguides.com/gis
   bash / Pythonhttp://www.fossworkflowguides.com/scripting
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie questions on import & cmd line run

2012-05-16 Thread Terry Reedy

On 5/16/2012 9:45 PM, gwhite wrote:

Hi,

I am a newbie running the latest pythonxy (2.7.2.1)&  spyder and
python 2.7.2.   I suspect my questions are mostly basic to python, and
not specific to Spyder or iPython.

Note: Up until now, I mainly used MATLAB, and thus need to de-program
myself appropriately.

I use Win7-64.

I wrote the following .py file:

-
#!
# Filename: newbie00.py

if __name__ == '__main__':
 print 'This program was called from the \
system command line.'
 print __name__ + '.py'
else:
 print 'This program was imported on the \
Python command line.'
 print __name__ + '.py'

-

If I run from the system (win cmd) command, I get:

C:\engineer\engruser\python>python  newbie00.py

This program was called from the system command line.
__main__.py

-
If I hit the run button in Sypder, I get (in the iPython command
console):

In [70]: runfile(r'C:\engineer\engruser\python\newbie00.py', wdir=r'C:
\engineer\engruser\python')
This program was called from the system command line.
__main__.py


-
If I import on the iPython command, I get:

In [71]: import newbie00
This program was imported on the Python command line.
newbie00.py

-
If I import *again* on the iPython command, I get:

In [72]: import newbie00

In [73]:




Read the doc for the import command. It only runs code when needed to 
create the module to import. When you import again, you re-import the 
existing module.



If I hit the run button (again) in Sypder, I get (in the iPython
command console):

In [73]: runfile(r'C:\engineer\engruser\python\newbie00.py', wdir=r'C:
\engineer\engruser\python')
UMD has deleted: newbie00


This output is Spyder specific. When you run from an Idle window, it prints
== RESTART 
which means that the user main namespace is reset. Idle usually does 
this by starting a new user subprocess (and terminating the old one), 
but the alternative would be to try to clean the existing subprocess by 
reversing and resetting everything that was done. Wiping out everything 
and restarting is easier ;-).



1.  If running from the system command line, or the Sypder "run"
button, "__name__" is "__main__" rather than "newbie00", as seen
above. [how to get latter]


# Running in 3.3 Idle from edit window: tem.py
print(dir())
print(__file__)
# output in shell window
['__builtins__', '__doc__', '__file__', '__name__', '__package__']
F:\Python\mypy\tem.py

os.path.something() can isolate the last part in a cross-platform manner.


2.  In python, there seems to be a distinction between running
something as if it is a system command of "C:\...>python myPyFile.py"


You use dos syntax in the dos command prompt window.


compared to simply entering that same ".py" file name directly on the
python console command line.


You use python syntax in the interactive python console. The Idle Shell 
closes simulates that. Other shells add extra syntax that only works in 
the specific shell.


> In fact, the latter does not work unless

the somewhat lengthy ">>>  runfile(r'C:\... wdir=r'C:\...) stuff is
entered (in iPython).  (I mean, my old MATLAB habit of simply entering
">>  mfilename" on the command line seems to be quite wrong in python.)


I believe runfile is ipython (?) specific.

>>> exec(open("F:\\Python\\mypy\\tem.py").read())
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 
'__warningregistry__']

F:\Python\mypy\tem.py

It is relatively unusual to run a file in the main space rather than 
importing it.



5.  I think #4 implies an import can be removed.  (Yes/No?)


Yes, but it is not a newbie thing to do, and it often does not do what 
people expect or want. My advice: forget about it.



I think I saw someplace where a .pyc file is created on an initial run
and subsequently run instead of the .py.


A .pyc file is only generated when the .py is imported. But this is 
unreleated to the above and a CPython-specific behind-the-scenes time 
optimization you should not worry about.



here, but if related, I guess an auxiliary question is how to easily
force the .py to run rather than the .pyc?


You do not want to do that. If the .py is changed, the .pyc is 
regenerated on the next import.



6.  Perhaps peripherally related to getting a running script/function/
module name, is getting a "call listing" of all the functions (and
modules) called by a .py program.  How would I get that?  I only ask
as it comes in handy if one distributes a program.  I mean, you only
give people what they actually need.


Look at the trace module. Of course, you distribute files, not 
functions. There are various packaging programs that do a module import 
trace rather than function call trace.


--
Terry Jan Reedy

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


Re: Newbie questions on import & cmd line run

2012-05-16 Thread Chris Rebert
On Wed, May 16, 2012 at 6:45 PM, gwhite  wrote:
> Hi,
>
> I am a newbie running the latest pythonxy (2.7.2.1) & spyder and
> python 2.7.2.   I suspect my questions are mostly basic to python, and
> not specific to Spyder or iPython.
>
> Note: Up until now, I mainly used MATLAB, and thus need to de-program
> myself appropriately.
>
> I use Win7-64.
>
> I wrote the following .py file:
>
> -
> #! 

That's a shebang line. See http://en.wikipedia.org/wiki/Shebang_(Unix)
It's doesn't matter at all since you're on Windows. On Unix-like
systems, one typically writes:
#!/usr/bin/env python

> # Filename: newbie00.py
>
> if __name__ == '__main__':

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


Algorithms in Python, #n+1

2012-05-16 Thread Antti J Ylikoski

I have continued my research in literature algorithms in Python.  The
algorithms in Knuth's volumes 1 -- 3 either have been incorporated
into Python, or they can be easily implemented with Python.  Quite as
John Nagle said here.  However, the Fascicles in Vol. 4 to my opinion
contain nontrivially useful material.

And, Vol. 2 has something interesting, however -- e. g. the statistical
tests on random number generators.

I have included in this entry a working Python program which carries
out the chi squared test on different random number generators.  It
demonstrates the paradigm of building Knuth's equations directly into
one's Python program, instead of the DFA/FSA approach.  Moreover,
someone over there may wish to test his/her random number generators.
Therefore, I feel that it is relevant to show this program (which is
about two pages long) in this context.

I have carried out the chi squared test on Python's own random number
generator, which to my knowledge is based on the Mersenne twister:

http://en.wikipedia.org/wiki/Mersenne_twister

and Knuth's (old) Linear Congruential method:

http://en.wikipedia.org/wiki/Linear_congruential_generator

If you run the program (with Python 3) you will see that the both
generators will pass the chi squared test.



# Random number generation and analysis after D. E. Knuth.
#
# See The Art of Computer Programming, VOL 2, Seminumerical Algorithms,
# 3rd Edition, pp. 1 --
#
# AJY 05-16-2012.

import math, random

# First do the MIX simulator.
# AJY 05-16-2012.

# -

# Python based MIX simulator.
#
# Written in the object oriented manner.
#
# AJY 05-16-2012.
#
# Do as follows:
#
# MIX_computer = MIXSIM(memory_size, word_size)
# A, X = MIX_computer.MUL(A, X)
#
# etc etc. in the same vein.
# This constitutes just a maximally simple demo.


class MIXSIM(object):
""" Maximally simple object oriented MIX simulator. """
def __init__(self, mem_size, word_size):
self.mem_size  = mem_size
self.word_size = word_size
self.w = 10**word_size # Assume a base 10 computer, 
after Knuth.


# No other methods in this class.  I said it only constitutes a demo.


# --

class LCRNG(object):
""" The Linear Congruential Random Number Generator. AJY 
05-16-2012. """


def __init__(self, modulus, multiplier, increment, seed):
self.modulus= modulus# Modulus == m in Knuth's text.
self.multiplier = multiplier # Multiplier == a in Knuth's text.
self.increment  = increment  # Increment == c in Knuth.
 # Seed == X0 in Knuth.
self.reg_X  = seed   # Initial value X0 to Register X.
self.MIX= MIXSIM(5000, 12) # Demo sample MIX computer.

def rand(self):
# See Knuth VOL 2 on p. 12.

# Following calculate the next pseudo random integer:
self.reg_A = (self.multiplier * self.reg_X) % (self.MIX.w + 1)

# Transfer A to X for the next round.
self.reg_X = self.reg_A

# And now return a floating point number x such that 0 <= x < 1.
return float(self.reg_A) / float (self.modulus)


class chi_squared_test(object):
""" Instances are chi squared tests of random number generators. """

def __init__(self, generator_f, buckets, iterations, name):
self.generator_f = generator_f  # The function to be tested.
self.buckets = buckets  # How many discrete buckets are 
used
self.iterations  = iterations   # The # of random numbers 
generated.

self.name= name # name of this test.
self.B = [0] * self.buckets # initially all buckets are empty

def run_test(self):
""" Generate random numbers and place them in buckets. """

for i in range(self.iterations):
random_nr = self.generator_f() # Random number, in interval 
[0, 1)
ind_bucket = int(self.buckets * random_nr) # Index, 0 .. 
#buckets-1

self.B[ind_bucket] += 1

def chi2test(self):
""" Carry out the chi squared test of the material generated. """

 # Calculate the chi squared statistics value.  See Knuth VOL 2
 # on Page 43.

Vsum = 0
for i in range(self.buckets):
Vsum += (self.B[i] - self.iterations * (1.0 / 
float(self.buckets)))**2 \

/ (self.iterations * (1.0 / float(self.buckets)))

V = Vsum

print("\n Chi squared test.  Name = ", self.name)
print("\n Chi squared value V = ", V)
print("\n Degrees of freedom:   ", self.buckets - 1)

chi2_1  = (self.buckets - 1) + math.sqrt(2.0 * self.buckets) \
  * -2.33 + (2.0/3.0)*(-2.33)**2 - (2.0/3.0)

chi2_99 = (self.buckets - 1) + math.sqrt(2

Re: Newbie questions on import & cmd line run

2012-05-16 Thread Steven D'Aprano
On Wed, 16 May 2012 18:45:39 -0700, gwhite wrote:

> #! 
> # Filename: newbie00.py

"Supposed to"? Nothing -- it is completely optional.

#! ("hash-bang") lines currently do nothing on Windows machines, they are 
just comments. However, on Unix and Linux machines (and Macintosh?) they 
are interpreted by the shell (equivalent to cmd.exe or command.com), in 
order to tell the shell what interpreter to use to execute the program if 
you run it directly. It is common to use something like:

#!/usr/bin/env python

but I stress that this is completely optional, and doesn't do anything on 
Windows.


> if __name__ == '__main__':
> print 'This program was called from the \
> system command line.'
> print __name__ + '.py'
> else:
> print 'This program was imported on the \
> Python command line.'
> print __name__ + '.py'
> 
> -
> 
> If I run from the system (win cmd) command, I get:
> 
> C:\engineer\engruser\python>python  newbie00.py
> 
> This program was called from the system command line. __main__.py

The magic variable "__name__" is special in Python. When you run a Python 
module as a script from the command line, it gets set to "__main__". Note 
that there is no such file "__main__.py" (unless you have happened to 
create one yourself).

When you import a module, rather than run it, __name__ gets set to the 
actual filename of the module, minus the file extension.


> -
> If I hit the run button in Sypder, I get (in the iPython command
> console):
> 
> In [70]: runfile(r'C:\engineer\engruser\python\newbie00.py', wdir=r'C:
> \engineer\engruser\python')
> This program was called from the system command line. __main__.py

I'm not sure what Spyder is. Is it part of iPython? You may need to 
consult the iPython or Spyder docs to find out exactly what tricks it 
plays in its interactive console.


> -
> If I import on the iPython command, I get:
> 
> In [71]: import newbie00
> This program was imported on the Python command line. newbie00.py

In this case, __name__ is set to the module name (the file name less the 
file extension). Your script adds the .py at the end.


> -
> If I import *again* on the iPython command, I get:
> 
> In [72]: import newbie00
> 
> In [73]:
> 
> 

That is correct. Python modules are only executed *once*, the first time 
they are imported. From then on, additional imports refer back to a 
cached module object.

When you say "import newbie00", the (highly simplified!) process is this:

* Python looks in sys.modules for the name "newbie00". If it finds
  something in the cache (usually a module object), it fetches that thing
  and assigns it to the variable "newbie00", and the import process is
  complete.

* But if it doesn't find anything in the cache, Python searches the
  locations listed in sys.path for a module, package, or library. That
  could mean any of:

  - newbie00.py   (source code)
  - newbie00.pyc  (compiled byte-code)
  - newbie00.pyo  (compiled optimized byte-code)
  - newbie00.pyw  (Windows only)
  - newbie00.dll  (Windows only C library)
  - newbie00.so   (Linux and Unix C library)

  as well as others (e.g. packages).

* If no module is found, Python raises an error, otherwise the first
  found module is used.

* If a compiled module (e.g. newbie00.pyc) is found, and is no older than
  the source code (newbie00.py), then Python uses the pre-compiled file.

  (If the compiled module is older than the source module, it is ignored.)

* Otherwise Python parses the newbie00.py source code, compiles it to
  byte-code, and writes it to the file newbie00.pyc so that the next time
  the import will be faster.

* At this point, Python now has a compiled chunk of Python byte-code. 
  It then sets the special global variable __name__ to the file name (less
  extension), and executes that code.

* If no fatal error occurs, Python now bundles the results of the
  executed code (any functions, classes, variables, etc.) into a module
  object, stores the module object in the cache sys.modules for next time,
  and finally assigns it to the name "newbie00".


There is a *lot* that goes on the first time you import a module, which 
is why Python tries really hard to avoid running modules unless you 
explicitly ask it to. So "import newbie00" only *executes* the code once 
per Python session. Subsequent imports use the cached version.

The process is quite different when you run a Python module as a script. 
In this case, the .pyc file (if any) is ignored, the script is parsed and 
compiled from scratch every single time, the magic variable __name__ is 
set to "__main__", and the script is executed every single time.


[...]
> Some questions:
> 
> 1.  If running from the system command line, or the Sypder "run" button,
> "__name__" is "__main__" rather than "newbie00", as seen above.
> 
> So, how would I get the file name newbie00.py in these two noted cases? 

The special variable "__file__" is set to the f

Re: Newbie questions on import & cmd line run

2012-05-16 Thread alex23
On May 17, 11:45 am, gwhite  wrote:
> 1.  If running from the system command line, or the Sypder "run"
> button, "__name__" is "__main__" rather than "newbie00", as seen
> above.
>
> So, how would I get the file name newbie00.py in these two noted
> cases?

You can get it from the file name:

import os.path
name = os.path.split(__file__)[-1]

However, you might find it better in the long term to always separate
your code into import-only modules and execute-only scripts. It avoids
this issue and others.

> 2.  Is there a shortened syntax of running a .py from the python command
> prompt, if not using a Spyder "run" button?  Or should I always run as
> if from the system prompt?  That is, dispense with the MATLAB-like
> "run from MATLAB/python command line" bias I may be holding.

Generally, the correct way of running Python code from the interpreter
is 'import '. You might find this pattern useful:

In your module:

def main(): # code goes here

if __name__ == '__main__': main()

Then from the interpreter:

import mymodule; mymodule.main()

> 3.  In injecting my old MATLAB bias of running via the command line
> ">> mfilename", I tried a tweak of  ">>>import newbie00".  That "sort
> of" worked, but only the first time.
>
> Why did the subsequent run of ">>>import newbie00" print nothing?  I'm
> just trying to understand how python works.

The import mechanism only imports a module once, as all files use the
same module instance (they're effectively singletons). This happens
with repeated imports in one module as well as across various modules
during a single execution. So re-importing a module does nothing; to
force a re-import, you can use the reload() function, although that
doesn't guarantee updating all references. For example:

import mymodule
from mymodule import myfunc

# modify myfunc code externally

reload(mymodule)
myfunc() # original reference
mymodule.myfunc()  # newly modified function

I don't think that only-one-import is true for scripts that are run
from the command line, though. They can exist as both '__main__' and
their actual name in the module table. (Someone please correct me if
this understanding is wrong...)

> 4.  The final case shown of hitting the Spyder run button included
> this:
>
> UMD has deleted: newbie00
>
> What does that mean?  I noted that after this "automatic" deletion, I
> could do the ">>>import newbie00" once again and get the print.  (I
> did not show that above.)

Spyder provides a convenience feature to force the reimport of user-
defined modules, like your newbie00. After execution, it appears to
drop all references to your module, forcing a garbage collection. As
its no longer loaded, a subsequent import works.

> 5.  I think #4 implies an import can be removed.  (Yes/No?)  I am not
> sure why that would be desired, but I will ask how to remove an
> import, or to "refresh" the run, of that is the appropriate question.

reload()

However, the only way to guarantee you've updated all references
correctly is to close the interpreter and re-start it. For this kind
of development process of modifying code and seeing the changes, it's
probably better to look into writing tests instead. These will always
be run in isolation, so you're guaranteed of having the correct
environment each time.

> I think I saw someplace where a .pyc file is created on an initial run
> and subsequently run instead of the .py.  I'm not sure if that applies
> here, but if related, I guess an auxiliary question is how to easily
> force the .py to run rather than the .pyc?

A .pyc file won't be created for .py files that are run directly, only
for those that are imported. You really shouldn't need to worry about
this, though. It's an implementation detail that isn't influencing the
issues you're seeing. However, if you do ever need to do it, you can
stop .pyc files by passing the -B flag to the interpreter, or by
setting the environment variable PYTHONDONTWRITEBYTECODE.

> 6.  Perhaps peripherally related to getting a running script/function/
> module name, is getting a "call listing" of all the functions (and
> modules) called by a .py program.  How would I get that?  I only ask
> as it comes in handy if one distributes a program.  I mean, you only
> give people what they actually need.

Perhaps this might be of use: http://pycallgraph.slowchop.com/

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