The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Page 4 -
Tips, tricks, inspiration, etc
Page 4 - Discuss Tips, tricks, inspiration, etc in the Python Programming forum on Dev Shed. Tips, tricks, inspiration, etc Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 8th, 2013, 01:52 PM
|
|
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.
|

January 30th, 2013, 03:21 PM
|
|
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.
|

February 20th, 2013, 01:25 PM
|
|
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!
|

March 12th, 2013, 03:17 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 9
Time spent in forums: 1 h 50 m 7 sec
Reputation Power: 0
|
|
Awesome links. Thanks!
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 
|

May 7th, 2013, 06:43 AM
|
|
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:
(but don't worry about this unless your resulting string is more than 500-1000 characters long)
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...
please keep in touch ...
Thank You...!!! 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|