comparison scripts/stacked_dae/utils.py @ 144:c958941c1b9d

merge
author XavierMuller
date Tue, 23 Feb 2010 18:16:55 -0500
parents 7d8366fb90bf
children
comparison
equal deleted inserted replaced
143:f341a4efb44a 144:c958941c1b9d
1 #!/usr/bin/python
2
3 from jobman import DD
4
5 # from pylearn codebase
6 def update_locals(obj, dct):
7 if 'self' in dct:
8 del dct['self']
9 obj.__dict__.update(dct)
10
11 def produit_croise_jobs(val_dict):
12 job_list = [DD()]
13 all_keys = val_dict.keys()
14
15 for key in all_keys:
16 possible_values = val_dict[key]
17 new_job_list = []
18 for val in possible_values:
19 for job in job_list:
20 to_insert = job.copy()
21 to_insert.update({key: val})
22 new_job_list.append(to_insert)
23 job_list = new_job_list
24
25 return job_list
26
27 def test_produit_croise_jobs():
28 vals = {'a': [1,2], 'b': [3,4,5]}
29 print produit_croise_jobs(vals)
30
31
32 # taken from http://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage-in-python
33 """Simple module for getting amount of memory used by a specified user's
34 processes on a UNIX system.
35 It uses UNIX ps utility to get the memory usage for a specified username and
36 pipe it to awk for summing up per application memory usage and return the total.
37 Python's Popen() from subprocess module is used for spawning ps and awk.
38
39 """
40
41 import subprocess
42
43 class MemoryMonitor(object):
44
45 def __init__(self, username):
46 """Create new MemoryMonitor instance."""
47 self.username = username
48
49 def usage(self):
50 """Return int containing memory used by user's processes."""
51 self.process = subprocess.Popen("ps -u %s -o rss | awk '{sum+=$1} END {print sum}'" % self.username,
52 shell=True,
53 stdout=subprocess.PIPE,
54 )
55 self.stdout_list = self.process.communicate()[0].split('\n')
56 return int(self.stdout_list[0])
57