Skip to content

Instantly share code, notes, and snippets.

View dmora's full-sized avatar

David Mora dmora

View GitHub Profile
@dmora
dmora / gist:6598382
Last active December 23, 2015 07:08
New Vagrant Machine recipe (python)
sudo apt-get update
sudo apt-get install git-core
sudo apt-get install python-pip python-dev build-essential
sudo apt-get install libxml2-dev
sudo apt-get install libxslt1-dev
sudo pip install --upgrade pip
sudo pip install virtualenvwrapper
sudo apt-get install libmysqlclient-dev
echo "export WORKON_HOME=$HOME/.virtualenvs" >> .bashrc
echo "export PROJECT_HOME={path to project}" >> .bashrc
/**
* @return bool
*/
public function close()
{
$result = TRUE;
if ($this->connected && !$this->persistent) {
try {
$result = $this->standalone ? fclose($this->redis) : $this->redis->close();
$this->connected = FALSE;
def _amount_residual(self, cr, uid, ids, name, args, context=None):
"""Function of the field residua. It computes the residual amount (balance) for each invoice"""
if context is None:
context = {}
ctx = context.copy()
result = {}
currency_obj = self.pool.get('res.currency')
for invoice in self.browse(cr, uid, ids, context=context):
nb_inv_in_partial_rec = max_invoice_id = 0
result[invoice.id] = 0.0
@dmora
dmora / gist:03391e00a327396eca97
Created March 9, 2015 01:26
Upload local file with python to filepicker using their REST API
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2
register_openers()
datagen, headers = multipart_encode({"fileUpload": open("filaname.jpg")})
request = urllib2.Request("https://www.filepicker.io/api/store/S3?key={yourKey}", \
datagen, headers)
response = urllib2.urlopen(request)
func main() {
.
.
<-done
// continue program execution
}
// allocate allocates jobs based on an array of resources to be processed by the worker pool
func (m *Manager) allocate(jobs []interface{}) {
defer close(m.jobs)
for i, v := range jobs {
job := Job{id: i, resource: v}
m.jobs <- job
}
}
// workerPool creates or spawns new "work" goRoutines to process the "Jobs" channel
func (m *Manager) workerPool(processor ProcessorFunc) {
defer close(m.results)
var wg sync.WaitGroup
for i := 0; i < m.numRoutines; i++ {
wg.Add(1)
go m.work(&wg, processor)
}
wg.Wait()
}
// work performs the actual work by calling the processor and passing in the Job as reference obtained
// from iterating over the "Jobs" channel
func (m *Manager) work(wg *sync.WaitGroup, processor ProcessorFunc) {
defer wg.Done()
for job := range m.jobs {
output := Result{job, processor(job.resource)}
m.results <- output
}
}
// Collect post processes the channel "Results" and calls the ResultProcessorFunc passed in as reference
// for further processing.
func (m *Manager) Collect(proc ResultProcessorFunc) {
for result := range m.results {
outcome := proc(result)
fmt.Printf("Job with id: [%d] completed, outcome: %s", result.job.id, outcome)
}
m.done <- true
}
func main() {
pool := workerpool.NewPool(3) // 3 goRoutines(workers)
pool.Start(resources, ResourceProcessor, ResultProcessor)
}