Display an image from a file in an IPython Notebook
from IPython.display import Image Image(filename=’test.png’)
In Python, how do I determine if an object is iterable?
try: some_object_iterator = iter(some_object) except TypeError, te: print some_object, ‘is not iterable’
try: _ = (e for e in my_object) except TypeError: print my_object, ‘is not iterable’
import collections
if isinstance(e, collections.Iterable): # e is iterable
http://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable
Best way to parse a URL query string
from urlparse import urlparse, parse_qs URL=’https://someurl.com/with/query_string?i=main&mode=front&sid=12ab&enc=+Hello’ parsed_url = urlparse(URL) parse_qs(parsed_url.query) {‘i’: [‘main’], ‘enc’: [’ Hello ‘], ‘mode’: [‘front’], ‘sid’: [‘12ab’]}
string format
content = u'''
<table><tbody>
<tr>
<td> 响应时间平均值 </td>
<td> {mean:.4f} </td>
</tr>
<tr>
<td> 响应时间75% </td>
<td> {seventy_five:.4f} </td>
</tr>
<tr>
<td> 响应时间95% </td>
<td> {ninety_five:.4f} </td>
</tr>
<tr>
<td> 可用性 </td>
<td> {ava} </td>
</tr>
</tbody></table>
'''.format(**{
'mean': df.mean()[0],
'seventy_five': df.quantile(.75)[0],
'ninety_five': df.quantile(.95)[0],
'ava': ava
})
Using % and .format() for great good!
How can I use Python to get the system hostname?
import socket
print(socket.gethostname())
import platform
platform.node()
http://stackoverflow.com/questions/4271740/how-can-i-use-python-to-get-the-system-hostname
Python: defaultdict of defaultdict?
defaultdict(lambda : defaultdict(int))
Python: defaultdict of defaultdict
get random string
Generating strings from (for example) lowercase characters:
import random, string
def randomword(length):
return ''.join(random.choice(string.lowercase) for i in range(length))
Results:
>>> randomword(10)
'vxnxikmhdc'
>>> randomword(10)
'ytqhdohksy'
近期评论