Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#46
January 8th, 2013, 01:52 PM
 Erents
Registered User

Join Date: Jan 2013
Posts: 3
Time spent in forums: 1 h 2 m 30 sec
Reputation Power: 0

Hi, I am a bit of a newbie and have a simple problem that I cannot solve so I have come here to get some help from the experts.

I am trying to count the number of gpio inputs per second. I have the code to count the number of inputs and I can count a second using time.time etc. I just cannot figure this out the code required to count inputs per second.

Any help would be appreciated!

Thanks,

Martin.

#47
January 30th, 2013, 03:21 PM
 Lucantrop
Registered User

Join Date: Dec 2012
Posts: 21
Time spent in forums: 17 h 15 m 41 sec
Reputation Power: 0
Hey, Martin, for starters, I'm pretty sure that this is not the right topic for asking questions, but since it's out here, why not answer it anyway - The math behind your problem is pretty simple as in total_inputs/total_time = inputs_per_second.
To get the inputs and time you need to:
Code:
```import time

start_time = time.time()
total_inputs = 0

# insert the code for inputs with total_inputs += 1 for every input
# probably best in a while loop

total_time = time.time() - start_time

def gpio_per_sec(inputs, total_time):
input_per_sec = inputs/time
print inputs_per_sec # optional

gpio_per_sec(inputs, total_time)```
And that should do it.

#48
February 20th, 2013, 01:25 PM
 PythonNewb
Registered User

Join Date: Feb 2013
Posts: 15
Time spent in forums: 1 h 54 m 19 sec
Reputation Power: 0
Thanks for the tips guys!

#49
March 12th, 2013, 03:17 AM
 aleque
Registered User

Join Date: Mar 2013
Posts: 9
Time spent in forums: 1 h 50 m 7 sec
Reputation Power: 0

Can someone please recommend me a good Python book? It must cover the most important parts of Python and appeal to a beginner like me. There are lots of books out there and it is hard to chose, so I trust the suggestions on this forum

#50
May 7th, 2013, 06:43 AM
 johnmacklen
Temporarily Banned

Join Date: Apr 2013
Posts: 11
Time spent in forums: 3 h 13 m 33 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
There are many tips and tricks you can learn in Python:

There are many tips and tricks you can learn in Python:

Strings:
*Triple quotes are an easy way to define a string with both single and double quotes.
*String concatenation is expensive. Use percent formatting and str.join() for concatenation:

Code:
```print "Spam" + " eggs" + " and" + " spam"               # DON'T DO THIS
print " ".join(["Spam","eggs","and","spam"])            # Much faster/more
# common Python idiom
print "%s %s %s %s" % ("Spam", "eggs", "and", "spam")   # Also a pythonic way of
# doing it - very fast```

Module choice:
*cPickle is a faster, C written module for pickle. cPickle is used to serialize python program. Other modules have C implementations as well, cStringIO for the StringIO module, and cProfile for the profile module.
Code:
`import cPickle # You may want to import it as P for convenience.`

*These can even fall back to the slower pure-python version if they fail to import.

Code:
```try:
import cPickle as pickle
except ImportError:
import pickle```

Some more tips & tricks on next posts...

Thank You...!!!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Tips, tricks, inspiration, etc