is python 3 better than python 2?

2011-04-05 Thread neil
what are the advantages? if it wasn't for python 3 breaking backwards
compatibility would it be the better choice? 

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


Re: Ask for help on using re

2021-08-05 Thread Neil
Jach Feng  wrote:
> I want to distinguish between numbers with/without a dot attached:
> 
 text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
 re.compile(r'ch \d{1,}[.]').findall(text)
> ['ch 1.', 'ch 23.']
 re.compile(r'ch \d{1,}[^.]').findall(text)
> ['ch 23', 'ch 4 ', 'ch 56 ']
> 
> I can guess why the 'ch 23' appears in the second list. But how to get rid of 
> it?
> 
> --Jach

Does

>>> re.findall(r'ch\s+\d+(?![.\d])',text)

do what you want?  This matches "ch", then any nonzero number of
whitespaces, then any nonzero number of digits, provided this is not
followed by a dot or another digit.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me split a string into elements

2021-09-04 Thread Neil
DFS  wrote:
> Typical cases:
>  lines = [('one\ntwo\nthree\n')]
>  print(str(lines[0]).splitlines())
>  ['one', 'two', 'three']
> 
>  lines = [('one two three\n')]
>  print(str(lines[0]).split())
>  ['one', 'two', 'three']
> 
> 
> That's the result I'm wanting, but I get data in a slightly different 
> format:
> 
> lines = [('one\ntwo\nthree\n',)]
> 
> Note the comma after the string data, but inside the paren. 
> splitlines() doesn't work on it:
> 
> print(str(lines[0]).splitlines())
> ["('one\\ntwo\\nthree\\n',)"]
> 
> 
> I've banged my head enough - can someone spot an easy fix?
> 
> Thanks

lines[0][0].splitlines()

(You have a list containing a tuple containing the string you want to
split up.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: C is it always faster than nump?

2022-02-26 Thread Neil
Dan Stromberg  wrote:
> On Fri, Feb 25, 2022 at 8:12 AM BELAHCENE Abdelkader <
> abdelkader.belahc...@enst.dz> wrote:
> 
>> Hi,
>> a lot of people think that C (or C++) is faster than python, yes I agree,
>> but I think that's not the case with numpy, I believe numpy is faster than
>> C, at least in some cases.
>>
> 
> This is all "last time I heard".
> 
> numpy is written, in significant part, in Fortran.
> 
> Fortran, especially for matrix math with variable dimensions, can be faster
> than C.
> 
> Fortran, (still last I heard) did not support pointers, which gives Fortran
> compilers the chance to exploit a very nice class of optimizations you
> can't use nearly as well in languages with pointers.
> 
> I used to code C to be built with the "noalias" optimization, to get much
> of the speed of Fortran in C.  But it required using an error prone subset
> of C without good error detection.

Pointers were introduced in Fortran 90.

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


python 2.5 and 3gb switch

2007-09-28 Thread neil
Hello group,

I have a question I hope someone knowledgeable here might assist me with :o)

I am working on a python exporter to get a scene out of Blender and into a 
renderer called Indigo. I hope you at least have heard of open source 
Blender..
The scene is very large requiring me to use a 3gb switch enabled version of 
Blender and of course boot into a 3gb XP.
Unfortunately it seems that the limitation is now that python wont handle 
the mission I pass to it.
I am wondering if there is any build of python available that is 3gb aware? 
( if that is indeed the issue)
I see python is not really there for 64 bit yet but most of the people 
wanting to use this exporter will have 32 bit anyway.

Any assistance is appreciated
regards
Neil 


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


Re: python 2.5 and 3gb switch

2007-09-28 Thread neil
why?
I am asking if any one knows of a 3gb python build.
The code runs successfully in lesser missions it just wont run in the extra 
memory available when I try to run it along with my other programs in a 3gb 
space.
thanks for your reply though 


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


Re: python 2.5 and 3gb switch

2007-09-29 Thread neil
no sir nothing spectacular like that,
I'll explain further -
The .blend file I have is 550mb
It originates from 13,000+ meshes imported from Solidworks ( 3d CAD program) 
of a piece of machinery. A python script was used for this purpose -it also 
performed some other tasks on the meshes like matching materials from a 
library and identifying sharp edges for normals in the renderer.
This part was not a problem - the commit space reached 2,4 gb
The next step is to export it in parts of about 2 million faces each in a 
file format Indigo uses.
I need to save and reopen to unload commit space to do that... this means I 
have about 1.4gb commit.
If I start the export script it will run fine until just about the end - 
this consumes another 700mb - ( I have the max MS recommends -2.9gb- set in 
the 3gb switch to play with and I actually know this is the limit cos I've 
hit that too..)
Right at this point I still have 800mb available but here's the kicker - It 
needs about the same 550mb to complete the save.I know this from previous 
work on lesser files.
This is where python gives 'memerror'. Although the space is there it wont 
use it.
I believe I should be using Python that is 3gb enabled as well
This is the same error I ran across doing smaller tasks without the switch
btw Blender does not crash.
HTH
Neil 


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


Re: python 2.5 and 3gb switch

2007-09-29 Thread neil
thanks for your interest
...well I am quoting what it says -
it gives me some recent lines executed in the console window and then 
'memerror'
possibly Blenders python API is slightly different from python itself
I see there is a python exception MemoryError...
most likely this is the equivalent and also using google I see other 
references to MemError or memerror

I'll have to get back here tomorrow its late now
I will get the exact statement for you 


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


Re: python 2.5 and 3gb switch

2007-09-29 Thread neil

Since I was last here I found someone who is willing to do a 3gb python 
build for me who builds Blender as well.
I am pretty sure python is just not recognising the extra space.
Apparently there is a python module used in the 3gb Blender build I have 
that may be contributing too although the script I have requires a full 
python install.
I will see how his assistance goes.
Thanks for your willing help guys.
regards
Neil


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


Newbie Q about Turtle Gfx

2009-02-18 Thread Neil
Hello

Sorry if this is not an appropriate newsgroup for this problem. I am very 
new to Python but not new to programming.

I am hoping to use Python to teach a class and have been looking at the 
online book 'Snake Wrangling for Kids'.

I have followed the example

>>>import turtle
>>>t=turtle.pen()

which works fine and displays a turtle in a window.

However whenever I try to use something like

>>>t.forward(50)   (taken from the book)

i get the error...

Traceback (most recent call last):
  File "", line 1, in 
t.forward(50)
AttributeError: 'dict' object has no attribute 'forward'

I get the same error with t.left(10) etc. etc.

I have tried this with V2.6 and V3

Any ideas would be much appreciated.

Thanks
Neil



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


Re: Newbie Q about Turtle Gfx

2009-02-18 Thread Neil
Forgot to say, that after the error message the turtle window 'hangs' as 
unresponsive ...

"Neil"  wrote in message 
news:ifidnr_gysdmfqhunz2dnekdnzzin...@bt.com...
> Hello
>
> Sorry if this is not an appropriate newsgroup for this problem. I am very 
> new to Python but not new to programming.
>
> I am hoping to use Python to teach a class and have been looking at the 
> online book 'Snake Wrangling for Kids'.
>
> I have followed the example
>
>>>>import turtle
>>>>t=turtle.pen()
>
> which works fine and displays a turtle in a window.
>
> However whenever I try to use something like
>
>>>>t.forward(50)   (taken from the book)
>
> i get the error...
>
> Traceback (most recent call last):
>  File "", line 1, in 
>t.forward(50)
> AttributeError: 'dict' object has no attribute 'forward'
>
> I get the same error with t.left(10) etc. etc.
>
> I have tried this with V2.6 and V3
>
> Any ideas would be much appreciated.
>
> Thanks
> Neil
>
>
> 


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


Re: Newbie Q about Turtle Gfx

2009-02-18 Thread Neil
Thanks for the very speedy reply!

Not sure what your reply means but I have emailed the author with the 
problem!

Cheers

Neil

"sjbrown"  wrote in message 
news:9ba48cb7-d32b-40ef-a6a6-d22508863...@l33g2000pri.googlegroups.com...
It looks like there may be a bug if you were expecting t to be some
object other than a dict.  You can apparently contact the authour
here:

http://www.briggs.net.nz/log/contact/

On Feb 18, 2:16 pm, "Neil"  wrote:
> Hello
>
> Sorry if this is not an appropriate newsgroup for this problem. I am very
> new to Python but not new to programming.
>
> I am hoping to use Python to teach a class and have been looking at the
> online book 'Snake Wrangling for Kids'.
>
> I have followed the example
>
> >>>import turtle
> >>>t=turtle.pen()
>
> which works fine and displays a turtle in a window.
>
> However whenever I try to use something like
>
> >>>t.forward(50) (taken from the book)
>
> i get the error...
>
> Traceback (most recent call last):
> File "", line 1, in 
> t.forward(50)
> AttributeError: 'dict' object has no attribute 'forward'
>
> I get the same error with t.left(10) etc. etc.
>
> I have tried this with V2.6 and V3
>
> Any ideas would be much appreciated.
>
> Thanks
> Neil


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


Re: Newbie Q about Turtle Gfx

2009-02-18 Thread Neil
Thanks everyone!

It appears the info in the book is wrong. Trying what you have all suggested
has got it to work!

Many thanks, and I am sure I will be back here again with other newbie
problems!

Neil

"Neil"  wrote in message 
news:ifidnr_gysdmfqhunz2dnekdnzzin...@bt.com...
> Hello
>
> Sorry if this is not an appropriate newsgroup for this problem. I am very 
> new to Python but not new to programming.
>
> I am hoping to use Python to teach a class and have been looking at the 
> online book 'Snake Wrangling for Kids'.
>
> I have followed the example
>
>>>>import turtle
>>>>t=turtle.pen()
>
> which works fine and displays a turtle in a window.
>
> However whenever I try to use something like
>
>>>>t.forward(50)   (taken from the book)
>
> i get the error...
>
> Traceback (most recent call last):
>  File "", line 1, in 
>t.forward(50)
> AttributeError: 'dict' object has no attribute 'forward'
>
> I get the same error with t.left(10) etc. etc.
>
> I have tried this with V2.6 and V3
>
> Any ideas would be much appreciated.
>
> Thanks
> Neil
>
>
> 


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


Re: Newbie Q about Turtle Gfx

2009-02-19 Thread Neil
Hi

It turns out I should have used

t=turtle.Pen()

and not

t=turtle.pen()

My stupid mistake!

"Neil"  wrote in message 
news:jyydnb8xe8hpoghunz2dnuvz8juwn...@bt.com...
> Thanks everyone!
>
> It appears the info in the book is wrong. Trying what you have all 
> suggested
> has got it to work!
>
> Many thanks, and I am sure I will be back here again with other newbie
> problems!
>
> Neil
>
> "Neil"  wrote in message 
> news:ifidnr_gysdmfqhunz2dnekdnzzin...@bt.com...
>> Hello
>>
>> Sorry if this is not an appropriate newsgroup for this problem. I am very 
>> new to Python but not new to programming.
>>
>> I am hoping to use Python to teach a class and have been looking at the 
>> online book 'Snake Wrangling for Kids'.
>>
>> I have followed the example
>>
>>>>>import turtle
>>>>>t=turtle.pen()
>>
>> which works fine and displays a turtle in a window.
>>
>> However whenever I try to use something like
>>
>>>>>t.forward(50)   (taken from the book)
>>
>> i get the error...
>>
>> Traceback (most recent call last):
>>  File "", line 1, in 
>>t.forward(50)
>> AttributeError: 'dict' object has no attribute 'forward'
>>
>> I get the same error with t.left(10) etc. etc.
>>
>> I have tried this with V2.6 and V3
>>
>> Any ideas would be much appreciated.
>>
>> Thanks
>> Neil
>>
>>
>>
>
> 


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


Error when import pycurl

2008-10-16 Thread neil
Guys, I meet a error when I import pycurl.

Python 2.5.2 (r252:60911, Sep  8 2008, 16:01:08)
[GCC 4.1.1 20070105 (Red Hat 4.1.1-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pycurl
Traceback (most recent call last):
  File "", line 1, in 
ImportError: /usr/local/lib/python2.5/site-packages/pycurl.so: undefined
symbol: curl_multi_socket_action
>>>

The libcurl version is 7.19.0 and pycurl version is pycurl-7.19.0.

How can I solve this problem?
--
http://mail.python.org/mailman/listinfo/python-list


Re: First python program, syntax error in while loop

2013-05-06 Thread Neil Cerutti
On 2013-05-06, Mark Lawrence  wrote:
> On 06/05/2013 13:06, Neil Cerutti wrote:
>> On 2013-05-03, John Gordon  wrote:
>>> In  Neil Cerutti  
>>> writes:
>>>
>>>> Not quite yet. Players who guess correctly on the fifth try don't
>>>> get credit.
>>>
>>> Are you sure?  tries is initialized to zero and isn't
>>> incremented for the initial guess.
>>
>> while (number != guess) and (tries < 5):
>>
>> Is the condition that concludes the game.
>>
>> After the game, you are told you lost if tries is not less than
>> five, regardless of if number == guess.
>
> One of these days I'll work out why some people insist on using
> superfluous parentheses in Python code.  Could it be that they
> enjoy exercising their fingers by reaching for the shift key in
> conjunction with the 9 or 0 key?

Superflous parenthesis are sometimes a nice aid to comprehension.

I don't know about the above case, though. I don't think it hurts
anything, but it doesn't add much.

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


Re: Why do Perl programmers make more money than Python programmers

2013-05-07 Thread Neil Hodgson

jmfauth:


2) More critical, Py 3.3, just becomes non unicode compliant,
(eg European languages or "ascii" typographers !)
...


   This is not demonstrating non-compliance. It is comparing 
performance, not compliance.


   Please show an example where Python 3.3 is not compliant with Unicode.

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


Re: Making safe file names

2013-05-07 Thread Neil Hodgson

Andrew Berg:


This is not a Unicode issue since (modern) file systems will happily accept it. 
The issue is that certain characters (which are ASCII) are
not allowed on some file systems:
  \ / : * ? "<  >  | @ and the NUL character
The first 9 are not allowed on NTFS, the @ is not allowed on ext3cow, and NUL 
and / are not allowed on pretty much any file system. Locale
settings and encodings aside, these 11 characters will need to be escaped.


   There's also the Windows device name hole. There may be trouble with 
artists named 'COM4', 'CLOCK$', 'Con', or similar.


http://support.microsoft.com/kb/74496
http://en.wikipedia.org/wiki/Nul_%28band%29

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


Re: help on Implementing a list of dicts with no data pattern

2013-05-09 Thread Neil Cerutti
On 2013-05-09, rlelis  wrote:
> This is what i have for now:
>
> highway_dict = {}
> aging_dict = {}
> queue_row = []
> for content in file_content:
>   if 'aging' in content:
>   # aging 0 100
>   collumns = ''.join(map(str, 
> content[:1])).replace('-','_').lower()
>   total_values =''.join(map(str, content[1:2]))
>   aging_values = ''.join(map(str, content[2:]))
>
>   aging_dict['total'], aging_dict[collumns] = total, aging_values
>   queue_row.append(aging_dict)
>
>   if 'highway' in content:
>   #highway|   4   |   disable |   25
>   collumns = ''.join(map(str, 
> content[:1])).replace('-','_').lower()
>   lanes_values  =''.join(map(str, content[1:2]))  
>   state_values = ''.join(map(str, content[2:3])).strip('')
>   limit_values = ''.join(map(str, content[3:4])).strip('')
>   
>   highway_dict['lanes'], highway_dict['state'], 
> highway_dict['limit(mph)'] = lanes, state, limit_values
>   queue_row.append(highway_dict)

Can you provide a short example of input and what you had hoped
to see in the lists and dicts at the end?

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


Re: Style question -- plural of class name?

2013-05-09 Thread Neil Cerutti
On 2013-05-08, Denis McMahon  wrote:
> On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote:
>
>> FooEntry is a class.  How would you describe a list of these in a
>> docstring?
>> 
>> "A list of FooEntries"
>> 
>> "A list of FooEntrys"
>> 
>> "A list of FooEntry's"
>> 
>> "A list of FooEntry instances"
>> 
>> The first one certainly sounds the best, but it seems wierd to change
>> the spelling of the class name to make it plural.
>
> I wouldn't use an apostrophe for pluralisation.

If there's no chance for confusion between a class named FooEntry
and another named FooEntries, then the first attempt seems best.
Pluralize a class name by following the usual rules, e.g.,
"strings" and "ints".

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


Re: Style question -- plural of class name?

2013-05-09 Thread Neil Cerutti
On 2013-05-09, Jussi Piitulainen  wrote:
> Neil Cerutti writes:
>> If there's no chance for confusion between a class named
>> FooEntry and another named FooEntries, then the first attempt
>> seems best. Pluralize a class name by following the usual
>> rules, e.g., "strings" and "ints".
>
> Like "strings" would be "foo entries". Which might work well.
>
> (I mean, isn't the class named "str"?)

Yeah, that's not such a good Python example. I used it to replace
"chars" and felt good at the time. ;)

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


Re: help on Implementing a list of dicts with no data pattern

2013-05-10 Thread Neil Cerutti
On 2013-05-09, Dave Angel  wrote:
> On 05/09/2013 05:22 PM, rlelis wrote:
>> On Thursday, May 9, 2013 7:19:38 PM UTC+1, Dave Angel wrote:
>>
>> Yes it's a list of string. I don't get the NameError: name 'file_content' is 
>> not defined in my code.
>
> That's because you have the 3 lines below which we hadn't seen yet.

Heroic efforts, Dave!

To rlelis:

Do not start to program until you understand what you want to do.
Work it out on a sheet of paper, or at least in your mind.

If you can't provide sample input and the expected output from
it, chances are you aren't ready to start writing code.

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


Re: Python for philosophers

2013-05-13 Thread Neil Cerutti
On 2013-05-13, Steven D'Aprano
 wrote:
> I'm not trying to beat the original Poster up for making an
> error, but demonstrating just how badly off track you can get
> by trying to reason from first principles (as Plato may have
> done) instead of empirical study (as Aristotle or Bacon may
> have done). Knowledge of how things actually are beats
> understanding of how they ought to be every time.

Wasn't it Aristarchus who was considered a trouble-maker for
dropping different sized lead balls from a tower? They would land
simultaneously just as his teacher, who taught that heavier
objects fall faster, was walking past.

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


Re: [Off topic] Software epigrams

2013-05-13 Thread Neil Cerutti
On 2013-05-13, Skip Montanaro  wrote:
>> 8. A programming language is low level when its programs
>> require attention to the irrelevant.
>>
>> So much a matter of debate. Indentation is irrelevant, why
>> should Python programs pay attention to it? Block delimiters
>> are irrelevant too, the interpreter should be able to figure
>> them out from the code layout. But this one is absolutely
>> right:
>
> I think "irrelevant" in this context means stuff like memory
> management.

I thought I liked that one at first, but upon reflection it
speciously inserts the word "irrelevant" in order to avoid
stating a tautology: A programming language is low level when its
programs require attention to low level details.

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


Re: [Off topic] Software epigrams

2013-05-15 Thread Neil Cerutti
On 2013-05-13, F?bio Santos  wrote:
>
> On 13 May 2013 19:48, "Neil Cerutti"  wrote:
>>
>> On 2013-05-13, Skip Montanaro  wrote:
>> >> 8. A programming language is low level when its programs
>> >> require attention to the irrelevant.
>> >>
>> >> So much a matter of debate. Indentation is irrelevant, why
>> >> should Python programs pay attention to it? Block delimiters
>> >> are irrelevant too, the interpreter should be able to figure
>> >> them out from the code layout. But this one is absolutely
>> >> right:
>> >
>> > I think "irrelevant" in this context means stuff like memory
>> > management.
>>
>> I thought I liked that one at first, but upon reflection it
>> speciously inserts the word "irrelevant" in order to avoid
>> stating a tautology: A programming language is low level when its
>> programs require attention to low level details.
>>
>> --
>> Neil Cerutti
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
> It's not a tautology in disguise. Irrelevant != low level. When
> low level details are relevant to the scope of my program, I
> use a low level language.

It is a tautology is disguise. When you use a low level language,
low level details are relevant to the scope of your program.

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


Re: [Off topic] Software epigrams

2013-05-15 Thread Neil Cerutti
On 2013-05-15, F?bio Santos  wrote:
>> It is a tautology is disguise. When you use a low level
>> language, low level details are relevant to the scope of your
>> program.
>
> I don't see it that way. I think relevance and level are two
> unrelated concepts.
>
> For example, in python you are handling irrelevant things if
> you are trying to start a program and redirecting its standard
> output into another program's standard input instead of just
> using the shell and a pipe to do it.
>
> And in C you are just at the right level to write something for
> a microchip, but then again you are doing a load of irrelevant
> stuff if you need to work numbers larger than the maximum
> permitted.

If you need numbers larger than the maximum permitted then all
the code you write to handle them is relevant.

If I want to bake bread I hope I don't have to till a garden,
plant the wheat, harvest the wheat, and grind the wheat. But
gardening is relevant to bread baking weather or not I do it.

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


Re: [Off topic] Software epigrams

2013-05-16 Thread Neil Cerutti
On 2013-05-16, F?bio Santos  wrote:
>> If I want to bake bread I hope I don't have to till a garden,
>> plant the wheat, harvest the wheat, and grind the wheat. But
>> gardening is relevant to bread baking weather or not I do it.
>
> Then memory management t is relevant to every python program
> even though it's done by the interpreter?

Yes, I think so. If you didn't understand how Python managed
memory, you couldn't write it effectively. You would end up
making unecessary copies, and other pathological programming
practices.

> And in Java we have factories, builders and builderfactories.
> What's so relevant about them? Java is high level, no?

When I tried to pin down what an irrelevant detail in a computer
program could be, I couldn't do it. I guess comment decorations,
maybe? But those would have no bearing on the level of problem
for which a programming language is most appropriate.

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


Re: any cherypy powred sites I can check out?

2013-05-17 Thread Neil Cerutti
On 2013-05-17, Mark Lawrence  wrote:
> On 17/05/2013 01:00, visphatesj...@gmail.com wrote:
>> fuck straight off
>
> I assume you're the author of "How to win friends and influence
> people"?

'"I recently wrote a book called "How to Get Along with
Everybody." I didn't write it myself--I wrote it some asshole.'
  --Steve Martin

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


Re: A computer programmer, web developer and network admin resume

2013-05-23 Thread Neil Cerutti
On 2013-05-22, Tim Chase  wrote:
> On 2013-05-22 01:15, i...@databaseprograms.biz wrote:
>> A computer programmer, web developer and network administrator
>
> ...walk into a bar...
>
> So what's the punchline?

"Ow." Get it? "Ow."

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


Re: Non-identifiers in dictionary keys for **expression syntax

2013-05-23 Thread Neil Cerutti
On 2013-05-23, Matthew Gilson  wrote:
> That's fine, but what is a keyword argument?  According to the glossary 
> (http://docs.python.org/3.3/glossary.html):
>
> /"keyword argument/: an argument preceded by an identifier (e.g. name=) 
> in a function call or passed as a value in a dictionary preceded by **."
>
> As far as I'm concerned, this leads to some ambiguity in
> whether the keys of the mapping need to be valid identifiers or
> not.

I don't see any ambiguity. A keyword argument is an argument
preceded by an identifier according to the definition. Where are
you perceiving wiggle room?

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


Re: Utility to locate errors in regular expressions

2013-05-24 Thread Neil Cerutti
On 2013-05-24, Roy Smith  wrote:
> Of course, most of Python user community are wimps and shy away
> from big hairy regexes [ducking and running].

I prefer the simple, lumbering regular expressions like those in
the original Night of the Regular Expressions. The fast, powerful
ones from programs like the remake of Dawn of the GREP, just
aren't as scary.

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


Re: Polymoprhism question

2013-05-24 Thread Neil Cerutti
On 2013-05-24, RVic  wrote:
> Thanks Steven,
>
> Yes, I see Python isn't going to do this very well, from what I
> can understand.
>
> Lets say I have a type of class, and this type of class will
> always have two methods, in() and out().
>
> Here is, essentially, what I am trying to do, but I don't know
> if this will make sense to you or if it is really doable in
> Python:  #thanks, RVic
>
> import sys
> argv = sys.argv[1:] 
> ClassIamInstantiating = argv
> ClassIamInstantiating.in("something")
> x = ClassIamInstantiating.out()

This is pretty easy in Python using the __name__ attribute.

import sys

class A:
 def in(self):
   print("A in")
 def out(self):
   print("A out")

class B:
 def in(self):
   print("B in")
 def out(self):
   print("B out")

classes = {cls.__name__: cls for cls in (A, B)}

ArgType = classes[sys.agrv[1]]

arg = ArgType()

arg.in("test")
arg.out("test")

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


Re: Python #ifdef

2013-05-28 Thread Neil Cerutti
On 2013-05-28, Joel Goldstick  wrote:
>
> No

Yes. More below.

> On May 28, 2013 3:48 PM, "Carlos Nepomuceno" 
> wrote:
>> Are there Python 'preprocessor directives'?
>>
>> I'd like to have something like '#ifdef' to mix code from Python 2 and 3
>> in a single file.
>>
>> Is that possible? How?

You need sys.version_info.

For more, see http://python3porting.com/noconv.html

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


Re: Short-circuit Logic

2013-05-30 Thread Neil Cerutti
On 2013-05-30, Chris Angelico  wrote:
> On Thu, May 30, 2013 at 3:10 PM, Steven D'Aprano
> wrote:
>> # Wrong, don't do this!
>> x = 0.1
>> while x != 17.3:
>> print(x)
>> x += 0.1
>
> Actually, I wouldn't do that with integers either.

I propose borrowing the concept of significant digits from the
world of Physics.

The above has at least three significant digits. With that scheme
x would approximately equal 17.3 when 17.25 <= x < 17.35.

But I don't see immediately how to calculate 17.25 and 17.35 from
17.3, 00.1 and 3 significant digits.

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


Re: Installing PyGame?

2013-06-08 Thread Neil Hodgson

Eam onn:


ImportError: 
dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so,
 2): no suitable image found.  Did find:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so:
 no matching architecture in universal wrapper


   This is saying that the version of Python you are using is a 
different architecture to the installed pygame library. This could be 
because you are using a 64-bit version of Python with a 32-bit library 
or vice-versa. Or you have a PowerPC library and Python is compiled for 
Intel processors.


   In Terminal, you can find the architecture of files with "otool -vh" 
followed by the file name. So try (on one line)


otool -vh 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so


   And the same with Python, first finding where Python is with
whereis python

   Then post all of the output text, not just your interpretation.

   Neil

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


Re: py_compile vs. built-in compile, with __future__

2013-06-11 Thread Neil Cerutti
On 2013-06-10, dhyams  wrote:
> On Monday, June 10, 2013 6:36:04 PM UTC-4, Chris Angelico wrote:
>> Can you read the file into a string, prepend a future directive, and
>> 
>> then compile the string?
>
> Technically yes, except that now there is complication of
> writing the modified module back to a file so that I can still
> use py_compile.compile() to byte compile that code.

You would use StringIO instead of writing a temp file.

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


Re: A certainl part of an if() structure never gets executed.

2013-06-12 Thread Neil Cerutti
On 2013-06-12, Chris Angelico  wrote:
> On Wed, Jun 12, 2013 at 5:45 PM,  ??  wrote:
>> First of all i have changed the code to the following because using a
>> regex
>> to detect a single char was an overkill.
>>
>> if '=' not in name and '=' not in month and '=' not in year:
>
> It'd be courteous to acknowledge those who made that
> suggestion, most notably alex23 who posted it in almost that
> exact form.

Also, I wish he would stop fudging his From info. I've got
something like 8 entries for this ass in my killfile, and it
seems I need a new one every day.

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


Re: py_compile vs. built-in compile, with __future__

2013-06-12 Thread Neil Cerutti
On 2013-06-11, dhyams  wrote:
>> You would use StringIO instead of writing a temp file.
>
> I don't think that would work...py_compile takes a filename as
> input, not a file object.

Dang. Sorry for the misinfo.

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


Re: A certainl part of an if() structure never gets executed.

2013-06-12 Thread Neil Cerutti
On 2013-06-12, Grant Edwards  wrote:
> On 2013-06-12, Tim Roberts  wrote:
>>  ??  wrote:
>>>
>>>[code]
>>> if not re.search( '=', name ) and not re.search( '=', month ) 
>>> and not re.search( '=', year ):
>>> cur.execute( '''SELECT * FROM works WHERE clientsID = 
>>> (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and 
>>> YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (name, month, year) )
>>> elif not re.search( '=', month ) and not re.search( '=', year ):
>>> cur.execute( '''SELECT * FROM works WHERE 
>>> MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', 
>>> (month, year) )
>>> elif not re.search( '=', year ):
>>> cur.execute( '''SELECT * FROM works WHERE 
>>> YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', year )
>>
>> There is so much you didn't tell us here, including which database you are
>> using.
>
> Are you guys _still_ on Nikos hook?
>
> [No, I don't really think he's trolling, but it would be really
> impressive if he were.]

He's definitely trolling. I can't think of any other reason to
make it so hard to kill-file himself.

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


Re: Turnign greek-iso filenames => utf-8 iso

2013-06-12 Thread Neil Cerutti
On 2013-06-12, Mark Lawrence  wrote:
> On 12/06/2013 13:42,   wrote:
>>
>> Something you want me to try?
>
> I'd suggest suicide but that would no doubt start another
> stream of questions along the lines of "How do I do it?".

hi. I loopet rope aroung and jumped, but bruise happen and erron
do the death.

Pls heelp!

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


Re: A certainl part of an if() structure never gets executed.

2013-06-13 Thread Neil Cerutti
On 2013-06-12, Zero Piraeus  wrote:
> On 12 June 2013 10:55, Neil Cerutti  wrote:
>>
>> He's definitely trolling. I can't think of any other reason to
>> make it so hard to kill-file himself.
>
> He's not a troll, he's a help vampire:
>
>   http://slash7.com/2006/12/22/vampires/
>
> ... a particularly extreme example, I'll admit: his lack of
> consideration for others apparently knows no bounds. The email thing
> is just another aspect of that.

He's also changed his NNTP-Posting-Host, just yesterday, along
with at least three changes in From address, and one change in
Reply-To.

And to start with he came here with an obvious troll name.

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


Re: Version Control Software

2013-06-13 Thread Neil Hodgson

Grant Edwards:


The last time we made the choice (4-5 years ago), Windows support for
get, bzr, and hg was definitely lacking compared to svn.  The lack of
something like tortoisesvn for hg/git/bzr was a killer.  It looks like
the situation has improved since then, but I'd be curious to hear from
people who do their development on Windows.


   GUIs for Hg/Git are now much more usable. On Windows, OS X, and 
Linux my GUI/command line use split is about 80/20.


   For Hg, TortoiseHg is quite good on Windows and Linux and so is 
SourceTree on OS X. I don't use Git as much but SourceTree works well on 
OS X.


   SourceTree is in beta on Windows and doesn't yet support Hg there.

http://tortoisehg.bitbucket.org/
http://www.sourcetreeapp.com/

   Neil

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


Re: Creating a Super Simple WWW Link, Copy, & Paste into Spreadsheet Program

2013-06-14 Thread Neil Cerutti
On 2013-06-13, Nick Cash  wrote:
>>> there is a python module that reads and writes to excel files.  look 
>>> for that
>
>>More than one, actually, and which to use depends on whether
>>"Excel files" means the .xls or .xlsx format.  On Windows, the
>>most flexible solution is going to be to just use COM to
>>control the Excel >application in reading and writing the
>>files.  Outside of Windows, the best bet is usually to work
>>>with csv files instead, as Dave suggested.
>
> I've had success with the xlrd and xlwt suite of modules
> (http://www.python-excel.org/), using both .xls and .xlsx, on
> Linux. 

I use xlrd for .xlsx on Windows with Office 2007, no problems.

I wouldn't call it convenient, though. It saves a coworker from
doing an export which seems worth it, but using csv.DictReader
is, much, much simpler. Unless there's some non-trivial need to
use Excel directly I strongly recommend exporting as csv and
using the csv module.

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


Re: Don't feed the troll...

2013-06-14 Thread Neil Cerutti
On 2013-06-14, Antoon Pardon  wrote:
> Now there is nothing wrong in being ignorant. The question is
> how do you proceed from there. The answer is not by starting a
> project that is far above your ability and pestering the
> experts in the hope they will spoon feed you.

A major issue is this: the spoon-feeding he does receive is
unefficacious. Smart, well-meaning, articulate people's time is
getting squandered.

I read the responses. I've learned things from them. But Nikos
has not. And once a discussion devolves to reitteration even that
value is lost.

And perhaps worst of all, there's none of the closure or
vicarious catharsis that usually comes from a well-designed
educational transaction.

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


Re: My son wants me to teach him Python

2013-06-14 Thread Neil Cerutti
On 2013-06-14, Steven D'Aprano  wrote:
> On Thu, 13 Jun 2013 20:33:40 -0700, Rick Johnson wrote:
>
>> On Thursday, June 13, 2013 3:18:57 PM UTC-5, Joshua Landau wrote:
>> 
>>> [...]
>>> GUI is boring. I don't give a damn about that. If I had it my way, I'd
>>> never write any interfaces again (although designing them is fine).
>>> Console interaction is faster to do and it lets me do the stuff I
>>> *want* to do quicker.
>> 
>> And are you willing to provide *proof* that the console is faster? Or is
>> this merely just your "opinion"? I would be ready and willing to compete
>> in a "Pepsi challenge" to disprove your claim if needed.  For instance,
>> if i want to open a text file on my machine, i merely navigate to the
>> file via my file browser interface, using clicks along the way, and then
>> the final double click will open the text file using it's default
>> program. Are you telling me you can type the address faster (much less
>> remember the full path) than i can point and click? 
>
> If you can remember the full path in order to point and click,
> then I'm sure Joshua can remember the full path in order to
> type.

My favorite current challenge for an IDE designer is
concatenating text files. This is a one-liner, even with cmd.exe,
but I don't even know how to do it in Explorer. I'd have to use X
number of text editing sessions.

> But in any case, there are certainly strengths and weaknesses
> of both GUIs and text interfaces, and one should design
> programs around whichever is best for the needs of the program
> and the user.

The side issue of keyboard shortcuts in GUI interface have
built-in stengths and weaknesses. I was going to write something
about them earlier, but I got bogged down when I thought of the
issue of accessibilty, which overtakes any such discussion.

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


Re: A Beginner's Doubt

2013-06-19 Thread Neil Cerutti
On 2013-06-19, augusto...@gmail.com  wrote:
> This is my first post in this group and the reason why I came
> across here is that, despite my complete lack of knowledge in
> the programming area, I received an order from my teacher to
> develop a visually interactive program, until 20th July, so we
> can participate in a kind of contest.
>
> My goal is to learn and program it by myself, as good as the
> time allows me.
>
> That said, what I seek here is advice from people who
> definitively have more experience than me on topics like: is it
> possible to develop this kind of program in such a short amount
> of time? What kinds of aspects of Python should I focus on
> learning? What tutorials and websites are out there that can
> help me? What kind of already done packages are out there that
> I can freely use, so I do not need to create all the aspects of
> the program froms scratch?
>
> It would be wise to give an abstract of the program. I made an
> information flux kind of graphic, but I do not know how to post
> it in here, so I'll use only words:
>
> Full screen window -> Title and brief introductory text -> 3 Buttons 
> (Credits) (Instructions) and (Start)
>
> (Credits) -> Just plain text and a return button 
> (Instructions) -> Just plain text and a return button
> (Start) -> Changes the screen so it displays a side-menu and a Canvas.
>
> Side menu -> X number of buttons (maybe 4 or 5)
> Buttons -> Clicked -> Submenu opens -> List of images
> -> Return button -> Back to side menu
>
> Image in List of images -> When clicked AND hold mouse button -> Make copy
> -> if: dragged to canvas -> paste the copy in place
> -> if: dragged anywhere else -> delete copy and 
> nothing happens
>
> On canvas:
> Image -> On click and drag can be moved
>   -> Double click -> Opens menu -> Resize, Deform, Rotate, Color, 
> Brigthness, Contrast, Color Curve, Saturation
>
> Then, somewhere in cavas:
>
> Save option -> Prompt for file and user's name
> -> Prompt if users want printed copy or not -> Print
> -> After saved, display random slideshow in other monitor, device 
> or screen with the users' creations.
>
> Thats basically the whole program. I've been studying Python
> for a week and half now, through: How to think like a Computer
> Scientist and Invent with Python and Pygame. I'm still at the
> very beggining, though, and I can't make much more than make
> some images appear on a Pygame screen in a menu-like style,
> with a patterned gap between them. No mouse interactions up to
> now.
>
> I really appreciate your suggestions and help.

First off, this is extremely ambitious for one month with no
programming experience. But it sounds like you've made some
significant progress. For a new programmer, mastering the
mechanical aspects of programming can be a huge hurdle, and
you've done that already.

Finally, my advice is to use tkinter combined with PIL for this
project, instead of PyGame. I have not personally used PyGame,
but my guess is it will be much harder to create a reasonable GUI
with PyGame than with tkinter. But I do not know how difficult
this project will be will be even using the libraries of least
resistance. The GUI you propose is very simple, except possibly
for the dragging and dropping, which I've not tried and might be
hairy. Moreover, I have not seriously used PIL and I don't even
know if it supports Python 3.

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


Re: Problem with the "for" loop syntax

2013-06-20 Thread Neil Cerutti
On 2013-06-20, Joshua Landau  wrote:
> On 20 June 2013 04:11, Cameron Simpson  wrote:
>> Also, opening-and-not-closing a set of brackets is almost the
>> only way in Python to make this kind of error (syntax at one
>> line, actual mistake far before).
>>
>> See if your editor has a show-the-matching-bracket mode.
>> If you suspect you failed to close a bracket, one approach is
>> to go _below_ the syntax error (or right on it) and type a
>> closing bracket. Then see where the editor thinks the opening
>> one is.
>
> Thanks for that, that's quite an ingenious technique.

The auto-indent feature of Vim catches this type of syntax error,
and I imagine other good autoindent support will do the same.
After I press enter and the following line's indent isn't what I
expect, it is nearly always due to a missing bracket, quote or
colon.

So if you press enter and the autoindent is unexpected, don't
just press space or backspace to fix it. It's usually a sign of
an earlier syntax error, so look for that first.

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


Re: Problem with the "for" loop syntax

2013-06-21 Thread Neil Cerutti
On 2013-06-20, Cameron Simpson  wrote:
> On 20Jun2013 13:55, Neil Cerutti  wrote:
>| On 2013-06-20, Joshua Landau  wrote:
>| > On 20 June 2013 04:11, Cameron Simpson  wrote:
>| >> Also, opening-and-not-closing a set of brackets is almost the
>| >> only way in Python to make this kind of error (syntax at one
>| >> line, actual mistake far before).
>| >>
>| >> See if your editor has a show-the-matching-bracket mode.
>| >> If you suspect you failed to close a bracket, one approach is
>| >> to go _below_ the syntax error (or right on it) and type a
>| >> closing bracket. Then see where the editor thinks the opening
>| >> one is.
>| >
>| > Thanks for that, that's quite an ingenious technique.
>| 
>| The auto-indent feature of Vim catches this type of syntax error,
>| and I imagine other good autoindent support will do the same.
>
> Interesting. I use autoindent but grew up with it for prose. I
> hadn't realised vim's support inderstaood python indentation.
> I'll have to pay more attention...

A standard Vim install autoindents Python tolerably well if
you've set filetype=python. If you've got a baked-in Python
interpreter you can get even more bells and whistles. The
standard executable installs I could find don't support Python 3,
so I haven't seen all the great stuff I'm missing.

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


Re: Default Value

2013-06-21 Thread Neil Cerutti
On 2013-06-21, Chris Angelico  wrote:
> On Sat, Jun 22, 2013 at 4:26 AM, Rick Johnson
> wrote:
>> I could cast a "virtual net" over my poor lemmings before
>> they jump off the cliff by throwing an exception:
>>
>>   Traceback (most recent screw-up last):
>>Line BLAH in SCRIPT
>> def f(x = [None, b, [a, [4]]]):
>>   ArgumentError: No mutable default arguments allowed!
>
> So tell me, oh Great and Powerful Wizard of Rick, how is the
> interpreter supposed to know which defaults are mutable? I
> mean, it's obviously some intrinsic property of the object.
> Somehow one thing is clearly immutable, another thing clearly
> isn't. Will there be a PyObject_IsImmutable() API?
>
> Oh! I know. Function argument defaults will now be restricted
> to int/float/tuple. That would do it, right? Nobody would be
> bothered by little restrictions like that, would they.

I've been around here long enough to have even participated in
one of these discussions before.

Rick, it's not a wart, It's a gotcha.

The reason it's a gotcha is this: In order to predict what will
happen correctly, you have to have mastered three separate Python
concepts.

1. How name-binding works.
2. How argument passing works, i.e., via name-binding.
3. When default arguments are evaluated.
4. The Python object model.

OK, you have to know four things. Curses! I'll come in again.

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


Re: Looking for a name for a deployment framework...

2013-06-24 Thread Neil Cerutti
On 2013-06-24, MRAB  wrote:
> On 24/06/2013 13:50, Roy Smith wrote:
>> In article <8b0d8931-cf02-4df4-8f17-a47ddd279...@googlegroups.com>,
>>   jonathan.slend...@gmail.com wrote:
>>
>>> Hi all,
>>>
>>> Any suggestions for a good name, for a framework that does automatic server
>>> deployments?
>>>
>>> It's like Fabric, but more powerful.
>>> It has some similarities with Puppet, Chef and Saltstack, but is written in
>>> Python.
>>>
>>> Key points are that it uses Python, but is still very declarative and
>>> supports introspection. It supports parallel deployments, and interactivity.
>>> And it has a nice commandline shell with autocompletion for traversing the
>>> deployment tree.
>>>
>>> The repository:
>>> https://github.com/jonathanslenders/python-deployer/tree/refactoring-a-lot-v2
>>>
>>>
>>> Suggestions welcome :)
>>> Jonathan
>>
>> Without forming any opinion on the software itself, the best advice I
>> can offer is that naming puns are very popular.  If you're thinking of
>> this as a fabric replacement, I would go with cloth, textile, material,
>> gabardine, etc.
>
> Snakeskin? Oh, I see that's already taken. :-(

Most things are taken nowadays.

A short nonsense-word is best. Something like "Folaf". Yeah, it
doesn't spark the imagination, but it's easy to find, if not to
remember.

Well, not "Folaf." That seems to be an African style restaurant
in L.A.

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


Re: Is this PEP-able? fwhile

2013-06-25 Thread Neil Hodgson

jim...@aol.com:


Syntax:
fwhile X in ListY and conditionZ:


   There is precedent in Algol 68:

for i from 0 to n while safe(i) do .. od

   which would also make a python proposal that needs no new key words:

for i in range(n) while safe(i): ..

   The benefit of the syntax would be to concentrate the code 
expressing the domain of the loop rather than have it in separate locations.


   Not a big win in my opinion.

   Neil

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


Re: Is this PEP-able? fwhile

2013-06-26 Thread Neil Cerutti
On 2013-06-25, rusi  wrote:
> On Tuesday, June 25, 2013 9:30:54 PM UTC+5:30, Ian wrote:
>> In my experience the sorts of people who preach "one exit point" are
>> also all about defining preconditions and postconditions and proving
>> that the postconditions follow from the preconditions.  I think that
>> the two are linked, because the "one exit point" rule makes those
>> sorts of proofs simpler.
>
> Ah! utopia!
>
> For every one who knows about pre/post/invariant conditions,
> there are 10 who follow goto-statement-is-harmful like a
> religious edict.

The one-exit-point rule is helpful for tracking entry and exit
invariants. But in my view it shouldn't be followed when it makes
code worse.

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


Re: Stupid ways to spell simple code

2013-07-01 Thread Neil Cerutti
On 2013-06-30, Chris Angelico  wrote:
> So, here's a challenge: Come up with something really simple,
> and write an insanely complicated - yet perfectly valid - way
> to achieve the same thing. Bonus points for horribly abusing
> Python's clean syntax in the process.
>
> Go on, do your worst!

I've often thought it was redundant for Python to support 'if'
when it has dictionaries, cf the rationale for having no
'switch'.

valid_name = None
while not valid_name:
name = input("Enter your name: ")
valid_name = {
True: lambda: print("No name longer than 20 letters."),
    False: lambda: True,
}[len(name) > 20]()

Much better.

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


Re: python adds an extra half space when reading from a string or list

2013-07-01 Thread Neil Cerutti
On 2013-07-01, rusi  wrote:
> To wit:
>
> 1. Kill-filing/spam-filtering are tools for spam.
> Nikos is certainly not spamming in the sense of automated
> sending out of cooked mail to zillions of recipients/lists.
> His posts are definite and intentional

I disagree. Kill-files are not only for spam. I filter out
anything I believe I won't want to see.

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


Re: Regular expression negative look-ahead

2013-07-02 Thread Neil Cerutti
On 2013-07-01, Jason Friedman  wrote:
>
> I have table names in this form:
> MY_TABLE
> MY_TABLE_CTL
> MY_TABLE_DEL
> MY_TABLE_RUN
> YOUR_TABLE
> YOUR_TABLE_CTL
> YOUR_TABLE_DEL
> YOUR_TABLE_RUN
>
> I am trying to create a regular expression that will return true for only
> these tables:
> MY_TABLE
> YOUR_TABLE

Use the "is not a word" character class on either end.

r"\WMY_TABLE\W"
r"\WYOUR_TABLE\W"

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


Re: HTML Parser

2013-07-02 Thread Neil Cerutti
On 2013-07-02, subhabangal...@gmail.com  wrote:
> Dear Group,
>
> I was looking for a good tutorial for a "HTML Parser". My
> intention was to extract tables from web pages or information
> from tables in web pages. 
>
> I tried to make a search, I got HTMLParser, BeautifulSoup, etc.
> HTMLParser works fine for me, but I am looking for a good
> tutorial to learn it nicely.

Take a read of the topic "Parsing, creating, and Manipulating
HTML Documents" from chapter five of Text Processing in Python.

http://gnosis.cx/TPiP/chap5.txt

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


Re: Parsing Text file

2013-07-02 Thread Neil Cerutti
On 2013-07-02, sas4...@gmail.com  wrote:
> I have a text file like this:
>
> Sometext
> Somemore
> Somemore
> maskit
>
> Sometext
> Somemore
> Somemore
> Somemore
> maskit
>
> Sometext
> Somemore
> maskit
>
> I want to search for the string maskit in this file and also
> need to print Sometext above it..SOmetext location can vary as
> you can see above.
>
> In the first instance it is 3 lines above mask it, in the
> second instance it is 4 lines above it and so on..
>
> Please help how to do it?

How can you tell the difference between Sometext and Somemore?

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


Re: Parsing Text file

2013-07-02 Thread Neil Cerutti
On 2013-07-02, Tobiah  wrote:
> On 07/02/2013 12:30 PM, sas4...@gmail.com wrote:
>> Somemore can be anything for instance:
>>
>> Sometext
>> mail
>> maskit
>>
>> Sometext
>> rupee
>> dollar
>> maskit
>>
>> and so on..
>>
>> Is there a way I can achieve this?
>
> How do we know whether we have Sometext?
> If it's really just a literal 'Sometext', then
> just print that when you hit maskit.
>
> Otherwise:
>
>
> for line in open('file.txt').readlines():
>   
>   if is_sometext(line):
>   memory = line
>
>   if line == 'maskit':
>   print memory

Tobiah's solution fits what little we can make of your problem.

My feeling is that you've simplified your question a little too
much in hopes that it would help us provide a better solution.
Can you provide more context? 

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


Re: Python list code of conduct

2013-07-03 Thread Neil Hodgson

Dennis Lee Bieber:


So who would enforce any rules? I doubt it could be ported to a new (if
approval could even be obtained) comp.lang.python.mod(erated) so nothing
can be enforced on the comp.lang.python side; and what would you do with
Google Groups?


   The current news group charter doesn't really have any rules. While 
it may be possible to recharter an existing news group, it would likely 
be simpler to just create a new one.



 CHARTER

Comp.lang.python is an unmoderated newsgroup which will serve
as a forum for discussing the Python computer language. The
group will serve both those who just program in Python and
those who work on developing the language. Topics that
may be discussed include:

- announcements of new versions of the language and
applications written in Python.

- discussion on the internals of the Python language.

- general information about the language.

- discussion on programming in Python.


http://www.python.org/search/hypermail/python-1994q1/0377.html

   Neil

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


Re: Default scope of variables

2013-07-05 Thread Neil Cerutti
On 2013-07-04, Dave Angel  wrote:
> On 07/04/2013 01:32 AM, Steven D'Aprano wrote:
>
>> Well, if I ever have more than 63,000,000 variables[1] in a
>> function, I'll keep that in mind.
>>
>  
>>
>> [1] Based on empirical evidence that Python supports names
>> with length at least up to one million characters long, and
>> assuming that each character can be an ASCII letter, digit or
>> underscore.
>
> Well, the number wouldn't be 63,000,000.  Rather it'd be
> 63**100

You should really count only the ones somebody might actually
want to use. That's a much, much smaller number, though still
plenty big.

Inner scopes (I don't remember the official name) is a great
feature of C++. It's not the right feature for Python, though,
since Python doesn't have deterministic destruction. It wouldn't
buy much except for namespace tidyness.

for x in range(4):
   print(x)
print(x) # Vader NOoOO!!!

Python provides deterministic destruction with a different
feature.

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


Re: Default scope of variables

2013-07-05 Thread Neil Cerutti
On 2013-07-05, Chris Angelico  wrote:
> On Fri, Jul 5, 2013 at 11:24 PM, Neil Cerutti
>  wrote:
>> Python provides deterministic destruction with a different
>> feature.
>
> You mean 'with'? That's not actually destruction, it just does
> one of the same jobs that deterministic destruction is used for
> (RAII). It doesn't, for instance, have any influence on memory
> usage, nor does it ensure the destruction of the object's
> referents. But yes, it does achieve (one of) the most important
> role(s) of destruction.

Yes, thanks. I meant the ability to grab and release a
resource deterministically.

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


Re: analyzing time

2013-07-05 Thread Neil Cerutti
On 2013-07-05, noydb  wrote:
> Hello All,
>
> I have a table with a column of type date, with dates and time
> combined (like '1/6/2013 3:52:69PM'), that spans many months.
> How would I pull out records that are the first and last
> entries per day?
>
> Also, if I wanted to find time clusters per day (or per week)
> -- like if an entry is made every day around 11am -- is there a
> way to get at that temporal statistical cluster?
>
> Python 2.7, Windows 7.
>
> Any guidance would be greatly appreciated!  Time seems tricky...

Time *is* really tricky, but that's because we humans created a
tricky system. If can ignore issues of timespampts, timezones and
daylight savings time, then time handling in Python can be
simple.

datetime.datetime.strptime can translate the time format above
into datetime.datetime objects, which provide all the methods you
will need.

To find clusters and min and max values you will likely need to
put the datetime objects in a list, and use some Python builtins
and list methods.

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


Re: Default scope of variables

2013-07-08 Thread Neil Cerutti
On 2013-07-07, Steven D'Aprano  wrote:
> On Fri, 05 Jul 2013 13:24:43 +0000, Neil Cerutti wrote:
>
>> for x in range(4):
>>print(x)
>> print(x) # Vader NOoOO!!!
>
> That loops do *not* introduce a new scope is a feature, not a bug. It is 
> *really* useful to be able to use the value of x after the loop has 
> finished.

I don't buy necessarily buy that it's "*really*" useful but I do
like introducing new names in (not really the scope of)
if/elif/else and for statement blocks.

z = record["Zip"]
if int(z) > 9:
zip_code = z[:-4].rjust(5, "0")
zip4 = z[-4:]
else:
  zip_code = z.rjust(5, "0")
  zip4 = ""


As opposed to:

zip_code = None
zip4 = None
z = record["Zip"]
if int(z) > 9:
zip_code = z[:-4].rjust(5, "0")
zip4 = z[-4:]
else:
zip_code = z.rjust(5, "0")
zip4 = ""

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


Re: crack a router passcode

2013-07-09 Thread Neil Cerutti
On 2013-07-09, saadharana  wrote:
> I need to crack my router passcode to see what firmware it's
> running. There's a passcode set but I don't remember it and
> it's not written down anywhere.

No you don't. If it's your router and you forgot the password
just reset it to factory defaults and reconfigure.

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


Re: Editor Ergonomics [was: Important features for editors]

2013-07-09 Thread Neil Cerutti
On 2013-07-09, Jason Friedman  wrote:
> I am right-handed and use a lefty-mouse about 50% of the time.
> It was difficult at first, now I'm almost as fast lefty as
> righty. As has been stated by others, changing the muscles
> being used reduces the impact on any one of them.

That's the system I've adopted. I use the mouse lefty all day
when working and righty all night when playing.

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


Re: hex dump w/ or w/out utf-8 chars

2013-07-09 Thread Neil Cerutti
On 2013-07-08, Dave Angel  wrote:
> I appreciate you've been around a long time, and worked in a
> lot of languages.  I've programmed professionally in at least
> 35 languages since 1967.  But we've come a long way from the
> 6bit characters I used in 1968.  At that time, we packed them
> 10 characters to each word.

One of the first Python project I undertook was a program to dump
the ZSCII strings from Infocom game files. They are mostly packed
one character per 5 bits, with escapes to (I had to recheck the
Z-machine spec) latin-1. Oh, those clever implementors: thwarting
hexdumping cheaters and cramming their games onto microcomputers
with one blow.

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


Re: hex dump w/ or w/out utf-8 chars

2013-07-09 Thread Neil Cerutti
On 2013-07-09, Dave Angel  wrote:
>> One of the first Python project I undertook was a program to
>> dump the ZSCII strings from Infocom game files. They are
>> mostly packed one character per 5 bits, with escapes to (I had
>> to recheck the Z-machine spec) latin-1. Oh, those clever
>> implementors: thwarting hexdumping cheaters and cramming their
>> games onto microcomputers with one blow.
>
> In 1973 I played with encoding some data that came over the
> public airwaves (I never learned the specific radio technology,
> probably used sidebands of FM stations). The data was encoded,
> with most characters taking 5 bits, and the decoded stream was
> like a ticker-tape.  With some hardware and the right software,
> you could track Wall Street in real time.  (Or maybe it had the
> usual 15 minute delay).
>
> Obviously, they didn't publish the spec any place. But some
> others had the beginnings of a decoder, and I expanded on that.
> We never did anything with it, it was just an interesting
> challenge.

Interestingly similar scheme. It wonder if 5-bit chars was a
common compression scheme. The Z-machine spec was never
officially published either. I believe a "task force" reverse
engineered it sometime in the 90's.

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


Re: crack a router passcode

2013-07-09 Thread Neil Cerutti
On 2013-07-09, Chris Angelico  wrote:
> On Tue, Jul 9, 2013 at 11:23 PM, Ferrous Cranus  wrote:
>> Could python somehow brute force http://192.168.1.1/login.php giving user
>> and pass trying to guess the password?
>>
>> Could it be able to pass values to the input boxes of router's web login
>> interface?
>
> It certainly could. It's just simple HTTP requests, which Python
> handles admirably. But this request was sent by a spambot and doesn't
> need a response.

FRANK DREBBIN

  Yes... I know that. Now.

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


Re: hex dump w/ or w/out utf-8 chars

2013-07-13 Thread Neil Hodgson

wxjmfa...@gmail.com:


The FSR is naive and badly working. I can not force people
to understand the coding of the characters [*].


   You could at least *try*.

   If there really was a problem with the FSR and you truly understood 
this problem then surely you would be able to communicate the problem to 
at least one person on the list.


   Neil

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


Re: Homework help requested (not what you think!)

2013-07-17 Thread Neil Cerutti
On 2013-07-16, John Ladasky  wrote:
> So, what I am seeking are suggestions for programming
> assignments that I can give to brand-new students of Python.
> Please keep in mind that none of them are even up to the task
> of a simple algorithm like Bubble Sort -- at least, not yet.

One of the first satisfying programs I wrote as a child
autodidact on my Commodore 64 was a random name generator. There
are lots of workable strategies and the output can be fun.

Hint: Putting together random syllables turned out to be much
more fun than random consonants and vowels.

Markov chains are an advanced technique you could introduce, but
you'd need a huge list of names broken into syllables from
somewhere.

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


Re: Homework help requested (not what you think!)

2013-07-17 Thread Neil Cerutti
On 2013-07-17, Chris Angelico  wrote:
> On Wed, Jul 17, 2013 at 11:20 PM, Neil Cerutti  wrote:
>> Markov chains are an advanced technique you could introduce, but
>> you'd need a huge list of names broken into syllables from
>> somewhere.
>
> You could use names broken into letters... or skip the notion
> of names and just generate words. Lists of words are easy to
> come by (eg /usr/share/dict/words on many Linux systems), so
> you can have some fun without much effort.

That's true. Something like syllables should emerge from markov
chains of letters pretty well, depending on how long the the
chain is.

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


Re: Python 3: dict & dict.keys()

2013-07-24 Thread Neil Cerutti
On 2013-07-24, Peter Otten <__pete...@web.de> wrote:
>> So, my question boils down to:  in Python 3 how is dict.keys()
>> different from dict?  What are the use cases?
>
> I just grepped through /usr/lib/python3, and could not identify
> a single line where some_object.keys() wasn't either wrapped in
> a list (or set, sorted, max) call, or iterated over.
>
> To me it looks like views are a solution waiting for a problem.

Here's a case of using keys as a set-like view from my own
"production" code (i.e., I used it once for one important job):

seen = set()
students = {}
dates = []

for fname in sorted(glob.glob("currentterm201320?.csv")):
print(fname, end="\n\t")
date = get_date(fname)
dates.append(date)
term = fname[-11:-4]
r = reg.registration(term, path=".")
regs = r.keys()
for alt_id in regs & seen:
students[alt_id].append(r[alt_id])
for alt_id in seen - regs:
students[alt_id].append(None)
for alt_id in regs - seen:
students[alt_id] = [None]*(len(dates)-1) + [r[alt_id]]
seen.add(alt_id)

It was a very nice way to to do three different things depending
on the student sin the set I was working with, compared to a
registration list:

Granted the line was originally "regs = set(regs.keys())" before
it occurred to me that it sucked to take what must be equivalent
to a set, convert to a list, and then back to set again.

Thanks to the set-like view of dict.keys it worked just like one
might hope.

Looking at it again "seen" might be a redundant parallel version
of students.keys().

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


Re: import syntax

2013-07-30 Thread Neil Cerutti
On 2013-07-29, Joshua Landau  wrote:
> Sure, just as one light is no brighter or dimmer than another
> when disregarding luminosity.
>
> As people have said, it improves diffs as well. It flows
> quicker into the "from module import things" form (which I oft
> prefer), too.
>
> When asking these questions, ask yourself "why would it
> *compile* differently? It wouldn't. Plus, premature
> optimisation is the root of all evil.
>
> 1) Write your code
> 2) If it's slow:
> 2a) Do you have time? If so:
> 2b) Is it important to speed up, or is the slowness not worth spending the
> hours fixing?
> 2c) Profile it to see what's actually slow
> 2d) Realise that the slow part is not what you thought it was
> 2e) Fix the bit that's slow (and nothing else)
> 2f) Repeat from 2
> 3) Write some more code

1a) Does it work?
1b) Can you prove it?

It's best to at least have some regression tests before you start
refactoring and optimizing.

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


Re: PEP8 79 char max

2013-07-30 Thread Neil Cerutti
On 2013-07-30, Skip Montanaro  wrote:
>> In that gauge I would exclude indentation (you don't count the
>> number of characters the margin takes) 
>
> I don't think anyone reads the margins. :-)
>
> That said, I agree that code and prose are fundamentally
> different beasts.  Still, when reading either and you get to
> the end of the line, you need to shift your gaze down a line
> and back to the left margin (or the left margin plus any
> indentation).  That task becomes more difficult as line length
> increases.

Most research about speed of comprehension of different line
lengths was based on subjects reading prose. The effect of code
line length hasn't been studied extensively.

> As programmers/software engineers, we need to read and write
> both code and text. I think 80 columns is still a decent
> compromise.

So rules of thumb, standardizations, and personal preferences are
mostly what we have to go by.

When code that looks very similar to code you've seen before
really *is* similar to code you've seen before, comprehension
speed can increase. A study of chess masters' ability to memorize
chess positions showed that they were powerfully accurate when
shown positions from real games, but no better than the average
schmoe when shown randomly positioned pieces. So if everyone
basically follows PEP8 we all benefit from playing by the same
game rules, as it were.

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


Re: RE Module Performance

2013-07-30 Thread Neil Hodgson

MRAB:


The disadvantage there is that when you move the cursor you must move
characters around. For example, what if the cursor was at the start and
you wanted to move it to the end? Also, when the gap has been filled,
you need to make a new one.


   The normal technique is to only move the gap when text is added or 
removed, not when the cursor moves. Code that reads the contents, such 
as for display, handles the gap by checking the requested position and 
using a different offset when the position is after the gap.


   Gap buffers work well because changes are generally close to the 
previous change, so require moving only a relatively small amount of 
text. Even an occasional move of the whole contents won't cause too much 
trouble for interactivity with current processors moving multiple 
megabytes per millisecond.


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


Re: PEP8 79 char max

2013-07-31 Thread Neil Cerutti
On 2013-07-31, Tim Chase  wrote:
> On 2013-07-31 07:16, Joshua Landau wrote:
>> On 30 July 2013 18:52, Grant Edwards wrote:
>>> I also find intializers for tables of data to be much more easily
>>> read and maintained if the columns can be aligned.
>> 
>> Why do you have tables in your Python code?
>
> I've had occasion to write things like:
>
>   for name, value, description in (
>   ("cost", 42, "How much it cost"),
>   ("status", 3141, "Status code from ISO-3.14159"),
>   ...
>   ):
> do_something(name, value)
> print(description)
>
> I interpret Grant's statement as wanting the "table" to look like
>
>   for name, value, description in (
>   ("cost",   42,   "How much it cost"),
>   ("status", 3141, "Status code from ISO-3.14159"),
>   ...
>   ):
> do_something(name, value)
> print(description)
>
> which does give some modest readability benefits, but at a
> creation cost I personally am unwilling to pay.

I'm actually OK with the creation cost, but not the maintenance cost.

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


Re: Script that converts between indentation and curly braces in Python code

2013-07-31 Thread Neil Hodgson

Musical Notation:


Is there any script that converts indentation in Python code to curly braces?
The indentation is sometime lost when I copy my code to an application or a 
website.


   pindent.py in the Tools/Scripts directory of Python installations 
does something similar by adding or removing comments that look like


# end if

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


Re: Problem with psycopg2, bytea, and memoryview

2013-07-31 Thread Neil Cerutti
On 2013-07-31, Frank Millman  wrote:
>
> "Antoine Pitrou"  wrote in message 
> news:loom.20130731t114936-...@post.gmane.org...
>> Frank Millman  chagford.com> writes:
>>>
>>> I have some binary data (a gzipped xml object) that I want to store in a
>>> database. For PostgreSQL I use a column with datatype 'bytea', which is
>>> their recommended way of storing binary strings.
>>>
>>> I use psycopg2 to access the database. It returns binary data
>>> in the form of a python 'memoryview'.
>>>
>> [...]
>>>
>>> Using MS SQL Server and pyodbc, it returns a byte string, not
>>> a memoryview, and it does compare equal with the original.
>>>
>>> I can hack my program to use tobytes(), but it would add
>>> complication, and it would be database-specific. I would
>>> prefer a cleaner solution.
>>
>> Just cast the result to bytes (`bytes(row[1])`). It will work
>> both with bytes and memoryview objcts.
>
> Thanks for that, Antoine. It is an improvement over tobytes(),
> but i am afraid it is still not ideal for my purposes.
>
> At present, I loop over a range of columns, comparing 'before'
> and 'after' values, without worrying about their types. Strings
> are returned as str, integers are returned as int, etc. Now I
> will have to check the type of each column before deciding
> whether to cast to 'bytes'.
>
> Can anyone explain *why* the results do not compare equal? If I
> understood the problem, I might be able to find a workaround.

A memoryview will compare equal to another object that supports
the buffer protocol when the format and shape are also equal. The
database must be returning chunks of binary data in a different
shape or format than you are writing it.

Perhaps psycopg2 is returning a chunk of ints when you have
written a chunk of bytes. Check the .format and .shape members of
the return value to see.

>>> x = memoryview(b"12345")
>>> x.format
'B'
>>> x.shape
(5,)
>>> x == b"12345"
True

My guess is you're getting format "I" from psycopg2. Hopefully
there's a way to coerce your desired "B" format interpretation of
the raw data using psycopg2's API.

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


Re: code review

2012-07-03 Thread Neil Cerutti
On 2012-07-02, Chris Angelico  wrote:
> On Tue, Jul 3, 2012 at 1:57 AM, Rick Johnson
> wrote:
>> Poor Chris. That's because you've been brainwashed into believing you
>> must spoon feed your interpreter to get your code working correctly.
>> Stop applying these naive assumptions to Python code. Python knows
>> when you reach the end of a statement, no need for redundant
>> semicolons! Python knows when its reached the end of block and needs
>> to drop back one level, no need for redundant road signs.  Python
>> knows Chris; Python KNOWS!
>
> Why "poor", Ralph?
>
> I am poor in the essence of ignorance's bliss, rich only in the
> never-ending thirst for knowledge and more languages. In me there meet
> a combination of antithetical elements which are at eternal war with
> one another... I hope I make myself clear, lady?

His simple eloquence goes to my very heart!

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


Re: Why doesn't Python remember the initial directory?

2012-08-20 Thread Neil Hodgson

Nobody:


Maybe. On Unix, it's possible that the current directory no longer
has a pathname.


   Its also possible that you do not have permission to successfully 
call getcwd. One example of this I have experienced is the OS X sandbox 
where you can run Python starting in a directory where you have only 
limited permissions.


   getcwd works by calling readdir and lstat and looping up from the 
current directory to the root to build the whole path so will break 
without read permissions on directories:

http://www.opensource.apple.com/source/Libc/Libc-763.13/gen/FreeBSD/getcwd.c

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


Re: How do I display unicode value stored in a string variable using ord()

2012-08-21 Thread Neil Hodgson

Steven D'Aprano:


Using variable-sized strings like UTF-8 and UTF-16 for in-memory
representations is a terrible idea because you can't assume that people
will only every want to index the first or last character. On average,
you need to scan half the string, one character at a time. In Big-Oh, we
can ignore the factor of 1/2 and just say we scan the string, O(N).


   In the majority of cases you can remove excessive scanning by 
caching the most recent index->offset result. If the next index request 
is nearer the cached index than to the beginning then iterate from that 
offset. This converts many operations from quadratic to linear. Locality 
of reference is common and can often be reasonably exploited.


   However, exposing the variable length nature of UTF-8 allows the 
application to choose efficient techniques for more cases.


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


Re: Flexible string representation, unicode, typography, ...

2012-08-23 Thread Neil Hodgson

wxjmfa...@gmail.com:


Small illustration. Take an a4 page containing 50 lines of 80 ascii
characters, add a single 'EM DASH' or an 'BULLET' (code points>  0x2000),
and you will see all the optimization efforts destroyed.


sys.getsizeof('a' * 80 * 50)

4025

sys.getsizeof('a' * 80 * 50 + '•')

8040


   This example is still benefiting from shrinking the number of bytes 
in half over using 32 bits per character as was the case with Python 3.2:


>>> sys.getsizeof('a' * 80 * 50)
16032
>>> sys.getsizeof('a' * 80 * 50 + '•')
16036
>>>

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


Re: Flexible string representation, unicode, typography, ...

2012-08-27 Thread Neil Hodgson

wxjmfa...@gmail.com:


Go "has" the integers int32 and int64. A rune ensure
the usage of int32. "Text libs" use runes. Go has only
bytes and runes.


Go's text libraries use UTF-8 encoded byte strings. Not arrays of 
runes. See, for example,

http://golang.org/pkg/regexp/

   Are you claiming that UTF-8 is the optimum string representation and 
therefore should be used by Python?


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


Re: Comparing strings from the back?

2012-09-04 Thread Neil Hodgson

Roy Smith:


I'm wondering if it might be faster to start at the ends of the strings
instead of at the beginning?  If the strings are indeed equal, it's the
same amount of work starting from either end.


   Most people write loops that go forwards. This leads to the 
processor designers prioritizing detection mechanisms like cache 
prefetching for that case.


   However, its not always the situation: a couple of years ago Intel 
contributed a memcpy implementation to glibc that went backwards for a 
performance improvement. An explanation from a related patch involves 
speculative and committed operations and address bits on some processors 
and quickly lost me:

paragraph 3 of
http://lists.freedesktop.org/archives/pixman/2010-August/000423.html

   The memcpy patch was controversial as it broke Adobe Flash which 
assumed memcpy was safe like memmove.


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


Re: Comparing strings from the back?

2012-09-17 Thread Neil Hodgson

Ethan Furman:

*plonk*


   I can't work out who you are plonking. While more than one of the 
posters on this thread seem worthy of a good plonk, by not including 
sufficient context, you've left me feeling puzzled. Is there a guideline 
for this in basic netiquette?


   Neil

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


Re: Batching HTTP requests with httplib (Python 2.7)

2012-09-18 Thread Neil Cerutti
On 2012-09-14, Xavier Combelle  wrote:
> Le 14/09/2012 12:56, Dwight Hutto a ?crit :
>> service_num_list = [num for num in range(0,5)]
>> for service_num in service_num_list:
>>  eval("web_service_call%i(%i)" % (service_num,service_num))
>>
>>
> service_num_list = [num for num in range(0,5)]

service_num_list = list(range(5))

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


Re: Decorators not worth the effort

2012-09-18 Thread Neil Cerutti
On 2012-09-14, Chris Angelico  wrote:
> But then again, who actually ever needs fibonacci numbers?

If it should happen that your question is not facetious:

http://en.wikipedia.org/wiki/Fibonacci_number#Applications

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


Re: sum works in sequences (Python 3)

2012-09-19 Thread Neil Cerutti
On 2012-09-19, Franck Ditter  wrote:
> Hello,
> I wonder why sum does not work on the string sequence in Python 3 :
>
>>>> sum((8,5,9,3))
> 25
>>>> sum([5,8,3,9,2])
> 27
>>>> sum('rtarze')
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>
> I naively thought that sum('abc') would expand to 'a'+'b'+'c'
> And the error message is somewhat cryptic...

You got that error message because the default value for the
second 'start' argument is 0. The function tried to add 'r' to 0.
That said:

>>> sum('rtarze', '')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: sum() can't sum strings [use ''.join(seq) instead]

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


Re: sum works in sequences (Python 3)

2012-09-19 Thread Neil Cerutti
On 2012-09-19, Ian Kelly  wrote:
> It notes in the doc string that it does not work on strings:
>
> sum(...)
> sum(sequence[, start]) -> value
>
> Returns the sum of a sequence of numbers (NOT strings) plus
> the value of parameter 'start' (which defaults to 0).  When
> the sequence is empty, returns start.
>
> I think this restriction is mainly for efficiency.  sum(['a',
> 'b', 'c', 'd', 'e']) would be the equivalent of 'a' + 'b' + 'c'
> + 'd' + 'e', which is an inefficient way to add together
> strings.  You should use ''.join instead:

While the docstring is still useful, it has diverged from the
documentation a little bit.

  sum(iterable[, start]) 

  Sums start and the items of an iterable from left to right and
  returns the total. start defaults to 0. The iterable‘s items
  are normally numbers, and the start value is not allowed to be
  a string.

  For some use cases, there are good alternatives to sum(). The
  preferred, fast way to concatenate a sequence of strings is by
  calling ''.join(sequence). To add floating point values with
  extended precision, see math.fsum(). To concatenate a series of
  iterables, consider using itertools.chain().

Are iterables and sequences different enough to warrant posting a
bug report?

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


Re: howto handle nested for

2012-09-28 Thread Neil Cerutti
On 2012-09-28, Laszlo Nagy  wrote:
> In your example, it seem that the iterable of the for loop is
> always the same: range(n_sysms). It seems to be a number. Is
> that true? If that is so, then here is something useful:
>
> import copy
>
> class MultiLevelIterator(object):
>  def __init__(self,levels,n):
>  assert(levels>0)
>  assert(n>0)
>  self.levels = levels
>  self.values = [0]*levels
>  self.n = n
>
>  def __iter__(self):
>  return self
>
>  def next(self):
>  res = copy.copy(self.values)
>  idx = self.levels-1
>  while idx>=0:
>  self.values[idx]+=1
>  if self.values[idx]>=self.n:
>  self.values[idx] = 0
>  idx-=1
>  else:
>  return res
>  raise StopIteration
>
> i = MultiLevelIterator(2,3)
> for values in i:
>  print values
>
> This will print:
>
> [0, 0]
> [0, 1]
> [0, 2]
> [1, 0]
> [1, 1]
> [1, 2]
> [2, 0]
> [2, 1]

It looks like you might have missed the last one. Also, be sure
to check itertools for occasionally for cool stuff like this.

>>> for values in itertools.product(range(3), repeat=2):
...   print(values)
...
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)

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


zip_longest_by

2012-10-17 Thread Neil Cerutti
While working through Project Euler, a fun source of exercises, I
composed the following iterator recipe to yield from multiple
iterators in fixed-length groups:

import itertools

def zip_longest_by(*args, fillvalue=None, n=1, grouper=tuple):
"""Yield n at a time from each of the args, with padding.
It terminates when the longest iterator is exhausted.

>>> for i, j in zip_longest_by("ABCDEFGH", "HIJKL",
...   fillvalue="-", n=3, grouper=''.join):
... print(i, j)
ABC HIJ
DEF KL-
GH- ---

>>> for n1, n2 in zip_longest_by(reversed('1234'), reversed('678'),
...   fillvalue='0', n=3, grouper=lambda a: ''.join(reversed(a))):
... print(n1, n2)
234 678
001 000
"""
it = itertools.zip_longest(*args, fillvalue=fillvalue)
while True:
accum = list()
try:
for i in range(n):
accum += zip(*next(it))
except StopIteration:
for i in range(n - i):
accum.append(tuple(itertools.repeat(fillvalue, len(args
yield tuple(grouper(item) for item in zip(*accum))
break
yield tuple(grouper(item) for item in zip(*accum))

The interface could stand improvement. I find the grouper
argument very convenient, but none of the other grouping
iterators find it needful. Forcing n to be a keyword argument is
unfortunate as well.

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


Re: A desperate lunge for on-topic-ness

2012-10-18 Thread Neil Cerutti
On 2012-10-18, Den  wrote:
> But I have to say I'm amused by the whole question, and others
> related to PEP8.  A quick aside, the width of our roads all go
> back to the width of a two horse rig.  The suggested maximum of
> 80 characters goes back to teletype machines, and IBM cards,
> and character based terminals
>
> Should that really be the basis for a suggested style now?

I had to use vim's reformatting powers to fix your first
paragraph. ;)

http://www.codinghorror.com/blog/2006/06/text-columns-how-long-is-too-long.html

Code is limited to one column, is left-justified, and
comprehension is much more important than reading speed. There
are lots of studies, but they are all about blocks of text, not
code.

Though technology has moved along swiftly, keeping your code
accessible to the guy using a crummy old console xterm might
still be worthwhile, and it makes printouts easy to create.

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


Re: get each pair from a string.

2012-10-23 Thread Neil Cerutti
On 2012-10-23, wxjmfa...@gmail.com  wrote:
> Why bother with speeed?

Because the differences between O(N), O(log(N)) and O(N ** 2)
operations are often relevant.

A Python string replace function experiencing a slow-down from
previous versions doesn't absolve me from making informed choices
of algorithm and implentation.

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


Re: simple string format question

2012-10-25 Thread Neil Cerutti
On 2012-10-25, Piet van Oostrum  wrote:
> Adrien  writes:
>
>> print "{:.3g}".format(2.356)  # this rounds up
>
> But:
>
>>>> print "{:.3g}".format(12.356) 
> 12.4
>>>> print "{:.3g}".format(123.356) 
> 123


  The precision is a decimal number indicating how many digits
  should be displayed after the decimal point for a floating
  point value formatted with 'f' and 'F', or before and after the
  decimal point for a floating point value formatted with 'g' or
  'G'. For non-number types the field indicates the maximum field
  size - in other words, how many characters will be used from
  the field content. The precision is not allowed for integer
  values.

So g will print a specific number of significant digits, so it
won't do what Adrien wants.

And f will print a fixed number of digits after the decimal
point, so it won't do want Adrien wants.

Adrien, you will need to do some post-processing on fixed point
output to remove trailing zeroes.

>>> print("{:.2f}".format(2.1).rstrip('0'))
2.1
>>> print("{:.2f}".format(2.127).rstrip('0'))
2.13

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


Re: bit count or bit set && Python3

2012-10-25 Thread Neil Cerutti
On 2012-10-25, Steven D'Aprano  wrote:
> On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote:
>> On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes
>> 
>> wrote:
>>> Simple, easy, faster than a Python loop but not very elegant:
>>>
>>>bin(number).count("1")
>> 
>> Unlikely to be fast.
>
> Oh I don't know about that.

Yes indeed! Python string operations are fast enough and its
arithmetic slow enough that I no longer assume I can beat a neat
lexicographical solution. Try defeating the following with
arithmetic:

def is_palindrom(n):
   s = str(n)
   return s = s[::-1]

> Here's some timing results using Python 2.7:

Excellent work.

You can of course drop to C for arithmetic and likely triumph
over Python strings. That's never been applicable for me, though.

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


Re: bit count or bit set && Python3

2012-10-25 Thread Neil Cerutti
On 2012-10-25, Neil Cerutti  wrote:
> Try defeating the following with arithmetic:
>
> def is_palindrom(n):
>s = str(n)
>return s = s[::-1]

Sorry for the typos. It should've been:

def is_palindrome(n):
   s = str(n)
   return s == s[::-1]

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


Re: bit count or bit set && Python3

2012-10-26 Thread Neil Cerutti
On 2012-10-25, Ian Kelly  wrote:
> On Thu, Oct 25, 2012 at 2:00 PM, Neil Cerutti
>  wrote:
>> Yes indeed! Python string operations are fast enough and its
>> arithmetic slow enough that I no longer assume I can beat a
>> neat lexicographical solution. Try defeating the following
>> with arithmetic:
>>
>> def is_palindrom(n):
>>s = str(n)
>>return s = s[::-1]
>
> Problems like these are fundamentally string problems, not math
> problems.  The question being asked isn't about some essential
> property of the number,  but about its digital representation.
> Certainly they can be reasoned about mathematically, but the
> fact remains that the math being done is about the properties
> of strings.

The "unexpected" part, to me, is that an optimal arithmetic based
solution conceptually is more efficient. You need to compute just
half the digits of the number and then perform a contant compare
operation.

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


Re: attaching names to subexpressions

2012-10-29 Thread Neil Cerutti
On 2012-10-28, Devin Jeanpierre  wrote:
>>> The 'canonical way'
>>> while True:
>>>  line = complex_expression
>>>  if not line:
>>>  break
>>>  do_something_with(line)
>>>
>>> avoids this problem, but I was never really convinced about the beauty /
>>> readbility of this construct.
>>>
>>> In
>>> my opinion I shouldn't be obliged to read any of the indented lines of
>>> the while statement on a first 'visual' pass through somebody elses code
>>> and still be able to see what the loop iterates through.
>>
>> Fine. Then write your code as:
>>
>> line = function(x, y, z)
>> while line:
>>  do something with(line)
>>  line = function(x, y, z)
>
> We have a problem, and two solutions. Solution 1 has downside
> A, and solution 2 has downside B. If he complains about
> downside A, you say, well, use solution 2. If he complains
> about downside B, you say, well, use solution 1.
>
> What if he wants to avoid both downsides A and B? What solution
> does he use then?

You abandon the while loop and compose a generator.

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


Re: csv read clean up and write out to csv

2012-11-02 Thread Neil Cerutti
On 2012-11-02, Sacha Rook  wrote:
> Hi
>
> I have a problem with a csv file from a supplier, so they
> export data to csv however the last column in the record is a
> description which is marked up with html.
>
> trying to automate the processing of this csv to upload
> elsewhere in a useable format. If i open the csv with csved it
> looks like all the records aren't escaped correctly as after a
> while i find html tags and text on the next line/record.

Maybe compose a simple parter to disambiguate the lines from the
file.

Something like (you'll have to write is_html, and my Python 2 is
mighty rusty, you'll have to fix up. Note that infile doesn't
have to be in binary mode with this scheme, but it would fail on
bizarre newlines in the file):

def parse_records(iter):
for line in iter:
if is_html(line):
yield ('html', line)
else:
yield ('csv', csv.reader([line.strip()]).next())

infile = open('c:\data\input.csv')
outfile = open('c:\data\output.csv', 'wb')

writer = csv.writer(outfile)

for tag, rec in parse_record(infile):
if tag == 'html':
print rec
elif tag == 'csv':
writer.writerow(rec)
else:
raise ValueError("Unknown record type %s" % tag)

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


Re: creating size-limited tar files

2012-11-07 Thread Neil Cerutti
On 2012-11-07, andrea crotti  wrote:
> Simple problem, given a lot of data in many files/directories, I
> should create a tar file splitted in chunks <= a given size.
>
> The simplest way would be to compress the whole thing and then split.
>
> At the moment the actual script which I'm replacing is doing a
> "system('split..')", which is not that great, so I would like to do it
> while compressing.
>
> So I thought about (in pseudocode)
>
> while remaining_files:
> tar_file.addfile(remaining_files.pop())
> if size(tar_file) >= limit:
>  close(tar_file)
>  tar_file = new_tar_file()
>

I have not used this module before, but what you seem to be
asking about is:

TarFile.gettarinfo().size

But your algorithm stops after the file is already too big.

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


  1   2   3   4   5   6   7   8   9   10   >