partyzan

Nompy: a neat calorie calculator

Posted by | English, Programming, Python | No Comments

Nompy 0.1Currently I’m trying to improve my diet, and calculating calories of meals became kind of annoying. Then a great idea formed, why not make a neat program to do most of the annoying stuff for me? Python was the obvious choice at the time, since I’ve been doing a lot of Python courses in the past, and all the scripts I wrote didn’t really serve any practical purpose. It was time I created something useful, and that’s how Nompy came to existence. It’s a project that I’m doing to improve my skills as a programmer, but at the same time, I’ll be using its features on a daily basis.

So as I started brainstorming on what technologies I’ll have to use. I knew two things:

– I needed a way to store all the nutritional facts, that food packages have listed
– I had to decide on what graphical user interface library I’m going to learn

For storing nutritional facts, I discovered YAML, and really liked the concept behind it. I wanted the data to be easily readable and editable by users, and it would work great for that purpose. But later I changed my mind, as I wanted to use as little external libraries as possible, and I learned that JSON was something I’d need to use at some point one way or another. Entering data in JSON format by hands is something I wouldn’t expect of an end user, so I plan on making a GUI feature in my application somewhere down the road.

As goes for GUI library, the only one that’s part of Python is Tkinter, but I don’t like the spartan look it has. I’ve always been a fan of Qt, and how the applications built on it looked awesome on all platforms (Windows, OSX and Linux). I knew I wanted to use it, but the next question was on which project to use, PyQt or PySide? After a brief googling session I came up with a few sources that described the current situation of both projects. PyQt has a larger user base, and is actively developed unlike PySide. PySide has one huge plus, it doesn’t have a limiting license like PyQt does. While PyQt is completely free for all open-source projects, Riverbank software charges about 480 euros for a commercial license. At the end I chose PyQt anyways, because the deciding factor was simply the fact, that it already supports Qt5, while PySide is still stuck with Qt4. I’m pretty sure that porting an application of this size to PySide 2 shouldn’t be a problem if it ever matures.

nompy01_interfaceAfter doing some research, I knew I wanted to separate my back end algorithms from UI logic. Designing an interface in Qt Designer is kind of straightforward, although widget alignment and their relative size can be challenging to configure. Designer generates an XML file with an extension of “.ui”, which can then be translated to Python by using pyuic5 tool bundled with PyQt. I created a third Python script, that links the PyQt and my back end. So this is the structure of files I ended up with:

– nompy.py (bridge between core and interface)
– nompy_core.py (back end)
– nompy_interface.py (PyQt5 project)

Script pulls data from two files, ingredients.json and dishes.json. Ingredients JSON contains nutritional facts for each ingredient, and dishes JSON contains ingredient names and their amount for every dish, so it’s basically a recipe.

Dish example:

{
	"Kosmiči z mlekom": {
		"Crispy z ječmenovo vlaknino" 	: 30,
		"Alpsko mleko (3,5)"			: 2
	}
}

And now its ingredients:

{
	"Crispy z ječmenovo vlaknino": {
		"unit"         : "gram",
		"amount"       : 10,
		"fat"          : 0.17,
		"saturatedFat" : 0.04,
		"carbs"        : 7.93,
		"fiber"        : 0.7,
		"protein"      : 0.65,
		"salt"         : 0.091,
		"iron"         : 0,
		"calories"     : 37.3
	},

	"Alpsko mleko (3,5)": {
		"unit"         : "deciliter",
		"amount"       : 1,
		"fat"          : 3.5,
		"saturatedFat" : 2.3,
		"carbs"        : 4.7,
		"fiber"        : 0,
		"protein"      : 3.3,
		"salt"         : 0.12,
		"iron"         : 0,
		"calories"     : 64
	}
}

Currently adding multiple same dishes to the list isn’t intuitive (e.g. if I want to eat two sandwiches, I have to add a sandwich dish twice), so I plan on making some interface changes in version 0.2. It’ll also have a search function, and it will display stats for every selected dish immediately.

If you want to give it a try, here is an archive of the script. You’ll have to have Python 3 and PyQt5 installed, then you can run nompy.py. It’s licensed under GNU GPLv3 license so feel free to modify any part of the script.

median exercise on Codecademy Python course

Posted by | English, Programming, Python | No Comments

I recently wanted to test out my fresh Python skills by doing a Codecademy Python course. I really like the way they explain things and even a complete beginner can start comfortably not just by reading trough pages of documentation, but by seeing examples and putting them to use on their online interpreter. I’ll be doing other courses they have over there for sure, and I highly recommend it, if you’re exploring a new language or are completely new to programming.

During a final exercise called median (under Loops / Practice Makes Perfect) I stumbled upon a first anomaly that I met up until now. The instructions are as follows:

Write a function called median that takes a list as an input and returns the median value of the list.

For example: median([1,1,2]) should return 1.

  1. The list can be of any size and the numbers are not guaranteed to be in any particular order.
  2. If the list contains an even number of elements, your function should return the average of the middle two.

I wrote the function, but line of code that calculated the average of two numbers didn’t quite work:

return (int(sorted_lst[0]) + int(sorted_lst[1])) / 2

Read More

Coding!

Posted by | English, Programming | No Comments

I’ve been diving in to the world of programming for about a year, as a hobby. Not having a job has its perks too, I’m trying to make out the most of the free time I have now by learning new things. So I started with C and C++, and learned quite a lot about embedded systems as well. It hasn’t been easy, but I feel like, I’m slowly making some progress. I’ve done some projects on a ARM Cortex M4 based board, and found the process of low-level tinkering very satisfying. Props go to my girlfriend for guidance and support, she has a lot of field experience and knows winIDEA inside out. I also wrote a few lines in Python a few months ago and fell in love with it.

So I’ve decided to to start blogging on this matter here in English. My posts will be about things that I find interesting in the process of learning and projects I’ll be working on in the future.

Stay tuned! :)

Veseli december

Posted by | Fotografija, Video | No Comments

Kratka posodobitev o vseh stvareh, ki sem jih počel ta mesec, bilo je zabavno in polno ustvarjalnosti.

Posnel sem video, ki je mišljen kot šala na temo kako priti do več ogledov vsebine s poceni triki. Preprosto, dodaj mačko (ali več le-teh) in profitiraj! Ideja je Anjina, maček pa Urškin.

Posneto na horuk zvečer, naslednje jutro je že tole pristalo na YouTubu. Recept: velika rola belega papirja, tri (pre)šibke fluorescentne luči z različno temperaturo svetlobe, iSYSTEM izdelki, mačja meta in Bacek. Set je izgledal takole:20141208_212103Fantje pri Asyst Electronic so veseli, čas zapravljen kvalitetno v dobri družbi. :)

Za konjeniško društvo Pesnica sem fotografiral dva dogodka, Božičkovanje na KD Pesnica in Štefanovo (blagoslov konj) v Pernici. Na prvem sem imel bitke z bliskavico in manjkajočimi stropi, na drugem pa z neugodnimi zimskimi kontrasti. Polarizacijski filter mi ne bi škodil pri tako nizkem soncu.

IMG_0325 IMG_0683 IMG_0474 IMG_0737

Z novim letom pa novim izzivom naproti. Srečno!

 

Zadnji meseci…

Posted by | Fotografija, Življenje | No Comments

Zadnje mesece se borim z iskanjem zaposlitve, ampak vseeno ta čas skušam izkoristiti za izpopolnjevanje svojih znanj. Konec februarja sem bil povabljen na fotografiranje dogodka P.R.A.V.I. v kristalni palači kjer sem bil prijetno presenečen, ko je novinarka časopisa Delo vprašala če lahko odkupi moje delo za objavo v nekaj člankih. Z nekaj pogajanja se je nekaj fotografij pojavilo na spletu in časopisu: Mladi pred delodajalce z idejami, Delo je že mogoče najti, kaj pa pošteno plačilo?, PDF tiskane izdaje. Za tem sem še nekaj drugih fotografij zasledil na ostalih spletnih straneh (Facebook, spletna stran talentiran.si, osebni blog Anje Perše). Ogromna zahvala gre Taju, brez njegove bliskavice bi bilo delo bistveno težje. Takoj, ko bodo finance dopuščale si bom privoščil Canon 600EX-RT.

Ostala dva meseca je nastalo nekaj osebnih galerij za katere se lahko zahvalim Anji, ki me v tem obdobju uspešno zabava z dogodivščinami, ki so vredne dokumentiranja. Portret Christiana iz Švice je nastal v Mariboru tik preden smo šli drsat v Ledno dvorano. Osvetlila ga je Anja kar z LED lučjo na mobilnem telefonu. Izkoristila sva, kar je bilo pri roki :).

Marca smo z Janjo in Sašem šli pohajkovat v Podpeč. Super druženje, ravno prav, da sem se nadihal svežega zraka po tem, ko sem prebolel bolezen. Sicer fizično naporno za stanje v katerem sem bil, ampak mi je dobro delo. Isti dan je nastala fotografija ob sončnem zahodu iz Konja (pri Litiji). Prejšnji teden sem eno popoldne izkoristil za fototep po Ljubljani. Prelomljeno srce na poti na Ljubljanski grad je klical po enem bolj romantičnem prizoru :)

Broken heart IMG_2807

SSD namesto optične enote v prenosniku, prenos sistema in symbolic linki na OSX

Posted by | Tehnologija | No Comments

hddcaddy_engrishŽe dolgo časa sem se poigraval z idejo, da bi optično enoto v svojem starem MacBook Pro zamenjal z SSDjem, a so bile prioritete finančnih vložkov drugje. Ko sem si za namizni računalnik kupil večji in hitrejši SSD (Samsung 830 serija) je stari Intelov X25-M 80GB SSD večinoma ostajal neuporabljen. Pred kakšnim tednom sem za hec preveril cene adapterjev oz. nosilcev (Second Drive Caddy) za moj model prenosnika na nemškem eBayu in našel kar nekaj izdelkov po presenetljivo nizki ceni. Naklikal sem tega, 13€ skupaj s poštnino je stal. Par dni kasneje me je kos aluminija že čakal v poštnem nabiralniku.

ssdinacaddyFantje pri iFixit so pripravili preprost vodič za vgradnjo, tako da sem se (pre)pogumno lotil opravila kar sam. Nasvet za vse, ki se spuščate v to: potrebujete dober mali križni izvijač. Ta, ki je priložen mi je povzročil nemalo preglavic in je popolnoma neuporaben. Če ni namagneten je dobro imeti pri roki tudi kak manjši magnetek, da se dvo-milimetrski vijaki ne zakotalijo v špranje prenosnika.

Po tem, ko sem s težavo odstranil 10 vijakov se je zataknilo pri zadnjemu, katerega glavo mi je kljub pazljivosti na pogled uspelo uničit. Ko sem že skoraj obupal nad tem mi je na pomoč priskočila punca, ki je iz službe prinesla dva kvalitetna izvijača. Mislil sem, da bom lahko samo še ostale privil nazaj in pogoltnil teh 13€, a mi je začuda uspelo odviti še tistega zadnjega “uničenega” brez vsakršnega problema … še lažje je šel ven kot vsi ostali s priloženim izvijačem. Sledila je brezhibna vgradnja in privijanje vijakov … tokrat z užitkom. Anja in Istok 12-02-14 at 17.18En high-five z Anjo kasneje sem že potrdil, da je bila vgradnja uspešna, ko je OSX videl dodaten SSD, še večje veselje pa je bilo ko sva ugotovila da kamera iSight in Bluetooth modul delujeta brez težav (en od konektorjev, ki ga je bilo potrebno odklopit je izredno občutljiv in nekateri uporabniki so imeli težave s kontaktom po vgradnji).

Ker se mi ni dalo ukvarjati s ponovno namestitvijo OSXa sem enostavno kloniral vsebino starega diska na SSD s pomočjo aplikacije Carbon Copy Cloner. Načeloma je plačljiva, a je za potrebe enkratnega kloniranja 30-dnevna preizkusna različica dovolj. Po končanem postopku sem najprej preveril, če se sistem zažene brez težav z SSDja in sicer tako, da sem držal tipko Alt med zagonom računalnika in izbral SSD. Problemov ni bilo nobenih, tako, da sem lahko brez slabe vesti nastavil privzeti zagonski disk na SSD (System Preferences > Startup Disk) in omogočil TRIM:

sudo perl -pi -e 's|(\x52\x6F\x74\x61\x74\x69\x6F\x6E\x61\x6C\x00{1,20})[^\x00]{9}(\x00{1,20}\x54)|$1\x00\x00\x00\x00\x00\x00\x00\x00\x00$2|sg' /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage
sudo touch /System/Library/Extensions/

Screenshot 2014-02-13 17.50.40Po ponovnem zagonu sistema sem preveril, če TRIM deluje tako, da sem odklikal na Apple logotip v levem zgornjem kotu > About This Mac > More Info… > System Report > SATA / SATA Express > model mojega SSDja > TRIM Support: Yes. Stari trdi disk sem formatiral. Ta mi bo od zdaj naprej služil kot shramba za končane videe in backup fotografij.

Zadnja stvar, ki sem jo želel urediti so bile privzete mape z multimedijsko vsebino. Ker je star SSD relativno majhen sem na mesto starih map (Downloads, Movies, Music, Pictures) ustvaril simbolične povezave (symbolic link), ki kažejo na star trdi disk. Najprej sem skopiral vsebino vseh teh map na stari (formatiran) disk in v terminalu pobrisal sistemske mape, na njihovem mestu pa ustvaril symlink:

megumi:~ partyzan$ chmod -N ~/Movies
megumi:~ partyzan$ rm -rf ~/Movies
megumi:~ partyzan$ ln -s /Volumes/Visnja/Movies/ ~/Movies

Screenshot 2014-02-13 17.29.31Vse zgoraj naštete mape sem “potegnil” nazaj v stranski “Favorites” meni Finderja, tako da zame in vse sistemske aplikacije ni prav nobene razlike zdaj, ko so datoteke na drugi lokaciji. Projektnih datotek Final Cuta, Aperture, Lightrooma in podobnih ni potrebno ponovno nastavljati s to metodo. Edina hiba tega postopka je kozmetične narave, v Favorites meniju sem izgubil stare ikone map, vse so generične. Stare ikone lahko na novi lokaciji sicer spremenim, a se te ne pokažejo v stranskem meniju in na mestu bljižnic v Home mapi.

No, s tem manjšim “hackom” sem naredil računalnik precej odzivnejši in upam da mi bo služil še kakšno leto ali več. Za osnovna opravila tudi po vseh teh letih še vedno zadostuje :)

Pa še eno opozorilo za vse, ki vgrajujete novejše SSD enote v novejše MacBooke: Pri SATA3 hitrostih je priporočljivo, da je navaden trdi disk v HDD Caddyju, SSD pa na njegovem prvotnem mestu zaradi hitrosti vodila, ki je namenjen optični enoti. Pa srečno!