Re: What's the best way to minimize the need of run time checks?

2016-08-09 Thread Michael Selik
On Tue, Aug 9, 2016 at 2:31 PM Juan Pablo Romero Méndez <
jpablo.rom...@gmail.com> wrote:

> Hello,
>
> In online forums sometimes people complain that they end up having to test
> constantly for None, or that a function's argument has a specific type /
> shape (which immediately brings the following aphorism to mind: "Any
> sufficiently large test suite contains an ad hoc, bug-ridden, slow
> implementation of half of Haskell's type system").
>
> Some responses I've seen to that particular complaint is that they are
> doing it wrong. That they are trying to force a type system into a dynamic
> language (Clojure, Ruby, Python, Javascript, etc).
>
> Do you guys have any resources you like that addresses the following issue:
> What is the best way to use the dynamic features of Python to avoid having
> to write a poor's man type system?
>

It'll be easiest to discuss a specific example. Can you share a chunk of
code which frustrates you?

In general, Python's TypeError and AttributeError serve much the same
purpose as other languages' type systems. Where other languages might force
you to create named, formally specified types, Python allows informally
specified interfaces.

"File-like" is a good example. Rather than go through the frustration of a
formal definition for what is file-like, stay productive and flexible with
duck typing. Objects that don't have the appropriate methods or attributes
will cause a TypeError or AttributeError. If you're passing in the wrong
kind of object, you'll find out almost as soon as you write the code
(because you test early and often), just as you would with a statically
analyzed type system.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-09 Thread Michael Selik
On Tue, Aug 9, 2016 at 2:59 PM Chris Angelico  wrote:

> On Wed, Aug 10, 2016 at 4:51 AM, Michael Selik 
> wrote:
> > "File-like" is a good example. Rather than go through the frustration of
> a
> > formal definition for what is file-like, stay productive and flexible
> with
> > duck typing. Objects that don't have the appropriate methods or
> attributes
> > will cause a TypeError or AttributeError. If you're passing in the wrong
> > kind of object, you'll find out almost as soon as you write the code
> > (because you test early and often), just as you would with a statically
> > analyzed type system.
>
> It's slightly delayed; with Python, you have to actually execute the
> code path in question, whereas a static type system would detect it at
> the compilation stage. So it may depend on the quality of your
> testing. MyPy may be able to help there.
>

Depends on how long it takes to compile!
https://xkcd.com/303/
:-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-09 Thread Michael Selik
On Tue, Aug 9, 2016 at 3:22 PM Juan Pablo Romero Méndez <
jpablo.rom...@gmail.com> wrote:

> I'm actually looking for ways to minimize run time errors, so that would
> include TypeError and AttributeError.
>
> In your "File-like" example having type information would prevent me from
> even passing the wrong object, so I don't need to test for that particular
> problem.
>

Let's clarify this discussion by using the word "check" for type-checks
that necessary for the application to behave as desired and using the word
"test" for checking application behavior under normal and edge cases, such
as unexpected types being passed.

Are you saying you want to minimize type-checks during the execution of
your application? Or do you want to minimize the amount of testing code you
write?

I don't check for file-likeness in the application code. If somehow a
non-file-like (file-unlike?) object makes its way into my function, I
expect (and hope) my program will immediately stop and print a nice
traceback for me. If I wanted some other behavior, I probably would already
have try/except surrounding the appropriate section to deal with not-found
or permissions errors. If I need to, I'll add TypeError and AttributeError
to that except statement.

I sometimes write tests for unexpected inputs, checking to ensure that
either a TypeError or AttributeError is raised. However, sometimes I'm not
worried about it. If the user gives me bizarre input, they should know to
expect (possibly) bizarre results. Who knows, maybe that's what they wanted.

I would add type-checking or value-checking to my application if the wrong
type or value does not raise an error but instead causes an infinite loop
or corrupts data. Those situations are fairly rare.



On Tue, Aug 9, 2016 at 4:52 PM Juan Pablo Romero Méndez <
jpablo.rom...@gmail.com> wrote:

> So as the writer of the function you expect the user to read the function
> body to determine what is safe to pass or not?


No. But I usually expect them to not freak out when they see a traceback.
Or if they do freak out, I want them to freak out and send a bug report.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-09 Thread Michael Selik
On Wed, Aug 10, 2016 at 1:22 AM Juan Pablo Romero Méndez <
jpablo.rom...@gmail.com> wrote:

> 1) catching exceptions at the point where you care, 2)
> preemptively check for certain runtime conditions to avoid exceptions 3)
> write as many tests as possible  4) learn to live with runtime errors.
>

I suggest focusing on #4, learn to appreciate/enjoy runtime errors. When
you've come around to that way of thinking, you'll have better judgement on
when to care about catching exceptions vs letting them stop your program.

As for writing as many tests as possible... eh. Think about your code
scientifically. You can disprove a falsifiable hypothesis, but you can't
prove a hypothesis. Sometimes you can formally prove the correctness of
your code via logical deduction, but that's not testing. Sometimes you can
exhaustively test all expected inputs, but it's impossible to exhaustively
test the infinity of possible inputs. So write tests as if you're
presenting evidence for a theory.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Michael Selik
On Tue, Aug 9, 2016 at 9:31 PM Steven D'Aprano 
wrote:

>
> http://steve-yegge.blogspot.com.au/2008/05/dynamic-languages-strike-back.html


Great link. I enjoyed the video, too.
https://www.youtube.com/watch?v=tz-Bb-D6teE

Buried deep in the QA section, there's a comment from the audience (I'll
paraphrase), that compiler type-checking is wonderful. Yegge replies that, "I
realized early in my career that I would actually rather have a runtime
error than a compile error."
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why can't I define a variable like "miles_driven" with an underscore in Python 3.4.3 ?

2016-08-10 Thread Michael Selik
On Wed, Aug 10, 2016 at 1:41 PM Cai Gengyang  wrote:

> I managed to get this piece of code to work :
>
> >>> print("This program calculates mpg.")
> This program calculates mpg.
> >>> milesdriven = input("Enter miles driven:")
> Enter miles driven: 50
> >>> milesdriven = float(milesdriven)
> >>> gallonsused = input("Enter gallons used:")
> Enter gallons used: 100
> >>> gallonsused = float(gallonsused)
> >>> gallonsused = float(gallonsused)
> >>> mpg = milesdriven / gallonsused
> >>> print("Miles per gallon:", mpg)
> Miles per gallon: 0.5
> >>> print("This program calculates mpg.")
> This program calculates mpg.
> >>> milesdriven = input("Enter miles driven:")
> Enter miles driven: 100
> >>> milesdriven = float(milesdriven)
> >>> gallonsused = input("Enter gallons used:")
> Enter gallons used: 500
> >>> gallonsused = float(gallonsused)
> >>> mpg = milesdriven / gallonsused
> >>> print("Miles per gallon:", mpg)
> Miles per gallon: 0.2
>
> But, why can't i define a variable like "miles_driven" with an underscore
> in my Python Shell ? When I try to define it , the underscore doesn't
> appear at all and instead I get this result :
> "miles driven" , with no underscore appearing even though I have already
> typed "_" between "miles" and "driven" ?
>

You'll have to give more information about the system you're using.
Underscores are allowed in Python variable names.

The first thought I have is that something outside of Python is changing
your underscore to a space. Perhaps your OS spelling autocorrect is overly
aggressive?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Michael Selik
On Wed, Aug 10, 2016, 4:34 PM Juan Pablo Romero Méndez <
jpablo.rom...@gmail.com> wrote:

>
> I've been trying to find (without success so far) an example of a situation
> where the dynamic features of a language like Python provides a clear
> advantage over languages with more than one type.
>

Only one type? There are a great variety of types for Python objects. Even
Python classes have types. Further, you can modify types at runtime! It's a
typing bonanza!

I suppose you could argue we're conflating types and classes. If so, it
sounds like you started this conversation with an ulterior motive and not
to learn about best practices for runtime type checking in Python.

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


Re: What's the best way to minimize the need of run time checks?

2016-08-10 Thread Michael Selik
On Wed, Aug 10, 2016, 6:44 PM Juan Pablo Romero Méndez <
jpablo.rom...@gmail.com> wrote:

> As to why I asked that, there are several reasons: I have a very concrete
> need right now to find pragmatic ways to increase code quality, reduce
> number of defects, etc. in a Python code base. But also I want to
> understand better the mind set and culture of Python's community.
>

That's a much better question. Arguing about type checking is somewhat of a
red herring. It's best not to fight the language, but to use its strengths.

I often find that trying to add type and value checks just makes my code
harder to read and harder to maintain, even if they solve a particular bug
in the moment. A better solution is to refactor in such a way that those
checks are unnecessary.

Are you familiar with context managers and the iterator protocol? If not, I
suggest you start by looking for setup/cleanup code or try/except/finally
blocks that you can refactor into a context manager and by looking for
awkward loops with explicit external state that you can refactor into an
iterator.

If you really must add type- and value-checks, I suggest trying to place
them in a property so that they don't clutter the more interesting code.

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


Re: Advice on optimizing a Python data driven rules engine

2016-08-11 Thread Michael Selik
On Thu, Aug 11, 2016 at 10:57 AM Malcolm Greene  wrote:

> Background: I'm building a  rules engine for transforming rows of data
> being returned by csv DictReader, eg. each row of data is a dict of column
> name to value mappings. My rules are a list of rule objects whose
> attributes get referenced by rule specific methods. Each rule has an
> associated method which gets called based on rule name.
>

Could you share the API you've designed for this tool? An example of usage
will clarify what you intend.

One technique for applying a transformation function to each record
returned by a csv DictReader:

import csv
import math
from io import StringIO

f = StringIO('''\
a,b,c
1,2,3
4,5,6
''')

def pass_(value):
return value

transforms = {
'a': lambda v: math.sin(int(v)),
'b': lambda v: math.cos(int(v)),
}

for row in csv.DictReader(f):
print({k: transforms.get(k, pass_)(v) for k, v in row.items()})



Using __missing__ for your transform dict might be more elegant than .get
with a pass_ as default.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Asynchronous programming

2016-08-11 Thread Michael Selik
On Thu, Aug 11, 2016 at 11:01 AM Steven D'Aprano 
wrote:

> That ... looks wrong. You're taking something which looks like a procedure
> in the first case (trn.execute), so it probably returns None, and yielding
> over it. Even it that's not wrong, and it actually returned something which
> you ignored in the first case
>

It's a standard, perhaps a mistaken standard, but nonetheless database
cursors tend to have that feature: execute returns the mutated self. I
agree that execute looks like it should return None instead. The return
self pattern feels Rubyish to me (or Rubic?).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Asynchronous programming

2016-08-11 Thread Michael Selik
On Thu, Aug 11, 2016 at 11:46 AM Michael Selik 
wrote:

> On Thu, Aug 11, 2016 at 11:01 AM Steven D'Aprano <
> steve+pyt...@pearwood.info> wrote:
>
>> That ... looks wrong. You're taking something which looks like a procedure
>> in the first case (trn.execute), so it probably returns None, and yielding
>> over it. Even it that's not wrong, and it actually returned something
>> which
>> you ignored in the first case
>>
>
> It's a standard, perhaps a mistaken standard, but nonetheless database
> cursors tend to have that feature: execute returns the mutated self. I
> agree that execute looks like it should return None instead. The return
> self pattern feels Rubyish to me (or Rubic?).
>

Contradicting myself:
yield from c.execute(query).fetchall()# looks good
yield from c.execute(query)   # looks bad
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Print function not working

2016-08-11 Thread Michael Selik
On Thu, Aug 11, 2016 at 1:38 PM MRAB  wrote:

> On 2016-08-11 18:18, Chris Angelico wrote:
> > On Fri, Aug 12, 2016 at 3:03 AM, Atri Mahapatra
> >  wrote:
> >> I have installed IDLE 3.5.1 and wrote the following  to check if print
> is working. When it runs, I do not see anything is printed:
> >>
> >> class Base: #{
> >> def __init__( self ): #{
> >> print("Hello, world: \n\n");
> >>
> >> #}
> >>
> >> #}
> >>
> >>
> >> if ( __name__ == " __main__"): #{
> >> root = Base();
> >> #}
> >>
> >> Can anyone please point out the reason?
> >>
> >> Thanks,
> >> Atri
> >
> > Is name equal to main? That is, are you running this code as a top-level
> script?
> >
> It's not checking whether the name is "__main__", but whether it's "
> __main__" (note the space after the first quote).
>
> > Also: You do not need to write Python code as if it were C or Java.
> > All that extra punctuation is just putting unnecessary stress on the
> > world's punctuation mines, which are already overworked. :)
> >
> +1
>

When debugging something like this, one technique is to remove bits of code
to reduce the complexity. Remove something, run the code, check the
results, then repeat if it's still failing.

If you're not sure how class initializers work, maybe convert to a regular
function and try again.

def foo():
print('hello')
if __name__ == '__main__':
foo()

If that still doesn't work, maybe get rid of the function.

if __name__ == '__main__':
print('hello')

And if that still doesn't work, maybe get rid of the if-statement.

print('hello')

I think you'll find that the single line program "print('hello')" works
just fine.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Michael Selik
On Fri, Aug 12, 2016, 7:11 AM Steven D'Aprano 
wrote:

>
> [1] Are there programming language aware spell checkers? If not, there
> should be.
>

A good autocomplete is much like a spell-checker. I have far fewer spelling
errors when using an editor with autocomplete.

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


Re: Creating dictionary of items from Excel with mutliple keys

2016-08-13 Thread Michael Selik
On Sat, Aug 13, 2016 at 12:46 PM Atri Mahapatra 
wrote:

> I am trying to create a following dictionary. I am reading data from excel
>

Rather than using xlrd or other tools to read from excel, can you save the
file as CSV (comma-separated values)? I think you'll find Python's csv
module is very pleasant to use.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding the first index in a list greater than a particular value

2016-08-14 Thread Michael Selik
On Sun, Aug 14, 2016 at 2:21 PM Atri Mahapatra 
wrote:

> I have a list of dictionaries which look like this:
> [{'Width': 100, 'Length': 20.0, 'Object': 'Object1'}, {'Width': 12.0,
> 'Length': 40.0, 'Object': 'Object2'}.. so on till 10]
>
> I would like to find the first index in the list of dictionaries whose
> length is greater than a particular value
>
> f=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii]['Length'] >
> m][0] and it throws an error
>
> can  anyone suggest a way  to do it?
>

There's no need to play code golf (squishing your thoughts into one line).

def index_of_first_record_of_minimum_size(records, m):
'records is an iterable of dictionaries'

for i, dictionary in enumerate(list_of_dicts):
if dictionary['Length'] > m:
return i
raise ValueError('no record found with "Length" greater than %r' %
(m,))


If you have trouble coming up with a good name for a function, that
suggests you are either doing too much or too little with that function. If
you want the same task outside of a function, change the return to a break.

if not records:
raise ValueError('no records found')
for index, d in enumerate(records):
if d['Length'] > m:
break # `index` is now the index of the first record of minimum
length
else:
raise ValueError('no record found with "Length" greater than %r' %
(m,))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding the first index in a list greater than a particular value

2016-08-15 Thread Michael Selik
On Mon, Aug 15, 2016 at 2:01 AM Jussi Piitulainen <
jussi.piitulai...@helsinki.fi> wrote:

> There is a tradition of returning -1 when no valid index is found.
>

Sometimes it's better to break with tradition. Raise a ValueError. No
silent errors, and all that Zen.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-17 Thread Michael Selik
On Sun, Aug 14, 2016 at 11:01 AM  wrote:

> On Sunday, August 14, 2016 at 7:09:47 AM UTC+1, Paul Rubin wrote:
> > Steven D'Aprano writes:
> > > If the Python community rallies around this "record" functionality and
> > > takes to it like they took too namedtuple
> >
> > I like namedtuple and I think that it's a feature that they're modified
> > by making a new copy.  I know that has overhead but it's palpably
> > bug-avoidant.  I've used them extensively in some programs and they took
> > a considerable burden off my mind compared to using something like
> > structs or records.
>
> You might find this https://glyph.twistedmatrix.com/2016/08/attrs.html an
> interesting read.
>

I disagree with a few points from that blog post.

1. I don't mind typing so much. I like to be explicit. The attrs library
uses some overly-concise abbreviations. For example, what's the meaning of
``@attrs.s`` or ``attrs.ib``?
2. When inheriting from a namedtuple, I use the same class name for the
base and the child, so my reprs look good.
3. I don't bother to fieldnames.split() when passing fieldnames as a
space-separated string, because the split is unnecessary.
4. I *like* that namedtuple is backwards-compatible with tuples, so that
refactoring from tuples to namedtuples is easy.
5. Inheritance is useful. Sure, there are times it fails, but that's true
for any technique.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two-Dimensional Expression Layout

2016-08-20 Thread Michael Selik
On Fri, Aug 19, 2016 at 5:01 AM Lawrence D’Oliveiro 
wrote:

> It is handy to be able to keep complex expressions together sometimes,
> when breaking them up would simply obscure their structure. To avoid lines
> getting long, why not take advantage of the two available screen/page
> dimensions to make their structure clearer? As a bonus, spacing out
> parentheses makes them look less of a clutter.
>
>

> Examples from <
> https://github.com/ldo/qahirah_examples/blob/master/operators>:
>
> A function call with complex arguments (specifying arguments by keywords
> is highly recommended here):
>
> rect_1_pattern = \
> qah.Pattern.create_linear \
>   (
> p0 = (0, 0),
> p1 = (major_dim, 0),
> colour_stops =
> (
> (0, rect_1_colour),
> (1, complement(rect_1_colour)),
> )
>   )
>

I'd rather have intermediate variables and avoid the non-semantic
indentation.

p0 = (0, 0)
p1 = (major_dim, 0)
colour_stops = (0, rect_1_colour), (1, complement(rect_1_colour))
rect_1_pattern = qah.Pattern.create_linear(p0, p1, colour_stops)



Computing a variable value (using redundant parentheses to avoid
> backslash-continuations):
>
> dest_rect = \
> (
> draw_bounds
> +
> Vector(col, row) * draw_bounds.dimensions
> +
> Vector(0, top_extra)
> )
>

I'd rather have one long line for this case, even if it's "too long" by PEP
8 standards. If it's too long to think about, I'd break the task into
parts, each with a well-considered variable name.

substep = a + b
final = substep + c



From , a
> complex condition (with redundant parentheses again):
>
> if (
> not isinstance(src, Image)
> or
> mask != None and not isinstance(mask, Image)
> or
> not isinstance(dest, Image)
> ) :
> raise TypeError("image args must be Image objects")
> #end if
>

No need for the separate calls to isinstance, nor the check for None.

if any(not isinstance(obj, Image) for obj in [src, mask, dest]):
...


If you prefer maps to comprehensions:

is_image = lambda obj: isinstance(obj, Image)
if any(map(is_image, [src, mask, dest])):
...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two-Dimensional Expression Layout

2016-08-20 Thread Michael Selik
On Sat, Aug 20, 2016 at 6:21 PM Lawrence D’Oliveiro 
wrote:

> > p0 = (0, 0)
> > p1 = (major_dim, 0)
> > colour_stops = (0, rect_1_colour), (1, complement(rect_1_colour))
> > rect_1_pattern = qah.Pattern.create_linear(p0, p1, colour_stops)
>
> That’s an example of what I mean about obscure structure.
>

To each his own. I was assuming all those variable names meant something to
you. If not, then I expect it'd read well with good names. In this example,
I think the abbreviations and numbers in the names could be changed to
something more meaningful.


> >> From , a
> >> complex condition (with redundant parentheses again):
> >>
> >> if (
> >> not isinstance(src, Image)
> >> or
> >> mask != None and not isinstance(mask, Image)
> >> or
> >> not isinstance(dest, Image)
> >> ) :
> >> raise TypeError("image args must be Image objects")
> >> #end if
> >>
> >
> > No need for the separate calls to isinstance, nor the check for None.
> >
> > if any(not isinstance(obj, Image) for obj in [src, mask, dest]):
> > ...
>
> Spot the bug in your version...
>

It'd be easier if I ran the code :-)
Let's see...
- the ``or`` translates to an ``any(...)``
- ``not isinstance(obj, Image)`` is repeated 3 times
- ``[src, mask, dest]`` corresponds to the 3 objects
- ``mask != None`` is unnecessary, unless somehow None has been registered
as an instance of Image.

... can't spot it. Give me a hint?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two-Dimensional Expression Layout

2016-08-20 Thread Michael Selik
On Sat, Aug 20, 2016 at 8:43 PM Michael Selik 
wrote:

> On Sat, Aug 20, 2016 at 6:21 PM Lawrence D’Oliveiro <
> lawrenced...@gmail.com> wrote:
>
>> > p0 = (0, 0)
>> > p1 = (major_dim, 0)
>> > colour_stops = (0, rect_1_colour), (1, complement(rect_1_colour))
>> > rect_1_pattern = qah.Pattern.create_linear(p0, p1, colour_stops)
>>
>> That’s an example of what I mean about obscure structure.
>>
>
> To each his own. I was assuming all those variable names meant something
> to you. If not, then I expect it'd read well with good names. In this
> example, I think the abbreviations and numbers in the names could be
> changed to something more meaningful.
>

I think that came out wrong. Let me try again...

origin = 0, 0
destination = width, 0
start_color = 0, selected_color
stop_color = 1, complement(selected_color)
pattern = qah.Pattern.from_line(origin, destination, (start_color,
stop_color))

Better?
I wasn't sure what the numbers 0 and 1 are doing for the colour_stops.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two-Dimensional Expression Layout

2016-08-20 Thread Michael Selik
On Sun, Aug 21, 2016 at 12:31 AM Lawrence D’Oliveiro 
wrote:

> On Sunday, August 21, 2016 at 12:44:21 PM UTC+12, Michael Selik wrote:
> >
> > On Sat, Aug 20, 2016 at 6:21 PM Lawrence D’Oliveiro wrote:
> >
> >>> if any(not isinstance(obj, Image) for obj in [src, mask, dest]):
> >>> ...
> >>
> >> Spot the bug in your version...
> >
> > - ``mask != None`` is unnecessary, unless somehow None has been
> registered
> > as an instance of Image.
>
> That’s the bug: it’s not unnecessary.
>
> Maybe a quick application of one of De Morgan’s theorems might help:
>
> if (
> isinstance(src, Image)
> and
> (mask == None or isinstance(mask, Image))
> and
> isinstance(dest, Image)
> ) :
> don’t raise TypeError("image args must be Image objects")
> #end if
>
> Is that better for you?
>

Indeed it is, not sure why. I think Steven's right in this case. Since the
condition isn't identical for all objects, and it's only three variables,
I'd rather break it apart.

if not isinstance(src, Image):
raise TypeError('src must be an Image')
if mask and not isinstance(mask, Image):
raise TypeError('mask must be an Image or None')
if not isinstance(dest, Image):
raise TypeError('dest must be an Image')

As he said as well, this has the added benefit of encouraging more explicit
error messages.

In programming and in prose, sometimes repetition feels ugly and redundant,
but sometimes repetition feels like a beautiful harmony.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two-Dimensional Expression Layout

2016-08-21 Thread Michael Selik
On Sun, Aug 21, 2016, 3:06 AM Lawrence D’Oliveiro 
wrote:

> On Sunday, August 21, 2016 at 6:49:19 PM UTC+12, Michael Selik wrote:
>
> > Indeed it is, not sure why.
>
> Moral: It helps to understand the code you’re criticizing, before you
> start criticizing, not after.
>

A true statement, but not my takeaway from this discussion.

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


Re: repeat items in a list

2016-03-30 Thread Michael Selik
I prefer itertools.chain.from_iterable to the sum trick.

>>> from itertools import chain
>>> lst = list('abc')
>>> list(chain.from_iterable([s]*3 for s in lst))
['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']



On Tue, Mar 29, 2016 at 5:28 PM Vito De Tullio 
wrote:

> Random832 wrote:
>
> > How do you turn ['a', 'c', 'b'] into ['a', 'a', 'a', 'c', 'c', 'c', 'b',
> > 'b', 'b']?
>
> >>> sum([[e]*3 for e in ['a', 'c', 'b']], [])
> ['a', 'a', 'a', 'c', 'c', 'c', 'b', 'b', 'b']
>
>
> --
> By ZeD
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Suggestion: make sequence and map interfaces more similar

2016-04-01 Thread Michael Selik

> On Mar 31, 2016, at 10:02 AM, Marko Rauhamaa  wrote:
> 
> However, weirdly, dicts have get but lists don't.

Read PEP 463 for discussion on this topic.
https://www.python.org/dev/peps/pep-0463/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The next major Python version will be Python 8

2016-04-01 Thread Michael Selik
It suddenly occurred to me that if Microsoft announced it's
Ubuntu-in-Windows feature today, no one would believe it.

On Thu, Mar 31, 2016 at 11:55 PM Steven D'Aprano 
wrote:

> On Fri, 1 Apr 2016 11:13 am, Chris Angelico wrote:
>
> > Now's the time to get in with the ideas. My proposal is that Python 8,
> > in keeping with its new opinionated style, will require everyone to
> > follow a single timezone: Europe/Amsterdam.
>
> Swatch Internet time:
>
> https://en.wikipedia.org/wiki/Swatch_Internet_Time
>
>
> --
> Steven
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Set type for datetime intervals

2016-04-01 Thread Michael Selik
On Fri, Apr 1, 2016 at 1:32 AM Nagy László Zsolt 
wrote:

> Does anyone know a library that already implements these functions?
>

What do you not like about the ones on PyPI?
https://pypi.python.org/pypi?%3Aaction=search&term=interval&submit=search

Depending on the resolution you want, you might be able to use builtin sets
for everything.

def interval(start, stop, precision=60):
a = round(start.timestamp(), precision)
b = round(stop.timestamp(), precision)
return set(range(a, b, precision))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Set type for datetime intervals

2016-04-01 Thread Michael Selik
Whoops, I mixed up tasks. Here's what I meant:

def interval(start, stop, precision=60):
a, b = start.timestamp(), stop.timestamp()
return set(range(a, b, precision))

On Fri, Apr 1, 2016 at 4:31 PM Michael Selik 
wrote:

> On Fri, Apr 1, 2016 at 1:32 AM Nagy László Zsolt 
> wrote:
>
>> Does anyone know a library that already implements these functions?
>>
>
> What do you not like about the ones on PyPI?
> https://pypi.python.org/pypi?%3Aaction=search&term=interval&submit=search
>
> Depending on the resolution you want, you might be able to use builtin
> sets for everything.
>
> def interval(start, stop, precision=60):
> a = round(start.timestamp(), precision)
> b = round(stop.timestamp(), precision)
> return set(range(a, b, precision))
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Drowning in a teacup?

2016-04-01 Thread Michael Selik
Give this a shot

def snap(keyword, words):
matches = [i for i, s in enumerate(words) if s.startswith(keyword)]
for i in matches:
lst.insert(0, lst.pop(i))

Your current implementation is reassigning the local variable ``mylist`` to
a new list inside the function.

On Fri, Apr 1, 2016 at 4:30 PM Fillmore  wrote:

>
> notorious pass by reference vs pass by value biting me in the backside
> here. Proceeding in order.
>
> I need to scan a list of strings. If one of the elements matches the
> beginning of a search keyword, that element needs to snap to the front
> of the list.
> I achieved that this way:
>
>
> for i in range(len(mylist)):
>  if(mylist[i].startswith(key)):
>  mylist = [mylist[i]] + mylist[:i] + mylist[i+1:]
>
> Since I need this code in multiple places, I placed it inside a function
>
> def bringOrderStringToFront(mylist, key):
>
>  for i in range(len(mylist)):
>  if(mylist[i].startswith(key)):
>  mylist = [mylist[i]] + mylist[:i] + mylist[i+1:]
>
> and called it this way:
>
>   if orderstring:
>   bringOrderStringToFront(Tokens, orderstring)
>
> right?
> Nope, wrong! contrary to what I thought I had understood about how
> parameters are passed in Python, the function is acting on a copy(!) and
> my original list is unchanged.
>
> I fixed it this way:
>
> def bringOrderStringToFront(mylist, key):
>
>  for i in range(len(mylist)):
>  if(mylist[i].startswith(key)):
>  mylist = [mylist[i]] + mylist[:i] + mylist[i+1:]
>  return(mylist)
>
> and:
>
> if orderstring:
> Tokens = bringOrderStringToFront(Tokens, orderstring)
>
> but I'm left with a sour taste of not understanding what I was doing
> wrong. Can anyone elaborate? what's the pythonista way to do it right?
>
> Thanks
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [beginner] What's wrong?

2016-04-01 Thread Michael Selik
Humans have always had trouble with this, in many contexts. I remember
being annoyed at folks saying the year 2000 was the first year of the new
millennium, rather than 2001. They'd forgotten the Gregorian calendar
starts from AD 1.

On Fri, Apr 1, 2016, 6:58 PM Mark Lawrence via Python-list <
python-list@python.org> wrote:

> On 01/04/2016 23:44, sohcahto...@gmail.com wrote:
> > On Friday, April 1, 2016 at 3:10:51 PM UTC-7, Michael Okuntsov wrote:
> >> Nevermind. for j in range(1,8) should be for j in range(8).
> >
> > I can't tell you how many times I've gotten bit in the ass with that
> off-by-one mistake whenever I use a range that doesn't start at zero.
> >
> > I know that if I want to loop 10 times and I either want to start at
> zero or just don't care about the actual number, I use `for i in
> range(10)`.  But if I want to loop from 10 to 20, my first instinct is to
> write `for i in range(10, 20)`, and then I'm left figuring out why my loop
> isn't executing the last step.
> >
>
> "First instinct"?  "I expected"?  The Python docs might not be perfect,
> but they were certainly adequate enough to get me going 15 years ago,
> and since then they've improved.  So where is the problem, other than
> failure to RTFM?
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [beginner] What's wrong?

2016-04-01 Thread Michael Selik
On Sat, Apr 2, 2016, 12:28 AM Random832  wrote:

> On Fri, Apr 1, 2016, at 19:29, Michael Selik wrote:
> > Humans have always had trouble with this, in many contexts. I remember
> > being annoyed at folks saying the year 2000 was the first year of the new
> > millennium, rather than 2001. They'd forgotten the Gregorian calendar
> > starts from AD 1.
>
> Naturally, this means the first millennium was only 999 years long, and
> all subsequent millennia were 1000 years long. (Whereas "millennium" is
> defined as the set of all years of a given era for a given integer k
> where y // 1000 == k. How else would you define it?)
>
> And if you want to get technical, the gregorian calendar starts from
> some year no earlier than 1582, depending on the country. The year
> numbering system has little to do with the calendar type - your
> assertion in fact regards the BC/AD year numbering system, which was
> invented by Bede.
>
> The astronomical year-numbering system, which does contain a year zero
> (and uses negative numbers rather than a reverse-numbered "BC" era), and
> is incidentally used by ISO 8601, was invented by Jacques Cassini in the
> 17th century.
>
>
>
> Rule #1 of being pedantic: There's always someone more pedantic than
> you, whose pedantry supports the opposite conclusion.
>

I'll have to remember that one. And thanks for the facts.

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


Re: Drowning in a teacup?

2016-04-01 Thread Michael Selik
On Sat, Apr 2, 2016, 1:46 AM Vito De Tullio  wrote:

> Fillmore wrote:
>
> > I need to scan a list of strings. If one of the elements matches the
> > beginning of a search keyword, that element needs to snap to the front
> > of the list.
>
> I know this post regards the function passing, but, on you specific
> problem,
> can't you just ... sort the list with a custom key?
>
> something like (new list)
>
> >>> sorted(['no_a', 'yes_c', 'no_b', 'yes_z', 'no_y', 'yes_x'],
> ... key=lambda e: not e.startswith('yes'))
> ['yes_c', 'yes_z', 'yes_x', 'no_a', 'no_b', 'no_y']
>
> or (in place)
>
> >>> l = ['no_a', 'yes_c', 'no_b', 'yes_z', 'no_y', 'yes_x']
> >>> l.sort(key=lambda e: not e.startswith('yes'))
> >>> l
> ['yes_c', 'yes_z', 'yes_x', 'no_a', 'no_b', 'no_y']
>

If the number of matches is small relative to the size of the list, I'd
expect the sort would be slower than most of the other suggestions.

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


Re: Drowning in a teacup?

2016-04-02 Thread Michael Selik
On Sat, Apr 2, 2016 at 6:32 AM Peter Otten <__pete...@web.de> wrote:

> Vito De Tullio wrote:
>
> > Michael Selik wrote:
> >
> >>> > I need to scan a list of strings. If one of the elements matches the
> >>> > beginning of a search keyword, that element needs to snap to the
> front
> >>> > of the list.
> >>>
> >>> I know this post regards the function passing, but, on you specific
> >>> problem,
> >>> can't you just ... sort the list with a custom key?
> >>
> >> If the number of matches is small relative to the size of the list, I'd
> >> expect the sort would be slower than most of the other suggestions.
> >
> > umm... I take the liberty to set up a little test
> >
> > $ cat test_sort_prefix.py
> > #!/usr/bin/env python3
> >
> > from sys import argv
> > from timeit import timeit
> >
> >
> > def sort_in_place(l):
> > def key(e):
> > return not e.startswith('yes')
> > l.sort(key=key)
> >
> >
> > def original_solution(mylist):
> > for i in range(len(mylist)):
> > if(mylist[i].startswith('yes')):
> > mylist[:] = [mylist[i]] + mylist[:i] + mylist[i+1:]
>
> original_solution() reverses the order of the matches while sort_in_place()
> doesn't. If the order is not relevant I'd try something like
>
> def exchange(items):
> pos = 0
> for index, item in enumerate(items):
> if item.startswith("yes"):
> items[pos], items[index] = item, items[pos]
> pos += 1
>
> > def main():
> > if argv[1] == 'sort_in_place':
> > f = sort_in_place
> > elif argv[1] == 'original_solution':
> > f = original_solution
> > else:
> > raise Exception()
> >
> > nomatches = int(argv[2])
> > matches = int(argv[3])
> >
> > l = ["no_" for _ in range(nomatches)] + ["yes_" for _ in
> > range(matches)]
>
> Python's timsort knows how to deal with (partially) sorted lists. I suggest
> that you add
>
> random.seed(42) # same list for all script invocations
> random.shuffle(l)
>
> here to spread the matches somewhat over the list.
>
> > print(timeit("f(l)", number=100, globals={"f": f, "l": l}))
>
> Remember that f(l) modifies l, so all but the first invocation will see a
> list where all matches are at the beginning. In other words: in 99 out of
> 100 runs the list is already sorted.
>
> While adding the overhead of copying the list I would expect the results of
>
> timeit("f(l[:])", ...)
>
> to be more realistic.
>
> > if __name__ == '__main__':
> > main()
> > $ ./test_sort_prefix.py sort_in_place 100 3
> > 33.260575089996564
> > $ ./test_sort_prefix.py original_solution 100 3
> > 35.93009542999789
> > $
> >
> >
> > in my tests there is no sensible difference between the two solutions...
> > and the number of matches is risible :)
>
> Indeed, as the number of matches rises your sort() approach will likely
> fare
> better.
>
> Your conclusions may be correct, but the benchmark to support them is
> flawed.
>
>
Option 1: scan and match, pop, insert
Scan and match is O(n)
pop is O(n) ... gotta shift everything over one after deleting
insert is O(n) on a list, better use deque.appendleft for O(1)

Option 2: map keyfunc, sort
Mapping the keyfunction is O(n)
(tim)Sorting is O(n log n) worst case, O(n) best case.

Option 1 using a deque will be O(n).
Option 2 will be O(n log n) worst case and O(n) best case.

On small datasets the constant factors matter, but then it's small, so who
cares about speed? (partially joking)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [beginner] What's wrong?

2016-04-02 Thread Michael Selik
On Sat, Apr 2, 2016 at 4:16 AM Chris Angelico  wrote:

> On Sat, Apr 2, 2016 at 3:27 PM, Random832  wrote:
> > On Fri, Apr 1, 2016, at 19:29, Michael Selik wrote:
> >> Humans have always had trouble with this, in many contexts. I remember
> >> being annoyed at folks saying the year 2000 was the first year of the
> new
> >> millennium, rather than 2001. They'd forgotten the Gregorian calendar
> >> starts from AD 1.
> >
> > Naturally, this means the first millennium was only 999 years long, and
> > all subsequent millennia were 1000 years long. (Whereas "millennium" is
> > defined as the set of all years of a given era for a given integer k
> > where y // 1000 == k. How else would you define it?)
> >
> > And if you want to get technical, the gregorian calendar starts from
> > some year no earlier than 1582, depending on the country. The year
> > numbering system has little to do with the calendar type - your
> > assertion in fact regards the BC/AD year numbering system, which was
> > invented by Bede.
> >
> > The astronomical year-numbering system, which does contain a year zero
> > (and uses negative numbers rather than a reverse-numbered "BC" era), and
> > is incidentally used by ISO 8601, was invented by Jacques Cassini in the
> > 17th century.
> >
>
> Are you sure? Because I'm pretty sure these folks were already talking
> about BC.
>
> http://xenohistorian.faithweb.com/holybook/quotes/YK.html
>
>
If they'd only used Unicode, they could have said "þou" in prayer and "ðousand"
for the year.

BTW, I finally know why there are all those "Ye Olde ...".
https://en.wikipedia.org/wiki/Thorn_(letter)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [beginner] What's wrong?

2016-04-02 Thread Michael Selik
On Sat, Apr 2, 2016, 3:40 PM Marko Rauhamaa  wrote:

> Terry Reedy :
>
> > On 4/2/2016 12:44 PM, Marko Rauhamaa wrote:
> >
> >> Nowadays software companies and communities are international.
> >
> > Grade school classrooms, especially pre-high school, are not.
>
> Parenthetically, English teachers in Finland have been happy with how
> teenage boys' English grades have gone up with the advent of online
> gaming.
>
>High school boys get more top scores in English than girls. According
>to a recent master's thesis, the most important causal factor for the
>boys' success is spending a lot of time playing computer games. An
>English language professor wants to raise awareness about the role of
>games for language skills.
>

Gaming also helps your reaction time. Normally 0.3 ms, but 0.1 ms for top
gamers. And fighter pilots.

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


Re: [Beginner] - Hanging in the code, can't figure out what's wrong

2016-04-02 Thread Michael Selik
I might be overlooking something, but raw_input (Python 2) and input
(Python 3) won't return the input from sys.stdin until you type ENTER. Or
did I misunderstand the question?

On Sat, Apr 2, 2016 at 6:30 PM BartC  wrote:

> On 02/04/2016 23:16, Ned Batchelder wrote:
> > On Saturday, April 2, 2016 at 6:09:13 PM UTC-4, BartC wrote:
> >> On 02/04/2016 22:59, Loop.IO wrote:
> >>> Hey
> >>>
> >>> So I built a keylogger using python as a test, got the code from the
> tutorial online, I want to improve on it to make it more automated, but the
> issue I'm having is it won't create the file until I press return, any
> clues where I'm going wrong?
> >>>
> >>> If I press return it makes the batch file, otherwise it just hangs.
> >>
> >>>   name=raw_input ('C:\\Documents\\PythonCoding\\')+'launch2.bat'
> >>
> >> You're asking for a file name to be entered. So that if ABC is pressed,
> >> followed by Enter, it will use:
> >>
> >> C:\Documents\PythonCoding\ABClaunch2.bat
> >
> > That line of code will prompt the user:
> >
> >  C:\Documents\PythonCoding\
> >
> > then the user enters ABC:
> >
> >  C:\Documents\PythonCoding\ABC
> >
> > and raw_input returns "ABC", so name becomes "ABClaunch2.bat".
>
> Yes, of course. I ran the code and picked up the wrong line even though
> I printed out 'name'!
>
> But, it does look as though that path is supposed to form part of the
> resulting filename.
>
> --
> Bartc
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] - Hanging in the code, can't figure out what's wrong

2016-04-03 Thread Michael Selik
How do you know when you're done typing the name of the file?

It's hard to get tone right on the internet, so I'll clarify: this is not a
rhetorical question and I mean you, LoopIO, not a generic person.

On Sun, Apr 3, 2016, 8:40 PM Loop.IO  wrote:

> On Sunday, April 3, 2016 at 8:32:06 PM UTC+1, Loop.IO wrote:
> > On Sunday, April 3, 2016 at 4:11:49 PM UTC+1, BartC wrote:
> > > On 03/04/2016 15:41, Loop.IO wrote:
> > > > On Sunday, April 3, 2016 at 1:12:23 AM UTC+1, BartC wrote:
> > > >> On 02/04/2016 23:31, Loop.IO wrote:
> > > >>
> > > >>> Oh i see, so the code prompts for a name.. so i'm more lost than i
> thought, what do I need to change to make it just create the file with the
> chosen name Launch2.bat without the prompt?
> > > >>
> > > >> If you don't want the user to enter anything, then I explained how
> > > >> before, just use:
> > > >>
> > > >>name='C:\\Documents\\PythonCoding\\launch2.bat'
> > > >>
> > > >> if that's the file name you need.
> > > >>
> > > >> --
> > > >> Bartc
> > > >
> > > > Hi Bartc, i tried that, didn't work
> > >
> > > You mean it gave an error when you tried to create that file?
> > >
> > > Does that path already exist on your machine? If not then trying to
> > > create a file in a non-existent path won't work.
> > >
> > > You can create the path manually outside of Python. Or look up the docs
> > > to find out how to do that. A quick google suggested using os.makedirs
> > > (to create multiple nested paths at the same time).
> > >
> > > The following code worked on my machine:
> > >
> > > import sys
> > > import os
> > >
> > > def create():
> > > print("creating new file")
> > >
> > > path="c:/Documents/PythonCoding/"
> > > name=path+"launch2.bat"
> > >
> > > try:
> > > os.stat(path)
> > > except:
> > > os.makedirs(path)
> > >
> > > print (name)
> > >
> > > try:
> > > file=open(name,'w')
> > > file.close()
> > > except:
> > > print("error occured")
> > > sys.exit(0)
> > >
> > > create()
> > >
> > > --
> > > Bartc
> >
> > The issue is that it hangs, there is no error. its like it pauses until
> i press enter, ill try what you've posted one moment
>
> Ok the Bartc code gives me an error.
>
> What is it that makes the code hang with what I have, you said it was that
> it's prompting for a name for the file, so how do I bypass that and force
> it to create the file with the name I've provided?
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Plot/Graph

2016-04-03 Thread Michael Selik
Indeed there is. Every example in the gallery shows the code to produce it.

http://matplotlib.org/gallery.html

On Sun, Apr 3, 2016, 8:05 PM Muhammad Ali 
wrote:

>
> Hi,
>
> Could anybody tell me that how can I plot graphs by matplotlib and get
> expertise in a short time? I have to plot 2D plots just like origin
> software.
>
> Secondly, how could we draw some horizontal reference line at zero when
> the vertical scale is from -3 to 3?
>
> Looking for your posts, please.
>
> Thank you.
>
> p.s: Is there any short and to the point text book/manual/pdf to learn 2D
> plotting with matplotlib?
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best Practices for Internal Package Structure

2016-04-04 Thread Michael Selik
On Mon, Apr 4, 2016 at 6:04 PM Sven R. Kunze  wrote:

> Hi Josh,
>
> good question.
>
> On 04.04.2016 18:47, Josh B. wrote:
> > My package, available at https://github.com/jab/bidict, is currently
> laid out like this:
> >
> > bidict/
> > ├── __init__.py
> > ├── _bidict.py
> > ├── _common.py
> > ├── _frozen.py
> > ├── _loose.py
> > ├── _named.py
> > ├── _ordered.py
> > ├── compat.py
> > ├── util.py
> >
> >
> > I'd like to get some more feedback on a question about this layout that
> I originally asked here: <
> https://github.com/jab/bidict/pull/33#issuecomment-193877248>:
> >
> > What do you think of the code layout, specifically the use of the _foo
> modules? It seems well-factored to me, but I haven't seen things laid out
> this way very often in other projects, and I'd like to do this as nicely as
> possible.
>

Using the _module.py convention for internals is fine, except that you have
few enough lines of code that you could have far fewer files. Why create a
package when you can just have a module, bidict.py?

I find it easier to find the right section of my code when I have just a
few files open rather than a dozen or so in different windows and tabs.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best Practices for Internal Package Structure

2016-04-05 Thread Michael Selik


> On Apr 5, 2016, at 7:49 PM, Sven R. Kunze  wrote:
> 
>> On 05.04.2016 19:59, Chris Angelico wrote:
>> On Wed, Apr 6, 2016 at 3:38 AM, Sven R. Kunze  wrote:
 Your package is currently under 500 lines. As it stands now, you could
 easily flatten it to a single module:
 
 bidict.py
>>> 
>>> I don't recommend this.
>>> 
>>> The line is blurry but 500 is definitely too much. Those will simply not fit
>>> on a 1 or 2 generous single screens anymore (which basically is our
>>> guideline). The intention here is to always have a bit more of a full screen
>>> of code (no wasted pixels) while benefiting from switching to another file
>>> (also seeing a full page of other code).
>> Clearly this is a matter of opinion. I have absolutely no problem with
>> a 500-lne file. As soon as you force people to split things across
>> files, you add a new level of indirection that causes new problems.
> 
> Guidelines. No forcing.
> 
>> I'd rather keep logically-related code together rather than splitting
>> across arbitrary boundaries;
> 
> That's a good advice and from what I can see bidict adheres to that. ;)
> 
>> you can always search within a file for the bit you want.
> 
> If you work like in the 80's, maybe. Instead of scrolling, (un)setting 
> jumppoints, or use splitview of the same file, it's just faster/easier to 
> jump between separate files in todays IDEs if you need to jump between 4 
> places within 3000 lines of code.

When you made that suggestion earlier, I immediately guessed that you were 
using PyCharm. I agree that the decision to split into multiple files or keep 
everything in just a few files seems to be based on your development tools. I 
use IPython and SublimeText, so my personal setup is more suited to one or a 
few files.

>> When you split a file into two, you duplicate the
>> headers at the top (imports and stuff), so you'll split a 100-line
>> file into two 60-line files or so. Do that to several levels in a big
>> project and you end up with a lot more billable lines of code, but no
>> actual improvement.
> 
> Who cares about the imports? As I said somewhere else in my response, it's 
> hidden from sight if you use a modern IDE. We call that folding. ;)
> 
> Who bills lines of code? Interesting business model. ;)
> 
>> I guess that's worth doing - lovely billable hours
>> doing the refactoring,
> 
> Refactoring is not just splitting files if that concept is new to you.
> 
> Refactoring it not an end to itself. It serves a purpose.
> 
>> more billable hours later on when you have to
>> read past the migration in source control ("where did this line come
>> from" gets two answers all the time), and more billable hours dealing
>> with circular imports when two fragments start referring to each
>> other. Sounds like a plan.
> 
> It appears to me as if you like messy code then. ;)
> 
> Best,
> Sven
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ANN: intervalset Was: Set type for datetime intervals

2016-04-05 Thread Michael Selik
It seems coding a generic interval and intervalset will bring a variety of 
difficult design choices. If I were you, I'd forget making it generic and build 
one specifically for the application you have in mind. That way you can ignore 
most of these feature discussions.

> On Apr 5, 2016, at 2:59 PM, Random832  wrote:
> 
>> On Tue, Apr 5, 2016, at 04:11, Nagy László Zsolt wrote:
>> But there is still need to check begin <= end. But maybe
>> you are right: if we suppose that nobody will try to use an interval
>> where end < begin then we can use plain tuples. But then the user *must*
>> make sure not to use invalid intervals.
> 
> The IntervalSet constructor and add method could switch them or throw an
> exception. But I'm not saying to use plain tuples. I'm saying to make
> Interval a "dumb" object, which *only* supports being constructed and
> added to a set, rather than putting a bunch of set-like operations on
> it.
> 
>> It is blurred by design. There is an interpretation where an interval
>> between [0..4] equals to a set of intervals ([0..2],[2..4]).
> 
> No, because 2.5 is in one and not the other. Floats are orderable with
> ints, so it's not reasonable to say "this is an int intervalset so no
> floats are contained in it". But that's another discussion.
> 
> But why would you want to use [0..4] directly instead of ([0..4])?
> 
>> Actually,
>> you can ask: "is an element within the interval?" The same question can
>> be asked for an Interval set. So the same type of value can be
>> "contained" within an interval and also witin an interval set. In the
>> current implementation, if you try to create a set with these two
>> intervals [0..2] and [2..4], then they are unified into a single element
>> on purpose: to make sure that there is only a single official
>> representation of it. E.g. ([0..2],[2..4]) is not a valid set, only
>> ([0..4]) is.
> 
> Uh, exactly. I don't understand why any of this makes it useful to have
> these operations on interval objects.
> 
>> By definition, a set is given with its elements, e.g. for any possible
>> item, you can tell if it is part of the set or not. So if
>> ([0..2],[2..4]) and ([0..4]) have exactly the same elements (by the
>> given definition), then they are not just equal: they are the same set.
>> The same reasoning is used in math when they say: there cannot be
>> multiple empty sets. There exists a single empty set. It happens that we
>> can only represent a finite number of elements in any set on a computer.
>> I wanted to have a well defined single way to represent any given set.
> 
> What? *I* am the one who wants a well defined single way to represent a
> given set. *You* are the one who wants to make [0..4] a set-like object
> that is different from ([0..4]) and doesn't quite support the same
> operations.
> 
>> I can also see your point. From another point a view, a set and a set of
>> sets is not the same type.
> 
> I'm saying an interval is _not_ a set. It is merely part of the way of
> describing the set. Just like the word "of" isn't a set just because
> "the set of all even numbers" is a set.
> 
> You could consider "the set of [dates|numbers|etc] in a single interval"
> to be a type of set, certainly. But there's no reason to have such a set
> _not_ fully support set operations even when those operations will not
> return a set of a single interval.
> 
>> However, I do not share this view, because if
>> we start thinking this way, then operations like the one below does not
>> make sense:
>> 
>> [0..4]) - (1..2] == [0..1] | [2..4]
>> 
>> If you make a strinct distinction between intervals and interval sets,
>> then the above equation does not make sense, because you cannot remove
>> an element from a set that is not an "element" of it. The above equation
>> makes sense only if the "intervalset" is a general representation of
>> possible values, and the "interval" type is a special representation of
>> (the same) possible values.
> 
> I don't understand why you want to let the user use the "interval" type
> for purposes other than adding to an intervalset in the first place
> Nothing you've posted has explained this.
> 
>> It is clear when you ask the question: "is 3
>> in the interval?" and you can also ask "is 3 in the interval set?"
>> - so
>> the value of the same type can be contained in the interval and also in
>> the interval set. Maybe the naming is bad, and it should be named
>> "Intervals" instead of IntervalSet?
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Import graphics error

2016-04-05 Thread Michael Selik


> On Apr 5, 2016, at 5:17 PM, Nicolae Morkov  wrote:
> 
> I copied the code from Python from everyone page 67.
> Following the instructions The graphic modules by John Zelle I copied  into
> the python lacation ,to be easier to find the path .

Please be more specific. What is the python location? What exactly did you 
copy? How did you copy? What is the exact path where Python is installed, where 
these graphics modules are, and where the code is that you're working on?

> On Tue, Apr 5, 2016 at 11:19 AM, Nicolae Morkov 
> wrote:
> 
>> # This program creates a graphics window with a rectangle. It provides the
>> 3 # template used with all of the graphical programs used in the book.
>> 4 #
>> 5
>> 6 from graphics import GraphicsWindow
>> 7
>> 8 # Create the window and access the canvas.
>> 9  win = GraphicsWindow()
>> 10 canvas = win.canvas()
>> 11
>> 12 # Draw on the canvas.
>> 13 canvas.drawRect(5, 10, 20, 30)
>> 14
>> 15 # Wait for the user to close the window.
>> 16 win.wait()
>> 
>> 
>> *This is the error I get*
>> 
>> Traceback (most recent call last):
>>  File
>> "C:/Users/Nicolae/AppData/Local/Programs/Python/Python35/pythonK/pr.py",
>> line 1, in 
>>from graphics import GraphicsWindow
>> ImportError: No module named 'graphics'
>> 
>> 
>> *And when I installed graphic library the error was :*
>> 
>> Traceback (most recent call last):
>>  File
>> "C:/Users/Nicolae/AppData/Local/Programs/Python/Python35/pythonK/pr.py",
>> line 1, in 
>>from graphics import GraphicsWindow
>> ImportError: cannot import name 'GraphicsWindow'
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python script for .dat file

2016-04-05 Thread Michael Selik
What code have you written so far?

> On Apr 5, 2016, at 5:27 PM, Muhammad Ali  wrote:
> 
>> On Tuesday, April 5, 2016 at 9:07:54 AM UTC-7, Oscar Benjamin wrote:
>>> On 5 April 2016 at 16:44, Muhammad Ali  wrote:
 On Tuesday, April 5, 2016 at 8:30:27 AM UTC-7, Joel Goldstick wrote:
 On Tue, Apr 5, 2016 at 11:23 AM, Muhammad Ali
  wrote:
> 
> Could any body tell me a general python script to generate .dat file 
> after the extraction of data from more than 2 files, say file A and file 
> B?
> 
> Or could any body tell me the python commands to generate .dat file after 
> the extraction of data from two or more than two files?
> 
> I have to modify some python code.
 
 What exactly is a .dat file? and how is it different from any other
 file? Is it binary or text data?
>>> 
>>> It is text data.
>> 
>> You haven't provided enough information for someone to answer your
>> question. This is a text mailing list so if a .dat file is text then
>> you can paste here an example of what it would look like. What would
>> be in your input files and what would be in your output files? What
>> code have you already written?
>> 
>> If the file is large then don't paste its entire content here. Just
>> show an example of what the data would look like if it were a smaller
>> file (maybe just show the first few lines of the file).
>> 
>> Probably what you want to do is easily achieved with basic Python
>> commands so I would recommend to have a look at a tutorial. There are
>> some listed here:
>> https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
>> 
>> Also the tutor mailing list is probably more appropriate for this
>> level of question:
>> https://mail.python.org/mailman/listinfo/tutor
>> 
>> --
>> Oscar
> 
> Input and outout files are text files. e.g: 
> #KptCoord #E-E_Fermi #delta_N
>  0.  -22.   0.000E+00
>  0.0707  -22.   0.000E+00
>  0.1415  -22.   0.000E+00
>  0.2122  -22.   0.000E+00
>  0.2830  -22.   0.000E+00
>  0.3537  -22.   0.000E+00
>  0.4245  -22.   0.000E+00
>  0.4952  -22.   0.000E+00
>  0.5660  -22.   0.000E+00
>  0.6367  -22.   0.000E+00
>  0.7075  -22.   0.000E+00
>  0.7782  -22.   0.000E+00
>  0.8490  -22.   0.000E+00
>  0.9197  -22.   0.000E+00
>  0.9905  -22.   0.000E+00
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best Practices for Internal Package Structure

2016-04-06 Thread Michael Selik
On Wed, Apr 6, 2016, 2:51 AM Steven D'Aprano  wrote:

> On Wed, 6 Apr 2016 05:56 am, Michael Selik wrote:
>
> [Sven R. Kunze]
> >> If you work like in the 80's, maybe. Instead of scrolling, (un)setting
> >> jumppoints, or use splitview of the same file, it's just faster/easier
> to
> >> jump between separate files in todays IDEs if you need to jump between 4
> >> places within 3000 lines of code.
>
> [Michael]
> > When you made that suggestion earlier, I immediately guessed that you
> were
> > using PyCharm. I agree that the decision to split into multiple files or
> > keep everything in just a few files seems to be based on your development
> > tools. I use IPython and SublimeText, so my personal setup is more suited
> > to one or a few files.
>
> How does PyCharm make the use of many files easier?
>

I'll let Sven answer that one. I don't know, but I've noticed the
correlation of habit to IDE.

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


Re: ANN: intervalset Was: Set type for datetime intervals

2016-04-06 Thread Michael Selik
On Tue, Apr 5, 2016, 10:46 PM Nagy László Zsolt 
wrote:

>
> >> How about creating two classes for this? One that supports zero sized
> >> intervals, and another that doesn't?
> > If you don't want zero sized intervals, just don't put any in it. You
> > don't have a separate list type to support even integers vs one that
> > supports all floats. What operations are made different in
> > implementation by this restriction?
> When you substract two sets, zero sized intervals may appear in the
> result. It is not just a question of "putting or not putting them in".
> Are zero sized intervals valid or not? In my particular application,
> they are not. I think Michael was right: this cannot be generalized. It
> is application dependent.
>

To emphasize that in your project, I'd keep the interval code in the same
module you have the time calculations, rather than breaking it into a more
generic sounding module. Once you relax your goals, you might not even need
a class, just one or a few functions.

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


Re: Promoting Python

2016-04-06 Thread Michael Selik
On Wed, Apr 6, 2016, 12:51 PM Marko Rauhamaa  wrote:

> BartC :
> Really, there's only one high-level construct you can't live without:
> the "while" statement. Virtually every Python program has at least one
> "while" statement, and in general, it is unavoidable.
>
> Basic programs, on the other hand, don't need that meaningless buzzword,
> but can live just fine with GOTO.
>

You don't need WHILE or GOTO, you can just copy-paste code. You probably
need an IF at some point.

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


Re: Checking function's parameters (type, value) or not ?

2016-04-06 Thread Michael Selik

> On Apr 6, 2016, at 2:07 PM, ast  wrote:
> 
> I would like to know if it is advised or not to test
> a function's parameters before running it, e.g
> for functions stored on a public library ?
> 
> def to_base(nber, base=16, use_af=True, sep=''):
>   assert isinstance(nber, int) and nber >= 0

> Without, python interpreter may crash later in the function
> or worse provide a meaningless result.

Crashing later isn the function is a *good* thing in many cases, because it 
provides a more specific error. Not only what input was bad, but *why* it was 
bad. [0]

Meaningless results might be meaningless to you, but your users may have 
thought of new ways to use your code. Assume they've thought of things you 
haven't.

You don't want to lock people into only your prescribed usage. Remember, they 
might have monkey-patched your dependency to do something really cool. Don't 
ruin their day with "discipline".

However, you might want to protect them from a subtle infinite loop or other 
traps that are tough to recover from. If it's an obvious infinity, well, that's 
their own fault. For example, ``range`` stops you from a zero step-size, but 
won't stop you from ``range(int(1e200))``.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: functools puzzle

2016-04-06 Thread Michael Selik

> On Apr 6, 2016, at 6:57 PM, George Trojan - NOAA Federal 
>  wrote:
> 
> The module functools has partial() defined as above, then overrides the
> definition by importing partial from _functools. That would explain the
> above behaviour. My question is why?

A couple speculations why an author might retain a vestigial Python 
implementation after re-implementing in C: to provide a backup in case the C 
fails to compile or to simply provide an easier-to-read example of what the C 
is doing.

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


Re: functools puzzle

2016-04-06 Thread Michael Selik
On Wed, Apr 6, 2016 at 8:16 PM George Trojan - NOAA Federal <
george.tro...@noaa.gov> wrote:

> My basic question is how to document functions created by
> functools.partial, such that the documentation can be viewed not only by
> reading the code. Of course, as the last resort, I could create my own
> implementation (i.e. copy the pure Python code).
>

I don't think this is a complete solution, but it might get you a bit
closer.

import functools
import math

def party(func, *args, **kwargs):
d = {'__name__': func.__name__,
 '__doc__': func.__doc__,}
partial = type('partial', (functools.partial,), d)
foo = partial(func, *args, **kwargs)
return foo

exp = party(pow, math.e)
help(exp)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: recursive methods require implementing a stack?

2016-04-07 Thread Michael Selik
On Thu, Apr 7, 2016, 7:51 AM Charles T. Smith 
wrote:

> On Wed, 06 Apr 2016 20:28:47 +, Rob Gaddi wrote:
>
> > Charles T. Smith wrote:
> >
> >> I just tried to write a recursive method in python - am I right that
> local
> >> variables are only lexically local scoped, so sub-instances have the
> same
> >> ones?  Is there a way out of that?  Do I have to push and pop my own
> simulated
> >> stack frame entry?
> >
> > You have been badly misled.  Python local variables are frame local, and
> > recursion just works.
>
>
> Well, I probably stumbled astray due to my own stupidity, can't blame
> anybody
> of having misled me...  ;)
>
> So it's a bug in my program!  Good news!  Thank you.
>

I'm guessing you are passing a list or dict to the recursive call and
discovering that the object is passed rather than a copy. Note that this is
not pass-by-reference, but "pass by object", for your Googling.

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


Re: Enum questions.

2016-04-13 Thread Michael Selik
On Wed, Apr 13, 2016, 12:14 PM Antoon Pardon 
wrote:

> I have been looking at the enum documentation and it
> seems enums are missing two features I rather find
> important.
>
> 1) Given an Enum value, someway to get the next/previous
>one
>
> 2) Given two Enum values, iterate over the values between
>them.
>
> Did I miss those in the documentation or are they really
> missing?
>

An Enum corresponds to "nominal" data that is coded as a number simply for
storage rather than meaning. If you want next/previous you are thinking of
"ordinal" data which is coded as numbers for the purpose of comparison (but
not arithmetic). Placing nominal data in order would be comparing apples
and oranges, so to speak.

However, IntEnum should give you the features you want.

https://docs.python.org/3/library/enum.html#intenum

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


Re: Looking for feedback on weighted voting algorithm

2016-04-14 Thread Michael Selik
On Thu, Apr 14, 2016, 7:37 PM justin walters 
wrote:

> On Apr 14, 2016 9:41 AM, "Martin A. Brown"  wrote:
> >
> >
> > Greetings Justin,
> >
> > >score = sum_of_votes/num_of_votes
> >
> > >votes = [(72, 4), (96, 3), (48, 2), (53, 1), (26, 4), (31, 3), (68, 2),
> (91, 1)]
> >
> > >Specifically, I'm wondering if this is a good algorithm for
> > >weighted voting. Essentially a vote is weighted by the number of
> > >votes it counts as. I realize that this is an extremely simple
> > >algorithm, but I was wondering if anyone had suggestions on how to
> > >improve it.
> >
> > I snipped most of your code.  I don't see anything wrong with your
> > overall approach.  I will make one suggestion: watch out for
> > DivisionByZero.
> >
> > try:
> > score = sum_of_votes / num_of_votes
> > except ZeroDivisionError:
> > score = float('nan')
> >
> > In your example data, all of the weights were integers, which means
> > that a simple mean function would work, as well, if you expanded the
> > votes to an alternate representation:
> >
> >   votes = [72, 72, 72, 72, 96, 96, 96, 48, 48, 53, 26, 26, 26, 26,
> >31, 31, 31, 68, 68, 91]
> >
> > But, don't bother!
> >
> > Your function can handle votes that have a float weight:
> >
> >   >>> weight([(4, 1.3), (1, 1),])
> >   2.695652173913044
> >
> > Have fun!
> >
> > -Martin
> >
> > --
> > Martin A. Brown
> > http://linux-ip.net/
>
> Thanks Martin!
>
> I'll add the check for division by zero. Didn't think about that. I think
> I'm going to sanitize input anyways, but always better to be safe than
> sorry.
>

I suggest not worrying about sanitizing inputs. If someone provides bad
data, Python will do the right thing: stop the program and print an
explanation of what went wrong, often a more helpful message than one you'd
write. Use error handling mostly for when you want to do something *other*
than stop the program.

I'm not sure I'd use NaN instead of raise division by zero error. NaNs can
be troublesome for downstream code that might not notice until it gets
confusing. A div-by-zero error is clear and easier to track down because of
the traceback.

What do you think of using list comprehensions?

weighted_sum = sum(rating * weight for rating, weight in votes)
total_weights = sum(weight for rating, weight in votes)
score = weighted_sum / total_weights

It's two loops as I wrote it, which is instinctively slower, but it might
actually execute faster because of the built-in sum vs a regular for loop.
Not sure.

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


Re: How to parameterize unittests

2016-04-15 Thread Michael Selik
On Fri, Apr 15, 2016, 11:16 AM Steven D'Aprano  wrote:

> On Fri, 15 Apr 2016 06:20 pm, Antoon Pardon wrote:
>
> >>> I see, that's going to be a lot of cut & pastes.
>
> (3) In your editor, run a global Find and Replace "avltree -> self.tree".
> You will need to inspect each one rather than do it automatically.
> Obviously you need to avoid changing the "tree = avltree" class attribute,
> and any import lines, but probably everything else should change.
>

There's probably some context around uses of avltree that should be
replaced. Perhaps a trailing dot: "avltree." replace with "self.tree." so
imports and assignments are ignored.

Also, your other, very helpful steps should minimize the trial and error
aspects of global find-replace. I wouldn't feel obligated to visually
inspect each one -- not on my first try, anyway.

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


Re: Looking for feedback on weighted voting algorithm

2016-04-15 Thread Michael Selik
On Fri, Apr 15, 2016, 7:56 PM  wrote:

> On Thursday, April 14, 2016 at 1:48:40 PM UTC-7, Michael Selik wrote:
> > I suggest not worrying about sanitizing inputs. If someone provides bad
> > data, Python will do the right thing: stop the program and print an
> > explanation of what went wrong, often a more helpful message than one
> you'd
> > write. Use error handling mostly for when you want to do something
> *other*
> > than stop the program.
> >
> > I'm not sure I'd use NaN instead of raise division by zero error. NaNs
> can
> > be troublesome for downstream code that might not notice until it gets
> > confusing. A div-by-zero error is clear and easier to track down because
> of
> > the traceback.
>
> I'd much rather sanitize the inputs and tell the user they did something
> bad than to have the script crash and give a 500 Internal Server Error to
> the user.
>

Right, my advice was only good if the user can read the original error and
traceback. And can restart the program, etc.

Even worse, I definitely wouldn't want to give any hints of the source code
> organization by letting the user see the traceback.
>

Depends on the kind of user you have.

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


Re: Dynamic inputs

2016-04-16 Thread Michael Selik
On Sat, Apr 16, 2016, 9:41 AM durgadevi1 <
srirajarajeswaridevikr...@gmail.com> wrote:

> what does dynamic inputs mean and how is it implemented in python
> programming?
>

In what context did you hear or read the phrase "dynamic inputs"?

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


Re: Guido sees the light: PEP 8 updated

2016-04-16 Thread Michael Selik
On Sat, Apr 16, 2016, 10:56 AM Marko Rauhamaa  wrote:

> Chris Angelico :
>
> > On Sat, Apr 16, 2016 at 6:06 PM, Marko Rauhamaa 
> wrote:
> >> It doesn't really matter one way or another. The true WTF is that it's
> >> been changed.
> >
> > Why? Was PEP 8 inscribed on stone tablets carried down from a mountain?
>
> In a way, yes.
>
> I don't follow PEP 8 to the tee; probably nobody does. However, I don't
> see the point of turning truckloads of exemplary Python code into
> truckloads of substandard Python code.
>

A change to PEP 8 does not change the quality of any code. Many teams
follow their own, different styles.

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


Re: Dynamic inputs

2016-04-17 Thread Michael Selik
On Sun, Apr 17, 2016, 7:01 AM durgadevi1 <
srirajarajeswaridevikr...@gmail.com> wrote:

> On Saturday, April 16, 2016 at 5:31:39 PM UTC+8, Michael Selik wrote:
> > On Sat, Apr 16, 2016, 9:41 AM durgadevi1 <
> > srirajarajeswaridevikr...@gmail.com> wrote:
> >
> > > what does dynamic inputs mean and how is it implemented in python
> > > programming?
> > >
> >
> > In what context did you hear or read the phrase "dynamic inputs"?
> >
> > >
>
> Oh I read it in my assignment manual. The sentence is "Use dynamic inputs
> to  XOR the binary file. " Thanks for your replies :)
>

It seems the assignment wants you to ask a user for input. What input
source would you like to use?

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


Re: How much sanity checking is required for function inputs?

2016-04-17 Thread Michael Selik
On Sun, Apr 17, 2016, 4:35 PM Christopher Reimer <
christopher_rei...@icloud.com> wrote:

> Greetings,
>
> I'm currently building a chess engine to learn the finer details of
> Python. When I learned all flavors of Java in community college a decade
> ago, we had to sanity check the hell out of the input values for every
> function and wrote a lot of redundant code in addition to the
> getters/setters code.
>
> Here's the input sanity checking I got for a generator function to
> return a set of chess pieces for a specified color and position.
>
>
> def generate_set(color, positions):
>
>  if color not in [VARS['COLOR_BLACK'], VARS['COLOR_WHITE']]:
>  raise Exception("Require \'{}\' or \'{}\' for input value, got
> \'{}\' instead.".format(VARS['COLOR_BLACK'], VARS['COLOR_WHITE'], color))
>
>  if len(positions) != 16:
>  raise Exception("Require 16 positions for input value, got {}
> instead.".format(len(positions)))
>
>
> The two sanity checks are fairly straight forward. Color input has to
> match the color constant strings. Positions input has to have 16 items.
>
> I *could* sanity check the positions input to determine if it had a list
> of 16 valid coordinates. The reason I don't is because the code that
> calls this function builds the coordinates from constants with valid
> coordinates. However, if I was doing this in my Java classes, one of my
> instructors rap me on the knuckles for not sanity checking to nth degree.
>
> How much sanity checking is too much in Python?
>

I'd rather turn the question around: how much sanity checking is necessary
or useful? You'll find the answer is "surprisingly little" compared to your
experience in Java. For example, you don't need to explicitly check whether
the color is present in your dictionary, because it'll give you a KeyError
if you look up a bad key.

Why does the len of positions need to be 16?

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


Re: How much sanity checking is required for function inputs?

2016-04-19 Thread Michael Selik
On Mon, Apr 18, 2016 at 1:05 AM Christopher Reimer <
christopher_rei...@icloud.com> wrote:

> On 4/17/2016 3:18 PM, Michael Selik wrote:
>
> > I'd rather turn the question around: how much sanity checking is
> > necessary or useful? You'll find the answer is "surprisingly little"
> > compared to your experience in Java.
>
> I'm looking for a pythonic approach to sanity checking. From what I read
> elsewhere, sanity checking belongs in the unit tests and/or library
> classes designed for other people to use. I haven't seen many examples
> of sanity checks that is common in Java.
>
> > For example, you don't need to
> > explicitly check whether the color is present in your dictionary,
> > because it'll give you a KeyError if you look up a bad key.
>
> Without the sanity check against the constant dictionary, the color
> variable could be anything (it should be a string value). Looking at the
> code again, I should relocate the sanity checks in the Piece base class.
>

Why relocate rather than remove? What message would you provide that's
better than ``KeyError: 42`` with a traceback that shows exactly which
dictionary is being used and how?

> Why does the len of positions need to be 16?
>
> The positions variable is list of coordinates for 16 chess pieces (eight
> pawns, two rooks, two knights, two bishops, a king and a queen) for each
> color, locating the pieces on either the bottom quarter (i.e., [(1,1),
> ..., (2,8)]) or the top quarter (i.e., [(7,1), ..., (8,8)]) of the board.
>

I meant, what goes wrong if the number of positions input is other than 16?
Without these "sanity" checks, your functions might be reusable in, say, a
checkers game.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python packages for hydrology and DEM

2016-04-19 Thread Michael Selik
On Tue, Apr 19, 2016 at 2:21 AM Xristos Xristoou  wrote:

> I want to ask for hydrology python packages with complete function to
> calculate hydrology tasks like fill,flow direction,flow accumulator and
> more?or how can i find genetic algorithms for to do this tasks to finaly
> create a complete hydro model with python.
>
> can someone tell me some documentation to do that with python ? i want to
> find like this TopoToolbox from matlab in python using
>
> also i want to ask and for python psckage for digital elevation models
> calcultor like idw krigging spline and more...or some documentation for this
> or some genetic algorithm for this
>
>
Sklearn is a good machine learning package (http://scikit-learn.org/).
I suggest simulated annealing rather than genetic algorithms.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How much sanity checking is required for function inputs?

2016-04-19 Thread Michael Selik
On Tue, Apr 19, 2016 at 11:23 PM Christopher Reimer <
christopher_rei...@icloud.com> wrote:

> On 4/19/2016 1:02 AM, Michael Selik wrote:
>
> > Why relocate rather than remove? What message would you provide that's
> > better than ``KeyError: 42`` with a traceback that shows exactly which
> > dictionary is being used and how?
>
> I think you misread my code. No dictionary exception occurs in the
> sanity checks. Below is the full function with the revised sanity check
> for positions that compares the input list with the two valid lists of
> board positions.
>

Perhaps I did misread it. What is the purpose of the "sanity check"? If
it's not obvious I suggest revising the code rather than adding comments.

The first time I read your code, I thought the check was designed to avoid
the possibility of a KeyError a few lines later. My suggestion was to
simply allow bad inputs to cause KeyErrors and not clutter your code. This
second time I'm reading, it seems to be a sort of boundary check, but using
``in`` which would cause a bug... Did you mean to use an inequality?


> > I meant, what goes wrong if the number of positions input is other than
> > 16? Without these "sanity" checks, your functions might be reusable in,
> > say, a checkers game.
>
> I have no desire to write reusable code for two
> similar but different games at the same time.
>

Reusability is a nice side-effect of fewer "sanity checks". Other goals are
clarity, efficiency, and productivity.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Creating Dict of Dict of Lists with joblib and Multiprocessing

2016-04-20 Thread Michael Selik
On Wed, Apr 20, 2016 at 10:50 AM Sims, David (NIH/NCI) [C] <
david.si...@nih.gov> wrote:

> Hi,
>
> Cross posted at
> http://stackoverflow.com/questions/36726024/creating-dict-of-dicts-with-joblib-and-multiprocessing,
> but thought I'd try here too as no responses there so far.
>
> A bit new to python and very new to parallel processing in python.  I have
> a script that will process a datafile and generate a dict of dicts.
> However, as I need to run this task on hundreds to thousands of these files
> and ultimately collate the data, I thought parallel processing made a lot
> of sense.  However, I can't seem to figure out how to create a data
> structure.  Minimal script without all the helper functions:
>
> #!/usr/bin/python
> import sys
> import os
> import re
> import subprocess
> import multiprocessing
> from joblib import Parallel, delayed
> from collections import defaultdict
> from pprint import pprint
>
> def proc_vcf(vcf,results):
> sample_name = vcf.rstrip('.vcf')
> results.setdefault(sample_name, {})
>
> # Run Helper functions 'run_cmd()' and 'parse_variant_data()' to
> generate a list of entries. Expect a dict of dict of lists
> all_vars = run_cmd('vcfExtractor',vcf)
> results[sample_name]['all_vars'] = parse_variant_data(all_vars,'all')
>
> # Run Helper functions 'run_cmd()' and 'parse_variant_data()' to
> generate a different list of data based on a different set of criteria.
> mois = run_cmd('moi_report', vcf)
> results[sample_name]['mois'] = parse_variant_data(mois, 'moi')
> return results
>
> def main():
> input_files = sys.argv[1:]
>
> # collected_data = defaultdict(lambda: defaultdict(dict))
> collected_data = {}
>
> # Parallel Processing version
> # num_cores = multiprocessing.cpu_count()
> # Parallel(n_jobs=num_cores)(delayed(proc_vcf)(vcf,collected_data) for
> vcf in input_files)
>
> # for vcf in input_files:
> # proc_vcf(vcf, collected_data)
>
> pprint(dict(collected_data))
> return
>
> if __name__=="__main__":
> main()
>
>
> Hard to provide source data as it's very large, but basically, the dataset
> will generate a dict of dicts of lists that contain two sets of data for
> each input keyed by sample and data type:
>
> { 'sample1' : {
> 'all_vars' : [
> 'data_val1',
> 'data_val2',
> 'etc'],
> 'mois' : [
> 'data_val_x',
> 'data_val_y',
> 'data_val_z']
> }
> 'sample2' : {
>'all_vars' : [
>.
>.
>.
>]
> }
> }
>
> If I run it without trying to multiprocess, not a problem.  I can't figure
> out how to parallelize this and create the same data structure.  I've tried
> to use defaultdict to create a defaultdict in main() to pass along, as well
> as a few other iterations, but I can't seem to get it right (getting key
> errors, pickle errors, etc.).  Can anyone help me with the proper way to do
> this?  I think I'm not making / initializing / working with the data
> structure correctly, but maybe my whole approach is ill conceived?
>


Processes cannot share memory, so your collected_data is only copied once,
at the time you pass it to each subprocess. There's an undocumented
ThreadPool that works the same as the process Pool (
https://docs.python.org/3.5/library/multiprocessing.html#using-a-pool-of-workers
)

ThreadPool will share memory across your subthreads. In the example I liked
to, just replace ``from multiprocessing import Pool`` with ``from
multiprocessing.pool import ThreadPool``.

How compute-intensive is your task? If it's mostly disk-read-intensive
rather than compute-intensive, then threads is all you need.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Detecting repeated subsequences of identical items

2016-04-20 Thread Michael Selik
On Wed, Apr 20, 2016 at 11:11 PM Steven D'Aprano 
wrote:

> I want to group [repeated] subsequences. For example, I have:
> "ABCABCABCDEABCDEFABCABCABCB"
> and I want to group it into repeating subsequences. I can see two
> ways... How can I do this? Does this problem have a standard name and/or
> solution?
>

I'm not aware of a standard name. This sounds like an unsupervised learning
problem. There's no objectively correct answer unless you add more
specificity to the problem statement.

Regexes may sound tempting at first, but because a repeating subsequence
may have nested repeating subsequences and this can go on infinitely, I
think we at least need a push-down automata.

I checked out some links for clustering algorithms that work on series
subsequences and I found some fun results.

Clustering is meaningless!
http://www.cs.ucr.edu/~eamonn/meaningless.pdf

I think you're in "no free lunch" territory. "Clustering of subsequence
time series remains an open issue in time series clustering"
http://www.hindawi.com/journals/tswj/2014/312521/

Any more detail on the problem to add constraints?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Detecting repeated subsequences of identical items

2016-04-20 Thread Michael Selik
On Thu, Apr 21, 2016 at 2:35 AM Michael Selik 
wrote:

> On Wed, Apr 20, 2016 at 11:11 PM Steven D'Aprano 
> wrote:
>
>> I want to group [repeated] subsequences. For example, I have:
>> "ABCABCABCDEABCDEFABCABCABCB"
>> and I want to group it into repeating subsequences. I can see two
>> ways... How can I do this? Does this problem have a standard name and/or
>> solution?
>>
>
> I'm not aware of a standard name. This sounds like an unsupervised
> learning problem. There's no objectively correct answer unless you add more
> specificity to the problem statement.
>
> Regexes may sound tempting at first, but because a repeating subsequence
> may have nested repeating subsequences and this can go on infinitely, I
> think we at least need a push-down automata.
>
> I checked out some links for clustering algorithms that work on series
> subsequences and I found some fun results.
>
> Clustering is meaningless!
> http://www.cs.ucr.edu/~eamonn/meaningless.pdf
>
> I think you're in "no free lunch" territory. "Clustering of subsequence
> time series remains an open issue in time series clustering"
> http://www.hindawi.com/journals/tswj/2014/312521/
>
> Any more detail on the problem to add constraints?
>

Some light reading suggests that you can improve your problem by defining a
minimum size for a subsequence to qualify. One paper suggests calling these
more interesting repetitions a "motif" to use a music metaphor. Looking for
any repetitions results in too many trivial results. Is that valid for your
usage?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Detecting repeated subsequences of identical items

2016-04-21 Thread Michael Selik
On Thu, Apr 21, 2016 at 2:55 AM Vlastimil Brom 
wrote:

> 2016-04-21 5:07 GMT+02:00 Steven D'Aprano :
> > I want to group subsequences.
> > "ABCABCABCDEABCDEFABCABCABCB"
> > ABC ABC ABCDE ABCDE F ABC ABC ABC B
> > or:
> > ABC ABC ABC D E A B C D E F ABC ABC ABC B
>
> if I am not missing something, the latter form of grouping might be
> achieved with the following regex: [snip]
> The former one seems to be more tricky...
>

Right. If the problem is constrained to say that repeated subsequences can
have no nested repeated subsequences, it's much easier to solve.

If you had "ABCABCABCABC" should that result in
ABC ABC ABC ABC, with 4 repetitions
or ABCABC ABCABC with 2 repetitions?
In this example, one might say the higher count is obviously better, but I
think it depends on the context. Maybe the user is looking for the biggest
patterns rather than the biggest counts.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How much sanity checking is required for function inputs?

2016-04-22 Thread Michael Selik
On Fri, Apr 22, 2016, 1:26 AM Stephen Hansen  wrote:

> On Thu, Apr 21, 2016, at 08:33 PM, Christopher Reimer wrote:
> > On 4/21/2016 7:20 PM, Stephen Hansen wrote:
> > > I... that... what... I'd forget that link and pretend you never went
> > > there. Its not helpful.
> >
> > I found it on the Internet, so it must be true -- and Pythonic at that!
>
> My advice is to not look at that site further. I can't count the number
> of things that are just... not useful or helpful.
>
> Directly translating the Gang of Four Design Pattern book to Python
> doesn't generally result in useful ideas, except in certain abstractions
> like the visitor pattern when you're designing big systems.
>

Frankly, for someone coming from Java, the best advice is to not write any
classes until you must. Of course classes in Python are very useful. It's
just that your Java habits are unnecessary and often counter-productive.

Just make some globals and some functions. Heck, even write procedurally
for a while. Free yourself from the Kingdom of Nouns.
http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html

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


Re: How much sanity checking is required for function inputs?

2016-04-23 Thread Michael Selik
On Sat, Apr 23, 2016 at 9:01 PM Christopher Reimer <
christopher_rei...@icloud.com> wrote:

> On 4/21/2016 9:46 PM, Ethan Furman wrote:
> > Oh! and Enum!!!  ;)
>
> OMG! I totally forgot about Enum. Oh, look. Python supports Enum. Now I
> don't have to roll my own!
>
> Hmm... What do we use Enum for? :)
>

You can use Enum in certain circumstances to replace int or str constants.
It can help avoid mistyping mistakes and might help your IDE give
auto-complete suggestions. I haven't found a good use for them myself, but
I'd been mostly stuck in Python 2 until recently.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How much sanity checking is required for function inputs?

2016-04-23 Thread Michael Selik
On Sat, Apr 23, 2016 at 9:31 PM Christopher Reimer <
christopher_rei...@icloud.com> wrote:

> On 4/21/2016 10:25 PM, Stephen Hansen wrote:
> >
> > Why not, 'color in ("black", "white")'?
>
> Checkers seems popular around here. What if I want to change "white" to
> "red," as red and black is a common color scheme for checkers. Do I
> change a single constant variable or replace all the occurrences in the
> files?
>

Why so many files? Python can easily support thousands of lines in a file.
If it's just one file any text editor can do a quick find-replace.

That said, it's easy to make some global ``red = 'red'``.

Some of these constants are shortcuts. Instead of writing slice(0, 16)
> or slice(48, 64), and getting the two confused, I write
> const['board_bottom'] or const['board_top'], respectively, when I want
> to pull the correct set of positions from coordinates list.
>

Why hide these things in a dict ``const`` instead of just making them
top-level variables in the module themselves?  ``board_top = slice(48,
64)``

> That said, if you're wanting to share constants across different parts
> > of your code, use a module.
>

Or just use one file to keep things easier. But, yes, I agree a module of
constants is appropriate for bigger projects.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How much sanity checking is required for function inputs?

2016-04-23 Thread Michael Selik
On Sun, Apr 24, 2016, 1:51 AM Steven D'Aprano  wrote:

> On Sun, 24 Apr 2016 12:34 pm, Michael Torrie wrote:
>
> > There are many aspects to Pythonic programming, not just OOP.  For
> > example using modules to store shared state for your program components
> > is very pythonic, rather than using classes.  A module is kind of like a
> > singleton instance, and still is object-oriented by the way (the module
> > is an object).
>
> I find myself going backwards and forwards on whether or not that is a good
> idea. I think it depends on whether you are writing a library or an
> application.
>
> If you're writing an application, then storing state in module-level
> variables works fine. Your application is, effectively, a singleton, and if
> you try to run it twice, you'll (probably) be running it in two distinct
> processes, so that's okay.
>
> (If you're not, if somehow you perform some sort of trickery where you have
> a single Python interpreter running your script twice in the same process,
> then it will break horribly. But if you can do that, you're living on the
> edge already and can probably deal with it.)
>
> But if you're writing a library, then using module state is a Bad Idea.
> Your
> library may be used by more than one client in the same process, and they
> will conflict over each other's library-level state. "Global variables
> considered harmful."
>

I think we're giving mixed messages because we're conflating "constants"
and globals that are expected to change.

In our case here, I think two clients in the same process sharing state
might be a feature rather than a bug. Or at least it has the same behavior
as the current implementation.

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


Re: How much sanity checking is required for function inputs?

2016-04-24 Thread Michael Selik
On Sun, Apr 24, 2016 at 2:08 PM Steven D'Aprano  wrote:

> On Sun, 24 Apr 2016 04:40 pm, Michael Selik wrote:
> > I think we're giving mixed messages because we're conflating
> "constants" and globals that are expected to change.
>
> When you talk about "state", that usually means "the current state of the
> program", not constants. math.pi is not "state".
>

Perhaps I was unclear. You provided an example of what I was trying to
point out.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Query regarding python 2.7.11 release

2016-04-29 Thread Michael Selik
>From searching bugs.python.org, I see that issues referencing CVE-2014-7185,
 CVE-2013-1752, and CVE-2014-1912 have all been marked as closed. I don't
see any issues referencing CVE-2014-4650 via Python's bug tracker, but did
spot it on Red Hat's. It appears to be related to issue 21766 (
http://bugs.python.org/issue21766) which has been marked closed, fixed.

So, yes, looks like they're all fixed.

On Thu, Apr 14, 2016 at 3:26 AM Gaurav Rastogi -X (garastog - ARICENT
TECHNOLOGIES MAURIITIUS LIMITED at Cisco)  wrote:

> Hi,
>
> We are currently using Python 2.6.7 in our product.
> We have received below vulnerabilities from field:
>
> CVE-2014-7185
>
> Integer overflow in bufferobject.c in Python before 2.7.8 allows
> context-dependent attackers to
> obtain sensitive information from process memory via a large size and
> offset in a "buffer" function.
>
> CVE-2013-1752
>
> python: multiple unbound readline() DoS flaws in python stdlib
>
> CVE-2014-1912
>
> python: buffer overflow in socket.recvfrom_into()
>
> CVE-2014-4650
>
> It was discovered that the CGIHTTPServer module incorrectly handled URL
> encoded paths.
> A remote attacker could use this flaw to execute scripts outside of the
> cgi-bin directory, or disclose source of scripts in the cgi-bin directory
>
>
> Currently I can see the 2.7.11 is the latest release as per the below link:
> https://www.python.org/downloads/
>
> Could you please suggest if the above mentioned vulnerabilities are
> resolved in the latest release?
>
> Regards
> Gaurav
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Conditionals And Control Flows

2016-05-04 Thread Michael Selik
On Wed, May 4, 2016 at 10:46 AM Cai Gengyang  wrote:

> I am trying to understand the boolean operator "and" in Python. It is
> supposed to return "True" when the expression on both sides of "and" are
> true
>

Not exactly, because they will short-circuit. Take a look at the docs. (
https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: No SQLite newsgroup, so I'll ask here about SQLite, python and MS Access

2016-05-04 Thread Michael Selik
On Wed, May 4, 2016, 6:51 PM DFS  wrote:

> Both of the following python commands successfully create a SQLite3
> datafile which crashes Access 2003 immediately upon trying to open it
> (via an ODBC linked table).
>

Have you tried using Access 2013?

On the other hand, a SQLite3 file created in VBScript, using the same
> ODBC driver, /is/ readable with Access 2003


Microsoft is pretty intense about backwards compatibility, sometimes even
staying compatibile with old bugs. Not saying that's the case here, but it
wouldn't surprise me.

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


Re: pylint woes

2016-05-07 Thread Michael Selik
On Sat, May 7, 2016 at 12:56 PM DFS  wrote:

> |mixed-indentation|186 | I always use tab
>

Don't mix tabs and spaces. I suggest selecting all lines and using your
editor to convert spaces to tabs. Usually there's a feature to "tabify".


> +-++
> |invalid-name |82  | every single variable name?!
>

Class names should be CamelCase
Everything else should be lowercase_with_underscores


> +-++
> |bad-whitespace   |65  | mostly because I line up =
>   signs:
>   var1  = value
>   var10 = value
>

Sure, that's your style. But pylint likes a different style. It's good to
use a standard. If it's just you, I suggest conforming to pylint. If you're
already on a team, use your team's standard.

+-++
> |trailing-whitespace  |59  | heh!
>

Get rid of it. Save some bytes.


> +-++
> |multiple-statements  |23  | do this to save lines.
>   Will continue doing it.
>

If you want to share your code with others, you should conform to community
standards to make things easier for others to read. Further, if you think
the core contributors are expert programmers, you should probably take
their advice: "sparse is better than dense". Do your future-self a favor
and write one statement per line. Today you find it easy to read. Six
months from now you won't.


> +-++
> |no-member|5   |
>
> "Module 'pyodbc' has no 'connect' member"   Yes it does.
> "Module 'pyodbc' has no 'Error' member" Yes it does.
>
> Issue with pylint, or pyodbc?
>

Not sure. Maybe pyodbc is written in a way that pylint can't see it's
connect or Error method/attribute.


> +-++
> |line-too-long|5   | meh
>

Yeah, I think 80 characters can be somewhat tight. Still, 5 long lines in
200ish lines of code? Sounds like you might be doing too much in those
lines or have too many levels of indentation.
"Sparse is better than dense"
"Flat is better than nested"


> +-++
> |wrong-import-order   |4   | does it matter?
>

No. I think pylint likes to alphabetize. With only 4 imports, it doesn't
matter. Still, why not alphabetize?


> +-++
> |missing-docstring|4   | what's the difference between
>   a docstring and a # comment?
>

Docstrings are tools for introspection. Many things in Python access the
__doc__ attribute to help you. Comments are never seen by module users.


> +-++
> |superfluous-parens   |3   | I like to surround 'or'
>   statments with parens
>

Ok. But over time you'll get used to not needing them. Edward Tufte says
you should have a high "information-to-ink" ratio.


> +-++
> |redefined-outer-name |3   | fixed. changed local var names.
> +-++
> |redefined-builtin|2   | fixed. Was using 'zip' and 'id'
> +-++
> |multiple-imports |2   | doesn't everyone?
>

Yeah, I do that as well.


> +-++
> |consider-using-enumerate |2   | see below [1]
>

As Chris explained.


> +-++
> |bad-builtin  |2   | warning because I used filter?
>

I think pylint likes comprehensions better. IMHO filter is OK. If you're
using a lambda, change to a comprehension.


> +-++
> |unused-import|1   | fixed
> +-++
> |unnecessary-pass |1   | fixed. left over from
>   Try..Except
> +-++
> |missing-final-newline|1   | I'm using Notepad++, with
>   EOL Conversion set to
>   'Windows Format'.  How
>   or should I fix this?
>

Add a few blank lines to the end of your file.


> +-++
> |fixme|1   | a TODO statement
> +-++
>
> Global evaluation
> -
> Your code has been rated at -7.64/10
>
>
>
> I assume -7.64 is really bad?
>
> Has anyone ever in history gotten 10/10 from pylint for a non-trivial
> program?
>

I'm certain of it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python - handling HTTP requests asynchronously

2016-05-07 Thread Michael Selik
On Fri, May 6, 2016 at 3:01 AM  wrote:

> The PDF is generated through an external API. Since currently is generated
> on demand, this is handled synchronously via an HTTP request/response.


Are you sending the request or are you receiving the request?
If you are sending, you can just use threads as you are only doing IO.
If you are receiving the requests and generating PDFs, you may want to use
subprocesses if the PDF-generation is compute-intensive.


> 3) multiprocessing module, with e.g. 10 as workers limit.
>

multiprocessing.Pool is an easy way to use subprocesses
multiprocessing.pool.ThreadPool is an easy way to use threads. It's not
well documented, but has the exact same interface as Pool.

the goal is to use the API concurrently (e.g. send 10 or 100 http requests
> simultaneously, depending on how many concurrent requests the API can
> handle).
>

Sounds like you want to use threads. How does the API let you know you're
hitting it too frequently? Perhaps you want to code an exponential backoff
and retry wrapper for your API requests.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String concatenation (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?)

2016-05-09 Thread Michael Selik
You're saying that wasn't a coded message?

On Sun, May 8, 2016, 10:44 PM srinivas devaki 
wrote:

> I'm so sorry, forgot to lock my phone.
> On May 9, 2016 9:01 AM, "srinivas devaki" 
> wrote:
>
> > f be gfdnbh be b GB GB BH GB vbjfhjb GB bffbbubbv GB hbu hbu
> > fjbjfbbbufhbvh VB have fqbgvfb NB bb GB GB GB GB bbu GB vu GB vu GB
> GB
> > b GB fbufjnb BH GB GB bvvfbubbjubuv GB b fbufbbby GB bfff GB f GB
> > bbbu GB GB ffinj GB vh vh fjb GB fj GB h h GB gjfthey're the b GB gjf GBG
> > GBG q GB fbb b bh VB ffbff GBG fbfvrgv
> > On May 9, 2016 7:49 AM, "Chris Angelico"  wrote:
> >
> > On Mon, May 9, 2016 at 10:44 AM, Thomas 'PointedEars' Lahn
> >  wrote:
> > > With the “%” string operator (deprecated), str.format(), and
> > str.Template,
> > > you can use other values in string values even without concatenation.
> >
> > Not deprecated. Don't spread FUD.
> >
> > > Finally, with SQL you should prefer Prepared Statements and Stored
> > > Procedures, not bare strings, to avoid SQL injection:
> > >
> > > 
> >
> > He is safe. He's using parameterized queries.
> >
> > > Also, it would be a good idea if you posted under your real name.
> > Internet
> > > is the thing with cables; Usenet is the thing with people.  I for one
> > tend
> > > to avoid communicating with few-letter entities; exceptions to that
> would
> > > probably include only E.T., M.J., ALF, and K.I.T.T.
> >
> > I'm not using Usenet, Mr PointedEars.
> >
> > ChrisA
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> >
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What should a decorator do if an attribute already exists?

2016-05-10 Thread Michael Selik
On Tue, May 10, 2016 at 11:48 AM Peter Otten <__pete...@web.de> wrote:

> Steven D'Aprano wrote:
> > I have a decorator that adds an attribute to the decorated function:
> >inner.instrument = instrument
> >return inner
>


> the original instrument is still accessible as f.__wrapped__.instrument


I'd say Peter's example is Option 6:
Wrap the instrumented function with a second instrumented wrapper.

Since you have various kinds of instruments, silently adding another layer
makes sense to me. Is it OK if the order matters? Will some of the
instruments get confused because they're working on a wrapper instead of
the original function?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python - handling HTTP requests asynchronously

2016-05-10 Thread Michael Selik
On Sat, May 7, 2016 at 4:46 PM  wrote:

> Il giorno sabato 7 maggio 2016 21:04:47 UTC+2, Michael Selik ha scritto:
> > On Fri, May 6, 2016 at 3:01 AM  wrote:
> >
> > > The PDF is generated through an external API. Since currently is
> generated
> > > on demand, this is handled synchronously via an HTTP request/response.
> >
> >
> > Are you sending the request or are you receiving the request?
> > If you are sending, you can just use threads as you are only doing IO.
> > If you are receiving the requests and generating PDFs, you may want to
> use
> > subprocesses if the PDF-generation is compute-intensive.
> >
> >
> > > 3) multiprocessing module, with e.g. 10 as workers limit.
> > >
> >
> > multiprocessing.Pool is an easy way to use subprocesses
> > multiprocessing.pool.ThreadPool is an easy way to use threads. It's not
> > well documented, but has the exact same interface as Pool.
> >
> > the goal is to use the API concurrently (e.g. send 10 or 100 http
> requests
> > > simultaneously, depending on how many concurrent requests the API can
> > > handle).
> > >
> >
> > Sounds like you want to use threads. How does the API let you know you're
> > hitting it too frequently? Perhaps you want to code an exponential
> backoff
> > and retry wrapper for your API requests.
>
> Thanks for the reply.
> Currently the django view that does the job does three http request:
> - the first does a POST and send the payload used during the PDF
> rendering, the response contains a url to check the pdf generation progress;
> - the second loop over that url, checking the progress of pdf generation.
> Once the response contains the keyword 'status': 'complete', then it give
> also a url for the file retrieval;
> - the third one is a GET to retrieve the file, the reponse contains the
> binary content of the file, then this content is read and wrapped as
> attachment of a django http response, and then returned to the user.
>
> the goal is to reuse the existing code as much as possible, possibly doing
> concurrently, and saving the file instead on a folder.
>

I'm not a Django expert. Does the Django View require all that stuff to
happen before Django can send an HTTP Response back to the user? If so, I
suggest you respond to the user immediately: "now generating a bunch of
PDFs..." and take care of the rest in the background. Perhaps just write to
a file, database, or message queue the info for the PDFs to generate. Have
a separate program periodically read the file, database, or message queue
to do that work and then maybe email the user when it's completed.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to put back a number-based index

2016-05-13 Thread Michael Selik
On Fri, May 13, 2016 at 12:27 PM David Shi via Python-list <
python-list@python.org> wrote:

> I lost my indexes after grouping in Pandas.
> I managed to rest_index and got back the index column.
> But How can I get back a index row?
>

Was the grouping an aggregation? If so, the original indexes are
meaningless. What you could do is reset_index before the grouping and when
you aggregate decide how to handle the formerly-known-as-index column (min,
max, mean, ?).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Distinction between “class” and “type”

2016-05-13 Thread Michael Selik
On Fri, May 13, 2016 at 1:10 AM Ben Finney 
wrote:

> Howdy all,
>
> Ever since Python's much-celebrated Grand Unification of classes and
> types, I have used those terms interchangeably: every class is a type,
> and every type is a class.
>
> That may be an unwise conflation. With the recent rise of optional type
> annotation in Python 3, more people are speaking about the important
> distinction between a class and a type.
>

The concept of a type existed before the concept of object-oriented
programming. A type is more of a mathematical concept. Do some reading on
"type theory" if you'd like to know more. A type system ideally allows
logical deductions about the validity of your expressions/statements. Stuff
a static analyzer is good at.

With the advent of object-orientation and the concept of inheritance,
things have gotten a little confused. You could make a distinction between
"type inheritance" and "implementation/code inheritance" but many languages
do not. When you make a subclass in Python, it inherits both the type and
the code of the superclass. I suppose you could say the composition vs
inheritance argument stems from some people advocating code inheritance
without type inheritance.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to put back a number-based index

2016-05-13 Thread Michael Selik
In order to preserve your index after the aggregation, you need to make
sure it is considered a data column (via reset_index) and then choose how
your aggregation will operate on that column.

On Fri, May 13, 2016 at 3:29 PM David Shi  wrote:

> Hello, Michael,
>
> Why reset_index before grouping?
>
> Regards.
>
> David
>
>
> On Friday, 13 May 2016, 17:57, Michael Selik 
> wrote:
>
>
>
>
> On Fri, May 13, 2016 at 12:27 PM David Shi via Python-list <
> python-list@python.org> wrote:
>
> I lost my indexes after grouping in Pandas.
> I managed to rest_index and got back the index column.
> But How can I get back a index row?
>
>
> Was the grouping an aggregation? If so, the original indexes are
> meaningless. What you could do is reset_index before the grouping and when
> you aggregate decide how to handle the formerly-known-as-index column (min,
> max, mean, ?).
>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to put back a number-based index

2016-05-13 Thread Michael Selik
Just in case I misunderstood, why don't you make a little example of before
and after the grouping? This mailing list does not accept attachments, so
you'll have to make do with pasting a few rows of comma-separated or
tab-separated values.

On Fri, May 13, 2016 at 3:56 PM Michael Selik 
wrote:

> In order to preserve your index after the aggregation, you need to make
> sure it is considered a data column (via reset_index) and then choose how
> your aggregation will operate on that column.
>
> On Fri, May 13, 2016 at 3:29 PM David Shi  wrote:
>
>> Hello, Michael,
>>
>> Why reset_index before grouping?
>>
>> Regards.
>>
>> David
>>
>>
>> On Friday, 13 May 2016, 17:57, Michael Selik 
>> wrote:
>>
>>
>>
>>
>> On Fri, May 13, 2016 at 12:27 PM David Shi via Python-list <
>> python-list@python.org> wrote:
>>
>> I lost my indexes after grouping in Pandas.
>> I managed to rest_index and got back the index column.
>> But How can I get back a index row?
>>
>>
>> Was the grouping an aggregation? If so, the original indexes are
>> meaningless. What you could do is reset_index before the grouping and when
>> you aggregate decide how to handle the formerly-known-as-index column (min,
>> max, mean, ?).
>>
>>
>>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Design: Idiom for classes and methods that are customizable by the user?

2016-05-13 Thread Michael Selik
On Fri, May 13, 2016 at 2:41 AM Gregory Ewing 
wrote:

> Dirk Bächle wrote:
> > I'm currently following the "Factory" pattern (more or less) as I know
> > it from C++ and similar languages.
>
> This statement sets off alarm bells for me. If you're using some
> design pattern in Python just because you learned to do it that
> way in C++/Java/whatever, you're probably making it more
> complicated than it needs to be.
>

I share Greg's trepidation when I hear a phrase like that, but the general
idea of a registry of classes or functions and then picking the right one
based on string input is fine.

How would the API work for custom Taskmasters? It's not so great to require
that the user must explicitly ``add`` their derived class after defining
it. Perhaps that add function could be a decorator?

def register(key):
def decorator(cls):
if not issubclass(cls, Taskmaster):
raise TypeError('You fool!')
registry[key] = cls
return decorator

@register('noclean')
class NoCleanTaskMaster(TaskMaster):
"Looks clean enough, why worry?"
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to put back a number-based index

2016-05-13 Thread Michael Selik
Here's an example.

>>> import pandas as pd
>>> df = pd.DataFrame({'group': list('AB') * 2, 'data': range(4)},
index=list('wxyz'))
>>> df
   data group
w 0 A
x 1 B
y 2 A
z 3 B
>>> df = df.reset_index()
>>> df
  index  data group
0 w 0 A
1 x 1 B
2 y 2 A
3 z 3 B
>>> df.groupby('group').max()
  index  data
group
A y 2
B z 3

If that doesn't help, you'll need to explain what you're trying to
accomplish in detail -- what variables you started with, what
transformations you want to do, and what variables you hope to have when
finished.

On Fri, May 13, 2016 at 4:36 PM David Shi  wrote:

> Hello, Michael,
>
> I changed groupby with one column.
>
> The index is different.
>
> Index([   u'AL',u'AR',u'AZ',u'CA',u'CO',u'CT',u'DC',
>   u'DE',u'FL',u'GA',u'IA',u'ID',u'IL',u'IN',
>   u'KS',u'KY',u'LA',u'MA',u'MD',u'ME',u'MI',
>   u'MN',u'MO',u'MS',u'MT',u'NC',u'ND',u'NE',
>   u'NH',u'NJ',u'NM',u'NV',u'NY',u'OH',u'OK',
>   u'OR',u'PA',u'RI',u'SC',u'SD', u'State',u'TN',
>   u'TX',u'UT',u'VA',u'VT',u'WA',u'WI',u'WV',
>   u'WY'],
>   dtype='object', name=0)
>
>
> How to use this index?
>
>
> Regards.
>
>
> David
>
>
>
> On Friday, 13 May 2016, 21:19, David Shi  wrote:
>
>
> Hello, Michael,
>
> I typed in df.index
>
> I got the following
>
> MultiIndex(levels=[[1.0, 4.0, 5.0, 6.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 
> 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 
> 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 
> 42.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 53.0, 54.0, 55.0, 
> 56.0], [u'AL', u'AR', u'AZ', u'CA', u'CO', u'CT', u'DC', u'DE', u'FL', u'GA', 
> u'IA', u'ID', u'IL', u'IN', u'KS', u'KY', u'LA', u'MA', u'MD', u'ME', u'MI', 
> u'MN', u'MO', u'MS', u'MT', u'NC', u'ND', u'NE', u'NH', u'NJ', u'NM', u'NV', 
> u'NY', u'OH', u'OK', u'OR', u'PA', u'RI', u'SC', u'SD', u'State', u'TN', 
> u'TX', u'UT', u'VA', u'VT', u'WA', u'WI', u'WV', u'WY']],
>labels=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
> 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 
> 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48], [0, 2, 1, 3, 4, 5, 7, 6, 
> 8, 9, 11, 12, 13, 10, 14, 15, 16, 19, 18, 17, 20, 21, 23, 22, 24, 27, 31, 28, 
> 29, 30, 32, 25, 26, 33, 34, 35, 36, 37, 38, 39, 41, 42, 43, 45, 44, 46, 48, 
> 47, 49]],
>names=[u'StateFIPS', 0])
>
> Regards.
>
>
> David
>
>
>
> On Friday, 13 May 2016, 21:11, David Shi  wrote:
>
>
> Dear Michael,
>
> I have done a number of operation in between.
>
> Providing that information does not help you
>
> How to reset index after grouping and various operations is of interest.
>
> How to type in a command to find out its current dataframe?
>
> Regards.
>
> David
>
>
> On Friday, 13 May 2016, 20:58, Michael Selik 
> wrote:
>
>
> Just in case I misunderstood, why don't you make a little example of
> before and after the grouping? This mailing list does not accept
> attachments, so you'll have to make do with pasting a few rows of
> comma-separated or tab-separated values.
>
> On Fri, May 13, 2016 at 3:56 PM Michael Selik 
> wrote:
>
> In order to preserve your index after the aggregation, you need to make
> sure it is considered a data column (via reset_index) and then choose how
> your aggregation will operate on that column.
>
> On Fri, May 13, 2016 at 3:29 PM David Shi  wrote:
>
> Hello, Michael,
>
> Why reset_index before grouping?
>
> Regards.
>
> David
>
>
> On Friday, 13 May 2016, 17:57, Michael Selik 
> wrote:
>
>
>
>
> On Fri, May 13, 2016 at 12:27 PM David Shi via Python-list <
> python-list@python.org> wrote:
>
> I lost my indexes after grouping in Pandas.
> I managed to rest_index and got back the index column.
> But How can I get back a index row?
>
>
> Was the grouping an aggregation? If so, the original indexes are
> meaningless. What you could do is reset_index before the grouping and when
> you aggregate decide how to handle the formerly-known-as-index column (min,
> max, mean, ?).
>
>
>
>
>
>
>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to put back a number-based index

2016-05-13 Thread Michael Selik
To clarify that you're specifying the index as a label, use df.iloc

>>> df = pd.DataFrame({'X': range(4)}, index=list('abcd'))
>>> df
   X
a  0
b  1
c  2
d  3
>>> df.loc['a']
X0
Name: a, dtype: int64
>>> df.iloc[0]
X0
Name: a, dtype: int64

On Fri, May 13, 2016 at 4:54 PM David Shi  wrote:

> Dear Michael,
>
> To avoid complication, I only groupby using one column.
>
> It is OK now.  But, how to refer to new row index?  How do I use floating
> index?
>
> Float64Index([ 1.0,  4.0,  5.0,  6.0,  8.0,  9.0, 10.0, 11.0, 12.0, 13.0, 
> 16.0,
>   17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 
> 27.0,
>   28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 
> 38.0,
>   39.0, 40.0, 41.0, 42.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 
> 50.0,
>   51.0, 53.0, 54.0, 55.0, 56.0],
>      dtype='float64', name=u'StateFIPS')
>
>
> Regards.
>
>
> David
>
>
>
> On Friday, 13 May 2016, 21:43, Michael Selik 
> wrote:
>
>
> Here's an example.
>
> >>> import pandas as pd
> >>> df = pd.DataFrame({'group': list('AB') * 2, 'data': range(4)},
> index=list('wxyz'))
> >>> df
>data group
> w 0 A
> x 1 B
> y 2 A
> z 3 B
> >>> df = df.reset_index()
> >>> df
>   index  data group
> 0 w 0 A
> 1 x 1 B
> 2 y 2 A
> 3 z 3 B
> >>> df.groupby('group').max()
>   index  data
> group
> A y 2
> B z 3
>
> If that doesn't help, you'll need to explain what you're trying to
> accomplish in detail -- what variables you started with, what
> transformations you want to do, and what variables you hope to have when
> finished.
>
> On Fri, May 13, 2016 at 4:36 PM David Shi  wrote:
>
> Hello, Michael,
>
> I changed groupby with one column.
>
> The index is different.
>
> Index([   u'AL',u'AR',u'AZ',u'CA',u'CO',u'CT',u'DC',
>   u'DE',u'FL',u'GA',u'IA',u'ID',u'IL',u'IN',
>   u'KS',u'KY',u'LA',u'MA',u'MD',u'ME',u'MI',
>   u'MN',u'MO',u'MS',u'MT',u'NC',u'ND',u'NE',
>   u'NH',u'NJ',u'NM',u'NV',u'NY',u'OH',u'OK',
>   u'OR',u'PA',u'RI',u'SC',u'SD', u'State',u'TN',
>   u'TX',u'UT',u'VA',u'VT',u'WA',u'WI',u'WV',
>   u'WY'],
>   dtype='object', name=0)
>
>
> How to use this index?
>
>
> Regards.
>
>
> David
>
>
>
> On Friday, 13 May 2016, 21:19, David Shi  wrote:
>
>
> Hello, Michael,
>
> I typed in df.index
>
> I got the following
>
> MultiIndex(levels=[[1.0, 4.0, 5.0, 6.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 
> 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 
> 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 
> 42.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 53.0, 54.0, 55.0, 
> 56.0], [u'AL', u'AR', u'AZ', u'CA', u'CO', u'CT', u'DC', u'DE', u'FL', u'GA', 
> u'IA', u'ID', u'IL', u'IN', u'KS', u'KY', u'LA', u'MA', u'MD', u'ME', u'MI', 
> u'MN', u'MO', u'MS', u'MT', u'NC', u'ND', u'NE', u'NH', u'NJ', u'NM', u'NV', 
> u'NY', u'OH', u'OK', u'OR', u'PA', u'RI', u'SC', u'SD', u'State', u'TN', 
> u'TX', u'UT', u'VA', u'VT', u'WA', u'WI', u'WV', u'WY']],

Re: How to put back a number-based index

2016-05-13 Thread Michael Selik
What have code you tried? What error message are you receiving?

On Fri, May 13, 2016, 5:54 PM David Shi  wrote:

> Hello, Michael,
>
> How to convert a float type column into an integer or label or string type?
>
>
> On Friday, 13 May 2016, 22:02, Michael Selik 
> wrote:
>
>
> To clarify that you're specifying the index as a label, use df.iloc
>
> >>> df = pd.DataFrame({'X': range(4)}, index=list('abcd'))
> >>> df
>X
> a  0
> b  1
> c  2
> d  3
> >>> df.loc['a']
> X0
> Name: a, dtype: int64
> >>> df.iloc[0]
> X0
> Name: a, dtype: int64
>
> On Fri, May 13, 2016 at 4:54 PM David Shi  wrote:
>
> Dear Michael,
>
> To avoid complication, I only groupby using one column.
>
> It is OK now.  But, how to refer to new row index?  How do I use floating
> index?
>
> Float64Index([ 1.0,  4.0,  5.0,  6.0,  8.0,  9.0, 10.0, 11.0, 12.0, 13.0, 
> 16.0,
>   17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 
> 27.0,
>   28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 
> 38.0,
>   39.0, 40.0, 41.0, 42.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 
> 50.0,
>   51.0, 53.0, 54.0, 55.0, 56.0],
>  dtype='float64', name=u'StateFIPS')
>
>
> Regards.
>
>
> David
>
>
>
> On Friday, 13 May 2016, 21:43, Michael Selik 
> wrote:
>
>
> Here's an example.
>
> >>> import pandas as pd
> >>> df = pd.DataFrame({'group': list('AB') * 2, 'data': range(4)},
> index=list('wxyz'))
> >>> df
>data group
> w 0 A
> x 1 B
> y 2 A
> z 3 B
> >>> df = df.reset_index()
> >>> df
>   index  data group
> 0 w 0 A
> 1 x 1 B
> 2 y 2 A
> 3 z 3 B
> >>> df.groupby('group').max()
>   index  data
> group
> A y 2
> B z 3
>
> If that doesn't help, you'll need to explain what you're trying to
> accomplish in detail -- what variables you started with, what
> transformations you want to do, and what variables you hope to have when
> finished.
>
> On Fri, May 13, 2016 at 4:36 PM David Shi  wrote:
>
> Hello, Michael,
>
> I changed groupby with one column.
>
> The index is different.
>
> Index([   u'AL',u'AR',u'AZ',u'CA',u'CO',u'CT',u'DC',
>   u'DE',u'FL',u'GA',u'IA',u'ID',u'IL',u'IN',
>   u'KS',u'KY',u'LA',u'MA',u'MD',u'ME',u'MI',
>   u'MN',u'MO',u'MS',u'MT',u'NC',u'ND',u'NE',
>   u'NH',u'NJ',u'NM',u'NV',u'NY',u'OH',u'OK',
>   u'OR',u'PA',u'RI',u'SC',u'SD', u'State',u'TN',
>   u'TX',u'UT',u'VA',u'VT',u'WA',u'WI',u'WV',
>   u'WY'],
>   dtype='object', name=0)
>
>
> How to use this index?
>
>
> Regards.
>
>
> David
>
>
>
> On Friday, 13 May 2016, 21:19, David Shi  wrote:
>
>
> Hello, Michael,
>
> I typed in df.index
>
> I got the following
>
> MultiIndex(levels=[[1.0, 4.0, 5.0, 6.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 
> 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 
> 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 
> 42.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 53.0, 54.0, 55.0, 
> 56.0], [u'AL', u'AR', u'AZ', u'CA', u'CO', u'CT', u'DC', u'DE', u'FL', u'GA', 
> u'IA', u'ID', u'IL', u'IN', u'KS', u'KY', u'LA', u'MA', u'MD', u'ME', u'MI', 
> u'MN', u'MO', u'MS', u'MT', u'NC', u'ND', u'NE', u&

Re: How to put back a number-based index

2016-05-13 Thread Michael Selik
What were you hoping to get from ``df[0]``?
When you say it "yields nothing" do you mean it raised an error? What was
the error message?

Have you tried a Google search for "pandas set index"?
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html

On Fri, May 13, 2016 at 11:18 PM David Shi  wrote:

> Hello, Michael,
>
> I tried to discover the problem.
>
> df[0]   yields nothing
> df[1]  yields nothing
> df[2] yields nothing
>
> However, df[3] gives the following:
>
> sid
> -9223372036854775808  NaN
>  1  133738.70
>  4  295256.11
>  5  137733.09
>  6  409413.58
>  8  269600.97
>  9   12852.94
>
>
> Can we split this back to normal?  or turn it into a dictionary, so that I 
> can put values back properly.
>
>
> I like to use sid as index, some way.
>
>
> Regards.
>
>
> David
>
>
>
> On Friday, 13 May 2016, 22:58, Michael Selik 
> wrote:
>
>
> What have code you tried? What error message are you receiving?
>
> On Fri, May 13, 2016, 5:54 PM David Shi  wrote:
>
> Hello, Michael,
>
> How to convert a float type column into an integer or label or string type?
>
>
> On Friday, 13 May 2016, 22:02, Michael Selik 
> wrote:
>
>
> To clarify that you're specifying the index as a label, use df.iloc
>
> >>> df = pd.DataFrame({'X': range(4)}, index=list('abcd'))
> >>> df
>X
> a  0
> b  1
> c  2
> d  3
> >>> df.loc['a']
> X0
> Name: a, dtype: int64
> >>> df.iloc[0]
> X0
> Name: a, dtype: int64
>
> On Fri, May 13, 2016 at 4:54 PM David Shi  wrote:
>
> Dear Michael,
>
> To avoid complication, I only groupby using one column.
>
> It is OK now.  But, how to refer to new row index?  How do I use floating
> index?
>
> Float64Index([ 1.0,  4.0,  5.0,  6.0,  8.0,  9.0, 10.0, 11.0, 12.0, 13.0, 
> 16.0,
>   17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 
> 27.0,
>       28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 
> 38.0,
>   39.0, 40.0, 41.0, 42.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 
> 50.0,
>   51.0, 53.0, 54.0, 55.0, 56.0],
>  dtype='float64', name=u'StateFIPS')
>
>
> Regards.
>
>
> David
>
>
>
> On Friday, 13 May 2016, 21:43, Michael Selik 
> wrote:
>
>
> Here's an example.
>
> >>> import pandas as pd
> >>> df = pd.DataFrame({'group': list('AB') * 2, 'data': range(4)},
> index=list('wxyz'))
> >>> df
>data group
> w 0 A
> x 1 B
> y 2 A
> z 3 B
> >>> df = df.reset_index()
> >>> df
>   index  data group
> 0 w 0 A
> 1 x 1 B
> 2 y 2 A
> 3 z 3 B
> >>> df.groupby('group').max()
>   index  data
> group
> A y 2
> B z 3
>
> If that doesn't help, you'll need to explain what you're trying to
> accomplish in detail -- what variables you started with, what
> transformations you want to do, and what variables you hope to have when
> finished.
>
> On Fri, May 13, 2016 at 4:36 PM David Shi  wrote:
>
> Hello, Michael,
>
> I changed groupby with one column.
>
> The index is different.
>
> Index([   u'AL',u'AR',u'AZ',u'CA',u'CO',u'CT',u'DC',
>   u'DE',u'FL',u'GA',u'IA',u'ID',u'IL',u'IN',
>   u'KS',u'KY',u'LA',u'MA',u'MD',u'ME',u'MI',
>   u'MN',u'MO',u'MS',u'MT',u'NC',u'ND',u'NE',
>   u'NH',u'NJ',u'NM',u'NV',u'NY',u'OH',u'OK',
>   u'OR',u'PA',u'RI',u'SC',u'SD', u'State',u'TN',
>   u'TX',u'UT',u'VA',u'VT',u'WA',u'WI',u'W

Re: How to put back a number-based index

2016-05-14 Thread Michael Selik
It looks like you're getting a Series. Apparently more that one row has the
same index.

On Fri, May 13, 2016 at 11:30 PM Michael Selik 
wrote:

> What were you hoping to get from ``df[0]``?
> When you say it "yields nothing" do you mean it raised an error? What was
> the error message?
>
> Have you tried a Google search for "pandas set index"?
>
> http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html
>
> On Fri, May 13, 2016 at 11:18 PM David Shi  wrote:
>
>> Hello, Michael,
>>
>> I tried to discover the problem.
>>
>> df[0]   yields nothing
>> df[1]  yields nothing
>> df[2] yields nothing
>>
>> However, df[3] gives the following:
>>
>> sid
>> -9223372036854775808  NaN
>>  1  133738.70
>>  4  295256.11
>>  5  137733.09
>>  6  409413.58
>>  8  269600.97
>>  9   12852.94
>>
>>
>> Can we split this back to normal?  or turn it into a dictionary, so that I 
>> can put values back properly.
>>
>>
>> I like to use sid as index, some way.
>>
>>
>> Regards.
>>
>>
>> David
>>
>>
>>
>> On Friday, 13 May 2016, 22:58, Michael Selik 
>> wrote:
>>
>>
>> What have code you tried? What error message are you receiving?
>>
>> On Fri, May 13, 2016, 5:54 PM David Shi  wrote:
>>
>> Hello, Michael,
>>
>> How to convert a float type column into an integer or label or string
>> type?
>>
>>
>> On Friday, 13 May 2016, 22:02, Michael Selik 
>> wrote:
>>
>>
>> To clarify that you're specifying the index as a label, use df.iloc
>>
>> >>> df = pd.DataFrame({'X': range(4)}, index=list('abcd'))
>> >>> df
>>X
>> a  0
>> b  1
>> c  2
>> d  3
>> >>> df.loc['a']
>> X0
>> Name: a, dtype: int64
>> >>> df.iloc[0]
>> X0
>> Name: a, dtype: int64
>>
>> On Fri, May 13, 2016 at 4:54 PM David Shi  wrote:
>>
>> Dear Michael,
>>
>> To avoid complication, I only groupby using one column.
>>
>> It is OK now.  But, how to refer to new row index?  How do I use floating
>> index?
>>
>> Float64Index([ 1.0,  4.0,  5.0,  6.0,  8.0,  9.0, 10.0, 11.0, 12.0, 13.0, 
>> 16.0,
>>   17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 
>> 27.0,
>>   28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 
>> 38.0,
>>   39.0, 40.0, 41.0, 42.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 
>> 50.0,
>>   51.0, 53.0, 54.0, 55.0, 56.0],
>>  dtype='float64', name=u'StateFIPS')
>>
>>
>> Regards.
>>
>>
>> David
>>
>>
>>
>> On Friday, 13 May 2016, 21:43, Michael Selik 
>> wrote:
>>
>>
>> Here's an example.
>>
>> >>> import pandas as pd
>> >>> df = pd.DataFrame({'group': list('AB') * 2, 'data': range(4)},
>> index=list('wxyz'))
>> >>> df
>>data group
>> w 0 A
>> x 1 B
>> y 2 A
>> z 3 B
>> >>> df = df.reset_index()
>> >>> df
>>   index  data group
>> 0 w 0 A
>> 1 x 1 B
>> 2 y 2 A
>> 3 z 3 B
>> >>> df.groupby('group').max()
>>   index  data
>> group
>> A y 2
>> B z 3
>>
>> If that doesn't help, you'll need to explain what you're trying to
>> accomplish in detail -- what variables you started with, what
>> transformations you want to do, and what variables you hope to have when
>> finished.
>>
>> On Fri, May 13, 2016 at 4:36 PM David Shi  wrote:
>>
>> Hello, Michael,
>>
>> I changed groupby with one column.
>>
>> The index is different.
>>
>> Index([   u'AL',u'AR',u'AZ',u'CA',u'CO',u'CT',u'DC',
>>   u'DE',u'FL',u'GA',u'IA',u'ID',u'IL',u'IN

Re: How to put back a number-based index

2016-05-14 Thread Michael Selik
David, it sounds like you'll need a thorough introduction to the basics of
Python.
Check out the tutorial: https://docs.python.org/3/tutorial/

On Sat, May 14, 2016 at 6:19 AM David Shi  wrote:

> Hello, Michael,
>
> I discovered that the problem is "two columns of data are put together"
> and "are recognised as one column".
>
> This is very strange.  I would like to understand the subject well.
>
> And, how many ways are there to investigate into the nature of objects
> dynamically?
>
> Some object types only get shown as an object.  Are there anything to be
> typed in Python, to reveal objects.
>
> Regards.
>
> David
>
>
> On Saturday, 14 May 2016, 4:30, Michael Selik 
> wrote:
>
>
> What were you hoping to get from ``df[0]``?
> When you say it "yields nothing" do you mean it raised an error? What was
> the error message?
>
> Have you tried a Google search for "pandas set index"?
>
> http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html
>
> On Fri, May 13, 2016 at 11:18 PM David Shi  wrote:
>
> Hello, Michael,
>
> I tried to discover the problem.
>
> df[0]   yields nothing
> df[1]  yields nothing
> df[2] yields nothing
>
> However, df[3] gives the following:
>
> sid
> -9223372036854775808  NaN
>  1  133738.70
>  4  295256.11
>  5  137733.09
>  6  409413.58
>  8  269600.97
>  9   12852.94
>
>
> Can we split this back to normal?  or turn it into a dictionary, so that I 
> can put values back properly.
>
>
> I like to use sid as index, some way.
>
>
> Regards.
>
>
> David
>
>
>
> On Friday, 13 May 2016, 22:58, Michael Selik 
> wrote:
>
>
> What have code you tried? What error message are you receiving?
>
> On Fri, May 13, 2016, 5:54 PM David Shi  wrote:
>
> Hello, Michael,
>
> How to convert a float type column into an integer or label or string type?
>
>
> On Friday, 13 May 2016, 22:02, Michael Selik 
> wrote:
>
>
> To clarify that you're specifying the index as a label, use df.iloc
>
> >>> df = pd.DataFrame({'X': range(4)}, index=list('abcd'))
> >>> df
>X
> a  0
> b  1
> c  2
> d  3
> >>> df.loc['a']
> X0
> Name: a, dtype: int64
> >>> df.iloc[0]
> X0
> Name: a, dtype: int64
>
> On Fri, May 13, 2016 at 4:54 PM David Shi  wrote:
>
> Dear Michael,
>
> To avoid complication, I only groupby using one column.
>
> It is OK now.  But, how to refer to new row index?  How do I use floating
> index?
>
> Float64Index([ 1.0,  4.0,  5.0,  6.0,  8.0,  9.0, 10.0, 11.0, 12.0, 13.0, 
> 16.0,
>   17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 
> 27.0,
>   28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 
> 38.0,
>   39.0, 40.0, 41.0, 42.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 
> 50.0,
>   51.0, 53.0, 54.0, 55.0, 56.0],
>  dtype='float64', name=u'StateFIPS')
>
>
> Regards.
>
>
> David
>
>
>
> On Friday, 13 May 2016, 21:43, Michael Selik 
> wrote:
>
>
> Here's an example.
>
> >>> import pandas as pd
> >>> df = pd.DataFrame({'group': list('AB') * 2, 'data': range(4)},
> index=list('wxyz'))
> >>> df
>data group
> w 0 A
> x 1 B
> y 2 A
> z 3 B
> >>> df = df.reset_index()
> >>> df
>   index  data group
> 0 w 0 A
> 1 x 1 B
> 2 y 2 A
> 3 z 3 B
> >>> df.groupby('group').max()
>   index  data
> group
> A y 2
> B z 3
>
> If that doesn't help, you'll need to explain what you're trying to
> accomplish in detail -- what variables you started with, what
> transformations you want to do, and what variables you hope to have when
> finished.
>
> On Fri, May 13, 2016 at 4:36 PM David Shi  wrote:
>
> Hello, Michael,
>
> I changed groupby with one column.
>
> The index is different.
>
> Index([   u'AL',u'AR',u'AZ',u'CA',u'CO',u'CT',u'DC',
>   u'DE',u'FL',u'GA

Re: How to put back a number-based index

2016-05-14 Thread Michael Selik
You might also be interested in "Python for Data Analysis" for a thorough
discussion of Pandas.
http://shop.oreilly.com/product/0636920023784.do

On Sat, May 14, 2016 at 10:29 AM Michael Selik 
wrote:

> David, it sounds like you'll need a thorough introduction to the basics of
> Python.
> Check out the tutorial: https://docs.python.org/3/tutorial/
>
> On Sat, May 14, 2016 at 6:19 AM David Shi  wrote:
>
>> Hello, Michael,
>>
>> I discovered that the problem is "two columns of data are put together"
>> and "are recognised as one column".
>>
>> This is very strange.  I would like to understand the subject well.
>>
>> And, how many ways are there to investigate into the nature of objects
>> dynamically?
>>
>> Some object types only get shown as an object.  Are there anything to be
>> typed in Python, to reveal objects.
>>
>> Regards.
>>
>> David
>>
>>
>> On Saturday, 14 May 2016, 4:30, Michael Selik 
>> wrote:
>>
>>
>> What were you hoping to get from ``df[0]``?
>> When you say it "yields nothing" do you mean it raised an error? What was
>> the error message?
>>
>> Have you tried a Google search for "pandas set index"?
>>
>> http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html
>>
>> On Fri, May 13, 2016 at 11:18 PM David Shi  wrote:
>>
>> Hello, Michael,
>>
>> I tried to discover the problem.
>>
>> df[0]   yields nothing
>> df[1]  yields nothing
>> df[2] yields nothing
>>
>> However, df[3] gives the following:
>>
>> sid
>> -9223372036854775808  NaN
>>  1  133738.70
>>  4  295256.11
>>  5  137733.09
>>  6  409413.58
>>  8  269600.97
>>  9   12852.94
>>
>>
>> Can we split this back to normal?  or turn it into a dictionary, so that I 
>> can put values back properly.
>>
>>
>> I like to use sid as index, some way.
>>
>>
>> Regards.
>>
>>
>> David
>>
>>
>>
>> On Friday, 13 May 2016, 22:58, Michael Selik 
>> wrote:
>>
>>
>> What have code you tried? What error message are you receiving?
>>
>> On Fri, May 13, 2016, 5:54 PM David Shi  wrote:
>>
>> Hello, Michael,
>>
>> How to convert a float type column into an integer or label or string
>> type?
>>
>>
>> On Friday, 13 May 2016, 22:02, Michael Selik 
>> wrote:
>>
>>
>> To clarify that you're specifying the index as a label, use df.iloc
>>
>> >>> df = pd.DataFrame({'X': range(4)}, index=list('abcd'))
>> >>> df
>>X
>> a  0
>> b  1
>> c  2
>> d  3
>> >>> df.loc['a']
>> X0
>> Name: a, dtype: int64
>> >>> df.iloc[0]
>> X0
>> Name: a, dtype: int64
>>
>> On Fri, May 13, 2016 at 4:54 PM David Shi  wrote:
>>
>> Dear Michael,
>>
>> To avoid complication, I only groupby using one column.
>>
>> It is OK now.  But, how to refer to new row index?  How do I use floating
>> index?
>>
>> Float64Index([ 1.0,  4.0,  5.0,  6.0,  8.0,  9.0, 10.0, 11.0, 12.0, 13.0, 
>> 16.0,
>>   17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 
>> 27.0,
>>   28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 
>> 38.0,
>>   39.0, 40.0, 41.0, 42.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 
>> 50.0,
>>   51.0, 53.0, 54.0, 55.0, 56.0],
>>  dtype='float64', name=u'StateFIPS')
>>
>>
>> Regards.
>>
>>
>> David
>>
>>
>>
>> On Friday, 13 May 2016, 21:43, Michael Selik 
>> wrote:
>>
>>
>> Here's an example.
>>
>> >>> import pandas as pd
>> >>> df = pd.DataFrame({'group': list('AB') * 2, 'data': range(4)},
>> index=list('wxyz'))
>> >>> df
>>data group
>> w 0 A
>> x 1 B
>> y 2 A
>> z 3 B
>> >>> df = df.reset_index()
>> >>> df
>>   index  data group
>> 0 w 0 A
>> 1 x 1 B
>> 2 

Re: Why online forums have bad behaviour

2016-05-14 Thread Michael Selik
On Sat, May 14, 2016 at 8:57 AM Ben Finney 
wrote:

>   If you dislike someone's behaviour, consider that they may not have a
>   well-thought-out or coherent rason for it; and, if pressed to come up
>   with a reason, we will employ all our faculties to *make up* a reason
>   (typically without being aware that's what we're doing!) that somehow
>   depicts us in a better light than others.
>

Since this was an assertion, I thought it'd be helpful to cite some
evidence. There's some very interesting studies of split-brain folks. If
the corpus callosum is severed, the right and left hemispheres cannot
communicate, allowing a researcher to talk to each hemisphere separately.
An action determined by the right hemisphere will be observed by the left.
If then asked why the action was taken, the left hemisphere will make
something up, completely unaware that it was not the one who thought of the
action.

I couldn't find a link to the research I'm referencing, but this Wikipedia
article is getting at a similar topic (
https://en.wikipedia.org/wiki/Left_brain_interpreter) and one could
probably chase down the original research via its citations.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to get a directory list sorted by date?

2016-05-15 Thread Michael Selik
On Sun, May 15, 2016, 10:37 AM Grant Edwards 
wrote:

> On 2016-05-15, Tim Chase  wrote:
> > On 2016-05-15 11:46, Peter Otten wrote:
> >> def sorted_dir(folder):
> >> def getmtime(name):
> >> path = os.path.join(folder, name)
> >> return os.path.getmtime(path)
> >>
> >> return sorted(os.listdir(folder), key=getmtime, reverse=True)
> >>
> >> The same idea will work with pathlib and os.scandir():
> >>
> >> def _getmtime(entry):
> >> return entry.stat().st_mtime
> >>
> >> def sd_sorted_dir(folder):
> >> return sorted(os.scandir(folder), key=_getmtime, reverse=True)
> >
> > unless sorted() returns a lazy sorter,
>
> What's a lazy sorter?
>

One that doesn't calculate the next item in the sequence until you ask for
it. It's impossible unless you don't mind an approximation rather than
correct sort.

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


Re: Pandas GroupBy does not behave consistently

2016-05-15 Thread Michael Selik
On Sun, May 15, 2016 at 7:07 AM David Shi  wrote:

> Hello, Michael,
>
> Pandas GroupBy does not behave consistently.
>
> Last time, when we had conversation, I used grouby.  It works well.
>
> Now, I thought to re-write the program, so that I can end up with a clean
> script.
>
> But, the problem is that a lot of columns are missing after groupby
> application.
>
> Any idea?
>

I'd guess that the columns lost after a groupby had an inappropriate
datatype for the operation you were doing. If you'd like a more thorough
response, you'll need to post the code you tried and the results you got.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to create development Python environment on Linux.

2016-05-16 Thread Michael Selik
On Mon, May 16, 2016 at 5:31 PM  wrote:

> After considering your guidance I think what I will do is install
> virtualenv using apt-get and then use that to create a dev environment. Is
> it ok to run get-pip.py in a virtual environment?
>

Recent versions of the virtualenv application create virtual environments
that already have pip. It's the default setting last time I checked.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Design: Idiom for classes and methods that are customizable by the user?

2016-05-17 Thread Michael Selik
On Tue, May 17, 2016 at 11:18 AM Dirk Bächle  wrote:

>
> > It's not so great to require
> > that the user must explicitly ``add`` their derived class after defining
> > it. Perhaps that add function could be a decorator?
>
> Our current API doesn't use decorators at all, since it's also aimed at
> people with no (or only some) knowledge of Python.


I think you'll find that users of decorators do not need any special
knowledge. The Flask framework (http://flask.pocoo.org/) is proof of that.


> decorators...would have looked strange to the average user perhaps?
>

Many beginners are willing to mimic your example code without questioning
what that "@" thingy is doing above the function or class.

Compare that with trying to explain to a C++ user that in Python classes
are actually objects and you can pass them to the ``add`` function to
register them... that's going to take a while.

We're now free to add them into the mix for our new Python 2.7.x/3.y
> codebase, but this would probably require an API discussion
> first.
>

An alternative would be creating a TaskmasterMeta metaclass that registers
the Taskmaster subclass when it's defined. I'm a little reluctant to
recommend that one. I find decorators to be more elegant.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: for / while else doesn't make sense

2016-05-20 Thread Michael Selik
On Thu, May 19, 2016 at 1:04 PM Ian Kelly  wrote:

> On Thu, May 19, 2016 at 10:31 AM, Herkermer Sherwood 
> wrote:
> > Most keywords in Python make linguistic sense, but using "else" in for
> and
> > while structures is kludgy and misleading. I am under the assumption that
> > this was just utilizing an already existing keyword. Adding another like
> > "andthen" would not be good.
>
> "else" makes sense from a certain point of view, but I think that
> logic may not be communicated well. At the start of each loop
> iteration, the loop construct makes a test for whether the loop should
> continue or not. If that test ever fails (i.e. if the condition of the
> while loop is false), the else block is executed instead. So you can
> think of it as a repeated if-else where the else block has the
> additional effect of exiting the loop.
>
> > But there is already a reserved keyword that would work great here.
> > "finally". It is already a known keyword used in try blocks, but would
> work
> > perfectly here. Best of all, it would actually make sense.
> >
> > Unfortunately, it wouldn't follow the semantics of
> try/except/else/finally.
>
> "finally" in exception handling denotes a block that is *always*
> executed. Using it for a block that is only sometimes executed would
> dilute that meaning.
>

It's unfortunate that so many people responded so quickly, since Ian's
explanation was so clear (I thought). For further clarity, I'll write out
the implicit if-statement that Ian described, though in my case it'd be at
the end of the block, somewhat like a do-while:

IF keep_looping()
THEN GOTO LOOP_START
ELSE GOTO LOOP_COMPLETED

Also, Nick Coghlan has a good post about this (
http://python-notes.curiousefficiency.org/en/latest/python_concepts/break_else.html
)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: for / while else doesn't make sense

2016-05-21 Thread Michael Selik
On Sat, May 21, 2016, 4:52 PM Erik  wrote:

> So I guess my question is perhaps whether Python compilers should start
> to go down the same path that C compilers did 30 years ago (by starting
> to include some linter functionality)
>

Well, there's that whole optional type hints thing. You should be asking
how to preserve Python's simplicity while adding more static analysis
features.

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


Re: numpy problem

2016-05-23 Thread Michael Selik
On Mon, May 23, 2016 at 9:12 AM  wrote:

> > On 23 mei 2016, at 14:19, Peter Otten <__pete...@web.de> wrote:
> > li...@onemanifest.net wrote:
> >
> >> I've got a 2D array
> >> And an array of indexes that for shows which row to keep for each column
> >> of values:
> >>
> >> keep = np.array([2, 3, 1, 9, 2])
> >>
> >> So, the result should be an array like array([ values[2,0], values[3,1],
> >> values[1,2], values[9,3], values[2,4] ]) == np.array([92, 62, 38, 81,
> 44])
> >>
> > values[keep].diagonal()
>
> That seems to do the trick!
>

To clarify, the fancy index is selecting all rows you want to keep, which
gives you a 5x5 array (repeating rows if necessary). Then you are getting
the diagonal of that selection.

If that was already clear, my apologies for the noise.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Q] ImportError by __import__() on Python >= 3.4

2016-06-02 Thread Michael Selik
On Thu, Jun 2, 2016 at 10:06 AM Makoto Kuwata  wrote:

> os.mkdir(name)
> with open(name + "/__init__.py", 'w') as f:
> f.write("X=1")
> f.flush()
>
> Please give me any advices or hints.
>

This wasn't your question, but you don't need to flush the file. The
``with`` statement will automatically flush and close your file for you
after you exit the block.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: 2d matrix into Nx3 column data

2016-06-02 Thread Michael Selik
On Thu, Jun 2, 2016 at 4:57 AM Anup reni  wrote:

> i would like to transform this:
>
>   -1 0-0.8  0.64 -0.36-0.4  0.16 -0.84
>  0.0 0 -1.00
>  0.4  0.16 -0.84
>  0.8  0.64 -0.36
>
> to something like this:
>
>  x  y  result
> id1  -0.8 -10.642  -0.8  0   -0.363  -0.4 -1
> 0.164  -0.4  0   -0.845   0.0 -10.006   0.0  0   -1.007   0.4 -1
>  0.168   0.4  0   -0.849   0.8 -10.6410  0.8  0   -0.36
>

It's hard to read your example. Could you write it out more simply? Perhaps
with comma separators instead of spaces? It might help to also give some
context around why you want to make the transformation.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Multiple inheritance, super() and changing signature

2016-06-02 Thread Michael Selik
On Thu, Jun 2, 2016 at 4:26 AM Lawrence D’Oliveiro 
wrote:

> On Wednesday, June 1, 2016 at 8:02:14 AM UTC+12, Ben Finney wrote:
> > (Note that ‘__init__’ is not a constructor, because it operates on the
> > *already constructed* instance, and does not return anything.
>
> Believe it or not, that *is* what “constructor” means in every OO
> language. Technically it should be called the “initializer”, but
> “constructor” is the accepted term for the special method that is called to
> initialize a newly-allocated class instance.
>

Perhaps a Pythonista may have different jargon? Since we have two different
words (initializer, constructor), we may as well give them different
meanings so that they are maximally useful in conversation.
-- 
https://mail.python.org/mailman/listinfo/python-list


  1   2   >