Re: Why should __prepare__ be explicitly decorated as a @classmethod?

2014-05-18 Thread Chris Angelico
On Sun, May 18, 2014 at 4:26 PM, Shriramana Sharma  wrote:
> https://docs.python.org/3/reference/datamodel.html#basic-customization 
> documents that __new__ is special-cased so that while it is actually a static 
> method, it need not be decorated as such. I have a similar question. IIUC 
> __prepare__ is always a class method to be used in metaclasses, so why isn't 
> it also special-cased so that it need not be decorated as a such?

Special cases aren't special enough to break the rules. The less
special cases a language has, the more likely it is to fit inside your
brain, and that's a good thing.

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


Re: using a new computer and bringing needed libraries to it

2014-05-18 Thread Ben Finney
Rustom Mody  writes:

> On Sunday, May 18, 2014 5:47:05 AM UTC+5:30, Ned Batchelder wrote:
> > Make a list of the [Python-specific] packages you need. Put it in a
> > file called requirements.txt. […]
>
> What about things installed at a lower level than pip, eg apt-get?

That's an important issue. Requirements of a project, such as packages
that need to be installed from the operating system (e.g. “you need
Python 3.2 or later for this project”), are ideal for documenting in
plain human-targeted text in a document called “requirements.txt”.

Which is why I advocate using a *different* filename, more explicit
about its special purpose (e.g. ‘pip_requirements’), for the
Pip-specific (and thereby Python-specific) machine-readable
configuration file.

-- 
 \   “But Marge, what if we chose the wrong religion? Each week we |
  `\  just make God madder and madder.” —Homer, _The Simpsons_ |
_o__)  |
Ben Finney

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


Re: Python and Math

2014-05-18 Thread Roy Smith
In article <53783c5f$0$29977$c3e8da3$54964...@news.astraweb.com>,
 Steven D'Aprano  wrote:

> You may find that the IPython interactive interface to Python is useful. 
> It presents an interface which should be familiar to anyone with 
> experience with Mathematica. 

I second the IPython suggestion.  I don't use it that often, but when 
I'm doing interactive number crunching, it's my tool of choice.  The 
ability to interactively go back and edit some block of code, then 
re-execute it, is really handy when exploring a dataset.  And the tight 
integration of graphing/plotting libraries is awesome.

I tend to use it in the mode where I'm running the compute kernel on a 
remote machine (typically a big machine in a data center somewhere) and 
the display portion in a browser on my desktop.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python and Math

2014-05-18 Thread Wolfgang Keller
> Does Python have good mathematical capabilities? 

SAGE: http://www.sagemath.org/

Sincerely,

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


Re: Python and Math

2014-05-18 Thread Mark Lawrence

On 18/05/2014 14:25, Roy Smith wrote:

In article <53783c5f$0$29977$c3e8da3$54964...@news.astraweb.com>,
  Steven D'Aprano  wrote:


You may find that the IPython interactive interface to Python is useful.
It presents an interface which should be familiar to anyone with
experience with Mathematica.


I second the IPython suggestion.  I don't use it that often, but when
I'm doing interactive number crunching, it's my tool of choice.  The
ability to interactively go back and edit some block of code, then
re-execute it, is really handy when exploring a dataset.  And the tight
integration of graphing/plotting libraries is awesome.



Big +1 from me.

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


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: Python and Math

2014-05-18 Thread Grant Edwards
On 2014-05-18, Bill Cunningham  wrote:

> Does Python have good mathematical capabilities?

No.

It has very good numerical computation capabilities, but it does not
really do "math" (at least not what a mathemetician would consider
"math").

> I am interested in learning a second language for mathematical
> purposes.

If you want to do calculations on numbers (integral, real, complex,
vectors, matrixes), analyze and visualize data (stuff that was
traditionally done in Fortran), then Python is brilliant.

If you want to do "math" (as in the study of number theory,
topologies, proofs, and so on) then no.  Python is not a good choice.

-- 
Grant


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


Re: Python and Math

2014-05-18 Thread Bill Cunningham

"Grant Edwards"  wrote in message 
news:llak9u$8rs$1...@reader1.panix.com...
> On 2014-05-18, Bill Cunningham  wrote:
>
>> Does Python have good mathematical capabilities?
>
> No.
>
> It has very good numerical computation capabilities, but it does not
> really do "math" (at least not what a mathemetician would consider
> "math").
>
>> I am interested in learning a second language for mathematical
>> purposes.
>
> If you want to do calculations on numbers (integral, real, complex,
> vectors, matrixes), analyze and visualize data (stuff that was
> traditionally done in Fortran), then Python is brilliant.
>
> If you want to do "math" (as in the study of number theory,
> topologies, proofs, and so on) then no.  Python is not a good choice.

linear algebra, expanding and factoring equations of all degrees. 
Geometry.

Bill


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


Re: Python and Math

2014-05-18 Thread Robert Kern

On 2014-05-18 16:40, Grant Edwards wrote:

On 2014-05-18, Bill Cunningham  wrote:


 Does Python have good mathematical capabilities?


No.

It has very good numerical computation capabilities, but it does not
really do "math" (at least not what a mathemetician would consider
"math").


Many mathematicians would disagree.

  http://sagemath.org/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Can't figure out 'instance has no attribute' error

2014-05-18 Thread varun7rs
On Sunday, 18 May 2014 01:56:42 UTC+2, varu...@gmail.com  wrote:
> Hello Friends,
> 
> 
> 
> I am working on this code but I kind of get the same error over and over 
> again. Could any of you help me fix this part of the error?
> 
> 
> 
> File RW1:
> 
> class PHY_NETWORK:
> 
> def __init__(self, nodes, edges):
> 
> self.nodes = nodes
> 
> self.edges = edges  
> 
> 
> 
>   def addNode(self, node):
> 
>   self.nodes.append( node )
> 
> 
> 
>   def addEdge(self, edge):
> 
>   self.edges.append( edge )
> 
> 
> 
> File RW3:
> 
> def createnetwork(graph):
> 
> 
> 
>   
> 
>   doc = parse( args.paramFile )
> 
>   noderef = []
> 
>   num = 0
> 
>   nodelist = doc.getElementsByTagName("node")
> 
>   for node in nodelist:
> 
>   noderef.append(node.getAttribute("id"))
> 
>   proc = random.randint(3500, 5000)
> 
>   stor = random.randint(7200, 8200)
> 
>   switch = random.randint(7000, 1)
> 
>   num = num + 1
> 
>   totaldemands = random.randint(1, 5)
> 
>   graph.addNode(PHY_NODES( node.getAttribute("id"), int(num), 
> float(xCoordinates.firstChild.data), float(yCoordinates.firstChild.data), 
> float(proc), float(stor), float(switch), int(totaldemands)))
> 
>   
> 
>   
> 
>   linkid = 0
> 
>   linklist = doc.getElementsByTagName("link")
> 
>   for link in linklist :
> 
>   linkid = linkid + 1
> 
>   Source = link.getElementsByTagName("source") [0]
> 
>   Destination = link.getElementsByTagName("target") [0]
> 
>   Capacity = link.getElementsByTagName("capacity") [0]
> 
>   SourceID = noderef.index(Source.firstChild.data) + 1
> 
>   DestinationID = noderef.index(Destination.firstChild.data) + 1
> 
>   graph.addEdge( PHY_LINKS( linkid, Source.firstChild.data, 
> Destination.firstChild.data, DestinationID, SourceID, 
> float(Capacity.firstChild.data) ))
> 
> 
> 
> 
> 
>   global args
> 
>   args = parser.parse_args()
> 
> 
> 
>   samplenetwork = PHY_NETWORK([], [])
> 
>   for i in range(1, 100):
> 
>   createnetwork(samplenetwork)
> 
>   exportXmlFile(samplenetwork, args.output, 'a' ) 
> 
>   
> 
>
> 
> if __name__ == "__main__":
> 
>   main(sys.argv[1:])
> 
> 
> 
> 
> 
> srva@hades:~$ python RW3.py --output topology.xml --xml germany50.xml
> 
> Traceback (most recent call last):
> 
>   File "RW3.py", line 157, in 
> 
> main(sys.argv[1:])
> 
>   File "RW3.py", line 152, in main
> 
> createnetwork(samplenetwork)
> 
>   File "RW3.py", line 31, in createnetwork
> 
> graph.addNode(PHY_NODES( node.getAttribute("id"), int(num), 
> float(xCoordinates.firstChild.data), float(yCoordinates.firstChild.data), 
> float(proc), float(stor), float(switch), int(totaldemands)))
> 
> AttributeError: PHY_NETWORK instance has no attribute 'addNode'
> 
> 
> 
> 
> 
> The error that it give back is as above. I have the classes defined in RW1 
> file and I am importing the classes onto RW3 file and yet, It doesn't seem to 
> work. I really am in need of your valuable suggestions. Thank You

Thank you very much Ned, Rodri and Gary. I changed the settings of gedit text 
editor as mentioned in the Zed Shaw tutorial. I think this is causing me the 
problem. I'll follow your advice.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can't figure out 'instance has no attribute' error

2014-05-18 Thread Chris Angelico
On Mon, May 19, 2014 at 5:02 AM,   wrote:
> Thank you very much Ned, Rodri and Gary. I changed the settings of gedit text 
> editor as mentioned in the Zed Shaw tutorial. I think this is causing me the 
> problem. I'll follow your advice.
>

I find that there are better editors than gedit. My personal
preference is SciTE; one of its syntax highlighting features is a nice
big warning if the indentation doesn't make sense from one line to the
next. So if you switch from spaces to tabs, or mismatch indents, or
anything, it gives you this blue marker right through the faulty
indented section. Pick an editor with that kind of feature, and you'll
find your job way easier.

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


Re: Loading modules from files through C++

2014-05-18 Thread Roland Plüss

On 05/17/2014 07:05 PM, Stefan Behnel wrote:
> Roland Plüss, 17.05.2014 18:28:
>> On 05/17/2014 05:49 PM, Stefan Behnel wrote:
>>> Roland Plüss, 17.05.2014 17:28:
 On 05/17/2014 04:01 PM, Stefan Behnel wrote:
> Roland Plüss, 17.05.2014 15:49:
>> On 05/17/2014 03:26 PM, Stefan Behnel wrote:
>>> Roland Plüss, 17.05.2014 15:00:
 On 05/17/2014 01:58 PM, Stefan Behnel wrote:
> Roland Plüss, 17.05.2014 02:27:
>> I'm using Python in an embedded situation. In particular I have to 
>> load
>> python scripts through a memory interface so regular python module
>> loading can not be used. I got working so far a module loader object
>> I've added using C++ to sys.meta_path . Now I'm totally stuck at the
>> finally loading step.
>>
>> I've got this a C++ loader method "load_module(fullname)" which does
>> load the requested module script files into a null-terminated 
>> string. I
>> know that "load_module" has to return the module PyObject*. But I 
>> can't
>> get the python source in the c-string into a module PyObject*.
>> [...]
>> Can anybody help how in gods name one is supposed to create a module
>> from an in-memory c-string when called from within load_module (or
>> anywhere)?
> Looks like you want to implement a SourceLoader:
>
> https://docs.python.org/3.4/library/importlib.html#importlib.abc.SourceLoader
>
> I recommend implementing this in Python code instead of C code, 
> though.
> Much easier. Cython can help with the integration between both.
 That doesn't work in 2.x, doesn't it?
>>> Is there a reason you have to use Py2?
>>>
>>> Anyway, PEP 302 predates Py3 by a couple of years:
>>>
>>> http://legacy.python.org/dev/peps/pep-0302/
>> I'm willing to go to Py3 but only if the solution to the problem is
>> simpler than getting it fixed in Py2. So some questions first:
>>
>> - does this importlib stuff you showed there apply to C++ land (I need
>> to fully drive it from C++ not Python code)?
> As I said, implementing this in Python code is much simpler than doing it
> in C/C++ code. Basically, stop where you got the C string and do the rest
> in Python. All your C code has to do is to take a module lookup request
> from your custom Python module Finder and return a byte string with the
> code. Then let your Python code wrap that in a Loader and return it to the
> import machinery.
 I don't get how this is supposed to work. I'm running it as fully
 embedded Python. There is no main script. The builtin modules are added
 as C++ bound classes and a user made main script is loaded but not run
 directly (I'm hooking into a create object). For this purpose I load the
 script module using C++ code using PyImport_ImportModule(moduleName). At
 this time the module loading code has to kick in already (I've added
 this one by C++ too before). The problem is now that in this call I end
 up in my C++ loader version where there is no Python script involved. I
 came to the conclusion that I can solve this only by having the C++ end
 properly load the module. I could add Python code with
 PyRun_SimpleString but then I'm down to the same problem as before: how
 to evaluate code so it is attached to a module or type-class? As I
 understand it the problem is the same as before just pushed around a bit.
>>> No, just run some Python code (using PyRun_SimpleString() if you have to)
>>> and let it do whatever you like. Such as, defining a Finder class and
>>> injecting it into the import hook. Just provide it with the entry point of
>>> your C++ loader as a (CFunction) object when you execute it, and then let
>>> it call that function at need whenever the Finder gets executed.
>>>
>>> Alternatively, compile your Python integration code with Cython and link it
>>> into your main program as yet another binary extension module.
>> I'm not using Cython so that's out of question. Concerning the injection
>> how would this work? From the PEP I assume it would have to look like this:
>>
>> # CODE #
>> import sys
>> class VFSModuleLoader:
>>def find_module(fullname, path=None):
>>   return self if VFS.exists( vfsPathFromFullname(fullname) ) else None
>>def load_module(fullname):
>>   sourceCode = VFS.read( vfsPathFromFullname(fullname))
>>   ???
>> # CODE #
>>
>> How does ??? work? If I use "eval" I end up with the code inside
>> VFSModuleLoader.load_modules as context but it should go into the global
>> context as an own module.
> Yeah, well, the import machinery is rather badly exposed in Py2. This got
> much cleaner in Py3, especially 3.3/3.4.
>
> Here's a hacky way to do it in Py2:
>
> import imp
> module = imp.new_module(module_name)
> module.__importer__ = sel

[RELEASED] Python 2.7.7 release candidate 1

2014-05-18 Thread Benjamin Peterson
Greetings Python users,
Python 2.7.7 release candidate 1 is now available for download. Python
2.7.7 is a regularly scheduled bugfix release for the Python 2.7 series.
The 2.7.7 release contains fixes for two severe, if arcane, potential
security vulnerabilities. The first was the possibility of reading
arbitrary process memory using JSONDecoder.raw_decode. [1] (No other
json APIs are affected.) The second security issue is an integer
overflow in the strop module. [2] (If you don't know what the strop
module is, go ahead and forget it now.) This release also includes
months of accumulated normal bugfixes. All the changes in Python 2.7.7
are described in detail in the Misc/NEWS file of the source tarball. You
can view it online at

http://hg.python.org/cpython/raw-file/e32e3a9f3902/Misc/NEWS

Downloads are at

https://python.org/download/releases/2.7.7/

This is a testing release. Assuming no horrible bugs are found, 2.7.7
final will be released in two weeks time. Please consider testing your
applications and libraries with the release candidate and reporting bugs
to

http://bugs.python.org/

Enjoy,
Benjamin Peterson
2.7 Release Manager

[1] http://bugs.python.org/issue21529
[2] http://bugs.python.org/issue21530
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Loading modules from files through C++

2014-05-18 Thread Chris Angelico
On Mon, May 19, 2014 at 5:41 AM, Roland Plüss  wrote:
> This exec source_code in module.__dict__ , should this not also be doable
> with PyEval_EvalCode?

General principle: The more code you write in Python and the less in
C/C++, the happier and more productive you will be.

Drop into Python as soon as you can, and do all the work from there.
You won't have to worry about RAM (de)allocation, Unicode (especially
if you use Python 3 rather than 2), integer overflow, etc, etc, etc.
Only write lower-level code for the bits that actually demand it; and
as Stefan has pointed out, Cython is a great help there.

(Which reminds me. I still need some "excuse project" to justify my
learning Cython. It's good-looking tech but everything I can imagine
writing seems to already exist.)

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


bz2.decompress as file handle

2014-05-18 Thread Vincent Davis
I have a file compressed with bz2 and a function that expects a file handle.
When I decompress the bz2 file I get a string (binary) not a file handle.

Here is what I have that does not work. There is no error (thats a seperate
issue) CelFile.read just fails to read the data(string).

from Bio.Affy import CelFile
from bz2 import decompress,

with open('Tests/Affy/affy_v3_ex.CEL.bz2', 'rb') as handle:
cel_data = decompress(handle.read())

c = CelFile.read(cel_data)



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


Re: bz2.decompress as file handle

2014-05-18 Thread Tim Chase
On 2014-05-18 19:53, Vincent Davis wrote:
> I have a file compressed with bz2 and a function that expects a
> file handle. When I decompress the bz2 file I get a string (binary)
> not a file handle.

> from bz2 import decompress,
> 
> with open('Tests/Affy/affy_v3_ex.CEL.bz2', 'rb') as handle:
> cel_data = decompress(handle.read())

When I try (without the Bio.Affy which isn't part of the stdlib), I
get correct bytes from this:

tim@bigbox:~$ echo hello world > test.txt
tim@bigbox:~$ bzip2 -9 test.txt 
tim@bigbox:~$ python3
Python 3.2.3 (default, Feb 20 2013, 14:44:27) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from bz2 import decompress
>>> with open('test.txt.bz2', 'rb') as f:
... data = decompress(f.read())
... 
>>> data
b'hello world\n'


> c = CelFile.read(cel_data)

So either you have bad data in the file to begin with, or your
CelFile.read() function has a bug in it.

-tkc



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


Re: bz2.decompress as file handle

2014-05-18 Thread Vincent Davis
Well after posting, I think I figured it out.
The key is to use StringIO to get a file handle on the string. The fact
that it is binary just complicates it a little.

with open('Tests/Affy/affy_v3_ex.CEL.bz2', 'rb') as handle:
cel_data = StringIO(decompress(handle.read()).decode('ascii'))

Vincent Davis
720-301-3003


On Sun, May 18, 2014 at 8:32 PM, Tim Chase wrote:

> On 2014-05-18 19:53, Vincent Davis wrote:
> > I have a file compressed with bz2 and a function that expects a
> > file handle. When I decompress the bz2 file I get a string (binary)
> > not a file handle.
>
> > from bz2 import decompress,
> >
> > with open('Tests/Affy/affy_v3_ex.CEL.bz2', 'rb') as handle:
> > cel_data = decompress(handle.read())
>
> When I try (without the Bio.Affy which isn't part of the stdlib), I
> get correct bytes from this:
>
> tim@bigbox:~$ echo hello world > test.txt
> tim@bigbox:~$ bzip2 -9 test.txt
> tim@bigbox:~$ python3
> Python 3.2.3 (default, Feb 20 2013, 14:44:27)
> [GCC 4.7.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from bz2 import decompress
> >>> with open('test.txt.bz2', 'rb') as f:
> ... data = decompress(f.read())
> ...
> >>> data
> b'hello world\n'
>
>
> > c = CelFile.read(cel_data)
>
> So either you have bad data in the file to begin with, or your
> CelFile.read() function has a bug in it.
>
> -tkc
>
>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bz2.decompress as file handle

2014-05-18 Thread Ian Kelly
On Sun, May 18, 2014 at 8:38 PM, Vincent Davis  wrote:
> Well after posting, I think I figured it out.
> The key is to use StringIO to get a file handle on the string. The fact that
> it is binary just complicates it a little.
>
> with open('Tests/Affy/affy_v3_ex.CEL.bz2', 'rb') as handle:
> cel_data = StringIO(decompress(handle.read()).decode('ascii'))

You can just use bz2.open:

>>> with bz2.open('test.txt.bz2', 'rt', encoding='ascii') as f:
... print(f.read())
...
hello!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bz2.decompress as file handle

2014-05-18 Thread Vincent Davis
On Sun, May 18, 2014 at 9:44 PM, Ian Kelly  wrote:

> You can just use bz2.open:
>
> >>> with bz2.open('test.txt.bz2', 'rt', encoding='ascii') as f:
> ... print(f.read())
>

​Thanks I like that better then my solution.
​


Vincent Davis
720-301-3003
-- 
https://mail.python.org/mailman/listinfo/python-list


Copying files from sub folders under source directories into sub folders with same names as source directory sub folders in destination directories without overwriting already existing files of same n

2014-05-18 Thread satishmlwizpro
Hi,

Consider
/src/alias/a.c
/src/alias/b.c
/src/xml/p.xml
/src/xml/c.xml
/src/h.c
as source directory 
and 
/dest/alias
/dest/xml
/dest
as destination directory. These are given in a csv file like
/src/alias/a.c, /dest/alias
/src/alias/b.c, /dest/alias
/src/xml/p.xml, /dest/xml
/src/xml/c.xml, /dest/xml
/src/h.c, /dest
Python code should read csv file. Copy files from source to destination(files 
in /src/alias should be copied to /dest/alias and not to /dest/xml i.e., 
directory names should be the same). If /dest contains same files as /src, then 
they code should give us a warning and such files shouldn't be copied. Other 
files should only be copied.

Could you kindly help?
-- 
https://mail.python.org/mailman/listinfo/python-list