Re: [Tutor] decorators in a class

2017-06-13 Thread Peter Otten
anish singh wrote:

> Trying to use decorators in my class. I am calling
> build_tree from the main function and i want to increment
> the arguments by a constant factor by using decorators.
> However as build_tree is a recursive function, I don't want
> to call it recursively with increased constant factor always.
> 
> Probably decorators are not the ideal way for this but still
> how to go about it using decorators.
> 
> Simple solution would be to just pass the parameters after
> incrementing with constant values in the main function.
> However, I want the caller of build_tree to not know that
> internally we increment the indexes in the class and work
> on that.
> I can also call a intermediate function and then call build_tree
> but then would that be the right way?

Your decorator is so specific that it will probably be used only once, and 
it interferes with your intention to call both the decorated and the 
undecorated version of the method.

These are strong indications that decorators are not the right tool in this 
case. Instead I suggest that you use two methods:

class SegmentTree:
def build_tree(self, left, right, root):
n = self.n
return self._build_tree(left + n, right + n, root)

def _build_tree(self, left, right, root):
# recursive calls invoke _build_tree, not build_tree
...

If you insist you can probably write that

class SegmentTree:
def _build_tree(self, left, right, root):
# recursive calls invoke _build_tree, not build_tree
...

build_tree = p_decorate(_build_tree)

> def pow_of_2(n):
> n -= 1
> n |= n >> 1
> n |= n >> 2
> n |= n >> 4
> n |= n >> 8
> n |= n >> 16
> n += 1
> return n
> 
> def p_decorate(func):
> def func_wrapper(self, left, right, root):
> return func(self, left+self.n, right+self.n, root)
> return func_wrapper
> 
> class segment_tree(object):
> def __init__(self, data):
> self.n = pow_of_2(len(data))
> self.tree = [0]*self.n + data + [0]*(self.n - len(data))
> 
> @p_decorate
> def build_tree(self, left, right, root):
> if left == right:
> return self.tree[left]
> #below build_tree should not use decorated function,
> #how to achieve that?
> s = self.build_tree(left, (left+right)/2, 2*root) +
>   self.build_tree(1+(left+right)/2, right, 2*root+1)
> self.tree[root] = s
> 
> def __repr__(self):
> return " ".join(str(i) for i in self.tree)
> 
> data = [1, 2, 3, 4]
> sg = segment_tree(data)
> sg.build_tree(0, 7, 1)
> print(sg)
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Query

2017-06-13 Thread Muddunuri Mahesh
Where can i get the perfect tutorials for black scripting using python
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query

2017-06-13 Thread Alan Gauld via Tutor
On 13/06/17 10:09, Muddunuri Mahesh wrote:
> Where can i get the perfect tutorials for black scripting using python

I'm not sure what you mean by black scripting - and
neither does google apparently... Other than that
it is a gothic style of typescript font...

But the perfect tutorial for anything does not exist
so you are going to have to be more specific about
what you want.

Can you already program in any language?
Can you already program in Python?
What do you want to achieve?
What kind of teaching style do you prefer - theory or hands-on?
What kind of OS do you use?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Raspberry pi 2 python help

2017-06-13 Thread DJ VIN Lom
Can i create a script to have my pi change to a certian directory
automaticlly after booting. I want it to be in a directory so when i ssh
into it its ready and i dont need to spend time to do it. As well i dont
want to carry a keyboard mouse and montor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Raspberry pi 2 python help

2017-06-13 Thread Francois Dion
"These are not the scripts you are looking for"

More seriously, you want to configure your shell. See the linux
documentation project, beginner's guide to bash, chapter 3 in particular:
http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html

Until you login, your shell doesn't even exists, so you cant preemptively
change it before the login. You can do it right at login time, through one
of the files mentioned that chapter.

Francois


On Tue, Jun 13, 2017 at 10:08 AM, DJ VIN Lom  wrote:

> Can i create a script to have my pi change to a certian directory
> automaticlly after booting. I want it to be in a directory so when i ssh
> into it its ready and i dont need to spend time to do it. As well i dont
> want to carry a keyboard mouse and montor
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
raspberry-python.blogspot.com - www.pyptug.org - www.3DFutureTech.info -
@f_dion
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Free Python Class on Coursera

2017-06-13 Thread leam hall
Hey everyone,

There's a free pair of Python classes on Coursera. I'm fixing to take the
second one "https://www.coursera.org/learn/program-code"; that starts 26
Jun. If you've been here for a while and learned the basics, now is a good
time to up your skills.

If you are new here the first of the series is available. The next one
starts 3 Jul and they run them a few times a year.

  https://www.coursera.org/learn/learn-to-program


I took the first class some time back and enjoyed it. if anyone wants to
join me on the second class, hop in! The class is free though Coursera
wants to ask you for money anyway.

Leam
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Raspberry pi 2 python help

2017-06-13 Thread Alan Gauld via Tutor
On 13/06/17 15:08, DJ VIN Lom wrote:
> Can i create a script to have my pi change to a certian directory
> automaticlly after booting. 

You have some serious misconceptions about how the Pi works.
The Pi does not have any idea of a "directory" when it boots
up. The whole concept of a home directory is related to
users. When a user logs in the shell sets a variable to
indicate that users home directory - you can have multiple
users logging into your Pi at once and they each have a
different home directory. But users log in after the Pi
has booted - and it could be seconds, minutes, hours
or days after before the first user logs in.

Having established that it's your login that needs to
be addressed we can look at how to set or change your
home directory. One way is in your user definition
in /etc/passwd. There is a field there that states
where the user goes when they login and you can edit
that.

Secondly when your shell starts up it executes various
startup scripts, depending on which shell you use.
Assuming its bash you can put commands into .bashrc
in your home directory, including a 'cd' to change to
wherever you want.

Finally, since you mention a Python script that
concerns you then you can put commands into the
script itself to change to any given directory
before doing anything else.

Use

import os
os.chdir(<"/your chosen/folder")

> I want it to be in a directory so when i ssh
> into it its ready and i dont need to spend time to do it. 

If it's about being in the right place at login you
probably should just change the login directory
in /etc/passwd. Although this begs the question
why you don't want to use the default /home/userid
directory? Maybe you should be creating links
or aliases to the files you need?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Raspberry pi 2 python help

2017-06-13 Thread Mats Wichmann
On 06/13/2017 08:08 AM, DJ VIN Lom wrote:
> Can i create a script to have my pi change to a certian directory
> automaticlly after booting. I want it to be in a directory so when i ssh
> into it its ready and i dont need to spend time to do it. As well i dont
> want to carry a keyboard mouse and montor
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 

There are a ton of ways to do this, and none of the common ones involve
python at all.



The wording "have my pi change to a directory" doesn't match how it
works... each new shell is a new context, so you need to set things upon
entering that context - namely after ssh establishes the connection and
the Pi launches a login shell in response.

Examples:

On the Pi, in your .bashrc, stick a cd command at the end
On the Pi, in your .bashrc define a short alias for the cd you want, so
what you type is very short
On the Pi, set the home directory of the account you are going to ssh
into to be the directory
On your host end, feed a command to ssh that includes doing something at
the other end, along this model:

ssh -t dj@pi 'cd /some/path && exec bash -l'

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Problem with Plot Legend

2017-06-13 Thread Stephen P. Molnar
I am using Python3.6 in the Spyder3IDE and have a problem with the 
legend for a plot.


I have attached the pg file.

The code (in part) is:
import numpy as np
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

.
.
.
(the portion of the code for Figure q1 has no problems)


def two_scales(ax1, time, data1, data2, c1, c2)

ax2 = ax1.twinx()

ax1.plot(time, data1, 'r')
ax1.set_xlabel("Distance ($\AA$)")
ax1.set_ylabel('Atom Charge',color='r')

ax2.plot(time, data2, 'b')
ax2.set_ylabel('Orbital Energy',color='b')
return ax1, ax2



t = data[:,0]
s1 = data[:,3]
s2 = data[:,5]

# Create axes
fig, ax = plt.subplots()
ax1, ax2 = two_scales(ax, t, s1, s2, 'r', 'b')


# Change color of each axis
def color_y_axis(ax, color):
"""Color your axes."""
for t in ax.get_yticklabels():
t.set_color(color)
return None
color_y_axis(ax1, 'r')
color_y_axis(ax2, 'b')

plt.title('Molecular Transforms')

patch_red = mpatches.Patch(color='red',label='Atom Charge')
patch_blue = mpatches.Patch(color='blue',label='Orbital Energy')
plt.legend(handles = [patch_red,patch_blue])

plt.draw()
plt.show()

name_plt = name+'-fig2.png'
fig.savefig(name_plt,bbox_inches='tight')

The problem is that the legend is where I want it in the figure and 
contains what I want with one exception:


The colored lines are too thick, the same width as the text.

I've look in the literature and Googled for the solution, but I'm 
beginning to think that I don't know just what the question that I 
should be asking, hence no results.


A pointer towards thech solution to this problem will be much appreciated.

Thanks in advance.
--
Stephen P. Molnar, Ph.D.
Consultant
www.molecular-modeling.net
(614)312-7528 (c)
Skype: smolnar1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor