Re: Minimising stack trace

2015-05-16 Thread Cecil Westerhof
Op Friday 15 May 2015 21:04 CEST schreef Ned Batchelder:

> On Friday, May 15, 2015 at 2:50:12 PM UTC-4, Cecil Westerhof wrote:
>> While playing with recursion I get:
>> RuntimeError: maximum recursion depth exceeded in comparison
>>
>> But then I get a very long stack trace. Is there a way to make this
>> a lot shorter. Now I 'lose' interesting information because of the
>> length of the stack trace.
>
> There isn't a way to shorten the stack trace.  If you are losing
> information at the top because of your terminal window, you can
> likely increase the number of lines it will keep for you.

Well, I am not really losing information, but it happens in a script
with some output. One of the things I output is information about how
deep I am going. If I then get a stack trace of a 1000 lines that is
not very helpful. Especially because except the first and last every
message is the same. What would be a lot more helpful would be
something like:
RuntimeError  Traceback (most recent call last)
/home/cecil/Python/mathDecebal.py in ()
355 for i in range(start, end + 1):
356 factorial_iter  = factorial_iterative(i)
--> 357 factorial_recur = factorial_recursive(i)
358 factorial_recur_old = factorial_recursive_old(i)
359 factorial_tail  = factorial_tail_recursion(i)

/home/cecil/Python/mathDecebal.py in factorial_recursive(x, y, z)
 51 if x < 2:
 52 return y
---> 53 return y if z > x else factorial_recursive(x, z * y, z + 1)
 54 
 55 def factorial_recursive_old(x, y = 1):

Last call repeated 153 times

/home/cecil/Python/mathDecebal.py in factorial_recursive(x, y, z)
 48 
 49 def factorial_recursive(x, y = 1, z = 1):
---> 50 assert x >= 0
 51 if x < 2:
 52 return y

RuntimeError: maximum recursion depth exceeded in comparison

I would find that a lot clearer and I do not think you are losing
anything useful.


> Another option is to reduce the maximum stack depth, so that it
> is exceeded sooner, which produces shorter stack traces:
>
> import sys;
> sys.setrecursionlimit(50)

Well that would break my code. I just got the above problem with my
math functions. I had it tweaked for testing. But when running the
test in ipython3 it goes wrong. It looks like ipython3 has a smaller
stack, or puts more on the stack as ipython, python3 and python2.

Yes, that is correct. When running:
python3 mathDecebal.py
there is no problem, but when running:
ipython3 mathDecebal.py
I get a stack overflow.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Building CPython

2015-05-16 Thread Marko Rauhamaa
BartC :

> I suppose in many cases an object will have no attributes of its own,
> and so it can rapidly bypass the first lookup.

Almost all objects have quite many instance attributes. That's what
tells objects apart.

> I don't understand the need for an object creation (to represent A.B
> so that it can call it?) but perhaps such an object can already exist,
> prepared ready for use.

Note that almost identical semantics could be achieved without a class.
Thus, these two constructs are almost identical:

class C:
def __init__(self, x):
self.x = x

def square(self):
return self.x * self.x

def cube(self):
return self.x * self.square()

##

class O: pass

def C(x):
o = O()

def square():
return x * x

def cube():
return x * square()

o.square = square
o.cube = cube
return o


IOW, the class is a virtually superfluous concept in Python. Python has
gotten it probably without much thought (other languages at the time had
it). I comes with advantages and disadvantages:

 + improves readability

 + makes objects slightly smaller

 + makes object instantiation slightly faster

 - goes against the grain of ducktyping

 - makes method calls slower

 - makes method call semantics a bit tricky


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


Re: Building CPython

2015-05-16 Thread Steven D'Aprano
On Sat, 16 May 2015 06:08 pm, Marko Rauhamaa wrote:

> Note that almost identical semantics could be achieved without a class.
> Thus, these two constructs are almost identical:
[...]

> IOW, the class is a virtually superfluous concept in Python. Python has
> gotten it probably without much thought (other languages at the time had
> it). I comes with advantages and disadvantages:

Your example is effectively just a way of using closures instead of a class
instance.

Almost anything you can do with classes, you can do with closures. The big
advantage of classes over closures is that you have an interface to access
arbitrary class attributes, while you would need a separate closure for
each and every attribute you want access to.

For example, here is sketch:

class K:
def method(self, arg):
return self.spam + arg


k = K()
the_method = k.method  # bound method


as a closure becomes:


def make_closure(instance):  # instance is equivalent to self above
def method(arg):
return instance.spam + arg
return method

the_method = make_closure(obj)  # Some object with a spam field.


The big advantage of a closure is that you have much more strict
encapsulation. The big disadvantage of a closure is that you have much more
strict encapsulation.


>  + improves readability

I wouldn't say that.


>  + makes objects slightly smaller
> 
>  + makes object instantiation slightly faster

Are you sure? Have you actually benchmarked this?

 
>  - goes against the grain of ducktyping
> 
>  - makes method calls slower
> 
>  - makes method call semantics a bit tricky


A couple more negatives:


- no such thing as inheritance;

- "is-a" relationship tests don't work;

- an unfamiliar idiom for most people;



Also, at least with Python's implementation, a couple of mixed blessings:

± closures are closed against modification;

± internals of the closure are strictly private;




-- 
Steven

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


Fastest way to remove the first x characters from a very long string

2015-05-16 Thread bruceg113355
I have a string that contains 10 million characters.

The string is formatted as:

"001 : some hexadecimal text ... \n
002 : some hexadecimal text ... \n
003 : some hexadecimal text ... \n
...
010 : some hexadecimal text ... \n
011 : some hexadecimal text ... \n"

and I need the string to look like:

"some hexadecimal text ... \n
some hexadecimal text ... \n
some hexadecimal text ... \n
...
some hexadecimal text ... \n
some hexadecimal text ... \n"

I can split the string at the ":" then iterate through the list removing the 
first 8 characters then convert back to a string. This method works, but it 
takes too long to execute.

Any tricks to remove the first n characters of each line in a string faster?

Thanks,
Bruce
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread Joel Goldstick
On Sat, May 16, 2015 at 9:28 AM,   wrote:
> I have a string that contains 10 million characters.
>
> The string is formatted as:
>
> "001 : some hexadecimal text ... \n
> 002 : some hexadecimal text ... \n
> 003 : some hexadecimal text ... \n
> ...
> 010 : some hexadecimal text ... \n
> 011 : some hexadecimal text ... \n"
>
> and I need the string to look like:
>
> "some hexadecimal text ... \n
> some hexadecimal text ... \n
> some hexadecimal text ... \n
> ...
> some hexadecimal text ... \n
> some hexadecimal text ... \n"
>
> I can split the string at the ":" then iterate through the list removing the 
> first 8 characters then convert back to a string. This method works, but it 
> takes too long to execute.
>
> Any tricks to remove the first n characters of each line in a string faster?
>
slicing might be faster than searching for :

Do you need to do this all at once?  If not, use a generator

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



-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread Chris Angelico
On Sat, May 16, 2015 at 11:28 PM,   wrote:
> I have a string that contains 10 million characters.
>
> The string is formatted as:
>
> "001 : some hexadecimal text ... \n
> 002 : some hexadecimal text ... \n
> 003 : some hexadecimal text ... \n
> ...
> 010 : some hexadecimal text ... \n
> 011 : some hexadecimal text ... \n"
>
> and I need the string to look like:
>
> "some hexadecimal text ... \n
> some hexadecimal text ... \n
> some hexadecimal text ... \n
> ...
> some hexadecimal text ... \n
> some hexadecimal text ... \n"
>
> I can split the string at the ":" then iterate through the list removing the 
> first 8 characters then convert back to a string. This method works, but it 
> takes too long to execute.
>
> Any tricks to remove the first n characters of each line in a string faster?

Given that your definition is "each line", what I'd advise is first
splitting the string into lines, then changing each line, and then
rejoining them into a single string.

lines = original_text.split("\n")
new_text = "\n".join(line[8:] for line in lines)

Would that work?

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


Re: Building CPython

2015-05-16 Thread Marko Rauhamaa
Steven D'Aprano :

> A couple more negatives:
>
> - no such thing as inheritance;

Untrue. My simple Scheme object system (125 lines incl. documentation)
supports multiple inheritance without classes. Maybe I should port that
to Python...

> - "is-a" relationship tests don't work;

>From the ducktyping point of view, that is an advantage. The whole
Linnaean categorization of objects is unnecessary ontological chaff. How
many times have people here had to advise newcomers not to inspect type
and instance relations of objects and just call the method?

> - an unfamiliar idiom for most people;

That's impossible to ascertain objectively. Java and JavaScript
programmers (of all people!) routinely deal with closures.


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


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread bruceg113355
On Saturday, May 16, 2015 at 9:46:17 AM UTC-4, Chris Angelico wrote:
> On Sat, May 16, 2015 at 11:28 PM,   wrote:
> > I have a string that contains 10 million characters.
> >
> > The string is formatted as:
> >
> > "001 : some hexadecimal text ... \n
> > 002 : some hexadecimal text ... \n
> > 003 : some hexadecimal text ... \n
> > ...
> > 010 : some hexadecimal text ... \n
> > 011 : some hexadecimal text ... \n"
> >
> > and I need the string to look like:
> >
> > "some hexadecimal text ... \n
> > some hexadecimal text ... \n
> > some hexadecimal text ... \n
> > ...
> > some hexadecimal text ... \n
> > some hexadecimal text ... \n"
> >
> > I can split the string at the ":" then iterate through the list removing 
> > the first 8 characters then convert back to a string. This method works, 
> > but it takes too long to execute.
> >
> > Any tricks to remove the first n characters of each line in a string faster?
> 
> Given that your definition is "each line", what I'd advise is first
> splitting the string into lines, then changing each line, and then
> rejoining them into a single string.
> 
> lines = original_text.split("\n")
> new_text = "\n".join(line[8:] for line in lines)
> 
> Would that work?
> 
> ChrisA


Hi Chris,

I meant to say I can split the string at the \n.

Your approach using .join is what I was looking for.
Thank you,

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


Re: Minimising stack trace

2015-05-16 Thread Cecil Westerhof
Op Friday 15 May 2015 20:17 CEST schreef Cecil Westerhof:

> While playing with recursion I get:
> RuntimeError: maximum recursion depth exceeded in comparison
>
> But then I get a very long stack trace. Is there a way to make this
> a lot shorter. Now I ‘lose’ interesting information because of the
> length of the stack trace.

I found something. I could use:
import sys

sys.tracebacklimit = 10

The output is different, but I find it useful enough.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
-- 
https://mail.python.org/mailman/listinfo/python-list


EuroPython 2015: Come with your partners

2015-05-16 Thread M.-A. Lemburg
We are happy to announce the official EuroPython Partner Program for
EuroPython 2015 in Bilbao:

  *** EuroPython 2015 Partner Program ***

  https://ep2015.europython.eu/en/events/partner-program/

There is plenty to see in and around Bilbao. We have worked out a
set of interesting tours, together with a local tour company to choose
from, for partners and EuroPython attendees alike:

 * Panoramic tour of Bilbao
 * Excursion to San Sebastian
 * Guggenheim Museum
 * Excursion to La Rioja
 * Boat tour of Bilbao

The tours include travel, lunch and tickets (where needed).

We’d like to encourage early sign-up, since seats are limited.

Registration deadline is June 20th.

Enjoy,
--
EuroPython 2015 Team
http://ep2015.europython.eu/
http://www.europython-society.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread Grant Edwards
On 2015-05-16, bruceg113...@gmail.com  wrote:

> I have a string that contains 10 million characters.
>
> The string is formatted as:
>
> "001 : some hexadecimal text ... \n
> 002 : some hexadecimal text ... \n
> 003 : some hexadecimal text ... \n
> ...
> 010 : some hexadecimal text ... \n
> 011 : some hexadecimal text ... \n"
>
> and I need the string to look like:
>
> "some hexadecimal text ... \n
> some hexadecimal text ... \n
> some hexadecimal text ... \n
> ...
> some hexadecimal text ... \n
> some hexadecimal text ... \n"
>
> I can split the string at the ":" then iterate through the list
> removing the first 8 characters then convert back to a string. This
> method works, but it takes too long to execute.
>
> Any tricks to remove the first n characters of each line in a string faster?

Well, if the strings are all in a file, I'd probably just use sed:

$ sed 's/^//g' file1.txt >file2.txt

or

$ sed 's/^.*://g' file1.txt >file2.txt
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread Rustom Mody
On Saturday, May 16, 2015 at 8:30:02 PM UTC+5:30, Grant Edwards wrote:
> On 2015-05-16, bruceg113355 wrote:
> 
> > I have a string that contains 10 million characters.
> >
> > The string is formatted as:
> >
> > "001 : some hexadecimal text ... \n
> > 002 : some hexadecimal text ... \n
> > 003 : some hexadecimal text ... \n
> > ...
> > 010 : some hexadecimal text ... \n
> > 011 : some hexadecimal text ... \n"
> >
> > and I need the string to look like:
> >
> > "some hexadecimal text ... \n
> > some hexadecimal text ... \n
> > some hexadecimal text ... \n
> > ...
> > some hexadecimal text ... \n
> > some hexadecimal text ... \n"
> >
> > I can split the string at the ":" then iterate through the list
> > removing the first 8 characters then convert back to a string. This
> > method works, but it takes too long to execute.
> >
> > Any tricks to remove the first n characters of each line in a string faster?
> 
> Well, if the strings are all in a file, I'd probably just use sed:
> 
> $ sed 's/^//g' file1.txt >file2.txt
> 
> or
> 
> $ sed 's/^.*://g' file1.txt >file2.txt


And if they are not in a file you could start by putting them (it) there :-)

Seriously... How does your 'string' come into existence?
How/when do you get hold of it?
-- 
https://mail.python.org/mailman/listinfo/python-list


Secret code in Ex Machina

2015-05-16 Thread Seymore4Head
http://www.reddit.com/r/movies/comments/365f9b/secret_code_in_ex_machina/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread bruceg113355
On Saturday, May 16, 2015 at 10:06:31 AM UTC-4, Stefan Ram wrote:
> bruceg113...@gmail.com writes:
> >Your approach using .join is what I was looking for.
> 
>   I'd appreciate a report of your measurements.

# Original Approach
# -
ss = ss.split("\n")
ss1 = ""
for sdata in ss:
ss1 = ss1 + (sdata[OFFSET:] + "\n")


# Chris's Approach
# 
lines = ss.split("\n")
new_text = "\n".join(line[8:] for line in lines)  


Test #1, Number of Characters: 165110
Original Approach: 18ms
Chris's Approach:   1ms

Test #2, Number of Characters: 470763
Original Approach: 593ms
Chris's Approach:   16ms

Test #3, Number of Characters: 944702
Original Approach: 2.824s
Chris's Approach:47ms

Test #4, Number of Characters: 5557394
Original Approach: 122s
Chris's Approach:   394ms
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread bruceg113355
On Saturday, May 16, 2015 at 11:13:45 AM UTC-4, Rustom Mody wrote:
> On Saturday, May 16, 2015 at 8:30:02 PM UTC+5:30, Grant Edwards wrote:
> > On 2015-05-16, bruceg113355 wrote:
> > 
> > > I have a string that contains 10 million characters.
> > >
> > > The string is formatted as:
> > >
> > > "001 : some hexadecimal text ... \n
> > > 002 : some hexadecimal text ... \n
> > > 003 : some hexadecimal text ... \n
> > > ...
> > > 010 : some hexadecimal text ... \n
> > > 011 : some hexadecimal text ... \n"
> > >
> > > and I need the string to look like:
> > >
> > > "some hexadecimal text ... \n
> > > some hexadecimal text ... \n
> > > some hexadecimal text ... \n
> > > ...
> > > some hexadecimal text ... \n
> > > some hexadecimal text ... \n"
> > >
> > > I can split the string at the ":" then iterate through the list
> > > removing the first 8 characters then convert back to a string. This
> > > method works, but it takes too long to execute.
> > >
> > > Any tricks to remove the first n characters of each line in a string 
> > > faster?
> > 
> > Well, if the strings are all in a file, I'd probably just use sed:
> > 
> > $ sed 's/^//g' file1.txt >file2.txt
> > 
> > or
> > 
> > $ sed 's/^.*://g' file1.txt >file2.txt
> 
> 
> And if they are not in a file you could start by putting them (it) there :-)
> 
> Seriously... How does your 'string' come into existence?
> How/when do you get hold of it?

Data is coming from a wxPython TextCtrl widget.
The widget is displaying data received on a serial port for a user to analyze.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread Ian Kelly
On Sat, May 16, 2015 at 10:22 AM,   wrote:
> # Chris's Approach
> # 
> lines = ss.split("\n")
> new_text = "\n".join(line[8:] for line in lines)

Looks like the approach you have may be fast enough already, but I'd
wager the generator expression could be replaced with:

map(operator.itemgetter(slice(8, None)), lines)

for a modest speed-up. On the downside, this is less readable.
Substitute itertools.imap for map if using Python 2.x.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread Irmen de Jong
On 16-5-2015 18:24, bruceg113...@gmail.com wrote:
> Data is coming from a wxPython TextCtrl widget.

Hm, there should be a better source of the data before it ends up in the 
textctrl widget.

> The widget is displaying data received on a serial port for a user to analyze.

If this is read from a serial port, can't you process the data directly when it 
arrives?
This may give you the chance to simply operate on the line as soon as it 
arrives from
the port, before pasting it all in the textctrl



Irmen

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


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread Chris Angelico
On Sun, May 17, 2015 at 2:22 AM,   wrote:
> # Original Approach
> # -
> ss = ss.split("\n")
> ss1 = ""
> for sdata in ss:
> ss1 = ss1 + (sdata[OFFSET:] + "\n")
>
>
> # Chris's Approach
> # 
> lines = ss.split("\n")
> new_text = "\n".join(line[8:] for line in lines)

Ah, yep. This is exactly what str.join() exists for :) Though do make
sure the results are the same for each - there are two noteworthy
differences between these two. Your version has a customizable OFFSET,
where mine is hard-coded; I'm sure you know how to change that part.
The subtler one is that "\n".join(...) won't put a \n after the final
string - your version ends up adding one more newline. If that's
important to you, you'll have to add one explicitly. (I suspect
probably not, though; ss.split("\n") won't expect a final newline, so
you'll get a blank entry in the list if there is one, and then you'll
end up reinstating the newline when that blank gets joined in.) Just
remember to check correctness before performance, and you should be
safe.

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


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread bruceg113355
On Saturday, May 16, 2015 at 12:59:19 PM UTC-4, Chris Angelico wrote:
> On Sun, May 17, 2015 at 2:22 AM,   wrote:
> > # Original Approach
> > # -
> > ss = ss.split("\n")
> > ss1 = ""
> > for sdata in ss:
> > ss1 = ss1 + (sdata[OFFSET:] + "\n")
> >
> >
> > # Chris's Approach
> > # 
> > lines = ss.split("\n")
> > new_text = "\n".join(line[8:] for line in lines)
> 
> Ah, yep. This is exactly what str.join() exists for :) Though do make
> sure the results are the same for each - there are two noteworthy
> differences between these two. Your version has a customizable OFFSET,
> where mine is hard-coded; I'm sure you know how to change that part.
> The subtler one is that "\n".join(...) won't put a \n after the final
> string - your version ends up adding one more newline. If that's
> important to you, you'll have to add one explicitly. (I suspect
> probably not, though; ss.split("\n") won't expect a final newline, so
> you'll get a blank entry in the list if there is one, and then you'll
> end up reinstating the newline when that blank gets joined in.) Just
> remember to check correctness before performance, and you should be
> safe.
> 
> ChrisA

Hi Chris,

Your approach more than meets my requirements.
Data is formatted correctly and performance is simply amazing. 
OFFSET and \n are small details.

Thank you again,
Bruce

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


Re: Secret code in Ex Machina

2015-05-16 Thread David H. Lipman

From: "Seymore4Head" 


http://www.reddit.com/r/movies/comments/365f9b/secret_code_in_ex_machina/


LOL - It is like an Easter Egg in a movie.

C O O L  !

--
Dave
Multi-AV Scanning Tool - http://multi-av.thespykiller.co.uk
http://www.pctipp.ch/downloads/dl/35905.asp
--
https://mail.python.org/mailman/listinfo/python-list


Re: Building CPython

2015-05-16 Thread Steven D'Aprano
On Sat, 16 May 2015 11:59 pm, Marko Rauhamaa wrote:

> Steven D'Aprano :
> 
>> A couple more negatives:
>>
>> - no such thing as inheritance;
> 
> Untrue. My simple Scheme object system (125 lines incl. documentation)

Ah yes, I've seen Javascript code like that too. Each line is thirty
thousand characters long...

*wink*


> supports multiple inheritance without classes. Maybe I should port that
> to Python...

I'd like to see it, but somehow I don't think that your "Scheme object
system" is another name for "closures". We were talking about closures,
weren't we?

It sounds like you have implemented a form of prototype-based object
programming. The question of whether prototype-OOP has inheritance is an
interesting one. Clearly prototypes implement something *like* inheritance,
but it is based in delegation or copying. Delegation-based cloning is quite
close to class-based inheritance, but copying-based cloning is not.

My sense is that I prefer to say that prototypes don't have inheritance in
the same sense as classes, but they have something that plays the same role
as inheritance. But if you want to call it inheritance, I can't really
argue.



>> - "is-a" relationship tests don't work;
> 
> From the ducktyping point of view, that is an advantage. The whole
> Linnaean categorization of objects is unnecessary ontological chaff. How
> many times have people here had to advise newcomers not to inspect type
> and instance relations of objects and just call the method?

Um, is that a trick question? I don't remember the last time.



>> - an unfamiliar idiom for most people;
> 
> That's impossible to ascertain objectively. Java and JavaScript
> programmers (of all people!) routinely deal with closures.

I don't think so. I can't say for Javascript, but for Java, there's a lot of
confusion around closures, they've been described as "evil" with the
recommendation not to use them, and people cannot even agree when they were
introduced!

http://java.dzone.com/articles/whats-wrong-java-8-currying-vs
www.javaworld.com/javaworld/jw-06-2008/jw-06-closures.html


I mean, people had to *debate* the introduction of closures? There were
three competing proposals for them, plus an argument for "don't add them".
Some people say closures were added in Java 7, others say closures have
been there since the beginning, and James Gosling himself says that Java
used inner classes instead of closures but the result was painful...

It seems to me that in the Java community, there's a lot of confusion over
closures, which are seen as an advanced (and rather scary) feature. Hardly
routine.


-- 
Steven

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


Re: Building CPython

2015-05-16 Thread Marko Rauhamaa
Steven D'Aprano :

> On Sat, 16 May 2015 11:59 pm, Marko Rauhamaa wrote:
>> supports multiple inheritance without classes. Maybe I should port that
>> to Python...
>
> I'd like to see it, but somehow I don't think that your "Scheme object
> system" is another name for "closures". We were talking about closures,
> weren't we?

Ok, here's a quick port that have barely tried out:



### Simple OO Framework

class _O: pass

def make_object(*procedures, base=None, bases=None):
o = _O()
methods = {}
setattr(o, '%methods', methods)
if base is not None:
inherit_single(o, base)
elif bases is not None:
inherit_multi(o, bases)
for procedure in procedures:
methods[procedure.__name__] = procedure
setattr(o, procedure.__name__, procedure)
return o

def inherit_single(o, base):
methods = getattr(o, '%methods')
for name, method in getattr(base, '%methods').items():
methods[name] = method
setattr(o, name, method)

def inherit_multi(o, bases):
for base in bases:
inherit_single(o, base)

### Used as follows

def TCPClient():
def connect(socket_address):
...
return make_object(connect)

def SMTPClient():
tcp_client = TCPClient()
def connect(host):
tcp_client.connect((host, 25))
def send_message(message):
...
return make_object(send_message, base=tcp_client)

client = SMTPClient()
client.connect('mail.example.com')


> I mean, people had to *debate* the introduction of closures? There were
> three competing proposals for them, plus an argument for "don't add them".
> Some people say closures were added in Java 7, others say closures have
> been there since the beginning, and James Gosling himself says that Java
> used inner classes instead of closures but the result was painful...

I'm with those who say anonymous and named inner classes have been there
forever and serve the purpose of closures. Yes, Java's boilerplate
requirements are painful, but if you don't like that, use Python.

> It seems to me that in the Java community, there's a lot of confusion over
> closures, which are seen as an advanced (and rather scary) feature. Hardly
> routine.

Importantly, anonymous inner classes have been in active use by Java
newbs from day one.


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


Rule of order for dot operators?

2015-05-16 Thread C.D. Reimer

Greetings,

Noobie question regarding a single line of code that transforms a URL 
slug ("this-is-a-slug") into a title ("This Is A Slug").


title = slug.replace('-',' ').title()

This line also works if I switched the dot operators around.

title = slug.title().replace('-',' ')

I'm reading the first example as character replacement first and title 
capitalization second, and the second example as title capitalization 
first and character replacement second.


Does python perform the dot operators from left to right or according to 
a rule of order (i.e., multiplication/division before add/subtract)?


Thank you,

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


Re: Rule of order for dot operators?

2015-05-16 Thread Gary Herron

On 05/16/2015 12:20 PM, C.D. Reimer wrote:

Greetings,

Noobie question regarding a single line of code that transforms a URL 
slug ("this-is-a-slug") into a title ("This Is A Slug").


title = slug.replace('-',' ').title()

This line also works if I switched the dot operators around.

title = slug.title().replace('-',' ')

I'm reading the first example as character replacement first and title 
capitalization second, and the second example as title capitalization 
first and character replacement second.


Does python perform the dot operators from left to right or according 
to a rule of order (i.e., multiplication/division before add/subtract)?


Yes, that's correct.

Gary Herron





Thank you,

Chris Reimer



--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

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


Re: Rule of order for dot operators?

2015-05-16 Thread Joel Goldstick
On Sat, May 16, 2015 at 3:20 PM, C.D. Reimer  wrote:
> Greetings,
>
> Noobie question regarding a single line of code that transforms a URL slug
> ("this-is-a-slug") into a title ("This Is A Slug").
>
> title = slug.replace('-',' ').title()
>
> This line also works if I switched the dot operators around.
>
> title = slug.title().replace('-',' ')
>
> I'm reading the first example as character replacement first and title
> capitalization second, and the second example as title capitalization first
> and character replacement second.
>
> Does python perform the dot operators from left to right or according to a
> rule of order (i.e., multiplication/division before add/subtract)?
>
> Thank you,
>
> Chris Reimer
> --
> https://mail.python.org/mailman/listinfo/python-list

>From left to right.  So in your first example it replaces - with
space, then capitalizes each word.
In your second example, it does the caps first, then gets rid of the dashes

-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Rule of order for dot operators?

2015-05-16 Thread Peter Otten
C.D. Reimer wrote:

> Greetings,
> 
> Noobie question regarding a single line of code that transforms a URL
> slug ("this-is-a-slug") into a title ("This Is A Slug").
> 
> title = slug.replace('-',' ').title()
> 
> This line also works if I switched the dot operators around.
> 
> title = slug.title().replace('-',' ')
> 
> I'm reading the first example as character replacement first and title
> capitalization second, and the second example as title capitalization
> first and character replacement second.
> 
> Does python perform the dot operators from left to right or according to
> a rule of order (i.e., multiplication/division before add/subtract)?

You can find out yourself by using operations where the order does matter:

"Test".upper().lower()


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


Re: Rule of order for dot operators?

2015-05-16 Thread Thomas 'PointedEars' Lahn
C.D. Reimer wrote:

Who?

> Noobie

What?

> question regarding a single line of code that transforms a URL
> slug ("this-is-a-slug") into a title ("This Is A Slug").
> 
> title = slug.replace('-',' ').title()
> 
> This line also works if I switched the dot operators around.
> 
> title = slug.title().replace('-',' ')
> 
> I'm reading the first example as character replacement first and title
> capitalization second, and the second example as title capitalization
> first and character replacement second.

You are reading correctly.

> Does python perform the dot operators from left to right or according to
> a rule of order (i.e., multiplication/division before add/subtract)?

Yes.  If you debug the code, which you should have done before posting [1] , 
you will see that

  'this-is-a-slug'.title() == 'This-Is-A-Slug'

It follows that in this special case it does not matter if you call .title() 
before or after .replace().

However, for greater efficiency, in general you should call .replace() in 
such a way that the length of the string it operates on is minimized.  For 
example, if feasible, always slice *before* .replace().

[1] 
 
-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Rule of order for dot operators?

2015-05-16 Thread C.D. Reimer

On 5/16/2015 12:40 PM, Thomas 'PointedEars' Lahn wrote:
However, for greater efficiency, in general you should call .replace() 
in such a way that the length of the string it operates on is 
minimized. For example, if feasible, always slice *before* .replace().


Slice was how I got the slug from the URL in the first place. :)

Thank you,

Chris Reimer

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


Re: Rule of order for dot operators?

2015-05-16 Thread C.D. Reimer

On 5/16/2015 12:34 PM, Peter Otten wrote:
You can find out yourself by using operations where the order does 
matter: "Test".upper().lower() 


I was wondering about that and couldn't think of an example off the top 
of my head.


Thank you,

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


Re: Rule of order for dot operators?

2015-05-16 Thread Tim Chase
On 2015-05-16 12:20, C.D. Reimer wrote:
> Does python perform the dot operators from left to right or
> according to a rule of order (i.e., multiplication/division before
> add/subtract)?

Yes, Python evaluates dot-operators from left to right.

-tkc


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


Re: Building CPython

2015-05-16 Thread Marko Rauhamaa
Marko Rauhamaa :

> Ok, here's a quick port that have barely tried out:

And here's a more complete port (with some possible dunder abuse):


### Simple OO Framework

class _O: pass

def make_object(*procedures, base=None, bases=None):
o = _O()
methods = {}
o.__methods__ = methods
o.__derived__ = None
if base is not None:
_inherit_single(o, base)
elif bases is not None:
_inherit_multi(o, bases)
for procedure in procedures:
methods[procedure.__name__] = procedure
def method(*args, __procedure__=procedure, __dispatch__=True, **kwargs):
if not __dispatch__ or o.__derived__ is None:
return __procedure__(*args, **kwargs)
derived = o
while derived.__derived__ is not None:
derived = derived.__derived__
return getattr(derived, __procedure__.__name__)(*args, **kwargs)
setattr(o, procedure.__name__, method)
return o

def _inherit_single(o, base):
methods = o.__methods__
for name, method in base.__methods__.items():
methods[name] = method
setattr(o, name, method)

def _inherit_multi(o, bases):
for base in bases:
_inherit_single(o, base)

def delegate(method, *args, **kwargs):
return method(*args, __dispatch__=False, **kwargs)

### Used as follows

def TCPClient():
def connect(address):
pass
def shut_down():
pass
return make_object(connect, shut_down)

def SMTPClient():
tcp_client = TCPClient()
def connect(address):
delegate(tcp_client.connect, address)
do_stuff()
def send_message(message):
pass
return make_object(connect, send_message, base=tcp_client)

client = SMTPClient()
client.connect(None)



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


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread Cameron Simpson

On 16May2015 10:35, bruceg113...@gmail.com  wrote:

On Saturday, May 16, 2015 at 12:59:19 PM UTC-4, Chris Angelico wrote:

On Sun, May 17, 2015 at 2:22 AM,   wrote:
> # Original Approach
> # -
> ss = ss.split("\n")
> ss1 = ""
> for sdata in ss:
> ss1 = ss1 + (sdata[OFFSET:] + "\n")
>
> # Chris's Approach
> # 
> lines = ss.split("\n")
> new_text = "\n".join(line[8:] for line in lines)

[...]


Your approach more than meets my requirements.
Data is formatted correctly and performance is simply amazing.
OFFSET and \n are small details.


The only comment I'd make at this point is to consider if you really need a 
single string at the end. Keeping it as a list of lines may be more flexible.  
(It will consume more memory.) If you're doing more stuff with the string as 
lines then you'd need to re-split it, and so forth.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Fastest way to remove the first x characters from a very long string

2015-05-16 Thread Denis McMahon
On Sat, 16 May 2015 06:28:19 -0700, bruceg113355 wrote:

> I have a string that contains 10 million characters.
> 
> The string is formatted as:
> 
> "001 : some hexadecimal text ... \n 002 : some hexadecimal text
> ... \n 003 : some hexadecimal text ... \n ...
> 010 : some hexadecimal text ... \n 011 : some hexadecimal text
> ... \n"
> 
> and I need the string to look like:
> 
> "some hexadecimal text ... \n some hexadecimal text ... \n some
> hexadecimal text ... \n ...
> some hexadecimal text ... \n some hexadecimal text ... \n"

Looks to me as if you have a 10 Mbyte encoded file with line numbers as 
ascii text and you're trying to strip the line numbers before decoding 
the file.

Are you looking for a one-off solution, or do you have a lot of these 
files?

If you have a lot of files to process, you could try using something like 
sed.

sed -i.old 's/^\d+ : //' *.ext

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Rule of order for dot operators?

2015-05-16 Thread Steven D'Aprano
On Sun, 17 May 2015 05:20 am, C.D. Reimer wrote:

> Greetings,
> 
> Noobie question regarding a single line of code that transforms a URL
> slug ("this-is-a-slug") into a title ("This Is A Slug").
> 
> title = slug.replace('-',' ').title()
> 
> This line also works if I switched the dot operators around.

Technically, dot is not an operator, but even if it was, swapping the *dots*
around makes no difference:

slug.replace('-',' ').title()  => slug.replace('-',' ').title()

Because a dot is a dot, right? What you mean is that you're swapping the
*methods* around, not just the dots:

slug.replace('-',' ').title()  => slug.title().replace('-',' ')


> title = slug.title().replace('-',' ')
> 
> I'm reading the first example as character replacement first and title
> capitalization second, and the second example as title capitalization
> first and character replacement second.
> 
> Does python perform the dot operators from left to right or according to
> a rule of order (i.e., multiplication/division before add/subtract)?

Left to right.




-- 
Steven

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


Re: Rule of order for dot operators?

2015-05-16 Thread Steven D'Aprano
On Sun, 17 May 2015 05:40 am, Thomas 'PointedEars' Lahn wrote:

> C.D. Reimer wrote:
> 
> Who?

Don't be a dick, Thomas. Lots of people use their initials. You use your
nickname as part of your sender address, why are you questioning somebody
for using their initials?


>> Noobie
> 
> What?

Where? When? Why? How?

I'm pretty sure you've been on the Internet for long enough to know
what "noobie", "n00b", "newbie" etc mean.

But just in case you need help:

http://lmgtfy.com/?q=noobie



-- 
Steven

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


Re: How to deploy a custom common module?

2015-05-16 Thread Jason Friedman
> When I deploy test.py on another computer, I put (rsync) both test.py and 
> cmn_funcs.py in the same remote directory.
>
> If I create another python project (test2.py) in new directory, that needs 
> common functions, what should I do with cmn_funcs.py?

I put my shared code in a separate folder, named something like
/path/to/module_dir.

I then add to /etc/profile.d/something.sh:

export PYTHONPATH=$PYTHONPATH:/path/to/module_dir
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to deploy a custom common module?

2015-05-16 Thread Irmen de Jong
On 17-5-2015 4:06, Jason Friedman wrote:
>> When I deploy test.py on another computer, I put (rsync) both test.py and 
>> cmn_funcs.py in the same remote directory.
>>
>> If I create another python project (test2.py) in new directory, that needs 
>> common functions, what should I do with cmn_funcs.py?
> 
> I put my shared code in a separate folder, named something like
> /path/to/module_dir.
> 
> I then add to /etc/profile.d/something.sh:
> 
> export PYTHONPATH=$PYTHONPATH:/path/to/module_dir
> 


I think you could use the user site packages directory as well for this... then 
you
should not have to change the PYTHONPATH.

https://docs.python.org/3.5/library/site.html#site.USER_SITE

(if you don't want to or cannot install into to the system's site-packages 
directory)


Irmen

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


Re: Rule of order for dot operators?

2015-05-16 Thread Rustom Mody
On Sunday, May 17, 2015 at 7:15:13 AM UTC+5:30, Steven D'Aprano wrote:
> On Sun, 17 May 2015 05:40 am, Thomas 'PointedEars' Lahn wrote:
> 
> > C.D. Reimer wrote:
> > 
> > Who?
> 
> Don't be a dick, Thomas. Lots of people use their initials. You use your
> nickname as part of your sender address, why are you questioning somebody
> for using their initials?

Not only is it a ridiculous objection, I dont even understand what the objection
is.

Among the eminent this is quite common
Dijkstra -- ewd
Stallman -- rms
Eric Raymond -- esr

And even for non-eminent like your truly, most of my working life most people
called me 'rpm' than Rusi/Mody or any such.
-- 
https://mail.python.org/mailman/listinfo/python-list