Python readings – Euler project #1

I recently found a website called Eulerproject.net. I found this while I was searching for “How to become an expert in Python programming language”. It stackoverflow there was a similar question posted “Python progression path – From apprentice to guru” . In the discussion there were bunch of  ideas that interested me. One of them is to solve the Eulerproject.net puzzles.

I had a look at the first problem and have tried to solve it the python way. I will continue posting the my solution and also the solution provided as part of overview.


#!/usr/bin/python
""" https://projecteuler.net/problem=1 """
"""
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
"""
sum = 0
for i in range(1000):
if i%3 == 0 or i%5 == 0:
sum += i
print "The sum is :", sum

Python readings – Process and Queue

#!/usr/bin/python
from multiprocessing import Process, Queue
import time
import sys

def Worker(queue):
    
    while(True):
        if not queue.empty():
            msg = queue.get()
            print msg    
        else:
            time.sleep(1)
            sys.stdout.write('#')
            sys.stdout.flush()



if __name__ == "__main__":
    queue = Queue()
    worker = Process(target=Worker, args=((queue),))
    worker.daemon = True
    worker.start()
        
    while(True):
        mesg = raw_input("Enter to print:")
        queue.put(mesg)

Although the output does not seem to give any meaningful understanding the way it is printed, just to give a
a rough idea about the flow. The code waits for user input until “enter” is pressed.
Meanwhile the worker thread keeps checking for queue messages and when empty prints “#” and wait for a second
WHen the enter is pressed the user input is fed to the queue and it is printed by the worker process.

Enter to print:##AAAA#
Enter to print:#AAAA
######

Fun with OpenSCAD and SolidPython

I have been thinking for a long time to get hands on with OpenSCAD. It is a programmers Solid 3D CAD modeller. Part of my python readings, I was looking for different fields or area of interest where python can be used and went through something called SolidPython. It  is a python frontend for solid modelling that compiles to OpenSCAD. Cool I quickly installed both the packages.


sudo apt-get install openscad

sudo pip install solidpython

I played around an example code and got a cube working. The code is here I will explain how I created the model
end of the blog

#! /usr/bin/env python
# -*- coding: UTF-8 -*-
from __future__ import division
import os, sys, re

from solid import *
from solid.utils import *
from solid import screw_thread

def assembly():
    c = cube(10)
    return  c

if __name__ == '__main__':
    out_dir = sys.argv[1] if len(sys.argv) > 1 else os.curdir
    file_out = os.path.join( out_dir, 'cube.scad')

    a = assembly()
    print("%(__file__)s: SCAD file written to: \n%(file_out)s"%vars())
    scad_render_to_file( a, file_out, include_orig_code=True)

frame00000
I will explain how I created the cube model as shown above.
Save the python code and execute it as follows

$ python cube.py
cube.py: SCAD file written to:
./cube.scad

This will generate cube.scad file.

Now run the OpenSCAD and open the cube.scad and type ‘F5′ or in the menu Design->Compile.
You will get this output.
OpenSCAD