### Thread: Tips, tricks, inspiration, etc

Page 4 of 4 First ... 234
1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
3
Rep 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.
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
34
Rep Power
7
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.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
15
Rep Power
0
Thanks for the tips guys!
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
9
Rep 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
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
13
Rep 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...!!!
6. No Profile Picture
Banned
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2011
Posts
7
Rep Power
0

#### Re:Tips, tricks, inspiration, etc

Hopefully this thread will be able to help any Pythoner. Whether it be a complete beginner or a very advanced expert.
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
2
Rep Power
0

8. List comprehensions work differently with Python2 and Python3 ...
python Code:
```''' cheese_list1.py
print the items in a list in sorted order using list comprehension
tested with Python33 (Python27 gives error)
'''

cheese_list = [
'Feta', 'Limburger', 'Camembert', 'Roquefort', 'Edam',
'Jarlsberg', 'Golka', 'Garrotxa', 'Swiss', 'Quesillo',
'Emmentaler', 'Appenzeller', 'Raclette', 'Asiago', 'Zuvi',
'Ricotta', 'Mozzarella', 'Munster', 'Parmesan']

[print(cheese) for cheese in sorted(cheese_list)]

''' result ...
Appenzeller
Asiago
Camembert
Dubliner
Edam
Emmentaler
Feta
Garrotxa
Golka
Gorgonzola
Gouda
Jarlsberg
Limburger
Liptauer
Mozzarella
Munster
Parmesan
Quesillo
Raclette
Ricotta
Roquefort
Swiss
Zuvi
'''```
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2013
Posts
6
Rep Power
0
Wow I found so many useful tips on this thread, thanks!
10. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
34
Rep Power
7
Originally Posted by Dietrich
List comprehensions work differently with Python2 and Python3 ...
python Code:
```''' cheese_list1.py
print the items in a list in sorted order using list comprehension
tested with Python33 (Python27 gives error)
'''

cheese_list = [
'Feta', 'Limburger', 'Camembert', 'Roquefort', 'Edam',
'Jarlsberg', 'Golka', 'Garrotxa', 'Swiss', 'Quesillo',
'Emmentaler', 'Appenzeller', 'Raclette', 'Asiago', 'Zuvi',
'Ricotta', 'Mozzarella', 'Munster', 'Parmesan']

[print(cheese) for cheese in sorted(cheese_list)]

''' result ...
Appenzeller
Asiago
Camembert
Dubliner
Edam
Emmentaler
Feta
Garrotxa
Golka
Gorgonzola
Gouda
Jarlsberg
Limburger
Liptauer
Mozzarella
Munster
Parmesan
Quesillo
Raclette
Ricotta
Roquefort
Swiss
Zuvi
'''```
And for anyone wondering, this happens because print was made into a function in python3, while in python2 it is a statement.
To do the same in python27:
python Code:
```list = ['b', 'a', 'e', 'c', 'd']
print '\n'.join([i for i in sorted(list)])

'''
result:
a
b
c
d
e
'''```
11. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
6
Rep Power
0
Many thanks for the tips guys!
Page 4 of 4 First ... 234