Skip to main content

Posts

Showing posts from November, 2016

Store data easily with Python

So, we want to make a program that stores some data we create on the fly when program is running and that we want to reuse when we open the program later? The shelve module is our module, then. This module creates a database and other 2 files,  First of all, let's import the module: import shelve but we just need to know that once we created an object like this s = shelve . open ( "todo.db" ) we can use s as a dictonary and preserve our data there. If we, for example add this code >>> a = input(What is your name?) >>> what is your name?John >>> s["name"] = a and then we close the file >>> s.close() if we close the editor. Then we open it again and reopen the db, >>> s = open("todo.py") if we do digit >>> print(s["name"]) we  will see this output John So, we stored that data in the db Let's make another example with a simple todo...

From Python to Exe (the fast way)

So, you've made your fabulous program (calculator.py, for example) in the charming Python code style and you want to make it accessible to other pc where there is not Python installed or that you want to make other people that have no familiarity with Python. So, you need and exe file. Here is what you have to do: 1) install py2exe pip install exe from the command line 2) create this setup.py file in a directory from distutils.core import setup import py2exe setup ( console = [ 'calculator.py' ]) 3) Right click on the directory and choose "open the cmd from here" and the digit.  python setup.py py2exe All the files you need will be created. What you need is the dist directory.. 4) To make it easier to find the file to click on, create a file .bat with this text inside (let's assume the exe is called calculator and that you  @echo off dist\calculator.exe and you're done!

Gui and Python

So, you want to make a gui with python? Let's start. First of all, we'll use the tkinter module that comes with python, so nothing to install. import tkinter as tk Then we create a class (app) that will create the frame into the window,  class app ( Frame ): def __init__ ( self ): tk . Frame . __init__ ( self ) self . option_add ( "*Font" , "arial 16 bold" ) self . pack ( expand = YES , fill = BOTH ) self . master . title ( "My first window" ) a label with written "Write down here" and an Entry text of a shining gold where I can write text for an input.... and that's all for now...  display = StringVar () l = tk . Label ( self , text = "Write down here" ) . pack () e = tk . Entry ( self , relief = RIDGE , textvariable = display , justify = 'left' , bd = 15 , ...

Jupyter to write nice equations

I have already mentioned jupyter notebook as a great way to program in python (this is what I use for making the code on this blog, and making this I re-discovered jupyter, as I was using sublime text 3 as editor, finding it very very useful and handy). Now, you can use jupyter not only for your code, but also for markdown... but I want to talk of this in particular... ho to write equations with markdown (it came to my mind doing the last post about equations)... So I want to let you see a little example that may want you to use it to display equations. This is what you write in the cell of jupyter in markdown mode: \begin{equation*} 1 + \frac{q^2}{1-q}+\frac{q^6}{(1-q^2)} \end{equation*} After this hit ctrl + enter and you'll get this pretty noce formula: 1 + q 2 1 − q + q 6 ( 1 − q 2 ) You have to deal with all those curly brakets and tag but I think it's a pretty nice way to show equations in jupyter. In case you want to know more go to  http://jupyter...

Python to solve equations

Python goes well with math, you know! So... how do you solve an equation of second degree? from math import sqrt x1 = 0 x2 = 0 def solve ( a = 0 , b = 0 , c = 0 ): global x1 , x2 'solve the equation' delta = ( b ** 2 ) - ( 4 * a * c ) print ( "Delta=" , delta ) if delta < 0 : print ( "There are no solutions, delta is negative" ) x1 = 'Nan' x2 = 'Nan' else : x1 = ( - b + sqrt ( delta )) / ( 2 * a ) x2 = ( - b - sqrt ( delta )) / ( 2 * a ) return x1 , x2 solve ( 2 , - 5 , - 3 ) # Change the parameters here print ( " \n x1 =" , x1 , " \n\n x2 =" , x2 ) Delta= 49 x1 = 3.0 x2 = -0.5

Create a test in html, through python

We start importing the random function and creating a file import random x = 0 file = open ( "1guerraMondiale.html" , "w" ) file . write ( "<h2>Test di Storia</h2>" ) Then we create a variable for the right answers and a tuple for the items of each question (you can change them, if you want) correttore = "Soluzione: " itemsletters = ( 'a) ' , 'b) ' , 'c) ' , 'd) ' ) Now we have the main function def test ( dom , pack ): global x global correttore global itemletters x += 1 esatta = pack [ 0 ] random . shuffle ( pack ) print ( x , dom ) # ===================== HTML ========================= file . write ( "<h3>" + str ( x ) + ") " + dom + "</h3>" ) file . write ( "<ol>" ) for i in pack : ...