Tuesday, March 21, 2017

Shelve - "Tietokannat"

#Tiedosto joka käyttäytyy tietokannan tavoin
#Ei oikea tietokanta. Käy kehitykseen ja pikku projekteihin

import shelve
a_list = ['One', 'Two', 'Buckle', 'my', 'shoe']
b_list = ['Yksi', 'Kaksi', 'Kolme', 'Neljä', 'Viisi']
dbase = shelve.open('tiedosto')
dbase['rhyme'] = a_list
dbase['numerot'] = b_list
b_list = dbase['numerot']
print(b_list)

Python: Print

#Tulostuksen muotoilu
#, end=' ' -poistaa rivinvaihdon jolloin kirjoitetaan samalle riville.



individuals = ('Yksi kaksi kolme','pekka irma kalle')

for record in individuals :
    columns = record.split()
    print(columns[0].ljust(10,'_'), end=' ')
    print(columns[1].ljust(10, '_'), end=' ')
    print(columns[2].rjust(10))

#voit ohjata tulostuksen virhe streamiksi
import sys
print('Error', file=sys.stderr)

Sunday, December 18, 2016

Windows Serial communication

http://forum.arduino.cc/index.php?topic=18371.0

"
You can bypass the DTR reset issue with the Arduino on PySerial by editing the serialWin32.py file in the python directory. It's usually located in lib\site-packages\serial.

Delete the compiled version of the file (serialWin32.pyc) and then edit the serialWin32.py file, changing the line that reads:
self._dtrState = win32.DTR_CONTROL_ENABLE
to:
self._dtrState = win32.DTR_CONTROL_DISABLE

This should now open the serial port without altering the DTR state.

I created a new module by copying the serial directory and renaming it serialArduino. That way, I could change my import serialArduino as serial without affecting other applications that might be expecting the serial module to operate with dtrState = DTR_CONTROL_ENABLE

"

Tuesday, June 7, 2016

Pythonista EXE

https://mborgerson.com/creating-an-executable-from-a-python-script


Installing PyInstaller

Note: Before installing PyInstaller on Windows, you will need to install PyWin32. You do not need to do this for GNU/Linux or Mac OS X systems.
PyInstaller can be installed using Pip, the Python package manager.
pip install pyinstaller

Building the Executable

Now, build the executable.
pyinstaller.exe --onefile --windowed app.py


Sunday, April 10, 2016

Kivy: Muokkaa KV:ta Pythonista

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.clock import Clock
import time


kv = '''
#:import aika time

kelloLabel:    id: koe    ##text: aika.strftime('%H:%M:%S', aika.localtime())

'''
class kelloLabel(Label):


    def __init__(self, **kwargs):
        super(kelloLabel, self).__init__(**kwargs)
        self.text = time.strftime('%H:%M:%S', time.localtime())
        #self.update()

    def update(self,x):
        print(time.strftime('%H:%M:%S', time.localtime()))
        self.text = time.strftime('%H:%M:%S', time.localtime())



class Kokeilu2App(App):

    def build(self):
        klabel = Builder.load_string(kv)
        Clock.schedule_interval(klabel.update, 1)
        return klabel

Kokeilu2App().run()

Saturday, April 9, 2016

Python: Time

import time

#Haetaan aika. Kun muuttujaa ei ole, niin metodi käyttää 
#automaattisesti time.time()localtime = time.localtime()
print(localtime)

'''Tällä muotoillaan aika järkevään muotoon,
Y = vuosi
m = kk
d = päivä
U = viikko
w = päivämäärä
H = tunti
M = minuutti
S = Sekuntti'''

print(time.strftime('%H:%M', localtime))

Tkinter: Message Box

#https://www.youtube.com/watch?v=IB6VkXJVf0Y&index=12&list=
#PL6gx4Cwl9DGBwibXFtPtflztSNPGuIB_d

from tkinter import *
import tkinter.messagebox

root = Tk()

#tkinter.messagebox.showinfo('Tää on huuteluikkuna', 'Hoi hoi Message')
root.update() #ilman tätä messagebox jää pyörimään ruudulle?
answer = tkinter.messagebox.askquestion('Kyssäri 1', 'Kyllä tai ei?')

if answer == 'yes':
    print(';-)')
elif answer == 'no':
    print('quit')
    root.destroy() #jostakin syystä quit ei toiminut, piti tehdä destroy
#ahaa siksi kun quit lopettaa mainloopin kokonaan


root.mainloop()