Re: [Tutor] [PYTHON27] How to save into .npy file?

2017-04-13 Thread Allan Tanaka via Tutor
.npy is numpy array. Thanks it's ok i manage to save it..








On Tuesday, 11 April 2017, 8:23, Steven D'Aprano  wrote:




On Mon, Apr 10, 2017 at 02:10:34PM +, Allan Tanaka via Tutor wrote:

> Hi.

> Is there a way to save module type data into .npy file that can be used 
> latter?


What's "module type data"?


What's a .npy file?


To answer your question, literally, the answer is "Yes, of course. 

Python can save ANY data into a file with ANY file extention". But I 

guess that answer isn't helpful to you if you need to save specific data 

to a specific file format.


But without knowing what that specific data is, and the specific format, 

how can we answer?




-- 

Steve


___

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] python help

2017-04-13 Thread Christina Hammer
Hi,
I downloaded the newest version of Python on my windows computer and am
having some trouble using it. I need to save my work because I am using it
for an online class and am going to have to send it to my professor. But I
cannot access a tool bar that would allow me to save it. I'm not sure if
someone can help me with this.
Thank you
Christina Hammer
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] counting function calls

2017-04-13 Thread marcus lütolf
Dear experts, Mats
I have found the solution, I put the counting variable at the wrong place: 

> #!/usr/bin/python3
> import sys, time
> import RPi.GPIO as gpio
> 
> gpio.setmode(gpio.BOARD)
> gpio.setup(23, gpio.IN)
> count = 0
> def mein_callback(pin):
> count += 1
> print('PIR 1 aktiviert', count)
> return
> 
> try:
   count = 0
> gpio.add_event_detect(23, gpio.RISING, callback = mein_callback)
> while True:
> time.sleep(2)
   count += 1
> except KeyboardInterrupt:
> print('PIR deaktiviert')

Marcus.


-Ursprüngliche Nachricht-
Von: Mats Wichmann [mailto:m...@wichmann.us] 
Gesendet: Montag, 10. April 2017 15:15
An: marcus lütolf ; tutor@python.org
Betreff: Re: [Tutor] counting function calls

On 04/10/2017 01:55 AM, marcus lütolf wrote:
> Dear experts,
> I have written the following code for motion detection with a PIR 
> sensor with a function and I need to count how many times the funtion 
> is called, but I get a traceback:
> 
> #!/usr/bin/python3
> import sys, time
> import RPi.GPIO as gpio
> 
> gpio.setmode(gpio.BOARD)
> gpio.setup(23, gpio.IN)
> count = 0
> def mein_callback(pin):
> count += 1
> print('PIR 1 aktiviert', count)
> return
> 
> try:
> gpio.add_event_detect(23, gpio.RISING, callback = mein_callback)
> while True:
> time.sleep(2)
> except KeyboardInterrupt:
> print('PIR deaktiviert')
> 
> PIR 1 aktiviert
> Traceback (most recent call last):
>   File "./PIRex.py", line 9, in mein_callback
> count += 1
> UnboundLocalError: local variable 'count' referenced before assignment 
> ^CPIR deaktiviert
> 
> Tanks for help, marcus.

Yes, what Python does here may be surprising at first: if you only
read-access a global variable in a local (function in this case) scope, it
gives you the value just fine.  If you however try to save something to a
global variable what happens is it creates a local variable,
*unless* you have previously informed Python you mean the global one, by
using the global statement as Alan listed. The specific error you see is
because in order to increment 'count' (which Python has already figured out
has to be local because it will be assigned to) you have to read the
existing value first, but there is no existing value in the local scope.

The Python programming FAQ has a short explanation of why this might be so:

https://docs.python.org/2/faq/programming.html#what-are-the-rules-for-local-
and-global-variables-in-python



---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus

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


[Tutor] creating .json files

2017-04-13 Thread Rafael Knuth
Is there a way to split these two into separate steps:
a) creating a .json file
b) manipulating it (a, r, w ...)

Example:

"import json
number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
file_name = "my_numbers.json"
with open(file_name, "w") as a:
json.dump(number_list, a)

What if I just wanted to create a .json file and do nothing with it?

import json
file_name = "my_numbers.json"

The above does not do the job. What am I getting wrong here? Thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python help

2017-04-13 Thread Alan Gauld via Tutor
On 13/04/17 15:33, Christina Hammer wrote:

> I downloaded the newest version of Python on my windows computer and am
> having some trouble using it. I need to save my work because I am using it
> for an online class and am going to have to send it to my professor. But I
> cannot access a tool bar that would allow me to save it. I'm not sure if
> someone can help me with this.

Python is a programming language interpreter, and as such it
does not have a GUI. Instead you create your programs using
a separate program, usually a text editor like Notepad,
or sometjhing more powerful. From there you save the program
as a file ending with .py.

On Windows you can then get Python to execute that file by either
1) Double clicking in Windows explorer (but this often results
in the program starting, running and closing so quickly you
can't see what it did*)
2) Start a command console (CMD.EXE) and type

C:\WINDOWS> python myprogram.py


(*)You can force the program to pause by adding a line
input("Hit ENTER to continue")
in your code.
-

Most Python downloads simplify this process by including a
development tool called IDLE and you should have an entry
for that in your menus. IDLE presents a GUI into which
you can enter python commands or open a new edit window
to type your code. You can then save that as before. But
IDLE also includes menus for running your code from within
IDLE. If you search on YouTube you will find several short
tutorial videos showing how to get started with IDLE.

Here is one specifically for Windows...

https://www.youtube.com/watch?v=5hwG2gEGzVg


-- 
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] counting function calls

2017-04-13 Thread Alan Gauld via Tutor
On 13/04/17 17:10, marcus lütolf wrote:
> Dear experts, Mats
> I have found the solution, I put the counting variable at the wrong place: 

I don;t think so, what you have done now is count the times
through the loop, but thats not (always) the same as the
number of times the function gets called, which is what
you said you wanted to count..


>> #!/usr/bin/python3
>> import sys, time
>> import RPi.GPIO as gpio
>>
>> gpio.setmode(gpio.BOARD)
>> gpio.setup(23, gpio.IN)
>> count = 0
>> def mein_callback(pin):
>> count += 1

This line will still give you an error.

>> print('PIR 1 aktiviert', count)
>> return
>>
>> try:
>count = 0
>> gpio.add_event_detect(23, gpio.RISING, callback = mein_callback)
>> while True:
>> time.sleep(2)
>count += 1

This just counts how many times your while loop goes
round - once every 2 seconds. It says nothing about
how often the callback gets executed.

To do that you need to add the line

global count

to your callback function.

But then both the loop and function will increment
the global count variable so you need to remove
(or rename) the one in the loop.

-- 
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] python help

2017-04-13 Thread Qiao Qiao
If you are going to send it to your professor. Maybe just copy all your
result to a txt file and send the text?

Qiao

Qiao Qiao
Web Engineer

On Thu, Apr 13, 2017 at 10:33 AM, Christina Hammer <
hamme...@mail.montclair.edu> wrote:

> Hi,
> I downloaded the newest version of Python on my windows computer and am
> having some trouble using it. I need to save my work because I am using it
> for an online class and am going to have to send it to my professor. But I
> cannot access a tool bar that would allow me to save it. I'm not sure if
> someone can help me with this.
> Thank you
> Christina Hammer
> ___
> 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


Re: [Tutor] creating .json files

2017-04-13 Thread leam hall
On Thu, Apr 13, 2017 at 12:32 PM, Rafael Knuth 
wrote:

> Is there a way to split these two into separate steps:
> a) creating a .json file
> b) manipulating it (a, r, w ...)
>
> Example:
>
> "import json
> number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
> file_name = "my_numbers.json"
> with open(file_name, "w") as a:
> json.dump(number_list, a)
>
> What if I just wanted to create a .json file and do nothing with it?
>
> import json
> file_name = "my_numbers.json"
>
> The above does not do the job. What am I getting wrong here? Thanks.
>
> If you want to have a file after the program runs you need to "open" it.
And close it. Otherwise you reference a file but leave no trace.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python help

2017-04-13 Thread George Fischhof
Hi Christina,

you should use an editor or an IDE (Integrated Development Environment) (a
quite good and my favorite IDE is PyCharm
https://www.jetbrains.com/pycharm/download/#section=windows ), write the
script in it,then save,  then run it from the IDE or from command line with
similar command:
python script_name.py

BR,
George

2017-04-13 16:33 GMT+02:00 Christina Hammer :

> Hi,
> I downloaded the newest version of Python on my windows computer and am
> having some trouble using it. I need to save my work because I am using it
> for an online class and am going to have to send it to my professor. But I
> cannot access a tool bar that would allow me to save it. I'm not sure if
> someone can help me with this.
> Thank you
> Christina Hammer
> ___
> 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


Re: [Tutor] creating .json files

2017-04-13 Thread Alan Gauld via Tutor
On 13/04/17 17:32, Rafael Knuth wrote:
> Is there a way to split these two into separate steps:
> a) creating a .json file
> b) manipulating it (a, r, w ...)

Of course.

> What if I just wanted to create a .json file and do nothing with it?
> 
> import json
> file_name = "my_numbers.json"
> 
> The above does not do the job. What am I getting wrong here? Thanks.

The above creates a variable called file_name that stores a string.
It has nothing to do with any files.

You need to open the file to create it:

with open(file_name,'w') as json_file: pass

Will open a new file and immediately close it again.

You could do the same explicitly with

json_file = open(file_name,'w')
json_file.close()

Remember that variable names are just labels for your benefit.
The fact that you call it file_name means nothing to Python,
you might as well call it xcdseqplrtyg123 so far as Python is concerned,
its just a lablel. The name is only meaningful to
you (and possibly to other human readers).

Similarly, although your string looks like a file name to
a human reader, to Python it's just a string of characters.
Python cannot draw any meaning from that.

Finally, the code above creates a new file called my_numbers.json
But it is an empty file and is NOT a json file, despite the name.
It only becomes a json file once you add some json data to it.
open() creates text files, it has no understanding of what the
data you write to those files means.

HTH

-- 
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] Python 3.6 Multiply the elements of a 2D Array by the elements of a 1D Aeeay

2017-04-13 Thread Stephen P. Molnar
I am attempting to port a program that I wrote about 20 years ago from 
FORTRAN to Python.  So far, I have bumbled my way to the point that I 
can get all of the input data resulting from a quantum mechanical 
calculation of a very simple organic molecule in to a Python program, 
but am encountering problems with processing the data.


I have an list generated by:   s = np.linspace(start,finish,points)

and an array D:

 [ 0.  2.0598013.60937686  3.32591826  2.81569212]
 [ 2.0598010.  4.71452879  4.45776445  4.00467382]
 [ 3.60937686  4.71452879  0.  5.66500917  5.26602175]
 [ 3.32591826  4.45776445  5.66500917  0.  5.02324896]
 [ 2.81569212  4.00467382  5.26602175  5.02324896  0.]

Now I can multiply the Array by one element of the list:

s2 = 1.100334448160535050  (The first non-zero list element.)
s2_D = s2*np.array(D)

which results in:

 [ 0.  2.26647 3.97152169  3.65962243  3.09820303]
 [ 2.26647 0.  5.18755844  4.90503178  4.40648056]
 [ 3.97152169  5.18755844  0.  6.23340474  5.79438514]
 [ 3.65962243  4.90503178  6.23340474  0.  5.52725387]
 [ 3.09820303  4.40648056  5.79438514  5.52725387  0.]

I checked this, rather laboriously, in a spreadsheet.

However, what I want to do is multiply each element ob D by each element 
of s and sum all of the products.


I have Goggled quite a bit, but have not found anything too awfully useful.

Any pointers in the right direction will be much appreciated.

Thanks in advance.

--
Stephen P. Molnar, Ph.D.Life is a fuzzy set
www.molecular-modeling.net  Stochastic and multivariate
(614)312-7528 (c)
Skype: smolnar1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 3.6 Multiply the elements of a 2D Array by the elements of a 1D Aeeay

2017-04-13 Thread Steven D'Aprano
On Thu, Apr 13, 2017 at 03:41:09PM -0400, Stephen P. Molnar wrote:

> I have an list generated by:   s = np.linspace(start,finish,points)
> 
> and an array D:
> 
>  [ 0.  2.0598013.60937686  3.32591826  2.81569212]
>  [ 2.0598010.  4.71452879  4.45776445  4.00467382]
>  [ 3.60937686  4.71452879  0.  5.66500917  5.26602175]
>  [ 3.32591826  4.45776445  5.66500917  0.  5.02324896]
>  [ 2.81569212  4.00467382  5.26602175  5.02324896  0.]

Some advice for when you ask for help: we're volunteers, not paid to 
work on your problem, so the easier you make it for us, the more likely 
you will get a good answer.

Start off by simplifying the problem. Do you really need a 5x5 array 
with so many decimal places just to ask the question? Probably not. A 
2x2 array will probably demonstrate the question just as well, and be 
MUCH easier for us to read and type.

What code did you use to create D? It is better to show us that rather 
than just the output of what D looks like. I've tried various things, 
and I cannot duplicate the output that you show. That means I cannot 
tell if I'm working with the same data type as you.

My *guess* is that you did something like this to get D:

py> D = np.array([[1.5, 2.5], [3.5, 4.5]])
py> print D
[[ 1.5  2.5]
 [ 3.5  4.5]]

but I can't be sure, and of course I have no idea what the mystery list 
"s" contains, because you don't tell us. So I'm working in the dark.


> However, what I want to do is multiply each element ob D by each element 
> of s and sum all of the products.

Can you give us an example? This is ambiguous, it could mean either of 
*at least* two things.

(1) Take each element of D in turn, and multiply by the corresponding 
element of s. Here is an example:

D = [[ 1  2 ]
 [ 3  4 ]]

s = [10 20 30 40]

So you want:

1*10 + 2*20 + 3*30 + 4*40


(2) Take each element of D in turn, and multiply by each of the elements 
of s. Using the same examples for D and s:

( 1*10 + 1*20 + 1*30 + 1*40 + 
  2*10 + 2*20 + 2*30 + 2*40 + 
  3*10 + 3*20 + 3*30 + 3*40 + 
  4*10 + 4*20 + 4*30 + 4*40 )


Which do you want?

Remember, the better the question you ask, the better the answers we can 
give.



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