Re: Writing binary files in windows

2011-02-11 Thread Nick Raptis

On 02/12/2011 05:20 AM, Abhishek Gulyani wrote:

When I write binary files in windows:

file  = open(r'D:\Data.bin','wb')
file.write('Random text')
file.close()

and then open the file it just shows up as normal text. There is 
nothing binary about it. Why is that?


Sorry if this is too much of a noobie question. I tried googling 
around but couldn't find an answer.


But then, you opened the file you created in Notepad and saw that the 
lines weren't wrapping maybe?


It all comes down to Line endings. Read here to see what LF vs. CRLF is 
all about http://en.wikipedia.org/wiki/Newline#Representations


You see, when you read a file, Python converts both LF and CRLF to "\n". 
When you write a "\n" to a file Python writes CRLF in Windows and just 
LF in Unix machines.
That'd be a heck troublesome when dealing with binary files, hence the 
'b' flag. It basically means *Don't convert newlines", and it's pretty 
common in software.


So, to answer your question: If you're only writing text to a file, 
you'll only see a difference if your external text editor isn't smart 
enough to treat both LF and CRLF as newlines. Like Notepad did back in 
the day, and maybe still does.
But if you're writing binary stuff, it makes all the difference in the 
world.


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


Re: 'reload M' doesn't update 'from M inport *'

2010-07-09 Thread Nick Raptis



I know I have more radical options, such as starting
a new IDLE window. That would save me time, but
I'd like to take the opportunity to understand what
is happening. Surely someone out there knows.

Frederic

   

Or you can restart the IDLE shell with CTRL+F6.

If you can't restart it, you're probably not using an IDLE subprocess:

On linux, make sure the menu item doesn't use "-n" in the command
On Windows, first open IDLE, then open the file, or change the Open 
command to not use the "-n" flag.


Sorry to not chip in to your question, frankly, Steven nailed it!
Just thought to share an IDLE tip :)

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


Re: Hello

2010-07-09 Thread Nick Raptis

Hello Dani!

IDLE is very friendly for new users and has got me a long way when I was 
starting. You also can't beat that it comes bundled with Python.


I'd also like to suggest the Python-Tutor list 
http://mail.python.org/mailman/listinfo/tutor for your "new-user" questions.

Lots of helpful folk there, myself included.

Nick

On 07/09/2010 07:31 PM, Dani Valverde wrote:

Hello!
I am new to python and pretty new to programming (I have some 
expertise wit R statistical programming language). I am just starting, 
so my questions may be a little bit stupid. Can anyone suggest a good 
editor for python?

Cheers!

Dani


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


Re: iptcinfo: Can not import: Newbie not really knowing what he is doing

2010-07-12 Thread Nick Raptis

Hi Richard!

I have downloaded iptcinfo and placed it in python27\Lib\site-packages
\iptcinfo

I guessed that was the right place, because that is where PIL ended
up, but that had a fancy installer with it.

   
You did place it in the right path, but the "fancy installer" does one 
more thing, it registers the package into the python-path, so python can 
get to it.
I've long switched from windows unfortunately and can't remember the way 
it does this though, so you can do it manually.


Have some good news though: I looked in the IPTCinfo package and it does 
indeed include a setup.py, which is a standard way of installing python 
packages.

All you need to do is unpack the package to any folder and run
setup.py install
from that folder, which should take care of everything for you.

Some documentation on installing packages for you to read:
http://docs.python.org/install/

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


Re: Nice way to cast a homogeneous tuple

2010-07-28 Thread Nick Raptis

On 07/28/2010 04:15 PM, wheres pythonmonks wrote:


f( *map(lambda x: int(x), struct.unpack('2s2s2s','123456')))
102

But this seems too complicated.


   

Well, you don't need the lambda at all
int   ===lambda x: int(x)

So just write

It's like writing:
def myint(x):
return int(x)


Nick,

Warm thanks to Steven D' Aprano who taught me that just yesterday in the 
Tutor list ;)

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


Re: Nice way to cast a homogeneous tuple

2010-07-28 Thread Nick Raptis

Ep, that missing line should be:

On 07/28/2010 04:27 PM, Nick Raptis wrote:

On 07/28/2010 04:15 PM, wheres pythonmonks wrote:


f( *map(lambda x: int(x), struct.unpack('2s2s2s','123456')))
102

But this seems too complicated.



Well, you don't need the lambda at all
int   ===lambda x: int(x)

So just write


f( *map(int, struct.unpack('2s2s2s', '123456')))

Pretty compact now, isn't it?


It's like writing:
def myint(x):
return int(x)


Nick,

Warm thanks to Steven D' Aprano who taught me that just yesterday in 
the Tutor list ;)

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


Re: default behavior

2010-07-29 Thread Nick Raptis

On 07/29/2010 09:12 PM, wheres pythonmonks wrote:

How do I build an "int1" type that has a default value of 1?

You mean something like:
>>> x = int()
>>> x
0
>>> def myint(value=1):
... return int(value)
...
>>> myint()
1
>>>

That's ugly on so many levels..

Anyway, basic types (and almost everything else) in Python are classes.
You can always subclass them to do whatever you like


[Hopefully no speed penalty.]
I am thinking about applications with collections.defaultdict.
What if I want to make a defaultdict of defaultdicts of lists?  [I
guess my Perl background is showing -- I miss auto-vivification.]


   

Ah, python is no perl. Then again, perl is no python either.
- Random pseudo-Confucius quote

Have fun,
Nick

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