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: 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: 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: 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-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: 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: 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: 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: 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: 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


Re: Python questions help

2012-11-19 Thread Neil Cerutti
On 2012-11-16, Chris Angelico  wrote:
> On Sat, Nov 17, 2012 at 5:00 AM, rh
>  wrote:
>> "How many people think programming skills are inherent?" i.e.
>> that some people are just born with the gift to be good
>> programmers Result: very few hands raised maybe a couple
>> (possibly non-progammers??)
>
> Maybe, but there's definitely something that happens close to
> birth. If your parents give you the name Chris, you're more
> likely to become a geek and a programmer.

There are people with rare talent who can program in a way that
most others can't, .e.g, Chris Sawyer. But, as Louis Moyse, a
great musician remarked: "Without hard work, talent means
nothing."

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


Re: 10 sec poll - please reply!

2012-11-20 Thread Neil Cerutti
On 2012-11-20, Dave Angel  wrote:
> I also vote for send_keys(), even before I saw Chris' reply.
>
> 'type' is too overloaded a word.  For example, in Windows, it's
> the command to display the contents of a file - it types it to
> the screen.

type is a nice verb, but since it's also a well-used noun it's
perhaps not quite as good as send.

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


Re: Problems on these two questions

2012-11-20 Thread Neil Cerutti
On 2012-11-19, Dennis Lee Bieber  wrote:
> On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090
> <129k...@gmail.com> declaimed the following in
> gmane.comp.python.general:
>
>> 
>> I all of the other problems but I have issues with these:
>> 
>> 1.Given a positive integer  n , assign True to  is_prime if  n
>> has no factors other than  1 and itself. (Remember,  m is a
>> factor of  n if  m divides  n evenly.) 
>>
>   Google: Sieve of Eratosthenes (might be mis-spelled)

The sieve is a nice simple and fast algorithm, provided there's a
bound on the highest n you need to check. It's much less simple
and less fast if n is unbounded or the bound is unknown.

Python's standard library isn't equipped with the an obvious
collection to use to implement it either.

>> 2.An  arithmetic progression is a sequence of numbers in which
>> the distance (or difference) between any two successive
>> numbers if the same. This in the sequence  1, 3, 5, 7, ... ,
>> the distance is 2 while in the sequence  6, 12, 18, 24, ... ,
>> the distance is 6. 
>> 
>>  Given the positive integer  distance and the positive integer
>>  n , associate the variable  sum with the sum of the elements
>>  of the arithmetic progression from  1 to  n with distance
>>  distance . For example, if  distance is 2 and  n is  10 ,
>>  then  sum would be associated with  26 because  1+3+5+7+9 =
>>  25 . 
>
> So, what have you tried?
>
> Consider: you have a "sum", you have a sequence of "elements"
> (based upon a spacing "distance"), and you have an upper bound
> "n"
>
> You need to generate a sequence of "elements" starting at "1",
> using "distance" as the spacing, until you exceed "n", and you
> want to produce a "sum" of all those elements...

This one's sort of a trick question, depending on your definition
of "trick". The most obvious implementation is pretty good.

In both cases a web search and a little high-density reading
provides insights and examples for the OP.

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


Re: Compare list entry from csv files

2012-11-27 Thread Neil Cerutti
On 2012-11-27, Anatoli Hristov  wrote:
> Thanks for your help. I will do my best for the forum :)
>
> I advanced a little bit with the algorithm and at least I can
> now extract and compare the fields :) For my beginner skills I
> think this is too much for me. Now next step is to add the
> second field with the number to the Namelist and copy it to a
> third filename I suppose.

I had to write a similar type of program, and I imagine it's a
common problem. Sometimes new students provide incorrect SSN's or
simply leave them blank. This makes it impossible for us to match
their application for financial aid to their admissions record.

You have to analyze how you're going to match records.

In my case, missing SSN's are one case. A likeley match in this
case is when the names are eerily similar.

In the other case, where they simply got their SSN wrong, I have
to check for both a similar SSN and a similar name.

But you still have to define "similar." I looked up an algorithm
on the web called Levenshtein Distance, and implemented it like
so.

def levenshteindistance(first, second):
"""Find the Levenshtein distance between two strings."""
if len(first) > len(second):
first, second = second, first
if len(second) == 0:
return len(first)
first_length = len(first) + 1
second_length = len(second) + 1
distance_matrix = [[0] * second_length for x in range(first_length)]
for i in range(first_length):
   distance_matrix[i][0] = i
for j in range(second_length):
   distance_matrix[0][j]=j
for i in range(1, first_length):
for j in range(1, second_length):
deletion = distance_matrix[i-1][j] + 1
insertion = distance_matrix[i][j-1] + 1
substitution = distance_matrix[i-1][j-1]
if first[i-1] != second[j-1]:
substitution += 1
distance_matrix[i][j] = min(insertion, deletion, substitution)
return distance_matrix[first_length-1][second_length-1]

The algorithm return a count of every difference between the two
strings, from 0 to the length of the longest string.

Python provides difflib, which implements a similar algorithm, so
I used that as well (kinda awkwardly). I used
difflib.get_close_matches to get candidates, and then
difflib.SequenceMatcher to provide me a score measuring the
closeness.

matches = difflib.get_close_matches(s1, s2)
for m in matches:
scorer = difflib.SequenceMatcher(None, s1, m)
ratio = scorer.ratio()
if ratio == 0.0:
# perfect match
if ratio > MAX_RATIO: # You gotta choose this. I used 0.1 
# close match

The two algorithms come up with different guesses, and I pass on
their suggestions for fixes to a human being. Both versions of
the program take roughly 5 minutes to run the comparison on
2000-12000 records between the two files.

I like the results of Levenshtein distance a little better, but
difflib finds some stuff that it misses.

In your case, the name is munged horribly in one of the files so
you'll first have to first sort it out somehow.

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


Re: Compare list entry from csv files

2012-11-27 Thread Neil Cerutti
On 2012-11-27, Anatoli Hristov  wrote:
> Thank you all for the help, but I figured that out and the
> program now works perfect. I would appreciate if you have some
> notes about my script as I'm noob :) Here is the code:
>
> import csv
>
> origf = open('c:/Working/Test_phonebook.csv', 'rt')
> secfile = open('c:/Working/phones.csv', 'rt')

csv module expects files to be opened in binary mode in Python
versions less than version 3.0. For Python versions >= 3.0, you
use the special keyword argument, newlines='', instead.

> phonelist = []
> namelist = []

The structure of your program is poor. It's workable for such a
short script, and sometimes my first cuts are similar, but it's
better to get out of the habit right away.

Once you get this working the way you'd like you should clean up
the structure as a service to your future self.

> names = csv.reader(origf, delimiter=';')
> phones = csv.reader(secfile, delimiter=';')

You csv files don't seem to have header rows, but even so you can
improve your code by providing fieldnames and using a DictReader
instead.

name_reader = csv.DictReader(origf, fieldnames=[
 'Name', 'Blah', 'Phone#'])

Then you can read from records with

  name = row['Name']

instead of using bare, undocumented integers.

> for tel in phones:
> phonelist.append(tel)
>
> def finder(name_row,rows):
> for ex_phone in phonelist:
> telstr = ex_phone[0].lower()
> if telstr.find(name_row) >= 0:

This strikes me as a crude way to match names. You don't really
want Donald to match perfectly with McDonald, do you? Or for
Smith to match with Smithfield?

Yes, a human being will clean it up, but your program can do a
better job.

> print "\nName found: %s" % name_row
> namelist[rows][-1] = ex_phone[-1].lower()
> else:
> pass
> return
>
> def name_find():
> rows = 0
> for row in names:
> namelist.append(row)
> name_row = row[0].lower()
> finder(name_row,rows)
> rows = rows+1

You can use the useful enumerate function instead of your own
counter.

  for rows, row in enumerate(names):

...though I would find 'rownum' or 'num' or just 'i' better than
the name 'rows', which I find confusing.

> name_find()
> ofile  = open('c:/Working/ttest.csv', "wb")
> writer = csv.writer(wfile, delimiter=';')
> for insert in namelist:
> writer.writerow(insert)
> wfile.close()

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


Re: Textmining

2012-11-30 Thread Neil Cerutti
On 2012-11-30, subhabangal...@gmail.com  wrote:
> Python has one textming library, but I am failing to install it
> in Windows. If any one can kindly help.

More information needed.

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


Re: List problem

2012-12-03 Thread Neil Cerutti
On 2012-12-02, Thomas Bach  wrote:
> On Sun, Dec 02, 2012 at 04:16:01PM +0100, Lutz Horn wrote:
>> 
>> len([x for x in l if x[1] == 'VBD'])
>> 
>
> Another way is
>
> sum(1 for x in l if x[1] == 'VBD')
>
> which saves the list creation.

To also index them:

vbdix = [i for i, a in emumerate(l) if a[1] == 'VBD']
vbdno = len(indices)

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


Re: Conversion of List of Tuples

2012-12-04 Thread Neil Cerutti
On 2012-12-04, Hans Mulder  wrote:
> It's considered bad style to use map it you don't want the list it
> produces.
>
>> There are more ways:
>> 
>>>>> from operator import add
>>>>> reduce(add, a)
>> (1, 2, 3, 4)
>
> There's a built-in that does "reduce(operator.add"; it's called "sum":
>
>>>> sum(a, ())
> (1, 2, 3, 4)

I thought that sort of thing would cause a warning. Maybe it's
only for lists.

Here's the recipe from the itertools documentation:

def flatten(listOfLists):
"Flatten one level of nesting"
return chain.from_iterable(listOfLists)

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


Re: CSV out of range

2012-12-04 Thread Neil Cerutti
On 2012-12-04, Anatoli Hristov  wrote:
> The issue is now solved I did:
>
> for x in mylist:
> try:
> sku.append(x[4])
> except IndexError:
> pass
>
> Thank you for your help

Optionally:

for x in mylist:
if len(x) >= 4:
sku.append(x[4])

But do you really need to save the whole file in a list first?
You could simply do:

for record in csvreader:
  if len(record) >= 4:
  sku.append(record[4])

Or even:

sku = [record[4] for record in csvreader if len(record) >= 4]

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-04 Thread Neil Cerutti
On 2012-12-04, Nick Mellor  wrote:
> I have a file full of things like this:
>
> "CAPSICUM RED fresh from Queensland"
>
> Product names (all caps, at start of string) and descriptions
> (mixed case, to end of string) all muddled up in the same
> field. And I need to split them into two fields. Note that if
> the text had said:
>
> "CAPSICUM RED fresh from QLD"
>
> I would want QLD in the description, not shunted forwards and
> put in the product name. So (uncontrived) list comprehensions
> and regex's are out.
>
> I want to split the above into:
>
> ("CAPSICUM RED", "fresh from QLD")
>
> Enter dropwhile and takewhile. 6 lines later:
>
> from itertools import takewhile, dropwhile
> def split_product_itertools(s):
> words = s.split()
> allcaps = lambda word: word == word.upper()
> product, description = takewhile(allcaps, words), dropwhile(allcaps, 
> words)
> return " ".join(product), " ".join(description)
>
> When I tried to refactor this code to use while or for loops, I
> couldn't find any way that felt shorter or more pythonic:

I'm really tempted to import re, and that means takewhile and
dropwhile need to stay. ;)

But seriously, this is a quick implementation of my first thought.

description = s.lstrip(string.ascii_uppercase + ' ')
product = s[:-len(description)-1]

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-04 Thread Neil Cerutti
On 2012-12-04, Nick Mellor  wrote:
> Hi Neil,
>
> Nice! But fails if the first word of the description starts
> with a capital letter.

Darn edge cases.

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-04 Thread Neil Cerutti
On 2012-12-04, Nick Mellor  wrote:
> I love the way you guys can write a line of code that does the
> same as 20 of mine :)
>
> I can turn up the heat on your regex by feeding it a null
> description or multiple white space (both in the original
> file.) I'm sure you'd adjust, but at the cost of a more complex
> regex.

A re.split should be able to handle this without too much hassle.

The simplicity of my two-line version will evaporate pretty
quickly to compensate for edge cases.

Here's one that can handle one of the edge cases you mention, but
it's hardly any shorter than what you had, and it doesn't
preserve non-standard whites space, like double spaces.

def prod_desc(s):
"""split s into product name and product description. Product
name is a series of one or more capitalized words followed
by white space. Everything after the trailing white space is
the product description.

>>> prod_desc("CAR FIFTY TWO Chrysler LeBaron.")
['CAR FIFTY TWO', 'Chrysler LeBaron.']
"""
prod = []
desc = []
target = prod
for word in s.split():
if target is prod and not word.isupper():
target = desc
target.append(word)
return [' '.join(prod), ' '.join(desc)]

When str methods fail I'll usually write my own parser before
turning to re. The following is no longer nice looking at all.

def prod_desc(s):
"""split s into product name and product description. Product
name is a series of one or more capitalized words followed
by white space. Everything after the trailing white space is
the product description.

>>> prod_desc("CAR FIFTY TWO Chrysler LeBaron.")
['CAR FIFTY TWO', 'Chrysler LeBaron.']

>>> prod_desc("MR.  JONESEY   Saskatchewan's finest")
['MR.  JONESEY', "Saskatchewan's finest"]
"""
i = 0
while not s[i].islower():
i += 1
i -= 1
while not s[i].isspace():
i -= 1
start_desc = i+1
while s[i].isspace():
i -= 1
end_prod = i+1
return [s[:end_prod], s[start_desc:]]

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Nick Mellor  wrote:
> Hi Terry,
>
> For my money, and especially in your versions, despite several
> expert solutions using other features, itertools has it. It
> seems to me to need less nutting out than the other approaches.
> It's short, robust, has a minimum of symbols, uses simple
> expressions and is not overly clever. If we could just get used
> to using takewhile.

The main reason most of the solutions posted failed is lack of
complete specification to work with while sumultaneously trying
to make as tiny and simplistic a solution as possible.

I'm struggling with the empty description bug right now. ;)

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Chris Angelico  wrote:
> On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor  wrote:
>>
>> takewhile mines for gold at the start of a sequence, dropwhile
>> drops the dross at the start of a sequence.
>
> When you're using both over the same sequence and with the same
> condition, it seems odd that you need to iterate over it twice.
> Perhaps a partitioning iterator would be cleaner - something
> like this:
>
> def partitionwhile(predicate, iterable):
> iterable = iter(iterable)
> while True:
> val = next(iterable)
> if not predicate(val): break
> yield val
> raise StopIteration # Signal the end of Phase 1
> for val in iterable: yield val # or just "yield from iterable", I think
>
> Only the cold hard boot of reality just stomped out the spark
> of an idea. Once StopIteration has been raised, that's it,
> there's no "resuming" the iterator. Is there a way around that?
> Is there a clean way to say "Done for now, but next time you
> ask, there'll be more"?
>
> I tested it on Python 3.2 (yeah, time I upgraded, I know).

Well, shoot! Then this is a job for groupby, not takewhile.

def prod_desc(s):
"""split s into product name and product description.

>>> prod_desc("CAR FIFTY TWO Chrysler LeBaron.")
['CAR FIFTY TWO', 'Chrysler LeBaron.']

>>> prod_desc("MR. JONESEY Saskatchewan's finest")
['MR. JONESEY', "Saskatchewan's finest"]

>>> prod_desc("no product name?")
['', 'no product name?']

>>> prod_desc("NO DESCRIPTION")
['NO DESCRIPTION', '']
"""
prod = ''
    desc = ''
for k, g in itertools.groupby(s.split(),
key=lambda w: any(c.islower() for c in w)):
a = ' '.join(g)
if k:
desc = a 
else:
prod = a
return [prod, desc]

This has no way to preserve odd white space which could break
evil product name differences.

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Ian Kelly  wrote:
> On Wed, Dec 5, 2012 at 7:34 AM, Neil Cerutti  wrote:
>> Well, shoot! Then this is a job for groupby, not takewhile.
>
> The problem with groupby is that you can't just limit it to two groups.
>
>>>> prod_desc("CAPSICUM RED fresh from QLD")
> ['QLD', 'fresh from']
>
> Once you've got a false key from the groupby, you would need to
> pretend that any subsequent groups are part of the false group
> and tack them on.

Whoops! Yep, that was from the very beginning of the thread.

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


Re: why does dead code costs time?

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Bruno Dupuis  wrote:
> Hi,
>
> I'm interested in compilers optimizations, so I study python
> compilation process
>
> I ran that script:
>
> import timeit
>
> def f(x):
> return None
>
> def g(x):
> return None
> print(x)
>
> number = 1
>
> print(timeit.timeit('f(1)',setup="from __main__ import f", number=number))
> print(timeit.timeit('g(1)',setup="from __main__ import g", 
> number=number))   
>
> print(dis.dis(f))
> print(dis.dis(g))
>
> It gives this output:
>
> 0.003460251959040761
> 0.004164454061537981
>  17   0 LOAD_CONST   0 (None) 
>   3 RETURN_VALUE 
> None
>  20   0 LOAD_GLOBAL  0 (None) 
>   3 RETURN_VALUE 
>
>  21   4 LOAD_GLOBAL  1 (print) 
>   7 LOAD_FAST0 (x) 
>  10 CALL_FUNCTION1 (1 positional, 0 keyword pair) 
>  13 POP_TOP  
> None
>
> I do not understand why the dead code `print(x)` takes time (~20% in
> that case). As we see in the opcode, a call to g(1) returns immediately, so
> there should be no delay at all. Where am i wrong?
>
> mmhh... it comes to me now that the gap must be in function loading time...
> I'll check ceval.c
>
> However, isn't there a room for a slight optim here? (in this case, the
> dead code is obvious, but it may be hidden by complex loops and
> conditions)

Maybe it's the difference between LOAD_CONST and LOAD_GLOBAL. We
can wonder why g uses the latter.

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Nick Mellor  wrote:
> Hi Neil,
>
> Here's some sample data. The live data is about 300 minor
> variations on the sample data, about 20,000 lines.

Thanks, Nick.

This slight variation on my first groupby try seems to work for
the test data.

def prod_desc(s):
prod = []
desc = []
for k, g in itertools.groupby(s.split(),
key=lambda w: any(c.islower() for c in w)):
if prod or k:
desc.extend(g)
else:
prod.extend(g)
return [' '.join(prod), ' '.join(desc)]

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Nick Mellor  wrote:
> Neil,
>
> Further down the data, found another edge case:
>
> "Spring ONION from QLD"
>
> Following the spec, the whole line should be description
> (description starts at first word that is not all caps.) This
> case breaks the latest groupby.

A-ha! I did check your samples for the case of an empty product
name and not find any started to think it couldn't happen.

Change

   if prod or k:

to

   if desc or prod or k:

If this data file gets any weirder, let me know. ;)

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-06 Thread Neil Cerutti
On 2012-12-05, Vlastimil Brom  wrote:
> ... PARSNIP, certified organic

I'm not sure on this one.

> ('PARSNIP', ', certified organic')

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


Re: Confused compare function :)

2012-12-06 Thread Neil Cerutti
On 2012-12-06, Steven D'Aprano
 wrote:
> total = 0
> for s in list_of_strings:
> try:
> total += int(s)
> except ValueError:
> pass  # Not a number, ignore it.

If it's internal data, perhaps. Of course, that would mean I had
the option of *not* creating that stupid list_of_strings.

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


Re: Confused compare function :)

2012-12-07 Thread Neil Cerutti
On 2012-12-07, Steven D'Aprano  wrote:
> On Thu, 06 Dec 2012 13:51:29 +0000, Neil Cerutti wrote:
>
>> On 2012-12-06, Steven D'Aprano
>>  wrote:
>>> total = 0
>>> for s in list_of_strings:
>>> try:
>>> total += int(s)
>>> except ValueError:
>>> pass  # Not a number, ignore it.
>> 
>> If it's internal data, perhaps. Of course, that would mean I
>> had the option of *not* creating that stupid list_of_strings.
>
> Not necessarily, it depends on the application.

I agree. I personally wouldn't want, e.g., 12.0 to get silently
skipped so I've never done this. I've never done anything like
that, but I can imagine it.

> If you have a spreadsheet, and create a formula =SUM(A1:A50)
> the expected behaviour is to just skip anything that is not a
> valid number, not raise an error. Sometimes correct
> application-level behaviour is to just ignore input that it
> doesn't care about.
>
> One of the things that I used to despise with a passion about
> MYOB is that if you clicked on the window outside of a button
> or field, it would scream at you "ERROR ERROR ERROR - that was
> not a button or field" That is to say, it would beep. I
> mean, *who fscking cares* that the user clicks in the window
> background? It's a harmless tick, like tapping your desk, just
> ignore it.

What happens in Word during a Mail Merge if an invalid field is
in the data file, one you don't even care about: You get to click
on a modular dialog box for every record you're merging with to
say IGNORE. And you can't quit.

> As a general rule, library functions should be strict about
> reporting errors, while applications may be more forgiving
> about errors that they don't care about. The "don't care about"
> part is important though -- your word processor shouldn't care
> about low level networking errors, but it should care if it
> can't save a file to a network drive. 

You have to draw the line somewhere in any case, and drawing it
all the way over to IGNORE is bound right some of the time. It
would be, I guess, Cargo Cult programming to never ignore errors.

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


Re: Error .. Please Help

2012-12-13 Thread Neil Cerutti
On 2012-12-13, inshu chauhan  wrote:
> For this I put an else clause at end but is there a better way
> to avoid this kind of situation ??

An if-elif-else structure is usually broken if you leave out the
else part. When I don't expect it to ever actually happen when
the program is working correctly it looks like this:

   else:
  raise SomeException("{} can't happen!".format(the_context))

else: raise exception constructs have saved me a lot of time.

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


Re: New to python, do I need an IDE or is vim still good enough?

2013-01-02 Thread Neil Cerutti
On 2012-12-30, Jamie Paul Griffin  wrote:
> Stick with what you've been using for the last couple of
> decades. These tools have stood the test of time for a good
> reason: they're powerful, efficient and made for the task of
> programming. 

There is a good Python plugin for Vim that will allow simple
reindenting and a bunch of other cool cursor movement powers I
don't even use. ctags will also work, though I've never really
needed it.

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


Re: New to python, do I need an IDE or is vim still good enough?

2013-01-02 Thread Neil Cerutti
On 2012-12-29, Roy Smith  wrote:
> emacs will parse that, highlight the filenames and line numbers
> and if I type M-`, it'll take me to the line of the next error
> (including opening the file if it's not already open).
>
> I assume other smart editors have similar capabilities.
> Different tools have different combinations of these, or
> slightly different implementations.  Find one you like and
> learn all of it's capabilities.  It makes a huge difference in
> how productive you are.

I used that power all the time writing C and C++ code. It felt
indespensable. But even though I can do the same with Python, it
doesn't feel crucial when writing Python. The errors are more
few. ;)

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


Re: Python is awesome (Project Euler)

2013-01-02 Thread Neil Cerutti
On 2012-12-31, Roy Smith  wrote:
> There's a problem I just worked where you need to find the last
> 10 digits of some million-digit prime.  Python's long ints
> don't help you there.  What does help you is figuring out a way
> to solve the problem that's not brute-force.  I think that's
> what Euler is all about.

I agree. The most interesting part of participating is finding
out how my solution could've been improved or just completely
replaced with zero programming in some memorable cases. My
algebra has gotten a major workout, and I've had to resurrect the
mostly-dead neurons in my skull in charge of calculus.

Participants sometimes come up with terrible failures that
nevertheless find the solution (I'm no exception, though I think
I learn my lesson in the discussion groups). It's a limitation of
the way answers are checked. Working to make a solution that's
complete and extensible yields the most educational benefits, I
think.

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


Re: RE Help splitting CVS data

2013-01-21 Thread Neil Cerutti
On 2013-01-21, Garry  wrote:
> Thanks everyone for your comments.  I'm new to Python, but can
> get around in Perl and regular expressions.  I sure was taking
> the long way trying to get the cvs data parsed.  
>
> Sure hope to teach myself python.  Maybe I need to look into
> courses offered at the local Jr College!
  
There's more than enough free resources online for the
resourceful Perl programmer to get going. It sounds like you
might be interested in Text Processing in Python.

http://gnosis.cx/TPiP/

Also good for your purposes is Dive Into Python.

http://www.diveintopython.net/

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


Re: The best, friendly and easy use Python Editor.

2013-01-24 Thread Neil Cerutti
On 2013-01-24, John Gordon  wrote:
> In  Sharwan Joram 
>  writes:
>
>> use vim.
>
> He said he wanted autocomplete.  Does Vim have that?

Yes, you use its ctags support to get it working, I believe.

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


Re: The best, friendly and easy use Python Editor.

2013-01-24 Thread Neil Cerutti
On 2013-01-24, Tim Chase  wrote:
> On 01/24/13 10:23, Thomas Heller wrote:
>> Am 24.01.2013 16:54, schrieb rusi:
>>> [I personally use emacs. It would be sadistic to make that into a
>>> recommendation]
>>>
>> It would be truly sadistic to force a long-time emacs user to any
>> other editor.
>
> I saw the recommendation for Vim elsewhere on the thread and comment 
> the same as this sub-thread: "I personally use vim. It would be 
> sadistic to make that into a recommendation"  And likewise, it's 
> "truly sadistic to force a long-time vim user to any other editor." :-)
>
> Not that Vim isn't great for programming Python (which I do 
> daily)...it *is*!  It's just not where I'd throw somebody who 
> doesn't already have an existing editor preference *and* doesn't 
> know Python.

I agree.

Vim is great, Emacs is great. I'm glad I know one of them. But
learning one of them is as project unto itself. So selecting
either just for Python is skipping too many decisions and maybe
biting off too big a piece of the snake.

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


Re: using split for a string : error

2013-01-25 Thread Neil Cerutti
On 2013-01-25, Oscar Benjamin  wrote:
> On 24 January 2013 11:35, Chris Angelico  wrote:
>> It's usually fine to have int() complain about any
>> non-numerics in the string, but I must confess, I do sometimes
>> yearn for atoi() semantics: atoi("123asd") == 123, and
>> atoi("qqq") == 0. I've not seen a convenient Python function
>> for doing that. Usually it involves manually getting the
>> digits off the front. All I want is to suppress the error on
>> finding a non-digit. Oh well.
>
> I'm interested to know what the situations are where you want
> the behaviour of atoi().

Right. atoi is no good even in C. You get much better control
using the sprintf family. int would need to return a tuple of the
number it found plus the number of characters consumed to be more
useful for parsing.

>>> intparse("123abc")
(123, 3)

But that would make it might inconvenient for general use.

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


Re: using split for a string : error

2013-01-25 Thread Neil Cerutti
On 2013-01-25, Hans Mulder  wrote:
>> Right. atoi is no good even in C. You get much better control
>> using the sprintf family.
>
> I think you meant sscanf.

Yes, thanks for knocking that huge chunk of rust off of me. ;)

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


  1   2   3   4   5   6   7   8   9   10   >